1 /*
2  * lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3  *	    monitoring
4  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5  * Copyright (c) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
6  * Copyright (c) 2003        Margit Schubert-While <margitsw@t-online.de>
7  * Copyright (c) 2004        Justin Thiessen <jthiessen@penguincomputing.com>
8  * Copyright (C) 2007--2014  Jean Delvare <jdelvare@suse.de>
9  *
10  * Chip details at	      <http://www.national.com/ds/LM/LM85.pdf>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26 
27 #include <linux/module.h>
28 #include <linux/of_device.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/jiffies.h>
32 #include <linux/i2c.h>
33 #include <linux/hwmon.h>
34 #include <linux/hwmon-vid.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/err.h>
37 #include <linux/mutex.h>
38 #include <linux/util_macros.h>
39 
40 /* Addresses to scan */
41 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
42 
43 enum chips {
44 	lm85,
45 	adm1027, adt7463, adt7468,
46 	emc6d100, emc6d102, emc6d103, emc6d103s
47 };
48 
49 /* The LM85 registers */
50 
51 #define LM85_REG_IN(nr)			(0x20 + (nr))
52 #define LM85_REG_IN_MIN(nr)		(0x44 + (nr) * 2)
53 #define LM85_REG_IN_MAX(nr)		(0x45 + (nr) * 2)
54 
55 #define LM85_REG_TEMP(nr)		(0x25 + (nr))
56 #define LM85_REG_TEMP_MIN(nr)		(0x4e + (nr) * 2)
57 #define LM85_REG_TEMP_MAX(nr)		(0x4f + (nr) * 2)
58 
59 /* Fan speeds are LSB, MSB (2 bytes) */
60 #define LM85_REG_FAN(nr)		(0x28 + (nr) * 2)
61 #define LM85_REG_FAN_MIN(nr)		(0x54 + (nr) * 2)
62 
63 #define LM85_REG_PWM(nr)		(0x30 + (nr))
64 
65 #define LM85_REG_COMPANY		0x3e
66 #define LM85_REG_VERSTEP		0x3f
67 
68 #define ADT7468_REG_CFG5		0x7c
69 #define ADT7468_OFF64			(1 << 0)
70 #define ADT7468_HFPWM			(1 << 1)
71 #define IS_ADT7468_OFF64(data)		\
72 	((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
73 #define IS_ADT7468_HFPWM(data)		\
74 	((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
75 
76 /* These are the recognized values for the above regs */
77 #define LM85_COMPANY_NATIONAL		0x01
78 #define LM85_COMPANY_ANALOG_DEV		0x41
79 #define LM85_COMPANY_SMSC		0x5c
80 #define LM85_VERSTEP_LM85C		0x60
81 #define LM85_VERSTEP_LM85B		0x62
82 #define LM85_VERSTEP_LM96000_1		0x68
83 #define LM85_VERSTEP_LM96000_2		0x69
84 #define LM85_VERSTEP_ADM1027		0x60
85 #define LM85_VERSTEP_ADT7463		0x62
86 #define LM85_VERSTEP_ADT7463C		0x6A
87 #define LM85_VERSTEP_ADT7468_1		0x71
88 #define LM85_VERSTEP_ADT7468_2		0x72
89 #define LM85_VERSTEP_EMC6D100_A0        0x60
90 #define LM85_VERSTEP_EMC6D100_A1        0x61
91 #define LM85_VERSTEP_EMC6D102		0x65
92 #define LM85_VERSTEP_EMC6D103_A0	0x68
93 #define LM85_VERSTEP_EMC6D103_A1	0x69
94 #define LM85_VERSTEP_EMC6D103S		0x6A	/* Also known as EMC6D103:A2 */
95 
96 #define LM85_REG_CONFIG			0x40
97 
98 #define LM85_REG_ALARM1			0x41
99 #define LM85_REG_ALARM2			0x42
100 
101 #define LM85_REG_VID			0x43
102 
103 /* Automated FAN control */
104 #define LM85_REG_AFAN_CONFIG(nr)	(0x5c + (nr))
105 #define LM85_REG_AFAN_RANGE(nr)		(0x5f + (nr))
106 #define LM85_REG_AFAN_SPIKE1		0x62
107 #define LM85_REG_AFAN_MINPWM(nr)	(0x64 + (nr))
108 #define LM85_REG_AFAN_LIMIT(nr)		(0x67 + (nr))
109 #define LM85_REG_AFAN_CRITICAL(nr)	(0x6a + (nr))
110 #define LM85_REG_AFAN_HYST1		0x6d
111 #define LM85_REG_AFAN_HYST2		0x6e
112 
113 #define ADM1027_REG_EXTEND_ADC1		0x76
114 #define ADM1027_REG_EXTEND_ADC2		0x77
115 
116 #define EMC6D100_REG_ALARM3             0x7d
117 /* IN5, IN6 and IN7 */
118 #define EMC6D100_REG_IN(nr)             (0x70 + ((nr) - 5))
119 #define EMC6D100_REG_IN_MIN(nr)         (0x73 + ((nr) - 5) * 2)
120 #define EMC6D100_REG_IN_MAX(nr)         (0x74 + ((nr) - 5) * 2)
121 #define EMC6D102_REG_EXTEND_ADC1	0x85
122 #define EMC6D102_REG_EXTEND_ADC2	0x86
123 #define EMC6D102_REG_EXTEND_ADC3	0x87
124 #define EMC6D102_REG_EXTEND_ADC4	0x88
125 
126 /*
127  * Conversions. Rounding and limit checking is only done on the TO_REG
128  * variants. Note that you should be a bit careful with which arguments
129  * these macros are called: arguments may be evaluated more than once.
130  */
131 
132 /* IN are scaled according to built-in resistors */
133 static const int lm85_scaling[] = {  /* .001 Volts */
134 	2500, 2250, 3300, 5000, 12000,
135 	3300, 1500, 1800 /*EMC6D100*/
136 };
137 #define SCALE(val, from, to)	(((val) * (to) + ((from) / 2)) / (from))
138 
139 #define INS_TO_REG(n, val)	\
140 		SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
141 		      lm85_scaling[n], 192)
142 
143 #define INSEXT_FROM_REG(n, val, ext)	\
144 		SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
145 
146 #define INS_FROM_REG(n, val)	SCALE((val), 192, lm85_scaling[n])
147 
148 /* FAN speed is measured using 90kHz clock */
FAN_TO_REG(unsigned long val)149 static inline u16 FAN_TO_REG(unsigned long val)
150 {
151 	if (!val)
152 		return 0xffff;
153 	return clamp_val(5400000 / val, 1, 0xfffe);
154 }
155 #define FAN_FROM_REG(val)	((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
156 				 5400000 / (val))
157 
158 /* Temperature is reported in .001 degC increments */
159 #define TEMP_TO_REG(val)	\
160 		DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
161 #define TEMPEXT_FROM_REG(val, ext)	\
162 		SCALE(((val) << 4) + (ext), 16, 1000)
163 #define TEMP_FROM_REG(val)	((val) * 1000)
164 
165 #define PWM_TO_REG(val)			clamp_val(val, 0, 255)
166 #define PWM_FROM_REG(val)		(val)
167 
168 
169 /*
170  * ZONEs have the following parameters:
171  *    Limit (low) temp,           1. degC
172  *    Hysteresis (below limit),   1. degC (0-15)
173  *    Range of speed control,     .1 degC (2-80)
174  *    Critical (high) temp,       1. degC
175  *
176  * FAN PWMs have the following parameters:
177  *    Reference Zone,                 1, 2, 3, etc.
178  *    Spinup time,                    .05 sec
179  *    PWM value at limit/low temp,    1 count
180  *    PWM Frequency,                  1. Hz
181  *    PWM is Min or OFF below limit,  flag
182  *    Invert PWM output,              flag
183  *
184  * Some chips filter the temp, others the fan.
185  *    Filter constant (or disabled)   .1 seconds
186  */
187 
188 /* These are the zone temperature range encodings in .001 degree C */
189 static const int lm85_range_map[] = {
190 	2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
191 	13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
192 };
193 
RANGE_TO_REG(long range)194 static int RANGE_TO_REG(long range)
195 {
196 	return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map));
197 }
198 #define RANGE_FROM_REG(val)	lm85_range_map[(val) & 0x0f]
199 
200 /* These are the PWM frequency encodings */
201 static const int lm85_freq_map[8] = { /* 1 Hz */
202 	10, 15, 23, 30, 38, 47, 61, 94
203 };
204 static const int adm1027_freq_map[8] = { /* 1 Hz */
205 	11, 15, 22, 29, 35, 44, 59, 88
206 };
207 #define FREQ_MAP_LEN	8
208 
FREQ_TO_REG(const int * map,unsigned int map_size,unsigned long freq)209 static int FREQ_TO_REG(const int *map,
210 		       unsigned int map_size, unsigned long freq)
211 {
212 	return find_closest(freq, map, map_size);
213 }
214 
FREQ_FROM_REG(const int * map,u8 reg)215 static int FREQ_FROM_REG(const int *map, u8 reg)
216 {
217 	return map[reg & 0x07];
218 }
219 
220 /*
221  * Since we can't use strings, I'm abusing these numbers
222  *   to stand in for the following meanings:
223  *      1 -- PWM responds to Zone 1
224  *      2 -- PWM responds to Zone 2
225  *      3 -- PWM responds to Zone 3
226  *     23 -- PWM responds to the higher temp of Zone 2 or 3
227  *    123 -- PWM responds to highest of Zone 1, 2, or 3
228  *      0 -- PWM is always at 0% (ie, off)
229  *     -1 -- PWM is always at 100%
230  *     -2 -- PWM responds to manual control
231  */
232 
233 static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
234 #define ZONE_FROM_REG(val)	lm85_zone_map[(val) >> 5]
235 
ZONE_TO_REG(int zone)236 static int ZONE_TO_REG(int zone)
237 {
238 	int i;
239 
240 	for (i = 0; i <= 7; ++i)
241 		if (zone == lm85_zone_map[i])
242 			break;
243 	if (i > 7)   /* Not found. */
244 		i = 3;  /* Always 100% */
245 	return i << 5;
246 }
247 
248 #define HYST_TO_REG(val)	clamp_val(((val) + 500) / 1000, 0, 15)
249 #define HYST_FROM_REG(val)	((val) * 1000)
250 
251 /*
252  * Chip sampling rates
253  *
254  * Some sensors are not updated more frequently than once per second
255  *    so it doesn't make sense to read them more often than that.
256  *    We cache the results and return the saved data if the driver
257  *    is called again before a second has elapsed.
258  *
259  * Also, there is significant configuration data for this chip
260  *    given the automatic PWM fan control that is possible.  There
261  *    are about 47 bytes of config data to only 22 bytes of actual
262  *    readings.  So, we keep the config data up to date in the cache
263  *    when it is written and only sample it once every 1 *minute*
264  */
265 #define LM85_DATA_INTERVAL  (HZ + HZ / 2)
266 #define LM85_CONFIG_INTERVAL  (1 * 60 * HZ)
267 
268 /*
269  * LM85 can automatically adjust fan speeds based on temperature
270  * This structure encapsulates an entire Zone config.  There are
271  * three zones (one for each temperature input) on the lm85
272  */
273 struct lm85_zone {
274 	s8 limit;	/* Low temp limit */
275 	u8 hyst;	/* Low limit hysteresis. (0-15) */
276 	u8 range;	/* Temp range, encoded */
277 	s8 critical;	/* "All fans ON" temp limit */
278 	u8 max_desired; /*
279 			 * Actual "max" temperature specified.  Preserved
280 			 * to prevent "drift" as other autofan control
281 			 * values change.
282 			 */
283 };
284 
285 struct lm85_autofan {
286 	u8 config;	/* Register value */
287 	u8 min_pwm;	/* Minimum PWM value, encoded */
288 	u8 min_off;	/* Min PWM or OFF below "limit", flag */
289 };
290 
291 /*
292  * For each registered chip, we need to keep some data in memory.
293  * The structure is dynamically allocated.
294  */
295 struct lm85_data {
296 	struct i2c_client *client;
297 	const struct attribute_group *groups[6];
298 	const int *freq_map;
299 	enum chips type;
300 
301 	bool has_vid5;	/* true if VID5 is configured for ADT7463 or ADT7468 */
302 
303 	struct mutex update_lock;
304 	int valid;		/* !=0 if following fields are valid */
305 	unsigned long last_reading;	/* In jiffies */
306 	unsigned long last_config;	/* In jiffies */
307 
308 	u8 in[8];		/* Register value */
309 	u8 in_max[8];		/* Register value */
310 	u8 in_min[8];		/* Register value */
311 	s8 temp[3];		/* Register value */
312 	s8 temp_min[3];		/* Register value */
313 	s8 temp_max[3];		/* Register value */
314 	u16 fan[4];		/* Register value */
315 	u16 fan_min[4];		/* Register value */
316 	u8 pwm[3];		/* Register value */
317 	u8 pwm_freq[3];		/* Register encoding */
318 	u8 temp_ext[3];		/* Decoded values */
319 	u8 in_ext[8];		/* Decoded values */
320 	u8 vid;			/* Register value */
321 	u8 vrm;			/* VRM version */
322 	u32 alarms;		/* Register encoding, combined */
323 	u8 cfg5;		/* Config Register 5 on ADT7468 */
324 	struct lm85_autofan autofan[3];
325 	struct lm85_zone zone[3];
326 };
327 
lm85_read_value(struct i2c_client * client,u8 reg)328 static int lm85_read_value(struct i2c_client *client, u8 reg)
329 {
330 	int res;
331 
332 	/* What size location is it? */
333 	switch (reg) {
334 	case LM85_REG_FAN(0):  /* Read WORD data */
335 	case LM85_REG_FAN(1):
336 	case LM85_REG_FAN(2):
337 	case LM85_REG_FAN(3):
338 	case LM85_REG_FAN_MIN(0):
339 	case LM85_REG_FAN_MIN(1):
340 	case LM85_REG_FAN_MIN(2):
341 	case LM85_REG_FAN_MIN(3):
342 	case LM85_REG_ALARM1:	/* Read both bytes at once */
343 		res = i2c_smbus_read_byte_data(client, reg) & 0xff;
344 		res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
345 		break;
346 	default:	/* Read BYTE data */
347 		res = i2c_smbus_read_byte_data(client, reg);
348 		break;
349 	}
350 
351 	return res;
352 }
353 
lm85_write_value(struct i2c_client * client,u8 reg,int value)354 static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
355 {
356 	switch (reg) {
357 	case LM85_REG_FAN(0):  /* Write WORD data */
358 	case LM85_REG_FAN(1):
359 	case LM85_REG_FAN(2):
360 	case LM85_REG_FAN(3):
361 	case LM85_REG_FAN_MIN(0):
362 	case LM85_REG_FAN_MIN(1):
363 	case LM85_REG_FAN_MIN(2):
364 	case LM85_REG_FAN_MIN(3):
365 	/* NOTE: ALARM is read only, so not included here */
366 		i2c_smbus_write_byte_data(client, reg, value & 0xff);
367 		i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
368 		break;
369 	default:	/* Write BYTE data */
370 		i2c_smbus_write_byte_data(client, reg, value);
371 		break;
372 	}
373 }
374 
lm85_update_device(struct device * dev)375 static struct lm85_data *lm85_update_device(struct device *dev)
376 {
377 	struct lm85_data *data = dev_get_drvdata(dev);
378 	struct i2c_client *client = data->client;
379 	int i;
380 
381 	mutex_lock(&data->update_lock);
382 
383 	if (!data->valid ||
384 	     time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
385 		/* Things that change quickly */
386 		dev_dbg(&client->dev, "Reading sensor values\n");
387 
388 		/*
389 		 * Have to read extended bits first to "freeze" the
390 		 * more significant bits that are read later.
391 		 * There are 2 additional resolution bits per channel and we
392 		 * have room for 4, so we shift them to the left.
393 		 */
394 		if (data->type == adm1027 || data->type == adt7463 ||
395 		    data->type == adt7468) {
396 			int ext1 = lm85_read_value(client,
397 						   ADM1027_REG_EXTEND_ADC1);
398 			int ext2 =  lm85_read_value(client,
399 						    ADM1027_REG_EXTEND_ADC2);
400 			int val = (ext1 << 8) + ext2;
401 
402 			for (i = 0; i <= 4; i++)
403 				data->in_ext[i] =
404 					((val >> (i * 2)) & 0x03) << 2;
405 
406 			for (i = 0; i <= 2; i++)
407 				data->temp_ext[i] =
408 					(val >> ((i + 4) * 2)) & 0x0c;
409 		}
410 
411 		data->vid = lm85_read_value(client, LM85_REG_VID);
412 
413 		for (i = 0; i <= 3; ++i) {
414 			data->in[i] =
415 			    lm85_read_value(client, LM85_REG_IN(i));
416 			data->fan[i] =
417 			    lm85_read_value(client, LM85_REG_FAN(i));
418 		}
419 
420 		if (!data->has_vid5)
421 			data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
422 
423 		if (data->type == adt7468)
424 			data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
425 
426 		for (i = 0; i <= 2; ++i) {
427 			data->temp[i] =
428 			    lm85_read_value(client, LM85_REG_TEMP(i));
429 			data->pwm[i] =
430 			    lm85_read_value(client, LM85_REG_PWM(i));
431 
432 			if (IS_ADT7468_OFF64(data))
433 				data->temp[i] -= 64;
434 		}
435 
436 		data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
437 
438 		if (data->type == emc6d100) {
439 			/* Three more voltage sensors */
440 			for (i = 5; i <= 7; ++i) {
441 				data->in[i] = lm85_read_value(client,
442 							EMC6D100_REG_IN(i));
443 			}
444 			/* More alarm bits */
445 			data->alarms |= lm85_read_value(client,
446 						EMC6D100_REG_ALARM3) << 16;
447 		} else if (data->type == emc6d102 || data->type == emc6d103 ||
448 			   data->type == emc6d103s) {
449 			/*
450 			 * Have to read LSB bits after the MSB ones because
451 			 * the reading of the MSB bits has frozen the
452 			 * LSBs (backward from the ADM1027).
453 			 */
454 			int ext1 = lm85_read_value(client,
455 						   EMC6D102_REG_EXTEND_ADC1);
456 			int ext2 = lm85_read_value(client,
457 						   EMC6D102_REG_EXTEND_ADC2);
458 			int ext3 = lm85_read_value(client,
459 						   EMC6D102_REG_EXTEND_ADC3);
460 			int ext4 = lm85_read_value(client,
461 						   EMC6D102_REG_EXTEND_ADC4);
462 			data->in_ext[0] = ext3 & 0x0f;
463 			data->in_ext[1] = ext4 & 0x0f;
464 			data->in_ext[2] = ext4 >> 4;
465 			data->in_ext[3] = ext3 >> 4;
466 			data->in_ext[4] = ext2 >> 4;
467 
468 			data->temp_ext[0] = ext1 & 0x0f;
469 			data->temp_ext[1] = ext2 & 0x0f;
470 			data->temp_ext[2] = ext1 >> 4;
471 		}
472 
473 		data->last_reading = jiffies;
474 	}  /* last_reading */
475 
476 	if (!data->valid ||
477 	     time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
478 		/* Things that don't change often */
479 		dev_dbg(&client->dev, "Reading config values\n");
480 
481 		for (i = 0; i <= 3; ++i) {
482 			data->in_min[i] =
483 			    lm85_read_value(client, LM85_REG_IN_MIN(i));
484 			data->in_max[i] =
485 			    lm85_read_value(client, LM85_REG_IN_MAX(i));
486 			data->fan_min[i] =
487 			    lm85_read_value(client, LM85_REG_FAN_MIN(i));
488 		}
489 
490 		if (!data->has_vid5)  {
491 			data->in_min[4] = lm85_read_value(client,
492 					  LM85_REG_IN_MIN(4));
493 			data->in_max[4] = lm85_read_value(client,
494 					  LM85_REG_IN_MAX(4));
495 		}
496 
497 		if (data->type == emc6d100) {
498 			for (i = 5; i <= 7; ++i) {
499 				data->in_min[i] = lm85_read_value(client,
500 						EMC6D100_REG_IN_MIN(i));
501 				data->in_max[i] = lm85_read_value(client,
502 						EMC6D100_REG_IN_MAX(i));
503 			}
504 		}
505 
506 		for (i = 0; i <= 2; ++i) {
507 			int val;
508 
509 			data->temp_min[i] =
510 			    lm85_read_value(client, LM85_REG_TEMP_MIN(i));
511 			data->temp_max[i] =
512 			    lm85_read_value(client, LM85_REG_TEMP_MAX(i));
513 
514 			data->autofan[i].config =
515 			    lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
516 			val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
517 			data->pwm_freq[i] = val & 0x07;
518 			data->zone[i].range = val >> 4;
519 			data->autofan[i].min_pwm =
520 			    lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
521 			data->zone[i].limit =
522 			    lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
523 			data->zone[i].critical =
524 			    lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
525 
526 			if (IS_ADT7468_OFF64(data)) {
527 				data->temp_min[i] -= 64;
528 				data->temp_max[i] -= 64;
529 				data->zone[i].limit -= 64;
530 				data->zone[i].critical -= 64;
531 			}
532 		}
533 
534 		if (data->type != emc6d103s) {
535 			i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
536 			data->autofan[0].min_off = (i & 0x20) != 0;
537 			data->autofan[1].min_off = (i & 0x40) != 0;
538 			data->autofan[2].min_off = (i & 0x80) != 0;
539 
540 			i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
541 			data->zone[0].hyst = i >> 4;
542 			data->zone[1].hyst = i & 0x0f;
543 
544 			i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
545 			data->zone[2].hyst = i >> 4;
546 		}
547 
548 		data->last_config = jiffies;
549 	}  /* last_config */
550 
551 	data->valid = 1;
552 
553 	mutex_unlock(&data->update_lock);
554 
555 	return data;
556 }
557 
558 /* 4 Fans */
show_fan(struct device * dev,struct device_attribute * attr,char * buf)559 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
560 		char *buf)
561 {
562 	int nr = to_sensor_dev_attr(attr)->index;
563 	struct lm85_data *data = lm85_update_device(dev);
564 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
565 }
566 
show_fan_min(struct device * dev,struct device_attribute * attr,char * buf)567 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
568 		char *buf)
569 {
570 	int nr = to_sensor_dev_attr(attr)->index;
571 	struct lm85_data *data = lm85_update_device(dev);
572 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
573 }
574 
set_fan_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)575 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
576 		const char *buf, size_t count)
577 {
578 	int nr = to_sensor_dev_attr(attr)->index;
579 	struct lm85_data *data = dev_get_drvdata(dev);
580 	struct i2c_client *client = data->client;
581 	unsigned long val;
582 	int err;
583 
584 	err = kstrtoul(buf, 10, &val);
585 	if (err)
586 		return err;
587 
588 	mutex_lock(&data->update_lock);
589 	data->fan_min[nr] = FAN_TO_REG(val);
590 	lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
591 	mutex_unlock(&data->update_lock);
592 	return count;
593 }
594 
595 #define show_fan_offset(offset)						\
596 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,			\
597 		show_fan, NULL, offset - 1);				\
598 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,		\
599 		show_fan_min, set_fan_min, offset - 1)
600 
601 show_fan_offset(1);
602 show_fan_offset(2);
603 show_fan_offset(3);
604 show_fan_offset(4);
605 
606 /* vid, vrm, alarms */
607 
cpu0_vid_show(struct device * dev,struct device_attribute * attr,char * buf)608 static ssize_t cpu0_vid_show(struct device *dev,
609 			     struct device_attribute *attr, char *buf)
610 {
611 	struct lm85_data *data = lm85_update_device(dev);
612 	int vid;
613 
614 	if (data->has_vid5) {
615 		/* 6-pin VID (VRM 10) */
616 		vid = vid_from_reg(data->vid & 0x3f, data->vrm);
617 	} else {
618 		/* 5-pin VID (VRM 9) */
619 		vid = vid_from_reg(data->vid & 0x1f, data->vrm);
620 	}
621 
622 	return sprintf(buf, "%d\n", vid);
623 }
624 
625 static DEVICE_ATTR_RO(cpu0_vid);
626 
vrm_show(struct device * dev,struct device_attribute * attr,char * buf)627 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
628 			char *buf)
629 {
630 	struct lm85_data *data = dev_get_drvdata(dev);
631 	return sprintf(buf, "%ld\n", (long) data->vrm);
632 }
633 
vrm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)634 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
635 			 const char *buf, size_t count)
636 {
637 	struct lm85_data *data = dev_get_drvdata(dev);
638 	unsigned long val;
639 	int err;
640 
641 	err = kstrtoul(buf, 10, &val);
642 	if (err)
643 		return err;
644 
645 	if (val > 255)
646 		return -EINVAL;
647 
648 	data->vrm = val;
649 	return count;
650 }
651 
652 static DEVICE_ATTR_RW(vrm);
653 
alarms_show(struct device * dev,struct device_attribute * attr,char * buf)654 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
655 			   char *buf)
656 {
657 	struct lm85_data *data = lm85_update_device(dev);
658 	return sprintf(buf, "%u\n", data->alarms);
659 }
660 
661 static DEVICE_ATTR_RO(alarms);
662 
show_alarm(struct device * dev,struct device_attribute * attr,char * buf)663 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
664 		char *buf)
665 {
666 	int nr = to_sensor_dev_attr(attr)->index;
667 	struct lm85_data *data = lm85_update_device(dev);
668 	return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
669 }
670 
671 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
672 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
673 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
674 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
675 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
676 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
677 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
678 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
679 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
680 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
681 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
682 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
683 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
684 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
685 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
686 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
687 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
688 
689 /* pwm */
690 
show_pwm(struct device * dev,struct device_attribute * attr,char * buf)691 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
692 		char *buf)
693 {
694 	int nr = to_sensor_dev_attr(attr)->index;
695 	struct lm85_data *data = lm85_update_device(dev);
696 	return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
697 }
698 
set_pwm(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)699 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
700 		const char *buf, size_t count)
701 {
702 	int nr = to_sensor_dev_attr(attr)->index;
703 	struct lm85_data *data = dev_get_drvdata(dev);
704 	struct i2c_client *client = data->client;
705 	unsigned long val;
706 	int err;
707 
708 	err = kstrtoul(buf, 10, &val);
709 	if (err)
710 		return err;
711 
712 	mutex_lock(&data->update_lock);
713 	data->pwm[nr] = PWM_TO_REG(val);
714 	lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
715 	mutex_unlock(&data->update_lock);
716 	return count;
717 }
718 
show_pwm_enable(struct device * dev,struct device_attribute * attr,char * buf)719 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
720 		*attr, char *buf)
721 {
722 	int nr = to_sensor_dev_attr(attr)->index;
723 	struct lm85_data *data = lm85_update_device(dev);
724 	int pwm_zone, enable;
725 
726 	pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
727 	switch (pwm_zone) {
728 	case -1:	/* PWM is always at 100% */
729 		enable = 0;
730 		break;
731 	case 0:		/* PWM is always at 0% */
732 	case -2:	/* PWM responds to manual control */
733 		enable = 1;
734 		break;
735 	default:	/* PWM in automatic mode */
736 		enable = 2;
737 	}
738 	return sprintf(buf, "%d\n", enable);
739 }
740 
set_pwm_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)741 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
742 		*attr, const char *buf, size_t count)
743 {
744 	int nr = to_sensor_dev_attr(attr)->index;
745 	struct lm85_data *data = dev_get_drvdata(dev);
746 	struct i2c_client *client = data->client;
747 	u8 config;
748 	unsigned long val;
749 	int err;
750 
751 	err = kstrtoul(buf, 10, &val);
752 	if (err)
753 		return err;
754 
755 	switch (val) {
756 	case 0:
757 		config = 3;
758 		break;
759 	case 1:
760 		config = 7;
761 		break;
762 	case 2:
763 		/*
764 		 * Here we have to choose arbitrarily one of the 5 possible
765 		 * configurations; I go for the safest
766 		 */
767 		config = 6;
768 		break;
769 	default:
770 		return -EINVAL;
771 	}
772 
773 	mutex_lock(&data->update_lock);
774 	data->autofan[nr].config = lm85_read_value(client,
775 		LM85_REG_AFAN_CONFIG(nr));
776 	data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
777 		| (config << 5);
778 	lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
779 		data->autofan[nr].config);
780 	mutex_unlock(&data->update_lock);
781 	return count;
782 }
783 
show_pwm_freq(struct device * dev,struct device_attribute * attr,char * buf)784 static ssize_t show_pwm_freq(struct device *dev,
785 		struct device_attribute *attr, char *buf)
786 {
787 	int nr = to_sensor_dev_attr(attr)->index;
788 	struct lm85_data *data = lm85_update_device(dev);
789 	int freq;
790 
791 	if (IS_ADT7468_HFPWM(data))
792 		freq = 22500;
793 	else
794 		freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
795 
796 	return sprintf(buf, "%d\n", freq);
797 }
798 
set_pwm_freq(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)799 static ssize_t set_pwm_freq(struct device *dev,
800 		struct device_attribute *attr, const char *buf, size_t count)
801 {
802 	int nr = to_sensor_dev_attr(attr)->index;
803 	struct lm85_data *data = dev_get_drvdata(dev);
804 	struct i2c_client *client = data->client;
805 	unsigned long val;
806 	int err;
807 
808 	err = kstrtoul(buf, 10, &val);
809 	if (err)
810 		return err;
811 
812 	mutex_lock(&data->update_lock);
813 	/*
814 	 * The ADT7468 has a special high-frequency PWM output mode,
815 	 * where all PWM outputs are driven by a 22.5 kHz clock.
816 	 * This might confuse the user, but there's not much we can do.
817 	 */
818 	if (data->type == adt7468 && val >= 11300) {	/* High freq. mode */
819 		data->cfg5 &= ~ADT7468_HFPWM;
820 		lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
821 	} else {					/* Low freq. mode */
822 		data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map,
823 						 FREQ_MAP_LEN, val);
824 		lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
825 				 (data->zone[nr].range << 4)
826 				 | data->pwm_freq[nr]);
827 		if (data->type == adt7468) {
828 			data->cfg5 |= ADT7468_HFPWM;
829 			lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
830 		}
831 	}
832 	mutex_unlock(&data->update_lock);
833 	return count;
834 }
835 
836 #define show_pwm_reg(offset)						\
837 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR,		\
838 		show_pwm, set_pwm, offset - 1);				\
839 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR,	\
840 		show_pwm_enable, set_pwm_enable, offset - 1);		\
841 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR,	\
842 		show_pwm_freq, set_pwm_freq, offset - 1)
843 
844 show_pwm_reg(1);
845 show_pwm_reg(2);
846 show_pwm_reg(3);
847 
848 /* Voltages */
849 
show_in(struct device * dev,struct device_attribute * attr,char * buf)850 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
851 		char *buf)
852 {
853 	int nr = to_sensor_dev_attr(attr)->index;
854 	struct lm85_data *data = lm85_update_device(dev);
855 	return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
856 						    data->in_ext[nr]));
857 }
858 
show_in_min(struct device * dev,struct device_attribute * attr,char * buf)859 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
860 		char *buf)
861 {
862 	int nr = to_sensor_dev_attr(attr)->index;
863 	struct lm85_data *data = lm85_update_device(dev);
864 	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
865 }
866 
set_in_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)867 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
868 		const char *buf, size_t count)
869 {
870 	int nr = to_sensor_dev_attr(attr)->index;
871 	struct lm85_data *data = dev_get_drvdata(dev);
872 	struct i2c_client *client = data->client;
873 	long val;
874 	int err;
875 
876 	err = kstrtol(buf, 10, &val);
877 	if (err)
878 		return err;
879 
880 	mutex_lock(&data->update_lock);
881 	data->in_min[nr] = INS_TO_REG(nr, val);
882 	lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
883 	mutex_unlock(&data->update_lock);
884 	return count;
885 }
886 
show_in_max(struct device * dev,struct device_attribute * attr,char * buf)887 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
888 		char *buf)
889 {
890 	int nr = to_sensor_dev_attr(attr)->index;
891 	struct lm85_data *data = lm85_update_device(dev);
892 	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
893 }
894 
set_in_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)895 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
896 		const char *buf, size_t count)
897 {
898 	int nr = to_sensor_dev_attr(attr)->index;
899 	struct lm85_data *data = dev_get_drvdata(dev);
900 	struct i2c_client *client = data->client;
901 	long val;
902 	int err;
903 
904 	err = kstrtol(buf, 10, &val);
905 	if (err)
906 		return err;
907 
908 	mutex_lock(&data->update_lock);
909 	data->in_max[nr] = INS_TO_REG(nr, val);
910 	lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
911 	mutex_unlock(&data->update_lock);
912 	return count;
913 }
914 
915 #define show_in_reg(offset)						\
916 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO,			\
917 		show_in, NULL, offset);					\
918 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,		\
919 		show_in_min, set_in_min, offset);			\
920 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,		\
921 		show_in_max, set_in_max, offset)
922 
923 show_in_reg(0);
924 show_in_reg(1);
925 show_in_reg(2);
926 show_in_reg(3);
927 show_in_reg(4);
928 show_in_reg(5);
929 show_in_reg(6);
930 show_in_reg(7);
931 
932 /* Temps */
933 
show_temp(struct device * dev,struct device_attribute * attr,char * buf)934 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
935 		char *buf)
936 {
937 	int nr = to_sensor_dev_attr(attr)->index;
938 	struct lm85_data *data = lm85_update_device(dev);
939 	return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
940 						     data->temp_ext[nr]));
941 }
942 
show_temp_min(struct device * dev,struct device_attribute * attr,char * buf)943 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
944 		char *buf)
945 {
946 	int nr = to_sensor_dev_attr(attr)->index;
947 	struct lm85_data *data = lm85_update_device(dev);
948 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
949 }
950 
set_temp_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)951 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
952 		const char *buf, size_t count)
953 {
954 	int nr = to_sensor_dev_attr(attr)->index;
955 	struct lm85_data *data = dev_get_drvdata(dev);
956 	struct i2c_client *client = data->client;
957 	long val;
958 	int err;
959 
960 	err = kstrtol(buf, 10, &val);
961 	if (err)
962 		return err;
963 
964 	if (IS_ADT7468_OFF64(data))
965 		val += 64;
966 
967 	mutex_lock(&data->update_lock);
968 	data->temp_min[nr] = TEMP_TO_REG(val);
969 	lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
970 	mutex_unlock(&data->update_lock);
971 	return count;
972 }
973 
show_temp_max(struct device * dev,struct device_attribute * attr,char * buf)974 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
975 		char *buf)
976 {
977 	int nr = to_sensor_dev_attr(attr)->index;
978 	struct lm85_data *data = lm85_update_device(dev);
979 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
980 }
981 
set_temp_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)982 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
983 		const char *buf, size_t count)
984 {
985 	int nr = to_sensor_dev_attr(attr)->index;
986 	struct lm85_data *data = dev_get_drvdata(dev);
987 	struct i2c_client *client = data->client;
988 	long val;
989 	int err;
990 
991 	err = kstrtol(buf, 10, &val);
992 	if (err)
993 		return err;
994 
995 	if (IS_ADT7468_OFF64(data))
996 		val += 64;
997 
998 	mutex_lock(&data->update_lock);
999 	data->temp_max[nr] = TEMP_TO_REG(val);
1000 	lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
1001 	mutex_unlock(&data->update_lock);
1002 	return count;
1003 }
1004 
1005 #define show_temp_reg(offset)						\
1006 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO,		\
1007 		show_temp, NULL, offset - 1);				\
1008 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,	\
1009 		show_temp_min, set_temp_min, offset - 1);		\
1010 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,	\
1011 		show_temp_max, set_temp_max, offset - 1);
1012 
1013 show_temp_reg(1);
1014 show_temp_reg(2);
1015 show_temp_reg(3);
1016 
1017 
1018 /* Automatic PWM control */
1019 
show_pwm_auto_channels(struct device * dev,struct device_attribute * attr,char * buf)1020 static ssize_t show_pwm_auto_channels(struct device *dev,
1021 		struct device_attribute *attr, char *buf)
1022 {
1023 	int nr = to_sensor_dev_attr(attr)->index;
1024 	struct lm85_data *data = lm85_update_device(dev);
1025 	return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
1026 }
1027 
set_pwm_auto_channels(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1028 static ssize_t set_pwm_auto_channels(struct device *dev,
1029 		struct device_attribute *attr, const char *buf, size_t count)
1030 {
1031 	int nr = to_sensor_dev_attr(attr)->index;
1032 	struct lm85_data *data = dev_get_drvdata(dev);
1033 	struct i2c_client *client = data->client;
1034 	long val;
1035 	int err;
1036 
1037 	err = kstrtol(buf, 10, &val);
1038 	if (err)
1039 		return err;
1040 
1041 	mutex_lock(&data->update_lock);
1042 	data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
1043 		| ZONE_TO_REG(val);
1044 	lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1045 		data->autofan[nr].config);
1046 	mutex_unlock(&data->update_lock);
1047 	return count;
1048 }
1049 
show_pwm_auto_pwm_min(struct device * dev,struct device_attribute * attr,char * buf)1050 static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1051 		struct device_attribute *attr, char *buf)
1052 {
1053 	int nr = to_sensor_dev_attr(attr)->index;
1054 	struct lm85_data *data = lm85_update_device(dev);
1055 	return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
1056 }
1057 
set_pwm_auto_pwm_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1058 static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1059 		struct device_attribute *attr, const char *buf, size_t count)
1060 {
1061 	int nr = to_sensor_dev_attr(attr)->index;
1062 	struct lm85_data *data = dev_get_drvdata(dev);
1063 	struct i2c_client *client = data->client;
1064 	unsigned long val;
1065 	int err;
1066 
1067 	err = kstrtoul(buf, 10, &val);
1068 	if (err)
1069 		return err;
1070 
1071 	mutex_lock(&data->update_lock);
1072 	data->autofan[nr].min_pwm = PWM_TO_REG(val);
1073 	lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1074 		data->autofan[nr].min_pwm);
1075 	mutex_unlock(&data->update_lock);
1076 	return count;
1077 }
1078 
show_pwm_auto_pwm_minctl(struct device * dev,struct device_attribute * attr,char * buf)1079 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1080 		struct device_attribute *attr, char *buf)
1081 {
1082 	int nr = to_sensor_dev_attr(attr)->index;
1083 	struct lm85_data *data = lm85_update_device(dev);
1084 	return sprintf(buf, "%d\n", data->autofan[nr].min_off);
1085 }
1086 
set_pwm_auto_pwm_minctl(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1087 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1088 		struct device_attribute *attr, const char *buf, size_t count)
1089 {
1090 	int nr = to_sensor_dev_attr(attr)->index;
1091 	struct lm85_data *data = dev_get_drvdata(dev);
1092 	struct i2c_client *client = data->client;
1093 	u8 tmp;
1094 	long val;
1095 	int err;
1096 
1097 	err = kstrtol(buf, 10, &val);
1098 	if (err)
1099 		return err;
1100 
1101 	mutex_lock(&data->update_lock);
1102 	data->autofan[nr].min_off = val;
1103 	tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1104 	tmp &= ~(0x20 << nr);
1105 	if (data->autofan[nr].min_off)
1106 		tmp |= 0x20 << nr;
1107 	lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
1108 	mutex_unlock(&data->update_lock);
1109 	return count;
1110 }
1111 
1112 #define pwm_auto(offset)						\
1113 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels,			\
1114 		S_IRUGO | S_IWUSR, show_pwm_auto_channels,		\
1115 		set_pwm_auto_channels, offset - 1);			\
1116 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min,			\
1117 		S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min,		\
1118 		set_pwm_auto_pwm_min, offset - 1);			\
1119 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl,		\
1120 		S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl,		\
1121 		set_pwm_auto_pwm_minctl, offset - 1)
1122 
1123 pwm_auto(1);
1124 pwm_auto(2);
1125 pwm_auto(3);
1126 
1127 /* Temperature settings for automatic PWM control */
1128 
show_temp_auto_temp_off(struct device * dev,struct device_attribute * attr,char * buf)1129 static ssize_t show_temp_auto_temp_off(struct device *dev,
1130 		struct device_attribute *attr, char *buf)
1131 {
1132 	int nr = to_sensor_dev_attr(attr)->index;
1133 	struct lm85_data *data = lm85_update_device(dev);
1134 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
1135 		HYST_FROM_REG(data->zone[nr].hyst));
1136 }
1137 
set_temp_auto_temp_off(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1138 static ssize_t set_temp_auto_temp_off(struct device *dev,
1139 		struct device_attribute *attr, const char *buf, size_t count)
1140 {
1141 	int nr = to_sensor_dev_attr(attr)->index;
1142 	struct lm85_data *data = dev_get_drvdata(dev);
1143 	struct i2c_client *client = data->client;
1144 	int min;
1145 	long val;
1146 	int err;
1147 
1148 	err = kstrtol(buf, 10, &val);
1149 	if (err)
1150 		return err;
1151 
1152 	mutex_lock(&data->update_lock);
1153 	min = TEMP_FROM_REG(data->zone[nr].limit);
1154 	data->zone[nr].hyst = HYST_TO_REG(min - val);
1155 	if (nr == 0 || nr == 1) {
1156 		lm85_write_value(client, LM85_REG_AFAN_HYST1,
1157 			(data->zone[0].hyst << 4)
1158 			| data->zone[1].hyst);
1159 	} else {
1160 		lm85_write_value(client, LM85_REG_AFAN_HYST2,
1161 			(data->zone[2].hyst << 4));
1162 	}
1163 	mutex_unlock(&data->update_lock);
1164 	return count;
1165 }
1166 
show_temp_auto_temp_min(struct device * dev,struct device_attribute * attr,char * buf)1167 static ssize_t show_temp_auto_temp_min(struct device *dev,
1168 		struct device_attribute *attr, char *buf)
1169 {
1170 	int nr = to_sensor_dev_attr(attr)->index;
1171 	struct lm85_data *data = lm85_update_device(dev);
1172 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
1173 }
1174 
set_temp_auto_temp_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1175 static ssize_t set_temp_auto_temp_min(struct device *dev,
1176 		struct device_attribute *attr, const char *buf, size_t count)
1177 {
1178 	int nr = to_sensor_dev_attr(attr)->index;
1179 	struct lm85_data *data = dev_get_drvdata(dev);
1180 	struct i2c_client *client = data->client;
1181 	long val;
1182 	int err;
1183 
1184 	err = kstrtol(buf, 10, &val);
1185 	if (err)
1186 		return err;
1187 
1188 	mutex_lock(&data->update_lock);
1189 	data->zone[nr].limit = TEMP_TO_REG(val);
1190 	lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1191 		data->zone[nr].limit);
1192 
1193 /* Update temp_auto_max and temp_auto_range */
1194 	data->zone[nr].range = RANGE_TO_REG(
1195 		TEMP_FROM_REG(data->zone[nr].max_desired) -
1196 		TEMP_FROM_REG(data->zone[nr].limit));
1197 	lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1198 		((data->zone[nr].range & 0x0f) << 4)
1199 		| (data->pwm_freq[nr] & 0x07));
1200 
1201 	mutex_unlock(&data->update_lock);
1202 	return count;
1203 }
1204 
show_temp_auto_temp_max(struct device * dev,struct device_attribute * attr,char * buf)1205 static ssize_t show_temp_auto_temp_max(struct device *dev,
1206 		struct device_attribute *attr, char *buf)
1207 {
1208 	int nr = to_sensor_dev_attr(attr)->index;
1209 	struct lm85_data *data = lm85_update_device(dev);
1210 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
1211 		RANGE_FROM_REG(data->zone[nr].range));
1212 }
1213 
set_temp_auto_temp_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1214 static ssize_t set_temp_auto_temp_max(struct device *dev,
1215 		struct device_attribute *attr, const char *buf, size_t count)
1216 {
1217 	int nr = to_sensor_dev_attr(attr)->index;
1218 	struct lm85_data *data = dev_get_drvdata(dev);
1219 	struct i2c_client *client = data->client;
1220 	int min;
1221 	long val;
1222 	int err;
1223 
1224 	err = kstrtol(buf, 10, &val);
1225 	if (err)
1226 		return err;
1227 
1228 	mutex_lock(&data->update_lock);
1229 	min = TEMP_FROM_REG(data->zone[nr].limit);
1230 	data->zone[nr].max_desired = TEMP_TO_REG(val);
1231 	data->zone[nr].range = RANGE_TO_REG(
1232 		val - min);
1233 	lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1234 		((data->zone[nr].range & 0x0f) << 4)
1235 		| (data->pwm_freq[nr] & 0x07));
1236 	mutex_unlock(&data->update_lock);
1237 	return count;
1238 }
1239 
show_temp_auto_temp_crit(struct device * dev,struct device_attribute * attr,char * buf)1240 static ssize_t show_temp_auto_temp_crit(struct device *dev,
1241 		struct device_attribute *attr, char *buf)
1242 {
1243 	int nr = to_sensor_dev_attr(attr)->index;
1244 	struct lm85_data *data = lm85_update_device(dev);
1245 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
1246 }
1247 
set_temp_auto_temp_crit(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1248 static ssize_t set_temp_auto_temp_crit(struct device *dev,
1249 		struct device_attribute *attr, const char *buf, size_t count)
1250 {
1251 	int nr = to_sensor_dev_attr(attr)->index;
1252 	struct lm85_data *data = dev_get_drvdata(dev);
1253 	struct i2c_client *client = data->client;
1254 	long val;
1255 	int err;
1256 
1257 	err = kstrtol(buf, 10, &val);
1258 	if (err)
1259 		return err;
1260 
1261 	mutex_lock(&data->update_lock);
1262 	data->zone[nr].critical = TEMP_TO_REG(val);
1263 	lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1264 		data->zone[nr].critical);
1265 	mutex_unlock(&data->update_lock);
1266 	return count;
1267 }
1268 
1269 #define temp_auto(offset)						\
1270 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off,			\
1271 		S_IRUGO | S_IWUSR, show_temp_auto_temp_off,		\
1272 		set_temp_auto_temp_off, offset - 1);			\
1273 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min,			\
1274 		S_IRUGO | S_IWUSR, show_temp_auto_temp_min,		\
1275 		set_temp_auto_temp_min, offset - 1);			\
1276 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max,			\
1277 		S_IRUGO | S_IWUSR, show_temp_auto_temp_max,		\
1278 		set_temp_auto_temp_max, offset - 1);			\
1279 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit,		\
1280 		S_IRUGO | S_IWUSR, show_temp_auto_temp_crit,		\
1281 		set_temp_auto_temp_crit, offset - 1);
1282 
1283 temp_auto(1);
1284 temp_auto(2);
1285 temp_auto(3);
1286 
1287 static struct attribute *lm85_attributes[] = {
1288 	&sensor_dev_attr_fan1_input.dev_attr.attr,
1289 	&sensor_dev_attr_fan2_input.dev_attr.attr,
1290 	&sensor_dev_attr_fan3_input.dev_attr.attr,
1291 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1292 	&sensor_dev_attr_fan1_min.dev_attr.attr,
1293 	&sensor_dev_attr_fan2_min.dev_attr.attr,
1294 	&sensor_dev_attr_fan3_min.dev_attr.attr,
1295 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1296 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1297 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1298 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1299 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1300 
1301 	&sensor_dev_attr_pwm1.dev_attr.attr,
1302 	&sensor_dev_attr_pwm2.dev_attr.attr,
1303 	&sensor_dev_attr_pwm3.dev_attr.attr,
1304 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
1305 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1306 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
1307 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
1308 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1309 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
1310 
1311 	&sensor_dev_attr_in0_input.dev_attr.attr,
1312 	&sensor_dev_attr_in1_input.dev_attr.attr,
1313 	&sensor_dev_attr_in2_input.dev_attr.attr,
1314 	&sensor_dev_attr_in3_input.dev_attr.attr,
1315 	&sensor_dev_attr_in0_min.dev_attr.attr,
1316 	&sensor_dev_attr_in1_min.dev_attr.attr,
1317 	&sensor_dev_attr_in2_min.dev_attr.attr,
1318 	&sensor_dev_attr_in3_min.dev_attr.attr,
1319 	&sensor_dev_attr_in0_max.dev_attr.attr,
1320 	&sensor_dev_attr_in1_max.dev_attr.attr,
1321 	&sensor_dev_attr_in2_max.dev_attr.attr,
1322 	&sensor_dev_attr_in3_max.dev_attr.attr,
1323 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1324 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
1325 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
1326 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1327 
1328 	&sensor_dev_attr_temp1_input.dev_attr.attr,
1329 	&sensor_dev_attr_temp2_input.dev_attr.attr,
1330 	&sensor_dev_attr_temp3_input.dev_attr.attr,
1331 	&sensor_dev_attr_temp1_min.dev_attr.attr,
1332 	&sensor_dev_attr_temp2_min.dev_attr.attr,
1333 	&sensor_dev_attr_temp3_min.dev_attr.attr,
1334 	&sensor_dev_attr_temp1_max.dev_attr.attr,
1335 	&sensor_dev_attr_temp2_max.dev_attr.attr,
1336 	&sensor_dev_attr_temp3_max.dev_attr.attr,
1337 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1338 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1339 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1340 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
1341 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
1342 
1343 	&sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1344 	&sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1345 	&sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1346 	&sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1347 	&sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1348 	&sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1349 
1350 	&sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1351 	&sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1352 	&sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1353 	&sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1354 	&sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1355 	&sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1356 	&sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1357 	&sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1358 	&sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1359 
1360 	&dev_attr_vrm.attr,
1361 	&dev_attr_cpu0_vid.attr,
1362 	&dev_attr_alarms.attr,
1363 	NULL
1364 };
1365 
1366 static const struct attribute_group lm85_group = {
1367 	.attrs = lm85_attributes,
1368 };
1369 
1370 static struct attribute *lm85_attributes_minctl[] = {
1371 	&sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1372 	&sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1373 	&sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1374 	NULL
1375 };
1376 
1377 static const struct attribute_group lm85_group_minctl = {
1378 	.attrs = lm85_attributes_minctl,
1379 };
1380 
1381 static struct attribute *lm85_attributes_temp_off[] = {
1382 	&sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1383 	&sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1384 	&sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1385 	NULL
1386 };
1387 
1388 static const struct attribute_group lm85_group_temp_off = {
1389 	.attrs = lm85_attributes_temp_off,
1390 };
1391 
1392 static struct attribute *lm85_attributes_in4[] = {
1393 	&sensor_dev_attr_in4_input.dev_attr.attr,
1394 	&sensor_dev_attr_in4_min.dev_attr.attr,
1395 	&sensor_dev_attr_in4_max.dev_attr.attr,
1396 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1397 	NULL
1398 };
1399 
1400 static const struct attribute_group lm85_group_in4 = {
1401 	.attrs = lm85_attributes_in4,
1402 };
1403 
1404 static struct attribute *lm85_attributes_in567[] = {
1405 	&sensor_dev_attr_in5_input.dev_attr.attr,
1406 	&sensor_dev_attr_in6_input.dev_attr.attr,
1407 	&sensor_dev_attr_in7_input.dev_attr.attr,
1408 	&sensor_dev_attr_in5_min.dev_attr.attr,
1409 	&sensor_dev_attr_in6_min.dev_attr.attr,
1410 	&sensor_dev_attr_in7_min.dev_attr.attr,
1411 	&sensor_dev_attr_in5_max.dev_attr.attr,
1412 	&sensor_dev_attr_in6_max.dev_attr.attr,
1413 	&sensor_dev_attr_in7_max.dev_attr.attr,
1414 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
1415 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
1416 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
1417 	NULL
1418 };
1419 
1420 static const struct attribute_group lm85_group_in567 = {
1421 	.attrs = lm85_attributes_in567,
1422 };
1423 
lm85_init_client(struct i2c_client * client)1424 static void lm85_init_client(struct i2c_client *client)
1425 {
1426 	int value;
1427 
1428 	/* Start monitoring if needed */
1429 	value = lm85_read_value(client, LM85_REG_CONFIG);
1430 	if (!(value & 0x01)) {
1431 		dev_info(&client->dev, "Starting monitoring\n");
1432 		lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1433 	}
1434 
1435 	/* Warn about unusual configuration bits */
1436 	if (value & 0x02)
1437 		dev_warn(&client->dev, "Device configuration is locked\n");
1438 	if (!(value & 0x04))
1439 		dev_warn(&client->dev, "Device is not ready\n");
1440 }
1441 
lm85_is_fake(struct i2c_client * client)1442 static int lm85_is_fake(struct i2c_client *client)
1443 {
1444 	/*
1445 	 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1446 	 * emulate the former except that it has no hardware monitoring function
1447 	 * so the readings are always 0.
1448 	 */
1449 	int i;
1450 	u8 in_temp, fan;
1451 
1452 	for (i = 0; i < 8; i++) {
1453 		in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1454 		fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1455 		if (in_temp != 0x00 || fan != 0xff)
1456 			return 0;
1457 	}
1458 
1459 	return 1;
1460 }
1461 
1462 /* Return 0 if detection is successful, -ENODEV otherwise */
lm85_detect(struct i2c_client * client,struct i2c_board_info * info)1463 static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1464 {
1465 	struct i2c_adapter *adapter = client->adapter;
1466 	int address = client->addr;
1467 	const char *type_name = NULL;
1468 	int company, verstep;
1469 
1470 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1471 		/* We need to be able to do byte I/O */
1472 		return -ENODEV;
1473 	}
1474 
1475 	/* Determine the chip type */
1476 	company = lm85_read_value(client, LM85_REG_COMPANY);
1477 	verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1478 
1479 	dev_dbg(&adapter->dev,
1480 		"Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1481 		address, company, verstep);
1482 
1483 	if (company == LM85_COMPANY_NATIONAL) {
1484 		switch (verstep) {
1485 		case LM85_VERSTEP_LM85C:
1486 			type_name = "lm85c";
1487 			break;
1488 		case LM85_VERSTEP_LM85B:
1489 			type_name = "lm85b";
1490 			break;
1491 		case LM85_VERSTEP_LM96000_1:
1492 		case LM85_VERSTEP_LM96000_2:
1493 			/* Check for Winbond WPCD377I */
1494 			if (lm85_is_fake(client)) {
1495 				dev_dbg(&adapter->dev,
1496 					"Found Winbond WPCD377I, ignoring\n");
1497 				return -ENODEV;
1498 			}
1499 			type_name = "lm85";
1500 			break;
1501 		}
1502 	} else if (company == LM85_COMPANY_ANALOG_DEV) {
1503 		switch (verstep) {
1504 		case LM85_VERSTEP_ADM1027:
1505 			type_name = "adm1027";
1506 			break;
1507 		case LM85_VERSTEP_ADT7463:
1508 		case LM85_VERSTEP_ADT7463C:
1509 			type_name = "adt7463";
1510 			break;
1511 		case LM85_VERSTEP_ADT7468_1:
1512 		case LM85_VERSTEP_ADT7468_2:
1513 			type_name = "adt7468";
1514 			break;
1515 		}
1516 	} else if (company == LM85_COMPANY_SMSC) {
1517 		switch (verstep) {
1518 		case LM85_VERSTEP_EMC6D100_A0:
1519 		case LM85_VERSTEP_EMC6D100_A1:
1520 			/* Note: we can't tell a '100 from a '101 */
1521 			type_name = "emc6d100";
1522 			break;
1523 		case LM85_VERSTEP_EMC6D102:
1524 			type_name = "emc6d102";
1525 			break;
1526 		case LM85_VERSTEP_EMC6D103_A0:
1527 		case LM85_VERSTEP_EMC6D103_A1:
1528 			type_name = "emc6d103";
1529 			break;
1530 		case LM85_VERSTEP_EMC6D103S:
1531 			type_name = "emc6d103s";
1532 			break;
1533 		}
1534 	}
1535 
1536 	if (!type_name)
1537 		return -ENODEV;
1538 
1539 	strlcpy(info->type, type_name, I2C_NAME_SIZE);
1540 
1541 	return 0;
1542 }
1543 
lm85_probe(struct i2c_client * client,const struct i2c_device_id * id)1544 static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
1545 {
1546 	struct device *dev = &client->dev;
1547 	struct device *hwmon_dev;
1548 	struct lm85_data *data;
1549 	int idx = 0;
1550 
1551 	data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
1552 	if (!data)
1553 		return -ENOMEM;
1554 
1555 	data->client = client;
1556 	if (client->dev.of_node)
1557 		data->type = (enum chips)of_device_get_match_data(&client->dev);
1558 	else
1559 		data->type = id->driver_data;
1560 	mutex_init(&data->update_lock);
1561 
1562 	/* Fill in the chip specific driver values */
1563 	switch (data->type) {
1564 	case adm1027:
1565 	case adt7463:
1566 	case adt7468:
1567 	case emc6d100:
1568 	case emc6d102:
1569 	case emc6d103:
1570 	case emc6d103s:
1571 		data->freq_map = adm1027_freq_map;
1572 		break;
1573 	default:
1574 		data->freq_map = lm85_freq_map;
1575 	}
1576 
1577 	/* Set the VRM version */
1578 	data->vrm = vid_which_vrm();
1579 
1580 	/* Initialize the LM85 chip */
1581 	lm85_init_client(client);
1582 
1583 	/* sysfs hooks */
1584 	data->groups[idx++] = &lm85_group;
1585 
1586 	/* minctl and temp_off exist on all chips except emc6d103s */
1587 	if (data->type != emc6d103s) {
1588 		data->groups[idx++] = &lm85_group_minctl;
1589 		data->groups[idx++] = &lm85_group_temp_off;
1590 	}
1591 
1592 	/*
1593 	 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1594 	 * as a sixth digital VID input rather than an analog input.
1595 	 */
1596 	if (data->type == adt7463 || data->type == adt7468) {
1597 		u8 vid = lm85_read_value(client, LM85_REG_VID);
1598 		if (vid & 0x80)
1599 			data->has_vid5 = true;
1600 	}
1601 
1602 	if (!data->has_vid5)
1603 		data->groups[idx++] = &lm85_group_in4;
1604 
1605 	/* The EMC6D100 has 3 additional voltage inputs */
1606 	if (data->type == emc6d100)
1607 		data->groups[idx++] = &lm85_group_in567;
1608 
1609 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1610 							   data, data->groups);
1611 	return PTR_ERR_OR_ZERO(hwmon_dev);
1612 }
1613 
1614 static const struct i2c_device_id lm85_id[] = {
1615 	{ "adm1027", adm1027 },
1616 	{ "adt7463", adt7463 },
1617 	{ "adt7468", adt7468 },
1618 	{ "lm85", lm85 },
1619 	{ "lm85b", lm85 },
1620 	{ "lm85c", lm85 },
1621 	{ "emc6d100", emc6d100 },
1622 	{ "emc6d101", emc6d100 },
1623 	{ "emc6d102", emc6d102 },
1624 	{ "emc6d103", emc6d103 },
1625 	{ "emc6d103s", emc6d103s },
1626 	{ }
1627 };
1628 MODULE_DEVICE_TABLE(i2c, lm85_id);
1629 
1630 static const struct of_device_id lm85_of_match[] = {
1631 	{
1632 		.compatible = "adi,adm1027",
1633 		.data = (void *)adm1027
1634 	},
1635 	{
1636 		.compatible = "adi,adt7463",
1637 		.data = (void *)adt7463
1638 	},
1639 	{
1640 		.compatible = "adi,adt7468",
1641 		.data = (void *)adt7468
1642 	},
1643 	{
1644 		.compatible = "national,lm85",
1645 		.data = (void *)lm85
1646 	},
1647 	{
1648 		.compatible = "national,lm85b",
1649 		.data = (void *)lm85
1650 	},
1651 	{
1652 		.compatible = "national,lm85c",
1653 		.data = (void *)lm85
1654 	},
1655 	{
1656 		.compatible = "smsc,emc6d100",
1657 		.data = (void *)emc6d100
1658 	},
1659 	{
1660 		.compatible = "smsc,emc6d101",
1661 		.data = (void *)emc6d100
1662 	},
1663 	{
1664 		.compatible = "smsc,emc6d102",
1665 		.data = (void *)emc6d102
1666 	},
1667 	{
1668 		.compatible = "smsc,emc6d103",
1669 		.data = (void *)emc6d103
1670 	},
1671 	{
1672 		.compatible = "smsc,emc6d103s",
1673 		.data = (void *)emc6d103s
1674 	},
1675 	{ },
1676 };
1677 MODULE_DEVICE_TABLE(of, lm85_of_match);
1678 
1679 static struct i2c_driver lm85_driver = {
1680 	.class		= I2C_CLASS_HWMON,
1681 	.driver = {
1682 		.name   = "lm85",
1683 		.of_match_table = of_match_ptr(lm85_of_match),
1684 	},
1685 	.probe		= lm85_probe,
1686 	.id_table	= lm85_id,
1687 	.detect		= lm85_detect,
1688 	.address_list	= normal_i2c,
1689 };
1690 
1691 module_i2c_driver(lm85_driver);
1692 
1693 MODULE_LICENSE("GPL");
1694 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1695 	"Margit Schubert-While <margitsw@t-online.de>, "
1696 	"Justin Thiessen <jthiessen@penguincomputing.com>");
1697 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1698