Skip to content

Commit 7086625

Browse files
Eddie Jamespavelmachek
authored andcommitted
leds: pca955x: Add brightness_get function
Add a function to fetch the state of the hardware LED. Signed-off-by: Eddie James <[email protected]> Signed-off-by: Pavel Machek <[email protected]>
1 parent 2420ae0 commit 7086625

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

drivers/leds/leds-pca955x.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,57 @@ static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
233233
return 0;
234234
}
235235

236+
static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
237+
{
238+
struct pca955x *pca955x = i2c_get_clientdata(client);
239+
u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
240+
int ret;
241+
242+
ret = i2c_smbus_read_byte_data(client, cmd);
243+
if (ret < 0) {
244+
dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
245+
__func__, n, ret);
246+
return ret;
247+
}
248+
*val = (u8)ret;
249+
return 0;
250+
}
251+
252+
static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
253+
{
254+
struct pca955x_led *pca955x_led = container_of(led_cdev,
255+
struct pca955x_led,
256+
led_cdev);
257+
struct pca955x *pca955x = pca955x_led->pca955x;
258+
u8 ls, pwm;
259+
int ret;
260+
261+
ret = pca955x_read_ls(pca955x->client, pca955x_led->led_num / 4, &ls);
262+
if (ret)
263+
return ret;
264+
265+
ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
266+
switch (ls) {
267+
case PCA955X_LS_LED_ON:
268+
ret = LED_FULL;
269+
break;
270+
case PCA955X_LS_LED_OFF:
271+
ret = LED_OFF;
272+
break;
273+
case PCA955X_LS_BLINK0:
274+
ret = LED_HALF;
275+
break;
276+
case PCA955X_LS_BLINK1:
277+
ret = pca955x_read_pwm(pca955x->client, 1, &pwm);
278+
if (ret)
279+
return ret;
280+
ret = 255 - pwm;
281+
break;
282+
}
283+
284+
return ret;
285+
}
286+
236287
static int pca955x_led_set(struct led_classdev *led_cdev,
237288
enum led_brightness value)
238289
{
@@ -512,6 +563,7 @@ static int pca955x_probe(struct i2c_client *client,
512563

513564
led->name = pca955x_led->name;
514565
led->brightness_set_blocking = pca955x_led_set;
566+
led->brightness_get = pca955x_led_get;
515567

516568
err = devm_led_classdev_register(&client->dev, led);
517569
if (err)

0 commit comments

Comments
 (0)