1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
4 * PWM controller chips from G762 family, i.e. G762 and G763
6 * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
8 * This work is based on a basic version for 2.6.31 kernel developed
9 * by Olivier Mouchet for LaCie. Updates and correction have been
10 * performed to run on recent kernels. Additional features, like the
11 * ability to configure various characteristics via .dts file or
12 * board init file have been added. Detailed datasheet on which this
13 * development is based is available here:
15 * http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
17 * Headers from previous developments have been kept below:
19 * Copyright (c) 2009 LaCie
21 * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
23 * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
24 * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
26 * g762: minimal datasheet available at:
27 * http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
30 #include <linux/device.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/jiffies.h>
34 #include <linux/i2c.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
37 #include <linux/err.h>
38 #include <linux/mutex.h>
39 #include <linux/kernel.h>
40 #include <linux/clk.h>
42 #include <linux/platform_data/g762.h>
44 #define DRVNAME "g762"
46 static const struct i2c_device_id g762_id
[] = {
52 MODULE_DEVICE_TABLE(i2c
, g762_id
);
55 G762_REG_SET_CNT
= 0x00,
56 G762_REG_ACT_CNT
= 0x01,
57 G762_REG_FAN_STA
= 0x02,
58 G762_REG_SET_OUT
= 0x03,
59 G762_REG_FAN_CMD1
= 0x04,
60 G762_REG_FAN_CMD2
= 0x05,
63 /* Config register bits */
64 #define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */
65 #define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */
66 #define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */
67 #define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */
68 #define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */
69 #define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04
70 #define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */
71 #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
73 #define G761_REG_FAN_CMD2_FAN_CLOCK 0x20 /* choose internal clock*/
74 #define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */
75 #define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04
76 #define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */
77 #define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01
79 #define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */
80 #define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */
82 /* Config register values */
83 #define G762_OUT_MODE_PWM 1
84 #define G762_OUT_MODE_DC 0
86 #define G762_FAN_MODE_CLOSED_LOOP 2
87 #define G762_FAN_MODE_OPEN_LOOP 1
89 #define G762_PWM_POLARITY_NEGATIVE 1
90 #define G762_PWM_POLARITY_POSITIVE 0
92 /* Register data is read (and cached) at most once per second. */
93 #define G762_UPDATE_INTERVAL HZ
96 * Extract pulse count per fan revolution value (2 or 4) from given
97 * FAN_CMD1 register value.
99 #define G762_PULSE_FROM_REG(reg) \
100 ((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
103 * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
106 #define G762_CLKDIV_FROM_REG(reg) \
107 (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \
108 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
111 * Extract fan gear mode multiplier value (0, 2 or 4) from given
112 * FAN_CMD2 register value.
114 #define G762_GEARMULT_FROM_REG(reg) \
115 (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \
116 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
119 struct i2c_client
*client
;
124 struct mutex update_lock
;
126 /* board specific parameters. */
129 /* g762 register cache */
131 unsigned long last_updated
; /* in jiffies */
133 u8 set_cnt
; /* controls fan rotation speed in closed-loop mode */
134 u8 act_cnt
; /* provides access to current fan RPM value */
135 u8 fan_sta
; /* bit 0: set when actual fan speed is more than
136 * 25% outside requested fan speed
137 * bit 1: set when no transition occurs on fan
140 u8 set_out
; /* controls fan rotation speed in open-loop mode */
141 u8 fan_cmd1
; /* 0: FG_PLS_ID0 FG pulses count per revolution
142 * 0: 2 counts per revolution
143 * 1: 4 counts per revolution
144 * 1: PWM_POLARITY 1: negative_duty
146 * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
147 * 00: Divide fan clock by 1
148 * 01: Divide fan clock by 2
149 * 10: Divide fan clock by 4
150 * 11: Divide fan clock by 8
151 * 4: FAN_MODE 1:closed-loop, 0:open-loop
152 * 5: OUT_MODE 1:PWM, 0:DC
153 * 6: DET_FAN_OOC enable "fan ooc" status
154 * 7: DET_FAN_FAIL enable "fan fail" status
156 u8 fan_cmd2
; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
161 * 4: Mask ALERT# (g763 only)
166 * Convert count value from fan controller register (FAN_SET_CNT) into fan
167 * speed RPM value. Note that the datasheet documents a basic formula;
168 * influence of additional parameters (fan clock divisor, fan gear mode)
169 * have been infered from examples in the datasheet and tests.
171 static inline unsigned int rpm_from_cnt(u8 cnt
, u32 clk_freq
, u16 p
,
172 u8 clk_div
, u8 gear_mult
)
174 if (cnt
== 0xff) /* setting cnt to 255 stops the fan */
177 return (clk_freq
* 30 * gear_mult
) / ((cnt
? cnt
: 1) * p
* clk_div
);
181 * Convert fan RPM value from sysfs into count value for fan controller
182 * register (FAN_SET_CNT).
184 static inline unsigned char cnt_from_rpm(unsigned long rpm
, u32 clk_freq
, u16 p
,
185 u8 clk_div
, u8 gear_mult
)
187 unsigned long f1
= clk_freq
* 30 * gear_mult
;
188 unsigned long f2
= p
* clk_div
;
190 if (!rpm
) /* to stop the fan, set cnt to 255 */
193 rpm
= clamp_val(rpm
, f1
/ (255 * f2
), ULONG_MAX
/ f2
);
194 return DIV_ROUND_CLOSEST(f1
, rpm
* f2
);
197 /* helper to grab and cache data, at most one time per second */
198 static struct g762_data
*g762_update_client(struct device
*dev
)
200 struct g762_data
*data
= dev_get_drvdata(dev
);
201 struct i2c_client
*client
= data
->client
;
204 mutex_lock(&data
->update_lock
);
205 if (time_before(jiffies
, data
->last_updated
+ G762_UPDATE_INTERVAL
) &&
209 ret
= i2c_smbus_read_byte_data(client
, G762_REG_SET_CNT
);
214 ret
= i2c_smbus_read_byte_data(client
, G762_REG_ACT_CNT
);
219 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_STA
);
224 ret
= i2c_smbus_read_byte_data(client
, G762_REG_SET_OUT
);
229 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_CMD1
);
232 data
->fan_cmd1
= ret
;
234 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_CMD2
);
237 data
->fan_cmd2
= ret
;
239 data
->last_updated
= jiffies
;
242 mutex_unlock(&data
->update_lock
);
244 if (ret
< 0) /* upon error, encode it in return value */
250 /* helpers for writing hardware parameters */
253 * Set input clock frequency received on CLK pin of the chip. Accepted values
254 * are between 0 and 0xffffff. If zero is given, then default frequency
255 * (32,768Hz) is used. Note that clock frequency is a characteristic of the
256 * system but an internal parameter, i.e. value is not passed to the device.
258 static int do_set_clk_freq(struct device
*dev
, unsigned long val
)
260 struct g762_data
*data
= dev_get_drvdata(dev
);
267 data
->clk_freq
= val
;
272 /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
273 static int do_set_pwm_mode(struct device
*dev
, unsigned long val
)
275 struct g762_data
*data
= g762_update_client(dev
);
279 return PTR_ERR(data
);
281 mutex_lock(&data
->update_lock
);
283 case G762_OUT_MODE_PWM
:
284 data
->fan_cmd1
|= G762_REG_FAN_CMD1_OUT_MODE
;
286 case G762_OUT_MODE_DC
:
287 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_OUT_MODE
;
293 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
297 mutex_unlock(&data
->update_lock
);
302 /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
303 static int do_set_fan_div(struct device
*dev
, unsigned long val
)
305 struct g762_data
*data
= g762_update_client(dev
);
309 return PTR_ERR(data
);
311 mutex_lock(&data
->update_lock
);
314 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID0
;
315 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID1
;
318 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID0
;
319 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID1
;
322 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID0
;
323 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID1
;
326 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID0
;
327 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID1
;
333 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
337 mutex_unlock(&data
->update_lock
);
342 /* Set fan gear mode. Accepts either 0, 1 or 2. */
343 static int do_set_fan_gear_mode(struct device
*dev
, unsigned long val
)
345 struct g762_data
*data
= g762_update_client(dev
);
349 return PTR_ERR(data
);
351 mutex_lock(&data
->update_lock
);
354 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_0
;
355 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_1
;
358 data
->fan_cmd2
|= G762_REG_FAN_CMD2_GEAR_MODE_0
;
359 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_1
;
362 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_0
;
363 data
->fan_cmd2
|= G762_REG_FAN_CMD2_GEAR_MODE_1
;
369 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD2
,
373 mutex_unlock(&data
->update_lock
);
378 /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
379 static int do_set_fan_pulses(struct device
*dev
, unsigned long val
)
381 struct g762_data
*data
= g762_update_client(dev
);
385 return PTR_ERR(data
);
387 mutex_lock(&data
->update_lock
);
390 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_PULSE_PER_REV
;
393 data
->fan_cmd1
|= G762_REG_FAN_CMD1_PULSE_PER_REV
;
399 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
403 mutex_unlock(&data
->update_lock
);
408 /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
409 static int do_set_pwm_enable(struct device
*dev
, unsigned long val
)
411 struct g762_data
*data
= g762_update_client(dev
);
415 return PTR_ERR(data
);
417 mutex_lock(&data
->update_lock
);
419 case G762_FAN_MODE_CLOSED_LOOP
:
420 data
->fan_cmd1
|= G762_REG_FAN_CMD1_FAN_MODE
;
422 case G762_FAN_MODE_OPEN_LOOP
:
423 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_FAN_MODE
;
425 * BUG FIX: if SET_CNT register value is 255 then, for some
426 * unknown reason, fan will not rotate as expected, no matter
427 * the value of SET_OUT (to be specific, this seems to happen
428 * only in PWM mode). To workaround this bug, we give SET_CNT
429 * value of 254 if it is 255 when switching to open-loop.
431 if (data
->set_cnt
== 0xff)
432 i2c_smbus_write_byte_data(data
->client
,
433 G762_REG_SET_CNT
, 254);
440 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
444 mutex_unlock(&data
->update_lock
);
449 /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
450 static int do_set_pwm_polarity(struct device
*dev
, unsigned long val
)
452 struct g762_data
*data
= g762_update_client(dev
);
456 return PTR_ERR(data
);
458 mutex_lock(&data
->update_lock
);
460 case G762_PWM_POLARITY_POSITIVE
:
461 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_PWM_POLARITY
;
463 case G762_PWM_POLARITY_NEGATIVE
:
464 data
->fan_cmd1
|= G762_REG_FAN_CMD1_PWM_POLARITY
;
470 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
474 mutex_unlock(&data
->update_lock
);
480 * Set pwm value. Accepts values between 0 (stops the fan) and
481 * 255 (full speed). This only makes sense in open-loop mode.
483 static int do_set_pwm(struct device
*dev
, unsigned long val
)
485 struct g762_data
*data
= dev_get_drvdata(dev
);
486 struct i2c_client
*client
= data
->client
;
492 mutex_lock(&data
->update_lock
);
493 ret
= i2c_smbus_write_byte_data(client
, G762_REG_SET_OUT
, val
);
495 mutex_unlock(&data
->update_lock
);
501 * Set fan RPM value. Can be called both in closed and open-loop mode
502 * but effect will only be seen after closed-loop mode is configured.
504 static int do_set_fan_target(struct device
*dev
, unsigned long val
)
506 struct g762_data
*data
= g762_update_client(dev
);
510 return PTR_ERR(data
);
512 mutex_lock(&data
->update_lock
);
513 data
->set_cnt
= cnt_from_rpm(val
, data
->clk_freq
,
514 G762_PULSE_FROM_REG(data
->fan_cmd1
),
515 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
516 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
517 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_SET_CNT
,
520 mutex_unlock(&data
->update_lock
);
525 /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
526 static int do_set_fan_startv(struct device
*dev
, unsigned long val
)
528 struct g762_data
*data
= g762_update_client(dev
);
532 return PTR_ERR(data
);
534 mutex_lock(&data
->update_lock
);
537 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_0
;
538 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_1
;
541 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_0
;
542 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_1
;
545 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_0
;
546 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_1
;
549 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_0
;
550 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_1
;
556 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD2
,
560 mutex_unlock(&data
->update_lock
);
566 * Helper to import hardware characteristics from .dts file and push
571 static const struct of_device_id g762_dt_match
[] = {
572 { .compatible
= "gmt,g761" },
573 { .compatible
= "gmt,g762" },
574 { .compatible
= "gmt,g763" },
577 MODULE_DEVICE_TABLE(of
, g762_dt_match
);
580 * Grab clock (a required property), enable it, get (fixed) clock frequency
581 * and store it. Note: upon success, clock has been prepared and enabled; it
582 * must later be unprepared and disabled (e.g. during module unloading) by a
583 * call to g762_of_clock_disable(). Note that a reference to clock is kept
584 * in our private data structure to be used in this function.
586 static void g762_of_clock_disable(void *data
)
588 struct g762_data
*g762
= data
;
590 clk_disable_unprepare(g762
->clk
);
594 static int g762_of_clock_enable(struct i2c_client
*client
)
596 struct g762_data
*data
;
597 unsigned long clk_freq
;
601 if (!client
->dev
.of_node
)
604 data
= i2c_get_clientdata(client
);
607 * Skip CLK detection and handling if we use internal clock.
608 * This is only valid for g761.
610 data
->internal_clock
= of_device_is_compatible(client
->dev
.of_node
,
612 !of_property_present(client
->dev
.of_node
,
614 if (data
->internal_clock
) {
615 do_set_clk_freq(&client
->dev
, 32768);
619 clk
= of_clk_get(client
->dev
.of_node
, 0);
621 dev_err(&client
->dev
, "failed to get clock\n");
625 ret
= clk_prepare_enable(clk
);
627 dev_err(&client
->dev
, "failed to enable clock\n");
631 clk_freq
= clk_get_rate(clk
);
632 ret
= do_set_clk_freq(&client
->dev
, clk_freq
);
634 dev_err(&client
->dev
, "invalid clock freq %lu\n", clk_freq
);
640 ret
= devm_add_action(&client
->dev
, g762_of_clock_disable
, data
);
642 dev_err(&client
->dev
, "failed to add disable clock action\n");
649 clk_disable_unprepare(clk
);
657 static int g762_of_prop_import_one(struct i2c_client
*client
,
659 int (*psetter
)(struct device
*dev
,
665 if (of_property_read_u32(client
->dev
.of_node
, pname
, &pval
))
668 dev_dbg(&client
->dev
, "found %s (%d)\n", pname
, pval
);
669 ret
= (*psetter
)(&client
->dev
, pval
);
671 dev_err(&client
->dev
, "unable to set %s (%d)\n", pname
, pval
);
676 static int g762_of_prop_import(struct i2c_client
*client
)
680 if (!client
->dev
.of_node
)
683 ret
= g762_of_prop_import_one(client
, "fan_gear_mode",
684 do_set_fan_gear_mode
);
688 ret
= g762_of_prop_import_one(client
, "pwm_polarity",
689 do_set_pwm_polarity
);
693 return g762_of_prop_import_one(client
, "fan_startv",
698 static int g762_of_prop_import(struct i2c_client
*client
)
703 static int g762_of_clock_enable(struct i2c_client
*client
)
710 * Helper to import hardware characteristics from .dts file and push
714 static int g762_pdata_prop_import(struct i2c_client
*client
)
716 struct g762_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
722 ret
= do_set_fan_gear_mode(&client
->dev
, pdata
->fan_gear_mode
);
726 ret
= do_set_pwm_polarity(&client
->dev
, pdata
->pwm_polarity
);
730 ret
= do_set_fan_startv(&client
->dev
, pdata
->fan_startv
);
734 return do_set_clk_freq(&client
->dev
, pdata
->clk_freq
);
742 * Read function for fan1_input sysfs file. Return current fan RPM value, or
743 * 0 if fan is out of control.
745 static ssize_t
fan1_input_show(struct device
*dev
,
746 struct device_attribute
*da
, char *buf
)
748 struct g762_data
*data
= g762_update_client(dev
);
749 unsigned int rpm
= 0;
752 return PTR_ERR(data
);
754 mutex_lock(&data
->update_lock
);
755 /* reverse logic: fan out of control reporting is enabled low */
756 if (data
->fan_sta
& G762_REG_FAN_STA_OOC
) {
757 rpm
= rpm_from_cnt(data
->act_cnt
, data
->clk_freq
,
758 G762_PULSE_FROM_REG(data
->fan_cmd1
),
759 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
760 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
762 mutex_unlock(&data
->update_lock
);
764 return sprintf(buf
, "%u\n", rpm
);
768 * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
769 * control mode i.e. PWM (1) or DC (0).
771 static ssize_t
pwm1_mode_show(struct device
*dev
, struct device_attribute
*da
,
774 struct g762_data
*data
= g762_update_client(dev
);
777 return PTR_ERR(data
);
779 return sprintf(buf
, "%d\n",
780 !!(data
->fan_cmd1
& G762_REG_FAN_CMD1_OUT_MODE
));
783 static ssize_t
pwm1_mode_store(struct device
*dev
,
784 struct device_attribute
*da
, const char *buf
,
790 if (kstrtoul(buf
, 10, &val
))
793 ret
= do_set_pwm_mode(dev
, val
);
801 * Read and write functions for fan1_div sysfs file. Get and set fan
802 * controller prescaler value
804 static ssize_t
fan1_div_show(struct device
*dev
, struct device_attribute
*da
,
807 struct g762_data
*data
= g762_update_client(dev
);
810 return PTR_ERR(data
);
812 return sprintf(buf
, "%d\n", G762_CLKDIV_FROM_REG(data
->fan_cmd1
));
815 static ssize_t
fan1_div_store(struct device
*dev
, struct device_attribute
*da
,
816 const char *buf
, size_t count
)
821 if (kstrtoul(buf
, 10, &val
))
824 ret
= do_set_fan_div(dev
, val
);
832 * Read and write functions for fan1_pulses sysfs file. Get and set number
833 * of tachometer pulses per fan revolution.
835 static ssize_t
fan1_pulses_show(struct device
*dev
,
836 struct device_attribute
*da
, char *buf
)
838 struct g762_data
*data
= g762_update_client(dev
);
841 return PTR_ERR(data
);
843 return sprintf(buf
, "%d\n", G762_PULSE_FROM_REG(data
->fan_cmd1
));
846 static ssize_t
fan1_pulses_store(struct device
*dev
,
847 struct device_attribute
*da
, const char *buf
,
853 if (kstrtoul(buf
, 10, &val
))
856 ret
= do_set_fan_pulses(dev
, val
);
864 * Read and write functions for pwm1_enable. Get and set fan speed control mode
865 * (i.e. closed or open-loop).
867 * Following documentation about hwmon's sysfs interface, a pwm1_enable node
868 * should accept the following:
870 * 0 : no fan speed control (i.e. fan at full speed)
871 * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
872 * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
874 * but we do not accept 0 as this mode is not natively supported by the chip
875 * and it is not emulated by g762 driver. -EINVAL is returned in this case.
877 static ssize_t
pwm1_enable_show(struct device
*dev
,
878 struct device_attribute
*da
, char *buf
)
880 struct g762_data
*data
= g762_update_client(dev
);
883 return PTR_ERR(data
);
885 return sprintf(buf
, "%d\n",
886 (!!(data
->fan_cmd1
& G762_REG_FAN_CMD1_FAN_MODE
)) + 1);
889 static ssize_t
pwm1_enable_store(struct device
*dev
,
890 struct device_attribute
*da
, const char *buf
,
896 if (kstrtoul(buf
, 10, &val
))
899 ret
= do_set_pwm_enable(dev
, val
);
907 * Read and write functions for pwm1 sysfs file. Get and set pwm value
908 * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
909 * makes it run at full speed.
911 static ssize_t
pwm1_show(struct device
*dev
, struct device_attribute
*da
,
914 struct g762_data
*data
= g762_update_client(dev
);
917 return PTR_ERR(data
);
919 return sprintf(buf
, "%d\n", data
->set_out
);
922 static ssize_t
pwm1_store(struct device
*dev
, struct device_attribute
*da
,
923 const char *buf
, size_t count
)
928 if (kstrtoul(buf
, 10, &val
))
931 ret
= do_set_pwm(dev
, val
);
939 * Read and write function for fan1_target sysfs file. Get/set the fan speed in
940 * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
941 * the fan speed using pulses from fan tachometer.
943 * Refer to rpm_from_cnt() implementation above to get info about count number
946 * Also note that due to rounding errors it is possible that you don't read
947 * back exactly the value you have set.
949 static ssize_t
fan1_target_show(struct device
*dev
,
950 struct device_attribute
*da
, char *buf
)
952 struct g762_data
*data
= g762_update_client(dev
);
956 return PTR_ERR(data
);
958 mutex_lock(&data
->update_lock
);
959 rpm
= rpm_from_cnt(data
->set_cnt
, data
->clk_freq
,
960 G762_PULSE_FROM_REG(data
->fan_cmd1
),
961 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
962 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
963 mutex_unlock(&data
->update_lock
);
965 return sprintf(buf
, "%u\n", rpm
);
968 static ssize_t
fan1_target_store(struct device
*dev
,
969 struct device_attribute
*da
, const char *buf
,
975 if (kstrtoul(buf
, 10, &val
))
978 ret
= do_set_fan_target(dev
, val
);
985 /* read function for fan1_fault sysfs file. */
986 static ssize_t
fan1_fault_show(struct device
*dev
, struct device_attribute
*da
,
989 struct g762_data
*data
= g762_update_client(dev
);
992 return PTR_ERR(data
);
994 return sprintf(buf
, "%u\n", !!(data
->fan_sta
& G762_REG_FAN_STA_FAIL
));
998 * read function for fan1_alarm sysfs file. Note that OOC condition is
1001 static ssize_t
fan1_alarm_show(struct device
*dev
,
1002 struct device_attribute
*da
, char *buf
)
1004 struct g762_data
*data
= g762_update_client(dev
);
1007 return PTR_ERR(data
);
1009 return sprintf(buf
, "%u\n", !(data
->fan_sta
& G762_REG_FAN_STA_OOC
));
1012 static DEVICE_ATTR_RW(pwm1
);
1013 static DEVICE_ATTR_RW(pwm1_mode
);
1014 static DEVICE_ATTR_RW(pwm1_enable
);
1015 static DEVICE_ATTR_RO(fan1_input
);
1016 static DEVICE_ATTR_RO(fan1_alarm
);
1017 static DEVICE_ATTR_RO(fan1_fault
);
1018 static DEVICE_ATTR_RW(fan1_target
);
1019 static DEVICE_ATTR_RW(fan1_div
);
1020 static DEVICE_ATTR_RW(fan1_pulses
);
1023 static struct attribute
*g762_attrs
[] = {
1024 &dev_attr_fan1_input
.attr
,
1025 &dev_attr_fan1_alarm
.attr
,
1026 &dev_attr_fan1_fault
.attr
,
1027 &dev_attr_fan1_target
.attr
,
1028 &dev_attr_fan1_div
.attr
,
1029 &dev_attr_fan1_pulses
.attr
,
1030 &dev_attr_pwm1
.attr
,
1031 &dev_attr_pwm1_mode
.attr
,
1032 &dev_attr_pwm1_enable
.attr
,
1036 ATTRIBUTE_GROUPS(g762
);
1039 * Enable both fan failure detection and fan out of control protection. The
1040 * function does not protect change/access to data structure; it must thus
1041 * only be called during initialization.
1043 static inline int g762_fan_init(struct device
*dev
)
1045 struct g762_data
*data
= g762_update_client(dev
);
1049 return PTR_ERR(data
);
1051 /* internal_clock can only be set with compatible g761 */
1052 if (data
->internal_clock
)
1053 data
->fan_cmd2
|= G761_REG_FAN_CMD2_FAN_CLOCK
;
1055 data
->fan_cmd1
|= G762_REG_FAN_CMD1_DET_FAN_FAIL
;
1056 data
->fan_cmd1
|= G762_REG_FAN_CMD1_DET_FAN_OOC
;
1057 data
->valid
= false;
1059 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
1064 return i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD2
,
1068 static int g762_probe(struct i2c_client
*client
)
1070 struct device
*dev
= &client
->dev
;
1071 struct device
*hwmon_dev
;
1072 struct g762_data
*data
;
1075 if (!i2c_check_functionality(client
->adapter
,
1076 I2C_FUNC_SMBUS_BYTE_DATA
))
1079 data
= devm_kzalloc(dev
, sizeof(struct g762_data
), GFP_KERNEL
);
1083 i2c_set_clientdata(client
, data
);
1084 data
->client
= client
;
1085 mutex_init(&data
->update_lock
);
1087 /* Get configuration via DT ... */
1088 ret
= g762_of_clock_enable(client
);
1092 /* Enable fan failure detection and fan out of control protection */
1093 ret
= g762_fan_init(dev
);
1097 ret
= g762_of_prop_import(client
);
1100 /* ... or platform_data */
1101 ret
= g762_pdata_prop_import(client
);
1105 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
1107 return PTR_ERR_OR_ZERO(hwmon_dev
);
1110 static struct i2c_driver g762_driver
= {
1113 .of_match_table
= of_match_ptr(g762_dt_match
),
1115 .probe
= g762_probe
,
1116 .id_table
= g762_id
,
1119 module_i2c_driver(g762_driver
);
1121 MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1122 MODULE_DESCRIPTION("GMT G762/G763 driver");
1123 MODULE_LICENSE("GPL");