phy: atheros: move delay config to common function
[oweals/u-boot.git] / drivers / pwm / pwm-imx.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014
4  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5  *
6  * Basic support for the pwm module on imx6.
7  */
8
9 #include <common.h>
10 #include <div64.h>
11 #include <dm.h>
12 #include <pwm.h>
13 #include <asm/arch/imx-regs.h>
14 #include <asm/io.h>
15 #include "pwm-imx-util.h"
16
17 int 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
21         if (!pwm)
22                 return -1;
23
24         writel(0, &pwm->ir);
25         return 0;
26 }
27
28 int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles,
29                         unsigned long duty_cycles, unsigned long prescale)
30 {
31         u32 cr;
32
33         writel(0, &pwm->ir);
34         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
46 int 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
60 int pwm_enable(int pwm_id)
61 {
62         struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
63
64         if (!pwm)
65                 return -1;
66
67         setbits_le32(&pwm->cr, PWMCR_EN);
68         return 0;
69 }
70
71 void pwm_disable(int pwm_id)
72 {
73         struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id);
74
75         if (!pwm)
76                 return;
77
78         clrbits_le32(&pwm->cr, PWMCR_EN);
79 }
80
81 #if defined(CONFIG_DM_PWM)
82 struct imx_pwm_priv {
83         struct pwm_regs *regs;
84         bool invert;
85 };
86
87 static 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
98 static 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
113 static 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
128 static 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
137 static int imx_pwm_probe(struct udevice *dev)
138 {
139         return 0;
140 }
141
142 static 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
148 static const struct udevice_id imx_pwm_ids[] = {
149         { .compatible = "fsl,imx27-pwm" },
150         { }
151 };
152
153 U_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