1 // SPDX-License-Identifier: GPL-2.0
3 * MediaTek Pulse Width Modulator driver
5 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
6 * Copyright (C) 2017 Zhi Mao <zhi.mao@mediatek.com>
10 #include <linux/err.h>
12 #include <linux/ioport.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/clk.h>
17 #include <linux/platform_device.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
22 /* PWM registers and bits definitions */
27 #define PWMWAVENUM 0x28
28 #define PWMDWIDTH 0x2c
29 #define PWM45DWIDTH_FIXUP 0x30
31 #define PWM45THRES_FIXUP 0x34
32 #define PWM_CK_26M_SEL 0x210
34 #define PWM_CLK_DIV_MAX 7
36 struct pwm_mediatek_of_data
{
37 unsigned int num_pwms
;
40 const unsigned int *reg_offset
;
44 * struct pwm_mediatek_chip - struct representing PWM chip
45 * @regs: base address of PWM chip
46 * @clk_top: the top clock generator
47 * @clk_main: the clock used by PWM core
48 * @clk_pwms: the clock used by each PWM channel
49 * @soc: pointer to chip's platform data
51 struct pwm_mediatek_chip
{
55 struct clk
**clk_pwms
;
56 const struct pwm_mediatek_of_data
*soc
;
59 static const unsigned int mtk_pwm_reg_offset_v1
[] = {
60 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
63 static const unsigned int mtk_pwm_reg_offset_v2
[] = {
64 0x0080, 0x00c0, 0x0100, 0x0140, 0x0180, 0x01c0, 0x0200, 0x0240
67 static inline struct pwm_mediatek_chip
*
68 to_pwm_mediatek_chip(struct pwm_chip
*chip
)
70 return pwmchip_get_drvdata(chip
);
73 static int pwm_mediatek_clk_enable(struct pwm_chip
*chip
,
74 struct pwm_device
*pwm
)
76 struct pwm_mediatek_chip
*pc
= to_pwm_mediatek_chip(chip
);
79 ret
= clk_prepare_enable(pc
->clk_top
);
83 ret
= clk_prepare_enable(pc
->clk_main
);
87 ret
= clk_prepare_enable(pc
->clk_pwms
[pwm
->hwpwm
]);
89 goto disable_clk_main
;
94 clk_disable_unprepare(pc
->clk_main
);
96 clk_disable_unprepare(pc
->clk_top
);
101 static void pwm_mediatek_clk_disable(struct pwm_chip
*chip
,
102 struct pwm_device
*pwm
)
104 struct pwm_mediatek_chip
*pc
= to_pwm_mediatek_chip(chip
);
106 clk_disable_unprepare(pc
->clk_pwms
[pwm
->hwpwm
]);
107 clk_disable_unprepare(pc
->clk_main
);
108 clk_disable_unprepare(pc
->clk_top
);
111 static inline void pwm_mediatek_writel(struct pwm_mediatek_chip
*chip
,
112 unsigned int num
, unsigned int offset
,
115 writel(value
, chip
->regs
+ chip
->soc
->reg_offset
[num
] + offset
);
118 static int pwm_mediatek_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
119 int duty_ns
, int period_ns
)
121 struct pwm_mediatek_chip
*pc
= to_pwm_mediatek_chip(chip
);
122 u32 clkdiv
= 0, cnt_period
, cnt_duty
, reg_width
= PWMDWIDTH
,
123 reg_thres
= PWMTHRES
;
127 ret
= pwm_mediatek_clk_enable(chip
, pwm
);
132 /* Make sure we use the bus clock and not the 26MHz clock */
133 if (pc
->soc
->has_ck_26m_sel
)
134 writel(0, pc
->regs
+ PWM_CK_26M_SEL
);
136 /* Using resolution in picosecond gets accuracy higher */
137 resolution
= (u64
)NSEC_PER_SEC
* 1000;
138 do_div(resolution
, clk_get_rate(pc
->clk_pwms
[pwm
->hwpwm
]));
140 cnt_period
= DIV_ROUND_CLOSEST_ULL((u64
)period_ns
* 1000, resolution
);
141 while (cnt_period
> 8191) {
144 cnt_period
= DIV_ROUND_CLOSEST_ULL((u64
)period_ns
* 1000,
148 if (clkdiv
> PWM_CLK_DIV_MAX
) {
149 pwm_mediatek_clk_disable(chip
, pwm
);
150 dev_err(pwmchip_parent(chip
), "period of %d ns not supported\n", period_ns
);
154 if (pc
->soc
->pwm45_fixup
&& pwm
->hwpwm
> 2) {
156 * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
157 * from the other PWMs on MT7623.
159 reg_width
= PWM45DWIDTH_FIXUP
;
160 reg_thres
= PWM45THRES_FIXUP
;
163 cnt_duty
= DIV_ROUND_CLOSEST_ULL((u64
)duty_ns
* 1000, resolution
);
164 pwm_mediatek_writel(pc
, pwm
->hwpwm
, PWMCON
, BIT(15) | clkdiv
);
165 pwm_mediatek_writel(pc
, pwm
->hwpwm
, reg_width
, cnt_period
);
166 pwm_mediatek_writel(pc
, pwm
->hwpwm
, reg_thres
, cnt_duty
);
168 pwm_mediatek_clk_disable(chip
, pwm
);
173 static int pwm_mediatek_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
175 struct pwm_mediatek_chip
*pc
= to_pwm_mediatek_chip(chip
);
179 ret
= pwm_mediatek_clk_enable(chip
, pwm
);
183 value
= readl(pc
->regs
);
184 value
|= BIT(pwm
->hwpwm
);
185 writel(value
, pc
->regs
);
190 static void pwm_mediatek_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
192 struct pwm_mediatek_chip
*pc
= to_pwm_mediatek_chip(chip
);
195 value
= readl(pc
->regs
);
196 value
&= ~BIT(pwm
->hwpwm
);
197 writel(value
, pc
->regs
);
199 pwm_mediatek_clk_disable(chip
, pwm
);
202 static int pwm_mediatek_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
203 const struct pwm_state
*state
)
207 if (state
->polarity
!= PWM_POLARITY_NORMAL
)
210 if (!state
->enabled
) {
211 if (pwm
->state
.enabled
)
212 pwm_mediatek_disable(chip
, pwm
);
217 err
= pwm_mediatek_config(chip
, pwm
, state
->duty_cycle
, state
->period
);
221 if (!pwm
->state
.enabled
)
222 err
= pwm_mediatek_enable(chip
, pwm
);
227 static const struct pwm_ops pwm_mediatek_ops
= {
228 .apply
= pwm_mediatek_apply
,
231 static int pwm_mediatek_probe(struct platform_device
*pdev
)
233 struct pwm_chip
*chip
;
234 struct pwm_mediatek_chip
*pc
;
235 const struct pwm_mediatek_of_data
*soc
;
239 soc
= of_device_get_match_data(&pdev
->dev
);
241 chip
= devm_pwmchip_alloc(&pdev
->dev
, soc
->num_pwms
, sizeof(*pc
));
243 return PTR_ERR(chip
);
244 pc
= to_pwm_mediatek_chip(chip
);
248 pc
->regs
= devm_platform_ioremap_resource(pdev
, 0);
249 if (IS_ERR(pc
->regs
))
250 return PTR_ERR(pc
->regs
);
252 pc
->clk_pwms
= devm_kmalloc_array(&pdev
->dev
, soc
->num_pwms
,
253 sizeof(*pc
->clk_pwms
), GFP_KERNEL
);
257 pc
->clk_top
= devm_clk_get(&pdev
->dev
, "top");
258 if (IS_ERR(pc
->clk_top
))
259 return dev_err_probe(&pdev
->dev
, PTR_ERR(pc
->clk_top
),
260 "Failed to get top clock\n");
262 pc
->clk_main
= devm_clk_get(&pdev
->dev
, "main");
263 if (IS_ERR(pc
->clk_main
))
264 return dev_err_probe(&pdev
->dev
, PTR_ERR(pc
->clk_main
),
265 "Failed to get main clock\n");
267 for (i
= 0; i
< soc
->num_pwms
; i
++) {
270 snprintf(name
, sizeof(name
), "pwm%d", i
+ 1);
272 pc
->clk_pwms
[i
] = devm_clk_get(&pdev
->dev
, name
);
273 if (IS_ERR(pc
->clk_pwms
[i
]))
274 return dev_err_probe(&pdev
->dev
, PTR_ERR(pc
->clk_pwms
[i
]),
275 "Failed to get %s clock\n", name
);
278 chip
->ops
= &pwm_mediatek_ops
;
280 ret
= devm_pwmchip_add(&pdev
->dev
, chip
);
282 return dev_err_probe(&pdev
->dev
, ret
, "pwmchip_add() failed\n");
287 static const struct pwm_mediatek_of_data mt2712_pwm_data
= {
289 .pwm45_fixup
= false,
290 .has_ck_26m_sel
= false,
291 .reg_offset
= mtk_pwm_reg_offset_v1
,
294 static const struct pwm_mediatek_of_data mt6795_pwm_data
= {
296 .pwm45_fixup
= false,
297 .has_ck_26m_sel
= false,
298 .reg_offset
= mtk_pwm_reg_offset_v1
,
301 static const struct pwm_mediatek_of_data mt7622_pwm_data
= {
303 .pwm45_fixup
= false,
304 .has_ck_26m_sel
= true,
305 .reg_offset
= mtk_pwm_reg_offset_v1
,
308 static const struct pwm_mediatek_of_data mt7623_pwm_data
= {
311 .has_ck_26m_sel
= false,
312 .reg_offset
= mtk_pwm_reg_offset_v1
,
315 static const struct pwm_mediatek_of_data mt7628_pwm_data
= {
318 .has_ck_26m_sel
= false,
319 .reg_offset
= mtk_pwm_reg_offset_v1
,
322 static const struct pwm_mediatek_of_data mt7629_pwm_data
= {
324 .pwm45_fixup
= false,
325 .has_ck_26m_sel
= false,
326 .reg_offset
= mtk_pwm_reg_offset_v1
,
329 static const struct pwm_mediatek_of_data mt7981_pwm_data
= {
331 .pwm45_fixup
= false,
332 .has_ck_26m_sel
= true,
333 .reg_offset
= mtk_pwm_reg_offset_v2
,
336 static const struct pwm_mediatek_of_data mt7986_pwm_data
= {
338 .pwm45_fixup
= false,
339 .has_ck_26m_sel
= true,
340 .reg_offset
= mtk_pwm_reg_offset_v1
,
343 static const struct pwm_mediatek_of_data mt7988_pwm_data
= {
345 .pwm45_fixup
= false,
346 .has_ck_26m_sel
= false,
347 .reg_offset
= mtk_pwm_reg_offset_v2
,
350 static const struct pwm_mediatek_of_data mt8183_pwm_data
= {
352 .pwm45_fixup
= false,
353 .has_ck_26m_sel
= true,
354 .reg_offset
= mtk_pwm_reg_offset_v1
,
357 static const struct pwm_mediatek_of_data mt8365_pwm_data
= {
359 .pwm45_fixup
= false,
360 .has_ck_26m_sel
= true,
361 .reg_offset
= mtk_pwm_reg_offset_v1
,
364 static const struct pwm_mediatek_of_data mt8516_pwm_data
= {
366 .pwm45_fixup
= false,
367 .has_ck_26m_sel
= true,
368 .reg_offset
= mtk_pwm_reg_offset_v1
,
371 static const struct of_device_id pwm_mediatek_of_match
[] = {
372 { .compatible
= "mediatek,mt2712-pwm", .data
= &mt2712_pwm_data
},
373 { .compatible
= "mediatek,mt6795-pwm", .data
= &mt6795_pwm_data
},
374 { .compatible
= "mediatek,mt7622-pwm", .data
= &mt7622_pwm_data
},
375 { .compatible
= "mediatek,mt7623-pwm", .data
= &mt7623_pwm_data
},
376 { .compatible
= "mediatek,mt7628-pwm", .data
= &mt7628_pwm_data
},
377 { .compatible
= "mediatek,mt7629-pwm", .data
= &mt7629_pwm_data
},
378 { .compatible
= "mediatek,mt7981-pwm", .data
= &mt7981_pwm_data
},
379 { .compatible
= "mediatek,mt7986-pwm", .data
= &mt7986_pwm_data
},
380 { .compatible
= "mediatek,mt7988-pwm", .data
= &mt7988_pwm_data
},
381 { .compatible
= "mediatek,mt8183-pwm", .data
= &mt8183_pwm_data
},
382 { .compatible
= "mediatek,mt8365-pwm", .data
= &mt8365_pwm_data
},
383 { .compatible
= "mediatek,mt8516-pwm", .data
= &mt8516_pwm_data
},
386 MODULE_DEVICE_TABLE(of
, pwm_mediatek_of_match
);
388 static struct platform_driver pwm_mediatek_driver
= {
390 .name
= "pwm-mediatek",
391 .of_match_table
= pwm_mediatek_of_match
,
393 .probe
= pwm_mediatek_probe
,
395 module_platform_driver(pwm_mediatek_driver
);
397 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
398 MODULE_DESCRIPTION("MediaTek general purpose Pulse Width Modulator driver");
399 MODULE_LICENSE("GPL v2");