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