i2c.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/i2c.c
  3. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016 Vadim Pasternak <vadimp@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/err.h>
  35. #include <linux/i2c.h>
  36. #include <linux/init.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/kernel.h>
  39. #include <linux/mutex.h>
  40. #include <linux/module.h>
  41. #include <linux/mod_devicetable.h>
  42. #include <linux/slab.h>
  43. #include "cmd.h"
  44. #include "core.h"
  45. #include "i2c.h"
  46. static const char mlxsw_i2c_driver_name[] = "mlxsw_i2c";
  47. #define MLXSW_I2C_CIR2_BASE 0x72000
  48. #define MLXSW_I2C_CIR_STATUS_OFF 0x18
  49. #define MLXSW_I2C_CIR2_OFF_STATUS (MLXSW_I2C_CIR2_BASE + \
  50. MLXSW_I2C_CIR_STATUS_OFF)
  51. #define MLXSW_I2C_OPMOD_SHIFT 12
  52. #define MLXSW_I2C_GO_BIT_SHIFT 23
  53. #define MLXSW_I2C_CIR_CTRL_STATUS_SHIFT 24
  54. #define MLXSW_I2C_GO_BIT BIT(MLXSW_I2C_GO_BIT_SHIFT)
  55. #define MLXSW_I2C_GO_OPMODE BIT(MLXSW_I2C_OPMOD_SHIFT)
  56. #define MLXSW_I2C_SET_IMM_CMD (MLXSW_I2C_GO_OPMODE | \
  57. MLXSW_CMD_OPCODE_QUERY_FW)
  58. #define MLXSW_I2C_PUSH_IMM_CMD (MLXSW_I2C_GO_BIT | \
  59. MLXSW_I2C_SET_IMM_CMD)
  60. #define MLXSW_I2C_SET_CMD (MLXSW_CMD_OPCODE_ACCESS_REG)
  61. #define MLXSW_I2C_PUSH_CMD (MLXSW_I2C_GO_BIT | MLXSW_I2C_SET_CMD)
  62. #define MLXSW_I2C_TLV_HDR_SIZE 0x10
  63. #define MLXSW_I2C_ADDR_WIDTH 4
  64. #define MLXSW_I2C_PUSH_CMD_SIZE (MLXSW_I2C_ADDR_WIDTH + 4)
  65. #define MLXSW_I2C_READ_SEMA_SIZE 4
  66. #define MLXSW_I2C_PREP_SIZE (MLXSW_I2C_ADDR_WIDTH + 28)
  67. #define MLXSW_I2C_MBOX_SIZE 20
  68. #define MLXSW_I2C_MBOX_OUT_PARAM_OFF 12
  69. #define MLXSW_I2C_MAX_BUFF_SIZE 32
  70. #define MLXSW_I2C_MBOX_OFFSET_BITS 20
  71. #define MLXSW_I2C_MBOX_SIZE_BITS 12
  72. #define MLXSW_I2C_ADDR_BUF_SIZE 4
  73. #define MLXSW_I2C_BLK_MAX 32
  74. #define MLXSW_I2C_RETRY 5
  75. #define MLXSW_I2C_TIMEOUT_MSECS 5000
  76. /**
  77. * struct mlxsw_i2c - device private data:
  78. * @cmd.mb_size_in: input mailbox size;
  79. * @cmd.mb_off_in: input mailbox offset in register space;
  80. * @cmd.mb_size_out: output mailbox size;
  81. * @cmd.mb_off_out: output mailbox offset in register space;
  82. * @cmd.lock: command execution lock;
  83. * @dev: I2C device;
  84. * @core: switch core pointer;
  85. * @bus_info: bus info block;
  86. */
  87. struct mlxsw_i2c {
  88. struct {
  89. u32 mb_size_in;
  90. u32 mb_off_in;
  91. u32 mb_size_out;
  92. u32 mb_off_out;
  93. struct mutex lock;
  94. } cmd;
  95. struct device *dev;
  96. struct mlxsw_core *core;
  97. struct mlxsw_bus_info bus_info;
  98. };
  99. #define MLXSW_I2C_READ_MSG(_client, _addr_buf, _buf, _len) { \
  100. { .addr = (_client)->addr, \
  101. .buf = (_addr_buf), \
  102. .len = MLXSW_I2C_ADDR_BUF_SIZE, \
  103. .flags = 0 }, \
  104. { .addr = (_client)->addr, \
  105. .buf = (_buf), \
  106. .len = (_len), \
  107. .flags = I2C_M_RD } }
  108. #define MLXSW_I2C_WRITE_MSG(_client, _buf, _len) \
  109. { .addr = (_client)->addr, \
  110. .buf = (u8 *)(_buf), \
  111. .len = (_len), \
  112. .flags = 0 }
  113. /* Routine converts in and out mail boxes offset and size. */
  114. static inline void
  115. mlxsw_i2c_convert_mbox(struct mlxsw_i2c *mlxsw_i2c, u8 *buf)
  116. {
  117. u32 tmp;
  118. /* Local in/out mailboxes: 20 bits for offset, 12 for size */
  119. tmp = be32_to_cpup((__be32 *) buf);
  120. mlxsw_i2c->cmd.mb_off_in = tmp &
  121. GENMASK(MLXSW_I2C_MBOX_OFFSET_BITS - 1, 0);
  122. mlxsw_i2c->cmd.mb_size_in = (tmp & GENMASK(31,
  123. MLXSW_I2C_MBOX_OFFSET_BITS)) >>
  124. MLXSW_I2C_MBOX_OFFSET_BITS;
  125. tmp = be32_to_cpup((__be32 *) (buf + MLXSW_I2C_ADDR_WIDTH));
  126. mlxsw_i2c->cmd.mb_off_out = tmp &
  127. GENMASK(MLXSW_I2C_MBOX_OFFSET_BITS - 1, 0);
  128. mlxsw_i2c->cmd.mb_size_out = (tmp & GENMASK(31,
  129. MLXSW_I2C_MBOX_OFFSET_BITS)) >>
  130. MLXSW_I2C_MBOX_OFFSET_BITS;
  131. }
  132. /* Routine obtains register size from mail box buffer. */
  133. static inline int mlxsw_i2c_get_reg_size(u8 *in_mbox)
  134. {
  135. u16 tmp = be16_to_cpup((__be16 *) (in_mbox + MLXSW_I2C_TLV_HDR_SIZE));
  136. return (tmp & 0x7ff) * 4 + MLXSW_I2C_TLV_HDR_SIZE;
  137. }
  138. /* Routine sets I2C device internal offset in the transaction buffer. */
  139. static inline void mlxsw_i2c_set_slave_addr(u8 *buf, u32 off)
  140. {
  141. __be32 *val = (__be32 *) buf;
  142. *val = htonl(off);
  143. }
  144. /* Routine waits until go bit is cleared. */
  145. static int mlxsw_i2c_wait_go_bit(struct i2c_client *client,
  146. struct mlxsw_i2c *mlxsw_i2c, u8 *p_status)
  147. {
  148. u8 addr_buf[MLXSW_I2C_ADDR_BUF_SIZE];
  149. u8 buf[MLXSW_I2C_READ_SEMA_SIZE];
  150. int len = MLXSW_I2C_READ_SEMA_SIZE;
  151. struct i2c_msg read_sema[] =
  152. MLXSW_I2C_READ_MSG(client, addr_buf, buf, len);
  153. bool wait_done = false;
  154. unsigned long end;
  155. int i = 0, err;
  156. mlxsw_i2c_set_slave_addr(addr_buf, MLXSW_I2C_CIR2_OFF_STATUS);
  157. end = jiffies + msecs_to_jiffies(MLXSW_I2C_TIMEOUT_MSECS);
  158. do {
  159. u32 ctrl;
  160. err = i2c_transfer(client->adapter, read_sema,
  161. ARRAY_SIZE(read_sema));
  162. ctrl = be32_to_cpu(*(__be32 *) buf);
  163. if (err == ARRAY_SIZE(read_sema)) {
  164. if (!(ctrl & MLXSW_I2C_GO_BIT)) {
  165. wait_done = true;
  166. *p_status = ctrl >>
  167. MLXSW_I2C_CIR_CTRL_STATUS_SHIFT;
  168. break;
  169. }
  170. }
  171. cond_resched();
  172. } while ((time_before(jiffies, end)) || (i++ < MLXSW_I2C_RETRY));
  173. if (wait_done) {
  174. if (*p_status)
  175. err = -EIO;
  176. } else {
  177. return -ETIMEDOUT;
  178. }
  179. return err > 0 ? 0 : err;
  180. }
  181. /* Routine posts a command to ASIC though mail box. */
  182. static int mlxsw_i2c_write_cmd(struct i2c_client *client,
  183. struct mlxsw_i2c *mlxsw_i2c,
  184. int immediate)
  185. {
  186. __be32 push_cmd_buf[MLXSW_I2C_PUSH_CMD_SIZE / 4] = {
  187. 0, cpu_to_be32(MLXSW_I2C_PUSH_IMM_CMD)
  188. };
  189. __be32 prep_cmd_buf[MLXSW_I2C_PREP_SIZE / 4] = {
  190. 0, 0, 0, 0, 0, 0,
  191. cpu_to_be32(client->adapter->nr & 0xffff),
  192. cpu_to_be32(MLXSW_I2C_SET_IMM_CMD)
  193. };
  194. struct i2c_msg push_cmd =
  195. MLXSW_I2C_WRITE_MSG(client, push_cmd_buf,
  196. MLXSW_I2C_PUSH_CMD_SIZE);
  197. struct i2c_msg prep_cmd =
  198. MLXSW_I2C_WRITE_MSG(client, prep_cmd_buf, MLXSW_I2C_PREP_SIZE);
  199. int err;
  200. if (!immediate) {
  201. push_cmd_buf[1] = cpu_to_be32(MLXSW_I2C_PUSH_CMD);
  202. prep_cmd_buf[7] = cpu_to_be32(MLXSW_I2C_SET_CMD);
  203. }
  204. mlxsw_i2c_set_slave_addr((u8 *)prep_cmd_buf,
  205. MLXSW_I2C_CIR2_BASE);
  206. mlxsw_i2c_set_slave_addr((u8 *)push_cmd_buf,
  207. MLXSW_I2C_CIR2_OFF_STATUS);
  208. /* Prepare Command Interface Register for transaction */
  209. err = i2c_transfer(client->adapter, &prep_cmd, 1);
  210. if (err < 0)
  211. return err;
  212. else if (err != 1)
  213. return -EIO;
  214. /* Write out Command Interface Register GO bit to push transaction */
  215. err = i2c_transfer(client->adapter, &push_cmd, 1);
  216. if (err < 0)
  217. return err;
  218. else if (err != 1)
  219. return -EIO;
  220. return 0;
  221. }
  222. /* Routine obtains mail box offsets from ASIC register space. */
  223. static int mlxsw_i2c_get_mbox(struct i2c_client *client,
  224. struct mlxsw_i2c *mlxsw_i2c)
  225. {
  226. u8 addr_buf[MLXSW_I2C_ADDR_BUF_SIZE];
  227. u8 buf[MLXSW_I2C_MBOX_SIZE];
  228. struct i2c_msg mbox_cmd[] =
  229. MLXSW_I2C_READ_MSG(client, addr_buf, buf, MLXSW_I2C_MBOX_SIZE);
  230. int err;
  231. /* Read mail boxes offsets. */
  232. mlxsw_i2c_set_slave_addr(addr_buf, MLXSW_I2C_CIR2_BASE);
  233. err = i2c_transfer(client->adapter, mbox_cmd, 2);
  234. if (err != 2) {
  235. dev_err(&client->dev, "Could not obtain mail boxes\n");
  236. if (!err)
  237. return -EIO;
  238. else
  239. return err;
  240. }
  241. /* Convert mail boxes. */
  242. mlxsw_i2c_convert_mbox(mlxsw_i2c, &buf[MLXSW_I2C_MBOX_OUT_PARAM_OFF]);
  243. return err;
  244. }
  245. /* Routine sends I2C write transaction to ASIC device. */
  246. static int
  247. mlxsw_i2c_write(struct device *dev, size_t in_mbox_size, u8 *in_mbox, int num,
  248. u8 *p_status)
  249. {
  250. struct i2c_client *client = to_i2c_client(dev);
  251. struct mlxsw_i2c *mlxsw_i2c = i2c_get_clientdata(client);
  252. unsigned long timeout = msecs_to_jiffies(MLXSW_I2C_TIMEOUT_MSECS);
  253. u8 tran_buf[MLXSW_I2C_MAX_BUFF_SIZE + MLXSW_I2C_ADDR_BUF_SIZE];
  254. int off = mlxsw_i2c->cmd.mb_off_in, chunk_size, i, j;
  255. unsigned long end;
  256. struct i2c_msg write_tran =
  257. MLXSW_I2C_WRITE_MSG(client, tran_buf, MLXSW_I2C_PUSH_CMD_SIZE);
  258. int err;
  259. for (i = 0; i < num; i++) {
  260. chunk_size = (in_mbox_size > MLXSW_I2C_BLK_MAX) ?
  261. MLXSW_I2C_BLK_MAX : in_mbox_size;
  262. write_tran.len = MLXSW_I2C_ADDR_WIDTH + chunk_size;
  263. mlxsw_i2c_set_slave_addr(tran_buf, off);
  264. memcpy(&tran_buf[MLXSW_I2C_ADDR_BUF_SIZE], in_mbox +
  265. chunk_size * i, chunk_size);
  266. j = 0;
  267. end = jiffies + timeout;
  268. do {
  269. err = i2c_transfer(client->adapter, &write_tran, 1);
  270. if (err == 1)
  271. break;
  272. cond_resched();
  273. } while ((time_before(jiffies, end)) ||
  274. (j++ < MLXSW_I2C_RETRY));
  275. if (err != 1) {
  276. if (!err)
  277. err = -EIO;
  278. return err;
  279. }
  280. off += chunk_size;
  281. in_mbox_size -= chunk_size;
  282. }
  283. /* Prepare and write out Command Interface Register for transaction. */
  284. err = mlxsw_i2c_write_cmd(client, mlxsw_i2c, 0);
  285. if (err) {
  286. dev_err(&client->dev, "Could not start transaction");
  287. return -EIO;
  288. }
  289. /* Wait until go bit is cleared. */
  290. err = mlxsw_i2c_wait_go_bit(client, mlxsw_i2c, p_status);
  291. if (err) {
  292. dev_err(&client->dev, "HW semaphore is not released");
  293. return err;
  294. }
  295. /* Validate transaction completion status. */
  296. if (*p_status) {
  297. dev_err(&client->dev, "Bad transaction completion status %x\n",
  298. *p_status);
  299. return -EIO;
  300. }
  301. return 0;
  302. }
  303. /* Routine executes I2C command. */
  304. static int
  305. mlxsw_i2c_cmd(struct device *dev, size_t in_mbox_size, u8 *in_mbox,
  306. size_t out_mbox_size, u8 *out_mbox, u8 *status)
  307. {
  308. struct i2c_client *client = to_i2c_client(dev);
  309. struct mlxsw_i2c *mlxsw_i2c = i2c_get_clientdata(client);
  310. unsigned long timeout = msecs_to_jiffies(MLXSW_I2C_TIMEOUT_MSECS);
  311. u8 tran_buf[MLXSW_I2C_ADDR_BUF_SIZE];
  312. int num, chunk_size, reg_size, i, j;
  313. int off = mlxsw_i2c->cmd.mb_off_out;
  314. unsigned long end;
  315. struct i2c_msg read_tran[] =
  316. MLXSW_I2C_READ_MSG(client, tran_buf, NULL, 0);
  317. int err;
  318. WARN_ON(in_mbox_size % sizeof(u32) || out_mbox_size % sizeof(u32));
  319. reg_size = mlxsw_i2c_get_reg_size(in_mbox);
  320. num = reg_size / MLXSW_I2C_BLK_MAX;
  321. if (reg_size % MLXSW_I2C_BLK_MAX)
  322. num++;
  323. if (mutex_lock_interruptible(&mlxsw_i2c->cmd.lock) < 0) {
  324. dev_err(&client->dev, "Could not acquire lock");
  325. return -EINVAL;
  326. }
  327. err = mlxsw_i2c_write(dev, reg_size, in_mbox, num, status);
  328. if (err)
  329. goto cmd_fail;
  330. /* No out mailbox is case of write transaction. */
  331. if (!out_mbox) {
  332. mutex_unlock(&mlxsw_i2c->cmd.lock);
  333. return 0;
  334. }
  335. /* Send read transaction to get output mailbox content. */
  336. read_tran[1].buf = out_mbox;
  337. for (i = 0; i < num; i++) {
  338. chunk_size = (reg_size > MLXSW_I2C_BLK_MAX) ?
  339. MLXSW_I2C_BLK_MAX : reg_size;
  340. read_tran[1].len = chunk_size;
  341. mlxsw_i2c_set_slave_addr(tran_buf, off);
  342. j = 0;
  343. end = jiffies + timeout;
  344. do {
  345. err = i2c_transfer(client->adapter, read_tran,
  346. ARRAY_SIZE(read_tran));
  347. if (err == ARRAY_SIZE(read_tran))
  348. break;
  349. cond_resched();
  350. } while ((time_before(jiffies, end)) ||
  351. (j++ < MLXSW_I2C_RETRY));
  352. if (err != ARRAY_SIZE(read_tran)) {
  353. if (!err)
  354. err = -EIO;
  355. goto cmd_fail;
  356. }
  357. off += chunk_size;
  358. reg_size -= chunk_size;
  359. read_tran[1].buf += chunk_size;
  360. }
  361. mutex_unlock(&mlxsw_i2c->cmd.lock);
  362. return 0;
  363. cmd_fail:
  364. mutex_unlock(&mlxsw_i2c->cmd.lock);
  365. return err;
  366. }
  367. static int mlxsw_i2c_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
  368. u32 in_mod, bool out_mbox_direct,
  369. char *in_mbox, size_t in_mbox_size,
  370. char *out_mbox, size_t out_mbox_size,
  371. u8 *status)
  372. {
  373. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  374. return mlxsw_i2c_cmd(mlxsw_i2c->dev, in_mbox_size, in_mbox,
  375. out_mbox_size, out_mbox, status);
  376. }
  377. static bool mlxsw_i2c_skb_transmit_busy(void *bus_priv,
  378. const struct mlxsw_tx_info *tx_info)
  379. {
  380. return false;
  381. }
  382. static int mlxsw_i2c_skb_transmit(void *bus_priv, struct sk_buff *skb,
  383. const struct mlxsw_tx_info *tx_info)
  384. {
  385. return 0;
  386. }
  387. static int
  388. mlxsw_i2c_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
  389. const struct mlxsw_config_profile *profile,
  390. struct mlxsw_res *resources)
  391. {
  392. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  393. mlxsw_i2c->core = mlxsw_core;
  394. return 0;
  395. }
  396. static void mlxsw_i2c_fini(void *bus_priv)
  397. {
  398. struct mlxsw_i2c *mlxsw_i2c = bus_priv;
  399. mlxsw_i2c->core = NULL;
  400. }
  401. static const struct mlxsw_bus mlxsw_i2c_bus = {
  402. .kind = "i2c",
  403. .init = mlxsw_i2c_init,
  404. .fini = mlxsw_i2c_fini,
  405. .skb_transmit_busy = mlxsw_i2c_skb_transmit_busy,
  406. .skb_transmit = mlxsw_i2c_skb_transmit,
  407. .cmd_exec = mlxsw_i2c_cmd_exec,
  408. };
  409. static int mlxsw_i2c_probe(struct i2c_client *client,
  410. const struct i2c_device_id *id)
  411. {
  412. struct mlxsw_i2c *mlxsw_i2c;
  413. u8 status;
  414. int err;
  415. mlxsw_i2c = devm_kzalloc(&client->dev, sizeof(*mlxsw_i2c), GFP_KERNEL);
  416. if (!mlxsw_i2c)
  417. return -ENOMEM;
  418. i2c_set_clientdata(client, mlxsw_i2c);
  419. mutex_init(&mlxsw_i2c->cmd.lock);
  420. /* In order to use mailboxes through the i2c, special area is reserved
  421. * on the i2c address space that can be used for input and output
  422. * mailboxes. Such mailboxes are called local mailboxes. When using a
  423. * local mailbox, software should specify 0 as the Input/Output
  424. * parameters. The location of the Local Mailbox addresses on the i2c
  425. * space can be retrieved through the QUERY_FW command.
  426. * For this purpose QUERY_FW is to be issued with opcode modifier equal
  427. * 0x01. For such command the output parameter is an immediate value.
  428. * Here QUERY_FW command is invoked for ASIC probing and for getting
  429. * local mailboxes addresses from immedate output parameters.
  430. */
  431. /* Prepare and write out Command Interface Register for transaction */
  432. err = mlxsw_i2c_write_cmd(client, mlxsw_i2c, 1);
  433. if (err) {
  434. dev_err(&client->dev, "Could not start transaction");
  435. goto errout;
  436. }
  437. /* Wait until go bit is cleared. */
  438. err = mlxsw_i2c_wait_go_bit(client, mlxsw_i2c, &status);
  439. if (err) {
  440. dev_err(&client->dev, "HW semaphore is not released");
  441. goto errout;
  442. }
  443. /* Validate transaction completion status. */
  444. if (status) {
  445. dev_err(&client->dev, "Bad transaction completion status %x\n",
  446. status);
  447. err = -EIO;
  448. goto errout;
  449. }
  450. /* Get mailbox offsets. */
  451. err = mlxsw_i2c_get_mbox(client, mlxsw_i2c);
  452. if (err < 0) {
  453. dev_err(&client->dev, "Fail to get mailboxes\n");
  454. goto errout;
  455. }
  456. dev_info(&client->dev, "%s mb size=%x off=0x%08x out mb size=%x off=0x%08x\n",
  457. id->name, mlxsw_i2c->cmd.mb_size_in,
  458. mlxsw_i2c->cmd.mb_off_in, mlxsw_i2c->cmd.mb_size_out,
  459. mlxsw_i2c->cmd.mb_off_out);
  460. /* Register device bus. */
  461. mlxsw_i2c->bus_info.device_kind = id->name;
  462. mlxsw_i2c->bus_info.device_name = client->name;
  463. mlxsw_i2c->bus_info.dev = &client->dev;
  464. mlxsw_i2c->dev = &client->dev;
  465. err = mlxsw_core_bus_device_register(&mlxsw_i2c->bus_info,
  466. &mlxsw_i2c_bus, mlxsw_i2c);
  467. if (err) {
  468. dev_err(&client->dev, "Fail to register core bus\n");
  469. return err;
  470. }
  471. return 0;
  472. errout:
  473. i2c_set_clientdata(client, NULL);
  474. return err;
  475. }
  476. static int mlxsw_i2c_remove(struct i2c_client *client)
  477. {
  478. struct mlxsw_i2c *mlxsw_i2c = i2c_get_clientdata(client);
  479. mlxsw_core_bus_device_unregister(mlxsw_i2c->core);
  480. mutex_destroy(&mlxsw_i2c->cmd.lock);
  481. return 0;
  482. }
  483. int mlxsw_i2c_driver_register(struct i2c_driver *i2c_driver)
  484. {
  485. i2c_driver->probe = mlxsw_i2c_probe;
  486. i2c_driver->remove = mlxsw_i2c_remove;
  487. return i2c_add_driver(i2c_driver);
  488. }
  489. EXPORT_SYMBOL(mlxsw_i2c_driver_register);
  490. void mlxsw_i2c_driver_unregister(struct i2c_driver *i2c_driver)
  491. {
  492. i2c_del_driver(i2c_driver);
  493. }
  494. EXPORT_SYMBOL(mlxsw_i2c_driver_unregister);
  495. MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
  496. MODULE_DESCRIPTION("Mellanox switch I2C interface driver");
  497. MODULE_LICENSE("Dual BSD/GPL");