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
lm95234_read_temp(struct i2c_client * client,int index,int * t)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
lm95234_fill_cache(struct lm95234_data * data,struct i2c_client * client)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
lm95234_update_device(struct lm95234_data * data)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
show_temp(struct device * dev,struct device_attribute * attr,char * buf)214 static ssize_t show_temp(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
show_alarm(struct device * dev,struct device_attribute * attr,char * buf)228 static ssize_t show_alarm(struct device *dev,
229 struct device_attribute *attr, 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
show_type(struct device * dev,struct device_attribute * attr,char * buf)241 static ssize_t show_type(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
set_type(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)254 static ssize_t set_type(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
show_tcrit2(struct device * dev,struct device_attribute * attr,char * buf)285 static ssize_t show_tcrit2(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
set_tcrit2(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)298 static ssize_t set_tcrit2(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
show_tcrit2_hyst(struct device * dev,struct device_attribute * attr,char * buf)323 static ssize_t show_tcrit2_hyst(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
show_tcrit1(struct device * dev,struct device_attribute * attr,char * buf)338 static ssize_t show_tcrit1(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
set_tcrit1(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)347 static ssize_t set_tcrit1(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
show_tcrit1_hyst(struct device * dev,struct device_attribute * attr,char * buf)372 static ssize_t show_tcrit1_hyst(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
set_tcrit1_hyst(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)387 static ssize_t set_tcrit1_hyst(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
show_offset(struct device * dev,struct device_attribute * attr,char * buf)414 static ssize_t show_offset(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
set_offset(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)427 static ssize_t set_offset(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
show_interval(struct device * dev,struct device_attribute * attr,char * buf)453 static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
454 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
set_interval(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)466 static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
467 const char *buf, size_t count)
468 {
469 struct lm95234_data *data = dev_get_drvdata(dev);
470 int ret = lm95234_update_device(data);
471 unsigned long val;
472 u8 regval;
473
474 if (ret)
475 return ret;
476
477 ret = kstrtoul(buf, 10, &val);
478 if (ret < 0)
479 return ret;
480
481 for (regval = 0; regval < 3; regval++) {
482 if (val <= update_intervals[regval])
483 break;
484 }
485
486 mutex_lock(&data->update_lock);
487 data->interval = msecs_to_jiffies(update_intervals[regval]);
488 i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
489 mutex_unlock(&data->update_lock);
490
491 return count;
492 }
493
494 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
495 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
496 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
497 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
498 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
499
500 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
501 BIT(0) | BIT(1));
502 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL,
503 BIT(2) | BIT(3));
504 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL,
505 BIT(4) | BIT(5));
506 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL,
507 BIT(6) | BIT(7));
508
509 static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
510 BIT(1));
511 static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
512 BIT(2));
513 static SENSOR_DEVICE_ATTR(temp4_type, S_IWUSR | S_IRUGO, show_type, set_type,
514 BIT(3));
515 static SENSOR_DEVICE_ATTR(temp5_type, S_IWUSR | S_IRUGO, show_type, set_type,
516 BIT(4));
517
518 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_tcrit1,
519 set_tcrit1, 0);
520 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_tcrit2,
521 set_tcrit2, 0);
522 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_tcrit2,
523 set_tcrit2, 1);
524 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_tcrit1,
525 set_tcrit1, 3);
526 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_tcrit1,
527 set_tcrit1, 4);
528
529 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_tcrit1_hyst,
530 set_tcrit1_hyst, 0);
531 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 0);
532 static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 1);
533 static SENSOR_DEVICE_ATTR(temp4_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 3);
534 static SENSOR_DEVICE_ATTR(temp5_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 4);
535
536 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
537 BIT(0 + 8));
538 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
539 BIT(1 + 16));
540 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
541 BIT(2 + 16));
542 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
543 BIT(3 + 8));
544 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
545 BIT(4 + 8));
546
547 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_tcrit1,
548 set_tcrit1, 1);
549 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_tcrit1,
550 set_tcrit1, 2);
551
552 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 1);
553 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 2);
554
555 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
556 BIT(1 + 8));
557 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
558 BIT(2 + 8));
559
560 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_offset,
561 set_offset, 0);
562 static SENSOR_DEVICE_ATTR(temp3_offset, S_IWUSR | S_IRUGO, show_offset,
563 set_offset, 1);
564 static SENSOR_DEVICE_ATTR(temp4_offset, S_IWUSR | S_IRUGO, show_offset,
565 set_offset, 2);
566 static SENSOR_DEVICE_ATTR(temp5_offset, S_IWUSR | S_IRUGO, show_offset,
567 set_offset, 3);
568
569 static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
570 set_interval);
571
572 static struct attribute *lm95234_common_attrs[] = {
573 &sensor_dev_attr_temp1_input.dev_attr.attr,
574 &sensor_dev_attr_temp2_input.dev_attr.attr,
575 &sensor_dev_attr_temp3_input.dev_attr.attr,
576 &sensor_dev_attr_temp2_fault.dev_attr.attr,
577 &sensor_dev_attr_temp3_fault.dev_attr.attr,
578 &sensor_dev_attr_temp2_type.dev_attr.attr,
579 &sensor_dev_attr_temp3_type.dev_attr.attr,
580 &sensor_dev_attr_temp1_max.dev_attr.attr,
581 &sensor_dev_attr_temp2_max.dev_attr.attr,
582 &sensor_dev_attr_temp3_max.dev_attr.attr,
583 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
584 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
585 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
586 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
587 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
588 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
589 &sensor_dev_attr_temp2_crit.dev_attr.attr,
590 &sensor_dev_attr_temp3_crit.dev_attr.attr,
591 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
592 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
593 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
594 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
595 &sensor_dev_attr_temp2_offset.dev_attr.attr,
596 &sensor_dev_attr_temp3_offset.dev_attr.attr,
597 &dev_attr_update_interval.attr,
598 NULL
599 };
600
601 static const struct attribute_group lm95234_common_group = {
602 .attrs = lm95234_common_attrs,
603 };
604
605 static struct attribute *lm95234_attrs[] = {
606 &sensor_dev_attr_temp4_input.dev_attr.attr,
607 &sensor_dev_attr_temp5_input.dev_attr.attr,
608 &sensor_dev_attr_temp4_fault.dev_attr.attr,
609 &sensor_dev_attr_temp5_fault.dev_attr.attr,
610 &sensor_dev_attr_temp4_type.dev_attr.attr,
611 &sensor_dev_attr_temp5_type.dev_attr.attr,
612 &sensor_dev_attr_temp4_max.dev_attr.attr,
613 &sensor_dev_attr_temp5_max.dev_attr.attr,
614 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
615 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
616 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
617 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
618 &sensor_dev_attr_temp4_offset.dev_attr.attr,
619 &sensor_dev_attr_temp5_offset.dev_attr.attr,
620 NULL
621 };
622
623 static const struct attribute_group lm95234_group = {
624 .attrs = lm95234_attrs,
625 };
626
lm95234_detect(struct i2c_client * client,struct i2c_board_info * info)627 static int lm95234_detect(struct i2c_client *client,
628 struct i2c_board_info *info)
629 {
630 struct i2c_adapter *adapter = client->adapter;
631 int address = client->addr;
632 u8 config_mask, model_mask;
633 int mfg_id, chip_id, val;
634 const char *name;
635
636 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
637 return -ENODEV;
638
639 mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
640 if (mfg_id != NATSEMI_MAN_ID)
641 return -ENODEV;
642
643 chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
644 switch (chip_id) {
645 case LM95233_CHIP_ID:
646 if (address != 0x18 && address != 0x2a && address != 0x2b)
647 return -ENODEV;
648 config_mask = 0xbf;
649 model_mask = 0xf9;
650 name = "lm95233";
651 break;
652 case LM95234_CHIP_ID:
653 if (address != 0x18 && address != 0x4d && address != 0x4e)
654 return -ENODEV;
655 config_mask = 0xbc;
656 model_mask = 0xe1;
657 name = "lm95234";
658 break;
659 default:
660 return -ENODEV;
661 }
662
663 val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
664 if (val & 0x30)
665 return -ENODEV;
666
667 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
668 if (val & config_mask)
669 return -ENODEV;
670
671 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
672 if (val & 0xfc)
673 return -ENODEV;
674
675 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
676 if (val & model_mask)
677 return -ENODEV;
678
679 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
680 if (val & model_mask)
681 return -ENODEV;
682
683 strlcpy(info->type, name, I2C_NAME_SIZE);
684 return 0;
685 }
686
lm95234_init_client(struct i2c_client * client)687 static int lm95234_init_client(struct i2c_client *client)
688 {
689 int val, model;
690
691 /* start conversion if necessary */
692 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
693 if (val < 0)
694 return val;
695 if (val & 0x40)
696 i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
697 val & ~0x40);
698
699 /* If diode type status reports an error, try to fix it */
700 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
701 if (val < 0)
702 return val;
703 model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
704 if (model < 0)
705 return model;
706 if (model & val) {
707 dev_notice(&client->dev,
708 "Fixing remote diode type misconfiguration (0x%x)\n",
709 val);
710 i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
711 model & ~val);
712 }
713 return 0;
714 }
715
lm95234_probe(struct i2c_client * client,const struct i2c_device_id * id)716 static int lm95234_probe(struct i2c_client *client,
717 const struct i2c_device_id *id)
718 {
719 struct device *dev = &client->dev;
720 struct lm95234_data *data;
721 struct device *hwmon_dev;
722 int err;
723
724 data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
725 if (!data)
726 return -ENOMEM;
727
728 data->client = client;
729 mutex_init(&data->update_lock);
730
731 /* Initialize the LM95234 chip */
732 err = lm95234_init_client(client);
733 if (err < 0)
734 return err;
735
736 data->groups[0] = &lm95234_common_group;
737 if (id->driver_data == lm95234)
738 data->groups[1] = &lm95234_group;
739
740 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
741 data, data->groups);
742 return PTR_ERR_OR_ZERO(hwmon_dev);
743 }
744
745 /* Driver data (common to all clients) */
746 static const struct i2c_device_id lm95234_id[] = {
747 { "lm95233", lm95233 },
748 { "lm95234", lm95234 },
749 { }
750 };
751 MODULE_DEVICE_TABLE(i2c, lm95234_id);
752
753 static struct i2c_driver lm95234_driver = {
754 .class = I2C_CLASS_HWMON,
755 .driver = {
756 .name = DRVNAME,
757 },
758 .probe = lm95234_probe,
759 .id_table = lm95234_id,
760 .detect = lm95234_detect,
761 .address_list = normal_i2c,
762 };
763
764 module_i2c_driver(lm95234_driver);
765
766 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
767 MODULE_DESCRIPTION("LM95233/LM95234 sensor driver");
768 MODULE_LICENSE("GPL");
769