1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale Vybrid vf610 DAC driver
5 * Copyright 2016 Toradex AG
10 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/slab.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
22 #define VF610_DACx_STATCTRL 0x20
24 #define VF610_DAC_DACEN BIT(15)
25 #define VF610_DAC_DACRFS BIT(14)
26 #define VF610_DAC_LPEN BIT(11)
28 #define VF610_DAC_DAT0(x) ((x) & 0xFFF)
30 enum vf610_conversion_mode_sel
{
31 VF610_DAC_CONV_HIGH_POWER
,
32 VF610_DAC_CONV_LOW_POWER
,
38 enum vf610_conversion_mode_sel conv_mode
;
43 static void vf610_dac_init(struct vf610_dac
*info
)
47 info
->conv_mode
= VF610_DAC_CONV_LOW_POWER
;
48 val
= VF610_DAC_DACEN
| VF610_DAC_DACRFS
|
50 writel(val
, info
->regs
+ VF610_DACx_STATCTRL
);
53 static void vf610_dac_exit(struct vf610_dac
*info
)
57 val
= readl(info
->regs
+ VF610_DACx_STATCTRL
);
58 val
&= ~VF610_DAC_DACEN
;
59 writel(val
, info
->regs
+ VF610_DACx_STATCTRL
);
62 static int vf610_set_conversion_mode(struct iio_dev
*indio_dev
,
63 const struct iio_chan_spec
*chan
,
66 struct vf610_dac
*info
= iio_priv(indio_dev
);
69 mutex_lock(&info
->lock
);
70 info
->conv_mode
= mode
;
71 val
= readl(info
->regs
+ VF610_DACx_STATCTRL
);
73 val
|= VF610_DAC_LPEN
;
75 val
&= ~VF610_DAC_LPEN
;
76 writel(val
, info
->regs
+ VF610_DACx_STATCTRL
);
77 mutex_unlock(&info
->lock
);
82 static int vf610_get_conversion_mode(struct iio_dev
*indio_dev
,
83 const struct iio_chan_spec
*chan
)
85 struct vf610_dac
*info
= iio_priv(indio_dev
);
87 return info
->conv_mode
;
90 static const char * const vf610_conv_modes
[] = { "high-power", "low-power" };
92 static const struct iio_enum vf610_conversion_mode
= {
93 .items
= vf610_conv_modes
,
94 .num_items
= ARRAY_SIZE(vf610_conv_modes
),
95 .get
= vf610_get_conversion_mode
,
96 .set
= vf610_set_conversion_mode
,
99 static const struct iio_chan_spec_ext_info vf610_ext_info
[] = {
100 IIO_ENUM("conversion_mode", IIO_SHARED_BY_DIR
,
101 &vf610_conversion_mode
),
105 #define VF610_DAC_CHAN(_chan_type) { \
106 .type = (_chan_type), \
108 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
109 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
110 .ext_info = vf610_ext_info, \
113 static const struct iio_chan_spec vf610_dac_iio_channels
[] = {
114 VF610_DAC_CHAN(IIO_VOLTAGE
),
117 static int vf610_read_raw(struct iio_dev
*indio_dev
,
118 struct iio_chan_spec
const *chan
,
122 struct vf610_dac
*info
= iio_priv(indio_dev
);
125 case IIO_CHAN_INFO_RAW
:
126 *val
= VF610_DAC_DAT0(readl(info
->regs
));
128 case IIO_CHAN_INFO_SCALE
:
130 * DACRFS is always 1 for valid reference and typical
131 * reference voltage as per Vybrid datasheet is 3.3V
132 * from section 9.1.2.1 of Vybrid datasheet
134 *val
= 3300 /* mV */;
136 return IIO_VAL_FRACTIONAL_LOG2
;
143 static int vf610_write_raw(struct iio_dev
*indio_dev
,
144 struct iio_chan_spec
const *chan
,
148 struct vf610_dac
*info
= iio_priv(indio_dev
);
151 case IIO_CHAN_INFO_RAW
:
152 mutex_lock(&info
->lock
);
153 writel(VF610_DAC_DAT0(val
), info
->regs
);
154 mutex_unlock(&info
->lock
);
162 static const struct iio_info vf610_dac_iio_info
= {
163 .read_raw
= &vf610_read_raw
,
164 .write_raw
= &vf610_write_raw
,
167 static const struct of_device_id vf610_dac_match
[] = {
168 { .compatible
= "fsl,vf610-dac", },
171 MODULE_DEVICE_TABLE(of
, vf610_dac_match
);
173 static int vf610_dac_probe(struct platform_device
*pdev
)
175 struct iio_dev
*indio_dev
;
176 struct vf610_dac
*info
;
179 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
180 sizeof(struct vf610_dac
));
182 dev_err(&pdev
->dev
, "Failed allocating iio device\n");
186 info
= iio_priv(indio_dev
);
187 info
->dev
= &pdev
->dev
;
189 info
->regs
= devm_platform_ioremap_resource(pdev
, 0);
190 if (IS_ERR(info
->regs
))
191 return PTR_ERR(info
->regs
);
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",
197 return PTR_ERR(info
->clk
);
200 platform_set_drvdata(pdev
, indio_dev
);
202 indio_dev
->name
= dev_name(&pdev
->dev
);
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 mutex_init(&info
->lock
);
210 ret
= clk_prepare_enable(info
->clk
);
213 "Could not prepare or enable the clock\n");
217 vf610_dac_init(info
);
219 ret
= iio_device_register(indio_dev
);
221 dev_err(&pdev
->dev
, "Couldn't register the device\n");
222 goto error_iio_device_register
;
227 error_iio_device_register
:
228 vf610_dac_exit(info
);
229 clk_disable_unprepare(info
->clk
);
234 static void vf610_dac_remove(struct platform_device
*pdev
)
236 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
237 struct vf610_dac
*info
= iio_priv(indio_dev
);
239 iio_device_unregister(indio_dev
);
240 vf610_dac_exit(info
);
241 clk_disable_unprepare(info
->clk
);
244 static int vf610_dac_suspend(struct device
*dev
)
246 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
247 struct vf610_dac
*info
= iio_priv(indio_dev
);
249 vf610_dac_exit(info
);
250 clk_disable_unprepare(info
->clk
);
255 static int vf610_dac_resume(struct device
*dev
)
257 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
258 struct vf610_dac
*info
= iio_priv(indio_dev
);
261 ret
= clk_prepare_enable(info
->clk
);
265 vf610_dac_init(info
);
270 static DEFINE_SIMPLE_DEV_PM_OPS(vf610_dac_pm_ops
, vf610_dac_suspend
,
273 static struct platform_driver vf610_dac_driver
= {
274 .probe
= vf610_dac_probe
,
275 .remove
= vf610_dac_remove
,
278 .of_match_table
= vf610_dac_match
,
279 .pm
= pm_sleep_ptr(&vf610_dac_pm_ops
),
282 module_platform_driver(vf610_dac_driver
);
284 MODULE_AUTHOR("Sanchayan Maity <sanchayan.maity@toradex.com>");
285 MODULE_DESCRIPTION("Freescale VF610 DAC driver");
286 MODULE_LICENSE("GPL v2");