xref: /linux/drivers/hwmon/g762.c (revision f4a180247e06c69e7d8fac4eb14d172c3a5d43a3)
18b9844d7SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2594fbe71SArnaud Ebalard /*
3594fbe71SArnaud Ebalard  * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
4594fbe71SArnaud Ebalard  *        PWM controller chips from G762 family, i.e. G762 and G763
5594fbe71SArnaud Ebalard  *
6594fbe71SArnaud Ebalard  * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
7594fbe71SArnaud Ebalard  *
8594fbe71SArnaud Ebalard  * This work is based on a basic version for 2.6.31 kernel developed
9594fbe71SArnaud Ebalard  * by Olivier Mouchet for LaCie. Updates and correction have been
10594fbe71SArnaud Ebalard  * performed to run on recent kernels. Additional features, like the
11594fbe71SArnaud Ebalard  * ability to configure various characteristics via .dts file or
12594fbe71SArnaud Ebalard  * board init file have been added. Detailed datasheet on which this
13594fbe71SArnaud Ebalard  * development is based is available here:
14594fbe71SArnaud Ebalard  *
15594fbe71SArnaud Ebalard  *  http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
16594fbe71SArnaud Ebalard  *
17594fbe71SArnaud Ebalard  * Headers from previous developments have been kept below:
18594fbe71SArnaud Ebalard  *
19594fbe71SArnaud Ebalard  * Copyright (c) 2009 LaCie
20594fbe71SArnaud Ebalard  *
21594fbe71SArnaud Ebalard  * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
22594fbe71SArnaud Ebalard  *
23594fbe71SArnaud Ebalard  * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
24594fbe71SArnaud Ebalard  * Copyright (C) 2007  Herbert Valerio Riedel <hvr@gnu.org>
25594fbe71SArnaud Ebalard  *
26594fbe71SArnaud Ebalard  * g762: minimal datasheet available at:
27594fbe71SArnaud Ebalard  *       http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
28594fbe71SArnaud Ebalard  */
29594fbe71SArnaud Ebalard 
30594fbe71SArnaud Ebalard #include <linux/device.h>
31594fbe71SArnaud Ebalard #include <linux/module.h>
32594fbe71SArnaud Ebalard #include <linux/init.h>
33594fbe71SArnaud Ebalard #include <linux/jiffies.h>
34594fbe71SArnaud Ebalard #include <linux/i2c.h>
35594fbe71SArnaud Ebalard #include <linux/hwmon.h>
36594fbe71SArnaud Ebalard #include <linux/hwmon-sysfs.h>
37594fbe71SArnaud Ebalard #include <linux/err.h>
38594fbe71SArnaud Ebalard #include <linux/mutex.h>
39594fbe71SArnaud Ebalard #include <linux/kernel.h>
40594fbe71SArnaud Ebalard #include <linux/clk.h>
41594fbe71SArnaud Ebalard #include <linux/of.h>
42594fbe71SArnaud Ebalard #include <linux/platform_data/g762.h>
43594fbe71SArnaud Ebalard 
44594fbe71SArnaud Ebalard #define DRVNAME "g762"
45594fbe71SArnaud Ebalard 
46594fbe71SArnaud Ebalard static const struct i2c_device_id g762_id[] = {
47*f4a18024SGuenter Roeck 	{ "g761" },
48d8a66f36SUwe Kleine-König 	{ "g762" },
49d8a66f36SUwe Kleine-König 	{ "g763" },
50594fbe71SArnaud Ebalard 	{ }
51594fbe71SArnaud Ebalard };
52594fbe71SArnaud Ebalard MODULE_DEVICE_TABLE(i2c, g762_id);
53594fbe71SArnaud Ebalard 
54594fbe71SArnaud Ebalard enum g762_regs {
55594fbe71SArnaud Ebalard 	G762_REG_SET_CNT  = 0x00,
56594fbe71SArnaud Ebalard 	G762_REG_ACT_CNT  = 0x01,
57594fbe71SArnaud Ebalard 	G762_REG_FAN_STA  = 0x02,
58594fbe71SArnaud Ebalard 	G762_REG_SET_OUT  = 0x03,
59594fbe71SArnaud Ebalard 	G762_REG_FAN_CMD1 = 0x04,
60594fbe71SArnaud Ebalard 	G762_REG_FAN_CMD2 = 0x05,
61594fbe71SArnaud Ebalard };
62594fbe71SArnaud Ebalard 
63594fbe71SArnaud Ebalard /* Config register bits */
64594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_DET_FAN_FAIL  0x80 /* enable fan_fail signal */
65594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_DET_FAN_OOC   0x40 /* enable fan_out_of_control */
66594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_OUT_MODE      0x20 /* out mode: PWM or DC */
67594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_FAN_MODE      0x10 /* fan mode: closed/open-loop */
68594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_CLK_DIV_ID1   0x08 /* clock divisor value */
69594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_CLK_DIV_ID0   0x04
70594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_PWM_POLARITY  0x02 /* PWM polarity */
71594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
72594fbe71SArnaud Ebalard 
736ce40232SChristian Marangi #define G761_REG_FAN_CMD2_FAN_CLOCK     0x20 /* choose internal clock*/
74594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_GEAR_MODE_1   0x08 /* fan gear mode */
75594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_GEAR_MODE_0   0x04
76594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_FAN_STARTV_1  0x02 /* fan startup voltage */
77594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_FAN_STARTV_0  0x01
78594fbe71SArnaud Ebalard 
79594fbe71SArnaud Ebalard #define G762_REG_FAN_STA_FAIL           0x02 /* fan fail */
80594fbe71SArnaud Ebalard #define G762_REG_FAN_STA_OOC            0x01 /* fan out of control */
81594fbe71SArnaud Ebalard 
82594fbe71SArnaud Ebalard /* Config register values */
83594fbe71SArnaud Ebalard #define G762_OUT_MODE_PWM            1
84594fbe71SArnaud Ebalard #define G762_OUT_MODE_DC             0
85594fbe71SArnaud Ebalard 
86594fbe71SArnaud Ebalard #define G762_FAN_MODE_CLOSED_LOOP    2
87594fbe71SArnaud Ebalard #define G762_FAN_MODE_OPEN_LOOP      1
88594fbe71SArnaud Ebalard 
89594fbe71SArnaud Ebalard #define G762_PWM_POLARITY_NEGATIVE   1
90594fbe71SArnaud Ebalard #define G762_PWM_POLARITY_POSITIVE   0
91594fbe71SArnaud Ebalard 
92594fbe71SArnaud Ebalard /* Register data is read (and cached) at most once per second. */
93594fbe71SArnaud Ebalard #define G762_UPDATE_INTERVAL    HZ
94594fbe71SArnaud Ebalard 
95594fbe71SArnaud Ebalard /*
96594fbe71SArnaud Ebalard  * Extract pulse count per fan revolution value (2 or 4) from given
97594fbe71SArnaud Ebalard  * FAN_CMD1 register value.
98594fbe71SArnaud Ebalard  */
99594fbe71SArnaud Ebalard #define G762_PULSE_FROM_REG(reg) \
100594fbe71SArnaud Ebalard 	((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
101594fbe71SArnaud Ebalard 
102594fbe71SArnaud Ebalard /*
103594fbe71SArnaud Ebalard  * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
104594fbe71SArnaud Ebalard  * register value.
105594fbe71SArnaud Ebalard  */
106594fbe71SArnaud Ebalard #define G762_CLKDIV_FROM_REG(reg) \
107594fbe71SArnaud Ebalard 	(1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 |	\
108594fbe71SArnaud Ebalard 			 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
109594fbe71SArnaud Ebalard 
110594fbe71SArnaud Ebalard /*
111594fbe71SArnaud Ebalard  * Extract fan gear mode multiplier value (0, 2 or 4) from given
112594fbe71SArnaud Ebalard  * FAN_CMD2 register value.
113594fbe71SArnaud Ebalard  */
114594fbe71SArnaud Ebalard #define G762_GEARMULT_FROM_REG(reg) \
115594fbe71SArnaud Ebalard 	(1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 |	\
116594fbe71SArnaud Ebalard 			 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
117594fbe71SArnaud Ebalard 
118594fbe71SArnaud Ebalard struct g762_data {
119398e16dbSAxel Lin 	struct i2c_client *client;
1206ce40232SChristian Marangi 	bool internal_clock;
121594fbe71SArnaud Ebalard 	struct clk *clk;
122594fbe71SArnaud Ebalard 
123594fbe71SArnaud Ebalard 	/* update mutex */
124594fbe71SArnaud Ebalard 	struct mutex update_lock;
125594fbe71SArnaud Ebalard 
126594fbe71SArnaud Ebalard 	/* board specific parameters. */
127594fbe71SArnaud Ebalard 	u32 clk_freq;
128594fbe71SArnaud Ebalard 
129594fbe71SArnaud Ebalard 	/* g762 register cache */
130594fbe71SArnaud Ebalard 	bool valid;
131594fbe71SArnaud Ebalard 	unsigned long last_updated; /* in jiffies */
132594fbe71SArnaud Ebalard 
133594fbe71SArnaud Ebalard 	u8 set_cnt;  /* controls fan rotation speed in closed-loop mode */
134594fbe71SArnaud Ebalard 	u8 act_cnt;  /* provides access to current fan RPM value */
135594fbe71SArnaud Ebalard 	u8 fan_sta;  /* bit 0: set when actual fan speed is more than
136594fbe71SArnaud Ebalard 		      *        25% outside requested fan speed
137594fbe71SArnaud Ebalard 		      * bit 1: set when no transition occurs on fan
138594fbe71SArnaud Ebalard 		      *        pin for 0.7s
139594fbe71SArnaud Ebalard 		      */
140594fbe71SArnaud Ebalard 	u8 set_out;  /* controls fan rotation speed in open-loop mode */
141594fbe71SArnaud Ebalard 	u8 fan_cmd1; /*   0: FG_PLS_ID0 FG pulses count per revolution
142594fbe71SArnaud Ebalard 		      *      0: 2 counts per revolution
143594fbe71SArnaud Ebalard 		      *      1: 4 counts per revolution
144594fbe71SArnaud Ebalard 		      *   1: PWM_POLARITY 1: negative_duty
145594fbe71SArnaud Ebalard 		      *                   0: positive_duty
146594fbe71SArnaud Ebalard 		      * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
147594fbe71SArnaud Ebalard 		      *         00: Divide fan clock by 1
148594fbe71SArnaud Ebalard 		      *         01: Divide fan clock by 2
149594fbe71SArnaud Ebalard 		      *         10: Divide fan clock by 4
150594fbe71SArnaud Ebalard 		      *         11: Divide fan clock by 8
151594fbe71SArnaud Ebalard 		      *   4: FAN_MODE 1:closed-loop, 0:open-loop
152594fbe71SArnaud Ebalard 		      *   5: OUT_MODE 1:PWM, 0:DC
153594fbe71SArnaud Ebalard 		      *   6: DET_FAN_OOC enable "fan ooc" status
154594fbe71SArnaud Ebalard 		      *   7: DET_FAN_FAIL enable "fan fail" status
155594fbe71SArnaud Ebalard 		      */
156594fbe71SArnaud Ebalard 	u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
157594fbe71SArnaud Ebalard 		      * 2,3: FG_GEAR_MODE
158594fbe71SArnaud Ebalard 		      *         00: multiplier = 1
159594fbe71SArnaud Ebalard 		      *         01: multiplier = 2
160594fbe71SArnaud Ebalard 		      *         10: multiplier = 4
161594fbe71SArnaud Ebalard 		      *   4: Mask ALERT# (g763 only)
162594fbe71SArnaud Ebalard 		      */
163594fbe71SArnaud Ebalard };
164594fbe71SArnaud Ebalard 
165594fbe71SArnaud Ebalard /*
166594fbe71SArnaud Ebalard  * Convert count value from fan controller register (FAN_SET_CNT) into fan
167594fbe71SArnaud Ebalard  * speed RPM value. Note that the datasheet documents a basic formula;
168594fbe71SArnaud Ebalard  * influence of additional parameters (fan clock divisor, fan gear mode)
169594fbe71SArnaud Ebalard  * have been infered from examples in the datasheet and tests.
170594fbe71SArnaud Ebalard  */
171594fbe71SArnaud Ebalard static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
172594fbe71SArnaud Ebalard 					u8 clk_div, u8 gear_mult)
173594fbe71SArnaud Ebalard {
174594fbe71SArnaud Ebalard 	if (cnt == 0xff)  /* setting cnt to 255 stops the fan */
175594fbe71SArnaud Ebalard 		return 0;
176594fbe71SArnaud Ebalard 
177594fbe71SArnaud Ebalard 	return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
178594fbe71SArnaud Ebalard }
179594fbe71SArnaud Ebalard 
180594fbe71SArnaud Ebalard /*
181594fbe71SArnaud Ebalard  * Convert fan RPM value from sysfs into count value for fan controller
182594fbe71SArnaud Ebalard  * register (FAN_SET_CNT).
183594fbe71SArnaud Ebalard  */
1844fccd4a1SGuenter Roeck static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
185594fbe71SArnaud Ebalard 					 u8 clk_div, u8 gear_mult)
186594fbe71SArnaud Ebalard {
1874fccd4a1SGuenter Roeck 	unsigned long f1 = clk_freq * 30 * gear_mult;
1884fccd4a1SGuenter Roeck 	unsigned long f2 = p * clk_div;
1894fccd4a1SGuenter Roeck 
190594fbe71SArnaud Ebalard 	if (!rpm)	/* to stop the fan, set cnt to 255 */
191594fbe71SArnaud Ebalard 		return 0xff;
192594fbe71SArnaud Ebalard 
1934fccd4a1SGuenter Roeck 	rpm = clamp_val(rpm, f1 / (255 * f2), ULONG_MAX / f2);
1944fccd4a1SGuenter Roeck 	return DIV_ROUND_CLOSEST(f1, rpm * f2);
195594fbe71SArnaud Ebalard }
196594fbe71SArnaud Ebalard 
197594fbe71SArnaud Ebalard /* helper to grab and cache data, at most one time per second */
198594fbe71SArnaud Ebalard static struct g762_data *g762_update_client(struct device *dev)
199594fbe71SArnaud Ebalard {
200398e16dbSAxel Lin 	struct g762_data *data = dev_get_drvdata(dev);
201398e16dbSAxel Lin 	struct i2c_client *client = data->client;
202594fbe71SArnaud Ebalard 	int ret = 0;
203594fbe71SArnaud Ebalard 
204594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
205594fbe71SArnaud Ebalard 	if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
206594fbe71SArnaud Ebalard 	    likely(data->valid))
207594fbe71SArnaud Ebalard 		goto out;
208594fbe71SArnaud Ebalard 
209594fbe71SArnaud Ebalard 	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
210594fbe71SArnaud Ebalard 	if (ret < 0)
211594fbe71SArnaud Ebalard 		goto out;
212594fbe71SArnaud Ebalard 	data->set_cnt = ret;
213594fbe71SArnaud Ebalard 
214594fbe71SArnaud Ebalard 	ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
215594fbe71SArnaud Ebalard 	if (ret < 0)
216594fbe71SArnaud Ebalard 		goto out;
217594fbe71SArnaud Ebalard 	data->act_cnt = ret;
218594fbe71SArnaud Ebalard 
219594fbe71SArnaud Ebalard 	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
220594fbe71SArnaud Ebalard 	if (ret < 0)
221594fbe71SArnaud Ebalard 		goto out;
222594fbe71SArnaud Ebalard 	data->fan_sta = ret;
223594fbe71SArnaud Ebalard 
224594fbe71SArnaud Ebalard 	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
225594fbe71SArnaud Ebalard 	if (ret < 0)
226594fbe71SArnaud Ebalard 		goto out;
227594fbe71SArnaud Ebalard 	data->set_out = ret;
228594fbe71SArnaud Ebalard 
229594fbe71SArnaud Ebalard 	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
230594fbe71SArnaud Ebalard 	if (ret < 0)
231594fbe71SArnaud Ebalard 		goto out;
232594fbe71SArnaud Ebalard 	data->fan_cmd1 = ret;
233594fbe71SArnaud Ebalard 
234594fbe71SArnaud Ebalard 	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
235594fbe71SArnaud Ebalard 	if (ret < 0)
236594fbe71SArnaud Ebalard 		goto out;
237594fbe71SArnaud Ebalard 	data->fan_cmd2 = ret;
238594fbe71SArnaud Ebalard 
239594fbe71SArnaud Ebalard 	data->last_updated = jiffies;
240594fbe71SArnaud Ebalard 	data->valid = true;
241594fbe71SArnaud Ebalard  out:
242594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
243594fbe71SArnaud Ebalard 
244594fbe71SArnaud Ebalard 	if (ret < 0) /* upon error, encode it in return value */
245594fbe71SArnaud Ebalard 		data = ERR_PTR(ret);
246594fbe71SArnaud Ebalard 
247594fbe71SArnaud Ebalard 	return data;
248594fbe71SArnaud Ebalard }
249594fbe71SArnaud Ebalard 
250594fbe71SArnaud Ebalard /* helpers for writing hardware parameters */
251594fbe71SArnaud Ebalard 
252594fbe71SArnaud Ebalard /*
253594fbe71SArnaud Ebalard  * Set input clock frequency received on CLK pin of the chip. Accepted values
254594fbe71SArnaud Ebalard  * are between 0 and 0xffffff. If zero is given, then default frequency
255594fbe71SArnaud Ebalard  * (32,768Hz) is used. Note that clock frequency is a characteristic of the
256594fbe71SArnaud Ebalard  * system but an internal parameter, i.e. value is not passed to the device.
257594fbe71SArnaud Ebalard  */
258594fbe71SArnaud Ebalard static int do_set_clk_freq(struct device *dev, unsigned long val)
259594fbe71SArnaud Ebalard {
260398e16dbSAxel Lin 	struct g762_data *data = dev_get_drvdata(dev);
261594fbe71SArnaud Ebalard 
262594fbe71SArnaud Ebalard 	if (val > 0xffffff)
263594fbe71SArnaud Ebalard 		return -EINVAL;
264594fbe71SArnaud Ebalard 	if (!val)
265594fbe71SArnaud Ebalard 		val = 32768;
266594fbe71SArnaud Ebalard 
267594fbe71SArnaud Ebalard 	data->clk_freq = val;
268594fbe71SArnaud Ebalard 
269594fbe71SArnaud Ebalard 	return 0;
270594fbe71SArnaud Ebalard }
271594fbe71SArnaud Ebalard 
272594fbe71SArnaud Ebalard /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
273594fbe71SArnaud Ebalard static int do_set_pwm_mode(struct device *dev, unsigned long val)
274594fbe71SArnaud Ebalard {
275594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
276594fbe71SArnaud Ebalard 	int ret;
277594fbe71SArnaud Ebalard 
278594fbe71SArnaud Ebalard 	if (IS_ERR(data))
279594fbe71SArnaud Ebalard 		return PTR_ERR(data);
280594fbe71SArnaud Ebalard 
281594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
282594fbe71SArnaud Ebalard 	switch (val) {
283594fbe71SArnaud Ebalard 	case G762_OUT_MODE_PWM:
284594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_OUT_MODE;
285594fbe71SArnaud Ebalard 		break;
286594fbe71SArnaud Ebalard 	case G762_OUT_MODE_DC:
287594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
288594fbe71SArnaud Ebalard 		break;
289594fbe71SArnaud Ebalard 	default:
290594fbe71SArnaud Ebalard 		ret = -EINVAL;
291594fbe71SArnaud Ebalard 		goto out;
292594fbe71SArnaud Ebalard 	}
293398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
294594fbe71SArnaud Ebalard 					data->fan_cmd1);
295594fbe71SArnaud Ebalard 	data->valid = false;
296594fbe71SArnaud Ebalard  out:
297594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
298594fbe71SArnaud Ebalard 
299594fbe71SArnaud Ebalard 	return ret;
300594fbe71SArnaud Ebalard }
301594fbe71SArnaud Ebalard 
302594fbe71SArnaud Ebalard /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
303594fbe71SArnaud Ebalard static int do_set_fan_div(struct device *dev, unsigned long val)
304594fbe71SArnaud Ebalard {
305594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
306594fbe71SArnaud Ebalard 	int ret;
307594fbe71SArnaud Ebalard 
308594fbe71SArnaud Ebalard 	if (IS_ERR(data))
309594fbe71SArnaud Ebalard 		return PTR_ERR(data);
310594fbe71SArnaud Ebalard 
311594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
312594fbe71SArnaud Ebalard 	switch (val) {
313594fbe71SArnaud Ebalard 	case 1:
314594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
315594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
316594fbe71SArnaud Ebalard 		break;
317594fbe71SArnaud Ebalard 	case 2:
318594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
319594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
320594fbe71SArnaud Ebalard 		break;
321594fbe71SArnaud Ebalard 	case 4:
322594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
323594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
324594fbe71SArnaud Ebalard 		break;
325594fbe71SArnaud Ebalard 	case 8:
326594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
327594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
328594fbe71SArnaud Ebalard 		break;
329594fbe71SArnaud Ebalard 	default:
330594fbe71SArnaud Ebalard 		ret = -EINVAL;
331594fbe71SArnaud Ebalard 		goto out;
332594fbe71SArnaud Ebalard 	}
333398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
334594fbe71SArnaud Ebalard 					data->fan_cmd1);
335594fbe71SArnaud Ebalard 	data->valid = false;
336594fbe71SArnaud Ebalard  out:
337594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
338594fbe71SArnaud Ebalard 
339594fbe71SArnaud Ebalard 	return ret;
340594fbe71SArnaud Ebalard }
341594fbe71SArnaud Ebalard 
342594fbe71SArnaud Ebalard /* Set fan gear mode. Accepts either 0, 1 or 2. */
343594fbe71SArnaud Ebalard static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
344594fbe71SArnaud Ebalard {
345594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
346594fbe71SArnaud Ebalard 	int ret;
347594fbe71SArnaud Ebalard 
348594fbe71SArnaud Ebalard 	if (IS_ERR(data))
349594fbe71SArnaud Ebalard 		return PTR_ERR(data);
350594fbe71SArnaud Ebalard 
351594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
352594fbe71SArnaud Ebalard 	switch (val) {
353594fbe71SArnaud Ebalard 	case 0:
354594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
355594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
356594fbe71SArnaud Ebalard 		break;
357594fbe71SArnaud Ebalard 	case 1:
358594fbe71SArnaud Ebalard 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_0;
359594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
360594fbe71SArnaud Ebalard 		break;
361594fbe71SArnaud Ebalard 	case 2:
362594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
363594fbe71SArnaud Ebalard 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_1;
364594fbe71SArnaud Ebalard 		break;
365594fbe71SArnaud Ebalard 	default:
366594fbe71SArnaud Ebalard 		ret = -EINVAL;
367594fbe71SArnaud Ebalard 		goto out;
368594fbe71SArnaud Ebalard 	}
369398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
370594fbe71SArnaud Ebalard 					data->fan_cmd2);
371594fbe71SArnaud Ebalard 	data->valid = false;
372594fbe71SArnaud Ebalard  out:
373594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
374594fbe71SArnaud Ebalard 
375594fbe71SArnaud Ebalard 	return ret;
376594fbe71SArnaud Ebalard }
377594fbe71SArnaud Ebalard 
378594fbe71SArnaud Ebalard /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
379594fbe71SArnaud Ebalard static int do_set_fan_pulses(struct device *dev, unsigned long val)
380594fbe71SArnaud Ebalard {
381594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
382594fbe71SArnaud Ebalard 	int ret;
383594fbe71SArnaud Ebalard 
384594fbe71SArnaud Ebalard 	if (IS_ERR(data))
385594fbe71SArnaud Ebalard 		return PTR_ERR(data);
386594fbe71SArnaud Ebalard 
387594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
388594fbe71SArnaud Ebalard 	switch (val) {
389594fbe71SArnaud Ebalard 	case 2:
390594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
391594fbe71SArnaud Ebalard 		break;
392594fbe71SArnaud Ebalard 	case 4:
393594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PULSE_PER_REV;
394594fbe71SArnaud Ebalard 		break;
395594fbe71SArnaud Ebalard 	default:
396594fbe71SArnaud Ebalard 		ret = -EINVAL;
397594fbe71SArnaud Ebalard 		goto out;
398594fbe71SArnaud Ebalard 	}
399398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
400594fbe71SArnaud Ebalard 					data->fan_cmd1);
401594fbe71SArnaud Ebalard 	data->valid = false;
402594fbe71SArnaud Ebalard  out:
403594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
404594fbe71SArnaud Ebalard 
405594fbe71SArnaud Ebalard 	return ret;
406594fbe71SArnaud Ebalard }
407594fbe71SArnaud Ebalard 
408594fbe71SArnaud Ebalard /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
409594fbe71SArnaud Ebalard static int do_set_pwm_enable(struct device *dev, unsigned long val)
410594fbe71SArnaud Ebalard {
411594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
412594fbe71SArnaud Ebalard 	int ret;
413594fbe71SArnaud Ebalard 
414594fbe71SArnaud Ebalard 	if (IS_ERR(data))
415594fbe71SArnaud Ebalard 		return PTR_ERR(data);
416594fbe71SArnaud Ebalard 
417594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
418594fbe71SArnaud Ebalard 	switch (val) {
419594fbe71SArnaud Ebalard 	case G762_FAN_MODE_CLOSED_LOOP:
420594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_FAN_MODE;
421594fbe71SArnaud Ebalard 		break;
422594fbe71SArnaud Ebalard 	case G762_FAN_MODE_OPEN_LOOP:
423594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
424594fbe71SArnaud Ebalard 		/*
425594fbe71SArnaud Ebalard 		 * BUG FIX: if SET_CNT register value is 255 then, for some
426594fbe71SArnaud Ebalard 		 * unknown reason, fan will not rotate as expected, no matter
427594fbe71SArnaud Ebalard 		 * the value of SET_OUT (to be specific, this seems to happen
428594fbe71SArnaud Ebalard 		 * only in PWM mode). To workaround this bug, we give SET_CNT
429594fbe71SArnaud Ebalard 		 * value of 254 if it is 255 when switching to open-loop.
430594fbe71SArnaud Ebalard 		 */
431594fbe71SArnaud Ebalard 		if (data->set_cnt == 0xff)
432398e16dbSAxel Lin 			i2c_smbus_write_byte_data(data->client,
433398e16dbSAxel Lin 						  G762_REG_SET_CNT, 254);
434594fbe71SArnaud Ebalard 		break;
435594fbe71SArnaud Ebalard 	default:
436594fbe71SArnaud Ebalard 		ret = -EINVAL;
437594fbe71SArnaud Ebalard 		goto out;
438594fbe71SArnaud Ebalard 	}
439594fbe71SArnaud Ebalard 
440398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
441594fbe71SArnaud Ebalard 					data->fan_cmd1);
442594fbe71SArnaud Ebalard 	data->valid = false;
443594fbe71SArnaud Ebalard  out:
444594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
445594fbe71SArnaud Ebalard 
446594fbe71SArnaud Ebalard 	return ret;
447594fbe71SArnaud Ebalard }
448594fbe71SArnaud Ebalard 
449594fbe71SArnaud Ebalard /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
450594fbe71SArnaud Ebalard static int do_set_pwm_polarity(struct device *dev, unsigned long val)
451594fbe71SArnaud Ebalard {
452594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
453594fbe71SArnaud Ebalard 	int ret;
454594fbe71SArnaud Ebalard 
455594fbe71SArnaud Ebalard 	if (IS_ERR(data))
456594fbe71SArnaud Ebalard 		return PTR_ERR(data);
457594fbe71SArnaud Ebalard 
458594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
459594fbe71SArnaud Ebalard 	switch (val) {
460594fbe71SArnaud Ebalard 	case G762_PWM_POLARITY_POSITIVE:
461594fbe71SArnaud Ebalard 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
462594fbe71SArnaud Ebalard 		break;
463594fbe71SArnaud Ebalard 	case G762_PWM_POLARITY_NEGATIVE:
464594fbe71SArnaud Ebalard 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PWM_POLARITY;
465594fbe71SArnaud Ebalard 		break;
466594fbe71SArnaud Ebalard 	default:
467594fbe71SArnaud Ebalard 		ret = -EINVAL;
468594fbe71SArnaud Ebalard 		goto out;
469594fbe71SArnaud Ebalard 	}
470398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
471594fbe71SArnaud Ebalard 					data->fan_cmd1);
472594fbe71SArnaud Ebalard 	data->valid = false;
473594fbe71SArnaud Ebalard  out:
474594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
475594fbe71SArnaud Ebalard 
476594fbe71SArnaud Ebalard 	return ret;
477594fbe71SArnaud Ebalard }
478594fbe71SArnaud Ebalard 
479594fbe71SArnaud Ebalard /*
480594fbe71SArnaud Ebalard  * Set pwm value. Accepts values between 0 (stops the fan) and
481594fbe71SArnaud Ebalard  * 255 (full speed). This only makes sense in open-loop mode.
482594fbe71SArnaud Ebalard  */
483594fbe71SArnaud Ebalard static int do_set_pwm(struct device *dev, unsigned long val)
484594fbe71SArnaud Ebalard {
485398e16dbSAxel Lin 	struct g762_data *data = dev_get_drvdata(dev);
486398e16dbSAxel Lin 	struct i2c_client *client = data->client;
487594fbe71SArnaud Ebalard 	int ret;
488594fbe71SArnaud Ebalard 
489594fbe71SArnaud Ebalard 	if (val > 255)
490594fbe71SArnaud Ebalard 		return -EINVAL;
491594fbe71SArnaud Ebalard 
492594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
493594fbe71SArnaud Ebalard 	ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
494594fbe71SArnaud Ebalard 	data->valid = false;
495594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
496594fbe71SArnaud Ebalard 
497594fbe71SArnaud Ebalard 	return ret;
498594fbe71SArnaud Ebalard }
499594fbe71SArnaud Ebalard 
500594fbe71SArnaud Ebalard /*
501594fbe71SArnaud Ebalard  * Set fan RPM value. Can be called both in closed and open-loop mode
502594fbe71SArnaud Ebalard  * but effect will only be seen after closed-loop mode is configured.
503594fbe71SArnaud Ebalard  */
504594fbe71SArnaud Ebalard static int do_set_fan_target(struct device *dev, unsigned long val)
505594fbe71SArnaud Ebalard {
506594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
507594fbe71SArnaud Ebalard 	int ret;
508594fbe71SArnaud Ebalard 
509594fbe71SArnaud Ebalard 	if (IS_ERR(data))
510594fbe71SArnaud Ebalard 		return PTR_ERR(data);
511594fbe71SArnaud Ebalard 
512594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
513594fbe71SArnaud Ebalard 	data->set_cnt = cnt_from_rpm(val, data->clk_freq,
514594fbe71SArnaud Ebalard 				     G762_PULSE_FROM_REG(data->fan_cmd1),
515594fbe71SArnaud Ebalard 				     G762_CLKDIV_FROM_REG(data->fan_cmd1),
516594fbe71SArnaud Ebalard 				     G762_GEARMULT_FROM_REG(data->fan_cmd2));
517398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
518594fbe71SArnaud Ebalard 					data->set_cnt);
519594fbe71SArnaud Ebalard 	data->valid = false;
520594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
521594fbe71SArnaud Ebalard 
522594fbe71SArnaud Ebalard 	return ret;
523594fbe71SArnaud Ebalard }
524594fbe71SArnaud Ebalard 
525594fbe71SArnaud Ebalard /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
526594fbe71SArnaud Ebalard static int do_set_fan_startv(struct device *dev, unsigned long val)
527594fbe71SArnaud Ebalard {
528594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
529594fbe71SArnaud Ebalard 	int ret;
530594fbe71SArnaud Ebalard 
531594fbe71SArnaud Ebalard 	if (IS_ERR(data))
532594fbe71SArnaud Ebalard 		return PTR_ERR(data);
533594fbe71SArnaud Ebalard 
534594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
535594fbe71SArnaud Ebalard 	switch (val) {
536594fbe71SArnaud Ebalard 	case 0:
537594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
538594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
539594fbe71SArnaud Ebalard 		break;
540594fbe71SArnaud Ebalard 	case 1:
541594fbe71SArnaud Ebalard 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
542594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
543594fbe71SArnaud Ebalard 		break;
544594fbe71SArnaud Ebalard 	case 2:
545594fbe71SArnaud Ebalard 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
546594fbe71SArnaud Ebalard 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
547594fbe71SArnaud Ebalard 		break;
548594fbe71SArnaud Ebalard 	case 3:
549594fbe71SArnaud Ebalard 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
550594fbe71SArnaud Ebalard 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
551594fbe71SArnaud Ebalard 		break;
552594fbe71SArnaud Ebalard 	default:
553594fbe71SArnaud Ebalard 		ret = -EINVAL;
554594fbe71SArnaud Ebalard 		goto out;
555594fbe71SArnaud Ebalard 	}
556398e16dbSAxel Lin 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
557594fbe71SArnaud Ebalard 					data->fan_cmd2);
558594fbe71SArnaud Ebalard 	data->valid = false;
559594fbe71SArnaud Ebalard  out:
560594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
561594fbe71SArnaud Ebalard 
562594fbe71SArnaud Ebalard 	return ret;
563594fbe71SArnaud Ebalard }
564594fbe71SArnaud Ebalard 
565594fbe71SArnaud Ebalard /*
566594fbe71SArnaud Ebalard  * Helper to import hardware characteristics from .dts file and push
567594fbe71SArnaud Ebalard  * those to the chip.
568594fbe71SArnaud Ebalard  */
569594fbe71SArnaud Ebalard 
570594fbe71SArnaud Ebalard #ifdef CONFIG_OF
571adaa50b0SJingoo Han static const struct of_device_id g762_dt_match[] = {
5726ce40232SChristian Marangi 	{ .compatible = "gmt,g761" },
573594fbe71SArnaud Ebalard 	{ .compatible = "gmt,g762" },
574594fbe71SArnaud Ebalard 	{ .compatible = "gmt,g763" },
575594fbe71SArnaud Ebalard 	{ },
576594fbe71SArnaud Ebalard };
577de66b380SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, g762_dt_match);
578594fbe71SArnaud Ebalard 
579594fbe71SArnaud Ebalard /*
580594fbe71SArnaud Ebalard  * Grab clock (a required property), enable it, get (fixed) clock frequency
581594fbe71SArnaud Ebalard  * and store it. Note: upon success, clock has been prepared and enabled; it
582594fbe71SArnaud Ebalard  * must later be unprepared and disabled (e.g. during module unloading) by a
583594fbe71SArnaud Ebalard  * call to g762_of_clock_disable(). Note that a reference to clock is kept
584594fbe71SArnaud Ebalard  * in our private data structure to be used in this function.
585594fbe71SArnaud Ebalard  */
58647591baaSHao Peng static void g762_of_clock_disable(void *data)
58747591baaSHao Peng {
58847591baaSHao Peng 	struct g762_data *g762 = data;
58947591baaSHao Peng 
59047591baaSHao Peng 	clk_disable_unprepare(g762->clk);
59147591baaSHao Peng 	clk_put(g762->clk);
59247591baaSHao Peng }
59347591baaSHao Peng 
594594fbe71SArnaud Ebalard static int g762_of_clock_enable(struct i2c_client *client)
595594fbe71SArnaud Ebalard {
596594fbe71SArnaud Ebalard 	struct g762_data *data;
597594fbe71SArnaud Ebalard 	unsigned long clk_freq;
598594fbe71SArnaud Ebalard 	struct clk *clk;
599594fbe71SArnaud Ebalard 	int ret;
600594fbe71SArnaud Ebalard 
601594fbe71SArnaud Ebalard 	if (!client->dev.of_node)
602594fbe71SArnaud Ebalard 		return 0;
603594fbe71SArnaud Ebalard 
6046ce40232SChristian Marangi 	data = i2c_get_clientdata(client);
6056ce40232SChristian Marangi 
6066ce40232SChristian Marangi 	/*
6076ce40232SChristian Marangi 	 * Skip CLK detection and handling if we use internal clock.
6086ce40232SChristian Marangi 	 * This is only valid for g761.
6096ce40232SChristian Marangi 	 */
6106ce40232SChristian Marangi 	data->internal_clock = of_device_is_compatible(client->dev.of_node,
6116ce40232SChristian Marangi 						       "gmt,g761") &&
6126ce40232SChristian Marangi 			       !of_property_present(client->dev.of_node,
6136ce40232SChristian Marangi 						    "clocks");
6146ce40232SChristian Marangi 	if (data->internal_clock) {
6156ce40232SChristian Marangi 		do_set_clk_freq(&client->dev, 32768);
6166ce40232SChristian Marangi 		return 0;
6176ce40232SChristian Marangi 	}
6186ce40232SChristian Marangi 
619594fbe71SArnaud Ebalard 	clk = of_clk_get(client->dev.of_node, 0);
620594fbe71SArnaud Ebalard 	if (IS_ERR(clk)) {
621594fbe71SArnaud Ebalard 		dev_err(&client->dev, "failed to get clock\n");
622594fbe71SArnaud Ebalard 		return PTR_ERR(clk);
623594fbe71SArnaud Ebalard 	}
624594fbe71SArnaud Ebalard 
625594fbe71SArnaud Ebalard 	ret = clk_prepare_enable(clk);
626594fbe71SArnaud Ebalard 	if (ret) {
627594fbe71SArnaud Ebalard 		dev_err(&client->dev, "failed to enable clock\n");
628594fbe71SArnaud Ebalard 		goto clk_put;
629594fbe71SArnaud Ebalard 	}
630594fbe71SArnaud Ebalard 
631594fbe71SArnaud Ebalard 	clk_freq = clk_get_rate(clk);
632594fbe71SArnaud Ebalard 	ret = do_set_clk_freq(&client->dev, clk_freq);
633594fbe71SArnaud Ebalard 	if (ret) {
634594fbe71SArnaud Ebalard 		dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
635594fbe71SArnaud Ebalard 		goto clk_unprep;
636594fbe71SArnaud Ebalard 	}
637594fbe71SArnaud Ebalard 
638594fbe71SArnaud Ebalard 	data->clk = clk;
639594fbe71SArnaud Ebalard 
64092e58a87SKang Chen 	ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
64192e58a87SKang Chen 	if (ret) {
64292e58a87SKang Chen 		dev_err(&client->dev, "failed to add disable clock action\n");
64392e58a87SKang Chen 		goto clk_unprep;
64492e58a87SKang Chen 	}
64592e58a87SKang Chen 
646594fbe71SArnaud Ebalard 	return 0;
647594fbe71SArnaud Ebalard 
648594fbe71SArnaud Ebalard  clk_unprep:
649594fbe71SArnaud Ebalard 	clk_disable_unprepare(clk);
650594fbe71SArnaud Ebalard 
651594fbe71SArnaud Ebalard  clk_put:
652594fbe71SArnaud Ebalard 	clk_put(clk);
653594fbe71SArnaud Ebalard 
654594fbe71SArnaud Ebalard 	return ret;
655594fbe71SArnaud Ebalard }
656594fbe71SArnaud Ebalard 
657594fbe71SArnaud Ebalard static int g762_of_prop_import_one(struct i2c_client *client,
658594fbe71SArnaud Ebalard 				   const char *pname,
659594fbe71SArnaud Ebalard 				   int (*psetter)(struct device *dev,
660594fbe71SArnaud Ebalard 						  unsigned long val))
661594fbe71SArnaud Ebalard {
662fce9626cSAxel Lin 	int ret;
663594fbe71SArnaud Ebalard 	u32 pval;
664594fbe71SArnaud Ebalard 
665fce9626cSAxel Lin 	if (of_property_read_u32(client->dev.of_node, pname, &pval))
666594fbe71SArnaud Ebalard 		return 0;
667594fbe71SArnaud Ebalard 
668594fbe71SArnaud Ebalard 	dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
669594fbe71SArnaud Ebalard 	ret = (*psetter)(&client->dev, pval);
670594fbe71SArnaud Ebalard 	if (ret)
671594fbe71SArnaud Ebalard 		dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
672594fbe71SArnaud Ebalard 
673594fbe71SArnaud Ebalard 	return ret;
674594fbe71SArnaud Ebalard }
675594fbe71SArnaud Ebalard 
676594fbe71SArnaud Ebalard static int g762_of_prop_import(struct i2c_client *client)
677594fbe71SArnaud Ebalard {
678594fbe71SArnaud Ebalard 	int ret;
679594fbe71SArnaud Ebalard 
680594fbe71SArnaud Ebalard 	if (!client->dev.of_node)
681594fbe71SArnaud Ebalard 		return 0;
682594fbe71SArnaud Ebalard 
683594fbe71SArnaud Ebalard 	ret = g762_of_prop_import_one(client, "fan_gear_mode",
684594fbe71SArnaud Ebalard 				      do_set_fan_gear_mode);
685594fbe71SArnaud Ebalard 	if (ret)
686594fbe71SArnaud Ebalard 		return ret;
687594fbe71SArnaud Ebalard 
688594fbe71SArnaud Ebalard 	ret = g762_of_prop_import_one(client, "pwm_polarity",
689594fbe71SArnaud Ebalard 				      do_set_pwm_polarity);
690594fbe71SArnaud Ebalard 	if (ret)
691594fbe71SArnaud Ebalard 		return ret;
692594fbe71SArnaud Ebalard 
693594fbe71SArnaud Ebalard 	return g762_of_prop_import_one(client, "fan_startv",
694594fbe71SArnaud Ebalard 				       do_set_fan_startv);
695594fbe71SArnaud Ebalard }
696594fbe71SArnaud Ebalard 
697594fbe71SArnaud Ebalard #else
698594fbe71SArnaud Ebalard static int g762_of_prop_import(struct i2c_client *client)
699594fbe71SArnaud Ebalard {
700594fbe71SArnaud Ebalard 	return 0;
701594fbe71SArnaud Ebalard }
702594fbe71SArnaud Ebalard 
703594fbe71SArnaud Ebalard static int g762_of_clock_enable(struct i2c_client *client)
704594fbe71SArnaud Ebalard {
705594fbe71SArnaud Ebalard 	return 0;
706594fbe71SArnaud Ebalard }
707594fbe71SArnaud Ebalard #endif
708594fbe71SArnaud Ebalard 
709594fbe71SArnaud Ebalard /*
710594fbe71SArnaud Ebalard  * Helper to import hardware characteristics from .dts file and push
711594fbe71SArnaud Ebalard  * those to the chip.
712594fbe71SArnaud Ebalard  */
713594fbe71SArnaud Ebalard 
714594fbe71SArnaud Ebalard static int g762_pdata_prop_import(struct i2c_client *client)
715594fbe71SArnaud Ebalard {
716a8b3a3a5SJingoo Han 	struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
717594fbe71SArnaud Ebalard 	int ret;
718594fbe71SArnaud Ebalard 
719594fbe71SArnaud Ebalard 	if (!pdata)
720594fbe71SArnaud Ebalard 		return 0;
721594fbe71SArnaud Ebalard 
722594fbe71SArnaud Ebalard 	ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
723594fbe71SArnaud Ebalard 	if (ret)
724594fbe71SArnaud Ebalard 		return ret;
725594fbe71SArnaud Ebalard 
726594fbe71SArnaud Ebalard 	ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
727594fbe71SArnaud Ebalard 	if (ret)
728594fbe71SArnaud Ebalard 		return ret;
729594fbe71SArnaud Ebalard 
730594fbe71SArnaud Ebalard 	ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
731594fbe71SArnaud Ebalard 	if (ret)
732594fbe71SArnaud Ebalard 		return ret;
733594fbe71SArnaud Ebalard 
734594fbe71SArnaud Ebalard 	return do_set_clk_freq(&client->dev, pdata->clk_freq);
735594fbe71SArnaud Ebalard }
736594fbe71SArnaud Ebalard 
737594fbe71SArnaud Ebalard /*
738594fbe71SArnaud Ebalard  * sysfs attributes
739594fbe71SArnaud Ebalard  */
740594fbe71SArnaud Ebalard 
741594fbe71SArnaud Ebalard /*
742594fbe71SArnaud Ebalard  * Read function for fan1_input sysfs file. Return current fan RPM value, or
743594fbe71SArnaud Ebalard  * 0 if fan is out of control.
744594fbe71SArnaud Ebalard  */
745251f65cdSJulia Lawall static ssize_t fan1_input_show(struct device *dev,
746251f65cdSJulia Lawall 			       struct device_attribute *da, char *buf)
747594fbe71SArnaud Ebalard {
748594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
749594fbe71SArnaud Ebalard 	unsigned int rpm = 0;
750594fbe71SArnaud Ebalard 
751594fbe71SArnaud Ebalard 	if (IS_ERR(data))
752594fbe71SArnaud Ebalard 		return PTR_ERR(data);
753594fbe71SArnaud Ebalard 
754594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
755594fbe71SArnaud Ebalard 	/* reverse logic: fan out of control reporting is enabled low */
756594fbe71SArnaud Ebalard 	if (data->fan_sta & G762_REG_FAN_STA_OOC) {
757594fbe71SArnaud Ebalard 		rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
758594fbe71SArnaud Ebalard 				   G762_PULSE_FROM_REG(data->fan_cmd1),
759594fbe71SArnaud Ebalard 				   G762_CLKDIV_FROM_REG(data->fan_cmd1),
760594fbe71SArnaud Ebalard 				   G762_GEARMULT_FROM_REG(data->fan_cmd2));
761594fbe71SArnaud Ebalard 	}
762594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
763594fbe71SArnaud Ebalard 
764594fbe71SArnaud Ebalard 	return sprintf(buf, "%u\n", rpm);
765594fbe71SArnaud Ebalard }
766594fbe71SArnaud Ebalard 
767594fbe71SArnaud Ebalard /*
768594fbe71SArnaud Ebalard  * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
769594fbe71SArnaud Ebalard  * control mode i.e. PWM (1) or DC (0).
770594fbe71SArnaud Ebalard  */
771251f65cdSJulia Lawall static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da,
772594fbe71SArnaud Ebalard 			      char *buf)
773594fbe71SArnaud Ebalard {
774594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
775594fbe71SArnaud Ebalard 
776594fbe71SArnaud Ebalard 	if (IS_ERR(data))
777594fbe71SArnaud Ebalard 		return PTR_ERR(data);
778594fbe71SArnaud Ebalard 
779594fbe71SArnaud Ebalard 	return sprintf(buf, "%d\n",
780594fbe71SArnaud Ebalard 		       !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
781594fbe71SArnaud Ebalard }
782594fbe71SArnaud Ebalard 
783251f65cdSJulia Lawall static ssize_t pwm1_mode_store(struct device *dev,
784251f65cdSJulia Lawall 			       struct device_attribute *da, const char *buf,
785251f65cdSJulia Lawall 			       size_t count)
786594fbe71SArnaud Ebalard {
787594fbe71SArnaud Ebalard 	unsigned long val;
788594fbe71SArnaud Ebalard 	int ret;
789594fbe71SArnaud Ebalard 
790594fbe71SArnaud Ebalard 	if (kstrtoul(buf, 10, &val))
791594fbe71SArnaud Ebalard 		return -EINVAL;
792594fbe71SArnaud Ebalard 
793594fbe71SArnaud Ebalard 	ret = do_set_pwm_mode(dev, val);
794594fbe71SArnaud Ebalard 	if (ret < 0)
795594fbe71SArnaud Ebalard 		return ret;
796594fbe71SArnaud Ebalard 
797594fbe71SArnaud Ebalard 	return count;
798594fbe71SArnaud Ebalard }
799594fbe71SArnaud Ebalard 
800594fbe71SArnaud Ebalard /*
801594fbe71SArnaud Ebalard  * Read and write functions for fan1_div sysfs file. Get and set fan
802594fbe71SArnaud Ebalard  * controller prescaler value
803594fbe71SArnaud Ebalard  */
804251f65cdSJulia Lawall static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da,
805251f65cdSJulia Lawall 			     char *buf)
806594fbe71SArnaud Ebalard {
807594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
808594fbe71SArnaud Ebalard 
809594fbe71SArnaud Ebalard 	if (IS_ERR(data))
810594fbe71SArnaud Ebalard 		return PTR_ERR(data);
811594fbe71SArnaud Ebalard 
812594fbe71SArnaud Ebalard 	return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
813594fbe71SArnaud Ebalard }
814594fbe71SArnaud Ebalard 
815251f65cdSJulia Lawall static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
816594fbe71SArnaud Ebalard 			      const char *buf, size_t count)
817594fbe71SArnaud Ebalard {
818594fbe71SArnaud Ebalard 	unsigned long val;
819594fbe71SArnaud Ebalard 	int ret;
820594fbe71SArnaud Ebalard 
821594fbe71SArnaud Ebalard 	if (kstrtoul(buf, 10, &val))
822594fbe71SArnaud Ebalard 		return -EINVAL;
823594fbe71SArnaud Ebalard 
824594fbe71SArnaud Ebalard 	ret = do_set_fan_div(dev, val);
825594fbe71SArnaud Ebalard 	if (ret < 0)
826594fbe71SArnaud Ebalard 		return ret;
827594fbe71SArnaud Ebalard 
828594fbe71SArnaud Ebalard 	return count;
829594fbe71SArnaud Ebalard }
830594fbe71SArnaud Ebalard 
831594fbe71SArnaud Ebalard /*
832594fbe71SArnaud Ebalard  * Read and write functions for fan1_pulses sysfs file. Get and set number
833594fbe71SArnaud Ebalard  * of tachometer pulses per fan revolution.
834594fbe71SArnaud Ebalard  */
835251f65cdSJulia Lawall static ssize_t fan1_pulses_show(struct device *dev,
836594fbe71SArnaud Ebalard 				struct device_attribute *da, char *buf)
837594fbe71SArnaud Ebalard {
838594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
839594fbe71SArnaud Ebalard 
840594fbe71SArnaud Ebalard 	if (IS_ERR(data))
841594fbe71SArnaud Ebalard 		return PTR_ERR(data);
842594fbe71SArnaud Ebalard 
843594fbe71SArnaud Ebalard 	return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
844594fbe71SArnaud Ebalard }
845594fbe71SArnaud Ebalard 
846251f65cdSJulia Lawall static ssize_t fan1_pulses_store(struct device *dev,
847251f65cdSJulia Lawall 				 struct device_attribute *da, const char *buf,
848251f65cdSJulia Lawall 				 size_t count)
849594fbe71SArnaud Ebalard {
850594fbe71SArnaud Ebalard 	unsigned long val;
851594fbe71SArnaud Ebalard 	int ret;
852594fbe71SArnaud Ebalard 
853594fbe71SArnaud Ebalard 	if (kstrtoul(buf, 10, &val))
854594fbe71SArnaud Ebalard 		return -EINVAL;
855594fbe71SArnaud Ebalard 
856594fbe71SArnaud Ebalard 	ret = do_set_fan_pulses(dev, val);
857594fbe71SArnaud Ebalard 	if (ret < 0)
858594fbe71SArnaud Ebalard 		return ret;
859594fbe71SArnaud Ebalard 
860594fbe71SArnaud Ebalard 	return count;
861594fbe71SArnaud Ebalard }
862594fbe71SArnaud Ebalard 
863594fbe71SArnaud Ebalard /*
864594fbe71SArnaud Ebalard  * Read and write functions for pwm1_enable. Get and set fan speed control mode
865594fbe71SArnaud Ebalard  * (i.e. closed or open-loop).
866594fbe71SArnaud Ebalard  *
867594fbe71SArnaud Ebalard  * Following documentation about hwmon's sysfs interface, a pwm1_enable node
8684091fb95SMasahiro Yamada  * should accept the following:
869594fbe71SArnaud Ebalard  *
870594fbe71SArnaud Ebalard  *  0 : no fan speed control (i.e. fan at full speed)
871594fbe71SArnaud Ebalard  *  1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
872594fbe71SArnaud Ebalard  *  2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
873594fbe71SArnaud Ebalard  *
874594fbe71SArnaud Ebalard  * but we do not accept 0 as this mode is not natively supported by the chip
875594fbe71SArnaud Ebalard  * and it is not emulated by g762 driver. -EINVAL is returned in this case.
876594fbe71SArnaud Ebalard  */
877251f65cdSJulia Lawall static ssize_t pwm1_enable_show(struct device *dev,
878594fbe71SArnaud Ebalard 				struct device_attribute *da, char *buf)
879594fbe71SArnaud Ebalard {
880594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
881594fbe71SArnaud Ebalard 
882594fbe71SArnaud Ebalard 	if (IS_ERR(data))
883594fbe71SArnaud Ebalard 		return PTR_ERR(data);
884594fbe71SArnaud Ebalard 
885594fbe71SArnaud Ebalard 	return sprintf(buf, "%d\n",
886594fbe71SArnaud Ebalard 		       (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
887594fbe71SArnaud Ebalard }
888594fbe71SArnaud Ebalard 
889251f65cdSJulia Lawall static ssize_t pwm1_enable_store(struct device *dev,
890251f65cdSJulia Lawall 				 struct device_attribute *da, const char *buf,
891251f65cdSJulia Lawall 				 size_t count)
892594fbe71SArnaud Ebalard {
893594fbe71SArnaud Ebalard 	unsigned long val;
894594fbe71SArnaud Ebalard 	int ret;
895594fbe71SArnaud Ebalard 
896594fbe71SArnaud Ebalard 	if (kstrtoul(buf, 10, &val))
897594fbe71SArnaud Ebalard 		return -EINVAL;
898594fbe71SArnaud Ebalard 
899594fbe71SArnaud Ebalard 	ret = do_set_pwm_enable(dev, val);
900594fbe71SArnaud Ebalard 	if (ret < 0)
901594fbe71SArnaud Ebalard 		return ret;
902594fbe71SArnaud Ebalard 
903594fbe71SArnaud Ebalard 	return count;
904594fbe71SArnaud Ebalard }
905594fbe71SArnaud Ebalard 
906594fbe71SArnaud Ebalard /*
907594fbe71SArnaud Ebalard  * Read and write functions for pwm1 sysfs file. Get and set pwm value
908594fbe71SArnaud Ebalard  * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
909594fbe71SArnaud Ebalard  * makes it run at full speed.
910594fbe71SArnaud Ebalard  */
911251f65cdSJulia Lawall static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
912594fbe71SArnaud Ebalard 			 char *buf)
913594fbe71SArnaud Ebalard {
914594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
915594fbe71SArnaud Ebalard 
916594fbe71SArnaud Ebalard 	if (IS_ERR(data))
917594fbe71SArnaud Ebalard 		return PTR_ERR(data);
918594fbe71SArnaud Ebalard 
919594fbe71SArnaud Ebalard 	return sprintf(buf, "%d\n", data->set_out);
920594fbe71SArnaud Ebalard }
921594fbe71SArnaud Ebalard 
922251f65cdSJulia Lawall static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
923594fbe71SArnaud Ebalard 			  const char *buf, size_t count)
924594fbe71SArnaud Ebalard {
925594fbe71SArnaud Ebalard 	unsigned long val;
926594fbe71SArnaud Ebalard 	int ret;
927594fbe71SArnaud Ebalard 
928594fbe71SArnaud Ebalard 	if (kstrtoul(buf, 10, &val))
929594fbe71SArnaud Ebalard 		return -EINVAL;
930594fbe71SArnaud Ebalard 
931594fbe71SArnaud Ebalard 	ret = do_set_pwm(dev, val);
932594fbe71SArnaud Ebalard 	if (ret < 0)
933594fbe71SArnaud Ebalard 		return ret;
934594fbe71SArnaud Ebalard 
935594fbe71SArnaud Ebalard 	return count;
936594fbe71SArnaud Ebalard }
937594fbe71SArnaud Ebalard 
938594fbe71SArnaud Ebalard /*
939594fbe71SArnaud Ebalard  * Read and write function for fan1_target sysfs file. Get/set the fan speed in
940594fbe71SArnaud Ebalard  * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
941594fbe71SArnaud Ebalard  * the fan speed using pulses from fan tachometer.
942594fbe71SArnaud Ebalard  *
943594fbe71SArnaud Ebalard  * Refer to rpm_from_cnt() implementation above to get info about count number
944594fbe71SArnaud Ebalard  * calculation.
945594fbe71SArnaud Ebalard  *
946594fbe71SArnaud Ebalard  * Also note that due to rounding errors it is possible that you don't read
947594fbe71SArnaud Ebalard  * back exactly the value you have set.
948594fbe71SArnaud Ebalard  */
949251f65cdSJulia Lawall static ssize_t fan1_target_show(struct device *dev,
950251f65cdSJulia Lawall 				struct device_attribute *da, char *buf)
951594fbe71SArnaud Ebalard {
952594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
953594fbe71SArnaud Ebalard 	unsigned int rpm;
954594fbe71SArnaud Ebalard 
955594fbe71SArnaud Ebalard 	if (IS_ERR(data))
956594fbe71SArnaud Ebalard 		return PTR_ERR(data);
957594fbe71SArnaud Ebalard 
958594fbe71SArnaud Ebalard 	mutex_lock(&data->update_lock);
959594fbe71SArnaud Ebalard 	rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
960594fbe71SArnaud Ebalard 			   G762_PULSE_FROM_REG(data->fan_cmd1),
961594fbe71SArnaud Ebalard 			   G762_CLKDIV_FROM_REG(data->fan_cmd1),
962594fbe71SArnaud Ebalard 			   G762_GEARMULT_FROM_REG(data->fan_cmd2));
963594fbe71SArnaud Ebalard 	mutex_unlock(&data->update_lock);
964594fbe71SArnaud Ebalard 
965594fbe71SArnaud Ebalard 	return sprintf(buf, "%u\n", rpm);
966594fbe71SArnaud Ebalard }
967594fbe71SArnaud Ebalard 
968251f65cdSJulia Lawall static ssize_t fan1_target_store(struct device *dev,
969251f65cdSJulia Lawall 				 struct device_attribute *da, const char *buf,
970251f65cdSJulia Lawall 				 size_t count)
971594fbe71SArnaud Ebalard {
972594fbe71SArnaud Ebalard 	unsigned long val;
973594fbe71SArnaud Ebalard 	int ret;
974594fbe71SArnaud Ebalard 
975594fbe71SArnaud Ebalard 	if (kstrtoul(buf, 10, &val))
976594fbe71SArnaud Ebalard 		return -EINVAL;
977594fbe71SArnaud Ebalard 
978594fbe71SArnaud Ebalard 	ret = do_set_fan_target(dev, val);
979594fbe71SArnaud Ebalard 	if (ret < 0)
980594fbe71SArnaud Ebalard 		return ret;
981594fbe71SArnaud Ebalard 
982594fbe71SArnaud Ebalard 	return count;
983594fbe71SArnaud Ebalard }
984594fbe71SArnaud Ebalard 
985594fbe71SArnaud Ebalard /* read function for fan1_fault sysfs file. */
986251f65cdSJulia Lawall static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da,
987594fbe71SArnaud Ebalard 			       char *buf)
988594fbe71SArnaud Ebalard {
989594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
990594fbe71SArnaud Ebalard 
991594fbe71SArnaud Ebalard 	if (IS_ERR(data))
992594fbe71SArnaud Ebalard 		return PTR_ERR(data);
993594fbe71SArnaud Ebalard 
994594fbe71SArnaud Ebalard 	return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
995594fbe71SArnaud Ebalard }
996594fbe71SArnaud Ebalard 
997594fbe71SArnaud Ebalard /*
998594fbe71SArnaud Ebalard  * read function for fan1_alarm sysfs file. Note that OOC condition is
999594fbe71SArnaud Ebalard  * enabled low
1000594fbe71SArnaud Ebalard  */
1001251f65cdSJulia Lawall static ssize_t fan1_alarm_show(struct device *dev,
1002251f65cdSJulia Lawall 			       struct device_attribute *da, char *buf)
1003594fbe71SArnaud Ebalard {
1004594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
1005594fbe71SArnaud Ebalard 
1006594fbe71SArnaud Ebalard 	if (IS_ERR(data))
1007594fbe71SArnaud Ebalard 		return PTR_ERR(data);
1008594fbe71SArnaud Ebalard 
1009594fbe71SArnaud Ebalard 	return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
1010594fbe71SArnaud Ebalard }
1011594fbe71SArnaud Ebalard 
1012251f65cdSJulia Lawall static DEVICE_ATTR_RW(pwm1);
1013251f65cdSJulia Lawall static DEVICE_ATTR_RW(pwm1_mode);
1014251f65cdSJulia Lawall static DEVICE_ATTR_RW(pwm1_enable);
1015251f65cdSJulia Lawall static DEVICE_ATTR_RO(fan1_input);
1016251f65cdSJulia Lawall static DEVICE_ATTR_RO(fan1_alarm);
1017251f65cdSJulia Lawall static DEVICE_ATTR_RO(fan1_fault);
1018251f65cdSJulia Lawall static DEVICE_ATTR_RW(fan1_target);
1019251f65cdSJulia Lawall static DEVICE_ATTR_RW(fan1_div);
1020251f65cdSJulia Lawall static DEVICE_ATTR_RW(fan1_pulses);
1021594fbe71SArnaud Ebalard 
1022594fbe71SArnaud Ebalard /* Driver data */
1023398e16dbSAxel Lin static struct attribute *g762_attrs[] = {
1024594fbe71SArnaud Ebalard 	&dev_attr_fan1_input.attr,
1025594fbe71SArnaud Ebalard 	&dev_attr_fan1_alarm.attr,
1026594fbe71SArnaud Ebalard 	&dev_attr_fan1_fault.attr,
1027594fbe71SArnaud Ebalard 	&dev_attr_fan1_target.attr,
1028594fbe71SArnaud Ebalard 	&dev_attr_fan1_div.attr,
1029594fbe71SArnaud Ebalard 	&dev_attr_fan1_pulses.attr,
1030594fbe71SArnaud Ebalard 	&dev_attr_pwm1.attr,
1031594fbe71SArnaud Ebalard 	&dev_attr_pwm1_mode.attr,
1032594fbe71SArnaud Ebalard 	&dev_attr_pwm1_enable.attr,
1033594fbe71SArnaud Ebalard 	NULL
1034594fbe71SArnaud Ebalard };
1035594fbe71SArnaud Ebalard 
1036398e16dbSAxel Lin ATTRIBUTE_GROUPS(g762);
1037594fbe71SArnaud Ebalard 
1038594fbe71SArnaud Ebalard /*
1039594fbe71SArnaud Ebalard  * Enable both fan failure detection and fan out of control protection. The
1040594fbe71SArnaud Ebalard  * function does not protect change/access to data structure; it must thus
1041594fbe71SArnaud Ebalard  * only be called during initialization.
1042594fbe71SArnaud Ebalard  */
1043594fbe71SArnaud Ebalard static inline int g762_fan_init(struct device *dev)
1044594fbe71SArnaud Ebalard {
1045594fbe71SArnaud Ebalard 	struct g762_data *data = g762_update_client(dev);
10466ce40232SChristian Marangi 	int ret;
1047594fbe71SArnaud Ebalard 
1048594fbe71SArnaud Ebalard 	if (IS_ERR(data))
1049594fbe71SArnaud Ebalard 		return PTR_ERR(data);
1050594fbe71SArnaud Ebalard 
10516ce40232SChristian Marangi 	/* internal_clock can only be set with compatible g761 */
10526ce40232SChristian Marangi 	if (data->internal_clock)
10536ce40232SChristian Marangi 		data->fan_cmd2 |= G761_REG_FAN_CMD2_FAN_CLOCK;
10546ce40232SChristian Marangi 
1055594fbe71SArnaud Ebalard 	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
1056594fbe71SArnaud Ebalard 	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
1057594fbe71SArnaud Ebalard 	data->valid = false;
1058594fbe71SArnaud Ebalard 
10596ce40232SChristian Marangi 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
1060594fbe71SArnaud Ebalard 					data->fan_cmd1);
10616ce40232SChristian Marangi 	if (ret)
10626ce40232SChristian Marangi 		return ret;
10636ce40232SChristian Marangi 
10646ce40232SChristian Marangi 	return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
10656ce40232SChristian Marangi 					 data->fan_cmd2);
1066594fbe71SArnaud Ebalard }
1067594fbe71SArnaud Ebalard 
106867487038SStephen Kitt static int g762_probe(struct i2c_client *client)
1069594fbe71SArnaud Ebalard {
1070398e16dbSAxel Lin 	struct device *dev = &client->dev;
107147591baaSHao Peng 	struct device *hwmon_dev;
1072594fbe71SArnaud Ebalard 	struct g762_data *data;
1073594fbe71SArnaud Ebalard 	int ret;
1074594fbe71SArnaud Ebalard 
1075594fbe71SArnaud Ebalard 	if (!i2c_check_functionality(client->adapter,
1076594fbe71SArnaud Ebalard 				     I2C_FUNC_SMBUS_BYTE_DATA))
1077594fbe71SArnaud Ebalard 		return -ENODEV;
1078594fbe71SArnaud Ebalard 
1079398e16dbSAxel Lin 	data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
1080594fbe71SArnaud Ebalard 	if (!data)
1081594fbe71SArnaud Ebalard 		return -ENOMEM;
1082594fbe71SArnaud Ebalard 
1083594fbe71SArnaud Ebalard 	i2c_set_clientdata(client, data);
1084594fbe71SArnaud Ebalard 	data->client = client;
1085594fbe71SArnaud Ebalard 	mutex_init(&data->update_lock);
1086594fbe71SArnaud Ebalard 
1087*f4a18024SGuenter Roeck 	/* Get configuration via DT ... */
1088*f4a18024SGuenter Roeck 	ret = g762_of_clock_enable(client);
1089*f4a18024SGuenter Roeck 	if (ret)
1090*f4a18024SGuenter Roeck 		return ret;
1091*f4a18024SGuenter Roeck 
1092594fbe71SArnaud Ebalard 	/* Enable fan failure detection and fan out of control protection */
1093398e16dbSAxel Lin 	ret = g762_fan_init(dev);
1094594fbe71SArnaud Ebalard 	if (ret)
1095594fbe71SArnaud Ebalard 		return ret;
1096594fbe71SArnaud Ebalard 
1097594fbe71SArnaud Ebalard 	ret = g762_of_prop_import(client);
1098594fbe71SArnaud Ebalard 	if (ret)
109947591baaSHao Peng 		return ret;
1100594fbe71SArnaud Ebalard 	/* ... or platform_data */
1101594fbe71SArnaud Ebalard 	ret = g762_pdata_prop_import(client);
1102594fbe71SArnaud Ebalard 	if (ret)
1103594fbe71SArnaud Ebalard 		return ret;
1104594fbe71SArnaud Ebalard 
110547591baaSHao Peng 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
110647591baaSHao Peng 							    data, g762_groups);
110747591baaSHao Peng 	return PTR_ERR_OR_ZERO(hwmon_dev);
1108594fbe71SArnaud Ebalard }
1109594fbe71SArnaud Ebalard 
1110594fbe71SArnaud Ebalard static struct i2c_driver g762_driver = {
1111594fbe71SArnaud Ebalard 	.driver = {
1112594fbe71SArnaud Ebalard 		.name = DRVNAME,
1113594fbe71SArnaud Ebalard 		.of_match_table = of_match_ptr(g762_dt_match),
1114594fbe71SArnaud Ebalard 	},
11151975d167SUwe Kleine-König 	.probe = g762_probe,
1116594fbe71SArnaud Ebalard 	.id_table = g762_id,
1117594fbe71SArnaud Ebalard };
1118594fbe71SArnaud Ebalard 
1119594fbe71SArnaud Ebalard module_i2c_driver(g762_driver);
1120594fbe71SArnaud Ebalard 
1121594fbe71SArnaud Ebalard MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1122594fbe71SArnaud Ebalard MODULE_DESCRIPTION("GMT G762/G763 driver");
1123594fbe71SArnaud Ebalard MODULE_LICENSE("GPL");
1124