i2c-jz4780.c 20 KB

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