i2c-sh_mobile.c 20 KB

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