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