Thomas Gleixner | 84a14ae | 2019-05-28 09:57:07 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 2 | /* |
| 3 | * Imagination Technologies Pulse Width Modulator driver |
| 4 | * |
| 5 | * Copyright (c) 2014-2015, Imagination Technologies |
| 6 | * |
| 7 | * Based on drivers/pwm/pwm-tegra.c, Copyright (c) 2010, NVIDIA Corporation |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/mfd/syscon.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of.h> |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 16 | #include <linux/of_device.h> |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 18 | #include <linux/pm_runtime.h> |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 19 | #include <linux/pwm.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/slab.h> |
| 22 | |
| 23 | /* PWM registers */ |
| 24 | #define PWM_CTRL_CFG 0x0000 |
| 25 | #define PWM_CTRL_CFG_NO_SUB_DIV 0 |
| 26 | #define PWM_CTRL_CFG_SUB_DIV0 1 |
| 27 | #define PWM_CTRL_CFG_SUB_DIV1 2 |
| 28 | #define PWM_CTRL_CFG_SUB_DIV0_DIV1 3 |
| 29 | #define PWM_CTRL_CFG_DIV_SHIFT(ch) ((ch) * 2 + 4) |
| 30 | #define PWM_CTRL_CFG_DIV_MASK 0x3 |
| 31 | |
| 32 | #define PWM_CH_CFG(ch) (0x4 + (ch) * 4) |
| 33 | #define PWM_CH_CFG_TMBASE_SHIFT 0 |
| 34 | #define PWM_CH_CFG_DUTY_SHIFT 16 |
| 35 | |
| 36 | #define PERIP_PWM_PDM_CONTROL 0x0140 |
| 37 | #define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1 |
| 38 | #define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4) |
| 39 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 40 | #define IMG_PWM_PM_TIMEOUT 1000 /* ms */ |
| 41 | |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 42 | /* |
| 43 | * PWM period is specified with a timebase register, |
| 44 | * in number of step periods. The PWM duty cycle is also |
| 45 | * specified in step periods, in the [0, $timebase] range. |
| 46 | * In other words, the timebase imposes the duty cycle |
| 47 | * resolution. Therefore, let's constraint the timebase to |
| 48 | * a minimum value to allow a sane range of duty cycle values. |
| 49 | * Imposing a minimum timebase, will impose a maximum PWM frequency. |
| 50 | * |
| 51 | * The value chosen is completely arbitrary. |
| 52 | */ |
| 53 | #define MIN_TMBASE_STEPS 16 |
| 54 | |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 55 | #define IMG_PWM_NPWM 4 |
| 56 | |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 57 | struct img_pwm_soc_data { |
| 58 | u32 max_timebase; |
| 59 | }; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 60 | |
| 61 | struct img_pwm_chip { |
| 62 | struct device *dev; |
| 63 | struct pwm_chip chip; |
| 64 | struct clk *pwm_clk; |
| 65 | struct clk *sys_clk; |
| 66 | void __iomem *base; |
| 67 | struct regmap *periph_regs; |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 68 | int max_period_ns; |
| 69 | int min_period_ns; |
| 70 | const struct img_pwm_soc_data *data; |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 71 | u32 suspend_ctrl_cfg; |
| 72 | u32 suspend_ch_cfg[IMG_PWM_NPWM]; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip) |
| 76 | { |
| 77 | return container_of(chip, struct img_pwm_chip, chip); |
| 78 | } |
| 79 | |
| 80 | static inline void img_pwm_writel(struct img_pwm_chip *chip, |
| 81 | u32 reg, u32 val) |
| 82 | { |
| 83 | writel(val, chip->base + reg); |
| 84 | } |
| 85 | |
| 86 | static inline u32 img_pwm_readl(struct img_pwm_chip *chip, |
| 87 | u32 reg) |
| 88 | { |
| 89 | return readl(chip->base + reg); |
| 90 | } |
| 91 | |
| 92 | static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
| 93 | int duty_ns, int period_ns) |
| 94 | { |
| 95 | u32 val, div, duty, timebase; |
| 96 | unsigned long mul, output_clk_hz, input_clk_hz; |
| 97 | struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip); |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 98 | unsigned int max_timebase = pwm_chip->data->max_timebase; |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 99 | int ret; |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 100 | |
| 101 | if (period_ns < pwm_chip->min_period_ns || |
| 102 | period_ns > pwm_chip->max_period_ns) { |
| 103 | dev_err(chip->dev, "configured period not in range\n"); |
| 104 | return -ERANGE; |
| 105 | } |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 106 | |
| 107 | input_clk_hz = clk_get_rate(pwm_chip->pwm_clk); |
| 108 | output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns); |
| 109 | |
| 110 | mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz); |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 111 | if (mul <= max_timebase) { |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 112 | div = PWM_CTRL_CFG_NO_SUB_DIV; |
| 113 | timebase = DIV_ROUND_UP(mul, 1); |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 114 | } else if (mul <= max_timebase * 8) { |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 115 | div = PWM_CTRL_CFG_SUB_DIV0; |
| 116 | timebase = DIV_ROUND_UP(mul, 8); |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 117 | } else if (mul <= max_timebase * 64) { |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 118 | div = PWM_CTRL_CFG_SUB_DIV1; |
| 119 | timebase = DIV_ROUND_UP(mul, 64); |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 120 | } else if (mul <= max_timebase * 512) { |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 121 | div = PWM_CTRL_CFG_SUB_DIV0_DIV1; |
| 122 | timebase = DIV_ROUND_UP(mul, 512); |
Nathan Chancellor | 4448195 | 2019-03-07 15:36:28 -0700 | [diff] [blame] | 123 | } else { |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 124 | dev_err(chip->dev, |
| 125 | "failed to configure timebase steps/divider value\n"); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | duty = DIV_ROUND_UP(timebase * duty_ns, period_ns); |
| 130 | |
Uwe Kleine-König | b6ce2af | 2021-11-12 20:00:15 +0100 | [diff] [blame] | 131 | ret = pm_runtime_resume_and_get(chip->dev); |
| 132 | if (ret < 0) |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 133 | return ret; |
| 134 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 135 | val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG); |
| 136 | val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm)); |
| 137 | val |= (div & PWM_CTRL_CFG_DIV_MASK) << |
| 138 | PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm); |
| 139 | img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val); |
| 140 | |
| 141 | val = (duty << PWM_CH_CFG_DUTY_SHIFT) | |
| 142 | (timebase << PWM_CH_CFG_TMBASE_SHIFT); |
| 143 | img_pwm_writel(pwm_chip, PWM_CH_CFG(pwm->hwpwm), val); |
| 144 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 145 | pm_runtime_mark_last_busy(chip->dev); |
| 146 | pm_runtime_put_autosuspend(chip->dev); |
| 147 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 152 | { |
| 153 | u32 val; |
| 154 | struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip); |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 155 | int ret; |
| 156 | |
Zou Wei | fde2529 | 2021-05-12 11:57:17 +0800 | [diff] [blame] | 157 | ret = pm_runtime_resume_and_get(chip->dev); |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 158 | if (ret < 0) |
| 159 | return ret; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 160 | |
| 161 | val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG); |
| 162 | val |= BIT(pwm->hwpwm); |
| 163 | img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val); |
| 164 | |
| 165 | regmap_update_bits(pwm_chip->periph_regs, PERIP_PWM_PDM_CONTROL, |
| 166 | PERIP_PWM_PDM_CONTROL_CH_MASK << |
| 167 | PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm), 0); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 173 | { |
| 174 | u32 val; |
| 175 | struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip); |
| 176 | |
| 177 | val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG); |
| 178 | val &= ~BIT(pwm->hwpwm); |
| 179 | img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val); |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 180 | |
| 181 | pm_runtime_mark_last_busy(chip->dev); |
| 182 | pm_runtime_put_autosuspend(chip->dev); |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 183 | } |
| 184 | |
Uwe Kleine-König | 0ee11b8 | 2021-10-29 12:56:17 +0200 | [diff] [blame] | 185 | static int img_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 186 | const struct pwm_state *state) |
| 187 | { |
| 188 | int err; |
| 189 | |
| 190 | if (state->polarity != PWM_POLARITY_NORMAL) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | if (!state->enabled) { |
| 194 | if (pwm->state.enabled) |
| 195 | img_pwm_disable(chip, pwm); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | err = img_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); |
| 201 | if (err) |
| 202 | return err; |
| 203 | |
| 204 | if (!pwm->state.enabled) |
| 205 | err = img_pwm_enable(chip, pwm); |
| 206 | |
| 207 | return err; |
| 208 | } |
| 209 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 210 | static const struct pwm_ops img_pwm_ops = { |
Uwe Kleine-König | 0ee11b8 | 2021-10-29 12:56:17 +0200 | [diff] [blame] | 211 | .apply = img_pwm_apply, |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 212 | .owner = THIS_MODULE, |
| 213 | }; |
| 214 | |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 215 | static const struct img_pwm_soc_data pistachio_pwm = { |
| 216 | .max_timebase = 255, |
| 217 | }; |
| 218 | |
| 219 | static const struct of_device_id img_pwm_of_match[] = { |
| 220 | { |
| 221 | .compatible = "img,pistachio-pwm", |
| 222 | .data = &pistachio_pwm, |
| 223 | }, |
| 224 | { } |
| 225 | }; |
| 226 | MODULE_DEVICE_TABLE(of, img_pwm_of_match); |
| 227 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 228 | static int img_pwm_runtime_suspend(struct device *dev) |
| 229 | { |
| 230 | struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev); |
| 231 | |
| 232 | clk_disable_unprepare(pwm_chip->pwm_clk); |
| 233 | clk_disable_unprepare(pwm_chip->sys_clk); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int img_pwm_runtime_resume(struct device *dev) |
| 239 | { |
| 240 | struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev); |
| 241 | int ret; |
| 242 | |
| 243 | ret = clk_prepare_enable(pwm_chip->sys_clk); |
| 244 | if (ret < 0) { |
| 245 | dev_err(dev, "could not prepare or enable sys clock\n"); |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | ret = clk_prepare_enable(pwm_chip->pwm_clk); |
| 250 | if (ret < 0) { |
| 251 | dev_err(dev, "could not prepare or enable pwm clock\n"); |
| 252 | clk_disable_unprepare(pwm_chip->sys_clk); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 259 | static int img_pwm_probe(struct platform_device *pdev) |
| 260 | { |
| 261 | int ret; |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 262 | u64 val; |
| 263 | unsigned long clk_rate; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 264 | struct img_pwm_chip *pwm; |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 265 | const struct of_device_id *of_dev_id; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 266 | |
| 267 | pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); |
| 268 | if (!pwm) |
| 269 | return -ENOMEM; |
| 270 | |
| 271 | pwm->dev = &pdev->dev; |
| 272 | |
Yangtao Li | d574ab6 | 2019-12-29 08:06:06 +0000 | [diff] [blame] | 273 | pwm->base = devm_platform_ioremap_resource(pdev, 0); |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 274 | if (IS_ERR(pwm->base)) |
| 275 | return PTR_ERR(pwm->base); |
| 276 | |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 277 | of_dev_id = of_match_device(img_pwm_of_match, &pdev->dev); |
| 278 | if (!of_dev_id) |
| 279 | return -ENODEV; |
| 280 | pwm->data = of_dev_id->data; |
| 281 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 282 | pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, |
| 283 | "img,cr-periph"); |
| 284 | if (IS_ERR(pwm->periph_regs)) |
| 285 | return PTR_ERR(pwm->periph_regs); |
| 286 | |
| 287 | pwm->sys_clk = devm_clk_get(&pdev->dev, "sys"); |
| 288 | if (IS_ERR(pwm->sys_clk)) { |
| 289 | dev_err(&pdev->dev, "failed to get system clock\n"); |
| 290 | return PTR_ERR(pwm->sys_clk); |
| 291 | } |
| 292 | |
| 293 | pwm->pwm_clk = devm_clk_get(&pdev->dev, "pwm"); |
| 294 | if (IS_ERR(pwm->pwm_clk)) { |
| 295 | dev_err(&pdev->dev, "failed to get pwm clock\n"); |
| 296 | return PTR_ERR(pwm->pwm_clk); |
| 297 | } |
| 298 | |
Hauke Mehrtens | b39c061 | 2020-08-20 19:14:25 +0200 | [diff] [blame] | 299 | platform_set_drvdata(pdev, pwm); |
| 300 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 301 | pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_PWM_PM_TIMEOUT); |
| 302 | pm_runtime_use_autosuspend(&pdev->dev); |
| 303 | pm_runtime_enable(&pdev->dev); |
| 304 | if (!pm_runtime_enabled(&pdev->dev)) { |
| 305 | ret = img_pwm_runtime_resume(&pdev->dev); |
| 306 | if (ret) |
| 307 | goto err_pm_disable; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 308 | } |
| 309 | |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 310 | clk_rate = clk_get_rate(pwm->pwm_clk); |
Wolfram Sang | bea307c | 2016-03-02 23:33:34 +0100 | [diff] [blame] | 311 | if (!clk_rate) { |
| 312 | dev_err(&pdev->dev, "pwm clock has no frequency\n"); |
| 313 | ret = -EINVAL; |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 314 | goto err_suspend; |
Wolfram Sang | bea307c | 2016-03-02 23:33:34 +0100 | [diff] [blame] | 315 | } |
Naidu Tellapati | 1e70897 | 2015-05-08 18:47:31 -0300 | [diff] [blame] | 316 | |
| 317 | /* The maximum input clock divider is 512 */ |
| 318 | val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase; |
| 319 | do_div(val, clk_rate); |
| 320 | pwm->max_period_ns = val; |
| 321 | |
| 322 | val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS; |
| 323 | do_div(val, clk_rate); |
| 324 | pwm->min_period_ns = val; |
| 325 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 326 | pwm->chip.dev = &pdev->dev; |
| 327 | pwm->chip.ops = &img_pwm_ops; |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 328 | pwm->chip.npwm = IMG_PWM_NPWM; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 329 | |
| 330 | ret = pwmchip_add(&pwm->chip); |
| 331 | if (ret < 0) { |
| 332 | dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret); |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 333 | goto err_suspend; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 334 | } |
| 335 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 336 | return 0; |
| 337 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 338 | err_suspend: |
| 339 | if (!pm_runtime_enabled(&pdev->dev)) |
| 340 | img_pwm_runtime_suspend(&pdev->dev); |
| 341 | err_pm_disable: |
| 342 | pm_runtime_disable(&pdev->dev); |
| 343 | pm_runtime_dont_use_autosuspend(&pdev->dev); |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | static int img_pwm_remove(struct platform_device *pdev) |
| 348 | { |
| 349 | struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev); |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 350 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 351 | pm_runtime_disable(&pdev->dev); |
| 352 | if (!pm_runtime_status_suspended(&pdev->dev)) |
| 353 | img_pwm_runtime_suspend(&pdev->dev); |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 354 | |
Uwe Kleine-König | fc3f3f5 | 2021-07-07 18:28:25 +0200 | [diff] [blame] | 355 | pwmchip_remove(&pwm_chip->chip); |
| 356 | |
| 357 | return 0; |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 358 | } |
| 359 | |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 360 | #ifdef CONFIG_PM_SLEEP |
| 361 | static int img_pwm_suspend(struct device *dev) |
| 362 | { |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 363 | struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev); |
| 364 | int i, ret; |
| 365 | |
| 366 | if (pm_runtime_status_suspended(dev)) { |
| 367 | ret = img_pwm_runtime_resume(dev); |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | } |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 371 | |
| 372 | for (i = 0; i < pwm_chip->chip.npwm; i++) |
| 373 | pwm_chip->suspend_ch_cfg[i] = img_pwm_readl(pwm_chip, |
| 374 | PWM_CH_CFG(i)); |
| 375 | |
| 376 | pwm_chip->suspend_ctrl_cfg = img_pwm_readl(pwm_chip, PWM_CTRL_CFG); |
| 377 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 378 | img_pwm_runtime_suspend(dev); |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int img_pwm_resume(struct device *dev) |
| 384 | { |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 385 | struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev); |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 386 | int ret; |
| 387 | int i; |
| 388 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 389 | ret = img_pwm_runtime_resume(dev); |
| 390 | if (ret) |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 391 | return ret; |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 392 | |
| 393 | for (i = 0; i < pwm_chip->chip.npwm; i++) |
| 394 | img_pwm_writel(pwm_chip, PWM_CH_CFG(i), |
| 395 | pwm_chip->suspend_ch_cfg[i]); |
| 396 | |
| 397 | img_pwm_writel(pwm_chip, PWM_CTRL_CFG, pwm_chip->suspend_ctrl_cfg); |
| 398 | |
| 399 | for (i = 0; i < pwm_chip->chip.npwm; i++) |
| 400 | if (pwm_chip->suspend_ctrl_cfg & BIT(i)) |
| 401 | regmap_update_bits(pwm_chip->periph_regs, |
| 402 | PERIP_PWM_PDM_CONTROL, |
| 403 | PERIP_PWM_PDM_CONTROL_CH_MASK << |
| 404 | PERIP_PWM_PDM_CONTROL_CH_SHIFT(i), |
| 405 | 0); |
| 406 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 407 | if (pm_runtime_status_suspended(dev)) |
| 408 | img_pwm_runtime_suspend(dev); |
| 409 | |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | #endif /* CONFIG_PM */ |
| 413 | |
Ed Blake | e690ae5 | 2017-10-02 10:51:48 +0100 | [diff] [blame] | 414 | static const struct dev_pm_ops img_pwm_pm_ops = { |
| 415 | SET_RUNTIME_PM_OPS(img_pwm_runtime_suspend, |
| 416 | img_pwm_runtime_resume, |
| 417 | NULL) |
| 418 | SET_SYSTEM_SLEEP_PM_OPS(img_pwm_suspend, img_pwm_resume) |
| 419 | }; |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 420 | |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 421 | static struct platform_driver img_pwm_driver = { |
| 422 | .driver = { |
| 423 | .name = "img-pwm", |
Ed Blake | a18afce | 2017-10-02 10:51:47 +0100 | [diff] [blame] | 424 | .pm = &img_pwm_pm_ops, |
Naidu Tellapati | 277bb6a | 2015-01-09 14:54:47 -0300 | [diff] [blame] | 425 | .of_match_table = img_pwm_of_match, |
| 426 | }, |
| 427 | .probe = img_pwm_probe, |
| 428 | .remove = img_pwm_remove, |
| 429 | }; |
| 430 | module_platform_driver(img_pwm_driver); |
| 431 | |
| 432 | MODULE_AUTHOR("Sai Masarapu <Sai.Masarapu@imgtec.com>"); |
| 433 | MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver"); |
| 434 | MODULE_LICENSE("GPL v2"); |