1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO DAC driver for NXP LPC18xx DAC
5 * Copyright (C) 2016 Joachim Eastwood <manabian@gmail.com>
7 * UNSUPPORTED hardware features:
12 #include <linux/clk.h>
13 #include <linux/err.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/driver.h>
17 #include <linux/iopoll.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
25 /* LPC18XX DAC registers and bits */
26 #define LPC18XX_DAC_CR 0x000
27 #define LPC18XX_DAC_CR_VALUE_SHIFT 6
28 #define LPC18XX_DAC_CR_VALUE_MASK 0x3ff
29 #define LPC18XX_DAC_CR_BIAS BIT(16)
30 #define LPC18XX_DAC_CTRL 0x004
31 #define LPC18XX_DAC_CTRL_DMA_ENA BIT(3)
34 struct regulator
*vref
;
40 static const struct iio_chan_spec lpc18xx_dac_iio_channels
[] = {
44 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
45 BIT(IIO_CHAN_INFO_SCALE
),
49 static int lpc18xx_dac_read_raw(struct iio_dev
*indio_dev
,
50 struct iio_chan_spec
const *chan
,
51 int *val
, int *val2
, long mask
)
53 struct lpc18xx_dac
*dac
= iio_priv(indio_dev
);
57 case IIO_CHAN_INFO_RAW
:
58 reg
= readl(dac
->base
+ LPC18XX_DAC_CR
);
59 *val
= reg
>> LPC18XX_DAC_CR_VALUE_SHIFT
;
60 *val
&= LPC18XX_DAC_CR_VALUE_MASK
;
64 case IIO_CHAN_INFO_SCALE
:
65 *val
= regulator_get_voltage(dac
->vref
) / 1000;
68 return IIO_VAL_FRACTIONAL_LOG2
;
74 static int lpc18xx_dac_write_raw(struct iio_dev
*indio_dev
,
75 struct iio_chan_spec
const *chan
,
76 int val
, int val2
, long mask
)
78 struct lpc18xx_dac
*dac
= iio_priv(indio_dev
);
82 case IIO_CHAN_INFO_RAW
:
83 if (val
< 0 || val
> LPC18XX_DAC_CR_VALUE_MASK
)
86 reg
= LPC18XX_DAC_CR_BIAS
;
87 reg
|= val
<< LPC18XX_DAC_CR_VALUE_SHIFT
;
89 mutex_lock(&dac
->lock
);
90 writel(reg
, dac
->base
+ LPC18XX_DAC_CR
);
91 writel(LPC18XX_DAC_CTRL_DMA_ENA
, dac
->base
+ LPC18XX_DAC_CTRL
);
92 mutex_unlock(&dac
->lock
);
100 static const struct iio_info lpc18xx_dac_info
= {
101 .read_raw
= lpc18xx_dac_read_raw
,
102 .write_raw
= lpc18xx_dac_write_raw
,
105 static int lpc18xx_dac_probe(struct platform_device
*pdev
)
107 struct iio_dev
*indio_dev
;
108 struct lpc18xx_dac
*dac
;
109 struct resource
*res
;
112 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*dac
));
116 platform_set_drvdata(pdev
, indio_dev
);
117 dac
= iio_priv(indio_dev
);
118 mutex_init(&dac
->lock
);
120 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
121 dac
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
122 if (IS_ERR(dac
->base
))
123 return PTR_ERR(dac
->base
);
125 dac
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
126 if (IS_ERR(dac
->clk
)) {
127 dev_err(&pdev
->dev
, "error getting clock\n");
128 return PTR_ERR(dac
->clk
);
131 dac
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
132 if (IS_ERR(dac
->vref
)) {
133 dev_err(&pdev
->dev
, "error getting regulator\n");
134 return PTR_ERR(dac
->vref
);
137 indio_dev
->name
= dev_name(&pdev
->dev
);
138 indio_dev
->dev
.parent
= &pdev
->dev
;
139 indio_dev
->info
= &lpc18xx_dac_info
;
140 indio_dev
->modes
= INDIO_DIRECT_MODE
;
141 indio_dev
->channels
= lpc18xx_dac_iio_channels
;
142 indio_dev
->num_channels
= ARRAY_SIZE(lpc18xx_dac_iio_channels
);
144 ret
= regulator_enable(dac
->vref
);
146 dev_err(&pdev
->dev
, "unable to enable regulator\n");
150 ret
= clk_prepare_enable(dac
->clk
);
152 dev_err(&pdev
->dev
, "unable to enable clock\n");
156 writel(0, dac
->base
+ LPC18XX_DAC_CTRL
);
157 writel(0, dac
->base
+ LPC18XX_DAC_CR
);
159 ret
= iio_device_register(indio_dev
);
161 dev_err(&pdev
->dev
, "unable to register device\n");
168 clk_disable_unprepare(dac
->clk
);
170 regulator_disable(dac
->vref
);
174 static int lpc18xx_dac_remove(struct platform_device
*pdev
)
176 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
177 struct lpc18xx_dac
*dac
= iio_priv(indio_dev
);
179 iio_device_unregister(indio_dev
);
181 writel(0, dac
->base
+ LPC18XX_DAC_CTRL
);
182 clk_disable_unprepare(dac
->clk
);
183 regulator_disable(dac
->vref
);
188 static const struct of_device_id lpc18xx_dac_match
[] = {
189 { .compatible
= "nxp,lpc1850-dac" },
192 MODULE_DEVICE_TABLE(of
, lpc18xx_dac_match
);
194 static struct platform_driver lpc18xx_dac_driver
= {
195 .probe
= lpc18xx_dac_probe
,
196 .remove
= lpc18xx_dac_remove
,
198 .name
= "lpc18xx-dac",
199 .of_match_table
= lpc18xx_dac_match
,
202 module_platform_driver(lpc18xx_dac_driver
);
204 MODULE_DESCRIPTION("LPC18xx DAC driver");
205 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
206 MODULE_LICENSE("GPL v2");