xref: /openbmc/linux/drivers/hwmon/lm83.c (revision 5a1ea477)
1 /*
2  * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
5  *
6  * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
7  * a sensor chip made by National Semiconductor. It reports up to four
8  * temperatures (its own plus up to three external ones) with a 1 deg
9  * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
10  * from National's website at:
11  *   http://www.national.com/pf/LM/LM83.html
12  * Since the datasheet omits to give the chip stepping code, I give it
13  * here: 0x03 (at register 0xff).
14  *
15  * Also supports the LM82 temp sensor, which is basically a stripped down
16  * model of the LM83.  Datasheet is here:
17  * http://www.national.com/pf/LM/LM82.html
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  */
29 
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/jiffies.h>
34 #include <linux/i2c.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/hwmon.h>
37 #include <linux/err.h>
38 #include <linux/mutex.h>
39 #include <linux/sysfs.h>
40 
41 /*
42  * Addresses to scan
43  * Address is selected using 2 three-level pins, resulting in 9 possible
44  * addresses.
45  */
46 
47 static const unsigned short normal_i2c[] = {
48 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
49 
50 enum chips { lm83, lm82 };
51 
52 /*
53  * The LM83 registers
54  * Manufacturer ID is 0x01 for National Semiconductor.
55  */
56 
57 #define LM83_REG_R_MAN_ID		0xFE
58 #define LM83_REG_R_CHIP_ID		0xFF
59 #define LM83_REG_R_CONFIG		0x03
60 #define LM83_REG_W_CONFIG		0x09
61 #define LM83_REG_R_STATUS1		0x02
62 #define LM83_REG_R_STATUS2		0x35
63 #define LM83_REG_R_LOCAL_TEMP		0x00
64 #define LM83_REG_R_LOCAL_HIGH		0x05
65 #define LM83_REG_W_LOCAL_HIGH		0x0B
66 #define LM83_REG_R_REMOTE1_TEMP		0x30
67 #define LM83_REG_R_REMOTE1_HIGH		0x38
68 #define LM83_REG_W_REMOTE1_HIGH		0x50
69 #define LM83_REG_R_REMOTE2_TEMP		0x01
70 #define LM83_REG_R_REMOTE2_HIGH		0x07
71 #define LM83_REG_W_REMOTE2_HIGH		0x0D
72 #define LM83_REG_R_REMOTE3_TEMP		0x31
73 #define LM83_REG_R_REMOTE3_HIGH		0x3A
74 #define LM83_REG_W_REMOTE3_HIGH		0x52
75 #define LM83_REG_R_TCRIT		0x42
76 #define LM83_REG_W_TCRIT		0x5A
77 
78 /*
79  * Conversions and various macros
80  * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
81  */
82 
83 #define TEMP_FROM_REG(val)	((val) * 1000)
84 #define TEMP_TO_REG(val)	((val) <= -128000 ? -128 : \
85 				 (val) >= 127000 ? 127 : \
86 				 (val) < 0 ? ((val) - 500) / 1000 : \
87 				 ((val) + 500) / 1000)
88 
89 static const u8 LM83_REG_R_TEMP[] = {
90 	LM83_REG_R_LOCAL_TEMP,
91 	LM83_REG_R_REMOTE1_TEMP,
92 	LM83_REG_R_REMOTE2_TEMP,
93 	LM83_REG_R_REMOTE3_TEMP,
94 	LM83_REG_R_LOCAL_HIGH,
95 	LM83_REG_R_REMOTE1_HIGH,
96 	LM83_REG_R_REMOTE2_HIGH,
97 	LM83_REG_R_REMOTE3_HIGH,
98 	LM83_REG_R_TCRIT,
99 };
100 
101 static const u8 LM83_REG_W_HIGH[] = {
102 	LM83_REG_W_LOCAL_HIGH,
103 	LM83_REG_W_REMOTE1_HIGH,
104 	LM83_REG_W_REMOTE2_HIGH,
105 	LM83_REG_W_REMOTE3_HIGH,
106 	LM83_REG_W_TCRIT,
107 };
108 
109 /*
110  * Client data (each client gets its own)
111  */
112 
113 struct lm83_data {
114 	struct i2c_client *client;
115 	const struct attribute_group *groups[3];
116 	struct mutex update_lock;
117 	char valid; /* zero until following fields are valid */
118 	unsigned long last_updated; /* in jiffies */
119 
120 	/* registers values */
121 	s8 temp[9];	/* 0..3: input 1-4,
122 			   4..7: high limit 1-4,
123 			   8   : critical limit */
124 	u16 alarms; /* bitvector, combined */
125 };
126 
127 static struct lm83_data *lm83_update_device(struct device *dev)
128 {
129 	struct lm83_data *data = dev_get_drvdata(dev);
130 	struct i2c_client *client = data->client;
131 
132 	mutex_lock(&data->update_lock);
133 
134 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
135 		int nr;
136 
137 		dev_dbg(&client->dev, "Updating lm83 data.\n");
138 		for (nr = 0; nr < 9; nr++) {
139 			data->temp[nr] =
140 			    i2c_smbus_read_byte_data(client,
141 			    LM83_REG_R_TEMP[nr]);
142 		}
143 		data->alarms =
144 		    i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
145 		    + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
146 		    << 8);
147 
148 		data->last_updated = jiffies;
149 		data->valid = 1;
150 	}
151 
152 	mutex_unlock(&data->update_lock);
153 
154 	return data;
155 }
156 
157 /*
158  * Sysfs stuff
159  */
160 
161 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
162 			 char *buf)
163 {
164 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
165 	struct lm83_data *data = lm83_update_device(dev);
166 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
167 }
168 
169 static ssize_t temp_store(struct device *dev,
170 			  struct device_attribute *devattr, const char *buf,
171 			  size_t count)
172 {
173 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
174 	struct lm83_data *data = dev_get_drvdata(dev);
175 	struct i2c_client *client = data->client;
176 	long val;
177 	int nr = attr->index;
178 	int err;
179 
180 	err = kstrtol(buf, 10, &val);
181 	if (err < 0)
182 		return err;
183 
184 	mutex_lock(&data->update_lock);
185 	data->temp[nr] = TEMP_TO_REG(val);
186 	i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
187 				  data->temp[nr]);
188 	mutex_unlock(&data->update_lock);
189 	return count;
190 }
191 
192 static ssize_t alarms_show(struct device *dev, struct device_attribute *dummy,
193 			   char *buf)
194 {
195 	struct lm83_data *data = lm83_update_device(dev);
196 	return sprintf(buf, "%d\n", data->alarms);
197 }
198 
199 static ssize_t alarm_show(struct device *dev,
200 			  struct device_attribute *devattr, char *buf)
201 {
202 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
203 	struct lm83_data *data = lm83_update_device(dev);
204 	int bitnr = attr->index;
205 
206 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
207 }
208 
209 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
210 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
211 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
212 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
213 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 4);
214 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 5);
215 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 6);
216 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 7);
217 static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp, 8);
218 static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp, 8);
219 static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 8);
220 static SENSOR_DEVICE_ATTR_RO(temp4_crit, temp, 8);
221 
222 /* Individual alarm files */
223 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 0);
224 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, 1);
225 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
226 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, 4);
227 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
228 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 8);
229 static SENSOR_DEVICE_ATTR_RO(temp4_crit_alarm, alarm, 9);
230 static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, 10);
231 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, 12);
232 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 13);
233 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 15);
234 /* Raw alarm file for compatibility */
235 static DEVICE_ATTR_RO(alarms);
236 
237 static struct attribute *lm83_attributes[] = {
238 	&sensor_dev_attr_temp1_input.dev_attr.attr,
239 	&sensor_dev_attr_temp3_input.dev_attr.attr,
240 	&sensor_dev_attr_temp1_max.dev_attr.attr,
241 	&sensor_dev_attr_temp3_max.dev_attr.attr,
242 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
243 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
244 
245 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
246 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
247 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
248 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
249 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
250 	&dev_attr_alarms.attr,
251 	NULL
252 };
253 
254 static const struct attribute_group lm83_group = {
255 	.attrs = lm83_attributes,
256 };
257 
258 static struct attribute *lm83_attributes_opt[] = {
259 	&sensor_dev_attr_temp2_input.dev_attr.attr,
260 	&sensor_dev_attr_temp4_input.dev_attr.attr,
261 	&sensor_dev_attr_temp2_max.dev_attr.attr,
262 	&sensor_dev_attr_temp4_max.dev_attr.attr,
263 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
264 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
265 
266 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
267 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
268 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
269 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
270 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
271 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
272 	NULL
273 };
274 
275 static const struct attribute_group lm83_group_opt = {
276 	.attrs = lm83_attributes_opt,
277 };
278 
279 /*
280  * Real code
281  */
282 
283 /* Return 0 if detection is successful, -ENODEV otherwise */
284 static int lm83_detect(struct i2c_client *new_client,
285 		       struct i2c_board_info *info)
286 {
287 	struct i2c_adapter *adapter = new_client->adapter;
288 	const char *name;
289 	u8 man_id, chip_id;
290 
291 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
292 		return -ENODEV;
293 
294 	/* Detection */
295 	if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
296 	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
297 	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
298 		dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
299 			new_client->addr);
300 		return -ENODEV;
301 	}
302 
303 	/* Identification */
304 	man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
305 	if (man_id != 0x01)	/* National Semiconductor */
306 		return -ENODEV;
307 
308 	chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
309 	switch (chip_id) {
310 	case 0x03:
311 		name = "lm83";
312 		break;
313 	case 0x01:
314 		name = "lm82";
315 		break;
316 	default:
317 		/* identification failed */
318 		dev_info(&adapter->dev,
319 			 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
320 			 man_id, chip_id);
321 		return -ENODEV;
322 	}
323 
324 	strlcpy(info->type, name, I2C_NAME_SIZE);
325 
326 	return 0;
327 }
328 
329 static int lm83_probe(struct i2c_client *new_client,
330 		      const struct i2c_device_id *id)
331 {
332 	struct device *hwmon_dev;
333 	struct lm83_data *data;
334 
335 	data = devm_kzalloc(&new_client->dev, sizeof(struct lm83_data),
336 			    GFP_KERNEL);
337 	if (!data)
338 		return -ENOMEM;
339 
340 	data->client = new_client;
341 	mutex_init(&data->update_lock);
342 
343 	/*
344 	 * Register sysfs hooks
345 	 * The LM82 can only monitor one external diode which is
346 	 * at the same register as the LM83 temp3 entry - so we
347 	 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
348 	 */
349 	data->groups[0] = &lm83_group;
350 	if (id->driver_data == lm83)
351 		data->groups[1] = &lm83_group_opt;
352 
353 	hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
354 							   new_client->name,
355 							   data, data->groups);
356 	return PTR_ERR_OR_ZERO(hwmon_dev);
357 }
358 
359 /*
360  * Driver data (common to all clients)
361  */
362 
363 static const struct i2c_device_id lm83_id[] = {
364 	{ "lm83", lm83 },
365 	{ "lm82", lm82 },
366 	{ }
367 };
368 MODULE_DEVICE_TABLE(i2c, lm83_id);
369 
370 static struct i2c_driver lm83_driver = {
371 	.class		= I2C_CLASS_HWMON,
372 	.driver = {
373 		.name	= "lm83",
374 	},
375 	.probe		= lm83_probe,
376 	.id_table	= lm83_id,
377 	.detect		= lm83_detect,
378 	.address_list	= normal_i2c,
379 };
380 
381 module_i2c_driver(lm83_driver);
382 
383 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
384 MODULE_DESCRIPTION("LM83 driver");
385 MODULE_LICENSE("GPL");
386