xref: /openbmc/linux/drivers/pwm/pwm-berlin.c (revision f220d3eb)
1 /*
2  * Marvell Berlin PWM driver
3  *
4  * Copyright (C) 2015 Marvell Technology Group Ltd.
5  *
6  * Author: Antoine Tenart <antoine.tenart@free-electrons.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2. This program is licensed "as is" without any
10  * warranty of any kind, whether express or implied.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/io.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
20 
21 #define BERLIN_PWM_EN			0x0
22 #define  BERLIN_PWM_ENABLE		BIT(0)
23 #define BERLIN_PWM_CONTROL		0x4
24 /*
25  * The prescaler claims to support 8 different moduli, configured using the
26  * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
27  * 256, 1024, and 4096.)  However, the moduli from 4 to 1024 appear to be
28  * implemented by internally shifting TCNT left without adding additional
29  * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
30  * for 8, 0x1fff; and so on. This means that those moduli are entirely
31  * useless, as we could just do the shift ourselves. The 4096 modulus is
32  * implemented with a real prescaler, so we do use that, but we treat it
33  * as a flag instead of pretending the modulus is actually configurable.
34  */
35 #define  BERLIN_PWM_PRESCALE_4096	0x7
36 #define  BERLIN_PWM_INVERT_POLARITY	BIT(3)
37 #define BERLIN_PWM_DUTY			0x8
38 #define BERLIN_PWM_TCNT			0xc
39 #define  BERLIN_PWM_MAX_TCNT		65535
40 
41 struct berlin_pwm_channel {
42 	u32 enable;
43 	u32 ctrl;
44 	u32 duty;
45 	u32 tcnt;
46 };
47 
48 struct berlin_pwm_chip {
49 	struct pwm_chip chip;
50 	struct clk *clk;
51 	void __iomem *base;
52 };
53 
54 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
55 {
56 	return container_of(chip, struct berlin_pwm_chip, chip);
57 }
58 
59 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip,
60 				   unsigned int channel, unsigned long offset)
61 {
62 	return readl_relaxed(chip->base + channel * 0x10 + offset);
63 }
64 
65 static inline void berlin_pwm_writel(struct berlin_pwm_chip *chip,
66 				     unsigned int channel, u32 value,
67 				     unsigned long offset)
68 {
69 	writel_relaxed(value, chip->base + channel * 0x10 + offset);
70 }
71 
72 static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
73 {
74 	struct berlin_pwm_channel *channel;
75 
76 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
77 	if (!channel)
78 		return -ENOMEM;
79 
80 	return pwm_set_chip_data(pwm, channel);
81 }
82 
83 static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
84 {
85 	struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
86 
87 	pwm_set_chip_data(pwm, NULL);
88 	kfree(channel);
89 }
90 
91 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
92 			     int duty_ns, int period_ns)
93 {
94 	struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
95 	bool prescale_4096 = false;
96 	u32 value, duty, period;
97 	u64 cycles;
98 
99 	cycles = clk_get_rate(pwm->clk);
100 	cycles *= period_ns;
101 	do_div(cycles, NSEC_PER_SEC);
102 
103 	if (cycles > BERLIN_PWM_MAX_TCNT) {
104 		prescale_4096 = true;
105 		cycles >>= 12; // Prescaled by 4096
106 
107 		if (cycles > BERLIN_PWM_MAX_TCNT)
108 			return -ERANGE;
109 	}
110 
111 	period = cycles;
112 	cycles *= duty_ns;
113 	do_div(cycles, period_ns);
114 	duty = cycles;
115 
116 	value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
117 	if (prescale_4096)
118 		value |= BERLIN_PWM_PRESCALE_4096;
119 	else
120 		value &= ~BERLIN_PWM_PRESCALE_4096;
121 	berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
122 
123 	berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);
124 	berlin_pwm_writel(pwm, pwm_dev->hwpwm, period, BERLIN_PWM_TCNT);
125 
126 	return 0;
127 }
128 
129 static int berlin_pwm_set_polarity(struct pwm_chip *chip,
130 				   struct pwm_device *pwm_dev,
131 				   enum pwm_polarity polarity)
132 {
133 	struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
134 	u32 value;
135 
136 	value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
137 
138 	if (polarity == PWM_POLARITY_NORMAL)
139 		value &= ~BERLIN_PWM_INVERT_POLARITY;
140 	else
141 		value |= BERLIN_PWM_INVERT_POLARITY;
142 
143 	berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
144 
145 	return 0;
146 }
147 
148 static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm_dev)
149 {
150 	struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
151 	u32 value;
152 
153 	value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
154 	value |= BERLIN_PWM_ENABLE;
155 	berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
156 
157 	return 0;
158 }
159 
160 static void berlin_pwm_disable(struct pwm_chip *chip,
161 			       struct pwm_device *pwm_dev)
162 {
163 	struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
164 	u32 value;
165 
166 	value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
167 	value &= ~BERLIN_PWM_ENABLE;
168 	berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
169 }
170 
171 static const struct pwm_ops berlin_pwm_ops = {
172 	.request = berlin_pwm_request,
173 	.free = berlin_pwm_free,
174 	.config = berlin_pwm_config,
175 	.set_polarity = berlin_pwm_set_polarity,
176 	.enable = berlin_pwm_enable,
177 	.disable = berlin_pwm_disable,
178 	.owner = THIS_MODULE,
179 };
180 
181 static const struct of_device_id berlin_pwm_match[] = {
182 	{ .compatible = "marvell,berlin-pwm" },
183 	{ },
184 };
185 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
186 
187 static int berlin_pwm_probe(struct platform_device *pdev)
188 {
189 	struct berlin_pwm_chip *pwm;
190 	struct resource *res;
191 	int ret;
192 
193 	pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
194 	if (!pwm)
195 		return -ENOMEM;
196 
197 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
198 	pwm->base = devm_ioremap_resource(&pdev->dev, res);
199 	if (IS_ERR(pwm->base))
200 		return PTR_ERR(pwm->base);
201 
202 	pwm->clk = devm_clk_get(&pdev->dev, NULL);
203 	if (IS_ERR(pwm->clk))
204 		return PTR_ERR(pwm->clk);
205 
206 	ret = clk_prepare_enable(pwm->clk);
207 	if (ret)
208 		return ret;
209 
210 	pwm->chip.dev = &pdev->dev;
211 	pwm->chip.ops = &berlin_pwm_ops;
212 	pwm->chip.base = -1;
213 	pwm->chip.npwm = 4;
214 	pwm->chip.of_xlate = of_pwm_xlate_with_flags;
215 	pwm->chip.of_pwm_n_cells = 3;
216 
217 	ret = pwmchip_add(&pwm->chip);
218 	if (ret < 0) {
219 		dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
220 		clk_disable_unprepare(pwm->clk);
221 		return ret;
222 	}
223 
224 	platform_set_drvdata(pdev, pwm);
225 
226 	return 0;
227 }
228 
229 static int berlin_pwm_remove(struct platform_device *pdev)
230 {
231 	struct berlin_pwm_chip *pwm = platform_get_drvdata(pdev);
232 	int ret;
233 
234 	ret = pwmchip_remove(&pwm->chip);
235 	clk_disable_unprepare(pwm->clk);
236 
237 	return ret;
238 }
239 
240 #ifdef CONFIG_PM_SLEEP
241 static int berlin_pwm_suspend(struct device *dev)
242 {
243 	struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
244 	unsigned int i;
245 
246 	for (i = 0; i < pwm->chip.npwm; i++) {
247 		struct berlin_pwm_channel *channel;
248 
249 		channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
250 		if (!channel)
251 			continue;
252 
253 		channel->enable = berlin_pwm_readl(pwm, i, BERLIN_PWM_ENABLE);
254 		channel->ctrl = berlin_pwm_readl(pwm, i, BERLIN_PWM_CONTROL);
255 		channel->duty = berlin_pwm_readl(pwm, i, BERLIN_PWM_DUTY);
256 		channel->tcnt = berlin_pwm_readl(pwm, i, BERLIN_PWM_TCNT);
257 	}
258 
259 	clk_disable_unprepare(pwm->clk);
260 
261 	return 0;
262 }
263 
264 static int berlin_pwm_resume(struct device *dev)
265 {
266 	struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
267 	unsigned int i;
268 	int ret;
269 
270 	ret = clk_prepare_enable(pwm->clk);
271 	if (ret)
272 		return ret;
273 
274 	for (i = 0; i < pwm->chip.npwm; i++) {
275 		struct berlin_pwm_channel *channel;
276 
277 		channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
278 		if (!channel)
279 			continue;
280 
281 		berlin_pwm_writel(pwm, i, channel->ctrl, BERLIN_PWM_CONTROL);
282 		berlin_pwm_writel(pwm, i, channel->duty, BERLIN_PWM_DUTY);
283 		berlin_pwm_writel(pwm, i, channel->tcnt, BERLIN_PWM_TCNT);
284 		berlin_pwm_writel(pwm, i, channel->enable, BERLIN_PWM_ENABLE);
285 	}
286 
287 	return 0;
288 }
289 #endif
290 
291 static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
292 			 berlin_pwm_resume);
293 
294 static struct platform_driver berlin_pwm_driver = {
295 	.probe = berlin_pwm_probe,
296 	.remove = berlin_pwm_remove,
297 	.driver = {
298 		.name = "berlin-pwm",
299 		.of_match_table = berlin_pwm_match,
300 		.pm = &berlin_pwm_pm_ops,
301 	},
302 };
303 module_platform_driver(berlin_pwm_driver);
304 
305 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
306 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
307 MODULE_LICENSE("GPL v2");
308