Skip to content

Commit 3b384bd

Browse files
furquan-googdtor
authored andcommitted
Input: raydium_ts_i2c - do not split tx transactions
Raydium device does not like splitting of tx transactions into multiple messages - one for the register address and one for the actual data. This results in incorrect behavior on the device side. This change updates raydium_i2c_read and raydium_i2c_write to create i2c_msg arrays separately and passes those arrays into raydium_i2c_xfer which decides based on the address whether the bank switch command should be sent. The bank switch header is still added by raydium_i2c_read and raydium_i2c_write to ensure that all these operations are performed as part of a single I2C transfer. It guarantees that no other transactions are initiated to any other device on the same bus after the bank switch command is sent. Signed-off-by: Furquan Shaikh <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 8c3b55a commit 3b384bd

File tree

1 file changed

+88
-38
lines changed

1 file changed

+88
-38
lines changed

drivers/input/touchscreen/raydium_i2c_ts.c

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -137,45 +137,25 @@ struct raydium_data {
137137
bool wake_irq_enabled;
138138
};
139139

140-
static int raydium_i2c_xfer(struct i2c_client *client,
141-
u32 addr, void *data, size_t len, bool is_read)
142-
{
143-
struct raydium_bank_switch_header {
144-
u8 cmd;
145-
__be32 be_addr;
146-
} __packed header = {
147-
.cmd = RM_CMD_BANK_SWITCH,
148-
.be_addr = cpu_to_be32(addr),
149-
};
150-
151-
u8 reg_addr = addr & 0xff;
152-
153-
struct i2c_msg xfer[] = {
154-
{
155-
.addr = client->addr,
156-
.len = sizeof(header),
157-
.buf = (u8 *)&header,
158-
},
159-
{
160-
.addr = client->addr,
161-
.len = 1,
162-
.buf = &reg_addr,
163-
},
164-
{
165-
.addr = client->addr,
166-
.len = len,
167-
.buf = data,
168-
.flags = is_read ? I2C_M_RD : 0,
169-
}
170-
};
140+
/*
141+
* Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
142+
* raydium_i2c_{read|send} below.
143+
*/
144+
struct __packed raydium_bank_switch_header {
145+
u8 cmd;
146+
__be32 be_addr;
147+
};
171148

149+
static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
150+
struct i2c_msg *xfer, size_t xfer_count)
151+
{
152+
int ret;
172153
/*
173154
* If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
174155
* sent first. Else, skip the header i.e. xfer[0].
175156
*/
176157
int xfer_start_idx = (addr > 0xff) ? 0 : 1;
177-
size_t xfer_count = ARRAY_SIZE(xfer) - xfer_start_idx;
178-
int ret;
158+
xfer_count -= xfer_start_idx;
179159

180160
ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
181161
if (likely(ret == xfer_count))
@@ -189,10 +169,46 @@ static int raydium_i2c_send(struct i2c_client *client,
189169
{
190170
int tries = 0;
191171
int error;
172+
u8 *tx_buf;
173+
u8 reg_addr = addr & 0xff;
174+
175+
tx_buf = kmalloc(len + 1, GFP_KERNEL);
176+
if (!tx_buf)
177+
return -ENOMEM;
178+
179+
tx_buf[0] = reg_addr;
180+
memcpy(tx_buf + 1, data, len);
192181

193182
do {
194-
error = raydium_i2c_xfer(client, addr, (void *)data, len,
195-
false);
183+
struct raydium_bank_switch_header header = {
184+
.cmd = RM_CMD_BANK_SWITCH,
185+
.be_addr = cpu_to_be32(addr),
186+
};
187+
188+
/*
189+
* Perform as a single i2c_transfer transaction to ensure that
190+
* no other I2C transactions are initiated on the bus to any
191+
* other device in between. Initiating transacations to other
192+
* devices after RM_CMD_BANK_SWITCH is sent is known to cause
193+
* issues. This is also why regmap infrastructure cannot be used
194+
* for this driver. Regmap handles page(bank) switch and reads
195+
* as separate i2c_transfer() operations. This can result in
196+
* problems if the Raydium device is on a shared I2C bus.
197+
*/
198+
struct i2c_msg xfer[] = {
199+
{
200+
.addr = client->addr,
201+
.len = sizeof(header),
202+
.buf = (u8 *)&header,
203+
},
204+
{
205+
.addr = client->addr,
206+
.len = len + 1,
207+
.buf = tx_buf,
208+
},
209+
};
210+
211+
error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
196212
if (likely(!error))
197213
return 0;
198214

@@ -206,12 +222,46 @@ static int raydium_i2c_send(struct i2c_client *client,
206222
static int raydium_i2c_read(struct i2c_client *client,
207223
u32 addr, void *data, size_t len)
208224
{
209-
size_t xfer_len;
210225
int error;
211226

212227
while (len) {
213-
xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
214-
error = raydium_i2c_xfer(client, addr, data, xfer_len, true);
228+
u8 reg_addr = addr & 0xff;
229+
struct raydium_bank_switch_header header = {
230+
.cmd = RM_CMD_BANK_SWITCH,
231+
.be_addr = cpu_to_be32(addr),
232+
};
233+
size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
234+
235+
/*
236+
* Perform as a single i2c_transfer transaction to ensure that
237+
* no other I2C transactions are initiated on the bus to any
238+
* other device in between. Initiating transacations to other
239+
* devices after RM_CMD_BANK_SWITCH is sent is known to cause
240+
* issues. This is also why regmap infrastructure cannot be used
241+
* for this driver. Regmap handles page(bank) switch and writes
242+
* as separate i2c_transfer() operations. This can result in
243+
* problems if the Raydium device is on a shared I2C bus.
244+
*/
245+
struct i2c_msg xfer[] = {
246+
{
247+
.addr = client->addr,
248+
.len = sizeof(header),
249+
.buf = (u8 *)&header,
250+
},
251+
{
252+
.addr = client->addr,
253+
.len = 1,
254+
.buf = &reg_addr,
255+
},
256+
{
257+
.addr = client->addr,
258+
.len = xfer_len,
259+
.buf = data,
260+
.flags = I2C_M_RD,
261+
}
262+
};
263+
264+
error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
215265
if (unlikely(error))
216266
return error;
217267

0 commit comments

Comments
 (0)