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