2 * powr1220.c - Driver for the Lattice POWR1220 programmable power supply
3 * and monitor. Users can read all ADC inputs along with their labels
4 * using the sysfs nodes.
6 * Copyright (c) 2014 Echo360 http://www.echo360.com
7 * Scott Kanowitz <skanowitz@echo360.com> <scott.kanowitz@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
32 #define ADC_MAX_LOW_MEASUREMENT_MV 2000
65 enum powr1220_adc_values
{
80 MAX_POWR1220_ADC_VALUES
83 struct powr1220_data
{
84 struct i2c_client
*client
;
85 struct mutex update_lock
;
86 bool adc_valid
[MAX_POWR1220_ADC_VALUES
];
87 /* the next value is in jiffies */
88 unsigned long adc_last_updated
[MAX_POWR1220_ADC_VALUES
];
91 int adc_maxes
[MAX_POWR1220_ADC_VALUES
];
92 int adc_values
[MAX_POWR1220_ADC_VALUES
];
95 static const char * const input_names
[] = {
112 /* Reads the specified ADC channel */
113 static int powr1220_read_adc(struct device
*dev
, int ch_num
)
115 struct powr1220_data
*data
= dev_get_drvdata(dev
);
120 mutex_lock(&data
->update_lock
);
122 if (time_after(jiffies
, data
->adc_last_updated
[ch_num
] + HZ
) ||
123 !data
->adc_valid
[ch_num
]) {
125 * figure out if we need to use the attenuator for
126 * high inputs or inputs that we don't yet have a measurement
127 * for. We dynamically set the attenuator depending on the
130 if (data
->adc_maxes
[ch_num
] > ADC_MAX_LOW_MEASUREMENT_MV
||
131 data
->adc_maxes
[ch_num
] == 0)
134 /* set the attenuator and mux */
135 result
= i2c_smbus_write_byte_data(data
->client
, ADC_MUX
,
141 * wait at least Tconvert time (200 us) for the
142 * conversion to complete
146 /* get the ADC reading */
147 result
= i2c_smbus_read_byte_data(data
->client
, ADC_VALUE_LOW
);
151 reading
= result
>> 4;
153 /* get the upper half of the reading */
154 result
= i2c_smbus_read_byte_data(data
->client
, ADC_VALUE_HIGH
);
158 reading
|= result
<< 4;
160 /* now convert the reading to a voltage */
161 reading
*= ADC_STEP_MV
;
162 data
->adc_values
[ch_num
] = reading
;
163 data
->adc_valid
[ch_num
] = true;
164 data
->adc_last_updated
[ch_num
] = jiffies
;
167 if (reading
> data
->adc_maxes
[ch_num
])
168 data
->adc_maxes
[ch_num
] = reading
;
170 result
= data
->adc_values
[ch_num
];
174 mutex_unlock(&data
->update_lock
);
179 /* Shows the voltage associated with the specified ADC channel */
180 static ssize_t
powr1220_show_voltage(struct device
*dev
,
181 struct device_attribute
*dev_attr
, char *buf
)
183 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(dev_attr
);
184 int adc_val
= powr1220_read_adc(dev
, attr
->index
);
189 return sprintf(buf
, "%d\n", adc_val
);
192 /* Shows the maximum setting associated with the specified ADC channel */
193 static ssize_t
powr1220_show_max(struct device
*dev
,
194 struct device_attribute
*dev_attr
, char *buf
)
196 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(dev_attr
);
197 struct powr1220_data
*data
= dev_get_drvdata(dev
);
199 return sprintf(buf
, "%d\n", data
->adc_maxes
[attr
->index
]);
202 /* Shows the label associated with the specified ADC channel */
203 static ssize_t
powr1220_show_label(struct device
*dev
,
204 struct device_attribute
*dev_attr
, char *buf
)
206 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(dev_attr
);
208 return sprintf(buf
, "%s\n", input_names
[attr
->index
]);
211 static SENSOR_DEVICE_ATTR(in0_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
213 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
215 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
217 static SENSOR_DEVICE_ATTR(in3_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
219 static SENSOR_DEVICE_ATTR(in4_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
221 static SENSOR_DEVICE_ATTR(in5_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
223 static SENSOR_DEVICE_ATTR(in6_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
225 static SENSOR_DEVICE_ATTR(in7_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
227 static SENSOR_DEVICE_ATTR(in8_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
229 static SENSOR_DEVICE_ATTR(in9_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
231 static SENSOR_DEVICE_ATTR(in10_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
233 static SENSOR_DEVICE_ATTR(in11_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
235 static SENSOR_DEVICE_ATTR(in12_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
237 static SENSOR_DEVICE_ATTR(in13_input
, S_IRUGO
, powr1220_show_voltage
, NULL
,
240 static SENSOR_DEVICE_ATTR(in0_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
242 static SENSOR_DEVICE_ATTR(in1_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
244 static SENSOR_DEVICE_ATTR(in2_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
246 static SENSOR_DEVICE_ATTR(in3_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
248 static SENSOR_DEVICE_ATTR(in4_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
250 static SENSOR_DEVICE_ATTR(in5_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
252 static SENSOR_DEVICE_ATTR(in6_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
254 static SENSOR_DEVICE_ATTR(in7_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
256 static SENSOR_DEVICE_ATTR(in8_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
258 static SENSOR_DEVICE_ATTR(in9_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
260 static SENSOR_DEVICE_ATTR(in10_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
262 static SENSOR_DEVICE_ATTR(in11_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
264 static SENSOR_DEVICE_ATTR(in12_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
266 static SENSOR_DEVICE_ATTR(in13_highest
, S_IRUGO
, powr1220_show_max
, NULL
,
269 static SENSOR_DEVICE_ATTR(in0_label
, S_IRUGO
, powr1220_show_label
, NULL
,
271 static SENSOR_DEVICE_ATTR(in1_label
, S_IRUGO
, powr1220_show_label
, NULL
,
273 static SENSOR_DEVICE_ATTR(in2_label
, S_IRUGO
, powr1220_show_label
, NULL
,
275 static SENSOR_DEVICE_ATTR(in3_label
, S_IRUGO
, powr1220_show_label
, NULL
,
277 static SENSOR_DEVICE_ATTR(in4_label
, S_IRUGO
, powr1220_show_label
, NULL
,
279 static SENSOR_DEVICE_ATTR(in5_label
, S_IRUGO
, powr1220_show_label
, NULL
,
281 static SENSOR_DEVICE_ATTR(in6_label
, S_IRUGO
, powr1220_show_label
, NULL
,
283 static SENSOR_DEVICE_ATTR(in7_label
, S_IRUGO
, powr1220_show_label
, NULL
,
285 static SENSOR_DEVICE_ATTR(in8_label
, S_IRUGO
, powr1220_show_label
, NULL
,
287 static SENSOR_DEVICE_ATTR(in9_label
, S_IRUGO
, powr1220_show_label
, NULL
,
289 static SENSOR_DEVICE_ATTR(in10_label
, S_IRUGO
, powr1220_show_label
, NULL
,
291 static SENSOR_DEVICE_ATTR(in11_label
, S_IRUGO
, powr1220_show_label
, NULL
,
293 static SENSOR_DEVICE_ATTR(in12_label
, S_IRUGO
, powr1220_show_label
, NULL
,
295 static SENSOR_DEVICE_ATTR(in13_label
, S_IRUGO
, powr1220_show_label
, NULL
,
298 static struct attribute
*powr1220_attrs
[] = {
299 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
300 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
301 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
302 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
303 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
304 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
305 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
306 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
307 &sensor_dev_attr_in8_input
.dev_attr
.attr
,
308 &sensor_dev_attr_in9_input
.dev_attr
.attr
,
309 &sensor_dev_attr_in10_input
.dev_attr
.attr
,
310 &sensor_dev_attr_in11_input
.dev_attr
.attr
,
311 &sensor_dev_attr_in12_input
.dev_attr
.attr
,
312 &sensor_dev_attr_in13_input
.dev_attr
.attr
,
314 &sensor_dev_attr_in0_highest
.dev_attr
.attr
,
315 &sensor_dev_attr_in1_highest
.dev_attr
.attr
,
316 &sensor_dev_attr_in2_highest
.dev_attr
.attr
,
317 &sensor_dev_attr_in3_highest
.dev_attr
.attr
,
318 &sensor_dev_attr_in4_highest
.dev_attr
.attr
,
319 &sensor_dev_attr_in5_highest
.dev_attr
.attr
,
320 &sensor_dev_attr_in6_highest
.dev_attr
.attr
,
321 &sensor_dev_attr_in7_highest
.dev_attr
.attr
,
322 &sensor_dev_attr_in8_highest
.dev_attr
.attr
,
323 &sensor_dev_attr_in9_highest
.dev_attr
.attr
,
324 &sensor_dev_attr_in10_highest
.dev_attr
.attr
,
325 &sensor_dev_attr_in11_highest
.dev_attr
.attr
,
326 &sensor_dev_attr_in12_highest
.dev_attr
.attr
,
327 &sensor_dev_attr_in13_highest
.dev_attr
.attr
,
329 &sensor_dev_attr_in0_label
.dev_attr
.attr
,
330 &sensor_dev_attr_in1_label
.dev_attr
.attr
,
331 &sensor_dev_attr_in2_label
.dev_attr
.attr
,
332 &sensor_dev_attr_in3_label
.dev_attr
.attr
,
333 &sensor_dev_attr_in4_label
.dev_attr
.attr
,
334 &sensor_dev_attr_in5_label
.dev_attr
.attr
,
335 &sensor_dev_attr_in6_label
.dev_attr
.attr
,
336 &sensor_dev_attr_in7_label
.dev_attr
.attr
,
337 &sensor_dev_attr_in8_label
.dev_attr
.attr
,
338 &sensor_dev_attr_in9_label
.dev_attr
.attr
,
339 &sensor_dev_attr_in10_label
.dev_attr
.attr
,
340 &sensor_dev_attr_in11_label
.dev_attr
.attr
,
341 &sensor_dev_attr_in12_label
.dev_attr
.attr
,
342 &sensor_dev_attr_in13_label
.dev_attr
.attr
,
347 ATTRIBUTE_GROUPS(powr1220
);
349 static int powr1220_probe(struct i2c_client
*client
,
350 const struct i2c_device_id
*id
)
352 struct powr1220_data
*data
;
353 struct device
*hwmon_dev
;
355 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
358 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
362 mutex_init(&data
->update_lock
);
363 data
->client
= client
;
365 hwmon_dev
= devm_hwmon_device_register_with_groups(&client
->dev
,
366 client
->name
, data
, powr1220_groups
);
368 return PTR_ERR_OR_ZERO(hwmon_dev
);
371 static const struct i2c_device_id powr1220_ids
[] = {
376 MODULE_DEVICE_TABLE(i2c
, powr1220_ids
);
378 static struct i2c_driver powr1220_driver
= {
379 .class = I2C_CLASS_HWMON
,
383 .probe
= powr1220_probe
,
384 .id_table
= powr1220_ids
,
387 module_i2c_driver(powr1220_driver
);
389 MODULE_AUTHOR("Scott Kanowitz");
390 MODULE_DESCRIPTION("POWR1220 driver");
391 MODULE_LICENSE("GPL");