i2c-qup.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2009-2013, 2016-2018, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2014, Sony Mobile Communications AB.
  5. *
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/atomic.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/scatterlist.h>
  23. /* QUP Registers */
  24. #define QUP_CONFIG 0x000
  25. #define QUP_STATE 0x004
  26. #define QUP_IO_MODE 0x008
  27. #define QUP_SW_RESET 0x00c
  28. #define QUP_OPERATIONAL 0x018
  29. #define QUP_ERROR_FLAGS 0x01c
  30. #define QUP_ERROR_FLAGS_EN 0x020
  31. #define QUP_OPERATIONAL_MASK 0x028
  32. #define QUP_HW_VERSION 0x030
  33. #define QUP_MX_OUTPUT_CNT 0x100
  34. #define QUP_OUT_FIFO_BASE 0x110
  35. #define QUP_MX_WRITE_CNT 0x150
  36. #define QUP_MX_INPUT_CNT 0x200
  37. #define QUP_MX_READ_CNT 0x208
  38. #define QUP_IN_FIFO_BASE 0x218
  39. #define QUP_I2C_CLK_CTL 0x400
  40. #define QUP_I2C_STATUS 0x404
  41. #define QUP_I2C_MASTER_GEN 0x408
  42. /* QUP States and reset values */
  43. #define QUP_RESET_STATE 0
  44. #define QUP_RUN_STATE 1
  45. #define QUP_PAUSE_STATE 3
  46. #define QUP_STATE_MASK 3
  47. #define QUP_STATE_VALID BIT(2)
  48. #define QUP_I2C_MAST_GEN BIT(4)
  49. #define QUP_I2C_FLUSH BIT(6)
  50. #define QUP_OPERATIONAL_RESET 0x000ff0
  51. #define QUP_I2C_STATUS_RESET 0xfffffc
  52. /* QUP OPERATIONAL FLAGS */
  53. #define QUP_I2C_NACK_FLAG BIT(3)
  54. #define QUP_OUT_NOT_EMPTY BIT(4)
  55. #define QUP_IN_NOT_EMPTY BIT(5)
  56. #define QUP_OUT_FULL BIT(6)
  57. #define QUP_OUT_SVC_FLAG BIT(8)
  58. #define QUP_IN_SVC_FLAG BIT(9)
  59. #define QUP_MX_OUTPUT_DONE BIT(10)
  60. #define QUP_MX_INPUT_DONE BIT(11)
  61. #define OUT_BLOCK_WRITE_REQ BIT(12)
  62. #define IN_BLOCK_READ_REQ BIT(13)
  63. /* I2C mini core related values */
  64. #define QUP_NO_INPUT BIT(7)
  65. #define QUP_CLOCK_AUTO_GATE BIT(13)
  66. #define I2C_MINI_CORE (2 << 8)
  67. #define I2C_N_VAL 15
  68. #define I2C_N_VAL_V2 7
  69. /* Most significant word offset in FIFO port */
  70. #define QUP_MSW_SHIFT (I2C_N_VAL + 1)
  71. /* Packing/Unpacking words in FIFOs, and IO modes */
  72. #define QUP_OUTPUT_BLK_MODE (1 << 10)
  73. #define QUP_OUTPUT_BAM_MODE (3 << 10)
  74. #define QUP_INPUT_BLK_MODE (1 << 12)
  75. #define QUP_INPUT_BAM_MODE (3 << 12)
  76. #define QUP_BAM_MODE (QUP_OUTPUT_BAM_MODE | QUP_INPUT_BAM_MODE)
  77. #define QUP_UNPACK_EN BIT(14)
  78. #define QUP_PACK_EN BIT(15)
  79. #define QUP_REPACK_EN (QUP_UNPACK_EN | QUP_PACK_EN)
  80. #define QUP_V2_TAGS_EN 1
  81. #define QUP_OUTPUT_BLOCK_SIZE(x)(((x) >> 0) & 0x03)
  82. #define QUP_OUTPUT_FIFO_SIZE(x) (((x) >> 2) & 0x07)
  83. #define QUP_INPUT_BLOCK_SIZE(x) (((x) >> 5) & 0x03)
  84. #define QUP_INPUT_FIFO_SIZE(x) (((x) >> 7) & 0x07)
  85. /* QUP tags */
  86. #define QUP_TAG_START (1 << 8)
  87. #define QUP_TAG_DATA (2 << 8)
  88. #define QUP_TAG_STOP (3 << 8)
  89. #define QUP_TAG_REC (4 << 8)
  90. #define QUP_BAM_INPUT_EOT 0x93
  91. #define QUP_BAM_FLUSH_STOP 0x96
  92. /* QUP v2 tags */
  93. #define QUP_TAG_V2_START 0x81
  94. #define QUP_TAG_V2_DATAWR 0x82
  95. #define QUP_TAG_V2_DATAWR_STOP 0x83
  96. #define QUP_TAG_V2_DATARD 0x85
  97. #define QUP_TAG_V2_DATARD_NACK 0x86
  98. #define QUP_TAG_V2_DATARD_STOP 0x87
  99. /* Status, Error flags */
  100. #define I2C_STATUS_WR_BUFFER_FULL BIT(0)
  101. #define I2C_STATUS_BUS_ACTIVE BIT(8)
  102. #define I2C_STATUS_ERROR_MASK 0x38000fc
  103. #define QUP_STATUS_ERROR_FLAGS 0x7c
  104. #define QUP_READ_LIMIT 256
  105. #define SET_BIT 0x1
  106. #define RESET_BIT 0x0
  107. #define ONE_BYTE 0x1
  108. #define QUP_I2C_MX_CONFIG_DURING_RUN BIT(31)
  109. /* Maximum transfer length for single DMA descriptor */
  110. #define MX_TX_RX_LEN SZ_64K
  111. #define MX_BLOCKS (MX_TX_RX_LEN / QUP_READ_LIMIT)
  112. /* Maximum transfer length for all DMA descriptors */
  113. #define MX_DMA_TX_RX_LEN (2 * MX_TX_RX_LEN)
  114. #define MX_DMA_BLOCKS (MX_DMA_TX_RX_LEN / QUP_READ_LIMIT)
  115. /*
  116. * Minimum transfer timeout for i2c transfers in seconds. It will be added on
  117. * the top of maximum transfer time calculated from i2c bus speed to compensate
  118. * the overheads.
  119. */
  120. #define TOUT_MIN 2
  121. /* I2C Frequency Modes */
  122. #define I2C_STANDARD_FREQ 100000
  123. #define I2C_FAST_MODE_FREQ 400000
  124. #define I2C_FAST_MODE_PLUS_FREQ 1000000
  125. /* Default values. Use these if FW query fails */
  126. #define DEFAULT_CLK_FREQ I2C_STANDARD_FREQ
  127. #define DEFAULT_SRC_CLK 20000000
  128. /*
  129. * Max tags length (start, stop and maximum 2 bytes address) for each QUP
  130. * data transfer
  131. */
  132. #define QUP_MAX_TAGS_LEN 4
  133. /* Max data length for each DATARD tags */
  134. #define RECV_MAX_DATA_LEN 254
  135. /* TAG length for DATA READ in RX FIFO */
  136. #define READ_RX_TAGS_LEN 2
  137. static unsigned int scl_freq;
  138. module_param_named(scl_freq, scl_freq, uint, 0444);
  139. MODULE_PARM_DESC(scl_freq, "SCL frequency override");
  140. /*
  141. * count: no of blocks
  142. * pos: current block number
  143. * tx_tag_len: tx tag length for current block
  144. * rx_tag_len: rx tag length for current block
  145. * data_len: remaining data length for current message
  146. * cur_blk_len: data length for current block
  147. * total_tx_len: total tx length including tag bytes for current QUP transfer
  148. * total_rx_len: total rx length including tag bytes for current QUP transfer
  149. * tx_fifo_data_pos: current byte number in TX FIFO word
  150. * tx_fifo_free: number of free bytes in current QUP block write.
  151. * rx_fifo_data_pos: current byte number in RX FIFO word
  152. * fifo_available: number of available bytes in RX FIFO for current
  153. * QUP block read
  154. * tx_fifo_data: QUP TX FIFO write works on word basis (4 bytes). New byte write
  155. * to TX FIFO will be appended in this data and will be written to
  156. * TX FIFO when all the 4 bytes are available.
  157. * rx_fifo_data: QUP RX FIFO read works on word basis (4 bytes). This will
  158. * contains the 4 bytes of RX data.
  159. * cur_data: pointer to tell cur data position for current message
  160. * cur_tx_tags: pointer to tell cur position in tags
  161. * tx_tags_sent: all tx tag bytes have been written in FIFO word
  162. * send_last_word: for tx FIFO, last word send is pending in current block
  163. * rx_bytes_read: if all the bytes have been read from rx FIFO.
  164. * rx_tags_fetched: all the rx tag bytes have been fetched from rx fifo word
  165. * is_tx_blk_mode: whether tx uses block or FIFO mode in case of non BAM xfer.
  166. * is_rx_blk_mode: whether rx uses block or FIFO mode in case of non BAM xfer.
  167. * tags: contains tx tag bytes for current QUP transfer
  168. */
  169. struct qup_i2c_block {
  170. int count;
  171. int pos;
  172. int tx_tag_len;
  173. int rx_tag_len;
  174. int data_len;
  175. int cur_blk_len;
  176. int total_tx_len;
  177. int total_rx_len;
  178. int tx_fifo_data_pos;
  179. int tx_fifo_free;
  180. int rx_fifo_data_pos;
  181. int fifo_available;
  182. u32 tx_fifo_data;
  183. u32 rx_fifo_data;
  184. u8 *cur_data;
  185. u8 *cur_tx_tags;
  186. bool tx_tags_sent;
  187. bool send_last_word;
  188. bool rx_tags_fetched;
  189. bool rx_bytes_read;
  190. bool is_tx_blk_mode;
  191. bool is_rx_blk_mode;
  192. u8 tags[6];
  193. };
  194. struct qup_i2c_tag {
  195. u8 *start;
  196. dma_addr_t addr;
  197. };
  198. struct qup_i2c_bam {
  199. struct qup_i2c_tag tag;
  200. struct dma_chan *dma;
  201. struct scatterlist *sg;
  202. unsigned int sg_cnt;
  203. };
  204. struct qup_i2c_dev {
  205. struct device *dev;
  206. void __iomem *base;
  207. int irq;
  208. struct clk *clk;
  209. struct clk *pclk;
  210. struct i2c_adapter adap;
  211. int clk_ctl;
  212. int out_fifo_sz;
  213. int in_fifo_sz;
  214. int out_blk_sz;
  215. int in_blk_sz;
  216. int blk_xfer_limit;
  217. unsigned long one_byte_t;
  218. unsigned long xfer_timeout;
  219. struct qup_i2c_block blk;
  220. struct i2c_msg *msg;
  221. /* Current posion in user message buffer */
  222. int pos;
  223. /* I2C protocol errors */
  224. u32 bus_err;
  225. /* QUP core errors */
  226. u32 qup_err;
  227. /* To check if this is the last msg */
  228. bool is_last;
  229. bool is_smbus_read;
  230. /* To configure when bus is in run state */
  231. u32 config_run;
  232. /* dma parameters */
  233. bool is_dma;
  234. /* To check if the current transfer is using DMA */
  235. bool use_dma;
  236. unsigned int max_xfer_sg_len;
  237. unsigned int tag_buf_pos;
  238. /* The threshold length above which block mode will be used */
  239. unsigned int blk_mode_threshold;
  240. struct dma_pool *dpool;
  241. struct qup_i2c_tag start_tag;
  242. struct qup_i2c_bam brx;
  243. struct qup_i2c_bam btx;
  244. struct completion xfer;
  245. /* function to write data in tx fifo */
  246. void (*write_tx_fifo)(struct qup_i2c_dev *qup);
  247. /* function to read data from rx fifo */
  248. void (*read_rx_fifo)(struct qup_i2c_dev *qup);
  249. /* function to write tags in tx fifo for i2c read transfer */
  250. void (*write_rx_tags)(struct qup_i2c_dev *qup);
  251. };
  252. static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
  253. {
  254. struct qup_i2c_dev *qup = dev;
  255. struct qup_i2c_block *blk = &qup->blk;
  256. u32 bus_err;
  257. u32 qup_err;
  258. u32 opflags;
  259. bus_err = readl(qup->base + QUP_I2C_STATUS);
  260. qup_err = readl(qup->base + QUP_ERROR_FLAGS);
  261. opflags = readl(qup->base + QUP_OPERATIONAL);
  262. if (!qup->msg) {
  263. /* Clear Error interrupt */
  264. writel(QUP_RESET_STATE, qup->base + QUP_STATE);
  265. return IRQ_HANDLED;
  266. }
  267. bus_err &= I2C_STATUS_ERROR_MASK;
  268. qup_err &= QUP_STATUS_ERROR_FLAGS;
  269. /* Clear the error bits in QUP_ERROR_FLAGS */
  270. if (qup_err)
  271. writel(qup_err, qup->base + QUP_ERROR_FLAGS);
  272. /* Clear the error bits in QUP_I2C_STATUS */
  273. if (bus_err)
  274. writel(bus_err, qup->base + QUP_I2C_STATUS);
  275. /*
  276. * Check for BAM mode and returns if already error has come for current
  277. * transfer. In Error case, sometimes, QUP generates more than one
  278. * interrupt.
  279. */
  280. if (qup->use_dma && (qup->qup_err || qup->bus_err))
  281. return IRQ_HANDLED;
  282. /* Reset the QUP State in case of error */
  283. if (qup_err || bus_err) {
  284. /*
  285. * Don’t reset the QUP state in case of BAM mode. The BAM
  286. * flush operation needs to be scheduled in transfer function
  287. * which will clear the remaining schedule descriptors in BAM
  288. * HW FIFO and generates the BAM interrupt.
  289. */
  290. if (!qup->use_dma)
  291. writel(QUP_RESET_STATE, qup->base + QUP_STATE);
  292. goto done;
  293. }
  294. if (opflags & QUP_OUT_SVC_FLAG) {
  295. writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
  296. if (opflags & OUT_BLOCK_WRITE_REQ) {
  297. blk->tx_fifo_free += qup->out_blk_sz;
  298. if (qup->msg->flags & I2C_M_RD)
  299. qup->write_rx_tags(qup);
  300. else
  301. qup->write_tx_fifo(qup);
  302. }
  303. }
  304. if (opflags & QUP_IN_SVC_FLAG) {
  305. writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
  306. if (!blk->is_rx_blk_mode) {
  307. blk->fifo_available += qup->in_fifo_sz;
  308. qup->read_rx_fifo(qup);
  309. } else if (opflags & IN_BLOCK_READ_REQ) {
  310. blk->fifo_available += qup->in_blk_sz;
  311. qup->read_rx_fifo(qup);
  312. }
  313. }
  314. if (qup->msg->flags & I2C_M_RD) {
  315. if (!blk->rx_bytes_read)
  316. return IRQ_HANDLED;
  317. } else {
  318. /*
  319. * Ideally, QUP_MAX_OUTPUT_DONE_FLAG should be checked
  320. * for FIFO mode also. But, QUP_MAX_OUTPUT_DONE_FLAG lags
  321. * behind QUP_OUTPUT_SERVICE_FLAG sometimes. The only reason
  322. * of interrupt for write message in FIFO mode is
  323. * QUP_MAX_OUTPUT_DONE_FLAG condition.
  324. */
  325. if (blk->is_tx_blk_mode && !(opflags & QUP_MX_OUTPUT_DONE))
  326. return IRQ_HANDLED;
  327. }
  328. done:
  329. qup->qup_err = qup_err;
  330. qup->bus_err = bus_err;
  331. complete(&qup->xfer);
  332. return IRQ_HANDLED;
  333. }
  334. static int qup_i2c_poll_state_mask(struct qup_i2c_dev *qup,
  335. u32 req_state, u32 req_mask)
  336. {
  337. int retries = 1;
  338. u32 state;
  339. /*
  340. * State transition takes 3 AHB clocks cycles + 3 I2C master clock
  341. * cycles. So retry once after a 1uS delay.
  342. */
  343. do {
  344. state = readl(qup->base + QUP_STATE);
  345. if (state & QUP_STATE_VALID &&
  346. (state & req_mask) == req_state)
  347. return 0;
  348. udelay(1);
  349. } while (retries--);
  350. return -ETIMEDOUT;
  351. }
  352. static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state)
  353. {
  354. return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK);
  355. }
  356. static void qup_i2c_flush(struct qup_i2c_dev *qup)
  357. {
  358. u32 val = readl(qup->base + QUP_STATE);
  359. val |= QUP_I2C_FLUSH;
  360. writel(val, qup->base + QUP_STATE);
  361. }
  362. static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup)
  363. {
  364. return qup_i2c_poll_state_mask(qup, 0, 0);
  365. }
  366. static int qup_i2c_poll_state_i2c_master(struct qup_i2c_dev *qup)
  367. {
  368. return qup_i2c_poll_state_mask(qup, QUP_I2C_MAST_GEN, QUP_I2C_MAST_GEN);
  369. }
  370. static int qup_i2c_change_state(struct qup_i2c_dev *qup, u32 state)
  371. {
  372. if (qup_i2c_poll_state_valid(qup) != 0)
  373. return -EIO;
  374. writel(state, qup->base + QUP_STATE);
  375. if (qup_i2c_poll_state(qup, state) != 0)
  376. return -EIO;
  377. return 0;
  378. }
  379. /* Check if I2C bus returns to IDLE state */
  380. static int qup_i2c_bus_active(struct qup_i2c_dev *qup, int len)
  381. {
  382. unsigned long timeout;
  383. u32 status;
  384. int ret = 0;
  385. timeout = jiffies + len * 4;
  386. for (;;) {
  387. status = readl(qup->base + QUP_I2C_STATUS);
  388. if (!(status & I2C_STATUS_BUS_ACTIVE))
  389. break;
  390. if (time_after(jiffies, timeout))
  391. ret = -ETIMEDOUT;
  392. usleep_range(len, len * 2);
  393. }
  394. return ret;
  395. }
  396. static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup)
  397. {
  398. struct qup_i2c_block *blk = &qup->blk;
  399. struct i2c_msg *msg = qup->msg;
  400. u32 addr = i2c_8bit_addr_from_msg(msg);
  401. u32 qup_tag;
  402. int idx;
  403. u32 val;
  404. if (qup->pos == 0) {
  405. val = QUP_TAG_START | addr;
  406. idx = 1;
  407. blk->tx_fifo_free--;
  408. } else {
  409. val = 0;
  410. idx = 0;
  411. }
  412. while (blk->tx_fifo_free && qup->pos < msg->len) {
  413. if (qup->pos == msg->len - 1)
  414. qup_tag = QUP_TAG_STOP;
  415. else
  416. qup_tag = QUP_TAG_DATA;
  417. if (idx & 1)
  418. val |= (qup_tag | msg->buf[qup->pos]) << QUP_MSW_SHIFT;
  419. else
  420. val = qup_tag | msg->buf[qup->pos];
  421. /* Write out the pair and the last odd value */
  422. if (idx & 1 || qup->pos == msg->len - 1)
  423. writel(val, qup->base + QUP_OUT_FIFO_BASE);
  424. qup->pos++;
  425. idx++;
  426. blk->tx_fifo_free--;
  427. }
  428. }
  429. static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
  430. struct i2c_msg *msg)
  431. {
  432. qup->blk.pos = 0;
  433. qup->blk.data_len = msg->len;
  434. qup->blk.count = DIV_ROUND_UP(msg->len, qup->blk_xfer_limit);
  435. }
  436. static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
  437. {
  438. int data_len;
  439. if (qup->blk.data_len > qup->blk_xfer_limit)
  440. data_len = qup->blk_xfer_limit;
  441. else
  442. data_len = qup->blk.data_len;
  443. return data_len;
  444. }
  445. static bool qup_i2c_check_msg_len(struct i2c_msg *msg)
  446. {
  447. return ((msg->flags & I2C_M_RD) && (msg->flags & I2C_M_RECV_LEN));
  448. }
  449. static int qup_i2c_set_tags_smb(u16 addr, u8 *tags, struct qup_i2c_dev *qup,
  450. struct i2c_msg *msg)
  451. {
  452. int len = 0;
  453. if (qup->is_smbus_read) {
  454. tags[len++] = QUP_TAG_V2_DATARD_STOP;
  455. tags[len++] = qup_i2c_get_data_len(qup);
  456. } else {
  457. tags[len++] = QUP_TAG_V2_START;
  458. tags[len++] = addr & 0xff;
  459. if (msg->flags & I2C_M_TEN)
  460. tags[len++] = addr >> 8;
  461. tags[len++] = QUP_TAG_V2_DATARD;
  462. /* Read 1 byte indicating the length of the SMBus message */
  463. tags[len++] = 1;
  464. }
  465. return len;
  466. }
  467. static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup,
  468. struct i2c_msg *msg)
  469. {
  470. u16 addr = i2c_8bit_addr_from_msg(msg);
  471. int len = 0;
  472. int data_len;
  473. int last = (qup->blk.pos == (qup->blk.count - 1)) && (qup->is_last);
  474. /* Handle tags for SMBus block read */
  475. if (qup_i2c_check_msg_len(msg))
  476. return qup_i2c_set_tags_smb(addr, tags, qup, msg);
  477. if (qup->blk.pos == 0) {
  478. tags[len++] = QUP_TAG_V2_START;
  479. tags[len++] = addr & 0xff;
  480. if (msg->flags & I2C_M_TEN)
  481. tags[len++] = addr >> 8;
  482. }
  483. /* Send _STOP commands for the last block */
  484. if (last) {
  485. if (msg->flags & I2C_M_RD)
  486. tags[len++] = QUP_TAG_V2_DATARD_STOP;
  487. else
  488. tags[len++] = QUP_TAG_V2_DATAWR_STOP;
  489. } else {
  490. if (msg->flags & I2C_M_RD)
  491. tags[len++] = qup->blk.pos == (qup->blk.count - 1) ?
  492. QUP_TAG_V2_DATARD_NACK :
  493. QUP_TAG_V2_DATARD;
  494. else
  495. tags[len++] = QUP_TAG_V2_DATAWR;
  496. }
  497. data_len = qup_i2c_get_data_len(qup);
  498. /* 0 implies 256 bytes */
  499. if (data_len == QUP_READ_LIMIT)
  500. tags[len++] = 0;
  501. else
  502. tags[len++] = data_len;
  503. return len;
  504. }
  505. static void qup_i2c_bam_cb(void *data)
  506. {
  507. struct qup_i2c_dev *qup = data;
  508. complete(&qup->xfer);
  509. }
  510. static int qup_sg_set_buf(struct scatterlist *sg, void *buf,
  511. unsigned int buflen, struct qup_i2c_dev *qup,
  512. int dir)
  513. {
  514. int ret;
  515. sg_set_buf(sg, buf, buflen);
  516. ret = dma_map_sg(qup->dev, sg, 1, dir);
  517. if (!ret)
  518. return -EINVAL;
  519. return 0;
  520. }
  521. static void qup_i2c_rel_dma(struct qup_i2c_dev *qup)
  522. {
  523. if (qup->btx.dma)
  524. dma_release_channel(qup->btx.dma);
  525. if (qup->brx.dma)
  526. dma_release_channel(qup->brx.dma);
  527. qup->btx.dma = NULL;
  528. qup->brx.dma = NULL;
  529. }
  530. static int qup_i2c_req_dma(struct qup_i2c_dev *qup)
  531. {
  532. int err;
  533. if (!qup->btx.dma) {
  534. qup->btx.dma = dma_request_slave_channel_reason(qup->dev, "tx");
  535. if (IS_ERR(qup->btx.dma)) {
  536. err = PTR_ERR(qup->btx.dma);
  537. qup->btx.dma = NULL;
  538. dev_err(qup->dev, "\n tx channel not available");
  539. return err;
  540. }
  541. }
  542. if (!qup->brx.dma) {
  543. qup->brx.dma = dma_request_slave_channel_reason(qup->dev, "rx");
  544. if (IS_ERR(qup->brx.dma)) {
  545. dev_err(qup->dev, "\n rx channel not available");
  546. err = PTR_ERR(qup->brx.dma);
  547. qup->brx.dma = NULL;
  548. qup_i2c_rel_dma(qup);
  549. return err;
  550. }
  551. }
  552. return 0;
  553. }
  554. static int qup_i2c_bam_make_desc(struct qup_i2c_dev *qup, struct i2c_msg *msg)
  555. {
  556. int ret = 0, limit = QUP_READ_LIMIT;
  557. u32 len = 0, blocks, rem;
  558. u32 i = 0, tlen, tx_len = 0;
  559. u8 *tags;
  560. qup->blk_xfer_limit = QUP_READ_LIMIT;
  561. qup_i2c_set_blk_data(qup, msg);
  562. blocks = qup->blk.count;
  563. rem = msg->len - (blocks - 1) * limit;
  564. if (msg->flags & I2C_M_RD) {
  565. while (qup->blk.pos < blocks) {
  566. tlen = (i == (blocks - 1)) ? rem : limit;
  567. tags = &qup->start_tag.start[qup->tag_buf_pos + len];
  568. len += qup_i2c_set_tags(tags, qup, msg);
  569. qup->blk.data_len -= tlen;
  570. /* scratch buf to read the start and len tags */
  571. ret = qup_sg_set_buf(&qup->brx.sg[qup->brx.sg_cnt++],
  572. &qup->brx.tag.start[0],
  573. 2, qup, DMA_FROM_DEVICE);
  574. if (ret)
  575. return ret;
  576. ret = qup_sg_set_buf(&qup->brx.sg[qup->brx.sg_cnt++],
  577. &msg->buf[limit * i],
  578. tlen, qup,
  579. DMA_FROM_DEVICE);
  580. if (ret)
  581. return ret;
  582. i++;
  583. qup->blk.pos = i;
  584. }
  585. ret = qup_sg_set_buf(&qup->btx.sg[qup->btx.sg_cnt++],
  586. &qup->start_tag.start[qup->tag_buf_pos],
  587. len, qup, DMA_TO_DEVICE);
  588. if (ret)
  589. return ret;
  590. qup->tag_buf_pos += len;
  591. } else {
  592. while (qup->blk.pos < blocks) {
  593. tlen = (i == (blocks - 1)) ? rem : limit;
  594. tags = &qup->start_tag.start[qup->tag_buf_pos + tx_len];
  595. len = qup_i2c_set_tags(tags, qup, msg);
  596. qup->blk.data_len -= tlen;
  597. ret = qup_sg_set_buf(&qup->btx.sg[qup->btx.sg_cnt++],
  598. tags, len,
  599. qup, DMA_TO_DEVICE);
  600. if (ret)
  601. return ret;
  602. tx_len += len;
  603. ret = qup_sg_set_buf(&qup->btx.sg[qup->btx.sg_cnt++],
  604. &msg->buf[limit * i],
  605. tlen, qup, DMA_TO_DEVICE);
  606. if (ret)
  607. return ret;
  608. i++;
  609. qup->blk.pos = i;
  610. }
  611. qup->tag_buf_pos += tx_len;
  612. }
  613. return 0;
  614. }
  615. static int qup_i2c_bam_schedule_desc(struct qup_i2c_dev *qup)
  616. {
  617. struct dma_async_tx_descriptor *txd, *rxd = NULL;
  618. int ret = 0;
  619. dma_cookie_t cookie_rx, cookie_tx;
  620. u32 len = 0;
  621. u32 tx_cnt = qup->btx.sg_cnt, rx_cnt = qup->brx.sg_cnt;
  622. /* schedule the EOT and FLUSH I2C tags */
  623. len = 1;
  624. if (rx_cnt) {
  625. qup->btx.tag.start[0] = QUP_BAM_INPUT_EOT;
  626. len++;
  627. /* scratch buf to read the BAM EOT FLUSH tags */
  628. ret = qup_sg_set_buf(&qup->brx.sg[rx_cnt++],
  629. &qup->brx.tag.start[0],
  630. 1, qup, DMA_FROM_DEVICE);
  631. if (ret)
  632. return ret;
  633. }
  634. qup->btx.tag.start[len - 1] = QUP_BAM_FLUSH_STOP;
  635. ret = qup_sg_set_buf(&qup->btx.sg[tx_cnt++], &qup->btx.tag.start[0],
  636. len, qup, DMA_TO_DEVICE);
  637. if (ret)
  638. return ret;
  639. txd = dmaengine_prep_slave_sg(qup->btx.dma, qup->btx.sg, tx_cnt,
  640. DMA_MEM_TO_DEV,
  641. DMA_PREP_INTERRUPT | DMA_PREP_FENCE);
  642. if (!txd) {
  643. dev_err(qup->dev, "failed to get tx desc\n");
  644. ret = -EINVAL;
  645. goto desc_err;
  646. }
  647. if (!rx_cnt) {
  648. txd->callback = qup_i2c_bam_cb;
  649. txd->callback_param = qup;
  650. }
  651. cookie_tx = dmaengine_submit(txd);
  652. if (dma_submit_error(cookie_tx)) {
  653. ret = -EINVAL;
  654. goto desc_err;
  655. }
  656. dma_async_issue_pending(qup->btx.dma);
  657. if (rx_cnt) {
  658. rxd = dmaengine_prep_slave_sg(qup->brx.dma, qup->brx.sg,
  659. rx_cnt, DMA_DEV_TO_MEM,
  660. DMA_PREP_INTERRUPT);
  661. if (!rxd) {
  662. dev_err(qup->dev, "failed to get rx desc\n");
  663. ret = -EINVAL;
  664. /* abort TX descriptors */
  665. dmaengine_terminate_all(qup->btx.dma);
  666. goto desc_err;
  667. }
  668. rxd->callback = qup_i2c_bam_cb;
  669. rxd->callback_param = qup;
  670. cookie_rx = dmaengine_submit(rxd);
  671. if (dma_submit_error(cookie_rx)) {
  672. ret = -EINVAL;
  673. goto desc_err;
  674. }
  675. dma_async_issue_pending(qup->brx.dma);
  676. }
  677. if (!wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout)) {
  678. dev_err(qup->dev, "normal trans timed out\n");
  679. ret = -ETIMEDOUT;
  680. }
  681. if (ret || qup->bus_err || qup->qup_err) {
  682. reinit_completion(&qup->xfer);
  683. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  684. if (ret) {
  685. dev_err(qup->dev, "change to run state timed out");
  686. goto desc_err;
  687. }
  688. qup_i2c_flush(qup);
  689. /* wait for remaining interrupts to occur */
  690. if (!wait_for_completion_timeout(&qup->xfer, HZ))
  691. dev_err(qup->dev, "flush timed out\n");
  692. ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
  693. }
  694. desc_err:
  695. dma_unmap_sg(qup->dev, qup->btx.sg, tx_cnt, DMA_TO_DEVICE);
  696. if (rx_cnt)
  697. dma_unmap_sg(qup->dev, qup->brx.sg, rx_cnt,
  698. DMA_FROM_DEVICE);
  699. return ret;
  700. }
  701. static void qup_i2c_bam_clear_tag_buffers(struct qup_i2c_dev *qup)
  702. {
  703. qup->btx.sg_cnt = 0;
  704. qup->brx.sg_cnt = 0;
  705. qup->tag_buf_pos = 0;
  706. }
  707. static int qup_i2c_bam_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  708. int num)
  709. {
  710. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  711. int ret = 0;
  712. int idx = 0;
  713. enable_irq(qup->irq);
  714. ret = qup_i2c_req_dma(qup);
  715. if (ret)
  716. goto out;
  717. writel(0, qup->base + QUP_MX_INPUT_CNT);
  718. writel(0, qup->base + QUP_MX_OUTPUT_CNT);
  719. /* set BAM mode */
  720. writel(QUP_REPACK_EN | QUP_BAM_MODE, qup->base + QUP_IO_MODE);
  721. /* mask fifo irqs */
  722. writel((0x3 << 8), qup->base + QUP_OPERATIONAL_MASK);
  723. /* set RUN STATE */
  724. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  725. if (ret)
  726. goto out;
  727. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  728. qup_i2c_bam_clear_tag_buffers(qup);
  729. for (idx = 0; idx < num; idx++) {
  730. qup->msg = msg + idx;
  731. qup->is_last = idx == (num - 1);
  732. ret = qup_i2c_bam_make_desc(qup, qup->msg);
  733. if (ret)
  734. break;
  735. /*
  736. * Make DMA descriptor and schedule the BAM transfer if its
  737. * already crossed the maximum length. Since the memory for all
  738. * tags buffers have been taken for 2 maximum possible
  739. * transfers length so it will never cross the buffer actual
  740. * length.
  741. */
  742. if (qup->btx.sg_cnt > qup->max_xfer_sg_len ||
  743. qup->brx.sg_cnt > qup->max_xfer_sg_len ||
  744. qup->is_last) {
  745. ret = qup_i2c_bam_schedule_desc(qup);
  746. if (ret)
  747. break;
  748. qup_i2c_bam_clear_tag_buffers(qup);
  749. }
  750. }
  751. out:
  752. disable_irq(qup->irq);
  753. qup->msg = NULL;
  754. return ret;
  755. }
  756. static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
  757. struct i2c_msg *msg)
  758. {
  759. unsigned long left;
  760. int ret = 0;
  761. left = wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout);
  762. if (!left) {
  763. writel(1, qup->base + QUP_SW_RESET);
  764. ret = -ETIMEDOUT;
  765. }
  766. if (qup->bus_err || qup->qup_err)
  767. ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
  768. return ret;
  769. }
  770. static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup)
  771. {
  772. struct qup_i2c_block *blk = &qup->blk;
  773. struct i2c_msg *msg = qup->msg;
  774. u32 val = 0;
  775. int idx = 0;
  776. while (blk->fifo_available && qup->pos < msg->len) {
  777. if ((idx & 1) == 0) {
  778. /* Reading 2 words at time */
  779. val = readl(qup->base + QUP_IN_FIFO_BASE);
  780. msg->buf[qup->pos++] = val & 0xFF;
  781. } else {
  782. msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
  783. }
  784. idx++;
  785. blk->fifo_available--;
  786. }
  787. if (qup->pos == msg->len)
  788. blk->rx_bytes_read = true;
  789. }
  790. static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup)
  791. {
  792. struct i2c_msg *msg = qup->msg;
  793. u32 addr, len, val;
  794. addr = i2c_8bit_addr_from_msg(msg);
  795. /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
  796. len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
  797. val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
  798. writel(val, qup->base + QUP_OUT_FIFO_BASE);
  799. }
  800. static void qup_i2c_conf_v1(struct qup_i2c_dev *qup)
  801. {
  802. struct qup_i2c_block *blk = &qup->blk;
  803. u32 qup_config = I2C_MINI_CORE | I2C_N_VAL;
  804. u32 io_mode = QUP_REPACK_EN;
  805. blk->is_tx_blk_mode =
  806. blk->total_tx_len > qup->out_fifo_sz ? true : false;
  807. blk->is_rx_blk_mode =
  808. blk->total_rx_len > qup->in_fifo_sz ? true : false;
  809. if (blk->is_tx_blk_mode) {
  810. io_mode |= QUP_OUTPUT_BLK_MODE;
  811. writel(0, qup->base + QUP_MX_WRITE_CNT);
  812. writel(blk->total_tx_len, qup->base + QUP_MX_OUTPUT_CNT);
  813. } else {
  814. writel(0, qup->base + QUP_MX_OUTPUT_CNT);
  815. writel(blk->total_tx_len, qup->base + QUP_MX_WRITE_CNT);
  816. }
  817. if (blk->total_rx_len) {
  818. if (blk->is_rx_blk_mode) {
  819. io_mode |= QUP_INPUT_BLK_MODE;
  820. writel(0, qup->base + QUP_MX_READ_CNT);
  821. writel(blk->total_rx_len, qup->base + QUP_MX_INPUT_CNT);
  822. } else {
  823. writel(0, qup->base + QUP_MX_INPUT_CNT);
  824. writel(blk->total_rx_len, qup->base + QUP_MX_READ_CNT);
  825. }
  826. } else {
  827. qup_config |= QUP_NO_INPUT;
  828. }
  829. writel(qup_config, qup->base + QUP_CONFIG);
  830. writel(io_mode, qup->base + QUP_IO_MODE);
  831. }
  832. static void qup_i2c_clear_blk_v1(struct qup_i2c_block *blk)
  833. {
  834. blk->tx_fifo_free = 0;
  835. blk->fifo_available = 0;
  836. blk->rx_bytes_read = false;
  837. }
  838. static int qup_i2c_conf_xfer_v1(struct qup_i2c_dev *qup, bool is_rx)
  839. {
  840. struct qup_i2c_block *blk = &qup->blk;
  841. int ret;
  842. qup_i2c_clear_blk_v1(blk);
  843. qup_i2c_conf_v1(qup);
  844. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  845. if (ret)
  846. return ret;
  847. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  848. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  849. if (ret)
  850. return ret;
  851. reinit_completion(&qup->xfer);
  852. enable_irq(qup->irq);
  853. if (!blk->is_tx_blk_mode) {
  854. blk->tx_fifo_free = qup->out_fifo_sz;
  855. if (is_rx)
  856. qup_i2c_write_rx_tags_v1(qup);
  857. else
  858. qup_i2c_write_tx_fifo_v1(qup);
  859. }
  860. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  861. if (ret)
  862. goto err;
  863. ret = qup_i2c_wait_for_complete(qup, qup->msg);
  864. if (ret)
  865. goto err;
  866. ret = qup_i2c_bus_active(qup, ONE_BYTE);
  867. err:
  868. disable_irq(qup->irq);
  869. return ret;
  870. }
  871. static int qup_i2c_write_one(struct qup_i2c_dev *qup)
  872. {
  873. struct i2c_msg *msg = qup->msg;
  874. struct qup_i2c_block *blk = &qup->blk;
  875. qup->pos = 0;
  876. blk->total_tx_len = msg->len + 1;
  877. blk->total_rx_len = 0;
  878. return qup_i2c_conf_xfer_v1(qup, false);
  879. }
  880. static int qup_i2c_read_one(struct qup_i2c_dev *qup)
  881. {
  882. struct qup_i2c_block *blk = &qup->blk;
  883. qup->pos = 0;
  884. blk->total_tx_len = 2;
  885. blk->total_rx_len = qup->msg->len;
  886. return qup_i2c_conf_xfer_v1(qup, true);
  887. }
  888. static int qup_i2c_xfer(struct i2c_adapter *adap,
  889. struct i2c_msg msgs[],
  890. int num)
  891. {
  892. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  893. int ret, idx;
  894. ret = pm_runtime_get_sync(qup->dev);
  895. if (ret < 0)
  896. goto out;
  897. qup->bus_err = 0;
  898. qup->qup_err = 0;
  899. writel(1, qup->base + QUP_SW_RESET);
  900. ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
  901. if (ret)
  902. goto out;
  903. /* Configure QUP as I2C mini core */
  904. writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
  905. for (idx = 0; idx < num; idx++) {
  906. if (qup_i2c_poll_state_i2c_master(qup)) {
  907. ret = -EIO;
  908. goto out;
  909. }
  910. if (qup_i2c_check_msg_len(&msgs[idx])) {
  911. ret = -EINVAL;
  912. goto out;
  913. }
  914. qup->msg = &msgs[idx];
  915. if (msgs[idx].flags & I2C_M_RD)
  916. ret = qup_i2c_read_one(qup);
  917. else
  918. ret = qup_i2c_write_one(qup);
  919. if (ret)
  920. break;
  921. ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
  922. if (ret)
  923. break;
  924. }
  925. if (ret == 0)
  926. ret = num;
  927. out:
  928. pm_runtime_mark_last_busy(qup->dev);
  929. pm_runtime_put_autosuspend(qup->dev);
  930. return ret;
  931. }
  932. /*
  933. * Configure registers related with reconfiguration during run and call it
  934. * before each i2c sub transfer.
  935. */
  936. static void qup_i2c_conf_count_v2(struct qup_i2c_dev *qup)
  937. {
  938. struct qup_i2c_block *blk = &qup->blk;
  939. u32 qup_config = I2C_MINI_CORE | I2C_N_VAL_V2;
  940. if (blk->is_tx_blk_mode)
  941. writel(qup->config_run | blk->total_tx_len,
  942. qup->base + QUP_MX_OUTPUT_CNT);
  943. else
  944. writel(qup->config_run | blk->total_tx_len,
  945. qup->base + QUP_MX_WRITE_CNT);
  946. if (blk->total_rx_len) {
  947. if (blk->is_rx_blk_mode)
  948. writel(qup->config_run | blk->total_rx_len,
  949. qup->base + QUP_MX_INPUT_CNT);
  950. else
  951. writel(qup->config_run | blk->total_rx_len,
  952. qup->base + QUP_MX_READ_CNT);
  953. } else {
  954. qup_config |= QUP_NO_INPUT;
  955. }
  956. writel(qup_config, qup->base + QUP_CONFIG);
  957. }
  958. /*
  959. * Configure registers related with transfer mode (FIFO/Block)
  960. * before starting of i2c transfer. It will be called only once in
  961. * QUP RESET state.
  962. */
  963. static void qup_i2c_conf_mode_v2(struct qup_i2c_dev *qup)
  964. {
  965. struct qup_i2c_block *blk = &qup->blk;
  966. u32 io_mode = QUP_REPACK_EN;
  967. if (blk->is_tx_blk_mode) {
  968. io_mode |= QUP_OUTPUT_BLK_MODE;
  969. writel(0, qup->base + QUP_MX_WRITE_CNT);
  970. } else {
  971. writel(0, qup->base + QUP_MX_OUTPUT_CNT);
  972. }
  973. if (blk->is_rx_blk_mode) {
  974. io_mode |= QUP_INPUT_BLK_MODE;
  975. writel(0, qup->base + QUP_MX_READ_CNT);
  976. } else {
  977. writel(0, qup->base + QUP_MX_INPUT_CNT);
  978. }
  979. writel(io_mode, qup->base + QUP_IO_MODE);
  980. }
  981. /* Clear required variables before starting of any QUP v2 sub transfer. */
  982. static void qup_i2c_clear_blk_v2(struct qup_i2c_block *blk)
  983. {
  984. blk->send_last_word = false;
  985. blk->tx_tags_sent = false;
  986. blk->tx_fifo_data = 0;
  987. blk->tx_fifo_data_pos = 0;
  988. blk->tx_fifo_free = 0;
  989. blk->rx_tags_fetched = false;
  990. blk->rx_bytes_read = false;
  991. blk->rx_fifo_data = 0;
  992. blk->rx_fifo_data_pos = 0;
  993. blk->fifo_available = 0;
  994. }
  995. /* Receive data from RX FIFO for read message in QUP v2 i2c transfer. */
  996. static void qup_i2c_recv_data(struct qup_i2c_dev *qup)
  997. {
  998. struct qup_i2c_block *blk = &qup->blk;
  999. int j;
  1000. for (j = blk->rx_fifo_data_pos;
  1001. blk->cur_blk_len && blk->fifo_available;
  1002. blk->cur_blk_len--, blk->fifo_available--) {
  1003. if (j == 0)
  1004. blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
  1005. *(blk->cur_data++) = blk->rx_fifo_data;
  1006. blk->rx_fifo_data >>= 8;
  1007. if (j == 3)
  1008. j = 0;
  1009. else
  1010. j++;
  1011. }
  1012. blk->rx_fifo_data_pos = j;
  1013. }
  1014. /* Receive tags for read message in QUP v2 i2c transfer. */
  1015. static void qup_i2c_recv_tags(struct qup_i2c_dev *qup)
  1016. {
  1017. struct qup_i2c_block *blk = &qup->blk;
  1018. blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
  1019. blk->rx_fifo_data >>= blk->rx_tag_len * 8;
  1020. blk->rx_fifo_data_pos = blk->rx_tag_len;
  1021. blk->fifo_available -= blk->rx_tag_len;
  1022. }
  1023. /*
  1024. * Read the data and tags from RX FIFO. Since in read case, the tags will be
  1025. * preceded by received data bytes so
  1026. * 1. Check if rx_tags_fetched is false i.e. the start of QUP block so receive
  1027. * all tag bytes and discard that.
  1028. * 2. Read the data from RX FIFO. When all the data bytes have been read then
  1029. * set rx_bytes_read to true.
  1030. */
  1031. static void qup_i2c_read_rx_fifo_v2(struct qup_i2c_dev *qup)
  1032. {
  1033. struct qup_i2c_block *blk = &qup->blk;
  1034. if (!blk->rx_tags_fetched) {
  1035. qup_i2c_recv_tags(qup);
  1036. blk->rx_tags_fetched = true;
  1037. }
  1038. qup_i2c_recv_data(qup);
  1039. if (!blk->cur_blk_len)
  1040. blk->rx_bytes_read = true;
  1041. }
  1042. /*
  1043. * Write bytes in TX FIFO for write message in QUP v2 i2c transfer. QUP TX FIFO
  1044. * write works on word basis (4 bytes). Append new data byte write for TX FIFO
  1045. * in tx_fifo_data and write to TX FIFO when all the 4 bytes are present.
  1046. */
  1047. static void
  1048. qup_i2c_write_blk_data(struct qup_i2c_dev *qup, u8 **data, unsigned int *len)
  1049. {
  1050. struct qup_i2c_block *blk = &qup->blk;
  1051. unsigned int j;
  1052. for (j = blk->tx_fifo_data_pos; *len && blk->tx_fifo_free;
  1053. (*len)--, blk->tx_fifo_free--) {
  1054. blk->tx_fifo_data |= *(*data)++ << (j * 8);
  1055. if (j == 3) {
  1056. writel(blk->tx_fifo_data,
  1057. qup->base + QUP_OUT_FIFO_BASE);
  1058. blk->tx_fifo_data = 0x0;
  1059. j = 0;
  1060. } else {
  1061. j++;
  1062. }
  1063. }
  1064. blk->tx_fifo_data_pos = j;
  1065. }
  1066. /* Transfer tags for read message in QUP v2 i2c transfer. */
  1067. static void qup_i2c_write_rx_tags_v2(struct qup_i2c_dev *qup)
  1068. {
  1069. struct qup_i2c_block *blk = &qup->blk;
  1070. qup_i2c_write_blk_data(qup, &blk->cur_tx_tags, &blk->tx_tag_len);
  1071. if (blk->tx_fifo_data_pos)
  1072. writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
  1073. }
  1074. /*
  1075. * Write the data and tags in TX FIFO. Since in write case, both tags and data
  1076. * need to be written and QUP write tags can have maximum 256 data length, so
  1077. *
  1078. * 1. Check if tx_tags_sent is false i.e. the start of QUP block so write the
  1079. * tags to TX FIFO and set tx_tags_sent to true.
  1080. * 2. Check if send_last_word is true. It will be set when last few data bytes
  1081. * (less than 4 bytes) are reamining to be written in FIFO because of no FIFO
  1082. * space. All this data bytes are available in tx_fifo_data so write this
  1083. * in FIFO.
  1084. * 3. Write the data to TX FIFO and check for cur_blk_len. If it is non zero
  1085. * then more data is pending otherwise following 3 cases can be possible
  1086. * a. if tx_fifo_data_pos is zero i.e. all the data bytes in this block
  1087. * have been written in TX FIFO so nothing else is required.
  1088. * b. tx_fifo_free is non zero i.e tx FIFO is free so copy the remaining data
  1089. * from tx_fifo_data to tx FIFO. Since, qup_i2c_write_blk_data do write
  1090. * in 4 bytes and FIFO space is in multiple of 4 bytes so tx_fifo_free
  1091. * will be always greater than or equal to 4 bytes.
  1092. * c. tx_fifo_free is zero. In this case, last few bytes (less than 4
  1093. * bytes) are copied to tx_fifo_data but couldn't be sent because of
  1094. * FIFO full so make send_last_word true.
  1095. */
  1096. static void qup_i2c_write_tx_fifo_v2(struct qup_i2c_dev *qup)
  1097. {
  1098. struct qup_i2c_block *blk = &qup->blk;
  1099. if (!blk->tx_tags_sent) {
  1100. qup_i2c_write_blk_data(qup, &blk->cur_tx_tags,
  1101. &blk->tx_tag_len);
  1102. blk->tx_tags_sent = true;
  1103. }
  1104. if (blk->send_last_word)
  1105. goto send_last_word;
  1106. qup_i2c_write_blk_data(qup, &blk->cur_data, &blk->cur_blk_len);
  1107. if (!blk->cur_blk_len) {
  1108. if (!blk->tx_fifo_data_pos)
  1109. return;
  1110. if (blk->tx_fifo_free)
  1111. goto send_last_word;
  1112. blk->send_last_word = true;
  1113. }
  1114. return;
  1115. send_last_word:
  1116. writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
  1117. }
  1118. /*
  1119. * Main transfer function which read or write i2c data.
  1120. * The QUP v2 supports reconfiguration during run in which multiple i2c sub
  1121. * transfers can be scheduled.
  1122. */
  1123. static int
  1124. qup_i2c_conf_xfer_v2(struct qup_i2c_dev *qup, bool is_rx, bool is_first,
  1125. bool change_pause_state)
  1126. {
  1127. struct qup_i2c_block *blk = &qup->blk;
  1128. struct i2c_msg *msg = qup->msg;
  1129. int ret;
  1130. /*
  1131. * Check if its SMBus Block read for which the top level read will be
  1132. * done into 2 QUP reads. One with message length 1 while other one is
  1133. * with actual length.
  1134. */
  1135. if (qup_i2c_check_msg_len(msg)) {
  1136. if (qup->is_smbus_read) {
  1137. /*
  1138. * If the message length is already read in
  1139. * the first byte of the buffer, account for
  1140. * that by setting the offset
  1141. */
  1142. blk->cur_data += 1;
  1143. is_first = false;
  1144. } else {
  1145. change_pause_state = false;
  1146. }
  1147. }
  1148. qup->config_run = is_first ? 0 : QUP_I2C_MX_CONFIG_DURING_RUN;
  1149. qup_i2c_clear_blk_v2(blk);
  1150. qup_i2c_conf_count_v2(qup);
  1151. /* If it is first sub transfer, then configure i2c bus clocks */
  1152. if (is_first) {
  1153. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  1154. if (ret)
  1155. return ret;
  1156. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  1157. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  1158. if (ret)
  1159. return ret;
  1160. }
  1161. reinit_completion(&qup->xfer);
  1162. enable_irq(qup->irq);
  1163. /*
  1164. * In FIFO mode, tx FIFO can be written directly while in block mode the
  1165. * it will be written after getting OUT_BLOCK_WRITE_REQ interrupt
  1166. */
  1167. if (!blk->is_tx_blk_mode) {
  1168. blk->tx_fifo_free = qup->out_fifo_sz;
  1169. if (is_rx)
  1170. qup_i2c_write_rx_tags_v2(qup);
  1171. else
  1172. qup_i2c_write_tx_fifo_v2(qup);
  1173. }
  1174. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  1175. if (ret)
  1176. goto err;
  1177. ret = qup_i2c_wait_for_complete(qup, msg);
  1178. if (ret)
  1179. goto err;
  1180. /* Move to pause state for all the transfers, except last one */
  1181. if (change_pause_state) {
  1182. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  1183. if (ret)
  1184. goto err;
  1185. }
  1186. err:
  1187. disable_irq(qup->irq);
  1188. return ret;
  1189. }
  1190. /*
  1191. * Transfer one read/write message in i2c transfer. It splits the message into
  1192. * multiple of blk_xfer_limit data length blocks and schedule each
  1193. * QUP block individually.
  1194. */
  1195. static int qup_i2c_xfer_v2_msg(struct qup_i2c_dev *qup, int msg_id, bool is_rx)
  1196. {
  1197. int ret = 0;
  1198. unsigned int data_len, i;
  1199. struct i2c_msg *msg = qup->msg;
  1200. struct qup_i2c_block *blk = &qup->blk;
  1201. u8 *msg_buf = msg->buf;
  1202. qup->blk_xfer_limit = is_rx ? RECV_MAX_DATA_LEN : QUP_READ_LIMIT;
  1203. qup_i2c_set_blk_data(qup, msg);
  1204. for (i = 0; i < blk->count; i++) {
  1205. data_len = qup_i2c_get_data_len(qup);
  1206. blk->pos = i;
  1207. blk->cur_tx_tags = blk->tags;
  1208. blk->cur_blk_len = data_len;
  1209. blk->tx_tag_len =
  1210. qup_i2c_set_tags(blk->cur_tx_tags, qup, qup->msg);
  1211. blk->cur_data = msg_buf;
  1212. if (is_rx) {
  1213. blk->total_tx_len = blk->tx_tag_len;
  1214. blk->rx_tag_len = 2;
  1215. blk->total_rx_len = blk->rx_tag_len + data_len;
  1216. } else {
  1217. blk->total_tx_len = blk->tx_tag_len + data_len;
  1218. blk->total_rx_len = 0;
  1219. }
  1220. ret = qup_i2c_conf_xfer_v2(qup, is_rx, !msg_id && !i,
  1221. !qup->is_last || i < blk->count - 1);
  1222. if (ret)
  1223. return ret;
  1224. /* Handle SMBus block read length */
  1225. if (qup_i2c_check_msg_len(msg) && msg->len == 1 &&
  1226. !qup->is_smbus_read) {
  1227. if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX)
  1228. return -EPROTO;
  1229. msg->len = msg->buf[0];
  1230. qup->is_smbus_read = true;
  1231. ret = qup_i2c_xfer_v2_msg(qup, msg_id, true);
  1232. qup->is_smbus_read = false;
  1233. if (ret)
  1234. return ret;
  1235. msg->len += 1;
  1236. }
  1237. msg_buf += data_len;
  1238. blk->data_len -= qup->blk_xfer_limit;
  1239. }
  1240. return ret;
  1241. }
  1242. /*
  1243. * QUP v2 supports 3 modes
  1244. * Programmed IO using FIFO mode : Less than FIFO size
  1245. * Programmed IO using Block mode : Greater than FIFO size
  1246. * DMA using BAM : Appropriate for any transaction size but the address should
  1247. * be DMA applicable
  1248. *
  1249. * This function determines the mode which will be used for this transfer. An
  1250. * i2c transfer contains multiple message. Following are the rules to determine
  1251. * the mode used.
  1252. * 1. Determine complete length, maximum tx and rx length for complete transfer.
  1253. * 2. If complete transfer length is greater than fifo size then use the DMA
  1254. * mode.
  1255. * 3. In FIFO or block mode, tx and rx can operate in different mode so check
  1256. * for maximum tx and rx length to determine mode.
  1257. */
  1258. static int
  1259. qup_i2c_determine_mode_v2(struct qup_i2c_dev *qup,
  1260. struct i2c_msg msgs[], int num)
  1261. {
  1262. int idx;
  1263. bool no_dma = false;
  1264. unsigned int max_tx_len = 0, max_rx_len = 0, total_len = 0;
  1265. /* All i2c_msgs should be transferred using either dma or cpu */
  1266. for (idx = 0; idx < num; idx++) {
  1267. if (msgs[idx].flags & I2C_M_RD)
  1268. max_rx_len = max_t(unsigned int, max_rx_len,
  1269. msgs[idx].len);
  1270. else
  1271. max_tx_len = max_t(unsigned int, max_tx_len,
  1272. msgs[idx].len);
  1273. if (is_vmalloc_addr(msgs[idx].buf))
  1274. no_dma = true;
  1275. total_len += msgs[idx].len;
  1276. }
  1277. if (!no_dma && qup->is_dma &&
  1278. (total_len > qup->out_fifo_sz || total_len > qup->in_fifo_sz)) {
  1279. qup->use_dma = true;
  1280. } else {
  1281. qup->blk.is_tx_blk_mode = max_tx_len > qup->out_fifo_sz -
  1282. QUP_MAX_TAGS_LEN ? true : false;
  1283. qup->blk.is_rx_blk_mode = max_rx_len > qup->in_fifo_sz -
  1284. READ_RX_TAGS_LEN ? true : false;
  1285. }
  1286. return 0;
  1287. }
  1288. static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
  1289. struct i2c_msg msgs[],
  1290. int num)
  1291. {
  1292. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  1293. int ret, idx = 0;
  1294. qup->bus_err = 0;
  1295. qup->qup_err = 0;
  1296. ret = pm_runtime_get_sync(qup->dev);
  1297. if (ret < 0)
  1298. goto out;
  1299. ret = qup_i2c_determine_mode_v2(qup, msgs, num);
  1300. if (ret)
  1301. goto out;
  1302. writel(1, qup->base + QUP_SW_RESET);
  1303. ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
  1304. if (ret)
  1305. goto out;
  1306. /* Configure QUP as I2C mini core */
  1307. writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
  1308. writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
  1309. if (qup_i2c_poll_state_i2c_master(qup)) {
  1310. ret = -EIO;
  1311. goto out;
  1312. }
  1313. if (qup->use_dma) {
  1314. reinit_completion(&qup->xfer);
  1315. ret = qup_i2c_bam_xfer(adap, &msgs[0], num);
  1316. qup->use_dma = false;
  1317. } else {
  1318. qup_i2c_conf_mode_v2(qup);
  1319. for (idx = 0; idx < num; idx++) {
  1320. qup->msg = &msgs[idx];
  1321. qup->is_last = idx == (num - 1);
  1322. ret = qup_i2c_xfer_v2_msg(qup, idx,
  1323. !!(msgs[idx].flags & I2C_M_RD));
  1324. if (ret)
  1325. break;
  1326. }
  1327. qup->msg = NULL;
  1328. }
  1329. if (!ret)
  1330. ret = qup_i2c_bus_active(qup, ONE_BYTE);
  1331. if (!ret)
  1332. qup_i2c_change_state(qup, QUP_RESET_STATE);
  1333. if (ret == 0)
  1334. ret = num;
  1335. out:
  1336. pm_runtime_mark_last_busy(qup->dev);
  1337. pm_runtime_put_autosuspend(qup->dev);
  1338. return ret;
  1339. }
  1340. static u32 qup_i2c_func(struct i2c_adapter *adap)
  1341. {
  1342. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  1343. }
  1344. static const struct i2c_algorithm qup_i2c_algo = {
  1345. .master_xfer = qup_i2c_xfer,
  1346. .functionality = qup_i2c_func,
  1347. };
  1348. static const struct i2c_algorithm qup_i2c_algo_v2 = {
  1349. .master_xfer = qup_i2c_xfer_v2,
  1350. .functionality = qup_i2c_func,
  1351. };
  1352. /*
  1353. * The QUP block will issue a NACK and STOP on the bus when reaching
  1354. * the end of the read, the length of the read is specified as one byte
  1355. * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
  1356. */
  1357. static const struct i2c_adapter_quirks qup_i2c_quirks = {
  1358. .flags = I2C_AQ_NO_ZERO_LEN,
  1359. .max_read_len = QUP_READ_LIMIT,
  1360. };
  1361. static const struct i2c_adapter_quirks qup_i2c_quirks_v2 = {
  1362. .flags = I2C_AQ_NO_ZERO_LEN,
  1363. };
  1364. static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
  1365. {
  1366. clk_prepare_enable(qup->clk);
  1367. clk_prepare_enable(qup->pclk);
  1368. }
  1369. static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
  1370. {
  1371. u32 config;
  1372. qup_i2c_change_state(qup, QUP_RESET_STATE);
  1373. clk_disable_unprepare(qup->clk);
  1374. config = readl(qup->base + QUP_CONFIG);
  1375. config |= QUP_CLOCK_AUTO_GATE;
  1376. writel(config, qup->base + QUP_CONFIG);
  1377. clk_disable_unprepare(qup->pclk);
  1378. }
  1379. static const struct acpi_device_id qup_i2c_acpi_match[] = {
  1380. { "QCOM8010"},
  1381. { },
  1382. };
  1383. MODULE_DEVICE_TABLE(acpi, qup_i2c_acpi_match);
  1384. static int qup_i2c_probe(struct platform_device *pdev)
  1385. {
  1386. static const int blk_sizes[] = {4, 16, 32};
  1387. struct qup_i2c_dev *qup;
  1388. unsigned long one_bit_t;
  1389. struct resource *res;
  1390. u32 io_mode, hw_ver, size;
  1391. int ret, fs_div, hs_div;
  1392. u32 src_clk_freq = DEFAULT_SRC_CLK;
  1393. u32 clk_freq = DEFAULT_CLK_FREQ;
  1394. int blocks;
  1395. bool is_qup_v1;
  1396. qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
  1397. if (!qup)
  1398. return -ENOMEM;
  1399. qup->dev = &pdev->dev;
  1400. init_completion(&qup->xfer);
  1401. platform_set_drvdata(pdev, qup);
  1402. if (scl_freq) {
  1403. dev_notice(qup->dev, "Using override frequency of %u\n", scl_freq);
  1404. clk_freq = scl_freq;
  1405. } else {
  1406. ret = device_property_read_u32(qup->dev, "clock-frequency", &clk_freq);
  1407. if (ret) {
  1408. dev_notice(qup->dev, "using default clock-frequency %d",
  1409. DEFAULT_CLK_FREQ);
  1410. }
  1411. }
  1412. if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
  1413. qup->adap.algo = &qup_i2c_algo;
  1414. qup->adap.quirks = &qup_i2c_quirks;
  1415. is_qup_v1 = true;
  1416. } else {
  1417. qup->adap.algo = &qup_i2c_algo_v2;
  1418. qup->adap.quirks = &qup_i2c_quirks_v2;
  1419. is_qup_v1 = false;
  1420. if (acpi_match_device(qup_i2c_acpi_match, qup->dev))
  1421. goto nodma;
  1422. else
  1423. ret = qup_i2c_req_dma(qup);
  1424. if (ret == -EPROBE_DEFER)
  1425. goto fail_dma;
  1426. else if (ret != 0)
  1427. goto nodma;
  1428. qup->max_xfer_sg_len = (MX_BLOCKS << 1);
  1429. blocks = (MX_DMA_BLOCKS << 1) + 1;
  1430. qup->btx.sg = devm_kcalloc(&pdev->dev,
  1431. blocks, sizeof(*qup->btx.sg),
  1432. GFP_KERNEL);
  1433. if (!qup->btx.sg) {
  1434. ret = -ENOMEM;
  1435. goto fail_dma;
  1436. }
  1437. sg_init_table(qup->btx.sg, blocks);
  1438. qup->brx.sg = devm_kcalloc(&pdev->dev,
  1439. blocks, sizeof(*qup->brx.sg),
  1440. GFP_KERNEL);
  1441. if (!qup->brx.sg) {
  1442. ret = -ENOMEM;
  1443. goto fail_dma;
  1444. }
  1445. sg_init_table(qup->brx.sg, blocks);
  1446. /* 2 tag bytes for each block + 5 for start, stop tags */
  1447. size = blocks * 2 + 5;
  1448. qup->start_tag.start = devm_kzalloc(&pdev->dev,
  1449. size, GFP_KERNEL);
  1450. if (!qup->start_tag.start) {
  1451. ret = -ENOMEM;
  1452. goto fail_dma;
  1453. }
  1454. qup->brx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
  1455. if (!qup->brx.tag.start) {
  1456. ret = -ENOMEM;
  1457. goto fail_dma;
  1458. }
  1459. qup->btx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
  1460. if (!qup->btx.tag.start) {
  1461. ret = -ENOMEM;
  1462. goto fail_dma;
  1463. }
  1464. qup->is_dma = true;
  1465. }
  1466. nodma:
  1467. /* We support frequencies up to FAST Mode Plus (1MHz) */
  1468. if (!clk_freq || clk_freq > I2C_FAST_MODE_PLUS_FREQ) {
  1469. dev_err(qup->dev, "clock frequency not supported %d\n",
  1470. clk_freq);
  1471. return -EINVAL;
  1472. }
  1473. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1474. qup->base = devm_ioremap_resource(qup->dev, res);
  1475. if (IS_ERR(qup->base))
  1476. return PTR_ERR(qup->base);
  1477. qup->irq = platform_get_irq(pdev, 0);
  1478. if (qup->irq < 0) {
  1479. dev_err(qup->dev, "No IRQ defined\n");
  1480. return qup->irq;
  1481. }
  1482. if (has_acpi_companion(qup->dev)) {
  1483. ret = device_property_read_u32(qup->dev,
  1484. "src-clock-hz", &src_clk_freq);
  1485. if (ret) {
  1486. dev_notice(qup->dev, "using default src-clock-hz %d",
  1487. DEFAULT_SRC_CLK);
  1488. }
  1489. ACPI_COMPANION_SET(&qup->adap.dev, ACPI_COMPANION(qup->dev));
  1490. } else {
  1491. qup->clk = devm_clk_get(qup->dev, "core");
  1492. if (IS_ERR(qup->clk)) {
  1493. dev_err(qup->dev, "Could not get core clock\n");
  1494. return PTR_ERR(qup->clk);
  1495. }
  1496. qup->pclk = devm_clk_get(qup->dev, "iface");
  1497. if (IS_ERR(qup->pclk)) {
  1498. dev_err(qup->dev, "Could not get iface clock\n");
  1499. return PTR_ERR(qup->pclk);
  1500. }
  1501. qup_i2c_enable_clocks(qup);
  1502. src_clk_freq = clk_get_rate(qup->clk);
  1503. }
  1504. /*
  1505. * Bootloaders might leave a pending interrupt on certain QUP's,
  1506. * so we reset the core before registering for interrupts.
  1507. */
  1508. writel(1, qup->base + QUP_SW_RESET);
  1509. ret = qup_i2c_poll_state_valid(qup);
  1510. if (ret)
  1511. goto fail;
  1512. ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
  1513. IRQF_TRIGGER_HIGH, "i2c_qup", qup);
  1514. if (ret) {
  1515. dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
  1516. goto fail;
  1517. }
  1518. disable_irq(qup->irq);
  1519. hw_ver = readl(qup->base + QUP_HW_VERSION);
  1520. dev_dbg(qup->dev, "Revision %x\n", hw_ver);
  1521. io_mode = readl(qup->base + QUP_IO_MODE);
  1522. /*
  1523. * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
  1524. * associated with each byte written/received
  1525. */
  1526. size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
  1527. if (size >= ARRAY_SIZE(blk_sizes)) {
  1528. ret = -EIO;
  1529. goto fail;
  1530. }
  1531. qup->out_blk_sz = blk_sizes[size];
  1532. size = QUP_INPUT_BLOCK_SIZE(io_mode);
  1533. if (size >= ARRAY_SIZE(blk_sizes)) {
  1534. ret = -EIO;
  1535. goto fail;
  1536. }
  1537. qup->in_blk_sz = blk_sizes[size];
  1538. if (is_qup_v1) {
  1539. /*
  1540. * in QUP v1, QUP_CONFIG uses N as 15 i.e 16 bits constitutes a
  1541. * single transfer but the block size is in bytes so divide the
  1542. * in_blk_sz and out_blk_sz by 2
  1543. */
  1544. qup->in_blk_sz /= 2;
  1545. qup->out_blk_sz /= 2;
  1546. qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1;
  1547. qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1;
  1548. qup->write_rx_tags = qup_i2c_write_rx_tags_v1;
  1549. } else {
  1550. qup->write_tx_fifo = qup_i2c_write_tx_fifo_v2;
  1551. qup->read_rx_fifo = qup_i2c_read_rx_fifo_v2;
  1552. qup->write_rx_tags = qup_i2c_write_rx_tags_v2;
  1553. }
  1554. size = QUP_OUTPUT_FIFO_SIZE(io_mode);
  1555. qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
  1556. size = QUP_INPUT_FIFO_SIZE(io_mode);
  1557. qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
  1558. hs_div = 3;
  1559. if (clk_freq <= I2C_STANDARD_FREQ) {
  1560. fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
  1561. qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
  1562. } else {
  1563. /* 33%/66% duty cycle */
  1564. fs_div = ((src_clk_freq / clk_freq) - 6) * 2 / 3;
  1565. qup->clk_ctl = ((fs_div / 2) << 16) | (hs_div << 8) | (fs_div & 0xff);
  1566. }
  1567. /*
  1568. * Time it takes for a byte to be clocked out on the bus.
  1569. * Each byte takes 9 clock cycles (8 bits + 1 ack).
  1570. */
  1571. one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
  1572. qup->one_byte_t = one_bit_t * 9;
  1573. qup->xfer_timeout = TOUT_MIN * HZ +
  1574. usecs_to_jiffies(MX_DMA_TX_RX_LEN * qup->one_byte_t);
  1575. dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
  1576. qup->in_blk_sz, qup->in_fifo_sz,
  1577. qup->out_blk_sz, qup->out_fifo_sz);
  1578. i2c_set_adapdata(&qup->adap, qup);
  1579. qup->adap.dev.parent = qup->dev;
  1580. qup->adap.dev.of_node = pdev->dev.of_node;
  1581. qup->is_last = true;
  1582. strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
  1583. pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
  1584. pm_runtime_use_autosuspend(qup->dev);
  1585. pm_runtime_set_active(qup->dev);
  1586. pm_runtime_enable(qup->dev);
  1587. ret = i2c_add_adapter(&qup->adap);
  1588. if (ret)
  1589. goto fail_runtime;
  1590. return 0;
  1591. fail_runtime:
  1592. pm_runtime_disable(qup->dev);
  1593. pm_runtime_set_suspended(qup->dev);
  1594. fail:
  1595. qup_i2c_disable_clocks(qup);
  1596. fail_dma:
  1597. if (qup->btx.dma)
  1598. dma_release_channel(qup->btx.dma);
  1599. if (qup->brx.dma)
  1600. dma_release_channel(qup->brx.dma);
  1601. return ret;
  1602. }
  1603. static int qup_i2c_remove(struct platform_device *pdev)
  1604. {
  1605. struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
  1606. if (qup->is_dma) {
  1607. dma_release_channel(qup->btx.dma);
  1608. dma_release_channel(qup->brx.dma);
  1609. }
  1610. disable_irq(qup->irq);
  1611. qup_i2c_disable_clocks(qup);
  1612. i2c_del_adapter(&qup->adap);
  1613. pm_runtime_disable(qup->dev);
  1614. pm_runtime_set_suspended(qup->dev);
  1615. return 0;
  1616. }
  1617. #ifdef CONFIG_PM
  1618. static int qup_i2c_pm_suspend_runtime(struct device *device)
  1619. {
  1620. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  1621. dev_dbg(device, "pm_runtime: suspending...\n");
  1622. qup_i2c_disable_clocks(qup);
  1623. return 0;
  1624. }
  1625. static int qup_i2c_pm_resume_runtime(struct device *device)
  1626. {
  1627. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  1628. dev_dbg(device, "pm_runtime: resuming...\n");
  1629. qup_i2c_enable_clocks(qup);
  1630. return 0;
  1631. }
  1632. #endif
  1633. #ifdef CONFIG_PM_SLEEP
  1634. static int qup_i2c_suspend(struct device *device)
  1635. {
  1636. if (!pm_runtime_suspended(device))
  1637. return qup_i2c_pm_suspend_runtime(device);
  1638. return 0;
  1639. }
  1640. static int qup_i2c_resume(struct device *device)
  1641. {
  1642. qup_i2c_pm_resume_runtime(device);
  1643. pm_runtime_mark_last_busy(device);
  1644. pm_request_autosuspend(device);
  1645. return 0;
  1646. }
  1647. #endif
  1648. static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
  1649. SET_SYSTEM_SLEEP_PM_OPS(
  1650. qup_i2c_suspend,
  1651. qup_i2c_resume)
  1652. SET_RUNTIME_PM_OPS(
  1653. qup_i2c_pm_suspend_runtime,
  1654. qup_i2c_pm_resume_runtime,
  1655. NULL)
  1656. };
  1657. static const struct of_device_id qup_i2c_dt_match[] = {
  1658. { .compatible = "qcom,i2c-qup-v1.1.1" },
  1659. { .compatible = "qcom,i2c-qup-v2.1.1" },
  1660. { .compatible = "qcom,i2c-qup-v2.2.1" },
  1661. {}
  1662. };
  1663. MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
  1664. static struct platform_driver qup_i2c_driver = {
  1665. .probe = qup_i2c_probe,
  1666. .remove = qup_i2c_remove,
  1667. .driver = {
  1668. .name = "i2c_qup",
  1669. .pm = &qup_i2c_qup_pm_ops,
  1670. .of_match_table = qup_i2c_dt_match,
  1671. .acpi_match_table = ACPI_PTR(qup_i2c_acpi_match),
  1672. },
  1673. };
  1674. module_platform_driver(qup_i2c_driver);
  1675. MODULE_LICENSE("GPL v2");
  1676. MODULE_ALIAS("platform:i2c_qup");