i2c-sh_mobile.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * SuperH Mobile I2C Controller
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Portions of the code based on out-of-tree driver i2c-sh7343.c
  7. * Copyright (c) 2006 Carlos Munoz <carlos@kenati.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/i2c.h>
  25. #include <linux/err.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/clk.h>
  28. #include <linux/io.h>
  29. #include <linux/slab.h>
  30. #include <linux/of_device.h>
  31. #include <linux/i2c/i2c-sh_mobile.h>
  32. /* Transmit operation: */
  33. /* */
  34. /* 0 byte transmit */
  35. /* BUS: S A8 ACK P(*) */
  36. /* IRQ: DTE WAIT */
  37. /* ICIC: */
  38. /* ICCR: 0x94 0x90 */
  39. /* ICDR: A8 */
  40. /* */
  41. /* 1 byte transmit */
  42. /* BUS: S A8 ACK D8(1) ACK P(*) */
  43. /* IRQ: DTE WAIT WAIT */
  44. /* ICIC: -DTE */
  45. /* ICCR: 0x94 0x90 */
  46. /* ICDR: A8 D8(1) */
  47. /* */
  48. /* 2 byte transmit */
  49. /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P(*) */
  50. /* IRQ: DTE WAIT WAIT WAIT */
  51. /* ICIC: -DTE */
  52. /* ICCR: 0x94 0x90 */
  53. /* ICDR: A8 D8(1) D8(2) */
  54. /* */
  55. /* 3 bytes or more, +---------+ gets repeated */
  56. /* */
  57. /* */
  58. /* Receive operation: */
  59. /* */
  60. /* 0 byte receive - not supported since slave may hold SDA low */
  61. /* */
  62. /* 1 byte receive [TX] | [RX] */
  63. /* BUS: S A8 ACK | D8(1) ACK P(*) */
  64. /* IRQ: DTE WAIT | WAIT DTE */
  65. /* ICIC: -DTE | +DTE */
  66. /* ICCR: 0x94 0x81 | 0xc0 */
  67. /* ICDR: A8 | D8(1) */
  68. /* */
  69. /* 2 byte receive [TX]| [RX] */
  70. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P(*) */
  71. /* IRQ: DTE WAIT | WAIT WAIT DTE */
  72. /* ICIC: -DTE | +DTE */
  73. /* ICCR: 0x94 0x81 | 0xc0 */
  74. /* ICDR: A8 | D8(1) D8(2) */
  75. /* */
  76. /* 3 byte receive [TX] | [RX] (*) */
  77. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
  78. /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
  79. /* ICIC: -DTE | +DTE */
  80. /* ICCR: 0x94 0x81 | 0xc0 */
  81. /* ICDR: A8 | D8(1) D8(2) D8(3) */
  82. /* */
  83. /* 4 bytes or more, this part is repeated +---------+ */
  84. /* */
  85. /* */
  86. /* Interrupt order and BUSY flag */
  87. /* ___ _ */
  88. /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
  89. /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
  90. /* */
  91. /* S D7 D6 D5 D4 D3 D2 D1 D0 P(*) */
  92. /* ___ */
  93. /* WAIT IRQ ________________________________/ \___________ */
  94. /* TACK IRQ ____________________________________/ \_______ */
  95. /* DTE IRQ __________________________________________/ \_ */
  96. /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  97. /* _______________________________________________ */
  98. /* BUSY __/ \_ */
  99. /* */
  100. /* (*) The STOP condition is only sent by the master at the end of the last */
  101. /* I2C message or if the I2C_M_STOP flag is set. Similarly, the BUSY bit is */
  102. /* only cleared after the STOP condition, so, between messages we have to */
  103. /* poll for the DTE bit. */
  104. /* */
  105. enum sh_mobile_i2c_op {
  106. OP_START = 0,
  107. OP_TX_FIRST,
  108. OP_TX,
  109. OP_TX_STOP,
  110. OP_TX_TO_RX,
  111. OP_RX,
  112. OP_RX_STOP,
  113. OP_RX_STOP_DATA,
  114. };
  115. struct sh_mobile_i2c_data {
  116. struct device *dev;
  117. void __iomem *reg;
  118. struct i2c_adapter adap;
  119. unsigned long bus_speed;
  120. unsigned int clks_per_count;
  121. struct clk *clk;
  122. u_int8_t icic;
  123. u_int8_t flags;
  124. u_int16_t iccl;
  125. u_int16_t icch;
  126. spinlock_t lock;
  127. wait_queue_head_t wait;
  128. struct i2c_msg *msg;
  129. int pos;
  130. int sr;
  131. bool send_stop;
  132. };
  133. struct sh_mobile_dt_config {
  134. int clks_per_count;
  135. };
  136. #define IIC_FLAG_HAS_ICIC67 (1 << 0)
  137. #define STANDARD_MODE 100000
  138. #define FAST_MODE 400000
  139. /* Register offsets */
  140. #define ICDR 0x00
  141. #define ICCR 0x04
  142. #define ICSR 0x08
  143. #define ICIC 0x0c
  144. #define ICCL 0x10
  145. #define ICCH 0x14
  146. /* Register bits */
  147. #define ICCR_ICE 0x80
  148. #define ICCR_RACK 0x40
  149. #define ICCR_TRS 0x10
  150. #define ICCR_BBSY 0x04
  151. #define ICCR_SCP 0x01
  152. #define ICSR_SCLM 0x80
  153. #define ICSR_SDAM 0x40
  154. #define SW_DONE 0x20
  155. #define ICSR_BUSY 0x10
  156. #define ICSR_AL 0x08
  157. #define ICSR_TACK 0x04
  158. #define ICSR_WAIT 0x02
  159. #define ICSR_DTE 0x01
  160. #define ICIC_ICCLB8 0x80
  161. #define ICIC_ICCHB8 0x40
  162. #define ICIC_ALE 0x08
  163. #define ICIC_TACKE 0x04
  164. #define ICIC_WAITE 0x02
  165. #define ICIC_DTEE 0x01
  166. static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
  167. {
  168. if (offs == ICIC)
  169. data |= pd->icic;
  170. iowrite8(data, pd->reg + offs);
  171. }
  172. static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
  173. {
  174. return ioread8(pd->reg + offs);
  175. }
  176. static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
  177. unsigned char set, unsigned char clr)
  178. {
  179. iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
  180. }
  181. static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf)
  182. {
  183. /*
  184. * Conditional expression:
  185. * ICCL >= COUNT_CLK * (tLOW + tf)
  186. *
  187. * SH-Mobile IIC hardware starts counting the LOW period of
  188. * the SCL signal (tLOW) as soon as it pulls the SCL line.
  189. * In order to meet the tLOW timing spec, we need to take into
  190. * account the fall time of SCL signal (tf). Default tf value
  191. * should be 0.3 us, for safety.
  192. */
  193. return (((count_khz * (tLOW + tf)) + 5000) / 10000);
  194. }
  195. static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf)
  196. {
  197. /*
  198. * Conditional expression:
  199. * ICCH >= COUNT_CLK * (tHIGH + tf)
  200. *
  201. * SH-Mobile IIC hardware is aware of SCL transition period 'tr',
  202. * and can ignore it. SH-Mobile IIC controller starts counting
  203. * the HIGH period of the SCL signal (tHIGH) after the SCL input
  204. * voltage increases at VIH.
  205. *
  206. * Afterward it turned out calculating ICCH using only tHIGH spec
  207. * will result in violation of the tHD;STA timing spec. We need
  208. * to take into account the fall time of SDA signal (tf) at START
  209. * condition, in order to meet both tHIGH and tHD;STA specs.
  210. */
  211. return (((count_khz * (tHIGH + tf)) + 5000) / 10000);
  212. }
  213. static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
  214. {
  215. unsigned long i2c_clk_khz;
  216. u32 tHIGH, tLOW, tf;
  217. uint16_t max_val;
  218. /* Get clock rate after clock is enabled */
  219. clk_prepare_enable(pd->clk);
  220. i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
  221. clk_disable_unprepare(pd->clk);
  222. i2c_clk_khz /= pd->clks_per_count;
  223. if (pd->bus_speed == STANDARD_MODE) {
  224. tLOW = 47; /* tLOW = 4.7 us */
  225. tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
  226. tf = 3; /* tf = 0.3 us */
  227. } else if (pd->bus_speed == FAST_MODE) {
  228. tLOW = 13; /* tLOW = 1.3 us */
  229. tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
  230. tf = 3; /* tf = 0.3 us */
  231. } else {
  232. dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
  233. pd->bus_speed);
  234. return -EINVAL;
  235. }
  236. pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf);
  237. pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf);
  238. max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff;
  239. if (pd->iccl > max_val || pd->icch > max_val) {
  240. dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n",
  241. pd->iccl, pd->icch);
  242. return -EINVAL;
  243. }
  244. /* one more bit of ICCL in ICIC */
  245. if (pd->iccl & 0x100)
  246. pd->icic |= ICIC_ICCLB8;
  247. else
  248. pd->icic &= ~ICIC_ICCLB8;
  249. /* one more bit of ICCH in ICIC */
  250. if (pd->icch & 0x100)
  251. pd->icic |= ICIC_ICCHB8;
  252. else
  253. pd->icic &= ~ICIC_ICCHB8;
  254. return 0;
  255. }
  256. static void activate_ch(struct sh_mobile_i2c_data *pd)
  257. {
  258. /* Wake up device and enable clock */
  259. pm_runtime_get_sync(pd->dev);
  260. clk_prepare_enable(pd->clk);
  261. /* Enable channel and configure rx ack */
  262. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  263. /* Mask all interrupts */
  264. iic_wr(pd, ICIC, 0);
  265. /* Set the clock */
  266. iic_wr(pd, ICCL, pd->iccl & 0xff);
  267. iic_wr(pd, ICCH, pd->icch & 0xff);
  268. }
  269. static void deactivate_ch(struct sh_mobile_i2c_data *pd)
  270. {
  271. /* Clear/disable interrupts */
  272. iic_wr(pd, ICSR, 0);
  273. iic_wr(pd, ICIC, 0);
  274. /* Disable channel */
  275. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  276. /* Disable clock and mark device as idle */
  277. clk_disable_unprepare(pd->clk);
  278. pm_runtime_put_sync(pd->dev);
  279. }
  280. static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
  281. enum sh_mobile_i2c_op op, unsigned char data)
  282. {
  283. unsigned char ret = 0;
  284. unsigned long flags;
  285. dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
  286. spin_lock_irqsave(&pd->lock, flags);
  287. switch (op) {
  288. case OP_START: /* issue start and trigger DTE interrupt */
  289. iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY);
  290. break;
  291. case OP_TX_FIRST: /* disable DTE interrupt and write data */
  292. iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  293. iic_wr(pd, ICDR, data);
  294. break;
  295. case OP_TX: /* write data */
  296. iic_wr(pd, ICDR, data);
  297. break;
  298. case OP_TX_STOP: /* write data and issue a stop afterwards */
  299. iic_wr(pd, ICDR, data);
  300. iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS
  301. : ICCR_ICE | ICCR_TRS | ICCR_BBSY);
  302. break;
  303. case OP_TX_TO_RX: /* select read mode */
  304. iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP);
  305. break;
  306. case OP_RX: /* just read data */
  307. ret = iic_rd(pd, ICDR);
  308. break;
  309. case OP_RX_STOP: /* enable DTE interrupt, issue stop */
  310. iic_wr(pd, ICIC,
  311. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  312. iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
  313. break;
  314. case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
  315. iic_wr(pd, ICIC,
  316. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  317. ret = iic_rd(pd, ICDR);
  318. iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
  319. break;
  320. }
  321. spin_unlock_irqrestore(&pd->lock, flags);
  322. dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
  323. return ret;
  324. }
  325. static bool sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
  326. {
  327. return pd->pos == -1;
  328. }
  329. static bool sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
  330. {
  331. return pd->pos == pd->msg->len - 1;
  332. }
  333. static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
  334. unsigned char *buf)
  335. {
  336. switch (pd->pos) {
  337. case -1:
  338. *buf = (pd->msg->addr & 0x7f) << 1;
  339. *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
  340. break;
  341. default:
  342. *buf = pd->msg->buf[pd->pos];
  343. }
  344. }
  345. static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
  346. {
  347. unsigned char data;
  348. if (pd->pos == pd->msg->len)
  349. return 1;
  350. sh_mobile_i2c_get_data(pd, &data);
  351. if (sh_mobile_i2c_is_last_byte(pd))
  352. i2c_op(pd, OP_TX_STOP, data);
  353. else if (sh_mobile_i2c_is_first_byte(pd))
  354. i2c_op(pd, OP_TX_FIRST, data);
  355. else
  356. i2c_op(pd, OP_TX, data);
  357. pd->pos++;
  358. return 0;
  359. }
  360. static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
  361. {
  362. unsigned char data;
  363. int real_pos;
  364. do {
  365. if (pd->pos <= -1) {
  366. sh_mobile_i2c_get_data(pd, &data);
  367. if (sh_mobile_i2c_is_first_byte(pd))
  368. i2c_op(pd, OP_TX_FIRST, data);
  369. else
  370. i2c_op(pd, OP_TX, data);
  371. break;
  372. }
  373. if (pd->pos == 0) {
  374. i2c_op(pd, OP_TX_TO_RX, 0);
  375. break;
  376. }
  377. real_pos = pd->pos - 2;
  378. if (pd->pos == pd->msg->len) {
  379. if (real_pos < 0) {
  380. i2c_op(pd, OP_RX_STOP, 0);
  381. break;
  382. }
  383. data = i2c_op(pd, OP_RX_STOP_DATA, 0);
  384. } else
  385. data = i2c_op(pd, OP_RX, 0);
  386. if (real_pos >= 0)
  387. pd->msg->buf[real_pos] = data;
  388. } while (0);
  389. pd->pos++;
  390. return pd->pos == (pd->msg->len + 2);
  391. }
  392. static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
  393. {
  394. struct platform_device *dev = dev_id;
  395. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  396. unsigned char sr;
  397. int wakeup;
  398. sr = iic_rd(pd, ICSR);
  399. pd->sr |= sr; /* remember state */
  400. dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
  401. (pd->msg->flags & I2C_M_RD) ? "read" : "write",
  402. pd->pos, pd->msg->len);
  403. if (sr & (ICSR_AL | ICSR_TACK)) {
  404. /* don't interrupt transaction - continue to issue stop */
  405. iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
  406. wakeup = 0;
  407. } else if (pd->msg->flags & I2C_M_RD)
  408. wakeup = sh_mobile_i2c_isr_rx(pd);
  409. else
  410. wakeup = sh_mobile_i2c_isr_tx(pd);
  411. if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
  412. iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
  413. if (wakeup) {
  414. pd->sr |= SW_DONE;
  415. wake_up(&pd->wait);
  416. }
  417. /* defeat write posting to avoid spurious WAIT interrupts */
  418. iic_rd(pd, ICSR);
  419. return IRQ_HANDLED;
  420. }
  421. static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
  422. bool do_init)
  423. {
  424. if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
  425. dev_err(pd->dev, "Unsupported zero length i2c read\n");
  426. return -EOPNOTSUPP;
  427. }
  428. if (do_init) {
  429. /* Initialize channel registers */
  430. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  431. /* Enable channel and configure rx ack */
  432. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  433. /* Set the clock */
  434. iic_wr(pd, ICCL, pd->iccl & 0xff);
  435. iic_wr(pd, ICCH, pd->icch & 0xff);
  436. }
  437. pd->msg = usr_msg;
  438. pd->pos = -1;
  439. pd->sr = 0;
  440. /* Enable all interrupts to begin with */
  441. iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  442. return 0;
  443. }
  444. static int poll_dte(struct sh_mobile_i2c_data *pd)
  445. {
  446. int i;
  447. for (i = 1000; i; i--) {
  448. u_int8_t val = iic_rd(pd, ICSR);
  449. if (val & ICSR_DTE)
  450. break;
  451. if (val & ICSR_TACK)
  452. return -ENXIO;
  453. udelay(10);
  454. }
  455. return i ? 0 : -ETIMEDOUT;
  456. }
  457. static int poll_busy(struct sh_mobile_i2c_data *pd)
  458. {
  459. int i;
  460. for (i = 1000; i; i--) {
  461. u_int8_t val = iic_rd(pd, ICSR);
  462. dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
  463. /* the interrupt handler may wake us up before the
  464. * transfer is finished, so poll the hardware
  465. * until we're done.
  466. */
  467. if (!(val & ICSR_BUSY)) {
  468. /* handle missing acknowledge and arbitration lost */
  469. val |= pd->sr;
  470. if (val & ICSR_TACK)
  471. return -ENXIO;
  472. if (val & ICSR_AL)
  473. return -EAGAIN;
  474. break;
  475. }
  476. udelay(10);
  477. }
  478. return i ? 0 : -ETIMEDOUT;
  479. }
  480. static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
  481. struct i2c_msg *msgs,
  482. int num)
  483. {
  484. struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
  485. struct i2c_msg *msg;
  486. int err = 0;
  487. int i, k;
  488. activate_ch(pd);
  489. /* Process all messages */
  490. for (i = 0; i < num; i++) {
  491. bool do_start = pd->send_stop || !i;
  492. msg = &msgs[i];
  493. pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP;
  494. err = start_ch(pd, msg, do_start);
  495. if (err)
  496. break;
  497. if (do_start)
  498. i2c_op(pd, OP_START, 0);
  499. /* The interrupt handler takes care of the rest... */
  500. k = wait_event_timeout(pd->wait,
  501. pd->sr & (ICSR_TACK | SW_DONE),
  502. 5 * HZ);
  503. if (!k) {
  504. dev_err(pd->dev, "Transfer request timed out\n");
  505. err = -ETIMEDOUT;
  506. break;
  507. }
  508. if (pd->send_stop)
  509. err = poll_busy(pd);
  510. else
  511. err = poll_dte(pd);
  512. if (err < 0)
  513. break;
  514. }
  515. deactivate_ch(pd);
  516. if (!err)
  517. err = num;
  518. return err;
  519. }
  520. static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
  521. {
  522. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  523. }
  524. static struct i2c_algorithm sh_mobile_i2c_algorithm = {
  525. .functionality = sh_mobile_i2c_func,
  526. .master_xfer = sh_mobile_i2c_xfer,
  527. };
  528. static const struct sh_mobile_dt_config default_dt_config = {
  529. .clks_per_count = 1,
  530. };
  531. static const struct sh_mobile_dt_config rcar_gen2_dt_config = {
  532. .clks_per_count = 2,
  533. };
  534. static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
  535. { .compatible = "renesas,rmobile-iic", .data = &default_dt_config },
  536. { .compatible = "renesas,iic-r8a7790", .data = &rcar_gen2_dt_config },
  537. { .compatible = "renesas,iic-r8a7791", .data = &rcar_gen2_dt_config },
  538. { .compatible = "renesas,iic-r8a7792", .data = &rcar_gen2_dt_config },
  539. { .compatible = "renesas,iic-r8a7793", .data = &rcar_gen2_dt_config },
  540. { .compatible = "renesas,iic-r8a7794", .data = &rcar_gen2_dt_config },
  541. {},
  542. };
  543. MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
  544. static int sh_mobile_i2c_hook_irqs(struct platform_device *dev)
  545. {
  546. struct resource *res;
  547. resource_size_t n;
  548. int k = 0, ret;
  549. while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
  550. for (n = res->start; n <= res->end; n++) {
  551. ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr,
  552. 0, dev_name(&dev->dev), dev);
  553. if (ret) {
  554. dev_err(&dev->dev, "cannot request IRQ %pa\n", &n);
  555. return ret;
  556. }
  557. }
  558. k++;
  559. }
  560. return k > 0 ? 0 : -ENOENT;
  561. }
  562. static int sh_mobile_i2c_probe(struct platform_device *dev)
  563. {
  564. struct i2c_sh_mobile_platform_data *pdata = dev_get_platdata(&dev->dev);
  565. struct sh_mobile_i2c_data *pd;
  566. struct i2c_adapter *adap;
  567. struct resource *res;
  568. int ret;
  569. u32 bus_speed;
  570. pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
  571. if (!pd)
  572. return -ENOMEM;
  573. pd->clk = devm_clk_get(&dev->dev, NULL);
  574. if (IS_ERR(pd->clk)) {
  575. dev_err(&dev->dev, "cannot get clock\n");
  576. return PTR_ERR(pd->clk);
  577. }
  578. ret = sh_mobile_i2c_hook_irqs(dev);
  579. if (ret)
  580. return ret;
  581. pd->dev = &dev->dev;
  582. platform_set_drvdata(dev, pd);
  583. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  584. pd->reg = devm_ioremap_resource(&dev->dev, res);
  585. if (IS_ERR(pd->reg))
  586. return PTR_ERR(pd->reg);
  587. /* Use platform data bus speed or STANDARD_MODE */
  588. ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed);
  589. pd->bus_speed = ret ? STANDARD_MODE : bus_speed;
  590. pd->clks_per_count = 1;
  591. if (dev->dev.of_node) {
  592. const struct of_device_id *match;
  593. match = of_match_device(sh_mobile_i2c_dt_ids, &dev->dev);
  594. if (match) {
  595. const struct sh_mobile_dt_config *config;
  596. config = match->data;
  597. pd->clks_per_count = config->clks_per_count;
  598. }
  599. } else {
  600. if (pdata && pdata->bus_speed)
  601. pd->bus_speed = pdata->bus_speed;
  602. if (pdata && pdata->clks_per_count)
  603. pd->clks_per_count = pdata->clks_per_count;
  604. }
  605. /* The IIC blocks on SH-Mobile ARM processors
  606. * come with two new bits in ICIC.
  607. */
  608. if (resource_size(res) > 0x17)
  609. pd->flags |= IIC_FLAG_HAS_ICIC67;
  610. ret = sh_mobile_i2c_init(pd);
  611. if (ret)
  612. return ret;
  613. /* Enable Runtime PM for this device.
  614. *
  615. * Also tell the Runtime PM core to ignore children
  616. * for this device since it is valid for us to suspend
  617. * this I2C master driver even though the slave devices
  618. * on the I2C bus may not be suspended.
  619. *
  620. * The state of the I2C hardware bus is unaffected by
  621. * the Runtime PM state.
  622. */
  623. pm_suspend_ignore_children(&dev->dev, true);
  624. pm_runtime_enable(&dev->dev);
  625. /* setup the private data */
  626. adap = &pd->adap;
  627. i2c_set_adapdata(adap, pd);
  628. adap->owner = THIS_MODULE;
  629. adap->algo = &sh_mobile_i2c_algorithm;
  630. adap->dev.parent = &dev->dev;
  631. adap->retries = 5;
  632. adap->nr = dev->id;
  633. adap->dev.of_node = dev->dev.of_node;
  634. strlcpy(adap->name, dev->name, sizeof(adap->name));
  635. spin_lock_init(&pd->lock);
  636. init_waitqueue_head(&pd->wait);
  637. ret = i2c_add_numbered_adapter(adap);
  638. if (ret < 0) {
  639. dev_err(&dev->dev, "cannot add numbered adapter\n");
  640. return ret;
  641. }
  642. dev_info(&dev->dev,
  643. "I2C adapter %d with bus speed %lu Hz (L/H=0x%x/0x%x)\n",
  644. adap->nr, pd->bus_speed, pd->iccl, pd->icch);
  645. return 0;
  646. }
  647. static int sh_mobile_i2c_remove(struct platform_device *dev)
  648. {
  649. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  650. i2c_del_adapter(&pd->adap);
  651. pm_runtime_disable(&dev->dev);
  652. return 0;
  653. }
  654. static int sh_mobile_i2c_runtime_nop(struct device *dev)
  655. {
  656. /* Runtime PM callback shared between ->runtime_suspend()
  657. * and ->runtime_resume(). Simply returns success.
  658. *
  659. * This driver re-initializes all registers after
  660. * pm_runtime_get_sync() anyway so there is no need
  661. * to save and restore registers here.
  662. */
  663. return 0;
  664. }
  665. static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
  666. .runtime_suspend = sh_mobile_i2c_runtime_nop,
  667. .runtime_resume = sh_mobile_i2c_runtime_nop,
  668. };
  669. static struct platform_driver sh_mobile_i2c_driver = {
  670. .driver = {
  671. .name = "i2c-sh_mobile",
  672. .owner = THIS_MODULE,
  673. .pm = &sh_mobile_i2c_dev_pm_ops,
  674. .of_match_table = sh_mobile_i2c_dt_ids,
  675. },
  676. .probe = sh_mobile_i2c_probe,
  677. .remove = sh_mobile_i2c_remove,
  678. };
  679. static int __init sh_mobile_i2c_adap_init(void)
  680. {
  681. return platform_driver_register(&sh_mobile_i2c_driver);
  682. }
  683. static void __exit sh_mobile_i2c_adap_exit(void)
  684. {
  685. platform_driver_unregister(&sh_mobile_i2c_driver);
  686. }
  687. subsys_initcall(sh_mobile_i2c_adap_init);
  688. module_exit(sh_mobile_i2c_adap_exit);
  689. MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
  690. MODULE_AUTHOR("Magnus Damm");
  691. MODULE_LICENSE("GPL v2");
  692. MODULE_ALIAS("platform:i2c-sh_mobile");