diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/i2c/chips/lm83.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/i2c/chips/lm83.c')
-rw-r--r-- | drivers/i2c/chips/lm83.c | 412 |
1 files changed, 412 insertions, 0 deletions
diff --git a/drivers/i2c/chips/lm83.c b/drivers/i2c/chips/lm83.c new file mode 100644 index 000000000000..3dafe60766ad --- /dev/null +++ b/drivers/i2c/chips/lm83.c | |||
@@ -0,0 +1,412 @@ | |||
1 | /* | ||
2 | * lm83.c - Part of lm_sensors, Linux kernel modules for hardware | ||
3 | * monitoring | ||
4 | * Copyright (C) 2003 Jean Delvare <khali@linux-fr.org> | ||
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 | |||
30 | #include <linux/config.h> | ||
31 | #include <linux/module.h> | ||
32 | #include <linux/init.h> | ||
33 | #include <linux/slab.h> | ||
34 | #include <linux/jiffies.h> | ||
35 | #include <linux/i2c.h> | ||
36 | #include <linux/i2c-sensor.h> | ||
37 | |||
38 | /* | ||
39 | * Addresses to scan | ||
40 | * Address is selected using 2 three-level pins, resulting in 9 possible | ||
41 | * addresses. | ||
42 | */ | ||
43 | |||
44 | static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a, | ||
45 | 0x29, 0x2a, 0x2b, | ||
46 | 0x4c, 0x4d, 0x4e, | ||
47 | I2C_CLIENT_END }; | ||
48 | static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; | ||
49 | |||
50 | /* | ||
51 | * Insmod parameters | ||
52 | */ | ||
53 | |||
54 | SENSORS_INSMOD_1(lm83); | ||
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 | ||
84 | * The LM83 uses signed 8-bit values with LSB = 1 degree Celcius. | ||
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 | |||
93 | static 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 | }; | ||
99 | |||
100 | static const u8 LM83_REG_R_HIGH[] = { | ||
101 | LM83_REG_R_LOCAL_HIGH, | ||
102 | LM83_REG_R_REMOTE1_HIGH, | ||
103 | LM83_REG_R_REMOTE2_HIGH, | ||
104 | LM83_REG_R_REMOTE3_HIGH | ||
105 | }; | ||
106 | |||
107 | static const u8 LM83_REG_W_HIGH[] = { | ||
108 | LM83_REG_W_LOCAL_HIGH, | ||
109 | LM83_REG_W_REMOTE1_HIGH, | ||
110 | LM83_REG_W_REMOTE2_HIGH, | ||
111 | LM83_REG_W_REMOTE3_HIGH | ||
112 | }; | ||
113 | |||
114 | /* | ||
115 | * Functions declaration | ||
116 | */ | ||
117 | |||
118 | static int lm83_attach_adapter(struct i2c_adapter *adapter); | ||
119 | static int lm83_detect(struct i2c_adapter *adapter, int address, int kind); | ||
120 | static int lm83_detach_client(struct i2c_client *client); | ||
121 | static struct lm83_data *lm83_update_device(struct device *dev); | ||
122 | |||
123 | /* | ||
124 | * Driver data (common to all clients) | ||
125 | */ | ||
126 | |||
127 | static struct i2c_driver lm83_driver = { | ||
128 | .owner = THIS_MODULE, | ||
129 | .name = "lm83", | ||
130 | .id = I2C_DRIVERID_LM83, | ||
131 | .flags = I2C_DF_NOTIFY, | ||
132 | .attach_adapter = lm83_attach_adapter, | ||
133 | .detach_client = lm83_detach_client, | ||
134 | }; | ||
135 | |||
136 | /* | ||
137 | * Client data (each client gets its own) | ||
138 | */ | ||
139 | |||
140 | struct lm83_data { | ||
141 | struct i2c_client client; | ||
142 | struct semaphore update_lock; | ||
143 | char valid; /* zero until following fields are valid */ | ||
144 | unsigned long last_updated; /* in jiffies */ | ||
145 | |||
146 | /* registers values */ | ||
147 | s8 temp_input[4]; | ||
148 | s8 temp_high[4]; | ||
149 | s8 temp_crit; | ||
150 | u16 alarms; /* bitvector, combined */ | ||
151 | }; | ||
152 | |||
153 | /* | ||
154 | * Sysfs stuff | ||
155 | */ | ||
156 | |||
157 | #define show_temp(suffix, value) \ | ||
158 | static ssize_t show_temp_##suffix(struct device *dev, char *buf) \ | ||
159 | { \ | ||
160 | struct lm83_data *data = lm83_update_device(dev); \ | ||
161 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \ | ||
162 | } | ||
163 | show_temp(input1, temp_input[0]); | ||
164 | show_temp(input2, temp_input[1]); | ||
165 | show_temp(input3, temp_input[2]); | ||
166 | show_temp(input4, temp_input[3]); | ||
167 | show_temp(high1, temp_high[0]); | ||
168 | show_temp(high2, temp_high[1]); | ||
169 | show_temp(high3, temp_high[2]); | ||
170 | show_temp(high4, temp_high[3]); | ||
171 | show_temp(crit, temp_crit); | ||
172 | |||
173 | #define set_temp(suffix, value, reg) \ | ||
174 | static ssize_t set_temp_##suffix(struct device *dev, const char *buf, \ | ||
175 | size_t count) \ | ||
176 | { \ | ||
177 | struct i2c_client *client = to_i2c_client(dev); \ | ||
178 | struct lm83_data *data = i2c_get_clientdata(client); \ | ||
179 | long val = simple_strtol(buf, NULL, 10); \ | ||
180 | \ | ||
181 | down(&data->update_lock); \ | ||
182 | data->value = TEMP_TO_REG(val); \ | ||
183 | i2c_smbus_write_byte_data(client, reg, data->value); \ | ||
184 | up(&data->update_lock); \ | ||
185 | return count; \ | ||
186 | } | ||
187 | set_temp(high1, temp_high[0], LM83_REG_W_LOCAL_HIGH); | ||
188 | set_temp(high2, temp_high[1], LM83_REG_W_REMOTE1_HIGH); | ||
189 | set_temp(high3, temp_high[2], LM83_REG_W_REMOTE2_HIGH); | ||
190 | set_temp(high4, temp_high[3], LM83_REG_W_REMOTE3_HIGH); | ||
191 | set_temp(crit, temp_crit, LM83_REG_W_TCRIT); | ||
192 | |||
193 | static ssize_t show_alarms(struct device *dev, char *buf) | ||
194 | { | ||
195 | struct lm83_data *data = lm83_update_device(dev); | ||
196 | return sprintf(buf, "%d\n", data->alarms); | ||
197 | } | ||
198 | |||
199 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL); | ||
200 | static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL); | ||
201 | static DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input3, NULL); | ||
202 | static DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input4, NULL); | ||
203 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1, | ||
204 | set_temp_high1); | ||
205 | static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2, | ||
206 | set_temp_high2); | ||
207 | static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_high3, | ||
208 | set_temp_high3); | ||
209 | static DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_high4, | ||
210 | set_temp_high4); | ||
211 | static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL); | ||
212 | static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL); | ||
213 | static DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp_crit, | ||
214 | set_temp_crit); | ||
215 | static DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL); | ||
216 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | ||
217 | |||
218 | /* | ||
219 | * Real code | ||
220 | */ | ||
221 | |||
222 | static int lm83_attach_adapter(struct i2c_adapter *adapter) | ||
223 | { | ||
224 | if (!(adapter->class & I2C_CLASS_HWMON)) | ||
225 | return 0; | ||
226 | return i2c_detect(adapter, &addr_data, lm83_detect); | ||
227 | } | ||
228 | |||
229 | /* | ||
230 | * The following function does more than just detection. If detection | ||
231 | * succeeds, it also registers the new chip. | ||
232 | */ | ||
233 | static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) | ||
234 | { | ||
235 | struct i2c_client *new_client; | ||
236 | struct lm83_data *data; | ||
237 | int err = 0; | ||
238 | const char *name = ""; | ||
239 | |||
240 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | ||
241 | goto exit; | ||
242 | |||
243 | if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) { | ||
244 | err = -ENOMEM; | ||
245 | goto exit; | ||
246 | } | ||
247 | memset(data, 0, sizeof(struct lm83_data)); | ||
248 | |||
249 | /* The common I2C client data is placed right after the | ||
250 | * LM83-specific data. */ | ||
251 | new_client = &data->client; | ||
252 | i2c_set_clientdata(new_client, data); | ||
253 | new_client->addr = address; | ||
254 | new_client->adapter = adapter; | ||
255 | new_client->driver = &lm83_driver; | ||
256 | new_client->flags = 0; | ||
257 | |||
258 | /* Now we do the detection and identification. A negative kind | ||
259 | * means that the driver was loaded with no force parameter | ||
260 | * (default), so we must both detect and identify the chip | ||
261 | * (actually there is only one possible kind of chip for now, LM83). | ||
262 | * A zero kind means that the driver was loaded with the force | ||
263 | * parameter, the detection step shall be skipped. A positive kind | ||
264 | * means that the driver was loaded with the force parameter and a | ||
265 | * given kind of chip is requested, so both the detection and the | ||
266 | * identification steps are skipped. */ | ||
267 | |||
268 | /* Default to an LM83 if forced */ | ||
269 | if (kind == 0) | ||
270 | kind = lm83; | ||
271 | |||
272 | if (kind < 0) { /* detection */ | ||
273 | if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) | ||
274 | & 0xA8) != 0x00) || | ||
275 | ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) | ||
276 | & 0x48) != 0x00) || | ||
277 | ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) | ||
278 | & 0x41) != 0x00)) { | ||
279 | dev_dbg(&adapter->dev, | ||
280 | "LM83 detection failed at 0x%02x.\n", address); | ||
281 | goto exit_free; | ||
282 | } | ||
283 | } | ||
284 | |||
285 | if (kind <= 0) { /* identification */ | ||
286 | u8 man_id, chip_id; | ||
287 | |||
288 | man_id = i2c_smbus_read_byte_data(new_client, | ||
289 | LM83_REG_R_MAN_ID); | ||
290 | chip_id = i2c_smbus_read_byte_data(new_client, | ||
291 | LM83_REG_R_CHIP_ID); | ||
292 | |||
293 | if (man_id == 0x01) { /* National Semiconductor */ | ||
294 | if (chip_id == 0x03) { | ||
295 | kind = lm83; | ||
296 | } | ||
297 | } | ||
298 | |||
299 | if (kind <= 0) { /* identification failed */ | ||
300 | dev_info(&adapter->dev, | ||
301 | "Unsupported chip (man_id=0x%02X, " | ||
302 | "chip_id=0x%02X).\n", man_id, chip_id); | ||
303 | goto exit_free; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | if (kind == lm83) { | ||
308 | name = "lm83"; | ||
309 | } | ||
310 | |||
311 | /* We can fill in the remaining client fields */ | ||
312 | strlcpy(new_client->name, name, I2C_NAME_SIZE); | ||
313 | data->valid = 0; | ||
314 | init_MUTEX(&data->update_lock); | ||
315 | |||
316 | /* Tell the I2C layer a new client has arrived */ | ||
317 | if ((err = i2c_attach_client(new_client))) | ||
318 | goto exit_free; | ||
319 | |||
320 | /* | ||
321 | * Initialize the LM83 chip | ||
322 | * (Nothing to do for this one.) | ||
323 | */ | ||
324 | |||
325 | /* Register sysfs hooks */ | ||
326 | device_create_file(&new_client->dev, &dev_attr_temp1_input); | ||
327 | device_create_file(&new_client->dev, &dev_attr_temp2_input); | ||
328 | device_create_file(&new_client->dev, &dev_attr_temp3_input); | ||
329 | device_create_file(&new_client->dev, &dev_attr_temp4_input); | ||
330 | device_create_file(&new_client->dev, &dev_attr_temp1_max); | ||
331 | device_create_file(&new_client->dev, &dev_attr_temp2_max); | ||
332 | device_create_file(&new_client->dev, &dev_attr_temp3_max); | ||
333 | device_create_file(&new_client->dev, &dev_attr_temp4_max); | ||
334 | device_create_file(&new_client->dev, &dev_attr_temp1_crit); | ||
335 | device_create_file(&new_client->dev, &dev_attr_temp2_crit); | ||
336 | device_create_file(&new_client->dev, &dev_attr_temp3_crit); | ||
337 | device_create_file(&new_client->dev, &dev_attr_temp4_crit); | ||
338 | device_create_file(&new_client->dev, &dev_attr_alarms); | ||
339 | |||
340 | return 0; | ||
341 | |||
342 | exit_free: | ||
343 | kfree(data); | ||
344 | exit: | ||
345 | return err; | ||
346 | } | ||
347 | |||
348 | static int lm83_detach_client(struct i2c_client *client) | ||
349 | { | ||
350 | int err; | ||
351 | |||
352 | if ((err = i2c_detach_client(client))) { | ||
353 | dev_err(&client->dev, | ||
354 | "Client deregistration failed, client not detached.\n"); | ||
355 | return err; | ||
356 | } | ||
357 | |||
358 | kfree(i2c_get_clientdata(client)); | ||
359 | return 0; | ||
360 | } | ||
361 | |||
362 | static struct lm83_data *lm83_update_device(struct device *dev) | ||
363 | { | ||
364 | struct i2c_client *client = to_i2c_client(dev); | ||
365 | struct lm83_data *data = i2c_get_clientdata(client); | ||
366 | |||
367 | down(&data->update_lock); | ||
368 | |||
369 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { | ||
370 | int nr; | ||
371 | |||
372 | dev_dbg(&client->dev, "Updating lm83 data.\n"); | ||
373 | for (nr = 0; nr < 4 ; nr++) { | ||
374 | data->temp_input[nr] = | ||
375 | i2c_smbus_read_byte_data(client, | ||
376 | LM83_REG_R_TEMP[nr]); | ||
377 | data->temp_high[nr] = | ||
378 | i2c_smbus_read_byte_data(client, | ||
379 | LM83_REG_R_HIGH[nr]); | ||
380 | } | ||
381 | data->temp_crit = | ||
382 | i2c_smbus_read_byte_data(client, LM83_REG_R_TCRIT); | ||
383 | data->alarms = | ||
384 | i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) | ||
385 | + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) | ||
386 | << 8); | ||
387 | |||
388 | data->last_updated = jiffies; | ||
389 | data->valid = 1; | ||
390 | } | ||
391 | |||
392 | up(&data->update_lock); | ||
393 | |||
394 | return data; | ||
395 | } | ||
396 | |||
397 | static int __init sensors_lm83_init(void) | ||
398 | { | ||
399 | return i2c_add_driver(&lm83_driver); | ||
400 | } | ||
401 | |||
402 | static void __exit sensors_lm83_exit(void) | ||
403 | { | ||
404 | i2c_del_driver(&lm83_driver); | ||
405 | } | ||
406 | |||
407 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | ||
408 | MODULE_DESCRIPTION("LM83 driver"); | ||
409 | MODULE_LICENSE("GPL"); | ||
410 | |||
411 | module_init(sensors_lm83_init); | ||
412 | module_exit(sensors_lm83_exit); | ||