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 i2c_client
*client
;
135 struct mutex update_lock
;
137 /* board specific parameters. */
140 /* g762 register cache */
142 unsigned long last_updated
; /* in jiffies */
144 u8 set_cnt
; /* controls fan rotation speed in closed-loop mode */
145 u8 act_cnt
; /* provides access to current fan RPM value */
146 u8 fan_sta
; /* bit 0: set when actual fan speed is more than
147 * 25% outside requested fan speed
148 * bit 1: set when no transition occurs on fan
151 u8 set_out
; /* controls fan rotation speed in open-loop mode */
152 u8 fan_cmd1
; /* 0: FG_PLS_ID0 FG pulses count per revolution
153 * 0: 2 counts per revolution
154 * 1: 4 counts per revolution
155 * 1: PWM_POLARITY 1: negative_duty
157 * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
158 * 00: Divide fan clock by 1
159 * 01: Divide fan clock by 2
160 * 10: Divide fan clock by 4
161 * 11: Divide fan clock by 8
162 * 4: FAN_MODE 1:closed-loop, 0:open-loop
163 * 5: OUT_MODE 1:PWM, 0:DC
164 * 6: DET_FAN_OOC enable "fan ooc" status
165 * 7: DET_FAN_FAIL enable "fan fail" status
167 u8 fan_cmd2
; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
172 * 4: Mask ALERT# (g763 only)
177 * Convert count value from fan controller register (FAN_SET_CNT) into fan
178 * speed RPM value. Note that the datasheet documents a basic formula;
179 * influence of additional parameters (fan clock divisor, fan gear mode)
180 * have been infered from examples in the datasheet and tests.
182 static inline unsigned int rpm_from_cnt(u8 cnt
, u32 clk_freq
, u16 p
,
183 u8 clk_div
, u8 gear_mult
)
185 if (cnt
== 0xff) /* setting cnt to 255 stops the fan */
188 return (clk_freq
* 30 * gear_mult
) / ((cnt
? cnt
: 1) * p
* clk_div
);
192 * Convert fan RPM value from sysfs into count value for fan controller
193 * register (FAN_SET_CNT).
195 static inline unsigned char cnt_from_rpm(unsigned long rpm
, u32 clk_freq
, u16 p
,
196 u8 clk_div
, u8 gear_mult
)
198 unsigned long f1
= clk_freq
* 30 * gear_mult
;
199 unsigned long f2
= p
* clk_div
;
201 if (!rpm
) /* to stop the fan, set cnt to 255 */
204 rpm
= clamp_val(rpm
, f1
/ (255 * f2
), ULONG_MAX
/ f2
);
205 return DIV_ROUND_CLOSEST(f1
, rpm
* f2
);
208 /* helper to grab and cache data, at most one time per second */
209 static struct g762_data
*g762_update_client(struct device
*dev
)
211 struct g762_data
*data
= dev_get_drvdata(dev
);
212 struct i2c_client
*client
= data
->client
;
215 mutex_lock(&data
->update_lock
);
216 if (time_before(jiffies
, data
->last_updated
+ G762_UPDATE_INTERVAL
) &&
220 ret
= i2c_smbus_read_byte_data(client
, G762_REG_SET_CNT
);
225 ret
= i2c_smbus_read_byte_data(client
, G762_REG_ACT_CNT
);
230 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_STA
);
235 ret
= i2c_smbus_read_byte_data(client
, G762_REG_SET_OUT
);
240 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_CMD1
);
243 data
->fan_cmd1
= ret
;
245 ret
= i2c_smbus_read_byte_data(client
, G762_REG_FAN_CMD2
);
248 data
->fan_cmd2
= ret
;
250 data
->last_updated
= jiffies
;
253 mutex_unlock(&data
->update_lock
);
255 if (ret
< 0) /* upon error, encode it in return value */
261 /* helpers for writing hardware parameters */
264 * Set input clock frequency received on CLK pin of the chip. Accepted values
265 * are between 0 and 0xffffff. If zero is given, then default frequency
266 * (32,768Hz) is used. Note that clock frequency is a characteristic of the
267 * system but an internal parameter, i.e. value is not passed to the device.
269 static int do_set_clk_freq(struct device
*dev
, unsigned long val
)
271 struct g762_data
*data
= dev_get_drvdata(dev
);
278 data
->clk_freq
= val
;
283 /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
284 static int do_set_pwm_mode(struct device
*dev
, unsigned long val
)
286 struct g762_data
*data
= g762_update_client(dev
);
290 return PTR_ERR(data
);
292 mutex_lock(&data
->update_lock
);
294 case G762_OUT_MODE_PWM
:
295 data
->fan_cmd1
|= G762_REG_FAN_CMD1_OUT_MODE
;
297 case G762_OUT_MODE_DC
:
298 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_OUT_MODE
;
304 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
308 mutex_unlock(&data
->update_lock
);
313 /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
314 static int do_set_fan_div(struct device
*dev
, unsigned long val
)
316 struct g762_data
*data
= g762_update_client(dev
);
320 return PTR_ERR(data
);
322 mutex_lock(&data
->update_lock
);
325 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID0
;
326 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID1
;
329 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID0
;
330 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID1
;
333 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_CLK_DIV_ID0
;
334 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID1
;
337 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID0
;
338 data
->fan_cmd1
|= G762_REG_FAN_CMD1_CLK_DIV_ID1
;
344 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
348 mutex_unlock(&data
->update_lock
);
353 /* Set fan gear mode. Accepts either 0, 1 or 2. */
354 static int do_set_fan_gear_mode(struct device
*dev
, unsigned long val
)
356 struct g762_data
*data
= g762_update_client(dev
);
360 return PTR_ERR(data
);
362 mutex_lock(&data
->update_lock
);
365 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_0
;
366 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_1
;
369 data
->fan_cmd2
|= G762_REG_FAN_CMD2_GEAR_MODE_0
;
370 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_1
;
373 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_GEAR_MODE_0
;
374 data
->fan_cmd2
|= G762_REG_FAN_CMD2_GEAR_MODE_1
;
380 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD2
,
384 mutex_unlock(&data
->update_lock
);
389 /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
390 static int do_set_fan_pulses(struct device
*dev
, unsigned long val
)
392 struct g762_data
*data
= g762_update_client(dev
);
396 return PTR_ERR(data
);
398 mutex_lock(&data
->update_lock
);
401 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_PULSE_PER_REV
;
404 data
->fan_cmd1
|= G762_REG_FAN_CMD1_PULSE_PER_REV
;
410 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
414 mutex_unlock(&data
->update_lock
);
419 /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
420 static int do_set_pwm_enable(struct device
*dev
, unsigned long val
)
422 struct g762_data
*data
= g762_update_client(dev
);
426 return PTR_ERR(data
);
428 mutex_lock(&data
->update_lock
);
430 case G762_FAN_MODE_CLOSED_LOOP
:
431 data
->fan_cmd1
|= G762_REG_FAN_CMD1_FAN_MODE
;
433 case G762_FAN_MODE_OPEN_LOOP
:
434 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_FAN_MODE
;
436 * BUG FIX: if SET_CNT register value is 255 then, for some
437 * unknown reason, fan will not rotate as expected, no matter
438 * the value of SET_OUT (to be specific, this seems to happen
439 * only in PWM mode). To workaround this bug, we give SET_CNT
440 * value of 254 if it is 255 when switching to open-loop.
442 if (data
->set_cnt
== 0xff)
443 i2c_smbus_write_byte_data(data
->client
,
444 G762_REG_SET_CNT
, 254);
451 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
455 mutex_unlock(&data
->update_lock
);
460 /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
461 static int do_set_pwm_polarity(struct device
*dev
, unsigned long val
)
463 struct g762_data
*data
= g762_update_client(dev
);
467 return PTR_ERR(data
);
469 mutex_lock(&data
->update_lock
);
471 case G762_PWM_POLARITY_POSITIVE
:
472 data
->fan_cmd1
&= ~G762_REG_FAN_CMD1_PWM_POLARITY
;
474 case G762_PWM_POLARITY_NEGATIVE
:
475 data
->fan_cmd1
|= G762_REG_FAN_CMD1_PWM_POLARITY
;
481 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
485 mutex_unlock(&data
->update_lock
);
491 * Set pwm value. Accepts values between 0 (stops the fan) and
492 * 255 (full speed). This only makes sense in open-loop mode.
494 static int do_set_pwm(struct device
*dev
, unsigned long val
)
496 struct g762_data
*data
= dev_get_drvdata(dev
);
497 struct i2c_client
*client
= data
->client
;
503 mutex_lock(&data
->update_lock
);
504 ret
= i2c_smbus_write_byte_data(client
, G762_REG_SET_OUT
, val
);
506 mutex_unlock(&data
->update_lock
);
512 * Set fan RPM value. Can be called both in closed and open-loop mode
513 * but effect will only be seen after closed-loop mode is configured.
515 static int do_set_fan_target(struct device
*dev
, unsigned long val
)
517 struct g762_data
*data
= g762_update_client(dev
);
521 return PTR_ERR(data
);
523 mutex_lock(&data
->update_lock
);
524 data
->set_cnt
= cnt_from_rpm(val
, data
->clk_freq
,
525 G762_PULSE_FROM_REG(data
->fan_cmd1
),
526 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
527 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
528 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_SET_CNT
,
531 mutex_unlock(&data
->update_lock
);
536 /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
537 static int do_set_fan_startv(struct device
*dev
, unsigned long val
)
539 struct g762_data
*data
= g762_update_client(dev
);
543 return PTR_ERR(data
);
545 mutex_lock(&data
->update_lock
);
548 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_0
;
549 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_1
;
552 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_0
;
553 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_1
;
556 data
->fan_cmd2
&= ~G762_REG_FAN_CMD2_FAN_STARTV_0
;
557 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_1
;
560 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_0
;
561 data
->fan_cmd2
|= G762_REG_FAN_CMD2_FAN_STARTV_1
;
567 ret
= i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD2
,
571 mutex_unlock(&data
->update_lock
);
577 * Helper to import hardware characteristics from .dts file and push
582 static const struct of_device_id g762_dt_match
[] = {
583 { .compatible
= "gmt,g762" },
584 { .compatible
= "gmt,g763" },
587 MODULE_DEVICE_TABLE(of
, g762_dt_match
);
590 * Grab clock (a required property), enable it, get (fixed) clock frequency
591 * and store it. Note: upon success, clock has been prepared and enabled; it
592 * must later be unprepared and disabled (e.g. during module unloading) by a
593 * call to g762_of_clock_disable(). Note that a reference to clock is kept
594 * in our private data structure to be used in this function.
596 static void g762_of_clock_disable(void *data
)
598 struct g762_data
*g762
= data
;
600 clk_disable_unprepare(g762
->clk
);
604 static int g762_of_clock_enable(struct i2c_client
*client
)
606 struct g762_data
*data
;
607 unsigned long clk_freq
;
611 if (!client
->dev
.of_node
)
614 clk
= of_clk_get(client
->dev
.of_node
, 0);
616 dev_err(&client
->dev
, "failed to get clock\n");
620 ret
= clk_prepare_enable(clk
);
622 dev_err(&client
->dev
, "failed to enable clock\n");
626 clk_freq
= clk_get_rate(clk
);
627 ret
= do_set_clk_freq(&client
->dev
, clk_freq
);
629 dev_err(&client
->dev
, "invalid clock freq %lu\n", clk_freq
);
633 data
= i2c_get_clientdata(client
);
636 devm_add_action(&client
->dev
, g762_of_clock_disable
, data
);
640 clk_disable_unprepare(clk
);
648 static int g762_of_prop_import_one(struct i2c_client
*client
,
650 int (*psetter
)(struct device
*dev
,
656 if (of_property_read_u32(client
->dev
.of_node
, pname
, &pval
))
659 dev_dbg(&client
->dev
, "found %s (%d)\n", pname
, pval
);
660 ret
= (*psetter
)(&client
->dev
, pval
);
662 dev_err(&client
->dev
, "unable to set %s (%d)\n", pname
, pval
);
667 static int g762_of_prop_import(struct i2c_client
*client
)
671 if (!client
->dev
.of_node
)
674 ret
= g762_of_prop_import_one(client
, "fan_gear_mode",
675 do_set_fan_gear_mode
);
679 ret
= g762_of_prop_import_one(client
, "pwm_polarity",
680 do_set_pwm_polarity
);
684 return g762_of_prop_import_one(client
, "fan_startv",
689 static int g762_of_prop_import(struct i2c_client
*client
)
694 static int g762_of_clock_enable(struct i2c_client
*client
)
701 * Helper to import hardware characteristics from .dts file and push
705 static int g762_pdata_prop_import(struct i2c_client
*client
)
707 struct g762_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
713 ret
= do_set_fan_gear_mode(&client
->dev
, pdata
->fan_gear_mode
);
717 ret
= do_set_pwm_polarity(&client
->dev
, pdata
->pwm_polarity
);
721 ret
= do_set_fan_startv(&client
->dev
, pdata
->fan_startv
);
725 return do_set_clk_freq(&client
->dev
, pdata
->clk_freq
);
733 * Read function for fan1_input sysfs file. Return current fan RPM value, or
734 * 0 if fan is out of control.
736 static ssize_t
fan1_input_show(struct device
*dev
,
737 struct device_attribute
*da
, char *buf
)
739 struct g762_data
*data
= g762_update_client(dev
);
740 unsigned int rpm
= 0;
743 return PTR_ERR(data
);
745 mutex_lock(&data
->update_lock
);
746 /* reverse logic: fan out of control reporting is enabled low */
747 if (data
->fan_sta
& G762_REG_FAN_STA_OOC
) {
748 rpm
= rpm_from_cnt(data
->act_cnt
, data
->clk_freq
,
749 G762_PULSE_FROM_REG(data
->fan_cmd1
),
750 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
751 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
753 mutex_unlock(&data
->update_lock
);
755 return sprintf(buf
, "%u\n", rpm
);
759 * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
760 * control mode i.e. PWM (1) or DC (0).
762 static ssize_t
pwm1_mode_show(struct device
*dev
, struct device_attribute
*da
,
765 struct g762_data
*data
= g762_update_client(dev
);
768 return PTR_ERR(data
);
770 return sprintf(buf
, "%d\n",
771 !!(data
->fan_cmd1
& G762_REG_FAN_CMD1_OUT_MODE
));
774 static ssize_t
pwm1_mode_store(struct device
*dev
,
775 struct device_attribute
*da
, const char *buf
,
781 if (kstrtoul(buf
, 10, &val
))
784 ret
= do_set_pwm_mode(dev
, val
);
792 * Read and write functions for fan1_div sysfs file. Get and set fan
793 * controller prescaler value
795 static ssize_t
fan1_div_show(struct device
*dev
, struct device_attribute
*da
,
798 struct g762_data
*data
= g762_update_client(dev
);
801 return PTR_ERR(data
);
803 return sprintf(buf
, "%d\n", G762_CLKDIV_FROM_REG(data
->fan_cmd1
));
806 static ssize_t
fan1_div_store(struct device
*dev
, struct device_attribute
*da
,
807 const char *buf
, size_t count
)
812 if (kstrtoul(buf
, 10, &val
))
815 ret
= do_set_fan_div(dev
, val
);
823 * Read and write functions for fan1_pulses sysfs file. Get and set number
824 * of tachometer pulses per fan revolution.
826 static ssize_t
fan1_pulses_show(struct device
*dev
,
827 struct device_attribute
*da
, char *buf
)
829 struct g762_data
*data
= g762_update_client(dev
);
832 return PTR_ERR(data
);
834 return sprintf(buf
, "%d\n", G762_PULSE_FROM_REG(data
->fan_cmd1
));
837 static ssize_t
fan1_pulses_store(struct device
*dev
,
838 struct device_attribute
*da
, const char *buf
,
844 if (kstrtoul(buf
, 10, &val
))
847 ret
= do_set_fan_pulses(dev
, val
);
855 * Read and write functions for pwm1_enable. Get and set fan speed control mode
856 * (i.e. closed or open-loop).
858 * Following documentation about hwmon's sysfs interface, a pwm1_enable node
859 * should accept the following:
861 * 0 : no fan speed control (i.e. fan at full speed)
862 * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
863 * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
865 * but we do not accept 0 as this mode is not natively supported by the chip
866 * and it is not emulated by g762 driver. -EINVAL is returned in this case.
868 static ssize_t
pwm1_enable_show(struct device
*dev
,
869 struct device_attribute
*da
, char *buf
)
871 struct g762_data
*data
= g762_update_client(dev
);
874 return PTR_ERR(data
);
876 return sprintf(buf
, "%d\n",
877 (!!(data
->fan_cmd1
& G762_REG_FAN_CMD1_FAN_MODE
)) + 1);
880 static ssize_t
pwm1_enable_store(struct device
*dev
,
881 struct device_attribute
*da
, const char *buf
,
887 if (kstrtoul(buf
, 10, &val
))
890 ret
= do_set_pwm_enable(dev
, val
);
898 * Read and write functions for pwm1 sysfs file. Get and set pwm value
899 * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
900 * makes it run at full speed.
902 static ssize_t
pwm1_show(struct device
*dev
, struct device_attribute
*da
,
905 struct g762_data
*data
= g762_update_client(dev
);
908 return PTR_ERR(data
);
910 return sprintf(buf
, "%d\n", data
->set_out
);
913 static ssize_t
pwm1_store(struct device
*dev
, struct device_attribute
*da
,
914 const char *buf
, size_t count
)
919 if (kstrtoul(buf
, 10, &val
))
922 ret
= do_set_pwm(dev
, val
);
930 * Read and write function for fan1_target sysfs file. Get/set the fan speed in
931 * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
932 * the fan speed using pulses from fan tachometer.
934 * Refer to rpm_from_cnt() implementation above to get info about count number
937 * Also note that due to rounding errors it is possible that you don't read
938 * back exactly the value you have set.
940 static ssize_t
fan1_target_show(struct device
*dev
,
941 struct device_attribute
*da
, char *buf
)
943 struct g762_data
*data
= g762_update_client(dev
);
947 return PTR_ERR(data
);
949 mutex_lock(&data
->update_lock
);
950 rpm
= rpm_from_cnt(data
->set_cnt
, data
->clk_freq
,
951 G762_PULSE_FROM_REG(data
->fan_cmd1
),
952 G762_CLKDIV_FROM_REG(data
->fan_cmd1
),
953 G762_GEARMULT_FROM_REG(data
->fan_cmd2
));
954 mutex_unlock(&data
->update_lock
);
956 return sprintf(buf
, "%u\n", rpm
);
959 static ssize_t
fan1_target_store(struct device
*dev
,
960 struct device_attribute
*da
, const char *buf
,
966 if (kstrtoul(buf
, 10, &val
))
969 ret
= do_set_fan_target(dev
, val
);
976 /* read function for fan1_fault sysfs file. */
977 static ssize_t
fan1_fault_show(struct device
*dev
, struct device_attribute
*da
,
980 struct g762_data
*data
= g762_update_client(dev
);
983 return PTR_ERR(data
);
985 return sprintf(buf
, "%u\n", !!(data
->fan_sta
& G762_REG_FAN_STA_FAIL
));
989 * read function for fan1_alarm sysfs file. Note that OOC condition is
992 static ssize_t
fan1_alarm_show(struct device
*dev
,
993 struct device_attribute
*da
, char *buf
)
995 struct g762_data
*data
= g762_update_client(dev
);
998 return PTR_ERR(data
);
1000 return sprintf(buf
, "%u\n", !(data
->fan_sta
& G762_REG_FAN_STA_OOC
));
1003 static DEVICE_ATTR_RW(pwm1
);
1004 static DEVICE_ATTR_RW(pwm1_mode
);
1005 static DEVICE_ATTR_RW(pwm1_enable
);
1006 static DEVICE_ATTR_RO(fan1_input
);
1007 static DEVICE_ATTR_RO(fan1_alarm
);
1008 static DEVICE_ATTR_RO(fan1_fault
);
1009 static DEVICE_ATTR_RW(fan1_target
);
1010 static DEVICE_ATTR_RW(fan1_div
);
1011 static DEVICE_ATTR_RW(fan1_pulses
);
1014 static struct attribute
*g762_attrs
[] = {
1015 &dev_attr_fan1_input
.attr
,
1016 &dev_attr_fan1_alarm
.attr
,
1017 &dev_attr_fan1_fault
.attr
,
1018 &dev_attr_fan1_target
.attr
,
1019 &dev_attr_fan1_div
.attr
,
1020 &dev_attr_fan1_pulses
.attr
,
1021 &dev_attr_pwm1
.attr
,
1022 &dev_attr_pwm1_mode
.attr
,
1023 &dev_attr_pwm1_enable
.attr
,
1027 ATTRIBUTE_GROUPS(g762
);
1030 * Enable both fan failure detection and fan out of control protection. The
1031 * function does not protect change/access to data structure; it must thus
1032 * only be called during initialization.
1034 static inline int g762_fan_init(struct device
*dev
)
1036 struct g762_data
*data
= g762_update_client(dev
);
1039 return PTR_ERR(data
);
1041 data
->fan_cmd1
|= G762_REG_FAN_CMD1_DET_FAN_FAIL
;
1042 data
->fan_cmd1
|= G762_REG_FAN_CMD1_DET_FAN_OOC
;
1043 data
->valid
= false;
1045 return i2c_smbus_write_byte_data(data
->client
, G762_REG_FAN_CMD1
,
1049 static int g762_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1051 struct device
*dev
= &client
->dev
;
1052 struct device
*hwmon_dev
;
1053 struct g762_data
*data
;
1056 if (!i2c_check_functionality(client
->adapter
,
1057 I2C_FUNC_SMBUS_BYTE_DATA
))
1060 data
= devm_kzalloc(dev
, sizeof(struct g762_data
), GFP_KERNEL
);
1064 i2c_set_clientdata(client
, data
);
1065 data
->client
= client
;
1066 mutex_init(&data
->update_lock
);
1068 /* Enable fan failure detection and fan out of control protection */
1069 ret
= g762_fan_init(dev
);
1073 /* Get configuration via DT ... */
1074 ret
= g762_of_clock_enable(client
);
1077 ret
= g762_of_prop_import(client
);
1080 /* ... or platform_data */
1081 ret
= g762_pdata_prop_import(client
);
1085 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
1087 return PTR_ERR_OR_ZERO(hwmon_dev
);
1090 static struct i2c_driver g762_driver
= {
1093 .of_match_table
= of_match_ptr(g762_dt_match
),
1095 .probe
= g762_probe
,
1096 .id_table
= g762_id
,
1099 module_i2c_driver(g762_driver
);
1101 MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1102 MODULE_DESCRIPTION("GMT G762/G763 driver");
1103 MODULE_LICENSE("GPL");