Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * STTS751 sensor driver
  4 *
  5 * Copyright (C) 2016-2017 Istituto Italiano di Tecnologia - RBCS - EDL
  6 * Robotics, Brain and Cognitive Sciences department
  7 * Electronic Design Laboratory
  8 *
  9 * Written by Andrea Merello <andrea.merello@gmail.com>
 10 *
 11 * Based on  LM95241 driver and LM90 driver
 
 
 
 
 
 
 
 
 
 
 12 */
 13
 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>
 28
 29#define DEVNAME "stts751"
 30
 31static const unsigned short normal_i2c[] = {
 32	0x48, 0x49, 0x38, 0x39,  /* STTS751-0 */
 33	0x4A, 0x4B, 0x3A, 0x3B,  /* STTS751-1 */
 34	I2C_CLIENT_END };
 35
 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
 55
 56#define STTS751_REG_PROD_ID	0xFD
 57#define STTS751_REG_MAN_ID	0xFE
 58#define STTS751_REG_REV_ID	0xFF
 59
 60#define STTS751_0_PROD_ID	0x00
 61#define STTS751_1_PROD_ID	0x01
 62#define ST_MAN_ID		0x53
 63
 64/*
 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
 68 * approx value in mS.
 69 */
 70static const int stts751_intervals[] = {
 71	16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 31
 72};
 73
 74static const struct i2c_device_id stts751_id[] = {
 75	{ "stts751", 0 },
 76	{ }
 77};
 78
 79static const struct of_device_id __maybe_unused stts751_of_match[] = {
 80	{ .compatible = "stts751" },
 81	{ },
 82};
 83MODULE_DEVICE_TABLE(of, stts751_of_match);
 84
 85struct stts751_priv {
 86	struct device *dev;
 87	struct i2c_client *client;
 88	struct mutex access_lock;
 89	u8 interval;
 90	int res;
 91	int event_max, event_min;
 92	int therm;
 93	int hyst;
 94	bool smbus_timeout;
 95	int temp;
 96	unsigned long last_update, last_alert_update;
 97	u8 config;
 98	bool min_alert, max_alert, therm_trip;
 99	bool data_valid, alert_valid;
100	bool notify_max, notify_min;
101};
102
103/*
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.
106 */
107static int stts751_to_deg(s16 hw_val)
108{
109	return hw_val * 125 / 32;
110}
111
112static s32 stts751_to_hw(int val)
113{
114	return DIV_ROUND_CLOSEST(val, 125) * 32;
115}
116
117static int stts751_adjust_resolution(struct stts751_priv *priv)
118{
119	u8 res;
120
121	switch (priv->interval) {
122	case 9:
123		/* 10 bits */
124		res = 0;
125		break;
126	case 8:
127		/* 11 bits */
128		res = 1;
129		break;
130	default:
131		/* 12 bits */
132		res = 3;
133		break;
134	}
135
136	if (priv->res == res)
137		return 0;
138
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",
142		res, priv->config);
143	priv->res = res;
144
145	return i2c_smbus_write_byte_data(priv->client,
146				STTS751_REG_CONF, priv->config);
147}
148
149static int stts751_update_temp(struct stts751_priv *priv)
150{
151	s32 integer1, integer2, frac;
152
153	/*
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
162	 * reading.
163	 */
164	integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
165	if (integer1 < 0) {
166		dev_dbg(&priv->client->dev,
167			"I2C read failed (temp H). ret: %x\n", integer1);
168		return integer1;
169	}
170
171	frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L);
172	if (frac < 0) {
173		dev_dbg(&priv->client->dev,
174			"I2C read failed (temp L). ret: %x\n", frac);
175		return frac;
176	}
177
178	integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
179	if (integer2 < 0) {
180		dev_dbg(&priv->client->dev,
181			"I2C 2nd read failed (temp H). ret: %x\n", integer2);
182		return integer2;
183	}
184
185	if (integer1 != integer2) {
186		frac = i2c_smbus_read_byte_data(priv->client,
187						STTS751_REG_TEMP_L);
188		if (frac < 0) {
189			dev_dbg(&priv->client->dev,
190				"I2C 2nd read failed (temp L). ret: %x\n",
191				frac);
192			return frac;
193		}
194	}
195
196	priv->temp = stts751_to_deg((integer1 << 8) | frac);
197	return 0;
198}
199
200static int stts751_set_temp_reg16(struct stts751_priv *priv, int temp,
201				  u8 hreg, u8 lreg)
202{
203	s32 hwval;
204	int ret;
205
206	hwval = stts751_to_hw(temp);
207
208	ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8);
209	if (ret)
210		return ret;
211
212	return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff);
213}
214
215static int stts751_set_temp_reg8(struct stts751_priv *priv, int temp, u8 reg)
216{
217	s32 hwval;
218
219	hwval = stts751_to_hw(temp);
220	return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8);
221}
222
223static int stts751_read_reg16(struct stts751_priv *priv, int *temp,
224			      u8 hreg, u8 lreg)
225{
226	int integer, frac;
227
228	integer = i2c_smbus_read_byte_data(priv->client, hreg);
229	if (integer < 0)
230		return integer;
231
232	frac = i2c_smbus_read_byte_data(priv->client, lreg);
233	if (frac < 0)
234		return frac;
235
236	*temp = stts751_to_deg((integer << 8) | frac);
237
238	return 0;
239}
240
241static int stts751_read_reg8(struct stts751_priv *priv, int *temp, u8 reg)
242{
243	int integer;
244
245	integer = i2c_smbus_read_byte_data(priv->client, reg);
246	if (integer < 0)
247		return integer;
248
249	*temp = stts751_to_deg(integer << 8);
250
251	return 0;
252}
253
254/*
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.
259 */
260static int stts751_update_alert(struct stts751_priv *priv)
261{
262	int ret;
263	bool conv_done;
264	int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
265
266	/*
267	 * Add another 10% because if we run faster than the HW conversion
268	 * rate we will end up in reporting incorrectly alarms.
269	 */
270	cache_time += cache_time / 10;
271
272	ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS);
273	if (ret < 0)
274		return ret;
275
276	dev_dbg(&priv->client->dev, "status reg %x\n", ret);
277	conv_done = ret & (STTS751_STATUS_TRIPH | STTS751_STATUS_TRIPL);
278	/*
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.
282	 *
283	 * Note that when the cache has been never written the point is
284	 * to correctly initialize the timestamp, rather than clearing
285	 * the cache values.
286	 *
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.
289	 */
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");
297	}
298
299	priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH);
300	priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL);
301	priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT);
302
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);
305
306	return 0;
307}
308
309static void stts751_alert(struct i2c_client *client,
310			  enum i2c_alert_protocol type, unsigned int data)
311{
312	int ret;
313	struct stts751_priv *priv = i2c_get_clientdata(client);
314
315	if (type != I2C_PROTOCOL_SMBUS_ALERT)
316		return;
317
318	dev_dbg(&client->dev, "alert!");
319
320	mutex_lock(&priv->access_lock);
321	ret = stts751_update_alert(priv);
322	if (ret < 0) {
323		/* default to worst case */
324		priv->max_alert = true;
325		priv->min_alert = true;
326
327		dev_warn(priv->dev,
328			 "Alert received, but can't communicate to the device. Triggering all alarms!");
329	}
330
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;
335
336		/* unblock alert poll */
337		sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm");
338	}
339
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;
344
345		/* unblock alert poll */
346		sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm");
347	}
348
349	if (priv->min_alert || priv->max_alert)
350		kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE);
351
352	mutex_unlock(&priv->access_lock);
353}
354
355static int stts751_update(struct stts751_priv *priv)
356{
357	int ret;
358	int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
359
360	if (time_after(jiffies,	priv->last_update + cache_time) ||
361	    !priv->data_valid) {
362		ret = stts751_update_temp(priv);
363		if (ret)
364			return ret;
365
366		ret = stts751_update_alert(priv);
367		if (ret)
368			return ret;
369		priv->data_valid = true;
370		priv->last_update = jiffies;
371	}
372
373	return 0;
374}
375
376static ssize_t max_alarm_show(struct device *dev,
377			      struct device_attribute *attr, char *buf)
378{
379	int ret;
380	struct stts751_priv *priv = dev_get_drvdata(dev);
381
382	mutex_lock(&priv->access_lock);
383	ret = stts751_update(priv);
384	if (!ret)
385		priv->notify_max = true;
386	mutex_unlock(&priv->access_lock);
387	if (ret < 0)
388		return ret;
389
390	return snprintf(buf, PAGE_SIZE, "%d\n", priv->max_alert);
391}
392
393static ssize_t min_alarm_show(struct device *dev,
394			      struct device_attribute *attr, char *buf)
395{
396	int ret;
397	struct stts751_priv *priv = dev_get_drvdata(dev);
398
399	mutex_lock(&priv->access_lock);
400	ret = stts751_update(priv);
401	if (!ret)
402		priv->notify_min = true;
403	mutex_unlock(&priv->access_lock);
404	if (ret < 0)
405		return ret;
406
407	return snprintf(buf, PAGE_SIZE, "%d\n", priv->min_alert);
408}
409
410static ssize_t input_show(struct device *dev, struct device_attribute *attr,
411			  char *buf)
412{
413	int ret;
414	struct stts751_priv *priv = dev_get_drvdata(dev);
415
416	mutex_lock(&priv->access_lock);
417	ret = stts751_update(priv);
418	mutex_unlock(&priv->access_lock);
419	if (ret < 0)
420		return ret;
421
422	return snprintf(buf, PAGE_SIZE, "%d\n", priv->temp);
423}
424
425static ssize_t therm_show(struct device *dev, struct device_attribute *attr,
426			  char *buf)
427{
428	struct stts751_priv *priv = dev_get_drvdata(dev);
429
430	return snprintf(buf, PAGE_SIZE, "%d\n", priv->therm);
431}
432
433static ssize_t therm_store(struct device *dev, struct device_attribute *attr,
434			   const char *buf, size_t count)
435{
436	int ret;
437	long temp;
438	struct stts751_priv *priv = dev_get_drvdata(dev);
439
440	if (kstrtol(buf, 10, &temp) < 0)
441		return -EINVAL;
442
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);
447	if (ret)
448		goto exit;
449
450	dev_dbg(&priv->client->dev, "setting therm %ld", temp);
451
452	/*
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.
455	 */
456	priv->hyst = temp - (priv->therm - priv->hyst);
457	priv->therm = temp;
458
459exit:
460	mutex_unlock(&priv->access_lock);
461	if (ret)
462		return ret;
463
464	return count;
465}
466
467static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
468			 char *buf)
469{
470	struct stts751_priv *priv = dev_get_drvdata(dev);
471
472	return snprintf(buf, PAGE_SIZE, "%d\n", priv->hyst);
473}
474
475static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
476			  const char *buf, size_t count)
477{
478	int ret;
479	long temp;
480
481	struct stts751_priv *priv = dev_get_drvdata(dev);
482
483	if (kstrtol(buf, 10, &temp) < 0)
484		return -EINVAL;
485
486	mutex_lock(&priv->access_lock);
487	/* HW works in range -64C to +127.937C */
488	temp = clamp_val(temp, -64000, priv->therm);
489	priv->hyst = temp;
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);
494	if (ret)
495		return ret;
496
497	return count;
498}
499
500static ssize_t therm_trip_show(struct device *dev,
501			       struct device_attribute *attr, char *buf)
502{
503	int ret;
504	struct stts751_priv *priv = dev_get_drvdata(dev);
505
506	mutex_lock(&priv->access_lock);
507	ret = stts751_update(priv);
508	mutex_unlock(&priv->access_lock);
509	if (ret < 0)
510		return ret;
511
512	return snprintf(buf, PAGE_SIZE, "%d\n", priv->therm_trip);
513}
514
515static ssize_t max_show(struct device *dev, struct device_attribute *attr,
516			char *buf)
517{
518	struct stts751_priv *priv = dev_get_drvdata(dev);
519
520	return snprintf(buf, PAGE_SIZE, "%d\n", priv->event_max);
521}
522
523static ssize_t max_store(struct device *dev, struct device_attribute *attr,
524			 const char *buf, size_t count)
525{
526	int ret;
527	long temp;
528	struct stts751_priv *priv = dev_get_drvdata(dev);
529
530	if (kstrtol(buf, 10, &temp) < 0)
531		return -EINVAL;
532
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);
538	if (ret)
539		goto exit;
540
541	dev_dbg(&priv->client->dev, "setting event max %ld", temp);
542	priv->event_max = temp;
543	ret = count;
544exit:
545	mutex_unlock(&priv->access_lock);
546	return ret;
547}
548
549static ssize_t min_show(struct device *dev, struct device_attribute *attr,
550			char *buf)
551{
552	struct stts751_priv *priv = dev_get_drvdata(dev);
553
554	return snprintf(buf, PAGE_SIZE, "%d\n", priv->event_min);
555}
556
557static ssize_t min_store(struct device *dev, struct device_attribute *attr,
558			 const char *buf, size_t count)
559{
560	int ret;
561	long temp;
562	struct stts751_priv *priv = dev_get_drvdata(dev);
563
564	if (kstrtol(buf, 10, &temp) < 0)
565		return -EINVAL;
566
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);
572	if (ret)
573		goto exit;
574
575	dev_dbg(&priv->client->dev, "setting event min %ld", temp);
576	priv->event_min = temp;
577	ret = count;
578exit:
579	mutex_unlock(&priv->access_lock);
580	return ret;
581}
582
583static ssize_t interval_show(struct device *dev,
584			     struct device_attribute *attr, char *buf)
585{
586	struct stts751_priv *priv = dev_get_drvdata(dev);
587
588	return snprintf(buf, PAGE_SIZE, "%d\n",
589			stts751_intervals[priv->interval]);
590}
591
592static ssize_t interval_store(struct device *dev,
593			      struct device_attribute *attr, const char *buf,
594			      size_t count)
595{
596	unsigned long val;
597	int idx;
598	int ret = count;
599	struct stts751_priv *priv = dev_get_drvdata(dev);
600
601	if (kstrtoul(buf, 10, &val) < 0)
602		return -EINVAL;
603
604	idx = find_closest_descending(val, stts751_intervals,
605				      ARRAY_SIZE(stts751_intervals));
606
607	dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d",
608		val, idx, stts751_intervals[idx]);
609
610	mutex_lock(&priv->access_lock);
611	if (priv->interval == idx)
612		goto exit;
613
614	/*
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.
620	 */
621
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);
627		if (ret)
628			goto exit;
629	}
630
631	ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx);
632	if (ret)
633		goto exit;
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);
639		if (ret)
640			goto exit;
641	}
642	ret = count;
643exit:
644	mutex_unlock(&priv->access_lock);
645
646	return ret;
647}
648
649static int stts751_detect(struct i2c_client *new_client,
650			  struct i2c_board_info *info)
651{
652	struct i2c_adapter *adapter = new_client->adapter;
653	const char *name;
654	int tmp;
655
656	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
657		return -ENODEV;
658
659	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_MAN_ID);
660	if (tmp != ST_MAN_ID)
661		return -ENODEV;
662
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);
665	if (tmp & 0xf)
666		return -ENODEV;
667
668	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_HLIM_L);
669	if (tmp & 0xf)
670		return -ENODEV;
671
672	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_LLIM_L);
673	if (tmp & 0xf)
674		return -ENODEV;
675
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);
678	if (tmp & 0x7f)
679		return -ENODEV;
680
681	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_PROD_ID);
682
683	switch (tmp) {
684	case STTS751_0_PROD_ID:
685		name = "STTS751-0";
686		break;
687	case STTS751_1_PROD_ID:
688		name = "STTS751-1";
689		break;
690	default:
691		return -ENODEV;
692	}
693	dev_dbg(&new_client->dev, "Chip %s detected", name);
694
695	strlcpy(info->type, stts751_id[0].name, I2C_NAME_SIZE);
696	return 0;
697}
698
699static int stts751_read_chip_config(struct stts751_priv *priv)
700{
701	int ret;
702	int tmp;
703
704	ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF);
705	if (ret < 0)
706		return ret;
707	priv->config = ret;
708	priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT;
709
710	ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE);
711	if (ret < 0)
712		return ret;
713	if (ret >= ARRAY_SIZE(stts751_intervals)) {
714		dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret);
715		return -ENODEV;
716	}
717	priv->interval = ret;
718
719	ret = stts751_read_reg16(priv, &priv->event_max,
720				 STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
721	if (ret)
722		return ret;
723
724	ret = stts751_read_reg16(priv, &priv->event_min,
725				 STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
726	if (ret)
727		return ret;
728
729	ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM);
730	if (ret)
731		return ret;
732
733	ret = stts751_read_reg8(priv, &tmp, STTS751_REG_HYST);
734	if (ret)
735		return ret;
736	priv->hyst = priv->therm - tmp;
737
738	return 0;
739}
740
741static SENSOR_DEVICE_ATTR_RO(temp1_input, input, 0);
742static SENSOR_DEVICE_ATTR_RW(temp1_min, min, 0);
743static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
744static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, min_alarm, 0);
745static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, max_alarm, 0);
746static SENSOR_DEVICE_ATTR_RW(temp1_crit, therm, 0);
747static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0);
748static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, therm_trip, 0);
749static SENSOR_DEVICE_ATTR_RW(update_interval, interval, 0);
 
