Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / max1668.c
blob88953f99e91437da56a3cad38f593141b3d2e98f
1 /*
2 Copyright (c) 2011 David George <david.george@ska.ac.za>
4 based on adm1021.c
5 some credit to Christoph Scheurer, but largely a rewrite
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.
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.
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.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.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 /* Addresses to scan */
33 static unsigned short max1668_addr_list[] = {
34 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
36 /* max1668 registers */
38 #define MAX1668_REG_TEMP(nr) (nr)
39 #define MAX1668_REG_STAT1 0x05
40 #define MAX1668_REG_STAT2 0x06
41 #define MAX1668_REG_MAN_ID 0xfe
42 #define MAX1668_REG_DEV_ID 0xff
44 /* limits */
46 /* write high limits */
47 #define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr))
48 /* write low limits */
49 #define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr))
50 /* read high limits */
51 #define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr))
52 /* read low limits */
53 #define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr))
55 /* manufacturer and device ID Constants */
56 #define MAN_ID_MAXIM 0x4d
57 #define DEV_ID_MAX1668 0x3
58 #define DEV_ID_MAX1805 0x5
59 #define DEV_ID_MAX1989 0xb
61 /* read only mode module parameter */
62 static bool read_only;
63 module_param(read_only, bool, 0);
64 MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
66 enum chips { max1668, max1805, max1989 };
68 struct max1668_data {
69 struct device *hwmon_dev;
70 enum chips type;
72 struct mutex update_lock;
73 char valid; /* !=0 if following fields are valid */
74 unsigned long last_updated; /* In jiffies */
76 /* 1x local and 4x remote */
77 s8 temp_max[5];
78 s8 temp_min[5];
79 s8 temp[5];
80 u16 alarms;
83 static struct max1668_data *max1668_update_device(struct device *dev)
85 struct i2c_client *client = to_i2c_client(dev);
86 struct max1668_data *data = i2c_get_clientdata(client);
87 struct max1668_data *ret = data;
88 s32 val;
89 int i;
91 mutex_lock(&data->update_lock);
93 if (data->valid && !time_after(jiffies,
94 data->last_updated + HZ + HZ / 2))
95 goto abort;
97 for (i = 0; i < 5; i++) {
98 val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i));
99 if (unlikely(val < 0)) {
100 ret = ERR_PTR(val);
101 goto abort;
103 data->temp[i] = (s8) val;
105 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i));
106 if (unlikely(val < 0)) {
107 ret = ERR_PTR(val);
108 goto abort;
110 data->temp_max[i] = (s8) val;
112 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i));
113 if (unlikely(val < 0)) {
114 ret = ERR_PTR(val);
115 goto abort;
117 data->temp_min[i] = (s8) val;
120 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1);
121 if (unlikely(val < 0)) {
122 ret = ERR_PTR(val);
123 goto abort;
125 data->alarms = val << 8;
127 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2);
128 if (unlikely(val < 0)) {
129 ret = ERR_PTR(val);
130 goto abort;
132 data->alarms |= val;
134 data->last_updated = jiffies;
135 data->valid = 1;
136 abort:
137 mutex_unlock(&data->update_lock);
139 return ret;
142 static ssize_t show_temp(struct device *dev,
143 struct device_attribute *devattr, char *buf)
145 int index = to_sensor_dev_attr(devattr)->index;
146 struct max1668_data *data = max1668_update_device(dev);
148 if (IS_ERR(data))
149 return PTR_ERR(data);
151 return sprintf(buf, "%d\n", data->temp[index] * 1000);
154 static ssize_t show_temp_max(struct device *dev,
155 struct device_attribute *devattr, char *buf)
157 int index = to_sensor_dev_attr(devattr)->index;
158 struct max1668_data *data = max1668_update_device(dev);
160 if (IS_ERR(data))
161 return PTR_ERR(data);
163 return sprintf(buf, "%d\n", data->temp_max[index] * 1000);
166 static ssize_t show_temp_min(struct device *dev,
167 struct device_attribute *devattr, char *buf)
169 int index = to_sensor_dev_attr(devattr)->index;
170 struct max1668_data *data = max1668_update_device(dev);
172 if (IS_ERR(data))
173 return PTR_ERR(data);
175 return sprintf(buf, "%d\n", data->temp_min[index] * 1000);
178 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
179 char *buf)
181 int index = to_sensor_dev_attr(attr)->index;
182 struct max1668_data *data = max1668_update_device(dev);
184 if (IS_ERR(data))
185 return PTR_ERR(data);
187 return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
190 static ssize_t show_fault(struct device *dev,
191 struct device_attribute *devattr, char *buf)
193 int index = to_sensor_dev_attr(devattr)->index;
194 struct max1668_data *data = max1668_update_device(dev);
196 if (IS_ERR(data))
197 return PTR_ERR(data);
199 return sprintf(buf, "%u\n",
200 (data->alarms & (1 << 12)) && data->temp[index] == 127);
203 static ssize_t set_temp_max(struct device *dev,
204 struct device_attribute *devattr,
205 const char *buf, size_t count)
207 int index = to_sensor_dev_attr(devattr)->index;
208 struct i2c_client *client = to_i2c_client(dev);
209 struct max1668_data *data = i2c_get_clientdata(client);
210 long temp;
211 int ret;
213 ret = kstrtol(buf, 10, &temp);
214 if (ret < 0)
215 return ret;
217 mutex_lock(&data->update_lock);
218 data->temp_max[index] = SENSORS_LIMIT(temp/1000, -128, 127);
219 if (i2c_smbus_write_byte_data(client,
220 MAX1668_REG_LIMH_WR(index),
221 data->temp_max[index]))
222 count = -EIO;
223 mutex_unlock(&data->update_lock);
225 return count;
228 static ssize_t set_temp_min(struct device *dev,
229 struct device_attribute *devattr,
230 const char *buf, size_t count)
232 int index = to_sensor_dev_attr(devattr)->index;
233 struct i2c_client *client = to_i2c_client(dev);
234 struct max1668_data *data = i2c_get_clientdata(client);
235 long temp;
236 int ret;
238 ret = kstrtol(buf, 10, &temp);
239 if (ret < 0)
240 return ret;
242 mutex_lock(&data->update_lock);
243 data->temp_min[index] = SENSORS_LIMIT(temp/1000, -128, 127);
244 if (i2c_smbus_write_byte_data(client,
245 MAX1668_REG_LIML_WR(index),
246 data->temp_max[index]))
247 count = -EIO;
248 mutex_unlock(&data->update_lock);
250 return count;
253 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
254 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
255 set_temp_max, 0);
256 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
257 set_temp_min, 0);
258 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
259 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
260 set_temp_max, 1);
261 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
262 set_temp_min, 1);
263 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
264 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
265 set_temp_max, 2);
266 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
267 set_temp_min, 2);
268 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
269 static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
270 set_temp_max, 3);
271 static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
272 set_temp_min, 3);
273 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
274 static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
275 set_temp_max, 4);
276 static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
277 set_temp_min, 4);
279 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
280 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
281 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
282 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
283 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
284 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
285 static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
286 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
287 static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
288 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
290 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
291 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
292 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
293 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
295 /* Attributes common to MAX1668, MAX1989 and MAX1805 */
296 static struct attribute *max1668_attribute_common[] = {
297 &sensor_dev_attr_temp1_max.dev_attr.attr,
298 &sensor_dev_attr_temp1_min.dev_attr.attr,
299 &sensor_dev_attr_temp1_input.dev_attr.attr,
300 &sensor_dev_attr_temp2_max.dev_attr.attr,
301 &sensor_dev_attr_temp2_min.dev_attr.attr,
302 &sensor_dev_attr_temp2_input.dev_attr.attr,
303 &sensor_dev_attr_temp3_max.dev_attr.attr,
304 &sensor_dev_attr_temp3_min.dev_attr.attr,
305 &sensor_dev_attr_temp3_input.dev_attr.attr,
307 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
308 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
309 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
310 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
311 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
312 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
314 &sensor_dev_attr_temp2_fault.dev_attr.attr,
315 &sensor_dev_attr_temp3_fault.dev_attr.attr,
316 NULL
319 /* Attributes not present on MAX1805 */
320 static struct attribute *max1668_attribute_unique[] = {
321 &sensor_dev_attr_temp4_max.dev_attr.attr,
322 &sensor_dev_attr_temp4_min.dev_attr.attr,
323 &sensor_dev_attr_temp4_input.dev_attr.attr,
324 &sensor_dev_attr_temp5_max.dev_attr.attr,
325 &sensor_dev_attr_temp5_min.dev_attr.attr,
326 &sensor_dev_attr_temp5_input.dev_attr.attr,
328 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
329 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
330 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
331 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
333 &sensor_dev_attr_temp4_fault.dev_attr.attr,
334 &sensor_dev_attr_temp5_fault.dev_attr.attr,
335 NULL
338 static umode_t max1668_attribute_mode(struct kobject *kobj,
339 struct attribute *attr, int index)
341 umode_t ret = S_IRUGO;
342 if (read_only)
343 return ret;
344 if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr ||
345 attr == &sensor_dev_attr_temp2_max.dev_attr.attr ||
346 attr == &sensor_dev_attr_temp3_max.dev_attr.attr ||
347 attr == &sensor_dev_attr_temp4_max.dev_attr.attr ||
348 attr == &sensor_dev_attr_temp5_max.dev_attr.attr ||
349 attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
350 attr == &sensor_dev_attr_temp2_min.dev_attr.attr ||
351 attr == &sensor_dev_attr_temp3_min.dev_attr.attr ||
352 attr == &sensor_dev_attr_temp4_min.dev_attr.attr ||
353 attr == &sensor_dev_attr_temp5_min.dev_attr.attr)
354 ret |= S_IWUSR;
355 return ret;
358 static const struct attribute_group max1668_group_common = {
359 .attrs = max1668_attribute_common,
360 .is_visible = max1668_attribute_mode
363 static const struct attribute_group max1668_group_unique = {
364 .attrs = max1668_attribute_unique,
365 .is_visible = max1668_attribute_mode
368 /* Return 0 if detection is successful, -ENODEV otherwise */
369 static int max1668_detect(struct i2c_client *client,
370 struct i2c_board_info *info)
372 struct i2c_adapter *adapter = client->adapter;
373 const char *type_name;
374 int man_id, dev_id;
376 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
377 return -ENODEV;
379 /* Check for unsupported part */
380 man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
381 if (man_id != MAN_ID_MAXIM)
382 return -ENODEV;
384 dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
385 if (dev_id < 0)
386 return -ENODEV;
388 type_name = NULL;
389 if (dev_id == DEV_ID_MAX1668)
390 type_name = "max1668";
391 else if (dev_id == DEV_ID_MAX1805)
392 type_name = "max1805";
393 else if (dev_id == DEV_ID_MAX1989)
394 type_name = "max1989";
396 if (!type_name)
397 return -ENODEV;
399 strlcpy(info->type, type_name, I2C_NAME_SIZE);
401 return 0;
404 static int max1668_probe(struct i2c_client *client,
405 const struct i2c_device_id *id)
407 struct i2c_adapter *adapter = client->adapter;
408 struct max1668_data *data;
409 int err;
411 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
412 return -ENODEV;
414 data = kzalloc(sizeof(struct max1668_data), GFP_KERNEL);
415 if (!data)
416 return -ENOMEM;
418 i2c_set_clientdata(client, data);
419 data->type = id->driver_data;
420 mutex_init(&data->update_lock);
422 /* Register sysfs hooks */
423 err = sysfs_create_group(&client->dev.kobj, &max1668_group_common);
424 if (err)
425 goto error_free;
427 if (data->type == max1668 || data->type == max1989) {
428 err = sysfs_create_group(&client->dev.kobj,
429 &max1668_group_unique);
430 if (err)
431 goto error_sysrem0;
434 data->hwmon_dev = hwmon_device_register(&client->dev);
435 if (IS_ERR(data->hwmon_dev)) {
436 err = PTR_ERR(data->hwmon_dev);
437 goto error_sysrem1;
440 return 0;
442 error_sysrem1:
443 if (data->type == max1668 || data->type == max1989)
444 sysfs_remove_group(&client->dev.kobj, &max1668_group_unique);
445 error_sysrem0:
446 sysfs_remove_group(&client->dev.kobj, &max1668_group_common);
447 error_free:
448 kfree(data);
449 return err;
452 static int max1668_remove(struct i2c_client *client)
454 struct max1668_data *data = i2c_get_clientdata(client);
456 hwmon_device_unregister(data->hwmon_dev);
457 if (data->type == max1668 || data->type == max1989)
458 sysfs_remove_group(&client->dev.kobj, &max1668_group_unique);
460 sysfs_remove_group(&client->dev.kobj, &max1668_group_common);
462 kfree(data);
463 return 0;
466 static const struct i2c_device_id max1668_id[] = {
467 { "max1668", max1668 },
468 { "max1805", max1805 },
469 { "max1989", max1989 },
472 MODULE_DEVICE_TABLE(i2c, max1668_id);
474 /* This is the driver that will be inserted */
475 static struct i2c_driver max1668_driver = {
476 .class = I2C_CLASS_HWMON,
477 .driver = {
478 .name = "max1668",
480 .probe = max1668_probe,
481 .remove = max1668_remove,
482 .id_table = max1668_id,
483 .detect = max1668_detect,
484 .address_list = max1668_addr_list,
487 static int __init sensors_max1668_init(void)
489 return i2c_add_driver(&max1668_driver);
492 static void __exit sensors_max1668_exit(void)
494 i2c_del_driver(&max1668_driver);
497 MODULE_AUTHOR("David George <david.george@ska.ac.za>");
498 MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
499 MODULE_LICENSE("GPL");
501 module_init(sensors_max1668_init)
502 module_exit(sensors_max1668_exit)