i2c-sh_mobile.c 20 KB

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