Linux Audio

Check our new training course

Loading...
v4.10.11
 
  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
 27enum 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
 57static 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
 66static 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
 75static 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
 84struct 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
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 clamp_val(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_swapped(client, reg);
141	if (unlikely(rv < 0))
142		return rv;
143	return rv >> 6;
144}
145
146static 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
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(data->client,
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 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
249static ssize_t max16065_show_limit(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 */
262static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, max16065_show_input, NULL, 0);
263static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, max16065_show_input, NULL, 1);
264static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, max16065_show_input, NULL, 2);
265static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, max16065_show_input, NULL, 3);
266static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, max16065_show_input, NULL, 4);
267static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, max16065_show_input, NULL, 5);
268static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, max16065_show_input, NULL, 6);
269static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, max16065_show_input, NULL, 7);
270static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, max16065_show_input, NULL, 8);
271static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, max16065_show_input, NULL, 9);
272static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, max16065_show_input, NULL, 10);
273static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, max16065_show_input, NULL, 11);
274static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, max16065_show_input, NULL, 12);
275
276/* Input voltages lcrit */
277static SENSOR_DEVICE_ATTR_2(in0_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
278			    max16065_set_limit, 2, 0);
279static SENSOR_DEVICE_ATTR_2(in1_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
280			    max16065_set_limit, 2, 1);
281static SENSOR_DEVICE_ATTR_2(in2_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
282			    max16065_set_limit, 2, 2);
283static SENSOR_DEVICE_ATTR_2(in3_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
284			    max16065_set_limit, 2, 3);
285static SENSOR_DEVICE_ATTR_2(in4_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
286			    max16065_set_limit, 2, 4);
287static SENSOR_DEVICE_ATTR_2(in5_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
288			    max16065_set_limit, 2, 5);
289static SENSOR_DEVICE_ATTR_2(in6_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
290			    max16065_set_limit, 2, 6);
291static SENSOR_DEVICE_ATTR_2(in7_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
292			    max16065_set_limit, 2, 7);
293static SENSOR_DEVICE_ATTR_2(in8_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
294			    max16065_set_limit, 2, 8);
295static SENSOR_DEVICE_ATTR_2(in9_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
296			    max16065_set_limit, 2, 9);
297static SENSOR_DEVICE_ATTR_2(in10_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
298			    max16065_set_limit, 2, 10);
299static SENSOR_DEVICE_ATTR_2(in11_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit,
300			    max16065_set_limit, 2, 11);
301
302/* Input voltages crit */
303static SENSOR_DEVICE_ATTR_2(in0_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
304			    max16065_set_limit, 1, 0);
305static SENSOR_DEVICE_ATTR_2(in1_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
306			    max16065_set_limit, 1, 1);
307static SENSOR_DEVICE_ATTR_2(in2_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
308			    max16065_set_limit, 1, 2);
309static SENSOR_DEVICE_ATTR_2(in3_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
310			    max16065_set_limit, 1, 3);
311static SENSOR_DEVICE_ATTR_2(in4_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
312			    max16065_set_limit, 1, 4);
313static SENSOR_DEVICE_ATTR_2(in5_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
314			    max16065_set_limit, 1, 5);
315static SENSOR_DEVICE_ATTR_2(in6_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
316			    max16065_set_limit, 1, 6);
317static SENSOR_DEVICE_ATTR_2(in7_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
318			    max16065_set_limit, 1, 7);
319static SENSOR_DEVICE_ATTR_2(in8_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
320			    max16065_set_limit, 1, 8);
321static SENSOR_DEVICE_ATTR_2(in9_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
322			    max16065_set_limit, 1, 9);
323static SENSOR_DEVICE_ATTR_2(in10_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
324			    max16065_set_limit, 1, 10);
325static SENSOR_DEVICE_ATTR_2(in11_crit, S_IWUSR | S_IRUGO, max16065_show_limit,
326			    max16065_set_limit, 1, 11);
327
328/* Input voltages min */
329static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, max16065_show_limit,
330			    max16065_set_limit, 0, 0);
331static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, max16065_show_limit,
332			    max16065_set_limit, 0, 1);
333static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, max16065_show_limit,
334			    max16065_set_limit, 0, 2);
335static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, max16065_show_limit,
336			    max16065_set_limit, 0, 3);
337static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, max16065_show_limit,
338			    max16065_set_limit, 0, 4);
339static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, max16065_show_limit,
340			    max16065_set_limit, 0, 5);
341static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, max16065_show_limit,
342			    max16065_set_limit, 0, 6);
343static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, max16065_show_limit,
344			    max16065_set_limit, 0, 7);
345static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, max16065_show_limit,
346			    max16065_set_limit, 0, 8);
347static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, max16065_show_limit,
348			    max16065_set_limit, 0, 9);
349static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, max16065_show_limit,
350			    max16065_set_limit, 0, 10);
351static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, max16065_show_limit,
352			    max16065_set_limit, 0, 11);
353
354/* Input voltages max */
355static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, max16065_show_limit,
356			    max16065_set_limit, 0, 0);
357static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, max16065_show_limit,
358			    max16065_set_limit, 0, 1);
359static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, max16065_show_limit,
360			    max16065_set_limit, 0, 2);
361static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, max16065_show_limit,
362			    max16065_set_limit, 0, 3);
363static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, max16065_show_limit,
364			    max16065_set_limit, 0, 4);
365static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, max16065_show_limit,
366			    max16065_set_limit, 0, 5);
367static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, max16065_show_limit,
368			    max16065_set_limit, 0, 6);
369static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, max16065_show_limit,
370			    max16065_set_limit, 0, 7);
371static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, max16065_show_limit,
372			    max16065_set_limit, 0, 8);
373static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, max16065_show_limit,
374			    max16065_set_limit, 0, 9);
375static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, max16065_show_limit,
376			    max16065_set_limit, 0, 10);
377static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, max16065_show_limit,
378			    max16065_set_limit, 0, 11);
379
380/* alarms */
381static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, max16065_show_alarm, NULL,
382			    0, 0);
383static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, max16065_show_alarm, NULL,
384			    0, 1);
385static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, max16065_show_alarm, NULL,
386			    0, 2);
387static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, max16065_show_alarm, NULL,
388			    0, 3);
389static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, max16065_show_alarm, NULL,
390			    0, 4);
391static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, max16065_show_alarm, NULL,
392			    0, 5);
393static SENSOR_DEVICE_ATTR_2(in6_alarm, S_IRUGO, max16065_show_alarm, NULL,
394			    0, 6);
395static SENSOR_DEVICE_ATTR_2(in7_alarm, S_IRUGO, max16065_show_alarm, NULL,
396			    0, 7);
397static SENSOR_DEVICE_ATTR_2(in8_alarm, S_IRUGO, max16065_show_alarm, NULL,
398			    1, 0);
399static SENSOR_DEVICE_ATTR_2(in9_alarm, S_IRUGO, max16065_show_alarm, NULL,
400			    1, 1);
401static SENSOR_DEVICE_ATTR_2(in10_alarm, S_IRUGO, max16065_show_alarm, NULL,
402			    1, 2);
403static SENSOR_DEVICE_ATTR_2(in11_alarm, S_IRUGO, max16065_show_alarm, NULL,
404			    1, 3);
405
406/* Current and alarm */
407static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, max16065_show_current, NULL, 0);
408static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, max16065_show_alarm, NULL,
409			    1, 4);
410
411/*
412 * Finally, construct an array of pointers to members of the above objects,
413 * as required for sysfs_create_group()
414 */
415static struct attribute *max16065_basic_attributes[] = {
416	&sensor_dev_attr_in0_input.dev_attr.attr,
417	&sensor_dev_attr_in0_lcrit.dev_attr.attr,
418	&sensor_dev_attr_in0_crit.dev_attr.attr,
419	&sensor_dev_attr_in0_alarm.dev_attr.attr,
420
421	&sensor_dev_attr_in1_input.dev_attr.attr,
422	&sensor_dev_attr_in1_lcrit.dev_attr.attr,
423	&sensor_dev_attr_in1_crit.dev_attr.attr,
424	&sensor_dev_attr_in1_alarm.dev_attr.attr,
425
426	&sensor_dev_attr_in2_input.dev_attr.attr,
427	&sensor_dev_attr_in2_lcrit.dev_attr.attr,
428	&sensor_dev_attr_in2_crit.dev_attr.attr,
429	&sensor_dev_attr_in2_alarm.dev_attr.attr,
430
431	&sensor_dev_attr_in3_input.dev_attr.attr,
432	&sensor_dev_attr_in3_lcrit.dev_attr.attr,
433	&sensor_dev_attr_in3_crit.dev_attr.attr,
434	&sensor_dev_attr_in3_alarm.dev_attr.attr,
435
436	&sensor_dev_attr_in4_input.dev_attr.attr,
437	&sensor_dev_attr_in4_lcrit.dev_attr.attr,
438	&sensor_dev_attr_in4_crit.dev_attr.attr,
439	&sensor_dev_attr_in4_alarm.dev_attr.attr,
440
441	&sensor_dev_attr_in5_input.dev_attr.attr,
442	&sensor_dev_attr_in5_lcrit.dev_attr.attr,
443	&sensor_dev_attr_in5_crit.dev_attr.attr,
444	&sensor_dev_attr_in5_alarm.dev_attr.attr,
445
446	&sensor_dev_attr_in6_input.dev_attr.attr,
447	&sensor_dev_attr_in6_lcrit.dev_attr.attr,
448	&sensor_dev_attr_in6_crit.dev_attr.attr,
449	&sensor_dev_attr_in6_alarm.dev_attr.attr,
450
451	&sensor_dev_attr_in7_input.dev_attr.attr,
452	&sensor_dev_attr_in7_lcrit.dev_attr.attr,
453	&sensor_dev_attr_in7_crit.dev_attr.attr,
454	&sensor_dev_attr_in7_alarm.dev_attr.attr,
455
456	&sensor_dev_attr_in8_input.dev_attr.attr,
457	&sensor_dev_attr_in8_lcrit.dev_attr.attr,
458	&sensor_dev_attr_in8_crit.dev_attr.attr,
459	&sensor_dev_attr_in8_alarm.dev_attr.attr,
460
461	&sensor_dev_attr_in9_input.dev_attr.attr,
462	&sensor_dev_attr_in9_lcrit.dev_attr.attr,
463	&sensor_dev_attr_in9_crit.dev_attr.attr,
464	&sensor_dev_attr_in9_alarm.dev_attr.attr,
465
466	&sensor_dev_attr_in10_input.dev_attr.attr,
467	&sensor_dev_attr_in10_lcrit.dev_attr.attr,
468	&sensor_dev_attr_in10_crit.dev_attr.attr,
469	&sensor_dev_attr_in10_alarm.dev_attr.attr,
470
471	&sensor_dev_attr_in11_input.dev_attr.attr,
472	&sensor_dev_attr_in11_lcrit.dev_attr.attr,
473	&sensor_dev_attr_in11_crit.dev_attr.attr,
474	&sensor_dev_attr_in11_alarm.dev_attr.attr,
475
476	NULL
477};
478
479static struct attribute *max16065_current_attributes[] = {
480	&sensor_dev_attr_in12_input.dev_attr.attr,
481	&sensor_dev_attr_curr1_input.dev_attr.attr,
482	&sensor_dev_attr_curr1_alarm.dev_attr.attr,
483	NULL
484};
485
486static struct attribute *max16065_min_attributes[] = {
487	&sensor_dev_attr_in0_min.dev_attr.attr,
488	&sensor_dev_attr_in1_min.dev_attr.attr,
489	&sensor_dev_attr_in2_min.dev_attr.attr,
490	&sensor_dev_attr_in3_min.dev_attr.attr,
491	&sensor_dev_attr_in4_min.dev_attr.attr,
492	&sensor_dev_attr_in5_min.dev_attr.attr,
493	&sensor_dev_attr_in6_min.dev_attr.attr,
494	&sensor_dev_attr_in7_min.dev_attr.attr,
495	&sensor_dev_attr_in8_min.dev_attr.attr,
496	&sensor_dev_attr_in9_min.dev_attr.attr,
497	&sensor_dev_attr_in10_min.dev_attr.attr,
498	&sensor_dev_attr_in11_min.dev_attr.attr,
499	NULL
500};
501
502static struct attribute *max16065_max_attributes[] = {
503	&sensor_dev_attr_in0_max.dev_attr.attr,
504	&sensor_dev_attr_in1_max.dev_attr.attr,
505	&sensor_dev_attr_in2_max.dev_attr.attr,
506	&sensor_dev_attr_in3_max.dev_attr.attr,
507	&sensor_dev_attr_in4_max.dev_attr.attr,
508	&sensor_dev_attr_in5_max.dev_attr.attr,
509	&sensor_dev_attr_in6_max.dev_attr.attr,
510	&sensor_dev_attr_in7_max.dev_attr.attr,
511	&sensor_dev_attr_in8_max.dev_attr.attr,
512	&sensor_dev_attr_in9_max.dev_attr.attr,
513	&sensor_dev_attr_in10_max.dev_attr.attr,
514	&sensor_dev_attr_in11_max.dev_attr.attr,
515	NULL
516};
517
518static umode_t max16065_basic_is_visible(struct kobject *kobj,
519					 struct attribute *a, int n)
520{
521	struct device *dev = container_of(kobj, struct device, kobj);
522	struct max16065_data *data = dev_get_drvdata(dev);
523	int index = n / 4;
524
525	if (index >= data->num_adc || !data->range[index])
526		return 0;
527	return a->mode;
528}
529
530static umode_t max16065_secondary_is_visible(struct kobject *kobj,
531					     struct attribute *a, int index)
532{
533	struct device *dev = container_of(kobj, struct device, kobj);
534	struct max16065_data *data = dev_get_drvdata(dev);
535
536	if (index >= data->num_adc)
537		return 0;
538	return a->mode;
539}
540
541static const struct attribute_group max16065_basic_group = {
542	.attrs = max16065_basic_attributes,
543	.is_visible = max16065_basic_is_visible,
544};
545
546static const struct attribute_group max16065_current_group = {
547	.attrs = max16065_current_attributes,
548};
549
550static const struct attribute_group max16065_min_group = {
551	.attrs = max16065_min_attributes,
552	.is_visible = max16065_secondary_is_visible,
553};
554
555static const struct attribute_group max16065_max_group = {
556	.attrs = max16065_max_attributes,
557	.is_visible = max16065_secondary_is_visible,
558};
559
560static int max16065_probe(struct i2c_client *client,
561			  const struct i2c_device_id *id)
562{
563	struct i2c_adapter *adapter = client->adapter;
564	struct max16065_data *data;
565	struct device *dev = &client->dev;
566	struct device *hwmon_dev;
567	int i, j, val;
568	bool have_secondary;		/* true if chip has secondary limits */
569	bool secondary_is_max = false;	/* secondary limits reflect max */
570	int groups = 0;
 
571
572	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
573				     | I2C_FUNC_SMBUS_READ_WORD_DATA))
574		return -ENODEV;
575
576	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
577	if (unlikely(!data))
578		return -ENOMEM;
579
 
