xref: /linux/drivers/hwmon/lm95234.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Texas Instruments / National Semiconductor LM95234
4  *
5  * Copyright (c) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
6  *
7  * Derived from lm95241.c
8  * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/i2c.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/sysfs.h>
21 
22 #define DRVNAME "lm95234"
23 
24 enum chips { lm95233, lm95234 };
25 
26 static const unsigned short normal_i2c[] = {
27 	0x18, 0x2a, 0x2b, 0x4d, 0x4e, I2C_CLIENT_END };
28 
29 /* LM95234 registers */
30 #define LM95234_REG_MAN_ID		0xFE
31 #define LM95234_REG_CHIP_ID		0xFF
32 #define LM95234_REG_STATUS		0x02
33 #define LM95234_REG_CONFIG		0x03
34 #define LM95234_REG_CONVRATE		0x04
35 #define LM95234_REG_STS_FAULT		0x07
36 #define LM95234_REG_STS_TCRIT1		0x08
37 #define LM95234_REG_STS_TCRIT2		0x09
38 #define LM95234_REG_TEMPH(x)		((x) + 0x10)
39 #define LM95234_REG_TEMPL(x)		((x) + 0x20)
40 #define LM95234_REG_UTEMPH(x)		((x) + 0x19)	/* Remote only */
41 #define LM95234_REG_UTEMPL(x)		((x) + 0x29)
42 #define LM95234_REG_REM_MODEL		0x30
43 #define LM95234_REG_REM_MODEL_STS	0x38
44 #define LM95234_REG_OFFSET(x)		((x) + 0x31)	/* Remote only */
45 #define LM95234_REG_TCRIT1(x)		((x) + 0x40)
46 #define LM95234_REG_TCRIT2(x)		((x) + 0x49)	/* Remote channel 1,2 */
47 #define LM95234_REG_TCRIT_HYST		0x5a
48 
49 #define NATSEMI_MAN_ID			0x01
50 #define LM95233_CHIP_ID			0x89
51 #define LM95234_CHIP_ID			0x79
52 
53 /* Client data (each client gets its own) */
54 struct lm95234_data {
55 	struct i2c_client *client;
56 	const struct attribute_group *groups[3];
57 	struct mutex update_lock;
58 	unsigned long last_updated, interval;	/* in jiffies */
59 	bool valid;		/* false until following fields are valid */
60 	/* registers values */
61 	int temp[5];		/* temperature (signed) */
62 	u32 status;		/* fault/alarm status */
63 	u8 tcrit1[5];		/* critical temperature limit */
64 	u8 tcrit2[2];		/* high temperature limit */
65 	s8 toffset[4];		/* remote temperature offset */
66 	u8 thyst;		/* common hysteresis */
67 
68 	u8 sensor_type;		/* temperature sensor type */
69 };
70 
71 static int lm95234_read_temp(struct i2c_client *client, int index, int *t)
72 {
73 	int val;
74 	u16 temp = 0;
75 
76 	if (index) {
77 		val = i2c_smbus_read_byte_data(client,
78 					       LM95234_REG_UTEMPH(index - 1));
79 		if (val < 0)
80 			return val;
81 		temp = val << 8;
82 		val = i2c_smbus_read_byte_data(client,
83 					       LM95234_REG_UTEMPL(index - 1));
84 		if (val < 0)
85 			return val;
86 		temp |= val;
87 		*t = temp;
88 	}
89 	/*
90 	 * Read signed temperature if unsigned temperature is 0,
91 	 * or if this is the local sensor.
92 	 */
93 	if (!temp) {
94 		val = i2c_smbus_read_byte_data(client,
95 					       LM95234_REG_TEMPH(index));
96 		if (val < 0)
97 			return val;
98 		temp = val << 8;
99 		val = i2c_smbus_read_byte_data(client,
100 					       LM95234_REG_TEMPL(index));
101 		if (val < 0)
102 			return val;
103 		temp |= val;
104 		*t = (s16)temp;
105 	}
106 	return 0;
107 }
108 
109 static u16 update_intervals[] = { 143, 364, 1000, 2500 };
110 
111 /* Fill value cache. Must be called with update lock held. */
112 
113 static int lm95234_fill_cache(struct lm95234_data *data,
114 			      struct i2c_client *client)
115 {
116 	int i, ret;
117 
118 	ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
119 	if (ret < 0)
120 		return ret;
121 
122 	data->interval = msecs_to_jiffies(update_intervals[ret & 0x03]);
123 
124 	for (i = 0; i < ARRAY_SIZE(data->tcrit1); i++) {
125 		ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT1(i));
126 		if (ret < 0)
127 			return ret;
128 		data->tcrit1[i] = ret;
129 	}
130 	for (i = 0; i < ARRAY_SIZE(data->tcrit2); i++) {
131 		ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT2(i));
132 		if (ret < 0)
133 			return ret;
134 		data->tcrit2[i] = ret;
135 	}
136 	for (i = 0; i < ARRAY_SIZE(data->toffset); i++) {
137 		ret = i2c_smbus_read_byte_data(client, LM95234_REG_OFFSET(i));
138 		if (ret < 0)
139 			return ret;
140 		data->toffset[i] = ret;
141 	}
142 
143 	ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST);
144 	if (ret < 0)
145 		return ret;
146 	data->thyst = ret;
147 
148 	ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
149 	if (ret < 0)
150 		return ret;
151 	data->sensor_type = ret;
152 
153 	return 0;
154 }
155 
156 static int lm95234_update_device(struct lm95234_data *data)
157 {
158 	struct i2c_client *client = data->client;
159 	int ret;
160 
161 	mutex_lock(&data->update_lock);
162 
163 	if (time_after(jiffies, data->last_updated + data->interval) ||
164 	    !data->valid) {
165 		int i;
166 
167 		if (!data->valid) {
168 			ret = lm95234_fill_cache(data, client);
169 			if (ret < 0)
170 				goto abort;
171 		}
172 
173 		data->valid = false;
174 		for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
175 			ret = lm95234_read_temp(client, i, &data->temp[i]);
176 			if (ret < 0)
177 				goto abort;
178 		}
179 
180 		ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT);
181 		if (ret < 0)
182 			goto abort;
183 		data->status = ret;
184 
185 		ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1);
186 		if (ret < 0)
187 			goto abort;
188 		data->status |= ret << 8;
189 
190 		ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2);
191 		if (ret < 0)
192 			goto abort;
193 		data->status |= ret << 16;
194 
195 		data->last_updated = jiffies;
196 		data->valid = true;
197 	}
198 	ret = 0;
199 abort:
200 	mutex_unlock(&data->update_lock);
201 
202 	return ret;
203 }
204 
205 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
206 			 char *buf)
207 {
208 	struct lm95234_data *data = dev_get_drvdata(dev);
209 	int index = to_sensor_dev_attr(attr)->index;
210 	int ret = lm95234_update_device(data);
211 
212 	if (ret)
213 		return ret;
214 
215 	return sprintf(buf, "%d\n",
216 		       DIV_ROUND_CLOSEST(data->temp[index] * 125, 32));
217 }
218 
219 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
220 			  char *buf)
221 {
222 	struct lm95234_data *data = dev_get_drvdata(dev);
223 	u32 mask = to_sensor_dev_attr(attr)->index;
224 	int ret = lm95234_update_device(data);
225 
226 	if (ret)
227 		return ret;
228 
229 	return sprintf(buf, "%u", !!(data->status & mask));
230 }
231 
232 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
233 			 char *buf)
234 {
235 	struct lm95234_data *data = dev_get_drvdata(dev);
236 	u8 mask = to_sensor_dev_attr(attr)->index;
237 	int ret = lm95234_update_device(data);
238 
239 	if (ret)
240 		return ret;
241 
242 	return sprintf(buf, data->sensor_type & mask ? "1\n" : "2\n");
243 }
244 
245 static ssize_t type_store(struct device *dev, struct device_attribute *attr,
246 			  const char *buf, size_t count)
247 {
248 	struct lm95234_data *data = dev_get_drvdata(dev);
249 	unsigned long val;
250 	u8 mask = to_sensor_dev_attr(attr)->index;
251 	int ret = lm95234_update_device(data);
252 
253 	if (ret)
254 		return ret;
255 
256 	ret = kstrtoul(buf, 10, &val);
257 	if (ret < 0)
258 		return ret;
259 
260 	if (val != 1 && val != 2)
261 		return -EINVAL;
262 
263 	mutex_lock(&data->update_lock);
264 	if (val == 1)
265 		data->sensor_type |= mask;
266 	else
267 		data->sensor_type &= ~mask;
268 	data->valid = false;
269 	i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL,
270 				  data->sensor_type);
271 	mutex_unlock(&data->update_lock);
272 
273 	return count;
274 }
275 
276 static ssize_t tcrit2_show(struct device *dev, struct device_attribute *attr,
277 			   char *buf)
278 {
279 	struct lm95234_data *data = dev_get_drvdata(dev);
280 	int index = to_sensor_dev_attr(attr)->index;
281 	int ret = lm95234_update_device(data);
282 
283 	if (ret)
284 		return ret;
285 
286 	return sprintf(buf, "%u", data->tcrit2[index] * 1000);
287 }
288 
289 static ssize_t tcrit2_store(struct device *dev, struct device_attribute *attr,
290 			    const char *buf, size_t count)
291 {
292 	struct lm95234_data *data = dev_get_drvdata(dev);
293 	int index = to_sensor_dev_attr(attr)->index;
294 	long val;
295 	int ret = lm95234_update_device(data);
296 
297 	if (ret)
298 		return ret;
299 
300 	ret = kstrtol(buf, 10, &val);
301 	if (ret < 0)
302 		return ret;
303 
304 	val = DIV_ROUND_CLOSEST(clamp_val(val, 0, (index ? 255 : 127) * 1000),
305 				1000);
306 
307 	mutex_lock(&data->update_lock);
308 	data->tcrit2[index] = val;
309 	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val);
310 	mutex_unlock(&data->update_lock);
311 
312 	return count;
313 }
314 
315 static ssize_t tcrit2_hyst_show(struct device *dev,
316 				struct device_attribute *attr, char *buf)
317 {
318 	struct lm95234_data *data = dev_get_drvdata(dev);
319 	int index = to_sensor_dev_attr(attr)->index;
320 	int ret = lm95234_update_device(data);
321 
322 	if (ret)
323 		return ret;
324 
325 	/* Result can be negative, so be careful with unsigned operands */
326 	return sprintf(buf, "%d",
327 		       ((int)data->tcrit2[index] - (int)data->thyst) * 1000);
328 }
329 
330 static ssize_t tcrit1_show(struct device *dev, struct device_attribute *attr,
331 			   char *buf)
332 {
333 	struct lm95234_data *data = dev_get_drvdata(dev);
334 	int index = to_sensor_dev_attr(attr)->index;
335 
336 	return sprintf(buf, "%u", data->tcrit1[index] * 1000);
337 }
338 
339 static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr,
340 			    const char *buf, size_t count)
341 {
342 	struct lm95234_data *data = dev_get_drvdata(dev);
343 	int index = to_sensor_dev_attr(attr)->index;
344 	int ret = lm95234_update_device(data);
345 	long val;
346 
347 	if (ret)
348 		return ret;
349 
350 	ret = kstrtol(buf, 10, &val);
351 	if (ret < 0)
352 		return ret;
353 
354 	val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 255000), 1000);
355 
356 	mutex_lock(&data->update_lock);
357 	data->tcrit1[index] = val;
358 	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val);
359 	mutex_unlock(&data->update_lock);
360 
361 	return count;
362 }
363 
364 static ssize_t tcrit1_hyst_show(struct device *dev,
365 				struct device_attribute *attr, char *buf)
366 {
367 	struct lm95234_data *data = dev_get_drvdata(dev);
368 	int index = to_sensor_dev_attr(attr)->index;
369 	int ret = lm95234_update_device(data);
370 
371 	if (ret)
372 		return ret;
373 
374 	/* Result can be negative, so be careful with unsigned operands */
375 	return sprintf(buf, "%d",
376 		       ((int)data->tcrit1[index] - (int)data->thyst) * 1000);
377 }
378 
379 static ssize_t tcrit1_hyst_store(struct device *dev,
380 				 struct device_attribute *attr,
381 				 const char *buf, size_t count)
382 {
383 	struct lm95234_data *data = dev_get_drvdata(dev);
384 	int index = to_sensor_dev_attr(attr)->index;
385 	int ret = lm95234_update_device(data);
386 	long val;
387 
388 	if (ret)
389 		return ret;
390 
391 	ret = kstrtol(buf, 10, &val);
392 	if (ret < 0)
393 		return ret;
394 
395 	val = DIV_ROUND_CLOSEST(clamp_val(val, -255000, 255000), 1000);
396 	val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
397 
398 	mutex_lock(&data->update_lock);
399 	data->thyst = val;
400 	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
401 	mutex_unlock(&data->update_lock);
402 
403 	return count;
404 }
405 
406 static ssize_t offset_show(struct device *dev, struct device_attribute *attr,
407 			   char *buf)
408 {
409 	struct lm95234_data *data = dev_get_drvdata(dev);
410 	int index = to_sensor_dev_attr(attr)->index;
411 	int ret = lm95234_update_device(data);
412 
413 	if (ret)
414 		return ret;
415 
416 	return sprintf(buf, "%d", data->toffset[index] * 500);
417 }
418 
419 static ssize_t offset_store(struct device *dev, struct device_attribute *attr,
420 			    const char *buf, size_t count)
421 {
422 	struct lm95234_data *data = dev_get_drvdata(dev);
423 	int index = to_sensor_dev_attr(attr)->index;
424 	int ret = lm95234_update_device(data);
425 	long val;
426 
427 	if (ret)
428 		return ret;
429 
430 	ret = kstrtol(buf, 10, &val);
431 	if (ret < 0)
432 		return ret;
433 
434 	/* Accuracy is 1/2 degrees C */
435 	val = DIV_ROUND_CLOSEST(clamp_val(val, -64000, 63500), 500);
436 
437 	mutex_lock(&data->update_lock);
438 	data->toffset[index] = val;
439 	i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val);
440 	mutex_unlock(&data->update_lock);
441 
442 	return count;
443 }
444 
445 static ssize_t update_interval_show(struct device *dev,
446 				    struct device_attribute *attr, char *buf)
447 {
448 	struct lm95234_data *data = dev_get_drvdata(dev);
449 	int ret = lm95234_update_device(data);
450 
451 	if (ret)
452 		return ret;
453 
454 	return sprintf(buf, "%lu\n",
455 		       DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
456 }
457 
458 static ssize_t update_interval_store(struct device *dev,
459 				     struct device_attribute *attr,
460 				     const char *buf, size_t count)
461 {
462 	struct lm95234_data *data = dev_get_drvdata(dev);
463 	int ret = lm95234_update_device(data);
464 	unsigned long val;
465 	u8 regval;
466 
467 	if (ret)
468 		return ret;
469 
470 	ret = kstrtoul(buf, 10, &val);
471 	if (ret < 0)
472 		return ret;
473 
474 	for (regval = 0; regval < 3; regval++) {
475 		if (val <= update_intervals[regval])
476 			break;
477 	}
478 
479 	mutex_lock(&data->update_lock);
480 	data->interval = msecs_to_jiffies(update_intervals[regval]);
481 	i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
482 	mutex_unlock(&data->update_lock);
483 
484 	return count;
485 }
486 
487 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
488 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
489 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
490 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
491 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
492 
493 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, BIT(0) | BIT(1));
494 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, BIT(2) | BIT(3));
495 static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, BIT(4) | BIT(5));
496 static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, BIT(6) | BIT(7));
497 
498 static SENSOR_DEVICE_ATTR_RW(temp2_type, type, BIT(1));
499 static SENSOR_DEVICE_ATTR_RW(temp3_type, type, BIT(2));
500 static SENSOR_DEVICE_ATTR_RW(temp4_type, type, BIT(3));
501 static SENSOR_DEVICE_ATTR_RW(temp5_type, type, BIT(4));
502 
503 static SENSOR_DEVICE_ATTR_RW(temp1_max, tcrit1, 0);
504 static SENSOR_DEVICE_ATTR_RW(temp2_max, tcrit2, 0);
505 static SENSOR_DEVICE_ATTR_RW(temp3_max, tcrit2, 1);
506 static SENSOR_DEVICE_ATTR_RW(temp4_max, tcrit1, 3);
507 static SENSOR_DEVICE_ATTR_RW(temp5_max, tcrit1, 4);
508 
509 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, tcrit1_hyst, 0);
510 static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, tcrit2_hyst, 0);
511 static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, tcrit2_hyst, 1);
512 static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, tcrit1_hyst, 3);
513 static SENSOR_DEVICE_ATTR_RO(temp5_max_hyst, tcrit1_hyst, 4);
514 
515 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, BIT(0 + 8));
516 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, BIT(1 + 16));
517 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, BIT(2 + 16));
518 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, BIT(3 + 8));
519 static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, BIT(4 + 8));
520 
521 static SENSOR_DEVICE_ATTR_RW(temp2_crit, tcrit1, 1);
522 static SENSOR_DEVICE_ATTR_RW(temp3_crit, tcrit1, 2);
523 
524 static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, tcrit1_hyst, 1);
525 static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, tcrit1_hyst, 2);
526 
527 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, BIT(1 + 8));
528 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, BIT(2 + 8));
529 
530 static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 0);
531 static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 1);
532 static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 2);
533 static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 3);
534 
535 static DEVICE_ATTR_RW(update_interval);
536 
537 static struct attribute *lm95234_common_attrs[] = {
538 	&sensor_dev_attr_temp1_input.dev_attr.attr,
539 	&sensor_dev_attr_temp2_input.dev_attr.attr,
540 	&sensor_dev_attr_temp3_input.dev_attr.attr,
541 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
542 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
543 	&sensor_dev_attr_temp2_type.dev_attr.attr,
544 	&sensor_dev_attr_temp3_type.dev_attr.attr,
545 	&sensor_dev_attr_temp1_max.dev_attr.attr,
546 	&sensor_dev_attr_temp2_max.dev_attr.attr,
547 	&sensor_dev_attr_temp3_max.dev_attr.attr,
548 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
549 	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
550 	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
551 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
552 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
553 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
554 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
555 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
556 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
557 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
558 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
559 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
560 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
561 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
562 	&dev_attr_update_interval.attr,
563 	NULL
564 };
565 
566 static const struct attribute_group lm95234_common_group = {
567 	.attrs = lm95234_common_attrs,
568 };
569 
570 static struct attribute *lm95234_attrs[] = {
571 	&sensor_dev_attr_temp4_input.dev_attr.attr,
572 	&sensor_dev_attr_temp5_input.dev_attr.attr,
573 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
574 	&sensor_dev_attr_temp5_fault.dev_attr.attr,
575 	&sensor_dev_attr_temp4_type.dev_attr.attr,
576 	&sensor_dev_attr_temp5_type.dev_attr.attr,
577 	&sensor_dev_attr_temp4_max.dev_attr.attr,
578 	&sensor_dev_attr_temp5_max.dev_attr.attr,
579 	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
580 	&sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
581 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
582 	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
583 	&sensor_dev_attr_temp4_offset.dev_attr.attr,
584 	&sensor_dev_attr_temp5_offset.dev_attr.attr,
585 	NULL
586 };
587 
588 static const struct attribute_group lm95234_group = {
589 	.attrs = lm95234_attrs,
590 };
591 
592 static int lm95234_detect(struct i2c_client *client,
593 			  struct i2c_board_info *info)
594 {
595 	struct i2c_adapter *adapter = client->adapter;
596 	int address = client->addr;
597 	u8 config_mask, model_mask;
598 	int mfg_id, chip_id, val;
599 	const char *name;
600 
601 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
602 		return -ENODEV;
603 
604 	mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
605 	if (mfg_id != NATSEMI_MAN_ID)
606 		return -ENODEV;
607 
608 	chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
609 	switch (chip_id) {
610 	case LM95233_CHIP_ID:
611 		if (address != 0x18 && address != 0x2a && address != 0x2b)
612 			return -ENODEV;
613 		config_mask = 0xbf;
614 		model_mask = 0xf9;
615 		name = "lm95233";
616 		break;
617 	case LM95234_CHIP_ID:
618 		if (address != 0x18 && address != 0x4d && address != 0x4e)
619 			return -ENODEV;
620 		config_mask = 0xbc;
621 		model_mask = 0xe1;
622 		name = "lm95234";
623 		break;
624 	default:
625 		return -ENODEV;
626 	}
627 
628 	val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
629 	if (val & 0x30)
630 		return -ENODEV;
631 
632 	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
633 	if (val & config_mask)
634 		return -ENODEV;
635 
636 	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
637 	if (val & 0xfc)
638 		return -ENODEV;
639 
640 	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
641 	if (val & model_mask)
642 		return -ENODEV;
643 
644 	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
645 	if (val & model_mask)
646 		return -ENODEV;
647 
648 	strscpy(info->type, name, I2C_NAME_SIZE);
649 	return 0;
650 }
651 
652 static int lm95234_init_client(struct i2c_client *client)
653 {
654 	int val, model;
655 
656 	/* start conversion if necessary */
657 	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
658 	if (val < 0)
659 		return val;
660 	if (val & 0x40)
661 		i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
662 					  val & ~0x40);
663 
664 	/* If diode type status reports an error, try to fix it */
665 	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
666 	if (val < 0)
667 		return val;
668 	model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
669 	if (model < 0)
670 		return model;
671 	if (model & val) {
672 		dev_notice(&client->dev,
673 			   "Fixing remote diode type misconfiguration (0x%x)\n",
674 			   val);
675 		i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
676 					  model & ~val);
677 	}
678 	return 0;
679 }
680 
681 static int lm95234_probe(struct i2c_client *client)
682 {
683 	enum chips type = (uintptr_t)i2c_get_match_data(client);
684 	struct device *dev = &client->dev;
685 	struct lm95234_data *data;
686 	struct device *hwmon_dev;
687 	int err;
688 
689 	data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
690 	if (!data)
691 		return -ENOMEM;
692 
693 	data->client = client;
694 	mutex_init(&data->update_lock);
695 
696 	/* Initialize the LM95234 chip */
697 	err = lm95234_init_client(client);
698 	if (err < 0)
699 		return err;
700 
701 	data->groups[0] = &lm95234_common_group;
702 	if (type == lm95234)
703 		data->groups[1] = &lm95234_group;
704 
705 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
706 							   data, data->groups);
707 	return PTR_ERR_OR_ZERO(hwmon_dev);
708 }
709 
710 /* Driver data (common to all clients) */
711 static const struct i2c_device_id lm95234_id[] = {
712 	{ "lm95233", lm95233 },
713 	{ "lm95234", lm95234 },
714 	{ }
715 };
716 MODULE_DEVICE_TABLE(i2c, lm95234_id);
717 
718 static struct i2c_driver lm95234_driver = {
719 	.class		= I2C_CLASS_HWMON,
720 	.driver = {
721 		.name	= DRVNAME,
722 	},
723 	.probe		= lm95234_probe,
724 	.id_table	= lm95234_id,
725 	.detect		= lm95234_detect,
726 	.address_list	= normal_i2c,
727 };
728 
729 module_i2c_driver(lm95234_driver);
730 
731 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
732 MODULE_DESCRIPTION("LM95233/LM95234 sensor driver");
733 MODULE_LICENSE("GPL");
734