1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for PCA9685 16-channel 12-bit PWM LED controller
5 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
6 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
8 * based on the pwm-twl-led.c driver
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>
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
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
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
50 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
51 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
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 */
57 #define PCA9685_NUMREGS 0xFF
58 #define PCA9685_MAXCHAN 0x10
60 #define LED_FULL (1 << 4)
61 #define MODE1_SLEEP (1 << 4)
62 #define MODE2_INVRT (1 << 4)
63 #define MODE2_OUTDRV (1 << 2)
65 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
66 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
67 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
68 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
72 struct regmap *regmap;
75 #if IS_ENABLED(CONFIG_GPIOLIB)
77 struct gpio_chip gpio;
78 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
82 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
84 return container_of(chip, struct pca9685, chip);
87 #if IS_ENABLED(CONFIG_GPIOLIB)
88 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
92 mutex_lock(&pca->lock);
93 if (pwm_idx >= PCA9685_MAXCHAN) {
96 * pretend already in use if any of the PWMs are requested
98 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
105 * pretend already in use if the "all LEDs" channel is requested
107 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
112 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
114 mutex_unlock(&pca->lock);
118 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
120 mutex_lock(&pca->lock);
121 clear_bit(pwm_idx, pca->pwms_inuse);
122 mutex_unlock(&pca->lock);
125 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
127 struct pca9685 *pca = gpiochip_get_data(gpio);
129 if (pca9685_pwm_test_and_set_inuse(pca, offset))
131 pm_runtime_get_sync(pca->chip.dev);
135 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
137 struct pca9685 *pca = gpiochip_get_data(gpio);
138 struct pwm_device *pwm = &pca->chip.pwms[offset];
141 regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
143 return value & LED_FULL;
146 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
149 struct pca9685 *pca = gpiochip_get_data(gpio);
150 struct pwm_device *pwm = &pca->chip.pwms[offset];
151 unsigned int on = value ? LED_FULL : 0;
153 /* Clear both OFF registers */
154 regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
155 regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
157 /* Set the full ON bit */
158 regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
161 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
163 struct pca9685 *pca = gpiochip_get_data(gpio);
165 pca9685_pwm_gpio_set(gpio, offset, 0);
166 pm_runtime_put(pca->chip.dev);
167 pca9685_pwm_clear_inuse(pca, offset);
170 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
177 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
183 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
184 unsigned int offset, int value)
186 pca9685_pwm_gpio_set(gpio, offset, value);
192 * The PCA9685 has a bit for turning the PWM output full off or on. Some
193 * boards like Intel Galileo actually uses these as normal GPIOs so we
194 * expose a GPIO chip here which can exclusively take over the underlying
197 static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
199 struct device *dev = pca->chip.dev;
201 mutex_init(&pca->lock);
203 pca->gpio.label = dev_name(dev);
204 pca->gpio.parent = dev;
205 pca->gpio.request = pca9685_pwm_gpio_request;
206 pca->gpio.free = pca9685_pwm_gpio_free;
207 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
208 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
209 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
210 pca->gpio.get = pca9685_pwm_gpio_get;
211 pca->gpio.set = pca9685_pwm_gpio_set;
213 pca->gpio.ngpio = PCA9685_MAXCHAN;
214 pca->gpio.can_sleep = true;
216 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
219 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
226 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
230 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
236 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
238 regmap_update_bits(pca->regmap, PCA9685_MODE1,
239 MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
241 /* Wait 500us for the oscillator to be back up */
246 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
247 int duty_ns, int period_ns)
249 struct pca9685 *pca = to_pca(chip);
250 unsigned long long duty;
254 if (period_ns != pca->period_ns) {
255 prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
256 PCA9685_COUNTER_RANGE * 1000) - 1;
258 if (prescale >= PCA9685_PRESCALE_MIN &&
259 prescale <= PCA9685_PRESCALE_MAX) {
261 * putting the chip briefly into SLEEP mode
262 * at this point won't interfere with the
263 * pm_runtime framework, because the pm_runtime
264 * state is guaranteed active here.
266 /* Put chip into sleep mode */
267 pca9685_set_sleep_mode(pca, true);
269 /* Change the chip-wide output frequency */
270 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
272 /* Wake the chip up */
273 pca9685_set_sleep_mode(pca, false);
275 pca->period_ns = period_ns;
278 "prescaler not set: period out of bounds!\n");
283 pca->duty_ns = duty_ns;
286 if (pwm->hwpwm >= PCA9685_MAXCHAN)
287 reg = PCA9685_ALL_LED_OFF_H;
289 reg = LED_N_OFF_H(pwm->hwpwm);
291 regmap_write(pca->regmap, reg, LED_FULL);
296 if (duty_ns == period_ns) {
297 /* Clear both OFF registers */
298 if (pwm->hwpwm >= PCA9685_MAXCHAN)
299 reg = PCA9685_ALL_LED_OFF_L;
301 reg = LED_N_OFF_L(pwm->hwpwm);
303 regmap_write(pca->regmap, reg, 0x0);
305 if (pwm->hwpwm >= PCA9685_MAXCHAN)
306 reg = PCA9685_ALL_LED_OFF_H;
308 reg = LED_N_OFF_H(pwm->hwpwm);
310 regmap_write(pca->regmap, reg, 0x0);
312 /* Set the full ON bit */
313 if (pwm->hwpwm >= PCA9685_MAXCHAN)
314 reg = PCA9685_ALL_LED_ON_H;
316 reg = LED_N_ON_H(pwm->hwpwm);
318 regmap_write(pca->regmap, reg, LED_FULL);
323 duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
324 duty = DIV_ROUND_UP_ULL(duty, period_ns);
326 if (pwm->hwpwm >= PCA9685_MAXCHAN)
327 reg = PCA9685_ALL_LED_OFF_L;
329 reg = LED_N_OFF_L(pwm->hwpwm);
331 regmap_write(pca->regmap, reg, (int)duty & 0xff);
333 if (pwm->hwpwm >= PCA9685_MAXCHAN)
334 reg = PCA9685_ALL_LED_OFF_H;
336 reg = LED_N_OFF_H(pwm->hwpwm);
338 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
340 /* Clear the full ON bit, otherwise the set OFF time has no effect */
341 if (pwm->hwpwm >= PCA9685_MAXCHAN)
342 reg = PCA9685_ALL_LED_ON_H;
344 reg = LED_N_ON_H(pwm->hwpwm);
346 regmap_write(pca->regmap, reg, 0);
351 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
353 struct pca9685 *pca = to_pca(chip);
357 * The PWM subsystem does not support a pre-delay.
358 * So, set the ON-timeout to 0
360 if (pwm->hwpwm >= PCA9685_MAXCHAN)
361 reg = PCA9685_ALL_LED_ON_L;
363 reg = LED_N_ON_L(pwm->hwpwm);
365 regmap_write(pca->regmap, reg, 0);
367 if (pwm->hwpwm >= PCA9685_MAXCHAN)
368 reg = PCA9685_ALL_LED_ON_H;
370 reg = LED_N_ON_H(pwm->hwpwm);
372 regmap_write(pca->regmap, reg, 0);
375 * Clear the full-off bit.
376 * It has precedence over the others and must be off.
378 if (pwm->hwpwm >= PCA9685_MAXCHAN)
379 reg = PCA9685_ALL_LED_OFF_H;
381 reg = LED_N_OFF_H(pwm->hwpwm);
383 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
388 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
390 struct pca9685 *pca = to_pca(chip);
393 if (pwm->hwpwm >= PCA9685_MAXCHAN)
394 reg = PCA9685_ALL_LED_OFF_H;
396 reg = LED_N_OFF_H(pwm->hwpwm);
398 regmap_write(pca->regmap, reg, LED_FULL);
400 /* Clear the LED_OFF counter. */
401 if (pwm->hwpwm >= PCA9685_MAXCHAN)
402 reg = PCA9685_ALL_LED_OFF_L;
404 reg = LED_N_OFF_L(pwm->hwpwm);
406 regmap_write(pca->regmap, reg, 0x0);
409 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
411 struct pca9685 *pca = to_pca(chip);
413 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
415 pm_runtime_get_sync(chip->dev);
420 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
422 struct pca9685 *pca = to_pca(chip);
424 pca9685_pwm_disable(chip, pwm);
425 pm_runtime_put(chip->dev);
426 pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
429 static const struct pwm_ops pca9685_pwm_ops = {
430 .enable = pca9685_pwm_enable,
431 .disable = pca9685_pwm_disable,
432 .config = pca9685_pwm_config,
433 .request = pca9685_pwm_request,
434 .free = pca9685_pwm_free,
435 .owner = THIS_MODULE,
438 static const struct regmap_config pca9685_regmap_i2c_config = {
441 .max_register = PCA9685_NUMREGS,
442 .cache_type = REGCACHE_NONE,
445 static int pca9685_pwm_probe(struct i2c_client *client,
446 const struct i2c_device_id *id)
452 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
456 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
457 if (IS_ERR(pca->regmap)) {
458 ret = PTR_ERR(pca->regmap);
459 dev_err(&client->dev, "Failed to initialize register map: %d\n",
464 pca->period_ns = PCA9685_DEFAULT_PERIOD;
466 i2c_set_clientdata(client, pca);
468 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
470 if (device_property_read_bool(&client->dev, "invert"))
471 mode2 |= MODE2_INVRT;
473 mode2 &= ~MODE2_INVRT;
475 if (device_property_read_bool(&client->dev, "open-drain"))
476 mode2 &= ~MODE2_OUTDRV;
478 mode2 |= MODE2_OUTDRV;
480 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
482 /* clear all "full off" bits */
483 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
484 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
486 pca->chip.ops = &pca9685_pwm_ops;
487 /* add an extra channel for ALL_LED */
488 pca->chip.npwm = PCA9685_MAXCHAN + 1;
490 pca->chip.dev = &client->dev;
493 ret = pwmchip_add(&pca->chip);
497 ret = pca9685_pwm_gpio_probe(pca);
499 pwmchip_remove(&pca->chip);
503 /* the chip comes out of power-up in the active state */
504 pm_runtime_set_active(&client->dev);
506 * enable will put the chip into suspend, which is what we
507 * want as all outputs are disabled at this point
509 pm_runtime_enable(&client->dev);
514 static int pca9685_pwm_remove(struct i2c_client *client)
516 struct pca9685 *pca = i2c_get_clientdata(client);
519 ret = pwmchip_remove(&pca->chip);
522 pm_runtime_disable(&client->dev);
527 static int pca9685_pwm_runtime_suspend(struct device *dev)
529 struct i2c_client *client = to_i2c_client(dev);
530 struct pca9685 *pca = i2c_get_clientdata(client);
532 pca9685_set_sleep_mode(pca, true);
536 static int pca9685_pwm_runtime_resume(struct device *dev)
538 struct i2c_client *client = to_i2c_client(dev);
539 struct pca9685 *pca = i2c_get_clientdata(client);
541 pca9685_set_sleep_mode(pca, false);
546 static const struct i2c_device_id pca9685_id[] = {
550 MODULE_DEVICE_TABLE(i2c, pca9685_id);
553 static const struct acpi_device_id pca9685_acpi_ids[] = {
557 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
561 static const struct of_device_id pca9685_dt_ids[] = {
562 { .compatible = "nxp,pca9685-pwm", },
565 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
568 static const struct dev_pm_ops pca9685_pwm_pm = {
569 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
570 pca9685_pwm_runtime_resume, NULL)
573 static struct i2c_driver pca9685_i2c_driver = {
575 .name = "pca9685-pwm",
576 .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
577 .of_match_table = of_match_ptr(pca9685_dt_ids),
578 .pm = &pca9685_pwm_pm,
580 .probe = pca9685_pwm_probe,
581 .remove = pca9685_pwm_remove,
582 .id_table = pca9685_id,
585 module_i2c_driver(pca9685_i2c_driver);
587 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
588 MODULE_DESCRIPTION("PWM driver for PCA9685");
589 MODULE_LICENSE("GPL");