Skip to content

Commit ca3362a

Browse files
Eddie Jameslag-linaro
authored andcommitted
leds: pca955x: Refactor with helper functions and renaming
Add helper functions to clean up the code, and rename a few oddly named functions and variables. Signed-off-by: Eddie James <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent 346e704 commit ca3362a

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

drivers/leds/leds-pca955x.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ struct pca955x_led {
124124
struct fwnode_handle *fwnode;
125125
};
126126

127+
#define led_to_pca955x(l) container_of(l, struct pca955x_led, led_cdev)
128+
127129
struct pca955x_platform_data {
128130
struct pca955x_led *leds;
129131
int num_leds;
130132
};
131133

132134
/* 8 bits per input register */
133-
static inline int pca95xx_num_input_regs(int bits)
135+
static inline int pca955x_num_input_regs(int bits)
134136
{
135137
return (bits + 7) / 8;
136138
}
@@ -145,14 +147,19 @@ static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
145147
((state & 0x3) << (led_num << 1));
146148
}
147149

150+
static inline int pca955x_ledstate(u8 ls, int led_num)
151+
{
152+
return (ls >> (led_num << 1)) & 0x3;
153+
}
154+
148155
/*
149156
* Write to frequency prescaler register, used to program the
150157
* period of the PWM output. period = (PSCx + 1) / 38
151158
*/
152159
static int pca955x_write_psc(struct i2c_client *client, int n, u8 val)
153160
{
154161
struct pca955x *pca955x = i2c_get_clientdata(client);
155-
u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + (2 * n);
162+
u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
156163
int ret;
157164

158165
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)
172179
static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
173180
{
174181
struct pca955x *pca955x = i2c_get_clientdata(client);
175-
u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
182+
u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
176183
int ret;
177184

178185
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)
189196
static int pca955x_write_ls(struct i2c_client *client, int n, u8 val)
190197
{
191198
struct pca955x *pca955x = i2c_get_clientdata(client);
192-
u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
199+
u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
193200
int ret;
194201

195202
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)
206213
static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
207214
{
208215
struct pca955x *pca955x = i2c_get_clientdata(client);
209-
u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
216+
u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
210217
int ret;
211218

212219
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)
222229
static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
223230
{
224231
struct pca955x *pca955x = i2c_get_clientdata(client);
225-
u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
232+
u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
226233
int ret;
227234

228235
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)
237244

238245
static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
239246
{
240-
struct pca955x_led *pca955x_led = container_of(led_cdev,
241-
struct pca955x_led,
242-
led_cdev);
247+
struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
243248
struct pca955x *pca955x = pca955x_led->pca955x;
244249
u8 ls, pwm;
245250
int ret;
@@ -248,8 +253,7 @@ static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
248253
if (ret)
249254
return ret;
250255

251-
ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
252-
switch (ls) {
256+
switch (pca955x_ledstate(ls, pca955x_led->led_num % 4)) {
253257
case PCA955X_LS_LED_ON:
254258
ret = LED_FULL;
255259
break;
@@ -273,34 +277,28 @@ static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
273277
static int pca955x_led_set(struct led_classdev *led_cdev,
274278
enum led_brightness value)
275279
{
276-
struct pca955x_led *pca955x_led;
277-
struct pca955x *pca955x;
280+
struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
281+
struct pca955x *pca955x = pca955x_led->pca955x;
282+
int reg = pca955x_led->led_num / 4;
283+
int bit = pca955x_led->led_num % 4;
278284
u8 ls;
279-
int chip_ls; /* which LSx to use (0-3 potentially) */
280-
int ls_led; /* which set of bits within LSx to use (0-3) */
281285
int ret;
282286

283-
pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
284-
pca955x = pca955x_led->pca955x;
285-
286-
chip_ls = pca955x_led->led_num / 4;
287-
ls_led = pca955x_led->led_num % 4;
288-
289287
mutex_lock(&pca955x->lock);
290288

291-
ret = pca955x_read_ls(pca955x->client, chip_ls, &ls);
289+
ret = pca955x_read_ls(pca955x->client, reg, &ls);
292290
if (ret)
293291
goto out;
294292

295293
switch (value) {
296294
case LED_FULL:
297-
ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
295+
ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_ON);
298296
break;
299297
case LED_OFF:
300-
ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
298+
ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
301299
break;
302300
case LED_HALF:
303-
ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
301+
ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK0);
304302
break;
305303
default:
306304
/*
@@ -313,11 +311,11 @@ static int pca955x_led_set(struct led_classdev *led_cdev,
313311
ret = pca955x_write_pwm(pca955x->client, 1, 255 - value);
314312
if (ret)
315313
goto out;
316-
ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
314+
ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK1);
317315
break;
318316
}
319317

320-
ret = pca955x_write_ls(pca955x->client, chip_ls, ls);
318+
ret = pca955x_write_ls(pca955x->client, reg, ls);
321319

322320
out:
323321
mutex_unlock(&pca955x->lock);

0 commit comments

Comments
 (0)