i2c-jz4780.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Ingenic JZ4780 I2C bus driver
  3. *
  4. * Copyright (C) 2006 - 2009 Ingenic Semiconductor Inc.
  5. * Copyright (C) 2015 Imagination Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/i2c.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/sched.h>
  30. #include <linux/slab.h>
  31. #include <linux/time.h>
  32. #define JZ4780_I2C_CTRL 0x00
  33. #define JZ4780_I2C_TAR 0x04
  34. #define JZ4780_I2C_SAR 0x08
  35. #define JZ4780_I2C_DC 0x10
  36. #define JZ4780_I2C_SHCNT 0x14
  37. #define JZ4780_I2C_SLCNT 0x18
  38. #define JZ4780_I2C_FHCNT 0x1C
  39. #define JZ4780_I2C_FLCNT 0x20
  40. #define JZ4780_I2C_INTST 0x2C
  41. #define JZ4780_I2C_INTM 0x30
  42. #define JZ4780_I2C_RXTL 0x38
  43. #define JZ4780_I2C_TXTL 0x3C
  44. #define JZ4780_I2C_CINTR 0x40
  45. #define JZ4780_I2C_CRXUF 0x44
  46. #define JZ4780_I2C_CRXOF 0x48
  47. #define JZ4780_I2C_CTXOF 0x4C
  48. #define JZ4780_I2C_CRXREQ 0x50
  49. #define JZ4780_I2C_CTXABRT 0x54
  50. #define JZ4780_I2C_CRXDONE 0x58
  51. #define JZ4780_I2C_CACT 0x5C
  52. #define JZ4780_I2C_CSTP 0x60
  53. #define JZ4780_I2C_CSTT 0x64
  54. #define JZ4780_I2C_CGC 0x68
  55. #define JZ4780_I2C_ENB 0x6C
  56. #define JZ4780_I2C_STA 0x70
  57. #define JZ4780_I2C_TXABRT 0x80
  58. #define JZ4780_I2C_DMACR 0x88
  59. #define JZ4780_I2C_DMATDLR 0x8C
  60. #define JZ4780_I2C_DMARDLR 0x90
  61. #define JZ4780_I2C_SDASU 0x94
  62. #define JZ4780_I2C_ACKGC 0x98
  63. #define JZ4780_I2C_ENSTA 0x9C
  64. #define JZ4780_I2C_SDAHD 0xD0
  65. #define JZ4780_I2C_CTRL_STPHLD BIT(7)
  66. #define JZ4780_I2C_CTRL_SLVDIS BIT(6)
  67. #define JZ4780_I2C_CTRL_REST BIT(5)
  68. #define JZ4780_I2C_CTRL_MATP BIT(4)
  69. #define JZ4780_I2C_CTRL_SATP BIT(3)
  70. #define JZ4780_I2C_CTRL_SPDF BIT(2)
  71. #define JZ4780_I2C_CTRL_SPDS BIT(1)
  72. #define JZ4780_I2C_CTRL_MD BIT(0)
  73. #define JZ4780_I2C_STA_SLVACT BIT(6)
  74. #define JZ4780_I2C_STA_MSTACT BIT(5)
  75. #define JZ4780_I2C_STA_RFF BIT(4)
  76. #define JZ4780_I2C_STA_RFNE BIT(3)
  77. #define JZ4780_I2C_STA_TFE BIT(2)
  78. #define JZ4780_I2C_STA_TFNF BIT(1)
  79. #define JZ4780_I2C_STA_ACT BIT(0)
  80. static const char * const jz4780_i2c_abrt_src[] = {
  81. "ABRT_7B_ADDR_NOACK",
  82. "ABRT_10ADDR1_NOACK",
  83. "ABRT_10ADDR2_NOACK",
  84. "ABRT_XDATA_NOACK",
  85. "ABRT_GCALL_NOACK",
  86. "ABRT_GCALL_READ",
  87. "ABRT_HS_ACKD",
  88. "SBYTE_ACKDET",
  89. "ABRT_HS_NORSTRT",
  90. "SBYTE_NORSTRT",
  91. "ABRT_10B_RD_NORSTRT",
  92. "ABRT_MASTER_DIS",
  93. "ARB_LOST",
  94. "SLVFLUSH_TXFIFO",
  95. "SLV_ARBLOST",
  96. "SLVRD_INTX",
  97. };
  98. #define JZ4780_I2C_INTST_IGC BIT(11)
  99. #define JZ4780_I2C_INTST_ISTT BIT(10)
  100. #define JZ4780_I2C_INTST_ISTP BIT(9)
  101. #define JZ4780_I2C_INTST_IACT BIT(8)
  102. #define JZ4780_I2C_INTST_RXDN BIT(7)
  103. #define JZ4780_I2C_INTST_TXABT BIT(6)
  104. #define JZ4780_I2C_INTST_RDREQ BIT(5)
  105. #define JZ4780_I2C_INTST_TXEMP BIT(4)
  106. #define JZ4780_I2C_INTST_TXOF BIT(3)
  107. #define JZ4780_I2C_INTST_RXFL BIT(2)
  108. #define JZ4780_I2C_INTST_RXOF BIT(1)
  109. #define JZ4780_I2C_INTST_RXUF BIT(0)
  110. #define JZ4780_I2C_INTM_MIGC BIT(11)
  111. #define JZ4780_I2C_INTM_MISTT BIT(10)
  112. #define JZ4780_I2C_INTM_MISTP BIT(9)
  113. #define JZ4780_I2C_INTM_MIACT BIT(8)
  114. #define JZ4780_I2C_INTM_MRXDN BIT(7)
  115. #define JZ4780_I2C_INTM_MTXABT BIT(6)
  116. #define JZ4780_I2C_INTM_MRDREQ BIT(5)
  117. #define JZ4780_I2C_INTM_MTXEMP BIT(4)
  118. #define JZ4780_I2C_INTM_MTXOF BIT(3)
  119. #define JZ4780_I2C_INTM_MRXFL BIT(2)
  120. #define JZ4780_I2C_INTM_MRXOF BIT(1)
  121. #define JZ4780_I2C_INTM_MRXUF BIT(0)
  122. #define JZ4780_I2C_DC_READ BIT(8)
  123. #define JZ4780_I2C_SDAHD_HDENB BIT(8)
  124. #define JZ4780_I2C_ENB_I2C BIT(0)
  125. #define JZ4780_I2CSHCNT_ADJUST(n) (((n) - 8) < 6 ? 6 : ((n) - 8))
  126. #define JZ4780_I2CSLCNT_ADJUST(n) (((n) - 1) < 8 ? 8 : ((n) - 1))
  127. #define JZ4780_I2CFHCNT_ADJUST(n) (((n) - 8) < 6 ? 6 : ((n) - 8))
  128. #define JZ4780_I2CFLCNT_ADJUST(n) (((n) - 1) < 8 ? 8 : ((n) - 1))
  129. #define JZ4780_I2C_FIFO_LEN 16
  130. #define TX_LEVEL 3
  131. #define RX_LEVEL (JZ4780_I2C_FIFO_LEN - TX_LEVEL - 1)
  132. #define JZ4780_I2C_TIMEOUT 300
  133. #define BUFSIZE 200
  134. struct jz4780_i2c {
  135. void __iomem *iomem;
  136. int irq;
  137. struct clk *clk;
  138. struct i2c_adapter adap;
  139. /* lock to protect rbuf and wbuf between xfer_rd/wr and irq handler */
  140. spinlock_t lock;
  141. /* beginning of lock scope */
  142. unsigned char *rbuf;
  143. int rd_total_len;
  144. int rd_data_xfered;
  145. int rd_cmd_xfered;
  146. unsigned char *wbuf;
  147. int wt_len;
  148. int is_write;
  149. int stop_hold;
  150. int speed;
  151. int data_buf[BUFSIZE];
  152. int cmd_buf[BUFSIZE];
  153. int cmd;
  154. /* end of lock scope */
  155. struct completion trans_waitq;
  156. };
  157. static inline unsigned short jz4780_i2c_readw(struct jz4780_i2c *i2c,
  158. unsigned long offset)
  159. {
  160. return readw(i2c->iomem + offset);
  161. }
  162. static inline void jz4780_i2c_writew(struct jz4780_i2c *i2c,
  163. unsigned long offset, unsigned short val)
  164. {
  165. writew(val, i2c->iomem + offset);
  166. }
  167. static int jz4780_i2c_disable(struct jz4780_i2c *i2c)
  168. {
  169. unsigned short regval;
  170. unsigned long loops = 5;
  171. jz4780_i2c_writew(i2c, JZ4780_I2C_ENB, 0);
  172. do {
  173. regval = jz4780_i2c_readw(i2c, JZ4780_I2C_ENSTA);
  174. if (!(regval & JZ4780_I2C_ENB_I2C))
  175. return 0;
  176. usleep_range(5000, 15000);
  177. } while (--loops);
  178. dev_err(&i2c->adap.dev, "disable failed: ENSTA=0x%04x\n", regval);
  179. return -ETIMEDOUT;
  180. }
  181. static int jz4780_i2c_enable(struct jz4780_i2c *i2c)
  182. {
  183. unsigned short regval;
  184. unsigned long loops = 5;
  185. jz4780_i2c_writew(i2c, JZ4780_I2C_ENB, 1);
  186. do {
  187. regval = jz4780_i2c_readw(i2c, JZ4780_I2C_ENSTA);
  188. if (regval & JZ4780_I2C_ENB_I2C)
  189. return 0;
  190. usleep_range(5000, 15000);
  191. } while (--loops);
  192. dev_err(&i2c->adap.dev, "enable failed: ENSTA=0x%04x\n", regval);
  193. return -ETIMEDOUT;
  194. }
  195. static int jz4780_i2c_set_target(struct jz4780_i2c *i2c, unsigned char address)
  196. {
  197. unsigned short regval;
  198. unsigned long loops = 5;
  199. do {
  200. regval = jz4780_i2c_readw(i2c, JZ4780_I2C_STA);
  201. if ((regval & JZ4780_I2C_STA_TFE) &&
  202. !(regval & JZ4780_I2C_STA_MSTACT))
  203. break;
  204. usleep_range(5000, 15000);
  205. } while (--loops);
  206. if (loops) {
  207. jz4780_i2c_writew(i2c, JZ4780_I2C_TAR, address);
  208. return 0;
  209. }
  210. dev_err(&i2c->adap.dev,
  211. "set device to address 0x%02x failed, STA=0x%04x\n",
  212. address, regval);
  213. return -ENXIO;
  214. }
  215. static int jz4780_i2c_set_speed(struct jz4780_i2c *i2c)
  216. {
  217. int dev_clk_khz = clk_get_rate(i2c->clk) / 1000;
  218. int cnt_high = 0; /* HIGH period count of the SCL clock */
  219. int cnt_low = 0; /* LOW period count of the SCL clock */
  220. int cnt_period = 0; /* period count of the SCL clock */
  221. int setup_time = 0;
  222. int hold_time = 0;
  223. unsigned short tmp = 0;
  224. int i2c_clk = i2c->speed;
  225. if (jz4780_i2c_disable(i2c))
  226. dev_dbg(&i2c->adap.dev, "i2c not disabled\n");
  227. /*
  228. * 1 JZ4780_I2C cycle equals to cnt_period PCLK(i2c_clk)
  229. * standard mode, min LOW and HIGH period are 4700 ns and 4000 ns
  230. * fast mode, min LOW and HIGH period are 1300 ns and 600 ns
  231. */
  232. cnt_period = dev_clk_khz / i2c_clk;
  233. if (i2c_clk <= 100)
  234. cnt_high = (cnt_period * 4000) / (4700 + 4000);
  235. else
  236. cnt_high = (cnt_period * 600) / (1300 + 600);
  237. cnt_low = cnt_period - cnt_high;
  238. /*
  239. * NOTE: JZ4780_I2C_CTRL_REST can't set when i2c enabled, because
  240. * normal read are 2 messages, we cannot disable i2c controller
  241. * between these two messages, this means that we must always set
  242. * JZ4780_I2C_CTRL_REST when init JZ4780_I2C_CTRL
  243. *
  244. */
  245. if (i2c_clk <= 100) {
  246. tmp = JZ4780_I2C_CTRL_SPDS | JZ4780_I2C_CTRL_REST
  247. | JZ4780_I2C_CTRL_SLVDIS | JZ4780_I2C_CTRL_MD;
  248. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  249. jz4780_i2c_writew(i2c, JZ4780_I2C_SHCNT,
  250. JZ4780_I2CSHCNT_ADJUST(cnt_high));
  251. jz4780_i2c_writew(i2c, JZ4780_I2C_SLCNT,
  252. JZ4780_I2CSLCNT_ADJUST(cnt_low));
  253. } else {
  254. tmp = JZ4780_I2C_CTRL_SPDF | JZ4780_I2C_CTRL_REST
  255. | JZ4780_I2C_CTRL_SLVDIS | JZ4780_I2C_CTRL_MD;
  256. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  257. jz4780_i2c_writew(i2c, JZ4780_I2C_FHCNT,
  258. JZ4780_I2CFHCNT_ADJUST(cnt_high));
  259. jz4780_i2c_writew(i2c, JZ4780_I2C_FLCNT,
  260. JZ4780_I2CFLCNT_ADJUST(cnt_low));
  261. }
  262. /*
  263. * a i2c device must internally provide a hold time at least 300ns
  264. * tHD:DAT
  265. * Standard Mode: min=300ns, max=3450ns
  266. * Fast Mode: min=0ns, max=900ns
  267. * tSU:DAT
  268. * Standard Mode: min=250ns, max=infinite
  269. * Fast Mode: min=100(250ns is recommended), max=infinite
  270. *
  271. * 1i2c_clk = 10^6 / dev_clk_khz
  272. * on FPGA, dev_clk_khz = 12000, so 1i2c_clk = 1000/12 = 83ns
  273. * on Pisces(1008M), dev_clk_khz=126000, so 1i2c_clk = 1000 / 126 = 8ns
  274. *
  275. * The actual hold time is (SDAHD + 1) * (i2c_clk period).
  276. *
  277. * Length of setup time calculated using (SDASU - 1) * (ic_clk_period)
  278. *
  279. */
  280. if (i2c_clk <= 100) { /* standard mode */
  281. setup_time = 300;
  282. hold_time = 400;
  283. } else {
  284. setup_time = 450;
  285. hold_time = 450;
  286. }
  287. hold_time = ((hold_time * dev_clk_khz) / 1000000) - 1;
  288. setup_time = ((setup_time * dev_clk_khz) / 1000000) + 1;
  289. if (setup_time > 255)
  290. setup_time = 255;
  291. if (setup_time <= 0)
  292. setup_time = 1;
  293. jz4780_i2c_writew(i2c, JZ4780_I2C_SDASU, setup_time);
  294. if (hold_time > 255)
  295. hold_time = 255;
  296. if (hold_time >= 0) {
  297. /*i2c hold time enable */
  298. hold_time |= JZ4780_I2C_SDAHD_HDENB;
  299. jz4780_i2c_writew(i2c, JZ4780_I2C_SDAHD, hold_time);
  300. } else {
  301. /* disable hold time */
  302. jz4780_i2c_writew(i2c, JZ4780_I2C_SDAHD, 0);
  303. }
  304. return 0;
  305. }
  306. static int jz4780_i2c_cleanup(struct jz4780_i2c *i2c)
  307. {
  308. int ret;
  309. unsigned long flags;
  310. unsigned short tmp;
  311. spin_lock_irqsave(&i2c->lock, flags);
  312. /* can send stop now if need */
  313. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_CTRL);
  314. tmp &= ~JZ4780_I2C_CTRL_STPHLD;
  315. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  316. /* disable all interrupts first */
  317. jz4780_i2c_writew(i2c, JZ4780_I2C_INTM, 0);
  318. /* then clear all interrupts */
  319. jz4780_i2c_readw(i2c, JZ4780_I2C_CTXABRT);
  320. jz4780_i2c_readw(i2c, JZ4780_I2C_CINTR);
  321. /* then disable the controller */
  322. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_CTRL);
  323. tmp &= ~JZ4780_I2C_ENB_I2C;
  324. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  325. udelay(10);
  326. tmp |= JZ4780_I2C_ENB_I2C;
  327. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  328. spin_unlock_irqrestore(&i2c->lock, flags);
  329. ret = jz4780_i2c_disable(i2c);
  330. if (ret)
  331. dev_err(&i2c->adap.dev,
  332. "unable to disable device during cleanup!\n");
  333. if (unlikely(jz4780_i2c_readw(i2c, JZ4780_I2C_INTM)
  334. & jz4780_i2c_readw(i2c, JZ4780_I2C_INTST)))
  335. dev_err(&i2c->adap.dev,
  336. "device has interrupts after a complete cleanup!\n");
  337. return ret;
  338. }
  339. static int jz4780_i2c_prepare(struct jz4780_i2c *i2c)
  340. {
  341. jz4780_i2c_set_speed(i2c);
  342. return jz4780_i2c_enable(i2c);
  343. }
  344. static void jz4780_i2c_send_rcmd(struct jz4780_i2c *i2c, int cmd_count)
  345. {
  346. int i;
  347. for (i = 0; i < cmd_count; i++)
  348. jz4780_i2c_writew(i2c, JZ4780_I2C_DC, JZ4780_I2C_DC_READ);
  349. }
  350. static void jz4780_i2c_trans_done(struct jz4780_i2c *i2c)
  351. {
  352. jz4780_i2c_writew(i2c, JZ4780_I2C_INTM, 0);
  353. complete(&i2c->trans_waitq);
  354. }
  355. static irqreturn_t jz4780_i2c_irq(int irqno, void *dev_id)
  356. {
  357. unsigned short tmp;
  358. unsigned short intst;
  359. unsigned short intmsk;
  360. struct jz4780_i2c *i2c = dev_id;
  361. unsigned long flags;
  362. spin_lock_irqsave(&i2c->lock, flags);
  363. intmsk = jz4780_i2c_readw(i2c, JZ4780_I2C_INTM);
  364. intst = jz4780_i2c_readw(i2c, JZ4780_I2C_INTST);
  365. intst &= intmsk;
  366. if (intst & JZ4780_I2C_INTST_TXABT) {
  367. jz4780_i2c_trans_done(i2c);
  368. goto done;
  369. }
  370. if (intst & JZ4780_I2C_INTST_RXOF) {
  371. dev_dbg(&i2c->adap.dev, "received fifo overflow!\n");
  372. jz4780_i2c_trans_done(i2c);
  373. goto done;
  374. }
  375. /*
  376. * When reading, always drain RX FIFO before we send more Read
  377. * Commands to avoid fifo overrun
  378. */
  379. if (i2c->is_write == 0) {
  380. int rd_left;
  381. while ((jz4780_i2c_readw(i2c, JZ4780_I2C_STA)
  382. & JZ4780_I2C_STA_RFNE)) {
  383. *(i2c->rbuf++) = jz4780_i2c_readw(i2c, JZ4780_I2C_DC)
  384. & 0xff;
  385. i2c->rd_data_xfered++;
  386. if (i2c->rd_data_xfered == i2c->rd_total_len) {
  387. jz4780_i2c_trans_done(i2c);
  388. goto done;
  389. }
  390. }
  391. rd_left = i2c->rd_total_len - i2c->rd_data_xfered;
  392. if (rd_left <= JZ4780_I2C_FIFO_LEN)
  393. jz4780_i2c_writew(i2c, JZ4780_I2C_RXTL, rd_left - 1);
  394. }
  395. if (intst & JZ4780_I2C_INTST_TXEMP) {
  396. if (i2c->is_write == 0) {
  397. int cmd_left = i2c->rd_total_len - i2c->rd_cmd_xfered;
  398. int max_send = (JZ4780_I2C_FIFO_LEN - 1)
  399. - (i2c->rd_cmd_xfered
  400. - i2c->rd_data_xfered);
  401. int cmd_to_send = min(cmd_left, max_send);
  402. if (i2c->rd_cmd_xfered != 0)
  403. cmd_to_send = min(cmd_to_send,
  404. JZ4780_I2C_FIFO_LEN
  405. - TX_LEVEL - 1);
  406. if (cmd_to_send) {
  407. jz4780_i2c_send_rcmd(i2c, cmd_to_send);
  408. i2c->rd_cmd_xfered += cmd_to_send;
  409. }
  410. cmd_left = i2c->rd_total_len - i2c->rd_cmd_xfered;
  411. if (cmd_left == 0) {
  412. intmsk = jz4780_i2c_readw(i2c, JZ4780_I2C_INTM);
  413. intmsk &= ~JZ4780_I2C_INTM_MTXEMP;
  414. jz4780_i2c_writew(i2c, JZ4780_I2C_INTM, intmsk);
  415. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_CTRL);
  416. tmp &= ~JZ4780_I2C_CTRL_STPHLD;
  417. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  418. }
  419. } else {
  420. unsigned short data;
  421. unsigned short i2c_sta;
  422. i2c_sta = jz4780_i2c_readw(i2c, JZ4780_I2C_STA);
  423. while ((i2c_sta & JZ4780_I2C_STA_TFNF) &&
  424. (i2c->wt_len > 0)) {
  425. i2c_sta = jz4780_i2c_readw(i2c, JZ4780_I2C_STA);
  426. data = *i2c->wbuf;
  427. data &= ~JZ4780_I2C_DC_READ;
  428. jz4780_i2c_writew(i2c, JZ4780_I2C_DC,
  429. data);
  430. i2c->wbuf++;
  431. i2c->wt_len--;
  432. }
  433. if (i2c->wt_len == 0) {
  434. if (!i2c->stop_hold) {
  435. tmp = jz4780_i2c_readw(i2c,
  436. JZ4780_I2C_CTRL);
  437. tmp &= ~JZ4780_I2C_CTRL_STPHLD;
  438. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL,
  439. tmp);
  440. }
  441. jz4780_i2c_trans_done(i2c);
  442. goto done;
  443. }
  444. }
  445. }
  446. done:
  447. spin_unlock_irqrestore(&i2c->lock, flags);
  448. return IRQ_HANDLED;
  449. }
  450. static void jz4780_i2c_txabrt(struct jz4780_i2c *i2c, int src)
  451. {
  452. int i;
  453. dev_err(&i2c->adap.dev, "txabrt: 0x%08x\n", src);
  454. dev_err(&i2c->adap.dev, "device addr=%x\n",
  455. jz4780_i2c_readw(i2c, JZ4780_I2C_TAR));
  456. dev_err(&i2c->adap.dev, "send cmd count:%d %d\n",
  457. i2c->cmd, i2c->cmd_buf[i2c->cmd]);
  458. dev_err(&i2c->adap.dev, "receive data count:%d %d\n",
  459. i2c->cmd, i2c->data_buf[i2c->cmd]);
  460. for (i = 0; i < 16; i++) {
  461. if (src & BIT(i))
  462. dev_dbg(&i2c->adap.dev, "I2C TXABRT[%d]=%s\n",
  463. i, jz4780_i2c_abrt_src[i]);
  464. }
  465. }
  466. static inline int jz4780_i2c_xfer_read(struct jz4780_i2c *i2c,
  467. unsigned char *buf, int len, int cnt,
  468. int idx)
  469. {
  470. int ret = 0;
  471. long timeout;
  472. int wait_time = JZ4780_I2C_TIMEOUT * (len + 5);
  473. unsigned short tmp;
  474. unsigned long flags;
  475. memset(buf, 0, len);
  476. spin_lock_irqsave(&i2c->lock, flags);
  477. i2c->stop_hold = 0;
  478. i2c->is_write = 0;
  479. i2c->rbuf = buf;
  480. i2c->rd_total_len = len;
  481. i2c->rd_data_xfered = 0;
  482. i2c->rd_cmd_xfered = 0;
  483. if (len <= JZ4780_I2C_FIFO_LEN)
  484. jz4780_i2c_writew(i2c, JZ4780_I2C_RXTL, len - 1);
  485. else
  486. jz4780_i2c_writew(i2c, JZ4780_I2C_RXTL, RX_LEVEL);
  487. jz4780_i2c_writew(i2c, JZ4780_I2C_TXTL, TX_LEVEL);
  488. jz4780_i2c_writew(i2c, JZ4780_I2C_INTM,
  489. JZ4780_I2C_INTM_MRXFL | JZ4780_I2C_INTM_MTXEMP
  490. | JZ4780_I2C_INTM_MTXABT | JZ4780_I2C_INTM_MRXOF);
  491. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_CTRL);
  492. tmp |= JZ4780_I2C_CTRL_STPHLD;
  493. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  494. spin_unlock_irqrestore(&i2c->lock, flags);
  495. timeout = wait_for_completion_timeout(&i2c->trans_waitq,
  496. msecs_to_jiffies(wait_time));
  497. if (!timeout) {
  498. dev_err(&i2c->adap.dev, "irq read timeout\n");
  499. dev_dbg(&i2c->adap.dev, "send cmd count:%d %d\n",
  500. i2c->cmd, i2c->cmd_buf[i2c->cmd]);
  501. dev_dbg(&i2c->adap.dev, "receive data count:%d %d\n",
  502. i2c->cmd, i2c->data_buf[i2c->cmd]);
  503. ret = -EIO;
  504. }
  505. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_TXABRT);
  506. if (tmp) {
  507. jz4780_i2c_txabrt(i2c, tmp);
  508. ret = -EIO;
  509. }
  510. return ret;
  511. }
  512. static inline int jz4780_i2c_xfer_write(struct jz4780_i2c *i2c,
  513. unsigned char *buf, int len,
  514. int cnt, int idx)
  515. {
  516. int ret = 0;
  517. int wait_time = JZ4780_I2C_TIMEOUT * (len + 5);
  518. long timeout;
  519. unsigned short tmp;
  520. unsigned long flags;
  521. spin_lock_irqsave(&i2c->lock, flags);
  522. if (idx < (cnt - 1))
  523. i2c->stop_hold = 1;
  524. else
  525. i2c->stop_hold = 0;
  526. i2c->is_write = 1;
  527. i2c->wbuf = buf;
  528. i2c->wt_len = len;
  529. jz4780_i2c_writew(i2c, JZ4780_I2C_TXTL, TX_LEVEL);
  530. jz4780_i2c_writew(i2c, JZ4780_I2C_INTM, JZ4780_I2C_INTM_MTXEMP
  531. | JZ4780_I2C_INTM_MTXABT);
  532. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_CTRL);
  533. tmp |= JZ4780_I2C_CTRL_STPHLD;
  534. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  535. spin_unlock_irqrestore(&i2c->lock, flags);
  536. timeout = wait_for_completion_timeout(&i2c->trans_waitq,
  537. msecs_to_jiffies(wait_time));
  538. if (timeout && !i2c->stop_hold) {
  539. unsigned short i2c_sta;
  540. int write_in_process;
  541. timeout = JZ4780_I2C_TIMEOUT * 100;
  542. for (; timeout > 0; timeout--) {
  543. i2c_sta = jz4780_i2c_readw(i2c, JZ4780_I2C_STA);
  544. write_in_process = (i2c_sta & JZ4780_I2C_STA_MSTACT) ||
  545. !(i2c_sta & JZ4780_I2C_STA_TFE);
  546. if (!write_in_process)
  547. break;
  548. udelay(10);
  549. }
  550. }
  551. if (!timeout) {
  552. dev_err(&i2c->adap.dev, "write wait timeout\n");
  553. ret = -EIO;
  554. }
  555. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_TXABRT);
  556. if (tmp) {
  557. jz4780_i2c_txabrt(i2c, tmp);
  558. ret = -EIO;
  559. }
  560. return ret;
  561. }
  562. static int jz4780_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  563. int count)
  564. {
  565. int i = -EIO;
  566. int ret = 0;
  567. struct jz4780_i2c *i2c = adap->algo_data;
  568. ret = jz4780_i2c_prepare(i2c);
  569. if (ret) {
  570. dev_err(&i2c->adap.dev, "I2C prepare failed\n");
  571. goto out;
  572. }
  573. if (msg->addr != jz4780_i2c_readw(i2c, JZ4780_I2C_TAR)) {
  574. ret = jz4780_i2c_set_target(i2c, msg->addr);
  575. if (ret)
  576. goto out;
  577. }
  578. for (i = 0; i < count; i++, msg++) {
  579. if (msg->flags & I2C_M_RD)
  580. ret = jz4780_i2c_xfer_read(i2c, msg->buf, msg->len,
  581. count, i);
  582. else
  583. ret = jz4780_i2c_xfer_write(i2c, msg->buf, msg->len,
  584. count, i);
  585. if (ret)
  586. goto out;
  587. }
  588. ret = i;
  589. out:
  590. jz4780_i2c_cleanup(i2c);
  591. return ret;
  592. }
  593. static u32 jz4780_i2c_functionality(struct i2c_adapter *adap)
  594. {
  595. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  596. }
  597. static const struct i2c_algorithm jz4780_i2c_algorithm = {
  598. .master_xfer = jz4780_i2c_xfer,
  599. .functionality = jz4780_i2c_functionality,
  600. };
  601. static const struct of_device_id jz4780_i2c_of_matches[] = {
  602. { .compatible = "ingenic,jz4780-i2c", },
  603. { /* sentinel */ }
  604. };
  605. static int jz4780_i2c_probe(struct platform_device *pdev)
  606. {
  607. int ret = 0;
  608. unsigned int clk_freq = 0;
  609. unsigned short tmp;
  610. struct resource *r;
  611. struct jz4780_i2c *i2c;
  612. i2c = devm_kzalloc(&pdev->dev, sizeof(struct jz4780_i2c), GFP_KERNEL);
  613. if (!i2c)
  614. return -ENOMEM;
  615. i2c->adap.owner = THIS_MODULE;
  616. i2c->adap.algo = &jz4780_i2c_algorithm;
  617. i2c->adap.algo_data = i2c;
  618. i2c->adap.retries = 5;
  619. i2c->adap.dev.parent = &pdev->dev;
  620. i2c->adap.dev.of_node = pdev->dev.of_node;
  621. sprintf(i2c->adap.name, "%s", pdev->name);
  622. init_completion(&i2c->trans_waitq);
  623. spin_lock_init(&i2c->lock);
  624. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  625. i2c->iomem = devm_ioremap_resource(&pdev->dev, r);
  626. if (IS_ERR(i2c->iomem))
  627. return PTR_ERR(i2c->iomem);
  628. platform_set_drvdata(pdev, i2c);
  629. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  630. if (IS_ERR(i2c->clk))
  631. return PTR_ERR(i2c->clk);
  632. ret = clk_prepare_enable(i2c->clk);
  633. if (ret)
  634. return ret;
  635. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  636. &clk_freq);
  637. if (ret) {
  638. dev_err(&pdev->dev, "clock-frequency not specified in DT");
  639. goto err;
  640. }
  641. i2c->speed = clk_freq / 1000;
  642. jz4780_i2c_set_speed(i2c);
  643. dev_info(&pdev->dev, "Bus frequency is %d KHz\n", i2c->speed);
  644. tmp = jz4780_i2c_readw(i2c, JZ4780_I2C_CTRL);
  645. tmp &= ~JZ4780_I2C_CTRL_STPHLD;
  646. jz4780_i2c_writew(i2c, JZ4780_I2C_CTRL, tmp);
  647. jz4780_i2c_writew(i2c, JZ4780_I2C_INTM, 0x0);
  648. i2c->irq = platform_get_irq(pdev, 0);
  649. ret = devm_request_irq(&pdev->dev, i2c->irq, jz4780_i2c_irq, 0,
  650. dev_name(&pdev->dev), i2c);
  651. if (ret)
  652. goto err;
  653. ret = i2c_add_adapter(&i2c->adap);
  654. if (ret < 0) {
  655. dev_err(&pdev->dev, "Failed to add bus\n");
  656. goto err;
  657. }
  658. return 0;
  659. err:
  660. clk_disable_unprepare(i2c->clk);
  661. return ret;
  662. }
  663. static int jz4780_i2c_remove(struct platform_device *pdev)
  664. {
  665. struct jz4780_i2c *i2c = platform_get_drvdata(pdev);
  666. clk_disable_unprepare(i2c->clk);
  667. i2c_del_adapter(&i2c->adap);
  668. return 0;
  669. }
  670. static struct platform_driver jz4780_i2c_driver = {
  671. .probe = jz4780_i2c_probe,
  672. .remove = jz4780_i2c_remove,
  673. .driver = {
  674. .name = "jz4780-i2c",
  675. .of_match_table = of_match_ptr(jz4780_i2c_of_matches),
  676. },
  677. };
  678. module_platform_driver(jz4780_i2c_driver);
  679. MODULE_LICENSE("GPL");
  680. MODULE_AUTHOR("ztyan<ztyan@ingenic.cn>");
  681. MODULE_DESCRIPTION("i2c driver for JZ4780 SoCs");