This source file includes following definitions.
- vf610_dac_init
- vf610_dac_exit
- vf610_set_conversion_mode
- vf610_get_conversion_mode
- vf610_read_raw
- vf610_write_raw
- vf610_dac_probe
- vf610_dac_remove
- vf610_dac_suspend
- vf610_dac_resume
1
2
3
4
5
6
7
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>
17
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20
21 #define VF610_DACx_STATCTRL 0x20
22
23 #define VF610_DAC_DACEN BIT(15)
24 #define VF610_DAC_DACRFS BIT(14)
25 #define VF610_DAC_LPEN BIT(11)
26
27 #define VF610_DAC_DAT0(x) ((x) & 0xFFF)
28
29 enum vf610_conversion_mode_sel {
30 VF610_DAC_CONV_HIGH_POWER,
31 VF610_DAC_CONV_LOW_POWER,
32 };
33
34 struct vf610_dac {
35 struct clk *clk;
36 struct device *dev;
37 enum vf610_conversion_mode_sel conv_mode;
38 void __iomem *regs;
39 };
40
41 static void vf610_dac_init(struct vf610_dac *info)
42 {
43 int val;
44
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);
49 }
50
51 static void vf610_dac_exit(struct vf610_dac *info)
52 {
53 int val;
54
55 val = readl(info->regs + VF610_DACx_STATCTRL);
56 val &= ~VF610_DAC_DACEN;
57 writel(val, info->regs + VF610_DACx_STATCTRL);
58 }
59
60 static int vf610_set_conversion_mode(struct iio_dev *indio_dev,
61 const struct iio_chan_spec *chan,
62 unsigned int mode)
63 {
64 struct vf610_dac *info = iio_priv(indio_dev);
65 int val;
66
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);
76
77 return 0;
78 }
79
80 static int vf610_get_conversion_mode(struct iio_dev *indio_dev,
81 const struct iio_chan_spec *chan)
82 {
83 struct vf610_dac *info = iio_priv(indio_dev);
84
85 return info->conv_mode;
86 }
87
88 static const char * const vf610_conv_modes[] = { "high-power", "low-power" };
89
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,
95 };
96
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),
100 {},
101 };
102
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, \
109 }
110
111 static const struct iio_chan_spec vf610_dac_iio_channels[] = {
112 VF610_DAC_CHAN(IIO_VOLTAGE),
113 };
114
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)
119 {
120 struct vf610_dac *info = iio_priv(indio_dev);
121
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:
127
128
129
130
131
132 *val = 3300 ;
133 *val2 = 12;
134 return IIO_VAL_FRACTIONAL_LOG2;
135
136 default:
137 return -EINVAL;
138 }
139 }
140
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)
145 {
146 struct vf610_dac *info = iio_priv(indio_dev);
147
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;
154
155 default:
156 return -EINVAL;
157 }
158 }
159
160 static const struct iio_info vf610_dac_iio_info = {
161 .read_raw = &vf610_read_raw,
162 .write_raw = &vf610_write_raw,
163 };
164
165 static const struct of_device_id vf610_dac_match[] = {
166 { .compatible = "fsl,vf610-dac", },
167 { }
168 };
169 MODULE_DEVICE_TABLE(of, vf610_dac_match);
170
171 static int vf610_dac_probe(struct platform_device *pdev)
172 {
173 struct iio_dev *indio_dev;
174 struct vf610_dac *info;
175 struct resource *mem;
176 int ret;
177
178 indio_dev = devm_iio_device_alloc(&pdev->dev,
179 sizeof(struct vf610_dac));
180 if (!indio_dev) {
181 dev_err(&pdev->dev, "Failed allocating iio device\n");
182 return -ENOMEM;
183 }
184
185 info = iio_priv(indio_dev);
186 info->dev = &pdev->dev;
187
188 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189 info->regs = devm_ioremap_resource(&pdev->dev, mem);
190 if (IS_ERR(info->regs))
191 return PTR_ERR(info->regs);
192
193 info->clk = devm_clk_get(&pdev->dev, "dac");
194 if (IS_ERR(info->clk)) {
195 dev_err(&pdev->dev, "Failed getting clock, err = %ld\n",
196 PTR_ERR(info->clk));
197 return PTR_ERR(info->clk);
198 }
199
200 platform_set_drvdata(pdev, indio_dev);
201
202 indio_dev->name = dev_name(&pdev->dev);
203 indio_dev->dev.parent = &pdev->dev;
204 indio_dev->dev.of_node = pdev->dev.of_node;
205 indio_dev->info = &vf610_dac_iio_info;
206 indio_dev->modes = INDIO_DIRECT_MODE;
207 indio_dev->channels = vf610_dac_iio_channels;
208 indio_dev->num_channels = ARRAY_SIZE(vf610_dac_iio_channels);
209
210 ret = clk_prepare_enable(info->clk);
211 if (ret) {
212 dev_err(&pdev->dev,
213 "Could not prepare or enable the clock\n");
214 return ret;
215 }
216
217 vf610_dac_init(info);
218
219 ret = iio_device_register(indio_dev);
220 if (ret) {
221 dev_err(&pdev->dev, "Couldn't register the device\n");
222 goto error_iio_device_register;
223 }
224
225 return 0;
226
227 error_iio_device_register:
228 vf610_dac_exit(info);
229 clk_disable_unprepare(info->clk);
230
231 return ret;
232 }
233
234 static int vf610_dac_remove(struct platform_device *pdev)
235 {
236 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
237 struct vf610_dac *info = iio_priv(indio_dev);
238
239 iio_device_unregister(indio_dev);
240 vf610_dac_exit(info);
241 clk_disable_unprepare(info->clk);
242
243 return 0;
244 }
245
246 #ifdef CONFIG_PM_SLEEP
247 static int vf610_dac_suspend(struct device *dev)
248 {
249 struct iio_dev *indio_dev = dev_get_drvdata(dev);
250 struct vf610_dac *info = iio_priv(indio_dev);
251
252 vf610_dac_exit(info);
253 clk_disable_unprepare(info->clk);
254
255 return 0;
256 }
257
258 static int vf610_dac_resume(struct device *dev)
259 {
260 struct iio_dev *indio_dev = dev_get_drvdata(dev);
261 struct vf610_dac *info = iio_priv(indio_dev);
262 int ret;
263
264 ret = clk_prepare_enable(info->clk);
265 if (ret)
266 return ret;
267
268 vf610_dac_init(info);
269
270 return 0;
271 }
272 #endif
273
274 static SIMPLE_DEV_PM_OPS(vf610_dac_pm_ops, vf610_dac_suspend, vf610_dac_resume);
275
276 static struct platform_driver vf610_dac_driver = {
277 .probe = vf610_dac_probe,
278 .remove = vf610_dac_remove,
279 .driver = {
280 .name = "vf610-dac",
281 .of_match_table = vf610_dac_match,
282 .pm = &vf610_dac_pm_ops,
283 },
284 };
285 module_platform_driver(vf610_dac_driver);
286
287 MODULE_AUTHOR("Sanchayan Maity <sanchayan.maity@toradex.com>");
288 MODULE_DESCRIPTION("Freescale VF610 DAC driver");
289 MODULE_LICENSE("GPL v2");