blob: 320ea7c4239ab605f1e50f01734d0767c9a597c8 [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
Heiko Schocher2b387762014-07-18 06:07:19 +02009#include <div64.h>
Heiko Schocher00aa0662019-05-28 06:51:52 +020010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Heiko Schocher2b387762014-07-18 06:07:19 +020012#include <pwm.h>
13#include <asm/arch/imx-regs.h>
14#include <asm/io.h>
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +010015#include <clk.h>
Heiko Schocher2b387762014-07-18 06:07:19 +020016
Heiko Schocher00aa0662019-05-28 06:51:52 +020017int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles,
18 unsigned long duty_cycles, unsigned long prescale)
Heiko Schocher2b387762014-07-18 06:07:19 +020019{
Heiko Schocher2b387762014-07-18 06:07:19 +020020 u32 cr;
21
Heiko Schocher00aa0662019-05-28 06:51:52 +020022 writel(0, &pwm->ir);
Heiko Schocher2b387762014-07-18 06:07:19 +020023 cr = PWMCR_PRESCALER(prescale) |
24 PWMCR_DOZEEN | PWMCR_WAITEN |
25 PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH;
26
27 writel(cr, &pwm->cr);
28 /* set duty cycles */
29 writel(duty_cycles, &pwm->sar);
30 /* set period cycles */
31 writel(period_cycles, &pwm->pr);
32 return 0;
33}
34
Tommaso Merciaif57578b2022-03-26 12:19:08 +010035#ifndef CONFIG_DM_PWM
Tommaso Merciai8c3b6172022-03-26 12:19:05 +010036/* pwm_id from 0..7 */
37struct pwm_regs *pwm_id_to_reg(int pwm_id)
38{
39
40 switch (pwm_id) {
41 case 0:
42 return (struct pwm_regs *)PWM1_BASE_ADDR;
43 case 1:
44 return (struct pwm_regs *)PWM2_BASE_ADDR;
45#ifdef CONFIG_MX6
46 case 2:
47 return (struct pwm_regs *)PWM3_BASE_ADDR;
48 case 3:
49 return (struct pwm_regs *)PWM4_BASE_ADDR;
50#endif
51#ifdef CONFIG_MX6SX
52 case 4:
53 return (struct pwm_regs *)PWM5_BASE_ADDR;
54 case 5:
55 return (struct pwm_regs *)PWM6_BASE_ADDR;
56 case 6:
57 return (struct pwm_regs *)PWM7_BASE_ADDR;
58 case 7:
59 return (struct pwm_regs *)PWM8_BASE_ADDR;
60#endif
61 default:
62 printf("unknown pwm_id: %d\n", pwm_id);
63 break;
64 }
65 return NULL;
66}
67
68int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c,
69 unsigned long *duty_c, unsigned long *prescale)
70{
71 unsigned long long c;
72
73 /*
74 * we have not yet a clock framework for imx6, so add the clock
75 * value here as a define. Replace it when we have the clock
76 * framework.
77 */
Tom Rinifbdbe532022-12-04 10:04:12 -050078 c = CFG_IMX6_PWM_PER_CLK;
Tommaso Merciai8c3b6172022-03-26 12:19:05 +010079 c = c * period_ns;
80 do_div(c, 1000000000);
81 *period_c = c;
82
83 *prescale = *period_c / 0x10000 + 1;
84
85 *period_c /= *prescale;
86 c = *period_c * (unsigned long long)duty_ns;
87 do_div(c, period_ns);
88 *duty_c = c;
89
90 /*
91 * according to imx pwm RM, the real period value should be
92 * PERIOD value in PWMPR plus 2.
93 */
94 if (*period_c > 2)
95 *period_c -= 2;
96 else
97 *period_c = 0;
98
99 return 0;
100}
101
Tommaso Merciaif57578b2022-03-26 12:19:08 +0100102int pwm_init(int pwm_id, int div, int invert)
103{
104 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
105
106 if (!pwm)
107 return -1;
108
109 writel(0, &pwm->ir);
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
Tommaso Merciaif57578b2022-03-26 12:19:08 +0100148#else
Heiko Schocher00aa0662019-05-28 06:51:52 +0200149struct 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