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>
25 * Because the PCA9685 has only one prescaler per chip, changing the period of
26 * one channel affects the period of all 16 PWM outputs!
27 * However, the ratio between each configured duty cycle and the chip-wide
28 * period remains constant, because the OFF time is set in proportion to the
32 #define PCA9685_MODE1 0x00
33 #define PCA9685_MODE2 0x01
34 #define PCA9685_SUBADDR1 0x02
35 #define PCA9685_SUBADDR2 0x03
36 #define PCA9685_SUBADDR3 0x04
37 #define PCA9685_ALLCALLADDR 0x05
38 #define PCA9685_LEDX_ON_L 0x06
39 #define PCA9685_LEDX_ON_H 0x07
40 #define PCA9685_LEDX_OFF_L 0x08
41 #define PCA9685_LEDX_OFF_H 0x09
43 #define PCA9685_ALL_LED_ON_L 0xFA
44 #define PCA9685_ALL_LED_ON_H 0xFB
45 #define PCA9685_ALL_LED_OFF_L 0xFC
46 #define PCA9685_ALL_LED_OFF_H 0xFD
47 #define PCA9685_PRESCALE 0xFE
49 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
50 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
52 #define PCA9685_COUNTER_RANGE 4096
53 #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
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 (1 << 4)
60 #define MODE1_SLEEP (1 << 4)
61 #define MODE2_INVRT (1 << 4)
62 #define MODE2_OUTDRV (1 << 2)
64 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
65 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
66 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
67 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
71 struct regmap
*regmap
;
74 #if IS_ENABLED(CONFIG_GPIOLIB)
76 struct gpio_chip gpio
;
80 static inline struct pca9685
*to_pca(struct pwm_chip
*chip
)
82 return container_of(chip
, struct pca9685
, chip
);
85 #if IS_ENABLED(CONFIG_GPIOLIB)
86 static int pca9685_pwm_gpio_request(struct gpio_chip
*gpio
, unsigned int offset
)
88 struct pca9685
*pca
= gpiochip_get_data(gpio
);
89 struct pwm_device
*pwm
;
91 mutex_lock(&pca
->lock
);
93 pwm
= &pca
->chip
.pwms
[offset
];
95 if (pwm
->flags
& (PWMF_REQUESTED
| PWMF_EXPORTED
)) {
96 mutex_unlock(&pca
->lock
);
100 pwm_set_chip_data(pwm
, (void *)1);
102 mutex_unlock(&pca
->lock
);
103 pm_runtime_get_sync(pca
->chip
.dev
);
107 static bool pca9685_pwm_is_gpio(struct pca9685
*pca
, struct pwm_device
*pwm
)
109 bool is_gpio
= false;
111 mutex_lock(&pca
->lock
);
113 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
) {
117 * Check if any of the GPIOs are requested and in that case
118 * prevent using the "all LEDs" channel.
120 for (i
= 0; i
< pca
->gpio
.ngpio
; i
++)
121 if (gpiochip_is_requested(&pca
->gpio
, i
)) {
125 } else if (pwm_get_chip_data(pwm
)) {
129 mutex_unlock(&pca
->lock
);
133 static int pca9685_pwm_gpio_get(struct gpio_chip
*gpio
, unsigned int offset
)
135 struct pca9685
*pca
= gpiochip_get_data(gpio
);
136 struct pwm_device
*pwm
= &pca
->chip
.pwms
[offset
];
139 regmap_read(pca
->regmap
, LED_N_ON_H(pwm
->hwpwm
), &value
);
141 return value
& LED_FULL
;
144 static void pca9685_pwm_gpio_set(struct gpio_chip
*gpio
, unsigned int offset
,
147 struct pca9685
*pca
= gpiochip_get_data(gpio
);
148 struct pwm_device
*pwm
= &pca
->chip
.pwms
[offset
];
149 unsigned int on
= value
? LED_FULL
: 0;
151 /* Clear both OFF registers */
152 regmap_write(pca
->regmap
, LED_N_OFF_L(pwm
->hwpwm
), 0);
153 regmap_write(pca
->regmap
, LED_N_OFF_H(pwm
->hwpwm
), 0);
155 /* Set the full ON bit */
156 regmap_write(pca
->regmap
, LED_N_ON_H(pwm
->hwpwm
), on
);
159 static void pca9685_pwm_gpio_free(struct gpio_chip
*gpio
, unsigned int offset
)
161 struct pca9685
*pca
= gpiochip_get_data(gpio
);
162 struct pwm_device
*pwm
;
164 pca9685_pwm_gpio_set(gpio
, offset
, 0);
165 pm_runtime_put(pca
->chip
.dev
);
166 mutex_lock(&pca
->lock
);
167 pwm
= &pca
->chip
.pwms
[offset
];
168 mutex_unlock(&pca
->lock
);
171 static int pca9685_pwm_gpio_get_direction(struct gpio_chip
*chip
,
178 static int pca9685_pwm_gpio_direction_input(struct gpio_chip
*gpio
,
184 static int pca9685_pwm_gpio_direction_output(struct gpio_chip
*gpio
,
185 unsigned int offset
, int value
)
187 pca9685_pwm_gpio_set(gpio
, offset
, value
);
193 * The PCA9685 has a bit for turning the PWM output full off or on. Some
194 * boards like Intel Galileo actually uses these as normal GPIOs so we
195 * expose a GPIO chip here which can exclusively take over the underlying
198 static int pca9685_pwm_gpio_probe(struct pca9685
*pca
)
200 struct device
*dev
= pca
->chip
.dev
;
202 mutex_init(&pca
->lock
);
204 pca
->gpio
.label
= dev_name(dev
);
205 pca
->gpio
.parent
= dev
;
206 pca
->gpio
.request
= pca9685_pwm_gpio_request
;
207 pca
->gpio
.free
= pca9685_pwm_gpio_free
;
208 pca
->gpio
.get_direction
= pca9685_pwm_gpio_get_direction
;
209 pca
->gpio
.direction_input
= pca9685_pwm_gpio_direction_input
;
210 pca
->gpio
.direction_output
= pca9685_pwm_gpio_direction_output
;
211 pca
->gpio
.get
= pca9685_pwm_gpio_get
;
212 pca
->gpio
.set
= pca9685_pwm_gpio_set
;
214 pca
->gpio
.ngpio
= PCA9685_MAXCHAN
;
215 pca
->gpio
.can_sleep
= true;
217 return devm_gpiochip_add_data(dev
, &pca
->gpio
, pca
);
220 static inline bool pca9685_pwm_is_gpio(struct pca9685
*pca
,
221 struct pwm_device
*pwm
)
226 static inline int pca9685_pwm_gpio_probe(struct pca9685
*pca
)
232 static void pca9685_set_sleep_mode(struct pca9685
*pca
, bool enable
)
234 regmap_update_bits(pca
->regmap
, PCA9685_MODE1
,
235 MODE1_SLEEP
, enable
? MODE1_SLEEP
: 0);
237 /* Wait 500us for the oscillator to be back up */
242 static int pca9685_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
243 int duty_ns
, int period_ns
)
245 struct pca9685
*pca
= to_pca(chip
);
246 unsigned long long duty
;
250 if (period_ns
!= pca
->period_ns
) {
251 prescale
= DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ
* period_ns
,
252 PCA9685_COUNTER_RANGE
* 1000) - 1;
254 if (prescale
>= PCA9685_PRESCALE_MIN
&&
255 prescale
<= PCA9685_PRESCALE_MAX
) {
257 * putting the chip briefly into SLEEP mode
258 * at this point won't interfere with the
259 * pm_runtime framework, because the pm_runtime
260 * state is guaranteed active here.
262 /* Put chip into sleep mode */
263 pca9685_set_sleep_mode(pca
, true);
265 /* Change the chip-wide output frequency */
266 regmap_write(pca
->regmap
, PCA9685_PRESCALE
, prescale
);
268 /* Wake the chip up */
269 pca9685_set_sleep_mode(pca
, false);
271 pca
->period_ns
= period_ns
;
274 "prescaler not set: period out of bounds!\n");
279 pca
->duty_ns
= duty_ns
;
282 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
283 reg
= PCA9685_ALL_LED_OFF_H
;
285 reg
= LED_N_OFF_H(pwm
->hwpwm
);
287 regmap_write(pca
->regmap
, reg
, LED_FULL
);
292 if (duty_ns
== period_ns
) {
293 /* Clear both OFF registers */
294 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
295 reg
= PCA9685_ALL_LED_OFF_L
;
297 reg
= LED_N_OFF_L(pwm
->hwpwm
);
299 regmap_write(pca
->regmap
, reg
, 0x0);
301 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
302 reg
= PCA9685_ALL_LED_OFF_H
;
304 reg
= LED_N_OFF_H(pwm
->hwpwm
);
306 regmap_write(pca
->regmap
, reg
, 0x0);
308 /* Set the full ON bit */
309 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
310 reg
= PCA9685_ALL_LED_ON_H
;
312 reg
= LED_N_ON_H(pwm
->hwpwm
);
314 regmap_write(pca
->regmap
, reg
, LED_FULL
);
319 duty
= PCA9685_COUNTER_RANGE
* (unsigned long long)duty_ns
;
320 duty
= DIV_ROUND_UP_ULL(duty
, period_ns
);
322 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
323 reg
= PCA9685_ALL_LED_OFF_L
;
325 reg
= LED_N_OFF_L(pwm
->hwpwm
);
327 regmap_write(pca
->regmap
, reg
, (int)duty
& 0xff);
329 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
330 reg
= PCA9685_ALL_LED_OFF_H
;
332 reg
= LED_N_OFF_H(pwm
->hwpwm
);
334 regmap_write(pca
->regmap
, reg
, ((int)duty
>> 8) & 0xf);
336 /* Clear the full ON bit, otherwise the set OFF time has no effect */
337 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
338 reg
= PCA9685_ALL_LED_ON_H
;
340 reg
= LED_N_ON_H(pwm
->hwpwm
);
342 regmap_write(pca
->regmap
, reg
, 0);
347 static int pca9685_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
349 struct pca9685
*pca
= to_pca(chip
);
353 * The PWM subsystem does not support a pre-delay.
354 * So, set the ON-timeout to 0
356 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
357 reg
= PCA9685_ALL_LED_ON_L
;
359 reg
= LED_N_ON_L(pwm
->hwpwm
);
361 regmap_write(pca
->regmap
, reg
, 0);
363 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
364 reg
= PCA9685_ALL_LED_ON_H
;
366 reg
= LED_N_ON_H(pwm
->hwpwm
);
368 regmap_write(pca
->regmap
, reg
, 0);
371 * Clear the full-off bit.
372 * It has precedence over the others and must be off.
374 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
375 reg
= PCA9685_ALL_LED_OFF_H
;
377 reg
= LED_N_OFF_H(pwm
->hwpwm
);
379 regmap_update_bits(pca
->regmap
, reg
, LED_FULL
, 0x0);
384 static void pca9685_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
386 struct pca9685
*pca
= to_pca(chip
);
389 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
390 reg
= PCA9685_ALL_LED_OFF_H
;
392 reg
= LED_N_OFF_H(pwm
->hwpwm
);
394 regmap_write(pca
->regmap
, reg
, LED_FULL
);
396 /* Clear the LED_OFF counter. */
397 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
398 reg
= PCA9685_ALL_LED_OFF_L
;
400 reg
= LED_N_OFF_L(pwm
->hwpwm
);
402 regmap_write(pca
->regmap
, reg
, 0x0);
405 static int pca9685_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
407 struct pca9685
*pca
= to_pca(chip
);
409 if (pca9685_pwm_is_gpio(pca
, pwm
))
411 pm_runtime_get_sync(chip
->dev
);
416 static void pca9685_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
418 pca9685_pwm_disable(chip
, pwm
);
419 pm_runtime_put(chip
->dev
);
422 static const struct pwm_ops pca9685_pwm_ops
= {
423 .enable
= pca9685_pwm_enable
,
424 .disable
= pca9685_pwm_disable
,
425 .config
= pca9685_pwm_config
,
426 .request
= pca9685_pwm_request
,
427 .free
= pca9685_pwm_free
,
428 .owner
= THIS_MODULE
,
431 static const struct regmap_config pca9685_regmap_i2c_config
= {
434 .max_register
= PCA9685_NUMREGS
,
435 .cache_type
= REGCACHE_NONE
,
438 static int pca9685_pwm_probe(struct i2c_client
*client
,
439 const struct i2c_device_id
*id
)
445 pca
= devm_kzalloc(&client
->dev
, sizeof(*pca
), GFP_KERNEL
);
449 pca
->regmap
= devm_regmap_init_i2c(client
, &pca9685_regmap_i2c_config
);
450 if (IS_ERR(pca
->regmap
)) {
451 ret
= PTR_ERR(pca
->regmap
);
452 dev_err(&client
->dev
, "Failed to initialize register map: %d\n",
457 pca
->period_ns
= PCA9685_DEFAULT_PERIOD
;
459 i2c_set_clientdata(client
, pca
);
461 regmap_read(pca
->regmap
, PCA9685_MODE2
, &mode2
);
463 if (device_property_read_bool(&client
->dev
, "invert"))
464 mode2
|= MODE2_INVRT
;
466 mode2
&= ~MODE2_INVRT
;
468 if (device_property_read_bool(&client
->dev
, "open-drain"))
469 mode2
&= ~MODE2_OUTDRV
;
471 mode2
|= MODE2_OUTDRV
;
473 regmap_write(pca
->regmap
, PCA9685_MODE2
, mode2
);
475 /* clear all "full off" bits */
476 regmap_write(pca
->regmap
, PCA9685_ALL_LED_OFF_L
, 0);
477 regmap_write(pca
->regmap
, PCA9685_ALL_LED_OFF_H
, 0);
479 pca
->chip
.ops
= &pca9685_pwm_ops
;
480 /* add an extra channel for ALL_LED */
481 pca
->chip
.npwm
= PCA9685_MAXCHAN
+ 1;
483 pca
->chip
.dev
= &client
->dev
;
486 ret
= pwmchip_add(&pca
->chip
);
490 ret
= pca9685_pwm_gpio_probe(pca
);
492 pwmchip_remove(&pca
->chip
);
496 /* the chip comes out of power-up in the active state */
497 pm_runtime_set_active(&client
->dev
);
499 * enable will put the chip into suspend, which is what we
500 * want as all outputs are disabled at this point
502 pm_runtime_enable(&client
->dev
);
507 static int pca9685_pwm_remove(struct i2c_client
*client
)
509 struct pca9685
*pca
= i2c_get_clientdata(client
);
512 ret
= pwmchip_remove(&pca
->chip
);
515 pm_runtime_disable(&client
->dev
);
520 static int pca9685_pwm_runtime_suspend(struct device
*dev
)
522 struct i2c_client
*client
= to_i2c_client(dev
);
523 struct pca9685
*pca
= i2c_get_clientdata(client
);
525 pca9685_set_sleep_mode(pca
, true);
529 static int pca9685_pwm_runtime_resume(struct device
*dev
)
531 struct i2c_client
*client
= to_i2c_client(dev
);
532 struct pca9685
*pca
= i2c_get_clientdata(client
);
534 pca9685_set_sleep_mode(pca
, false);
539 static const struct i2c_device_id pca9685_id
[] = {
543 MODULE_DEVICE_TABLE(i2c
, pca9685_id
);
546 static const struct acpi_device_id pca9685_acpi_ids
[] = {
550 MODULE_DEVICE_TABLE(acpi
, pca9685_acpi_ids
);
554 static const struct of_device_id pca9685_dt_ids
[] = {
555 { .compatible
= "nxp,pca9685-pwm", },
558 MODULE_DEVICE_TABLE(of
, pca9685_dt_ids
);
561 static const struct dev_pm_ops pca9685_pwm_pm
= {
562 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend
,
563 pca9685_pwm_runtime_resume
, NULL
)
566 static struct i2c_driver pca9685_i2c_driver
= {
568 .name
= "pca9685-pwm",
569 .acpi_match_table
= ACPI_PTR(pca9685_acpi_ids
),
570 .of_match_table
= of_match_ptr(pca9685_dt_ids
),
571 .pm
= &pca9685_pwm_pm
,
573 .probe
= pca9685_pwm_probe
,
574 .remove
= pca9685_pwm_remove
,
575 .id_table
= pca9685_id
,
578 module_i2c_driver(pca9685_i2c_driver
);
580 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
581 MODULE_DESCRIPTION("PWM driver for PCA9685");
582 MODULE_LICENSE("GPL");