1 // SPDX-License-Identifier: GPL-2.0
3 * R-Car PWM Timer driver
5 * Copyright (C) 2015 Renesas Electronics Corporation
11 #include <linux/log2.h>
12 #include <linux/math64.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/pwm.h>
18 #include <linux/slab.h>
20 #define RCAR_PWM_MAX_DIVISION 24
21 #define RCAR_PWM_MAX_CYCLE 1023
23 #define RCAR_PWMCR 0x00
24 #define RCAR_PWMCR_CC0_MASK 0x000f0000
25 #define RCAR_PWMCR_CC0_SHIFT 16
26 #define RCAR_PWMCR_CCMD BIT(15)
27 #define RCAR_PWMCR_SYNC BIT(11)
28 #define RCAR_PWMCR_SS0 BIT(4)
29 #define RCAR_PWMCR_EN0 BIT(0)
31 #define RCAR_PWMCNT 0x04
32 #define RCAR_PWMCNT_CYC0_MASK 0x03ff0000
33 #define RCAR_PWMCNT_CYC0_SHIFT 16
34 #define RCAR_PWMCNT_PH0_MASK 0x000003ff
35 #define RCAR_PWMCNT_PH0_SHIFT 0
37 struct rcar_pwm_chip
{
43 static inline struct rcar_pwm_chip
*to_rcar_pwm_chip(struct pwm_chip
*chip
)
45 return container_of(chip
, struct rcar_pwm_chip
, chip
);
48 static void rcar_pwm_write(struct rcar_pwm_chip
*rp
, u32 data
,
51 writel(data
, rp
->base
+ offset
);
54 static u32
rcar_pwm_read(struct rcar_pwm_chip
*rp
, unsigned int offset
)
56 return readl(rp
->base
+ offset
);
59 static void rcar_pwm_update(struct rcar_pwm_chip
*rp
, u32 mask
, u32 data
,
64 value
= rcar_pwm_read(rp
, offset
);
67 rcar_pwm_write(rp
, value
, offset
);
70 static int rcar_pwm_get_clock_division(struct rcar_pwm_chip
*rp
, int period_ns
)
72 unsigned long clk_rate
= clk_get_rate(rp
->clk
);
78 div
= (u64
)NSEC_PER_SEC
* RCAR_PWM_MAX_CYCLE
;
79 tmp
= (u64
)period_ns
* clk_rate
+ div
- 1;
80 tmp
= div64_u64(tmp
, div
);
81 div
= ilog2(tmp
- 1) + 1;
83 return (div
<= RCAR_PWM_MAX_DIVISION
) ? div
: -ERANGE
;
86 static void rcar_pwm_set_clock_control(struct rcar_pwm_chip
*rp
,
91 value
= rcar_pwm_read(rp
, RCAR_PWMCR
);
92 value
&= ~(RCAR_PWMCR_CCMD
| RCAR_PWMCR_CC0_MASK
);
95 value
|= RCAR_PWMCR_CCMD
;
99 value
|= div
<< RCAR_PWMCR_CC0_SHIFT
;
100 rcar_pwm_write(rp
, value
, RCAR_PWMCR
);
103 static int rcar_pwm_set_counter(struct rcar_pwm_chip
*rp
, int div
, int duty_ns
,
106 unsigned long long one_cycle
, tmp
; /* 0.01 nanoseconds */
107 unsigned long clk_rate
= clk_get_rate(rp
->clk
);
110 one_cycle
= (unsigned long long)NSEC_PER_SEC
* 100ULL * (1 << div
);
111 do_div(one_cycle
, clk_rate
);
113 tmp
= period_ns
* 100ULL;
114 do_div(tmp
, one_cycle
);
115 cyc
= (tmp
<< RCAR_PWMCNT_CYC0_SHIFT
) & RCAR_PWMCNT_CYC0_MASK
;
117 tmp
= duty_ns
* 100ULL;
118 do_div(tmp
, one_cycle
);
119 ph
= tmp
& RCAR_PWMCNT_PH0_MASK
;
121 /* Avoid prohibited setting */
122 if (cyc
== 0 || ph
== 0)
125 rcar_pwm_write(rp
, cyc
| ph
, RCAR_PWMCNT
);
130 static int rcar_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
132 return pm_runtime_get_sync(chip
->dev
);
135 static void rcar_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
137 pm_runtime_put(chip
->dev
);
140 static int rcar_pwm_enable(struct rcar_pwm_chip
*rp
)
144 /* Don't enable the PWM device if CYC0 or PH0 is 0 */
145 value
= rcar_pwm_read(rp
, RCAR_PWMCNT
);
146 if ((value
& RCAR_PWMCNT_CYC0_MASK
) == 0 ||
147 (value
& RCAR_PWMCNT_PH0_MASK
) == 0)
150 rcar_pwm_update(rp
, RCAR_PWMCR_EN0
, RCAR_PWMCR_EN0
, RCAR_PWMCR
);
155 static void rcar_pwm_disable(struct rcar_pwm_chip
*rp
)
157 rcar_pwm_update(rp
, RCAR_PWMCR_EN0
, 0, RCAR_PWMCR
);
160 static int rcar_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
161 const struct pwm_state
*state
)
163 struct rcar_pwm_chip
*rp
= to_rcar_pwm_chip(chip
);
164 struct pwm_state cur_state
;
167 /* This HW/driver only supports normal polarity */
168 pwm_get_state(pwm
, &cur_state
);
169 if (state
->polarity
!= PWM_POLARITY_NORMAL
)
172 if (!state
->enabled
) {
173 rcar_pwm_disable(rp
);
177 div
= rcar_pwm_get_clock_division(rp
, state
->period
);
181 rcar_pwm_update(rp
, RCAR_PWMCR_SYNC
, RCAR_PWMCR_SYNC
, RCAR_PWMCR
);
183 ret
= rcar_pwm_set_counter(rp
, div
, state
->duty_cycle
, state
->period
);
185 rcar_pwm_set_clock_control(rp
, div
);
187 /* The SYNC should be set to 0 even if rcar_pwm_set_counter failed */
188 rcar_pwm_update(rp
, RCAR_PWMCR_SYNC
, 0, RCAR_PWMCR
);
191 ret
= rcar_pwm_enable(rp
);
196 static const struct pwm_ops rcar_pwm_ops
= {
197 .request
= rcar_pwm_request
,
198 .free
= rcar_pwm_free
,
199 .apply
= rcar_pwm_apply
,
200 .owner
= THIS_MODULE
,
203 static int rcar_pwm_probe(struct platform_device
*pdev
)
205 struct rcar_pwm_chip
*rcar_pwm
;
206 struct resource
*res
;
209 rcar_pwm
= devm_kzalloc(&pdev
->dev
, sizeof(*rcar_pwm
), GFP_KERNEL
);
210 if (rcar_pwm
== NULL
)
213 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
214 rcar_pwm
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
215 if (IS_ERR(rcar_pwm
->base
))
216 return PTR_ERR(rcar_pwm
->base
);
218 rcar_pwm
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
219 if (IS_ERR(rcar_pwm
->clk
)) {
220 dev_err(&pdev
->dev
, "cannot get clock\n");
221 return PTR_ERR(rcar_pwm
->clk
);
224 platform_set_drvdata(pdev
, rcar_pwm
);
226 rcar_pwm
->chip
.dev
= &pdev
->dev
;
227 rcar_pwm
->chip
.ops
= &rcar_pwm_ops
;
228 rcar_pwm
->chip
.base
= -1;
229 rcar_pwm
->chip
.npwm
= 1;
231 ret
= pwmchip_add(&rcar_pwm
->chip
);
233 dev_err(&pdev
->dev
, "failed to register PWM chip: %d\n", ret
);
237 pm_runtime_enable(&pdev
->dev
);
242 static int rcar_pwm_remove(struct platform_device
*pdev
)
244 struct rcar_pwm_chip
*rcar_pwm
= platform_get_drvdata(pdev
);
246 pm_runtime_disable(&pdev
->dev
);
248 return pwmchip_remove(&rcar_pwm
->chip
);
251 static const struct of_device_id rcar_pwm_of_table
[] = {
252 { .compatible
= "renesas,pwm-rcar", },
255 MODULE_DEVICE_TABLE(of
, rcar_pwm_of_table
);
257 static struct platform_driver rcar_pwm_driver
= {
258 .probe
= rcar_pwm_probe
,
259 .remove
= rcar_pwm_remove
,
262 .of_match_table
= of_match_ptr(rcar_pwm_of_table
),
265 module_platform_driver(rcar_pwm_driver
);
267 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
268 MODULE_DESCRIPTION("Renesas PWM Timer Driver");
269 MODULE_LICENSE("GPL v2");
270 MODULE_ALIAS("platform:pwm-rcar");