blob: 6099f50b28aaa80f74e9ac69373f73efe4e852f1 [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
Joe Perches2e991202010-10-20 06:51:27 +000022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070024#include <linux/module.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/err.h>
30#include <linux/mutex.h>
31#include <linux/delay.h>
32#include <linux/log2.h>
Darrick J. Wong89fac112009-01-06 14:41:34 -080033#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070035
36/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050037static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070038
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070039/* ADT7470 registers */
40#define ADT7470_REG_BASE_ADDR 0x20
41#define ADT7470_REG_TEMP_BASE_ADDR 0x20
42#define ADT7470_REG_TEMP_MAX_ADDR 0x29
43#define ADT7470_REG_FAN_BASE_ADDR 0x2A
44#define ADT7470_REG_FAN_MAX_ADDR 0x31
45#define ADT7470_REG_PWM_BASE_ADDR 0x32
46#define ADT7470_REG_PWM_MAX_ADDR 0x35
47#define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
48#define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
49#define ADT7470_REG_CFG 0x40
50#define ADT7470_FSPD_MASK 0x04
51#define ADT7470_REG_ALARM1 0x41
Darrick J. Wongfe03f282007-12-19 14:11:25 -080052#define ADT7470_R1T_ALARM 0x01
53#define ADT7470_R2T_ALARM 0x02
54#define ADT7470_R3T_ALARM 0x04
55#define ADT7470_R4T_ALARM 0x08
56#define ADT7470_R5T_ALARM 0x10
57#define ADT7470_R6T_ALARM 0x20
58#define ADT7470_R7T_ALARM 0x40
59#define ADT7470_OOL_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070060#define ADT7470_REG_ALARM2 0x42
Darrick J. Wongfe03f282007-12-19 14:11:25 -080061#define ADT7470_R8T_ALARM 0x01
62#define ADT7470_R9T_ALARM 0x02
63#define ADT7470_R10T_ALARM 0x04
64#define ADT7470_FAN1_ALARM 0x10
65#define ADT7470_FAN2_ALARM 0x20
66#define ADT7470_FAN3_ALARM 0x40
67#define ADT7470_FAN4_ALARM 0x80
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070068#define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
69#define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
70#define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
71#define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
72#define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
73#define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
74#define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
75#define ADT7470_REG_PWM12_CFG 0x68
76#define ADT7470_PWM2_AUTO_MASK 0x40
77#define ADT7470_PWM1_AUTO_MASK 0x80
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -080078#define ADT7470_PWM_AUTO_MASK 0xC0
Darrick J. Wong6f9703d2007-07-31 11:06:52 -070079#define ADT7470_REG_PWM34_CFG 0x69
80#define ADT7470_PWM3_AUTO_MASK 0x40
81#define ADT7470_PWM4_AUTO_MASK 0x80
82#define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
83#define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
84#define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
85#define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
86#define ADT7470_REG_ACOUSTICS12 0x75
87#define ADT7470_REG_ACOUSTICS34 0x76
88#define ADT7470_REG_DEVICE 0x3D
89#define ADT7470_REG_VENDOR 0x3E
90#define ADT7470_REG_REVISION 0x3F
91#define ADT7470_REG_ALARM1_MASK 0x72
92#define ADT7470_REG_ALARM2_MASK 0x73
93#define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
94#define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
95#define ADT7470_REG_MAX_ADDR 0x81
96
97#define ADT7470_TEMP_COUNT 10
98#define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
99#define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
100#define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
101 ((x) * 2) + 1)
102
103#define ADT7470_FAN_COUNT 4
104#define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
105#define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
106#define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
107
108#define ADT7470_PWM_COUNT 4
109#define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
110#define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
111#define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
112#define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
113#define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
114#define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
115 ((x) / 2))
116
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800117#define ALARM2(x) ((x) << 8)
118
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700119#define ADT7470_VENDOR 0x41
120#define ADT7470_DEVICE 0x70
121/* datasheet only mentions a revision 2 */
122#define ADT7470_REVISION 0x02
123
124/* "all temps" according to hwmon sysfs interface spec */
125#define ADT7470_PWM_ALL_TEMPS 0x3FF
126
127/* How often do we reread sensors values? (In jiffies) */
128#define SENSOR_REFRESH_INTERVAL (5 * HZ)
129
130/* How often do we reread sensor limit values? (In jiffies) */
131#define LIMIT_REFRESH_INTERVAL (60 * HZ)
132
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800133/* Wait at least 200ms per sensor for 10 sensors */
134#define TEMP_COLLECTION_TIME 2000
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700135
Darrick J. Wong89fac112009-01-06 14:41:34 -0800136/* auto update thing won't fire more than every 2s */
137#define AUTO_UPDATE_INTERVAL 2000
138
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700139/* datasheet says to divide this number by the fan reading to get fan rpm */
140#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
141#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
142#define FAN_PERIOD_INVALID 65535
143#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
144
145struct adt7470_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700146 struct device *hwmon_dev;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700147 struct attribute_group attrs;
148 struct mutex lock;
149 char sensors_valid;
150 char limits_valid;
151 unsigned long sensors_last_updated; /* In jiffies */
152 unsigned long limits_last_updated; /* In jiffies */
153
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800154 int num_temp_sensors; /* -1 = probe */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800155 int temperatures_probed;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800156
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700157 s8 temp[ADT7470_TEMP_COUNT];
158 s8 temp_min[ADT7470_TEMP_COUNT];
159 s8 temp_max[ADT7470_TEMP_COUNT];
160 u16 fan[ADT7470_FAN_COUNT];
161 u16 fan_min[ADT7470_FAN_COUNT];
162 u16 fan_max[ADT7470_FAN_COUNT];
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800163 u16 alarm;
164 u16 alarms_mask;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700165 u8 force_pwm_max;
166 u8 pwm[ADT7470_PWM_COUNT];
167 u8 pwm_max[ADT7470_PWM_COUNT];
168 u8 pwm_automatic[ADT7470_PWM_COUNT];
169 u8 pwm_min[ADT7470_PWM_COUNT];
170 s8 pwm_tmin[ADT7470_PWM_COUNT];
171 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
Darrick J. Wong89fac112009-01-06 14:41:34 -0800172
173 struct task_struct *auto_update;
174 struct completion auto_update_stop;
175 unsigned int auto_update_interval;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700176};
177
Jean Delvare008f1ca2008-07-16 19:30:10 +0200178static int adt7470_probe(struct i2c_client *client,
179 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100180static int adt7470_detect(struct i2c_client *client,
Jean Delvare008f1ca2008-07-16 19:30:10 +0200181 struct i2c_board_info *info);
182static int adt7470_remove(struct i2c_client *client);
183
184static const struct i2c_device_id adt7470_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100185 { "adt7470", 0 },
Jean Delvare008f1ca2008-07-16 19:30:10 +0200186 { }
187};
188MODULE_DEVICE_TABLE(i2c, adt7470_id);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700189
190static struct i2c_driver adt7470_driver = {
Jean Delvare008f1ca2008-07-16 19:30:10 +0200191 .class = I2C_CLASS_HWMON,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700192 .driver = {
193 .name = "adt7470",
194 },
Jean Delvare008f1ca2008-07-16 19:30:10 +0200195 .probe = adt7470_probe,
196 .remove = adt7470_remove,
197 .id_table = adt7470_id,
198 .detect = adt7470_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100199 .address_list = normal_i2c,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700200};
201
202/*
203 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
204 * that the low byte must be read before the high byte.
205 */
206static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
207{
208 u16 foo;
209 foo = i2c_smbus_read_byte_data(client, reg);
210 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
211 return foo;
212}
213
214static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
215 u16 value)
216{
217 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
Curt Brune93d783b2013-08-08 12:11:03 -0700218 || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700219}
220
221static void adt7470_init_client(struct i2c_client *client)
222{
223 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
224
225 if (reg < 0) {
226 dev_err(&client->dev, "cannot read configuration register\n");
227 } else {
228 /* start monitoring (and do a self-test) */
229 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
230 }
231}
232
Darrick J. Wong89fac112009-01-06 14:41:34 -0800233/* Probe for temperature sensors. Assumes lock is held */
234static int adt7470_read_temperatures(struct i2c_client *client,
235 struct adt7470_data *data)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700236{
Darrick J. Wong89fac112009-01-06 14:41:34 -0800237 unsigned long res;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700238 int i;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800239 u8 cfg, pwm[4], pwm_cfg[2];
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700240
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800241 /* save pwm[1-4] config register */
242 pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
243 pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
244
245 /* set manual pwm to whatever it is set to now */
246 for (i = 0; i < ADT7470_FAN_COUNT; i++)
247 pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
248
249 /* put pwm in manual mode */
250 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
251 pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
252 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
253 pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
254
255 /* write pwm control to whatever it was */
256 for (i = 0; i < ADT7470_FAN_COUNT; i++)
257 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
258
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700259 /* start reading temperature sensors */
260 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
261 cfg |= 0x80;
262 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
263
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800264 /* Delay is 200ms * number of temp sensors. */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800265 res = msleep_interruptible((data->num_temp_sensors >= 0 ?
266 data->num_temp_sensors * 200 :
267 TEMP_COLLECTION_TIME));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700268
269 /* done reading temperature sensors */
270 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
271 cfg &= ~0x80;
272 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
273
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800274 /* restore pwm[1-4] config registers */
275 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
276 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
277
Darrick J. Wong89fac112009-01-06 14:41:34 -0800278 if (res) {
Joe Perches2e991202010-10-20 06:51:27 +0000279 pr_err("ha ha, interrupted\n");
Darrick J. Wong89fac112009-01-06 14:41:34 -0800280 return -EAGAIN;
281 }
282
283 /* Only count fans if we have to */
284 if (data->num_temp_sensors >= 0)
285 return 0;
286
287 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700288 data->temp[i] = i2c_smbus_read_byte_data(client,
289 ADT7470_TEMP_REG(i));
Darrick J. Wong89fac112009-01-06 14:41:34 -0800290 if (data->temp[i])
291 data->num_temp_sensors = i + 1;
292 }
293 data->temperatures_probed = 1;
294 return 0;
295}
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700296
Darrick J. Wong89fac112009-01-06 14:41:34 -0800297static int adt7470_update_thread(void *p)
298{
299 struct i2c_client *client = p;
300 struct adt7470_data *data = i2c_get_clientdata(client);
301
302 while (!kthread_should_stop()) {
303 mutex_lock(&data->lock);
304 adt7470_read_temperatures(client, data);
305 mutex_unlock(&data->lock);
306 if (kthread_should_stop())
307 break;
308 msleep_interruptible(data->auto_update_interval);
309 }
310
311 complete_all(&data->auto_update_stop);
312 return 0;
313}
314
315static struct adt7470_data *adt7470_update_device(struct device *dev)
316{
317 struct i2c_client *client = to_i2c_client(dev);
318 struct adt7470_data *data = i2c_get_clientdata(client);
319 unsigned long local_jiffies = jiffies;
320 u8 cfg;
321 int i;
322 int need_sensors = 1;
323 int need_limits = 1;
324
325 /*
326 * Figure out if we need to update the shadow registers.
327 * Lockless means that we may occasionally report out of
328 * date data.
329 */
330 if (time_before(local_jiffies, data->sensors_last_updated +
331 SENSOR_REFRESH_INTERVAL) &&
332 data->sensors_valid)
333 need_sensors = 0;
334
335 if (time_before(local_jiffies, data->limits_last_updated +
336 LIMIT_REFRESH_INTERVAL) &&
337 data->limits_valid)
338 need_limits = 0;
339
340 if (!need_sensors && !need_limits)
341 return data;
342
343 mutex_lock(&data->lock);
344 if (!need_sensors)
345 goto no_sensor_update;
346
347 if (!data->temperatures_probed)
348 adt7470_read_temperatures(client, data);
349 else
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800350 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
Darrick J. Wong89fac112009-01-06 14:41:34 -0800351 data->temp[i] = i2c_smbus_read_byte_data(client,
352 ADT7470_TEMP_REG(i));
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800353
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700354 for (i = 0; i < ADT7470_FAN_COUNT; i++)
355 data->fan[i] = adt7470_read_word_data(client,
356 ADT7470_REG_FAN(i));
357
358 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
359 int reg;
360 int reg_mask;
361
362 data->pwm[i] = i2c_smbus_read_byte_data(client,
363 ADT7470_REG_PWM(i));
364
365 if (i % 2)
366 reg_mask = ADT7470_PWM2_AUTO_MASK;
367 else
368 reg_mask = ADT7470_PWM1_AUTO_MASK;
369
370 reg = ADT7470_REG_PWM_CFG(i);
371 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
372 data->pwm_automatic[i] = 1;
373 else
374 data->pwm_automatic[i] = 0;
375
376 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
377 cfg = i2c_smbus_read_byte_data(client, reg);
378 if (!(i % 2))
379 data->pwm_auto_temp[i] = cfg >> 4;
380 else
381 data->pwm_auto_temp[i] = cfg & 0xF;
382 }
383
384 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
385 ADT7470_FSPD_MASK)
386 data->force_pwm_max = 1;
387 else
388 data->force_pwm_max = 0;
389
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800390 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
391 if (data->alarm & ADT7470_OOL_ALARM)
392 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
393 ADT7470_REG_ALARM2));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700394 data->alarms_mask = adt7470_read_word_data(client,
395 ADT7470_REG_ALARM1_MASK);
396
397 data->sensors_last_updated = local_jiffies;
398 data->sensors_valid = 1;
399
400no_sensor_update:
Darrick J. Wong89fac112009-01-06 14:41:34 -0800401 if (!need_limits)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700402 goto out;
403
404 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
405 data->temp_min[i] = i2c_smbus_read_byte_data(client,
406 ADT7470_TEMP_MIN_REG(i));
407 data->temp_max[i] = i2c_smbus_read_byte_data(client,
408 ADT7470_TEMP_MAX_REG(i));
409 }
410
411 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
412 data->fan_min[i] = adt7470_read_word_data(client,
413 ADT7470_REG_FAN_MIN(i));
414 data->fan_max[i] = adt7470_read_word_data(client,
415 ADT7470_REG_FAN_MAX(i));
416 }
417
418 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
419 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
420 ADT7470_REG_PWM_MAX(i));
421 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
422 ADT7470_REG_PWM_MIN(i));
423 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
424 ADT7470_REG_PWM_TMIN(i));
425 }
426
427 data->limits_last_updated = local_jiffies;
428 data->limits_valid = 1;
429
430out:
431 mutex_unlock(&data->lock);
432 return data;
433}
434
Darrick J. Wong89fac112009-01-06 14:41:34 -0800435static ssize_t show_auto_update_interval(struct device *dev,
436 struct device_attribute *devattr,
437 char *buf)
438{
439 struct adt7470_data *data = adt7470_update_device(dev);
440 return sprintf(buf, "%d\n", data->auto_update_interval);
441}
442
443static ssize_t set_auto_update_interval(struct device *dev,
444 struct device_attribute *devattr,
445 const char *buf,
446 size_t count)
447{
448 struct i2c_client *client = to_i2c_client(dev);
449 struct adt7470_data *data = i2c_get_clientdata(client);
450 long temp;
451
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100452 if (kstrtol(buf, 10, &temp))
Darrick J. Wong89fac112009-01-06 14:41:34 -0800453 return -EINVAL;
454
Guenter Roeck2a844c12013-01-09 08:09:34 -0800455 temp = clamp_val(temp, 0, 60000);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800456
457 mutex_lock(&data->lock);
458 data->auto_update_interval = temp;
459 mutex_unlock(&data->lock);
460
461 return count;
462}
463
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800464static ssize_t show_num_temp_sensors(struct device *dev,
465 struct device_attribute *devattr,
466 char *buf)
467{
468 struct adt7470_data *data = adt7470_update_device(dev);
469 return sprintf(buf, "%d\n", data->num_temp_sensors);
470}
471
472static ssize_t set_num_temp_sensors(struct device *dev,
473 struct device_attribute *devattr,
474 const char *buf,
475 size_t count)
476{
477 struct i2c_client *client = to_i2c_client(dev);
478 struct adt7470_data *data = i2c_get_clientdata(client);
479 long temp;
480
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100481 if (kstrtol(buf, 10, &temp))
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800482 return -EINVAL;
483
Guenter Roeck2a844c12013-01-09 08:09:34 -0800484 temp = clamp_val(temp, -1, 10);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800485
486 mutex_lock(&data->lock);
487 data->num_temp_sensors = temp;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800488 if (temp < 0)
489 data->temperatures_probed = 0;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800490 mutex_unlock(&data->lock);
491
492 return count;
493}
494
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700495static ssize_t show_temp_min(struct device *dev,
496 struct device_attribute *devattr,
497 char *buf)
498{
499 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
500 struct adt7470_data *data = adt7470_update_device(dev);
501 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
502}
503
504static ssize_t set_temp_min(struct device *dev,
505 struct device_attribute *devattr,
506 const char *buf,
507 size_t count)
508{
509 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
510 struct i2c_client *client = to_i2c_client(dev);
511 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800512 long temp;
513
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100514 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800515 return -EINVAL;
516
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800517 temp = DIV_ROUND_CLOSEST(temp, 1000);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800518 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700519
520 mutex_lock(&data->lock);
521 data->temp_min[attr->index] = temp;
522 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
523 temp);
524 mutex_unlock(&data->lock);
525
526 return count;
527}
528
529static ssize_t show_temp_max(struct device *dev,
530 struct device_attribute *devattr,
531 char *buf)
532{
533 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
534 struct adt7470_data *data = adt7470_update_device(dev);
535 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
536}
537
538static ssize_t set_temp_max(struct device *dev,
539 struct device_attribute *devattr,
540 const char *buf,
541 size_t count)
542{
543 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
544 struct i2c_client *client = to_i2c_client(dev);
545 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800546 long temp;
547
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100548 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800549 return -EINVAL;
550
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800551 temp = DIV_ROUND_CLOSEST(temp, 1000);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800552 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700553
554 mutex_lock(&data->lock);
555 data->temp_max[attr->index] = temp;
556 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
557 temp);
558 mutex_unlock(&data->lock);
559
560 return count;
561}
562
563static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
564 char *buf)
565{
566 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
567 struct adt7470_data *data = adt7470_update_device(dev);
568 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
569}
570
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800571static ssize_t show_alarm_mask(struct device *dev,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700572 struct device_attribute *devattr,
573 char *buf)
574{
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700575 struct adt7470_data *data = adt7470_update_device(dev);
576
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800577 return sprintf(buf, "%x\n", data->alarms_mask);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700578}
579
580static ssize_t show_fan_max(struct device *dev,
581 struct device_attribute *devattr,
582 char *buf)
583{
584 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
585 struct adt7470_data *data = adt7470_update_device(dev);
586
587 if (FAN_DATA_VALID(data->fan_max[attr->index]))
588 return sprintf(buf, "%d\n",
589 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
590 else
591 return sprintf(buf, "0\n");
592}
593
594static ssize_t set_fan_max(struct device *dev,
595 struct device_attribute *devattr,
596 const char *buf, size_t count)
597{
598 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
599 struct i2c_client *client = to_i2c_client(dev);
600 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800601 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700602
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100603 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700604 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800605
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700606 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800607 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700608
609 mutex_lock(&data->lock);
610 data->fan_max[attr->index] = temp;
611 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
612 mutex_unlock(&data->lock);
613
614 return count;
615}
616
617static ssize_t show_fan_min(struct device *dev,
618 struct device_attribute *devattr,
619 char *buf)
620{
621 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
622 struct adt7470_data *data = adt7470_update_device(dev);
623
624 if (FAN_DATA_VALID(data->fan_min[attr->index]))
625 return sprintf(buf, "%d\n",
626 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
627 else
628 return sprintf(buf, "0\n");
629}
630
631static ssize_t set_fan_min(struct device *dev,
632 struct device_attribute *devattr,
633 const char *buf, size_t count)
634{
635 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
636 struct i2c_client *client = to_i2c_client(dev);
637 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800638 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700639
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100640 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700641 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800642
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700643 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800644 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700645
646 mutex_lock(&data->lock);
647 data->fan_min[attr->index] = temp;
648 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
649 mutex_unlock(&data->lock);
650
651 return count;
652}
653
654static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
655 char *buf)
656{
657 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
658 struct adt7470_data *data = adt7470_update_device(dev);
659
660 if (FAN_DATA_VALID(data->fan[attr->index]))
661 return sprintf(buf, "%d\n",
662 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
663 else
664 return sprintf(buf, "0\n");
665}
666
667static ssize_t show_force_pwm_max(struct device *dev,
668 struct device_attribute *devattr,
669 char *buf)
670{
671 struct adt7470_data *data = adt7470_update_device(dev);
672 return sprintf(buf, "%d\n", data->force_pwm_max);
673}
674
675static ssize_t set_force_pwm_max(struct device *dev,
676 struct device_attribute *devattr,
677 const char *buf,
678 size_t count)
679{
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;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700683 u8 reg;
684
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100685 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800686 return -EINVAL;
687
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700688 mutex_lock(&data->lock);
689 data->force_pwm_max = temp;
690 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
691 if (temp)
692 reg |= ADT7470_FSPD_MASK;
693 else
694 reg &= ~ADT7470_FSPD_MASK;
695 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
696 mutex_unlock(&data->lock);
697
698 return count;
699}
700
701static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
702 char *buf)
703{
704 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
705 struct adt7470_data *data = adt7470_update_device(dev);
706 return sprintf(buf, "%d\n", data->pwm[attr->index]);
707}
708
709static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
710 const char *buf, size_t count)
711{
712 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
713 struct i2c_client *client = to_i2c_client(dev);
714 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800715 long temp;
716
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100717 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800718 return -EINVAL;
719
Guenter Roeck2a844c12013-01-09 08:09:34 -0800720 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700721
722 mutex_lock(&data->lock);
723 data->pwm[attr->index] = temp;
724 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
725 mutex_unlock(&data->lock);
726
727 return count;
728}
729
730static ssize_t show_pwm_max(struct device *dev,
731 struct device_attribute *devattr,
732 char *buf)
733{
734 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
735 struct adt7470_data *data = adt7470_update_device(dev);
736 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
737}
738
739static ssize_t set_pwm_max(struct device *dev,
740 struct device_attribute *devattr,
741 const char *buf,
742 size_t count)
743{
744 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
745 struct i2c_client *client = to_i2c_client(dev);
746 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800747 long temp;
748
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100749 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800750 return -EINVAL;
751
Guenter Roeck2a844c12013-01-09 08:09:34 -0800752 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700753
754 mutex_lock(&data->lock);
755 data->pwm_max[attr->index] = temp;
756 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
757 temp);
758 mutex_unlock(&data->lock);
759
760 return count;
761}
762
763static ssize_t show_pwm_min(struct device *dev,
764 struct device_attribute *devattr,
765 char *buf)
766{
767 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
768 struct adt7470_data *data = adt7470_update_device(dev);
769 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
770}
771
772static ssize_t set_pwm_min(struct device *dev,
773 struct device_attribute *devattr,
774 const char *buf,
775 size_t count)
776{
777 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
778 struct i2c_client *client = to_i2c_client(dev);
779 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800780 long temp;
781
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100782 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800783 return -EINVAL;
784
Guenter Roeck2a844c12013-01-09 08:09:34 -0800785 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700786
787 mutex_lock(&data->lock);
788 data->pwm_min[attr->index] = temp;
789 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
790 temp);
791 mutex_unlock(&data->lock);
792
793 return count;
794}
795
796static ssize_t show_pwm_tmax(struct device *dev,
797 struct device_attribute *devattr,
798 char *buf)
799{
800 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
801 struct adt7470_data *data = adt7470_update_device(dev);
802 /* the datasheet says that tmax = tmin + 20C */
803 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
804}
805
806static ssize_t show_pwm_tmin(struct device *dev,
807 struct device_attribute *devattr,
808 char *buf)
809{
810 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
811 struct adt7470_data *data = adt7470_update_device(dev);
812 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
813}
814
815static ssize_t set_pwm_tmin(struct device *dev,
816 struct device_attribute *devattr,
817 const char *buf,
818 size_t count)
819{
820 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
821 struct i2c_client *client = to_i2c_client(dev);
822 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800823 long temp;
824
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100825 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800826 return -EINVAL;
827
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800828 temp = DIV_ROUND_CLOSEST(temp, 1000);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800829 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700830
831 mutex_lock(&data->lock);
832 data->pwm_tmin[attr->index] = temp;
833 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
834 temp);
835 mutex_unlock(&data->lock);
836
837 return count;
838}
839
840static ssize_t show_pwm_auto(struct device *dev,
841 struct device_attribute *devattr,
842 char *buf)
843{
844 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
845 struct adt7470_data *data = adt7470_update_device(dev);
846 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
847}
848
849static ssize_t set_pwm_auto(struct device *dev,
850 struct device_attribute *devattr,
851 const char *buf,
852 size_t count)
853{
854 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
855 struct i2c_client *client = to_i2c_client(dev);
856 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700857 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
858 int pwm_auto_reg_mask;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800859 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700860 u8 reg;
861
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100862 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800863 return -EINVAL;
864
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700865 if (attr->index % 2)
866 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
867 else
868 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
869
870 if (temp != 2 && temp != 1)
871 return -EINVAL;
872 temp--;
873
874 mutex_lock(&data->lock);
875 data->pwm_automatic[attr->index] = temp;
876 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
877 if (temp)
878 reg |= pwm_auto_reg_mask;
879 else
880 reg &= ~pwm_auto_reg_mask;
881 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
882 mutex_unlock(&data->lock);
883
884 return count;
885}
886
887static ssize_t show_pwm_auto_temp(struct device *dev,
888 struct device_attribute *devattr,
889 char *buf)
890{
891 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
892 struct adt7470_data *data = adt7470_update_device(dev);
893 u8 ctrl = data->pwm_auto_temp[attr->index];
894
895 if (ctrl)
896 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
897 else
898 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
899}
900
901static int cvt_auto_temp(int input)
902{
903 if (input == ADT7470_PWM_ALL_TEMPS)
904 return 0;
Robert P. J. Dayce9c2f42007-11-06 03:21:42 -0500905 if (input < 1 || !is_power_of_2(input))
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700906 return -EINVAL;
907 return ilog2(input) + 1;
908}
909
910static ssize_t set_pwm_auto_temp(struct device *dev,
911 struct device_attribute *devattr,
912 const char *buf,
913 size_t count)
914{
915 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
916 struct i2c_client *client = to_i2c_client(dev);
917 struct adt7470_data *data = i2c_get_clientdata(client);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700918 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800919 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700920 u8 reg;
921
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100922 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800923 return -EINVAL;
924
925 temp = cvt_auto_temp(temp);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700926 if (temp < 0)
927 return temp;
928
929 mutex_lock(&data->lock);
930 data->pwm_automatic[attr->index] = temp;
931 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
932
933 if (!(attr->index % 2)) {
934 reg &= 0xF;
935 reg |= (temp << 4) & 0xF0;
936 } else {
937 reg &= 0xF0;
938 reg |= temp & 0xF;
939 }
940
941 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
942 mutex_unlock(&data->lock);
943
944 return count;
945}
946
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800947static ssize_t show_alarm(struct device *dev,
948 struct device_attribute *devattr,
949 char *buf)
950{
951 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
952 struct adt7470_data *data = adt7470_update_device(dev);
953
954 if (data->alarm & attr->index)
955 return sprintf(buf, "1\n");
956 else
957 return sprintf(buf, "0\n");
958}
959
960static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800961static DEVICE_ATTR(num_temp_sensors, S_IWUSR | S_IRUGO, show_num_temp_sensors,
962 set_num_temp_sensors);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800963static DEVICE_ATTR(auto_update_interval, S_IWUSR | S_IRUGO,
964 show_auto_update_interval, set_auto_update_interval);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700965
966static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
967 set_temp_max, 0);
968static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
969 set_temp_max, 1);
970static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
971 set_temp_max, 2);
972static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
973 set_temp_max, 3);
974static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
975 set_temp_max, 4);
976static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
977 set_temp_max, 5);
978static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
979 set_temp_max, 6);
980static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
981 set_temp_max, 7);
982static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
983 set_temp_max, 8);
984static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
985 set_temp_max, 9);
986
987static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
988 set_temp_min, 0);
989static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
990 set_temp_min, 1);
991static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
992 set_temp_min, 2);
993static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
994 set_temp_min, 3);
995static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
996 set_temp_min, 4);
997static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
998 set_temp_min, 5);
999static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
1000 set_temp_min, 6);
1001static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
1002 set_temp_min, 7);
1003static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
1004 set_temp_min, 8);
1005static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
1006 set_temp_min, 9);
1007
1008static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1009static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1010static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1011static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
1012static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
1013static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
1014static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
1015static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
1016static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
1017static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
1018
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001019static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1020 ADT7470_R1T_ALARM);
1021static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1022 ADT7470_R2T_ALARM);
1023static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1024 ADT7470_R3T_ALARM);
1025static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1026 ADT7470_R4T_ALARM);
1027static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1028 ADT7470_R5T_ALARM);
1029static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1030 ADT7470_R6T_ALARM);
1031static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
1032 ADT7470_R7T_ALARM);
1033static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
1034 ALARM2(ADT7470_R8T_ALARM));
1035static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
1036 ALARM2(ADT7470_R9T_ALARM));
1037static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
1038 ALARM2(ADT7470_R10T_ALARM));
1039
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001040static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
1041 set_fan_max, 0);
1042static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
1043 set_fan_max, 1);
1044static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
1045 set_fan_max, 2);
1046static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
1047 set_fan_max, 3);
1048
1049static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1050 set_fan_min, 0);
1051static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1052 set_fan_min, 1);
1053static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1054 set_fan_min, 2);
1055static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1056 set_fan_min, 3);
1057
1058static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
1059static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
1060static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
1061static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
1062
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001063static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
1064 ALARM2(ADT7470_FAN1_ALARM));
1065static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
1066 ALARM2(ADT7470_FAN2_ALARM));
1067static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
1068 ALARM2(ADT7470_FAN3_ALARM));
1069static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
1070 ALARM2(ADT7470_FAN4_ALARM));
1071
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001072static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
1073 show_force_pwm_max, set_force_pwm_max, 0);
1074
1075static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
1076static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
1077static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
1078static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
1079
1080static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
1081 show_pwm_min, set_pwm_min, 0);
1082static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
1083 show_pwm_min, set_pwm_min, 1);
1084static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
1085 show_pwm_min, set_pwm_min, 2);
1086static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
1087 show_pwm_min, set_pwm_min, 3);
1088
1089static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
1090 show_pwm_max, set_pwm_max, 0);
1091static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
1092 show_pwm_max, set_pwm_max, 1);
1093static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
1094 show_pwm_max, set_pwm_max, 2);
1095static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
1096 show_pwm_max, set_pwm_max, 3);
1097
1098static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
1099 show_pwm_tmin, set_pwm_tmin, 0);
1100static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
1101 show_pwm_tmin, set_pwm_tmin, 1);
1102static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
1103 show_pwm_tmin, set_pwm_tmin, 2);
1104static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
1105 show_pwm_tmin, set_pwm_tmin, 3);
1106
1107static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1108 NULL, 0);
1109static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1110 NULL, 1);
1111static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1112 NULL, 2);
1113static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1114 NULL, 3);
1115
1116static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1117 set_pwm_auto, 0);
1118static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1119 set_pwm_auto, 1);
1120static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1121 set_pwm_auto, 2);
1122static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1123 set_pwm_auto, 3);
1124
1125static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1126 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1127static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1128 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1129static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1130 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1131static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
1132 show_pwm_auto_temp, set_pwm_auto_temp, 3);
1133
Frans Meulenbroeks6b904b62012-01-08 19:34:13 +01001134static struct attribute *adt7470_attr[] = {
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001135 &dev_attr_alarm_mask.attr,
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001136 &dev_attr_num_temp_sensors.attr,
Darrick J. Wong89fac112009-01-06 14:41:34 -08001137 &dev_attr_auto_update_interval.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001138 &sensor_dev_attr_temp1_max.dev_attr.attr,
1139 &sensor_dev_attr_temp2_max.dev_attr.attr,
1140 &sensor_dev_attr_temp3_max.dev_attr.attr,
1141 &sensor_dev_attr_temp4_max.dev_attr.attr,
1142 &sensor_dev_attr_temp5_max.dev_attr.attr,
1143 &sensor_dev_attr_temp6_max.dev_attr.attr,
1144 &sensor_dev_attr_temp7_max.dev_attr.attr,
1145 &sensor_dev_attr_temp8_max.dev_attr.attr,
1146 &sensor_dev_attr_temp9_max.dev_attr.attr,
1147 &sensor_dev_attr_temp10_max.dev_attr.attr,
1148 &sensor_dev_attr_temp1_min.dev_attr.attr,
1149 &sensor_dev_attr_temp2_min.dev_attr.attr,
1150 &sensor_dev_attr_temp3_min.dev_attr.attr,
1151 &sensor_dev_attr_temp4_min.dev_attr.attr,
1152 &sensor_dev_attr_temp5_min.dev_attr.attr,
1153 &sensor_dev_attr_temp6_min.dev_attr.attr,
1154 &sensor_dev_attr_temp7_min.dev_attr.attr,
1155 &sensor_dev_attr_temp8_min.dev_attr.attr,
1156 &sensor_dev_attr_temp9_min.dev_attr.attr,
1157 &sensor_dev_attr_temp10_min.dev_attr.attr,
1158 &sensor_dev_attr_temp1_input.dev_attr.attr,
1159 &sensor_dev_attr_temp2_input.dev_attr.attr,
1160 &sensor_dev_attr_temp3_input.dev_attr.attr,
1161 &sensor_dev_attr_temp4_input.dev_attr.attr,
1162 &sensor_dev_attr_temp5_input.dev_attr.attr,
1163 &sensor_dev_attr_temp6_input.dev_attr.attr,
1164 &sensor_dev_attr_temp7_input.dev_attr.attr,
1165 &sensor_dev_attr_temp8_input.dev_attr.attr,
1166 &sensor_dev_attr_temp9_input.dev_attr.attr,
1167 &sensor_dev_attr_temp10_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001168 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1169 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1170 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1171 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1172 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1173 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1174 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1175 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1176 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1177 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001178 &sensor_dev_attr_fan1_max.dev_attr.attr,
1179 &sensor_dev_attr_fan2_max.dev_attr.attr,
1180 &sensor_dev_attr_fan3_max.dev_attr.attr,
1181 &sensor_dev_attr_fan4_max.dev_attr.attr,
1182 &sensor_dev_attr_fan1_min.dev_attr.attr,
1183 &sensor_dev_attr_fan2_min.dev_attr.attr,
1184 &sensor_dev_attr_fan3_min.dev_attr.attr,
1185 &sensor_dev_attr_fan4_min.dev_attr.attr,
1186 &sensor_dev_attr_fan1_input.dev_attr.attr,
1187 &sensor_dev_attr_fan2_input.dev_attr.attr,
1188 &sensor_dev_attr_fan3_input.dev_attr.attr,
1189 &sensor_dev_attr_fan4_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001190 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1191 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1192 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1193 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001194 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1195 &sensor_dev_attr_pwm1.dev_attr.attr,
1196 &sensor_dev_attr_pwm2.dev_attr.attr,
1197 &sensor_dev_attr_pwm3.dev_attr.attr,
1198 &sensor_dev_attr_pwm4.dev_attr.attr,
1199 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1200 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1201 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1202 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1203 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1204 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1205 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1206 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1207 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1208 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1209 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1210 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1211 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1212 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1213 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1214 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1215 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1216 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1217 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1218 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1219 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1220 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1221 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1222 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1223 NULL
1224};
1225
Jean Delvare008f1ca2008-07-16 19:30:10 +02001226/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001227static int adt7470_detect(struct i2c_client *client,
Jean Delvare008f1ca2008-07-16 19:30:10 +02001228 struct i2c_board_info *info)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001229{
Jean Delvare008f1ca2008-07-16 19:30:10 +02001230 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01001231 int vendor, device, revision;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001232
1233 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001234 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001235
Jean Delvare52df6442009-12-09 20:35:57 +01001236 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1237 if (vendor != ADT7470_VENDOR)
1238 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001239
Jean Delvare52df6442009-12-09 20:35:57 +01001240 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1241 if (device != ADT7470_DEVICE)
1242 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001243
Jean Delvare52df6442009-12-09 20:35:57 +01001244 revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1245 if (revision != ADT7470_REVISION)
1246 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001247
Jean Delvare008f1ca2008-07-16 19:30:10 +02001248 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001249
Jean Delvare008f1ca2008-07-16 19:30:10 +02001250 return 0;
1251}
1252
1253static int adt7470_probe(struct i2c_client *client,
1254 const struct i2c_device_id *id)
1255{
1256 struct adt7470_data *data;
1257 int err;
1258
Guenter Roeck9cc7dcc2012-06-02 09:58:01 -07001259 data = devm_kzalloc(&client->dev, sizeof(struct adt7470_data),
1260 GFP_KERNEL);
1261 if (!data)
1262 return -ENOMEM;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001263
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001264 data->num_temp_sensors = -1;
Darrick J. Wong89fac112009-01-06 14:41:34 -08001265 data->auto_update_interval = AUTO_UPDATE_INTERVAL;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001266
Jean Delvare008f1ca2008-07-16 19:30:10 +02001267 i2c_set_clientdata(client, data);
1268 mutex_init(&data->lock);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001269
1270 dev_info(&client->dev, "%s chip found\n", client->name);
1271
1272 /* Initialize the ADT7470 chip */
1273 adt7470_init_client(client);
1274
1275 /* Register sysfs hooks */
1276 data->attrs.attrs = adt7470_attr;
Frans Meulenbroeks6b904b62012-01-08 19:34:13 +01001277 err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1278 if (err)
Guenter Roeck9cc7dcc2012-06-02 09:58:01 -07001279 return err;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001280
Tony Jones1beeffe2007-08-20 13:46:20 -07001281 data->hwmon_dev = hwmon_device_register(&client->dev);
1282 if (IS_ERR(data->hwmon_dev)) {
1283 err = PTR_ERR(data->hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001284 goto exit_remove;
1285 }
1286
Darrick J. Wong89fac112009-01-06 14:41:34 -08001287 init_completion(&data->auto_update_stop);
Kees Cookf1701682013-07-03 15:04:58 -07001288 data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
Darrick J. Wong89fac112009-01-06 14:41:34 -08001289 dev_name(data->hwmon_dev));
Axel Linf7334b42010-11-08 00:11:33 -05001290 if (IS_ERR(data->auto_update)) {
1291 err = PTR_ERR(data->auto_update);
Darrick J. Wong89fac112009-01-06 14:41:34 -08001292 goto exit_unregister;
Axel Linf7334b42010-11-08 00:11:33 -05001293 }
Darrick J. Wong89fac112009-01-06 14:41:34 -08001294
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001295 return 0;
1296
Darrick J. Wong89fac112009-01-06 14:41:34 -08001297exit_unregister:
1298 hwmon_device_unregister(data->hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001299exit_remove:
1300 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001301 return err;
1302}
1303
Jean Delvare008f1ca2008-07-16 19:30:10 +02001304static int adt7470_remove(struct i2c_client *client)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001305{
1306 struct adt7470_data *data = i2c_get_clientdata(client);
1307
Darrick J. Wong89fac112009-01-06 14:41:34 -08001308 kthread_stop(data->auto_update);
1309 wait_for_completion(&data->auto_update_stop);
Tony Jones1beeffe2007-08-20 13:46:20 -07001310 hwmon_device_unregister(data->hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001311 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001312 return 0;
1313}
1314
Axel Linf0967ee2012-01-20 15:38:18 +08001315module_i2c_driver(adt7470_driver);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001316
1317MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1318MODULE_DESCRIPTION("ADT7470 driver");
1319MODULE_LICENSE("GPL");