xref: /openbmc/linux/drivers/hwmon/lm95245.c (revision 1a59d1b8)
1 /*
2  * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
3  *
4  * The LM95245 is a sensor chip made by TI / National Semiconductor.
5  * It reports up to two temperatures (its own plus an external one).
6  *
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.
18  */
19 
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/hwmon.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 
29 static 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 
86 #define STATUS1_ROS		0x10
87 #define STATUS1_DIODE_FAULT	0x04
88 #define STATUS1_RTCRIT		0x02
89 #define STATUS1_LOC		0x01
90 
91 #define MANUFACTURER_ID		0x01
92 #define LM95235_REVISION	0xB1
93 #define LM95245_REVISION	0xB3
94 
95 /* Client data (each client gets its own) */
96 struct lm95245_data {
97 	struct regmap *regmap;
98 	struct mutex update_lock;
99 	int interval;	/* in msecs */
100 };
101 
102 /* Conversions */
103 static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
104 {
105 	return val_h * 1000 + val_l * 1000 / 256;
106 }
107 
108 static int temp_from_reg_signed(u8 val_h, u8 val_l)
109 {
110 	if (val_h & 0x80)
111 		return (val_h - 0x100) * 1000;
112 	return temp_from_reg_unsigned(val_h, val_l);
113 }
114 
115 static int lm95245_read_conversion_rate(struct lm95245_data *data)
116 {
117 	unsigned int rate;
118 	int ret;
119 
120 	ret = regmap_read(data->regmap, LM95245_REG_RW_CONVERS_RATE, &rate);
121 	if (ret < 0)
122 		return ret;
123 
124 	switch (rate) {
125 	case RATE_CR0063:
126 		data->interval = 63;
127 		break;
128 	case RATE_CR0364:
129 		data->interval = 364;
130 		break;
131 	case RATE_CR1000:
132 		data->interval = 1000;
133 		break;
134 	case RATE_CR2500:
135 	default:
136 		data->interval = 2500;
137 		break;
138 	}
139 	return 0;
140 }
141 
142 static int lm95245_set_conversion_rate(struct lm95245_data *data, long interval)
143 {
144 	int ret, rate;
145 
146 	if (interval <= 63) {
147 		interval = 63;
148 		rate = RATE_CR0063;
149 	} else if (interval <= 364) {
150 		interval = 364;
151 		rate = RATE_CR0364;
152 	} else if (interval <= 1000) {
153 		interval = 1000;
154 		rate = RATE_CR1000;
155 	} else {
156 		interval = 2500;
157 		rate = RATE_CR2500;
158 	}
159 
160 	ret = regmap_write(data->regmap, LM95245_REG_RW_CONVERS_RATE, rate);
161 	if (ret < 0)
162 		return ret;
163 
164 	data->interval = interval;
165 	return 0;
166 }
167 
168 static int lm95245_read_temp(struct device *dev, u32 attr, int channel,
169 			     long *val)
170 {
171 	struct lm95245_data *data = dev_get_drvdata(dev);
172 	struct regmap *regmap = data->regmap;
173 	int ret, regl, regh, regvall, regvalh;
174 
175 	switch (attr) {
176 	case hwmon_temp_input:
177 		regl = channel ? LM95245_REG_R_REMOTE_TEMPL_S :
178 				 LM95245_REG_R_LOCAL_TEMPL_S;
179 		regh = channel ? LM95245_REG_R_REMOTE_TEMPH_S :
180 				 LM95245_REG_R_LOCAL_TEMPH_S;
181 		ret = regmap_read(regmap, regl, &regvall);
182 		if (ret < 0)
183 			return ret;
184 		ret = regmap_read(regmap, regh, &regvalh);
185 		if (ret < 0)
186 			return ret;
187 		/*
188 		 * Local temp is always signed.
189 		 * Remote temp has both signed and unsigned data.
190 		 * Use signed calculation for remote if signed bit is set
191 		 * or if reported temperature is below signed limit.
192 		 */
193 		if (!channel || (regvalh & 0x80) || regvalh < 0x7f) {
194 			*val = temp_from_reg_signed(regvalh, regvall);
195 			return 0;
196 		}
197 		ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPL_U,
198 				  &regvall);
199 		if (ret < 0)
200 			return ret;
201 		ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPH_U,
202 				  &regvalh);
203 		if (ret < 0)
204 			return ret;
205 		*val = temp_from_reg_unsigned(regvalh, regvall);
206 		return 0;
207 	case hwmon_temp_max:
208 		ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
209 				  &regvalh);
210 		if (ret < 0)
211 			return ret;
212 		*val = regvalh * 1000;
213 		return 0;
214 	case hwmon_temp_crit:
215 		regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
216 				 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
217 		ret = regmap_read(regmap, regh, &regvalh);
218 		if (ret < 0)
219 			return ret;
220 		*val = regvalh * 1000;
221 		return 0;
222 	case hwmon_temp_max_hyst:
223 		ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
224 				  &regvalh);
225 		if (ret < 0)
226 			return ret;
227 		ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
228 				  &regvall);
229 		if (ret < 0)
230 			return ret;
231 		*val = (regvalh - regvall) * 1000;
232 		return 0;
233 	case hwmon_temp_crit_hyst:
234 		regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
235 				 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
236 		ret = regmap_read(regmap, regh, &regvalh);
237 		if (ret < 0)
238 			return ret;
239 		ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
240 				  &regvall);
241 		if (ret < 0)
242 			return ret;
243 		*val = (regvalh - regvall) * 1000;
244 		return 0;
245 	case hwmon_temp_type:
246 		ret = regmap_read(regmap, LM95245_REG_RW_CONFIG2, &regvalh);
247 		if (ret < 0)
248 			return ret;
249 		*val = (regvalh & CFG2_REMOTE_TT) ? 1 : 2;
250 		return 0;
251 	case hwmon_temp_offset:
252 		ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFL,
253 				  &regvall);
254 		if (ret < 0)
255 			return ret;
256 		ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFH,
257 				  &regvalh);
258 		if (ret < 0)
259 			return ret;
260 		*val = temp_from_reg_signed(regvalh, regvall);
261 		return 0;
262 	case hwmon_temp_max_alarm:
263 		ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
264 		if (ret < 0)
265 			return ret;
266 		*val = !!(regvalh & STATUS1_ROS);
267 		return 0;
268 	case hwmon_temp_crit_alarm:
269 		ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
270 		if (ret < 0)
271 			return ret;
272 		*val = !!(regvalh & (channel ? STATUS1_RTCRIT : STATUS1_LOC));
273 		return 0;
274 	case hwmon_temp_fault:
275 		ret = regmap_read(regmap, LM95245_REG_R_STATUS1, &regvalh);
276 		if (ret < 0)
277 			return ret;
278 		*val = !!(regvalh & STATUS1_DIODE_FAULT);
279 		return 0;
280 	default:
281 		return -EOPNOTSUPP;
282 	}
283 }
284 
285 static int lm95245_write_temp(struct device *dev, u32 attr, int channel,
286 			      long val)
287 {
288 	struct lm95245_data *data = dev_get_drvdata(dev);
289 	struct regmap *regmap = data->regmap;
290 	unsigned int regval;
291 	int ret, reg;
292 
293 	switch (attr) {
294 	case hwmon_temp_max:
295 		val = clamp_val(val / 1000, 0, 255);
296 		ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, val);
297 		return ret;
298 	case hwmon_temp_crit:
299 		reg = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
300 				LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
301 		val = clamp_val(val / 1000, 0, channel ? 255 : 127);
302 		ret = regmap_write(regmap, reg, val);
303 		return ret;
304 	case hwmon_temp_crit_hyst:
305 		mutex_lock(&data->update_lock);
306 		ret = regmap_read(regmap, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
307 				  &regval);
308 		if (ret < 0) {
309 			mutex_unlock(&data->update_lock);
310 			return ret;
311 		}
312 		/* Clamp to reasonable range to prevent overflow */
313 		val = clamp_val(val, -1000000, 1000000);
314 		val = regval - val / 1000;
315 		val = clamp_val(val, 0, 31);
316 		ret = regmap_write(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
317 				   val);
318 		mutex_unlock(&data->update_lock);
319 		return ret;
320 	case hwmon_temp_offset:
321 		val = clamp_val(val, -128000, 127875);
322 		val = val * 256 / 1000;
323 		mutex_lock(&data->update_lock);
324 		ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFL,
325 				   val & 0xe0);
326 		if (ret < 0) {
327 			mutex_unlock(&data->update_lock);
328 			return ret;
329 		}
330 		ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFH,
331 				   (val >> 8) & 0xff);
332 		mutex_unlock(&data->update_lock);
333 		return ret;
334 	case hwmon_temp_type:
335 		if (val != 1 && val != 2)
336 			return -EINVAL;
337 		ret = regmap_update_bits(regmap, LM95245_REG_RW_CONFIG2,
338 					 CFG2_REMOTE_TT,
339 					 val == 1 ? CFG2_REMOTE_TT : 0);
340 		return ret;
341 	default:
342 		return -EOPNOTSUPP;
343 	}
344 }
345 
346 static int lm95245_read_chip(struct device *dev, u32 attr, int channel,
347 			     long *val)
348 {
349 	struct lm95245_data *data = dev_get_drvdata(dev);
350 
351 	switch (attr) {
352 	case hwmon_chip_update_interval:
353 		*val = data->interval;
354 		return 0;
355 	default:
356 		return -EOPNOTSUPP;
357 	}
358 }
359 
360 static int lm95245_write_chip(struct device *dev, u32 attr, int channel,
361 			      long val)
362 {
363 	struct lm95245_data *data = dev_get_drvdata(dev);
364 	int ret;
365 
366 	switch (attr) {
367 	case hwmon_chip_update_interval:
368 		mutex_lock(&data->update_lock);
369 		ret = lm95245_set_conversion_rate(data, val);
370 		mutex_unlock(&data->update_lock);
371 		return ret;
372 	default:
373 		return -EOPNOTSUPP;
374 	}
375 }
376 
377 static int lm95245_read(struct device *dev, enum hwmon_sensor_types type,
378 			u32 attr, int channel, long *val)
379 {
380 	switch (type) {
381 	case hwmon_chip:
382 		return lm95245_read_chip(dev, attr, channel, val);
383 	case hwmon_temp:
384 		return lm95245_read_temp(dev, attr, channel, val);
385 	default:
386 		return -EOPNOTSUPP;
387 	}
388 }
389 
390 static int lm95245_write(struct device *dev, enum hwmon_sensor_types type,
391 			 u32 attr, int channel, long val)
392 {
393 	switch (type) {
394 	case hwmon_chip:
395 		return lm95245_write_chip(dev, attr, channel, val);
396 	case hwmon_temp:
397 		return lm95245_write_temp(dev, attr, channel, val);
398 	default:
399 		return -EOPNOTSUPP;
400 	}
401 }
402 
403 static umode_t lm95245_temp_is_visible(const void *data, u32 attr, int channel)
404 {
405 	switch (attr) {
406 	case hwmon_temp_input:
407 	case hwmon_temp_max_alarm:
408 	case hwmon_temp_max_hyst:
409 	case hwmon_temp_crit_alarm:
410 	case hwmon_temp_fault:
411 		return 0444;
412 	case hwmon_temp_type:
413 	case hwmon_temp_max:
414 	case hwmon_temp_crit:
415 	case hwmon_temp_offset:
416 		return 0644;
417 	case hwmon_temp_crit_hyst:
418 		return (channel == 0) ? 0644 : 0444;
419 	default:
420 		return 0;
421 	}
422 }
423 
424 static umode_t lm95245_is_visible(const void *data,
425 				  enum hwmon_sensor_types type,
426 				  u32 attr, int channel)
427 {
428 	switch (type) {
429 	case hwmon_chip:
430 		switch (attr) {
431 		case hwmon_chip_update_interval:
432 			return 0644;
433 		default:
434 			return 0;
435 		}
436 	case hwmon_temp:
437 		return lm95245_temp_is_visible(data, attr, channel);
438 	default:
439 		return 0;
440 	}
441 }
442 
443 /* Return 0 if detection is successful, -ENODEV otherwise */
444 static int lm95245_detect(struct i2c_client *new_client,
445 			  struct i2c_board_info *info)
446 {
447 	struct i2c_adapter *adapter = new_client->adapter;
448 	int address = new_client->addr;
449 	const char *name;
450 	int rev, id;
451 
452 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
453 		return -ENODEV;
454 
455 	id = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID);
456 	if (id != MANUFACTURER_ID)
457 		return -ENODEV;
458 
459 	rev = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID);
460 	switch (rev) {
461 	case LM95235_REVISION:
462 		if (address != 0x18 && address != 0x29 && address != 0x4c)
463 			return -ENODEV;
464 		name = "lm95235";
465 		break;
466 	case LM95245_REVISION:
467 		name = "lm95245";
468 		break;
469 	default:
470 		return -ENODEV;
471 	}
472 
473 	strlcpy(info->type, name, I2C_NAME_SIZE);
474 	return 0;
475 }
476 
477 static int lm95245_init_client(struct lm95245_data *data)
478 {
479 	int ret;
480 
481 	ret = lm95245_read_conversion_rate(data);
482 	if (ret < 0)
483 		return ret;
484 
485 	return regmap_update_bits(data->regmap, LM95245_REG_RW_CONFIG1,
486 				  CFG_STOP, 0);
487 }
488 
489 static bool lm95245_is_writeable_reg(struct device *dev, unsigned int reg)
490 {
491 	switch (reg) {
492 	case LM95245_REG_RW_CONFIG1:
493 	case LM95245_REG_RW_CONVERS_RATE:
494 	case LM95245_REG_W_ONE_SHOT:
495 	case LM95245_REG_RW_CONFIG2:
496 	case LM95245_REG_RW_REMOTE_OFFH:
497 	case LM95245_REG_RW_REMOTE_OFFL:
498 	case LM95245_REG_RW_REMOTE_OS_LIMIT:
499 	case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT:
500 	case LM95245_REG_RW_REMOTE_TCRIT_LIMIT:
501 	case LM95245_REG_RW_COMMON_HYSTERESIS:
502 		return true;
503 	default:
504 		return false;
505 	}
506 }
507 
508 static bool lm95245_is_volatile_reg(struct device *dev, unsigned int reg)
509 {
510 	switch (reg) {
511 	case LM95245_REG_R_STATUS1:
512 	case LM95245_REG_R_STATUS2:
513 	case LM95245_REG_R_LOCAL_TEMPH_S:
514 	case LM95245_REG_R_LOCAL_TEMPL_S:
515 	case LM95245_REG_R_REMOTE_TEMPH_S:
516 	case LM95245_REG_R_REMOTE_TEMPL_S:
517 	case LM95245_REG_R_REMOTE_TEMPH_U:
518 	case LM95245_REG_R_REMOTE_TEMPL_U:
519 		return true;
520 	default:
521 		return false;
522 	}
523 }
524 
525 static const struct regmap_config lm95245_regmap_config = {
526 	.reg_bits = 8,
527 	.val_bits = 8,
528 	.writeable_reg = lm95245_is_writeable_reg,
529 	.volatile_reg = lm95245_is_volatile_reg,
530 	.cache_type = REGCACHE_RBTREE,
531 	.use_single_read = true,
532 	.use_single_write = true,
533 };
534 
535 static const struct hwmon_channel_info *lm95245_info[] = {
536 	HWMON_CHANNEL_INFO(chip,
537 			   HWMON_C_UPDATE_INTERVAL),
538 	HWMON_CHANNEL_INFO(temp,
539 			   HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_HYST |
540 			   HWMON_T_CRIT_ALARM,
541 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
542 			   HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_FAULT |
543 			   HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM |
544 			   HWMON_T_TYPE | HWMON_T_OFFSET),
545 	NULL
546 };
547 
548 static const struct hwmon_ops lm95245_hwmon_ops = {
549 	.is_visible = lm95245_is_visible,
550 	.read = lm95245_read,
551 	.write = lm95245_write,
552 };
553 
554 static const struct hwmon_chip_info lm95245_chip_info = {
555 	.ops = &lm95245_hwmon_ops,
556 	.info = lm95245_info,
557 };
558 
559 static int lm95245_probe(struct i2c_client *client,
560 			 const struct i2c_device_id *id)
561 {
562 	struct device *dev = &client->dev;
563 	struct lm95245_data *data;
564 	struct device *hwmon_dev;
565 	int ret;
566 
567 	data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL);
568 	if (!data)
569 		return -ENOMEM;
570 
571 	data->regmap = devm_regmap_init_i2c(client, &lm95245_regmap_config);
572 	if (IS_ERR(data->regmap))
573 		return PTR_ERR(data->regmap);
574 
575 	mutex_init(&data->update_lock);
576 
577 	/* Initialize the LM95245 chip */
578 	ret = lm95245_init_client(data);
579 	if (ret < 0)
580 		return ret;
581 
582 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
583 							 data,
584 							 &lm95245_chip_info,
585 							 NULL);
586 	return PTR_ERR_OR_ZERO(hwmon_dev);
587 }
588 
589 /* Driver data (common to all clients) */
590 static const struct i2c_device_id lm95245_id[] = {
591 	{ "lm95235", 0 },
592 	{ "lm95245", 0 },
593 	{ }
594 };
595 MODULE_DEVICE_TABLE(i2c, lm95245_id);
596 
597 static const struct of_device_id __maybe_unused lm95245_of_match[] = {
598 	{ .compatible = "national,lm95235" },
599 	{ .compatible = "national,lm95245" },
600 	{ },
601 };
602 MODULE_DEVICE_TABLE(of, lm95245_of_match);
603 
604 static struct i2c_driver lm95245_driver = {
605 	.class		= I2C_CLASS_HWMON,
606 	.driver = {
607 		.name	= "lm95245",
608 		.of_match_table = of_match_ptr(lm95245_of_match),
609 	},
610 	.probe		= lm95245_probe,
611 	.id_table	= lm95245_id,
612 	.detect		= lm95245_detect,
613 	.address_list	= normal_i2c,
614 };
615 
616 module_i2c_driver(lm95245_driver);
617 
618 MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
619 MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
620 MODULE_LICENSE("GPL");
621