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> 144d4b3052SFabrice Gasnier #include <linux/module.h> 154d4b3052SFabrice Gasnier #include <linux/platform_device.h> 169d8e91d9SFabrice Gasnier #include <linux/pm_runtime.h> 174d4b3052SFabrice Gasnier 184d4b3052SFabrice Gasnier #include "stm32-dac-core.h" 194d4b3052SFabrice Gasnier 204d4b3052SFabrice Gasnier #define STM32_DAC_CHANNEL_1 1 214d4b3052SFabrice Gasnier #define STM32_DAC_CHANNEL_2 2 224d4b3052SFabrice Gasnier #define STM32_DAC_IS_CHAN_1(ch) ((ch) & STM32_DAC_CHANNEL_1) 234d4b3052SFabrice Gasnier 249d8e91d9SFabrice Gasnier #define STM32_DAC_AUTO_SUSPEND_DELAY_MS 2000 259d8e91d9SFabrice Gasnier 264d4b3052SFabrice Gasnier /** 274d4b3052SFabrice Gasnier * struct stm32_dac - private data of DAC driver 284d4b3052SFabrice Gasnier * @common: reference to DAC common data 292544ea7bSSergiu Cuciurean * @lock: lock to protect against potential races when reading 302544ea7bSSergiu Cuciurean * and update CR, to keep it in sync with pm_runtime 314d4b3052SFabrice Gasnier */ 324d4b3052SFabrice Gasnier struct stm32_dac { 334d4b3052SFabrice Gasnier struct stm32_dac_common *common; 342544ea7bSSergiu Cuciurean struct mutex lock; 354d4b3052SFabrice Gasnier }; 364d4b3052SFabrice Gasnier 374d4b3052SFabrice Gasnier static int stm32_dac_is_enabled(struct iio_dev *indio_dev, int channel) 384d4b3052SFabrice Gasnier { 394d4b3052SFabrice Gasnier struct stm32_dac *dac = iio_priv(indio_dev); 404d4b3052SFabrice Gasnier u32 en, val; 414d4b3052SFabrice Gasnier int ret; 424d4b3052SFabrice Gasnier 434d4b3052SFabrice Gasnier ret = regmap_read(dac->common->regmap, STM32_DAC_CR, &val); 444d4b3052SFabrice Gasnier if (ret < 0) 454d4b3052SFabrice Gasnier return ret; 464d4b3052SFabrice Gasnier if (STM32_DAC_IS_CHAN_1(channel)) 474d4b3052SFabrice Gasnier en = FIELD_GET(STM32_DAC_CR_EN1, val); 484d4b3052SFabrice Gasnier else 494d4b3052SFabrice Gasnier en = FIELD_GET(STM32_DAC_CR_EN2, val); 504d4b3052SFabrice Gasnier 514d4b3052SFabrice Gasnier return !!en; 524d4b3052SFabrice Gasnier } 534d4b3052SFabrice Gasnier 544d4b3052SFabrice Gasnier static int stm32_dac_set_enable_state(struct iio_dev *indio_dev, int ch, 554d4b3052SFabrice Gasnier bool enable) 564d4b3052SFabrice Gasnier { 574d4b3052SFabrice Gasnier struct stm32_dac *dac = iio_priv(indio_dev); 589d8e91d9SFabrice Gasnier struct device *dev = indio_dev->dev.parent; 594d4b3052SFabrice Gasnier u32 msk = STM32_DAC_IS_CHAN_1(ch) ? STM32_DAC_CR_EN1 : STM32_DAC_CR_EN2; 604d4b3052SFabrice Gasnier u32 en = enable ? msk : 0; 614d4b3052SFabrice Gasnier int ret; 624d4b3052SFabrice Gasnier 639d8e91d9SFabrice Gasnier /* already enabled / disabled ? */ 642544ea7bSSergiu Cuciurean mutex_lock(&dac->lock); 659d8e91d9SFabrice Gasnier ret = stm32_dac_is_enabled(indio_dev, ch); 669d8e91d9SFabrice Gasnier if (ret < 0 || enable == !!ret) { 672544ea7bSSergiu Cuciurean mutex_unlock(&dac->lock); 689d8e91d9SFabrice Gasnier return ret < 0 ? ret : 0; 699d8e91d9SFabrice Gasnier } 709d8e91d9SFabrice Gasnier 719d8e91d9SFabrice Gasnier if (enable) { 729d8e91d9SFabrice Gasnier ret = pm_runtime_get_sync(dev); 739d8e91d9SFabrice Gasnier if (ret < 0) { 749d8e91d9SFabrice Gasnier pm_runtime_put_noidle(dev); 752544ea7bSSergiu Cuciurean mutex_unlock(&dac->lock); 769d8e91d9SFabrice Gasnier return ret; 779d8e91d9SFabrice Gasnier } 789d8e91d9SFabrice Gasnier } 799d8e91d9SFabrice Gasnier 804d4b3052SFabrice Gasnier ret = regmap_update_bits(dac->common->regmap, STM32_DAC_CR, msk, en); 812544ea7bSSergiu Cuciurean mutex_unlock(&dac->lock); 824d4b3052SFabrice Gasnier if (ret < 0) { 834d4b3052SFabrice Gasnier dev_err(&indio_dev->dev, "%s failed\n", en ? 844d4b3052SFabrice Gasnier "Enable" : "Disable"); 859d8e91d9SFabrice Gasnier goto err_put_pm; 864d4b3052SFabrice Gasnier } 874d4b3052SFabrice Gasnier 884d4b3052SFabrice Gasnier /* 894d4b3052SFabrice Gasnier * When HFSEL is set, it is not allowed to write the DHRx register 904d4b3052SFabrice Gasnier * during 8 clock cycles after the ENx bit is set. It is not allowed 914d4b3052SFabrice Gasnier * to make software/hardware trigger during this period either. 924d4b3052SFabrice Gasnier */ 934d4b3052SFabrice Gasnier if (en && dac->common->hfsel) 944d4b3052SFabrice Gasnier udelay(1); 954d4b3052SFabrice Gasnier 969d8e91d9SFabrice Gasnier if (!enable) { 979d8e91d9SFabrice Gasnier pm_runtime_mark_last_busy(dev); 989d8e91d9SFabrice Gasnier pm_runtime_put_autosuspend(dev); 999d8e91d9SFabrice Gasnier } 1009d8e91d9SFabrice Gasnier 1014d4b3052SFabrice Gasnier return 0; 1029d8e91d9SFabrice Gasnier 1039d8e91d9SFabrice Gasnier err_put_pm: 1049d8e91d9SFabrice Gasnier if (enable) { 1059d8e91d9SFabrice Gasnier pm_runtime_mark_last_busy(dev); 1069d8e91d9SFabrice Gasnier pm_runtime_put_autosuspend(dev); 1079d8e91d9SFabrice Gasnier } 1089d8e91d9SFabrice Gasnier 1099d8e91d9SFabrice Gasnier return ret; 1104d4b3052SFabrice Gasnier } 1114d4b3052SFabrice Gasnier 1124d4b3052SFabrice Gasnier static int stm32_dac_get_value(struct stm32_dac *dac, int channel, int *val) 1134d4b3052SFabrice Gasnier { 1144d4b3052SFabrice Gasnier int ret; 1154d4b3052SFabrice Gasnier 1164d4b3052SFabrice Gasnier if (STM32_DAC_IS_CHAN_1(channel)) 1174d4b3052SFabrice Gasnier ret = regmap_read(dac->common->regmap, STM32_DAC_DOR1, val); 1184d4b3052SFabrice Gasnier else 1194d4b3052SFabrice Gasnier ret = regmap_read(dac->common->regmap, STM32_DAC_DOR2, val); 1204d4b3052SFabrice Gasnier 1214d4b3052SFabrice Gasnier return ret ? ret : IIO_VAL_INT; 1224d4b3052SFabrice Gasnier } 1234d4b3052SFabrice Gasnier 1244d4b3052SFabrice Gasnier static int stm32_dac_set_value(struct stm32_dac *dac, int channel, int val) 1254d4b3052SFabrice Gasnier { 1264d4b3052SFabrice Gasnier int ret; 1274d4b3052SFabrice Gasnier 1284d4b3052SFabrice Gasnier if (STM32_DAC_IS_CHAN_1(channel)) 1294d4b3052SFabrice Gasnier ret = regmap_write(dac->common->regmap, STM32_DAC_DHR12R1, val); 1304d4b3052SFabrice Gasnier else 1314d4b3052SFabrice Gasnier ret = regmap_write(dac->common->regmap, STM32_DAC_DHR12R2, val); 1324d4b3052SFabrice Gasnier 1334d4b3052SFabrice Gasnier return ret; 1344d4b3052SFabrice Gasnier } 1354d4b3052SFabrice Gasnier 1364d4b3052SFabrice Gasnier static int stm32_dac_read_raw(struct iio_dev *indio_dev, 1374d4b3052SFabrice Gasnier struct iio_chan_spec const *chan, 1384d4b3052SFabrice Gasnier int *val, int *val2, long mask) 1394d4b3052SFabrice Gasnier { 1404d4b3052SFabrice Gasnier struct stm32_dac *dac = iio_priv(indio_dev); 1414d4b3052SFabrice Gasnier 1424d4b3052SFabrice Gasnier switch (mask) { 1434d4b3052SFabrice Gasnier case IIO_CHAN_INFO_RAW: 1444d4b3052SFabrice Gasnier return stm32_dac_get_value(dac, chan->channel, val); 1454d4b3052SFabrice Gasnier case IIO_CHAN_INFO_SCALE: 1464d4b3052SFabrice Gasnier *val = dac->common->vref_mv; 1474d4b3052SFabrice Gasnier *val2 = chan->scan_type.realbits; 1484d4b3052SFabrice Gasnier return IIO_VAL_FRACTIONAL_LOG2; 1494d4b3052SFabrice Gasnier default: 1504d4b3052SFabrice Gasnier return -EINVAL; 1514d4b3052SFabrice Gasnier } 1524d4b3052SFabrice Gasnier } 1534d4b3052SFabrice Gasnier 1544d4b3052SFabrice Gasnier static int stm32_dac_write_raw(struct iio_dev *indio_dev, 1554d4b3052SFabrice Gasnier struct iio_chan_spec const *chan, 1564d4b3052SFabrice Gasnier int val, int val2, long mask) 1574d4b3052SFabrice Gasnier { 1584d4b3052SFabrice Gasnier struct stm32_dac *dac = iio_priv(indio_dev); 1594d4b3052SFabrice Gasnier 1604d4b3052SFabrice Gasnier switch (mask) { 1614d4b3052SFabrice Gasnier case IIO_CHAN_INFO_RAW: 1624d4b3052SFabrice Gasnier return stm32_dac_set_value(dac, chan->channel, val); 1634d4b3052SFabrice Gasnier default: 1644d4b3052SFabrice Gasnier return -EINVAL; 1654d4b3052SFabrice Gasnier } 1664d4b3052SFabrice Gasnier } 1674d4b3052SFabrice Gasnier 1684d4b3052SFabrice Gasnier static int stm32_dac_debugfs_reg_access(struct iio_dev *indio_dev, 1694d4b3052SFabrice Gasnier unsigned reg, unsigned writeval, 1704d4b3052SFabrice Gasnier unsigned *readval) 1714d4b3052SFabrice Gasnier { 1724d4b3052SFabrice Gasnier struct stm32_dac *dac = iio_priv(indio_dev); 1734d4b3052SFabrice Gasnier 1744d4b3052SFabrice Gasnier if (!readval) 1754d4b3052SFabrice Gasnier return regmap_write(dac->common->regmap, reg, writeval); 1764d4b3052SFabrice Gasnier else 1774d4b3052SFabrice Gasnier return regmap_read(dac->common->regmap, reg, readval); 1784d4b3052SFabrice Gasnier } 1794d4b3052SFabrice Gasnier 1804d4b3052SFabrice Gasnier static const struct iio_info stm32_dac_iio_info = { 1814d4b3052SFabrice Gasnier .read_raw = stm32_dac_read_raw, 1824d4b3052SFabrice Gasnier .write_raw = stm32_dac_write_raw, 1834d4b3052SFabrice Gasnier .debugfs_reg_access = stm32_dac_debugfs_reg_access, 1844d4b3052SFabrice Gasnier }; 1854d4b3052SFabrice Gasnier 1864d4b3052SFabrice Gasnier static const char * const stm32_dac_powerdown_modes[] = { 1874d4b3052SFabrice Gasnier "three_state", 1884d4b3052SFabrice Gasnier }; 1894d4b3052SFabrice Gasnier 1904d4b3052SFabrice Gasnier static int stm32_dac_get_powerdown_mode(struct iio_dev *indio_dev, 1914d4b3052SFabrice Gasnier const struct iio_chan_spec *chan) 1924d4b3052SFabrice Gasnier { 1934d4b3052SFabrice Gasnier return 0; 1944d4b3052SFabrice Gasnier } 1954d4b3052SFabrice Gasnier 1964d4b3052SFabrice Gasnier static int stm32_dac_set_powerdown_mode(struct iio_dev *indio_dev, 1974d4b3052SFabrice Gasnier const struct iio_chan_spec *chan, 1984d4b3052SFabrice Gasnier unsigned int type) 1994d4b3052SFabrice Gasnier { 2004d4b3052SFabrice Gasnier return 0; 2014d4b3052SFabrice Gasnier } 2024d4b3052SFabrice Gasnier 2034d4b3052SFabrice Gasnier static ssize_t stm32_dac_read_powerdown(struct iio_dev *indio_dev, 2044d4b3052SFabrice Gasnier uintptr_t private, 2054d4b3052SFabrice Gasnier const struct iio_chan_spec *chan, 2064d4b3052SFabrice Gasnier char *buf) 2074d4b3052SFabrice Gasnier { 2084d4b3052SFabrice Gasnier int ret = stm32_dac_is_enabled(indio_dev, chan->channel); 2094d4b3052SFabrice Gasnier 2104d4b3052SFabrice Gasnier if (ret < 0) 2114d4b3052SFabrice Gasnier return ret; 2124d4b3052SFabrice Gasnier 213*f46ac009SLars-Peter Clausen return sysfs_emit(buf, "%d\n", ret ? 0 : 1); 2144d4b3052SFabrice Gasnier } 2154d4b3052SFabrice Gasnier 2164d4b3052SFabrice Gasnier static ssize_t stm32_dac_write_powerdown(struct iio_dev *indio_dev, 2174d4b3052SFabrice Gasnier uintptr_t private, 2184d4b3052SFabrice Gasnier const struct iio_chan_spec *chan, 2194d4b3052SFabrice Gasnier const char *buf, size_t len) 2204d4b3052SFabrice Gasnier { 2214d4b3052SFabrice Gasnier bool powerdown; 2224d4b3052SFabrice Gasnier int ret; 2234d4b3052SFabrice Gasnier 2244d4b3052SFabrice Gasnier ret = strtobool(buf, &powerdown); 2254d4b3052SFabrice Gasnier if (ret) 2264d4b3052SFabrice Gasnier return ret; 2274d4b3052SFabrice Gasnier 2284d4b3052SFabrice Gasnier ret = stm32_dac_set_enable_state(indio_dev, chan->channel, !powerdown); 2294d4b3052SFabrice Gasnier if (ret) 2304d4b3052SFabrice Gasnier return ret; 2314d4b3052SFabrice Gasnier 2324d4b3052SFabrice Gasnier return len; 2334d4b3052SFabrice Gasnier } 2344d4b3052SFabrice Gasnier 2354d4b3052SFabrice Gasnier static const struct iio_enum stm32_dac_powerdown_mode_en = { 2364d4b3052SFabrice Gasnier .items = stm32_dac_powerdown_modes, 2374d4b3052SFabrice Gasnier .num_items = ARRAY_SIZE(stm32_dac_powerdown_modes), 2384d4b3052SFabrice Gasnier .get = stm32_dac_get_powerdown_mode, 2394d4b3052SFabrice Gasnier .set = stm32_dac_set_powerdown_mode, 2404d4b3052SFabrice Gasnier }; 2414d4b3052SFabrice Gasnier 2424d4b3052SFabrice Gasnier static const struct iio_chan_spec_ext_info stm32_dac_ext_info[] = { 2434d4b3052SFabrice Gasnier { 2444d4b3052SFabrice Gasnier .name = "powerdown", 2454d4b3052SFabrice Gasnier .read = stm32_dac_read_powerdown, 2464d4b3052SFabrice Gasnier .write = stm32_dac_write_powerdown, 2474d4b3052SFabrice Gasnier .shared = IIO_SEPARATE, 2484d4b3052SFabrice Gasnier }, 2494d4b3052SFabrice Gasnier IIO_ENUM("powerdown_mode", IIO_SEPARATE, &stm32_dac_powerdown_mode_en), 2504d4b3052SFabrice Gasnier IIO_ENUM_AVAILABLE("powerdown_mode", &stm32_dac_powerdown_mode_en), 2514d4b3052SFabrice Gasnier {}, 2524d4b3052SFabrice Gasnier }; 2534d4b3052SFabrice Gasnier 2544d4b3052SFabrice Gasnier #define STM32_DAC_CHANNEL(chan, name) { \ 2554d4b3052SFabrice Gasnier .type = IIO_VOLTAGE, \ 2564d4b3052SFabrice Gasnier .indexed = 1, \ 2574d4b3052SFabrice Gasnier .output = 1, \ 2584d4b3052SFabrice Gasnier .channel = chan, \ 2594d4b3052SFabrice Gasnier .info_mask_separate = \ 2604d4b3052SFabrice Gasnier BIT(IIO_CHAN_INFO_RAW) | \ 2614d4b3052SFabrice Gasnier BIT(IIO_CHAN_INFO_SCALE), \ 2624d4b3052SFabrice Gasnier /* scan_index is always 0 as num_channels is 1 */ \ 2634d4b3052SFabrice Gasnier .scan_type = { \ 2644d4b3052SFabrice Gasnier .sign = 'u', \ 2654d4b3052SFabrice Gasnier .realbits = 12, \ 2664d4b3052SFabrice Gasnier .storagebits = 16, \ 2674d4b3052SFabrice Gasnier }, \ 2684d4b3052SFabrice Gasnier .datasheet_name = name, \ 2694d4b3052SFabrice Gasnier .ext_info = stm32_dac_ext_info \ 2704d4b3052SFabrice Gasnier } 2714d4b3052SFabrice Gasnier 2724d4b3052SFabrice Gasnier static const struct iio_chan_spec stm32_dac_channels[] = { 2734d4b3052SFabrice Gasnier STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_1, "out1"), 2744d4b3052SFabrice Gasnier STM32_DAC_CHANNEL(STM32_DAC_CHANNEL_2, "out2"), 2754d4b3052SFabrice Gasnier }; 2764d4b3052SFabrice Gasnier 2774d4b3052SFabrice Gasnier static int stm32_dac_chan_of_init(struct iio_dev *indio_dev) 2784d4b3052SFabrice Gasnier { 2794d4b3052SFabrice Gasnier struct device_node *np = indio_dev->dev.of_node; 2804d4b3052SFabrice Gasnier unsigned int i; 2814d4b3052SFabrice Gasnier u32 channel; 2824d4b3052SFabrice Gasnier int ret; 2834d4b3052SFabrice Gasnier 2844d4b3052SFabrice Gasnier ret = of_property_read_u32(np, "reg", &channel); 2854d4b3052SFabrice Gasnier if (ret) { 2864d4b3052SFabrice Gasnier dev_err(&indio_dev->dev, "Failed to read reg property\n"); 2874d4b3052SFabrice Gasnier return ret; 2884d4b3052SFabrice Gasnier } 2894d4b3052SFabrice Gasnier 2904d4b3052SFabrice Gasnier for (i = 0; i < ARRAY_SIZE(stm32_dac_channels); i++) { 2914d4b3052SFabrice Gasnier if (stm32_dac_channels[i].channel == channel) 2924d4b3052SFabrice Gasnier break; 2934d4b3052SFabrice Gasnier } 2944d4b3052SFabrice Gasnier if (i >= ARRAY_SIZE(stm32_dac_channels)) { 2950c1a1b6cSFabrice Gasnier dev_err(&indio_dev->dev, "Invalid reg property\n"); 2964d4b3052SFabrice Gasnier return -EINVAL; 2974d4b3052SFabrice Gasnier } 2984d4b3052SFabrice Gasnier 2994d4b3052SFabrice Gasnier indio_dev->channels = &stm32_dac_channels[i]; 3004d4b3052SFabrice Gasnier /* 3014d4b3052SFabrice Gasnier * Expose only one channel here, as they can be used independently, 3024d4b3052SFabrice Gasnier * with separate trigger. Then separate IIO devices are instantiated 3034d4b3052SFabrice Gasnier * to manage this. 3044d4b3052SFabrice Gasnier */ 3054d4b3052SFabrice Gasnier indio_dev->num_channels = 1; 3064d4b3052SFabrice Gasnier 3074d4b3052SFabrice Gasnier return 0; 3084d4b3052SFabrice Gasnier }; 3094d4b3052SFabrice Gasnier 3104d4b3052SFabrice Gasnier static int stm32_dac_probe(struct platform_device *pdev) 3114d4b3052SFabrice Gasnier { 3124d4b3052SFabrice Gasnier struct device_node *np = pdev->dev.of_node; 3139d8e91d9SFabrice Gasnier struct device *dev = &pdev->dev; 3144d4b3052SFabrice Gasnier struct iio_dev *indio_dev; 3154d4b3052SFabrice Gasnier struct stm32_dac *dac; 3164d4b3052SFabrice Gasnier int ret; 3174d4b3052SFabrice Gasnier 3184d4b3052SFabrice Gasnier if (!np) 3194d4b3052SFabrice Gasnier return -ENODEV; 3204d4b3052SFabrice Gasnier 3214d4b3052SFabrice Gasnier indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac)); 3224d4b3052SFabrice Gasnier if (!indio_dev) 3234d4b3052SFabrice Gasnier return -ENOMEM; 3244d4b3052SFabrice Gasnier platform_set_drvdata(pdev, indio_dev); 3254d4b3052SFabrice Gasnier 3264d4b3052SFabrice Gasnier dac = iio_priv(indio_dev); 3274d4b3052SFabrice Gasnier dac->common = dev_get_drvdata(pdev->dev.parent); 3284d4b3052SFabrice Gasnier indio_dev->name = dev_name(&pdev->dev); 3294d4b3052SFabrice Gasnier indio_dev->dev.of_node = pdev->dev.of_node; 3304d4b3052SFabrice Gasnier indio_dev->info = &stm32_dac_iio_info; 3314d4b3052SFabrice Gasnier indio_dev->modes = INDIO_DIRECT_MODE; 3324d4b3052SFabrice Gasnier 3332544ea7bSSergiu Cuciurean mutex_init(&dac->lock); 3342544ea7bSSergiu Cuciurean 3354d4b3052SFabrice Gasnier ret = stm32_dac_chan_of_init(indio_dev); 3364d4b3052SFabrice Gasnier if (ret < 0) 3374d4b3052SFabrice Gasnier return ret; 3384d4b3052SFabrice Gasnier 3399d8e91d9SFabrice Gasnier /* Get stm32-dac-core PM online */ 3409d8e91d9SFabrice Gasnier pm_runtime_get_noresume(dev); 3419d8e91d9SFabrice Gasnier pm_runtime_set_active(dev); 3429d8e91d9SFabrice Gasnier pm_runtime_set_autosuspend_delay(dev, STM32_DAC_AUTO_SUSPEND_DELAY_MS); 3439d8e91d9SFabrice Gasnier pm_runtime_use_autosuspend(dev); 3449d8e91d9SFabrice Gasnier pm_runtime_enable(dev); 3459d8e91d9SFabrice Gasnier 3469d8e91d9SFabrice Gasnier ret = iio_device_register(indio_dev); 3479d8e91d9SFabrice Gasnier if (ret) 3489d8e91d9SFabrice Gasnier goto err_pm_put; 3499d8e91d9SFabrice Gasnier 3509d8e91d9SFabrice Gasnier pm_runtime_mark_last_busy(dev); 3519d8e91d9SFabrice Gasnier pm_runtime_put_autosuspend(dev); 3529d8e91d9SFabrice Gasnier 3539d8e91d9SFabrice Gasnier return 0; 3549d8e91d9SFabrice Gasnier 3559d8e91d9SFabrice Gasnier err_pm_put: 3569d8e91d9SFabrice Gasnier pm_runtime_disable(dev); 3579d8e91d9SFabrice Gasnier pm_runtime_set_suspended(dev); 3589d8e91d9SFabrice Gasnier pm_runtime_put_noidle(dev); 3599d8e91d9SFabrice Gasnier 3609d8e91d9SFabrice Gasnier return ret; 3614d4b3052SFabrice Gasnier } 3624d4b3052SFabrice Gasnier 3639d8e91d9SFabrice Gasnier static int stm32_dac_remove(struct platform_device *pdev) 3649d8e91d9SFabrice Gasnier { 3659d8e91d9SFabrice Gasnier struct iio_dev *indio_dev = platform_get_drvdata(pdev); 3669d8e91d9SFabrice Gasnier 3679d8e91d9SFabrice Gasnier pm_runtime_get_sync(&pdev->dev); 3689d8e91d9SFabrice Gasnier iio_device_unregister(indio_dev); 3699d8e91d9SFabrice Gasnier pm_runtime_disable(&pdev->dev); 3709d8e91d9SFabrice Gasnier pm_runtime_set_suspended(&pdev->dev); 3719d8e91d9SFabrice Gasnier pm_runtime_put_noidle(&pdev->dev); 3729d8e91d9SFabrice Gasnier 3739d8e91d9SFabrice Gasnier return 0; 3749d8e91d9SFabrice Gasnier } 3759d8e91d9SFabrice Gasnier 3769d8e91d9SFabrice Gasnier static int __maybe_unused 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 3909d8e91d9SFabrice Gasnier static const struct dev_pm_ops stm32_dac_pm_ops = { 3919d8e91d9SFabrice Gasnier SET_SYSTEM_SLEEP_PM_OPS(stm32_dac_suspend, pm_runtime_force_resume) 3929d8e91d9SFabrice Gasnier }; 3939d8e91d9SFabrice Gasnier 3944d4b3052SFabrice Gasnier static const struct of_device_id stm32_dac_of_match[] = { 3954d4b3052SFabrice Gasnier { .compatible = "st,stm32-dac", }, 3964d4b3052SFabrice Gasnier {}, 3974d4b3052SFabrice Gasnier }; 3984d4b3052SFabrice Gasnier MODULE_DEVICE_TABLE(of, stm32_dac_of_match); 3994d4b3052SFabrice Gasnier 4004d4b3052SFabrice Gasnier static struct platform_driver stm32_dac_driver = { 4014d4b3052SFabrice Gasnier .probe = stm32_dac_probe, 4029d8e91d9SFabrice Gasnier .remove = stm32_dac_remove, 4034d4b3052SFabrice Gasnier .driver = { 4044d4b3052SFabrice Gasnier .name = "stm32-dac", 4054d4b3052SFabrice Gasnier .of_match_table = stm32_dac_of_match, 4069d8e91d9SFabrice Gasnier .pm = &stm32_dac_pm_ops, 4074d4b3052SFabrice Gasnier }, 4084d4b3052SFabrice Gasnier }; 4094d4b3052SFabrice Gasnier module_platform_driver(stm32_dac_driver); 4104d4b3052SFabrice Gasnier 4114d4b3052SFabrice Gasnier MODULE_ALIAS("platform:stm32-dac"); 4124d4b3052SFabrice Gasnier MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>"); 4134d4b3052SFabrice Gasnier MODULE_DESCRIPTION("STMicroelectronics STM32 DAC driver"); 4144d4b3052SFabrice Gasnier MODULE_LICENSE("GPL v2"); 415