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