blob: db986b77e556d6f7d6c17782c597abb54855579d [file] [log] [blame]
John Crispincaf065f2017-01-23 19:34:37 +01001/*
2 * Mediatek Pulse Width Modulator driver
3 *
4 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
Zhi Maoe7c197e2017-06-30 14:05:18 +08005 * Copyright (C) 2017 Zhi Mao <zhi.mao@mediatek.com>
John Crispincaf065f2017-01-23 19:34:37 +01006 *
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.
10 */
11
12#include <linux/err.h>
13#include <linux/io.h>
14#include <linux/ioport.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/clk.h>
18#include <linux/of.h>
Zhi Mao424268c2017-10-25 18:11:01 +080019#include <linux/of_device.h>
John Crispincaf065f2017-01-23 19:34:37 +010020#include <linux/platform_device.h>
21#include <linux/pwm.h>
22#include <linux/slab.h>
23#include <linux/types.h>
24
25/* PWM registers and bits definitions */
26#define PWMCON 0x00
27#define PWMHDUR 0x04
28#define PWMLDUR 0x08
29#define PWMGDUR 0x0c
30#define PWMWAVENUM 0x28
31#define PWMDWIDTH 0x2c
Sean Wang360cc032018-03-01 16:19:12 +080032#define PWM45DWIDTH_FIXUP 0x30
John Crispincaf065f2017-01-23 19:34:37 +010033#define PWMTHRES 0x30
Sean Wang360cc032018-03-01 16:19:12 +080034#define PWM45THRES_FIXUP 0x34
John Crispincaf065f2017-01-23 19:34:37 +010035
Zhi Mao8bdb65d2017-06-30 14:05:20 +080036#define PWM_CLK_DIV_MAX 7
37
Zhi Mao424268c2017-10-25 18:11:01 +080038struct mtk_pwm_platform_data {
39 unsigned int num_pwms;
Sean Wang360cc032018-03-01 16:19:12 +080040 bool pwm45_fixup;
John Crispincaf065f2017-01-23 19:34:37 +010041};
42
43/**
44 * struct mtk_pwm_chip - struct representing PWM chip
45 * @chip: linux PWM chip representation
46 * @regs: base address of PWM chip
Sam Shihefecdeb2019-09-20 06:49:04 +080047 * @clk_top: the top clock generator
48 * @clk_main: the clock used by PWM core
49 * @clk_pwms: the clock used by each PWM channel
50 * @clk_freq: the fix clock frequency of legacy MIPS SoC
John Crispincaf065f2017-01-23 19:34:37 +010051 */
52struct mtk_pwm_chip {
53 struct pwm_chip chip;
54 void __iomem *regs;
Sam Shihefecdeb2019-09-20 06:49:04 +080055 struct clk *clk_top;
56 struct clk *clk_main;
57 struct clk **clk_pwms;
Sean Wang360cc032018-03-01 16:19:12 +080058 const struct mtk_pwm_platform_data *soc;
John Crispincaf065f2017-01-23 19:34:37 +010059};
60
Zhi Mao424268c2017-10-25 18:11:01 +080061static const unsigned int mtk_pwm_reg_offset[] = {
62 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
63};
64
John Crispincaf065f2017-01-23 19:34:37 +010065static inline struct mtk_pwm_chip *to_mtk_pwm_chip(struct pwm_chip *chip)
66{
67 return container_of(chip, struct mtk_pwm_chip, chip);
68}
69
Zhi Maoe7c197e2017-06-30 14:05:18 +080070static int mtk_pwm_clk_enable(struct pwm_chip *chip, struct pwm_device *pwm)
71{
72 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
73 int ret;
74
Sam Shihefecdeb2019-09-20 06:49:04 +080075 ret = clk_prepare_enable(pc->clk_top);
Zhi Maoe7c197e2017-06-30 14:05:18 +080076 if (ret < 0)
77 return ret;
78
Sam Shihefecdeb2019-09-20 06:49:04 +080079 ret = clk_prepare_enable(pc->clk_main);
Zhi Maoe7c197e2017-06-30 14:05:18 +080080 if (ret < 0)
81 goto disable_clk_top;
82
Sam Shihefecdeb2019-09-20 06:49:04 +080083 ret = clk_prepare_enable(pc->clk_pwms[pwm->hwpwm]);
Zhi Maoe7c197e2017-06-30 14:05:18 +080084 if (ret < 0)
85 goto disable_clk_main;
86
87 return 0;
88
89disable_clk_main:
Sam Shihefecdeb2019-09-20 06:49:04 +080090 clk_disable_unprepare(pc->clk_main);
Zhi Maoe7c197e2017-06-30 14:05:18 +080091disable_clk_top:
Sam Shihefecdeb2019-09-20 06:49:04 +080092 clk_disable_unprepare(pc->clk_top);
Zhi Maoe7c197e2017-06-30 14:05:18 +080093
94 return ret;
95}
96
97static void mtk_pwm_clk_disable(struct pwm_chip *chip, struct pwm_device *pwm)
98{
99 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
100
Sam Shihefecdeb2019-09-20 06:49:04 +0800101 clk_disable_unprepare(pc->clk_pwms[pwm->hwpwm]);
102 clk_disable_unprepare(pc->clk_main);
103 clk_disable_unprepare(pc->clk_top);
Zhi Maoe7c197e2017-06-30 14:05:18 +0800104}
105
John Crispincaf065f2017-01-23 19:34:37 +0100106static inline u32 mtk_pwm_readl(struct mtk_pwm_chip *chip, unsigned int num,
107 unsigned int offset)
108{
Zhi Mao424268c2017-10-25 18:11:01 +0800109 return readl(chip->regs + mtk_pwm_reg_offset[num] + offset);
John Crispincaf065f2017-01-23 19:34:37 +0100110}
111
112static inline void mtk_pwm_writel(struct mtk_pwm_chip *chip,
113 unsigned int num, unsigned int offset,
114 u32 value)
115{
Zhi Mao424268c2017-10-25 18:11:01 +0800116 writel(value, chip->regs + mtk_pwm_reg_offset[num] + offset);
John Crispincaf065f2017-01-23 19:34:37 +0100117}
118
119static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
120 int duty_ns, int period_ns)
121{
122 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
Sam Shihefecdeb2019-09-20 06:49:04 +0800123 struct clk *clk = pc->clk_pwms[pwm->hwpwm];
Sean Wang04c0a4e2018-03-02 16:49:14 +0800124 u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH,
Sean Wang360cc032018-03-01 16:19:12 +0800125 reg_thres = PWMTHRES;
Sean Wang04c0a4e2018-03-02 16:49:14 +0800126 u64 resolution;
Zhi Maoe7c197e2017-06-30 14:05:18 +0800127 int ret;
128
129 ret = mtk_pwm_clk_enable(chip, pwm);
130 if (ret < 0)
131 return ret;
John Crispincaf065f2017-01-23 19:34:37 +0100132
Sean Wang04c0a4e2018-03-02 16:49:14 +0800133 /* Using resolution in picosecond gets accuracy higher */
134 resolution = (u64)NSEC_PER_SEC * 1000;
135 do_div(resolution, clk_get_rate(clk));
John Crispincaf065f2017-01-23 19:34:37 +0100136
Sean Wang04c0a4e2018-03-02 16:49:14 +0800137 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
138 while (cnt_period > 8191) {
John Crispincaf065f2017-01-23 19:34:37 +0100139 resolution *= 2;
140 clkdiv++;
Sean Wang04c0a4e2018-03-02 16:49:14 +0800141 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
142 resolution);
John Crispincaf065f2017-01-23 19:34:37 +0100143 }
144
Zhi Mao8bdb65d2017-06-30 14:05:20 +0800145 if (clkdiv > PWM_CLK_DIV_MAX) {
146 mtk_pwm_clk_disable(chip, pwm);
147 dev_err(chip->dev, "period %d not supported\n", period_ns);
John Crispincaf065f2017-01-23 19:34:37 +0100148 return -EINVAL;
Zhi Mao8bdb65d2017-06-30 14:05:20 +0800149 }
John Crispincaf065f2017-01-23 19:34:37 +0100150
Sean Wang360cc032018-03-01 16:19:12 +0800151 if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) {
152 /*
153 * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
154 * from the other PWMs on MT7623.
155 */
156 reg_width = PWM45DWIDTH_FIXUP;
157 reg_thres = PWM45THRES_FIXUP;
158 }
159
Sean Wang04c0a4e2018-03-02 16:49:14 +0800160 cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
Zhi Maocd307982017-06-30 14:05:17 +0800161 mtk_pwm_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
Sean Wang04c0a4e2018-03-02 16:49:14 +0800162 mtk_pwm_writel(pc, pwm->hwpwm, reg_width, cnt_period);
163 mtk_pwm_writel(pc, pwm->hwpwm, reg_thres, cnt_duty);
John Crispincaf065f2017-01-23 19:34:37 +0100164
Zhi Maoe7c197e2017-06-30 14:05:18 +0800165 mtk_pwm_clk_disable(chip, pwm);
166
John Crispincaf065f2017-01-23 19:34:37 +0100167 return 0;
168}
169
170static int mtk_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
171{
172 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
173 u32 value;
174 int ret;
175
Zhi Maoe7c197e2017-06-30 14:05:18 +0800176 ret = mtk_pwm_clk_enable(chip, pwm);
John Crispincaf065f2017-01-23 19:34:37 +0100177 if (ret < 0)
178 return ret;
179
180 value = readl(pc->regs);
181 value |= BIT(pwm->hwpwm);
182 writel(value, pc->regs);
183
184 return 0;
185}
186
187static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
188{
189 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
190 u32 value;
191
192 value = readl(pc->regs);
193 value &= ~BIT(pwm->hwpwm);
194 writel(value, pc->regs);
195
Zhi Maoe7c197e2017-06-30 14:05:18 +0800196 mtk_pwm_clk_disable(chip, pwm);
John Crispincaf065f2017-01-23 19:34:37 +0100197}
198
199static const struct pwm_ops mtk_pwm_ops = {
200 .config = mtk_pwm_config,
201 .enable = mtk_pwm_enable,
202 .disable = mtk_pwm_disable,
203 .owner = THIS_MODULE,
204};
205
206static int mtk_pwm_probe(struct platform_device *pdev)
207{
208 struct mtk_pwm_chip *pc;
209 struct resource *res;
210 unsigned int i;
211 int ret;
212
213 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
214 if (!pc)
215 return -ENOMEM;
216
Sam Shihe6c7c252019-09-20 06:49:02 +0800217 pc->soc = of_device_get_match_data(&pdev->dev);
Zhi Mao424268c2017-10-25 18:11:01 +0800218
John Crispincaf065f2017-01-23 19:34:37 +0100219 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
220 pc->regs = devm_ioremap_resource(&pdev->dev, res);
221 if (IS_ERR(pc->regs))
222 return PTR_ERR(pc->regs);
223
Sam Shihefecdeb2019-09-20 06:49:04 +0800224 pc->clk_pwms = devm_kcalloc(&pdev->dev, pc->soc->num_pwms,
225 sizeof(*pc->clk_pwms), GFP_KERNEL);
226 if (!pc->clk_pwms)
227 return -ENOMEM;
228
229 pc->clk_top = devm_clk_get(&pdev->dev, "top");
230 if (IS_ERR(pc->clk_top)) {
231 dev_err(&pdev->dev, "clock: top fail: %ld\n",
232 PTR_ERR(pc->clk_top));
233 return PTR_ERR(pc->clk_top);
234 }
235
236 pc->clk_main = devm_clk_get(&pdev->dev, "main");
237 if (IS_ERR(pc->clk_main)) {
238 dev_err(&pdev->dev, "clock: main fail: %ld\n",
239 PTR_ERR(pc->clk_main));
240 return PTR_ERR(pc->clk_main);
241 }
242
243 for (i = 0; i < pc->soc->num_pwms; i++) {
244 char name[8];
245
246 snprintf(name, sizeof(name), "pwm%d", i + 1);
247
248 pc->clk_pwms[i] = devm_clk_get(&pdev->dev, name);
249 if (IS_ERR(pc->clk_pwms[i])) {
Zhi Mao424268c2017-10-25 18:11:01 +0800250 dev_err(&pdev->dev, "clock: %s fail: %ld\n",
Sam Shihefecdeb2019-09-20 06:49:04 +0800251 name, PTR_ERR(pc->clk_pwms[i]));
252 return PTR_ERR(pc->clk_pwms[i]);
Zhi Mao424268c2017-10-25 18:11:01 +0800253 }
John Crispincaf065f2017-01-23 19:34:37 +0100254 }
255
John Crispincaf065f2017-01-23 19:34:37 +0100256 platform_set_drvdata(pdev, pc);
257
258 pc->chip.dev = &pdev->dev;
259 pc->chip.ops = &mtk_pwm_ops;
260 pc->chip.base = -1;
Sam Shihe6c7c252019-09-20 06:49:02 +0800261 pc->chip.npwm = pc->soc->num_pwms;
John Crispincaf065f2017-01-23 19:34:37 +0100262
263 ret = pwmchip_add(&pc->chip);
264 if (ret < 0) {
265 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
Zhi Maoe7c197e2017-06-30 14:05:18 +0800266 return ret;
John Crispincaf065f2017-01-23 19:34:37 +0100267 }
268
269 return 0;
John Crispincaf065f2017-01-23 19:34:37 +0100270}
271
272static int mtk_pwm_remove(struct platform_device *pdev)
273{
274 struct mtk_pwm_chip *pc = platform_get_drvdata(pdev);
John Crispincaf065f2017-01-23 19:34:37 +0100275
276 return pwmchip_remove(&pc->chip);
277}
278
Zhi Mao424268c2017-10-25 18:11:01 +0800279static const struct mtk_pwm_platform_data mt2712_pwm_data = {
280 .num_pwms = 8,
Sean Wang360cc032018-03-01 16:19:12 +0800281 .pwm45_fixup = false,
Zhi Mao424268c2017-10-25 18:11:01 +0800282};
283
284static const struct mtk_pwm_platform_data mt7622_pwm_data = {
285 .num_pwms = 6,
Sean Wang360cc032018-03-01 16:19:12 +0800286 .pwm45_fixup = false,
Zhi Mao424268c2017-10-25 18:11:01 +0800287};
288
289static const struct mtk_pwm_platform_data mt7623_pwm_data = {
290 .num_pwms = 5,
Sean Wang360cc032018-03-01 16:19:12 +0800291 .pwm45_fixup = true,
John Crispin8cdc43a2018-07-25 11:52:09 +0200292};
293
294static const struct mtk_pwm_platform_data mt7628_pwm_data = {
295 .num_pwms = 4,
296 .pwm45_fixup = true,
Zhi Mao424268c2017-10-25 18:11:01 +0800297};
298
Fabien Parent8d190722019-08-05 14:58:48 +0200299static const struct mtk_pwm_platform_data mt8516_pwm_data = {
300 .num_pwms = 5,
301 .pwm45_fixup = false,
Fabien Parent8d190722019-08-05 14:58:48 +0200302};
303
John Crispincaf065f2017-01-23 19:34:37 +0100304static const struct of_device_id mtk_pwm_of_match[] = {
Zhi Mao424268c2017-10-25 18:11:01 +0800305 { .compatible = "mediatek,mt2712-pwm", .data = &mt2712_pwm_data },
306 { .compatible = "mediatek,mt7622-pwm", .data = &mt7622_pwm_data },
307 { .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data },
John Crispin8cdc43a2018-07-25 11:52:09 +0200308 { .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data },
Fabien Parent8d190722019-08-05 14:58:48 +0200309 { .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data },
Zhi Mao424268c2017-10-25 18:11:01 +0800310 { },
John Crispincaf065f2017-01-23 19:34:37 +0100311};
312MODULE_DEVICE_TABLE(of, mtk_pwm_of_match);
313
314static struct platform_driver mtk_pwm_driver = {
315 .driver = {
316 .name = "mtk-pwm",
John Crispincaf065f2017-01-23 19:34:37 +0100317 .of_match_table = mtk_pwm_of_match,
318 },
319 .probe = mtk_pwm_probe,
320 .remove = mtk_pwm_remove,
321};
322module_platform_driver(mtk_pwm_driver);
323
324MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
John Crispincaf065f2017-01-23 19:34:37 +0100325MODULE_LICENSE("GPL");