xref: /openbmc/linux/drivers/pwm/pwm-berlin.c (revision 0ef9d78b)
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/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pwm.h>
20 #include <linux/slab.h>
21 
22 #define BERLIN_PWM_EN			0x0
23 #define  BERLIN_PWM_ENABLE		BIT(0)
24 #define BERLIN_PWM_CONTROL		0x4
25 /*
26  * The prescaler claims to support 8 different moduli, configured using the
27  * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
28  * 256, 1024, and 4096.)  However, the moduli from 4 to 1024 appear to be
29  * implemented by internally shifting TCNT left without adding additional
30  * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
31  * for 8, 0x1fff; and so on. This means that those moduli are entirely
32  * useless, as we could just do the shift ourselves. The 4096 modulus is
33  * implemented with a real prescaler, so we do use that, but we treat it
34  * as a flag instead of pretending the modulus is actually configurable.
35  */
36 #define  BERLIN_PWM_PRESCALE_4096	0x7
37 #define  BERLIN_PWM_INVERT_POLARITY	BIT(3)
38 #define BERLIN_PWM_DUTY			0x8
39 #define BERLIN_PWM_TCNT			0xc
40 #define  BERLIN_PWM_MAX_TCNT		65535
41 
42 struct berlin_pwm_channel {
43 	u32 enable;
44 	u32 ctrl;
45 	u32 duty;
46 	u32 tcnt;
47 };
48 
49 struct berlin_pwm_chip {
50 	struct pwm_chip chip;
51 	struct clk *clk;
52 	void __iomem *base;
53 };
54 
55 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
56 {
57 	return container_of(chip, struct berlin_pwm_chip, chip);
58 }
59 
60 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
61 				   unsigned int channel, unsigned long offset)
62 {
63 	return readl_relaxed(bpc->base + channel * 0x10 + offset);
64 }
65 
66 static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
67 				     unsigned int channel, u32 value,
68 				     unsigned long offset)
69 {
70 	writel_relaxed(value, bpc->base + channel * 0x10 + offset);
71 }
72 
73 static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
74 {
75 	struct berlin_pwm_channel *channel;
76 
77 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
78 	if (!channel)
79 		return -ENOMEM;
80 
81 	return pwm_set_chip_data(pwm, channel);
82 }
83 
84 static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
85 {
86 	struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
87 
88 	kfree(channel);
89 }
90 
91 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
92 			     u64 duty_ns, u64 period_ns)
93 {
94 	struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
95 	bool prescale_4096 = false;
96 	u32 value, duty, period;
97 	u64 cycles;
98 
99 	cycles = clk_get_rate(bpc->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(bpc, pwm->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(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
122 
123 	berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
124 	berlin_pwm_writel(bpc, pwm->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,
131 				   enum pwm_polarity polarity)
132 {
133 	struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
134 	u32 value;
135 
136 	value = berlin_pwm_readl(bpc, pwm->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(bpc, pwm->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)
149 {
150 	struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
151 	u32 value;
152 
153 	value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
154 	value |= BERLIN_PWM_ENABLE;
155 	berlin_pwm_writel(bpc, pwm->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)
162 {
163 	struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
164 	u32 value;
165 
166 	value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
167 	value &= ~BERLIN_PWM_ENABLE;
168 	berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
169 }
170 
171 static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
172 			    const struct pwm_state *state)
173 {
174 	int err;
175 	bool enabled = pwm->state.enabled;
176 
177 	if (state->polarity != pwm->state.polarity) {
178 		if (enabled) {
179 			berlin_pwm_disable(chip, pwm);
180 			enabled = false;
181 		}
182 
183 		err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
184 		if (err)
185 			return err;
186 	}
187 
188 	if (!state->enabled) {
189 		if (enabled)
190 			berlin_pwm_disable(chip, pwm);
191 		return 0;
192 	}
193 
194 	err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
195 	if (err)
196 		return err;
197 
198 	if (!enabled)
199 		return berlin_pwm_enable(chip, pwm);
200 
201 	return 0;
202 }
203 
204 static const struct pwm_ops berlin_pwm_ops = {
205 	.request = berlin_pwm_request,
206 	.free = berlin_pwm_free,
207 	.apply = berlin_pwm_apply,
208 	.owner = THIS_MODULE,
209 };
210 
211 static const struct of_device_id berlin_pwm_match[] = {
212 	{ .compatible = "marvell,berlin-pwm" },
213 	{ },
214 };
215 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
216 
217 static int berlin_pwm_probe(struct platform_device *pdev)
218 {
219 	struct berlin_pwm_chip *bpc;
220 	int ret;
221 
222 	bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL);
223 	if (!bpc)
224 		return -ENOMEM;
225 
226 	bpc->base = devm_platform_ioremap_resource(pdev, 0);
227 	if (IS_ERR(bpc->base))
228 		return PTR_ERR(bpc->base);
229 
230 	bpc->clk = devm_clk_get(&pdev->dev, NULL);
231 	if (IS_ERR(bpc->clk))
232 		return PTR_ERR(bpc->clk);
233 
234 	ret = clk_prepare_enable(bpc->clk);
235 	if (ret)
236 		return ret;
237 
238 	bpc->chip.dev = &pdev->dev;
239 	bpc->chip.ops = &berlin_pwm_ops;
240 	bpc->chip.npwm = 4;
241 
242 	ret = pwmchip_add(&bpc->chip);
243 	if (ret < 0) {
244 		dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
245 		clk_disable_unprepare(bpc->clk);
246 		return ret;
247 	}
248 
249 	platform_set_drvdata(pdev, bpc);
250 
251 	return 0;
252 }
253 
254 static void berlin_pwm_remove(struct platform_device *pdev)
255 {
256 	struct berlin_pwm_chip *bpc = platform_get_drvdata(pdev);
257 
258 	pwmchip_remove(&bpc->chip);
259 
260 	clk_disable_unprepare(bpc->clk);
261 }
262 
263 #ifdef CONFIG_PM_SLEEP
264 static int berlin_pwm_suspend(struct device *dev)
265 {
266 	struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
267 	unsigned int i;
268 
269 	for (i = 0; i < bpc->chip.npwm; i++) {
270 		struct berlin_pwm_channel *channel;
271 
272 		channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
273 		if (!channel)
274 			continue;
275 
276 		channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE);
277 		channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
278 		channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
279 		channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
280 	}
281 
282 	clk_disable_unprepare(bpc->clk);
283 
284 	return 0;
285 }
286 
287 static int berlin_pwm_resume(struct device *dev)
288 {
289 	struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
290 	unsigned int i;
291 	int ret;
292 
293 	ret = clk_prepare_enable(bpc->clk);
294 	if (ret)
295 		return ret;
296 
297 	for (i = 0; i < bpc->chip.npwm; i++) {
298 		struct berlin_pwm_channel *channel;
299 
300 		channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
301 		if (!channel)
302 			continue;
303 
304 		berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
305 		berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
306 		berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
307 		berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE);
308 	}
309 
310 	return 0;
311 }
312 #endif
313 
314 static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
315 			 berlin_pwm_resume);
316 
317 static struct platform_driver berlin_pwm_driver = {
318 	.probe = berlin_pwm_probe,
319 	.remove_new = berlin_pwm_remove,
320 	.driver = {
321 		.name = "berlin-pwm",
322 		.of_match_table = berlin_pwm_match,
323 		.pm = &berlin_pwm_pm_ops,
324 	},
325 };
326 module_platform_driver(berlin_pwm_driver);
327 
328 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
329 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
330 MODULE_LICENSE("GPL v2");
331