xref: /openbmc/linux/drivers/iio/adc/rcar-gyroadc.c (revision 9e255e2b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Renesas R-Car GyroADC driver
4  *
5  * Copyright 2016 Marek Vasut <marek.vasut@gmail.com>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/io.h>
14 #include <linux/clk.h>
15 #include <linux/of.h>
16 #include <linux/of_irq.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/of_platform.h>
19 #include <linux/err.h>
20 #include <linux/pm_runtime.h>
21 
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
25 
26 #define DRIVER_NAME				"rcar-gyroadc"
27 
28 /* GyroADC registers. */
29 #define RCAR_GYROADC_MODE_SELECT		0x00
30 #define RCAR_GYROADC_MODE_SELECT_1_MB88101A	0x0
31 #define RCAR_GYROADC_MODE_SELECT_2_ADCS7476	0x1
32 #define RCAR_GYROADC_MODE_SELECT_3_MAX1162	0x3
33 
34 #define RCAR_GYROADC_START_STOP			0x04
35 #define RCAR_GYROADC_START_STOP_START		BIT(0)
36 
37 #define RCAR_GYROADC_CLOCK_LENGTH		0x08
38 #define RCAR_GYROADC_1_25MS_LENGTH		0x0c
39 
40 #define RCAR_GYROADC_REALTIME_DATA(ch)		(0x10 + ((ch) * 4))
41 #define RCAR_GYROADC_100MS_ADDED_DATA(ch)	(0x30 + ((ch) * 4))
42 #define RCAR_GYROADC_10MS_AVG_DATA(ch)		(0x50 + ((ch) * 4))
43 
44 #define RCAR_GYROADC_FIFO_STATUS		0x70
45 #define RCAR_GYROADC_FIFO_STATUS_EMPTY(ch)	BIT(0 + (4 * (ch)))
46 #define RCAR_GYROADC_FIFO_STATUS_FULL(ch)	BIT(1 + (4 * (ch)))
47 #define RCAR_GYROADC_FIFO_STATUS_ERROR(ch)	BIT(2 + (4 * (ch)))
48 
49 #define RCAR_GYROADC_INTR			0x74
50 #define RCAR_GYROADC_INTR_INT			BIT(0)
51 
52 #define RCAR_GYROADC_INTENR			0x78
53 #define RCAR_GYROADC_INTENR_INTEN		BIT(0)
54 
55 #define RCAR_GYROADC_SAMPLE_RATE		800	/* Hz */
56 
57 #define RCAR_GYROADC_RUNTIME_PM_DELAY_MS	2000
58 
59 enum rcar_gyroadc_model {
60 	RCAR_GYROADC_MODEL_DEFAULT,
61 	RCAR_GYROADC_MODEL_R8A7792,
62 };
63 
64 struct rcar_gyroadc {
65 	struct device			*dev;
66 	void __iomem			*regs;
67 	struct clk			*clk;
68 	struct regulator		*vref[8];
69 	unsigned int			num_channels;
70 	enum rcar_gyroadc_model		model;
71 	unsigned int			mode;
72 	unsigned int			sample_width;
73 };
74 
75 static void rcar_gyroadc_hw_init(struct rcar_gyroadc *priv)
76 {
77 	const unsigned long clk_mhz = clk_get_rate(priv->clk) / 1000000;
78 	const unsigned long clk_mul =
79 		(priv->mode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) ? 10 : 5;
80 	unsigned long clk_len = clk_mhz * clk_mul;
81 
82 	/*
83 	 * According to the R-Car Gen2 datasheet Rev. 1.01, Sept 08 2014,
84 	 * page 77-7, clock length must be even number. If it's odd number,
85 	 * add one.
86 	 */
87 	if (clk_len & 1)
88 		clk_len++;
89 
90 	/* Stop the GyroADC. */
91 	writel(0, priv->regs + RCAR_GYROADC_START_STOP);
92 
93 	/* Disable IRQ on V2H. */
94 	if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
95 		writel(0, priv->regs + RCAR_GYROADC_INTENR);
96 
97 	/* Set mode and timing. */
98 	writel(priv->mode, priv->regs + RCAR_GYROADC_MODE_SELECT);
99 	writel(clk_len, priv->regs + RCAR_GYROADC_CLOCK_LENGTH);
100 	writel(clk_mhz * 1250, priv->regs + RCAR_GYROADC_1_25MS_LENGTH);
101 }
102 
103 static void rcar_gyroadc_hw_start(struct rcar_gyroadc *priv)
104 {
105 	/* Start sampling. */
106 	writel(RCAR_GYROADC_START_STOP_START,
107 	       priv->regs + RCAR_GYROADC_START_STOP);
108 
109 	/*
110 	 * Wait for the first conversion to complete. This is longer than
111 	 * the 1.25 mS in the datasheet because 1.25 mS is not enough for
112 	 * the hardware to deliver the first sample and the hardware does
113 	 * then return zeroes instead of valid data.
114 	 */
115 	mdelay(3);
116 }
117 
118 static void rcar_gyroadc_hw_stop(struct rcar_gyroadc *priv)
119 {
120 	/* Stop the GyroADC. */
121 	writel(0, priv->regs + RCAR_GYROADC_START_STOP);
122 }
123 
124 #define RCAR_GYROADC_CHAN(_idx) {				\
125 	.type			= IIO_VOLTAGE,			\
126 	.indexed		= 1,				\
127 	.channel		= (_idx),			\
128 	.info_mask_separate	= BIT(IIO_CHAN_INFO_RAW) |	\
129 				  BIT(IIO_CHAN_INFO_SCALE),	\
130 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
131 }
132 
133 static const struct iio_chan_spec rcar_gyroadc_iio_channels_1[] = {
134 	RCAR_GYROADC_CHAN(0),
135 	RCAR_GYROADC_CHAN(1),
136 	RCAR_GYROADC_CHAN(2),
137 	RCAR_GYROADC_CHAN(3),
138 };
139 
140 static const struct iio_chan_spec rcar_gyroadc_iio_channels_2[] = {
141 	RCAR_GYROADC_CHAN(0),
142 	RCAR_GYROADC_CHAN(1),
143 	RCAR_GYROADC_CHAN(2),
144 	RCAR_GYROADC_CHAN(3),
145 	RCAR_GYROADC_CHAN(4),
146 	RCAR_GYROADC_CHAN(5),
147 	RCAR_GYROADC_CHAN(6),
148 	RCAR_GYROADC_CHAN(7),
149 };
150 
151 static const struct iio_chan_spec rcar_gyroadc_iio_channels_3[] = {
152 	RCAR_GYROADC_CHAN(0),
153 	RCAR_GYROADC_CHAN(1),
154 	RCAR_GYROADC_CHAN(2),
155 	RCAR_GYROADC_CHAN(3),
156 	RCAR_GYROADC_CHAN(4),
157 	RCAR_GYROADC_CHAN(5),
158 	RCAR_GYROADC_CHAN(6),
159 	RCAR_GYROADC_CHAN(7),
160 };
161 
162 static int rcar_gyroadc_set_power(struct rcar_gyroadc *priv, bool on)
163 {
164 	struct device *dev = priv->dev;
165 	int ret;
166 
167 	if (on) {
168 		ret = pm_runtime_get_sync(dev);
169 		if (ret < 0)
170 			pm_runtime_put_noidle(dev);
171 	} else {
172 		pm_runtime_mark_last_busy(dev);
173 		ret = pm_runtime_put_autosuspend(dev);
174 	}
175 
176 	return ret;
177 }
178 
179 static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
180 				 struct iio_chan_spec const *chan,
181 				 int *val, int *val2, long mask)
182 {
183 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
184 	struct regulator *consumer;
185 	unsigned int datareg = RCAR_GYROADC_REALTIME_DATA(chan->channel);
186 	unsigned int vref;
187 	int ret;
188 
189 	/*
190 	 * MB88101 is special in that it has only single regulator for
191 	 * all four channels.
192 	 */
193 	if (priv->mode == RCAR_GYROADC_MODE_SELECT_1_MB88101A)
194 		consumer = priv->vref[0];
195 	else
196 		consumer = priv->vref[chan->channel];
197 
198 	switch (mask) {
199 	case IIO_CHAN_INFO_RAW:
200 		if (chan->type != IIO_VOLTAGE)
201 			return -EINVAL;
202 
203 		/* Channel not connected. */
204 		if (!consumer)
205 			return -EINVAL;
206 
207 		ret = iio_device_claim_direct_mode(indio_dev);
208 		if (ret)
209 			return ret;
210 
211 		ret = rcar_gyroadc_set_power(priv, true);
212 		if (ret < 0) {
213 			iio_device_release_direct_mode(indio_dev);
214 			return ret;
215 		}
216 
217 		*val = readl(priv->regs + datareg);
218 		*val &= BIT(priv->sample_width) - 1;
219 
220 		ret = rcar_gyroadc_set_power(priv, false);
221 		iio_device_release_direct_mode(indio_dev);
222 		if (ret < 0)
223 			return ret;
224 
225 		return IIO_VAL_INT;
226 	case IIO_CHAN_INFO_SCALE:
227 		/* Channel not connected. */
228 		if (!consumer)
229 			return -EINVAL;
230 
231 		vref = regulator_get_voltage(consumer);
232 		*val = vref / 1000;
233 		*val2 = 1 << priv->sample_width;
234 
235 		return IIO_VAL_FRACTIONAL;
236 	case IIO_CHAN_INFO_SAMP_FREQ:
237 		*val = RCAR_GYROADC_SAMPLE_RATE;
238 
239 		return IIO_VAL_INT;
240 	default:
241 		return -EINVAL;
242 	}
243 }
244 
245 static int rcar_gyroadc_reg_access(struct iio_dev *indio_dev,
246 				   unsigned int reg, unsigned int writeval,
247 				   unsigned int *readval)
248 {
249 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
250 	unsigned int maxreg = RCAR_GYROADC_FIFO_STATUS;
251 
252 	if (readval == NULL)
253 		return -EINVAL;
254 
255 	if (reg % 4)
256 		return -EINVAL;
257 
258 	/* Handle the V2H case with extra interrupt block. */
259 	if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
260 		maxreg = RCAR_GYROADC_INTENR;
261 
262 	if (reg > maxreg)
263 		return -EINVAL;
264 
265 	*readval = readl(priv->regs + reg);
266 
267 	return 0;
268 }
269 
270 static const struct iio_info rcar_gyroadc_iio_info = {
271 	.read_raw		= rcar_gyroadc_read_raw,
272 	.debugfs_reg_access	= rcar_gyroadc_reg_access,
273 };
274 
275 static const struct of_device_id rcar_gyroadc_match[] = {
276 	{
277 		/* R-Car compatible GyroADC */
278 		.compatible	= "renesas,rcar-gyroadc",
279 		.data		= (void *)RCAR_GYROADC_MODEL_DEFAULT,
280 	}, {
281 		/* R-Car V2H specialty with interrupt registers. */
282 		.compatible	= "renesas,r8a7792-gyroadc",
283 		.data		= (void *)RCAR_GYROADC_MODEL_R8A7792,
284 	}, {
285 		/* sentinel */
286 	}
287 };
288 
289 MODULE_DEVICE_TABLE(of, rcar_gyroadc_match);
290 
291 static const struct of_device_id rcar_gyroadc_child_match[] = {
292 	/* Mode 1 ADCs */
293 	{
294 		.compatible	= "fujitsu,mb88101a",
295 		.data		= (void *)RCAR_GYROADC_MODE_SELECT_1_MB88101A,
296 	},
297 	/* Mode 2 ADCs */
298 	{
299 		.compatible	= "ti,adcs7476",
300 		.data		= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
301 	}, {
302 		.compatible	= "ti,adc121",
303 		.data		= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
304 	}, {
305 		.compatible	= "adi,ad7476",
306 		.data		= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
307 	},
308 	/* Mode 3 ADCs */
309 	{
310 		.compatible	= "maxim,max1162",
311 		.data		= (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
312 	}, {
313 		.compatible	= "maxim,max11100",
314 		.data		= (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
315 	},
316 	{ /* sentinel */ }
317 };
318 
319 static int rcar_gyroadc_parse_subdevs(struct iio_dev *indio_dev)
320 {
321 	const struct of_device_id *of_id;
322 	const struct iio_chan_spec *channels;
323 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
324 	struct device *dev = priv->dev;
325 	struct device_node *np = dev->of_node;
326 	struct device_node *child;
327 	struct regulator *vref;
328 	unsigned int reg;
329 	unsigned int adcmode = -1, childmode;
330 	unsigned int sample_width;
331 	unsigned int num_channels;
332 	int ret, first = 1;
333 
334 	for_each_child_of_node(np, child) {
335 		of_id = of_match_node(rcar_gyroadc_child_match, child);
336 		if (!of_id) {
337 			dev_err(dev, "Ignoring unsupported ADC \"%pOFn\".",
338 				child);
339 			continue;
340 		}
341 
342 		childmode = (uintptr_t)of_id->data;
343 		switch (childmode) {
344 		case RCAR_GYROADC_MODE_SELECT_1_MB88101A:
345 			sample_width = 12;
346 			channels = rcar_gyroadc_iio_channels_1;
347 			num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_1);
348 			break;
349 		case RCAR_GYROADC_MODE_SELECT_2_ADCS7476:
350 			sample_width = 15;
351 			channels = rcar_gyroadc_iio_channels_2;
352 			num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_2);
353 			break;
354 		case RCAR_GYROADC_MODE_SELECT_3_MAX1162:
355 			sample_width = 16;
356 			channels = rcar_gyroadc_iio_channels_3;
357 			num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_3);
358 			break;
359 		default:
360 			goto err_e_inval;
361 		}
362 
363 		/*
364 		 * MB88101 is special in that it's only a single chip taking
365 		 * up all the CHS lines. Thus, the DT binding is also special
366 		 * and has no reg property. If we run into such ADC, handle
367 		 * it here.
368 		 */
369 		if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
370 			reg = 0;
371 		} else {
372 			ret = of_property_read_u32(child, "reg", &reg);
373 			if (ret) {
374 				dev_err(dev,
375 					"Failed to get child reg property of ADC \"%pOFn\".\n",
376 					child);
377 				goto err_of_node_put;
378 			}
379 
380 			/* Channel number is too high. */
381 			if (reg >= num_channels) {
382 				dev_err(dev,
383 					"Only %i channels supported with %pOFn, but reg = <%i>.\n",
384 					num_channels, child, reg);
385 				goto err_e_inval;
386 			}
387 		}
388 
389 		/* Child node selected different mode than the rest. */
390 		if (!first && (adcmode != childmode)) {
391 			dev_err(dev,
392 				"Channel %i uses different ADC mode than the rest.\n",
393 				reg);
394 			goto err_e_inval;
395 		}
396 
397 		/* Channel is valid, grab the regulator. */
398 		dev->of_node = child;
399 		vref = devm_regulator_get(dev, "vref");
400 		dev->of_node = np;
401 		if (IS_ERR(vref)) {
402 			dev_dbg(dev, "Channel %i 'vref' supply not connected.\n",
403 				reg);
404 			ret = PTR_ERR(vref);
405 			goto err_of_node_put;
406 		}
407 
408 		priv->vref[reg] = vref;
409 
410 		if (!first)
411 			continue;
412 
413 		/* First child node which passed sanity tests. */
414 		adcmode = childmode;
415 		first = 0;
416 
417 		priv->num_channels = num_channels;
418 		priv->mode = childmode;
419 		priv->sample_width = sample_width;
420 
421 		indio_dev->channels = channels;
422 		indio_dev->num_channels = num_channels;
423 
424 		/*
425 		 * MB88101 is special and we only have one such device
426 		 * attached to the GyroADC at a time, so if we found it,
427 		 * we can stop parsing here.
428 		 */
429 		if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
430 			of_node_put(child);
431 			break;
432 		}
433 	}
434 
435 	if (first) {
436 		dev_err(dev, "No valid ADC channels found, aborting.\n");
437 		return -EINVAL;
438 	}
439 
440 	return 0;
441 
442 err_e_inval:
443 	ret = -EINVAL;
444 err_of_node_put:
445 	of_node_put(child);
446 	return ret;
447 }
448 
449 static void rcar_gyroadc_deinit_supplies(struct iio_dev *indio_dev)
450 {
451 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
452 	unsigned int i;
453 
454 	for (i = 0; i < priv->num_channels; i++) {
455 		if (!priv->vref[i])
456 			continue;
457 
458 		regulator_disable(priv->vref[i]);
459 	}
460 }
461 
462 static int rcar_gyroadc_init_supplies(struct iio_dev *indio_dev)
463 {
464 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
465 	struct device *dev = priv->dev;
466 	unsigned int i;
467 	int ret;
468 
469 	for (i = 0; i < priv->num_channels; i++) {
470 		if (!priv->vref[i])
471 			continue;
472 
473 		ret = regulator_enable(priv->vref[i]);
474 		if (ret) {
475 			dev_err(dev, "Failed to enable regulator %i (ret=%i)\n",
476 				i, ret);
477 			goto err;
478 		}
479 	}
480 
481 	return 0;
482 
483 err:
484 	rcar_gyroadc_deinit_supplies(indio_dev);
485 	return ret;
486 }
487 
488 static int rcar_gyroadc_probe(struct platform_device *pdev)
489 {
490 	struct device *dev = &pdev->dev;
491 	struct rcar_gyroadc *priv;
492 	struct iio_dev *indio_dev;
493 	int ret;
494 
495 	indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
496 	if (!indio_dev)
497 		return -ENOMEM;
498 
499 	priv = iio_priv(indio_dev);
500 	priv->dev = dev;
501 
502 	priv->regs = devm_platform_ioremap_resource(pdev, 0);
503 	if (IS_ERR(priv->regs))
504 		return PTR_ERR(priv->regs);
505 
506 	priv->clk = devm_clk_get(dev, "fck");
507 	if (IS_ERR(priv->clk))
508 		return dev_err_probe(dev, PTR_ERR(priv->clk),
509 				     "Failed to get IF clock\n");
510 
511 	ret = rcar_gyroadc_parse_subdevs(indio_dev);
512 	if (ret)
513 		return ret;
514 
515 	ret = rcar_gyroadc_init_supplies(indio_dev);
516 	if (ret)
517 		return ret;
518 
519 	priv->model = (enum rcar_gyroadc_model)
520 		of_device_get_match_data(&pdev->dev);
521 
522 	platform_set_drvdata(pdev, indio_dev);
523 
524 	indio_dev->name = DRIVER_NAME;
525 	indio_dev->info = &rcar_gyroadc_iio_info;
526 	indio_dev->modes = INDIO_DIRECT_MODE;
527 
528 	ret = clk_prepare_enable(priv->clk);
529 	if (ret) {
530 		dev_err(dev, "Could not prepare or enable the IF clock.\n");
531 		goto err_clk_if_enable;
532 	}
533 
534 	pm_runtime_set_autosuspend_delay(dev, RCAR_GYROADC_RUNTIME_PM_DELAY_MS);
535 	pm_runtime_use_autosuspend(dev);
536 	pm_runtime_enable(dev);
537 
538 	pm_runtime_get_sync(dev);
539 	rcar_gyroadc_hw_init(priv);
540 	rcar_gyroadc_hw_start(priv);
541 
542 	ret = iio_device_register(indio_dev);
543 	if (ret) {
544 		dev_err(dev, "Couldn't register IIO device.\n");
545 		goto err_iio_device_register;
546 	}
547 
548 	pm_runtime_put_sync(dev);
549 
550 	return 0;
551 
552 err_iio_device_register:
553 	rcar_gyroadc_hw_stop(priv);
554 	pm_runtime_put_sync(dev);
555 	pm_runtime_disable(dev);
556 	pm_runtime_set_suspended(dev);
557 	clk_disable_unprepare(priv->clk);
558 err_clk_if_enable:
559 	rcar_gyroadc_deinit_supplies(indio_dev);
560 
561 	return ret;
562 }
563 
564 static int rcar_gyroadc_remove(struct platform_device *pdev)
565 {
566 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
567 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
568 	struct device *dev = priv->dev;
569 
570 	iio_device_unregister(indio_dev);
571 	pm_runtime_get_sync(dev);
572 	rcar_gyroadc_hw_stop(priv);
573 	pm_runtime_put_sync(dev);
574 	pm_runtime_disable(dev);
575 	pm_runtime_set_suspended(dev);
576 	clk_disable_unprepare(priv->clk);
577 	rcar_gyroadc_deinit_supplies(indio_dev);
578 
579 	return 0;
580 }
581 
582 #if defined(CONFIG_PM)
583 static int rcar_gyroadc_suspend(struct device *dev)
584 {
585 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
586 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
587 
588 	rcar_gyroadc_hw_stop(priv);
589 
590 	return 0;
591 }
592 
593 static int rcar_gyroadc_resume(struct device *dev)
594 {
595 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
596 	struct rcar_gyroadc *priv = iio_priv(indio_dev);
597 
598 	rcar_gyroadc_hw_start(priv);
599 
600 	return 0;
601 }
602 #endif
603 
604 static const struct dev_pm_ops rcar_gyroadc_pm_ops = {
605 	SET_RUNTIME_PM_OPS(rcar_gyroadc_suspend, rcar_gyroadc_resume, NULL)
606 };
607 
608 static struct platform_driver rcar_gyroadc_driver = {
609 	.probe          = rcar_gyroadc_probe,
610 	.remove         = rcar_gyroadc_remove,
611 	.driver         = {
612 		.name		= DRIVER_NAME,
613 		.of_match_table	= rcar_gyroadc_match,
614 		.pm		= &rcar_gyroadc_pm_ops,
615 	},
616 };
617 
618 module_platform_driver(rcar_gyroadc_driver);
619 
620 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
621 MODULE_DESCRIPTION("Renesas R-Car GyroADC driver");
622 MODULE_LICENSE("GPL");
623