xref: /openbmc/linux/drivers/hwmon/g762.c (revision 657c45b3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
4  *        PWM controller chips from G762 family, i.e. G762 and G763
5  *
6  * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
7  *
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:
14  *
15  *  http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
16  *
17  * Headers from previous developments have been kept below:
18  *
19  * Copyright (c) 2009 LaCie
20  *
21  * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
22  *
23  * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
24  * Copyright (C) 2007  Herbert Valerio Riedel <hvr@gnu.org>
25  *
26  * g762: minimal datasheet available at:
27  *       http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
28  */
29 
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>
41 #include <linux/of.h>
42 #include <linux/of_device.h>
43 #include <linux/platform_data/g762.h>
44 
45 #define DRVNAME "g762"
46 
47 static const struct i2c_device_id g762_id[] = {
48 	{ "g762", 0 },
49 	{ "g763", 0 },
50 	{ }
51 };
52 MODULE_DEVICE_TABLE(i2c, g762_id);
53 
54 enum g762_regs {
55 	G762_REG_SET_CNT  = 0x00,
56 	G762_REG_ACT_CNT  = 0x01,
57 	G762_REG_FAN_STA  = 0x02,
58 	G762_REG_SET_OUT  = 0x03,
59 	G762_REG_FAN_CMD1 = 0x04,
60 	G762_REG_FAN_CMD2 = 0x05,
61 };
62 
63 /* Config register bits */
64 #define G762_REG_FAN_CMD1_DET_FAN_FAIL  0x80 /* enable fan_fail signal */
65 #define G762_REG_FAN_CMD1_DET_FAN_OOC   0x40 /* enable fan_out_of_control */
66 #define G762_REG_FAN_CMD1_OUT_MODE      0x20 /* out mode: PWM or DC */
67 #define G762_REG_FAN_CMD1_FAN_MODE      0x10 /* fan mode: closed/open-loop */
68 #define G762_REG_FAN_CMD1_CLK_DIV_ID1   0x08 /* clock divisor value */
69 #define G762_REG_FAN_CMD1_CLK_DIV_ID0   0x04
70 #define G762_REG_FAN_CMD1_PWM_POLARITY  0x02 /* PWM polarity */
71 #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
72 
73 #define G762_REG_FAN_CMD2_GEAR_MODE_1   0x08 /* fan gear mode */
74 #define G762_REG_FAN_CMD2_GEAR_MODE_0   0x04
75 #define G762_REG_FAN_CMD2_FAN_STARTV_1  0x02 /* fan startup voltage */
76 #define G762_REG_FAN_CMD2_FAN_STARTV_0  0x01
77 
78 #define G762_REG_FAN_STA_FAIL           0x02 /* fan fail */
79 #define G762_REG_FAN_STA_OOC            0x01 /* fan out of control */
80 
81 /* Config register values */
82 #define G762_OUT_MODE_PWM            1
83 #define G762_OUT_MODE_DC             0
84 
85 #define G762_FAN_MODE_CLOSED_LOOP    2
86 #define G762_FAN_MODE_OPEN_LOOP      1
87 
88 #define G762_PWM_POLARITY_NEGATIVE   1
89 #define G762_PWM_POLARITY_POSITIVE   0
90 
91 /* Register data is read (and cached) at most once per second. */
92 #define G762_UPDATE_INTERVAL    HZ
93 
94 /*
95  * Extract pulse count per fan revolution value (2 or 4) from given
96  * FAN_CMD1 register value.
97  */
98 #define G762_PULSE_FROM_REG(reg) \
99 	((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
100 
101 /*
102  * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
103  * register value.
104  */
105 #define G762_CLKDIV_FROM_REG(reg) \
106 	(1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 |	\
107 			 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
108 
109 /*
110  * Extract fan gear mode multiplier value (0, 2 or 4) from given
111  * FAN_CMD2 register value.
112  */
113 #define G762_GEARMULT_FROM_REG(reg) \
114 	(1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 |	\
115 			 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
116 
117 struct g762_data {
118 	struct i2c_client *client;
119 	struct clk *clk;
120 
121 	/* update mutex */
122 	struct mutex update_lock;
123 
124 	/* board specific parameters. */
125 	u32 clk_freq;
126 
127 	/* g762 register cache */
128 	bool valid;
129 	unsigned long last_updated; /* in jiffies */
130 
131 	u8 set_cnt;  /* controls fan rotation speed in closed-loop mode */
132 	u8 act_cnt;  /* provides access to current fan RPM value */
133 	u8 fan_sta;  /* bit 0: set when actual fan speed is more than
134 		      *        25% outside requested fan speed
135 		      * bit 1: set when no transition occurs on fan
136 		      *        pin for 0.7s
137 		      */
138 	u8 set_out;  /* controls fan rotation speed in open-loop mode */
139 	u8 fan_cmd1; /*   0: FG_PLS_ID0 FG pulses count per revolution
140 		      *      0: 2 counts per revolution
141 		      *      1: 4 counts per revolution
142 		      *   1: PWM_POLARITY 1: negative_duty
143 		      *                   0: positive_duty
144 		      * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
145 		      *         00: Divide fan clock by 1
146 		      *         01: Divide fan clock by 2
147 		      *         10: Divide fan clock by 4
148 		      *         11: Divide fan clock by 8
149 		      *   4: FAN_MODE 1:closed-loop, 0:open-loop
150 		      *   5: OUT_MODE 1:PWM, 0:DC
151 		      *   6: DET_FAN_OOC enable "fan ooc" status
152 		      *   7: DET_FAN_FAIL enable "fan fail" status
153 		      */
154 	u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
155 		      * 2,3: FG_GEAR_MODE
156 		      *         00: multiplier = 1
157 		      *         01: multiplier = 2
158 		      *         10: multiplier = 4
159 		      *   4: Mask ALERT# (g763 only)
160 		      */
161 };
162 
163 /*
164  * Convert count value from fan controller register (FAN_SET_CNT) into fan
165  * speed RPM value. Note that the datasheet documents a basic formula;
166  * influence of additional parameters (fan clock divisor, fan gear mode)
167  * have been infered from examples in the datasheet and tests.
168  */
169 static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
170 					u8 clk_div, u8 gear_mult)
171 {
172 	if (cnt == 0xff)  /* setting cnt to 255 stops the fan */
173 		return 0;
174 
175 	return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
176 }
177 
178 /*
179  * Convert fan RPM value from sysfs into count value for fan controller
180  * register (FAN_SET_CNT).
181  */
182 static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
183 					 u8 clk_div, u8 gear_mult)
184 {
185 	unsigned long f1 = clk_freq * 30 * gear_mult;
186 	unsigned long f2 = p * clk_div;
187 
188 	if (!rpm)	/* to stop the fan, set cnt to 255 */
189 		return 0xff;
190 
191 	rpm = clamp_val(rpm, f1 / (255 * f2), ULONG_MAX / f2);
192 	return DIV_ROUND_CLOSEST(f1, rpm * f2);
193 }
194 
195 /* helper to grab and cache data, at most one time per second */
196 static struct g762_data *g762_update_client(struct device *dev)
197 {
198 	struct g762_data *data = dev_get_drvdata(dev);
199 	struct i2c_client *client = data->client;
200 	int ret = 0;
201 
202 	mutex_lock(&data->update_lock);
203 	if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
204 	    likely(data->valid))
205 		goto out;
206 
207 	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
208 	if (ret < 0)
209 		goto out;
210 	data->set_cnt = ret;
211 
212 	ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
213 	if (ret < 0)
214 		goto out;
215 	data->act_cnt = ret;
216 
217 	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
218 	if (ret < 0)
219 		goto out;
220 	data->fan_sta = ret;
221 
222 	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
223 	if (ret < 0)
224 		goto out;
225 	data->set_out = ret;
226 
227 	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
228 	if (ret < 0)
229 		goto out;
230 	data->fan_cmd1 = ret;
231 
232 	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
233 	if (ret < 0)
234 		goto out;
235 	data->fan_cmd2 = ret;
236 
237 	data->last_updated = jiffies;
238 	data->valid = true;
239  out:
240 	mutex_unlock(&data->update_lock);
241 
242 	if (ret < 0) /* upon error, encode it in return value */
243 		data = ERR_PTR(ret);
244 
245 	return data;
246 }
247 
248 /* helpers for writing hardware parameters */
249 
250 /*
251  * Set input clock frequency received on CLK pin of the chip. Accepted values
252  * are between 0 and 0xffffff. If zero is given, then default frequency
253  * (32,768Hz) is used. Note that clock frequency is a characteristic of the
254  * system but an internal parameter, i.e. value is not passed to the device.
255  */
256 static int do_set_clk_freq(struct device *dev, unsigned long val)
257 {
258 	struct g762_data *data = dev_get_drvdata(dev);
259 
260 	if (val > 0xffffff)
261 		return -EINVAL;
262 	if (!val)
263 		val = 32768;
264 
265 	data->clk_freq = val;
266 
267 	return 0;
268 }
269 
270 /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
271 static int do_set_pwm_mode(struct device *dev, unsigned long val)
272 {
273 	struct g762_data *data = g762_update_client(dev);
274 	int ret;
275 
276 	if (IS_ERR(data))
277 		return PTR_ERR(data);
278 
279 	mutex_lock(&data->update_lock);
280 	switch (val) {
281 	case G762_OUT_MODE_PWM:
282 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_OUT_MODE;
283 		break;
284 	case G762_OUT_MODE_DC:
285 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
286 		break;
287 	default:
288 		ret = -EINVAL;
289 		goto out;
290 	}
291 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
292 					data->fan_cmd1);
293 	data->valid = false;
294  out:
295 	mutex_unlock(&data->update_lock);
296 
297 	return ret;
298 }
299 
300 /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
301 static int do_set_fan_div(struct device *dev, unsigned long val)
302 {
303 	struct g762_data *data = g762_update_client(dev);
304 	int ret;
305 
306 	if (IS_ERR(data))
307 		return PTR_ERR(data);
308 
309 	mutex_lock(&data->update_lock);
310 	switch (val) {
311 	case 1:
312 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
313 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
314 		break;
315 	case 2:
316 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
317 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
318 		break;
319 	case 4:
320 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
321 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
322 		break;
323 	case 8:
324 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
325 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
326 		break;
327 	default:
328 		ret = -EINVAL;
329 		goto out;
330 	}
331 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
332 					data->fan_cmd1);
333 	data->valid = false;
334  out:
335 	mutex_unlock(&data->update_lock);
336 
337 	return ret;
338 }
339 
340 /* Set fan gear mode. Accepts either 0, 1 or 2. */
341 static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
342 {
343 	struct g762_data *data = g762_update_client(dev);
344 	int ret;
345 
346 	if (IS_ERR(data))
347 		return PTR_ERR(data);
348 
349 	mutex_lock(&data->update_lock);
350 	switch (val) {
351 	case 0:
352 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
353 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
354 		break;
355 	case 1:
356 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_0;
357 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
358 		break;
359 	case 2:
360 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
361 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_1;
362 		break;
363 	default:
364 		ret = -EINVAL;
365 		goto out;
366 	}
367 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
368 					data->fan_cmd2);
369 	data->valid = false;
370  out:
371 	mutex_unlock(&data->update_lock);
372 
373 	return ret;
374 }
375 
376 /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
377 static int do_set_fan_pulses(struct device *dev, unsigned long val)
378 {
379 	struct g762_data *data = g762_update_client(dev);
380 	int ret;
381 
382 	if (IS_ERR(data))
383 		return PTR_ERR(data);
384 
385 	mutex_lock(&data->update_lock);
386 	switch (val) {
387 	case 2:
388 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
389 		break;
390 	case 4:
391 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PULSE_PER_REV;
392 		break;
393 	default:
394 		ret = -EINVAL;
395 		goto out;
396 	}
397 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
398 					data->fan_cmd1);
399 	data->valid = false;
400  out:
401 	mutex_unlock(&data->update_lock);
402 
403 	return ret;
404 }
405 
406 /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
407 static int do_set_pwm_enable(struct device *dev, unsigned long val)
408 {
409 	struct g762_data *data = g762_update_client(dev);
410 	int ret;
411 
412 	if (IS_ERR(data))
413 		return PTR_ERR(data);
414 
415 	mutex_lock(&data->update_lock);
416 	switch (val) {
417 	case G762_FAN_MODE_CLOSED_LOOP:
418 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_FAN_MODE;
419 		break;
420 	case G762_FAN_MODE_OPEN_LOOP:
421 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
422 		/*
423 		 * BUG FIX: if SET_CNT register value is 255 then, for some
424 		 * unknown reason, fan will not rotate as expected, no matter
425 		 * the value of SET_OUT (to be specific, this seems to happen
426 		 * only in PWM mode). To workaround this bug, we give SET_CNT
427 		 * value of 254 if it is 255 when switching to open-loop.
428 		 */
429 		if (data->set_cnt == 0xff)
430 			i2c_smbus_write_byte_data(data->client,
431 						  G762_REG_SET_CNT, 254);
432 		break;
433 	default:
434 		ret = -EINVAL;
435 		goto out;
436 	}
437 
438 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
439 					data->fan_cmd1);
440 	data->valid = false;
441  out:
442 	mutex_unlock(&data->update_lock);
443 
444 	return ret;
445 }
446 
447 /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
448 static int do_set_pwm_polarity(struct device *dev, unsigned long val)
449 {
450 	struct g762_data *data = g762_update_client(dev);
451 	int ret;
452 
453 	if (IS_ERR(data))
454 		return PTR_ERR(data);
455 
456 	mutex_lock(&data->update_lock);
457 	switch (val) {
458 	case G762_PWM_POLARITY_POSITIVE:
459 		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
460 		break;
461 	case G762_PWM_POLARITY_NEGATIVE:
462 		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PWM_POLARITY;
463 		break;
464 	default:
465 		ret = -EINVAL;
466 		goto out;
467 	}
468 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
469 					data->fan_cmd1);
470 	data->valid = false;
471  out:
472 	mutex_unlock(&data->update_lock);
473 
474 	return ret;
475 }
476 
477 /*
478  * Set pwm value. Accepts values between 0 (stops the fan) and
479  * 255 (full speed). This only makes sense in open-loop mode.
480  */
481 static int do_set_pwm(struct device *dev, unsigned long val)
482 {
483 	struct g762_data *data = dev_get_drvdata(dev);
484 	struct i2c_client *client = data->client;
485 	int ret;
486 
487 	if (val > 255)
488 		return -EINVAL;
489 
490 	mutex_lock(&data->update_lock);
491 	ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
492 	data->valid = false;
493 	mutex_unlock(&data->update_lock);
494 
495 	return ret;
496 }
497 
498 /*
499  * Set fan RPM value. Can be called both in closed and open-loop mode
500  * but effect will only be seen after closed-loop mode is configured.
501  */
502 static int do_set_fan_target(struct device *dev, unsigned long val)
503 {
504 	struct g762_data *data = g762_update_client(dev);
505 	int ret;
506 
507 	if (IS_ERR(data))
508 		return PTR_ERR(data);
509 
510 	mutex_lock(&data->update_lock);
511 	data->set_cnt = cnt_from_rpm(val, data->clk_freq,
512 				     G762_PULSE_FROM_REG(data->fan_cmd1),
513 				     G762_CLKDIV_FROM_REG(data->fan_cmd1),
514 				     G762_GEARMULT_FROM_REG(data->fan_cmd2));
515 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
516 					data->set_cnt);
517 	data->valid = false;
518 	mutex_unlock(&data->update_lock);
519 
520 	return ret;
521 }
522 
523 /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
524 static int do_set_fan_startv(struct device *dev, unsigned long val)
525 {
526 	struct g762_data *data = g762_update_client(dev);
527 	int ret;
528 
529 	if (IS_ERR(data))
530 		return PTR_ERR(data);
531 
532 	mutex_lock(&data->update_lock);
533 	switch (val) {
534 	case 0:
535 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
536 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
537 		break;
538 	case 1:
539 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
540 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
541 		break;
542 	case 2:
543 		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
544 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
545 		break;
546 	case 3:
547 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
548 		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
549 		break;
550 	default:
551 		ret = -EINVAL;
552 		goto out;
553 	}
554 	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
555 					data->fan_cmd2);
556 	data->valid = false;
557  out:
558 	mutex_unlock(&data->update_lock);
559 
560 	return ret;
561 }
562 
563 /*
564  * Helper to import hardware characteristics from .dts file and push
565  * those to the chip.
566  */
567 
568 #ifdef CONFIG_OF
569 static const struct of_device_id g762_dt_match[] = {
570 	{ .compatible = "gmt,g762" },
571 	{ .compatible = "gmt,g763" },
572 	{ },
573 };
574 MODULE_DEVICE_TABLE(of, g762_dt_match);
575 
576 /*
577  * Grab clock (a required property), enable it, get (fixed) clock frequency
578  * and store it. Note: upon success, clock has been prepared and enabled; it
579  * must later be unprepared and disabled (e.g. during module unloading) by a
580  * call to g762_of_clock_disable(). Note that a reference to clock is kept
581  * in our private data structure to be used in this function.
582  */
583 static void g762_of_clock_disable(void *data)
584 {
585 	struct g762_data *g762 = data;
586 
587 	clk_disable_unprepare(g762->clk);
588 	clk_put(g762->clk);
589 }
590 
591 static int g762_of_clock_enable(struct i2c_client *client)
592 {
593 	struct g762_data *data;
594 	unsigned long clk_freq;
595 	struct clk *clk;
596 	int ret;
597 
598 	if (!client->dev.of_node)
599 		return 0;
600 
601 	clk = of_clk_get(client->dev.of_node, 0);
602 	if (IS_ERR(clk)) {
603 		dev_err(&client->dev, "failed to get clock\n");
604 		return PTR_ERR(clk);
605 	}
606 
607 	ret = clk_prepare_enable(clk);
608 	if (ret) {
609 		dev_err(&client->dev, "failed to enable clock\n");
610 		goto clk_put;
611 	}
612 
613 	clk_freq = clk_get_rate(clk);
614 	ret = do_set_clk_freq(&client->dev, clk_freq);
615 	if (ret) {
616 		dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
617 		goto clk_unprep;
618 	}
619 
620 	data = i2c_get_clientdata(client);
621 	data->clk = clk;
622 
623 	ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
624 	if (ret) {
625 		dev_err(&client->dev, "failed to add disable clock action\n");
626 		goto clk_unprep;
627 	}
628 
629 	return 0;
630 
631  clk_unprep:
632 	clk_disable_unprepare(clk);
633 
634  clk_put:
635 	clk_put(clk);
636 
637 	return ret;
638 }
639 
640 static int g762_of_prop_import_one(struct i2c_client *client,
641 				   const char *pname,
642 				   int (*psetter)(struct device *dev,
643 						  unsigned long val))
644 {
645 	int ret;
646 	u32 pval;
647 
648 	if (of_property_read_u32(client->dev.of_node, pname, &pval))
649 		return 0;
650 
651 	dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
652 	ret = (*psetter)(&client->dev, pval);
653 	if (ret)
654 		dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
655 
656 	return ret;
657 }
658 
659 static int g762_of_prop_import(struct i2c_client *client)
660 {
661 	int ret;
662 
663 	if (!client->dev.of_node)
664 		return 0;
665 
666 	ret = g762_of_prop_import_one(client, "fan_gear_mode",
667 				      do_set_fan_gear_mode);
668 	if (ret)
669 		return ret;
670 
671 	ret = g762_of_prop_import_one(client, "pwm_polarity",
672 				      do_set_pwm_polarity);
673 	if (ret)
674 		return ret;
675 
676 	return g762_of_prop_import_one(client, "fan_startv",
677 				       do_set_fan_startv);
678 }
679 
680 #else
681 static int g762_of_prop_import(struct i2c_client *client)
682 {
683 	return 0;
684 }
685 
686 static int g762_of_clock_enable(struct i2c_client *client)
687 {
688 	return 0;
689 }
690 #endif
691 
692 /*
693  * Helper to import hardware characteristics from .dts file and push
694  * those to the chip.
695  */
696 
697 static int g762_pdata_prop_import(struct i2c_client *client)
698 {
699 	struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
700 	int ret;
701 
702 	if (!pdata)
703 		return 0;
704 
705 	ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
706 	if (ret)
707 		return ret;
708 
709 	ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
710 	if (ret)
711 		return ret;
712 
713 	ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
714 	if (ret)
715 		return ret;
716 
717 	return do_set_clk_freq(&client->dev, pdata->clk_freq);
718 }
719 
720 /*
721  * sysfs attributes
722  */
723 
724 /*
725  * Read function for fan1_input sysfs file. Return current fan RPM value, or
726  * 0 if fan is out of control.
727  */
728 static ssize_t fan1_input_show(struct device *dev,
729 			       struct device_attribute *da, char *buf)
730 {
731 	struct g762_data *data = g762_update_client(dev);
732 	unsigned int rpm = 0;
733 
734 	if (IS_ERR(data))
735 		return PTR_ERR(data);
736 
737 	mutex_lock(&data->update_lock);
738 	/* reverse logic: fan out of control reporting is enabled low */
739 	if (data->fan_sta & G762_REG_FAN_STA_OOC) {
740 		rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
741 				   G762_PULSE_FROM_REG(data->fan_cmd1),
742 				   G762_CLKDIV_FROM_REG(data->fan_cmd1),
743 				   G762_GEARMULT_FROM_REG(data->fan_cmd2));
744 	}
745 	mutex_unlock(&data->update_lock);
746 
747 	return sprintf(buf, "%u\n", rpm);
748 }
749 
750 /*
751  * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
752  * control mode i.e. PWM (1) or DC (0).
753  */
754 static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da,
755 			      char *buf)
756 {
757 	struct g762_data *data = g762_update_client(dev);
758 
759 	if (IS_ERR(data))
760 		return PTR_ERR(data);
761 
762 	return sprintf(buf, "%d\n",
763 		       !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
764 }
765 
766 static ssize_t pwm1_mode_store(struct device *dev,
767 			       struct device_attribute *da, const char *buf,
768 			       size_t count)
769 {
770 	unsigned long val;
771 	int ret;
772 
773 	if (kstrtoul(buf, 10, &val))
774 		return -EINVAL;
775 
776 	ret = do_set_pwm_mode(dev, val);
777 	if (ret < 0)
778 		return ret;
779 
780 	return count;
781 }
782 
783 /*
784  * Read and write functions for fan1_div sysfs file. Get and set fan
785  * controller prescaler value
786  */
787 static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da,
788 			     char *buf)
789 {
790 	struct g762_data *data = g762_update_client(dev);
791 
792 	if (IS_ERR(data))
793 		return PTR_ERR(data);
794 
795 	return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
796 }
797 
798 static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
799 			      const char *buf, size_t count)
800 {
801 	unsigned long val;
802 	int ret;
803 
804 	if (kstrtoul(buf, 10, &val))
805 		return -EINVAL;
806 
807 	ret = do_set_fan_div(dev, val);
808 	if (ret < 0)
809 		return ret;
810 
811 	return count;
812 }
813 
814 /*
815  * Read and write functions for fan1_pulses sysfs file. Get and set number
816  * of tachometer pulses per fan revolution.
817  */
818 static ssize_t fan1_pulses_show(struct device *dev,
819 				struct device_attribute *da, char *buf)
820 {
821 	struct g762_data *data = g762_update_client(dev);
822 
823 	if (IS_ERR(data))
824 		return PTR_ERR(data);
825 
826 	return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
827 }
828 
829 static ssize_t fan1_pulses_store(struct device *dev,
830 				 struct device_attribute *da, const char *buf,
831 				 size_t count)
832 {
833 	unsigned long val;
834 	int ret;
835 
836 	if (kstrtoul(buf, 10, &val))
837 		return -EINVAL;
838 
839 	ret = do_set_fan_pulses(dev, val);
840 	if (ret < 0)
841 		return ret;
842 
843 	return count;
844 }
845 
846 /*
847  * Read and write functions for pwm1_enable. Get and set fan speed control mode
848  * (i.e. closed or open-loop).
849  *
850  * Following documentation about hwmon's sysfs interface, a pwm1_enable node
851  * should accept the following:
852  *
853  *  0 : no fan speed control (i.e. fan at full speed)
854  *  1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
855  *  2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
856  *
857  * but we do not accept 0 as this mode is not natively supported by the chip
858  * and it is not emulated by g762 driver. -EINVAL is returned in this case.
859  */
860 static ssize_t pwm1_enable_show(struct device *dev,
861 				struct device_attribute *da, char *buf)
862 {
863 	struct g762_data *data = g762_update_client(dev);
864 
865 	if (IS_ERR(data))
866 		return PTR_ERR(data);
867 
868 	return sprintf(buf, "%d\n",
869 		       (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
870 }
871 
872 static ssize_t pwm1_enable_store(struct device *dev,
873 				 struct device_attribute *da, const char *buf,
874 				 size_t count)
875 {
876 	unsigned long val;
877 	int ret;
878 
879 	if (kstrtoul(buf, 10, &val))
880 		return -EINVAL;
881 
882 	ret = do_set_pwm_enable(dev, val);
883 	if (ret < 0)
884 		return ret;
885 
886 	return count;
887 }
888 
889 /*
890  * Read and write functions for pwm1 sysfs file. Get and set pwm value
891  * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
892  * makes it run at full speed.
893  */
894 static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
895 			 char *buf)
896 {
897 	struct g762_data *data = g762_update_client(dev);
898 
899 	if (IS_ERR(data))
900 		return PTR_ERR(data);
901 
902 	return sprintf(buf, "%d\n", data->set_out);
903 }
904 
905 static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
906 			  const char *buf, size_t count)
907 {
908 	unsigned long val;
909 	int ret;
910 
911 	if (kstrtoul(buf, 10, &val))
912 		return -EINVAL;
913 
914 	ret = do_set_pwm(dev, val);
915 	if (ret < 0)
916 		return ret;
917 
918 	return count;
919 }
920 
921 /*
922  * Read and write function for fan1_target sysfs file. Get/set the fan speed in
923  * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
924  * the fan speed using pulses from fan tachometer.
925  *
926  * Refer to rpm_from_cnt() implementation above to get info about count number
927  * calculation.
928  *
929  * Also note that due to rounding errors it is possible that you don't read
930  * back exactly the value you have set.
931  */
932 static ssize_t fan1_target_show(struct device *dev,
933 				struct device_attribute *da, char *buf)
934 {
935 	struct g762_data *data = g762_update_client(dev);
936 	unsigned int rpm;
937 
938 	if (IS_ERR(data))
939 		return PTR_ERR(data);
940 
941 	mutex_lock(&data->update_lock);
942 	rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
943 			   G762_PULSE_FROM_REG(data->fan_cmd1),
944 			   G762_CLKDIV_FROM_REG(data->fan_cmd1),
945 			   G762_GEARMULT_FROM_REG(data->fan_cmd2));
946 	mutex_unlock(&data->update_lock);
947 
948 	return sprintf(buf, "%u\n", rpm);
949 }
950 
951 static ssize_t fan1_target_store(struct device *dev,
952 				 struct device_attribute *da, const char *buf,
953 				 size_t count)
954 {
955 	unsigned long val;
956 	int ret;
957 
958 	if (kstrtoul(buf, 10, &val))
959 		return -EINVAL;
960 
961 	ret = do_set_fan_target(dev, val);
962 	if (ret < 0)
963 		return ret;
964 
965 	return count;
966 }
967 
968 /* read function for fan1_fault sysfs file. */
969 static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da,
970 			       char *buf)
971 {
972 	struct g762_data *data = g762_update_client(dev);
973 
974 	if (IS_ERR(data))
975 		return PTR_ERR(data);
976 
977 	return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
978 }
979 
980 /*
981  * read function for fan1_alarm sysfs file. Note that OOC condition is
982  * enabled low
983  */
984 static ssize_t fan1_alarm_show(struct device *dev,
985 			       struct device_attribute *da, char *buf)
986 {
987 	struct g762_data *data = g762_update_client(dev);
988 
989 	if (IS_ERR(data))
990 		return PTR_ERR(data);
991 
992 	return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
993 }
994 
995 static DEVICE_ATTR_RW(pwm1);
996 static DEVICE_ATTR_RW(pwm1_mode);
997 static DEVICE_ATTR_RW(pwm1_enable);
998 static DEVICE_ATTR_RO(fan1_input);
999 static DEVICE_ATTR_RO(fan1_alarm);
1000 static DEVICE_ATTR_RO(fan1_fault);
1001 static DEVICE_ATTR_RW(fan1_target);
1002 static DEVICE_ATTR_RW(fan1_div);
1003 static DEVICE_ATTR_RW(fan1_pulses);
1004 
1005 /* Driver data */
1006 static struct attribute *g762_attrs[] = {
1007 	&dev_attr_fan1_input.attr,
1008 	&dev_attr_fan1_alarm.attr,
1009 	&dev_attr_fan1_fault.attr,
1010 	&dev_attr_fan1_target.attr,
1011 	&dev_attr_fan1_div.attr,
1012 	&dev_attr_fan1_pulses.attr,
1013 	&dev_attr_pwm1.attr,
1014 	&dev_attr_pwm1_mode.attr,
1015 	&dev_attr_pwm1_enable.attr,
1016 	NULL
1017 };
1018 
1019 ATTRIBUTE_GROUPS(g762);
1020 
1021 /*
1022  * Enable both fan failure detection and fan out of control protection. The
1023  * function does not protect change/access to data structure; it must thus
1024  * only be called during initialization.
1025  */
1026 static inline int g762_fan_init(struct device *dev)
1027 {
1028 	struct g762_data *data = g762_update_client(dev);
1029 
1030 	if (IS_ERR(data))
1031 		return PTR_ERR(data);
1032 
1033 	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
1034 	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
1035 	data->valid = false;
1036 
1037 	return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
1038 					 data->fan_cmd1);
1039 }
1040 
1041 static int g762_probe(struct i2c_client *client)
1042 {
1043 	struct device *dev = &client->dev;
1044 	struct device *hwmon_dev;
1045 	struct g762_data *data;
1046 	int ret;
1047 
1048 	if (!i2c_check_functionality(client->adapter,
1049 				     I2C_FUNC_SMBUS_BYTE_DATA))
1050 		return -ENODEV;
1051 
1052 	data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
1053 	if (!data)
1054 		return -ENOMEM;
1055 
1056 	i2c_set_clientdata(client, data);
1057 	data->client = client;
1058 	mutex_init(&data->update_lock);
1059 
1060 	/* Enable fan failure detection and fan out of control protection */
1061 	ret = g762_fan_init(dev);
1062 	if (ret)
1063 		return ret;
1064 
1065 	/* Get configuration via DT ... */
1066 	ret = g762_of_clock_enable(client);
1067 	if (ret)
1068 		return ret;
1069 	ret = g762_of_prop_import(client);
1070 	if (ret)
1071 		return ret;
1072 	/* ... or platform_data */
1073 	ret = g762_pdata_prop_import(client);
1074 	if (ret)
1075 		return ret;
1076 
1077 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1078 							    data, g762_groups);
1079 	return PTR_ERR_OR_ZERO(hwmon_dev);
1080 }
1081 
1082 static struct i2c_driver g762_driver = {
1083 	.driver = {
1084 		.name = DRVNAME,
1085 		.of_match_table = of_match_ptr(g762_dt_match),
1086 	},
1087 	.probe = g762_probe,
1088 	.id_table = g762_id,
1089 };
1090 
1091 module_i2c_driver(g762_driver);
1092 
1093 MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1094 MODULE_DESCRIPTION("GMT G762/G763 driver");
1095 MODULE_LICENSE("GPL");
1096