3 * Maxim MAX16065/MAX16066 12-Channel/8-Channel, Flash-Configurable
4 * System Managers with Nonvolatile Fault Registers
5 * Maxim MAX16067/MAX16068 6-Channel, Flash-Configurable System Managers
6 * with Nonvolatile Fault Registers
7 * Maxim MAX16070/MAX16071 12-Channel/8-Channel, Flash-Configurable System
8 * Monitors with Nonvolatile Fault Registers
10 * Copyright (C) 2011 Ericsson AB.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/i2c.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/delay.h>
26 #include <linux/jiffies.h>
28 enum chips
{ max16065
, max16066
, max16067
, max16068
, max16070
, max16071
};
33 #define MAX16065_ADC(x) ((x) * 2)
35 #define MAX16065_CURR_SENSE 0x18
36 #define MAX16065_CSP_ADC 0x19
37 #define MAX16065_FAULT(x) (0x1b + (x))
38 #define MAX16065_SCALE(x) (0x43 + (x))
39 #define MAX16065_CURR_CONTROL 0x47
40 #define MAX16065_LIMIT(l, x) (0x48 + (l) + (x) * 3) /*
48 #define MAX16065_SW_ENABLE 0x73
50 #define MAX16065_WARNING_OV (1 << 3) /* Set if secondary threshold is OV
53 #define MAX16065_CURR_ENABLE (1 << 0)
55 #define MAX16065_NUM_LIMIT 3
56 #define MAX16065_NUM_ADC 12 /* maximum number of ADC channels */
58 static const int max16065_num_adc
[] = {
67 static const bool max16065_have_secondary
[] = {
76 static const bool max16065_have_current
[] = {
85 struct max16065_data
{
87 struct device
*hwmon_dev
;
88 struct mutex update_lock
;
90 unsigned long last_updated
; /* in jiffies */
94 /* limits are in mV */
95 int limit
[MAX16065_NUM_LIMIT
][MAX16065_NUM_ADC
];
96 int range
[MAX16065_NUM_ADC
+ 1];/* voltage range */
97 int adc
[MAX16065_NUM_ADC
+ 1]; /* adc values (raw) including csp_adc */
102 static const int max16065_adc_range
[] = { 5560, 2780, 1390, 0 };
103 static const int max16065_csp_adc_range
[] = { 7000, 14000 };
105 /* ADC registers have 10 bit resolution. */
106 static inline int ADC_TO_MV(int adc
, int range
)
108 return (adc
* range
) / 1024;
112 * Limit registers have 8 bit resolution and match upper 8 bits of ADC
115 static inline int LIMIT_TO_MV(int limit
, int range
)
117 return limit
* range
/ 256;
120 static inline int MV_TO_LIMIT(int mv
, int range
)
122 return SENSORS_LIMIT(DIV_ROUND_CLOSEST(mv
* 256, range
), 0, 255);
125 static inline int ADC_TO_CURR(int adc
, int gain
)
127 return adc
* 1400000 / (gain
* 255);
131 * max16065_read_adc()
133 * Read 16 bit value from <reg>, <reg+1>.
134 * Upper 8 bits are in <reg>, lower 2 bits are in bits 7:6 of <reg+1>.
136 static int max16065_read_adc(struct i2c_client
*client
, int reg
)
140 rv
= i2c_smbus_read_word_swapped(client
, reg
);
141 if (unlikely(rv
< 0))
146 static struct max16065_data
*max16065_update_device(struct device
*dev
)
148 struct i2c_client
*client
= to_i2c_client(dev
);
149 struct max16065_data
*data
= i2c_get_clientdata(client
);
151 mutex_lock(&data
->update_lock
);
152 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
155 for (i
= 0; i
< data
->num_adc
; i
++)
157 = max16065_read_adc(client
, MAX16065_ADC(i
));
159 if (data
->have_current
) {
160 data
->adc
[MAX16065_NUM_ADC
]
161 = max16065_read_adc(client
, MAX16065_CSP_ADC
);
163 = i2c_smbus_read_byte_data(client
,
164 MAX16065_CURR_SENSE
);
167 for (i
= 0; i
< DIV_ROUND_UP(data
->num_adc
, 8); i
++)
169 = i2c_smbus_read_byte_data(client
, MAX16065_FAULT(i
));
171 data
->last_updated
= jiffies
;
174 mutex_unlock(&data
->update_lock
);
178 static ssize_t
max16065_show_alarm(struct device
*dev
,
179 struct device_attribute
*da
, char *buf
)
181 struct sensor_device_attribute_2
*attr2
= to_sensor_dev_attr_2(da
);
182 struct max16065_data
*data
= max16065_update_device(dev
);
183 int val
= data
->fault
[attr2
->nr
];
188 val
&= (1 << attr2
->index
);
190 i2c_smbus_write_byte_data(to_i2c_client(dev
),
191 MAX16065_FAULT(attr2
->nr
), val
);
193 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!val
);
196 static ssize_t
max16065_show_input(struct device
*dev
,
197 struct device_attribute
*da
, char *buf
)
199 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
200 struct max16065_data
*data
= max16065_update_device(dev
);
201 int adc
= data
->adc
[attr
->index
];
203 if (unlikely(adc
< 0))
206 return snprintf(buf
, PAGE_SIZE
, "%d\n",
207 ADC_TO_MV(adc
, data
->range
[attr
->index
]));
210 static ssize_t
max16065_show_current(struct device
*dev
,
211 struct device_attribute
*da
, char *buf
)
213 struct max16065_data
*data
= max16065_update_device(dev
);
215 if (unlikely(data
->curr_sense
< 0))
216 return data
->curr_sense
;
218 return snprintf(buf
, PAGE_SIZE
, "%d\n",
219 ADC_TO_CURR(data
->curr_sense
, data
->curr_gain
));
222 static ssize_t
max16065_set_limit(struct device
*dev
,
223 struct device_attribute
*da
,
224 const char *buf
, size_t count
)
226 struct sensor_device_attribute_2
*attr2
= to_sensor_dev_attr_2(da
);
227 struct i2c_client
*client
= to_i2c_client(dev
);
228 struct max16065_data
*data
= i2c_get_clientdata(client
);
233 err
= kstrtoul(buf
, 10, &val
);
234 if (unlikely(err
< 0))
237 limit
= MV_TO_LIMIT(val
, data
->range
[attr2
->index
]);
239 mutex_lock(&data
->update_lock
);
240 data
->limit
[attr2
->nr
][attr2
->index
]
241 = LIMIT_TO_MV(limit
, data
->range
[attr2
->index
]);
242 i2c_smbus_write_byte_data(client
,
243 MAX16065_LIMIT(attr2
->nr
, attr2
->index
),
245 mutex_unlock(&data
->update_lock
);
250 static ssize_t
max16065_show_limit(struct device
*dev
,
251 struct device_attribute
*da
, char *buf
)
253 struct sensor_device_attribute_2
*attr2
= to_sensor_dev_attr_2(da
);
254 struct i2c_client
*client
= to_i2c_client(dev
);
255 struct max16065_data
*data
= i2c_get_clientdata(client
);
257 return snprintf(buf
, PAGE_SIZE
, "%d\n",
258 data
->limit
[attr2
->nr
][attr2
->index
]);
261 /* Construct a sensor_device_attribute structure for each register */
264 static SENSOR_DEVICE_ATTR(in0_input
, S_IRUGO
, max16065_show_input
, NULL
, 0);
265 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, max16065_show_input
, NULL
, 1);
266 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, max16065_show_input
, NULL
, 2);
267 static SENSOR_DEVICE_ATTR(in3_input
, S_IRUGO
, max16065_show_input
, NULL
, 3);
268 static SENSOR_DEVICE_ATTR(in4_input
, S_IRUGO
, max16065_show_input
, NULL
, 4);
269 static SENSOR_DEVICE_ATTR(in5_input
, S_IRUGO
, max16065_show_input
, NULL
, 5);
270 static SENSOR_DEVICE_ATTR(in6_input
, S_IRUGO
, max16065_show_input
, NULL
, 6);
271 static SENSOR_DEVICE_ATTR(in7_input
, S_IRUGO
, max16065_show_input
, NULL
, 7);
272 static SENSOR_DEVICE_ATTR(in8_input
, S_IRUGO
, max16065_show_input
, NULL
, 8);
273 static SENSOR_DEVICE_ATTR(in9_input
, S_IRUGO
, max16065_show_input
, NULL
, 9);
274 static SENSOR_DEVICE_ATTR(in10_input
, S_IRUGO
, max16065_show_input
, NULL
, 10);
275 static SENSOR_DEVICE_ATTR(in11_input
, S_IRUGO
, max16065_show_input
, NULL
, 11);
276 static SENSOR_DEVICE_ATTR(in12_input
, S_IRUGO
, max16065_show_input
, NULL
, 12);
278 /* Input voltages lcrit */
279 static SENSOR_DEVICE_ATTR_2(in0_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
280 max16065_set_limit
, 2, 0);
281 static SENSOR_DEVICE_ATTR_2(in1_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
282 max16065_set_limit
, 2, 1);
283 static SENSOR_DEVICE_ATTR_2(in2_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
284 max16065_set_limit
, 2, 2);
285 static SENSOR_DEVICE_ATTR_2(in3_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
286 max16065_set_limit
, 2, 3);
287 static SENSOR_DEVICE_ATTR_2(in4_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
288 max16065_set_limit
, 2, 4);
289 static SENSOR_DEVICE_ATTR_2(in5_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
290 max16065_set_limit
, 2, 5);
291 static SENSOR_DEVICE_ATTR_2(in6_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
292 max16065_set_limit
, 2, 6);
293 static SENSOR_DEVICE_ATTR_2(in7_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
294 max16065_set_limit
, 2, 7);
295 static SENSOR_DEVICE_ATTR_2(in8_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
296 max16065_set_limit
, 2, 8);
297 static SENSOR_DEVICE_ATTR_2(in9_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
298 max16065_set_limit
, 2, 9);
299 static SENSOR_DEVICE_ATTR_2(in10_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
300 max16065_set_limit
, 2, 10);
301 static SENSOR_DEVICE_ATTR_2(in11_lcrit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
302 max16065_set_limit
, 2, 11);
304 /* Input voltages crit */
305 static SENSOR_DEVICE_ATTR_2(in0_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
306 max16065_set_limit
, 1, 0);
307 static SENSOR_DEVICE_ATTR_2(in1_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
308 max16065_set_limit
, 1, 1);
309 static SENSOR_DEVICE_ATTR_2(in2_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
310 max16065_set_limit
, 1, 2);
311 static SENSOR_DEVICE_ATTR_2(in3_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
312 max16065_set_limit
, 1, 3);
313 static SENSOR_DEVICE_ATTR_2(in4_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
314 max16065_set_limit
, 1, 4);
315 static SENSOR_DEVICE_ATTR_2(in5_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
316 max16065_set_limit
, 1, 5);
317 static SENSOR_DEVICE_ATTR_2(in6_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
318 max16065_set_limit
, 1, 6);
319 static SENSOR_DEVICE_ATTR_2(in7_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
320 max16065_set_limit
, 1, 7);
321 static SENSOR_DEVICE_ATTR_2(in8_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
322 max16065_set_limit
, 1, 8);
323 static SENSOR_DEVICE_ATTR_2(in9_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
324 max16065_set_limit
, 1, 9);
325 static SENSOR_DEVICE_ATTR_2(in10_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
326 max16065_set_limit
, 1, 10);
327 static SENSOR_DEVICE_ATTR_2(in11_crit
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
328 max16065_set_limit
, 1, 11);
330 /* Input voltages min */
331 static SENSOR_DEVICE_ATTR_2(in0_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
332 max16065_set_limit
, 0, 0);
333 static SENSOR_DEVICE_ATTR_2(in1_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
334 max16065_set_limit
, 0, 1);
335 static SENSOR_DEVICE_ATTR_2(in2_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
336 max16065_set_limit
, 0, 2);
337 static SENSOR_DEVICE_ATTR_2(in3_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
338 max16065_set_limit
, 0, 3);
339 static SENSOR_DEVICE_ATTR_2(in4_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
340 max16065_set_limit
, 0, 4);
341 static SENSOR_DEVICE_ATTR_2(in5_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
342 max16065_set_limit
, 0, 5);
343 static SENSOR_DEVICE_ATTR_2(in6_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
344 max16065_set_limit
, 0, 6);
345 static SENSOR_DEVICE_ATTR_2(in7_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
346 max16065_set_limit
, 0, 7);
347 static SENSOR_DEVICE_ATTR_2(in8_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
348 max16065_set_limit
, 0, 8);
349 static SENSOR_DEVICE_ATTR_2(in9_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
350 max16065_set_limit
, 0, 9);
351 static SENSOR_DEVICE_ATTR_2(in10_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
352 max16065_set_limit
, 0, 10);
353 static SENSOR_DEVICE_ATTR_2(in11_min
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
354 max16065_set_limit
, 0, 11);
356 /* Input voltages max */
357 static SENSOR_DEVICE_ATTR_2(in0_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
358 max16065_set_limit
, 0, 0);
359 static SENSOR_DEVICE_ATTR_2(in1_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
360 max16065_set_limit
, 0, 1);
361 static SENSOR_DEVICE_ATTR_2(in2_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
362 max16065_set_limit
, 0, 2);
363 static SENSOR_DEVICE_ATTR_2(in3_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
364 max16065_set_limit
, 0, 3);
365 static SENSOR_DEVICE_ATTR_2(in4_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
366 max16065_set_limit
, 0, 4);
367 static SENSOR_DEVICE_ATTR_2(in5_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
368 max16065_set_limit
, 0, 5);
369 static SENSOR_DEVICE_ATTR_2(in6_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
370 max16065_set_limit
, 0, 6);
371 static SENSOR_DEVICE_ATTR_2(in7_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
372 max16065_set_limit
, 0, 7);
373 static SENSOR_DEVICE_ATTR_2(in8_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
374 max16065_set_limit
, 0, 8);
375 static SENSOR_DEVICE_ATTR_2(in9_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
376 max16065_set_limit
, 0, 9);
377 static SENSOR_DEVICE_ATTR_2(in10_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
378 max16065_set_limit
, 0, 10);
379 static SENSOR_DEVICE_ATTR_2(in11_max
, S_IWUSR
| S_IRUGO
, max16065_show_limit
,
380 max16065_set_limit
, 0, 11);
383 static SENSOR_DEVICE_ATTR_2(in0_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
385 static SENSOR_DEVICE_ATTR_2(in1_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
387 static SENSOR_DEVICE_ATTR_2(in2_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
389 static SENSOR_DEVICE_ATTR_2(in3_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
391 static SENSOR_DEVICE_ATTR_2(in4_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
393 static SENSOR_DEVICE_ATTR_2(in5_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
395 static SENSOR_DEVICE_ATTR_2(in6_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
397 static SENSOR_DEVICE_ATTR_2(in7_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
399 static SENSOR_DEVICE_ATTR_2(in8_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
401 static SENSOR_DEVICE_ATTR_2(in9_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
403 static SENSOR_DEVICE_ATTR_2(in10_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
405 static SENSOR_DEVICE_ATTR_2(in11_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
408 /* Current and alarm */
409 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, max16065_show_current
, NULL
, 0);
410 static SENSOR_DEVICE_ATTR_2(curr1_alarm
, S_IRUGO
, max16065_show_alarm
, NULL
,
414 * Finally, construct an array of pointers to members of the above objects,
415 * as required for sysfs_create_group()
417 static struct attribute
*max16065_basic_attributes
[] = {
418 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
419 &sensor_dev_attr_in0_lcrit
.dev_attr
.attr
,
420 &sensor_dev_attr_in0_crit
.dev_attr
.attr
,
421 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
423 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
424 &sensor_dev_attr_in1_lcrit
.dev_attr
.attr
,
425 &sensor_dev_attr_in1_crit
.dev_attr
.attr
,
426 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
428 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
429 &sensor_dev_attr_in2_lcrit
.dev_attr
.attr
,
430 &sensor_dev_attr_in2_crit
.dev_attr
.attr
,
431 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
433 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
434 &sensor_dev_attr_in3_lcrit
.dev_attr
.attr
,
435 &sensor_dev_attr_in3_crit
.dev_attr
.attr
,
436 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
438 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
439 &sensor_dev_attr_in4_lcrit
.dev_attr
.attr
,
440 &sensor_dev_attr_in4_crit
.dev_attr
.attr
,
441 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
443 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
444 &sensor_dev_attr_in5_lcrit
.dev_attr
.attr
,
445 &sensor_dev_attr_in5_crit
.dev_attr
.attr
,
446 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
448 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
449 &sensor_dev_attr_in6_lcrit
.dev_attr
.attr
,
450 &sensor_dev_attr_in6_crit
.dev_attr
.attr
,
451 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
453 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
454 &sensor_dev_attr_in7_lcrit
.dev_attr
.attr
,
455 &sensor_dev_attr_in7_crit
.dev_attr
.attr
,
456 &sensor_dev_attr_in7_alarm
.dev_attr
.attr
,
458 &sensor_dev_attr_in8_input
.dev_attr
.attr
,
459 &sensor_dev_attr_in8_lcrit
.dev_attr
.attr
,
460 &sensor_dev_attr_in8_crit
.dev_attr
.attr
,
461 &sensor_dev_attr_in8_alarm
.dev_attr
.attr
,
463 &sensor_dev_attr_in9_input
.dev_attr
.attr
,
464 &sensor_dev_attr_in9_lcrit
.dev_attr
.attr
,
465 &sensor_dev_attr_in9_crit
.dev_attr
.attr
,
466 &sensor_dev_attr_in9_alarm
.dev_attr
.attr
,
468 &sensor_dev_attr_in10_input
.dev_attr
.attr
,
469 &sensor_dev_attr_in10_lcrit
.dev_attr
.attr
,
470 &sensor_dev_attr_in10_crit
.dev_attr
.attr
,
471 &sensor_dev_attr_in10_alarm
.dev_attr
.attr
,
473 &sensor_dev_attr_in11_input
.dev_attr
.attr
,
474 &sensor_dev_attr_in11_lcrit
.dev_attr
.attr
,
475 &sensor_dev_attr_in11_crit
.dev_attr
.attr
,
476 &sensor_dev_attr_in11_alarm
.dev_attr
.attr
,
481 static struct attribute
*max16065_current_attributes
[] = {
482 &sensor_dev_attr_in12_input
.dev_attr
.attr
,
483 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
484 &sensor_dev_attr_curr1_alarm
.dev_attr
.attr
,
488 static struct attribute
*max16065_min_attributes
[] = {
489 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
490 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
491 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
492 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
493 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
494 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
495 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
496 &sensor_dev_attr_in7_min
.dev_attr
.attr
,
497 &sensor_dev_attr_in8_min
.dev_attr
.attr
,
498 &sensor_dev_attr_in9_min
.dev_attr
.attr
,
499 &sensor_dev_attr_in10_min
.dev_attr
.attr
,
500 &sensor_dev_attr_in11_min
.dev_attr
.attr
,
504 static struct attribute
*max16065_max_attributes
[] = {
505 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
506 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
507 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
508 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
509 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
510 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
511 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
512 &sensor_dev_attr_in7_max
.dev_attr
.attr
,
513 &sensor_dev_attr_in8_max
.dev_attr
.attr
,
514 &sensor_dev_attr_in9_max
.dev_attr
.attr
,
515 &sensor_dev_attr_in10_max
.dev_attr
.attr
,
516 &sensor_dev_attr_in11_max
.dev_attr
.attr
,
520 static const struct attribute_group max16065_basic_group
= {
521 .attrs
= max16065_basic_attributes
,
524 static const struct attribute_group max16065_current_group
= {
525 .attrs
= max16065_current_attributes
,
528 static const struct attribute_group max16065_min_group
= {
529 .attrs
= max16065_min_attributes
,
532 static const struct attribute_group max16065_max_group
= {
533 .attrs
= max16065_max_attributes
,
536 static void max16065_cleanup(struct i2c_client
*client
)
538 sysfs_remove_group(&client
->dev
.kobj
, &max16065_max_group
);
539 sysfs_remove_group(&client
->dev
.kobj
, &max16065_min_group
);
540 sysfs_remove_group(&client
->dev
.kobj
, &max16065_current_group
);
541 sysfs_remove_group(&client
->dev
.kobj
, &max16065_basic_group
);
544 static int max16065_probe(struct i2c_client
*client
,
545 const struct i2c_device_id
*id
)
547 struct i2c_adapter
*adapter
= client
->adapter
;
548 struct max16065_data
*data
;
550 bool have_secondary
; /* true if chip has secondary limits */
551 bool secondary_is_max
= false; /* secondary limits reflect max */
553 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
554 | I2C_FUNC_SMBUS_READ_WORD_DATA
))
557 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
561 i2c_set_clientdata(client
, data
);
562 mutex_init(&data
->update_lock
);
564 data
->num_adc
= max16065_num_adc
[id
->driver_data
];
565 data
->have_current
= max16065_have_current
[id
->driver_data
];
566 have_secondary
= max16065_have_secondary
[id
->driver_data
];
568 if (have_secondary
) {
569 val
= i2c_smbus_read_byte_data(client
, MAX16065_SW_ENABLE
);
570 if (unlikely(val
< 0))
572 secondary_is_max
= val
& MAX16065_WARNING_OV
;
575 /* Read scale registers, convert to range */
576 for (i
= 0; i
< DIV_ROUND_UP(data
->num_adc
, 4); i
++) {
577 val
= i2c_smbus_read_byte_data(client
, MAX16065_SCALE(i
));
578 if (unlikely(val
< 0))
580 for (j
= 0; j
< 4 && i
* 4 + j
< data
->num_adc
; j
++) {
581 data
->range
[i
* 4 + j
] =
582 max16065_adc_range
[(val
>> (j
* 2)) & 0x3];
587 for (i
= 0; i
< MAX16065_NUM_LIMIT
; i
++) {
588 if (i
== 0 && !have_secondary
)
591 for (j
= 0; j
< data
->num_adc
; j
++) {
592 val
= i2c_smbus_read_byte_data(client
,
593 MAX16065_LIMIT(i
, j
));
594 if (unlikely(val
< 0))
596 data
->limit
[i
][j
] = LIMIT_TO_MV(val
, data
->range
[j
]);
600 /* Register sysfs hooks */
601 for (i
= 0; i
< data
->num_adc
* 4; i
++) {
602 /* Do not create sysfs entry if channel is disabled */
603 if (!data
->range
[i
/ 4])
606 ret
= sysfs_create_file(&client
->dev
.kobj
,
607 max16065_basic_attributes
[i
]);
612 if (have_secondary
) {
613 struct attribute
**attr
= secondary_is_max
?
614 max16065_max_attributes
: max16065_min_attributes
;
616 for (i
= 0; i
< data
->num_adc
; i
++) {
620 ret
= sysfs_create_file(&client
->dev
.kobj
, attr
[i
]);
626 if (data
->have_current
) {
627 val
= i2c_smbus_read_byte_data(client
, MAX16065_CURR_CONTROL
);
628 if (unlikely(val
< 0)) {
632 if (val
& MAX16065_CURR_ENABLE
) {
634 * Current gain is 6, 12, 24, 48 based on values in
637 data
->curr_gain
= 6 << ((val
>> 2) & 0x03);
638 data
->range
[MAX16065_NUM_ADC
]
639 = max16065_csp_adc_range
[(val
>> 1) & 0x01];
640 ret
= sysfs_create_group(&client
->dev
.kobj
,
641 &max16065_current_group
);
645 data
->have_current
= false;
649 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
650 if (unlikely(IS_ERR(data
->hwmon_dev
))) {
651 ret
= PTR_ERR(data
->hwmon_dev
);
657 max16065_cleanup(client
);
661 static int max16065_remove(struct i2c_client
*client
)
663 struct max16065_data
*data
= i2c_get_clientdata(client
);
665 hwmon_device_unregister(data
->hwmon_dev
);
666 max16065_cleanup(client
);
671 static const struct i2c_device_id max16065_id
[] = {
672 { "max16065", max16065
},
673 { "max16066", max16066
},
674 { "max16067", max16067
},
675 { "max16068", max16068
},
676 { "max16070", max16070
},
677 { "max16071", max16071
},
681 MODULE_DEVICE_TABLE(i2c
, max16065_id
);
683 /* This is the driver that will be inserted */
684 static struct i2c_driver max16065_driver
= {
688 .probe
= max16065_probe
,
689 .remove
= max16065_remove
,
690 .id_table
= max16065_id
,
693 module_i2c_driver(max16065_driver
);
695 MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
696 MODULE_DESCRIPTION("MAX16065 driver");
697 MODULE_LICENSE("GPL");