blob: f5da39a68929ddf6fb119ca17321d04d12d81f8c [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 *
Darrick J. Wong5407e0512013-08-26 15:42:27 -07005 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07006 *
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 {
Axel Lin30485772014-07-16 23:12:54 +0800146 struct i2c_client *client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700147 struct mutex lock;
148 char sensors_valid;
149 char limits_valid;
150 unsigned long sensors_last_updated; /* In jiffies */
151 unsigned long limits_last_updated; /* In jiffies */
152
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800153 int num_temp_sensors; /* -1 = probe */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800154 int temperatures_probed;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800155
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700156 s8 temp[ADT7470_TEMP_COUNT];
157 s8 temp_min[ADT7470_TEMP_COUNT];
158 s8 temp_max[ADT7470_TEMP_COUNT];
159 u16 fan[ADT7470_FAN_COUNT];
160 u16 fan_min[ADT7470_FAN_COUNT];
161 u16 fan_max[ADT7470_FAN_COUNT];
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800162 u16 alarm;
163 u16 alarms_mask;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700164 u8 force_pwm_max;
165 u8 pwm[ADT7470_PWM_COUNT];
166 u8 pwm_max[ADT7470_PWM_COUNT];
167 u8 pwm_automatic[ADT7470_PWM_COUNT];
168 u8 pwm_min[ADT7470_PWM_COUNT];
169 s8 pwm_tmin[ADT7470_PWM_COUNT];
170 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
Darrick J. Wong89fac112009-01-06 14:41:34 -0800171
172 struct task_struct *auto_update;
173 struct completion auto_update_stop;
174 unsigned int auto_update_interval;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700175};
176
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700177/*
178 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
179 * that the low byte must be read before the high byte.
180 */
181static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
182{
183 u16 foo;
184 foo = i2c_smbus_read_byte_data(client, reg);
185 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
186 return foo;
187}
188
189static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
190 u16 value)
191{
192 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
Curt Brune93d783b2013-08-08 12:11:03 -0700193 || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700194}
195
Darrick J. Wong89fac112009-01-06 14:41:34 -0800196/* Probe for temperature sensors. Assumes lock is held */
197static int adt7470_read_temperatures(struct i2c_client *client,
198 struct adt7470_data *data)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700199{
Darrick J. Wong89fac112009-01-06 14:41:34 -0800200 unsigned long res;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700201 int i;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800202 u8 cfg, pwm[4], pwm_cfg[2];
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700203
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800204 /* save pwm[1-4] config register */
205 pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
206 pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
207
208 /* set manual pwm to whatever it is set to now */
209 for (i = 0; i < ADT7470_FAN_COUNT; i++)
210 pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
211
212 /* put pwm in manual mode */
213 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
214 pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
215 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
216 pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
217
218 /* write pwm control to whatever it was */
219 for (i = 0; i < ADT7470_FAN_COUNT; i++)
220 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
221
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700222 /* start reading temperature sensors */
223 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
224 cfg |= 0x80;
225 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
226
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800227 /* Delay is 200ms * number of temp sensors. */
Darrick J. Wong89fac112009-01-06 14:41:34 -0800228 res = msleep_interruptible((data->num_temp_sensors >= 0 ?
229 data->num_temp_sensors * 200 :
230 TEMP_COLLECTION_TIME));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700231
232 /* done reading temperature sensors */
233 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
234 cfg &= ~0x80;
235 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
236
Darrick J. Wong2e75a4b2009-01-06 14:41:32 -0800237 /* restore pwm[1-4] config registers */
238 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
239 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
240
Darrick J. Wong89fac112009-01-06 14:41:34 -0800241 if (res) {
Joe Perches2e991202010-10-20 06:51:27 +0000242 pr_err("ha ha, interrupted\n");
Darrick J. Wong89fac112009-01-06 14:41:34 -0800243 return -EAGAIN;
244 }
245
246 /* Only count fans if we have to */
247 if (data->num_temp_sensors >= 0)
248 return 0;
249
250 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700251 data->temp[i] = i2c_smbus_read_byte_data(client,
252 ADT7470_TEMP_REG(i));
Darrick J. Wong89fac112009-01-06 14:41:34 -0800253 if (data->temp[i])
254 data->num_temp_sensors = i + 1;
255 }
256 data->temperatures_probed = 1;
257 return 0;
258}
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700259
Darrick J. Wong89fac112009-01-06 14:41:34 -0800260static int adt7470_update_thread(void *p)
261{
262 struct i2c_client *client = p;
263 struct adt7470_data *data = i2c_get_clientdata(client);
264
265 while (!kthread_should_stop()) {
266 mutex_lock(&data->lock);
267 adt7470_read_temperatures(client, data);
268 mutex_unlock(&data->lock);
269 if (kthread_should_stop())
270 break;
271 msleep_interruptible(data->auto_update_interval);
272 }
273
274 complete_all(&data->auto_update_stop);
275 return 0;
276}
277
278static struct adt7470_data *adt7470_update_device(struct device *dev)
279{
Axel Lin30485772014-07-16 23:12:54 +0800280 struct adt7470_data *data = dev_get_drvdata(dev);
281 struct i2c_client *client = data->client;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800282 unsigned long local_jiffies = jiffies;
283 u8 cfg;
284 int i;
285 int need_sensors = 1;
286 int need_limits = 1;
287
288 /*
289 * Figure out if we need to update the shadow registers.
290 * Lockless means that we may occasionally report out of
291 * date data.
292 */
293 if (time_before(local_jiffies, data->sensors_last_updated +
294 SENSOR_REFRESH_INTERVAL) &&
295 data->sensors_valid)
296 need_sensors = 0;
297
298 if (time_before(local_jiffies, data->limits_last_updated +
299 LIMIT_REFRESH_INTERVAL) &&
300 data->limits_valid)
301 need_limits = 0;
302
303 if (!need_sensors && !need_limits)
304 return data;
305
306 mutex_lock(&data->lock);
307 if (!need_sensors)
308 goto no_sensor_update;
309
310 if (!data->temperatures_probed)
311 adt7470_read_temperatures(client, data);
312 else
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800313 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
Darrick J. Wong89fac112009-01-06 14:41:34 -0800314 data->temp[i] = i2c_smbus_read_byte_data(client,
315 ADT7470_TEMP_REG(i));
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800316
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700317 for (i = 0; i < ADT7470_FAN_COUNT; i++)
318 data->fan[i] = adt7470_read_word_data(client,
319 ADT7470_REG_FAN(i));
320
321 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
322 int reg;
323 int reg_mask;
324
325 data->pwm[i] = i2c_smbus_read_byte_data(client,
326 ADT7470_REG_PWM(i));
327
328 if (i % 2)
329 reg_mask = ADT7470_PWM2_AUTO_MASK;
330 else
331 reg_mask = ADT7470_PWM1_AUTO_MASK;
332
333 reg = ADT7470_REG_PWM_CFG(i);
334 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
335 data->pwm_automatic[i] = 1;
336 else
337 data->pwm_automatic[i] = 0;
338
339 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
340 cfg = i2c_smbus_read_byte_data(client, reg);
341 if (!(i % 2))
342 data->pwm_auto_temp[i] = cfg >> 4;
343 else
344 data->pwm_auto_temp[i] = cfg & 0xF;
345 }
346
347 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
348 ADT7470_FSPD_MASK)
349 data->force_pwm_max = 1;
350 else
351 data->force_pwm_max = 0;
352
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800353 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
354 if (data->alarm & ADT7470_OOL_ALARM)
355 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
356 ADT7470_REG_ALARM2));
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700357 data->alarms_mask = adt7470_read_word_data(client,
358 ADT7470_REG_ALARM1_MASK);
359
360 data->sensors_last_updated = local_jiffies;
361 data->sensors_valid = 1;
362
363no_sensor_update:
Darrick J. Wong89fac112009-01-06 14:41:34 -0800364 if (!need_limits)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700365 goto out;
366
367 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
368 data->temp_min[i] = i2c_smbus_read_byte_data(client,
369 ADT7470_TEMP_MIN_REG(i));
370 data->temp_max[i] = i2c_smbus_read_byte_data(client,
371 ADT7470_TEMP_MAX_REG(i));
372 }
373
374 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
375 data->fan_min[i] = adt7470_read_word_data(client,
376 ADT7470_REG_FAN_MIN(i));
377 data->fan_max[i] = adt7470_read_word_data(client,
378 ADT7470_REG_FAN_MAX(i));
379 }
380
381 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
382 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
383 ADT7470_REG_PWM_MAX(i));
384 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
385 ADT7470_REG_PWM_MIN(i));
386 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
387 ADT7470_REG_PWM_TMIN(i));
388 }
389
390 data->limits_last_updated = local_jiffies;
391 data->limits_valid = 1;
392
393out:
394 mutex_unlock(&data->lock);
395 return data;
396}
397
Darrick J. Wong89fac112009-01-06 14:41:34 -0800398static ssize_t show_auto_update_interval(struct device *dev,
399 struct device_attribute *devattr,
400 char *buf)
401{
402 struct adt7470_data *data = adt7470_update_device(dev);
403 return sprintf(buf, "%d\n", data->auto_update_interval);
404}
405
406static ssize_t set_auto_update_interval(struct device *dev,
407 struct device_attribute *devattr,
408 const char *buf,
409 size_t count)
410{
Axel Lin30485772014-07-16 23:12:54 +0800411 struct adt7470_data *data = dev_get_drvdata(dev);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800412 long temp;
413
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100414 if (kstrtol(buf, 10, &temp))
Darrick J. Wong89fac112009-01-06 14:41:34 -0800415 return -EINVAL;
416
Guenter Roeck2a844c12013-01-09 08:09:34 -0800417 temp = clamp_val(temp, 0, 60000);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800418
419 mutex_lock(&data->lock);
420 data->auto_update_interval = temp;
421 mutex_unlock(&data->lock);
422
423 return count;
424}
425
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800426static ssize_t show_num_temp_sensors(struct device *dev,
427 struct device_attribute *devattr,
428 char *buf)
429{
430 struct adt7470_data *data = adt7470_update_device(dev);
431 return sprintf(buf, "%d\n", data->num_temp_sensors);
432}
433
434static ssize_t set_num_temp_sensors(struct device *dev,
435 struct device_attribute *devattr,
436 const char *buf,
437 size_t count)
438{
Axel Lin30485772014-07-16 23:12:54 +0800439 struct adt7470_data *data = dev_get_drvdata(dev);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800440 long temp;
441
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100442 if (kstrtol(buf, 10, &temp))
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800443 return -EINVAL;
444
Guenter Roeck2a844c12013-01-09 08:09:34 -0800445 temp = clamp_val(temp, -1, 10);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800446
447 mutex_lock(&data->lock);
448 data->num_temp_sensors = temp;
Darrick J. Wong89fac112009-01-06 14:41:34 -0800449 if (temp < 0)
450 data->temperatures_probed = 0;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800451 mutex_unlock(&data->lock);
452
453 return count;
454}
455
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700456static ssize_t show_temp_min(struct device *dev,
457 struct device_attribute *devattr,
458 char *buf)
459{
460 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
461 struct adt7470_data *data = adt7470_update_device(dev);
462 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
463}
464
465static ssize_t set_temp_min(struct device *dev,
466 struct device_attribute *devattr,
467 const char *buf,
468 size_t count)
469{
470 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800471 struct adt7470_data *data = dev_get_drvdata(dev);
472 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800473 long temp;
474
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100475 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800476 return -EINVAL;
477
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800478 temp = DIV_ROUND_CLOSEST(temp, 1000);
Guenter Roeckde12d6f2014-07-16 17:40:31 -0700479 temp = clamp_val(temp, -128, 127);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700480
481 mutex_lock(&data->lock);
482 data->temp_min[attr->index] = temp;
483 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
484 temp);
485 mutex_unlock(&data->lock);
486
487 return count;
488}
489
490static ssize_t show_temp_max(struct device *dev,
491 struct device_attribute *devattr,
492 char *buf)
493{
494 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
495 struct adt7470_data *data = adt7470_update_device(dev);
496 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
497}
498
499static ssize_t set_temp_max(struct device *dev,
500 struct device_attribute *devattr,
501 const char *buf,
502 size_t count)
503{
504 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800505 struct adt7470_data *data = dev_get_drvdata(dev);
506 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800507 long temp;
508
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100509 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800510 return -EINVAL;
511
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800512 temp = DIV_ROUND_CLOSEST(temp, 1000);
Guenter Roeckde12d6f2014-07-16 17:40:31 -0700513 temp = clamp_val(temp, -128, 127);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700514
515 mutex_lock(&data->lock);
516 data->temp_max[attr->index] = temp;
517 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
518 temp);
519 mutex_unlock(&data->lock);
520
521 return count;
522}
523
524static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
525 char *buf)
526{
527 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
528 struct adt7470_data *data = adt7470_update_device(dev);
529 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
530}
531
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800532static ssize_t show_alarm_mask(struct device *dev,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700533 struct device_attribute *devattr,
534 char *buf)
535{
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700536 struct adt7470_data *data = adt7470_update_device(dev);
537
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800538 return sprintf(buf, "%x\n", data->alarms_mask);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700539}
540
541static ssize_t show_fan_max(struct device *dev,
542 struct device_attribute *devattr,
543 char *buf)
544{
545 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
546 struct adt7470_data *data = adt7470_update_device(dev);
547
548 if (FAN_DATA_VALID(data->fan_max[attr->index]))
549 return sprintf(buf, "%d\n",
550 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
551 else
552 return sprintf(buf, "0\n");
553}
554
555static ssize_t set_fan_max(struct device *dev,
556 struct device_attribute *devattr,
557 const char *buf, size_t count)
558{
559 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800560 struct adt7470_data *data = dev_get_drvdata(dev);
561 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800562 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700563
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100564 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700565 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800566
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700567 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800568 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700569
570 mutex_lock(&data->lock);
571 data->fan_max[attr->index] = temp;
572 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
573 mutex_unlock(&data->lock);
574
575 return count;
576}
577
578static ssize_t show_fan_min(struct device *dev,
579 struct device_attribute *devattr,
580 char *buf)
581{
582 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
583 struct adt7470_data *data = adt7470_update_device(dev);
584
585 if (FAN_DATA_VALID(data->fan_min[attr->index]))
586 return sprintf(buf, "%d\n",
587 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
588 else
589 return sprintf(buf, "0\n");
590}
591
592static ssize_t set_fan_min(struct device *dev,
593 struct device_attribute *devattr,
594 const char *buf, size_t count)
595{
596 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800597 struct adt7470_data *data = dev_get_drvdata(dev);
598 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800599 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700600
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100601 if (kstrtol(buf, 10, &temp) || !temp)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700602 return -EINVAL;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800603
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700604 temp = FAN_RPM_TO_PERIOD(temp);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800605 temp = clamp_val(temp, 1, 65534);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700606
607 mutex_lock(&data->lock);
608 data->fan_min[attr->index] = temp;
609 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
610 mutex_unlock(&data->lock);
611
612 return count;
613}
614
615static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
616 char *buf)
617{
618 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
619 struct adt7470_data *data = adt7470_update_device(dev);
620
621 if (FAN_DATA_VALID(data->fan[attr->index]))
622 return sprintf(buf, "%d\n",
623 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
624 else
625 return sprintf(buf, "0\n");
626}
627
628static ssize_t show_force_pwm_max(struct device *dev,
629 struct device_attribute *devattr,
630 char *buf)
631{
632 struct adt7470_data *data = adt7470_update_device(dev);
633 return sprintf(buf, "%d\n", data->force_pwm_max);
634}
635
636static ssize_t set_force_pwm_max(struct device *dev,
637 struct device_attribute *devattr,
638 const char *buf,
639 size_t count)
640{
Axel Lin30485772014-07-16 23:12:54 +0800641 struct adt7470_data *data = dev_get_drvdata(dev);
642 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800643 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700644 u8 reg;
645
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100646 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800647 return -EINVAL;
648
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700649 mutex_lock(&data->lock);
650 data->force_pwm_max = temp;
651 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
652 if (temp)
653 reg |= ADT7470_FSPD_MASK;
654 else
655 reg &= ~ADT7470_FSPD_MASK;
656 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
657 mutex_unlock(&data->lock);
658
659 return count;
660}
661
662static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
663 char *buf)
664{
665 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
666 struct adt7470_data *data = adt7470_update_device(dev);
667 return sprintf(buf, "%d\n", data->pwm[attr->index]);
668}
669
670static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
671 const char *buf, size_t count)
672{
673 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800674 struct adt7470_data *data = dev_get_drvdata(dev);
675 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800676 long temp;
677
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100678 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800679 return -EINVAL;
680
Guenter Roeck2a844c12013-01-09 08:09:34 -0800681 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700682
683 mutex_lock(&data->lock);
684 data->pwm[attr->index] = temp;
685 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
686 mutex_unlock(&data->lock);
687
688 return count;
689}
690
691static ssize_t show_pwm_max(struct device *dev,
692 struct device_attribute *devattr,
693 char *buf)
694{
695 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
696 struct adt7470_data *data = adt7470_update_device(dev);
697 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
698}
699
700static ssize_t set_pwm_max(struct device *dev,
701 struct device_attribute *devattr,
702 const char *buf,
703 size_t count)
704{
705 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800706 struct adt7470_data *data = dev_get_drvdata(dev);
707 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800708 long temp;
709
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100710 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800711 return -EINVAL;
712
Guenter Roeck2a844c12013-01-09 08:09:34 -0800713 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700714
715 mutex_lock(&data->lock);
716 data->pwm_max[attr->index] = temp;
717 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
718 temp);
719 mutex_unlock(&data->lock);
720
721 return count;
722}
723
724static ssize_t show_pwm_min(struct device *dev,
725 struct device_attribute *devattr,
726 char *buf)
727{
728 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
729 struct adt7470_data *data = adt7470_update_device(dev);
730 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
731}
732
733static ssize_t set_pwm_min(struct device *dev,
734 struct device_attribute *devattr,
735 const char *buf,
736 size_t count)
737{
738 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800739 struct adt7470_data *data = dev_get_drvdata(dev);
740 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800741 long temp;
742
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100743 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800744 return -EINVAL;
745
Guenter Roeck2a844c12013-01-09 08:09:34 -0800746 temp = clamp_val(temp, 0, 255);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700747
748 mutex_lock(&data->lock);
749 data->pwm_min[attr->index] = temp;
750 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
751 temp);
752 mutex_unlock(&data->lock);
753
754 return count;
755}
756
757static ssize_t show_pwm_tmax(struct device *dev,
758 struct device_attribute *devattr,
759 char *buf)
760{
761 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
762 struct adt7470_data *data = adt7470_update_device(dev);
763 /* the datasheet says that tmax = tmin + 20C */
764 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
765}
766
767static ssize_t show_pwm_tmin(struct device *dev,
768 struct device_attribute *devattr,
769 char *buf)
770{
771 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
772 struct adt7470_data *data = adt7470_update_device(dev);
773 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
774}
775
776static ssize_t set_pwm_tmin(struct device *dev,
777 struct device_attribute *devattr,
778 const char *buf,
779 size_t count)
780{
781 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800782 struct adt7470_data *data = dev_get_drvdata(dev);
783 struct i2c_client *client = data->client;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800784 long temp;
785
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100786 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800787 return -EINVAL;
788
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800789 temp = DIV_ROUND_CLOSEST(temp, 1000);
Guenter Roeckde12d6f2014-07-16 17:40:31 -0700790 temp = clamp_val(temp, -128, 127);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700791
792 mutex_lock(&data->lock);
793 data->pwm_tmin[attr->index] = temp;
794 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
795 temp);
796 mutex_unlock(&data->lock);
797
798 return count;
799}
800
801static ssize_t show_pwm_auto(struct device *dev,
802 struct device_attribute *devattr,
803 char *buf)
804{
805 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
806 struct adt7470_data *data = adt7470_update_device(dev);
807 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
808}
809
810static ssize_t set_pwm_auto(struct device *dev,
811 struct device_attribute *devattr,
812 const char *buf,
813 size_t count)
814{
815 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800816 struct adt7470_data *data = dev_get_drvdata(dev);
817 struct i2c_client *client = data->client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700818 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
819 int pwm_auto_reg_mask;
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800820 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700821 u8 reg;
822
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100823 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800824 return -EINVAL;
825
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700826 if (attr->index % 2)
827 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
828 else
829 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
830
831 if (temp != 2 && temp != 1)
832 return -EINVAL;
833 temp--;
834
835 mutex_lock(&data->lock);
836 data->pwm_automatic[attr->index] = temp;
837 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
838 if (temp)
839 reg |= pwm_auto_reg_mask;
840 else
841 reg &= ~pwm_auto_reg_mask;
842 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
843 mutex_unlock(&data->lock);
844
845 return count;
846}
847
848static ssize_t show_pwm_auto_temp(struct device *dev,
849 struct device_attribute *devattr,
850 char *buf)
851{
852 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
853 struct adt7470_data *data = adt7470_update_device(dev);
854 u8 ctrl = data->pwm_auto_temp[attr->index];
855
856 if (ctrl)
857 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
858 else
859 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
860}
861
862static int cvt_auto_temp(int input)
863{
864 if (input == ADT7470_PWM_ALL_TEMPS)
865 return 0;
Robert P. J. Dayce9c2f42007-11-06 03:21:42 -0500866 if (input < 1 || !is_power_of_2(input))
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700867 return -EINVAL;
868 return ilog2(input) + 1;
869}
870
871static ssize_t set_pwm_auto_temp(struct device *dev,
872 struct device_attribute *devattr,
873 const char *buf,
874 size_t count)
875{
876 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin30485772014-07-16 23:12:54 +0800877 struct adt7470_data *data = dev_get_drvdata(dev);
878 struct i2c_client *client = data->client;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700879 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800880 long temp;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700881 u8 reg;
882
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100883 if (kstrtol(buf, 10, &temp))
Darrick J. Wong05a9bd42008-11-12 13:26:57 -0800884 return -EINVAL;
885
886 temp = cvt_auto_temp(temp);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700887 if (temp < 0)
888 return temp;
889
890 mutex_lock(&data->lock);
891 data->pwm_automatic[attr->index] = temp;
892 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
893
894 if (!(attr->index % 2)) {
895 reg &= 0xF;
896 reg |= (temp << 4) & 0xF0;
897 } else {
898 reg &= 0xF0;
899 reg |= temp & 0xF;
900 }
901
902 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
903 mutex_unlock(&data->lock);
904
905 return count;
906}
907
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800908static ssize_t show_alarm(struct device *dev,
909 struct device_attribute *devattr,
910 char *buf)
911{
912 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
913 struct adt7470_data *data = adt7470_update_device(dev);
914
915 if (data->alarm & attr->index)
916 return sprintf(buf, "1\n");
917 else
918 return sprintf(buf, "0\n");
919}
920
921static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -0800922static DEVICE_ATTR(num_temp_sensors, S_IWUSR | S_IRUGO, show_num_temp_sensors,
923 set_num_temp_sensors);
Darrick J. Wong89fac112009-01-06 14:41:34 -0800924static DEVICE_ATTR(auto_update_interval, S_IWUSR | S_IRUGO,
925 show_auto_update_interval, set_auto_update_interval);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -0700926
927static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
928 set_temp_max, 0);
929static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
930 set_temp_max, 1);
931static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
932 set_temp_max, 2);
933static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
934 set_temp_max, 3);
935static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
936 set_temp_max, 4);
937static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
938 set_temp_max, 5);
939static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
940 set_temp_max, 6);
941static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
942 set_temp_max, 7);
943static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
944 set_temp_max, 8);
945static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
946 set_temp_max, 9);
947
948static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
949 set_temp_min, 0);
950static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
951 set_temp_min, 1);
952static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
953 set_temp_min, 2);
954static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
955 set_temp_min, 3);
956static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
957 set_temp_min, 4);
958static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
959 set_temp_min, 5);
960static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
961 set_temp_min, 6);
962static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
963 set_temp_min, 7);
964static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
965 set_temp_min, 8);
966static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
967 set_temp_min, 9);
968
969static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
970static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
971static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
972static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
973static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
974static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
975static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
976static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
977static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
978static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
979
Darrick J. Wongfe03f282007-12-19 14:11:25 -0800980static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
981 ADT7470_R1T_ALARM);
982static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
983 ADT7470_R2T_ALARM);
984static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
985 ADT7470_R3T_ALARM);
986static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
987 ADT7470_R4T_ALARM);
988static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
989 ADT7470_R5T_ALARM);
990static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
991 ADT7470_R6T_ALARM);
992static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
993 ADT7470_R7T_ALARM);
994static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
995 ALARM2(ADT7470_R8T_ALARM));
996static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
997 ALARM2(ADT7470_R9T_ALARM));
998static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
999 ALARM2(ADT7470_R10T_ALARM));
1000
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001001static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
1002 set_fan_max, 0);
1003static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
1004 set_fan_max, 1);
1005static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
1006 set_fan_max, 2);
1007static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
1008 set_fan_max, 3);
1009
1010static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1011 set_fan_min, 0);
1012static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1013 set_fan_min, 1);
1014static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1015 set_fan_min, 2);
1016static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1017 set_fan_min, 3);
1018
1019static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
1020static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
1021static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
1022static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
1023
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001024static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
1025 ALARM2(ADT7470_FAN1_ALARM));
1026static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
1027 ALARM2(ADT7470_FAN2_ALARM));
1028static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
1029 ALARM2(ADT7470_FAN3_ALARM));
1030static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
1031 ALARM2(ADT7470_FAN4_ALARM));
1032
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001033static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
1034 show_force_pwm_max, set_force_pwm_max, 0);
1035
1036static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
1037static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
1038static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
1039static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
1040
1041static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
1042 show_pwm_min, set_pwm_min, 0);
1043static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
1044 show_pwm_min, set_pwm_min, 1);
1045static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
1046 show_pwm_min, set_pwm_min, 2);
1047static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
1048 show_pwm_min, set_pwm_min, 3);
1049
1050static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
1051 show_pwm_max, set_pwm_max, 0);
1052static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
1053 show_pwm_max, set_pwm_max, 1);
1054static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
1055 show_pwm_max, set_pwm_max, 2);
1056static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
1057 show_pwm_max, set_pwm_max, 3);
1058
1059static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
1060 show_pwm_tmin, set_pwm_tmin, 0);
1061static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
1062 show_pwm_tmin, set_pwm_tmin, 1);
1063static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
1064 show_pwm_tmin, set_pwm_tmin, 2);
1065static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
1066 show_pwm_tmin, set_pwm_tmin, 3);
1067
1068static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1069 NULL, 0);
1070static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1071 NULL, 1);
1072static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1073 NULL, 2);
1074static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1075 NULL, 3);
1076
1077static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1078 set_pwm_auto, 0);
1079static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1080 set_pwm_auto, 1);
1081static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1082 set_pwm_auto, 2);
1083static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1084 set_pwm_auto, 3);
1085
1086static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1087 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1088static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1089 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1090static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1091 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1092static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
1093 show_pwm_auto_temp, set_pwm_auto_temp, 3);
1094
Axel Lin30485772014-07-16 23:12:54 +08001095static struct attribute *adt7470_attrs[] = {
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001096 &dev_attr_alarm_mask.attr,
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001097 &dev_attr_num_temp_sensors.attr,
Darrick J. Wong89fac112009-01-06 14:41:34 -08001098 &dev_attr_auto_update_interval.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001099 &sensor_dev_attr_temp1_max.dev_attr.attr,
1100 &sensor_dev_attr_temp2_max.dev_attr.attr,
1101 &sensor_dev_attr_temp3_max.dev_attr.attr,
1102 &sensor_dev_attr_temp4_max.dev_attr.attr,
1103 &sensor_dev_attr_temp5_max.dev_attr.attr,
1104 &sensor_dev_attr_temp6_max.dev_attr.attr,
1105 &sensor_dev_attr_temp7_max.dev_attr.attr,
1106 &sensor_dev_attr_temp8_max.dev_attr.attr,
1107 &sensor_dev_attr_temp9_max.dev_attr.attr,
1108 &sensor_dev_attr_temp10_max.dev_attr.attr,
1109 &sensor_dev_attr_temp1_min.dev_attr.attr,
1110 &sensor_dev_attr_temp2_min.dev_attr.attr,
1111 &sensor_dev_attr_temp3_min.dev_attr.attr,
1112 &sensor_dev_attr_temp4_min.dev_attr.attr,
1113 &sensor_dev_attr_temp5_min.dev_attr.attr,
1114 &sensor_dev_attr_temp6_min.dev_attr.attr,
1115 &sensor_dev_attr_temp7_min.dev_attr.attr,
1116 &sensor_dev_attr_temp8_min.dev_attr.attr,
1117 &sensor_dev_attr_temp9_min.dev_attr.attr,
1118 &sensor_dev_attr_temp10_min.dev_attr.attr,
1119 &sensor_dev_attr_temp1_input.dev_attr.attr,
1120 &sensor_dev_attr_temp2_input.dev_attr.attr,
1121 &sensor_dev_attr_temp3_input.dev_attr.attr,
1122 &sensor_dev_attr_temp4_input.dev_attr.attr,
1123 &sensor_dev_attr_temp5_input.dev_attr.attr,
1124 &sensor_dev_attr_temp6_input.dev_attr.attr,
1125 &sensor_dev_attr_temp7_input.dev_attr.attr,
1126 &sensor_dev_attr_temp8_input.dev_attr.attr,
1127 &sensor_dev_attr_temp9_input.dev_attr.attr,
1128 &sensor_dev_attr_temp10_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001129 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1130 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1131 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1132 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1133 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1134 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1135 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1136 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1137 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1138 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001139 &sensor_dev_attr_fan1_max.dev_attr.attr,
1140 &sensor_dev_attr_fan2_max.dev_attr.attr,
1141 &sensor_dev_attr_fan3_max.dev_attr.attr,
1142 &sensor_dev_attr_fan4_max.dev_attr.attr,
1143 &sensor_dev_attr_fan1_min.dev_attr.attr,
1144 &sensor_dev_attr_fan2_min.dev_attr.attr,
1145 &sensor_dev_attr_fan3_min.dev_attr.attr,
1146 &sensor_dev_attr_fan4_min.dev_attr.attr,
1147 &sensor_dev_attr_fan1_input.dev_attr.attr,
1148 &sensor_dev_attr_fan2_input.dev_attr.attr,
1149 &sensor_dev_attr_fan3_input.dev_attr.attr,
1150 &sensor_dev_attr_fan4_input.dev_attr.attr,
Darrick J. Wongfe03f282007-12-19 14:11:25 -08001151 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1152 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1153 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1154 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001155 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1156 &sensor_dev_attr_pwm1.dev_attr.attr,
1157 &sensor_dev_attr_pwm2.dev_attr.attr,
1158 &sensor_dev_attr_pwm3.dev_attr.attr,
1159 &sensor_dev_attr_pwm4.dev_attr.attr,
1160 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1161 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1162 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1163 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1164 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1165 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1166 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1167 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1168 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1169 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1170 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1171 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1172 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1173 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1174 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1175 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1176 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1177 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1178 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1179 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1180 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1181 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1182 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1183 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1184 NULL
1185};
1186
Axel Lin30485772014-07-16 23:12:54 +08001187ATTRIBUTE_GROUPS(adt7470);
1188
Jean Delvare008f1ca2008-07-16 19:30:10 +02001189/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001190static int adt7470_detect(struct i2c_client *client,
Jean Delvare008f1ca2008-07-16 19:30:10 +02001191 struct i2c_board_info *info)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001192{
Jean Delvare008f1ca2008-07-16 19:30:10 +02001193 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01001194 int vendor, device, revision;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001195
1196 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare008f1ca2008-07-16 19:30:10 +02001197 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001198
Jean Delvare52df6442009-12-09 20:35:57 +01001199 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1200 if (vendor != ADT7470_VENDOR)
1201 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001202
Jean Delvare52df6442009-12-09 20:35:57 +01001203 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1204 if (device != ADT7470_DEVICE)
1205 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001206
Jean Delvare52df6442009-12-09 20:35:57 +01001207 revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1208 if (revision != ADT7470_REVISION)
1209 return -ENODEV;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001210
Jean Delvare008f1ca2008-07-16 19:30:10 +02001211 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001212
Jean Delvare008f1ca2008-07-16 19:30:10 +02001213 return 0;
1214}
1215
Axel Lin9027d932014-07-16 23:12:05 +08001216static void adt7470_init_client(struct i2c_client *client)
1217{
1218 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
1219
1220 if (reg < 0) {
1221 dev_err(&client->dev, "cannot read configuration register\n");
1222 } else {
1223 /* start monitoring (and do a self-test) */
1224 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
1225 }
1226}
1227
Jean Delvare008f1ca2008-07-16 19:30:10 +02001228static int adt7470_probe(struct i2c_client *client,
1229 const struct i2c_device_id *id)
1230{
Axel Lin30485772014-07-16 23:12:54 +08001231 struct device *dev = &client->dev;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001232 struct adt7470_data *data;
Axel Lin30485772014-07-16 23:12:54 +08001233 struct device *hwmon_dev;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001234
Axel Lin30485772014-07-16 23:12:54 +08001235 data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
Guenter Roeck9cc7dcc2012-06-02 09:58:01 -07001236 if (!data)
1237 return -ENOMEM;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001238
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001239 data->num_temp_sensors = -1;
Darrick J. Wong89fac112009-01-06 14:41:34 -08001240 data->auto_update_interval = AUTO_UPDATE_INTERVAL;
Darrick J. Wong2f22d5d2009-01-06 14:41:33 -08001241
Jean Delvare008f1ca2008-07-16 19:30:10 +02001242 i2c_set_clientdata(client, data);
Axel Lin30485772014-07-16 23:12:54 +08001243 data->client = client;
Jean Delvare008f1ca2008-07-16 19:30:10 +02001244 mutex_init(&data->lock);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001245
1246 dev_info(&client->dev, "%s chip found\n", client->name);
1247
1248 /* Initialize the ADT7470 chip */
1249 adt7470_init_client(client);
1250
1251 /* Register sysfs hooks */
Axel Lin30485772014-07-16 23:12:54 +08001252 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1253 data,
1254 adt7470_groups);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001255
Axel Lin30485772014-07-16 23:12:54 +08001256 if (IS_ERR(hwmon_dev))
1257 return PTR_ERR(hwmon_dev);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001258
Darrick J. Wong89fac112009-01-06 14:41:34 -08001259 init_completion(&data->auto_update_stop);
Kees Cookf1701682013-07-03 15:04:58 -07001260 data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
Axel Lin30485772014-07-16 23:12:54 +08001261 dev_name(hwmon_dev));
Axel Linf7334b42010-11-08 00:11:33 -05001262 if (IS_ERR(data->auto_update)) {
Axel Lin30485772014-07-16 23:12:54 +08001263 return PTR_ERR(data->auto_update);
Axel Linf7334b42010-11-08 00:11:33 -05001264 }
Darrick J. Wong89fac112009-01-06 14:41:34 -08001265
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001266 return 0;
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001267}
1268
Jean Delvare008f1ca2008-07-16 19:30:10 +02001269static int adt7470_remove(struct i2c_client *client)
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001270{
1271 struct adt7470_data *data = i2c_get_clientdata(client);
1272
Darrick J. Wong89fac112009-01-06 14:41:34 -08001273 kthread_stop(data->auto_update);
1274 wait_for_completion(&data->auto_update_stop);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001275 return 0;
1276}
1277
Axel Lin9027d932014-07-16 23:12:05 +08001278static const struct i2c_device_id adt7470_id[] = {
1279 { "adt7470", 0 },
1280 { }
1281};
1282MODULE_DEVICE_TABLE(i2c, adt7470_id);
1283
1284static struct i2c_driver adt7470_driver = {
1285 .class = I2C_CLASS_HWMON,
1286 .driver = {
1287 .name = "adt7470",
1288 },
1289 .probe = adt7470_probe,
1290 .remove = adt7470_remove,
1291 .id_table = adt7470_id,
1292 .detect = adt7470_detect,
1293 .address_list = normal_i2c,
1294};
1295
Axel Linf0967ee2012-01-20 15:38:18 +08001296module_i2c_driver(adt7470_driver);
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001297
Darrick J. Wong5407e0512013-08-26 15:42:27 -07001298MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
Darrick J. Wong6f9703d2007-07-31 11:06:52 -07001299MODULE_DESCRIPTION("ADT7470 driver");
1300MODULE_LICENSE("GPL");