blob: 1311a595147e17a136fbe5dbe1b81ebaa635acfe [file] [log] [blame]
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001/*
2 * A hwmon driver for the Analog Devices ADT7470
3 * Copyright (C) 2007 IBM
4 *
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
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
32/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050033static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070034
35/* Insmod parameters */
36I2C_CLIENT_INSMOD_1(adt7470);
37
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
Darrick J. Wongfe03f282007-12-19 14:11:25 -080051#define ADT7470_R1T_ALARM 0x01
52#define ADT7470_R2T_ALARM 0x02
53#define ADT7470_R3T_ALARM 0x04
54#define ADT7470_R4T_ALARM 0x08
55#define ADT7470_R5T_ALARM 0x10
56#define ADT7470_R6T_ALARM 0x20
57#define ADT7470_R7T_ALARM 0x40
58#define ADT7470_OOL_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070059#define ADT7470_REG_ALARM2 0x42
Darrick J. Wongfe03f282007-12-19 14:11:25 -080060#define ADT7470_R8T_ALARM 0x01
61#define ADT7470_R9T_ALARM 0x02
62#define ADT7470_R10T_ALARM 0x04
63#define ADT7470_FAN1_ALARM 0x10
64#define ADT7470_FAN2_ALARM 0x20
65#define ADT7470_FAN3_ALARM 0x40
66#define ADT7470_FAN4_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070067#define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
68#define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
69#define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
70#define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
71#define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
72#define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
73#define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
74#define ADT7470_REG_PWM12_CFG 0x68
75#define ADT7470_PWM2_AUTO_MASK 0x40
76#define ADT7470_PWM1_AUTO_MASK 0x80
77#define ADT7470_REG_PWM34_CFG 0x69
78#define ADT7470_PWM3_AUTO_MASK 0x40
79#define ADT7470_PWM4_AUTO_MASK 0x80
80#define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
81#define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
82#define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
83#define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
84#define ADT7470_REG_ACOUSTICS12 0x75
85#define ADT7470_REG_ACOUSTICS34 0x76
86#define ADT7470_REG_DEVICE 0x3D
87#define ADT7470_REG_VENDOR 0x3E
88#define ADT7470_REG_REVISION 0x3F
89#define ADT7470_REG_ALARM1_MASK 0x72
90#define ADT7470_REG_ALARM2_MASK 0x73
91#define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
92#define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
93#define ADT7470_REG_MAX_ADDR 0x81
94
95#define ADT7470_TEMP_COUNT 10
96#define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
97#define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
98#define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
99 ((x) * 2) + 1)
100
101#define ADT7470_FAN_COUNT 4
102#define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
103#define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
104#define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
105
106#define ADT7470_PWM_COUNT 4
107#define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
108#define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
109#define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
110#define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
111#define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
112#define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
113 ((x) / 2))
114
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800115#define ALARM2(x) ((x) << 8)
116
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700117#define ADT7470_VENDOR 0x41
118#define ADT7470_DEVICE 0x70
119/* datasheet only mentions a revision 2 */
120#define ADT7470_REVISION 0x02
121
122/* "all temps" according to hwmon sysfs interface spec */
123#define ADT7470_PWM_ALL_TEMPS 0x3FF
124
125/* How often do we reread sensors values? (In jiffies) */
126#define SENSOR_REFRESH_INTERVAL (5 * HZ)
127
128/* How often do we reread sensor limit values? (In jiffies) */
129#define LIMIT_REFRESH_INTERVAL (60 * HZ)
130
131/* sleep 1s while gathering temperature data */
132#define TEMP_COLLECTION_TIME 1000
133
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700134/* datasheet says to divide this number by the fan reading to get fan rpm */
135#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
136#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
137#define FAN_PERIOD_INVALID 65535
138#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
139
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800140#define ROUND_DIV(x, divisor) (((x) + ((divisor) / 2)) / (divisor))
141
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700142struct adt7470_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700143 struct device *hwmon_dev;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700144 struct attribute_group attrs;
145 struct mutex lock;
146 char sensors_valid;
147 char limits_valid;
148 unsigned long sensors_last_updated; /* In jiffies */
149 unsigned long limits_last_updated; /* In jiffies */
150
151 s8 temp[ADT7470_TEMP_COUNT];
152 s8 temp_min[ADT7470_TEMP_COUNT];
153 s8 temp_max[ADT7470_TEMP_COUNT];
154 u16 fan[ADT7470_FAN_COUNT];
155 u16 fan_min[ADT7470_FAN_COUNT];
156 u16 fan_max[ADT7470_FAN_COUNT];
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800157 u16 alarm;
158 u16 alarms_mask;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700159 u8 force_pwm_max;
160 u8 pwm[ADT7470_PWM_COUNT];
161 u8 pwm_max[ADT7470_PWM_COUNT];
162 u8 pwm_automatic[ADT7470_PWM_COUNT];
163 u8 pwm_min[ADT7470_PWM_COUNT];
164 s8 pwm_tmin[ADT7470_PWM_COUNT];
165 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
166};
167
Jean Delvare008f1ca2008-07-16 19:30:10 +0200168static int adt7470_probe(struct i2c_client *client,
169 const struct i2c_device_id *id);
170static int adt7470_detect(struct i2c_client *client, int kind,
171 struct i2c_board_info *info);
172static int adt7470_remove(struct i2c_client *client);
173
174static const struct i2c_device_id adt7470_id[] = {
175 { "adt7470", adt7470 },
176 { }
177};
178MODULE_DEVICE_TABLE(i2c, adt7470_id);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700179
180static struct i2c_driver adt7470_driver = {
Jean Delvare008f1ca2008-07-16 19:30:10 +0200181 .class = I2C_CLASS_HWMON,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700182 .driver = {
183 .name = "adt7470",
184 },
Jean Delvare008f1ca2008-07-16 19:30:10 +0200185 .probe = adt7470_probe,
186 .remove = adt7470_remove,
187 .id_table = adt7470_id,
188 .detect = adt7470_detect,
189 .address_data = &addr_data,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700190};
191
192/*
193 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
194 * that the low byte must be read before the high byte.
195 */
196static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
197{
198 u16 foo;
199 foo = i2c_smbus_read_byte_data(client, reg);
200 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
201 return foo;
202}
203
204static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
205 u16 value)
206{
207 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
208 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
209}
210
211static void adt7470_init_client(struct i2c_client *client)
212{
213 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
214
215 if (reg < 0) {
216 dev_err(&client->dev, "cannot read configuration register\n");
217 } else {
218 /* start monitoring (and do a self-test) */
219 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
220 }
221}
222
223static struct adt7470_data *adt7470_update_device(struct device *dev)
224{
225 struct i2c_client *client = to_i2c_client(dev);
226 struct adt7470_data *data = i2c_get_clientdata(client);
227 unsigned long local_jiffies = jiffies;
228 u8 cfg;
229 int i;
230
231 mutex_lock(&data->lock);
232 if (time_before(local_jiffies, data->sensors_last_updated +
233 SENSOR_REFRESH_INTERVAL)
234 && data->sensors_valid)
235 goto no_sensor_update;
236
237 /* start reading temperature sensors */
238 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
239 cfg |= 0x80;
240 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
241
242 /*
243 * Delay is 200ms * number of tmp05 sensors. Too bad
244 * there's no way to figure out how many are connected.
245 * For now, assume 1s will work.
246 */
247 msleep(TEMP_COLLECTION_TIME);
248
249 /* done reading temperature sensors */
250 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
251 cfg &= ~0x80;
252 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
253
254 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
255 data->temp[i] = i2c_smbus_read_byte_data(client,
256 ADT7470_TEMP_REG(i));
257
258 for (i = 0; i < ADT7470_FAN_COUNT; i++)
259 data->fan[i] = adt7470_read_word_data(client,
260 ADT7470_REG_FAN(i));
261
262 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
263 int reg;
264 int reg_mask;
265
266 data->pwm[i] = i2c_smbus_read_byte_data(client,
267 ADT7470_REG_PWM(i));
268
269 if (i % 2)
270 reg_mask = ADT7470_PWM2_AUTO_MASK;
271 else
272 reg_mask = ADT7470_PWM1_AUTO_MASK;
273
274 reg = ADT7470_REG_PWM_CFG(i);
275 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
276 data->pwm_automatic[i] = 1;
277 else
278 data->pwm_automatic[i] = 0;
279
280 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
281 cfg = i2c_smbus_read_byte_data(client, reg);
282 if (!(i % 2))
283 data->pwm_auto_temp[i] = cfg >> 4;
284 else
285 data->pwm_auto_temp[i] = cfg & 0xF;
286 }
287
288 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
289 ADT7470_FSPD_MASK)
290 data->force_pwm_max = 1;
291 else
292 data->force_pwm_max = 0;
293
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800294 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
295 if (data->alarm & ADT7470_OOL_ALARM)
296 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
297 ADT7470_REG_ALARM2));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700298 data->alarms_mask = adt7470_read_word_data(client,
299 ADT7470_REG_ALARM1_MASK);
300
301 data->sensors_last_updated = local_jiffies;
302 data->sensors_valid = 1;
303
304no_sensor_update:
305 if (time_before(local_jiffies, data->limits_last_updated +
306 LIMIT_REFRESH_INTERVAL)
307 && data->limits_valid)
308 goto out;
309
310 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
311 data->temp_min[i] = i2c_smbus_read_byte_data(client,
312 ADT7470_TEMP_MIN_REG(i));
313 data->temp_max[i] = i2c_smbus_read_byte_data(client,
314 ADT7470_TEMP_MAX_REG(i));
315 }
316
317 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
318 data->fan_min[i] = adt7470_read_word_data(client,
319 ADT7470_REG_FAN_MIN(i));
320 data->fan_max[i] = adt7470_read_word_data(client,
321 ADT7470_REG_FAN_MAX(i));
322 }
323
324 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
325 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
326 ADT7470_REG_PWM_MAX(i));
327 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
328 ADT7470_REG_PWM_MIN(i));
329 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
330 ADT7470_REG_PWM_TMIN(i));
331 }
332
333 data->limits_last_updated = local_jiffies;
334 data->limits_valid = 1;
335
336out:
337 mutex_unlock(&data->lock);
338 return data;
339}
340
341static ssize_t show_temp_min(struct device *dev,
342 struct device_attribute *devattr,
343 char *buf)
344{
345 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
346 struct adt7470_data *data = adt7470_update_device(dev);
347 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
348}
349
350static ssize_t set_temp_min(struct device *dev,
351 struct device_attribute *devattr,
352 const char *buf,
353 size_t count)
354{
355 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
356 struct i2c_client *client = to_i2c_client(dev);
357 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800358 long temp;
359
360 if (strict_strtol(buf, 10, &temp))
361 return -EINVAL;
362
363 temp = ROUND_DIV(temp, 1000);
364 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700365
366 mutex_lock(&data->lock);
367 data->temp_min[attr->index] = temp;
368 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
369 temp);
370 mutex_unlock(&data->lock);
371
372 return count;
373}
374
375static ssize_t show_temp_max(struct device *dev,
376 struct device_attribute *devattr,
377 char *buf)
378{
379 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
380 struct adt7470_data *data = adt7470_update_device(dev);
381 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
382}
383
384static ssize_t set_temp_max(struct device *dev,
385 struct device_attribute *devattr,
386 const char *buf,
387 size_t count)
388{
389 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
390 struct i2c_client *client = to_i2c_client(dev);
391 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800392 long temp;
393
394 if (strict_strtol(buf, 10, &temp))
395 return -EINVAL;
396
397 temp = ROUND_DIV(temp, 1000);
398 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700399
400 mutex_lock(&data->lock);
401 data->temp_max[attr->index] = temp;
402 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
403 temp);
404 mutex_unlock(&data->lock);
405
406 return count;
407}
408
409static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
410 char *buf)
411{
412 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
413 struct adt7470_data *data = adt7470_update_device(dev);
414 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
415}
416
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800417static ssize_t show_alarm_mask(struct device *dev,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700418 struct device_attribute *devattr,
419 char *buf)
420{
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700421 struct adt7470_data *data = adt7470_update_device(dev);
422
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800423 return sprintf(buf, "%x\n", data->alarms_mask);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700424}
425
426static ssize_t show_fan_max(struct device *dev,
427 struct device_attribute *devattr,
428 char *buf)
429{
430 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
431 struct adt7470_data *data = adt7470_update_device(dev);
432
433 if (FAN_DATA_VALID(data->fan_max[attr->index]))
434 return sprintf(buf, "%d\n",
435 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
436 else
437 return sprintf(buf, "0\n");
438}
439
440static ssize_t set_fan_max(struct device *dev,
441 struct device_attribute *devattr,
442 const char *buf, size_t count)
443{
444 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
445 struct i2c_client *client = to_i2c_client(dev);
446 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800447 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700448
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800449 if (strict_strtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700450 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800451
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700452 temp = FAN_RPM_TO_PERIOD(temp);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800453 temp = SENSORS_LIMIT(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700454
455 mutex_lock(&data->lock);
456 data->fan_max[attr->index] = temp;
457 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
458 mutex_unlock(&data->lock);
459
460 return count;
461}
462
463static ssize_t show_fan_min(struct device *dev,
464 struct device_attribute *devattr,
465 char *buf)
466{
467 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
468 struct adt7470_data *data = adt7470_update_device(dev);
469
470 if (FAN_DATA_VALID(data->fan_min[attr->index]))
471 return sprintf(buf, "%d\n",
472 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
473 else
474 return sprintf(buf, "0\n");
475}
476
477static ssize_t set_fan_min(struct device *dev,
478 struct device_attribute *devattr,
479 const char *buf, size_t count)
480{
481 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
482 struct i2c_client *client = to_i2c_client(dev);
483 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800484 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700485
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800486 if (strict_strtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700487 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800488
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700489 temp = FAN_RPM_TO_PERIOD(temp);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800490 temp = SENSORS_LIMIT(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700491
492 mutex_lock(&data->lock);
493 data->fan_min[attr->index] = temp;
494 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
495 mutex_unlock(&data->lock);
496
497 return count;
498}
499
500static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
501 char *buf)
502{
503 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
504 struct adt7470_data *data = adt7470_update_device(dev);
505
506 if (FAN_DATA_VALID(data->fan[attr->index]))
507 return sprintf(buf, "%d\n",
508 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
509 else
510 return sprintf(buf, "0\n");
511}
512
513static ssize_t show_force_pwm_max(struct device *dev,
514 struct device_attribute *devattr,
515 char *buf)
516{
517 struct adt7470_data *data = adt7470_update_device(dev);
518 return sprintf(buf, "%d\n", data->force_pwm_max);
519}
520
521static ssize_t set_force_pwm_max(struct device *dev,
522 struct device_attribute *devattr,
523 const char *buf,
524 size_t count)
525{
526 struct i2c_client *client = to_i2c_client(dev);
527 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800528 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700529 u8 reg;
530
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800531 if (strict_strtol(buf, 10, &temp))
532 return -EINVAL;
533
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700534 mutex_lock(&data->lock);
535 data->force_pwm_max = temp;
536 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
537 if (temp)
538 reg |= ADT7470_FSPD_MASK;
539 else
540 reg &= ~ADT7470_FSPD_MASK;
541 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
542 mutex_unlock(&data->lock);
543
544 return count;
545}
546
547static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
548 char *buf)
549{
550 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
551 struct adt7470_data *data = adt7470_update_device(dev);
552 return sprintf(buf, "%d\n", data->pwm[attr->index]);
553}
554
555static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
556 const char *buf, size_t count)
557{
558 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
559 struct i2c_client *client = to_i2c_client(dev);
560 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800561 long temp;
562
563 if (strict_strtol(buf, 10, &temp))
564 return -EINVAL;
565
566 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700567
568 mutex_lock(&data->lock);
569 data->pwm[attr->index] = temp;
570 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
571 mutex_unlock(&data->lock);
572
573 return count;
574}
575
576static ssize_t show_pwm_max(struct device *dev,
577 struct device_attribute *devattr,
578 char *buf)
579{
580 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
581 struct adt7470_data *data = adt7470_update_device(dev);
582 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
583}
584
585static ssize_t set_pwm_max(struct device *dev,
586 struct device_attribute *devattr,
587 const char *buf,
588 size_t count)
589{
590 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
591 struct i2c_client *client = to_i2c_client(dev);
592 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800593 long temp;
594
595 if (strict_strtol(buf, 10, &temp))
596 return -EINVAL;
597
598 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700599
600 mutex_lock(&data->lock);
601 data->pwm_max[attr->index] = temp;
602 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
603 temp);
604 mutex_unlock(&data->lock);
605
606 return count;
607}
608
609static ssize_t show_pwm_min(struct device *dev,
610 struct device_attribute *devattr,
611 char *buf)
612{
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", data->pwm_min[attr->index]);
616}
617
618static ssize_t set_pwm_min(struct device *dev,
619 struct device_attribute *devattr,
620 const char *buf,
621 size_t count)
622{
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);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800626 long temp;
627
628 if (strict_strtol(buf, 10, &temp))
629 return -EINVAL;
630
631 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700632
633 mutex_lock(&data->lock);
634 data->pwm_min[attr->index] = temp;
635 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
636 temp);
637 mutex_unlock(&data->lock);
638
639 return count;
640}
641
642static ssize_t show_pwm_tmax(struct device *dev,
643 struct device_attribute *devattr,
644 char *buf)
645{
646 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
647 struct adt7470_data *data = adt7470_update_device(dev);
648 /* the datasheet says that tmax = tmin + 20C */
649 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
650}
651
652static ssize_t show_pwm_tmin(struct device *dev,
653 struct device_attribute *devattr,
654 char *buf)
655{
656 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
657 struct adt7470_data *data = adt7470_update_device(dev);
658 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
659}
660
661static ssize_t set_pwm_tmin(struct device *dev,
662 struct device_attribute *devattr,
663 const char *buf,
664 size_t count)
665{
666 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
667 struct i2c_client *client = to_i2c_client(dev);
668 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800669 long temp;
670
671 if (strict_strtol(buf, 10, &temp))
672 return -EINVAL;
673
674 temp = ROUND_DIV(temp, 1000);
675 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700676
677 mutex_lock(&data->lock);
678 data->pwm_tmin[attr->index] = temp;
679 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
680 temp);
681 mutex_unlock(&data->lock);
682
683 return count;
684}
685
686static ssize_t show_pwm_auto(struct device *dev,
687 struct device_attribute *devattr,
688 char *buf)
689{
690 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
691 struct adt7470_data *data = adt7470_update_device(dev);
692 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
693}
694
695static ssize_t set_pwm_auto(struct device *dev,
696 struct device_attribute *devattr,
697 const char *buf,
698 size_t count)
699{
700 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 struct i2c_client *client = to_i2c_client(dev);
702 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700703 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
704 int pwm_auto_reg_mask;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800705 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700706 u8 reg;
707
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800708 if (strict_strtol(buf, 10, &temp))
709 return -EINVAL;
710
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700711 if (attr->index % 2)
712 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
713 else
714 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
715
716 if (temp != 2 && temp != 1)
717 return -EINVAL;
718 temp--;
719
720 mutex_lock(&data->lock);
721 data->pwm_automatic[attr->index] = temp;
722 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
723 if (temp)
724 reg |= pwm_auto_reg_mask;
725 else
726 reg &= ~pwm_auto_reg_mask;
727 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
728 mutex_unlock(&data->lock);
729
730 return count;
731}
732
733static ssize_t show_pwm_auto_temp(struct device *dev,
734 struct device_attribute *devattr,
735 char *buf)
736{
737 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
738 struct adt7470_data *data = adt7470_update_device(dev);
739 u8 ctrl = data->pwm_auto_temp[attr->index];
740
741 if (ctrl)
742 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
743 else
744 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
745}
746
747static int cvt_auto_temp(int input)
748{
749 if (input == ADT7470_PWM_ALL_TEMPS)
750 return 0;
Robert P. J. Dayce9c2f42007-11-06 03:21:42 -0500751 if (input < 1 || !is_power_of_2(input))
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700752 return -EINVAL;
753 return ilog2(input) + 1;
754}
755
756static ssize_t set_pwm_auto_temp(struct device *dev,
757 struct device_attribute *devattr,
758 const char *buf,
759 size_t count)
760{
761 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
762 struct i2c_client *client = to_i2c_client(dev);
763 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700764 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800765 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700766 u8 reg;
767
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800768 if (strict_strtol(buf, 10, &temp))
769 return -EINVAL;
770
771 temp = cvt_auto_temp(temp);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700772 if (temp < 0)
773 return temp;
774
775 mutex_lock(&data->lock);
776 data->pwm_automatic[attr->index] = temp;
777 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
778
779 if (!(attr->index % 2)) {
780 reg &= 0xF;
781 reg |= (temp << 4) & 0xF0;
782 } else {
783 reg &= 0xF0;
784 reg |= temp & 0xF;
785 }
786
787 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
788 mutex_unlock(&data->lock);
789
790 return count;
791}
792
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800793static ssize_t show_alarm(struct device *dev,
794 struct device_attribute *devattr,
795 char *buf)
796{
797 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
798 struct adt7470_data *data = adt7470_update_device(dev);
799
800 if (data->alarm & attr->index)
801 return sprintf(buf, "1\n");
802 else
803 return sprintf(buf, "0\n");
804}
805
806static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700807
808static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
809 set_temp_max, 0);
810static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
811 set_temp_max, 1);
812static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
813 set_temp_max, 2);
814static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
815 set_temp_max, 3);
816static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
817 set_temp_max, 4);
818static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
819 set_temp_max, 5);
820static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
821 set_temp_max, 6);
822static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
823 set_temp_max, 7);
824static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
825 set_temp_max, 8);
826static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
827 set_temp_max, 9);
828
829static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
830 set_temp_min, 0);
831static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
832 set_temp_min, 1);
833static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
834 set_temp_min, 2);
835static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
836 set_temp_min, 3);
837static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
838 set_temp_min, 4);
839static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
840 set_temp_min, 5);
841static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
842 set_temp_min, 6);
843static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
844 set_temp_min, 7);
845static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
846 set_temp_min, 8);
847static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
848 set_temp_min, 9);
849
850static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
851static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
852static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
853static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
854static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
855static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
856static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
857static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
858static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
859static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
860
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800861static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
862 ADT7470_R1T_ALARM);
863static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
864 ADT7470_R2T_ALARM);
865static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
866 ADT7470_R3T_ALARM);
867static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
868 ADT7470_R4T_ALARM);
869static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
870 ADT7470_R5T_ALARM);
871static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
872 ADT7470_R6T_ALARM);
873static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
874 ADT7470_R7T_ALARM);
875static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
876 ALARM2(ADT7470_R8T_ALARM));
877static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
878 ALARM2(ADT7470_R9T_ALARM));
879static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
880 ALARM2(ADT7470_R10T_ALARM));
881
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700882static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
883 set_fan_max, 0);
884static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
885 set_fan_max, 1);
886static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
887 set_fan_max, 2);
888static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
889 set_fan_max, 3);
890
891static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
892 set_fan_min, 0);
893static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
894 set_fan_min, 1);
895static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
896 set_fan_min, 2);
897static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
898 set_fan_min, 3);
899
900static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
901static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
902static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
903static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
904
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800905static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
906 ALARM2(ADT7470_FAN1_ALARM));
907static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
908 ALARM2(ADT7470_FAN2_ALARM));
909static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
910 ALARM2(ADT7470_FAN3_ALARM));
911static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
912 ALARM2(ADT7470_FAN4_ALARM));
913
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700914static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
915 show_force_pwm_max, set_force_pwm_max, 0);
916
917static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
918static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
919static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
920static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
921
922static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
923 show_pwm_min, set_pwm_min, 0);
924static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
925 show_pwm_min, set_pwm_min, 1);
926static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
927 show_pwm_min, set_pwm_min, 2);
928static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
929 show_pwm_min, set_pwm_min, 3);
930
931static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
932 show_pwm_max, set_pwm_max, 0);
933static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
934 show_pwm_max, set_pwm_max, 1);
935static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
936 show_pwm_max, set_pwm_max, 2);
937static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
938 show_pwm_max, set_pwm_max, 3);
939
940static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
941 show_pwm_tmin, set_pwm_tmin, 0);
942static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
943 show_pwm_tmin, set_pwm_tmin, 1);
944static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
945 show_pwm_tmin, set_pwm_tmin, 2);
946static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
947 show_pwm_tmin, set_pwm_tmin, 3);
948
949static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
950 NULL, 0);
951static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
952 NULL, 1);
953static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
954 NULL, 2);
955static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
956 NULL, 3);
957
958static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
959 set_pwm_auto, 0);
960static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
961 set_pwm_auto, 1);
962static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
963 set_pwm_auto, 2);
964static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
965 set_pwm_auto, 3);
966
967static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
968 show_pwm_auto_temp, set_pwm_auto_temp, 0);
969static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
970 show_pwm_auto_temp, set_pwm_auto_temp, 1);
971static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
972 show_pwm_auto_temp, set_pwm_auto_temp, 2);
973static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
974 show_pwm_auto_temp, set_pwm_auto_temp, 3);
975
976static struct attribute *adt7470_attr[] =
977{
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800978 &dev_attr_alarm_mask.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700979 &sensor_dev_attr_temp1_max.dev_attr.attr,
980 &sensor_dev_attr_temp2_max.dev_attr.attr,
981 &sensor_dev_attr_temp3_max.dev_attr.attr,
982 &sensor_dev_attr_temp4_max.dev_attr.attr,
983 &sensor_dev_attr_temp5_max.dev_attr.attr,
984 &sensor_dev_attr_temp6_max.dev_attr.attr,
985 &sensor_dev_attr_temp7_max.dev_attr.attr,
986 &sensor_dev_attr_temp8_max.dev_attr.attr,
987 &sensor_dev_attr_temp9_max.dev_attr.attr,
988 &sensor_dev_attr_temp10_max.dev_attr.attr,
989 &sensor_dev_attr_temp1_min.dev_attr.attr,
990 &sensor_dev_attr_temp2_min.dev_attr.attr,
991 &sensor_dev_attr_temp3_min.dev_attr.attr,
992 &sensor_dev_attr_temp4_min.dev_attr.attr,
993 &sensor_dev_attr_temp5_min.dev_attr.attr,
994 &sensor_dev_attr_temp6_min.dev_attr.attr,
995 &sensor_dev_attr_temp7_min.dev_attr.attr,
996 &sensor_dev_attr_temp8_min.dev_attr.attr,
997 &sensor_dev_attr_temp9_min.dev_attr.attr,
998 &sensor_dev_attr_temp10_min.dev_attr.attr,
999 &sensor_dev_attr_temp1_input.dev_attr.attr,
1000 &sensor_dev_attr_temp2_input.dev_attr.attr,
1001 &sensor_dev_attr_temp3_input.dev_attr.attr,
1002 &sensor_dev_attr_temp4_input.dev_attr.attr,
1003 &sensor_dev_attr_temp5_input.dev_attr.attr,
1004 &sensor_dev_attr_temp6_input.dev_attr.attr,
1005 &sensor_dev_attr_temp7_input.dev_attr.attr,
1006 &sensor_dev_attr_temp8_input.dev_attr.attr,
1007 &sensor_dev_attr_temp9_input.dev_attr.attr,
1008 &sensor_dev_attr_temp10_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001009 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1010 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1011 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1012 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1013 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1014 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1015 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1016 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1017 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1018 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001019 &sensor_dev_attr_fan1_max.dev_attr.attr,
1020 &sensor_dev_attr_fan2_max.dev_attr.attr,
1021 &sensor_dev_attr_fan3_max.dev_attr.attr,
1022 &sensor_dev_attr_fan4_max.dev_attr.attr,
1023 &sensor_dev_attr_fan1_min.dev_attr.attr,
1024 &sensor_dev_attr_fan2_min.dev_attr.attr,
1025 &sensor_dev_attr_fan3_min.dev_attr.attr,
1026 &sensor_dev_attr_fan4_min.dev_attr.attr,
1027 &sensor_dev_attr_fan1_input.dev_attr.attr,
1028 &sensor_dev_attr_fan2_input.dev_attr.attr,
1029 &sensor_dev_attr_fan3_input.dev_attr.attr,
1030 &sensor_dev_attr_fan4_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001031 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1032 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1033 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1034 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001035 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1036 &sensor_dev_attr_pwm1.dev_attr.attr,
1037 &sensor_dev_attr_pwm2.dev_attr.attr,
1038 &sensor_dev_attr_pwm3.dev_attr.attr,
1039 &sensor_dev_attr_pwm4.dev_attr.attr,
1040 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1041 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1042 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1043 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1044 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1045 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1046 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1047 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1048 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1049 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1050 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1051 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1052 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1053 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1054 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1055 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1056 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1057 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1058 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1059 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1060 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1061 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1062 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1063 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1064 NULL
1065};
1066
Jean Delvare008f1ca2008-07-16 19:30:10 +02001067/* Return 0 if detection is successful, -ENODEV otherwise */
1068static int adt7470_detect(struct i2c_client *client, int kind,
1069 struct i2c_board_info *info)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001070{
Jean Delvare008f1ca2008-07-16 19:30:10 +02001071 struct i2c_adapter *adapter = client->adapter;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001072
1073 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001074 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001075
1076 if (kind <= 0) {
1077 int vendor, device, revision;
1078
1079 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
Jean Delvare008f1ca2008-07-16 19:30:10 +02001080 if (vendor != ADT7470_VENDOR)
1081 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001082
1083 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
Jean Delvare008f1ca2008-07-16 19:30:10 +02001084 if (device != ADT7470_DEVICE)
1085 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001086
1087 revision = i2c_smbus_read_byte_data(client,
1088 ADT7470_REG_REVISION);
Jean Delvare008f1ca2008-07-16 19:30:10 +02001089 if (revision != ADT7470_REVISION)
1090 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001091 } else
1092 dev_dbg(&adapter->dev, "detection forced\n");
1093
Jean Delvare008f1ca2008-07-16 19:30:10 +02001094 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001095
Jean Delvare008f1ca2008-07-16 19:30:10 +02001096 return 0;
1097}
1098
1099static int adt7470_probe(struct i2c_client *client,
1100 const struct i2c_device_id *id)
1101{
1102 struct adt7470_data *data;
1103 int err;
1104
1105 data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL);
1106 if (!data) {
1107 err = -ENOMEM;
1108 goto exit;
1109 }
1110
1111 i2c_set_clientdata(client, data);
1112 mutex_init(&data->lock);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001113
1114 dev_info(&client->dev, "%s chip found\n", client->name);
1115
1116 /* Initialize the ADT7470 chip */
1117 adt7470_init_client(client);
1118
1119 /* Register sysfs hooks */
1120 data->attrs.attrs = adt7470_attr;
1121 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001122 goto exit_free;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001123
Tony Jones1beeffe2007-08-20 13:46:20 -07001124 data->hwmon_dev = hwmon_device_register(&client->dev);
1125 if (IS_ERR(data->hwmon_dev)) {
1126 err = PTR_ERR(data->hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001127 goto exit_remove;
1128 }
1129
1130 return 0;
1131
1132exit_remove:
1133 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001134exit_free:
1135 kfree(data);
1136exit:
1137 return err;
1138}
1139
Jean Delvare008f1ca2008-07-16 19:30:10 +02001140static int adt7470_remove(struct i2c_client *client)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001141{
1142 struct adt7470_data *data = i2c_get_clientdata(client);
1143
Tony Jones1beeffe2007-08-20 13:46:20 -07001144 hwmon_device_unregister(data->hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001145 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001146 kfree(data);
1147 return 0;
1148}
1149
1150static int __init adt7470_init(void)
1151{
1152 return i2c_add_driver(&adt7470_driver);
1153}
1154
1155static void __exit adt7470_exit(void)
1156{
1157 i2c_del_driver(&adt7470_driver);
1158}
1159
1160MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1161MODULE_DESCRIPTION("ADT7470 driver");
1162MODULE_LICENSE("GPL");
1163
1164module_init(adt7470_init);
1165module_exit(adt7470_exit);