Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PWM driver for Rockchip SoCs |
| 3 | * |
| 4 | * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com> |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 5 | * Copyright (C) 2014 ROCKCHIP, Inc. |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/clk.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of.h> |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 16 | #include <linux/of_device.h> |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/pwm.h> |
| 19 | #include <linux/time.h> |
| 20 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 21 | #define PWM_CTRL_TIMER_EN (1 << 0) |
| 22 | #define PWM_CTRL_OUTPUT_EN (1 << 3) |
| 23 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 24 | #define PWM_ENABLE (1 << 0) |
| 25 | #define PWM_CONTINUOUS (1 << 1) |
| 26 | #define PWM_DUTY_POSITIVE (1 << 3) |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 27 | #define PWM_DUTY_NEGATIVE (0 << 3) |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 28 | #define PWM_INACTIVE_NEGATIVE (0 << 4) |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 29 | #define PWM_INACTIVE_POSITIVE (1 << 4) |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 30 | #define PWM_OUTPUT_LEFT (0 << 5) |
| 31 | #define PWM_LP_DISABLE (0 << 8) |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 32 | |
| 33 | struct rockchip_pwm_chip { |
| 34 | struct pwm_chip chip; |
| 35 | struct clk *clk; |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 36 | struct clk *pclk; |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 37 | const struct rockchip_pwm_data *data; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 38 | void __iomem *base; |
| 39 | }; |
| 40 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 41 | struct rockchip_pwm_regs { |
| 42 | unsigned long duty; |
| 43 | unsigned long period; |
| 44 | unsigned long cntr; |
| 45 | unsigned long ctrl; |
| 46 | }; |
| 47 | |
| 48 | struct rockchip_pwm_data { |
| 49 | struct rockchip_pwm_regs regs; |
| 50 | unsigned int prescaler; |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 51 | bool supports_polarity; |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 52 | const struct pwm_ops *ops; |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 53 | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 54 | void (*set_enable)(struct pwm_chip *chip, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 55 | struct pwm_device *pwm, bool enable, |
| 56 | enum pwm_polarity polarity); |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 57 | void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, |
| 58 | struct pwm_state *state); |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 59 | }; |
| 60 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 61 | static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c) |
| 62 | { |
| 63 | return container_of(c, struct rockchip_pwm_chip, chip); |
| 64 | } |
| 65 | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 66 | static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 67 | struct pwm_device *pwm, bool enable, |
| 68 | enum pwm_polarity polarity) |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 69 | { |
| 70 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 71 | u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN; |
| 72 | u32 val; |
| 73 | |
| 74 | val = readl_relaxed(pc->base + pc->data->regs.ctrl); |
| 75 | |
| 76 | if (enable) |
| 77 | val |= enable_conf; |
| 78 | else |
| 79 | val &= ~enable_conf; |
| 80 | |
| 81 | writel_relaxed(val, pc->base + pc->data->regs.ctrl); |
| 82 | } |
| 83 | |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 84 | static void rockchip_pwm_get_state_v1(struct pwm_chip *chip, |
| 85 | struct pwm_device *pwm, |
| 86 | struct pwm_state *state) |
| 87 | { |
| 88 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 89 | u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN; |
| 90 | u32 val; |
| 91 | |
| 92 | val = readl_relaxed(pc->base + pc->data->regs.ctrl); |
| 93 | if ((val & enable_conf) == enable_conf) |
| 94 | state->enabled = true; |
| 95 | } |
| 96 | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 97 | static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 98 | struct pwm_device *pwm, bool enable, |
| 99 | enum pwm_polarity polarity) |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 100 | { |
| 101 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 102 | u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 103 | PWM_CONTINUOUS; |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 104 | u32 val; |
| 105 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 106 | if (polarity == PWM_POLARITY_INVERSED) |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 107 | enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE; |
| 108 | else |
| 109 | enable_conf |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE; |
| 110 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 111 | val = readl_relaxed(pc->base + pc->data->regs.ctrl); |
| 112 | |
| 113 | if (enable) |
| 114 | val |= enable_conf; |
| 115 | else |
| 116 | val &= ~enable_conf; |
| 117 | |
| 118 | writel_relaxed(val, pc->base + pc->data->regs.ctrl); |
| 119 | } |
| 120 | |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 121 | static void rockchip_pwm_get_state_v2(struct pwm_chip *chip, |
| 122 | struct pwm_device *pwm, |
| 123 | struct pwm_state *state) |
| 124 | { |
| 125 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 126 | u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE | |
| 127 | PWM_CONTINUOUS; |
| 128 | u32 val; |
| 129 | |
| 130 | val = readl_relaxed(pc->base + pc->data->regs.ctrl); |
| 131 | if ((val & enable_conf) != enable_conf) |
| 132 | return; |
| 133 | |
| 134 | state->enabled = true; |
| 135 | |
| 136 | if (!(val & PWM_DUTY_POSITIVE)) |
| 137 | state->polarity = PWM_POLARITY_INVERSED; |
| 138 | } |
| 139 | |
| 140 | static void rockchip_pwm_get_state(struct pwm_chip *chip, |
| 141 | struct pwm_device *pwm, |
| 142 | struct pwm_state *state) |
| 143 | { |
| 144 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 145 | unsigned long clk_rate; |
| 146 | u64 tmp; |
| 147 | int ret; |
| 148 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 149 | ret = clk_enable(pc->pclk); |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 150 | if (ret) |
| 151 | return; |
| 152 | |
| 153 | clk_rate = clk_get_rate(pc->clk); |
| 154 | |
| 155 | tmp = readl_relaxed(pc->base + pc->data->regs.period); |
| 156 | tmp *= pc->data->prescaler * NSEC_PER_SEC; |
| 157 | state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); |
| 158 | |
| 159 | tmp = readl_relaxed(pc->base + pc->data->regs.duty); |
| 160 | tmp *= pc->data->prescaler * NSEC_PER_SEC; |
| 161 | state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); |
| 162 | |
| 163 | pc->data->get_state(chip, pwm, state); |
| 164 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 165 | clk_disable(pc->pclk); |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 168 | static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
| 169 | int duty_ns, int period_ns) |
| 170 | { |
| 171 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 172 | unsigned long period, duty; |
| 173 | u64 clk_rate, div; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 174 | |
| 175 | clk_rate = clk_get_rate(pc->clk); |
| 176 | |
| 177 | /* |
| 178 | * Since period and duty cycle registers have a width of 32 |
| 179 | * bits, every possible input period can be obtained using the |
| 180 | * default prescaler value for all practical clock rate values. |
| 181 | */ |
| 182 | div = clk_rate * period_ns; |
Boris Brezillon | 12f9ce4 | 2016-06-14 11:13:11 +0200 | [diff] [blame] | 183 | period = DIV_ROUND_CLOSEST_ULL(div, |
| 184 | pc->data->prescaler * NSEC_PER_SEC); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 185 | |
| 186 | div = clk_rate * duty_ns; |
Boris Brezillon | 12f9ce4 | 2016-06-14 11:13:11 +0200 | [diff] [blame] | 187 | duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 188 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 189 | writel(period, pc->base + pc->data->regs.period); |
| 190 | writel(duty, pc->base + pc->data->regs.duty); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
David Wu | a900152 | 2017-03-01 19:10:55 +0800 | [diff] [blame] | 195 | static int rockchip_pwm_enable(struct pwm_chip *chip, |
| 196 | struct pwm_device *pwm, |
| 197 | bool enable, |
| 198 | enum pwm_polarity polarity) |
| 199 | { |
| 200 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 201 | int ret; |
| 202 | |
| 203 | if (enable) { |
| 204 | ret = clk_enable(pc->clk); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | pc->data->set_enable(chip, pwm, enable, polarity); |
| 210 | |
| 211 | if (!enable) |
| 212 | clk_disable(pc->clk); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 217 | static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 218 | struct pwm_state *state) |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 219 | { |
| 220 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 221 | struct pwm_state curstate; |
| 222 | bool enabled; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 223 | int ret; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 224 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 225 | pwm_get_state(pwm, &curstate); |
| 226 | enabled = curstate.enabled; |
| 227 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 228 | ret = clk_enable(pc->pclk); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 229 | if (ret) |
| 230 | return ret; |
| 231 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 232 | if (state->polarity != curstate.polarity && enabled) { |
David Wu | a900152 | 2017-03-01 19:10:55 +0800 | [diff] [blame] | 233 | ret = rockchip_pwm_enable(chip, pwm, false, state->polarity); |
| 234 | if (ret) |
| 235 | goto out; |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 236 | enabled = false; |
| 237 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 238 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 239 | ret = rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period); |
| 240 | if (ret) { |
| 241 | if (enabled != curstate.enabled) |
David Wu | a900152 | 2017-03-01 19:10:55 +0800 | [diff] [blame] | 242 | rockchip_pwm_enable(chip, pwm, !enabled, |
| 243 | state->polarity); |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 244 | goto out; |
| 245 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 246 | |
David Wu | a900152 | 2017-03-01 19:10:55 +0800 | [diff] [blame] | 247 | if (state->enabled != enabled) { |
| 248 | ret = rockchip_pwm_enable(chip, pwm, state->enabled, |
| 249 | state->polarity); |
| 250 | if (ret) |
| 251 | goto out; |
| 252 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 253 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 254 | /* |
| 255 | * Update the state with the real hardware, which can differ a bit |
| 256 | * because of period/duty_cycle approximation. |
| 257 | */ |
| 258 | rockchip_pwm_get_state(chip, pwm, state); |
| 259 | |
| 260 | out: |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 261 | clk_disable(pc->pclk); |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 262 | |
| 263 | return ret; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 266 | static const struct pwm_ops rockchip_pwm_ops_v1 = { |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 267 | .get_state = rockchip_pwm_get_state, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 268 | .apply = rockchip_pwm_apply, |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 269 | .owner = THIS_MODULE, |
| 270 | }; |
| 271 | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 272 | static const struct pwm_ops rockchip_pwm_ops_v2 = { |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 273 | .get_state = rockchip_pwm_get_state, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 274 | .apply = rockchip_pwm_apply, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 275 | .owner = THIS_MODULE, |
| 276 | }; |
| 277 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 278 | static const struct rockchip_pwm_data pwm_data_v1 = { |
| 279 | .regs = { |
| 280 | .duty = 0x04, |
| 281 | .period = 0x08, |
| 282 | .cntr = 0x00, |
| 283 | .ctrl = 0x0c, |
| 284 | }, |
| 285 | .prescaler = 2, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 286 | .ops = &rockchip_pwm_ops_v1, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 287 | .set_enable = rockchip_pwm_set_enable_v1, |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 288 | .get_state = rockchip_pwm_get_state_v1, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | static const struct rockchip_pwm_data pwm_data_v2 = { |
| 292 | .regs = { |
| 293 | .duty = 0x08, |
| 294 | .period = 0x04, |
| 295 | .cntr = 0x00, |
| 296 | .ctrl = 0x0c, |
| 297 | }, |
| 298 | .prescaler = 1, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 299 | .supports_polarity = true, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 300 | .ops = &rockchip_pwm_ops_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 301 | .set_enable = rockchip_pwm_set_enable_v2, |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 302 | .get_state = rockchip_pwm_get_state_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | static const struct rockchip_pwm_data pwm_data_vop = { |
| 306 | .regs = { |
| 307 | .duty = 0x08, |
| 308 | .period = 0x04, |
| 309 | .cntr = 0x0c, |
| 310 | .ctrl = 0x00, |
| 311 | }, |
| 312 | .prescaler = 1, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 313 | .supports_polarity = true, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 314 | .ops = &rockchip_pwm_ops_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 315 | .set_enable = rockchip_pwm_set_enable_v2, |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 316 | .get_state = rockchip_pwm_get_state_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | static const struct of_device_id rockchip_pwm_dt_ids[] = { |
| 320 | { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1}, |
| 321 | { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2}, |
| 322 | { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop}, |
| 323 | { /* sentinel */ } |
| 324 | }; |
| 325 | MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids); |
| 326 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 327 | static int rockchip_pwm_probe(struct platform_device *pdev) |
| 328 | { |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 329 | const struct of_device_id *id; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 330 | struct rockchip_pwm_chip *pc; |
| 331 | struct resource *r; |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 332 | int ret, count; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 333 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 334 | id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev); |
| 335 | if (!id) |
| 336 | return -EINVAL; |
| 337 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 338 | pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); |
| 339 | if (!pc) |
| 340 | return -ENOMEM; |
| 341 | |
| 342 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 343 | pc->base = devm_ioremap_resource(&pdev->dev, r); |
| 344 | if (IS_ERR(pc->base)) |
| 345 | return PTR_ERR(pc->base); |
| 346 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 347 | pc->clk = devm_clk_get(&pdev->dev, "pwm"); |
| 348 | if (IS_ERR(pc->clk)) { |
| 349 | pc->clk = devm_clk_get(&pdev->dev, NULL); |
| 350 | if (IS_ERR(pc->clk)) { |
| 351 | ret = PTR_ERR(pc->clk); |
| 352 | if (ret != -EPROBE_DEFER) |
| 353 | dev_err(&pdev->dev, "Can't get bus clk: %d\n", |
| 354 | ret); |
| 355 | return ret; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | count = of_count_phandle_with_args(pdev->dev.of_node, |
| 360 | "clocks", "#clock-cells"); |
| 361 | if (count == 2) |
| 362 | pc->pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 363 | else |
| 364 | pc->pclk = pc->clk; |
| 365 | |
| 366 | if (IS_ERR(pc->pclk)) { |
| 367 | ret = PTR_ERR(pc->pclk); |
| 368 | if (ret != -EPROBE_DEFER) |
| 369 | dev_err(&pdev->dev, "Can't get APB clk: %d\n", ret); |
| 370 | return ret; |
| 371 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 372 | |
Boris Brezillon | 48cf973 | 2016-06-14 11:13:13 +0200 | [diff] [blame] | 373 | ret = clk_prepare_enable(pc->clk); |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 374 | if (ret) { |
| 375 | dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 376 | return ret; |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 377 | } |
| 378 | |
| 379 | ret = clk_prepare(pc->pclk); |
| 380 | if (ret) { |
| 381 | dev_err(&pdev->dev, "Can't prepare APB clk: %d\n", ret); |
| 382 | goto err_clk; |
| 383 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 384 | |
| 385 | platform_set_drvdata(pdev, pc); |
| 386 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 387 | pc->data = id->data; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 388 | pc->chip.dev = &pdev->dev; |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 389 | pc->chip.ops = pc->data->ops; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 390 | pc->chip.base = -1; |
| 391 | pc->chip.npwm = 1; |
| 392 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 393 | if (pc->data->supports_polarity) { |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 394 | pc->chip.of_xlate = of_pwm_xlate_with_flags; |
| 395 | pc->chip.of_pwm_n_cells = 3; |
| 396 | } |
| 397 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 398 | ret = pwmchip_add(&pc->chip); |
| 399 | if (ret < 0) { |
| 400 | clk_unprepare(pc->clk); |
| 401 | dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 402 | goto err_pclk; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Boris Brezillon | 48cf973 | 2016-06-14 11:13:13 +0200 | [diff] [blame] | 405 | /* Keep the PWM clk enabled if the PWM appears to be up and running. */ |
| 406 | if (!pwm_is_enabled(pc->chip.pwms)) |
| 407 | clk_disable(pc->clk); |
| 408 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 409 | return 0; |
| 410 | |
| 411 | err_pclk: |
| 412 | clk_unprepare(pc->pclk); |
| 413 | err_clk: |
| 414 | clk_disable_unprepare(pc->clk); |
| 415 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | static int rockchip_pwm_remove(struct platform_device *pdev) |
| 420 | { |
| 421 | struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev); |
| 422 | |
Boris Brezillon | 48cf973 | 2016-06-14 11:13:13 +0200 | [diff] [blame] | 423 | /* |
| 424 | * Disable the PWM clk before unpreparing it if the PWM device is still |
| 425 | * running. This should only happen when the last PWM user left it |
| 426 | * enabled, or when nobody requested a PWM that was previously enabled |
| 427 | * by the bootloader. |
| 428 | * |
| 429 | * FIXME: Maybe the core should disable all PWM devices in |
| 430 | * pwmchip_remove(). In this case we'd only have to call |
| 431 | * clk_unprepare() after pwmchip_remove(). |
| 432 | * |
| 433 | */ |
| 434 | if (pwm_is_enabled(pc->chip.pwms)) |
| 435 | clk_disable(pc->clk); |
| 436 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame^] | 437 | clk_unprepare(pc->pclk); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 438 | clk_unprepare(pc->clk); |
| 439 | |
| 440 | return pwmchip_remove(&pc->chip); |
| 441 | } |
| 442 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 443 | static struct platform_driver rockchip_pwm_driver = { |
| 444 | .driver = { |
| 445 | .name = "rockchip-pwm", |
| 446 | .of_match_table = rockchip_pwm_dt_ids, |
| 447 | }, |
| 448 | .probe = rockchip_pwm_probe, |
| 449 | .remove = rockchip_pwm_remove, |
| 450 | }; |
| 451 | module_platform_driver(rockchip_pwm_driver); |
| 452 | |
| 453 | MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>"); |
| 454 | MODULE_DESCRIPTION("Rockchip SoC PWM driver"); |
| 455 | MODULE_LICENSE("GPL v2"); |