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 | |
David Wu | f90df9c | 2017-08-08 23:38:30 +0800 | [diff] [blame^] | 168 | static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 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 | |
David Wu | a900152 | 2017-03-01 19:10:55 +0800 | [diff] [blame] | 193 | static int rockchip_pwm_enable(struct pwm_chip *chip, |
| 194 | struct pwm_device *pwm, |
| 195 | bool enable, |
| 196 | enum pwm_polarity polarity) |
| 197 | { |
| 198 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
| 199 | int ret; |
| 200 | |
| 201 | if (enable) { |
| 202 | ret = clk_enable(pc->clk); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | pc->data->set_enable(chip, pwm, enable, polarity); |
| 208 | |
| 209 | if (!enable) |
| 210 | clk_disable(pc->clk); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 215 | static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 216 | struct pwm_state *state) |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 217 | { |
| 218 | struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 219 | struct pwm_state curstate; |
| 220 | bool enabled; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 221 | int ret; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 222 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 223 | pwm_get_state(pwm, &curstate); |
| 224 | enabled = curstate.enabled; |
| 225 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 226 | ret = clk_enable(pc->pclk); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 227 | if (ret) |
| 228 | return ret; |
| 229 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 230 | if (state->polarity != curstate.polarity && enabled) { |
David Wu | a900152 | 2017-03-01 19:10:55 +0800 | [diff] [blame] | 231 | ret = rockchip_pwm_enable(chip, pwm, false, state->polarity); |
| 232 | if (ret) |
| 233 | goto out; |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 234 | enabled = false; |
| 235 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 236 | |
David Wu | f90df9c | 2017-08-08 23:38:30 +0800 | [diff] [blame^] | 237 | rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 238 | |
David Wu | a900152 | 2017-03-01 19:10:55 +0800 | [diff] [blame] | 239 | if (state->enabled != enabled) { |
| 240 | ret = rockchip_pwm_enable(chip, pwm, state->enabled, |
| 241 | state->polarity); |
| 242 | if (ret) |
| 243 | goto out; |
| 244 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 245 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 246 | /* |
| 247 | * Update the state with the real hardware, which can differ a bit |
| 248 | * because of period/duty_cycle approximation. |
| 249 | */ |
| 250 | rockchip_pwm_get_state(chip, pwm, state); |
| 251 | |
| 252 | out: |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 253 | clk_disable(pc->pclk); |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 254 | |
| 255 | return ret; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 258 | static const struct pwm_ops rockchip_pwm_ops_v1 = { |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 259 | .get_state = rockchip_pwm_get_state, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 260 | .apply = rockchip_pwm_apply, |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 261 | .owner = THIS_MODULE, |
| 262 | }; |
| 263 | |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 264 | static const struct pwm_ops rockchip_pwm_ops_v2 = { |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 265 | .get_state = rockchip_pwm_get_state, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 266 | .apply = rockchip_pwm_apply, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 267 | .owner = THIS_MODULE, |
| 268 | }; |
| 269 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 270 | static const struct rockchip_pwm_data pwm_data_v1 = { |
| 271 | .regs = { |
| 272 | .duty = 0x04, |
| 273 | .period = 0x08, |
| 274 | .cntr = 0x00, |
| 275 | .ctrl = 0x0c, |
| 276 | }, |
| 277 | .prescaler = 2, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 278 | .ops = &rockchip_pwm_ops_v1, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 279 | .set_enable = rockchip_pwm_set_enable_v1, |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 280 | .get_state = rockchip_pwm_get_state_v1, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static const struct rockchip_pwm_data pwm_data_v2 = { |
| 284 | .regs = { |
| 285 | .duty = 0x08, |
| 286 | .period = 0x04, |
| 287 | .cntr = 0x00, |
| 288 | .ctrl = 0x0c, |
| 289 | }, |
| 290 | .prescaler = 1, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 291 | .supports_polarity = true, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 292 | .ops = &rockchip_pwm_ops_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 293 | .set_enable = rockchip_pwm_set_enable_v2, |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 294 | .get_state = rockchip_pwm_get_state_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | static const struct rockchip_pwm_data pwm_data_vop = { |
| 298 | .regs = { |
| 299 | .duty = 0x08, |
| 300 | .period = 0x04, |
| 301 | .cntr = 0x0c, |
| 302 | .ctrl = 0x00, |
| 303 | }, |
| 304 | .prescaler = 1, |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 305 | .supports_polarity = true, |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 306 | .ops = &rockchip_pwm_ops_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 307 | .set_enable = rockchip_pwm_set_enable_v2, |
Boris Brezillon | 1ebb74c | 2016-06-14 11:13:12 +0200 | [diff] [blame] | 308 | .get_state = rockchip_pwm_get_state_v2, |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | static const struct of_device_id rockchip_pwm_dt_ids[] = { |
| 312 | { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1}, |
| 313 | { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2}, |
| 314 | { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop}, |
| 315 | { /* sentinel */ } |
| 316 | }; |
| 317 | MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids); |
| 318 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 319 | static int rockchip_pwm_probe(struct platform_device *pdev) |
| 320 | { |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 321 | const struct of_device_id *id; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 322 | struct rockchip_pwm_chip *pc; |
| 323 | struct resource *r; |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 324 | int ret, count; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 325 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 326 | id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev); |
| 327 | if (!id) |
| 328 | return -EINVAL; |
| 329 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 330 | pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); |
| 331 | if (!pc) |
| 332 | return -ENOMEM; |
| 333 | |
| 334 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 335 | pc->base = devm_ioremap_resource(&pdev->dev, r); |
| 336 | if (IS_ERR(pc->base)) |
| 337 | return PTR_ERR(pc->base); |
| 338 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 339 | pc->clk = devm_clk_get(&pdev->dev, "pwm"); |
| 340 | if (IS_ERR(pc->clk)) { |
| 341 | pc->clk = devm_clk_get(&pdev->dev, NULL); |
| 342 | if (IS_ERR(pc->clk)) { |
| 343 | ret = PTR_ERR(pc->clk); |
| 344 | if (ret != -EPROBE_DEFER) |
| 345 | dev_err(&pdev->dev, "Can't get bus clk: %d\n", |
| 346 | ret); |
| 347 | return ret; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | count = of_count_phandle_with_args(pdev->dev.of_node, |
| 352 | "clocks", "#clock-cells"); |
| 353 | if (count == 2) |
| 354 | pc->pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 355 | else |
| 356 | pc->pclk = pc->clk; |
| 357 | |
| 358 | if (IS_ERR(pc->pclk)) { |
| 359 | ret = PTR_ERR(pc->pclk); |
| 360 | if (ret != -EPROBE_DEFER) |
| 361 | dev_err(&pdev->dev, "Can't get APB clk: %d\n", ret); |
| 362 | return ret; |
| 363 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 364 | |
Boris Brezillon | 48cf973 | 2016-06-14 11:13:13 +0200 | [diff] [blame] | 365 | ret = clk_prepare_enable(pc->clk); |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 366 | if (ret) { |
| 367 | 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] | 368 | return ret; |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | ret = clk_prepare(pc->pclk); |
| 372 | if (ret) { |
| 373 | dev_err(&pdev->dev, "Can't prepare APB clk: %d\n", ret); |
| 374 | goto err_clk; |
| 375 | } |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 376 | |
| 377 | platform_set_drvdata(pdev, pc); |
| 378 | |
Caesar Wang | f630629 | 2014-08-08 15:28:49 +0800 | [diff] [blame] | 379 | pc->data = id->data; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 380 | pc->chip.dev = &pdev->dev; |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 381 | pc->chip.ops = pc->data->ops; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 382 | pc->chip.base = -1; |
| 383 | pc->chip.npwm = 1; |
| 384 | |
Boris Brezillon | 2bf1c98 | 2016-06-14 11:13:14 +0200 | [diff] [blame] | 385 | if (pc->data->supports_polarity) { |
Doug Anderson | 7264354 | 2014-08-25 15:59:25 -0700 | [diff] [blame] | 386 | pc->chip.of_xlate = of_pwm_xlate_with_flags; |
| 387 | pc->chip.of_pwm_n_cells = 3; |
| 388 | } |
| 389 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 390 | ret = pwmchip_add(&pc->chip); |
| 391 | if (ret < 0) { |
| 392 | clk_unprepare(pc->clk); |
| 393 | dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 394 | goto err_pclk; |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Boris Brezillon | 48cf973 | 2016-06-14 11:13:13 +0200 | [diff] [blame] | 397 | /* Keep the PWM clk enabled if the PWM appears to be up and running. */ |
| 398 | if (!pwm_is_enabled(pc->chip.pwms)) |
| 399 | clk_disable(pc->clk); |
| 400 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 401 | return 0; |
| 402 | |
| 403 | err_pclk: |
| 404 | clk_unprepare(pc->pclk); |
| 405 | err_clk: |
| 406 | clk_disable_unprepare(pc->clk); |
| 407 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static int rockchip_pwm_remove(struct platform_device *pdev) |
| 412 | { |
| 413 | struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev); |
| 414 | |
Boris Brezillon | 48cf973 | 2016-06-14 11:13:13 +0200 | [diff] [blame] | 415 | /* |
| 416 | * Disable the PWM clk before unpreparing it if the PWM device is still |
| 417 | * running. This should only happen when the last PWM user left it |
| 418 | * enabled, or when nobody requested a PWM that was previously enabled |
| 419 | * by the bootloader. |
| 420 | * |
| 421 | * FIXME: Maybe the core should disable all PWM devices in |
| 422 | * pwmchip_remove(). In this case we'd only have to call |
| 423 | * clk_unprepare() after pwmchip_remove(). |
| 424 | * |
| 425 | */ |
| 426 | if (pwm_is_enabled(pc->chip.pwms)) |
| 427 | clk_disable(pc->clk); |
| 428 | |
David Wu | 27922ff5 | 2017-08-08 23:38:29 +0800 | [diff] [blame] | 429 | clk_unprepare(pc->pclk); |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 430 | clk_unprepare(pc->clk); |
| 431 | |
| 432 | return pwmchip_remove(&pc->chip); |
| 433 | } |
| 434 | |
Beniamino Galvani | 101353c | 2014-06-21 16:22:06 +0200 | [diff] [blame] | 435 | static struct platform_driver rockchip_pwm_driver = { |
| 436 | .driver = { |
| 437 | .name = "rockchip-pwm", |
| 438 | .of_match_table = rockchip_pwm_dt_ids, |
| 439 | }, |
| 440 | .probe = rockchip_pwm_probe, |
| 441 | .remove = rockchip_pwm_remove, |
| 442 | }; |
| 443 | module_platform_driver(rockchip_pwm_driver); |
| 444 | |
| 445 | MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>"); |
| 446 | MODULE_DESCRIPTION("Rockchip SoC PWM driver"); |
| 447 | MODULE_LICENSE("GPL v2"); |