1
2
3
4
5
6
7
8
9#include <common.h>
10#include <div64.h>
11#include <dm.h>
12#include <log.h>
13#include <pwm.h>
14#include <asm/arch/imx-regs.h>
15#include <asm/io.h>
16#include "pwm-imx-util.h"
17
18int pwm_init(int pwm_id, int div, int invert)
19{
20 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
21
22 if (!pwm)
23 return -1;
24
25 writel(0, &pwm->ir);
26 return 0;
27}
28
29int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles,
30 unsigned long duty_cycles, unsigned long prescale)
31{
32 u32 cr;
33
34 writel(0, &pwm->ir);
35 cr = PWMCR_PRESCALER(prescale) |
36 PWMCR_DOZEEN | PWMCR_WAITEN |
37 PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH;
38
39 writel(cr, &pwm->cr);
40
41 writel(duty_cycles, &pwm->sar);
42
43 writel(period_cycles, &pwm->pr);
44 return 0;
45}
46
47int pwm_config(int pwm_id, int duty_ns, int period_ns)
48{
49 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
50 unsigned long period_cycles, duty_cycles, prescale;
51
52 if (!pwm)
53 return -1;
54
55 pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles,
56 &prescale);
57
58 return pwm_config_internal(pwm, period_cycles, duty_cycles, prescale);
59}
60
61int pwm_enable(int pwm_id)
62{
63 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
64
65 if (!pwm)
66 return -1;
67
68 setbits_le32(&pwm->cr, PWMCR_EN);
69 return 0;
70}
71
72void pwm_disable(int pwm_id)
73{
74 struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
75
76 if (!pwm)
77 return;
78
79 clrbits_le32(&pwm->cr, PWMCR_EN);
80}
81
82#if defined(CONFIG_DM_PWM)
83struct imx_pwm_priv {
84 struct pwm_regs *regs;
85 bool invert;
86};
87
88static int imx_pwm_set_invert(struct udevice *dev, uint channel,
89 bool polarity)
90{
91 struct imx_pwm_priv *priv = dev_get_priv(dev);
92
93 debug("%s: polarity=%u\n", __func__, polarity);
94 priv->invert = polarity;
95
96 return 0;
97}
98
99static int imx_pwm_set_config(struct udevice *dev, uint channel,
100 uint period_ns, uint duty_ns)
101{
102 struct imx_pwm_priv *priv = dev_get_priv(dev);
103 struct pwm_regs *regs = priv->regs;
104 unsigned long period_cycles, duty_cycles, prescale;
105
106 debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel);
107
108 pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles,
109 &prescale);
110
111 return pwm_config_internal(regs, period_cycles, duty_cycles, prescale);
112};
113
114static int imx_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
115{
116 struct imx_pwm_priv *priv = dev_get_priv(dev);
117 struct pwm_regs *regs = priv->regs;
118
119 debug("%s: Enable '%s' state: %d\n", __func__, dev->name, enable);
120
121 if (enable)
122 setbits_le32(®s->cr, PWMCR_EN);
123 else
124 clrbits_le32(®s->cr, PWMCR_EN);
125
126 return 0;
127};
128
129static int imx_pwm_ofdata_to_platdata(struct udevice *dev)
130{
131 struct imx_pwm_priv *priv = dev_get_priv(dev);
132
133 priv->regs = dev_read_addr_ptr(dev);
134
135 return 0;
136}
137
138static int imx_pwm_probe(struct udevice *dev)
139{
140 return 0;
141}
142
143static const struct pwm_ops imx_pwm_ops = {
144 .set_invert = imx_pwm_set_invert,
145 .set_config = imx_pwm_set_config,
146 .set_enable = imx_pwm_set_enable,
147};
148
149static const struct udevice_id imx_pwm_ids[] = {
150 { .compatible = "fsl,imx27-pwm" },
151 { }
152};
153
154U_BOOT_DRIVER(imx_pwm) = {
155 .name = "imx_pwm",
156 .id = UCLASS_PWM,
157 .of_match = imx_pwm_ids,
158 .ops = &imx_pwm_ops,
159 .ofdata_to_platdata = imx_pwm_ofdata_to_platdata,
160 .probe = imx_pwm_probe,
161 .priv_auto_alloc_size = sizeof(struct imx_pwm_priv),
162};
163#endif
164