xref: /openbmc/linux/drivers/iio/adc/stmpe-adc.c (revision 62e59c4e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  STMicroelectronics STMPE811 IIO ADC Driver
4  *
5  *  4 channel, 10/12-bit ADC
6  *
7  *  Copyright (C) 2013-2018 Toradex AG <stefan.agner@toradex.com>
8  */
9 
10 #include <linux/completion.h>
11 #include <linux/err.h>
12 #include <linux/iio/iio.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/stmpe.h>
16 #include <linux/module.h>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
19 #include <linux/device.h>
20 
21 #define STMPE_REG_INT_STA		0x0B
22 #define STMPE_REG_ADC_INT_EN		0x0E
23 #define STMPE_REG_ADC_INT_STA		0x0F
24 
25 #define STMPE_REG_ADC_CTRL1		0x20
26 #define STMPE_REG_ADC_CTRL2		0x21
27 #define STMPE_REG_ADC_CAPT		0x22
28 #define STMPE_REG_ADC_DATA_CH(channel)	(0x30 + 2 * (channel))
29 
30 #define STMPE_REG_TEMP_CTRL		0x60
31 #define STMPE_TEMP_CTRL_ENABLE		BIT(0)
32 #define STMPE_TEMP_CTRL_ACQ		BIT(1)
33 #define STMPE_TEMP_CTRL_THRES_EN	BIT(3)
34 #define STMPE_START_ONE_TEMP_CONV	(STMPE_TEMP_CTRL_ENABLE | \
35 					STMPE_TEMP_CTRL_ACQ | \
36 					STMPE_TEMP_CTRL_THRES_EN)
37 #define STMPE_REG_TEMP_DATA		0x61
38 #define STMPE_REG_TEMP_TH		0x63
39 #define STMPE_ADC_LAST_NR		7
40 #define STMPE_TEMP_CHANNEL		(STMPE_ADC_LAST_NR + 1)
41 
42 #define STMPE_ADC_CH(channel)		((1 << (channel)) & 0xff)
43 
44 #define STMPE_ADC_TIMEOUT		msecs_to_jiffies(1000)
45 
46 struct stmpe_adc {
47 	struct stmpe *stmpe;
48 	struct clk *clk;
49 	struct device *dev;
50 	struct mutex lock;
51 
52 	/* We are allocating plus one for the temperature channel */
53 	struct iio_chan_spec stmpe_adc_iio_channels[STMPE_ADC_LAST_NR + 2];
54 
55 	struct completion completion;
56 
57 	u8 channel;
58 	u32 value;
59 };
60 
61 static int stmpe_read_voltage(struct stmpe_adc *info,
62 		struct iio_chan_spec const *chan, int *val)
63 {
64 	long ret;
65 
66 	mutex_lock(&info->lock);
67 
68 	info->channel = (u8)chan->channel;
69 
70 	if (info->channel > STMPE_ADC_LAST_NR) {
71 		mutex_unlock(&info->lock);
72 		return -EINVAL;
73 	}
74 
75 	stmpe_reg_write(info->stmpe, STMPE_REG_ADC_INT_EN,
76 			STMPE_ADC_CH(info->channel));
77 
78 	stmpe_reg_write(info->stmpe, STMPE_REG_ADC_CAPT,
79 			STMPE_ADC_CH(info->channel));
80 
81 	*val = info->value;
82 
83 	ret = wait_for_completion_interruptible_timeout
84 		(&info->completion, STMPE_ADC_TIMEOUT);
85 
86 	if (ret <= 0) {
87 		mutex_unlock(&info->lock);
88 		if (ret == 0)
89 			return -ETIMEDOUT;
90 		else
91 			return ret;
92 	}
93 
94 	*val = info->value;
95 
96 	mutex_unlock(&info->lock);
97 
98 	return 0;
99 }
100 
101 static int stmpe_read_temp(struct stmpe_adc *info,
102 		struct iio_chan_spec const *chan, int *val)
103 {
104 	long ret;
105 
106 	mutex_lock(&info->lock);
107 
108 	info->channel = (u8)chan->channel;
109 
110 	if (info->channel != STMPE_TEMP_CHANNEL) {
111 		mutex_unlock(&info->lock);
112 		return -EINVAL;
113 	}
114 
115 	stmpe_reg_write(info->stmpe, STMPE_REG_TEMP_CTRL,
116 			STMPE_START_ONE_TEMP_CONV);
117 
118 	ret = wait_for_completion_interruptible_timeout
119 		(&info->completion, STMPE_ADC_TIMEOUT);
120 
121 	if (ret <= 0) {
122 		mutex_unlock(&info->lock);
123 		if (ret == 0)
124 			return -ETIMEDOUT;
125 		else
126 			return ret;
127 	}
128 
129 	/*
130 	 * absolute temp = +V3.3 * value /7.51 [K]
131 	 * scale to [milli °C]
132 	 */
133 	*val = ((449960l * info->value) / 1024l) - 273150;
134 
135 	mutex_unlock(&info->lock);
136 
137 	return 0;
138 }
139 
140 static int stmpe_read_raw(struct iio_dev *indio_dev,
141 			  struct iio_chan_spec const *chan,
142 			  int *val,
143 			  int *val2,
144 			  long mask)
145 {
146 	struct stmpe_adc *info = iio_priv(indio_dev);
147 	long ret;
148 
149 	switch (mask) {
150 	case IIO_CHAN_INFO_RAW:
151 	case IIO_CHAN_INFO_PROCESSED:
152 
153 		switch (chan->type) {
154 		case IIO_VOLTAGE:
155 			ret = stmpe_read_voltage(info, chan, val);
156 			break;
157 
158 		case IIO_TEMP:
159 			ret = stmpe_read_temp(info, chan, val);
160 			break;
161 		default:
162 			return -EINVAL;
163 		}
164 
165 		if (ret < 0)
166 			return ret;
167 
168 		return IIO_VAL_INT;
169 
170 	case IIO_CHAN_INFO_SCALE:
171 		*val = 3300;
172 		*val2 = info->stmpe->mod_12b ? 12 : 10;
173 		return IIO_VAL_FRACTIONAL_LOG2;
174 
175 	default:
176 		break;
177 	}
178 
179 	return -EINVAL;
180 }
181 
182 static irqreturn_t stmpe_adc_isr(int irq, void *dev_id)
183 {
184 	struct stmpe_adc *info = (struct stmpe_adc *)dev_id;
185 	u16 data;
186 
187 	if (info->channel > STMPE_TEMP_CHANNEL)
188 		return IRQ_NONE;
189 
190 	if (info->channel <= STMPE_ADC_LAST_NR) {
191 		int int_sta;
192 
193 		int_sta = stmpe_reg_read(info->stmpe, STMPE_REG_ADC_INT_STA);
194 
195 		/* Is the interrupt relevant */
196 		if (!(int_sta & STMPE_ADC_CH(info->channel)))
197 			return IRQ_NONE;
198 
199 		/* Read value */
200 		stmpe_block_read(info->stmpe,
201 			STMPE_REG_ADC_DATA_CH(info->channel), 2, (u8 *) &data);
202 
203 		stmpe_reg_write(info->stmpe, STMPE_REG_ADC_INT_STA, int_sta);
204 	} else if (info->channel == STMPE_TEMP_CHANNEL) {
205 		/* Read value */
206 		stmpe_block_read(info->stmpe, STMPE_REG_TEMP_DATA, 2,
207 				(u8 *) &data);
208 	}
209 
210 	info->value = (u32) be16_to_cpu(data);
211 	complete(&info->completion);
212 
213 	return IRQ_HANDLED;
214 }
215 
216 static const struct iio_info stmpe_adc_iio_info = {
217 	.read_raw = &stmpe_read_raw,
218 };
219 
220 static void stmpe_adc_voltage_chan(struct iio_chan_spec *ics, int chan)
221 {
222 	ics->type = IIO_VOLTAGE;
223 	ics->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
224 	ics->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
225 	ics->indexed = 1;
226 	ics->channel = chan;
227 }
228 
229 static void stmpe_adc_temp_chan(struct iio_chan_spec *ics, int chan)
230 {
231 	ics->type = IIO_TEMP;
232 	ics->info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED);
233 	ics->indexed = 1;
234 	ics->channel = chan;
235 }
236 
237 static int stmpe_adc_init_hw(struct stmpe_adc *adc)
238 {
239 	int ret;
240 	struct stmpe *stmpe = adc->stmpe;
241 
242 	ret = stmpe_enable(stmpe, STMPE_BLOCK_ADC);
243 	if (ret) {
244 		dev_err(stmpe->dev, "Could not enable clock for ADC\n");
245 		return ret;
246 	}
247 
248 	ret = stmpe811_adc_common_init(stmpe);
249 	if (ret) {
250 		stmpe_disable(stmpe, STMPE_BLOCK_ADC);
251 		return ret;
252 	}
253 
254 	/* use temp irq for each conversion completion */
255 	stmpe_reg_write(stmpe, STMPE_REG_TEMP_TH, 0);
256 	stmpe_reg_write(stmpe, STMPE_REG_TEMP_TH + 1, 0);
257 
258 	return 0;
259 }
260 
261 static int stmpe_adc_probe(struct platform_device *pdev)
262 {
263 	struct iio_dev *indio_dev;
264 	struct stmpe_adc *info;
265 	struct device_node *np;
266 	u32 norequest_mask = 0;
267 	int irq_temp, irq_adc;
268 	int num_chan = 0;
269 	int i = 0;
270 	int ret;
271 
272 	irq_adc = platform_get_irq_byname(pdev, "STMPE_ADC");
273 	if (irq_adc < 0)
274 		return irq_adc;
275 
276 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct stmpe_adc));
277 	if (!indio_dev) {
278 		dev_err(&pdev->dev, "failed allocating iio device\n");
279 		return -ENOMEM;
280 	}
281 
282 	info = iio_priv(indio_dev);
283 	mutex_init(&info->lock);
284 
285 	init_completion(&info->completion);
286 	ret = devm_request_threaded_irq(&pdev->dev, irq_adc, NULL,
287 					stmpe_adc_isr, IRQF_ONESHOT,
288 					"stmpe-adc", info);
289 	if (ret < 0) {
290 		dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
291 				irq_adc);
292 		return ret;
293 	}
294 
295 	irq_temp = platform_get_irq_byname(pdev, "STMPE_TEMP_SENS");
296 	if (irq_temp >= 0) {
297 		ret = devm_request_threaded_irq(&pdev->dev, irq_temp, NULL,
298 						stmpe_adc_isr, IRQF_ONESHOT,
299 						"stmpe-adc", info);
300 		if (ret < 0)
301 			dev_warn(&pdev->dev, "failed requesting irq for"
302 				 " temp sensor, irq = %d\n", irq_temp);
303 	}
304 
305 	platform_set_drvdata(pdev, indio_dev);
306 
307 	indio_dev->name		= dev_name(&pdev->dev);
308 	indio_dev->dev.parent	= &pdev->dev;
309 	indio_dev->info		= &stmpe_adc_iio_info;
310 	indio_dev->modes	= INDIO_DIRECT_MODE;
311 
312 	info->stmpe = dev_get_drvdata(pdev->dev.parent);
313 
314 	np = pdev->dev.of_node;
315 
316 	if (!np)
317 		dev_err(&pdev->dev, "no device tree node found\n");
318 
319 	of_property_read_u32(np, "st,norequest-mask", &norequest_mask);
320 
321 	for_each_clear_bit(i, (unsigned long *) &norequest_mask,
322 			   (STMPE_ADC_LAST_NR + 1)) {
323 		stmpe_adc_voltage_chan(&info->stmpe_adc_iio_channels[num_chan], i);
324 		num_chan++;
325 	}
326 	stmpe_adc_temp_chan(&info->stmpe_adc_iio_channels[num_chan], i);
327 	num_chan++;
328 	indio_dev->channels = info->stmpe_adc_iio_channels;
329 	indio_dev->num_channels = num_chan;
330 
331 	ret = stmpe_adc_init_hw(info);
332 	if (ret)
333 		return ret;
334 
335 	return devm_iio_device_register(&pdev->dev, indio_dev);
336 }
337 
338 static int __maybe_unused stmpe_adc_resume(struct device *dev)
339 {
340 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
341 	struct stmpe_adc *info = iio_priv(indio_dev);
342 
343 	stmpe_adc_init_hw(info);
344 
345 	return 0;
346 }
347 
348 static SIMPLE_DEV_PM_OPS(stmpe_adc_pm_ops, NULL, stmpe_adc_resume);
349 
350 static struct platform_driver stmpe_adc_driver = {
351 	.probe		= stmpe_adc_probe,
352 	.driver		= {
353 		.name	= "stmpe-adc",
354 		.pm	= &stmpe_adc_pm_ops,
355 	},
356 };
357 
358 module_platform_driver(stmpe_adc_driver);
359 
360 MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
361 MODULE_DESCRIPTION("STMPEXXX ADC driver");
362 MODULE_LICENSE("GPL v2");
363 MODULE_ALIAS("platform:stmpe-adc");
364