perf bench futex: Cache align the worker struct
[linux/fpc-iii.git] / drivers / hwmon / f75375s.c
blob80c42bea90ed59a0a8c89d996e78613df33f8745
1 /*
2 * f75375s.c - driver for the Fintek F75375/SP, F75373 and
3 * F75387SG/RG hardware monitoring features
4 * Copyright (C) 2006-2007 Riku Voipio
6 * Datasheets available at:
8 * f75375:
9 * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
11 * f75373:
12 * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
14 * f75387:
15 * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
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.
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.
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.
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>
40 #include <linux/f75375s.h>
41 #include <linux/slab.h>
43 /* Addresses to scan */
44 static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
46 enum chips { f75373, f75375, f75387 };
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
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)
64 #define F75375_REG_TEMP(nr) (0x14 + (nr))
65 #define F75387_REG_TEMP11_LSB(nr) (0x1a + (nr))
66 #define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
67 #define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
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)
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)
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
85 #define F75375_FAN_CTRL_LINEAR(nr) (4 + nr)
86 #define F75387_FAN_CTRL_LINEAR(nr) (1 + ((nr) * 4))
87 #define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
88 #define F75387_FAN_DUTY_MODE(nr) (2 + ((nr) * 4))
89 #define F75387_FAN_MANU_MODE(nr) ((nr) * 4)
92 * Data structures and manipulation thereof
95 struct f75375_data {
96 unsigned short addr;
97 struct device *hwmon_dev;
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 */
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];
112 u16 fan_max[2];
113 u16 fan_target[2];
114 u8 fan_timer;
115 u8 pwm[2];
116 u8 pwm_mode[2];
117 u8 pwm_enable[2];
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.
123 s16 temp11[2];
124 s8 temp_high[2];
125 s8 temp_max_hyst[2];
128 static int f75375_detect(struct i2c_client *client,
129 struct i2c_board_info *info);
130 static int f75375_probe(struct i2c_client *client,
131 const struct i2c_device_id *id);
132 static int f75375_remove(struct i2c_client *client);
134 static const struct i2c_device_id f75375_id[] = {
135 { "f75373", f75373 },
136 { "f75375", f75375 },
137 { "f75387", f75387 },
140 MODULE_DEVICE_TABLE(i2c, f75375_id);
142 static struct i2c_driver f75375_driver = {
143 .class = I2C_CLASS_HWMON,
144 .driver = {
145 .name = "f75375",
147 .probe = f75375_probe,
148 .remove = f75375_remove,
149 .id_table = f75375_id,
150 .detect = f75375_detect,
151 .address_list = normal_i2c,
154 static inline int f75375_read8(struct i2c_client *client, u8 reg)
156 return i2c_smbus_read_byte_data(client, reg);
159 /* in most cases, should be called while holding update_lock */
160 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
162 return (i2c_smbus_read_byte_data(client, reg) << 8)
163 | i2c_smbus_read_byte_data(client, reg + 1);
166 static inline void f75375_write8(struct i2c_client *client, u8 reg,
167 u8 value)
169 i2c_smbus_write_byte_data(client, reg, value);
172 static inline void f75375_write16(struct i2c_client *client, u8 reg,
173 u16 value)
175 int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
176 if (err)
177 return;
178 i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
181 static void f75375_write_pwm(struct i2c_client *client, int nr)
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]);
191 static struct f75375_data *f75375_update_device(struct device *dev)
193 struct i2c_client *client = to_i2c_client(dev);
194 struct f75375_data *data = i2c_get_clientdata(client);
195 int nr;
197 mutex_lock(&data->update_lock);
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));
207 data->fan_max[nr] =
208 f75375_read16(client, F75375_REG_FAN_FULL(nr));
209 data->fan_min[nr] =
210 f75375_read16(client, F75375_REG_FAN_MIN(nr));
211 data->fan_target[nr] =
212 f75375_read16(client, F75375_REG_FAN_EXP(nr));
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));
220 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
221 data->last_limits = jiffies;
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++) {
228 data->pwm[nr] = f75375_read8(client,
229 F75375_REG_FAN_PWM_DUTY(nr));
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));
238 data->fan[nr] =
239 f75375_read16(client, F75375_REG_FAN(nr));
241 for (nr = 0; nr < 4; nr++)
242 data->in[nr] =
243 f75375_read8(client, F75375_REG_VOLT(nr));
245 data->last_updated = jiffies;
246 data->valid = 1;
249 mutex_unlock(&data->update_lock);
250 return data;
253 static inline u16 rpm_from_reg(u16 reg)
255 if (reg == 0 || reg == 0xffff)
256 return 0;
257 return 1500000 / reg;
260 static inline u16 rpm_to_reg(int rpm)
262 if (rpm < 367 || rpm > 0xffff)
263 return 0xffff;
264 return 1500000 / rpm;
267 static bool duty_mode_enabled(u8 pwm_enable)
269 switch (pwm_enable) {
270 case 0: /* Manual, duty mode (full speed) */
271 case 1: /* Manual, duty mode */
272 case 4: /* Auto, duty mode */
273 return true;
274 case 2: /* Auto, speed mode */
275 case 3: /* Manual, speed mode */
276 return false;
277 default:
278 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
279 return true;
283 static bool auto_mode_enabled(u8 pwm_enable)
285 switch (pwm_enable) {
286 case 0: /* Manual, duty mode (full speed) */
287 case 1: /* Manual, duty mode */
288 case 3: /* Manual, speed mode */
289 return false;
290 case 2: /* Auto, speed mode */
291 case 4: /* Auto, duty mode */
292 return true;
293 default:
294 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
295 return false;
299 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
300 const char *buf, size_t count)
302 int nr = to_sensor_dev_attr(attr)->index;
303 struct i2c_client *client = to_i2c_client(dev);
304 struct f75375_data *data = i2c_get_clientdata(client);
305 unsigned long val;
306 int err;
308 err = kstrtoul(buf, 10, &val);
309 if (err < 0)
310 return err;
312 mutex_lock(&data->update_lock);
313 data->fan_min[nr] = rpm_to_reg(val);
314 f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
315 mutex_unlock(&data->update_lock);
316 return count;
319 static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr,
320 const char *buf, size_t count)
322 int nr = to_sensor_dev_attr(attr)->index;
323 struct i2c_client *client = to_i2c_client(dev);
324 struct f75375_data *data = i2c_get_clientdata(client);
325 unsigned long val;
326 int err;
328 err = kstrtoul(buf, 10, &val);
329 if (err < 0)
330 return err;
332 if (auto_mode_enabled(data->pwm_enable[nr]))
333 return -EINVAL;
334 if (data->kind == f75387 && duty_mode_enabled(data->pwm_enable[nr]))
335 return -EINVAL;
337 mutex_lock(&data->update_lock);
338 data->fan_target[nr] = rpm_to_reg(val);
339 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
340 mutex_unlock(&data->update_lock);
341 return count;
344 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
345 const char *buf, size_t count)
347 int nr = to_sensor_dev_attr(attr)->index;
348 struct i2c_client *client = to_i2c_client(dev);
349 struct f75375_data *data = i2c_get_clientdata(client);
350 unsigned long val;
351 int err;
353 err = kstrtoul(buf, 10, &val);
354 if (err < 0)
355 return err;
357 if (auto_mode_enabled(data->pwm_enable[nr]) ||
358 !duty_mode_enabled(data->pwm_enable[nr]))
359 return -EINVAL;
361 mutex_lock(&data->update_lock);
362 data->pwm[nr] = clamp_val(val, 0, 255);
363 f75375_write_pwm(client, nr);
364 mutex_unlock(&data->update_lock);
365 return count;
368 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
369 *attr, char *buf)
371 int nr = to_sensor_dev_attr(attr)->index;
372 struct f75375_data *data = f75375_update_device(dev);
373 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
376 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
378 struct f75375_data *data = i2c_get_clientdata(client);
379 u8 fanmode;
381 if (val < 0 || val > 4)
382 return -EINVAL;
384 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
385 if (data->kind == f75387) {
386 /* For now, deny dangerous toggling of duty mode */
387 if (duty_mode_enabled(data->pwm_enable[nr]) !=
388 duty_mode_enabled(val))
389 return -EOPNOTSUPP;
390 /* clear each fanX_mode bit before setting them properly */
391 fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr));
392 fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr));
393 switch (val) {
394 case 0: /* full speed */
395 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
396 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
397 data->pwm[nr] = 255;
398 break;
399 case 1: /* PWM */
400 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
401 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
402 break;
403 case 2: /* Automatic, speed mode */
404 break;
405 case 3: /* fan speed */
406 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
407 break;
408 case 4: /* Automatic, pwm */
409 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
410 break;
412 } else {
413 /* clear each fanX_mode bit before setting them properly */
414 fanmode &= ~(3 << FAN_CTRL_MODE(nr));
415 switch (val) {
416 case 0: /* full speed */
417 fanmode |= (3 << FAN_CTRL_MODE(nr));
418 data->pwm[nr] = 255;
419 break;
420 case 1: /* PWM */
421 fanmode |= (3 << FAN_CTRL_MODE(nr));
422 break;
423 case 2: /* AUTOMATIC*/
424 fanmode |= (1 << FAN_CTRL_MODE(nr));
425 break;
426 case 3: /* fan speed */
427 break;
428 case 4: /* Automatic pwm */
429 return -EINVAL;
433 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
434 data->pwm_enable[nr] = val;
435 if (val == 0)
436 f75375_write_pwm(client, nr);
437 return 0;
440 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
441 const char *buf, size_t count)
443 int nr = to_sensor_dev_attr(attr)->index;
444 struct i2c_client *client = to_i2c_client(dev);
445 struct f75375_data *data = i2c_get_clientdata(client);
446 unsigned long val;
447 int err;
449 err = kstrtoul(buf, 10, &val);
450 if (err < 0)
451 return err;
453 mutex_lock(&data->update_lock);
454 err = set_pwm_enable_direct(client, nr, val);
455 mutex_unlock(&data->update_lock);
456 return err ? err : count;
459 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
460 const char *buf, size_t count)
462 int nr = to_sensor_dev_attr(attr)->index;
463 struct i2c_client *client = to_i2c_client(dev);
464 struct f75375_data *data = i2c_get_clientdata(client);
465 unsigned long val;
466 int err;
467 u8 conf;
468 char reg, ctrl;
470 err = kstrtoul(buf, 10, &val);
471 if (err < 0)
472 return err;
474 if (!(val == 0 || val == 1))
475 return -EINVAL;
477 /* F75373 does not support DC (linear voltage) fan control mode */
478 if (data->kind == f75373 && val == 0)
479 return -EINVAL;
481 /* take care for different registers */
482 if (data->kind == f75387) {
483 reg = F75375_REG_FAN_TIMER;
484 ctrl = F75387_FAN_CTRL_LINEAR(nr);
485 } else {
486 reg = F75375_REG_CONFIG1;
487 ctrl = F75375_FAN_CTRL_LINEAR(nr);
490 mutex_lock(&data->update_lock);
491 conf = f75375_read8(client, reg);
492 conf &= ~(1 << ctrl);
494 if (val == 0)
495 conf |= (1 << ctrl);
497 f75375_write8(client, reg, conf);
498 data->pwm_mode[nr] = val;
499 mutex_unlock(&data->update_lock);
500 return count;
503 static ssize_t show_pwm(struct device *dev, struct device_attribute
504 *attr, char *buf)
506 int nr = to_sensor_dev_attr(attr)->index;
507 struct f75375_data *data = f75375_update_device(dev);
508 return sprintf(buf, "%d\n", data->pwm[nr]);
511 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
512 *attr, char *buf)
514 int nr = to_sensor_dev_attr(attr)->index;
515 struct f75375_data *data = f75375_update_device(dev);
516 return sprintf(buf, "%d\n", data->pwm_mode[nr]);
519 #define VOLT_FROM_REG(val) ((val) * 8)
520 #define VOLT_TO_REG(val) ((val) / 8)
522 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
523 char *buf)
525 int nr = to_sensor_dev_attr(attr)->index;
526 struct f75375_data *data = f75375_update_device(dev);
527 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
530 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
531 char *buf)
533 int nr = to_sensor_dev_attr(attr)->index;
534 struct f75375_data *data = f75375_update_device(dev);
535 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
538 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
539 char *buf)
541 int nr = to_sensor_dev_attr(attr)->index;
542 struct f75375_data *data = f75375_update_device(dev);
543 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
546 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
547 const char *buf, size_t count)
549 int nr = to_sensor_dev_attr(attr)->index;
550 struct i2c_client *client = to_i2c_client(dev);
551 struct f75375_data *data = i2c_get_clientdata(client);
552 unsigned long val;
553 int err;
555 err = kstrtoul(buf, 10, &val);
556 if (err < 0)
557 return err;
559 val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
560 mutex_lock(&data->update_lock);
561 data->in_max[nr] = val;
562 f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
563 mutex_unlock(&data->update_lock);
564 return count;
567 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
568 const char *buf, size_t count)
570 int nr = to_sensor_dev_attr(attr)->index;
571 struct i2c_client *client = to_i2c_client(dev);
572 struct f75375_data *data = i2c_get_clientdata(client);
573 unsigned long val;
574 int err;
576 err = kstrtoul(buf, 10, &val);
577 if (err < 0)
578 return err;
580 val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
581 mutex_lock(&data->update_lock);
582 data->in_min[nr] = val;
583 f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
584 mutex_unlock(&data->update_lock);
585 return count;
587 #define TEMP_FROM_REG(val) ((val) * 1000)
588 #define TEMP_TO_REG(val) ((val) / 1000)
589 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
591 static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
592 char *buf)
594 int nr = to_sensor_dev_attr(attr)->index;
595 struct f75375_data *data = f75375_update_device(dev);
596 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr]));
599 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
600 char *buf)
602 int nr = to_sensor_dev_attr(attr)->index;
603 struct f75375_data *data = f75375_update_device(dev);
604 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
607 static ssize_t show_temp_max_hyst(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 int nr = to_sensor_dev_attr(attr)->index;
611 struct f75375_data *data = f75375_update_device(dev);
612 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
615 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
616 const char *buf, size_t count)
618 int nr = to_sensor_dev_attr(attr)->index;
619 struct i2c_client *client = to_i2c_client(dev);
620 struct f75375_data *data = i2c_get_clientdata(client);
621 unsigned long val;
622 int err;
624 err = kstrtoul(buf, 10, &val);
625 if (err < 0)
626 return err;
628 val = clamp_val(TEMP_TO_REG(val), 0, 127);
629 mutex_lock(&data->update_lock);
630 data->temp_high[nr] = val;
631 f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
632 mutex_unlock(&data->update_lock);
633 return count;
636 static ssize_t set_temp_max_hyst(struct device *dev,
637 struct device_attribute *attr, const char *buf, size_t count)
639 int nr = to_sensor_dev_attr(attr)->index;
640 struct i2c_client *client = to_i2c_client(dev);
641 struct f75375_data *data = i2c_get_clientdata(client);
642 unsigned long val;
643 int err;
645 err = kstrtoul(buf, 10, &val);
646 if (err < 0)
647 return err;
649 val = clamp_val(TEMP_TO_REG(val), 0, 127);
650 mutex_lock(&data->update_lock);
651 data->temp_max_hyst[nr] = val;
652 f75375_write8(client, F75375_REG_TEMP_HYST(nr),
653 data->temp_max_hyst[nr]);
654 mutex_unlock(&data->update_lock);
655 return count;
658 #define show_fan(thing) \
659 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
660 char *buf)\
662 int nr = to_sensor_dev_attr(attr)->index;\
663 struct f75375_data *data = f75375_update_device(dev); \
664 return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
667 show_fan(fan);
668 show_fan(fan_min);
669 show_fan(fan_max);
670 show_fan(fan_target);
672 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
673 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
674 show_in_max, set_in_max, 0);
675 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
676 show_in_min, set_in_min, 0);
677 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
678 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
679 show_in_max, set_in_max, 1);
680 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
681 show_in_min, set_in_min, 1);
682 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
683 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
684 show_in_max, set_in_max, 2);
685 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
686 show_in_min, set_in_min, 2);
687 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
688 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
689 show_in_max, set_in_max, 3);
690 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
691 show_in_min, set_in_min, 3);
692 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
693 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
694 show_temp_max_hyst, set_temp_max_hyst, 0);
695 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
696 show_temp_max, set_temp_max, 0);
697 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
698 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
699 show_temp_max_hyst, set_temp_max_hyst, 1);
700 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
701 show_temp_max, set_temp_max, 1);
702 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
703 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
704 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
705 show_fan_min, set_fan_min, 0);
706 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
707 show_fan_target, set_fan_target, 0);
708 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
709 static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
710 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
711 show_fan_min, set_fan_min, 1);
712 static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
713 show_fan_target, set_fan_target, 1);
714 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
715 show_pwm, set_pwm, 0);
716 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
717 show_pwm_enable, set_pwm_enable, 0);
718 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
719 show_pwm_mode, set_pwm_mode, 0);
720 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
721 show_pwm, set_pwm, 1);
722 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
723 show_pwm_enable, set_pwm_enable, 1);
724 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
725 show_pwm_mode, set_pwm_mode, 1);
727 static struct attribute *f75375_attributes[] = {
728 &sensor_dev_attr_temp1_input.dev_attr.attr,
729 &sensor_dev_attr_temp1_max.dev_attr.attr,
730 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
731 &sensor_dev_attr_temp2_input.dev_attr.attr,
732 &sensor_dev_attr_temp2_max.dev_attr.attr,
733 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
734 &sensor_dev_attr_fan1_input.dev_attr.attr,
735 &sensor_dev_attr_fan1_max.dev_attr.attr,
736 &sensor_dev_attr_fan1_min.dev_attr.attr,
737 &sensor_dev_attr_fan1_target.dev_attr.attr,
738 &sensor_dev_attr_fan2_input.dev_attr.attr,
739 &sensor_dev_attr_fan2_max.dev_attr.attr,
740 &sensor_dev_attr_fan2_min.dev_attr.attr,
741 &sensor_dev_attr_fan2_target.dev_attr.attr,
742 &sensor_dev_attr_pwm1.dev_attr.attr,
743 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
744 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
745 &sensor_dev_attr_pwm2.dev_attr.attr,
746 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
747 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
748 &sensor_dev_attr_in0_input.dev_attr.attr,
749 &sensor_dev_attr_in0_max.dev_attr.attr,
750 &sensor_dev_attr_in0_min.dev_attr.attr,
751 &sensor_dev_attr_in1_input.dev_attr.attr,
752 &sensor_dev_attr_in1_max.dev_attr.attr,
753 &sensor_dev_attr_in1_min.dev_attr.attr,
754 &sensor_dev_attr_in2_input.dev_attr.attr,
755 &sensor_dev_attr_in2_max.dev_attr.attr,
756 &sensor_dev_attr_in2_min.dev_attr.attr,
757 &sensor_dev_attr_in3_input.dev_attr.attr,
758 &sensor_dev_attr_in3_max.dev_attr.attr,
759 &sensor_dev_attr_in3_min.dev_attr.attr,
760 NULL
763 static const struct attribute_group f75375_group = {
764 .attrs = f75375_attributes,
767 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
768 struct f75375s_platform_data *f75375s_pdata)
770 int nr;
772 if (!f75375s_pdata) {
773 u8 conf, mode;
774 int nr;
776 conf = f75375_read8(client, F75375_REG_CONFIG1);
777 mode = f75375_read8(client, F75375_REG_FAN_TIMER);
778 for (nr = 0; nr < 2; nr++) {
779 if (data->kind == f75387) {
780 bool manu, duty;
782 if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr))))
783 data->pwm_mode[nr] = 1;
785 manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
786 duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
787 if (!manu && duty)
788 /* auto, pwm */
789 data->pwm_enable[nr] = 4;
790 else if (manu && !duty)
791 /* manual, speed */
792 data->pwm_enable[nr] = 3;
793 else if (!manu && !duty)
794 /* automatic, speed */
795 data->pwm_enable[nr] = 2;
796 else
797 /* manual, pwm */
798 data->pwm_enable[nr] = 1;
799 } else {
800 if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr))))
801 data->pwm_mode[nr] = 1;
803 switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
804 case 0: /* speed */
805 data->pwm_enable[nr] = 3;
806 break;
807 case 1: /* automatic */
808 data->pwm_enable[nr] = 2;
809 break;
810 default: /* manual */
811 data->pwm_enable[nr] = 1;
812 break;
816 return;
819 set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
820 set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
821 for (nr = 0; nr < 2; nr++) {
822 if (auto_mode_enabled(f75375s_pdata->pwm_enable[nr]) ||
823 !duty_mode_enabled(f75375s_pdata->pwm_enable[nr]))
824 continue;
825 data->pwm[nr] = clamp_val(f75375s_pdata->pwm[nr], 0, 255);
826 f75375_write_pwm(client, nr);
831 static int f75375_probe(struct i2c_client *client,
832 const struct i2c_device_id *id)
834 struct f75375_data *data;
835 struct f75375s_platform_data *f75375s_pdata =
836 dev_get_platdata(&client->dev);
837 int err;
839 if (!i2c_check_functionality(client->adapter,
840 I2C_FUNC_SMBUS_BYTE_DATA))
841 return -EIO;
842 data = devm_kzalloc(&client->dev, sizeof(struct f75375_data),
843 GFP_KERNEL);
844 if (!data)
845 return -ENOMEM;
847 i2c_set_clientdata(client, data);
848 mutex_init(&data->update_lock);
849 data->kind = id->driver_data;
851 err = sysfs_create_group(&client->dev.kobj, &f75375_group);
852 if (err)
853 return err;
855 if (data->kind != f75373) {
856 err = sysfs_chmod_file(&client->dev.kobj,
857 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
858 S_IRUGO | S_IWUSR);
859 if (err)
860 goto exit_remove;
861 err = sysfs_chmod_file(&client->dev.kobj,
862 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
863 S_IRUGO | S_IWUSR);
864 if (err)
865 goto exit_remove;
868 data->hwmon_dev = hwmon_device_register(&client->dev);
869 if (IS_ERR(data->hwmon_dev)) {
870 err = PTR_ERR(data->hwmon_dev);
871 goto exit_remove;
874 f75375_init(client, data, f75375s_pdata);
876 return 0;
878 exit_remove:
879 sysfs_remove_group(&client->dev.kobj, &f75375_group);
880 return err;
883 static int f75375_remove(struct i2c_client *client)
885 struct f75375_data *data = i2c_get_clientdata(client);
886 hwmon_device_unregister(data->hwmon_dev);
887 sysfs_remove_group(&client->dev.kobj, &f75375_group);
888 return 0;
891 /* Return 0 if detection is successful, -ENODEV otherwise */
892 static int f75375_detect(struct i2c_client *client,
893 struct i2c_board_info *info)
895 struct i2c_adapter *adapter = client->adapter;
896 u16 vendid, chipid;
897 u8 version;
898 const char *name;
900 vendid = f75375_read16(client, F75375_REG_VENDOR);
901 chipid = f75375_read16(client, F75375_CHIP_ID);
902 if (vendid != 0x1934)
903 return -ENODEV;
905 if (chipid == 0x0306)
906 name = "f75375";
907 else if (chipid == 0x0204)
908 name = "f75373";
909 else if (chipid == 0x0410)
910 name = "f75387";
911 else
912 return -ENODEV;
914 version = f75375_read8(client, F75375_REG_VERSION);
915 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
916 strlcpy(info->type, name, I2C_NAME_SIZE);
918 return 0;
921 module_i2c_driver(f75375_driver);
923 MODULE_AUTHOR("Riku Voipio");
924 MODULE_LICENSE("GPL");
925 MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");