blob: 7ca889910262b61cb5a2ee08cab20bed54537dda [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 */
Axel Lin28681d72014-07-18 16:02:38 +080033static const unsigned short max1668_addr_list[] = {
David George731b4ca2011-06-02 08:43:45 -070034 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 {
Guenter Roeck52305512014-02-15 17:50:48 -080069 struct i2c_client *client;
70 const struct attribute_group *groups[3];
David George731b4ca2011-06-02 08:43:45 -070071 enum chips type;
72
73 struct mutex update_lock;
74 char valid; /* !=0 if following fields are valid */
75 unsigned long last_updated; /* In jiffies */
76
77 /* 1x local and 4x remote */
78 s8 temp_max[5];
79 s8 temp_min[5];
80 s8 temp[5];
81 u16 alarms;
82};
83
84static struct max1668_data *max1668_update_device(struct device *dev)
85{
Guenter Roeck52305512014-02-15 17:50:48 -080086 struct max1668_data *data = dev_get_drvdata(dev);
87 struct i2c_client *client = data->client;
David George731b4ca2011-06-02 08:43:45 -070088 struct max1668_data *ret = data;
89 s32 val;
90 int i;
91
92 mutex_lock(&data->update_lock);
93
94 if (data->valid && !time_after(jiffies,
95 data->last_updated + HZ + HZ / 2))
96 goto abort;
97
98 for (i = 0; i < 5; i++) {
99 val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i));
100 if (unlikely(val < 0)) {
101 ret = ERR_PTR(val);
102 goto abort;
103 }
104 data->temp[i] = (s8) val;
105
106 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i));
107 if (unlikely(val < 0)) {
108 ret = ERR_PTR(val);
109 goto abort;
110 }
111 data->temp_max[i] = (s8) val;
112
113 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i));
114 if (unlikely(val < 0)) {
115 ret = ERR_PTR(val);
116 goto abort;
117 }
118 data->temp_min[i] = (s8) val;
119 }
120
121 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1);
122 if (unlikely(val < 0)) {
123 ret = ERR_PTR(val);
124 goto abort;
125 }
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700126 data->alarms = val << 8;
David George731b4ca2011-06-02 08:43:45 -0700127
128 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2);
129 if (unlikely(val < 0)) {
130 ret = ERR_PTR(val);
131 goto abort;
132 }
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700133 data->alarms |= val;
David George731b4ca2011-06-02 08:43:45 -0700134
135 data->last_updated = jiffies;
136 data->valid = 1;
137abort:
138 mutex_unlock(&data->update_lock);
139
140 return ret;
141}
142
143static ssize_t show_temp(struct device *dev,
144 struct device_attribute *devattr, char *buf)
145{
146 int index = to_sensor_dev_attr(devattr)->index;
147 struct max1668_data *data = max1668_update_device(dev);
148
149 if (IS_ERR(data))
150 return PTR_ERR(data);
151
152 return sprintf(buf, "%d\n", data->temp[index] * 1000);
153}
154
155static ssize_t show_temp_max(struct device *dev,
156 struct device_attribute *devattr, char *buf)
157{
158 int index = to_sensor_dev_attr(devattr)->index;
159 struct max1668_data *data = max1668_update_device(dev);
160
161 if (IS_ERR(data))
162 return PTR_ERR(data);
163
164 return sprintf(buf, "%d\n", data->temp_max[index] * 1000);
165}
166
167static ssize_t show_temp_min(struct device *dev,
168 struct device_attribute *devattr, char *buf)
169{
170 int index = to_sensor_dev_attr(devattr)->index;
171 struct max1668_data *data = max1668_update_device(dev);
172
173 if (IS_ERR(data))
174 return PTR_ERR(data);
175
176 return sprintf(buf, "%d\n", data->temp_min[index] * 1000);
177}
178
179static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
180 char *buf)
181{
182 int index = to_sensor_dev_attr(attr)->index;
183 struct max1668_data *data = max1668_update_device(dev);
184
185 if (IS_ERR(data))
186 return PTR_ERR(data);
187
188 return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
189}
190
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700191static ssize_t show_fault(struct device *dev,
192 struct device_attribute *devattr, char *buf)
193{
194 int index = to_sensor_dev_attr(devattr)->index;
195 struct max1668_data *data = max1668_update_device(dev);
196
197 if (IS_ERR(data))
198 return PTR_ERR(data);
199
200 return sprintf(buf, "%u\n",
201 (data->alarms & (1 << 12)) && data->temp[index] == 127);
202}
203
David George731b4ca2011-06-02 08:43:45 -0700204static ssize_t set_temp_max(struct device *dev,
205 struct device_attribute *devattr,
206 const char *buf, size_t count)
207{
208 int index = to_sensor_dev_attr(devattr)->index;
Guenter Roeck52305512014-02-15 17:50:48 -0800209 struct max1668_data *data = dev_get_drvdata(dev);
210 struct i2c_client *client = data->client;
David George731b4ca2011-06-02 08:43:45 -0700211 long temp;
212 int ret;
213
214 ret = kstrtol(buf, 10, &temp);
215 if (ret < 0)
216 return ret;
217
218 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800219 data->temp_max[index] = clamp_val(temp/1000, -128, 127);
Guenter Roeckc5a70662014-02-15 17:59:05 -0800220 ret = i2c_smbus_write_byte_data(client,
David George731b4ca2011-06-02 08:43:45 -0700221 MAX1668_REG_LIMH_WR(index),
Guenter Roeckc5a70662014-02-15 17:59:05 -0800222 data->temp_max[index]);
223 if (ret < 0)
224 count = ret;
David George731b4ca2011-06-02 08:43:45 -0700225 mutex_unlock(&data->update_lock);
226
227 return count;
228}
229
230static ssize_t set_temp_min(struct device *dev,
231 struct device_attribute *devattr,
232 const char *buf, size_t count)
233{
234 int index = to_sensor_dev_attr(devattr)->index;
Guenter Roeck52305512014-02-15 17:50:48 -0800235 struct max1668_data *data = dev_get_drvdata(dev);
236 struct i2c_client *client = data->client;
David George731b4ca2011-06-02 08:43:45 -0700237 long temp;
238 int ret;
239
240 ret = kstrtol(buf, 10, &temp);
241 if (ret < 0)
242 return ret;
243
244 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800245 data->temp_min[index] = clamp_val(temp/1000, -128, 127);
Guenter Roeckc5a70662014-02-15 17:59:05 -0800246 ret = i2c_smbus_write_byte_data(client,
David George731b4ca2011-06-02 08:43:45 -0700247 MAX1668_REG_LIML_WR(index),
Guenter Roeckc5a70662014-02-15 17:59:05 -0800248 data->temp_min[index]);
249 if (ret < 0)
250 count = ret;
David George731b4ca2011-06-02 08:43:45 -0700251 mutex_unlock(&data->update_lock);
252
253 return count;
254}
255
256static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
257static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
258 set_temp_max, 0);
259static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
260 set_temp_min, 0);
261static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
262static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
263 set_temp_max, 1);
264static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
265 set_temp_min, 1);
266static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
267static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
268 set_temp_max, 2);
269static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
270 set_temp_min, 2);
271static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
272static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
273 set_temp_max, 3);
274static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
275 set_temp_min, 3);
276static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
277static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
278 set_temp_max, 4);
279static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
280 set_temp_min, 4);
281
282static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
283static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
284static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
285static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
286static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
287static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
288static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
289static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
290static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
291static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
292
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700293static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
294static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
295static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
296static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
297
David George731b4ca2011-06-02 08:43:45 -0700298/* Attributes common to MAX1668, MAX1989 and MAX1805 */
299static struct attribute *max1668_attribute_common[] = {
300 &sensor_dev_attr_temp1_max.dev_attr.attr,
301 &sensor_dev_attr_temp1_min.dev_attr.attr,
302 &sensor_dev_attr_temp1_input.dev_attr.attr,
303 &sensor_dev_attr_temp2_max.dev_attr.attr,
304 &sensor_dev_attr_temp2_min.dev_attr.attr,
305 &sensor_dev_attr_temp2_input.dev_attr.attr,
306 &sensor_dev_attr_temp3_max.dev_attr.attr,
307 &sensor_dev_attr_temp3_min.dev_attr.attr,
308 &sensor_dev_attr_temp3_input.dev_attr.attr,
309
310 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
311 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
312 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
313 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
314 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
315 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700316
317 &sensor_dev_attr_temp2_fault.dev_attr.attr,
318 &sensor_dev_attr_temp3_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700319 NULL
320};
321
322/* Attributes not present on MAX1805 */
323static struct attribute *max1668_attribute_unique[] = {
324 &sensor_dev_attr_temp4_max.dev_attr.attr,
325 &sensor_dev_attr_temp4_min.dev_attr.attr,
326 &sensor_dev_attr_temp4_input.dev_attr.attr,
327 &sensor_dev_attr_temp5_max.dev_attr.attr,
328 &sensor_dev_attr_temp5_min.dev_attr.attr,
329 &sensor_dev_attr_temp5_input.dev_attr.attr,
330
331 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
332 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
333 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
334 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700335
336 &sensor_dev_attr_temp4_fault.dev_attr.attr,
337 &sensor_dev_attr_temp5_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700338 NULL
339};
340
Al Viro587a1f12011-07-23 23:11:19 -0400341static umode_t max1668_attribute_mode(struct kobject *kobj,
David George731b4ca2011-06-02 08:43:45 -0700342 struct attribute *attr, int index)
343{
Al Viro587a1f12011-07-23 23:11:19 -0400344 umode_t ret = S_IRUGO;
David George731b4ca2011-06-02 08:43:45 -0700345 if (read_only)
346 return ret;
347 if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr ||
348 attr == &sensor_dev_attr_temp2_max.dev_attr.attr ||
349 attr == &sensor_dev_attr_temp3_max.dev_attr.attr ||
350 attr == &sensor_dev_attr_temp4_max.dev_attr.attr ||
351 attr == &sensor_dev_attr_temp5_max.dev_attr.attr ||
352 attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
353 attr == &sensor_dev_attr_temp2_min.dev_attr.attr ||
354 attr == &sensor_dev_attr_temp3_min.dev_attr.attr ||
355 attr == &sensor_dev_attr_temp4_min.dev_attr.attr ||
356 attr == &sensor_dev_attr_temp5_min.dev_attr.attr)
357 ret |= S_IWUSR;
358 return ret;
359}
360
361static const struct attribute_group max1668_group_common = {
362 .attrs = max1668_attribute_common,
363 .is_visible = max1668_attribute_mode
364};
365
366static const struct attribute_group max1668_group_unique = {
367 .attrs = max1668_attribute_unique,
368 .is_visible = max1668_attribute_mode
369};
370
371/* Return 0 if detection is successful, -ENODEV otherwise */
372static int max1668_detect(struct i2c_client *client,
373 struct i2c_board_info *info)
374{
375 struct i2c_adapter *adapter = client->adapter;
376 const char *type_name;
377 int man_id, dev_id;
378
379 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
380 return -ENODEV;
381
382 /* Check for unsupported part */
383 man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
384 if (man_id != MAN_ID_MAXIM)
385 return -ENODEV;
386
387 dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
388 if (dev_id < 0)
389 return -ENODEV;
390
391 type_name = NULL;
392 if (dev_id == DEV_ID_MAX1668)
393 type_name = "max1668";
394 else if (dev_id == DEV_ID_MAX1805)
395 type_name = "max1805";
396 else if (dev_id == DEV_ID_MAX1989)
397 type_name = "max1989";
398
399 if (!type_name)
400 return -ENODEV;
401
402 strlcpy(info->type, type_name, I2C_NAME_SIZE);
403
404 return 0;
405}
406
407static int max1668_probe(struct i2c_client *client,
408 const struct i2c_device_id *id)
409{
410 struct i2c_adapter *adapter = client->adapter;
Guenter Roeck52305512014-02-15 17:50:48 -0800411 struct device *dev = &client->dev;
412 struct device *hwmon_dev;
David George731b4ca2011-06-02 08:43:45 -0700413 struct max1668_data *data;
David George731b4ca2011-06-02 08:43:45 -0700414
415 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
416 return -ENODEV;
417
Guenter Roeck52305512014-02-15 17:50:48 -0800418 data = devm_kzalloc(dev, sizeof(struct max1668_data), GFP_KERNEL);
David George731b4ca2011-06-02 08:43:45 -0700419 if (!data)
420 return -ENOMEM;
421
Guenter Roeck52305512014-02-15 17:50:48 -0800422 data->client = client;
David George731b4ca2011-06-02 08:43:45 -0700423 data->type = id->driver_data;
424 mutex_init(&data->update_lock);
425
Guenter Roeck52305512014-02-15 17:50:48 -0800426 /* sysfs hooks */
427 data->groups[0] = &max1668_group_common;
David George731b4ca2011-06-02 08:43:45 -0700428 if (data->type == max1668 || data->type == max1989)
Guenter Roeck52305512014-02-15 17:50:48 -0800429 data->groups[1] = &max1668_group_unique;
David George731b4ca2011-06-02 08:43:45 -0700430
Guenter Roeck52305512014-02-15 17:50:48 -0800431 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
432 data, data->groups);
433 return PTR_ERR_OR_ZERO(hwmon_dev);
David George731b4ca2011-06-02 08:43:45 -0700434}
435
436static const struct i2c_device_id max1668_id[] = {
437 { "max1668", max1668 },
438 { "max1805", max1805 },
439 { "max1989", max1989 },
440 { }
441};
442MODULE_DEVICE_TABLE(i2c, max1668_id);
443
444/* This is the driver that will be inserted */
445static struct i2c_driver max1668_driver = {
446 .class = I2C_CLASS_HWMON,
447 .driver = {
448 .name = "max1668",
449 },
450 .probe = max1668_probe,
David George731b4ca2011-06-02 08:43:45 -0700451 .id_table = max1668_id,
452 .detect = max1668_detect,
453 .address_list = max1668_addr_list,
454};
455
Axel Linf0967ee2012-01-20 15:38:18 +0800456module_i2c_driver(max1668_driver);
David George731b4ca2011-06-02 08:43:45 -0700457
458MODULE_AUTHOR("David George <david.george@ska.ac.za>");
459MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
460MODULE_LICENSE("GPL");