xref: /openbmc/linux/drivers/hwmon/max16065.c (revision dc3e0aa5)
1 /*
2  * Driver for
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
9  *
10  * Copyright (C) 2011 Ericsson AB.
11  *
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.
15  */
16 
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/jiffies.h>
26 
27 enum chips { max16065, max16066, max16067, max16068, max16070, max16071 };
28 
29 /*
30  * Registers
31  */
32 #define MAX16065_ADC(x)		((x) * 2)
33 
34 #define MAX16065_CURR_SENSE	0x18
35 #define MAX16065_CSP_ADC	0x19
36 #define MAX16065_FAULT(x)	(0x1b + (x))
37 #define MAX16065_SCALE(x)	(0x43 + (x))
38 #define MAX16065_CURR_CONTROL	0x47
39 #define MAX16065_LIMIT(l, x)	(0x48 + (l) + (x) * 3)	/*
40 							 * l: limit
41 							 *  0: min/max
42 							 *  1: crit
43 							 *  2: lcrit
44 							 * x: ADC index
45 							 */
46 
47 #define MAX16065_SW_ENABLE	0x73
48 
49 #define MAX16065_WARNING_OV	(1 << 3) /* Set if secondary threshold is OV
50 					    warning */
51 
52 #define MAX16065_CURR_ENABLE	(1 << 0)
53 
54 #define MAX16065_NUM_LIMIT	3
55 #define MAX16065_NUM_ADC	12	/* maximum number of ADC channels */
56 
57 static const int max16065_num_adc[] = {
58 	[max16065] = 12,
59 	[max16066] = 8,
60 	[max16067] = 6,
61 	[max16068] = 6,
62 	[max16070] = 12,
63 	[max16071] = 8,
64 };
65 
66 static const bool max16065_have_secondary[] = {
67 	[max16065] = true,
68 	[max16066] = true,
69 	[max16067] = false,
70 	[max16068] = false,
71 	[max16070] = true,
72 	[max16071] = true,
73 };
74 
75 static const bool max16065_have_current[] = {
76 	[max16065] = true,
77 	[max16066] = true,
78 	[max16067] = false,
79 	[max16068] = false,
80 	[max16070] = true,
81 	[max16071] = true,
82 };
83 
84 struct max16065_data {
85 	enum chips type;
86 	struct i2c_client *client;
87 	const struct attribute_group *groups[4];
88 	struct mutex update_lock;
89 	bool valid;
90 	unsigned long last_updated; /* in jiffies */
91 	int num_adc;
92 	bool have_current;
93 	int curr_gain;
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 */
98 	int curr_sense;
99 	int fault[2];
100 };
101 
102 static const int max16065_adc_range[] = { 5560, 2780, 1390, 0 };
103 static const int max16065_csp_adc_range[] = { 7000, 14000 };
104 
105 /* ADC registers have 10 bit resolution. */
106 static inline int ADC_TO_MV(int adc, int range)
107 {
108 	return (adc * range) / 1024;
109 }
110 
111 /*
112  * Limit registers have 8 bit resolution and match upper 8 bits of ADC
113  * registers.
114  */
115 static inline int LIMIT_TO_MV(int limit, int range)
116 {
117 	return limit * range / 256;
118 }
119 
120 static inline int MV_TO_LIMIT(int mv, int range)
121 {
122 	return clamp_val(DIV_ROUND_CLOSEST(mv * 256, range), 0, 255);
123 }
124 
125 static inline int ADC_TO_CURR(int adc, int gain)
126 {
127 	return adc * 1400000 / (gain * 255);
128 }
129 
130 /*
131  * max16065_read_adc()
132  *
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>.
135  */
136 static int max16065_read_adc(struct i2c_client *client, int reg)
137 {
138 	int rv;
139 
140 	rv = i2c_smbus_read_word_swapped(client, reg);
141 	if (unlikely(rv < 0))
142 		return rv;
143 	return rv >> 6;
144 }
145 
146 static struct max16065_data *max16065_update_device(struct device *dev)
147 {
148 	struct max16065_data *data = dev_get_drvdata(dev);
149 	struct i2c_client *client = data->client;
150 
151 	mutex_lock(&data->update_lock);
152 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
153 		int i;
154 
155 		for (i = 0; i < data->num_adc; i++)
156 			data->adc[i]
157 			  = max16065_read_adc(client, MAX16065_ADC(i));
158 
159 		if (data->have_current) {
160 			data->adc[MAX16065_NUM_ADC]
161 			  = max16065_read_adc(client, MAX16065_CSP_ADC);
162 			data->curr_sense
163 			  = i2c_smbus_read_byte_data(client,
164 						     MAX16065_CURR_SENSE);
165 		}
166 
167 		for (i = 0; i < DIV_ROUND_UP(data->num_adc, 8); i++)
168 			data->fault[i]
169 			  = i2c_smbus_read_byte_data(client, MAX16065_FAULT(i));
170 
171 		data->last_updated = jiffies;
172 		data->valid = 1;
173 	}
174 	mutex_unlock(&data->update_lock);
175 	return data;
176 }
177 
178 static ssize_t max16065_alarm_show(struct device *dev,
179 				   struct device_attribute *da, char *buf)
180 {
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];
184 
185 	if (val < 0)
186 		return val;
187 
188 	val &= (1 << attr2->index);
189 	if (val)
190 		i2c_smbus_write_byte_data(data->client,
191 					  MAX16065_FAULT(attr2->nr), val);
192 
193 	return snprintf(buf, PAGE_SIZE, "%d\n", !!val);
194 }
195 
196 static ssize_t max16065_input_show(struct device *dev,
197 				   struct device_attribute *da, char *buf)
198 {
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];
202 
203 	if (unlikely(adc < 0))
204 		return adc;
205 
206 	return snprintf(buf, PAGE_SIZE, "%d\n",
207 			ADC_TO_MV(adc, data->range[attr->index]));
208 }
209 
210 static ssize_t max16065_current_show(struct device *dev,
211 				     struct device_attribute *da, char *buf)
212 {
213 	struct max16065_data *data = max16065_update_device(dev);
214 
215 	if (unlikely(data->curr_sense < 0))
216 		return data->curr_sense;
217 
218 	return snprintf(buf, PAGE_SIZE, "%d\n",
219 			ADC_TO_CURR(data->curr_sense, data->curr_gain));
220 }
221 
222 static ssize_t max16065_limit_store(struct device *dev,
223 				    struct device_attribute *da,
224 				    const char *buf, size_t count)
225 {
226 	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
227 	struct max16065_data *data = dev_get_drvdata(dev);
228 	unsigned long val;
229 	int err;
230 	int limit;
231 
232 	err = kstrtoul(buf, 10, &val);
233 	if (unlikely(err < 0))
234 		return err;
235 
236 	limit = MV_TO_LIMIT(val, data->range[attr2->index]);
237 
238 	mutex_lock(&data->update_lock);
239 	data->limit[attr2->nr][attr2->index]
240 	  = LIMIT_TO_MV(limit, data->range[attr2->index]);
241 	i2c_smbus_write_byte_data(data->client,
242 				  MAX16065_LIMIT(attr2->nr, attr2->index),
243 				  limit);
244 	mutex_unlock(&data->update_lock);
245 
246 	return count;
247 }
248 
249 static ssize_t max16065_limit_show(struct device *dev,
250 				   struct device_attribute *da, char *buf)
251 {
252 	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
253 	struct max16065_data *data = dev_get_drvdata(dev);
254 
255 	return snprintf(buf, PAGE_SIZE, "%d\n",
256 			data->limit[attr2->nr][attr2->index]);
257 }
258 
259 /* Construct a sensor_device_attribute structure for each register */
260 
261 /* Input voltages */
262 static SENSOR_DEVICE_ATTR_RO(in0_input, max16065_input, 0);
263 static SENSOR_DEVICE_ATTR_RO(in1_input, max16065_input, 1);
264 static SENSOR_DEVICE_ATTR_RO(in2_input, max16065_input, 2);
265 static SENSOR_DEVICE_ATTR_RO(in3_input, max16065_input, 3);
266 static SENSOR_DEVICE_ATTR_RO(in4_input, max16065_input, 4);
267 static SENSOR_DEVICE_ATTR_RO(in5_input, max16065_input, 5);
268 static SENSOR_DEVICE_ATTR_RO(in6_input, max16065_input, 6);
269 static SENSOR_DEVICE_ATTR_RO(in7_input, max16065_input, 7);
270 static SENSOR_DEVICE_ATTR_RO(in8_input, max16065_input, 8);
271 static SENSOR_DEVICE_ATTR_RO(in9_input, max16065_input, 9);
272 static SENSOR_DEVICE_ATTR_RO(in10_input, max16065_input, 10);
273 static SENSOR_DEVICE_ATTR_RO(in11_input, max16065_input, 11);
274 static SENSOR_DEVICE_ATTR_RO(in12_input, max16065_input, 12);
275 
276 /* Input voltages lcrit */
277 static SENSOR_DEVICE_ATTR_2_RW(in0_lcrit, max16065_limit, 2, 0);
278 static SENSOR_DEVICE_ATTR_2_RW(in1_lcrit, max16065_limit, 2, 1);
279 static SENSOR_DEVICE_ATTR_2_RW(in2_lcrit, max16065_limit, 2, 2);
280 static SENSOR_DEVICE_ATTR_2_RW(in3_lcrit, max16065_limit, 2, 3);
281 static SENSOR_DEVICE_ATTR_2_RW(in4_lcrit, max16065_limit, 2, 4);
282 static SENSOR_DEVICE_ATTR_2_RW(in5_lcrit, max16065_limit, 2, 5);
283 static SENSOR_DEVICE_ATTR_2_RW(in6_lcrit, max16065_limit, 2, 6);
284 static SENSOR_DEVICE_ATTR_2_RW(in7_lcrit, max16065_limit, 2, 7);
285 static SENSOR_DEVICE_ATTR_2_RW(in8_lcrit, max16065_limit, 2, 8);
286 static SENSOR_DEVICE_ATTR_2_RW(in9_lcrit, max16065_limit, 2, 9);
287 static SENSOR_DEVICE_ATTR_2_RW(in10_lcrit, max16065_limit, 2, 10);
288 static SENSOR_DEVICE_ATTR_2_RW(in11_lcrit, max16065_limit, 2, 11);
289 
290 /* Input voltages crit */
291 static SENSOR_DEVICE_ATTR_2_RW(in0_crit, max16065_limit, 1, 0);
292 static SENSOR_DEVICE_ATTR_2_RW(in1_crit, max16065_limit, 1, 1);
293 static SENSOR_DEVICE_ATTR_2_RW(in2_crit, max16065_limit, 1, 2);
294 static SENSOR_DEVICE_ATTR_2_RW(in3_crit, max16065_limit, 1, 3);
295 static SENSOR_DEVICE_ATTR_2_RW(in4_crit, max16065_limit, 1, 4);
296 static SENSOR_DEVICE_ATTR_2_RW(in5_crit, max16065_limit, 1, 5);
297 static SENSOR_DEVICE_ATTR_2_RW(in6_crit, max16065_limit, 1, 6);
298 static SENSOR_DEVICE_ATTR_2_RW(in7_crit, max16065_limit, 1, 7);
299 static SENSOR_DEVICE_ATTR_2_RW(in8_crit, max16065_limit, 1, 8);
300 static SENSOR_DEVICE_ATTR_2_RW(in9_crit, max16065_limit, 1, 9);
301 static SENSOR_DEVICE_ATTR_2_RW(in10_crit, max16065_limit, 1, 10);
302 static SENSOR_DEVICE_ATTR_2_RW(in11_crit, max16065_limit, 1, 11);
303 
304 /* Input voltages min */
305 static SENSOR_DEVICE_ATTR_2_RW(in0_min, max16065_limit, 0, 0);
306 static SENSOR_DEVICE_ATTR_2_RW(in1_min, max16065_limit, 0, 1);
307 static SENSOR_DEVICE_ATTR_2_RW(in2_min, max16065_limit, 0, 2);
308 static SENSOR_DEVICE_ATTR_2_RW(in3_min, max16065_limit, 0, 3);
309 static SENSOR_DEVICE_ATTR_2_RW(in4_min, max16065_limit, 0, 4);
310 static SENSOR_DEVICE_ATTR_2_RW(in5_min, max16065_limit, 0, 5);
311 static SENSOR_DEVICE_ATTR_2_RW(in6_min, max16065_limit, 0, 6);
312 static SENSOR_DEVICE_ATTR_2_RW(in7_min, max16065_limit, 0, 7);
313 static SENSOR_DEVICE_ATTR_2_RW(in8_min, max16065_limit, 0, 8);
314 static SENSOR_DEVICE_ATTR_2_RW(in9_min, max16065_limit, 0, 9);
315 static SENSOR_DEVICE_ATTR_2_RW(in10_min, max16065_limit, 0, 10);
316 static SENSOR_DEVICE_ATTR_2_RW(in11_min, max16065_limit, 0, 11);
317 
318 /* Input voltages max */
319 static SENSOR_DEVICE_ATTR_2_RW(in0_max, max16065_limit, 0, 0);
320 static SENSOR_DEVICE_ATTR_2_RW(in1_max, max16065_limit, 0, 1);
321 static SENSOR_DEVICE_ATTR_2_RW(in2_max, max16065_limit, 0, 2);
322 static SENSOR_DEVICE_ATTR_2_RW(in3_max, max16065_limit, 0, 3);
323 static SENSOR_DEVICE_ATTR_2_RW(in4_max, max16065_limit, 0, 4);
324 static SENSOR_DEVICE_ATTR_2_RW(in5_max, max16065_limit, 0, 5);
325 static SENSOR_DEVICE_ATTR_2_RW(in6_max, max16065_limit, 0, 6);
326 static SENSOR_DEVICE_ATTR_2_RW(in7_max, max16065_limit, 0, 7);
327 static SENSOR_DEVICE_ATTR_2_RW(in8_max, max16065_limit, 0, 8);
328 static SENSOR_DEVICE_ATTR_2_RW(in9_max, max16065_limit, 0, 9);
329 static SENSOR_DEVICE_ATTR_2_RW(in10_max, max16065_limit, 0, 10);
330 static SENSOR_DEVICE_ATTR_2_RW(in11_max, max16065_limit, 0, 11);
331 
332 /* alarms */
333 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, max16065_alarm, 0, 0);
334 static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, max16065_alarm, 0, 1);
335 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, max16065_alarm, 0, 2);
336 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, max16065_alarm, 0, 3);
337 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, max16065_alarm, 0, 4);
338 static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, max16065_alarm, 0, 5);
339 static SENSOR_DEVICE_ATTR_2_RO(in6_alarm, max16065_alarm, 0, 6);
340 static SENSOR_DEVICE_ATTR_2_RO(in7_alarm, max16065_alarm, 0, 7);
341 static SENSOR_DEVICE_ATTR_2_RO(in8_alarm, max16065_alarm, 1, 0);
342 static SENSOR_DEVICE_ATTR_2_RO(in9_alarm, max16065_alarm, 1, 1);
343 static SENSOR_DEVICE_ATTR_2_RO(in10_alarm, max16065_alarm, 1, 2);
344 static SENSOR_DEVICE_ATTR_2_RO(in11_alarm, max16065_alarm, 1, 3);
345 
346 /* Current and alarm */
347 static SENSOR_DEVICE_ATTR_RO(curr1_input, max16065_current, 0);
348 static SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, max16065_alarm, 1, 4);
349 
350 /*
351  * Finally, construct an array of pointers to members of the above objects,
352  * as required for sysfs_create_group()
353  */
354 static struct attribute *max16065_basic_attributes[] = {
355 	&sensor_dev_attr_in0_input.dev_attr.attr,
356 	&sensor_dev_attr_in0_lcrit.dev_attr.attr,
357 	&sensor_dev_attr_in0_crit.dev_attr.attr,
358 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
359 
360 	&sensor_dev_attr_in1_input.dev_attr.attr,
361 	&sensor_dev_attr_in1_lcrit.dev_attr.attr,
362 	&sensor_dev_attr_in1_crit.dev_attr.attr,
363 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
364 
365 	&sensor_dev_attr_in2_input.dev_attr.attr,
366 	&sensor_dev_attr_in2_lcrit.dev_attr.attr,
367 	&sensor_dev_attr_in2_crit.dev_attr.attr,
368 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
369 
370 	&sensor_dev_attr_in3_input.dev_attr.attr,
371 	&sensor_dev_attr_in3_lcrit.dev_attr.attr,
372 	&sensor_dev_attr_in3_crit.dev_attr.attr,
373 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
374 
375 	&sensor_dev_attr_in4_input.dev_attr.attr,
376 	&sensor_dev_attr_in4_lcrit.dev_attr.attr,
377 	&sensor_dev_attr_in4_crit.dev_attr.attr,
378 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
379 
380 	&sensor_dev_attr_in5_input.dev_attr.attr,
381 	&sensor_dev_attr_in5_lcrit.dev_attr.attr,
382 	&sensor_dev_attr_in5_crit.dev_attr.attr,
383 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
384 
385 	&sensor_dev_attr_in6_input.dev_attr.attr,
386 	&sensor_dev_attr_in6_lcrit.dev_attr.attr,
387 	&sensor_dev_attr_in6_crit.dev_attr.attr,
388 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
389 
390 	&sensor_dev_attr_in7_input.dev_attr.attr,
391 	&sensor_dev_attr_in7_lcrit.dev_attr.attr,
392 	&sensor_dev_attr_in7_crit.dev_attr.attr,
393 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
394 
395 	&sensor_dev_attr_in8_input.dev_attr.attr,
396 	&sensor_dev_attr_in8_lcrit.dev_attr.attr,
397 	&sensor_dev_attr_in8_crit.dev_attr.attr,
398 	&sensor_dev_attr_in8_alarm.dev_attr.attr,
399 
400 	&sensor_dev_attr_in9_input.dev_attr.attr,
401 	&sensor_dev_attr_in9_lcrit.dev_attr.attr,
402 	&sensor_dev_attr_in9_crit.dev_attr.attr,
403 	&sensor_dev_attr_in9_alarm.dev_attr.attr,
404 
405 	&sensor_dev_attr_in10_input.dev_attr.attr,
406 	&sensor_dev_attr_in10_lcrit.dev_attr.attr,
407 	&sensor_dev_attr_in10_crit.dev_attr.attr,
408 	&sensor_dev_attr_in10_alarm.dev_attr.attr,
409 
410 	&sensor_dev_attr_in11_input.dev_attr.attr,
411 	&sensor_dev_attr_in11_lcrit.dev_attr.attr,
412 	&sensor_dev_attr_in11_crit.dev_attr.attr,
413 	&sensor_dev_attr_in11_alarm.dev_attr.attr,
414 
415 	NULL
416 };
417 
418 static struct attribute *max16065_current_attributes[] = {
419 	&sensor_dev_attr_in12_input.dev_attr.attr,
420 	&sensor_dev_attr_curr1_input.dev_attr.attr,
421 	&sensor_dev_attr_curr1_alarm.dev_attr.attr,
422 	NULL
423 };
424 
425 static struct attribute *max16065_min_attributes[] = {
426 	&sensor_dev_attr_in0_min.dev_attr.attr,
427 	&sensor_dev_attr_in1_min.dev_attr.attr,
428 	&sensor_dev_attr_in2_min.dev_attr.attr,
429 	&sensor_dev_attr_in3_min.dev_attr.attr,
430 	&sensor_dev_attr_in4_min.dev_attr.attr,
431 	&sensor_dev_attr_in5_min.dev_attr.attr,
432 	&sensor_dev_attr_in6_min.dev_attr.attr,
433 	&sensor_dev_attr_in7_min.dev_attr.attr,
434 	&sensor_dev_attr_in8_min.dev_attr.attr,
435 	&sensor_dev_attr_in9_min.dev_attr.attr,
436 	&sensor_dev_attr_in10_min.dev_attr.attr,
437 	&sensor_dev_attr_in11_min.dev_attr.attr,
438 	NULL
439 };
440 
441 static struct attribute *max16065_max_attributes[] = {
442 	&sensor_dev_attr_in0_max.dev_attr.attr,
443 	&sensor_dev_attr_in1_max.dev_attr.attr,
444 	&sensor_dev_attr_in2_max.dev_attr.attr,
445 	&sensor_dev_attr_in3_max.dev_attr.attr,
446 	&sensor_dev_attr_in4_max.dev_attr.attr,
447 	&sensor_dev_attr_in5_max.dev_attr.attr,
448 	&sensor_dev_attr_in6_max.dev_attr.attr,
449 	&sensor_dev_attr_in7_max.dev_attr.attr,
450 	&sensor_dev_attr_in8_max.dev_attr.attr,
451 	&sensor_dev_attr_in9_max.dev_attr.attr,
452 	&sensor_dev_attr_in10_max.dev_attr.attr,
453 	&sensor_dev_attr_in11_max.dev_attr.attr,
454 	NULL
455 };
456 
457 static umode_t max16065_basic_is_visible(struct kobject *kobj,
458 					 struct attribute *a, int n)
459 {
460 	struct device *dev = container_of(kobj, struct device, kobj);
461 	struct max16065_data *data = dev_get_drvdata(dev);
462 	int index = n / 4;
463 
464 	if (index >= data->num_adc || !data->range[index])
465 		return 0;
466 	return a->mode;
467 }
468 
469 static umode_t max16065_secondary_is_visible(struct kobject *kobj,
470 					     struct attribute *a, int index)
471 {
472 	struct device *dev = container_of(kobj, struct device, kobj);
473 	struct max16065_data *data = dev_get_drvdata(dev);
474 
475 	if (index >= data->num_adc)
476 		return 0;
477 	return a->mode;
478 }
479 
480 static const struct attribute_group max16065_basic_group = {
481 	.attrs = max16065_basic_attributes,
482 	.is_visible = max16065_basic_is_visible,
483 };
484 
485 static const struct attribute_group max16065_current_group = {
486 	.attrs = max16065_current_attributes,
487 };
488 
489 static const struct attribute_group max16065_min_group = {
490 	.attrs = max16065_min_attributes,
491 	.is_visible = max16065_secondary_is_visible,
492 };
493 
494 static const struct attribute_group max16065_max_group = {
495 	.attrs = max16065_max_attributes,
496 	.is_visible = max16065_secondary_is_visible,
497 };
498 
499 static int max16065_probe(struct i2c_client *client,
500 			  const struct i2c_device_id *id)
501 {
502 	struct i2c_adapter *adapter = client->adapter;
503 	struct max16065_data *data;
504 	struct device *dev = &client->dev;
505 	struct device *hwmon_dev;
506 	int i, j, val;
507 	bool have_secondary;		/* true if chip has secondary limits */
508 	bool secondary_is_max = false;	/* secondary limits reflect max */
509 	int groups = 0;
510 
511 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
512 				     | I2C_FUNC_SMBUS_READ_WORD_DATA))
513 		return -ENODEV;
514 
515 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
516 	if (unlikely(!data))
517 		return -ENOMEM;
518 
519 	data->client = client;
520 	mutex_init(&data->update_lock);
521 
522 	data->num_adc = max16065_num_adc[id->driver_data];
523 	data->have_current = max16065_have_current[id->driver_data];
524 	have_secondary = max16065_have_secondary[id->driver_data];
525 
526 	if (have_secondary) {
527 		val = i2c_smbus_read_byte_data(client, MAX16065_SW_ENABLE);
528 		if (unlikely(val < 0))
529 			return val;
530 		secondary_is_max = val & MAX16065_WARNING_OV;
531 	}
532 
533 	/* Read scale registers, convert to range */
534 	for (i = 0; i < DIV_ROUND_UP(data->num_adc, 4); i++) {
535 		val = i2c_smbus_read_byte_data(client, MAX16065_SCALE(i));
536 		if (unlikely(val < 0))
537 			return val;
538 		for (j = 0; j < 4 && i * 4 + j < data->num_adc; j++) {
539 			data->range[i * 4 + j] =
540 			  max16065_adc_range[(val >> (j * 2)) & 0x3];
541 		}
542 	}
543 
544 	/* Read limits */
545 	for (i = 0; i < MAX16065_NUM_LIMIT; i++) {
546 		if (i == 0 && !have_secondary)
547 			continue;
548 
549 		for (j = 0; j < data->num_adc; j++) {
550 			val = i2c_smbus_read_byte_data(client,
551 						       MAX16065_LIMIT(i, j));
552 			if (unlikely(val < 0))
553 				return val;
554 			data->limit[i][j] = LIMIT_TO_MV(val, data->range[j]);
555 		}
556 	}
557 
558 	/* sysfs hooks */
559 	data->groups[groups++] = &max16065_basic_group;
560 	if (have_secondary)
561 		data->groups[groups++] = secondary_is_max ?
562 			&max16065_max_group : &max16065_min_group;
563 
564 	if (data->have_current) {
565 		val = i2c_smbus_read_byte_data(client, MAX16065_CURR_CONTROL);
566 		if (unlikely(val < 0))
567 			return val;
568 		if (val & MAX16065_CURR_ENABLE) {
569 			/*
570 			 * Current gain is 6, 12, 24, 48 based on values in
571 			 * bit 2,3.
572 			 */
573 			data->curr_gain = 6 << ((val >> 2) & 0x03);
574 			data->range[MAX16065_NUM_ADC]
575 			  = max16065_csp_adc_range[(val >> 1) & 0x01];
576 			data->groups[groups++] = &max16065_current_group;
577 		} else {
578 			data->have_current = false;
579 		}
580 	}
581 
582 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
583 							   data, data->groups);
584 	return PTR_ERR_OR_ZERO(hwmon_dev);
585 }
586 
587 static const struct i2c_device_id max16065_id[] = {
588 	{ "max16065", max16065 },
589 	{ "max16066", max16066 },
590 	{ "max16067", max16067 },
591 	{ "max16068", max16068 },
592 	{ "max16070", max16070 },
593 	{ "max16071", max16071 },
594 	{ }
595 };
596 
597 MODULE_DEVICE_TABLE(i2c, max16065_id);
598 
599 /* This is the driver that will be inserted */
600 static struct i2c_driver max16065_driver = {
601 	.driver = {
602 		.name = "max16065",
603 	},
604 	.probe = max16065_probe,
605 	.id_table = max16065_id,
606 };
607 
608 module_i2c_driver(max16065_driver);
609 
610 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
611 MODULE_DESCRIPTION("MAX16065 driver");
612 MODULE_LICENSE("GPL");
613