Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / max6697.c
blob6df28fe0577dac0f42732c7f82d7cd7ea4425da2
1 /*
2 * Copyright (c) 2012 Guenter Roeck <linux@roeck-us.net>
4 * based on max1668.c
5 * Copyright (c) 2011 David George <david.george@ska.ac.za>
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.
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.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/jiffies.h>
22 #include <linux/i2c.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/err.h>
26 #include <linux/mutex.h>
27 #include <linux/of_device.h>
28 #include <linux/of.h>
30 #include <linux/platform_data/max6697.h>
32 enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694,
33 max6697, max6698, max6699 };
35 /* Report local sensor as temp1 */
37 static const u8 MAX6697_REG_TEMP[] = {
38 0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08 };
39 static const u8 MAX6697_REG_TEMP_EXT[] = {
40 0x57, 0x09, 0x52, 0x53, 0x54, 0x55, 0x56, 0 };
41 static const u8 MAX6697_REG_MAX[] = {
42 0x17, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18 };
43 static const u8 MAX6697_REG_CRIT[] = {
44 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
47 * Map device tree / platform data register bit map to chip bit map.
48 * Applies to alert register and over-temperature register.
50 #define MAX6697_ALERT_MAP_BITS(reg) ((((reg) & 0x7e) >> 1) | \
51 (((reg) & 0x01) << 6) | ((reg) & 0x80))
52 #define MAX6697_OVERT_MAP_BITS(reg) (((reg) >> 1) | (((reg) & 0x01) << 7))
54 #define MAX6697_REG_STAT(n) (0x44 + (n))
56 #define MAX6697_REG_CONFIG 0x41
57 #define MAX6581_CONF_EXTENDED (1 << 1)
58 #define MAX6693_CONF_BETA (1 << 2)
59 #define MAX6697_CONF_RESISTANCE (1 << 3)
60 #define MAX6697_CONF_TIMEOUT (1 << 5)
61 #define MAX6697_REG_ALERT_MASK 0x42
62 #define MAX6697_REG_OVERT_MASK 0x43
64 #define MAX6581_REG_RESISTANCE 0x4a
65 #define MAX6581_REG_IDEALITY 0x4b
66 #define MAX6581_REG_IDEALITY_SELECT 0x4c
67 #define MAX6581_REG_OFFSET 0x4d
68 #define MAX6581_REG_OFFSET_SELECT 0x4e
70 #define MAX6697_CONV_TIME 156 /* ms per channel, worst case */
72 struct max6697_chip_data {
73 int channels;
74 u32 have_ext;
75 u32 have_crit;
76 u32 have_fault;
77 u8 valid_conf;
78 const u8 *alarm_map;
81 struct max6697_data {
82 struct i2c_client *client;
84 enum chips type;
85 const struct max6697_chip_data *chip;
87 int update_interval; /* in milli-seconds */
88 int temp_offset; /* in degrees C */
90 struct mutex update_lock;
91 unsigned long last_updated; /* In jiffies */
92 bool valid; /* true if following fields are valid */
94 /* 1x local and up to 7x remote */
95 u8 temp[8][4]; /* [nr][0]=temp [1]=ext [2]=max [3]=crit */
96 #define MAX6697_TEMP_INPUT 0
97 #define MAX6697_TEMP_EXT 1
98 #define MAX6697_TEMP_MAX 2
99 #define MAX6697_TEMP_CRIT 3
100 u32 alarms;
103 /* Diode fault status bits on MAX6581 are right shifted by one bit */
104 static const u8 max6581_alarm_map[] = {
105 0, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15,
106 16, 17, 18, 19, 20, 21, 22, 23 };
108 static const struct max6697_chip_data max6697_chip_data[] = {
109 [max6581] = {
110 .channels = 8,
111 .have_crit = 0xff,
112 .have_ext = 0x7f,
113 .have_fault = 0xfe,
114 .valid_conf = MAX6581_CONF_EXTENDED | MAX6697_CONF_TIMEOUT,
115 .alarm_map = max6581_alarm_map,
117 [max6602] = {
118 .channels = 5,
119 .have_crit = 0x12,
120 .have_ext = 0x02,
121 .have_fault = 0x1e,
122 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
124 [max6622] = {
125 .channels = 5,
126 .have_crit = 0x12,
127 .have_ext = 0x02,
128 .have_fault = 0x1e,
129 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
131 [max6636] = {
132 .channels = 7,
133 .have_crit = 0x72,
134 .have_ext = 0x02,
135 .have_fault = 0x7e,
136 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
138 [max6689] = {
139 .channels = 7,
140 .have_crit = 0x72,
141 .have_ext = 0x02,
142 .have_fault = 0x7e,
143 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
145 [max6693] = {
146 .channels = 7,
147 .have_crit = 0x72,
148 .have_ext = 0x02,
149 .have_fault = 0x7e,
150 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA |
151 MAX6697_CONF_TIMEOUT,
153 [max6694] = {
154 .channels = 5,
155 .have_crit = 0x12,
156 .have_ext = 0x02,
157 .have_fault = 0x1e,
158 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA |
159 MAX6697_CONF_TIMEOUT,
161 [max6697] = {
162 .channels = 7,
163 .have_crit = 0x72,
164 .have_ext = 0x02,
165 .have_fault = 0x7e,
166 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
168 [max6698] = {
169 .channels = 7,
170 .have_crit = 0x72,
171 .have_ext = 0x02,
172 .have_fault = 0x0e,
173 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
175 [max6699] = {
176 .channels = 5,
177 .have_crit = 0x12,
178 .have_ext = 0x02,
179 .have_fault = 0x1e,
180 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
184 static struct max6697_data *max6697_update_device(struct device *dev)
186 struct max6697_data *data = dev_get_drvdata(dev);
187 struct i2c_client *client = data->client;
188 struct max6697_data *ret = data;
189 int val;
190 int i;
191 u32 alarms;
193 mutex_lock(&data->update_lock);
195 if (data->valid &&
196 !time_after(jiffies, data->last_updated
197 + msecs_to_jiffies(data->update_interval)))
198 goto abort;
200 for (i = 0; i < data->chip->channels; i++) {
201 if (data->chip->have_ext & (1 << i)) {
202 val = i2c_smbus_read_byte_data(client,
203 MAX6697_REG_TEMP_EXT[i]);
204 if (unlikely(val < 0)) {
205 ret = ERR_PTR(val);
206 goto abort;
208 data->temp[i][MAX6697_TEMP_EXT] = val;
211 val = i2c_smbus_read_byte_data(client, MAX6697_REG_TEMP[i]);
212 if (unlikely(val < 0)) {
213 ret = ERR_PTR(val);
214 goto abort;
216 data->temp[i][MAX6697_TEMP_INPUT] = val;
218 val = i2c_smbus_read_byte_data(client, MAX6697_REG_MAX[i]);
219 if (unlikely(val < 0)) {
220 ret = ERR_PTR(val);
221 goto abort;
223 data->temp[i][MAX6697_TEMP_MAX] = val;
225 if (data->chip->have_crit & (1 << i)) {
226 val = i2c_smbus_read_byte_data(client,
227 MAX6697_REG_CRIT[i]);
228 if (unlikely(val < 0)) {
229 ret = ERR_PTR(val);
230 goto abort;
232 data->temp[i][MAX6697_TEMP_CRIT] = val;
236 alarms = 0;
237 for (i = 0; i < 3; i++) {
238 val = i2c_smbus_read_byte_data(client, MAX6697_REG_STAT(i));
239 if (unlikely(val < 0)) {
240 ret = ERR_PTR(val);
241 goto abort;
243 alarms = (alarms << 8) | val;
245 data->alarms = alarms;
246 data->last_updated = jiffies;
247 data->valid = true;
248 abort:
249 mutex_unlock(&data->update_lock);
251 return ret;
254 static ssize_t show_temp_input(struct device *dev,
255 struct device_attribute *devattr, char *buf)
257 int index = to_sensor_dev_attr(devattr)->index;
258 struct max6697_data *data = max6697_update_device(dev);
259 int temp;
261 if (IS_ERR(data))
262 return PTR_ERR(data);
264 temp = (data->temp[index][MAX6697_TEMP_INPUT] - data->temp_offset) << 3;
265 temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5;
267 return sprintf(buf, "%d\n", temp * 125);
270 static ssize_t show_temp(struct device *dev,
271 struct device_attribute *devattr, char *buf)
273 int nr = to_sensor_dev_attr_2(devattr)->nr;
274 int index = to_sensor_dev_attr_2(devattr)->index;
275 struct max6697_data *data = max6697_update_device(dev);
276 int temp;
278 if (IS_ERR(data))
279 return PTR_ERR(data);
281 temp = data->temp[nr][index];
282 temp -= data->temp_offset;
284 return sprintf(buf, "%d\n", temp * 1000);
287 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
288 char *buf)
290 int index = to_sensor_dev_attr(attr)->index;
291 struct max6697_data *data = max6697_update_device(dev);
293 if (IS_ERR(data))
294 return PTR_ERR(data);
296 if (data->chip->alarm_map)
297 index = data->chip->alarm_map[index];
299 return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
302 static ssize_t set_temp(struct device *dev,
303 struct device_attribute *devattr,
304 const char *buf, size_t count)
306 int nr = to_sensor_dev_attr_2(devattr)->nr;
307 int index = to_sensor_dev_attr_2(devattr)->index;
308 struct max6697_data *data = dev_get_drvdata(dev);
309 long temp;
310 int ret;
312 ret = kstrtol(buf, 10, &temp);
313 if (ret < 0)
314 return ret;
316 mutex_lock(&data->update_lock);
317 temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset;
318 temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127);
319 data->temp[nr][index] = temp;
320 ret = i2c_smbus_write_byte_data(data->client,
321 index == 2 ? MAX6697_REG_MAX[nr]
322 : MAX6697_REG_CRIT[nr],
323 temp);
324 mutex_unlock(&data->update_lock);
326 return ret < 0 ? ret : count;
329 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
330 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
331 0, MAX6697_TEMP_MAX);
332 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
333 0, MAX6697_TEMP_CRIT);
335 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
336 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
337 1, MAX6697_TEMP_MAX);
338 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
339 1, MAX6697_TEMP_CRIT);
341 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2);
342 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
343 2, MAX6697_TEMP_MAX);
344 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
345 2, MAX6697_TEMP_CRIT);
347 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input, NULL, 3);
348 static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
349 3, MAX6697_TEMP_MAX);
350 static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
351 3, MAX6697_TEMP_CRIT);
353 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_input, NULL, 4);
354 static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
355 4, MAX6697_TEMP_MAX);
356 static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
357 4, MAX6697_TEMP_CRIT);
359 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_input, NULL, 5);
360 static SENSOR_DEVICE_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
361 5, MAX6697_TEMP_MAX);
362 static SENSOR_DEVICE_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
363 5, MAX6697_TEMP_CRIT);
365 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp_input, NULL, 6);
366 static SENSOR_DEVICE_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
367 6, MAX6697_TEMP_MAX);
368 static SENSOR_DEVICE_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
369 6, MAX6697_TEMP_CRIT);
371 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp_input, NULL, 7);
372 static SENSOR_DEVICE_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
373 7, MAX6697_TEMP_MAX);
374 static SENSOR_DEVICE_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
375 7, MAX6697_TEMP_CRIT);
377 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 22);
378 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 16);
379 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 17);
380 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 18);
381 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 19);
382 static SENSOR_DEVICE_ATTR(temp6_max_alarm, S_IRUGO, show_alarm, NULL, 20);
383 static SENSOR_DEVICE_ATTR(temp7_max_alarm, S_IRUGO, show_alarm, NULL, 21);
384 static SENSOR_DEVICE_ATTR(temp8_max_alarm, S_IRUGO, show_alarm, NULL, 23);
386 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
387 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
388 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
389 static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 10);
390 static SENSOR_DEVICE_ATTR(temp5_crit_alarm, S_IRUGO, show_alarm, NULL, 11);
391 static SENSOR_DEVICE_ATTR(temp6_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
392 static SENSOR_DEVICE_ATTR(temp7_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
393 static SENSOR_DEVICE_ATTR(temp8_crit_alarm, S_IRUGO, show_alarm, NULL, 15);
395 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 1);
396 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
397 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 3);
398 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL, 4);
399 static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_alarm, NULL, 5);
400 static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_alarm, NULL, 6);
401 static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_alarm, NULL, 7);
403 static DEVICE_ATTR(dummy, 0, NULL, NULL);
405 static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr,
406 int index)
408 struct device *dev = container_of(kobj, struct device, kobj);
409 struct max6697_data *data = dev_get_drvdata(dev);
410 const struct max6697_chip_data *chip = data->chip;
411 int channel = index / 6; /* channel number */
412 int nr = index % 6; /* attribute index within channel */
414 if (channel >= chip->channels)
415 return 0;
417 if ((nr == 3 || nr == 4) && !(chip->have_crit & (1 << channel)))
418 return 0;
419 if (nr == 5 && !(chip->have_fault & (1 << channel)))
420 return 0;
422 return attr->mode;
426 * max6697_is_visible uses the index into the following array to determine
427 * if attributes should be created or not. Any change in order or content
428 * must be matched in max6697_is_visible.
430 static struct attribute *max6697_attributes[] = {
431 &sensor_dev_attr_temp1_input.dev_attr.attr,
432 &sensor_dev_attr_temp1_max.dev_attr.attr,
433 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
434 &sensor_dev_attr_temp1_crit.dev_attr.attr,
435 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
436 &dev_attr_dummy.attr,
438 &sensor_dev_attr_temp2_input.dev_attr.attr,
439 &sensor_dev_attr_temp2_max.dev_attr.attr,
440 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
441 &sensor_dev_attr_temp2_crit.dev_attr.attr,
442 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
443 &sensor_dev_attr_temp2_fault.dev_attr.attr,
445 &sensor_dev_attr_temp3_input.dev_attr.attr,
446 &sensor_dev_attr_temp3_max.dev_attr.attr,
447 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
448 &sensor_dev_attr_temp3_crit.dev_attr.attr,
449 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
450 &sensor_dev_attr_temp3_fault.dev_attr.attr,
452 &sensor_dev_attr_temp4_input.dev_attr.attr,
453 &sensor_dev_attr_temp4_max.dev_attr.attr,
454 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
455 &sensor_dev_attr_temp4_crit.dev_attr.attr,
456 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
457 &sensor_dev_attr_temp4_fault.dev_attr.attr,
459 &sensor_dev_attr_temp5_input.dev_attr.attr,
460 &sensor_dev_attr_temp5_max.dev_attr.attr,
461 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
462 &sensor_dev_attr_temp5_crit.dev_attr.attr,
463 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
464 &sensor_dev_attr_temp5_fault.dev_attr.attr,
466 &sensor_dev_attr_temp6_input.dev_attr.attr,
467 &sensor_dev_attr_temp6_max.dev_attr.attr,
468 &sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
469 &sensor_dev_attr_temp6_crit.dev_attr.attr,
470 &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
471 &sensor_dev_attr_temp6_fault.dev_attr.attr,
473 &sensor_dev_attr_temp7_input.dev_attr.attr,
474 &sensor_dev_attr_temp7_max.dev_attr.attr,
475 &sensor_dev_attr_temp7_max_alarm.dev_attr.attr,
476 &sensor_dev_attr_temp7_crit.dev_attr.attr,
477 &sensor_dev_attr_temp7_crit_alarm.dev_attr.attr,
478 &sensor_dev_attr_temp7_fault.dev_attr.attr,
480 &sensor_dev_attr_temp8_input.dev_attr.attr,
481 &sensor_dev_attr_temp8_max.dev_attr.attr,
482 &sensor_dev_attr_temp8_max_alarm.dev_attr.attr,
483 &sensor_dev_attr_temp8_crit.dev_attr.attr,
484 &sensor_dev_attr_temp8_crit_alarm.dev_attr.attr,
485 &sensor_dev_attr_temp8_fault.dev_attr.attr,
486 NULL
489 static const struct attribute_group max6697_group = {
490 .attrs = max6697_attributes, .is_visible = max6697_is_visible,
492 __ATTRIBUTE_GROUPS(max6697);
494 static void max6697_get_config_of(struct device_node *node,
495 struct max6697_platform_data *pdata)
497 int len;
498 const __be32 *prop;
500 pdata->smbus_timeout_disable =
501 of_property_read_bool(node, "smbus-timeout-disable");
502 pdata->extended_range_enable =
503 of_property_read_bool(node, "extended-range-enable");
504 pdata->beta_compensation =
505 of_property_read_bool(node, "beta-compensation-enable");
507 prop = of_get_property(node, "alert-mask", &len);
508 if (prop && len == sizeof(u32))
509 pdata->alert_mask = be32_to_cpu(prop[0]);
510 prop = of_get_property(node, "over-temperature-mask", &len);
511 if (prop && len == sizeof(u32))
512 pdata->over_temperature_mask = be32_to_cpu(prop[0]);
513 prop = of_get_property(node, "resistance-cancellation", &len);
514 if (prop) {
515 if (len == sizeof(u32))
516 pdata->resistance_cancellation = be32_to_cpu(prop[0]);
517 else
518 pdata->resistance_cancellation = 0xfe;
520 prop = of_get_property(node, "transistor-ideality", &len);
521 if (prop && len == 2 * sizeof(u32)) {
522 pdata->ideality_mask = be32_to_cpu(prop[0]);
523 pdata->ideality_value = be32_to_cpu(prop[1]);
527 static int max6697_init_chip(struct max6697_data *data,
528 struct i2c_client *client)
530 struct max6697_platform_data *pdata = dev_get_platdata(&client->dev);
531 struct max6697_platform_data p;
532 const struct max6697_chip_data *chip = data->chip;
533 int factor = chip->channels;
534 int ret, reg;
537 * Don't touch configuration if neither platform data nor OF
538 * configuration was specified. If that is the case, use the
539 * current chip configuration.
541 if (!pdata && !client->dev.of_node) {
542 reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG);
543 if (reg < 0)
544 return reg;
545 if (data->type == max6581) {
546 if (reg & MAX6581_CONF_EXTENDED)
547 data->temp_offset = 64;
548 reg = i2c_smbus_read_byte_data(client,
549 MAX6581_REG_RESISTANCE);
550 if (reg < 0)
551 return reg;
552 factor += hweight8(reg);
553 } else {
554 if (reg & MAX6697_CONF_RESISTANCE)
555 factor++;
557 goto done;
560 if (client->dev.of_node) {
561 memset(&p, 0, sizeof(p));
562 max6697_get_config_of(client->dev.of_node, &p);
563 pdata = &p;
566 reg = 0;
567 if (pdata->smbus_timeout_disable &&
568 (chip->valid_conf & MAX6697_CONF_TIMEOUT)) {
569 reg |= MAX6697_CONF_TIMEOUT;
571 if (pdata->extended_range_enable &&
572 (chip->valid_conf & MAX6581_CONF_EXTENDED)) {
573 reg |= MAX6581_CONF_EXTENDED;
574 data->temp_offset = 64;
576 if (pdata->resistance_cancellation &&
577 (chip->valid_conf & MAX6697_CONF_RESISTANCE)) {
578 reg |= MAX6697_CONF_RESISTANCE;
579 factor++;
581 if (pdata->beta_compensation &&
582 (chip->valid_conf & MAX6693_CONF_BETA)) {
583 reg |= MAX6693_CONF_BETA;
586 ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, reg);
587 if (ret < 0)
588 return ret;
590 ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK,
591 MAX6697_ALERT_MAP_BITS(pdata->alert_mask));
592 if (ret < 0)
593 return ret;
595 ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK,
596 MAX6697_OVERT_MAP_BITS(pdata->over_temperature_mask));
597 if (ret < 0)
598 return ret;
600 if (data->type == max6581) {
601 factor += hweight8(pdata->resistance_cancellation >> 1);
602 ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE,
603 pdata->resistance_cancellation >> 1);
604 if (ret < 0)
605 return ret;
606 ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY,
607 pdata->ideality_value);
608 if (ret < 0)
609 return ret;
610 ret = i2c_smbus_write_byte_data(client,
611 MAX6581_REG_IDEALITY_SELECT,
612 pdata->ideality_mask >> 1);
613 if (ret < 0)
614 return ret;
616 done:
617 data->update_interval = factor * MAX6697_CONV_TIME;
618 return 0;
621 static int max6697_probe(struct i2c_client *client,
622 const struct i2c_device_id *id)
624 struct i2c_adapter *adapter = client->adapter;
625 struct device *dev = &client->dev;
626 struct max6697_data *data;
627 struct device *hwmon_dev;
628 int err;
630 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
631 return -ENODEV;
633 data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL);
634 if (!data)
635 return -ENOMEM;
637 if (client->dev.of_node)
638 data->type = (enum chips)of_device_get_match_data(&client->dev);
639 else
640 data->type = id->driver_data;
641 data->chip = &max6697_chip_data[data->type];
642 data->client = client;
643 mutex_init(&data->update_lock);
645 err = max6697_init_chip(data, client);
646 if (err)
647 return err;
649 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
650 data,
651 max6697_groups);
652 return PTR_ERR_OR_ZERO(hwmon_dev);
655 static const struct i2c_device_id max6697_id[] = {
656 { "max6581", max6581 },
657 { "max6602", max6602 },
658 { "max6622", max6622 },
659 { "max6636", max6636 },
660 { "max6689", max6689 },
661 { "max6693", max6693 },
662 { "max6694", max6694 },
663 { "max6697", max6697 },
664 { "max6698", max6698 },
665 { "max6699", max6699 },
668 MODULE_DEVICE_TABLE(i2c, max6697_id);
670 static const struct of_device_id max6697_of_match[] = {
672 .compatible = "maxim,max6581",
673 .data = (void *)max6581
676 .compatible = "maxim,max6602",
677 .data = (void *)max6602
680 .compatible = "maxim,max6622",
681 .data = (void *)max6622
684 .compatible = "maxim,max6636",
685 .data = (void *)max6636
688 .compatible = "maxim,max6689",
689 .data = (void *)max6689
692 .compatible = "maxim,max6693",
693 .data = (void *)max6693
696 .compatible = "maxim,max6694",
697 .data = (void *)max6694
700 .compatible = "maxim,max6697",
701 .data = (void *)max6697
704 .compatible = "maxim,max6698",
705 .data = (void *)max6698
708 .compatible = "maxim,max6699",
709 .data = (void *)max6699
711 { },
713 MODULE_DEVICE_TABLE(of, max6697_of_match);
715 static struct i2c_driver max6697_driver = {
716 .class = I2C_CLASS_HWMON,
717 .driver = {
718 .name = "max6697",
719 .of_match_table = of_match_ptr(max6697_of_match),
721 .probe = max6697_probe,
722 .id_table = max6697_id,
725 module_i2c_driver(max6697_driver);
727 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
728 MODULE_DESCRIPTION("MAX6697 temperature sensor driver");
729 MODULE_LICENSE("GPL");