bdb095dc2e69400f3832b1906fa875b3ca6210bf
[openwrt/openwrt.git] / target / linux / generic / backport-5.10 / 841-v5.15-leds-pca955x-add-brightness-get-function.patch
1 From 7086625fde6538b2c0623eb767ad23c7ac3d7f3a Mon Sep 17 00:00:00 2001
2 From: Eddie James <eajames@linux.ibm.com>
3 Date: Fri, 16 Jul 2021 17:03:28 -0500
4 Subject: [PATCH] leds: pca955x: Add brightness_get function
5
6 Add a function to fetch the state of the hardware LED.
7
8 Signed-off-by: Eddie James <eajames@linux.ibm.com>
9 Signed-off-by: Pavel Machek <pavel@ucw.cz>
10 ---
11 drivers/leds/leds-pca955x.c | 52 +++++++++++++++++++++++++++++++++++++
12 1 file changed, 52 insertions(+)
13
14 --- a/drivers/leds/leds-pca955x.c
15 +++ b/drivers/leds/leds-pca955x.c
16 @@ -233,6 +233,57 @@ static int pca955x_read_ls(struct i2c_cl
17 return 0;
18 }
19
20 +static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
21 +{
22 + struct pca955x *pca955x = i2c_get_clientdata(client);
23 + u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
24 + int ret;
25 +
26 + ret = i2c_smbus_read_byte_data(client, cmd);
27 + if (ret < 0) {
28 + dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
29 + __func__, n, ret);
30 + return ret;
31 + }
32 + *val = (u8)ret;
33 + return 0;
34 +}
35 +
36 +static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
37 +{
38 + struct pca955x_led *pca955x_led = container_of(led_cdev,
39 + struct pca955x_led,
40 + led_cdev);
41 + struct pca955x *pca955x = pca955x_led->pca955x;
42 + u8 ls, pwm;
43 + int ret;
44 +
45 + ret = pca955x_read_ls(pca955x->client, pca955x_led->led_num / 4, &ls);
46 + if (ret)
47 + return ret;
48 +
49 + ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
50 + switch (ls) {
51 + case PCA955X_LS_LED_ON:
52 + ret = LED_FULL;
53 + break;
54 + case PCA955X_LS_LED_OFF:
55 + ret = LED_OFF;
56 + break;
57 + case PCA955X_LS_BLINK0:
58 + ret = LED_HALF;
59 + break;
60 + case PCA955X_LS_BLINK1:
61 + ret = pca955x_read_pwm(pca955x->client, 1, &pwm);
62 + if (ret)
63 + return ret;
64 + ret = 255 - pwm;
65 + break;
66 + }
67 +
68 + return ret;
69 +}
70 +
71 static int pca955x_led_set(struct led_classdev *led_cdev,
72 enum led_brightness value)
73 {
74 @@ -512,6 +563,7 @@ static int pca955x_probe(struct i2c_clie
75
76 led->name = pca955x_led->name;
77 led->brightness_set_blocking = pca955x_led_set;
78 + led->brightness_get = pca955x_led_get;
79
80 err = devm_led_classdev_register(&client->dev, led);
81 if (err)