blob: 60959720dac8ad1d9f5ed3f98fac36036a67831c [file] [log] [blame]
Neil Armstrong2d481b22020-10-01 10:04:56 +02001// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2/*
3 * Copyright (C) 2020 BayLibre, SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Copyright (C) 2014 Amlogic, Inc.
6 *
7 * This PWM is only a set of Gates, Dividers and Counters:
8 * PWM output is achieved by calculating a clock that permits calculating
9 * two periods (low and high). The counter then has to be set to switch after
10 * N cycles for the first half period.
11 * The hardware has no "polarity" setting. This driver reverses the period
12 * cycles (the low length is inverted with the high length) for
13 * PWM_POLARITY_INVERSED.
14 * Setting the polarity will disable and re-enable the PWM output.
15 * Disabling the PWM stops the output immediately (without waiting for the
16 * current period to complete first).
17 */
18
19#include <common.h>
20#include <clk.h>
21#include <div64.h>
22#include <dm.h>
23#include <pwm.h>
24#include <regmap.h>
25#include <linux/io.h>
26#include <linux/math64.h>
27#include <linux/bitfield.h>
28#include <linux/clk-provider.h>
Igor Prusov13248d62023-11-09 20:10:04 +030029#include <linux/time.h>
Neil Armstrong2d481b22020-10-01 10:04:56 +020030
31#define REG_PWM_A 0x0
32#define REG_PWM_B 0x4
33#define PWM_LOW_MASK GENMASK(15, 0)
34#define PWM_HIGH_MASK GENMASK(31, 16)
35
36#define REG_MISC_AB 0x8
37#define MISC_B_CLK_EN BIT(23)
38#define MISC_A_CLK_EN BIT(15)
39#define MISC_CLK_DIV_MASK 0x7f
40#define MISC_B_CLK_DIV_SHIFT 16
41#define MISC_A_CLK_DIV_SHIFT 8
42#define MISC_B_CLK_SEL_SHIFT 6
43#define MISC_A_CLK_SEL_SHIFT 4
44#define MISC_CLK_SEL_MASK 0x3
45#define MISC_B_EN BIT(1)
46#define MISC_A_EN BIT(0)
47
48#define MESON_NUM_PWMS 2
49
50static struct meson_pwm_channel_data {
51 u8 reg_offset;
52 u8 clk_sel_shift;
53 u8 clk_div_shift;
54 u32 clk_en_mask;
55 u32 pwm_en_mask;
56} meson_pwm_per_channel_data[MESON_NUM_PWMS] = {
57 {
58 .reg_offset = REG_PWM_A,
59 .clk_sel_shift = MISC_A_CLK_SEL_SHIFT,
60 .clk_div_shift = MISC_A_CLK_DIV_SHIFT,
61 .clk_en_mask = MISC_A_CLK_EN,
62 .pwm_en_mask = MISC_A_EN,
63 },
64 {
65 .reg_offset = REG_PWM_B,
66 .clk_sel_shift = MISC_B_CLK_SEL_SHIFT,
67 .clk_div_shift = MISC_B_CLK_DIV_SHIFT,
68 .clk_en_mask = MISC_B_CLK_EN,
69 .pwm_en_mask = MISC_B_EN,
70 }
71};
72
73struct meson_pwm_channel {
74 unsigned int hi;
75 unsigned int lo;
76 u8 pre_div;
77 uint period_ns;
78 uint duty_ns;
79 bool configured;
80 bool enabled;
81 bool polarity;
82 struct clk clk;
83};
84
85struct meson_pwm_data {
86 const long *parent_ids;
87 unsigned int num_parents;
88};
89
90struct meson_pwm {
91 const struct meson_pwm_data *data;
92 struct meson_pwm_channel channels[MESON_NUM_PWMS];
93 void __iomem *base;
94};
95
96static int meson_pwm_set_enable(struct udevice *dev, uint channel, bool enable);
97
98static int meson_pwm_set_config(struct udevice *dev, uint channeln,
99 uint period_ns, uint duty_ns)
100{
101 struct meson_pwm *priv = dev_get_priv(dev);
102 struct meson_pwm_channel *channel;
103 struct meson_pwm_channel_data *channel_data;
104 unsigned int duty, period, pre_div, cnt, duty_cnt;
105 unsigned long fin_freq;
106
107 if (channeln >= MESON_NUM_PWMS)
108 return -ENODEV;
109
110 channel = &priv->channels[channeln];
111 channel_data = &meson_pwm_per_channel_data[channeln];
112
113 period = period_ns;
114 if (channel->polarity)
115 duty = period_ns - duty_ns;
116 else
117 duty = duty_ns;
118
119 debug("%s%d: polarity %s duty %d period %d\n", __func__, channeln,
120 channel->polarity ? "true" : "false", duty, period);
121
122 fin_freq = clk_get_rate(&channel->clk);
123 if (fin_freq == 0) {
124 printf("%s%d: invalid source clock frequency\n", __func__, channeln);
125 return -EINVAL;
126 }
127
128 debug("%s%d: fin_freq: %lu Hz\n", __func__, channeln, fin_freq);
129
130 pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL);
131 if (pre_div > MISC_CLK_DIV_MASK) {
132 printf("%s%d: unable to get period pre_div\n", __func__, channeln);
133 return -EINVAL;
134 }
135
136 cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1));
137 if (cnt > 0xffff) {
138 printf("%s%d: unable to get period cnt\n", __func__, channeln);
139 return -EINVAL;
140 }
141
142 debug("%s%d: period=%u pre_div=%u cnt=%u\n", __func__, channeln, period, pre_div, cnt);
143
144 if (duty == period) {
145 channel->pre_div = pre_div;
146 channel->hi = cnt;
147 channel->lo = 0;
148 } else if (duty == 0) {
149 channel->pre_div = pre_div;
150 channel->hi = 0;
151 channel->lo = cnt;
152 } else {
153 /* Then check is we can have the duty with the same pre_div */
154 duty_cnt = div64_u64(fin_freq * (u64)duty, NSEC_PER_SEC * (pre_div + 1));
155 if (duty_cnt > 0xffff) {
156 printf("%s%d: unable to get duty cycle\n", __func__, channeln);
157 return -EINVAL;
158 }
159
160 debug("%s%d: duty=%u pre_div=%u duty_cnt=%u\n",
161 __func__, channeln, duty, pre_div, duty_cnt);
162
163 channel->pre_div = pre_div;
164 channel->hi = duty_cnt;
165 channel->lo = cnt - duty_cnt;
166 }
167
168 channel->period_ns = period_ns;
169 channel->duty_ns = duty_ns;
170 channel->configured = true;
171
172 if (channel->enabled) {
173 meson_pwm_set_enable(dev, channeln, false);
174 meson_pwm_set_enable(dev, channeln, true);
175 }
176
177 return 0;
178}
179
180static int meson_pwm_set_enable(struct udevice *dev, uint channeln, bool enable)
181{
182 struct meson_pwm *priv = dev_get_priv(dev);
183 struct meson_pwm_channel *channel;
184 struct meson_pwm_channel_data *channel_data;
185 u32 value;
186
187 if (channeln >= MESON_NUM_PWMS)
188 return -ENODEV;
189
190 channel = &priv->channels[channeln];
191 channel_data = &meson_pwm_per_channel_data[channeln];
192
193 if (!channel->configured)
194 return -EINVAL;
195
196 if (enable) {
197 if (channel->enabled)
198 return 0;
199
200 value = readl(priv->base + REG_MISC_AB);
201 value &= ~(MISC_CLK_DIV_MASK << channel_data->clk_div_shift);
202 value |= channel->pre_div << channel_data->clk_div_shift;
203 value |= channel_data->clk_en_mask;
204 writel(value, priv->base + REG_MISC_AB);
205
206 value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
207 FIELD_PREP(PWM_LOW_MASK, channel->lo);
208 writel(value, priv->base + channel_data->reg_offset);
209
210 value = readl(priv->base + REG_MISC_AB);
211 value |= channel_data->pwm_en_mask;
212 writel(value, priv->base + REG_MISC_AB);
213
214 debug("%s%d: enabled\n", __func__, channeln);
215 channel->enabled = true;
216 } else {
217 if (!channel->enabled)
218 return 0;
219
220 value = readl(priv->base + REG_MISC_AB);
221 value &= channel_data->pwm_en_mask;
222 writel(value, priv->base + REG_MISC_AB);
223
224 debug("%s%d: disabled\n", __func__, channeln);
225 channel->enabled = false;
226 }
227
228 return 0;
229}
230
231static int meson_pwm_set_invert(struct udevice *dev, uint channeln, bool polarity)
232{
233 struct meson_pwm *priv = dev_get_priv(dev);
234 struct meson_pwm_channel *channel;
235
236 if (channeln >= MESON_NUM_PWMS)
237 return -ENODEV;
238
239 debug("%s%d: set invert %s\n", __func__, channeln, polarity ? "true" : "false");
240
241 channel = &priv->channels[channeln];
242
243 channel->polarity = polarity;
244
245 if (!channel->configured)
246 return 0;
247
248 return meson_pwm_set_config(dev, channeln, channel->period_ns, channel->duty_ns);
249}
250
Simon Glassd1998a92020-12-03 16:55:21 -0700251static int meson_pwm_of_to_plat(struct udevice *dev)
Neil Armstrong2d481b22020-10-01 10:04:56 +0200252{
253 struct meson_pwm *priv = dev_get_priv(dev);
254
255 priv->base = dev_read_addr_ptr(dev);
256
257 return 0;
258}
259
260static int meson_pwm_probe(struct udevice *dev)
261{
262 struct meson_pwm *priv = dev_get_priv(dev);
263 struct meson_pwm_data *data;
264 unsigned int i, p;
265 char name[255];
266 int err;
267 u32 reg;
268
269 data = (struct meson_pwm_data *)dev_get_driver_data(dev);
270 if (!data)
271 return -EINVAL;
272
273 for (i = 0; i < MESON_NUM_PWMS; i++) {
274 struct meson_pwm_channel *channel = &priv->channels[i];
275 struct meson_pwm_channel_data *channel_data = &meson_pwm_per_channel_data[i];
276
277 snprintf(name, sizeof(name), "clkin%u", i);
278
279 err = clk_get_by_name(dev, name, &channel->clk);
280 /* If clock is not specified, use the already set clock */
281 if (err == -ENODATA) {
282 struct udevice *cdev;
283 struct uclass *uc;
284
285 /* Get parent from mux */
286 p = (readl(priv->base + REG_MISC_AB) >> channel_data->clk_sel_shift) &
287 MISC_CLK_SEL_MASK;
288
289 if (p >= data->num_parents) {
290 printf("%s%d: hw parent is invalid\n", __func__, i);
291 return -EINVAL;
292 }
293
294 if (data->parent_ids[p] == -1) {
295 /* Search for xtal clk */
296 const char *str;
297
298 err = uclass_get(UCLASS_CLK, &uc);
299 if (err)
300 return err;
301
302 uclass_foreach_dev(cdev, uc) {
303 if (strcmp(cdev->driver->name, "fixed_rate_clock"))
304 continue;
305
Simon Glassf10643c2020-12-19 10:40:14 -0700306 str = ofnode_read_string(dev_ofnode(cdev),
307 "clock-output-names");
Neil Armstrong2d481b22020-10-01 10:04:56 +0200308 if (!str)
309 continue;
310
311 if (!strcmp(str, "xtal")) {
312 err = uclass_get_device_by_ofnode(UCLASS_CLK,
Simon Glassf10643c2020-12-19 10:40:14 -0700313 dev_ofnode(cdev),
Neil Armstrong2d481b22020-10-01 10:04:56 +0200314 &cdev);
315 if (err) {
316 printf("%s%d: Failed to get xtal clk\n", __func__, i);
317 return err;
318 }
319
320 break;
321 }
322 }
323
324 if (!cdev) {
325 printf("%s%d: Failed to find xtal clk device\n", __func__, i);
326 return -EINVAL;
327 }
328
329 channel->clk.dev = cdev;
330 channel->clk.id = 0;
331 channel->clk.data = 0;
332 } else {
333 /* Look for parent clock */
334 err = uclass_get(UCLASS_CLK, &uc);
335 if (err)
336 return err;
337
338 uclass_foreach_dev(cdev, uc) {
339 if (strstr(cdev->driver->name, "meson_clk"))
340 break;
341 }
342
343 if (!cdev) {
344 printf("%s%d: Failed to find clk device\n", __func__, i);
345 return -EINVAL;
346 }
347
Simon Glassf10643c2020-12-19 10:40:14 -0700348 err = uclass_get_device_by_ofnode(UCLASS_CLK,
349 dev_ofnode(cdev),
350 &cdev);
Neil Armstrong2d481b22020-10-01 10:04:56 +0200351 if (err) {
352 printf("%s%d: Failed to get clk controller\n", __func__, i);
353 return err;
354 }
355
356 channel->clk.dev = cdev;
357 channel->clk.id = data->parent_ids[p];
358 channel->clk.data = 0;
359 }
360
361 /* We have our source clock, do not alter HW clock mux */
362 continue;
363 } else
364 return err;
365
366 /* Get id in list */
367 for (p = 0 ; p < data->num_parents ; ++p) {
368 if (!strcmp(channel->clk.dev->driver->name, "fixed_rate_clock")) {
369 if (data->parent_ids[p] == -1)
370 break;
371 } else {
372 if (data->parent_ids[p] == channel->clk.id)
373 break;
374 }
375 }
376
377 /* Invalid clock ID */
378 if (p == data->num_parents) {
379 printf("%s%d: source clock is invalid\n", __func__, i);
380 return -EINVAL;
381 }
382
383 /* switch parent in mux */
384 reg = readl(priv->base + REG_MISC_AB);
385
386 debug("%s%d: switching parent %d to %d\n", __func__, i,
387 (reg >> channel_data->clk_sel_shift) & MISC_CLK_SEL_MASK, p);
388
389 reg &= MISC_CLK_SEL_MASK << channel_data->clk_sel_shift;
390 reg |= (p & MISC_CLK_SEL_MASK) << channel_data->clk_sel_shift;
391 writel(reg, priv->base + REG_MISC_AB);
392 }
393
394 return 0;
395}
396
397static const struct pwm_ops meson_pwm_ops = {
398 .set_config = meson_pwm_set_config,
399 .set_enable = meson_pwm_set_enable,
400 .set_invert = meson_pwm_set_invert,
401};
402
Wolfgang Denk0cf207e2021-09-27 17:42:39 +0200403#define XTAL -1
Neil Armstrong2d481b22020-10-01 10:04:56 +0200404
405/* Local clock ids aliases to avoid define conflicts */
406#define GXBB_CLKID_HDMI_PLL 2
407#define GXBB_CLKID_FCLK_DIV3 5
408#define GXBB_CLKID_FCLK_DIV4 6
409#define GXBB_CLKID_CLK81 12
410
411static const long pwm_gxbb_parent_ids[] = {
412 XTAL, GXBB_CLKID_HDMI_PLL, GXBB_CLKID_FCLK_DIV4, GXBB_CLKID_FCLK_DIV3
413};
414
415static const struct meson_pwm_data pwm_gxbb_data = {
416 .parent_ids = pwm_gxbb_parent_ids,
417 .num_parents = ARRAY_SIZE(pwm_gxbb_parent_ids),
418};
419
420/*
421 * Only the 2 first inputs of the GXBB AO PWMs are valid
422 * The last 2 are grounded
423 */
424static const long pwm_gxbb_ao_parent_ids[] = {
425 XTAL, GXBB_CLKID_CLK81
426};
427
428static const struct meson_pwm_data pwm_gxbb_ao_data = {
429 .parent_ids = pwm_gxbb_ao_parent_ids,
430 .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_ids),
431};
432
433/* Local clock ids aliases to avoid define conflicts */
434#define AXG_CLKID_FCLK_DIV3 3
435#define AXG_CLKID_FCLK_DIV4 4
436#define AXG_CLKID_FCLK_DIV5 5
437#define AXG_CLKID_CLK81 10
438
439static const long pwm_axg_ee_parent_ids[] = {
440 XTAL, AXG_CLKID_FCLK_DIV5, AXG_CLKID_FCLK_DIV4, AXG_CLKID_FCLK_DIV3
441};
442
443static const struct meson_pwm_data pwm_axg_ee_data = {
444 .parent_ids = pwm_axg_ee_parent_ids,
445 .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_ids),
446};
447
448static const long pwm_axg_ao_parent_ids[] = {
449 AXG_CLKID_CLK81, XTAL, AXG_CLKID_FCLK_DIV4, AXG_CLKID_FCLK_DIV5
450};
451
452static const struct meson_pwm_data pwm_axg_ao_data = {
453 .parent_ids = pwm_axg_ao_parent_ids,
454 .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_ids),
455};
456
457/* Local clock ids aliases to avoid define conflicts */
458#define G12A_CLKID_FCLK_DIV3 3
459#define G12A_CLKID_FCLK_DIV4 4
460#define G12A_CLKID_FCLK_DIV5 5
461#define G12A_CLKID_CLK81 10
462#define G12A_CLKID_HDMI_PLL 128
463
464static const long pwm_g12a_ao_ab_parent_ids[] = {
465 XTAL, G12A_CLKID_CLK81, G12A_CLKID_FCLK_DIV4, G12A_CLKID_FCLK_DIV5
466};
467
468static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
469 .parent_ids = pwm_g12a_ao_ab_parent_ids,
470 .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_ids),
471};
472
473static const long pwm_g12a_ao_cd_parent_ids[] = {
474 XTAL, G12A_CLKID_CLK81,
475};
476
477static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
478 .parent_ids = pwm_g12a_ao_cd_parent_ids,
479 .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_ids),
480};
481
482static const long pwm_g12a_ee_parent_ids[] = {
483 XTAL, G12A_CLKID_HDMI_PLL, G12A_CLKID_FCLK_DIV4, G12A_CLKID_FCLK_DIV3
484};
485
486static const struct meson_pwm_data pwm_g12a_ee_data = {
487 .parent_ids = pwm_g12a_ee_parent_ids,
488 .num_parents = ARRAY_SIZE(pwm_g12a_ee_parent_ids),
489};
490
491static const struct udevice_id meson_pwm_ids[] = {
492 {
493 .compatible = "amlogic,meson-gxbb-pwm",
494 .data = (ulong)&pwm_gxbb_data
495 },
496 {
497 .compatible = "amlogic,meson-gxbb-ao-pwm",
498 .data = (ulong)&pwm_gxbb_ao_data
499 },
500 {
501 .compatible = "amlogic,meson-axg-ee-pwm",
502 .data = (ulong)&pwm_axg_ee_data
503 },
504 {
505 .compatible = "amlogic,meson-axg-ao-pwm",
506 .data = (ulong)&pwm_axg_ao_data
507 },
508 {
509 .compatible = "amlogic,meson-g12a-ee-pwm",
510 .data = (ulong)&pwm_g12a_ee_data
511 },
512 {
513 .compatible = "amlogic,meson-g12a-ao-pwm-ab",
514 .data = (ulong)&pwm_g12a_ao_ab_data
515 },
516 {
517 .compatible = "amlogic,meson-g12a-ao-pwm-cd",
518 .data = (ulong)&pwm_g12a_ao_cd_data
519 },
520};
521
522U_BOOT_DRIVER(meson_pwm) = {
523 .name = "meson_pwm",
524 .id = UCLASS_PWM,
525 .of_match = meson_pwm_ids,
526 .ops = &meson_pwm_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700527 .of_to_plat = meson_pwm_of_to_plat,
Neil Armstrong2d481b22020-10-01 10:04:56 +0200528 .probe = meson_pwm_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700529 .priv_auto = sizeof(struct meson_pwm),
Neil Armstrong2d481b22020-10-01 10:04:56 +0200530};