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