xref: /openbmc/linux/drivers/hwmon/powr1220.c (revision be709d48)
1 /*
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.
5  *
6  * Copyright (c) 2014 Echo360 http://www.echo360.com
7  * Scott Kanowitz <skanowitz@echo360.com> <scott.kanowitz@gmail.com>
8  *
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.
13  *
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.
18  */
19 
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>
30 
31 #define ADC_STEP_MV			2
32 #define ADC_MAX_LOW_MEASUREMENT_MV	2000
33 
34 enum powr1220_regs {
35 	VMON_STATUS0,
36 	VMON_STATUS1,
37 	VMON_STATUS2,
38 	OUTPUT_STATUS0,
39 	OUTPUT_STATUS1,
40 	OUTPUT_STATUS2,
41 	INPUT_STATUS,
42 	ADC_VALUE_LOW,
43 	ADC_VALUE_HIGH,
44 	ADC_MUX,
45 	UES_BYTE0,
46 	UES_BYTE1,
47 	UES_BYTE2,
48 	UES_BYTE3,
49 	GP_OUTPUT1,
50 	GP_OUTPUT2,
51 	GP_OUTPUT3,
52 	INPUT_VALUE,
53 	RESET,
54 	TRIM1_TRIM,
55 	TRIM2_TRIM,
56 	TRIM3_TRIM,
57 	TRIM4_TRIM,
58 	TRIM5_TRIM,
59 	TRIM6_TRIM,
60 	TRIM7_TRIM,
61 	TRIM8_TRIM,
62 	MAX_POWR1220_REGS
63 };
64 
65 enum powr1220_adc_values {
66 	VMON1,
67 	VMON2,
68 	VMON3,
69 	VMON4,
70 	VMON5,
71 	VMON6,
72 	VMON7,
73 	VMON8,
74 	VMON9,
75 	VMON10,
76 	VMON11,
77 	VMON12,
78 	VCCA,
79 	VCCINP,
80 	MAX_POWR1220_ADC_VALUES
81 };
82 
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];
89 
90 	/* values */
91 	int adc_maxes[MAX_POWR1220_ADC_VALUES];
92 	int adc_values[MAX_POWR1220_ADC_VALUES];
93 };
94 
95 static const char * const input_names[] = {
96 	[VMON1]    = "vmon1",
97 	[VMON2]    = "vmon2",
98 	[VMON3]    = "vmon3",
99 	[VMON4]    = "vmon4",
100 	[VMON5]    = "vmon5",
101 	[VMON6]    = "vmon6",
102 	[VMON7]    = "vmon7",
103 	[VMON8]    = "vmon8",
104 	[VMON9]    = "vmon9",
105 	[VMON10]   = "vmon10",
106 	[VMON11]   = "vmon11",
107 	[VMON12]   = "vmon12",
108 	[VCCA]     = "vcca",
109 	[VCCINP]   = "vccinp",
110 };
111 
112 /* Reads the specified ADC channel */
113 static int powr1220_read_adc(struct device *dev, int ch_num)
114 {
115 	struct powr1220_data *data = dev_get_drvdata(dev);
116 	int reading;
117 	int result;
118 	int adc_range = 0;
119 
120 	mutex_lock(&data->update_lock);
121 
122 	if (time_after(jiffies, data->adc_last_updated[ch_num] + HZ) ||
123 			!data->adc_valid[ch_num]) {
124 		/*
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
128 		 * max reading.
129 		 */
130 		if (data->adc_maxes[ch_num] > ADC_MAX_LOW_MEASUREMENT_MV ||
131 				data->adc_maxes[ch_num] == 0)
132 			adc_range = 1 << 4;
133 
134 		/* set the attenuator and mux */
135 		result = i2c_smbus_write_byte_data(data->client, ADC_MUX,
136 				adc_range | ch_num);
137 		if (result)
138 			goto exit;
139 
140 		/*
141 		 * wait at least Tconvert time (200 us) for the
142 		 * conversion to complete
143 		 */
144 		udelay(200);
145 
146 		/* get the ADC reading */
147 		result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_LOW);
148 		if (result < 0)
149 			goto exit;
150 
151 		reading = result >> 4;
152 
153 		/* get the upper half of the reading */
154 		result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_HIGH);
155 		if (result < 0)
156 			goto exit;
157 
158 		reading |= result << 4;
159 
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;
165 		result = reading;
166 
167 		if (reading > data->adc_maxes[ch_num])
168 			data->adc_maxes[ch_num] = reading;
169 	} else {
170 		result = data->adc_values[ch_num];
171 	}
172 
173 exit:
174 	mutex_unlock(&data->update_lock);
175 
176 	return result;
177 }
178 
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,
182 				     char *buf)
183 {
184 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
185 	int adc_val = powr1220_read_adc(dev, attr->index);
186 
187 	if (adc_val < 0)
188 		return adc_val;
189 
190 	return sprintf(buf, "%d\n", adc_val);
191 }
192 
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)
196 {
197 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
198 	struct powr1220_data *data = dev_get_drvdata(dev);
199 
200 	return sprintf(buf, "%d\n", data->adc_maxes[attr->index]);
201 }
202 
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,
206 				   char *buf)
207 {
208 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
209 
210 	return sprintf(buf, "%s\n", input_names[attr->index]);
211 }
212 
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);
227 
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);
242 
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);
257 
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,
273 
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,
288 
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,
303 
304 	NULL
305 };
306 
307 ATTRIBUTE_GROUPS(powr1220);
308 
309 static int powr1220_probe(struct i2c_client *client,
310 		const struct i2c_device_id *id)
311 {
312 	struct powr1220_data *data;
313 	struct device *hwmon_dev;
314 
315 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
316 		return -ENODEV;
317 
318 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
319 	if (!data)
320 		return -ENOMEM;
321 
322 	mutex_init(&data->update_lock);
323 	data->client = client;
324 
325 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
326 			client->name, data, powr1220_groups);
327 
328 	return PTR_ERR_OR_ZERO(hwmon_dev);
329 }
330 
331 static const struct i2c_device_id powr1220_ids[] = {
332 	{ "powr1220", 0, },
333 	{ }
334 };
335 
336 MODULE_DEVICE_TABLE(i2c, powr1220_ids);
337 
338 static struct i2c_driver powr1220_driver = {
339 	.class		= I2C_CLASS_HWMON,
340 	.driver = {
341 		.name	= "powr1220",
342 	},
343 	.probe		= powr1220_probe,
344 	.id_table	= powr1220_ids,
345 };
346 
347 module_i2c_driver(powr1220_driver);
348 
349 MODULE_AUTHOR("Scott Kanowitz");
350 MODULE_DESCRIPTION("POWR1220 driver");
351 MODULE_LICENSE("GPL");
352