xref: /openbmc/linux/drivers/hwmon/lm83.c (revision 8dd3cdea)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
4  *          monitoring
5  * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
6  *
7  * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
8  * a sensor chip made by National Semiconductor. It reports up to four
9  * temperatures (its own plus up to three external ones) with a 1 deg
10  * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
11  * from National's website at:
12  *   http://www.national.com/pf/LM/LM83.html
13  * Since the datasheet omits to give the chip stepping code, I give it
14  * here: 0x03 (at register 0xff).
15  *
16  * Also supports the LM82 temp sensor, which is basically a stripped down
17  * model of the LM83.  Datasheet is here:
18  * http://www.national.com/pf/LM/LM82.html
19  */
20 
21 #include <linux/bits.h>
22 #include <linux/err.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/hwmon.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <linux/sysfs.h>
31 
32 /*
33  * Addresses to scan
34  * Address is selected using 2 three-level pins, resulting in 9 possible
35  * addresses.
36  */
37 
38 static const unsigned short normal_i2c[] = {
39 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
40 
41 enum chips { lm83, lm82 };
42 
43 /*
44  * The LM83 registers
45  * Manufacturer ID is 0x01 for National Semiconductor.
46  */
47 
48 #define LM83_REG_R_MAN_ID		0xFE
49 #define LM83_REG_R_CHIP_ID		0xFF
50 #define LM83_REG_R_CONFIG		0x03
51 #define LM83_REG_W_CONFIG		0x09
52 #define LM83_REG_R_STATUS1		0x02
53 #define LM83_REG_R_STATUS2		0x35
54 #define LM83_REG_R_LOCAL_TEMP		0x00
55 #define LM83_REG_R_LOCAL_HIGH		0x05
56 #define LM83_REG_W_LOCAL_HIGH		0x0B
57 #define LM83_REG_R_REMOTE1_TEMP		0x30
58 #define LM83_REG_R_REMOTE1_HIGH		0x38
59 #define LM83_REG_W_REMOTE1_HIGH		0x50
60 #define LM83_REG_R_REMOTE2_TEMP		0x01
61 #define LM83_REG_R_REMOTE2_HIGH		0x07
62 #define LM83_REG_W_REMOTE2_HIGH		0x0D
63 #define LM83_REG_R_REMOTE3_TEMP		0x31
64 #define LM83_REG_R_REMOTE3_HIGH		0x3A
65 #define LM83_REG_W_REMOTE3_HIGH		0x52
66 #define LM83_REG_R_TCRIT		0x42
67 #define LM83_REG_W_TCRIT		0x5A
68 
69 static const u8 LM83_REG_TEMP[] = {
70 	LM83_REG_R_LOCAL_TEMP,
71 	LM83_REG_R_REMOTE1_TEMP,
72 	LM83_REG_R_REMOTE2_TEMP,
73 	LM83_REG_R_REMOTE3_TEMP,
74 };
75 
76 static const u8 LM83_REG_MAX[] = {
77 	LM83_REG_R_LOCAL_HIGH,
78 	LM83_REG_R_REMOTE1_HIGH,
79 	LM83_REG_R_REMOTE2_HIGH,
80 	LM83_REG_R_REMOTE3_HIGH,
81 };
82 
83 /* alarm and fault registers and bits, indexed by channel */
84 static const u8 LM83_ALARM_REG[] = {
85 	LM83_REG_R_STATUS1, LM83_REG_R_STATUS2, LM83_REG_R_STATUS1, LM83_REG_R_STATUS2
86 };
87 
88 static const u8 LM83_MAX_ALARM_BIT[] = {
89 	BIT(6), BIT(7), BIT(4), BIT(4)
90 };
91 
92 static const u8 LM83_CRIT_ALARM_BIT[] = {
93 	BIT(0), BIT(0), BIT(1), BIT(1)
94 };
95 
96 static const u8 LM83_FAULT_BIT[] = {
97 	0, BIT(5), BIT(2), BIT(2)
98 };
99 
100 /*
101  * Client data (each client gets its own)
102  */
103 
104 struct lm83_data {
105 	struct regmap *regmap;
106 	enum chips type;
107 };
108 
109 /* regmap code */
110 
111 static int lm83_regmap_reg_read(void *context, unsigned int reg, unsigned int *val)
112 {
113 	struct i2c_client *client = context;
114 	int ret;
115 
116 	ret = i2c_smbus_read_byte_data(client, reg);
117 	if (ret < 0)
118 		return ret;
119 
120 	*val = ret;
121 	return 0;
122 }
123 
124 /*
125  * The regmap write function maps read register addresses to write register
126  * addresses. This is necessary for regmap register caching to work.
127  * An alternative would be to clear the regmap cache whenever a register is
128  * written, but that would be much more expensive.
129  */
130 static int lm83_regmap_reg_write(void *context, unsigned int reg, unsigned int val)
131 {
132 	struct i2c_client *client = context;
133 
134 	switch (reg) {
135 	case LM83_REG_R_CONFIG:
136 	case LM83_REG_R_LOCAL_HIGH:
137 	case LM83_REG_R_REMOTE2_HIGH:
138 		reg += 0x06;
139 		break;
140 	case LM83_REG_R_REMOTE1_HIGH:
141 	case LM83_REG_R_REMOTE3_HIGH:
142 	case LM83_REG_R_TCRIT:
143 		reg += 0x18;
144 		break;
145 	default:
146 		break;
147 	}
148 
149 	return i2c_smbus_write_byte_data(client, reg, val);
150 }
151 
152 static bool lm83_regmap_is_volatile(struct device *dev, unsigned int reg)
153 {
154 	switch (reg) {
155 	case LM83_REG_R_LOCAL_TEMP:
156 	case LM83_REG_R_REMOTE1_TEMP:
157 	case LM83_REG_R_REMOTE2_TEMP:
158 	case LM83_REG_R_REMOTE3_TEMP:
159 	case LM83_REG_R_STATUS1:
160 	case LM83_REG_R_STATUS2:
161 		return true;
162 	default:
163 		return false;
164 	}
165 }
166 
167 static const struct regmap_config lm83_regmap_config = {
168 	.reg_bits = 8,
169 	.val_bits = 8,
170 	.cache_type = REGCACHE_RBTREE,
171 	.volatile_reg = lm83_regmap_is_volatile,
172 	.reg_read = lm83_regmap_reg_read,
173 	.reg_write = lm83_regmap_reg_write,
174 };
175 
176 /* hwmon API */
177 
178 static int lm83_temp_read(struct device *dev, u32 attr, int channel, long *val)
179 {
180 	struct lm83_data *data = dev_get_drvdata(dev);
181 	unsigned int regval;
182 	int err;
183 
184 	switch (attr) {
185 	case hwmon_temp_input:
186 		err = regmap_read(data->regmap, LM83_REG_TEMP[channel], &regval);
187 		if (err < 0)
188 			return err;
189 		*val = (s8)regval * 1000;
190 		break;
191 	case hwmon_temp_max:
192 		err = regmap_read(data->regmap, LM83_REG_MAX[channel], &regval);
193 		if (err < 0)
194 			return err;
195 		*val = (s8)regval * 1000;
196 		break;
197 	case hwmon_temp_crit:
198 		err = regmap_read(data->regmap, LM83_REG_R_TCRIT, &regval);
199 		if (err < 0)
200 			return err;
201 		*val = (s8)regval * 1000;
202 		break;
203 	case hwmon_temp_max_alarm:
204 		err = regmap_read(data->regmap, LM83_ALARM_REG[channel], &regval);
205 		if (err < 0)
206 			return err;
207 		*val = !!(regval & LM83_MAX_ALARM_BIT[channel]);
208 		break;
209 	case hwmon_temp_crit_alarm:
210 		err = regmap_read(data->regmap, LM83_ALARM_REG[channel], &regval);
211 		if (err < 0)
212 			return err;
213 		*val = !!(regval & LM83_CRIT_ALARM_BIT[channel]);
214 		break;
215 	case hwmon_temp_fault:
216 		err = regmap_read(data->regmap, LM83_ALARM_REG[channel], &regval);
217 		if (err < 0)
218 			return err;
219 		*val = !!(regval & LM83_FAULT_BIT[channel]);
220 		break;
221 	default:
222 		return -EOPNOTSUPP;
223 	}
224 	return 0;
225 }
226 
227 static int lm83_temp_write(struct device *dev, u32 attr, int channel, long val)
228 {
229 	struct lm83_data *data = dev_get_drvdata(dev);
230 	unsigned int regval;
231 	int err;
232 
233 	regval = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
234 
235 	switch (attr) {
236 	case hwmon_temp_max:
237 		err = regmap_write(data->regmap, LM83_REG_MAX[channel], regval);
238 		if (err < 0)
239 			return err;
240 		break;
241 	case hwmon_temp_crit:
242 		err = regmap_write(data->regmap, LM83_REG_R_TCRIT, regval);
243 		if (err < 0)
244 			return err;
245 		break;
246 	default:
247 		return -EOPNOTSUPP;
248 	}
249 	return 0;
250 }
251 
252 static int lm83_chip_read(struct device *dev, u32 attr, int channel, long *val)
253 {
254 	struct lm83_data *data = dev_get_drvdata(dev);
255 	unsigned int regval;
256 	int err;
257 
258 	switch (attr) {
259 	case hwmon_chip_alarms:
260 		err = regmap_read(data->regmap, LM83_REG_R_STATUS1, &regval);
261 		if (err < 0)
262 			return err;
263 		*val = regval;
264 		err = regmap_read(data->regmap, LM83_REG_R_STATUS2, &regval);
265 		if (err < 0)
266 			return err;
267 		*val |= regval << 8;
268 		return 0;
269 	default:
270 		return -EOPNOTSUPP;
271 	}
272 
273 	return 0;
274 }
275 
276 static int lm83_read(struct device *dev, enum hwmon_sensor_types type,
277 		     u32 attr, int channel, long *val)
278 {
279 	switch (type) {
280 	case hwmon_chip:
281 		return lm83_chip_read(dev, attr, channel, val);
282 	case hwmon_temp:
283 		return lm83_temp_read(dev, attr, channel, val);
284 	default:
285 		return -EOPNOTSUPP;
286 	}
287 }
288 
289 static int lm83_write(struct device *dev, enum hwmon_sensor_types type,
290 		      u32 attr, int channel, long val)
291 {
292 	switch (type) {
293 	case hwmon_temp:
294 		return lm83_temp_write(dev, attr, channel, val);
295 	default:
296 		return -EOPNOTSUPP;
297 	}
298 }
299 
300 static umode_t lm83_is_visible(const void *_data, enum hwmon_sensor_types type,
301 			       u32 attr, int channel)
302 {
303 	const struct lm83_data *data = _data;
304 
305 	/*
306 	 * LM82 only supports a single external channel, modeled as channel 2.
307 	 */
308 	if (data->type == lm82 && (channel == 1 || channel == 3))
309 		return 0;
310 
311 	switch (type) {
312 	case hwmon_chip:
313 		if (attr == hwmon_chip_alarms)
314 			return 0444;
315 		break;
316 	case hwmon_temp:
317 		switch (attr) {
318 		case hwmon_temp_input:
319 		case hwmon_temp_max_alarm:
320 		case hwmon_temp_crit_alarm:
321 			return 0444;
322 		case hwmon_temp_fault:
323 			if (channel)
324 				return 0444;
325 			break;
326 		case hwmon_temp_max:
327 			return 0644;
328 		case hwmon_temp_crit:
329 			if (channel == 2)
330 				return 0644;
331 			return 0444;
332 		default:
333 			break;
334 		}
335 		break;
336 	default:
337 		break;
338 	}
339 	return 0;
340 }
341 
342 static const struct hwmon_channel_info *lm83_info[] = {
343 	HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS),
344 	HWMON_CHANNEL_INFO(temp,
345 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
346 			   HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM,
347 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
348 			   HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT,
349 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
350 			   HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT,
351 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
352 			   HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT
353 			   ),
354 	NULL
355 };
356 
357 static const struct hwmon_ops lm83_hwmon_ops = {
358 	.is_visible = lm83_is_visible,
359 	.read = lm83_read,
360 	.write = lm83_write,
361 };
362 
363 static const struct hwmon_chip_info lm83_chip_info = {
364 	.ops = &lm83_hwmon_ops,
365 	.info = lm83_info,
366 };
367 
368 /* Return 0 if detection is successful, -ENODEV otherwise */
369 static int lm83_detect(struct i2c_client *client,
370 		       struct i2c_board_info *info)
371 {
372 	struct i2c_adapter *adapter = client->adapter;
373 	const char *name;
374 	u8 man_id, chip_id;
375 
376 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
377 		return -ENODEV;
378 
379 	/* Detection */
380 	if ((i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) & 0xA8) ||
381 	    (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) & 0x48) ||
382 	    (i2c_smbus_read_byte_data(client, LM83_REG_R_CONFIG) & 0x41)) {
383 		dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
384 			client->addr);
385 		return -ENODEV;
386 	}
387 
388 	/* Identification */
389 	man_id = i2c_smbus_read_byte_data(client, LM83_REG_R_MAN_ID);
390 	if (man_id != 0x01)	/* National Semiconductor */
391 		return -ENODEV;
392 
393 	chip_id = i2c_smbus_read_byte_data(client, LM83_REG_R_CHIP_ID);
394 	switch (chip_id) {
395 	case 0x03:
396 		/*
397 		 * According to the LM82 datasheet dated March 2013, recent
398 		 * revisions of LM82 have a die revision of 0x03. This was
399 		 * confirmed with a real chip. Further details in this revision
400 		 * of the LM82 datasheet strongly suggest that LM82 is just a
401 		 * repackaged LM83. It is therefore impossible to distinguish
402 		 * those chips from LM83, and they will be misdetected as LM83.
403 		 */
404 		name = "lm83";
405 		break;
406 	case 0x01:
407 		name = "lm82";
408 		break;
409 	default:
410 		/* identification failed */
411 		dev_dbg(&adapter->dev,
412 			"Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
413 			man_id, chip_id);
414 		return -ENODEV;
415 	}
416 
417 	strlcpy(info->type, name, I2C_NAME_SIZE);
418 
419 	return 0;
420 }
421 
422 static const struct i2c_device_id lm83_id[] = {
423 	{ "lm83", lm83 },
424 	{ "lm82", lm82 },
425 	{ }
426 };
427 MODULE_DEVICE_TABLE(i2c, lm83_id);
428 
429 static int lm83_probe(struct i2c_client *client)
430 {
431 	struct device *dev = &client->dev;
432 	struct device *hwmon_dev;
433 	struct lm83_data *data;
434 
435 	data = devm_kzalloc(dev, sizeof(struct lm83_data), GFP_KERNEL);
436 	if (!data)
437 		return -ENOMEM;
438 
439 	data->regmap = devm_regmap_init(dev, NULL, client, &lm83_regmap_config);
440 	if (IS_ERR(data->regmap))
441 		return PTR_ERR(data->regmap);
442 
443 	data->type = i2c_match_id(lm83_id, client)->driver_data;
444 
445 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
446 							 data, &lm83_chip_info, NULL);
447 	return PTR_ERR_OR_ZERO(hwmon_dev);
448 }
449 
450 /*
451  * Driver data (common to all clients)
452  */
453 
454 static struct i2c_driver lm83_driver = {
455 	.class		= I2C_CLASS_HWMON,
456 	.driver = {
457 		.name	= "lm83",
458 	},
459 	.probe_new	= lm83_probe,
460 	.id_table	= lm83_id,
461 	.detect		= lm83_detect,
462 	.address_list	= normal_i2c,
463 };
464 
465 module_i2c_driver(lm83_driver);
466 
467 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
468 MODULE_DESCRIPTION("LM83 driver");
469 MODULE_LICENSE("GPL");
470