]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/f75375s.c
hwmon: (f75375s) Make pwm*_mode writable for the F75387
[mirror_ubuntu-bionic-kernel.git] / drivers / hwmon / f75375s.c
CommitLineData
84f1e442 1/*
f58c44e6
BG
2 * f75375s.c - driver for the Fintek F75375/SP, F75373 and
3 * F75387SG/RG hardware monitoring features
b26e0ed4 4 * Copyright (C) 2006-2007 Riku Voipio
84f1e442
RV
5 *
6 * Datasheets available at:
7 *
8 * f75375:
4fd826ef 9 * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
84f1e442
RV
10 *
11 * f75373:
631dd1a8 12 * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
84f1e442 13 *
f58c44e6
BG
14 * f75387:
15 * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
16 *
84f1e442
RV
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *
31 */
32
33#include <linux/module.h>
34#include <linux/jiffies.h>
35#include <linux/hwmon.h>
36#include <linux/hwmon-sysfs.h>
37#include <linux/i2c.h>
38#include <linux/err.h>
39#include <linux/mutex.h>
ff312d07 40#include <linux/f75375s.h>
5a0e3ad6 41#include <linux/slab.h>
84f1e442
RV
42
43/* Addresses to scan */
25e9c86d 44static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
84f1e442 45
f58c44e6 46enum chips { f75373, f75375, f75387 };
84f1e442
RV
47
48/* Fintek F75375 registers */
49#define F75375_REG_CONFIG0 0x0
50#define F75375_REG_CONFIG1 0x1
51#define F75375_REG_CONFIG2 0x2
52#define F75375_REG_CONFIG3 0x3
53#define F75375_REG_ADDR 0x4
54#define F75375_REG_INTR 0x31
55#define F75375_CHIP_ID 0x5A
56#define F75375_REG_VERSION 0x5C
57#define F75375_REG_VENDOR 0x5D
58#define F75375_REG_FAN_TIMER 0x60
59
60#define F75375_REG_VOLT(nr) (0x10 + (nr))
61#define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2)
62#define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2)
63
64#define F75375_REG_TEMP(nr) (0x14 + (nr))
f58c44e6 65#define F75387_REG_TEMP11_LSB(nr) (0x1a + (nr))
84f1e442
RV
66#define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
67#define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
68
69#define F75375_REG_FAN(nr) (0x16 + (nr) * 2)
70#define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2)
71#define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10)
72#define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10)
73#define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10)
74
75#define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10)
76#define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step))
77#define F75375_REG_FAN_B_SPEED(nr, step) \
78 ((0xA5 + (nr) * 0x10) + (step) * 2)
79
80#define F75375_REG_PWM1_RAISE_DUTY 0x69
81#define F75375_REG_PWM2_RAISE_DUTY 0x6A
82#define F75375_REG_PWM1_DROP_DUTY 0x6B
83#define F75375_REG_PWM2_DROP_DUTY 0x6C
84
f58c44e6
BG
85#define F75375_FAN_CTRL_LINEAR(nr) (4 + nr)
86#define F75387_FAN_CTRL_LINEAR(nr) (1 + ((nr) * 4))
96f36408 87#define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
f58c44e6
BG
88#define F75387_FAN_DUTY_MODE(nr) (2 + ((nr) * 4))
89#define F75387_FAN_MANU_MODE(nr) ((nr) * 4)
84f1e442
RV
90
91/*
92 * Data structures and manipulation thereof
93 */
94
95struct f75375_data {
96 unsigned short addr;
1beeffe4 97 struct device *hwmon_dev;
84f1e442
RV
98
99 const char *name;
100 int kind;
101 struct mutex update_lock; /* protect register access */
102 char valid;
103 unsigned long last_updated; /* In jiffies */
104 unsigned long last_limits; /* In jiffies */
105
106 /* Register values */
107 u8 in[4];
108 u8 in_max[4];
109 u8 in_min[4];
110 u16 fan[2];
111 u16 fan_min[2];
740f6be3
GR
112 u16 fan_max[2];
113 u16 fan_target[2];
84f1e442
RV
114 u8 fan_timer;
115 u8 pwm[2];
116 u8 pwm_mode[2];
117 u8 pwm_enable[2];
f58c44e6
BG
118 /*
119 * f75387: For remote temperature reading, it uses signed 11-bit
120 * values with LSB = 0.125 degree Celsius, left-justified in 16-bit
121 * registers. For original 8-bit temp readings, the LSB just is 0.
122 */
123 s16 temp11[2];
84f1e442
RV
124 s8 temp_high[2];
125 s8 temp_max_hyst[2];
126};
127
310ec792 128static int f75375_detect(struct i2c_client *client,
935ada8c 129 struct i2c_board_info *info);
d2653e92
JD
130static int f75375_probe(struct i2c_client *client,
131 const struct i2c_device_id *id);
620c142d 132static int f75375_remove(struct i2c_client *client);
84f1e442 133
3760f736
JD
134static const struct i2c_device_id f75375_id[] = {
135 { "f75373", f75373 },
136 { "f75375", f75375 },
f58c44e6 137 { "f75387", f75387 },
3760f736
JD
138 { }
139};
140MODULE_DEVICE_TABLE(i2c, f75375_id);
141
620c142d 142static struct i2c_driver f75375_driver = {
935ada8c 143 .class = I2C_CLASS_HWMON,
620c142d
RV
144 .driver = {
145 .name = "f75375",
146 },
147 .probe = f75375_probe,
148 .remove = f75375_remove,
3760f736 149 .id_table = f75375_id,
935ada8c 150 .detect = f75375_detect,
c3813d6a 151 .address_list = normal_i2c,
620c142d
RV
152};
153
84f1e442
RV
154static inline int f75375_read8(struct i2c_client *client, u8 reg)
155{
156 return i2c_smbus_read_byte_data(client, reg);
157}
158
159/* in most cases, should be called while holding update_lock */
160static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
161{
4fd826ef
GR
162 return (i2c_smbus_read_byte_data(client, reg) << 8)
163 | i2c_smbus_read_byte_data(client, reg + 1);
84f1e442
RV
164}
165
166static inline void f75375_write8(struct i2c_client *client, u8 reg,
167 u8 value)
168{
169 i2c_smbus_write_byte_data(client, reg, value);
170}
171
172static inline void f75375_write16(struct i2c_client *client, u8 reg,
173 u16 value)
174{
eb2f255b 175 int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
84f1e442
RV
176 if (err)
177 return;
178 i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
179}
180
331255d3
NS
181static void f75375_write_pwm(struct i2c_client *client, int nr)
182{
183 struct f75375_data *data = i2c_get_clientdata(client);
184 if (data->kind == f75387)
185 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->pwm[nr]);
186 else
187 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
188 data->pwm[nr]);
189}
190
84f1e442
RV
191static struct f75375_data *f75375_update_device(struct device *dev)
192{
193 struct i2c_client *client = to_i2c_client(dev);
194 struct f75375_data *data = i2c_get_clientdata(client);
195 int nr;
196
197 mutex_lock(&data->update_lock);
198
199 /* Limit registers cache is refreshed after 60 seconds */
200 if (time_after(jiffies, data->last_limits + 60 * HZ)
201 || !data->valid) {
202 for (nr = 0; nr < 2; nr++) {
203 data->temp_high[nr] =
204 f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
205 data->temp_max_hyst[nr] =
206 f75375_read8(client, F75375_REG_TEMP_HYST(nr));
740f6be3 207 data->fan_max[nr] =
84f1e442
RV
208 f75375_read16(client, F75375_REG_FAN_FULL(nr));
209 data->fan_min[nr] =
210 f75375_read16(client, F75375_REG_FAN_MIN(nr));
740f6be3 211 data->fan_target[nr] =
84f1e442 212 f75375_read16(client, F75375_REG_FAN_EXP(nr));
84f1e442
RV
213 }
214 for (nr = 0; nr < 4; nr++) {
215 data->in_max[nr] =
216 f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
217 data->in_min[nr] =
218 f75375_read8(client, F75375_REG_VOLT_LOW(nr));
219 }
220 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
221 data->last_limits = jiffies;
222 }
223
224 /* Measurement registers cache is refreshed after 2 second */
225 if (time_after(jiffies, data->last_updated + 2 * HZ)
226 || !data->valid) {
227 for (nr = 0; nr < 2; nr++) {
a1c1baf0
NS
228 data->pwm[nr] = f75375_read8(client,
229 F75375_REG_FAN_PWM_DUTY(nr));
f58c44e6
BG
230 /* assign MSB, therefore shift it by 8 bits */
231 data->temp11[nr] =
232 f75375_read8(client, F75375_REG_TEMP(nr)) << 8;
233 if (data->kind == f75387)
234 /* merge F75387's temperature LSB (11-bit) */
235 data->temp11[nr] |=
236 f75375_read8(client,
237 F75387_REG_TEMP11_LSB(nr));
84f1e442
RV
238 data->fan[nr] =
239 f75375_read16(client, F75375_REG_FAN(nr));
240 }
241 for (nr = 0; nr < 4; nr++)
242 data->in[nr] =
243 f75375_read8(client, F75375_REG_VOLT(nr));
244
245 data->last_updated = jiffies;
246 data->valid = 1;
247 }
248
249 mutex_unlock(&data->update_lock);
250 return data;
251}
252
253static inline u16 rpm_from_reg(u16 reg)
254{
255 if (reg == 0 || reg == 0xffff)
256 return 0;
4fd826ef 257 return 1500000 / reg;
84f1e442
RV
258}
259
260static inline u16 rpm_to_reg(int rpm)
261{
262 if (rpm < 367 || rpm > 0xffff)
263 return 0xffff;
4fd826ef 264 return 1500000 / rpm;
84f1e442
RV
265}
266
267static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
268 const char *buf, size_t count)
269{
270 int nr = to_sensor_dev_attr(attr)->index;
271 struct i2c_client *client = to_i2c_client(dev);
272 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
273 unsigned long val;
274 int err;
275
276 err = kstrtoul(buf, 10, &val);
277 if (err < 0)
278 return err;
84f1e442
RV
279
280 mutex_lock(&data->update_lock);
281 data->fan_min[nr] = rpm_to_reg(val);
282 f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
283 mutex_unlock(&data->update_lock);
284 return count;
285}
286
740f6be3 287static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr,
84f1e442
RV
288 const char *buf, size_t count)
289{
290 int nr = to_sensor_dev_attr(attr)->index;
291 struct i2c_client *client = to_i2c_client(dev);
292 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
293 unsigned long val;
294 int err;
295
296 err = kstrtoul(buf, 10, &val);
297 if (err < 0)
298 return err;
84f1e442
RV
299
300 mutex_lock(&data->update_lock);
740f6be3
GR
301 data->fan_target[nr] = rpm_to_reg(val);
302 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
84f1e442
RV
303 mutex_unlock(&data->update_lock);
304 return count;
305}
306
307static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
308 const char *buf, size_t count)
309{
310 int nr = to_sensor_dev_attr(attr)->index;
311 struct i2c_client *client = to_i2c_client(dev);
312 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
313 unsigned long val;
314 int err;
315
316 err = kstrtoul(buf, 10, &val);
317 if (err < 0)
318 return err;
84f1e442
RV
319
320 mutex_lock(&data->update_lock);
321 data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
331255d3 322 f75375_write_pwm(client, nr);
84f1e442
RV
323 mutex_unlock(&data->update_lock);
324 return count;
325}
326
327static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
328 *attr, char *buf)
329{
330 int nr = to_sensor_dev_attr(attr)->index;
331 struct f75375_data *data = f75375_update_device(dev);
332 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
333}
334
ff312d07 335static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
84f1e442 336{
84f1e442 337 struct f75375_data *data = i2c_get_clientdata(client);
84f1e442
RV
338 u8 fanmode;
339
3310600a 340 if (val < 0 || val > 3)
84f1e442
RV
341 return -EINVAL;
342
84f1e442 343 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
f58c44e6
BG
344 if (data->kind == f75387) {
345 /* clear each fanX_mode bit before setting them properly */
346 fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr));
347 fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr));
348 switch (val) {
349 case 0: /* full speed */
350 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
351 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
352 data->pwm[nr] = 255;
f58c44e6
BG
353 break;
354 case 1: /* PWM */
355 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
356 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
357 break;
358 case 2: /* AUTOMATIC*/
359 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
360 break;
361 case 3: /* fan speed */
362 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
363 break;
364 }
365 } else {
366 /* clear each fanX_mode bit before setting them properly */
367 fanmode &= ~(3 << FAN_CTRL_MODE(nr));
368 switch (val) {
369 case 0: /* full speed */
370 fanmode |= (3 << FAN_CTRL_MODE(nr));
371 data->pwm[nr] = 255;
f58c44e6
BG
372 break;
373 case 1: /* PWM */
374 fanmode |= (3 << FAN_CTRL_MODE(nr));
375 break;
376 case 2: /* AUTOMATIC*/
09e87e5c 377 fanmode |= (1 << FAN_CTRL_MODE(nr));
f58c44e6
BG
378 break;
379 case 3: /* fan speed */
380 break;
381 }
84f1e442 382 }
f58c44e6 383
84f1e442
RV
384 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
385 data->pwm_enable[nr] = val;
c1c1a3d0 386 if (val == 0)
331255d3 387 f75375_write_pwm(client, nr);
ff312d07
RV
388 return 0;
389}
390
391static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
392 const char *buf, size_t count)
393{
394 int nr = to_sensor_dev_attr(attr)->index;
395 struct i2c_client *client = to_i2c_client(dev);
396 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
397 unsigned long val;
398 int err;
399
400 err = kstrtoul(buf, 10, &val);
401 if (err < 0)
402 return err;
ff312d07
RV
403
404 mutex_lock(&data->update_lock);
405 err = set_pwm_enable_direct(client, nr, val);
84f1e442 406 mutex_unlock(&data->update_lock);
ff312d07 407 return err ? err : count;
84f1e442
RV
408}
409
410static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t count)
412{
413 int nr = to_sensor_dev_attr(attr)->index;
414 struct i2c_client *client = to_i2c_client(dev);
415 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
416 unsigned long val;
417 int err;
418 u8 conf;
f58c44e6 419 char reg, ctrl;
4fd826ef
GR
420
421 err = kstrtoul(buf, 10, &val);
422 if (err < 0)
423 return err;
84f1e442 424
76e83bef 425 if (!(val == 0 || val == 1))
84f1e442
RV
426 return -EINVAL;
427
5cd3222a
GR
428 /* F75373 does not support DC (linear voltage) fan control mode */
429 if (data->kind == f75373 && val == 0)
430 return -EINVAL;
431
f58c44e6
BG
432 /* take care for different registers */
433 if (data->kind == f75387) {
434 reg = F75375_REG_FAN_TIMER;
435 ctrl = F75387_FAN_CTRL_LINEAR(nr);
436 } else {
437 reg = F75375_REG_CONFIG1;
438 ctrl = F75375_FAN_CTRL_LINEAR(nr);
439 }
440
84f1e442 441 mutex_lock(&data->update_lock);
f58c44e6
BG
442 conf = f75375_read8(client, reg);
443 conf &= ~(1 << ctrl);
84f1e442
RV
444
445 if (val == 0)
f58c44e6 446 conf |= (1 << ctrl);
84f1e442 447
f58c44e6 448 f75375_write8(client, reg, conf);
84f1e442
RV
449 data->pwm_mode[nr] = val;
450 mutex_unlock(&data->update_lock);
451 return count;
452}
453
454static ssize_t show_pwm(struct device *dev, struct device_attribute
455 *attr, char *buf)
456{
457 int nr = to_sensor_dev_attr(attr)->index;
458 struct f75375_data *data = f75375_update_device(dev);
459 return sprintf(buf, "%d\n", data->pwm[nr]);
460}
461
462static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
463 *attr, char *buf)
464{
465 int nr = to_sensor_dev_attr(attr)->index;
466 struct f75375_data *data = f75375_update_device(dev);
467 return sprintf(buf, "%d\n", data->pwm_mode[nr]);
468}
469
470#define VOLT_FROM_REG(val) ((val) * 8)
471#define VOLT_TO_REG(val) ((val) / 8)
472
473static ssize_t show_in(struct device *dev, struct device_attribute *attr,
474 char *buf)
475{
476 int nr = to_sensor_dev_attr(attr)->index;
477 struct f75375_data *data = f75375_update_device(dev);
478 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
479}
480
481static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
482 char *buf)
483{
484 int nr = to_sensor_dev_attr(attr)->index;
485 struct f75375_data *data = f75375_update_device(dev);
486 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
487}
488
489static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
490 char *buf)
491{
492 int nr = to_sensor_dev_attr(attr)->index;
493 struct f75375_data *data = f75375_update_device(dev);
494 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
495}
496
497static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
498 const char *buf, size_t count)
499{
500 int nr = to_sensor_dev_attr(attr)->index;
501 struct i2c_client *client = to_i2c_client(dev);
502 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
503 unsigned long val;
504 int err;
505
506 err = kstrtoul(buf, 10, &val);
507 if (err < 0)
508 return err;
509
84f1e442
RV
510 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
511 mutex_lock(&data->update_lock);
512 data->in_max[nr] = val;
513 f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
514 mutex_unlock(&data->update_lock);
515 return count;
516}
517
518static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
519 const char *buf, size_t count)
520{
521 int nr = to_sensor_dev_attr(attr)->index;
522 struct i2c_client *client = to_i2c_client(dev);
523 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
524 unsigned long val;
525 int err;
526
527 err = kstrtoul(buf, 10, &val);
528 if (err < 0)
529 return err;
530
84f1e442
RV
531 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
532 mutex_lock(&data->update_lock);
533 data->in_min[nr] = val;
534 f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
535 mutex_unlock(&data->update_lock);
536 return count;
537}
538#define TEMP_FROM_REG(val) ((val) * 1000)
539#define TEMP_TO_REG(val) ((val) / 1000)
f58c44e6 540#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
84f1e442 541
f58c44e6 542static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
84f1e442
RV
543 char *buf)
544{
545 int nr = to_sensor_dev_attr(attr)->index;
546 struct f75375_data *data = f75375_update_device(dev);
f58c44e6 547 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr]));
84f1e442
RV
548}
549
550static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
551 char *buf)
552{
553 int nr = to_sensor_dev_attr(attr)->index;
554 struct f75375_data *data = f75375_update_device(dev);
555 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
556}
557
558static ssize_t show_temp_max_hyst(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
561 int nr = to_sensor_dev_attr(attr)->index;
562 struct f75375_data *data = f75375_update_device(dev);
563 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
564}
565
566static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
568{
569 int nr = to_sensor_dev_attr(attr)->index;
570 struct i2c_client *client = to_i2c_client(dev);
571 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
572 unsigned long val;
573 int err;
574
575 err = kstrtoul(buf, 10, &val);
576 if (err < 0)
577 return err;
578
84f1e442
RV
579 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
580 mutex_lock(&data->update_lock);
581 data->temp_high[nr] = val;
582 f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
583 mutex_unlock(&data->update_lock);
584 return count;
585}
586
587static ssize_t set_temp_max_hyst(struct device *dev,
588 struct device_attribute *attr, const char *buf, size_t count)
589{
590 int nr = to_sensor_dev_attr(attr)->index;
591 struct i2c_client *client = to_i2c_client(dev);
592 struct f75375_data *data = i2c_get_clientdata(client);
4fd826ef
GR
593 unsigned long val;
594 int err;
595
596 err = kstrtoul(buf, 10, &val);
597 if (err < 0)
598 return err;
599
84f1e442
RV
600 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
601 mutex_lock(&data->update_lock);
602 data->temp_max_hyst[nr] = val;
603 f75375_write8(client, F75375_REG_TEMP_HYST(nr),
604 data->temp_max_hyst[nr]);
605 mutex_unlock(&data->update_lock);
606 return count;
607}
608
609#define show_fan(thing) \
610static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
611 char *buf)\
612{\
613 int nr = to_sensor_dev_attr(attr)->index;\
614 struct f75375_data *data = f75375_update_device(dev); \
615 return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
616}
617
618show_fan(fan);
619show_fan(fan_min);
740f6be3
GR
620show_fan(fan_max);
621show_fan(fan_target);
84f1e442
RV
622
623static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
624static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
625 show_in_max, set_in_max, 0);
626static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
627 show_in_min, set_in_min, 0);
628static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
629static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
630 show_in_max, set_in_max, 1);
631static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
632 show_in_min, set_in_min, 1);
633static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
634static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
635 show_in_max, set_in_max, 2);
636static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
637 show_in_min, set_in_min, 2);
638static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
639static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
640 show_in_max, set_in_max, 3);
641static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
642 show_in_min, set_in_min, 3);
f58c44e6 643static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
84f1e442
RV
644static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
645 show_temp_max_hyst, set_temp_max_hyst, 0);
646static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
647 show_temp_max, set_temp_max, 0);
f58c44e6 648static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
84f1e442
RV
649static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
650 show_temp_max_hyst, set_temp_max_hyst, 1);
651static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
652 show_temp_max, set_temp_max, 1);
653static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
740f6be3 654static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
84f1e442
RV
655static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
656 show_fan_min, set_fan_min, 0);
740f6be3
GR
657static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
658 show_fan_target, set_fan_target, 0);
84f1e442 659static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
740f6be3 660static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
84f1e442
RV
661static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
662 show_fan_min, set_fan_min, 1);
740f6be3
GR
663static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
664 show_fan_target, set_fan_target, 1);
84f1e442
RV
665static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
666 show_pwm, set_pwm, 0);
667static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
668 show_pwm_enable, set_pwm_enable, 0);
76e83bef 669static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
84f1e442
RV
670 show_pwm_mode, set_pwm_mode, 0);
671static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
672 show_pwm, set_pwm, 1);
673static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
674 show_pwm_enable, set_pwm_enable, 1);
76e83bef 675static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
84f1e442
RV
676 show_pwm_mode, set_pwm_mode, 1);
677
678static struct attribute *f75375_attributes[] = {
679 &sensor_dev_attr_temp1_input.dev_attr.attr,
680 &sensor_dev_attr_temp1_max.dev_attr.attr,
681 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
682 &sensor_dev_attr_temp2_input.dev_attr.attr,
683 &sensor_dev_attr_temp2_max.dev_attr.attr,
684 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
685 &sensor_dev_attr_fan1_input.dev_attr.attr,
740f6be3 686 &sensor_dev_attr_fan1_max.dev_attr.attr,
84f1e442 687 &sensor_dev_attr_fan1_min.dev_attr.attr,
740f6be3 688 &sensor_dev_attr_fan1_target.dev_attr.attr,
84f1e442 689 &sensor_dev_attr_fan2_input.dev_attr.attr,
740f6be3 690 &sensor_dev_attr_fan2_max.dev_attr.attr,
84f1e442 691 &sensor_dev_attr_fan2_min.dev_attr.attr,
740f6be3 692 &sensor_dev_attr_fan2_target.dev_attr.attr,
84f1e442
RV
693 &sensor_dev_attr_pwm1.dev_attr.attr,
694 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
695 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
696 &sensor_dev_attr_pwm2.dev_attr.attr,
697 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
698 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
699 &sensor_dev_attr_in0_input.dev_attr.attr,
700 &sensor_dev_attr_in0_max.dev_attr.attr,
701 &sensor_dev_attr_in0_min.dev_attr.attr,
702 &sensor_dev_attr_in1_input.dev_attr.attr,
703 &sensor_dev_attr_in1_max.dev_attr.attr,
704 &sensor_dev_attr_in1_min.dev_attr.attr,
705 &sensor_dev_attr_in2_input.dev_attr.attr,
706 &sensor_dev_attr_in2_max.dev_attr.attr,
707 &sensor_dev_attr_in2_min.dev_attr.attr,
708 &sensor_dev_attr_in3_input.dev_attr.attr,
709 &sensor_dev_attr_in3_max.dev_attr.attr,
710 &sensor_dev_attr_in3_min.dev_attr.attr,
711 NULL
712};
713
714static const struct attribute_group f75375_group = {
715 .attrs = f75375_attributes,
716};
717
ff312d07
RV
718static void f75375_init(struct i2c_client *client, struct f75375_data *data,
719 struct f75375s_platform_data *f75375s_pdata)
720{
721 int nr;
b1b561a2
GR
722
723 if (!f75375s_pdata) {
724 u8 conf, mode;
725 int nr;
726
727 conf = f75375_read8(client, F75375_REG_CONFIG1);
728 mode = f75375_read8(client, F75375_REG_FAN_TIMER);
729 for (nr = 0; nr < 2; nr++) {
f58c44e6
BG
730 if (data->kind == f75387) {
731 bool manu, duty;
732
a367a1e0 733 if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr))))
f58c44e6
BG
734 data->pwm_mode[nr] = 1;
735
736 manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
737 duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
738 if (manu && duty)
739 /* speed */
740 data->pwm_enable[nr] = 3;
741 else if (!manu && duty)
742 /* automatic */
743 data->pwm_enable[nr] = 2;
744 else
745 /* manual */
746 data->pwm_enable[nr] = 1;
747 } else {
748 if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr))))
749 data->pwm_mode[nr] = 1;
750
751 switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
752 case 0: /* speed */
753 data->pwm_enable[nr] = 3;
754 break;
755 case 1: /* automatic */
756 data->pwm_enable[nr] = 2;
757 break;
758 default: /* manual */
759 data->pwm_enable[nr] = 1;
760 break;
761 }
b1b561a2
GR
762 }
763 }
764 return;
765 }
766
ff312d07
RV
767 set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
768 set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
769 for (nr = 0; nr < 2; nr++) {
770 data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
331255d3 771 f75375_write_pwm(client, nr);
ff312d07
RV
772 }
773
774}
775
d2653e92
JD
776static int f75375_probe(struct i2c_client *client,
777 const struct i2c_device_id *id)
620c142d 778{
51b3e270 779 struct f75375_data *data;
ff312d07 780 struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
620c142d
RV
781 int err;
782
783 if (!i2c_check_functionality(client->adapter,
784 I2C_FUNC_SMBUS_BYTE_DATA))
785 return -EIO;
4fd826ef
GR
786 data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL);
787 if (!data)
620c142d
RV
788 return -ENOMEM;
789
790 i2c_set_clientdata(client, data);
620c142d 791 mutex_init(&data->update_lock);
3760f736 792 data->kind = id->driver_data;
620c142d 793
4fd826ef
GR
794 err = sysfs_create_group(&client->dev.kobj, &f75375_group);
795 if (err)
620c142d
RV
796 goto exit_free;
797
edeea102 798 if (data->kind != f75373) {
76e83bef
RV
799 err = sysfs_chmod_file(&client->dev.kobj,
800 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
801 S_IRUGO | S_IWUSR);
802 if (err)
803 goto exit_remove;
804 err = sysfs_chmod_file(&client->dev.kobj,
805 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
806 S_IRUGO | S_IWUSR);
807 if (err)
808 goto exit_remove;
809 }
810
620c142d
RV
811 data->hwmon_dev = hwmon_device_register(&client->dev);
812 if (IS_ERR(data->hwmon_dev)) {
813 err = PTR_ERR(data->hwmon_dev);
814 goto exit_remove;
815 }
816
b1b561a2 817 f75375_init(client, data, f75375s_pdata);
ff312d07 818
620c142d
RV
819 return 0;
820
821exit_remove:
822 sysfs_remove_group(&client->dev.kobj, &f75375_group);
823exit_free:
824 kfree(data);
620c142d
RV
825 return err;
826}
827
828static int f75375_remove(struct i2c_client *client)
829{
830 struct f75375_data *data = i2c_get_clientdata(client);
831 hwmon_device_unregister(data->hwmon_dev);
832 sysfs_remove_group(&client->dev.kobj, &f75375_group);
84f1e442
RV
833 kfree(data);
834 return 0;
835}
836
935ada8c 837/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 838static int f75375_detect(struct i2c_client *client,
935ada8c 839 struct i2c_board_info *info)
84f1e442 840{
935ada8c 841 struct i2c_adapter *adapter = client->adapter;
52df6440
JD
842 u16 vendid, chipid;
843 u8 version;
844 const char *name;
84f1e442 845
52df6440
JD
846 vendid = f75375_read16(client, F75375_REG_VENDOR);
847 chipid = f75375_read16(client, F75375_CHIP_ID);
f58c44e6
BG
848 if (vendid != 0x1934)
849 return -ENODEV;
850
851 if (chipid == 0x0306)
84f1e442 852 name = "f75375";
f58c44e6 853 else if (chipid == 0x0204)
84f1e442 854 name = "f75373";
f58c44e6
BG
855 else if (chipid == 0x0410)
856 name = "f75387";
52df6440
JD
857 else
858 return -ENODEV;
859
860 version = f75375_read8(client, F75375_REG_VERSION);
84f1e442 861 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
935ada8c 862 strlcpy(info->type, name, I2C_NAME_SIZE);
84f1e442 863
84f1e442 864 return 0;
84f1e442
RV
865}
866
867static int __init sensors_f75375_init(void)
868{
935ada8c 869 return i2c_add_driver(&f75375_driver);
84f1e442
RV
870}
871
872static void __exit sensors_f75375_exit(void)
873{
874 i2c_del_driver(&f75375_driver);
875}
876
b26e0ed4 877MODULE_AUTHOR("Riku Voipio");
84f1e442 878MODULE_LICENSE("GPL");
f58c44e6 879MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");
84f1e442
RV
880
881module_init(sensors_f75375_init);
882module_exit(sensors_f75375_exit);