1 // SPDX-License-Identifier: GPL-2.0-only
3 * MediaTek display pulse-width-modulation controller driver.
4 * Copyright (c) 2015 MediaTek Inc.
5 * Author: YH Huang <yh.huang@mediatek.com>
8 #include <linux/bitfield.h>
10 #include <linux/err.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/pwm.h>
17 #include <linux/slab.h>
19 #define DISP_PWM_EN 0x00
21 #define PWM_CLKDIV_SHIFT 16
22 #define PWM_CLKDIV_MAX 0x3ff
23 #define PWM_CLKDIV_MASK (PWM_CLKDIV_MAX << PWM_CLKDIV_SHIFT)
25 #define PWM_PERIOD_BIT_WIDTH 12
26 #define PWM_PERIOD_MASK ((1 << PWM_PERIOD_BIT_WIDTH) - 1)
28 #define PWM_HIGH_WIDTH_SHIFT 16
29 #define PWM_HIGH_WIDTH_MASK (0x1fff << PWM_HIGH_WIDTH_SHIFT)
39 unsigned int commit_mask;
41 unsigned int bls_debug;
47 const struct mtk_pwm_data *data;
54 static inline struct mtk_disp_pwm *to_mtk_disp_pwm(struct pwm_chip *chip)
56 return container_of(chip, struct mtk_disp_pwm, chip);
59 static void mtk_disp_pwm_update_bits(struct mtk_disp_pwm *mdp, u32 offset,
62 void __iomem *address = mdp->base + offset;
65 value = readl(address);
68 writel(value, address);
71 static int mtk_disp_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
72 const struct pwm_state *state)
74 struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
75 u32 clk_div, period, high_width, value;
79 if (state->polarity != PWM_POLARITY_NORMAL)
82 if (!state->enabled) {
83 mtk_disp_pwm_update_bits(mdp, DISP_PWM_EN, mdp->data->enable_mask,
87 clk_disable_unprepare(mdp->clk_mm);
88 clk_disable_unprepare(mdp->clk_main);
96 err = clk_prepare_enable(mdp->clk_main);
98 dev_err(chip->dev, "Can't enable mdp->clk_main: %pe\n",
103 err = clk_prepare_enable(mdp->clk_mm);
105 dev_err(chip->dev, "Can't enable mdp->clk_mm: %pe\n",
107 clk_disable_unprepare(mdp->clk_main);
113 * Find period, high_width and clk_div to suit duty_ns and period_ns.
114 * Calculate proper div value to keep period value in the bound.
116 * period_ns = 10^9 * (clk_div + 1) * (period + 1) / PWM_CLK_RATE
117 * duty_ns = 10^9 * (clk_div + 1) * high_width / PWM_CLK_RATE
119 * period = (PWM_CLK_RATE * period_ns) / (10^9 * (clk_div + 1)) - 1
120 * high_width = (PWM_CLK_RATE * duty_ns) / (10^9 * (clk_div + 1))
122 rate = clk_get_rate(mdp->clk_main);
123 clk_div = mul_u64_u64_div_u64(state->period, rate, NSEC_PER_SEC) >>
124 PWM_PERIOD_BIT_WIDTH;
125 if (clk_div > PWM_CLKDIV_MAX) {
127 clk_disable_unprepare(mdp->clk_mm);
128 clk_disable_unprepare(mdp->clk_main);
133 div = NSEC_PER_SEC * (clk_div + 1);
134 period = mul_u64_u64_div_u64(state->period, rate, div);
138 high_width = mul_u64_u64_div_u64(state->duty_cycle, rate, div);
139 value = period | (high_width << PWM_HIGH_WIDTH_SHIFT);
141 mtk_disp_pwm_update_bits(mdp, mdp->data->con0,
143 clk_div << PWM_CLKDIV_SHIFT);
144 mtk_disp_pwm_update_bits(mdp, mdp->data->con1,
145 PWM_PERIOD_MASK | PWM_HIGH_WIDTH_MASK,
148 if (mdp->data->has_commit) {
149 mtk_disp_pwm_update_bits(mdp, mdp->data->commit,
150 mdp->data->commit_mask,
151 mdp->data->commit_mask);
152 mtk_disp_pwm_update_bits(mdp, mdp->data->commit,
153 mdp->data->commit_mask,
157 * For MT2701, disable double buffer before writing register
158 * and select manual mode and use PWM_PERIOD/PWM_HIGH_WIDTH.
160 mtk_disp_pwm_update_bits(mdp, mdp->data->bls_debug,
161 mdp->data->bls_debug_mask,
162 mdp->data->bls_debug_mask);
163 mtk_disp_pwm_update_bits(mdp, mdp->data->con0,
165 mdp->data->con0_sel);
168 mtk_disp_pwm_update_bits(mdp, DISP_PWM_EN, mdp->data->enable_mask,
169 mdp->data->enable_mask);
175 static void mtk_disp_pwm_get_state(struct pwm_chip *chip,
176 struct pwm_device *pwm,
177 struct pwm_state *state)
179 struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
180 u64 rate, period, high_width;
181 u32 clk_div, con0, con1;
184 err = clk_prepare_enable(mdp->clk_main);
186 dev_err(chip->dev, "Can't enable mdp->clk_main: %pe\n", ERR_PTR(err));
190 err = clk_prepare_enable(mdp->clk_mm);
192 dev_err(chip->dev, "Can't enable mdp->clk_mm: %pe\n", ERR_PTR(err));
193 clk_disable_unprepare(mdp->clk_main);
197 rate = clk_get_rate(mdp->clk_main);
198 con0 = readl(mdp->base + mdp->data->con0);
199 con1 = readl(mdp->base + mdp->data->con1);
200 state->enabled = !!(con0 & BIT(0));
201 clk_div = FIELD_GET(PWM_CLKDIV_MASK, con0);
202 period = FIELD_GET(PWM_PERIOD_MASK, con1);
204 * period has 12 bits, clk_div 11 and NSEC_PER_SEC has 30,
205 * so period * (clk_div + 1) * NSEC_PER_SEC doesn't overflow.
207 state->period = DIV64_U64_ROUND_UP(period * (clk_div + 1) * NSEC_PER_SEC, rate);
208 high_width = FIELD_GET(PWM_HIGH_WIDTH_MASK, con1);
209 state->duty_cycle = DIV64_U64_ROUND_UP(high_width * (clk_div + 1) * NSEC_PER_SEC,
211 state->polarity = PWM_POLARITY_NORMAL;
212 clk_disable_unprepare(mdp->clk_mm);
213 clk_disable_unprepare(mdp->clk_main);
216 static const struct pwm_ops mtk_disp_pwm_ops = {
217 .apply = mtk_disp_pwm_apply,
218 .get_state = mtk_disp_pwm_get_state,
219 .owner = THIS_MODULE,
222 static int mtk_disp_pwm_probe(struct platform_device *pdev)
224 struct mtk_disp_pwm *mdp;
227 mdp = devm_kzalloc(&pdev->dev, sizeof(*mdp), GFP_KERNEL);
231 mdp->data = of_device_get_match_data(&pdev->dev);
233 mdp->base = devm_platform_ioremap_resource(pdev, 0);
234 if (IS_ERR(mdp->base))
235 return PTR_ERR(mdp->base);
237 mdp->clk_main = devm_clk_get(&pdev->dev, "main");
238 if (IS_ERR(mdp->clk_main))
239 return PTR_ERR(mdp->clk_main);
241 mdp->clk_mm = devm_clk_get(&pdev->dev, "mm");
242 if (IS_ERR(mdp->clk_mm))
243 return PTR_ERR(mdp->clk_mm);
245 mdp->chip.dev = &pdev->dev;
246 mdp->chip.ops = &mtk_disp_pwm_ops;
249 ret = pwmchip_add(&mdp->chip);
251 dev_err(&pdev->dev, "pwmchip_add() failed: %pe\n", ERR_PTR(ret));
255 platform_set_drvdata(pdev, mdp);
260 static int mtk_disp_pwm_remove(struct platform_device *pdev)
262 struct mtk_disp_pwm *mdp = platform_get_drvdata(pdev);
264 pwmchip_remove(&mdp->chip);
269 static const struct mtk_pwm_data mt2701_pwm_data = {
270 .enable_mask = BIT(16),
276 .bls_debug_mask = 0x3,
279 static const struct mtk_pwm_data mt8173_pwm_data = {
280 .enable_mask = BIT(0),
289 static const struct mtk_pwm_data mt8183_pwm_data = {
290 .enable_mask = BIT(0),
296 .bls_debug_mask = 0x3,
299 static const struct of_device_id mtk_disp_pwm_of_match[] = {
300 { .compatible = "mediatek,mt2701-disp-pwm", .data = &mt2701_pwm_data},
301 { .compatible = "mediatek,mt6595-disp-pwm", .data = &mt8173_pwm_data},
302 { .compatible = "mediatek,mt8173-disp-pwm", .data = &mt8173_pwm_data},
303 { .compatible = "mediatek,mt8183-disp-pwm", .data = &mt8183_pwm_data},
306 MODULE_DEVICE_TABLE(of, mtk_disp_pwm_of_match);
308 static struct platform_driver mtk_disp_pwm_driver = {
310 .name = "mediatek-disp-pwm",
311 .of_match_table = mtk_disp_pwm_of_match,
313 .probe = mtk_disp_pwm_probe,
314 .remove = mtk_disp_pwm_remove,
316 module_platform_driver(mtk_disp_pwm_driver);
318 MODULE_AUTHOR("YH Huang <yh.huang@mediatek.com>");
319 MODULE_DESCRIPTION("MediaTek SoC display PWM driver");
320 MODULE_LICENSE("GPL v2");