blob: 6370459983180c6bbe5944653ca1b72fe3fe976a [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>
19#include <linux/platform_device.h>
20#include <linux/pwm.h>
21#include <linux/slab.h>
22#include <linux/types.h>
23
24/* PWM registers and bits definitions */
25#define PWMCON 0x00
26#define PWMHDUR 0x04
27#define PWMLDUR 0x08
28#define PWMGDUR 0x0c
29#define PWMWAVENUM 0x28
30#define PWMDWIDTH 0x2c
31#define PWMTHRES 0x30
32
33enum {
34 MTK_CLK_MAIN = 0,
35 MTK_CLK_TOP,
36 MTK_CLK_PWM1,
37 MTK_CLK_PWM2,
38 MTK_CLK_PWM3,
39 MTK_CLK_PWM4,
40 MTK_CLK_PWM5,
41 MTK_CLK_MAX,
42};
43
44static const char * const mtk_pwm_clk_name[] = {
45 "main", "top", "pwm1", "pwm2", "pwm3", "pwm4", "pwm5"
46};
47
48/**
49 * struct mtk_pwm_chip - struct representing PWM chip
50 * @chip: linux PWM chip representation
51 * @regs: base address of PWM chip
52 * @clks: list of clocks
53 */
54struct mtk_pwm_chip {
55 struct pwm_chip chip;
56 void __iomem *regs;
57 struct clk *clks[MTK_CLK_MAX];
58};
59
60static inline struct mtk_pwm_chip *to_mtk_pwm_chip(struct pwm_chip *chip)
61{
62 return container_of(chip, struct mtk_pwm_chip, chip);
63}
64
Zhi Maoe7c197e2017-06-30 14:05:18 +080065static int mtk_pwm_clk_enable(struct pwm_chip *chip, struct pwm_device *pwm)
66{
67 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
68 int ret;
69
70 ret = clk_prepare_enable(pc->clks[MTK_CLK_TOP]);
71 if (ret < 0)
72 return ret;
73
74 ret = clk_prepare_enable(pc->clks[MTK_CLK_MAIN]);
75 if (ret < 0)
76 goto disable_clk_top;
77
78 ret = clk_prepare_enable(pc->clks[MTK_CLK_PWM1 + pwm->hwpwm]);
79 if (ret < 0)
80 goto disable_clk_main;
81
82 return 0;
83
84disable_clk_main:
85 clk_disable_unprepare(pc->clks[MTK_CLK_MAIN]);
86disable_clk_top:
87 clk_disable_unprepare(pc->clks[MTK_CLK_TOP]);
88
89 return ret;
90}
91
92static void mtk_pwm_clk_disable(struct pwm_chip *chip, struct pwm_device *pwm)
93{
94 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
95
96 clk_disable_unprepare(pc->clks[MTK_CLK_PWM1 + pwm->hwpwm]);
97 clk_disable_unprepare(pc->clks[MTK_CLK_MAIN]);
98 clk_disable_unprepare(pc->clks[MTK_CLK_TOP]);
99}
100
John Crispincaf065f2017-01-23 19:34:37 +0100101static inline u32 mtk_pwm_readl(struct mtk_pwm_chip *chip, unsigned int num,
102 unsigned int offset)
103{
104 return readl(chip->regs + 0x10 + (num * 0x40) + offset);
105}
106
107static inline void mtk_pwm_writel(struct mtk_pwm_chip *chip,
108 unsigned int num, unsigned int offset,
109 u32 value)
110{
111 writel(value, chip->regs + 0x10 + (num * 0x40) + offset);
112}
113
114static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
115 int duty_ns, int period_ns)
116{
117 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
118 struct clk *clk = pc->clks[MTK_CLK_PWM1 + pwm->hwpwm];
119 u32 resolution, clkdiv = 0;
Zhi Maoe7c197e2017-06-30 14:05:18 +0800120 int ret;
121
122 ret = mtk_pwm_clk_enable(chip, pwm);
123 if (ret < 0)
124 return ret;
John Crispincaf065f2017-01-23 19:34:37 +0100125
126 resolution = NSEC_PER_SEC / clk_get_rate(clk);
127
128 while (period_ns / resolution > 8191) {
129 resolution *= 2;
130 clkdiv++;
131 }
132
133 if (clkdiv > 7)
134 return -EINVAL;
135
Zhi Maocd307982017-06-30 14:05:17 +0800136 mtk_pwm_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
John Crispincaf065f2017-01-23 19:34:37 +0100137 mtk_pwm_writel(pc, pwm->hwpwm, PWMDWIDTH, period_ns / resolution);
138 mtk_pwm_writel(pc, pwm->hwpwm, PWMTHRES, duty_ns / resolution);
139
Zhi Maoe7c197e2017-06-30 14:05:18 +0800140 mtk_pwm_clk_disable(chip, pwm);
141
John Crispincaf065f2017-01-23 19:34:37 +0100142 return 0;
143}
144
145static int mtk_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
146{
147 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
148 u32 value;
149 int ret;
150
Zhi Maoe7c197e2017-06-30 14:05:18 +0800151 ret = mtk_pwm_clk_enable(chip, pwm);
John Crispincaf065f2017-01-23 19:34:37 +0100152 if (ret < 0)
153 return ret;
154
155 value = readl(pc->regs);
156 value |= BIT(pwm->hwpwm);
157 writel(value, pc->regs);
158
159 return 0;
160}
161
162static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
163{
164 struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
165 u32 value;
166
167 value = readl(pc->regs);
168 value &= ~BIT(pwm->hwpwm);
169 writel(value, pc->regs);
170
Zhi Maoe7c197e2017-06-30 14:05:18 +0800171 mtk_pwm_clk_disable(chip, pwm);
John Crispincaf065f2017-01-23 19:34:37 +0100172}
173
174static const struct pwm_ops mtk_pwm_ops = {
175 .config = mtk_pwm_config,
176 .enable = mtk_pwm_enable,
177 .disable = mtk_pwm_disable,
178 .owner = THIS_MODULE,
179};
180
181static int mtk_pwm_probe(struct platform_device *pdev)
182{
183 struct mtk_pwm_chip *pc;
184 struct resource *res;
185 unsigned int i;
186 int ret;
187
188 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
189 if (!pc)
190 return -ENOMEM;
191
192 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 pc->regs = devm_ioremap_resource(&pdev->dev, res);
194 if (IS_ERR(pc->regs))
195 return PTR_ERR(pc->regs);
196
197 for (i = 0; i < MTK_CLK_MAX; i++) {
198 pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
199 if (IS_ERR(pc->clks[i]))
200 return PTR_ERR(pc->clks[i]);
201 }
202
John Crispincaf065f2017-01-23 19:34:37 +0100203 platform_set_drvdata(pdev, pc);
204
205 pc->chip.dev = &pdev->dev;
206 pc->chip.ops = &mtk_pwm_ops;
207 pc->chip.base = -1;
208 pc->chip.npwm = 5;
209
210 ret = pwmchip_add(&pc->chip);
211 if (ret < 0) {
212 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
Zhi Maoe7c197e2017-06-30 14:05:18 +0800213 return ret;
John Crispincaf065f2017-01-23 19:34:37 +0100214 }
215
216 return 0;
John Crispincaf065f2017-01-23 19:34:37 +0100217}
218
219static int mtk_pwm_remove(struct platform_device *pdev)
220{
221 struct mtk_pwm_chip *pc = platform_get_drvdata(pdev);
John Crispincaf065f2017-01-23 19:34:37 +0100222
223 return pwmchip_remove(&pc->chip);
224}
225
226static const struct of_device_id mtk_pwm_of_match[] = {
227 { .compatible = "mediatek,mt7623-pwm" },
228 { }
229};
230MODULE_DEVICE_TABLE(of, mtk_pwm_of_match);
231
232static struct platform_driver mtk_pwm_driver = {
233 .driver = {
234 .name = "mtk-pwm",
John Crispincaf065f2017-01-23 19:34:37 +0100235 .of_match_table = mtk_pwm_of_match,
236 },
237 .probe = mtk_pwm_probe,
238 .remove = mtk_pwm_remove,
239};
240module_platform_driver(mtk_pwm_driver);
241
242MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
243MODULE_ALIAS("platform:mtk-pwm");
244MODULE_LICENSE("GPL");