hid-mcp2221.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 <[email protected]>
  6. *
  7. * Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf
  8. */
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/mutex.h>
  12. #include <linux/completion.h>
  13. #include <linux/delay.h>
  14. #include <linux/hid.h>
  15. #include <linux/hidraw.h>
  16. #include <linux/i2c.h>
  17. #include <linux/gpio/driver.h>
  18. #include "hid-ids.h"
  19. /* Commands codes in a raw output report */
  20. enum {
  21. MCP2221_I2C_WR_DATA = 0x90,
  22. MCP2221_I2C_WR_NO_STOP = 0x94,
  23. MCP2221_I2C_RD_DATA = 0x91,
  24. MCP2221_I2C_RD_RPT_START = 0x93,
  25. MCP2221_I2C_GET_DATA = 0x40,
  26. MCP2221_I2C_PARAM_OR_STATUS = 0x10,
  27. MCP2221_I2C_SET_SPEED = 0x20,
  28. MCP2221_I2C_CANCEL = 0x10,
  29. MCP2221_GPIO_SET = 0x50,
  30. MCP2221_GPIO_GET = 0x51,
  31. };
  32. /* Response codes in a raw input report */
  33. enum {
  34. MCP2221_SUCCESS = 0x00,
  35. MCP2221_I2C_ENG_BUSY = 0x01,
  36. MCP2221_I2C_START_TOUT = 0x12,
  37. MCP2221_I2C_STOP_TOUT = 0x62,
  38. MCP2221_I2C_WRADDRL_TOUT = 0x23,
  39. MCP2221_I2C_WRDATA_TOUT = 0x44,
  40. MCP2221_I2C_WRADDRL_NACK = 0x25,
  41. MCP2221_I2C_MASK_ADDR_NACK = 0x40,
  42. MCP2221_I2C_WRADDRL_SEND = 0x21,
  43. MCP2221_I2C_ADDR_NACK = 0x25,
  44. MCP2221_I2C_READ_COMPL = 0x55,
  45. MCP2221_ALT_F_NOT_GPIOV = 0xEE,
  46. MCP2221_ALT_F_NOT_GPIOD = 0xEF,
  47. };
  48. /* MCP GPIO direction encoding */
  49. enum {
  50. MCP2221_DIR_OUT = 0x00,
  51. MCP2221_DIR_IN = 0x01,
  52. };
  53. #define MCP_NGPIO 4
  54. /* MCP GPIO set command layout */
  55. struct mcp_set_gpio {
  56. u8 cmd;
  57. u8 dummy;
  58. struct {
  59. u8 change_value;
  60. u8 value;
  61. u8 change_direction;
  62. u8 direction;
  63. } gpio[MCP_NGPIO];
  64. } __packed;
  65. /* MCP GPIO get command layout */
  66. struct mcp_get_gpio {
  67. u8 cmd;
  68. u8 dummy;
  69. struct {
  70. u8 direction;
  71. u8 value;
  72. } gpio[MCP_NGPIO];
  73. } __packed;
  74. /*
  75. * There is no way to distinguish responses. Therefore next command
  76. * is sent only after response to previous has been received. Mutex
  77. * lock is used for this purpose mainly.
  78. */
  79. struct mcp2221 {
  80. struct hid_device *hdev;
  81. struct i2c_adapter adapter;
  82. struct mutex lock;
  83. struct completion wait_in_report;
  84. u8 *rxbuf;
  85. u8 txbuf[64];
  86. int rxbuf_idx;
  87. int status;
  88. u8 cur_i2c_clk_div;
  89. struct gpio_chip *gc;
  90. u8 gp_idx;
  91. u8 gpio_dir;
  92. };
  93. /*
  94. * Default i2c bus clock frequency 400 kHz. Modify this if you
  95. * want to set some other frequency (min 50 kHz - max 400 kHz).
  96. */
  97. static uint i2c_clk_freq = 400;
  98. /* Synchronously send output report to the device */
  99. static int mcp_send_report(struct mcp2221 *mcp,
  100. u8 *out_report, size_t len)
  101. {
  102. u8 *buf;
  103. int ret;
  104. buf = kmemdup(out_report, len, GFP_KERNEL);
  105. if (!buf)
  106. return -ENOMEM;
  107. /* mcp2221 uses interrupt endpoint for out reports */
  108. ret = hid_hw_output_report(mcp->hdev, buf, len);
  109. kfree(buf);
  110. if (ret < 0)
  111. return ret;
  112. return 0;
  113. }
  114. /*
  115. * Send o/p report to the device and wait for i/p report to be
  116. * received from the device. If the device does not respond,
  117. * we timeout.
  118. */
  119. static int mcp_send_data_req_status(struct mcp2221 *mcp,
  120. u8 *out_report, int len)
  121. {
  122. int ret;
  123. unsigned long t;
  124. reinit_completion(&mcp->wait_in_report);
  125. ret = mcp_send_report(mcp, out_report, len);
  126. if (ret)
  127. return ret;
  128. t = wait_for_completion_timeout(&mcp->wait_in_report,
  129. msecs_to_jiffies(4000));
  130. if (!t)
  131. return -ETIMEDOUT;
  132. return mcp->status;
  133. }
  134. /* Check pass/fail for actual communication with i2c slave */
  135. static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
  136. {
  137. memset(mcp->txbuf, 0, 8);
  138. mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
  139. return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
  140. }
  141. /* Cancels last command releasing i2c bus just in case occupied */
  142. static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
  143. {
  144. memset(mcp->txbuf, 0, 8);
  145. mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
  146. mcp->txbuf[2] = MCP2221_I2C_CANCEL;
  147. return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
  148. }
  149. static int mcp_set_i2c_speed(struct mcp2221 *mcp)
  150. {
  151. int ret;
  152. memset(mcp->txbuf, 0, 8);
  153. mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
  154. mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
  155. mcp->txbuf[4] = mcp->cur_i2c_clk_div;
  156. ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
  157. if (ret) {
  158. /* Small delay is needed here */
  159. usleep_range(980, 1000);
  160. mcp_cancel_last_cmd(mcp);
  161. }
  162. return 0;
  163. }
  164. /*
  165. * An output report can contain minimum 1 and maximum 60 user data
  166. * bytes. If the number of data bytes is more then 60, we send it
  167. * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
  168. * bytes. Total number of bytes is informed in very first report to
  169. * mcp2221, from that point onwards it first collect all the data
  170. * from host and then send to i2c slave device.
  171. */
  172. static int mcp_i2c_write(struct mcp2221 *mcp,
  173. struct i2c_msg *msg, int type, u8 last_status)
  174. {
  175. int ret, len, idx, sent;
  176. idx = 0;
  177. sent = 0;
  178. if (msg->len < 60)
  179. len = msg->len;
  180. else
  181. len = 60;
  182. do {
  183. mcp->txbuf[0] = type;
  184. mcp->txbuf[1] = msg->len & 0xff;
  185. mcp->txbuf[2] = msg->len >> 8;
  186. mcp->txbuf[3] = (u8)(msg->addr << 1);
  187. memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
  188. ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
  189. if (ret)
  190. return ret;
  191. usleep_range(980, 1000);
  192. if (last_status) {
  193. ret = mcp_chk_last_cmd_status(mcp);
  194. if (ret)
  195. return ret;
  196. }
  197. sent = sent + len;
  198. if (sent >= msg->len)
  199. break;
  200. idx = idx + len;
  201. if ((msg->len - sent) < 60)
  202. len = msg->len - sent;
  203. else
  204. len = 60;
  205. /*
  206. * Testing shows delay is needed between successive writes
  207. * otherwise next write fails on first-try from i2c core.
  208. * This value is obtained through automated stress testing.
  209. */
  210. usleep_range(980, 1000);
  211. } while (len > 0);
  212. return ret;
  213. }
  214. /*
  215. * Device reads all data (0 - 65535 bytes) from i2c slave device and
  216. * stores it in device itself. This data is read back from device to
  217. * host in multiples of 60 bytes using input reports.
  218. */
  219. static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
  220. struct i2c_msg *msg, int type, u16 smbus_addr,
  221. u8 smbus_len, u8 *smbus_buf)
  222. {
  223. int ret;
  224. u16 total_len;
  225. mcp->txbuf[0] = type;
  226. if (msg) {
  227. mcp->txbuf[1] = msg->len & 0xff;
  228. mcp->txbuf[2] = msg->len >> 8;
  229. mcp->txbuf[3] = (u8)(msg->addr << 1);
  230. total_len = msg->len;
  231. mcp->rxbuf = msg->buf;
  232. } else {
  233. mcp->txbuf[1] = smbus_len;
  234. mcp->txbuf[2] = 0;
  235. mcp->txbuf[3] = (u8)(smbus_addr << 1);
  236. total_len = smbus_len;
  237. mcp->rxbuf = smbus_buf;
  238. }
  239. ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
  240. if (ret)
  241. return ret;
  242. mcp->rxbuf_idx = 0;
  243. do {
  244. memset(mcp->txbuf, 0, 4);
  245. mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
  246. ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
  247. if (ret)
  248. return ret;
  249. ret = mcp_chk_last_cmd_status(mcp);
  250. if (ret)
  251. return ret;
  252. usleep_range(980, 1000);
  253. } while (mcp->rxbuf_idx < total_len);
  254. return ret;
  255. }
  256. static int mcp_i2c_xfer(struct i2c_adapter *adapter,
  257. struct i2c_msg msgs[], int num)
  258. {
  259. int ret;
  260. struct mcp2221 *mcp = i2c_get_adapdata(adapter);
  261. hid_hw_power(mcp->hdev, PM_HINT_FULLON);
  262. mutex_lock(&mcp->lock);
  263. /* Setting speed before every transaction is required for mcp2221 */
  264. ret = mcp_set_i2c_speed(mcp);
  265. if (ret)
  266. goto exit;
  267. if (num == 1) {
  268. if (msgs->flags & I2C_M_RD) {
  269. ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
  270. 0, 0, NULL);
  271. } else {
  272. ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
  273. }
  274. if (ret)
  275. goto exit;
  276. ret = num;
  277. } else if (num == 2) {
  278. /* Ex transaction; send reg address and read its contents */
  279. if (msgs[0].addr == msgs[1].addr &&
  280. !(msgs[0].flags & I2C_M_RD) &&
  281. (msgs[1].flags & I2C_M_RD)) {
  282. ret = mcp_i2c_write(mcp, &msgs[0],
  283. MCP2221_I2C_WR_NO_STOP, 0);
  284. if (ret)
  285. goto exit;
  286. ret = mcp_i2c_smbus_read(mcp, &msgs[1],
  287. MCP2221_I2C_RD_RPT_START,
  288. 0, 0, NULL);
  289. if (ret)
  290. goto exit;
  291. ret = num;
  292. } else {
  293. dev_err(&adapter->dev,
  294. "unsupported multi-msg i2c transaction\n");
  295. ret = -EOPNOTSUPP;
  296. }
  297. } else {
  298. dev_err(&adapter->dev,
  299. "unsupported multi-msg i2c transaction\n");
  300. ret = -EOPNOTSUPP;
  301. }
  302. exit:
  303. hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
  304. mutex_unlock(&mcp->lock);
  305. return ret;
  306. }
  307. static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
  308. u8 command, u8 *buf, u8 len, int type,
  309. u8 last_status)
  310. {
  311. int data_len, ret;
  312. mcp->txbuf[0] = type;
  313. mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
  314. mcp->txbuf[2] = 0;
  315. mcp->txbuf[3] = (u8)(addr << 1);
  316. mcp->txbuf[4] = command;
  317. switch (len) {
  318. case 0:
  319. data_len = 5;
  320. break;
  321. case 1:
  322. mcp->txbuf[5] = buf[0];
  323. data_len = 6;
  324. break;
  325. case 2:
  326. mcp->txbuf[5] = buf[0];
  327. mcp->txbuf[6] = buf[1];
  328. data_len = 7;
  329. break;
  330. default:
  331. if (len > I2C_SMBUS_BLOCK_MAX)
  332. return -EINVAL;
  333. memcpy(&mcp->txbuf[5], buf, len);
  334. data_len = len + 5;
  335. }
  336. ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
  337. if (ret)
  338. return ret;
  339. if (last_status) {
  340. usleep_range(980, 1000);
  341. ret = mcp_chk_last_cmd_status(mcp);
  342. if (ret)
  343. return ret;
  344. }
  345. return ret;
  346. }
  347. static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  348. unsigned short flags, char read_write,
  349. u8 command, int size,
  350. union i2c_smbus_data *data)
  351. {
  352. int ret;
  353. struct mcp2221 *mcp = i2c_get_adapdata(adapter);
  354. hid_hw_power(mcp->hdev, PM_HINT_FULLON);
  355. mutex_lock(&mcp->lock);
  356. ret = mcp_set_i2c_speed(mcp);
  357. if (ret)
  358. goto exit;
  359. switch (size) {
  360. case I2C_SMBUS_QUICK:
  361. if (read_write == I2C_SMBUS_READ)
  362. ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
  363. addr, 0, &data->byte);
  364. else
  365. ret = mcp_smbus_write(mcp, addr, command, NULL,
  366. 0, MCP2221_I2C_WR_DATA, 1);
  367. break;
  368. case I2C_SMBUS_BYTE:
  369. if (read_write == I2C_SMBUS_READ)
  370. ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
  371. addr, 1, &data->byte);
  372. else
  373. ret = mcp_smbus_write(mcp, addr, command, NULL,
  374. 0, MCP2221_I2C_WR_DATA, 1);
  375. break;
  376. case I2C_SMBUS_BYTE_DATA:
  377. if (read_write == I2C_SMBUS_READ) {
  378. ret = mcp_smbus_write(mcp, addr, command, NULL,
  379. 0, MCP2221_I2C_WR_NO_STOP, 0);
  380. if (ret)
  381. goto exit;
  382. ret = mcp_i2c_smbus_read(mcp, NULL,
  383. MCP2221_I2C_RD_RPT_START,
  384. addr, 1, &data->byte);
  385. } else {
  386. ret = mcp_smbus_write(mcp, addr, command, &data->byte,
  387. 1, MCP2221_I2C_WR_DATA, 1);
  388. }
  389. break;
  390. case I2C_SMBUS_WORD_DATA:
  391. if (read_write == I2C_SMBUS_READ) {
  392. ret = mcp_smbus_write(mcp, addr, command, NULL,
  393. 0, MCP2221_I2C_WR_NO_STOP, 0);
  394. if (ret)
  395. goto exit;
  396. ret = mcp_i2c_smbus_read(mcp, NULL,
  397. MCP2221_I2C_RD_RPT_START,
  398. addr, 2, (u8 *)&data->word);
  399. } else {
  400. ret = mcp_smbus_write(mcp, addr, command,
  401. (u8 *)&data->word, 2,
  402. MCP2221_I2C_WR_DATA, 1);
  403. }
  404. break;
  405. case I2C_SMBUS_BLOCK_DATA:
  406. if (read_write == I2C_SMBUS_READ) {
  407. ret = mcp_smbus_write(mcp, addr, command, NULL,
  408. 0, MCP2221_I2C_WR_NO_STOP, 1);
  409. if (ret)
  410. goto exit;
  411. mcp->rxbuf_idx = 0;
  412. mcp->rxbuf = data->block;
  413. mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
  414. ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
  415. if (ret)
  416. goto exit;
  417. } else {
  418. if (!data->block[0]) {
  419. ret = -EINVAL;
  420. goto exit;
  421. }
  422. ret = mcp_smbus_write(mcp, addr, command, data->block,
  423. data->block[0] + 1,
  424. MCP2221_I2C_WR_DATA, 1);
  425. }
  426. break;
  427. case I2C_SMBUS_I2C_BLOCK_DATA:
  428. if (read_write == I2C_SMBUS_READ) {
  429. ret = mcp_smbus_write(mcp, addr, command, NULL,
  430. 0, MCP2221_I2C_WR_NO_STOP, 1);
  431. if (ret)
  432. goto exit;
  433. mcp->rxbuf_idx = 0;
  434. mcp->rxbuf = data->block;
  435. mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
  436. ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
  437. if (ret)
  438. goto exit;
  439. } else {
  440. if (!data->block[0]) {
  441. ret = -EINVAL;
  442. goto exit;
  443. }
  444. ret = mcp_smbus_write(mcp, addr, command,
  445. &data->block[1], data->block[0],
  446. MCP2221_I2C_WR_DATA, 1);
  447. }
  448. break;
  449. case I2C_SMBUS_PROC_CALL:
  450. ret = mcp_smbus_write(mcp, addr, command,
  451. (u8 *)&data->word,
  452. 2, MCP2221_I2C_WR_NO_STOP, 0);
  453. if (ret)
  454. goto exit;
  455. ret = mcp_i2c_smbus_read(mcp, NULL,
  456. MCP2221_I2C_RD_RPT_START,
  457. addr, 2, (u8 *)&data->word);
  458. break;
  459. case I2C_SMBUS_BLOCK_PROC_CALL:
  460. ret = mcp_smbus_write(mcp, addr, command, data->block,
  461. data->block[0] + 1,
  462. MCP2221_I2C_WR_NO_STOP, 0);
  463. if (ret)
  464. goto exit;
  465. ret = mcp_i2c_smbus_read(mcp, NULL,
  466. MCP2221_I2C_RD_RPT_START,
  467. addr, I2C_SMBUS_BLOCK_MAX,
  468. data->block);
  469. break;
  470. default:
  471. dev_err(&mcp->adapter.dev,
  472. "unsupported smbus transaction size:%d\n", size);
  473. ret = -EOPNOTSUPP;
  474. }
  475. exit:
  476. hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
  477. mutex_unlock(&mcp->lock);
  478. return ret;
  479. }
  480. static u32 mcp_i2c_func(struct i2c_adapter *adapter)
  481. {
  482. return I2C_FUNC_I2C |
  483. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  484. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  485. (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
  486. }
  487. static const struct i2c_algorithm mcp_i2c_algo = {
  488. .master_xfer = mcp_i2c_xfer,
  489. .smbus_xfer = mcp_smbus_xfer,
  490. .functionality = mcp_i2c_func,
  491. };
  492. static int mcp_gpio_get(struct gpio_chip *gc,
  493. unsigned int offset)
  494. {
  495. int ret;
  496. struct mcp2221 *mcp = gpiochip_get_data(gc);
  497. mcp->txbuf[0] = MCP2221_GPIO_GET;
  498. mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].value);
  499. mutex_lock(&mcp->lock);
  500. ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
  501. mutex_unlock(&mcp->lock);
  502. return ret;
  503. }
  504. static void mcp_gpio_set(struct gpio_chip *gc,
  505. unsigned int offset, int value)
  506. {
  507. struct mcp2221 *mcp = gpiochip_get_data(gc);
  508. memset(mcp->txbuf, 0, 18);
  509. mcp->txbuf[0] = MCP2221_GPIO_SET;
  510. mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
  511. mcp->txbuf[mcp->gp_idx - 1] = 1;
  512. mcp->txbuf[mcp->gp_idx] = !!value;
  513. mutex_lock(&mcp->lock);
  514. mcp_send_data_req_status(mcp, mcp->txbuf, 18);
  515. mutex_unlock(&mcp->lock);
  516. }
  517. static int mcp_gpio_dir_set(struct mcp2221 *mcp,
  518. unsigned int offset, u8 val)
  519. {
  520. memset(mcp->txbuf, 0, 18);
  521. mcp->txbuf[0] = MCP2221_GPIO_SET;
  522. mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
  523. mcp->txbuf[mcp->gp_idx - 1] = 1;
  524. mcp->txbuf[mcp->gp_idx] = val;
  525. return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
  526. }
  527. static int mcp_gpio_direction_input(struct gpio_chip *gc,
  528. unsigned int offset)
  529. {
  530. int ret;
  531. struct mcp2221 *mcp = gpiochip_get_data(gc);
  532. mutex_lock(&mcp->lock);
  533. ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
  534. mutex_unlock(&mcp->lock);
  535. return ret;
  536. }
  537. static int mcp_gpio_direction_output(struct gpio_chip *gc,
  538. unsigned int offset, int value)
  539. {
  540. int ret;
  541. struct mcp2221 *mcp = gpiochip_get_data(gc);
  542. mutex_lock(&mcp->lock);
  543. ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
  544. mutex_unlock(&mcp->lock);
  545. /* Can't configure as output, bailout early */
  546. if (ret)
  547. return ret;
  548. mcp_gpio_set(gc, offset, value);
  549. return 0;
  550. }
  551. static int mcp_gpio_get_direction(struct gpio_chip *gc,
  552. unsigned int offset)
  553. {
  554. int ret;
  555. struct mcp2221 *mcp = gpiochip_get_data(gc);
  556. mcp->txbuf[0] = MCP2221_GPIO_GET;
  557. mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].direction);
  558. mutex_lock(&mcp->lock);
  559. ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
  560. mutex_unlock(&mcp->lock);
  561. if (ret)
  562. return ret;
  563. if (mcp->gpio_dir == MCP2221_DIR_IN)
  564. return GPIO_LINE_DIRECTION_IN;
  565. return GPIO_LINE_DIRECTION_OUT;
  566. }
  567. /* Gives current state of i2c engine inside mcp2221 */
  568. static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
  569. u8 *data, u8 idx)
  570. {
  571. int ret;
  572. switch (data[idx]) {
  573. case MCP2221_I2C_WRADDRL_NACK:
  574. case MCP2221_I2C_WRADDRL_SEND:
  575. ret = -ENXIO;
  576. break;
  577. case MCP2221_I2C_START_TOUT:
  578. case MCP2221_I2C_STOP_TOUT:
  579. case MCP2221_I2C_WRADDRL_TOUT:
  580. case MCP2221_I2C_WRDATA_TOUT:
  581. ret = -ETIMEDOUT;
  582. break;
  583. case MCP2221_I2C_ENG_BUSY:
  584. ret = -EAGAIN;
  585. break;
  586. case MCP2221_SUCCESS:
  587. ret = 0x00;
  588. break;
  589. default:
  590. ret = -EIO;
  591. }
  592. return ret;
  593. }
  594. /*
  595. * MCP2221 uses interrupt endpoint for input reports. This function
  596. * is called by HID layer when it receives i/p report from mcp2221,
  597. * which is actually a response to the previously sent command.
  598. *
  599. * MCP2221A firmware specific return codes are parsed and 0 or
  600. * appropriate negative error code is returned. Delayed response
  601. * results in timeout error and stray reponses results in -EIO.
  602. */
  603. static int mcp2221_raw_event(struct hid_device *hdev,
  604. struct hid_report *report, u8 *data, int size)
  605. {
  606. u8 *buf;
  607. struct mcp2221 *mcp = hid_get_drvdata(hdev);
  608. switch (data[0]) {
  609. case MCP2221_I2C_WR_DATA:
  610. case MCP2221_I2C_WR_NO_STOP:
  611. case MCP2221_I2C_RD_DATA:
  612. case MCP2221_I2C_RD_RPT_START:
  613. switch (data[1]) {
  614. case MCP2221_SUCCESS:
  615. mcp->status = 0;
  616. break;
  617. default:
  618. mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
  619. }
  620. complete(&mcp->wait_in_report);
  621. break;
  622. case MCP2221_I2C_PARAM_OR_STATUS:
  623. switch (data[1]) {
  624. case MCP2221_SUCCESS:
  625. if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
  626. (data[3] != MCP2221_I2C_SET_SPEED)) {
  627. mcp->status = -EAGAIN;
  628. break;
  629. }
  630. if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
  631. mcp->status = -ENXIO;
  632. break;
  633. }
  634. mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
  635. break;
  636. default:
  637. mcp->status = -EIO;
  638. }
  639. complete(&mcp->wait_in_report);
  640. break;
  641. case MCP2221_I2C_GET_DATA:
  642. switch (data[1]) {
  643. case MCP2221_SUCCESS:
  644. if (data[2] == MCP2221_I2C_ADDR_NACK) {
  645. mcp->status = -ENXIO;
  646. break;
  647. }
  648. if (!mcp_get_i2c_eng_state(mcp, data, 2)
  649. && (data[3] == 0)) {
  650. mcp->status = 0;
  651. break;
  652. }
  653. if (data[3] == 127) {
  654. mcp->status = -EIO;
  655. break;
  656. }
  657. if (data[2] == MCP2221_I2C_READ_COMPL) {
  658. buf = mcp->rxbuf;
  659. memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
  660. mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
  661. mcp->status = 0;
  662. break;
  663. }
  664. mcp->status = -EIO;
  665. break;
  666. default:
  667. mcp->status = -EIO;
  668. }
  669. complete(&mcp->wait_in_report);
  670. break;
  671. case MCP2221_GPIO_GET:
  672. switch (data[1]) {
  673. case MCP2221_SUCCESS:
  674. if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
  675. (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
  676. mcp->status = -ENOENT;
  677. } else {
  678. mcp->status = !!data[mcp->gp_idx];
  679. mcp->gpio_dir = data[mcp->gp_idx + 1];
  680. }
  681. break;
  682. default:
  683. mcp->status = -EAGAIN;
  684. }
  685. complete(&mcp->wait_in_report);
  686. break;
  687. case MCP2221_GPIO_SET:
  688. switch (data[1]) {
  689. case MCP2221_SUCCESS:
  690. if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
  691. (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
  692. mcp->status = -ENOENT;
  693. } else {
  694. mcp->status = 0;
  695. }
  696. break;
  697. default:
  698. mcp->status = -EAGAIN;
  699. }
  700. complete(&mcp->wait_in_report);
  701. break;
  702. default:
  703. mcp->status = -EIO;
  704. complete(&mcp->wait_in_report);
  705. }
  706. return 1;
  707. }
  708. static int mcp2221_probe(struct hid_device *hdev,
  709. const struct hid_device_id *id)
  710. {
  711. int ret;
  712. struct mcp2221 *mcp;
  713. mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
  714. if (!mcp)
  715. return -ENOMEM;
  716. ret = hid_parse(hdev);
  717. if (ret) {
  718. hid_err(hdev, "can't parse reports\n");
  719. return ret;
  720. }
  721. /*
  722. * This driver uses the .raw_event callback and therefore does not need any
  723. * HID_CONNECT_xxx flags.
  724. */
  725. ret = hid_hw_start(hdev, 0);
  726. if (ret) {
  727. hid_err(hdev, "can't start hardware\n");
  728. return ret;
  729. }
  730. hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8,
  731. hdev->version & 0xff, hdev->name, hdev->phys);
  732. ret = hid_hw_open(hdev);
  733. if (ret) {
  734. hid_err(hdev, "can't open device\n");
  735. goto err_hstop;
  736. }
  737. mutex_init(&mcp->lock);
  738. init_completion(&mcp->wait_in_report);
  739. hid_set_drvdata(hdev, mcp);
  740. mcp->hdev = hdev;
  741. /* Set I2C bus clock diviser */
  742. if (i2c_clk_freq > 400)
  743. i2c_clk_freq = 400;
  744. if (i2c_clk_freq < 50)
  745. i2c_clk_freq = 50;
  746. mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
  747. mcp->adapter.owner = THIS_MODULE;
  748. mcp->adapter.class = I2C_CLASS_HWMON;
  749. mcp->adapter.algo = &mcp_i2c_algo;
  750. mcp->adapter.retries = 1;
  751. mcp->adapter.dev.parent = &hdev->dev;
  752. snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
  753. "MCP2221 usb-i2c bridge");
  754. ret = i2c_add_adapter(&mcp->adapter);
  755. if (ret) {
  756. hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
  757. goto err_i2c;
  758. }
  759. i2c_set_adapdata(&mcp->adapter, mcp);
  760. /* Setup GPIO chip */
  761. mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
  762. if (!mcp->gc) {
  763. ret = -ENOMEM;
  764. goto err_gc;
  765. }
  766. mcp->gc->label = "mcp2221_gpio";
  767. mcp->gc->direction_input = mcp_gpio_direction_input;
  768. mcp->gc->direction_output = mcp_gpio_direction_output;
  769. mcp->gc->get_direction = mcp_gpio_get_direction;
  770. mcp->gc->set = mcp_gpio_set;
  771. mcp->gc->get = mcp_gpio_get;
  772. mcp->gc->ngpio = MCP_NGPIO;
  773. mcp->gc->base = -1;
  774. mcp->gc->can_sleep = 1;
  775. mcp->gc->parent = &hdev->dev;
  776. ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
  777. if (ret)
  778. goto err_gc;
  779. return 0;
  780. err_gc:
  781. i2c_del_adapter(&mcp->adapter);
  782. err_i2c:
  783. hid_hw_close(mcp->hdev);
  784. err_hstop:
  785. hid_hw_stop(mcp->hdev);
  786. return ret;
  787. }
  788. static void mcp2221_remove(struct hid_device *hdev)
  789. {
  790. struct mcp2221 *mcp = hid_get_drvdata(hdev);
  791. i2c_del_adapter(&mcp->adapter);
  792. hid_hw_close(mcp->hdev);
  793. hid_hw_stop(mcp->hdev);
  794. }
  795. static const struct hid_device_id mcp2221_devices[] = {
  796. { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
  797. { }
  798. };
  799. MODULE_DEVICE_TABLE(hid, mcp2221_devices);
  800. static struct hid_driver mcp2221_driver = {
  801. .name = "mcp2221",
  802. .id_table = mcp2221_devices,
  803. .probe = mcp2221_probe,
  804. .remove = mcp2221_remove,
  805. .raw_event = mcp2221_raw_event,
  806. };
  807. /* Register with HID core */
  808. module_hid_driver(mcp2221_driver);
  809. MODULE_AUTHOR("Rishi Gupta <[email protected]>");
  810. MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
  811. MODULE_LICENSE("GPL v2");