Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pwm: pca9685: Restrict period change for enabled PWMs

Previously, the last used PWM channel could change the global prescale
setting, even if other channels are already in use.

Fix it by only allowing the first enabled PWM to change the global
chip-wide prescale setting. If there is more than one channel in use,
the prescale settings resulting from the chosen periods must match.

GPIOs do not count as enabled PWMs as they are not using the prescaler
and can't change it.

Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>

authored by

Clemens Gruber and committed by
Thierry Reding
6d6e7050 ae16db1f

+64 -10
+64 -10
drivers/pwm/pwm-pca9685.c
··· 23 23 #include <linux/bitmap.h> 24 24 25 25 /* 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 30 - * counter range. 26 + * Because the PCA9685 has only one prescaler per chip, only the first channel 27 + * that is enabled is allowed to change the prescale register. 28 + * PWM channels requested afterwards must use a period that results in the same 29 + * prescale setting as the one set by the first requested channel. 30 + * GPIOs do not count as enabled PWMs as they are not using the prescaler. 31 31 */ 32 32 33 33 #define PCA9685_MODE1 0x00 ··· 78 78 struct pca9685 { 79 79 struct pwm_chip chip; 80 80 struct regmap *regmap; 81 - #if IS_ENABLED(CONFIG_GPIOLIB) 82 81 struct mutex lock; 82 + DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1); 83 + #if IS_ENABLED(CONFIG_GPIOLIB) 83 84 struct gpio_chip gpio; 84 85 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1); 85 86 #endif ··· 89 88 static inline struct pca9685 *to_pca(struct pwm_chip *chip) 90 89 { 91 90 return container_of(chip, struct pca9685, chip); 91 + } 92 + 93 + /* This function is supposed to be called with the lock mutex held */ 94 + static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channel) 95 + { 96 + /* No PWM enabled: Change allowed */ 97 + if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1)) 98 + return true; 99 + /* More than one PWM enabled: Change not allowed */ 100 + if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1) 101 + return false; 102 + /* 103 + * Only one PWM enabled: Change allowed if the PWM about to 104 + * be changed is the one that is already enabled 105 + */ 106 + return test_bit(channel, pca->pwms_enabled); 92 107 } 93 108 94 109 /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */ ··· 286 269 { 287 270 struct device *dev = pca->chip.dev; 288 271 289 - mutex_init(&pca->lock); 290 - 291 272 pca->gpio.label = dev_name(dev); 292 273 pca->gpio.parent = dev; 293 274 pca->gpio.request = pca9685_pwm_gpio_request; ··· 329 314 } 330 315 } 331 316 332 - static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 333 - const struct pwm_state *state) 317 + static int __pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 318 + const struct pwm_state *state) 334 319 { 335 320 struct pca9685 *pca = to_pca(chip); 336 321 unsigned long long duty, prescale; ··· 353 338 354 339 regmap_read(pca->regmap, PCA9685_PRESCALE, &val); 355 340 if (prescale != val) { 341 + if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { 342 + dev_err(chip->dev, 343 + "pwm not changed: periods of enabled pwms must match!\n"); 344 + return -EBUSY; 345 + } 346 + 356 347 /* 357 348 * Putting the chip briefly into SLEEP mode 358 349 * at this point won't interfere with the ··· 379 358 duty = DIV_ROUND_UP_ULL(duty, state->period); 380 359 pca9685_pwm_set_duty(pca, pwm->hwpwm, duty); 381 360 return 0; 361 + } 362 + 363 + static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 364 + const struct pwm_state *state) 365 + { 366 + struct pca9685 *pca = to_pca(chip); 367 + int ret; 368 + 369 + mutex_lock(&pca->lock); 370 + ret = __pca9685_pwm_apply(chip, pwm, state); 371 + if (ret == 0) { 372 + if (state->enabled) 373 + set_bit(pwm->hwpwm, pca->pwms_enabled); 374 + else 375 + clear_bit(pwm->hwpwm, pca->pwms_enabled); 376 + } 377 + mutex_unlock(&pca->lock); 378 + 379 + return ret; 382 380 } 383 381 384 382 static void pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, ··· 441 401 442 402 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm)) 443 403 return -EBUSY; 404 + 405 + if (pwm->hwpwm < PCA9685_MAXCHAN) { 406 + /* PWMs - except the "all LEDs" channel - default to enabled */ 407 + mutex_lock(&pca->lock); 408 + set_bit(pwm->hwpwm, pca->pwms_enabled); 409 + mutex_unlock(&pca->lock); 410 + } 411 + 444 412 pm_runtime_get_sync(chip->dev); 445 413 446 414 return 0; ··· 458 410 { 459 411 struct pca9685 *pca = to_pca(chip); 460 412 413 + mutex_lock(&pca->lock); 461 414 pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); 415 + clear_bit(pwm->hwpwm, pca->pwms_enabled); 416 + mutex_unlock(&pca->lock); 417 + 462 418 pm_runtime_put(chip->dev); 463 419 pca9685_pwm_clear_inuse(pca, pwm->hwpwm); 464 420 } ··· 502 450 } 503 451 504 452 i2c_set_clientdata(client, pca); 453 + 454 + mutex_init(&pca->lock); 505 455 506 456 regmap_read(pca->regmap, PCA9685_MODE2, &reg); 507 457