hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/drivers/pwm/pwm-pca9685.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Driver for PCA9685 16-channel 12-bit PWM LED controller
34 *
....@@ -5,18 +6,6 @@
56 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
67 *
78 * 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/>.
209 */
2110
2211 #include <linux/acpi.h>
....@@ -68,10 +57,14 @@
6857 #define PCA9685_NUMREGS 0xFF
6958 #define PCA9685_MAXCHAN 0x10
7059
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)
7568
7669 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
7770 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
....@@ -81,7 +74,6 @@
8174 struct pca9685 {
8275 struct pwm_chip chip;
8376 struct regmap *regmap;
84
- int duty_ns;
8577 int period_ns;
8678 #if IS_ENABLED(CONFIG_GPIOLIB)
8779 struct mutex lock;
....@@ -103,7 +95,7 @@
10395 mutex_lock(&pca->lock);
10496 if (pwm_idx >= PCA9685_MAXCHAN) {
10597 /*
106
- * "all LEDs" channel:
98
+ * "All LEDs" channel:
10799 * pretend already in use if any of the PWMs are requested
108100 */
109101 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
....@@ -112,7 +104,7 @@
112104 }
113105 } else {
114106 /*
115
- * regular channel:
107
+ * Regular channel:
116108 * pretend already in use if the "all LEDs" channel is requested
117109 */
118110 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
....@@ -182,7 +174,7 @@
182174 unsigned int offset)
183175 {
184176 /* Always out */
185
- return 0;
177
+ return GPIO_LINE_DIRECTION_OUT;
186178 }
187179
188180 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
....@@ -269,7 +261,7 @@
269261 if (prescale >= PCA9685_PRESCALE_MIN &&
270262 prescale <= PCA9685_PRESCALE_MAX) {
271263 /*
272
- * putting the chip briefly into SLEEP mode
264
+ * Putting the chip briefly into SLEEP mode
273265 * at this point won't interfere with the
274266 * pm_runtime framework, because the pm_runtime
275267 * state is guaranteed active here.
....@@ -290,8 +282,6 @@
290282 return -EINVAL;
291283 }
292284 }
293
-
294
- pca->duty_ns = duty_ns;
295285
296286 if (duty_ns < 1) {
297287 if (pwm->hwpwm >= PCA9685_MAXCHAN)
....@@ -457,8 +447,8 @@
457447 const struct i2c_device_id *id)
458448 {
459449 struct pca9685 *pca;
450
+ unsigned int reg;
460451 int ret;
461
- int mode2;
462452
463453 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
464454 if (!pca)
....@@ -471,31 +461,35 @@
471461 ret);
472462 return ret;
473463 }
474
- pca->duty_ns = 0;
475464 pca->period_ns = PCA9685_DEFAULT_PERIOD;
476465
477466 i2c_set_clientdata(client, pca);
478467
479
- regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
468
+ regmap_read(pca->regmap, PCA9685_MODE2, &reg);
480469
481470 if (device_property_read_bool(&client->dev, "invert"))
482
- mode2 |= MODE2_INVRT;
471
+ reg |= MODE2_INVRT;
483472 else
484
- mode2 &= ~MODE2_INVRT;
473
+ reg &= ~MODE2_INVRT;
485474
486475 if (device_property_read_bool(&client->dev, "open-drain"))
487
- mode2 &= ~MODE2_OUTDRV;
476
+ reg &= ~MODE2_OUTDRV;
488477 else
489
- mode2 |= MODE2_OUTDRV;
478
+ reg |= MODE2_OUTDRV;
490479
491
- regmap_write(pca->regmap, PCA9685_MODE2, mode2);
480
+ regmap_write(pca->regmap, PCA9685_MODE2, reg);
492481
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, &reg);
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 */
494488 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
495489 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
496490
497491 pca->chip.ops = &pca9685_pwm_ops;
498
- /* add an extra channel for ALL_LED */
492
+ /* Add an extra channel for ALL_LED */
499493 pca->chip.npwm = PCA9685_MAXCHAN + 1;
500494
501495 pca->chip.dev = &client->dev;
....@@ -511,10 +505,10 @@
511505 return ret;
512506 }
513507
514
- /* the chip comes out of power-up in the active state */
508
+ /* The chip comes out of power-up in the active state */
515509 pm_runtime_set_active(&client->dev);
516510 /*
517
- * enable will put the chip into suspend, which is what we
511
+ * Enable will put the chip into suspend, which is what we
518512 * want as all outputs are disabled at this point
519513 */
520514 pm_runtime_enable(&client->dev);
....@@ -534,8 +528,7 @@
534528 return 0;
535529 }
536530
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)
539532 {
540533 struct i2c_client *client = to_i2c_client(dev);
541534 struct pca9685 *pca = i2c_get_clientdata(client);
....@@ -544,7 +537,7 @@
544537 return 0;
545538 }
546539
547
-static int pca9685_pwm_runtime_resume(struct device *dev)
540
+static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
548541 {
549542 struct i2c_client *client = to_i2c_client(dev);
550543 struct pca9685 *pca = i2c_get_clientdata(client);
....@@ -552,7 +545,6 @@
552545 pca9685_set_sleep_mode(pca, false);
553546 return 0;
554547 }
555
-#endif
556548
557549 static const struct i2c_device_id pca9685_id[] = {
558550 { "pca9685", 0 },