blob: 33bbb5a97b723f77d892aff35c5b86dd0c5f050b [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
David Wuf90df9c2017-08-08 23:38:30 +0800168static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
Beniamino Galvani101353c2014-06-21 16:22:06 +0200169 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
David Wua9001522017-03-01 19:10:55 +0800193static 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 Brezillon2bf1c982016-06-14 11:13:14 +0200215static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
216 struct pwm_state *state)
Beniamino Galvani101353c2014-06-21 16:22:06 +0200217{
218 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200219 struct pwm_state curstate;
220 bool enabled;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200221 int ret;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200222
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200223 pwm_get_state(pwm, &curstate);
224 enabled = curstate.enabled;
225
David Wu27922ff52017-08-08 23:38:29 +0800226 ret = clk_enable(pc->pclk);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200227 if (ret)
228 return ret;
229
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200230 if (state->polarity != curstate.polarity && enabled) {
David Wua9001522017-03-01 19:10:55 +0800231 ret = rockchip_pwm_enable(chip, pwm, false, state->polarity);
232 if (ret)
233 goto out;
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200234 enabled = false;
235 }
Beniamino Galvani101353c2014-06-21 16:22:06 +0200236
David Wuf90df9c2017-08-08 23:38:30 +0800237 rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200238
David Wua9001522017-03-01 19:10:55 +0800239 if (state->enabled != enabled) {
240 ret = rockchip_pwm_enable(chip, pwm, state->enabled,
241 state->polarity);
242 if (ret)
243 goto out;
244 }
Beniamino Galvani101353c2014-06-21 16:22:06 +0200245
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200246 /*
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
252out:
David Wu27922ff52017-08-08 23:38:29 +0800253 clk_disable(pc->pclk);
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200254
255 return ret;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200256}
257
Doug Anderson72643542014-08-25 15:59:25 -0700258static const struct pwm_ops rockchip_pwm_ops_v1 = {
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200259 .get_state = rockchip_pwm_get_state,
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200260 .apply = rockchip_pwm_apply,
Beniamino Galvani101353c2014-06-21 16:22:06 +0200261 .owner = THIS_MODULE,
262};
263
Doug Anderson72643542014-08-25 15:59:25 -0700264static const struct pwm_ops rockchip_pwm_ops_v2 = {
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200265 .get_state = rockchip_pwm_get_state,
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200266 .apply = rockchip_pwm_apply,
Doug Anderson72643542014-08-25 15:59:25 -0700267 .owner = THIS_MODULE,
268};
269
Caesar Wangf6306292014-08-08 15:28:49 +0800270static 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 Anderson72643542014-08-25 15:59:25 -0700278 .ops = &rockchip_pwm_ops_v1,
Caesar Wangf6306292014-08-08 15:28:49 +0800279 .set_enable = rockchip_pwm_set_enable_v1,
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200280 .get_state = rockchip_pwm_get_state_v1,
Caesar Wangf6306292014-08-08 15:28:49 +0800281};
282
283static 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 Brezillon2bf1c982016-06-14 11:13:14 +0200291 .supports_polarity = true,
Doug Anderson72643542014-08-25 15:59:25 -0700292 .ops = &rockchip_pwm_ops_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800293 .set_enable = rockchip_pwm_set_enable_v2,
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200294 .get_state = rockchip_pwm_get_state_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800295};
296
297static 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 Brezillon2bf1c982016-06-14 11:13:14 +0200305 .supports_polarity = true,
Doug Anderson72643542014-08-25 15:59:25 -0700306 .ops = &rockchip_pwm_ops_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800307 .set_enable = rockchip_pwm_set_enable_v2,
Boris Brezillon1ebb74c2016-06-14 11:13:12 +0200308 .get_state = rockchip_pwm_get_state_v2,
Caesar Wangf6306292014-08-08 15:28:49 +0800309};
310
311static 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};
317MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
318
Beniamino Galvani101353c2014-06-21 16:22:06 +0200319static int rockchip_pwm_probe(struct platform_device *pdev)
320{
Caesar Wangf6306292014-08-08 15:28:49 +0800321 const struct of_device_id *id;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200322 struct rockchip_pwm_chip *pc;
323 struct resource *r;
David Wu27922ff52017-08-08 23:38:29 +0800324 int ret, count;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200325
Caesar Wangf6306292014-08-08 15:28:49 +0800326 id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
327 if (!id)
328 return -EINVAL;
329
Beniamino Galvani101353c2014-06-21 16:22:06 +0200330 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 Wu27922ff52017-08-08 23:38:29 +0800339 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 Galvani101353c2014-06-21 16:22:06 +0200364
Boris Brezillon48cf9732016-06-14 11:13:13 +0200365 ret = clk_prepare_enable(pc->clk);
David Wu27922ff52017-08-08 23:38:29 +0800366 if (ret) {
367 dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200368 return ret;
David Wu27922ff52017-08-08 23:38:29 +0800369 }
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 Galvani101353c2014-06-21 16:22:06 +0200376
377 platform_set_drvdata(pdev, pc);
378
Caesar Wangf6306292014-08-08 15:28:49 +0800379 pc->data = id->data;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200380 pc->chip.dev = &pdev->dev;
Doug Anderson72643542014-08-25 15:59:25 -0700381 pc->chip.ops = pc->data->ops;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200382 pc->chip.base = -1;
383 pc->chip.npwm = 1;
384
Boris Brezillon2bf1c982016-06-14 11:13:14 +0200385 if (pc->data->supports_polarity) {
Doug Anderson72643542014-08-25 15:59:25 -0700386 pc->chip.of_xlate = of_pwm_xlate_with_flags;
387 pc->chip.of_pwm_n_cells = 3;
388 }
389
Beniamino Galvani101353c2014-06-21 16:22:06 +0200390 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 Wu27922ff52017-08-08 23:38:29 +0800394 goto err_pclk;
Beniamino Galvani101353c2014-06-21 16:22:06 +0200395 }
396
Boris Brezillon48cf9732016-06-14 11:13:13 +0200397 /* 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 Wu27922ff52017-08-08 23:38:29 +0800401 return 0;
402
403err_pclk:
404 clk_unprepare(pc->pclk);
405err_clk:
406 clk_disable_unprepare(pc->clk);
407
Beniamino Galvani101353c2014-06-21 16:22:06 +0200408 return ret;
409}
410
411static int rockchip_pwm_remove(struct platform_device *pdev)
412{
413 struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
414
Boris Brezillon48cf9732016-06-14 11:13:13 +0200415 /*
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 Wu27922ff52017-08-08 23:38:29 +0800429 clk_unprepare(pc->pclk);
Beniamino Galvani101353c2014-06-21 16:22:06 +0200430 clk_unprepare(pc->clk);
431
432 return pwmchip_remove(&pc->chip);
433}
434
Beniamino Galvani101353c2014-06-21 16:22:06 +0200435static 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};
443module_platform_driver(rockchip_pwm_driver);
444
445MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
446MODULE_DESCRIPTION("Rockchip SoC PWM driver");
447MODULE_LICENSE("GPL v2");