inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / pwm / pwm-berlin.c
blob01339c152ab00bb69b4ff1ee0c87c096151bd91c
1 /*
2 * Marvell Berlin PWM driver
4 * Copyright (C) 2015 Marvell Technology Group Ltd.
6 * Author: Antoine Tenart <antoine.tenart@free-electrons.com>
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.
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>
21 #define BERLIN_PWM_EN 0x0
22 #define BERLIN_PWM_ENABLE BIT(0)
23 #define BERLIN_PWM_CONTROL 0x4
24 #define BERLIN_PWM_PRESCALE_MASK 0x7
25 #define BERLIN_PWM_PRESCALE_MAX 4096
26 #define BERLIN_PWM_INVERT_POLARITY BIT(3)
27 #define BERLIN_PWM_DUTY 0x8
28 #define BERLIN_PWM_TCNT 0xc
29 #define BERLIN_PWM_MAX_TCNT 65535
31 struct berlin_pwm_channel {
32 u32 enable;
33 u32 ctrl;
34 u32 duty;
35 u32 tcnt;
38 struct berlin_pwm_chip {
39 struct pwm_chip chip;
40 struct clk *clk;
41 void __iomem *base;
44 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
46 return container_of(chip, struct berlin_pwm_chip, chip);
49 static const u32 prescaler_table[] = {
50 1, 4, 8, 16, 64, 256, 1024, 4096
53 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip,
54 unsigned int channel, unsigned long offset)
56 return readl_relaxed(chip->base + channel * 0x10 + offset);
59 static inline void berlin_pwm_writel(struct berlin_pwm_chip *chip,
60 unsigned int channel, u32 value,
61 unsigned long offset)
63 writel_relaxed(value, chip->base + channel * 0x10 + offset);
66 static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
68 struct berlin_pwm_channel *channel;
70 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
71 if (!channel)
72 return -ENOMEM;
74 return pwm_set_chip_data(pwm, channel);
77 static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
79 struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
81 pwm_set_chip_data(pwm, NULL);
82 kfree(channel);
85 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
86 int duty_ns, int period_ns)
88 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
89 unsigned int prescale;
90 u32 value, duty, period;
91 u64 cycles, tmp;
93 cycles = clk_get_rate(pwm->clk);
94 cycles *= period_ns;
95 do_div(cycles, NSEC_PER_SEC);
97 for (prescale = 0; prescale < ARRAY_SIZE(prescaler_table); prescale++) {
98 tmp = cycles;
99 do_div(tmp, prescaler_table[prescale]);
101 if (tmp <= BERLIN_PWM_MAX_TCNT)
102 break;
105 if (tmp > BERLIN_PWM_MAX_TCNT)
106 return -ERANGE;
108 period = tmp;
109 cycles = tmp * duty_ns;
110 do_div(cycles, period_ns);
111 duty = cycles;
113 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
114 value &= ~BERLIN_PWM_PRESCALE_MASK;
115 value |= prescale;
116 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
118 berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);
119 berlin_pwm_writel(pwm, pwm_dev->hwpwm, period, BERLIN_PWM_TCNT);
121 return 0;
124 static int berlin_pwm_set_polarity(struct pwm_chip *chip,
125 struct pwm_device *pwm_dev,
126 enum pwm_polarity polarity)
128 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
129 u32 value;
131 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
133 if (polarity == PWM_POLARITY_NORMAL)
134 value &= ~BERLIN_PWM_INVERT_POLARITY;
135 else
136 value |= BERLIN_PWM_INVERT_POLARITY;
138 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
140 return 0;
143 static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm_dev)
145 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
146 u32 value;
148 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
149 value |= BERLIN_PWM_ENABLE;
150 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
152 return 0;
155 static void berlin_pwm_disable(struct pwm_chip *chip,
156 struct pwm_device *pwm_dev)
158 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
159 u32 value;
161 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
162 value &= ~BERLIN_PWM_ENABLE;
163 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
166 static const struct pwm_ops berlin_pwm_ops = {
167 .request = berlin_pwm_request,
168 .free = berlin_pwm_free,
169 .config = berlin_pwm_config,
170 .set_polarity = berlin_pwm_set_polarity,
171 .enable = berlin_pwm_enable,
172 .disable = berlin_pwm_disable,
173 .owner = THIS_MODULE,
176 static const struct of_device_id berlin_pwm_match[] = {
177 { .compatible = "marvell,berlin-pwm" },
178 { },
180 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
182 static int berlin_pwm_probe(struct platform_device *pdev)
184 struct berlin_pwm_chip *pwm;
185 struct resource *res;
186 int ret;
188 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
189 if (!pwm)
190 return -ENOMEM;
192 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 pwm->base = devm_ioremap_resource(&pdev->dev, res);
194 if (IS_ERR(pwm->base))
195 return PTR_ERR(pwm->base);
197 pwm->clk = devm_clk_get(&pdev->dev, NULL);
198 if (IS_ERR(pwm->clk))
199 return PTR_ERR(pwm->clk);
201 ret = clk_prepare_enable(pwm->clk);
202 if (ret)
203 return ret;
205 pwm->chip.dev = &pdev->dev;
206 pwm->chip.ops = &berlin_pwm_ops;
207 pwm->chip.base = -1;
208 pwm->chip.npwm = 4;
209 pwm->chip.can_sleep = true;
210 pwm->chip.of_xlate = of_pwm_xlate_with_flags;
211 pwm->chip.of_pwm_n_cells = 3;
213 ret = pwmchip_add(&pwm->chip);
214 if (ret < 0) {
215 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
216 clk_disable_unprepare(pwm->clk);
217 return ret;
220 platform_set_drvdata(pdev, pwm);
222 return 0;
225 static int berlin_pwm_remove(struct platform_device *pdev)
227 struct berlin_pwm_chip *pwm = platform_get_drvdata(pdev);
228 int ret;
230 ret = pwmchip_remove(&pwm->chip);
231 clk_disable_unprepare(pwm->clk);
233 return ret;
236 #ifdef CONFIG_PM_SLEEP
237 static int berlin_pwm_suspend(struct device *dev)
239 struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
240 unsigned int i;
242 for (i = 0; i < pwm->chip.npwm; i++) {
243 struct berlin_pwm_channel *channel;
245 channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
246 if (!channel)
247 continue;
249 channel->enable = berlin_pwm_readl(pwm, i, BERLIN_PWM_ENABLE);
250 channel->ctrl = berlin_pwm_readl(pwm, i, BERLIN_PWM_CONTROL);
251 channel->duty = berlin_pwm_readl(pwm, i, BERLIN_PWM_DUTY);
252 channel->tcnt = berlin_pwm_readl(pwm, i, BERLIN_PWM_TCNT);
255 clk_disable_unprepare(pwm->clk);
257 return 0;
260 static int berlin_pwm_resume(struct device *dev)
262 struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
263 unsigned int i;
264 int ret;
266 ret = clk_prepare_enable(pwm->clk);
267 if (ret)
268 return ret;
270 for (i = 0; i < pwm->chip.npwm; i++) {
271 struct berlin_pwm_channel *channel;
273 channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
274 if (!channel)
275 continue;
277 berlin_pwm_writel(pwm, i, channel->ctrl, BERLIN_PWM_CONTROL);
278 berlin_pwm_writel(pwm, i, channel->duty, BERLIN_PWM_DUTY);
279 berlin_pwm_writel(pwm, i, channel->tcnt, BERLIN_PWM_TCNT);
280 berlin_pwm_writel(pwm, i, channel->enable, BERLIN_PWM_ENABLE);
283 return 0;
285 #endif
287 static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
288 berlin_pwm_resume);
290 static struct platform_driver berlin_pwm_driver = {
291 .probe = berlin_pwm_probe,
292 .remove = berlin_pwm_remove,
293 .driver = {
294 .name = "berlin-pwm",
295 .of_match_table = berlin_pwm_match,
296 .pm = &berlin_pwm_pm_ops,
299 module_platform_driver(berlin_pwm_driver);
301 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
302 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
303 MODULE_LICENSE("GPL v2");