i2c-tegra.c 33 KB

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