580	data->client = client;
581	mutex_init(&data->update_lock);
582
583	data->num_adc = max16065_num_adc[id->driver_data];
584	data->have_current = max16065_have_current[id->driver_data];
585	have_secondary = max16065_have_secondary[id->driver_data];
586
587	if (have_secondary) {
588		val = i2c_smbus_read_byte_data(client, MAX16065_SW_ENABLE);
589		if (unlikely(val < 0))
590			return val;
591		secondary_is_max = val & MAX16065_WARNING_OV;
592	}
593
594	/* Read scale registers, convert to range */
595	for (i = 0; i < DIV_ROUND_UP(data->num_adc, 4); i++) {
596		val = i2c_smbus_read_byte_data(client, MAX16065_SCALE(i));
597		if (unlikely(val < 0))
598			return val;
599		for (j = 0; j < 4 && i * 4 + j < data->num_adc; j++) {
600			data->range[i * 4 + j] =
601			  max16065_adc_range[(val >> (j * 2)) & 0x3];
602		}
603	}
604
605	/* Read limits */
606	for (i = 0; i < MAX16065_NUM_LIMIT; i++) {
607		if (i == 0 && !have_secondary)
608			continue;
609
610		for (j = 0; j < data->num_adc; j++) {
611			val = i2c_smbus_read_byte_data(client,
612						       MAX16065_LIMIT(i, j));
613			if (unlikely(val < 0))
614				return val;
615			data->limit[i][j] = LIMIT_TO_MV(val, data->range[j]);
616		}
617	}
618
619	/* sysfs hooks */
620	data->groups[groups++] = &max16065_basic_group;
621	if (have_secondary)
622		data->groups[groups++] = secondary_is_max ?
623			&max16065_max_group : &max16065_min_group;
624
625	if (data->have_current) {
626		val = i2c_smbus_read_byte_data(client, MAX16065_CURR_CONTROL);
627		if (unlikely(val < 0))
628			return val;
629		if (val & MAX16065_CURR_ENABLE) {
630			/*
631			 * Current gain is 6, 12, 24, 48 based on values in
632			 * bit 2,3.
633			 */
634			data->curr_gain = 6 << ((val >> 2) & 0x03);
635			data->range[MAX16065_NUM_ADC]
636			  = max16065_csp_adc_range[(val >> 1) & 0x01];
637			data->groups[groups++] = &max16065_current_group;
638		} else {
639			data->have_current = false;
640		}
641	}
642
643	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
644							   data, data->groups);
645	return PTR_ERR_OR_ZERO(hwmon_dev);
646}
647
648static const struct i2c_device_id max16065_id[] = {
649	{ "max16065", max16065 },
650	{ "max16066", max16066 },
651	{ "max16067", max16067 },
652	{ "max16068", max16068 },
653	{ "max16070", max16070 },
654	{ "max16071", max16071 },
655	{ }
656};
657
658MODULE_DEVICE_TABLE(i2c, max16065_id);
659
660/* This is the driver that will be inserted */
661static struct i2c_driver max16065_driver = {
662	.driver = {
663		.name = "max16065",
664	},
665	.probe = max16065_probe,
666	.id_table = max16065_id,
667};
668
669module_i2c_driver(max16065_driver);
670
671MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
672MODULE_DESCRIPTION("MAX16065 driver");
673MODULE_LICENSE("GPL");
v6.13.7
  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 chip;
 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(unsigned long mv, int range)
