blob: bbbe340332b9524f949118171c023ece97d93a26 [file] [log] [blame]
David George731b4ca2011-06-02 08:43:45 -07001/*
Guenter Roeckb6707b72012-01-19 11:02:22 -08002 * Copyright (c) 2011 David George <david.george@ska.ac.za>
3 *
4 * based on adm1021.c
5 * some credit to Christoph Scheurer, but largely a rewrite
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 */
David George731b4ca2011-06-02 08:43:45 -070021
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>
31
32/* Addresses to scan */
33static unsigned short max1668_addr_list[] = {
34 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
35
36/* max1668 registers */
37
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
43
44/* limits */
45
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))
54
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
60
61/* read only mode module parameter */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103062static bool read_only;
David George731b4ca2011-06-02 08:43:45 -070063module_param(read_only, bool, 0);
64MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
65
66enum chips { max1668, max1805, max1989 };
67
68struct max1668_data {
69 struct device *hwmon_dev;
70 enum chips type;
71
72 struct mutex update_lock;
73 char valid; /* !=0 if following fields are valid */
74 unsigned long last_updated; /* In jiffies */
75
76 /* 1x local and 4x remote */
77 s8 temp_max[5];
78 s8 temp_min[5];
79 s8 temp[5];
80 u16 alarms;
81};
82
83static struct max1668_data *max1668_update_device(struct device *dev)
84{
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;
90
91 mutex_lock(&data->update_lock);
92
93 if (data->valid && !time_after(jiffies,
94 data->last_updated + HZ + HZ / 2))
95 goto abort;
96
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;
102 }
103 data->temp[i] = (s8) val;
104
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;
109 }
110 data->temp_max[i] = (s8) val;
111
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;
116 }
117 data->temp_min[i] = (s8) val;
118 }
119
120 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1);
121 if (unlikely(val < 0)) {
122 ret = ERR_PTR(val);
123 goto abort;
124 }
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700125 data->alarms = val << 8;
David George731b4ca2011-06-02 08:43:45 -0700126
127 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2);
128 if (unlikely(val < 0)) {
129 ret = ERR_PTR(val);
130 goto abort;
131 }
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700132 data->alarms |= val;
David George731b4ca2011-06-02 08:43:45 -0700133
134 data->last_updated = jiffies;
135 data->valid = 1;
136abort:
137 mutex_unlock(&data->update_lock);
138
139 return ret;
140}
141
142static ssize_t show_temp(struct device *dev,
143 struct device_attribute *devattr, char *buf)
144{
145 int index = to_sensor_dev_attr(devattr)->index;
146 struct max1668_data *data = max1668_update_device(dev);
147
148 if (IS_ERR(data))
149 return PTR_ERR(data);
150
151 return sprintf(buf, "%d\n", data->temp[index] * 1000);
152}
153
154static ssize_t show_temp_max(struct device *dev,
155 struct device_attribute *devattr, char *buf)
156{
157 int index = to_sensor_dev_attr(devattr)->index;
158 struct max1668_data *data = max1668_update_device(dev);
159
160 if (IS_ERR(data))
161 return PTR_ERR(data);
162
163 return sprintf(buf, "%d\n", data->temp_max[index] * 1000);
164}
165
166static ssize_t show_temp_min(struct device *dev,
167 struct device_attribute *devattr, char *buf)
168{
169 int index = to_sensor_dev_attr(devattr)->index;
170 struct max1668_data *data = max1668_update_device(dev);
171
172 if (IS_ERR(data))
173 return PTR_ERR(data);
174
175 return sprintf(buf, "%d\n", data->temp_min[index] * 1000);
176}
177
178static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
179 char *buf)
180{
181 int index = to_sensor_dev_attr(attr)->index;
182 struct max1668_data *data = max1668_update_device(dev);
183
184 if (IS_ERR(data))
185 return PTR_ERR(data);
186
187 return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
188}
189
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700190static ssize_t show_fault(struct device *dev,
191 struct device_attribute *devattr, char *buf)
192{
193 int index = to_sensor_dev_attr(devattr)->index;
194 struct max1668_data *data = max1668_update_device(dev);
195
196 if (IS_ERR(data))
197 return PTR_ERR(data);
198
199 return sprintf(buf, "%u\n",
200 (data->alarms & (1 << 12)) && data->temp[index] == 127);
201}
202
David George731b4ca2011-06-02 08:43:45 -0700203static ssize_t set_temp_max(struct device *dev,
204 struct device_attribute *devattr,
205 const char *buf, size_t count)
206{
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;
212
213 ret = kstrtol(buf, 10, &temp);
214 if (ret < 0)
215 return ret;
216
217 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800218 data->temp_max[index] = clamp_val(temp/1000, -128, 127);
Guenter Roeckc5a70662014-02-15 17:59:05 -0800219 ret = i2c_smbus_write_byte_data(client,
David George731b4ca2011-06-02 08:43:45 -0700220 MAX1668_REG_LIMH_WR(index),
Guenter Roeckc5a70662014-02-15 17:59:05 -0800221 data->temp_max[index]);
222 if (ret < 0)
223 count = ret;
David George731b4ca2011-06-02 08:43:45 -0700224 mutex_unlock(&data->update_lock);
225
226 return count;
227}
228
229static ssize_t set_temp_min(struct device *dev,
230 struct device_attribute *devattr,
231 const char *buf, size_t count)
232{
233 int index = to_sensor_dev_attr(devattr)->index;
234 struct i2c_client *client = to_i2c_client(dev);
235 struct max1668_data *data = i2c_get_clientdata(client);
236 long temp;
237 int ret;
238
239 ret = kstrtol(buf, 10, &temp);
240 if (ret < 0)
241 return ret;
242
243 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800244 data->temp_min[index] = clamp_val(temp/1000, -128, 127);
Guenter Roeckc5a70662014-02-15 17:59:05 -0800245 ret = i2c_smbus_write_byte_data(client,
David George731b4ca2011-06-02 08:43:45 -0700246 MAX1668_REG_LIML_WR(index),
Guenter Roeckc5a70662014-02-15 17:59:05 -0800247 data->temp_min[index]);
248 if (ret < 0)
249 count = ret;
David George731b4ca2011-06-02 08:43:45 -0700250 mutex_unlock(&data->update_lock);
251
252 return count;
253}
254
255static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
256static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
257 set_temp_max, 0);
258static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
259 set_temp_min, 0);
260static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
261static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
262 set_temp_max, 1);
263static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
264 set_temp_min, 1);
265static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
266static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
267 set_temp_max, 2);
268static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
269 set_temp_min, 2);
270static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
271static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
272 set_temp_max, 3);
273static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
274 set_temp_min, 3);
275static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
276static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
277 set_temp_max, 4);
278static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
279 set_temp_min, 4);
280
281static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
282static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
283static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
284static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
285static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
286static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
287static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
288static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
289static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
290static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
291
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700292static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
293static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
294static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
295static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
296
David George731b4ca2011-06-02 08:43:45 -0700297/* Attributes common to MAX1668, MAX1989 and MAX1805 */
298static struct attribute *max1668_attribute_common[] = {
299 &sensor_dev_attr_temp1_max.dev_attr.attr,
300 &sensor_dev_attr_temp1_min.dev_attr.attr,
301 &sensor_dev_attr_temp1_input.dev_attr.attr,
302 &sensor_dev_attr_temp2_max.dev_attr.attr,
303 &sensor_dev_attr_temp2_min.dev_attr.attr,
304 &sensor_dev_attr_temp2_input.dev_attr.attr,
305 &sensor_dev_attr_temp3_max.dev_attr.attr,
306 &sensor_dev_attr_temp3_min.dev_attr.attr,
307 &sensor_dev_attr_temp3_input.dev_attr.attr,
308
309 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
310 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
311 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
312 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
313 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
314 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700315
316 &sensor_dev_attr_temp2_fault.dev_attr.attr,
317 &sensor_dev_attr_temp3_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700318 NULL
319};
320
321/* Attributes not present on MAX1805 */
322static struct attribute *max1668_attribute_unique[] = {
323 &sensor_dev_attr_temp4_max.dev_attr.attr,
324 &sensor_dev_attr_temp4_min.dev_attr.attr,
325 &sensor_dev_attr_temp4_input.dev_attr.attr,
326 &sensor_dev_attr_temp5_max.dev_attr.attr,
327 &sensor_dev_attr_temp5_min.dev_attr.attr,
328 &sensor_dev_attr_temp5_input.dev_attr.attr,
329
330 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
331 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
332 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
333 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700334
335 &sensor_dev_attr_temp4_fault.dev_attr.attr,
336 &sensor_dev_attr_temp5_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700337 NULL
338};
339
Al Viro587a1f12011-07-23 23:11:19 -0400340static umode_t max1668_attribute_mode(struct kobject *kobj,
David George731b4ca2011-06-02 08:43:45 -0700341 struct attribute *attr, int index)
342{
Al Viro587a1f12011-07-23 23:11:19 -0400343 umode_t ret = S_IRUGO;
David George731b4ca2011-06-02 08:43:45 -0700344 if (read_only)
345 return ret;
346 if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr ||
347 attr == &sensor_dev_attr_temp2_max.dev_attr.attr ||
348 attr == &sensor_dev_attr_temp3_max.dev_attr.attr ||
349 attr == &sensor_dev_attr_temp4_max.dev_attr.attr ||
350 attr == &sensor_dev_attr_temp5_max.dev_attr.attr ||
351 attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
352 attr == &sensor_dev_attr_temp2_min.dev_attr.attr ||
353 attr == &sensor_dev_attr_temp3_min.dev_attr.attr ||
354 attr == &sensor_dev_attr_temp4_min.dev_attr.attr ||
355 attr == &sensor_dev_attr_temp5_min.dev_attr.attr)
356 ret |= S_IWUSR;
357 return ret;
358}
359
360static const struct attribute_group max1668_group_common = {
361 .attrs = max1668_attribute_common,
362 .is_visible = max1668_attribute_mode
363};
364
365static const struct attribute_group max1668_group_unique = {
366 .attrs = max1668_attribute_unique,
367 .is_visible = max1668_attribute_mode
368};
369
370/* Return 0 if detection is successful, -ENODEV otherwise */
371static int max1668_detect(struct i2c_client *client,
372 struct i2c_board_info *info)
373{
374 struct i2c_adapter *adapter = client->adapter;
375 const char *type_name;
376 int man_id, dev_id;
377
378 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
379 return -ENODEV;
380
381 /* Check for unsupported part */
382 man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
383 if (man_id != MAN_ID_MAXIM)
384 return -ENODEV;
385
386 dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
387 if (dev_id < 0)
388 return -ENODEV;
389
390 type_name = NULL;
391 if (dev_id == DEV_ID_MAX1668)
392 type_name = "max1668";
393 else if (dev_id == DEV_ID_MAX1805)
394 type_name = "max1805";
395 else if (dev_id == DEV_ID_MAX1989)
396 type_name = "max1989";
397
398 if (!type_name)
399 return -ENODEV;
400
401 strlcpy(info->type, type_name, I2C_NAME_SIZE);
402
403 return 0;
404}
405
406static int max1668_probe(struct i2c_client *client,
407 const struct i2c_device_id *id)
408{
409 struct i2c_adapter *adapter = client->adapter;
410 struct max1668_data *data;
411 int err;
412
413 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
414 return -ENODEV;
415
Guenter Roeck7008b972012-06-02 09:58:12 -0700416 data = devm_kzalloc(&client->dev, sizeof(struct max1668_data),
417 GFP_KERNEL);
David George731b4ca2011-06-02 08:43:45 -0700418 if (!data)
419 return -ENOMEM;
420
421 i2c_set_clientdata(client, data);
422 data->type = id->driver_data;
423 mutex_init(&data->update_lock);
424
425 /* Register sysfs hooks */
426 err = sysfs_create_group(&client->dev.kobj, &max1668_group_common);
427 if (err)
Guenter Roeck7008b972012-06-02 09:58:12 -0700428 return err;
David George731b4ca2011-06-02 08:43:45 -0700429
430 if (data->type == max1668 || data->type == max1989) {
431 err = sysfs_create_group(&client->dev.kobj,
432 &max1668_group_unique);
433 if (err)
434 goto error_sysrem0;
435 }
436
437 data->hwmon_dev = hwmon_device_register(&client->dev);
438 if (IS_ERR(data->hwmon_dev)) {
439 err = PTR_ERR(data->hwmon_dev);
440 goto error_sysrem1;
441 }
442
443 return 0;
444
445error_sysrem1:
446 if (data->type == max1668 || data->type == max1989)
447 sysfs_remove_group(&client->dev.kobj, &max1668_group_unique);
448error_sysrem0:
449 sysfs_remove_group(&client->dev.kobj, &max1668_group_common);
David George731b4ca2011-06-02 08:43:45 -0700450 return err;
451}
452
453static int max1668_remove(struct i2c_client *client)
454{
455 struct max1668_data *data = i2c_get_clientdata(client);
456
457 hwmon_device_unregister(data->hwmon_dev);
458 if (data->type == max1668 || data->type == max1989)
459 sysfs_remove_group(&client->dev.kobj, &max1668_group_unique);
460
461 sysfs_remove_group(&client->dev.kobj, &max1668_group_common);
462
David George731b4ca2011-06-02 08:43:45 -0700463 return 0;
464}
465
466static const struct i2c_device_id max1668_id[] = {
467 { "max1668", max1668 },
468 { "max1805", max1805 },
469 { "max1989", max1989 },
470 { }
471};
472MODULE_DEVICE_TABLE(i2c, max1668_id);
473
474/* This is the driver that will be inserted */
475static struct i2c_driver max1668_driver = {
476 .class = I2C_CLASS_HWMON,
477 .driver = {
478 .name = "max1668",
479 },
480 .probe = max1668_probe,
481 .remove = max1668_remove,
482 .id_table = max1668_id,
483 .detect = max1668_detect,
484 .address_list = max1668_addr_list,
485};
486
Axel Linf0967ee2012-01-20 15:38:18 +0800487module_i2c_driver(max1668_driver);
David George731b4ca2011-06-02 08:43:45 -0700488
489MODULE_AUTHOR("David George <david.george@ska.ac.za>");
490MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
491MODULE_LICENSE("GPL");