1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
33static unsigned short max1668_addr_list[] = {
34 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
35
36
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
45
46
47#define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr))
48
49#define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr))
50
51#define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr))
52
53#define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr))
54
55
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
62static bool read_only;
63module_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;
74 unsigned long last_updated;
75
76
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 }
125 data->alarms = val << 8;
126
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 }
132 data->alarms |= val;
133
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
190static 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
203static 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] = clamp_val(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] = clamp_val(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
290static 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
295
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,
313
314 &sensor_dev_attr_temp2_fault.dev_attr.attr,
315 &sensor_dev_attr_temp3_fault.dev_attr.attr,
316 NULL
317};
318
319
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,
332
333 &sensor_dev_attr_temp4_fault.dev_attr.attr,
334 &sensor_dev_attr_temp5_fault.dev_attr.attr,
335 NULL
336};
337
338static umode_t max1668_attribute_mode(struct kobject *kobj,
339 struct attribute *attr, int index)
340{
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;
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
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
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
414 data = devm_kzalloc(&client->dev, sizeof(struct max1668_data),
415 GFP_KERNEL);
416 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
424 err = sysfs_create_group(&client->dev.kobj, &max1668_group_common);
425 if (err)
426 return err;
427
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);
448 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
461 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
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
485module_i2c_driver(max1668_driver);
486
487MODULE_AUTHOR("David George <david.george@ska.ac.za>");
488MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
489MODULE_LICENSE("GPL");
490