xref: /linux/drivers/pwm/pwm-lpc18xx-sct.c (revision 3f0a50f345f78183f6e9b39c2f45ca5dcaa511ca)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP LPC18xx State Configurable Timer - Pulse Width Modulator driver
4  *
5  * Copyright (c) 2015 Ariel D'Alessandro <ariel@vanguardiasur.com>
6  *
7  * Notes
8  * =====
9  * NXP LPC18xx provides a State Configurable Timer (SCT) which can be configured
10  * as a Pulse Width Modulator.
11  *
12  * SCT supports 16 outputs, 16 events and 16 registers. Each event will be
13  * triggered when its related register matches the SCT counter value, and it
14  * will set or clear a selected output.
15  *
16  * One of the events is preselected to generate the period, thus the maximum
17  * number of simultaneous channels is limited to 15. Notice that period is
18  * global to all the channels, thus PWM driver will refuse setting different
19  * values to it, unless there's only one channel requested.
20  */
21 
22 #include <linux/clk.h>
23 #include <linux/err.h>
24 #include <linux/io.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pwm.h>
28 
29 /* LPC18xx SCT registers */
30 #define LPC18XX_PWM_CONFIG		0x000
31 #define LPC18XX_PWM_CONFIG_UNIFY	BIT(0)
32 #define LPC18XX_PWM_CONFIG_NORELOAD	BIT(7)
33 
34 #define LPC18XX_PWM_CTRL		0x004
35 #define LPC18XX_PWM_CTRL_HALT		BIT(2)
36 #define LPC18XX_PWM_BIDIR		BIT(4)
37 #define LPC18XX_PWM_PRE_SHIFT		5
38 #define LPC18XX_PWM_PRE_MASK		(0xff << LPC18XX_PWM_PRE_SHIFT)
39 #define LPC18XX_PWM_PRE(x)		(x << LPC18XX_PWM_PRE_SHIFT)
40 
41 #define LPC18XX_PWM_LIMIT		0x008
42 
43 #define LPC18XX_PWM_RES_BASE		0x058
44 #define LPC18XX_PWM_RES_SHIFT(_ch)	(_ch * 2)
45 #define LPC18XX_PWM_RES(_ch, _action)	(_action << LPC18XX_PWM_RES_SHIFT(_ch))
46 #define LPC18XX_PWM_RES_MASK(_ch)	(0x3 << LPC18XX_PWM_RES_SHIFT(_ch))
47 
48 #define LPC18XX_PWM_MATCH_BASE		0x100
49 #define LPC18XX_PWM_MATCH(_ch)		(LPC18XX_PWM_MATCH_BASE + _ch * 4)
50 
51 #define LPC18XX_PWM_MATCHREL_BASE	0x200
52 #define LPC18XX_PWM_MATCHREL(_ch)	(LPC18XX_PWM_MATCHREL_BASE + _ch * 4)
53 
54 #define LPC18XX_PWM_EVSTATEMSK_BASE	0x300
55 #define LPC18XX_PWM_EVSTATEMSK(_ch)	(LPC18XX_PWM_EVSTATEMSK_BASE + _ch * 8)
56 #define LPC18XX_PWM_EVSTATEMSK_ALL	0xffffffff
57 
58 #define LPC18XX_PWM_EVCTRL_BASE		0x304
59 #define LPC18XX_PWM_EVCTRL(_ev)		(LPC18XX_PWM_EVCTRL_BASE + _ev * 8)
60 
61 #define LPC18XX_PWM_EVCTRL_MATCH(_ch)	_ch
62 
63 #define LPC18XX_PWM_EVCTRL_COMB_SHIFT	12
64 #define LPC18XX_PWM_EVCTRL_COMB_MATCH	(0x1 << LPC18XX_PWM_EVCTRL_COMB_SHIFT)
65 
66 #define LPC18XX_PWM_OUTPUTSET_BASE	0x500
67 #define LPC18XX_PWM_OUTPUTSET(_ch)	(LPC18XX_PWM_OUTPUTSET_BASE + _ch * 8)
68 
69 #define LPC18XX_PWM_OUTPUTCL_BASE	0x504
70 #define LPC18XX_PWM_OUTPUTCL(_ch)	(LPC18XX_PWM_OUTPUTCL_BASE + _ch * 8)
71 
72 /* LPC18xx SCT unified counter */
73 #define LPC18XX_PWM_TIMER_MAX		0xffffffff
74 
75 /* LPC18xx SCT events */
76 #define LPC18XX_PWM_EVENT_PERIOD	0
77 #define LPC18XX_PWM_EVENT_MAX		16
78 
79 #define LPC18XX_NUM_PWMS		16
80 
81 /* SCT conflict resolution */
82 enum lpc18xx_pwm_res_action {
83 	LPC18XX_PWM_RES_NONE,
84 	LPC18XX_PWM_RES_SET,
85 	LPC18XX_PWM_RES_CLEAR,
86 	LPC18XX_PWM_RES_TOGGLE,
87 };
88 
89 struct lpc18xx_pwm_data {
90 	unsigned int duty_event;
91 };
92 
93 struct lpc18xx_pwm_chip {
94 	struct device *dev;
95 	struct pwm_chip chip;
96 	void __iomem *base;
97 	struct clk *pwm_clk;
98 	unsigned long clk_rate;
99 	unsigned int period_ns;
100 	unsigned int min_period_ns;
101 	unsigned int max_period_ns;
102 	unsigned int period_event;
103 	unsigned long event_map;
104 	struct mutex res_lock;
105 	struct mutex period_lock;
106 	struct lpc18xx_pwm_data channeldata[LPC18XX_NUM_PWMS];
107 };
108 
109 static inline struct lpc18xx_pwm_chip *
110 to_lpc18xx_pwm_chip(struct pwm_chip *chip)
111 {
112 	return container_of(chip, struct lpc18xx_pwm_chip, chip);
113 }
114 
115 static inline void lpc18xx_pwm_writel(struct lpc18xx_pwm_chip *lpc18xx_pwm,
116 				      u32 reg, u32 val)
117 {
118 	writel(val, lpc18xx_pwm->base + reg);
119 }
120 
121 static inline u32 lpc18xx_pwm_readl(struct lpc18xx_pwm_chip *lpc18xx_pwm,
122 				    u32 reg)
123 {
124 	return readl(lpc18xx_pwm->base + reg);
125 }
126 
127 static void lpc18xx_pwm_set_conflict_res(struct lpc18xx_pwm_chip *lpc18xx_pwm,
128 					 struct pwm_device *pwm,
129 					 enum lpc18xx_pwm_res_action action)
130 {
131 	u32 val;
132 
133 	mutex_lock(&lpc18xx_pwm->res_lock);
134 
135 	/*
136 	 * Simultaneous set and clear may happen on an output, that is the case
137 	 * when duty_ns == period_ns. LPC18xx SCT allows to set a conflict
138 	 * resolution action to be taken in such a case.
139 	 */
140 	val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_RES_BASE);
141 	val &= ~LPC18XX_PWM_RES_MASK(pwm->hwpwm);
142 	val |= LPC18XX_PWM_RES(pwm->hwpwm, action);
143 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_RES_BASE, val);
144 
145 	mutex_unlock(&lpc18xx_pwm->res_lock);
146 }
147 
148 static void lpc18xx_pwm_config_period(struct pwm_chip *chip, int period_ns)
149 {
150 	struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
151 	u64 val;
152 
153 	val = (u64)period_ns * lpc18xx_pwm->clk_rate;
154 	do_div(val, NSEC_PER_SEC);
155 
156 	lpc18xx_pwm_writel(lpc18xx_pwm,
157 			   LPC18XX_PWM_MATCH(lpc18xx_pwm->period_event),
158 			   (u32)val - 1);
159 
160 	lpc18xx_pwm_writel(lpc18xx_pwm,
161 			   LPC18XX_PWM_MATCHREL(lpc18xx_pwm->period_event),
162 			   (u32)val - 1);
163 }
164 
165 static void lpc18xx_pwm_config_duty(struct pwm_chip *chip,
166 				    struct pwm_device *pwm, int duty_ns)
167 {
168 	struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
169 	struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
170 	u64 val;
171 
172 	val = (u64)duty_ns * lpc18xx_pwm->clk_rate;
173 	do_div(val, NSEC_PER_SEC);
174 
175 	lpc18xx_pwm_writel(lpc18xx_pwm,
176 			   LPC18XX_PWM_MATCH(lpc18xx_data->duty_event),
177 			   (u32)val);
178 
179 	lpc18xx_pwm_writel(lpc18xx_pwm,
180 			   LPC18XX_PWM_MATCHREL(lpc18xx_data->duty_event),
181 			   (u32)val);
182 }
183 
184 static int lpc18xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
185 			      int duty_ns, int period_ns)
186 {
187 	struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
188 	int requested_events, i;
189 
190 	if (period_ns < lpc18xx_pwm->min_period_ns ||
191 	    period_ns > lpc18xx_pwm->max_period_ns) {
192 		dev_err(chip->dev, "period %d not in range\n", period_ns);
193 		return -ERANGE;
194 	}
195 
196 	mutex_lock(&lpc18xx_pwm->period_lock);
197 
198 	requested_events = bitmap_weight(&lpc18xx_pwm->event_map,
199 					 LPC18XX_PWM_EVENT_MAX);
200 
201 	/*
202 	 * The PWM supports only a single period for all PWM channels.
203 	 * Once the period is set, it can only be changed if no more than one
204 	 * channel is requested at that moment.
205 	 */
206 	if (requested_events > 2 && lpc18xx_pwm->period_ns != period_ns &&
207 	    lpc18xx_pwm->period_ns) {
208 		dev_err(chip->dev, "conflicting period requested for PWM %u\n",
209 			pwm->hwpwm);
210 		mutex_unlock(&lpc18xx_pwm->period_lock);
211 		return -EBUSY;
212 	}
213 
214 	if ((requested_events <= 2 && lpc18xx_pwm->period_ns != period_ns) ||
215 	    !lpc18xx_pwm->period_ns) {
216 		lpc18xx_pwm->period_ns = period_ns;
217 		for (i = 0; i < chip->npwm; i++)
218 			pwm_set_period(&chip->pwms[i], period_ns);
219 		lpc18xx_pwm_config_period(chip, period_ns);
220 	}
221 
222 	mutex_unlock(&lpc18xx_pwm->period_lock);
223 
224 	lpc18xx_pwm_config_duty(chip, pwm, duty_ns);
225 
226 	return 0;
227 }
228 
229 static int lpc18xx_pwm_set_polarity(struct pwm_chip *chip,
230 				    struct pwm_device *pwm,
231 				    enum pwm_polarity polarity)
232 {
233 	return 0;
234 }
235 
236 static int lpc18xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
237 {
238 	struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
239 	struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
240 	enum lpc18xx_pwm_res_action res_action;
241 	unsigned int set_event, clear_event;
242 
243 	lpc18xx_pwm_writel(lpc18xx_pwm,
244 			   LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event),
245 			   LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_data->duty_event) |
246 			   LPC18XX_PWM_EVCTRL_COMB_MATCH);
247 
248 	lpc18xx_pwm_writel(lpc18xx_pwm,
249 			   LPC18XX_PWM_EVSTATEMSK(lpc18xx_data->duty_event),
250 			   LPC18XX_PWM_EVSTATEMSK_ALL);
251 
252 	if (pwm_get_polarity(pwm) == PWM_POLARITY_NORMAL) {
253 		set_event = lpc18xx_pwm->period_event;
254 		clear_event = lpc18xx_data->duty_event;
255 		res_action = LPC18XX_PWM_RES_SET;
256 	} else {
257 		set_event = lpc18xx_data->duty_event;
258 		clear_event = lpc18xx_pwm->period_event;
259 		res_action = LPC18XX_PWM_RES_CLEAR;
260 	}
261 
262 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm),
263 			   BIT(set_event));
264 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm),
265 			   BIT(clear_event));
266 	lpc18xx_pwm_set_conflict_res(lpc18xx_pwm, pwm, res_action);
267 
268 	return 0;
269 }
270 
271 static void lpc18xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
272 {
273 	struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
274 	struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
275 
276 	lpc18xx_pwm_writel(lpc18xx_pwm,
277 			   LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event), 0);
278 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm), 0);
279 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm), 0);
280 }
281 
282 static int lpc18xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
283 {
284 	struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
285 	struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
286 	unsigned long event;
287 
288 	event = find_first_zero_bit(&lpc18xx_pwm->event_map,
289 				    LPC18XX_PWM_EVENT_MAX);
290 
291 	if (event >= LPC18XX_PWM_EVENT_MAX) {
292 		dev_err(lpc18xx_pwm->dev,
293 			"maximum number of simultaneous channels reached\n");
294 		return -EBUSY;
295 	}
296 
297 	set_bit(event, &lpc18xx_pwm->event_map);
298 	lpc18xx_data->duty_event = event;
299 
300 	return 0;
301 }
302 
303 static void lpc18xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
304 {
305 	struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
306 	struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
307 
308 	clear_bit(lpc18xx_data->duty_event, &lpc18xx_pwm->event_map);
309 }
310 
311 static const struct pwm_ops lpc18xx_pwm_ops = {
312 	.config = lpc18xx_pwm_config,
313 	.set_polarity = lpc18xx_pwm_set_polarity,
314 	.enable = lpc18xx_pwm_enable,
315 	.disable = lpc18xx_pwm_disable,
316 	.request = lpc18xx_pwm_request,
317 	.free = lpc18xx_pwm_free,
318 	.owner = THIS_MODULE,
319 };
320 
321 static const struct of_device_id lpc18xx_pwm_of_match[] = {
322 	{ .compatible = "nxp,lpc1850-sct-pwm" },
323 	{}
324 };
325 MODULE_DEVICE_TABLE(of, lpc18xx_pwm_of_match);
326 
327 static int lpc18xx_pwm_probe(struct platform_device *pdev)
328 {
329 	struct lpc18xx_pwm_chip *lpc18xx_pwm;
330 	int ret;
331 	u64 val;
332 
333 	lpc18xx_pwm = devm_kzalloc(&pdev->dev, sizeof(*lpc18xx_pwm),
334 				   GFP_KERNEL);
335 	if (!lpc18xx_pwm)
336 		return -ENOMEM;
337 
338 	lpc18xx_pwm->dev = &pdev->dev;
339 
340 	lpc18xx_pwm->base = devm_platform_ioremap_resource(pdev, 0);
341 	if (IS_ERR(lpc18xx_pwm->base))
342 		return PTR_ERR(lpc18xx_pwm->base);
343 
344 	lpc18xx_pwm->pwm_clk = devm_clk_get(&pdev->dev, "pwm");
345 	if (IS_ERR(lpc18xx_pwm->pwm_clk)) {
346 		dev_err(&pdev->dev, "failed to get pwm clock\n");
347 		return PTR_ERR(lpc18xx_pwm->pwm_clk);
348 	}
349 
350 	ret = clk_prepare_enable(lpc18xx_pwm->pwm_clk);
351 	if (ret < 0) {
352 		dev_err(&pdev->dev, "could not prepare or enable pwm clock\n");
353 		return ret;
354 	}
355 
356 	lpc18xx_pwm->clk_rate = clk_get_rate(lpc18xx_pwm->pwm_clk);
357 	if (!lpc18xx_pwm->clk_rate) {
358 		dev_err(&pdev->dev, "pwm clock has no frequency\n");
359 		ret = -EINVAL;
360 		goto disable_pwmclk;
361 	}
362 
363 	mutex_init(&lpc18xx_pwm->res_lock);
364 	mutex_init(&lpc18xx_pwm->period_lock);
365 
366 	val = (u64)NSEC_PER_SEC * LPC18XX_PWM_TIMER_MAX;
367 	do_div(val, lpc18xx_pwm->clk_rate);
368 	lpc18xx_pwm->max_period_ns = val;
369 
370 	lpc18xx_pwm->min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC,
371 						  lpc18xx_pwm->clk_rate);
372 
373 	lpc18xx_pwm->chip.dev = &pdev->dev;
374 	lpc18xx_pwm->chip.ops = &lpc18xx_pwm_ops;
375 	lpc18xx_pwm->chip.npwm = LPC18XX_NUM_PWMS;
376 
377 	/* SCT counter must be in unify (32 bit) mode */
378 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CONFIG,
379 			   LPC18XX_PWM_CONFIG_UNIFY);
380 
381 	/*
382 	 * Everytime the timer counter reaches the period value, the related
383 	 * event will be triggered and the counter reset to 0.
384 	 */
385 	set_bit(LPC18XX_PWM_EVENT_PERIOD, &lpc18xx_pwm->event_map);
386 	lpc18xx_pwm->period_event = LPC18XX_PWM_EVENT_PERIOD;
387 
388 	lpc18xx_pwm_writel(lpc18xx_pwm,
389 			   LPC18XX_PWM_EVSTATEMSK(lpc18xx_pwm->period_event),
390 			   LPC18XX_PWM_EVSTATEMSK_ALL);
391 
392 	val = LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_pwm->period_event) |
393 	      LPC18XX_PWM_EVCTRL_COMB_MATCH;
394 	lpc18xx_pwm_writel(lpc18xx_pwm,
395 			   LPC18XX_PWM_EVCTRL(lpc18xx_pwm->period_event), val);
396 
397 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_LIMIT,
398 			   BIT(lpc18xx_pwm->period_event));
399 
400 	val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
401 	val &= ~LPC18XX_PWM_BIDIR;
402 	val &= ~LPC18XX_PWM_CTRL_HALT;
403 	val &= ~LPC18XX_PWM_PRE_MASK;
404 	val |= LPC18XX_PWM_PRE(0);
405 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL, val);
406 
407 	ret = pwmchip_add(&lpc18xx_pwm->chip);
408 	if (ret < 0) {
409 		dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
410 		goto disable_pwmclk;
411 	}
412 
413 	platform_set_drvdata(pdev, lpc18xx_pwm);
414 
415 	return 0;
416 
417 disable_pwmclk:
418 	clk_disable_unprepare(lpc18xx_pwm->pwm_clk);
419 	return ret;
420 }
421 
422 static int lpc18xx_pwm_remove(struct platform_device *pdev)
423 {
424 	struct lpc18xx_pwm_chip *lpc18xx_pwm = platform_get_drvdata(pdev);
425 	u32 val;
426 
427 	pwmchip_remove(&lpc18xx_pwm->chip);
428 
429 	val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
430 	lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL,
431 			   val | LPC18XX_PWM_CTRL_HALT);
432 
433 	clk_disable_unprepare(lpc18xx_pwm->pwm_clk);
434 
435 	return 0;
436 }
437 
438 static struct platform_driver lpc18xx_pwm_driver = {
439 	.driver = {
440 		.name = "lpc18xx-sct-pwm",
441 		.of_match_table = lpc18xx_pwm_of_match,
442 	},
443 	.probe = lpc18xx_pwm_probe,
444 	.remove = lpc18xx_pwm_remove,
445 };
446 module_platform_driver(lpc18xx_pwm_driver);
447 
448 MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
449 MODULE_DESCRIPTION("NXP LPC18xx PWM driver");
450 MODULE_LICENSE("GPL v2");
451