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