xref: /openbmc/linux/drivers/hwmon/tmp401.c (revision d9fd5a71)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* tmp401.c
3  *
4  * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
5  * Preliminary tmp411 support by:
6  * Gabriel Konat, Sander Leget, Wouter Willems
7  * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
8  *
9  * Cleanup and support for TMP431 and TMP432 by Guenter Roeck
10  * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net>
11  */
12 
13 /*
14  * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
15  *
16  * Note this IC is in some aspect similar to the LM90, but it has quite a
17  * few differences too, for example the local temp has a higher resolution
18  * and thus has 16 bits registers for its value and limit instead of 8 bits.
19  */
20 
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/bitops.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.h>
26 #include <linux/i2c.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/err.h>
30 #include <linux/mutex.h>
31 #include <linux/sysfs.h>
32 
33 /* Addresses to scan */
34 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d,
35 	0x4e, 0x4f, I2C_CLIENT_END };
36 
37 enum chips { tmp401, tmp411, tmp431, tmp432, tmp435, tmp461 };
38 
39 /*
40  * The TMP401 registers, note some registers have different addresses for
41  * reading and writing
42  */
43 #define TMP401_STATUS				0x02
44 #define TMP401_CONFIG_READ			0x03
45 #define TMP401_CONFIG_WRITE			0x09
46 #define TMP401_CONVERSION_RATE_READ		0x04
47 #define TMP401_CONVERSION_RATE_WRITE		0x0A
48 #define TMP401_TEMP_CRIT_HYST			0x21
49 #define TMP401_MANUFACTURER_ID_REG		0xFE
50 #define TMP401_DEVICE_ID_REG			0xFF
51 
52 static const u8 TMP401_TEMP_MSB_READ[7][2] = {
53 	{ 0x00, 0x01 },	/* temp */
54 	{ 0x06, 0x08 },	/* low limit */
55 	{ 0x05, 0x07 },	/* high limit */
56 	{ 0x20, 0x19 },	/* therm (crit) limit */
57 	{ 0x30, 0x34 },	/* lowest */
58 	{ 0x32, 0x36 },	/* highest */
59 	{ 0, 0x11 },	/* offset */
60 };
61 
62 static const u8 TMP401_TEMP_MSB_WRITE[7][2] = {
63 	{ 0, 0 },	/* temp (unused) */
64 	{ 0x0C, 0x0E },	/* low limit */
65 	{ 0x0B, 0x0D },	/* high limit */
66 	{ 0x20, 0x19 },	/* therm (crit) limit */
67 	{ 0x30, 0x34 },	/* lowest */
68 	{ 0x32, 0x36 },	/* highest */
69 	{ 0, 0x11 },	/* offset */
70 };
71 
72 static const u8 TMP432_TEMP_MSB_READ[4][3] = {
73 	{ 0x00, 0x01, 0x23 },	/* temp */
74 	{ 0x06, 0x08, 0x16 },	/* low limit */
75 	{ 0x05, 0x07, 0x15 },	/* high limit */
76 	{ 0x20, 0x19, 0x1A },	/* therm (crit) limit */
77 };
78 
79 static const u8 TMP432_TEMP_MSB_WRITE[4][3] = {
80 	{ 0, 0, 0 },		/* temp  - unused */
81 	{ 0x0C, 0x0E, 0x16 },	/* low limit */
82 	{ 0x0B, 0x0D, 0x15 },	/* high limit */
83 	{ 0x20, 0x19, 0x1A },	/* therm (crit) limit */
84 };
85 
86 /* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */
87 static const u8 TMP432_STATUS_REG[] = {
88 	0x1b, 0x36, 0x35, 0x37 };
89 
90 /* Flags */
91 #define TMP401_CONFIG_RANGE			BIT(2)
92 #define TMP401_CONFIG_SHUTDOWN			BIT(6)
93 #define TMP401_STATUS_LOCAL_CRIT		BIT(0)
94 #define TMP401_STATUS_REMOTE_CRIT		BIT(1)
95 #define TMP401_STATUS_REMOTE_OPEN		BIT(2)
96 #define TMP401_STATUS_REMOTE_LOW		BIT(3)
97 #define TMP401_STATUS_REMOTE_HIGH		BIT(4)
98 #define TMP401_STATUS_LOCAL_LOW			BIT(5)
99 #define TMP401_STATUS_LOCAL_HIGH		BIT(6)
100 
101 /* On TMP432, each status has its own register */
102 #define TMP432_STATUS_LOCAL			BIT(0)
103 #define TMP432_STATUS_REMOTE1			BIT(1)
104 #define TMP432_STATUS_REMOTE2			BIT(2)
105 
106 /* Manufacturer / Device ID's */
107 #define TMP401_MANUFACTURER_ID			0x55
108 #define TMP401_DEVICE_ID			0x11
109 #define TMP411A_DEVICE_ID			0x12
110 #define TMP411B_DEVICE_ID			0x13
111 #define TMP411C_DEVICE_ID			0x10
112 #define TMP431_DEVICE_ID			0x31
113 #define TMP432_DEVICE_ID			0x32
114 #define TMP435_DEVICE_ID			0x35
115 
116 /*
117  * Driver data (common to all clients)
118  */
119 
120 static const struct i2c_device_id tmp401_id[] = {
121 	{ "tmp401", tmp401 },
122 	{ "tmp411", tmp411 },
123 	{ "tmp431", tmp431 },
124 	{ "tmp432", tmp432 },
125 	{ "tmp435", tmp435 },
126 	{ "tmp461", tmp461 },
127 	{ }
128 };
129 MODULE_DEVICE_TABLE(i2c, tmp401_id);
130 
131 /*
132  * Client data (each client gets its own)
133  */
134 
135 struct tmp401_data {
136 	struct i2c_client *client;
137 	const struct attribute_group *groups[3];
138 	struct mutex update_lock;
139 	char valid; /* zero until following fields are valid */
140 	unsigned long last_updated; /* in jiffies */
141 	enum chips kind;
142 
143 	unsigned int update_interval;	/* in milliseconds */
144 
145 	/* register values */
146 	u8 status[4];
147 	u8 config;
148 	u16 temp[7][3];
149 	u8 temp_crit_hyst;
150 };
151 
152 /*
153  * Sysfs attr show / store functions
154  */
155 
156 static int tmp401_register_to_temp(u16 reg, u8 config)
157 {
158 	int temp = reg;
159 
160 	if (config & TMP401_CONFIG_RANGE)
161 		temp -= 64 * 256;
162 
163 	return DIV_ROUND_CLOSEST(temp * 125, 32);
164 }
165 
166 static u16 tmp401_temp_to_register(long temp, u8 config, int zbits)
167 {
168 	if (config & TMP401_CONFIG_RANGE) {
169 		temp = clamp_val(temp, -64000, 191000);
170 		temp += 64000;
171 	} else
172 		temp = clamp_val(temp, 0, 127000);
173 
174 	return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
175 }
176 
177 static int tmp401_update_device_reg16(struct i2c_client *client,
178 				      struct tmp401_data *data)
179 {
180 	int i, j, val;
181 	int num_regs = data->kind == tmp411 ? 6 : 4;
182 	int num_sensors = data->kind == tmp432 ? 3 : 2;
183 
184 	for (i = 0; i < num_sensors; i++) {		/* local / r1 / r2 */
185 		for (j = 0; j < num_regs; j++) {	/* temp / low / ... */
186 			u8 regaddr;
187 
188 			regaddr = data->kind == tmp432 ?
189 						TMP432_TEMP_MSB_READ[j][i] :
190 						TMP401_TEMP_MSB_READ[j][i];
191 			if (j == 3) { /* crit is msb only */
192 				val = i2c_smbus_read_byte_data(client, regaddr);
193 			} else {
194 				val = i2c_smbus_read_word_swapped(client,
195 								  regaddr);
196 			}
197 			if (val < 0)
198 				return val;
199 
200 			data->temp[j][i] = j == 3 ? val << 8 : val;
201 		}
202 	}
203 	return 0;
204 }
205 
206 static struct tmp401_data *tmp401_update_device(struct device *dev)
207 {
208 	struct tmp401_data *data = dev_get_drvdata(dev);
209 	struct i2c_client *client = data->client;
210 	struct tmp401_data *ret = data;
211 	int i, val;
212 	unsigned long next_update;
213 
214 	mutex_lock(&data->update_lock);
215 
216 	next_update = data->last_updated +
217 		      msecs_to_jiffies(data->update_interval);
218 	if (time_after(jiffies, next_update) || !data->valid) {
219 		if (data->kind != tmp432) {
220 			/*
221 			 * The driver uses the TMP432 status format internally.
222 			 * Convert status to TMP432 format for other chips.
223 			 */
224 			val = i2c_smbus_read_byte_data(client, TMP401_STATUS);
225 			if (val < 0) {
226 				ret = ERR_PTR(val);
227 				goto abort;
228 			}
229 			data->status[0] =
230 			  (val & TMP401_STATUS_REMOTE_OPEN) >> 1;
231 			data->status[1] =
232 			  ((val & TMP401_STATUS_REMOTE_LOW) >> 2) |
233 			  ((val & TMP401_STATUS_LOCAL_LOW) >> 5);
234 			data->status[2] =
235 			  ((val & TMP401_STATUS_REMOTE_HIGH) >> 3) |
236 			  ((val & TMP401_STATUS_LOCAL_HIGH) >> 6);
237 			data->status[3] = val & (TMP401_STATUS_LOCAL_CRIT
238 						| TMP401_STATUS_REMOTE_CRIT);
239 		} else {
240 			for (i = 0; i < ARRAY_SIZE(data->status); i++) {
241 				val = i2c_smbus_read_byte_data(client,
242 							TMP432_STATUS_REG[i]);
243 				if (val < 0) {
244 					ret = ERR_PTR(val);
245 					goto abort;
246 				}
247 				data->status[i] = val;
248 			}
249 		}
250 
251 		val = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
252 		if (val < 0) {
253 			ret = ERR_PTR(val);
254 			goto abort;
255 		}
256 		data->config = val;
257 		val = tmp401_update_device_reg16(client, data);
258 		if (val < 0) {
259 			ret = ERR_PTR(val);
260 			goto abort;
261 		}
262 		val = i2c_smbus_read_byte_data(client, TMP401_TEMP_CRIT_HYST);
263 		if (val < 0) {
264 			ret = ERR_PTR(val);
265 			goto abort;
266 		}
267 		data->temp_crit_hyst = val;
268 
269 		data->last_updated = jiffies;
270 		data->valid = 1;
271 	}
272 
273 abort:
274 	mutex_unlock(&data->update_lock);
275 	return ret;
276 }
277 
278 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
279 			 char *buf)
280 {
281 	int nr = to_sensor_dev_attr_2(devattr)->nr;
282 	int index = to_sensor_dev_attr_2(devattr)->index;
283 	struct tmp401_data *data = tmp401_update_device(dev);
284 
285 	if (IS_ERR(data))
286 		return PTR_ERR(data);
287 
288 	return sprintf(buf, "%d\n",
289 		tmp401_register_to_temp(data->temp[nr][index], data->config));
290 }
291 
292 static ssize_t temp_crit_hyst_show(struct device *dev,
293 				   struct device_attribute *devattr,
294 				   char *buf)
295 {
296 	int temp, index = to_sensor_dev_attr(devattr)->index;
297 	struct tmp401_data *data = tmp401_update_device(dev);
298 
299 	if (IS_ERR(data))
300 		return PTR_ERR(data);
301 
302 	mutex_lock(&data->update_lock);
303 	temp = tmp401_register_to_temp(data->temp[3][index], data->config);
304 	temp -= data->temp_crit_hyst * 1000;
305 	mutex_unlock(&data->update_lock);
306 
307 	return sprintf(buf, "%d\n", temp);
308 }
309 
310 static ssize_t status_show(struct device *dev,
311 			   struct device_attribute *devattr, char *buf)
312 {
313 	int nr = to_sensor_dev_attr_2(devattr)->nr;
314 	int mask = to_sensor_dev_attr_2(devattr)->index;
315 	struct tmp401_data *data = tmp401_update_device(dev);
316 
317 	if (IS_ERR(data))
318 		return PTR_ERR(data);
319 
320 	return sprintf(buf, "%d\n", !!(data->status[nr] & mask));
321 }
322 
323 static ssize_t temp_store(struct device *dev,
324 			  struct device_attribute *devattr, const char *buf,
325 			  size_t count)
326 {
327 	int nr = to_sensor_dev_attr_2(devattr)->nr;
328 	int index = to_sensor_dev_attr_2(devattr)->index;
329 	struct tmp401_data *data = dev_get_drvdata(dev);
330 	struct i2c_client *client = data->client;
331 	long val;
332 	u16 reg;
333 	u8 regaddr;
334 
335 	if (kstrtol(buf, 10, &val))
336 		return -EINVAL;
337 
338 	reg = tmp401_temp_to_register(val, data->config, nr == 3 ? 8 : 4);
339 
340 	mutex_lock(&data->update_lock);
341 
342 	regaddr = data->kind == tmp432 ? TMP432_TEMP_MSB_WRITE[nr][index]
343 				       : TMP401_TEMP_MSB_WRITE[nr][index];
344 	if (nr == 3) { /* crit is msb only */
345 		i2c_smbus_write_byte_data(client, regaddr, reg >> 8);
346 	} else {
347 		/* Hardware expects big endian data --> use _swapped */
348 		i2c_smbus_write_word_swapped(client, regaddr, reg);
349 	}
350 	data->temp[nr][index] = reg;
351 
352 	mutex_unlock(&data->update_lock);
353 
354 	return count;
355 }
356 
357 static ssize_t temp_crit_hyst_store(struct device *dev,
358 				    struct device_attribute *devattr,
359 				    const char *buf, size_t count)
360 {
361 	int temp, index = to_sensor_dev_attr(devattr)->index;
362 	struct tmp401_data *data = tmp401_update_device(dev);
363 	long val;
364 	u8 reg;
365 
366 	if (IS_ERR(data))
367 		return PTR_ERR(data);
368 
369 	if (kstrtol(buf, 10, &val))
370 		return -EINVAL;
371 
372 	if (data->config & TMP401_CONFIG_RANGE)
373 		val = clamp_val(val, -64000, 191000);
374 	else
375 		val = clamp_val(val, 0, 127000);
376 
377 	mutex_lock(&data->update_lock);
378 	temp = tmp401_register_to_temp(data->temp[3][index], data->config);
379 	val = clamp_val(val, temp - 255000, temp);
380 	reg = ((temp - val) + 500) / 1000;
381 
382 	i2c_smbus_write_byte_data(data->client, TMP401_TEMP_CRIT_HYST,
383 				  reg);
384 
385 	data->temp_crit_hyst = reg;
386 
387 	mutex_unlock(&data->update_lock);
388 
389 	return count;
390 }
391 
392 /*
393  * Resets the historical measurements of minimum and maximum temperatures.
394  * This is done by writing any value to any of the minimum/maximum registers
395  * (0x30-0x37).
396  */
397 static ssize_t reset_temp_history_store(struct device *dev,
398 					struct device_attribute *devattr,
399 					const char *buf, size_t count)
400 {
401 	struct tmp401_data *data = dev_get_drvdata(dev);
402 	struct i2c_client *client = data->client;
403 	long val;
404 
405 	if (kstrtol(buf, 10, &val))
406 		return -EINVAL;
407 
408 	if (val != 1) {
409 		dev_err(dev,
410 			"temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
411 			val);
412 		return -EINVAL;
413 	}
414 	mutex_lock(&data->update_lock);
415 	i2c_smbus_write_byte_data(client, TMP401_TEMP_MSB_WRITE[5][0], val);
416 	data->valid = 0;
417 	mutex_unlock(&data->update_lock);
418 
419 	return count;
420 }
421 
422 static ssize_t update_interval_show(struct device *dev,
423 				    struct device_attribute *attr, char *buf)
424 {
425 	struct tmp401_data *data = dev_get_drvdata(dev);
426 
427 	return sprintf(buf, "%u\n", data->update_interval);
428 }
429 
430 static ssize_t update_interval_store(struct device *dev,
431 				     struct device_attribute *attr,
432 				     const char *buf, size_t count)
433 {
434 	struct tmp401_data *data = dev_get_drvdata(dev);
435 	struct i2c_client *client = data->client;
436 	unsigned long val;
437 	int err, rate;
438 
439 	err = kstrtoul(buf, 10, &val);
440 	if (err)
441 		return err;
442 
443 	/*
444 	 * For valid rates, interval can be calculated as
445 	 *	interval = (1 << (7 - rate)) * 125;
446 	 * Rounded rate is therefore
447 	 *	rate = 7 - __fls(interval * 4 / (125 * 3));
448 	 * Use clamp_val() to avoid overflows, and to ensure valid input
449 	 * for __fls.
450 	 */
451 	val = clamp_val(val, 125, 16000);
452 	rate = 7 - __fls(val * 4 / (125 * 3));
453 	mutex_lock(&data->update_lock);
454 	i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate);
455 	data->update_interval = (1 << (7 - rate)) * 125;
456 	mutex_unlock(&data->update_lock);
457 
458 	return count;
459 }
460 
461 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
462 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 1, 0);
463 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 2, 0);
464 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 3, 0);
465 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_crit_hyst, 0);
466 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, status, 1,
467 			       TMP432_STATUS_LOCAL);
468 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, status, 2,
469 			       TMP432_STATUS_LOCAL);
470 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, status, 3,
471 			       TMP432_STATUS_LOCAL);
472 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
473 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, 1);
474 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 2, 1);
475 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 3, 1);
476 static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, temp_crit_hyst, 1);
477 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, status, 0, TMP432_STATUS_REMOTE1);
478 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, status, 1,
479 			       TMP432_STATUS_REMOTE1);
480 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, status, 2,
481 			       TMP432_STATUS_REMOTE1);
482 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, status, 3,
483 			       TMP432_STATUS_REMOTE1);
484 
485 static DEVICE_ATTR_RW(update_interval);
486 
487 static struct attribute *tmp401_attributes[] = {
488 	&sensor_dev_attr_temp1_input.dev_attr.attr,
489 	&sensor_dev_attr_temp1_min.dev_attr.attr,
490 	&sensor_dev_attr_temp1_max.dev_attr.attr,
491 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
492 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
493 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
494 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
495 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
496 
497 	&sensor_dev_attr_temp2_input.dev_attr.attr,
498 	&sensor_dev_attr_temp2_min.dev_attr.attr,
499 	&sensor_dev_attr_temp2_max.dev_attr.attr,
500 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
501 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
502 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
503 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
504 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
505 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
506 
507 	&dev_attr_update_interval.attr,
508 
509 	NULL
510 };
511 
512 static const struct attribute_group tmp401_group = {
513 	.attrs = tmp401_attributes,
514 };
515 
516 /*
517  * Additional features of the TMP411 chip.
518  * The TMP411 stores the minimum and maximum
519  * temperature measured since power-on, chip-reset, or
520  * minimum and maximum register reset for both the local
521  * and remote channels.
522  */
523 static SENSOR_DEVICE_ATTR_2_RO(temp1_lowest, temp, 4, 0);
524 static SENSOR_DEVICE_ATTR_2_RO(temp1_highest, temp, 5, 0);
525 static SENSOR_DEVICE_ATTR_2_RO(temp2_lowest, temp, 4, 1);
526 static SENSOR_DEVICE_ATTR_2_RO(temp2_highest, temp, 5, 1);
527 static SENSOR_DEVICE_ATTR_WO(temp_reset_history, reset_temp_history, 0);
528 
529 static struct attribute *tmp411_attributes[] = {
530 	&sensor_dev_attr_temp1_highest.dev_attr.attr,
531 	&sensor_dev_attr_temp1_lowest.dev_attr.attr,
532 	&sensor_dev_attr_temp2_highest.dev_attr.attr,
533 	&sensor_dev_attr_temp2_lowest.dev_attr.attr,
534 	&sensor_dev_attr_temp_reset_history.dev_attr.attr,
535 	NULL
536 };
537 
538 static const struct attribute_group tmp411_group = {
539 	.attrs = tmp411_attributes,
540 };
541 
542 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0, 2);
543 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 1, 2);
544 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, 2);
545 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 3, 2);
546 static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, temp_crit_hyst, 2);
547 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, status, 0, TMP432_STATUS_REMOTE2);
548 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, status, 1,
549 			       TMP432_STATUS_REMOTE2);
550 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, status, 2,
551 			       TMP432_STATUS_REMOTE2);
552 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, status, 3,
553 			       TMP432_STATUS_REMOTE2);
554 
555 static struct attribute *tmp432_attributes[] = {
556 	&sensor_dev_attr_temp3_input.dev_attr.attr,
557 	&sensor_dev_attr_temp3_min.dev_attr.attr,
558 	&sensor_dev_attr_temp3_max.dev_attr.attr,
559 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
560 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
561 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
562 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
563 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
564 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
565 
566 	NULL
567 };
568 
569 static const struct attribute_group tmp432_group = {
570 	.attrs = tmp432_attributes,
571 };
572 
573 /*
574  * Additional features of the TMP461 chip.
575  * The TMP461 temperature offset for the remote channel.
576  */
577 static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, 6, 1);
578 
579 static struct attribute *tmp461_attributes[] = {
580 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
581 	NULL
582 };
583 
584 static const struct attribute_group tmp461_group = {
585 	.attrs = tmp461_attributes,
586 };
587 
588 /*
589  * Begin non sysfs callback code (aka Real code)
590  */
591 
592 static int tmp401_init_client(struct tmp401_data *data,
593 			      struct i2c_client *client)
594 {
595 	int config, config_orig, status = 0;
596 
597 	/* Set the conversion rate to 2 Hz */
598 	i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
599 	data->update_interval = 500;
600 
601 	/* Start conversions (disable shutdown if necessary) */
602 	config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
603 	if (config < 0)
604 		return config;
605 
606 	config_orig = config;
607 	config &= ~TMP401_CONFIG_SHUTDOWN;
608 
609 	if (config != config_orig)
610 		status = i2c_smbus_write_byte_data(client,
611 						   TMP401_CONFIG_WRITE,
612 						   config);
613 
614 	return status;
615 }
616 
617 static int tmp401_detect(struct i2c_client *client,
618 			 struct i2c_board_info *info)
619 {
620 	enum chips kind;
621 	struct i2c_adapter *adapter = client->adapter;
622 	u8 reg;
623 
624 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
625 		return -ENODEV;
626 
627 	/* Detect and identify the chip */
628 	reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
629 	if (reg != TMP401_MANUFACTURER_ID)
630 		return -ENODEV;
631 
632 	reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
633 
634 	switch (reg) {
635 	case TMP401_DEVICE_ID:
636 		if (client->addr != 0x4c)
637 			return -ENODEV;
638 		kind = tmp401;
639 		break;
640 	case TMP411A_DEVICE_ID:
641 		if (client->addr != 0x4c)
642 			return -ENODEV;
643 		kind = tmp411;
644 		break;
645 	case TMP411B_DEVICE_ID:
646 		if (client->addr != 0x4d)
647 			return -ENODEV;
648 		kind = tmp411;
649 		break;
650 	case TMP411C_DEVICE_ID:
651 		if (client->addr != 0x4e)
652 			return -ENODEV;
653 		kind = tmp411;
654 		break;
655 	case TMP431_DEVICE_ID:
656 		if (client->addr != 0x4c && client->addr != 0x4d)
657 			return -ENODEV;
658 		kind = tmp431;
659 		break;
660 	case TMP432_DEVICE_ID:
661 		if (client->addr != 0x4c && client->addr != 0x4d)
662 			return -ENODEV;
663 		kind = tmp432;
664 		break;
665 	case TMP435_DEVICE_ID:
666 		kind = tmp435;
667 		break;
668 	default:
669 		return -ENODEV;
670 	}
671 
672 	reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
673 	if (reg & 0x1b)
674 		return -ENODEV;
675 
676 	reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
677 	/* Datasheet says: 0x1-0x6 */
678 	if (reg > 15)
679 		return -ENODEV;
680 
681 	strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
682 
683 	return 0;
684 }
685 
686 static int tmp401_probe(struct i2c_client *client)
687 {
688 	static const char * const names[] = {
689 		"TMP401", "TMP411", "TMP431", "TMP432", "TMP435", "TMP461"
690 	};
691 	struct device *dev = &client->dev;
692 	struct device *hwmon_dev;
693 	struct tmp401_data *data;
694 	int groups = 0, status;
695 
696 	data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL);
697 	if (!data)
698 		return -ENOMEM;
699 
700 	data->client = client;
701 	mutex_init(&data->update_lock);
702 	data->kind = i2c_match_id(tmp401_id, client)->driver_data;
703 
704 	/* Initialize the TMP401 chip */
705 	status = tmp401_init_client(data, client);
706 	if (status < 0)
707 		return status;
708 
709 	/* Register sysfs hooks */
710 	data->groups[groups++] = &tmp401_group;
711 
712 	/* Register additional tmp411 sysfs hooks */
713 	if (data->kind == tmp411)
714 		data->groups[groups++] = &tmp411_group;
715 
716 	/* Register additional tmp432 sysfs hooks */
717 	if (data->kind == tmp432)
718 		data->groups[groups++] = &tmp432_group;
719 
720 	if (data->kind == tmp461)
721 		data->groups[groups++] = &tmp461_group;
722 
723 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
724 							   data, data->groups);
725 	if (IS_ERR(hwmon_dev))
726 		return PTR_ERR(hwmon_dev);
727 
728 	dev_info(dev, "Detected TI %s chip\n", names[data->kind]);
729 
730 	return 0;
731 }
732 
733 static struct i2c_driver tmp401_driver = {
734 	.class		= I2C_CLASS_HWMON,
735 	.driver = {
736 		.name	= "tmp401",
737 	},
738 	.probe_new	= tmp401_probe,
739 	.id_table	= tmp401_id,
740 	.detect		= tmp401_detect,
741 	.address_list	= normal_i2c,
742 };
743 
744 module_i2c_driver(tmp401_driver);
745 
746 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
747 MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
748 MODULE_LICENSE("GPL");
749