i2c-tegra.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. i2c_dev->msg_buf = msg->buf;
  588. i2c_dev->msg_buf_remaining = msg->len;
  589. i2c_dev->msg_err = I2C_ERR_NONE;
  590. i2c_dev->msg_read = (msg->flags & I2C_M_RD);
  591. reinit_completion(&i2c_dev->msg_complete);
  592. spin_lock_irqsave(&i2c_dev->xfer_lock, flags);
  593. int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
  594. tegra_i2c_unmask_irq(i2c_dev, int_mask);
  595. packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
  596. PACKET_HEADER0_PROTOCOL_I2C |
  597. (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
  598. (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
  599. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  600. packet_header = msg->len - 1;
  601. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  602. packet_header = I2C_HEADER_IE_ENABLE;
  603. if (end_state == MSG_END_CONTINUE)
  604. packet_header |= I2C_HEADER_CONTINUE_XFER;
  605. else if (end_state == MSG_END_REPEAT_START)
  606. packet_header |= I2C_HEADER_REPEAT_START;
  607. if (msg->flags & I2C_M_TEN) {
  608. packet_header |= msg->addr;
  609. packet_header |= I2C_HEADER_10BIT_ADDR;
  610. } else {
  611. packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
  612. }
  613. if (msg->flags & I2C_M_IGNORE_NAK)
  614. packet_header |= I2C_HEADER_CONT_ON_NAK;
  615. if (msg->flags & I2C_M_RD)
  616. packet_header |= I2C_HEADER_READ;
  617. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  618. if (!(msg->flags & I2C_M_RD))
  619. tegra_i2c_fill_tx_fifo(i2c_dev);
  620. if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
  621. int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
  622. if (msg->flags & I2C_M_RD)
  623. int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
  624. else if (i2c_dev->msg_buf_remaining)
  625. int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
  626. tegra_i2c_unmask_irq(i2c_dev, int_mask);
  627. spin_unlock_irqrestore(&i2c_dev->xfer_lock, flags);
  628. dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
  629. i2c_readl(i2c_dev, I2C_INT_MASK));
  630. time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
  631. TEGRA_I2C_TIMEOUT);
  632. tegra_i2c_mask_irq(i2c_dev, int_mask);
  633. if (time_left == 0) {
  634. dev_err(i2c_dev->dev, "i2c transfer timed out\n");
  635. tegra_i2c_init(i2c_dev);
  636. return -ETIMEDOUT;
  637. }
  638. dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
  639. time_left, completion_done(&i2c_dev->msg_complete),
  640. i2c_dev->msg_err);
  641. if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
  642. return 0;
  643. tegra_i2c_init(i2c_dev);
  644. if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
  645. if (msg->flags & I2C_M_IGNORE_NAK)
  646. return 0;
  647. return -EREMOTEIO;
  648. }
  649. return -EIO;
  650. }
  651. static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  652. int num)
  653. {
  654. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  655. int i;
  656. int ret = 0;
  657. ret = pm_runtime_get_sync(i2c_dev->dev);
  658. if (ret < 0) {
  659. dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
  660. return ret;
  661. }
  662. for (i = 0; i < num; i++) {
  663. enum msg_end_type end_type = MSG_END_STOP;
  664. if (i < (num - 1)) {
  665. if (msgs[i + 1].flags & I2C_M_NOSTART)
  666. end_type = MSG_END_CONTINUE;
  667. else
  668. end_type = MSG_END_REPEAT_START;
  669. }
  670. ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
  671. if (ret)
  672. break;
  673. }
  674. pm_runtime_put(i2c_dev->dev);
  675. return ret ?: i;
  676. }
  677. static u32 tegra_i2c_func(struct i2c_adapter *adap)
  678. {
  679. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  680. u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  681. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  682. if (i2c_dev->hw->has_continue_xfer_support)
  683. ret |= I2C_FUNC_NOSTART;
  684. return ret;
  685. }
  686. static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
  687. {
  688. struct device_node *np = i2c_dev->dev->of_node;
  689. int ret;
  690. ret = of_property_read_u32(np, "clock-frequency",
  691. &i2c_dev->bus_clk_rate);
  692. if (ret)
  693. i2c_dev->bus_clk_rate = 100000; /* default clock rate */
  694. i2c_dev->is_multimaster_mode = of_property_read_bool(np,
  695. "multi-master");
  696. }
  697. static const struct i2c_algorithm tegra_i2c_algo = {
  698. .master_xfer = tegra_i2c_xfer,
  699. .functionality = tegra_i2c_func,
  700. };
  701. /* payload size is only 12 bit */
  702. static const struct i2c_adapter_quirks tegra_i2c_quirks = {
  703. .flags = I2C_AQ_NO_ZERO_LEN,
  704. .max_read_len = 4096,
  705. .max_write_len = 4096,
  706. };
  707. static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
  708. .has_continue_xfer_support = false,
  709. .has_per_pkt_xfer_complete_irq = false,
  710. .has_single_clk_source = false,
  711. .clk_divisor_hs_mode = 3,
  712. .clk_divisor_std_fast_mode = 0,
  713. .clk_divisor_fast_plus_mode = 0,
  714. .has_config_load_reg = false,
  715. .has_multi_master_mode = false,
  716. .has_slcg_override_reg = false,
  717. .has_mst_fifo = false,
  718. };
  719. static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
  720. .has_continue_xfer_support = true,
  721. .has_per_pkt_xfer_complete_irq = false,
  722. .has_single_clk_source = false,
  723. .clk_divisor_hs_mode = 3,
  724. .clk_divisor_std_fast_mode = 0,
  725. .clk_divisor_fast_plus_mode = 0,
  726. .has_config_load_reg = false,
  727. .has_multi_master_mode = false,
  728. .has_slcg_override_reg = false,
  729. .has_mst_fifo = false,
  730. };
  731. static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
  732. .has_continue_xfer_support = true,
  733. .has_per_pkt_xfer_complete_irq = true,
  734. .has_single_clk_source = true,
  735. .clk_divisor_hs_mode = 1,
  736. .clk_divisor_std_fast_mode = 0x19,
  737. .clk_divisor_fast_plus_mode = 0x10,
  738. .has_config_load_reg = false,
  739. .has_multi_master_mode = false,
  740. .has_slcg_override_reg = false,
  741. .has_mst_fifo = false,
  742. };
  743. static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
  744. .has_continue_xfer_support = true,
  745. .has_per_pkt_xfer_complete_irq = true,
  746. .has_single_clk_source = true,
  747. .clk_divisor_hs_mode = 1,
  748. .clk_divisor_std_fast_mode = 0x19,
  749. .clk_divisor_fast_plus_mode = 0x10,
  750. .has_config_load_reg = true,
  751. .has_multi_master_mode = false,
  752. .has_slcg_override_reg = true,
  753. .has_mst_fifo = false,
  754. };
  755. static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
  756. .has_continue_xfer_support = true,
  757. .has_per_pkt_xfer_complete_irq = true,
  758. .has_single_clk_source = true,
  759. .clk_divisor_hs_mode = 1,
  760. .clk_divisor_std_fast_mode = 0x19,
  761. .clk_divisor_fast_plus_mode = 0x10,
  762. .has_config_load_reg = true,
  763. .has_multi_master_mode = true,
  764. .has_slcg_override_reg = true,
  765. .has_mst_fifo = false,
  766. };
  767. static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
  768. .has_continue_xfer_support = true,
  769. .has_per_pkt_xfer_complete_irq = true,
  770. .has_single_clk_source = true,
  771. .clk_divisor_hs_mode = 1,
  772. .clk_divisor_std_fast_mode = 0x19,
  773. .clk_divisor_fast_plus_mode = 0x10,
  774. .has_config_load_reg = true,
  775. .has_multi_master_mode = true,
  776. .has_slcg_override_reg = true,
  777. .has_mst_fifo = true,
  778. };
  779. /* Match table for of_platform binding */
  780. static const struct of_device_id tegra_i2c_of_match[] = {
  781. { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
  782. { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
  783. { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
  784. { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
  785. { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
  786. { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
  787. { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
  788. {},
  789. };
  790. MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
  791. static int tegra_i2c_probe(struct platform_device *pdev)
  792. {
  793. struct tegra_i2c_dev *i2c_dev;
  794. struct resource *res;
  795. struct clk *div_clk;
  796. struct clk *fast_clk;
  797. void __iomem *base;
  798. int irq;
  799. int ret = 0;
  800. int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
  801. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  802. base = devm_ioremap_resource(&pdev->dev, res);
  803. if (IS_ERR(base))
  804. return PTR_ERR(base);
  805. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  806. if (!res) {
  807. dev_err(&pdev->dev, "no irq resource\n");
  808. return -EINVAL;
  809. }
  810. irq = res->start;
  811. div_clk = devm_clk_get(&pdev->dev, "div-clk");
  812. if (IS_ERR(div_clk)) {
  813. dev_err(&pdev->dev, "missing controller clock\n");
  814. return PTR_ERR(div_clk);
  815. }
  816. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  817. if (!i2c_dev)
  818. return -ENOMEM;
  819. i2c_dev->base = base;
  820. i2c_dev->div_clk = div_clk;
  821. i2c_dev->adapter.algo = &tegra_i2c_algo;
  822. i2c_dev->adapter.quirks = &tegra_i2c_quirks;
  823. i2c_dev->irq = irq;
  824. i2c_dev->cont_id = pdev->id;
  825. i2c_dev->dev = &pdev->dev;
  826. i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c");
  827. if (IS_ERR(i2c_dev->rst)) {
  828. dev_err(&pdev->dev, "missing controller reset\n");
  829. return PTR_ERR(i2c_dev->rst);
  830. }
  831. tegra_i2c_parse_dt(i2c_dev);
  832. i2c_dev->hw = of_device_get_match_data(&pdev->dev);
  833. i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
  834. "nvidia,tegra20-i2c-dvc");
  835. init_completion(&i2c_dev->msg_complete);
  836. spin_lock_init(&i2c_dev->xfer_lock);
  837. if (!i2c_dev->hw->has_single_clk_source) {
  838. fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
  839. if (IS_ERR(fast_clk)) {
  840. dev_err(&pdev->dev, "missing fast clock\n");
  841. return PTR_ERR(fast_clk);
  842. }
  843. i2c_dev->fast_clk = fast_clk;
  844. }
  845. platform_set_drvdata(pdev, i2c_dev);
  846. if (!i2c_dev->hw->has_single_clk_source) {
  847. ret = clk_prepare(i2c_dev->fast_clk);
  848. if (ret < 0) {
  849. dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
  850. return ret;
  851. }
  852. }
  853. i2c_dev->clk_divisor_non_hs_mode =
  854. i2c_dev->hw->clk_divisor_std_fast_mode;
  855. if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
  856. (i2c_dev->bus_clk_rate == 1000000))
  857. i2c_dev->clk_divisor_non_hs_mode =
  858. i2c_dev->hw->clk_divisor_fast_plus_mode;
  859. clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
  860. ret = clk_set_rate(i2c_dev->div_clk,
  861. i2c_dev->bus_clk_rate * clk_multiplier);
  862. if (ret) {
  863. dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
  864. goto unprepare_fast_clk;
  865. }
  866. ret = clk_prepare(i2c_dev->div_clk);
  867. if (ret < 0) {
  868. dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
  869. goto unprepare_fast_clk;
  870. }
  871. pm_runtime_enable(&pdev->dev);
  872. if (!pm_runtime_enabled(&pdev->dev)) {
  873. ret = tegra_i2c_runtime_resume(&pdev->dev);
  874. if (ret < 0) {
  875. dev_err(&pdev->dev, "runtime resume failed\n");
  876. goto unprepare_div_clk;
  877. }
  878. }
  879. if (i2c_dev->is_multimaster_mode) {
  880. ret = clk_enable(i2c_dev->div_clk);
  881. if (ret < 0) {
  882. dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
  883. ret);
  884. goto disable_rpm;
  885. }
  886. }
  887. ret = tegra_i2c_init(i2c_dev);
  888. if (ret) {
  889. dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
  890. goto disable_div_clk;
  891. }
  892. ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
  893. tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
  894. if (ret) {
  895. dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
  896. goto disable_div_clk;
  897. }
  898. i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
  899. i2c_dev->adapter.owner = THIS_MODULE;
  900. i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
  901. strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
  902. sizeof(i2c_dev->adapter.name));
  903. i2c_dev->adapter.dev.parent = &pdev->dev;
  904. i2c_dev->adapter.nr = pdev->id;
  905. i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
  906. ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
  907. if (ret)
  908. goto disable_div_clk;
  909. return 0;
  910. disable_div_clk:
  911. if (i2c_dev->is_multimaster_mode)
  912. clk_disable(i2c_dev->div_clk);
  913. disable_rpm:
  914. pm_runtime_disable(&pdev->dev);
  915. if (!pm_runtime_status_suspended(&pdev->dev))
  916. tegra_i2c_runtime_suspend(&pdev->dev);
  917. unprepare_div_clk:
  918. clk_unprepare(i2c_dev->div_clk);
  919. unprepare_fast_clk:
  920. if (!i2c_dev->hw->has_single_clk_source)
  921. clk_unprepare(i2c_dev->fast_clk);
  922. return ret;
  923. }
  924. static int tegra_i2c_remove(struct platform_device *pdev)
  925. {
  926. struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  927. i2c_del_adapter(&i2c_dev->adapter);
  928. if (i2c_dev->is_multimaster_mode)
  929. clk_disable(i2c_dev->div_clk);
  930. pm_runtime_disable(&pdev->dev);
  931. if (!pm_runtime_status_suspended(&pdev->dev))
  932. tegra_i2c_runtime_suspend(&pdev->dev);
  933. clk_unprepare(i2c_dev->div_clk);
  934. if (!i2c_dev->hw->has_single_clk_source)
  935. clk_unprepare(i2c_dev->fast_clk);
  936. return 0;
  937. }
  938. #ifdef CONFIG_PM_SLEEP
  939. static const struct dev_pm_ops tegra_i2c_pm = {
  940. SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
  941. NULL)
  942. };
  943. #define TEGRA_I2C_PM (&tegra_i2c_pm)
  944. #else
  945. #define TEGRA_I2C_PM NULL
  946. #endif
  947. static struct platform_driver tegra_i2c_driver = {
  948. .probe = tegra_i2c_probe,
  949. .remove = tegra_i2c_remove,
  950. .driver = {
  951. .name = "tegra-i2c",
  952. .of_match_table = tegra_i2c_of_match,
  953. .pm = TEGRA_I2C_PM,
  954. },
  955. };
  956. static int __init tegra_i2c_init_driver(void)
  957. {
  958. return platform_driver_register(&tegra_i2c_driver);
  959. }
  960. static void __exit tegra_i2c_exit_driver(void)
  961. {
  962. platform_driver_unregister(&tegra_i2c_driver);
  963. }
  964. subsys_initcall(tegra_i2c_init_driver);
  965. module_exit(tegra_i2c_exit_driver);
  966. MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
  967. MODULE_AUTHOR("Colin Cross");
  968. MODULE_LICENSE("GPL v2");