blob: bb1bec05ec34a3c8ea23c029592c9658179c09ad [file] [log] [blame]
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2017-2018 Vasily Khoruzhick <anarsoul@gmail.com>
4 */
5
6#include <common.h>
7#include <div64.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070010#include <pwm.h>
11#include <regmap.h>
12#include <syscon.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070014#include <asm/io.h>
15#include <asm/arch/pwm.h>
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070016#include <power/regulator.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20#define OSC_24MHZ 24000000
21
22struct sunxi_pwm_priv {
23 struct sunxi_pwm *regs;
24 bool invert;
25 u32 prescaler;
26};
27
28static const u32 prescaler_table[] = {
29 120, /* 0000 */
30 180, /* 0001 */
31 240, /* 0010 */
32 360, /* 0011 */
33 480, /* 0100 */
34 0, /* 0101 */
35 0, /* 0110 */
36 0, /* 0111 */
37 12000, /* 1000 */
38 24000, /* 1001 */
39 36000, /* 1010 */
40 48000, /* 1011 */
41 72000, /* 1100 */
42 0, /* 1101 */
43 0, /* 1110 */
44 1, /* 1111 */
45};
46
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070047static int sunxi_pwm_set_invert(struct udevice *dev, uint channel,
48 bool polarity)
49{
50 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
51
52 debug("%s: polarity=%u\n", __func__, polarity);
53 priv->invert = polarity;
54
55 return 0;
56}
57
58static int sunxi_pwm_set_config(struct udevice *dev, uint channel,
59 uint period_ns, uint duty_ns)
60{
61 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
62 struct sunxi_pwm *regs = priv->regs;
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070063 int best_prescaler = 0;
64 u32 v, best_period = 0, duty;
65 u64 best_scaled_freq = 0;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070066 const u32 nsecs_per_sec = 1000000000U;
67
68 debug("%s: period_ns=%u, duty_ns=%u\n", __func__, period_ns, duty_ns);
69
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070070 for (int prescaler = 0; prescaler <= SUNXI_PWM_CTRL_PRESCALE0_MASK;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070071 prescaler++) {
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070072 u32 period = 0;
73 u64 scaled_freq = 0;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070074 if (!prescaler_table[prescaler])
75 continue;
76 scaled_freq = lldiv(OSC_24MHZ, prescaler_table[prescaler]);
77 period = lldiv(scaled_freq * period_ns, nsecs_per_sec);
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070078 if ((period - 1 <= SUNXI_PWM_CH0_PERIOD_MAX) &&
79 best_period < period) {
80 best_period = period;
81 best_scaled_freq = scaled_freq;
82 best_prescaler = prescaler;
83 }
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070084 }
85
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070086 if (best_period - 1 > SUNXI_PWM_CH0_PERIOD_MAX) {
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070087 debug("%s: failed to find prescaler value\n", __func__);
88 return -EINVAL;
89 }
90
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070091 duty = lldiv(best_scaled_freq * duty_ns, nsecs_per_sec);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070092
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070093 if (priv->prescaler != best_prescaler) {
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070094 /* Mask clock to update prescaler */
95 v = readl(&regs->ctrl);
96 v &= ~SUNXI_PWM_CTRL_CLK_GATE;
97 writel(v, &regs->ctrl);
98 v &= ~SUNXI_PWM_CTRL_PRESCALE0_MASK;
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070099 v |= (best_prescaler & SUNXI_PWM_CTRL_PRESCALE0_MASK);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700100 writel(v, &regs->ctrl);
101 v |= SUNXI_PWM_CTRL_CLK_GATE;
102 writel(v, &regs->ctrl);
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -0700103 priv->prescaler = best_prescaler;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700104 }
105
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -0700106 writel(SUNXI_PWM_CH0_PERIOD_PRD(best_period) |
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700107 SUNXI_PWM_CH0_PERIOD_DUTY(duty), &regs->ch0_period);
108
109 debug("%s: prescaler: %d, period: %d, duty: %d\n",
110 __func__, priv->prescaler,
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -0700111 best_period, duty);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700112
113 return 0;
114}
115
116static int sunxi_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
117{
118 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
119 struct sunxi_pwm *regs = priv->regs;
120 u32 v;
121
122 debug("%s: Enable '%s'\n", __func__, dev->name);
123
124 v = readl(&regs->ctrl);
125 if (!enable) {
126 v &= ~SUNXI_PWM_CTRL_ENABLE0;
127 writel(v, &regs->ctrl);
128 return 0;
129 }
130
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700131 if (priv->invert)
132 v &= ~SUNXI_PWM_CTRL_CH0_ACT_STA;
133 else
134 v |= SUNXI_PWM_CTRL_CH0_ACT_STA;
135 v |= SUNXI_PWM_CTRL_ENABLE0;
136 writel(v, &regs->ctrl);
137
138 return 0;
139}
140
Simon Glassaad29ae2020-12-03 16:55:21 -0700141static int sunxi_pwm_of_to_plat(struct udevice *dev)
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700142{
143 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
144
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900145 priv->regs = dev_read_addr_ptr(dev);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700146
147 return 0;
148}
149
150static int sunxi_pwm_probe(struct udevice *dev)
151{
152 return 0;
153}
154
155static const struct pwm_ops sunxi_pwm_ops = {
156 .set_invert = sunxi_pwm_set_invert,
157 .set_config = sunxi_pwm_set_config,
158 .set_enable = sunxi_pwm_set_enable,
159};
160
161static const struct udevice_id sunxi_pwm_ids[] = {
162 { .compatible = "allwinner,sun5i-a13-pwm" },
163 { .compatible = "allwinner,sun50i-a64-pwm" },
164 { }
165};
166
167U_BOOT_DRIVER(sunxi_pwm) = {
168 .name = "sunxi_pwm",
169 .id = UCLASS_PWM,
170 .of_match = sunxi_pwm_ids,
171 .ops = &sunxi_pwm_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700172 .of_to_plat = sunxi_pwm_of_to_plat,
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700173 .probe = sunxi_pwm_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700174 .priv_auto = sizeof(struct sunxi_pwm_priv),
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700175};