blob: ac3cd5ec5310d5e5bae276c1d04a21f9d808fb72 [file] [log] [blame]
Beniamino Galvani101353c2014-06-21 16:22:06 +02001/*
2 * PWM driver for Rockchip SoCs
3 *
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
Caesar Wangf6306292014-08-08 15:28:49 +08005 * Copyright (C) 2014 ROCKCHIP, Inc.
Beniamino Galvani101353c2014-06-21 16:22:06 +02006 *
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 Wangf6306292014-08-08 15:28:49 +080016#include <linux/of_device.h>
Beniamino Galvani101353c2014-06-21 16:22:06 +020017#include <linux/platform_device.h>
18#include <linux/pwm.h>
19#include <linux/time.h>
20
Beniamino Galvani101353c2014-06-21 16:22:06 +020021#define PWM_CTRL_TIMER_EN (1 << 0)
22#define PWM_CTRL_OUTPUT_EN (1 << 3)
23
Caesar Wangf6306292014-08-08 15:28:49 +080024#define PWM_ENABLE (1 << 0)
25#define PWM_CONTINUOUS (1 << 1)
26#define PWM_DUTY_POSITIVE (1 << 3)
Doug Anderson72643542014-08-25 15:59:25 -070027#define PWM_DUTY_NEGATIVE (0 << 3)
Caesar Wangf6306292014-08-08 15:28:49 +080028#define PWM_INACTIVE_NEGATIVE (0 << 4)
Doug Anderson72643542014-08-25 15:59:25 -070029#define PWM_INACTIVE_POSITIVE (1 << 4)
Caesar Wangf6306292014-08-08 15:28:49 +080030#define PWM_OUTPUT_LEFT (0 << 5)
31#define PWM_LP_DISABLE (0 << 8)
Beniamino Galvani101353c2014-06-21 16:22:06 +020032
33struct rockchip_pwm_chip {
34 struct pwm_chip chip;
35 struct clk *clk;
David Wu27922ff52017-08-08 23:38:29 +080036 struct clk *pclk;
Caesar Wangf6306292014-08-08 15:28:49 +080037 const struct rockchip_pwm_data *data;
Beniamino Galvani101353c2014-06-21 16:22:06 +020038 void __iomem *base;
39};
40
Caesar Wangf6306292014-08-08 15:28:49 +080041struct rockchip_pwm_regs {
42 unsigned long duty;
43 unsigned long period;
44 unsigned long cntr;
45 unsigned long ctrl;
46};
47
48struct rockchip_pwm_data {
49 struct rockchip_pwm_regs regs;
50 unsigned int prescaler;
Boris Brezillon2bf1c982016-06-14 11:13:14 +020051 bool supports_polarity;
Doug Anderson72643542014-08-25 15:59:25 -070052 const struct pwm_ops *ops;
Caesar Wangf6306292014-08-08 15:28:49 +080053
Doug Anderson72643542014-08-25 15:59:25 -070054 void (*set_enable)(struct pwm_chip *chip,
Boris Brezillon2bf1c982016-06-14 11:13:14 +020055 struct pwm_device *pwm, bool enable,
56 enum pwm_polarity polarity);
Boris Brezillon1ebb74c2016-06-14 11:13:12 +020057 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
58 struct pwm_state *state);
Caesar Wangf6306292014-08-08 15:28:49 +080059};
60
Beniamino Galvani101353c2014-06-21 16:22:06 +020061static 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 Anderson72643542014-08-25 15:59:25 -070066static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip,
Boris Brezillon2bf1c982016-06-14 11:13:14 +020067 struct pwm_device *pwm, bool enable,
68 enum pwm_polarity polarity)
Caesar Wangf6306292014-08-08 15:28:49 +080069{
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 Brezillon1ebb74c2016-06-14 11:13:12 +020084static 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 Anderson72643542014-08-25 15:59:25 -070097static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip,
Boris Brezillon2bf1c982016-06-14 11:13:14 +020098 struct pwm_device *pwm, bool enable,
99 enum pwm_polarity polarity)
Caesar Wangf6306292014-08-08 15:28:49 +0800100{
101 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
102 u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
Doug Anderson72643542014-08-25 15:59:25 -0700103 PWM_CONTINUOUS;
Caesar Wangf6306292014-08-08 15:28:49 +0800104 u32 val;
105
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200106 if (polarity == PWM_POLARITY_INVERSED)
Doug Anderson72643542014-08-25 15:59:25 -0700107 enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
108 else
109 enable_conf |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
110
Caesar Wangf6306292014-08-08 15:28:49 +0800111 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 Brezillon1ebb74c2016-06-14 11:13:12 +0200121static 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
140static 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 Wu27922ff52017-08-08 23:38:29 +0800149 ret = clk_enable(pc->pclk);
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200150 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 Wu27922ff52017-08-08 23:38:29 +0800165 clk_disable(pc->pclk);
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200166}
167
Beniamino Galvani101353c2014-06-21 16:22:06 +0200168static 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 Galvani101353c2014-06-21 16:22:06 +0200174
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 Brezillon12f9ce42016-06-14 11:13:11 +0200183 period = DIV_ROUND_CLOSEST_ULL(div,
184 pc->data->prescaler * NSEC_PER_SEC);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200185
186 div = clk_rate * duty_ns;
Boris Brezillon12f9ce42016-06-14 11:13:11 +0200187 duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200188
Caesar Wangf6306292014-08-08 15:28:49 +0800189 writel(period, pc->base + pc->data->regs.period);
190 writel(duty, pc->base + pc->data->regs.duty);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200191
192 return 0;
193}
194
David Wua9001522017-03-01 19:10:55 +0800195static 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 Brezillon2bf1c982016-06-14 11:13:14 +0200217static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
218 struct pwm_state *state)
Beniamino Galvani101353c2014-06-21 16:22:06 +0200219{
220 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200221 struct pwm_state curstate;
222 bool enabled;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200223 int ret;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200224
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200225 pwm_get_state(pwm, &curstate);
226 enabled = curstate.enabled;
227
David Wu27922ff52017-08-08 23:38:29 +0800228 ret = clk_enable(pc->pclk);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200229 if (ret)
230 return ret;
231
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200232 if (state->polarity != curstate.polarity && enabled) {
David Wua9001522017-03-01 19:10:55 +0800233 ret = rockchip_pwm_enable(chip, pwm, false, state->polarity);
234 if (ret)
235 goto out;
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200236 enabled = false;
237 }
Beniamino Galvani101353c2014-06-21 16:22:06 +0200238
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200239 ret = rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period);
240 if (ret) {
241 if (enabled != curstate.enabled)
David Wua9001522017-03-01 19:10:55 +0800242 rockchip_pwm_enable(chip, pwm, !enabled,
243 state->polarity);
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200244 goto out;
245 }
Beniamino Galvani101353c2014-06-21 16:22:06 +0200246
David Wua9001522017-03-01 19:10:55 +0800247 if (state->enabled != enabled) {
248 ret = rockchip_pwm_enable(chip, pwm, state->enabled,
249 state->polarity);
250 if (ret)
251 goto out;
252 }
Beniamino Galvani101353c2014-06-21 16:22:06 +0200253
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200254 /*
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
260out:
David Wu27922ff52017-08-08 23:38:29 +0800261 clk_disable(pc->pclk);
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200262
263 return ret;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200264}
265
Doug Anderson72643542014-08-25 15:59:25 -0700266static const struct pwm_ops rockchip_pwm_ops_v1 = {
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200267 .get_state = rockchip_pwm_get_state,
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200268 .apply = rockchip_pwm_apply,
Beniamino Galvani101353c2014-06-21 16:22:06 +0200269 .owner = THIS_MODULE,
270};
271
Doug Anderson72643542014-08-25 15:59:25 -0700272static const struct pwm_ops rockchip_pwm_ops_v2 = {
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200273 .get_state = rockchip_pwm_get_state,
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200274 .apply = rockchip_pwm_apply,
Doug Anderson72643542014-08-25 15:59:25 -0700275 .owner = THIS_MODULE,
276};
277
Caesar Wangf6306292014-08-08 15:28:49 +0800278static 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 Anderson72643542014-08-25 15:59:25 -0700286 .ops = &rockchip_pwm_ops_v1,
Caesar Wangf6306292014-08-08 15:28:49 +0800287 .set_enable = rockchip_pwm_set_enable_v1,
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200288 .get_state = rockchip_pwm_get_state_v1,
Caesar Wangf6306292014-08-08 15:28:49 +0800289};
290
291static 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 Brezillon2bf1c982016-06-14 11:13:14 +0200299 .supports_polarity = true,
Doug Anderson72643542014-08-25 15:59:25 -0700300 .ops = &rockchip_pwm_ops_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800301 .set_enable = rockchip_pwm_set_enable_v2,
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200302 .get_state = rockchip_pwm_get_state_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800303};
304
305static 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 Brezillon2bf1c982016-06-14 11:13:14 +0200313 .supports_polarity = true,
Doug Anderson72643542014-08-25 15:59:25 -0700314 .ops = &rockchip_pwm_ops_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800315 .set_enable = rockchip_pwm_set_enable_v2,
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200316 .get_state = rockchip_pwm_get_state_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800317};
318
319static 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};
325MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
326
Beniamino Galvani101353c2014-06-21 16:22:06 +0200327static int rockchip_pwm_probe(struct platform_device *pdev)
328{
Caesar Wangf6306292014-08-08 15:28:49 +0800329 const struct of_device_id *id;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200330 struct rockchip_pwm_chip *pc;
331 struct resource *r;
David Wu27922ff52017-08-08 23:38:29 +0800332 int ret, count;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200333
Caesar Wangf6306292014-08-08 15:28:49 +0800334 id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
335 if (!id)
336 return -EINVAL;
337
Beniamino Galvani101353c2014-06-21 16:22:06 +0200338 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 Wu27922ff52017-08-08 23:38:29 +0800347 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 Galvani101353c2014-06-21 16:22:06 +0200372
Boris Brezillon48cf9732016-06-14 11:13:13 +0200373 ret = clk_prepare_enable(pc->clk);
David Wu27922ff52017-08-08 23:38:29 +0800374 if (ret) {
375 dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200376 return ret;
David Wu27922ff52017-08-08 23:38:29 +0800377 }
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 Galvani101353c2014-06-21 16:22:06 +0200384
385 platform_set_drvdata(pdev, pc);
386
Caesar Wangf6306292014-08-08 15:28:49 +0800387 pc->data = id->data;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200388 pc->chip.dev = &pdev->dev;
Doug Anderson72643542014-08-25 15:59:25 -0700389 pc->chip.ops = pc->data->ops;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200390 pc->chip.base = -1;
391 pc->chip.npwm = 1;
392
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200393 if (pc->data->supports_polarity) {
Doug Anderson72643542014-08-25 15:59:25 -0700394 pc->chip.of_xlate = of_pwm_xlate_with_flags;
395 pc->chip.of_pwm_n_cells = 3;
396 }
397
Beniamino Galvani101353c2014-06-21 16:22:06 +0200398 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 Wu27922ff52017-08-08 23:38:29 +0800402 goto err_pclk;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200403 }
404
Boris Brezillon48cf9732016-06-14 11:13:13 +0200405 /* 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 Wu27922ff52017-08-08 23:38:29 +0800409 return 0;
410
411err_pclk:
412 clk_unprepare(pc->pclk);
413err_clk:
414 clk_disable_unprepare(pc->clk);
415
Beniamino Galvani101353c2014-06-21 16:22:06 +0200416 return ret;
417}
418
419static int rockchip_pwm_remove(struct platform_device *pdev)
420{
421 struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
422
Boris Brezillon48cf9732016-06-14 11:13:13 +0200423 /*
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 Wu27922ff52017-08-08 23:38:29 +0800437 clk_unprepare(pc->pclk);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200438 clk_unprepare(pc->clk);
439
440 return pwmchip_remove(&pc->chip);
441}
442
Beniamino Galvani101353c2014-06-21 16:22:06 +0200443static 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};
451module_platform_driver(rockchip_pwm_driver);
452
453MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
454MODULE_DESCRIPTION("Rockchip SoC PWM driver");
455MODULE_LICENSE("GPL v2");