1 // SPDX-License-Identifier: GPL-2.0
3 * RZ/G2L A/D Converter driver
5 * Copyright (c) 2021 Renesas Electronics Europe GmbH
7 * Author: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
10 #include <linux/bitfield.h>
11 #include <linux/clk.h>
12 #include <linux/completion.h>
13 #include <linux/delay.h>
14 #include <linux/iio/iio.h>
15 #include <linux/interrupt.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/property.h>
22 #include <linux/reset.h>
24 #define DRIVER_NAME "rzg2l-adc"
26 #define RZG2L_ADM(n) ((n) * 0x4)
27 #define RZG2L_ADM0_ADCE BIT(0)
28 #define RZG2L_ADM0_ADBSY BIT(1)
29 #define RZG2L_ADM0_PWDWNB BIT(2)
30 #define RZG2L_ADM0_SRESB BIT(15)
31 #define RZG2L_ADM1_TRG BIT(0)
32 #define RZG2L_ADM1_MS BIT(2)
33 #define RZG2L_ADM1_BS BIT(4)
34 #define RZG2L_ADM1_EGA_MASK GENMASK(13, 12)
35 #define RZG2L_ADM2_CHSEL_MASK GENMASK(7, 0)
36 #define RZG2L_ADM3_ADIL_MASK GENMASK(31, 24)
37 #define RZG2L_ADM3_ADCMP_MASK GENMASK(23, 16)
38 #define RZG2L_ADM3_ADCMP_E FIELD_PREP(RZG2L_ADM3_ADCMP_MASK, 0xe)
39 #define RZG2L_ADM3_ADSMP_MASK GENMASK(15, 0)
41 #define RZG2L_ADINT 0x20
42 #define RZG2L_ADINT_INTEN_MASK GENMASK(7, 0)
43 #define RZG2L_ADINT_CSEEN BIT(16)
44 #define RZG2L_ADINT_INTS BIT(31)
46 #define RZG2L_ADSTS 0x24
47 #define RZG2L_ADSTS_CSEST BIT(16)
48 #define RZG2L_ADSTS_INTST_MASK GENMASK(7, 0)
50 #define RZG2L_ADIVC 0x28
51 #define RZG2L_ADIVC_DIVADC_MASK GENMASK(8, 0)
52 #define RZG2L_ADIVC_DIVADC_4 FIELD_PREP(RZG2L_ADIVC_DIVADC_MASK, 0x4)
54 #define RZG2L_ADFIL 0x2c
56 #define RZG2L_ADCR(n) (0x30 + ((n) * 0x4))
57 #define RZG2L_ADCR_AD_MASK GENMASK(11, 0)
59 #define RZG2L_ADSMP_DEFAULT_SAMPLING 0x578
61 #define RZG2L_ADC_MAX_CHANNELS 8
62 #define RZG2L_ADC_CHN_MASK 0x7
63 #define RZG2L_ADC_TIMEOUT usecs_to_jiffies(1 * 4)
65 struct rzg2l_adc_data {
66 const struct iio_chan_spec *channels;
74 struct reset_control *presetn;
75 struct reset_control *adrstn;
76 struct completion completion;
77 const struct rzg2l_adc_data *data;
79 u16 last_val[RZG2L_ADC_MAX_CHANNELS];
82 static const char * const rzg2l_adc_channel_name[] = {
93 static unsigned int rzg2l_adc_readl(struct rzg2l_adc *adc, u32 reg)
95 return readl(adc->base + reg);
98 static void rzg2l_adc_writel(struct rzg2l_adc *adc, unsigned int reg, u32 val)
100 writel(val, adc->base + reg);
103 static void rzg2l_adc_pwr(struct rzg2l_adc *adc, bool on)
107 reg = rzg2l_adc_readl(adc, RZG2L_ADM(0));
109 reg |= RZG2L_ADM0_PWDWNB;
111 reg &= ~RZG2L_ADM0_PWDWNB;
112 rzg2l_adc_writel(adc, RZG2L_ADM(0), reg);
116 static void rzg2l_adc_start_stop(struct rzg2l_adc *adc, bool start)
121 reg = rzg2l_adc_readl(adc, RZG2L_ADM(0));
123 reg |= RZG2L_ADM0_ADCE;
125 reg &= ~RZG2L_ADM0_ADCE;
126 rzg2l_adc_writel(adc, RZG2L_ADM(0), reg);
132 usleep_range(100, 200);
133 reg = rzg2l_adc_readl(adc, RZG2L_ADM(0));
136 pr_err("%s stopping ADC timed out\n", __func__);
139 } while (((reg & RZG2L_ADM0_ADBSY) || (reg & RZG2L_ADM0_ADCE)));
142 static void rzg2l_set_trigger(struct rzg2l_adc *adc)
147 * Setup ADM1 for SW trigger
148 * EGA[13:12] - Set 00 to indicate hardware trigger is invalid
149 * BS[4] - Enable 1-buffer mode
150 * MS[1] - Enable Select mode
151 * TRG[0] - Enable software trigger mode
153 reg = rzg2l_adc_readl(adc, RZG2L_ADM(1));
154 reg &= ~RZG2L_ADM1_EGA_MASK;
155 reg &= ~RZG2L_ADM1_BS;
156 reg &= ~RZG2L_ADM1_TRG;
157 reg |= RZG2L_ADM1_MS;
158 rzg2l_adc_writel(adc, RZG2L_ADM(1), reg);
161 static int rzg2l_adc_conversion_setup(struct rzg2l_adc *adc, u8 ch)
165 if (rzg2l_adc_readl(adc, RZG2L_ADM(0)) & RZG2L_ADM0_ADBSY)
168 rzg2l_set_trigger(adc);
170 /* Select analog input channel subjected to conversion. */
171 reg = rzg2l_adc_readl(adc, RZG2L_ADM(2));
172 reg &= ~RZG2L_ADM2_CHSEL_MASK;
174 rzg2l_adc_writel(adc, RZG2L_ADM(2), reg);
178 * INTS[31] - Select pulse signal
179 * CSEEN[16] - Enable channel select error interrupt
180 * INTEN[7:0] - Select channel interrupt
182 reg = rzg2l_adc_readl(adc, RZG2L_ADINT);
183 reg &= ~RZG2L_ADINT_INTS;
184 reg &= ~RZG2L_ADINT_INTEN_MASK;
185 reg |= (RZG2L_ADINT_CSEEN | BIT(ch));
186 rzg2l_adc_writel(adc, RZG2L_ADINT, reg);
191 static int rzg2l_adc_set_power(struct iio_dev *indio_dev, bool on)
193 struct device *dev = indio_dev->dev.parent;
196 return pm_runtime_resume_and_get(dev);
198 return pm_runtime_put_sync(dev);
201 static int rzg2l_adc_conversion(struct iio_dev *indio_dev, struct rzg2l_adc *adc, u8 ch)
205 ret = rzg2l_adc_set_power(indio_dev, true);
209 ret = rzg2l_adc_conversion_setup(adc, ch);
211 rzg2l_adc_set_power(indio_dev, false);
215 reinit_completion(&adc->completion);
217 rzg2l_adc_start_stop(adc, true);
219 if (!wait_for_completion_timeout(&adc->completion, RZG2L_ADC_TIMEOUT)) {
220 rzg2l_adc_writel(adc, RZG2L_ADINT,
221 rzg2l_adc_readl(adc, RZG2L_ADINT) & ~RZG2L_ADINT_INTEN_MASK);
222 rzg2l_adc_start_stop(adc, false);
223 rzg2l_adc_set_power(indio_dev, false);
227 return rzg2l_adc_set_power(indio_dev, false);
230 static int rzg2l_adc_read_raw(struct iio_dev *indio_dev,
231 struct iio_chan_spec const *chan,
232 int *val, int *val2, long mask)
234 struct rzg2l_adc *adc = iio_priv(indio_dev);
239 case IIO_CHAN_INFO_RAW:
240 if (chan->type != IIO_VOLTAGE)
243 mutex_lock(&adc->lock);
244 ch = chan->channel & RZG2L_ADC_CHN_MASK;
245 ret = rzg2l_adc_conversion(indio_dev, adc, ch);
247 mutex_unlock(&adc->lock);
250 *val = adc->last_val[ch];
251 mutex_unlock(&adc->lock);
260 static int rzg2l_adc_read_label(struct iio_dev *iio_dev,
261 const struct iio_chan_spec *chan,
264 return sysfs_emit(label, "%s\n", rzg2l_adc_channel_name[chan->channel]);
267 static const struct iio_info rzg2l_adc_iio_info = {
268 .read_raw = rzg2l_adc_read_raw,
269 .read_label = rzg2l_adc_read_label,
272 static irqreturn_t rzg2l_adc_isr(int irq, void *dev_id)
274 struct rzg2l_adc *adc = dev_id;
279 reg = rzg2l_adc_readl(adc, RZG2L_ADSTS);
281 /* A/D conversion channel select error interrupt */
282 if (reg & RZG2L_ADSTS_CSEST) {
283 rzg2l_adc_writel(adc, RZG2L_ADSTS, reg);
287 intst = reg & RZG2L_ADSTS_INTST_MASK;
291 for_each_set_bit(ch, &intst, RZG2L_ADC_MAX_CHANNELS)
292 adc->last_val[ch] = rzg2l_adc_readl(adc, RZG2L_ADCR(ch)) & RZG2L_ADCR_AD_MASK;
294 /* clear the channel interrupt */
295 rzg2l_adc_writel(adc, RZG2L_ADSTS, reg);
297 complete(&adc->completion);
302 static int rzg2l_adc_parse_properties(struct platform_device *pdev, struct rzg2l_adc *adc)
304 struct iio_chan_spec *chan_array;
305 struct fwnode_handle *fwnode;
306 struct rzg2l_adc_data *data;
307 unsigned int channel;
312 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
316 num_channels = device_get_child_node_count(&pdev->dev);
318 dev_err(&pdev->dev, "no channel children\n");
322 if (num_channels > RZG2L_ADC_MAX_CHANNELS) {
323 dev_err(&pdev->dev, "num of channel children out of range\n");
327 chan_array = devm_kcalloc(&pdev->dev, num_channels, sizeof(*chan_array),
333 device_for_each_child_node(&pdev->dev, fwnode) {
334 ret = fwnode_property_read_u32(fwnode, "reg", &channel);
336 fwnode_handle_put(fwnode);
340 if (channel >= RZG2L_ADC_MAX_CHANNELS) {
341 fwnode_handle_put(fwnode);
345 chan_array[i].type = IIO_VOLTAGE;
346 chan_array[i].indexed = 1;
347 chan_array[i].channel = channel;
348 chan_array[i].info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
349 chan_array[i].datasheet_name = rzg2l_adc_channel_name[channel];
353 data->num_channels = num_channels;
354 data->channels = chan_array;
360 static int rzg2l_adc_hw_init(struct rzg2l_adc *adc)
366 ret = clk_prepare_enable(adc->pclk);
371 reg = rzg2l_adc_readl(adc, RZG2L_ADM(0));
372 reg |= RZG2L_ADM0_SRESB;
373 rzg2l_adc_writel(adc, RZG2L_ADM(0), reg);
375 while (!(rzg2l_adc_readl(adc, RZG2L_ADM(0)) & RZG2L_ADM0_SRESB)) {
381 usleep_range(100, 200);
384 /* Only division by 4 can be set */
385 reg = rzg2l_adc_readl(adc, RZG2L_ADIVC);
386 reg &= ~RZG2L_ADIVC_DIVADC_MASK;
387 reg |= RZG2L_ADIVC_DIVADC_4;
388 rzg2l_adc_writel(adc, RZG2L_ADIVC, reg);
392 * ADIL[31:24] - Should be always set to 0
393 * ADCMP[23:16] - Should be always set to 0xe
394 * ADSMP[15:0] - Set default (0x578) sampling period
396 reg = rzg2l_adc_readl(adc, RZG2L_ADM(3));
397 reg &= ~RZG2L_ADM3_ADIL_MASK;
398 reg &= ~RZG2L_ADM3_ADCMP_MASK;
399 reg &= ~RZG2L_ADM3_ADSMP_MASK;
400 reg |= (RZG2L_ADM3_ADCMP_E | RZG2L_ADSMP_DEFAULT_SAMPLING);
401 rzg2l_adc_writel(adc, RZG2L_ADM(3), reg);
404 clk_disable_unprepare(adc->pclk);
409 static void rzg2l_adc_pm_runtime_disable(void *data)
411 struct device *dev = data;
413 pm_runtime_disable(dev->parent);
416 static void rzg2l_adc_pm_runtime_set_suspended(void *data)
418 struct device *dev = data;
420 pm_runtime_set_suspended(dev->parent);
423 static void rzg2l_adc_reset_assert(void *data)
425 reset_control_assert(data);
428 static int rzg2l_adc_probe(struct platform_device *pdev)
430 struct device *dev = &pdev->dev;
431 struct iio_dev *indio_dev;
432 struct rzg2l_adc *adc;
436 indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
440 adc = iio_priv(indio_dev);
442 ret = rzg2l_adc_parse_properties(pdev, adc);
446 mutex_init(&adc->lock);
448 adc->base = devm_platform_ioremap_resource(pdev, 0);
449 if (IS_ERR(adc->base))
450 return PTR_ERR(adc->base);
452 adc->pclk = devm_clk_get(dev, "pclk");
453 if (IS_ERR(adc->pclk)) {
454 dev_err(dev, "Failed to get pclk");
455 return PTR_ERR(adc->pclk);
458 adc->adclk = devm_clk_get(dev, "adclk");
459 if (IS_ERR(adc->adclk)) {
460 dev_err(dev, "Failed to get adclk");
461 return PTR_ERR(adc->adclk);
464 adc->adrstn = devm_reset_control_get_exclusive(dev, "adrst-n");
465 if (IS_ERR(adc->adrstn)) {
466 dev_err(dev, "failed to get adrstn\n");
467 return PTR_ERR(adc->adrstn);
470 adc->presetn = devm_reset_control_get_exclusive(dev, "presetn");
471 if (IS_ERR(adc->presetn)) {
472 dev_err(dev, "failed to get presetn\n");
473 return PTR_ERR(adc->presetn);
476 ret = reset_control_deassert(adc->adrstn);
478 dev_err(&pdev->dev, "failed to deassert adrstn pin, %d\n", ret);
482 ret = devm_add_action_or_reset(&pdev->dev,
483 rzg2l_adc_reset_assert, adc->adrstn);
485 dev_err(&pdev->dev, "failed to register adrstn assert devm action, %d\n",
490 ret = reset_control_deassert(adc->presetn);
492 dev_err(&pdev->dev, "failed to deassert presetn pin, %d\n", ret);
496 ret = devm_add_action_or_reset(&pdev->dev,
497 rzg2l_adc_reset_assert, adc->presetn);
499 dev_err(&pdev->dev, "failed to register presetn assert devm action, %d\n",
504 ret = rzg2l_adc_hw_init(adc);
506 dev_err(&pdev->dev, "failed to initialize ADC HW, %d\n", ret);
510 irq = platform_get_irq(pdev, 0);
514 ret = devm_request_irq(dev, irq, rzg2l_adc_isr,
515 0, dev_name(dev), adc);
519 init_completion(&adc->completion);
521 platform_set_drvdata(pdev, indio_dev);
523 indio_dev->name = DRIVER_NAME;
524 indio_dev->info = &rzg2l_adc_iio_info;
525 indio_dev->modes = INDIO_DIRECT_MODE;
526 indio_dev->channels = adc->data->channels;
527 indio_dev->num_channels = adc->data->num_channels;
529 pm_runtime_set_suspended(dev);
530 ret = devm_add_action_or_reset(&pdev->dev,
531 rzg2l_adc_pm_runtime_set_suspended, &indio_dev->dev);
535 pm_runtime_enable(dev);
536 ret = devm_add_action_or_reset(&pdev->dev,
537 rzg2l_adc_pm_runtime_disable, &indio_dev->dev);
541 return devm_iio_device_register(dev, indio_dev);
544 static const struct of_device_id rzg2l_adc_match[] = {
545 { .compatible = "renesas,rzg2l-adc",},
548 MODULE_DEVICE_TABLE(of, rzg2l_adc_match);
550 static int __maybe_unused rzg2l_adc_pm_runtime_suspend(struct device *dev)
552 struct iio_dev *indio_dev = dev_get_drvdata(dev);
553 struct rzg2l_adc *adc = iio_priv(indio_dev);
555 rzg2l_adc_pwr(adc, false);
556 clk_disable_unprepare(adc->adclk);
557 clk_disable_unprepare(adc->pclk);
562 static int __maybe_unused rzg2l_adc_pm_runtime_resume(struct device *dev)
564 struct iio_dev *indio_dev = dev_get_drvdata(dev);
565 struct rzg2l_adc *adc = iio_priv(indio_dev);
568 ret = clk_prepare_enable(adc->pclk);
572 ret = clk_prepare_enable(adc->adclk);
574 clk_disable_unprepare(adc->pclk);
578 rzg2l_adc_pwr(adc, true);
583 static const struct dev_pm_ops rzg2l_adc_pm_ops = {
584 SET_RUNTIME_PM_OPS(rzg2l_adc_pm_runtime_suspend,
585 rzg2l_adc_pm_runtime_resume,
589 static struct platform_driver rzg2l_adc_driver = {
590 .probe = rzg2l_adc_probe,
593 .of_match_table = rzg2l_adc_match,
594 .pm = &rzg2l_adc_pm_ops,
598 module_platform_driver(rzg2l_adc_driver);
600 MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>");
601 MODULE_DESCRIPTION("Renesas RZ/G2L ADC driver");
602 MODULE_LICENSE("GPL v2");