i2c-xlp9xx.c 16 KB

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