xref: /linux/drivers/iio/dac/stm32-dac.c (revision 85a32a0ae2212ccbf8551993e494d68dbfc059ef)
16e93e261SBenjamin Gaignard // SPDX-License-Identifier: GPL-2.0
24d4b3052SFabrice Gasnier /*
34d4b3052SFabrice Gasnier  * This file is part of STM32 DAC driver
44d4b3052SFabrice Gasnier  *
54d4b3052SFabrice Gasnier  * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
64d4b3052SFabrice Gasnier  * Authors: Amelie Delaunay <amelie.delaunay@st.com>
74d4b3052SFabrice Gasnier  *	    Fabrice Gasnier <fabrice.gasnier@st.com>
84d4b3052SFabrice Gasnier  */
94d4b3052SFabrice Gasnier 
104d4b3052SFabrice Gasnier #include <linux/bitfield.h>
114d4b3052SFabrice Gasnier #include <linux/delay.h>
124d4b3052SFabrice Gasnier #include <linux/iio/iio.h>
134d4b3052SFabrice Gasnier #include <linux/kernel.h>
148c337436SAndy Shevchenko #include <linux/kstrtox.h>
154d4b3052SFabrice Gasnier #include <linux/module.h>
162a53b91cSNuno Sá #include <linux/mod_devicetable.h>
172a53b91cSNuno Sá #include <linux/of.h>
184d4b3052SFabrice Gasnier #include <linux/platform_device.h>
199d8e91d9SFabrice Gasnier #include <linux/pm_runtime.h>
208c337436SAndy Shevchenko #include <linux/string_choices.h>
214d4b3052SFabrice Gasnier 
224d4b3052SFabrice Gasnier #include "stm32-dac-core.h"
234d4b3052SFabrice Gasnier 
244d4b3052SFabrice Gasnier #define STM32_DAC_CHANNEL_1		1
254d4b3052SFabrice Gasnier #define STM32_DAC_CHANNEL_2		2
264d4b3052SFabrice Gasnier #define STM32_DAC_IS_CHAN_1(ch)		((ch) & STM32_DAC_CHANNEL_1)
274d4b3052SFabrice Gasnier 
289d8e91d9SFabrice Gasnier #define STM32_DAC_AUTO_SUSPEND_DELAY_MS	2000
299d8e91d9SFabrice Gasnier 
304d4b3052SFabrice Gasnier /**
314d4b3052SFabrice Gasnier  * struct stm32_dac - private data of DAC driver
324d4b3052SFabrice Gasnier  * @common:		reference to DAC common data
332544ea7bSSergiu Cuciurean  * @lock:		lock to protect against potential races when reading
342544ea7bSSergiu Cuciurean  *			and update CR, to keep it in sync with pm_runtime
354d4b3052SFabrice Gasnier  */
364d4b3052SFabrice Gasnier struct stm32_dac {
374d4b3052SFabrice Gasnier 	struct stm32_dac_common *common;
382544ea7bSSergiu Cuciurean 	struct mutex		lock;
394d4b3052SFabrice Gasnier };
404d4b3052SFabrice Gasnier 
414d4b3052SFabrice Gasnier static int stm32_dac_is_enabled(struct iio_dev *indio_dev, int channel)
424d4b3052SFabrice Gasnier {
434d4b3052SFabrice Gasnier 	struct stm32_dac *dac = iio_priv(indio_dev);
444d4b3052SFabrice Gasnier 	u32 en, val;
454d4b3052SFabrice Gasnier 	int ret;
464d4b3052SFabrice Gasnier 
474d4b3052SFabrice Gasnier 	ret = regmap_read(dac->common->regmap, STM32_DAC_CR, &val);
484d4b3052SFabrice Gasnier 	if (ret < 0)
494d4b3052SFabrice Gasnier 		return ret;
504d4b3052SFabrice Gasnier 	if (STM32_DAC_IS_CHAN_1(channel))
514d4b3052SFabrice Gasnier 		en = FIELD_GET(STM32_DAC_CR_EN1, val);
524d4b3052SFabrice Gasnier 	else
534d4b3052SFabrice Gasnier 		en = FIELD_GET(STM32_DAC_CR_EN2, val);
544d4b3052SFabrice Gasnier 
554d4b3052SFabrice Gasnier 	return !!en;
564d4b3052SFabrice Gasnier }
574d4b3052SFabrice Gasnier 
584d4b3052SFabrice Gasnier static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch,
594d4b3052SFabrice Gasnier 				      bool enable)
604d4b3052SFabrice Gasnier {
614d4b3052SFabrice Gasnier 	struct stm32_dac *dac = iio_priv(indio_dev);
629d8e91d9SFabrice Gasnier 	struct device *dev = indio_dev->dev.parent;
634d4b3052SFabrice Gasnier 	u32 msk = STM32_DAC_IS_CHAN_1(ch) ? STM32_DAC_CR_EN1 : STM32_DAC_CR_EN2;
644d4b3052SFabrice Gasnier 	u32 en = enable ? msk : 0;
654d4b3052SFabrice Gasnier 	int ret;
664d4b3052SFabrice Gasnier 
679d8e91d9SFabrice Gasnier 	/* already enabled / disabled ? */
682544ea7bSSergiu Cuciurean 	mutex_lock(&dac->lock);
699d8e91d9SFabrice Gasnier 	ret = stm32_dac_is_enabled(indio_dev, ch);
709d8e91d9SFabrice Gasnier 	if (ret < 0 || enable == !!ret) {
712544ea7bSSergiu Cuciurean 		mutex_unlock(&dac->lock);
729d8e91d9SFabrice Gasnier 		return ret < 0 ? ret : 0;
739d8e91d9SFabrice Gasnier 	}
749d8e91d9SFabrice Gasnier 
759d8e91d9SFabrice Gasnier 	if (enable) {
7654e81f68SJonathan Cameron 		ret = pm_runtime_resume_and_get(dev);
779d8e91d9SFabrice Gasnier 		if (ret < 0) {
782544ea7bSSergiu Cuciurean 			mutex_unlock(&dac->lock);
799d8e91d9SFabrice Gasnier 			return ret;
809d8e91d9SFabrice Gasnier 		}
819d8e91d9SFabrice Gasnier 	}
829d8e91d9SFabrice Gasnier 
834d4b3052SFabrice Gasnier 	ret = regmap_update_bits(dac->common->regmap, STM32_DAC_CR, msk, en);
842544ea7bSSergiu Cuciurean 	mutex_unlock(&dac->lock);
854d4b3052SFabrice Gasnier 	if (ret < 0) {
86f7108bcbSAndy Shevchenko 		dev_err(&indio_dev->dev, "%s failed\n", str_enable_disable(en));
879d8e91d9SFabrice Gasnier 		goto err_put_pm;
884d4b3052SFabrice Gasnier 	}
894d4b3052SFabrice Gasnier 
904d4b3052SFabrice Gasnier 	/*
914d4b3052SFabrice Gasnier 	 * When HFSEL is set, it is not allowed to write the DHRx register
924d4b3052SFabrice Gasnier 	 * during 8 clock cycles after the ENx bit is set. It is not allowed
934d4b3052SFabrice Gasnier 	 * to make software/hardware trigger during this period either.
944d4b3052SFabrice Gasnier 	 */
954d4b3052SFabrice Gasnier 	if (en && dac->common->hfsel)
964d4b3052SFabrice Gasnier 		udelay(1);
974d4b3052SFabrice Gasnier 
989d8e91d9SFabrice Gasnier 	if (!enable) {
999d8e91d9SFabrice Gasnier 		pm_runtime_mark_last_busy(dev);
1009d8e91d9SFabrice Gasnier 		pm_runtime_put_autosuspend(dev);
1019d8e91d9SFabrice Gasnier 	}
1029d8e91d9SFabrice Gasnier 
1034d4b3052SFabrice Gasnier 	return 0;
1049d8e91d9SFabrice Gasnier 
1059d8e91d9SFabrice Gasnier err_put_pm:
1069d8e91d9SFabrice Gasnier 	if (enable) {
1079d8e91d9SFabrice Gasnier 		pm_runtime_mark_last_busy(dev);
1089d8e91d9SFabrice Gasnier 		pm_runtime_put_autosuspend(dev);
1099d8e91d9SFabrice Gasnier 	}
1109d8e91d9SFabrice Gasnier 
1119d8e91d9SFabrice Gasnier 	return ret;
1124d4b3052SFabrice Gasnier }
1134d4b3052SFabrice Gasnier 
1144d4b3052SFabrice Gasnier static int stm32_dac_get_value(struct stm32_dac *dac, int channel, int *val)
1154d4b3052SFabrice Gasnier {
1164d4b3052SFabrice Gasnier 	int ret;
1174d4b3052SFabrice Gasnier 
1184d4b3052SFabrice Gasnier 	if (STM32_DAC_IS_CHAN_1(channel))
1194d4b3052SFabrice Gasnier 		ret = regmap_read(dac->common->regmap, STM32_DAC_DOR1, val);
1204d4b3052SFabrice Gasnier 	else
1214d4b3052SFabrice Gasnier 		ret = regmap_read(dac->common->regmap, STM32_DAC_DOR2, val);
1224d4b3052SFabrice Gasnier 
1234d4b3052SFabrice Gasnier 	return ret ? ret : IIO_VAL_INT;
1244d4b3052SFabrice Gasnier }
1254d4b3052SFabrice Gasnier 
1264d4b3052SFabrice Gasnier static int stm32_dac_set_value(struct stm32_dac *dac, int channel, int val)
1274d4b3052SFabrice Gasnier {
1284d4b3052SFabrice Gasnier 	int ret;
1294d4b3052SFabrice Gasnier 
1304d4b3052SFabrice Gasnier 	if (STM32_DAC_IS_CHAN_1(channel))
1314d4b3052SFabrice Gasnier 		ret = regmap_write(dac->common->regmap, STM32_DAC_DHR12R1, val);
1324d4b3052SFabrice Gasnier 	else
1334d4b3052SFabrice Gasnier 		ret = regmap_write(dac->common->regmap, STM32_DAC_DHR12R2, val);
1344d4b3052SFabrice Gasnier 
1354d4b3052SFabrice Gasnier 	return ret;
1364d4b3052SFabrice Gasnier }
1374d4b3052SFabrice Gasnier 
1384d4b3052SFabrice Gasnier static int stm32_dac_read_raw(struct iio_dev *indio_dev,
1394d4b3052SFabrice Gasnier 			      struct iio_chan_spec const *chan,
1404d4b3052SFabrice Gasnier 			      int *val, int *val2, long mask)
1414d4b3052SFabrice Gasnier {
1424d4b3052SFabrice Gasnier 	struct stm32_dac *dac = iio_priv(indio_dev);
1434d4b3052SFabrice Gasnier 
1444d4b3052SFabrice Gasnier 	switch (mask) {
1454d4b3052SFabrice Gasnier 	case IIO_CHAN_INFO_RAW:
1464d4b3052SFabrice Gasnier 		return stm32_dac_get_value(dac, chan->channel, val);
1474d4b3052SFabrice Gasnier 	case IIO_CHAN_INFO_SCALE:
1484d4b3052SFabrice Gasnier 		*val = dac->common->vref_mv;
1494d4b3052SFabrice Gasnier 		*val2 = chan->scan_type.realbits;
1504d4b3052SFabrice Gasnier 		return IIO_VAL_FRACTIONAL_LOG2;
1514d4b3052SFabrice Gasnier 	default:
1524d4b3052SFabrice Gasnier 		return -EINVAL;
1534d4b3052SFabrice Gasnier 	}
1544d4b3052SFabrice Gasnier }
1554d4b3052SFabrice Gasnier 
1564d4b3052SFabrice Gasnier static int stm32_dac_write_raw(struct iio_dev *indio_dev,
1574d4b3052SFabrice Gasnier 			       struct iio_chan_spec const *chan,
1584d4b3052SFabrice Gasnier 			       int val, int val2, long mask)
1594d4b3052SFabrice Gasnier {
1604d4b3052SFabrice Gasnier 	struct stm32_dac *dac = iio_priv(indio_dev);
1614d4b3052SFabrice Gasnier 
1624d4b3052SFabrice Gasnier 	switch (mask) {
1634d4b3052SFabrice Gasnier 	case IIO_CHAN_INFO_RAW:
1644d4b3052SFabrice Gasnier 		return stm32_dac_set_value(dac, chan->channel, val);
1654d4b3052SFabrice Gasnier 	default:
1664d4b3052SFabrice Gasnier 		return -EINVAL;
1674d4b3052SFabrice Gasnier 	}
1684d4b3052SFabrice Gasnier }
1694d4b3052SFabrice Gasnier 
1704d4b3052SFabrice Gasnier static int stm32_dac_debugfs_reg_access(struct iio_dev *indio_dev,
1714d4b3052SFabrice Gasnier 					unsigned reg, unsigned writeval,
1724d4b3052SFabrice Gasnier 					unsigned *readval)
1734d4b3052SFabrice Gasnier {
1744d4b3052SFabrice Gasnier 	struct stm32_dac *dac = iio_priv(indio_dev);
1754d4b3052SFabrice Gasnier 
1764d4b3052SFabrice Gasnier 	if (!readval)
1774d4b3052SFabrice Gasnier 		return regmap_write(dac->common->regmap, reg, writeval);
1784d4b3052SFabrice Gasnier 	else
1794d4b3052SFabrice Gasnier 		return regmap_read(dac->common->regmap, reg, readval);
1804d4b3052SFabrice Gasnier }
1814d4b3052SFabrice Gasnier 
1824d4b3052SFabrice Gasnier static const struct iio_info stm32_dac_iio_info = {
1834d4b3052SFabrice Gasnier 	.read_raw = stm32_dac_read_raw,
1844d4b3052SFabrice Gasnier 	.write_raw = stm32_dac_write_raw,
1854d4b3052SFabrice Gasnier 	.debugfs_reg_access = stm32_dac_debugfs_reg_access,
1864d4b3052SFabrice Gasnier };
1874d4b3052SFabrice Gasnier 
1884d4b3052SFabrice Gasnier static const char * const stm32_dac_powerdown_modes[] = {
1894d4b3052SFabrice Gasnier 	"three_state",
1904d4b3052SFabrice Gasnier };
1914d4b3052SFabrice Gasnier 
1924d4b3052SFabrice Gasnier static int stm32_dac_get_powerdown_mode(struct iio_dev *indio_dev,
1934d4b3052SFabrice Gasnier 					const struct iio_chan_spec *chan)
1944d4b3052SFabrice Gasnier {
1954d4b3052SFabrice Gasnier 	return 0;
1964d4b3052SFabrice Gasnier }
1974d4b3052SFabrice Gasnier 
1984d4b3052SFabrice Gasnier static int stm32_dac_set_powerdown_mode(struct iio_dev *indio_dev,
1994d4b3052SFabrice Gasnier 					const struct iio_chan_spec *chan,
2004d4b3052SFabrice Gasnier 					unsigned int type)
2014d4b3052SFabrice Gasnier {
2024d4b3052SFabrice Gasnier 	return 0;
2034d4b3052SFabrice Gasnier }
2044d4b3052SFabrice Gasnier 
2054d4b3052SFabrice Gasnier static ssize_t stm32_dac_read_powerdown(struct iio_dev *indio_dev,
2064d4b3052SFabrice Gasnier 					uintptr_t private,
2074d4b3052SFabrice Gasnier 					const struct iio_chan_spec *chan,
2084d4b3052SFabrice Gasnier 					char *buf)
2094d4b3052SFabrice Gasnier {
2104d4b3052SFabrice Gasnier 	int ret = stm32_dac_is_enabled(indio_dev, chan->channel);
2114d4b3052SFabrice Gasnier 
2124d4b3052SFabrice Gasnier 	if (ret < 0)
2134d4b3052SFabrice Gasnier 		return ret;
2144d4b3052SFabrice Gasnier 
215f46ac009SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", ret ? 0 : 1);
2164d4b3052SFabrice Gasnier }
2174d4b3052SFabrice Gasnier 
2184d4b3052SFabrice Gasnier static ssize_t stm32_dac_write_powerdown(struct iio_dev *indio_dev,
2194d4b3052SFabrice Gasnier 					 uintptr_t private,
2204d4b3052SFabrice Gasnier 					 const struct iio_chan_spec *chan,
2214d4b3052SFabrice Gasnier 					 const char *buf, size_t len)
2224d4b3052SFabrice Gasnier {
2234d4b3052SFabrice Gasnier 	bool powerdown;
2244d4b3052SFabrice Gasnier 	int ret;
2254d4b3052SFabrice Gasnier 
22674f582ecSLars-Peter Clausen 	ret = kstrtobool(buf, &powerdown);
2274d4b3052SFabrice Gasnier 	if (ret)
2284d4b3052SFabrice Gasnier 		return ret;
2294d4b3052SFabrice Gasnier 
2304d4b3052SFabrice Gasnier 	ret = stm32_dac_set_enable_state(indio_dev, chan->channel, !powerdown);
2314d4b3052SFabrice Gasnier 	if (ret)
2324d4b3052SFabrice Gasnier 		return ret;
2334d4b3052SFabrice Gasnier 
2344d4b3052SFabrice Gasnier 	return len;
2354d4b3052SFabrice Gasnier }
2364d4b3052SFabrice Gasnier 
2374d4b3052SFabrice Gasnier static const struct iio_enum stm32_dac_powerdown_mode_en = {
2384d4b3052SFabrice Gasnier 	.items = stm32_dac_powerdown_modes,
2394d4b3052SFabrice Gasnier 	.num_items = ARRAY_SIZE(stm32_dac_powerdown_modes),
2404d4b3052SFabrice Gasnier 	.get = stm32_dac_get_powerdown_mode,
2414d4b3052SFabrice Gasnier 	.set = stm32_dac_set_powerdown_mode,
2424d4b3052SFabrice Gasnier };
2434d4b3052SFabrice Gasnier 
2444d4b3052SFabrice Gasnier static const struct iio_chan_spec_ext_info stm32_dac_ext_info[] = {
2454d4b3052SFabrice Gasnier 	{
2464d4b3052SFabrice Gasnier 		.name = "powerdown",
2474d4b3052SFabrice Gasnier 		.read = stm32_dac_read_powerdown,
2484d4b3052SFabrice Gasnier 		.write = stm32_dac_write_powerdown,
2494d4b3052SFabrice Gasnier 		.shared = IIO_SEPARATE,
2504d4b3052SFabrice Gasnier 	},
2514d4b3052SFabrice Gasnier 	IIO_ENUM("powerdown_mode", IIO_SEPARATE, &stm32_dac_powerdown_mode_en),
252ffc7c517SAntoniu Miclaus 	IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &stm32_dac_powerdown_mode_en),
2534d4b3052SFabrice Gasnier 	{},
2544d4b3052SFabrice Gasnier };
2554d4b3052SFabrice Gasnier 
2564d4b3052SFabrice Gasnier #define STM32_DAC_CHANNEL(chan, name) {			\
2574d4b3052SFabrice Gasnier 	.type = IIO_VOLTAGE,				\
2584d4b3052SFabrice Gasnier 	.indexed = 1,					\
2594d4b3052SFabrice Gasnier 	.output = 1,					\
2604d4b3052SFabrice Gasnier 	.channel = chan,				\
2614d4b3052SFabrice Gasnier 	.info_mask_separate =				\
2624d4b3052SFabrice Gasnier 		BIT(IIO_CHAN_INFO_RAW) |		\
2634d4b3052SFabrice Gasnier 		BIT(IIO_CHAN_INFO_SCALE),		\
2644d4b3052SFabrice Gasnier 	/* scan_index is always 0 as num_channels is 1 */ \
2654d4b3052SFabrice Gasnier 	.scan_type = {					\
2664d4b3052SFabrice Gasnier 		.sign = 'u',				\
2674d4b3052SFabrice Gasnier 		.realbits = 12,				\
2684d4b3052SFabrice Gasnier 		.storagebits = 16,			\
2694d4b3052SFabrice Gasnier 	},						\
2704d4b3052SFabrice Gasnier 	.datasheet_name = name,				\
2714d4b3052SFabrice Gasnier 	.ext_info = stm32_dac_ext_info			\
2724d4b3052SFabrice Gasnier }
2734d4b3052SFabrice Gasnier 
2744d4b3052SFabrice Gasnier static const struct iio_chan_spec stm32_dac_channels[] = {
2754d4b3052SFabrice Gasnier 	STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_1, "out1"),
2764d4b3052SFabrice Gasnier 	STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_2, "out2"),
2774d4b3052SFabrice Gasnier };
2784d4b3052SFabrice Gasnier 
2794d4b3052SFabrice Gasnier static int stm32_dac_chan_of_init(struct iio_dev *indio_dev)
2804d4b3052SFabrice Gasnier {
2814d4b3052SFabrice Gasnier 	struct device_node *np = indio_dev->dev.of_node;
2824d4b3052SFabrice Gasnier 	unsigned int i;
2834d4b3052SFabrice Gasnier 	u32 channel;
2844d4b3052SFabrice Gasnier 	int ret;
2854d4b3052SFabrice Gasnier 
2864d4b3052SFabrice Gasnier 	ret = of_property_read_u32(np, "reg", &channel);
2874d4b3052SFabrice Gasnier 	if (ret) {
2884d4b3052SFabrice Gasnier 		dev_err(&indio_dev->dev, "Failed to read reg property\n");
2894d4b3052SFabrice Gasnier 		return ret;
2904d4b3052SFabrice Gasnier 	}
2914d4b3052SFabrice Gasnier 
2924d4b3052SFabrice Gasnier 	for (i = 0; i < ARRAY_SIZE(stm32_dac_channels); i++) {
2934d4b3052SFabrice Gasnier 		if (stm32_dac_channels[i].channel == channel)
2944d4b3052SFabrice Gasnier 			break;
2954d4b3052SFabrice Gasnier 	}
2964d4b3052SFabrice Gasnier 	if (i >= ARRAY_SIZE(stm32_dac_channels)) {
2970c1a1b6cSFabrice Gasnier 		dev_err(&indio_dev->dev, "Invalid reg property\n");
2984d4b3052SFabrice Gasnier 		return -EINVAL;
2994d4b3052SFabrice Gasnier 	}
3004d4b3052SFabrice Gasnier 
3014d4b3052SFabrice Gasnier 	indio_dev->channels = &stm32_dac_channels[i];
3024d4b3052SFabrice Gasnier 	/*
3034d4b3052SFabrice Gasnier 	 * Expose only one channel here, as they can be used independently,
3044d4b3052SFabrice Gasnier 	 * with separate trigger. Then separate IIO devices are instantiated
3054d4b3052SFabrice Gasnier 	 * to manage this.
3064d4b3052SFabrice Gasnier 	 */
3074d4b3052SFabrice Gasnier 	indio_dev->num_channels = 1;
3084d4b3052SFabrice Gasnier 
3094d4b3052SFabrice Gasnier 	return 0;
3104d4b3052SFabrice Gasnier };
3114d4b3052SFabrice Gasnier 
3124d4b3052SFabrice Gasnier static int stm32_dac_probe(struct platform_device *pdev)
3134d4b3052SFabrice Gasnier {
3144d4b3052SFabrice Gasnier 	struct device_node *np = pdev->dev.of_node;
3159d8e91d9SFabrice Gasnier 	struct device *dev = &pdev->dev;
3164d4b3052SFabrice Gasnier 	struct iio_dev *indio_dev;
3174d4b3052SFabrice Gasnier 	struct stm32_dac *dac;
3184d4b3052SFabrice Gasnier 	int ret;
3194d4b3052SFabrice Gasnier 
3204d4b3052SFabrice Gasnier 	if (!np)
3214d4b3052SFabrice Gasnier 		return -ENODEV;
3224d4b3052SFabrice Gasnier 
3234d4b3052SFabrice Gasnier 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac));
3244d4b3052SFabrice Gasnier 	if (!indio_dev)
3254d4b3052SFabrice Gasnier 		return -ENOMEM;
3264d4b3052SFabrice Gasnier 	platform_set_drvdata(pdev, indio_dev);
3274d4b3052SFabrice Gasnier 
3284d4b3052SFabrice Gasnier 	dac = iio_priv(indio_dev);
3294d4b3052SFabrice Gasnier 	dac->common = dev_get_drvdata(pdev->dev.parent);
3304d4b3052SFabrice Gasnier 	indio_dev->name = dev_name(&pdev->dev);
3314d4b3052SFabrice Gasnier 	indio_dev->dev.of_node = pdev->dev.of_node;
3324d4b3052SFabrice Gasnier 	indio_dev->info = &stm32_dac_iio_info;
3334d4b3052SFabrice Gasnier 	indio_dev->modes = INDIO_DIRECT_MODE;
3344d4b3052SFabrice Gasnier 
3352544ea7bSSergiu Cuciurean 	mutex_init(&dac->lock);
3362544ea7bSSergiu Cuciurean 
3374d4b3052SFabrice Gasnier 	ret = stm32_dac_chan_of_init(indio_dev);
3384d4b3052SFabrice Gasnier 	if (ret < 0)
3394d4b3052SFabrice Gasnier 		return ret;
3404d4b3052SFabrice Gasnier 
3419d8e91d9SFabrice Gasnier 	/* Get stm32-dac-core PM online */
3429d8e91d9SFabrice Gasnier 	pm_runtime_get_noresume(dev);
3439d8e91d9SFabrice Gasnier 	pm_runtime_set_active(dev);
3449d8e91d9SFabrice Gasnier 	pm_runtime_set_autosuspend_delay(dev, STM32_DAC_AUTO_SUSPEND_DELAY_MS);
3459d8e91d9SFabrice Gasnier 	pm_runtime_use_autosuspend(dev);
3469d8e91d9SFabrice Gasnier 	pm_runtime_enable(dev);
3479d8e91d9SFabrice Gasnier 
3489d8e91d9SFabrice Gasnier 	ret = iio_device_register(indio_dev);
3499d8e91d9SFabrice Gasnier 	if (ret)
3509d8e91d9SFabrice Gasnier 		goto err_pm_put;
3519d8e91d9SFabrice Gasnier 
3529d8e91d9SFabrice Gasnier 	pm_runtime_mark_last_busy(dev);
3539d8e91d9SFabrice Gasnier 	pm_runtime_put_autosuspend(dev);
3549d8e91d9SFabrice Gasnier 
3559d8e91d9SFabrice Gasnier 	return 0;
3569d8e91d9SFabrice Gasnier 
3579d8e91d9SFabrice Gasnier err_pm_put:
3589d8e91d9SFabrice Gasnier 	pm_runtime_disable(dev);
3599d8e91d9SFabrice Gasnier 	pm_runtime_set_suspended(dev);
3609d8e91d9SFabrice Gasnier 	pm_runtime_put_noidle(dev);
3619d8e91d9SFabrice Gasnier 
3629d8e91d9SFabrice Gasnier 	return ret;
3634d4b3052SFabrice Gasnier }
3644d4b3052SFabrice Gasnier 
365*85a32a0aSUwe Kleine-König static void stm32_dac_remove(struct platform_device *pdev)
3669d8e91d9SFabrice Gasnier {
3679d8e91d9SFabrice Gasnier 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
3689d8e91d9SFabrice Gasnier 
3699d8e91d9SFabrice Gasnier 	pm_runtime_get_sync(&pdev->dev);
3709d8e91d9SFabrice Gasnier 	iio_device_unregister(indio_dev);
3719d8e91d9SFabrice Gasnier 	pm_runtime_disable(&pdev->dev);
3729d8e91d9SFabrice Gasnier 	pm_runtime_set_suspended(&pdev->dev);
3739d8e91d9SFabrice Gasnier 	pm_runtime_put_noidle(&pdev->dev);
3749d8e91d9SFabrice Gasnier }
3759d8e91d9SFabrice Gasnier 
376ade59a7aSJonathan Cameron static int stm32_dac_suspend(struct device *dev)
3779d8e91d9SFabrice Gasnier {
3789d8e91d9SFabrice Gasnier 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
3799d8e91d9SFabrice Gasnier 	int channel = indio_dev->channels[0].channel;
3809d8e91d9SFabrice Gasnier 	int ret;
3819d8e91d9SFabrice Gasnier 
3829d8e91d9SFabrice Gasnier 	/* Ensure DAC is disabled before suspend */
3839d8e91d9SFabrice Gasnier 	ret = stm32_dac_is_enabled(indio_dev, channel);
3849d8e91d9SFabrice Gasnier 	if (ret)
3859d8e91d9SFabrice Gasnier 		return ret < 0 ? ret : -EBUSY;
3869d8e91d9SFabrice Gasnier 
3879d8e91d9SFabrice Gasnier 	return pm_runtime_force_suspend(dev);
3889d8e91d9SFabrice Gasnier }
3899d8e91d9SFabrice Gasnier 
390ade59a7aSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(stm32_dac_pm_ops, stm32_dac_suspend,
391ade59a7aSJonathan Cameron 				pm_runtime_force_resume);
3929d8e91d9SFabrice Gasnier 
3934d4b3052SFabrice Gasnier static const struct of_device_id stm32_dac_of_match[] = {
3944d4b3052SFabrice Gasnier 	{ .compatible = "st,stm32-dac", },
3954d4b3052SFabrice Gasnier 	{},
3964d4b3052SFabrice Gasnier };
3974d4b3052SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
3984d4b3052SFabrice Gasnier 
3994d4b3052SFabrice Gasnier static struct platform_driver stm32_dac_driver = {
4004d4b3052SFabrice Gasnier 	.probe = stm32_dac_probe,
401*85a32a0aSUwe Kleine-König 	.remove_new = stm32_dac_remove,
4024d4b3052SFabrice Gasnier 	.driver = {
4034d4b3052SFabrice Gasnier 		.name = "stm32-dac",
4044d4b3052SFabrice Gasnier 		.of_match_table = stm32_dac_of_match,
405ade59a7aSJonathan Cameron 		.pm = pm_sleep_ptr(&stm32_dac_pm_ops),
4064d4b3052SFabrice Gasnier 	},
4074d4b3052SFabrice Gasnier };
4084d4b3052SFabrice Gasnier module_platform_driver(stm32_dac_driver);
4094d4b3052SFabrice Gasnier 
4104d4b3052SFabrice Gasnier MODULE_ALIAS("platform:stm32-dac");
4114d4b3052SFabrice Gasnier MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
4124d4b3052SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 DAC driver");
4134d4b3052SFabrice Gasnier MODULE_LICENSE("GPL v2");
414