2 * Mediatek Pulse Width Modulator driver
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;
63 * struct mtk_pwm_chip - struct representing PWM chip
64 * @chip: linux PWM chip representation
65 * @regs: base address of PWM chip
66 * @clks: list of clocks
71 struct clk *clks[MTK_CLK_MAX];
72 const struct mtk_pwm_platform_data *soc;
75 static const unsigned int mtk_pwm_reg_offset[] = {
76 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
79 static inline struct mtk_pwm_chip *to_mtk_pwm_chip(struct pwm_chip *chip)
81 return container_of(chip, struct mtk_pwm_chip, chip);
84 static int mtk_pwm_clk_enable(struct pwm_chip *chip, struct pwm_device *pwm)
86 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
89 ret = clk_prepare_enable(pc->clks[MTK_CLK_TOP]);
93 ret = clk_prepare_enable(pc->clks[MTK_CLK_MAIN]);
97 ret = clk_prepare_enable(pc->clks[MTK_CLK_PWM1 + pwm->hwpwm]);
99 goto disable_clk_main;
104 clk_disable_unprepare(pc->clks[MTK_CLK_MAIN]);
106 clk_disable_unprepare(pc->clks[MTK_CLK_TOP]);
111 static void mtk_pwm_clk_disable(struct pwm_chip *chip, struct pwm_device *pwm)
113 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
115 clk_disable_unprepare(pc->clks[MTK_CLK_PWM1 + pwm->hwpwm]);
116 clk_disable_unprepare(pc->clks[MTK_CLK_MAIN]);
117 clk_disable_unprepare(pc->clks[MTK_CLK_TOP]);
120 static inline u32 mtk_pwm_readl(struct mtk_pwm_chip *chip, unsigned int num,
123 return readl(chip->regs + mtk_pwm_reg_offset[num] + offset);
126 static inline void mtk_pwm_writel(struct mtk_pwm_chip *chip,
127 unsigned int num, unsigned int offset,
130 writel(value, chip->regs + mtk_pwm_reg_offset[num] + offset);
133 static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
134 int duty_ns, int period_ns)
136 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
137 struct clk *clk = pc->clks[MTK_CLK_PWM1 + pwm->hwpwm];
138 u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH,
139 reg_thres = PWMTHRES;
143 ret = mtk_pwm_clk_enable(chip, pwm);
147 /* Using resolution in picosecond gets accuracy higher */
148 resolution = (u64)NSEC_PER_SEC * 1000;
149 do_div(resolution, clk_get_rate(clk));
151 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
152 while (cnt_period > 8191) {
155 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
159 if (clkdiv > PWM_CLK_DIV_MAX) {
160 mtk_pwm_clk_disable(chip, pwm);
161 dev_err(chip->dev, "period %d not supported\n", period_ns);
165 if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) {
167 * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
168 * from the other PWMs on MT7623.
170 reg_width = PWM45DWIDTH_FIXUP;
171 reg_thres = PWM45THRES_FIXUP;
174 cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
175 mtk_pwm_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
176 mtk_pwm_writel(pc, pwm->hwpwm, reg_width, cnt_period);
177 mtk_pwm_writel(pc, pwm->hwpwm, reg_thres, cnt_duty);
179 mtk_pwm_clk_disable(chip, pwm);
184 static int mtk_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
186 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
190 ret = mtk_pwm_clk_enable(chip, pwm);
194 value = readl(pc->regs);
195 value |= BIT(pwm->hwpwm);
196 writel(value, pc->regs);
201 static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
203 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
206 value = readl(pc->regs);
207 value &= ~BIT(pwm->hwpwm);
208 writel(value, pc->regs);
210 mtk_pwm_clk_disable(chip, pwm);
213 static const struct pwm_ops mtk_pwm_ops = {
214 .config = mtk_pwm_config,
215 .enable = mtk_pwm_enable,
216 .disable = mtk_pwm_disable,
217 .owner = THIS_MODULE,
220 static int mtk_pwm_probe(struct platform_device *pdev)
222 const struct mtk_pwm_platform_data *data;
223 struct mtk_pwm_chip *pc;
224 struct resource *res;
228 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
232 data = of_device_get_match_data(&pdev->dev);
237 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 pc->regs = devm_ioremap_resource(&pdev->dev, res);
239 if (IS_ERR(pc->regs))
240 return PTR_ERR(pc->regs);
242 for (i = 0; i < data->num_pwms + 2; i++) {
243 pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
244 if (IS_ERR(pc->clks[i])) {
245 dev_err(&pdev->dev, "clock: %s fail: %ld\n",
246 mtk_pwm_clk_name[i], PTR_ERR(pc->clks[i]));
247 return PTR_ERR(pc->clks[i]);
251 platform_set_drvdata(pdev, pc);
253 pc->chip.dev = &pdev->dev;
254 pc->chip.ops = &mtk_pwm_ops;
256 pc->chip.npwm = data->num_pwms;
258 ret = pwmchip_add(&pc->chip);
260 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
267 static int mtk_pwm_remove(struct platform_device *pdev)
269 struct mtk_pwm_chip *pc = platform_get_drvdata(pdev);
271 return pwmchip_remove(&pc->chip);
274 static const struct mtk_pwm_platform_data mt2712_pwm_data = {
276 .pwm45_fixup = false,
279 static const struct mtk_pwm_platform_data mt7622_pwm_data = {
281 .pwm45_fixup = false,
284 static const struct mtk_pwm_platform_data mt7623_pwm_data = {
289 static const struct of_device_id mtk_pwm_of_match[] = {
290 { .compatible = "mediatek,mt2712-pwm", .data = &mt2712_pwm_data },
291 { .compatible = "mediatek,mt7622-pwm", .data = &mt7622_pwm_data },
292 { .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data },
295 MODULE_DEVICE_TABLE(of, mtk_pwm_of_match);
297 static struct platform_driver mtk_pwm_driver = {
300 .of_match_table = mtk_pwm_of_match,
302 .probe = mtk_pwm_probe,
303 .remove = mtk_pwm_remove,
305 module_platform_driver(mtk_pwm_driver);
308 MODULE_LICENSE("GPL");