2 * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
3 * PWM controller chips from G762 family, i.e. G762 and G763
5 * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
7 * This work is based on a basic version for 2.6.31 kernel developed
8 * by Olivier Mouchet for LaCie. Updates and correction have been
9 * performed to run on recent kernels. Additional features, like the
10 * ability to configure various characteristics via .dts file or
11 * board init file have been added. Detailed datasheet on which this
12 * development is based is available here:
14 * http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
16 * Headers from previous developments have been kept below:
18 * Copyright (c) 2009 LaCie
20 * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
22 * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
23 * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
25 * g762: minimal datasheet available at:
26 * http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
43 #include <linux/device.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/jiffies.h>
47 #include <linux/i2c.h>
48 #include <linux/hwmon.h>
49 #include <linux/hwmon-sysfs.h>
50 #include <linux/err.h>
51 #include <linux/mutex.h>
52 #include <linux/kernel.h>
53 #include <linux/clk.h>
55 #include <linux/of_device.h>
56 #include <linux/platform_data/g762.h>
58 #define DRVNAME "g762"
60 static const struct i2c_device_id g762_id
[] = {
65 MODULE_DEVICE_TABLE(i2c
, g762_id
);
68 G762_REG_SET_CNT
= 0x00,
69 G762_REG_ACT_CNT
= 0x01,
70 G762_REG_FAN_STA
= 0x02,
71 G762_REG_SET_OUT
= 0x03,
72 G762_REG_FAN_CMD1
= 0x04,
73 G762_REG_FAN_CMD2
= 0x05,
76 /* Config register bits */
77 #define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */
78 #define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */
79 #define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */
80 #define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */
81 #define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */
82 #define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04
83 #define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */
84 #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
86 #define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */
87 #define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04
88 #define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */
89 #define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01
91 #define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */
92 #define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */
94 /* Config register values */
95 #define G762_OUT_MODE_PWM 1
96 #define G762_OUT_MODE_DC 0
98 #define G762_FAN_MODE_CLOSED_LOOP 2
99 #define G762_FAN_MODE_OPEN_LOOP 1
101 #define G762_PWM_POLARITY_NEGATIVE 1
102 #define G762_PWM_POLARITY_POSITIVE 0
104 /* Register data is read (and cached) at most once per second. */
105 #define G762_UPDATE_INTERVAL HZ
108 * Extract pulse count per fan revolution value (2 or 4) from given
109 * FAN_CMD1 register value.
111 #define G762_PULSE_FROM_REG(reg) \
112 ((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
115 * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
118 #define G762_CLKDIV_FROM_REG(reg) \
119 (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \
120 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
123 * Extract fan gear mode multiplier value (0, 2 or 4) from given
124 * FAN_CMD2 register value.
126 #define G762_GEARMULT_FROM_REG(reg) \
127 (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \
128 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
131 struct device
*hwmon_dev
;
132 struct i2c_client
*client
;
136 struct mutex update_lock
;
138 /* board specific parameters. */
141 /* g762 register cache */
143 unsigned long last_updated
; /* in jiffies */
145 u8 set_cnt
; /* controls fan rotation speed in closed-loop mode */
146 u8 act_cnt
; /* provides access to current fan RPM value */
147 u8 fan_sta
; /* bit 0: set when actual fan speed is more than
148 * 25% outside requested fan speed
149 * bit 1: set when no transition occurs on fan
152 u8 set_out
; /* controls fan rotation speed in open-loop mode */
153 u8 fan_cmd1
; /* 0: FG_PLS_ID0 FG pulses count per revolution
154 * 0: 2 counts per revolution
155 * 1: 4 counts per revolution
156 * 1: PWM_POLARITY 1: negative_duty
158 * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
159 * 00: Divide fan clock by 1
160 * 01: Divide fan clock by 2
161 * 10: Divide fan clock by 4
162 * 11: Divide fan clock by 8
163 * 4: FAN_MODE 1:closed-loop, 0:open-loop
164 * 5: OUT_MODE 1:PWM, 0:DC
165 * 6: DET_FAN_OOC enable "fan ooc" status
166 * 7: DET_FAN_FAIL enable "fan fail" status
168 u8 fan_cmd2
; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
173 * 4: Mask ALERT# (g763 only)
178 * Convert count value from fan controller register (FAN_SET_CNT) into fan
179 * speed RPM value. Note that the datasheet documents a basic formula;
180 * influence of additional parameters (fan clock divisor, fan gear mode)
181 * have been infered from examples in the datasheet and tests.
183 static inline unsigned int rpm_from_cnt(u8 cnt
, u32 clk_freq
, u16 p
,
184 u8 clk_div
, u8 gear_mult
)
186 if (cnt
== 0xff) /* setting cnt to 255 stops the fan */
189 return (clk_freq
* 30 * gear_mult
) / ((cnt
? cnt
: 1) * p
* clk_div
);
193 * Convert fan RPM value from sysfs into count value for fan controller
194 * register (FAN_SET_CNT).
196 static inline unsigned char cnt_from_rpm(unsigned long rpm
, u32 clk_freq
, u16 p
,
197 u8 clk_div
, u8 gear_mult
)
199 unsigned long f1
= clk_freq
* 30 * gear_mult
;
200 unsigned long f2
= p
* clk_div
;
202 if (!rpm
) /* to stop the fan, set cnt to 255 */
205 rpm
= clamp_val(rpm
, f1
/ (255 * f2
), ULONG_MAX
/ f2
);
206 return DIV_ROUND_CLOSEST(f1
, rpm
* f2
);
209 /* helper to grab and cache data, at most one time per second */
210 static struct g762_data
*g762_update_client(struct device
*dev
)
212 struct g762_data
*data
= dev_get_drvdata(dev
);
213 struct i2c_client
*client
= data
->client
;
216 mutex_lock(&data
->update_lock
);
217 if (time_before(jiffies
, data
->last_updated
+ G762_UPDATE_INTERVAL
) &&
221 ret
= i2c_smbus_read_byte_data(client
, G762_REG_SET_CNT
);
226 ret
= i2c_smbus_read_byte_data(client
, G762_REG_ACT_CNT
);
231 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_STA
);
236 ret
= i2c_smbus_read_byte_data(client
, G762_REG_SET_OUT
);
241 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_CMD1
);
244 data
->fan_cmd1
= ret
;
246 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_CMD2
);
249 data
->fan_cmd2
= ret
;
251 data
->last_updated
= jiffies
;
254 mutex_unlock(&data
->update_lock
);
256 if (ret
< 0) /* upon error, encode it in return value */
262 /* helpers for writing hardware parameters */
265 * Set input clock frequency received on CLK pin of the chip. Accepted values
266 * are between 0 and 0xffffff. If zero is given, then default frequency
267 * (32,768Hz) is used. Note that clock frequency is a characteristic of the
268 * system but an internal parameter, i.e. value is not passed to the device.
270 static int do_set_clk_freq(struct device
*dev
, unsigned long val
)
272 struct g762_data
*data
= dev_get_drvdata(dev
);
279 data
->clk_freq
= val
;
284 /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
285 static int do_set_pwm_mode(struct device
*dev
, unsigned long val
)
287 struct g762_data
*data
= g762_update_client(dev
);
291 return PTR_ERR(data
);
293 mutex_lock(&data
->update_lock
);
295 case G762_OUT_MODE_PWM
:
296 data
->fan_cmd1
|= G762_REG_FAN_CMD1_OUT_MODE
;
298 case G762_OUT_MODE_DC
:
299 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_OUT_MODE
;
305 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
309 mutex_unlock(&data
->update_lock
);
314 /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
315 static int do_set_fan_div(struct device
*dev
, unsigned long val
)
317 struct g762_data
*data
= g762_update_client(dev
);
321 return PTR_ERR(data
);
323 mutex_lock(&data
->update_lock
);
326 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID0
;
327 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID1
;
330 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID0
;
331 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID1
;
334 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID0
;
335 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID1
;
338 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID0
;
339 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID1
;
345 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
349 mutex_unlock(&data
->update_lock
);
354 /* Set fan gear mode. Accepts either 0, 1 or 2. */
355 static int do_set_fan_gear_mode(struct device
*dev
, unsigned long val
)
357 struct g762_data
*data
= g762_update_client(dev
);
361 return PTR_ERR(data
);
363 mutex_lock(&data
->update_lock
);
366 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_0
;
367 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_1
;
370 data
->fan_cmd2
|= G762_REG_FAN_CMD2_GEAR_MODE_0
;
371 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_1
;
374 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_0
;
375 data
->fan_cmd2
|= G762_REG_FAN_CMD2_GEAR_MODE_1
;
381 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD2
,
385 mutex_unlock(&data
->update_lock
);
390 /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
391 static int do_set_fan_pulses(struct device
*dev
, unsigned long val
)
393 struct g762_data
*data
= g762_update_client(dev
);
397 return PTR_ERR(data
);
399 mutex_lock(&data
->update_lock
);
402 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_PULSE_PER_REV
;
405 data
->fan_cmd1
|= G762_REG_FAN_CMD1_PULSE_PER_REV
;
411 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
415 mutex_unlock(&data
->update_lock
);
420 /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
421 static int do_set_pwm_enable(struct device
*dev
, unsigned long val
)
423 struct g762_data
*data
= g762_update_client(dev
);
427 return PTR_ERR(data
);
429 mutex_lock(&data
->update_lock
);
431 case G762_FAN_MODE_CLOSED_LOOP
:
432 data
->fan_cmd1
|= G762_REG_FAN_CMD1_FAN_MODE
;
434 case G762_FAN_MODE_OPEN_LOOP
:
435 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_FAN_MODE
;
437 * BUG FIX: if SET_CNT register value is 255 then, for some
438 * unknown reason, fan will not rotate as expected, no matter
439 * the value of SET_OUT (to be specific, this seems to happen
440 * only in PWM mode). To workaround this bug, we give SET_CNT
441 * value of 254 if it is 255 when switching to open-loop.
443 if (data
->set_cnt
== 0xff)
444 i2c_smbus_write_byte_data(data
->client
,
445 G762_REG_SET_CNT
, 254);
452 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
456 mutex_unlock(&data
->update_lock
);
461 /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
462 static int do_set_pwm_polarity(struct device
*dev
, unsigned long val
)
464 struct g762_data
*data
= g762_update_client(dev
);
468 return PTR_ERR(data
);
470 mutex_lock(&data
->update_lock
);
472 case G762_PWM_POLARITY_POSITIVE
:
473 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_PWM_POLARITY
;
475 case G762_PWM_POLARITY_NEGATIVE
:
476 data
->fan_cmd1
|= G762_REG_FAN_CMD1_PWM_POLARITY
;
482 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
486 mutex_unlock(&data
->update_lock
);
492 * Set pwm value. Accepts values between 0 (stops the fan) and
493 * 255 (full speed). This only makes sense in open-loop mode.
495 static int do_set_pwm(struct device
*dev
, unsigned long val
)
497 struct g762_data
*data
= dev_get_drvdata(dev
);
498 struct i2c_client
*client
= data
->client
;
504 mutex_lock(&data
->update_lock
);
505 ret
= i2c_smbus_write_byte_data(client
, G762_REG_SET_OUT
, val
);
507 mutex_unlock(&data
->update_lock
);
513 * Set fan RPM value. Can be called both in closed and open-loop mode
514 * but effect will only be seen after closed-loop mode is configured.
516 static int do_set_fan_target(struct device
*dev
, unsigned long val
)
518 struct g762_data
*data
= g762_update_client(dev
);
522 return PTR_ERR(data
);
524 mutex_lock(&data
->update_lock
);
525 data
->set_cnt
= cnt_from_rpm(val
, data
->clk_freq
,
526 G762_PULSE_FROM_REG(data
->fan_cmd1
),
527 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
528 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
529 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_SET_CNT
,
532 mutex_unlock(&data
->update_lock
);
537 /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
538 static int do_set_fan_startv(struct device
*dev
, unsigned long val
)
540 struct g762_data
*data
= g762_update_client(dev
);
544 return PTR_ERR(data
);
546 mutex_lock(&data
->update_lock
);
549 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_0
;
550 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_1
;
553 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_0
;
554 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_1
;
557 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_0
;
558 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_1
;
561 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_0
;
562 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_1
;
568 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD2
,
572 mutex_unlock(&data
->update_lock
);
578 * Helper to import hardware characteristics from .dts file and push
583 static const struct of_device_id g762_dt_match
[] = {
584 { .compatible
= "gmt,g762" },
585 { .compatible
= "gmt,g763" },
588 MODULE_DEVICE_TABLE(of
, g762_dt_match
);
591 * Grab clock (a required property), enable it, get (fixed) clock frequency
592 * and store it. Note: upon success, clock has been prepared and enabled; it
593 * must later be unprepared and disabled (e.g. during module unloading) by a
594 * call to g762_of_clock_disable(). Note that a reference to clock is kept
595 * in our private data structure to be used in this function.
597 static int g762_of_clock_enable(struct i2c_client
*client
)
599 struct g762_data
*data
;
600 unsigned long clk_freq
;
604 if (!client
->dev
.of_node
)
607 clk
= of_clk_get(client
->dev
.of_node
, 0);
609 dev_err(&client
->dev
, "failed to get clock\n");
613 ret
= clk_prepare_enable(clk
);
615 dev_err(&client
->dev
, "failed to enable clock\n");
619 clk_freq
= clk_get_rate(clk
);
620 ret
= do_set_clk_freq(&client
->dev
, clk_freq
);
622 dev_err(&client
->dev
, "invalid clock freq %lu\n", clk_freq
);
626 data
= i2c_get_clientdata(client
);
632 clk_disable_unprepare(clk
);
640 static void g762_of_clock_disable(struct i2c_client
*client
)
642 struct g762_data
*data
= i2c_get_clientdata(client
);
647 clk_disable_unprepare(data
->clk
);
651 static int g762_of_prop_import_one(struct i2c_client
*client
,
653 int (*psetter
)(struct device
*dev
,
659 if (of_property_read_u32(client
->dev
.of_node
, pname
, &pval
))
662 dev_dbg(&client
->dev
, "found %s (%d)\n", pname
, pval
);
663 ret
= (*psetter
)(&client
->dev
, pval
);
665 dev_err(&client
->dev
, "unable to set %s (%d)\n", pname
, pval
);
670 static int g762_of_prop_import(struct i2c_client
*client
)
674 if (!client
->dev
.of_node
)
677 ret
= g762_of_prop_import_one(client
, "fan_gear_mode",
678 do_set_fan_gear_mode
);
682 ret
= g762_of_prop_import_one(client
, "pwm_polarity",
683 do_set_pwm_polarity
);
687 return g762_of_prop_import_one(client
, "fan_startv",
692 static int g762_of_prop_import(struct i2c_client
*client
)
697 static int g762_of_clock_enable(struct i2c_client
*client
)
702 static void g762_of_clock_disable(struct i2c_client
*client
) { }
706 * Helper to import hardware characteristics from .dts file and push
710 static int g762_pdata_prop_import(struct i2c_client
*client
)
712 struct g762_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
718 ret
= do_set_fan_gear_mode(&client
->dev
, pdata
->fan_gear_mode
);
722 ret
= do_set_pwm_polarity(&client
->dev
, pdata
->pwm_polarity
);
726 ret
= do_set_fan_startv(&client
->dev
, pdata
->fan_startv
);
730 return do_set_clk_freq(&client
->dev
, pdata
->clk_freq
);
738 * Read function for fan1_input sysfs file. Return current fan RPM value, or
739 * 0 if fan is out of control.
741 static ssize_t
get_fan_rpm(struct device
*dev
, struct device_attribute
*da
,
744 struct g762_data
*data
= g762_update_client(dev
);
745 unsigned int rpm
= 0;
748 return PTR_ERR(data
);
750 mutex_lock(&data
->update_lock
);
751 /* reverse logic: fan out of control reporting is enabled low */
752 if (data
->fan_sta
& G762_REG_FAN_STA_OOC
) {
753 rpm
= rpm_from_cnt(data
->act_cnt
, data
->clk_freq
,
754 G762_PULSE_FROM_REG(data
->fan_cmd1
),
755 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
756 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
758 mutex_unlock(&data
->update_lock
);
760 return sprintf(buf
, "%u\n", rpm
);
764 * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
765 * control mode i.e. PWM (1) or DC (0).
767 static ssize_t
get_pwm_mode(struct device
*dev
, struct device_attribute
*da
,
770 struct g762_data
*data
= g762_update_client(dev
);
773 return PTR_ERR(data
);
775 return sprintf(buf
, "%d\n",
776 !!(data
->fan_cmd1
& G762_REG_FAN_CMD1_OUT_MODE
));
779 static ssize_t
set_pwm_mode(struct device
*dev
, struct device_attribute
*da
,
780 const char *buf
, size_t count
)
785 if (kstrtoul(buf
, 10, &val
))
788 ret
= do_set_pwm_mode(dev
, val
);
796 * Read and write functions for fan1_div sysfs file. Get and set fan
797 * controller prescaler value
799 static ssize_t
get_fan_div(struct device
*dev
,
800 struct device_attribute
*da
, char *buf
)
802 struct g762_data
*data
= g762_update_client(dev
);
805 return PTR_ERR(data
);
807 return sprintf(buf
, "%d\n", G762_CLKDIV_FROM_REG(data
->fan_cmd1
));
810 static ssize_t
set_fan_div(struct device
*dev
,
811 struct device_attribute
*da
,
812 const char *buf
, size_t count
)
817 if (kstrtoul(buf
, 10, &val
))
820 ret
= do_set_fan_div(dev
, val
);
828 * Read and write functions for fan1_pulses sysfs file. Get and set number
829 * of tachometer pulses per fan revolution.
831 static ssize_t
get_fan_pulses(struct device
*dev
,
832 struct device_attribute
*da
, char *buf
)
834 struct g762_data
*data
= g762_update_client(dev
);
837 return PTR_ERR(data
);
839 return sprintf(buf
, "%d\n", G762_PULSE_FROM_REG(data
->fan_cmd1
));
842 static ssize_t
set_fan_pulses(struct device
*dev
,
843 struct device_attribute
*da
,
844 const char *buf
, size_t count
)
849 if (kstrtoul(buf
, 10, &val
))
852 ret
= do_set_fan_pulses(dev
, val
);
860 * Read and write functions for pwm1_enable. Get and set fan speed control mode
861 * (i.e. closed or open-loop).
863 * Following documentation about hwmon's sysfs interface, a pwm1_enable node
864 * should accept followings:
866 * 0 : no fan speed control (i.e. fan at full speed)
867 * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
868 * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
870 * but we do not accept 0 as this mode is not natively supported by the chip
871 * and it is not emulated by g762 driver. -EINVAL is returned in this case.
873 static ssize_t
get_pwm_enable(struct device
*dev
,
874 struct device_attribute
*da
, char *buf
)
876 struct g762_data
*data
= g762_update_client(dev
);
879 return PTR_ERR(data
);
881 return sprintf(buf
, "%d\n",
882 (!!(data
->fan_cmd1
& G762_REG_FAN_CMD1_FAN_MODE
)) + 1);
885 static ssize_t
set_pwm_enable(struct device
*dev
,
886 struct device_attribute
*da
,
887 const char *buf
, size_t count
)
892 if (kstrtoul(buf
, 10, &val
))
895 ret
= do_set_pwm_enable(dev
, val
);
903 * Read and write functions for pwm1 sysfs file. Get and set pwm value
904 * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
905 * makes it run at full speed.
907 static ssize_t
get_pwm(struct device
*dev
, struct device_attribute
*da
,
910 struct g762_data
*data
= g762_update_client(dev
);
913 return PTR_ERR(data
);
915 return sprintf(buf
, "%d\n", data
->set_out
);
918 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*da
,
919 const char *buf
, size_t count
)
924 if (kstrtoul(buf
, 10, &val
))
927 ret
= do_set_pwm(dev
, val
);
935 * Read and write function for fan1_target sysfs file. Get/set the fan speed in
936 * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
937 * the fan speed using pulses from fan tachometer.
939 * Refer to rpm_from_cnt() implementation above to get info about count number
942 * Also note that due to rounding errors it is possible that you don't read
943 * back exactly the value you have set.
945 static ssize_t
get_fan_target(struct device
*dev
, struct device_attribute
*da
,
948 struct g762_data
*data
= g762_update_client(dev
);
952 return PTR_ERR(data
);
954 mutex_lock(&data
->update_lock
);
955 rpm
= rpm_from_cnt(data
->set_cnt
, data
->clk_freq
,
956 G762_PULSE_FROM_REG(data
->fan_cmd1
),
957 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
958 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
959 mutex_unlock(&data
->update_lock
);
961 return sprintf(buf
, "%u\n", rpm
);
964 static ssize_t
set_fan_target(struct device
*dev
, struct device_attribute
*da
,
965 const char *buf
, size_t count
)
970 if (kstrtoul(buf
, 10, &val
))
973 ret
= do_set_fan_target(dev
, val
);
980 /* read function for fan1_fault sysfs file. */
981 static ssize_t
get_fan_failure(struct device
*dev
, struct device_attribute
*da
,
984 struct g762_data
*data
= g762_update_client(dev
);
987 return PTR_ERR(data
);
989 return sprintf(buf
, "%u\n", !!(data
->fan_sta
& G762_REG_FAN_STA_FAIL
));
993 * read function for fan1_alarm sysfs file. Note that OOC condition is
996 static ssize_t
get_fan_ooc(struct device
*dev
, struct device_attribute
*da
,
999 struct g762_data
*data
= g762_update_client(dev
);
1002 return PTR_ERR(data
);
1004 return sprintf(buf
, "%u\n", !(data
->fan_sta
& G762_REG_FAN_STA_OOC
));
1007 static DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, get_pwm
, set_pwm
);
1008 static DEVICE_ATTR(pwm1_mode
, S_IWUSR
| S_IRUGO
, get_pwm_mode
, set_pwm_mode
);
1009 static DEVICE_ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
1010 get_pwm_enable
, set_pwm_enable
);
1011 static DEVICE_ATTR(fan1_input
, S_IRUGO
, get_fan_rpm
, NULL
);
1012 static DEVICE_ATTR(fan1_alarm
, S_IRUGO
, get_fan_ooc
, NULL
);
1013 static DEVICE_ATTR(fan1_fault
, S_IRUGO
, get_fan_failure
, NULL
);
1014 static DEVICE_ATTR(fan1_target
, S_IWUSR
| S_IRUGO
,
1015 get_fan_target
, set_fan_target
);
1016 static DEVICE_ATTR(fan1_div
, S_IWUSR
| S_IRUGO
, get_fan_div
, set_fan_div
);
1017 static DEVICE_ATTR(fan1_pulses
, S_IWUSR
| S_IRUGO
,
1018 get_fan_pulses
, set_fan_pulses
);
1021 static struct attribute
*g762_attrs
[] = {
1022 &dev_attr_fan1_input
.attr
,
1023 &dev_attr_fan1_alarm
.attr
,
1024 &dev_attr_fan1_fault
.attr
,
1025 &dev_attr_fan1_target
.attr
,
1026 &dev_attr_fan1_div
.attr
,
1027 &dev_attr_fan1_pulses
.attr
,
1028 &dev_attr_pwm1
.attr
,
1029 &dev_attr_pwm1_mode
.attr
,
1030 &dev_attr_pwm1_enable
.attr
,
1034 ATTRIBUTE_GROUPS(g762
);
1037 * Enable both fan failure detection and fan out of control protection. The
1038 * function does not protect change/access to data structure; it must thus
1039 * only be called during initialization.
1041 static inline int g762_fan_init(struct device
*dev
)
1043 struct g762_data
*data
= g762_update_client(dev
);
1046 return PTR_ERR(data
);
1048 data
->fan_cmd1
|= G762_REG_FAN_CMD1_DET_FAN_FAIL
;
1049 data
->fan_cmd1
|= G762_REG_FAN_CMD1_DET_FAN_OOC
;
1050 data
->valid
= false;
1052 return i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
1056 static int g762_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1058 struct device
*dev
= &client
->dev
;
1059 struct g762_data
*data
;
1062 if (!i2c_check_functionality(client
->adapter
,
1063 I2C_FUNC_SMBUS_BYTE_DATA
))
1066 data
= devm_kzalloc(dev
, sizeof(struct g762_data
), GFP_KERNEL
);
1070 i2c_set_clientdata(client
, data
);
1071 data
->client
= client
;
1072 mutex_init(&data
->update_lock
);
1074 /* Enable fan failure detection and fan out of control protection */
1075 ret
= g762_fan_init(dev
);
1079 /* Get configuration via DT ... */
1080 ret
= g762_of_clock_enable(client
);
1083 ret
= g762_of_prop_import(client
);
1086 /* ... or platform_data */
1087 ret
= g762_pdata_prop_import(client
);
1091 data
->hwmon_dev
= hwmon_device_register_with_groups(dev
, client
->name
,
1093 if (IS_ERR(data
->hwmon_dev
)) {
1094 ret
= PTR_ERR(data
->hwmon_dev
);
1101 g762_of_clock_disable(client
);
1106 static int g762_remove(struct i2c_client
*client
)
1108 struct g762_data
*data
= i2c_get_clientdata(client
);
1110 hwmon_device_unregister(data
->hwmon_dev
);
1111 g762_of_clock_disable(client
);
1116 static struct i2c_driver g762_driver
= {
1119 .of_match_table
= of_match_ptr(g762_dt_match
),
1121 .probe
= g762_probe
,
1122 .remove
= g762_remove
,
1123 .id_table
= g762_id
,
1126 module_i2c_driver(g762_driver
);
1128 MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1129 MODULE_DESCRIPTION("GMT G762/G763 driver");
1130 MODULE_LICENSE("GPL");