i2c-sh_mobile.c 20 KB

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