5460881cca182f2bc9d63c5418fc7fc9d5bf750e
[openwrt/staging/jow.git] / target / linux / generic / backport-5.15 / 830-v6.7-1-leds-turris-omnia-Do-not-use-SMBUS-calls.patch
1 From 28350bc0ac77e17365ba87d3edb2db0a79c98fdd Mon Sep 17 00:00:00 2001
2 From: Marek Behún <kabel@kernel.org>
3 Date: Mon, 18 Sep 2023 18:11:01 +0200
4 Subject: leds: turris-omnia: Do not use SMBUS calls
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 The leds-turris-omnia driver uses three function for I2C access:
10 - i2c_smbus_write_byte_data() and i2c_smbus_read_byte_data(), which
11 cause an emulated SMBUS transfer,
12 - i2c_master_send(), which causes an ordinary I2C transfer.
13
14 The Turris Omnia MCU LED controller is not semantically SMBUS, it
15 operates as a simple I2C bus. It does not implement any of the SMBUS
16 specific features, like PEC, or procedure calls, or anything. Moreover
17 the I2C controller driver also does not implement SMBUS, and so the
18 emulated SMBUS procedure from drivers/i2c/i2c-core-smbus.c is used for
19 the SMBUS calls, which gives an unnecessary overhead.
20
21 When I first wrote the driver, I was unaware of these facts, and I
22 simply used the first function that worked.
23
24 Drop the I2C SMBUS calls and instead use simple I2C transfers.
25
26 Fixes: 089381b27abe ("leds: initial support for Turris Omnia LEDs")
27 Signed-off-by: Marek Behún <kabel@kernel.org>
28 Link: https://lore.kernel.org/r/20230918161104.20860-2-kabel@kernel.org
29 Signed-off-by: Lee Jones <lee@kernel.org>
30 ---
31 drivers/leds/leds-turris-omnia.c | 54 +++++++++++++++++++++++++++++++---------
32 1 file changed, 42 insertions(+), 12 deletions(-)
33
34 --- a/drivers/leds/leds-turris-omnia.c
35 +++ b/drivers/leds/leds-turris-omnia.c
36 @@ -2,7 +2,7 @@
37 /*
38 * CZ.NIC's Turris Omnia LEDs driver
39 *
40 - * 2020 by Marek Behún <kabel@kernel.org>
41 + * 2020, 2023 by Marek Behún <kabel@kernel.org>
42 */
43
44 #include <linux/i2c.h>
45 @@ -41,6 +41,37 @@ struct omnia_leds {
46 struct omnia_led leds[];
47 };
48
49 +static int omnia_cmd_write_u8(const struct i2c_client *client, u8 cmd, u8 val)
50 +{
51 + u8 buf[2] = { cmd, val };
52 +
53 + return i2c_master_send(client, buf, sizeof(buf));
54 +}
55 +
56 +static int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd)
57 +{
58 + struct i2c_msg msgs[2];
59 + u8 reply;
60 + int ret;
61 +
62 + msgs[0].addr = client->addr;
63 + msgs[0].flags = 0;
64 + msgs[0].len = 1;
65 + msgs[0].buf = &cmd;
66 + msgs[1].addr = client->addr;
67 + msgs[1].flags = I2C_M_RD;
68 + msgs[1].len = 1;
69 + msgs[1].buf = &reply;
70 +
71 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
72 + if (likely(ret == ARRAY_SIZE(msgs)))
73 + return reply;
74 + else if (ret < 0)
75 + return ret;
76 + else
77 + return -EIO;
78 +}
79 +
80 static int omnia_led_brightness_set_blocking(struct led_classdev *cdev,
81 enum led_brightness brightness)
82 {
83 @@ -64,7 +95,7 @@ static int omnia_led_brightness_set_bloc
84 if (buf[2] || buf[3] || buf[4])
85 state |= CMD_LED_STATE_ON;
86
87 - ret = i2c_smbus_write_byte_data(leds->client, CMD_LED_STATE, state);
88 + ret = omnia_cmd_write_u8(leds->client, CMD_LED_STATE, state);
89 if (ret >= 0 && (state & CMD_LED_STATE_ON))
90 ret = i2c_master_send(leds->client, buf, 5);
91
92 @@ -114,9 +145,9 @@ static int omnia_led_register(struct i2c
93 cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
94
95 /* put the LED into software mode */
96 - ret = i2c_smbus_write_byte_data(client, CMD_LED_MODE,
97 - CMD_LED_MODE_LED(led->reg) |
98 - CMD_LED_MODE_USER);
99 + ret = omnia_cmd_write_u8(client, CMD_LED_MODE,
100 + CMD_LED_MODE_LED(led->reg) |
101 + CMD_LED_MODE_USER);
102 if (ret < 0) {
103 dev_err(dev, "Cannot set LED %pOF to software mode: %i\n", np,
104 ret);
105 @@ -124,8 +155,8 @@ static int omnia_led_register(struct i2c
106 }
107
108 /* disable the LED */
109 - ret = i2c_smbus_write_byte_data(client, CMD_LED_STATE,
110 - CMD_LED_STATE_LED(led->reg));
111 + ret = omnia_cmd_write_u8(client, CMD_LED_STATE,
112 + CMD_LED_STATE_LED(led->reg));
113 if (ret < 0) {
114 dev_err(dev, "Cannot set LED %pOF brightness: %i\n", np, ret);
115 return ret;
116 @@ -158,7 +189,7 @@ static ssize_t brightness_show(struct de
117 struct i2c_client *client = to_i2c_client(dev);
118 int ret;
119
120 - ret = i2c_smbus_read_byte_data(client, CMD_LED_GET_BRIGHTNESS);
121 + ret = omnia_cmd_read_u8(client, CMD_LED_GET_BRIGHTNESS);
122
123 if (ret < 0)
124 return ret;
125 @@ -179,8 +210,7 @@ static ssize_t brightness_store(struct d
126 if (brightness > 100)
127 return -EINVAL;
128
129 - ret = i2c_smbus_write_byte_data(client, CMD_LED_SET_BRIGHTNESS,
130 - (u8)brightness);
131 + ret = omnia_cmd_write_u8(client, CMD_LED_SET_BRIGHTNESS, brightness);
132
133 return ret < 0 ? ret : count;
134 }
135 @@ -238,8 +268,8 @@ static int omnia_leds_remove(struct i2c_
136 u8 buf[5];
137
138 /* put all LEDs into default (HW triggered) mode */
139 - i2c_smbus_write_byte_data(client, CMD_LED_MODE,
140 - CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
141 + omnia_cmd_write_u8(client, CMD_LED_MODE,
142 + CMD_LED_MODE_LED(OMNIA_BOARD_LEDS));
143
144 /* set all LEDs color to [255, 255, 255] */
145 buf[0] = CMD_LED_COLOR;