WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / adc / rockchip_saradc.c
blob12584f1631d88887c8835e366f82a602ddc0a2b2
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Rockchip Successive Approximation Register (SAR) A/D Converter
4 * Copyright (C) 2014 ROCKCHIP, Inc.
5 */
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/of.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/buffer.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/iio/triggered_buffer.h>
23 #define SARADC_DATA 0x00
25 #define SARADC_STAS 0x04
26 #define SARADC_STAS_BUSY BIT(0)
28 #define SARADC_CTRL 0x08
29 #define SARADC_CTRL_IRQ_STATUS BIT(6)
30 #define SARADC_CTRL_IRQ_ENABLE BIT(5)
31 #define SARADC_CTRL_POWER_CTRL BIT(3)
32 #define SARADC_CTRL_CHN_MASK 0x7
34 #define SARADC_DLY_PU_SOC 0x0c
35 #define SARADC_DLY_PU_SOC_MASK 0x3f
37 #define SARADC_TIMEOUT msecs_to_jiffies(100)
38 #define SARADC_MAX_CHANNELS 6
40 struct rockchip_saradc_data {
41 const struct iio_chan_spec *channels;
42 int num_channels;
43 unsigned long clk_rate;
46 struct rockchip_saradc {
47 void __iomem *regs;
48 struct clk *pclk;
49 struct clk *clk;
50 struct completion completion;
51 struct regulator *vref;
52 struct reset_control *reset;
53 const struct rockchip_saradc_data *data;
54 u16 last_val;
55 const struct iio_chan_spec *last_chan;
58 static void rockchip_saradc_power_down(struct rockchip_saradc *info)
60 /* Clear irq & power down adc */
61 writel_relaxed(0, info->regs + SARADC_CTRL);
64 static int rockchip_saradc_conversion(struct rockchip_saradc *info,
65 struct iio_chan_spec const *chan)
67 reinit_completion(&info->completion);
69 /* 8 clock periods as delay between power up and start cmd */
70 writel_relaxed(8, info->regs + SARADC_DLY_PU_SOC);
72 info->last_chan = chan;
74 /* Select the channel to be used and trigger conversion */
75 writel(SARADC_CTRL_POWER_CTRL
76 | (chan->channel & SARADC_CTRL_CHN_MASK)
77 | SARADC_CTRL_IRQ_ENABLE,
78 info->regs + SARADC_CTRL);
80 if (!wait_for_completion_timeout(&info->completion, SARADC_TIMEOUT))
81 return -ETIMEDOUT;
83 return 0;
86 static int rockchip_saradc_read_raw(struct iio_dev *indio_dev,
87 struct iio_chan_spec const *chan,
88 int *val, int *val2, long mask)
90 struct rockchip_saradc *info = iio_priv(indio_dev);
91 int ret;
93 switch (mask) {
94 case IIO_CHAN_INFO_RAW:
95 mutex_lock(&indio_dev->mlock);
97 ret = rockchip_saradc_conversion(info, chan);
98 if (ret) {
99 rockchip_saradc_power_down(info);
100 mutex_unlock(&indio_dev->mlock);
101 return ret;
104 *val = info->last_val;
105 mutex_unlock(&indio_dev->mlock);
106 return IIO_VAL_INT;
107 case IIO_CHAN_INFO_SCALE:
108 ret = regulator_get_voltage(info->vref);
109 if (ret < 0) {
110 dev_err(&indio_dev->dev, "failed to get voltage\n");
111 return ret;
114 *val = ret / 1000;
115 *val2 = chan->scan_type.realbits;
116 return IIO_VAL_FRACTIONAL_LOG2;
117 default:
118 return -EINVAL;
122 static irqreturn_t rockchip_saradc_isr(int irq, void *dev_id)
124 struct rockchip_saradc *info = dev_id;
126 /* Read value */
127 info->last_val = readl_relaxed(info->regs + SARADC_DATA);
128 info->last_val &= GENMASK(info->last_chan->scan_type.realbits - 1, 0);
130 rockchip_saradc_power_down(info);
132 complete(&info->completion);
134 return IRQ_HANDLED;
137 static const struct iio_info rockchip_saradc_iio_info = {
138 .read_raw = rockchip_saradc_read_raw,
141 #define SARADC_CHANNEL(_index, _id, _res) { \
142 .type = IIO_VOLTAGE, \
143 .indexed = 1, \
144 .channel = _index, \
145 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
146 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
147 .datasheet_name = _id, \
148 .scan_index = _index, \
149 .scan_type = { \
150 .sign = 'u', \
151 .realbits = _res, \
152 .storagebits = 16, \
153 .endianness = IIO_CPU, \
154 }, \
157 static const struct iio_chan_spec rockchip_saradc_iio_channels[] = {
158 SARADC_CHANNEL(0, "adc0", 10),
159 SARADC_CHANNEL(1, "adc1", 10),
160 SARADC_CHANNEL(2, "adc2", 10),
163 static const struct rockchip_saradc_data saradc_data = {
164 .channels = rockchip_saradc_iio_channels,
165 .num_channels = ARRAY_SIZE(rockchip_saradc_iio_channels),
166 .clk_rate = 1000000,
169 static const struct iio_chan_spec rockchip_rk3066_tsadc_iio_channels[] = {
170 SARADC_CHANNEL(0, "adc0", 12),
171 SARADC_CHANNEL(1, "adc1", 12),
174 static const struct rockchip_saradc_data rk3066_tsadc_data = {
175 .channels = rockchip_rk3066_tsadc_iio_channels,
176 .num_channels = ARRAY_SIZE(rockchip_rk3066_tsadc_iio_channels),
177 .clk_rate = 50000,
180 static const struct iio_chan_spec rockchip_rk3399_saradc_iio_channels[] = {
181 SARADC_CHANNEL(0, "adc0", 10),
182 SARADC_CHANNEL(1, "adc1", 10),
183 SARADC_CHANNEL(2, "adc2", 10),
184 SARADC_CHANNEL(3, "adc3", 10),
185 SARADC_CHANNEL(4, "adc4", 10),
186 SARADC_CHANNEL(5, "adc5", 10),
189 static const struct rockchip_saradc_data rk3399_saradc_data = {
190 .channels = rockchip_rk3399_saradc_iio_channels,
191 .num_channels = ARRAY_SIZE(rockchip_rk3399_saradc_iio_channels),
192 .clk_rate = 1000000,
195 static const struct of_device_id rockchip_saradc_match[] = {
197 .compatible = "rockchip,saradc",
198 .data = &saradc_data,
199 }, {
200 .compatible = "rockchip,rk3066-tsadc",
201 .data = &rk3066_tsadc_data,
202 }, {
203 .compatible = "rockchip,rk3399-saradc",
204 .data = &rk3399_saradc_data,
208 MODULE_DEVICE_TABLE(of, rockchip_saradc_match);
211 * Reset SARADC Controller.
213 static void rockchip_saradc_reset_controller(struct reset_control *reset)
215 reset_control_assert(reset);
216 usleep_range(10, 20);
217 reset_control_deassert(reset);
220 static void rockchip_saradc_clk_disable(void *data)
222 struct rockchip_saradc *info = data;
224 clk_disable_unprepare(info->clk);
227 static void rockchip_saradc_pclk_disable(void *data)
229 struct rockchip_saradc *info = data;
231 clk_disable_unprepare(info->pclk);
234 static void rockchip_saradc_regulator_disable(void *data)
236 struct rockchip_saradc *info = data;
238 regulator_disable(info->vref);
241 static irqreturn_t rockchip_saradc_trigger_handler(int irq, void *p)
243 struct iio_poll_func *pf = p;
244 struct iio_dev *i_dev = pf->indio_dev;
245 struct rockchip_saradc *info = iio_priv(i_dev);
247 * @values: each channel takes an u16 value
248 * @timestamp: will be 8-byte aligned automatically
250 struct {
251 u16 values[SARADC_MAX_CHANNELS];
252 int64_t timestamp;
253 } data;
254 int ret;
255 int i, j = 0;
257 mutex_lock(&i_dev->mlock);
259 for_each_set_bit(i, i_dev->active_scan_mask, i_dev->masklength) {
260 const struct iio_chan_spec *chan = &i_dev->channels[i];
262 ret = rockchip_saradc_conversion(info, chan);
263 if (ret) {
264 rockchip_saradc_power_down(info);
265 goto out;
268 data.values[j] = info->last_val;
269 j++;
272 iio_push_to_buffers_with_timestamp(i_dev, &data, iio_get_time_ns(i_dev));
273 out:
274 mutex_unlock(&i_dev->mlock);
276 iio_trigger_notify_done(i_dev->trig);
278 return IRQ_HANDLED;
281 static int rockchip_saradc_probe(struct platform_device *pdev)
283 struct rockchip_saradc *info = NULL;
284 struct device_node *np = pdev->dev.of_node;
285 struct iio_dev *indio_dev = NULL;
286 struct resource *mem;
287 const struct of_device_id *match;
288 int ret;
289 int irq;
291 if (!np)
292 return -ENODEV;
294 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
295 if (!indio_dev) {
296 dev_err(&pdev->dev, "failed allocating iio device\n");
297 return -ENOMEM;
299 info = iio_priv(indio_dev);
301 match = of_match_device(rockchip_saradc_match, &pdev->dev);
302 if (!match) {
303 dev_err(&pdev->dev, "failed to match device\n");
304 return -ENODEV;
307 info->data = match->data;
309 /* Sanity check for possible later IP variants with more channels */
310 if (info->data->num_channels > SARADC_MAX_CHANNELS) {
311 dev_err(&pdev->dev, "max channels exceeded");
312 return -EINVAL;
315 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
316 info->regs = devm_ioremap_resource(&pdev->dev, mem);
317 if (IS_ERR(info->regs))
318 return PTR_ERR(info->regs);
321 * The reset should be an optional property, as it should work
322 * with old devicetrees as well
324 info->reset = devm_reset_control_get_exclusive(&pdev->dev,
325 "saradc-apb");
326 if (IS_ERR(info->reset)) {
327 ret = PTR_ERR(info->reset);
328 if (ret != -ENOENT)
329 return ret;
331 dev_dbg(&pdev->dev, "no reset control found\n");
332 info->reset = NULL;
335 init_completion(&info->completion);
337 irq = platform_get_irq(pdev, 0);
338 if (irq < 0)
339 return irq;
341 ret = devm_request_irq(&pdev->dev, irq, rockchip_saradc_isr,
342 0, dev_name(&pdev->dev), info);
343 if (ret < 0) {
344 dev_err(&pdev->dev, "failed requesting irq %d\n", irq);
345 return ret;
348 info->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
349 if (IS_ERR(info->pclk)) {
350 dev_err(&pdev->dev, "failed to get pclk\n");
351 return PTR_ERR(info->pclk);
354 info->clk = devm_clk_get(&pdev->dev, "saradc");
355 if (IS_ERR(info->clk)) {
356 dev_err(&pdev->dev, "failed to get adc clock\n");
357 return PTR_ERR(info->clk);
360 info->vref = devm_regulator_get(&pdev->dev, "vref");
361 if (IS_ERR(info->vref)) {
362 dev_err(&pdev->dev, "failed to get regulator, %ld\n",
363 PTR_ERR(info->vref));
364 return PTR_ERR(info->vref);
367 if (info->reset)
368 rockchip_saradc_reset_controller(info->reset);
371 * Use a default value for the converter clock.
372 * This may become user-configurable in the future.
374 ret = clk_set_rate(info->clk, info->data->clk_rate);
375 if (ret < 0) {
376 dev_err(&pdev->dev, "failed to set adc clk rate, %d\n", ret);
377 return ret;
380 ret = regulator_enable(info->vref);
381 if (ret < 0) {
382 dev_err(&pdev->dev, "failed to enable vref regulator\n");
383 return ret;
385 ret = devm_add_action_or_reset(&pdev->dev,
386 rockchip_saradc_regulator_disable, info);
387 if (ret) {
388 dev_err(&pdev->dev, "failed to register devm action, %d\n",
389 ret);
390 return ret;
393 ret = clk_prepare_enable(info->pclk);
394 if (ret < 0) {
395 dev_err(&pdev->dev, "failed to enable pclk\n");
396 return ret;
398 ret = devm_add_action_or_reset(&pdev->dev,
399 rockchip_saradc_pclk_disable, info);
400 if (ret) {
401 dev_err(&pdev->dev, "failed to register devm action, %d\n",
402 ret);
403 return ret;
406 ret = clk_prepare_enable(info->clk);
407 if (ret < 0) {
408 dev_err(&pdev->dev, "failed to enable converter clock\n");
409 return ret;
411 ret = devm_add_action_or_reset(&pdev->dev,
412 rockchip_saradc_clk_disable, info);
413 if (ret) {
414 dev_err(&pdev->dev, "failed to register devm action, %d\n",
415 ret);
416 return ret;
419 platform_set_drvdata(pdev, indio_dev);
421 indio_dev->name = dev_name(&pdev->dev);
422 indio_dev->info = &rockchip_saradc_iio_info;
423 indio_dev->modes = INDIO_DIRECT_MODE;
425 indio_dev->channels = info->data->channels;
426 indio_dev->num_channels = info->data->num_channels;
427 ret = devm_iio_triggered_buffer_setup(&indio_dev->dev, indio_dev, NULL,
428 rockchip_saradc_trigger_handler,
429 NULL);
430 if (ret)
431 return ret;
433 return devm_iio_device_register(&pdev->dev, indio_dev);
436 #ifdef CONFIG_PM_SLEEP
437 static int rockchip_saradc_suspend(struct device *dev)
439 struct iio_dev *indio_dev = dev_get_drvdata(dev);
440 struct rockchip_saradc *info = iio_priv(indio_dev);
442 clk_disable_unprepare(info->clk);
443 clk_disable_unprepare(info->pclk);
444 regulator_disable(info->vref);
446 return 0;
449 static int rockchip_saradc_resume(struct device *dev)
451 struct iio_dev *indio_dev = dev_get_drvdata(dev);
452 struct rockchip_saradc *info = iio_priv(indio_dev);
453 int ret;
455 ret = regulator_enable(info->vref);
456 if (ret)
457 return ret;
459 ret = clk_prepare_enable(info->pclk);
460 if (ret)
461 return ret;
463 ret = clk_prepare_enable(info->clk);
464 if (ret)
465 clk_disable_unprepare(info->pclk);
467 return ret;
469 #endif
471 static SIMPLE_DEV_PM_OPS(rockchip_saradc_pm_ops,
472 rockchip_saradc_suspend, rockchip_saradc_resume);
474 static struct platform_driver rockchip_saradc_driver = {
475 .probe = rockchip_saradc_probe,
476 .driver = {
477 .name = "rockchip-saradc",
478 .of_match_table = rockchip_saradc_match,
479 .pm = &rockchip_saradc_pm_ops,
483 module_platform_driver(rockchip_saradc_driver);
485 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
486 MODULE_DESCRIPTION("Rockchip SARADC driver");
487 MODULE_LICENSE("GPL v2");