1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Texas Instruments / National Semiconductor LM95234
5 * Copyright (c) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
7 * Derived from lm95241.c
8 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/i2c.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/sysfs.h>
22 #define DRVNAME "lm95234"
24 enum chips { lm95233, lm95234 };
26 static const unsigned short normal_i2c[] = {
27 0x18, 0x2a, 0x2b, 0x4d, 0x4e, I2C_CLIENT_END };
29 /* LM95234 registers */
30 #define LM95234_REG_MAN_ID 0xFE
31 #define LM95234_REG_CHIP_ID 0xFF
32 #define LM95234_REG_STATUS 0x02
33 #define LM95234_REG_CONFIG 0x03
34 #define LM95234_REG_CONVRATE 0x04
35 #define LM95234_REG_STS_FAULT 0x07
36 #define LM95234_REG_STS_TCRIT1 0x08
37 #define LM95234_REG_STS_TCRIT2 0x09
38 #define LM95234_REG_TEMPH(x) ((x) + 0x10)
39 #define LM95234_REG_TEMPL(x) ((x) + 0x20)
40 #define LM95234_REG_UTEMPH(x) ((x) + 0x19) /* Remote only */
41 #define LM95234_REG_UTEMPL(x) ((x) + 0x29)
42 #define LM95234_REG_REM_MODEL 0x30
43 #define LM95234_REG_REM_MODEL_STS 0x38
44 #define LM95234_REG_OFFSET(x) ((x) + 0x31) /* Remote only */
45 #define LM95234_REG_TCRIT1(x) ((x) + 0x40)
46 #define LM95234_REG_TCRIT2(x) ((x) + 0x49) /* Remote channel 1,2 */
47 #define LM95234_REG_TCRIT_HYST 0x5a
49 #define NATSEMI_MAN_ID 0x01
50 #define LM95233_CHIP_ID 0x89
51 #define LM95234_CHIP_ID 0x79
53 /* Client data (each client gets its own) */
55 struct i2c_client *client;
56 const struct attribute_group *groups[3];
57 struct mutex update_lock;
58 unsigned long last_updated, interval; /* in jiffies */
59 bool valid; /* false until following fields are valid */
60 /* registers values */
61 int temp[5]; /* temperature (signed) */
62 u32 status; /* fault/alarm status */
63 u8 tcrit1[5]; /* critical temperature limit */
64 u8 tcrit2[2]; /* high temperature limit */
65 s8 toffset[4]; /* remote temperature offset */
66 u8 thyst; /* common hysteresis */
68 u8 sensor_type; /* temperature sensor type */
71 static int lm95234_read_temp(struct i2c_client *client, int index, int *t)
77 val = i2c_smbus_read_byte_data(client,
78 LM95234_REG_UTEMPH(index - 1));
82 val = i2c_smbus_read_byte_data(client,
83 LM95234_REG_UTEMPL(index - 1));
90 * Read signed temperature if unsigned temperature is 0,
91 * or if this is the local sensor.
94 val = i2c_smbus_read_byte_data(client,
95 LM95234_REG_TEMPH(index));
99 val = i2c_smbus_read_byte_data(client,
100 LM95234_REG_TEMPL(index));
109 static u16 update_intervals[] = { 143, 364, 1000, 2500 };
111 /* Fill value cache. Must be called with update lock held. */
113 static int lm95234_fill_cache(struct lm95234_data *data,
114 struct i2c_client *client)
118 ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
122 data->interval = msecs_to_jiffies(update_intervals[ret & 0x03]);
124 for (i = 0; i < ARRAY_SIZE(data->tcrit1); i++) {
125 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT1(i));
128 data->tcrit1[i] = ret;
130 for (i = 0; i < ARRAY_SIZE(data->tcrit2); i++) {
131 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT2(i));
134 data->tcrit2[i] = ret;
136 for (i = 0; i < ARRAY_SIZE(data->toffset); i++) {
137 ret = i2c_smbus_read_byte_data(client, LM95234_REG_OFFSET(i));
140 data->toffset[i] = ret;
143 ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST);
148 ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
151 data->sensor_type = ret;
156 static int lm95234_update_device(struct lm95234_data *data)
158 struct i2c_client *client = data->client;
161 mutex_lock(&data->update_lock);
163 if (time_after(jiffies, data->last_updated + data->interval) ||
168 ret = lm95234_fill_cache(data, client);
174 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
175 ret = lm95234_read_temp(client, i, &data->temp[i]);
180 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT);
185 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1);
188 data->status |= ret << 8;
190 ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2);
193 data->status |= ret << 16;
195 data->last_updated = jiffies;
200 mutex_unlock(&data->update_lock);
205 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
208 struct lm95234_data *data = dev_get_drvdata(dev);
209 int index = to_sensor_dev_attr(attr)->index;
210 int ret = lm95234_update_device(data);
215 return sprintf(buf, "%d\n",
216 DIV_ROUND_CLOSEST(data->temp[index] * 125, 32));
219 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
222 struct lm95234_data *data = dev_get_drvdata(dev);
223 u32 mask = to_sensor_dev_attr(attr)->index;
224 int ret = lm95234_update_device(data);
229 return sprintf(buf, "%u", !!(data->status & mask));
232 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
235 struct lm95234_data *data = dev_get_drvdata(dev);
236 u8 mask = to_sensor_dev_attr(attr)->index;
237 int ret = lm95234_update_device(data);
242 return sprintf(buf, data->sensor_type & mask ? "1\n" : "2\n");
245 static ssize_t type_store(struct device *dev, struct device_attribute *attr,
246 const char *buf, size_t count)
248 struct lm95234_data *data = dev_get_drvdata(dev);
250 u8 mask = to_sensor_dev_attr(attr)->index;
251 int ret = lm95234_update_device(data);
256 ret = kstrtoul(buf, 10, &val);
260 if (val != 1 && val != 2)
263 mutex_lock(&data->update_lock);
265 data->sensor_type |= mask;
267 data->sensor_type &= ~mask;
269 i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL,
271 mutex_unlock(&data->update_lock);
276 static ssize_t tcrit2_show(struct device *dev, struct device_attribute *attr,
279 struct lm95234_data *data = dev_get_drvdata(dev);
280 int index = to_sensor_dev_attr(attr)->index;
281 int ret = lm95234_update_device(data);
286 return sprintf(buf, "%u", data->tcrit2[index] * 1000);
289 static ssize_t tcrit2_store(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
292 struct lm95234_data *data = dev_get_drvdata(dev);
293 int index = to_sensor_dev_attr(attr)->index;
295 int ret = lm95234_update_device(data);
300 ret = kstrtol(buf, 10, &val);
304 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127);
306 mutex_lock(&data->update_lock);
307 data->tcrit2[index] = val;
308 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val);
309 mutex_unlock(&data->update_lock);
314 static ssize_t tcrit2_hyst_show(struct device *dev,
315 struct device_attribute *attr, char *buf)
317 struct lm95234_data *data = dev_get_drvdata(dev);
318 int index = to_sensor_dev_attr(attr)->index;
319 int ret = lm95234_update_device(data);
324 /* Result can be negative, so be careful with unsigned operands */
325 return sprintf(buf, "%d",
326 ((int)data->tcrit2[index] - (int)data->thyst) * 1000);
329 static ssize_t tcrit1_show(struct device *dev, struct device_attribute *attr,
332 struct lm95234_data *data = dev_get_drvdata(dev);
333 int index = to_sensor_dev_attr(attr)->index;
335 return sprintf(buf, "%u", data->tcrit1[index] * 1000);
338 static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr,
339 const char *buf, size_t count)
341 struct lm95234_data *data = dev_get_drvdata(dev);
342 int index = to_sensor_dev_attr(attr)->index;
343 int ret = lm95234_update_device(data);
349 ret = kstrtol(buf, 10, &val);
353 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
355 mutex_lock(&data->update_lock);
356 data->tcrit1[index] = val;
357 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val);
358 mutex_unlock(&data->update_lock);
363 static ssize_t tcrit1_hyst_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 struct lm95234_data *data = dev_get_drvdata(dev);
367 int index = to_sensor_dev_attr(attr)->index;
368 int ret = lm95234_update_device(data);
373 /* Result can be negative, so be careful with unsigned operands */
374 return sprintf(buf, "%d",
375 ((int)data->tcrit1[index] - (int)data->thyst) * 1000);
378 static ssize_t tcrit1_hyst_store(struct device *dev,
379 struct device_attribute *attr,
380 const char *buf, size_t count)
382 struct lm95234_data *data = dev_get_drvdata(dev);
383 int index = to_sensor_dev_attr(attr)->index;
384 int ret = lm95234_update_device(data);
390 ret = kstrtol(buf, 10, &val);
394 val = DIV_ROUND_CLOSEST(val, 1000);
395 val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
397 mutex_lock(&data->update_lock);
399 i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val);
400 mutex_unlock(&data->update_lock);
405 static ssize_t offset_show(struct device *dev, struct device_attribute *attr,
408 struct lm95234_data *data = dev_get_drvdata(dev);
409 int index = to_sensor_dev_attr(attr)->index;
410 int ret = lm95234_update_device(data);
415 return sprintf(buf, "%d", data->toffset[index] * 500);
418 static ssize_t offset_store(struct device *dev, struct device_attribute *attr,
419 const char *buf, size_t count)
421 struct lm95234_data *data = dev_get_drvdata(dev);
422 int index = to_sensor_dev_attr(attr)->index;
423 int ret = lm95234_update_device(data);
429 ret = kstrtol(buf, 10, &val);
433 /* Accuracy is 1/2 degrees C */
434 val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127);
436 mutex_lock(&data->update_lock);
437 data->toffset[index] = val;
438 i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val);
439 mutex_unlock(&data->update_lock);
444 static ssize_t update_interval_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct lm95234_data *data = dev_get_drvdata(dev);
448 int ret = lm95234_update_device(data);
453 return sprintf(buf, "%lu\n",
454 DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
457 static ssize_t update_interval_store(struct device *dev,
458 struct device_attribute *attr,
459 const char *buf, size_t count)
461 struct lm95234_data *data = dev_get_drvdata(dev);
462 int ret = lm95234_update_device(data);
469 ret = kstrtoul(buf, 10, &val);
473 for (regval = 0; regval < 3; regval++) {
474 if (val <= update_intervals[regval])
478 mutex_lock(&data->update_lock);
479 data->interval = msecs_to_jiffies(update_intervals[regval]);
480 i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval);
481 mutex_unlock(&data->update_lock);
486 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
487 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
488 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
489 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
490 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
492 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, BIT(0) | BIT(1));
493 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, BIT(2) | BIT(3));
494 static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, BIT(4) | BIT(5));
495 static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, BIT(6) | BIT(7));
497 static SENSOR_DEVICE_ATTR_RW(temp2_type, type, BIT(1));
498 static SENSOR_DEVICE_ATTR_RW(temp3_type, type, BIT(2));
499 static SENSOR_DEVICE_ATTR_RW(temp4_type, type, BIT(3));
500 static SENSOR_DEVICE_ATTR_RW(temp5_type, type, BIT(4));
502 static SENSOR_DEVICE_ATTR_RW(temp1_max, tcrit1, 0);
503 static SENSOR_DEVICE_ATTR_RW(temp2_max, tcrit2, 0);
504 static SENSOR_DEVICE_ATTR_RW(temp3_max, tcrit2, 1);
505 static SENSOR_DEVICE_ATTR_RW(temp4_max, tcrit1, 3);
506 static SENSOR_DEVICE_ATTR_RW(temp5_max, tcrit1, 4);
508 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, tcrit1_hyst, 0);
509 static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, tcrit2_hyst, 0);
510 static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, tcrit2_hyst, 1);
511 static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, tcrit1_hyst, 3);
512 static SENSOR_DEVICE_ATTR_RO(temp5_max_hyst, tcrit1_hyst, 4);
514 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, BIT(0 + 8));
515 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, BIT(1 + 16));
516 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, BIT(2 + 16));
517 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, BIT(3 + 8));
518 static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, BIT(4 + 8));
520 static SENSOR_DEVICE_ATTR_RW(temp2_crit, tcrit1, 1);
521 static SENSOR_DEVICE_ATTR_RW(temp3_crit, tcrit1, 2);
523 static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, tcrit1_hyst, 1);
524 static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, tcrit1_hyst, 2);
526 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, BIT(1 + 8));
527 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, BIT(2 + 8));
529 static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 0);
530 static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 1);
531 static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 2);
532 static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 3);
534 static DEVICE_ATTR_RW(update_interval);
536 static struct attribute *lm95234_common_attrs[] = {
537 &sensor_dev_attr_temp1_input.dev_attr.attr,
538 &sensor_dev_attr_temp2_input.dev_attr.attr,
539 &sensor_dev_attr_temp3_input.dev_attr.attr,
540 &sensor_dev_attr_temp2_fault.dev_attr.attr,
541 &sensor_dev_attr_temp3_fault.dev_attr.attr,
542 &sensor_dev_attr_temp2_type.dev_attr.attr,
543 &sensor_dev_attr_temp3_type.dev_attr.attr,
544 &sensor_dev_attr_temp1_max.dev_attr.attr,
545 &sensor_dev_attr_temp2_max.dev_attr.attr,
546 &sensor_dev_attr_temp3_max.dev_attr.attr,
547 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
548 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
549 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
550 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
551 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
552 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
553 &sensor_dev_attr_temp2_crit.dev_attr.attr,
554 &sensor_dev_attr_temp3_crit.dev_attr.attr,
555 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
556 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
557 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
558 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
559 &sensor_dev_attr_temp2_offset.dev_attr.attr,
560 &sensor_dev_attr_temp3_offset.dev_attr.attr,
561 &dev_attr_update_interval.attr,
565 static const struct attribute_group lm95234_common_group = {
566 .attrs = lm95234_common_attrs,
569 static struct attribute *lm95234_attrs[] = {
570 &sensor_dev_attr_temp4_input.dev_attr.attr,
571 &sensor_dev_attr_temp5_input.dev_attr.attr,
572 &sensor_dev_attr_temp4_fault.dev_attr.attr,
573 &sensor_dev_attr_temp5_fault.dev_attr.attr,
574 &sensor_dev_attr_temp4_type.dev_attr.attr,
575 &sensor_dev_attr_temp5_type.dev_attr.attr,
576 &sensor_dev_attr_temp4_max.dev_attr.attr,
577 &sensor_dev_attr_temp5_max.dev_attr.attr,
578 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
579 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
580 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
581 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
582 &sensor_dev_attr_temp4_offset.dev_attr.attr,
583 &sensor_dev_attr_temp5_offset.dev_attr.attr,
587 static const struct attribute_group lm95234_group = {
588 .attrs = lm95234_attrs,
591 static int lm95234_detect(struct i2c_client *client,
592 struct i2c_board_info *info)
594 struct i2c_adapter *adapter = client->adapter;
595 int address = client->addr;
596 u8 config_mask, model_mask;
597 int mfg_id, chip_id, val;
600 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
603 mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
604 if (mfg_id != NATSEMI_MAN_ID)
607 chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
609 case LM95233_CHIP_ID:
610 if (address != 0x18 && address != 0x2a && address != 0x2b)
616 case LM95234_CHIP_ID:
617 if (address != 0x18 && address != 0x4d && address != 0x4e)
627 val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
631 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
632 if (val & config_mask)
635 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
639 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
640 if (val & model_mask)
643 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
644 if (val & model_mask)
647 strscpy(info->type, name, I2C_NAME_SIZE);
651 static int lm95234_init_client(struct i2c_client *client)
655 /* start conversion if necessary */
656 val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
660 i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
663 /* If diode type status reports an error, try to fix it */
664 val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
667 model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
671 dev_notice(&client->dev,
672 "Fixing remote diode type misconfiguration (0x%x)\n",
674 i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
680 static const struct i2c_device_id lm95234_id[];
682 static int lm95234_probe(struct i2c_client *client)
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 data->groups[0] = &lm95234_common_group;
702 if (i2c_match_id(lm95234_id, client)->driver_data == lm95234)
703 data->groups[1] = &lm95234_group;
705 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
707 return PTR_ERR_OR_ZERO(hwmon_dev);
710 /* Driver data (common to all clients) */
711 static const struct i2c_device_id lm95234_id[] = {
712 { "lm95233", lm95233 },
713 { "lm95234", lm95234 },
716 MODULE_DEVICE_TABLE(i2c, lm95234_id);
718 static struct i2c_driver lm95234_driver = {
719 .class = I2C_CLASS_HWMON,
723 .probe_new = lm95234_probe,
724 .id_table = lm95234_id,
725 .detect = lm95234_detect,
726 .address_list = normal_i2c,
729 module_i2c_driver(lm95234_driver);
731 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
732 MODULE_DESCRIPTION("LM95233/LM95234 sensor driver");
733 MODULE_LICENSE("GPL");