blob: ab001ce55178eabf019d5992c23ff75746afb6a2 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
David Brazdil0f672f62019-12-10 10:32:29 +00003 * MediaTek Pulse Width Modulator driver
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 *
5 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
6 * Copyright (C) 2017 Zhi Mao <zhi.mao@mediatek.com>
7 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9
10#include <linux/err.h>
11#include <linux/io.h>
12#include <linux/ioport.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/clk.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
19#include <linux/pwm.h>
20#include <linux/slab.h>
21#include <linux/types.h>
22
23/* PWM registers and bits definitions */
24#define PWMCON 0x00
25#define PWMHDUR 0x04
26#define PWMLDUR 0x08
27#define PWMGDUR 0x0c
28#define PWMWAVENUM 0x28
29#define PWMDWIDTH 0x2c
30#define PWM45DWIDTH_FIXUP 0x30
31#define PWMTHRES 0x30
32#define PWM45THRES_FIXUP 0x34
33
34#define PWM_CLK_DIV_MAX 7
35
David Brazdil0f672f62019-12-10 10:32:29 +000036struct pwm_mediatek_of_data {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 unsigned int num_pwms;
38 bool pwm45_fixup;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039};
40
41/**
David Brazdil0f672f62019-12-10 10:32:29 +000042 * struct pwm_mediatek_chip - struct representing PWM chip
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 * @chip: linux PWM chip representation
44 * @regs: base address of PWM chip
David Brazdil0f672f62019-12-10 10:32:29 +000045 * @clk_top: the top clock generator
46 * @clk_main: the clock used by PWM core
47 * @clk_pwms: the clock used by each PWM channel
48 * @clk_freq: the fix clock frequency of legacy MIPS SoC
Olivier Deprez157378f2022-04-04 15:47:50 +020049 * @soc: pointer to chip's platform data
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 */
David Brazdil0f672f62019-12-10 10:32:29 +000051struct pwm_mediatek_chip {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052 struct pwm_chip chip;
53 void __iomem *regs;
David Brazdil0f672f62019-12-10 10:32:29 +000054 struct clk *clk_top;
55 struct clk *clk_main;
56 struct clk **clk_pwms;
57 const struct pwm_mediatek_of_data *soc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058};
59
David Brazdil0f672f62019-12-10 10:32:29 +000060static const unsigned int pwm_mediatek_reg_offset[] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
62};
63
David Brazdil0f672f62019-12-10 10:32:29 +000064static inline struct pwm_mediatek_chip *
65to_pwm_mediatek_chip(struct pwm_chip *chip)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066{
David Brazdil0f672f62019-12-10 10:32:29 +000067 return container_of(chip, struct pwm_mediatek_chip, chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068}
69
David Brazdil0f672f62019-12-10 10:32:29 +000070static int pwm_mediatek_clk_enable(struct pwm_chip *chip,
71 struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072{
David Brazdil0f672f62019-12-10 10:32:29 +000073 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 int ret;
75
David Brazdil0f672f62019-12-10 10:32:29 +000076 ret = clk_prepare_enable(pc->clk_top);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 if (ret < 0)
78 return ret;
79
David Brazdil0f672f62019-12-10 10:32:29 +000080 ret = clk_prepare_enable(pc->clk_main);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081 if (ret < 0)
82 goto disable_clk_top;
83
David Brazdil0f672f62019-12-10 10:32:29 +000084 ret = clk_prepare_enable(pc->clk_pwms[pwm->hwpwm]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085 if (ret < 0)
86 goto disable_clk_main;
87
88 return 0;
89
90disable_clk_main:
David Brazdil0f672f62019-12-10 10:32:29 +000091 clk_disable_unprepare(pc->clk_main);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092disable_clk_top:
David Brazdil0f672f62019-12-10 10:32:29 +000093 clk_disable_unprepare(pc->clk_top);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094
95 return ret;
96}
97
David Brazdil0f672f62019-12-10 10:32:29 +000098static void pwm_mediatek_clk_disable(struct pwm_chip *chip,
99 struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100{
David Brazdil0f672f62019-12-10 10:32:29 +0000101 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102
David Brazdil0f672f62019-12-10 10:32:29 +0000103 clk_disable_unprepare(pc->clk_pwms[pwm->hwpwm]);
104 clk_disable_unprepare(pc->clk_main);
105 clk_disable_unprepare(pc->clk_top);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106}
107
David Brazdil0f672f62019-12-10 10:32:29 +0000108static inline u32 pwm_mediatek_readl(struct pwm_mediatek_chip *chip,
109 unsigned int num, unsigned int offset)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110{
David Brazdil0f672f62019-12-10 10:32:29 +0000111 return readl(chip->regs + pwm_mediatek_reg_offset[num] + offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112}
113
David Brazdil0f672f62019-12-10 10:32:29 +0000114static inline void pwm_mediatek_writel(struct pwm_mediatek_chip *chip,
115 unsigned int num, unsigned int offset,
116 u32 value)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117{
David Brazdil0f672f62019-12-10 10:32:29 +0000118 writel(value, chip->regs + pwm_mediatek_reg_offset[num] + offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119}
120
David Brazdil0f672f62019-12-10 10:32:29 +0000121static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
122 int duty_ns, int period_ns)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123{
David Brazdil0f672f62019-12-10 10:32:29 +0000124 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH,
126 reg_thres = PWMTHRES;
127 u64 resolution;
128 int ret;
129
David Brazdil0f672f62019-12-10 10:32:29 +0000130 ret = pwm_mediatek_clk_enable(chip, pwm);
131
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 if (ret < 0)
133 return ret;
134
135 /* Using resolution in picosecond gets accuracy higher */
136 resolution = (u64)NSEC_PER_SEC * 1000;
David Brazdil0f672f62019-12-10 10:32:29 +0000137 do_div(resolution, clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138
139 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
140 while (cnt_period > 8191) {
141 resolution *= 2;
142 clkdiv++;
143 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
144 resolution);
145 }
146
147 if (clkdiv > PWM_CLK_DIV_MAX) {
David Brazdil0f672f62019-12-10 10:32:29 +0000148 pwm_mediatek_clk_disable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000149 dev_err(chip->dev, "period %d not supported\n", period_ns);
150 return -EINVAL;
151 }
152
153 if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) {
154 /*
155 * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
156 * from the other PWMs on MT7623.
157 */
158 reg_width = PWM45DWIDTH_FIXUP;
159 reg_thres = PWM45THRES_FIXUP;
160 }
161
162 cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
David Brazdil0f672f62019-12-10 10:32:29 +0000163 pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
164 pwm_mediatek_writel(pc, pwm->hwpwm, reg_width, cnt_period);
165 pwm_mediatek_writel(pc, pwm->hwpwm, reg_thres, cnt_duty);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000166
David Brazdil0f672f62019-12-10 10:32:29 +0000167 pwm_mediatek_clk_disable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000168
169 return 0;
170}
171
David Brazdil0f672f62019-12-10 10:32:29 +0000172static int pwm_mediatek_enable(struct pwm_chip *chip, struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000173{
David Brazdil0f672f62019-12-10 10:32:29 +0000174 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000175 u32 value;
176 int ret;
177
David Brazdil0f672f62019-12-10 10:32:29 +0000178 ret = pwm_mediatek_clk_enable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179 if (ret < 0)
180 return ret;
181
182 value = readl(pc->regs);
183 value |= BIT(pwm->hwpwm);
184 writel(value, pc->regs);
185
186 return 0;
187}
188
David Brazdil0f672f62019-12-10 10:32:29 +0000189static void pwm_mediatek_disable(struct pwm_chip *chip, struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190{
David Brazdil0f672f62019-12-10 10:32:29 +0000191 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000192 u32 value;
193
194 value = readl(pc->regs);
195 value &= ~BIT(pwm->hwpwm);
196 writel(value, pc->regs);
197
David Brazdil0f672f62019-12-10 10:32:29 +0000198 pwm_mediatek_clk_disable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199}
200
David Brazdil0f672f62019-12-10 10:32:29 +0000201static const struct pwm_ops pwm_mediatek_ops = {
202 .config = pwm_mediatek_config,
203 .enable = pwm_mediatek_enable,
204 .disable = pwm_mediatek_disable,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000205 .owner = THIS_MODULE,
206};
207
David Brazdil0f672f62019-12-10 10:32:29 +0000208static int pwm_mediatek_probe(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000209{
David Brazdil0f672f62019-12-10 10:32:29 +0000210 struct pwm_mediatek_chip *pc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000211 struct resource *res;
212 unsigned int i;
213 int ret;
214
215 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
216 if (!pc)
217 return -ENOMEM;
218
David Brazdil0f672f62019-12-10 10:32:29 +0000219 pc->soc = of_device_get_match_data(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220
221 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
222 pc->regs = devm_ioremap_resource(&pdev->dev, res);
223 if (IS_ERR(pc->regs))
224 return PTR_ERR(pc->regs);
225
David Brazdil0f672f62019-12-10 10:32:29 +0000226 pc->clk_pwms = devm_kcalloc(&pdev->dev, pc->soc->num_pwms,
227 sizeof(*pc->clk_pwms), GFP_KERNEL);
228 if (!pc->clk_pwms)
229 return -ENOMEM;
230
231 pc->clk_top = devm_clk_get(&pdev->dev, "top");
232 if (IS_ERR(pc->clk_top)) {
233 dev_err(&pdev->dev, "clock: top fail: %ld\n",
234 PTR_ERR(pc->clk_top));
235 return PTR_ERR(pc->clk_top);
236 }
237
238 pc->clk_main = devm_clk_get(&pdev->dev, "main");
239 if (IS_ERR(pc->clk_main)) {
240 dev_err(&pdev->dev, "clock: main fail: %ld\n",
241 PTR_ERR(pc->clk_main));
242 return PTR_ERR(pc->clk_main);
243 }
244
245 for (i = 0; i < pc->soc->num_pwms; i++) {
246 char name[8];
247
248 snprintf(name, sizeof(name), "pwm%d", i + 1);
249
250 pc->clk_pwms[i] = devm_clk_get(&pdev->dev, name);
251 if (IS_ERR(pc->clk_pwms[i])) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252 dev_err(&pdev->dev, "clock: %s fail: %ld\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000253 name, PTR_ERR(pc->clk_pwms[i]));
254 return PTR_ERR(pc->clk_pwms[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255 }
256 }
257
258 platform_set_drvdata(pdev, pc);
259
260 pc->chip.dev = &pdev->dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000261 pc->chip.ops = &pwm_mediatek_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262 pc->chip.base = -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000263 pc->chip.npwm = pc->soc->num_pwms;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000264
265 ret = pwmchip_add(&pc->chip);
266 if (ret < 0) {
267 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
268 return ret;
269 }
270
271 return 0;
272}
273
David Brazdil0f672f62019-12-10 10:32:29 +0000274static int pwm_mediatek_remove(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275{
David Brazdil0f672f62019-12-10 10:32:29 +0000276 struct pwm_mediatek_chip *pc = platform_get_drvdata(pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277
278 return pwmchip_remove(&pc->chip);
279}
280
David Brazdil0f672f62019-12-10 10:32:29 +0000281static const struct pwm_mediatek_of_data mt2712_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000282 .num_pwms = 8,
283 .pwm45_fixup = false,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000284};
285
David Brazdil0f672f62019-12-10 10:32:29 +0000286static const struct pwm_mediatek_of_data mt7622_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000287 .num_pwms = 6,
288 .pwm45_fixup = false,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289};
290
David Brazdil0f672f62019-12-10 10:32:29 +0000291static const struct pwm_mediatek_of_data mt7623_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292 .num_pwms = 5,
293 .pwm45_fixup = true,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000294};
295
David Brazdil0f672f62019-12-10 10:32:29 +0000296static const struct pwm_mediatek_of_data mt7628_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297 .num_pwms = 4,
298 .pwm45_fixup = true,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299};
300
David Brazdil0f672f62019-12-10 10:32:29 +0000301static const struct pwm_mediatek_of_data mt7629_pwm_data = {
302 .num_pwms = 1,
303 .pwm45_fixup = false,
304};
305
306static const struct pwm_mediatek_of_data mt8516_pwm_data = {
307 .num_pwms = 5,
308 .pwm45_fixup = false,
309};
310
311static const struct of_device_id pwm_mediatek_of_match[] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000312 { .compatible = "mediatek,mt2712-pwm", .data = &mt2712_pwm_data },
313 { .compatible = "mediatek,mt7622-pwm", .data = &mt7622_pwm_data },
314 { .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data },
315 { .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data },
David Brazdil0f672f62019-12-10 10:32:29 +0000316 { .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data },
317 { .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000318 { },
319};
David Brazdil0f672f62019-12-10 10:32:29 +0000320MODULE_DEVICE_TABLE(of, pwm_mediatek_of_match);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321
David Brazdil0f672f62019-12-10 10:32:29 +0000322static struct platform_driver pwm_mediatek_driver = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000323 .driver = {
David Brazdil0f672f62019-12-10 10:32:29 +0000324 .name = "pwm-mediatek",
325 .of_match_table = pwm_mediatek_of_match,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326 },
David Brazdil0f672f62019-12-10 10:32:29 +0000327 .probe = pwm_mediatek_probe,
328 .remove = pwm_mediatek_remove,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329};
David Brazdil0f672f62019-12-10 10:32:29 +0000330module_platform_driver(pwm_mediatek_driver);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000331
332MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
David Brazdil0f672f62019-12-10 10:32:29 +0000333MODULE_LICENSE("GPL v2");