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