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