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
31 struct stm32_dac_common
*common
;
34 static int stm32_dac_is_enabled(struct iio_dev
*indio_dev
, int channel
)
36 struct stm32_dac
*dac
= iio_priv(indio_dev
);
40 ret
= regmap_read(dac
->common
->regmap
, STM32_DAC_CR
, &val
);
43 if (STM32_DAC_IS_CHAN_1(channel
))
44 en
= FIELD_GET(STM32_DAC_CR_EN1
, val
);
46 en
= FIELD_GET(STM32_DAC_CR_EN2
, val
);
51 static int stm32_dac_set_enable_state(struct iio_dev
*indio_dev
, int ch
,
54 struct stm32_dac
*dac
= iio_priv(indio_dev
);
55 struct device
*dev
= indio_dev
->dev
.parent
;
56 u32 msk
= STM32_DAC_IS_CHAN_1(ch
) ? STM32_DAC_CR_EN1
: STM32_DAC_CR_EN2
;
57 u32 en
= enable
? msk
: 0;
60 /* already enabled / disabled ? */
61 mutex_lock(&indio_dev
->mlock
);
62 ret
= stm32_dac_is_enabled(indio_dev
, ch
);
63 if (ret
< 0 || enable
== !!ret
) {
64 mutex_unlock(&indio_dev
->mlock
);
65 return ret
< 0 ? ret
: 0;
69 ret
= pm_runtime_get_sync(dev
);
71 pm_runtime_put_noidle(dev
);
72 mutex_unlock(&indio_dev
->mlock
);
77 ret
= regmap_update_bits(dac
->common
->regmap
, STM32_DAC_CR
, msk
, en
);
78 mutex_unlock(&indio_dev
->mlock
);
80 dev_err(&indio_dev
->dev
, "%s failed\n", en
?
81 "Enable" : "Disable");
86 * When HFSEL is set, it is not allowed to write the DHRx register
87 * during 8 clock cycles after the ENx bit is set. It is not allowed
88 * to make software/hardware trigger during this period either.
90 if (en
&& dac
->common
->hfsel
)
94 pm_runtime_mark_last_busy(dev
);
95 pm_runtime_put_autosuspend(dev
);
102 pm_runtime_mark_last_busy(dev
);
103 pm_runtime_put_autosuspend(dev
);
109 static int stm32_dac_get_value(struct stm32_dac
*dac
, int channel
, int *val
)
113 if (STM32_DAC_IS_CHAN_1(channel
))
114 ret
= regmap_read(dac
->common
->regmap
, STM32_DAC_DOR1
, val
);
116 ret
= regmap_read(dac
->common
->regmap
, STM32_DAC_DOR2
, val
);
118 return ret
? ret
: IIO_VAL_INT
;
121 static int stm32_dac_set_value(struct stm32_dac
*dac
, int channel
, int val
)
125 if (STM32_DAC_IS_CHAN_1(channel
))
126 ret
= regmap_write(dac
->common
->regmap
, STM32_DAC_DHR12R1
, val
);
128 ret
= regmap_write(dac
->common
->regmap
, STM32_DAC_DHR12R2
, val
);
133 static int stm32_dac_read_raw(struct iio_dev
*indio_dev
,
134 struct iio_chan_spec
const *chan
,
135 int *val
, int *val2
, long mask
)
137 struct stm32_dac
*dac
= iio_priv(indio_dev
);
140 case IIO_CHAN_INFO_RAW
:
141 return stm32_dac_get_value(dac
, chan
->channel
, val
);
142 case IIO_CHAN_INFO_SCALE
:
143 *val
= dac
->common
->vref_mv
;
144 *val2
= chan
->scan_type
.realbits
;
145 return IIO_VAL_FRACTIONAL_LOG2
;
151 static int stm32_dac_write_raw(struct iio_dev
*indio_dev
,
152 struct iio_chan_spec
const *chan
,
153 int val
, int val2
, long mask
)
155 struct stm32_dac
*dac
= iio_priv(indio_dev
);
158 case IIO_CHAN_INFO_RAW
:
159 return stm32_dac_set_value(dac
, chan
->channel
, val
);
165 static int stm32_dac_debugfs_reg_access(struct iio_dev
*indio_dev
,
166 unsigned reg
, unsigned writeval
,
169 struct stm32_dac
*dac
= iio_priv(indio_dev
);
172 return regmap_write(dac
->common
->regmap
, reg
, writeval
);
174 return regmap_read(dac
->common
->regmap
, reg
, readval
);
177 static const struct iio_info stm32_dac_iio_info
= {
178 .read_raw
= stm32_dac_read_raw
,
179 .write_raw
= stm32_dac_write_raw
,
180 .debugfs_reg_access
= stm32_dac_debugfs_reg_access
,
183 static const char * const stm32_dac_powerdown_modes
[] = {
187 static int stm32_dac_get_powerdown_mode(struct iio_dev
*indio_dev
,
188 const struct iio_chan_spec
*chan
)
193 static int stm32_dac_set_powerdown_mode(struct iio_dev
*indio_dev
,
194 const struct iio_chan_spec
*chan
,
200 static ssize_t
stm32_dac_read_powerdown(struct iio_dev
*indio_dev
,
202 const struct iio_chan_spec
*chan
,
205 int ret
= stm32_dac_is_enabled(indio_dev
, chan
->channel
);
210 return sprintf(buf
, "%d\n", ret
? 0 : 1);
213 static ssize_t
stm32_dac_write_powerdown(struct iio_dev
*indio_dev
,
215 const struct iio_chan_spec
*chan
,
216 const char *buf
, size_t len
)
221 ret
= strtobool(buf
, &powerdown
);
225 ret
= stm32_dac_set_enable_state(indio_dev
, chan
->channel
, !powerdown
);
232 static const struct iio_enum stm32_dac_powerdown_mode_en
= {
233 .items
= stm32_dac_powerdown_modes
,
234 .num_items
= ARRAY_SIZE(stm32_dac_powerdown_modes
),
235 .get
= stm32_dac_get_powerdown_mode
,
236 .set
= stm32_dac_set_powerdown_mode
,
239 static const struct iio_chan_spec_ext_info stm32_dac_ext_info
[] = {
242 .read
= stm32_dac_read_powerdown
,
243 .write
= stm32_dac_write_powerdown
,
244 .shared
= IIO_SEPARATE
,
246 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &stm32_dac_powerdown_mode_en
),
247 IIO_ENUM_AVAILABLE("powerdown_mode", &stm32_dac_powerdown_mode_en
),
251 #define STM32_DAC_CHANNEL(chan, name) { \
252 .type = IIO_VOLTAGE, \
256 .info_mask_separate = \
257 BIT(IIO_CHAN_INFO_RAW) | \
258 BIT(IIO_CHAN_INFO_SCALE), \
259 /* scan_index is always 0 as num_channels is 1 */ \
265 .datasheet_name = name, \
266 .ext_info = stm32_dac_ext_info \
269 static const struct iio_chan_spec stm32_dac_channels
[] = {
270 STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_1
, "out1"),
271 STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_2
, "out2"),
274 static int stm32_dac_chan_of_init(struct iio_dev
*indio_dev
)
276 struct device_node
*np
= indio_dev
->dev
.of_node
;
281 ret
= of_property_read_u32(np
, "reg", &channel
);
283 dev_err(&indio_dev
->dev
, "Failed to read reg property\n");
287 for (i
= 0; i
< ARRAY_SIZE(stm32_dac_channels
); i
++) {
288 if (stm32_dac_channels
[i
].channel
== channel
)
291 if (i
>= ARRAY_SIZE(stm32_dac_channels
)) {
292 dev_err(&indio_dev
->dev
, "Invalid reg property\n");
296 indio_dev
->channels
= &stm32_dac_channels
[i
];
298 * Expose only one channel here, as they can be used independently,
299 * with separate trigger. Then separate IIO devices are instantiated
302 indio_dev
->num_channels
= 1;
307 static int stm32_dac_probe(struct platform_device
*pdev
)
309 struct device_node
*np
= pdev
->dev
.of_node
;
310 struct device
*dev
= &pdev
->dev
;
311 struct iio_dev
*indio_dev
;
312 struct stm32_dac
*dac
;
318 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*dac
));
321 platform_set_drvdata(pdev
, indio_dev
);
323 dac
= iio_priv(indio_dev
);
324 dac
->common
= dev_get_drvdata(pdev
->dev
.parent
);
325 indio_dev
->name
= dev_name(&pdev
->dev
);
326 indio_dev
->dev
.parent
= &pdev
->dev
;
327 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
328 indio_dev
->info
= &stm32_dac_iio_info
;
329 indio_dev
->modes
= INDIO_DIRECT_MODE
;
331 ret
= stm32_dac_chan_of_init(indio_dev
);
335 /* Get stm32-dac-core PM online */
336 pm_runtime_get_noresume(dev
);
337 pm_runtime_set_active(dev
);
338 pm_runtime_set_autosuspend_delay(dev
, STM32_DAC_AUTO_SUSPEND_DELAY_MS
);
339 pm_runtime_use_autosuspend(dev
);
340 pm_runtime_enable(dev
);
342 ret
= iio_device_register(indio_dev
);
346 pm_runtime_mark_last_busy(dev
);
347 pm_runtime_put_autosuspend(dev
);
352 pm_runtime_disable(dev
);
353 pm_runtime_set_suspended(dev
);
354 pm_runtime_put_noidle(dev
);
359 static int stm32_dac_remove(struct platform_device
*pdev
)
361 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
363 pm_runtime_get_sync(&pdev
->dev
);
364 iio_device_unregister(indio_dev
);
365 pm_runtime_disable(&pdev
->dev
);
366 pm_runtime_set_suspended(&pdev
->dev
);
367 pm_runtime_put_noidle(&pdev
->dev
);
372 static int __maybe_unused
stm32_dac_suspend(struct device
*dev
)
374 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
375 int channel
= indio_dev
->channels
[0].channel
;
378 /* Ensure DAC is disabled before suspend */
379 ret
= stm32_dac_is_enabled(indio_dev
, channel
);
381 return ret
< 0 ? ret
: -EBUSY
;
383 return pm_runtime_force_suspend(dev
);
386 static const struct dev_pm_ops stm32_dac_pm_ops
= {
387 SET_SYSTEM_SLEEP_PM_OPS(stm32_dac_suspend
, pm_runtime_force_resume
)
390 static const struct of_device_id stm32_dac_of_match
[] = {
391 { .compatible
= "st,stm32-dac", },
394 MODULE_DEVICE_TABLE(of
, stm32_dac_of_match
);
396 static struct platform_driver stm32_dac_driver
= {
397 .probe
= stm32_dac_probe
,
398 .remove
= stm32_dac_remove
,
401 .of_match_table
= stm32_dac_of_match
,
402 .pm
= &stm32_dac_pm_ops
,
405 module_platform_driver(stm32_dac_driver
);
407 MODULE_ALIAS("platform:stm32-dac");
408 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
409 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC driver");
410 MODULE_LICENSE("GPL v2");