i2c-sh_mobile.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. enum sh_mobile_i2c_op {
  105. OP_START = 0,
  106. OP_TX_FIRST,
  107. OP_TX,
  108. OP_TX_STOP,
  109. OP_TX_TO_RX,
  110. OP_RX,
  111. OP_RX_STOP,
  112. OP_RX_STOP_DATA,
  113. };
  114. struct sh_mobile_i2c_data {
  115. struct device *dev;
  116. void __iomem *reg;
  117. struct i2c_adapter adap;
  118. unsigned long bus_speed;
  119. struct clk *clk;
  120. u_int8_t icic;
  121. u_int8_t flags;
  122. u_int16_t iccl;
  123. u_int16_t icch;
  124. spinlock_t lock;
  125. wait_queue_head_t wait;
  126. struct i2c_msg *msg;
  127. int pos;
  128. int sr;
  129. };
  130. #define IIC_FLAG_HAS_ICIC67 (1 << 0)
  131. #define STANDARD_MODE 100000
  132. #define FAST_MODE 400000
  133. /* Register offsets */
  134. #define ICDR 0x00
  135. #define ICCR 0x04
  136. #define ICSR 0x08
  137. #define ICIC 0x0c
  138. #define ICCL 0x10
  139. #define ICCH 0x14
  140. /* Register bits */
  141. #define ICCR_ICE 0x80
  142. #define ICCR_RACK 0x40
  143. #define ICCR_TRS 0x10
  144. #define ICCR_BBSY 0x04
  145. #define ICCR_SCP 0x01
  146. #define ICSR_SCLM 0x80
  147. #define ICSR_SDAM 0x40
  148. #define SW_DONE 0x20
  149. #define ICSR_BUSY 0x10
  150. #define ICSR_AL 0x08
  151. #define ICSR_TACK 0x04
  152. #define ICSR_WAIT 0x02
  153. #define ICSR_DTE 0x01
  154. #define ICIC_ICCLB8 0x80
  155. #define ICIC_ICCHB8 0x40
  156. #define ICIC_ALE 0x08
  157. #define ICIC_TACKE 0x04
  158. #define ICIC_WAITE 0x02
  159. #define ICIC_DTEE 0x01
  160. static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
  161. {
  162. if (offs == ICIC)
  163. data |= pd->icic;
  164. iowrite8(data, pd->reg + offs);
  165. }
  166. static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
  167. {
  168. return ioread8(pd->reg + offs);
  169. }
  170. static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
  171. unsigned char set, unsigned char clr)
  172. {
  173. iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
  174. }
  175. static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf, int offset)
  176. {
  177. /*
  178. * Conditional expression:
  179. * ICCL >= COUNT_CLK * (tLOW + tf)
  180. *
  181. * SH-Mobile IIC hardware starts counting the LOW period of
  182. * the SCL signal (tLOW) as soon as it pulls the SCL line.
  183. * In order to meet the tLOW timing spec, we need to take into
  184. * account the fall time of SCL signal (tf). Default tf value
  185. * should be 0.3 us, for safety.
  186. */
  187. return (((count_khz * (tLOW + tf)) + 5000) / 10000) + offset;
  188. }
  189. static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf, int offset)
  190. {
  191. /*
  192. * Conditional expression:
  193. * ICCH >= COUNT_CLK * (tHIGH + tf)
  194. *
  195. * SH-Mobile IIC hardware is aware of SCL transition period 'tr',
  196. * and can ignore it. SH-Mobile IIC controller starts counting
  197. * the HIGH period of the SCL signal (tHIGH) after the SCL input
  198. * voltage increases at VIH.
  199. *
  200. * Afterward it turned out calculating ICCH using only tHIGH spec
  201. * will result in violation of the tHD;STA timing spec. We need
  202. * to take into account the fall time of SDA signal (tf) at START
  203. * condition, in order to meet both tHIGH and tHD;STA specs.
  204. */
  205. return (((count_khz * (tHIGH + tf)) + 5000) / 10000) + offset;
  206. }
  207. static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
  208. {
  209. unsigned long i2c_clk_khz;
  210. u32 tHIGH, tLOW, tf;
  211. int offset;
  212. /* Get clock rate after clock is enabled */
  213. clk_enable(pd->clk);
  214. i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
  215. if (pd->bus_speed == STANDARD_MODE) {
  216. tLOW = 47; /* tLOW = 4.7 us */
  217. tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
  218. tf = 3; /* tf = 0.3 us */
  219. offset = 0; /* No offset */
  220. } else if (pd->bus_speed == FAST_MODE) {
  221. tLOW = 13; /* tLOW = 1.3 us */
  222. tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
  223. tf = 3; /* tf = 0.3 us */
  224. offset = 0; /* No offset */
  225. } else {
  226. dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
  227. pd->bus_speed);
  228. goto out;
  229. }
  230. pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf, offset);
  231. /* one more bit of ICCL in ICIC */
  232. if ((pd->iccl > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
  233. pd->icic |= ICIC_ICCLB8;
  234. else
  235. pd->icic &= ~ICIC_ICCLB8;
  236. pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf, offset);
  237. /* one more bit of ICCH in ICIC */
  238. if ((pd->icch > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
  239. pd->icic |= ICIC_ICCHB8;
  240. else
  241. pd->icic &= ~ICIC_ICCHB8;
  242. out:
  243. clk_disable(pd->clk);
  244. }
  245. static void activate_ch(struct sh_mobile_i2c_data *pd)
  246. {
  247. /* Wake up device and enable clock */
  248. pm_runtime_get_sync(pd->dev);
  249. clk_enable(pd->clk);
  250. /* Enable channel and configure rx ack */
  251. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  252. /* Mask all interrupts */
  253. iic_wr(pd, ICIC, 0);
  254. /* Set the clock */
  255. iic_wr(pd, ICCL, pd->iccl & 0xff);
  256. iic_wr(pd, ICCH, pd->icch & 0xff);
  257. }
  258. static void deactivate_ch(struct sh_mobile_i2c_data *pd)
  259. {
  260. /* Clear/disable interrupts */
  261. iic_wr(pd, ICSR, 0);
  262. iic_wr(pd, ICIC, 0);
  263. /* Disable channel */
  264. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  265. /* Disable clock and mark device as idle */
  266. clk_disable(pd->clk);
  267. pm_runtime_put_sync(pd->dev);
  268. }
  269. static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
  270. enum sh_mobile_i2c_op op, unsigned char data)
  271. {
  272. unsigned char ret = 0;
  273. unsigned long flags;
  274. dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
  275. spin_lock_irqsave(&pd->lock, flags);
  276. switch (op) {
  277. case OP_START: /* issue start and trigger DTE interrupt */
  278. iic_wr(pd, ICCR, 0x94);
  279. break;
  280. case OP_TX_FIRST: /* disable DTE interrupt and write data */
  281. iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  282. iic_wr(pd, ICDR, data);
  283. break;
  284. case OP_TX: /* write data */
  285. iic_wr(pd, ICDR, data);
  286. break;
  287. case OP_TX_STOP: /* write data and issue a stop afterwards */
  288. iic_wr(pd, ICDR, data);
  289. iic_wr(pd, ICCR, 0x90);
  290. break;
  291. case OP_TX_TO_RX: /* select read mode */
  292. iic_wr(pd, ICCR, 0x81);
  293. break;
  294. case OP_RX: /* just read data */
  295. ret = iic_rd(pd, ICDR);
  296. break;
  297. case OP_RX_STOP: /* enable DTE interrupt, issue stop */
  298. iic_wr(pd, ICIC,
  299. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  300. iic_wr(pd, ICCR, 0xc0);
  301. break;
  302. case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
  303. iic_wr(pd, ICIC,
  304. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  305. ret = iic_rd(pd, ICDR);
  306. iic_wr(pd, ICCR, 0xc0);
  307. break;
  308. }
  309. spin_unlock_irqrestore(&pd->lock, flags);
  310. dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
  311. return ret;
  312. }
  313. static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
  314. {
  315. if (pd->pos == -1)
  316. return 1;
  317. return 0;
  318. }
  319. static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
  320. {
  321. if (pd->pos == (pd->msg->len - 1))
  322. return 1;
  323. return 0;
  324. }
  325. static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
  326. unsigned char *buf)
  327. {
  328. switch (pd->pos) {
  329. case -1:
  330. *buf = (pd->msg->addr & 0x7f) << 1;
  331. *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
  332. break;
  333. default:
  334. *buf = pd->msg->buf[pd->pos];
  335. }
  336. }
  337. static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
  338. {
  339. unsigned char data;
  340. if (pd->pos == pd->msg->len)
  341. return 1;
  342. sh_mobile_i2c_get_data(pd, &data);
  343. if (sh_mobile_i2c_is_last_byte(pd))
  344. i2c_op(pd, OP_TX_STOP, data);
  345. else if (sh_mobile_i2c_is_first_byte(pd))
  346. i2c_op(pd, OP_TX_FIRST, data);
  347. else
  348. i2c_op(pd, OP_TX, data);
  349. pd->pos++;
  350. return 0;
  351. }
  352. static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
  353. {
  354. unsigned char data;
  355. int real_pos;
  356. do {
  357. if (pd->pos <= -1) {
  358. sh_mobile_i2c_get_data(pd, &data);
  359. if (sh_mobile_i2c_is_first_byte(pd))
  360. i2c_op(pd, OP_TX_FIRST, data);
  361. else
  362. i2c_op(pd, OP_TX, data);
  363. break;
  364. }
  365. if (pd->pos == 0) {
  366. i2c_op(pd, OP_TX_TO_RX, 0);
  367. break;
  368. }
  369. real_pos = pd->pos - 2;
  370. if (pd->pos == pd->msg->len) {
  371. if (real_pos < 0) {
  372. i2c_op(pd, OP_RX_STOP, 0);
  373. break;
  374. }
  375. data = i2c_op(pd, OP_RX_STOP_DATA, 0);
  376. } else
  377. data = i2c_op(pd, OP_RX, 0);
  378. if (real_pos >= 0)
  379. pd->msg->buf[real_pos] = data;
  380. } while (0);
  381. pd->pos++;
  382. return pd->pos == (pd->msg->len + 2);
  383. }
  384. static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
  385. {
  386. struct platform_device *dev = dev_id;
  387. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  388. unsigned char sr;
  389. int wakeup;
  390. sr = iic_rd(pd, ICSR);
  391. pd->sr |= sr; /* remember state */
  392. dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
  393. (pd->msg->flags & I2C_M_RD) ? "read" : "write",
  394. pd->pos, pd->msg->len);
  395. if (sr & (ICSR_AL | ICSR_TACK)) {
  396. /* don't interrupt transaction - continue to issue stop */
  397. iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
  398. wakeup = 0;
  399. } else if (pd->msg->flags & I2C_M_RD)
  400. wakeup = sh_mobile_i2c_isr_rx(pd);
  401. else
  402. wakeup = sh_mobile_i2c_isr_tx(pd);
  403. if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
  404. iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
  405. if (wakeup) {
  406. pd->sr |= SW_DONE;
  407. wake_up(&pd->wait);
  408. }
  409. return IRQ_HANDLED;
  410. }
  411. static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg)
  412. {
  413. if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
  414. dev_err(pd->dev, "Unsupported zero length i2c read\n");
  415. return -EIO;
  416. }
  417. /* Initialize channel registers */
  418. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  419. /* Enable channel and configure rx ack */
  420. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  421. /* Set the clock */
  422. iic_wr(pd, ICCL, pd->iccl & 0xff);
  423. iic_wr(pd, ICCH, pd->icch & 0xff);
  424. pd->msg = usr_msg;
  425. pd->pos = -1;
  426. pd->sr = 0;
  427. /* Enable all interrupts to begin with */
  428. iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  429. return 0;
  430. }
  431. static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
  432. struct i2c_msg *msgs,
  433. int num)
  434. {
  435. struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
  436. struct i2c_msg *msg;
  437. int err = 0;
  438. u_int8_t val;
  439. int i, k, retry_count;
  440. activate_ch(pd);
  441. /* Process all messages */
  442. for (i = 0; i < num; i++) {
  443. msg = &msgs[i];
  444. err = start_ch(pd, msg);
  445. if (err)
  446. break;
  447. i2c_op(pd, OP_START, 0);
  448. /* The interrupt handler takes care of the rest... */
  449. k = wait_event_timeout(pd->wait,
  450. pd->sr & (ICSR_TACK | SW_DONE),
  451. 5 * HZ);
  452. if (!k)
  453. dev_err(pd->dev, "Transfer request timed out\n");
  454. retry_count = 1000;
  455. again:
  456. val = iic_rd(pd, ICSR);
  457. dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
  458. /* the interrupt handler may wake us up before the
  459. * transfer is finished, so poll the hardware
  460. * until we're done.
  461. */
  462. if (val & ICSR_BUSY) {
  463. udelay(10);
  464. if (retry_count--)
  465. goto again;
  466. err = -EIO;
  467. dev_err(pd->dev, "Polling timed out\n");
  468. break;
  469. }
  470. /* handle missing acknowledge and arbitration lost */
  471. if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) {
  472. err = -EIO;
  473. break;
  474. }
  475. }
  476. deactivate_ch(pd);
  477. if (!err)
  478. err = num;
  479. return err;
  480. }
  481. static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
  482. {
  483. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  484. }
  485. static struct i2c_algorithm sh_mobile_i2c_algorithm = {
  486. .functionality = sh_mobile_i2c_func,
  487. .master_xfer = sh_mobile_i2c_xfer,
  488. };
  489. static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook)
  490. {
  491. struct resource *res;
  492. int ret = -ENXIO;
  493. int n, k = 0;
  494. while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
  495. for (n = res->start; hook && n <= res->end; n++) {
  496. if (request_irq(n, sh_mobile_i2c_isr, 0,
  497. dev_name(&dev->dev), dev)) {
  498. for (n--; n >= res->start; n--)
  499. free_irq(n, dev);
  500. goto rollback;
  501. }
  502. }
  503. k++;
  504. }
  505. if (hook)
  506. return k > 0 ? 0 : -ENOENT;
  507. ret = 0;
  508. rollback:
  509. k--;
  510. while (k >= 0) {
  511. res = platform_get_resource(dev, IORESOURCE_IRQ, k);
  512. for (n = res->start; n <= res->end; n++)
  513. free_irq(n, dev);
  514. k--;
  515. }
  516. return ret;
  517. }
  518. static int sh_mobile_i2c_probe(struct platform_device *dev)
  519. {
  520. struct i2c_sh_mobile_platform_data *pdata = dev->dev.platform_data;
  521. struct sh_mobile_i2c_data *pd;
  522. struct i2c_adapter *adap;
  523. struct resource *res;
  524. int size;
  525. int ret;
  526. pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
  527. if (pd == NULL) {
  528. dev_err(&dev->dev, "cannot allocate private data\n");
  529. return -ENOMEM;
  530. }
  531. pd->clk = clk_get(&dev->dev, NULL);
  532. if (IS_ERR(pd->clk)) {
  533. dev_err(&dev->dev, "cannot get clock\n");
  534. ret = PTR_ERR(pd->clk);
  535. goto err;
  536. }
  537. ret = sh_mobile_i2c_hook_irqs(dev, 1);
  538. if (ret) {
  539. dev_err(&dev->dev, "cannot request IRQ\n");
  540. goto err_clk;
  541. }
  542. pd->dev = &dev->dev;
  543. platform_set_drvdata(dev, pd);
  544. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  545. if (res == NULL) {
  546. dev_err(&dev->dev, "cannot find IO resource\n");
  547. ret = -ENOENT;
  548. goto err_irq;
  549. }
  550. size = resource_size(res);
  551. pd->reg = ioremap(res->start, size);
  552. if (pd->reg == NULL) {
  553. dev_err(&dev->dev, "cannot map IO\n");
  554. ret = -ENXIO;
  555. goto err_irq;
  556. }
  557. /* Use platform data bus speed or STANDARD_MODE */
  558. pd->bus_speed = STANDARD_MODE;
  559. if (pdata && pdata->bus_speed)
  560. pd->bus_speed = pdata->bus_speed;
  561. /* The IIC blocks on SH-Mobile ARM processors
  562. * come with two new bits in ICIC.
  563. */
  564. if (size > 0x17)
  565. pd->flags |= IIC_FLAG_HAS_ICIC67;
  566. sh_mobile_i2c_init(pd);
  567. /* Enable Runtime PM for this device.
  568. *
  569. * Also tell the Runtime PM core to ignore children
  570. * for this device since it is valid for us to suspend
  571. * this I2C master driver even though the slave devices
  572. * on the I2C bus may not be suspended.
  573. *
  574. * The state of the I2C hardware bus is unaffected by
  575. * the Runtime PM state.
  576. */
  577. pm_suspend_ignore_children(&dev->dev, true);
  578. pm_runtime_enable(&dev->dev);
  579. /* setup the private data */
  580. adap = &pd->adap;
  581. i2c_set_adapdata(adap, pd);
  582. adap->owner = THIS_MODULE;
  583. adap->algo = &sh_mobile_i2c_algorithm;
  584. adap->dev.parent = &dev->dev;
  585. adap->retries = 5;
  586. adap->nr = dev->id;
  587. adap->dev.of_node = dev->dev.of_node;
  588. strlcpy(adap->name, dev->name, sizeof(adap->name));
  589. spin_lock_init(&pd->lock);
  590. init_waitqueue_head(&pd->wait);
  591. ret = i2c_add_numbered_adapter(adap);
  592. if (ret < 0) {
  593. dev_err(&dev->dev, "cannot add numbered adapter\n");
  594. goto err_all;
  595. }
  596. dev_info(&dev->dev,
  597. "I2C adapter %d with bus speed %lu Hz (L/H=%x/%x)\n",
  598. adap->nr, pd->bus_speed, pd->iccl, pd->icch);
  599. of_i2c_register_devices(adap);
  600. return 0;
  601. err_all:
  602. iounmap(pd->reg);
  603. err_irq:
  604. sh_mobile_i2c_hook_irqs(dev, 0);
  605. err_clk:
  606. clk_put(pd->clk);
  607. err:
  608. kfree(pd);
  609. return ret;
  610. }
  611. static int sh_mobile_i2c_remove(struct platform_device *dev)
  612. {
  613. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  614. i2c_del_adapter(&pd->adap);
  615. iounmap(pd->reg);
  616. sh_mobile_i2c_hook_irqs(dev, 0);
  617. clk_put(pd->clk);
  618. pm_runtime_disable(&dev->dev);
  619. kfree(pd);
  620. return 0;
  621. }
  622. static int sh_mobile_i2c_runtime_nop(struct device *dev)
  623. {
  624. /* Runtime PM callback shared between ->runtime_suspend()
  625. * and ->runtime_resume(). Simply returns success.
  626. *
  627. * This driver re-initializes all registers after
  628. * pm_runtime_get_sync() anyway so there is no need
  629. * to save and restore registers here.
  630. */
  631. return 0;
  632. }
  633. static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
  634. .runtime_suspend = sh_mobile_i2c_runtime_nop,
  635. .runtime_resume = sh_mobile_i2c_runtime_nop,
  636. };
  637. static const struct of_device_id sh_mobile_i2c_dt_ids[] __devinitconst = {
  638. { .compatible = "renesas,rmobile-iic", },
  639. {},
  640. };
  641. MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
  642. static struct platform_driver sh_mobile_i2c_driver = {
  643. .driver = {
  644. .name = "i2c-sh_mobile",
  645. .owner = THIS_MODULE,
  646. .pm = &sh_mobile_i2c_dev_pm_ops,
  647. .of_match_table = sh_mobile_i2c_dt_ids,
  648. },
  649. .probe = sh_mobile_i2c_probe,
  650. .remove = sh_mobile_i2c_remove,
  651. };
  652. static int __init sh_mobile_i2c_adap_init(void)
  653. {
  654. return platform_driver_register(&sh_mobile_i2c_driver);
  655. }
  656. static void __exit sh_mobile_i2c_adap_exit(void)
  657. {
  658. platform_driver_unregister(&sh_mobile_i2c_driver);
  659. }
  660. subsys_initcall(sh_mobile_i2c_adap_init);
  661. module_exit(sh_mobile_i2c_adap_exit);
  662. MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
  663. MODULE_AUTHOR("Magnus Damm");
  664. MODULE_LICENSE("GPL v2");
  665. MODULE_ALIAS("platform:i2c-sh_mobile");