blob: 02cd48bdde8dfafc425c90d4e2fcb87e0ca43bd4 [file] [log] [blame]
Guenter Roecke1eb4902013-03-10 16:54:19 -07001/*
2 * Driver for Texas Instruments / National Semiconductor LM95234
3 *
Guenter Roeckdfcd4c52014-04-22 09:34:14 -07004 * Copyright (c) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
Guenter Roecke1eb4902013-03-10 16:54:19 -07005 *
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
Guenter Roeckdfcd4c52014-04-22 09:34:14 -070033enum chips { lm95233, lm95234 };
34
35static const unsigned short normal_i2c[] = {
36 0x18, 0x2a, 0x2b, 0x4d, 0x4e, I2C_CLIENT_END };
Guenter Roecke1eb4902013-03-10 16:54:19 -070037
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
Guenter Roeckdfcd4c52014-04-22 09:34:14 -070059#define LM95233_CHIP_ID 0x89
Guenter Roecke1eb4902013-03-10 16:54:19 -070060#define LM95234_CHIP_ID 0x79
61
62/* Client data (each client gets its own) */
63struct lm95234_data {
Guenter Roeck9d86bd62013-09-04 16:39:12 -070064 struct i2c_client *client;
Guenter Roeckdfcd4c52014-04-22 09:34:14 -070065 const struct attribute_group *groups[3];
Guenter Roecke1eb4902013-03-10 16:54:19 -070066 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
80static 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
118static u16 update_intervals[] = { 143, 364, 1000, 2500 };
119
120/* Fill value cache. Must be called with update lock held. */
121
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700122static int lm95234_fill_cache(struct lm95234_data *data,
123 struct i2c_client *client)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700124{
Guenter Roecke1eb4902013-03-10 16:54:19 -0700125 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
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700165static int lm95234_update_device(struct lm95234_data *data)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700166{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700167 struct i2c_client *client = data->client;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700168 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) {
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700177 ret = lm95234_fill_cache(data, client);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700178 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;
208abort:
209 mutex_unlock(&data->update_lock);
210
211 return ret;
212}
213
Guenter Roeck33721d92018-12-06 10:38:29 -0800214static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700215 char *buf)
216{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700217 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700218 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700219 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700220
221 if (ret)
222 return ret;
223
224 return sprintf(buf, "%d\n",
225 DIV_ROUND_CLOSEST(data->temp[index] * 125, 32));
226}
227
Guenter Roeck33721d92018-12-06 10:38:29 -0800228static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
229 char *buf)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700230{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700231 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700232 u32 mask = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700233 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700234
235 if (ret)
236 return ret;
237
238 return sprintf(buf, "%u", !!(data->status & mask));
239}
240
Guenter Roeck33721d92018-12-06 10:38:29 -0800241static ssize_t type_show(struct device *dev, struct device_attribute *attr,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700242 char *buf)
243{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700244 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700245 u8 mask = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700246 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700247
248 if (ret)
249 return ret;
250
251 return sprintf(buf, data->sensor_type & mask ? "1\n" : "2\n");
252}
253
Guenter Roeck33721d92018-12-06 10:38:29 -0800254static ssize_t type_store(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t count)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700256{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700257 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700258 unsigned long val;
259 u8 mask = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700260 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700261
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;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700278 i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700279 data->sensor_type);
280 mutex_unlock(&data->update_lock);
281
282 return count;
283}
284
Guenter Roeck33721d92018-12-06 10:38:29 -0800285static ssize_t tcrit2_show(struct device *dev, struct device_attribute *attr,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700286 char *buf)
287{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700288 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700289 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700290 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700291
292 if (ret)
293 return ret;
294
295 return sprintf(buf, "%u", data->tcrit2[index] * 1000);
296}
297
Guenter Roeck33721d92018-12-06 10:38:29 -0800298static ssize_t tcrit2_store(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t count)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700300{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700301 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700302 int index = to_sensor_dev_attr(attr)->index;
303 long val;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700304 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700305
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;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700317 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700318 mutex_unlock(&data->update_lock);
319
320 return count;
321}
322
Guenter Roeck33721d92018-12-06 10:38:29 -0800323static ssize_t tcrit2_hyst_show(struct device *dev,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700324 struct device_attribute *attr, char *buf)
325{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700326 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700327 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700328 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700329
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
Guenter Roeck33721d92018-12-06 10:38:29 -0800338static ssize_t tcrit1_show(struct device *dev, struct device_attribute *attr,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700339 char *buf)
340{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700341 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700342 int index = to_sensor_dev_attr(attr)->index;
343
344 return sprintf(buf, "%u", data->tcrit1[index] * 1000);
345}
346
Guenter Roeck33721d92018-12-06 10:38:29 -0800347static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr,
348 const char *buf, size_t count)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700349{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700350 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700351 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700352 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700353 long val;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700354
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;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700366 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700367 mutex_unlock(&data->update_lock);
368
369 return count;
370}
371
Guenter Roeck33721d92018-12-06 10:38:29 -0800372static ssize_t tcrit1_hyst_show(struct device *dev,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700373 struct device_attribute *attr, char *buf)
374{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700375 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700376 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700377 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700378
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
Guenter Roeck33721d92018-12-06 10:38:29 -0800387static ssize_t tcrit1_hyst_store(struct device *dev,
388 struct device_attribute *attr,
389 const char *buf, size_t count)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700390{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700391 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700392 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700393 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700394 long val;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700395
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;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700408 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700409 mutex_unlock(&data->update_lock);
410
411 return count;
412}
413
Guenter Roeck33721d92018-12-06 10:38:29 -0800414static ssize_t offset_show(struct device *dev, struct device_attribute *attr,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700415 char *buf)
416{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700417 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700418 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700419 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700420
421 if (ret)
422 return ret;
423
424 return sprintf(buf, "%d", data->toffset[index] * 500);
425}
426
Guenter Roeck33721d92018-12-06 10:38:29 -0800427static ssize_t offset_store(struct device *dev, struct device_attribute *attr,
428 const char *buf, size_t count)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700429{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700430 struct lm95234_data *data = dev_get_drvdata(dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700431 int index = to_sensor_dev_attr(attr)->index;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700432 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700433 long val;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700434
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;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700447 i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700448 mutex_unlock(&data->update_lock);
449
450 return count;
451}
452
Julia Lawall2f2dbea2016-12-22 13:05:32 +0100453static ssize_t update_interval_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700455{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700456 struct lm95234_data *data = dev_get_drvdata(dev);
457 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700458
459 if (ret)
460 return ret;
461
462 return sprintf(buf, "%lu\n",
463 DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
464}
465
Julia Lawall2f2dbea2016-12-22 13:05:32 +0100466static ssize_t update_interval_store(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700469{
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700470 struct lm95234_data *data = dev_get_drvdata(dev);
471 int ret = lm95234_update_device(data);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700472 unsigned long val;
473 u8 regval;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700474
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]);
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700489 i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700490 mutex_unlock(&data->update_lock);
491
492 return count;
493}
494
Guenter Roeck33721d92018-12-06 10:38:29 -0800495static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
496static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
497static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
498static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
499static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700500
Guenter Roeck33721d92018-12-06 10:38:29 -0800501static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, BIT(0) | BIT(1));
502static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, BIT(2) | BIT(3));
503static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, BIT(4) | BIT(5));
504static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, BIT(6) | BIT(7));
Guenter Roecke1eb4902013-03-10 16:54:19 -0700505
Guenter Roeck33721d92018-12-06 10:38:29 -0800506static SENSOR_DEVICE_ATTR_RW(temp2_type, type, BIT(1));
507static SENSOR_DEVICE_ATTR_RW(temp3_type, type, BIT(2));
508static SENSOR_DEVICE_ATTR_RW(temp4_type, type, BIT(3));
509static SENSOR_DEVICE_ATTR_RW(temp5_type, type, BIT(4));
Guenter Roecke1eb4902013-03-10 16:54:19 -0700510
Guenter Roeck33721d92018-12-06 10:38:29 -0800511static SENSOR_DEVICE_ATTR_RW(temp1_max, tcrit1, 0);
512static SENSOR_DEVICE_ATTR_RW(temp2_max, tcrit2, 0);
513static SENSOR_DEVICE_ATTR_RW(temp3_max, tcrit2, 1);
514static SENSOR_DEVICE_ATTR_RW(temp4_max, tcrit1, 3);
515static SENSOR_DEVICE_ATTR_RW(temp5_max, tcrit1, 4);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700516
Guenter Roeck33721d92018-12-06 10:38:29 -0800517static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, tcrit1_hyst, 0);
518static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, tcrit2_hyst, 0);
519static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, tcrit2_hyst, 1);
520static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, tcrit1_hyst, 3);
521static SENSOR_DEVICE_ATTR_RO(temp5_max_hyst, tcrit1_hyst, 4);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700522
Guenter Roeck33721d92018-12-06 10:38:29 -0800523static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, BIT(0 + 8));
524static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, BIT(1 + 16));
525static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, BIT(2 + 16));
526static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, BIT(3 + 8));
527static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, BIT(4 + 8));
Guenter Roecke1eb4902013-03-10 16:54:19 -0700528
Guenter Roeck33721d92018-12-06 10:38:29 -0800529static SENSOR_DEVICE_ATTR_RW(temp2_crit, tcrit1, 1);
530static SENSOR_DEVICE_ATTR_RW(temp3_crit, tcrit1, 2);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700531
Guenter Roeck33721d92018-12-06 10:38:29 -0800532static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, tcrit1_hyst, 1);
533static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, tcrit1_hyst, 2);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700534
Guenter Roeck33721d92018-12-06 10:38:29 -0800535static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, BIT(1 + 8));
536static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, BIT(2 + 8));
Guenter Roecke1eb4902013-03-10 16:54:19 -0700537
Guenter Roeck33721d92018-12-06 10:38:29 -0800538static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 0);
539static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 1);
540static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 2);
541static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 3);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700542
Julia Lawall2f2dbea2016-12-22 13:05:32 +0100543static DEVICE_ATTR_RW(update_interval);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700544
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700545static struct attribute *lm95234_common_attrs[] = {
Guenter Roecke1eb4902013-03-10 16:54:19 -0700546 &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,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700549 &sensor_dev_attr_temp2_fault.dev_attr.attr,
550 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700551 &sensor_dev_attr_temp2_type.dev_attr.attr,
552 &sensor_dev_attr_temp3_type.dev_attr.attr,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700553 &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,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700556 &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,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700559 &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,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700562 &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,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700570 &dev_attr_update_interval.attr,
571 NULL
572};
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700573
574static const struct attribute_group lm95234_common_group = {
575 .attrs = lm95234_common_attrs,
576};
577
578static 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
596static const struct attribute_group lm95234_group = {
597 .attrs = lm95234_attrs,
598};
Guenter Roecke1eb4902013-03-10 16:54:19 -0700599
600static int lm95234_detect(struct i2c_client *client,
601 struct i2c_board_info *info)
602{
603 struct i2c_adapter *adapter = client->adapter;
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700604 int address = client->addr;
605 u8 config_mask, model_mask;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700606 int mfg_id, chip_id, val;
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700607 const char *name;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700608
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);
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700617 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:
Guenter Roecke1eb4902013-03-10 16:54:19 -0700633 return -ENODEV;
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700634 }
Guenter Roecke1eb4902013-03-10 16:54:19 -0700635
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);
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700641 if (val & config_mask)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700642 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);
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700649 if (val & model_mask)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700650 return -ENODEV;
651
652 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700653 if (val & model_mask)
Guenter Roecke1eb4902013-03-10 16:54:19 -0700654 return -ENODEV;
655
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700656 strlcpy(info->type, name, I2C_NAME_SIZE);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700657 return 0;
658}
659
660static 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
689static 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;
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700694 struct device *hwmon_dev;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700695 int err;
696
697 data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
698 if (!data)
699 return -ENOMEM;
700
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700701 data->client = client;
Guenter Roecke1eb4902013-03-10 16:54:19 -0700702 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
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700709 data->groups[0] = &lm95234_common_group;
710 if (id->driver_data == lm95234)
711 data->groups[1] = &lm95234_group;
712
Guenter Roeck9d86bd62013-09-04 16:39:12 -0700713 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700714 data, data->groups);
Fengguang Wu3ea85ba2013-09-17 20:54:14 -0700715 return PTR_ERR_OR_ZERO(hwmon_dev);
Guenter Roecke1eb4902013-03-10 16:54:19 -0700716}
717
718/* Driver data (common to all clients) */
719static const struct i2c_device_id lm95234_id[] = {
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700720 { "lm95233", lm95233 },
721 { "lm95234", lm95234 },
Guenter Roecke1eb4902013-03-10 16:54:19 -0700722 { }
723};
724MODULE_DEVICE_TABLE(i2c, lm95234_id);
725
726static struct i2c_driver lm95234_driver = {
727 .class = I2C_CLASS_HWMON,
728 .driver = {
729 .name = DRVNAME,
730 },
731 .probe = lm95234_probe,
Guenter Roecke1eb4902013-03-10 16:54:19 -0700732 .id_table = lm95234_id,
733 .detect = lm95234_detect,
734 .address_list = normal_i2c,
735};
736
737module_i2c_driver(lm95234_driver);
738
739MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
Guenter Roeckdfcd4c52014-04-22 09:34:14 -0700740MODULE_DESCRIPTION("LM95233/LM95234 sensor driver");
Guenter Roecke1eb4902013-03-10 16:54:19 -0700741MODULE_LICENSE("GPL");