2 * MediaTek display pulse-width-modulation controller driver.
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: YH Huang <yh.huang@mediatek.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/clk.h>
17 #include <linux/err.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/pwm.h>
23 #include <linux/slab.h>
25 #define DISP_PWM_EN 0x00
26 #define PWM_ENABLE_MASK BIT(0)
28 #define DISP_PWM_COMMIT 0x08
29 #define PWM_COMMIT_MASK BIT(0)
31 #define DISP_PWM_CON_0 0x10
32 #define PWM_CLKDIV_SHIFT 16
33 #define PWM_CLKDIV_MAX 0x3ff
34 #define PWM_CLKDIV_MASK (PWM_CLKDIV_MAX << PWM_CLKDIV_SHIFT)
36 #define DISP_PWM_CON_1 0x14
37 #define PWM_PERIOD_BIT_WIDTH 12
38 #define PWM_PERIOD_MASK ((1 << PWM_PERIOD_BIT_WIDTH) - 1)
40 #define PWM_HIGH_WIDTH_SHIFT 16
41 #define PWM_HIGH_WIDTH_MASK (0x1fff << PWM_HIGH_WIDTH_SHIFT)
50 static inline struct mtk_disp_pwm
*to_mtk_disp_pwm(struct pwm_chip
*chip
)
52 return container_of(chip
, struct mtk_disp_pwm
, chip
);
55 static void mtk_disp_pwm_update_bits(struct mtk_disp_pwm
*mdp
, u32 offset
,
58 void __iomem
*address
= mdp
->base
+ offset
;
61 value
= readl(address
);
64 writel(value
, address
);
67 static int mtk_disp_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
68 int duty_ns
, int period_ns
)
70 struct mtk_disp_pwm
*mdp
= to_mtk_disp_pwm(chip
);
71 u32 clk_div
, period
, high_width
, value
;
76 * Find period, high_width and clk_div to suit duty_ns and period_ns.
77 * Calculate proper div value to keep period value in the bound.
79 * period_ns = 10^9 * (clk_div + 1) * (period + 1) / PWM_CLK_RATE
80 * duty_ns = 10^9 * (clk_div + 1) * high_width / PWM_CLK_RATE
82 * period = (PWM_CLK_RATE * period_ns) / (10^9 * (clk_div + 1)) - 1
83 * high_width = (PWM_CLK_RATE * duty_ns) / (10^9 * (clk_div + 1))
85 rate
= clk_get_rate(mdp
->clk_main
);
86 clk_div
= div_u64(rate
* period_ns
, NSEC_PER_SEC
) >>
88 if (clk_div
> PWM_CLKDIV_MAX
)
91 div
= NSEC_PER_SEC
* (clk_div
+ 1);
92 period
= div64_u64(rate
* period_ns
, div
);
96 high_width
= div64_u64(rate
* duty_ns
, div
);
97 value
= period
| (high_width
<< PWM_HIGH_WIDTH_SHIFT
);
99 err
= clk_enable(mdp
->clk_main
);
103 err
= clk_enable(mdp
->clk_mm
);
105 clk_disable(mdp
->clk_main
);
109 mtk_disp_pwm_update_bits(mdp
, DISP_PWM_CON_0
, PWM_CLKDIV_MASK
,
110 clk_div
<< PWM_CLKDIV_SHIFT
);
111 mtk_disp_pwm_update_bits(mdp
, DISP_PWM_CON_1
,
112 PWM_PERIOD_MASK
| PWM_HIGH_WIDTH_MASK
, value
);
113 mtk_disp_pwm_update_bits(mdp
, DISP_PWM_COMMIT
, PWM_COMMIT_MASK
, 1);
114 mtk_disp_pwm_update_bits(mdp
, DISP_PWM_COMMIT
, PWM_COMMIT_MASK
, 0);
116 clk_disable(mdp
->clk_mm
);
117 clk_disable(mdp
->clk_main
);
122 static int mtk_disp_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
124 struct mtk_disp_pwm
*mdp
= to_mtk_disp_pwm(chip
);
127 err
= clk_enable(mdp
->clk_main
);
131 err
= clk_enable(mdp
->clk_mm
);
133 clk_disable(mdp
->clk_main
);
137 mtk_disp_pwm_update_bits(mdp
, DISP_PWM_EN
, PWM_ENABLE_MASK
, 1);
142 static void mtk_disp_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
144 struct mtk_disp_pwm
*mdp
= to_mtk_disp_pwm(chip
);
146 mtk_disp_pwm_update_bits(mdp
, DISP_PWM_EN
, PWM_ENABLE_MASK
, 0);
148 clk_disable(mdp
->clk_mm
);
149 clk_disable(mdp
->clk_main
);
152 static const struct pwm_ops mtk_disp_pwm_ops
= {
153 .config
= mtk_disp_pwm_config
,
154 .enable
= mtk_disp_pwm_enable
,
155 .disable
= mtk_disp_pwm_disable
,
156 .owner
= THIS_MODULE
,
159 static int mtk_disp_pwm_probe(struct platform_device
*pdev
)
161 struct mtk_disp_pwm
*mdp
;
165 mdp
= devm_kzalloc(&pdev
->dev
, sizeof(*mdp
), GFP_KERNEL
);
169 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
170 mdp
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
171 if (IS_ERR(mdp
->base
))
172 return PTR_ERR(mdp
->base
);
174 mdp
->clk_main
= devm_clk_get(&pdev
->dev
, "main");
175 if (IS_ERR(mdp
->clk_main
))
176 return PTR_ERR(mdp
->clk_main
);
178 mdp
->clk_mm
= devm_clk_get(&pdev
->dev
, "mm");
179 if (IS_ERR(mdp
->clk_mm
))
180 return PTR_ERR(mdp
->clk_mm
);
182 ret
= clk_prepare(mdp
->clk_main
);
186 ret
= clk_prepare(mdp
->clk_mm
);
188 goto disable_clk_main
;
190 mdp
->chip
.dev
= &pdev
->dev
;
191 mdp
->chip
.ops
= &mtk_disp_pwm_ops
;
195 ret
= pwmchip_add(&mdp
->chip
);
197 dev_err(&pdev
->dev
, "pwmchip_add() failed: %d\n", ret
);
201 platform_set_drvdata(pdev
, mdp
);
206 clk_unprepare(mdp
->clk_mm
);
208 clk_unprepare(mdp
->clk_main
);
212 static int mtk_disp_pwm_remove(struct platform_device
*pdev
)
214 struct mtk_disp_pwm
*mdp
= platform_get_drvdata(pdev
);
217 ret
= pwmchip_remove(&mdp
->chip
);
218 clk_unprepare(mdp
->clk_mm
);
219 clk_unprepare(mdp
->clk_main
);
224 static const struct of_device_id mtk_disp_pwm_of_match
[] = {
225 { .compatible
= "mediatek,mt8173-disp-pwm" },
226 { .compatible
= "mediatek,mt6595-disp-pwm" },
229 MODULE_DEVICE_TABLE(of
, mtk_disp_pwm_of_match
);
231 static struct platform_driver mtk_disp_pwm_driver
= {
233 .name
= "mediatek-disp-pwm",
234 .of_match_table
= mtk_disp_pwm_of_match
,
236 .probe
= mtk_disp_pwm_probe
,
237 .remove
= mtk_disp_pwm_remove
,
239 module_platform_driver(mtk_disp_pwm_driver
);
241 MODULE_AUTHOR("YH Huang <yh.huang@mediatek.com>");
242 MODULE_DESCRIPTION("MediaTek SoC display PWM driver");
243 MODULE_LICENSE("GPL v2");