Skip to content

Commit 01ec847

Browse files
clemensgthierryreding
authored andcommitted
pwm-pca9685: Support changing the output frequency
Previously, period_ns and duty_ns were only used to determine the ratio of ON and OFF time, the default frequency of 200 Hz was never changed. The PCA9685 however is capable of changing the PWM output frequency, which is expected when changing the period. This patch configures the prescaler accordingly, using the formula and notes provided in the PCA9685 datasheet. Bounds checking for the minimum and maximum frequencies, last updated in revision v.4 of said datasheet, is also added. The prescaler is only touched if the period changed, because we have to put the chip into sleep mode to unlock the prescale register. If it is changed, the PWM output frequency changes for all outputs, because there is one prescaler per chip. This is documented in the PCA9685 datasheet and in the comments. If the duty cycle is not changed at the same time as the period, then we restart the PWM output using the duty cycle to period ratio from before the period change. When using LEDs for example, previously set brightness levels stay the same when the frequency changes. Cc: Thierry Reding <[email protected]> Cc: Steffen Trumtrar <[email protected]> Signed-off-by: Clemens Gruber <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 4a627b5 commit 01ec847

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

drivers/pwm/pwm-pca9685.c

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Driver for PCA9685 16-channel 12-bit PWM LED controller
33
*
44
* Copyright (C) 2013 Steffen Trumtrar <[email protected]>
5+
* Copyright (C) 2015 Clemens Gruber <[email protected]>
56
*
67
* based on the pwm-twl-led.c driver
78
*
@@ -24,6 +25,15 @@
2425
#include <linux/pwm.h>
2526
#include <linux/regmap.h>
2627
#include <linux/slab.h>
28+
#include <linux/delay.h>
29+
30+
/*
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
35+
* counter range.
36+
*/
2737

2838
#define PCA9685_MODE1 0x00
2939
#define PCA9685_MODE2 0x01
@@ -42,10 +52,18 @@
4252
#define PCA9685_ALL_LED_OFF_H 0xFD
4353
#define PCA9685_PRESCALE 0xFE
4454

55+
#define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
56+
#define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
57+
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 */
61+
4562
#define PCA9685_NUMREGS 0xFF
4663
#define PCA9685_MAXCHAN 0x10
4764

4865
#define LED_FULL (1 << 4)
66+
#define MODE1_RESTART (1 << 7)
4967
#define MODE1_SLEEP (1 << 4)
5068
#define MODE2_INVRT (1 << 4)
5169
#define MODE2_OUTDRV (1 << 2)
@@ -59,6 +77,8 @@ struct pca9685 {
5977
struct pwm_chip chip;
6078
struct regmap *regmap;
6179
int active_cnt;
80+
int duty_ns;
81+
int period_ns;
6282
};
6383

6484
static inline struct pca9685 *to_pca(struct pwm_chip *chip)
@@ -72,6 +92,47 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
7292
struct pca9685 *pca = to_pca(chip);
7393
unsigned long long duty;
7494
unsigned int reg;
95+
int prescale;
96+
97+
if (period_ns != pca->period_ns) {
98+
prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
99+
PCA9685_COUNTER_RANGE * 1000) - 1;
100+
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);
106+
107+
/* Change the chip-wide output frequency */
108+
regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
109+
110+
/* Wake the chip up */
111+
regmap_update_bits(pca->regmap, PCA9685_MODE1,
112+
MODE1_SLEEP, 0x0);
113+
114+
/* Wait 500us for the oscillator to be back up */
115+
udelay(500);
116+
117+
pca->period_ns = period_ns;
118+
119+
/*
120+
* If the duty cycle did not change, restart PWM with
121+
* the same duty cycle to period ratio and return.
122+
*/
123+
if (duty_ns == pca->duty_ns) {
124+
regmap_update_bits(pca->regmap, PCA9685_MODE1,
125+
MODE1_RESTART, 0x1);
126+
return 0;
127+
}
128+
} else {
129+
dev_err(chip->dev,
130+
"prescaler not set: period out of bounds!\n");
131+
return -EINVAL;
132+
}
133+
}
134+
135+
pca->duty_ns = duty_ns;
75136

76137
if (duty_ns < 1) {
77138
if (pwm->hwpwm >= PCA9685_MAXCHAN)
@@ -111,7 +172,7 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
111172
return 0;
112173
}
113174

114-
duty = 4096 * (unsigned long long)duty_ns;
175+
duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
115176
duty = DIV_ROUND_UP_ULL(duty, period_ns);
116177

117178
if (pwm->hwpwm >= PCA9685_MAXCHAN)
@@ -252,6 +313,8 @@ static int pca9685_pwm_probe(struct i2c_client *client,
252313
ret);
253314
return ret;
254315
}
316+
pca->duty_ns = 0;
317+
pca->period_ns = PCA9685_DEFAULT_PERIOD;
255318

256319
i2c_set_clientdata(client, pca);
257320

0 commit comments

Comments
 (0)