PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / hwmon / lm95234.c
blob411202bdaf6b6bf8c3eb0141ad7a62ecd7d4cfbe
1 /*
2 * Driver for Texas Instruments / National Semiconductor LM95234
4 * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net>
6 * Derived from lm95241.c
7 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
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.
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.
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>
31 #define DRVNAME "lm95234"
33 static const unsigned short normal_i2c[] = { 0x18, 0x4d, 0x4e, I2C_CLIENT_END };
35 /* LM95234 registers */
36 #define LM95234_REG_MAN_ID 0xFE
37 #define LM95234_REG_CHIP_ID 0xFF
38 #define LM95234_REG_STATUS 0x02
39 #define LM95234_REG_CONFIG 0x03
40 #define LM95234_REG_CONVRATE 0x04
41 #define LM95234_REG_STS_FAULT 0x07
42 #define LM95234_REG_STS_TCRIT1 0x08
43 #define LM95234_REG_STS_TCRIT2 0x09
44 #define LM95234_REG_TEMPH(x) ((x) + 0x10)
45 #define LM95234_REG_TEMPL(x) ((x) + 0x20)
46 #define LM95234_REG_UTEMPH(x) ((x) + 0x19) /* Remote only */
47 #define LM95234_REG_UTEMPL(x) ((x) + 0x29)
48 #define LM95234_REG_REM_MODEL 0x30
49 #define LM95234_REG_REM_MODEL_STS 0x38
50 #define LM95234_REG_OFFSET(x) ((x) + 0x31) /* Remote only */
51 #define LM95234_REG_TCRIT1(x) ((x) + 0x40)
52 #define LM95234_REG_TCRIT2(x) ((x) + 0x49) /* Remote channel 1,2 */
53 #define LM95234_REG_TCRIT_HYST 0x5a
55 #define NATSEMI_MAN_ID 0x01
56 #define LM95234_CHIP_ID 0x79
58 /* Client data (each client gets its own) */
59 struct lm95234_data {
60 struct i2c_client *client;
61 struct mutex update_lock;
62 unsigned long last_updated, interval; /* in jiffies */
63 bool valid; /* false until following fields are valid */
64 /* registers values */
65 int temp[5]; /* temperature (signed) */
66 u32 status; /* fault/alarm status */
67 u8 tcrit1[5]; /* critical temperature limit */
68 u8 tcrit2[2]; /* high temperature limit */
69 s8 toffset[4]; /* remote temperature offset */
70 u8 thyst; /* common hysteresis */
72 u8 sensor_type; /* temperature sensor type */
75 static int lm95234_read_temp(struct i2c_client *client, int index, int *t)
77 int val;
78 u16 temp = 0;
80 if (index) {
81 val = i2c_smbus_read_byte_data(client,
82 LM95234_REG_UTEMPH(index - 1));
83 if (val < 0)
84 return val;
85 temp = val << 8;
86 val = i2c_smbus_read_byte_data(client,
87 LM95234_REG_UTEMPL(index - 1));
88 if (val < 0)
89 return val;
90 temp |= val;
91 *t = temp;
94 * Read signed temperature if unsigned temperature is 0,
95 * or if this is the local sensor.
97 if (!temp) {
98 val = i2c_smbus_read_byte_data(client,
99 LM95234_REG_TEMPH(index));
100 if (val < 0)
101 return val;
102 temp = val << 8;
103 val = i2c_smbus_read_byte_data(client,
104 LM95234_REG_TEMPL(index));
105 if (val < 0)
106 return val;
107 temp |= val;
108 *t = (s16)temp;
110 return 0;
113 static u16 update_intervals[] = { 143, 364, 1000, 2500 };
115 /* Fill value cache. Must be called with update lock held. */
117 static int lm95234_fill_cache(struct lm95234_data *data,
118 struct i2c_client *client)
120 int i, ret;
122 ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
123 if (ret < 0)
124 return ret;
126 data->interval = msecs_to_jiffies(update_intervals[ret & 0x03]);
128 for (i = 0; i < ARRAY_SIZE(data->tcrit1); i++) {
129 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT1(i));
130 if (ret < 0)
131 return ret;
132 data->tcrit1[i] = ret;
134 for (i = 0; i < ARRAY_SIZE(data->tcrit2); i++) {
135 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT2(i));
136 if (ret < 0)
137 return ret;
138 data->tcrit2[i] = ret;
140 for (i = 0; i < ARRAY_SIZE(data->toffset); i++) {
141 ret = i2c_smbus_read_byte_data(client, LM95234_REG_OFFSET(i));
142 if (ret < 0)
143 return ret;
144 data->toffset[i] = ret;
147 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST);
148 if (ret < 0)
149 return ret;
150 data->thyst = ret;
152 ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
153 if (ret < 0)
154 return ret;
155 data->sensor_type = ret;
157 return 0;
160 static int lm95234_update_device(struct lm95234_data *data)
162 struct i2c_client *client = data->client;
163 int ret;
165 mutex_lock(&data->update_lock);
167 if (time_after(jiffies, data->last_updated + data->interval) ||
168 !data->valid) {
169 int i;
171 if (!data->valid) {
172 ret = lm95234_fill_cache(data, client);
173 if (ret < 0)
174 goto abort;
177 data->valid = false;
178 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
179 ret = lm95234_read_temp(client, i, &data->temp[i]);
180 if (ret < 0)
181 goto abort;
184 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT);
185 if (ret < 0)
186 goto abort;
187 data->status = ret;
189 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1);
190 if (ret < 0)
191 goto abort;
192 data->status |= ret << 8;
194 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2);
195 if (ret < 0)
196 goto abort;
197 data->status |= ret << 16;
199 data->last_updated = jiffies;
200 data->valid = true;
202 ret = 0;
203 abort:
204 mutex_unlock(&data->update_lock);
206 return ret;
209 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
210 char *buf)
212 struct lm95234_data *data = dev_get_drvdata(dev);
213 int index = to_sensor_dev_attr(attr)->index;
214 int ret = lm95234_update_device(data);
216 if (ret)
217 return ret;
219 return sprintf(buf, "%d\n",
220 DIV_ROUND_CLOSEST(data->temp[index] * 125, 32));
223 static ssize_t show_alarm(struct device *dev,
224 struct device_attribute *attr, char *buf)
226 struct lm95234_data *data = dev_get_drvdata(dev);
227 u32 mask = to_sensor_dev_attr(attr)->index;
228 int ret = lm95234_update_device(data);
230 if (ret)
231 return ret;
233 return sprintf(buf, "%u", !!(data->status & mask));
236 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
237 char *buf)
239 struct lm95234_data *data = dev_get_drvdata(dev);
240 u8 mask = to_sensor_dev_attr(attr)->index;
241 int ret = lm95234_update_device(data);
243 if (ret)
244 return ret;
246 return sprintf(buf, data->sensor_type & mask ? "1\n" : "2\n");
249 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t count)
252 struct lm95234_data *data = dev_get_drvdata(dev);
253 unsigned long val;
254 u8 mask = to_sensor_dev_attr(attr)->index;
255 int ret = lm95234_update_device(data);
257 if (ret)
258 return ret;
260 ret = kstrtoul(buf, 10, &val);
261 if (ret < 0)
262 return ret;
264 if (val != 1 && val != 2)
265 return -EINVAL;
267 mutex_lock(&data->update_lock);
268 if (val == 1)
269 data->sensor_type |= mask;
270 else
271 data->sensor_type &= ~mask;
272 data->valid = false;
273 i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL,
274 data->sensor_type);
275 mutex_unlock(&data->update_lock);
277 return count;
280 static ssize_t show_tcrit2(struct device *dev, struct device_attribute *attr,
281 char *buf)
283 struct lm95234_data *data = dev_get_drvdata(dev);
284 int index = to_sensor_dev_attr(attr)->index;
285 int ret = lm95234_update_device(data);
287 if (ret)
288 return ret;
290 return sprintf(buf, "%u", data->tcrit2[index] * 1000);
293 static ssize_t set_tcrit2(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
296 struct lm95234_data *data = dev_get_drvdata(dev);
297 int index = to_sensor_dev_attr(attr)->index;
298 long val;
299 int ret = lm95234_update_device(data);
301 if (ret)
302 return ret;
304 ret = kstrtol(buf, 10, &val);
305 if (ret < 0)
306 return ret;
308 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127);
310 mutex_lock(&data->update_lock);
311 data->tcrit2[index] = val;
312 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val);
313 mutex_unlock(&data->update_lock);
315 return count;
318 static ssize_t show_tcrit2_hyst(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 struct lm95234_data *data = dev_get_drvdata(dev);
322 int index = to_sensor_dev_attr(attr)->index;
323 int ret = lm95234_update_device(data);
325 if (ret)
326 return ret;
328 /* Result can be negative, so be careful with unsigned operands */
329 return sprintf(buf, "%d",
330 ((int)data->tcrit2[index] - (int)data->thyst) * 1000);
333 static ssize_t show_tcrit1(struct device *dev, struct device_attribute *attr,
334 char *buf)
336 struct lm95234_data *data = dev_get_drvdata(dev);
337 int index = to_sensor_dev_attr(attr)->index;
339 return sprintf(buf, "%u", data->tcrit1[index] * 1000);
342 static ssize_t set_tcrit1(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
345 struct lm95234_data *data = dev_get_drvdata(dev);
346 int index = to_sensor_dev_attr(attr)->index;
347 int ret = lm95234_update_device(data);
348 long val;
350 if (ret)
351 return ret;
353 ret = kstrtol(buf, 10, &val);
354 if (ret < 0)
355 return ret;
357 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
359 mutex_lock(&data->update_lock);
360 data->tcrit1[index] = val;
361 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val);
362 mutex_unlock(&data->update_lock);
364 return count;
367 static ssize_t show_tcrit1_hyst(struct device *dev,
368 struct device_attribute *attr, char *buf)
370 struct lm95234_data *data = dev_get_drvdata(dev);
371 int index = to_sensor_dev_attr(attr)->index;
372 int ret = lm95234_update_device(data);
374 if (ret)
375 return ret;
377 /* Result can be negative, so be careful with unsigned operands */
378 return sprintf(buf, "%d",
379 ((int)data->tcrit1[index] - (int)data->thyst) * 1000);
382 static ssize_t set_tcrit1_hyst(struct device *dev,
383 struct device_attribute *attr,
384 const char *buf, size_t count)
386 struct lm95234_data *data = dev_get_drvdata(dev);
387 int index = to_sensor_dev_attr(attr)->index;
388 int ret = lm95234_update_device(data);
389 long val;
391 if (ret)
392 return ret;
394 ret = kstrtol(buf, 10, &val);
395 if (ret < 0)
396 return ret;
398 val = DIV_ROUND_CLOSEST(val, 1000);
399 val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
401 mutex_lock(&data->update_lock);
402 data->thyst = val;
403 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
404 mutex_unlock(&data->update_lock);
406 return count;
409 static ssize_t show_offset(struct device *dev, struct device_attribute *attr,
410 char *buf)
412 struct lm95234_data *data = dev_get_drvdata(dev);
413 int index = to_sensor_dev_attr(attr)->index;
414 int ret = lm95234_update_device(data);
416 if (ret)
417 return ret;
419 return sprintf(buf, "%d", data->toffset[index] * 500);
422 static ssize_t set_offset(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
425 struct lm95234_data *data = dev_get_drvdata(dev);
426 int index = to_sensor_dev_attr(attr)->index;
427 int ret = lm95234_update_device(data);
428 long val;
430 if (ret)
431 return ret;
433 ret = kstrtol(buf, 10, &val);
434 if (ret < 0)
435 return ret;
437 /* Accuracy is 1/2 degrees C */
438 val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127);
440 mutex_lock(&data->update_lock);
441 data->toffset[index] = val;
442 i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val);
443 mutex_unlock(&data->update_lock);
445 return count;
448 static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
449 char *buf)
451 struct lm95234_data *data = dev_get_drvdata(dev);
452 int ret = lm95234_update_device(data);
454 if (ret)
455 return ret;
457 return sprintf(buf, "%lu\n",
458 DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
461 static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
462 const char *buf, size_t count)
464 struct lm95234_data *data = dev_get_drvdata(dev);
465 int ret = lm95234_update_device(data);
466 unsigned long val;
467 u8 regval;
469 if (ret)
470 return ret;
472 ret = kstrtoul(buf, 10, &val);
473 if (ret < 0)
474 return ret;
476 for (regval = 0; regval < 3; regval++) {
477 if (val <= update_intervals[regval])
478 break;
481 mutex_lock(&data->update_lock);
482 data->interval = msecs_to_jiffies(update_intervals[regval]);
483 i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
484 mutex_unlock(&data->update_lock);
486 return count;
489 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
490 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
491 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
492 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
493 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
495 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
496 BIT(0) | BIT(1));
497 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL,
498 BIT(2) | BIT(3));
499 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL,
500 BIT(4) | BIT(5));
501 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL,
502 BIT(6) | BIT(7));
504 static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
505 BIT(1));
506 static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
507 BIT(2));
508 static SENSOR_DEVICE_ATTR(temp4_type, S_IWUSR | S_IRUGO, show_type, set_type,
509 BIT(3));
510 static SENSOR_DEVICE_ATTR(temp5_type, S_IWUSR | S_IRUGO, show_type, set_type,
511 BIT(4));
513 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_tcrit1,
514 set_tcrit1, 0);
515 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_tcrit2,
516 set_tcrit2, 0);
517 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_tcrit2,
518 set_tcrit2, 1);
519 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_tcrit1,
520 set_tcrit1, 3);
521 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_tcrit1,
522 set_tcrit1, 4);
524 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_tcrit1_hyst,
525 set_tcrit1_hyst, 0);
526 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 0);
527 static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 1);
528 static SENSOR_DEVICE_ATTR(temp4_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 3);
529 static SENSOR_DEVICE_ATTR(temp5_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 4);
531 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
532 BIT(0 + 8));
533 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
534 BIT(1 + 16));
535 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
536 BIT(2 + 16));
537 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
538 BIT(3 + 8));
539 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
540 BIT(4 + 8));
542 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_tcrit1,
543 set_tcrit1, 1);
544 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_tcrit1,
545 set_tcrit1, 2);
547 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 1);
548 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 2);
550 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
551 BIT(1 + 8));
552 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
553 BIT(2 + 8));
555 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_offset,
556 set_offset, 0);
557 static SENSOR_DEVICE_ATTR(temp3_offset, S_IWUSR | S_IRUGO, show_offset,
558 set_offset, 1);
559 static SENSOR_DEVICE_ATTR(temp4_offset, S_IWUSR | S_IRUGO, show_offset,
560 set_offset, 2);
561 static SENSOR_DEVICE_ATTR(temp5_offset, S_IWUSR | S_IRUGO, show_offset,
562 set_offset, 3);
564 static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
565 set_interval);
567 static struct attribute *lm95234_attrs[] = {
568 &sensor_dev_attr_temp1_input.dev_attr.attr,
569 &sensor_dev_attr_temp2_input.dev_attr.attr,
570 &sensor_dev_attr_temp3_input.dev_attr.attr,
571 &sensor_dev_attr_temp4_input.dev_attr.attr,
572 &sensor_dev_attr_temp5_input.dev_attr.attr,
573 &sensor_dev_attr_temp2_fault.dev_attr.attr,
574 &sensor_dev_attr_temp3_fault.dev_attr.attr,
575 &sensor_dev_attr_temp4_fault.dev_attr.attr,
576 &sensor_dev_attr_temp5_fault.dev_attr.attr,
577 &sensor_dev_attr_temp2_type.dev_attr.attr,
578 &sensor_dev_attr_temp3_type.dev_attr.attr,
579 &sensor_dev_attr_temp4_type.dev_attr.attr,
580 &sensor_dev_attr_temp5_type.dev_attr.attr,
581 &sensor_dev_attr_temp1_max.dev_attr.attr,
582 &sensor_dev_attr_temp2_max.dev_attr.attr,
583 &sensor_dev_attr_temp3_max.dev_attr.attr,
584 &sensor_dev_attr_temp4_max.dev_attr.attr,
585 &sensor_dev_attr_temp5_max.dev_attr.attr,
586 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
587 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
588 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
589 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
590 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
591 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
592 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
593 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
594 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
595 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
596 &sensor_dev_attr_temp2_crit.dev_attr.attr,
597 &sensor_dev_attr_temp3_crit.dev_attr.attr,
598 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
599 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
600 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
601 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
602 &sensor_dev_attr_temp2_offset.dev_attr.attr,
603 &sensor_dev_attr_temp3_offset.dev_attr.attr,
604 &sensor_dev_attr_temp4_offset.dev_attr.attr,
605 &sensor_dev_attr_temp5_offset.dev_attr.attr,
606 &dev_attr_update_interval.attr,
607 NULL
609 ATTRIBUTE_GROUPS(lm95234);
611 static int lm95234_detect(struct i2c_client *client,
612 struct i2c_board_info *info)
614 struct i2c_adapter *adapter = client->adapter;
615 int mfg_id, chip_id, val;
617 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
618 return -ENODEV;
620 mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
621 if (mfg_id != NATSEMI_MAN_ID)
622 return -ENODEV;
624 chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
625 if (chip_id != LM95234_CHIP_ID)
626 return -ENODEV;
628 val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
629 if (val & 0x30)
630 return -ENODEV;
632 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
633 if (val & 0xbc)
634 return -ENODEV;
636 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
637 if (val & 0xfc)
638 return -ENODEV;
640 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
641 if (val & 0xe1)
642 return -ENODEV;
644 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
645 if (val & 0xe1)
646 return -ENODEV;
648 strlcpy(info->type, "lm95234", I2C_NAME_SIZE);
649 return 0;
652 static int lm95234_init_client(struct i2c_client *client)
654 int val, model;
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);
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);
678 return 0;
681 static int lm95234_probe(struct i2c_client *client,
682 const struct i2c_device_id *id)
684 struct device *dev = &client->dev;
685 struct lm95234_data *data;
686 struct device *hwmon_dev;
687 int err;
689 data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
690 if (!data)
691 return -ENOMEM;
693 data->client = client;
694 mutex_init(&data->update_lock);
696 /* Initialize the LM95234 chip */
697 err = lm95234_init_client(client);
698 if (err < 0)
699 return err;
701 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
702 data,
703 lm95234_groups);
704 return PTR_ERR_OR_ZERO(hwmon_dev);
707 /* Driver data (common to all clients) */
708 static const struct i2c_device_id lm95234_id[] = {
709 { "lm95234", 0 },
712 MODULE_DEVICE_TABLE(i2c, lm95234_id);
714 static struct i2c_driver lm95234_driver = {
715 .class = I2C_CLASS_HWMON,
716 .driver = {
717 .name = DRVNAME,
719 .probe = lm95234_probe,
720 .id_table = lm95234_id,
721 .detect = lm95234_detect,
722 .address_list = normal_i2c,
725 module_i2c_driver(lm95234_driver);
727 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
728 MODULE_DESCRIPTION("LM95234 sensor driver");
729 MODULE_LICENSE("GPL");