1 // SPDX-License-Identifier: GPL-2.0+
3 * Renesas R-Car GyroADC driver
5 * Copyright 2016 Marek Vasut <marek.vasut@gmail.com>
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>
14 #include <linux/clk.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>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
26 #define DRIVER_NAME "rcar-gyroadc"
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
34 #define RCAR_GYROADC_START_STOP 0x04
35 #define RCAR_GYROADC_START_STOP_START BIT(0)
37 #define RCAR_GYROADC_CLOCK_LENGTH 0x08
38 #define RCAR_GYROADC_1_25MS_LENGTH 0x0c
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))
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)))
49 #define RCAR_GYROADC_INTR 0x74
50 #define RCAR_GYROADC_INTR_INT BIT(0)
52 #define RCAR_GYROADC_INTENR 0x78
53 #define RCAR_GYROADC_INTENR_INTEN BIT(0)
55 #define RCAR_GYROADC_SAMPLE_RATE 800 /* Hz */
57 #define RCAR_GYROADC_RUNTIME_PM_DELAY_MS 2000
59 enum rcar_gyroadc_model
{
60 RCAR_GYROADC_MODEL_DEFAULT
,
61 RCAR_GYROADC_MODEL_R8A7792
,
68 struct regulator
*vref
[8];
69 unsigned int num_channels
;
70 enum rcar_gyroadc_model model
;
72 unsigned int sample_width
;
75 static void rcar_gyroadc_hw_init(struct rcar_gyroadc
*priv
)
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
;
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,
90 /* Stop the GyroADC. */
91 writel(0, priv
->regs
+ RCAR_GYROADC_START_STOP
);
93 /* Disable IRQ on V2H. */
94 if (priv
->model
== RCAR_GYROADC_MODEL_R8A7792
)
95 writel(0, priv
->regs
+ RCAR_GYROADC_INTENR
);
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
);
103 static void rcar_gyroadc_hw_start(struct rcar_gyroadc
*priv
)
105 /* Start sampling. */
106 writel(RCAR_GYROADC_START_STOP_START
,
107 priv
->regs
+ RCAR_GYROADC_START_STOP
);
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.
118 static void rcar_gyroadc_hw_stop(struct rcar_gyroadc
*priv
)
120 /* Stop the GyroADC. */
121 writel(0, priv
->regs
+ RCAR_GYROADC_START_STOP
);
124 #define RCAR_GYROADC_CHAN(_idx) { \
125 .type = IIO_VOLTAGE, \
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), \
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),
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),
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),
162 static int rcar_gyroadc_set_power(struct rcar_gyroadc
*priv
, bool on
)
164 struct device
*dev
= priv
->dev
;
167 return pm_runtime_resume_and_get(dev
);
169 pm_runtime_mark_last_busy(dev
);
170 return pm_runtime_put_autosuspend(dev
);
174 static int rcar_gyroadc_read_raw(struct iio_dev
*indio_dev
,
175 struct iio_chan_spec
const *chan
,
176 int *val
, int *val2
, long mask
)
178 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
179 struct regulator
*consumer
;
180 unsigned int datareg
= RCAR_GYROADC_REALTIME_DATA(chan
->channel
);
185 * MB88101 is special in that it has only single regulator for
188 if (priv
->mode
== RCAR_GYROADC_MODE_SELECT_1_MB88101A
)
189 consumer
= priv
->vref
[0];
191 consumer
= priv
->vref
[chan
->channel
];
194 case IIO_CHAN_INFO_RAW
:
195 if (chan
->type
!= IIO_VOLTAGE
)
198 /* Channel not connected. */
202 ret
= iio_device_claim_direct_mode(indio_dev
);
206 ret
= rcar_gyroadc_set_power(priv
, true);
208 iio_device_release_direct_mode(indio_dev
);
212 *val
= readl(priv
->regs
+ datareg
);
213 *val
&= BIT(priv
->sample_width
) - 1;
215 ret
= rcar_gyroadc_set_power(priv
, false);
216 iio_device_release_direct_mode(indio_dev
);
221 case IIO_CHAN_INFO_SCALE
:
222 /* Channel not connected. */
226 vref
= regulator_get_voltage(consumer
);
228 *val2
= 1 << priv
->sample_width
;
230 return IIO_VAL_FRACTIONAL
;
231 case IIO_CHAN_INFO_SAMP_FREQ
:
232 *val
= RCAR_GYROADC_SAMPLE_RATE
;
240 static int rcar_gyroadc_reg_access(struct iio_dev
*indio_dev
,
241 unsigned int reg
, unsigned int writeval
,
242 unsigned int *readval
)
244 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
245 unsigned int maxreg
= RCAR_GYROADC_FIFO_STATUS
;
253 /* Handle the V2H case with extra interrupt block. */
254 if (priv
->model
== RCAR_GYROADC_MODEL_R8A7792
)
255 maxreg
= RCAR_GYROADC_INTENR
;
260 *readval
= readl(priv
->regs
+ reg
);
265 static const struct iio_info rcar_gyroadc_iio_info
= {
266 .read_raw
= rcar_gyroadc_read_raw
,
267 .debugfs_reg_access
= rcar_gyroadc_reg_access
,
270 static const struct of_device_id rcar_gyroadc_match
[] = {
272 /* R-Car compatible GyroADC */
273 .compatible
= "renesas,rcar-gyroadc",
274 .data
= (void *)RCAR_GYROADC_MODEL_DEFAULT
,
276 /* R-Car V2H specialty with interrupt registers. */
277 .compatible
= "renesas,r8a7792-gyroadc",
278 .data
= (void *)RCAR_GYROADC_MODEL_R8A7792
,
284 MODULE_DEVICE_TABLE(of
, rcar_gyroadc_match
);
286 static const struct of_device_id rcar_gyroadc_child_match
[] __maybe_unused
= {
289 .compatible
= "fujitsu,mb88101a",
290 .data
= (void *)RCAR_GYROADC_MODE_SELECT_1_MB88101A
,
294 .compatible
= "ti,adcs7476",
295 .data
= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476
,
297 .compatible
= "ti,adc121",
298 .data
= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476
,
300 .compatible
= "adi,ad7476",
301 .data
= (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476
,
305 .compatible
= "maxim,max1162",
306 .data
= (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162
,
308 .compatible
= "maxim,max11100",
309 .data
= (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162
,
314 static int rcar_gyroadc_parse_subdevs(struct iio_dev
*indio_dev
)
316 const struct of_device_id
*of_id
;
317 const struct iio_chan_spec
*channels
;
318 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
319 struct device
*dev
= priv
->dev
;
320 struct device_node
*np
= dev
->of_node
;
321 struct regulator
*vref
;
323 unsigned int adcmode
= -1, childmode
;
324 unsigned int sample_width
;
325 unsigned int num_channels
;
328 for_each_available_child_of_node_scoped(np
, child
) {
329 of_id
= of_match_node(rcar_gyroadc_child_match
, child
);
331 dev_err(dev
, "Ignoring unsupported ADC \"%pOFn\".",
336 childmode
= (uintptr_t)of_id
->data
;
338 case RCAR_GYROADC_MODE_SELECT_1_MB88101A
:
340 channels
= rcar_gyroadc_iio_channels_1
;
341 num_channels
= ARRAY_SIZE(rcar_gyroadc_iio_channels_1
);
343 case RCAR_GYROADC_MODE_SELECT_2_ADCS7476
:
345 channels
= rcar_gyroadc_iio_channels_2
;
346 num_channels
= ARRAY_SIZE(rcar_gyroadc_iio_channels_2
);
348 case RCAR_GYROADC_MODE_SELECT_3_MAX1162
:
350 channels
= rcar_gyroadc_iio_channels_3
;
351 num_channels
= ARRAY_SIZE(rcar_gyroadc_iio_channels_3
);
358 * MB88101 is special in that it's only a single chip taking
359 * up all the CHS lines. Thus, the DT binding is also special
360 * and has no reg property. If we run into such ADC, handle
363 if (childmode
== RCAR_GYROADC_MODE_SELECT_1_MB88101A
) {
366 ret
= of_property_read_u32(child
, "reg", ®
);
369 "Failed to get child reg property of ADC \"%pOFn\".\n",
374 /* Channel number is too high. */
375 if (reg
>= num_channels
) {
377 "Only %i channels supported with %pOFn, but reg = <%i>.\n",
378 num_channels
, child
, reg
);
383 /* Child node selected different mode than the rest. */
384 if (!first
&& (adcmode
!= childmode
)) {
386 "Channel %i uses different ADC mode than the rest.\n",
391 /* Channel is valid, grab the regulator. */
392 dev
->of_node
= child
;
393 vref
= devm_regulator_get(dev
, "vref");
396 dev_dbg(dev
, "Channel %i 'vref' supply not connected.\n",
398 return PTR_ERR(vref
);
401 priv
->vref
[reg
] = vref
;
406 /* First child node which passed sanity tests. */
410 priv
->num_channels
= num_channels
;
411 priv
->mode
= childmode
;
412 priv
->sample_width
= sample_width
;
414 indio_dev
->channels
= channels
;
415 indio_dev
->num_channels
= num_channels
;
418 * MB88101 is special and we only have one such device
419 * attached to the GyroADC at a time, so if we found it,
420 * we can stop parsing here.
422 if (childmode
== RCAR_GYROADC_MODE_SELECT_1_MB88101A
) {
428 dev_err(dev
, "No valid ADC channels found, aborting.\n");
435 static void rcar_gyroadc_deinit_supplies(struct iio_dev
*indio_dev
)
437 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
440 for (i
= 0; i
< priv
->num_channels
; i
++) {
444 regulator_disable(priv
->vref
[i
]);
448 static int rcar_gyroadc_init_supplies(struct iio_dev
*indio_dev
)
450 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
451 struct device
*dev
= priv
->dev
;
455 for (i
= 0; i
< priv
->num_channels
; i
++) {
459 ret
= regulator_enable(priv
->vref
[i
]);
461 dev_err(dev
, "Failed to enable regulator %i (ret=%i)\n",
470 rcar_gyroadc_deinit_supplies(indio_dev
);
474 static int rcar_gyroadc_probe(struct platform_device
*pdev
)
476 struct device
*dev
= &pdev
->dev
;
477 struct rcar_gyroadc
*priv
;
478 struct iio_dev
*indio_dev
;
481 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
485 priv
= iio_priv(indio_dev
);
488 priv
->regs
= devm_platform_ioremap_resource(pdev
, 0);
489 if (IS_ERR(priv
->regs
))
490 return PTR_ERR(priv
->regs
);
492 priv
->clk
= devm_clk_get(dev
, "fck");
493 if (IS_ERR(priv
->clk
))
494 return dev_err_probe(dev
, PTR_ERR(priv
->clk
),
495 "Failed to get IF clock\n");
497 ret
= rcar_gyroadc_parse_subdevs(indio_dev
);
501 ret
= rcar_gyroadc_init_supplies(indio_dev
);
505 priv
->model
= (uintptr_t)of_device_get_match_data(&pdev
->dev
);
507 platform_set_drvdata(pdev
, indio_dev
);
509 indio_dev
->name
= DRIVER_NAME
;
510 indio_dev
->info
= &rcar_gyroadc_iio_info
;
511 indio_dev
->modes
= INDIO_DIRECT_MODE
;
513 ret
= clk_prepare_enable(priv
->clk
);
515 dev_err(dev
, "Could not prepare or enable the IF clock.\n");
516 goto err_clk_if_enable
;
519 pm_runtime_set_autosuspend_delay(dev
, RCAR_GYROADC_RUNTIME_PM_DELAY_MS
);
520 pm_runtime_use_autosuspend(dev
);
521 pm_runtime_enable(dev
);
523 ret
= pm_runtime_resume_and_get(dev
);
527 rcar_gyroadc_hw_init(priv
);
528 rcar_gyroadc_hw_start(priv
);
530 ret
= iio_device_register(indio_dev
);
532 dev_err(dev
, "Couldn't register IIO device.\n");
533 goto err_iio_device_register
;
536 pm_runtime_put_sync(dev
);
540 err_iio_device_register
:
541 rcar_gyroadc_hw_stop(priv
);
542 pm_runtime_put_sync(dev
);
544 pm_runtime_disable(dev
);
545 pm_runtime_set_suspended(dev
);
546 clk_disable_unprepare(priv
->clk
);
548 rcar_gyroadc_deinit_supplies(indio_dev
);
553 static void rcar_gyroadc_remove(struct platform_device
*pdev
)
555 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
556 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
557 struct device
*dev
= priv
->dev
;
559 iio_device_unregister(indio_dev
);
560 pm_runtime_get_sync(dev
);
561 rcar_gyroadc_hw_stop(priv
);
562 pm_runtime_put_sync(dev
);
563 pm_runtime_disable(dev
);
564 pm_runtime_set_suspended(dev
);
565 clk_disable_unprepare(priv
->clk
);
566 rcar_gyroadc_deinit_supplies(indio_dev
);
569 static int rcar_gyroadc_suspend(struct device
*dev
)
571 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
572 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
574 rcar_gyroadc_hw_stop(priv
);
579 static int rcar_gyroadc_resume(struct device
*dev
)
581 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
582 struct rcar_gyroadc
*priv
= iio_priv(indio_dev
);
584 rcar_gyroadc_hw_start(priv
);
589 static const struct dev_pm_ops rcar_gyroadc_pm_ops
= {
590 RUNTIME_PM_OPS(rcar_gyroadc_suspend
, rcar_gyroadc_resume
, NULL
)
593 static struct platform_driver rcar_gyroadc_driver
= {
594 .probe
= rcar_gyroadc_probe
,
595 .remove
= rcar_gyroadc_remove
,
598 .of_match_table
= rcar_gyroadc_match
,
599 .pm
= pm_ptr(&rcar_gyroadc_pm_ops
),
603 module_platform_driver(rcar_gyroadc_driver
);
605 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
606 MODULE_DESCRIPTION("Renesas R-Car GyroADC driver");
607 MODULE_LICENSE("GPL");