1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of STM32 DAC driver
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Authors: Amelie Delaunay <amelie.delaunay@st.com>
7 * Fabrice Gasnier <fabrice.gasnier@st.com>
10 #include <linux/bitfield.h>
11 #include <linux/delay.h>
12 #include <linux/iio/iio.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
18 #include "stm32-dac-core.h"
20 #define STM32_DAC_CHANNEL_1 1
21 #define STM32_DAC_CHANNEL_2 2
22 #define STM32_DAC_IS_CHAN_1(ch) ((ch) & STM32_DAC_CHANNEL_1)
24 #define STM32_DAC_AUTO_SUSPEND_DELAY_MS 2000
27 * struct stm32_dac - private data of DAC driver
28 * @common: reference to DAC common data
29 * @lock: lock to protect against potential races when reading
30 * and update CR, to keep it in sync with pm_runtime
33 struct stm32_dac_common
*common
;
37 static int stm32_dac_is_enabled(struct iio_dev
*indio_dev
, int channel
)
39 struct stm32_dac
*dac
= iio_priv(indio_dev
);
43 ret
= regmap_read(dac
->common
->regmap
, STM32_DAC_CR
, &val
);
46 if (STM32_DAC_IS_CHAN_1(channel
))
47 en
= FIELD_GET(STM32_DAC_CR_EN1
, val
);
49 en
= FIELD_GET(STM32_DAC_CR_EN2
, val
);
54 static int stm32_dac_set_enable_state(struct iio_dev
*indio_dev
, int ch
,
57 struct stm32_dac
*dac
= iio_priv(indio_dev
);
58 struct device
*dev
= indio_dev
->dev
.parent
;
59 u32 msk
= STM32_DAC_IS_CHAN_1(ch
) ? STM32_DAC_CR_EN1
: STM32_DAC_CR_EN2
;
60 u32 en
= enable
? msk
: 0;
63 /* already enabled / disabled ? */
64 mutex_lock(&dac
->lock
);
65 ret
= stm32_dac_is_enabled(indio_dev
, ch
);
66 if (ret
< 0 || enable
== !!ret
) {
67 mutex_unlock(&dac
->lock
);
68 return ret
< 0 ? ret
: 0;
72 ret
= pm_runtime_get_sync(dev
);
74 pm_runtime_put_noidle(dev
);
75 mutex_unlock(&dac
->lock
);
80 ret
= regmap_update_bits(dac
->common
->regmap
, STM32_DAC_CR
, msk
, en
);
81 mutex_unlock(&dac
->lock
);
83 dev_err(&indio_dev
->dev
, "%s failed\n", en
?
84 "Enable" : "Disable");
89 * When HFSEL is set, it is not allowed to write the DHRx register
90 * during 8 clock cycles after the ENx bit is set. It is not allowed
91 * to make software/hardware trigger during this period either.
93 if (en
&& dac
->common
->hfsel
)
97 pm_runtime_mark_last_busy(dev
);
98 pm_runtime_put_autosuspend(dev
);
105 pm_runtime_mark_last_busy(dev
);
106 pm_runtime_put_autosuspend(dev
);
112 static int stm32_dac_get_value(struct stm32_dac
*dac
, int channel
, int *val
)
116 if (STM32_DAC_IS_CHAN_1(channel
))
117 ret
= regmap_read(dac
->common
->regmap
, STM32_DAC_DOR1
, val
);
119 ret
= regmap_read(dac
->common
->regmap
, STM32_DAC_DOR2
, val
);
121 return ret
? ret
: IIO_VAL_INT
;
124 static int stm32_dac_set_value(struct stm32_dac
*dac
, int channel
, int val
)
128 if (STM32_DAC_IS_CHAN_1(channel
))
129 ret
= regmap_write(dac
->common
->regmap
, STM32_DAC_DHR12R1
, val
);
131 ret
= regmap_write(dac
->common
->regmap
, STM32_DAC_DHR12R2
, val
);
136 static int stm32_dac_read_raw(struct iio_dev
*indio_dev
,
137 struct iio_chan_spec
const *chan
,
138 int *val
, int *val2
, long mask
)
140 struct stm32_dac
*dac
= iio_priv(indio_dev
);
143 case IIO_CHAN_INFO_RAW
:
144 return stm32_dac_get_value(dac
, chan
->channel
, val
);
145 case IIO_CHAN_INFO_SCALE
:
146 *val
= dac
->common
->vref_mv
;
147 *val2
= chan
->scan_type
.realbits
;
148 return IIO_VAL_FRACTIONAL_LOG2
;
154 static int stm32_dac_write_raw(struct iio_dev
*indio_dev
,
155 struct iio_chan_spec
const *chan
,
156 int val
, int val2
, long mask
)
158 struct stm32_dac
*dac
= iio_priv(indio_dev
);
161 case IIO_CHAN_INFO_RAW
:
162 return stm32_dac_set_value(dac
, chan
->channel
, val
);
168 static int stm32_dac_debugfs_reg_access(struct iio_dev
*indio_dev
,
169 unsigned reg
, unsigned writeval
,
172 struct stm32_dac
*dac
= iio_priv(indio_dev
);
175 return regmap_write(dac
->common
->regmap
, reg
, writeval
);
177 return regmap_read(dac
->common
->regmap
, reg
, readval
);
180 static const struct iio_info stm32_dac_iio_info
= {
181 .read_raw
= stm32_dac_read_raw
,
182 .write_raw
= stm32_dac_write_raw
,
183 .debugfs_reg_access
= stm32_dac_debugfs_reg_access
,
186 static const char * const stm32_dac_powerdown_modes
[] = {
190 static int stm32_dac_get_powerdown_mode(struct iio_dev
*indio_dev
,
191 const struct iio_chan_spec
*chan
)
196 static int stm32_dac_set_powerdown_mode(struct iio_dev
*indio_dev
,
197 const struct iio_chan_spec
*chan
,
203 static ssize_t
stm32_dac_read_powerdown(struct iio_dev
*indio_dev
,
205 const struct iio_chan_spec
*chan
,
208 int ret
= stm32_dac_is_enabled(indio_dev
, chan
->channel
);
213 return sprintf(buf
, "%d\n", ret
? 0 : 1);
216 static ssize_t
stm32_dac_write_powerdown(struct iio_dev
*indio_dev
,
218 const struct iio_chan_spec
*chan
,
219 const char *buf
, size_t len
)
224 ret
= strtobool(buf
, &powerdown
);
228 ret
= stm32_dac_set_enable_state(indio_dev
, chan
->channel
, !powerdown
);
235 static const struct iio_enum stm32_dac_powerdown_mode_en
= {
236 .items
= stm32_dac_powerdown_modes
,
237 .num_items
= ARRAY_SIZE(stm32_dac_powerdown_modes
),
238 .get
= stm32_dac_get_powerdown_mode
,
239 .set
= stm32_dac_set_powerdown_mode
,
242 static const struct iio_chan_spec_ext_info stm32_dac_ext_info
[] = {
245 .read
= stm32_dac_read_powerdown
,
246 .write
= stm32_dac_write_powerdown
,
247 .shared
= IIO_SEPARATE
,
249 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &stm32_dac_powerdown_mode_en
),
250 IIO_ENUM_AVAILABLE("powerdown_mode", &stm32_dac_powerdown_mode_en
),
254 #define STM32_DAC_CHANNEL(chan, name) { \
255 .type = IIO_VOLTAGE, \
259 .info_mask_separate = \
260 BIT(IIO_CHAN_INFO_RAW) | \
261 BIT(IIO_CHAN_INFO_SCALE), \
262 /* scan_index is always 0 as num_channels is 1 */ \
268 .datasheet_name = name, \
269 .ext_info = stm32_dac_ext_info \
272 static const struct iio_chan_spec stm32_dac_channels
[] = {
273 STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_1
, "out1"),
274 STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_2
, "out2"),
277 static int stm32_dac_chan_of_init(struct iio_dev
*indio_dev
)
279 struct device_node
*np
= indio_dev
->dev
.of_node
;
284 ret
= of_property_read_u32(np
, "reg", &channel
);
286 dev_err(&indio_dev
->dev
, "Failed to read reg property\n");
290 for (i
= 0; i
< ARRAY_SIZE(stm32_dac_channels
); i
++) {
291 if (stm32_dac_channels
[i
].channel
== channel
)
294 if (i
>= ARRAY_SIZE(stm32_dac_channels
)) {
295 dev_err(&indio_dev
->dev
, "Invalid reg property\n");
299 indio_dev
->channels
= &stm32_dac_channels
[i
];
301 * Expose only one channel here, as they can be used independently,
302 * with separate trigger. Then separate IIO devices are instantiated
305 indio_dev
->num_channels
= 1;
310 static int stm32_dac_probe(struct platform_device
*pdev
)
312 struct device_node
*np
= pdev
->dev
.of_node
;
313 struct device
*dev
= &pdev
->dev
;
314 struct iio_dev
*indio_dev
;
315 struct stm32_dac
*dac
;
321 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*dac
));
324 platform_set_drvdata(pdev
, indio_dev
);
326 dac
= iio_priv(indio_dev
);
327 dac
->common
= dev_get_drvdata(pdev
->dev
.parent
);
328 indio_dev
->name
= dev_name(&pdev
->dev
);
329 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
330 indio_dev
->info
= &stm32_dac_iio_info
;
331 indio_dev
->modes
= INDIO_DIRECT_MODE
;
333 mutex_init(&dac
->lock
);
335 ret
= stm32_dac_chan_of_init(indio_dev
);
339 /* Get stm32-dac-core PM online */
340 pm_runtime_get_noresume(dev
);
341 pm_runtime_set_active(dev
);
342 pm_runtime_set_autosuspend_delay(dev
, STM32_DAC_AUTO_SUSPEND_DELAY_MS
);
343 pm_runtime_use_autosuspend(dev
);
344 pm_runtime_enable(dev
);
346 ret
= iio_device_register(indio_dev
);
350 pm_runtime_mark_last_busy(dev
);
351 pm_runtime_put_autosuspend(dev
);
356 pm_runtime_disable(dev
);
357 pm_runtime_set_suspended(dev
);
358 pm_runtime_put_noidle(dev
);
363 static int stm32_dac_remove(struct platform_device
*pdev
)
365 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
367 pm_runtime_get_sync(&pdev
->dev
);
368 iio_device_unregister(indio_dev
);
369 pm_runtime_disable(&pdev
->dev
);
370 pm_runtime_set_suspended(&pdev
->dev
);
371 pm_runtime_put_noidle(&pdev
->dev
);
376 static int __maybe_unused
stm32_dac_suspend(struct device
*dev
)
378 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
379 int channel
= indio_dev
->channels
[0].channel
;
382 /* Ensure DAC is disabled before suspend */
383 ret
= stm32_dac_is_enabled(indio_dev
, channel
);
385 return ret
< 0 ? ret
: -EBUSY
;
387 return pm_runtime_force_suspend(dev
);
390 static const struct dev_pm_ops stm32_dac_pm_ops
= {
391 SET_SYSTEM_SLEEP_PM_OPS(stm32_dac_suspend
, pm_runtime_force_resume
)
394 static const struct of_device_id stm32_dac_of_match
[] = {
395 { .compatible
= "st,stm32-dac", },
398 MODULE_DEVICE_TABLE(of
, stm32_dac_of_match
);
400 static struct platform_driver stm32_dac_driver
= {
401 .probe
= stm32_dac_probe
,
402 .remove
= stm32_dac_remove
,
405 .of_match_table
= stm32_dac_of_match
,
406 .pm
= &stm32_dac_pm_ops
,
409 module_platform_driver(stm32_dac_driver
);
411 MODULE_ALIAS("platform:stm32-dac");
412 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
413 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC driver");
414 MODULE_LICENSE("GPL v2");