118{
119	mv = clamp_val(mv, 0, ULONG_MAX / 256);
120	return DIV_ROUND_CLOSEST(clamp_val(mv * 256, 0, range * 255), range);
121}
122
123static inline int ADC_TO_CURR(int adc, int gain)
124{
125	return adc * 1400000 / (gain * 255);
126}
127
128/*
129 * max16065_read_adc()
130 *
131 * Read 16 bit value from <reg>, <reg+1>.
132 * Upper 8 bits are in <reg>, lower 2 bits are in bits 7:6 of <reg+1>.
133 */
134static int max16065_read_adc(struct i2c_client *client, int reg)
135{
136	int rv;
137
138	rv = i2c_smbus_read_word_swapped(client, reg);
139	if (unlikely(rv < 0))
140		return rv;
141	return rv >> 6;
142}
143
144static struct max16065_data *max16065_update_device(struct device *dev)
145{
146	struct max16065_data *data = dev_get_drvdata(dev);
147	struct i2c_client *client = data->client;
148
149	mutex_lock(&data->update_lock);
150	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
151		int i;
152
153		for (i = 0; i < data->num_adc; i++)
154			data->adc[i]
155			  = max16065_read_adc(client, MAX16065_ADC(i));
156
157		if (data->have_current) {
158			data->adc[MAX16065_NUM_ADC]
159			  = max16065_read_adc(client, MAX16065_CSP_ADC);
160			data->curr_sense
161			  = i2c_smbus_read_byte_data(client,
162						     MAX16065_CURR_SENSE);
163		}
164
165		for (i = 0; i < 2; i++)
166			data->fault[i]
167			  = i2c_smbus_read_byte_data(client, MAX16065_FAULT(i));
168
169		/*
170		 * MAX16067 and MAX16068 have separate undervoltage and
171		 * overvoltage alarm bits. Squash them together.
172		 */
173		if (data->chip == max16067 || data->chip == max16068)
174			data->fault[0] |= data->fault[1];
175
176		data->last_updated = jiffies;
177		data->valid = true;
178	}
179	mutex_unlock(&data->update_lock);
180	return data;
181}
182
183static ssize_t max16065_alarm_show(struct device *dev,
184				   struct device_attribute *da, char *buf)
185{
186	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
187	struct max16065_data *data = max16065_update_device(dev);
188	int val = data->fault[attr2->nr];
189
190	if (val < 0)
191		return val;
192
193	val &= (1 << attr2->index);
194	if (val)
195		i2c_smbus_write_byte_data(data->client,
196					  MAX16065_FAULT(attr2->nr), val);
197
198	return sysfs_emit(buf, "%d\n", !!val);
199}
200
201static ssize_t max16065_input_show(struct device *dev,
202				   struct device_attribute *da, char *buf)
203{
204	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
205	struct max16065_data *data = max16065_update_device(dev);
206	int adc = data->adc[attr->index];
207
208	if (unlikely(adc < 0))
209		return adc;
210
211	return sysfs_emit(buf, "%d\n",
212			  ADC_TO_MV(adc, data->range[attr->index]));
213}
214
215static ssize_t max16065_current_show(struct device *dev,
216				     struct device_attribute *da, char *buf)
217{
218	struct max16065_data *data = max16065_update_device(dev);
219
220	if (unlikely(data->curr_sense < 0))
221		return data->curr_sense;
222
223	return sysfs_emit(buf, "%d\n",
224			  ADC_TO_CURR(data->curr_sense, data->curr_gain));
225}
226
227static ssize_t max16065_limit_store(struct device *dev,
228				    struct device_attribute *da,
229				    const char *buf, size_t count)
230{
231	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
232	struct max16065_data *data = dev_get_drvdata(dev);
233	unsigned long val;
234	int err;
235	int limit;
236
237	err = kstrtoul(buf, 10, &val);
238	if (unlikely(err < 0))
239		return err;
240
241	limit = MV_TO_LIMIT(val, data->range[attr2->index]);
242
243	mutex_lock(&data->update_lock);
244	data->limit[attr2->nr][attr2->index]
245	  = LIMIT_TO_MV(limit, data->range[attr2->index]);
246	i2c_smbus_write_byte_data(data->client,
247				  MAX16065_LIMIT(attr2->nr, attr2->index),
248				  limit);
249	mutex_unlock(&data->update_lock);
250
251	return count;
252}
253
254static ssize_t max16065_limit_show(struct device *dev,
255				   struct device_attribute *da, char *buf)
256{
257	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
258	struct max16065_data *data = dev_get_drvdata(dev);
259
260	return sysfs_emit(buf, "%d\n",
261			  data->limit[attr2->nr][attr2->index]);
262}
263
264/* Construct a sensor_device_attribute structure for each register */
265
266/* Input voltages */
267static SENSOR_DEVICE_ATTR_RO(in0_input, max16065_input, 0);
268static SENSOR_DEVICE_ATTR_RO(in1_input, max16065_input, 1);
269static SENSOR_DEVICE_ATTR_RO(in2_input, max16065_input, 2);
270static SENSOR_DEVICE_ATTR_RO(in3_input, max16065_input, 3);
271static SENSOR_DEVICE_ATTR_RO(in4_input, max16065_input, 4);
272static SENSOR_DEVICE_ATTR_RO(in5_input, max16065_input, 5);
273static SENSOR_DEVICE_ATTR_RO(in6_input, max16065_input, 6);
274static SENSOR_DEVICE_ATTR_RO(in7_input, max16065_input, 7);
275static SENSOR_DEVICE_ATTR_RO(in8_input, max16065_input, 8);
276static SENSOR_DEVICE_ATTR_RO(in9_input, max16065_input, 9);
277static SENSOR_DEVICE_ATTR_RO(in10_input, max16065_input, 10);
278static SENSOR_DEVICE_ATTR_RO(in11_input, max16065_input, 11);
279static SENSOR_DEVICE_ATTR_RO(in12_input, max16065_input, 12);
280
281/* Input voltages lcrit */
282static SENSOR_DEVICE_ATTR_2_RW(in0_lcrit, max16065_limit, 2, 0);
283static SENSOR_DEVICE_ATTR_2_RW(in1_lcrit, max16065_limit, 2, 1);
284static SENSOR_DEVICE_ATTR_2_RW(in2_lcrit, max16065_limit, 2, 2);
285static SENSOR_DEVICE_ATTR_2_RW(in3_lcrit, max16065_limit, 2, 3);
286static SENSOR_DEVICE_ATTR_2_RW(in4_lcrit, max16065_limit, 2, 4);
287static SENSOR_DEVICE_ATTR_2_RW(in5_lcrit, max16065_limit, 2, 5);
288static SENSOR_DEVICE_ATTR_2_RW(in6_lcrit, max16065_limit, 2, 6);
289static SENSOR_DEVICE_ATTR_2_RW(in7_lcrit, max16065_limit, 2, 7);
290static SENSOR_DEVICE_ATTR_2_RW(in8_lcrit, max16065_limit, 2, 8);
291static SENSOR_DEVICE_ATTR_2_RW(in9_lcrit, max16065_limit, 2, 9);
292static SENSOR_DEVICE_ATTR_2_RW(in10_lcrit, max16065_limit, 2, 10);
293static SENSOR_DEVICE_ATTR_2_RW(in11_lcrit, max16065_limit, 2, 11);
 
 
 
 
 
 
 
 
 
 
 
 
294
295/* Input voltages crit */
296static SENSOR_DEVICE_ATTR_2_RW(in0_crit, max16065_limit, 1, 0);
297static SENSOR_DEVICE_ATTR_2_RW(in1_crit, max16065_limit, 1, 1);
298static SENSOR_DEVICE_ATTR_2_RW(in2_crit, max16065_limit, 1, 2);
299static SENSOR_DEVICE_ATTR_2_RW(in3_crit, max16065_limit, 1, 3);
300static SENSOR_DEVICE_ATTR_2_RW(in4_crit, max16065_limit, 1, 4);
301static SENSOR_DEVICE_ATTR_2_RW(in5_crit, max16065_limit, 1, 5);
302static SENSOR_DEVICE_ATTR_2_RW(in6_crit, max16065_limit, 1, 6);
303static SENSOR_DEVICE_ATTR_2_RW(in7_crit, max16065_limit, 1, 7);
304static SENSOR_DEVICE_ATTR_2_RW(in8_crit, max16065_limit, 1, 8);
305static SENSOR_DEVICE_ATTR_2_RW(in9_crit, max16065_limit, 1, 9);
306static SENSOR_DEVICE_ATTR_2_RW(in10_crit, max16065_limit, 1, 10);
307static SENSOR_DEVICE_ATTR_2_RW(in11_crit, max16065_limit, 1, 11);
 
 
 
 
 
 
 
 
 
 
 
 
308
309/* Input voltages min */
310static SENSOR_DEVICE_ATTR_2_RW(in0_min, max16065_limit, 0, 0);
311static SENSOR_DEVICE_ATTR_2_RW(in1_min, max16065_limit, 0, 1);
312static SENSOR_DEVICE_ATTR_2_RW(in2_min, max16065_limit, 0, 2);
313static SENSOR_DEVICE_ATTR_2_RW(in3_min, max16065_limit, 0, 3);
314static SENSOR_DEVICE_ATTR_2_RW(in4_min, max16065_limit, 0, 4);
315static SENSOR_DEVICE_ATTR_2_RW(in5_min, max16065_limit, 0, 5);
316static SENSOR_DEVICE_ATTR_2_RW(in6_min, max16065_limit, 0, 6);
317static SENSOR_DEVICE_ATTR_2_RW(in7_min, max16065_limit, 0, 7);
318static SENSOR_DEVICE_ATTR_2_RW(in8_min, max16065_limit, 0, 8);
319static SENSOR_DEVICE_ATTR_2_RW(in9_min, max16065_limit, 0, 9);
320static SENSOR_DEVICE_ATTR_2_RW(in10_min, max16065_limit, 0, 10);
321static SENSOR_DEVICE_ATTR_2_RW(in11_min, max16065_limit, 0, 11);
 
 
 
 
 
 
 
 
 
 
 
 
322
323/* Input voltages max */
324static SENSOR_DEVICE_ATTR_2_RW(in0_max, max16065_limit, 0, 0);
325static SENSOR_DEVICE_ATTR_2_RW(in1_max, max16065_limit, 0, 1);
326static SENSOR_DEVICE_ATTR_2_RW(in2_max, max16065_limit, 0, 2);
327static SENSOR_DEVICE_ATTR_2_RW(in3_max, max16065_limit, 0, 3);
328static SENSOR_DEVICE_ATTR_2_RW(in4_max, max16065_limit, 0, 4);
329static SENSOR_DEVICE_ATTR_2_RW(in5_max, max16065_limit, 0, 5);
330static SENSOR_DEVICE_ATTR_2_RW(in6_max, max16065_limit, 0, 6);
331static SENSOR_DEVICE_ATTR_2_RW(in7_max, max16065_limit, 0, 7);
332static SENSOR_DEVICE_ATTR_2_RW(in8_max, max16065_limit, 0, 8);
333static SENSOR_DEVICE_ATTR_2_RW(in9_max, max16065_limit, 0, 9);
334static SENSOR_DEVICE_ATTR_2_RW(in10_max, max16065_limit, 0, 10);
335static SENSOR_DEVICE_ATTR_2_RW(in11_max, max16065_limit, 0, 11);
 
 
 
 
 
 
 
 
 
 
 
 
336
337/* alarms */
338static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, max16065_alarm, 0, 0);
339static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, max16065_alarm, 0, 1);
340static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, max16065_alarm, 0, 2);
341static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, max16065_alarm, 0, 3);
342static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, max16065_alarm, 0, 4);
343static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, max16065_alarm, 0, 5);
344static SENSOR_DEVICE_ATTR_2_RO(in6_alarm, max16065_alarm, 0, 6);
345static SENSOR_DEVICE_ATTR_2_RO(in7_alarm, max16065_alarm, 0, 7);
346static SENSOR_DEVICE_ATTR_2_RO(in8_alarm, max16065_alarm, 1, 0);
347static SENSOR_DEVICE_ATTR_2_RO(in9_alarm, max16065_alarm, 1, 1);
348static SENSOR_DEVICE_ATTR_2_RO(in10_alarm, max16065_alarm, 1, 2);
349static SENSOR_DEVICE_ATTR_2_RO(in11_alarm, max16065_alarm, 1, 3);
 
 
 
 
 
 
 
 
 
 
 
 
350
351/* Current and alarm */
352static SENSOR_DEVICE_ATTR_RO(curr1_input, max16065_current, 0);
353static SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, max16065_alarm, 1, 4);
 
