]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/lm83.c
hwmon: New driver for Analog Devices ADT7473 sensor chip
[mirror_ubuntu-bionic-kernel.git] / drivers / hwmon / lm83.c
CommitLineData
1da177e4
LT
1/*
2 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
2d45771e 4 * Copyright (C) 2003-2006 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
51static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
52 0x29, 0x2a, 0x2b,
53 0x4c, 0x4d, 0x4e,
54 I2C_CLIENT_END };
1da177e4
LT
55
56/*
57 * Insmod parameters
58 */
59
43cb7ebe 60I2C_CLIENT_INSMOD_2(lm83, lm82);
1da177e4
LT
61
62/*
63 * The LM83 registers
64 * Manufacturer ID is 0x01 for National Semiconductor.
65 */
66
67#define LM83_REG_R_MAN_ID 0xFE
68#define LM83_REG_R_CHIP_ID 0xFF
69#define LM83_REG_R_CONFIG 0x03
70#define LM83_REG_W_CONFIG 0x09
71#define LM83_REG_R_STATUS1 0x02
72#define LM83_REG_R_STATUS2 0x35
73#define LM83_REG_R_LOCAL_TEMP 0x00
74#define LM83_REG_R_LOCAL_HIGH 0x05
75#define LM83_REG_W_LOCAL_HIGH 0x0B
76#define LM83_REG_R_REMOTE1_TEMP 0x30
77#define LM83_REG_R_REMOTE1_HIGH 0x38
78#define LM83_REG_W_REMOTE1_HIGH 0x50
79#define LM83_REG_R_REMOTE2_TEMP 0x01
80#define LM83_REG_R_REMOTE2_HIGH 0x07
81#define LM83_REG_W_REMOTE2_HIGH 0x0D
82#define LM83_REG_R_REMOTE3_TEMP 0x31
83#define LM83_REG_R_REMOTE3_HIGH 0x3A
84#define LM83_REG_W_REMOTE3_HIGH 0x52
85#define LM83_REG_R_TCRIT 0x42
86#define LM83_REG_W_TCRIT 0x5A
87
88/*
89 * Conversions and various macros
44bbe87e 90 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
1da177e4
LT
91 */
92
93#define TEMP_FROM_REG(val) ((val) * 1000)
94#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
95 (val) >= 127000 ? 127 : \
96 (val) < 0 ? ((val) - 500) / 1000 : \
97 ((val) + 500) / 1000)
98
99static const u8 LM83_REG_R_TEMP[] = {
100 LM83_REG_R_LOCAL_TEMP,
101 LM83_REG_R_REMOTE1_TEMP,
102 LM83_REG_R_REMOTE2_TEMP,
1a86c051 103 LM83_REG_R_REMOTE3_TEMP,
1da177e4
LT
104 LM83_REG_R_LOCAL_HIGH,
105 LM83_REG_R_REMOTE1_HIGH,
106 LM83_REG_R_REMOTE2_HIGH,
1a86c051
JD
107 LM83_REG_R_REMOTE3_HIGH,
108 LM83_REG_R_TCRIT,
1da177e4
LT
109};
110
111static const u8 LM83_REG_W_HIGH[] = {
112 LM83_REG_W_LOCAL_HIGH,
113 LM83_REG_W_REMOTE1_HIGH,
114 LM83_REG_W_REMOTE2_HIGH,
1a86c051
JD
115 LM83_REG_W_REMOTE3_HIGH,
116 LM83_REG_W_TCRIT,
1da177e4
LT
117};
118
119/*
120 * Functions declaration
121 */
122
123static int lm83_attach_adapter(struct i2c_adapter *adapter);
124static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
125static int lm83_detach_client(struct i2c_client *client);
126static struct lm83_data *lm83_update_device(struct device *dev);
127
128/*
129 * Driver data (common to all clients)
130 */
131
132static struct i2c_driver lm83_driver = {
cdaf7934 133 .driver = {
cdaf7934
LR
134 .name = "lm83",
135 },
1da177e4
LT
136 .attach_adapter = lm83_attach_adapter,
137 .detach_client = lm83_detach_client,
138};
139
140/*
141 * Client data (each client gets its own)
142 */
143
144struct lm83_data {
145 struct i2c_client client;
1beeffe4 146 struct device *hwmon_dev;
9a61bf63 147 struct mutex update_lock;
1da177e4
LT
148 char valid; /* zero until following fields are valid */
149 unsigned long last_updated; /* in jiffies */
150
151 /* registers values */
1a86c051
JD
152 s8 temp[9]; /* 0..3: input 1-4,
153 4..7: high limit 1-4,
154 8 : critical limit */
1da177e4
LT
155 u16 alarms; /* bitvector, combined */
156};
157
158/*
159 * Sysfs stuff
160 */
161
1a86c051
JD
162static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
163 char *buf)
164{
165 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
166 struct lm83_data *data = lm83_update_device(dev);
167 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
1da177e4 168}
1a86c051
JD
169
170static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
171 const char *buf, size_t count)
172{
173 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
174 struct i2c_client *client = to_i2c_client(dev);
175 struct lm83_data *data = i2c_get_clientdata(client);
176 long val = simple_strtol(buf, NULL, 10);
177 int nr = attr->index;
178
9a61bf63 179 mutex_lock(&data->update_lock);
1a86c051
JD
180 data->temp[nr] = TEMP_TO_REG(val);
181 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
182 data->temp[nr]);
9a61bf63 183 mutex_unlock(&data->update_lock);
1a86c051 184 return count;
1da177e4 185}
1da177e4 186
1a86c051
JD
187static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
188 char *buf)
1da177e4
LT
189{
190 struct lm83_data *data = lm83_update_device(dev);
191 return sprintf(buf, "%d\n", data->alarms);
192}
193
2d45771e
JD
194static ssize_t show_alarm(struct device *dev, struct device_attribute
195 *devattr, char *buf)
196{
197 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
198 struct lm83_data *data = lm83_update_device(dev);
199 int bitnr = attr->index;
200
201 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
202}
203
1a86c051
JD
204static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
205static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
206static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
207static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
208static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
209 set_temp, 4);
210static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
211 set_temp, 5);
212static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
213 set_temp, 6);
214static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
215 set_temp, 7);
216static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
217static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
218static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
219 set_temp, 8);
220static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
2d45771e
JD
221
222/* Individual alarm files */
223static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
224static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
7817a39e 225static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
2d45771e
JD
226static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
227static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
228static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
229static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
7817a39e 230static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
2d45771e 231static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
7817a39e 232static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
2d45771e
JD
233static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
234/* Raw alarm file for compatibility */
1da177e4
LT
235static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
236
0e39e01c
JD
237static 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,
7817a39e 247 &sensor_dev_attr_temp3_fault.dev_attr.attr,
0e39e01c
JD
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
254static const struct attribute_group lm83_group = {
255 .attrs = lm83_attributes,
256};
257
258static 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,
7817a39e 268 &sensor_dev_attr_temp4_fault.dev_attr.attr,
0e39e01c 269 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
7817a39e 270 &sensor_dev_attr_temp2_fault.dev_attr.attr,
0e39e01c
JD
271 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
272 NULL
273};
274
275static const struct attribute_group lm83_group_opt = {
276 .attrs = lm83_attributes_opt,
277};
278
1da177e4
LT
279/*
280 * Real code
281 */
282
283static int lm83_attach_adapter(struct i2c_adapter *adapter)
284{
285 if (!(adapter->class & I2C_CLASS_HWMON))
286 return 0;
2ed2dc3c 287 return i2c_probe(adapter, &addr_data, lm83_detect);
1da177e4
LT
288}
289
290/*
291 * The following function does more than just detection. If detection
292 * succeeds, it also registers the new chip.
293 */
294static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
295{
296 struct i2c_client *new_client;
297 struct lm83_data *data;
298 int err = 0;
299 const char *name = "";
300
301 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
302 goto exit;
303
ba9c2e8d 304 if (!(data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
1da177e4
LT
305 err = -ENOMEM;
306 goto exit;
307 }
1da177e4
LT
308
309 /* The common I2C client data is placed right after the
310 * LM83-specific data. */
311 new_client = &data->client;
312 i2c_set_clientdata(new_client, data);
313 new_client->addr = address;
314 new_client->adapter = adapter;
315 new_client->driver = &lm83_driver;
316 new_client->flags = 0;
317
318 /* Now we do the detection and identification. A negative kind
319 * means that the driver was loaded with no force parameter
320 * (default), so we must both detect and identify the chip
321 * (actually there is only one possible kind of chip for now, LM83).
322 * A zero kind means that the driver was loaded with the force
323 * parameter, the detection step shall be skipped. A positive kind
324 * means that the driver was loaded with the force parameter and a
325 * given kind of chip is requested, so both the detection and the
326 * identification steps are skipped. */
327
328 /* Default to an LM83 if forced */
329 if (kind == 0)
330 kind = lm83;
331
332 if (kind < 0) { /* detection */
333 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
334 & 0xA8) != 0x00) ||
335 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
336 & 0x48) != 0x00) ||
337 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
338 & 0x41) != 0x00)) {
339 dev_dbg(&adapter->dev,
340 "LM83 detection failed at 0x%02x.\n", address);
341 goto exit_free;
342 }
343 }
344
345 if (kind <= 0) { /* identification */
346 u8 man_id, chip_id;
347
348 man_id = i2c_smbus_read_byte_data(new_client,
349 LM83_REG_R_MAN_ID);
350 chip_id = i2c_smbus_read_byte_data(new_client,
351 LM83_REG_R_CHIP_ID);
352
353 if (man_id == 0x01) { /* National Semiconductor */
354 if (chip_id == 0x03) {
355 kind = lm83;
43cb7ebe
JC
356 } else
357 if (chip_id == 0x01) {
358 kind = lm82;
1da177e4
LT
359 }
360 }
361
362 if (kind <= 0) { /* identification failed */
363 dev_info(&adapter->dev,
364 "Unsupported chip (man_id=0x%02X, "
365 "chip_id=0x%02X).\n", man_id, chip_id);
366 goto exit_free;
367 }
368 }
369
370 if (kind == lm83) {
371 name = "lm83";
43cb7ebe
JC
372 } else
373 if (kind == lm82) {
374 name = "lm82";
1da177e4
LT
375 }
376
377 /* We can fill in the remaining client fields */
378 strlcpy(new_client->name, name, I2C_NAME_SIZE);
379 data->valid = 0;
9a61bf63 380 mutex_init(&data->update_lock);
1da177e4
LT
381
382 /* Tell the I2C layer a new client has arrived */
383 if ((err = i2c_attach_client(new_client)))
384 goto exit_free;
385
386 /*
0e39e01c 387 * Register sysfs hooks
43cb7ebe
JC
388 * The LM82 can only monitor one external diode which is
389 * at the same register as the LM83 temp3 entry - so we
390 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
391 */
392
0e39e01c
JD
393 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
394 goto exit_detach;
1da177e4 395
43cb7ebe 396 if (kind == lm83) {
0e39e01c
JD
397 if ((err = sysfs_create_group(&new_client->dev.kobj,
398 &lm83_group_opt)))
399 goto exit_remove_files;
400 }
401
1beeffe4
TJ
402 data->hwmon_dev = hwmon_device_register(&new_client->dev);
403 if (IS_ERR(data->hwmon_dev)) {
404 err = PTR_ERR(data->hwmon_dev);
0e39e01c 405 goto exit_remove_files;
43cb7ebe
JC
406 }
407
1da177e4
LT
408 return 0;
409
0e39e01c
JD
410exit_remove_files:
411 sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
412 sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
943b0830
MH
413exit_detach:
414 i2c_detach_client(new_client);
1da177e4
LT
415exit_free:
416 kfree(data);
417exit:
418 return err;
419}
420
421static int lm83_detach_client(struct i2c_client *client)
422{
943b0830 423 struct lm83_data *data = i2c_get_clientdata(client);
1da177e4
LT
424 int err;
425
1beeffe4 426 hwmon_device_unregister(data->hwmon_dev);
0e39e01c
JD
427 sysfs_remove_group(&client->dev.kobj, &lm83_group);
428 sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
943b0830 429
7bef5594 430 if ((err = i2c_detach_client(client)))
1da177e4 431 return err;
1da177e4 432
943b0830 433 kfree(data);
1da177e4
LT
434 return 0;
435}
436
437static struct lm83_data *lm83_update_device(struct device *dev)
438{
439 struct i2c_client *client = to_i2c_client(dev);
440 struct lm83_data *data = i2c_get_clientdata(client);
441
9a61bf63 442 mutex_lock(&data->update_lock);
1da177e4
LT
443
444 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
445 int nr;
446
447 dev_dbg(&client->dev, "Updating lm83 data.\n");
1a86c051
JD
448 for (nr = 0; nr < 9; nr++) {
449 data->temp[nr] =
1da177e4
LT
450 i2c_smbus_read_byte_data(client,
451 LM83_REG_R_TEMP[nr]);
1da177e4 452 }
1da177e4
LT
453 data->alarms =
454 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
455 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
456 << 8);
457
458 data->last_updated = jiffies;
459 data->valid = 1;
460 }
461
9a61bf63 462 mutex_unlock(&data->update_lock);
1da177e4
LT
463
464 return data;
465}
466
467static int __init sensors_lm83_init(void)
468{
469 return i2c_add_driver(&lm83_driver);
470}
471
472static void __exit sensors_lm83_exit(void)
473{
474 i2c_del_driver(&lm83_driver);
475}
476
477MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
478MODULE_DESCRIPTION("LM83 driver");
479MODULE_LICENSE("GPL");
480
481module_init(sensors_lm83_init);
482module_exit(sensors_lm83_exit);