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_voltage_show(struct device *dev,
181 struct device_attribute *dev_attr,
184 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
185 int adc_val = powr1220_read_adc(dev, attr->index);
190 return sprintf(buf, "%d\n", adc_val);
193 /* Shows the maximum setting associated with the specified ADC channel */
194 static ssize_t powr1220_max_show(struct device *dev,
195 struct device_attribute *dev_attr, char *buf)
197 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
198 struct powr1220_data *data = dev_get_drvdata(dev);
200 return sprintf(buf, "%d\n", data->adc_maxes[attr->index]);
203 /* Shows the label associated with the specified ADC channel */
204 static ssize_t powr1220_label_show(struct device *dev,
205 struct device_attribute *dev_attr,
208 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
210 return sprintf(buf, "%s\n", input_names[attr->index]);
213 static SENSOR_DEVICE_ATTR_RO(in0_input, powr1220_voltage, VMON1);
214 static SENSOR_DEVICE_ATTR_RO(in1_input, powr1220_voltage, VMON2);
215 static SENSOR_DEVICE_ATTR_RO(in2_input, powr1220_voltage, VMON3);
216 static SENSOR_DEVICE_ATTR_RO(in3_input, powr1220_voltage, VMON4);
217 static SENSOR_DEVICE_ATTR_RO(in4_input, powr1220_voltage, VMON5);
218 static SENSOR_DEVICE_ATTR_RO(in5_input, powr1220_voltage, VMON6);
219 static SENSOR_DEVICE_ATTR_RO(in6_input, powr1220_voltage, VMON7);
220 static SENSOR_DEVICE_ATTR_RO(in7_input, powr1220_voltage, VMON8);
221 static SENSOR_DEVICE_ATTR_RO(in8_input, powr1220_voltage, VMON9);
222 static SENSOR_DEVICE_ATTR_RO(in9_input, powr1220_voltage, VMON10);
223 static SENSOR_DEVICE_ATTR_RO(in10_input, powr1220_voltage, VMON11);
224 static SENSOR_DEVICE_ATTR_RO(in11_input, powr1220_voltage, VMON12);
225 static SENSOR_DEVICE_ATTR_RO(in12_input, powr1220_voltage, VCCA);
226 static SENSOR_DEVICE_ATTR_RO(in13_input, powr1220_voltage, VCCINP);
228 static SENSOR_DEVICE_ATTR_RO(in0_highest, powr1220_max, VMON1);
229 static SENSOR_DEVICE_ATTR_RO(in1_highest, powr1220_max, VMON2);
230 static SENSOR_DEVICE_ATTR_RO(in2_highest, powr1220_max, VMON3);
231 static SENSOR_DEVICE_ATTR_RO(in3_highest, powr1220_max, VMON4);
232 static SENSOR_DEVICE_ATTR_RO(in4_highest, powr1220_max, VMON5);
233 static SENSOR_DEVICE_ATTR_RO(in5_highest, powr1220_max, VMON6);
234 static SENSOR_DEVICE_ATTR_RO(in6_highest, powr1220_max, VMON7);
235 static SENSOR_DEVICE_ATTR_RO(in7_highest, powr1220_max, VMON8);
236 static SENSOR_DEVICE_ATTR_RO(in8_highest, powr1220_max, VMON9);
237 static SENSOR_DEVICE_ATTR_RO(in9_highest, powr1220_max, VMON10);
238 static SENSOR_DEVICE_ATTR_RO(in10_highest, powr1220_max, VMON11);
239 static SENSOR_DEVICE_ATTR_RO(in11_highest, powr1220_max, VMON12);
240 static SENSOR_DEVICE_ATTR_RO(in12_highest, powr1220_max, VCCA);
241 static SENSOR_DEVICE_ATTR_RO(in13_highest, powr1220_max, VCCINP);
243 static SENSOR_DEVICE_ATTR_RO(in0_label, powr1220_label, VMON1);
244 static SENSOR_DEVICE_ATTR_RO(in1_label, powr1220_label, VMON2);
245 static SENSOR_DEVICE_ATTR_RO(in2_label, powr1220_label, VMON3);
246 static SENSOR_DEVICE_ATTR_RO(in3_label, powr1220_label, VMON4);
247 static SENSOR_DEVICE_ATTR_RO(in4_label, powr1220_label, VMON5);
248 static SENSOR_DEVICE_ATTR_RO(in5_label, powr1220_label, VMON6);
249 static SENSOR_DEVICE_ATTR_RO(in6_label, powr1220_label, VMON7);
250 static SENSOR_DEVICE_ATTR_RO(in7_label, powr1220_label, VMON8);
251 static SENSOR_DEVICE_ATTR_RO(in8_label, powr1220_label, VMON9);
252 static SENSOR_DEVICE_ATTR_RO(in9_label, powr1220_label, VMON10);
253 static SENSOR_DEVICE_ATTR_RO(in10_label, powr1220_label, VMON11);
254 static SENSOR_DEVICE_ATTR_RO(in11_label, powr1220_label, VMON12);
255 static SENSOR_DEVICE_ATTR_RO(in12_label, powr1220_label, VCCA);
256 static SENSOR_DEVICE_ATTR_RO(in13_label, powr1220_label, VCCINP);
258 static struct attribute *powr1220_attrs[] = {
259 &sensor_dev_attr_in0_input.dev_attr.attr,
260 &sensor_dev_attr_in1_input.dev_attr.attr,
261 &sensor_dev_attr_in2_input.dev_attr.attr,
262 &sensor_dev_attr_in3_input.dev_attr.attr,
263 &sensor_dev_attr_in4_input.dev_attr.attr,
264 &sensor_dev_attr_in5_input.dev_attr.attr,
265 &sensor_dev_attr_in6_input.dev_attr.attr,
266 &sensor_dev_attr_in7_input.dev_attr.attr,
267 &sensor_dev_attr_in8_input.dev_attr.attr,
268 &sensor_dev_attr_in9_input.dev_attr.attr,
269 &sensor_dev_attr_in10_input.dev_attr.attr,
270 &sensor_dev_attr_in11_input.dev_attr.attr,
271 &sensor_dev_attr_in12_input.dev_attr.attr,
272 &sensor_dev_attr_in13_input.dev_attr.attr,
274 &sensor_dev_attr_in0_highest.dev_attr.attr,
275 &sensor_dev_attr_in1_highest.dev_attr.attr,
276 &sensor_dev_attr_in2_highest.dev_attr.attr,
277 &sensor_dev_attr_in3_highest.dev_attr.attr,
278 &sensor_dev_attr_in4_highest.dev_attr.attr,
279 &sensor_dev_attr_in5_highest.dev_attr.attr,
280 &sensor_dev_attr_in6_highest.dev_attr.attr,
281 &sensor_dev_attr_in7_highest.dev_attr.attr,
282 &sensor_dev_attr_in8_highest.dev_attr.attr,
283 &sensor_dev_attr_in9_highest.dev_attr.attr,
284 &sensor_dev_attr_in10_highest.dev_attr.attr,
285 &sensor_dev_attr_in11_highest.dev_attr.attr,
286 &sensor_dev_attr_in12_highest.dev_attr.attr,
287 &sensor_dev_attr_in13_highest.dev_attr.attr,
289 &sensor_dev_attr_in0_label.dev_attr.attr,
290 &sensor_dev_attr_in1_label.dev_attr.attr,
291 &sensor_dev_attr_in2_label.dev_attr.attr,
292 &sensor_dev_attr_in3_label.dev_attr.attr,
293 &sensor_dev_attr_in4_label.dev_attr.attr,
294 &sensor_dev_attr_in5_label.dev_attr.attr,
295 &sensor_dev_attr_in6_label.dev_attr.attr,
296 &sensor_dev_attr_in7_label.dev_attr.attr,
297 &sensor_dev_attr_in8_label.dev_attr.attr,
298 &sensor_dev_attr_in9_label.dev_attr.attr,
299 &sensor_dev_attr_in10_label.dev_attr.attr,
300 &sensor_dev_attr_in11_label.dev_attr.attr,
301 &sensor_dev_attr_in12_label.dev_attr.attr,
302 &sensor_dev_attr_in13_label.dev_attr.attr,
307 ATTRIBUTE_GROUPS(powr1220);
309 static int powr1220_probe(struct i2c_client *client,
310 const struct i2c_device_id *id)
312 struct powr1220_data *data;
313 struct device *hwmon_dev;
315 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
318 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
322 mutex_init(&data->update_lock);
323 data->client = client;
325 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
326 client->name, data, powr1220_groups);
328 return PTR_ERR_OR_ZERO(hwmon_dev);
331 static const struct i2c_device_id powr1220_ids[] = {
336 MODULE_DEVICE_TABLE(i2c, powr1220_ids);
338 static struct i2c_driver powr1220_driver = {
339 .class = I2C_CLASS_HWMON,
343 .probe = powr1220_probe,
344 .id_table = powr1220_ids,
347 module_i2c_driver(powr1220_driver);
349 MODULE_AUTHOR("Scott Kanowitz");
350 MODULE_DESCRIPTION("POWR1220 driver");
351 MODULE_LICENSE("GPL");