1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Rockchip Successive Approximation Register (SAR) A/D Converter
4 * Copyright (C) 2014 ROCKCHIP, Inc.
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/interrupt.h>
12 #include <linux/of_device.h>
13 #include <linux/clk.h>
14 #include <linux/completion.h>
15 #include <linux/delay.h>
16 #include <linux/reset.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/iio/iio.h>
20 #define SARADC_DATA 0x00
22 #define SARADC_STAS 0x04
23 #define SARADC_STAS_BUSY BIT(0)
25 #define SARADC_CTRL 0x08
26 #define SARADC_CTRL_IRQ_STATUS BIT(6)
27 #define SARADC_CTRL_IRQ_ENABLE BIT(5)
28 #define SARADC_CTRL_POWER_CTRL BIT(3)
29 #define SARADC_CTRL_CHN_MASK 0x7
31 #define SARADC_DLY_PU_SOC 0x0c
32 #define SARADC_DLY_PU_SOC_MASK 0x3f
34 #define SARADC_TIMEOUT msecs_to_jiffies(100)
36 struct rockchip_saradc_data
{
38 const struct iio_chan_spec
*channels
;
40 unsigned long clk_rate
;
43 struct rockchip_saradc
{
47 struct completion completion
;
48 struct regulator
*vref
;
49 struct reset_control
*reset
;
50 const struct rockchip_saradc_data
*data
;
54 static int rockchip_saradc_read_raw(struct iio_dev
*indio_dev
,
55 struct iio_chan_spec
const *chan
,
56 int *val
, int *val2
, long mask
)
58 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
62 case IIO_CHAN_INFO_RAW
:
63 mutex_lock(&indio_dev
->mlock
);
65 reinit_completion(&info
->completion
);
67 /* 8 clock periods as delay between power up and start cmd */
68 writel_relaxed(8, info
->regs
+ SARADC_DLY_PU_SOC
);
70 /* Select the channel to be used and trigger conversion */
71 writel(SARADC_CTRL_POWER_CTRL
72 | (chan
->channel
& SARADC_CTRL_CHN_MASK
)
73 | SARADC_CTRL_IRQ_ENABLE
,
74 info
->regs
+ SARADC_CTRL
);
76 if (!wait_for_completion_timeout(&info
->completion
,
78 writel_relaxed(0, info
->regs
+ SARADC_CTRL
);
79 mutex_unlock(&indio_dev
->mlock
);
83 *val
= info
->last_val
;
84 mutex_unlock(&indio_dev
->mlock
);
86 case IIO_CHAN_INFO_SCALE
:
87 ret
= regulator_get_voltage(info
->vref
);
89 dev_err(&indio_dev
->dev
, "failed to get voltage\n");
94 *val2
= info
->data
->num_bits
;
95 return IIO_VAL_FRACTIONAL_LOG2
;
101 static irqreturn_t
rockchip_saradc_isr(int irq
, void *dev_id
)
103 struct rockchip_saradc
*info
= dev_id
;
106 info
->last_val
= readl_relaxed(info
->regs
+ SARADC_DATA
);
107 info
->last_val
&= GENMASK(info
->data
->num_bits
- 1, 0);
109 /* Clear irq & power down adc */
110 writel_relaxed(0, info
->regs
+ SARADC_CTRL
);
112 complete(&info
->completion
);
117 static const struct iio_info rockchip_saradc_iio_info
= {
118 .read_raw
= rockchip_saradc_read_raw
,
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 const struct rockchip_saradc_data saradc_data
= {
138 .channels
= rockchip_saradc_iio_channels
,
139 .num_channels
= ARRAY_SIZE(rockchip_saradc_iio_channels
),
143 static const struct iio_chan_spec rockchip_rk3066_tsadc_iio_channels
[] = {
144 ADC_CHANNEL(0, "adc0"),
145 ADC_CHANNEL(1, "adc1"),
148 static const struct rockchip_saradc_data rk3066_tsadc_data
= {
150 .channels
= rockchip_rk3066_tsadc_iio_channels
,
151 .num_channels
= ARRAY_SIZE(rockchip_rk3066_tsadc_iio_channels
),
155 static const struct iio_chan_spec rockchip_rk3399_saradc_iio_channels
[] = {
156 ADC_CHANNEL(0, "adc0"),
157 ADC_CHANNEL(1, "adc1"),
158 ADC_CHANNEL(2, "adc2"),
159 ADC_CHANNEL(3, "adc3"),
160 ADC_CHANNEL(4, "adc4"),
161 ADC_CHANNEL(5, "adc5"),
164 static const struct rockchip_saradc_data rk3399_saradc_data
= {
166 .channels
= rockchip_rk3399_saradc_iio_channels
,
167 .num_channels
= ARRAY_SIZE(rockchip_rk3399_saradc_iio_channels
),
171 static const struct of_device_id rockchip_saradc_match
[] = {
173 .compatible
= "rockchip,saradc",
174 .data
= &saradc_data
,
176 .compatible
= "rockchip,rk3066-tsadc",
177 .data
= &rk3066_tsadc_data
,
179 .compatible
= "rockchip,rk3399-saradc",
180 .data
= &rk3399_saradc_data
,
184 MODULE_DEVICE_TABLE(of
, rockchip_saradc_match
);
187 * Reset SARADC Controller.
189 static void rockchip_saradc_reset_controller(struct reset_control
*reset
)
191 reset_control_assert(reset
);
192 usleep_range(10, 20);
193 reset_control_deassert(reset
);
196 static int rockchip_saradc_probe(struct platform_device
*pdev
)
198 struct rockchip_saradc
*info
= NULL
;
199 struct device_node
*np
= pdev
->dev
.of_node
;
200 struct iio_dev
*indio_dev
= NULL
;
201 struct resource
*mem
;
202 const struct of_device_id
*match
;
209 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*info
));
211 dev_err(&pdev
->dev
, "failed allocating iio device\n");
214 info
= iio_priv(indio_dev
);
216 match
= of_match_device(rockchip_saradc_match
, &pdev
->dev
);
218 dev_err(&pdev
->dev
, "failed to match device\n");
222 info
->data
= match
->data
;
224 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
225 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
226 if (IS_ERR(info
->regs
))
227 return PTR_ERR(info
->regs
);
230 * The reset should be an optional property, as it should work
231 * with old devicetrees as well
233 info
->reset
= devm_reset_control_get_exclusive(&pdev
->dev
,
235 if (IS_ERR(info
->reset
)) {
236 ret
= PTR_ERR(info
->reset
);
240 dev_dbg(&pdev
->dev
, "no reset control found\n");
244 init_completion(&info
->completion
);
246 irq
= platform_get_irq(pdev
, 0);
250 ret
= devm_request_irq(&pdev
->dev
, irq
, rockchip_saradc_isr
,
251 0, dev_name(&pdev
->dev
), info
);
253 dev_err(&pdev
->dev
, "failed requesting irq %d\n", irq
);
257 info
->pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
258 if (IS_ERR(info
->pclk
)) {
259 dev_err(&pdev
->dev
, "failed to get pclk\n");
260 return PTR_ERR(info
->pclk
);
263 info
->clk
= devm_clk_get(&pdev
->dev
, "saradc");
264 if (IS_ERR(info
->clk
)) {
265 dev_err(&pdev
->dev
, "failed to get adc clock\n");
266 return PTR_ERR(info
->clk
);
269 info
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
270 if (IS_ERR(info
->vref
)) {
271 dev_err(&pdev
->dev
, "failed to get regulator, %ld\n",
272 PTR_ERR(info
->vref
));
273 return PTR_ERR(info
->vref
);
277 rockchip_saradc_reset_controller(info
->reset
);
280 * Use a default value for the converter clock.
281 * This may become user-configurable in the future.
283 ret
= clk_set_rate(info
->clk
, info
->data
->clk_rate
);
285 dev_err(&pdev
->dev
, "failed to set adc clk rate, %d\n", ret
);
289 ret
= regulator_enable(info
->vref
);
291 dev_err(&pdev
->dev
, "failed to enable vref regulator\n");
295 ret
= clk_prepare_enable(info
->pclk
);
297 dev_err(&pdev
->dev
, "failed to enable pclk\n");
298 goto err_reg_voltage
;
301 ret
= clk_prepare_enable(info
->clk
);
303 dev_err(&pdev
->dev
, "failed to enable converter clock\n");
307 platform_set_drvdata(pdev
, indio_dev
);
309 indio_dev
->name
= dev_name(&pdev
->dev
);
310 indio_dev
->dev
.parent
= &pdev
->dev
;
311 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
312 indio_dev
->info
= &rockchip_saradc_iio_info
;
313 indio_dev
->modes
= INDIO_DIRECT_MODE
;
315 indio_dev
->channels
= info
->data
->channels
;
316 indio_dev
->num_channels
= info
->data
->num_channels
;
318 ret
= iio_device_register(indio_dev
);
325 clk_disable_unprepare(info
->clk
);
327 clk_disable_unprepare(info
->pclk
);
329 regulator_disable(info
->vref
);
333 static int rockchip_saradc_remove(struct platform_device
*pdev
)
335 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
336 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
338 iio_device_unregister(indio_dev
);
339 clk_disable_unprepare(info
->clk
);
340 clk_disable_unprepare(info
->pclk
);
341 regulator_disable(info
->vref
);
346 #ifdef CONFIG_PM_SLEEP
347 static int rockchip_saradc_suspend(struct device
*dev
)
349 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
350 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
352 clk_disable_unprepare(info
->clk
);
353 clk_disable_unprepare(info
->pclk
);
354 regulator_disable(info
->vref
);
359 static int rockchip_saradc_resume(struct device
*dev
)
361 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
362 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
365 ret
= regulator_enable(info
->vref
);
369 ret
= clk_prepare_enable(info
->pclk
);
373 ret
= clk_prepare_enable(info
->clk
);
381 static SIMPLE_DEV_PM_OPS(rockchip_saradc_pm_ops
,
382 rockchip_saradc_suspend
, rockchip_saradc_resume
);
384 static struct platform_driver rockchip_saradc_driver
= {
385 .probe
= rockchip_saradc_probe
,
386 .remove
= rockchip_saradc_remove
,
388 .name
= "rockchip-saradc",
389 .of_match_table
= rockchip_saradc_match
,
390 .pm
= &rockchip_saradc_pm_ops
,
394 module_platform_driver(rockchip_saradc_driver
);
396 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
397 MODULE_DESCRIPTION("Rockchip SARADC driver");
398 MODULE_LICENSE("GPL v2");