blob: e157273fd2f70c58e64b435edad4266830b0d60a [file] [log] [blame]
Antoine Ténart59d5c8b2015-10-02 16:59:47 +02001/*
2 * Marvell Berlin PWM driver
3 *
4 * Copyright (C) 2015 Marvell Technology Group Ltd.
5 *
6 * Author: Antoine Tenart <antoine.tenart@free-electrons.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/clk.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/pwm.h>
Jisheng Zhangbbf07222015-11-25 17:41:25 +080019#include <linux/slab.h>
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020020
21#define BERLIN_PWM_EN 0x0
22#define BERLIN_PWM_ENABLE BIT(0)
23#define BERLIN_PWM_CONTROL 0x4
Thomas Hebb4de445c2018-06-06 13:42:10 -040024/*
25 * The prescaler claims to support 8 different moduli, configured using the
26 * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
27 * 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be
28 * implemented by internally shifting TCNT left without adding additional
29 * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
30 * for 8, 0x1fff; and so on. This means that those moduli are entirely
31 * useless, as we could just do the shift ourselves. The 4096 modulus is
32 * implemented with a real prescaler, so we do use that, but we treat it
33 * as a flag instead of pretending the modulus is actually configurable.
34 */
35#define BERLIN_PWM_PRESCALE_4096 0x7
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020036#define BERLIN_PWM_INVERT_POLARITY BIT(3)
37#define BERLIN_PWM_DUTY 0x8
38#define BERLIN_PWM_TCNT 0xc
39#define BERLIN_PWM_MAX_TCNT 65535
40
Jisheng Zhangbbf07222015-11-25 17:41:25 +080041struct berlin_pwm_channel {
42 u32 enable;
43 u32 ctrl;
44 u32 duty;
45 u32 tcnt;
46};
47
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020048struct berlin_pwm_chip {
49 struct pwm_chip chip;
50 struct clk *clk;
51 void __iomem *base;
52};
53
54static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
55{
56 return container_of(chip, struct berlin_pwm_chip, chip);
57}
58
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +020059static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020060 unsigned int channel, unsigned long offset)
61{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +020062 return readl_relaxed(bpc->base + channel * 0x10 + offset);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020063}
64
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +020065static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020066 unsigned int channel, u32 value,
67 unsigned long offset)
68{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +020069 writel_relaxed(value, bpc->base + channel * 0x10 + offset);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020070}
71
Jisheng Zhangbbf07222015-11-25 17:41:25 +080072static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
73{
74 struct berlin_pwm_channel *channel;
75
76 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
77 if (!channel)
78 return -ENOMEM;
79
80 return pwm_set_chip_data(pwm, channel);
81}
82
83static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
84{
85 struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
86
Jisheng Zhangbbf07222015-11-25 17:41:25 +080087 kfree(channel);
88}
89
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +020090static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
Uwe Kleine-König30dffb42021-05-04 15:25:36 +020091 u64 duty_ns, u64 period_ns)
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020092{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +020093 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
Thomas Hebb4de445c2018-06-06 13:42:10 -040094 bool prescale_4096 = false;
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020095 u32 value, duty, period;
Thomas Hebb4de445c2018-06-06 13:42:10 -040096 u64 cycles;
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020097
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +020098 cycles = clk_get_rate(bpc->clk);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +020099 cycles *= period_ns;
100 do_div(cycles, NSEC_PER_SEC);
101
Thomas Hebb4de445c2018-06-06 13:42:10 -0400102 if (cycles > BERLIN_PWM_MAX_TCNT) {
103 prescale_4096 = true;
104 cycles >>= 12; // Prescaled by 4096
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200105
Thomas Hebb4de445c2018-06-06 13:42:10 -0400106 if (cycles > BERLIN_PWM_MAX_TCNT)
107 return -ERANGE;
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200108 }
109
Thomas Hebb4de445c2018-06-06 13:42:10 -0400110 period = cycles;
111 cycles *= duty_ns;
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200112 do_div(cycles, period_ns);
113 duty = cycles;
114
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200115 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
Thomas Hebb4de445c2018-06-06 13:42:10 -0400116 if (prescale_4096)
117 value |= BERLIN_PWM_PRESCALE_4096;
118 else
119 value &= ~BERLIN_PWM_PRESCALE_4096;
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200120 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200121
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200122 berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
123 berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200124
125 return 0;
126}
127
128static int berlin_pwm_set_polarity(struct pwm_chip *chip,
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200129 struct pwm_device *pwm,
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200130 enum pwm_polarity polarity)
131{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200132 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200133 u32 value;
134
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200135 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200136
137 if (polarity == PWM_POLARITY_NORMAL)
138 value &= ~BERLIN_PWM_INVERT_POLARITY;
139 else
140 value |= BERLIN_PWM_INVERT_POLARITY;
141
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200142 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200143
144 return 0;
145}
146
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200147static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200148{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200149 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200150 u32 value;
151
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200152 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200153 value |= BERLIN_PWM_ENABLE;
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200154 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200155
156 return 0;
157}
158
159static void berlin_pwm_disable(struct pwm_chip *chip,
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200160 struct pwm_device *pwm)
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200161{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200162 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200163 u32 value;
164
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200165 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200166 value &= ~BERLIN_PWM_ENABLE;
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200167 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200168}
169
Uwe Kleine-König30dffb42021-05-04 15:25:36 +0200170static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
171 const struct pwm_state *state)
172{
173 int err;
174 bool enabled = pwm->state.enabled;
175
176 if (state->polarity != pwm->state.polarity) {
177 if (enabled) {
178 berlin_pwm_disable(chip, pwm);
179 enabled = false;
180 }
181
182 err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
183 if (err)
184 return err;
185 }
186
187 if (!state->enabled) {
188 if (enabled)
189 berlin_pwm_disable(chip, pwm);
190 return 0;
191 }
192
Uwe Kleine-König7d6d4aa2021-07-01 10:27:54 +0200193 err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
194 if (err)
195 return err;
Uwe Kleine-König30dffb42021-05-04 15:25:36 +0200196
197 if (!enabled)
198 return berlin_pwm_enable(chip, pwm);
199
200 return 0;
201}
202
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200203static const struct pwm_ops berlin_pwm_ops = {
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800204 .request = berlin_pwm_request,
205 .free = berlin_pwm_free,
Uwe Kleine-König30dffb42021-05-04 15:25:36 +0200206 .apply = berlin_pwm_apply,
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200207 .owner = THIS_MODULE,
208};
209
210static const struct of_device_id berlin_pwm_match[] = {
211 { .compatible = "marvell,berlin-pwm" },
212 { },
213};
214MODULE_DEVICE_TABLE(of, berlin_pwm_match);
215
216static int berlin_pwm_probe(struct platform_device *pdev)
217{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200218 struct berlin_pwm_chip *bpc;
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200219 int ret;
220
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200221 bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL);
222 if (!bpc)
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200223 return -ENOMEM;
224
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200225 bpc->base = devm_platform_ioremap_resource(pdev, 0);
226 if (IS_ERR(bpc->base))
227 return PTR_ERR(bpc->base);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200228
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200229 bpc->clk = devm_clk_get(&pdev->dev, NULL);
230 if (IS_ERR(bpc->clk))
231 return PTR_ERR(bpc->clk);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200232
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200233 ret = clk_prepare_enable(bpc->clk);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200234 if (ret)
235 return ret;
236
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200237 bpc->chip.dev = &pdev->dev;
238 bpc->chip.ops = &berlin_pwm_ops;
239 bpc->chip.npwm = 4;
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200240
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200241 ret = pwmchip_add(&bpc->chip);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200242 if (ret < 0) {
243 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200244 clk_disable_unprepare(bpc->clk);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200245 return ret;
246 }
247
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200248 platform_set_drvdata(pdev, bpc);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200249
250 return 0;
251}
252
253static int berlin_pwm_remove(struct platform_device *pdev)
254{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200255 struct berlin_pwm_chip *bpc = platform_get_drvdata(pdev);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200256
Uwe Kleine-König0512f052021-05-04 15:25:37 +0200257 pwmchip_remove(&bpc->chip);
258
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200259 clk_disable_unprepare(bpc->clk);
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200260
Uwe Kleine-König0512f052021-05-04 15:25:37 +0200261 return 0;
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200262}
263
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800264#ifdef CONFIG_PM_SLEEP
265static int berlin_pwm_suspend(struct device *dev)
266{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200267 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800268 unsigned int i;
269
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200270 for (i = 0; i < bpc->chip.npwm; i++) {
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800271 struct berlin_pwm_channel *channel;
272
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200273 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800274 if (!channel)
275 continue;
276
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200277 channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE);
278 channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
279 channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
280 channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800281 }
282
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200283 clk_disable_unprepare(bpc->clk);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800284
285 return 0;
286}
287
288static int berlin_pwm_resume(struct device *dev)
289{
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200290 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800291 unsigned int i;
292 int ret;
293
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200294 ret = clk_prepare_enable(bpc->clk);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800295 if (ret)
296 return ret;
297
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200298 for (i = 0; i < bpc->chip.npwm; i++) {
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800299 struct berlin_pwm_channel *channel;
300
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200301 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800302 if (!channel)
303 continue;
304
Uwe Kleine-König3f3e8052021-05-04 15:25:34 +0200305 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
306 berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
307 berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
308 berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE);
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800309 }
310
311 return 0;
312}
313#endif
314
315static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
316 berlin_pwm_resume);
317
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200318static struct platform_driver berlin_pwm_driver = {
319 .probe = berlin_pwm_probe,
320 .remove = berlin_pwm_remove,
321 .driver = {
322 .name = "berlin-pwm",
323 .of_match_table = berlin_pwm_match,
Jisheng Zhangbbf07222015-11-25 17:41:25 +0800324 .pm = &berlin_pwm_pm_ops,
Antoine Ténart59d5c8b2015-10-02 16:59:47 +0200325 },
326};
327module_platform_driver(berlin_pwm_driver);
328
329MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
330MODULE_DESCRIPTION("Marvell Berlin PWM driver");
331MODULE_LICENSE("GPL v2");