xref: /openbmc/linux/drivers/iio/adc/npcm_adc.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
19bf85fbcSTomer Maimon // SPDX-License-Identifier: GPL-2.0
29bf85fbcSTomer Maimon // Copyright (c) 2019 Nuvoton Technology corporation.
39bf85fbcSTomer Maimon 
49bf85fbcSTomer Maimon #include <linux/clk.h>
59bf85fbcSTomer Maimon #include <linux/device.h>
69bf85fbcSTomer Maimon #include <linux/mfd/syscon.h>
79bf85fbcSTomer Maimon #include <linux/io.h>
89bf85fbcSTomer Maimon #include <linux/iio/iio.h>
99bf85fbcSTomer Maimon #include <linux/interrupt.h>
109bf85fbcSTomer Maimon #include <linux/kernel.h>
11074d68c1SNuno Sá #include <linux/mod_devicetable.h>
129bf85fbcSTomer Maimon #include <linux/module.h>
139bf85fbcSTomer Maimon #include <linux/platform_device.h>
143ccb2524STomer Maimon #include <linux/property.h>
159bf85fbcSTomer Maimon #include <linux/regmap.h>
169bf85fbcSTomer Maimon #include <linux/regulator/consumer.h>
179bf85fbcSTomer Maimon #include <linux/spinlock.h>
189bf85fbcSTomer Maimon #include <linux/uaccess.h>
19db6bcb8cSTomer Maimon #include <linux/reset.h>
209bf85fbcSTomer Maimon 
213ccb2524STomer Maimon struct npcm_adc_info {
223ccb2524STomer Maimon 	u32 data_mask;
233ccb2524STomer Maimon 	u32 internal_vref;
243ccb2524STomer Maimon 	u32 res_bits;
253ccb2524STomer Maimon };
263ccb2524STomer Maimon 
279bf85fbcSTomer Maimon struct npcm_adc {
289bf85fbcSTomer Maimon 	bool int_status;
299bf85fbcSTomer Maimon 	u32 adc_sample_hz;
309bf85fbcSTomer Maimon 	struct device *dev;
319bf85fbcSTomer Maimon 	void __iomem *regs;
329bf85fbcSTomer Maimon 	struct clk *adc_clk;
339bf85fbcSTomer Maimon 	wait_queue_head_t wq;
349bf85fbcSTomer Maimon 	struct regulator *vref;
35db6bcb8cSTomer Maimon 	struct reset_control *reset;
3608dfc6f8SSergiu Cuciurean 	/*
3708dfc6f8SSergiu Cuciurean 	 * Lock to protect the device state during a potential concurrent
3808dfc6f8SSergiu Cuciurean 	 * read access from userspace. Reading a raw value requires a sequence
3908dfc6f8SSergiu Cuciurean 	 * of register writes, then a wait for a event and finally a register
4008dfc6f8SSergiu Cuciurean 	 * read, during which userspace could issue another read request.
4108dfc6f8SSergiu Cuciurean 	 * This lock protects a read access from ocurring before another one
4208dfc6f8SSergiu Cuciurean 	 * has finished.
4308dfc6f8SSergiu Cuciurean 	 */
4408dfc6f8SSergiu Cuciurean 	struct mutex lock;
453ccb2524STomer Maimon 	const struct npcm_adc_info *data;
469bf85fbcSTomer Maimon };
479bf85fbcSTomer Maimon 
489bf85fbcSTomer Maimon /* ADC registers */
499bf85fbcSTomer Maimon #define NPCM_ADCCON	 0x00
509bf85fbcSTomer Maimon #define NPCM_ADCDATA	 0x04
519bf85fbcSTomer Maimon 
529bf85fbcSTomer Maimon /* ADCCON Register Bits */
539bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_INT_EN		BIT(21)
549bf85fbcSTomer Maimon #define NPCM_ADCCON_REFSEL		BIT(19)
559bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_INT_ST		BIT(18)
569bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_EN		BIT(17)
579bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_RST		BIT(16)
589bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_CONV		BIT(13)
599bf85fbcSTomer Maimon 
609bf85fbcSTomer Maimon #define NPCM_ADCCON_CH_MASK		GENMASK(27, 24)
619bf85fbcSTomer Maimon #define NPCM_ADCCON_CH(x)		((x) << 24)
629bf85fbcSTomer Maimon #define NPCM_ADCCON_DIV_SHIFT		1
639bf85fbcSTomer Maimon #define NPCM_ADCCON_DIV_MASK		GENMASK(8, 1)
649bf85fbcSTomer Maimon 
659bf85fbcSTomer Maimon #define NPCM_ADC_ENABLE		(NPCM_ADCCON_ADC_EN | NPCM_ADCCON_ADC_INT_EN)
669bf85fbcSTomer Maimon 
679bf85fbcSTomer Maimon /* ADC General Definition */
683ccb2524STomer Maimon static const struct npcm_adc_info npxm7xx_adc_info = {
693ccb2524STomer Maimon 	.data_mask = GENMASK(9, 0),
703ccb2524STomer Maimon 	.internal_vref = 2048,
713ccb2524STomer Maimon 	.res_bits = 10,
723ccb2524STomer Maimon };
733ccb2524STomer Maimon 
743ccb2524STomer Maimon static const struct npcm_adc_info npxm8xx_adc_info = {
753ccb2524STomer Maimon 	.data_mask = GENMASK(11, 0),
763ccb2524STomer Maimon 	.internal_vref = 1229,
773ccb2524STomer Maimon 	.res_bits = 12,
783ccb2524STomer Maimon };
799bf85fbcSTomer Maimon 
809bf85fbcSTomer Maimon #define NPCM_ADC_CHAN(ch) {					\
819bf85fbcSTomer Maimon 	.type = IIO_VOLTAGE,					\
829bf85fbcSTomer Maimon 	.indexed = 1,						\
839bf85fbcSTomer Maimon 	.channel = ch,						\
849bf85fbcSTomer Maimon 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
859bf85fbcSTomer Maimon 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |	\
869bf85fbcSTomer Maimon 				BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
879bf85fbcSTomer Maimon }
889bf85fbcSTomer Maimon 
899bf85fbcSTomer Maimon static const struct iio_chan_spec npcm_adc_iio_channels[] = {
909bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(0),
919bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(1),
929bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(2),
939bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(3),
949bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(4),
959bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(5),
969bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(6),
979bf85fbcSTomer Maimon 	NPCM_ADC_CHAN(7),
989bf85fbcSTomer Maimon };
999bf85fbcSTomer Maimon 
npcm_adc_isr(int irq,void * data)1009bf85fbcSTomer Maimon static irqreturn_t npcm_adc_isr(int irq, void *data)
1019bf85fbcSTomer Maimon {
1029bf85fbcSTomer Maimon 	u32 regtemp;
1039bf85fbcSTomer Maimon 	struct iio_dev *indio_dev = data;
1049bf85fbcSTomer Maimon 	struct npcm_adc *info = iio_priv(indio_dev);
1059bf85fbcSTomer Maimon 
1069bf85fbcSTomer Maimon 	regtemp = ioread32(info->regs + NPCM_ADCCON);
1079bf85fbcSTomer Maimon 	if (regtemp & NPCM_ADCCON_ADC_INT_ST) {
1089bf85fbcSTomer Maimon 		iowrite32(regtemp, info->regs + NPCM_ADCCON);
1099bf85fbcSTomer Maimon 		wake_up_interruptible(&info->wq);
1109bf85fbcSTomer Maimon 		info->int_status = true;
1119bf85fbcSTomer Maimon 	}
1129bf85fbcSTomer Maimon 
1139bf85fbcSTomer Maimon 	return IRQ_HANDLED;
1149bf85fbcSTomer Maimon }
1159bf85fbcSTomer Maimon 
npcm_adc_read(struct npcm_adc * info,int * val,u8 channel)1169bf85fbcSTomer Maimon static int npcm_adc_read(struct npcm_adc *info, int *val, u8 channel)
1179bf85fbcSTomer Maimon {
1189bf85fbcSTomer Maimon 	int ret;
1199bf85fbcSTomer Maimon 	u32 regtemp;
1209bf85fbcSTomer Maimon 
1219bf85fbcSTomer Maimon 	/* Select ADC channel */
1229bf85fbcSTomer Maimon 	regtemp = ioread32(info->regs + NPCM_ADCCON);
1239bf85fbcSTomer Maimon 	regtemp &= ~NPCM_ADCCON_CH_MASK;
1249bf85fbcSTomer Maimon 	info->int_status = false;
1259bf85fbcSTomer Maimon 	iowrite32(regtemp | NPCM_ADCCON_CH(channel) |
1269bf85fbcSTomer Maimon 		  NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON);
1279bf85fbcSTomer Maimon 
1289bf85fbcSTomer Maimon 	ret = wait_event_interruptible_timeout(info->wq, info->int_status,
1299bf85fbcSTomer Maimon 					       msecs_to_jiffies(10));
1309bf85fbcSTomer Maimon 	if (ret == 0) {
1319bf85fbcSTomer Maimon 		regtemp = ioread32(info->regs + NPCM_ADCCON);
132db6bcb8cSTomer Maimon 		if (regtemp & NPCM_ADCCON_ADC_CONV) {
1339bf85fbcSTomer Maimon 			/* if conversion failed - reset ADC module */
134db6bcb8cSTomer Maimon 			reset_control_assert(info->reset);
1359bf85fbcSTomer Maimon 			msleep(100);
136db6bcb8cSTomer Maimon 			reset_control_deassert(info->reset);
1379bf85fbcSTomer Maimon 			msleep(100);
1389bf85fbcSTomer Maimon 
1399bf85fbcSTomer Maimon 			/* Enable ADC and start conversion module */
1409bf85fbcSTomer Maimon 			iowrite32(NPCM_ADC_ENABLE | NPCM_ADCCON_ADC_CONV,
1419bf85fbcSTomer Maimon 				  info->regs + NPCM_ADCCON);
1429bf85fbcSTomer Maimon 			dev_err(info->dev, "RESET ADC Complete\n");
1439bf85fbcSTomer Maimon 		}
1449bf85fbcSTomer Maimon 		return -ETIMEDOUT;
1459bf85fbcSTomer Maimon 	}
1469bf85fbcSTomer Maimon 	if (ret < 0)
1479bf85fbcSTomer Maimon 		return ret;
1489bf85fbcSTomer Maimon 
1493ccb2524STomer Maimon 	*val = ioread32(info->regs + NPCM_ADCDATA);
1503ccb2524STomer Maimon 	*val &= info->data->data_mask;
1519bf85fbcSTomer Maimon 
1529bf85fbcSTomer Maimon 	return 0;
1539bf85fbcSTomer Maimon }
1549bf85fbcSTomer Maimon 
npcm_adc_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)1559bf85fbcSTomer Maimon static int npcm_adc_read_raw(struct iio_dev *indio_dev,
1569bf85fbcSTomer Maimon 			     struct iio_chan_spec const *chan, int *val,
1579bf85fbcSTomer Maimon 			     int *val2, long mask)
1589bf85fbcSTomer Maimon {
1599bf85fbcSTomer Maimon 	int ret;
1609bf85fbcSTomer Maimon 	int vref_uv;
1619bf85fbcSTomer Maimon 	struct npcm_adc *info = iio_priv(indio_dev);
1629bf85fbcSTomer Maimon 
1639bf85fbcSTomer Maimon 	switch (mask) {
1649bf85fbcSTomer Maimon 	case IIO_CHAN_INFO_RAW:
16508dfc6f8SSergiu Cuciurean 		mutex_lock(&info->lock);
1669bf85fbcSTomer Maimon 		ret = npcm_adc_read(info, val, chan->channel);
16708dfc6f8SSergiu Cuciurean 		mutex_unlock(&info->lock);
1689bf85fbcSTomer Maimon 		if (ret) {
1699bf85fbcSTomer Maimon 			dev_err(info->dev, "NPCM ADC read failed\n");
1709bf85fbcSTomer Maimon 			return ret;
1719bf85fbcSTomer Maimon 		}
1729bf85fbcSTomer Maimon 		return IIO_VAL_INT;
1739bf85fbcSTomer Maimon 	case IIO_CHAN_INFO_SCALE:
1744e63ed6bSTomer Maimon 		if (!IS_ERR(info->vref)) {
1759bf85fbcSTomer Maimon 			vref_uv = regulator_get_voltage(info->vref);
1769bf85fbcSTomer Maimon 			*val = vref_uv / 1000;
1779bf85fbcSTomer Maimon 		} else {
1783ccb2524STomer Maimon 			*val = info->data->internal_vref;
1799bf85fbcSTomer Maimon 		}
1803ccb2524STomer Maimon 		*val2 = info->data->res_bits;
1819bf85fbcSTomer Maimon 		return IIO_VAL_FRACTIONAL_LOG2;
1829bf85fbcSTomer Maimon 	case IIO_CHAN_INFO_SAMP_FREQ:
1839bf85fbcSTomer Maimon 		*val = info->adc_sample_hz;
1849bf85fbcSTomer Maimon 		return IIO_VAL_INT;
1859bf85fbcSTomer Maimon 	default:
1869bf85fbcSTomer Maimon 		return -EINVAL;
1879bf85fbcSTomer Maimon 	}
1889bf85fbcSTomer Maimon 
1899bf85fbcSTomer Maimon 	return 0;
1909bf85fbcSTomer Maimon }
1919bf85fbcSTomer Maimon 
1929bf85fbcSTomer Maimon static const struct iio_info npcm_adc_iio_info = {
1939bf85fbcSTomer Maimon 	.read_raw = &npcm_adc_read_raw,
1949bf85fbcSTomer Maimon };
1959bf85fbcSTomer Maimon 
1969bf85fbcSTomer Maimon static const struct of_device_id npcm_adc_match[] = {
1973ccb2524STomer Maimon 	{ .compatible = "nuvoton,npcm750-adc", .data = &npxm7xx_adc_info},
1983ccb2524STomer Maimon 	{ .compatible = "nuvoton,npcm845-adc", .data = &npxm8xx_adc_info},
1999bf85fbcSTomer Maimon 	{ /* sentinel */ }
2009bf85fbcSTomer Maimon };
2019bf85fbcSTomer Maimon MODULE_DEVICE_TABLE(of, npcm_adc_match);
2029bf85fbcSTomer Maimon 
npcm_adc_probe(struct platform_device * pdev)2039bf85fbcSTomer Maimon static int npcm_adc_probe(struct platform_device *pdev)
2049bf85fbcSTomer Maimon {
2059bf85fbcSTomer Maimon 	int ret;
2069bf85fbcSTomer Maimon 	int irq;
2079bf85fbcSTomer Maimon 	u32 div;
2089bf85fbcSTomer Maimon 	u32 reg_con;
2099bf85fbcSTomer Maimon 	struct npcm_adc *info;
2109bf85fbcSTomer Maimon 	struct iio_dev *indio_dev;
2119bf85fbcSTomer Maimon 	struct device *dev = &pdev->dev;
2129bf85fbcSTomer Maimon 
2139bf85fbcSTomer Maimon 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
2149bf85fbcSTomer Maimon 	if (!indio_dev)
2159bf85fbcSTomer Maimon 		return -ENOMEM;
2169bf85fbcSTomer Maimon 	info = iio_priv(indio_dev);
2179bf85fbcSTomer Maimon 
2183ccb2524STomer Maimon 	info->data = device_get_match_data(dev);
2193ccb2524STomer Maimon 	if (!info->data)
2203ccb2524STomer Maimon 		return -EINVAL;
2213ccb2524STomer Maimon 
22208dfc6f8SSergiu Cuciurean 	mutex_init(&info->lock);
22308dfc6f8SSergiu Cuciurean 
2249bf85fbcSTomer Maimon 	info->dev = &pdev->dev;
2259bf85fbcSTomer Maimon 
22608cf48c7SJonathan Cameron 	info->regs = devm_platform_ioremap_resource(pdev, 0);
2279bf85fbcSTomer Maimon 	if (IS_ERR(info->regs))
2289bf85fbcSTomer Maimon 		return PTR_ERR(info->regs);
2299bf85fbcSTomer Maimon 
230db6bcb8cSTomer Maimon 	info->reset = devm_reset_control_get(&pdev->dev, NULL);
231db6bcb8cSTomer Maimon 	if (IS_ERR(info->reset))
232db6bcb8cSTomer Maimon 		return PTR_ERR(info->reset);
233db6bcb8cSTomer Maimon 
2349bf85fbcSTomer Maimon 	info->adc_clk = devm_clk_get(&pdev->dev, NULL);
2359bf85fbcSTomer Maimon 	if (IS_ERR(info->adc_clk)) {
2369bf85fbcSTomer Maimon 		dev_warn(&pdev->dev, "ADC clock failed: can't read clk\n");
2379bf85fbcSTomer Maimon 		return PTR_ERR(info->adc_clk);
2389bf85fbcSTomer Maimon 	}
2399bf85fbcSTomer Maimon 
2409bf85fbcSTomer Maimon 	/* calculate ADC clock sample rate */
2419bf85fbcSTomer Maimon 	reg_con = ioread32(info->regs + NPCM_ADCCON);
2429bf85fbcSTomer Maimon 	div = reg_con & NPCM_ADCCON_DIV_MASK;
2439bf85fbcSTomer Maimon 	div = div >> NPCM_ADCCON_DIV_SHIFT;
2449bf85fbcSTomer Maimon 	info->adc_sample_hz = clk_get_rate(info->adc_clk) / ((div + 1) * 2);
2459bf85fbcSTomer Maimon 
2469bf85fbcSTomer Maimon 	irq = platform_get_irq(pdev, 0);
247*c09ddcddSRuan Jinjie 	if (irq < 0) {
248*c09ddcddSRuan Jinjie 		ret = irq;
2499bf85fbcSTomer Maimon 		goto err_disable_clk;
2509bf85fbcSTomer Maimon 	}
2519bf85fbcSTomer Maimon 
2529bf85fbcSTomer Maimon 	ret = devm_request_irq(&pdev->dev, irq, npcm_adc_isr, 0,
2539bf85fbcSTomer Maimon 			       "NPCM_ADC", indio_dev);
2549bf85fbcSTomer Maimon 	if (ret < 0) {
2559bf85fbcSTomer Maimon 		dev_err(dev, "failed requesting interrupt\n");
2569bf85fbcSTomer Maimon 		goto err_disable_clk;
2579bf85fbcSTomer Maimon 	}
2589bf85fbcSTomer Maimon 
2599bf85fbcSTomer Maimon 	reg_con = ioread32(info->regs + NPCM_ADCCON);
2609bf85fbcSTomer Maimon 	info->vref = devm_regulator_get_optional(&pdev->dev, "vref");
2619bf85fbcSTomer Maimon 	if (!IS_ERR(info->vref)) {
2629bf85fbcSTomer Maimon 		ret = regulator_enable(info->vref);
2639bf85fbcSTomer Maimon 		if (ret) {
2649bf85fbcSTomer Maimon 			dev_err(&pdev->dev, "Can't enable ADC reference voltage\n");
2659bf85fbcSTomer Maimon 			goto err_disable_clk;
2669bf85fbcSTomer Maimon 		}
2679bf85fbcSTomer Maimon 
2689bf85fbcSTomer Maimon 		iowrite32(reg_con & ~NPCM_ADCCON_REFSEL,
2699bf85fbcSTomer Maimon 			  info->regs + NPCM_ADCCON);
2709bf85fbcSTomer Maimon 	} else {
2719bf85fbcSTomer Maimon 		/*
2729bf85fbcSTomer Maimon 		 * Any error which is not ENODEV indicates the regulator
2739bf85fbcSTomer Maimon 		 * has been specified and so is a failure case.
2749bf85fbcSTomer Maimon 		 */
2759bf85fbcSTomer Maimon 		if (PTR_ERR(info->vref) != -ENODEV) {
2769bf85fbcSTomer Maimon 			ret = PTR_ERR(info->vref);
2779bf85fbcSTomer Maimon 			goto err_disable_clk;
2789bf85fbcSTomer Maimon 		}
2799bf85fbcSTomer Maimon 
2809bf85fbcSTomer Maimon 		/* Use internal reference */
2819bf85fbcSTomer Maimon 		iowrite32(reg_con | NPCM_ADCCON_REFSEL,
2829bf85fbcSTomer Maimon 			  info->regs + NPCM_ADCCON);
2839bf85fbcSTomer Maimon 	}
2849bf85fbcSTomer Maimon 
2859bf85fbcSTomer Maimon 	init_waitqueue_head(&info->wq);
2869bf85fbcSTomer Maimon 
2879bf85fbcSTomer Maimon 	reg_con = ioread32(info->regs + NPCM_ADCCON);
2889bf85fbcSTomer Maimon 	reg_con |= NPCM_ADC_ENABLE;
2899bf85fbcSTomer Maimon 
2909bf85fbcSTomer Maimon 	/* Enable the ADC Module */
2919bf85fbcSTomer Maimon 	iowrite32(reg_con, info->regs + NPCM_ADCCON);
2929bf85fbcSTomer Maimon 
2939bf85fbcSTomer Maimon 	/* Start ADC conversion */
2949bf85fbcSTomer Maimon 	iowrite32(reg_con | NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON);
2959bf85fbcSTomer Maimon 
2969bf85fbcSTomer Maimon 	platform_set_drvdata(pdev, indio_dev);
2979bf85fbcSTomer Maimon 	indio_dev->name = dev_name(&pdev->dev);
2989bf85fbcSTomer Maimon 	indio_dev->info = &npcm_adc_iio_info;
2999bf85fbcSTomer Maimon 	indio_dev->modes = INDIO_DIRECT_MODE;
3009bf85fbcSTomer Maimon 	indio_dev->channels = npcm_adc_iio_channels;
3019bf85fbcSTomer Maimon 	indio_dev->num_channels = ARRAY_SIZE(npcm_adc_iio_channels);
3029bf85fbcSTomer Maimon 
3039bf85fbcSTomer Maimon 	ret = iio_device_register(indio_dev);
3049bf85fbcSTomer Maimon 	if (ret) {
3059bf85fbcSTomer Maimon 		dev_err(&pdev->dev, "Couldn't register the device.\n");
3069bf85fbcSTomer Maimon 		goto err_iio_register;
3079bf85fbcSTomer Maimon 	}
3089bf85fbcSTomer Maimon 
3099bf85fbcSTomer Maimon 	pr_info("NPCM ADC driver probed\n");
3109bf85fbcSTomer Maimon 
3119bf85fbcSTomer Maimon 	return 0;
3129bf85fbcSTomer Maimon 
3139bf85fbcSTomer Maimon err_iio_register:
3149bf85fbcSTomer Maimon 	iowrite32(reg_con & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON);
3159bf85fbcSTomer Maimon 	if (!IS_ERR(info->vref))
3169bf85fbcSTomer Maimon 		regulator_disable(info->vref);
3179bf85fbcSTomer Maimon err_disable_clk:
3189bf85fbcSTomer Maimon 	clk_disable_unprepare(info->adc_clk);
3199bf85fbcSTomer Maimon 
3209bf85fbcSTomer Maimon 	return ret;
3219bf85fbcSTomer Maimon }
3229bf85fbcSTomer Maimon 
npcm_adc_remove(struct platform_device * pdev)3239bf85fbcSTomer Maimon static int npcm_adc_remove(struct platform_device *pdev)
3249bf85fbcSTomer Maimon {
3259bf85fbcSTomer Maimon 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
3269bf85fbcSTomer Maimon 	struct npcm_adc *info = iio_priv(indio_dev);
3279bf85fbcSTomer Maimon 	u32 regtemp;
3289bf85fbcSTomer Maimon 
3299bf85fbcSTomer Maimon 	iio_device_unregister(indio_dev);
3309bf85fbcSTomer Maimon 
3319bf85fbcSTomer Maimon 	regtemp = ioread32(info->regs + NPCM_ADCCON);
3329bf85fbcSTomer Maimon 	iowrite32(regtemp & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON);
3339bf85fbcSTomer Maimon 	if (!IS_ERR(info->vref))
3349bf85fbcSTomer Maimon 		regulator_disable(info->vref);
3359bf85fbcSTomer Maimon 	clk_disable_unprepare(info->adc_clk);
3369bf85fbcSTomer Maimon 
3379bf85fbcSTomer Maimon 	return 0;
3389bf85fbcSTomer Maimon }
3399bf85fbcSTomer Maimon 
3409bf85fbcSTomer Maimon static struct platform_driver npcm_adc_driver = {
3419bf85fbcSTomer Maimon 	.probe		= npcm_adc_probe,
3429bf85fbcSTomer Maimon 	.remove		= npcm_adc_remove,
3439bf85fbcSTomer Maimon 	.driver		= {
3449bf85fbcSTomer Maimon 		.name	= "npcm_adc",
3459bf85fbcSTomer Maimon 		.of_match_table = npcm_adc_match,
3469bf85fbcSTomer Maimon 	},
3479bf85fbcSTomer Maimon };
3489bf85fbcSTomer Maimon 
3499bf85fbcSTomer Maimon module_platform_driver(npcm_adc_driver);
3509bf85fbcSTomer Maimon 
3519bf85fbcSTomer Maimon MODULE_DESCRIPTION("Nuvoton NPCM ADC Driver");
3529bf85fbcSTomer Maimon MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
3539bf85fbcSTomer Maimon MODULE_LICENSE("GPL v2");
354