blob: 78688e6cb87d99be3094df1933c52b675e421706 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David George731b4ca2011-06-02 08:43:45 -07002/*
Guenter Roeckb6707b72012-01-19 11:02:22 -08003 * Copyright (c) 2011 David George <david.george@ska.ac.za>
4 *
5 * based on adm1021.c
6 * some credit to Christoph Scheurer, but largely a rewrite
Guenter Roeckb6707b72012-01-19 11:02:22 -08007 */
David George731b4ca2011-06-02 08:43:45 -07008
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/jiffies.h>
13#include <linux/i2c.h>
14#include <linux/hwmon.h>
15#include <linux/hwmon-sysfs.h>
16#include <linux/err.h>
17#include <linux/mutex.h>
18
19/* Addresses to scan */
Axel Lin28681d72014-07-18 16:02:38 +080020static const unsigned short max1668_addr_list[] = {
David George731b4ca2011-06-02 08:43:45 -070021 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
22
23/* max1668 registers */
24
25#define MAX1668_REG_TEMP(nr) (nr)
26#define MAX1668_REG_STAT1 0x05
27#define MAX1668_REG_STAT2 0x06
28#define MAX1668_REG_MAN_ID 0xfe
29#define MAX1668_REG_DEV_ID 0xff
30
31/* limits */
32
33/* write high limits */
34#define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr))
35/* write low limits */
36#define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr))
37/* read high limits */
38#define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr))
39/* read low limits */
40#define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr))
41
42/* manufacturer and device ID Constants */
43#define MAN_ID_MAXIM 0x4d
44#define DEV_ID_MAX1668 0x3
45#define DEV_ID_MAX1805 0x5
46#define DEV_ID_MAX1989 0xb
47
48/* read only mode module parameter */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103049static bool read_only;
David George731b4ca2011-06-02 08:43:45 -070050module_param(read_only, bool, 0);
51MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
52
53enum chips { max1668, max1805, max1989 };
54
55struct max1668_data {
Guenter Roeck52305512014-02-15 17:50:48 -080056 struct i2c_client *client;
57 const struct attribute_group *groups[3];
David George731b4ca2011-06-02 08:43:45 -070058 enum chips type;
59
60 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +030061 bool valid; /* true if following fields are valid */
David George731b4ca2011-06-02 08:43:45 -070062 unsigned long last_updated; /* In jiffies */
63
64 /* 1x local and 4x remote */
65 s8 temp_max[5];
66 s8 temp_min[5];
67 s8 temp[5];
68 u16 alarms;
69};
70
71static struct max1668_data *max1668_update_device(struct device *dev)
72{
Guenter Roeck52305512014-02-15 17:50:48 -080073 struct max1668_data *data = dev_get_drvdata(dev);
74 struct i2c_client *client = data->client;
David George731b4ca2011-06-02 08:43:45 -070075 struct max1668_data *ret = data;
76 s32 val;
77 int i;
78
79 mutex_lock(&data->update_lock);
80
81 if (data->valid && !time_after(jiffies,
82 data->last_updated + HZ + HZ / 2))
83 goto abort;
84
85 for (i = 0; i < 5; i++) {
86 val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i));
87 if (unlikely(val < 0)) {
88 ret = ERR_PTR(val);
89 goto abort;
90 }
91 data->temp[i] = (s8) val;
92
93 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i));
94 if (unlikely(val < 0)) {
95 ret = ERR_PTR(val);
96 goto abort;
97 }
98 data->temp_max[i] = (s8) val;
99
100 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i));
101 if (unlikely(val < 0)) {
102 ret = ERR_PTR(val);
103 goto abort;
104 }
105 data->temp_min[i] = (s8) val;
106 }
107
108 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1);
109 if (unlikely(val < 0)) {
110 ret = ERR_PTR(val);
111 goto abort;
112 }
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700113 data->alarms = val << 8;
David George731b4ca2011-06-02 08:43:45 -0700114
115 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2);
116 if (unlikely(val < 0)) {
117 ret = ERR_PTR(val);
118 goto abort;
119 }
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700120 data->alarms |= val;
David George731b4ca2011-06-02 08:43:45 -0700121
122 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +0300123 data->valid = true;
David George731b4ca2011-06-02 08:43:45 -0700124abort:
125 mutex_unlock(&data->update_lock);
126
127 return ret;
128}
129
130static ssize_t show_temp(struct device *dev,
131 struct device_attribute *devattr, char *buf)
132{
133 int index = to_sensor_dev_attr(devattr)->index;
134 struct max1668_data *data = max1668_update_device(dev);
135
136 if (IS_ERR(data))
137 return PTR_ERR(data);
138
139 return sprintf(buf, "%d\n", data->temp[index] * 1000);
140}
141
142static ssize_t show_temp_max(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_max[index] * 1000);
152}
153
154static ssize_t show_temp_min(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_min[index] * 1000);
164}
165
166static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
167 char *buf)
168{
169 int index = to_sensor_dev_attr(attr)->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, "%u\n", (data->alarms >> index) & 0x1);
176}
177
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700178static ssize_t show_fault(struct device *dev,
179 struct device_attribute *devattr, char *buf)
180{
181 int index = to_sensor_dev_attr(devattr)->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",
188 (data->alarms & (1 << 12)) && data->temp[index] == 127);
189}
190
David George731b4ca2011-06-02 08:43:45 -0700191static ssize_t set_temp_max(struct device *dev,
192 struct device_attribute *devattr,
193 const char *buf, size_t count)
194{
195 int index = to_sensor_dev_attr(devattr)->index;
Guenter Roeck52305512014-02-15 17:50:48 -0800196 struct max1668_data *data = dev_get_drvdata(dev);
197 struct i2c_client *client = data->client;
David George731b4ca2011-06-02 08:43:45 -0700198 long temp;
199 int ret;
200
201 ret = kstrtol(buf, 10, &temp);
202 if (ret < 0)
203 return ret;
204
205 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800206 data->temp_max[index] = clamp_val(temp/1000, -128, 127);
Guenter Roeckc5a70662014-02-15 17:59:05 -0800207 ret = i2c_smbus_write_byte_data(client,
David George731b4ca2011-06-02 08:43:45 -0700208 MAX1668_REG_LIMH_WR(index),
Guenter Roeckc5a70662014-02-15 17:59:05 -0800209 data->temp_max[index]);
210 if (ret < 0)
211 count = ret;
David George731b4ca2011-06-02 08:43:45 -0700212 mutex_unlock(&data->update_lock);
213
214 return count;
215}
216
217static ssize_t set_temp_min(struct device *dev,
218 struct device_attribute *devattr,
219 const char *buf, size_t count)
220{
221 int index = to_sensor_dev_attr(devattr)->index;
Guenter Roeck52305512014-02-15 17:50:48 -0800222 struct max1668_data *data = dev_get_drvdata(dev);
223 struct i2c_client *client = data->client;
David George731b4ca2011-06-02 08:43:45 -0700224 long temp;
225 int ret;
226
227 ret = kstrtol(buf, 10, &temp);
228 if (ret < 0)
229 return ret;
230
231 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800232 data->temp_min[index] = clamp_val(temp/1000, -128, 127);
Guenter Roeckc5a70662014-02-15 17:59:05 -0800233 ret = i2c_smbus_write_byte_data(client,
David George731b4ca2011-06-02 08:43:45 -0700234 MAX1668_REG_LIML_WR(index),
Guenter Roeckc5a70662014-02-15 17:59:05 -0800235 data->temp_min[index]);
236 if (ret < 0)
237 count = ret;
David George731b4ca2011-06-02 08:43:45 -0700238 mutex_unlock(&data->update_lock);
239
240 return count;
241}
242
243static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
244static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
245 set_temp_max, 0);
246static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
247 set_temp_min, 0);
248static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
249static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
250 set_temp_max, 1);
251static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
252 set_temp_min, 1);
253static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
254static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
255 set_temp_max, 2);
256static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
257 set_temp_min, 2);
258static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
259static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
260 set_temp_max, 3);
261static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
262 set_temp_min, 3);
263static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
264static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
265 set_temp_max, 4);
266static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
267 set_temp_min, 4);
268
269static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
270static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
271static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
272static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
273static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
274static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
275static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
276static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
277static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
278static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
279
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700280static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
281static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
282static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
283static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
284
David George731b4ca2011-06-02 08:43:45 -0700285/* Attributes common to MAX1668, MAX1989 and MAX1805 */
286static struct attribute *max1668_attribute_common[] = {
287 &sensor_dev_attr_temp1_max.dev_attr.attr,
288 &sensor_dev_attr_temp1_min.dev_attr.attr,
289 &sensor_dev_attr_temp1_input.dev_attr.attr,
290 &sensor_dev_attr_temp2_max.dev_attr.attr,
291 &sensor_dev_attr_temp2_min.dev_attr.attr,
292 &sensor_dev_attr_temp2_input.dev_attr.attr,
293 &sensor_dev_attr_temp3_max.dev_attr.attr,
294 &sensor_dev_attr_temp3_min.dev_attr.attr,
295 &sensor_dev_attr_temp3_input.dev_attr.attr,
296
297 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
298 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
299 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
300 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
301 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
302 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700303
304 &sensor_dev_attr_temp2_fault.dev_attr.attr,
305 &sensor_dev_attr_temp3_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700306 NULL
307};
308
309/* Attributes not present on MAX1805 */
310static struct attribute *max1668_attribute_unique[] = {
311 &sensor_dev_attr_temp4_max.dev_attr.attr,
312 &sensor_dev_attr_temp4_min.dev_attr.attr,
313 &sensor_dev_attr_temp4_input.dev_attr.attr,
314 &sensor_dev_attr_temp5_max.dev_attr.attr,
315 &sensor_dev_attr_temp5_min.dev_attr.attr,
316 &sensor_dev_attr_temp5_input.dev_attr.attr,
317
318 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
319 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
320 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
321 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
Guenter Roeckdabaa0d2011-06-09 18:05:16 -0700322
323 &sensor_dev_attr_temp4_fault.dev_attr.attr,
324 &sensor_dev_attr_temp5_fault.dev_attr.attr,
David George731b4ca2011-06-02 08:43:45 -0700325 NULL
326};
327
Al Viro587a1f12011-07-23 23:11:19 -0400328static umode_t max1668_attribute_mode(struct kobject *kobj,
David George731b4ca2011-06-02 08:43:45 -0700329 struct attribute *attr, int index)
330{
Al Viro587a1f12011-07-23 23:11:19 -0400331 umode_t ret = S_IRUGO;
David George731b4ca2011-06-02 08:43:45 -0700332 if (read_only)
333 return ret;
334 if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr ||
335 attr == &sensor_dev_attr_temp2_max.dev_attr.attr ||
336 attr == &sensor_dev_attr_temp3_max.dev_attr.attr ||
337 attr == &sensor_dev_attr_temp4_max.dev_attr.attr ||
338 attr == &sensor_dev_attr_temp5_max.dev_attr.attr ||
339 attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
340 attr == &sensor_dev_attr_temp2_min.dev_attr.attr ||
341 attr == &sensor_dev_attr_temp3_min.dev_attr.attr ||
342 attr == &sensor_dev_attr_temp4_min.dev_attr.attr ||
343 attr == &sensor_dev_attr_temp5_min.dev_attr.attr)
344 ret |= S_IWUSR;
345 return ret;
346}
347
348static const struct attribute_group max1668_group_common = {
349 .attrs = max1668_attribute_common,
350 .is_visible = max1668_attribute_mode
351};
352
353static const struct attribute_group max1668_group_unique = {
354 .attrs = max1668_attribute_unique,
355 .is_visible = max1668_attribute_mode
356};
357
358/* Return 0 if detection is successful, -ENODEV otherwise */
359static int max1668_detect(struct i2c_client *client,
360 struct i2c_board_info *info)
361{
362 struct i2c_adapter *adapter = client->adapter;
363 const char *type_name;
364 int man_id, dev_id;
365
366 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
367 return -ENODEV;
368
369 /* Check for unsupported part */
370 man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
371 if (man_id != MAN_ID_MAXIM)
372 return -ENODEV;
373
374 dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
375 if (dev_id < 0)
376 return -ENODEV;
377
378 type_name = NULL;
379 if (dev_id == DEV_ID_MAX1668)
380 type_name = "max1668";
381 else if (dev_id == DEV_ID_MAX1805)
382 type_name = "max1805";
383 else if (dev_id == DEV_ID_MAX1989)
384 type_name = "max1989";
385
386 if (!type_name)
387 return -ENODEV;
388
389 strlcpy(info->type, type_name, I2C_NAME_SIZE);
390
391 return 0;
392}
393
Stephen Kitt67487032020-08-13 18:02:22 +0200394static const struct i2c_device_id max1668_id[];
395
396static int max1668_probe(struct i2c_client *client)
David George731b4ca2011-06-02 08:43:45 -0700397{
398 struct i2c_adapter *adapter = client->adapter;
Guenter Roeck52305512014-02-15 17:50:48 -0800399 struct device *dev = &client->dev;
400 struct device *hwmon_dev;
David George731b4ca2011-06-02 08:43:45 -0700401 struct max1668_data *data;
David George731b4ca2011-06-02 08:43:45 -0700402
403 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
404 return -ENODEV;
405
Guenter Roeck52305512014-02-15 17:50:48 -0800406 data = devm_kzalloc(dev, sizeof(struct max1668_data), GFP_KERNEL);
David George731b4ca2011-06-02 08:43:45 -0700407 if (!data)
408 return -ENOMEM;
409
Guenter Roeck52305512014-02-15 17:50:48 -0800410 data->client = client;
Stephen Kitt67487032020-08-13 18:02:22 +0200411 data->type = i2c_match_id(max1668_id, client)->driver_data;
David George731b4ca2011-06-02 08:43:45 -0700412 mutex_init(&data->update_lock);
413
Guenter Roeck52305512014-02-15 17:50:48 -0800414 /* sysfs hooks */
415 data->groups[0] = &max1668_group_common;
David George731b4ca2011-06-02 08:43:45 -0700416 if (data->type == max1668 || data->type == max1989)
Guenter Roeck52305512014-02-15 17:50:48 -0800417 data->groups[1] = &max1668_group_unique;
David George731b4ca2011-06-02 08:43:45 -0700418
Guenter Roeck52305512014-02-15 17:50:48 -0800419 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
420 data, data->groups);
421 return PTR_ERR_OR_ZERO(hwmon_dev);
David George731b4ca2011-06-02 08:43:45 -0700422}
423
424static const struct i2c_device_id max1668_id[] = {
425 { "max1668", max1668 },
426 { "max1805", max1805 },
427 { "max1989", max1989 },
428 { }
429};
430MODULE_DEVICE_TABLE(i2c, max1668_id);
431
432/* This is the driver that will be inserted */
433static struct i2c_driver max1668_driver = {
434 .class = I2C_CLASS_HWMON,
435 .driver = {
436 .name = "max1668",
437 },
Stephen Kitt67487032020-08-13 18:02:22 +0200438 .probe_new = max1668_probe,
David George731b4ca2011-06-02 08:43:45 -0700439 .id_table = max1668_id,
440 .detect = max1668_detect,
441 .address_list = max1668_addr_list,
442};
443
Axel Linf0967ee2012-01-20 15:38:18 +0800444module_i2c_driver(max1668_driver);
David George731b4ca2011-06-02 08:43:45 -0700445
446MODULE_AUTHOR("David George <david.george@ska.ac.za>");
447MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
448MODULE_LICENSE("GPL");