xref: /openbmc/linux/drivers/pwm/pwm-pca9685.c (revision abcda807)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for PCA9685 16-channel 12-bit PWM LED controller
4  *
5  * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
6  * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
7  *
8  * based on the pwm-twl-led.c driver
9  */
10 
11 #include <linux/acpi.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/pwm.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/bitmap.h>
24 
25 /*
26  * Because the PCA9685 has only one prescaler per chip, changing the period of
27  * one channel affects the period of all 16 PWM outputs!
28  * However, the ratio between each configured duty cycle and the chip-wide
29  * period remains constant, because the OFF time is set in proportion to the
30  * counter range.
31  */
32 
33 #define PCA9685_MODE1		0x00
34 #define PCA9685_MODE2		0x01
35 #define PCA9685_SUBADDR1	0x02
36 #define PCA9685_SUBADDR2	0x03
37 #define PCA9685_SUBADDR3	0x04
38 #define PCA9685_ALLCALLADDR	0x05
39 #define PCA9685_LEDX_ON_L	0x06
40 #define PCA9685_LEDX_ON_H	0x07
41 #define PCA9685_LEDX_OFF_L	0x08
42 #define PCA9685_LEDX_OFF_H	0x09
43 
44 #define PCA9685_ALL_LED_ON_L	0xFA
45 #define PCA9685_ALL_LED_ON_H	0xFB
46 #define PCA9685_ALL_LED_OFF_L	0xFC
47 #define PCA9685_ALL_LED_OFF_H	0xFD
48 #define PCA9685_PRESCALE	0xFE
49 
50 #define PCA9685_PRESCALE_MIN	0x03	/* => max. frequency of 1526 Hz */
51 #define PCA9685_PRESCALE_MAX	0xFF	/* => min. frequency of 24 Hz */
52 
53 #define PCA9685_COUNTER_RANGE	4096
54 #define PCA9685_DEFAULT_PERIOD	5000000	/* Default period_ns = 1/200 Hz */
55 #define PCA9685_OSC_CLOCK_MHZ	25	/* Internal oscillator with 25 MHz */
56 
57 #define PCA9685_NUMREGS		0xFF
58 #define PCA9685_MAXCHAN		0x10
59 
60 #define LED_FULL		BIT(4)
61 #define MODE1_ALLCALL		BIT(0)
62 #define MODE1_SUB3		BIT(1)
63 #define MODE1_SUB2		BIT(2)
64 #define MODE1_SUB1		BIT(3)
65 #define MODE1_SLEEP		BIT(4)
66 #define MODE2_INVRT		BIT(4)
67 #define MODE2_OUTDRV		BIT(2)
68 
69 #define LED_N_ON_H(N)	(PCA9685_LEDX_ON_H + (4 * (N)))
70 #define LED_N_ON_L(N)	(PCA9685_LEDX_ON_L + (4 * (N)))
71 #define LED_N_OFF_H(N)	(PCA9685_LEDX_OFF_H + (4 * (N)))
72 #define LED_N_OFF_L(N)	(PCA9685_LEDX_OFF_L + (4 * (N)))
73 
74 struct pca9685 {
75 	struct pwm_chip chip;
76 	struct regmap *regmap;
77 	int period_ns;
78 #if IS_ENABLED(CONFIG_GPIOLIB)
79 	struct mutex lock;
80 	struct gpio_chip gpio;
81 	DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
82 #endif
83 };
84 
85 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
86 {
87 	return container_of(chip, struct pca9685, chip);
88 }
89 
90 #if IS_ENABLED(CONFIG_GPIOLIB)
91 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
92 {
93 	bool is_inuse;
94 
95 	mutex_lock(&pca->lock);
96 	if (pwm_idx >= PCA9685_MAXCHAN) {
97 		/*
98 		 * "All LEDs" channel:
99 		 * pretend already in use if any of the PWMs are requested
100 		 */
101 		if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
102 			is_inuse = true;
103 			goto out;
104 		}
105 	} else {
106 		/*
107 		 * Regular channel:
108 		 * pretend already in use if the "all LEDs" channel is requested
109 		 */
110 		if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
111 			is_inuse = true;
112 			goto out;
113 		}
114 	}
115 	is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
116 out:
117 	mutex_unlock(&pca->lock);
118 	return is_inuse;
119 }
120 
121 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
122 {
123 	mutex_lock(&pca->lock);
124 	clear_bit(pwm_idx, pca->pwms_inuse);
125 	mutex_unlock(&pca->lock);
126 }
127 
128 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
129 {
130 	struct pca9685 *pca = gpiochip_get_data(gpio);
131 
132 	if (pca9685_pwm_test_and_set_inuse(pca, offset))
133 		return -EBUSY;
134 	pm_runtime_get_sync(pca->chip.dev);
135 	return 0;
136 }
137 
138 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
139 {
140 	struct pca9685 *pca = gpiochip_get_data(gpio);
141 	struct pwm_device *pwm = &pca->chip.pwms[offset];
142 	unsigned int value;
143 
144 	regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
145 
146 	return value & LED_FULL;
147 }
148 
149 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
150 				 int value)
151 {
152 	struct pca9685 *pca = gpiochip_get_data(gpio);
153 	struct pwm_device *pwm = &pca->chip.pwms[offset];
154 	unsigned int on = value ? LED_FULL : 0;
155 
156 	/* Clear both OFF registers */
157 	regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
158 	regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
159 
160 	/* Set the full ON bit */
161 	regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
162 }
163 
164 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
165 {
166 	struct pca9685 *pca = gpiochip_get_data(gpio);
167 
168 	pca9685_pwm_gpio_set(gpio, offset, 0);
169 	pm_runtime_put(pca->chip.dev);
170 	pca9685_pwm_clear_inuse(pca, offset);
171 }
172 
173 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
174 					  unsigned int offset)
175 {
176 	/* Always out */
177 	return GPIO_LINE_DIRECTION_OUT;
178 }
179 
180 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
181 					    unsigned int offset)
182 {
183 	return -EINVAL;
184 }
185 
186 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
187 					     unsigned int offset, int value)
188 {
189 	pca9685_pwm_gpio_set(gpio, offset, value);
190 
191 	return 0;
192 }
193 
194 /*
195  * The PCA9685 has a bit for turning the PWM output full off or on. Some
196  * boards like Intel Galileo actually uses these as normal GPIOs so we
197  * expose a GPIO chip here which can exclusively take over the underlying
198  * PWM channel.
199  */
200 static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
201 {
202 	struct device *dev = pca->chip.dev;
203 
204 	mutex_init(&pca->lock);
205 
206 	pca->gpio.label = dev_name(dev);
207 	pca->gpio.parent = dev;
208 	pca->gpio.request = pca9685_pwm_gpio_request;
209 	pca->gpio.free = pca9685_pwm_gpio_free;
210 	pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
211 	pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
212 	pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
213 	pca->gpio.get = pca9685_pwm_gpio_get;
214 	pca->gpio.set = pca9685_pwm_gpio_set;
215 	pca->gpio.base = -1;
216 	pca->gpio.ngpio = PCA9685_MAXCHAN;
217 	pca->gpio.can_sleep = true;
218 
219 	return devm_gpiochip_add_data(dev, &pca->gpio, pca);
220 }
221 #else
222 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
223 						  int pwm_idx)
224 {
225 	return false;
226 }
227 
228 static inline void
229 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
230 {
231 }
232 
233 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
234 {
235 	return 0;
236 }
237 #endif
238 
239 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
240 {
241 	regmap_update_bits(pca->regmap, PCA9685_MODE1,
242 			   MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
243 	if (!enable) {
244 		/* Wait 500us for the oscillator to be back up */
245 		udelay(500);
246 	}
247 }
248 
249 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
250 			      int duty_ns, int period_ns)
251 {
252 	struct pca9685 *pca = to_pca(chip);
253 	unsigned long long duty;
254 	unsigned int reg;
255 	int prescale;
256 
257 	if (period_ns != pca->period_ns) {
258 		prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
259 					     PCA9685_COUNTER_RANGE * 1000) - 1;
260 
261 		if (prescale >= PCA9685_PRESCALE_MIN &&
262 			prescale <= PCA9685_PRESCALE_MAX) {
263 			/*
264 			 * Putting the chip briefly into SLEEP mode
265 			 * at this point won't interfere with the
266 			 * pm_runtime framework, because the pm_runtime
267 			 * state is guaranteed active here.
268 			 */
269 			/* Put chip into sleep mode */
270 			pca9685_set_sleep_mode(pca, true);
271 
272 			/* Change the chip-wide output frequency */
273 			regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
274 
275 			/* Wake the chip up */
276 			pca9685_set_sleep_mode(pca, false);
277 
278 			pca->period_ns = period_ns;
279 		} else {
280 			dev_err(chip->dev,
281 				"prescaler not set: period out of bounds!\n");
282 			return -EINVAL;
283 		}
284 	}
285 
286 	if (duty_ns < 1) {
287 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
288 			reg = PCA9685_ALL_LED_OFF_H;
289 		else
290 			reg = LED_N_OFF_H(pwm->hwpwm);
291 
292 		regmap_write(pca->regmap, reg, LED_FULL);
293 
294 		return 0;
295 	}
296 
297 	if (duty_ns == period_ns) {
298 		/* Clear both OFF registers */
299 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
300 			reg = PCA9685_ALL_LED_OFF_L;
301 		else
302 			reg = LED_N_OFF_L(pwm->hwpwm);
303 
304 		regmap_write(pca->regmap, reg, 0x0);
305 
306 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
307 			reg = PCA9685_ALL_LED_OFF_H;
308 		else
309 			reg = LED_N_OFF_H(pwm->hwpwm);
310 
311 		regmap_write(pca->regmap, reg, 0x0);
312 
313 		/* Set the full ON bit */
314 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
315 			reg = PCA9685_ALL_LED_ON_H;
316 		else
317 			reg = LED_N_ON_H(pwm->hwpwm);
318 
319 		regmap_write(pca->regmap, reg, LED_FULL);
320 
321 		return 0;
322 	}
323 
324 	duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
325 	duty = DIV_ROUND_UP_ULL(duty, period_ns);
326 
327 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
328 		reg = PCA9685_ALL_LED_OFF_L;
329 	else
330 		reg = LED_N_OFF_L(pwm->hwpwm);
331 
332 	regmap_write(pca->regmap, reg, (int)duty & 0xff);
333 
334 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
335 		reg = PCA9685_ALL_LED_OFF_H;
336 	else
337 		reg = LED_N_OFF_H(pwm->hwpwm);
338 
339 	regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
340 
341 	/* Clear the full ON bit, otherwise the set OFF time has no effect */
342 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
343 		reg = PCA9685_ALL_LED_ON_H;
344 	else
345 		reg = LED_N_ON_H(pwm->hwpwm);
346 
347 	regmap_write(pca->regmap, reg, 0);
348 
349 	return 0;
350 }
351 
352 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
353 {
354 	struct pca9685 *pca = to_pca(chip);
355 	unsigned int reg;
356 
357 	/*
358 	 * The PWM subsystem does not support a pre-delay.
359 	 * So, set the ON-timeout to 0
360 	 */
361 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
362 		reg = PCA9685_ALL_LED_ON_L;
363 	else
364 		reg = LED_N_ON_L(pwm->hwpwm);
365 
366 	regmap_write(pca->regmap, reg, 0);
367 
368 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
369 		reg = PCA9685_ALL_LED_ON_H;
370 	else
371 		reg = LED_N_ON_H(pwm->hwpwm);
372 
373 	regmap_write(pca->regmap, reg, 0);
374 
375 	/*
376 	 * Clear the full-off bit.
377 	 * It has precedence over the others and must be off.
378 	 */
379 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
380 		reg = PCA9685_ALL_LED_OFF_H;
381 	else
382 		reg = LED_N_OFF_H(pwm->hwpwm);
383 
384 	regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
385 
386 	return 0;
387 }
388 
389 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
390 {
391 	struct pca9685 *pca = to_pca(chip);
392 	unsigned int reg;
393 
394 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
395 		reg = PCA9685_ALL_LED_OFF_H;
396 	else
397 		reg = LED_N_OFF_H(pwm->hwpwm);
398 
399 	regmap_write(pca->regmap, reg, LED_FULL);
400 
401 	/* Clear the LED_OFF counter. */
402 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
403 		reg = PCA9685_ALL_LED_OFF_L;
404 	else
405 		reg = LED_N_OFF_L(pwm->hwpwm);
406 
407 	regmap_write(pca->regmap, reg, 0x0);
408 }
409 
410 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
411 {
412 	struct pca9685 *pca = to_pca(chip);
413 
414 	if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
415 		return -EBUSY;
416 	pm_runtime_get_sync(chip->dev);
417 
418 	return 0;
419 }
420 
421 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
422 {
423 	struct pca9685 *pca = to_pca(chip);
424 
425 	pca9685_pwm_disable(chip, pwm);
426 	pm_runtime_put(chip->dev);
427 	pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
428 }
429 
430 static const struct pwm_ops pca9685_pwm_ops = {
431 	.enable = pca9685_pwm_enable,
432 	.disable = pca9685_pwm_disable,
433 	.config = pca9685_pwm_config,
434 	.request = pca9685_pwm_request,
435 	.free = pca9685_pwm_free,
436 	.owner = THIS_MODULE,
437 };
438 
439 static const struct regmap_config pca9685_regmap_i2c_config = {
440 	.reg_bits = 8,
441 	.val_bits = 8,
442 	.max_register = PCA9685_NUMREGS,
443 	.cache_type = REGCACHE_NONE,
444 };
445 
446 static int pca9685_pwm_probe(struct i2c_client *client,
447 				const struct i2c_device_id *id)
448 {
449 	struct pca9685 *pca;
450 	unsigned int reg;
451 	int ret;
452 
453 	pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
454 	if (!pca)
455 		return -ENOMEM;
456 
457 	pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
458 	if (IS_ERR(pca->regmap)) {
459 		ret = PTR_ERR(pca->regmap);
460 		dev_err(&client->dev, "Failed to initialize register map: %d\n",
461 			ret);
462 		return ret;
463 	}
464 	pca->period_ns = PCA9685_DEFAULT_PERIOD;
465 
466 	i2c_set_clientdata(client, pca);
467 
468 	regmap_read(pca->regmap, PCA9685_MODE2, &reg);
469 
470 	if (device_property_read_bool(&client->dev, "invert"))
471 		reg |= MODE2_INVRT;
472 	else
473 		reg &= ~MODE2_INVRT;
474 
475 	if (device_property_read_bool(&client->dev, "open-drain"))
476 		reg &= ~MODE2_OUTDRV;
477 	else
478 		reg |= MODE2_OUTDRV;
479 
480 	regmap_write(pca->regmap, PCA9685_MODE2, reg);
481 
482 	/* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
483 	regmap_read(pca->regmap, PCA9685_MODE1, &reg);
484 	reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
485 	regmap_write(pca->regmap, PCA9685_MODE1, reg);
486 
487 	/* Clear all "full off" bits */
488 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
489 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
490 
491 	pca->chip.ops = &pca9685_pwm_ops;
492 	/* Add an extra channel for ALL_LED */
493 	pca->chip.npwm = PCA9685_MAXCHAN + 1;
494 
495 	pca->chip.dev = &client->dev;
496 	pca->chip.base = -1;
497 
498 	ret = pwmchip_add(&pca->chip);
499 	if (ret < 0)
500 		return ret;
501 
502 	ret = pca9685_pwm_gpio_probe(pca);
503 	if (ret < 0) {
504 		pwmchip_remove(&pca->chip);
505 		return ret;
506 	}
507 
508 	/* The chip comes out of power-up in the active state */
509 	pm_runtime_set_active(&client->dev);
510 	/*
511 	 * Enable will put the chip into suspend, which is what we
512 	 * want as all outputs are disabled at this point
513 	 */
514 	pm_runtime_enable(&client->dev);
515 
516 	return 0;
517 }
518 
519 static int pca9685_pwm_remove(struct i2c_client *client)
520 {
521 	struct pca9685 *pca = i2c_get_clientdata(client);
522 	int ret;
523 
524 	ret = pwmchip_remove(&pca->chip);
525 	if (ret)
526 		return ret;
527 	pm_runtime_disable(&client->dev);
528 	return 0;
529 }
530 
531 static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
532 {
533 	struct i2c_client *client = to_i2c_client(dev);
534 	struct pca9685 *pca = i2c_get_clientdata(client);
535 
536 	pca9685_set_sleep_mode(pca, true);
537 	return 0;
538 }
539 
540 static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
541 {
542 	struct i2c_client *client = to_i2c_client(dev);
543 	struct pca9685 *pca = i2c_get_clientdata(client);
544 
545 	pca9685_set_sleep_mode(pca, false);
546 	return 0;
547 }
548 
549 static const struct i2c_device_id pca9685_id[] = {
550 	{ "pca9685", 0 },
551 	{ /* sentinel */ },
552 };
553 MODULE_DEVICE_TABLE(i2c, pca9685_id);
554 
555 #ifdef CONFIG_ACPI
556 static const struct acpi_device_id pca9685_acpi_ids[] = {
557 	{ "INT3492", 0 },
558 	{ /* sentinel */ },
559 };
560 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
561 #endif
562 
563 #ifdef CONFIG_OF
564 static const struct of_device_id pca9685_dt_ids[] = {
565 	{ .compatible = "nxp,pca9685-pwm", },
566 	{ /* sentinel */ }
567 };
568 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
569 #endif
570 
571 static const struct dev_pm_ops pca9685_pwm_pm = {
572 	SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
573 			   pca9685_pwm_runtime_resume, NULL)
574 };
575 
576 static struct i2c_driver pca9685_i2c_driver = {
577 	.driver = {
578 		.name = "pca9685-pwm",
579 		.acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
580 		.of_match_table = of_match_ptr(pca9685_dt_ids),
581 		.pm = &pca9685_pwm_pm,
582 	},
583 	.probe = pca9685_pwm_probe,
584 	.remove = pca9685_pwm_remove,
585 	.id_table = pca9685_id,
586 };
587 
588 module_i2c_driver(pca9685_i2c_driver);
589 
590 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
591 MODULE_DESCRIPTION("PWM driver for PCA9685");
592 MODULE_LICENSE("GPL");
593