1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * powr1220.c - Driver for the Lattice POWR1220 programmable power supply
4 * and monitor. Users can read all ADC inputs along with their labels
5 * using the sysfs nodes.
7 * Copyright (c) 2014 Echo360 https://www.echo360.com
8 * Scott Kanowitz <skanowitz@echo360.com> <scott.kanowitz@gmail.com>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/i2c.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/delay.h>
23 #define ADC_MAX_LOW_MEASUREMENT_MV 2000
56 enum powr1220_adc_values
{
71 MAX_POWR1220_ADC_VALUES
74 struct powr1220_data
{
75 struct i2c_client
*client
;
76 struct mutex update_lock
;
77 bool adc_valid
[MAX_POWR1220_ADC_VALUES
];
78 /* the next value is in jiffies */
79 unsigned long adc_last_updated
[MAX_POWR1220_ADC_VALUES
];
82 int adc_maxes
[MAX_POWR1220_ADC_VALUES
];
83 int adc_values
[MAX_POWR1220_ADC_VALUES
];
86 static const char * const input_names
[] = {
103 /* Reads the specified ADC channel */
104 static int powr1220_read_adc(struct device
*dev
, int ch_num
)
106 struct powr1220_data
*data
= dev_get_drvdata(dev
);
111 mutex_lock(&data
->update_lock
);
113 if (time_after(jiffies
, data
->adc_last_updated
[ch_num
] + HZ
) ||
114 !data
->adc_valid
[ch_num
]) {
116 * figure out if we need to use the attenuator for
117 * high inputs or inputs that we don't yet have a measurement
118 * for. We dynamically set the attenuator depending on the
121 if (data
->adc_maxes
[ch_num
] > ADC_MAX_LOW_MEASUREMENT_MV
||
122 data
->adc_maxes
[ch_num
] == 0)
125 /* set the attenuator and mux */
126 result
= i2c_smbus_write_byte_data(data
->client
, ADC_MUX
,
132 * wait at least Tconvert time (200 us) for the
133 * conversion to complete
137 /* get the ADC reading */
138 result
= i2c_smbus_read_byte_data(data
->client
, ADC_VALUE_LOW
);
142 reading
= result
>> 4;
144 /* get the upper half of the reading */
145 result
= i2c_smbus_read_byte_data(data
->client
, ADC_VALUE_HIGH
);
149 reading
|= result
<< 4;
151 /* now convert the reading to a voltage */
152 reading
*= ADC_STEP_MV
;
153 data
->adc_values
[ch_num
] = reading
;
154 data
->adc_valid
[ch_num
] = true;
155 data
->adc_last_updated
[ch_num
] = jiffies
;
158 if (reading
> data
->adc_maxes
[ch_num
])
159 data
->adc_maxes
[ch_num
] = reading
;
161 result
= data
->adc_values
[ch_num
];
165 mutex_unlock(&data
->update_lock
);
170 /* Shows the voltage associated with the specified ADC channel */
171 static ssize_t
powr1220_voltage_show(struct device
*dev
,
172 struct device_attribute
*dev_attr
,
175 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(dev_attr
);
176 int adc_val
= powr1220_read_adc(dev
, attr
->index
);
181 return sprintf(buf
, "%d\n", adc_val
);
184 /* Shows the maximum setting associated with the specified ADC channel */
185 static ssize_t
powr1220_max_show(struct device
*dev
,
186 struct device_attribute
*dev_attr
, char *buf
)
188 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(dev_attr
);
189 struct powr1220_data
*data
= dev_get_drvdata(dev
);
191 return sprintf(buf
, "%d\n", data
->adc_maxes
[attr
->index
]);
194 /* Shows the label associated with the specified ADC channel */
195 static ssize_t
powr1220_label_show(struct device
*dev
,
196 struct device_attribute
*dev_attr
,
199 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(dev_attr
);
201 return sprintf(buf
, "%s\n", input_names
[attr
->index
]);
204 static SENSOR_DEVICE_ATTR_RO(in0_input
, powr1220_voltage
, VMON1
);
205 static SENSOR_DEVICE_ATTR_RO(in1_input
, powr1220_voltage
, VMON2
);
206 static SENSOR_DEVICE_ATTR_RO(in2_input
, powr1220_voltage
, VMON3
);
207 static SENSOR_DEVICE_ATTR_RO(in3_input
, powr1220_voltage
, VMON4
);
208 static SENSOR_DEVICE_ATTR_RO(in4_input
, powr1220_voltage
, VMON5
);
209 static SENSOR_DEVICE_ATTR_RO(in5_input
, powr1220_voltage
, VMON6
);
210 static SENSOR_DEVICE_ATTR_RO(in6_input
, powr1220_voltage
, VMON7
);
211 static SENSOR_DEVICE_ATTR_RO(in7_input
, powr1220_voltage
, VMON8
);
212 static SENSOR_DEVICE_ATTR_RO(in8_input
, powr1220_voltage
, VMON9
);
213 static SENSOR_DEVICE_ATTR_RO(in9_input
, powr1220_voltage
, VMON10
);
214 static SENSOR_DEVICE_ATTR_RO(in10_input
, powr1220_voltage
, VMON11
);
215 static SENSOR_DEVICE_ATTR_RO(in11_input
, powr1220_voltage
, VMON12
);
216 static SENSOR_DEVICE_ATTR_RO(in12_input
, powr1220_voltage
, VCCA
);
217 static SENSOR_DEVICE_ATTR_RO(in13_input
, powr1220_voltage
, VCCINP
);
219 static SENSOR_DEVICE_ATTR_RO(in0_highest
, powr1220_max
, VMON1
);
220 static SENSOR_DEVICE_ATTR_RO(in1_highest
, powr1220_max
, VMON2
);
221 static SENSOR_DEVICE_ATTR_RO(in2_highest
, powr1220_max
, VMON3
);
222 static SENSOR_DEVICE_ATTR_RO(in3_highest
, powr1220_max
, VMON4
);
223 static SENSOR_DEVICE_ATTR_RO(in4_highest
, powr1220_max
, VMON5
);
224 static SENSOR_DEVICE_ATTR_RO(in5_highest
, powr1220_max
, VMON6
);
225 static SENSOR_DEVICE_ATTR_RO(in6_highest
, powr1220_max
, VMON7
);
226 static SENSOR_DEVICE_ATTR_RO(in7_highest
, powr1220_max
, VMON8
);
227 static SENSOR_DEVICE_ATTR_RO(in8_highest
, powr1220_max
, VMON9
);
228 static SENSOR_DEVICE_ATTR_RO(in9_highest
, powr1220_max
, VMON10
);
229 static SENSOR_DEVICE_ATTR_RO(in10_highest
, powr1220_max
, VMON11
);
230 static SENSOR_DEVICE_ATTR_RO(in11_highest
, powr1220_max
, VMON12
);
231 static SENSOR_DEVICE_ATTR_RO(in12_highest
, powr1220_max
, VCCA
);
232 static SENSOR_DEVICE_ATTR_RO(in13_highest
, powr1220_max
, VCCINP
);
234 static SENSOR_DEVICE_ATTR_RO(in0_label
, powr1220_label
, VMON1
);
235 static SENSOR_DEVICE_ATTR_RO(in1_label
, powr1220_label
, VMON2
);
236 static SENSOR_DEVICE_ATTR_RO(in2_label
, powr1220_label
, VMON3
);
237 static SENSOR_DEVICE_ATTR_RO(in3_label
, powr1220_label
, VMON4
);
238 static SENSOR_DEVICE_ATTR_RO(in4_label
, powr1220_label
, VMON5
);
239 static SENSOR_DEVICE_ATTR_RO(in5_label
, powr1220_label
, VMON6
);
240 static SENSOR_DEVICE_ATTR_RO(in6_label
, powr1220_label
, VMON7
);
241 static SENSOR_DEVICE_ATTR_RO(in7_label
, powr1220_label
, VMON8
);
242 static SENSOR_DEVICE_ATTR_RO(in8_label
, powr1220_label
, VMON9
);
243 static SENSOR_DEVICE_ATTR_RO(in9_label
, powr1220_label
, VMON10
);
244 static SENSOR_DEVICE_ATTR_RO(in10_label
, powr1220_label
, VMON11
);
245 static SENSOR_DEVICE_ATTR_RO(in11_label
, powr1220_label
, VMON12
);
246 static SENSOR_DEVICE_ATTR_RO(in12_label
, powr1220_label
, VCCA
);
247 static SENSOR_DEVICE_ATTR_RO(in13_label
, powr1220_label
, VCCINP
);
249 static struct attribute
*powr1220_attrs
[] = {
250 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
251 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
252 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
253 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
254 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
255 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
256 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
257 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
258 &sensor_dev_attr_in8_input
.dev_attr
.attr
,
259 &sensor_dev_attr_in9_input
.dev_attr
.attr
,
260 &sensor_dev_attr_in10_input
.dev_attr
.attr
,
261 &sensor_dev_attr_in11_input
.dev_attr
.attr
,
262 &sensor_dev_attr_in12_input
.dev_attr
.attr
,
263 &sensor_dev_attr_in13_input
.dev_attr
.attr
,
265 &sensor_dev_attr_in0_highest
.dev_attr
.attr
,
266 &sensor_dev_attr_in1_highest
.dev_attr
.attr
,
267 &sensor_dev_attr_in2_highest
.dev_attr
.attr
,
268 &sensor_dev_attr_in3_highest
.dev_attr
.attr
,
269 &sensor_dev_attr_in4_highest
.dev_attr
.attr
,
270 &sensor_dev_attr_in5_highest
.dev_attr
.attr
,
271 &sensor_dev_attr_in6_highest
.dev_attr
.attr
,
272 &sensor_dev_attr_in7_highest
.dev_attr
.attr
,
273 &sensor_dev_attr_in8_highest
.dev_attr
.attr
,
274 &sensor_dev_attr_in9_highest
.dev_attr
.attr
,
275 &sensor_dev_attr_in10_highest
.dev_attr
.attr
,
276 &sensor_dev_attr_in11_highest
.dev_attr
.attr
,
277 &sensor_dev_attr_in12_highest
.dev_attr
.attr
,
278 &sensor_dev_attr_in13_highest
.dev_attr
.attr
,
280 &sensor_dev_attr_in0_label
.dev_attr
.attr
,
281 &sensor_dev_attr_in1_label
.dev_attr
.attr
,
282 &sensor_dev_attr_in2_label
.dev_attr
.attr
,
283 &sensor_dev_attr_in3_label
.dev_attr
.attr
,
284 &sensor_dev_attr_in4_label
.dev_attr
.attr
,
285 &sensor_dev_attr_in5_label
.dev_attr
.attr
,
286 &sensor_dev_attr_in6_label
.dev_attr
.attr
,
287 &sensor_dev_attr_in7_label
.dev_attr
.attr
,
288 &sensor_dev_attr_in8_label
.dev_attr
.attr
,
289 &sensor_dev_attr_in9_label
.dev_attr
.attr
,
290 &sensor_dev_attr_in10_label
.dev_attr
.attr
,
291 &sensor_dev_attr_in11_label
.dev_attr
.attr
,
292 &sensor_dev_attr_in12_label
.dev_attr
.attr
,
293 &sensor_dev_attr_in13_label
.dev_attr
.attr
,
298 ATTRIBUTE_GROUPS(powr1220
);
300 static int powr1220_probe(struct i2c_client
*client
)
302 struct powr1220_data
*data
;
303 struct device
*hwmon_dev
;
305 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
308 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
312 mutex_init(&data
->update_lock
);
313 data
->client
= client
;
315 hwmon_dev
= devm_hwmon_device_register_with_groups(&client
->dev
,
316 client
->name
, data
, powr1220_groups
);
318 return PTR_ERR_OR_ZERO(hwmon_dev
);
321 static const struct i2c_device_id powr1220_ids
[] = {
326 MODULE_DEVICE_TABLE(i2c
, powr1220_ids
);
328 static struct i2c_driver powr1220_driver
= {
329 .class = I2C_CLASS_HWMON
,
333 .probe_new
= powr1220_probe
,
334 .id_table
= powr1220_ids
,
337 module_i2c_driver(powr1220_driver
);
339 MODULE_AUTHOR("Scott Kanowitz");
340 MODULE_DESCRIPTION("POWR1220 driver");
341 MODULE_LICENSE("GPL");