i2c-tegra.c 30 KB

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