blob: 9b8a8c189d0938815436e59f04692afc28825f90 [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>
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +010016#include <clk.h>
Heiko Schocher2b387762014-07-18 06:07:19 +020017
Heiko Schocher00aa0662019-05-28 06:51:52 +020018int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles,
19 unsigned long duty_cycles, unsigned long prescale)
Heiko Schocher2b387762014-07-18 06:07:19 +020020{
Heiko Schocher2b387762014-07-18 06:07:19 +020021 u32 cr;
22
Heiko Schocher00aa0662019-05-28 06:51:52 +020023 writel(0, &pwm->ir);
Heiko Schocher2b387762014-07-18 06:07:19 +020024 cr = PWMCR_PRESCALER(prescale) |
25 PWMCR_DOZEEN | PWMCR_WAITEN |
26 PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH;
27
28 writel(cr, &pwm->cr);
29 /* set duty cycles */
30 writel(duty_cycles, &pwm->sar);
31 /* set period cycles */
32 writel(period_cycles, &pwm->pr);
33 return 0;
34}
35
Tommaso Merciaif57578b2022-03-26 12:19:08 +010036#ifndef CONFIG_DM_PWM
Tommaso Merciai8c3b6172022-03-26 12:19:05 +010037/* pwm_id from 0..7 */
38struct pwm_regs *pwm_id_to_reg(int pwm_id)
39{
40
41 switch (pwm_id) {
42 case 0:
43 return (struct pwm_regs *)PWM1_BASE_ADDR;
44 case 1:
45 return (struct pwm_regs *)PWM2_BASE_ADDR;
46#ifdef CONFIG_MX6
47 case 2:
48 return (struct pwm_regs *)PWM3_BASE_ADDR;
49 case 3:
50 return (struct pwm_regs *)PWM4_BASE_ADDR;
51#endif
52#ifdef CONFIG_MX6SX
53 case 4:
54 return (struct pwm_regs *)PWM5_BASE_ADDR;
55 case 5:
56 return (struct pwm_regs *)PWM6_BASE_ADDR;
57 case 6:
58 return (struct pwm_regs *)PWM7_BASE_ADDR;
59 case 7:
60 return (struct pwm_regs *)PWM8_BASE_ADDR;
61#endif
62 default:
63 printf("unknown pwm_id: %d\n", pwm_id);
64 break;
65 }
66 return NULL;
67}
68
69int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c,
70 unsigned long *duty_c, unsigned long *prescale)
71{
72 unsigned long long c;
73
74 /*
75 * we have not yet a clock framework for imx6, so add the clock
76 * value here as a define. Replace it when we have the clock
77 * framework.
78 */
79 c = CONFIG_IMX6_PWM_PER_CLK;
80 c = c * period_ns;
81 do_div(c, 1000000000);
82 *period_c = c;
83
84 *prescale = *period_c / 0x10000 + 1;
85
86 *period_c /= *prescale;
87 c = *period_c * (unsigned long long)duty_ns;
88 do_div(c, period_ns);
89 *duty_c = c;
90
91 /*
92 * according to imx pwm RM, the real period value should be
93 * PERIOD value in PWMPR plus 2.
94 */
95 if (*period_c > 2)
96 *period_c -= 2;
97 else
98 *period_c = 0;
99
100 return 0;
101}
102
Tommaso Merciaif57578b2022-03-26 12:19:08 +0100103int pwm_init(int pwm_id, int div, int invert)
104{
105 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
106
107 if (!pwm)
108 return -1;
109
110 writel(0, &pwm->ir);
111 return 0;
112}
113
Heiko Schocher00aa0662019-05-28 06:51:52 +0200114int pwm_config(int pwm_id, int duty_ns, int period_ns)
115{
116 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
117 unsigned long period_cycles, duty_cycles, prescale;
118
119 if (!pwm)
120 return -1;
121
122 pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles,
123 &prescale);
124
125 return pwm_config_internal(pwm, period_cycles, duty_cycles, prescale);
126}
127
Heiko Schocher2b387762014-07-18 06:07:19 +0200128int pwm_enable(int pwm_id)
129{
130 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
131
Axel Linecae6442015-05-23 15:16:48 +0800132 if (!pwm)
133 return -1;
134
Heiko Schocher2b387762014-07-18 06:07:19 +0200135 setbits_le32(&pwm->cr, PWMCR_EN);
136 return 0;
137}
138
139void pwm_disable(int pwm_id)
140{
141 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
142
Axel Linecae6442015-05-23 15:16:48 +0800143 if (!pwm)
144 return;
145
Heiko Schocher2b387762014-07-18 06:07:19 +0200146 clrbits_le32(&pwm->cr, PWMCR_EN);
147}
Heiko Schocher00aa0662019-05-28 06:51:52 +0200148
Tommaso Merciaif57578b2022-03-26 12:19:08 +0100149#else
Heiko Schocher00aa0662019-05-28 06:51:52 +0200150struct imx_pwm_priv {
151 struct pwm_regs *regs;
152 bool invert;
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100153 struct clk per_clk;
154 struct clk ipg_clk;
Heiko Schocher00aa0662019-05-28 06:51:52 +0200155};
156
Tommaso Merciai42eb2962022-03-26 12:19:07 +0100157int pwm_dm_imx_get_parms(struct imx_pwm_priv *priv, int period_ns,
158 int duty_ns, unsigned long *period_c, unsigned long *duty_c,
159 unsigned long *prescale)
160{
161 unsigned long long c;
162
163 c = clk_get_rate(&priv->per_clk);
164 c = c * period_ns;
165 do_div(c, 1000000000);
166 *period_c = c;
167
168 *prescale = *period_c / 0x10000 + 1;
169
170 *period_c /= *prescale;
171 c = *period_c * (unsigned long long)duty_ns;
172 do_div(c, period_ns);
173 *duty_c = c;
174
175 /*
176 * according to imx pwm RM, the real period value should be
177 * PERIOD value in PWMPR plus 2.
178 */
179 if (*period_c > 2)
180 *period_c -= 2;
181 else
182 *period_c = 0;
183
184 return 0;
185}
186
Heiko Schocher00aa0662019-05-28 06:51:52 +0200187static int imx_pwm_set_invert(struct udevice *dev, uint channel,
188 bool polarity)
189{
190 struct imx_pwm_priv *priv = dev_get_priv(dev);
191
192 debug("%s: polarity=%u\n", __func__, polarity);
193 priv->invert = polarity;
194
195 return 0;
196}
197
198static int imx_pwm_set_config(struct udevice *dev, uint channel,
199 uint period_ns, uint duty_ns)
200{
201 struct imx_pwm_priv *priv = dev_get_priv(dev);
202 struct pwm_regs *regs = priv->regs;
203 unsigned long period_cycles, duty_cycles, prescale;
204
205 debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel);
206
Tommaso Merciai42eb2962022-03-26 12:19:07 +0100207 pwm_dm_imx_get_parms(priv, period_ns, duty_ns, &period_cycles, &duty_cycles,
Heiko Schocher00aa0662019-05-28 06:51:52 +0200208 &prescale);
209
210 return pwm_config_internal(regs, period_cycles, duty_cycles, prescale);
211};
212
213static int imx_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
214{
215 struct imx_pwm_priv *priv = dev_get_priv(dev);
216 struct pwm_regs *regs = priv->regs;
217
218 debug("%s: Enable '%s' state: %d\n", __func__, dev->name, enable);
219
220 if (enable)
221 setbits_le32(&regs->cr, PWMCR_EN);
222 else
223 clrbits_le32(&regs->cr, PWMCR_EN);
224
225 return 0;
226};
227
Simon Glassaad29ae2020-12-03 16:55:21 -0700228static int imx_pwm_of_to_plat(struct udevice *dev)
Heiko Schocher00aa0662019-05-28 06:51:52 +0200229{
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100230 int ret;
Heiko Schocher00aa0662019-05-28 06:51:52 +0200231 struct imx_pwm_priv *priv = dev_get_priv(dev);
232
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900233 priv->regs = dev_read_addr_ptr(dev);
Heiko Schocher00aa0662019-05-28 06:51:52 +0200234
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100235 ret = clk_get_by_name(dev, "per", &priv->per_clk);
236 if (ret) {
237 printf("Failed to get per_clk\n");
238 return ret;
239 }
240
241 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
242 if (ret) {
243 printf("Failed to get ipg_clk\n");
244 return ret;
245 }
246
Heiko Schocher00aa0662019-05-28 06:51:52 +0200247 return 0;
248}
249
250static int imx_pwm_probe(struct udevice *dev)
251{
Tommaso Merciai20b9c1d2022-03-26 12:19:06 +0100252 int ret;
253 struct imx_pwm_priv *priv = dev_get_priv(dev);
254
255 ret = clk_enable(&priv->per_clk);
256 if (ret) {
257 printf("Failed to enable per_clk\n");
258 return ret;
259 }
260
261 ret = clk_enable(&priv->ipg_clk);
262 if (ret) {
263 printf("Failed to enable ipg_clk\n");
264 return ret;
265 }
266
Heiko Schocher00aa0662019-05-28 06:51:52 +0200267 return 0;
268}
269
270static const struct pwm_ops imx_pwm_ops = {
271 .set_invert = imx_pwm_set_invert,
272 .set_config = imx_pwm_set_config,
273 .set_enable = imx_pwm_set_enable,
274};
275
276static const struct udevice_id imx_pwm_ids[] = {
277 { .compatible = "fsl,imx27-pwm" },
278 { }
279};
280
281U_BOOT_DRIVER(imx_pwm) = {
282 .name = "imx_pwm",
283 .id = UCLASS_PWM,
284 .of_match = imx_pwm_ids,
285 .ops = &imx_pwm_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700286 .of_to_plat = imx_pwm_of_to_plat,
Heiko Schocher00aa0662019-05-28 06:51:52 +0200287 .probe = imx_pwm_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700288 .priv_auto = sizeof(struct imx_pwm_priv),
Heiko Schocher00aa0662019-05-28 06:51:52 +0200289};
290#endif