i2c-tegra.c 21 KB

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