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