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_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
56 #define PCA9685_NUMREGS 0xFF
57 #define PCA9685_MAXCHAN 0x10
59 #define LED_FULL BIT(4)
60 #define MODE1_ALLCALL BIT(0)
61 #define MODE1_SUB3 BIT(1)
62 #define MODE1_SUB2 BIT(2)
63 #define MODE1_SUB1 BIT(3)
64 #define MODE1_SLEEP BIT(4)
65 #define MODE2_INVRT BIT(4)
66 #define MODE2_OUTDRV BIT(2)
68 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
69 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
70 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
71 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
73 #define REG_ON_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_H : LED_N_ON_H((C)))
74 #define REG_ON_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_L : LED_N_ON_L((C)))
75 #define REG_OFF_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_H : LED_N_OFF_H((C)))
76 #define REG_OFF_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_L : LED_N_OFF_L((C)))
80 struct regmap *regmap;
81 #if IS_ENABLED(CONFIG_GPIOLIB)
83 struct gpio_chip gpio;
84 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
88 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
90 return container_of(chip, struct pca9685, chip);
93 /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */
94 static void pca9685_pwm_set_duty(struct pca9685 *pca, int channel, unsigned int duty)
97 /* Set the full OFF bit, which has the highest precedence */
98 regmap_write(pca->regmap, REG_OFF_H(channel), LED_FULL);
99 } else if (duty >= PCA9685_COUNTER_RANGE) {
100 /* Set the full ON bit and clear the full OFF bit */
101 regmap_write(pca->regmap, REG_ON_H(channel), LED_FULL);
102 regmap_write(pca->regmap, REG_OFF_H(channel), 0);
104 /* Set OFF time (clears the full OFF bit) */
105 regmap_write(pca->regmap, REG_OFF_L(channel), duty & 0xff);
106 regmap_write(pca->regmap, REG_OFF_H(channel), (duty >> 8) & 0xf);
107 /* Clear the full ON bit */
108 regmap_write(pca->regmap, REG_ON_H(channel), 0);
112 static unsigned int pca9685_pwm_get_duty(struct pca9685 *pca, int channel)
114 unsigned int off_h = 0, val = 0;
116 if (WARN_ON(channel >= PCA9685_MAXCHAN)) {
117 /* HW does not support reading state of "all LEDs" channel */
121 regmap_read(pca->regmap, LED_N_OFF_H(channel), &off_h);
122 if (off_h & LED_FULL) {
123 /* Full OFF bit is set */
127 regmap_read(pca->regmap, LED_N_ON_H(channel), &val);
128 if (val & LED_FULL) {
129 /* Full ON bit is set */
130 return PCA9685_COUNTER_RANGE;
133 if (regmap_read(pca->regmap, LED_N_OFF_L(channel), &val)) {
134 /* Reset val to 0 in case reading LED_N_OFF_L failed */
137 return ((off_h & 0xf) << 8) | (val & 0xff);
140 #if IS_ENABLED(CONFIG_GPIOLIB)
141 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
145 mutex_lock(&pca->lock);
146 if (pwm_idx >= PCA9685_MAXCHAN) {
148 * "All LEDs" channel:
149 * pretend already in use if any of the PWMs are requested
151 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
158 * pretend already in use if the "all LEDs" channel is requested
160 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
165 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
167 mutex_unlock(&pca->lock);
171 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
173 mutex_lock(&pca->lock);
174 clear_bit(pwm_idx, pca->pwms_inuse);
175 mutex_unlock(&pca->lock);
178 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
180 struct pca9685 *pca = gpiochip_get_data(gpio);
182 if (pca9685_pwm_test_and_set_inuse(pca, offset))
184 pm_runtime_get_sync(pca->chip.dev);
188 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
190 struct pca9685 *pca = gpiochip_get_data(gpio);
192 return pca9685_pwm_get_duty(pca, offset) != 0;
195 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
198 struct pca9685 *pca = gpiochip_get_data(gpio);
200 pca9685_pwm_set_duty(pca, offset, value ? PCA9685_COUNTER_RANGE : 0);
203 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
205 struct pca9685 *pca = gpiochip_get_data(gpio);
207 pca9685_pwm_set_duty(pca, offset, 0);
208 pm_runtime_put(pca->chip.dev);
209 pca9685_pwm_clear_inuse(pca, offset);
212 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
216 return GPIO_LINE_DIRECTION_OUT;
219 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
225 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
226 unsigned int offset, int value)
228 pca9685_pwm_gpio_set(gpio, offset, value);
234 * The PCA9685 has a bit for turning the PWM output full off or on. Some
235 * boards like Intel Galileo actually uses these as normal GPIOs so we
236 * expose a GPIO chip here which can exclusively take over the underlying
239 static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
241 struct device *dev = pca->chip.dev;
243 mutex_init(&pca->lock);
245 pca->gpio.label = dev_name(dev);
246 pca->gpio.parent = dev;
247 pca->gpio.request = pca9685_pwm_gpio_request;
248 pca->gpio.free = pca9685_pwm_gpio_free;
249 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
250 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
251 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
252 pca->gpio.get = pca9685_pwm_gpio_get;
253 pca->gpio.set = pca9685_pwm_gpio_set;
255 pca->gpio.ngpio = PCA9685_MAXCHAN;
256 pca->gpio.can_sleep = true;
258 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
261 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
268 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
272 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
278 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
280 regmap_update_bits(pca->regmap, PCA9685_MODE1,
281 MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
283 /* Wait 500us for the oscillator to be back up */
288 static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
289 const struct pwm_state *state)
291 struct pca9685 *pca = to_pca(chip);
292 unsigned long long duty, prescale;
293 unsigned int val = 0;
295 if (state->polarity != PWM_POLARITY_NORMAL)
298 prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period,
299 PCA9685_COUNTER_RANGE * 1000) - 1;
300 if (prescale < PCA9685_PRESCALE_MIN || prescale > PCA9685_PRESCALE_MAX) {
301 dev_err(chip->dev, "pwm not changed: period out of bounds!\n");
305 if (!state->enabled) {
306 pca9685_pwm_set_duty(pca, pwm->hwpwm, 0);
310 regmap_read(pca->regmap, PCA9685_PRESCALE, &val);
311 if (prescale != val) {
313 * Putting the chip briefly into SLEEP mode
314 * at this point won't interfere with the
315 * pm_runtime framework, because the pm_runtime
316 * state is guaranteed active here.
318 /* Put chip into sleep mode */
319 pca9685_set_sleep_mode(pca, true);
321 /* Change the chip-wide output frequency */
322 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
324 /* Wake the chip up */
325 pca9685_set_sleep_mode(pca, false);
328 duty = PCA9685_COUNTER_RANGE * state->duty_cycle;
329 duty = DIV_ROUND_UP_ULL(duty, state->period);
330 pca9685_pwm_set_duty(pca, pwm->hwpwm, duty);
334 static void pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
335 struct pwm_state *state)
337 struct pca9685 *pca = to_pca(chip);
338 unsigned long long duty;
339 unsigned int val = 0;
341 /* Calculate (chip-wide) period from prescale value */
342 regmap_read(pca->regmap, PCA9685_PRESCALE, &val);
344 * PCA9685_OSC_CLOCK_MHZ is 25, i.e. an integer divider of 1000.
345 * The following calculation is therefore only a multiplication
346 * and we are not losing precision.
348 state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) *
351 /* The (per-channel) polarity is fixed */
352 state->polarity = PWM_POLARITY_NORMAL;
354 if (pwm->hwpwm >= PCA9685_MAXCHAN) {
356 * The "all LEDs" channel does not support HW readout
357 * Return 0 and disabled for backwards compatibility
359 state->duty_cycle = 0;
360 state->enabled = false;
364 state->enabled = true;
365 duty = pca9685_pwm_get_duty(pca, pwm->hwpwm);
366 state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE);
369 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
371 struct pca9685 *pca = to_pca(chip);
373 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
375 pm_runtime_get_sync(chip->dev);
380 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
382 struct pca9685 *pca = to_pca(chip);
384 pca9685_pwm_set_duty(pca, pwm->hwpwm, 0);
385 pm_runtime_put(chip->dev);
386 pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
389 static const struct pwm_ops pca9685_pwm_ops = {
390 .apply = pca9685_pwm_apply,
391 .get_state = pca9685_pwm_get_state,
392 .request = pca9685_pwm_request,
393 .free = pca9685_pwm_free,
394 .owner = THIS_MODULE,
397 static const struct regmap_config pca9685_regmap_i2c_config = {
400 .max_register = PCA9685_NUMREGS,
401 .cache_type = REGCACHE_NONE,
404 static int pca9685_pwm_probe(struct i2c_client *client,
405 const struct i2c_device_id *id)
411 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
415 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
416 if (IS_ERR(pca->regmap)) {
417 ret = PTR_ERR(pca->regmap);
418 dev_err(&client->dev, "Failed to initialize register map: %d\n",
423 i2c_set_clientdata(client, pca);
425 regmap_read(pca->regmap, PCA9685_MODE2, ®);
427 if (device_property_read_bool(&client->dev, "invert"))
432 if (device_property_read_bool(&client->dev, "open-drain"))
433 reg &= ~MODE2_OUTDRV;
437 regmap_write(pca->regmap, PCA9685_MODE2, reg);
439 /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
440 regmap_read(pca->regmap, PCA9685_MODE1, ®);
441 reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
442 regmap_write(pca->regmap, PCA9685_MODE1, reg);
444 /* Reset OFF registers to POR default */
445 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, LED_FULL);
446 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, LED_FULL);
448 pca->chip.ops = &pca9685_pwm_ops;
449 /* Add an extra channel for ALL_LED */
450 pca->chip.npwm = PCA9685_MAXCHAN + 1;
452 pca->chip.dev = &client->dev;
454 ret = pwmchip_add(&pca->chip);
458 ret = pca9685_pwm_gpio_probe(pca);
460 pwmchip_remove(&pca->chip);
464 pm_runtime_enable(&client->dev);
466 if (pm_runtime_enabled(&client->dev)) {
468 * Although the chip comes out of power-up in the sleep state,
469 * we force it to sleep in case it was woken up before
471 pca9685_set_sleep_mode(pca, true);
472 pm_runtime_set_suspended(&client->dev);
474 /* Wake the chip up if runtime PM is disabled */
475 pca9685_set_sleep_mode(pca, false);
481 static int pca9685_pwm_remove(struct i2c_client *client)
483 struct pca9685 *pca = i2c_get_clientdata(client);
486 ret = pwmchip_remove(&pca->chip);
490 if (!pm_runtime_enabled(&client->dev)) {
491 /* Put chip in sleep state if runtime PM is disabled */
492 pca9685_set_sleep_mode(pca, true);
495 pm_runtime_disable(&client->dev);
500 static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
502 struct i2c_client *client = to_i2c_client(dev);
503 struct pca9685 *pca = i2c_get_clientdata(client);
505 pca9685_set_sleep_mode(pca, true);
509 static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
511 struct i2c_client *client = to_i2c_client(dev);
512 struct pca9685 *pca = i2c_get_clientdata(client);
514 pca9685_set_sleep_mode(pca, false);
518 static const struct i2c_device_id pca9685_id[] = {
522 MODULE_DEVICE_TABLE(i2c, pca9685_id);
525 static const struct acpi_device_id pca9685_acpi_ids[] = {
529 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
533 static const struct of_device_id pca9685_dt_ids[] = {
534 { .compatible = "nxp,pca9685-pwm", },
537 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
540 static const struct dev_pm_ops pca9685_pwm_pm = {
541 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
542 pca9685_pwm_runtime_resume, NULL)
545 static struct i2c_driver pca9685_i2c_driver = {
547 .name = "pca9685-pwm",
548 .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
549 .of_match_table = of_match_ptr(pca9685_dt_ids),
550 .pm = &pca9685_pwm_pm,
552 .probe = pca9685_pwm_probe,
553 .remove = pca9685_pwm_remove,
554 .id_table = pca9685_id,
557 module_i2c_driver(pca9685_i2c_driver);
559 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
560 MODULE_DESCRIPTION("PWM driver for PCA9685");
561 MODULE_LICENSE("GPL");