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:
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/driver.h>
20 #include <linux/iopoll.h>
21 #include <linux/module.h>
22 #include <linux/mutex.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)
37 struct regulator
*vref
;
43 static const struct iio_chan_spec lpc18xx_dac_iio_channels
[] = {
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
);
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
;
67 case IIO_CHAN_INFO_SCALE
:
68 *val
= regulator_get_voltage(dac
->vref
) / 1000;
71 return IIO_VAL_FRACTIONAL_LOG2
;
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
);
85 case IIO_CHAN_INFO_RAW
:
86 if (val
< 0 || val
> LPC18XX_DAC_CR_VALUE_MASK
)
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
);
103 static const struct iio_info lpc18xx_dac_info
= {
104 .read_raw
= lpc18xx_dac_read_raw
,
105 .write_raw
= lpc18xx_dac_write_raw
,
106 .driver_module
= THIS_MODULE
,
109 static int lpc18xx_dac_probe(struct platform_device
*pdev
)
111 struct iio_dev
*indio_dev
;
112 struct lpc18xx_dac
*dac
;
113 struct resource
*res
;
116 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*dac
));
120 platform_set_drvdata(pdev
, indio_dev
);
121 dac
= iio_priv(indio_dev
);
122 mutex_init(&dac
->lock
);
124 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
125 dac
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
126 if (IS_ERR(dac
->base
))
127 return PTR_ERR(dac
->base
);
129 dac
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
130 if (IS_ERR(dac
->clk
)) {
131 dev_err(&pdev
->dev
, "error getting clock\n");
132 return PTR_ERR(dac
->clk
);
135 dac
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
136 if (IS_ERR(dac
->vref
)) {
137 dev_err(&pdev
->dev
, "error getting regulator\n");
138 return PTR_ERR(dac
->vref
);
141 indio_dev
->name
= dev_name(&pdev
->dev
);
142 indio_dev
->dev
.parent
= &pdev
->dev
;
143 indio_dev
->info
= &lpc18xx_dac_info
;
144 indio_dev
->modes
= INDIO_DIRECT_MODE
;
145 indio_dev
->channels
= lpc18xx_dac_iio_channels
;
146 indio_dev
->num_channels
= ARRAY_SIZE(lpc18xx_dac_iio_channels
);
148 ret
= regulator_enable(dac
->vref
);
150 dev_err(&pdev
->dev
, "unable to enable regulator\n");
154 ret
= clk_prepare_enable(dac
->clk
);
156 dev_err(&pdev
->dev
, "unable to enable clock\n");
160 writel(0, dac
->base
+ LPC18XX_DAC_CTRL
);
161 writel(0, dac
->base
+ LPC18XX_DAC_CR
);
163 ret
= iio_device_register(indio_dev
);
165 dev_err(&pdev
->dev
, "unable to register device\n");
172 clk_disable_unprepare(dac
->clk
);
174 regulator_disable(dac
->vref
);
178 static int lpc18xx_dac_remove(struct platform_device
*pdev
)
180 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
181 struct lpc18xx_dac
*dac
= iio_priv(indio_dev
);
183 iio_device_unregister(indio_dev
);
185 writel(0, dac
->base
+ LPC18XX_DAC_CTRL
);
186 clk_disable_unprepare(dac
->clk
);
187 regulator_disable(dac
->vref
);
192 static const struct of_device_id lpc18xx_dac_match
[] = {
193 { .compatible
= "nxp,lpc1850-dac" },
196 MODULE_DEVICE_TABLE(of
, lpc18xx_dac_match
);
198 static struct platform_driver lpc18xx_dac_driver
= {
199 .probe
= lpc18xx_dac_probe
,
200 .remove
= lpc18xx_dac_remove
,
202 .name
= "lpc18xx-dac",
203 .of_match_table
= lpc18xx_dac_match
,
206 module_platform_driver(lpc18xx_dac_driver
);
208 MODULE_DESCRIPTION("LPC18xx DAC driver");
209 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
210 MODULE_LICENSE("GPL v2");