i2c-sh_mobile.c 23 KB

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