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