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