blob: a3bfd88752ca253281bb15f5d3f55dafb3a92039 [file] [log] [blame]
Alexander Steinfffd80c2011-06-28 15:11:23 +00001/*
2 * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
3 *
Guenter Roeck162a8df2014-04-22 08:48:57 -07004 * The LM95245 is a sensor chip made by TI / National Semiconductor.
Alexander Steinfffd80c2011-06-28 15:11:23 +00005 * It reports up to two temperatures (its own plus an external one).
Alexander Steinfffd80c2011-06-28 15:11:23 +00006 *
7 * This driver is based on lm95241.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Alexander Steinfffd80c2011-06-28 15:11:23 +000018 */
19
Alexander Steinfffd80c2011-06-28 15:11:23 +000020#include <linux/err.h>
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070021#include <linux/init.h>
22#include <linux/hwmon.h>
23#include <linux/i2c.h>
24#include <linux/module.h>
Alexander Steinfffd80c2011-06-28 15:11:23 +000025#include <linux/mutex.h>
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070026#include <linux/regmap.h>
27#include <linux/slab.h>
Alexander Steinfffd80c2011-06-28 15:11:23 +000028
Alexander Steinfffd80c2011-06-28 15:11:23 +000029static const unsigned short normal_i2c[] = {
30 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END };
31
32/* LM95245 registers */
33/* general registers */
34#define LM95245_REG_RW_CONFIG1 0x03
35#define LM95245_REG_RW_CONVERS_RATE 0x04
36#define LM95245_REG_W_ONE_SHOT 0x0F
37
38/* diode configuration */
39#define LM95245_REG_RW_CONFIG2 0xBF
40#define LM95245_REG_RW_REMOTE_OFFH 0x11
41#define LM95245_REG_RW_REMOTE_OFFL 0x12
42
43/* status registers */
44#define LM95245_REG_R_STATUS1 0x02
45#define LM95245_REG_R_STATUS2 0x33
46
47/* limit registers */
48#define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
49#define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
50#define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
51#define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
52
53/* temperature signed */
54#define LM95245_REG_R_LOCAL_TEMPH_S 0x00
55#define LM95245_REG_R_LOCAL_TEMPL_S 0x30
56#define LM95245_REG_R_REMOTE_TEMPH_S 0x01
57#define LM95245_REG_R_REMOTE_TEMPL_S 0x10
58/* temperature unsigned */
59#define LM95245_REG_R_REMOTE_TEMPH_U 0x31
60#define LM95245_REG_R_REMOTE_TEMPL_U 0x32
61
62/* id registers */
63#define LM95245_REG_R_MAN_ID 0xFE
64#define LM95245_REG_R_CHIP_ID 0xFF
65
66/* LM95245 specific bitfields */
67#define CFG_STOP 0x40
68#define CFG_REMOTE_TCRIT_MASK 0x10
69#define CFG_REMOTE_OS_MASK 0x08
70#define CFG_LOCAL_TCRIT_MASK 0x04
71#define CFG_LOCAL_OS_MASK 0x02
72
73#define CFG2_OS_A0 0x40
74#define CFG2_DIODE_FAULT_OS 0x20
75#define CFG2_DIODE_FAULT_TCRIT 0x10
76#define CFG2_REMOTE_TT 0x08
77#define CFG2_REMOTE_FILTER_DIS 0x00
78#define CFG2_REMOTE_FILTER_EN 0x06
79
80/* conversation rate in ms */
81#define RATE_CR0063 0x00
82#define RATE_CR0364 0x01
83#define RATE_CR1000 0x02
84#define RATE_CR2500 0x03
85
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -070086#define STATUS1_ROS 0x10
Alexander Steinfffd80c2011-06-28 15:11:23 +000087#define STATUS1_DIODE_FAULT 0x04
88#define STATUS1_RTCRIT 0x02
89#define STATUS1_LOC 0x01
90
91#define MANUFACTURER_ID 0x01
Guenter Roeck162a8df2014-04-22 08:48:57 -070092#define LM95235_REVISION 0xB1
93#define LM95245_REVISION 0xB3
Alexander Steinfffd80c2011-06-28 15:11:23 +000094
95static const u8 lm95245_reg_address[] = {
96 LM95245_REG_R_LOCAL_TEMPH_S,
97 LM95245_REG_R_LOCAL_TEMPL_S,
98 LM95245_REG_R_REMOTE_TEMPH_S,
99 LM95245_REG_R_REMOTE_TEMPL_S,
100 LM95245_REG_R_REMOTE_TEMPH_U,
101 LM95245_REG_R_REMOTE_TEMPL_U,
102 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
103 LM95245_REG_RW_REMOTE_TCRIT_LIMIT,
104 LM95245_REG_RW_COMMON_HYSTERESIS,
105 LM95245_REG_R_STATUS1,
106};
107
108/* Client data (each client gets its own) */
109struct lm95245_data {
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700110 struct regmap *regmap;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000111 struct mutex update_lock;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700112 int interval; /* in msecs */
Alexander Steinfffd80c2011-06-28 15:11:23 +0000113};
114
115/* Conversions */
116static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
117{
118 return val_h * 1000 + val_l * 1000 / 256;
119}
120
121static int temp_from_reg_signed(u8 val_h, u8 val_l)
122{
123 if (val_h & 0x80)
124 return (val_h - 0x100) * 1000;
125 return temp_from_reg_unsigned(val_h, val_l);
126}
127
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700128static int lm95245_read_conversion_rate(struct lm95245_data *data)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000129{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700130 unsigned int rate;
131 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000132
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700133 ret = regmap_read(data->regmap, LM95245_REG_RW_CONVERS_RATE, &rate);
134 if (ret < 0)
135 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000136
137 switch (rate) {
138 case RATE_CR0063:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700139 data->interval = 63;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000140 break;
141 case RATE_CR0364:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700142 data->interval = 364;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000143 break;
144 case RATE_CR1000:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700145 data->interval = 1000;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000146 break;
147 case RATE_CR2500:
148 default:
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700149 data->interval = 2500;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000150 break;
151 }
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700152 return 0;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000153}
154
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700155static int lm95245_set_conversion_rate(struct lm95245_data *data, long interval)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000156{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700157 int ret, rate;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000158
159 if (interval <= 63) {
160 interval = 63;
161 rate = RATE_CR0063;
162 } else if (interval <= 364) {
163 interval = 364;
164 rate = RATE_CR0364;
165 } else if (interval <= 1000) {
166 interval = 1000;
167 rate = RATE_CR1000;
168 } else {
169 interval = 2500;
170 rate = RATE_CR2500;
171 }
172
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700173 ret = regmap_write(data->regmap, LM95245_REG_RW_CONVERS_RATE, rate);
174 if (ret < 0)
175 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000176
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700177 data->interval = interval;
178 return 0;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000179}
180
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700181static int lm95245_read_temp(struct device *dev, u32 attr, int channel,
182 long *val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000183{
Guenter Roeck7276d552014-01-20 09:17:16 -0800184 struct lm95245_data *data = dev_get_drvdata(dev);
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700185 struct regmap *regmap = data->regmap;
186 int ret, regl, regh, regvall, regvalh;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000187
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700188 switch (attr) {
189 case hwmon_temp_input:
190 regl = channel ? LM95245_REG_R_REMOTE_TEMPL_S :
191 LM95245_REG_R_LOCAL_TEMPL_S;
192 regh = channel ? LM95245_REG_R_REMOTE_TEMPH_S :
193 LM95245_REG_R_LOCAL_TEMPH_S;
194 ret = regmap_read(regmap, regl, &regvall);
195 if (ret < 0)
196 return ret;
197 ret = regmap_read(regmap, regh, &regvalh);
198 if (ret < 0)
199 return ret;
200 /*
201 * Local temp is always signed.
202 * Remote temp has both signed and unsigned data.
203 * Use signed calculation for remote if signed bit is set
204 * or if reported temperature is below signed limit.
205 */
206 if (!channel || (regvalh & 0x80) || regvalh < 0x7f) {
207 *val = temp_from_reg_signed(regvalh, regvall);
208 return 0;
209 }
210 ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPL_U,
211 &regvall);
212 if (ret < 0)
213 return ret;
214 ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPH_U,
215 &regvalh);
216 if (ret < 0)
217 return ret;
218 *val = temp_from_reg_unsigned(regvalh, regvall);
219 return 0;
220 case hwmon_temp_max:
221 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
222 &regvalh);
223 if (ret < 0)
224 return ret;
225 *val = regvalh * 1000;
226 return 0;
227 case hwmon_temp_crit:
228 regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
229 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
230 ret = regmap_read(regmap, regh, &regvalh);
231 if (ret < 0)
232 return ret;
233 *val = regvalh * 1000;
234 return 0;
235 case hwmon_temp_max_hyst:
236 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
237 &regvalh);
238 if (ret < 0)
239 return ret;
240 ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
241 &regvall);
242 if (ret < 0)
243 return ret;
244 *val = (regvalh - regvall) * 1000;
245 return 0;
246 case hwmon_temp_crit_hyst:
247 regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
248 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
249 ret = regmap_read(regmap, regh, &regvalh);
250 if (ret < 0)
251 return ret;
252 ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
253 &regvall);
254 if (ret < 0)
255 return ret;
256 *val = (regvalh - regvall) * 1000;
257 return 0;
258 case hwmon_temp_type:
259 ret = regmap_read(regmap, LM95245_REG_RW_CONFIG2, &regvalh);
260 if (ret < 0)
261 return ret;
262 *val = (regvalh & CFG2_REMOTE_TT) ? 1 : 2;
263 return 0;
264 case hwmon_temp_offset:
265 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFL,
266 &regvall);
267 if (ret < 0)
268 return ret;
269 ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFH,
270 &regvalh);
271 if (ret < 0)
272 return ret;
273 *val = temp_from_reg_signed(regvalh, regvall);
274 return 0;
275 case hwmon_temp_max_alarm:
276 ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
277 if (ret < 0)
278 return ret;
279 *val = !!(regvalh & STATUS1_ROS);
280 return 0;
281 case hwmon_temp_crit_alarm:
282 ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
283 if (ret < 0)
284 return ret;
285 *val = !!(regvalh & (channel ? STATUS1_RTCRIT : STATUS1_LOC));
286 return 0;
287 case hwmon_temp_fault:
288 ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
289 if (ret < 0)
290 return ret;
291 *val = !!(regvalh & STATUS1_DIODE_FAULT);
292 return 0;
293 default:
294 return -EOPNOTSUPP;
295 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000296}
297
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700298static int lm95245_write_temp(struct device *dev, u32 attr, int channel,
299 long val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000300{
Guenter Roeck7276d552014-01-20 09:17:16 -0800301 struct lm95245_data *data = dev_get_drvdata(dev);
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700302 struct regmap *regmap = data->regmap;
303 unsigned int regval;
304 int ret, reg;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000305
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700306 switch (attr) {
307 case hwmon_temp_max:
308 val = clamp_val(val / 1000, 0, 255);
309 ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, val);
310 return ret;
311 case hwmon_temp_crit:
312 reg = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
313 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
314 val = clamp_val(val / 1000, 0, channel ? 255 : 127);
315 ret = regmap_write(regmap, reg, val);
316 return ret;
317 case hwmon_temp_crit_hyst:
318 mutex_lock(&data->update_lock);
319 ret = regmap_read(regmap, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
320 &regval);
321 if (ret < 0) {
322 mutex_unlock(&data->update_lock);
323 return ret;
324 }
325 /* Clamp to reasonable range to prevent overflow */
326 val = clamp_val(val, -1000000, 1000000);
327 val = regval - val / 1000;
328 val = clamp_val(val, 0, 31);
329 ret = regmap_write(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
330 val);
331 mutex_unlock(&data->update_lock);
332 return ret;
333 case hwmon_temp_offset:
334 val = clamp_val(val, -128000, 127875);
335 val = val * 256 / 1000;
336 mutex_lock(&data->update_lock);
337 ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFL,
338 val & 0xe0);
339 if (ret < 0) {
340 mutex_unlock(&data->update_lock);
341 return ret;
342 }
343 ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFH,
344 (val >> 8) & 0xff);
345 mutex_unlock(&data->update_lock);
346 return ret;
347 case hwmon_temp_type:
348 if (val != 1 && val != 2)
349 return -EINVAL;
350 ret = regmap_update_bits(regmap, LM95245_REG_RW_CONFIG2,
351 CFG2_REMOTE_TT,
352 val == 1 ? CFG2_REMOTE_TT : 0);
353 return ret;
354 default:
355 return -EOPNOTSUPP;
356 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000357}
358
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700359static int lm95245_read_chip(struct device *dev, u32 attr, int channel,
360 long *val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000361{
Guenter Roeck7276d552014-01-20 09:17:16 -0800362 struct lm95245_data *data = dev_get_drvdata(dev);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000363
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700364 switch (attr) {
365 case hwmon_chip_update_interval:
366 *val = data->interval;
367 return 0;
368 default:
369 return -EOPNOTSUPP;
370 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000371}
372
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700373static int lm95245_write_chip(struct device *dev, u32 attr, int channel,
374 long val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000375{
Guenter Roeck7276d552014-01-20 09:17:16 -0800376 struct lm95245_data *data = dev_get_drvdata(dev);
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700377 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000378
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700379 switch (attr) {
380 case hwmon_chip_update_interval:
381 mutex_lock(&data->update_lock);
382 ret = lm95245_set_conversion_rate(data, val);
383 mutex_unlock(&data->update_lock);
384 return ret;
385 default:
386 return -EOPNOTSUPP;
387 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000388}
389
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700390static int lm95245_read(struct device *dev, enum hwmon_sensor_types type,
391 u32 attr, int channel, long *val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000392{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700393 switch (type) {
394 case hwmon_chip:
395 return lm95245_read_chip(dev, attr, channel, val);
396 case hwmon_temp:
397 return lm95245_read_temp(dev, attr, channel, val);
398 default:
399 return -EOPNOTSUPP;
400 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000401}
402
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700403static int lm95245_write(struct device *dev, enum hwmon_sensor_types type,
404 u32 attr, int channel, long val)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000405{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700406 switch (type) {
407 case hwmon_chip:
408 return lm95245_write_chip(dev, attr, channel, val);
409 case hwmon_temp:
410 return lm95245_write_temp(dev, attr, channel, val);
411 default:
412 return -EOPNOTSUPP;
413 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000414}
415
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700416static umode_t lm95245_temp_is_visible(const void *data, u32 attr, int channel)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000417{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700418 switch (attr) {
419 case hwmon_temp_input:
420 case hwmon_temp_max_alarm:
421 case hwmon_temp_max_hyst:
422 case hwmon_temp_crit_alarm:
423 case hwmon_temp_fault:
424 return S_IRUGO;
425 case hwmon_temp_type:
426 case hwmon_temp_max:
427 case hwmon_temp_crit:
428 case hwmon_temp_offset:
429 return S_IRUGO | S_IWUSR;
430 case hwmon_temp_crit_hyst:
431 return (channel == 0) ? S_IRUGO | S_IWUSR : S_IRUGO;
432 default:
433 return 0;
434 }
Alexander Steinfffd80c2011-06-28 15:11:23 +0000435}
436
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700437static umode_t lm95245_is_visible(const void *data,
438 enum hwmon_sensor_types type,
439 u32 attr, int channel)
440{
441 switch (type) {
442 case hwmon_chip:
443 switch (attr) {
444 case hwmon_chip_update_interval:
445 return S_IRUGO | S_IWUSR;
446 default:
447 return 0;
448 }
449 case hwmon_temp:
450 return lm95245_temp_is_visible(data, attr, channel);
451 default:
452 return 0;
453 }
454}
Alexander Steinfffd80c2011-06-28 15:11:23 +0000455
456/* Return 0 if detection is successful, -ENODEV otherwise */
457static int lm95245_detect(struct i2c_client *new_client,
458 struct i2c_board_info *info)
459{
460 struct i2c_adapter *adapter = new_client->adapter;
Guenter Roeck162a8df2014-04-22 08:48:57 -0700461 int address = new_client->addr;
462 const char *name;
463 int rev, id;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000464
465 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
466 return -ENODEV;
467
Guenter Roeck162a8df2014-04-22 08:48:57 -0700468 id = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID);
469 if (id != MANUFACTURER_ID)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000470 return -ENODEV;
471
Guenter Roeck162a8df2014-04-22 08:48:57 -0700472 rev = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID);
473 switch (rev) {
474 case LM95235_REVISION:
475 if (address != 0x18 && address != 0x29 && address != 0x4c)
476 return -ENODEV;
477 name = "lm95235";
478 break;
479 case LM95245_REVISION:
480 name = "lm95245";
481 break;
482 default:
483 return -ENODEV;
484 }
485
486 strlcpy(info->type, name, I2C_NAME_SIZE);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000487 return 0;
488}
489
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700490static int lm95245_init_client(struct lm95245_data *data)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000491{
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700492 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000493
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700494 ret = lm95245_read_conversion_rate(data);
495 if (ret < 0)
496 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000497
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700498 return regmap_update_bits(data->regmap, LM95245_REG_RW_CONFIG1,
499 CFG_STOP, 0);
500}
501
502static bool lm95245_is_writeable_reg(struct device *dev, unsigned int reg)
503{
504 switch (reg) {
505 case LM95245_REG_RW_CONFIG1:
506 case LM95245_REG_RW_CONVERS_RATE:
507 case LM95245_REG_W_ONE_SHOT:
508 case LM95245_REG_RW_CONFIG2:
509 case LM95245_REG_RW_REMOTE_OFFH:
510 case LM95245_REG_RW_REMOTE_OFFL:
511 case LM95245_REG_RW_REMOTE_OS_LIMIT:
512 case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT:
513 case LM95245_REG_RW_REMOTE_TCRIT_LIMIT:
514 case LM95245_REG_RW_COMMON_HYSTERESIS:
515 return true;
516 default:
517 return false;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000518 }
519}
520
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700521static bool lm95245_is_volatile_reg(struct device *dev, unsigned int reg)
522{
523 switch (reg) {
524 case LM95245_REG_R_STATUS1:
525 case LM95245_REG_R_STATUS2:
526 case LM95245_REG_R_LOCAL_TEMPH_S:
527 case LM95245_REG_R_LOCAL_TEMPL_S:
528 case LM95245_REG_R_REMOTE_TEMPH_S:
529 case LM95245_REG_R_REMOTE_TEMPL_S:
530 case LM95245_REG_R_REMOTE_TEMPH_U:
531 case LM95245_REG_R_REMOTE_TEMPL_U:
532 return true;
533 default:
534 return false;
535 }
536}
537
538static const struct regmap_config lm95245_regmap_config = {
539 .reg_bits = 8,
540 .val_bits = 8,
541 .writeable_reg = lm95245_is_writeable_reg,
542 .volatile_reg = lm95245_is_volatile_reg,
543 .cache_type = REGCACHE_RBTREE,
544 .use_single_rw = true,
545};
546
547static const u32 lm95245_chip_config[] = {
548 HWMON_C_UPDATE_INTERVAL,
549 0
550};
551
552static const struct hwmon_channel_info lm95245_chip = {
553 .type = hwmon_chip,
554 .config = lm95245_chip_config,
555};
556
557static const u32 lm95245_temp_config[] = {
558 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_CRIT_ALARM,
559 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_CRIT |
560 HWMON_T_CRIT_HYST | HWMON_T_FAULT | HWMON_T_MAX_ALARM |
561 HWMON_T_CRIT_ALARM | HWMON_T_TYPE | HWMON_T_OFFSET,
562 0
563};
564
565static const struct hwmon_channel_info lm95245_temp = {
566 .type = hwmon_temp,
567 .config = lm95245_temp_config,
568};
569
570static const struct hwmon_channel_info *lm95245_info[] = {
571 &lm95245_chip,
572 &lm95245_temp,
573 NULL
574};
575
576static const struct hwmon_ops lm95245_hwmon_ops = {
577 .is_visible = lm95245_is_visible,
578 .read = lm95245_read,
579 .write = lm95245_write,
580};
581
582static const struct hwmon_chip_info lm95245_chip_info = {
583 .ops = &lm95245_hwmon_ops,
584 .info = lm95245_info,
585};
586
Guenter Roeck7276d552014-01-20 09:17:16 -0800587static int lm95245_probe(struct i2c_client *client,
Alexander Steinfffd80c2011-06-28 15:11:23 +0000588 const struct i2c_device_id *id)
589{
Guenter Roeck7276d552014-01-20 09:17:16 -0800590 struct device *dev = &client->dev;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000591 struct lm95245_data *data;
Guenter Roeck7276d552014-01-20 09:17:16 -0800592 struct device *hwmon_dev;
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700593 int ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000594
Guenter Roeck7276d552014-01-20 09:17:16 -0800595 data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL);
Guenter Roecka8dd9462012-06-02 09:58:12 -0700596 if (!data)
597 return -ENOMEM;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000598
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700599 data->regmap = devm_regmap_init_i2c(client, &lm95245_regmap_config);
600 if (IS_ERR(data->regmap))
601 return PTR_ERR(data->regmap);
602
Alexander Steinfffd80c2011-06-28 15:11:23 +0000603 mutex_init(&data->update_lock);
604
605 /* Initialize the LM95245 chip */
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700606 ret = lm95245_init_client(data);
607 if (ret < 0)
608 return ret;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000609
Guenter Roeckc0a4b9e2016-07-05 19:10:52 -0700610 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
611 data,
612 &lm95245_chip_info,
613 NULL);
Guenter Roeck7276d552014-01-20 09:17:16 -0800614 return PTR_ERR_OR_ZERO(hwmon_dev);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000615}
616
617/* Driver data (common to all clients) */
618static const struct i2c_device_id lm95245_id[] = {
Guenter Roeck162a8df2014-04-22 08:48:57 -0700619 { "lm95235", 0 },
620 { "lm95245", 0 },
Alexander Steinfffd80c2011-06-28 15:11:23 +0000621 { }
622};
623MODULE_DEVICE_TABLE(i2c, lm95245_id);
624
625static struct i2c_driver lm95245_driver = {
626 .class = I2C_CLASS_HWMON,
627 .driver = {
Guenter Roeck162a8df2014-04-22 08:48:57 -0700628 .name = "lm95245",
Alexander Steinfffd80c2011-06-28 15:11:23 +0000629 },
630 .probe = lm95245_probe,
Alexander Steinfffd80c2011-06-28 15:11:23 +0000631 .id_table = lm95245_id,
632 .detect = lm95245_detect,
633 .address_list = normal_i2c,
634};
635
Axel Linf0967ee2012-01-20 15:38:18 +0800636module_i2c_driver(lm95245_driver);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000637
638MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
Guenter Roeck162a8df2014-04-22 08:48:57 -0700639MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
Alexander Steinfffd80c2011-06-28 15:11:23 +0000640MODULE_LICENSE("GPL");