blob: 29388fcf5f74d64617acaeb649dbe42a98e59af9 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alexander Steinfffd80c2011-06-28 15:11:23 +00002/*
3 * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
4 *
Guenter Roeck162a8df2014-04-22 08:48:57 -07005 * The LM95245 is a sensor chip made by TI / National Semiconductor.
Alexander Steinfffd80c2011-06-28 15:11:23 +00006 * It reports up to two temperatures (its own plus an external one).
Alexander Steinfffd80c2011-06-28 15:11:23 +00007 *
8 * This driver is based on lm95241.c
Alexander Steinfffd80c2011-06-28 15:11:23 +00009 */
10
Alexander Steinfffd80c2011-06-28 15:11:23 +000011#include <linux/err.h>
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070012#include <linux/init.h>
13#include <linux/hwmon.h>
14#include <linux/i2c.h>
15#include <linux/module.h>
Alexander Steinfffd80c2011-06-28 15:11:23 +000016#include <linux/mutex.h>
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070017#include <linux/regmap.h>
18#include <linux/slab.h>
Alexander Steinfffd80c2011-06-28 15:11:23 +000019
Alexander Steinfffd80c2011-06-28 15:11:23 +000020static const unsigned short normal_i2c[] = {
21 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END };
22
23/* LM95245 registers */
24/* general registers */
25#define LM95245_REG_RW_CONFIG1 0x03
26#define LM95245_REG_RW_CONVERS_RATE 0x04
27#define LM95245_REG_W_ONE_SHOT 0x0F
28
29/* diode configuration */
30#define LM95245_REG_RW_CONFIG2 0xBF
31#define LM95245_REG_RW_REMOTE_OFFH 0x11
32#define LM95245_REG_RW_REMOTE_OFFL 0x12
33
34/* status registers */
35#define LM95245_REG_R_STATUS1 0x02
36#define LM95245_REG_R_STATUS2 0x33
37
38/* limit registers */
39#define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
40#define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
41#define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
42#define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
43
44/* temperature signed */
45#define LM95245_REG_R_LOCAL_TEMPH_S 0x00
46#define LM95245_REG_R_LOCAL_TEMPL_S 0x30
47#define LM95245_REG_R_REMOTE_TEMPH_S 0x01
48#define LM95245_REG_R_REMOTE_TEMPL_S 0x10
49/* temperature unsigned */
50#define LM95245_REG_R_REMOTE_TEMPH_U 0x31
51#define LM95245_REG_R_REMOTE_TEMPL_U 0x32
52
53/* id registers */
54#define LM95245_REG_R_MAN_ID 0xFE
55#define LM95245_REG_R_CHIP_ID 0xFF
56
57/* LM95245 specific bitfields */
58#define CFG_STOP 0x40
59#define CFG_REMOTE_TCRIT_MASK 0x10
60#define CFG_REMOTE_OS_MASK 0x08
61#define CFG_LOCAL_TCRIT_MASK 0x04
62#define CFG_LOCAL_OS_MASK 0x02
63
64#define CFG2_OS_A0 0x40
65#define CFG2_DIODE_FAULT_OS 0x20
66#define CFG2_DIODE_FAULT_TCRIT 0x10
67#define CFG2_REMOTE_TT 0x08
68#define CFG2_REMOTE_FILTER_DIS 0x00
69#define CFG2_REMOTE_FILTER_EN 0x06
70
71/* conversation rate in ms */
72#define RATE_CR0063 0x00
73#define RATE_CR0364 0x01
74#define RATE_CR1000 0x02
75#define RATE_CR2500 0x03
76
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070077#define STATUS1_ROS 0x10
Alexander Steinfffd80c2011-06-28 15:11:23 +000078#define STATUS1_DIODE_FAULT 0x04
79#define STATUS1_RTCRIT 0x02
80#define STATUS1_LOC 0x01
81
82#define MANUFACTURER_ID 0x01
Guenter Roeck162a8df2014-04-22 08:48:57 -070083#define LM95235_REVISION 0xB1
84#define LM95245_REVISION 0xB3
Alexander Steinfffd80c2011-06-28 15:11:23 +000085
Alexander Steinfffd80c2011-06-28 15:11:23 +000086/* Client data (each client gets its own) */
87struct lm95245_data {
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070088 struct regmap *regmap;
Alexander Steinfffd80c2011-06-28 15:11:23 +000089 struct mutex update_lock;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070090 int interval; /* in msecs */
Alexander Steinfffd80c2011-06-28 15:11:23 +000091};
92
93/* Conversions */
94static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
95{
96 return val_h * 1000 + val_l * 1000 / 256;
97}
98
99static int temp_from_reg_signed(u8 val_h, u8 val_l)
100{
101 if (val_h & 0x80)
102 return (val_h - 0x100) * 1000;
103 return temp_from_reg_unsigned(val_h, val_l);
104}
105
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700106static int lm95245_read_conversion_rate(struct lm95245_data *data)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000107{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700108 unsigned int rate;
109 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000110
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700111 ret = regmap_read(data->regmap, LM95245_REG_RW_CONVERS_RATE, &rate);
112 if (ret < 0)
113 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000114
115 switch (rate) {
116 case RATE_CR0063:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700117 data->interval = 63;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000118 break;
119 case RATE_CR0364:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700120 data->interval = 364;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000121 break;
122 case RATE_CR1000:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700123 data->interval = 1000;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000124 break;
125 case RATE_CR2500:
126 default:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700127 data->interval = 2500;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000128 break;
129 }
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700130 return 0;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000131}
132
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700133static int lm95245_set_conversion_rate(struct lm95245_data *data, long interval)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000134{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700135 int ret, rate;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000136
137 if (interval <= 63) {
138 interval = 63;
139 rate = RATE_CR0063;
140 } else if (interval <= 364) {
141 interval = 364;
142 rate = RATE_CR0364;
143 } else if (interval <= 1000) {
144 interval = 1000;
145 rate = RATE_CR1000;
146 } else {
147 interval = 2500;
148 rate = RATE_CR2500;
149 }
150
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700151 ret = regmap_write(data->regmap, LM95245_REG_RW_CONVERS_RATE, rate);
152 if (ret < 0)
153 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000154
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700155 data->interval = interval;
156 return 0;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000157}
158
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700159static int lm95245_read_temp(struct device *dev, u32 attr, int channel,
160 long *val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000161{
Guenter Roeck7276d552014-01-20 09:17:16 -0800162 struct lm95245_data *data = dev_get_drvdata(dev);
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700163 struct regmap *regmap = data->regmap;
164 int ret, regl, regh, regvall, regvalh;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000165
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700166 switch (attr) {
167 case hwmon_temp_input:
168 regl = channel ? LM95245_REG_R_REMOTE_TEMPL_S :
169 LM95245_REG_R_LOCAL_TEMPL_S;
170 regh = channel ? LM95245_REG_R_REMOTE_TEMPH_S :
171 LM95245_REG_R_LOCAL_TEMPH_S;
172 ret = regmap_read(regmap, regl, &regvall);
173 if (ret < 0)
174 return ret;
175 ret = regmap_read(regmap, regh, &regvalh);
176 if (ret < 0)
177 return ret;
178 /*
179 * Local temp is always signed.
180 * Remote temp has both signed and unsigned data.
181 * Use signed calculation for remote if signed bit is set
182 * or if reported temperature is below signed limit.
183 */
184 if (!channel || (regvalh & 0x80) || regvalh < 0x7f) {
185 *val = temp_from_reg_signed(regvalh, regvall);
186 return 0;
187 }
188 ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPL_U,
189 &regvall);
190 if (ret < 0)
191 return ret;
192 ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPH_U,
193 &regvalh);
194 if (ret < 0)
195 return ret;
196 *val = temp_from_reg_unsigned(regvalh, regvall);
197 return 0;
198 case hwmon_temp_max:
199 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
200 &regvalh);
201 if (ret < 0)
202 return ret;
203 *val = regvalh * 1000;
204 return 0;
205 case hwmon_temp_crit:
206 regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
207 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
208 ret = regmap_read(regmap, regh, &regvalh);
209 if (ret < 0)
210 return ret;
211 *val = regvalh * 1000;
212 return 0;
213 case hwmon_temp_max_hyst:
214 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
215 &regvalh);
216 if (ret < 0)
217 return ret;
218 ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
219 &regvall);
220 if (ret < 0)
221 return ret;
222 *val = (regvalh - regvall) * 1000;
223 return 0;
224 case hwmon_temp_crit_hyst:
225 regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
226 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
227 ret = regmap_read(regmap, regh, &regvalh);
228 if (ret < 0)
229 return ret;
230 ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
231 &regvall);
232 if (ret < 0)
233 return ret;
234 *val = (regvalh - regvall) * 1000;
235 return 0;
236 case hwmon_temp_type:
237 ret = regmap_read(regmap, LM95245_REG_RW_CONFIG2, &regvalh);
238 if (ret < 0)
239 return ret;
240 *val = (regvalh & CFG2_REMOTE_TT) ? 1 : 2;
241 return 0;
242 case hwmon_temp_offset:
243 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFL,
244 &regvall);
245 if (ret < 0)
246 return ret;
247 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFH,
248 &regvalh);
249 if (ret < 0)
250 return ret;
251 *val = temp_from_reg_signed(regvalh, regvall);
252 return 0;
253 case hwmon_temp_max_alarm:
254 ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
255 if (ret < 0)
256 return ret;
257 *val = !!(regvalh & STATUS1_ROS);
258 return 0;
259 case hwmon_temp_crit_alarm:
260 ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
261 if (ret < 0)
262 return ret;
263 *val = !!(regvalh & (channel ? STATUS1_RTCRIT : STATUS1_LOC));
264 return 0;
265 case hwmon_temp_fault:
266 ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
267 if (ret < 0)
268 return ret;
269 *val = !!(regvalh & STATUS1_DIODE_FAULT);
270 return 0;
271 default:
272 return -EOPNOTSUPP;
273 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000274}
275
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700276static int lm95245_write_temp(struct device *dev, u32 attr, int channel,
277 long val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000278{
Guenter Roeck7276d552014-01-20 09:17:16 -0800279 struct lm95245_data *data = dev_get_drvdata(dev);
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700280 struct regmap *regmap = data->regmap;
281 unsigned int regval;
282 int ret, reg;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000283
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700284 switch (attr) {
285 case hwmon_temp_max:
286 val = clamp_val(val / 1000, 0, 255);
287 ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, val);
288 return ret;
289 case hwmon_temp_crit:
290 reg = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
291 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
292 val = clamp_val(val / 1000, 0, channel ? 255 : 127);
293 ret = regmap_write(regmap, reg, val);
294 return ret;
295 case hwmon_temp_crit_hyst:
296 mutex_lock(&data->update_lock);
297 ret = regmap_read(regmap, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
298 &regval);
299 if (ret < 0) {
300 mutex_unlock(&data->update_lock);
301 return ret;
302 }
303 /* Clamp to reasonable range to prevent overflow */
304 val = clamp_val(val, -1000000, 1000000);
305 val = regval - val / 1000;
306 val = clamp_val(val, 0, 31);
307 ret = regmap_write(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
308 val);
309 mutex_unlock(&data->update_lock);
310 return ret;
311 case hwmon_temp_offset:
312 val = clamp_val(val, -128000, 127875);
313 val = val * 256 / 1000;
314 mutex_lock(&data->update_lock);
315 ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFL,
316 val & 0xe0);
317 if (ret < 0) {
318 mutex_unlock(&data->update_lock);
319 return ret;
320 }
321 ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFH,
322 (val >> 8) & 0xff);
323 mutex_unlock(&data->update_lock);
324 return ret;
325 case hwmon_temp_type:
326 if (val != 1 && val != 2)
327 return -EINVAL;
328 ret = regmap_update_bits(regmap, LM95245_REG_RW_CONFIG2,
329 CFG2_REMOTE_TT,
330 val == 1 ? CFG2_REMOTE_TT : 0);
331 return ret;
332 default:
333 return -EOPNOTSUPP;
334 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000335}
336
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700337static int lm95245_read_chip(struct device *dev, u32 attr, int channel,
338 long *val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000339{
Guenter Roeck7276d552014-01-20 09:17:16 -0800340 struct lm95245_data *data = dev_get_drvdata(dev);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000341
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700342 switch (attr) {
343 case hwmon_chip_update_interval:
344 *val = data->interval;
345 return 0;
346 default:
347 return -EOPNOTSUPP;
348 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000349}
350
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700351static int lm95245_write_chip(struct device *dev, u32 attr, int channel,
352 long val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000353{
Guenter Roeck7276d552014-01-20 09:17:16 -0800354 struct lm95245_data *data = dev_get_drvdata(dev);
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700355 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000356
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700357 switch (attr) {
358 case hwmon_chip_update_interval:
359 mutex_lock(&data->update_lock);
360 ret = lm95245_set_conversion_rate(data, val);
361 mutex_unlock(&data->update_lock);
362 return ret;
363 default:
364 return -EOPNOTSUPP;
365 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000366}
367
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700368static int lm95245_read(struct device *dev, enum hwmon_sensor_types type,
369 u32 attr, int channel, long *val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000370{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700371 switch (type) {
372 case hwmon_chip:
373 return lm95245_read_chip(dev, attr, channel, val);
374 case hwmon_temp:
375 return lm95245_read_temp(dev, attr, channel, val);
376 default:
377 return -EOPNOTSUPP;
378 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000379}
380
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700381static int lm95245_write(struct device *dev, enum hwmon_sensor_types type,
382 u32 attr, int channel, long val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000383{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700384 switch (type) {
385 case hwmon_chip:
386 return lm95245_write_chip(dev, attr, channel, val);
387 case hwmon_temp:
388 return lm95245_write_temp(dev, attr, channel, val);
389 default:
390 return -EOPNOTSUPP;
391 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000392}
393
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700394static umode_t lm95245_temp_is_visible(const void *data, u32 attr, int channel)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000395{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700396 switch (attr) {
397 case hwmon_temp_input:
398 case hwmon_temp_max_alarm:
399 case hwmon_temp_max_hyst:
400 case hwmon_temp_crit_alarm:
401 case hwmon_temp_fault:
Guenter Roeck5a2d18c2018-12-10 14:02:14 -0800402 return 0444;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700403 case hwmon_temp_type:
404 case hwmon_temp_max:
405 case hwmon_temp_crit:
406 case hwmon_temp_offset:
Guenter Roeck5a2d18c2018-12-10 14:02:14 -0800407 return 0644;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700408 case hwmon_temp_crit_hyst:
Guenter Roeck5a2d18c2018-12-10 14:02:14 -0800409 return (channel == 0) ? 0644 : 0444;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700410 default:
411 return 0;
412 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000413}
414
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700415static umode_t lm95245_is_visible(const void *data,
416 enum hwmon_sensor_types type,
417 u32 attr, int channel)
418{
419 switch (type) {
420 case hwmon_chip:
421 switch (attr) {
422 case hwmon_chip_update_interval:
Guenter Roeck5a2d18c2018-12-10 14:02:14 -0800423 return 0644;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700424 default:
425 return 0;
426 }
427 case hwmon_temp:
428 return lm95245_temp_is_visible(data, attr, channel);
429 default:
430 return 0;
431 }
432}
Alexander Steinfffd80c2011-06-28 15:11:23 +0000433
434/* Return 0 if detection is successful, -ENODEV otherwise */
435static int lm95245_detect(struct i2c_client *new_client,
436 struct i2c_board_info *info)
437{
438 struct i2c_adapter *adapter = new_client->adapter;
Guenter Roeck162a8df2014-04-22 08:48:57 -0700439 int address = new_client->addr;
440 const char *name;
441 int rev, id;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000442
443 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
444 return -ENODEV;
445
Guenter Roeck162a8df2014-04-22 08:48:57 -0700446 id = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID);
447 if (id != MANUFACTURER_ID)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000448 return -ENODEV;
449
Guenter Roeck162a8df2014-04-22 08:48:57 -0700450 rev = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID);
451 switch (rev) {
452 case LM95235_REVISION:
453 if (address != 0x18 && address != 0x29 && address != 0x4c)
454 return -ENODEV;
455 name = "lm95235";
456 break;
457 case LM95245_REVISION:
458 name = "lm95245";
459 break;
460 default:
461 return -ENODEV;
462 }
463
464 strlcpy(info->type, name, I2C_NAME_SIZE);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000465 return 0;
466}
467
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700468static int lm95245_init_client(struct lm95245_data *data)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000469{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700470 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000471
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700472 ret = lm95245_read_conversion_rate(data);
473 if (ret < 0)
474 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000475
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700476 return regmap_update_bits(data->regmap, LM95245_REG_RW_CONFIG1,
477 CFG_STOP, 0);
478}
479
480static bool lm95245_is_writeable_reg(struct device *dev, unsigned int reg)
481{
482 switch (reg) {
483 case LM95245_REG_RW_CONFIG1:
484 case LM95245_REG_RW_CONVERS_RATE:
485 case LM95245_REG_W_ONE_SHOT:
486 case LM95245_REG_RW_CONFIG2:
487 case LM95245_REG_RW_REMOTE_OFFH:
488 case LM95245_REG_RW_REMOTE_OFFL:
489 case LM95245_REG_RW_REMOTE_OS_LIMIT:
490 case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT:
491 case LM95245_REG_RW_REMOTE_TCRIT_LIMIT:
492 case LM95245_REG_RW_COMMON_HYSTERESIS:
493 return true;
494 default:
495 return false;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000496 }
497}
498
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700499static bool lm95245_is_volatile_reg(struct device *dev, unsigned int reg)
500{
501 switch (reg) {
502 case LM95245_REG_R_STATUS1:
503 case LM95245_REG_R_STATUS2:
504 case LM95245_REG_R_LOCAL_TEMPH_S:
505 case LM95245_REG_R_LOCAL_TEMPL_S:
506 case LM95245_REG_R_REMOTE_TEMPH_S:
507 case LM95245_REG_R_REMOTE_TEMPL_S:
508 case LM95245_REG_R_REMOTE_TEMPH_U:
509 case LM95245_REG_R_REMOTE_TEMPL_U:
510 return true;
511 default:
512 return false;
513 }
514}
515
516static const struct regmap_config lm95245_regmap_config = {
517 .reg_bits = 8,
518 .val_bits = 8,
519 .writeable_reg = lm95245_is_writeable_reg,
520 .volatile_reg = lm95245_is_volatile_reg,
521 .cache_type = REGCACHE_RBTREE,
David Frey1c96a2f2018-09-01 09:50:41 -0700522 .use_single_read = true,
523 .use_single_write = true,
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700524};
525
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700526static const struct hwmon_channel_info *lm95245_info[] = {
Guenter Roeck7aea0062019-03-31 10:53:48 -0700527 HWMON_CHANNEL_INFO(chip,
528 HWMON_C_UPDATE_INTERVAL),
529 HWMON_CHANNEL_INFO(temp,
530 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_HYST |
531 HWMON_T_CRIT_ALARM,
532 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
533 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_FAULT |
534 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM |
535 HWMON_T_TYPE | HWMON_T_OFFSET),
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700536 NULL
537};
538
539static const struct hwmon_ops lm95245_hwmon_ops = {
540 .is_visible = lm95245_is_visible,
541 .read = lm95245_read,
542 .write = lm95245_write,
543};
544
545static const struct hwmon_chip_info lm95245_chip_info = {
546 .ops = &lm95245_hwmon_ops,
547 .info = lm95245_info,
548};
549
Stephen Kitt67487032020-08-13 18:02:22 +0200550static int lm95245_probe(struct i2c_client *client)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000551{
Guenter Roeck7276d552014-01-20 09:17:16 -0800552 struct device *dev = &client->dev;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000553 struct lm95245_data *data;
Guenter Roeck7276d552014-01-20 09:17:16 -0800554 struct device *hwmon_dev;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700555 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000556
Guenter Roeck7276d552014-01-20 09:17:16 -0800557 data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL);
Guenter Roecka8dd9462012-06-02 09:58:12 -0700558 if (!data)
559 return -ENOMEM;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000560
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700561 data->regmap = devm_regmap_init_i2c(client, &lm95245_regmap_config);
562 if (IS_ERR(data->regmap))
563 return PTR_ERR(data->regmap);
564
Alexander Steinfffd80c2011-06-28 15:11:23 +0000565 mutex_init(&data->update_lock);
566
567 /* Initialize the LM95245 chip */
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700568 ret = lm95245_init_client(data);
569 if (ret < 0)
570 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000571
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700572 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
573 data,
574 &lm95245_chip_info,
575 NULL);
Guenter Roeck7276d552014-01-20 09:17:16 -0800576 return PTR_ERR_OR_ZERO(hwmon_dev);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000577}
578
579/* Driver data (common to all clients) */
580static const struct i2c_device_id lm95245_id[] = {
Guenter Roeck162a8df2014-04-22 08:48:57 -0700581 { "lm95235", 0 },
582 { "lm95245", 0 },
Alexander Steinfffd80c2011-06-28 15:11:23 +0000583 { }
584};
585MODULE_DEVICE_TABLE(i2c, lm95245_id);
586
Guenter Roeck0e09e9f2019-04-04 07:52:05 -0700587static const struct of_device_id __maybe_unused lm95245_of_match[] = {
Javier Martinez Canillas5ada7052017-02-24 10:13:05 -0300588 { .compatible = "national,lm95235" },
589 { .compatible = "national,lm95245" },
590 { },
591};
592MODULE_DEVICE_TABLE(of, lm95245_of_match);
593
Alexander Steinfffd80c2011-06-28 15:11:23 +0000594static struct i2c_driver lm95245_driver = {
595 .class = I2C_CLASS_HWMON,
596 .driver = {
Guenter Roeck162a8df2014-04-22 08:48:57 -0700597 .name = "lm95245",
Javier Martinez Canillas5ada7052017-02-24 10:13:05 -0300598 .of_match_table = of_match_ptr(lm95245_of_match),
Alexander Steinfffd80c2011-06-28 15:11:23 +0000599 },
Stephen Kitt67487032020-08-13 18:02:22 +0200600 .probe_new = lm95245_probe,
Alexander Steinfffd80c2011-06-28 15:11:23 +0000601 .id_table = lm95245_id,
602 .detect = lm95245_detect,
603 .address_list = normal_i2c,
604};
605
Axel Linf0967ee2012-01-20 15:38:18 +0800606module_i2c_driver(lm95245_driver);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000607
608MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
Guenter Roeck162a8df2014-04-22 08:48:57 -0700609MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
Alexander Steinfffd80c2011-06-28 15:11:23 +0000610MODULE_LICENSE("GPL");