From: Eddie James <eajames@linux.ibm.com>
To: linux-leds@vger.kernel.org
Cc: lee@kernel.org, pavel@ucw.cz, andy.shevchenko@gmail.com,
eajames@linux.ibm.com
Subject: [PATCH v7 RESEND 1/4] leds: pca955x: Refactor with helper functions and renaming
Date: Thu, 30 Jan 2025 14:12:43 -0600 [thread overview]
Message-ID: <20250130201246.292079-2-eajames@linux.ibm.com> (raw)
In-Reply-To: <20250130201246.292079-1-eajames@linux.ibm.com>
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>
---
drivers/leds/leds-pca955x.c | 52 ++++++++++++++++++-------------------
1 file changed, 25 insertions(+), 27 deletions(-)
diff --git a/drivers/leds/leds-pca955x.c b/drivers/leds/leds-pca955x.c
index 94a9f8a54b359..4a7111a24ab49 100644
--- a/drivers/leds/leds-pca955x.c
+++ b/drivers/leds/leds-pca955x.c
@@ -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);
--
2.43.5
next prev parent reply other threads:[~2025-01-30 20:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-30 20:12 [PATCH v7 RESEND 0/4] leds: pca955x: Add HW blink support Eddie James
2025-01-30 20:12 ` Eddie James [this message]
2025-01-30 20:12 ` [PATCH v7 RESEND 2/4] leds: pca955x: Use pointers to driver data rather than I2C client Eddie James
2025-01-30 20:12 ` [PATCH v7 RESEND 3/4] leds: pca955x: Optimize probe led selection Eddie James
2025-02-10 17:43 ` Lee Jones
2025-01-30 20:12 ` [PATCH v7 RESEND 4/4] leds: pca955x: Add HW blink support Eddie James
2025-02-10 17:50 ` Lee Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250130201246.292079-2-eajames@linux.ibm.com \
--to=eajames@linux.ibm.com \
--cc=andy.shevchenko@gmail.com \
--cc=lee@kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=pavel@ucw.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.