Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / thmc50.c
blobce9ed16ec1c822b3fb9068a46bf674e5ded0e7af
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>
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.
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.
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.
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>
32 MODULE_LICENSE("GPL");
34 /* Addresses to scan */
35 <<<<<<< HEAD:drivers/hwmon/thmc50.c
36 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37 =======
38 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
39 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/thmc50.c
41 /* Insmod parameters */
42 I2C_CLIENT_INSMOD_2(thmc50, adm1022);
43 I2C_CLIENT_MODULE_PARM(adm1022_temp3, "List of adapter,address pairs "
44 "to enable 3rd temperature (ADM1022 only)");
46 /* Many THMC50 constants specified below */
48 /* The THMC50 registers */
49 #define THMC50_REG_CONF 0x40
50 #define THMC50_REG_COMPANY_ID 0x3E
51 #define THMC50_REG_DIE_CODE 0x3F
52 #define THMC50_REG_ANALOG_OUT 0x19
54 * The mirror status register cannot be used as
55 * reading it does not clear alarms.
57 #define THMC50_REG_INTR 0x41
59 <<<<<<< HEAD:drivers/hwmon/thmc50.c
60 const static u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
61 const static u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
62 const static u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
63 =======
64 static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
65 static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
66 static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
67 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/thmc50.c
69 #define THMC50_REG_CONF_nFANOFF 0x20
71 /* Each client has this additional data */
72 struct thmc50_data {
73 struct i2c_client client;
74 struct device *hwmon_dev;
76 struct mutex update_lock;
77 enum chips type;
78 unsigned long last_updated; /* In jiffies */
79 char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */
80 char valid; /* !=0 if following fields are valid */
82 /* Register values */
83 s8 temp_input[3];
84 s8 temp_max[3];
85 s8 temp_min[3];
86 u8 analog_out;
87 u8 alarms;
90 static int thmc50_attach_adapter(struct i2c_adapter *adapter);
91 static int thmc50_detach_client(struct i2c_client *client);
92 static void thmc50_init_client(struct i2c_client *client);
93 static struct thmc50_data *thmc50_update_device(struct device *dev);
95 static struct i2c_driver thmc50_driver = {
96 .driver = {
97 .name = "thmc50",
99 .attach_adapter = thmc50_attach_adapter,
100 .detach_client = thmc50_detach_client,
103 static ssize_t show_analog_out(struct device *dev,
104 struct device_attribute *attr, char *buf)
106 struct thmc50_data *data = thmc50_update_device(dev);
107 return sprintf(buf, "%d\n", data->analog_out);
110 static ssize_t set_analog_out(struct device *dev,
111 struct device_attribute *attr,
112 const char *buf, size_t count)
114 struct i2c_client *client = to_i2c_client(dev);
115 struct thmc50_data *data = i2c_get_clientdata(client);
116 int tmp = simple_strtoul(buf, NULL, 10);
117 int config;
119 mutex_lock(&data->update_lock);
120 data->analog_out = SENSORS_LIMIT(tmp, 0, 255);
121 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
122 data->analog_out);
124 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
125 if (data->analog_out == 0)
126 config &= ~THMC50_REG_CONF_nFANOFF;
127 else
128 config |= THMC50_REG_CONF_nFANOFF;
129 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
131 mutex_unlock(&data->update_lock);
132 return count;
135 /* There is only one PWM mode = DC */
136 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
137 char *buf)
139 return sprintf(buf, "0\n");
142 /* Temperatures */
143 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
144 char *buf)
146 int nr = to_sensor_dev_attr(attr)->index;
147 struct thmc50_data *data = thmc50_update_device(dev);
148 return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
151 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
152 char *buf)
154 int nr = to_sensor_dev_attr(attr)->index;
155 struct thmc50_data *data = thmc50_update_device(dev);
156 return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
159 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
160 const char *buf, size_t count)
162 int nr = to_sensor_dev_attr(attr)->index;
163 struct i2c_client *client = to_i2c_client(dev);
164 struct thmc50_data *data = i2c_get_clientdata(client);
165 int val = simple_strtol(buf, NULL, 10);
167 mutex_lock(&data->update_lock);
168 data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
169 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
170 data->temp_min[nr]);
171 mutex_unlock(&data->update_lock);
172 return count;
175 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
176 char *buf)
178 int nr = to_sensor_dev_attr(attr)->index;
179 struct thmc50_data *data = thmc50_update_device(dev);
180 return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
183 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
184 const char *buf, size_t count)
186 int nr = to_sensor_dev_attr(attr)->index;
187 struct i2c_client *client = to_i2c_client(dev);
188 struct thmc50_data *data = i2c_get_clientdata(client);
189 int val = simple_strtol(buf, NULL, 10);
191 mutex_lock(&data->update_lock);
192 data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
193 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
194 data->temp_max[nr]);
195 mutex_unlock(&data->update_lock);
196 return count;
199 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
200 char *buf)
202 int index = to_sensor_dev_attr(attr)->index;
203 struct thmc50_data *data = thmc50_update_device(dev);
205 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
208 #define temp_reg(offset) \
209 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
210 NULL, offset - 1); \
211 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
212 show_temp_min, set_temp_min, offset - 1); \
213 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
214 show_temp_max, set_temp_max, offset - 1);
216 temp_reg(1);
217 temp_reg(2);
218 temp_reg(3);
220 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
221 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
222 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
223 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
224 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
226 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
227 set_analog_out, 0);
228 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
230 static struct attribute *thmc50_attributes[] = {
231 &sensor_dev_attr_temp1_max.dev_attr.attr,
232 &sensor_dev_attr_temp1_min.dev_attr.attr,
233 &sensor_dev_attr_temp1_input.dev_attr.attr,
234 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
235 &sensor_dev_attr_temp2_max.dev_attr.attr,
236 &sensor_dev_attr_temp2_min.dev_attr.attr,
237 &sensor_dev_attr_temp2_input.dev_attr.attr,
238 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
239 &sensor_dev_attr_temp2_fault.dev_attr.attr,
240 &sensor_dev_attr_pwm1.dev_attr.attr,
241 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
242 NULL
245 static const struct attribute_group thmc50_group = {
246 .attrs = thmc50_attributes,
249 /* for ADM1022 3rd temperature mode */
250 static struct attribute *temp3_attributes[] = {
251 &sensor_dev_attr_temp3_max.dev_attr.attr,
252 &sensor_dev_attr_temp3_min.dev_attr.attr,
253 &sensor_dev_attr_temp3_input.dev_attr.attr,
254 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
255 &sensor_dev_attr_temp3_fault.dev_attr.attr,
256 NULL
259 static const struct attribute_group temp3_group = {
260 .attrs = temp3_attributes,
263 static int thmc50_detect(struct i2c_adapter *adapter, int address, int kind)
265 unsigned company;
266 unsigned revision;
267 unsigned config;
268 struct i2c_client *client;
269 struct thmc50_data *data;
270 struct device *dev;
271 int err = 0;
272 const char *type_name;
274 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
275 pr_debug("thmc50: detect failed, "
276 "smbus byte data not supported!\n");
277 goto exit;
280 /* OK. For now, we presume we have a valid client. We now create the
281 client structure, even though we cannot fill it completely yet.
282 But it allows us to access thmc50 registers. */
283 if (!(data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL))) {
284 pr_debug("thmc50: detect failed, kzalloc failed!\n");
285 err = -ENOMEM;
286 goto exit;
289 client = &data->client;
290 i2c_set_clientdata(client, data);
291 client->addr = address;
292 client->adapter = adapter;
293 client->driver = &thmc50_driver;
294 dev = &client->dev;
296 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
297 client->addr, i2c_adapter_id(client->adapter));
299 /* Now, we do the remaining detection. */
300 company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
301 revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
302 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
304 if (kind == 0)
305 kind = thmc50;
306 else if (kind < 0) {
307 err = -ENODEV;
308 if (revision >= 0xc0 && ((config & 0x10) == 0)) {
309 if (company == 0x49) {
310 kind = thmc50;
311 err = 0;
312 } else if (company == 0x41) {
313 kind = adm1022;
314 err = 0;
318 if (err == -ENODEV) {
319 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
320 goto exit_free;
322 data->type = kind;
324 if (kind == adm1022) {
325 int id = i2c_adapter_id(client->adapter);
326 int i;
328 type_name = "adm1022";
329 data->has_temp3 = (config >> 7) & 1; /* config MSB */
330 for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
331 if (adm1022_temp3[i] == id &&
332 adm1022_temp3[i + 1] == address) {
333 /* enable 2nd remote temp */
334 data->has_temp3 = 1;
335 break;
337 } else {
338 type_name = "thmc50";
340 pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
341 type_name, (revision >> 4) - 0xc, revision & 0xf);
343 /* Fill in the remaining client fields & put it into the global list */
344 strlcpy(client->name, type_name, I2C_NAME_SIZE);
345 mutex_init(&data->update_lock);
347 /* Tell the I2C layer a new client has arrived */
348 if ((err = i2c_attach_client(client)))
349 goto exit_free;
351 thmc50_init_client(client);
353 /* Register sysfs hooks */
354 if ((err = sysfs_create_group(&client->dev.kobj, &thmc50_group)))
355 goto exit_detach;
357 /* Register ADM1022 sysfs hooks */
358 if (data->has_temp3)
359 if ((err = sysfs_create_group(&client->dev.kobj,
360 &temp3_group)))
361 goto exit_remove_sysfs_thmc50;
363 /* Register a new directory entry with module sensors */
364 data->hwmon_dev = hwmon_device_register(&client->dev);
365 if (IS_ERR(data->hwmon_dev)) {
366 err = PTR_ERR(data->hwmon_dev);
367 goto exit_remove_sysfs;
370 return 0;
372 exit_remove_sysfs:
373 if (data->has_temp3)
374 sysfs_remove_group(&client->dev.kobj, &temp3_group);
375 exit_remove_sysfs_thmc50:
376 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
377 exit_detach:
378 i2c_detach_client(client);
379 exit_free:
380 kfree(data);
381 exit:
382 return err;
385 static int thmc50_attach_adapter(struct i2c_adapter *adapter)
387 if (!(adapter->class & I2C_CLASS_HWMON))
388 return 0;
389 return i2c_probe(adapter, &addr_data, thmc50_detect);
392 static int thmc50_detach_client(struct i2c_client *client)
394 struct thmc50_data *data = i2c_get_clientdata(client);
395 int err;
397 hwmon_device_unregister(data->hwmon_dev);
398 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
399 if (data->has_temp3)
400 sysfs_remove_group(&client->dev.kobj, &temp3_group);
402 if ((err = i2c_detach_client(client)))
403 return err;
405 kfree(data);
407 return 0;
410 static void thmc50_init_client(struct i2c_client *client)
412 struct thmc50_data *data = i2c_get_clientdata(client);
413 int config;
415 data->analog_out = i2c_smbus_read_byte_data(client,
416 THMC50_REG_ANALOG_OUT);
417 /* set up to at least 1 */
418 if (data->analog_out == 0) {
419 data->analog_out = 1;
420 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
421 data->analog_out);
423 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
424 config |= 0x1; /* start the chip if it is in standby mode */
425 if (data->has_temp3)
426 config |= 0x80; /* enable 2nd remote temp */
427 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
430 static struct thmc50_data *thmc50_update_device(struct device *dev)
432 struct i2c_client *client = to_i2c_client(dev);
433 struct thmc50_data *data = i2c_get_clientdata(client);
434 int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
436 mutex_lock(&data->update_lock);
438 if (time_after(jiffies, data->last_updated + timeout)
439 || !data->valid) {
441 int temps = data->has_temp3 ? 3 : 2;
442 int i;
443 for (i = 0; i < temps; i++) {
444 data->temp_input[i] = i2c_smbus_read_byte_data(client,
445 THMC50_REG_TEMP[i]);
446 data->temp_max[i] = i2c_smbus_read_byte_data(client,
447 THMC50_REG_TEMP_MAX[i]);
448 data->temp_min[i] = i2c_smbus_read_byte_data(client,
449 THMC50_REG_TEMP_MIN[i]);
451 data->analog_out =
452 i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
453 data->alarms =
454 i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
455 data->last_updated = jiffies;
456 data->valid = 1;
459 mutex_unlock(&data->update_lock);
461 return data;
464 static int __init sm_thmc50_init(void)
466 return i2c_add_driver(&thmc50_driver);
469 static void __exit sm_thmc50_exit(void)
471 i2c_del_driver(&thmc50_driver);
474 MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
475 MODULE_DESCRIPTION("THMC50 driver");
477 module_init(sm_thmc50_init);
478 module_exit(sm_thmc50_exit);