1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
65
66#define RCAR_GYROADC_RUNTIME_PM_DELAY_MS 2000
67
68enum rcar_gyroadc_model {
69 RCAR_GYROADC_MODEL_DEFAULT,
70 RCAR_GYROADC_MODEL_R8A7792,
71};
72
73struct rcar_gyroadc {
74 struct device *dev;
75 void __iomem *regs;
76 struct clk *clk;
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
84static void rcar_gyroadc_hw_init(struct rcar_gyroadc *priv)
85{
86 const unsigned long clk_mhz = clk_get_rate(priv->clk) / 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
93
94
95
96 if (clk_len & 1)
97 clk_len++;
98
99
100 writel(0, priv->regs + RCAR_GYROADC_START_STOP);
101
102
103 if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
104 writel(0, priv->regs + RCAR_GYROADC_INTENR);
105
106
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
112static void rcar_gyroadc_hw_start(struct rcar_gyroadc *priv)
113{
114
115 writel(RCAR_GYROADC_START_STOP_START,
116 priv->regs + RCAR_GYROADC_START_STOP);
117
118
119
120
121
122
123
124 mdelay(3);
125}
126
127static void rcar_gyroadc_hw_stop(struct rcar_gyroadc *priv)
128{
129
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
142static 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
149static 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
160static 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
171static 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
188static 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
200
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
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
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
254static 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
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
279static const struct iio_info rcar_gyroadc_iio_info = {
280 .read_raw = rcar_gyroadc_read_raw,
281 .debugfs_reg_access = rcar_gyroadc_reg_access,
282};
283
284static const struct of_device_id rcar_gyroadc_match[] = {
285 {
286
287 .compatible = "renesas,rcar-gyroadc",
288 .data = (void *)RCAR_GYROADC_MODEL_DEFAULT,
289 }, {
290
291 .compatible = "renesas,r8a7792-gyroadc",
292 .data = (void *)RCAR_GYROADC_MODEL_R8A7792,
293 }, {
294
295 }
296};
297
298MODULE_DEVICE_TABLE(of, rcar_gyroadc_match);
299
300static const struct of_device_id rcar_gyroadc_child_match[] = {
301
302 {
303 .compatible = "fujitsu,mb88101a",
304 .data = (void *)RCAR_GYROADC_MODE_SELECT_1_MB88101A,
305 },
306
307 {
308 .compatible = "ti,adcs7476",
309 .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
310 }, {
311 .compatible = "ti,adc121",
312 .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
313 }, {
314 .compatible = "adi,ad7476",
315 .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
316 },
317
318 {
319 .compatible = "maxim,max1162",
320 .data = (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
321 }, {
322 .compatible = "maxim,max11100",
323 .data = (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
324 },
325 { }
326};
327
328static int rcar_gyroadc_parse_subdevs(struct iio_dev *indio_dev)
329{
330 const struct of_device_id *of_id;
331 const struct iio_chan_spec *channels;
332 struct rcar_gyroadc *priv = iio_priv(indio_dev);
333 struct device *dev = priv->dev;
334 struct device_node *np = dev->of_node;
335 struct device_node *child;
336 struct regulator *vref;
337 unsigned int reg;
338 unsigned int adcmode = -1, childmode;
339 unsigned int sample_width;
340 unsigned int num_channels;
341 int ret, first = 1;
342
343 for_each_child_of_node(np, child) {
344 of_id = of_match_node(rcar_gyroadc_child_match, child);
345 if (!of_id) {
346 dev_err(dev, "Ignoring unsupported ADC \"%s\".",
347 child->name);
348 continue;
349 }
350
351 childmode = (uintptr_t)of_id->data;
352 switch (childmode) {
353 case RCAR_GYROADC_MODE_SELECT_1_MB88101A:
354 sample_width = 12;
355 channels = rcar_gyroadc_iio_channels_1;
356 num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_1);
357 break;
358 case RCAR_GYROADC_MODE_SELECT_2_ADCS7476:
359 sample_width = 15;
360 channels = rcar_gyroadc_iio_channels_2;
361 num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_2);
362 break;
363 case RCAR_GYROADC_MODE_SELECT_3_MAX1162:
364 sample_width = 16;
365 channels = rcar_gyroadc_iio_channels_3;
366 num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_3);
367 break;
368 default:
369 return -EINVAL;
370 }
371
372
373
374
375
376
377
378 if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
379 reg = 0;
380 } else {
381 ret = of_property_read_u32(child, "reg", ®);
382 if (ret) {
383 dev_err(dev,
384 "Failed to get child reg property of ADC \"%s\".\n",
385 child->name);
386 return ret;
387 }
388
389
390 if (reg >= num_channels) {
391 dev_err(dev,
392 "Only %i channels supported with %s, but reg = <%i>.\n",
393 num_channels, child->name, reg);
394 return ret;
395 }
396 }
397
398
399 if (!first && (adcmode != childmode)) {
400 dev_err(dev,
401 "Channel %i uses different ADC mode than the rest.\n",
402 reg);
403 return ret;
404 }
405
406
407 dev->of_node = child;
408 vref = devm_regulator_get(dev, "vref");
409 dev->of_node = np;
410 if (IS_ERR(vref)) {
411 dev_dbg(dev, "Channel %i 'vref' supply not connected.\n",
412 reg);
413 return PTR_ERR(vref);
414 }
415
416 priv->vref[reg] = vref;
417
418 if (!first)
419 continue;
420
421
422 adcmode = childmode;
423 first = 0;
424
425 priv->num_channels = num_channels;
426 priv->mode = childmode;
427 priv->sample_width = sample_width;
428
429 indio_dev->channels = channels;
430 indio_dev->num_channels = num_channels;
431
432
433
434
435
436
437 if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A)
438 break;
439 }
440
441 if (first) {
442 dev_err(dev, "No valid ADC channels found, aborting.\n");
443 return -EINVAL;
444 }
445
446 return 0;
447}
448
449static 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
462static 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
483err:
484 rcar_gyroadc_deinit_supplies(indio_dev);
485 return ret;
486}
487
488static 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 struct resource *mem;
494 int ret;
495
496 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
497 if (!indio_dev) {
498 dev_err(dev, "Failed to allocate IIO device.\n");
499 return -ENOMEM;
500 }
501
502 priv = iio_priv(indio_dev);
503 priv->dev = dev;
504
505 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
506 priv->regs = devm_ioremap_resource(dev, mem);
507 if (IS_ERR(priv->regs))
508 return PTR_ERR(priv->regs);
509
510 priv->clk = devm_clk_get(dev, "fck");
511 if (IS_ERR(priv->clk)) {
512 ret = PTR_ERR(priv->clk);
513 if (ret != -EPROBE_DEFER)
514 dev_err(dev, "Failed to get IF clock (ret=%i)\n", ret);
515 return ret;
516 }
517
518 ret = rcar_gyroadc_parse_subdevs(indio_dev);
519 if (ret)
520 return ret;
521
522 ret = rcar_gyroadc_init_supplies(indio_dev);
523 if (ret)
524 return ret;
525
526 priv->model = (enum rcar_gyroadc_model)
527 of_device_get_match_data(&pdev->dev);
528
529 platform_set_drvdata(pdev, indio_dev);
530
531 indio_dev->name = DRIVER_NAME;
532 indio_dev->dev.parent = dev;
533 indio_dev->dev.of_node = pdev->dev.of_node;
534 indio_dev->info = &rcar_gyroadc_iio_info;
535 indio_dev->modes = INDIO_DIRECT_MODE;
536
537 ret = clk_prepare_enable(priv->clk);
538 if (ret) {
539 dev_err(dev, "Could not prepare or enable the IF clock.\n");
540 goto err_clk_if_enable;
541 }
542
543 pm_runtime_set_autosuspend_delay(dev, RCAR_GYROADC_RUNTIME_PM_DELAY_MS);
544 pm_runtime_use_autosuspend(dev);
545 pm_runtime_enable(dev);
546
547 pm_runtime_get_sync(dev);
548 rcar_gyroadc_hw_init(priv);
549 rcar_gyroadc_hw_start(priv);
550
551 ret = iio_device_register(indio_dev);
552 if (ret) {
553 dev_err(dev, "Couldn't register IIO device.\n");
554 goto err_iio_device_register;
555 }
556
557 pm_runtime_put_sync(dev);
558
559 return 0;
560
561err_iio_device_register:
562 rcar_gyroadc_hw_stop(priv);
563 pm_runtime_put_sync(dev);
564 pm_runtime_disable(dev);
565 pm_runtime_set_suspended(dev);
566 clk_disable_unprepare(priv->clk);
567err_clk_if_enable:
568 rcar_gyroadc_deinit_supplies(indio_dev);
569
570 return ret;
571}
572
573static int rcar_gyroadc_remove(struct platform_device *pdev)
574{
575 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
576 struct rcar_gyroadc *priv = iio_priv(indio_dev);
577 struct device *dev = priv->dev;
578
579 iio_device_unregister(indio_dev);
580 pm_runtime_get_sync(dev);
581 rcar_gyroadc_hw_stop(priv);
582 pm_runtime_put_sync(dev);
583 pm_runtime_disable(dev);
584 pm_runtime_set_suspended(dev);
585 clk_disable_unprepare(priv->clk);
586 rcar_gyroadc_deinit_supplies(indio_dev);
587
588 return 0;
589}
590
591#if defined(CONFIG_PM)
592static int rcar_gyroadc_suspend(struct device *dev)
593{
594 struct iio_dev *indio_dev = dev_get_drvdata(dev);
595 struct rcar_gyroadc *priv = iio_priv(indio_dev);
596
597 rcar_gyroadc_hw_stop(priv);
598
599 return 0;
600}
601
602static int rcar_gyroadc_resume(struct device *dev)
603{
604 struct iio_dev *indio_dev = dev_get_drvdata(dev);
605 struct rcar_gyroadc *priv = iio_priv(indio_dev);
606
607 rcar_gyroadc_hw_start(priv);
608
609 return 0;
610}
611#endif
612
613static const struct dev_pm_ops rcar_gyroadc_pm_ops = {
614 SET_RUNTIME_PM_OPS(rcar_gyroadc_suspend, rcar_gyroadc_resume, NULL)
615};
616
617static struct platform_driver rcar_gyroadc_driver = {
618 .probe = rcar_gyroadc_probe,
619 .remove = rcar_gyroadc_remove,
620 .driver = {
621 .name = DRIVER_NAME,
622 .of_match_table = rcar_gyroadc_match,
623 .pm = &rcar_gyroadc_pm_ops,
624 },
625};
626
627module_platform_driver(rcar_gyroadc_driver);
628
629MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
630MODULE_DESCRIPTION("Renesas R-Car GyroADC driver");
631MODULE_LICENSE("GPL");
632