i2c-tegra.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/i2c/busses/i2c-tegra.c
  4. *
  5. * Copyright (C) 2010 Google, Inc.
  6. * Author: Colin Cross <ccross@android.com>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/irq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/ktime.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/reset.h>
  29. #define BYTES_PER_FIFO_WORD 4
  30. #define I2C_CNFG 0x000
  31. #define I2C_CNFG_DEBOUNCE_CNT GENMASK(14, 12)
  32. #define I2C_CNFG_PACKET_MODE_EN BIT(10)
  33. #define I2C_CNFG_NEW_MASTER_FSM BIT(11)
  34. #define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
  35. #define I2C_STATUS 0x01c
  36. #define I2C_SL_CNFG 0x020
  37. #define I2C_SL_CNFG_NACK BIT(1)
  38. #define I2C_SL_CNFG_NEWSL BIT(2)
  39. #define I2C_SL_ADDR1 0x02c
  40. #define I2C_SL_ADDR2 0x030
  41. #define I2C_TLOW_SEXT 0x034
  42. #define I2C_TX_FIFO 0x050
  43. #define I2C_RX_FIFO 0x054
  44. #define I2C_PACKET_TRANSFER_STATUS 0x058
  45. #define I2C_FIFO_CONTROL 0x05c
  46. #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
  47. #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
  48. #define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5)
  49. #define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2)
  50. #define I2C_FIFO_STATUS 0x060
  51. #define I2C_FIFO_STATUS_TX GENMASK(7, 4)
  52. #define I2C_FIFO_STATUS_RX GENMASK(3, 0)
  53. #define I2C_INT_MASK 0x064
  54. #define I2C_INT_STATUS 0x068
  55. #define I2C_INT_BUS_CLR_DONE BIT(11)
  56. #define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
  57. #define I2C_INT_NO_ACK BIT(3)
  58. #define I2C_INT_ARBITRATION_LOST BIT(2)
  59. #define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
  60. #define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
  61. #define I2C_CLK_DIVISOR 0x06c
  62. #define I2C_CLK_DIVISOR_STD_FAST_MODE GENMASK(31, 16)
  63. #define I2C_CLK_DIVISOR_HSMODE GENMASK(15, 0)
  64. #define DVC_CTRL_REG1 0x000
  65. #define DVC_CTRL_REG1_INTR_EN BIT(10)
  66. #define DVC_CTRL_REG3 0x008
  67. #define DVC_CTRL_REG3_SW_PROG BIT(26)
  68. #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
  69. #define DVC_STATUS 0x00c
  70. #define DVC_STATUS_I2C_DONE_INTR BIT(30)
  71. #define I2C_ERR_NONE 0x00
  72. #define I2C_ERR_NO_ACK BIT(0)
  73. #define I2C_ERR_ARBITRATION_LOST BIT(1)
  74. #define I2C_ERR_UNKNOWN_INTERRUPT BIT(2)
  75. #define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3)
  76. #define PACKET_HEADER0_HEADER_SIZE GENMASK(29, 28)
  77. #define PACKET_HEADER0_PACKET_ID GENMASK(23, 16)
  78. #define PACKET_HEADER0_CONT_ID GENMASK(15, 12)
  79. #define PACKET_HEADER0_PROTOCOL GENMASK(7, 4)
  80. #define PACKET_HEADER0_PROTOCOL_I2C 1
  81. #define I2C_HEADER_CONT_ON_NAK BIT(21)
  82. #define I2C_HEADER_READ BIT(19)
  83. #define I2C_HEADER_10BIT_ADDR BIT(18)
  84. #define I2C_HEADER_IE_ENABLE BIT(17)
  85. #define I2C_HEADER_REPEAT_START BIT(16)
  86. #define I2C_HEADER_CONTINUE_XFER BIT(15)
  87. #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
  88. #define I2C_BUS_CLEAR_CNFG 0x084
  89. #define I2C_BC_SCLK_THRESHOLD GENMASK(23, 16)
  90. #define I2C_BC_STOP_COND BIT(2)
  91. #define I2C_BC_TERMINATE BIT(1)
  92. #define I2C_BC_ENABLE BIT(0)
  93. #define I2C_BUS_CLEAR_STATUS 0x088
  94. #define I2C_BC_STATUS BIT(0)
  95. #define I2C_CONFIG_LOAD 0x08c
  96. #define I2C_MSTR_CONFIG_LOAD BIT(0)
  97. #define I2C_CLKEN_OVERRIDE 0x090
  98. #define I2C_MST_CORE_CLKEN_OVR BIT(0)
  99. #define I2C_INTERFACE_TIMING_0 0x094
  100. #define I2C_INTERFACE_TIMING_THIGH GENMASK(13, 8)
  101. #define I2C_INTERFACE_TIMING_TLOW GENMASK(5, 0)
  102. #define I2C_INTERFACE_TIMING_1 0x098
  103. #define I2C_INTERFACE_TIMING_TBUF GENMASK(29, 24)
  104. #define I2C_INTERFACE_TIMING_TSU_STO GENMASK(21, 16)
  105. #define I2C_INTERFACE_TIMING_THD_STA GENMASK(13, 8)
  106. #define I2C_INTERFACE_TIMING_TSU_STA GENMASK(5, 0)
  107. #define I2C_HS_INTERFACE_TIMING_0 0x09c
  108. #define I2C_HS_INTERFACE_TIMING_THIGH GENMASK(13, 8)
  109. #define I2C_HS_INTERFACE_TIMING_TLOW GENMASK(5, 0)
  110. #define I2C_HS_INTERFACE_TIMING_1 0x0a0
  111. #define I2C_HS_INTERFACE_TIMING_TSU_STO GENMASK(21, 16)
  112. #define I2C_HS_INTERFACE_TIMING_THD_STA GENMASK(13, 8)
  113. #define I2C_HS_INTERFACE_TIMING_TSU_STA GENMASK(5, 0)
  114. #define I2C_MST_FIFO_CONTROL 0x0b4
  115. #define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0)
  116. #define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1)
  117. #define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4)
  118. #define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16)
  119. #define I2C_MST_FIFO_STATUS 0x0b8
  120. #define I2C_MST_FIFO_STATUS_TX GENMASK(23, 16)
  121. #define I2C_MST_FIFO_STATUS_RX GENMASK(7, 0)
  122. /* configuration load timeout in microseconds */
  123. #define I2C_CONFIG_LOAD_TIMEOUT 1000000
  124. /* packet header size in bytes */
  125. #define I2C_PACKET_HEADER_SIZE 12
  126. /*
  127. * I2C Controller will use PIO mode for transfers up to 32 bytes in order to
  128. * avoid DMA overhead, otherwise external APB DMA controller will be used.
  129. * Note that the actual MAX PIO length is 20 bytes because 32 bytes include
  130. * I2C_PACKET_HEADER_SIZE.
  131. */
  132. #define I2C_PIO_MODE_PREFERRED_LEN 32
  133. /*
  134. * msg_end_type: The bus control which needs to be sent at end of transfer.
  135. * @MSG_END_STOP: Send stop pulse.
  136. * @MSG_END_REPEAT_START: Send repeat-start.
  137. * @MSG_END_CONTINUE: Don't send stop or repeat-start.
  138. */
  139. enum msg_end_type {
  140. MSG_END_STOP,
  141. MSG_END_REPEAT_START,
  142. MSG_END_CONTINUE,
  143. };
  144. /**
  145. * struct tegra_i2c_hw_feature : per hardware generation features
  146. * @has_continue_xfer_support: continue-transfer supported
  147. * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
  148. * completion interrupt on per packet basis.
  149. * @has_config_load_reg: Has the config load register to load the new
  150. * configuration.
  151. * @clk_divisor_hs_mode: Clock divisor in HS mode.
  152. * @clk_divisor_std_mode: Clock divisor in standard mode. It is
  153. * applicable if there is no fast clock source i.e. single clock
  154. * source.
  155. * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
  156. * applicable if there is no fast clock source i.e. single clock
  157. * source.
  158. * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
  159. * applicable if there is no fast clock source (i.e. single
  160. * clock source).
  161. * @has_multi_master_mode: The I2C controller supports running in single-master
  162. * or multi-master mode.
  163. * @has_slcg_override_reg: The I2C controller supports a register that
  164. * overrides the second level clock gating.
  165. * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
  166. * provides additional features and allows for longer messages to
  167. * be transferred in one go.
  168. * @quirks: I2C adapter quirks for limiting write/read transfer size and not
  169. * allowing 0 length transfers.
  170. * @supports_bus_clear: Bus Clear support to recover from bus hang during
  171. * SDA stuck low from device for some unknown reasons.
  172. * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
  173. * @tlow_std_mode: Low period of the clock in standard mode.
  174. * @thigh_std_mode: High period of the clock in standard mode.
  175. * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
  176. * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
  177. * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
  178. * in standard mode.
  179. * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
  180. * conditions in fast/fast-plus modes.
  181. * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
  182. * in HS mode.
  183. * @has_interface_timing_reg: Has interface timing register to program the tuned
  184. * timing settings.
  185. */
  186. struct tegra_i2c_hw_feature {
  187. bool has_continue_xfer_support;
  188. bool has_per_pkt_xfer_complete_irq;
  189. bool has_config_load_reg;
  190. u32 clk_divisor_hs_mode;
  191. u32 clk_divisor_std_mode;
  192. u32 clk_divisor_fast_mode;
  193. u32 clk_divisor_fast_plus_mode;
  194. bool has_multi_master_mode;
  195. bool has_slcg_override_reg;
  196. bool has_mst_fifo;
  197. const struct i2c_adapter_quirks *quirks;
  198. bool supports_bus_clear;
  199. bool has_apb_dma;
  200. u32 tlow_std_mode;
  201. u32 thigh_std_mode;
  202. u32 tlow_fast_fastplus_mode;
  203. u32 thigh_fast_fastplus_mode;
  204. u32 setup_hold_time_std_mode;
  205. u32 setup_hold_time_fast_fast_plus_mode;
  206. u32 setup_hold_time_hs_mode;
  207. bool has_interface_timing_reg;
  208. };
  209. /**
  210. * struct tegra_i2c_dev - per device I2C context
  211. * @dev: device reference for power management
  212. * @hw: Tegra I2C HW feature
  213. * @adapter: core I2C layer adapter information
  214. * @div_clk: clock reference for div clock of I2C controller
  215. * @clocks: array of I2C controller clocks
  216. * @nclocks: number of clocks in the array
  217. * @rst: reset control for the I2C controller
  218. * @base: ioremapped registers cookie
  219. * @base_phys: physical base address of the I2C controller
  220. * @cont_id: I2C controller ID, used for packet header
  221. * @irq: IRQ number of transfer complete interrupt
  222. * @is_dvc: identifies the DVC I2C controller, has a different register layout
  223. * @is_vi: identifies the VI I2C controller, has a different register layout
  224. * @msg_complete: transfer completion notifier
  225. * @msg_buf_remaining: size of unsent data in the message buffer
  226. * @msg_len: length of message in current transfer
  227. * @msg_err: error code for completed message
  228. * @msg_buf: pointer to current message data
  229. * @msg_read: indicates that the transfer is a read access
  230. * @timings: i2c timings information like bus frequency
  231. * @multimaster_mode: indicates that I2C controller is in multi-master mode
  232. * @dma_chan: DMA channel
  233. * @dma_phys: handle to DMA resources
  234. * @dma_buf: pointer to allocated DMA buffer
  235. * @dma_buf_size: DMA buffer size
  236. * @dma_mode: indicates active DMA transfer
  237. * @dma_complete: DMA completion notifier
  238. * @atomic_mode: indicates active atomic transfer
  239. */
  240. struct tegra_i2c_dev {
  241. struct device *dev;
  242. struct i2c_adapter adapter;
  243. const struct tegra_i2c_hw_feature *hw;
  244. struct reset_control *rst;
  245. unsigned int cont_id;
  246. unsigned int irq;
  247. phys_addr_t base_phys;
  248. void __iomem *base;
  249. struct clk_bulk_data clocks[2];
  250. unsigned int nclocks;
  251. struct clk *div_clk;
  252. struct i2c_timings timings;
  253. struct completion msg_complete;
  254. size_t msg_buf_remaining;
  255. unsigned int msg_len;
  256. int msg_err;
  257. u8 *msg_buf;
  258. struct completion dma_complete;
  259. struct dma_chan *dma_chan;
  260. unsigned int dma_buf_size;
  261. struct device *dma_dev;
  262. dma_addr_t dma_phys;
  263. void *dma_buf;
  264. bool multimaster_mode;
  265. bool atomic_mode;
  266. bool dma_mode;
  267. bool msg_read;
  268. bool is_dvc;
  269. bool is_vi;
  270. };
  271. #define IS_DVC(dev) (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) && (dev)->is_dvc)
  272. #define IS_VI(dev) (IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) && (dev)->is_vi)
  273. static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
  274. unsigned int reg)
  275. {
  276. writel_relaxed(val, i2c_dev->base + reg);
  277. }
  278. static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
  279. {
  280. return readl_relaxed(i2c_dev->base + reg);
  281. }
  282. /*
  283. * If necessary, i2c_writel() and i2c_readl() will offset the register
  284. * in order to talk to the I2C block inside the DVC block.
  285. */
  286. static u32 tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
  287. {
  288. if (IS_DVC(i2c_dev))
  289. reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
  290. else if (IS_VI(i2c_dev))
  291. reg = 0xc00 + (reg << 2);
  292. return reg;
  293. }
  294. static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned int reg)
  295. {
  296. writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  297. /* read back register to make sure that register writes completed */
  298. if (reg != I2C_TX_FIFO)
  299. readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  300. else if (IS_VI(i2c_dev))
  301. readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, I2C_INT_STATUS));
  302. }
  303. static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
  304. {
  305. return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
  306. }
  307. static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
  308. unsigned int reg, unsigned int len)
  309. {
  310. writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
  311. }
  312. static void i2c_writesl_vi(struct tegra_i2c_dev *i2c_dev, void *data,
  313. unsigned int reg, unsigned int len)
  314. {
  315. u32 *data32 = data;
  316. /*
  317. * VI I2C controller has known hardware bug where writes get stuck
  318. * when immediate multiple writes happen to TX_FIFO register.
  319. * Recommended software work around is to read I2C register after
  320. * each write to TX_FIFO register to flush out the data.
  321. */
  322. while (len--)
  323. i2c_writel(i2c_dev, *data32++, reg);
  324. }
  325. static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
  326. unsigned int reg, unsigned int len)
  327. {
  328. readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
  329. }
  330. static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
  331. {
  332. u32 int_mask;
  333. int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
  334. i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
  335. }
  336. static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
  337. {
  338. u32 int_mask;
  339. int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
  340. i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
  341. }
  342. static void tegra_i2c_dma_complete(void *args)
  343. {
  344. struct tegra_i2c_dev *i2c_dev = args;
  345. complete(&i2c_dev->dma_complete);
  346. }
  347. static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len)
  348. {
  349. struct dma_async_tx_descriptor *dma_desc;
  350. enum dma_transfer_direction dir;
  351. dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len);
  352. reinit_completion(&i2c_dev->dma_complete);
  353. dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  354. dma_desc = dmaengine_prep_slave_single(i2c_dev->dma_chan, i2c_dev->dma_phys,
  355. len, dir, DMA_PREP_INTERRUPT |
  356. DMA_CTRL_ACK);
  357. if (!dma_desc) {
  358. dev_err(i2c_dev->dev, "failed to get %s DMA descriptor\n",
  359. i2c_dev->msg_read ? "RX" : "TX");
  360. return -EINVAL;
  361. }
  362. dma_desc->callback = tegra_i2c_dma_complete;
  363. dma_desc->callback_param = i2c_dev;
  364. dmaengine_submit(dma_desc);
  365. dma_async_issue_pending(i2c_dev->dma_chan);
  366. return 0;
  367. }
  368. static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev)
  369. {
  370. if (i2c_dev->dma_buf) {
  371. dma_free_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size,
  372. i2c_dev->dma_buf, i2c_dev->dma_phys);
  373. i2c_dev->dma_buf = NULL;
  374. }
  375. if (i2c_dev->dma_chan) {
  376. dma_release_channel(i2c_dev->dma_chan);
  377. i2c_dev->dma_chan = NULL;
  378. }
  379. }
  380. static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
  381. {
  382. dma_addr_t dma_phys;
  383. u32 *dma_buf;
  384. int err;
  385. if (IS_VI(i2c_dev))
  386. return 0;
  387. if (i2c_dev->hw->has_apb_dma) {
  388. if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
  389. dev_dbg(i2c_dev->dev, "APB DMA support not enabled\n");
  390. return 0;
  391. }
  392. } else if (!IS_ENABLED(CONFIG_TEGRA186_GPC_DMA)) {
  393. dev_dbg(i2c_dev->dev, "GPC DMA support not enabled\n");
  394. return 0;
  395. }
  396. /*
  397. * The same channel will be used for both RX and TX.
  398. * Keeping the name as "tx" for backward compatibility
  399. * with existing devicetrees.
  400. */
  401. i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx");
  402. if (IS_ERR(i2c_dev->dma_chan)) {
  403. err = PTR_ERR(i2c_dev->dma_chan);
  404. i2c_dev->dma_chan = NULL;
  405. goto err_out;
  406. }
  407. i2c_dev->dma_dev = i2c_dev->dma_chan->device->dev;
  408. i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len +
  409. I2C_PACKET_HEADER_SIZE;
  410. dma_buf = dma_alloc_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size,
  411. &dma_phys, GFP_KERNEL | __GFP_NOWARN);
  412. if (!dma_buf) {
  413. dev_err(i2c_dev->dev, "failed to allocate DMA buffer\n");
  414. err = -ENOMEM;
  415. goto err_out;
  416. }
  417. i2c_dev->dma_buf = dma_buf;
  418. i2c_dev->dma_phys = dma_phys;
  419. return 0;
  420. err_out:
  421. tegra_i2c_release_dma(i2c_dev);
  422. if (err != -EPROBE_DEFER) {
  423. dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
  424. dev_err(i2c_dev->dev, "falling back to PIO\n");
  425. return 0;
  426. }
  427. return err;
  428. }
  429. /*
  430. * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
  431. * block. This block is identical to the rest of the I2C blocks, except that
  432. * it only supports master mode, it has registers moved around, and it needs
  433. * some extra init to get it into I2C mode. The register moves are handled
  434. * by i2c_readl() and i2c_writel().
  435. */
  436. static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
  437. {
  438. u32 val;
  439. val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
  440. val |= DVC_CTRL_REG3_SW_PROG;
  441. val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
  442. dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
  443. val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
  444. val |= DVC_CTRL_REG1_INTR_EN;
  445. dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
  446. }
  447. static void tegra_i2c_vi_init(struct tegra_i2c_dev *i2c_dev)
  448. {
  449. u32 value;
  450. value = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, 2) |
  451. FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, 4);
  452. i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_0);
  453. value = FIELD_PREP(I2C_INTERFACE_TIMING_TBUF, 4) |
  454. FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STO, 7) |
  455. FIELD_PREP(I2C_INTERFACE_TIMING_THD_STA, 4) |
  456. FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STA, 4);
  457. i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_1);
  458. value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, 3) |
  459. FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, 8);
  460. i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_0);
  461. value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STO, 11) |
  462. FIELD_PREP(I2C_HS_INTERFACE_TIMING_THD_STA, 11) |
  463. FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STA, 11);
  464. i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_1);
  465. value = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND;
  466. i2c_writel(i2c_dev, value, I2C_BUS_CLEAR_CNFG);
  467. i2c_writel(i2c_dev, 0x0, I2C_TLOW_SEXT);
  468. }
  469. static int tegra_i2c_poll_register(struct tegra_i2c_dev *i2c_dev,
  470. u32 reg, u32 mask, u32 delay_us,
  471. u32 timeout_us)
  472. {
  473. void __iomem *addr = i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg);
  474. u32 val;
  475. if (!i2c_dev->atomic_mode)
  476. return readl_relaxed_poll_timeout(addr, val, !(val & mask),
  477. delay_us, timeout_us);
  478. return readl_relaxed_poll_timeout_atomic(addr, val, !(val & mask),
  479. delay_us, timeout_us);
  480. }
  481. static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
  482. {
  483. u32 mask, val, offset;
  484. int err;
  485. if (i2c_dev->hw->has_mst_fifo) {
  486. mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
  487. I2C_MST_FIFO_CONTROL_RX_FLUSH;
  488. offset = I2C_MST_FIFO_CONTROL;
  489. } else {
  490. mask = I2C_FIFO_CONTROL_TX_FLUSH |
  491. I2C_FIFO_CONTROL_RX_FLUSH;
  492. offset = I2C_FIFO_CONTROL;
  493. }
  494. val = i2c_readl(i2c_dev, offset);
  495. val |= mask;
  496. i2c_writel(i2c_dev, val, offset);
  497. err = tegra_i2c_poll_register(i2c_dev, offset, mask, 1000, 1000000);
  498. if (err) {
  499. dev_err(i2c_dev->dev, "failed to flush FIFO\n");
  500. return err;
  501. }
  502. return 0;
  503. }
  504. static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
  505. {
  506. int err;
  507. if (!i2c_dev->hw->has_config_load_reg)
  508. return 0;
  509. i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
  510. err = tegra_i2c_poll_register(i2c_dev, I2C_CONFIG_LOAD, 0xffffffff,
  511. 1000, I2C_CONFIG_LOAD_TIMEOUT);
  512. if (err) {
  513. dev_err(i2c_dev->dev, "failed to load config\n");
  514. return err;
  515. }
  516. return 0;
  517. }
  518. static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
  519. {
  520. u32 val, clk_divisor, clk_multiplier, tsu_thd, tlow, thigh, non_hs_mode;
  521. struct i2c_timings *t = &i2c_dev->timings;
  522. int err;
  523. /*
  524. * The reset shouldn't ever fail in practice. The failure will be a
  525. * sign of a severe problem that needs to be resolved. Still we don't
  526. * want to fail the initialization completely because this may break
  527. * kernel boot up since voltage regulators use I2C. Hence, we will
  528. * emit a noisy warning on error, which won't stay unnoticed and
  529. * won't hose machine entirely.
  530. */
  531. err = device_reset(i2c_dev->dev);
  532. WARN_ON_ONCE(err);
  533. if (IS_DVC(i2c_dev))
  534. tegra_dvc_init(i2c_dev);
  535. val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
  536. FIELD_PREP(I2C_CNFG_DEBOUNCE_CNT, 2);
  537. if (i2c_dev->hw->has_multi_master_mode)
  538. val |= I2C_CNFG_MULTI_MASTER_MODE;
  539. i2c_writel(i2c_dev, val, I2C_CNFG);
  540. i2c_writel(i2c_dev, 0, I2C_INT_MASK);
  541. if (IS_VI(i2c_dev))
  542. tegra_i2c_vi_init(i2c_dev);
  543. switch (t->bus_freq_hz) {
  544. case I2C_MAX_STANDARD_MODE_FREQ + 1 ... I2C_MAX_FAST_MODE_PLUS_FREQ:
  545. default:
  546. tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
  547. thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
  548. tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
  549. if (t->bus_freq_hz > I2C_MAX_FAST_MODE_FREQ)
  550. non_hs_mode = i2c_dev->hw->clk_divisor_fast_plus_mode;
  551. else
  552. non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode;
  553. break;
  554. case 0 ... I2C_MAX_STANDARD_MODE_FREQ:
  555. tlow = i2c_dev->hw->tlow_std_mode;
  556. thigh = i2c_dev->hw->thigh_std_mode;
  557. tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
  558. non_hs_mode = i2c_dev->hw->clk_divisor_std_mode;
  559. break;
  560. }
  561. /* make sure clock divisor programmed correctly */
  562. clk_divisor = FIELD_PREP(I2C_CLK_DIVISOR_HSMODE,
  563. i2c_dev->hw->clk_divisor_hs_mode) |
  564. FIELD_PREP(I2C_CLK_DIVISOR_STD_FAST_MODE, non_hs_mode);
  565. i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
  566. if (i2c_dev->hw->has_interface_timing_reg) {
  567. val = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, thigh) |
  568. FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, tlow);
  569. i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
  570. }
  571. /*
  572. * Configure setup and hold times only when tsu_thd is non-zero.
  573. * Otherwise, preserve the chip default values.
  574. */
  575. if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
  576. i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
  577. clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
  578. err = clk_set_rate(i2c_dev->div_clk,
  579. t->bus_freq_hz * clk_multiplier);
  580. if (err) {
  581. dev_err(i2c_dev->dev, "failed to set div-clk rate: %d\n", err);
  582. return err;
  583. }
  584. if (!IS_DVC(i2c_dev) && !IS_VI(i2c_dev)) {
  585. u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
  586. sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
  587. i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
  588. i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
  589. i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
  590. }
  591. err = tegra_i2c_flush_fifos(i2c_dev);
  592. if (err)
  593. return err;
  594. if (i2c_dev->multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
  595. i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
  596. err = tegra_i2c_wait_for_config_load(i2c_dev);
  597. if (err)
  598. return err;
  599. return 0;
  600. }
  601. static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
  602. {
  603. u32 cnfg;
  604. /*
  605. * NACK interrupt is generated before the I2C controller generates
  606. * the STOP condition on the bus. So, wait for 2 clock periods
  607. * before disabling the controller so that the STOP condition has
  608. * been delivered properly.
  609. */
  610. udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->timings.bus_freq_hz));
  611. cnfg = i2c_readl(i2c_dev, I2C_CNFG);
  612. if (cnfg & I2C_CNFG_PACKET_MODE_EN)
  613. i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
  614. return tegra_i2c_wait_for_config_load(i2c_dev);
  615. }
  616. static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
  617. {
  618. size_t buf_remaining = i2c_dev->msg_buf_remaining;
  619. unsigned int words_to_transfer, rx_fifo_avail;
  620. u8 *buf = i2c_dev->msg_buf;
  621. u32 val;
  622. /*
  623. * Catch overflow due to message fully sent before the check for
  624. * RX FIFO availability.
  625. */
  626. if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining)))
  627. return -EINVAL;
  628. if (i2c_dev->hw->has_mst_fifo) {
  629. val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
  630. rx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_RX, val);
  631. } else {
  632. val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
  633. rx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_RX, val);
  634. }
  635. /* round down to exclude partial word at the end of buffer */
  636. words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
  637. if (words_to_transfer > rx_fifo_avail)
  638. words_to_transfer = rx_fifo_avail;
  639. i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
  640. buf += words_to_transfer * BYTES_PER_FIFO_WORD;
  641. buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
  642. rx_fifo_avail -= words_to_transfer;
  643. /*
  644. * If there is a partial word at the end of buffer, handle it
  645. * manually to prevent overwriting past the end of buffer.
  646. */
  647. if (rx_fifo_avail > 0 && buf_remaining > 0) {
  648. /*
  649. * buf_remaining > 3 check not needed as rx_fifo_avail == 0
  650. * when (words_to_transfer was > rx_fifo_avail) earlier
  651. * in this function.
  652. */
  653. val = i2c_readl(i2c_dev, I2C_RX_FIFO);
  654. val = cpu_to_le32(val);
  655. memcpy(buf, &val, buf_remaining);
  656. buf_remaining = 0;
  657. rx_fifo_avail--;
  658. }
  659. /* RX FIFO must be drained, otherwise it's an Overflow case. */
  660. if (WARN_ON_ONCE(rx_fifo_avail))
  661. return -EINVAL;
  662. i2c_dev->msg_buf_remaining = buf_remaining;
  663. i2c_dev->msg_buf = buf;
  664. return 0;
  665. }
  666. static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
  667. {
  668. size_t buf_remaining = i2c_dev->msg_buf_remaining;
  669. unsigned int words_to_transfer, tx_fifo_avail;
  670. u8 *buf = i2c_dev->msg_buf;
  671. u32 val;
  672. if (i2c_dev->hw->has_mst_fifo) {
  673. val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
  674. tx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_TX, val);
  675. } else {
  676. val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
  677. tx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_TX, val);
  678. }
  679. /* round down to exclude partial word at the end of buffer */
  680. words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
  681. /*
  682. * This hunk pushes 4 bytes at a time into the TX FIFO.
  683. *
  684. * It's very common to have < 4 bytes, hence there is no word
  685. * to push if we have less than 4 bytes to transfer.
  686. */
  687. if (words_to_transfer) {
  688. if (words_to_transfer > tx_fifo_avail)
  689. words_to_transfer = tx_fifo_avail;
  690. /*
  691. * Update state before writing to FIFO. Note that this may
  692. * cause us to finish writing all bytes (AKA buf_remaining
  693. * goes to 0), hence we have a potential for an interrupt
  694. * (PACKET_XFER_COMPLETE is not maskable), but GIC interrupt
  695. * is disabled at this point.
  696. */
  697. buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
  698. tx_fifo_avail -= words_to_transfer;
  699. i2c_dev->msg_buf_remaining = buf_remaining;
  700. i2c_dev->msg_buf = buf + words_to_transfer * BYTES_PER_FIFO_WORD;
  701. if (IS_VI(i2c_dev))
  702. i2c_writesl_vi(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
  703. else
  704. i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
  705. buf += words_to_transfer * BYTES_PER_FIFO_WORD;
  706. }
  707. /*
  708. * If there is a partial word at the end of buffer, handle it manually
  709. * to prevent reading past the end of buffer, which could cross a page
  710. * boundary and fault.
  711. */
  712. if (tx_fifo_avail > 0 && buf_remaining > 0) {
  713. /*
  714. * buf_remaining > 3 check not needed as tx_fifo_avail == 0
  715. * when (words_to_transfer was > tx_fifo_avail) earlier
  716. * in this function for non-zero words_to_transfer.
  717. */
  718. memcpy(&val, buf, buf_remaining);
  719. val = le32_to_cpu(val);
  720. i2c_dev->msg_buf_remaining = 0;
  721. i2c_dev->msg_buf = NULL;
  722. i2c_writel(i2c_dev, val, I2C_TX_FIFO);
  723. }
  724. return 0;
  725. }
  726. static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
  727. {
  728. const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
  729. struct tegra_i2c_dev *i2c_dev = dev_id;
  730. u32 status;
  731. status = i2c_readl(i2c_dev, I2C_INT_STATUS);
  732. if (status == 0) {
  733. dev_warn(i2c_dev->dev, "IRQ status 0 %08x %08x %08x\n",
  734. i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
  735. i2c_readl(i2c_dev, I2C_STATUS),
  736. i2c_readl(i2c_dev, I2C_CNFG));
  737. i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
  738. goto err;
  739. }
  740. if (status & status_err) {
  741. tegra_i2c_disable_packet_mode(i2c_dev);
  742. if (status & I2C_INT_NO_ACK)
  743. i2c_dev->msg_err |= I2C_ERR_NO_ACK;
  744. if (status & I2C_INT_ARBITRATION_LOST)
  745. i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
  746. goto err;
  747. }
  748. /*
  749. * I2C transfer is terminated during the bus clear, so skip
  750. * processing the other interrupts.
  751. */
  752. if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
  753. goto err;
  754. if (!i2c_dev->dma_mode) {
  755. if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
  756. if (tegra_i2c_empty_rx_fifo(i2c_dev)) {
  757. /*
  758. * Overflow error condition: message fully sent,
  759. * with no XFER_COMPLETE interrupt but hardware
  760. * asks to transfer more.
  761. */
  762. i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW;
  763. goto err;
  764. }
  765. }
  766. if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
  767. if (i2c_dev->msg_buf_remaining)
  768. tegra_i2c_fill_tx_fifo(i2c_dev);
  769. else
  770. tegra_i2c_mask_irq(i2c_dev,
  771. I2C_INT_TX_FIFO_DATA_REQ);
  772. }
  773. }
  774. i2c_writel(i2c_dev, status, I2C_INT_STATUS);
  775. if (IS_DVC(i2c_dev))
  776. dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
  777. /*
  778. * During message read XFER_COMPLETE interrupt is triggered prior to
  779. * DMA completion and during message write XFER_COMPLETE interrupt is
  780. * triggered after DMA completion.
  781. *
  782. * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer,
  783. * so forcing msg_buf_remaining to 0 in DMA mode.
  784. */
  785. if (status & I2C_INT_PACKET_XFER_COMPLETE) {
  786. if (i2c_dev->dma_mode)
  787. i2c_dev->msg_buf_remaining = 0;
  788. /*
  789. * Underflow error condition: XFER_COMPLETE before message
  790. * fully sent.
  791. */
  792. if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
  793. i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
  794. goto err;
  795. }
  796. complete(&i2c_dev->msg_complete);
  797. }
  798. goto done;
  799. err:
  800. /* mask all interrupts on error */
  801. tegra_i2c_mask_irq(i2c_dev,
  802. I2C_INT_NO_ACK |
  803. I2C_INT_ARBITRATION_LOST |
  804. I2C_INT_PACKET_XFER_COMPLETE |
  805. I2C_INT_TX_FIFO_DATA_REQ |
  806. I2C_INT_RX_FIFO_DATA_REQ);
  807. if (i2c_dev->hw->supports_bus_clear)
  808. tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
  809. i2c_writel(i2c_dev, status, I2C_INT_STATUS);
  810. if (IS_DVC(i2c_dev))
  811. dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
  812. if (i2c_dev->dma_mode) {
  813. dmaengine_terminate_async(i2c_dev->dma_chan);
  814. complete(&i2c_dev->dma_complete);
  815. }
  816. complete(&i2c_dev->msg_complete);
  817. done:
  818. return IRQ_HANDLED;
  819. }
  820. static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
  821. size_t len)
  822. {
  823. struct dma_slave_config slv_config = {0};
  824. u32 val, reg, dma_burst, reg_offset;
  825. int err;
  826. if (i2c_dev->hw->has_mst_fifo)
  827. reg = I2C_MST_FIFO_CONTROL;
  828. else
  829. reg = I2C_FIFO_CONTROL;
  830. if (i2c_dev->dma_mode) {
  831. if (len & 0xF)
  832. dma_burst = 1;
  833. else if (len & 0x10)
  834. dma_burst = 4;
  835. else
  836. dma_burst = 8;
  837. if (i2c_dev->msg_read) {
  838. reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
  839. slv_config.src_addr = i2c_dev->base_phys + reg_offset;
  840. slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  841. slv_config.src_maxburst = dma_burst;
  842. if (i2c_dev->hw->has_mst_fifo)
  843. val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
  844. else
  845. val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
  846. } else {
  847. reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
  848. slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
  849. slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  850. slv_config.dst_maxburst = dma_burst;
  851. if (i2c_dev->hw->has_mst_fifo)
  852. val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
  853. else
  854. val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
  855. }
  856. slv_config.device_fc = true;
  857. err = dmaengine_slave_config(i2c_dev->dma_chan, &slv_config);
  858. if (err) {
  859. dev_err(i2c_dev->dev, "DMA config failed: %d\n", err);
  860. dev_err(i2c_dev->dev, "falling back to PIO\n");
  861. tegra_i2c_release_dma(i2c_dev);
  862. i2c_dev->dma_mode = false;
  863. } else {
  864. goto out;
  865. }
  866. }
  867. if (i2c_dev->hw->has_mst_fifo)
  868. val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
  869. I2C_MST_FIFO_CONTROL_RX_TRIG(1);
  870. else
  871. val = I2C_FIFO_CONTROL_TX_TRIG(8) |
  872. I2C_FIFO_CONTROL_RX_TRIG(1);
  873. out:
  874. i2c_writel(i2c_dev, val, reg);
  875. }
  876. static unsigned long tegra_i2c_poll_completion(struct tegra_i2c_dev *i2c_dev,
  877. struct completion *complete,
  878. unsigned int timeout_ms)
  879. {
  880. ktime_t ktime = ktime_get();
  881. ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms);
  882. do {
  883. u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
  884. if (status)
  885. tegra_i2c_isr(i2c_dev->irq, i2c_dev);
  886. if (completion_done(complete)) {
  887. s64 delta = ktime_ms_delta(ktimeout, ktime);
  888. return msecs_to_jiffies(delta) ?: 1;
  889. }
  890. ktime = ktime_get();
  891. } while (ktime_before(ktime, ktimeout));
  892. return 0;
  893. }
  894. static unsigned long tegra_i2c_wait_completion(struct tegra_i2c_dev *i2c_dev,
  895. struct completion *complete,
  896. unsigned int timeout_ms)
  897. {
  898. unsigned long ret;
  899. if (i2c_dev->atomic_mode) {
  900. ret = tegra_i2c_poll_completion(i2c_dev, complete, timeout_ms);
  901. } else {
  902. enable_irq(i2c_dev->irq);
  903. ret = wait_for_completion_timeout(complete,
  904. msecs_to_jiffies(timeout_ms));
  905. disable_irq(i2c_dev->irq);
  906. /*
  907. * Under some rare circumstances (like running KASAN +
  908. * NFS root) CPU, which handles interrupt, may stuck in
  909. * uninterruptible state for a significant time. In this
  910. * case we will get timeout if I2C transfer is running on
  911. * a sibling CPU, despite of IRQ being raised.
  912. *
  913. * In order to handle this rare condition, the IRQ status
  914. * needs to be checked after timeout.
  915. */
  916. if (ret == 0)
  917. ret = tegra_i2c_poll_completion(i2c_dev, complete, 0);
  918. }
  919. return ret;
  920. }
  921. static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
  922. {
  923. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  924. u32 val, time_left;
  925. int err;
  926. reinit_completion(&i2c_dev->msg_complete);
  927. val = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND |
  928. I2C_BC_TERMINATE;
  929. i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
  930. err = tegra_i2c_wait_for_config_load(i2c_dev);
  931. if (err)
  932. return err;
  933. val |= I2C_BC_ENABLE;
  934. i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
  935. tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
  936. time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 50);
  937. tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
  938. if (time_left == 0) {
  939. dev_err(i2c_dev->dev, "failed to clear bus\n");
  940. return -ETIMEDOUT;
  941. }
  942. val = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
  943. if (!(val & I2C_BC_STATUS)) {
  944. dev_err(i2c_dev->dev, "un-recovered arbitration lost\n");
  945. return -EIO;
  946. }
  947. return -EAGAIN;
  948. }
  949. static void tegra_i2c_push_packet_header(struct tegra_i2c_dev *i2c_dev,
  950. struct i2c_msg *msg,
  951. enum msg_end_type end_state)
  952. {
  953. u32 *dma_buf = i2c_dev->dma_buf;
  954. u32 packet_header;
  955. packet_header = FIELD_PREP(PACKET_HEADER0_HEADER_SIZE, 0) |
  956. FIELD_PREP(PACKET_HEADER0_PROTOCOL,
  957. PACKET_HEADER0_PROTOCOL_I2C) |
  958. FIELD_PREP(PACKET_HEADER0_CONT_ID, i2c_dev->cont_id) |
  959. FIELD_PREP(PACKET_HEADER0_PACKET_ID, 1);
  960. if (i2c_dev->dma_mode && !i2c_dev->msg_read)
  961. *dma_buf++ = packet_header;
  962. else
  963. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  964. packet_header = i2c_dev->msg_len - 1;
  965. if (i2c_dev->dma_mode && !i2c_dev->msg_read)
  966. *dma_buf++ = packet_header;
  967. else
  968. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  969. packet_header = I2C_HEADER_IE_ENABLE;
  970. if (end_state == MSG_END_CONTINUE)
  971. packet_header |= I2C_HEADER_CONTINUE_XFER;
  972. else if (end_state == MSG_END_REPEAT_START)
  973. packet_header |= I2C_HEADER_REPEAT_START;
  974. if (msg->flags & I2C_M_TEN) {
  975. packet_header |= msg->addr;
  976. packet_header |= I2C_HEADER_10BIT_ADDR;
  977. } else {
  978. packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
  979. }
  980. if (msg->flags & I2C_M_IGNORE_NAK)
  981. packet_header |= I2C_HEADER_CONT_ON_NAK;
  982. if (msg->flags & I2C_M_RD)
  983. packet_header |= I2C_HEADER_READ;
  984. if (i2c_dev->dma_mode && !i2c_dev->msg_read)
  985. *dma_buf++ = packet_header;
  986. else
  987. i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
  988. }
  989. static int tegra_i2c_error_recover(struct tegra_i2c_dev *i2c_dev,
  990. struct i2c_msg *msg)
  991. {
  992. if (i2c_dev->msg_err == I2C_ERR_NONE)
  993. return 0;
  994. tegra_i2c_init(i2c_dev);
  995. /* start recovery upon arbitration loss in single master mode */
  996. if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
  997. if (!i2c_dev->multimaster_mode)
  998. return i2c_recover_bus(&i2c_dev->adapter);
  999. return -EAGAIN;
  1000. }
  1001. if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
  1002. if (msg->flags & I2C_M_IGNORE_NAK)
  1003. return 0;
  1004. return -EREMOTEIO;
  1005. }
  1006. return -EIO;
  1007. }
  1008. static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
  1009. struct i2c_msg *msg,
  1010. enum msg_end_type end_state)
  1011. {
  1012. unsigned long time_left, xfer_time = 100;
  1013. size_t xfer_size;
  1014. u32 int_mask;
  1015. int err;
  1016. err = tegra_i2c_flush_fifos(i2c_dev);
  1017. if (err)
  1018. return err;
  1019. i2c_dev->msg_buf = msg->buf;
  1020. i2c_dev->msg_len = msg->len;
  1021. i2c_dev->msg_err = I2C_ERR_NONE;
  1022. i2c_dev->msg_read = !!(msg->flags & I2C_M_RD);
  1023. reinit_completion(&i2c_dev->msg_complete);
  1024. /*
  1025. * For SMBUS block read command, read only 1 byte in the first transfer.
  1026. * Adjust that 1 byte for the next transfer in the msg buffer and msg
  1027. * length.
  1028. */
  1029. if (msg->flags & I2C_M_RECV_LEN) {
  1030. if (end_state == MSG_END_CONTINUE) {
  1031. i2c_dev->msg_len = 1;
  1032. } else {
  1033. i2c_dev->msg_buf += 1;
  1034. i2c_dev->msg_len -= 1;
  1035. }
  1036. }
  1037. i2c_dev->msg_buf_remaining = i2c_dev->msg_len;
  1038. if (i2c_dev->msg_read)
  1039. xfer_size = i2c_dev->msg_len;
  1040. else
  1041. xfer_size = i2c_dev->msg_len + I2C_PACKET_HEADER_SIZE;
  1042. xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
  1043. i2c_dev->dma_mode = xfer_size > I2C_PIO_MODE_PREFERRED_LEN &&
  1044. i2c_dev->dma_buf && !i2c_dev->atomic_mode;
  1045. tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
  1046. /*
  1047. * Transfer time in mSec = Total bits / transfer rate
  1048. * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
  1049. */
  1050. xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
  1051. i2c_dev->timings.bus_freq_hz);
  1052. int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
  1053. tegra_i2c_unmask_irq(i2c_dev, int_mask);
  1054. if (i2c_dev->dma_mode) {
  1055. if (i2c_dev->msg_read) {
  1056. dma_sync_single_for_device(i2c_dev->dma_dev,
  1057. i2c_dev->dma_phys,
  1058. xfer_size, DMA_FROM_DEVICE);
  1059. err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
  1060. if (err)
  1061. return err;
  1062. } else {
  1063. dma_sync_single_for_cpu(i2c_dev->dma_dev,
  1064. i2c_dev->dma_phys,
  1065. xfer_size, DMA_TO_DEVICE);
  1066. }
  1067. }
  1068. tegra_i2c_push_packet_header(i2c_dev, msg, end_state);
  1069. if (!i2c_dev->msg_read) {
  1070. if (i2c_dev->dma_mode) {
  1071. memcpy(i2c_dev->dma_buf + I2C_PACKET_HEADER_SIZE,
  1072. msg->buf, i2c_dev->msg_len);
  1073. dma_sync_single_for_device(i2c_dev->dma_dev,
  1074. i2c_dev->dma_phys,
  1075. xfer_size, DMA_TO_DEVICE);
  1076. err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
  1077. if (err)
  1078. return err;
  1079. } else {
  1080. tegra_i2c_fill_tx_fifo(i2c_dev);
  1081. }
  1082. }
  1083. if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
  1084. int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
  1085. if (!i2c_dev->dma_mode) {
  1086. if (msg->flags & I2C_M_RD)
  1087. int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
  1088. else if (i2c_dev->msg_buf_remaining)
  1089. int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
  1090. }
  1091. tegra_i2c_unmask_irq(i2c_dev, int_mask);
  1092. dev_dbg(i2c_dev->dev, "unmasked IRQ: %02x\n",
  1093. i2c_readl(i2c_dev, I2C_INT_MASK));
  1094. if (i2c_dev->dma_mode) {
  1095. time_left = tegra_i2c_wait_completion(i2c_dev,
  1096. &i2c_dev->dma_complete,
  1097. xfer_time);
  1098. /*
  1099. * Synchronize DMA first, since dmaengine_terminate_sync()
  1100. * performs synchronization after the transfer's termination
  1101. * and we want to get a completion if transfer succeeded.
  1102. */
  1103. dmaengine_synchronize(i2c_dev->dma_chan);
  1104. dmaengine_terminate_sync(i2c_dev->dma_chan);
  1105. if (!time_left && !completion_done(&i2c_dev->dma_complete)) {
  1106. tegra_i2c_init(i2c_dev);
  1107. return -ETIMEDOUT;
  1108. }
  1109. if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
  1110. dma_sync_single_for_cpu(i2c_dev->dma_dev,
  1111. i2c_dev->dma_phys,
  1112. xfer_size, DMA_FROM_DEVICE);
  1113. memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf, i2c_dev->msg_len);
  1114. }
  1115. }
  1116. time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete,
  1117. xfer_time);
  1118. tegra_i2c_mask_irq(i2c_dev, int_mask);
  1119. if (time_left == 0) {
  1120. tegra_i2c_init(i2c_dev);
  1121. return -ETIMEDOUT;
  1122. }
  1123. dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
  1124. time_left, completion_done(&i2c_dev->msg_complete),
  1125. i2c_dev->msg_err);
  1126. i2c_dev->dma_mode = false;
  1127. err = tegra_i2c_error_recover(i2c_dev, msg);
  1128. if (err)
  1129. return err;
  1130. return 0;
  1131. }
  1132. static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  1133. int num)
  1134. {
  1135. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  1136. int i, ret;
  1137. ret = pm_runtime_get_sync(i2c_dev->dev);
  1138. if (ret < 0) {
  1139. dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
  1140. pm_runtime_put_noidle(i2c_dev->dev);
  1141. return ret;
  1142. }
  1143. for (i = 0; i < num; i++) {
  1144. enum msg_end_type end_type = MSG_END_STOP;
  1145. if (i < (num - 1)) {
  1146. /* check whether follow up message is coming */
  1147. if (msgs[i + 1].flags & I2C_M_NOSTART)
  1148. end_type = MSG_END_CONTINUE;
  1149. else
  1150. end_type = MSG_END_REPEAT_START;
  1151. }
  1152. /* If M_RECV_LEN use ContinueXfer to read the first byte */
  1153. if (msgs[i].flags & I2C_M_RECV_LEN) {
  1154. ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], MSG_END_CONTINUE);
  1155. if (ret)
  1156. break;
  1157. /* Validate message length before proceeding */
  1158. if (msgs[i].buf[0] == 0 || msgs[i].buf[0] > I2C_SMBUS_BLOCK_MAX)
  1159. break;
  1160. /* Set the msg length from first byte */
  1161. msgs[i].len += msgs[i].buf[0];
  1162. dev_dbg(i2c_dev->dev, "reading %d bytes\n", msgs[i].len);
  1163. }
  1164. ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
  1165. if (ret)
  1166. break;
  1167. }
  1168. pm_runtime_put(i2c_dev->dev);
  1169. return ret ?: i;
  1170. }
  1171. static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap,
  1172. struct i2c_msg msgs[], int num)
  1173. {
  1174. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  1175. int ret;
  1176. i2c_dev->atomic_mode = true;
  1177. ret = tegra_i2c_xfer(adap, msgs, num);
  1178. i2c_dev->atomic_mode = false;
  1179. return ret;
  1180. }
  1181. static u32 tegra_i2c_func(struct i2c_adapter *adap)
  1182. {
  1183. struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  1184. u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  1185. I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
  1186. if (i2c_dev->hw->has_continue_xfer_support)
  1187. ret |= I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  1188. return ret;
  1189. }
  1190. static const struct i2c_algorithm tegra_i2c_algo = {
  1191. .master_xfer = tegra_i2c_xfer,
  1192. .master_xfer_atomic = tegra_i2c_xfer_atomic,
  1193. .functionality = tegra_i2c_func,
  1194. };
  1195. /* payload size is only 12 bit */
  1196. static const struct i2c_adapter_quirks tegra_i2c_quirks = {
  1197. .flags = I2C_AQ_NO_ZERO_LEN,
  1198. .max_read_len = SZ_4K,
  1199. .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
  1200. };
  1201. static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
  1202. .flags = I2C_AQ_NO_ZERO_LEN,
  1203. .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
  1204. };
  1205. static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
  1206. .recover_bus = tegra_i2c_issue_bus_clear,
  1207. };
  1208. static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
  1209. .has_continue_xfer_support = false,
  1210. .has_per_pkt_xfer_complete_irq = false,
  1211. .clk_divisor_hs_mode = 3,
  1212. .clk_divisor_std_mode = 0,
  1213. .clk_divisor_fast_mode = 0,
  1214. .clk_divisor_fast_plus_mode = 0,
  1215. .has_config_load_reg = false,
  1216. .has_multi_master_mode = false,
  1217. .has_slcg_override_reg = false,
  1218. .has_mst_fifo = false,
  1219. .quirks = &tegra_i2c_quirks,
  1220. .supports_bus_clear = false,
  1221. .has_apb_dma = true,
  1222. .tlow_std_mode = 0x4,
  1223. .thigh_std_mode = 0x2,
  1224. .tlow_fast_fastplus_mode = 0x4,
  1225. .thigh_fast_fastplus_mode = 0x2,
  1226. .setup_hold_time_std_mode = 0x0,
  1227. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1228. .setup_hold_time_hs_mode = 0x0,
  1229. .has_interface_timing_reg = false,
  1230. };
  1231. static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
  1232. .has_continue_xfer_support = true,
  1233. .has_per_pkt_xfer_complete_irq = false,
  1234. .clk_divisor_hs_mode = 3,
  1235. .clk_divisor_std_mode = 0,
  1236. .clk_divisor_fast_mode = 0,
  1237. .clk_divisor_fast_plus_mode = 0,
  1238. .has_config_load_reg = false,
  1239. .has_multi_master_mode = false,
  1240. .has_slcg_override_reg = false,
  1241. .has_mst_fifo = false,
  1242. .quirks = &tegra_i2c_quirks,
  1243. .supports_bus_clear = false,
  1244. .has_apb_dma = true,
  1245. .tlow_std_mode = 0x4,
  1246. .thigh_std_mode = 0x2,
  1247. .tlow_fast_fastplus_mode = 0x4,
  1248. .thigh_fast_fastplus_mode = 0x2,
  1249. .setup_hold_time_std_mode = 0x0,
  1250. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1251. .setup_hold_time_hs_mode = 0x0,
  1252. .has_interface_timing_reg = false,
  1253. };
  1254. static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
  1255. .has_continue_xfer_support = true,
  1256. .has_per_pkt_xfer_complete_irq = true,
  1257. .clk_divisor_hs_mode = 1,
  1258. .clk_divisor_std_mode = 0x19,
  1259. .clk_divisor_fast_mode = 0x19,
  1260. .clk_divisor_fast_plus_mode = 0x10,
  1261. .has_config_load_reg = false,
  1262. .has_multi_master_mode = false,
  1263. .has_slcg_override_reg = false,
  1264. .has_mst_fifo = false,
  1265. .quirks = &tegra_i2c_quirks,
  1266. .supports_bus_clear = true,
  1267. .has_apb_dma = true,
  1268. .tlow_std_mode = 0x4,
  1269. .thigh_std_mode = 0x2,
  1270. .tlow_fast_fastplus_mode = 0x4,
  1271. .thigh_fast_fastplus_mode = 0x2,
  1272. .setup_hold_time_std_mode = 0x0,
  1273. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1274. .setup_hold_time_hs_mode = 0x0,
  1275. .has_interface_timing_reg = false,
  1276. };
  1277. static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
  1278. .has_continue_xfer_support = true,
  1279. .has_per_pkt_xfer_complete_irq = true,
  1280. .clk_divisor_hs_mode = 1,
  1281. .clk_divisor_std_mode = 0x19,
  1282. .clk_divisor_fast_mode = 0x19,
  1283. .clk_divisor_fast_plus_mode = 0x10,
  1284. .has_config_load_reg = true,
  1285. .has_multi_master_mode = false,
  1286. .has_slcg_override_reg = true,
  1287. .has_mst_fifo = false,
  1288. .quirks = &tegra_i2c_quirks,
  1289. .supports_bus_clear = true,
  1290. .has_apb_dma = true,
  1291. .tlow_std_mode = 0x4,
  1292. .thigh_std_mode = 0x2,
  1293. .tlow_fast_fastplus_mode = 0x4,
  1294. .thigh_fast_fastplus_mode = 0x2,
  1295. .setup_hold_time_std_mode = 0x0,
  1296. .setup_hold_time_fast_fast_plus_mode = 0x0,
  1297. .setup_hold_time_hs_mode = 0x0,
  1298. .has_interface_timing_reg = true,
  1299. };
  1300. static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
  1301. .has_continue_xfer_support = true,
  1302. .has_per_pkt_xfer_complete_irq = true,
  1303. .clk_divisor_hs_mode = 1,
  1304. .clk_divisor_std_mode = 0x19,
  1305. .clk_divisor_fast_mode = 0x19,
  1306. .clk_divisor_fast_plus_mode = 0x10,
  1307. .has_config_load_reg = true,
  1308. .has_multi_master_mode = false,
  1309. .has_slcg_override_reg = true,
  1310. .has_mst_fifo = false,
  1311. .quirks = &tegra_i2c_quirks,
  1312. .supports_bus_clear = true,
  1313. .has_apb_dma = true,
  1314. .tlow_std_mode = 0x4,
  1315. .thigh_std_mode = 0x2,
  1316. .tlow_fast_fastplus_mode = 0x4,
  1317. .thigh_fast_fastplus_mode = 0x2,
  1318. .setup_hold_time_std_mode = 0,
  1319. .setup_hold_time_fast_fast_plus_mode = 0,
  1320. .setup_hold_time_hs_mode = 0,
  1321. .has_interface_timing_reg = true,
  1322. };
  1323. static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
  1324. .has_continue_xfer_support = true,
  1325. .has_per_pkt_xfer_complete_irq = true,
  1326. .clk_divisor_hs_mode = 1,
  1327. .clk_divisor_std_mode = 0x16,
  1328. .clk_divisor_fast_mode = 0x19,
  1329. .clk_divisor_fast_plus_mode = 0x10,
  1330. .has_config_load_reg = true,
  1331. .has_multi_master_mode = false,
  1332. .has_slcg_override_reg = true,
  1333. .has_mst_fifo = false,
  1334. .quirks = &tegra_i2c_quirks,
  1335. .supports_bus_clear = true,
  1336. .has_apb_dma = false,
  1337. .tlow_std_mode = 0x4,
  1338. .thigh_std_mode = 0x3,
  1339. .tlow_fast_fastplus_mode = 0x4,
  1340. .thigh_fast_fastplus_mode = 0x2,
  1341. .setup_hold_time_std_mode = 0,
  1342. .setup_hold_time_fast_fast_plus_mode = 0,
  1343. .setup_hold_time_hs_mode = 0,
  1344. .has_interface_timing_reg = true,
  1345. };
  1346. static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
  1347. .has_continue_xfer_support = true,
  1348. .has_per_pkt_xfer_complete_irq = true,
  1349. .clk_divisor_hs_mode = 1,
  1350. .clk_divisor_std_mode = 0x4f,
  1351. .clk_divisor_fast_mode = 0x3c,
  1352. .clk_divisor_fast_plus_mode = 0x16,
  1353. .has_config_load_reg = true,
  1354. .has_multi_master_mode = true,
  1355. .has_slcg_override_reg = true,
  1356. .has_mst_fifo = true,
  1357. .quirks = &tegra194_i2c_quirks,
  1358. .supports_bus_clear = true,
  1359. .has_apb_dma = false,
  1360. .tlow_std_mode = 0x8,
  1361. .thigh_std_mode = 0x7,
  1362. .tlow_fast_fastplus_mode = 0x2,
  1363. .thigh_fast_fastplus_mode = 0x2,
  1364. .setup_hold_time_std_mode = 0x08080808,
  1365. .setup_hold_time_fast_fast_plus_mode = 0x02020202,
  1366. .setup_hold_time_hs_mode = 0x090909,
  1367. .has_interface_timing_reg = true,
  1368. };
  1369. static const struct of_device_id tegra_i2c_of_match[] = {
  1370. { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
  1371. { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
  1372. #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
  1373. { .compatible = "nvidia,tegra210-i2c-vi", .data = &tegra210_i2c_hw, },
  1374. #endif
  1375. { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
  1376. { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
  1377. { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
  1378. { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
  1379. { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
  1380. #if IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC)
  1381. { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
  1382. #endif
  1383. {},
  1384. };
  1385. MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
  1386. static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
  1387. {
  1388. struct device_node *np = i2c_dev->dev->of_node;
  1389. bool multi_mode;
  1390. i2c_parse_fw_timings(i2c_dev->dev, &i2c_dev->timings, true);
  1391. multi_mode = device_property_read_bool(i2c_dev->dev, "multi-master");
  1392. i2c_dev->multimaster_mode = multi_mode;
  1393. if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) &&
  1394. of_device_is_compatible(np, "nvidia,tegra20-i2c-dvc"))
  1395. i2c_dev->is_dvc = true;
  1396. if (IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) &&
  1397. of_device_is_compatible(np, "nvidia,tegra210-i2c-vi"))
  1398. i2c_dev->is_vi = true;
  1399. }
  1400. static int tegra_i2c_init_clocks(struct tegra_i2c_dev *i2c_dev)
  1401. {
  1402. int err;
  1403. if (ACPI_HANDLE(i2c_dev->dev))
  1404. return 0;
  1405. i2c_dev->clocks[i2c_dev->nclocks++].id = "div-clk";
  1406. if (i2c_dev->hw == &tegra20_i2c_hw || i2c_dev->hw == &tegra30_i2c_hw)
  1407. i2c_dev->clocks[i2c_dev->nclocks++].id = "fast-clk";
  1408. if (IS_VI(i2c_dev))
  1409. i2c_dev->clocks[i2c_dev->nclocks++].id = "slow";
  1410. err = devm_clk_bulk_get(i2c_dev->dev, i2c_dev->nclocks,
  1411. i2c_dev->clocks);
  1412. if (err)
  1413. return err;
  1414. err = clk_bulk_prepare(i2c_dev->nclocks, i2c_dev->clocks);
  1415. if (err)
  1416. return err;
  1417. i2c_dev->div_clk = i2c_dev->clocks[0].clk;
  1418. if (!i2c_dev->multimaster_mode)
  1419. return 0;
  1420. err = clk_enable(i2c_dev->div_clk);
  1421. if (err) {
  1422. dev_err(i2c_dev->dev, "failed to enable div-clk: %d\n", err);
  1423. goto unprepare_clocks;
  1424. }
  1425. return 0;
  1426. unprepare_clocks:
  1427. clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
  1428. return err;
  1429. }
  1430. static void tegra_i2c_release_clocks(struct tegra_i2c_dev *i2c_dev)
  1431. {
  1432. if (i2c_dev->multimaster_mode)
  1433. clk_disable(i2c_dev->div_clk);
  1434. clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
  1435. }
  1436. static int tegra_i2c_init_hardware(struct tegra_i2c_dev *i2c_dev)
  1437. {
  1438. int ret;
  1439. ret = pm_runtime_get_sync(i2c_dev->dev);
  1440. if (ret < 0)
  1441. dev_err(i2c_dev->dev, "runtime resume failed: %d\n", ret);
  1442. else
  1443. ret = tegra_i2c_init(i2c_dev);
  1444. pm_runtime_put_sync(i2c_dev->dev);
  1445. return ret;
  1446. }
  1447. static int tegra_i2c_probe(struct platform_device *pdev)
  1448. {
  1449. struct tegra_i2c_dev *i2c_dev;
  1450. struct resource *res;
  1451. int err;
  1452. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  1453. if (!i2c_dev)
  1454. return -ENOMEM;
  1455. platform_set_drvdata(pdev, i2c_dev);
  1456. init_completion(&i2c_dev->msg_complete);
  1457. init_completion(&i2c_dev->dma_complete);
  1458. i2c_dev->hw = device_get_match_data(&pdev->dev);
  1459. i2c_dev->cont_id = pdev->id;
  1460. i2c_dev->dev = &pdev->dev;
  1461. i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1462. if (IS_ERR(i2c_dev->base))
  1463. return PTR_ERR(i2c_dev->base);
  1464. i2c_dev->base_phys = res->start;
  1465. err = platform_get_irq(pdev, 0);
  1466. if (err < 0)
  1467. return err;
  1468. i2c_dev->irq = err;
  1469. /* interrupt will be enabled during of transfer time */
  1470. irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN);
  1471. err = devm_request_threaded_irq(i2c_dev->dev, i2c_dev->irq,
  1472. NULL, tegra_i2c_isr,
  1473. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1474. dev_name(i2c_dev->dev), i2c_dev);
  1475. if (err)
  1476. return err;
  1477. tegra_i2c_parse_dt(i2c_dev);
  1478. err = tegra_i2c_init_clocks(i2c_dev);
  1479. if (err)
  1480. return err;
  1481. err = tegra_i2c_init_dma(i2c_dev);
  1482. if (err)
  1483. goto release_clocks;
  1484. /*
  1485. * VI I2C is in VE power domain which is not always ON and not
  1486. * IRQ-safe. Thus, IRQ-safe device shouldn't be attached to a
  1487. * non IRQ-safe domain because this prevents powering off the power
  1488. * domain.
  1489. *
  1490. * VI I2C device shouldn't be marked as IRQ-safe because VI I2C won't
  1491. * be used for atomic transfers. ACPI device is not IRQ safe also.
  1492. */
  1493. if (!IS_VI(i2c_dev) && !has_acpi_companion(i2c_dev->dev))
  1494. pm_runtime_irq_safe(i2c_dev->dev);
  1495. pm_runtime_enable(i2c_dev->dev);
  1496. err = tegra_i2c_init_hardware(i2c_dev);
  1497. if (err)
  1498. goto release_rpm;
  1499. i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
  1500. i2c_dev->adapter.dev.of_node = i2c_dev->dev->of_node;
  1501. i2c_dev->adapter.dev.parent = i2c_dev->dev;
  1502. i2c_dev->adapter.retries = 1;
  1503. i2c_dev->adapter.timeout = 6 * HZ;
  1504. i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
  1505. i2c_dev->adapter.owner = THIS_MODULE;
  1506. i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
  1507. i2c_dev->adapter.algo = &tegra_i2c_algo;
  1508. i2c_dev->adapter.nr = pdev->id;
  1509. ACPI_COMPANION_SET(&i2c_dev->adapter.dev, ACPI_COMPANION(&pdev->dev));
  1510. if (i2c_dev->hw->supports_bus_clear)
  1511. i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
  1512. strscpy(i2c_dev->adapter.name, dev_name(i2c_dev->dev),
  1513. sizeof(i2c_dev->adapter.name));
  1514. err = i2c_add_numbered_adapter(&i2c_dev->adapter);
  1515. if (err)
  1516. goto release_rpm;
  1517. return 0;
  1518. release_rpm:
  1519. pm_runtime_disable(i2c_dev->dev);
  1520. tegra_i2c_release_dma(i2c_dev);
  1521. release_clocks:
  1522. tegra_i2c_release_clocks(i2c_dev);
  1523. return err;
  1524. }
  1525. static void tegra_i2c_remove(struct platform_device *pdev)
  1526. {
  1527. struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  1528. i2c_del_adapter(&i2c_dev->adapter);
  1529. pm_runtime_force_suspend(i2c_dev->dev);
  1530. tegra_i2c_release_dma(i2c_dev);
  1531. tegra_i2c_release_clocks(i2c_dev);
  1532. }
  1533. static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev)
  1534. {
  1535. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1536. int err;
  1537. err = pinctrl_pm_select_default_state(dev);
  1538. if (err)
  1539. return err;
  1540. err = clk_bulk_enable(i2c_dev->nclocks, i2c_dev->clocks);
  1541. if (err)
  1542. return err;
  1543. /*
  1544. * VI I2C device is attached to VE power domain which goes through
  1545. * power ON/OFF during runtime PM resume/suspend, meaning that
  1546. * controller needs to be re-initialized after power ON.
  1547. */
  1548. if (IS_VI(i2c_dev)) {
  1549. err = tegra_i2c_init(i2c_dev);
  1550. if (err)
  1551. goto disable_clocks;
  1552. }
  1553. return 0;
  1554. disable_clocks:
  1555. clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
  1556. return err;
  1557. }
  1558. static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev)
  1559. {
  1560. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1561. clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
  1562. return pinctrl_pm_select_idle_state(dev);
  1563. }
  1564. static int __maybe_unused tegra_i2c_suspend(struct device *dev)
  1565. {
  1566. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1567. int err;
  1568. i2c_mark_adapter_suspended(&i2c_dev->adapter);
  1569. if (!pm_runtime_status_suspended(dev)) {
  1570. err = tegra_i2c_runtime_suspend(dev);
  1571. if (err)
  1572. return err;
  1573. }
  1574. return 0;
  1575. }
  1576. static int __maybe_unused tegra_i2c_resume(struct device *dev)
  1577. {
  1578. struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  1579. int err;
  1580. /*
  1581. * We need to ensure that clocks are enabled so that registers can be
  1582. * restored in tegra_i2c_init().
  1583. */
  1584. err = tegra_i2c_runtime_resume(dev);
  1585. if (err)
  1586. return err;
  1587. err = tegra_i2c_init(i2c_dev);
  1588. if (err)
  1589. return err;
  1590. /*
  1591. * In case we are runtime suspended, disable clocks again so that we
  1592. * don't unbalance the clock reference counts during the next runtime
  1593. * resume transition.
  1594. */
  1595. if (pm_runtime_status_suspended(dev)) {
  1596. err = tegra_i2c_runtime_suspend(dev);
  1597. if (err)
  1598. return err;
  1599. }
  1600. i2c_mark_adapter_resumed(&i2c_dev->adapter);
  1601. return 0;
  1602. }
  1603. static const struct dev_pm_ops tegra_i2c_pm = {
  1604. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
  1605. SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
  1606. NULL)
  1607. };
  1608. static const struct acpi_device_id tegra_i2c_acpi_match[] = {
  1609. {.id = "NVDA0101", .driver_data = (kernel_ulong_t)&tegra210_i2c_hw},
  1610. {.id = "NVDA0201", .driver_data = (kernel_ulong_t)&tegra186_i2c_hw},
  1611. {.id = "NVDA0301", .driver_data = (kernel_ulong_t)&tegra194_i2c_hw},
  1612. { }
  1613. };
  1614. MODULE_DEVICE_TABLE(acpi, tegra_i2c_acpi_match);
  1615. static struct platform_driver tegra_i2c_driver = {
  1616. .probe = tegra_i2c_probe,
  1617. .remove_new = tegra_i2c_remove,
  1618. .driver = {
  1619. .name = "tegra-i2c",
  1620. .of_match_table = tegra_i2c_of_match,
  1621. .acpi_match_table = tegra_i2c_acpi_match,
  1622. .pm = &tegra_i2c_pm,
  1623. },
  1624. };
  1625. module_platform_driver(tegra_i2c_driver);
  1626. MODULE_DESCRIPTION("NVIDIA Tegra I2C Bus Controller driver");
  1627. MODULE_AUTHOR("Colin Cross");
  1628. MODULE_LICENSE("GPL v2");