blob: defc27d880f3bda57a18458030b4e2e9461ed18e [file] [log] [blame]
Neil Armstrong211ed632016-08-22 17:36:30 +02001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright (c) 2016 BayLibre, SAS.
8 * Author: Neil Armstrong <narmstrong@baylibre.com>
9 * Copyright (C) 2014 Amlogic, Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
24 *
25 * BSD LICENSE
26 *
27 * Copyright (c) 2016 BayLibre, SAS.
28 * Author: Neil Armstrong <narmstrong@baylibre.com>
29 * Copyright (C) 2014 Amlogic, Inc.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 *
35 * * Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * * Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in
39 * the documentation and/or other materials provided with the
40 * distribution.
41 * * Neither the name of Intel Corporation nor the names of its
42 * contributors may be used to endorse or promote products derived
43 * from this software without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
49 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57
58#include <linux/clk.h>
59#include <linux/clk-provider.h>
60#include <linux/err.h>
61#include <linux/io.h>
62#include <linux/kernel.h>
63#include <linux/module.h>
64#include <linux/of.h>
65#include <linux/of_device.h>
66#include <linux/platform_device.h>
67#include <linux/pwm.h>
68#include <linux/slab.h>
69#include <linux/spinlock.h>
70
71#define REG_PWM_A 0x0
72#define REG_PWM_B 0x4
73#define PWM_HIGH_SHIFT 16
74
75#define REG_MISC_AB 0x8
76#define MISC_B_CLK_EN BIT(23)
77#define MISC_A_CLK_EN BIT(15)
78#define MISC_CLK_DIV_MASK 0x7f
79#define MISC_B_CLK_DIV_SHIFT 16
80#define MISC_A_CLK_DIV_SHIFT 8
81#define MISC_B_CLK_SEL_SHIFT 6
82#define MISC_A_CLK_SEL_SHIFT 4
83#define MISC_CLK_SEL_WIDTH 2
84#define MISC_B_EN BIT(1)
85#define MISC_A_EN BIT(0)
86
87static const unsigned int mux_reg_shifts[] = {
88 MISC_A_CLK_SEL_SHIFT,
89 MISC_B_CLK_SEL_SHIFT
90};
91
92struct meson_pwm_channel {
93 unsigned int hi;
94 unsigned int lo;
95 u8 pre_div;
96
97 struct pwm_state state;
98
99 struct clk *clk_parent;
100 struct clk_mux mux;
101 struct clk *clk;
102};
103
104struct meson_pwm_data {
105 const char * const *parent_names;
Jerome Brunetd396b202017-06-08 14:24:15 +0200106 unsigned int num_parents;
Neil Armstrong211ed632016-08-22 17:36:30 +0200107};
108
109struct meson_pwm {
110 struct pwm_chip chip;
111 const struct meson_pwm_data *data;
112 void __iomem *base;
113 u8 inverter_mask;
114 spinlock_t lock;
115};
116
117static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
118{
119 return container_of(chip, struct meson_pwm, chip);
120}
121
122static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
123{
124 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
125 struct device *dev = chip->dev;
126 int err;
127
128 if (!channel)
129 return -ENODEV;
130
131 if (channel->clk_parent) {
132 err = clk_set_parent(channel->clk, channel->clk_parent);
133 if (err < 0) {
134 dev_err(dev, "failed to set parent %s for %s: %d\n",
135 __clk_get_name(channel->clk_parent),
136 __clk_get_name(channel->clk), err);
137 return err;
138 }
139 }
140
141 err = clk_prepare_enable(channel->clk);
142 if (err < 0) {
143 dev_err(dev, "failed to enable clock %s: %d\n",
144 __clk_get_name(channel->clk), err);
145 return err;
146 }
147
148 chip->ops->get_state(chip, pwm, &channel->state);
149
150 return 0;
151}
152
153static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
154{
155 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
156
157 if (channel)
158 clk_disable_unprepare(channel->clk);
159}
160
161static int meson_pwm_calc(struct meson_pwm *meson,
162 struct meson_pwm_channel *channel, unsigned int id,
163 unsigned int duty, unsigned int period)
164{
165 unsigned int pre_div, cnt, duty_cnt;
166 unsigned long fin_freq = -1, fin_ns;
167
168 if (~(meson->inverter_mask >> id) & 0x1)
169 duty = period - duty;
170
171 if (period == channel->state.period &&
172 duty == channel->state.duty_cycle)
173 return 0;
174
175 fin_freq = clk_get_rate(channel->clk);
176 if (fin_freq == 0) {
177 dev_err(meson->chip.dev, "invalid source clock frequency\n");
178 return -EINVAL;
179 }
180
181 dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
182 fin_ns = NSEC_PER_SEC / fin_freq;
183
184 /* Calc pre_div with the period */
185 for (pre_div = 0; pre_div < MISC_CLK_DIV_MASK; pre_div++) {
186 cnt = DIV_ROUND_CLOSEST(period, fin_ns * (pre_div + 1));
187 dev_dbg(meson->chip.dev, "fin_ns=%lu pre_div=%u cnt=%u\n",
188 fin_ns, pre_div, cnt);
189 if (cnt <= 0xffff)
190 break;
191 }
192
193 if (pre_div == MISC_CLK_DIV_MASK) {
194 dev_err(meson->chip.dev, "unable to get period pre_div\n");
195 return -EINVAL;
196 }
197
198 dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period,
199 pre_div, cnt);
200
201 if (duty == period) {
202 channel->pre_div = pre_div;
203 channel->hi = cnt;
204 channel->lo = 0;
205 } else if (duty == 0) {
206 channel->pre_div = pre_div;
207 channel->hi = 0;
208 channel->lo = cnt;
209 } else {
210 /* Then check is we can have the duty with the same pre_div */
211 duty_cnt = DIV_ROUND_CLOSEST(duty, fin_ns * (pre_div + 1));
212 if (duty_cnt > 0xffff) {
213 dev_err(meson->chip.dev, "unable to get duty cycle\n");
214 return -EINVAL;
215 }
216
217 dev_dbg(meson->chip.dev, "duty=%u pre_div=%u duty_cnt=%u\n",
218 duty, pre_div, duty_cnt);
219
220 channel->pre_div = pre_div;
221 channel->hi = duty_cnt;
222 channel->lo = cnt - duty_cnt;
223 }
224
225 return 0;
226}
227
228static void meson_pwm_enable(struct meson_pwm *meson,
229 struct meson_pwm_channel *channel,
230 unsigned int id)
231{
232 u32 value, clk_shift, clk_enable, enable;
233 unsigned int offset;
234
235 switch (id) {
236 case 0:
237 clk_shift = MISC_A_CLK_DIV_SHIFT;
238 clk_enable = MISC_A_CLK_EN;
239 enable = MISC_A_EN;
240 offset = REG_PWM_A;
241 break;
242
243 case 1:
244 clk_shift = MISC_B_CLK_DIV_SHIFT;
245 clk_enable = MISC_B_CLK_EN;
246 enable = MISC_B_EN;
247 offset = REG_PWM_B;
248 break;
Arnd Bergmann2fbc4872016-09-06 14:50:47 +0200249
250 default:
251 return;
Neil Armstrong211ed632016-08-22 17:36:30 +0200252 }
253
254 value = readl(meson->base + REG_MISC_AB);
255 value &= ~(MISC_CLK_DIV_MASK << clk_shift);
256 value |= channel->pre_div << clk_shift;
257 value |= clk_enable;
258 writel(value, meson->base + REG_MISC_AB);
259
260 value = (channel->hi << PWM_HIGH_SHIFT) | channel->lo;
261 writel(value, meson->base + offset);
262
263 value = readl(meson->base + REG_MISC_AB);
264 value |= enable;
265 writel(value, meson->base + REG_MISC_AB);
266}
267
268static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
269{
270 u32 value, enable;
271
272 switch (id) {
273 case 0:
274 enable = MISC_A_EN;
275 break;
276
277 case 1:
278 enable = MISC_B_EN;
279 break;
Arnd Bergmann2fbc4872016-09-06 14:50:47 +0200280
281 default:
282 return;
Neil Armstrong211ed632016-08-22 17:36:30 +0200283 }
284
285 value = readl(meson->base + REG_MISC_AB);
286 value &= ~enable;
287 writel(value, meson->base + REG_MISC_AB);
288}
289
290static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
291 struct pwm_state *state)
292{
293 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
294 struct meson_pwm *meson = to_meson_pwm(chip);
295 unsigned long flags;
296 int err = 0;
297
298 if (!state)
299 return -EINVAL;
300
301 spin_lock_irqsave(&meson->lock, flags);
302
303 if (!state->enabled) {
304 meson_pwm_disable(meson, pwm->hwpwm);
305 channel->state.enabled = false;
306
307 goto unlock;
308 }
309
310 if (state->period != channel->state.period ||
311 state->duty_cycle != channel->state.duty_cycle ||
312 state->polarity != channel->state.polarity) {
313 if (channel->state.enabled) {
314 meson_pwm_disable(meson, pwm->hwpwm);
315 channel->state.enabled = false;
316 }
317
318 if (state->polarity != channel->state.polarity) {
319 if (state->polarity == PWM_POLARITY_NORMAL)
320 meson->inverter_mask |= BIT(pwm->hwpwm);
321 else
322 meson->inverter_mask &= ~BIT(pwm->hwpwm);
323 }
324
325 err = meson_pwm_calc(meson, channel, pwm->hwpwm,
326 state->duty_cycle, state->period);
327 if (err < 0)
328 goto unlock;
329
330 channel->state.polarity = state->polarity;
331 channel->state.period = state->period;
332 channel->state.duty_cycle = state->duty_cycle;
333 }
334
335 if (state->enabled && !channel->state.enabled) {
336 meson_pwm_enable(meson, channel, pwm->hwpwm);
337 channel->state.enabled = true;
338 }
339
340unlock:
341 spin_unlock_irqrestore(&meson->lock, flags);
342 return err;
343}
344
345static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
346 struct pwm_state *state)
347{
348 struct meson_pwm *meson = to_meson_pwm(chip);
349 u32 value, mask;
350
351 if (!state)
352 return;
353
354 switch (pwm->hwpwm) {
355 case 0:
356 mask = MISC_A_EN;
357 break;
358
359 case 1:
360 mask = MISC_B_EN;
361 break;
Arnd Bergmann2fbc4872016-09-06 14:50:47 +0200362
363 default:
364 return;
Neil Armstrong211ed632016-08-22 17:36:30 +0200365 }
366
367 value = readl(meson->base + REG_MISC_AB);
368 state->enabled = (value & mask) != 0;
369}
370
371static const struct pwm_ops meson_pwm_ops = {
372 .request = meson_pwm_request,
373 .free = meson_pwm_free,
374 .apply = meson_pwm_apply,
375 .get_state = meson_pwm_get_state,
376 .owner = THIS_MODULE,
377};
378
379static const char * const pwm_meson8b_parent_names[] = {
380 "xtal", "vid_pll", "fclk_div4", "fclk_div3"
381};
382
383static const struct meson_pwm_data pwm_meson8b_data = {
384 .parent_names = pwm_meson8b_parent_names,
Jerome Brunetd396b202017-06-08 14:24:15 +0200385 .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names),
Neil Armstrong211ed632016-08-22 17:36:30 +0200386};
387
388static const char * const pwm_gxbb_parent_names[] = {
389 "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
390};
391
392static const struct meson_pwm_data pwm_gxbb_data = {
393 .parent_names = pwm_gxbb_parent_names,
Jerome Brunetd396b202017-06-08 14:24:15 +0200394 .num_parents = ARRAY_SIZE(pwm_gxbb_parent_names),
395};
396
397/*
398 * Only the 2 first inputs of the GXBB AO PWMs are valid
399 * The last 2 are grounded
400 */
401static const char * const pwm_gxbb_ao_parent_names[] = {
402 "xtal", "clk81"
403};
404
405static const struct meson_pwm_data pwm_gxbb_ao_data = {
406 .parent_names = pwm_gxbb_ao_parent_names,
407 .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names),
Neil Armstrong211ed632016-08-22 17:36:30 +0200408};
409
410static const struct of_device_id meson_pwm_matches[] = {
Jerome Brunetd396b202017-06-08 14:24:15 +0200411 {
412 .compatible = "amlogic,meson8b-pwm",
413 .data = &pwm_meson8b_data
414 },
415 {
416 .compatible = "amlogic,meson-gxbb-pwm",
417 .data = &pwm_gxbb_data
418 },
419 {
420 .compatible = "amlogic,meson-gxbb-ao-pwm",
421 .data = &pwm_gxbb_ao_data
422 },
Neil Armstrong211ed632016-08-22 17:36:30 +0200423 {},
424};
425MODULE_DEVICE_TABLE(of, meson_pwm_matches);
426
427static int meson_pwm_init_channels(struct meson_pwm *meson,
428 struct meson_pwm_channel *channels)
429{
430 struct device *dev = meson->chip.dev;
431 struct device_node *np = dev->of_node;
432 struct clk_init_data init;
433 unsigned int i;
434 char name[255];
435 int err;
436
437 for (i = 0; i < meson->chip.npwm; i++) {
438 struct meson_pwm_channel *channel = &channels[i];
439
440 snprintf(name, sizeof(name), "%s#mux%u", np->full_name, i);
441
442 init.name = name;
443 init.ops = &clk_mux_ops;
444 init.flags = CLK_IS_BASIC;
445 init.parent_names = meson->data->parent_names;
Jerome Brunetd396b202017-06-08 14:24:15 +0200446 init.num_parents = meson->data->num_parents;
Neil Armstrong211ed632016-08-22 17:36:30 +0200447
448 channel->mux.reg = meson->base + REG_MISC_AB;
449 channel->mux.shift = mux_reg_shifts[i];
450 channel->mux.mask = BIT(MISC_CLK_SEL_WIDTH) - 1;
451 channel->mux.flags = 0;
452 channel->mux.lock = &meson->lock;
453 channel->mux.table = NULL;
454 channel->mux.hw.init = &init;
455
456 channel->clk = devm_clk_register(dev, &channel->mux.hw);
457 if (IS_ERR(channel->clk)) {
458 err = PTR_ERR(channel->clk);
459 dev_err(dev, "failed to register %s: %d\n", name, err);
460 return err;
461 }
462
463 snprintf(name, sizeof(name), "clkin%u", i);
464
465 channel->clk_parent = devm_clk_get(dev, name);
466 if (IS_ERR(channel->clk_parent)) {
467 err = PTR_ERR(channel->clk_parent);
468 if (err == -EPROBE_DEFER)
469 return err;
470
471 channel->clk_parent = NULL;
472 }
473 }
474
475 return 0;
476}
477
478static void meson_pwm_add_channels(struct meson_pwm *meson,
479 struct meson_pwm_channel *channels)
480{
481 unsigned int i;
482
483 for (i = 0; i < meson->chip.npwm; i++)
484 pwm_set_chip_data(&meson->chip.pwms[i], &channels[i]);
485}
486
487static int meson_pwm_probe(struct platform_device *pdev)
488{
489 struct meson_pwm_channel *channels;
490 struct meson_pwm *meson;
491 struct resource *regs;
492 int err;
493
494 meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL);
495 if (!meson)
496 return -ENOMEM;
497
498 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
499 meson->base = devm_ioremap_resource(&pdev->dev, regs);
500 if (IS_ERR(meson->base))
501 return PTR_ERR(meson->base);
502
Axel Linc6999952016-09-10 09:55:49 +0800503 spin_lock_init(&meson->lock);
Neil Armstrong211ed632016-08-22 17:36:30 +0200504 meson->chip.dev = &pdev->dev;
505 meson->chip.ops = &meson_pwm_ops;
506 meson->chip.base = -1;
507 meson->chip.npwm = 2;
508 meson->chip.of_xlate = of_pwm_xlate_with_flags;
509 meson->chip.of_pwm_n_cells = 3;
510
511 meson->data = of_device_get_match_data(&pdev->dev);
512 meson->inverter_mask = BIT(meson->chip.npwm) - 1;
513
514 channels = devm_kcalloc(&pdev->dev, meson->chip.npwm, sizeof(*meson),
515 GFP_KERNEL);
516 if (!channels)
517 return -ENOMEM;
518
519 err = meson_pwm_init_channels(meson, channels);
520 if (err < 0)
521 return err;
522
523 err = pwmchip_add(&meson->chip);
524 if (err < 0) {
525 dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err);
526 return err;
527 }
528
529 meson_pwm_add_channels(meson, channels);
530
531 platform_set_drvdata(pdev, meson);
532
533 return 0;
534}
535
536static int meson_pwm_remove(struct platform_device *pdev)
537{
538 struct meson_pwm *meson = platform_get_drvdata(pdev);
539
540 return pwmchip_remove(&meson->chip);
541}
542
543static struct platform_driver meson_pwm_driver = {
544 .driver = {
545 .name = "meson-pwm",
546 .of_match_table = meson_pwm_matches,
547 },
548 .probe = meson_pwm_probe,
549 .remove = meson_pwm_remove,
550};
551module_platform_driver(meson_pwm_driver);
552
Neil Armstrong211ed632016-08-22 17:36:30 +0200553MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
554MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
555MODULE_LICENSE("Dual BSD/GPL");