iwmc3200wifi: Implement cfg80211 PMKSA API
[linux/fpc-iii.git] / drivers / hwmon / adt7470.c
blob633e1a1e9d79843f1d8feebbb276e737903b685f
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>
31 #include <linux/kthread.h>
33 /* Addresses to scan */
34 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
36 /* Insmod parameters */
37 I2C_CLIENT_INSMOD_1(adt7470);
39 /* ADT7470 registers */
40 #define ADT7470_REG_BASE_ADDR 0x20
41 #define ADT7470_REG_TEMP_BASE_ADDR 0x20
42 #define ADT7470_REG_TEMP_MAX_ADDR 0x29
43 #define ADT7470_REG_FAN_BASE_ADDR 0x2A
44 #define ADT7470_REG_FAN_MAX_ADDR 0x31
45 #define ADT7470_REG_PWM_BASE_ADDR 0x32
46 #define ADT7470_REG_PWM_MAX_ADDR 0x35
47 #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
48 #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
49 #define ADT7470_REG_CFG 0x40
50 #define ADT7470_FSPD_MASK 0x04
51 #define ADT7470_REG_ALARM1 0x41
52 #define ADT7470_R1T_ALARM 0x01
53 #define ADT7470_R2T_ALARM 0x02
54 #define ADT7470_R3T_ALARM 0x04
55 #define ADT7470_R4T_ALARM 0x08
56 #define ADT7470_R5T_ALARM 0x10
57 #define ADT7470_R6T_ALARM 0x20
58 #define ADT7470_R7T_ALARM 0x40
59 #define ADT7470_OOL_ALARM 0x80
60 #define ADT7470_REG_ALARM2 0x42
61 #define ADT7470_R8T_ALARM 0x01
62 #define ADT7470_R9T_ALARM 0x02
63 #define ADT7470_R10T_ALARM 0x04
64 #define ADT7470_FAN1_ALARM 0x10
65 #define ADT7470_FAN2_ALARM 0x20
66 #define ADT7470_FAN3_ALARM 0x40
67 #define ADT7470_FAN4_ALARM 0x80
68 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
69 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
70 #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
71 #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
72 #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
73 #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
74 #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
75 #define ADT7470_REG_PWM12_CFG 0x68
76 #define ADT7470_PWM2_AUTO_MASK 0x40
77 #define ADT7470_PWM1_AUTO_MASK 0x80
78 #define ADT7470_PWM_AUTO_MASK 0xC0
79 #define ADT7470_REG_PWM34_CFG 0x69
80 #define ADT7470_PWM3_AUTO_MASK 0x40
81 #define ADT7470_PWM4_AUTO_MASK 0x80
82 #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
83 #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
84 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
85 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
86 #define ADT7470_REG_ACOUSTICS12 0x75
87 #define ADT7470_REG_ACOUSTICS34 0x76
88 #define ADT7470_REG_DEVICE 0x3D
89 #define ADT7470_REG_VENDOR 0x3E
90 #define ADT7470_REG_REVISION 0x3F
91 #define ADT7470_REG_ALARM1_MASK 0x72
92 #define ADT7470_REG_ALARM2_MASK 0x73
93 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
94 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
95 #define ADT7470_REG_MAX_ADDR 0x81
97 #define ADT7470_TEMP_COUNT 10
98 #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
99 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
100 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
101 ((x) * 2) + 1)
103 #define ADT7470_FAN_COUNT 4
104 #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
105 #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
106 #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
108 #define ADT7470_PWM_COUNT 4
109 #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
110 #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
111 #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
112 #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
113 #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
114 #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
115 ((x) / 2))
117 #define ALARM2(x) ((x) << 8)
119 #define ADT7470_VENDOR 0x41
120 #define ADT7470_DEVICE 0x70
121 /* datasheet only mentions a revision 2 */
122 #define ADT7470_REVISION 0x02
124 /* "all temps" according to hwmon sysfs interface spec */
125 #define ADT7470_PWM_ALL_TEMPS 0x3FF
127 /* How often do we reread sensors values? (In jiffies) */
128 #define SENSOR_REFRESH_INTERVAL (5 * HZ)
130 /* How often do we reread sensor limit values? (In jiffies) */
131 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
133 /* Wait at least 200ms per sensor for 10 sensors */
134 #define TEMP_COLLECTION_TIME 2000
136 /* auto update thing won't fire more than every 2s */
137 #define AUTO_UPDATE_INTERVAL 2000
139 /* datasheet says to divide this number by the fan reading to get fan rpm */
140 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
141 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
142 #define FAN_PERIOD_INVALID 65535
143 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
145 struct adt7470_data {
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 int num_temp_sensors; /* -1 = probe */
155 int temperatures_probed;
157 s8 temp[ADT7470_TEMP_COUNT];
158 s8 temp_min[ADT7470_TEMP_COUNT];
159 s8 temp_max[ADT7470_TEMP_COUNT];
160 u16 fan[ADT7470_FAN_COUNT];
161 u16 fan_min[ADT7470_FAN_COUNT];
162 u16 fan_max[ADT7470_FAN_COUNT];
163 u16 alarm;
164 u16 alarms_mask;
165 u8 force_pwm_max;
166 u8 pwm[ADT7470_PWM_COUNT];
167 u8 pwm_max[ADT7470_PWM_COUNT];
168 u8 pwm_automatic[ADT7470_PWM_COUNT];
169 u8 pwm_min[ADT7470_PWM_COUNT];
170 s8 pwm_tmin[ADT7470_PWM_COUNT];
171 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
173 struct task_struct *auto_update;
174 struct completion auto_update_stop;
175 unsigned int auto_update_interval;
178 static int adt7470_probe(struct i2c_client *client,
179 const struct i2c_device_id *id);
180 static int adt7470_detect(struct i2c_client *client, int kind,
181 struct i2c_board_info *info);
182 static int adt7470_remove(struct i2c_client *client);
184 static const struct i2c_device_id adt7470_id[] = {
185 { "adt7470", adt7470 },
188 MODULE_DEVICE_TABLE(i2c, adt7470_id);
190 static struct i2c_driver adt7470_driver = {
191 .class = I2C_CLASS_HWMON,
192 .driver = {
193 .name = "adt7470",
195 .probe = adt7470_probe,
196 .remove = adt7470_remove,
197 .id_table = adt7470_id,
198 .detect = adt7470_detect,
199 .address_data = &addr_data,
203 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
204 * that the low byte must be read before the high byte.
206 static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
208 u16 foo;
209 foo = i2c_smbus_read_byte_data(client, reg);
210 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
211 return foo;
214 static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
215 u16 value)
217 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
218 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
221 static void adt7470_init_client(struct i2c_client *client)
223 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
225 if (reg < 0) {
226 dev_err(&client->dev, "cannot read configuration register\n");
227 } else {
228 /* start monitoring (and do a self-test) */
229 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
233 /* Probe for temperature sensors. Assumes lock is held */
234 static int adt7470_read_temperatures(struct i2c_client *client,
235 struct adt7470_data *data)
237 unsigned long res;
238 int i;
239 u8 cfg, pwm[4], pwm_cfg[2];
241 /* save pwm[1-4] config register */
242 pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
243 pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
245 /* set manual pwm to whatever it is set to now */
246 for (i = 0; i < ADT7470_FAN_COUNT; i++)
247 pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
249 /* put pwm in manual mode */
250 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
251 pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
252 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
253 pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
255 /* write pwm control to whatever it was */
256 for (i = 0; i < ADT7470_FAN_COUNT; i++)
257 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
259 /* start reading temperature sensors */
260 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
261 cfg |= 0x80;
262 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
264 /* Delay is 200ms * number of temp sensors. */
265 res = msleep_interruptible((data->num_temp_sensors >= 0 ?
266 data->num_temp_sensors * 200 :
267 TEMP_COLLECTION_TIME));
269 /* done reading temperature sensors */
270 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
271 cfg &= ~0x80;
272 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
274 /* restore pwm[1-4] config registers */
275 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
276 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
278 if (res) {
279 printk(KERN_ERR "ha ha, interrupted");
280 return -EAGAIN;
283 /* Only count fans if we have to */
284 if (data->num_temp_sensors >= 0)
285 return 0;
287 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
288 data->temp[i] = i2c_smbus_read_byte_data(client,
289 ADT7470_TEMP_REG(i));
290 if (data->temp[i])
291 data->num_temp_sensors = i + 1;
293 data->temperatures_probed = 1;
294 return 0;
297 static int adt7470_update_thread(void *p)
299 struct i2c_client *client = p;
300 struct adt7470_data *data = i2c_get_clientdata(client);
302 while (!kthread_should_stop()) {
303 mutex_lock(&data->lock);
304 adt7470_read_temperatures(client, data);
305 mutex_unlock(&data->lock);
306 if (kthread_should_stop())
307 break;
308 msleep_interruptible(data->auto_update_interval);
311 complete_all(&data->auto_update_stop);
312 return 0;
315 static struct adt7470_data *adt7470_update_device(struct device *dev)
317 struct i2c_client *client = to_i2c_client(dev);
318 struct adt7470_data *data = i2c_get_clientdata(client);
319 unsigned long local_jiffies = jiffies;
320 u8 cfg;
321 int i;
322 int need_sensors = 1;
323 int need_limits = 1;
326 * Figure out if we need to update the shadow registers.
327 * Lockless means that we may occasionally report out of
328 * date data.
330 if (time_before(local_jiffies, data->sensors_last_updated +
331 SENSOR_REFRESH_INTERVAL) &&
332 data->sensors_valid)
333 need_sensors = 0;
335 if (time_before(local_jiffies, data->limits_last_updated +
336 LIMIT_REFRESH_INTERVAL) &&
337 data->limits_valid)
338 need_limits = 0;
340 if (!need_sensors && !need_limits)
341 return data;
343 mutex_lock(&data->lock);
344 if (!need_sensors)
345 goto no_sensor_update;
347 if (!data->temperatures_probed)
348 adt7470_read_temperatures(client, data);
349 else
350 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
351 data->temp[i] = i2c_smbus_read_byte_data(client,
352 ADT7470_TEMP_REG(i));
354 for (i = 0; i < ADT7470_FAN_COUNT; i++)
355 data->fan[i] = adt7470_read_word_data(client,
356 ADT7470_REG_FAN(i));
358 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
359 int reg;
360 int reg_mask;
362 data->pwm[i] = i2c_smbus_read_byte_data(client,
363 ADT7470_REG_PWM(i));
365 if (i % 2)
366 reg_mask = ADT7470_PWM2_AUTO_MASK;
367 else
368 reg_mask = ADT7470_PWM1_AUTO_MASK;
370 reg = ADT7470_REG_PWM_CFG(i);
371 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
372 data->pwm_automatic[i] = 1;
373 else
374 data->pwm_automatic[i] = 0;
376 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
377 cfg = i2c_smbus_read_byte_data(client, reg);
378 if (!(i % 2))
379 data->pwm_auto_temp[i] = cfg >> 4;
380 else
381 data->pwm_auto_temp[i] = cfg & 0xF;
384 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
385 ADT7470_FSPD_MASK)
386 data->force_pwm_max = 1;
387 else
388 data->force_pwm_max = 0;
390 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
391 if (data->alarm & ADT7470_OOL_ALARM)
392 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
393 ADT7470_REG_ALARM2));
394 data->alarms_mask = adt7470_read_word_data(client,
395 ADT7470_REG_ALARM1_MASK);
397 data->sensors_last_updated = local_jiffies;
398 data->sensors_valid = 1;
400 no_sensor_update:
401 if (!need_limits)
402 goto out;
404 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
405 data->temp_min[i] = i2c_smbus_read_byte_data(client,
406 ADT7470_TEMP_MIN_REG(i));
407 data->temp_max[i] = i2c_smbus_read_byte_data(client,
408 ADT7470_TEMP_MAX_REG(i));
411 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
412 data->fan_min[i] = adt7470_read_word_data(client,
413 ADT7470_REG_FAN_MIN(i));
414 data->fan_max[i] = adt7470_read_word_data(client,
415 ADT7470_REG_FAN_MAX(i));
418 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
419 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
420 ADT7470_REG_PWM_MAX(i));
421 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
422 ADT7470_REG_PWM_MIN(i));
423 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
424 ADT7470_REG_PWM_TMIN(i));
427 data->limits_last_updated = local_jiffies;
428 data->limits_valid = 1;
430 out:
431 mutex_unlock(&data->lock);
432 return data;
435 static ssize_t show_auto_update_interval(struct device *dev,
436 struct device_attribute *devattr,
437 char *buf)
439 struct adt7470_data *data = adt7470_update_device(dev);
440 return sprintf(buf, "%d\n", data->auto_update_interval);
443 static ssize_t set_auto_update_interval(struct device *dev,
444 struct device_attribute *devattr,
445 const char *buf,
446 size_t count)
448 struct i2c_client *client = to_i2c_client(dev);
449 struct adt7470_data *data = i2c_get_clientdata(client);
450 long temp;
452 if (strict_strtol(buf, 10, &temp))
453 return -EINVAL;
455 temp = SENSORS_LIMIT(temp, 0, 60000);
457 mutex_lock(&data->lock);
458 data->auto_update_interval = temp;
459 mutex_unlock(&data->lock);
461 return count;
464 static ssize_t show_num_temp_sensors(struct device *dev,
465 struct device_attribute *devattr,
466 char *buf)
468 struct adt7470_data *data = adt7470_update_device(dev);
469 return sprintf(buf, "%d\n", data->num_temp_sensors);
472 static ssize_t set_num_temp_sensors(struct device *dev,
473 struct device_attribute *devattr,
474 const char *buf,
475 size_t count)
477 struct i2c_client *client = to_i2c_client(dev);
478 struct adt7470_data *data = i2c_get_clientdata(client);
479 long temp;
481 if (strict_strtol(buf, 10, &temp))
482 return -EINVAL;
484 temp = SENSORS_LIMIT(temp, -1, 10);
486 mutex_lock(&data->lock);
487 data->num_temp_sensors = temp;
488 if (temp < 0)
489 data->temperatures_probed = 0;
490 mutex_unlock(&data->lock);
492 return count;
495 static ssize_t show_temp_min(struct device *dev,
496 struct device_attribute *devattr,
497 char *buf)
499 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
500 struct adt7470_data *data = adt7470_update_device(dev);
501 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
504 static ssize_t set_temp_min(struct device *dev,
505 struct device_attribute *devattr,
506 const char *buf,
507 size_t count)
509 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
510 struct i2c_client *client = to_i2c_client(dev);
511 struct adt7470_data *data = i2c_get_clientdata(client);
512 long temp;
514 if (strict_strtol(buf, 10, &temp))
515 return -EINVAL;
517 temp = DIV_ROUND_CLOSEST(temp, 1000);
518 temp = SENSORS_LIMIT(temp, 0, 255);
520 mutex_lock(&data->lock);
521 data->temp_min[attr->index] = temp;
522 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
523 temp);
524 mutex_unlock(&data->lock);
526 return count;
529 static ssize_t show_temp_max(struct device *dev,
530 struct device_attribute *devattr,
531 char *buf)
533 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
534 struct adt7470_data *data = adt7470_update_device(dev);
535 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
538 static ssize_t set_temp_max(struct device *dev,
539 struct device_attribute *devattr,
540 const char *buf,
541 size_t count)
543 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
544 struct i2c_client *client = to_i2c_client(dev);
545 struct adt7470_data *data = i2c_get_clientdata(client);
546 long temp;
548 if (strict_strtol(buf, 10, &temp))
549 return -EINVAL;
551 temp = DIV_ROUND_CLOSEST(temp, 1000);
552 temp = SENSORS_LIMIT(temp, 0, 255);
554 mutex_lock(&data->lock);
555 data->temp_max[attr->index] = temp;
556 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
557 temp);
558 mutex_unlock(&data->lock);
560 return count;
563 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
564 char *buf)
566 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
567 struct adt7470_data *data = adt7470_update_device(dev);
568 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
571 static ssize_t show_alarm_mask(struct device *dev,
572 struct device_attribute *devattr,
573 char *buf)
575 struct adt7470_data *data = adt7470_update_device(dev);
577 return sprintf(buf, "%x\n", data->alarms_mask);
580 static ssize_t show_fan_max(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);
587 if (FAN_DATA_VALID(data->fan_max[attr->index]))
588 return sprintf(buf, "%d\n",
589 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
590 else
591 return sprintf(buf, "0\n");
594 static ssize_t set_fan_max(struct device *dev,
595 struct device_attribute *devattr,
596 const char *buf, size_t count)
598 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
599 struct i2c_client *client = to_i2c_client(dev);
600 struct adt7470_data *data = i2c_get_clientdata(client);
601 long temp;
603 if (strict_strtol(buf, 10, &temp) || !temp)
604 return -EINVAL;
606 temp = FAN_RPM_TO_PERIOD(temp);
607 temp = SENSORS_LIMIT(temp, 1, 65534);
609 mutex_lock(&data->lock);
610 data->fan_max[attr->index] = temp;
611 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
612 mutex_unlock(&data->lock);
614 return count;
617 static ssize_t show_fan_min(struct device *dev,
618 struct device_attribute *devattr,
619 char *buf)
621 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
622 struct adt7470_data *data = adt7470_update_device(dev);
624 if (FAN_DATA_VALID(data->fan_min[attr->index]))
625 return sprintf(buf, "%d\n",
626 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
627 else
628 return sprintf(buf, "0\n");
631 static ssize_t set_fan_min(struct device *dev,
632 struct device_attribute *devattr,
633 const char *buf, size_t count)
635 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
636 struct i2c_client *client = to_i2c_client(dev);
637 struct adt7470_data *data = i2c_get_clientdata(client);
638 long temp;
640 if (strict_strtol(buf, 10, &temp) || !temp)
641 return -EINVAL;
643 temp = FAN_RPM_TO_PERIOD(temp);
644 temp = SENSORS_LIMIT(temp, 1, 65534);
646 mutex_lock(&data->lock);
647 data->fan_min[attr->index] = temp;
648 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
649 mutex_unlock(&data->lock);
651 return count;
654 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
655 char *buf)
657 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
658 struct adt7470_data *data = adt7470_update_device(dev);
660 if (FAN_DATA_VALID(data->fan[attr->index]))
661 return sprintf(buf, "%d\n",
662 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
663 else
664 return sprintf(buf, "0\n");
667 static ssize_t show_force_pwm_max(struct device *dev,
668 struct device_attribute *devattr,
669 char *buf)
671 struct adt7470_data *data = adt7470_update_device(dev);
672 return sprintf(buf, "%d\n", data->force_pwm_max);
675 static ssize_t set_force_pwm_max(struct device *dev,
676 struct device_attribute *devattr,
677 const char *buf,
678 size_t count)
680 struct i2c_client *client = to_i2c_client(dev);
681 struct adt7470_data *data = i2c_get_clientdata(client);
682 long temp;
683 u8 reg;
685 if (strict_strtol(buf, 10, &temp))
686 return -EINVAL;
688 mutex_lock(&data->lock);
689 data->force_pwm_max = temp;
690 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
691 if (temp)
692 reg |= ADT7470_FSPD_MASK;
693 else
694 reg &= ~ADT7470_FSPD_MASK;
695 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
696 mutex_unlock(&data->lock);
698 return count;
701 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
702 char *buf)
704 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
705 struct adt7470_data *data = adt7470_update_device(dev);
706 return sprintf(buf, "%d\n", data->pwm[attr->index]);
709 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
710 const char *buf, size_t count)
712 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
713 struct i2c_client *client = to_i2c_client(dev);
714 struct adt7470_data *data = i2c_get_clientdata(client);
715 long temp;
717 if (strict_strtol(buf, 10, &temp))
718 return -EINVAL;
720 temp = SENSORS_LIMIT(temp, 0, 255);
722 mutex_lock(&data->lock);
723 data->pwm[attr->index] = temp;
724 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
725 mutex_unlock(&data->lock);
727 return count;
730 static ssize_t show_pwm_max(struct device *dev,
731 struct device_attribute *devattr,
732 char *buf)
734 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
735 struct adt7470_data *data = adt7470_update_device(dev);
736 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
739 static ssize_t set_pwm_max(struct device *dev,
740 struct device_attribute *devattr,
741 const char *buf,
742 size_t count)
744 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
745 struct i2c_client *client = to_i2c_client(dev);
746 struct adt7470_data *data = i2c_get_clientdata(client);
747 long temp;
749 if (strict_strtol(buf, 10, &temp))
750 return -EINVAL;
752 temp = SENSORS_LIMIT(temp, 0, 255);
754 mutex_lock(&data->lock);
755 data->pwm_max[attr->index] = temp;
756 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
757 temp);
758 mutex_unlock(&data->lock);
760 return count;
763 static ssize_t show_pwm_min(struct device *dev,
764 struct device_attribute *devattr,
765 char *buf)
767 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
768 struct adt7470_data *data = adt7470_update_device(dev);
769 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
772 static ssize_t set_pwm_min(struct device *dev,
773 struct device_attribute *devattr,
774 const char *buf,
775 size_t count)
777 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
778 struct i2c_client *client = to_i2c_client(dev);
779 struct adt7470_data *data = i2c_get_clientdata(client);
780 long temp;
782 if (strict_strtol(buf, 10, &temp))
783 return -EINVAL;
785 temp = SENSORS_LIMIT(temp, 0, 255);
787 mutex_lock(&data->lock);
788 data->pwm_min[attr->index] = temp;
789 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
790 temp);
791 mutex_unlock(&data->lock);
793 return count;
796 static ssize_t show_pwm_tmax(struct device *dev,
797 struct device_attribute *devattr,
798 char *buf)
800 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
801 struct adt7470_data *data = adt7470_update_device(dev);
802 /* the datasheet says that tmax = tmin + 20C */
803 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
806 static ssize_t show_pwm_tmin(struct device *dev,
807 struct device_attribute *devattr,
808 char *buf)
810 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
811 struct adt7470_data *data = adt7470_update_device(dev);
812 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
815 static ssize_t set_pwm_tmin(struct device *dev,
816 struct device_attribute *devattr,
817 const char *buf,
818 size_t count)
820 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
821 struct i2c_client *client = to_i2c_client(dev);
822 struct adt7470_data *data = i2c_get_clientdata(client);
823 long temp;
825 if (strict_strtol(buf, 10, &temp))
826 return -EINVAL;
828 temp = DIV_ROUND_CLOSEST(temp, 1000);
829 temp = SENSORS_LIMIT(temp, 0, 255);
831 mutex_lock(&data->lock);
832 data->pwm_tmin[attr->index] = temp;
833 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
834 temp);
835 mutex_unlock(&data->lock);
837 return count;
840 static ssize_t show_pwm_auto(struct device *dev,
841 struct device_attribute *devattr,
842 char *buf)
844 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
845 struct adt7470_data *data = adt7470_update_device(dev);
846 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
849 static ssize_t set_pwm_auto(struct device *dev,
850 struct device_attribute *devattr,
851 const char *buf,
852 size_t count)
854 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
855 struct i2c_client *client = to_i2c_client(dev);
856 struct adt7470_data *data = i2c_get_clientdata(client);
857 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
858 int pwm_auto_reg_mask;
859 long temp;
860 u8 reg;
862 if (strict_strtol(buf, 10, &temp))
863 return -EINVAL;
865 if (attr->index % 2)
866 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
867 else
868 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
870 if (temp != 2 && temp != 1)
871 return -EINVAL;
872 temp--;
874 mutex_lock(&data->lock);
875 data->pwm_automatic[attr->index] = temp;
876 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
877 if (temp)
878 reg |= pwm_auto_reg_mask;
879 else
880 reg &= ~pwm_auto_reg_mask;
881 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
882 mutex_unlock(&data->lock);
884 return count;
887 static ssize_t show_pwm_auto_temp(struct device *dev,
888 struct device_attribute *devattr,
889 char *buf)
891 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
892 struct adt7470_data *data = adt7470_update_device(dev);
893 u8 ctrl = data->pwm_auto_temp[attr->index];
895 if (ctrl)
896 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
897 else
898 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
901 static int cvt_auto_temp(int input)
903 if (input == ADT7470_PWM_ALL_TEMPS)
904 return 0;
905 if (input < 1 || !is_power_of_2(input))
906 return -EINVAL;
907 return ilog2(input) + 1;
910 static ssize_t set_pwm_auto_temp(struct device *dev,
911 struct device_attribute *devattr,
912 const char *buf,
913 size_t count)
915 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
916 struct i2c_client *client = to_i2c_client(dev);
917 struct adt7470_data *data = i2c_get_clientdata(client);
918 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
919 long temp;
920 u8 reg;
922 if (strict_strtol(buf, 10, &temp))
923 return -EINVAL;
925 temp = cvt_auto_temp(temp);
926 if (temp < 0)
927 return temp;
929 mutex_lock(&data->lock);
930 data->pwm_automatic[attr->index] = temp;
931 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
933 if (!(attr->index % 2)) {
934 reg &= 0xF;
935 reg |= (temp << 4) & 0xF0;
936 } else {
937 reg &= 0xF0;
938 reg |= temp & 0xF;
941 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
942 mutex_unlock(&data->lock);
944 return count;
947 static ssize_t show_alarm(struct device *dev,
948 struct device_attribute *devattr,
949 char *buf)
951 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
952 struct adt7470_data *data = adt7470_update_device(dev);
954 if (data->alarm & attr->index)
955 return sprintf(buf, "1\n");
956 else
957 return sprintf(buf, "0\n");
960 static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
961 static DEVICE_ATTR(num_temp_sensors, S_IWUSR | S_IRUGO, show_num_temp_sensors,
962 set_num_temp_sensors);
963 static DEVICE_ATTR(auto_update_interval, S_IWUSR | S_IRUGO,
964 show_auto_update_interval, set_auto_update_interval);
966 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
967 set_temp_max, 0);
968 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
969 set_temp_max, 1);
970 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
971 set_temp_max, 2);
972 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
973 set_temp_max, 3);
974 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
975 set_temp_max, 4);
976 static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
977 set_temp_max, 5);
978 static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
979 set_temp_max, 6);
980 static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
981 set_temp_max, 7);
982 static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
983 set_temp_max, 8);
984 static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
985 set_temp_max, 9);
987 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
988 set_temp_min, 0);
989 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
990 set_temp_min, 1);
991 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
992 set_temp_min, 2);
993 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
994 set_temp_min, 3);
995 static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
996 set_temp_min, 4);
997 static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
998 set_temp_min, 5);
999 static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
1000 set_temp_min, 6);
1001 static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
1002 set_temp_min, 7);
1003 static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
1004 set_temp_min, 8);
1005 static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
1006 set_temp_min, 9);
1008 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1009 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1010 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1011 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
1012 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
1013 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
1014 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
1015 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
1016 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
1017 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
1019 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1020 ADT7470_R1T_ALARM);
1021 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1022 ADT7470_R2T_ALARM);
1023 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1024 ADT7470_R3T_ALARM);
1025 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1026 ADT7470_R4T_ALARM);
1027 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1028 ADT7470_R5T_ALARM);
1029 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1030 ADT7470_R6T_ALARM);
1031 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
1032 ADT7470_R7T_ALARM);
1033 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
1034 ALARM2(ADT7470_R8T_ALARM));
1035 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
1036 ALARM2(ADT7470_R9T_ALARM));
1037 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
1038 ALARM2(ADT7470_R10T_ALARM));
1040 static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
1041 set_fan_max, 0);
1042 static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
1043 set_fan_max, 1);
1044 static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
1045 set_fan_max, 2);
1046 static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
1047 set_fan_max, 3);
1049 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1050 set_fan_min, 0);
1051 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1052 set_fan_min, 1);
1053 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1054 set_fan_min, 2);
1055 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1056 set_fan_min, 3);
1058 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
1059 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
1060 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
1061 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
1063 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
1064 ALARM2(ADT7470_FAN1_ALARM));
1065 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
1066 ALARM2(ADT7470_FAN2_ALARM));
1067 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
1068 ALARM2(ADT7470_FAN3_ALARM));
1069 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
1070 ALARM2(ADT7470_FAN4_ALARM));
1072 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
1073 show_force_pwm_max, set_force_pwm_max, 0);
1075 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
1076 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
1077 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
1078 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
1080 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
1081 show_pwm_min, set_pwm_min, 0);
1082 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
1083 show_pwm_min, set_pwm_min, 1);
1084 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
1085 show_pwm_min, set_pwm_min, 2);
1086 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
1087 show_pwm_min, set_pwm_min, 3);
1089 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
1090 show_pwm_max, set_pwm_max, 0);
1091 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
1092 show_pwm_max, set_pwm_max, 1);
1093 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
1094 show_pwm_max, set_pwm_max, 2);
1095 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
1096 show_pwm_max, set_pwm_max, 3);
1098 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
1099 show_pwm_tmin, set_pwm_tmin, 0);
1100 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
1101 show_pwm_tmin, set_pwm_tmin, 1);
1102 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
1103 show_pwm_tmin, set_pwm_tmin, 2);
1104 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
1105 show_pwm_tmin, set_pwm_tmin, 3);
1107 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1108 NULL, 0);
1109 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1110 NULL, 1);
1111 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1112 NULL, 2);
1113 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1114 NULL, 3);
1116 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1117 set_pwm_auto, 0);
1118 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1119 set_pwm_auto, 1);
1120 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1121 set_pwm_auto, 2);
1122 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1123 set_pwm_auto, 3);
1125 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1126 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1127 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1128 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1129 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1130 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1131 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
1132 show_pwm_auto_temp, set_pwm_auto_temp, 3);
1134 static struct attribute *adt7470_attr[] =
1136 &dev_attr_alarm_mask.attr,
1137 &dev_attr_num_temp_sensors.attr,
1138 &dev_attr_auto_update_interval.attr,
1139 &sensor_dev_attr_temp1_max.dev_attr.attr,
1140 &sensor_dev_attr_temp2_max.dev_attr.attr,
1141 &sensor_dev_attr_temp3_max.dev_attr.attr,
1142 &sensor_dev_attr_temp4_max.dev_attr.attr,
1143 &sensor_dev_attr_temp5_max.dev_attr.attr,
1144 &sensor_dev_attr_temp6_max.dev_attr.attr,
1145 &sensor_dev_attr_temp7_max.dev_attr.attr,
1146 &sensor_dev_attr_temp8_max.dev_attr.attr,
1147 &sensor_dev_attr_temp9_max.dev_attr.attr,
1148 &sensor_dev_attr_temp10_max.dev_attr.attr,
1149 &sensor_dev_attr_temp1_min.dev_attr.attr,
1150 &sensor_dev_attr_temp2_min.dev_attr.attr,
1151 &sensor_dev_attr_temp3_min.dev_attr.attr,
1152 &sensor_dev_attr_temp4_min.dev_attr.attr,
1153 &sensor_dev_attr_temp5_min.dev_attr.attr,
1154 &sensor_dev_attr_temp6_min.dev_attr.attr,
1155 &sensor_dev_attr_temp7_min.dev_attr.attr,
1156 &sensor_dev_attr_temp8_min.dev_attr.attr,
1157 &sensor_dev_attr_temp9_min.dev_attr.attr,
1158 &sensor_dev_attr_temp10_min.dev_attr.attr,
1159 &sensor_dev_attr_temp1_input.dev_attr.attr,
1160 &sensor_dev_attr_temp2_input.dev_attr.attr,
1161 &sensor_dev_attr_temp3_input.dev_attr.attr,
1162 &sensor_dev_attr_temp4_input.dev_attr.attr,
1163 &sensor_dev_attr_temp5_input.dev_attr.attr,
1164 &sensor_dev_attr_temp6_input.dev_attr.attr,
1165 &sensor_dev_attr_temp7_input.dev_attr.attr,
1166 &sensor_dev_attr_temp8_input.dev_attr.attr,
1167 &sensor_dev_attr_temp9_input.dev_attr.attr,
1168 &sensor_dev_attr_temp10_input.dev_attr.attr,
1169 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1170 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1171 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1172 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1173 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1174 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1175 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1176 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1177 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1178 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
1179 &sensor_dev_attr_fan1_max.dev_attr.attr,
1180 &sensor_dev_attr_fan2_max.dev_attr.attr,
1181 &sensor_dev_attr_fan3_max.dev_attr.attr,
1182 &sensor_dev_attr_fan4_max.dev_attr.attr,
1183 &sensor_dev_attr_fan1_min.dev_attr.attr,
1184 &sensor_dev_attr_fan2_min.dev_attr.attr,
1185 &sensor_dev_attr_fan3_min.dev_attr.attr,
1186 &sensor_dev_attr_fan4_min.dev_attr.attr,
1187 &sensor_dev_attr_fan1_input.dev_attr.attr,
1188 &sensor_dev_attr_fan2_input.dev_attr.attr,
1189 &sensor_dev_attr_fan3_input.dev_attr.attr,
1190 &sensor_dev_attr_fan4_input.dev_attr.attr,
1191 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1192 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1193 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1194 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1195 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1196 &sensor_dev_attr_pwm1.dev_attr.attr,
1197 &sensor_dev_attr_pwm2.dev_attr.attr,
1198 &sensor_dev_attr_pwm3.dev_attr.attr,
1199 &sensor_dev_attr_pwm4.dev_attr.attr,
1200 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1201 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1202 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1203 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1204 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1205 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1206 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1207 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1208 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1209 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1210 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1211 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1212 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1213 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1214 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1215 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1216 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1217 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1218 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1219 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1220 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1221 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1222 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1223 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1224 NULL
1227 /* Return 0 if detection is successful, -ENODEV otherwise */
1228 static int adt7470_detect(struct i2c_client *client, int kind,
1229 struct i2c_board_info *info)
1231 struct i2c_adapter *adapter = client->adapter;
1233 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1234 return -ENODEV;
1236 if (kind <= 0) {
1237 int vendor, device, revision;
1239 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1240 if (vendor != ADT7470_VENDOR)
1241 return -ENODEV;
1243 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1244 if (device != ADT7470_DEVICE)
1245 return -ENODEV;
1247 revision = i2c_smbus_read_byte_data(client,
1248 ADT7470_REG_REVISION);
1249 if (revision != ADT7470_REVISION)
1250 return -ENODEV;
1251 } else
1252 dev_dbg(&adapter->dev, "detection forced\n");
1254 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
1256 return 0;
1259 static int adt7470_probe(struct i2c_client *client,
1260 const struct i2c_device_id *id)
1262 struct adt7470_data *data;
1263 int err;
1265 data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL);
1266 if (!data) {
1267 err = -ENOMEM;
1268 goto exit;
1271 data->num_temp_sensors = -1;
1272 data->auto_update_interval = AUTO_UPDATE_INTERVAL;
1274 i2c_set_clientdata(client, data);
1275 mutex_init(&data->lock);
1277 dev_info(&client->dev, "%s chip found\n", client->name);
1279 /* Initialize the ADT7470 chip */
1280 adt7470_init_client(client);
1282 /* Register sysfs hooks */
1283 data->attrs.attrs = adt7470_attr;
1284 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
1285 goto exit_free;
1287 data->hwmon_dev = hwmon_device_register(&client->dev);
1288 if (IS_ERR(data->hwmon_dev)) {
1289 err = PTR_ERR(data->hwmon_dev);
1290 goto exit_remove;
1293 init_completion(&data->auto_update_stop);
1294 data->auto_update = kthread_run(adt7470_update_thread, client,
1295 dev_name(data->hwmon_dev));
1296 if (IS_ERR(data->auto_update))
1297 goto exit_unregister;
1299 return 0;
1301 exit_unregister:
1302 hwmon_device_unregister(data->hwmon_dev);
1303 exit_remove:
1304 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1305 exit_free:
1306 kfree(data);
1307 exit:
1308 return err;
1311 static int adt7470_remove(struct i2c_client *client)
1313 struct adt7470_data *data = i2c_get_clientdata(client);
1315 kthread_stop(data->auto_update);
1316 wait_for_completion(&data->auto_update_stop);
1317 hwmon_device_unregister(data->hwmon_dev);
1318 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1319 kfree(data);
1320 return 0;
1323 static int __init adt7470_init(void)
1325 return i2c_add_driver(&adt7470_driver);
1328 static void __exit adt7470_exit(void)
1330 i2c_del_driver(&adt7470_driver);
1333 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1334 MODULE_DESCRIPTION("ADT7470 driver");
1335 MODULE_LICENSE("GPL");
1337 module_init(adt7470_init);
1338 module_exit(adt7470_exit);