xref: /openbmc/linux/drivers/hwmon/adt7470.c (revision ebd09753)
1 /*
2  * A hwmon driver for the Analog Devices ADT7470
3  * Copyright (C) 2007 IBM
4  *
5  * Author: Darrick J. Wong <darrick.wong@oracle.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 
24 #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>
33 #include <linux/kthread.h>
34 #include <linux/slab.h>
35 #include <linux/util_macros.h>
36 
37 /* Addresses to scan */
38 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
39 
40 /* ADT7470 registers */
41 #define ADT7470_REG_BASE_ADDR			0x20
42 #define ADT7470_REG_TEMP_BASE_ADDR		0x20
43 #define ADT7470_REG_TEMP_MAX_ADDR		0x29
44 #define ADT7470_REG_FAN_BASE_ADDR		0x2A
45 #define ADT7470_REG_FAN_MAX_ADDR		0x31
46 #define ADT7470_REG_PWM_BASE_ADDR		0x32
47 #define ADT7470_REG_PWM_MAX_ADDR		0x35
48 #define ADT7470_REG_PWM_MAX_BASE_ADDR		0x38
49 #define ADT7470_REG_PWM_MAX_MAX_ADDR		0x3B
50 #define ADT7470_REG_CFG				0x40
51 #define		ADT7470_FSPD_MASK		0x04
52 #define ADT7470_REG_ALARM1			0x41
53 #define		ADT7470_R1T_ALARM		0x01
54 #define		ADT7470_R2T_ALARM		0x02
55 #define		ADT7470_R3T_ALARM		0x04
56 #define		ADT7470_R4T_ALARM		0x08
57 #define		ADT7470_R5T_ALARM		0x10
58 #define		ADT7470_R6T_ALARM		0x20
59 #define		ADT7470_R7T_ALARM		0x40
60 #define		ADT7470_OOL_ALARM		0x80
61 #define ADT7470_REG_ALARM2			0x42
62 #define		ADT7470_R8T_ALARM		0x01
63 #define		ADT7470_R9T_ALARM		0x02
64 #define		ADT7470_R10T_ALARM		0x04
65 #define		ADT7470_FAN1_ALARM		0x10
66 #define		ADT7470_FAN2_ALARM		0x20
67 #define		ADT7470_FAN3_ALARM		0x40
68 #define		ADT7470_FAN4_ALARM		0x80
69 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR	0x44
70 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR	0x57
71 #define ADT7470_REG_FAN_MIN_BASE_ADDR		0x58
72 #define ADT7470_REG_FAN_MIN_MAX_ADDR		0x5F
73 #define ADT7470_REG_FAN_MAX_BASE_ADDR		0x60
74 #define ADT7470_REG_FAN_MAX_MAX_ADDR		0x67
75 #define ADT7470_REG_PWM_CFG_BASE_ADDR		0x68
76 #define ADT7470_REG_PWM12_CFG			0x68
77 #define		ADT7470_PWM2_AUTO_MASK		0x40
78 #define		ADT7470_PWM1_AUTO_MASK		0x80
79 #define		ADT7470_PWM_AUTO_MASK		0xC0
80 #define ADT7470_REG_PWM34_CFG			0x69
81 #define		ADT7470_PWM3_AUTO_MASK		0x40
82 #define		ADT7470_PWM4_AUTO_MASK		0x80
83 #define	ADT7470_REG_PWM_MIN_BASE_ADDR		0x6A
84 #define ADT7470_REG_PWM_MIN_MAX_ADDR		0x6D
85 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR	0x6E
86 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR	0x71
87 #define ADT7470_REG_CFG_2			0x74
88 #define ADT7470_REG_ACOUSTICS12			0x75
89 #define ADT7470_REG_ACOUSTICS34			0x76
90 #define ADT7470_REG_DEVICE			0x3D
91 #define ADT7470_REG_VENDOR			0x3E
92 #define ADT7470_REG_REVISION			0x3F
93 #define ADT7470_REG_ALARM1_MASK			0x72
94 #define ADT7470_REG_ALARM2_MASK			0x73
95 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR	0x7C
96 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR	0x7D
97 #define ADT7470_REG_MAX_ADDR			0x81
98 
99 #define ADT7470_TEMP_COUNT	10
100 #define ADT7470_TEMP_REG(x)	(ADT7470_REG_TEMP_BASE_ADDR + (x))
101 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
102 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
103 				((x) * 2) + 1)
104 
105 #define ADT7470_FAN_COUNT	4
106 #define ADT7470_REG_FAN(x)	(ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
107 #define ADT7470_REG_FAN_MIN(x)	(ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
108 #define ADT7470_REG_FAN_MAX(x)	(ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
109 
110 #define ADT7470_PWM_COUNT	4
111 #define ADT7470_REG_PWM(x)	(ADT7470_REG_PWM_BASE_ADDR + (x))
112 #define ADT7470_REG_PWM_MAX(x)	(ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
113 #define ADT7470_REG_PWM_MIN(x)	(ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
114 #define ADT7470_REG_PWM_TMIN(x)	(ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
115 #define ADT7470_REG_PWM_CFG(x)	(ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
116 #define ADT7470_REG_PWM_AUTO_TEMP(x)	(ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
117 					((x) / 2))
118 
119 #define ALARM2(x)		((x) << 8)
120 
121 #define ADT7470_VENDOR		0x41
122 #define ADT7470_DEVICE		0x70
123 /* datasheet only mentions a revision 2 */
124 #define ADT7470_REVISION	0x02
125 
126 /* "all temps" according to hwmon sysfs interface spec */
127 #define ADT7470_PWM_ALL_TEMPS	0x3FF
128 
129 /* How often do we reread sensors values? (In jiffies) */
130 #define SENSOR_REFRESH_INTERVAL	(5 * HZ)
131 
132 /* How often do we reread sensor limit values? (In jiffies) */
133 #define LIMIT_REFRESH_INTERVAL	(60 * HZ)
134 
135 /* Wait at least 200ms per sensor for 10 sensors */
136 #define TEMP_COLLECTION_TIME	2000
137 
138 /* auto update thing won't fire more than every 2s */
139 #define AUTO_UPDATE_INTERVAL	2000
140 
141 /* datasheet says to divide this number by the fan reading to get fan rpm */
142 #define FAN_PERIOD_TO_RPM(x)	((90000 * 60) / (x))
143 #define FAN_RPM_TO_PERIOD	FAN_PERIOD_TO_RPM
144 #define FAN_PERIOD_INVALID	65535
145 #define FAN_DATA_VALID(x)	((x) && (x) != FAN_PERIOD_INVALID)
146 
147 /* Config registers 1 and 2 include fields for selecting the PWM frequency */
148 #define ADT7470_CFG_LF		0x40
149 #define ADT7470_FREQ_MASK	0x70
150 #define ADT7470_FREQ_SHIFT	4
151 
152 struct adt7470_data {
153 	struct i2c_client	*client;
154 	struct mutex		lock;
155 	char			sensors_valid;
156 	char			limits_valid;
157 	unsigned long		sensors_last_updated;	/* In jiffies */
158 	unsigned long		limits_last_updated;	/* In jiffies */
159 
160 	int			num_temp_sensors;	/* -1 = probe */
161 	int			temperatures_probed;
162 
163 	s8			temp[ADT7470_TEMP_COUNT];
164 	s8			temp_min[ADT7470_TEMP_COUNT];
165 	s8			temp_max[ADT7470_TEMP_COUNT];
166 	u16			fan[ADT7470_FAN_COUNT];
167 	u16			fan_min[ADT7470_FAN_COUNT];
168 	u16			fan_max[ADT7470_FAN_COUNT];
169 	u16			alarm;
170 	u16			alarms_mask;
171 	u8			force_pwm_max;
172 	u8			pwm[ADT7470_PWM_COUNT];
173 	u8			pwm_max[ADT7470_PWM_COUNT];
174 	u8			pwm_automatic[ADT7470_PWM_COUNT];
175 	u8			pwm_min[ADT7470_PWM_COUNT];
176 	s8			pwm_tmin[ADT7470_PWM_COUNT];
177 	u8			pwm_auto_temp[ADT7470_PWM_COUNT];
178 
179 	struct task_struct	*auto_update;
180 	unsigned int		auto_update_interval;
181 };
182 
183 /*
184  * 16-bit registers on the ADT7470 are low-byte first.  The data sheet says
185  * that the low byte must be read before the high byte.
186  */
187 static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
188 {
189 	u16 foo;
190 	foo = i2c_smbus_read_byte_data(client, reg);
191 	foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
192 	return foo;
193 }
194 
195 static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
196 					  u16 value)
197 {
198 	return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
199 	       || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
200 }
201 
202 /* Probe for temperature sensors.  Assumes lock is held */
203 static int adt7470_read_temperatures(struct i2c_client *client,
204 				     struct adt7470_data *data)
205 {
206 	unsigned long res;
207 	int i;
208 	u8 cfg, pwm[4], pwm_cfg[2];
209 
210 	/* save pwm[1-4] config register */
211 	pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
212 	pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
213 
214 	/* set manual pwm to whatever it is set to now */
215 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
216 		pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
217 
218 	/* put pwm in manual mode */
219 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
220 		pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
221 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
222 		pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
223 
224 	/* write pwm control to whatever it was */
225 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
226 		i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
227 
228 	/* start reading temperature sensors */
229 	cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
230 	cfg |= 0x80;
231 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
232 
233 	/* Delay is 200ms * number of temp sensors. */
234 	res = msleep_interruptible((data->num_temp_sensors >= 0 ?
235 				    data->num_temp_sensors * 200 :
236 				    TEMP_COLLECTION_TIME));
237 
238 	/* done reading temperature sensors */
239 	cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
240 	cfg &= ~0x80;
241 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
242 
243 	/* restore pwm[1-4] config registers */
244 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
245 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
246 
247 	if (res) {
248 		pr_err("ha ha, interrupted\n");
249 		return -EAGAIN;
250 	}
251 
252 	/* Only count fans if we have to */
253 	if (data->num_temp_sensors >= 0)
254 		return 0;
255 
256 	for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
257 		data->temp[i] = i2c_smbus_read_byte_data(client,
258 						ADT7470_TEMP_REG(i));
259 		if (data->temp[i])
260 			data->num_temp_sensors = i + 1;
261 	}
262 	data->temperatures_probed = 1;
263 	return 0;
264 }
265 
266 static int adt7470_update_thread(void *p)
267 {
268 	struct i2c_client *client = p;
269 	struct adt7470_data *data = i2c_get_clientdata(client);
270 
271 	while (!kthread_should_stop()) {
272 		mutex_lock(&data->lock);
273 		adt7470_read_temperatures(client, data);
274 		mutex_unlock(&data->lock);
275 
276 		set_current_state(TASK_INTERRUPTIBLE);
277 		if (kthread_should_stop())
278 			break;
279 
280 		schedule_timeout(msecs_to_jiffies(data->auto_update_interval));
281 	}
282 
283 	return 0;
284 }
285 
286 static struct adt7470_data *adt7470_update_device(struct device *dev)
287 {
288 	struct adt7470_data *data = dev_get_drvdata(dev);
289 	struct i2c_client *client = data->client;
290 	unsigned long local_jiffies = jiffies;
291 	u8 cfg;
292 	int i;
293 	int need_sensors = 1;
294 	int need_limits = 1;
295 
296 	/*
297 	 * Figure out if we need to update the shadow registers.
298 	 * Lockless means that we may occasionally report out of
299 	 * date data.
300 	 */
301 	if (time_before(local_jiffies, data->sensors_last_updated +
302 			SENSOR_REFRESH_INTERVAL) &&
303 	    data->sensors_valid)
304 		need_sensors = 0;
305 
306 	if (time_before(local_jiffies, data->limits_last_updated +
307 			LIMIT_REFRESH_INTERVAL) &&
308 	    data->limits_valid)
309 		need_limits = 0;
310 
311 	if (!need_sensors && !need_limits)
312 		return data;
313 
314 	mutex_lock(&data->lock);
315 	if (!need_sensors)
316 		goto no_sensor_update;
317 
318 	if (!data->temperatures_probed)
319 		adt7470_read_temperatures(client, data);
320 	else
321 		for (i = 0; i < ADT7470_TEMP_COUNT; i++)
322 			data->temp[i] = i2c_smbus_read_byte_data(client,
323 						ADT7470_TEMP_REG(i));
324 
325 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
326 		data->fan[i] = adt7470_read_word_data(client,
327 						ADT7470_REG_FAN(i));
328 
329 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
330 		int reg;
331 		int reg_mask;
332 
333 		data->pwm[i] = i2c_smbus_read_byte_data(client,
334 						ADT7470_REG_PWM(i));
335 
336 		if (i % 2)
337 			reg_mask = ADT7470_PWM2_AUTO_MASK;
338 		else
339 			reg_mask = ADT7470_PWM1_AUTO_MASK;
340 
341 		reg = ADT7470_REG_PWM_CFG(i);
342 		if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
343 			data->pwm_automatic[i] = 1;
344 		else
345 			data->pwm_automatic[i] = 0;
346 
347 		reg = ADT7470_REG_PWM_AUTO_TEMP(i);
348 		cfg = i2c_smbus_read_byte_data(client, reg);
349 		if (!(i % 2))
350 			data->pwm_auto_temp[i] = cfg >> 4;
351 		else
352 			data->pwm_auto_temp[i] = cfg & 0xF;
353 	}
354 
355 	if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
356 	    ADT7470_FSPD_MASK)
357 		data->force_pwm_max = 1;
358 	else
359 		data->force_pwm_max = 0;
360 
361 	data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
362 	if (data->alarm & ADT7470_OOL_ALARM)
363 		data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
364 							ADT7470_REG_ALARM2));
365 	data->alarms_mask = adt7470_read_word_data(client,
366 						   ADT7470_REG_ALARM1_MASK);
367 
368 	data->sensors_last_updated = local_jiffies;
369 	data->sensors_valid = 1;
370 
371 no_sensor_update:
372 	if (!need_limits)
373 		goto out;
374 
375 	for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
376 		data->temp_min[i] = i2c_smbus_read_byte_data(client,
377 						ADT7470_TEMP_MIN_REG(i));
378 		data->temp_max[i] = i2c_smbus_read_byte_data(client,
379 						ADT7470_TEMP_MAX_REG(i));
380 	}
381 
382 	for (i = 0; i < ADT7470_FAN_COUNT; i++) {
383 		data->fan_min[i] = adt7470_read_word_data(client,
384 						ADT7470_REG_FAN_MIN(i));
385 		data->fan_max[i] = adt7470_read_word_data(client,
386 						ADT7470_REG_FAN_MAX(i));
387 	}
388 
389 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
390 		data->pwm_max[i] = i2c_smbus_read_byte_data(client,
391 						ADT7470_REG_PWM_MAX(i));
392 		data->pwm_min[i] = i2c_smbus_read_byte_data(client,
393 						ADT7470_REG_PWM_MIN(i));
394 		data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
395 						ADT7470_REG_PWM_TMIN(i));
396 	}
397 
398 	data->limits_last_updated = local_jiffies;
399 	data->limits_valid = 1;
400 
401 out:
402 	mutex_unlock(&data->lock);
403 	return data;
404 }
405 
406 static ssize_t auto_update_interval_show(struct device *dev,
407 					 struct device_attribute *devattr,
408 					 char *buf)
409 {
410 	struct adt7470_data *data = adt7470_update_device(dev);
411 	return sprintf(buf, "%d\n", data->auto_update_interval);
412 }
413 
414 static ssize_t auto_update_interval_store(struct device *dev,
415 					  struct device_attribute *devattr,
416 					  const char *buf, size_t count)
417 {
418 	struct adt7470_data *data = dev_get_drvdata(dev);
419 	long temp;
420 
421 	if (kstrtol(buf, 10, &temp))
422 		return -EINVAL;
423 
424 	temp = clamp_val(temp, 0, 60000);
425 
426 	mutex_lock(&data->lock);
427 	data->auto_update_interval = temp;
428 	mutex_unlock(&data->lock);
429 
430 	return count;
431 }
432 
433 static ssize_t num_temp_sensors_show(struct device *dev,
434 				     struct device_attribute *devattr,
435 				     char *buf)
436 {
437 	struct adt7470_data *data = adt7470_update_device(dev);
438 	return sprintf(buf, "%d\n", data->num_temp_sensors);
439 }
440 
441 static ssize_t num_temp_sensors_store(struct device *dev,
442 				      struct device_attribute *devattr,
443 				      const char *buf, size_t count)
444 {
445 	struct adt7470_data *data = dev_get_drvdata(dev);
446 	long temp;
447 
448 	if (kstrtol(buf, 10, &temp))
449 		return -EINVAL;
450 
451 	temp = clamp_val(temp, -1, 10);
452 
453 	mutex_lock(&data->lock);
454 	data->num_temp_sensors = temp;
455 	if (temp < 0)
456 		data->temperatures_probed = 0;
457 	mutex_unlock(&data->lock);
458 
459 	return count;
460 }
461 
462 static ssize_t show_temp_min(struct device *dev,
463 			     struct device_attribute *devattr,
464 			     char *buf)
465 {
466 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
467 	struct adt7470_data *data = adt7470_update_device(dev);
468 	return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
469 }
470 
471 static ssize_t set_temp_min(struct device *dev,
472 			    struct device_attribute *devattr,
473 			    const char *buf,
474 			    size_t count)
475 {
476 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
477 	struct adt7470_data *data = dev_get_drvdata(dev);
478 	struct i2c_client *client = data->client;
479 	long temp;
480 
481 	if (kstrtol(buf, 10, &temp))
482 		return -EINVAL;
483 
484 	temp = clamp_val(temp, -128000, 127000);
485 	temp = DIV_ROUND_CLOSEST(temp, 1000);
486 
487 	mutex_lock(&data->lock);
488 	data->temp_min[attr->index] = temp;
489 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
490 				  temp);
491 	mutex_unlock(&data->lock);
492 
493 	return count;
494 }
495 
496 static ssize_t show_temp_max(struct device *dev,
497 			     struct device_attribute *devattr,
498 			     char *buf)
499 {
500 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
501 	struct adt7470_data *data = adt7470_update_device(dev);
502 	return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
503 }
504 
505 static ssize_t set_temp_max(struct device *dev,
506 			    struct device_attribute *devattr,
507 			    const char *buf,
508 			    size_t count)
509 {
510 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
511 	struct adt7470_data *data = dev_get_drvdata(dev);
512 	struct i2c_client *client = data->client;
513 	long temp;
514 
515 	if (kstrtol(buf, 10, &temp))
516 		return -EINVAL;
517 
518 	temp = clamp_val(temp, -128000, 127000);
519 	temp = DIV_ROUND_CLOSEST(temp, 1000);
520 
521 	mutex_lock(&data->lock);
522 	data->temp_max[attr->index] = temp;
523 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
524 				  temp);
525 	mutex_unlock(&data->lock);
526 
527 	return count;
528 }
529 
530 static ssize_t show_temp(struct device *dev, 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[attr->index]);
536 }
537 
538 static ssize_t alarm_mask_show(struct device *dev,
539 			   struct device_attribute *devattr,
540 			   char *buf)
541 {
542 	struct adt7470_data *data = adt7470_update_device(dev);
543 
544 	return sprintf(buf, "%x\n", data->alarms_mask);
545 }
546 
547 static ssize_t alarm_mask_store(struct device *dev,
548 				struct device_attribute *devattr,
549 				const char *buf, size_t count)
550 {
551 	struct adt7470_data *data = dev_get_drvdata(dev);
552 	long mask;
553 
554 	if (kstrtoul(buf, 0, &mask))
555 		return -EINVAL;
556 
557 	if (mask & ~0xffff)
558 		return -EINVAL;
559 
560 	mutex_lock(&data->lock);
561 	data->alarms_mask = mask;
562 	adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
563 	mutex_unlock(&data->lock);
564 
565 	return count;
566 }
567 
568 static ssize_t show_fan_max(struct device *dev,
569 			    struct device_attribute *devattr,
570 			    char *buf)
571 {
572 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
573 	struct adt7470_data *data = adt7470_update_device(dev);
574 
575 	if (FAN_DATA_VALID(data->fan_max[attr->index]))
576 		return sprintf(buf, "%d\n",
577 			       FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
578 	else
579 		return sprintf(buf, "0\n");
580 }
581 
582 static ssize_t set_fan_max(struct device *dev,
583 			   struct device_attribute *devattr,
584 			   const char *buf, size_t count)
585 {
586 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
587 	struct adt7470_data *data = dev_get_drvdata(dev);
588 	struct i2c_client *client = data->client;
589 	long temp;
590 
591 	if (kstrtol(buf, 10, &temp) || !temp)
592 		return -EINVAL;
593 
594 	temp = FAN_RPM_TO_PERIOD(temp);
595 	temp = clamp_val(temp, 1, 65534);
596 
597 	mutex_lock(&data->lock);
598 	data->fan_max[attr->index] = temp;
599 	adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
600 	mutex_unlock(&data->lock);
601 
602 	return count;
603 }
604 
605 static ssize_t show_fan_min(struct device *dev,
606 			    struct device_attribute *devattr,
607 			    char *buf)
608 {
609 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
610 	struct adt7470_data *data = adt7470_update_device(dev);
611 
612 	if (FAN_DATA_VALID(data->fan_min[attr->index]))
613 		return sprintf(buf, "%d\n",
614 			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
615 	else
616 		return sprintf(buf, "0\n");
617 }
618 
619 static ssize_t set_fan_min(struct device *dev,
620 			   struct device_attribute *devattr,
621 			   const char *buf, size_t count)
622 {
623 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
624 	struct adt7470_data *data = dev_get_drvdata(dev);
625 	struct i2c_client *client = data->client;
626 	long temp;
627 
628 	if (kstrtol(buf, 10, &temp) || !temp)
629 		return -EINVAL;
630 
631 	temp = FAN_RPM_TO_PERIOD(temp);
632 	temp = clamp_val(temp, 1, 65534);
633 
634 	mutex_lock(&data->lock);
635 	data->fan_min[attr->index] = temp;
636 	adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
637 	mutex_unlock(&data->lock);
638 
639 	return count;
640 }
641 
642 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
643 			char *buf)
644 {
645 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
646 	struct adt7470_data *data = adt7470_update_device(dev);
647 
648 	if (FAN_DATA_VALID(data->fan[attr->index]))
649 		return sprintf(buf, "%d\n",
650 			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
651 	else
652 		return sprintf(buf, "0\n");
653 }
654 
655 static ssize_t show_force_pwm_max(struct device *dev,
656 				  struct device_attribute *devattr,
657 				  char *buf)
658 {
659 	struct adt7470_data *data = adt7470_update_device(dev);
660 	return sprintf(buf, "%d\n", data->force_pwm_max);
661 }
662 
663 static ssize_t set_force_pwm_max(struct device *dev,
664 				 struct device_attribute *devattr,
665 				 const char *buf,
666 				 size_t count)
667 {
668 	struct adt7470_data *data = dev_get_drvdata(dev);
669 	struct i2c_client *client = data->client;
670 	long temp;
671 	u8 reg;
672 
673 	if (kstrtol(buf, 10, &temp))
674 		return -EINVAL;
675 
676 	mutex_lock(&data->lock);
677 	data->force_pwm_max = temp;
678 	reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
679 	if (temp)
680 		reg |= ADT7470_FSPD_MASK;
681 	else
682 		reg &= ~ADT7470_FSPD_MASK;
683 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
684 	mutex_unlock(&data->lock);
685 
686 	return count;
687 }
688 
689 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
690 			char *buf)
691 {
692 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
693 	struct adt7470_data *data = adt7470_update_device(dev);
694 	return sprintf(buf, "%d\n", data->pwm[attr->index]);
695 }
696 
697 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
698 			const char *buf, size_t count)
699 {
700 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 	struct adt7470_data *data = dev_get_drvdata(dev);
702 	struct i2c_client *client = data->client;
703 	long temp;
704 
705 	if (kstrtol(buf, 10, &temp))
706 		return -EINVAL;
707 
708 	temp = clamp_val(temp, 0, 255);
709 
710 	mutex_lock(&data->lock);
711 	data->pwm[attr->index] = temp;
712 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
713 	mutex_unlock(&data->lock);
714 
715 	return count;
716 }
717 
718 /* These are the valid PWM frequencies to the nearest Hz */
719 static const int adt7470_freq_map[] = {
720 	11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
721 };
722 
723 static ssize_t pwm1_freq_show(struct device *dev,
724 			      struct device_attribute *devattr, char *buf)
725 {
726 	struct adt7470_data *data = adt7470_update_device(dev);
727 	unsigned char cfg_reg_1;
728 	unsigned char cfg_reg_2;
729 	int index;
730 
731 	mutex_lock(&data->lock);
732 	cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
733 	cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
734 	mutex_unlock(&data->lock);
735 
736 	index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
737 	if (!(cfg_reg_1 & ADT7470_CFG_LF))
738 		index += 8;
739 	if (index >= ARRAY_SIZE(adt7470_freq_map))
740 		index = ARRAY_SIZE(adt7470_freq_map) - 1;
741 
742 	return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
743 }
744 
745 static ssize_t pwm1_freq_store(struct device *dev,
746 			       struct device_attribute *devattr,
747 			       const char *buf, size_t count)
748 {
749 	struct adt7470_data *data = dev_get_drvdata(dev);
750 	struct i2c_client *client = data->client;
751 	long freq;
752 	int index;
753 	int low_freq = ADT7470_CFG_LF;
754 	unsigned char val;
755 
756 	if (kstrtol(buf, 10, &freq))
757 		return -EINVAL;
758 
759 	/* Round the user value given to the closest available frequency */
760 	index = find_closest(freq, adt7470_freq_map,
761 			     ARRAY_SIZE(adt7470_freq_map));
762 
763 	if (index >= 8) {
764 		index -= 8;
765 		low_freq = 0;
766 	}
767 
768 	mutex_lock(&data->lock);
769 	/* Configuration Register 1 */
770 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
771 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
772 				  (val & ~ADT7470_CFG_LF) | low_freq);
773 	/* Configuration Register 2 */
774 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
775 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
776 		(val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
777 	mutex_unlock(&data->lock);
778 
779 	return count;
780 }
781 
782 static ssize_t show_pwm_max(struct device *dev,
783 			    struct device_attribute *devattr,
784 			    char *buf)
785 {
786 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
787 	struct adt7470_data *data = adt7470_update_device(dev);
788 	return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
789 }
790 
791 static ssize_t set_pwm_max(struct device *dev,
792 			   struct device_attribute *devattr,
793 			   const char *buf,
794 			   size_t count)
795 {
796 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
797 	struct adt7470_data *data = dev_get_drvdata(dev);
798 	struct i2c_client *client = data->client;
799 	long temp;
800 
801 	if (kstrtol(buf, 10, &temp))
802 		return -EINVAL;
803 
804 	temp = clamp_val(temp, 0, 255);
805 
806 	mutex_lock(&data->lock);
807 	data->pwm_max[attr->index] = temp;
808 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
809 				  temp);
810 	mutex_unlock(&data->lock);
811 
812 	return count;
813 }
814 
815 static ssize_t show_pwm_min(struct device *dev,
816 			    struct device_attribute *devattr,
817 			    char *buf)
818 {
819 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
820 	struct adt7470_data *data = adt7470_update_device(dev);
821 	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
822 }
823 
824 static ssize_t set_pwm_min(struct device *dev,
825 			   struct device_attribute *devattr,
826 			   const char *buf,
827 			   size_t count)
828 {
829 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
830 	struct adt7470_data *data = dev_get_drvdata(dev);
831 	struct i2c_client *client = data->client;
832 	long temp;
833 
834 	if (kstrtol(buf, 10, &temp))
835 		return -EINVAL;
836 
837 	temp = clamp_val(temp, 0, 255);
838 
839 	mutex_lock(&data->lock);
840 	data->pwm_min[attr->index] = temp;
841 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
842 				  temp);
843 	mutex_unlock(&data->lock);
844 
845 	return count;
846 }
847 
848 static ssize_t show_pwm_tmax(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 	/* the datasheet says that tmax = tmin + 20C */
855 	return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
856 }
857 
858 static ssize_t show_pwm_tmin(struct device *dev,
859 			     struct device_attribute *devattr,
860 			     char *buf)
861 {
862 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
863 	struct adt7470_data *data = adt7470_update_device(dev);
864 	return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
865 }
866 
867 static ssize_t set_pwm_tmin(struct device *dev,
868 			    struct device_attribute *devattr,
869 			    const char *buf,
870 			    size_t count)
871 {
872 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
873 	struct adt7470_data *data = dev_get_drvdata(dev);
874 	struct i2c_client *client = data->client;
875 	long temp;
876 
877 	if (kstrtol(buf, 10, &temp))
878 		return -EINVAL;
879 
880 	temp = clamp_val(temp, -128000, 127000);
881 	temp = DIV_ROUND_CLOSEST(temp, 1000);
882 
883 	mutex_lock(&data->lock);
884 	data->pwm_tmin[attr->index] = temp;
885 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
886 				  temp);
887 	mutex_unlock(&data->lock);
888 
889 	return count;
890 }
891 
892 static ssize_t show_pwm_auto(struct device *dev,
893 			     struct device_attribute *devattr,
894 			     char *buf)
895 {
896 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
897 	struct adt7470_data *data = adt7470_update_device(dev);
898 	return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
899 }
900 
901 static ssize_t set_pwm_auto(struct device *dev,
902 			    struct device_attribute *devattr,
903 			    const char *buf,
904 			    size_t count)
905 {
906 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
907 	struct adt7470_data *data = dev_get_drvdata(dev);
908 	struct i2c_client *client = data->client;
909 	int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
910 	int pwm_auto_reg_mask;
911 	long temp;
912 	u8 reg;
913 
914 	if (kstrtol(buf, 10, &temp))
915 		return -EINVAL;
916 
917 	if (attr->index % 2)
918 		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
919 	else
920 		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
921 
922 	if (temp != 2 && temp != 1)
923 		return -EINVAL;
924 	temp--;
925 
926 	mutex_lock(&data->lock);
927 	data->pwm_automatic[attr->index] = temp;
928 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
929 	if (temp)
930 		reg |= pwm_auto_reg_mask;
931 	else
932 		reg &= ~pwm_auto_reg_mask;
933 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
934 	mutex_unlock(&data->lock);
935 
936 	return count;
937 }
938 
939 static ssize_t show_pwm_auto_temp(struct device *dev,
940 				  struct device_attribute *devattr,
941 				  char *buf)
942 {
943 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
944 	struct adt7470_data *data = adt7470_update_device(dev);
945 	u8 ctrl = data->pwm_auto_temp[attr->index];
946 
947 	if (ctrl)
948 		return sprintf(buf, "%d\n", 1 << (ctrl - 1));
949 	else
950 		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
951 }
952 
953 static int cvt_auto_temp(int input)
954 {
955 	if (input == ADT7470_PWM_ALL_TEMPS)
956 		return 0;
957 	if (input < 1 || !is_power_of_2(input))
958 		return -EINVAL;
959 	return ilog2(input) + 1;
960 }
961 
962 static ssize_t set_pwm_auto_temp(struct device *dev,
963 				 struct device_attribute *devattr,
964 				 const char *buf,
965 				 size_t count)
966 {
967 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
968 	struct adt7470_data *data = dev_get_drvdata(dev);
969 	struct i2c_client *client = data->client;
970 	int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
971 	long temp;
972 	u8 reg;
973 
974 	if (kstrtol(buf, 10, &temp))
975 		return -EINVAL;
976 
977 	temp = cvt_auto_temp(temp);
978 	if (temp < 0)
979 		return temp;
980 
981 	mutex_lock(&data->lock);
982 	data->pwm_automatic[attr->index] = temp;
983 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
984 
985 	if (!(attr->index % 2)) {
986 		reg &= 0xF;
987 		reg |= (temp << 4) & 0xF0;
988 	} else {
989 		reg &= 0xF0;
990 		reg |= temp & 0xF;
991 	}
992 
993 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
994 	mutex_unlock(&data->lock);
995 
996 	return count;
997 }
998 
999 static ssize_t show_alarm(struct device *dev,
1000 			  struct device_attribute *devattr,
1001 			  char *buf)
1002 {
1003 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1004 	struct adt7470_data *data = adt7470_update_device(dev);
1005 
1006 	if (data->alarm & attr->index)
1007 		return sprintf(buf, "1\n");
1008 	else
1009 		return sprintf(buf, "0\n");
1010 }
1011 
1012 static DEVICE_ATTR_RW(alarm_mask);
1013 static DEVICE_ATTR_RW(num_temp_sensors);
1014 static DEVICE_ATTR_RW(auto_update_interval);
1015 
1016 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
1017 		    set_temp_max, 0);
1018 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
1019 		    set_temp_max, 1);
1020 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
1021 		    set_temp_max, 2);
1022 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
1023 		    set_temp_max, 3);
1024 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
1025 		    set_temp_max, 4);
1026 static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
1027 		    set_temp_max, 5);
1028 static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
1029 		    set_temp_max, 6);
1030 static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
1031 		    set_temp_max, 7);
1032 static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
1033 		    set_temp_max, 8);
1034 static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
1035 		    set_temp_max, 9);
1036 
1037 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
1038 		    set_temp_min, 0);
1039 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
1040 		    set_temp_min, 1);
1041 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
1042 		    set_temp_min, 2);
1043 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
1044 		    set_temp_min, 3);
1045 static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
1046 		    set_temp_min, 4);
1047 static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
1048 		    set_temp_min, 5);
1049 static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
1050 		    set_temp_min, 6);
1051 static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
1052 		    set_temp_min, 7);
1053 static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
1054 		    set_temp_min, 8);
1055 static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
1056 		    set_temp_min, 9);
1057 
1058 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1059 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1060 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1061 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
1062 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
1063 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
1064 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
1065 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
1066 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
1067 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
1068 
1069 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1070 			  ADT7470_R1T_ALARM);
1071 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1072 			  ADT7470_R2T_ALARM);
1073 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1074 			  ADT7470_R3T_ALARM);
1075 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1076 			  ADT7470_R4T_ALARM);
1077 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1078 			  ADT7470_R5T_ALARM);
1079 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1080 			  ADT7470_R6T_ALARM);
1081 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
1082 			  ADT7470_R7T_ALARM);
1083 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
1084 			  ALARM2(ADT7470_R8T_ALARM));
1085 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
1086 			  ALARM2(ADT7470_R9T_ALARM));
1087 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
1088 			  ALARM2(ADT7470_R10T_ALARM));
1089 
1090 static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
1091 		    set_fan_max, 0);
1092 static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
1093 		    set_fan_max, 1);
1094 static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
1095 		    set_fan_max, 2);
1096 static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
1097 		    set_fan_max, 3);
1098 
1099 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1100 		    set_fan_min, 0);
1101 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1102 		    set_fan_min, 1);
1103 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1104 		    set_fan_min, 2);
1105 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1106 		    set_fan_min, 3);
1107 
1108 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
1109 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
1110 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
1111 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
1112 
1113 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
1114 			  ALARM2(ADT7470_FAN1_ALARM));
1115 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
1116 			  ALARM2(ADT7470_FAN2_ALARM));
1117 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
1118 			  ALARM2(ADT7470_FAN3_ALARM));
1119 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
1120 			  ALARM2(ADT7470_FAN4_ALARM));
1121 
1122 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
1123 		    show_force_pwm_max, set_force_pwm_max, 0);
1124 
1125 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
1126 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
1127 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
1128 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
1129 
1130 static DEVICE_ATTR_RW(pwm1_freq);
1131 
1132 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
1133 		    show_pwm_min, set_pwm_min, 0);
1134 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
1135 		    show_pwm_min, set_pwm_min, 1);
1136 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
1137 		    show_pwm_min, set_pwm_min, 2);
1138 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
1139 		    show_pwm_min, set_pwm_min, 3);
1140 
1141 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
1142 		    show_pwm_max, set_pwm_max, 0);
1143 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
1144 		    show_pwm_max, set_pwm_max, 1);
1145 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
1146 		    show_pwm_max, set_pwm_max, 2);
1147 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
1148 		    show_pwm_max, set_pwm_max, 3);
1149 
1150 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
1151 		    show_pwm_tmin, set_pwm_tmin, 0);
1152 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
1153 		    show_pwm_tmin, set_pwm_tmin, 1);
1154 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
1155 		    show_pwm_tmin, set_pwm_tmin, 2);
1156 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
1157 		    show_pwm_tmin, set_pwm_tmin, 3);
1158 
1159 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1160 		    NULL, 0);
1161 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1162 		    NULL, 1);
1163 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1164 		    NULL, 2);
1165 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
1166 		    NULL, 3);
1167 
1168 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1169 		    set_pwm_auto, 0);
1170 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1171 		    set_pwm_auto, 1);
1172 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1173 		    set_pwm_auto, 2);
1174 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1175 		    set_pwm_auto, 3);
1176 
1177 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1178 		    show_pwm_auto_temp, set_pwm_auto_temp, 0);
1179 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1180 		    show_pwm_auto_temp, set_pwm_auto_temp, 1);
1181 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1182 		    show_pwm_auto_temp, set_pwm_auto_temp, 2);
1183 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
1184 		    show_pwm_auto_temp, set_pwm_auto_temp, 3);
1185 
1186 static struct attribute *adt7470_attrs[] = {
1187 	&dev_attr_alarm_mask.attr,
1188 	&dev_attr_num_temp_sensors.attr,
1189 	&dev_attr_auto_update_interval.attr,
1190 	&sensor_dev_attr_temp1_max.dev_attr.attr,
1191 	&sensor_dev_attr_temp2_max.dev_attr.attr,
1192 	&sensor_dev_attr_temp3_max.dev_attr.attr,
1193 	&sensor_dev_attr_temp4_max.dev_attr.attr,
1194 	&sensor_dev_attr_temp5_max.dev_attr.attr,
1195 	&sensor_dev_attr_temp6_max.dev_attr.attr,
1196 	&sensor_dev_attr_temp7_max.dev_attr.attr,
1197 	&sensor_dev_attr_temp8_max.dev_attr.attr,
1198 	&sensor_dev_attr_temp9_max.dev_attr.attr,
1199 	&sensor_dev_attr_temp10_max.dev_attr.attr,
1200 	&sensor_dev_attr_temp1_min.dev_attr.attr,
1201 	&sensor_dev_attr_temp2_min.dev_attr.attr,
1202 	&sensor_dev_attr_temp3_min.dev_attr.attr,
1203 	&sensor_dev_attr_temp4_min.dev_attr.attr,
1204 	&sensor_dev_attr_temp5_min.dev_attr.attr,
1205 	&sensor_dev_attr_temp6_min.dev_attr.attr,
1206 	&sensor_dev_attr_temp7_min.dev_attr.attr,
1207 	&sensor_dev_attr_temp8_min.dev_attr.attr,
1208 	&sensor_dev_attr_temp9_min.dev_attr.attr,
1209 	&sensor_dev_attr_temp10_min.dev_attr.attr,
1210 	&sensor_dev_attr_temp1_input.dev_attr.attr,
1211 	&sensor_dev_attr_temp2_input.dev_attr.attr,
1212 	&sensor_dev_attr_temp3_input.dev_attr.attr,
1213 	&sensor_dev_attr_temp4_input.dev_attr.attr,
1214 	&sensor_dev_attr_temp5_input.dev_attr.attr,
1215 	&sensor_dev_attr_temp6_input.dev_attr.attr,
1216 	&sensor_dev_attr_temp7_input.dev_attr.attr,
1217 	&sensor_dev_attr_temp8_input.dev_attr.attr,
1218 	&sensor_dev_attr_temp9_input.dev_attr.attr,
1219 	&sensor_dev_attr_temp10_input.dev_attr.attr,
1220 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1221 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1222 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1223 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
1224 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
1225 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
1226 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
1227 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
1228 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
1229 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
1230 	&sensor_dev_attr_fan1_max.dev_attr.attr,
1231 	&sensor_dev_attr_fan2_max.dev_attr.attr,
1232 	&sensor_dev_attr_fan3_max.dev_attr.attr,
1233 	&sensor_dev_attr_fan4_max.dev_attr.attr,
1234 	&sensor_dev_attr_fan1_min.dev_attr.attr,
1235 	&sensor_dev_attr_fan2_min.dev_attr.attr,
1236 	&sensor_dev_attr_fan3_min.dev_attr.attr,
1237 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1238 	&sensor_dev_attr_fan1_input.dev_attr.attr,
1239 	&sensor_dev_attr_fan2_input.dev_attr.attr,
1240 	&sensor_dev_attr_fan3_input.dev_attr.attr,
1241 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1242 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1243 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1244 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1245 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1246 	&sensor_dev_attr_force_pwm_max.dev_attr.attr,
1247 	&sensor_dev_attr_pwm1.dev_attr.attr,
1248 	&dev_attr_pwm1_freq.attr,
1249 	&sensor_dev_attr_pwm2.dev_attr.attr,
1250 	&sensor_dev_attr_pwm3.dev_attr.attr,
1251 	&sensor_dev_attr_pwm4.dev_attr.attr,
1252 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1253 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1254 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1255 	&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1256 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1257 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1258 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1259 	&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1260 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1261 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1262 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1263 	&sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1264 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1265 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1266 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1267 	&sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1268 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
1269 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1270 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
1271 	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
1272 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1273 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1274 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1275 	&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1276 	NULL
1277 };
1278 
1279 ATTRIBUTE_GROUPS(adt7470);
1280 
1281 /* Return 0 if detection is successful, -ENODEV otherwise */
1282 static int adt7470_detect(struct i2c_client *client,
1283 			  struct i2c_board_info *info)
1284 {
1285 	struct i2c_adapter *adapter = client->adapter;
1286 	int vendor, device, revision;
1287 
1288 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1289 		return -ENODEV;
1290 
1291 	vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1292 	if (vendor != ADT7470_VENDOR)
1293 		return -ENODEV;
1294 
1295 	device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1296 	if (device != ADT7470_DEVICE)
1297 		return -ENODEV;
1298 
1299 	revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1300 	if (revision != ADT7470_REVISION)
1301 		return -ENODEV;
1302 
1303 	strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
1304 
1305 	return 0;
1306 }
1307 
1308 static void adt7470_init_client(struct i2c_client *client)
1309 {
1310 	int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
1311 
1312 	if (reg < 0) {
1313 		dev_err(&client->dev, "cannot read configuration register\n");
1314 	} else {
1315 		/* start monitoring (and do a self-test) */
1316 		i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
1317 	}
1318 }
1319 
1320 static int adt7470_probe(struct i2c_client *client,
1321 			 const struct i2c_device_id *id)
1322 {
1323 	struct device *dev = &client->dev;
1324 	struct adt7470_data *data;
1325 	struct device *hwmon_dev;
1326 
1327 	data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
1328 	if (!data)
1329 		return -ENOMEM;
1330 
1331 	data->num_temp_sensors = -1;
1332 	data->auto_update_interval = AUTO_UPDATE_INTERVAL;
1333 
1334 	i2c_set_clientdata(client, data);
1335 	data->client = client;
1336 	mutex_init(&data->lock);
1337 
1338 	dev_info(&client->dev, "%s chip found\n", client->name);
1339 
1340 	/* Initialize the ADT7470 chip */
1341 	adt7470_init_client(client);
1342 
1343 	/* Register sysfs hooks */
1344 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1345 							   data,
1346 							   adt7470_groups);
1347 
1348 	if (IS_ERR(hwmon_dev))
1349 		return PTR_ERR(hwmon_dev);
1350 
1351 	data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
1352 					dev_name(hwmon_dev));
1353 	if (IS_ERR(data->auto_update)) {
1354 		return PTR_ERR(data->auto_update);
1355 	}
1356 
1357 	return 0;
1358 }
1359 
1360 static int adt7470_remove(struct i2c_client *client)
1361 {
1362 	struct adt7470_data *data = i2c_get_clientdata(client);
1363 
1364 	kthread_stop(data->auto_update);
1365 	return 0;
1366 }
1367 
1368 static const struct i2c_device_id adt7470_id[] = {
1369 	{ "adt7470", 0 },
1370 	{ }
1371 };
1372 MODULE_DEVICE_TABLE(i2c, adt7470_id);
1373 
1374 static struct i2c_driver adt7470_driver = {
1375 	.class		= I2C_CLASS_HWMON,
1376 	.driver = {
1377 		.name	= "adt7470",
1378 	},
1379 	.probe		= adt7470_probe,
1380 	.remove		= adt7470_remove,
1381 	.id_table	= adt7470_id,
1382 	.detect		= adt7470_detect,
1383 	.address_list	= normal_i2c,
1384 };
1385 
1386 module_i2c_driver(adt7470_driver);
1387 
1388 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1389 MODULE_DESCRIPTION("ADT7470 driver");
1390 MODULE_LICENSE("GPL");
1391