2 * Rockchip Successive Approximation Register (SAR) A/D Converter
3 * Copyright (C) 2014 ROCKCHIP, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
21 #include <linux/clk.h>
22 #include <linux/completion.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/iio/iio.h>
26 #define SARADC_DATA 0x00
27 #define SARADC_DATA_MASK 0x3ff
29 #define SARADC_STAS 0x04
30 #define SARADC_STAS_BUSY BIT(0)
32 #define SARADC_CTRL 0x08
33 #define SARADC_CTRL_IRQ_STATUS BIT(6)
34 #define SARADC_CTRL_IRQ_ENABLE BIT(5)
35 #define SARADC_CTRL_POWER_CTRL BIT(3)
36 #define SARADC_CTRL_CHN_MASK 0x7
38 #define SARADC_DLY_PU_SOC 0x0c
39 #define SARADC_DLY_PU_SOC_MASK 0x3f
41 #define SARADC_BITS 10
42 #define SARADC_TIMEOUT msecs_to_jiffies(100)
44 struct rockchip_saradc
{
48 struct completion completion
;
49 struct regulator
*vref
;
53 static int rockchip_saradc_read_raw(struct iio_dev
*indio_dev
,
54 struct iio_chan_spec
const *chan
,
55 int *val
, int *val2
, long mask
)
57 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
61 case IIO_CHAN_INFO_RAW
:
62 mutex_lock(&indio_dev
->mlock
);
64 reinit_completion(&info
->completion
);
66 /* 8 clock periods as delay between power up and start cmd */
67 writel_relaxed(8, info
->regs
+ SARADC_DLY_PU_SOC
);
69 /* Select the channel to be used and trigger conversion */
70 writel(SARADC_CTRL_POWER_CTRL
71 | (chan
->channel
& SARADC_CTRL_CHN_MASK
)
72 | SARADC_CTRL_IRQ_ENABLE
,
73 info
->regs
+ SARADC_CTRL
);
75 if (!wait_for_completion_timeout(&info
->completion
,
77 writel_relaxed(0, info
->regs
+ SARADC_CTRL
);
78 mutex_unlock(&indio_dev
->mlock
);
82 *val
= info
->last_val
;
83 mutex_unlock(&indio_dev
->mlock
);
85 case IIO_CHAN_INFO_SCALE
:
86 ret
= regulator_get_voltage(info
->vref
);
88 dev_err(&indio_dev
->dev
, "failed to get voltage\n");
94 return IIO_VAL_FRACTIONAL_LOG2
;
100 static irqreturn_t
rockchip_saradc_isr(int irq
, void *dev_id
)
102 struct rockchip_saradc
*info
= (struct rockchip_saradc
*)dev_id
;
105 info
->last_val
= readl_relaxed(info
->regs
+ SARADC_DATA
);
106 info
->last_val
&= SARADC_DATA_MASK
;
108 /* Clear irq & power down adc */
109 writel_relaxed(0, info
->regs
+ SARADC_CTRL
);
111 complete(&info
->completion
);
116 static const struct iio_info rockchip_saradc_iio_info
= {
117 .read_raw
= rockchip_saradc_read_raw
,
118 .driver_module
= THIS_MODULE
,
121 #define ADC_CHANNEL(_index, _id) { \
122 .type = IIO_VOLTAGE, \
125 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
126 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
127 .datasheet_name = _id, \
130 static const struct iio_chan_spec rockchip_saradc_iio_channels
[] = {
131 ADC_CHANNEL(0, "adc0"),
132 ADC_CHANNEL(1, "adc1"),
133 ADC_CHANNEL(2, "adc2"),
136 static int rockchip_saradc_probe(struct platform_device
*pdev
)
138 struct rockchip_saradc
*info
= NULL
;
139 struct device_node
*np
= pdev
->dev
.of_node
;
140 struct iio_dev
*indio_dev
= NULL
;
141 struct resource
*mem
;
148 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*info
));
150 dev_err(&pdev
->dev
, "failed allocating iio device\n");
153 info
= iio_priv(indio_dev
);
155 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
156 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
157 if (IS_ERR(info
->regs
))
158 return PTR_ERR(info
->regs
);
160 init_completion(&info
->completion
);
162 irq
= platform_get_irq(pdev
, 0);
164 dev_err(&pdev
->dev
, "no irq resource?\n");
168 ret
= devm_request_irq(&pdev
->dev
, irq
, rockchip_saradc_isr
,
169 0, dev_name(&pdev
->dev
), info
);
171 dev_err(&pdev
->dev
, "failed requesting irq %d\n", irq
);
175 info
->pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
176 if (IS_ERR(info
->pclk
)) {
177 dev_err(&pdev
->dev
, "failed to get pclk\n");
178 return PTR_ERR(info
->pclk
);
181 info
->clk
= devm_clk_get(&pdev
->dev
, "saradc");
182 if (IS_ERR(info
->clk
)) {
183 dev_err(&pdev
->dev
, "failed to get adc clock\n");
184 return PTR_ERR(info
->clk
);
187 info
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
188 if (IS_ERR(info
->vref
)) {
189 dev_err(&pdev
->dev
, "failed to get regulator, %ld\n",
190 PTR_ERR(info
->vref
));
191 return PTR_ERR(info
->vref
);
195 * Use a default of 1MHz for the converter clock.
196 * This may become user-configurable in the future.
198 ret
= clk_set_rate(info
->clk
, 1000000);
200 dev_err(&pdev
->dev
, "failed to set adc clk rate, %d\n", ret
);
204 ret
= regulator_enable(info
->vref
);
206 dev_err(&pdev
->dev
, "failed to enable vref regulator\n");
210 ret
= clk_prepare_enable(info
->pclk
);
212 dev_err(&pdev
->dev
, "failed to enable pclk\n");
213 goto err_reg_voltage
;
216 ret
= clk_prepare_enable(info
->clk
);
218 dev_err(&pdev
->dev
, "failed to enable converter clock\n");
222 platform_set_drvdata(pdev
, indio_dev
);
224 indio_dev
->name
= dev_name(&pdev
->dev
);
225 indio_dev
->dev
.parent
= &pdev
->dev
;
226 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
227 indio_dev
->info
= &rockchip_saradc_iio_info
;
228 indio_dev
->modes
= INDIO_DIRECT_MODE
;
230 indio_dev
->channels
= rockchip_saradc_iio_channels
;
231 indio_dev
->num_channels
= ARRAY_SIZE(rockchip_saradc_iio_channels
);
233 ret
= iio_device_register(indio_dev
);
240 clk_disable_unprepare(info
->clk
);
242 clk_disable_unprepare(info
->pclk
);
244 regulator_disable(info
->vref
);
248 static int rockchip_saradc_remove(struct platform_device
*pdev
)
250 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
251 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
253 iio_device_unregister(indio_dev
);
254 clk_disable_unprepare(info
->clk
);
255 clk_disable_unprepare(info
->pclk
);
256 regulator_disable(info
->vref
);
261 #ifdef CONFIG_PM_SLEEP
262 static int rockchip_saradc_suspend(struct device
*dev
)
264 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
265 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
267 clk_disable_unprepare(info
->clk
);
268 clk_disable_unprepare(info
->pclk
);
269 regulator_disable(info
->vref
);
274 static int rockchip_saradc_resume(struct device
*dev
)
276 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
277 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
280 ret
= regulator_enable(info
->vref
);
284 ret
= clk_prepare_enable(info
->pclk
);
288 ret
= clk_prepare_enable(info
->clk
);
296 static SIMPLE_DEV_PM_OPS(rockchip_saradc_pm_ops
,
297 rockchip_saradc_suspend
, rockchip_saradc_resume
);
299 static const struct of_device_id rockchip_saradc_match
[] = {
300 { .compatible
= "rockchip,saradc" },
303 MODULE_DEVICE_TABLE(of
, rockchip_saradc_match
);
305 static struct platform_driver rockchip_saradc_driver
= {
306 .probe
= rockchip_saradc_probe
,
307 .remove
= rockchip_saradc_remove
,
309 .name
= "rockchip-saradc",
310 .owner
= THIS_MODULE
,
311 .of_match_table
= rockchip_saradc_match
,
312 .pm
= &rockchip_saradc_pm_ops
,
316 module_platform_driver(rockchip_saradc_driver
);