blob: 666d9f6263eb462e41a5ec319ca5d29dc67eb540 [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);
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);
224
225 return count;
226}
227
228static ssize_t set_temp_min(struct device *dev,
229 struct device_attribute *devattr,
230 const char *buf, size_t count)
231{
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;
237
238 ret = kstrtol(buf, 10, &temp);
239 if (ret < 0)
240 return ret;
241
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);
249
250 return count;
251}
252
253static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
254static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
255 set_temp_max, 0);
256static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
257 set_temp_min, 0);
258static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
259static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
260 set_temp_max, 1);
261static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
262 set_temp_min, 1);
263static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
264static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
265 set_temp_max, 2);
266static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
267 set_temp_min, 2);
268static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
269static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
270 set_temp_max, 3);
271static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
272 set_temp_min, 3);
273static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
274static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
275 set_temp_max, 4);
276static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
277 set_temp_min, 4);
278
279static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
280static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
281static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
282static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
283static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
284static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
285static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
286static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
287static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
288static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
289
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700290static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
291static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
292static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
293static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
294
David George731b4ca2011-06-02 08:43:45 -0700295/* Attributes common to MAX1668, MAX1989 and MAX1805 */
296static 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,
306
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,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700313
314 &sensor_dev_attr_temp2_fault.dev_attr.attr,
315 &sensor_dev_attr_temp3_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700316 NULL
317};
318
319/* Attributes not present on MAX1805 */
320static 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,
327
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,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700332
333 &sensor_dev_attr_temp4_fault.dev_attr.attr,
334 &sensor_dev_attr_temp5_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700335 NULL
336};
337
Al Viro587a1f12011-07-23 23:11:19 -0400338static umode_t max1668_attribute_mode(struct kobject *kobj,
David George731b4ca2011-06-02 08:43:45 -0700339 struct attribute *attr, int index)
340{
Al Viro587a1f12011-07-23 23:11:19 -0400341 umode_t ret = S_IRUGO;
David George731b4ca2011-06-02 08:43:45 -0700342 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;
356}
357
358static const struct attribute_group max1668_group_common = {
359 .attrs = max1668_attribute_common,
360 .is_visible = max1668_attribute_mode
361};
362
363static const struct attribute_group max1668_group_unique = {
364 .attrs = max1668_attribute_unique,
365 .is_visible = max1668_attribute_mode
366};
367
368/* Return 0 if detection is successful, -ENODEV otherwise */
369static int max1668_detect(struct i2c_client *client,
370 struct i2c_board_info *info)
371{
372 struct i2c_adapter *adapter = client->adapter;
373 const char *type_name;
374 int man_id, dev_id;
375
376 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
377 return -ENODEV;
378
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;
383
384 dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
385 if (dev_id < 0)
386 return -ENODEV;
387
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";
395
396 if (!type_name)
397 return -ENODEV;
398
399 strlcpy(info->type, type_name, I2C_NAME_SIZE);
400
401 return 0;
402}
403
404static int max1668_probe(struct i2c_client *client,
405 const struct i2c_device_id *id)
406{
407 struct i2c_adapter *adapter = client->adapter;
408 struct max1668_data *data;
409 int err;
410
411 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
412 return -ENODEV;
413
Guenter Roeck7008b972012-06-02 09:58:12 -0700414 data = devm_kzalloc(&client->dev, sizeof(struct max1668_data),
415 GFP_KERNEL);
David George731b4ca2011-06-02 08:43:45 -0700416 if (!data)
417 return -ENOMEM;
418
419 i2c_set_clientdata(client, data);
420 data->type = id->driver_data;
421 mutex_init(&data->update_lock);
422
423 /* Register sysfs hooks */
424 err = sysfs_create_group(&client->dev.kobj, &max1668_group_common);
425 if (err)
Guenter Roeck7008b972012-06-02 09:58:12 -0700426 return err;
David George731b4ca2011-06-02 08:43:45 -0700427
428 if (data->type == max1668 || data->type == max1989) {
429 err = sysfs_create_group(&client->dev.kobj,
430 &max1668_group_unique);
431 if (err)
432 goto error_sysrem0;
433 }
434
435 data->hwmon_dev = hwmon_device_register(&client->dev);
436 if (IS_ERR(data->hwmon_dev)) {
437 err = PTR_ERR(data->hwmon_dev);
438 goto error_sysrem1;
439 }
440
441 return 0;
442
443error_sysrem1:
444 if (data->type == max1668 || data->type == max1989)
445 sysfs_remove_group(&client->dev.kobj, &max1668_group_unique);
446error_sysrem0:
447 sysfs_remove_group(&client->dev.kobj, &max1668_group_common);
David George731b4ca2011-06-02 08:43:45 -0700448 return err;
449}
450
451static int max1668_remove(struct i2c_client *client)
452{
453 struct max1668_data *data = i2c_get_clientdata(client);
454
455 hwmon_device_unregister(data->hwmon_dev);
456 if (data->type == max1668 || data->type == max1989)
457 sysfs_remove_group(&client->dev.kobj, &max1668_group_unique);
458
459 sysfs_remove_group(&client->dev.kobj, &max1668_group_common);
460
David George731b4ca2011-06-02 08:43:45 -0700461 return 0;
462}
463
464static const struct i2c_device_id max1668_id[] = {
465 { "max1668", max1668 },
466 { "max1805", max1805 },
467 { "max1989", max1989 },
468 { }
469};
470MODULE_DEVICE_TABLE(i2c, max1668_id);
471
472/* This is the driver that will be inserted */
473static struct i2c_driver max1668_driver = {
474 .class = I2C_CLASS_HWMON,
475 .driver = {
476 .name = "max1668",
477 },
478 .probe = max1668_probe,
479 .remove = max1668_remove,
480 .id_table = max1668_id,
481 .detect = max1668_detect,
482 .address_list = max1668_addr_list,
483};
484
Axel Linf0967ee2012-01-20 15:38:18 +0800485module_i2c_driver(max1668_driver);
David George731b4ca2011-06-02 08:43:45 -0700486
487MODULE_AUTHOR("David George <david.george@ska.ac.za>");
488MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
489MODULE_LICENSE("GPL");