i2c-tegra.c 29 KB

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