blob: b94e0d09c300f8bdc2cb23f85c39223d2968948f [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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 */
David Brazdil0f672f62019-12-10 10:32:29 +000050struct pwm_mediatek_chip {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 struct pwm_chip chip;
52 void __iomem *regs;
David Brazdil0f672f62019-12-10 10:32:29 +000053 struct clk *clk_top;
54 struct clk *clk_main;
55 struct clk **clk_pwms;
56 const struct pwm_mediatek_of_data *soc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057};
58
David Brazdil0f672f62019-12-10 10:32:29 +000059static const unsigned int pwm_mediatek_reg_offset[] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
61};
62
David Brazdil0f672f62019-12-10 10:32:29 +000063static inline struct pwm_mediatek_chip *
64to_pwm_mediatek_chip(struct pwm_chip *chip)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065{
David Brazdil0f672f62019-12-10 10:32:29 +000066 return container_of(chip, struct pwm_mediatek_chip, chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067}
68
David Brazdil0f672f62019-12-10 10:32:29 +000069static int pwm_mediatek_clk_enable(struct pwm_chip *chip,
70 struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071{
David Brazdil0f672f62019-12-10 10:32:29 +000072 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 int ret;
74
David Brazdil0f672f62019-12-10 10:32:29 +000075 ret = clk_prepare_enable(pc->clk_top);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 if (ret < 0)
77 return ret;
78
David Brazdil0f672f62019-12-10 10:32:29 +000079 ret = clk_prepare_enable(pc->clk_main);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080 if (ret < 0)
81 goto disable_clk_top;
82
David Brazdil0f672f62019-12-10 10:32:29 +000083 ret = clk_prepare_enable(pc->clk_pwms[pwm->hwpwm]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 if (ret < 0)
85 goto disable_clk_main;
86
87 return 0;
88
89disable_clk_main:
David Brazdil0f672f62019-12-10 10:32:29 +000090 clk_disable_unprepare(pc->clk_main);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091disable_clk_top:
David Brazdil0f672f62019-12-10 10:32:29 +000092 clk_disable_unprepare(pc->clk_top);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
94 return ret;
95}
96
David Brazdil0f672f62019-12-10 10:32:29 +000097static void pwm_mediatek_clk_disable(struct pwm_chip *chip,
98 struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099{
David Brazdil0f672f62019-12-10 10:32:29 +0000100 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101
David Brazdil0f672f62019-12-10 10:32:29 +0000102 clk_disable_unprepare(pc->clk_pwms[pwm->hwpwm]);
103 clk_disable_unprepare(pc->clk_main);
104 clk_disable_unprepare(pc->clk_top);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105}
106
David Brazdil0f672f62019-12-10 10:32:29 +0000107static inline u32 pwm_mediatek_readl(struct pwm_mediatek_chip *chip,
108 unsigned int num, unsigned int offset)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109{
David Brazdil0f672f62019-12-10 10:32:29 +0000110 return readl(chip->regs + pwm_mediatek_reg_offset[num] + offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111}
112
David Brazdil0f672f62019-12-10 10:32:29 +0000113static inline void pwm_mediatek_writel(struct pwm_mediatek_chip *chip,
114 unsigned int num, unsigned int offset,
115 u32 value)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116{
David Brazdil0f672f62019-12-10 10:32:29 +0000117 writel(value, chip->regs + pwm_mediatek_reg_offset[num] + offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118}
119
David Brazdil0f672f62019-12-10 10:32:29 +0000120static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
121 int duty_ns, int period_ns)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000122{
David Brazdil0f672f62019-12-10 10:32:29 +0000123 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124 u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH,
125 reg_thres = PWMTHRES;
126 u64 resolution;
127 int ret;
128
David Brazdil0f672f62019-12-10 10:32:29 +0000129 ret = pwm_mediatek_clk_enable(chip, pwm);
130
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131 if (ret < 0)
132 return ret;
133
134 /* Using resolution in picosecond gets accuracy higher */
135 resolution = (u64)NSEC_PER_SEC * 1000;
David Brazdil0f672f62019-12-10 10:32:29 +0000136 do_div(resolution, clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137
138 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
139 while (cnt_period > 8191) {
140 resolution *= 2;
141 clkdiv++;
142 cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
143 resolution);
144 }
145
146 if (clkdiv > PWM_CLK_DIV_MAX) {
David Brazdil0f672f62019-12-10 10:32:29 +0000147 pwm_mediatek_clk_disable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 dev_err(chip->dev, "period %d not supported\n", period_ns);
149 return -EINVAL;
150 }
151
152 if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) {
153 /*
154 * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
155 * from the other PWMs on MT7623.
156 */
157 reg_width = PWM45DWIDTH_FIXUP;
158 reg_thres = PWM45THRES_FIXUP;
159 }
160
161 cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
David Brazdil0f672f62019-12-10 10:32:29 +0000162 pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
163 pwm_mediatek_writel(pc, pwm->hwpwm, reg_width, cnt_period);
164 pwm_mediatek_writel(pc, pwm->hwpwm, reg_thres, cnt_duty);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165
David Brazdil0f672f62019-12-10 10:32:29 +0000166 pwm_mediatek_clk_disable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000167
168 return 0;
169}
170
David Brazdil0f672f62019-12-10 10:32:29 +0000171static int pwm_mediatek_enable(struct pwm_chip *chip, struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172{
David Brazdil0f672f62019-12-10 10:32:29 +0000173 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 u32 value;
175 int ret;
176
David Brazdil0f672f62019-12-10 10:32:29 +0000177 ret = pwm_mediatek_clk_enable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 if (ret < 0)
179 return ret;
180
181 value = readl(pc->regs);
182 value |= BIT(pwm->hwpwm);
183 writel(value, pc->regs);
184
185 return 0;
186}
187
David Brazdil0f672f62019-12-10 10:32:29 +0000188static void pwm_mediatek_disable(struct pwm_chip *chip, struct pwm_device *pwm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189{
David Brazdil0f672f62019-12-10 10:32:29 +0000190 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000191 u32 value;
192
193 value = readl(pc->regs);
194 value &= ~BIT(pwm->hwpwm);
195 writel(value, pc->regs);
196
David Brazdil0f672f62019-12-10 10:32:29 +0000197 pwm_mediatek_clk_disable(chip, pwm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198}
199
David Brazdil0f672f62019-12-10 10:32:29 +0000200static const struct pwm_ops pwm_mediatek_ops = {
201 .config = pwm_mediatek_config,
202 .enable = pwm_mediatek_enable,
203 .disable = pwm_mediatek_disable,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204 .owner = THIS_MODULE,
205};
206
David Brazdil0f672f62019-12-10 10:32:29 +0000207static int pwm_mediatek_probe(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000208{
David Brazdil0f672f62019-12-10 10:32:29 +0000209 struct pwm_mediatek_chip *pc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210 struct resource *res;
211 unsigned int i;
212 int ret;
213
214 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
215 if (!pc)
216 return -ENOMEM;
217
David Brazdil0f672f62019-12-10 10:32:29 +0000218 pc->soc = of_device_get_match_data(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000219
220 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
221 pc->regs = devm_ioremap_resource(&pdev->dev, res);
222 if (IS_ERR(pc->regs))
223 return PTR_ERR(pc->regs);
224
David Brazdil0f672f62019-12-10 10:32:29 +0000225 pc->clk_pwms = devm_kcalloc(&pdev->dev, pc->soc->num_pwms,
226 sizeof(*pc->clk_pwms), GFP_KERNEL);
227 if (!pc->clk_pwms)
228 return -ENOMEM;
229
230 pc->clk_top = devm_clk_get(&pdev->dev, "top");
231 if (IS_ERR(pc->clk_top)) {
232 dev_err(&pdev->dev, "clock: top fail: %ld\n",
233 PTR_ERR(pc->clk_top));
234 return PTR_ERR(pc->clk_top);
235 }
236
237 pc->clk_main = devm_clk_get(&pdev->dev, "main");
238 if (IS_ERR(pc->clk_main)) {
239 dev_err(&pdev->dev, "clock: main fail: %ld\n",
240 PTR_ERR(pc->clk_main));
241 return PTR_ERR(pc->clk_main);
242 }
243
244 for (i = 0; i < pc->soc->num_pwms; i++) {
245 char name[8];
246
247 snprintf(name, sizeof(name), "pwm%d", i + 1);
248
249 pc->clk_pwms[i] = devm_clk_get(&pdev->dev, name);
250 if (IS_ERR(pc->clk_pwms[i])) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251 dev_err(&pdev->dev, "clock: %s fail: %ld\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000252 name, PTR_ERR(pc->clk_pwms[i]));
253 return PTR_ERR(pc->clk_pwms[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254 }
255 }
256
257 platform_set_drvdata(pdev, pc);
258
259 pc->chip.dev = &pdev->dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000260 pc->chip.ops = &pwm_mediatek_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000261 pc->chip.base = -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000262 pc->chip.npwm = pc->soc->num_pwms;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263
264 ret = pwmchip_add(&pc->chip);
265 if (ret < 0) {
266 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
267 return ret;
268 }
269
270 return 0;
271}
272
David Brazdil0f672f62019-12-10 10:32:29 +0000273static int pwm_mediatek_remove(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274{
David Brazdil0f672f62019-12-10 10:32:29 +0000275 struct pwm_mediatek_chip *pc = platform_get_drvdata(pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276
277 return pwmchip_remove(&pc->chip);
278}
279
David Brazdil0f672f62019-12-10 10:32:29 +0000280static const struct pwm_mediatek_of_data mt2712_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281 .num_pwms = 8,
282 .pwm45_fixup = false,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283};
284
David Brazdil0f672f62019-12-10 10:32:29 +0000285static const struct pwm_mediatek_of_data mt7622_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286 .num_pwms = 6,
287 .pwm45_fixup = false,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288};
289
David Brazdil0f672f62019-12-10 10:32:29 +0000290static const struct pwm_mediatek_of_data mt7623_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291 .num_pwms = 5,
292 .pwm45_fixup = true,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000293};
294
David Brazdil0f672f62019-12-10 10:32:29 +0000295static const struct pwm_mediatek_of_data mt7628_pwm_data = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296 .num_pwms = 4,
297 .pwm45_fixup = true,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298};
299
David Brazdil0f672f62019-12-10 10:32:29 +0000300static const struct pwm_mediatek_of_data mt7629_pwm_data = {
301 .num_pwms = 1,
302 .pwm45_fixup = false,
303};
304
305static const struct pwm_mediatek_of_data mt8516_pwm_data = {
306 .num_pwms = 5,
307 .pwm45_fixup = false,
308};
309
310static const struct of_device_id pwm_mediatek_of_match[] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311 { .compatible = "mediatek,mt2712-pwm", .data = &mt2712_pwm_data },
312 { .compatible = "mediatek,mt7622-pwm", .data = &mt7622_pwm_data },
313 { .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data },
314 { .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data },
David Brazdil0f672f62019-12-10 10:32:29 +0000315 { .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data },
316 { .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317 { },
318};
David Brazdil0f672f62019-12-10 10:32:29 +0000319MODULE_DEVICE_TABLE(of, pwm_mediatek_of_match);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000320
David Brazdil0f672f62019-12-10 10:32:29 +0000321static struct platform_driver pwm_mediatek_driver = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 .driver = {
David Brazdil0f672f62019-12-10 10:32:29 +0000323 .name = "pwm-mediatek",
324 .of_match_table = pwm_mediatek_of_match,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325 },
David Brazdil0f672f62019-12-10 10:32:29 +0000326 .probe = pwm_mediatek_probe,
327 .remove = pwm_mediatek_remove,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000328};
David Brazdil0f672f62019-12-10 10:32:29 +0000329module_platform_driver(pwm_mediatek_driver);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330
331MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
David Brazdil0f672f62019-12-10 10:32:29 +0000332MODULE_LICENSE("GPL v2");