i2c-imx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. if (for_busy && (temp & I2SR_IBB))
  241. break;
  242. if (!for_busy && !(temp & I2SR_IBB))
  243. break;
  244. if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
  245. dev_dbg(&i2c_imx->adapter.dev,
  246. "<%s> I2C bus is busy\n", __func__);
  247. return -ETIMEDOUT;
  248. }
  249. schedule();
  250. }
  251. return 0;
  252. }
  253. static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
  254. {
  255. wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
  256. if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
  257. dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
  258. return -ETIMEDOUT;
  259. }
  260. dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
  261. i2c_imx->i2csr = 0;
  262. return 0;
  263. }
  264. static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
  265. {
  266. if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) {
  267. dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
  268. return -EIO; /* No ACK */
  269. }
  270. dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
  271. return 0;
  272. }
  273. static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx)
  274. {
  275. struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
  276. unsigned int i2c_clk_rate;
  277. unsigned int div;
  278. int i;
  279. /* Divider value calculation */
  280. i2c_clk_rate = clk_get_rate(i2c_imx->clk);
  281. if (i2c_imx->cur_clk == i2c_clk_rate)
  282. return;
  283. else
  284. i2c_imx->cur_clk = i2c_clk_rate;
  285. div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate;
  286. if (div < i2c_clk_div[0].div)
  287. i = 0;
  288. else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
  289. i = i2c_imx->hwdata->ndivs - 1;
  290. else
  291. for (i = 0; i2c_clk_div[i].div < div; i++);
  292. /* Store divider value */
  293. i2c_imx->ifdr = i2c_clk_div[i].val;
  294. /*
  295. * There dummy delay is calculated.
  296. * It should be about one I2C clock period long.
  297. * This delay is used in I2C bus disable function
  298. * to fix chip hardware bug.
  299. */
  300. i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
  301. + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
  302. #ifdef CONFIG_I2C_DEBUG_BUS
  303. dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n",
  304. i2c_clk_rate, div);
  305. dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
  306. i2c_clk_div[i].val, i2c_clk_div[i].div);
  307. #endif
  308. }
  309. static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
  310. {
  311. unsigned int temp = 0;
  312. int result;
  313. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  314. i2c_imx_set_clk(i2c_imx);
  315. result = clk_prepare_enable(i2c_imx->clk);
  316. if (result)
  317. return result;
  318. imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
  319. /* Enable I2C controller */
  320. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  321. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR);
  322. /* Wait controller to be stable */
  323. udelay(50);
  324. /* Start I2C transaction */
  325. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  326. temp |= I2CR_MSTA;
  327. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  328. result = i2c_imx_bus_busy(i2c_imx, 1);
  329. if (result)
  330. return result;
  331. i2c_imx->stopped = 0;
  332. temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
  333. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  334. return result;
  335. }
  336. static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
  337. {
  338. unsigned int temp = 0;
  339. if (!i2c_imx->stopped) {
  340. /* Stop I2C transaction */
  341. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  342. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  343. temp &= ~(I2CR_MSTA | I2CR_MTX);
  344. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  345. }
  346. if (is_imx1_i2c(i2c_imx)) {
  347. /*
  348. * This delay caused by an i.MXL hardware bug.
  349. * If no (or too short) delay, no "STOP" bit will be generated.
  350. */
  351. udelay(i2c_imx->disable_delay);
  352. }
  353. if (!i2c_imx->stopped) {
  354. i2c_imx_bus_busy(i2c_imx, 0);
  355. i2c_imx->stopped = 1;
  356. }
  357. /* Disable I2C controller */
  358. temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  359. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  360. clk_disable_unprepare(i2c_imx->clk);
  361. }
  362. static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
  363. {
  364. struct imx_i2c_struct *i2c_imx = dev_id;
  365. unsigned int temp;
  366. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  367. if (temp & I2SR_IIF) {
  368. /* save status register */
  369. i2c_imx->i2csr = temp;
  370. temp &= ~I2SR_IIF;
  371. temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
  372. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  373. wake_up(&i2c_imx->queue);
  374. return IRQ_HANDLED;
  375. }
  376. return IRQ_NONE;
  377. }
  378. static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
  379. {
  380. int i, result;
  381. dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
  382. __func__, msgs->addr << 1);
  383. /* write slave address */
  384. imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
  385. result = i2c_imx_trx_complete(i2c_imx);
  386. if (result)
  387. return result;
  388. result = i2c_imx_acked(i2c_imx);
  389. if (result)
  390. return result;
  391. dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
  392. /* write data */
  393. for (i = 0; i < msgs->len; i++) {
  394. dev_dbg(&i2c_imx->adapter.dev,
  395. "<%s> write byte: B%d=0x%X\n",
  396. __func__, i, msgs->buf[i]);
  397. imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
  398. result = i2c_imx_trx_complete(i2c_imx);
  399. if (result)
  400. return result;
  401. result = i2c_imx_acked(i2c_imx);
  402. if (result)
  403. return result;
  404. }
  405. return 0;
  406. }
  407. static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
  408. {
  409. int i, result;
  410. unsigned int temp;
  411. int block_data = msgs->flags & I2C_M_RECV_LEN;
  412. dev_dbg(&i2c_imx->adapter.dev,
  413. "<%s> write slave address: addr=0x%x\n",
  414. __func__, (msgs->addr << 1) | 0x01);
  415. /* write slave address */
  416. imx_i2c_write_reg((msgs->addr << 1) | 0x01, i2c_imx, IMX_I2C_I2DR);
  417. result = i2c_imx_trx_complete(i2c_imx);
  418. if (result)
  419. return result;
  420. result = i2c_imx_acked(i2c_imx);
  421. if (result)
  422. return result;
  423. dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
  424. /* setup bus to read data */
  425. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  426. temp &= ~I2CR_MTX;
  427. /*
  428. * Reset the I2CR_TXAK flag initially for SMBus block read since the
  429. * length is unknown
  430. */
  431. if ((msgs->len - 1) || block_data)
  432. temp &= ~I2CR_TXAK;
  433. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  434. imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
  435. dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
  436. /* read data */
  437. for (i = 0; i < msgs->len; i++) {
  438. u8 len = 0;
  439. result = i2c_imx_trx_complete(i2c_imx);
  440. if (result)
  441. return result;
  442. /*
  443. * First byte is the length of remaining packet
  444. * in the SMBus block data read. Add it to
  445. * msgs->len.
  446. */
  447. if ((!i) && block_data) {
  448. len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  449. if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX))
  450. return -EPROTO;
  451. dev_dbg(&i2c_imx->adapter.dev,
  452. "<%s> read length: 0x%X\n",
  453. __func__, len);
  454. msgs->len += len;
  455. }
  456. if (i == (msgs->len - 1)) {
  457. if (is_lastmsg) {
  458. /*
  459. * It must generate STOP before read I2DR to prevent
  460. * controller from generating another clock cycle
  461. */
  462. dev_dbg(&i2c_imx->adapter.dev,
  463. "<%s> clear MSTA\n", __func__);
  464. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  465. temp &= ~(I2CR_MSTA | I2CR_MTX);
  466. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  467. i2c_imx_bus_busy(i2c_imx, 0);
  468. i2c_imx->stopped = 1;
  469. } else {
  470. /*
  471. * For i2c master receiver repeat restart operation like:
  472. * read -> repeat MSTA -> read/write
  473. * The controller must set MTX before read the last byte in
  474. * the first read operation, otherwise the first read cost
  475. * one extra clock cycle.
  476. */
  477. temp = readb(i2c_imx->base + IMX_I2C_I2CR);
  478. temp |= I2CR_MTX;
  479. writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
  480. }
  481. } else if (i == (msgs->len - 2)) {
  482. dev_dbg(&i2c_imx->adapter.dev,
  483. "<%s> set TXAK\n", __func__);
  484. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  485. temp |= I2CR_TXAK;
  486. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  487. }
  488. if ((!i) && block_data)
  489. msgs->buf[0] = len;
  490. else
  491. msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  492. dev_dbg(&i2c_imx->adapter.dev,
  493. "<%s> read byte: B%d=0x%X\n",
  494. __func__, i, msgs->buf[i]);
  495. }
  496. return 0;
  497. }
  498. static int i2c_imx_xfer(struct i2c_adapter *adapter,
  499. struct i2c_msg *msgs, int num)
  500. {
  501. unsigned int i, temp;
  502. int result;
  503. bool is_lastmsg = false;
  504. struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
  505. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  506. /* Start I2C transfer */
  507. result = i2c_imx_start(i2c_imx);
  508. if (result)
  509. goto fail0;
  510. /* read/write data */
  511. for (i = 0; i < num; i++) {
  512. if (i == num - 1)
  513. is_lastmsg = true;
  514. if (i) {
  515. dev_dbg(&i2c_imx->adapter.dev,
  516. "<%s> repeated start\n", __func__);
  517. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  518. temp |= I2CR_RSTA;
  519. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  520. result = i2c_imx_bus_busy(i2c_imx, 1);
  521. if (result)
  522. goto fail0;
  523. }
  524. dev_dbg(&i2c_imx->adapter.dev,
  525. "<%s> transfer message: %d\n", __func__, i);
  526. /* write/read data */
  527. #ifdef CONFIG_I2C_DEBUG_BUS
  528. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  529. dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, "
  530. "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__,
  531. (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
  532. (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
  533. (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
  534. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  535. dev_dbg(&i2c_imx->adapter.dev,
  536. "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, "
  537. "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__,
  538. (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
  539. (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
  540. (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
  541. (temp & I2SR_RXAK ? 1 : 0));
  542. #endif
  543. if (msgs[i].flags & I2C_M_RD)
  544. result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
  545. else
  546. result = i2c_imx_write(i2c_imx, &msgs[i]);
  547. if (result)
  548. goto fail0;
  549. }
  550. fail0:
  551. /* Stop I2C transfer */
  552. i2c_imx_stop(i2c_imx);
  553. dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
  554. (result < 0) ? "error" : "success msg",
  555. (result < 0) ? result : num);
  556. return (result < 0) ? result : num;
  557. }
  558. static u32 i2c_imx_func(struct i2c_adapter *adapter)
  559. {
  560. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  561. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  562. }
  563. static struct i2c_algorithm i2c_imx_algo = {
  564. .master_xfer = i2c_imx_xfer,
  565. .functionality = i2c_imx_func,
  566. };
  567. static int i2c_imx_probe(struct platform_device *pdev)
  568. {
  569. const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
  570. &pdev->dev);
  571. struct imx_i2c_struct *i2c_imx;
  572. struct resource *res;
  573. struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
  574. void __iomem *base;
  575. int irq, ret;
  576. dev_dbg(&pdev->dev, "<%s>\n", __func__);
  577. irq = platform_get_irq(pdev, 0);
  578. if (irq < 0) {
  579. dev_err(&pdev->dev, "can't get irq number\n");
  580. return irq;
  581. }
  582. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  583. base = devm_ioremap_resource(&pdev->dev, res);
  584. if (IS_ERR(base))
  585. return PTR_ERR(base);
  586. i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct),
  587. GFP_KERNEL);
  588. if (!i2c_imx)
  589. return -ENOMEM;
  590. if (of_id)
  591. i2c_imx->hwdata = of_id->data;
  592. else
  593. i2c_imx->hwdata = (struct imx_i2c_hwdata *)
  594. platform_get_device_id(pdev)->driver_data;
  595. /* Setup i2c_imx driver structure */
  596. strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
  597. i2c_imx->adapter.owner = THIS_MODULE;
  598. i2c_imx->adapter.algo = &i2c_imx_algo;
  599. i2c_imx->adapter.dev.parent = &pdev->dev;
  600. i2c_imx->adapter.nr = pdev->id;
  601. i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
  602. i2c_imx->base = base;
  603. /* Get I2C clock */
  604. i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
  605. if (IS_ERR(i2c_imx->clk)) {
  606. dev_err(&pdev->dev, "can't get I2C clock\n");
  607. return PTR_ERR(i2c_imx->clk);
  608. }
  609. ret = clk_prepare_enable(i2c_imx->clk);
  610. if (ret) {
  611. dev_err(&pdev->dev, "can't enable I2C clock\n");
  612. return ret;
  613. }
  614. /* Request IRQ */
  615. ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0,
  616. pdev->name, i2c_imx);
  617. if (ret) {
  618. dev_err(&pdev->dev, "can't claim irq %d\n", irq);
  619. return ret;
  620. }
  621. /* Init queue */
  622. init_waitqueue_head(&i2c_imx->queue);
  623. /* Set up adapter data */
  624. i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
  625. /* Set up clock divider */
  626. i2c_imx->bitrate = IMX_I2C_BIT_RATE;
  627. ret = of_property_read_u32(pdev->dev.of_node,
  628. "clock-frequency", &i2c_imx->bitrate);
  629. if (ret < 0 && pdata && pdata->bitrate)
  630. i2c_imx->bitrate = pdata->bitrate;
  631. /* Set up chip registers to defaults */
  632. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  633. i2c_imx, IMX_I2C_I2CR);
  634. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  635. /* Add I2C adapter */
  636. ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
  637. if (ret < 0) {
  638. dev_err(&pdev->dev, "registration failed\n");
  639. return ret;
  640. }
  641. /* Set up platform driver data */
  642. platform_set_drvdata(pdev, i2c_imx);
  643. clk_disable_unprepare(i2c_imx->clk);
  644. dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
  645. dev_dbg(&i2c_imx->adapter.dev, "device resources from 0x%x to 0x%x\n",
  646. res->start, res->end);
  647. dev_dbg(&i2c_imx->adapter.dev, "allocated %d bytes at 0x%x\n",
  648. resource_size(res), res->start);
  649. dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
  650. i2c_imx->adapter.name);
  651. dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
  652. return 0; /* Return OK */
  653. }
  654. static int i2c_imx_remove(struct platform_device *pdev)
  655. {
  656. struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
  657. /* remove adapter */
  658. dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
  659. i2c_del_adapter(&i2c_imx->adapter);
  660. /* setup chip registers to defaults */
  661. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
  662. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);
  663. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR);
  664. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR);
  665. return 0;
  666. }
  667. static struct platform_driver i2c_imx_driver = {
  668. .probe = i2c_imx_probe,
  669. .remove = i2c_imx_remove,
  670. .driver = {
  671. .name = DRIVER_NAME,
  672. .owner = THIS_MODULE,
  673. .of_match_table = i2c_imx_dt_ids,
  674. },
  675. .id_table = imx_i2c_devtype,
  676. };
  677. static int __init i2c_adap_imx_init(void)
  678. {
  679. return platform_driver_register(&i2c_imx_driver);
  680. }
  681. subsys_initcall(i2c_adap_imx_init);
  682. static void __exit i2c_adap_imx_exit(void)
  683. {
  684. platform_driver_unregister(&i2c_imx_driver);
  685. }
  686. module_exit(i2c_adap_imx_exit);
  687. MODULE_LICENSE("GPL");
  688. MODULE_AUTHOR("Darius Augulis");
  689. MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
  690. MODULE_ALIAS("platform:" DRIVER_NAME);