i2c-tegra.c 25 KB

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