2 * Mediatek Pulse Width Modulator driver
4 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
5 * Copyright (C) 2017 Zhi Mao <zhi.mao@mediatek.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/err.h>
14 #include <linux/ioport.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/clk.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/pwm.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
25 /* PWM registers and bits definitions */
30 #define PWMWAVENUM 0x28
31 #define PWMDWIDTH 0x2c
32 #define PWM45DWIDTH_FIXUP 0x30
34 #define PWM45THRES_FIXUP 0x34
36 #define PWM_CLK_DIV_MAX 7
52 static const char * const mtk_pwm_clk_name
[MTK_CLK_MAX
] = {
53 "main", "top", "pwm1", "pwm2", "pwm3", "pwm4", "pwm5", "pwm6", "pwm7",
57 struct mtk_pwm_platform_data
{
58 unsigned int num_pwms
;
64 * struct mtk_pwm_chip - struct representing PWM chip
65 * @chip: linux PWM chip representation
66 * @regs: base address of PWM chip
67 * @clks: list of clocks
72 struct clk
*clks
[MTK_CLK_MAX
];
73 const struct mtk_pwm_platform_data
*soc
;
76 static const unsigned int mtk_pwm_reg_offset
[] = {
77 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
80 static inline struct mtk_pwm_chip
*to_mtk_pwm_chip(struct pwm_chip
*chip
)
82 return container_of(chip
, struct mtk_pwm_chip
, chip
);
85 static int mtk_pwm_clk_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
87 struct mtk_pwm_chip
*pc
= to_mtk_pwm_chip(chip
);
90 if (!pc
->soc
->has_clks
)
93 ret
= clk_prepare_enable(pc
->clks
[MTK_CLK_TOP
]);
97 ret
= clk_prepare_enable(pc
->clks
[MTK_CLK_MAIN
]);
101 ret
= clk_prepare_enable(pc
->clks
[MTK_CLK_PWM1
+ pwm
->hwpwm
]);
103 goto disable_clk_main
;
108 clk_disable_unprepare(pc
->clks
[MTK_CLK_MAIN
]);
110 clk_disable_unprepare(pc
->clks
[MTK_CLK_TOP
]);
115 static void mtk_pwm_clk_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
117 struct mtk_pwm_chip
*pc
= to_mtk_pwm_chip(chip
);
119 if (!pc
->soc
->has_clks
)
122 clk_disable_unprepare(pc
->clks
[MTK_CLK_PWM1
+ pwm
->hwpwm
]);
123 clk_disable_unprepare(pc
->clks
[MTK_CLK_MAIN
]);
124 clk_disable_unprepare(pc
->clks
[MTK_CLK_TOP
]);
127 static inline u32
mtk_pwm_readl(struct mtk_pwm_chip
*chip
, unsigned int num
,
130 return readl(chip
->regs
+ mtk_pwm_reg_offset
[num
] + offset
);
133 static inline void mtk_pwm_writel(struct mtk_pwm_chip
*chip
,
134 unsigned int num
, unsigned int offset
,
137 writel(value
, chip
->regs
+ mtk_pwm_reg_offset
[num
] + offset
);
140 static int mtk_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
141 int duty_ns
, int period_ns
)
143 struct mtk_pwm_chip
*pc
= to_mtk_pwm_chip(chip
);
144 struct clk
*clk
= pc
->clks
[MTK_CLK_PWM1
+ pwm
->hwpwm
];
145 u32 clkdiv
= 0, cnt_period
, cnt_duty
, reg_width
= PWMDWIDTH
,
146 reg_thres
= PWMTHRES
;
150 ret
= mtk_pwm_clk_enable(chip
, pwm
);
154 /* Using resolution in picosecond gets accuracy higher */
155 resolution
= (u64
)NSEC_PER_SEC
* 1000;
156 do_div(resolution
, clk_get_rate(clk
));
158 cnt_period
= DIV_ROUND_CLOSEST_ULL((u64
)period_ns
* 1000, resolution
);
159 while (cnt_period
> 8191) {
162 cnt_period
= DIV_ROUND_CLOSEST_ULL((u64
)period_ns
* 1000,
166 if (clkdiv
> PWM_CLK_DIV_MAX
) {
167 mtk_pwm_clk_disable(chip
, pwm
);
168 dev_err(chip
->dev
, "period %d not supported\n", period_ns
);
172 if (pc
->soc
->pwm45_fixup
&& pwm
->hwpwm
> 2) {
174 * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
175 * from the other PWMs on MT7623.
177 reg_width
= PWM45DWIDTH_FIXUP
;
178 reg_thres
= PWM45THRES_FIXUP
;
181 cnt_duty
= DIV_ROUND_CLOSEST_ULL((u64
)duty_ns
* 1000, resolution
);
182 mtk_pwm_writel(pc
, pwm
->hwpwm
, PWMCON
, BIT(15) | clkdiv
);
183 mtk_pwm_writel(pc
, pwm
->hwpwm
, reg_width
, cnt_period
);
184 mtk_pwm_writel(pc
, pwm
->hwpwm
, reg_thres
, cnt_duty
);
186 mtk_pwm_clk_disable(chip
, pwm
);
191 static int mtk_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
193 struct mtk_pwm_chip
*pc
= to_mtk_pwm_chip(chip
);
197 ret
= mtk_pwm_clk_enable(chip
, pwm
);
201 value
= readl(pc
->regs
);
202 value
|= BIT(pwm
->hwpwm
);
203 writel(value
, pc
->regs
);
208 static void mtk_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
210 struct mtk_pwm_chip
*pc
= to_mtk_pwm_chip(chip
);
213 value
= readl(pc
->regs
);
214 value
&= ~BIT(pwm
->hwpwm
);
215 writel(value
, pc
->regs
);
217 mtk_pwm_clk_disable(chip
, pwm
);
220 static const struct pwm_ops mtk_pwm_ops
= {
221 .config
= mtk_pwm_config
,
222 .enable
= mtk_pwm_enable
,
223 .disable
= mtk_pwm_disable
,
224 .owner
= THIS_MODULE
,
227 static int mtk_pwm_probe(struct platform_device
*pdev
)
229 const struct mtk_pwm_platform_data
*data
;
230 struct mtk_pwm_chip
*pc
;
231 struct resource
*res
;
235 pc
= devm_kzalloc(&pdev
->dev
, sizeof(*pc
), GFP_KERNEL
);
239 data
= of_device_get_match_data(&pdev
->dev
);
244 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
245 pc
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
246 if (IS_ERR(pc
->regs
))
247 return PTR_ERR(pc
->regs
);
249 for (i
= 0; i
< data
->num_pwms
+ 2 && pc
->soc
->has_clks
; i
++) {
250 pc
->clks
[i
] = devm_clk_get(&pdev
->dev
, mtk_pwm_clk_name
[i
]);
251 if (IS_ERR(pc
->clks
[i
])) {
252 dev_err(&pdev
->dev
, "clock: %s fail: %ld\n",
253 mtk_pwm_clk_name
[i
], PTR_ERR(pc
->clks
[i
]));
254 return PTR_ERR(pc
->clks
[i
]);
258 platform_set_drvdata(pdev
, pc
);
260 pc
->chip
.dev
= &pdev
->dev
;
261 pc
->chip
.ops
= &mtk_pwm_ops
;
263 pc
->chip
.npwm
= data
->num_pwms
;
265 ret
= pwmchip_add(&pc
->chip
);
267 dev_err(&pdev
->dev
, "pwmchip_add() failed: %d\n", ret
);
274 static int mtk_pwm_remove(struct platform_device
*pdev
)
276 struct mtk_pwm_chip
*pc
= platform_get_drvdata(pdev
);
278 return pwmchip_remove(&pc
->chip
);
281 static const struct mtk_pwm_platform_data mt2712_pwm_data
= {
283 .pwm45_fixup
= false,
287 static const struct mtk_pwm_platform_data mt7622_pwm_data
= {
289 .pwm45_fixup
= false,
293 static const struct mtk_pwm_platform_data mt7623_pwm_data
= {
299 static const struct mtk_pwm_platform_data mt7628_pwm_data
= {
305 static const struct of_device_id mtk_pwm_of_match
[] = {
306 { .compatible
= "mediatek,mt2712-pwm", .data
= &mt2712_pwm_data
},
307 { .compatible
= "mediatek,mt7622-pwm", .data
= &mt7622_pwm_data
},
308 { .compatible
= "mediatek,mt7623-pwm", .data
= &mt7623_pwm_data
},
309 { .compatible
= "mediatek,mt7628-pwm", .data
= &mt7628_pwm_data
},
312 MODULE_DEVICE_TABLE(of
, mtk_pwm_of_match
);
314 static struct platform_driver mtk_pwm_driver
= {
317 .of_match_table
= mtk_pwm_of_match
,
319 .probe
= mtk_pwm_probe
,
320 .remove
= mtk_pwm_remove
,
322 module_platform_driver(mtk_pwm_driver
);
324 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
325 MODULE_LICENSE("GPL");