2 * PWM driver for Rockchip SoCs
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
5 * Copyright (C) 2014 ROCKCHIP, Inc.
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.
12 #include <linux/clk.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pwm.h>
19 #include <linux/time.h>
21 #define PWM_CTRL_TIMER_EN (1 << 0)
22 #define PWM_CTRL_OUTPUT_EN (1 << 3)
24 #define PWM_ENABLE (1 << 0)
25 #define PWM_CONTINUOUS (1 << 1)
26 #define PWM_DUTY_POSITIVE (1 << 3)
27 #define PWM_DUTY_NEGATIVE (0 << 3)
28 #define PWM_INACTIVE_NEGATIVE (0 << 4)
29 #define PWM_INACTIVE_POSITIVE (1 << 4)
30 #define PWM_POLARITY_MASK (PWM_DUTY_POSITIVE | PWM_INACTIVE_POSITIVE)
31 #define PWM_OUTPUT_LEFT (0 << 5)
32 #define PWM_LP_DISABLE (0 << 8)
34 struct rockchip_pwm_chip {
38 const struct rockchip_pwm_data *data;
42 struct rockchip_pwm_regs {
49 struct rockchip_pwm_data {
50 struct rockchip_pwm_regs regs;
51 unsigned int prescaler;
52 bool supports_polarity;
56 static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
58 return container_of(c, struct rockchip_pwm_chip, chip);
61 static void rockchip_pwm_get_state(struct pwm_chip *chip,
62 struct pwm_device *pwm,
63 struct pwm_state *state)
65 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
66 u32 enable_conf = pc->data->enable_conf;
67 unsigned long clk_rate;
72 ret = clk_enable(pc->pclk);
76 clk_rate = clk_get_rate(pc->clk);
78 tmp = readl_relaxed(pc->base + pc->data->regs.period);
79 tmp *= pc->data->prescaler * NSEC_PER_SEC;
80 state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
82 tmp = readl_relaxed(pc->base + pc->data->regs.duty);
83 tmp *= pc->data->prescaler * NSEC_PER_SEC;
84 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
86 val = readl_relaxed(pc->base + pc->data->regs.ctrl);
87 if (pc->data->supports_polarity)
88 state->enabled = ((val & enable_conf) != enable_conf) ?
91 state->enabled = ((val & enable_conf) == enable_conf) ?
94 if (pc->data->supports_polarity) {
95 if (!(val & PWM_DUTY_POSITIVE))
96 state->polarity = PWM_POLARITY_INVERSED;
99 clk_disable(pc->pclk);
102 static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
103 struct pwm_state *state)
105 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
106 unsigned long period, duty;
110 clk_rate = clk_get_rate(pc->clk);
113 * Since period and duty cycle registers have a width of 32
114 * bits, every possible input period can be obtained using the
115 * default prescaler value for all practical clock rate values.
117 div = clk_rate * state->period;
118 period = DIV_ROUND_CLOSEST_ULL(div,
119 pc->data->prescaler * NSEC_PER_SEC);
121 div = clk_rate * state->duty_cycle;
122 duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
124 writel(period, pc->base + pc->data->regs.period);
125 writel(duty, pc->base + pc->data->regs.duty);
127 ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
128 if (pc->data->supports_polarity) {
129 ctrl &= ~PWM_POLARITY_MASK;
130 if (state->polarity == PWM_POLARITY_INVERSED)
131 ctrl |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
133 ctrl |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
135 writel(ctrl, pc->base + pc->data->regs.ctrl);
138 static int rockchip_pwm_enable(struct pwm_chip *chip,
139 struct pwm_device *pwm,
142 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
143 u32 enable_conf = pc->data->enable_conf;
148 ret = clk_enable(pc->clk);
153 val = readl_relaxed(pc->base + pc->data->regs.ctrl);
160 writel_relaxed(val, pc->base + pc->data->regs.ctrl);
163 clk_disable(pc->clk);
168 static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
169 struct pwm_state *state)
171 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
172 struct pwm_state curstate;
176 ret = clk_enable(pc->pclk);
180 pwm_get_state(pwm, &curstate);
181 enabled = curstate.enabled;
183 if (state->polarity != curstate.polarity && enabled) {
184 ret = rockchip_pwm_enable(chip, pwm, false);
190 rockchip_pwm_config(chip, pwm, state);
191 if (state->enabled != enabled) {
192 ret = rockchip_pwm_enable(chip, pwm, state->enabled);
198 * Update the state with the real hardware, which can differ a bit
199 * because of period/duty_cycle approximation.
201 rockchip_pwm_get_state(chip, pwm, state);
204 clk_disable(pc->pclk);
209 static const struct pwm_ops rockchip_pwm_ops = {
210 .get_state = rockchip_pwm_get_state,
211 .apply = rockchip_pwm_apply,
212 .owner = THIS_MODULE,
215 static const struct rockchip_pwm_data pwm_data_v1 = {
223 .supports_polarity = false,
224 .enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN,
227 static const struct rockchip_pwm_data pwm_data_v2 = {
235 .supports_polarity = true,
236 .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
240 static const struct rockchip_pwm_data pwm_data_vop = {
248 .supports_polarity = true,
249 .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
253 static const struct of_device_id rockchip_pwm_dt_ids[] = {
254 { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
255 { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
256 { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
259 MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
261 static int rockchip_pwm_probe(struct platform_device *pdev)
263 const struct of_device_id *id;
264 struct rockchip_pwm_chip *pc;
268 id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
272 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
276 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
277 pc->base = devm_ioremap_resource(&pdev->dev, r);
278 if (IS_ERR(pc->base))
279 return PTR_ERR(pc->base);
281 pc->clk = devm_clk_get(&pdev->dev, "pwm");
282 if (IS_ERR(pc->clk)) {
283 pc->clk = devm_clk_get(&pdev->dev, NULL);
284 if (IS_ERR(pc->clk)) {
285 ret = PTR_ERR(pc->clk);
286 if (ret != -EPROBE_DEFER)
287 dev_err(&pdev->dev, "Can't get bus clk: %d\n",
293 count = of_count_phandle_with_args(pdev->dev.of_node,
294 "clocks", "#clock-cells");
296 pc->pclk = devm_clk_get(&pdev->dev, "pclk");
300 if (IS_ERR(pc->pclk)) {
301 ret = PTR_ERR(pc->pclk);
302 if (ret != -EPROBE_DEFER)
303 dev_err(&pdev->dev, "Can't get APB clk: %d\n", ret);
307 ret = clk_prepare_enable(pc->clk);
309 dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
313 ret = clk_prepare(pc->pclk);
315 dev_err(&pdev->dev, "Can't prepare APB clk: %d\n", ret);
319 platform_set_drvdata(pdev, pc);
322 pc->chip.dev = &pdev->dev;
323 pc->chip.ops = &rockchip_pwm_ops;
327 if (pc->data->supports_polarity) {
328 pc->chip.of_xlate = of_pwm_xlate_with_flags;
329 pc->chip.of_pwm_n_cells = 3;
332 ret = pwmchip_add(&pc->chip);
334 clk_unprepare(pc->clk);
335 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
339 /* Keep the PWM clk enabled if the PWM appears to be up and running. */
340 if (!pwm_is_enabled(pc->chip.pwms))
341 clk_disable(pc->clk);
346 clk_unprepare(pc->pclk);
348 clk_disable_unprepare(pc->clk);
353 static int rockchip_pwm_remove(struct platform_device *pdev)
355 struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
358 * Disable the PWM clk before unpreparing it if the PWM device is still
359 * running. This should only happen when the last PWM user left it
360 * enabled, or when nobody requested a PWM that was previously enabled
363 * FIXME: Maybe the core should disable all PWM devices in
364 * pwmchip_remove(). In this case we'd only have to call
365 * clk_unprepare() after pwmchip_remove().
368 if (pwm_is_enabled(pc->chip.pwms))
369 clk_disable(pc->clk);
371 clk_unprepare(pc->pclk);
372 clk_unprepare(pc->clk);
374 return pwmchip_remove(&pc->chip);
377 static struct platform_driver rockchip_pwm_driver = {
379 .name = "rockchip-pwm",
380 .of_match_table = rockchip_pwm_dt_ids,
382 .probe = rockchip_pwm_probe,
383 .remove = rockchip_pwm_remove,
385 module_platform_driver(rockchip_pwm_driver);
387 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
388 MODULE_DESCRIPTION("Rockchip SoC PWM driver");
389 MODULE_LICENSE("GPL v2");