kernel: add upstream patches for pca955x led driver
[openwrt/staging/hauke.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 diff --git a/drivers/leds/leds-pca955x.c b/drivers/leds/leds-pca955x.c
15 index f0d841cb59fcc8..e47ba7c3b7c7d8 100644
16 --- a/drivers/leds/leds-pca955x.c
17 +++ b/drivers/leds/leds-pca955x.c
18 @@ -233,6 +233,57 @@ static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
19 return 0;
20 }
21
22 +static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
23 +{
24 + struct pca955x *pca955x = i2c_get_clientdata(client);
25 + u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
26 + int ret;
27 +
28 + ret = i2c_smbus_read_byte_data(client, cmd);
29 + if (ret < 0) {
30 + dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
31 + __func__, n, ret);
32 + return ret;
33 + }
34 + *val = (u8)ret;
35 + return 0;
36 +}
37 +
38 +static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
39 +{
40 + struct pca955x_led *pca955x_led = container_of(led_cdev,
41 + struct pca955x_led,
42 + led_cdev);
43 + struct pca955x *pca955x = pca955x_led->pca955x;
44 + u8 ls, pwm;
45 + int ret;
46 +
47 + ret = pca955x_read_ls(pca955x->client, pca955x_led->led_num / 4, &ls);
48 + if (ret)
49 + return ret;
50 +
51 + ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
52 + switch (ls) {
53 + case PCA955X_LS_LED_ON:
54 + ret = LED_FULL;
55 + break;
56 + case PCA955X_LS_LED_OFF:
57 + ret = LED_OFF;
58 + break;
59 + case PCA955X_LS_BLINK0:
60 + ret = LED_HALF;
61 + break;
62 + case PCA955X_LS_BLINK1:
63 + ret = pca955x_read_pwm(pca955x->client, 1, &pwm);
64 + if (ret)
65 + return ret;
66 + ret = 255 - pwm;
67 + break;
68 + }
69 +
70 + return ret;
71 +}
72 +
73 static int pca955x_led_set(struct led_classdev *led_cdev,
74 enum led_brightness value)
75 {
76 @@ -512,6 +563,7 @@ static int pca955x_probe(struct i2c_client *client,
77
78 led->name = pca955x_led->name;
79 led->brightness_set_blocking = pca955x_led_set;
80 + led->brightness_get = pca955x_led_get;
81
82 err = devm_led_classdev_register(&client->dev, led);
83 if (err)