hwmon: (f71805f) Convert to use devm_ functions
[linux-2.6-block.git] / drivers / hwmon / lm83.c
CommitLineData
1da177e4
LT
1/*
2 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
b57dc394 4 * Copyright (C) 2003-2009 Jean Delvare <khali@linux-fr.org>
1da177e4
LT
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 *
43cb7ebe
JC
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 *
1da177e4
LT
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 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
1da177e4
LT
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
10c08f81 39#include <linux/hwmon-sysfs.h>
943b0830
MH
40#include <linux/hwmon.h>
41#include <linux/err.h>
9a61bf63 42#include <linux/mutex.h>
0e39e01c 43#include <linux/sysfs.h>
1da177e4
LT
44
45/*
46 * Addresses to scan
47 * Address is selected using 2 three-level pins, resulting in 9 possible
48 * addresses.
49 */
50
25e9c86d
MH
51static const unsigned short normal_i2c[] = {
52 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
1da177e4 53
e5e9f44c 54enum chips { lm83, lm82 };
1da177e4
LT
55
56/*
57 * The LM83 registers
58 * Manufacturer ID is 0x01 for National Semiconductor.
59 */
60
61#define LM83_REG_R_MAN_ID 0xFE
62#define LM83_REG_R_CHIP_ID 0xFF
63#define LM83_REG_R_CONFIG 0x03
64#define LM83_REG_W_CONFIG 0x09
65#define LM83_REG_R_STATUS1 0x02
66#define LM83_REG_R_STATUS2 0x35
67#define LM83_REG_R_LOCAL_TEMP 0x00
68#define LM83_REG_R_LOCAL_HIGH 0x05
69#define LM83_REG_W_LOCAL_HIGH 0x0B
70#define LM83_REG_R_REMOTE1_TEMP 0x30
71#define LM83_REG_R_REMOTE1_HIGH 0x38
72#define LM83_REG_W_REMOTE1_HIGH 0x50
73#define LM83_REG_R_REMOTE2_TEMP 0x01
74#define LM83_REG_R_REMOTE2_HIGH 0x07
75#define LM83_REG_W_REMOTE2_HIGH 0x0D
76#define LM83_REG_R_REMOTE3_TEMP 0x31
77#define LM83_REG_R_REMOTE3_HIGH 0x3A
78#define LM83_REG_W_REMOTE3_HIGH 0x52
79#define LM83_REG_R_TCRIT 0x42
80#define LM83_REG_W_TCRIT 0x5A
81
82/*
83 * Conversions and various macros
44bbe87e 84 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
1da177e4
LT
85 */
86
87#define TEMP_FROM_REG(val) ((val) * 1000)
88#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
89 (val) >= 127000 ? 127 : \
90 (val) < 0 ? ((val) - 500) / 1000 : \
91 ((val) + 500) / 1000)
92
93static const u8 LM83_REG_R_TEMP[] = {
94 LM83_REG_R_LOCAL_TEMP,
95 LM83_REG_R_REMOTE1_TEMP,
96 LM83_REG_R_REMOTE2_TEMP,
1a86c051 97 LM83_REG_R_REMOTE3_TEMP,
1da177e4
LT
98 LM83_REG_R_LOCAL_HIGH,
99 LM83_REG_R_REMOTE1_HIGH,
100 LM83_REG_R_REMOTE2_HIGH,
1a86c051
JD
101 LM83_REG_R_REMOTE3_HIGH,
102 LM83_REG_R_TCRIT,
1da177e4
LT
103};
104
105static const u8 LM83_REG_W_HIGH[] = {
106 LM83_REG_W_LOCAL_HIGH,
107 LM83_REG_W_REMOTE1_HIGH,
108 LM83_REG_W_REMOTE2_HIGH,
1a86c051
JD
109 LM83_REG_W_REMOTE3_HIGH,
110 LM83_REG_W_TCRIT,
1da177e4
LT
111};
112
113/*
114 * Functions declaration
115 */
116
310ec792 117static int lm83_detect(struct i2c_client *new_client,
b6aacdce
JD
118 struct i2c_board_info *info);
119static int lm83_probe(struct i2c_client *client,
120 const struct i2c_device_id *id);
121static int lm83_remove(struct i2c_client *client);
1da177e4
LT
122static struct lm83_data *lm83_update_device(struct device *dev);
123
124/*
125 * Driver data (common to all clients)
126 */
b3789a0d 127
b6aacdce
JD
128static const struct i2c_device_id lm83_id[] = {
129 { "lm83", lm83 },
130 { "lm82", lm82 },
131 { }
132};
133MODULE_DEVICE_TABLE(i2c, lm83_id);
134
1da177e4 135static struct i2c_driver lm83_driver = {
b6aacdce 136 .class = I2C_CLASS_HWMON,
cdaf7934 137 .driver = {
cdaf7934
LR
138 .name = "lm83",
139 },
b6aacdce
JD
140 .probe = lm83_probe,
141 .remove = lm83_remove,
142 .id_table = lm83_id,
143 .detect = lm83_detect,
c3813d6a 144 .address_list = normal_i2c,
1da177e4
LT
145};
146
147/*
148 * Client data (each client gets its own)
149 */
150
151struct lm83_data {
1beeffe4 152 struct device *hwmon_dev;
9a61bf63 153 struct mutex update_lock;
1da177e4
LT
154 char valid; /* zero until following fields are valid */
155 unsigned long last_updated; /* in jiffies */
156
157 /* registers values */
1a86c051
JD
158 s8 temp[9]; /* 0..3: input 1-4,
159 4..7: high limit 1-4,
160 8 : critical limit */
1da177e4
LT
161 u16 alarms; /* bitvector, combined */
162};
163
164/*
165 * Sysfs stuff
166 */
167
1a86c051
JD
168static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
169 char *buf)
170{
171 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
172 struct lm83_data *data = lm83_update_device(dev);
173 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
1da177e4 174}
1a86c051
JD
175
176static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
177 const char *buf, size_t count)
178{
179 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
180 struct i2c_client *client = to_i2c_client(dev);
181 struct lm83_data *data = i2c_get_clientdata(client);
b3789a0d 182 long val;
1a86c051 183 int nr = attr->index;
b3789a0d
FM
184 int err;
185
186 err = kstrtol(buf, 10, &val);
187 if (err < 0)
188 return err;
1a86c051 189
9a61bf63 190 mutex_lock(&data->update_lock);
1a86c051
JD
191 data->temp[nr] = TEMP_TO_REG(val);
192 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
193 data->temp[nr]);
9a61bf63 194 mutex_unlock(&data->update_lock);
1a86c051 195 return count;
1da177e4 196}
1da177e4 197
1a86c051
JD
198static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
199 char *buf)
1da177e4
LT
200{
201 struct lm83_data *data = lm83_update_device(dev);
202 return sprintf(buf, "%d\n", data->alarms);
203}
204
2d45771e
JD
205static ssize_t show_alarm(struct device *dev, struct device_attribute
206 *devattr, char *buf)
207{
208 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
209 struct lm83_data *data = lm83_update_device(dev);
210 int bitnr = attr->index;
211
212 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
213}
214
1a86c051
JD
215static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
216static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
217static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
218static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
219static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
220 set_temp, 4);
221static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
222 set_temp, 5);
223static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
224 set_temp, 6);
225static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
226 set_temp, 7);
227static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
228static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
229static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
230 set_temp, 8);
231static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
2d45771e
JD
232
233/* Individual alarm files */
234static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
235static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
7817a39e 236static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
2d45771e
JD
237static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
238static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
239static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
240static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
7817a39e 241static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
2d45771e 242static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
7817a39e 243static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
2d45771e
JD
244static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
245/* Raw alarm file for compatibility */
1da177e4
LT
246static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
247
0e39e01c
JD
248static struct attribute *lm83_attributes[] = {
249 &sensor_dev_attr_temp1_input.dev_attr.attr,
250 &sensor_dev_attr_temp3_input.dev_attr.attr,
251 &sensor_dev_attr_temp1_max.dev_attr.attr,
252 &sensor_dev_attr_temp3_max.dev_attr.attr,
253 &sensor_dev_attr_temp1_crit.dev_attr.attr,
254 &sensor_dev_attr_temp3_crit.dev_attr.attr,
255
256 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
257 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
7817a39e 258 &sensor_dev_attr_temp3_fault.dev_attr.attr,
0e39e01c
JD
259 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
260 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
261 &dev_attr_alarms.attr,
262 NULL
263};
264
265static const struct attribute_group lm83_group = {
266 .attrs = lm83_attributes,
267};
268
269static struct attribute *lm83_attributes_opt[] = {
270 &sensor_dev_attr_temp2_input.dev_attr.attr,
271 &sensor_dev_attr_temp4_input.dev_attr.attr,
272 &sensor_dev_attr_temp2_max.dev_attr.attr,
273 &sensor_dev_attr_temp4_max.dev_attr.attr,
274 &sensor_dev_attr_temp2_crit.dev_attr.attr,
275 &sensor_dev_attr_temp4_crit.dev_attr.attr,
276
277 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
278 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
7817a39e 279 &sensor_dev_attr_temp4_fault.dev_attr.attr,
0e39e01c 280 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
7817a39e 281 &sensor_dev_attr_temp2_fault.dev_attr.attr,
0e39e01c
JD
282 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
283 NULL
284};
285
286static const struct attribute_group lm83_group_opt = {
287 .attrs = lm83_attributes_opt,
288};
289
1da177e4
LT
290/*
291 * Real code
292 */
293
b6aacdce 294/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 295static int lm83_detect(struct i2c_client *new_client,
b6aacdce 296 struct i2c_board_info *info)
1da177e4 297{
b6aacdce 298 struct i2c_adapter *adapter = new_client->adapter;
b57dc394
JD
299 const char *name;
300 u8 man_id, chip_id;
1da177e4
LT
301
302 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
b6aacdce 303 return -ENODEV;
1da177e4 304
b57dc394
JD
305 /* Detection */
306 if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
307 (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
308 (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
309 dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
310 new_client->addr);
311 return -ENODEV;
1da177e4
LT
312 }
313
b57dc394
JD
314 /* Identification */
315 man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
316 if (man_id != 0x01) /* National Semiconductor */
317 return -ENODEV;
1da177e4 318
b57dc394
JD
319 chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
320 switch (chip_id) {
321 case 0x03:
1da177e4 322 name = "lm83";
b57dc394
JD
323 break;
324 case 0x01:
43cb7ebe 325 name = "lm82";
b57dc394
JD
326 break;
327 default:
328 /* identification failed */
329 dev_info(&adapter->dev,
330 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
331 man_id, chip_id);
332 return -ENODEV;
1da177e4
LT
333 }
334
b6aacdce
JD
335 strlcpy(info->type, name, I2C_NAME_SIZE);
336
337 return 0;
338}
339
340static int lm83_probe(struct i2c_client *new_client,
341 const struct i2c_device_id *id)
342{
343 struct lm83_data *data;
344 int err;
345
346 data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL);
347 if (!data) {
348 err = -ENOMEM;
349 goto exit;
350 }
351
352 i2c_set_clientdata(new_client, data);
1da177e4 353 data->valid = 0;
9a61bf63 354 mutex_init(&data->update_lock);
1da177e4 355
1da177e4 356 /*
0e39e01c 357 * Register sysfs hooks
43cb7ebe
JC
358 * The LM82 can only monitor one external diode which is
359 * at the same register as the LM83 temp3 entry - so we
360 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
361 */
362
b3789a0d
FM
363 err = sysfs_create_group(&new_client->dev.kobj, &lm83_group);
364 if (err)
b6aacdce 365 goto exit_free;
1da177e4 366
b6aacdce 367 if (id->driver_data == lm83) {
b3789a0d
FM
368 err = sysfs_create_group(&new_client->dev.kobj,
369 &lm83_group_opt);
370 if (err)
0e39e01c
JD
371 goto exit_remove_files;
372 }
373
1beeffe4
TJ
374 data->hwmon_dev = hwmon_device_register(&new_client->dev);
375 if (IS_ERR(data->hwmon_dev)) {
376 err = PTR_ERR(data->hwmon_dev);
0e39e01c 377 goto exit_remove_files;
43cb7ebe
JC
378 }
379
1da177e4
LT
380 return 0;
381
0e39e01c
JD
382exit_remove_files:
383 sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
384 sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
1da177e4
LT
385exit_free:
386 kfree(data);
387exit:
388 return err;
389}
390
b6aacdce 391static int lm83_remove(struct i2c_client *client)
1da177e4 392{
943b0830 393 struct lm83_data *data = i2c_get_clientdata(client);
1da177e4 394
1beeffe4 395 hwmon_device_unregister(data->hwmon_dev);
0e39e01c
JD
396 sysfs_remove_group(&client->dev.kobj, &lm83_group);
397 sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
943b0830 398
943b0830 399 kfree(data);
1da177e4
LT
400 return 0;
401}
402
403static struct lm83_data *lm83_update_device(struct device *dev)
404{
405 struct i2c_client *client = to_i2c_client(dev);
406 struct lm83_data *data = i2c_get_clientdata(client);
407
9a61bf63 408 mutex_lock(&data->update_lock);
1da177e4
LT
409
410 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
411 int nr;
412
413 dev_dbg(&client->dev, "Updating lm83 data.\n");
1a86c051
JD
414 for (nr = 0; nr < 9; nr++) {
415 data->temp[nr] =
1da177e4
LT
416 i2c_smbus_read_byte_data(client,
417 LM83_REG_R_TEMP[nr]);
1da177e4 418 }
1da177e4
LT
419 data->alarms =
420 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
421 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
422 << 8);
423
424 data->last_updated = jiffies;
425 data->valid = 1;
426 }
427
9a61bf63 428 mutex_unlock(&data->update_lock);
1da177e4
LT
429
430 return data;
431}
432
f0967eea 433module_i2c_driver(lm83_driver);
1da177e4
LT
434
435MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
436MODULE_DESCRIPTION("LM83 driver");
437MODULE_LICENSE("GPL");