dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / iio / dac / lpc18xx_dac.c
blob7036f77fdf232d82b5d69c936bd42c4c2e6d15fb
1 /*
2 * IIO DAC driver for NXP LPC18xx DAC
4 * Copyright (C) 2016 Joachim Eastwood <manabian@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * UNSUPPORTED hardware features:
11 * - Interrupts
12 * - DMA
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/driver.h>
19 #include <linux/io.h>
20 #include <linux/iopoll.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/regulator/consumer.h>
28 /* LPC18XX DAC registers and bits */
29 #define LPC18XX_DAC_CR 0x000
30 #define LPC18XX_DAC_CR_VALUE_SHIFT 6
31 #define LPC18XX_DAC_CR_VALUE_MASK 0x3ff
32 #define LPC18XX_DAC_CR_BIAS BIT(16)
33 #define LPC18XX_DAC_CTRL 0x004
34 #define LPC18XX_DAC_CTRL_DMA_ENA BIT(3)
36 struct lpc18xx_dac {
37 struct regulator *vref;
38 void __iomem *base;
39 struct mutex lock;
40 struct clk *clk;
43 static const struct iio_chan_spec lpc18xx_dac_iio_channels[] = {
45 .type = IIO_VOLTAGE,
46 .output = 1,
47 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
48 BIT(IIO_CHAN_INFO_SCALE),
52 static int lpc18xx_dac_read_raw(struct iio_dev *indio_dev,
53 struct iio_chan_spec const *chan,
54 int *val, int *val2, long mask)
56 struct lpc18xx_dac *dac = iio_priv(indio_dev);
57 u32 reg;
59 switch (mask) {
60 case IIO_CHAN_INFO_RAW:
61 reg = readl(dac->base + LPC18XX_DAC_CR);
62 *val = reg >> LPC18XX_DAC_CR_VALUE_SHIFT;
63 *val &= LPC18XX_DAC_CR_VALUE_MASK;
65 return IIO_VAL_INT;
67 case IIO_CHAN_INFO_SCALE:
68 *val = regulator_get_voltage(dac->vref) / 1000;
69 *val2 = 10;
71 return IIO_VAL_FRACTIONAL_LOG2;
74 return -EINVAL;
77 static int lpc18xx_dac_write_raw(struct iio_dev *indio_dev,
78 struct iio_chan_spec const *chan,
79 int val, int val2, long mask)
81 struct lpc18xx_dac *dac = iio_priv(indio_dev);
82 u32 reg;
84 switch (mask) {
85 case IIO_CHAN_INFO_RAW:
86 if (val < 0 || val > LPC18XX_DAC_CR_VALUE_MASK)
87 return -EINVAL;
89 reg = LPC18XX_DAC_CR_BIAS;
90 reg |= val << LPC18XX_DAC_CR_VALUE_SHIFT;
92 mutex_lock(&dac->lock);
93 writel(reg, dac->base + LPC18XX_DAC_CR);
94 writel(LPC18XX_DAC_CTRL_DMA_ENA, dac->base + LPC18XX_DAC_CTRL);
95 mutex_unlock(&dac->lock);
97 return 0;
100 return -EINVAL;
103 static const struct iio_info lpc18xx_dac_info = {
104 .read_raw = lpc18xx_dac_read_raw,
105 .write_raw = lpc18xx_dac_write_raw,
108 static int lpc18xx_dac_probe(struct platform_device *pdev)
110 struct iio_dev *indio_dev;
111 struct lpc18xx_dac *dac;
112 struct resource *res;
113 int ret;
115 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac));
116 if (!indio_dev)
117 return -ENOMEM;
119 platform_set_drvdata(pdev, indio_dev);
120 dac = iio_priv(indio_dev);
121 mutex_init(&dac->lock);
123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
124 dac->base = devm_ioremap_resource(&pdev->dev, res);
125 if (IS_ERR(dac->base))
126 return PTR_ERR(dac->base);
128 dac->clk = devm_clk_get(&pdev->dev, NULL);
129 if (IS_ERR(dac->clk)) {
130 dev_err(&pdev->dev, "error getting clock\n");
131 return PTR_ERR(dac->clk);
134 dac->vref = devm_regulator_get(&pdev->dev, "vref");
135 if (IS_ERR(dac->vref)) {
136 dev_err(&pdev->dev, "error getting regulator\n");
137 return PTR_ERR(dac->vref);
140 indio_dev->name = dev_name(&pdev->dev);
141 indio_dev->dev.parent = &pdev->dev;
142 indio_dev->info = &lpc18xx_dac_info;
143 indio_dev->modes = INDIO_DIRECT_MODE;
144 indio_dev->channels = lpc18xx_dac_iio_channels;
145 indio_dev->num_channels = ARRAY_SIZE(lpc18xx_dac_iio_channels);
147 ret = regulator_enable(dac->vref);
148 if (ret) {
149 dev_err(&pdev->dev, "unable to enable regulator\n");
150 return ret;
153 ret = clk_prepare_enable(dac->clk);
154 if (ret) {
155 dev_err(&pdev->dev, "unable to enable clock\n");
156 goto dis_reg;
159 writel(0, dac->base + LPC18XX_DAC_CTRL);
160 writel(0, dac->base + LPC18XX_DAC_CR);
162 ret = iio_device_register(indio_dev);
163 if (ret) {
164 dev_err(&pdev->dev, "unable to register device\n");
165 goto dis_clk;
168 return 0;
170 dis_clk:
171 clk_disable_unprepare(dac->clk);
172 dis_reg:
173 regulator_disable(dac->vref);
174 return ret;
177 static int lpc18xx_dac_remove(struct platform_device *pdev)
179 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
180 struct lpc18xx_dac *dac = iio_priv(indio_dev);
182 iio_device_unregister(indio_dev);
184 writel(0, dac->base + LPC18XX_DAC_CTRL);
185 clk_disable_unprepare(dac->clk);
186 regulator_disable(dac->vref);
188 return 0;
191 static const struct of_device_id lpc18xx_dac_match[] = {
192 { .compatible = "nxp,lpc1850-dac" },
193 { /* sentinel */ }
195 MODULE_DEVICE_TABLE(of, lpc18xx_dac_match);
197 static struct platform_driver lpc18xx_dac_driver = {
198 .probe = lpc18xx_dac_probe,
199 .remove = lpc18xx_dac_remove,
200 .driver = {
201 .name = "lpc18xx-dac",
202 .of_match_table = lpc18xx_dac_match,
205 module_platform_driver(lpc18xx_dac_driver);
207 MODULE_DESCRIPTION("LPC18xx DAC driver");
208 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
209 MODULE_LICENSE("GPL v2");