1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STTS751 sensor driver
5 * Copyright (C) 2016-2017 Istituto Italiano di Tecnologia - RBCS - EDL
6 * Robotics, Brain and Cognitive Sciences department
7 * Electronic Design Laboratory
9 * Written by Andrea Merello <andrea.merello@gmail.com>
11 * Based on LM95241 driver and LM90 driver
14 #include <linux/bitops.h>
15 #include <linux/err.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/i2c.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/jiffies.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/property.h>
25 #include <linux/slab.h>
26 #include <linux/sysfs.h>
27 #include <linux/util_macros.h>
29 #define DEVNAME "stts751"
31 static const unsigned short normal_i2c[] = {
32 0x48, 0x49, 0x38, 0x39, /* STTS751-0 */
33 0x4A, 0x4B, 0x3A, 0x3B, /* STTS751-1 */
36 #define STTS751_REG_TEMP_H 0x00
37 #define STTS751_REG_STATUS 0x01
38 #define STTS751_STATUS_TRIPT BIT(0)
39 #define STTS751_STATUS_TRIPL BIT(5)
40 #define STTS751_STATUS_TRIPH BIT(6)
41 #define STTS751_REG_TEMP_L 0x02
42 #define STTS751_REG_CONF 0x03
43 #define STTS751_CONF_RES_MASK 0x0C
44 #define STTS751_CONF_RES_SHIFT 2
45 #define STTS751_CONF_EVENT_DIS BIT(7)
46 #define STTS751_CONF_STOP BIT(6)
47 #define STTS751_REG_RATE 0x04
48 #define STTS751_REG_HLIM_H 0x05
49 #define STTS751_REG_HLIM_L 0x06
50 #define STTS751_REG_LLIM_H 0x07
51 #define STTS751_REG_LLIM_L 0x08
52 #define STTS751_REG_TLIM 0x20
53 #define STTS751_REG_HYST 0x21
54 #define STTS751_REG_SMBUS_TO 0x22
56 #define STTS751_REG_PROD_ID 0xFD
57 #define STTS751_REG_MAN_ID 0xFE
58 #define STTS751_REG_REV_ID 0xFF
60 #define STTS751_0_PROD_ID 0x00
61 #define STTS751_1_PROD_ID 0x01
62 #define ST_MAN_ID 0x53
65 * Possible update intervals are (in mS):
66 * 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62.5, 31.25
67 * However we are not going to complicate things too much and we stick to the
70 static const int stts751_intervals[] = {
71 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 31
74 static const struct i2c_device_id stts751_id[] = {
79 static const struct of_device_id __maybe_unused stts751_of_match[] = {
80 { .compatible = "stts751" },
83 MODULE_DEVICE_TABLE(of, stts751_of_match);
87 struct i2c_client *client;
88 struct mutex access_lock;
91 int event_max, event_min;
95 unsigned long last_update, last_alert_update;
97 bool min_alert, max_alert, therm_trip;
98 bool data_valid, alert_valid;
99 bool notify_max, notify_min;
103 * These functions converts temperature from HW format to integer format and
104 * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
106 static int stts751_to_deg(s16 hw_val)
108 return hw_val * 125 / 32;
111 static s32 stts751_to_hw(int val)
113 return DIV_ROUND_CLOSEST(val, 125) * 32;
116 static int stts751_adjust_resolution(struct stts751_priv *priv)
120 switch (priv->interval) {
135 if (priv->res == res)
138 priv->config &= ~STTS751_CONF_RES_MASK;
139 priv->config |= res << STTS751_CONF_RES_SHIFT;
140 dev_dbg(&priv->client->dev, "setting res %d. config %x",
144 return i2c_smbus_write_byte_data(priv->client,
145 STTS751_REG_CONF, priv->config);
148 static int stts751_update_temp(struct stts751_priv *priv)
150 s32 integer1, integer2, frac;
153 * There is a trick here, like in the lm90 driver. We have to read two
154 * registers to get the sensor temperature, but we have to beware a
155 * conversion could occur between the readings. We could use the
156 * one-shot conversion register, but we don't want to do this (disables
157 * hardware monitoring). So the solution used here is to read the high
158 * byte once, then the low byte, then the high byte again. If the new
159 * high byte matches the old one, then we have a valid reading. Else we
160 * have to read the low byte again, and now we believe we have a correct
163 integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
165 dev_dbg(&priv->client->dev,
166 "I2C read failed (temp H). ret: %x\n", integer1);
170 frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L);
172 dev_dbg(&priv->client->dev,
173 "I2C read failed (temp L). ret: %x\n", frac);
177 integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
179 dev_dbg(&priv->client->dev,
180 "I2C 2nd read failed (temp H). ret: %x\n", integer2);
184 if (integer1 != integer2) {
185 frac = i2c_smbus_read_byte_data(priv->client,
188 dev_dbg(&priv->client->dev,
189 "I2C 2nd read failed (temp L). ret: %x\n",
195 priv->temp = stts751_to_deg((integer1 << 8) | frac);
199 static int stts751_set_temp_reg16(struct stts751_priv *priv, int temp,
205 hwval = stts751_to_hw(temp);
207 ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8);
211 return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff);
214 static int stts751_set_temp_reg8(struct stts751_priv *priv, int temp, u8 reg)
218 hwval = stts751_to_hw(temp);
219 return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8);
222 static int stts751_read_reg16(struct stts751_priv *priv, int *temp,
227 integer = i2c_smbus_read_byte_data(priv->client, hreg);
231 frac = i2c_smbus_read_byte_data(priv->client, lreg);
235 *temp = stts751_to_deg((integer << 8) | frac);
240 static int stts751_read_reg8(struct stts751_priv *priv, int *temp, u8 reg)
244 integer = i2c_smbus_read_byte_data(priv->client, reg);
248 *temp = stts751_to_deg(integer << 8);
254 * Update alert flags without waiting for cache to expire. We detects alerts
255 * immediately for the sake of the alert handler; we still need to deal with
256 * caching to workaround the fact that alarm flags int the status register,
257 * despite what the datasheet claims, gets always cleared on read.
259 static int stts751_update_alert(struct stts751_priv *priv)
263 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
266 * Add another 10% because if we run faster than the HW conversion
267 * rate we will end up in reporting incorrectly alarms.
269 cache_time += cache_time / 10;
271 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS);
275 dev_dbg(&priv->client->dev, "status reg %x\n", ret);
276 conv_done = ret & (STTS751_STATUS_TRIPH | STTS751_STATUS_TRIPL);
278 * Reset the cache if the cache time expired, or if we are sure
279 * we have valid data from a device conversion, or if we know
280 * our cache has been never written.
282 * Note that when the cache has been never written the point is
283 * to correctly initialize the timestamp, rather than clearing
286 * Note that updating the cache timestamp when we get an alarm flag
287 * is required, otherwise we could incorrectly report alarms to be zero.
289 if (time_after(jiffies, priv->last_alert_update + cache_time) ||
290 conv_done || !priv->alert_valid) {
291 priv->max_alert = false;
292 priv->min_alert = false;
293 priv->alert_valid = true;
294 priv->last_alert_update = jiffies;
295 dev_dbg(&priv->client->dev, "invalidating alert cache\n");
298 priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH);
299 priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL);
300 priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT);
302 dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n",
303 priv->max_alert, priv->min_alert, priv->therm_trip);
308 static void stts751_alert(struct i2c_client *client,
309 enum i2c_alert_protocol type, unsigned int data)
312 struct stts751_priv *priv = i2c_get_clientdata(client);
314 if (type != I2C_PROTOCOL_SMBUS_ALERT)
317 dev_dbg(&client->dev, "alert!");
319 mutex_lock(&priv->access_lock);
320 ret = stts751_update_alert(priv);
322 /* default to worst case */
323 priv->max_alert = true;
324 priv->min_alert = true;
327 "Alert received, but can't communicate to the device. Triggering all alarms!");
330 if (priv->max_alert) {
331 if (priv->notify_max)
332 dev_notice(priv->dev, "got alert for HIGH temperature");
333 priv->notify_max = false;
335 /* unblock alert poll */
336 sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm");
339 if (priv->min_alert) {
340 if (priv->notify_min)
341 dev_notice(priv->dev, "got alert for LOW temperature");
342 priv->notify_min = false;
344 /* unblock alert poll */
345 sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm");
348 if (priv->min_alert || priv->max_alert)
349 kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE);
351 mutex_unlock(&priv->access_lock);
354 static int stts751_update(struct stts751_priv *priv)
357 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
359 if (time_after(jiffies, priv->last_update + cache_time) ||
361 ret = stts751_update_temp(priv);
365 ret = stts751_update_alert(priv);
368 priv->data_valid = true;
369 priv->last_update = jiffies;
375 static ssize_t max_alarm_show(struct device *dev,
376 struct device_attribute *attr, char *buf)
379 struct stts751_priv *priv = dev_get_drvdata(dev);
381 mutex_lock(&priv->access_lock);
382 ret = stts751_update(priv);
384 priv->notify_max = true;
385 mutex_unlock(&priv->access_lock);
389 return sysfs_emit(buf, "%d\n", priv->max_alert);
392 static ssize_t min_alarm_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
396 struct stts751_priv *priv = dev_get_drvdata(dev);
398 mutex_lock(&priv->access_lock);
399 ret = stts751_update(priv);
401 priv->notify_min = true;
402 mutex_unlock(&priv->access_lock);
406 return sysfs_emit(buf, "%d\n", priv->min_alert);
409 static ssize_t input_show(struct device *dev, struct device_attribute *attr,
413 struct stts751_priv *priv = dev_get_drvdata(dev);
415 mutex_lock(&priv->access_lock);
416 ret = stts751_update(priv);
417 mutex_unlock(&priv->access_lock);
421 return sysfs_emit(buf, "%d\n", priv->temp);
424 static ssize_t therm_show(struct device *dev, struct device_attribute *attr,
427 struct stts751_priv *priv = dev_get_drvdata(dev);
429 return sysfs_emit(buf, "%d\n", priv->therm);
432 static ssize_t therm_store(struct device *dev, struct device_attribute *attr,
433 const char *buf, size_t count)
437 struct stts751_priv *priv = dev_get_drvdata(dev);
439 if (kstrtol(buf, 10, &temp) < 0)
442 /* HW works in range -64C to +127.937C */
443 temp = clamp_val(temp, -64000, 127937);
444 mutex_lock(&priv->access_lock);
445 ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_TLIM);
449 dev_dbg(&priv->client->dev, "setting therm %ld", temp);
452 * hysteresis reg is relative to therm, so the HW does not need to be
453 * adjusted, we need to update our local copy only.
455 priv->hyst = temp - (priv->therm - priv->hyst);
459 mutex_unlock(&priv->access_lock);
466 static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
469 struct stts751_priv *priv = dev_get_drvdata(dev);
471 return sysfs_emit(buf, "%d\n", priv->hyst);
474 static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
475 const char *buf, size_t count)
480 struct stts751_priv *priv = dev_get_drvdata(dev);
482 if (kstrtol(buf, 10, &temp) < 0)
485 mutex_lock(&priv->access_lock);
486 /* HW works in range -64C to +127.937C */
487 temp = clamp_val(temp, -64000, priv->therm);
489 dev_dbg(&priv->client->dev, "setting hyst %ld", temp);
490 temp = priv->therm - temp;
491 ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_HYST);
492 mutex_unlock(&priv->access_lock);
499 static ssize_t therm_trip_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
503 struct stts751_priv *priv = dev_get_drvdata(dev);
505 mutex_lock(&priv->access_lock);
506 ret = stts751_update(priv);
507 mutex_unlock(&priv->access_lock);
511 return sysfs_emit(buf, "%d\n", priv->therm_trip);
514 static ssize_t max_show(struct device *dev, struct device_attribute *attr,
517 struct stts751_priv *priv = dev_get_drvdata(dev);
519 return sysfs_emit(buf, "%d\n", priv->event_max);
522 static ssize_t max_store(struct device *dev, struct device_attribute *attr,
523 const char *buf, size_t count)
527 struct stts751_priv *priv = dev_get_drvdata(dev);
529 if (kstrtol(buf, 10, &temp) < 0)
532 mutex_lock(&priv->access_lock);
533 /* HW works in range -64C to +127.937C */
534 temp = clamp_val(temp, priv->event_min, 127937);
535 ret = stts751_set_temp_reg16(priv, temp,
536 STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
540 dev_dbg(&priv->client->dev, "setting event max %ld", temp);
541 priv->event_max = temp;
544 mutex_unlock(&priv->access_lock);
548 static ssize_t min_show(struct device *dev, struct device_attribute *attr,
551 struct stts751_priv *priv = dev_get_drvdata(dev);
553 return sysfs_emit(buf, "%d\n", priv->event_min);
556 static ssize_t min_store(struct device *dev, struct device_attribute *attr,
557 const char *buf, size_t count)
561 struct stts751_priv *priv = dev_get_drvdata(dev);
563 if (kstrtol(buf, 10, &temp) < 0)
566 mutex_lock(&priv->access_lock);
567 /* HW works in range -64C to +127.937C */
568 temp = clamp_val(temp, -64000, priv->event_max);
569 ret = stts751_set_temp_reg16(priv, temp,
570 STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
574 dev_dbg(&priv->client->dev, "setting event min %ld", temp);
575 priv->event_min = temp;
578 mutex_unlock(&priv->access_lock);
582 static ssize_t interval_show(struct device *dev,
583 struct device_attribute *attr, char *buf)
585 struct stts751_priv *priv = dev_get_drvdata(dev);
587 return sysfs_emit(buf, "%d\n",
588 stts751_intervals[priv->interval]);
591 static ssize_t interval_store(struct device *dev,
592 struct device_attribute *attr, const char *buf,
598 struct stts751_priv *priv = dev_get_drvdata(dev);
600 if (kstrtoul(buf, 10, &val) < 0)
603 idx = find_closest_descending(val, stts751_intervals,
604 ARRAY_SIZE(stts751_intervals));
606 dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d",
607 val, idx, stts751_intervals[idx]);
609 mutex_lock(&priv->access_lock);
610 if (priv->interval == idx)
614 * In early development stages I've become suspicious about the chip
615 * starting to misbehave if I ever set, even briefly, an invalid
616 * configuration. While I'm not sure this is really needed, be
617 * conservative and set rate/resolution in such an order that avoids
618 * passing through an invalid configuration.
621 /* speed up: lower the resolution, then modify convrate */
622 if (priv->interval < idx) {
623 dev_dbg(&priv->client->dev, "lower resolution, then modify convrate");
624 priv->interval = idx;
625 ret = stts751_adjust_resolution(priv);
630 ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx);
633 /* slow down: modify convrate, then raise resolution */
634 if (priv->interval != idx) {
635 dev_dbg(&priv->client->dev, "modify convrate, then raise resolution");
636 priv->interval = idx;
637 ret = stts751_adjust_resolution(priv);
643 mutex_unlock(&priv->access_lock);
648 static int stts751_detect(struct i2c_client *new_client,
649 struct i2c_board_info *info)
651 struct i2c_adapter *adapter = new_client->adapter;
655 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
658 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_MAN_ID);
659 if (tmp != ST_MAN_ID)
662 /* lower temperaure registers always have bits 0-3 set to zero */
663 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_TEMP_L);
667 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_HLIM_L);
671 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_LLIM_L);
675 /* smbus timeout register always have bits 0-7 set to zero */
676 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_SMBUS_TO);
680 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_PROD_ID);
683 case STTS751_0_PROD_ID:
686 case STTS751_1_PROD_ID:
692 dev_dbg(&new_client->dev, "Chip %s detected", name);
694 strscpy(info->type, stts751_id[0].name, I2C_NAME_SIZE);
698 static int stts751_read_chip_config(struct stts751_priv *priv)
703 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF);
707 priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT;
709 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE);
712 if (ret >= ARRAY_SIZE(stts751_intervals)) {
713 dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret);
716 priv->interval = ret;
718 ret = stts751_read_reg16(priv, &priv->event_max,
719 STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
723 ret = stts751_read_reg16(priv, &priv->event_min,
724 STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
728 ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM);
732 ret = stts751_read_reg8(priv, &tmp, STTS751_REG_HYST);
735 priv->hyst = priv->therm - tmp;
740 static SENSOR_DEVICE_ATTR_RO(temp1_input, input, 0);
741 static SENSOR_DEVICE_ATTR_RW(temp1_min, min, 0);
742 static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
743 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, min_alarm, 0);
744 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, max_alarm, 0);
745 static SENSOR_DEVICE_ATTR_RW(temp1_crit, therm, 0);
746 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0);
747 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, therm_trip, 0);
748 static SENSOR_DEVICE_ATTR_RW(update_interval, interval, 0);
750 static struct attribute *stts751_attrs[] = {
751 &sensor_dev_attr_temp1_input.dev_attr.attr,
752 &sensor_dev_attr_temp1_min.dev_attr.attr,
753 &sensor_dev_attr_temp1_max.dev_attr.attr,
754 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
755 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
756 &sensor_dev_attr_temp1_crit.dev_attr.attr,
757 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
758 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
759 &sensor_dev_attr_update_interval.dev_attr.attr,
762 ATTRIBUTE_GROUPS(stts751);
764 static int stts751_probe(struct i2c_client *client)
766 struct stts751_priv *priv;
771 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
775 priv->client = client;
776 priv->notify_max = true;
777 priv->notify_min = true;
778 i2c_set_clientdata(client, priv);
779 mutex_init(&priv->access_lock);
781 if (device_property_present(&client->dev,
782 "smbus-timeout-disable")) {
783 smbus_nto = device_property_read_bool(&client->dev,
784 "smbus-timeout-disable");
786 ret = i2c_smbus_write_byte_data(client, STTS751_REG_SMBUS_TO,
787 smbus_nto ? 0 : 0x80);
792 rev_id = i2c_smbus_read_byte_data(client, STTS751_REG_REV_ID);
796 dev_dbg(&client->dev, "Chip revision 0x%x is untested\n",
800 ret = stts751_read_chip_config(priv);
804 priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS);
805 ret = i2c_smbus_write_byte_data(client, STTS751_REG_CONF, priv->config);
809 priv->dev = devm_hwmon_device_register_with_groups(&client->dev,
812 return PTR_ERR_OR_ZERO(priv->dev);
815 MODULE_DEVICE_TABLE(i2c, stts751_id);
817 static struct i2c_driver stts751_driver = {
818 .class = I2C_CLASS_HWMON,
821 .of_match_table = of_match_ptr(stts751_of_match),
823 .probe = stts751_probe,
824 .id_table = stts751_id,
825 .detect = stts751_detect,
826 .alert = stts751_alert,
827 .address_list = normal_i2c,
830 module_i2c_driver(stts751_driver);
832 MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
833 MODULE_DESCRIPTION("STTS751 sensor driver");
834 MODULE_LICENSE("GPL");