xref: /linux/drivers/hwmon/lm95234.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
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 = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127);
305 
306 	mutex_lock(&data->update_lock);
307 	data->tcrit2[index] = val;
308 	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val);
309 	mutex_unlock(&data->update_lock);
310 
311 	return count;
312 }
313 
314 static ssize_t tcrit2_hyst_show(struct device *dev,
315 				struct device_attribute *attr, char *buf)
316 {
317 	struct lm95234_data *data = dev_get_drvdata(dev);
318 	int index = to_sensor_dev_attr(attr)->index;
319 	int ret = lm95234_update_device(data);
320 
321 	if (ret)
322 		return ret;
323 
324 	/* Result can be negative, so be careful with unsigned operands */
325 	return sprintf(buf, "%d",
326 		       ((int)data->tcrit2[index] - (int)data->thyst) * 1000);
327 }
328 
329 static ssize_t tcrit1_show(struct device *dev, struct device_attribute *attr,
330 			   char *buf)
331 {
332 	struct lm95234_data *data = dev_get_drvdata(dev);
333 	int index = to_sensor_dev_attr(attr)->index;
334 
335 	return sprintf(buf, "%u", data->tcrit1[index] * 1000);
336 }
337 
338 static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr,
339 			    const char *buf, size_t count)
340 {
341 	struct lm95234_data *data = dev_get_drvdata(dev);
342 	int index = to_sensor_dev_attr(attr)->index;
343 	int ret = lm95234_update_device(data);
344 	long val;
345 
346 	if (ret)
347 		return ret;
348 
349 	ret = kstrtol(buf, 10, &val);
350 	if (ret < 0)
351 		return ret;
352 
353 	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
354 
355 	mutex_lock(&data->update_lock);
356 	data->tcrit1[index] = val;
357 	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val);
358 	mutex_unlock(&data->update_lock);
359 
360 	return count;
361 }
362 
363 static ssize_t tcrit1_hyst_show(struct device *dev,
364 				struct device_attribute *attr, char *buf)
365 {
366 	struct lm95234_data *data = dev_get_drvdata(dev);
367 	int index = to_sensor_dev_attr(attr)->index;
368 	int ret = lm95234_update_device(data);
369 
370 	if (ret)
371 		return ret;
372 
373 	/* Result can be negative, so be careful with unsigned operands */
374 	return sprintf(buf, "%d",
375 		       ((int)data->tcrit1[index] - (int)data->thyst) * 1000);
376 }
377 
378 static ssize_t tcrit1_hyst_store(struct device *dev,
379 				 struct device_attribute *attr,
380 				 const char *buf, size_t count)
381 {
382 	struct lm95234_data *data = dev_get_drvdata(dev);
383 	int index = to_sensor_dev_attr(attr)->index;
384 	int ret = lm95234_update_device(data);
385 	long val;
386 
387 	if (ret)
388 		return ret;
389 
390 	ret = kstrtol(buf, 10, &val);
391 	if (ret < 0)
392 		return ret;
393 
394 	val = DIV_ROUND_CLOSEST(val, 1000);
395 	val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
396 
397 	mutex_lock(&data->update_lock);
398 	data->thyst = val;
399 	i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
400 	mutex_unlock(&data->update_lock);
401 
402 	return count;
403 }
404 
405 static ssize_t offset_show(struct device *dev, struct device_attribute *attr,
406 			   char *buf)
407 {
408 	struct lm95234_data *data = dev_get_drvdata(dev);
409 	int index = to_sensor_dev_attr(attr)->index;
410 	int ret = lm95234_update_device(data);
411 
412 	if (ret)
413 		return ret;
414 
415 	return sprintf(buf, "%d", data->toffset[index] * 500);
416 }
417 
418 static ssize_t offset_store(struct device *dev, struct device_attribute *attr,
419 			    const char *buf, size_t count)
420 {
421 	struct lm95234_data *data = dev_get_drvdata(dev);
422 	int index = to_sensor_dev_attr(attr)->index;
423 	int ret = lm95234_update_device(data);
424 	long val;
425 
426 	if (ret)
427 		return ret;
428 
429 	ret = kstrtol(buf, 10, &val);
430 	if (ret < 0)
431 		return ret;
432 
433 	/* Accuracy is 1/2 degrees C */
434 	val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127);
435 
436 	mutex_lock(&data->update_lock);
437 	data->toffset[index] = val;
438 	i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val);
439 	mutex_unlock(&data->update_lock);
440 
441 	return count;
442 }
443 
444 static ssize_t update_interval_show(struct device *dev,
445 				    struct device_attribute *attr, char *buf)
446 {
447 	struct lm95234_data *data = dev_get_drvdata(dev);
448 	int ret = lm95234_update_device(data);
449 
450 	if (ret)
451 		return ret;
452 
453 	return sprintf(buf, "%lu\n",
454 		       DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
455 }
456 
457 static ssize_t update_interval_store(struct device *dev,
458 				     struct device_attribute *attr,
459 				     const char *buf, size_t count)
460 {
461 	struct lm95234_data *data = dev_get_drvdata(dev);
462 	int ret = lm95234_update_device(data);
463 	unsigned long val;
464 	u8 regval;
465 
466 	if (ret)
467 		return ret;
468 
469 	ret = kstrtoul(buf, 10, &val);
470 	if (ret < 0)
471 		return ret;
472 
473 	for (regval = 0; regval < 3; regval++) {
474 		if (val <= update_intervals[regval])
475 			break;
476 	}
477 
478 	mutex_lock(&data->update_lock);
479 	data->interval = msecs_to_jiffies(update_intervals[regval]);
480 	i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
481 	mutex_unlock(&data->update_lock);
482 
483 	return count;
484 }
485 
486 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
487 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
488 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
489 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
490 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
491 
492 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, BIT(0) | BIT(1));
493 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, BIT(2) | BIT(3));
494 static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, BIT(4) | BIT(5));
495 static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, BIT(6) | BIT(7));
496 
497 static SENSOR_DEVICE_ATTR_RW(temp2_type, type, BIT(1));
498 static SENSOR_DEVICE_ATTR_RW(temp3_type, type, BIT(2));
499 static SENSOR_DEVICE_ATTR_RW(temp4_type, type, BIT(3));
500 static SENSOR_DEVICE_ATTR_RW(temp5_type, type, BIT(4));
501 
502 static SENSOR_DEVICE_ATTR_RW(temp1_max, tcrit1, 0);
503 static SENSOR_DEVICE_ATTR_RW(temp2_max, tcrit2, 0);
504 static SENSOR_DEVICE_ATTR_RW(temp3_max, tcrit2, 1);
505 static SENSOR_DEVICE_ATTR_RW(temp4_max, tcrit1, 3);
506 static SENSOR_DEVICE_ATTR_RW(temp5_max, tcrit1, 4);
507 
508 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, tcrit1_hyst, 0);
509 static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, tcrit2_hyst, 0);
510 static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, tcrit2_hyst, 1);
511 static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, tcrit1_hyst, 3);
512 static SENSOR_DEVICE_ATTR_RO(temp5_max_hyst, tcrit1_hyst, 4);
513 
514 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, BIT(0 + 8));
515 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, BIT(1 + 16));
516 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, BIT(2 + 16));
517 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, BIT(3 + 8));
518 static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, BIT(4 + 8));
519 
520 static SENSOR_DEVICE_ATTR_RW(temp2_crit, tcrit1, 1);
521 static SENSOR_DEVICE_ATTR_RW(temp3_crit, tcrit1, 2);
522 
523 static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, tcrit1_hyst, 1);
524 static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, tcrit1_hyst, 2);
525 
526 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, BIT(1 + 8));
527 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, BIT(2 + 8));
528 
529 static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 0);
530 static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 1);
531 static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 2);
532 static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 3);
533 
534 static DEVICE_ATTR_RW(update_interval);
535 
536 static struct attribute *lm95234_common_attrs[] = {
537 	&sensor_dev_attr_temp1_input.dev_attr.attr,
538 	&sensor_dev_attr_temp2_input.dev_attr.attr,
539 	&sensor_dev_attr_temp3_input.dev_attr.attr,
540 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
541 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
542 	&sensor_dev_attr_temp2_type.dev_attr.attr,
543 	&sensor_dev_attr_temp3_type.dev_attr.attr,
544 	&sensor_dev_attr_temp1_max.dev_attr.attr,
545 	&sensor_dev_attr_temp2_max.dev_attr.attr,
546 	&sensor_dev_attr_temp3_max.dev_attr.attr,
547 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
548 	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
549 	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
550 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
551 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
552 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
553 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
554 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
555 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
556 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
557 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
558 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
559 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
560 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
561 	&dev_attr_update_interval.attr,
562 	NULL
563 };
564 
565 static const struct attribute_group lm95234_common_group = {
566 	.attrs = lm95234_common_attrs,
567 };
568 
569 static struct attribute *lm95234_attrs[] = {
570 	&sensor_dev_attr_temp4_input.dev_attr.attr,
571 	&sensor_dev_attr_temp5_input.dev_attr.attr,
572 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
573 	&sensor_dev_attr_temp5_fault.dev_attr.attr,
574 	&sensor_dev_attr_temp4_type.dev_attr.attr,
575 	&sensor_dev_attr_temp5_type.dev_attr.attr,
576 	&sensor_dev_attr_temp4_max.dev_attr.attr,
577 	&sensor_dev_attr_temp5_max.dev_attr.attr,
578 	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
579 	&sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
580 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
581 	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
582 	&sensor_dev_attr_temp4_offset.dev_attr.attr,
583 	&sensor_dev_attr_temp5_offset.dev_attr.attr,
584 	NULL
585 };
586 
587 static const struct attribute_group lm95234_group = {
588 	.attrs = lm95234_attrs,
589 };
590 
591 static int lm95234_detect(struct i2c_client *client,
592 			  struct i2c_board_info *info)
593 {
594 	struct i2c_adapter *adapter = client->adapter;
595 	int address = client->addr;
596 	u8 config_mask, model_mask;
597 	int mfg_id, chip_id, val;
598 	const char *name;
599 
600 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
601 		return -ENODEV;
602 
603 	mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
604 	if (mfg_id != NATSEMI_MAN_ID)
605 		return -ENODEV;
606 
607 	chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
608 	switch (chip_id) {
609 	case LM95233_CHIP_ID:
610 		if (address != 0x18 && address != 0x2a && address != 0x2b)
611 			return -ENODEV;
612 		config_mask = 0xbf;
613 		model_mask = 0xf9;
614 		name = "lm95233";
615 		break;
616 	case LM95234_CHIP_ID:
617 		if (address != 0x18 && address != 0x4d && address != 0x4e)
618 			return -ENODEV;
619 		config_mask = 0xbc;
620 		model_mask = 0xe1;
621 		name = "lm95234";
622 		break;
623 	default:
624 		return -ENODEV;
625 	}
626 
627 	val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
628 	if (val & 0x30)
629 		return -ENODEV;
630 
631 	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
632 	if (val & config_mask)
633 		return -ENODEV;
634 
635 	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
636 	if (val & 0xfc)
637 		return -ENODEV;
638 
639 	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
640 	if (val & model_mask)
641 		return -ENODEV;
642 
643 	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
644 	if (val & model_mask)
645 		return -ENODEV;
646 
647 	strscpy(info->type, name, I2C_NAME_SIZE);
648 	return 0;
649 }
650 
651 static int lm95234_init_client(struct i2c_client *client)
652 {
653 	int val, model;
654 
655 	/* start conversion if necessary */
656 	val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
657 	if (val < 0)
658 		return val;
659 	if (val & 0x40)
660 		i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
661 					  val & ~0x40);
662 
663 	/* If diode type status reports an error, try to fix it */
664 	val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
665 	if (val < 0)
666 		return val;
667 	model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
668 	if (model < 0)
669 		return model;
670 	if (model & val) {
671 		dev_notice(&client->dev,
672 			   "Fixing remote diode type misconfiguration (0x%x)\n",
673 			   val);
674 		i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
675 					  model & ~val);
676 	}
677 	return 0;
678 }
679 
680 static const struct i2c_device_id lm95234_id[];
681 
682 static int lm95234_probe(struct i2c_client *client)
683 {
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 (i2c_match_id(lm95234_id, client)->driver_data == 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