i2c-tegra.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * drivers/i2c/busses/i2c-tegra.c
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. * Author: Colin Cross <ccross@android.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #include <linux/i2c.h>
  23. #include <linux/io.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c-tegra.h>
  28. #include <linux/of_i2c.h>
  29. #include <linux/of_device.h>
  30. #include <linux/module.h>
  31. #include <asm/unaligned.h>
  32. #include <mach/clk.h>
  33. #define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
  34. #define BYTES_PER_FIFO_WORD 4
  35. #define I2C_CNFG 0x000
  36. #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
  37. #define I2C_CNFG_PACKET_MODE_EN (1<<10)
  38. #define I2C_CNFG_NEW_MASTER_FSM (1<<11)
  39. #define I2C_STATUS 0x01C
  40. #define I2C_SL_CNFG 0x020
  41. #define I2C_SL_CNFG_NACK (1<<1)
  42. #define I2C_SL_CNFG_NEWSL (1<<2)
  43. #define I2C_SL_ADDR1 0x02c
  44. #define I2C_SL_ADDR2 0x030
  45. #define I2C_TX_FIFO 0x050
  46. #define I2C_RX_FIFO 0x054
  47. #define I2C_PACKET_TRANSFER_STATUS 0x058
  48. #define I2C_FIFO_CONTROL 0x05c
  49. #define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
  50. #define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
  51. #define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
  52. #define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
  53. #define I2C_FIFO_STATUS 0x060
  54. #define I2C_FIFO_STATUS_TX_MASK 0xF0
  55. #define I2C_FIFO_STATUS_TX_SHIFT 4
  56. #define I2C_FIFO_STATUS_RX_MASK 0x0F
  57. #define I2C_FIFO_STATUS_RX_SHIFT 0
  58. #define I2C_INT_MASK 0x064
  59. #define I2C_INT_STATUS 0x068
  60. #define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
  61. #define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
  62. #define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
  63. #define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
  64. #define I2C_INT_NO_ACK (1<<3)
  65. #define I2C_INT_ARBITRATION_LOST (1<<2)
  66. #define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
  67. #define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
  68. #define I2C_CLK_DIVISOR 0x06c
  69. #define DVC_CTRL_REG1 0x000
  70. #define DVC_CTRL_REG1_INTR_EN (1<<10)
  71. #define DVC_CTRL_REG2 0x004
  72. #define DVC_CTRL_REG3 0x008
  73. #define DVC_CTRL_REG3_SW_PROG (1<<26)
  74. #define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
  75. #define DVC_STATUS 0x00c
  76. #define DVC_STATUS_I2C_DONE_INTR (1<<30)
  77. #define I2C_ERR_NONE 0x00
  78. #define I2C_ERR_NO_ACK 0x01
  79. #define I2C_ERR_ARBITRATION_LOST 0x02
  80. #define I2C_ERR_UNKNOWN_INTERRUPT 0x04
  81. #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
  82. #define PACKET_HEADER0_PACKET_ID_SHIFT 16
  83. #define PACKET_HEADER0_CONT_ID_SHIFT 12
  84. #define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
  85. #define I2C_HEADER_HIGHSPEED_MODE (1<<22)
  86. #define I2C_HEADER_CONT_ON_NAK (1<<21)
  87. #define I2C_HEADER_SEND_START_BYTE (1<<20)
  88. #define I2C_HEADER_READ (1<<19)
  89. #define I2C_HEADER_10BIT_ADDR (1<<18)
  90. #define I2C_HEADER_IE_ENABLE (1<<17)
  91. #define I2C_HEADER_REPEAT_START (1<<16)
  92. #define I2C_HEADER_CONTINUE_XFER (1<<15)
  93. #define I2C_HEADER_MASTER_ADDR_SHIFT 12
  94. #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
  95. /*
  96. * msg_end_type: The bus control which need to be send at end of transfer.
  97. * @MSG_END_STOP: Send stop pulse at end of transfer.
  98. * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
  99. * @MSG_END_CONTINUE: The following on message is coming and so do not send
  100. * stop or repeat start.
  101. */
  102. enum msg_end_type {
  103. MSG_END_STOP,
  104. MSG_END_REPEAT_START,
  105. MSG_END_CONTINUE,
  106. };
  107. /**
  108. * struct tegra_i2c_hw_feature : Different HW support on Tegra
  109. * @has_continue_xfer_support: Continue transfer supports.
  110. */
  111. struct tegra_i2c_hw_feature {
  112. bool has_continue_xfer_support;
  113. };
  114. /**
  115. * struct tegra_i2c_dev - per device i2c context
  116. * @dev: device reference for power management
  117. * @hw: Tegra i2c hw feature.
  118. * @adapter: core i2c layer adapter information
  119. * @div_clk: clock reference for div clock of i2c controller.
  120. * @fast_clk: clock reference for fast clock of i2c controller.
  121. * @base: ioremapped registers cookie
  122. * @cont_id: i2c controller id, used for for packet header
  123. * @irq: irq number of transfer complete interrupt
  124. * @is_dvc: identifies the DVC i2c controller, has a different register layout
  125. * @msg_complete: transfer completion notifier
  126. * @msg_err: error code for completed message
  127. * @msg_buf: pointer to current message data
  128. * @msg_buf_remaining: size of unsent data in the message buffer
  129. * @msg_read: identifies read transfers
  130. * @bus_clk_rate: current i2c bus clock rate
  131. * @is_suspended: prevents i2c controller accesses after suspend is called
  132. */
  133. struct tegra_i2c_dev {
  134. struct device *dev;
  135. const struct tegra_i2c_hw_feature *hw;
  136. struct i2c_adapter adapter;
  137. struct clk *div_clk;
  138. struct clk *fast_clk;
  139. void __iomem *base;
  140. int cont_id;
  141. int irq;
  142. bool irq_disabled;
  143. int is_dvc;
  144. struct completion msg_complete;
  145. int msg_err;
  146. u8 *msg_buf;
  147. size_t msg_buf_remaining;
  148. int msg_read;
  149. unsigned long bus_clk_rate;
  150. bool is_suspended;
  151. };
  152. static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
  153. {
  154. writel(val, i2c_dev->base + reg);
  155. }
  156. static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
  157. {
  158. return readl(i2c_dev->base + reg);
  159. }
  160. /*
  161. * i2c_writel and i2c_readl will offset the register if necessary to talk
  162. * to the I2C block inside the DVC block
  163. */
  164. static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
  165. unsigned long reg)
  166. {
  167. if (i2c_dev->is_dvc)
  168. reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
  169. return reg;
  170. }
  171. static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
  172. unsigned long reg)
  173. {
  174. writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  175. /* Read back register to make sure that register writes completed */
  176. if (reg != I2C_TX_FIFO)
  177. readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  178. }
  179. static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
  180. {
  181. return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  182. }
  183. static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
  184. unsigned long reg, int len)
  185. {
  186. writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
  187. }
  188. static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
  189. unsigned long reg, int len)
  190. {
  191. readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
  192. }
  193. static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
  194. {
  195. u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
  196. int_mask &= ~mask;
  197. i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
  198. }
  199. static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
  200. {
  201. u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
  202. int_mask |= mask;
  203. i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
  204. }
  205. static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
  206. {
  207. unsigned long timeout = jiffies + HZ;
  208. u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
  209. val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
  210. i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
  211. while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
  212. (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
  213. if (time_after(jiffies, timeout)) {
  214. dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
  215. return -ETIMEDOUT;
  216. }
  217. msleep(1);
  218. }
  219. return 0;
  220. }
  221. static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
  222. {
  223. u32 val;
  224. int rx_fifo_avail;
  225. u8 *buf = i2c_dev->msg_buf;
  226. size_t buf_remaining = i2c_dev->msg_buf_remaining;
  227. int words_to_transfer;
  228. val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
  229. rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
  230. I2C_FIFO_STATUS_RX_SHIFT;
  231. /* Rounds down to not include partial word at the end of buf */
  232. words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
  233. if (words_to_transfer > rx_fifo_avail)
  234. words_to_transfer = rx_fifo_avail;
  235. i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
  236. buf += words_to_transfer * BYTES_PER_FIFO_WORD;
  237. buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
  238. rx_fifo_avail -= words_to_transfer;
  239. /*
  240. * If there is a partial word at the end of buf, handle it manually to
  241. * prevent overwriting past the end of buf
  242. */
  243. if (rx_fifo_avail > 0 && buf_remaining > 0) {
  244. BUG_ON(buf_remaining > 3);
  245. val = i2c_readl(i2c_dev, I2C_RX_FIFO);
  246. memcpy(buf, &val, buf_remaining);
  247. buf_remaining = 0;
  248. rx_fifo_avail--;
  249. }
  250. BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
  251. i2c_dev->msg_buf_remaining = buf_remaining;
  252. i2c_dev->msg_buf = buf;
  253. return 0;
  254. }
  255. static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
  256. {
  257. u32 val;
  258. int tx_fifo_avail;
  259. u8 *buf = i2c_dev->msg_buf;
  260. size_t buf_remaining = i2c_dev->msg_buf_remaining;
  261. int words_to_transfer;
  262. val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
  263. tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
  264. I2C_FIFO_STATUS_TX_SHIFT;
  265. /* Rounds down to not include partial word at the end of buf */
  266. words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
  267. /* It's very common to have < 4 bytes, so optimize that case. */
  268. if (words_to_transfer) {
  269. if (words_to_transfer > tx_fifo_avail)
  270. words_to_transfer = tx_fifo_avail;
  271. /*
  272. * Update state before writing to FIFO. If this casues us
  273. * to finish writing all bytes (AKA buf_remaining goes to 0) we
  274. * have a potential for an interrupt (PACKET_XFER_COMPLETE is
  275. * not maskable). We need to make sure that the isr sees
  276. * buf_remaining as 0 and doesn't call us back re-entrantly.
  277. */
  278. buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
  279. tx_fifo_avail -= words_to_transfer;
  280. i2c_dev->msg_buf_remaining = buf_remaining;
  281. i2c_dev->msg_buf = buf +
  282. words_to_transfer * BYTES_PER_FIFO_WORD;
  283. barrier();
  284. i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
  285. buf += words_to_transfer * BYTES_PER_FIFO_WORD;
  286. }
  287. /*
  288. * If there is a partial word at the end of buf, handle it manually to
  289. * prevent reading past the end of buf, which could cross a page
  290. * boundary and fault.
  291. */
  292. if (tx_fifo_avail > 0 && buf_remaining > 0) {
  293. BUG_ON(buf_remaining > 3);
  294. memcpy(&val, buf, buf_remaining);
  295. /* Again update before writing to FIFO to make sure isr sees. */
  296. i2c_dev->msg_buf_remaining = 0;
  297. i2c_dev->msg_buf = NULL;
  298. barrier();
  299. i2c_writel(i2c_dev, val, I2C_TX_FIFO);
  300. }
  301. return 0;
  302. }
  303. /*
  304. * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
  305. * block. This block is identical to the rest of the I2C blocks, except that
  306. * it only supports master mode, it has registers moved around, and it needs
  307. * some extra init to get it into I2C mode. The register moves are handled
  308. * by i2c_readl and i2c_writel
  309. */
  310. static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
  311. {
  312. u32 val = 0;
  313. val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
  314. val |= DVC_CTRL_REG3_SW_PROG;
  315. val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
  316. dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
  317. val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
  318. val |= DVC_CTRL_REG1_INTR_EN;
  319. dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
  320. }
  321. static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
  322. {
  323. u32 val;
  324. int err = 0;
  325. clk_prepare_enable(i2c_dev->div_clk);
  326. tegra_periph_reset_assert(i2c_dev->div_clk);
  327. udelay(2);
  328. tegra_periph_reset_deassert(i2c_dev->div_clk);
  329. if (i2c_dev->is_dvc)
  330. tegra_dvc_init(i2c_dev);
  331. val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
  332. (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
  333. i2c_writel(i2c_dev, val, I2C_CNFG);
  334. i2c_writel(i2c_dev, 0, I2C_INT_MASK);
  335. clk_set_rate(i2c_dev->div_clk, i2c_dev->bus_clk_rate * 8);
  336. if (!i2c_dev->is_dvc) {
  337. u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
  338. sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
  339. i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
  340. i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
  341. i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
  342. }
  343. val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
  344. 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
  345. i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
  346. if (tegra_i2c_flush_fifos(i2c_dev))
  347. err = -ETIMEDOUT;
  348. clk_disable_unprepare(i2c_dev->div_clk);
  349. if (i2c_dev->irq_disabled) {
  350. i2c_dev->irq_disabled = 0;
  351. enable_irq(i2c_dev->irq);
  352. }
  353. return err;
  354. }
  355. static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
  356. {
  357. u32 status;
  358. const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
  359. struct tegra_i2c_dev *i2c_dev = dev_id;
  360. status = i2c_readl(i2c_dev, I2C_INT_STATUS);
  361. if (status == 0) {
  362. dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
  363. i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
  364. i2c_readl(i2c_dev, I2C_STATUS),
  365. i2c_readl(i2c_dev, I2C_CNFG));
  366. i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
  367. if (!i2c_dev->irq_disabled) {
  368. disable_irq_nosync(i2c_dev->irq);
  369. i2c_dev->irq_disabled = 1;
  370. }
  371. goto err;
  372. }
  373. if (unlikely(status & status_err)) {
  374. if (status & I2C_INT_NO_ACK)
  375. i2c_dev->msg_err |= I2C_ERR_NO_ACK;
  376. if (status & I2C_INT_ARBITRATION_LOST)
  377. i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
  378. goto err;
  379. }
  380. if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
  381. if (i2c_dev->msg_buf_remaining)
  382. tegra_i2c_empty_rx_fifo(i2c_dev);
  383. else
  384. BUG();
  385. }
  386. if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
  387. if (i2c_dev->msg_buf_remaining)
  388. tegra_i2c_fill_tx_fifo(i2c_dev);
  389. else
  390. tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
  391. }
  392. i2c_writel(i2c_dev, status, I2C_INT_STATUS);
  393. if (i2c_dev->is_dvc)
  394. dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
  395. if (status & I2C_INT_PACKET_XFER_COMPLETE) {
  396. BUG_ON(i2c_dev->msg_buf_remaining);
  397. complete(&i2c_dev->msg_complete);
  398. }
  399. return IRQ_HANDLED;
  400. err:
  401. /* An error occurred, mask all interrupts */
  402. tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
  403. I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
  404. I2C_INT_RX_FIFO_DATA_REQ);
  405. i2c_writel(i2c_dev, status, I2C_INT_STATUS);
  406. if (i2c_dev->is_dvc)
  407. dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
  408. complete(&i2c_dev->msg_complete);
  409. return IRQ_HANDLED;
  410. }
  411. static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
  412. struct i2c_msg *msg, enum msg_end_type end_state)
  413. {
  414. u32 packet_header;
  415. u32 int_mask;
  416. int ret;
  417. tegra_i2c_flush_fifos(i2c_dev);
  418. if (msg->len == 0)
  419. return -EINVAL;
  420. i2c_dev->msg_buf = msg->buf;
  421. i2c_dev->msg_buf_remaining = msg->len;
  422. i2c_dev->msg_err = I2C_ERR_NONE;
  423. i2c_dev->msg_read = (msg->flags & I2C_M_RD);
  424. INIT_COMPLETION(i2c_dev->msg_complete);
  425. packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
  426. PACKET_HEADER0_PROTOCOL_I2C |
  427. (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
  428. (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
  429. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  430. packet_header = msg->len - 1;
  431. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  432. packet_header = I2C_HEADER_IE_ENABLE;
  433. if (end_state == MSG_END_CONTINUE)
  434. packet_header |= I2C_HEADER_CONTINUE_XFER;
  435. else if (end_state == MSG_END_REPEAT_START)
  436. packet_header |= I2C_HEADER_REPEAT_START;
  437. if (msg->flags & I2C_M_TEN) {
  438. packet_header |= msg->addr;
  439. packet_header |= I2C_HEADER_10BIT_ADDR;
  440. } else {
  441. packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
  442. }
  443. if (msg->flags & I2C_M_IGNORE_NAK)
  444. packet_header |= I2C_HEADER_CONT_ON_NAK;
  445. if (msg->flags & I2C_M_RD)
  446. packet_header |= I2C_HEADER_READ;
  447. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  448. if (!(msg->flags & I2C_M_RD))
  449. tegra_i2c_fill_tx_fifo(i2c_dev);
  450. int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
  451. if (msg->flags & I2C_M_RD)
  452. int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
  453. else if (i2c_dev->msg_buf_remaining)
  454. int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
  455. tegra_i2c_unmask_irq(i2c_dev, int_mask);
  456. dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
  457. i2c_readl(i2c_dev, I2C_INT_MASK));
  458. ret = wait_for_completion_timeout(&i2c_dev->msg_complete, TEGRA_I2C_TIMEOUT);
  459. tegra_i2c_mask_irq(i2c_dev, int_mask);
  460. if (WARN_ON(ret == 0)) {
  461. dev_err(i2c_dev->dev, "i2c transfer timed out\n");
  462. tegra_i2c_init(i2c_dev);
  463. return -ETIMEDOUT;
  464. }
  465. dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
  466. ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
  467. if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
  468. return 0;
  469. /*
  470. * NACK interrupt is generated before the I2C controller generates the
  471. * STOP condition on the bus. So wait for 2 clock periods before resetting
  472. * the controller so that STOP condition has been delivered properly.
  473. */
  474. if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
  475. udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
  476. tegra_i2c_init(i2c_dev);
  477. if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
  478. if (msg->flags & I2C_M_IGNORE_NAK)
  479. return 0;
  480. return -EREMOTEIO;
  481. }
  482. return -EIO;
  483. }
  484. static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  485. int num)
  486. {
  487. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  488. int i;
  489. int ret = 0;
  490. if (i2c_dev->is_suspended)
  491. return -EBUSY;
  492. clk_prepare_enable(i2c_dev->div_clk);
  493. for (i = 0; i < num; i++) {
  494. enum msg_end_type end_type = MSG_END_STOP;
  495. if (i < (num - 1)) {
  496. if (msgs[i + 1].flags & I2C_M_NOSTART)
  497. end_type = MSG_END_CONTINUE;
  498. else
  499. end_type = MSG_END_REPEAT_START;
  500. }
  501. ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
  502. if (ret)
  503. break;
  504. }
  505. clk_disable_unprepare(i2c_dev->div_clk);
  506. return ret ?: i;
  507. }
  508. static u32 tegra_i2c_func(struct i2c_adapter *adap)
  509. {
  510. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  511. u32 ret = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
  512. I2C_FUNC_PROTOCOL_MANGLING;
  513. if (i2c_dev->hw->has_continue_xfer_support)
  514. ret |= I2C_FUNC_NOSTART;
  515. return ret;
  516. }
  517. static const struct i2c_algorithm tegra_i2c_algo = {
  518. .master_xfer = tegra_i2c_xfer,
  519. .functionality = tegra_i2c_func,
  520. };
  521. static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
  522. .has_continue_xfer_support = false,
  523. };
  524. static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
  525. .has_continue_xfer_support = true,
  526. };
  527. #if defined(CONFIG_OF)
  528. /* Match table for of_platform binding */
  529. static const struct of_device_id tegra_i2c_of_match[] __devinitconst = {
  530. { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
  531. { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
  532. { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
  533. {},
  534. };
  535. MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
  536. #endif
  537. static int __devinit tegra_i2c_probe(struct platform_device *pdev)
  538. {
  539. struct tegra_i2c_dev *i2c_dev;
  540. struct tegra_i2c_platform_data *pdata = pdev->dev.platform_data;
  541. struct resource *res;
  542. struct clk *div_clk;
  543. struct clk *fast_clk;
  544. const unsigned int *prop;
  545. void __iomem *base;
  546. int irq;
  547. int ret = 0;
  548. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  549. if (!res) {
  550. dev_err(&pdev->dev, "no mem resource\n");
  551. return -EINVAL;
  552. }
  553. base = devm_request_and_ioremap(&pdev->dev, res);
  554. if (!base) {
  555. dev_err(&pdev->dev, "Cannot request/ioremap I2C registers\n");
  556. return -EADDRNOTAVAIL;
  557. }
  558. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  559. if (!res) {
  560. dev_err(&pdev->dev, "no irq resource\n");
  561. return -EINVAL;
  562. }
  563. irq = res->start;
  564. div_clk = devm_clk_get(&pdev->dev, "div-clk");
  565. if (IS_ERR(div_clk)) {
  566. dev_err(&pdev->dev, "missing controller clock");
  567. return PTR_ERR(div_clk);
  568. }
  569. fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
  570. if (IS_ERR(fast_clk)) {
  571. dev_err(&pdev->dev, "missing bus clock");
  572. return PTR_ERR(fast_clk);
  573. }
  574. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  575. if (!i2c_dev) {
  576. dev_err(&pdev->dev, "Could not allocate struct tegra_i2c_dev");
  577. return -ENOMEM;
  578. }
  579. i2c_dev->base = base;
  580. i2c_dev->div_clk = div_clk;
  581. i2c_dev->fast_clk = fast_clk;
  582. i2c_dev->adapter.algo = &tegra_i2c_algo;
  583. i2c_dev->irq = irq;
  584. i2c_dev->cont_id = pdev->id;
  585. i2c_dev->dev = &pdev->dev;
  586. i2c_dev->bus_clk_rate = 100000; /* default clock rate */
  587. if (pdata) {
  588. i2c_dev->bus_clk_rate = pdata->bus_clk_rate;
  589. } else if (i2c_dev->dev->of_node) { /* if there is a device tree node ... */
  590. prop = of_get_property(i2c_dev->dev->of_node,
  591. "clock-frequency", NULL);
  592. if (prop)
  593. i2c_dev->bus_clk_rate = be32_to_cpup(prop);
  594. }
  595. i2c_dev->hw = &tegra20_i2c_hw;
  596. if (pdev->dev.of_node) {
  597. const struct of_device_id *match;
  598. match = of_match_device(of_match_ptr(tegra_i2c_of_match),
  599. &pdev->dev);
  600. i2c_dev->hw = match->data;
  601. i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
  602. "nvidia,tegra20-i2c-dvc");
  603. } else if (pdev->id == 3) {
  604. i2c_dev->is_dvc = 1;
  605. }
  606. init_completion(&i2c_dev->msg_complete);
  607. platform_set_drvdata(pdev, i2c_dev);
  608. ret = tegra_i2c_init(i2c_dev);
  609. if (ret) {
  610. dev_err(&pdev->dev, "Failed to initialize i2c controller");
  611. return ret;
  612. }
  613. ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
  614. tegra_i2c_isr, 0, pdev->name, i2c_dev);
  615. if (ret) {
  616. dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
  617. return ret;
  618. }
  619. clk_prepare_enable(i2c_dev->fast_clk);
  620. i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
  621. i2c_dev->adapter.owner = THIS_MODULE;
  622. i2c_dev->adapter.class = I2C_CLASS_HWMON;
  623. strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
  624. sizeof(i2c_dev->adapter.name));
  625. i2c_dev->adapter.algo = &tegra_i2c_algo;
  626. i2c_dev->adapter.dev.parent = &pdev->dev;
  627. i2c_dev->adapter.nr = pdev->id;
  628. i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
  629. ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
  630. if (ret) {
  631. dev_err(&pdev->dev, "Failed to add I2C adapter\n");
  632. clk_disable_unprepare(i2c_dev->fast_clk);
  633. return ret;
  634. }
  635. of_i2c_register_devices(&i2c_dev->adapter);
  636. return 0;
  637. }
  638. static int __devexit tegra_i2c_remove(struct platform_device *pdev)
  639. {
  640. struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  641. i2c_del_adapter(&i2c_dev->adapter);
  642. return 0;
  643. }
  644. #ifdef CONFIG_PM_SLEEP
  645. static int tegra_i2c_suspend(struct device *dev)
  646. {
  647. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  648. i2c_lock_adapter(&i2c_dev->adapter);
  649. i2c_dev->is_suspended = true;
  650. i2c_unlock_adapter(&i2c_dev->adapter);
  651. return 0;
  652. }
  653. static int tegra_i2c_resume(struct device *dev)
  654. {
  655. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  656. int ret;
  657. i2c_lock_adapter(&i2c_dev->adapter);
  658. ret = tegra_i2c_init(i2c_dev);
  659. if (ret) {
  660. i2c_unlock_adapter(&i2c_dev->adapter);
  661. return ret;
  662. }
  663. i2c_dev->is_suspended = false;
  664. i2c_unlock_adapter(&i2c_dev->adapter);
  665. return 0;
  666. }
  667. static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume);
  668. #define TEGRA_I2C_PM (&tegra_i2c_pm)
  669. #else
  670. #define TEGRA_I2C_PM NULL
  671. #endif
  672. static struct platform_driver tegra_i2c_driver = {
  673. .probe = tegra_i2c_probe,
  674. .remove = __devexit_p(tegra_i2c_remove),
  675. .driver = {
  676. .name = "tegra-i2c",
  677. .owner = THIS_MODULE,
  678. .of_match_table = of_match_ptr(tegra_i2c_of_match),
  679. .pm = TEGRA_I2C_PM,
  680. },
  681. };
  682. static int __init tegra_i2c_init_driver(void)
  683. {
  684. return platform_driver_register(&tegra_i2c_driver);
  685. }
  686. static void __exit tegra_i2c_exit_driver(void)
  687. {
  688. platform_driver_unregister(&tegra_i2c_driver);
  689. }
  690. subsys_initcall(tegra_i2c_init_driver);
  691. module_exit(tegra_i2c_exit_driver);
  692. MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
  693. MODULE_AUTHOR("Colin Cross");
  694. MODULE_LICENSE("GPL v2");