0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/clk.h>
0013 #include <linux/err.h>
0014 #include <linux/iio/iio.h>
0015 #include <linux/iio/driver.h>
0016 #include <linux/io.h>
0017 #include <linux/iopoll.h>
0018 #include <linux/module.h>
0019 #include <linux/mod_devicetable.h>
0020 #include <linux/mutex.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/regulator/consumer.h>
0023
0024
0025 #define LPC18XX_DAC_CR 0x000
0026 #define LPC18XX_DAC_CR_VALUE_SHIFT 6
0027 #define LPC18XX_DAC_CR_VALUE_MASK 0x3ff
0028 #define LPC18XX_DAC_CR_BIAS BIT(16)
0029 #define LPC18XX_DAC_CTRL 0x004
0030 #define LPC18XX_DAC_CTRL_DMA_ENA BIT(3)
0031
0032 struct lpc18xx_dac {
0033 struct regulator *vref;
0034 void __iomem *base;
0035 struct mutex lock;
0036 struct clk *clk;
0037 };
0038
0039 static const struct iio_chan_spec lpc18xx_dac_iio_channels[] = {
0040 {
0041 .type = IIO_VOLTAGE,
0042 .output = 1,
0043 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0044 BIT(IIO_CHAN_INFO_SCALE),
0045 },
0046 };
0047
0048 static int lpc18xx_dac_read_raw(struct iio_dev *indio_dev,
0049 struct iio_chan_spec const *chan,
0050 int *val, int *val2, long mask)
0051 {
0052 struct lpc18xx_dac *dac = iio_priv(indio_dev);
0053 u32 reg;
0054
0055 switch (mask) {
0056 case IIO_CHAN_INFO_RAW:
0057 reg = readl(dac->base + LPC18XX_DAC_CR);
0058 *val = reg >> LPC18XX_DAC_CR_VALUE_SHIFT;
0059 *val &= LPC18XX_DAC_CR_VALUE_MASK;
0060
0061 return IIO_VAL_INT;
0062
0063 case IIO_CHAN_INFO_SCALE:
0064 *val = regulator_get_voltage(dac->vref) / 1000;
0065 *val2 = 10;
0066
0067 return IIO_VAL_FRACTIONAL_LOG2;
0068 }
0069
0070 return -EINVAL;
0071 }
0072
0073 static int lpc18xx_dac_write_raw(struct iio_dev *indio_dev,
0074 struct iio_chan_spec const *chan,
0075 int val, int val2, long mask)
0076 {
0077 struct lpc18xx_dac *dac = iio_priv(indio_dev);
0078 u32 reg;
0079
0080 switch (mask) {
0081 case IIO_CHAN_INFO_RAW:
0082 if (val < 0 || val > LPC18XX_DAC_CR_VALUE_MASK)
0083 return -EINVAL;
0084
0085 reg = LPC18XX_DAC_CR_BIAS;
0086 reg |= val << LPC18XX_DAC_CR_VALUE_SHIFT;
0087
0088 mutex_lock(&dac->lock);
0089 writel(reg, dac->base + LPC18XX_DAC_CR);
0090 writel(LPC18XX_DAC_CTRL_DMA_ENA, dac->base + LPC18XX_DAC_CTRL);
0091 mutex_unlock(&dac->lock);
0092
0093 return 0;
0094 }
0095
0096 return -EINVAL;
0097 }
0098
0099 static const struct iio_info lpc18xx_dac_info = {
0100 .read_raw = lpc18xx_dac_read_raw,
0101 .write_raw = lpc18xx_dac_write_raw,
0102 };
0103
0104 static int lpc18xx_dac_probe(struct platform_device *pdev)
0105 {
0106 struct iio_dev *indio_dev;
0107 struct lpc18xx_dac *dac;
0108 int ret;
0109
0110 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac));
0111 if (!indio_dev)
0112 return -ENOMEM;
0113
0114 platform_set_drvdata(pdev, indio_dev);
0115 dac = iio_priv(indio_dev);
0116 mutex_init(&dac->lock);
0117
0118 dac->base = devm_platform_ioremap_resource(pdev, 0);
0119 if (IS_ERR(dac->base))
0120 return PTR_ERR(dac->base);
0121
0122 dac->clk = devm_clk_get(&pdev->dev, NULL);
0123 if (IS_ERR(dac->clk))
0124 return dev_err_probe(&pdev->dev, PTR_ERR(dac->clk),
0125 "error getting clock\n");
0126
0127 dac->vref = devm_regulator_get(&pdev->dev, "vref");
0128 if (IS_ERR(dac->vref))
0129 return dev_err_probe(&pdev->dev, PTR_ERR(dac->vref),
0130 "error getting regulator\n");
0131
0132 indio_dev->name = dev_name(&pdev->dev);
0133 indio_dev->info = &lpc18xx_dac_info;
0134 indio_dev->modes = INDIO_DIRECT_MODE;
0135 indio_dev->channels = lpc18xx_dac_iio_channels;
0136 indio_dev->num_channels = ARRAY_SIZE(lpc18xx_dac_iio_channels);
0137
0138 ret = regulator_enable(dac->vref);
0139 if (ret) {
0140 dev_err(&pdev->dev, "unable to enable regulator\n");
0141 return ret;
0142 }
0143
0144 ret = clk_prepare_enable(dac->clk);
0145 if (ret) {
0146 dev_err(&pdev->dev, "unable to enable clock\n");
0147 goto dis_reg;
0148 }
0149
0150 writel(0, dac->base + LPC18XX_DAC_CTRL);
0151 writel(0, dac->base + LPC18XX_DAC_CR);
0152
0153 ret = iio_device_register(indio_dev);
0154 if (ret) {
0155 dev_err(&pdev->dev, "unable to register device\n");
0156 goto dis_clk;
0157 }
0158
0159 return 0;
0160
0161 dis_clk:
0162 clk_disable_unprepare(dac->clk);
0163 dis_reg:
0164 regulator_disable(dac->vref);
0165 return ret;
0166 }
0167
0168 static int lpc18xx_dac_remove(struct platform_device *pdev)
0169 {
0170 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
0171 struct lpc18xx_dac *dac = iio_priv(indio_dev);
0172
0173 iio_device_unregister(indio_dev);
0174
0175 writel(0, dac->base + LPC18XX_DAC_CTRL);
0176 clk_disable_unprepare(dac->clk);
0177 regulator_disable(dac->vref);
0178
0179 return 0;
0180 }
0181
0182 static const struct of_device_id lpc18xx_dac_match[] = {
0183 { .compatible = "nxp,lpc1850-dac" },
0184 { }
0185 };
0186 MODULE_DEVICE_TABLE(of, lpc18xx_dac_match);
0187
0188 static struct platform_driver lpc18xx_dac_driver = {
0189 .probe = lpc18xx_dac_probe,
0190 .remove = lpc18xx_dac_remove,
0191 .driver = {
0192 .name = "lpc18xx-dac",
0193 .of_match_table = lpc18xx_dac_match,
0194 },
0195 };
0196 module_platform_driver(lpc18xx_dac_driver);
0197
0198 MODULE_DESCRIPTION("LPC18xx DAC driver");
0199 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
0200 MODULE_LICENSE("GPL v2");