GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / pwm / pwm-berlin.c
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 #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
30
31 struct berlin_pwm_channel {
32         u32 enable;
33         u32 ctrl;
34         u32 duty;
35         u32 tcnt;
36 };
37
38 struct berlin_pwm_chip {
39         struct pwm_chip chip;
40         struct clk *clk;
41         void __iomem *base;
42 };
43
44 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
45 {
46         return container_of(chip, struct berlin_pwm_chip, chip);
47 }
48
49 static const u32 prescaler_table[] = {
50         1, 4, 8, 16, 64, 256, 1024, 4096
51 };
52
53 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip,
54                                    unsigned int channel, unsigned long offset)
55 {
56         return readl_relaxed(chip->base + channel * 0x10 + offset);
57 }
58
59 static inline void berlin_pwm_writel(struct berlin_pwm_chip *chip,
60                                      unsigned int channel, u32 value,
61                                      unsigned long offset)
62 {
63         writel_relaxed(value, chip->base + channel * 0x10 + offset);
64 }
65
66 static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
67 {
68         struct berlin_pwm_channel *channel;
69
70         channel = kzalloc(sizeof(*channel), GFP_KERNEL);
71         if (!channel)
72                 return -ENOMEM;
73
74         return pwm_set_chip_data(pwm, channel);
75 }
76
77 static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
78 {
79         struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
80
81         kfree(channel);
82 }
83
84 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
85                              int duty_ns, int period_ns)
86 {
87         struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
88         unsigned int prescale;
89         u32 value, duty, period;
90         u64 cycles, tmp;
91
92         cycles = clk_get_rate(pwm->clk);
93         cycles *= period_ns;
94         do_div(cycles, NSEC_PER_SEC);
95
96         for (prescale = 0; prescale < ARRAY_SIZE(prescaler_table); prescale++) {
97                 tmp = cycles;
98                 do_div(tmp, prescaler_table[prescale]);
99
100                 if (tmp <= BERLIN_PWM_MAX_TCNT)
101                         break;
102         }
103
104         if (tmp > BERLIN_PWM_MAX_TCNT)
105                 return -ERANGE;
106
107         period = tmp;
108         cycles = tmp * duty_ns;
109         do_div(cycles, period_ns);
110         duty = cycles;
111
112         value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
113         value &= ~BERLIN_PWM_PRESCALE_MASK;
114         value |= prescale;
115         berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
116
117         berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);
118         berlin_pwm_writel(pwm, pwm_dev->hwpwm, period, BERLIN_PWM_TCNT);
119
120         return 0;
121 }
122
123 static int berlin_pwm_set_polarity(struct pwm_chip *chip,
124                                    struct pwm_device *pwm_dev,
125                                    enum pwm_polarity polarity)
126 {
127         struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
128         u32 value;
129
130         value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
131
132         if (polarity == PWM_POLARITY_NORMAL)
133                 value &= ~BERLIN_PWM_INVERT_POLARITY;
134         else
135                 value |= BERLIN_PWM_INVERT_POLARITY;
136
137         berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
138
139         return 0;
140 }
141
142 static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm_dev)
143 {
144         struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
145         u32 value;
146
147         value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
148         value |= BERLIN_PWM_ENABLE;
149         berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
150
151         return 0;
152 }
153
154 static void berlin_pwm_disable(struct pwm_chip *chip,
155                                struct pwm_device *pwm_dev)
156 {
157         struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
158         u32 value;
159
160         value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
161         value &= ~BERLIN_PWM_ENABLE;
162         berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
163 }
164
165 static const struct pwm_ops berlin_pwm_ops = {
166         .request = berlin_pwm_request,
167         .free = berlin_pwm_free,
168         .config = berlin_pwm_config,
169         .set_polarity = berlin_pwm_set_polarity,
170         .enable = berlin_pwm_enable,
171         .disable = berlin_pwm_disable,
172         .owner = THIS_MODULE,
173 };
174
175 static const struct of_device_id berlin_pwm_match[] = {
176         { .compatible = "marvell,berlin-pwm" },
177         { },
178 };
179 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
180
181 static int berlin_pwm_probe(struct platform_device *pdev)
182 {
183         struct berlin_pwm_chip *pwm;
184         struct resource *res;
185         int ret;
186
187         pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
188         if (!pwm)
189                 return -ENOMEM;
190
191         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192         pwm->base = devm_ioremap_resource(&pdev->dev, res);
193         if (IS_ERR(pwm->base))
194                 return PTR_ERR(pwm->base);
195
196         pwm->clk = devm_clk_get(&pdev->dev, NULL);
197         if (IS_ERR(pwm->clk))
198                 return PTR_ERR(pwm->clk);
199
200         ret = clk_prepare_enable(pwm->clk);
201         if (ret)
202                 return ret;
203
204         pwm->chip.dev = &pdev->dev;
205         pwm->chip.ops = &berlin_pwm_ops;
206         pwm->chip.base = -1;
207         pwm->chip.npwm = 4;
208         pwm->chip.of_xlate = of_pwm_xlate_with_flags;
209         pwm->chip.of_pwm_n_cells = 3;
210
211         ret = pwmchip_add(&pwm->chip);
212         if (ret < 0) {
213                 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
214                 clk_disable_unprepare(pwm->clk);
215                 return ret;
216         }
217
218         platform_set_drvdata(pdev, pwm);
219
220         return 0;
221 }
222
223 static int berlin_pwm_remove(struct platform_device *pdev)
224 {
225         struct berlin_pwm_chip *pwm = platform_get_drvdata(pdev);
226         int ret;
227
228         ret = pwmchip_remove(&pwm->chip);
229         clk_disable_unprepare(pwm->clk);
230
231         return ret;
232 }
233
234 #ifdef CONFIG_PM_SLEEP
235 static int berlin_pwm_suspend(struct device *dev)
236 {
237         struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
238         unsigned int i;
239
240         for (i = 0; i < pwm->chip.npwm; i++) {
241                 struct berlin_pwm_channel *channel;
242
243                 channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
244                 if (!channel)
245                         continue;
246
247                 channel->enable = berlin_pwm_readl(pwm, i, BERLIN_PWM_ENABLE);
248                 channel->ctrl = berlin_pwm_readl(pwm, i, BERLIN_PWM_CONTROL);
249                 channel->duty = berlin_pwm_readl(pwm, i, BERLIN_PWM_DUTY);
250                 channel->tcnt = berlin_pwm_readl(pwm, i, BERLIN_PWM_TCNT);
251         }
252
253         clk_disable_unprepare(pwm->clk);
254
255         return 0;
256 }
257
258 static int berlin_pwm_resume(struct device *dev)
259 {
260         struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
261         unsigned int i;
262         int ret;
263
264         ret = clk_prepare_enable(pwm->clk);
265         if (ret)
266                 return ret;
267
268         for (i = 0; i < pwm->chip.npwm; i++) {
269                 struct berlin_pwm_channel *channel;
270
271                 channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
272                 if (!channel)
273                         continue;
274
275                 berlin_pwm_writel(pwm, i, channel->ctrl, BERLIN_PWM_CONTROL);
276                 berlin_pwm_writel(pwm, i, channel->duty, BERLIN_PWM_DUTY);
277                 berlin_pwm_writel(pwm, i, channel->tcnt, BERLIN_PWM_TCNT);
278                 berlin_pwm_writel(pwm, i, channel->enable, BERLIN_PWM_ENABLE);
279         }
280
281         return 0;
282 }
283 #endif
284
285 static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
286                          berlin_pwm_resume);
287
288 static struct platform_driver berlin_pwm_driver = {
289         .probe = berlin_pwm_probe,
290         .remove = berlin_pwm_remove,
291         .driver = {
292                 .name = "berlin-pwm",
293                 .of_match_table = berlin_pwm_match,
294                 .pm = &berlin_pwm_pm_ops,
295         },
296 };
297 module_platform_driver(berlin_pwm_driver);
298
299 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
300 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
301 MODULE_LICENSE("GPL v2");