Lines Matching +full:imx93 +full:- +full:adc
1 // SPDX-License-Identifier: GPL-2.0+
3 * NXP i.MX93 ADC driver
22 #define IMX93_ADC_DRIVER_NAME "imx93-adc"
42 /* ADC bit shift */
61 /* ADC status */
102 static void imx93_adc_power_down(struct imx93_adc *adc) in imx93_adc_power_down() argument
107 mcr = readl(adc->regs + IMX93_ADC_MCR); in imx93_adc_power_down()
109 writel(mcr, adc->regs + IMX93_ADC_MCR); in imx93_adc_power_down()
111 ret = readl_poll_timeout(adc->regs + IMX93_ADC_MSR, msr, in imx93_adc_power_down()
115 if (ret == -ETIMEDOUT) in imx93_adc_power_down()
116 dev_warn(adc->dev, in imx93_adc_power_down()
117 "ADC do not in power down mode, current MSR is %x\n", in imx93_adc_power_down()
121 static void imx93_adc_power_up(struct imx93_adc *adc) in imx93_adc_power_up() argument
125 /* bring ADC out of power down state, in idle state */ in imx93_adc_power_up()
126 mcr = readl(adc->regs + IMX93_ADC_MCR); in imx93_adc_power_up()
128 writel(mcr, adc->regs + IMX93_ADC_MCR); in imx93_adc_power_up()
131 static void imx93_adc_config_ad_clk(struct imx93_adc *adc) in imx93_adc_config_ad_clk() argument
135 /* put adc in power down mode */ in imx93_adc_config_ad_clk()
136 imx93_adc_power_down(adc); in imx93_adc_config_ad_clk()
139 mcr = readl(adc->regs + IMX93_ADC_MCR); in imx93_adc_config_ad_clk()
141 writel(mcr, adc->regs + IMX93_ADC_MCR); in imx93_adc_config_ad_clk()
143 imx93_adc_power_up(adc); in imx93_adc_config_ad_clk()
146 static int imx93_adc_calibration(struct imx93_adc *adc) in imx93_adc_calibration() argument
151 /* make sure ADC in power down mode */ in imx93_adc_calibration()
152 imx93_adc_power_down(adc); in imx93_adc_calibration()
155 mcr = readl(adc->regs + IMX93_ADC_MCR); in imx93_adc_calibration()
157 writel(mcr, adc->regs + IMX93_ADC_MCR); in imx93_adc_calibration()
159 imx93_adc_power_up(adc); in imx93_adc_calibration()
167 mcr = readl(adc->regs + IMX93_ADC_MCR); in imx93_adc_calibration()
169 writel(mcr, adc->regs + IMX93_ADC_MCR); in imx93_adc_calibration()
172 ret = readl_poll_timeout(adc->regs + IMX93_ADC_MSR, msr, in imx93_adc_calibration()
174 if (ret == -ETIMEDOUT) { in imx93_adc_calibration()
175 dev_warn(adc->dev, "ADC do not finish calibration in 2 min!\n"); in imx93_adc_calibration()
176 imx93_adc_power_down(adc); in imx93_adc_calibration()
181 msr = readl(adc->regs + IMX93_ADC_MSR); in imx93_adc_calibration()
183 dev_warn(adc->dev, "ADC calibration failed!\n"); in imx93_adc_calibration()
184 imx93_adc_power_down(adc); in imx93_adc_calibration()
185 return -EAGAIN; in imx93_adc_calibration()
191 static int imx93_adc_read_channel_conversion(struct imx93_adc *adc, in imx93_adc_read_channel_conversion() argument
199 reinit_completion(&adc->completion); in imx93_adc_read_channel_conversion()
203 writel(channel, adc->regs + IMX93_ADC_NCMR0); in imx93_adc_read_channel_conversion()
209 writel(imr, adc->regs + IMX93_ADC_IMR); in imx93_adc_read_channel_conversion()
210 writel(channel, adc->regs + IMX93_ADC_CIMR0); in imx93_adc_read_channel_conversion()
212 /* config one-shot mode */ in imx93_adc_read_channel_conversion()
213 mcr = readl(adc->regs + IMX93_ADC_MCR); in imx93_adc_read_channel_conversion()
215 writel(mcr, adc->regs + IMX93_ADC_MCR); in imx93_adc_read_channel_conversion()
218 mcr = readl(adc->regs + IMX93_ADC_MCR); in imx93_adc_read_channel_conversion()
220 writel(mcr, adc->regs + IMX93_ADC_MCR); in imx93_adc_read_channel_conversion()
222 ret = wait_for_completion_interruptible_timeout(&adc->completion, in imx93_adc_read_channel_conversion()
225 return -ETIMEDOUT; in imx93_adc_read_channel_conversion()
230 pcda = readl(adc->regs + IMX93_ADC_PCDR0 + channel_number * 4); in imx93_adc_read_channel_conversion()
241 struct imx93_adc *adc = iio_priv(indio_dev); in imx93_adc_read_raw() local
242 struct device *dev = adc->dev; in imx93_adc_read_raw()
248 mutex_lock(&adc->lock); in imx93_adc_read_raw()
249 ret = imx93_adc_read_channel_conversion(adc, chan->channel, val); in imx93_adc_read_raw()
250 mutex_unlock(&adc->lock); in imx93_adc_read_raw()
259 ret = regulator_get_voltage(adc->vref); in imx93_adc_read_raw()
267 *val = clk_get_rate(adc->ipg_clk); in imx93_adc_read_raw()
271 return -EINVAL; in imx93_adc_read_raw()
277 struct imx93_adc *adc = dev_id; in imx93_adc_isr() local
280 isr = readl(adc->regs + IMX93_ADC_ISR); in imx93_adc_isr()
284 writel(eoc, adc->regs + IMX93_ADC_ISR); in imx93_adc_isr()
285 complete(&adc->completion); in imx93_adc_isr()
290 writel(unexpected, adc->regs + IMX93_ADC_ISR); in imx93_adc_isr()
291 dev_err(adc->dev, "Unexpected interrupt 0x%08x.\n", unexpected); in imx93_adc_isr()
304 struct imx93_adc *adc; in imx93_adc_probe() local
306 struct device *dev = &pdev->dev; in imx93_adc_probe()
309 indio_dev = devm_iio_device_alloc(dev, sizeof(*adc)); in imx93_adc_probe()
311 return dev_err_probe(dev, -ENOMEM, in imx93_adc_probe()
314 adc = iio_priv(indio_dev); in imx93_adc_probe()
315 adc->dev = dev; in imx93_adc_probe()
317 mutex_init(&adc->lock); in imx93_adc_probe()
318 adc->regs = devm_platform_ioremap_resource(pdev, 0); in imx93_adc_probe()
319 if (IS_ERR(adc->regs)) in imx93_adc_probe()
320 return dev_err_probe(dev, PTR_ERR(adc->regs), in imx93_adc_probe()
323 /* The third irq is for ADC conversion usage */ in imx93_adc_probe()
324 adc->irq = platform_get_irq(pdev, 2); in imx93_adc_probe()
325 if (adc->irq < 0) in imx93_adc_probe()
326 return adc->irq; in imx93_adc_probe()
328 adc->ipg_clk = devm_clk_get(dev, "ipg"); in imx93_adc_probe()
329 if (IS_ERR(adc->ipg_clk)) in imx93_adc_probe()
330 return dev_err_probe(dev, PTR_ERR(adc->ipg_clk), in imx93_adc_probe()
333 adc->vref = devm_regulator_get(dev, "vref"); in imx93_adc_probe()
334 if (IS_ERR(adc->vref)) in imx93_adc_probe()
335 return dev_err_probe(dev, PTR_ERR(adc->vref), in imx93_adc_probe()
338 ret = regulator_enable(adc->vref); in imx93_adc_probe()
345 init_completion(&adc->completion); in imx93_adc_probe()
347 indio_dev->name = "imx93-adc"; in imx93_adc_probe()
348 indio_dev->info = &imx93_adc_iio_info; in imx93_adc_probe()
349 indio_dev->modes = INDIO_DIRECT_MODE; in imx93_adc_probe()
350 indio_dev->channels = imx93_adc_iio_channels; in imx93_adc_probe()
351 indio_dev->num_channels = ARRAY_SIZE(imx93_adc_iio_channels); in imx93_adc_probe()
353 ret = clk_prepare_enable(adc->ipg_clk); in imx93_adc_probe()
360 ret = request_irq(adc->irq, imx93_adc_isr, 0, IMX93_ADC_DRIVER_NAME, adc); in imx93_adc_probe()
363 "Failed requesting irq, irq = %d\n", adc->irq); in imx93_adc_probe()
367 ret = imx93_adc_calibration(adc); in imx93_adc_probe()
371 imx93_adc_config_ad_clk(adc); in imx93_adc_probe()
388 imx93_adc_power_down(adc); in imx93_adc_probe()
390 free_irq(adc->irq, adc); in imx93_adc_probe()
392 clk_disable_unprepare(adc->ipg_clk); in imx93_adc_probe()
394 regulator_disable(adc->vref); in imx93_adc_probe()
402 struct imx93_adc *adc = iio_priv(indio_dev); in imx93_adc_remove() local
403 struct device *dev = adc->dev; in imx93_adc_remove()
405 /* adc power down need clock on */ in imx93_adc_remove()
413 imx93_adc_power_down(adc); in imx93_adc_remove()
414 free_irq(adc->irq, adc); in imx93_adc_remove()
415 clk_disable_unprepare(adc->ipg_clk); in imx93_adc_remove()
416 regulator_disable(adc->vref); in imx93_adc_remove()
422 struct imx93_adc *adc = iio_priv(indio_dev); in imx93_adc_runtime_suspend() local
424 imx93_adc_power_down(adc); in imx93_adc_runtime_suspend()
425 clk_disable_unprepare(adc->ipg_clk); in imx93_adc_runtime_suspend()
426 regulator_disable(adc->vref); in imx93_adc_runtime_suspend()
434 struct imx93_adc *adc = iio_priv(indio_dev); in imx93_adc_runtime_resume() local
437 ret = regulator_enable(adc->vref); in imx93_adc_runtime_resume()
440 "Can't enable adc reference top voltage, err = %d\n", in imx93_adc_runtime_resume()
445 ret = clk_prepare_enable(adc->ipg_clk); in imx93_adc_runtime_resume()
451 imx93_adc_power_up(adc); in imx93_adc_runtime_resume()
456 regulator_disable(adc->vref); in imx93_adc_runtime_resume()
466 { .compatible = "nxp,imx93-adc", },
483 MODULE_DESCRIPTION("NXP i.MX93 ADC driver");