Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[pv_ops_mirror.git] / drivers / hwmon / adt7470.c
blob9810aaa0489d44619e677e6808fc920569a32b64
1 /*
2 * A hwmon driver for the Analog Devices ADT7470
3 * Copyright (C) 2007 IBM
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/log2.h>
32 /* Addresses to scan */
33 static unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
35 /* Insmod parameters */
36 I2C_CLIENT_INSMOD_1(adt7470);
38 /* ADT7470 registers */
39 #define ADT7470_REG_BASE_ADDR 0x20
40 #define ADT7470_REG_TEMP_BASE_ADDR 0x20
41 #define ADT7470_REG_TEMP_MAX_ADDR 0x29
42 #define ADT7470_REG_FAN_BASE_ADDR 0x2A
43 #define ADT7470_REG_FAN_MAX_ADDR 0x31
44 #define ADT7470_REG_PWM_BASE_ADDR 0x32
45 #define ADT7470_REG_PWM_MAX_ADDR 0x35
46 #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
47 #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
48 #define ADT7470_REG_CFG 0x40
49 #define ADT7470_FSPD_MASK 0x04
50 #define ADT7470_REG_ALARM1 0x41
51 #define ADT7470_REG_ALARM2 0x42
52 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
53 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
54 #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
55 #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
56 #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
57 #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
58 #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
59 #define ADT7470_REG_PWM12_CFG 0x68
60 #define ADT7470_PWM2_AUTO_MASK 0x40
61 #define ADT7470_PWM1_AUTO_MASK 0x80
62 #define ADT7470_REG_PWM34_CFG 0x69
63 #define ADT7470_PWM3_AUTO_MASK 0x40
64 #define ADT7470_PWM4_AUTO_MASK 0x80
65 #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
66 #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
67 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
68 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
69 #define ADT7470_REG_ACOUSTICS12 0x75
70 #define ADT7470_REG_ACOUSTICS34 0x76
71 #define ADT7470_REG_DEVICE 0x3D
72 #define ADT7470_REG_VENDOR 0x3E
73 #define ADT7470_REG_REVISION 0x3F
74 #define ADT7470_REG_ALARM1_MASK 0x72
75 #define ADT7470_REG_ALARM2_MASK 0x73
76 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
77 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
78 #define ADT7470_REG_MAX_ADDR 0x81
80 #define ADT7470_TEMP_COUNT 10
81 #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
82 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
83 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
84 ((x) * 2) + 1)
86 #define ADT7470_FAN_COUNT 4
87 #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
88 #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
89 #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
91 #define ADT7470_PWM_COUNT 4
92 #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
93 #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
94 #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
95 #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
96 #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
97 #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
98 ((x) / 2))
100 #define ADT7470_VENDOR 0x41
101 #define ADT7470_DEVICE 0x70
102 /* datasheet only mentions a revision 2 */
103 #define ADT7470_REVISION 0x02
105 /* "all temps" according to hwmon sysfs interface spec */
106 #define ADT7470_PWM_ALL_TEMPS 0x3FF
108 /* How often do we reread sensors values? (In jiffies) */
109 #define SENSOR_REFRESH_INTERVAL (5 * HZ)
111 /* How often do we reread sensor limit values? (In jiffies) */
112 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
114 /* sleep 1s while gathering temperature data */
115 #define TEMP_COLLECTION_TIME 1000
117 #define power_of_2(x) (((x) & ((x) - 1)) == 0)
119 /* datasheet says to divide this number by the fan reading to get fan rpm */
120 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
121 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
122 #define FAN_PERIOD_INVALID 65535
123 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
125 struct adt7470_data {
126 struct i2c_client client;
127 struct device *hwmon_dev;
128 struct attribute_group attrs;
129 struct mutex lock;
130 char sensors_valid;
131 char limits_valid;
132 unsigned long sensors_last_updated; /* In jiffies */
133 unsigned long limits_last_updated; /* In jiffies */
135 s8 temp[ADT7470_TEMP_COUNT];
136 s8 temp_min[ADT7470_TEMP_COUNT];
137 s8 temp_max[ADT7470_TEMP_COUNT];
138 u16 fan[ADT7470_FAN_COUNT];
139 u16 fan_min[ADT7470_FAN_COUNT];
140 u16 fan_max[ADT7470_FAN_COUNT];
141 u16 alarms, alarms_mask;
142 u8 force_pwm_max;
143 u8 pwm[ADT7470_PWM_COUNT];
144 u8 pwm_max[ADT7470_PWM_COUNT];
145 u8 pwm_automatic[ADT7470_PWM_COUNT];
146 u8 pwm_min[ADT7470_PWM_COUNT];
147 s8 pwm_tmin[ADT7470_PWM_COUNT];
148 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
151 static int adt7470_attach_adapter(struct i2c_adapter *adapter);
152 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind);
153 static int adt7470_detach_client(struct i2c_client *client);
155 static struct i2c_driver adt7470_driver = {
156 .driver = {
157 .name = "adt7470",
159 .attach_adapter = adt7470_attach_adapter,
160 .detach_client = adt7470_detach_client,
164 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
165 * that the low byte must be read before the high byte.
167 static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
169 u16 foo;
170 foo = i2c_smbus_read_byte_data(client, reg);
171 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
172 return foo;
175 static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
176 u16 value)
178 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
179 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
182 static void adt7470_init_client(struct i2c_client *client)
184 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
186 if (reg < 0) {
187 dev_err(&client->dev, "cannot read configuration register\n");
188 } else {
189 /* start monitoring (and do a self-test) */
190 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
194 static struct adt7470_data *adt7470_update_device(struct device *dev)
196 struct i2c_client *client = to_i2c_client(dev);
197 struct adt7470_data *data = i2c_get_clientdata(client);
198 unsigned long local_jiffies = jiffies;
199 u8 cfg;
200 int i;
202 mutex_lock(&data->lock);
203 if (time_before(local_jiffies, data->sensors_last_updated +
204 SENSOR_REFRESH_INTERVAL)
205 && data->sensors_valid)
206 goto no_sensor_update;
208 /* start reading temperature sensors */
209 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
210 cfg |= 0x80;
211 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
214 * Delay is 200ms * number of tmp05 sensors. Too bad
215 * there's no way to figure out how many are connected.
216 * For now, assume 1s will work.
218 msleep(TEMP_COLLECTION_TIME);
220 /* done reading temperature sensors */
221 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
222 cfg &= ~0x80;
223 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
225 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
226 data->temp[i] = i2c_smbus_read_byte_data(client,
227 ADT7470_TEMP_REG(i));
229 for (i = 0; i < ADT7470_FAN_COUNT; i++)
230 data->fan[i] = adt7470_read_word_data(client,
231 ADT7470_REG_FAN(i));
233 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
234 int reg;
235 int reg_mask;
237 data->pwm[i] = i2c_smbus_read_byte_data(client,
238 ADT7470_REG_PWM(i));
240 if (i % 2)
241 reg_mask = ADT7470_PWM2_AUTO_MASK;
242 else
243 reg_mask = ADT7470_PWM1_AUTO_MASK;
245 reg = ADT7470_REG_PWM_CFG(i);
246 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
247 data->pwm_automatic[i] = 1;
248 else
249 data->pwm_automatic[i] = 0;
251 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
252 cfg = i2c_smbus_read_byte_data(client, reg);
253 if (!(i % 2))
254 data->pwm_auto_temp[i] = cfg >> 4;
255 else
256 data->pwm_auto_temp[i] = cfg & 0xF;
259 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
260 ADT7470_FSPD_MASK)
261 data->force_pwm_max = 1;
262 else
263 data->force_pwm_max = 0;
265 data->alarms = adt7470_read_word_data(client, ADT7470_REG_ALARM1);
266 data->alarms_mask = adt7470_read_word_data(client,
267 ADT7470_REG_ALARM1_MASK);
269 data->sensors_last_updated = local_jiffies;
270 data->sensors_valid = 1;
272 no_sensor_update:
273 if (time_before(local_jiffies, data->limits_last_updated +
274 LIMIT_REFRESH_INTERVAL)
275 && data->limits_valid)
276 goto out;
278 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
279 data->temp_min[i] = i2c_smbus_read_byte_data(client,
280 ADT7470_TEMP_MIN_REG(i));
281 data->temp_max[i] = i2c_smbus_read_byte_data(client,
282 ADT7470_TEMP_MAX_REG(i));
285 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
286 data->fan_min[i] = adt7470_read_word_data(client,
287 ADT7470_REG_FAN_MIN(i));
288 data->fan_max[i] = adt7470_read_word_data(client,
289 ADT7470_REG_FAN_MAX(i));
292 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
293 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
294 ADT7470_REG_PWM_MAX(i));
295 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
296 ADT7470_REG_PWM_MIN(i));
297 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
298 ADT7470_REG_PWM_TMIN(i));
301 data->limits_last_updated = local_jiffies;
302 data->limits_valid = 1;
304 out:
305 mutex_unlock(&data->lock);
306 return data;
309 static ssize_t show_temp_min(struct device *dev,
310 struct device_attribute *devattr,
311 char *buf)
313 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
314 struct adt7470_data *data = adt7470_update_device(dev);
315 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
318 static ssize_t set_temp_min(struct device *dev,
319 struct device_attribute *devattr,
320 const char *buf,
321 size_t count)
323 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
324 struct i2c_client *client = to_i2c_client(dev);
325 struct adt7470_data *data = i2c_get_clientdata(client);
326 int temp = simple_strtol(buf, NULL, 10) / 1000;
328 mutex_lock(&data->lock);
329 data->temp_min[attr->index] = temp;
330 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
331 temp);
332 mutex_unlock(&data->lock);
334 return count;
337 static ssize_t show_temp_max(struct device *dev,
338 struct device_attribute *devattr,
339 char *buf)
341 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
342 struct adt7470_data *data = adt7470_update_device(dev);
343 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
346 static ssize_t set_temp_max(struct device *dev,
347 struct device_attribute *devattr,
348 const char *buf,
349 size_t count)
351 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
352 struct i2c_client *client = to_i2c_client(dev);
353 struct adt7470_data *data = i2c_get_clientdata(client);
354 int temp = simple_strtol(buf, NULL, 10) / 1000;
356 mutex_lock(&data->lock);
357 data->temp_max[attr->index] = temp;
358 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
359 temp);
360 mutex_unlock(&data->lock);
362 return count;
365 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
366 char *buf)
368 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
369 struct adt7470_data *data = adt7470_update_device(dev);
370 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
373 static ssize_t show_alarms(struct device *dev,
374 struct device_attribute *devattr,
375 char *buf)
377 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
378 struct adt7470_data *data = adt7470_update_device(dev);
380 if (attr->index)
381 return sprintf(buf, "%x\n", data->alarms);
382 else
383 return sprintf(buf, "%x\n", data->alarms_mask);
386 static ssize_t show_fan_max(struct device *dev,
387 struct device_attribute *devattr,
388 char *buf)
390 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
391 struct adt7470_data *data = adt7470_update_device(dev);
393 if (FAN_DATA_VALID(data->fan_max[attr->index]))
394 return sprintf(buf, "%d\n",
395 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
396 else
397 return sprintf(buf, "0\n");
400 static ssize_t set_fan_max(struct device *dev,
401 struct device_attribute *devattr,
402 const char *buf, size_t count)
404 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
405 struct i2c_client *client = to_i2c_client(dev);
406 struct adt7470_data *data = i2c_get_clientdata(client);
407 int temp = simple_strtol(buf, NULL, 10);
409 if (!temp)
410 return -EINVAL;
411 temp = FAN_RPM_TO_PERIOD(temp);
413 mutex_lock(&data->lock);
414 data->fan_max[attr->index] = temp;
415 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
416 mutex_unlock(&data->lock);
418 return count;
421 static ssize_t show_fan_min(struct device *dev,
422 struct device_attribute *devattr,
423 char *buf)
425 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
426 struct adt7470_data *data = adt7470_update_device(dev);
428 if (FAN_DATA_VALID(data->fan_min[attr->index]))
429 return sprintf(buf, "%d\n",
430 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
431 else
432 return sprintf(buf, "0\n");
435 static ssize_t set_fan_min(struct device *dev,
436 struct device_attribute *devattr,
437 const char *buf, size_t count)
439 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
440 struct i2c_client *client = to_i2c_client(dev);
441 struct adt7470_data *data = i2c_get_clientdata(client);
442 int temp = simple_strtol(buf, NULL, 10);
444 if (!temp)
445 return -EINVAL;
446 temp = FAN_RPM_TO_PERIOD(temp);
448 mutex_lock(&data->lock);
449 data->fan_min[attr->index] = temp;
450 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
451 mutex_unlock(&data->lock);
453 return count;
456 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
457 char *buf)
459 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
460 struct adt7470_data *data = adt7470_update_device(dev);
462 if (FAN_DATA_VALID(data->fan[attr->index]))
463 return sprintf(buf, "%d\n",
464 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
465 else
466 return sprintf(buf, "0\n");
469 static ssize_t show_force_pwm_max(struct device *dev,
470 struct device_attribute *devattr,
471 char *buf)
473 struct adt7470_data *data = adt7470_update_device(dev);
474 return sprintf(buf, "%d\n", data->force_pwm_max);
477 static ssize_t set_force_pwm_max(struct device *dev,
478 struct device_attribute *devattr,
479 const char *buf,
480 size_t count)
482 struct i2c_client *client = to_i2c_client(dev);
483 struct adt7470_data *data = i2c_get_clientdata(client);
484 int temp = simple_strtol(buf, NULL, 10);
485 u8 reg;
487 mutex_lock(&data->lock);
488 data->force_pwm_max = temp;
489 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
490 if (temp)
491 reg |= ADT7470_FSPD_MASK;
492 else
493 reg &= ~ADT7470_FSPD_MASK;
494 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
495 mutex_unlock(&data->lock);
497 return count;
500 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
501 char *buf)
503 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
504 struct adt7470_data *data = adt7470_update_device(dev);
505 return sprintf(buf, "%d\n", data->pwm[attr->index]);
508 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
509 const char *buf, size_t count)
511 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
512 struct i2c_client *client = to_i2c_client(dev);
513 struct adt7470_data *data = i2c_get_clientdata(client);
514 int temp = simple_strtol(buf, NULL, 10);
516 mutex_lock(&data->lock);
517 data->pwm[attr->index] = temp;
518 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
519 mutex_unlock(&data->lock);
521 return count;
524 static ssize_t show_pwm_max(struct device *dev,
525 struct device_attribute *devattr,
526 char *buf)
528 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
529 struct adt7470_data *data = adt7470_update_device(dev);
530 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
533 static ssize_t set_pwm_max(struct device *dev,
534 struct device_attribute *devattr,
535 const char *buf,
536 size_t count)
538 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
539 struct i2c_client *client = to_i2c_client(dev);
540 struct adt7470_data *data = i2c_get_clientdata(client);
541 int temp = simple_strtol(buf, NULL, 10);
543 mutex_lock(&data->lock);
544 data->pwm_max[attr->index] = temp;
545 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
546 temp);
547 mutex_unlock(&data->lock);
549 return count;
552 static ssize_t show_pwm_min(struct device *dev,
553 struct device_attribute *devattr,
554 char *buf)
556 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
557 struct adt7470_data *data = adt7470_update_device(dev);
558 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
561 static ssize_t set_pwm_min(struct device *dev,
562 struct device_attribute *devattr,
563 const char *buf,
564 size_t count)
566 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
567 struct i2c_client *client = to_i2c_client(dev);
568 struct adt7470_data *data = i2c_get_clientdata(client);
569 int temp = simple_strtol(buf, NULL, 10);
571 mutex_lock(&data->lock);
572 data->pwm_min[attr->index] = temp;
573 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
574 temp);
575 mutex_unlock(&data->lock);
577 return count;
580 static ssize_t show_pwm_tmax(struct device *dev,
581 struct device_attribute *devattr,
582 char *buf)
584 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
585 struct adt7470_data *data = adt7470_update_device(dev);
586 /* the datasheet says that tmax = tmin + 20C */
587 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
590 static ssize_t show_pwm_tmin(struct device *dev,
591 struct device_attribute *devattr,
592 char *buf)
594 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
595 struct adt7470_data *data = adt7470_update_device(dev);
596 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
599 static ssize_t set_pwm_tmin(struct device *dev,
600 struct device_attribute *devattr,
601 const char *buf,
602 size_t count)
604 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
605 struct i2c_client *client = to_i2c_client(dev);
606 struct adt7470_data *data = i2c_get_clientdata(client);
607 int temp = simple_strtol(buf, NULL, 10) / 1000;
609 mutex_lock(&data->lock);
610 data->pwm_tmin[attr->index] = temp;
611 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
612 temp);
613 mutex_unlock(&data->lock);
615 return count;
618 static ssize_t show_pwm_auto(struct device *dev,
619 struct device_attribute *devattr,
620 char *buf)
622 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
623 struct adt7470_data *data = adt7470_update_device(dev);
624 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
627 static ssize_t set_pwm_auto(struct device *dev,
628 struct device_attribute *devattr,
629 const char *buf,
630 size_t count)
632 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
633 struct i2c_client *client = to_i2c_client(dev);
634 struct adt7470_data *data = i2c_get_clientdata(client);
635 int temp = simple_strtol(buf, NULL, 10);
636 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
637 int pwm_auto_reg_mask;
638 u8 reg;
640 if (attr->index % 2)
641 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
642 else
643 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
645 if (temp != 2 && temp != 1)
646 return -EINVAL;
647 temp--;
649 mutex_lock(&data->lock);
650 data->pwm_automatic[attr->index] = temp;
651 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
652 if (temp)
653 reg |= pwm_auto_reg_mask;
654 else
655 reg &= ~pwm_auto_reg_mask;
656 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
657 mutex_unlock(&data->lock);
659 return count;
662 static ssize_t show_pwm_auto_temp(struct device *dev,
663 struct device_attribute *devattr,
664 char *buf)
666 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
667 struct adt7470_data *data = adt7470_update_device(dev);
668 u8 ctrl = data->pwm_auto_temp[attr->index];
670 if (ctrl)
671 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
672 else
673 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
676 static int cvt_auto_temp(int input)
678 if (input == ADT7470_PWM_ALL_TEMPS)
679 return 0;
680 if (input < 1 || !power_of_2(input))
681 return -EINVAL;
682 return ilog2(input) + 1;
685 static ssize_t set_pwm_auto_temp(struct device *dev,
686 struct device_attribute *devattr,
687 const char *buf,
688 size_t count)
690 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
691 struct i2c_client *client = to_i2c_client(dev);
692 struct adt7470_data *data = i2c_get_clientdata(client);
693 int temp = cvt_auto_temp(simple_strtol(buf, NULL, 10));
694 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
695 u8 reg;
697 if (temp < 0)
698 return temp;
700 mutex_lock(&data->lock);
701 data->pwm_automatic[attr->index] = temp;
702 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
704 if (!(attr->index % 2)) {
705 reg &= 0xF;
706 reg |= (temp << 4) & 0xF0;
707 } else {
708 reg &= 0xF0;
709 reg |= temp & 0xF;
712 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
713 mutex_unlock(&data->lock);
715 return count;
718 static SENSOR_DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL, 0);
719 static SENSOR_DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarms, NULL, 1);
721 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
722 set_temp_max, 0);
723 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
724 set_temp_max, 1);
725 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
726 set_temp_max, 2);
727 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
728 set_temp_max, 3);
729 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
730 set_temp_max, 4);
731 static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
732 set_temp_max, 5);
733 static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
734 set_temp_max, 6);
735 static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
736 set_temp_max, 7);
737 static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
738 set_temp_max, 8);
739 static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
740 set_temp_max, 9);
742 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
743 set_temp_min, 0);
744 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
745 set_temp_min, 1);
746 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
747 set_temp_min, 2);
748 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
749 set_temp_min, 3);
750 static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
751 set_temp_min, 4);
752 static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
753 set_temp_min, 5);
754 static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
755 set_temp_min, 6);
756 static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
757 set_temp_min, 7);
758 static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
759 set_temp_min, 8);
760 static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
761 set_temp_min, 9);
763 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
764 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
765 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
766 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
767 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
768 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
769 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
770 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
771 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
772 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
774 static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
775 set_fan_max, 0);
776 static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
777 set_fan_max, 1);
778 static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
779 set_fan_max, 2);
780 static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
781 set_fan_max, 3);
783 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
784 set_fan_min, 0);
785 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
786 set_fan_min, 1);
787 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
788 set_fan_min, 2);
789 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
790 set_fan_min, 3);
792 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
793 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
794 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
795 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
797 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
798 show_force_pwm_max, set_force_pwm_max, 0);
800 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
801 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
802 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
803 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
805 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
806 show_pwm_min, set_pwm_min, 0);
807 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
808 show_pwm_min, set_pwm_min, 1);
809 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
810 show_pwm_min, set_pwm_min, 2);
811 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
812 show_pwm_min, set_pwm_min, 3);
814 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
815 show_pwm_max, set_pwm_max, 0);
816 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
817 show_pwm_max, set_pwm_max, 1);
818 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
819 show_pwm_max, set_pwm_max, 2);
820 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
821 show_pwm_max, set_pwm_max, 3);
823 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
824 show_pwm_tmin, set_pwm_tmin, 0);
825 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
826 show_pwm_tmin, set_pwm_tmin, 1);
827 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
828 show_pwm_tmin, set_pwm_tmin, 2);
829 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
830 show_pwm_tmin, set_pwm_tmin, 3);
832 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
833 NULL, 0);
834 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
835 NULL, 1);
836 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
837 NULL, 2);
838 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
839 NULL, 3);
841 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
842 set_pwm_auto, 0);
843 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
844 set_pwm_auto, 1);
845 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
846 set_pwm_auto, 2);
847 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
848 set_pwm_auto, 3);
850 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
851 show_pwm_auto_temp, set_pwm_auto_temp, 0);
852 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
853 show_pwm_auto_temp, set_pwm_auto_temp, 1);
854 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
855 show_pwm_auto_temp, set_pwm_auto_temp, 2);
856 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
857 show_pwm_auto_temp, set_pwm_auto_temp, 3);
859 static struct attribute *adt7470_attr[] =
861 &sensor_dev_attr_alarms.dev_attr.attr,
862 &sensor_dev_attr_alarm_mask.dev_attr.attr,
863 &sensor_dev_attr_temp1_max.dev_attr.attr,
864 &sensor_dev_attr_temp2_max.dev_attr.attr,
865 &sensor_dev_attr_temp3_max.dev_attr.attr,
866 &sensor_dev_attr_temp4_max.dev_attr.attr,
867 &sensor_dev_attr_temp5_max.dev_attr.attr,
868 &sensor_dev_attr_temp6_max.dev_attr.attr,
869 &sensor_dev_attr_temp7_max.dev_attr.attr,
870 &sensor_dev_attr_temp8_max.dev_attr.attr,
871 &sensor_dev_attr_temp9_max.dev_attr.attr,
872 &sensor_dev_attr_temp10_max.dev_attr.attr,
873 &sensor_dev_attr_temp1_min.dev_attr.attr,
874 &sensor_dev_attr_temp2_min.dev_attr.attr,
875 &sensor_dev_attr_temp3_min.dev_attr.attr,
876 &sensor_dev_attr_temp4_min.dev_attr.attr,
877 &sensor_dev_attr_temp5_min.dev_attr.attr,
878 &sensor_dev_attr_temp6_min.dev_attr.attr,
879 &sensor_dev_attr_temp7_min.dev_attr.attr,
880 &sensor_dev_attr_temp8_min.dev_attr.attr,
881 &sensor_dev_attr_temp9_min.dev_attr.attr,
882 &sensor_dev_attr_temp10_min.dev_attr.attr,
883 &sensor_dev_attr_temp1_input.dev_attr.attr,
884 &sensor_dev_attr_temp2_input.dev_attr.attr,
885 &sensor_dev_attr_temp3_input.dev_attr.attr,
886 &sensor_dev_attr_temp4_input.dev_attr.attr,
887 &sensor_dev_attr_temp5_input.dev_attr.attr,
888 &sensor_dev_attr_temp6_input.dev_attr.attr,
889 &sensor_dev_attr_temp7_input.dev_attr.attr,
890 &sensor_dev_attr_temp8_input.dev_attr.attr,
891 &sensor_dev_attr_temp9_input.dev_attr.attr,
892 &sensor_dev_attr_temp10_input.dev_attr.attr,
893 &sensor_dev_attr_fan1_max.dev_attr.attr,
894 &sensor_dev_attr_fan2_max.dev_attr.attr,
895 &sensor_dev_attr_fan3_max.dev_attr.attr,
896 &sensor_dev_attr_fan4_max.dev_attr.attr,
897 &sensor_dev_attr_fan1_min.dev_attr.attr,
898 &sensor_dev_attr_fan2_min.dev_attr.attr,
899 &sensor_dev_attr_fan3_min.dev_attr.attr,
900 &sensor_dev_attr_fan4_min.dev_attr.attr,
901 &sensor_dev_attr_fan1_input.dev_attr.attr,
902 &sensor_dev_attr_fan2_input.dev_attr.attr,
903 &sensor_dev_attr_fan3_input.dev_attr.attr,
904 &sensor_dev_attr_fan4_input.dev_attr.attr,
905 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
906 &sensor_dev_attr_pwm1.dev_attr.attr,
907 &sensor_dev_attr_pwm2.dev_attr.attr,
908 &sensor_dev_attr_pwm3.dev_attr.attr,
909 &sensor_dev_attr_pwm4.dev_attr.attr,
910 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
911 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
912 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
913 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
914 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
915 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
916 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
917 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
918 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
919 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
920 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
921 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
922 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
923 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
924 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
925 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
926 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
927 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
928 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
929 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
930 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
931 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
932 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
933 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
934 NULL
937 static int adt7470_attach_adapter(struct i2c_adapter *adapter)
939 if (!(adapter->class & I2C_CLASS_HWMON))
940 return 0;
941 return i2c_probe(adapter, &addr_data, adt7470_detect);
944 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind)
946 struct i2c_client *client;
947 struct adt7470_data *data;
948 int err = 0;
950 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
951 goto exit;
953 if (!(data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL))) {
954 err = -ENOMEM;
955 goto exit;
958 client = &data->client;
959 client->addr = address;
960 client->adapter = adapter;
961 client->driver = &adt7470_driver;
963 i2c_set_clientdata(client, data);
965 mutex_init(&data->lock);
967 if (kind <= 0) {
968 int vendor, device, revision;
970 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
971 if (vendor != ADT7470_VENDOR) {
972 err = -ENODEV;
973 goto exit_free;
976 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
977 if (device != ADT7470_DEVICE) {
978 err = -ENODEV;
979 goto exit_free;
982 revision = i2c_smbus_read_byte_data(client,
983 ADT7470_REG_REVISION);
984 if (revision != ADT7470_REVISION) {
985 err = -ENODEV;
986 goto exit_free;
988 } else
989 dev_dbg(&adapter->dev, "detection forced\n");
991 strlcpy(client->name, "adt7470", I2C_NAME_SIZE);
993 if ((err = i2c_attach_client(client)))
994 goto exit_free;
996 dev_info(&client->dev, "%s chip found\n", client->name);
998 /* Initialize the ADT7470 chip */
999 adt7470_init_client(client);
1001 /* Register sysfs hooks */
1002 data->attrs.attrs = adt7470_attr;
1003 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
1004 goto exit_detach;
1006 data->hwmon_dev = hwmon_device_register(&client->dev);
1007 if (IS_ERR(data->hwmon_dev)) {
1008 err = PTR_ERR(data->hwmon_dev);
1009 goto exit_remove;
1012 return 0;
1014 exit_remove:
1015 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1016 exit_detach:
1017 i2c_detach_client(client);
1018 exit_free:
1019 kfree(data);
1020 exit:
1021 return err;
1024 static int adt7470_detach_client(struct i2c_client *client)
1026 struct adt7470_data *data = i2c_get_clientdata(client);
1028 hwmon_device_unregister(data->hwmon_dev);
1029 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1030 i2c_detach_client(client);
1031 kfree(data);
1032 return 0;
1035 static int __init adt7470_init(void)
1037 return i2c_add_driver(&adt7470_driver);
1040 static void __exit adt7470_exit(void)
1042 i2c_del_driver(&adt7470_driver);
1045 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1046 MODULE_DESCRIPTION("ADT7470 driver");
1047 MODULE_LICENSE("GPL");
1049 module_init(adt7470_init);
1050 module_exit(adt7470_exit);