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 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)
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)))
76 struct regmap
*regmap
;
78 #if IS_ENABLED(CONFIG_GPIOLIB)
80 struct gpio_chip gpio
;
81 DECLARE_BITMAP(pwms_inuse
, PCA9685_MAXCHAN
+ 1);
85 static inline struct pca9685
*to_pca(struct pwm_chip
*chip
)
87 return container_of(chip
, struct pca9685
, chip
);
90 #if IS_ENABLED(CONFIG_GPIOLIB)
91 static bool pca9685_pwm_test_and_set_inuse(struct pca9685
*pca
, int pwm_idx
)
95 mutex_lock(&pca
->lock
);
96 if (pwm_idx
>= PCA9685_MAXCHAN
) {
99 * pretend already in use if any of the PWMs are requested
101 if (!bitmap_empty(pca
->pwms_inuse
, PCA9685_MAXCHAN
)) {
108 * pretend already in use if the "all LEDs" channel is requested
110 if (test_bit(PCA9685_MAXCHAN
, pca
->pwms_inuse
)) {
115 is_inuse
= test_and_set_bit(pwm_idx
, pca
->pwms_inuse
);
117 mutex_unlock(&pca
->lock
);
121 static void pca9685_pwm_clear_inuse(struct pca9685
*pca
, int pwm_idx
)
123 mutex_lock(&pca
->lock
);
124 clear_bit(pwm_idx
, pca
->pwms_inuse
);
125 mutex_unlock(&pca
->lock
);
128 static int pca9685_pwm_gpio_request(struct gpio_chip
*gpio
, unsigned int offset
)
130 struct pca9685
*pca
= gpiochip_get_data(gpio
);
132 if (pca9685_pwm_test_and_set_inuse(pca
, offset
))
134 pm_runtime_get_sync(pca
->chip
.dev
);
138 static int pca9685_pwm_gpio_get(struct gpio_chip
*gpio
, unsigned int offset
)
140 struct pca9685
*pca
= gpiochip_get_data(gpio
);
141 struct pwm_device
*pwm
= &pca
->chip
.pwms
[offset
];
144 regmap_read(pca
->regmap
, LED_N_ON_H(pwm
->hwpwm
), &value
);
146 return value
& LED_FULL
;
149 static void pca9685_pwm_gpio_set(struct gpio_chip
*gpio
, unsigned int offset
,
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;
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);
160 /* Set the full ON bit */
161 regmap_write(pca
->regmap
, LED_N_ON_H(pwm
->hwpwm
), on
);
164 static void pca9685_pwm_gpio_free(struct gpio_chip
*gpio
, unsigned int offset
)
166 struct pca9685
*pca
= gpiochip_get_data(gpio
);
168 pca9685_pwm_gpio_set(gpio
, offset
, 0);
169 pm_runtime_put(pca
->chip
.dev
);
170 pca9685_pwm_clear_inuse(pca
, offset
);
173 static int pca9685_pwm_gpio_get_direction(struct gpio_chip
*chip
,
177 return GPIO_LINE_DIRECTION_OUT
;
180 static int pca9685_pwm_gpio_direction_input(struct gpio_chip
*gpio
,
186 static int pca9685_pwm_gpio_direction_output(struct gpio_chip
*gpio
,
187 unsigned int offset
, int value
)
189 pca9685_pwm_gpio_set(gpio
, offset
, value
);
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
200 static int pca9685_pwm_gpio_probe(struct pca9685
*pca
)
202 struct device
*dev
= pca
->chip
.dev
;
204 mutex_init(&pca
->lock
);
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
;
216 pca
->gpio
.ngpio
= PCA9685_MAXCHAN
;
217 pca
->gpio
.can_sleep
= true;
219 return devm_gpiochip_add_data(dev
, &pca
->gpio
, pca
);
222 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685
*pca
,
229 pca9685_pwm_clear_inuse(struct pca9685
*pca
, int pwm_idx
)
233 static inline int pca9685_pwm_gpio_probe(struct pca9685
*pca
)
239 static void pca9685_set_sleep_mode(struct pca9685
*pca
, bool enable
)
241 regmap_update_bits(pca
->regmap
, PCA9685_MODE1
,
242 MODE1_SLEEP
, enable
? MODE1_SLEEP
: 0);
244 /* Wait 500us for the oscillator to be back up */
249 static int pca9685_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
250 int duty_ns
, int period_ns
)
252 struct pca9685
*pca
= to_pca(chip
);
253 unsigned long long duty
;
257 if (period_ns
!= pca
->period_ns
) {
258 prescale
= DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ
* period_ns
,
259 PCA9685_COUNTER_RANGE
* 1000) - 1;
261 if (prescale
>= PCA9685_PRESCALE_MIN
&&
262 prescale
<= PCA9685_PRESCALE_MAX
) {
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.
269 /* Put chip into sleep mode */
270 pca9685_set_sleep_mode(pca
, true);
272 /* Change the chip-wide output frequency */
273 regmap_write(pca
->regmap
, PCA9685_PRESCALE
, prescale
);
275 /* Wake the chip up */
276 pca9685_set_sleep_mode(pca
, false);
278 pca
->period_ns
= period_ns
;
281 "prescaler not set: period out of bounds!\n");
287 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
288 reg
= PCA9685_ALL_LED_OFF_H
;
290 reg
= LED_N_OFF_H(pwm
->hwpwm
);
292 regmap_write(pca
->regmap
, reg
, LED_FULL
);
297 if (duty_ns
== period_ns
) {
298 /* Clear both OFF registers */
299 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
300 reg
= PCA9685_ALL_LED_OFF_L
;
302 reg
= LED_N_OFF_L(pwm
->hwpwm
);
304 regmap_write(pca
->regmap
, reg
, 0x0);
306 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
307 reg
= PCA9685_ALL_LED_OFF_H
;
309 reg
= LED_N_OFF_H(pwm
->hwpwm
);
311 regmap_write(pca
->regmap
, reg
, 0x0);
313 /* Set the full ON bit */
314 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
315 reg
= PCA9685_ALL_LED_ON_H
;
317 reg
= LED_N_ON_H(pwm
->hwpwm
);
319 regmap_write(pca
->regmap
, reg
, LED_FULL
);
324 duty
= PCA9685_COUNTER_RANGE
* (unsigned long long)duty_ns
;
325 duty
= DIV_ROUND_UP_ULL(duty
, period_ns
);
327 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
328 reg
= PCA9685_ALL_LED_OFF_L
;
330 reg
= LED_N_OFF_L(pwm
->hwpwm
);
332 regmap_write(pca
->regmap
, reg
, (int)duty
& 0xff);
334 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
335 reg
= PCA9685_ALL_LED_OFF_H
;
337 reg
= LED_N_OFF_H(pwm
->hwpwm
);
339 regmap_write(pca
->regmap
, reg
, ((int)duty
>> 8) & 0xf);
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
;
345 reg
= LED_N_ON_H(pwm
->hwpwm
);
347 regmap_write(pca
->regmap
, reg
, 0);
352 static int pca9685_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
354 struct pca9685
*pca
= to_pca(chip
);
358 * The PWM subsystem does not support a pre-delay.
359 * So, set the ON-timeout to 0
361 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
362 reg
= PCA9685_ALL_LED_ON_L
;
364 reg
= LED_N_ON_L(pwm
->hwpwm
);
366 regmap_write(pca
->regmap
, reg
, 0);
368 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
369 reg
= PCA9685_ALL_LED_ON_H
;
371 reg
= LED_N_ON_H(pwm
->hwpwm
);
373 regmap_write(pca
->regmap
, reg
, 0);
376 * Clear the full-off bit.
377 * It has precedence over the others and must be off.
379 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
380 reg
= PCA9685_ALL_LED_OFF_H
;
382 reg
= LED_N_OFF_H(pwm
->hwpwm
);
384 regmap_update_bits(pca
->regmap
, reg
, LED_FULL
, 0x0);
389 static void pca9685_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
391 struct pca9685
*pca
= to_pca(chip
);
394 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
395 reg
= PCA9685_ALL_LED_OFF_H
;
397 reg
= LED_N_OFF_H(pwm
->hwpwm
);
399 regmap_write(pca
->regmap
, reg
, LED_FULL
);
401 /* Clear the LED_OFF counter. */
402 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
403 reg
= PCA9685_ALL_LED_OFF_L
;
405 reg
= LED_N_OFF_L(pwm
->hwpwm
);
407 regmap_write(pca
->regmap
, reg
, 0x0);
410 static int pca9685_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
412 struct pca9685
*pca
= to_pca(chip
);
414 if (pca9685_pwm_test_and_set_inuse(pca
, pwm
->hwpwm
))
416 pm_runtime_get_sync(chip
->dev
);
421 static void pca9685_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
423 struct pca9685
*pca
= to_pca(chip
);
425 pca9685_pwm_disable(chip
, pwm
);
426 pm_runtime_put(chip
->dev
);
427 pca9685_pwm_clear_inuse(pca
, pwm
->hwpwm
);
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
,
439 static const struct regmap_config pca9685_regmap_i2c_config
= {
442 .max_register
= PCA9685_NUMREGS
,
443 .cache_type
= REGCACHE_NONE
,
446 static int pca9685_pwm_probe(struct i2c_client
*client
,
447 const struct i2c_device_id
*id
)
453 pca
= devm_kzalloc(&client
->dev
, sizeof(*pca
), GFP_KERNEL
);
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",
464 pca
->period_ns
= PCA9685_DEFAULT_PERIOD
;
466 i2c_set_clientdata(client
, pca
);
468 regmap_read(pca
->regmap
, PCA9685_MODE2
, ®
);
470 if (device_property_read_bool(&client
->dev
, "invert"))
475 if (device_property_read_bool(&client
->dev
, "open-drain"))
476 reg
&= ~MODE2_OUTDRV
;
480 regmap_write(pca
->regmap
, PCA9685_MODE2
, reg
);
482 /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
483 regmap_read(pca
->regmap
, PCA9685_MODE1
, ®
);
484 reg
&= ~(MODE1_ALLCALL
| MODE1_SUB1
| MODE1_SUB2
| MODE1_SUB3
);
485 regmap_write(pca
->regmap
, PCA9685_MODE1
, reg
);
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);
491 pca
->chip
.ops
= &pca9685_pwm_ops
;
492 /* Add an extra channel for ALL_LED */
493 pca
->chip
.npwm
= PCA9685_MAXCHAN
+ 1;
495 pca
->chip
.dev
= &client
->dev
;
498 ret
= pwmchip_add(&pca
->chip
);
502 ret
= pca9685_pwm_gpio_probe(pca
);
504 pwmchip_remove(&pca
->chip
);
508 /* The chip comes out of power-up in the active state */
509 pm_runtime_set_active(&client
->dev
);
511 * Enable will put the chip into suspend, which is what we
512 * want as all outputs are disabled at this point
514 pm_runtime_enable(&client
->dev
);
519 static int pca9685_pwm_remove(struct i2c_client
*client
)
521 struct pca9685
*pca
= i2c_get_clientdata(client
);
524 ret
= pwmchip_remove(&pca
->chip
);
527 pm_runtime_disable(&client
->dev
);
531 static int __maybe_unused
pca9685_pwm_runtime_suspend(struct device
*dev
)
533 struct i2c_client
*client
= to_i2c_client(dev
);
534 struct pca9685
*pca
= i2c_get_clientdata(client
);
536 pca9685_set_sleep_mode(pca
, true);
540 static int __maybe_unused
pca9685_pwm_runtime_resume(struct device
*dev
)
542 struct i2c_client
*client
= to_i2c_client(dev
);
543 struct pca9685
*pca
= i2c_get_clientdata(client
);
545 pca9685_set_sleep_mode(pca
, false);
549 static const struct i2c_device_id pca9685_id
[] = {
553 MODULE_DEVICE_TABLE(i2c
, pca9685_id
);
556 static const struct acpi_device_id pca9685_acpi_ids
[] = {
560 MODULE_DEVICE_TABLE(acpi
, pca9685_acpi_ids
);
564 static const struct of_device_id pca9685_dt_ids
[] = {
565 { .compatible
= "nxp,pca9685-pwm", },
568 MODULE_DEVICE_TABLE(of
, pca9685_dt_ids
);
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
)
576 static struct i2c_driver pca9685_i2c_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
,
583 .probe
= pca9685_pwm_probe
,
584 .remove
= pca9685_pwm_remove
,
585 .id_table
= pca9685_id
,
588 module_i2c_driver(pca9685_i2c_driver
);
590 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
591 MODULE_DESCRIPTION("PWM driver for PCA9685");
592 MODULE_LICENSE("GPL");