Loading...
1/*
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/mod_devicetable.h>
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/pwm.h>
20#include <linux/slab.h>
21
22#define BERLIN_PWM_EN 0x0
23#define BERLIN_PWM_ENABLE BIT(0)
24#define BERLIN_PWM_CONTROL 0x4
25/*
26 * The prescaler claims to support 8 different moduli, configured using the
27 * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
28 * 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be
29 * implemented by internally shifting TCNT left without adding additional
30 * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
31 * for 8, 0x1fff; and so on. This means that those moduli are entirely
32 * useless, as we could just do the shift ourselves. The 4096 modulus is
33 * implemented with a real prescaler, so we do use that, but we treat it
34 * as a flag instead of pretending the modulus is actually configurable.
35 */
36#define BERLIN_PWM_PRESCALE_4096 0x7
37#define BERLIN_PWM_INVERT_POLARITY BIT(3)
38#define BERLIN_PWM_DUTY 0x8
39#define BERLIN_PWM_TCNT 0xc
40#define BERLIN_PWM_MAX_TCNT 65535
41
42#define BERLIN_PWM_NUMPWMS 4
43
44struct berlin_pwm_channel {
45 u32 enable;
46 u32 ctrl;
47 u32 duty;
48 u32 tcnt;
49};
50
51struct berlin_pwm_chip {
52 struct pwm_chip chip;
53 struct clk *clk;
54 void __iomem *base;
55 struct berlin_pwm_channel channel[BERLIN_PWM_NUMPWMS];
56};
57
58static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
59{
60 return container_of(chip, struct berlin_pwm_chip, chip);
61}
62
63static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
64 unsigned int channel, unsigned long offset)
65{
66 return readl_relaxed(bpc->base + channel * 0x10 + offset);
67}
68
69static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
70 unsigned int channel, u32 value,
71 unsigned long offset)
72{
73 writel_relaxed(value, bpc->base + channel * 0x10 + offset);
74}
75
76static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
77 u64 duty_ns, u64 period_ns)
78{
79 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
80 bool prescale_4096 = false;
81 u32 value, duty, period;
82 u64 cycles;
83
84 cycles = clk_get_rate(bpc->clk);
85 cycles *= period_ns;
86 do_div(cycles, NSEC_PER_SEC);
87
88 if (cycles > BERLIN_PWM_MAX_TCNT) {
89 prescale_4096 = true;
90 cycles >>= 12; // Prescaled by 4096
91
92 if (cycles > BERLIN_PWM_MAX_TCNT)
93 return -ERANGE;
94 }
95
96 period = cycles;
97 cycles *= duty_ns;
98 do_div(cycles, period_ns);
99 duty = cycles;
100
101 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
102 if (prescale_4096)
103 value |= BERLIN_PWM_PRESCALE_4096;
104 else
105 value &= ~BERLIN_PWM_PRESCALE_4096;
106 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
107
108 berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
109 berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
110
111 return 0;
112}
113
114static int berlin_pwm_set_polarity(struct pwm_chip *chip,
115 struct pwm_device *pwm,
116 enum pwm_polarity polarity)
117{
118 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
119 u32 value;
120
121 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
122
123 if (polarity == PWM_POLARITY_NORMAL)
124 value &= ~BERLIN_PWM_INVERT_POLARITY;
125 else
126 value |= BERLIN_PWM_INVERT_POLARITY;
127
128 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
129
130 return 0;
131}
132
133static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
134{
135 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
136 u32 value;
137
138 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
139 value |= BERLIN_PWM_ENABLE;
140 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
141
142 return 0;
143}
144
145static void berlin_pwm_disable(struct pwm_chip *chip,
146 struct pwm_device *pwm)
147{
148 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
149 u32 value;
150
151 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
152 value &= ~BERLIN_PWM_ENABLE;
153 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
154}
155
156static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
157 const struct pwm_state *state)
158{
159 int err;
160 bool enabled = pwm->state.enabled;
161
162 if (state->polarity != pwm->state.polarity) {
163 if (enabled) {
164 berlin_pwm_disable(chip, pwm);
165 enabled = false;
166 }
167
168 err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
169 if (err)
170 return err;
171 }
172
173 if (!state->enabled) {
174 if (enabled)
175 berlin_pwm_disable(chip, pwm);
176 return 0;
177 }
178
179 err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
180 if (err)
181 return err;
182
183 if (!enabled)
184 return berlin_pwm_enable(chip, pwm);
185
186 return 0;
187}
188
189static const struct pwm_ops berlin_pwm_ops = {
190 .apply = berlin_pwm_apply,
191};
192
193static const struct of_device_id berlin_pwm_match[] = {
194 { .compatible = "marvell,berlin-pwm" },
195 { },
196};
197MODULE_DEVICE_TABLE(of, berlin_pwm_match);
198
199static int berlin_pwm_probe(struct platform_device *pdev)
200{
201 struct berlin_pwm_chip *bpc;
202 int ret;
203
204 bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL);
205 if (!bpc)
206 return -ENOMEM;
207
208 bpc->base = devm_platform_ioremap_resource(pdev, 0);
209 if (IS_ERR(bpc->base))
210 return PTR_ERR(bpc->base);
211
212 bpc->clk = devm_clk_get_enabled(&pdev->dev, NULL);
213 if (IS_ERR(bpc->clk))
214 return PTR_ERR(bpc->clk);
215
216 bpc->chip.dev = &pdev->dev;
217 bpc->chip.ops = &berlin_pwm_ops;
218 bpc->chip.npwm = BERLIN_PWM_NUMPWMS;
219
220 ret = devm_pwmchip_add(&pdev->dev, &bpc->chip);
221 if (ret < 0)
222 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
223
224 platform_set_drvdata(pdev, bpc);
225
226 return 0;
227}
228
229static int berlin_pwm_suspend(struct device *dev)
230{
231 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
232 unsigned int i;
233
234 for (i = 0; i < bpc->chip.npwm; i++) {
235 struct berlin_pwm_channel *channel = &bpc->channel[i];
236
237 channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE);
238 channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
239 channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
240 channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
241 }
242
243 clk_disable_unprepare(bpc->clk);
244
245 return 0;
246}
247
248static int berlin_pwm_resume(struct device *dev)
249{
250 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
251 unsigned int i;
252 int ret;
253
254 ret = clk_prepare_enable(bpc->clk);
255 if (ret)
256 return ret;
257
258 for (i = 0; i < bpc->chip.npwm; i++) {
259 struct berlin_pwm_channel *channel = &bpc->channel[i];
260
261 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
262 berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
263 berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
264 berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE);
265 }
266
267 return 0;
268}
269
270static DEFINE_SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
271 berlin_pwm_resume);
272
273static struct platform_driver berlin_pwm_driver = {
274 .probe = berlin_pwm_probe,
275 .driver = {
276 .name = "berlin-pwm",
277 .of_match_table = berlin_pwm_match,
278 .pm = pm_ptr(&berlin_pwm_pm_ops),
279 },
280};
281module_platform_driver(berlin_pwm_driver);
282
283MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
284MODULE_DESCRIPTION("Marvell Berlin PWM driver");
285MODULE_LICENSE("GPL v2");
1/*
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>
19#include <linux/slab.h>
20
21#define BERLIN_PWM_EN 0x0
22#define BERLIN_PWM_ENABLE BIT(0)
23#define BERLIN_PWM_CONTROL 0x4
24/*
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
36#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
41struct berlin_pwm_channel {
42 u32 enable;
43 u32 ctrl;
44 u32 duty;
45 u32 tcnt;
46};
47
48struct 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
59static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip,
60 unsigned int channel, unsigned long offset)
61{
62 return readl_relaxed(chip->base + channel * 0x10 + offset);
63}
64
65static inline void berlin_pwm_writel(struct berlin_pwm_chip *chip,
66 unsigned int channel, u32 value,
67 unsigned long offset)
68{
69 writel_relaxed(value, chip->base + channel * 0x10 + offset);
70}
71
72static 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
87 kfree(channel);
88}
89
90static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
91 int duty_ns, int period_ns)
92{
93 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
94 bool prescale_4096 = false;
95 u32 value, duty, period;
96 u64 cycles;
97
98 cycles = clk_get_rate(pwm->clk);
99 cycles *= period_ns;
100 do_div(cycles, NSEC_PER_SEC);
101
102 if (cycles > BERLIN_PWM_MAX_TCNT) {
103 prescale_4096 = true;
104 cycles >>= 12; // Prescaled by 4096
105
106 if (cycles > BERLIN_PWM_MAX_TCNT)
107 return -ERANGE;
108 }
109
110 period = cycles;
111 cycles *= duty_ns;
112 do_div(cycles, period_ns);
113 duty = cycles;
114
115 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
116 if (prescale_4096)
117 value |= BERLIN_PWM_PRESCALE_4096;
118 else
119 value &= ~BERLIN_PWM_PRESCALE_4096;
120 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
121
122 berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);
123 berlin_pwm_writel(pwm, pwm_dev->hwpwm, period, BERLIN_PWM_TCNT);
124
125 return 0;
126}
127
128static int berlin_pwm_set_polarity(struct pwm_chip *chip,
129 struct pwm_device *pwm_dev,
130 enum pwm_polarity polarity)
131{
132 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
133 u32 value;
134
135 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
136
137 if (polarity == PWM_POLARITY_NORMAL)
138 value &= ~BERLIN_PWM_INVERT_POLARITY;
139 else
140 value |= BERLIN_PWM_INVERT_POLARITY;
141
142 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
143
144 return 0;
145}
146
147static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm_dev)
148{
149 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
150 u32 value;
151
152 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
153 value |= BERLIN_PWM_ENABLE;
154 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
155
156 return 0;
157}
158
159static void berlin_pwm_disable(struct pwm_chip *chip,
160 struct pwm_device *pwm_dev)
161{
162 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
163 u32 value;
164
165 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
166 value &= ~BERLIN_PWM_ENABLE;
167 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
168}
169
170static const struct pwm_ops berlin_pwm_ops = {
171 .request = berlin_pwm_request,
172 .free = berlin_pwm_free,
173 .config = berlin_pwm_config,
174 .set_polarity = berlin_pwm_set_polarity,
175 .enable = berlin_pwm_enable,
176 .disable = berlin_pwm_disable,
177 .owner = THIS_MODULE,
178};
179
180static const struct of_device_id berlin_pwm_match[] = {
181 { .compatible = "marvell,berlin-pwm" },
182 { },
183};
184MODULE_DEVICE_TABLE(of, berlin_pwm_match);
185
186static int berlin_pwm_probe(struct platform_device *pdev)
187{
188 struct berlin_pwm_chip *pwm;
189 struct resource *res;
190 int ret;
191
192 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
193 if (!pwm)
194 return -ENOMEM;
195
196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 pwm->base = devm_ioremap_resource(&pdev->dev, res);
198 if (IS_ERR(pwm->base))
199 return PTR_ERR(pwm->base);
200
201 pwm->clk = devm_clk_get(&pdev->dev, NULL);
202 if (IS_ERR(pwm->clk))
203 return PTR_ERR(pwm->clk);
204
205 ret = clk_prepare_enable(pwm->clk);
206 if (ret)
207 return ret;
208
209 pwm->chip.dev = &pdev->dev;
210 pwm->chip.ops = &berlin_pwm_ops;
211 pwm->chip.base = -1;
212 pwm->chip.npwm = 4;
213 pwm->chip.of_xlate = of_pwm_xlate_with_flags;
214 pwm->chip.of_pwm_n_cells = 3;
215
216 ret = pwmchip_add(&pwm->chip);
217 if (ret < 0) {
218 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
219 clk_disable_unprepare(pwm->clk);
220 return ret;
221 }
222
223 platform_set_drvdata(pdev, pwm);
224
225 return 0;
226}
227
228static int berlin_pwm_remove(struct platform_device *pdev)
229{
230 struct berlin_pwm_chip *pwm = platform_get_drvdata(pdev);
231 int ret;
232
233 ret = pwmchip_remove(&pwm->chip);
234 clk_disable_unprepare(pwm->clk);
235
236 return ret;
237}
238
239#ifdef CONFIG_PM_SLEEP
240static int berlin_pwm_suspend(struct device *dev)
241{
242 struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
243 unsigned int i;
244
245 for (i = 0; i < pwm->chip.npwm; i++) {
246 struct berlin_pwm_channel *channel;
247
248 channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
249 if (!channel)
250 continue;
251
252 channel->enable = berlin_pwm_readl(pwm, i, BERLIN_PWM_ENABLE);
253 channel->ctrl = berlin_pwm_readl(pwm, i, BERLIN_PWM_CONTROL);
254 channel->duty = berlin_pwm_readl(pwm, i, BERLIN_PWM_DUTY);
255 channel->tcnt = berlin_pwm_readl(pwm, i, BERLIN_PWM_TCNT);
256 }
257
258 clk_disable_unprepare(pwm->clk);
259
260 return 0;
261}
262
263static int berlin_pwm_resume(struct device *dev)
264{
265 struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
266 unsigned int i;
267 int ret;
268
269 ret = clk_prepare_enable(pwm->clk);
270 if (ret)
271 return ret;
272
273 for (i = 0; i < pwm->chip.npwm; i++) {
274 struct berlin_pwm_channel *channel;
275
276 channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
277 if (!channel)
278 continue;
279
280 berlin_pwm_writel(pwm, i, channel->ctrl, BERLIN_PWM_CONTROL);
281 berlin_pwm_writel(pwm, i, channel->duty, BERLIN_PWM_DUTY);
282 berlin_pwm_writel(pwm, i, channel->tcnt, BERLIN_PWM_TCNT);
283 berlin_pwm_writel(pwm, i, channel->enable, BERLIN_PWM_ENABLE);
284 }
285
286 return 0;
287}
288#endif
289
290static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
291 berlin_pwm_resume);
292
293static struct platform_driver berlin_pwm_driver = {
294 .probe = berlin_pwm_probe,
295 .remove = berlin_pwm_remove,
296 .driver = {
297 .name = "berlin-pwm",
298 .of_match_table = berlin_pwm_match,
299 .pm = &berlin_pwm_pm_ops,
300 },
301};
302module_platform_driver(berlin_pwm_driver);
303
304MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
305MODULE_DESCRIPTION("Marvell Berlin PWM driver");
306MODULE_LICENSE("GPL v2");