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) */
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)
81 val = i2c_smbus_read_byte_data(client,
82 LM95234_REG_UTEMPH(index - 1));
86 val = i2c_smbus_read_byte_data(client,
87 LM95234_REG_UTEMPL(index - 1));
94 * Read signed temperature if unsigned temperature is 0,
95 * or if this is the local sensor.
98 val = i2c_smbus_read_byte_data(client,
99 LM95234_REG_TEMPH(index));
103 val = i2c_smbus_read_byte_data(client,
104 LM95234_REG_TEMPL(index));
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)
122 ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
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));
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));
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));
144 data->toffset[i] = ret;
147 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST);
152 ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
155 data->sensor_type = ret;
160 static int lm95234_update_device(struct lm95234_data *data)
162 struct i2c_client *client = data->client;
165 mutex_lock(&data->update_lock);
167 if (time_after(jiffies, data->last_updated + data->interval) ||
172 ret = lm95234_fill_cache(data, client);
178 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
179 ret = lm95234_read_temp(client, i, &data->temp[i]);
184 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT);
189 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1);
192 data->status |= ret << 8;
194 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2);
197 data->status |= ret << 16;
199 data->last_updated = jiffies;
204 mutex_unlock(&data->update_lock);
209 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
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);
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);
233 return sprintf(buf, "%u", !!(data->status & mask));
236 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
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);
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);
254 u8 mask = to_sensor_dev_attr(attr)->index;
255 int ret = lm95234_update_device(data);
260 ret = kstrtoul(buf, 10, &val);
264 if (val != 1 && val != 2)
267 mutex_lock(&data->update_lock);
269 data->sensor_type |= mask;
271 data->sensor_type &= ~mask;
273 i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL,
275 mutex_unlock(&data->update_lock);
280 static ssize_t show_tcrit2(struct device *dev, struct device_attribute *attr,
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);
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;
299 int ret = lm95234_update_device(data);
304 ret = kstrtol(buf, 10, &val);
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);
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);
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,
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);
353 ret = kstrtol(buf, 10, &val);
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);
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);
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);
394 ret = kstrtol(buf, 10, &val);
398 val = DIV_ROUND_CLOSEST(val, 1000);
399 val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
401 mutex_lock(&data->update_lock);
403 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
404 mutex_unlock(&data->update_lock);
409 static ssize_t show_offset(struct device *dev, struct device_attribute *attr,
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);
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);
433 ret = kstrtol(buf, 10, &val);
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);
448 static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
451 struct lm95234_data *data = dev_get_drvdata(dev);
452 int ret = lm95234_update_device(data);
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);
472 ret = kstrtoul(buf, 10, &val);
476 for (regval = 0; regval < 3; regval++) {
477 if (val <= update_intervals[regval])
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);
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,
497 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL,
499 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL,
501 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL,
504 static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
506 static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
508 static SENSOR_DEVICE_ATTR(temp4_type, S_IWUSR | S_IRUGO, show_type, set_type,
510 static SENSOR_DEVICE_ATTR(temp5_type, S_IWUSR | S_IRUGO, show_type, set_type,
513 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_tcrit1,
515 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_tcrit2,
517 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_tcrit2,
519 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_tcrit1,
521 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_tcrit1,
524 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_tcrit1_hyst,
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,
533 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
535 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
537 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
539 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
542 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_tcrit1,
544 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_tcrit1,
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,
552 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
555 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_offset,
557 static SENSOR_DEVICE_ATTR(temp3_offset, S_IWUSR | S_IRUGO, show_offset,
559 static SENSOR_DEVICE_ATTR(temp4_offset, S_IWUSR | S_IRUGO, show_offset,
561 static SENSOR_DEVICE_ATTR(temp5_offset, S_IWUSR | S_IRUGO, show_offset,
564 static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_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,
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))
620 mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
621 if (mfg_id != NATSEMI_MAN_ID)
624 chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
625 if (chip_id != LM95234_CHIP_ID)
628 val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
632 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
636 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
640 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
644 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
648 strlcpy(info->type, "lm95234", I2C_NAME_SIZE);
652 static int lm95234_init_client(struct i2c_client *client)
656 /* start conversion if necessary */
657 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
661 i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
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);
668 model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
672 dev_notice(&client->dev,
673 "Fixing remote diode type misconfiguration (0x%x)\n",
675 i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
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;
689 data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
693 data->client = client;
694 mutex_init(&data->update_lock);
696 /* Initialize the LM95234 chip */
697 err = lm95234_init_client(client);
701 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
704 return PTR_ERR_OR_ZERO(hwmon_dev);
707 /* Driver data (common to all clients) */
708 static const struct i2c_device_id lm95234_id[] = {
712 MODULE_DEVICE_TABLE(i2c, lm95234_id);
714 static struct i2c_driver lm95234_driver = {
715 .class = I2C_CLASS_HWMON,
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");