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[] = {
51 MODULE_DEVICE_TABLE(i2c, g762_id);
54 G762_REG_SET_CNT = 0x00,
55 G762_REG_ACT_CNT = 0x01,
56 G762_REG_FAN_STA = 0x02,
57 G762_REG_SET_OUT = 0x03,
58 G762_REG_FAN_CMD1 = 0x04,
59 G762_REG_FAN_CMD2 = 0x05,
62 /* Config register bits */
63 #define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */
64 #define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */
65 #define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */
66 #define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */
67 #define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */
68 #define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04
69 #define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */
70 #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
72 #define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */
73 #define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04
74 #define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */
75 #define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01
77 #define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */
78 #define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */
80 /* Config register values */
81 #define G762_OUT_MODE_PWM 1
82 #define G762_OUT_MODE_DC 0
84 #define G762_FAN_MODE_CLOSED_LOOP 2
85 #define G762_FAN_MODE_OPEN_LOOP 1
87 #define G762_PWM_POLARITY_NEGATIVE 1
88 #define G762_PWM_POLARITY_POSITIVE 0
90 /* Register data is read (and cached) at most once per second. */
91 #define G762_UPDATE_INTERVAL HZ
94 * Extract pulse count per fan revolution value (2 or 4) from given
95 * FAN_CMD1 register value.
97 #define G762_PULSE_FROM_REG(reg) \
98 ((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
101 * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
104 #define G762_CLKDIV_FROM_REG(reg) \
105 (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \
106 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
109 * Extract fan gear mode multiplier value (0, 2 or 4) from given
110 * FAN_CMD2 register value.
112 #define G762_GEARMULT_FROM_REG(reg) \
113 (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \
114 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
117 struct i2c_client *client;
121 struct mutex update_lock;
123 /* board specific parameters. */
126 /* g762 register cache */
128 unsigned long last_updated; /* in jiffies */
130 u8 set_cnt; /* controls fan rotation speed in closed-loop mode */
131 u8 act_cnt; /* provides access to current fan RPM value */
132 u8 fan_sta; /* bit 0: set when actual fan speed is more than
133 * 25% outside requested fan speed
134 * bit 1: set when no transition occurs on fan
137 u8 set_out; /* controls fan rotation speed in open-loop mode */
138 u8 fan_cmd1; /* 0: FG_PLS_ID0 FG pulses count per revolution
139 * 0: 2 counts per revolution
140 * 1: 4 counts per revolution
141 * 1: PWM_POLARITY 1: negative_duty
143 * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
144 * 00: Divide fan clock by 1
145 * 01: Divide fan clock by 2
146 * 10: Divide fan clock by 4
147 * 11: Divide fan clock by 8
148 * 4: FAN_MODE 1:closed-loop, 0:open-loop
149 * 5: OUT_MODE 1:PWM, 0:DC
150 * 6: DET_FAN_OOC enable "fan ooc" status
151 * 7: DET_FAN_FAIL enable "fan fail" status
153 u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
158 * 4: Mask ALERT# (g763 only)
163 * Convert count value from fan controller register (FAN_SET_CNT) into fan
164 * speed RPM value. Note that the datasheet documents a basic formula;
165 * influence of additional parameters (fan clock divisor, fan gear mode)
166 * have been infered from examples in the datasheet and tests.
168 static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
169 u8 clk_div, u8 gear_mult)
171 if (cnt == 0xff) /* setting cnt to 255 stops the fan */
174 return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
178 * Convert fan RPM value from sysfs into count value for fan controller
179 * register (FAN_SET_CNT).
181 static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
182 u8 clk_div, u8 gear_mult)
184 unsigned long f1 = clk_freq * 30 * gear_mult;
185 unsigned long f2 = p * clk_div;
187 if (!rpm) /* to stop the fan, set cnt to 255 */
190 rpm = clamp_val(rpm, f1 / (255 * f2), ULONG_MAX / f2);
191 return DIV_ROUND_CLOSEST(f1, rpm * f2);
194 /* helper to grab and cache data, at most one time per second */
195 static struct g762_data *g762_update_client(struct device *dev)
197 struct g762_data *data = dev_get_drvdata(dev);
198 struct i2c_client *client = data->client;
201 mutex_lock(&data->update_lock);
202 if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
206 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
211 ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
216 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
221 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
226 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
229 data->fan_cmd1 = ret;
231 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
234 data->fan_cmd2 = ret;
236 data->last_updated = jiffies;
239 mutex_unlock(&data->update_lock);
241 if (ret < 0) /* upon error, encode it in return value */
247 /* helpers for writing hardware parameters */
250 * Set input clock frequency received on CLK pin of the chip. Accepted values
251 * are between 0 and 0xffffff. If zero is given, then default frequency
252 * (32,768Hz) is used. Note that clock frequency is a characteristic of the
253 * system but an internal parameter, i.e. value is not passed to the device.
255 static int do_set_clk_freq(struct device *dev, unsigned long val)
257 struct g762_data *data = dev_get_drvdata(dev);
264 data->clk_freq = val;
269 /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
270 static int do_set_pwm_mode(struct device *dev, unsigned long val)
272 struct g762_data *data = g762_update_client(dev);
276 return PTR_ERR(data);
278 mutex_lock(&data->update_lock);
280 case G762_OUT_MODE_PWM:
281 data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE;
283 case G762_OUT_MODE_DC:
284 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
290 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
294 mutex_unlock(&data->update_lock);
299 /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
300 static int do_set_fan_div(struct device *dev, unsigned long val)
302 struct g762_data *data = g762_update_client(dev);
306 return PTR_ERR(data);
308 mutex_lock(&data->update_lock);
311 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
312 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
315 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
316 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
319 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
320 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
323 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
324 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
330 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
334 mutex_unlock(&data->update_lock);
339 /* Set fan gear mode. Accepts either 0, 1 or 2. */
340 static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
342 struct g762_data *data = g762_update_client(dev);
346 return PTR_ERR(data);
348 mutex_lock(&data->update_lock);
351 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
352 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
355 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0;
356 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
359 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
360 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1;
366 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
370 mutex_unlock(&data->update_lock);
375 /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
376 static int do_set_fan_pulses(struct device *dev, unsigned long val)
378 struct g762_data *data = g762_update_client(dev);
382 return PTR_ERR(data);
384 mutex_lock(&data->update_lock);
387 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
390 data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV;
396 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
400 mutex_unlock(&data->update_lock);
405 /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
406 static int do_set_pwm_enable(struct device *dev, unsigned long val)
408 struct g762_data *data = g762_update_client(dev);
412 return PTR_ERR(data);
414 mutex_lock(&data->update_lock);
416 case G762_FAN_MODE_CLOSED_LOOP:
417 data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE;
419 case G762_FAN_MODE_OPEN_LOOP:
420 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
422 * BUG FIX: if SET_CNT register value is 255 then, for some
423 * unknown reason, fan will not rotate as expected, no matter
424 * the value of SET_OUT (to be specific, this seems to happen
425 * only in PWM mode). To workaround this bug, we give SET_CNT
426 * value of 254 if it is 255 when switching to open-loop.
428 if (data->set_cnt == 0xff)
429 i2c_smbus_write_byte_data(data->client,
430 G762_REG_SET_CNT, 254);
437 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
441 mutex_unlock(&data->update_lock);
446 /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
447 static int do_set_pwm_polarity(struct device *dev, unsigned long val)
449 struct g762_data *data = g762_update_client(dev);
453 return PTR_ERR(data);
455 mutex_lock(&data->update_lock);
457 case G762_PWM_POLARITY_POSITIVE:
458 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
460 case G762_PWM_POLARITY_NEGATIVE:
461 data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY;
467 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
471 mutex_unlock(&data->update_lock);
477 * Set pwm value. Accepts values between 0 (stops the fan) and
478 * 255 (full speed). This only makes sense in open-loop mode.
480 static int do_set_pwm(struct device *dev, unsigned long val)
482 struct g762_data *data = dev_get_drvdata(dev);
483 struct i2c_client *client = data->client;
489 mutex_lock(&data->update_lock);
490 ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
492 mutex_unlock(&data->update_lock);
498 * Set fan RPM value. Can be called both in closed and open-loop mode
499 * but effect will only be seen after closed-loop mode is configured.
501 static int do_set_fan_target(struct device *dev, unsigned long val)
503 struct g762_data *data = g762_update_client(dev);
507 return PTR_ERR(data);
509 mutex_lock(&data->update_lock);
510 data->set_cnt = cnt_from_rpm(val, data->clk_freq,
511 G762_PULSE_FROM_REG(data->fan_cmd1),
512 G762_CLKDIV_FROM_REG(data->fan_cmd1),
513 G762_GEARMULT_FROM_REG(data->fan_cmd2));
514 ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
517 mutex_unlock(&data->update_lock);
522 /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
523 static int do_set_fan_startv(struct device *dev, unsigned long val)
525 struct g762_data *data = g762_update_client(dev);
529 return PTR_ERR(data);
531 mutex_lock(&data->update_lock);
534 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
535 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
538 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
539 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
542 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
543 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
546 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
547 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
553 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
557 mutex_unlock(&data->update_lock);
563 * Helper to import hardware characteristics from .dts file and push
568 static const struct of_device_id g762_dt_match[] = {
569 { .compatible = "gmt,g762" },
570 { .compatible = "gmt,g763" },
573 MODULE_DEVICE_TABLE(of, g762_dt_match);
576 * Grab clock (a required property), enable it, get (fixed) clock frequency
577 * and store it. Note: upon success, clock has been prepared and enabled; it
578 * must later be unprepared and disabled (e.g. during module unloading) by a
579 * call to g762_of_clock_disable(). Note that a reference to clock is kept
580 * in our private data structure to be used in this function.
582 static void g762_of_clock_disable(void *data)
584 struct g762_data *g762 = data;
586 clk_disable_unprepare(g762->clk);
590 static int g762_of_clock_enable(struct i2c_client *client)
592 struct g762_data *data;
593 unsigned long clk_freq;
597 if (!client->dev.of_node)
600 clk = of_clk_get(client->dev.of_node, 0);
602 dev_err(&client->dev, "failed to get clock\n");
606 ret = clk_prepare_enable(clk);
608 dev_err(&client->dev, "failed to enable clock\n");
612 clk_freq = clk_get_rate(clk);
613 ret = do_set_clk_freq(&client->dev, clk_freq);
615 dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
619 data = i2c_get_clientdata(client);
622 ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
624 dev_err(&client->dev, "failed to add disable clock action\n");
631 clk_disable_unprepare(clk);
639 static int g762_of_prop_import_one(struct i2c_client *client,
641 int (*psetter)(struct device *dev,
647 if (of_property_read_u32(client->dev.of_node, pname, &pval))
650 dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
651 ret = (*psetter)(&client->dev, pval);
653 dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
658 static int g762_of_prop_import(struct i2c_client *client)
662 if (!client->dev.of_node)
665 ret = g762_of_prop_import_one(client, "fan_gear_mode",
666 do_set_fan_gear_mode);
670 ret = g762_of_prop_import_one(client, "pwm_polarity",
671 do_set_pwm_polarity);
675 return g762_of_prop_import_one(client, "fan_startv",
680 static int g762_of_prop_import(struct i2c_client *client)
685 static int g762_of_clock_enable(struct i2c_client *client)
692 * Helper to import hardware characteristics from .dts file and push
696 static int g762_pdata_prop_import(struct i2c_client *client)
698 struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
704 ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
708 ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
712 ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
716 return do_set_clk_freq(&client->dev, pdata->clk_freq);
724 * Read function for fan1_input sysfs file. Return current fan RPM value, or
725 * 0 if fan is out of control.
727 static ssize_t fan1_input_show(struct device *dev,
728 struct device_attribute *da, char *buf)
730 struct g762_data *data = g762_update_client(dev);
731 unsigned int rpm = 0;
734 return PTR_ERR(data);
736 mutex_lock(&data->update_lock);
737 /* reverse logic: fan out of control reporting is enabled low */
738 if (data->fan_sta & G762_REG_FAN_STA_OOC) {
739 rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
740 G762_PULSE_FROM_REG(data->fan_cmd1),
741 G762_CLKDIV_FROM_REG(data->fan_cmd1),
742 G762_GEARMULT_FROM_REG(data->fan_cmd2));
744 mutex_unlock(&data->update_lock);
746 return sprintf(buf, "%u\n", rpm);
750 * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
751 * control mode i.e. PWM (1) or DC (0).
753 static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da,
756 struct g762_data *data = g762_update_client(dev);
759 return PTR_ERR(data);
761 return sprintf(buf, "%d\n",
762 !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
765 static ssize_t pwm1_mode_store(struct device *dev,
766 struct device_attribute *da, const char *buf,
772 if (kstrtoul(buf, 10, &val))
775 ret = do_set_pwm_mode(dev, val);
783 * Read and write functions for fan1_div sysfs file. Get and set fan
784 * controller prescaler value
786 static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da,
789 struct g762_data *data = g762_update_client(dev);
792 return PTR_ERR(data);
794 return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
797 static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
798 const char *buf, size_t count)
803 if (kstrtoul(buf, 10, &val))
806 ret = do_set_fan_div(dev, val);
814 * Read and write functions for fan1_pulses sysfs file. Get and set number
815 * of tachometer pulses per fan revolution.
817 static ssize_t fan1_pulses_show(struct device *dev,
818 struct device_attribute *da, char *buf)
820 struct g762_data *data = g762_update_client(dev);
823 return PTR_ERR(data);
825 return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
828 static ssize_t fan1_pulses_store(struct device *dev,
829 struct device_attribute *da, const char *buf,
835 if (kstrtoul(buf, 10, &val))
838 ret = do_set_fan_pulses(dev, val);
846 * Read and write functions for pwm1_enable. Get and set fan speed control mode
847 * (i.e. closed or open-loop).
849 * Following documentation about hwmon's sysfs interface, a pwm1_enable node
850 * should accept the following:
852 * 0 : no fan speed control (i.e. fan at full speed)
853 * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
854 * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
856 * but we do not accept 0 as this mode is not natively supported by the chip
857 * and it is not emulated by g762 driver. -EINVAL is returned in this case.
859 static ssize_t pwm1_enable_show(struct device *dev,
860 struct device_attribute *da, char *buf)
862 struct g762_data *data = g762_update_client(dev);
865 return PTR_ERR(data);
867 return sprintf(buf, "%d\n",
868 (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
871 static ssize_t pwm1_enable_store(struct device *dev,
872 struct device_attribute *da, const char *buf,
878 if (kstrtoul(buf, 10, &val))
881 ret = do_set_pwm_enable(dev, val);
889 * Read and write functions for pwm1 sysfs file. Get and set pwm value
890 * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
891 * makes it run at full speed.
893 static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
896 struct g762_data *data = g762_update_client(dev);
899 return PTR_ERR(data);
901 return sprintf(buf, "%d\n", data->set_out);
904 static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
905 const char *buf, size_t count)
910 if (kstrtoul(buf, 10, &val))
913 ret = do_set_pwm(dev, val);
921 * Read and write function for fan1_target sysfs file. Get/set the fan speed in
922 * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
923 * the fan speed using pulses from fan tachometer.
925 * Refer to rpm_from_cnt() implementation above to get info about count number
928 * Also note that due to rounding errors it is possible that you don't read
929 * back exactly the value you have set.
931 static ssize_t fan1_target_show(struct device *dev,
932 struct device_attribute *da, char *buf)
934 struct g762_data *data = g762_update_client(dev);
938 return PTR_ERR(data);
940 mutex_lock(&data->update_lock);
941 rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
942 G762_PULSE_FROM_REG(data->fan_cmd1),
943 G762_CLKDIV_FROM_REG(data->fan_cmd1),
944 G762_GEARMULT_FROM_REG(data->fan_cmd2));
945 mutex_unlock(&data->update_lock);
947 return sprintf(buf, "%u\n", rpm);
950 static ssize_t fan1_target_store(struct device *dev,
951 struct device_attribute *da, const char *buf,
957 if (kstrtoul(buf, 10, &val))
960 ret = do_set_fan_target(dev, val);
967 /* read function for fan1_fault sysfs file. */
968 static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da,
971 struct g762_data *data = g762_update_client(dev);
974 return PTR_ERR(data);
976 return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
980 * read function for fan1_alarm sysfs file. Note that OOC condition is
983 static ssize_t fan1_alarm_show(struct device *dev,
984 struct device_attribute *da, char *buf)
986 struct g762_data *data = g762_update_client(dev);
989 return PTR_ERR(data);
991 return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
994 static DEVICE_ATTR_RW(pwm1);
995 static DEVICE_ATTR_RW(pwm1_mode);
996 static DEVICE_ATTR_RW(pwm1_enable);
997 static DEVICE_ATTR_RO(fan1_input);
998 static DEVICE_ATTR_RO(fan1_alarm);
999 static DEVICE_ATTR_RO(fan1_fault);
1000 static DEVICE_ATTR_RW(fan1_target);
1001 static DEVICE_ATTR_RW(fan1_div);
1002 static DEVICE_ATTR_RW(fan1_pulses);
1005 static struct attribute *g762_attrs[] = {
1006 &dev_attr_fan1_input.attr,
1007 &dev_attr_fan1_alarm.attr,
1008 &dev_attr_fan1_fault.attr,
1009 &dev_attr_fan1_target.attr,
1010 &dev_attr_fan1_div.attr,
1011 &dev_attr_fan1_pulses.attr,
1012 &dev_attr_pwm1.attr,
1013 &dev_attr_pwm1_mode.attr,
1014 &dev_attr_pwm1_enable.attr,
1018 ATTRIBUTE_GROUPS(g762);
1021 * Enable both fan failure detection and fan out of control protection. The
1022 * function does not protect change/access to data structure; it must thus
1023 * only be called during initialization.
1025 static inline int g762_fan_init(struct device *dev)
1027 struct g762_data *data = g762_update_client(dev);
1030 return PTR_ERR(data);
1032 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
1033 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
1034 data->valid = false;
1036 return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
1040 static int g762_probe(struct i2c_client *client)
1042 struct device *dev = &client->dev;
1043 struct device *hwmon_dev;
1044 struct g762_data *data;
1047 if (!i2c_check_functionality(client->adapter,
1048 I2C_FUNC_SMBUS_BYTE_DATA))
1051 data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
1055 i2c_set_clientdata(client, data);
1056 data->client = client;
1057 mutex_init(&data->update_lock);
1059 /* Enable fan failure detection and fan out of control protection */
1060 ret = g762_fan_init(dev);
1064 /* Get configuration via DT ... */
1065 ret = g762_of_clock_enable(client);
1068 ret = g762_of_prop_import(client);
1071 /* ... or platform_data */
1072 ret = g762_pdata_prop_import(client);
1076 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1078 return PTR_ERR_OR_ZERO(hwmon_dev);
1081 static struct i2c_driver g762_driver = {
1084 .of_match_table = of_match_ptr(g762_dt_match),
1086 .probe = g762_probe,
1087 .id_table = g762_id,
1090 module_i2c_driver(g762_driver);
1092 MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1093 MODULE_DESCRIPTION("GMT G762/G763 driver");
1094 MODULE_LICENSE("GPL");