1 From 6f5941c93bdf7649f392f1263b9068d360ceab4d Mon Sep 17 00:00:00 2001
2 From: John Crispin <john@phrozen.org>
3 Date: Fri, 6 May 2016 02:55:48 +0200
4 Subject: [PATCH 071/102] pwm: add pwm-mediatek
6 Signed-off-by: John Crispin <john@phrozen.org>
8 arch/arm/boot/dts/mt7623-evb.dts | 17 +++
9 arch/arm/boot/dts/mt7623.dtsi | 22 ++++
10 drivers/pwm/Kconfig | 9 ++
11 drivers/pwm/Makefile | 1 +
12 drivers/pwm/pwm-mediatek.c | 230 ++++++++++++++++++++++++++++++++++++++
13 5 files changed, 279 insertions(+)
14 create mode 100644 drivers/pwm/pwm-mediatek.c
16 --- a/arch/arm/boot/dts/mt7623-evb.dts
17 +++ b/arch/arm/boot/dts/mt7623-evb.dts
25 + pinmux = <MT7623_PIN_204_PWM1_FUNC_PWM1>;
29 + pinmux = <MT7623_PIN_205_PWM2_FUNC_PWM2>;
37 mediatek,reset-pin = <&pio 15 0>;
42 + pinctrl-names = "default";
43 + pinctrl-0 = <&pwm_pins>;
46 --- a/arch/arm/boot/dts/mt7623.dtsi
47 +++ b/arch/arm/boot/dts/mt7623.dtsi
53 + compatible = "mediatek,mt7623-pwm";
55 + reg = <0 0x11006000 0 0x1000>;
57 + resets = <&pericfg MT2701_PERI_PWM_SW_RST>;
58 + reset-names = "pwm";
61 + clocks = <&topckgen CLK_TOP_PWM_SEL>,
62 + <&pericfg CLK_PERI_PWM>,
63 + <&pericfg CLK_PERI_PWM1>,
64 + <&pericfg CLK_PERI_PWM2>,
65 + <&pericfg CLK_PERI_PWM3>,
66 + <&pericfg CLK_PERI_PWM4>,
67 + <&pericfg CLK_PERI_PWM5>;
68 + clock-names = "top", "main", "pwm1", "pwm2",
69 + "pwm3", "pwm4", "pwm5";
71 + status = "disabled";
75 compatible = "mediatek,mt7623-spi", "mediatek,mt6589-spi";
76 reg = <0 0x1100a000 0 0x1000>;
77 --- a/drivers/pwm/Kconfig
78 +++ b/drivers/pwm/Kconfig
79 @@ -260,6 +260,15 @@ config PWM_MTK_DISP
80 To compile this driver as a module, choose M here: the module
81 will be called pwm-mtk-disp.
84 + tristate "MediaTek PWM support"
85 + depends on ARCH_MEDIATEK || COMPILE_TEST
87 + Generic PWM framework driver for Mediatek ARM SoC.
89 + To compile this driver as a module, choose M here: the module
90 + will be called pwm-mxs.
93 tristate "Freescale MXS PWM support"
94 depends on ARCH_MXS && OF
95 --- a/drivers/pwm/Makefile
96 +++ b/drivers/pwm/Makefile
97 @@ -22,6 +22,7 @@ obj-$(CONFIG_PWM_LPC32XX) += pwm-lpc32xx
98 obj-$(CONFIG_PWM_LPSS) += pwm-lpss.o
99 obj-$(CONFIG_PWM_LPSS_PCI) += pwm-lpss-pci.o
100 obj-$(CONFIG_PWM_LPSS_PLATFORM) += pwm-lpss-platform.o
101 +obj-$(CONFIG_PWM_MEDIATEK) += pwm-mediatek.o
102 obj-$(CONFIG_PWM_MTK_DISP) += pwm-mtk-disp.o
103 obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
104 obj-$(CONFIG_PWM_PCA9685) += pwm-pca9685.o
106 +++ b/drivers/pwm/pwm-mediatek.c
109 + * Mediatek Pulse Width Modulator driver
111 + * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
113 + * This file is licensed under the terms of the GNU General Public
114 + * License version 2. This program is licensed "as is" without any
115 + * warranty of any kind, whether express or implied.
118 +#include <linux/err.h>
119 +#include <linux/io.h>
120 +#include <linux/ioport.h>
121 +#include <linux/kernel.h>
122 +#include <linux/module.h>
123 +#include <linux/clk.h>
124 +#include <linux/of.h>
125 +#include <linux/platform_device.h>
126 +#include <linux/pwm.h>
127 +#include <linux/slab.h>
128 +#include <linux/types.h>
132 +/* PWM registers and bits definitions */
134 +#define PWMHDUR 0x04
135 +#define PWMLDUR 0x08
136 +#define PWMGDUR 0x0c
137 +#define PWMWAVENUM 0x28
138 +#define PWMDWIDTH 0x2c
139 +#define PWMTHRES 0x30
142 + * struct mtk_pwm_chip - struct representing pwm chip
144 + * @mmio_base: base address of pwm chip
145 + * @chip: linux pwm chip representation
147 +struct mtk_pwm_chip {
148 + void __iomem *mmio_base;
149 + struct pwm_chip chip;
150 + struct clk *clk_top;
151 + struct clk *clk_main;
152 + struct clk *clk_pwm[NUM_PWM];
155 +static inline struct mtk_pwm_chip *to_mtk_pwm_chip(struct pwm_chip *chip)
157 + return container_of(chip, struct mtk_pwm_chip, chip);
160 +static inline u32 mtk_pwm_readl(struct mtk_pwm_chip *chip, unsigned int num,
161 + unsigned long offset)
163 + return ioread32(chip->mmio_base + 0x10 + (num * 0x40) + offset);
166 +static inline void mtk_pwm_writel(struct mtk_pwm_chip *chip,
167 + unsigned int num, unsigned long offset,
170 + iowrite32(val, chip->mmio_base + 0x10 + (num * 0x40) + offset);
173 +static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
174 + int duty_ns, int period_ns)
176 + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
177 + u32 resolution = 100 / 4;
180 + resolution = 1000000000 / (clk_get_rate(pc->clk_pwm[pwm->hwpwm]));
182 + while (period_ns / resolution > 8191) {
190 + mtk_pwm_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | BIT(3) | clkdiv);
191 + mtk_pwm_writel(pc, pwm->hwpwm, PWMDWIDTH, period_ns / resolution);
192 + mtk_pwm_writel(pc, pwm->hwpwm, PWMTHRES, duty_ns / resolution);
196 +static int mtk_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
198 + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
202 + ret = clk_prepare(pc->clk_pwm[pwm->hwpwm]);
206 + val = ioread32(pc->mmio_base);
207 + val |= BIT(pwm->hwpwm);
208 + iowrite32(val, pc->mmio_base);
213 +static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
215 + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
218 + val = ioread32(pc->mmio_base);
219 + val &= ~BIT(pwm->hwpwm);
220 + iowrite32(val, pc->mmio_base);
221 + clk_unprepare(pc->clk_pwm[pwm->hwpwm]);
224 +static const struct pwm_ops mtk_pwm_ops = {
225 + .config = mtk_pwm_config,
226 + .enable = mtk_pwm_enable,
227 + .disable = mtk_pwm_disable,
228 + .owner = THIS_MODULE,
231 +static int mtk_pwm_probe(struct platform_device *pdev)
233 + struct mtk_pwm_chip *pc;
234 + struct resource *r;
237 + pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
241 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
242 + pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
243 + if (IS_ERR(pc->mmio_base))
244 + return PTR_ERR(pc->mmio_base);
246 + pc->clk_main = devm_clk_get(&pdev->dev, "main");
247 + if (IS_ERR(pc->clk_main))
248 + return PTR_ERR(pc->clk_main);
250 + pc->clk_top = devm_clk_get(&pdev->dev, "top");
251 + if (IS_ERR(pc->clk_top))
252 + return PTR_ERR(pc->clk_top);
254 + pc->clk_pwm[0] = devm_clk_get(&pdev->dev, "pwm1");
255 + if (IS_ERR(pc->clk_pwm[0]))
256 + return PTR_ERR(pc->clk_pwm[0]);
258 + pc->clk_pwm[1] = devm_clk_get(&pdev->dev, "pwm2");
259 + if (IS_ERR(pc->clk_pwm[1]))
260 + return PTR_ERR(pc->clk_pwm[1]);
262 + pc->clk_pwm[2] = devm_clk_get(&pdev->dev, "pwm3");
263 + if (IS_ERR(pc->clk_pwm[2]))
264 + return PTR_ERR(pc->clk_pwm[2]);
266 + pc->clk_pwm[3] = devm_clk_get(&pdev->dev, "pwm4");
267 + if (IS_ERR(pc->clk_pwm[3]))
268 + return PTR_ERR(pc->clk_pwm[3]);
270 + pc->clk_pwm[4] = devm_clk_get(&pdev->dev, "pwm5");
271 + if (IS_ERR(pc->clk_pwm[4]))
272 + return PTR_ERR(pc->clk_pwm[4]);
274 + ret = clk_prepare(pc->clk_top);
278 + ret = clk_prepare(pc->clk_main);
280 + goto disable_clk_top;
282 + platform_set_drvdata(pdev, pc);
284 + pc->chip.dev = &pdev->dev;
285 + pc->chip.ops = &mtk_pwm_ops;
286 + pc->chip.base = -1;
287 + pc->chip.npwm = NUM_PWM;
289 + ret = pwmchip_add(&pc->chip);
291 + dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
292 + goto disable_clk_main;
298 + clk_unprepare(pc->clk_main);
300 + clk_unprepare(pc->clk_top);
305 +static int mtk_pwm_remove(struct platform_device *pdev)
307 + struct mtk_pwm_chip *pc = platform_get_drvdata(pdev);
310 + for (i = 0; i < NUM_PWM; i++)
311 + pwm_disable(&pc->chip.pwms[i]);
313 + return pwmchip_remove(&pc->chip);
316 +static const struct of_device_id mtk_pwm_of_match[] = {
317 + { .compatible = "mediatek,mt7623-pwm" },
321 +MODULE_DEVICE_TABLE(of, mtk_pwm_of_match);
323 +static struct platform_driver mtk_pwm_driver = {
326 + .owner = THIS_MODULE,
327 + .of_match_table = mtk_pwm_of_match,
329 + .probe = mtk_pwm_probe,
330 + .remove = mtk_pwm_remove,
333 +module_platform_driver(mtk_pwm_driver);
335 +MODULE_LICENSE("GPL");
336 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
337 +MODULE_ALIAS("platform:mtk-pwm");