File Index
Files and directories
Sign in
FILE

rcar-gyroadc.c

File preview with syntax highlighting.

linux-6.19.14/drivers/iio/adc/rcar-gyroadc.c C
  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
 59enum rcar_gyroadc_model {
 60	RCAR_GYROADC_MODEL_DEFAULT,
 61	RCAR_GYROADC_MODEL_R8A7792,
 62};
 63
 64struct 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
 75static 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
103static 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
118static 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
133static 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
140static 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
151static 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
162static int rcar_gyroadc_set_power(struct rcar_gyroadc *priv, bool on)
163{
164	struct device *dev = priv->dev;
165
166	if (on)
167		return pm_runtime_resume_and_get(dev);
168
169	return pm_runtime_put_autosuspend(dev);
170}
171
172static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
173				 struct iio_chan_spec const *chan,
174				 int *val, int *val2, long mask)
175{
176	struct rcar_gyroadc *priv = iio_priv(indio_dev);
177	struct regulator *consumer;
178	unsigned int datareg = RCAR_GYROADC_REALTIME_DATA(chan->channel);
179	unsigned int vref;
180	int ret;
181
182	/*
183	 * MB88101 is special in that it has only single regulator for
184	 * all four channels.
185	 */
186	if (priv->mode == RCAR_GYROADC_MODE_SELECT_1_MB88101A)
187		consumer = priv->vref[0];
188	else
189		consumer = priv->vref[chan->channel];
190
191	switch (mask) {
192	case IIO_CHAN_INFO_RAW:
193		if (chan->type != IIO_VOLTAGE)
194			return -EINVAL;
195
196		/* Channel not connected. */
197		if (!consumer)
198			return -EINVAL;
199
200		if (!iio_device_claim_direct(indio_dev))
201			return -EBUSY;
202
203		ret = rcar_gyroadc_set_power(priv, true);
204		if (ret < 0) {
205			iio_device_release_direct(indio_dev);
206			return ret;
207		}
208
209		*val = readl(priv->regs + datareg);
210		*val &= BIT(priv->sample_width) - 1;
211
212		ret = rcar_gyroadc_set_power(priv, false);
213		iio_device_release_direct(indio_dev);
214		if (ret < 0)
215			return ret;
216
217		return IIO_VAL_INT;
218	case IIO_CHAN_INFO_SCALE:
219		/* Channel not connected. */
220		if (!consumer)
221			return -EINVAL;
222
223		vref = regulator_get_voltage(consumer);
224		*val = vref / 1000;
225		*val2 = 1 << priv->sample_width;
226
227		return IIO_VAL_FRACTIONAL;
228	case IIO_CHAN_INFO_SAMP_FREQ:
229		*val = RCAR_GYROADC_SAMPLE_RATE;
230
231		return IIO_VAL_INT;
232	default:
233		return -EINVAL;
234	}
235}
236
237static int rcar_gyroadc_reg_access(struct iio_dev *indio_dev,
238				   unsigned int reg, unsigned int writeval,
239				   unsigned int *readval)
240{
241	struct rcar_gyroadc *priv = iio_priv(indio_dev);
242	unsigned int maxreg = RCAR_GYROADC_FIFO_STATUS;
243
244	if (readval == NULL)
245		return -EINVAL;
246
247	if (reg % 4)
248		return -EINVAL;
249
250	/* Handle the V2H case with extra interrupt block. */
251	if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
252		maxreg = RCAR_GYROADC_INTENR;
253
254	if (reg > maxreg)
255		return -EINVAL;
256
257	*readval = readl(priv->regs + reg);
258
259	return 0;
260}
261
262static const struct iio_info rcar_gyroadc_iio_info = {
263	.read_raw		= rcar_gyroadc_read_raw,
264	.debugfs_reg_access	= rcar_gyroadc_reg_access,
265};
266
267static const struct of_device_id rcar_gyroadc_match[] = {
268	{
269		/* R-Car compatible GyroADC */
270		.compatible	= "renesas,rcar-gyroadc",
271		.data		= (void *)RCAR_GYROADC_MODEL_DEFAULT,
272	}, {
273		/* R-Car V2H specialty with interrupt registers. */
274		.compatible	= "renesas,r8a7792-gyroadc",
275		.data		= (void *)RCAR_GYROADC_MODEL_R8A7792,
276	}, {
277		/* sentinel */
278	}
279};
280
281MODULE_DEVICE_TABLE(of, rcar_gyroadc_match);
282
283static const struct of_device_id rcar_gyroadc_child_match[] __maybe_unused = {
284	/* Mode 1 ADCs */
285	{
286		.compatible	= "fujitsu,mb88101a",
287		.data		= (void *)RCAR_GYROADC_MODE_SELECT_1_MB88101A,
288	},
289	/* Mode 2 ADCs */
290	{
291		.compatible	= "ti,adcs7476",
292		.data		= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
293	}, {
294		.compatible	= "ti,adc121",
295		.data		= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
296	}, {
297		.compatible	= "adi,ad7476",
298		.data		= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
299	},
300	/* Mode 3 ADCs */
301	{
302		.compatible	= "maxim,max1162",
303		.data		= (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
304	}, {
305		.compatible	= "maxim,max11100",
306		.data		= (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
307	},
308	{ }
309};
310
311static int rcar_gyroadc_parse_subdevs(struct iio_dev *indio_dev)
312{
313	const struct of_device_id *of_id;
314	const struct iio_chan_spec *channels;
315	struct rcar_gyroadc *priv = iio_priv(indio_dev);
316	struct device *dev = priv->dev;
317	struct device_node *np = dev->of_node;
318	struct regulator *vref;
319	unsigned int reg;
320	unsigned int adcmode = -1, childmode;
321	unsigned int sample_width;
322	unsigned int num_channels;
323	int ret, first = 1;
324
325	for_each_available_child_of_node_scoped(np, child) {
326		of_id = of_match_node(rcar_gyroadc_child_match, child);
327		if (!of_id) {
328			dev_err(dev, "Ignoring unsupported ADC \"%pOFn\".",
329				child);
330			continue;
331		}
332
333		childmode = (uintptr_t)of_id->data;
334		switch (childmode) {
335		case RCAR_GYROADC_MODE_SELECT_1_MB88101A:
336			sample_width = 12;
337			channels = rcar_gyroadc_iio_channels_1;
338			num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_1);
339			break;
340		case RCAR_GYROADC_MODE_SELECT_2_ADCS7476:
341			sample_width = 15;
342			channels = rcar_gyroadc_iio_channels_2;
343			num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_2);
344			break;
345		case RCAR_GYROADC_MODE_SELECT_3_MAX1162:
346			sample_width = 16;
347			channels = rcar_gyroadc_iio_channels_3;
348			num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_3);
349			break;
350		default:
351			return -EINVAL;
352		}
353
354		/*
355		 * MB88101 is special in that it's only a single chip taking
356		 * up all the CHS lines. Thus, the DT binding is also special
357		 * and has no reg property. If we run into such ADC, handle
358		 * it here.
359		 */
360		if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
361			reg = 0;
362		} else {
363			ret = of_property_read_u32(child, "reg", &reg);
364			if (ret) {
365				dev_err(dev,
366					"Failed to get child reg property of ADC \"%pOFn\".\n",
367					child);
368				return ret;
369			}
370
371			/* Channel number is too high. */
372			if (reg >= num_channels) {
373				dev_err(dev,
374					"Only %i channels supported with %pOFn, but reg = <%i>.\n",
375					num_channels, child, reg);
376				return -EINVAL;
377			}
378		}
379
380		/* Child node selected different mode than the rest. */
381		if (!first && (adcmode != childmode)) {
382			dev_err(dev,
383				"Channel %i uses different ADC mode than the rest.\n",
384				reg);
385			return -EINVAL;
386		}
387
388		/* Channel is valid, grab the regulator. */
389		dev->of_node = child;
390		vref = devm_regulator_get(dev, "vref");
391		dev->of_node = np;
392		if (IS_ERR(vref)) {
393			dev_dbg(dev, "Channel %i 'vref' supply not connected.\n",
394				reg);
395			return PTR_ERR(vref);
396		}
397
398		priv->vref[reg] = vref;
399
400		if (!first)
401			continue;
402
403		/* First child node which passed sanity tests. */
404		adcmode = childmode;
405		first = 0;
406
407		priv->num_channels = num_channels;
408		priv->mode = childmode;
409		priv->sample_width = sample_width;
410
411		indio_dev->channels = channels;
412		indio_dev->num_channels = num_channels;
413
414		/*
415		 * MB88101 is special and we only have one such device
416		 * attached to the GyroADC at a time, so if we found it,
417		 * we can stop parsing here.
418		 */
419		if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
420			break;
421		}
422	}
423
424	if (first) {
425		dev_err(dev, "No valid ADC channels found, aborting.\n");
426		return -EINVAL;
427	}
428
429	return 0;
430}
431
432static void rcar_gyroadc_deinit_supplies(struct iio_dev *indio_dev)
433{
434	struct rcar_gyroadc *priv = iio_priv(indio_dev);
435	unsigned int i;
436
437	for (i = 0; i < priv->num_channels; i++) {
438		if (!priv->vref[i])
439			continue;
440
441		regulator_disable(priv->vref[i]);
442	}
443}
444
445static int rcar_gyroadc_init_supplies(struct iio_dev *indio_dev)
446{
447	struct rcar_gyroadc *priv = iio_priv(indio_dev);
448	struct device *dev = priv->dev;
449	unsigned int i;
450	int ret;
451
452	for (i = 0; i < priv->num_channels; i++) {
453		if (!priv->vref[i])
454			continue;
455
456		ret = regulator_enable(priv->vref[i]);
457		if (ret) {
458			dev_err(dev, "Failed to enable regulator %i (ret=%i)\n",
459				i, ret);
460			goto err;
461		}
462	}
463
464	return 0;
465
466err:
467	rcar_gyroadc_deinit_supplies(indio_dev);
468	return ret;
469}
470
471static int rcar_gyroadc_probe(struct platform_device *pdev)
472{
473	struct device *dev = &pdev->dev;
474	struct rcar_gyroadc *priv;
475	struct iio_dev *indio_dev;
476	int ret;
477
478	indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
479	if (!indio_dev)
480		return -ENOMEM;
481
482	priv = iio_priv(indio_dev);
483	priv->dev = dev;
484
485	priv->regs = devm_platform_ioremap_resource(pdev, 0);
486	if (IS_ERR(priv->regs))
487		return PTR_ERR(priv->regs);
488
489	priv->clk = devm_clk_get(dev, "fck");
490	if (IS_ERR(priv->clk))
491		return dev_err_probe(dev, PTR_ERR(priv->clk),
492				     "Failed to get IF clock\n");
493
494	ret = rcar_gyroadc_parse_subdevs(indio_dev);
495	if (ret)
496		return ret;
497
498	ret = rcar_gyroadc_init_supplies(indio_dev);
499	if (ret)
500		return ret;
501
502	priv->model = (uintptr_t)of_device_get_match_data(&pdev->dev);
503
504	platform_set_drvdata(pdev, indio_dev);
505
506	indio_dev->name = DRIVER_NAME;
507	indio_dev->info = &rcar_gyroadc_iio_info;
508	indio_dev->modes = INDIO_DIRECT_MODE;
509
510	ret = clk_prepare_enable(priv->clk);
511	if (ret) {
512		dev_err(dev, "Could not prepare or enable the IF clock.\n");
513		goto err_clk_if_enable;
514	}
515
516	pm_runtime_set_autosuspend_delay(dev, RCAR_GYROADC_RUNTIME_PM_DELAY_MS);
517	pm_runtime_use_autosuspend(dev);
518	pm_runtime_enable(dev);
519
520	ret = pm_runtime_resume_and_get(dev);
521	if (ret)
522		goto err_power_up;
523
524	rcar_gyroadc_hw_init(priv);
525	rcar_gyroadc_hw_start(priv);
526
527	ret = iio_device_register(indio_dev);
528	if (ret) {
529		dev_err(dev, "Couldn't register IIO device.\n");
530		goto err_iio_device_register;
531	}
532
533	pm_runtime_put_sync(dev);
534
535	return 0;
536
537err_iio_device_register:
538	rcar_gyroadc_hw_stop(priv);
539	pm_runtime_put_sync(dev);
540err_power_up:
541	pm_runtime_disable(dev);
542	pm_runtime_set_suspended(dev);
543	clk_disable_unprepare(priv->clk);
544err_clk_if_enable:
545	rcar_gyroadc_deinit_supplies(indio_dev);
546
547	return ret;
548}
549
550static void rcar_gyroadc_remove(struct platform_device *pdev)
551{
552	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
553	struct rcar_gyroadc *priv = iio_priv(indio_dev);
554	struct device *dev = priv->dev;
555
556	iio_device_unregister(indio_dev);
557	pm_runtime_get_sync(dev);
558	rcar_gyroadc_hw_stop(priv);
559	pm_runtime_put_sync(dev);
560	pm_runtime_disable(dev);
561	pm_runtime_set_suspended(dev);
562	clk_disable_unprepare(priv->clk);
563	rcar_gyroadc_deinit_supplies(indio_dev);
564}
565
566static int rcar_gyroadc_suspend(struct device *dev)
567{
568	struct iio_dev *indio_dev = dev_get_drvdata(dev);
569	struct rcar_gyroadc *priv = iio_priv(indio_dev);
570
571	rcar_gyroadc_hw_stop(priv);
572
573	return 0;
574}
575
576static int rcar_gyroadc_resume(struct device *dev)
577{
578	struct iio_dev *indio_dev = dev_get_drvdata(dev);
579	struct rcar_gyroadc *priv = iio_priv(indio_dev);
580
581	rcar_gyroadc_hw_start(priv);
582
583	return 0;
584}
585
586static const struct dev_pm_ops rcar_gyroadc_pm_ops = {
587	RUNTIME_PM_OPS(rcar_gyroadc_suspend, rcar_gyroadc_resume, NULL)
588};
589
590static struct platform_driver rcar_gyroadc_driver = {
591	.probe          = rcar_gyroadc_probe,
592	.remove         = rcar_gyroadc_remove,
593	.driver         = {
594		.name		= DRIVER_NAME,
595		.of_match_table	= rcar_gyroadc_match,
596		.pm		= pm_ptr(&rcar_gyroadc_pm_ops),
597	},
598};
599
600module_platform_driver(rcar_gyroadc_driver);
601
602MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
603MODULE_DESCRIPTION("Renesas R-Car GyroADC driver");
604MODULE_LICENSE("GPL");
~/ Read-only file preview.