i2c-qup.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  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 (msgs[idx].len == 0) {
  906. ret = -EINVAL;
  907. goto out;
  908. }
  909. if (qup_i2c_poll_state_i2c_master(qup)) {
  910. ret = -EIO;
  911. goto out;
  912. }
  913. if (qup_i2c_check_msg_len(&msgs[idx])) {
  914. ret = -EINVAL;
  915. goto out;
  916. }
  917. qup->msg = &msgs[idx];
  918. if (msgs[idx].flags & I2C_M_RD)
  919. ret = qup_i2c_read_one(qup);
  920. else
  921. ret = qup_i2c_write_one(qup);
  922. if (ret)
  923. break;
  924. ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
  925. if (ret)
  926. break;
  927. }
  928. if (ret == 0)
  929. ret = num;
  930. out:
  931. pm_runtime_mark_last_busy(qup->dev);
  932. pm_runtime_put_autosuspend(qup->dev);
  933. return ret;
  934. }
  935. /*
  936. * Configure registers related with reconfiguration during run and call it
  937. * before each i2c sub transfer.
  938. */
  939. static void qup_i2c_conf_count_v2(struct qup_i2c_dev *qup)
  940. {
  941. struct qup_i2c_block *blk = &qup->blk;
  942. u32 qup_config = I2C_MINI_CORE | I2C_N_VAL_V2;
  943. if (blk->is_tx_blk_mode)
  944. writel(qup->config_run | blk->total_tx_len,
  945. qup->base + QUP_MX_OUTPUT_CNT);
  946. else
  947. writel(qup->config_run | blk->total_tx_len,
  948. qup->base + QUP_MX_WRITE_CNT);
  949. if (blk->total_rx_len) {
  950. if (blk->is_rx_blk_mode)
  951. writel(qup->config_run | blk->total_rx_len,
  952. qup->base + QUP_MX_INPUT_CNT);
  953. else
  954. writel(qup->config_run | blk->total_rx_len,
  955. qup->base + QUP_MX_READ_CNT);
  956. } else {
  957. qup_config |= QUP_NO_INPUT;
  958. }
  959. writel(qup_config, qup->base + QUP_CONFIG);
  960. }
  961. /*
  962. * Configure registers related with transfer mode (FIFO/Block)
  963. * before starting of i2c transfer. It will be called only once in
  964. * QUP RESET state.
  965. */
  966. static void qup_i2c_conf_mode_v2(struct qup_i2c_dev *qup)
  967. {
  968. struct qup_i2c_block *blk = &qup->blk;
  969. u32 io_mode = QUP_REPACK_EN;
  970. if (blk->is_tx_blk_mode) {
  971. io_mode |= QUP_OUTPUT_BLK_MODE;
  972. writel(0, qup->base + QUP_MX_WRITE_CNT);
  973. } else {
  974. writel(0, qup->base + QUP_MX_OUTPUT_CNT);
  975. }
  976. if (blk->is_rx_blk_mode) {
  977. io_mode |= QUP_INPUT_BLK_MODE;
  978. writel(0, qup->base + QUP_MX_READ_CNT);
  979. } else {
  980. writel(0, qup->base + QUP_MX_INPUT_CNT);
  981. }
  982. writel(io_mode, qup->base + QUP_IO_MODE);
  983. }
  984. /* Clear required variables before starting of any QUP v2 sub transfer. */
  985. static void qup_i2c_clear_blk_v2(struct qup_i2c_block *blk)
  986. {
  987. blk->send_last_word = false;
  988. blk->tx_tags_sent = false;
  989. blk->tx_fifo_data = 0;
  990. blk->tx_fifo_data_pos = 0;
  991. blk->tx_fifo_free = 0;
  992. blk->rx_tags_fetched = false;
  993. blk->rx_bytes_read = false;
  994. blk->rx_fifo_data = 0;
  995. blk->rx_fifo_data_pos = 0;
  996. blk->fifo_available = 0;
  997. }
  998. /* Receive data from RX FIFO for read message in QUP v2 i2c transfer. */
  999. static void qup_i2c_recv_data(struct qup_i2c_dev *qup)
  1000. {
  1001. struct qup_i2c_block *blk = &qup->blk;
  1002. int j;
  1003. for (j = blk->rx_fifo_data_pos;
  1004. blk->cur_blk_len && blk->fifo_available;
  1005. blk->cur_blk_len--, blk->fifo_available--) {
  1006. if (j == 0)
  1007. blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
  1008. *(blk->cur_data++) = blk->rx_fifo_data;
  1009. blk->rx_fifo_data >>= 8;
  1010. if (j == 3)
  1011. j = 0;
  1012. else
  1013. j++;
  1014. }
  1015. blk->rx_fifo_data_pos = j;
  1016. }
  1017. /* Receive tags for read message in QUP v2 i2c transfer. */
  1018. static void qup_i2c_recv_tags(struct qup_i2c_dev *qup)
  1019. {
  1020. struct qup_i2c_block *blk = &qup->blk;
  1021. blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
  1022. blk->rx_fifo_data >>= blk->rx_tag_len * 8;
  1023. blk->rx_fifo_data_pos = blk->rx_tag_len;
  1024. blk->fifo_available -= blk->rx_tag_len;
  1025. }
  1026. /*
  1027. * Read the data and tags from RX FIFO. Since in read case, the tags will be
  1028. * preceded by received data bytes so
  1029. * 1. Check if rx_tags_fetched is false i.e. the start of QUP block so receive
  1030. * all tag bytes and discard that.
  1031. * 2. Read the data from RX FIFO. When all the data bytes have been read then
  1032. * set rx_bytes_read to true.
  1033. */
  1034. static void qup_i2c_read_rx_fifo_v2(struct qup_i2c_dev *qup)
  1035. {
  1036. struct qup_i2c_block *blk = &qup->blk;
  1037. if (!blk->rx_tags_fetched) {
  1038. qup_i2c_recv_tags(qup);
  1039. blk->rx_tags_fetched = true;
  1040. }
  1041. qup_i2c_recv_data(qup);
  1042. if (!blk->cur_blk_len)
  1043. blk->rx_bytes_read = true;
  1044. }
  1045. /*
  1046. * Write bytes in TX FIFO for write message in QUP v2 i2c transfer. QUP TX FIFO
  1047. * write works on word basis (4 bytes). Append new data byte write for TX FIFO
  1048. * in tx_fifo_data and write to TX FIFO when all the 4 bytes are present.
  1049. */
  1050. static void
  1051. qup_i2c_write_blk_data(struct qup_i2c_dev *qup, u8 **data, unsigned int *len)
  1052. {
  1053. struct qup_i2c_block *blk = &qup->blk;
  1054. unsigned int j;
  1055. for (j = blk->tx_fifo_data_pos; *len && blk->tx_fifo_free;
  1056. (*len)--, blk->tx_fifo_free--) {
  1057. blk->tx_fifo_data |= *(*data)++ << (j * 8);
  1058. if (j == 3) {
  1059. writel(blk->tx_fifo_data,
  1060. qup->base + QUP_OUT_FIFO_BASE);
  1061. blk->tx_fifo_data = 0x0;
  1062. j = 0;
  1063. } else {
  1064. j++;
  1065. }
  1066. }
  1067. blk->tx_fifo_data_pos = j;
  1068. }
  1069. /* Transfer tags for read message in QUP v2 i2c transfer. */
  1070. static void qup_i2c_write_rx_tags_v2(struct qup_i2c_dev *qup)
  1071. {
  1072. struct qup_i2c_block *blk = &qup->blk;
  1073. qup_i2c_write_blk_data(qup, &blk->cur_tx_tags, &blk->tx_tag_len);
  1074. if (blk->tx_fifo_data_pos)
  1075. writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
  1076. }
  1077. /*
  1078. * Write the data and tags in TX FIFO. Since in write case, both tags and data
  1079. * need to be written and QUP write tags can have maximum 256 data length, so
  1080. *
  1081. * 1. Check if tx_tags_sent is false i.e. the start of QUP block so write the
  1082. * tags to TX FIFO and set tx_tags_sent to true.
  1083. * 2. Check if send_last_word is true. It will be set when last few data bytes
  1084. * (less than 4 bytes) are reamining to be written in FIFO because of no FIFO
  1085. * space. All this data bytes are available in tx_fifo_data so write this
  1086. * in FIFO.
  1087. * 3. Write the data to TX FIFO and check for cur_blk_len. If it is non zero
  1088. * then more data is pending otherwise following 3 cases can be possible
  1089. * a. if tx_fifo_data_pos is zero i.e. all the data bytes in this block
  1090. * have been written in TX FIFO so nothing else is required.
  1091. * b. tx_fifo_free is non zero i.e tx FIFO is free so copy the remaining data
  1092. * from tx_fifo_data to tx FIFO. Since, qup_i2c_write_blk_data do write
  1093. * in 4 bytes and FIFO space is in multiple of 4 bytes so tx_fifo_free
  1094. * will be always greater than or equal to 4 bytes.
  1095. * c. tx_fifo_free is zero. In this case, last few bytes (less than 4
  1096. * bytes) are copied to tx_fifo_data but couldn't be sent because of
  1097. * FIFO full so make send_last_word true.
  1098. */
  1099. static void qup_i2c_write_tx_fifo_v2(struct qup_i2c_dev *qup)
  1100. {
  1101. struct qup_i2c_block *blk = &qup->blk;
  1102. if (!blk->tx_tags_sent) {
  1103. qup_i2c_write_blk_data(qup, &blk->cur_tx_tags,
  1104. &blk->tx_tag_len);
  1105. blk->tx_tags_sent = true;
  1106. }
  1107. if (blk->send_last_word)
  1108. goto send_last_word;
  1109. qup_i2c_write_blk_data(qup, &blk->cur_data, &blk->cur_blk_len);
  1110. if (!blk->cur_blk_len) {
  1111. if (!blk->tx_fifo_data_pos)
  1112. return;
  1113. if (blk->tx_fifo_free)
  1114. goto send_last_word;
  1115. blk->send_last_word = true;
  1116. }
  1117. return;
  1118. send_last_word:
  1119. writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
  1120. }
  1121. /*
  1122. * Main transfer function which read or write i2c data.
  1123. * The QUP v2 supports reconfiguration during run in which multiple i2c sub
  1124. * transfers can be scheduled.
  1125. */
  1126. static int
  1127. qup_i2c_conf_xfer_v2(struct qup_i2c_dev *qup, bool is_rx, bool is_first,
  1128. bool change_pause_state)
  1129. {
  1130. struct qup_i2c_block *blk = &qup->blk;
  1131. struct i2c_msg *msg = qup->msg;
  1132. int ret;
  1133. /*
  1134. * Check if its SMBus Block read for which the top level read will be
  1135. * done into 2 QUP reads. One with message length 1 while other one is
  1136. * with actual length.
  1137. */
  1138. if (qup_i2c_check_msg_len(msg)) {
  1139. if (qup->is_smbus_read) {
  1140. /*
  1141. * If the message length is already read in
  1142. * the first byte of the buffer, account for
  1143. * that by setting the offset
  1144. */
  1145. blk->cur_data += 1;
  1146. is_first = false;
  1147. } else {
  1148. change_pause_state = false;
  1149. }
  1150. }
  1151. qup->config_run = is_first ? 0 : QUP_I2C_MX_CONFIG_DURING_RUN;
  1152. qup_i2c_clear_blk_v2(blk);
  1153. qup_i2c_conf_count_v2(qup);
  1154. /* If it is first sub transfer, then configure i2c bus clocks */
  1155. if (is_first) {
  1156. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  1157. if (ret)
  1158. return ret;
  1159. writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
  1160. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  1161. if (ret)
  1162. return ret;
  1163. }
  1164. reinit_completion(&qup->xfer);
  1165. enable_irq(qup->irq);
  1166. /*
  1167. * In FIFO mode, tx FIFO can be written directly while in block mode the
  1168. * it will be written after getting OUT_BLOCK_WRITE_REQ interrupt
  1169. */
  1170. if (!blk->is_tx_blk_mode) {
  1171. blk->tx_fifo_free = qup->out_fifo_sz;
  1172. if (is_rx)
  1173. qup_i2c_write_rx_tags_v2(qup);
  1174. else
  1175. qup_i2c_write_tx_fifo_v2(qup);
  1176. }
  1177. ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
  1178. if (ret)
  1179. goto err;
  1180. ret = qup_i2c_wait_for_complete(qup, msg);
  1181. if (ret)
  1182. goto err;
  1183. /* Move to pause state for all the transfers, except last one */
  1184. if (change_pause_state) {
  1185. ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
  1186. if (ret)
  1187. goto err;
  1188. }
  1189. err:
  1190. disable_irq(qup->irq);
  1191. return ret;
  1192. }
  1193. /*
  1194. * Transfer one read/write message in i2c transfer. It splits the message into
  1195. * multiple of blk_xfer_limit data length blocks and schedule each
  1196. * QUP block individually.
  1197. */
  1198. static int qup_i2c_xfer_v2_msg(struct qup_i2c_dev *qup, int msg_id, bool is_rx)
  1199. {
  1200. int ret = 0;
  1201. unsigned int data_len, i;
  1202. struct i2c_msg *msg = qup->msg;
  1203. struct qup_i2c_block *blk = &qup->blk;
  1204. u8 *msg_buf = msg->buf;
  1205. qup->blk_xfer_limit = is_rx ? RECV_MAX_DATA_LEN : QUP_READ_LIMIT;
  1206. qup_i2c_set_blk_data(qup, msg);
  1207. for (i = 0; i < blk->count; i++) {
  1208. data_len = qup_i2c_get_data_len(qup);
  1209. blk->pos = i;
  1210. blk->cur_tx_tags = blk->tags;
  1211. blk->cur_blk_len = data_len;
  1212. blk->tx_tag_len =
  1213. qup_i2c_set_tags(blk->cur_tx_tags, qup, qup->msg);
  1214. blk->cur_data = msg_buf;
  1215. if (is_rx) {
  1216. blk->total_tx_len = blk->tx_tag_len;
  1217. blk->rx_tag_len = 2;
  1218. blk->total_rx_len = blk->rx_tag_len + data_len;
  1219. } else {
  1220. blk->total_tx_len = blk->tx_tag_len + data_len;
  1221. blk->total_rx_len = 0;
  1222. }
  1223. ret = qup_i2c_conf_xfer_v2(qup, is_rx, !msg_id && !i,
  1224. !qup->is_last || i < blk->count - 1);
  1225. if (ret)
  1226. return ret;
  1227. /* Handle SMBus block read length */
  1228. if (qup_i2c_check_msg_len(msg) && msg->len == 1 &&
  1229. !qup->is_smbus_read) {
  1230. if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX)
  1231. return -EPROTO;
  1232. msg->len = msg->buf[0];
  1233. qup->is_smbus_read = true;
  1234. ret = qup_i2c_xfer_v2_msg(qup, msg_id, true);
  1235. qup->is_smbus_read = false;
  1236. if (ret)
  1237. return ret;
  1238. msg->len += 1;
  1239. }
  1240. msg_buf += data_len;
  1241. blk->data_len -= qup->blk_xfer_limit;
  1242. }
  1243. return ret;
  1244. }
  1245. /*
  1246. * QUP v2 supports 3 modes
  1247. * Programmed IO using FIFO mode : Less than FIFO size
  1248. * Programmed IO using Block mode : Greater than FIFO size
  1249. * DMA using BAM : Appropriate for any transaction size but the address should
  1250. * be DMA applicable
  1251. *
  1252. * This function determines the mode which will be used for this transfer. An
  1253. * i2c transfer contains multiple message. Following are the rules to determine
  1254. * the mode used.
  1255. * 1. Determine complete length, maximum tx and rx length for complete transfer.
  1256. * 2. If complete transfer length is greater than fifo size then use the DMA
  1257. * mode.
  1258. * 3. In FIFO or block mode, tx and rx can operate in different mode so check
  1259. * for maximum tx and rx length to determine mode.
  1260. */
  1261. static int
  1262. qup_i2c_determine_mode_v2(struct qup_i2c_dev *qup,
  1263. struct i2c_msg msgs[], int num)
  1264. {
  1265. int idx;
  1266. bool no_dma = false;
  1267. unsigned int max_tx_len = 0, max_rx_len = 0, total_len = 0;
  1268. /* All i2c_msgs should be transferred using either dma or cpu */
  1269. for (idx = 0; idx < num; idx++) {
  1270. if (msgs[idx].len == 0)
  1271. return -EINVAL;
  1272. if (msgs[idx].flags & I2C_M_RD)
  1273. max_rx_len = max_t(unsigned int, max_rx_len,
  1274. msgs[idx].len);
  1275. else
  1276. max_tx_len = max_t(unsigned int, max_tx_len,
  1277. msgs[idx].len);
  1278. if (is_vmalloc_addr(msgs[idx].buf))
  1279. no_dma = true;
  1280. total_len += msgs[idx].len;
  1281. }
  1282. if (!no_dma && qup->is_dma &&
  1283. (total_len > qup->out_fifo_sz || total_len > qup->in_fifo_sz)) {
  1284. qup->use_dma = true;
  1285. } else {
  1286. qup->blk.is_tx_blk_mode = max_tx_len > qup->out_fifo_sz -
  1287. QUP_MAX_TAGS_LEN ? true : false;
  1288. qup->blk.is_rx_blk_mode = max_rx_len > qup->in_fifo_sz -
  1289. READ_RX_TAGS_LEN ? true : false;
  1290. }
  1291. return 0;
  1292. }
  1293. static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
  1294. struct i2c_msg msgs[],
  1295. int num)
  1296. {
  1297. struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
  1298. int ret, idx = 0;
  1299. qup->bus_err = 0;
  1300. qup->qup_err = 0;
  1301. ret = pm_runtime_get_sync(qup->dev);
  1302. if (ret < 0)
  1303. goto out;
  1304. ret = qup_i2c_determine_mode_v2(qup, msgs, num);
  1305. if (ret)
  1306. goto out;
  1307. writel(1, qup->base + QUP_SW_RESET);
  1308. ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
  1309. if (ret)
  1310. goto out;
  1311. /* Configure QUP as I2C mini core */
  1312. writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
  1313. writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
  1314. if (qup_i2c_poll_state_i2c_master(qup)) {
  1315. ret = -EIO;
  1316. goto out;
  1317. }
  1318. if (qup->use_dma) {
  1319. reinit_completion(&qup->xfer);
  1320. ret = qup_i2c_bam_xfer(adap, &msgs[0], num);
  1321. qup->use_dma = false;
  1322. } else {
  1323. qup_i2c_conf_mode_v2(qup);
  1324. for (idx = 0; idx < num; idx++) {
  1325. qup->msg = &msgs[idx];
  1326. qup->is_last = idx == (num - 1);
  1327. ret = qup_i2c_xfer_v2_msg(qup, idx,
  1328. !!(msgs[idx].flags & I2C_M_RD));
  1329. if (ret)
  1330. break;
  1331. }
  1332. qup->msg = NULL;
  1333. }
  1334. if (!ret)
  1335. ret = qup_i2c_bus_active(qup, ONE_BYTE);
  1336. if (!ret)
  1337. qup_i2c_change_state(qup, QUP_RESET_STATE);
  1338. if (ret == 0)
  1339. ret = num;
  1340. out:
  1341. pm_runtime_mark_last_busy(qup->dev);
  1342. pm_runtime_put_autosuspend(qup->dev);
  1343. return ret;
  1344. }
  1345. static u32 qup_i2c_func(struct i2c_adapter *adap)
  1346. {
  1347. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  1348. }
  1349. static const struct i2c_algorithm qup_i2c_algo = {
  1350. .master_xfer = qup_i2c_xfer,
  1351. .functionality = qup_i2c_func,
  1352. };
  1353. static const struct i2c_algorithm qup_i2c_algo_v2 = {
  1354. .master_xfer = qup_i2c_xfer_v2,
  1355. .functionality = qup_i2c_func,
  1356. };
  1357. /*
  1358. * The QUP block will issue a NACK and STOP on the bus when reaching
  1359. * the end of the read, the length of the read is specified as one byte
  1360. * which limits the possible read to 256 (QUP_READ_LIMIT) bytes.
  1361. */
  1362. static const struct i2c_adapter_quirks qup_i2c_quirks = {
  1363. .max_read_len = QUP_READ_LIMIT,
  1364. };
  1365. static void qup_i2c_enable_clocks(struct qup_i2c_dev *qup)
  1366. {
  1367. clk_prepare_enable(qup->clk);
  1368. clk_prepare_enable(qup->pclk);
  1369. }
  1370. static void qup_i2c_disable_clocks(struct qup_i2c_dev *qup)
  1371. {
  1372. u32 config;
  1373. qup_i2c_change_state(qup, QUP_RESET_STATE);
  1374. clk_disable_unprepare(qup->clk);
  1375. config = readl(qup->base + QUP_CONFIG);
  1376. config |= QUP_CLOCK_AUTO_GATE;
  1377. writel(config, qup->base + QUP_CONFIG);
  1378. clk_disable_unprepare(qup->pclk);
  1379. }
  1380. static const struct acpi_device_id qup_i2c_acpi_match[] = {
  1381. { "QCOM8010"},
  1382. { },
  1383. };
  1384. MODULE_DEVICE_TABLE(acpi, qup_i2c_acpi_match);
  1385. static int qup_i2c_probe(struct platform_device *pdev)
  1386. {
  1387. static const int blk_sizes[] = {4, 16, 32};
  1388. struct qup_i2c_dev *qup;
  1389. unsigned long one_bit_t;
  1390. struct resource *res;
  1391. u32 io_mode, hw_ver, size;
  1392. int ret, fs_div, hs_div;
  1393. u32 src_clk_freq = DEFAULT_SRC_CLK;
  1394. u32 clk_freq = DEFAULT_CLK_FREQ;
  1395. int blocks;
  1396. bool is_qup_v1;
  1397. qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
  1398. if (!qup)
  1399. return -ENOMEM;
  1400. qup->dev = &pdev->dev;
  1401. init_completion(&qup->xfer);
  1402. platform_set_drvdata(pdev, qup);
  1403. if (scl_freq) {
  1404. dev_notice(qup->dev, "Using override frequency of %u\n", scl_freq);
  1405. clk_freq = scl_freq;
  1406. } else {
  1407. ret = device_property_read_u32(qup->dev, "clock-frequency", &clk_freq);
  1408. if (ret) {
  1409. dev_notice(qup->dev, "using default clock-frequency %d",
  1410. DEFAULT_CLK_FREQ);
  1411. }
  1412. }
  1413. if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
  1414. qup->adap.algo = &qup_i2c_algo;
  1415. qup->adap.quirks = &qup_i2c_quirks;
  1416. is_qup_v1 = true;
  1417. } else {
  1418. qup->adap.algo = &qup_i2c_algo_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");