2 * Driver for PCA9685 16-channel 12-bit PWM LED controller
4 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
5 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
7 * based on the pwm-twl-led.c driver
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/pwm.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
31 * Because the PCA9685 has only one prescaler per chip, changing the period of
32 * one channel affects the period of all 16 PWM outputs!
33 * However, the ratio between each configured duty cycle and the chip-wide
34 * period remains constant, because the OFF time is set in proportion to the
38 #define PCA9685_MODE1 0x00
39 #define PCA9685_MODE2 0x01
40 #define PCA9685_SUBADDR1 0x02
41 #define PCA9685_SUBADDR2 0x03
42 #define PCA9685_SUBADDR3 0x04
43 #define PCA9685_ALLCALLADDR 0x05
44 #define PCA9685_LEDX_ON_L 0x06
45 #define PCA9685_LEDX_ON_H 0x07
46 #define PCA9685_LEDX_OFF_L 0x08
47 #define PCA9685_LEDX_OFF_H 0x09
49 #define PCA9685_ALL_LED_ON_L 0xFA
50 #define PCA9685_ALL_LED_ON_H 0xFB
51 #define PCA9685_ALL_LED_OFF_L 0xFC
52 #define PCA9685_ALL_LED_OFF_H 0xFD
53 #define PCA9685_PRESCALE 0xFE
55 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
56 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
58 #define PCA9685_COUNTER_RANGE 4096
59 #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
60 #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
62 #define PCA9685_NUMREGS 0xFF
63 #define PCA9685_MAXCHAN 0x10
65 #define LED_FULL (1 << 4)
66 #define MODE1_RESTART (1 << 7)
67 #define MODE1_SLEEP (1 << 4)
68 #define MODE2_INVRT (1 << 4)
69 #define MODE2_OUTDRV (1 << 2)
71 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
72 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
73 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
74 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
78 struct regmap
*regmap
;
84 static inline struct pca9685
*to_pca(struct pwm_chip
*chip
)
86 return container_of(chip
, struct pca9685
, chip
);
89 static int pca9685_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
90 int duty_ns
, int period_ns
)
92 struct pca9685
*pca
= to_pca(chip
);
93 unsigned long long duty
;
97 if (period_ns
!= pca
->period_ns
) {
98 prescale
= DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ
* period_ns
,
99 PCA9685_COUNTER_RANGE
* 1000) - 1;
101 if (prescale
>= PCA9685_PRESCALE_MIN
&&
102 prescale
<= PCA9685_PRESCALE_MAX
) {
103 /* Put chip into sleep mode */
104 regmap_update_bits(pca
->regmap
, PCA9685_MODE1
,
105 MODE1_SLEEP
, MODE1_SLEEP
);
107 /* Change the chip-wide output frequency */
108 regmap_write(pca
->regmap
, PCA9685_PRESCALE
, prescale
);
110 /* Wake the chip up */
111 regmap_update_bits(pca
->regmap
, PCA9685_MODE1
,
114 /* Wait 500us for the oscillator to be back up */
117 pca
->period_ns
= period_ns
;
120 * If the duty cycle did not change, restart PWM with
121 * the same duty cycle to period ratio and return.
123 if (duty_ns
== pca
->duty_ns
) {
124 regmap_update_bits(pca
->regmap
, PCA9685_MODE1
,
130 "prescaler not set: period out of bounds!\n");
135 pca
->duty_ns
= duty_ns
;
138 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
139 reg
= PCA9685_ALL_LED_OFF_H
;
141 reg
= LED_N_OFF_H(pwm
->hwpwm
);
143 regmap_write(pca
->regmap
, reg
, LED_FULL
);
148 if (duty_ns
== period_ns
) {
149 /* Clear both OFF registers */
150 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
151 reg
= PCA9685_ALL_LED_OFF_L
;
153 reg
= LED_N_OFF_L(pwm
->hwpwm
);
155 regmap_write(pca
->regmap
, reg
, 0x0);
157 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
158 reg
= PCA9685_ALL_LED_OFF_H
;
160 reg
= LED_N_OFF_H(pwm
->hwpwm
);
162 regmap_write(pca
->regmap
, reg
, 0x0);
164 /* Set the full ON bit */
165 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
166 reg
= PCA9685_ALL_LED_ON_H
;
168 reg
= LED_N_ON_H(pwm
->hwpwm
);
170 regmap_write(pca
->regmap
, reg
, LED_FULL
);
175 duty
= PCA9685_COUNTER_RANGE
* (unsigned long long)duty_ns
;
176 duty
= DIV_ROUND_UP_ULL(duty
, period_ns
);
178 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
179 reg
= PCA9685_ALL_LED_OFF_L
;
181 reg
= LED_N_OFF_L(pwm
->hwpwm
);
183 regmap_write(pca
->regmap
, reg
, (int)duty
& 0xff);
185 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
186 reg
= PCA9685_ALL_LED_OFF_H
;
188 reg
= LED_N_OFF_H(pwm
->hwpwm
);
190 regmap_write(pca
->regmap
, reg
, ((int)duty
>> 8) & 0xf);
192 /* Clear the full ON bit, otherwise the set OFF time has no effect */
193 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
194 reg
= PCA9685_ALL_LED_ON_H
;
196 reg
= LED_N_ON_H(pwm
->hwpwm
);
198 regmap_write(pca
->regmap
, reg
, 0);
203 static int pca9685_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
205 struct pca9685
*pca
= to_pca(chip
);
209 * The PWM subsystem does not support a pre-delay.
210 * So, set the ON-timeout to 0
212 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
213 reg
= PCA9685_ALL_LED_ON_L
;
215 reg
= LED_N_ON_L(pwm
->hwpwm
);
217 regmap_write(pca
->regmap
, reg
, 0);
219 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
220 reg
= PCA9685_ALL_LED_ON_H
;
222 reg
= LED_N_ON_H(pwm
->hwpwm
);
224 regmap_write(pca
->regmap
, reg
, 0);
227 * Clear the full-off bit.
228 * It has precedence over the others and must be off.
230 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
231 reg
= PCA9685_ALL_LED_OFF_H
;
233 reg
= LED_N_OFF_H(pwm
->hwpwm
);
235 regmap_update_bits(pca
->regmap
, reg
, LED_FULL
, 0x0);
240 static void pca9685_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
242 struct pca9685
*pca
= to_pca(chip
);
245 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
246 reg
= PCA9685_ALL_LED_OFF_H
;
248 reg
= LED_N_OFF_H(pwm
->hwpwm
);
250 regmap_write(pca
->regmap
, reg
, LED_FULL
);
252 /* Clear the LED_OFF counter. */
253 if (pwm
->hwpwm
>= PCA9685_MAXCHAN
)
254 reg
= PCA9685_ALL_LED_OFF_L
;
256 reg
= LED_N_OFF_L(pwm
->hwpwm
);
258 regmap_write(pca
->regmap
, reg
, 0x0);
261 static int pca9685_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
263 struct pca9685
*pca
= to_pca(chip
);
265 if (pca
->active_cnt
++ == 0)
266 return regmap_update_bits(pca
->regmap
, PCA9685_MODE1
,
272 static void pca9685_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
274 struct pca9685
*pca
= to_pca(chip
);
276 if (--pca
->active_cnt
== 0)
277 regmap_update_bits(pca
->regmap
, PCA9685_MODE1
, MODE1_SLEEP
,
281 static const struct pwm_ops pca9685_pwm_ops
= {
282 .enable
= pca9685_pwm_enable
,
283 .disable
= pca9685_pwm_disable
,
284 .config
= pca9685_pwm_config
,
285 .request
= pca9685_pwm_request
,
286 .free
= pca9685_pwm_free
,
287 .owner
= THIS_MODULE
,
290 static const struct regmap_config pca9685_regmap_i2c_config
= {
293 .max_register
= PCA9685_NUMREGS
,
294 .cache_type
= REGCACHE_NONE
,
297 static int pca9685_pwm_probe(struct i2c_client
*client
,
298 const struct i2c_device_id
*id
)
300 struct device_node
*np
= client
->dev
.of_node
;
305 pca
= devm_kzalloc(&client
->dev
, sizeof(*pca
), GFP_KERNEL
);
309 pca
->regmap
= devm_regmap_init_i2c(client
, &pca9685_regmap_i2c_config
);
310 if (IS_ERR(pca
->regmap
)) {
311 ret
= PTR_ERR(pca
->regmap
);
312 dev_err(&client
->dev
, "Failed to initialize register map: %d\n",
317 pca
->period_ns
= PCA9685_DEFAULT_PERIOD
;
319 i2c_set_clientdata(client
, pca
);
321 regmap_read(pca
->regmap
, PCA9685_MODE2
, &mode2
);
323 if (of_property_read_bool(np
, "invert"))
324 mode2
|= MODE2_INVRT
;
326 mode2
&= ~MODE2_INVRT
;
328 if (of_property_read_bool(np
, "open-drain"))
329 mode2
&= ~MODE2_OUTDRV
;
331 mode2
|= MODE2_OUTDRV
;
333 regmap_write(pca
->regmap
, PCA9685_MODE2
, mode2
);
335 /* clear all "full off" bits */
336 regmap_write(pca
->regmap
, PCA9685_ALL_LED_OFF_L
, 0);
337 regmap_write(pca
->regmap
, PCA9685_ALL_LED_OFF_H
, 0);
339 pca
->chip
.ops
= &pca9685_pwm_ops
;
340 /* add an extra channel for ALL_LED */
341 pca
->chip
.npwm
= PCA9685_MAXCHAN
+ 1;
343 pca
->chip
.dev
= &client
->dev
;
345 pca
->chip
.can_sleep
= true;
347 return pwmchip_add(&pca
->chip
);
350 static int pca9685_pwm_remove(struct i2c_client
*client
)
352 struct pca9685
*pca
= i2c_get_clientdata(client
);
354 regmap_update_bits(pca
->regmap
, PCA9685_MODE1
, MODE1_SLEEP
,
357 return pwmchip_remove(&pca
->chip
);
360 static const struct i2c_device_id pca9685_id
[] = {
364 MODULE_DEVICE_TABLE(i2c
, pca9685_id
);
366 static const struct of_device_id pca9685_dt_ids
[] = {
367 { .compatible
= "nxp,pca9685-pwm", },
370 MODULE_DEVICE_TABLE(of
, pca9685_dt_ids
);
372 static struct i2c_driver pca9685_i2c_driver
= {
374 .name
= "pca9685-pwm",
375 .of_match_table
= pca9685_dt_ids
,
377 .probe
= pca9685_pwm_probe
,
378 .remove
= pca9685_pwm_remove
,
379 .id_table
= pca9685_id
,
382 module_i2c_driver(pca9685_i2c_driver
);
384 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
385 MODULE_DESCRIPTION("PWM driver for PCA9685");
386 MODULE_LICENSE("GPL");