blob: 1d656e726f54e023a593332f1d3b8f98cd83a6ef [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher2b387762014-07-18 06:07:19 +02002/*
3 * (C) Copyright 2014
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 *
Robert P. J. Day1f8378a2016-09-13 08:35:18 -04006 * Basic support for the pwm module on imx6.
Heiko Schocher2b387762014-07-18 06:07:19 +02007 */
8
9#include <common.h>
10#include <div64.h>
Heiko Schocher00aa0662019-05-28 06:51:52 +020011#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Heiko Schocher2b387762014-07-18 06:07:19 +020013#include <pwm.h>
14#include <asm/arch/imx-regs.h>
15#include <asm/io.h>
Heiko Schocher2b387762014-07-18 06:07:19 +020016
17int pwm_init(int pwm_id, int div, int invert)
18{
19 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
20
Axel Linecae6442015-05-23 15:16:48 +080021 if (!pwm)
22 return -1;
23
Heiko Schocher2b387762014-07-18 06:07:19 +020024 writel(0, &pwm->ir);
25 return 0;
26}
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +010027#include <clk.h>
Heiko Schocher2b387762014-07-18 06:07:19 +020028
Heiko Schocher00aa0662019-05-28 06:51:52 +020029int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles,
30 unsigned long duty_cycles, unsigned long prescale)
Heiko Schocher2b387762014-07-18 06:07:19 +020031{
Heiko Schocher2b387762014-07-18 06:07:19 +020032 u32 cr;
33
Heiko Schocher00aa0662019-05-28 06:51:52 +020034 writel(0, &pwm->ir);
Heiko Schocher2b387762014-07-18 06:07:19 +020035 cr = PWMCR_PRESCALER(prescale) |
36 PWMCR_DOZEEN | PWMCR_WAITEN |
37 PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH;
38
39 writel(cr, &pwm->cr);
40 /* set duty cycles */
41 writel(duty_cycles, &pwm->sar);
42 /* set period cycles */
43 writel(period_cycles, &pwm->pr);
44 return 0;
45}
46
Tommaso Merciai8c3b6172022-03-26 12:19:05 +010047/* pwm_id from 0..7 */
48struct pwm_regs *pwm_id_to_reg(int pwm_id)
49{
50
51 switch (pwm_id) {
52 case 0:
53 return (struct pwm_regs *)PWM1_BASE_ADDR;
54 case 1:
55 return (struct pwm_regs *)PWM2_BASE_ADDR;
56#ifdef CONFIG_MX6
57 case 2:
58 return (struct pwm_regs *)PWM3_BASE_ADDR;
59 case 3:
60 return (struct pwm_regs *)PWM4_BASE_ADDR;
61#endif
62#ifdef CONFIG_MX6SX
63 case 4:
64 return (struct pwm_regs *)PWM5_BASE_ADDR;
65 case 5:
66 return (struct pwm_regs *)PWM6_BASE_ADDR;
67 case 6:
68 return (struct pwm_regs *)PWM7_BASE_ADDR;
69 case 7:
70 return (struct pwm_regs *)PWM8_BASE_ADDR;
71#endif
72 default:
73 printf("unknown pwm_id: %d\n", pwm_id);
74 break;
75 }
76 return NULL;
77}
78
79int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c,
80 unsigned long *duty_c, unsigned long *prescale)
81{
82 unsigned long long c;
83
84 /*
85 * we have not yet a clock framework for imx6, so add the clock
86 * value here as a define. Replace it when we have the clock
87 * framework.
88 */
89 c = CONFIG_IMX6_PWM_PER_CLK;
90 c = c * period_ns;
91 do_div(c, 1000000000);
92 *period_c = c;
93
94 *prescale = *period_c / 0x10000 + 1;
95
96 *period_c /= *prescale;
97 c = *period_c * (unsigned long long)duty_ns;
98 do_div(c, period_ns);
99 *duty_c = c;
100
101 /*
102 * according to imx pwm RM, the real period value should be
103 * PERIOD value in PWMPR plus 2.
104 */
105 if (*period_c > 2)
106 *period_c -= 2;
107 else
108 *period_c = 0;
109
110 return 0;
111}
112
Heiko Schocher00aa0662019-05-28 06:51:52 +0200113int pwm_config(int pwm_id, int duty_ns, int period_ns)
114{
115 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
116 unsigned long period_cycles, duty_cycles, prescale;
117
118 if (!pwm)
119 return -1;
120
121 pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles,
122 &prescale);
123
124 return pwm_config_internal(pwm, period_cycles, duty_cycles, prescale);
125}
126
Heiko Schocher2b387762014-07-18 06:07:19 +0200127int pwm_enable(int pwm_id)
128{
129 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
130
Axel Linecae6442015-05-23 15:16:48 +0800131 if (!pwm)
132 return -1;
133
Heiko Schocher2b387762014-07-18 06:07:19 +0200134 setbits_le32(&pwm->cr, PWMCR_EN);
135 return 0;
136}
137
138void pwm_disable(int pwm_id)
139{
140 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
141
Axel Linecae6442015-05-23 15:16:48 +0800142 if (!pwm)
143 return;
144
Heiko Schocher2b387762014-07-18 06:07:19 +0200145 clrbits_le32(&pwm->cr, PWMCR_EN);
146}
Heiko Schocher00aa0662019-05-28 06:51:52 +0200147
148#if defined(CONFIG_DM_PWM)
149struct imx_pwm_priv {
150 struct pwm_regs *regs;
151 bool invert;
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100152 struct clk per_clk;
153 struct clk ipg_clk;
Heiko Schocher00aa0662019-05-28 06:51:52 +0200154};
155
Tommaso Merciai42eb2962022-03-26 12:19:07 +0100156int pwm_dm_imx_get_parms(struct imx_pwm_priv *priv, int period_ns,
157 int duty_ns, unsigned long *period_c, unsigned long *duty_c,
158 unsigned long *prescale)
159{
160 unsigned long long c;
161
162 c = clk_get_rate(&priv->per_clk);
163 c = c * period_ns;
164 do_div(c, 1000000000);
165 *period_c = c;
166
167 *prescale = *period_c / 0x10000 + 1;
168
169 *period_c /= *prescale;
170 c = *period_c * (unsigned long long)duty_ns;
171 do_div(c, period_ns);
172 *duty_c = c;
173
174 /*
175 * according to imx pwm RM, the real period value should be
176 * PERIOD value in PWMPR plus 2.
177 */
178 if (*period_c > 2)
179 *period_c -= 2;
180 else
181 *period_c = 0;
182
183 return 0;
184}
185
Heiko Schocher00aa0662019-05-28 06:51:52 +0200186static int imx_pwm_set_invert(struct udevice *dev, uint channel,
187 bool polarity)
188{
189 struct imx_pwm_priv *priv = dev_get_priv(dev);
190
191 debug("%s: polarity=%u\n", __func__, polarity);
192 priv->invert = polarity;
193
194 return 0;
195}
196
197static int imx_pwm_set_config(struct udevice *dev, uint channel,
198 uint period_ns, uint duty_ns)
199{
200 struct imx_pwm_priv *priv = dev_get_priv(dev);
201 struct pwm_regs *regs = priv->regs;
202 unsigned long period_cycles, duty_cycles, prescale;
203
204 debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel);
205
Tommaso Merciai42eb2962022-03-26 12:19:07 +0100206 pwm_dm_imx_get_parms(priv, period_ns, duty_ns, &period_cycles, &duty_cycles,
Heiko Schocher00aa0662019-05-28 06:51:52 +0200207 &prescale);
208
209 return pwm_config_internal(regs, period_cycles, duty_cycles, prescale);
210};
211
212static int imx_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
213{
214 struct imx_pwm_priv *priv = dev_get_priv(dev);
215 struct pwm_regs *regs = priv->regs;
216
217 debug("%s: Enable '%s' state: %d\n", __func__, dev->name, enable);
218
219 if (enable)
220 setbits_le32(&regs->cr, PWMCR_EN);
221 else
222 clrbits_le32(&regs->cr, PWMCR_EN);
223
224 return 0;
225};
226
Simon Glassaad29ae2020-12-03 16:55:21 -0700227static int imx_pwm_of_to_plat(struct udevice *dev)
Heiko Schocher00aa0662019-05-28 06:51:52 +0200228{
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100229 int ret;
Heiko Schocher00aa0662019-05-28 06:51:52 +0200230 struct imx_pwm_priv *priv = dev_get_priv(dev);
231
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900232 priv->regs = dev_read_addr_ptr(dev);
Heiko Schocher00aa0662019-05-28 06:51:52 +0200233
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100234 ret = clk_get_by_name(dev, "per", &priv->per_clk);
235 if (ret) {
236 printf("Failed to get per_clk\n");
237 return ret;
238 }
239
240 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
241 if (ret) {
242 printf("Failed to get ipg_clk\n");
243 return ret;
244 }
245
Heiko Schocher00aa0662019-05-28 06:51:52 +0200246 return 0;
247}
248
249static int imx_pwm_probe(struct udevice *dev)
250{
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100251 int ret;
252 struct imx_pwm_priv *priv = dev_get_priv(dev);
253
254 ret = clk_enable(&priv->per_clk);
255 if (ret) {
256 printf("Failed to enable per_clk\n");
257 return ret;
258 }
259
260 ret = clk_enable(&priv->ipg_clk);
261 if (ret) {
262 printf("Failed to enable ipg_clk\n");
263 return ret;
264 }
265
Heiko Schocher00aa0662019-05-28 06:51:52 +0200266 return 0;
267}
268
269static const struct pwm_ops imx_pwm_ops = {
270 .set_invert = imx_pwm_set_invert,
271 .set_config = imx_pwm_set_config,
272 .set_enable = imx_pwm_set_enable,
273};
274
275static const struct udevice_id imx_pwm_ids[] = {
276 { .compatible = "fsl,imx27-pwm" },
277 { }
278};
279
280U_BOOT_DRIVER(imx_pwm) = {
281 .name = "imx_pwm",
282 .id = UCLASS_PWM,
283 .of_match = imx_pwm_ids,
284 .ops = &imx_pwm_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700285 .of_to_plat = imx_pwm_of_to_plat,
Heiko Schocher00aa0662019-05-28 06:51:52 +0200286 .probe = imx_pwm_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700287 .priv_auto = sizeof(struct imx_pwm_priv),
Heiko Schocher00aa0662019-05-28 06:51:52 +0200288};
289#endif