blob: 9b55e673b67caf1365c7452ce51a22a37510af02 [file] [log] [blame]
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001/*
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 {
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200131 struct device *hwmon_dev;
Axel Lin398e16d2014-07-19 22:29:44 +0800132 struct i2c_client *client;
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200133 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(u32 rpm, u32 clk_freq, u16 p,
197 u8 clk_div, u8 gear_mult)
198{
199 if (!rpm) /* to stop the fan, set cnt to 255 */
200 return 0xff;
201
202 return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)),
203 0, 255);
204}
205
206/* helper to grab and cache data, at most one time per second */
207static struct g762_data *g762_update_client(struct device *dev)
208{
Axel Lin398e16d2014-07-19 22:29:44 +0800209 struct g762_data *data = dev_get_drvdata(dev);
210 struct i2c_client *client = data->client;
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200211 int ret = 0;
212
213 mutex_lock(&data->update_lock);
214 if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
215 likely(data->valid))
216 goto out;
217
218 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
219 if (ret < 0)
220 goto out;
221 data->set_cnt = ret;
222
223 ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
224 if (ret < 0)
225 goto out;
226 data->act_cnt = ret;
227
228 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
229 if (ret < 0)
230 goto out;
231 data->fan_sta = ret;
232
233 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
234 if (ret < 0)
235 goto out;
236 data->set_out = ret;
237
238 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
239 if (ret < 0)
240 goto out;
241 data->fan_cmd1 = ret;
242
243 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
244 if (ret < 0)
245 goto out;
246 data->fan_cmd2 = ret;
247
248 data->last_updated = jiffies;
249 data->valid = true;
250 out:
251 mutex_unlock(&data->update_lock);
252
253 if (ret < 0) /* upon error, encode it in return value */
254 data = ERR_PTR(ret);
255
256 return data;
257}
258
259/* helpers for writing hardware parameters */
260
261/*
262 * Set input clock frequency received on CLK pin of the chip. Accepted values
263 * are between 0 and 0xffffff. If zero is given, then default frequency
264 * (32,768Hz) is used. Note that clock frequency is a characteristic of the
265 * system but an internal parameter, i.e. value is not passed to the device.
266 */
267static int do_set_clk_freq(struct device *dev, unsigned long val)
268{
Axel Lin398e16d2014-07-19 22:29:44 +0800269 struct g762_data *data = dev_get_drvdata(dev);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200270
271 if (val > 0xffffff)
272 return -EINVAL;
273 if (!val)
274 val = 32768;
275
276 data->clk_freq = val;
277
278 return 0;
279}
280
281/* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
282static int do_set_pwm_mode(struct device *dev, unsigned long val)
283{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200284 struct g762_data *data = g762_update_client(dev);
285 int ret;
286
287 if (IS_ERR(data))
288 return PTR_ERR(data);
289
290 mutex_lock(&data->update_lock);
291 switch (val) {
292 case G762_OUT_MODE_PWM:
293 data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE;
294 break;
295 case G762_OUT_MODE_DC:
296 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
297 break;
298 default:
299 ret = -EINVAL;
300 goto out;
301 }
Axel Lin398e16d2014-07-19 22:29:44 +0800302 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200303 data->fan_cmd1);
304 data->valid = false;
305 out:
306 mutex_unlock(&data->update_lock);
307
308 return ret;
309}
310
311/* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
312static int do_set_fan_div(struct device *dev, unsigned long val)
313{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200314 struct g762_data *data = g762_update_client(dev);
315 int ret;
316
317 if (IS_ERR(data))
318 return PTR_ERR(data);
319
320 mutex_lock(&data->update_lock);
321 switch (val) {
322 case 1:
323 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
324 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
325 break;
326 case 2:
327 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
328 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
329 break;
330 case 4:
331 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
332 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
333 break;
334 case 8:
335 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
336 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
337 break;
338 default:
339 ret = -EINVAL;
340 goto out;
341 }
Axel Lin398e16d2014-07-19 22:29:44 +0800342 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200343 data->fan_cmd1);
344 data->valid = false;
345 out:
346 mutex_unlock(&data->update_lock);
347
348 return ret;
349}
350
351/* Set fan gear mode. Accepts either 0, 1 or 2. */
352static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
353{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200354 struct g762_data *data = g762_update_client(dev);
355 int ret;
356
357 if (IS_ERR(data))
358 return PTR_ERR(data);
359
360 mutex_lock(&data->update_lock);
361 switch (val) {
362 case 0:
363 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
364 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
365 break;
366 case 1:
367 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0;
368 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
369 break;
370 case 2:
371 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
372 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1;
373 break;
374 default:
375 ret = -EINVAL;
376 goto out;
377 }
Axel Lin398e16d2014-07-19 22:29:44 +0800378 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200379 data->fan_cmd2);
380 data->valid = false;
381 out:
382 mutex_unlock(&data->update_lock);
383
384 return ret;
385}
386
387/* Set number of fan pulses per revolution. Accepts either 2 or 4. */
388static int do_set_fan_pulses(struct device *dev, unsigned long val)
389{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200390 struct g762_data *data = g762_update_client(dev);
391 int ret;
392
393 if (IS_ERR(data))
394 return PTR_ERR(data);
395
396 mutex_lock(&data->update_lock);
397 switch (val) {
398 case 2:
399 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
400 break;
401 case 4:
402 data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV;
403 break;
404 default:
405 ret = -EINVAL;
406 goto out;
407 }
Axel Lin398e16d2014-07-19 22:29:44 +0800408 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200409 data->fan_cmd1);
410 data->valid = false;
411 out:
412 mutex_unlock(&data->update_lock);
413
414 return ret;
415}
416
417/* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
418static int do_set_pwm_enable(struct device *dev, unsigned long val)
419{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200420 struct g762_data *data = g762_update_client(dev);
421 int ret;
422
423 if (IS_ERR(data))
424 return PTR_ERR(data);
425
426 mutex_lock(&data->update_lock);
427 switch (val) {
428 case G762_FAN_MODE_CLOSED_LOOP:
429 data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE;
430 break;
431 case G762_FAN_MODE_OPEN_LOOP:
432 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
433 /*
434 * BUG FIX: if SET_CNT register value is 255 then, for some
435 * unknown reason, fan will not rotate as expected, no matter
436 * the value of SET_OUT (to be specific, this seems to happen
437 * only in PWM mode). To workaround this bug, we give SET_CNT
438 * value of 254 if it is 255 when switching to open-loop.
439 */
440 if (data->set_cnt == 0xff)
Axel Lin398e16d2014-07-19 22:29:44 +0800441 i2c_smbus_write_byte_data(data->client,
442 G762_REG_SET_CNT, 254);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200443 break;
444 default:
445 ret = -EINVAL;
446 goto out;
447 }
448
Axel Lin398e16d2014-07-19 22:29:44 +0800449 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200450 data->fan_cmd1);
451 data->valid = false;
452 out:
453 mutex_unlock(&data->update_lock);
454
455 return ret;
456}
457
458/* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
459static int do_set_pwm_polarity(struct device *dev, unsigned long val)
460{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200461 struct g762_data *data = g762_update_client(dev);
462 int ret;
463
464 if (IS_ERR(data))
465 return PTR_ERR(data);
466
467 mutex_lock(&data->update_lock);
468 switch (val) {
469 case G762_PWM_POLARITY_POSITIVE:
470 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
471 break;
472 case G762_PWM_POLARITY_NEGATIVE:
473 data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY;
474 break;
475 default:
476 ret = -EINVAL;
477 goto out;
478 }
Axel Lin398e16d2014-07-19 22:29:44 +0800479 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200480 data->fan_cmd1);
481 data->valid = false;
482 out:
483 mutex_unlock(&data->update_lock);
484
485 return ret;
486}
487
488/*
489 * Set pwm value. Accepts values between 0 (stops the fan) and
490 * 255 (full speed). This only makes sense in open-loop mode.
491 */
492static int do_set_pwm(struct device *dev, unsigned long val)
493{
Axel Lin398e16d2014-07-19 22:29:44 +0800494 struct g762_data *data = dev_get_drvdata(dev);
495 struct i2c_client *client = data->client;
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200496 int ret;
497
498 if (val > 255)
499 return -EINVAL;
500
501 mutex_lock(&data->update_lock);
502 ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
503 data->valid = false;
504 mutex_unlock(&data->update_lock);
505
506 return ret;
507}
508
509/*
510 * Set fan RPM value. Can be called both in closed and open-loop mode
511 * but effect will only be seen after closed-loop mode is configured.
512 */
513static int do_set_fan_target(struct device *dev, unsigned long val)
514{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200515 struct g762_data *data = g762_update_client(dev);
516 int ret;
517
518 if (IS_ERR(data))
519 return PTR_ERR(data);
520
521 mutex_lock(&data->update_lock);
522 data->set_cnt = cnt_from_rpm(val, data->clk_freq,
523 G762_PULSE_FROM_REG(data->fan_cmd1),
524 G762_CLKDIV_FROM_REG(data->fan_cmd1),
525 G762_GEARMULT_FROM_REG(data->fan_cmd2));
Axel Lin398e16d2014-07-19 22:29:44 +0800526 ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200527 data->set_cnt);
528 data->valid = false;
529 mutex_unlock(&data->update_lock);
530
531 return ret;
532}
533
534/* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
535static int do_set_fan_startv(struct device *dev, unsigned long val)
536{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200537 struct g762_data *data = g762_update_client(dev);
538 int ret;
539
540 if (IS_ERR(data))
541 return PTR_ERR(data);
542
543 mutex_lock(&data->update_lock);
544 switch (val) {
545 case 0:
546 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
547 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
548 break;
549 case 1:
550 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
551 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
552 break;
553 case 2:
554 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
555 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
556 break;
557 case 3:
558 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
559 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
560 break;
561 default:
562 ret = -EINVAL;
563 goto out;
564 }
Axel Lin398e16d2014-07-19 22:29:44 +0800565 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200566 data->fan_cmd2);
567 data->valid = false;
568 out:
569 mutex_unlock(&data->update_lock);
570
571 return ret;
572}
573
574/*
575 * Helper to import hardware characteristics from .dts file and push
576 * those to the chip.
577 */
578
579#ifdef CONFIG_OF
Jingoo Hanadaa50b2014-05-07 17:25:14 +0900580static const struct of_device_id g762_dt_match[] = {
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200581 { .compatible = "gmt,g762" },
582 { .compatible = "gmt,g763" },
583 { },
584};
585
586/*
587 * Grab clock (a required property), enable it, get (fixed) clock frequency
588 * and store it. Note: upon success, clock has been prepared and enabled; it
589 * must later be unprepared and disabled (e.g. during module unloading) by a
590 * call to g762_of_clock_disable(). Note that a reference to clock is kept
591 * in our private data structure to be used in this function.
592 */
593static int g762_of_clock_enable(struct i2c_client *client)
594{
595 struct g762_data *data;
596 unsigned long clk_freq;
597 struct clk *clk;
598 int ret;
599
600 if (!client->dev.of_node)
601 return 0;
602
603 clk = of_clk_get(client->dev.of_node, 0);
604 if (IS_ERR(clk)) {
605 dev_err(&client->dev, "failed to get clock\n");
606 return PTR_ERR(clk);
607 }
608
609 ret = clk_prepare_enable(clk);
610 if (ret) {
611 dev_err(&client->dev, "failed to enable clock\n");
612 goto clk_put;
613 }
614
615 clk_freq = clk_get_rate(clk);
616 ret = do_set_clk_freq(&client->dev, clk_freq);
617 if (ret) {
618 dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
619 goto clk_unprep;
620 }
621
622 data = i2c_get_clientdata(client);
623 data->clk = clk;
624
625 return 0;
626
627 clk_unprep:
628 clk_disable_unprepare(clk);
629
630 clk_put:
631 clk_put(clk);
632
633 return ret;
634}
635
636static void g762_of_clock_disable(struct i2c_client *client)
637{
638 struct g762_data *data = i2c_get_clientdata(client);
639
640 if (!data->clk)
641 return;
642
643 clk_disable_unprepare(data->clk);
644 clk_put(data->clk);
645}
646
647static int g762_of_prop_import_one(struct i2c_client *client,
648 const char *pname,
649 int (*psetter)(struct device *dev,
650 unsigned long val))
651{
Axel Linfce96262014-08-01 10:07:25 +0800652 int ret;
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200653 u32 pval;
654
Axel Linfce96262014-08-01 10:07:25 +0800655 if (of_property_read_u32(client->dev.of_node, pname, &pval))
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200656 return 0;
657
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200658 dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
659 ret = (*psetter)(&client->dev, pval);
660 if (ret)
661 dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
662
663 return ret;
664}
665
666static int g762_of_prop_import(struct i2c_client *client)
667{
668 int ret;
669
670 if (!client->dev.of_node)
671 return 0;
672
673 ret = g762_of_prop_import_one(client, "fan_gear_mode",
674 do_set_fan_gear_mode);
675 if (ret)
676 return ret;
677
678 ret = g762_of_prop_import_one(client, "pwm_polarity",
679 do_set_pwm_polarity);
680 if (ret)
681 return ret;
682
683 return g762_of_prop_import_one(client, "fan_startv",
684 do_set_fan_startv);
685}
686
687#else
688static int g762_of_prop_import(struct i2c_client *client)
689{
690 return 0;
691}
692
693static int g762_of_clock_enable(struct i2c_client *client)
694{
695 return 0;
696}
697
698static void g762_of_clock_disable(struct i2c_client *client) { }
699#endif
700
701/*
702 * Helper to import hardware characteristics from .dts file and push
703 * those to the chip.
704 */
705
706static int g762_pdata_prop_import(struct i2c_client *client)
707{
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900708 struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +0200709 int ret;
710
711 if (!pdata)
712 return 0;
713
714 ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
715 if (ret)
716 return ret;
717
718 ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
719 if (ret)
720 return ret;
721
722 ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
723 if (ret)
724 return ret;
725
726 return do_set_clk_freq(&client->dev, pdata->clk_freq);
727}
728
729/*
730 * sysfs attributes
731 */
732
733/*
734 * Read function for fan1_input sysfs file. Return current fan RPM value, or
735 * 0 if fan is out of control.
736 */
737static ssize_t get_fan_rpm(struct device *dev, struct device_attribute *da,
738 char *buf)
739{
740 struct g762_data *data = g762_update_client(dev);
741 unsigned int rpm = 0;
742
743 if (IS_ERR(data))
744 return PTR_ERR(data);
745
746 mutex_lock(&data->update_lock);
747 /* reverse logic: fan out of control reporting is enabled low */
748 if (data->fan_sta & G762_REG_FAN_STA_OOC) {
749 rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
750 G762_PULSE_FROM_REG(data->fan_cmd1),
751 G762_CLKDIV_FROM_REG(data->fan_cmd1),
752 G762_GEARMULT_FROM_REG(data->fan_cmd2));
753 }
754 mutex_unlock(&data->update_lock);
755
756 return sprintf(buf, "%u\n", rpm);
757}
758
759/*
760 * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
761 * control mode i.e. PWM (1) or DC (0).
762 */
763static ssize_t get_pwm_mode(struct device *dev, struct device_attribute *da,
764 char *buf)
765{
766 struct g762_data *data = g762_update_client(dev);
767
768 if (IS_ERR(data))
769 return PTR_ERR(data);
770
771 return sprintf(buf, "%d\n",
772 !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
773}
774
775static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *da,
776 const char *buf, size_t count)
777{
778 unsigned long val;
779 int ret;
780
781 if (kstrtoul(buf, 10, &val))
782 return -EINVAL;
783
784 ret = do_set_pwm_mode(dev, val);
785 if (ret < 0)
786 return ret;
787
788 return count;
789}
790
791/*
792 * Read and write functions for fan1_div sysfs file. Get and set fan
793 * controller prescaler value
794 */
795static ssize_t get_fan_div(struct device *dev,
796 struct device_attribute *da, char *buf)
797{
798 struct g762_data *data = g762_update_client(dev);
799
800 if (IS_ERR(data))
801 return PTR_ERR(data);
802
803 return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
804}
805
806static ssize_t set_fan_div(struct device *dev,
807 struct device_attribute *da,
808 const char *buf, size_t count)
809{
810 unsigned long val;
811 int ret;
812
813 if (kstrtoul(buf, 10, &val))
814 return -EINVAL;
815
816 ret = do_set_fan_div(dev, val);
817 if (ret < 0)
818 return ret;
819
820 return count;
821}
822
823/*
824 * Read and write functions for fan1_pulses sysfs file. Get and set number
825 * of tachometer pulses per fan revolution.
826 */
827static ssize_t get_fan_pulses(struct device *dev,
828 struct device_attribute *da, char *buf)
829{
830 struct g762_data *data = g762_update_client(dev);
831
832 if (IS_ERR(data))
833 return PTR_ERR(data);
834
835 return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
836}
837
838static ssize_t set_fan_pulses(struct device *dev,
839 struct device_attribute *da,
840 const char *buf, size_t count)
841{
842 unsigned long val;
843 int ret;
844
845 if (kstrtoul(buf, 10, &val))
846 return -EINVAL;
847
848 ret = do_set_fan_pulses(dev, val);
849 if (ret < 0)
850 return ret;
851
852 return count;
853}
854
855/*
856 * Read and write functions for pwm1_enable. Get and set fan speed control mode
857 * (i.e. closed or open-loop).
858 *
859 * Following documentation about hwmon's sysfs interface, a pwm1_enable node
860 * should accept followings:
861 *
862 * 0 : no fan speed control (i.e. fan at full speed)
863 * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
864 * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
865 *
866 * but we do not accept 0 as this mode is not natively supported by the chip
867 * and it is not emulated by g762 driver. -EINVAL is returned in this case.
868 */
869static ssize_t get_pwm_enable(struct device *dev,
870 struct device_attribute *da, char *buf)
871{
872 struct g762_data *data = g762_update_client(dev);
873
874 if (IS_ERR(data))
875 return PTR_ERR(data);
876
877 return sprintf(buf, "%d\n",
878 (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
879}
880
881static ssize_t set_pwm_enable(struct device *dev,
882 struct device_attribute *da,
883 const char *buf, size_t count)
884{
885 unsigned long val;
886 int ret;
887
888 if (kstrtoul(buf, 10, &val))
889 return -EINVAL;
890
891 ret = do_set_pwm_enable(dev, val);
892 if (ret < 0)
893 return ret;
894
895 return count;
896}
897
898/*
899 * Read and write functions for pwm1 sysfs file. Get and set pwm value
900 * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
901 * makes it run at full speed.
902 */
903static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
904 char *buf)
905{
906 struct g762_data *data = g762_update_client(dev);
907
908 if (IS_ERR(data))
909 return PTR_ERR(data);
910
911 return sprintf(buf, "%d\n", data->set_out);
912}
913
914static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
915 const char *buf, size_t count)
916{
917 unsigned long val;
918 int ret;
919
920 if (kstrtoul(buf, 10, &val))
921 return -EINVAL;
922
923 ret = do_set_pwm(dev, val);
924 if (ret < 0)
925 return ret;
926
927 return count;
928}
929
930/*
931 * Read and write function for fan1_target sysfs file. Get/set the fan speed in
932 * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
933 * the fan speed using pulses from fan tachometer.
934 *
935 * Refer to rpm_from_cnt() implementation above to get info about count number
936 * calculation.
937 *
938 * Also note that due to rounding errors it is possible that you don't read
939 * back exactly the value you have set.
940 */
941static ssize_t get_fan_target(struct device *dev, struct device_attribute *da,
942 char *buf)
943{
944 struct g762_data *data = g762_update_client(dev);
945 unsigned int rpm;
946
947 if (IS_ERR(data))
948 return PTR_ERR(data);
949
950 mutex_lock(&data->update_lock);
951 rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
952 G762_PULSE_FROM_REG(data->fan_cmd1),
953 G762_CLKDIV_FROM_REG(data->fan_cmd1),
954 G762_GEARMULT_FROM_REG(data->fan_cmd2));
955 mutex_unlock(&data->update_lock);
956
957 return sprintf(buf, "%u\n", rpm);
958}
959
960static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
961 const char *buf, size_t count)
962{
963 unsigned long val;
964 int ret;
965
966 if (kstrtoul(buf, 10, &val))
967 return -EINVAL;
968
969 ret = do_set_fan_target(dev, val);
970 if (ret < 0)
971 return ret;
972
973 return count;
974}
975
976/* read function for fan1_fault sysfs file. */
977static ssize_t get_fan_failure(struct device *dev, struct device_attribute *da,
978 char *buf)
979{
980 struct g762_data *data = g762_update_client(dev);
981
982 if (IS_ERR(data))
983 return PTR_ERR(data);
984
985 return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
986}
987
988/*
989 * read function for fan1_alarm sysfs file. Note that OOC condition is
990 * enabled low
991 */
992static ssize_t get_fan_ooc(struct device *dev, struct device_attribute *da,
993 char *buf)
994{
995 struct g762_data *data = g762_update_client(dev);
996
997 if (IS_ERR(data))
998 return PTR_ERR(data);
999
1000 return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
1001}
1002
1003static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
1004static DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, get_pwm_mode, set_pwm_mode);
1005static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
1006 get_pwm_enable, set_pwm_enable);
1007static DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_rpm, NULL);
1008static DEVICE_ATTR(fan1_alarm, S_IRUGO, get_fan_ooc, NULL);
1009static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL);
1010static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
1011 get_fan_target, set_fan_target);
1012static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div);
1013static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO,
1014 get_fan_pulses, set_fan_pulses);
1015
1016/* Driver data */
Axel Lin398e16d2014-07-19 22:29:44 +08001017static struct attribute *g762_attrs[] = {
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001018 &dev_attr_fan1_input.attr,
1019 &dev_attr_fan1_alarm.attr,
1020 &dev_attr_fan1_fault.attr,
1021 &dev_attr_fan1_target.attr,
1022 &dev_attr_fan1_div.attr,
1023 &dev_attr_fan1_pulses.attr,
1024 &dev_attr_pwm1.attr,
1025 &dev_attr_pwm1_mode.attr,
1026 &dev_attr_pwm1_enable.attr,
1027 NULL
1028};
1029
Axel Lin398e16d2014-07-19 22:29:44 +08001030ATTRIBUTE_GROUPS(g762);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001031
1032/*
1033 * Enable both fan failure detection and fan out of control protection. The
1034 * function does not protect change/access to data structure; it must thus
1035 * only be called during initialization.
1036 */
1037static inline int g762_fan_init(struct device *dev)
1038{
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001039 struct g762_data *data = g762_update_client(dev);
1040
1041 if (IS_ERR(data))
1042 return PTR_ERR(data);
1043
1044 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
1045 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
1046 data->valid = false;
1047
Axel Lin398e16d2014-07-19 22:29:44 +08001048 return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001049 data->fan_cmd1);
1050}
1051
1052static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
1053{
Axel Lin398e16d2014-07-19 22:29:44 +08001054 struct device *dev = &client->dev;
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001055 struct g762_data *data;
1056 int ret;
1057
1058 if (!i2c_check_functionality(client->adapter,
1059 I2C_FUNC_SMBUS_BYTE_DATA))
1060 return -ENODEV;
1061
Axel Lin398e16d2014-07-19 22:29:44 +08001062 data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001063 if (!data)
1064 return -ENOMEM;
1065
1066 i2c_set_clientdata(client, data);
1067 data->client = client;
1068 mutex_init(&data->update_lock);
1069
1070 /* Enable fan failure detection and fan out of control protection */
Axel Lin398e16d2014-07-19 22:29:44 +08001071 ret = g762_fan_init(dev);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001072 if (ret)
1073 return ret;
1074
1075 /* Get configuration via DT ... */
1076 ret = g762_of_clock_enable(client);
1077 if (ret)
1078 return ret;
1079 ret = g762_of_prop_import(client);
1080 if (ret)
1081 goto clock_dis;
1082 /* ... or platform_data */
1083 ret = g762_pdata_prop_import(client);
1084 if (ret)
1085 goto clock_dis;
1086
Arnaud Ebalard6b19b662014-11-19 22:52:36 +01001087 data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
1088 data, g762_groups);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001089 if (IS_ERR(data->hwmon_dev)) {
1090 ret = PTR_ERR(data->hwmon_dev);
Axel Lin398e16d2014-07-19 22:29:44 +08001091 goto clock_dis;
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001092 }
1093
1094 return 0;
1095
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001096 clock_dis:
1097 g762_of_clock_disable(client);
1098
1099 return ret;
1100}
1101
1102static int g762_remove(struct i2c_client *client)
1103{
1104 struct g762_data *data = i2c_get_clientdata(client);
1105
1106 hwmon_device_unregister(data->hwmon_dev);
Arnaud Ebalard594fbe72013-06-20 22:21:04 +02001107 g762_of_clock_disable(client);
1108
1109 return 0;
1110}
1111
1112static struct i2c_driver g762_driver = {
1113 .driver = {
1114 .name = DRVNAME,
1115 .owner = THIS_MODULE,
1116 .of_match_table = of_match_ptr(g762_dt_match),
1117 },
1118 .probe = g762_probe,
1119 .remove = g762_remove,
1120 .id_table = g762_id,
1121};
1122
1123module_i2c_driver(g762_driver);
1124
1125MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1126MODULE_DESCRIPTION("GMT G762/G763 driver");
1127MODULE_LICENSE("GPL");