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 <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 
pwm_init(int pwm_id,int div,int invert)18 int 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 
pwm_config_internal(struct pwm_regs * pwm,unsigned long period_cycles,unsigned long duty_cycles,unsigned long prescale)29 int 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 	/* set duty cycles */
41 	writel(duty_cycles, &pwm->sar);
42 	/* set period cycles */
43 	writel(period_cycles, &pwm->pr);
44 	return 0;
45 }
46 
pwm_config(int pwm_id,int duty_ns,int period_ns)47 int 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 
pwm_enable(int pwm_id)61 int 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 
pwm_disable(int pwm_id)72 void 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)
83 struct imx_pwm_priv {
84 	struct pwm_regs *regs;
85 	bool invert;
86 };
87 
imx_pwm_set_invert(struct udevice * dev,uint channel,bool polarity)88 static 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 
imx_pwm_set_config(struct udevice * dev,uint channel,uint period_ns,uint duty_ns)99 static 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 
imx_pwm_set_enable(struct udevice * dev,uint channel,bool enable)114 static 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(&regs->cr, PWMCR_EN);
123 	else
124 		clrbits_le32(&regs->cr, PWMCR_EN);
125 
126 	return 0;
127 };
128 
imx_pwm_of_to_plat(struct udevice * dev)129 static int imx_pwm_of_to_plat(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 
imx_pwm_probe(struct udevice * dev)138 static int imx_pwm_probe(struct udevice *dev)
139 {
140 	return 0;
141 }
142 
143 static 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 
149 static const struct udevice_id imx_pwm_ids[] = {
150 	{ .compatible = "fsl,imx27-pwm" },
151 	{ }
152 };
153 
154 U_BOOT_DRIVER(imx_pwm) = {
155 	.name	= "imx_pwm",
156 	.id	= UCLASS_PWM,
157 	.of_match = imx_pwm_ids,
158 	.ops	= &imx_pwm_ops,
159 	.of_to_plat	= imx_pwm_of_to_plat,
160 	.probe		= imx_pwm_probe,
161 	.priv_auto	= sizeof(struct imx_pwm_priv),
162 };
163 #endif
164