354
355/*
356 * Finally, construct an array of pointers to members of the above objects,
357 * as required for sysfs_create_group()
358 */
359static struct attribute *max16065_basic_attributes[] = {
360	&sensor_dev_attr_in0_input.dev_attr.attr,
361	&sensor_dev_attr_in0_lcrit.dev_attr.attr,
362	&sensor_dev_attr_in0_crit.dev_attr.attr,
363	&sensor_dev_attr_in0_alarm.dev_attr.attr,
364
365	&sensor_dev_attr_in1_input.dev_attr.attr,
366	&sensor_dev_attr_in1_lcrit.dev_attr.attr,
367	&sensor_dev_attr_in1_crit.dev_attr.attr,
368	&sensor_dev_attr_in1_alarm.dev_attr.attr,
369
370	&sensor_dev_attr_in2_input.dev_attr.attr,
371	&sensor_dev_attr_in2_lcrit.dev_attr.attr,
372	&sensor_dev_attr_in2_crit.dev_attr.attr,
373	&sensor_dev_attr_in2_alarm.dev_attr.attr,
374
375	&sensor_dev_attr_in3_input.dev_attr.attr,
376	&sensor_dev_attr_in3_lcrit.dev_attr.attr,
377	&sensor_dev_attr_in3_crit.dev_attr.attr,
378	&sensor_dev_attr_in3_alarm.dev_attr.attr,
379
380	&sensor_dev_attr_in4_input.dev_attr.attr,
381	&sensor_dev_attr_in4_lcrit.dev_attr.attr,
382	&sensor_dev_attr_in4_crit.dev_attr.attr,
383	&sensor_dev_attr_in4_alarm.dev_attr.attr,
384
385	&sensor_dev_attr_in5_input.dev_attr.attr,
386	&sensor_dev_attr_in5_lcrit.dev_attr.attr,
387	&sensor_dev_attr_in5_crit.dev_attr.attr,
388	&sensor_dev_attr_in5_alarm.dev_attr.attr,
389
390	&sensor_dev_attr_in6_input.dev_attr.attr,
391	&sensor_dev_attr_in6_lcrit.dev_attr.attr,
392	&sensor_dev_attr_in6_crit.dev_attr.attr,
393	&sensor_dev_attr_in6_alarm.dev_attr.attr,
394
395	&sensor_dev_attr_in7_input.dev_attr.attr,
396	&sensor_dev_attr_in7_lcrit.dev_attr.attr,
397	&sensor_dev_attr_in7_crit.dev_attr.attr,
398	&sensor_dev_attr_in7_alarm.dev_attr.attr,
399
400	&sensor_dev_attr_in8_input.dev_attr.attr,
401	&sensor_dev_attr_in8_lcrit.dev_attr.attr,
402	&sensor_dev_attr_in8_crit.dev_attr.attr,
403	&sensor_dev_attr_in8_alarm.dev_attr.attr,
404
405	&sensor_dev_attr_in9_input.dev_attr.attr,
406	&sensor_dev_attr_in9_lcrit.dev_attr.attr,
407	&sensor_dev_attr_in9_crit.dev_attr.attr,
408	&sensor_dev_attr_in9_alarm.dev_attr.attr,
409
410	&sensor_dev_attr_in10_input.dev_attr.attr,
411	&sensor_dev_attr_in10_lcrit.dev_attr.attr,
412	&sensor_dev_attr_in10_crit.dev_attr.attr,
413	&sensor_dev_attr_in10_alarm.dev_attr.attr,
414
415	&sensor_dev_attr_in11_input.dev_attr.attr,
416	&sensor_dev_attr_in11_lcrit.dev_attr.attr,
417	&sensor_dev_attr_in11_crit.dev_attr.attr,
418	&sensor_dev_attr_in11_alarm.dev_attr.attr,
419
420	NULL
421};
422
423static struct attribute *max16065_current_attributes[] = {
424	&sensor_dev_attr_in12_input.dev_attr.attr,
425	&sensor_dev_attr_curr1_input.dev_attr.attr,
426	&sensor_dev_attr_curr1_alarm.dev_attr.attr,
427	NULL
428};
429
430static struct attribute *max16065_min_attributes[] = {
431	&sensor_dev_attr_in0_min.dev_attr.attr,
432	&sensor_dev_attr_in1_min.dev_attr.attr,
433	&sensor_dev_attr_in2_min.dev_attr.attr,
434	&sensor_dev_attr_in3_min.dev_attr.attr,
435	&sensor_dev_attr_in4_min.dev_attr.attr,
436	&sensor_dev_attr_in5_min.dev_attr.attr,
437	&sensor_dev_attr_in6_min.dev_attr.attr,
438	&sensor_dev_attr_in7_min.dev_attr.attr,
439	&sensor_dev_attr_in8_min.dev_attr.attr,
440	&sensor_dev_attr_in9_min.dev_attr.attr,
441	&sensor_dev_attr_in10_min.dev_attr.attr,
442	&sensor_dev_attr_in11_min.dev_attr.attr,
443	NULL
444};
445
446static struct attribute *max16065_max_attributes[] = {
447	&sensor_dev_attr_in0_max.dev_attr.attr,
448	&sensor_dev_attr_in1_max.dev_attr.attr,
449	&sensor_dev_attr_in2_max.dev_attr.attr,
450	&sensor_dev_attr_in3_max.dev_attr.attr,
451	&sensor_dev_attr_in4_max.dev_attr.attr,
452	&sensor_dev_attr_in5_max.dev_attr.attr,
453	&sensor_dev_attr_in6_max.dev_attr.attr,
454	&sensor_dev_attr_in7_max.dev_attr.attr,
455	&sensor_dev_attr_in8_max.dev_attr.attr,
456	&sensor_dev_attr_in9_max.dev_attr.attr,
457	&sensor_dev_attr_in10_max.dev_attr.attr,
458	&sensor_dev_attr_in11_max.dev_attr.attr,
459	NULL
460};
461
462static umode_t max16065_basic_is_visible(struct kobject *kobj,
463					 struct attribute *a, int n)
464{
465	struct device *dev = kobj_to_dev(kobj);
466	struct max16065_data *data = dev_get_drvdata(dev);
467	int index = n / 4;
468
469	if (index >= data->num_adc || !data->range[index])
470		return 0;
471	return a->mode;
472}
473
474static umode_t max16065_secondary_is_visible(struct kobject *kobj,
475					     struct attribute *a, int index)
476{
477	struct device *dev = kobj_to_dev(kobj);
478	struct max16065_data *data = dev_get_drvdata(dev);
479
480	if (index >= data->num_adc)
481		return 0;
482	return a->mode;
483}
484
485static const struct attribute_group max16065_basic_group = {
486	.attrs = max16065_basic_attributes,
487	.is_visible = max16065_basic_is_visible,
488};
489
490static const struct attribute_group max16065_current_group = {
491	.attrs = max16065_current_attributes,
492};
493
494static const struct attribute_group max16065_min_group = {
495	.attrs = max16065_min_attributes,
496	.is_visible = max16065_secondary_is_visible,
497};
498
499static const struct attribute_group max16065_max_group = {
500	.attrs = max16065_max_attributes,
501	.is_visible = max16065_secondary_is_visible,
502};
503
504static int max16065_probe(struct i2c_client *client)
 
