xref: /openbmc/linux/drivers/hwmon/powr1220.c (revision 915d4664)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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.
6  *
7  * Copyright (c) 2014 Echo360 https://www.echo360.com
8  * Scott Kanowitz <skanowitz@echo360.com> <scott.kanowitz@gmail.com>
9  */
10 
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>
21 
22 #define ADC_STEP_MV			2
23 #define ADC_MAX_LOW_MEASUREMENT_MV	2000
24 
25 enum powr1220_regs {
26 	VMON_STATUS0,
27 	VMON_STATUS1,
28 	VMON_STATUS2,
29 	OUTPUT_STATUS0,
30 	OUTPUT_STATUS1,
31 	OUTPUT_STATUS2,
32 	INPUT_STATUS,
33 	ADC_VALUE_LOW,
34 	ADC_VALUE_HIGH,
35 	ADC_MUX,
36 	UES_BYTE0,
37 	UES_BYTE1,
38 	UES_BYTE2,
39 	UES_BYTE3,
40 	GP_OUTPUT1,
41 	GP_OUTPUT2,
42 	GP_OUTPUT3,
43 	INPUT_VALUE,
44 	RESET,
45 	TRIM1_TRIM,
46 	TRIM2_TRIM,
47 	TRIM3_TRIM,
48 	TRIM4_TRIM,
49 	TRIM5_TRIM,
50 	TRIM6_TRIM,
51 	TRIM7_TRIM,
52 	TRIM8_TRIM,
53 	MAX_POWR1220_REGS
54 };
55 
56 enum powr1220_adc_values {
57 	VMON1,
58 	VMON2,
59 	VMON3,
60 	VMON4,
61 	VMON5,
62 	VMON6,
63 	VMON7,
64 	VMON8,
65 	VMON9,
66 	VMON10,
67 	VMON11,
68 	VMON12,
69 	VCCA,
70 	VCCINP,
71 	MAX_POWR1220_ADC_VALUES
72 };
73 
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];
80 
81 	/* values */
82 	int adc_maxes[MAX_POWR1220_ADC_VALUES];
83 	int adc_values[MAX_POWR1220_ADC_VALUES];
84 };
85 
86 static const char * const input_names[] = {
87 	[VMON1]    = "vmon1",
88 	[VMON2]    = "vmon2",
89 	[VMON3]    = "vmon3",
90 	[VMON4]    = "vmon4",
91 	[VMON5]    = "vmon5",
92 	[VMON6]    = "vmon6",
93 	[VMON7]    = "vmon7",
94 	[VMON8]    = "vmon8",
95 	[VMON9]    = "vmon9",
96 	[VMON10]   = "vmon10",
97 	[VMON11]   = "vmon11",
98 	[VMON12]   = "vmon12",
99 	[VCCA]     = "vcca",
100 	[VCCINP]   = "vccinp",
101 };
102 
103 /* Reads the specified ADC channel */
104 static int powr1220_read_adc(struct device *dev, int ch_num)
105 {
106 	struct powr1220_data *data = dev_get_drvdata(dev);
107 	int reading;
108 	int result;
109 	int adc_range = 0;
110 
111 	mutex_lock(&data->update_lock);
112 
113 	if (time_after(jiffies, data->adc_last_updated[ch_num] + HZ) ||
114 	    !data->adc_valid[ch_num]) {
115 		/*
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
119 		 * max reading.
120 		 */
121 		if (data->adc_maxes[ch_num] > ADC_MAX_LOW_MEASUREMENT_MV ||
122 		    data->adc_maxes[ch_num] == 0)
123 			adc_range = 1 << 4;
124 
125 		/* set the attenuator and mux */
126 		result = i2c_smbus_write_byte_data(data->client, ADC_MUX,
127 						   adc_range | ch_num);
128 		if (result)
129 			goto exit;
130 
131 		/*
132 		 * wait at least Tconvert time (200 us) for the
133 		 * conversion to complete
134 		 */
135 		udelay(200);
136 
137 		/* get the ADC reading */
138 		result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_LOW);
139 		if (result < 0)
140 			goto exit;
141 
142 		reading = result >> 4;
143 
144 		/* get the upper half of the reading */
145 		result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_HIGH);
146 		if (result < 0)
147 			goto exit;
148 
149 		reading |= result << 4;
150 
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;
156 		result = reading;
157 
158 		if (reading > data->adc_maxes[ch_num])
159 			data->adc_maxes[ch_num] = reading;
160 	} else {
161 		result = data->adc_values[ch_num];
162 	}
163 
164 exit:
165 	mutex_unlock(&data->update_lock);
166 
167 	return result;
168 }
169 
170 static umode_t
171 powr1220_is_visible(const void *data, enum hwmon_sensor_types type, u32
172 		    attr, int channel)
173 {
174 	switch (type) {
175 	case hwmon_in:
176 		switch (attr) {
177 		case hwmon_in_input:
178 		case hwmon_in_highest:
179 		case hwmon_in_label:
180 			return 0444;
181 		default:
182 			break;
183 		}
184 		break;
185 	default:
186 		break;
187 	}
188 
189 	return 0;
190 }
191 
192 static int
193 powr1220_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
194 		     int channel, const char **str)
195 {
196 	switch (type) {
197 	case hwmon_in:
198 		switch (attr) {
199 		case hwmon_in_label:
200 			*str = input_names[channel];
201 			return 0;
202 		default:
203 			return -EOPNOTSUPP;
204 		}
205 		break;
206 	default:
207 		return -EOPNOTSUPP;
208 	}
209 
210 	return -EOPNOTSUPP;
211 }
212 
213 static int
214 powr1220_read(struct device *dev, enum hwmon_sensor_types type, u32
215 	      attr, int channel, long *val)
216 {
217 	struct powr1220_data *data = dev_get_drvdata(dev);
218 	int ret;
219 
220 	switch (type) {
221 	case hwmon_in:
222 		switch (attr) {
223 		case hwmon_in_input:
224 			ret = powr1220_read_adc(dev, channel);
225 			if (ret < 0)
226 				return ret;
227 			*val = ret;
228 			break;
229 		case hwmon_in_highest:
230 			*val = data->adc_maxes[channel];
231 			break;
232 		default:
233 			return -EOPNOTSUPP;
234 		}
235 		break;
236 	default:
237 		return -EOPNOTSUPP;
238 }
239 
240 	return 0;
241 }
242 
243 static const struct hwmon_channel_info *powr1220_info[] = {
244 	HWMON_CHANNEL_INFO(in,
245 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
246 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
247 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
248 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
249 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
250 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
251 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
252 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
253 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
254 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
255 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
256 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
257 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
258 			   HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL),
259 
260 	NULL
261 };
262 
263 static const struct hwmon_ops powr1220_hwmon_ops = {
264 	.read = powr1220_read,
265 	.read_string = powr1220_read_string,
266 	.is_visible = powr1220_is_visible,
267 };
268 
269 static const struct hwmon_chip_info powr1220_chip_info = {
270 	.ops = &powr1220_hwmon_ops,
271 	.info = powr1220_info,
272 };
273 
274 static int powr1220_probe(struct i2c_client *client)
275 {
276 	struct powr1220_data *data;
277 	struct device *hwmon_dev;
278 
279 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
280 		return -ENODEV;
281 
282 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
283 	if (!data)
284 		return -ENOMEM;
285 
286 	mutex_init(&data->update_lock);
287 	data->client = client;
288 
289 	hwmon_dev = devm_hwmon_device_register_with_info(&client->dev,
290 							 client->name,
291 							 data,
292 							 &powr1220_chip_info,
293 							 NULL);
294 
295 	return PTR_ERR_OR_ZERO(hwmon_dev);
296 }
297 
298 static const struct i2c_device_id powr1220_ids[] = {
299 	{ "powr1220", 0, },
300 	{ }
301 };
302 
303 MODULE_DEVICE_TABLE(i2c, powr1220_ids);
304 
305 static struct i2c_driver powr1220_driver = {
306 	.class		= I2C_CLASS_HWMON,
307 	.driver = {
308 		.name	= "powr1220",
309 	},
310 	.probe_new	= powr1220_probe,
311 	.id_table	= powr1220_ids,
312 };
313 
314 module_i2c_driver(powr1220_driver);
315 
316 MODULE_AUTHOR("Scott Kanowitz");
317 MODULE_DESCRIPTION("POWR1220 driver");
318 MODULE_LICENSE("GPL");
319