Lines Matching +full:update +full:- +full:interval
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2016-2017 Istituto Italiano di Tecnologia - RBCS - EDL
17 #include <linux/hwmon-sysfs.h>
32 0x48, 0x49, 0x38, 0x39, /* STTS751-0 */
33 0x4A, 0x4B, 0x3A, 0x3B, /* STTS751-1 */
65 * Possible update intervals are (in mS):
89 u8 interval; member
105 * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
121 switch (priv->interval) { in stts751_adjust_resolution()
136 if (priv->res == res) in stts751_adjust_resolution()
139 priv->config &= ~STTS751_CONF_RES_MASK; in stts751_adjust_resolution()
140 priv->config |= res << STTS751_CONF_RES_SHIFT; in stts751_adjust_resolution()
141 dev_dbg(&priv->client->dev, "setting res %d. config %x", in stts751_adjust_resolution()
142 res, priv->config); in stts751_adjust_resolution()
143 priv->res = res; in stts751_adjust_resolution()
145 return i2c_smbus_write_byte_data(priv->client, in stts751_adjust_resolution()
146 STTS751_REG_CONF, priv->config); in stts751_adjust_resolution()
157 * one-shot conversion register, but we don't want to do this (disables in stts751_update_temp()
164 integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H); in stts751_update_temp()
166 dev_dbg(&priv->client->dev, in stts751_update_temp()
171 frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L); in stts751_update_temp()
173 dev_dbg(&priv->client->dev, in stts751_update_temp()
178 integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H); in stts751_update_temp()
180 dev_dbg(&priv->client->dev, in stts751_update_temp()
186 frac = i2c_smbus_read_byte_data(priv->client, in stts751_update_temp()
189 dev_dbg(&priv->client->dev, in stts751_update_temp()
196 priv->temp = stts751_to_deg((integer1 << 8) | frac); in stts751_update_temp()
208 ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8); in stts751_set_temp_reg16()
212 return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff); in stts751_set_temp_reg16()
220 return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8); in stts751_set_temp_reg8()
228 integer = i2c_smbus_read_byte_data(priv->client, hreg); in stts751_read_reg16()
232 frac = i2c_smbus_read_byte_data(priv->client, lreg); in stts751_read_reg16()
245 integer = i2c_smbus_read_byte_data(priv->client, reg); in stts751_read_reg8()
255 * Update alert flags without waiting for cache to expire. We detects alerts
264 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]); in stts751_update_alert()
272 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS); in stts751_update_alert()
276 dev_dbg(&priv->client->dev, "status reg %x\n", ret); in stts751_update_alert()
290 if (time_after(jiffies, priv->last_alert_update + cache_time) || in stts751_update_alert()
291 conv_done || !priv->alert_valid) { in stts751_update_alert()
292 priv->max_alert = false; in stts751_update_alert()
293 priv->min_alert = false; in stts751_update_alert()
294 priv->alert_valid = true; in stts751_update_alert()
295 priv->last_alert_update = jiffies; in stts751_update_alert()
296 dev_dbg(&priv->client->dev, "invalidating alert cache\n"); in stts751_update_alert()
299 priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH); in stts751_update_alert()
300 priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL); in stts751_update_alert()
301 priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT); in stts751_update_alert()
303 dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n", in stts751_update_alert()
304 priv->max_alert, priv->min_alert, priv->therm_trip); in stts751_update_alert()
318 dev_dbg(&client->dev, "alert!"); in stts751_alert()
320 mutex_lock(&priv->access_lock); in stts751_alert()
324 priv->max_alert = true; in stts751_alert()
325 priv->min_alert = true; in stts751_alert()
327 dev_warn(priv->dev, in stts751_alert()
331 if (priv->max_alert) { in stts751_alert()
332 if (priv->notify_max) in stts751_alert()
333 dev_notice(priv->dev, "got alert for HIGH temperature"); in stts751_alert()
334 priv->notify_max = false; in stts751_alert()
337 sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm"); in stts751_alert()
340 if (priv->min_alert) { in stts751_alert()
341 if (priv->notify_min) in stts751_alert()
342 dev_notice(priv->dev, "got alert for LOW temperature"); in stts751_alert()
343 priv->notify_min = false; in stts751_alert()
346 sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm"); in stts751_alert()
349 if (priv->min_alert || priv->max_alert) in stts751_alert()
350 kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE); in stts751_alert()
352 mutex_unlock(&priv->access_lock); in stts751_alert()
358 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]); in stts751_update()
360 if (time_after(jiffies, priv->last_update + cache_time) || in stts751_update()
361 !priv->data_valid) { in stts751_update()
369 priv->data_valid = true; in stts751_update()
370 priv->last_update = jiffies; in stts751_update()
382 mutex_lock(&priv->access_lock); in max_alarm_show()
385 priv->notify_max = true; in max_alarm_show()
386 mutex_unlock(&priv->access_lock); in max_alarm_show()
390 return sysfs_emit(buf, "%d\n", priv->max_alert); in max_alarm_show()
399 mutex_lock(&priv->access_lock); in min_alarm_show()
402 priv->notify_min = true; in min_alarm_show()
403 mutex_unlock(&priv->access_lock); in min_alarm_show()
407 return sysfs_emit(buf, "%d\n", priv->min_alert); in min_alarm_show()
416 mutex_lock(&priv->access_lock); in input_show()
418 mutex_unlock(&priv->access_lock); in input_show()
422 return sysfs_emit(buf, "%d\n", priv->temp); in input_show()
430 return sysfs_emit(buf, "%d\n", priv->therm); in therm_show()
441 return -EINVAL; in therm_store()
443 /* HW works in range -64C to +127.937C */ in therm_store()
444 temp = clamp_val(temp, -64000, 127937); in therm_store()
445 mutex_lock(&priv->access_lock); in therm_store()
450 dev_dbg(&priv->client->dev, "setting therm %ld", temp); in therm_store()
454 * adjusted, we need to update our local copy only. in therm_store()
456 priv->hyst = temp - (priv->therm - priv->hyst); in therm_store()
457 priv->therm = temp; in therm_store()
460 mutex_unlock(&priv->access_lock); in therm_store()
472 return sysfs_emit(buf, "%d\n", priv->hyst); in hyst_show()
484 return -EINVAL; in hyst_store()
486 mutex_lock(&priv->access_lock); in hyst_store()
487 /* HW works in range -64C to +127.937C */ in hyst_store()
488 temp = clamp_val(temp, -64000, priv->therm); in hyst_store()
489 priv->hyst = temp; in hyst_store()
490 dev_dbg(&priv->client->dev, "setting hyst %ld", temp); in hyst_store()
491 temp = priv->therm - temp; in hyst_store()
493 mutex_unlock(&priv->access_lock); in hyst_store()
506 mutex_lock(&priv->access_lock); in therm_trip_show()
508 mutex_unlock(&priv->access_lock); in therm_trip_show()
512 return sysfs_emit(buf, "%d\n", priv->therm_trip); in therm_trip_show()
520 return sysfs_emit(buf, "%d\n", priv->event_max); in max_show()
531 return -EINVAL; in max_store()
533 mutex_lock(&priv->access_lock); in max_store()
534 /* HW works in range -64C to +127.937C */ in max_store()
535 temp = clamp_val(temp, priv->event_min, 127937); in max_store()
541 dev_dbg(&priv->client->dev, "setting event max %ld", temp); in max_store()
542 priv->event_max = temp; in max_store()
545 mutex_unlock(&priv->access_lock); in max_store()
554 return sysfs_emit(buf, "%d\n", priv->event_min); in min_show()
565 return -EINVAL; in min_store()
567 mutex_lock(&priv->access_lock); in min_store()
568 /* HW works in range -64C to +127.937C */ in min_store()
569 temp = clamp_val(temp, -64000, priv->event_max); in min_store()
575 dev_dbg(&priv->client->dev, "setting event min %ld", temp); in min_store()
576 priv->event_min = temp; in min_store()
579 mutex_unlock(&priv->access_lock); in min_store()
589 stts751_intervals[priv->interval]); in interval_show()
602 return -EINVAL; in interval_store()
607 dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d", in interval_store()
610 mutex_lock(&priv->access_lock); in interval_store()
611 if (priv->interval == idx) in interval_store()
623 if (priv->interval < idx) { in interval_store()
624 dev_dbg(&priv->client->dev, "lower resolution, then modify convrate"); in interval_store()
625 priv->interval = idx; in interval_store()
631 ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx); in interval_store()
635 if (priv->interval != idx) { in interval_store()
636 dev_dbg(&priv->client->dev, "modify convrate, then raise resolution"); in interval_store()
637 priv->interval = idx; in interval_store()
644 mutex_unlock(&priv->access_lock); in interval_store()
652 struct i2c_adapter *adapter = new_client->adapter; in stts751_detect()
657 return -ENODEV; in stts751_detect()
661 return -ENODEV; in stts751_detect()
663 /* lower temperaure registers always have bits 0-3 set to zero */ in stts751_detect()
666 return -ENODEV; in stts751_detect()
670 return -ENODEV; in stts751_detect()
674 return -ENODEV; in stts751_detect()
676 /* smbus timeout register always have bits 0-7 set to zero */ in stts751_detect()
679 return -ENODEV; in stts751_detect()
685 name = "STTS751-0"; in stts751_detect()
688 name = "STTS751-1"; in stts751_detect()
691 return -ENODEV; in stts751_detect()
693 dev_dbg(&new_client->dev, "Chip %s detected", name); in stts751_detect()
695 strscpy(info->type, stts751_id[0].name, I2C_NAME_SIZE); in stts751_detect()
704 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF); in stts751_read_chip_config()
707 priv->config = ret; in stts751_read_chip_config()
708 priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT; in stts751_read_chip_config()
710 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE); in stts751_read_chip_config()
714 dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret); in stts751_read_chip_config()
715 return -ENODEV; in stts751_read_chip_config()
717 priv->interval = ret; in stts751_read_chip_config()
719 ret = stts751_read_reg16(priv, &priv->event_max, in stts751_read_chip_config()
724 ret = stts751_read_reg16(priv, &priv->event_min, in stts751_read_chip_config()
729 ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM); in stts751_read_chip_config()
736 priv->hyst = priv->therm - tmp; in stts751_read_chip_config()
749 static SENSOR_DEVICE_ATTR_RW(update_interval, interval, 0);
772 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); in stts751_probe()
774 return -ENOMEM; in stts751_probe()
776 priv->client = client; in stts751_probe()
777 priv->notify_max = true; in stts751_probe()
778 priv->notify_min = true; in stts751_probe()
780 mutex_init(&priv->access_lock); in stts751_probe()
782 if (device_property_present(&client->dev, in stts751_probe()
783 "smbus-timeout-disable")) { in stts751_probe()
784 smbus_nto = device_property_read_bool(&client->dev, in stts751_probe()
785 "smbus-timeout-disable"); in stts751_probe()
795 return -ENODEV; in stts751_probe()
797 dev_dbg(&client->dev, "Chip revision 0x%x is untested\n", in stts751_probe()
805 priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS); in stts751_probe()
806 ret = i2c_smbus_write_byte_data(client, STTS751_REG_CONF, priv->config); in stts751_probe()
810 priv->dev = devm_hwmon_device_register_with_groups(&client->dev, in stts751_probe()
811 client->name, priv, in stts751_probe()
813 return PTR_ERR_OR_ZERO(priv->dev); in stts751_probe()