blob: 8d8f3e6f9f98603ed5b07d2e23ce42eff079319d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocherb2f97cf2014-07-18 06:07:19 +02002/*
3 * (C) Copyright 2014
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 *
Robert P. J. Day5052e812016-09-13 08:35:18 -04006 * Basic support for the pwm module on imx6.
Heiko Schocherb2f97cf2014-07-18 06:07:19 +02007 */
8
9#include <common.h>
10#include <div64.h>
Heiko Schocher46e10e92019-05-28 06:51:52 +020011#include <dm.h>
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020012#include <pwm.h>
13#include <asm/arch/imx-regs.h>
14#include <asm/io.h>
15#include "pwm-imx-util.h"
16
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 Lin16b0c0c2015-05-23 15:16:48 +080021 if (!pwm)
22 return -1;
23
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020024 writel(0, &pwm->ir);
25 return 0;
26}
27
Heiko Schocher46e10e92019-05-28 06:51:52 +020028int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles,
29 unsigned long duty_cycles, unsigned long prescale)
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020030{
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020031 u32 cr;
32
Heiko Schocher46e10e92019-05-28 06:51:52 +020033 writel(0, &pwm->ir);
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020034 cr = PWMCR_PRESCALER(prescale) |
35 PWMCR_DOZEEN | PWMCR_WAITEN |
36 PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH;
37
38 writel(cr, &pwm->cr);
39 /* set duty cycles */
40 writel(duty_cycles, &pwm->sar);
41 /* set period cycles */
42 writel(period_cycles, &pwm->pr);
43 return 0;
44}
45
Heiko Schocher46e10e92019-05-28 06:51:52 +020046int pwm_config(int pwm_id, int duty_ns, int period_ns)
47{
48 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
49 unsigned long period_cycles, duty_cycles, prescale;
50
51 if (!pwm)
52 return -1;
53
54 pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles,
55 &prescale);
56
57 return pwm_config_internal(pwm, period_cycles, duty_cycles, prescale);
58}
59
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020060int pwm_enable(int pwm_id)
61{
62 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
63
Axel Lin16b0c0c2015-05-23 15:16:48 +080064 if (!pwm)
65 return -1;
66
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020067 setbits_le32(&pwm->cr, PWMCR_EN);
68 return 0;
69}
70
71void pwm_disable(int pwm_id)
72{
73 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
74
Axel Lin16b0c0c2015-05-23 15:16:48 +080075 if (!pwm)
76 return;
77
Heiko Schocherb2f97cf2014-07-18 06:07:19 +020078 clrbits_le32(&pwm->cr, PWMCR_EN);
79}
Heiko Schocher46e10e92019-05-28 06:51:52 +020080
81#if defined(CONFIG_DM_PWM)
82struct imx_pwm_priv {
83 struct pwm_regs *regs;
84 bool invert;
85};
86
87static int imx_pwm_set_invert(struct udevice *dev, uint channel,
88 bool polarity)
89{
90 struct imx_pwm_priv *priv = dev_get_priv(dev);
91
92 debug("%s: polarity=%u\n", __func__, polarity);
93 priv->invert = polarity;
94
95 return 0;
96}
97
98static int imx_pwm_set_config(struct udevice *dev, uint channel,
99 uint period_ns, uint duty_ns)
100{
101 struct imx_pwm_priv *priv = dev_get_priv(dev);
102 struct pwm_regs *regs = priv->regs;
103 unsigned long period_cycles, duty_cycles, prescale;
104
105 debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel);
106
107 pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles,
108 &prescale);
109
110 return pwm_config_internal(regs, period_cycles, duty_cycles, prescale);
111};
112
113static int imx_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
114{
115 struct imx_pwm_priv *priv = dev_get_priv(dev);
116 struct pwm_regs *regs = priv->regs;
117
118 debug("%s: Enable '%s' state: %d\n", __func__, dev->name, enable);
119
120 if (enable)
121 setbits_le32(&regs->cr, PWMCR_EN);
122 else
123 clrbits_le32(&regs->cr, PWMCR_EN);
124
125 return 0;
126};
127
128static int imx_pwm_ofdata_to_platdata(struct udevice *dev)
129{
130 struct imx_pwm_priv *priv = dev_get_priv(dev);
131
132 priv->regs = (struct pwm_regs *)devfdt_get_addr(dev);
133
134 return 0;
135}
136
137static int imx_pwm_probe(struct udevice *dev)
138{
139 return 0;
140}
141
142static const struct pwm_ops imx_pwm_ops = {
143 .set_invert = imx_pwm_set_invert,
144 .set_config = imx_pwm_set_config,
145 .set_enable = imx_pwm_set_enable,
146};
147
148static const struct udevice_id imx_pwm_ids[] = {
149 { .compatible = "fsl,imx27-pwm" },
150 { }
151};
152
153U_BOOT_DRIVER(imx_pwm) = {
154 .name = "imx_pwm",
155 .id = UCLASS_PWM,
156 .of_match = imx_pwm_ids,
157 .ops = &imx_pwm_ops,
158 .ofdata_to_platdata = imx_pwm_ofdata_to_platdata,
159 .probe = imx_pwm_probe,
160 .priv_auto_alloc_size = sizeof(struct imx_pwm_priv),
161};
162#endif