750
751static 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,
761	NULL
762};
763ATTRIBUTE_GROUPS(stts751);
764
765static int stts751_probe(struct i2c_client *client,
766			 const struct i2c_device_id *id)
767{
768	struct stts751_priv *priv;
769	int ret;
770	bool smbus_nto;
771	int rev_id;
772
773	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
774	if (!priv)
775		return -ENOMEM;
776
777	priv->client = client;
778	priv->notify_max = true;
779	priv->notify_min = true;
780	i2c_set_clientdata(client, priv);
781	mutex_init(&priv->access_lock);
782
783	if (device_property_present(&client->dev,
784				    "smbus-timeout-disable")) {
785		smbus_nto = device_property_read_bool(&client->dev,
786						      "smbus-timeout-disable");
787
788		ret = i2c_smbus_write_byte_data(client,	STTS751_REG_SMBUS_TO,
789						smbus_nto ? 0 : 0x80);
790		if (ret)
791			return ret;
792	}
793
794	rev_id = i2c_smbus_read_byte_data(client, STTS751_REG_REV_ID);
795	if (rev_id < 0)
796		return -ENODEV;
797	if (rev_id != 0x1) {
798		dev_dbg(&client->dev, "Chip revision 0x%x is untested\n",
799			rev_id);
800	}
801
802	ret = stts751_read_chip_config(priv);
803	if (ret)
804		return ret;
805
806	priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS);
807	ret = i2c_smbus_write_byte_data(client,	STTS751_REG_CONF, priv->config);
808	if (ret)
809		return ret;
810
811	priv->dev = devm_hwmon_device_register_with_groups(&client->dev,
812							client->name, priv,
813							stts751_groups);
814	return PTR_ERR_OR_ZERO(priv->dev);
815}
816
817MODULE_DEVICE_TABLE(i2c, stts751_id);
818
819static struct i2c_driver stts751_driver = {
820	.class		= I2C_CLASS_HWMON,
821	.driver = {
822		.name	= DEVNAME,
823		.of_match_table = of_match_ptr(stts751_of_match),
824	},
825	.probe		= stts751_probe,
826	.id_table	= stts751_id,
827	.detect		= stts751_detect,
828	.alert		= stts751_alert,
829	.address_list	= normal_i2c,
830};
831
832module_i2c_driver(stts751_driver);
833
834MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
835MODULE_DESCRIPTION("STTS751 sensor driver");
836MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * STTS751 sensor driver
  3 *
  4 * Copyright (C) 2016-2017 Istituto Italiano di Tecnologia - RBCS - EDL
  5 * Robotics, Brain and Cognitive Sciences department
  6 * Electronic Design Laboratory
  7 *
  8 * Written by Andrea Merello <andrea.merello@gmail.com>
  9 *
 10 * Based on  LM95241 driver and LM90 driver
 11 *
 12 * This program is free software; you can redistribute it and/or modify
 13 * it under the terms of the GNU General Public License as published by
 14 * the Free Software Foundation; either version 2 of the License, or
 15 * (at your option) any later version.
 16 *
 17 * This program is distributed in the hope that it will be useful,
 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20 * GNU General Public License for more details.
 21 */
 22
 23#include <linux/bitops.h>
 24#include <linux/err.h>
 25#include <linux/hwmon.h>
 26#include <linux/hwmon-sysfs.h>
 27#include <linux/i2c.h>
 28#include <linux/init.h>
 29#include <linux/interrupt.h>
 30#include <linux/jiffies.h>
 31#include <linux/module.h>
 32#include <linux/mutex.h>
 33#include <linux/property.h>
 34#include <linux/slab.h>
 35#include <linux/sysfs.h>
 36#include <linux/util_macros.h>
 37
 38#define DEVNAME "stts751"
 39
 40static const unsigned short normal_i2c[] = {
 41	0x48, 0x49, 0x38, 0x39,  /* STTS751-0 */
 42	0x4A, 0x4B, 0x3A, 0x3B,  /* STTS751-1 */
 43	I2C_CLIENT_END };
 44
 45#define STTS751_REG_TEMP_H	0x00
 46#define STTS751_REG_STATUS	0x01
 47#define STTS751_STATUS_TRIPT	BIT(0)
 48#define STTS751_STATUS_TRIPL	BIT(5)
 49#define STTS751_STATUS_TRIPH	BIT(6)
 50#define STTS751_REG_TEMP_L	0x02
 51#define STTS751_REG_CONF	0x03
 52#define STTS751_CONF_RES_MASK	0x0C
 53#define STTS751_CONF_RES_SHIFT  2
 54#define STTS751_CONF_EVENT_DIS  BIT(7)
 55#define STTS751_CONF_STOP	BIT(6)
 56#define STTS751_REG_RATE	0x04
 57#define STTS751_REG_HLIM_H	0x05
 58#define STTS751_REG_HLIM_L	0x06
 59#define STTS751_REG_LLIM_H	0x07
 60#define STTS751_REG_LLIM_L	0x08
 61#define STTS751_REG_TLIM	0x20
 62#define STTS751_REG_HYST	0x21
 63#define STTS751_REG_SMBUS_TO	0x22
 64
 65#define STTS751_REG_PROD_ID	0xFD
 66#define STTS751_REG_MAN_ID	0xFE
 67#define STTS751_REG_REV_ID	0xFF
 68
 69#define STTS751_0_PROD_ID	0x00
 70#define STTS751_1_PROD_ID	0x01
 71#define ST_MAN_ID		0x53
 72
 73/*
 74 * Possible update intervals are (in mS):
 75 * 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62.5, 31.25
 76 * However we are not going to complicate things too much and we stick to the
 77 * approx value in mS.
 78 */
 79static const int stts751_intervals[] = {
 80	16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 31
 81};
 82
 83static const struct i2c_device_id stts751_id[] = {
 84	{ "stts751", 0 },
 85	{ }
 86};
 87
 88static const struct of_device_id stts751_of_match[] = {
 89	{ .compatible = "stts751" },
 90	{ },
 91};
 92MODULE_DEVICE_TABLE(of, stts751_of_match);
 93
 94struct stts751_priv {
 95	struct device *dev;
 96	struct i2c_client *client;
 97	struct mutex access_lock;
 98	u8 interval;
 99	int res;
100	int event_max, event_min;
101	int therm;
102	int hyst;
103	bool smbus_timeout;
104	int temp;
105	unsigned long last_update, last_alert_update;
106	u8 config;
107	bool min_alert, max_alert, therm_trip;
108	bool data_valid, alert_valid;
109	bool notify_max, notify_min;
110};
111
112/*
113 * These functions converts temperature from HW format to integer format and
114 * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
115 */
116static int stts751_to_deg(s16 hw_val)
117{
118	return hw_val * 125 / 32;
119}
120
121static s32 stts751_to_hw(int val)
122{
123	return DIV_ROUND_CLOSEST(val, 125) * 32;
124}
125
126static int stts751_adjust_resolution(struct stts751_priv *priv)
127{
128	u8 res;
129
130	switch (priv->interval) {
131	case 9:
132		/* 10 bits */
133		res = 0;
134		break;
135	case 8:
136		/* 11 bits */
137		res = 1;
138		break;
139	default:
140		/* 12 bits */
141		res = 3;
142		break;
143	}
144
145	if (priv->res == res)
146		return 0;
147
148	priv->config &= ~STTS751_CONF_RES_MASK;
149	priv->config |= res << STTS751_CONF_RES_SHIFT;
150	dev_dbg(&priv->client->dev, "setting res %d. config %x",
151		res, priv->config);
152	priv->res = res;
153
154	return i2c_smbus_write_byte_data(priv->client,
155				STTS751_REG_CONF, priv->config);
156}
157
158static int stts751_update_temp(struct stts751_priv *priv)
159{
160	s32 integer1, integer2, frac;
161
162	/*
163	 * There is a trick here, like in the lm90 driver. We have to read two
164	 * registers to get the sensor temperature, but we have to beware a
165	 * conversion could occur between the readings. We could use the
166	 * one-shot conversion register, but we don't want to do this (disables
167	 * hardware monitoring). So the solution used here is to read the high
168	 * byte once, then the low byte, then the high byte again. If the new
169	 * high byte matches the old one, then we have a valid reading. Else we
170	 * have to read the low byte again, and now we believe we have a correct
171	 * reading.
172	 */
173	integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
174	if (integer1 < 0) {
175		dev_dbg(&priv->client->dev,
176			"I2C read failed (temp H). ret: %x\n", integer1);
177		return integer1;
178	}
179
180	frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L);
181	if (frac < 0) {
182		dev_dbg(&priv->client->dev,
183			"I2C read failed (temp L). ret: %x\n", frac);
184		return frac;
185	}
186
187	integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
188	if (integer2 < 0) {
189		dev_dbg(&priv->client->dev,
190			"I2C 2nd read failed (temp H). ret: %x\n", integer2);
191		return integer2;
192	}
193
194	if (integer1 != integer2) {
195		frac = i2c_smbus_read_byte_data(priv->client,
196						STTS751_REG_TEMP_L);
197		if (frac < 0) {
198			dev_dbg(&priv->client->dev,
199				"I2C 2nd read failed (temp L). ret: %x\n",
200				frac);
201			return frac;
202		}
203	}
204
205	priv->temp = stts751_to_deg((integer1 << 8) | frac);
206	return 0;
207}
208
209static int stts751_set_temp_reg16(struct stts751_priv *priv, int temp,
210				  u8 hreg, u8 lreg)
211{
212	s32 hwval;
213	int ret;
214
215	hwval = stts751_to_hw(temp);
216
217	ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8);
218	if (ret)
219		return ret;
220
221	return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff);
222}
223
224static int stts751_set_temp_reg8(struct stts751_priv *priv, int temp, u8 reg)
225{
226	s32 hwval;
227
228	hwval = stts751_to_hw(temp);
229	return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8);
230}
231
232static int stts751_read_reg16(struct stts751_priv *priv, int *temp,
233			      u8 hreg, u8 lreg)
234{
235	int integer, frac;
236
237	integer = i2c_smbus_read_byte_data(priv->client, hreg);
238	if (integer < 0)
239		return integer;
240
241	frac = i2c_smbus_read_byte_data(priv->client, lreg);
242	if (frac < 0)
243		return frac;
244
245	*temp = stts751_to_deg((integer << 8) | frac);
246
247	return 0;
248}
249
250static int stts751_read_reg8(struct stts751_priv *priv, int *temp, u8 reg)
251{
252	int integer;
253
254	integer = i2c_smbus_read_byte_data(priv->client, reg);
255	if (integer < 0)
256		return integer;
257
258	*temp = stts751_to_deg(integer << 8);
259
260	return 0;
261}
262
263/*
264 * Update alert flags without waiting for cache to expire. We detects alerts
265 * immediately for the sake of the alert handler; we still need to deal with
266 * caching to workaround the fact that alarm flags int the status register,
267 * despite what the datasheet claims, gets always cleared on read.
268 */
269static int stts751_update_alert(struct stts751_priv *priv)
270{
271	int ret;
272	bool conv_done;
273	int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
274
275	/*
276	 * Add another 10% because if we run faster than the HW conversion
277	 * rate we will end up in reporting incorrectly alarms.
278	 */
279	cache_time += cache_time / 10;
280
281	ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS);
282	if (ret < 0)
283		return ret;
284
285	dev_dbg(&priv->client->dev, "status reg %x\n", ret);
286	conv_done = ret & (STTS751_STATUS_TRIPH | STTS751_STATUS_TRIPL);
287	/*
288	 * Reset the cache if the cache time expired, or if we are sure
289	 * we have valid data from a device conversion, or if we know
290	 * our cache has been never written.
291	 *
292	 * Note that when the cache has been never written the point is
293	 * to correctly initialize the timestamp, rather than clearing
294	 * the cache values.
295	 *
296	 * Note that updating the cache timestamp when we get an alarm flag
297	 * is required, otherwise we could incorrectly report alarms to be zero.
298	 */
299	if (time_after(jiffies,	priv->last_alert_update + cache_time) ||
300	    conv_done || !priv->alert_valid) {
301		priv->max_alert = false;
302		priv->min_alert = false;
303		priv->alert_valid = true;
304		priv->last_alert_update = jiffies;
305		dev_dbg(&priv->client->dev, "invalidating alert cache\n");
306	}
307
308	priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH);
309	priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL);
310	priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT);
311
312	dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n",
313		priv->max_alert, priv->min_alert, priv->therm_trip);
314
315	return 0;
316}
317
318static void stts751_alert(struct i2c_client *client,
319			  enum i2c_alert_protocol type, unsigned int data)
320{
321	int ret;
322	struct stts751_priv *priv = i2c_get_clientdata(client);
323
324	if (type != I2C_PROTOCOL_SMBUS_ALERT)
325		return;
326
327	dev_dbg(&client->dev, "alert!");
328
329	mutex_lock(&priv->access_lock);
330	ret = stts751_update_alert(priv);
331	if (ret < 0) {
332		/* default to worst case */
333		priv->max_alert = true;
334		priv->min_alert = true;
335
336		dev_warn(priv->dev,
337			 "Alert received, but can't communicate to the device. Triggering all alarms!");
338	}
339
340	if (priv->max_alert) {
341		if (priv->notify_max)
342			dev_notice(priv->dev, "got alert for HIGH temperature");
343		priv->notify_max = false;
344
345		/* unblock alert poll */
346		sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm");
347	}
348
349	if (priv->min_alert) {
350		if (priv->notify_min)
351			dev_notice(priv->dev, "got alert for LOW temperature");
352		priv->notify_min = false;
353
354		/* unblock alert poll */
355		sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm");
356	}
357
358	if (priv->min_alert || priv->max_alert)
359		kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE);
360
361	mutex_unlock(&priv->access_lock);
362}
363
364static int stts751_update(struct stts751_priv *priv)
365{
366	int ret;
367	int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
368
369	if (time_after(jiffies,	priv->last_update + cache_time) ||
370	    !priv->data_valid) {
371		ret = stts751_update_temp(priv);
372		if (ret)
373			return ret;
374
375		ret = stts751_update_alert(priv);
376		if (ret)
377			return ret;
378		priv->data_valid = true;
379		priv->last_update = jiffies;
380	}
381
382	return 0;
383}
384
385static ssize_t show_max_alarm(struct device *dev, struct device_attribute *attr,
386			      char *buf)
387{
388	int ret;
389	struct stts751_priv *priv = dev_get_drvdata(dev);
390
391	mutex_lock(&priv->access_lock);
392	ret = stts751_update(priv);
393	if (!ret)
394		priv->notify_max = true;
395	mutex_unlock(&priv->access_lock);
396	if (ret < 0)
397		return ret;
398
399	return snprintf(buf, PAGE_SIZE, "%d\n", priv->max_alert);
400}
401
402static ssize_t show_min_alarm(struct device *dev, struct device_attribute *attr,
403			      char *buf)
404{
405	int ret;
406	struct stts751_priv *priv = dev_get_drvdata(dev);
407
408	mutex_lock(&priv->access_lock);
409	ret = stts751_update(priv);
410	if (!ret)
411		priv->notify_min = true;
412	mutex_unlock(&priv->access_lock);
413	if (ret < 0)
414		return ret;
415
416	return snprintf(buf, PAGE_SIZE, "%d\n", priv->min_alert);
417}
418
419static ssize_t show_input(struct device *dev, struct device_attribute *attr,
420			  char *buf)
421{
422	int ret;
423	struct stts751_priv *priv = dev_get_drvdata(dev);
424
425	mutex_lock(&priv->access_lock);
426	ret = stts751_update(priv);
427	mutex_unlock(&priv->access_lock);
428	if (ret < 0)
429		return ret;
430
431	return snprintf(buf, PAGE_SIZE, "%d\n", priv->temp);
432}
433
434static ssize_t show_therm(struct device *dev, struct device_attribute *attr,
435			  char *buf)
436{
437	struct stts751_priv *priv = dev_get_drvdata(dev);
438
439	return snprintf(buf, PAGE_SIZE, "%d\n", priv->therm);
440}
441
442static ssize_t set_therm(struct device *dev, struct device_attribute *attr,
443			 const char *buf, size_t count)
444{
445	int ret;
446	long temp;
447	struct stts751_priv *priv = dev_get_drvdata(dev);
448
449	if (kstrtol(buf, 10, &temp) < 0)
450		return -EINVAL;
451
452	/* HW works in range -64C to +127.937C */
453	temp = clamp_val(temp, -64000, 127937);
454	mutex_lock(&priv->access_lock);
455	ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_TLIM);
456	if (ret)
457		goto exit;
458
459	dev_dbg(&priv->client->dev, "setting therm %ld", temp);
460
461	/*
462	 * hysteresis reg is relative to therm, so the HW does not need to be
463	 * adjusted, we need to update our local copy only.
464	 */
465	priv->hyst = temp - (priv->therm - priv->hyst);
466	priv->therm = temp;
467
468exit:
469	mutex_unlock(&priv->access_lock);
470	if (ret)
471		return ret;
472
473	return count;
474}
475
476static ssize_t show_hyst(struct device *dev, struct device_attribute *attr,
477			 char *buf)
478{
479	struct stts751_priv *priv = dev_get_drvdata(dev);
480
481	return snprintf(buf, PAGE_SIZE, "%d\n", priv->hyst);
482}
483
484static ssize_t set_hyst(struct device *dev, struct device_attribute *attr,
485			const char *buf, size_t count)
486{
487	int ret;
488	long temp;
489
490	struct stts751_priv *priv = dev_get_drvdata(dev);
491
492	if (kstrtol(buf, 10, &temp) < 0)
493		return -EINVAL;
494
495	mutex_lock(&priv->access_lock);
496	/* HW works in range -64C to +127.937C */
497	temp = clamp_val(temp, -64000, priv->therm);
498	priv->hyst = temp;
499	dev_dbg(&priv->client->dev, "setting hyst %ld", temp);
500	temp = priv->therm - temp;
501	ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_HYST);
502	mutex_unlock(&priv->access_lock);
503	if (ret)
504		return ret;
505
506	return count;
507}
508
509static ssize_t show_therm_trip(struct device *dev,
510			       struct device_attribute *attr, char *buf)
511{
512	int ret;
513	struct stts751_priv *priv = dev_get_drvdata(dev);
514
515	mutex_lock(&priv->access_lock);
516	ret = stts751_update(priv);
517	mutex_unlock(&priv->access_lock);
518	if (ret < 0)
519		return ret;
520
521	return snprintf(buf, PAGE_SIZE, "%d\n", priv->therm_trip);
522}
523
524static ssize_t show_max(struct device *dev, struct device_attribute *attr,
525			char *buf)
526{
527	struct stts751_priv *priv = dev_get_drvdata(dev);
528
529	return snprintf(buf, PAGE_SIZE, "%d\n", priv->event_max);
530}
531
532static ssize_t set_max(struct device *dev, struct device_attribute *attr,
533		       const char *buf, size_t count)
534{
535	int ret;
536	long temp;
537	struct stts751_priv *priv = dev_get_drvdata(dev);
538
539	if (kstrtol(buf, 10, &temp) < 0)
540		return -EINVAL;
541
542	mutex_lock(&priv->access_lock);
543	/* HW works in range -64C to +127.937C */
544	temp = clamp_val(temp, priv->event_min, 127937);
545	ret = stts751_set_temp_reg16(priv, temp,
546				     STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
547	if (ret)
548		goto exit;
549
550	dev_dbg(&priv->client->dev, "setting event max %ld", temp);
551	priv->event_max = temp;
552	ret = count;
553exit:
554	mutex_unlock(&priv->access_lock);
555	return ret;
556}
557
558static ssize_t show_min(struct device *dev, struct device_attribute *attr,
559			char *buf)
560{
561	struct stts751_priv *priv = dev_get_drvdata(dev);
562
563	return snprintf(buf, PAGE_SIZE, "%d\n", priv->event_min);
564}
565
566static ssize_t set_min(struct device *dev, struct device_attribute *attr,
567		       const char *buf, size_t count)
568{
569	int ret;
570	long temp;
571	struct stts751_priv *priv = dev_get_drvdata(dev);
572
573	if (kstrtol(buf, 10, &temp) < 0)
574		return -EINVAL;
575
576	mutex_lock(&priv->access_lock);
577	/* HW works in range -64C to +127.937C */
578	temp = clamp_val(temp, -64000, priv->event_max);
579	ret = stts751_set_temp_reg16(priv, temp,
580				     STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
581	if (ret)
582		goto exit;
583
584	dev_dbg(&priv->client->dev, "setting event min %ld", temp);
585	priv->event_min = temp;
586	ret = count;
587exit:
588	mutex_unlock(&priv->access_lock);
589	return ret;
590}
591
592static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
593			     char *buf)
594{
595	struct stts751_priv *priv = dev_get_drvdata(dev);
596
597	return snprintf(buf, PAGE_SIZE, "%d\n",
598			stts751_intervals[priv->interval]);
599}
600
601static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
602			    const char *buf, size_t count)
 
