xref: /openbmc/linux/drivers/hwmon/adt7470.c (revision 151f4e2b)
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 temp_min_show(struct device *dev,
463 			     struct device_attribute *devattr, char *buf)
464 {
465 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
466 	struct adt7470_data *data = adt7470_update_device(dev);
467 	return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
468 }
469 
470 static ssize_t temp_min_store(struct device *dev,
471 			      struct device_attribute *devattr,
472 			      const char *buf, size_t count)
473 {
474 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
475 	struct adt7470_data *data = dev_get_drvdata(dev);
476 	struct i2c_client *client = data->client;
477 	long temp;
478 
479 	if (kstrtol(buf, 10, &temp))
480 		return -EINVAL;
481 
482 	temp = clamp_val(temp, -128000, 127000);
483 	temp = DIV_ROUND_CLOSEST(temp, 1000);
484 
485 	mutex_lock(&data->lock);
486 	data->temp_min[attr->index] = temp;
487 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
488 				  temp);
489 	mutex_unlock(&data->lock);
490 
491 	return count;
492 }
493 
494 static ssize_t temp_max_show(struct device *dev,
495 			     struct device_attribute *devattr, char *buf)
496 {
497 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
498 	struct adt7470_data *data = adt7470_update_device(dev);
499 	return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
500 }
501 
502 static ssize_t temp_max_store(struct device *dev,
503 			      struct device_attribute *devattr,
504 			      const char *buf, size_t count)
505 {
506 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
507 	struct adt7470_data *data = dev_get_drvdata(dev);
508 	struct i2c_client *client = data->client;
509 	long temp;
510 
511 	if (kstrtol(buf, 10, &temp))
512 		return -EINVAL;
513 
514 	temp = clamp_val(temp, -128000, 127000);
515 	temp = DIV_ROUND_CLOSEST(temp, 1000);
516 
517 	mutex_lock(&data->lock);
518 	data->temp_max[attr->index] = temp;
519 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
520 				  temp);
521 	mutex_unlock(&data->lock);
522 
523 	return count;
524 }
525 
526 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
527 			 char *buf)
528 {
529 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
530 	struct adt7470_data *data = adt7470_update_device(dev);
531 	return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
532 }
533 
534 static ssize_t alarm_mask_show(struct device *dev,
535 			   struct device_attribute *devattr,
536 			   char *buf)
537 {
538 	struct adt7470_data *data = adt7470_update_device(dev);
539 
540 	return sprintf(buf, "%x\n", data->alarms_mask);
541 }
542 
543 static ssize_t alarm_mask_store(struct device *dev,
544 				struct device_attribute *devattr,
545 				const char *buf, size_t count)
546 {
547 	struct adt7470_data *data = dev_get_drvdata(dev);
548 	long mask;
549 
550 	if (kstrtoul(buf, 0, &mask))
551 		return -EINVAL;
552 
553 	if (mask & ~0xffff)
554 		return -EINVAL;
555 
556 	mutex_lock(&data->lock);
557 	data->alarms_mask = mask;
558 	adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
559 	mutex_unlock(&data->lock);
560 
561 	return count;
562 }
563 
564 static ssize_t fan_max_show(struct device *dev,
565 			    struct device_attribute *devattr, char *buf)
566 {
567 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
568 	struct adt7470_data *data = adt7470_update_device(dev);
569 
570 	if (FAN_DATA_VALID(data->fan_max[attr->index]))
571 		return sprintf(buf, "%d\n",
572 			       FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
573 	else
574 		return sprintf(buf, "0\n");
575 }
576 
577 static ssize_t fan_max_store(struct device *dev,
578 			     struct device_attribute *devattr,
579 			     const char *buf, size_t count)
580 {
581 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
582 	struct adt7470_data *data = dev_get_drvdata(dev);
583 	struct i2c_client *client = data->client;
584 	long temp;
585 
586 	if (kstrtol(buf, 10, &temp) || !temp)
587 		return -EINVAL;
588 
589 	temp = FAN_RPM_TO_PERIOD(temp);
590 	temp = clamp_val(temp, 1, 65534);
591 
592 	mutex_lock(&data->lock);
593 	data->fan_max[attr->index] = temp;
594 	adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
595 	mutex_unlock(&data->lock);
596 
597 	return count;
598 }
599 
600 static ssize_t fan_min_show(struct device *dev,
601 			    struct device_attribute *devattr, char *buf)
602 {
603 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
604 	struct adt7470_data *data = adt7470_update_device(dev);
605 
606 	if (FAN_DATA_VALID(data->fan_min[attr->index]))
607 		return sprintf(buf, "%d\n",
608 			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
609 	else
610 		return sprintf(buf, "0\n");
611 }
612 
613 static ssize_t fan_min_store(struct device *dev,
614 			     struct device_attribute *devattr,
615 			     const char *buf, size_t count)
616 {
617 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
618 	struct adt7470_data *data = dev_get_drvdata(dev);
619 	struct i2c_client *client = data->client;
620 	long temp;
621 
622 	if (kstrtol(buf, 10, &temp) || !temp)
623 		return -EINVAL;
624 
625 	temp = FAN_RPM_TO_PERIOD(temp);
626 	temp = clamp_val(temp, 1, 65534);
627 
628 	mutex_lock(&data->lock);
629 	data->fan_min[attr->index] = temp;
630 	adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
631 	mutex_unlock(&data->lock);
632 
633 	return count;
634 }
635 
636 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
637 			char *buf)
638 {
639 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
640 	struct adt7470_data *data = adt7470_update_device(dev);
641 
642 	if (FAN_DATA_VALID(data->fan[attr->index]))
643 		return sprintf(buf, "%d\n",
644 			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
645 	else
646 		return sprintf(buf, "0\n");
647 }
648 
649 static ssize_t force_pwm_max_show(struct device *dev,
650 				  struct device_attribute *devattr, char *buf)
651 {
652 	struct adt7470_data *data = adt7470_update_device(dev);
653 	return sprintf(buf, "%d\n", data->force_pwm_max);
654 }
655 
656 static ssize_t force_pwm_max_store(struct device *dev,
657 				   struct device_attribute *devattr,
658 				   const char *buf, size_t count)
659 {
660 	struct adt7470_data *data = dev_get_drvdata(dev);
661 	struct i2c_client *client = data->client;
662 	long temp;
663 	u8 reg;
664 
665 	if (kstrtol(buf, 10, &temp))
666 		return -EINVAL;
667 
668 	mutex_lock(&data->lock);
669 	data->force_pwm_max = temp;
670 	reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
671 	if (temp)
672 		reg |= ADT7470_FSPD_MASK;
673 	else
674 		reg &= ~ADT7470_FSPD_MASK;
675 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
676 	mutex_unlock(&data->lock);
677 
678 	return count;
679 }
680 
681 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
682 			char *buf)
683 {
684 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
685 	struct adt7470_data *data = adt7470_update_device(dev);
686 	return sprintf(buf, "%d\n", data->pwm[attr->index]);
687 }
688 
689 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
690 			 const char *buf, size_t count)
691 {
692 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
693 	struct adt7470_data *data = dev_get_drvdata(dev);
694 	struct i2c_client *client = data->client;
695 	long temp;
696 
697 	if (kstrtol(buf, 10, &temp))
698 		return -EINVAL;
699 
700 	temp = clamp_val(temp, 0, 255);
701 
702 	mutex_lock(&data->lock);
703 	data->pwm[attr->index] = temp;
704 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
705 	mutex_unlock(&data->lock);
706 
707 	return count;
708 }
709 
710 /* These are the valid PWM frequencies to the nearest Hz */
711 static const int adt7470_freq_map[] = {
712 	11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
713 };
714 
715 static ssize_t pwm1_freq_show(struct device *dev,
716 			      struct device_attribute *devattr, char *buf)
717 {
718 	struct adt7470_data *data = adt7470_update_device(dev);
719 	unsigned char cfg_reg_1;
720 	unsigned char cfg_reg_2;
721 	int index;
722 
723 	mutex_lock(&data->lock);
724 	cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
725 	cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
726 	mutex_unlock(&data->lock);
727 
728 	index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
729 	if (!(cfg_reg_1 & ADT7470_CFG_LF))
730 		index += 8;
731 	if (index >= ARRAY_SIZE(adt7470_freq_map))
732 		index = ARRAY_SIZE(adt7470_freq_map) - 1;
733 
734 	return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
735 }
736 
737 static ssize_t pwm1_freq_store(struct device *dev,
738 			       struct device_attribute *devattr,
739 			       const char *buf, size_t count)
740 {
741 	struct adt7470_data *data = dev_get_drvdata(dev);
742 	struct i2c_client *client = data->client;
743 	long freq;
744 	int index;
745 	int low_freq = ADT7470_CFG_LF;
746 	unsigned char val;
747 
748 	if (kstrtol(buf, 10, &freq))
749 		return -EINVAL;
750 
751 	/* Round the user value given to the closest available frequency */
752 	index = find_closest(freq, adt7470_freq_map,
753 			     ARRAY_SIZE(adt7470_freq_map));
754 
755 	if (index >= 8) {
756 		index -= 8;
757 		low_freq = 0;
758 	}
759 
760 	mutex_lock(&data->lock);
761 	/* Configuration Register 1 */
762 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
763 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
764 				  (val & ~ADT7470_CFG_LF) | low_freq);
765 	/* Configuration Register 2 */
766 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
767 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
768 		(val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
769 	mutex_unlock(&data->lock);
770 
771 	return count;
772 }
773 
774 static ssize_t pwm_max_show(struct device *dev,
775 			    struct device_attribute *devattr, char *buf)
776 {
777 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
778 	struct adt7470_data *data = adt7470_update_device(dev);
779 	return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
780 }
781 
782 static ssize_t pwm_max_store(struct device *dev,
783 			     struct device_attribute *devattr,
784 			     const char *buf, size_t count)
785 {
786 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
787 	struct adt7470_data *data = dev_get_drvdata(dev);
788 	struct i2c_client *client = data->client;
789 	long temp;
790 
791 	if (kstrtol(buf, 10, &temp))
792 		return -EINVAL;
793 
794 	temp = clamp_val(temp, 0, 255);
795 
796 	mutex_lock(&data->lock);
797 	data->pwm_max[attr->index] = temp;
798 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
799 				  temp);
800 	mutex_unlock(&data->lock);
801 
802 	return count;
803 }
804 
805 static ssize_t pwm_min_show(struct device *dev,
806 			    struct device_attribute *devattr, char *buf)
807 {
808 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
809 	struct adt7470_data *data = adt7470_update_device(dev);
810 	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
811 }
812 
813 static ssize_t pwm_min_store(struct device *dev,
814 			     struct device_attribute *devattr,
815 			     const char *buf, size_t count)
816 {
817 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
818 	struct adt7470_data *data = dev_get_drvdata(dev);
819 	struct i2c_client *client = data->client;
820 	long temp;
821 
822 	if (kstrtol(buf, 10, &temp))
823 		return -EINVAL;
824 
825 	temp = clamp_val(temp, 0, 255);
826 
827 	mutex_lock(&data->lock);
828 	data->pwm_min[attr->index] = temp;
829 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
830 				  temp);
831 	mutex_unlock(&data->lock);
832 
833 	return count;
834 }
835 
836 static ssize_t pwm_tmax_show(struct device *dev,
837 			     struct device_attribute *devattr, char *buf)
838 {
839 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
840 	struct adt7470_data *data = adt7470_update_device(dev);
841 	/* the datasheet says that tmax = tmin + 20C */
842 	return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
843 }
844 
845 static ssize_t pwm_tmin_show(struct device *dev,
846 			     struct device_attribute *devattr, char *buf)
847 {
848 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
849 	struct adt7470_data *data = adt7470_update_device(dev);
850 	return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
851 }
852 
853 static ssize_t pwm_tmin_store(struct device *dev,
854 			      struct device_attribute *devattr,
855 			      const char *buf, size_t count)
856 {
857 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
858 	struct adt7470_data *data = dev_get_drvdata(dev);
859 	struct i2c_client *client = data->client;
860 	long temp;
861 
862 	if (kstrtol(buf, 10, &temp))
863 		return -EINVAL;
864 
865 	temp = clamp_val(temp, -128000, 127000);
866 	temp = DIV_ROUND_CLOSEST(temp, 1000);
867 
868 	mutex_lock(&data->lock);
869 	data->pwm_tmin[attr->index] = temp;
870 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
871 				  temp);
872 	mutex_unlock(&data->lock);
873 
874 	return count;
875 }
876 
877 static ssize_t pwm_auto_show(struct device *dev,
878 			     struct device_attribute *devattr, char *buf)
879 {
880 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
881 	struct adt7470_data *data = adt7470_update_device(dev);
882 	return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
883 }
884 
885 static ssize_t pwm_auto_store(struct device *dev,
886 			      struct device_attribute *devattr,
887 			      const char *buf, size_t count)
888 {
889 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
890 	struct adt7470_data *data = dev_get_drvdata(dev);
891 	struct i2c_client *client = data->client;
892 	int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
893 	int pwm_auto_reg_mask;
894 	long temp;
895 	u8 reg;
896 
897 	if (kstrtol(buf, 10, &temp))
898 		return -EINVAL;
899 
900 	if (attr->index % 2)
901 		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
902 	else
903 		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
904 
905 	if (temp != 2 && temp != 1)
906 		return -EINVAL;
907 	temp--;
908 
909 	mutex_lock(&data->lock);
910 	data->pwm_automatic[attr->index] = temp;
911 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
912 	if (temp)
913 		reg |= pwm_auto_reg_mask;
914 	else
915 		reg &= ~pwm_auto_reg_mask;
916 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
917 	mutex_unlock(&data->lock);
918 
919 	return count;
920 }
921 
922 static ssize_t pwm_auto_temp_show(struct device *dev,
923 				  struct device_attribute *devattr, char *buf)
924 {
925 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
926 	struct adt7470_data *data = adt7470_update_device(dev);
927 	u8 ctrl = data->pwm_auto_temp[attr->index];
928 
929 	if (ctrl)
930 		return sprintf(buf, "%d\n", 1 << (ctrl - 1));
931 	else
932 		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
933 }
934 
935 static int cvt_auto_temp(int input)
936 {
937 	if (input == ADT7470_PWM_ALL_TEMPS)
938 		return 0;
939 	if (input < 1 || !is_power_of_2(input))
940 		return -EINVAL;
941 	return ilog2(input) + 1;
942 }
943 
944 static ssize_t pwm_auto_temp_store(struct device *dev,
945 				   struct device_attribute *devattr,
946 				   const char *buf, size_t count)
947 {
948 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
949 	struct adt7470_data *data = dev_get_drvdata(dev);
950 	struct i2c_client *client = data->client;
951 	int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
952 	long temp;
953 	u8 reg;
954 
955 	if (kstrtol(buf, 10, &temp))
956 		return -EINVAL;
957 
958 	temp = cvt_auto_temp(temp);
959 	if (temp < 0)
960 		return temp;
961 
962 	mutex_lock(&data->lock);
963 	data->pwm_automatic[attr->index] = temp;
964 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
965 
966 	if (!(attr->index % 2)) {
967 		reg &= 0xF;
968 		reg |= (temp << 4) & 0xF0;
969 	} else {
970 		reg &= 0xF0;
971 		reg |= temp & 0xF;
972 	}
973 
974 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
975 	mutex_unlock(&data->lock);
976 
977 	return count;
978 }
979 
980 static ssize_t alarm_show(struct device *dev,
981 			  struct device_attribute *devattr, char *buf)
982 {
983 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
984 	struct adt7470_data *data = adt7470_update_device(dev);
985 
986 	if (data->alarm & attr->index)
987 		return sprintf(buf, "1\n");
988 	else
989 		return sprintf(buf, "0\n");
990 }
991 
992 static DEVICE_ATTR_RW(alarm_mask);
993 static DEVICE_ATTR_RW(num_temp_sensors);
994 static DEVICE_ATTR_RW(auto_update_interval);
995 
996 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
997 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
998 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
999 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
1000 static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
1001 static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
1002 static SENSOR_DEVICE_ATTR_RW(temp7_max, temp_max, 6);
1003 static SENSOR_DEVICE_ATTR_RW(temp8_max, temp_max, 7);
1004 static SENSOR_DEVICE_ATTR_RW(temp9_max, temp_max, 8);
1005 static SENSOR_DEVICE_ATTR_RW(temp10_max, temp_max, 9);
1006 
1007 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1008 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1009 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1010 static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
1011 static SENSOR_DEVICE_ATTR_RW(temp5_min, temp_min, 4);
1012 static SENSOR_DEVICE_ATTR_RW(temp6_min, temp_min, 5);
1013 static SENSOR_DEVICE_ATTR_RW(temp7_min, temp_min, 6);
1014 static SENSOR_DEVICE_ATTR_RW(temp8_min, temp_min, 7);
1015 static SENSOR_DEVICE_ATTR_RW(temp9_min, temp_min, 8);
1016 static SENSOR_DEVICE_ATTR_RW(temp10_min, temp_min, 9);
1017 
1018 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1019 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1020 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1021 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
1022 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
1023 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
1024 static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6);
1025 static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7);
1026 static SENSOR_DEVICE_ATTR_RO(temp9_input, temp, 8);
1027 static SENSOR_DEVICE_ATTR_RO(temp10_input, temp, 9);
1028 
1029 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, ADT7470_R1T_ALARM);
1030 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, ADT7470_R2T_ALARM);
1031 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, ADT7470_R3T_ALARM);
1032 static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, ADT7470_R4T_ALARM);
1033 static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, ADT7470_R5T_ALARM);
1034 static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, ADT7470_R6T_ALARM);
1035 static SENSOR_DEVICE_ATTR_RO(temp7_alarm, alarm, ADT7470_R7T_ALARM);
1036 static SENSOR_DEVICE_ATTR_RO(temp8_alarm, alarm, ALARM2(ADT7470_R8T_ALARM));
1037 static SENSOR_DEVICE_ATTR_RO(temp9_alarm, alarm, ALARM2(ADT7470_R9T_ALARM));
1038 static SENSOR_DEVICE_ATTR_RO(temp10_alarm, alarm, ALARM2(ADT7470_R10T_ALARM));
1039 
1040 static SENSOR_DEVICE_ATTR_RW(fan1_max, fan_max, 0);
1041 static SENSOR_DEVICE_ATTR_RW(fan2_max, fan_max, 1);
1042 static SENSOR_DEVICE_ATTR_RW(fan3_max, fan_max, 2);
1043 static SENSOR_DEVICE_ATTR_RW(fan4_max, fan_max, 3);
1044 
1045 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1046 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1047 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1048 static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
1049 
1050 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
1051 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
1052 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
1053 static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
1054 
1055 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, ALARM2(ADT7470_FAN1_ALARM));
1056 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, ALARM2(ADT7470_FAN2_ALARM));
1057 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, ALARM2(ADT7470_FAN3_ALARM));
1058 static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, ALARM2(ADT7470_FAN4_ALARM));
1059 
1060 static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
1061 
1062 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1063 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1064 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
1065 static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
1066 
1067 static DEVICE_ATTR_RW(pwm1_freq);
1068 
1069 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
1070 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
1071 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
1072 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
1073 
1074 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
1075 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
1076 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
1077 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
1078 
1079 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0);
1080 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1);
1081 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2);
1082 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3);
1083 
1084 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0);
1085 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1);
1086 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2);
1087 static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3);
1088 
1089 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
1090 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
1091 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
1092 static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
1093 
1094 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
1095 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
1096 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
1097 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
1098 
1099 static struct attribute *adt7470_attrs[] = {
1100 	&dev_attr_alarm_mask.attr,
1101 	&dev_attr_num_temp_sensors.attr,
1102 	&dev_attr_auto_update_interval.attr,
1103 	&sensor_dev_attr_temp1_max.dev_attr.attr,
1104 	&sensor_dev_attr_temp2_max.dev_attr.attr,
1105 	&sensor_dev_attr_temp3_max.dev_attr.attr,
1106 	&sensor_dev_attr_temp4_max.dev_attr.attr,
1107 	&sensor_dev_attr_temp5_max.dev_attr.attr,
1108 	&sensor_dev_attr_temp6_max.dev_attr.attr,
1109 	&sensor_dev_attr_temp7_max.dev_attr.attr,
1110 	&sensor_dev_attr_temp8_max.dev_attr.attr,
1111 	&sensor_dev_attr_temp9_max.dev_attr.attr,
1112 	&sensor_dev_attr_temp10_max.dev_attr.attr,
1113 	&sensor_dev_attr_temp1_min.dev_attr.attr,
1114 	&sensor_dev_attr_temp2_min.dev_attr.attr,
1115 	&sensor_dev_attr_temp3_min.dev_attr.attr,
1116 	&sensor_dev_attr_temp4_min.dev_attr.attr,
1117 	&sensor_dev_attr_temp5_min.dev_attr.attr,
1118 	&sensor_dev_attr_temp6_min.dev_attr.attr,
1119 	&sensor_dev_attr_temp7_min.dev_attr.attr,
1120 	&sensor_dev_attr_temp8_min.dev_attr.attr,
1121 	&sensor_dev_attr_temp9_min.dev_attr.attr,
1122 	&sensor_dev_attr_temp10_min.dev_attr.attr,
1123 	&sensor_dev_attr_temp1_input.dev_attr.attr,
1124 	&sensor_dev_attr_temp2_input.dev_attr.attr,
1125 	&sensor_dev_attr_temp3_input.dev_attr.attr,
1126 	&sensor_dev_attr_temp4_input.dev_attr.attr,
1127 	&sensor_dev_attr_temp5_input.dev_attr.attr,
1128 	&sensor_dev_attr_temp6_input.dev_attr.attr,
1129 	&sensor_dev_attr_temp7_input.dev_attr.attr,
1130 	&sensor_dev_attr_temp8_input.dev_attr.attr,
1131 	&sensor_dev_attr_temp9_input.dev_attr.attr,
1132 	&sensor_dev_attr_temp10_input.dev_attr.attr,
1133 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1134 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1135 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1136 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
1137 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
1138 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
1139 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
1140 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
1141 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
1142 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
1143 	&sensor_dev_attr_fan1_max.dev_attr.attr,
1144 	&sensor_dev_attr_fan2_max.dev_attr.attr,
1145 	&sensor_dev_attr_fan3_max.dev_attr.attr,
1146 	&sensor_dev_attr_fan4_max.dev_attr.attr,
1147 	&sensor_dev_attr_fan1_min.dev_attr.attr,
1148 	&sensor_dev_attr_fan2_min.dev_attr.attr,
1149 	&sensor_dev_attr_fan3_min.dev_attr.attr,
1150 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1151 	&sensor_dev_attr_fan1_input.dev_attr.attr,
1152 	&sensor_dev_attr_fan2_input.dev_attr.attr,
1153 	&sensor_dev_attr_fan3_input.dev_attr.attr,
1154 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1155 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1156 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1157 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1158 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1159 	&sensor_dev_attr_force_pwm_max.dev_attr.attr,
1160 	&sensor_dev_attr_pwm1.dev_attr.attr,
1161 	&dev_attr_pwm1_freq.attr,
1162 	&sensor_dev_attr_pwm2.dev_attr.attr,
1163 	&sensor_dev_attr_pwm3.dev_attr.attr,
1164 	&sensor_dev_attr_pwm4.dev_attr.attr,
1165 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1166 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1167 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1168 	&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1169 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1170 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1171 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1172 	&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1173 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1174 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1175 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1176 	&sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1177 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1178 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1179 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1180 	&sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1181 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
1182 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1183 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
1184 	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
1185 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1186 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1187 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1188 	&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1189 	NULL
1190 };
1191 
1192 ATTRIBUTE_GROUPS(adt7470);
1193 
1194 /* Return 0 if detection is successful, -ENODEV otherwise */
1195 static int adt7470_detect(struct i2c_client *client,
1196 			  struct i2c_board_info *info)
1197 {
1198 	struct i2c_adapter *adapter = client->adapter;
1199 	int vendor, device, revision;
1200 
1201 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1202 		return -ENODEV;
1203 
1204 	vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1205 	if (vendor != ADT7470_VENDOR)
1206 		return -ENODEV;
1207 
1208 	device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1209 	if (device != ADT7470_DEVICE)
1210 		return -ENODEV;
1211 
1212 	revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1213 	if (revision != ADT7470_REVISION)
1214 		return -ENODEV;
1215 
1216 	strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
1217 
1218 	return 0;
1219 }
1220 
1221 static void adt7470_init_client(struct i2c_client *client)
1222 {
1223 	int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
1224 
1225 	if (reg < 0) {
1226 		dev_err(&client->dev, "cannot read configuration register\n");
1227 	} else {
1228 		/* start monitoring (and do a self-test) */
1229 		i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
1230 	}
1231 }
1232 
1233 static int adt7470_probe(struct i2c_client *client,
1234 			 const struct i2c_device_id *id)
1235 {
1236 	struct device *dev = &client->dev;
1237 	struct adt7470_data *data;
1238 	struct device *hwmon_dev;
1239 
1240 	data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
1241 	if (!data)
1242 		return -ENOMEM;
1243 
1244 	data->num_temp_sensors = -1;
1245 	data->auto_update_interval = AUTO_UPDATE_INTERVAL;
1246 
1247 	i2c_set_clientdata(client, data);
1248 	data->client = client;
1249 	mutex_init(&data->lock);
1250 
1251 	dev_info(&client->dev, "%s chip found\n", client->name);
1252 
1253 	/* Initialize the ADT7470 chip */
1254 	adt7470_init_client(client);
1255 
1256 	/* Register sysfs hooks */
1257 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1258 							   data,
1259 							   adt7470_groups);
1260 
1261 	if (IS_ERR(hwmon_dev))
1262 		return PTR_ERR(hwmon_dev);
1263 
1264 	data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
1265 					dev_name(hwmon_dev));
1266 	if (IS_ERR(data->auto_update)) {
1267 		return PTR_ERR(data->auto_update);
1268 	}
1269 
1270 	return 0;
1271 }
1272 
1273 static int adt7470_remove(struct i2c_client *client)
1274 {
1275 	struct adt7470_data *data = i2c_get_clientdata(client);
1276 
1277 	kthread_stop(data->auto_update);
1278 	return 0;
1279 }
1280 
1281 static const struct i2c_device_id adt7470_id[] = {
1282 	{ "adt7470", 0 },
1283 	{ }
1284 };
1285 MODULE_DEVICE_TABLE(i2c, adt7470_id);
1286 
1287 static struct i2c_driver adt7470_driver = {
1288 	.class		= I2C_CLASS_HWMON,
1289 	.driver = {
1290 		.name	= "adt7470",
1291 	},
1292 	.probe		= adt7470_probe,
1293 	.remove		= adt7470_remove,
1294 	.id_table	= adt7470_id,
1295 	.detect		= adt7470_detect,
1296 	.address_list	= normal_i2c,
1297 };
1298 
1299 module_i2c_driver(adt7470_driver);
1300 
1301 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1302 MODULE_DESCRIPTION("ADT7470 driver");
1303 MODULE_LICENSE("GPL");
1304