Lines Matching +full:poll +full:- +full:rate +full:- +full:ms
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):
68 * approx value in mS.
104 * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
120 switch (priv->interval) { in stts751_adjust_resolution()
135 if (priv->res == res) in stts751_adjust_resolution()
138 priv->config &= ~STTS751_CONF_RES_MASK; in stts751_adjust_resolution()
139 priv->config |= res << STTS751_CONF_RES_SHIFT; in stts751_adjust_resolution()
140 dev_dbg(&priv->client->dev, "setting res %d. config %x", in stts751_adjust_resolution()
141 res, priv->config); in stts751_adjust_resolution()
142 priv->res = res; in stts751_adjust_resolution()
144 return i2c_smbus_write_byte_data(priv->client, in stts751_adjust_resolution()
145 STTS751_REG_CONF, priv->config); in stts751_adjust_resolution()
156 * one-shot conversion register, but we don't want to do this (disables in stts751_update_temp()
163 integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H); in stts751_update_temp()
165 dev_dbg(&priv->client->dev, in stts751_update_temp()
170 frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L); in stts751_update_temp()
172 dev_dbg(&priv->client->dev, in stts751_update_temp()
177 integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H); in stts751_update_temp()
179 dev_dbg(&priv->client->dev, in stts751_update_temp()
185 frac = i2c_smbus_read_byte_data(priv->client, in stts751_update_temp()
188 dev_dbg(&priv->client->dev, in stts751_update_temp()
195 priv->temp = stts751_to_deg((integer1 << 8) | frac); in stts751_update_temp()
207 ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8); in stts751_set_temp_reg16()
211 return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff); in stts751_set_temp_reg16()
219 return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8); in stts751_set_temp_reg8()
227 integer = i2c_smbus_read_byte_data(priv->client, hreg); in stts751_read_reg16()
231 frac = i2c_smbus_read_byte_data(priv->client, lreg); in stts751_read_reg16()
244 integer = i2c_smbus_read_byte_data(priv->client, reg); in stts751_read_reg8()
263 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]); in stts751_update_alert()
267 * rate we will end up in reporting incorrectly alarms. in stts751_update_alert()
271 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS); in stts751_update_alert()
275 dev_dbg(&priv->client->dev, "status reg %x\n", ret); in stts751_update_alert()
289 if (time_after(jiffies, priv->last_alert_update + cache_time) || in stts751_update_alert()
290 conv_done || !priv->alert_valid) { in stts751_update_alert()
291 priv->max_alert = false; in stts751_update_alert()
292 priv->min_alert = false; in stts751_update_alert()
293 priv->alert_valid = true; in stts751_update_alert()
294 priv->last_alert_update = jiffies; in stts751_update_alert()
295 dev_dbg(&priv->client->dev, "invalidating alert cache\n"); in stts751_update_alert()
298 priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH); in stts751_update_alert()
299 priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL); in stts751_update_alert()
300 priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT); in stts751_update_alert()
302 dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n", in stts751_update_alert()
303 priv->max_alert, priv->min_alert, priv->therm_trip); in stts751_update_alert()
317 dev_dbg(&client->dev, "alert!"); in stts751_alert()
319 mutex_lock(&priv->access_lock); in stts751_alert()
323 priv->max_alert = true; in stts751_alert()
324 priv->min_alert = true; in stts751_alert()
326 dev_warn(priv->dev, in stts751_alert()
330 if (priv->max_alert) { in stts751_alert()
331 if (priv->notify_max) in stts751_alert()
332 dev_notice(priv->dev, "got alert for HIGH temperature"); in stts751_alert()
333 priv->notify_max = false; in stts751_alert()
335 /* unblock alert poll */ in stts751_alert()
336 sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm"); in stts751_alert()
339 if (priv->min_alert) { in stts751_alert()
340 if (priv->notify_min) in stts751_alert()
341 dev_notice(priv->dev, "got alert for LOW temperature"); in stts751_alert()
342 priv->notify_min = false; in stts751_alert()
344 /* unblock alert poll */ in stts751_alert()
345 sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm"); in stts751_alert()
348 if (priv->min_alert || priv->max_alert) in stts751_alert()
349 kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE); in stts751_alert()
351 mutex_unlock(&priv->access_lock); in stts751_alert()
357 int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]); in stts751_update()
359 if (time_after(jiffies, priv->last_update + cache_time) || in stts751_update()
360 !priv->data_valid) { in stts751_update()
368 priv->data_valid = true; in stts751_update()
369 priv->last_update = jiffies; in stts751_update()
381 mutex_lock(&priv->access_lock); in max_alarm_show()
384 priv->notify_max = true; in max_alarm_show()
385 mutex_unlock(&priv->access_lock); in max_alarm_show()
389 return sysfs_emit(buf, "%d\n", priv->max_alert); in max_alarm_show()
398 mutex_lock(&priv->access_lock); in min_alarm_show()
401 priv->notify_min = true; in min_alarm_show()
402 mutex_unlock(&priv->access_lock); in min_alarm_show()
406 return sysfs_emit(buf, "%d\n", priv->min_alert); in min_alarm_show()
415 mutex_lock(&priv->access_lock); in input_show()
417 mutex_unlock(&priv->access_lock); in input_show()
421 return sysfs_emit(buf, "%d\n", priv->temp); in input_show()
429 return sysfs_emit(buf, "%d\n", priv->therm); in therm_show()
440 return -EINVAL; in therm_store()
442 /* HW works in range -64C to +127.937C */ in therm_store()
443 temp = clamp_val(temp, -64000, 127937); in therm_store()
444 mutex_lock(&priv->access_lock); in therm_store()
449 dev_dbg(&priv->client->dev, "setting therm %ld", temp); in therm_store()
455 priv->hyst = temp - (priv->therm - priv->hyst); in therm_store()
456 priv->therm = temp; in therm_store()
459 mutex_unlock(&priv->access_lock); in therm_store()
471 return sysfs_emit(buf, "%d\n", priv->hyst); in hyst_show()
483 return -EINVAL; in hyst_store()
485 mutex_lock(&priv->access_lock); in hyst_store()
486 /* HW works in range -64C to +127.937C */ in hyst_store()
487 temp = clamp_val(temp, -64000, priv->therm); in hyst_store()
488 priv->hyst = temp; in hyst_store()
489 dev_dbg(&priv->client->dev, "setting hyst %ld", temp); in hyst_store()
490 temp = priv->therm - temp; in hyst_store()
492 mutex_unlock(&priv->access_lock); in hyst_store()
505 mutex_lock(&priv->access_lock); in therm_trip_show()
507 mutex_unlock(&priv->access_lock); in therm_trip_show()
511 return sysfs_emit(buf, "%d\n", priv->therm_trip); in therm_trip_show()
519 return sysfs_emit(buf, "%d\n", priv->event_max); in max_show()
530 return -EINVAL; in max_store()
532 mutex_lock(&priv->access_lock); in max_store()
533 /* HW works in range -64C to +127.937C */ in max_store()
534 temp = clamp_val(temp, priv->event_min, 127937); in max_store()
540 dev_dbg(&priv->client->dev, "setting event max %ld", temp); in max_store()
541 priv->event_max = temp; in max_store()
544 mutex_unlock(&priv->access_lock); in max_store()
553 return sysfs_emit(buf, "%d\n", priv->event_min); in min_show()
564 return -EINVAL; in min_store()
566 mutex_lock(&priv->access_lock); in min_store()
567 /* HW works in range -64C to +127.937C */ in min_store()
568 temp = clamp_val(temp, -64000, priv->event_max); in min_store()
574 dev_dbg(&priv->client->dev, "setting event min %ld", temp); in min_store()
575 priv->event_min = temp; in min_store()
578 mutex_unlock(&priv->access_lock); in min_store()
588 stts751_intervals[priv->interval]); in interval_show()
601 return -EINVAL; in interval_store()
606 dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d", in interval_store()
609 mutex_lock(&priv->access_lock); in interval_store()
610 if (priv->interval == idx) in interval_store()
617 * conservative and set rate/resolution in such an order that avoids in interval_store()
622 if (priv->interval < idx) { in interval_store()
623 dev_dbg(&priv->client->dev, "lower resolution, then modify convrate"); in interval_store()
624 priv->interval = idx; in interval_store()
630 ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx); in interval_store()
634 if (priv->interval != idx) { in interval_store()
635 dev_dbg(&priv->client->dev, "modify convrate, then raise resolution"); in interval_store()
636 priv->interval = idx; in interval_store()
643 mutex_unlock(&priv->access_lock); in interval_store()
651 struct i2c_adapter *adapter = new_client->adapter; in stts751_detect()
656 return -ENODEV; in stts751_detect()
660 return -ENODEV; in stts751_detect()
662 /* lower temperaure registers always have bits 0-3 set to zero */ in stts751_detect()
665 return -ENODEV; in stts751_detect()
669 return -ENODEV; in stts751_detect()
673 return -ENODEV; in stts751_detect()
675 /* smbus timeout register always have bits 0-7 set to zero */ in stts751_detect()
678 return -ENODEV; in stts751_detect()
684 name = "STTS751-0"; in stts751_detect()
687 name = "STTS751-1"; in stts751_detect()
690 return -ENODEV; in stts751_detect()
692 dev_dbg(&new_client->dev, "Chip %s detected", name); in stts751_detect()
694 strscpy(info->type, stts751_id[0].name, I2C_NAME_SIZE); in stts751_detect()
703 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF); in stts751_read_chip_config()
706 priv->config = ret; in stts751_read_chip_config()
707 priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT; in stts751_read_chip_config()
709 ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE); in stts751_read_chip_config()
713 dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret); in stts751_read_chip_config()
714 return -ENODEV; in stts751_read_chip_config()
716 priv->interval = ret; in stts751_read_chip_config()
718 ret = stts751_read_reg16(priv, &priv->event_max, in stts751_read_chip_config()
723 ret = stts751_read_reg16(priv, &priv->event_min, in stts751_read_chip_config()
728 ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM); in stts751_read_chip_config()
735 priv->hyst = priv->therm - tmp; in stts751_read_chip_config()
771 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); in stts751_probe()
773 return -ENOMEM; in stts751_probe()
775 priv->client = client; in stts751_probe()
776 priv->notify_max = true; in stts751_probe()
777 priv->notify_min = true; in stts751_probe()
779 mutex_init(&priv->access_lock); in stts751_probe()
781 if (device_property_present(&client->dev, in stts751_probe()
782 "smbus-timeout-disable")) { in stts751_probe()
783 smbus_nto = device_property_read_bool(&client->dev, in stts751_probe()
784 "smbus-timeout-disable"); in stts751_probe()
794 return -ENODEV; in stts751_probe()
796 dev_dbg(&client->dev, "Chip revision 0x%x is untested\n", in stts751_probe()
804 priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS); in stts751_probe()
805 ret = i2c_smbus_write_byte_data(client, STTS751_REG_CONF, priv->config); in stts751_probe()
809 priv->dev = devm_hwmon_device_register_with_groups(&client->dev, in stts751_probe()
810 client->name, priv, in stts751_probe()
812 return PTR_ERR_OR_ZERO(priv->dev); in stts751_probe()