| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Driver for PCA9685 16-channel 12-bit PWM LED controller |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com> |
|---|
| 6 | 7 | * |
|---|
| 7 | 8 | * based on the pwm-twl-led.c driver |
|---|
| 8 | | - * |
|---|
| 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. |
|---|
| 12 | | - * |
|---|
| 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 |
|---|
| 16 | | - * more details. |
|---|
| 17 | | - * |
|---|
| 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/>. |
|---|
| 20 | 9 | */ |
|---|
| 21 | 10 | |
|---|
| 22 | 11 | #include <linux/acpi.h> |
|---|
| .. | .. |
|---|
| 68 | 57 | #define PCA9685_NUMREGS 0xFF |
|---|
| 69 | 58 | #define PCA9685_MAXCHAN 0x10 |
|---|
| 70 | 59 | |
|---|
| 71 | | -#define LED_FULL (1 << 4) |
|---|
| 72 | | -#define MODE1_SLEEP (1 << 4) |
|---|
| 73 | | -#define MODE2_INVRT (1 << 4) |
|---|
| 74 | | -#define MODE2_OUTDRV (1 << 2) |
|---|
| 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) |
|---|
| 75 | 68 | |
|---|
| 76 | 69 | #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N))) |
|---|
| 77 | 70 | #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N))) |
|---|
| .. | .. |
|---|
| 81 | 74 | struct pca9685 { |
|---|
| 82 | 75 | struct pwm_chip chip; |
|---|
| 83 | 76 | struct regmap *regmap; |
|---|
| 84 | | - int duty_ns; |
|---|
| 85 | 77 | int period_ns; |
|---|
| 86 | 78 | #if IS_ENABLED(CONFIG_GPIOLIB) |
|---|
| 87 | 79 | struct mutex lock; |
|---|
| .. | .. |
|---|
| 103 | 95 | mutex_lock(&pca->lock); |
|---|
| 104 | 96 | if (pwm_idx >= PCA9685_MAXCHAN) { |
|---|
| 105 | 97 | /* |
|---|
| 106 | | - * "all LEDs" channel: |
|---|
| 98 | + * "All LEDs" channel: |
|---|
| 107 | 99 | * pretend already in use if any of the PWMs are requested |
|---|
| 108 | 100 | */ |
|---|
| 109 | 101 | if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) { |
|---|
| .. | .. |
|---|
| 112 | 104 | } |
|---|
| 113 | 105 | } else { |
|---|
| 114 | 106 | /* |
|---|
| 115 | | - * regular channel: |
|---|
| 107 | + * Regular channel: |
|---|
| 116 | 108 | * pretend already in use if the "all LEDs" channel is requested |
|---|
| 117 | 109 | */ |
|---|
| 118 | 110 | if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) { |
|---|
| .. | .. |
|---|
| 182 | 174 | unsigned int offset) |
|---|
| 183 | 175 | { |
|---|
| 184 | 176 | /* Always out */ |
|---|
| 185 | | - return 0; |
|---|
| 177 | + return GPIO_LINE_DIRECTION_OUT; |
|---|
| 186 | 178 | } |
|---|
| 187 | 179 | |
|---|
| 188 | 180 | static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio, |
|---|
| .. | .. |
|---|
| 269 | 261 | if (prescale >= PCA9685_PRESCALE_MIN && |
|---|
| 270 | 262 | prescale <= PCA9685_PRESCALE_MAX) { |
|---|
| 271 | 263 | /* |
|---|
| 272 | | - * putting the chip briefly into SLEEP mode |
|---|
| 264 | + * Putting the chip briefly into SLEEP mode |
|---|
| 273 | 265 | * at this point won't interfere with the |
|---|
| 274 | 266 | * pm_runtime framework, because the pm_runtime |
|---|
| 275 | 267 | * state is guaranteed active here. |
|---|
| .. | .. |
|---|
| 290 | 282 | return -EINVAL; |
|---|
| 291 | 283 | } |
|---|
| 292 | 284 | } |
|---|
| 293 | | - |
|---|
| 294 | | - pca->duty_ns = duty_ns; |
|---|
| 295 | 285 | |
|---|
| 296 | 286 | if (duty_ns < 1) { |
|---|
| 297 | 287 | if (pwm->hwpwm >= PCA9685_MAXCHAN) |
|---|
| .. | .. |
|---|
| 457 | 447 | const struct i2c_device_id *id) |
|---|
| 458 | 448 | { |
|---|
| 459 | 449 | struct pca9685 *pca; |
|---|
| 450 | + unsigned int reg; |
|---|
| 460 | 451 | int ret; |
|---|
| 461 | | - int mode2; |
|---|
| 462 | 452 | |
|---|
| 463 | 453 | pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL); |
|---|
| 464 | 454 | if (!pca) |
|---|
| .. | .. |
|---|
| 471 | 461 | ret); |
|---|
| 472 | 462 | return ret; |
|---|
| 473 | 463 | } |
|---|
| 474 | | - pca->duty_ns = 0; |
|---|
| 475 | 464 | pca->period_ns = PCA9685_DEFAULT_PERIOD; |
|---|
| 476 | 465 | |
|---|
| 477 | 466 | i2c_set_clientdata(client, pca); |
|---|
| 478 | 467 | |
|---|
| 479 | | - regmap_read(pca->regmap, PCA9685_MODE2, &mode2); |
|---|
| 468 | + regmap_read(pca->regmap, PCA9685_MODE2, ®); |
|---|
| 480 | 469 | |
|---|
| 481 | 470 | if (device_property_read_bool(&client->dev, "invert")) |
|---|
| 482 | | - mode2 |= MODE2_INVRT; |
|---|
| 471 | + reg |= MODE2_INVRT; |
|---|
| 483 | 472 | else |
|---|
| 484 | | - mode2 &= ~MODE2_INVRT; |
|---|
| 473 | + reg &= ~MODE2_INVRT; |
|---|
| 485 | 474 | |
|---|
| 486 | 475 | if (device_property_read_bool(&client->dev, "open-drain")) |
|---|
| 487 | | - mode2 &= ~MODE2_OUTDRV; |
|---|
| 476 | + reg &= ~MODE2_OUTDRV; |
|---|
| 488 | 477 | else |
|---|
| 489 | | - mode2 |= MODE2_OUTDRV; |
|---|
| 478 | + reg |= MODE2_OUTDRV; |
|---|
| 490 | 479 | |
|---|
| 491 | | - regmap_write(pca->regmap, PCA9685_MODE2, mode2); |
|---|
| 480 | + regmap_write(pca->regmap, PCA9685_MODE2, reg); |
|---|
| 492 | 481 | |
|---|
| 493 | | - /* clear all "full off" bits */ |
|---|
| 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); |
|---|
| 486 | + |
|---|
| 487 | + /* Clear all "full off" bits */ |
|---|
| 494 | 488 | regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0); |
|---|
| 495 | 489 | regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0); |
|---|
| 496 | 490 | |
|---|
| 497 | 491 | pca->chip.ops = &pca9685_pwm_ops; |
|---|
| 498 | | - /* add an extra channel for ALL_LED */ |
|---|
| 492 | + /* Add an extra channel for ALL_LED */ |
|---|
| 499 | 493 | pca->chip.npwm = PCA9685_MAXCHAN + 1; |
|---|
| 500 | 494 | |
|---|
| 501 | 495 | pca->chip.dev = &client->dev; |
|---|
| .. | .. |
|---|
| 511 | 505 | return ret; |
|---|
| 512 | 506 | } |
|---|
| 513 | 507 | |
|---|
| 514 | | - /* the chip comes out of power-up in the active state */ |
|---|
| 508 | + /* The chip comes out of power-up in the active state */ |
|---|
| 515 | 509 | pm_runtime_set_active(&client->dev); |
|---|
| 516 | 510 | /* |
|---|
| 517 | | - * enable will put the chip into suspend, which is what we |
|---|
| 511 | + * Enable will put the chip into suspend, which is what we |
|---|
| 518 | 512 | * want as all outputs are disabled at this point |
|---|
| 519 | 513 | */ |
|---|
| 520 | 514 | pm_runtime_enable(&client->dev); |
|---|
| .. | .. |
|---|
| 534 | 528 | return 0; |
|---|
| 535 | 529 | } |
|---|
| 536 | 530 | |
|---|
| 537 | | -#ifdef CONFIG_PM |
|---|
| 538 | | -static int pca9685_pwm_runtime_suspend(struct device *dev) |
|---|
| 531 | +static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev) |
|---|
| 539 | 532 | { |
|---|
| 540 | 533 | struct i2c_client *client = to_i2c_client(dev); |
|---|
| 541 | 534 | struct pca9685 *pca = i2c_get_clientdata(client); |
|---|
| .. | .. |
|---|
| 544 | 537 | return 0; |
|---|
| 545 | 538 | } |
|---|
| 546 | 539 | |
|---|
| 547 | | -static int pca9685_pwm_runtime_resume(struct device *dev) |
|---|
| 540 | +static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev) |
|---|
| 548 | 541 | { |
|---|
| 549 | 542 | struct i2c_client *client = to_i2c_client(dev); |
|---|
| 550 | 543 | struct pca9685 *pca = i2c_get_clientdata(client); |
|---|
| .. | .. |
|---|
| 552 | 545 | pca9685_set_sleep_mode(pca, false); |
|---|
| 553 | 546 | return 0; |
|---|
| 554 | 547 | } |
|---|
| 555 | | -#endif |
|---|
| 556 | 548 | |
|---|
| 557 | 549 | static const struct i2c_device_id pca9685_id[] = { |
|---|
| 558 | 550 | { "pca9685", 0 }, |
|---|