Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Marvell Berlin PWM driver |
| 3 | * |
| 4 | * Copyright (C) 2015 Marvell Technology Group Ltd. |
| 5 | * |
| 6 | * Author: Antoine Tenart <antoine.tenart@free-electrons.com> |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public |
| 9 | * License version 2. This program is licensed "as is" without any |
| 10 | * warranty of any kind, whether express or implied. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/pwm.h> |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 20 | |
| 21 | #define BERLIN_PWM_EN 0x0 |
| 22 | #define BERLIN_PWM_ENABLE BIT(0) |
| 23 | #define BERLIN_PWM_CONTROL 0x4 |
Thomas Hebb | 4de445c | 2018-06-06 13:42:10 -0400 | [diff] [blame] | 24 | /* |
| 25 | * The prescaler claims to support 8 different moduli, configured using the |
| 26 | * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64, |
| 27 | * 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be |
| 28 | * implemented by internally shifting TCNT left without adding additional |
| 29 | * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff; |
| 30 | * for 8, 0x1fff; and so on. This means that those moduli are entirely |
| 31 | * useless, as we could just do the shift ourselves. The 4096 modulus is |
| 32 | * implemented with a real prescaler, so we do use that, but we treat it |
| 33 | * as a flag instead of pretending the modulus is actually configurable. |
| 34 | */ |
| 35 | #define BERLIN_PWM_PRESCALE_4096 0x7 |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 36 | #define BERLIN_PWM_INVERT_POLARITY BIT(3) |
| 37 | #define BERLIN_PWM_DUTY 0x8 |
| 38 | #define BERLIN_PWM_TCNT 0xc |
| 39 | #define BERLIN_PWM_MAX_TCNT 65535 |
| 40 | |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 41 | struct berlin_pwm_channel { |
| 42 | u32 enable; |
| 43 | u32 ctrl; |
| 44 | u32 duty; |
| 45 | u32 tcnt; |
| 46 | }; |
| 47 | |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 48 | struct berlin_pwm_chip { |
| 49 | struct pwm_chip chip; |
| 50 | struct clk *clk; |
| 51 | void __iomem *base; |
| 52 | }; |
| 53 | |
| 54 | static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip) |
| 55 | { |
| 56 | return container_of(chip, struct berlin_pwm_chip, chip); |
| 57 | } |
| 58 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 59 | static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc, |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 60 | unsigned int channel, unsigned long offset) |
| 61 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 62 | return readl_relaxed(bpc->base + channel * 0x10 + offset); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 65 | static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc, |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 66 | unsigned int channel, u32 value, |
| 67 | unsigned long offset) |
| 68 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 69 | writel_relaxed(value, bpc->base + channel * 0x10 + offset); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 72 | static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 73 | { |
| 74 | struct berlin_pwm_channel *channel; |
| 75 | |
| 76 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
| 77 | if (!channel) |
| 78 | return -ENOMEM; |
| 79 | |
| 80 | return pwm_set_chip_data(pwm, channel); |
| 81 | } |
| 82 | |
| 83 | static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 84 | { |
| 85 | struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm); |
| 86 | |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 87 | kfree(channel); |
| 88 | } |
| 89 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 90 | static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
Uwe Kleine-König | 30dffb4 | 2021-05-04 15:25:36 +0200 | [diff] [blame] | 91 | u64 duty_ns, u64 period_ns) |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 92 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 93 | struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip); |
Thomas Hebb | 4de445c | 2018-06-06 13:42:10 -0400 | [diff] [blame] | 94 | bool prescale_4096 = false; |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 95 | u32 value, duty, period; |
Thomas Hebb | 4de445c | 2018-06-06 13:42:10 -0400 | [diff] [blame] | 96 | u64 cycles; |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 97 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 98 | cycles = clk_get_rate(bpc->clk); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 99 | cycles *= period_ns; |
| 100 | do_div(cycles, NSEC_PER_SEC); |
| 101 | |
Thomas Hebb | 4de445c | 2018-06-06 13:42:10 -0400 | [diff] [blame] | 102 | if (cycles > BERLIN_PWM_MAX_TCNT) { |
| 103 | prescale_4096 = true; |
| 104 | cycles >>= 12; // Prescaled by 4096 |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 105 | |
Thomas Hebb | 4de445c | 2018-06-06 13:42:10 -0400 | [diff] [blame] | 106 | if (cycles > BERLIN_PWM_MAX_TCNT) |
| 107 | return -ERANGE; |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Thomas Hebb | 4de445c | 2018-06-06 13:42:10 -0400 | [diff] [blame] | 110 | period = cycles; |
| 111 | cycles *= duty_ns; |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 112 | do_div(cycles, period_ns); |
| 113 | duty = cycles; |
| 114 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 115 | value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL); |
Thomas Hebb | 4de445c | 2018-06-06 13:42:10 -0400 | [diff] [blame] | 116 | if (prescale_4096) |
| 117 | value |= BERLIN_PWM_PRESCALE_4096; |
| 118 | else |
| 119 | value &= ~BERLIN_PWM_PRESCALE_4096; |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 120 | berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 121 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 122 | berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY); |
| 123 | berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int berlin_pwm_set_polarity(struct pwm_chip *chip, |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 129 | struct pwm_device *pwm, |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 130 | enum pwm_polarity polarity) |
| 131 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 132 | struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 133 | u32 value; |
| 134 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 135 | value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 136 | |
| 137 | if (polarity == PWM_POLARITY_NORMAL) |
| 138 | value &= ~BERLIN_PWM_INVERT_POLARITY; |
| 139 | else |
| 140 | value |= BERLIN_PWM_INVERT_POLARITY; |
| 141 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 142 | berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 147 | static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 148 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 149 | struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 150 | u32 value; |
| 151 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 152 | value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 153 | value |= BERLIN_PWM_ENABLE; |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 154 | berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static void berlin_pwm_disable(struct pwm_chip *chip, |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 160 | struct pwm_device *pwm) |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 161 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 162 | struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 163 | u32 value; |
| 164 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 165 | value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 166 | value &= ~BERLIN_PWM_ENABLE; |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 167 | berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Uwe Kleine-König | 30dffb4 | 2021-05-04 15:25:36 +0200 | [diff] [blame] | 170 | static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 171 | const struct pwm_state *state) |
| 172 | { |
| 173 | int err; |
| 174 | bool enabled = pwm->state.enabled; |
| 175 | |
| 176 | if (state->polarity != pwm->state.polarity) { |
| 177 | if (enabled) { |
| 178 | berlin_pwm_disable(chip, pwm); |
| 179 | enabled = false; |
| 180 | } |
| 181 | |
| 182 | err = berlin_pwm_set_polarity(chip, pwm, state->polarity); |
| 183 | if (err) |
| 184 | return err; |
| 185 | } |
| 186 | |
| 187 | if (!state->enabled) { |
| 188 | if (enabled) |
| 189 | berlin_pwm_disable(chip, pwm); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | if (state->period != pwm->state.period || |
| 194 | state->duty_cycle != pwm->state.duty_cycle) { |
| 195 | err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period); |
| 196 | if (err) |
| 197 | return err; |
| 198 | } |
| 199 | |
| 200 | if (!enabled) |
| 201 | return berlin_pwm_enable(chip, pwm); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 206 | static const struct pwm_ops berlin_pwm_ops = { |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 207 | .request = berlin_pwm_request, |
| 208 | .free = berlin_pwm_free, |
Uwe Kleine-König | 30dffb4 | 2021-05-04 15:25:36 +0200 | [diff] [blame] | 209 | .apply = berlin_pwm_apply, |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 210 | .owner = THIS_MODULE, |
| 211 | }; |
| 212 | |
| 213 | static const struct of_device_id berlin_pwm_match[] = { |
| 214 | { .compatible = "marvell,berlin-pwm" }, |
| 215 | { }, |
| 216 | }; |
| 217 | MODULE_DEVICE_TABLE(of, berlin_pwm_match); |
| 218 | |
| 219 | static int berlin_pwm_probe(struct platform_device *pdev) |
| 220 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 221 | struct berlin_pwm_chip *bpc; |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 222 | int ret; |
| 223 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 224 | bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL); |
| 225 | if (!bpc) |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 226 | return -ENOMEM; |
| 227 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 228 | bpc->base = devm_platform_ioremap_resource(pdev, 0); |
| 229 | if (IS_ERR(bpc->base)) |
| 230 | return PTR_ERR(bpc->base); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 231 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 232 | bpc->clk = devm_clk_get(&pdev->dev, NULL); |
| 233 | if (IS_ERR(bpc->clk)) |
| 234 | return PTR_ERR(bpc->clk); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 235 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 236 | ret = clk_prepare_enable(bpc->clk); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 237 | if (ret) |
| 238 | return ret; |
| 239 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 240 | bpc->chip.dev = &pdev->dev; |
| 241 | bpc->chip.ops = &berlin_pwm_ops; |
| 242 | bpc->chip.npwm = 4; |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 243 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 244 | ret = pwmchip_add(&bpc->chip); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 245 | if (ret < 0) { |
| 246 | dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 247 | clk_disable_unprepare(bpc->clk); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 251 | platform_set_drvdata(pdev, bpc); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int berlin_pwm_remove(struct platform_device *pdev) |
| 257 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 258 | struct berlin_pwm_chip *bpc = platform_get_drvdata(pdev); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 259 | |
Uwe Kleine-König | 0512f05 | 2021-05-04 15:25:37 +0200 | [diff] [blame] | 260 | pwmchip_remove(&bpc->chip); |
| 261 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 262 | clk_disable_unprepare(bpc->clk); |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 263 | |
Uwe Kleine-König | 0512f05 | 2021-05-04 15:25:37 +0200 | [diff] [blame] | 264 | return 0; |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 267 | #ifdef CONFIG_PM_SLEEP |
| 268 | static int berlin_pwm_suspend(struct device *dev) |
| 269 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 270 | struct berlin_pwm_chip *bpc = dev_get_drvdata(dev); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 271 | unsigned int i; |
| 272 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 273 | for (i = 0; i < bpc->chip.npwm; i++) { |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 274 | struct berlin_pwm_channel *channel; |
| 275 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 276 | channel = pwm_get_chip_data(&bpc->chip.pwms[i]); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 277 | if (!channel) |
| 278 | continue; |
| 279 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 280 | channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE); |
| 281 | channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL); |
| 282 | channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY); |
| 283 | channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 284 | } |
| 285 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 286 | clk_disable_unprepare(bpc->clk); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static int berlin_pwm_resume(struct device *dev) |
| 292 | { |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 293 | struct berlin_pwm_chip *bpc = dev_get_drvdata(dev); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 294 | unsigned int i; |
| 295 | int ret; |
| 296 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 297 | ret = clk_prepare_enable(bpc->clk); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 298 | if (ret) |
| 299 | return ret; |
| 300 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 301 | for (i = 0; i < bpc->chip.npwm; i++) { |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 302 | struct berlin_pwm_channel *channel; |
| 303 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 304 | channel = pwm_get_chip_data(&bpc->chip.pwms[i]); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 305 | if (!channel) |
| 306 | continue; |
| 307 | |
Uwe Kleine-König | 3f3e805 | 2021-05-04 15:25:34 +0200 | [diff] [blame] | 308 | berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL); |
| 309 | berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY); |
| 310 | berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT); |
| 311 | berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE); |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | #endif |
| 317 | |
| 318 | static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend, |
| 319 | berlin_pwm_resume); |
| 320 | |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 321 | static struct platform_driver berlin_pwm_driver = { |
| 322 | .probe = berlin_pwm_probe, |
| 323 | .remove = berlin_pwm_remove, |
| 324 | .driver = { |
| 325 | .name = "berlin-pwm", |
| 326 | .of_match_table = berlin_pwm_match, |
Jisheng Zhang | bbf0722 | 2015-11-25 17:41:25 +0800 | [diff] [blame] | 327 | .pm = &berlin_pwm_pm_ops, |
Antoine Ténart | 59d5c8b | 2015-10-02 16:59:47 +0200 | [diff] [blame] | 328 | }, |
| 329 | }; |
| 330 | module_platform_driver(berlin_pwm_driver); |
| 331 | |
| 332 | MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>"); |
| 333 | MODULE_DESCRIPTION("Marvell Berlin PWM driver"); |
| 334 | MODULE_LICENSE("GPL v2"); |