i2c-tegra.c 25 KB

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