]> err.no Git - linux-2.6/blob - drivers/hwmon/thmc50.c
Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / hwmon / thmc50.c
1 /*
2     thmc50.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl>
5     Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and
6     Philip Edelbrock <phil@netroedge.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/err.h>
30 #include <linux/mutex.h>
31
32 MODULE_LICENSE("GPL");
33
34 /* Addresses to scan */
35 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
36
37 /* Insmod parameters */
38 I2C_CLIENT_INSMOD_2(thmc50, adm1022);
39 I2C_CLIENT_MODULE_PARM(adm1022_temp3, "List of adapter,address pairs "
40                         "to enable 3rd temperature (ADM1022 only)");
41
42 /* Many THMC50 constants specified below */
43
44 /* The THMC50 registers */
45 #define THMC50_REG_CONF                         0x40
46 #define THMC50_REG_COMPANY_ID                   0x3E
47 #define THMC50_REG_DIE_CODE                     0x3F
48 #define THMC50_REG_ANALOG_OUT                   0x19
49 /*
50  * The mirror status register cannot be used as
51  * reading it does not clear alarms.
52  */
53 #define THMC50_REG_INTR                         0x41
54
55 static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
56 static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
57 static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
58
59 #define THMC50_REG_CONF_nFANOFF                 0x20
60
61 /* Each client has this additional data */
62 struct thmc50_data {
63         struct device *hwmon_dev;
64
65         struct mutex update_lock;
66         enum chips type;
67         unsigned long last_updated;     /* In jiffies */
68         char has_temp3;         /* !=0 if it is ADM1022 in temp3 mode */
69         char valid;             /* !=0 if following fields are valid */
70
71         /* Register values */
72         s8 temp_input[3];
73         s8 temp_max[3];
74         s8 temp_min[3];
75         u8 analog_out;
76         u8 alarms;
77 };
78
79 static int thmc50_detect(struct i2c_client *client, int kind,
80                          struct i2c_board_info *info);
81 static int thmc50_probe(struct i2c_client *client,
82                         const struct i2c_device_id *id);
83 static int thmc50_remove(struct i2c_client *client);
84 static void thmc50_init_client(struct i2c_client *client);
85 static struct thmc50_data *thmc50_update_device(struct device *dev);
86
87 static const struct i2c_device_id thmc50_id[] = {
88         { "adm1022", adm1022 },
89         { "thmc50", thmc50 },
90         { }
91 };
92 MODULE_DEVICE_TABLE(i2c, thmc50_id);
93
94 static struct i2c_driver thmc50_driver = {
95         .class = I2C_CLASS_HWMON,
96         .driver = {
97                 .name = "thmc50",
98         },
99         .probe = thmc50_probe,
100         .remove = thmc50_remove,
101         .id_table = thmc50_id,
102         .detect = thmc50_detect,
103         .address_data = &addr_data,
104 };
105
106 static ssize_t show_analog_out(struct device *dev,
107                                struct device_attribute *attr, char *buf)
108 {
109         struct thmc50_data *data = thmc50_update_device(dev);
110         return sprintf(buf, "%d\n", data->analog_out);
111 }
112
113 static ssize_t set_analog_out(struct device *dev,
114                               struct device_attribute *attr,
115                               const char *buf, size_t count)
116 {
117         struct i2c_client *client = to_i2c_client(dev);
118         struct thmc50_data *data = i2c_get_clientdata(client);
119         int tmp = simple_strtoul(buf, NULL, 10);
120         int config;
121
122         mutex_lock(&data->update_lock);
123         data->analog_out = SENSORS_LIMIT(tmp, 0, 255);
124         i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
125                                   data->analog_out);
126
127         config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
128         if (data->analog_out == 0)
129                 config &= ~THMC50_REG_CONF_nFANOFF;
130         else
131                 config |= THMC50_REG_CONF_nFANOFF;
132         i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
133
134         mutex_unlock(&data->update_lock);
135         return count;
136 }
137
138 /* There is only one PWM mode = DC */
139 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
140                              char *buf)
141 {
142         return sprintf(buf, "0\n");
143 }
144
145 /* Temperatures */
146 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
147                          char *buf)
148 {
149         int nr = to_sensor_dev_attr(attr)->index;
150         struct thmc50_data *data = thmc50_update_device(dev);
151         return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
152 }
153
154 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
155                              char *buf)
156 {
157         int nr = to_sensor_dev_attr(attr)->index;
158         struct thmc50_data *data = thmc50_update_device(dev);
159         return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
160 }
161
162 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
163                             const char *buf, size_t count)
164 {
165         int nr = to_sensor_dev_attr(attr)->index;
166         struct i2c_client *client = to_i2c_client(dev);
167         struct thmc50_data *data = i2c_get_clientdata(client);
168         int val = simple_strtol(buf, NULL, 10);
169
170         mutex_lock(&data->update_lock);
171         data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
172         i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
173                                   data->temp_min[nr]);
174         mutex_unlock(&data->update_lock);
175         return count;
176 }
177
178 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
179                              char *buf)
180 {
181         int nr = to_sensor_dev_attr(attr)->index;
182         struct thmc50_data *data = thmc50_update_device(dev);
183         return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
184 }
185
186 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
187                             const char *buf, size_t count)
188 {
189         int nr = to_sensor_dev_attr(attr)->index;
190         struct i2c_client *client = to_i2c_client(dev);
191         struct thmc50_data *data = i2c_get_clientdata(client);
192         int val = simple_strtol(buf, NULL, 10);
193
194         mutex_lock(&data->update_lock);
195         data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
196         i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
197                                   data->temp_max[nr]);
198         mutex_unlock(&data->update_lock);
199         return count;
200 }
201
202 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
203                           char *buf)
204 {
205         int index = to_sensor_dev_attr(attr)->index;
206         struct thmc50_data *data = thmc50_update_device(dev);
207
208         return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
209 }
210
211 #define temp_reg(offset)                                                \
212 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp,     \
213                         NULL, offset - 1);                              \
214 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,        \
215                         show_temp_min, set_temp_min, offset - 1);       \
216 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,        \
217                         show_temp_max, set_temp_max, offset - 1);
218
219 temp_reg(1);
220 temp_reg(2);
221 temp_reg(3);
222
223 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
224 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
225 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
226 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
227 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
228
229 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
230                           set_analog_out, 0);
231 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
232
233 static struct attribute *thmc50_attributes[] = {
234         &sensor_dev_attr_temp1_max.dev_attr.attr,
235         &sensor_dev_attr_temp1_min.dev_attr.attr,
236         &sensor_dev_attr_temp1_input.dev_attr.attr,
237         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
238         &sensor_dev_attr_temp2_max.dev_attr.attr,
239         &sensor_dev_attr_temp2_min.dev_attr.attr,
240         &sensor_dev_attr_temp2_input.dev_attr.attr,
241         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
242         &sensor_dev_attr_temp2_fault.dev_attr.attr,
243         &sensor_dev_attr_pwm1.dev_attr.attr,
244         &sensor_dev_attr_pwm1_mode.dev_attr.attr,
245         NULL
246 };
247
248 static const struct attribute_group thmc50_group = {
249         .attrs = thmc50_attributes,
250 };
251
252 /* for ADM1022 3rd temperature mode */
253 static struct attribute *temp3_attributes[] = {
254         &sensor_dev_attr_temp3_max.dev_attr.attr,
255         &sensor_dev_attr_temp3_min.dev_attr.attr,
256         &sensor_dev_attr_temp3_input.dev_attr.attr,
257         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
258         &sensor_dev_attr_temp3_fault.dev_attr.attr,
259         NULL
260 };
261
262 static const struct attribute_group temp3_group = {
263         .attrs = temp3_attributes,
264 };
265
266 /* Return 0 if detection is successful, -ENODEV otherwise */
267 static int thmc50_detect(struct i2c_client *client, int kind,
268                          struct i2c_board_info *info)
269 {
270         unsigned company;
271         unsigned revision;
272         unsigned config;
273         struct i2c_adapter *adapter = client->adapter;
274         int err = 0;
275         const char *type_name;
276
277         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
278                 pr_debug("thmc50: detect failed, "
279                          "smbus byte data not supported!\n");
280                 return -ENODEV;
281         }
282
283         pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
284                  client->addr, i2c_adapter_id(client->adapter));
285
286         /* Now, we do the remaining detection. */
287         company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
288         revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
289         config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
290
291         if (kind == 0)
292                 kind = thmc50;
293         else if (kind < 0) {
294                 err = -ENODEV;
295                 if (revision >= 0xc0 && ((config & 0x10) == 0)) {
296                         if (company == 0x49) {
297                                 kind = thmc50;
298                                 err = 0;
299                         } else if (company == 0x41) {
300                                 kind = adm1022;
301                                 err = 0;
302                         }
303                 }
304         }
305         if (err == -ENODEV) {
306                 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
307                 return err;
308         }
309
310         if (kind == adm1022) {
311                 int id = i2c_adapter_id(client->adapter);
312                 int i;
313
314                 type_name = "adm1022";
315                 for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
316                         if (adm1022_temp3[i] == id &&
317                             adm1022_temp3[i + 1] == client->addr) {
318                                 /* enable 2nd remote temp */
319                                 config |= (1 << 7);
320                                 i2c_smbus_write_byte_data(client,
321                                                           THMC50_REG_CONF,
322                                                           config);
323                                 break;
324                         }
325         } else {
326                 type_name = "thmc50";
327         }
328         pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
329                  type_name, (revision >> 4) - 0xc, revision & 0xf);
330
331         strlcpy(info->type, type_name, I2C_NAME_SIZE);
332
333         return 0;
334 }
335
336 static int thmc50_probe(struct i2c_client *client,
337                         const struct i2c_device_id *id)
338 {
339         struct thmc50_data *data;
340         int err;
341
342         data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL);
343         if (!data) {
344                 pr_debug("thmc50: detect failed, kzalloc failed!\n");
345                 err = -ENOMEM;
346                 goto exit;
347         }
348
349         i2c_set_clientdata(client, data);
350         data->type = id->driver_data;
351         mutex_init(&data->update_lock);
352
353         thmc50_init_client(client);
354
355         /* Register sysfs hooks */
356         if ((err = sysfs_create_group(&client->dev.kobj, &thmc50_group)))
357                 goto exit_free;
358
359         /* Register ADM1022 sysfs hooks */
360         if (data->has_temp3)
361                 if ((err = sysfs_create_group(&client->dev.kobj,
362                                               &temp3_group)))
363                         goto exit_remove_sysfs_thmc50;
364
365         /* Register a new directory entry with module sensors */
366         data->hwmon_dev = hwmon_device_register(&client->dev);
367         if (IS_ERR(data->hwmon_dev)) {
368                 err = PTR_ERR(data->hwmon_dev);
369                 goto exit_remove_sysfs;
370         }
371
372         return 0;
373
374 exit_remove_sysfs:
375         if (data->has_temp3)
376                 sysfs_remove_group(&client->dev.kobj, &temp3_group);
377 exit_remove_sysfs_thmc50:
378         sysfs_remove_group(&client->dev.kobj, &thmc50_group);
379 exit_free:
380         kfree(data);
381 exit:
382         return err;
383 }
384
385 static int thmc50_remove(struct i2c_client *client)
386 {
387         struct thmc50_data *data = i2c_get_clientdata(client);
388
389         hwmon_device_unregister(data->hwmon_dev);
390         sysfs_remove_group(&client->dev.kobj, &thmc50_group);
391         if (data->has_temp3)
392                 sysfs_remove_group(&client->dev.kobj, &temp3_group);
393
394         kfree(data);
395
396         return 0;
397 }
398
399 static void thmc50_init_client(struct i2c_client *client)
400 {
401         struct thmc50_data *data = i2c_get_clientdata(client);
402         int config;
403
404         data->analog_out = i2c_smbus_read_byte_data(client,
405                                                     THMC50_REG_ANALOG_OUT);
406         /* set up to at least 1 */
407         if (data->analog_out == 0) {
408                 data->analog_out = 1;
409                 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
410                                           data->analog_out);
411         }
412         config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
413         config |= 0x1;  /* start the chip if it is in standby mode */
414         if (data->type == adm1022 && (config & (1 << 7)))
415                 data->has_temp3 = 1;
416         i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
417 }
418
419 static struct thmc50_data *thmc50_update_device(struct device *dev)
420 {
421         struct i2c_client *client = to_i2c_client(dev);
422         struct thmc50_data *data = i2c_get_clientdata(client);
423         int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
424
425         mutex_lock(&data->update_lock);
426
427         if (time_after(jiffies, data->last_updated + timeout)
428             || !data->valid) {
429
430                 int temps = data->has_temp3 ? 3 : 2;
431                 int i;
432                 for (i = 0; i < temps; i++) {
433                         data->temp_input[i] = i2c_smbus_read_byte_data(client,
434                                                 THMC50_REG_TEMP[i]);
435                         data->temp_max[i] = i2c_smbus_read_byte_data(client,
436                                                 THMC50_REG_TEMP_MAX[i]);
437                         data->temp_min[i] = i2c_smbus_read_byte_data(client,
438                                                 THMC50_REG_TEMP_MIN[i]);
439                 }
440                 data->analog_out =
441                     i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
442                 data->alarms =
443                     i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
444                 data->last_updated = jiffies;
445                 data->valid = 1;
446         }
447
448         mutex_unlock(&data->update_lock);
449
450         return data;
451 }
452
453 static int __init sm_thmc50_init(void)
454 {
455         return i2c_add_driver(&thmc50_driver);
456 }
457
458 static void __exit sm_thmc50_exit(void)
459 {
460         i2c_del_driver(&thmc50_driver);
461 }
462
463 MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
464 MODULE_DESCRIPTION("THMC50 driver");
465
466 module_init(sm_thmc50_init);
467 module_exit(sm_thmc50_exit);