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;
96 unsigned long last_update, last_alert_update;
98 bool min_alert, max_alert, therm_trip;
99 bool data_valid, alert_valid;
100 bool notify_max, notify_min;
104 * These functions converts temperature from HW format to integer format and
105 * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
107 static int stts751_to_deg(s16 hw_val)
109 return hw_val * 125 / 32;
112 static s32 stts751_to_hw(int val)
114 return DIV_ROUND_CLOSEST(val, 125) * 32;
117 static int stts751_adjust_resolution(struct stts751_priv *priv)
121 switch (priv->interval) {
136 if (priv->res == res)
139 priv->config &= ~STTS751_CONF_RES_MASK;
140 priv->config |= res << STTS751_CONF_RES_SHIFT;
141 dev_dbg(&priv->client->dev, "setting res %d. config %x",
145 return i2c_smbus_write_byte_data(priv->client,
146 STTS751_REG_CONF, priv->config);
149 static int stts751_update_temp(struct stts751_priv *priv)
151 s32 integer1, integer2, frac;
154 * There is a trick here, like in the lm90 driver. We have to read two
155 * registers to get the sensor temperature, but we have to beware a
156 * conversion could occur between the readings. We could use the
157 * one-shot conversion register, but we don't want to do this (disables
158 * hardware monitoring). So the solution used here is to read the high
159 * byte once, then the low byte, then the high byte again. If the new
160 * high byte matches the old one, then we have a valid reading. Else we
161 * have to read the low byte again, and now we believe we have a correct
164 integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
166 dev_dbg(&priv->client->dev,
167 "I2C read failed (temp H). ret: %x\n", integer1);
171 frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L);
173 dev_dbg(&priv->client->dev,
174 "I2C read failed (temp L). ret: %x\n", frac);
178 integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
180 dev_dbg(&priv->client->dev,
181 "I2C 2nd read failed (temp H). ret: %x\n", integer2);
185 if (integer1 != integer2) {
186 frac = i2c_smbus_read_byte_data(priv->client,
189 dev_dbg(&priv->client->dev,
190 "I2C 2nd read failed (temp L). ret: %x\n",
196 priv->temp = stts751_to_deg((integer1 << 8) | frac);
200 static int stts751_set_temp_reg16(struct stts751_priv *priv, int temp,
206 hwval = stts751_to_hw(temp);
208 ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8);
212 return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff);
215 static int stts751_set_temp_reg8(struct stts751_priv *priv, int temp, u8 reg)
219 hwval = stts751_to_hw(temp);
220 return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8);
223 static int stts751_read_reg16(struct stts751_priv *priv, int *temp,
228 integer = i2c_smbus_read_byte_data(priv->client, hreg);
232 frac = i2c_smbus_read_byte_data(priv->client, lreg);
236 *temp = stts751_to_deg((integer << 8) | frac);
241 static int stts751_read_reg8(struct stts751_priv *priv, int *temp, u8 reg)
245 integer = i2c_smbus_read_byte_data(priv->client, reg);
249 *temp = stts751_to_deg(integer << 8);
255 * Update alert flags without waiting for cache to expire. We detects alerts
256 * immediately for the sake of the alert handler; we still need to deal with
257 * caching to workaround the fact that alarm flags int the status register,
258 * despite what the datasheet claims, gets always cleared on read.
260 static int stts751_update_alert(struct stts751_priv *priv)
264 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
267 * Add another 10% because if we run faster than the HW conversion
268 * rate we will end up in reporting incorrectly alarms.
270 cache_time += cache_time / 10;
272 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS);
276 dev_dbg(&priv->client->dev, "status reg %x\n", ret);
277 conv_done = ret & (STTS751_STATUS_TRIPH | STTS751_STATUS_TRIPL);
279 * Reset the cache if the cache time expired, or if we are sure
280 * we have valid data from a device conversion, or if we know
281 * our cache has been never written.
283 * Note that when the cache has been never written the point is
284 * to correctly initialize the timestamp, rather than clearing
287 * Note that updating the cache timestamp when we get an alarm flag
288 * is required, otherwise we could incorrectly report alarms to be zero.
290 if (time_after(jiffies, priv->last_alert_update + cache_time) ||
291 conv_done || !priv->alert_valid) {
292 priv->max_alert = false;
293 priv->min_alert = false;
294 priv->alert_valid = true;
295 priv->last_alert_update = jiffies;
296 dev_dbg(&priv->client->dev, "invalidating alert cache\n");
299 priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH);
300 priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL);
301 priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT);
303 dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n",
304 priv->max_alert, priv->min_alert, priv->therm_trip);
309 static void stts751_alert(struct i2c_client *client,
310 enum i2c_alert_protocol type, unsigned int data)
313 struct stts751_priv *priv = i2c_get_clientdata(client);
315 if (type != I2C_PROTOCOL_SMBUS_ALERT)
318 dev_dbg(&client->dev, "alert!");
320 mutex_lock(&priv->access_lock);
321 ret = stts751_update_alert(priv);
323 /* default to worst case */
324 priv->max_alert = true;
325 priv->min_alert = true;
328 "Alert received, but can't communicate to the device. Triggering all alarms!");
331 if (priv->max_alert) {
332 if (priv->notify_max)
333 dev_notice(priv->dev, "got alert for HIGH temperature");
334 priv->notify_max = false;
336 /* unblock alert poll */
337 sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm");
340 if (priv->min_alert) {
341 if (priv->notify_min)
342 dev_notice(priv->dev, "got alert for LOW temperature");
343 priv->notify_min = false;
345 /* unblock alert poll */
346 sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm");
349 if (priv->min_alert || priv->max_alert)
350 kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE);
352 mutex_unlock(&priv->access_lock);
355 static int stts751_update(struct stts751_priv *priv)
358 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
360 if (time_after(jiffies, priv->last_update + cache_time) ||
362 ret = stts751_update_temp(priv);
366 ret = stts751_update_alert(priv);
369 priv->data_valid = true;
370 priv->last_update = jiffies;
376 static ssize_t max_alarm_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
380 struct stts751_priv *priv = dev_get_drvdata(dev);
382 mutex_lock(&priv->access_lock);
383 ret = stts751_update(priv);
385 priv->notify_max = true;
386 mutex_unlock(&priv->access_lock);
390 return sysfs_emit(buf, "%d\n", priv->max_alert);
393 static ssize_t min_alarm_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
397 struct stts751_priv *priv = dev_get_drvdata(dev);
399 mutex_lock(&priv->access_lock);
400 ret = stts751_update(priv);
402 priv->notify_min = true;
403 mutex_unlock(&priv->access_lock);
407 return sysfs_emit(buf, "%d\n", priv->min_alert);
410 static ssize_t input_show(struct device *dev, struct device_attribute *attr,
414 struct stts751_priv *priv = dev_get_drvdata(dev);
416 mutex_lock(&priv->access_lock);
417 ret = stts751_update(priv);
418 mutex_unlock(&priv->access_lock);
422 return sysfs_emit(buf, "%d\n", priv->temp);
425 static ssize_t therm_show(struct device *dev, struct device_attribute *attr,
428 struct stts751_priv *priv = dev_get_drvdata(dev);
430 return sysfs_emit(buf, "%d\n", priv->therm);
433 static ssize_t therm_store(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
438 struct stts751_priv *priv = dev_get_drvdata(dev);
440 if (kstrtol(buf, 10, &temp) < 0)
443 /* HW works in range -64C to +127.937C */
444 temp = clamp_val(temp, -64000, 127937);
445 mutex_lock(&priv->access_lock);
446 ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_TLIM);
450 dev_dbg(&priv->client->dev, "setting therm %ld", temp);
453 * hysteresis reg is relative to therm, so the HW does not need to be
454 * adjusted, we need to update our local copy only.
456 priv->hyst = temp - (priv->therm - priv->hyst);
460 mutex_unlock(&priv->access_lock);
467 static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
470 struct stts751_priv *priv = dev_get_drvdata(dev);
472 return sysfs_emit(buf, "%d\n", priv->hyst);
475 static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
481 struct stts751_priv *priv = dev_get_drvdata(dev);
483 if (kstrtol(buf, 10, &temp) < 0)
486 mutex_lock(&priv->access_lock);
487 /* HW works in range -64C to +127.937C */
488 temp = clamp_val(temp, -64000, priv->therm);
490 dev_dbg(&priv->client->dev, "setting hyst %ld", temp);
491 temp = priv->therm - temp;
492 ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_HYST);
493 mutex_unlock(&priv->access_lock);
500 static ssize_t therm_trip_show(struct device *dev,
501 struct device_attribute *attr, char *buf)
504 struct stts751_priv *priv = dev_get_drvdata(dev);
506 mutex_lock(&priv->access_lock);
507 ret = stts751_update(priv);
508 mutex_unlock(&priv->access_lock);
512 return sysfs_emit(buf, "%d\n", priv->therm_trip);
515 static ssize_t max_show(struct device *dev, struct device_attribute *attr,
518 struct stts751_priv *priv = dev_get_drvdata(dev);
520 return sysfs_emit(buf, "%d\n", priv->event_max);
523 static ssize_t max_store(struct device *dev, struct device_attribute *attr,
524 const char *buf, size_t count)
528 struct stts751_priv *priv = dev_get_drvdata(dev);
530 if (kstrtol(buf, 10, &temp) < 0)
533 mutex_lock(&priv->access_lock);
534 /* HW works in range -64C to +127.937C */
535 temp = clamp_val(temp, priv->event_min, 127937);
536 ret = stts751_set_temp_reg16(priv, temp,
537 STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
541 dev_dbg(&priv->client->dev, "setting event max %ld", temp);
542 priv->event_max = temp;
545 mutex_unlock(&priv->access_lock);
549 static ssize_t min_show(struct device *dev, struct device_attribute *attr,
552 struct stts751_priv *priv = dev_get_drvdata(dev);
554 return sysfs_emit(buf, "%d\n", priv->event_min);
557 static ssize_t min_store(struct device *dev, struct device_attribute *attr,
558 const char *buf, size_t count)
562 struct stts751_priv *priv = dev_get_drvdata(dev);
564 if (kstrtol(buf, 10, &temp) < 0)
567 mutex_lock(&priv->access_lock);
568 /* HW works in range -64C to +127.937C */
569 temp = clamp_val(temp, -64000, priv->event_max);
570 ret = stts751_set_temp_reg16(priv, temp,
571 STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
575 dev_dbg(&priv->client->dev, "setting event min %ld", temp);
576 priv->event_min = temp;
579 mutex_unlock(&priv->access_lock);
583 static ssize_t interval_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct stts751_priv *priv = dev_get_drvdata(dev);
588 return sysfs_emit(buf, "%d\n",
589 stts751_intervals[priv->interval]);
592 static ssize_t interval_store(struct device *dev,
593 struct device_attribute *attr, const char *buf,
599 struct stts751_priv *priv = dev_get_drvdata(dev);
601 if (kstrtoul(buf, 10, &val) < 0)
604 idx = find_closest_descending(val, stts751_intervals,
605 ARRAY_SIZE(stts751_intervals));
607 dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d",
608 val, idx, stts751_intervals[idx]);
610 mutex_lock(&priv->access_lock);
611 if (priv->interval == idx)
615 * In early development stages I've become suspicious about the chip
616 * starting to misbehave if I ever set, even briefly, an invalid
617 * configuration. While I'm not sure this is really needed, be
618 * conservative and set rate/resolution in such an order that avoids
619 * passing through an invalid configuration.
622 /* speed up: lower the resolution, then modify convrate */
623 if (priv->interval < idx) {
624 dev_dbg(&priv->client->dev, "lower resolution, then modify convrate");
625 priv->interval = idx;
626 ret = stts751_adjust_resolution(priv);
631 ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx);
634 /* slow down: modify convrate, then raise resolution */
635 if (priv->interval != idx) {
636 dev_dbg(&priv->client->dev, "modify convrate, then raise resolution");
637 priv->interval = idx;
638 ret = stts751_adjust_resolution(priv);
644 mutex_unlock(&priv->access_lock);
649 static int stts751_detect(struct i2c_client *new_client,
650 struct i2c_board_info *info)
652 struct i2c_adapter *adapter = new_client->adapter;
656 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
659 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_MAN_ID);
660 if (tmp != ST_MAN_ID)
663 /* lower temperaure registers always have bits 0-3 set to zero */
664 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_TEMP_L);
668 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_HLIM_L);
672 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_LLIM_L);
676 /* smbus timeout register always have bits 0-7 set to zero */
677 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_SMBUS_TO);
681 tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_PROD_ID);
684 case STTS751_0_PROD_ID:
687 case STTS751_1_PROD_ID:
693 dev_dbg(&new_client->dev, "Chip %s detected", name);
695 strscpy(info->type, stts751_id[0].name, I2C_NAME_SIZE);
699 static int stts751_read_chip_config(struct stts751_priv *priv)
704 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF);
708 priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT;
710 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE);
713 if (ret >= ARRAY_SIZE(stts751_intervals)) {
714 dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret);
717 priv->interval = ret;
719 ret = stts751_read_reg16(priv, &priv->event_max,
720 STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
724 ret = stts751_read_reg16(priv, &priv->event_min,
725 STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
729 ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM);
733 ret = stts751_read_reg8(priv, &tmp, STTS751_REG_HYST);
736 priv->hyst = priv->therm - tmp;
741 static SENSOR_DEVICE_ATTR_RO(temp1_input, input, 0);
742 static SENSOR_DEVICE_ATTR_RW(temp1_min, min, 0);
743 static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
744 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, min_alarm, 0);
745 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, max_alarm, 0);
746 static SENSOR_DEVICE_ATTR_RW(temp1_crit, therm, 0);
747 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0);
748 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, therm_trip, 0);
749 static SENSOR_DEVICE_ATTR_RW(update_interval, interval, 0);
751 static struct attribute *stts751_attrs[] = {
752 &sensor_dev_attr_temp1_input.dev_attr.attr,
753 &sensor_dev_attr_temp1_min.dev_attr.attr,
754 &sensor_dev_attr_temp1_max.dev_attr.attr,
755 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
756 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
757 &sensor_dev_attr_temp1_crit.dev_attr.attr,
758 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
759 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
760 &sensor_dev_attr_update_interval.dev_attr.attr,
763 ATTRIBUTE_GROUPS(stts751);
765 static int stts751_probe(struct i2c_client *client)
767 struct stts751_priv *priv;
772 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
776 priv->client = client;
777 priv->notify_max = true;
778 priv->notify_min = true;
779 i2c_set_clientdata(client, priv);
780 mutex_init(&priv->access_lock);
782 if (device_property_present(&client->dev,
783 "smbus-timeout-disable")) {
784 smbus_nto = device_property_read_bool(&client->dev,
785 "smbus-timeout-disable");
787 ret = i2c_smbus_write_byte_data(client, STTS751_REG_SMBUS_TO,
788 smbus_nto ? 0 : 0x80);
793 rev_id = i2c_smbus_read_byte_data(client, STTS751_REG_REV_ID);
797 dev_dbg(&client->dev, "Chip revision 0x%x is untested\n",
801 ret = stts751_read_chip_config(priv);
805 priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS);
806 ret = i2c_smbus_write_byte_data(client, STTS751_REG_CONF, priv->config);
810 priv->dev = devm_hwmon_device_register_with_groups(&client->dev,
813 return PTR_ERR_OR_ZERO(priv->dev);
816 MODULE_DEVICE_TABLE(i2c, stts751_id);
818 static struct i2c_driver stts751_driver = {
819 .class = I2C_CLASS_HWMON,
822 .of_match_table = of_match_ptr(stts751_of_match),
824 .probe = stts751_probe,
825 .id_table = stts751_id,
826 .detect = stts751_detect,
827 .alert = stts751_alert,
828 .address_list = normal_i2c,
831 module_i2c_driver(stts751_driver);
833 MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
834 MODULE_DESCRIPTION("STTS751 sensor driver");
835 MODULE_LICENSE("GPL");