505{
506	struct i2c_adapter *adapter = client->adapter;
507	struct max16065_data *data;
508	struct device *dev = &client->dev;
509	struct device *hwmon_dev;
510	int i, j, val;
511	bool have_secondary;		/* true if chip has secondary limits */
512	bool secondary_is_max = false;	/* secondary limits reflect max */
513	int groups = 0;
514	enum chips chip = (uintptr_t)i2c_get_match_data(client);
515
516	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
517				     | I2C_FUNC_SMBUS_READ_WORD_DATA))
518		return -ENODEV;
519
520	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
521	if (unlikely(!data))
522		return -ENOMEM;
523
524	data->chip = chip;
525	data->client = client;
526	mutex_init(&data->update_lock);
527
528	data->num_adc = max16065_num_adc[chip];
529	data->have_current = max16065_have_current[chip];
530	have_secondary = max16065_have_secondary[chip];
531
532	if (have_secondary) {
533		val = i2c_smbus_read_byte_data(client, MAX16065_SW_ENABLE);
534		if (unlikely(val < 0))
535			return val;
536		secondary_is_max = val & MAX16065_WARNING_OV;
537	}
538
539	/* Read scale registers, convert to range */
540	for (i = 0; i < DIV_ROUND_UP(data->num_adc, 4); i++) {
541		val = i2c_smbus_read_byte_data(client, MAX16065_SCALE(i));
542		if (unlikely(val < 0))
543			return val;
544		for (j = 0; j < 4 && i * 4 + j < data->num_adc; j++) {
545			data->range[i * 4 + j] =
546			  max16065_adc_range[(val >> (j * 2)) & 0x3];
547		}
548	}
549
550	/* Read limits */
551	for (i = 0; i < MAX16065_NUM_LIMIT; i++) {
552		if (i == 0 && !have_secondary)
553			continue;
554
555		for (j = 0; j < data->num_adc; j++) {
556			val = i2c_smbus_read_byte_data(client,
557						       MAX16065_LIMIT(i, j));
558			if (unlikely(val < 0))
559				return val;
560			data->limit[i][j] = LIMIT_TO_MV(val, data->range[j]);
561		}
562	}
563
564	/* sysfs hooks */
565	data->groups[groups++] = &max16065_basic_group;
566	if (have_secondary)
567		data->groups[groups++] = secondary_is_max ?
568			&max16065_max_group : &max16065_min_group;
569
570	if (data->have_current) {
571		val = i2c_smbus_read_byte_data(client, MAX16065_CURR_CONTROL);
572		if (unlikely(val < 0))
573			return val;
574		if (val & MAX16065_CURR_ENABLE) {
575			/*
576			 * Current gain is 6, 12, 24, 48 based on values in
577			 * bit 2,3.
578			 */
579			data->curr_gain = 6 << ((val >> 2) & 0x03);
580			data->range[MAX16065_NUM_ADC]
581			  = max16065_csp_adc_range[(val >> 1) & 0x01];
582			data->groups[groups++] = &max16065_current_group;
583		} else {
584			data->have_current = false;
585		}
586	}
587
588	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
589							   data, data->groups);
590	return PTR_ERR_OR_ZERO(hwmon_dev);
591}
592
593static const struct i2c_device_id max16065_id[] = {
594	{ "max16065", max16065 },
595	{ "max16066", max16066 },
596	{ "max16067", max16067 },
597	{ "max16068", max16068 },
598	{ "max16070", max16070 },
599	{ "max16071", max16071 },
600	{ }
601};
602
603MODULE_DEVICE_TABLE(i2c, max16065_id);
604
605/* This is the driver that will be inserted */
606static struct i2c_driver max16065_driver = {
607	.driver = {
608		.name = "max16065",
609	},
610	.probe = max16065_probe,
611	.id_table = max16065_id,
612};
613
614module_i2c_driver(max16065_driver);
615
616MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
617MODULE_DESCRIPTION("MAX16065 driver");
618MODULE_LICENSE("GPL");