i2c-imx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * Copyright (C) 2002 Motorola GSG-China
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. *
  19. * Author:
  20. * Darius Augulis, Teltonika Inc.
  21. *
  22. * Desc.:
  23. * Implementation of I2C Adapter/Algorithm Driver
  24. * for I2C Bus integrated in Freescale i.MX/MXC processors
  25. *
  26. * Derived from Motorola GSG China I2C example driver
  27. *
  28. * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de
  29. * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de
  30. * Copyright (C) 2007 RightHand Technologies, Inc.
  31. * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
  32. *
  33. * Copyright 2013 Freescale Semiconductor, Inc.
  34. *
  35. */
  36. /** Includes *******************************************************************
  37. *******************************************************************************/
  38. #include <linux/init.h>
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/err.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/delay.h>
  45. #include <linux/i2c.h>
  46. #include <linux/io.h>
  47. #include <linux/sched.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/clk.h>
  50. #include <linux/slab.h>
  51. #include <linux/of.h>
  52. #include <linux/of_device.h>
  53. #include <linux/platform_data/i2c-imx.h>
  54. /** Defines ********************************************************************
  55. *******************************************************************************/
  56. /* This will be the driver name the kernel reports */
  57. #define DRIVER_NAME "imx-i2c"
  58. /* Default value */
  59. #define IMX_I2C_BIT_RATE 100000 /* 100kHz */
  60. /* IMX I2C registers:
  61. * the I2C register offset is different between SoCs,
  62. * to provid support for all these chips, split the
  63. * register offset into a fixed base address and a
  64. * variable shift value, then the full register offset
  65. * will be calculated by
  66. * reg_off = ( reg_base_addr << reg_shift)
  67. */
  68. #define IMX_I2C_IADR 0x00 /* i2c slave address */
  69. #define IMX_I2C_IFDR 0x01 /* i2c frequency divider */
  70. #define IMX_I2C_I2CR 0x02 /* i2c control */
  71. #define IMX_I2C_I2SR 0x03 /* i2c status */
  72. #define IMX_I2C_I2DR 0x04 /* i2c transfer data */
  73. #define IMX_I2C_REGSHIFT 2
  74. #define VF610_I2C_REGSHIFT 0
  75. /* Bits of IMX I2C registers */
  76. #define I2SR_RXAK 0x01
  77. #define I2SR_IIF 0x02
  78. #define I2SR_SRW 0x04
  79. #define I2SR_IAL 0x10
  80. #define I2SR_IBB 0x20
  81. #define I2SR_IAAS 0x40
  82. #define I2SR_ICF 0x80
  83. #define I2CR_RSTA 0x04
  84. #define I2CR_TXAK 0x08
  85. #define I2CR_MTX 0x10
  86. #define I2CR_MSTA 0x20
  87. #define I2CR_IIEN 0x40
  88. #define I2CR_IEN 0x80
  89. /* register bits different operating codes definition:
  90. * 1) I2SR: Interrupt flags clear operation differ between SoCs:
  91. * - write zero to clear(w0c) INT flag on i.MX,
  92. * - but write one to clear(w1c) INT flag on Vybrid.
  93. * 2) I2CR: I2C module enable operation also differ between SoCs:
  94. * - set I2CR_IEN bit enable the module on i.MX,
  95. * - but clear I2CR_IEN bit enable the module on Vybrid.
  96. */
  97. #define I2SR_CLR_OPCODE_W0C 0x0
  98. #define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF)
  99. #define I2CR_IEN_OPCODE_0 0x0
  100. #define I2CR_IEN_OPCODE_1 I2CR_IEN
  101. /** Variables ******************************************************************
  102. *******************************************************************************/
  103. /*
  104. * sorted list of clock divider, register value pairs
  105. * taken from table 26-5, p.26-9, Freescale i.MX
  106. * Integrated Portable System Processor Reference Manual
  107. * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
  108. *
  109. * Duplicated divider values removed from list
  110. */
  111. struct imx_i2c_clk_pair {
  112. u16 div;
  113. u16 val;
  114. };
  115. static struct imx_i2c_clk_pair imx_i2c_clk_div[] = {
  116. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  117. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  118. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  119. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  120. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  121. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  122. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  123. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  124. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  125. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  126. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  127. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  128. { 3072, 0x1E }, { 3840, 0x1F }
  129. };
  130. /* Vybrid VF610 clock divider, register value pairs */
  131. static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = {
  132. { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
  133. { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
  134. { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
  135. { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
  136. { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
  137. { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
  138. { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
  139. { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
  140. { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
  141. { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
  142. { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
  143. { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
  144. { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
  145. { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
  146. { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
  147. };
  148. enum imx_i2c_type {
  149. IMX1_I2C,
  150. IMX21_I2C,
  151. VF610_I2C,
  152. };
  153. struct imx_i2c_hwdata {
  154. enum imx_i2c_type devtype;
  155. unsigned regshift;
  156. struct imx_i2c_clk_pair *clk_div;
  157. unsigned ndivs;
  158. unsigned i2sr_clr_opcode;
  159. unsigned i2cr_ien_opcode;
  160. };
  161. struct imx_i2c_struct {
  162. struct i2c_adapter adapter;
  163. struct clk *clk;
  164. void __iomem *base;
  165. wait_queue_head_t queue;
  166. unsigned long i2csr;
  167. unsigned int disable_delay;
  168. int stopped;
  169. unsigned int ifdr; /* IMX_I2C_IFDR */
  170. unsigned int cur_clk;
  171. unsigned int bitrate;
  172. const struct imx_i2c_hwdata *hwdata;
  173. };
  174. static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
  175. .devtype = IMX1_I2C,
  176. .regshift = IMX_I2C_REGSHIFT,
  177. .clk_div = imx_i2c_clk_div,
  178. .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
  179. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
  180. .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
  181. };
  182. static const struct imx_i2c_hwdata imx21_i2c_hwdata = {
  183. .devtype = IMX21_I2C,
  184. .regshift = IMX_I2C_REGSHIFT,
  185. .clk_div = imx_i2c_clk_div,
  186. .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
  187. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
  188. .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
  189. };
  190. static struct imx_i2c_hwdata vf610_i2c_hwdata = {
  191. .devtype = VF610_I2C,
  192. .regshift = VF610_I2C_REGSHIFT,
  193. .clk_div = vf610_i2c_clk_div,
  194. .ndivs = ARRAY_SIZE(vf610_i2c_clk_div),
  195. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C,
  196. .i2cr_ien_opcode = I2CR_IEN_OPCODE_0,
  197. };
  198. static struct platform_device_id imx_i2c_devtype[] = {
  199. {
  200. .name = "imx1-i2c",
  201. .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata,
  202. }, {
  203. .name = "imx21-i2c",
  204. .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata,
  205. }, {
  206. /* sentinel */
  207. }
  208. };
  209. MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
  210. static const struct of_device_id i2c_imx_dt_ids[] = {
  211. { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, },
  212. { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, },
  213. { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, },
  214. { /* sentinel */ }
  215. };
  216. MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids);
  217. static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx)
  218. {
  219. return i2c_imx->hwdata->devtype == IMX1_I2C;
  220. }
  221. static inline void imx_i2c_write_reg(unsigned int val,
  222. struct imx_i2c_struct *i2c_imx, unsigned int reg)
  223. {
  224. writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
  225. }
  226. static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx,
  227. unsigned int reg)
  228. {
  229. return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
  230. }
  231. /** Functions for IMX I2C adapter driver ***************************************
  232. *******************************************************************************/
  233. static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
  234. {
  235. unsigned long orig_jiffies = jiffies;
  236. unsigned int temp;
  237. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  238. while (1) {
  239. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  240. /* check for arbitration lost */
  241. if (temp & I2SR_IAL) {
  242. temp &= ~I2SR_IAL;
  243. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  244. return -EAGAIN;
  245. }
  246. if (for_busy && (temp & I2SR_IBB))
  247. break;
  248. if (!for_busy && !(temp & I2SR_IBB))
  249. break;
  250. if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
  251. dev_dbg(&i2c_imx->adapter.dev,
  252. "<%s> I2C bus is busy\n", __func__);
  253. return -ETIMEDOUT;
  254. }
  255. schedule();
  256. }
  257. return 0;
  258. }
  259. static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
  260. {
  261. wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
  262. if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
  263. dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
  264. return -ETIMEDOUT;
  265. }
  266. dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
  267. i2c_imx->i2csr = 0;
  268. return 0;
  269. }
  270. static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
  271. {
  272. if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) {
  273. dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
  274. return -EIO; /* No ACK */
  275. }
  276. dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
  277. return 0;
  278. }
  279. static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx)
  280. {
  281. struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
  282. unsigned int i2c_clk_rate;
  283. unsigned int div;
  284. int i;
  285. /* Divider value calculation */
  286. i2c_clk_rate = clk_get_rate(i2c_imx->clk);
  287. if (i2c_imx->cur_clk == i2c_clk_rate)
  288. return;
  289. else
  290. i2c_imx->cur_clk = i2c_clk_rate;
  291. div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate;
  292. if (div < i2c_clk_div[0].div)
  293. i = 0;
  294. else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
  295. i = i2c_imx->hwdata->ndivs - 1;
  296. else
  297. for (i = 0; i2c_clk_div[i].div < div; i++);
  298. /* Store divider value */
  299. i2c_imx->ifdr = i2c_clk_div[i].val;
  300. /*
  301. * There dummy delay is calculated.
  302. * It should be about one I2C clock period long.
  303. * This delay is used in I2C bus disable function
  304. * to fix chip hardware bug.
  305. */
  306. i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
  307. + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
  308. #ifdef CONFIG_I2C_DEBUG_BUS
  309. dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n",
  310. i2c_clk_rate, div);
  311. dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
  312. i2c_clk_div[i].val, i2c_clk_div[i].div);
  313. #endif
  314. }
  315. static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
  316. {
  317. unsigned int temp = 0;
  318. int result;
  319. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  320. i2c_imx_set_clk(i2c_imx);
  321. result = clk_prepare_enable(i2c_imx->clk);
  322. if (result)
  323. return result;
  324. imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
  325. /* Enable I2C controller */
  326. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  327. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR);
  328. /* Wait controller to be stable */
  329. udelay(50);
  330. /* Start I2C transaction */
  331. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  332. temp |= I2CR_MSTA;
  333. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  334. result = i2c_imx_bus_busy(i2c_imx, 1);
  335. if (result)
  336. return result;
  337. i2c_imx->stopped = 0;
  338. temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
  339. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  340. return result;
  341. }
  342. static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
  343. {
  344. unsigned int temp = 0;
  345. if (!i2c_imx->stopped) {
  346. /* Stop I2C transaction */
  347. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  348. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  349. temp &= ~(I2CR_MSTA | I2CR_MTX);
  350. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  351. }
  352. if (is_imx1_i2c(i2c_imx)) {
  353. /*
  354. * This delay caused by an i.MXL hardware bug.
  355. * If no (or too short) delay, no "STOP" bit will be generated.
  356. */
  357. udelay(i2c_imx->disable_delay);
  358. }
  359. if (!i2c_imx->stopped) {
  360. i2c_imx_bus_busy(i2c_imx, 0);
  361. i2c_imx->stopped = 1;
  362. }
  363. /* Disable I2C controller */
  364. temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  365. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  366. clk_disable_unprepare(i2c_imx->clk);
  367. }
  368. static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
  369. {
  370. struct imx_i2c_struct *i2c_imx = dev_id;
  371. unsigned int temp;
  372. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  373. if (temp & I2SR_IIF) {
  374. /* save status register */
  375. i2c_imx->i2csr = temp;
  376. temp &= ~I2SR_IIF;
  377. temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
  378. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  379. wake_up(&i2c_imx->queue);
  380. return IRQ_HANDLED;
  381. }
  382. return IRQ_NONE;
  383. }
  384. static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
  385. {
  386. int i, result;
  387. dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
  388. __func__, msgs->addr << 1);
  389. /* write slave address */
  390. imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
  391. result = i2c_imx_trx_complete(i2c_imx);
  392. if (result)
  393. return result;
  394. result = i2c_imx_acked(i2c_imx);
  395. if (result)
  396. return result;
  397. dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
  398. /* write data */
  399. for (i = 0; i < msgs->len; i++) {
  400. dev_dbg(&i2c_imx->adapter.dev,
  401. "<%s> write byte: B%d=0x%X\n",
  402. __func__, i, msgs->buf[i]);
  403. imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
  404. result = i2c_imx_trx_complete(i2c_imx);
  405. if (result)
  406. return result;
  407. result = i2c_imx_acked(i2c_imx);
  408. if (result)
  409. return result;
  410. }
  411. return 0;
  412. }
  413. static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
  414. {
  415. int i, result;
  416. unsigned int temp;
  417. int block_data = msgs->flags & I2C_M_RECV_LEN;
  418. dev_dbg(&i2c_imx->adapter.dev,
  419. "<%s> write slave address: addr=0x%x\n",
  420. __func__, (msgs->addr << 1) | 0x01);
  421. /* write slave address */
  422. imx_i2c_write_reg((msgs->addr << 1) | 0x01, i2c_imx, IMX_I2C_I2DR);
  423. result = i2c_imx_trx_complete(i2c_imx);
  424. if (result)
  425. return result;
  426. result = i2c_imx_acked(i2c_imx);
  427. if (result)
  428. return result;
  429. dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
  430. /* setup bus to read data */
  431. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  432. temp &= ~I2CR_MTX;
  433. /*
  434. * Reset the I2CR_TXAK flag initially for SMBus block read since the
  435. * length is unknown
  436. */
  437. if ((msgs->len - 1) || block_data)
  438. temp &= ~I2CR_TXAK;
  439. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  440. imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
  441. dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
  442. /* read data */
  443. for (i = 0; i < msgs->len; i++) {
  444. u8 len = 0;
  445. result = i2c_imx_trx_complete(i2c_imx);
  446. if (result)
  447. return result;
  448. /*
  449. * First byte is the length of remaining packet
  450. * in the SMBus block data read. Add it to
  451. * msgs->len.
  452. */
  453. if ((!i) && block_data) {
  454. len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  455. if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX))
  456. return -EPROTO;
  457. dev_dbg(&i2c_imx->adapter.dev,
  458. "<%s> read length: 0x%X\n",
  459. __func__, len);
  460. msgs->len += len;
  461. }
  462. if (i == (msgs->len - 1)) {
  463. if (is_lastmsg) {
  464. /*
  465. * It must generate STOP before read I2DR to prevent
  466. * controller from generating another clock cycle
  467. */
  468. dev_dbg(&i2c_imx->adapter.dev,
  469. "<%s> clear MSTA\n", __func__);
  470. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  471. temp &= ~(I2CR_MSTA | I2CR_MTX);
  472. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  473. i2c_imx_bus_busy(i2c_imx, 0);
  474. i2c_imx->stopped = 1;
  475. } else {
  476. /*
  477. * For i2c master receiver repeat restart operation like:
  478. * read -> repeat MSTA -> read/write
  479. * The controller must set MTX before read the last byte in
  480. * the first read operation, otherwise the first read cost
  481. * one extra clock cycle.
  482. */
  483. temp = readb(i2c_imx->base + IMX_I2C_I2CR);
  484. temp |= I2CR_MTX;
  485. writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
  486. }
  487. } else if (i == (msgs->len - 2)) {
  488. dev_dbg(&i2c_imx->adapter.dev,
  489. "<%s> set TXAK\n", __func__);
  490. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  491. temp |= I2CR_TXAK;
  492. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  493. }
  494. if ((!i) && block_data)
  495. msgs->buf[0] = len;
  496. else
  497. msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  498. dev_dbg(&i2c_imx->adapter.dev,
  499. "<%s> read byte: B%d=0x%X\n",
  500. __func__, i, msgs->buf[i]);
  501. }
  502. return 0;
  503. }
  504. static int i2c_imx_xfer(struct i2c_adapter *adapter,
  505. struct i2c_msg *msgs, int num)
  506. {
  507. unsigned int i, temp;
  508. int result;
  509. bool is_lastmsg = false;
  510. struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
  511. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  512. /* Start I2C transfer */
  513. result = i2c_imx_start(i2c_imx);
  514. if (result)
  515. goto fail0;
  516. /* read/write data */
  517. for (i = 0; i < num; i++) {
  518. if (i == num - 1)
  519. is_lastmsg = true;
  520. if (i) {
  521. dev_dbg(&i2c_imx->adapter.dev,
  522. "<%s> repeated start\n", __func__);
  523. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  524. temp |= I2CR_RSTA;
  525. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  526. result = i2c_imx_bus_busy(i2c_imx, 1);
  527. if (result)
  528. goto fail0;
  529. }
  530. dev_dbg(&i2c_imx->adapter.dev,
  531. "<%s> transfer message: %d\n", __func__, i);
  532. /* write/read data */
  533. #ifdef CONFIG_I2C_DEBUG_BUS
  534. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  535. dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, "
  536. "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__,
  537. (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
  538. (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
  539. (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
  540. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  541. dev_dbg(&i2c_imx->adapter.dev,
  542. "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, "
  543. "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__,
  544. (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
  545. (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
  546. (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
  547. (temp & I2SR_RXAK ? 1 : 0));
  548. #endif
  549. if (msgs[i].flags & I2C_M_RD)
  550. result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
  551. else
  552. result = i2c_imx_write(i2c_imx, &msgs[i]);
  553. if (result)
  554. goto fail0;
  555. }
  556. fail0:
  557. /* Stop I2C transfer */
  558. i2c_imx_stop(i2c_imx);
  559. dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
  560. (result < 0) ? "error" : "success msg",
  561. (result < 0) ? result : num);
  562. return (result < 0) ? result : num;
  563. }
  564. static u32 i2c_imx_func(struct i2c_adapter *adapter)
  565. {
  566. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  567. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  568. }
  569. static struct i2c_algorithm i2c_imx_algo = {
  570. .master_xfer = i2c_imx_xfer,
  571. .functionality = i2c_imx_func,
  572. };
  573. static int i2c_imx_probe(struct platform_device *pdev)
  574. {
  575. const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
  576. &pdev->dev);
  577. struct imx_i2c_struct *i2c_imx;
  578. struct resource *res;
  579. struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
  580. void __iomem *base;
  581. int irq, ret;
  582. dev_dbg(&pdev->dev, "<%s>\n", __func__);
  583. irq = platform_get_irq(pdev, 0);
  584. if (irq < 0) {
  585. dev_err(&pdev->dev, "can't get irq number\n");
  586. return irq;
  587. }
  588. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  589. base = devm_ioremap_resource(&pdev->dev, res);
  590. if (IS_ERR(base))
  591. return PTR_ERR(base);
  592. i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct),
  593. GFP_KERNEL);
  594. if (!i2c_imx)
  595. return -ENOMEM;
  596. if (of_id)
  597. i2c_imx->hwdata = of_id->data;
  598. else
  599. i2c_imx->hwdata = (struct imx_i2c_hwdata *)
  600. platform_get_device_id(pdev)->driver_data;
  601. /* Setup i2c_imx driver structure */
  602. strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
  603. i2c_imx->adapter.owner = THIS_MODULE;
  604. i2c_imx->adapter.algo = &i2c_imx_algo;
  605. i2c_imx->adapter.dev.parent = &pdev->dev;
  606. i2c_imx->adapter.nr = pdev->id;
  607. i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
  608. i2c_imx->base = base;
  609. /* Get I2C clock */
  610. i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
  611. if (IS_ERR(i2c_imx->clk)) {
  612. dev_err(&pdev->dev, "can't get I2C clock\n");
  613. return PTR_ERR(i2c_imx->clk);
  614. }
  615. ret = clk_prepare_enable(i2c_imx->clk);
  616. if (ret) {
  617. dev_err(&pdev->dev, "can't enable I2C clock\n");
  618. return ret;
  619. }
  620. /* Request IRQ */
  621. ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0,
  622. pdev->name, i2c_imx);
  623. if (ret) {
  624. dev_err(&pdev->dev, "can't claim irq %d\n", irq);
  625. goto clk_disable;
  626. }
  627. /* Init queue */
  628. init_waitqueue_head(&i2c_imx->queue);
  629. /* Set up adapter data */
  630. i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
  631. /* Set up clock divider */
  632. i2c_imx->bitrate = IMX_I2C_BIT_RATE;
  633. ret = of_property_read_u32(pdev->dev.of_node,
  634. "clock-frequency", &i2c_imx->bitrate);
  635. if (ret < 0 && pdata && pdata->bitrate)
  636. i2c_imx->bitrate = pdata->bitrate;
  637. /* Set up chip registers to defaults */
  638. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  639. i2c_imx, IMX_I2C_I2CR);
  640. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  641. /* Add I2C adapter */
  642. ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
  643. if (ret < 0) {
  644. dev_err(&pdev->dev, "registration failed\n");
  645. goto clk_disable;
  646. }
  647. /* Set up platform driver data */
  648. platform_set_drvdata(pdev, i2c_imx);
  649. clk_disable_unprepare(i2c_imx->clk);
  650. dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
  651. dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res);
  652. dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
  653. i2c_imx->adapter.name);
  654. dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
  655. return 0; /* Return OK */
  656. clk_disable:
  657. clk_disable_unprepare(i2c_imx->clk);
  658. return ret;
  659. }
  660. static int i2c_imx_remove(struct platform_device *pdev)
  661. {
  662. struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
  663. /* remove adapter */
  664. dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
  665. i2c_del_adapter(&i2c_imx->adapter);
  666. /* setup chip registers to defaults */
  667. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
  668. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);
  669. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR);
  670. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR);
  671. return 0;
  672. }
  673. static struct platform_driver i2c_imx_driver = {
  674. .probe = i2c_imx_probe,
  675. .remove = i2c_imx_remove,
  676. .driver = {
  677. .name = DRIVER_NAME,
  678. .owner = THIS_MODULE,
  679. .of_match_table = i2c_imx_dt_ids,
  680. },
  681. .id_table = imx_i2c_devtype,
  682. };
  683. static int __init i2c_adap_imx_init(void)
  684. {
  685. return platform_driver_register(&i2c_imx_driver);
  686. }
  687. subsys_initcall(i2c_adap_imx_init);
  688. static void __exit i2c_adap_imx_exit(void)
  689. {
  690. platform_driver_unregister(&i2c_imx_driver);
  691. }
  692. module_exit(i2c_adap_imx_exit);
  693. MODULE_LICENSE("GPL");
  694. MODULE_AUTHOR("Darius Augulis");
  695. MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
  696. MODULE_ALIAS("platform:" DRIVER_NAME);