2 * Freescale Vybrid vf610 DAC driver
4 * Copyright 2016 Toradex AG
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk.h>
18 #include <linux/err.h>
19 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
30 #define VF610_DACx_STATCTRL 0x20
32 #define VF610_DAC_DACEN BIT(15)
33 #define VF610_DAC_DACRFS BIT(14)
34 #define VF610_DAC_LPEN BIT(11)
36 #define VF610_DAC_DAT0(x) ((x) & 0xFFF)
38 enum vf610_conversion_mode_sel
{
39 VF610_DAC_CONV_HIGH_POWER
,
40 VF610_DAC_CONV_LOW_POWER
,
46 enum vf610_conversion_mode_sel conv_mode
;
50 static void vf610_dac_init(struct vf610_dac
*info
)
54 info
->conv_mode
= VF610_DAC_CONV_LOW_POWER
;
55 val
= VF610_DAC_DACEN
| VF610_DAC_DACRFS
|
57 writel(val
, info
->regs
+ VF610_DACx_STATCTRL
);
60 static void vf610_dac_exit(struct vf610_dac
*info
)
64 val
= readl(info
->regs
+ VF610_DACx_STATCTRL
);
65 val
&= ~VF610_DAC_DACEN
;
66 writel(val
, info
->regs
+ VF610_DACx_STATCTRL
);
69 static int vf610_set_conversion_mode(struct iio_dev
*indio_dev
,
70 const struct iio_chan_spec
*chan
,
73 struct vf610_dac
*info
= iio_priv(indio_dev
);
76 mutex_lock(&indio_dev
->mlock
);
77 info
->conv_mode
= mode
;
78 val
= readl(info
->regs
+ VF610_DACx_STATCTRL
);
80 val
|= VF610_DAC_LPEN
;
82 val
&= ~VF610_DAC_LPEN
;
83 writel(val
, info
->regs
+ VF610_DACx_STATCTRL
);
84 mutex_unlock(&indio_dev
->mlock
);
89 static int vf610_get_conversion_mode(struct iio_dev
*indio_dev
,
90 const struct iio_chan_spec
*chan
)
92 struct vf610_dac
*info
= iio_priv(indio_dev
);
94 return info
->conv_mode
;
97 static const char * const vf610_conv_modes
[] = { "high-power", "low-power" };
99 static const struct iio_enum vf610_conversion_mode
= {
100 .items
= vf610_conv_modes
,
101 .num_items
= ARRAY_SIZE(vf610_conv_modes
),
102 .get
= vf610_get_conversion_mode
,
103 .set
= vf610_set_conversion_mode
,
106 static const struct iio_chan_spec_ext_info vf610_ext_info
[] = {
107 IIO_ENUM("conversion_mode", IIO_SHARED_BY_DIR
,
108 &vf610_conversion_mode
),
112 #define VF610_DAC_CHAN(_chan_type) { \
113 .type = (_chan_type), \
115 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
116 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
117 .ext_info = vf610_ext_info, \
120 static const struct iio_chan_spec vf610_dac_iio_channels
[] = {
121 VF610_DAC_CHAN(IIO_VOLTAGE
),
124 static int vf610_read_raw(struct iio_dev
*indio_dev
,
125 struct iio_chan_spec
const *chan
,
129 struct vf610_dac
*info
= iio_priv(indio_dev
);
132 case IIO_CHAN_INFO_RAW
:
133 *val
= VF610_DAC_DAT0(readl(info
->regs
));
135 case IIO_CHAN_INFO_SCALE
:
137 * DACRFS is always 1 for valid reference and typical
138 * reference voltage as per Vybrid datasheet is 3.3V
139 * from section 9.1.2.1 of Vybrid datasheet
141 *val
= 3300 /* mV */;
143 return IIO_VAL_FRACTIONAL_LOG2
;
150 static int vf610_write_raw(struct iio_dev
*indio_dev
,
151 struct iio_chan_spec
const *chan
,
155 struct vf610_dac
*info
= iio_priv(indio_dev
);
158 case IIO_CHAN_INFO_RAW
:
159 mutex_lock(&indio_dev
->mlock
);
160 writel(VF610_DAC_DAT0(val
), info
->regs
);
161 mutex_unlock(&indio_dev
->mlock
);
169 static const struct iio_info vf610_dac_iio_info
= {
170 .driver_module
= THIS_MODULE
,
171 .read_raw
= &vf610_read_raw
,
172 .write_raw
= &vf610_write_raw
,
175 static const struct of_device_id vf610_dac_match
[] = {
176 { .compatible
= "fsl,vf610-dac", },
179 MODULE_DEVICE_TABLE(of
, vf610_dac_match
);
181 static int vf610_dac_probe(struct platform_device
*pdev
)
183 struct iio_dev
*indio_dev
;
184 struct vf610_dac
*info
;
185 struct resource
*mem
;
188 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
189 sizeof(struct vf610_dac
));
191 dev_err(&pdev
->dev
, "Failed allocating iio device\n");
195 info
= iio_priv(indio_dev
);
196 info
->dev
= &pdev
->dev
;
198 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
199 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
200 if (IS_ERR(info
->regs
))
201 return PTR_ERR(info
->regs
);
203 info
->clk
= devm_clk_get(&pdev
->dev
, "dac");
204 if (IS_ERR(info
->clk
)) {
205 dev_err(&pdev
->dev
, "Failed getting clock, err = %ld\n",
207 return PTR_ERR(info
->clk
);
210 platform_set_drvdata(pdev
, indio_dev
);
212 indio_dev
->name
= dev_name(&pdev
->dev
);
213 indio_dev
->dev
.parent
= &pdev
->dev
;
214 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
215 indio_dev
->info
= &vf610_dac_iio_info
;
216 indio_dev
->modes
= INDIO_DIRECT_MODE
;
217 indio_dev
->channels
= vf610_dac_iio_channels
;
218 indio_dev
->num_channels
= ARRAY_SIZE(vf610_dac_iio_channels
);
220 ret
= clk_prepare_enable(info
->clk
);
223 "Could not prepare or enable the clock\n");
227 vf610_dac_init(info
);
229 ret
= iio_device_register(indio_dev
);
231 dev_err(&pdev
->dev
, "Couldn't register the device\n");
232 goto error_iio_device_register
;
237 error_iio_device_register
:
238 clk_disable_unprepare(info
->clk
);
243 static int vf610_dac_remove(struct platform_device
*pdev
)
245 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
246 struct vf610_dac
*info
= iio_priv(indio_dev
);
248 iio_device_unregister(indio_dev
);
249 vf610_dac_exit(info
);
250 clk_disable_unprepare(info
->clk
);
255 #ifdef CONFIG_PM_SLEEP
256 static int vf610_dac_suspend(struct device
*dev
)
258 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
259 struct vf610_dac
*info
= iio_priv(indio_dev
);
261 vf610_dac_exit(info
);
262 clk_disable_unprepare(info
->clk
);
267 static int vf610_dac_resume(struct device
*dev
)
269 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
270 struct vf610_dac
*info
= iio_priv(indio_dev
);
273 ret
= clk_prepare_enable(info
->clk
);
277 vf610_dac_init(info
);
283 static SIMPLE_DEV_PM_OPS(vf610_dac_pm_ops
, vf610_dac_suspend
, vf610_dac_resume
);
285 static struct platform_driver vf610_dac_driver
= {
286 .probe
= vf610_dac_probe
,
287 .remove
= vf610_dac_remove
,
290 .of_match_table
= vf610_dac_match
,
291 .pm
= &vf610_dac_pm_ops
,
294 module_platform_driver(vf610_dac_driver
);
296 MODULE_AUTHOR("Sanchayan Maity <sanchayan.maity@toradex.com>");
297 MODULE_DESCRIPTION("Freescale VF610 DAC driver");
298 MODULE_LICENSE("GPL v2");