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