blob: ab8d5ebc9f75e25838d3dbe80c5ef8100d426f53 [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
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -080077#define ADT7470_PWM_AUTO_MASK 0xC0
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070078#define ADT7470_REG_PWM34_CFG 0x69
79#define ADT7470_PWM3_AUTO_MASK 0x40
80#define ADT7470_PWM4_AUTO_MASK 0x80
81#define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
82#define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
83#define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
84#define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
85#define ADT7470_REG_ACOUSTICS12 0x75
86#define ADT7470_REG_ACOUSTICS34 0x76
87#define ADT7470_REG_DEVICE 0x3D
88#define ADT7470_REG_VENDOR 0x3E
89#define ADT7470_REG_REVISION 0x3F
90#define ADT7470_REG_ALARM1_MASK 0x72
91#define ADT7470_REG_ALARM2_MASK 0x73
92#define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
93#define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
94#define ADT7470_REG_MAX_ADDR 0x81
95
96#define ADT7470_TEMP_COUNT 10
97#define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
98#define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
99#define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
100 ((x) * 2) + 1)
101
102#define ADT7470_FAN_COUNT 4
103#define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
104#define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
105#define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
106
107#define ADT7470_PWM_COUNT 4
108#define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
109#define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
110#define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
111#define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
112#define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
113#define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
114 ((x) / 2))
115
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800116#define ALARM2(x) ((x) << 8)
117
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700118#define ADT7470_VENDOR 0x41
119#define ADT7470_DEVICE 0x70
120/* datasheet only mentions a revision 2 */
121#define ADT7470_REVISION 0x02
122
123/* "all temps" according to hwmon sysfs interface spec */
124#define ADT7470_PWM_ALL_TEMPS 0x3FF
125
126/* How often do we reread sensors values? (In jiffies) */
127#define SENSOR_REFRESH_INTERVAL (5 * HZ)
128
129/* How often do we reread sensor limit values? (In jiffies) */
130#define LIMIT_REFRESH_INTERVAL (60 * HZ)
131
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800132/* Wait at least 200ms per sensor for 10 sensors */
133#define TEMP_COLLECTION_TIME 2000
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700134
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700135/* datasheet says to divide this number by the fan reading to get fan rpm */
136#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
137#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
138#define FAN_PERIOD_INVALID 65535
139#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
140
141struct adt7470_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700142 struct device *hwmon_dev;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700143 struct attribute_group attrs;
144 struct mutex lock;
145 char sensors_valid;
146 char limits_valid;
147 unsigned long sensors_last_updated; /* In jiffies */
148 unsigned long limits_last_updated; /* In jiffies */
149
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800150 int num_temp_sensors; /* -1 = probe */
151
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700152 s8 temp[ADT7470_TEMP_COUNT];
153 s8 temp_min[ADT7470_TEMP_COUNT];
154 s8 temp_max[ADT7470_TEMP_COUNT];
155 u16 fan[ADT7470_FAN_COUNT];
156 u16 fan_min[ADT7470_FAN_COUNT];
157 u16 fan_max[ADT7470_FAN_COUNT];
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800158 u16 alarm;
159 u16 alarms_mask;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700160 u8 force_pwm_max;
161 u8 pwm[ADT7470_PWM_COUNT];
162 u8 pwm_max[ADT7470_PWM_COUNT];
163 u8 pwm_automatic[ADT7470_PWM_COUNT];
164 u8 pwm_min[ADT7470_PWM_COUNT];
165 s8 pwm_tmin[ADT7470_PWM_COUNT];
166 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
167};
168
Jean Delvare008f1ca2008-07-16 19:30:10 +0200169static int adt7470_probe(struct i2c_client *client,
170 const struct i2c_device_id *id);
171static int adt7470_detect(struct i2c_client *client, int kind,
172 struct i2c_board_info *info);
173static int adt7470_remove(struct i2c_client *client);
174
175static const struct i2c_device_id adt7470_id[] = {
176 { "adt7470", adt7470 },
177 { }
178};
179MODULE_DEVICE_TABLE(i2c, adt7470_id);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700180
181static struct i2c_driver adt7470_driver = {
Jean Delvare008f1ca2008-07-16 19:30:10 +0200182 .class = I2C_CLASS_HWMON,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700183 .driver = {
184 .name = "adt7470",
185 },
Jean Delvare008f1ca2008-07-16 19:30:10 +0200186 .probe = adt7470_probe,
187 .remove = adt7470_remove,
188 .id_table = adt7470_id,
189 .detect = adt7470_detect,
190 .address_data = &addr_data,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700191};
192
193/*
194 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
195 * that the low byte must be read before the high byte.
196 */
197static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
198{
199 u16 foo;
200 foo = i2c_smbus_read_byte_data(client, reg);
201 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
202 return foo;
203}
204
205static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
206 u16 value)
207{
208 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
209 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
210}
211
212static void adt7470_init_client(struct i2c_client *client)
213{
214 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
215
216 if (reg < 0) {
217 dev_err(&client->dev, "cannot read configuration register\n");
218 } else {
219 /* start monitoring (and do a self-test) */
220 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
221 }
222}
223
224static struct adt7470_data *adt7470_update_device(struct device *dev)
225{
226 struct i2c_client *client = to_i2c_client(dev);
227 struct adt7470_data *data = i2c_get_clientdata(client);
228 unsigned long local_jiffies = jiffies;
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800229 u8 cfg, pwm[4], pwm_cfg[2];
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700230 int i;
231
232 mutex_lock(&data->lock);
233 if (time_before(local_jiffies, data->sensors_last_updated +
234 SENSOR_REFRESH_INTERVAL)
235 && data->sensors_valid)
236 goto no_sensor_update;
237
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800238 /* save pwm[1-4] config register */
239 pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
240 pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
241
242 /* set manual pwm to whatever it is set to now */
243 for (i = 0; i < ADT7470_FAN_COUNT; i++)
244 pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
245
246 /* put pwm in manual mode */
247 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
248 pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
249 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
250 pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
251
252 /* write pwm control to whatever it was */
253 for (i = 0; i < ADT7470_FAN_COUNT; i++)
254 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
255
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700256 /* start reading temperature sensors */
257 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
258 cfg |= 0x80;
259 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
260
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800261 /* Delay is 200ms * number of temp sensors. */
262 msleep((data->num_temp_sensors >= 0 ?
263 data->num_temp_sensors * 200 :
264 TEMP_COLLECTION_TIME));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700265
266 /* done reading temperature sensors */
267 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
268 cfg &= ~0x80;
269 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
270
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800271 /* restore pwm[1-4] config registers */
272 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
273 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
274
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700275 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
276 data->temp[i] = i2c_smbus_read_byte_data(client,
277 ADT7470_TEMP_REG(i));
278
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800279 /* Figure out the number of temp sensors */
280 if (data->num_temp_sensors < 0)
281 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
282 if (data->temp[i])
283 data->num_temp_sensors = i + 1;
284
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700285 for (i = 0; i < ADT7470_FAN_COUNT; i++)
286 data->fan[i] = adt7470_read_word_data(client,
287 ADT7470_REG_FAN(i));
288
289 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
290 int reg;
291 int reg_mask;
292
293 data->pwm[i] = i2c_smbus_read_byte_data(client,
294 ADT7470_REG_PWM(i));
295
296 if (i % 2)
297 reg_mask = ADT7470_PWM2_AUTO_MASK;
298 else
299 reg_mask = ADT7470_PWM1_AUTO_MASK;
300
301 reg = ADT7470_REG_PWM_CFG(i);
302 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
303 data->pwm_automatic[i] = 1;
304 else
305 data->pwm_automatic[i] = 0;
306
307 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
308 cfg = i2c_smbus_read_byte_data(client, reg);
309 if (!(i % 2))
310 data->pwm_auto_temp[i] = cfg >> 4;
311 else
312 data->pwm_auto_temp[i] = cfg & 0xF;
313 }
314
315 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
316 ADT7470_FSPD_MASK)
317 data->force_pwm_max = 1;
318 else
319 data->force_pwm_max = 0;
320
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800321 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
322 if (data->alarm & ADT7470_OOL_ALARM)
323 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
324 ADT7470_REG_ALARM2));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700325 data->alarms_mask = adt7470_read_word_data(client,
326 ADT7470_REG_ALARM1_MASK);
327
328 data->sensors_last_updated = local_jiffies;
329 data->sensors_valid = 1;
330
331no_sensor_update:
332 if (time_before(local_jiffies, data->limits_last_updated +
333 LIMIT_REFRESH_INTERVAL)
334 && data->limits_valid)
335 goto out;
336
337 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
338 data->temp_min[i] = i2c_smbus_read_byte_data(client,
339 ADT7470_TEMP_MIN_REG(i));
340 data->temp_max[i] = i2c_smbus_read_byte_data(client,
341 ADT7470_TEMP_MAX_REG(i));
342 }
343
344 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
345 data->fan_min[i] = adt7470_read_word_data(client,
346 ADT7470_REG_FAN_MIN(i));
347 data->fan_max[i] = adt7470_read_word_data(client,
348 ADT7470_REG_FAN_MAX(i));
349 }
350
351 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
352 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
353 ADT7470_REG_PWM_MAX(i));
354 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
355 ADT7470_REG_PWM_MIN(i));
356 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
357 ADT7470_REG_PWM_TMIN(i));
358 }
359
360 data->limits_last_updated = local_jiffies;
361 data->limits_valid = 1;
362
363out:
364 mutex_unlock(&data->lock);
365 return data;
366}
367
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800368static ssize_t show_num_temp_sensors(struct device *dev,
369 struct device_attribute *devattr,
370 char *buf)
371{
372 struct adt7470_data *data = adt7470_update_device(dev);
373 return sprintf(buf, "%d\n", data->num_temp_sensors);
374}
375
376static ssize_t set_num_temp_sensors(struct device *dev,
377 struct device_attribute *devattr,
378 const char *buf,
379 size_t count)
380{
381 struct i2c_client *client = to_i2c_client(dev);
382 struct adt7470_data *data = i2c_get_clientdata(client);
383 long temp;
384
385 if (strict_strtol(buf, 10, &temp))
386 return -EINVAL;
387
388 temp = SENSORS_LIMIT(temp, -1, 10);
389
390 mutex_lock(&data->lock);
391 data->num_temp_sensors = temp;
392 mutex_unlock(&data->lock);
393
394 return count;
395}
396
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700397static ssize_t show_temp_min(struct device *dev,
398 struct device_attribute *devattr,
399 char *buf)
400{
401 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
402 struct adt7470_data *data = adt7470_update_device(dev);
403 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
404}
405
406static ssize_t set_temp_min(struct device *dev,
407 struct device_attribute *devattr,
408 const char *buf,
409 size_t count)
410{
411 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
412 struct i2c_client *client = to_i2c_client(dev);
413 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800414 long temp;
415
416 if (strict_strtol(buf, 10, &temp))
417 return -EINVAL;
418
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800419 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800420 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700421
422 mutex_lock(&data->lock);
423 data->temp_min[attr->index] = temp;
424 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
425 temp);
426 mutex_unlock(&data->lock);
427
428 return count;
429}
430
431static ssize_t show_temp_max(struct device *dev,
432 struct device_attribute *devattr,
433 char *buf)
434{
435 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
436 struct adt7470_data *data = adt7470_update_device(dev);
437 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
438}
439
440static ssize_t set_temp_max(struct device *dev,
441 struct device_attribute *devattr,
442 const char *buf,
443 size_t count)
444{
445 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
446 struct i2c_client *client = to_i2c_client(dev);
447 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800448 long temp;
449
450 if (strict_strtol(buf, 10, &temp))
451 return -EINVAL;
452
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800453 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800454 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700455
456 mutex_lock(&data->lock);
457 data->temp_max[attr->index] = temp;
458 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
459 temp);
460 mutex_unlock(&data->lock);
461
462 return count;
463}
464
465static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
466 char *buf)
467{
468 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
469 struct adt7470_data *data = adt7470_update_device(dev);
470 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
471}
472
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800473static ssize_t show_alarm_mask(struct device *dev,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700474 struct device_attribute *devattr,
475 char *buf)
476{
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700477 struct adt7470_data *data = adt7470_update_device(dev);
478
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800479 return sprintf(buf, "%x\n", data->alarms_mask);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700480}
481
482static ssize_t show_fan_max(struct device *dev,
483 struct device_attribute *devattr,
484 char *buf)
485{
486 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
487 struct adt7470_data *data = adt7470_update_device(dev);
488
489 if (FAN_DATA_VALID(data->fan_max[attr->index]))
490 return sprintf(buf, "%d\n",
491 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
492 else
493 return sprintf(buf, "0\n");
494}
495
496static ssize_t set_fan_max(struct device *dev,
497 struct device_attribute *devattr,
498 const char *buf, size_t count)
499{
500 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
501 struct i2c_client *client = to_i2c_client(dev);
502 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800503 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700504
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800505 if (strict_strtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700506 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800507
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700508 temp = FAN_RPM_TO_PERIOD(temp);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800509 temp = SENSORS_LIMIT(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700510
511 mutex_lock(&data->lock);
512 data->fan_max[attr->index] = temp;
513 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
514 mutex_unlock(&data->lock);
515
516 return count;
517}
518
519static ssize_t show_fan_min(struct device *dev,
520 struct device_attribute *devattr,
521 char *buf)
522{
523 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
524 struct adt7470_data *data = adt7470_update_device(dev);
525
526 if (FAN_DATA_VALID(data->fan_min[attr->index]))
527 return sprintf(buf, "%d\n",
528 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
529 else
530 return sprintf(buf, "0\n");
531}
532
533static ssize_t set_fan_min(struct device *dev,
534 struct device_attribute *devattr,
535 const char *buf, size_t count)
536{
537 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
538 struct i2c_client *client = to_i2c_client(dev);
539 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800540 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700541
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800542 if (strict_strtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700543 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800544
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700545 temp = FAN_RPM_TO_PERIOD(temp);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800546 temp = SENSORS_LIMIT(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700547
548 mutex_lock(&data->lock);
549 data->fan_min[attr->index] = temp;
550 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
551 mutex_unlock(&data->lock);
552
553 return count;
554}
555
556static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
557 char *buf)
558{
559 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
560 struct adt7470_data *data = adt7470_update_device(dev);
561
562 if (FAN_DATA_VALID(data->fan[attr->index]))
563 return sprintf(buf, "%d\n",
564 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
565 else
566 return sprintf(buf, "0\n");
567}
568
569static ssize_t show_force_pwm_max(struct device *dev,
570 struct device_attribute *devattr,
571 char *buf)
572{
573 struct adt7470_data *data = adt7470_update_device(dev);
574 return sprintf(buf, "%d\n", data->force_pwm_max);
575}
576
577static ssize_t set_force_pwm_max(struct device *dev,
578 struct device_attribute *devattr,
579 const char *buf,
580 size_t count)
581{
582 struct i2c_client *client = to_i2c_client(dev);
583 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800584 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700585 u8 reg;
586
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800587 if (strict_strtol(buf, 10, &temp))
588 return -EINVAL;
589
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700590 mutex_lock(&data->lock);
591 data->force_pwm_max = temp;
592 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
593 if (temp)
594 reg |= ADT7470_FSPD_MASK;
595 else
596 reg &= ~ADT7470_FSPD_MASK;
597 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
598 mutex_unlock(&data->lock);
599
600 return count;
601}
602
603static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
604 char *buf)
605{
606 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
607 struct adt7470_data *data = adt7470_update_device(dev);
608 return sprintf(buf, "%d\n", data->pwm[attr->index]);
609}
610
611static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
612 const char *buf, size_t count)
613{
614 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
615 struct i2c_client *client = to_i2c_client(dev);
616 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800617 long temp;
618
619 if (strict_strtol(buf, 10, &temp))
620 return -EINVAL;
621
622 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700623
624 mutex_lock(&data->lock);
625 data->pwm[attr->index] = temp;
626 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
627 mutex_unlock(&data->lock);
628
629 return count;
630}
631
632static ssize_t show_pwm_max(struct device *dev,
633 struct device_attribute *devattr,
634 char *buf)
635{
636 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
637 struct adt7470_data *data = adt7470_update_device(dev);
638 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
639}
640
641static ssize_t set_pwm_max(struct device *dev,
642 struct device_attribute *devattr,
643 const char *buf,
644 size_t count)
645{
646 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
647 struct i2c_client *client = to_i2c_client(dev);
648 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800649 long temp;
650
651 if (strict_strtol(buf, 10, &temp))
652 return -EINVAL;
653
654 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700655
656 mutex_lock(&data->lock);
657 data->pwm_max[attr->index] = temp;
658 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
659 temp);
660 mutex_unlock(&data->lock);
661
662 return count;
663}
664
665static ssize_t show_pwm_min(struct device *dev,
666 struct device_attribute *devattr,
667 char *buf)
668{
669 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
670 struct adt7470_data *data = adt7470_update_device(dev);
671 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
672}
673
674static ssize_t set_pwm_min(struct device *dev,
675 struct device_attribute *devattr,
676 const char *buf,
677 size_t count)
678{
679 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
680 struct i2c_client *client = to_i2c_client(dev);
681 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800682 long temp;
683
684 if (strict_strtol(buf, 10, &temp))
685 return -EINVAL;
686
687 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700688
689 mutex_lock(&data->lock);
690 data->pwm_min[attr->index] = temp;
691 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
692 temp);
693 mutex_unlock(&data->lock);
694
695 return count;
696}
697
698static ssize_t show_pwm_tmax(struct device *dev,
699 struct device_attribute *devattr,
700 char *buf)
701{
702 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
703 struct adt7470_data *data = adt7470_update_device(dev);
704 /* the datasheet says that tmax = tmin + 20C */
705 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
706}
707
708static ssize_t show_pwm_tmin(struct device *dev,
709 struct device_attribute *devattr,
710 char *buf)
711{
712 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
713 struct adt7470_data *data = adt7470_update_device(dev);
714 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
715}
716
717static ssize_t set_pwm_tmin(struct device *dev,
718 struct device_attribute *devattr,
719 const char *buf,
720 size_t count)
721{
722 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
723 struct i2c_client *client = to_i2c_client(dev);
724 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800725 long temp;
726
727 if (strict_strtol(buf, 10, &temp))
728 return -EINVAL;
729
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800730 temp = DIV_ROUND_CLOSEST(temp, 1000);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800731 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700732
733 mutex_lock(&data->lock);
734 data->pwm_tmin[attr->index] = temp;
735 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
736 temp);
737 mutex_unlock(&data->lock);
738
739 return count;
740}
741
742static ssize_t show_pwm_auto(struct device *dev,
743 struct device_attribute *devattr,
744 char *buf)
745{
746 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
747 struct adt7470_data *data = adt7470_update_device(dev);
748 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
749}
750
751static ssize_t set_pwm_auto(struct device *dev,
752 struct device_attribute *devattr,
753 const char *buf,
754 size_t count)
755{
756 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
757 struct i2c_client *client = to_i2c_client(dev);
758 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700759 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
760 int pwm_auto_reg_mask;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800761 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700762 u8 reg;
763
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800764 if (strict_strtol(buf, 10, &temp))
765 return -EINVAL;
766
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700767 if (attr->index % 2)
768 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
769 else
770 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
771
772 if (temp != 2 && temp != 1)
773 return -EINVAL;
774 temp--;
775
776 mutex_lock(&data->lock);
777 data->pwm_automatic[attr->index] = temp;
778 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
779 if (temp)
780 reg |= pwm_auto_reg_mask;
781 else
782 reg &= ~pwm_auto_reg_mask;
783 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
784 mutex_unlock(&data->lock);
785
786 return count;
787}
788
789static ssize_t show_pwm_auto_temp(struct device *dev,
790 struct device_attribute *devattr,
791 char *buf)
792{
793 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
794 struct adt7470_data *data = adt7470_update_device(dev);
795 u8 ctrl = data->pwm_auto_temp[attr->index];
796
797 if (ctrl)
798 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
799 else
800 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
801}
802
803static int cvt_auto_temp(int input)
804{
805 if (input == ADT7470_PWM_ALL_TEMPS)
806 return 0;
Robert P. J. Dayce9c2f42007-11-06 03:21:42 -0500807 if (input < 1 || !is_power_of_2(input))
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700808 return -EINVAL;
809 return ilog2(input) + 1;
810}
811
812static ssize_t set_pwm_auto_temp(struct device *dev,
813 struct device_attribute *devattr,
814 const char *buf,
815 size_t count)
816{
817 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
818 struct i2c_client *client = to_i2c_client(dev);
819 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700820 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800821 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700822 u8 reg;
823
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800824 if (strict_strtol(buf, 10, &temp))
825 return -EINVAL;
826
827 temp = cvt_auto_temp(temp);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700828 if (temp < 0)
829 return temp;
830
831 mutex_lock(&data->lock);
832 data->pwm_automatic[attr->index] = temp;
833 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
834
835 if (!(attr->index % 2)) {
836 reg &= 0xF;
837 reg |= (temp << 4) & 0xF0;
838 } else {
839 reg &= 0xF0;
840 reg |= temp & 0xF;
841 }
842
843 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
844 mutex_unlock(&data->lock);
845
846 return count;
847}
848
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800849static ssize_t show_alarm(struct device *dev,
850 struct device_attribute *devattr,
851 char *buf)
852{
853 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
854 struct adt7470_data *data = adt7470_update_device(dev);
855
856 if (data->alarm & attr->index)
857 return sprintf(buf, "1\n");
858 else
859 return sprintf(buf, "0\n");
860}
861
862static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800863static DEVICE_ATTR(num_temp_sensors, S_IWUSR | S_IRUGO, show_num_temp_sensors,
864 set_num_temp_sensors);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700865
866static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
867 set_temp_max, 0);
868static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
869 set_temp_max, 1);
870static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
871 set_temp_max, 2);
872static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
873 set_temp_max, 3);
874static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
875 set_temp_max, 4);
876static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
877 set_temp_max, 5);
878static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
879 set_temp_max, 6);
880static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
881 set_temp_max, 7);
882static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
883 set_temp_max, 8);
884static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
885 set_temp_max, 9);
886
887static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
888 set_temp_min, 0);
889static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
890 set_temp_min, 1);
891static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
892 set_temp_min, 2);
893static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
894 set_temp_min, 3);
895static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
896 set_temp_min, 4);
897static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
898 set_temp_min, 5);
899static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
900 set_temp_min, 6);
901static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
902 set_temp_min, 7);
903static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
904 set_temp_min, 8);
905static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
906 set_temp_min, 9);
907
908static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
909static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
910static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
911static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
912static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
913static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
914static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
915static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
916static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
917static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
918
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800919static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
920 ADT7470_R1T_ALARM);
921static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
922 ADT7470_R2T_ALARM);
923static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
924 ADT7470_R3T_ALARM);
925static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
926 ADT7470_R4T_ALARM);
927static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
928 ADT7470_R5T_ALARM);
929static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
930 ADT7470_R6T_ALARM);
931static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
932 ADT7470_R7T_ALARM);
933static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
934 ALARM2(ADT7470_R8T_ALARM));
935static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
936 ALARM2(ADT7470_R9T_ALARM));
937static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
938 ALARM2(ADT7470_R10T_ALARM));
939
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700940static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
941 set_fan_max, 0);
942static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
943 set_fan_max, 1);
944static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
945 set_fan_max, 2);
946static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
947 set_fan_max, 3);
948
949static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
950 set_fan_min, 0);
951static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
952 set_fan_min, 1);
953static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
954 set_fan_min, 2);
955static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
956 set_fan_min, 3);
957
958static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
959static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
960static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
961static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
962
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800963static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
964 ALARM2(ADT7470_FAN1_ALARM));
965static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
966 ALARM2(ADT7470_FAN2_ALARM));
967static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
968 ALARM2(ADT7470_FAN3_ALARM));
969static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
970 ALARM2(ADT7470_FAN4_ALARM));
971
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700972static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
973 show_force_pwm_max, set_force_pwm_max, 0);
974
975static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
976static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
977static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
978static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
979
980static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
981 show_pwm_min, set_pwm_min, 0);
982static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
983 show_pwm_min, set_pwm_min, 1);
984static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
985 show_pwm_min, set_pwm_min, 2);
986static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
987 show_pwm_min, set_pwm_min, 3);
988
989static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
990 show_pwm_max, set_pwm_max, 0);
991static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
992 show_pwm_max, set_pwm_max, 1);
993static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
994 show_pwm_max, set_pwm_max, 2);
995static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
996 show_pwm_max, set_pwm_max, 3);
997
998static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
999 show_pwm_tmin, set_pwm_tmin, 0);
1000static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
1001 show_pwm_tmin, set_pwm_tmin, 1);
1002static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
1003 show_pwm_tmin, set_pwm_tmin, 2);
1004static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
1005 show_pwm_tmin, set_pwm_tmin, 3);
1006
1007static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1008 NULL, 0);
1009static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1010 NULL, 1);
1011static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1012 NULL, 2);
1013static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1014 NULL, 3);
1015
1016static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1017 set_pwm_auto, 0);
1018static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1019 set_pwm_auto, 1);
1020static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1021 set_pwm_auto, 2);
1022static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1023 set_pwm_auto, 3);
1024
1025static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1026 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1027static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1028 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1029static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1030 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1031static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
1032 show_pwm_auto_temp, set_pwm_auto_temp, 3);
1033
1034static struct attribute *adt7470_attr[] =
1035{
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001036 &dev_attr_alarm_mask.attr,
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001037 &dev_attr_num_temp_sensors.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001038 &sensor_dev_attr_temp1_max.dev_attr.attr,
1039 &sensor_dev_attr_temp2_max.dev_attr.attr,
1040 &sensor_dev_attr_temp3_max.dev_attr.attr,
1041 &sensor_dev_attr_temp4_max.dev_attr.attr,
1042 &sensor_dev_attr_temp5_max.dev_attr.attr,
1043 &sensor_dev_attr_temp6_max.dev_attr.attr,
1044 &sensor_dev_attr_temp7_max.dev_attr.attr,
1045 &sensor_dev_attr_temp8_max.dev_attr.attr,
1046 &sensor_dev_attr_temp9_max.dev_attr.attr,
1047 &sensor_dev_attr_temp10_max.dev_attr.attr,
1048 &sensor_dev_attr_temp1_min.dev_attr.attr,
1049 &sensor_dev_attr_temp2_min.dev_attr.attr,
1050 &sensor_dev_attr_temp3_min.dev_attr.attr,
1051 &sensor_dev_attr_temp4_min.dev_attr.attr,
1052 &sensor_dev_attr_temp5_min.dev_attr.attr,
1053 &sensor_dev_attr_temp6_min.dev_attr.attr,
1054 &sensor_dev_attr_temp7_min.dev_attr.attr,
1055 &sensor_dev_attr_temp8_min.dev_attr.attr,
1056 &sensor_dev_attr_temp9_min.dev_attr.attr,
1057 &sensor_dev_attr_temp10_min.dev_attr.attr,
1058 &sensor_dev_attr_temp1_input.dev_attr.attr,
1059 &sensor_dev_attr_temp2_input.dev_attr.attr,
1060 &sensor_dev_attr_temp3_input.dev_attr.attr,
1061 &sensor_dev_attr_temp4_input.dev_attr.attr,
1062 &sensor_dev_attr_temp5_input.dev_attr.attr,
1063 &sensor_dev_attr_temp6_input.dev_attr.attr,
1064 &sensor_dev_attr_temp7_input.dev_attr.attr,
1065 &sensor_dev_attr_temp8_input.dev_attr.attr,
1066 &sensor_dev_attr_temp9_input.dev_attr.attr,
1067 &sensor_dev_attr_temp10_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001068 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1069 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1070 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1071 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1072 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1073 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1074 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1075 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1076 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1077 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001078 &sensor_dev_attr_fan1_max.dev_attr.attr,
1079 &sensor_dev_attr_fan2_max.dev_attr.attr,
1080 &sensor_dev_attr_fan3_max.dev_attr.attr,
1081 &sensor_dev_attr_fan4_max.dev_attr.attr,
1082 &sensor_dev_attr_fan1_min.dev_attr.attr,
1083 &sensor_dev_attr_fan2_min.dev_attr.attr,
1084 &sensor_dev_attr_fan3_min.dev_attr.attr,
1085 &sensor_dev_attr_fan4_min.dev_attr.attr,
1086 &sensor_dev_attr_fan1_input.dev_attr.attr,
1087 &sensor_dev_attr_fan2_input.dev_attr.attr,
1088 &sensor_dev_attr_fan3_input.dev_attr.attr,
1089 &sensor_dev_attr_fan4_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001090 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1091 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1092 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1093 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001094 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1095 &sensor_dev_attr_pwm1.dev_attr.attr,
1096 &sensor_dev_attr_pwm2.dev_attr.attr,
1097 &sensor_dev_attr_pwm3.dev_attr.attr,
1098 &sensor_dev_attr_pwm4.dev_attr.attr,
1099 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1100 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1101 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1102 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1103 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1104 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1105 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1106 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1107 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1108 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1109 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1110 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1111 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1112 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1113 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1114 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1115 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1116 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1117 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1118 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1119 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1120 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1121 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1122 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1123 NULL
1124};
1125
Jean Delvare008f1ca2008-07-16 19:30:10 +02001126/* Return 0 if detection is successful, -ENODEV otherwise */
1127static int adt7470_detect(struct i2c_client *client, int kind,
1128 struct i2c_board_info *info)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001129{
Jean Delvare008f1ca2008-07-16 19:30:10 +02001130 struct i2c_adapter *adapter = client->adapter;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001131
1132 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001133 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001134
1135 if (kind <= 0) {
1136 int vendor, device, revision;
1137
1138 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
Jean Delvare008f1ca2008-07-16 19:30:10 +02001139 if (vendor != ADT7470_VENDOR)
1140 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001141
1142 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
Jean Delvare008f1ca2008-07-16 19:30:10 +02001143 if (device != ADT7470_DEVICE)
1144 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001145
1146 revision = i2c_smbus_read_byte_data(client,
1147 ADT7470_REG_REVISION);
Jean Delvare008f1ca2008-07-16 19:30:10 +02001148 if (revision != ADT7470_REVISION)
1149 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001150 } else
1151 dev_dbg(&adapter->dev, "detection forced\n");
1152
Jean Delvare008f1ca2008-07-16 19:30:10 +02001153 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001154
Jean Delvare008f1ca2008-07-16 19:30:10 +02001155 return 0;
1156}
1157
1158static int adt7470_probe(struct i2c_client *client,
1159 const struct i2c_device_id *id)
1160{
1161 struct adt7470_data *data;
1162 int err;
1163
1164 data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL);
1165 if (!data) {
1166 err = -ENOMEM;
1167 goto exit;
1168 }
1169
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001170 data->num_temp_sensors = -1;
1171
Jean Delvare008f1ca2008-07-16 19:30:10 +02001172 i2c_set_clientdata(client, data);
1173 mutex_init(&data->lock);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001174
1175 dev_info(&client->dev, "%s chip found\n", client->name);
1176
1177 /* Initialize the ADT7470 chip */
1178 adt7470_init_client(client);
1179
1180 /* Register sysfs hooks */
1181 data->attrs.attrs = adt7470_attr;
1182 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001183 goto exit_free;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001184
Tony Jones1beeffe2007-08-20 13:46:20 -07001185 data->hwmon_dev = hwmon_device_register(&client->dev);
1186 if (IS_ERR(data->hwmon_dev)) {
1187 err = PTR_ERR(data->hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001188 goto exit_remove;
1189 }
1190
1191 return 0;
1192
1193exit_remove:
1194 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001195exit_free:
1196 kfree(data);
1197exit:
1198 return err;
1199}
1200
Jean Delvare008f1ca2008-07-16 19:30:10 +02001201static int adt7470_remove(struct i2c_client *client)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001202{
1203 struct adt7470_data *data = i2c_get_clientdata(client);
1204
Tony Jones1beeffe2007-08-20 13:46:20 -07001205 hwmon_device_unregister(data->hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001206 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001207 kfree(data);
1208 return 0;
1209}
1210
1211static int __init adt7470_init(void)
1212{
1213 return i2c_add_driver(&adt7470_driver);
1214}
1215
1216static void __exit adt7470_exit(void)
1217{
1218 i2c_del_driver(&adt7470_driver);
1219}
1220
1221MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1222MODULE_DESCRIPTION("ADT7470 driver");
1223MODULE_LICENSE("GPL");
1224
1225module_init(adt7470_init);
1226module_exit(adt7470_exit);