i2c-xlp9xx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright (c) 2003-2015 Broadcom Corporation
  3. *
  4. * This file is licensed under the terms of the GNU General Public
  5. * License version 2. This program is licensed "as is" without any
  6. * warranty of any kind, whether express or implied.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/clk.h>
  10. #include <linux/completion.h>
  11. #include <linux/i2c.h>
  12. #include <linux/i2c-smbus.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/delay.h>
  20. #define XLP9XX_I2C_DIV 0x0
  21. #define XLP9XX_I2C_CTRL 0x1
  22. #define XLP9XX_I2C_CMD 0x2
  23. #define XLP9XX_I2C_STATUS 0x3
  24. #define XLP9XX_I2C_MTXFIFO 0x4
  25. #define XLP9XX_I2C_MRXFIFO 0x5
  26. #define XLP9XX_I2C_MFIFOCTRL 0x6
  27. #define XLP9XX_I2C_STXFIFO 0x7
  28. #define XLP9XX_I2C_SRXFIFO 0x8
  29. #define XLP9XX_I2C_SFIFOCTRL 0x9
  30. #define XLP9XX_I2C_SLAVEADDR 0xA
  31. #define XLP9XX_I2C_OWNADDR 0xB
  32. #define XLP9XX_I2C_FIFOWCNT 0xC
  33. #define XLP9XX_I2C_INTEN 0xD
  34. #define XLP9XX_I2C_INTST 0xE
  35. #define XLP9XX_I2C_WAITCNT 0xF
  36. #define XLP9XX_I2C_TIMEOUT 0X10
  37. #define XLP9XX_I2C_GENCALLADDR 0x11
  38. #define XLP9XX_I2C_STATUS_BUSY BIT(0)
  39. #define XLP9XX_I2C_CMD_START BIT(7)
  40. #define XLP9XX_I2C_CMD_STOP BIT(6)
  41. #define XLP9XX_I2C_CMD_READ BIT(5)
  42. #define XLP9XX_I2C_CMD_WRITE BIT(4)
  43. #define XLP9XX_I2C_CMD_ACK BIT(3)
  44. #define XLP9XX_I2C_CTRL_MCTLEN_SHIFT 16
  45. #define XLP9XX_I2C_CTRL_MCTLEN_MASK 0xffff0000
  46. #define XLP9XX_I2C_CTRL_RST BIT(8)
  47. #define XLP9XX_I2C_CTRL_EN BIT(6)
  48. #define XLP9XX_I2C_CTRL_MASTER BIT(4)
  49. #define XLP9XX_I2C_CTRL_FIFORD BIT(1)
  50. #define XLP9XX_I2C_CTRL_ADDMODE BIT(0)
  51. #define XLP9XX_I2C_INTEN_NACKADDR BIT(25)
  52. #define XLP9XX_I2C_INTEN_SADDR BIT(13)
  53. #define XLP9XX_I2C_INTEN_DATADONE BIT(12)
  54. #define XLP9XX_I2C_INTEN_ARLOST BIT(11)
  55. #define XLP9XX_I2C_INTEN_MFIFOFULL BIT(4)
  56. #define XLP9XX_I2C_INTEN_MFIFOEMTY BIT(3)
  57. #define XLP9XX_I2C_INTEN_MFIFOHI BIT(2)
  58. #define XLP9XX_I2C_INTEN_BUSERR BIT(0)
  59. #define XLP9XX_I2C_MFIFOCTRL_HITH_SHIFT 8
  60. #define XLP9XX_I2C_MFIFOCTRL_LOTH_SHIFT 0
  61. #define XLP9XX_I2C_MFIFOCTRL_RST BIT(16)
  62. #define XLP9XX_I2C_SLAVEADDR_RW BIT(0)
  63. #define XLP9XX_I2C_SLAVEADDR_ADDR_SHIFT 1
  64. #define XLP9XX_I2C_IP_CLK_FREQ 133000000UL
  65. #define XLP9XX_I2C_FIFO_SIZE 0x80U
  66. #define XLP9XX_I2C_TIMEOUT_MS 1000
  67. #define XLP9XX_I2C_BUSY_TIMEOUT 50
  68. #define XLP9XX_I2C_FIFO_WCNT_MASK 0xff
  69. #define XLP9XX_I2C_STATUS_ERRMASK (XLP9XX_I2C_INTEN_ARLOST | \
  70. XLP9XX_I2C_INTEN_NACKADDR | XLP9XX_I2C_INTEN_BUSERR)
  71. struct xlp9xx_i2c_dev {
  72. struct device *dev;
  73. struct i2c_adapter adapter;
  74. struct completion msg_complete;
  75. struct i2c_smbus_alert_setup alert_data;
  76. struct i2c_client *ara;
  77. int irq;
  78. bool msg_read;
  79. bool len_recv;
  80. bool client_pec;
  81. u32 __iomem *base;
  82. u32 msg_buf_remaining;
  83. u32 msg_len;
  84. u32 ip_clk_hz;
  85. u32 clk_hz;
  86. u32 msg_err;
  87. u8 *msg_buf;
  88. };
  89. static inline void xlp9xx_write_i2c_reg(struct xlp9xx_i2c_dev *priv,
  90. unsigned long reg, u32 val)
  91. {
  92. writel(val, priv->base + reg);
  93. }
  94. static inline u32 xlp9xx_read_i2c_reg(struct xlp9xx_i2c_dev *priv,
  95. unsigned long reg)
  96. {
  97. return readl(priv->base + reg);
  98. }
  99. static void xlp9xx_i2c_mask_irq(struct xlp9xx_i2c_dev *priv, u32 mask)
  100. {
  101. u32 inten;
  102. inten = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_INTEN) & ~mask;
  103. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, inten);
  104. }
  105. static void xlp9xx_i2c_unmask_irq(struct xlp9xx_i2c_dev *priv, u32 mask)
  106. {
  107. u32 inten;
  108. inten = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_INTEN) | mask;
  109. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, inten);
  110. }
  111. static void xlp9xx_i2c_update_rx_fifo_thres(struct xlp9xx_i2c_dev *priv)
  112. {
  113. u32 thres;
  114. if (priv->len_recv)
  115. /* interrupt after the first read to examine
  116. * the length byte before proceeding further
  117. */
  118. thres = 1;
  119. else if (priv->msg_buf_remaining > XLP9XX_I2C_FIFO_SIZE)
  120. thres = XLP9XX_I2C_FIFO_SIZE;
  121. else
  122. thres = priv->msg_buf_remaining;
  123. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_MFIFOCTRL,
  124. thres << XLP9XX_I2C_MFIFOCTRL_HITH_SHIFT);
  125. }
  126. static void xlp9xx_i2c_fill_tx_fifo(struct xlp9xx_i2c_dev *priv)
  127. {
  128. u32 len, i;
  129. u8 *buf = priv->msg_buf;
  130. len = min(priv->msg_buf_remaining, XLP9XX_I2C_FIFO_SIZE);
  131. for (i = 0; i < len; i++)
  132. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_MTXFIFO, buf[i]);
  133. priv->msg_buf_remaining -= len;
  134. priv->msg_buf += len;
  135. }
  136. static void xlp9xx_i2c_update_rlen(struct xlp9xx_i2c_dev *priv)
  137. {
  138. u32 val, len;
  139. /*
  140. * Update receive length. Re-read len to get the latest value,
  141. * and then add 4 to have a minimum value that can be safely
  142. * written. This is to account for the byte read above, the
  143. * transfer in progress and any delays in the register I/O
  144. */
  145. val = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_CTRL);
  146. len = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_FIFOWCNT) &
  147. XLP9XX_I2C_FIFO_WCNT_MASK;
  148. len = max_t(u32, priv->msg_len, len + 4);
  149. if (len >= I2C_SMBUS_BLOCK_MAX + 2)
  150. return;
  151. val = (val & ~XLP9XX_I2C_CTRL_MCTLEN_MASK) |
  152. (len << XLP9XX_I2C_CTRL_MCTLEN_SHIFT);
  153. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, val);
  154. }
  155. static void xlp9xx_i2c_drain_rx_fifo(struct xlp9xx_i2c_dev *priv)
  156. {
  157. u32 len, i;
  158. u8 rlen, *buf = priv->msg_buf;
  159. len = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_FIFOWCNT) &
  160. XLP9XX_I2C_FIFO_WCNT_MASK;
  161. if (!len)
  162. return;
  163. if (priv->len_recv) {
  164. /* read length byte */
  165. rlen = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_MRXFIFO);
  166. /*
  167. * We expect at least 2 interrupts for I2C_M_RECV_LEN
  168. * transactions. The length is updated during the first
  169. * interrupt, and the buffer contents are only copied
  170. * during subsequent interrupts. If in case the interrupts
  171. * get merged we would complete the transaction without
  172. * copying out the bytes from RX fifo. To avoid this now we
  173. * drain the fifo as and when data is available.
  174. * We drained the rlen byte already, decrement total length
  175. * by one.
  176. */
  177. len--;
  178. if (rlen > I2C_SMBUS_BLOCK_MAX || rlen == 0) {
  179. rlen = 0; /*abort transfer */
  180. priv->msg_buf_remaining = 0;
  181. priv->msg_len = 0;
  182. xlp9xx_i2c_update_rlen(priv);
  183. return;
  184. }
  185. *buf++ = rlen;
  186. if (priv->client_pec)
  187. ++rlen; /* account for error check byte */
  188. /* update remaining bytes and message length */
  189. priv->msg_buf_remaining = rlen;
  190. priv->msg_len = rlen + 1;
  191. xlp9xx_i2c_update_rlen(priv);
  192. priv->len_recv = false;
  193. }
  194. len = min(priv->msg_buf_remaining, len);
  195. for (i = 0; i < len; i++, buf++)
  196. *buf = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_MRXFIFO);
  197. priv->msg_buf_remaining -= len;
  198. priv->msg_buf = buf;
  199. if (priv->msg_buf_remaining)
  200. xlp9xx_i2c_update_rx_fifo_thres(priv);
  201. }
  202. static irqreturn_t xlp9xx_i2c_isr(int irq, void *dev_id)
  203. {
  204. struct xlp9xx_i2c_dev *priv = dev_id;
  205. u32 status;
  206. status = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_INTST);
  207. if (status == 0)
  208. return IRQ_NONE;
  209. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTST, status);
  210. if (status & XLP9XX_I2C_STATUS_ERRMASK) {
  211. priv->msg_err = status;
  212. goto xfer_done;
  213. }
  214. /* SADDR ACK for SMBUS_QUICK */
  215. if ((status & XLP9XX_I2C_INTEN_SADDR) && (priv->msg_len == 0))
  216. goto xfer_done;
  217. if (!priv->msg_read) {
  218. if (status & XLP9XX_I2C_INTEN_MFIFOEMTY) {
  219. /* TX FIFO got empty, fill it up again */
  220. if (priv->msg_buf_remaining)
  221. xlp9xx_i2c_fill_tx_fifo(priv);
  222. else
  223. xlp9xx_i2c_mask_irq(priv,
  224. XLP9XX_I2C_INTEN_MFIFOEMTY);
  225. }
  226. } else {
  227. if (status & (XLP9XX_I2C_INTEN_DATADONE |
  228. XLP9XX_I2C_INTEN_MFIFOHI)) {
  229. /* data is in FIFO, read it */
  230. if (priv->msg_buf_remaining)
  231. xlp9xx_i2c_drain_rx_fifo(priv);
  232. }
  233. }
  234. /* Transfer complete */
  235. if (status & XLP9XX_I2C_INTEN_DATADONE)
  236. goto xfer_done;
  237. return IRQ_HANDLED;
  238. xfer_done:
  239. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, 0);
  240. complete(&priv->msg_complete);
  241. return IRQ_HANDLED;
  242. }
  243. static int xlp9xx_i2c_check_bus_status(struct xlp9xx_i2c_dev *priv)
  244. {
  245. u32 status;
  246. u32 busy_timeout = XLP9XX_I2C_BUSY_TIMEOUT;
  247. while (busy_timeout) {
  248. status = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_STATUS);
  249. if ((status & XLP9XX_I2C_STATUS_BUSY) == 0)
  250. break;
  251. busy_timeout--;
  252. usleep_range(1000, 1100);
  253. }
  254. if (!busy_timeout)
  255. return -EIO;
  256. return 0;
  257. }
  258. static int xlp9xx_i2c_init(struct xlp9xx_i2c_dev *priv)
  259. {
  260. u32 prescale;
  261. /*
  262. * The controller uses 5 * SCL clock internally.
  263. * So prescale value should be divided by 5.
  264. */
  265. prescale = DIV_ROUND_UP(priv->ip_clk_hz, priv->clk_hz);
  266. prescale = ((prescale - 8) / 5) - 1;
  267. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, XLP9XX_I2C_CTRL_RST);
  268. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, XLP9XX_I2C_CTRL_EN |
  269. XLP9XX_I2C_CTRL_MASTER);
  270. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_DIV, prescale);
  271. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, 0);
  272. return 0;
  273. }
  274. static int xlp9xx_i2c_xfer_msg(struct xlp9xx_i2c_dev *priv, struct i2c_msg *msg,
  275. int last_msg)
  276. {
  277. unsigned long timeleft;
  278. u32 intr_mask, cmd, val, len;
  279. priv->msg_buf = msg->buf;
  280. priv->msg_buf_remaining = priv->msg_len = msg->len;
  281. priv->msg_err = 0;
  282. priv->msg_read = (msg->flags & I2C_M_RD);
  283. reinit_completion(&priv->msg_complete);
  284. /* Reset FIFO */
  285. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_MFIFOCTRL,
  286. XLP9XX_I2C_MFIFOCTRL_RST);
  287. /* set slave addr */
  288. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_SLAVEADDR,
  289. (msg->addr << XLP9XX_I2C_SLAVEADDR_ADDR_SHIFT) |
  290. (priv->msg_read ? XLP9XX_I2C_SLAVEADDR_RW : 0));
  291. /* Build control word for transfer */
  292. val = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_CTRL);
  293. if (!priv->msg_read)
  294. val &= ~XLP9XX_I2C_CTRL_FIFORD;
  295. else
  296. val |= XLP9XX_I2C_CTRL_FIFORD; /* read */
  297. if (msg->flags & I2C_M_TEN)
  298. val |= XLP9XX_I2C_CTRL_ADDMODE; /* 10-bit address mode*/
  299. else
  300. val &= ~XLP9XX_I2C_CTRL_ADDMODE;
  301. priv->len_recv = msg->flags & I2C_M_RECV_LEN;
  302. len = priv->len_recv ? I2C_SMBUS_BLOCK_MAX + 2 : msg->len;
  303. priv->client_pec = msg->flags & I2C_CLIENT_PEC;
  304. /* set FIFO threshold if reading */
  305. if (priv->msg_read)
  306. xlp9xx_i2c_update_rx_fifo_thres(priv);
  307. /* set data length to be transferred */
  308. val = (val & ~XLP9XX_I2C_CTRL_MCTLEN_MASK) |
  309. (len << XLP9XX_I2C_CTRL_MCTLEN_SHIFT);
  310. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, val);
  311. /* fill fifo during tx */
  312. if (!priv->msg_read)
  313. xlp9xx_i2c_fill_tx_fifo(priv);
  314. /* set interrupt mask */
  315. intr_mask = (XLP9XX_I2C_INTEN_ARLOST | XLP9XX_I2C_INTEN_BUSERR |
  316. XLP9XX_I2C_INTEN_NACKADDR | XLP9XX_I2C_INTEN_DATADONE);
  317. if (priv->msg_read) {
  318. intr_mask |= XLP9XX_I2C_INTEN_MFIFOHI;
  319. if (msg->len == 0)
  320. intr_mask |= XLP9XX_I2C_INTEN_SADDR;
  321. } else {
  322. if (msg->len == 0)
  323. intr_mask |= XLP9XX_I2C_INTEN_SADDR;
  324. else
  325. intr_mask |= XLP9XX_I2C_INTEN_MFIFOEMTY;
  326. }
  327. xlp9xx_i2c_unmask_irq(priv, intr_mask);
  328. /* set cmd reg */
  329. cmd = XLP9XX_I2C_CMD_START;
  330. if (msg->len)
  331. cmd |= (priv->msg_read ?
  332. XLP9XX_I2C_CMD_READ : XLP9XX_I2C_CMD_WRITE);
  333. if (last_msg)
  334. cmd |= XLP9XX_I2C_CMD_STOP;
  335. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CMD, cmd);
  336. timeleft = msecs_to_jiffies(XLP9XX_I2C_TIMEOUT_MS);
  337. timeleft = wait_for_completion_timeout(&priv->msg_complete, timeleft);
  338. if (priv->msg_err & XLP9XX_I2C_INTEN_BUSERR) {
  339. dev_dbg(priv->dev, "transfer error %x!\n", priv->msg_err);
  340. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CMD, XLP9XX_I2C_CMD_STOP);
  341. return -EIO;
  342. } else if (priv->msg_err & XLP9XX_I2C_INTEN_NACKADDR) {
  343. return -ENXIO;
  344. }
  345. if (timeleft == 0) {
  346. dev_dbg(priv->dev, "i2c transfer timed out!\n");
  347. xlp9xx_i2c_init(priv);
  348. return -ETIMEDOUT;
  349. }
  350. /* update msg->len with actual received length */
  351. if (msg->flags & I2C_M_RECV_LEN) {
  352. if (!priv->msg_len)
  353. return -EPROTO;
  354. msg->len = priv->msg_len;
  355. }
  356. return 0;
  357. }
  358. static int xlp9xx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  359. int num)
  360. {
  361. int i, ret;
  362. struct xlp9xx_i2c_dev *priv = i2c_get_adapdata(adap);
  363. ret = xlp9xx_i2c_check_bus_status(priv);
  364. if (ret) {
  365. xlp9xx_i2c_init(priv);
  366. ret = xlp9xx_i2c_check_bus_status(priv);
  367. if (ret)
  368. return ret;
  369. }
  370. for (i = 0; i < num; i++) {
  371. ret = xlp9xx_i2c_xfer_msg(priv, &msgs[i], i == num - 1);
  372. if (ret != 0)
  373. return ret;
  374. }
  375. return num;
  376. }
  377. static u32 xlp9xx_i2c_functionality(struct i2c_adapter *adapter)
  378. {
  379. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  380. I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
  381. }
  382. static const struct i2c_algorithm xlp9xx_i2c_algo = {
  383. .master_xfer = xlp9xx_i2c_xfer,
  384. .functionality = xlp9xx_i2c_functionality,
  385. };
  386. static int xlp9xx_i2c_get_frequency(struct platform_device *pdev,
  387. struct xlp9xx_i2c_dev *priv)
  388. {
  389. struct clk *clk;
  390. u32 freq;
  391. int err;
  392. clk = devm_clk_get(&pdev->dev, NULL);
  393. if (IS_ERR(clk)) {
  394. priv->ip_clk_hz = XLP9XX_I2C_IP_CLK_FREQ;
  395. dev_dbg(&pdev->dev, "using default input frequency %u\n",
  396. priv->ip_clk_hz);
  397. } else {
  398. priv->ip_clk_hz = clk_get_rate(clk);
  399. }
  400. err = device_property_read_u32(&pdev->dev, "clock-frequency", &freq);
  401. if (err) {
  402. freq = I2C_MAX_STANDARD_MODE_FREQ;
  403. dev_dbg(&pdev->dev, "using default frequency %u\n", freq);
  404. } else if (freq == 0 || freq > I2C_MAX_FAST_MODE_FREQ) {
  405. dev_warn(&pdev->dev, "invalid frequency %u, using default\n",
  406. freq);
  407. freq = I2C_MAX_STANDARD_MODE_FREQ;
  408. }
  409. priv->clk_hz = freq;
  410. return 0;
  411. }
  412. static int xlp9xx_i2c_smbus_setup(struct xlp9xx_i2c_dev *priv,
  413. struct platform_device *pdev)
  414. {
  415. struct i2c_client *ara;
  416. if (!priv->alert_data.irq)
  417. return -EINVAL;
  418. ara = i2c_new_smbus_alert_device(&priv->adapter, &priv->alert_data);
  419. if (IS_ERR(ara))
  420. return PTR_ERR(ara);
  421. priv->ara = ara;
  422. return 0;
  423. }
  424. static int xlp9xx_i2c_probe(struct platform_device *pdev)
  425. {
  426. struct xlp9xx_i2c_dev *priv;
  427. int err = 0;
  428. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  429. if (!priv)
  430. return -ENOMEM;
  431. priv->base = devm_platform_ioremap_resource(pdev, 0);
  432. if (IS_ERR(priv->base))
  433. return PTR_ERR(priv->base);
  434. priv->irq = platform_get_irq(pdev, 0);
  435. if (priv->irq < 0)
  436. return priv->irq;
  437. /* SMBAlert irq */
  438. priv->alert_data.irq = platform_get_irq(pdev, 1);
  439. if (priv->alert_data.irq <= 0)
  440. priv->alert_data.irq = 0;
  441. xlp9xx_i2c_get_frequency(pdev, priv);
  442. xlp9xx_i2c_init(priv);
  443. err = devm_request_irq(&pdev->dev, priv->irq, xlp9xx_i2c_isr, 0,
  444. pdev->name, priv);
  445. if (err) {
  446. dev_err(&pdev->dev, "IRQ request failed!\n");
  447. return err;
  448. }
  449. init_completion(&priv->msg_complete);
  450. priv->adapter.dev.parent = &pdev->dev;
  451. priv->adapter.algo = &xlp9xx_i2c_algo;
  452. priv->adapter.class = I2C_CLASS_HWMON;
  453. ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&pdev->dev));
  454. priv->adapter.dev.of_node = pdev->dev.of_node;
  455. priv->dev = &pdev->dev;
  456. snprintf(priv->adapter.name, sizeof(priv->adapter.name), "xlp9xx-i2c");
  457. i2c_set_adapdata(&priv->adapter, priv);
  458. err = i2c_add_adapter(&priv->adapter);
  459. if (err)
  460. return err;
  461. err = xlp9xx_i2c_smbus_setup(priv, pdev);
  462. if (err)
  463. dev_dbg(&pdev->dev, "No active SMBus alert %d\n", err);
  464. platform_set_drvdata(pdev, priv);
  465. dev_dbg(&pdev->dev, "I2C bus:%d added\n", priv->adapter.nr);
  466. return 0;
  467. }
  468. static int xlp9xx_i2c_remove(struct platform_device *pdev)
  469. {
  470. struct xlp9xx_i2c_dev *priv;
  471. priv = platform_get_drvdata(pdev);
  472. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, 0);
  473. synchronize_irq(priv->irq);
  474. i2c_del_adapter(&priv->adapter);
  475. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, 0);
  476. return 0;
  477. }
  478. #ifdef CONFIG_ACPI
  479. static const struct acpi_device_id xlp9xx_i2c_acpi_ids[] = {
  480. {"BRCM9007", 0},
  481. {"CAV9007", 0},
  482. {}
  483. };
  484. MODULE_DEVICE_TABLE(acpi, xlp9xx_i2c_acpi_ids);
  485. #endif
  486. static struct platform_driver xlp9xx_i2c_driver = {
  487. .probe = xlp9xx_i2c_probe,
  488. .remove = xlp9xx_i2c_remove,
  489. .driver = {
  490. .name = "xlp9xx-i2c",
  491. .acpi_match_table = ACPI_PTR(xlp9xx_i2c_acpi_ids),
  492. },
  493. };
  494. module_platform_driver(xlp9xx_i2c_driver);
  495. MODULE_AUTHOR("Subhendu Sekhar Behera <[email protected]>");
  496. MODULE_DESCRIPTION("XLP9XX/5XX I2C Bus Controller Driver");
  497. MODULE_LICENSE("GPL v2");