hid-mcp2221.c (d9786159d229cdc1f579f7cf3abf464efb453e40) hid-mcp2221.c (02a46753601a24e1673d9c28173121055e8e6cc9)
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * MCP2221A - Microchip USB to I2C Host Protocol Bridge
4 *
5 * Copyright (c) 2020, Rishi Gupta <gupt21@gmail.com>
6 *
7 * Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf
8 */

--- 173 unchanged lines hidden (view full) ---

182{
183 memset(mcp->txbuf, 0, 8);
184 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
185 mcp->txbuf[2] = MCP2221_I2C_CANCEL;
186
187 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
188}
189
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * MCP2221A - Microchip USB to I2C Host Protocol Bridge
4 *
5 * Copyright (c) 2020, Rishi Gupta <gupt21@gmail.com>
6 *
7 * Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf
8 */

--- 173 unchanged lines hidden (view full) ---

182{
183 memset(mcp->txbuf, 0, 8);
184 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
185 mcp->txbuf[2] = MCP2221_I2C_CANCEL;
186
187 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
188}
189
190/* Check if the last command succeeded or failed and return the result.
191 * If the command did fail, cancel that command which will free the i2c bus.
192 */
193static int mcp_chk_last_cmd_status_free_bus(struct mcp2221 *mcp)
194{
195 int ret;
196
197 ret = mcp_chk_last_cmd_status(mcp);
198 if (ret) {
199 /* The last command was a failure.
200 * Send a cancel which will also free the bus.
201 */
202 usleep_range(980, 1000);
203 mcp_cancel_last_cmd(mcp);
204 }
205
206 return ret;
207}
208
190static int mcp_set_i2c_speed(struct mcp2221 *mcp)
191{
192 int ret;
193
194 memset(mcp->txbuf, 0, 8);
195 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
196 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
197 mcp->txbuf[4] = mcp->cur_i2c_clk_div;

--- 38 unchanged lines hidden (view full) ---

236
237 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
238 if (ret)
239 return ret;
240
241 usleep_range(980, 1000);
242
243 if (last_status) {
209static int mcp_set_i2c_speed(struct mcp2221 *mcp)
210{
211 int ret;
212
213 memset(mcp->txbuf, 0, 8);
214 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
215 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
216 mcp->txbuf[4] = mcp->cur_i2c_clk_div;

--- 38 unchanged lines hidden (view full) ---

255
256 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
257 if (ret)
258 return ret;
259
260 usleep_range(980, 1000);
261
262 if (last_status) {
244 ret = mcp_chk_last_cmd_status(mcp);
263 ret = mcp_chk_last_cmd_status_free_bus(mcp);
245 if (ret)
246 return ret;
247 }
248
249 sent = sent + len;
250 if (sent >= msg->len)
251 break;
252

--- 50 unchanged lines hidden (view full) ---

303 do {
304 memset(mcp->txbuf, 0, 4);
305 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
306
307 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
308 if (ret)
309 return ret;
310
264 if (ret)
265 return ret;
266 }
267
268 sent = sent + len;
269 if (sent >= msg->len)
270 break;
271

--- 50 unchanged lines hidden (view full) ---

322 do {
323 memset(mcp->txbuf, 0, 4);
324 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
325
326 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
327 if (ret)
328 return ret;
329
311 ret = mcp_chk_last_cmd_status(mcp);
330 ret = mcp_chk_last_cmd_status_free_bus(mcp);
312 if (ret)
313 return ret;
314
315 usleep_range(980, 1000);
316 } while (mcp->rxbuf_idx < total_len);
317
318 return ret;
319}
320
321static int mcp_i2c_xfer(struct i2c_adapter *adapter,
322 struct i2c_msg msgs[], int num)
323{
324 int ret;
325 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
326
327 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
328
329 mutex_lock(&mcp->lock);
330
331 if (ret)
332 return ret;
333
334 usleep_range(980, 1000);
335 } while (mcp->rxbuf_idx < total_len);
336
337 return ret;
338}
339
340static int mcp_i2c_xfer(struct i2c_adapter *adapter,
341 struct i2c_msg msgs[], int num)
342{
343 int ret;
344 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
345
346 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
347
348 mutex_lock(&mcp->lock);
349
331 /* Setting speed before every transaction is required for mcp2221 */
332 ret = mcp_set_i2c_speed(mcp);
333 if (ret)
334 goto exit;
335
336 if (num == 1) {
337 if (msgs->flags & I2C_M_RD) {
338 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
339 0, 0, NULL);
340 } else {
341 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
342 }
343 if (ret)

--- 68 unchanged lines hidden (view full) ---

412
413 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
414 if (ret)
415 return ret;
416
417 if (last_status) {
418 usleep_range(980, 1000);
419
350 if (num == 1) {
351 if (msgs->flags & I2C_M_RD) {
352 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
353 0, 0, NULL);
354 } else {
355 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
356 }
357 if (ret)

--- 68 unchanged lines hidden (view full) ---

426
427 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
428 if (ret)
429 return ret;
430
431 if (last_status) {
432 usleep_range(980, 1000);
433
420 ret = mcp_chk_last_cmd_status(mcp);
421 if (ret)
422 return ret;
434 ret = mcp_chk_last_cmd_status_free_bus(mcp);
423 }
424
425 return ret;
426}
427
428static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
429 unsigned short flags, char read_write,
430 u8 command, int size,
431 union i2c_smbus_data *data)
432{
433 int ret;
434 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
435
436 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
437
438 mutex_lock(&mcp->lock);
439
435 }
436
437 return ret;
438}
439
440static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
441 unsigned short flags, char read_write,
442 u8 command, int size,
443 union i2c_smbus_data *data)
444{
445 int ret;
446 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
447
448 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
449
450 mutex_lock(&mcp->lock);
451
440 ret = mcp_set_i2c_speed(mcp);
441 if (ret)
442 goto exit;
443
444 switch (size) {
445
446 case I2C_SMBUS_QUICK:
447 if (read_write == I2C_SMBUS_READ)
448 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
449 addr, 0, &data->byte);
450 else
451 ret = mcp_smbus_write(mcp, addr, command, NULL,

--- 691 unchanged lines hidden (view full) ---

1143 return ret;
1144
1145 /* Set I2C bus clock diviser */
1146 if (i2c_clk_freq > 400)
1147 i2c_clk_freq = 400;
1148 if (i2c_clk_freq < 50)
1149 i2c_clk_freq = 50;
1150 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
452 switch (size) {
453
454 case I2C_SMBUS_QUICK:
455 if (read_write == I2C_SMBUS_READ)
456 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
457 addr, 0, &data->byte);
458 else
459 ret = mcp_smbus_write(mcp, addr, command, NULL,

--- 691 unchanged lines hidden (view full) ---

1151 return ret;
1152
1153 /* Set I2C bus clock diviser */
1154 if (i2c_clk_freq > 400)
1155 i2c_clk_freq = 400;
1156 if (i2c_clk_freq < 50)
1157 i2c_clk_freq = 50;
1158 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
1159 ret = mcp_set_i2c_speed(mcp);
1160 if (ret) {
1161 hid_err(hdev, "can't set i2c speed: %d\n", ret);
1162 return ret;
1163 }
1151
1152 mcp->adapter.owner = THIS_MODULE;
1153 mcp->adapter.class = I2C_CLASS_HWMON;
1154 mcp->adapter.algo = &mcp_i2c_algo;
1155 mcp->adapter.retries = 1;
1156 mcp->adapter.dev.parent = &hdev->dev;
1157 ACPI_COMPANION_SET(&mcp->adapter.dev, ACPI_COMPANION(hdev->dev.parent));
1158 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),

--- 59 unchanged lines hidden ---
1164
1165 mcp->adapter.owner = THIS_MODULE;
1166 mcp->adapter.class = I2C_CLASS_HWMON;
1167 mcp->adapter.algo = &mcp_i2c_algo;
1168 mcp->adapter.retries = 1;
1169 mcp->adapter.dev.parent = &hdev->dev;
1170 ACPI_COMPANION_SET(&mcp->adapter.dev, ACPI_COMPANION(hdev->dev.parent));
1171 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),

--- 59 unchanged lines hidden ---