[PATCH] I2C hwmon: add hwmon sysfs class to drivers
[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
1a86c051 4 * Copyright (C) 2003-2005 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 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
35#include <linux/i2c-sensor.h>
10c08f81 36#include <linux/hwmon-sysfs.h>
943b0830
MH
37#include <linux/hwmon.h>
38#include <linux/err.h>
1da177e4
LT
39
40/*
41 * Addresses to scan
42 * Address is selected using 2 three-level pins, resulting in 9 possible
43 * addresses.
44 */
45
46static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
47 0x29, 0x2a, 0x2b,
48 0x4c, 0x4d, 0x4e,
49 I2C_CLIENT_END };
50static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
51
52/*
53 * Insmod parameters
54 */
55
56SENSORS_INSMOD_1(lm83);
57
58/*
59 * The LM83 registers
60 * Manufacturer ID is 0x01 for National Semiconductor.
61 */
62
63#define LM83_REG_R_MAN_ID 0xFE
64#define LM83_REG_R_CHIP_ID 0xFF
65#define LM83_REG_R_CONFIG 0x03
66#define LM83_REG_W_CONFIG 0x09
67#define LM83_REG_R_STATUS1 0x02
68#define LM83_REG_R_STATUS2 0x35
69#define LM83_REG_R_LOCAL_TEMP 0x00
70#define LM83_REG_R_LOCAL_HIGH 0x05
71#define LM83_REG_W_LOCAL_HIGH 0x0B
72#define LM83_REG_R_REMOTE1_TEMP 0x30
73#define LM83_REG_R_REMOTE1_HIGH 0x38
74#define LM83_REG_W_REMOTE1_HIGH 0x50
75#define LM83_REG_R_REMOTE2_TEMP 0x01
76#define LM83_REG_R_REMOTE2_HIGH 0x07
77#define LM83_REG_W_REMOTE2_HIGH 0x0D
78#define LM83_REG_R_REMOTE3_TEMP 0x31
79#define LM83_REG_R_REMOTE3_HIGH 0x3A
80#define LM83_REG_W_REMOTE3_HIGH 0x52
81#define LM83_REG_R_TCRIT 0x42
82#define LM83_REG_W_TCRIT 0x5A
83
84/*
85 * Conversions and various macros
44bbe87e 86 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
1da177e4
LT
87 */
88
89#define TEMP_FROM_REG(val) ((val) * 1000)
90#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
91 (val) >= 127000 ? 127 : \
92 (val) < 0 ? ((val) - 500) / 1000 : \
93 ((val) + 500) / 1000)
94
95static const u8 LM83_REG_R_TEMP[] = {
96 LM83_REG_R_LOCAL_TEMP,
97 LM83_REG_R_REMOTE1_TEMP,
98 LM83_REG_R_REMOTE2_TEMP,
1a86c051 99 LM83_REG_R_REMOTE3_TEMP,
1da177e4
LT
100 LM83_REG_R_LOCAL_HIGH,
101 LM83_REG_R_REMOTE1_HIGH,
102 LM83_REG_R_REMOTE2_HIGH,
1a86c051
JD
103 LM83_REG_R_REMOTE3_HIGH,
104 LM83_REG_R_TCRIT,
1da177e4
LT
105};
106
107static const u8 LM83_REG_W_HIGH[] = {
108 LM83_REG_W_LOCAL_HIGH,
109 LM83_REG_W_REMOTE1_HIGH,
110 LM83_REG_W_REMOTE2_HIGH,
1a86c051
JD
111 LM83_REG_W_REMOTE3_HIGH,
112 LM83_REG_W_TCRIT,
1da177e4
LT
113};
114
115/*
116 * Functions declaration
117 */
118
119static int lm83_attach_adapter(struct i2c_adapter *adapter);
120static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
121static int lm83_detach_client(struct i2c_client *client);
122static struct lm83_data *lm83_update_device(struct device *dev);
123
124/*
125 * Driver data (common to all clients)
126 */
127
128static struct i2c_driver lm83_driver = {
129 .owner = THIS_MODULE,
130 .name = "lm83",
131 .id = I2C_DRIVERID_LM83,
132 .flags = I2C_DF_NOTIFY,
133 .attach_adapter = lm83_attach_adapter,
134 .detach_client = lm83_detach_client,
135};
136
137/*
138 * Client data (each client gets its own)
139 */
140
141struct lm83_data {
142 struct i2c_client client;
943b0830 143 struct class_device *class_dev;
1da177e4
LT
144 struct semaphore update_lock;
145 char valid; /* zero until following fields are valid */
146 unsigned long last_updated; /* in jiffies */
147
148 /* registers values */
1a86c051
JD
149 s8 temp[9]; /* 0..3: input 1-4,
150 4..7: high limit 1-4,
151 8 : critical limit */
1da177e4
LT
152 u16 alarms; /* bitvector, combined */
153};
154
155/*
156 * Sysfs stuff
157 */
158
1a86c051
JD
159static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
160 char *buf)
161{
162 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
163 struct lm83_data *data = lm83_update_device(dev);
164 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
1da177e4 165}
1a86c051
JD
166
167static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
168 const char *buf, size_t count)
169{
170 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
171 struct i2c_client *client = to_i2c_client(dev);
172 struct lm83_data *data = i2c_get_clientdata(client);
173 long val = simple_strtol(buf, NULL, 10);
174 int nr = attr->index;
175
176 down(&data->update_lock);
177 data->temp[nr] = TEMP_TO_REG(val);
178 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
179 data->temp[nr]);
180 up(&data->update_lock);
181 return count;
1da177e4 182}
1da177e4 183
1a86c051
JD
184static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
185 char *buf)
1da177e4
LT
186{
187 struct lm83_data *data = lm83_update_device(dev);
188 return sprintf(buf, "%d\n", data->alarms);
189}
190
1a86c051
JD
191static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
192static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
193static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
194static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
195static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
196 set_temp, 4);
197static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
198 set_temp, 5);
199static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
200 set_temp, 6);
201static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
202 set_temp, 7);
203static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
204static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
205static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
206 set_temp, 8);
207static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
1da177e4
LT
208static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
209
210/*
211 * Real code
212 */
213
214static int lm83_attach_adapter(struct i2c_adapter *adapter)
215{
216 if (!(adapter->class & I2C_CLASS_HWMON))
217 return 0;
218 return i2c_detect(adapter, &addr_data, lm83_detect);
219}
220
221/*
222 * The following function does more than just detection. If detection
223 * succeeds, it also registers the new chip.
224 */
225static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
226{
227 struct i2c_client *new_client;
228 struct lm83_data *data;
229 int err = 0;
230 const char *name = "";
231
232 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
233 goto exit;
234
235 if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
236 err = -ENOMEM;
237 goto exit;
238 }
239 memset(data, 0, sizeof(struct lm83_data));
240
241 /* The common I2C client data is placed right after the
242 * LM83-specific data. */
243 new_client = &data->client;
244 i2c_set_clientdata(new_client, data);
245 new_client->addr = address;
246 new_client->adapter = adapter;
247 new_client->driver = &lm83_driver;
248 new_client->flags = 0;
249
250 /* Now we do the detection and identification. A negative kind
251 * means that the driver was loaded with no force parameter
252 * (default), so we must both detect and identify the chip
253 * (actually there is only one possible kind of chip for now, LM83).
254 * A zero kind means that the driver was loaded with the force
255 * parameter, the detection step shall be skipped. A positive kind
256 * means that the driver was loaded with the force parameter and a
257 * given kind of chip is requested, so both the detection and the
258 * identification steps are skipped. */
259
260 /* Default to an LM83 if forced */
261 if (kind == 0)
262 kind = lm83;
263
264 if (kind < 0) { /* detection */
265 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
266 & 0xA8) != 0x00) ||
267 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
268 & 0x48) != 0x00) ||
269 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
270 & 0x41) != 0x00)) {
271 dev_dbg(&adapter->dev,
272 "LM83 detection failed at 0x%02x.\n", address);
273 goto exit_free;
274 }
275 }
276
277 if (kind <= 0) { /* identification */
278 u8 man_id, chip_id;
279
280 man_id = i2c_smbus_read_byte_data(new_client,
281 LM83_REG_R_MAN_ID);
282 chip_id = i2c_smbus_read_byte_data(new_client,
283 LM83_REG_R_CHIP_ID);
284
285 if (man_id == 0x01) { /* National Semiconductor */
286 if (chip_id == 0x03) {
287 kind = lm83;
288 }
289 }
290
291 if (kind <= 0) { /* identification failed */
292 dev_info(&adapter->dev,
293 "Unsupported chip (man_id=0x%02X, "
294 "chip_id=0x%02X).\n", man_id, chip_id);
295 goto exit_free;
296 }
297 }
298
299 if (kind == lm83) {
300 name = "lm83";
301 }
302
303 /* We can fill in the remaining client fields */
304 strlcpy(new_client->name, name, I2C_NAME_SIZE);
305 data->valid = 0;
306 init_MUTEX(&data->update_lock);
307
308 /* Tell the I2C layer a new client has arrived */
309 if ((err = i2c_attach_client(new_client)))
310 goto exit_free;
311
312 /*
313 * Initialize the LM83 chip
314 * (Nothing to do for this one.)
315 */
316
317 /* Register sysfs hooks */
943b0830
MH
318 data->class_dev = hwmon_device_register(&new_client->dev);
319 if (IS_ERR(data->class_dev)) {
320 err = PTR_ERR(data->class_dev);
321 goto exit_detach;
322 }
323
1a86c051
JD
324 device_create_file(&new_client->dev,
325 &sensor_dev_attr_temp1_input.dev_attr);
326 device_create_file(&new_client->dev,
327 &sensor_dev_attr_temp2_input.dev_attr);
328 device_create_file(&new_client->dev,
329 &sensor_dev_attr_temp3_input.dev_attr);
330 device_create_file(&new_client->dev,
331 &sensor_dev_attr_temp4_input.dev_attr);
332 device_create_file(&new_client->dev,
333 &sensor_dev_attr_temp1_max.dev_attr);
334 device_create_file(&new_client->dev,
335 &sensor_dev_attr_temp2_max.dev_attr);
336 device_create_file(&new_client->dev,
337 &sensor_dev_attr_temp3_max.dev_attr);
338 device_create_file(&new_client->dev,
339 &sensor_dev_attr_temp4_max.dev_attr);
340 device_create_file(&new_client->dev,
341 &sensor_dev_attr_temp1_crit.dev_attr);
342 device_create_file(&new_client->dev,
343 &sensor_dev_attr_temp2_crit.dev_attr);
344 device_create_file(&new_client->dev,
345 &sensor_dev_attr_temp3_crit.dev_attr);
346 device_create_file(&new_client->dev,
347 &sensor_dev_attr_temp4_crit.dev_attr);
1da177e4
LT
348 device_create_file(&new_client->dev, &dev_attr_alarms);
349
350 return 0;
351
943b0830
MH
352exit_detach:
353 i2c_detach_client(new_client);
1da177e4
LT
354exit_free:
355 kfree(data);
356exit:
357 return err;
358}
359
360static int lm83_detach_client(struct i2c_client *client)
361{
943b0830 362 struct lm83_data *data = i2c_get_clientdata(client);
1da177e4
LT
363 int err;
364
943b0830
MH
365 hwmon_device_unregister(data->class_dev);
366
1da177e4
LT
367 if ((err = i2c_detach_client(client))) {
368 dev_err(&client->dev,
369 "Client deregistration failed, client not detached.\n");
370 return err;
371 }
372
943b0830 373 kfree(data);
1da177e4
LT
374 return 0;
375}
376
377static struct lm83_data *lm83_update_device(struct device *dev)
378{
379 struct i2c_client *client = to_i2c_client(dev);
380 struct lm83_data *data = i2c_get_clientdata(client);
381
382 down(&data->update_lock);
383
384 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
385 int nr;
386
387 dev_dbg(&client->dev, "Updating lm83 data.\n");
1a86c051
JD
388 for (nr = 0; nr < 9; nr++) {
389 data->temp[nr] =
1da177e4
LT
390 i2c_smbus_read_byte_data(client,
391 LM83_REG_R_TEMP[nr]);
1da177e4 392 }
1da177e4
LT
393 data->alarms =
394 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
395 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
396 << 8);
397
398 data->last_updated = jiffies;
399 data->valid = 1;
400 }
401
402 up(&data->update_lock);
403
404 return data;
405}
406
407static int __init sensors_lm83_init(void)
408{
409 return i2c_add_driver(&lm83_driver);
410}
411
412static void __exit sensors_lm83_exit(void)
413{
414 i2c_del_driver(&lm83_driver);
415}
416
417MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
418MODULE_DESCRIPTION("LM83 driver");
419MODULE_LICENSE("GPL");
420
421module_init(sensors_lm83_init);
422module_exit(sensors_lm83_exit);
This page took 0.144698 seconds and 5 git commands to generate.