Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
420 | giacomo | 1 | /* |
2 | adm1021.c - Part of lm_sensors, Linux kernel modules for hardware |
||
3 | monitoring |
||
4 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and |
||
5 | Philip Edelbrock <phil@netroedge.com> |
||
6 | |||
7 | This program is free software; you can redistribute it and/or modify |
||
8 | it under the terms of the GNU General Public License as published by |
||
9 | the Free Software Foundation; either version 2 of the License, or |
||
10 | (at your option) any later version. |
||
11 | |||
12 | This program is distributed in the hope that it will be useful, |
||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | GNU General Public License for more details. |
||
16 | |||
17 | You should have received a copy of the GNU General Public License |
||
18 | along with this program; if not, write to the Free Software |
||
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
20 | */ |
||
21 | |||
22 | #include <linux/module.h> |
||
23 | #include <linux/init.h> |
||
24 | #include <linux/slab.h> |
||
25 | #include <linux/i2c.h> |
||
26 | #include <linux/i2c-sensor.h> |
||
27 | |||
28 | |||
29 | /* Registers */ |
||
30 | #define ADM1021_SYSCTL_TEMP 1200 |
||
31 | #define ADM1021_SYSCTL_REMOTE_TEMP 1201 |
||
32 | #define ADM1021_SYSCTL_DIE_CODE 1202 |
||
33 | #define ADM1021_SYSCTL_ALARMS 1203 |
||
34 | |||
35 | #define ADM1021_ALARM_TEMP_HIGH 0x40 |
||
36 | #define ADM1021_ALARM_TEMP_LOW 0x20 |
||
37 | #define ADM1021_ALARM_RTEMP_HIGH 0x10 |
||
38 | #define ADM1021_ALARM_RTEMP_LOW 0x08 |
||
39 | #define ADM1021_ALARM_RTEMP_NA 0x04 |
||
40 | |||
41 | /* Addresses to scan */ |
||
42 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; |
||
43 | static unsigned short normal_i2c_range[] = { 0x18, 0x1a, 0x29, 0x2b, |
||
44 | 0x4c, 0x4e, I2C_CLIENT_END |
||
45 | }; |
||
46 | static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; |
||
47 | static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END }; |
||
48 | |||
49 | /* Insmod parameters */ |
||
50 | SENSORS_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066); |
||
51 | |||
52 | /* adm1021 constants specified below */ |
||
53 | |||
54 | /* The adm1021 registers */ |
||
55 | /* Read-only */ |
||
56 | #define ADM1021_REG_TEMP 0x00 |
||
57 | #define ADM1021_REG_REMOTE_TEMP 0x01 |
||
58 | #define ADM1021_REG_STATUS 0x02 |
||
59 | #define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/ |
||
60 | #define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */ |
||
61 | #define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */ |
||
62 | /* These use different addresses for reading/writing */ |
||
63 | #define ADM1021_REG_CONFIG_R 0x03 |
||
64 | #define ADM1021_REG_CONFIG_W 0x09 |
||
65 | #define ADM1021_REG_CONV_RATE_R 0x04 |
||
66 | #define ADM1021_REG_CONV_RATE_W 0x0A |
||
67 | /* These are for the ADM1023's additional precision on the remote temp sensor */ |
||
68 | #define ADM1021_REG_REM_TEMP_PREC 0x010 |
||
69 | #define ADM1021_REG_REM_OFFSET 0x011 |
||
70 | #define ADM1021_REG_REM_OFFSET_PREC 0x012 |
||
71 | #define ADM1021_REG_REM_TOS_PREC 0x013 |
||
72 | #define ADM1021_REG_REM_THYST_PREC 0x014 |
||
73 | /* limits */ |
||
74 | #define ADM1021_REG_TOS_R 0x05 |
||
75 | #define ADM1021_REG_TOS_W 0x0B |
||
76 | #define ADM1021_REG_REMOTE_TOS_R 0x07 |
||
77 | #define ADM1021_REG_REMOTE_TOS_W 0x0D |
||
78 | #define ADM1021_REG_THYST_R 0x06 |
||
79 | #define ADM1021_REG_THYST_W 0x0C |
||
80 | #define ADM1021_REG_REMOTE_THYST_R 0x08 |
||
81 | #define ADM1021_REG_REMOTE_THYST_W 0x0E |
||
82 | /* write-only */ |
||
83 | #define ADM1021_REG_ONESHOT 0x0F |
||
84 | |||
85 | |||
86 | /* Conversions. Rounding and limit checking is only done on the TO_REG |
||
87 | variants. Note that you should be a bit careful with which arguments |
||
88 | these macros are called: arguments may be evaluated more than once. |
||
89 | Fixing this is just not worth it. */ |
||
90 | /* Conversions note: 1021 uses normal integer signed-byte format*/ |
||
91 | #define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000) |
||
92 | #define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255)) |
||
93 | |||
94 | /* Initial values */ |
||
95 | |||
96 | /* Note: Even though I left the low and high limits named os and hyst, |
||
97 | they don't quite work like a thermostat the way the LM75 does. I.e., |
||
98 | a lower temp than THYST actually triggers an alarm instead of |
||
99 | clearing it. Weird, ey? --Phil */ |
||
100 | #define adm1021_INIT_TOS 60 |
||
101 | #define adm1021_INIT_THYST 20 |
||
102 | #define adm1021_INIT_REMOTE_TOS 60 |
||
103 | #define adm1021_INIT_REMOTE_THYST 20 |
||
104 | |||
105 | /* Each client has this additional data */ |
||
106 | struct adm1021_data { |
||
107 | enum chips type; |
||
108 | |||
109 | struct semaphore update_lock; |
||
110 | char valid; /* !=0 if following fields are valid */ |
||
111 | unsigned long last_updated; /* In jiffies */ |
||
112 | |||
113 | u8 temp_max; /* Register values */ |
||
114 | u8 temp_hyst; |
||
115 | u8 temp_input; |
||
116 | u8 remote_temp_max; |
||
117 | u8 remote_temp_hyst; |
||
118 | u8 remote_temp_input; |
||
119 | u8 alarms; |
||
120 | /* special values for ADM1021 only */ |
||
121 | u8 die_code; |
||
122 | /* Special values for ADM1023 only */ |
||
123 | u8 remote_temp_prec; |
||
124 | u8 remote_temp_os_prec; |
||
125 | u8 remote_temp_hyst_prec; |
||
126 | u8 remote_temp_offset; |
||
127 | u8 remote_temp_offset_prec; |
||
128 | }; |
||
129 | |||
130 | static int adm1021_attach_adapter(struct i2c_adapter *adapter); |
||
131 | static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind); |
||
132 | static void adm1021_init_client(struct i2c_client *client); |
||
133 | static int adm1021_detach_client(struct i2c_client *client); |
||
134 | static int adm1021_read_value(struct i2c_client *client, u8 reg); |
||
135 | static int adm1021_write_value(struct i2c_client *client, u8 reg, |
||
136 | u16 value); |
||
137 | static void adm1021_update_client(struct i2c_client *client); |
||
138 | |||
139 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ |
||
140 | static int read_only = 0; |
||
141 | |||
142 | |||
143 | /* This is the driver that will be inserted */ |
||
144 | static struct i2c_driver adm1021_driver = { |
||
145 | .owner = THIS_MODULE, |
||
146 | .name = "ADM1021-MAX1617", |
||
147 | .id = I2C_DRIVERID_ADM1021, |
||
148 | .flags = I2C_DF_NOTIFY, |
||
149 | .attach_adapter = adm1021_attach_adapter, |
||
150 | .detach_client = adm1021_detach_client, |
||
151 | }; |
||
152 | |||
153 | /* I choose here for semi-static allocation. Complete dynamic |
||
154 | allocation could also be used; the code needed for this would probably |
||
155 | take more memory than the datastructure takes now. */ |
||
156 | static int adm1021_id = 0; |
||
157 | |||
158 | #define show(value) \ |
||
159 | static ssize_t show_##value(struct device *dev, char *buf) \ |
||
160 | { \ |
||
161 | struct i2c_client *client = to_i2c_client(dev); \ |
||
162 | struct adm1021_data *data = i2c_get_clientdata(client); \ |
||
163 | int temp; \ |
||
164 | \ |
||
165 | adm1021_update_client(client); \ |
||
166 | temp = TEMP_FROM_REG(data->value); \ |
||
167 | return sprintf(buf, "%d\n", temp); \ |
||
168 | } |
||
169 | show(temp_max); |
||
170 | show(temp_hyst); |
||
171 | show(temp_input); |
||
172 | show(remote_temp_max); |
||
173 | show(remote_temp_hyst); |
||
174 | show(remote_temp_input); |
||
175 | |||
176 | #define show2(value) \ |
||
177 | static ssize_t show_##value(struct device *dev, char *buf) \ |
||
178 | { \ |
||
179 | struct i2c_client *client = to_i2c_client(dev); \ |
||
180 | struct adm1021_data *data = i2c_get_clientdata(client); \ |
||
181 | \ |
||
182 | adm1021_update_client(client); \ |
||
183 | return sprintf(buf, "%d\n", data->value); \ |
||
184 | } |
||
185 | show2(alarms); |
||
186 | show2(die_code); |
||
187 | |||
188 | #define set(value, reg) \ |
||
189 | static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \ |
||
190 | { \ |
||
191 | struct i2c_client *client = to_i2c_client(dev); \ |
||
192 | struct adm1021_data *data = i2c_get_clientdata(client); \ |
||
193 | int temp = simple_strtoul(buf, NULL, 10); \ |
||
194 | \ |
||
195 | data->value = TEMP_TO_REG(temp); \ |
||
196 | adm1021_write_value(client, reg, data->value); \ |
||
197 | return count; \ |
||
198 | } |
||
199 | set(temp_max, ADM1021_REG_TOS_W); |
||
200 | set(temp_hyst, ADM1021_REG_THYST_W); |
||
201 | set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W); |
||
202 | set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W); |
||
203 | |||
204 | static DEVICE_ATTR(temp_max1, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max); |
||
205 | static DEVICE_ATTR(temp_min1, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst); |
||
206 | static DEVICE_ATTR(temp_input1, S_IRUGO, show_temp_input, NULL); |
||
207 | static DEVICE_ATTR(temp_max2, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max); |
||
208 | static DEVICE_ATTR(temp_min2, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst); |
||
209 | static DEVICE_ATTR(temp_input2, S_IRUGO, show_remote_temp_input, NULL); |
||
210 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
||
211 | static DEVICE_ATTR(die_code, S_IRUGO, show_die_code, NULL); |
||
212 | |||
213 | |||
214 | static int adm1021_attach_adapter(struct i2c_adapter *adapter) |
||
215 | { |
||
216 | if (!(adapter->class & I2C_ADAP_CLASS_SMBUS)) |
||
217 | return 0; |
||
218 | return i2c_detect(adapter, &addr_data, adm1021_detect); |
||
219 | } |
||
220 | |||
221 | static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind) |
||
222 | { |
||
223 | int i; |
||
224 | struct i2c_client *new_client; |
||
225 | struct adm1021_data *data; |
||
226 | int err = 0; |
||
227 | const char *type_name = ""; |
||
228 | |||
229 | /* Make sure we aren't probing the ISA bus!! This is just a safety check |
||
230 | at this moment; i2c_detect really won't call us. */ |
||
231 | #ifdef DEBUG |
||
232 | if (i2c_is_isa_adapter(adapter)) { |
||
233 | dev_dbg(&adapter->dev, "adm1021_detect called for an ISA bus adapter?!?\n"); |
||
234 | return 0; |
||
235 | } |
||
236 | #endif |
||
237 | |||
238 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
||
239 | goto error0; |
||
240 | |||
241 | /* OK. For now, we presume we have a valid client. We now create the |
||
242 | client structure, even though we cannot fill it completely yet. |
||
243 | But it allows us to access adm1021_{read,write}_value. */ |
||
244 | |||
245 | if (!(new_client = kmalloc(sizeof(struct i2c_client) + |
||
246 | sizeof(struct adm1021_data), |
||
247 | GFP_KERNEL))) { |
||
248 | err = -ENOMEM; |
||
249 | goto error0; |
||
250 | } |
||
251 | memset(new_client, 0x00, sizeof(struct i2c_client) + |
||
252 | sizeof(struct adm1021_data)); |
||
253 | |||
254 | data = (struct adm1021_data *) (new_client + 1); |
||
255 | i2c_set_clientdata(new_client, data); |
||
256 | new_client->addr = address; |
||
257 | new_client->adapter = adapter; |
||
258 | new_client->driver = &adm1021_driver; |
||
259 | new_client->flags = 0; |
||
260 | |||
261 | /* Now, we do the remaining detection. */ |
||
262 | if (kind < 0) { |
||
263 | if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00) |
||
264 | goto error1; |
||
265 | } |
||
266 | |||
267 | /* Determine the chip type. */ |
||
268 | if (kind <= 0) { |
||
269 | i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID); |
||
270 | if (i == 0x41) |
||
271 | if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030) |
||
272 | kind = adm1023; |
||
273 | else |
||
274 | kind = adm1021; |
||
275 | else if (i == 0x49) |
||
276 | kind = thmc10; |
||
277 | else if (i == 0x23) |
||
278 | kind = gl523sm; |
||
279 | else if ((i == 0x4d) && |
||
280 | (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01)) |
||
281 | kind = max1617a; |
||
282 | /* LM84 Mfr ID in a different place */ |
||
283 | else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00) |
||
284 | kind = lm84; |
||
285 | else if (i == 0x54) |
||
286 | kind = mc1066; |
||
287 | else |
||
288 | kind = max1617; |
||
289 | } |
||
290 | |||
291 | if (kind == max1617) { |
||
292 | type_name = "max1617"; |
||
293 | } else if (kind == max1617a) { |
||
294 | type_name = "max1617a"; |
||
295 | } else if (kind == adm1021) { |
||
296 | type_name = "adm1021"; |
||
297 | } else if (kind == adm1023) { |
||
298 | type_name = "adm1023"; |
||
299 | } else if (kind == thmc10) { |
||
300 | type_name = "thmc10"; |
||
301 | } else if (kind == lm84) { |
||
302 | type_name = "lm84"; |
||
303 | } else if (kind == gl523sm) { |
||
304 | type_name = "gl523sm"; |
||
305 | } else if (kind == mc1066) { |
||
306 | type_name = "mc1066"; |
||
307 | } else { |
||
308 | dev_err(&adapter->dev, "Internal error: unknown kind (%d)?!?", |
||
309 | kind); |
||
310 | goto error1; |
||
311 | } |
||
312 | |||
313 | /* Fill in the remaining client fields and put it into the global list */ |
||
314 | strlcpy(new_client->name, type_name, I2C_NAME_SIZE); |
||
315 | data->type = kind; |
||
316 | |||
317 | new_client->id = adm1021_id++; |
||
318 | data->valid = 0; |
||
319 | init_MUTEX(&data->update_lock); |
||
320 | |||
321 | /* Tell the I2C layer a new client has arrived */ |
||
322 | if ((err = i2c_attach_client(new_client))) |
||
323 | goto error3; |
||
324 | |||
325 | /* Initialize the ADM1021 chip */ |
||
326 | adm1021_init_client(new_client); |
||
327 | |||
328 | /* Register sysfs hooks */ |
||
329 | device_create_file(&new_client->dev, &dev_attr_temp_max1); |
||
330 | device_create_file(&new_client->dev, &dev_attr_temp_min1); |
||
331 | device_create_file(&new_client->dev, &dev_attr_temp_input1); |
||
332 | device_create_file(&new_client->dev, &dev_attr_temp_max2); |
||
333 | device_create_file(&new_client->dev, &dev_attr_temp_min2); |
||
334 | device_create_file(&new_client->dev, &dev_attr_temp_input2); |
||
335 | device_create_file(&new_client->dev, &dev_attr_alarms); |
||
336 | if (data->type == adm1021) |
||
337 | device_create_file(&new_client->dev, &dev_attr_die_code); |
||
338 | |||
339 | return 0; |
||
340 | |||
341 | error3: |
||
342 | error1: |
||
343 | kfree(new_client); |
||
344 | error0: |
||
345 | return err; |
||
346 | } |
||
347 | |||
348 | static void adm1021_init_client(struct i2c_client *client) |
||
349 | { |
||
350 | /* Initialize the adm1021 chip */ |
||
351 | adm1021_write_value(client, ADM1021_REG_TOS_W, |
||
352 | adm1021_INIT_TOS); |
||
353 | adm1021_write_value(client, ADM1021_REG_THYST_W, |
||
354 | adm1021_INIT_THYST); |
||
355 | adm1021_write_value(client, ADM1021_REG_REMOTE_TOS_W, |
||
356 | adm1021_INIT_REMOTE_TOS); |
||
357 | adm1021_write_value(client, ADM1021_REG_REMOTE_THYST_W, |
||
358 | adm1021_INIT_REMOTE_THYST); |
||
359 | /* Enable ADC and disable suspend mode */ |
||
360 | adm1021_write_value(client, ADM1021_REG_CONFIG_W, 0); |
||
361 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
||
362 | adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04); |
||
363 | } |
||
364 | |||
365 | static int adm1021_detach_client(struct i2c_client *client) |
||
366 | { |
||
367 | int err; |
||
368 | |||
369 | if ((err = i2c_detach_client(client))) { |
||
370 | dev_err(&client->dev, "Client deregistration failed, client not detached.\n"); |
||
371 | return err; |
||
372 | } |
||
373 | |||
374 | kfree(client); |
||
375 | return 0; |
||
376 | } |
||
377 | |||
378 | /* All registers are byte-sized */ |
||
379 | static int adm1021_read_value(struct i2c_client *client, u8 reg) |
||
380 | { |
||
381 | return i2c_smbus_read_byte_data(client, reg); |
||
382 | } |
||
383 | |||
384 | static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value) |
||
385 | { |
||
386 | if (!read_only) |
||
387 | return i2c_smbus_write_byte_data(client, reg, value); |
||
388 | return 0; |
||
389 | } |
||
390 | |||
391 | static void adm1021_update_client(struct i2c_client *client) |
||
392 | { |
||
393 | struct adm1021_data *data = i2c_get_clientdata(client); |
||
394 | |||
395 | down(&data->update_lock); |
||
396 | |||
397 | if ((jiffies - data->last_updated > HZ + HZ / 2) || |
||
398 | (jiffies < data->last_updated) || !data->valid) { |
||
399 | dev_dbg(&client->dev, "Starting adm1021 update\n"); |
||
400 | |||
401 | data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP); |
||
402 | data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R); |
||
403 | data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R); |
||
404 | data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP); |
||
405 | data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R); |
||
406 | data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R); |
||
407 | data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0xec; |
||
408 | if (data->type == adm1021) |
||
409 | data->die_code = adm1021_read_value(client, ADM1021_REG_DIE_CODE); |
||
410 | if (data->type == adm1023) { |
||
411 | data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC); |
||
412 | data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC); |
||
413 | data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC); |
||
414 | data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET); |
||
415 | data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC); |
||
416 | } |
||
417 | data->last_updated = jiffies; |
||
418 | data->valid = 1; |
||
419 | } |
||
420 | |||
421 | up(&data->update_lock); |
||
422 | } |
||
423 | |||
424 | static int __init sensors_adm1021_init(void) |
||
425 | { |
||
426 | return i2c_add_driver(&adm1021_driver); |
||
427 | } |
||
428 | |||
429 | static void __exit sensors_adm1021_exit(void) |
||
430 | { |
||
431 | i2c_del_driver(&adm1021_driver); |
||
432 | } |
||
433 | |||
434 | MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and " |
||
435 | "Philip Edelbrock <phil@netroedge.com>"); |
||
436 | MODULE_DESCRIPTION("adm1021 driver"); |
||
437 | MODULE_LICENSE("GPL"); |
||
438 | |||
439 | MODULE_PARM(read_only, "i"); |
||
440 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); |
||
441 | |||
442 | module_init(sensors_adm1021_init) |
||
443 | module_exit(sensors_adm1021_exit) |