Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / adt7470.c
blob05c58e59d22fb8ca59596486ad40b4c3c5dc1311
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 <<<<<<< HEAD:drivers/hwmon/adt7470.c
34 static unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
35 =======
36 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
37 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/adt7470.c
39 /* Insmod parameters */
40 I2C_CLIENT_INSMOD_1(adt7470);
42 /* ADT7470 registers */
43 #define ADT7470_REG_BASE_ADDR 0x20
44 #define ADT7470_REG_TEMP_BASE_ADDR 0x20
45 #define ADT7470_REG_TEMP_MAX_ADDR 0x29
46 #define ADT7470_REG_FAN_BASE_ADDR 0x2A
47 #define ADT7470_REG_FAN_MAX_ADDR 0x31
48 #define ADT7470_REG_PWM_BASE_ADDR 0x32
49 #define ADT7470_REG_PWM_MAX_ADDR 0x35
50 #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
51 #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
52 #define ADT7470_REG_CFG 0x40
53 #define ADT7470_FSPD_MASK 0x04
54 #define ADT7470_REG_ALARM1 0x41
55 #define ADT7470_R1T_ALARM 0x01
56 #define ADT7470_R2T_ALARM 0x02
57 #define ADT7470_R3T_ALARM 0x04
58 #define ADT7470_R4T_ALARM 0x08
59 #define ADT7470_R5T_ALARM 0x10
60 #define ADT7470_R6T_ALARM 0x20
61 #define ADT7470_R7T_ALARM 0x40
62 #define ADT7470_OOL_ALARM 0x80
63 #define ADT7470_REG_ALARM2 0x42
64 #define ADT7470_R8T_ALARM 0x01
65 #define ADT7470_R9T_ALARM 0x02
66 #define ADT7470_R10T_ALARM 0x04
67 #define ADT7470_FAN1_ALARM 0x10
68 #define ADT7470_FAN2_ALARM 0x20
69 #define ADT7470_FAN3_ALARM 0x40
70 #define ADT7470_FAN4_ALARM 0x80
71 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
72 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
73 #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
74 #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
75 #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
76 #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
77 #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
78 #define ADT7470_REG_PWM12_CFG 0x68
79 #define ADT7470_PWM2_AUTO_MASK 0x40
80 #define ADT7470_PWM1_AUTO_MASK 0x80
81 #define ADT7470_REG_PWM34_CFG 0x69
82 #define ADT7470_PWM3_AUTO_MASK 0x40
83 #define ADT7470_PWM4_AUTO_MASK 0x80
84 #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
85 #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
86 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
87 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
88 #define ADT7470_REG_ACOUSTICS12 0x75
89 #define ADT7470_REG_ACOUSTICS34 0x76
90 #define ADT7470_REG_DEVICE 0x3D
91 #define ADT7470_REG_VENDOR 0x3E
92 #define ADT7470_REG_REVISION 0x3F
93 #define ADT7470_REG_ALARM1_MASK 0x72
94 #define ADT7470_REG_ALARM2_MASK 0x73
95 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
96 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
97 #define ADT7470_REG_MAX_ADDR 0x81
99 #define ADT7470_TEMP_COUNT 10
100 #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
101 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
102 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
103 ((x) * 2) + 1)
105 #define ADT7470_FAN_COUNT 4
106 #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
107 #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
108 #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
110 #define ADT7470_PWM_COUNT 4
111 #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
112 #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
113 #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
114 #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
115 #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
116 #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
117 ((x) / 2))
119 #define ALARM2(x) ((x) << 8)
121 #define ADT7470_VENDOR 0x41
122 #define ADT7470_DEVICE 0x70
123 /* datasheet only mentions a revision 2 */
124 #define ADT7470_REVISION 0x02
126 /* "all temps" according to hwmon sysfs interface spec */
127 #define ADT7470_PWM_ALL_TEMPS 0x3FF
129 /* How often do we reread sensors values? (In jiffies) */
130 #define SENSOR_REFRESH_INTERVAL (5 * HZ)
132 /* How often do we reread sensor limit values? (In jiffies) */
133 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
135 /* sleep 1s while gathering temperature data */
136 #define TEMP_COLLECTION_TIME 1000
138 /* datasheet says to divide this number by the fan reading to get fan rpm */
139 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
140 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
141 #define FAN_PERIOD_INVALID 65535
142 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
144 struct adt7470_data {
145 struct i2c_client client;
146 struct device *hwmon_dev;
147 struct attribute_group attrs;
148 struct mutex lock;
149 char sensors_valid;
150 char limits_valid;
151 unsigned long sensors_last_updated; /* In jiffies */
152 unsigned long limits_last_updated; /* In jiffies */
154 s8 temp[ADT7470_TEMP_COUNT];
155 s8 temp_min[ADT7470_TEMP_COUNT];
156 s8 temp_max[ADT7470_TEMP_COUNT];
157 u16 fan[ADT7470_FAN_COUNT];
158 u16 fan_min[ADT7470_FAN_COUNT];
159 u16 fan_max[ADT7470_FAN_COUNT];
160 u16 alarm;
161 u16 alarms_mask;
162 u8 force_pwm_max;
163 u8 pwm[ADT7470_PWM_COUNT];
164 u8 pwm_max[ADT7470_PWM_COUNT];
165 u8 pwm_automatic[ADT7470_PWM_COUNT];
166 u8 pwm_min[ADT7470_PWM_COUNT];
167 s8 pwm_tmin[ADT7470_PWM_COUNT];
168 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
171 static int adt7470_attach_adapter(struct i2c_adapter *adapter);
172 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind);
173 static int adt7470_detach_client(struct i2c_client *client);
175 static struct i2c_driver adt7470_driver = {
176 .driver = {
177 .name = "adt7470",
179 .attach_adapter = adt7470_attach_adapter,
180 .detach_client = adt7470_detach_client,
184 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
185 * that the low byte must be read before the high byte.
187 static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
189 u16 foo;
190 foo = i2c_smbus_read_byte_data(client, reg);
191 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
192 return foo;
195 static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
196 u16 value)
198 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
199 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
202 static void adt7470_init_client(struct i2c_client *client)
204 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
206 if (reg < 0) {
207 dev_err(&client->dev, "cannot read configuration register\n");
208 } else {
209 /* start monitoring (and do a self-test) */
210 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
214 static struct adt7470_data *adt7470_update_device(struct device *dev)
216 struct i2c_client *client = to_i2c_client(dev);
217 struct adt7470_data *data = i2c_get_clientdata(client);
218 unsigned long local_jiffies = jiffies;
219 u8 cfg;
220 int i;
222 mutex_lock(&data->lock);
223 if (time_before(local_jiffies, data->sensors_last_updated +
224 SENSOR_REFRESH_INTERVAL)
225 && data->sensors_valid)
226 goto no_sensor_update;
228 /* start reading temperature sensors */
229 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
230 cfg |= 0x80;
231 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
234 * Delay is 200ms * number of tmp05 sensors. Too bad
235 * there's no way to figure out how many are connected.
236 * For now, assume 1s will work.
238 msleep(TEMP_COLLECTION_TIME);
240 /* done reading temperature sensors */
241 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
242 cfg &= ~0x80;
243 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
245 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
246 data->temp[i] = i2c_smbus_read_byte_data(client,
247 ADT7470_TEMP_REG(i));
249 for (i = 0; i < ADT7470_FAN_COUNT; i++)
250 data->fan[i] = adt7470_read_word_data(client,
251 ADT7470_REG_FAN(i));
253 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
254 int reg;
255 int reg_mask;
257 data->pwm[i] = i2c_smbus_read_byte_data(client,
258 ADT7470_REG_PWM(i));
260 if (i % 2)
261 reg_mask = ADT7470_PWM2_AUTO_MASK;
262 else
263 reg_mask = ADT7470_PWM1_AUTO_MASK;
265 reg = ADT7470_REG_PWM_CFG(i);
266 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
267 data->pwm_automatic[i] = 1;
268 else
269 data->pwm_automatic[i] = 0;
271 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
272 cfg = i2c_smbus_read_byte_data(client, reg);
273 if (!(i % 2))
274 data->pwm_auto_temp[i] = cfg >> 4;
275 else
276 data->pwm_auto_temp[i] = cfg & 0xF;
279 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
280 ADT7470_FSPD_MASK)
281 data->force_pwm_max = 1;
282 else
283 data->force_pwm_max = 0;
285 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
286 if (data->alarm & ADT7470_OOL_ALARM)
287 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
288 ADT7470_REG_ALARM2));
289 data->alarms_mask = adt7470_read_word_data(client,
290 ADT7470_REG_ALARM1_MASK);
292 data->sensors_last_updated = local_jiffies;
293 data->sensors_valid = 1;
295 no_sensor_update:
296 if (time_before(local_jiffies, data->limits_last_updated +
297 LIMIT_REFRESH_INTERVAL)
298 && data->limits_valid)
299 goto out;
301 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
302 data->temp_min[i] = i2c_smbus_read_byte_data(client,
303 ADT7470_TEMP_MIN_REG(i));
304 data->temp_max[i] = i2c_smbus_read_byte_data(client,
305 ADT7470_TEMP_MAX_REG(i));
308 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
309 data->fan_min[i] = adt7470_read_word_data(client,
310 ADT7470_REG_FAN_MIN(i));
311 data->fan_max[i] = adt7470_read_word_data(client,
312 ADT7470_REG_FAN_MAX(i));
315 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
316 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
317 ADT7470_REG_PWM_MAX(i));
318 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
319 ADT7470_REG_PWM_MIN(i));
320 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
321 ADT7470_REG_PWM_TMIN(i));
324 data->limits_last_updated = local_jiffies;
325 data->limits_valid = 1;
327 out:
328 mutex_unlock(&data->lock);
329 return data;
332 static ssize_t show_temp_min(struct device *dev,
333 struct device_attribute *devattr,
334 char *buf)
336 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
337 struct adt7470_data *data = adt7470_update_device(dev);
338 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
341 static ssize_t set_temp_min(struct device *dev,
342 struct device_attribute *devattr,
343 const char *buf,
344 size_t count)
346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
347 struct i2c_client *client = to_i2c_client(dev);
348 struct adt7470_data *data = i2c_get_clientdata(client);
349 int temp = simple_strtol(buf, NULL, 10) / 1000;
351 mutex_lock(&data->lock);
352 data->temp_min[attr->index] = temp;
353 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
354 temp);
355 mutex_unlock(&data->lock);
357 return count;
360 static ssize_t show_temp_max(struct device *dev,
361 struct device_attribute *devattr,
362 char *buf)
364 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
365 struct adt7470_data *data = adt7470_update_device(dev);
366 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
369 static ssize_t set_temp_max(struct device *dev,
370 struct device_attribute *devattr,
371 const char *buf,
372 size_t count)
374 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
375 struct i2c_client *client = to_i2c_client(dev);
376 struct adt7470_data *data = i2c_get_clientdata(client);
377 int temp = simple_strtol(buf, NULL, 10) / 1000;
379 mutex_lock(&data->lock);
380 data->temp_max[attr->index] = temp;
381 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
382 temp);
383 mutex_unlock(&data->lock);
385 return count;
388 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
389 char *buf)
391 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
392 struct adt7470_data *data = adt7470_update_device(dev);
393 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
396 static ssize_t show_alarm_mask(struct device *dev,
397 struct device_attribute *devattr,
398 char *buf)
400 struct adt7470_data *data = adt7470_update_device(dev);
402 return sprintf(buf, "%x\n", data->alarms_mask);
405 static ssize_t show_fan_max(struct device *dev,
406 struct device_attribute *devattr,
407 char *buf)
409 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
410 struct adt7470_data *data = adt7470_update_device(dev);
412 if (FAN_DATA_VALID(data->fan_max[attr->index]))
413 return sprintf(buf, "%d\n",
414 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
415 else
416 return sprintf(buf, "0\n");
419 static ssize_t set_fan_max(struct device *dev,
420 struct device_attribute *devattr,
421 const char *buf, size_t count)
423 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
424 struct i2c_client *client = to_i2c_client(dev);
425 struct adt7470_data *data = i2c_get_clientdata(client);
426 int temp = simple_strtol(buf, NULL, 10);
428 if (!temp)
429 return -EINVAL;
430 temp = FAN_RPM_TO_PERIOD(temp);
432 mutex_lock(&data->lock);
433 data->fan_max[attr->index] = temp;
434 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
435 mutex_unlock(&data->lock);
437 return count;
440 static ssize_t show_fan_min(struct device *dev,
441 struct device_attribute *devattr,
442 char *buf)
444 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
445 struct adt7470_data *data = adt7470_update_device(dev);
447 if (FAN_DATA_VALID(data->fan_min[attr->index]))
448 return sprintf(buf, "%d\n",
449 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
450 else
451 return sprintf(buf, "0\n");
454 static ssize_t set_fan_min(struct device *dev,
455 struct device_attribute *devattr,
456 const char *buf, size_t count)
458 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
459 struct i2c_client *client = to_i2c_client(dev);
460 struct adt7470_data *data = i2c_get_clientdata(client);
461 int temp = simple_strtol(buf, NULL, 10);
463 if (!temp)
464 return -EINVAL;
465 temp = FAN_RPM_TO_PERIOD(temp);
467 mutex_lock(&data->lock);
468 data->fan_min[attr->index] = temp;
469 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
470 mutex_unlock(&data->lock);
472 return count;
475 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
476 char *buf)
478 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
479 struct adt7470_data *data = adt7470_update_device(dev);
481 if (FAN_DATA_VALID(data->fan[attr->index]))
482 return sprintf(buf, "%d\n",
483 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
484 else
485 return sprintf(buf, "0\n");
488 static ssize_t show_force_pwm_max(struct device *dev,
489 struct device_attribute *devattr,
490 char *buf)
492 struct adt7470_data *data = adt7470_update_device(dev);
493 return sprintf(buf, "%d\n", data->force_pwm_max);
496 static ssize_t set_force_pwm_max(struct device *dev,
497 struct device_attribute *devattr,
498 const char *buf,
499 size_t count)
501 struct i2c_client *client = to_i2c_client(dev);
502 struct adt7470_data *data = i2c_get_clientdata(client);
503 int temp = simple_strtol(buf, NULL, 10);
504 u8 reg;
506 mutex_lock(&data->lock);
507 data->force_pwm_max = temp;
508 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
509 if (temp)
510 reg |= ADT7470_FSPD_MASK;
511 else
512 reg &= ~ADT7470_FSPD_MASK;
513 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
514 mutex_unlock(&data->lock);
516 return count;
519 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
520 char *buf)
522 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
523 struct adt7470_data *data = adt7470_update_device(dev);
524 return sprintf(buf, "%d\n", data->pwm[attr->index]);
527 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
528 const char *buf, size_t count)
530 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
531 struct i2c_client *client = to_i2c_client(dev);
532 struct adt7470_data *data = i2c_get_clientdata(client);
533 int temp = simple_strtol(buf, NULL, 10);
535 mutex_lock(&data->lock);
536 data->pwm[attr->index] = temp;
537 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
538 mutex_unlock(&data->lock);
540 return count;
543 static ssize_t show_pwm_max(struct device *dev,
544 struct device_attribute *devattr,
545 char *buf)
547 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
548 struct adt7470_data *data = adt7470_update_device(dev);
549 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
552 static ssize_t set_pwm_max(struct device *dev,
553 struct device_attribute *devattr,
554 const char *buf,
555 size_t count)
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558 struct i2c_client *client = to_i2c_client(dev);
559 struct adt7470_data *data = i2c_get_clientdata(client);
560 int temp = simple_strtol(buf, NULL, 10);
562 mutex_lock(&data->lock);
563 data->pwm_max[attr->index] = temp;
564 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
565 temp);
566 mutex_unlock(&data->lock);
568 return count;
571 static ssize_t show_pwm_min(struct device *dev,
572 struct device_attribute *devattr,
573 char *buf)
575 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
576 struct adt7470_data *data = adt7470_update_device(dev);
577 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
580 static ssize_t set_pwm_min(struct device *dev,
581 struct device_attribute *devattr,
582 const char *buf,
583 size_t count)
585 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
586 struct i2c_client *client = to_i2c_client(dev);
587 struct adt7470_data *data = i2c_get_clientdata(client);
588 int temp = simple_strtol(buf, NULL, 10);
590 mutex_lock(&data->lock);
591 data->pwm_min[attr->index] = temp;
592 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
593 temp);
594 mutex_unlock(&data->lock);
596 return count;
599 static ssize_t show_pwm_tmax(struct device *dev,
600 struct device_attribute *devattr,
601 char *buf)
603 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
604 struct adt7470_data *data = adt7470_update_device(dev);
605 /* the datasheet says that tmax = tmin + 20C */
606 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
609 static ssize_t show_pwm_tmin(struct device *dev,
610 struct device_attribute *devattr,
611 char *buf)
613 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
614 struct adt7470_data *data = adt7470_update_device(dev);
615 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
618 static ssize_t set_pwm_tmin(struct device *dev,
619 struct device_attribute *devattr,
620 const char *buf,
621 size_t count)
623 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
624 struct i2c_client *client = to_i2c_client(dev);
625 struct adt7470_data *data = i2c_get_clientdata(client);
626 int temp = simple_strtol(buf, NULL, 10) / 1000;
628 mutex_lock(&data->lock);
629 data->pwm_tmin[attr->index] = temp;
630 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
631 temp);
632 mutex_unlock(&data->lock);
634 return count;
637 static ssize_t show_pwm_auto(struct device *dev,
638 struct device_attribute *devattr,
639 char *buf)
641 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
642 struct adt7470_data *data = adt7470_update_device(dev);
643 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
646 static ssize_t set_pwm_auto(struct device *dev,
647 struct device_attribute *devattr,
648 const char *buf,
649 size_t count)
651 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
652 struct i2c_client *client = to_i2c_client(dev);
653 struct adt7470_data *data = i2c_get_clientdata(client);
654 int temp = simple_strtol(buf, NULL, 10);
655 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
656 int pwm_auto_reg_mask;
657 u8 reg;
659 if (attr->index % 2)
660 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
661 else
662 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
664 if (temp != 2 && temp != 1)
665 return -EINVAL;
666 temp--;
668 mutex_lock(&data->lock);
669 data->pwm_automatic[attr->index] = temp;
670 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
671 if (temp)
672 reg |= pwm_auto_reg_mask;
673 else
674 reg &= ~pwm_auto_reg_mask;
675 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
676 mutex_unlock(&data->lock);
678 return count;
681 static ssize_t show_pwm_auto_temp(struct device *dev,
682 struct device_attribute *devattr,
683 char *buf)
685 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
686 struct adt7470_data *data = adt7470_update_device(dev);
687 u8 ctrl = data->pwm_auto_temp[attr->index];
689 if (ctrl)
690 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
691 else
692 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
695 static int cvt_auto_temp(int input)
697 if (input == ADT7470_PWM_ALL_TEMPS)
698 return 0;
699 if (input < 1 || !is_power_of_2(input))
700 return -EINVAL;
701 return ilog2(input) + 1;
704 static ssize_t set_pwm_auto_temp(struct device *dev,
705 struct device_attribute *devattr,
706 const char *buf,
707 size_t count)
709 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
710 struct i2c_client *client = to_i2c_client(dev);
711 struct adt7470_data *data = i2c_get_clientdata(client);
712 int temp = cvt_auto_temp(simple_strtol(buf, NULL, 10));
713 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
714 u8 reg;
716 if (temp < 0)
717 return temp;
719 mutex_lock(&data->lock);
720 data->pwm_automatic[attr->index] = temp;
721 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
723 if (!(attr->index % 2)) {
724 reg &= 0xF;
725 reg |= (temp << 4) & 0xF0;
726 } else {
727 reg &= 0xF0;
728 reg |= temp & 0xF;
731 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
732 mutex_unlock(&data->lock);
734 return count;
737 static ssize_t show_alarm(struct device *dev,
738 struct device_attribute *devattr,
739 char *buf)
741 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
742 struct adt7470_data *data = adt7470_update_device(dev);
744 if (data->alarm & attr->index)
745 return sprintf(buf, "1\n");
746 else
747 return sprintf(buf, "0\n");
750 static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
752 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
753 set_temp_max, 0);
754 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
755 set_temp_max, 1);
756 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
757 set_temp_max, 2);
758 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
759 set_temp_max, 3);
760 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
761 set_temp_max, 4);
762 static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
763 set_temp_max, 5);
764 static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
765 set_temp_max, 6);
766 static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
767 set_temp_max, 7);
768 static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
769 set_temp_max, 8);
770 static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
771 set_temp_max, 9);
773 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
774 set_temp_min, 0);
775 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
776 set_temp_min, 1);
777 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
778 set_temp_min, 2);
779 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
780 set_temp_min, 3);
781 static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
782 set_temp_min, 4);
783 static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
784 set_temp_min, 5);
785 static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
786 set_temp_min, 6);
787 static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
788 set_temp_min, 7);
789 static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
790 set_temp_min, 8);
791 static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
792 set_temp_min, 9);
794 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
795 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
796 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
797 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
798 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
799 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
800 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
801 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
802 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
803 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
805 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
806 ADT7470_R1T_ALARM);
807 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
808 ADT7470_R2T_ALARM);
809 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
810 ADT7470_R3T_ALARM);
811 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
812 ADT7470_R4T_ALARM);
813 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
814 ADT7470_R5T_ALARM);
815 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
816 ADT7470_R6T_ALARM);
817 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
818 ADT7470_R7T_ALARM);
819 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
820 ALARM2(ADT7470_R8T_ALARM));
821 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
822 ALARM2(ADT7470_R9T_ALARM));
823 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
824 ALARM2(ADT7470_R10T_ALARM));
826 static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
827 set_fan_max, 0);
828 static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
829 set_fan_max, 1);
830 static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
831 set_fan_max, 2);
832 static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
833 set_fan_max, 3);
835 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
836 set_fan_min, 0);
837 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
838 set_fan_min, 1);
839 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
840 set_fan_min, 2);
841 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
842 set_fan_min, 3);
844 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
845 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
846 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
847 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
849 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
850 ALARM2(ADT7470_FAN1_ALARM));
851 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
852 ALARM2(ADT7470_FAN2_ALARM));
853 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
854 ALARM2(ADT7470_FAN3_ALARM));
855 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
856 ALARM2(ADT7470_FAN4_ALARM));
858 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
859 show_force_pwm_max, set_force_pwm_max, 0);
861 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
862 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
863 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
864 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
866 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
867 show_pwm_min, set_pwm_min, 0);
868 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
869 show_pwm_min, set_pwm_min, 1);
870 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
871 show_pwm_min, set_pwm_min, 2);
872 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
873 show_pwm_min, set_pwm_min, 3);
875 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
876 show_pwm_max, set_pwm_max, 0);
877 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
878 show_pwm_max, set_pwm_max, 1);
879 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
880 show_pwm_max, set_pwm_max, 2);
881 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
882 show_pwm_max, set_pwm_max, 3);
884 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
885 show_pwm_tmin, set_pwm_tmin, 0);
886 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
887 show_pwm_tmin, set_pwm_tmin, 1);
888 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
889 show_pwm_tmin, set_pwm_tmin, 2);
890 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
891 show_pwm_tmin, set_pwm_tmin, 3);
893 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
894 NULL, 0);
895 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
896 NULL, 1);
897 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
898 NULL, 2);
899 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
900 NULL, 3);
902 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
903 set_pwm_auto, 0);
904 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
905 set_pwm_auto, 1);
906 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
907 set_pwm_auto, 2);
908 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
909 set_pwm_auto, 3);
911 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
912 show_pwm_auto_temp, set_pwm_auto_temp, 0);
913 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
914 show_pwm_auto_temp, set_pwm_auto_temp, 1);
915 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
916 show_pwm_auto_temp, set_pwm_auto_temp, 2);
917 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
918 show_pwm_auto_temp, set_pwm_auto_temp, 3);
920 static struct attribute *adt7470_attr[] =
922 &dev_attr_alarm_mask.attr,
923 &sensor_dev_attr_temp1_max.dev_attr.attr,
924 &sensor_dev_attr_temp2_max.dev_attr.attr,
925 &sensor_dev_attr_temp3_max.dev_attr.attr,
926 &sensor_dev_attr_temp4_max.dev_attr.attr,
927 &sensor_dev_attr_temp5_max.dev_attr.attr,
928 &sensor_dev_attr_temp6_max.dev_attr.attr,
929 &sensor_dev_attr_temp7_max.dev_attr.attr,
930 &sensor_dev_attr_temp8_max.dev_attr.attr,
931 &sensor_dev_attr_temp9_max.dev_attr.attr,
932 &sensor_dev_attr_temp10_max.dev_attr.attr,
933 &sensor_dev_attr_temp1_min.dev_attr.attr,
934 &sensor_dev_attr_temp2_min.dev_attr.attr,
935 &sensor_dev_attr_temp3_min.dev_attr.attr,
936 &sensor_dev_attr_temp4_min.dev_attr.attr,
937 &sensor_dev_attr_temp5_min.dev_attr.attr,
938 &sensor_dev_attr_temp6_min.dev_attr.attr,
939 &sensor_dev_attr_temp7_min.dev_attr.attr,
940 &sensor_dev_attr_temp8_min.dev_attr.attr,
941 &sensor_dev_attr_temp9_min.dev_attr.attr,
942 &sensor_dev_attr_temp10_min.dev_attr.attr,
943 &sensor_dev_attr_temp1_input.dev_attr.attr,
944 &sensor_dev_attr_temp2_input.dev_attr.attr,
945 &sensor_dev_attr_temp3_input.dev_attr.attr,
946 &sensor_dev_attr_temp4_input.dev_attr.attr,
947 &sensor_dev_attr_temp5_input.dev_attr.attr,
948 &sensor_dev_attr_temp6_input.dev_attr.attr,
949 &sensor_dev_attr_temp7_input.dev_attr.attr,
950 &sensor_dev_attr_temp8_input.dev_attr.attr,
951 &sensor_dev_attr_temp9_input.dev_attr.attr,
952 &sensor_dev_attr_temp10_input.dev_attr.attr,
953 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
954 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
955 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
956 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
957 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
958 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
959 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
960 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
961 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
962 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
963 &sensor_dev_attr_fan1_max.dev_attr.attr,
964 &sensor_dev_attr_fan2_max.dev_attr.attr,
965 &sensor_dev_attr_fan3_max.dev_attr.attr,
966 &sensor_dev_attr_fan4_max.dev_attr.attr,
967 &sensor_dev_attr_fan1_min.dev_attr.attr,
968 &sensor_dev_attr_fan2_min.dev_attr.attr,
969 &sensor_dev_attr_fan3_min.dev_attr.attr,
970 &sensor_dev_attr_fan4_min.dev_attr.attr,
971 &sensor_dev_attr_fan1_input.dev_attr.attr,
972 &sensor_dev_attr_fan2_input.dev_attr.attr,
973 &sensor_dev_attr_fan3_input.dev_attr.attr,
974 &sensor_dev_attr_fan4_input.dev_attr.attr,
975 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
976 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
977 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
978 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
979 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
980 &sensor_dev_attr_pwm1.dev_attr.attr,
981 &sensor_dev_attr_pwm2.dev_attr.attr,
982 &sensor_dev_attr_pwm3.dev_attr.attr,
983 &sensor_dev_attr_pwm4.dev_attr.attr,
984 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
985 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
986 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
987 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
988 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
989 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
990 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
991 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
992 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
993 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
994 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
995 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
996 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
997 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
998 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
999 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1000 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1001 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1002 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1003 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1004 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1005 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1006 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1007 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1008 NULL
1011 static int adt7470_attach_adapter(struct i2c_adapter *adapter)
1013 if (!(adapter->class & I2C_CLASS_HWMON))
1014 return 0;
1015 return i2c_probe(adapter, &addr_data, adt7470_detect);
1018 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind)
1020 struct i2c_client *client;
1021 struct adt7470_data *data;
1022 int err = 0;
1024 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1025 goto exit;
1027 if (!(data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL))) {
1028 err = -ENOMEM;
1029 goto exit;
1032 client = &data->client;
1033 client->addr = address;
1034 client->adapter = adapter;
1035 client->driver = &adt7470_driver;
1037 i2c_set_clientdata(client, data);
1039 mutex_init(&data->lock);
1041 if (kind <= 0) {
1042 int vendor, device, revision;
1044 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1045 if (vendor != ADT7470_VENDOR) {
1046 err = -ENODEV;
1047 goto exit_free;
1050 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1051 if (device != ADT7470_DEVICE) {
1052 err = -ENODEV;
1053 goto exit_free;
1056 revision = i2c_smbus_read_byte_data(client,
1057 ADT7470_REG_REVISION);
1058 if (revision != ADT7470_REVISION) {
1059 err = -ENODEV;
1060 goto exit_free;
1062 } else
1063 dev_dbg(&adapter->dev, "detection forced\n");
1065 strlcpy(client->name, "adt7470", I2C_NAME_SIZE);
1067 if ((err = i2c_attach_client(client)))
1068 goto exit_free;
1070 dev_info(&client->dev, "%s chip found\n", client->name);
1072 /* Initialize the ADT7470 chip */
1073 adt7470_init_client(client);
1075 /* Register sysfs hooks */
1076 data->attrs.attrs = adt7470_attr;
1077 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
1078 goto exit_detach;
1080 data->hwmon_dev = hwmon_device_register(&client->dev);
1081 if (IS_ERR(data->hwmon_dev)) {
1082 err = PTR_ERR(data->hwmon_dev);
1083 goto exit_remove;
1086 return 0;
1088 exit_remove:
1089 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1090 exit_detach:
1091 i2c_detach_client(client);
1092 exit_free:
1093 kfree(data);
1094 exit:
1095 return err;
1098 static int adt7470_detach_client(struct i2c_client *client)
1100 struct adt7470_data *data = i2c_get_clientdata(client);
1102 hwmon_device_unregister(data->hwmon_dev);
1103 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1104 i2c_detach_client(client);
1105 kfree(data);
1106 return 0;
1109 static int __init adt7470_init(void)
1111 return i2c_add_driver(&adt7470_driver);
1114 static void __exit adt7470_exit(void)
1116 i2c_del_driver(&adt7470_driver);
1119 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1120 MODULE_DESCRIPTION("ADT7470 driver");
1121 MODULE_LICENSE("GPL");
1123 module_init(adt7470_init);
1124 module_exit(adt7470_exit);