i2c-imx.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2002 Motorola GSG-China
  4. *
  5. * Author:
  6. * Darius Augulis, Teltonika Inc.
  7. *
  8. * Desc.:
  9. * Implementation of I2C Adapter/Algorithm Driver
  10. * for I2C Bus integrated in Freescale i.MX/MXC processors
  11. *
  12. * Derived from Motorola GSG China I2C example driver
  13. *
  14. * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de
  15. * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de
  16. * Copyright (C) 2007 RightHand Technologies, Inc.
  17. * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
  18. *
  19. * Copyright 2013 Freescale Semiconductor, Inc.
  20. *
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/completion.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/dmapool.h>
  28. #include <linux/err.h>
  29. #include <linux/errno.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/i2c.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/io.h>
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/of.h>
  38. #include <linux/of_device.h>
  39. #include <linux/of_dma.h>
  40. #include <linux/pinctrl/consumer.h>
  41. #include <linux/platform_data/i2c-imx.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/sched.h>
  45. #include <linux/slab.h>
  46. /* This will be the driver name the kernel reports */
  47. #define DRIVER_NAME "imx-i2c"
  48. /* Default value */
  49. #define IMX_I2C_BIT_RATE 100000 /* 100kHz */
  50. /*
  51. * Enable DMA if transfer byte size is bigger than this threshold.
  52. * As the hardware request, it must bigger than 4 bytes.\
  53. * I have set '16' here, maybe it's not the best but I think it's
  54. * the appropriate.
  55. */
  56. #define DMA_THRESHOLD 16
  57. #define DMA_TIMEOUT 1000
  58. /* IMX I2C registers:
  59. * the I2C register offset is different between SoCs,
  60. * to provid support for all these chips, split the
  61. * register offset into a fixed base address and a
  62. * variable shift value, then the full register offset
  63. * will be calculated by
  64. * reg_off = ( reg_base_addr << reg_shift)
  65. */
  66. #define IMX_I2C_IADR 0x00 /* i2c slave address */
  67. #define IMX_I2C_IFDR 0x01 /* i2c frequency divider */
  68. #define IMX_I2C_I2CR 0x02 /* i2c control */
  69. #define IMX_I2C_I2SR 0x03 /* i2c status */
  70. #define IMX_I2C_I2DR 0x04 /* i2c transfer data */
  71. #define IMX_I2C_REGSHIFT 2
  72. #define VF610_I2C_REGSHIFT 0
  73. /* Bits of IMX I2C registers */
  74. #define I2SR_RXAK 0x01
  75. #define I2SR_IIF 0x02
  76. #define I2SR_SRW 0x04
  77. #define I2SR_IAL 0x10
  78. #define I2SR_IBB 0x20
  79. #define I2SR_IAAS 0x40
  80. #define I2SR_ICF 0x80
  81. #define I2CR_DMAEN 0x02
  82. #define I2CR_RSTA 0x04
  83. #define I2CR_TXAK 0x08
  84. #define I2CR_MTX 0x10
  85. #define I2CR_MSTA 0x20
  86. #define I2CR_IIEN 0x40
  87. #define I2CR_IEN 0x80
  88. /* register bits different operating codes definition:
  89. * 1) I2SR: Interrupt flags clear operation differ between SoCs:
  90. * - write zero to clear(w0c) INT flag on i.MX,
  91. * - but write one to clear(w1c) INT flag on Vybrid.
  92. * 2) I2CR: I2C module enable operation also differ between SoCs:
  93. * - set I2CR_IEN bit enable the module on i.MX,
  94. * - but clear I2CR_IEN bit enable the module on Vybrid.
  95. */
  96. #define I2SR_CLR_OPCODE_W0C 0x0
  97. #define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF)
  98. #define I2CR_IEN_OPCODE_0 0x0
  99. #define I2CR_IEN_OPCODE_1 I2CR_IEN
  100. #define I2C_PM_TIMEOUT 10 /* ms */
  101. /*
  102. * sorted list of clock divider, register value pairs
  103. * taken from table 26-5, p.26-9, Freescale i.MX
  104. * Integrated Portable System Processor Reference Manual
  105. * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
  106. *
  107. * Duplicated divider values removed from list
  108. */
  109. struct imx_i2c_clk_pair {
  110. u16 div;
  111. u16 val;
  112. };
  113. static struct imx_i2c_clk_pair imx_i2c_clk_div[] = {
  114. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  115. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  116. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  117. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  118. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  119. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  120. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  121. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  122. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  123. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  124. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  125. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  126. { 3072, 0x1E }, { 3840, 0x1F }
  127. };
  128. /* Vybrid VF610 clock divider, register value pairs */
  129. static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = {
  130. { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
  131. { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
  132. { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
  133. { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
  134. { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
  135. { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
  136. { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
  137. { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
  138. { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
  139. { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
  140. { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
  141. { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
  142. { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
  143. { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
  144. { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
  145. };
  146. enum imx_i2c_type {
  147. IMX1_I2C,
  148. IMX21_I2C,
  149. VF610_I2C,
  150. };
  151. struct imx_i2c_hwdata {
  152. enum imx_i2c_type devtype;
  153. unsigned regshift;
  154. struct imx_i2c_clk_pair *clk_div;
  155. unsigned ndivs;
  156. unsigned i2sr_clr_opcode;
  157. unsigned i2cr_ien_opcode;
  158. };
  159. struct imx_i2c_dma {
  160. struct dma_chan *chan_tx;
  161. struct dma_chan *chan_rx;
  162. struct dma_chan *chan_using;
  163. struct completion cmd_complete;
  164. dma_addr_t dma_buf;
  165. unsigned int dma_len;
  166. enum dma_transfer_direction dma_transfer_dir;
  167. enum dma_data_direction dma_data_dir;
  168. };
  169. struct imx_i2c_struct {
  170. struct i2c_adapter adapter;
  171. struct clk *clk;
  172. struct notifier_block clk_change_nb;
  173. void __iomem *base;
  174. wait_queue_head_t queue;
  175. unsigned long i2csr;
  176. unsigned int disable_delay;
  177. int stopped;
  178. unsigned int ifdr; /* IMX_I2C_IFDR */
  179. unsigned int cur_clk;
  180. unsigned int bitrate;
  181. const struct imx_i2c_hwdata *hwdata;
  182. struct i2c_bus_recovery_info rinfo;
  183. struct pinctrl *pinctrl;
  184. struct pinctrl_state *pinctrl_pins_default;
  185. struct pinctrl_state *pinctrl_pins_gpio;
  186. struct imx_i2c_dma *dma;
  187. };
  188. static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
  189. .devtype = IMX1_I2C,
  190. .regshift = IMX_I2C_REGSHIFT,
  191. .clk_div = imx_i2c_clk_div,
  192. .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
  193. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
  194. .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
  195. };
  196. static const struct imx_i2c_hwdata imx21_i2c_hwdata = {
  197. .devtype = IMX21_I2C,
  198. .regshift = IMX_I2C_REGSHIFT,
  199. .clk_div = imx_i2c_clk_div,
  200. .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
  201. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
  202. .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
  203. };
  204. static struct imx_i2c_hwdata vf610_i2c_hwdata = {
  205. .devtype = VF610_I2C,
  206. .regshift = VF610_I2C_REGSHIFT,
  207. .clk_div = vf610_i2c_clk_div,
  208. .ndivs = ARRAY_SIZE(vf610_i2c_clk_div),
  209. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C,
  210. .i2cr_ien_opcode = I2CR_IEN_OPCODE_0,
  211. };
  212. static const struct platform_device_id imx_i2c_devtype[] = {
  213. {
  214. .name = "imx1-i2c",
  215. .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata,
  216. }, {
  217. .name = "imx21-i2c",
  218. .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata,
  219. }, {
  220. /* sentinel */
  221. }
  222. };
  223. MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
  224. static const struct of_device_id i2c_imx_dt_ids[] = {
  225. { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, },
  226. { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, },
  227. { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, },
  228. { /* sentinel */ }
  229. };
  230. MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids);
  231. static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx)
  232. {
  233. return i2c_imx->hwdata->devtype == IMX1_I2C;
  234. }
  235. static inline void imx_i2c_write_reg(unsigned int val,
  236. struct imx_i2c_struct *i2c_imx, unsigned int reg)
  237. {
  238. writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
  239. }
  240. static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx,
  241. unsigned int reg)
  242. {
  243. return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
  244. }
  245. /* Functions for DMA support */
  246. static void i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx,
  247. dma_addr_t phy_addr)
  248. {
  249. struct imx_i2c_dma *dma;
  250. struct dma_slave_config dma_sconfig;
  251. struct device *dev = &i2c_imx->adapter.dev;
  252. int ret;
  253. dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
  254. if (!dma)
  255. return;
  256. dma->chan_tx = dma_request_slave_channel(dev, "tx");
  257. if (!dma->chan_tx) {
  258. dev_dbg(dev, "can't request DMA tx channel\n");
  259. goto fail_al;
  260. }
  261. dma_sconfig.dst_addr = phy_addr +
  262. (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
  263. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  264. dma_sconfig.dst_maxburst = 1;
  265. dma_sconfig.direction = DMA_MEM_TO_DEV;
  266. ret = dmaengine_slave_config(dma->chan_tx, &dma_sconfig);
  267. if (ret < 0) {
  268. dev_dbg(dev, "can't configure tx channel\n");
  269. goto fail_tx;
  270. }
  271. dma->chan_rx = dma_request_slave_channel(dev, "rx");
  272. if (!dma->chan_rx) {
  273. dev_dbg(dev, "can't request DMA rx channel\n");
  274. goto fail_tx;
  275. }
  276. dma_sconfig.src_addr = phy_addr +
  277. (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
  278. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  279. dma_sconfig.src_maxburst = 1;
  280. dma_sconfig.direction = DMA_DEV_TO_MEM;
  281. ret = dmaengine_slave_config(dma->chan_rx, &dma_sconfig);
  282. if (ret < 0) {
  283. dev_dbg(dev, "can't configure rx channel\n");
  284. goto fail_rx;
  285. }
  286. i2c_imx->dma = dma;
  287. init_completion(&dma->cmd_complete);
  288. dev_info(dev, "using %s (tx) and %s (rx) for DMA transfers\n",
  289. dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
  290. return;
  291. fail_rx:
  292. dma_release_channel(dma->chan_rx);
  293. fail_tx:
  294. dma_release_channel(dma->chan_tx);
  295. fail_al:
  296. devm_kfree(dev, dma);
  297. dev_info(dev, "can't use DMA, using PIO instead.\n");
  298. }
  299. static void i2c_imx_dma_callback(void *arg)
  300. {
  301. struct imx_i2c_struct *i2c_imx = (struct imx_i2c_struct *)arg;
  302. struct imx_i2c_dma *dma = i2c_imx->dma;
  303. dma_unmap_single(dma->chan_using->device->dev, dma->dma_buf,
  304. dma->dma_len, dma->dma_data_dir);
  305. complete(&dma->cmd_complete);
  306. }
  307. static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
  308. struct i2c_msg *msgs)
  309. {
  310. struct imx_i2c_dma *dma = i2c_imx->dma;
  311. struct dma_async_tx_descriptor *txdesc;
  312. struct device *dev = &i2c_imx->adapter.dev;
  313. struct device *chan_dev = dma->chan_using->device->dev;
  314. dma->dma_buf = dma_map_single(chan_dev, msgs->buf,
  315. dma->dma_len, dma->dma_data_dir);
  316. if (dma_mapping_error(chan_dev, dma->dma_buf)) {
  317. dev_err(dev, "DMA mapping failed\n");
  318. goto err_map;
  319. }
  320. txdesc = dmaengine_prep_slave_single(dma->chan_using, dma->dma_buf,
  321. dma->dma_len, dma->dma_transfer_dir,
  322. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  323. if (!txdesc) {
  324. dev_err(dev, "Not able to get desc for DMA xfer\n");
  325. goto err_desc;
  326. }
  327. reinit_completion(&dma->cmd_complete);
  328. txdesc->callback = i2c_imx_dma_callback;
  329. txdesc->callback_param = i2c_imx;
  330. if (dma_submit_error(dmaengine_submit(txdesc))) {
  331. dev_err(dev, "DMA submit failed\n");
  332. goto err_submit;
  333. }
  334. dma_async_issue_pending(dma->chan_using);
  335. return 0;
  336. err_submit:
  337. dmaengine_terminate_all(dma->chan_using);
  338. err_desc:
  339. dma_unmap_single(chan_dev, dma->dma_buf,
  340. dma->dma_len, dma->dma_data_dir);
  341. err_map:
  342. return -EINVAL;
  343. }
  344. static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
  345. {
  346. struct imx_i2c_dma *dma = i2c_imx->dma;
  347. dma->dma_buf = 0;
  348. dma->dma_len = 0;
  349. dma_release_channel(dma->chan_tx);
  350. dma->chan_tx = NULL;
  351. dma_release_channel(dma->chan_rx);
  352. dma->chan_rx = NULL;
  353. dma->chan_using = NULL;
  354. }
  355. static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned int bits)
  356. {
  357. unsigned int temp;
  358. /*
  359. * i2sr_clr_opcode is the value to clear all interrupts. Here we want to
  360. * clear only <bits>, so we write ~i2sr_clr_opcode with just <bits>
  361. * toggled. This is required because i.MX needs W0C and Vybrid uses W1C.
  362. */
  363. temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ bits;
  364. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  365. }
  366. static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
  367. {
  368. unsigned long orig_jiffies = jiffies;
  369. unsigned int temp;
  370. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  371. while (1) {
  372. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  373. /* check for arbitration lost */
  374. if (temp & I2SR_IAL) {
  375. i2c_imx_clear_irq(i2c_imx, I2SR_IAL);
  376. return -EAGAIN;
  377. }
  378. if (for_busy && (temp & I2SR_IBB)) {
  379. i2c_imx->stopped = 0;
  380. break;
  381. }
  382. if (!for_busy && !(temp & I2SR_IBB)) {
  383. i2c_imx->stopped = 1;
  384. break;
  385. }
  386. if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
  387. dev_dbg(&i2c_imx->adapter.dev,
  388. "<%s> I2C bus is busy\n", __func__);
  389. return -ETIMEDOUT;
  390. }
  391. schedule();
  392. }
  393. return 0;
  394. }
  395. static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
  396. {
  397. wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
  398. if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
  399. dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
  400. return -ETIMEDOUT;
  401. }
  402. /* check for arbitration lost */
  403. if (i2c_imx->i2csr & I2SR_IAL) {
  404. dev_dbg(&i2c_imx->adapter.dev, "<%s> Arbitration lost\n", __func__);
  405. i2c_imx_clear_irq(i2c_imx, I2SR_IAL);
  406. i2c_imx->i2csr = 0;
  407. return -EAGAIN;
  408. }
  409. dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
  410. i2c_imx->i2csr = 0;
  411. return 0;
  412. }
  413. static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
  414. {
  415. if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) {
  416. dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
  417. return -ENXIO; /* No ACK */
  418. }
  419. dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
  420. return 0;
  421. }
  422. static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
  423. unsigned int i2c_clk_rate)
  424. {
  425. struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
  426. unsigned int div;
  427. int i;
  428. /* Divider value calculation */
  429. if (i2c_imx->cur_clk == i2c_clk_rate)
  430. return;
  431. i2c_imx->cur_clk = i2c_clk_rate;
  432. div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate;
  433. if (div < i2c_clk_div[0].div)
  434. i = 0;
  435. else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
  436. i = i2c_imx->hwdata->ndivs - 1;
  437. else
  438. for (i = 0; i2c_clk_div[i].div < div; i++)
  439. ;
  440. /* Store divider value */
  441. i2c_imx->ifdr = i2c_clk_div[i].val;
  442. /*
  443. * There dummy delay is calculated.
  444. * It should be about one I2C clock period long.
  445. * This delay is used in I2C bus disable function
  446. * to fix chip hardware bug.
  447. */
  448. i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
  449. + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
  450. #ifdef CONFIG_I2C_DEBUG_BUS
  451. dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n",
  452. i2c_clk_rate, div);
  453. dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
  454. i2c_clk_div[i].val, i2c_clk_div[i].div);
  455. #endif
  456. }
  457. static int i2c_imx_clk_notifier_call(struct notifier_block *nb,
  458. unsigned long action, void *data)
  459. {
  460. struct clk_notifier_data *ndata = data;
  461. struct imx_i2c_struct *i2c_imx = container_of(nb,
  462. struct imx_i2c_struct,
  463. clk_change_nb);
  464. if (action & POST_RATE_CHANGE)
  465. i2c_imx_set_clk(i2c_imx, ndata->new_rate);
  466. return NOTIFY_OK;
  467. }
  468. static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
  469. {
  470. unsigned int temp = 0;
  471. int result;
  472. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  473. imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
  474. /* Enable I2C controller */
  475. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  476. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR);
  477. /* Wait controller to be stable */
  478. usleep_range(50, 150);
  479. /* Start I2C transaction */
  480. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  481. temp |= I2CR_MSTA;
  482. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  483. result = i2c_imx_bus_busy(i2c_imx, 1);
  484. if (result)
  485. return result;
  486. temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
  487. temp &= ~I2CR_DMAEN;
  488. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  489. return result;
  490. }
  491. static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
  492. {
  493. unsigned int temp = 0;
  494. if (!i2c_imx->stopped) {
  495. /* Stop I2C transaction */
  496. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  497. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  498. if (!(temp & I2CR_MSTA))
  499. i2c_imx->stopped = 1;
  500. temp &= ~(I2CR_MSTA | I2CR_MTX);
  501. if (i2c_imx->dma)
  502. temp &= ~I2CR_DMAEN;
  503. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  504. }
  505. if (is_imx1_i2c(i2c_imx)) {
  506. /*
  507. * This delay caused by an i.MXL hardware bug.
  508. * If no (or too short) delay, no "STOP" bit will be generated.
  509. */
  510. udelay(i2c_imx->disable_delay);
  511. }
  512. if (!i2c_imx->stopped)
  513. i2c_imx_bus_busy(i2c_imx, 0);
  514. /* Disable I2C controller */
  515. temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  516. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  517. }
  518. static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
  519. {
  520. struct imx_i2c_struct *i2c_imx = dev_id;
  521. unsigned int temp;
  522. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  523. if (temp & I2SR_IIF) {
  524. /* save status register */
  525. i2c_imx->i2csr = temp;
  526. i2c_imx_clear_irq(i2c_imx, I2SR_IIF);
  527. wake_up(&i2c_imx->queue);
  528. return IRQ_HANDLED;
  529. }
  530. return IRQ_NONE;
  531. }
  532. static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
  533. struct i2c_msg *msgs)
  534. {
  535. int result;
  536. unsigned long time_left;
  537. unsigned int temp = 0;
  538. unsigned long orig_jiffies = jiffies;
  539. struct imx_i2c_dma *dma = i2c_imx->dma;
  540. struct device *dev = &i2c_imx->adapter.dev;
  541. dma->chan_using = dma->chan_tx;
  542. dma->dma_transfer_dir = DMA_MEM_TO_DEV;
  543. dma->dma_data_dir = DMA_TO_DEVICE;
  544. dma->dma_len = msgs->len - 1;
  545. result = i2c_imx_dma_xfer(i2c_imx, msgs);
  546. if (result)
  547. return result;
  548. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  549. temp |= I2CR_DMAEN;
  550. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  551. /*
  552. * Write slave address.
  553. * The first byte must be transmitted by the CPU.
  554. */
  555. imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
  556. time_left = wait_for_completion_timeout(
  557. &i2c_imx->dma->cmd_complete,
  558. msecs_to_jiffies(DMA_TIMEOUT));
  559. if (time_left == 0) {
  560. dmaengine_terminate_all(dma->chan_using);
  561. return -ETIMEDOUT;
  562. }
  563. /* Waiting for transfer complete. */
  564. while (1) {
  565. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  566. if (temp & I2SR_ICF)
  567. break;
  568. if (time_after(jiffies, orig_jiffies +
  569. msecs_to_jiffies(DMA_TIMEOUT))) {
  570. dev_dbg(dev, "<%s> Timeout\n", __func__);
  571. return -ETIMEDOUT;
  572. }
  573. schedule();
  574. }
  575. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  576. temp &= ~I2CR_DMAEN;
  577. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  578. /* The last data byte must be transferred by the CPU. */
  579. imx_i2c_write_reg(msgs->buf[msgs->len-1],
  580. i2c_imx, IMX_I2C_I2DR);
  581. result = i2c_imx_trx_complete(i2c_imx);
  582. if (result)
  583. return result;
  584. return i2c_imx_acked(i2c_imx);
  585. }
  586. static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
  587. struct i2c_msg *msgs, bool is_lastmsg)
  588. {
  589. int result;
  590. unsigned long time_left;
  591. unsigned int temp;
  592. unsigned long orig_jiffies = jiffies;
  593. struct imx_i2c_dma *dma = i2c_imx->dma;
  594. struct device *dev = &i2c_imx->adapter.dev;
  595. dma->chan_using = dma->chan_rx;
  596. dma->dma_transfer_dir = DMA_DEV_TO_MEM;
  597. dma->dma_data_dir = DMA_FROM_DEVICE;
  598. /* The last two data bytes must be transferred by the CPU. */
  599. dma->dma_len = msgs->len - 2;
  600. result = i2c_imx_dma_xfer(i2c_imx, msgs);
  601. if (result)
  602. return result;
  603. time_left = wait_for_completion_timeout(
  604. &i2c_imx->dma->cmd_complete,
  605. msecs_to_jiffies(DMA_TIMEOUT));
  606. if (time_left == 0) {
  607. dmaengine_terminate_all(dma->chan_using);
  608. return -ETIMEDOUT;
  609. }
  610. /* waiting for transfer complete. */
  611. while (1) {
  612. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  613. if (temp & I2SR_ICF)
  614. break;
  615. if (time_after(jiffies, orig_jiffies +
  616. msecs_to_jiffies(DMA_TIMEOUT))) {
  617. dev_dbg(dev, "<%s> Timeout\n", __func__);
  618. return -ETIMEDOUT;
  619. }
  620. schedule();
  621. }
  622. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  623. temp &= ~I2CR_DMAEN;
  624. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  625. /* read n-1 byte data */
  626. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  627. temp |= I2CR_TXAK;
  628. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  629. msgs->buf[msgs->len-2] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  630. /* read n byte data */
  631. result = i2c_imx_trx_complete(i2c_imx);
  632. if (result)
  633. return result;
  634. if (is_lastmsg) {
  635. /*
  636. * It must generate STOP before read I2DR to prevent
  637. * controller from generating another clock cycle
  638. */
  639. dev_dbg(dev, "<%s> clear MSTA\n", __func__);
  640. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  641. if (!(temp & I2CR_MSTA))
  642. i2c_imx->stopped = 1;
  643. temp &= ~(I2CR_MSTA | I2CR_MTX);
  644. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  645. if (!i2c_imx->stopped)
  646. i2c_imx_bus_busy(i2c_imx, 0);
  647. } else {
  648. /*
  649. * For i2c master receiver repeat restart operation like:
  650. * read -> repeat MSTA -> read/write
  651. * The controller must set MTX before read the last byte in
  652. * the first read operation, otherwise the first read cost
  653. * one extra clock cycle.
  654. */
  655. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  656. temp |= I2CR_MTX;
  657. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  658. }
  659. msgs->buf[msgs->len-1] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  660. return 0;
  661. }
  662. static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
  663. {
  664. int i, result;
  665. dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
  666. __func__, i2c_8bit_addr_from_msg(msgs));
  667. /* write slave address */
  668. imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
  669. result = i2c_imx_trx_complete(i2c_imx);
  670. if (result)
  671. return result;
  672. result = i2c_imx_acked(i2c_imx);
  673. if (result)
  674. return result;
  675. dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
  676. /* write data */
  677. for (i = 0; i < msgs->len; i++) {
  678. dev_dbg(&i2c_imx->adapter.dev,
  679. "<%s> write byte: B%d=0x%X\n",
  680. __func__, i, msgs->buf[i]);
  681. imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
  682. result = i2c_imx_trx_complete(i2c_imx);
  683. if (result)
  684. return result;
  685. result = i2c_imx_acked(i2c_imx);
  686. if (result)
  687. return result;
  688. }
  689. return 0;
  690. }
  691. static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
  692. {
  693. int i, result;
  694. unsigned int temp;
  695. int block_data = msgs->flags & I2C_M_RECV_LEN;
  696. int use_dma = i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data;
  697. dev_dbg(&i2c_imx->adapter.dev,
  698. "<%s> write slave address: addr=0x%x\n",
  699. __func__, i2c_8bit_addr_from_msg(msgs));
  700. /* write slave address */
  701. imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
  702. result = i2c_imx_trx_complete(i2c_imx);
  703. if (result)
  704. return result;
  705. result = i2c_imx_acked(i2c_imx);
  706. if (result)
  707. return result;
  708. dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
  709. /* setup bus to read data */
  710. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  711. temp &= ~I2CR_MTX;
  712. /*
  713. * Reset the I2CR_TXAK flag initially for SMBus block read since the
  714. * length is unknown
  715. */
  716. if ((msgs->len - 1) || block_data)
  717. temp &= ~I2CR_TXAK;
  718. if (use_dma)
  719. temp |= I2CR_DMAEN;
  720. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  721. imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
  722. dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
  723. if (use_dma)
  724. return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg);
  725. /* read data */
  726. for (i = 0; i < msgs->len; i++) {
  727. u8 len = 0;
  728. result = i2c_imx_trx_complete(i2c_imx);
  729. if (result)
  730. return result;
  731. /*
  732. * First byte is the length of remaining packet
  733. * in the SMBus block data read. Add it to
  734. * msgs->len.
  735. */
  736. if ((!i) && block_data) {
  737. len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  738. if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX))
  739. return -EPROTO;
  740. dev_dbg(&i2c_imx->adapter.dev,
  741. "<%s> read length: 0x%X\n",
  742. __func__, len);
  743. msgs->len += len;
  744. }
  745. if (i == (msgs->len - 1)) {
  746. if (is_lastmsg) {
  747. /*
  748. * It must generate STOP before read I2DR to prevent
  749. * controller from generating another clock cycle
  750. */
  751. dev_dbg(&i2c_imx->adapter.dev,
  752. "<%s> clear MSTA\n", __func__);
  753. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  754. if (!(temp & I2CR_MSTA))
  755. i2c_imx->stopped = 1;
  756. temp &= ~(I2CR_MSTA | I2CR_MTX);
  757. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  758. if (!i2c_imx->stopped)
  759. i2c_imx_bus_busy(i2c_imx, 0);
  760. } else {
  761. /*
  762. * For i2c master receiver repeat restart operation like:
  763. * read -> repeat MSTA -> read/write
  764. * The controller must set MTX before read the last byte in
  765. * the first read operation, otherwise the first read cost
  766. * one extra clock cycle.
  767. */
  768. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  769. temp |= I2CR_MTX;
  770. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  771. }
  772. } else if (i == (msgs->len - 2)) {
  773. dev_dbg(&i2c_imx->adapter.dev,
  774. "<%s> set TXAK\n", __func__);
  775. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  776. temp |= I2CR_TXAK;
  777. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  778. }
  779. if ((!i) && block_data)
  780. msgs->buf[0] = len;
  781. else
  782. msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  783. dev_dbg(&i2c_imx->adapter.dev,
  784. "<%s> read byte: B%d=0x%X\n",
  785. __func__, i, msgs->buf[i]);
  786. }
  787. return 0;
  788. }
  789. static int i2c_imx_xfer(struct i2c_adapter *adapter,
  790. struct i2c_msg *msgs, int num)
  791. {
  792. unsigned int i, temp;
  793. int result;
  794. bool is_lastmsg = false;
  795. struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
  796. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  797. result = pm_runtime_get_sync(i2c_imx->adapter.dev.parent);
  798. if (result < 0)
  799. goto out;
  800. /* Start I2C transfer */
  801. result = i2c_imx_start(i2c_imx);
  802. if (result) {
  803. if (i2c_imx->adapter.bus_recovery_info) {
  804. i2c_recover_bus(&i2c_imx->adapter);
  805. result = i2c_imx_start(i2c_imx);
  806. }
  807. }
  808. if (result)
  809. goto fail0;
  810. /* read/write data */
  811. for (i = 0; i < num; i++) {
  812. if (i == num - 1)
  813. is_lastmsg = true;
  814. if (i) {
  815. dev_dbg(&i2c_imx->adapter.dev,
  816. "<%s> repeated start\n", __func__);
  817. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  818. temp |= I2CR_RSTA;
  819. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  820. result = i2c_imx_bus_busy(i2c_imx, 1);
  821. if (result)
  822. goto fail0;
  823. }
  824. dev_dbg(&i2c_imx->adapter.dev,
  825. "<%s> transfer message: %d\n", __func__, i);
  826. /* write/read data */
  827. #ifdef CONFIG_I2C_DEBUG_BUS
  828. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  829. dev_dbg(&i2c_imx->adapter.dev,
  830. "<%s> CONTROL: IEN=%d, IIEN=%d, MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n",
  831. __func__,
  832. (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
  833. (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
  834. (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
  835. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  836. dev_dbg(&i2c_imx->adapter.dev,
  837. "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n",
  838. __func__,
  839. (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
  840. (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
  841. (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
  842. (temp & I2SR_RXAK ? 1 : 0));
  843. #endif
  844. if (msgs[i].flags & I2C_M_RD)
  845. result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
  846. else {
  847. if (i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD)
  848. result = i2c_imx_dma_write(i2c_imx, &msgs[i]);
  849. else
  850. result = i2c_imx_write(i2c_imx, &msgs[i]);
  851. }
  852. if (result)
  853. goto fail0;
  854. }
  855. fail0:
  856. /* Stop I2C transfer */
  857. i2c_imx_stop(i2c_imx);
  858. pm_runtime_mark_last_busy(i2c_imx->adapter.dev.parent);
  859. pm_runtime_put_autosuspend(i2c_imx->adapter.dev.parent);
  860. out:
  861. dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
  862. (result < 0) ? "error" : "success msg",
  863. (result < 0) ? result : num);
  864. return (result < 0) ? result : num;
  865. }
  866. static void i2c_imx_prepare_recovery(struct i2c_adapter *adap)
  867. {
  868. struct imx_i2c_struct *i2c_imx;
  869. i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
  870. pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_gpio);
  871. }
  872. static void i2c_imx_unprepare_recovery(struct i2c_adapter *adap)
  873. {
  874. struct imx_i2c_struct *i2c_imx;
  875. i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
  876. pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_default);
  877. }
  878. /*
  879. * We switch SCL and SDA to their GPIO function and do some bitbanging
  880. * for bus recovery. These alternative pinmux settings can be
  881. * described in the device tree by a separate pinctrl state "gpio". If
  882. * this is missing this is not a big problem, the only implication is
  883. * that we can't do bus recovery.
  884. */
  885. static int i2c_imx_init_recovery_info(struct imx_i2c_struct *i2c_imx,
  886. struct platform_device *pdev)
  887. {
  888. struct i2c_bus_recovery_info *rinfo = &i2c_imx->rinfo;
  889. i2c_imx->pinctrl = devm_pinctrl_get(&pdev->dev);
  890. if (!i2c_imx->pinctrl || IS_ERR(i2c_imx->pinctrl)) {
  891. dev_info(&pdev->dev, "can't get pinctrl, bus recovery not supported\n");
  892. return PTR_ERR(i2c_imx->pinctrl);
  893. }
  894. i2c_imx->pinctrl_pins_default = pinctrl_lookup_state(i2c_imx->pinctrl,
  895. PINCTRL_STATE_DEFAULT);
  896. i2c_imx->pinctrl_pins_gpio = pinctrl_lookup_state(i2c_imx->pinctrl,
  897. "gpio");
  898. rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN);
  899. rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
  900. if (PTR_ERR(rinfo->sda_gpiod) == -EPROBE_DEFER ||
  901. PTR_ERR(rinfo->scl_gpiod) == -EPROBE_DEFER) {
  902. return -EPROBE_DEFER;
  903. } else if (IS_ERR(rinfo->sda_gpiod) ||
  904. IS_ERR(rinfo->scl_gpiod) ||
  905. IS_ERR(i2c_imx->pinctrl_pins_default) ||
  906. IS_ERR(i2c_imx->pinctrl_pins_gpio)) {
  907. dev_dbg(&pdev->dev, "recovery information incomplete\n");
  908. return 0;
  909. }
  910. dev_dbg(&pdev->dev, "using scl%s for recovery\n",
  911. rinfo->sda_gpiod ? ",sda" : "");
  912. rinfo->prepare_recovery = i2c_imx_prepare_recovery;
  913. rinfo->unprepare_recovery = i2c_imx_unprepare_recovery;
  914. rinfo->recover_bus = i2c_generic_scl_recovery;
  915. i2c_imx->adapter.bus_recovery_info = rinfo;
  916. return 0;
  917. }
  918. static u32 i2c_imx_func(struct i2c_adapter *adapter)
  919. {
  920. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  921. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  922. }
  923. static const struct i2c_algorithm i2c_imx_algo = {
  924. .master_xfer = i2c_imx_xfer,
  925. .functionality = i2c_imx_func,
  926. };
  927. static int i2c_imx_probe(struct platform_device *pdev)
  928. {
  929. const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
  930. &pdev->dev);
  931. struct imx_i2c_struct *i2c_imx;
  932. struct resource *res;
  933. struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
  934. void __iomem *base;
  935. int irq, ret;
  936. dma_addr_t phy_addr;
  937. dev_dbg(&pdev->dev, "<%s>\n", __func__);
  938. irq = platform_get_irq(pdev, 0);
  939. if (irq < 0) {
  940. dev_err(&pdev->dev, "can't get irq number\n");
  941. return irq;
  942. }
  943. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  944. base = devm_ioremap_resource(&pdev->dev, res);
  945. if (IS_ERR(base))
  946. return PTR_ERR(base);
  947. phy_addr = (dma_addr_t)res->start;
  948. i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL);
  949. if (!i2c_imx)
  950. return -ENOMEM;
  951. if (of_id)
  952. i2c_imx->hwdata = of_id->data;
  953. else
  954. i2c_imx->hwdata = (struct imx_i2c_hwdata *)
  955. platform_get_device_id(pdev)->driver_data;
  956. /* Setup i2c_imx driver structure */
  957. strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
  958. i2c_imx->adapter.owner = THIS_MODULE;
  959. i2c_imx->adapter.algo = &i2c_imx_algo;
  960. i2c_imx->adapter.dev.parent = &pdev->dev;
  961. i2c_imx->adapter.nr = pdev->id;
  962. i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
  963. i2c_imx->base = base;
  964. /* Get I2C clock */
  965. i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
  966. if (IS_ERR(i2c_imx->clk)) {
  967. if (PTR_ERR(i2c_imx->clk) != -EPROBE_DEFER)
  968. dev_err(&pdev->dev, "can't get I2C clock\n");
  969. return PTR_ERR(i2c_imx->clk);
  970. }
  971. ret = clk_prepare_enable(i2c_imx->clk);
  972. if (ret) {
  973. dev_err(&pdev->dev, "can't enable I2C clock, ret=%d\n", ret);
  974. return ret;
  975. }
  976. /* Init queue */
  977. init_waitqueue_head(&i2c_imx->queue);
  978. /* Set up adapter data */
  979. i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
  980. /* Set up platform driver data */
  981. platform_set_drvdata(pdev, i2c_imx);
  982. pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT);
  983. pm_runtime_use_autosuspend(&pdev->dev);
  984. pm_runtime_set_active(&pdev->dev);
  985. pm_runtime_enable(&pdev->dev);
  986. ret = pm_runtime_get_sync(&pdev->dev);
  987. if (ret < 0)
  988. goto rpm_disable;
  989. /* Request IRQ */
  990. ret = request_threaded_irq(irq, i2c_imx_isr, NULL, IRQF_SHARED,
  991. pdev->name, i2c_imx);
  992. if (ret) {
  993. dev_err(&pdev->dev, "can't claim irq %d\n", irq);
  994. goto rpm_disable;
  995. }
  996. /* Set up clock divider */
  997. i2c_imx->bitrate = IMX_I2C_BIT_RATE;
  998. ret = of_property_read_u32(pdev->dev.of_node,
  999. "clock-frequency", &i2c_imx->bitrate);
  1000. if (ret < 0 && pdata && pdata->bitrate)
  1001. i2c_imx->bitrate = pdata->bitrate;
  1002. i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call;
  1003. clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb);
  1004. i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
  1005. /* Set up chip registers to defaults */
  1006. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  1007. i2c_imx, IMX_I2C_I2CR);
  1008. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  1009. /* Init optional bus recovery function */
  1010. ret = i2c_imx_init_recovery_info(i2c_imx, pdev);
  1011. /* Give it another chance if pinctrl used is not ready yet */
  1012. if (ret == -EPROBE_DEFER)
  1013. goto clk_notifier_unregister;
  1014. /* Add I2C adapter */
  1015. ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
  1016. if (ret < 0)
  1017. goto clk_notifier_unregister;
  1018. pm_runtime_mark_last_busy(&pdev->dev);
  1019. pm_runtime_put_autosuspend(&pdev->dev);
  1020. dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
  1021. dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res);
  1022. dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
  1023. i2c_imx->adapter.name);
  1024. dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
  1025. /* Init DMA config if supported */
  1026. i2c_imx_dma_request(i2c_imx, phy_addr);
  1027. return 0; /* Return OK */
  1028. clk_notifier_unregister:
  1029. clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb);
  1030. free_irq(irq, i2c_imx);
  1031. rpm_disable:
  1032. pm_runtime_put_noidle(&pdev->dev);
  1033. pm_runtime_disable(&pdev->dev);
  1034. pm_runtime_set_suspended(&pdev->dev);
  1035. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1036. clk_disable_unprepare(i2c_imx->clk);
  1037. return ret;
  1038. }
  1039. static int i2c_imx_remove(struct platform_device *pdev)
  1040. {
  1041. struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
  1042. int irq, ret;
  1043. ret = pm_runtime_get_sync(&pdev->dev);
  1044. if (ret < 0)
  1045. return ret;
  1046. /* remove adapter */
  1047. dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
  1048. i2c_del_adapter(&i2c_imx->adapter);
  1049. if (i2c_imx->dma)
  1050. i2c_imx_dma_free(i2c_imx);
  1051. /* setup chip registers to defaults */
  1052. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
  1053. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);
  1054. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR);
  1055. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR);
  1056. clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb);
  1057. irq = platform_get_irq(pdev, 0);
  1058. if (irq >= 0)
  1059. free_irq(irq, i2c_imx);
  1060. clk_disable_unprepare(i2c_imx->clk);
  1061. pm_runtime_put_noidle(&pdev->dev);
  1062. pm_runtime_disable(&pdev->dev);
  1063. return 0;
  1064. }
  1065. #ifdef CONFIG_PM
  1066. static int i2c_imx_runtime_suspend(struct device *dev)
  1067. {
  1068. struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
  1069. clk_disable(i2c_imx->clk);
  1070. return 0;
  1071. }
  1072. static int i2c_imx_runtime_resume(struct device *dev)
  1073. {
  1074. struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
  1075. int ret;
  1076. ret = clk_enable(i2c_imx->clk);
  1077. if (ret)
  1078. dev_err(dev, "can't enable I2C clock, ret=%d\n", ret);
  1079. return ret;
  1080. }
  1081. static const struct dev_pm_ops i2c_imx_pm_ops = {
  1082. SET_RUNTIME_PM_OPS(i2c_imx_runtime_suspend,
  1083. i2c_imx_runtime_resume, NULL)
  1084. };
  1085. #define I2C_IMX_PM_OPS (&i2c_imx_pm_ops)
  1086. #else
  1087. #define I2C_IMX_PM_OPS NULL
  1088. #endif /* CONFIG_PM */
  1089. static struct platform_driver i2c_imx_driver = {
  1090. .probe = i2c_imx_probe,
  1091. .remove = i2c_imx_remove,
  1092. .driver = {
  1093. .name = DRIVER_NAME,
  1094. .pm = I2C_IMX_PM_OPS,
  1095. .of_match_table = i2c_imx_dt_ids,
  1096. },
  1097. .id_table = imx_i2c_devtype,
  1098. };
  1099. static int __init i2c_adap_imx_init(void)
  1100. {
  1101. return platform_driver_register(&i2c_imx_driver);
  1102. }
  1103. subsys_initcall(i2c_adap_imx_init);
  1104. static void __exit i2c_adap_imx_exit(void)
  1105. {
  1106. platform_driver_unregister(&i2c_imx_driver);
  1107. }
  1108. module_exit(i2c_adap_imx_exit);
  1109. MODULE_LICENSE("GPL");
  1110. MODULE_AUTHOR("Darius Augulis");
  1111. MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
  1112. MODULE_ALIAS("platform:" DRIVER_NAME);