1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 David George <david.george@ska.ac.za>
6 * some credit to Christoph Scheurer, but largely a rewrite
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>
19 /* Addresses to scan */
20 static const unsigned short max1668_addr_list
[] = {
21 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END
};
23 /* max1668 registers */
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
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))
40 #define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr))
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
48 /* read only mode module parameter */
49 static bool read_only
;
50 module_param(read_only
, bool, 0);
51 MODULE_PARM_DESC(read_only
, "Don't set any values, read only mode");
53 enum chips
{ max1668
, max1805
, max1989
};
56 struct i2c_client
*client
;
57 const struct attribute_group
*groups
[3];
60 struct mutex update_lock
;
61 char valid
; /* !=0 if following fields are valid */
62 unsigned long last_updated
; /* In jiffies */
64 /* 1x local and 4x remote */
71 static struct max1668_data
*max1668_update_device(struct device
*dev
)
73 struct max1668_data
*data
= dev_get_drvdata(dev
);
74 struct i2c_client
*client
= data
->client
;
75 struct max1668_data
*ret
= data
;
79 mutex_lock(&data
->update_lock
);
81 if (data
->valid
&& !time_after(jiffies
,
82 data
->last_updated
+ HZ
+ HZ
/ 2))
85 for (i
= 0; i
< 5; i
++) {
86 val
= i2c_smbus_read_byte_data(client
, MAX1668_REG_TEMP(i
));
87 if (unlikely(val
< 0)) {
91 data
->temp
[i
] = (s8
) val
;
93 val
= i2c_smbus_read_byte_data(client
, MAX1668_REG_LIMH_RD(i
));
94 if (unlikely(val
< 0)) {
98 data
->temp_max
[i
] = (s8
) val
;
100 val
= i2c_smbus_read_byte_data(client
, MAX1668_REG_LIML_RD(i
));
101 if (unlikely(val
< 0)) {
105 data
->temp_min
[i
] = (s8
) val
;
108 val
= i2c_smbus_read_byte_data(client
, MAX1668_REG_STAT1
);
109 if (unlikely(val
< 0)) {
113 data
->alarms
= val
<< 8;
115 val
= i2c_smbus_read_byte_data(client
, MAX1668_REG_STAT2
);
116 if (unlikely(val
< 0)) {
122 data
->last_updated
= jiffies
;
125 mutex_unlock(&data
->update_lock
);
130 static ssize_t
show_temp(struct device
*dev
,
131 struct device_attribute
*devattr
, char *buf
)
133 int index
= to_sensor_dev_attr(devattr
)->index
;
134 struct max1668_data
*data
= max1668_update_device(dev
);
137 return PTR_ERR(data
);
139 return sprintf(buf
, "%d\n", data
->temp
[index
] * 1000);
142 static ssize_t
show_temp_max(struct device
*dev
,
143 struct device_attribute
*devattr
, char *buf
)
145 int index
= to_sensor_dev_attr(devattr
)->index
;
146 struct max1668_data
*data
= max1668_update_device(dev
);
149 return PTR_ERR(data
);
151 return sprintf(buf
, "%d\n", data
->temp_max
[index
] * 1000);
154 static ssize_t
show_temp_min(struct device
*dev
,
155 struct device_attribute
*devattr
, char *buf
)
157 int index
= to_sensor_dev_attr(devattr
)->index
;
158 struct max1668_data
*data
= max1668_update_device(dev
);
161 return PTR_ERR(data
);
163 return sprintf(buf
, "%d\n", data
->temp_min
[index
] * 1000);
166 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
169 int index
= to_sensor_dev_attr(attr
)->index
;
170 struct max1668_data
*data
= max1668_update_device(dev
);
173 return PTR_ERR(data
);
175 return sprintf(buf
, "%u\n", (data
->alarms
>> index
) & 0x1);
178 static ssize_t
show_fault(struct device
*dev
,
179 struct device_attribute
*devattr
, char *buf
)
181 int index
= to_sensor_dev_attr(devattr
)->index
;
182 struct max1668_data
*data
= max1668_update_device(dev
);
185 return PTR_ERR(data
);
187 return sprintf(buf
, "%u\n",
188 (data
->alarms
& (1 << 12)) && data
->temp
[index
] == 127);
191 static ssize_t
set_temp_max(struct device
*dev
,
192 struct device_attribute
*devattr
,
193 const char *buf
, size_t count
)
195 int index
= to_sensor_dev_attr(devattr
)->index
;
196 struct max1668_data
*data
= dev_get_drvdata(dev
);
197 struct i2c_client
*client
= data
->client
;
201 ret
= kstrtol(buf
, 10, &temp
);
205 mutex_lock(&data
->update_lock
);
206 data
->temp_max
[index
] = clamp_val(temp
/1000, -128, 127);
207 ret
= i2c_smbus_write_byte_data(client
,
208 MAX1668_REG_LIMH_WR(index
),
209 data
->temp_max
[index
]);
212 mutex_unlock(&data
->update_lock
);
217 static ssize_t
set_temp_min(struct device
*dev
,
218 struct device_attribute
*devattr
,
219 const char *buf
, size_t count
)
221 int index
= to_sensor_dev_attr(devattr
)->index
;
222 struct max1668_data
*data
= dev_get_drvdata(dev
);
223 struct i2c_client
*client
= data
->client
;
227 ret
= kstrtol(buf
, 10, &temp
);
231 mutex_lock(&data
->update_lock
);
232 data
->temp_min
[index
] = clamp_val(temp
/1000, -128, 127);
233 ret
= i2c_smbus_write_byte_data(client
,
234 MAX1668_REG_LIML_WR(index
),
235 data
->temp_min
[index
]);
238 mutex_unlock(&data
->update_lock
);
243 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0);
244 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
, show_temp_max
,
246 static SENSOR_DEVICE_ATTR(temp1_min
, S_IRUGO
, show_temp_min
,
248 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp
, NULL
, 1);
249 static SENSOR_DEVICE_ATTR(temp2_max
, S_IRUGO
, show_temp_max
,
251 static SENSOR_DEVICE_ATTR(temp2_min
, S_IRUGO
, show_temp_min
,
253 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
, show_temp
, NULL
, 2);
254 static SENSOR_DEVICE_ATTR(temp3_max
, S_IRUGO
, show_temp_max
,
256 static SENSOR_DEVICE_ATTR(temp3_min
, S_IRUGO
, show_temp_min
,
258 static SENSOR_DEVICE_ATTR(temp4_input
, S_IRUGO
, show_temp
, NULL
, 3);
259 static SENSOR_DEVICE_ATTR(temp4_max
, S_IRUGO
, show_temp_max
,
261 static SENSOR_DEVICE_ATTR(temp4_min
, S_IRUGO
, show_temp_min
,
263 static SENSOR_DEVICE_ATTR(temp5_input
, S_IRUGO
, show_temp
, NULL
, 4);
264 static SENSOR_DEVICE_ATTR(temp5_max
, S_IRUGO
, show_temp_max
,
266 static SENSOR_DEVICE_ATTR(temp5_min
, S_IRUGO
, show_temp_min
,
269 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
270 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
271 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 7);
272 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
273 static SENSOR_DEVICE_ATTR(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
274 static SENSOR_DEVICE_ATTR(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
275 static SENSOR_DEVICE_ATTR(temp4_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
276 static SENSOR_DEVICE_ATTR(temp4_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
277 static SENSOR_DEVICE_ATTR(temp5_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
278 static SENSOR_DEVICE_ATTR(temp5_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
280 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_fault
, NULL
, 1);
281 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_fault
, NULL
, 2);
282 static SENSOR_DEVICE_ATTR(temp4_fault
, S_IRUGO
, show_fault
, NULL
, 3);
283 static SENSOR_DEVICE_ATTR(temp5_fault
, S_IRUGO
, show_fault
, NULL
, 4);
285 /* Attributes common to MAX1668, MAX1989 and MAX1805 */
286 static 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
,
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
,
304 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
305 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
309 /* Attributes not present on MAX1805 */
310 static 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
,
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
,
323 &sensor_dev_attr_temp4_fault
.dev_attr
.attr
,
324 &sensor_dev_attr_temp5_fault
.dev_attr
.attr
,
328 static umode_t
max1668_attribute_mode(struct kobject
*kobj
,
329 struct attribute
*attr
, int index
)
331 umode_t ret
= S_IRUGO
;
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
)
348 static const struct attribute_group max1668_group_common
= {
349 .attrs
= max1668_attribute_common
,
350 .is_visible
= max1668_attribute_mode
353 static const struct attribute_group max1668_group_unique
= {
354 .attrs
= max1668_attribute_unique
,
355 .is_visible
= max1668_attribute_mode
358 /* Return 0 if detection is successful, -ENODEV otherwise */
359 static int max1668_detect(struct i2c_client
*client
,
360 struct i2c_board_info
*info
)
362 struct i2c_adapter
*adapter
= client
->adapter
;
363 const char *type_name
;
366 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
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
)
374 dev_id
= i2c_smbus_read_byte_data(client
, MAX1668_REG_DEV_ID
);
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";
389 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
394 static const struct i2c_device_id max1668_id
[];
396 static int max1668_probe(struct i2c_client
*client
)
398 struct i2c_adapter
*adapter
= client
->adapter
;
399 struct device
*dev
= &client
->dev
;
400 struct device
*hwmon_dev
;
401 struct max1668_data
*data
;
403 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
406 data
= devm_kzalloc(dev
, sizeof(struct max1668_data
), GFP_KERNEL
);
410 data
->client
= client
;
411 data
->type
= i2c_match_id(max1668_id
, client
)->driver_data
;
412 mutex_init(&data
->update_lock
);
415 data
->groups
[0] = &max1668_group_common
;
416 if (data
->type
== max1668
|| data
->type
== max1989
)
417 data
->groups
[1] = &max1668_group_unique
;
419 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
421 return PTR_ERR_OR_ZERO(hwmon_dev
);
424 static const struct i2c_device_id max1668_id
[] = {
425 { "max1668", max1668
},
426 { "max1805", max1805
},
427 { "max1989", max1989
},
430 MODULE_DEVICE_TABLE(i2c
, max1668_id
);
432 /* This is the driver that will be inserted */
433 static struct i2c_driver max1668_driver
= {
434 .class = I2C_CLASS_HWMON
,
438 .probe_new
= max1668_probe
,
439 .id_table
= max1668_id
,
440 .detect
= max1668_detect
,
441 .address_list
= max1668_addr_list
,
444 module_i2c_driver(max1668_driver
);
446 MODULE_AUTHOR("David George <david.george@ska.ac.za>");
447 MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
448 MODULE_LICENSE("GPL");