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/of_device.h>
22 #include <linux/clk.h>
23 #include <linux/completion.h>
24 #include <linux/delay.h>
25 #include <linux/reset.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/iio/iio.h>
29 #define SARADC_DATA 0x00
31 #define SARADC_STAS 0x04
32 #define SARADC_STAS_BUSY BIT(0)
34 #define SARADC_CTRL 0x08
35 #define SARADC_CTRL_IRQ_STATUS BIT(6)
36 #define SARADC_CTRL_IRQ_ENABLE BIT(5)
37 #define SARADC_CTRL_POWER_CTRL BIT(3)
38 #define SARADC_CTRL_CHN_MASK 0x7
40 #define SARADC_DLY_PU_SOC 0x0c
41 #define SARADC_DLY_PU_SOC_MASK 0x3f
43 #define SARADC_TIMEOUT msecs_to_jiffies(100)
45 struct rockchip_saradc_data
{
47 const struct iio_chan_spec
*channels
;
49 unsigned long clk_rate
;
52 struct rockchip_saradc
{
56 struct completion completion
;
57 struct regulator
*vref
;
58 struct reset_control
*reset
;
59 const struct rockchip_saradc_data
*data
;
63 static int rockchip_saradc_read_raw(struct iio_dev
*indio_dev
,
64 struct iio_chan_spec
const *chan
,
65 int *val
, int *val2
, long mask
)
67 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
71 case IIO_CHAN_INFO_RAW
:
72 mutex_lock(&indio_dev
->mlock
);
74 reinit_completion(&info
->completion
);
76 /* 8 clock periods as delay between power up and start cmd */
77 writel_relaxed(8, info
->regs
+ SARADC_DLY_PU_SOC
);
79 /* Select the channel to be used and trigger conversion */
80 writel(SARADC_CTRL_POWER_CTRL
81 | (chan
->channel
& SARADC_CTRL_CHN_MASK
)
82 | SARADC_CTRL_IRQ_ENABLE
,
83 info
->regs
+ SARADC_CTRL
);
85 if (!wait_for_completion_timeout(&info
->completion
,
87 writel_relaxed(0, info
->regs
+ SARADC_CTRL
);
88 mutex_unlock(&indio_dev
->mlock
);
92 *val
= info
->last_val
;
93 mutex_unlock(&indio_dev
->mlock
);
95 case IIO_CHAN_INFO_SCALE
:
96 ret
= regulator_get_voltage(info
->vref
);
98 dev_err(&indio_dev
->dev
, "failed to get voltage\n");
103 *val2
= info
->data
->num_bits
;
104 return IIO_VAL_FRACTIONAL_LOG2
;
110 static irqreturn_t
rockchip_saradc_isr(int irq
, void *dev_id
)
112 struct rockchip_saradc
*info
= dev_id
;
115 info
->last_val
= readl_relaxed(info
->regs
+ SARADC_DATA
);
116 info
->last_val
&= GENMASK(info
->data
->num_bits
- 1, 0);
118 /* Clear irq & power down adc */
119 writel_relaxed(0, info
->regs
+ SARADC_CTRL
);
121 complete(&info
->completion
);
126 static const struct iio_info rockchip_saradc_iio_info
= {
127 .read_raw
= rockchip_saradc_read_raw
,
130 #define ADC_CHANNEL(_index, _id) { \
131 .type = IIO_VOLTAGE, \
134 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
135 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
136 .datasheet_name = _id, \
139 static const struct iio_chan_spec rockchip_saradc_iio_channels
[] = {
140 ADC_CHANNEL(0, "adc0"),
141 ADC_CHANNEL(1, "adc1"),
142 ADC_CHANNEL(2, "adc2"),
145 static const struct rockchip_saradc_data saradc_data
= {
147 .channels
= rockchip_saradc_iio_channels
,
148 .num_channels
= ARRAY_SIZE(rockchip_saradc_iio_channels
),
152 static const struct iio_chan_spec rockchip_rk3066_tsadc_iio_channels
[] = {
153 ADC_CHANNEL(0, "adc0"),
154 ADC_CHANNEL(1, "adc1"),
157 static const struct rockchip_saradc_data rk3066_tsadc_data
= {
159 .channels
= rockchip_rk3066_tsadc_iio_channels
,
160 .num_channels
= ARRAY_SIZE(rockchip_rk3066_tsadc_iio_channels
),
164 static const struct iio_chan_spec rockchip_rk3399_saradc_iio_channels
[] = {
165 ADC_CHANNEL(0, "adc0"),
166 ADC_CHANNEL(1, "adc1"),
167 ADC_CHANNEL(2, "adc2"),
168 ADC_CHANNEL(3, "adc3"),
169 ADC_CHANNEL(4, "adc4"),
170 ADC_CHANNEL(5, "adc5"),
173 static const struct rockchip_saradc_data rk3399_saradc_data
= {
175 .channels
= rockchip_rk3399_saradc_iio_channels
,
176 .num_channels
= ARRAY_SIZE(rockchip_rk3399_saradc_iio_channels
),
180 static const struct of_device_id rockchip_saradc_match
[] = {
182 .compatible
= "rockchip,saradc",
183 .data
= &saradc_data
,
185 .compatible
= "rockchip,rk3066-tsadc",
186 .data
= &rk3066_tsadc_data
,
188 .compatible
= "rockchip,rk3399-saradc",
189 .data
= &rk3399_saradc_data
,
193 MODULE_DEVICE_TABLE(of
, rockchip_saradc_match
);
196 * Reset SARADC Controller.
198 static void rockchip_saradc_reset_controller(struct reset_control
*reset
)
200 reset_control_assert(reset
);
201 usleep_range(10, 20);
202 reset_control_deassert(reset
);
205 static int rockchip_saradc_probe(struct platform_device
*pdev
)
207 struct rockchip_saradc
*info
= NULL
;
208 struct device_node
*np
= pdev
->dev
.of_node
;
209 struct iio_dev
*indio_dev
= NULL
;
210 struct resource
*mem
;
211 const struct of_device_id
*match
;
218 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*info
));
220 dev_err(&pdev
->dev
, "failed allocating iio device\n");
223 info
= iio_priv(indio_dev
);
225 match
= of_match_device(rockchip_saradc_match
, &pdev
->dev
);
227 dev_err(&pdev
->dev
, "failed to match device\n");
231 info
->data
= match
->data
;
233 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
234 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
235 if (IS_ERR(info
->regs
))
236 return PTR_ERR(info
->regs
);
239 * The reset should be an optional property, as it should work
240 * with old devicetrees as well
242 info
->reset
= devm_reset_control_get_exclusive(&pdev
->dev
,
244 if (IS_ERR(info
->reset
)) {
245 ret
= PTR_ERR(info
->reset
);
249 dev_dbg(&pdev
->dev
, "no reset control found\n");
253 init_completion(&info
->completion
);
255 irq
= platform_get_irq(pdev
, 0);
257 dev_err(&pdev
->dev
, "no irq resource?\n");
261 ret
= devm_request_irq(&pdev
->dev
, irq
, rockchip_saradc_isr
,
262 0, dev_name(&pdev
->dev
), info
);
264 dev_err(&pdev
->dev
, "failed requesting irq %d\n", irq
);
268 info
->pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
269 if (IS_ERR(info
->pclk
)) {
270 dev_err(&pdev
->dev
, "failed to get pclk\n");
271 return PTR_ERR(info
->pclk
);
274 info
->clk
= devm_clk_get(&pdev
->dev
, "saradc");
275 if (IS_ERR(info
->clk
)) {
276 dev_err(&pdev
->dev
, "failed to get adc clock\n");
277 return PTR_ERR(info
->clk
);
280 info
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
281 if (IS_ERR(info
->vref
)) {
282 dev_err(&pdev
->dev
, "failed to get regulator, %ld\n",
283 PTR_ERR(info
->vref
));
284 return PTR_ERR(info
->vref
);
288 rockchip_saradc_reset_controller(info
->reset
);
291 * Use a default value for the converter clock.
292 * This may become user-configurable in the future.
294 ret
= clk_set_rate(info
->clk
, info
->data
->clk_rate
);
296 dev_err(&pdev
->dev
, "failed to set adc clk rate, %d\n", ret
);
300 ret
= regulator_enable(info
->vref
);
302 dev_err(&pdev
->dev
, "failed to enable vref regulator\n");
306 ret
= clk_prepare_enable(info
->pclk
);
308 dev_err(&pdev
->dev
, "failed to enable pclk\n");
309 goto err_reg_voltage
;
312 ret
= clk_prepare_enable(info
->clk
);
314 dev_err(&pdev
->dev
, "failed to enable converter clock\n");
318 platform_set_drvdata(pdev
, indio_dev
);
320 indio_dev
->name
= dev_name(&pdev
->dev
);
321 indio_dev
->dev
.parent
= &pdev
->dev
;
322 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
323 indio_dev
->info
= &rockchip_saradc_iio_info
;
324 indio_dev
->modes
= INDIO_DIRECT_MODE
;
326 indio_dev
->channels
= info
->data
->channels
;
327 indio_dev
->num_channels
= info
->data
->num_channels
;
329 ret
= iio_device_register(indio_dev
);
336 clk_disable_unprepare(info
->clk
);
338 clk_disable_unprepare(info
->pclk
);
340 regulator_disable(info
->vref
);
344 static int rockchip_saradc_remove(struct platform_device
*pdev
)
346 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
347 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
349 iio_device_unregister(indio_dev
);
350 clk_disable_unprepare(info
->clk
);
351 clk_disable_unprepare(info
->pclk
);
352 regulator_disable(info
->vref
);
357 #ifdef CONFIG_PM_SLEEP
358 static int rockchip_saradc_suspend(struct device
*dev
)
360 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
361 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
363 clk_disable_unprepare(info
->clk
);
364 clk_disable_unprepare(info
->pclk
);
365 regulator_disable(info
->vref
);
370 static int rockchip_saradc_resume(struct device
*dev
)
372 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
373 struct rockchip_saradc
*info
= iio_priv(indio_dev
);
376 ret
= regulator_enable(info
->vref
);
380 ret
= clk_prepare_enable(info
->pclk
);
384 ret
= clk_prepare_enable(info
->clk
);
392 static SIMPLE_DEV_PM_OPS(rockchip_saradc_pm_ops
,
393 rockchip_saradc_suspend
, rockchip_saradc_resume
);
395 static struct platform_driver rockchip_saradc_driver
= {
396 .probe
= rockchip_saradc_probe
,
397 .remove
= rockchip_saradc_remove
,
399 .name
= "rockchip-saradc",
400 .of_match_table
= rockchip_saradc_match
,
401 .pm
= &rockchip_saradc_pm_ops
,
405 module_platform_driver(rockchip_saradc_driver
);
407 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
408 MODULE_DESCRIPTION("Rockchip SARADC driver");
409 MODULE_LICENSE("GPL v2");