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_OUTPUT_LEFT (0 << 5)
31 #define PWM_LP_DISABLE (0 << 8)
33 struct rockchip_pwm_chip {
36 const struct rockchip_pwm_data *data;
40 struct rockchip_pwm_regs {
47 struct rockchip_pwm_data {
48 struct rockchip_pwm_regs regs;
49 unsigned int prescaler;
50 const struct pwm_ops *ops;
52 void (*set_enable)(struct pwm_chip *chip,
53 struct pwm_device *pwm, bool enable);
54 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
55 struct pwm_state *state);
58 static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
60 return container_of(c, struct rockchip_pwm_chip, chip);
63 static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip,
64 struct pwm_device *pwm, bool enable)
66 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
67 u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
70 val = readl_relaxed(pc->base + pc->data->regs.ctrl);
77 writel_relaxed(val, pc->base + pc->data->regs.ctrl);
80 static void rockchip_pwm_get_state_v1(struct pwm_chip *chip,
81 struct pwm_device *pwm,
82 struct pwm_state *state)
84 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
85 u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
88 val = readl_relaxed(pc->base + pc->data->regs.ctrl);
89 if ((val & enable_conf) == enable_conf)
90 state->enabled = true;
93 static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip,
94 struct pwm_device *pwm, bool enable)
96 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
97 u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
101 if (pwm_get_polarity(pwm) == PWM_POLARITY_INVERSED)
102 enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
104 enable_conf |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
106 val = readl_relaxed(pc->base + pc->data->regs.ctrl);
113 writel_relaxed(val, pc->base + pc->data->regs.ctrl);
116 static void rockchip_pwm_get_state_v2(struct pwm_chip *chip,
117 struct pwm_device *pwm,
118 struct pwm_state *state)
120 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
121 u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
125 val = readl_relaxed(pc->base + pc->data->regs.ctrl);
126 if ((val & enable_conf) != enable_conf)
129 state->enabled = true;
131 if (!(val & PWM_DUTY_POSITIVE))
132 state->polarity = PWM_POLARITY_INVERSED;
135 static void rockchip_pwm_get_state(struct pwm_chip *chip,
136 struct pwm_device *pwm,
137 struct pwm_state *state)
139 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
140 unsigned long clk_rate;
144 ret = clk_enable(pc->clk);
148 clk_rate = clk_get_rate(pc->clk);
150 tmp = readl_relaxed(pc->base + pc->data->regs.period);
151 tmp *= pc->data->prescaler * NSEC_PER_SEC;
152 state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
154 tmp = readl_relaxed(pc->base + pc->data->regs.duty);
155 tmp *= pc->data->prescaler * NSEC_PER_SEC;
156 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
158 pc->data->get_state(chip, pwm, state);
160 clk_disable(pc->clk);
163 static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
164 int duty_ns, int period_ns)
166 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
167 unsigned long period, duty;
171 clk_rate = clk_get_rate(pc->clk);
174 * Since period and duty cycle registers have a width of 32
175 * bits, every possible input period can be obtained using the
176 * default prescaler value for all practical clock rate values.
178 div = clk_rate * period_ns;
179 period = DIV_ROUND_CLOSEST_ULL(div,
180 pc->data->prescaler * NSEC_PER_SEC);
182 div = clk_rate * duty_ns;
183 duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
185 ret = clk_enable(pc->clk);
189 writel(period, pc->base + pc->data->regs.period);
190 writel(duty, pc->base + pc->data->regs.duty);
191 writel(0, pc->base + pc->data->regs.cntr);
193 clk_disable(pc->clk);
198 static int rockchip_pwm_set_polarity(struct pwm_chip *chip,
199 struct pwm_device *pwm,
200 enum pwm_polarity polarity)
203 * No action needed here because pwm->polarity will be set by the core
204 * and the core will only change polarity when the PWM is not enabled.
205 * We'll handle things in set_enable().
211 static int rockchip_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
213 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
216 ret = clk_enable(pc->clk);
220 pc->data->set_enable(chip, pwm, true);
225 static void rockchip_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
227 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
229 pc->data->set_enable(chip, pwm, false);
231 clk_disable(pc->clk);
234 static const struct pwm_ops rockchip_pwm_ops_v1 = {
235 .get_state = rockchip_pwm_get_state,
236 .config = rockchip_pwm_config,
237 .enable = rockchip_pwm_enable,
238 .disable = rockchip_pwm_disable,
239 .owner = THIS_MODULE,
242 static const struct pwm_ops rockchip_pwm_ops_v2 = {
243 .get_state = rockchip_pwm_get_state,
244 .config = rockchip_pwm_config,
245 .set_polarity = rockchip_pwm_set_polarity,
246 .enable = rockchip_pwm_enable,
247 .disable = rockchip_pwm_disable,
248 .owner = THIS_MODULE,
251 static const struct rockchip_pwm_data pwm_data_v1 = {
259 .ops = &rockchip_pwm_ops_v1,
260 .set_enable = rockchip_pwm_set_enable_v1,
261 .get_state = rockchip_pwm_get_state_v1,
264 static const struct rockchip_pwm_data pwm_data_v2 = {
272 .ops = &rockchip_pwm_ops_v2,
273 .set_enable = rockchip_pwm_set_enable_v2,
274 .get_state = rockchip_pwm_get_state_v2,
277 static const struct rockchip_pwm_data pwm_data_vop = {
285 .ops = &rockchip_pwm_ops_v2,
286 .set_enable = rockchip_pwm_set_enable_v2,
287 .get_state = rockchip_pwm_get_state_v2,
290 static const struct of_device_id rockchip_pwm_dt_ids[] = {
291 { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
292 { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
293 { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
296 MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
298 static int rockchip_pwm_probe(struct platform_device *pdev)
300 const struct of_device_id *id;
301 struct rockchip_pwm_chip *pc;
305 id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
309 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
313 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
314 pc->base = devm_ioremap_resource(&pdev->dev, r);
315 if (IS_ERR(pc->base))
316 return PTR_ERR(pc->base);
318 pc->clk = devm_clk_get(&pdev->dev, NULL);
320 return PTR_ERR(pc->clk);
322 ret = clk_prepare_enable(pc->clk);
326 platform_set_drvdata(pdev, pc);
329 pc->chip.dev = &pdev->dev;
330 pc->chip.ops = pc->data->ops;
334 if (pc->data->ops->set_polarity) {
335 pc->chip.of_xlate = of_pwm_xlate_with_flags;
336 pc->chip.of_pwm_n_cells = 3;
339 ret = pwmchip_add(&pc->chip);
341 clk_unprepare(pc->clk);
342 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
345 /* Keep the PWM clk enabled if the PWM appears to be up and running. */
346 if (!pwm_is_enabled(pc->chip.pwms))
347 clk_disable(pc->clk);
352 static int rockchip_pwm_remove(struct platform_device *pdev)
354 struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
357 * Disable the PWM clk before unpreparing it if the PWM device is still
358 * running. This should only happen when the last PWM user left it
359 * enabled, or when nobody requested a PWM that was previously enabled
362 * FIXME: Maybe the core should disable all PWM devices in
363 * pwmchip_remove(). In this case we'd only have to call
364 * clk_unprepare() after pwmchip_remove().
367 if (pwm_is_enabled(pc->chip.pwms))
368 clk_disable(pc->clk);
370 clk_unprepare(pc->clk);
372 return pwmchip_remove(&pc->chip);
375 static struct platform_driver rockchip_pwm_driver = {
377 .name = "rockchip-pwm",
378 .of_match_table = rockchip_pwm_dt_ids,
380 .probe = rockchip_pwm_probe,
381 .remove = rockchip_pwm_remove,
383 module_platform_driver(rockchip_pwm_driver);
385 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
386 MODULE_DESCRIPTION("Rockchip SoC PWM driver");
387 MODULE_LICENSE("GPL v2");