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