i2c-tegra.c 31 KB

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