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/mod_devicetable.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/consumer.h>
24 /* LPC18XX DAC registers and bits */
25 #define LPC18XX_DAC_CR 0x000
26 #define LPC18XX_DAC_CR_VALUE_SHIFT 6
27 #define LPC18XX_DAC_CR_VALUE_MASK 0x3ff
28 #define LPC18XX_DAC_CR_BIAS BIT(16)
29 #define LPC18XX_DAC_CTRL 0x004
30 #define LPC18XX_DAC_CTRL_DMA_ENA BIT(3)
33 struct regulator
*vref
;
39 static const struct iio_chan_spec lpc18xx_dac_iio_channels
[] = {
43 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
44 BIT(IIO_CHAN_INFO_SCALE
),
48 static int lpc18xx_dac_read_raw(struct iio_dev
*indio_dev
,
49 struct iio_chan_spec
const *chan
,
50 int *val
, int *val2
, long mask
)
52 struct lpc18xx_dac
*dac
= iio_priv(indio_dev
);
56 case IIO_CHAN_INFO_RAW
:
57 reg
= readl(dac
->base
+ LPC18XX_DAC_CR
);
58 *val
= reg
>> LPC18XX_DAC_CR_VALUE_SHIFT
;
59 *val
&= LPC18XX_DAC_CR_VALUE_MASK
;
63 case IIO_CHAN_INFO_SCALE
:
64 *val
= regulator_get_voltage(dac
->vref
) / 1000;
67 return IIO_VAL_FRACTIONAL_LOG2
;
73 static int lpc18xx_dac_write_raw(struct iio_dev
*indio_dev
,
74 struct iio_chan_spec
const *chan
,
75 int val
, int val2
, long mask
)
77 struct lpc18xx_dac
*dac
= iio_priv(indio_dev
);
81 case IIO_CHAN_INFO_RAW
:
82 if (val
< 0 || val
> LPC18XX_DAC_CR_VALUE_MASK
)
85 reg
= LPC18XX_DAC_CR_BIAS
;
86 reg
|= val
<< LPC18XX_DAC_CR_VALUE_SHIFT
;
88 mutex_lock(&dac
->lock
);
89 writel(reg
, dac
->base
+ LPC18XX_DAC_CR
);
90 writel(LPC18XX_DAC_CTRL_DMA_ENA
, dac
->base
+ LPC18XX_DAC_CTRL
);
91 mutex_unlock(&dac
->lock
);
99 static const struct iio_info lpc18xx_dac_info
= {
100 .read_raw
= lpc18xx_dac_read_raw
,
101 .write_raw
= lpc18xx_dac_write_raw
,
104 static int lpc18xx_dac_probe(struct platform_device
*pdev
)
106 struct iio_dev
*indio_dev
;
107 struct lpc18xx_dac
*dac
;
110 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*dac
));
114 platform_set_drvdata(pdev
, indio_dev
);
115 dac
= iio_priv(indio_dev
);
116 mutex_init(&dac
->lock
);
118 dac
->base
= devm_platform_ioremap_resource(pdev
, 0);
119 if (IS_ERR(dac
->base
))
120 return PTR_ERR(dac
->base
);
122 dac
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
123 if (IS_ERR(dac
->clk
))
124 return dev_err_probe(&pdev
->dev
, PTR_ERR(dac
->clk
),
125 "error getting clock\n");
127 dac
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
128 if (IS_ERR(dac
->vref
))
129 return dev_err_probe(&pdev
->dev
, PTR_ERR(dac
->vref
),
130 "error getting regulator\n");
132 indio_dev
->name
= dev_name(&pdev
->dev
);
133 indio_dev
->info
= &lpc18xx_dac_info
;
134 indio_dev
->modes
= INDIO_DIRECT_MODE
;
135 indio_dev
->channels
= lpc18xx_dac_iio_channels
;
136 indio_dev
->num_channels
= ARRAY_SIZE(lpc18xx_dac_iio_channels
);
138 ret
= regulator_enable(dac
->vref
);
140 dev_err(&pdev
->dev
, "unable to enable regulator\n");
144 ret
= clk_prepare_enable(dac
->clk
);
146 dev_err(&pdev
->dev
, "unable to enable clock\n");
150 writel(0, dac
->base
+ LPC18XX_DAC_CTRL
);
151 writel(0, dac
->base
+ LPC18XX_DAC_CR
);
153 ret
= iio_device_register(indio_dev
);
155 dev_err(&pdev
->dev
, "unable to register device\n");
162 clk_disable_unprepare(dac
->clk
);
164 regulator_disable(dac
->vref
);
168 static void lpc18xx_dac_remove(struct platform_device
*pdev
)
170 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
171 struct lpc18xx_dac
*dac
= iio_priv(indio_dev
);
173 iio_device_unregister(indio_dev
);
175 writel(0, dac
->base
+ LPC18XX_DAC_CTRL
);
176 clk_disable_unprepare(dac
->clk
);
177 regulator_disable(dac
->vref
);
180 static const struct of_device_id lpc18xx_dac_match
[] = {
181 { .compatible
= "nxp,lpc1850-dac" },
184 MODULE_DEVICE_TABLE(of
, lpc18xx_dac_match
);
186 static struct platform_driver lpc18xx_dac_driver
= {
187 .probe
= lpc18xx_dac_probe
,
188 .remove
= lpc18xx_dac_remove
,
190 .name
= "lpc18xx-dac",
191 .of_match_table
= lpc18xx_dac_match
,
194 module_platform_driver(lpc18xx_dac_driver
);
196 MODULE_DESCRIPTION("LPC18xx DAC driver");
197 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
198 MODULE_LICENSE("GPL v2");