dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / dac / vf610_dac.c
blob7f1e9317c3f3b44b2c9267f62b9d28d1b07ec204
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Freescale Vybrid vf610 DAC driver
5 * Copyright 2016 Toradex AG
6 */
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define VF610_DACx_STATCTRL 0x20
23 #define VF610_DAC_DACEN BIT(15)
24 #define VF610_DAC_DACRFS BIT(14)
25 #define VF610_DAC_LPEN BIT(11)
27 #define VF610_DAC_DAT0(x) ((x) & 0xFFF)
29 enum vf610_conversion_mode_sel {
30 VF610_DAC_CONV_HIGH_POWER,
31 VF610_DAC_CONV_LOW_POWER,
34 struct vf610_dac {
35 struct clk *clk;
36 struct device *dev;
37 enum vf610_conversion_mode_sel conv_mode;
38 void __iomem *regs;
41 static void vf610_dac_init(struct vf610_dac *info)
43 int val;
45 info->conv_mode = VF610_DAC_CONV_LOW_POWER;
46 val = VF610_DAC_DACEN | VF610_DAC_DACRFS |
47 VF610_DAC_LPEN;
48 writel(val, info->regs + VF610_DACx_STATCTRL);
51 static void vf610_dac_exit(struct vf610_dac *info)
53 int val;
55 val = readl(info->regs + VF610_DACx_STATCTRL);
56 val &= ~VF610_DAC_DACEN;
57 writel(val, info->regs + VF610_DACx_STATCTRL);
60 static int vf610_set_conversion_mode(struct iio_dev *indio_dev,
61 const struct iio_chan_spec *chan,
62 unsigned int mode)
64 struct vf610_dac *info = iio_priv(indio_dev);
65 int val;
67 mutex_lock(&indio_dev->mlock);
68 info->conv_mode = mode;
69 val = readl(info->regs + VF610_DACx_STATCTRL);
70 if (mode)
71 val |= VF610_DAC_LPEN;
72 else
73 val &= ~VF610_DAC_LPEN;
74 writel(val, info->regs + VF610_DACx_STATCTRL);
75 mutex_unlock(&indio_dev->mlock);
77 return 0;
80 static int vf610_get_conversion_mode(struct iio_dev *indio_dev,
81 const struct iio_chan_spec *chan)
83 struct vf610_dac *info = iio_priv(indio_dev);
85 return info->conv_mode;
88 static const char * const vf610_conv_modes[] = { "high-power", "low-power" };
90 static const struct iio_enum vf610_conversion_mode = {
91 .items = vf610_conv_modes,
92 .num_items = ARRAY_SIZE(vf610_conv_modes),
93 .get = vf610_get_conversion_mode,
94 .set = vf610_set_conversion_mode,
97 static const struct iio_chan_spec_ext_info vf610_ext_info[] = {
98 IIO_ENUM("conversion_mode", IIO_SHARED_BY_DIR,
99 &vf610_conversion_mode),
103 #define VF610_DAC_CHAN(_chan_type) { \
104 .type = (_chan_type), \
105 .output = 1, \
106 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
107 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
108 .ext_info = vf610_ext_info, \
111 static const struct iio_chan_spec vf610_dac_iio_channels[] = {
112 VF610_DAC_CHAN(IIO_VOLTAGE),
115 static int vf610_read_raw(struct iio_dev *indio_dev,
116 struct iio_chan_spec const *chan,
117 int *val, int *val2,
118 long mask)
120 struct vf610_dac *info = iio_priv(indio_dev);
122 switch (mask) {
123 case IIO_CHAN_INFO_RAW:
124 *val = VF610_DAC_DAT0(readl(info->regs));
125 return IIO_VAL_INT;
126 case IIO_CHAN_INFO_SCALE:
128 * DACRFS is always 1 for valid reference and typical
129 * reference voltage as per Vybrid datasheet is 3.3V
130 * from section 9.1.2.1 of Vybrid datasheet
132 *val = 3300 /* mV */;
133 *val2 = 12;
134 return IIO_VAL_FRACTIONAL_LOG2;
136 default:
137 return -EINVAL;
141 static int vf610_write_raw(struct iio_dev *indio_dev,
142 struct iio_chan_spec const *chan,
143 int val, int val2,
144 long mask)
146 struct vf610_dac *info = iio_priv(indio_dev);
148 switch (mask) {
149 case IIO_CHAN_INFO_RAW:
150 mutex_lock(&indio_dev->mlock);
151 writel(VF610_DAC_DAT0(val), info->regs);
152 mutex_unlock(&indio_dev->mlock);
153 return 0;
155 default:
156 return -EINVAL;
160 static const struct iio_info vf610_dac_iio_info = {
161 .read_raw = &vf610_read_raw,
162 .write_raw = &vf610_write_raw,
165 static const struct of_device_id vf610_dac_match[] = {
166 { .compatible = "fsl,vf610-dac", },
167 { /* sentinel */ }
169 MODULE_DEVICE_TABLE(of, vf610_dac_match);
171 static int vf610_dac_probe(struct platform_device *pdev)
173 struct iio_dev *indio_dev;
174 struct vf610_dac *info;
175 int ret;
177 indio_dev = devm_iio_device_alloc(&pdev->dev,
178 sizeof(struct vf610_dac));
179 if (!indio_dev) {
180 dev_err(&pdev->dev, "Failed allocating iio device\n");
181 return -ENOMEM;
184 info = iio_priv(indio_dev);
185 info->dev = &pdev->dev;
187 info->regs = devm_platform_ioremap_resource(pdev, 0);
188 if (IS_ERR(info->regs))
189 return PTR_ERR(info->regs);
191 info->clk = devm_clk_get(&pdev->dev, "dac");
192 if (IS_ERR(info->clk)) {
193 dev_err(&pdev->dev, "Failed getting clock, err = %ld\n",
194 PTR_ERR(info->clk));
195 return PTR_ERR(info->clk);
198 platform_set_drvdata(pdev, indio_dev);
200 indio_dev->name = dev_name(&pdev->dev);
201 indio_dev->dev.parent = &pdev->dev;
202 indio_dev->dev.of_node = pdev->dev.of_node;
203 indio_dev->info = &vf610_dac_iio_info;
204 indio_dev->modes = INDIO_DIRECT_MODE;
205 indio_dev->channels = vf610_dac_iio_channels;
206 indio_dev->num_channels = ARRAY_SIZE(vf610_dac_iio_channels);
208 ret = clk_prepare_enable(info->clk);
209 if (ret) {
210 dev_err(&pdev->dev,
211 "Could not prepare or enable the clock\n");
212 return ret;
215 vf610_dac_init(info);
217 ret = iio_device_register(indio_dev);
218 if (ret) {
219 dev_err(&pdev->dev, "Couldn't register the device\n");
220 goto error_iio_device_register;
223 return 0;
225 error_iio_device_register:
226 vf610_dac_exit(info);
227 clk_disable_unprepare(info->clk);
229 return ret;
232 static int vf610_dac_remove(struct platform_device *pdev)
234 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
235 struct vf610_dac *info = iio_priv(indio_dev);
237 iio_device_unregister(indio_dev);
238 vf610_dac_exit(info);
239 clk_disable_unprepare(info->clk);
241 return 0;
244 #ifdef CONFIG_PM_SLEEP
245 static int vf610_dac_suspend(struct device *dev)
247 struct iio_dev *indio_dev = dev_get_drvdata(dev);
248 struct vf610_dac *info = iio_priv(indio_dev);
250 vf610_dac_exit(info);
251 clk_disable_unprepare(info->clk);
253 return 0;
256 static int vf610_dac_resume(struct device *dev)
258 struct iio_dev *indio_dev = dev_get_drvdata(dev);
259 struct vf610_dac *info = iio_priv(indio_dev);
260 int ret;
262 ret = clk_prepare_enable(info->clk);
263 if (ret)
264 return ret;
266 vf610_dac_init(info);
268 return 0;
270 #endif
272 static SIMPLE_DEV_PM_OPS(vf610_dac_pm_ops, vf610_dac_suspend, vf610_dac_resume);
274 static struct platform_driver vf610_dac_driver = {
275 .probe = vf610_dac_probe,
276 .remove = vf610_dac_remove,
277 .driver = {
278 .name = "vf610-dac",
279 .of_match_table = vf610_dac_match,
280 .pm = &vf610_dac_pm_ops,
283 module_platform_driver(vf610_dac_driver);
285 MODULE_AUTHOR("Sanchayan Maity <sanchayan.maity@toradex.com>");
286 MODULE_DESCRIPTION("Freescale VF610 DAC driver");
287 MODULE_LICENSE("GPL v2");