blob: 411202bdaf6b6bf8c3eb0141ad7a62ecd7d4cfbe [file] [log] [blame]
Guenter Roecke1eb4902013-03-10 16:54:19 -07001/*
2 * Driver for Texas Instruments / National Semiconductor LM95234
3 *
4 * Copyright (c) 2013 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
33static const unsigned short normal_i2c[] = { 0x18, 0x4d, 0x4e, I2C_CLIENT_END };
34
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
54
55#define NATSEMI_MAN_ID 0x01
56#define LM95234_CHIP_ID 0x79
57
58/* Client data (each client gets its own) */
59struct lm95234_data {
Guenter Roeck9d86bd62013-09-04 16:39:12 -070060 struct i2c_client *client;
Guenter Roecke1eb4902013-03-10 16:54:19 -070061 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 */
71
72 u8 sensor_type; /* temperature sensor type */
73};
74
75static int lm95234_read_temp(struct i2c_client *client, int index, int *t)
76{
77 int val;
78 u16 temp = 0;
79
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;
92 }
93 /*
94 * Read signed temperature if unsigned temperature is 0,
95 * or if this is the local sensor.
96 */
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;
109 }
110 return 0;
111}
112
113static u16 update_intervals[] = { 143, 364, 1000, 2500 };
114
115/* Fill value cache. Must be called with update lock held. */
116
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700117static int lm95234_fill_cache(struct lm95234_data *data,
118 struct i2c_client *client)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700119{
Guenter Roecke1eb4902013-03-10 16:54:19 -0700120 int i, ret;
121
122 ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
123 if (ret < 0)
124 return ret;
125
126 data->interval = msecs_to_jiffies(update_intervals[ret & 0x03]);
127
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;
133 }
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;
139 }
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;
145 }
146
147 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST);
148 if (ret < 0)
149 return ret;
150 data->thyst = ret;
151
152 ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
153 if (ret < 0)
154 return ret;
155 data->sensor_type = ret;
156
157 return 0;
158}
159
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700160static int lm95234_update_device(struct lm95234_data *data)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700161{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700162 struct i2c_client *client = data->client;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700163 int ret;
164
165 mutex_lock(&data->update_lock);
166
167 if (time_after(jiffies, data->last_updated + data->interval) ||
168 !data->valid) {
169 int i;
170
171 if (!data->valid) {
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700172 ret = lm95234_fill_cache(data, client);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700173 if (ret < 0)
174 goto abort;
175 }
176
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;
182 }
183
184 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT);
185 if (ret < 0)
186 goto abort;
187 data->status = ret;
188
189 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1);
190 if (ret < 0)
191 goto abort;
192 data->status |= ret << 8;
193
194 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2);
195 if (ret < 0)
196 goto abort;
197 data->status |= ret << 16;
198
199 data->last_updated = jiffies;
200 data->valid = true;
201 }
202 ret = 0;
203abort:
204 mutex_unlock(&data->update_lock);
205
206 return ret;
207}
208
209static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
210 char *buf)
211{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700212 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700213 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700214 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700215
216 if (ret)
217 return ret;
218
219 return sprintf(buf, "%d\n",
220 DIV_ROUND_CLOSEST(data->temp[index] * 125, 32));
221}
222
223static ssize_t show_alarm(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700226 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700227 u32 mask = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700228 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700229
230 if (ret)
231 return ret;
232
233 return sprintf(buf, "%u", !!(data->status & mask));
234}
235
236static ssize_t show_type(struct device *dev, struct device_attribute *attr,
237 char *buf)
238{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700239 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700240 u8 mask = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700241 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700242
243 if (ret)
244 return ret;
245
246 return sprintf(buf, data->sensor_type & mask ? "1\n" : "2\n");
247}
248
249static ssize_t set_type(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t count)
251{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700252 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700253 unsigned long val;
254 u8 mask = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700255 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700256
257 if (ret)
258 return ret;
259
260 ret = kstrtoul(buf, 10, &val);
261 if (ret < 0)
262 return ret;
263
264 if (val != 1 && val != 2)
265 return -EINVAL;
266
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;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700273 i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700274 data->sensor_type);
275 mutex_unlock(&data->update_lock);
276
277 return count;
278}
279
280static ssize_t show_tcrit2(struct device *dev, struct device_attribute *attr,
281 char *buf)
282{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700283 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700284 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700285 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700286
287 if (ret)
288 return ret;
289
290 return sprintf(buf, "%u", data->tcrit2[index] * 1000);
291}
292
293static ssize_t set_tcrit2(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
295{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700296 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700297 int index = to_sensor_dev_attr(attr)->index;
298 long val;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700299 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700300
301 if (ret)
302 return ret;
303
304 ret = kstrtol(buf, 10, &val);
305 if (ret < 0)
306 return ret;
307
308 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127);
309
310 mutex_lock(&data->update_lock);
311 data->tcrit2[index] = val;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700312 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700313 mutex_unlock(&data->update_lock);
314
315 return count;
316}
317
318static ssize_t show_tcrit2_hyst(struct device *dev,
319 struct device_attribute *attr, char *buf)
320{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700321 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700322 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700323 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700324
325 if (ret)
326 return ret;
327
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);
331}
332
333static ssize_t show_tcrit1(struct device *dev, struct device_attribute *attr,
334 char *buf)
335{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700336 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700337 int index = to_sensor_dev_attr(attr)->index;
338
339 return sprintf(buf, "%u", data->tcrit1[index] * 1000);
340}
341
342static ssize_t set_tcrit1(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
344{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700345 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700346 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700347 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700348 long val;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700349
350 if (ret)
351 return ret;
352
353 ret = kstrtol(buf, 10, &val);
354 if (ret < 0)
355 return ret;
356
357 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
358
359 mutex_lock(&data->update_lock);
360 data->tcrit1[index] = val;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700361 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700362 mutex_unlock(&data->update_lock);
363
364 return count;
365}
366
367static ssize_t show_tcrit1_hyst(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700370 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700371 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700372 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700373
374 if (ret)
375 return ret;
376
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);
380}
381
382static ssize_t set_tcrit1_hyst(struct device *dev,
383 struct device_attribute *attr,
384 const char *buf, size_t count)
385{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700386 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700387 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700388 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700389 long val;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700390
391 if (ret)
392 return ret;
393
394 ret = kstrtol(buf, 10, &val);
395 if (ret < 0)
396 return ret;
397
398 val = DIV_ROUND_CLOSEST(val, 1000);
399 val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
400
401 mutex_lock(&data->update_lock);
402 data->thyst = val;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700403 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700404 mutex_unlock(&data->update_lock);
405
406 return count;
407}
408
409static ssize_t show_offset(struct device *dev, struct device_attribute *attr,
410 char *buf)
411{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700412 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700413 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700414 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700415
416 if (ret)
417 return ret;
418
419 return sprintf(buf, "%d", data->toffset[index] * 500);
420}
421
422static ssize_t set_offset(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
424{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700425 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700426 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700427 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700428 long val;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700429
430 if (ret)
431 return ret;
432
433 ret = kstrtol(buf, 10, &val);
434 if (ret < 0)
435 return ret;
436
437 /* Accuracy is 1/2 degrees C */
438 val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127);
439
440 mutex_lock(&data->update_lock);
441 data->toffset[index] = val;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700442 i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700443 mutex_unlock(&data->update_lock);
444
445 return count;
446}
447
448static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
449 char *buf)
450{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700451 struct lm95234_data *data = dev_get_drvdata(dev);
452 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700453
454 if (ret)
455 return ret;
456
457 return sprintf(buf, "%lu\n",
458 DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
459}
460
461static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
462 const char *buf, size_t count)
463{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700464 struct lm95234_data *data = dev_get_drvdata(dev);
465 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700466 unsigned long val;
467 u8 regval;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700468
469 if (ret)
470 return ret;
471
472 ret = kstrtoul(buf, 10, &val);
473 if (ret < 0)
474 return ret;
475
476 for (regval = 0; regval < 3; regval++) {
477 if (val <= update_intervals[regval])
478 break;
479 }
480
481 mutex_lock(&data->update_lock);
482 data->interval = msecs_to_jiffies(update_intervals[regval]);
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700483 i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700484 mutex_unlock(&data->update_lock);
485
486 return count;
487}
488
489static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
490static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
491static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
492static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
493static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
494
495static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
496 BIT(0) | BIT(1));
497static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL,
498 BIT(2) | BIT(3));
499static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL,
500 BIT(4) | BIT(5));
501static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL,
502 BIT(6) | BIT(7));
503
504static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
505 BIT(1));
506static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
507 BIT(2));
508static SENSOR_DEVICE_ATTR(temp4_type, S_IWUSR | S_IRUGO, show_type, set_type,
509 BIT(3));
510static SENSOR_DEVICE_ATTR(temp5_type, S_IWUSR | S_IRUGO, show_type, set_type,
511 BIT(4));
512
513static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_tcrit1,
514 set_tcrit1, 0);
515static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_tcrit2,
516 set_tcrit2, 0);
517static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_tcrit2,
518 set_tcrit2, 1);
519static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_tcrit1,
520 set_tcrit1, 3);
521static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_tcrit1,
522 set_tcrit1, 4);
523
524static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_tcrit1_hyst,
525 set_tcrit1_hyst, 0);
526static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 0);
527static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 1);
528static SENSOR_DEVICE_ATTR(temp4_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 3);
529static SENSOR_DEVICE_ATTR(temp5_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 4);
530
531static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
532 BIT(0 + 8));
533static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
534 BIT(1 + 16));
535static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
536 BIT(2 + 16));
537static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
538 BIT(3 + 8));
539static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
540 BIT(4 + 8));
541
542static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_tcrit1,
543 set_tcrit1, 1);
544static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_tcrit1,
545 set_tcrit1, 2);
546
547static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 1);
548static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 2);
549
550static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
551 BIT(1 + 8));
552static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
553 BIT(2 + 8));
554
555static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_offset,
556 set_offset, 0);
557static SENSOR_DEVICE_ATTR(temp3_offset, S_IWUSR | S_IRUGO, show_offset,
558 set_offset, 1);
559static SENSOR_DEVICE_ATTR(temp4_offset, S_IWUSR | S_IRUGO, show_offset,
560 set_offset, 2);
561static SENSOR_DEVICE_ATTR(temp5_offset, S_IWUSR | S_IRUGO, show_offset,
562 set_offset, 3);
563
564static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
565 set_interval);
566
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700567static struct attribute *lm95234_attrs[] = {
Guenter Roecke1eb4902013-03-10 16:54:19 -0700568 &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
608};
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700609ATTRIBUTE_GROUPS(lm95234);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700610
611static int lm95234_detect(struct i2c_client *client,
612 struct i2c_board_info *info)
613{
614 struct i2c_adapter *adapter = client->adapter;
615 int mfg_id, chip_id, val;
616
617 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
618 return -ENODEV;
619
620 mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
621 if (mfg_id != NATSEMI_MAN_ID)
622 return -ENODEV;
623
624 chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
625 if (chip_id != LM95234_CHIP_ID)
626 return -ENODEV;
627
628 val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
629 if (val & 0x30)
630 return -ENODEV;
631
632 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
633 if (val & 0xbc)
634 return -ENODEV;
635
636 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
637 if (val & 0xfc)
638 return -ENODEV;
639
640 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
641 if (val & 0xe1)
642 return -ENODEV;
643
644 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
645 if (val & 0xe1)
646 return -ENODEV;
647
648 strlcpy(info->type, "lm95234", I2C_NAME_SIZE);
649 return 0;
650}
651
652static int lm95234_init_client(struct i2c_client *client)
653{
654 int val, model;
655
656 /* start conversion if necessary */
657 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
658 if (val < 0)
659 return val;
660 if (val & 0x40)
661 i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
662 val & ~0x40);
663
664 /* If diode type status reports an error, try to fix it */
665 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
666 if (val < 0)
667 return val;
668 model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
669 if (model < 0)
670 return model;
671 if (model & val) {
672 dev_notice(&client->dev,
673 "Fixing remote diode type misconfiguration (0x%x)\n",
674 val);
675 i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
676 model & ~val);
677 }
678 return 0;
679}
680
681static int lm95234_probe(struct i2c_client *client,
682 const struct i2c_device_id *id)
683{
684 struct device *dev = &client->dev;
685 struct lm95234_data *data;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700686 struct device *hwmon_dev;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700687 int err;
688
689 data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
690 if (!data)
691 return -ENOMEM;
692
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700693 data->client = client;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700694 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
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700701 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
702 data,
703 lm95234_groups);
Fengguang Wu3ea85ba2013-09-17 20:54:14 -0700704 return PTR_ERR_OR_ZERO(hwmon_dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700705}
706
707/* Driver data (common to all clients) */
708static const struct i2c_device_id lm95234_id[] = {
709 { "lm95234", 0 },
710 { }
711};
712MODULE_DEVICE_TABLE(i2c, lm95234_id);
713
714static struct i2c_driver lm95234_driver = {
715 .class = I2C_CLASS_HWMON,
716 .driver = {
717 .name = DRVNAME,
718 },
719 .probe = lm95234_probe,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700720 .id_table = lm95234_id,
721 .detect = lm95234_detect,
722 .address_list = normal_i2c,
723};
724
725module_i2c_driver(lm95234_driver);
726
727MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
728MODULE_DESCRIPTION("LM95234 sensor driver");
729MODULE_LICENSE("GPL");