i2c-qup.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  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. if (qup_i2c_change_state(qup, QUP_RUN_STATE)) {
  684. dev_err(qup->dev, "change to run state timed out");
  685. goto desc_err;
  686. }
  687. qup_i2c_flush(qup);
  688. /* wait for remaining interrupts to occur */
  689. if (!wait_for_completion_timeout(&qup->xfer, HZ))
  690. dev_err(qup->dev, "flush timed out\n");
  691. ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
  692. }
  693. desc_err:
  694. dma_unmap_sg(qup->dev, qup->btx.sg, tx_cnt, DMA_TO_DEVICE);
  695. if (rx_cnt)
  696. dma_unmap_sg(qup->dev, qup->brx.sg, rx_cnt,
  697. DMA_FROM_DEVICE);
  698. return ret;
  699. }
  700. static void qup_i2c_bam_clear_tag_buffers(struct qup_i2c_dev *qup)
  701. {
  702. qup->btx.sg_cnt = 0;
  703. qup->brx.sg_cnt = 0;
  704. qup->tag_buf_pos = 0;
  705. }
  706. static int qup_i2c_bam_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  707. int num)
  708. {
  709. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  710. int ret = 0;
  711. int idx = 0;
  712. enable_irq(qup->irq);
  713. ret = qup_i2c_req_dma(qup);
  714. if (ret)
  715. goto out;
  716. writel(0, qup->base + QUP_MX_INPUT_CNT);
  717. writel(0, qup->base + QUP_MX_OUTPUT_CNT);
  718. /* set BAM mode */
  719. writel(QUP_REPACK_EN | QUP_BAM_MODE, qup->base + QUP_IO_MODE);
  720. /* mask fifo irqs */
  721. writel((0x3 << 8), qup->base + QUP_OPERATIONAL_MASK);
  722. /* set RUN STATE */
  723. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  724. if (ret)
  725. goto out;
  726. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  727. qup_i2c_bam_clear_tag_buffers(qup);
  728. for (idx = 0; idx < num; idx++) {
  729. qup->msg = msg + idx;
  730. qup->is_last = idx == (num - 1);
  731. ret = qup_i2c_bam_make_desc(qup, qup->msg);
  732. if (ret)
  733. break;
  734. /*
  735. * Make DMA descriptor and schedule the BAM transfer if its
  736. * already crossed the maximum length. Since the memory for all
  737. * tags buffers have been taken for 2 maximum possible
  738. * transfers length so it will never cross the buffer actual
  739. * length.
  740. */
  741. if (qup->btx.sg_cnt > qup->max_xfer_sg_len ||
  742. qup->brx.sg_cnt > qup->max_xfer_sg_len ||
  743. qup->is_last) {
  744. ret = qup_i2c_bam_schedule_desc(qup);
  745. if (ret)
  746. break;
  747. qup_i2c_bam_clear_tag_buffers(qup);
  748. }
  749. }
  750. out:
  751. disable_irq(qup->irq);
  752. qup->msg = NULL;
  753. return ret;
  754. }
  755. static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
  756. struct i2c_msg *msg)
  757. {
  758. unsigned long left;
  759. int ret = 0;
  760. left = wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout);
  761. if (!left) {
  762. writel(1, qup->base + QUP_SW_RESET);
  763. ret = -ETIMEDOUT;
  764. }
  765. if (qup->bus_err || qup->qup_err)
  766. ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
  767. return ret;
  768. }
  769. static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup)
  770. {
  771. struct qup_i2c_block *blk = &qup->blk;
  772. struct i2c_msg *msg = qup->msg;
  773. u32 val = 0;
  774. int idx = 0;
  775. while (blk->fifo_available && qup->pos < msg->len) {
  776. if ((idx & 1) == 0) {
  777. /* Reading 2 words at time */
  778. val = readl(qup->base + QUP_IN_FIFO_BASE);
  779. msg->buf[qup->pos++] = val & 0xFF;
  780. } else {
  781. msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
  782. }
  783. idx++;
  784. blk->fifo_available--;
  785. }
  786. if (qup->pos == msg->len)
  787. blk->rx_bytes_read = true;
  788. }
  789. static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup)
  790. {
  791. struct i2c_msg *msg = qup->msg;
  792. u32 addr, len, val;
  793. addr = i2c_8bit_addr_from_msg(msg);
  794. /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
  795. len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
  796. val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
  797. writel(val, qup->base + QUP_OUT_FIFO_BASE);
  798. }
  799. static void qup_i2c_conf_v1(struct qup_i2c_dev *qup)
  800. {
  801. struct qup_i2c_block *blk = &qup->blk;
  802. u32 qup_config = I2C_MINI_CORE | I2C_N_VAL;
  803. u32 io_mode = QUP_REPACK_EN;
  804. blk->is_tx_blk_mode =
  805. blk->total_tx_len > qup->out_fifo_sz ? true : false;
  806. blk->is_rx_blk_mode =
  807. blk->total_rx_len > qup->in_fifo_sz ? true : false;
  808. if (blk->is_tx_blk_mode) {
  809. io_mode |= QUP_OUTPUT_BLK_MODE;
  810. writel(0, qup->base + QUP_MX_WRITE_CNT);
  811. writel(blk->total_tx_len, qup->base + QUP_MX_OUTPUT_CNT);
  812. } else {
  813. writel(0, qup->base + QUP_MX_OUTPUT_CNT);
  814. writel(blk->total_tx_len, qup->base + QUP_MX_WRITE_CNT);
  815. }
  816. if (blk->total_rx_len) {
  817. if (blk->is_rx_blk_mode) {
  818. io_mode |= QUP_INPUT_BLK_MODE;
  819. writel(0, qup->base + QUP_MX_READ_CNT);
  820. writel(blk->total_rx_len, qup->base + QUP_MX_INPUT_CNT);
  821. } else {
  822. writel(0, qup->base + QUP_MX_INPUT_CNT);
  823. writel(blk->total_rx_len, qup->base + QUP_MX_READ_CNT);
  824. }
  825. } else {
  826. qup_config |= QUP_NO_INPUT;
  827. }
  828. writel(qup_config, qup->base + QUP_CONFIG);
  829. writel(io_mode, qup->base + QUP_IO_MODE);
  830. }
  831. static void qup_i2c_clear_blk_v1(struct qup_i2c_block *blk)
  832. {
  833. blk->tx_fifo_free = 0;
  834. blk->fifo_available = 0;
  835. blk->rx_bytes_read = false;
  836. }
  837. static int qup_i2c_conf_xfer_v1(struct qup_i2c_dev *qup, bool is_rx)
  838. {
  839. struct qup_i2c_block *blk = &qup->blk;
  840. int ret;
  841. qup_i2c_clear_blk_v1(blk);
  842. qup_i2c_conf_v1(qup);
  843. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  844. if (ret)
  845. return ret;
  846. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  847. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  848. if (ret)
  849. return ret;
  850. reinit_completion(&qup->xfer);
  851. enable_irq(qup->irq);
  852. if (!blk->is_tx_blk_mode) {
  853. blk->tx_fifo_free = qup->out_fifo_sz;
  854. if (is_rx)
  855. qup_i2c_write_rx_tags_v1(qup);
  856. else
  857. qup_i2c_write_tx_fifo_v1(qup);
  858. }
  859. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  860. if (ret)
  861. goto err;
  862. ret = qup_i2c_wait_for_complete(qup, qup->msg);
  863. if (ret)
  864. goto err;
  865. ret = qup_i2c_bus_active(qup, ONE_BYTE);
  866. err:
  867. disable_irq(qup->irq);
  868. return ret;
  869. }
  870. static int qup_i2c_write_one(struct qup_i2c_dev *qup)
  871. {
  872. struct i2c_msg *msg = qup->msg;
  873. struct qup_i2c_block *blk = &qup->blk;
  874. qup->pos = 0;
  875. blk->total_tx_len = msg->len + 1;
  876. blk->total_rx_len = 0;
  877. return qup_i2c_conf_xfer_v1(qup, false);
  878. }
  879. static int qup_i2c_read_one(struct qup_i2c_dev *qup)
  880. {
  881. struct qup_i2c_block *blk = &qup->blk;
  882. qup->pos = 0;
  883. blk->total_tx_len = 2;
  884. blk->total_rx_len = qup->msg->len;
  885. return qup_i2c_conf_xfer_v1(qup, true);
  886. }
  887. static int qup_i2c_xfer(struct i2c_adapter *adap,
  888. struct i2c_msg msgs[],
  889. int num)
  890. {
  891. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  892. int ret, idx;
  893. ret = pm_runtime_get_sync(qup->dev);
  894. if (ret < 0)
  895. goto out;
  896. qup->bus_err = 0;
  897. qup->qup_err = 0;
  898. writel(1, qup->base + QUP_SW_RESET);
  899. ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
  900. if (ret)
  901. goto out;
  902. /* Configure QUP as I2C mini core */
  903. writel(I2C_MINI_CORE | I2C_N_VAL, qup->base + QUP_CONFIG);
  904. for (idx = 0; idx < num; idx++) {
  905. if (qup_i2c_poll_state_i2c_master(qup)) {
  906. ret = -EIO;
  907. goto out;
  908. }
  909. if (qup_i2c_check_msg_len(&msgs[idx])) {
  910. ret = -EINVAL;
  911. goto out;
  912. }
  913. qup->msg = &msgs[idx];
  914. if (msgs[idx].flags & I2C_M_RD)
  915. ret = qup_i2c_read_one(qup);
  916. else
  917. ret = qup_i2c_write_one(qup);
  918. if (ret)
  919. break;
  920. ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
  921. if (ret)
  922. break;
  923. }
  924. if (ret == 0)
  925. ret = num;
  926. out:
  927. pm_runtime_mark_last_busy(qup->dev);
  928. pm_runtime_put_autosuspend(qup->dev);
  929. return ret;
  930. }
  931. /*
  932. * Configure registers related with reconfiguration during run and call it
  933. * before each i2c sub transfer.
  934. */
  935. static void qup_i2c_conf_count_v2(struct qup_i2c_dev *qup)
  936. {
  937. struct qup_i2c_block *blk = &qup->blk;
  938. u32 qup_config = I2C_MINI_CORE | I2C_N_VAL_V2;
  939. if (blk->is_tx_blk_mode)
  940. writel(qup->config_run | blk->total_tx_len,
  941. qup->base + QUP_MX_OUTPUT_CNT);
  942. else
  943. writel(qup->config_run | blk->total_tx_len,
  944. qup->base + QUP_MX_WRITE_CNT);
  945. if (blk->total_rx_len) {
  946. if (blk->is_rx_blk_mode)
  947. writel(qup->config_run | blk->total_rx_len,
  948. qup->base + QUP_MX_INPUT_CNT);
  949. else
  950. writel(qup->config_run | blk->total_rx_len,
  951. qup->base + QUP_MX_READ_CNT);
  952. } else {
  953. qup_config |= QUP_NO_INPUT;
  954. }
  955. writel(qup_config, qup->base + QUP_CONFIG);
  956. }
  957. /*
  958. * Configure registers related with transfer mode (FIFO/Block)
  959. * before starting of i2c transfer. It will be called only once in
  960. * QUP RESET state.
  961. */
  962. static void qup_i2c_conf_mode_v2(struct qup_i2c_dev *qup)
  963. {
  964. struct qup_i2c_block *blk = &qup->blk;
  965. u32 io_mode = QUP_REPACK_EN;
  966. if (blk->is_tx_blk_mode) {
  967. io_mode |= QUP_OUTPUT_BLK_MODE;
  968. writel(0, qup->base + QUP_MX_WRITE_CNT);
  969. } else {
  970. writel(0, qup->base + QUP_MX_OUTPUT_CNT);
  971. }
  972. if (blk->is_rx_blk_mode) {
  973. io_mode |= QUP_INPUT_BLK_MODE;
  974. writel(0, qup->base + QUP_MX_READ_CNT);
  975. } else {
  976. writel(0, qup->base + QUP_MX_INPUT_CNT);
  977. }
  978. writel(io_mode, qup->base + QUP_IO_MODE);
  979. }
  980. /* Clear required variables before starting of any QUP v2 sub transfer. */
  981. static void qup_i2c_clear_blk_v2(struct qup_i2c_block *blk)
  982. {
  983. blk->send_last_word = false;
  984. blk->tx_tags_sent = false;
  985. blk->tx_fifo_data = 0;
  986. blk->tx_fifo_data_pos = 0;
  987. blk->tx_fifo_free = 0;
  988. blk->rx_tags_fetched = false;
  989. blk->rx_bytes_read = false;
  990. blk->rx_fifo_data = 0;
  991. blk->rx_fifo_data_pos = 0;
  992. blk->fifo_available = 0;
  993. }
  994. /* Receive data from RX FIFO for read message in QUP v2 i2c transfer. */
  995. static void qup_i2c_recv_data(struct qup_i2c_dev *qup)
  996. {
  997. struct qup_i2c_block *blk = &qup->blk;
  998. int j;
  999. for (j = blk->rx_fifo_data_pos;
  1000. blk->cur_blk_len && blk->fifo_available;
  1001. blk->cur_blk_len--, blk->fifo_available--) {
  1002. if (j == 0)
  1003. blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
  1004. *(blk->cur_data++) = blk->rx_fifo_data;
  1005. blk->rx_fifo_data >>= 8;
  1006. if (j == 3)
  1007. j = 0;
  1008. else
  1009. j++;
  1010. }
  1011. blk->rx_fifo_data_pos = j;
  1012. }
  1013. /* Receive tags for read message in QUP v2 i2c transfer. */
  1014. static void qup_i2c_recv_tags(struct qup_i2c_dev *qup)
  1015. {
  1016. struct qup_i2c_block *blk = &qup->blk;
  1017. blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
  1018. blk->rx_fifo_data >>= blk->rx_tag_len * 8;
  1019. blk->rx_fifo_data_pos = blk->rx_tag_len;
  1020. blk->fifo_available -= blk->rx_tag_len;
  1021. }
  1022. /*
  1023. * Read the data and tags from RX FIFO. Since in read case, the tags will be
  1024. * preceded by received data bytes so
  1025. * 1. Check if rx_tags_fetched is false i.e. the start of QUP block so receive
  1026. * all tag bytes and discard that.
  1027. * 2. Read the data from RX FIFO. When all the data bytes have been read then
  1028. * set rx_bytes_read to true.
  1029. */
  1030. static void qup_i2c_read_rx_fifo_v2(struct qup_i2c_dev *qup)
  1031. {
  1032. struct qup_i2c_block *blk = &qup->blk;
  1033. if (!blk->rx_tags_fetched) {
  1034. qup_i2c_recv_tags(qup);
  1035. blk->rx_tags_fetched = true;
  1036. }
  1037. qup_i2c_recv_data(qup);
  1038. if (!blk->cur_blk_len)
  1039. blk->rx_bytes_read = true;
  1040. }
  1041. /*
  1042. * Write bytes in TX FIFO for write message in QUP v2 i2c transfer. QUP TX FIFO
  1043. * write works on word basis (4 bytes). Append new data byte write for TX FIFO
  1044. * in tx_fifo_data and write to TX FIFO when all the 4 bytes are present.
  1045. */
  1046. static void
  1047. qup_i2c_write_blk_data(struct qup_i2c_dev *qup, u8 **data, unsigned int *len)
  1048. {
  1049. struct qup_i2c_block *blk = &qup->blk;
  1050. unsigned int j;
  1051. for (j = blk->tx_fifo_data_pos; *len && blk->tx_fifo_free;
  1052. (*len)--, blk->tx_fifo_free--) {
  1053. blk->tx_fifo_data |= *(*data)++ << (j * 8);
  1054. if (j == 3) {
  1055. writel(blk->tx_fifo_data,
  1056. qup->base + QUP_OUT_FIFO_BASE);
  1057. blk->tx_fifo_data = 0x0;
  1058. j = 0;
  1059. } else {
  1060. j++;
  1061. }
  1062. }
  1063. blk->tx_fifo_data_pos = j;
  1064. }
  1065. /* Transfer tags for read message in QUP v2 i2c transfer. */
  1066. static void qup_i2c_write_rx_tags_v2(struct qup_i2c_dev *qup)
  1067. {
  1068. struct qup_i2c_block *blk = &qup->blk;
  1069. qup_i2c_write_blk_data(qup, &blk->cur_tx_tags, &blk->tx_tag_len);
  1070. if (blk->tx_fifo_data_pos)
  1071. writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
  1072. }
  1073. /*
  1074. * Write the data and tags in TX FIFO. Since in write case, both tags and data
  1075. * need to be written and QUP write tags can have maximum 256 data length, so
  1076. *
  1077. * 1. Check if tx_tags_sent is false i.e. the start of QUP block so write the
  1078. * tags to TX FIFO and set tx_tags_sent to true.
  1079. * 2. Check if send_last_word is true. It will be set when last few data bytes
  1080. * (less than 4 bytes) are reamining to be written in FIFO because of no FIFO
  1081. * space. All this data bytes are available in tx_fifo_data so write this
  1082. * in FIFO.
  1083. * 3. Write the data to TX FIFO and check for cur_blk_len. If it is non zero
  1084. * then more data is pending otherwise following 3 cases can be possible
  1085. * a. if tx_fifo_data_pos is zero i.e. all the data bytes in this block
  1086. * have been written in TX FIFO so nothing else is required.
  1087. * b. tx_fifo_free is non zero i.e tx FIFO is free so copy the remaining data
  1088. * from tx_fifo_data to tx FIFO. Since, qup_i2c_write_blk_data do write
  1089. * in 4 bytes and FIFO space is in multiple of 4 bytes so tx_fifo_free
  1090. * will be always greater than or equal to 4 bytes.
  1091. * c. tx_fifo_free is zero. In this case, last few bytes (less than 4
  1092. * bytes) are copied to tx_fifo_data but couldn't be sent because of
  1093. * FIFO full so make send_last_word true.
  1094. */
  1095. static void qup_i2c_write_tx_fifo_v2(struct qup_i2c_dev *qup)
  1096. {
  1097. struct qup_i2c_block *blk = &qup->blk;
  1098. if (!blk->tx_tags_sent) {
  1099. qup_i2c_write_blk_data(qup, &blk->cur_tx_tags,
  1100. &blk->tx_tag_len);
  1101. blk->tx_tags_sent = true;
  1102. }
  1103. if (blk->send_last_word)
  1104. goto send_last_word;
  1105. qup_i2c_write_blk_data(qup, &blk->cur_data, &blk->cur_blk_len);
  1106. if (!blk->cur_blk_len) {
  1107. if (!blk->tx_fifo_data_pos)
  1108. return;
  1109. if (blk->tx_fifo_free)
  1110. goto send_last_word;
  1111. blk->send_last_word = true;
  1112. }
  1113. return;
  1114. send_last_word:
  1115. writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
  1116. }
  1117. /*
  1118. * Main transfer function which read or write i2c data.
  1119. * The QUP v2 supports reconfiguration during run in which multiple i2c sub
  1120. * transfers can be scheduled.
  1121. */
  1122. static int
  1123. qup_i2c_conf_xfer_v2(struct qup_i2c_dev *qup, bool is_rx, bool is_first,
  1124. bool change_pause_state)
  1125. {
  1126. struct qup_i2c_block *blk = &qup->blk;
  1127. struct i2c_msg *msg = qup->msg;
  1128. int ret;
  1129. /*
  1130. * Check if its SMBus Block read for which the top level read will be
  1131. * done into 2 QUP reads. One with message length 1 while other one is
  1132. * with actual length.
  1133. */
  1134. if (qup_i2c_check_msg_len(msg)) {
  1135. if (qup->is_smbus_read) {
  1136. /*
  1137. * If the message length is already read in
  1138. * the first byte of the buffer, account for
  1139. * that by setting the offset
  1140. */
  1141. blk->cur_data += 1;
  1142. is_first = false;
  1143. } else {
  1144. change_pause_state = false;
  1145. }
  1146. }
  1147. qup->config_run = is_first ? 0 : QUP_I2C_MX_CONFIG_DURING_RUN;
  1148. qup_i2c_clear_blk_v2(blk);
  1149. qup_i2c_conf_count_v2(qup);
  1150. /* If it is first sub transfer, then configure i2c bus clocks */
  1151. if (is_first) {
  1152. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  1153. if (ret)
  1154. return ret;
  1155. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  1156. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  1157. if (ret)
  1158. return ret;
  1159. }
  1160. reinit_completion(&qup->xfer);
  1161. enable_irq(qup->irq);
  1162. /*
  1163. * In FIFO mode, tx FIFO can be written directly while in block mode the
  1164. * it will be written after getting OUT_BLOCK_WRITE_REQ interrupt
  1165. */
  1166. if (!blk->is_tx_blk_mode) {
  1167. blk->tx_fifo_free = qup->out_fifo_sz;
  1168. if (is_rx)
  1169. qup_i2c_write_rx_tags_v2(qup);
  1170. else
  1171. qup_i2c_write_tx_fifo_v2(qup);
  1172. }
  1173. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  1174. if (ret)
  1175. goto err;
  1176. ret = qup_i2c_wait_for_complete(qup, msg);
  1177. if (ret)
  1178. goto err;
  1179. /* Move to pause state for all the transfers, except last one */
  1180. if (change_pause_state) {
  1181. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  1182. if (ret)
  1183. goto err;
  1184. }
  1185. err:
  1186. disable_irq(qup->irq);
  1187. return ret;
  1188. }
  1189. /*
  1190. * Transfer one read/write message in i2c transfer. It splits the message into
  1191. * multiple of blk_xfer_limit data length blocks and schedule each
  1192. * QUP block individually.
  1193. */
  1194. static int qup_i2c_xfer_v2_msg(struct qup_i2c_dev *qup, int msg_id, bool is_rx)
  1195. {
  1196. int ret = 0;
  1197. unsigned int data_len, i;
  1198. struct i2c_msg *msg = qup->msg;
  1199. struct qup_i2c_block *blk = &qup->blk;
  1200. u8 *msg_buf = msg->buf;
  1201. qup->blk_xfer_limit = is_rx ? RECV_MAX_DATA_LEN : QUP_READ_LIMIT;
  1202. qup_i2c_set_blk_data(qup, msg);
  1203. for (i = 0; i < blk->count; i++) {
  1204. data_len = qup_i2c_get_data_len(qup);
  1205. blk->pos = i;
  1206. blk->cur_tx_tags = blk->tags;
  1207. blk->cur_blk_len = data_len;
  1208. blk->tx_tag_len =
  1209. qup_i2c_set_tags(blk->cur_tx_tags, qup, qup->msg);
  1210. blk->cur_data = msg_buf;
  1211. if (is_rx) {
  1212. blk->total_tx_len = blk->tx_tag_len;
  1213. blk->rx_tag_len = 2;
  1214. blk->total_rx_len = blk->rx_tag_len + data_len;
  1215. } else {
  1216. blk->total_tx_len = blk->tx_tag_len + data_len;
  1217. blk->total_rx_len = 0;
  1218. }
  1219. ret = qup_i2c_conf_xfer_v2(qup, is_rx, !msg_id && !i,
  1220. !qup->is_last || i < blk->count - 1);
  1221. if (ret)
  1222. return ret;
  1223. /* Handle SMBus block read length */
  1224. if (qup_i2c_check_msg_len(msg) && msg->len == 1 &&
  1225. !qup->is_smbus_read) {
  1226. if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX)
  1227. return -EPROTO;
  1228. msg->len = msg->buf[0];
  1229. qup->is_smbus_read = true;
  1230. ret = qup_i2c_xfer_v2_msg(qup, msg_id, true);
  1231. qup->is_smbus_read = false;
  1232. if (ret)
  1233. return ret;
  1234. msg->len += 1;
  1235. }
  1236. msg_buf += data_len;
  1237. blk->data_len -= qup->blk_xfer_limit;
  1238. }
  1239. return ret;
  1240. }
  1241. /*
  1242. * QUP v2 supports 3 modes
  1243. * Programmed IO using FIFO mode : Less than FIFO size
  1244. * Programmed IO using Block mode : Greater than FIFO size
  1245. * DMA using BAM : Appropriate for any transaction size but the address should
  1246. * be DMA applicable
  1247. *
  1248. * This function determines the mode which will be used for this transfer. An
  1249. * i2c transfer contains multiple message. Following are the rules to determine
  1250. * the mode used.
  1251. * 1. Determine complete length, maximum tx and rx length for complete transfer.
  1252. * 2. If complete transfer length is greater than fifo size then use the DMA
  1253. * mode.
  1254. * 3. In FIFO or block mode, tx and rx can operate in different mode so check
  1255. * for maximum tx and rx length to determine mode.
  1256. */
  1257. static int
  1258. qup_i2c_determine_mode_v2(struct qup_i2c_dev *qup,
  1259. struct i2c_msg msgs[], int num)
  1260. {
  1261. int idx;
  1262. bool no_dma = false;
  1263. unsigned int max_tx_len = 0, max_rx_len = 0, total_len = 0;
  1264. /* All i2c_msgs should be transferred using either dma or cpu */
  1265. for (idx = 0; idx < num; idx++) {
  1266. if (msgs[idx].flags & I2C_M_RD)
  1267. max_rx_len = max_t(unsigned int, max_rx_len,
  1268. msgs[idx].len);
  1269. else
  1270. max_tx_len = max_t(unsigned int, max_tx_len,
  1271. msgs[idx].len);
  1272. if (is_vmalloc_addr(msgs[idx].buf))
  1273. no_dma = true;
  1274. total_len += msgs[idx].len;
  1275. }
  1276. if (!no_dma && qup->is_dma &&
  1277. (total_len > qup->out_fifo_sz || total_len > qup->in_fifo_sz)) {
  1278. qup->use_dma = true;
  1279. } else {
  1280. qup->blk.is_tx_blk_mode = max_tx_len > qup->out_fifo_sz -
  1281. QUP_MAX_TAGS_LEN ? true : false;
  1282. qup->blk.is_rx_blk_mode = max_rx_len > qup->in_fifo_sz -
  1283. READ_RX_TAGS_LEN ? true : false;
  1284. }
  1285. return 0;
  1286. }
  1287. static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
  1288. struct i2c_msg msgs[],
  1289. int num)
  1290. {
  1291. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  1292. int ret, idx = 0;
  1293. qup->bus_err = 0;
  1294. qup->qup_err = 0;
  1295. ret = pm_runtime_get_sync(qup->dev);
  1296. if (ret < 0)
  1297. goto out;
  1298. ret = qup_i2c_determine_mode_v2(qup, msgs, num);
  1299. if (ret)
  1300. goto out;
  1301. writel(1, qup->base + QUP_SW_RESET);
  1302. ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
  1303. if (ret)
  1304. goto out;
  1305. /* Configure QUP as I2C mini core */
  1306. writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
  1307. writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
  1308. if (qup_i2c_poll_state_i2c_master(qup)) {
  1309. ret = -EIO;
  1310. goto out;
  1311. }
  1312. if (qup->use_dma) {
  1313. reinit_completion(&qup->xfer);
  1314. ret = qup_i2c_bam_xfer(adap, &msgs[0], num);
  1315. qup->use_dma = false;
  1316. } else {
  1317. qup_i2c_conf_mode_v2(qup);
  1318. for (idx = 0; idx < num; idx++) {
  1319. qup->msg = &msgs[idx];
  1320. qup->is_last = idx == (num - 1);
  1321. ret = qup_i2c_xfer_v2_msg(qup, idx,
  1322. !!(msgs[idx].flags & I2C_M_RD));
  1323. if (ret)
  1324. break;
  1325. }
  1326. qup->msg = NULL;
  1327. }
  1328. if (!ret)
  1329. ret = qup_i2c_bus_active(qup, ONE_BYTE);
  1330. if (!ret)
  1331. qup_i2c_change_state(qup, QUP_RESET_STATE);
  1332. if (ret == 0)
  1333. ret = num;
  1334. out:
  1335. pm_runtime_mark_last_busy(qup->dev);
  1336. pm_runtime_put_autosuspend(qup->dev);
  1337. return ret;
  1338. }
  1339. static u32 qup_i2c_func(struct i2c_adapter *adap)
  1340. {
  1341. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  1342. }
  1343. static const struct i2c_algorithm qup_i2c_algo = {
  1344. .master_xfer = qup_i2c_xfer,
  1345. .functionality = qup_i2c_func,
  1346. };
  1347. static const struct i2c_algorithm qup_i2c_algo_v2 = {
  1348. .master_xfer = qup_i2c_xfer_v2,
  1349. .functionality = qup_i2c_func,
  1350. };
  1351. /*
  1352. * The QUP block will issue a NACK and STOP on the bus when reaching
  1353. * the end of the read, the length of the read is specified as one byte
  1354. * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
  1355. */
  1356. static const struct i2c_adapter_quirks qup_i2c_quirks = {
  1357. .flags = I2C_AQ_NO_ZERO_LEN,
  1358. .max_read_len = QUP_READ_LIMIT,
  1359. };
  1360. static const struct i2c_adapter_quirks qup_i2c_quirks_v2 = {
  1361. .flags = I2C_AQ_NO_ZERO_LEN,
  1362. };
  1363. static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
  1364. {
  1365. clk_prepare_enable(qup->clk);
  1366. clk_prepare_enable(qup->pclk);
  1367. }
  1368. static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
  1369. {
  1370. u32 config;
  1371. qup_i2c_change_state(qup, QUP_RESET_STATE);
  1372. clk_disable_unprepare(qup->clk);
  1373. config = readl(qup->base + QUP_CONFIG);
  1374. config |= QUP_CLOCK_AUTO_GATE;
  1375. writel(config, qup->base + QUP_CONFIG);
  1376. clk_disable_unprepare(qup->pclk);
  1377. }
  1378. static const struct acpi_device_id qup_i2c_acpi_match[] = {
  1379. { "QCOM8010"},
  1380. { },
  1381. };
  1382. MODULE_DEVICE_TABLE(acpi, qup_i2c_acpi_match);
  1383. static int qup_i2c_probe(struct platform_device *pdev)
  1384. {
  1385. static const int blk_sizes[] = {4, 16, 32};
  1386. struct qup_i2c_dev *qup;
  1387. unsigned long one_bit_t;
  1388. struct resource *res;
  1389. u32 io_mode, hw_ver, size;
  1390. int ret, fs_div, hs_div;
  1391. u32 src_clk_freq = DEFAULT_SRC_CLK;
  1392. u32 clk_freq = DEFAULT_CLK_FREQ;
  1393. int blocks;
  1394. bool is_qup_v1;
  1395. qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
  1396. if (!qup)
  1397. return -ENOMEM;
  1398. qup->dev = &pdev->dev;
  1399. init_completion(&qup->xfer);
  1400. platform_set_drvdata(pdev, qup);
  1401. if (scl_freq) {
  1402. dev_notice(qup->dev, "Using override frequency of %u\n", scl_freq);
  1403. clk_freq = scl_freq;
  1404. } else {
  1405. ret = device_property_read_u32(qup->dev, "clock-frequency", &clk_freq);
  1406. if (ret) {
  1407. dev_notice(qup->dev, "using default clock-frequency %d",
  1408. DEFAULT_CLK_FREQ);
  1409. }
  1410. }
  1411. if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
  1412. qup->adap.algo = &qup_i2c_algo;
  1413. qup->adap.quirks = &qup_i2c_quirks;
  1414. is_qup_v1 = true;
  1415. } else {
  1416. qup->adap.algo = &qup_i2c_algo_v2;
  1417. qup->adap.quirks = &qup_i2c_quirks_v2;
  1418. is_qup_v1 = false;
  1419. if (acpi_match_device(qup_i2c_acpi_match, qup->dev))
  1420. goto nodma;
  1421. else
  1422. ret = qup_i2c_req_dma(qup);
  1423. if (ret == -EPROBE_DEFER)
  1424. goto fail_dma;
  1425. else if (ret != 0)
  1426. goto nodma;
  1427. qup->max_xfer_sg_len = (MX_BLOCKS << 1);
  1428. blocks = (MX_DMA_BLOCKS << 1) + 1;
  1429. qup->btx.sg = devm_kcalloc(&pdev->dev,
  1430. blocks, sizeof(*qup->btx.sg),
  1431. GFP_KERNEL);
  1432. if (!qup->btx.sg) {
  1433. ret = -ENOMEM;
  1434. goto fail_dma;
  1435. }
  1436. sg_init_table(qup->btx.sg, blocks);
  1437. qup->brx.sg = devm_kcalloc(&pdev->dev,
  1438. blocks, sizeof(*qup->brx.sg),
  1439. GFP_KERNEL);
  1440. if (!qup->brx.sg) {
  1441. ret = -ENOMEM;
  1442. goto fail_dma;
  1443. }
  1444. sg_init_table(qup->brx.sg, blocks);
  1445. /* 2 tag bytes for each block + 5 for start, stop tags */
  1446. size = blocks * 2 + 5;
  1447. qup->start_tag.start = devm_kzalloc(&pdev->dev,
  1448. size, GFP_KERNEL);
  1449. if (!qup->start_tag.start) {
  1450. ret = -ENOMEM;
  1451. goto fail_dma;
  1452. }
  1453. qup->brx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
  1454. if (!qup->brx.tag.start) {
  1455. ret = -ENOMEM;
  1456. goto fail_dma;
  1457. }
  1458. qup->btx.tag.start = devm_kzalloc(&pdev->dev, 2, GFP_KERNEL);
  1459. if (!qup->btx.tag.start) {
  1460. ret = -ENOMEM;
  1461. goto fail_dma;
  1462. }
  1463. qup->is_dma = true;
  1464. }
  1465. nodma:
  1466. /* We support frequencies up to FAST Mode Plus (1MHz) */
  1467. if (!clk_freq || clk_freq > I2C_FAST_MODE_PLUS_FREQ) {
  1468. dev_err(qup->dev, "clock frequency not supported %d\n",
  1469. clk_freq);
  1470. return -EINVAL;
  1471. }
  1472. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1473. qup->base = devm_ioremap_resource(qup->dev, res);
  1474. if (IS_ERR(qup->base))
  1475. return PTR_ERR(qup->base);
  1476. qup->irq = platform_get_irq(pdev, 0);
  1477. if (qup->irq < 0) {
  1478. dev_err(qup->dev, "No IRQ defined\n");
  1479. return qup->irq;
  1480. }
  1481. if (has_acpi_companion(qup->dev)) {
  1482. ret = device_property_read_u32(qup->dev,
  1483. "src-clock-hz", &src_clk_freq);
  1484. if (ret) {
  1485. dev_notice(qup->dev, "using default src-clock-hz %d",
  1486. DEFAULT_SRC_CLK);
  1487. }
  1488. ACPI_COMPANION_SET(&qup->adap.dev, ACPI_COMPANION(qup->dev));
  1489. } else {
  1490. qup->clk = devm_clk_get(qup->dev, "core");
  1491. if (IS_ERR(qup->clk)) {
  1492. dev_err(qup->dev, "Could not get core clock\n");
  1493. return PTR_ERR(qup->clk);
  1494. }
  1495. qup->pclk = devm_clk_get(qup->dev, "iface");
  1496. if (IS_ERR(qup->pclk)) {
  1497. dev_err(qup->dev, "Could not get iface clock\n");
  1498. return PTR_ERR(qup->pclk);
  1499. }
  1500. qup_i2c_enable_clocks(qup);
  1501. src_clk_freq = clk_get_rate(qup->clk);
  1502. }
  1503. /*
  1504. * Bootloaders might leave a pending interrupt on certain QUP's,
  1505. * so we reset the core before registering for interrupts.
  1506. */
  1507. writel(1, qup->base + QUP_SW_RESET);
  1508. ret = qup_i2c_poll_state_valid(qup);
  1509. if (ret)
  1510. goto fail;
  1511. ret = devm_request_irq(qup->dev, qup->irq, qup_i2c_interrupt,
  1512. IRQF_TRIGGER_HIGH, "i2c_qup", qup);
  1513. if (ret) {
  1514. dev_err(qup->dev, "Request %d IRQ failed\n", qup->irq);
  1515. goto fail;
  1516. }
  1517. disable_irq(qup->irq);
  1518. hw_ver = readl(qup->base + QUP_HW_VERSION);
  1519. dev_dbg(qup->dev, "Revision %x\n", hw_ver);
  1520. io_mode = readl(qup->base + QUP_IO_MODE);
  1521. /*
  1522. * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag'
  1523. * associated with each byte written/received
  1524. */
  1525. size = QUP_OUTPUT_BLOCK_SIZE(io_mode);
  1526. if (size >= ARRAY_SIZE(blk_sizes)) {
  1527. ret = -EIO;
  1528. goto fail;
  1529. }
  1530. qup->out_blk_sz = blk_sizes[size];
  1531. size = QUP_INPUT_BLOCK_SIZE(io_mode);
  1532. if (size >= ARRAY_SIZE(blk_sizes)) {
  1533. ret = -EIO;
  1534. goto fail;
  1535. }
  1536. qup->in_blk_sz = blk_sizes[size];
  1537. if (is_qup_v1) {
  1538. /*
  1539. * in QUP v1, QUP_CONFIG uses N as 15 i.e 16 bits constitutes a
  1540. * single transfer but the block size is in bytes so divide the
  1541. * in_blk_sz and out_blk_sz by 2
  1542. */
  1543. qup->in_blk_sz /= 2;
  1544. qup->out_blk_sz /= 2;
  1545. qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1;
  1546. qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1;
  1547. qup->write_rx_tags = qup_i2c_write_rx_tags_v1;
  1548. } else {
  1549. qup->write_tx_fifo = qup_i2c_write_tx_fifo_v2;
  1550. qup->read_rx_fifo = qup_i2c_read_rx_fifo_v2;
  1551. qup->write_rx_tags = qup_i2c_write_rx_tags_v2;
  1552. }
  1553. size = QUP_OUTPUT_FIFO_SIZE(io_mode);
  1554. qup->out_fifo_sz = qup->out_blk_sz * (2 << size);
  1555. size = QUP_INPUT_FIFO_SIZE(io_mode);
  1556. qup->in_fifo_sz = qup->in_blk_sz * (2 << size);
  1557. hs_div = 3;
  1558. if (clk_freq <= I2C_STANDARD_FREQ) {
  1559. fs_div = ((src_clk_freq / clk_freq) / 2) - 3;
  1560. qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff);
  1561. } else {
  1562. /* 33%/66% duty cycle */
  1563. fs_div = ((src_clk_freq / clk_freq) - 6) * 2 / 3;
  1564. qup->clk_ctl = ((fs_div / 2) << 16) | (hs_div << 8) | (fs_div & 0xff);
  1565. }
  1566. /*
  1567. * Time it takes for a byte to be clocked out on the bus.
  1568. * Each byte takes 9 clock cycles (8 bits + 1 ack).
  1569. */
  1570. one_bit_t = (USEC_PER_SEC / clk_freq) + 1;
  1571. qup->one_byte_t = one_bit_t * 9;
  1572. qup->xfer_timeout = TOUT_MIN * HZ +
  1573. usecs_to_jiffies(MX_DMA_TX_RX_LEN * qup->one_byte_t);
  1574. dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
  1575. qup->in_blk_sz, qup->in_fifo_sz,
  1576. qup->out_blk_sz, qup->out_fifo_sz);
  1577. i2c_set_adapdata(&qup->adap, qup);
  1578. qup->adap.dev.parent = qup->dev;
  1579. qup->adap.dev.of_node = pdev->dev.of_node;
  1580. qup->is_last = true;
  1581. strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
  1582. pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
  1583. pm_runtime_use_autosuspend(qup->dev);
  1584. pm_runtime_set_active(qup->dev);
  1585. pm_runtime_enable(qup->dev);
  1586. ret = i2c_add_adapter(&qup->adap);
  1587. if (ret)
  1588. goto fail_runtime;
  1589. return 0;
  1590. fail_runtime:
  1591. pm_runtime_disable(qup->dev);
  1592. pm_runtime_set_suspended(qup->dev);
  1593. fail:
  1594. qup_i2c_disable_clocks(qup);
  1595. fail_dma:
  1596. if (qup->btx.dma)
  1597. dma_release_channel(qup->btx.dma);
  1598. if (qup->brx.dma)
  1599. dma_release_channel(qup->brx.dma);
  1600. return ret;
  1601. }
  1602. static int qup_i2c_remove(struct platform_device *pdev)
  1603. {
  1604. struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
  1605. if (qup->is_dma) {
  1606. dma_release_channel(qup->btx.dma);
  1607. dma_release_channel(qup->brx.dma);
  1608. }
  1609. disable_irq(qup->irq);
  1610. qup_i2c_disable_clocks(qup);
  1611. i2c_del_adapter(&qup->adap);
  1612. pm_runtime_disable(qup->dev);
  1613. pm_runtime_set_suspended(qup->dev);
  1614. return 0;
  1615. }
  1616. #ifdef CONFIG_PM
  1617. static int qup_i2c_pm_suspend_runtime(struct device *device)
  1618. {
  1619. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  1620. dev_dbg(device, "pm_runtime: suspending...\n");
  1621. qup_i2c_disable_clocks(qup);
  1622. return 0;
  1623. }
  1624. static int qup_i2c_pm_resume_runtime(struct device *device)
  1625. {
  1626. struct qup_i2c_dev *qup = dev_get_drvdata(device);
  1627. dev_dbg(device, "pm_runtime: resuming...\n");
  1628. qup_i2c_enable_clocks(qup);
  1629. return 0;
  1630. }
  1631. #endif
  1632. #ifdef CONFIG_PM_SLEEP
  1633. static int qup_i2c_suspend(struct device *device)
  1634. {
  1635. if (!pm_runtime_suspended(device))
  1636. return qup_i2c_pm_suspend_runtime(device);
  1637. return 0;
  1638. }
  1639. static int qup_i2c_resume(struct device *device)
  1640. {
  1641. qup_i2c_pm_resume_runtime(device);
  1642. pm_runtime_mark_last_busy(device);
  1643. pm_request_autosuspend(device);
  1644. return 0;
  1645. }
  1646. #endif
  1647. static const struct dev_pm_ops qup_i2c_qup_pm_ops = {
  1648. SET_SYSTEM_SLEEP_PM_OPS(
  1649. qup_i2c_suspend,
  1650. qup_i2c_resume)
  1651. SET_RUNTIME_PM_OPS(
  1652. qup_i2c_pm_suspend_runtime,
  1653. qup_i2c_pm_resume_runtime,
  1654. NULL)
  1655. };
  1656. static const struct of_device_id qup_i2c_dt_match[] = {
  1657. { .compatible = "qcom,i2c-qup-v1.1.1" },
  1658. { .compatible = "qcom,i2c-qup-v2.1.1" },
  1659. { .compatible = "qcom,i2c-qup-v2.2.1" },
  1660. {}
  1661. };
  1662. MODULE_DEVICE_TABLE(of, qup_i2c_dt_match);
  1663. static struct platform_driver qup_i2c_driver = {
  1664. .probe = qup_i2c_probe,
  1665. .remove = qup_i2c_remove,
  1666. .driver = {
  1667. .name = "i2c_qup",
  1668. .pm = &qup_i2c_qup_pm_ops,
  1669. .of_match_table = qup_i2c_dt_match,
  1670. .acpi_match_table = ACPI_PTR(qup_i2c_acpi_match),
  1671. },
  1672. };
  1673. module_platform_driver(qup_i2c_driver);
  1674. MODULE_LICENSE("GPL v2");
  1675. MODULE_ALIAS("platform:i2c_qup");