1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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>
39#include <linux/hwmon-sysfs.h>
40#include <linux/hwmon.h>
41#include <linux/err.h>
42#include <linux/mutex.h>
43#include <linux/sysfs.h>
44
45
46
47
48
49
50
51static const unsigned short normal_i2c[] = {
52 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
53
54enum chips { lm83, lm82 };
55
56
57
58
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
84
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,
97 LM83_REG_R_REMOTE3_TEMP,
98 LM83_REG_R_LOCAL_HIGH,
99 LM83_REG_R_REMOTE1_HIGH,
100 LM83_REG_R_REMOTE2_HIGH,
101 LM83_REG_R_REMOTE3_HIGH,
102 LM83_REG_R_TCRIT,
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,
109 LM83_REG_W_REMOTE3_HIGH,
110 LM83_REG_W_TCRIT,
111};
112
113
114
115
116
117static int lm83_detect(struct i2c_client *new_client,
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);
122static struct lm83_data *lm83_update_device(struct device *dev);
123
124
125
126
127
128static const struct i2c_device_id lm83_id[] = {
129 { "lm83", lm83 },
130 { "lm82", lm82 },
131 { }
132};
133MODULE_DEVICE_TABLE(i2c, lm83_id);
134
135static struct i2c_driver lm83_driver = {
136 .class = I2C_CLASS_HWMON,
137 .driver = {
138 .name = "lm83",
139 },
140 .probe = lm83_probe,
141 .remove = lm83_remove,
142 .id_table = lm83_id,
143 .detect = lm83_detect,
144 .address_list = normal_i2c,
145};
146
147
148
149
150
151struct lm83_data {
152 struct device *hwmon_dev;
153 struct mutex update_lock;
154 char valid;
155 unsigned long last_updated;
156
157
158 s8 temp[9];
159
160
161 u16 alarms;
162};
163
164
165
166
167
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]));
174}
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);
182 long val = simple_strtol(buf, NULL, 10);
183 int nr = attr->index;
184
185 mutex_lock(&data->update_lock);
186 data->temp[nr] = TEMP_TO_REG(val);
187 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
188 data->temp[nr]);
189 mutex_unlock(&data->update_lock);
190 return count;
191}
192
193static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
194 char *buf)
195{
196 struct lm83_data *data = lm83_update_device(dev);
197 return sprintf(buf, "%d\n", data->alarms);
198}
199
200static ssize_t show_alarm(struct device *dev, struct device_attribute
201 *devattr, char *buf)
202{
203 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
204 struct lm83_data *data = lm83_update_device(dev);
205 int bitnr = attr->index;
206
207 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
208}
209
210static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
211static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
212static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
213static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
214static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
215 set_temp, 4);
216static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
217 set_temp, 5);
218static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
219 set_temp, 6);
220static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
221 set_temp, 7);
222static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
223static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
224static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
225 set_temp, 8);
226static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
227
228
229static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
230static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
231static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
232static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
233static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
234static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
235static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
236static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
237static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
238static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
239static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
240
241static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
242
243static struct attribute *lm83_attributes[] = {
244 &sensor_dev_attr_temp1_input.dev_attr.attr,
245 &sensor_dev_attr_temp3_input.dev_attr.attr,
246 &sensor_dev_attr_temp1_max.dev_attr.attr,
247 &sensor_dev_attr_temp3_max.dev_attr.attr,
248 &sensor_dev_attr_temp1_crit.dev_attr.attr,
249 &sensor_dev_attr_temp3_crit.dev_attr.attr,
250
251 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
252 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
253 &sensor_dev_attr_temp3_fault.dev_attr.attr,
254 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
255 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
256 &dev_attr_alarms.attr,
257 NULL
258};
259
260static const struct attribute_group lm83_group = {
261 .attrs = lm83_attributes,
262};
263
264static struct attribute *lm83_attributes_opt[] = {
265 &sensor_dev_attr_temp2_input.dev_attr.attr,
266 &sensor_dev_attr_temp4_input.dev_attr.attr,
267 &sensor_dev_attr_temp2_max.dev_attr.attr,
268 &sensor_dev_attr_temp4_max.dev_attr.attr,
269 &sensor_dev_attr_temp2_crit.dev_attr.attr,
270 &sensor_dev_attr_temp4_crit.dev_attr.attr,
271
272 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
273 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
274 &sensor_dev_attr_temp4_fault.dev_attr.attr,
275 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
276 &sensor_dev_attr_temp2_fault.dev_attr.attr,
277 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
278 NULL
279};
280
281static const struct attribute_group lm83_group_opt = {
282 .attrs = lm83_attributes_opt,
283};
284
285
286
287
288
289
290static int lm83_detect(struct i2c_client *new_client,
291 struct i2c_board_info *info)
292{
293 struct i2c_adapter *adapter = new_client->adapter;
294 const char *name;
295 u8 man_id, chip_id;
296
297 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
298 return -ENODEV;
299
300
301 if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
302 (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
303 (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
304 dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
305 new_client->addr);
306 return -ENODEV;
307 }
308
309
310 man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
311 if (man_id != 0x01)
312 return -ENODEV;
313
314 chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
315 switch (chip_id) {
316 case 0x03:
317 name = "lm83";
318 break;
319 case 0x01:
320 name = "lm82";
321 break;
322 default:
323
324 dev_info(&adapter->dev,
325 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
326 man_id, chip_id);
327 return -ENODEV;
328 }
329
330 strlcpy(info->type, name, I2C_NAME_SIZE);
331
332 return 0;
333}
334
335static int lm83_probe(struct i2c_client *new_client,
336 const struct i2c_device_id *id)
337{
338 struct lm83_data *data;
339 int err;
340
341 data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL);
342 if (!data) {
343 err = -ENOMEM;
344 goto exit;
345 }
346
347 i2c_set_clientdata(new_client, data);
348 data->valid = 0;
349 mutex_init(&data->update_lock);
350
351
352
353
354
355
356
357
358 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
359 goto exit_free;
360
361 if (id->driver_data == lm83) {
362 if ((err = sysfs_create_group(&new_client->dev.kobj,
363 &lm83_group_opt)))
364 goto exit_remove_files;
365 }
366
367 data->hwmon_dev = hwmon_device_register(&new_client->dev);
368 if (IS_ERR(data->hwmon_dev)) {
369 err = PTR_ERR(data->hwmon_dev);
370 goto exit_remove_files;
371 }
372
373 return 0;
374
375exit_remove_files:
376 sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
377 sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
378exit_free:
379 kfree(data);
380exit:
381 return err;
382}
383
384static int lm83_remove(struct i2c_client *client)
385{
386 struct lm83_data *data = i2c_get_clientdata(client);
387
388 hwmon_device_unregister(data->hwmon_dev);
389 sysfs_remove_group(&client->dev.kobj, &lm83_group);
390 sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
391
392 kfree(data);
393 return 0;
394}
395
396static struct lm83_data *lm83_update_device(struct device *dev)
397{
398 struct i2c_client *client = to_i2c_client(dev);
399 struct lm83_data *data = i2c_get_clientdata(client);
400
401 mutex_lock(&data->update_lock);
402
403 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
404 int nr;
405
406 dev_dbg(&client->dev, "Updating lm83 data.\n");
407 for (nr = 0; nr < 9; nr++) {
408 data->temp[nr] =
409 i2c_smbus_read_byte_data(client,
410 LM83_REG_R_TEMP[nr]);
411 }
412 data->alarms =
413 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
414 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
415 << 8);
416
417 data->last_updated = jiffies;
418 data->valid = 1;
419 }
420
421 mutex_unlock(&data->update_lock);
422
423 return data;
424}
425
426static int __init sensors_lm83_init(void)
427{
428 return i2c_add_driver(&lm83_driver);
429}
430
431static void __exit sensors_lm83_exit(void)
432{
433 i2c_del_driver(&lm83_driver);
434}
435
436MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
437MODULE_DESCRIPTION("LM83 driver");
438MODULE_LICENSE("GPL");
439
440module_init(sensors_lm83_init);
441module_exit(sensors_lm83_exit);
442