603{
604	unsigned long val;
605	int idx;
606	int ret = count;
607	struct stts751_priv *priv = dev_get_drvdata(dev);
608
609	if (kstrtoul(buf, 10, &val) < 0)
610		return -EINVAL;
611
612	idx = find_closest_descending(val, stts751_intervals,
613				      ARRAY_SIZE(stts751_intervals));
614
615	dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d",
616		val, idx, stts751_intervals[idx]);
617
618	mutex_lock(&priv->access_lock);
619	if (priv->interval == idx)
620		goto exit;
621
622	/*
623	 * In early development stages I've become suspicious about the chip
624	 * starting to misbehave if I ever set, even briefly, an invalid
625	 * configuration. While I'm not sure this is really needed, be
626	 * conservative and set rate/resolution in such an order that avoids
627	 * passing through an invalid configuration.
628	 */
629
630	/* speed up: lower the resolution, then modify convrate */
631	if (priv->interval < idx) {
632		dev_dbg(&priv->client->dev, "lower resolution, then modify convrate");
633		priv->interval = idx;
634		ret = stts751_adjust_resolution(priv);
635		if (ret)
636			goto exit;
637	}
638
639	ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx);
640	if (ret)
641		goto exit;
642	/* slow down: modify convrate, then raise resolution */
643	if (priv->interval != idx) {
644		dev_dbg(&priv->client->dev, "modify convrate, then raise resolution");
645		priv->interval = idx;
646		ret = stts751_adjust_resolution(priv);
647		if (ret)
648			goto exit;
649	}
650	ret = count;
651exit:
652	mutex_unlock(&priv->access_lock);
653
654	return ret;
655}
656
657static int stts751_detect(struct i2c_client *new_client,
658			  struct i2c_board_info *info)
659{
660	struct i2c_adapter *adapter = new_client->adapter;
661	const char *name;
662	int tmp;
663
664	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
665		return -ENODEV;
666
667	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_MAN_ID);
668	if (tmp != ST_MAN_ID)
669		return -ENODEV;
670
671	/* lower temperaure registers always have bits 0-3 set to zero */
672	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_TEMP_L);
673	if (tmp & 0xf)
674		return -ENODEV;
675
676	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_HLIM_L);
677	if (tmp & 0xf)
678		return -ENODEV;
679
680	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_LLIM_L);
681	if (tmp & 0xf)
682		return -ENODEV;
683
684	/* smbus timeout register always have bits 0-7 set to zero */
685	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_SMBUS_TO);
686	if (tmp & 0x7f)
687		return -ENODEV;
688
689	tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_PROD_ID);
690
691	switch (tmp) {
692	case STTS751_0_PROD_ID:
693		name = "STTS751-0";
694		break;
695	case STTS751_1_PROD_ID:
696		name = "STTS751-1";
697		break;
698	default:
699		return -ENODEV;
700	}
701	dev_dbg(&new_client->dev, "Chip %s detected", name);
702
703	strlcpy(info->type, stts751_id[0].name, I2C_NAME_SIZE);
704	return 0;
705}
706
707static int stts751_read_chip_config(struct stts751_priv *priv)
708{
709	int ret;
710	int tmp;
711
712	ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF);
713	if (ret < 0)
714		return ret;
715	priv->config = ret;
716	priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT;
717
718	ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE);
719	if (ret < 0)
720		return ret;
721	if (ret >= ARRAY_SIZE(stts751_intervals)) {
722		dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret);
723		return -ENODEV;
724	}
725	priv->interval = ret;
726
727	ret = stts751_read_reg16(priv, &priv->event_max,
728				 STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
729	if (ret)
730		return ret;
731
732	ret = stts751_read_reg16(priv, &priv->event_min,
733				 STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
734	if (ret)
735		return ret;
736
737	ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM);
738	if (ret)
739		return ret;
740
741	ret = stts751_read_reg8(priv, &tmp, STTS751_REG_HYST);
742	if (ret)
743		return ret;
744	priv->hyst = priv->therm - tmp;
745
746	return 0;
747}
748
749static SENSOR_DEVICE_ATTR(temp1_input, 0444, show_input, NULL, 0);
750static SENSOR_DEVICE_ATTR(temp1_min, 0644, show_min, set_min, 0);
751static SENSOR_DEVICE_ATTR(temp1_max, 0644, show_max, set_max, 0);
752static SENSOR_DEVICE_ATTR(temp1_min_alarm, 0444, show_min_alarm, NULL, 0);
753static SENSOR_DEVICE_ATTR(temp1_max_alarm, 0444, show_max_alarm, NULL, 0);
754static SENSOR_DEVICE_ATTR(temp1_crit, 0644, show_therm,	set_therm, 0);
755static SENSOR_DEVICE_ATTR(temp1_crit_hyst, 0644, show_hyst, set_hyst, 0);
756static SENSOR_DEVICE_ATTR(temp1_crit_alarm, 0444, show_therm_trip, NULL, 0);
757static SENSOR_DEVICE_ATTR(update_interval, 0644,
758			  show_interval, set_interval, 0);
759
760static struct attribute *stts751_attrs[] = {
761	&sensor_dev_attr_temp1_input.dev_attr.attr,
762	&sensor_dev_attr_temp1_min.dev_attr.attr,
763	&sensor_dev_attr_temp1_max.dev_attr.attr,
764	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
765	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
766	&sensor_dev_attr_temp1_crit.dev_attr.attr,
767	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
768	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
769	&sensor_dev_attr_update_interval.dev_attr.attr,
770	NULL
771};
772ATTRIBUTE_GROUPS(stts751);
773
774static int stts751_probe(struct i2c_client *client,
775			 const struct i2c_device_id *id)
776{
777	struct stts751_priv *priv;
778	int ret;
779	bool smbus_nto;
780	int rev_id;
781
782	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
783	if (!priv)
784		return -ENOMEM;
785
786	priv->client = client;
787	priv->notify_max = true;
788	priv->notify_min = true;
789	i2c_set_clientdata(client, priv);
790	mutex_init(&priv->access_lock);
791
792	if (device_property_present(&client->dev,
793				    "smbus-timeout-disable")) {
794		smbus_nto = device_property_read_bool(&client->dev,
795						      "smbus-timeout-disable");
796
797		ret = i2c_smbus_write_byte_data(client,	STTS751_REG_SMBUS_TO,
798						smbus_nto ? 0 : 0x80);
799		if (ret)
800			return ret;
801	}
802
803	rev_id = i2c_smbus_read_byte_data(client, STTS751_REG_REV_ID);
804	if (rev_id < 0)
805		return -ENODEV;
806	if (rev_id != 0x1) {
807		dev_dbg(&client->dev, "Chip revision 0x%x is untested\n",
808			rev_id);
809	}
810
811	ret = stts751_read_chip_config(priv);
812	if (ret)
813		return ret;
814
815	priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS);
816	ret = i2c_smbus_write_byte_data(client,	STTS751_REG_CONF, priv->config);
817	if (ret)
818		return ret;
819
820	priv->dev = devm_hwmon_device_register_with_groups(&client->dev,
821							client->name, priv,
822							stts751_groups);
823	return PTR_ERR_OR_ZERO(priv->dev);
824}
825
826MODULE_DEVICE_TABLE(i2c, stts751_id);
827
828static struct i2c_driver stts751_driver = {
829	.class		= I2C_CLASS_HWMON,
830	.driver = {
831		.name	= DEVNAME,
832		.of_match_table = of_match_ptr(stts751_of_match),
833	},
834	.probe		= stts751_probe,
835	.id_table	= stts751_id,
836	.detect		= stts751_detect,
837	.alert		= stts751_alert,
838	.address_list	= normal_i2c,
839};
840
841module_i2c_driver(stts751_driver);
842
843MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
844MODULE_DESCRIPTION("STTS751 sensor driver");
845MODULE_LICENSE("GPL");