]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
leds: pca955x: Refactor with helper functions and renaming
authorEddie James <eajames@linux.ibm.com>
Wed, 12 Feb 2025 14:30:35 +0000 (08:30 -0600)
committerLee Jones <lee@kernel.org>
Thu, 20 Feb 2025 14:29:02 +0000 (14:29 +0000)
Add helper functions to clean up the code, and rename a few
oddly named functions and variables.

Signed-off-by: Eddie James <eajames@linux.ibm.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20250212143038.1416501-2-eajames@linux.ibm.com
Signed-off-by: Lee Jones <lee@kernel.org>
drivers/leds/leds-pca955x.c

index 94a9f8a54b35933d68cab6b64f1b9d7126c46740..4a7111a24ab49cf9cac0132a8b1a6683ef38cf9f 100644 (file)
@@ -124,13 +124,15 @@ struct pca955x_led {
        struct fwnode_handle    *fwnode;
 };
 
+#define led_to_pca955x(l)      container_of(l, struct pca955x_led, led_cdev)
+
 struct pca955x_platform_data {
        struct pca955x_led      *leds;
        int                     num_leds;
 };
 
 /* 8 bits per input register */
-static inline int pca95xx_num_input_regs(int bits)
+static inline int pca955x_num_input_regs(int bits)
 {
        return (bits + 7) / 8;
 }
@@ -145,6 +147,11 @@ static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
                ((state & 0x3) << (led_num << 1));
 }
 
+static inline int pca955x_ledstate(u8 ls, int led_num)
+{
+       return (ls >> (led_num << 1)) & 0x3;
+}
+
 /*
  * Write to frequency prescaler register, used to program the
  * period of the PWM output.  period = (PSCx + 1) / 38
@@ -152,7 +159,7 @@ static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
 static int pca955x_write_psc(struct i2c_client *client, int n, u8 val)
 {
        struct pca955x *pca955x = i2c_get_clientdata(client);
-       u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + (2 * n);
+       u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
        int ret;
 
        ret = i2c_smbus_write_byte_data(client, cmd, val);
@@ -172,7 +179,7 @@ static int pca955x_write_psc(struct i2c_client *client, int n, u8 val)
 static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
 {
        struct pca955x *pca955x = i2c_get_clientdata(client);
-       u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
+       u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
        int ret;
 
        ret = i2c_smbus_write_byte_data(client, cmd, val);
@@ -189,7 +196,7 @@ static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
 static int pca955x_write_ls(struct i2c_client *client, int n, u8 val)
 {
        struct pca955x *pca955x = i2c_get_clientdata(client);
-       u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
+       u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
        int ret;
 
        ret = i2c_smbus_write_byte_data(client, cmd, val);
@@ -206,7 +213,7 @@ static int pca955x_write_ls(struct i2c_client *client, int n, u8 val)
 static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
 {
        struct pca955x *pca955x = i2c_get_clientdata(client);
-       u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
+       u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
        int ret;
 
        ret = i2c_smbus_read_byte_data(client, cmd);
@@ -222,7 +229,7 @@ static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
 static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
 {
        struct pca955x *pca955x = i2c_get_clientdata(client);
-       u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
+       u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
        int ret;
 
        ret = i2c_smbus_read_byte_data(client, cmd);
@@ -237,9 +244,7 @@ static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
 
 static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
 {
-       struct pca955x_led *pca955x_led = container_of(led_cdev,
-                                                      struct pca955x_led,
-                                                      led_cdev);
+       struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
        struct pca955x *pca955x = pca955x_led->pca955x;
        u8 ls, pwm;
        int ret;
@@ -248,8 +253,7 @@ static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
        if (ret)
                return ret;
 
-       ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
-       switch (ls) {
+       switch (pca955x_ledstate(ls, pca955x_led->led_num % 4)) {
        case PCA955X_LS_LED_ON:
                ret = LED_FULL;
                break;
@@ -273,34 +277,28 @@ static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
 static int pca955x_led_set(struct led_classdev *led_cdev,
                            enum led_brightness value)
 {
-       struct pca955x_led *pca955x_led;
-       struct pca955x *pca955x;
+       struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
+       struct pca955x *pca955x = pca955x_led->pca955x;
+       int reg = pca955x_led->led_num / 4;
+       int bit = pca955x_led->led_num % 4;
        u8 ls;
-       int chip_ls;    /* which LSx to use (0-3 potentially) */
-       int ls_led;     /* which set of bits within LSx to use (0-3) */
        int ret;
 
-       pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
-       pca955x = pca955x_led->pca955x;
-
-       chip_ls = pca955x_led->led_num / 4;
-       ls_led = pca955x_led->led_num % 4;
-
        mutex_lock(&pca955x->lock);
 
-       ret = pca955x_read_ls(pca955x->client, chip_ls, &ls);
+       ret = pca955x_read_ls(pca955x->client, reg, &ls);
        if (ret)
                goto out;
 
        switch (value) {
        case LED_FULL:
-               ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
+               ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_ON);
                break;
        case LED_OFF:
-               ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
+               ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
                break;
        case LED_HALF:
-               ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
+               ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK0);
                break;
        default:
                /*
@@ -313,11 +311,11 @@ static int pca955x_led_set(struct led_classdev *led_cdev,
                ret = pca955x_write_pwm(pca955x->client, 1, 255 - value);
                if (ret)
                        goto out;
-               ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
+               ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK1);
                break;
        }
 
-       ret = pca955x_write_ls(pca955x->client, chip_ls, ls);
+       ret = pca955x_write_ls(pca955x->client, reg, ls);
 
 out:
        mutex_unlock(&pca955x->lock);