i2c-ibm_iic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * drivers/i2c/busses/i2c-ibm_iic.c
  3. *
  4. * Support for the IIC peripheral on IBM PPC 4xx
  5. *
  6. * Copyright (c) 2003, 2004 Zultys Technologies.
  7. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  8. *
  9. * Copyright (c) 2008 PIKA Technologies
  10. * Sean MacLennan <smaclennan@pikatech.com>
  11. *
  12. * Based on original work by
  13. * Ian DaSilva <idasilva@mvista.com>
  14. * Armin Kuster <akuster@mvista.com>
  15. * Matt Porter <mporter@mvista.com>
  16. *
  17. * Copyright 2000-2003 MontaVista Software Inc.
  18. *
  19. * Original driver version was highly leveraged from i2c-elektor.c
  20. *
  21. * Copyright 1995-97 Simon G. Vogl
  22. * 1998-99 Hans Berglund
  23. *
  24. * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
  25. * and even Frodo Looijaard <frodol@dds.nl>
  26. *
  27. * This program is free software; you can redistribute it and/or modify it
  28. * under the terms of the GNU General Public License as published by the
  29. * Free Software Foundation; either version 2 of the License, or (at your
  30. * option) any later version.
  31. *
  32. */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/ioport.h>
  36. #include <linux/delay.h>
  37. #include <linux/slab.h>
  38. #include <linux/interrupt.h>
  39. #include <asm/irq.h>
  40. #include <linux/io.h>
  41. #include <linux/i2c.h>
  42. #include <linux/of_address.h>
  43. #include <linux/of_irq.h>
  44. #include <linux/of_platform.h>
  45. #include "i2c-ibm_iic.h"
  46. #define DRIVER_VERSION "2.2"
  47. MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
  48. MODULE_LICENSE("GPL");
  49. static bool iic_force_poll;
  50. module_param(iic_force_poll, bool, 0);
  51. MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
  52. static bool iic_force_fast;
  53. module_param(iic_force_fast, bool, 0);
  54. MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
  55. #define DBG_LEVEL 0
  56. #ifdef DBG
  57. #undef DBG
  58. #endif
  59. #ifdef DBG2
  60. #undef DBG2
  61. #endif
  62. #if DBG_LEVEL > 0
  63. # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
  64. #else
  65. # define DBG(f,x...) ((void)0)
  66. #endif
  67. #if DBG_LEVEL > 1
  68. # define DBG2(f,x...) DBG(f, ##x)
  69. #else
  70. # define DBG2(f,x...) ((void)0)
  71. #endif
  72. #if DBG_LEVEL > 2
  73. static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
  74. {
  75. volatile struct iic_regs __iomem *iic = dev->vaddr;
  76. printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
  77. printk(KERN_DEBUG
  78. " cntl = 0x%02x, mdcntl = 0x%02x\n"
  79. " sts = 0x%02x, extsts = 0x%02x\n"
  80. " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
  81. " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
  82. in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
  83. in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
  84. in_8(&iic->xtcntlss), in_8(&iic->directcntl));
  85. }
  86. # define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
  87. #else
  88. # define DUMP_REGS(h,dev) ((void)0)
  89. #endif
  90. /* Bus timings (in ns) for bit-banging */
  91. static struct i2c_timings {
  92. unsigned int hd_sta;
  93. unsigned int su_sto;
  94. unsigned int low;
  95. unsigned int high;
  96. unsigned int buf;
  97. } timings [] = {
  98. /* Standard mode (100 KHz) */
  99. {
  100. .hd_sta = 4000,
  101. .su_sto = 4000,
  102. .low = 4700,
  103. .high = 4000,
  104. .buf = 4700,
  105. },
  106. /* Fast mode (400 KHz) */
  107. {
  108. .hd_sta = 600,
  109. .su_sto = 600,
  110. .low = 1300,
  111. .high = 600,
  112. .buf = 1300,
  113. }};
  114. /* Enable/disable interrupt generation */
  115. static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
  116. {
  117. out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
  118. }
  119. /*
  120. * Initialize IIC interface.
  121. */
  122. static void iic_dev_init(struct ibm_iic_private* dev)
  123. {
  124. volatile struct iic_regs __iomem *iic = dev->vaddr;
  125. DBG("%d: init\n", dev->idx);
  126. /* Clear master address */
  127. out_8(&iic->lmadr, 0);
  128. out_8(&iic->hmadr, 0);
  129. /* Clear slave address */
  130. out_8(&iic->lsadr, 0);
  131. out_8(&iic->hsadr, 0);
  132. /* Clear status & extended status */
  133. out_8(&iic->sts, STS_SCMP | STS_IRQA);
  134. out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
  135. | EXTSTS_ICT | EXTSTS_XFRA);
  136. /* Set clock divider */
  137. out_8(&iic->clkdiv, dev->clckdiv);
  138. /* Clear transfer count */
  139. out_8(&iic->xfrcnt, 0);
  140. /* Clear extended control and status */
  141. out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
  142. | XTCNTLSS_SWS);
  143. /* Clear control register */
  144. out_8(&iic->cntl, 0);
  145. /* Enable interrupts if possible */
  146. iic_interrupt_mode(dev, dev->irq >= 0);
  147. /* Set mode control */
  148. out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
  149. | (dev->fast_mode ? MDCNTL_FSM : 0));
  150. DUMP_REGS("iic_init", dev);
  151. }
  152. /*
  153. * Reset IIC interface
  154. */
  155. static void iic_dev_reset(struct ibm_iic_private* dev)
  156. {
  157. volatile struct iic_regs __iomem *iic = dev->vaddr;
  158. int i;
  159. u8 dc;
  160. DBG("%d: soft reset\n", dev->idx);
  161. DUMP_REGS("reset", dev);
  162. /* Place chip in the reset state */
  163. out_8(&iic->xtcntlss, XTCNTLSS_SRST);
  164. /* Check if bus is free */
  165. dc = in_8(&iic->directcntl);
  166. if (!DIRCTNL_FREE(dc)){
  167. DBG("%d: trying to regain bus control\n", dev->idx);
  168. /* Try to set bus free state */
  169. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  170. /* Wait until we regain bus control */
  171. for (i = 0; i < 100; ++i){
  172. dc = in_8(&iic->directcntl);
  173. if (DIRCTNL_FREE(dc))
  174. break;
  175. /* Toggle SCL line */
  176. dc ^= DIRCNTL_SCC;
  177. out_8(&iic->directcntl, dc);
  178. udelay(10);
  179. dc ^= DIRCNTL_SCC;
  180. out_8(&iic->directcntl, dc);
  181. /* be nice */
  182. cond_resched();
  183. }
  184. }
  185. /* Remove reset */
  186. out_8(&iic->xtcntlss, 0);
  187. /* Reinitialize interface */
  188. iic_dev_init(dev);
  189. }
  190. /*
  191. * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
  192. */
  193. /* Wait for SCL and/or SDA to be high */
  194. static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
  195. {
  196. unsigned long x = jiffies + HZ / 28 + 2;
  197. while ((in_8(&iic->directcntl) & mask) != mask){
  198. if (unlikely(time_after(jiffies, x)))
  199. return -1;
  200. cond_resched();
  201. }
  202. return 0;
  203. }
  204. static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
  205. {
  206. volatile struct iic_regs __iomem *iic = dev->vaddr;
  207. const struct i2c_timings* t = &timings[dev->fast_mode ? 1 : 0];
  208. u8 mask, v, sda;
  209. int i, res;
  210. /* Only 7-bit addresses are supported */
  211. if (unlikely(p->flags & I2C_M_TEN)){
  212. DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
  213. dev->idx);
  214. return -EINVAL;
  215. }
  216. DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
  217. /* Reset IIC interface */
  218. out_8(&iic->xtcntlss, XTCNTLSS_SRST);
  219. /* Wait for bus to become free */
  220. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  221. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
  222. goto err;
  223. ndelay(t->buf);
  224. /* START */
  225. out_8(&iic->directcntl, DIRCNTL_SCC);
  226. sda = 0;
  227. ndelay(t->hd_sta);
  228. /* Send address */
  229. v = (u8)((p->addr << 1) | ((p->flags & I2C_M_RD) ? 1 : 0));
  230. for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
  231. out_8(&iic->directcntl, sda);
  232. ndelay(t->low / 2);
  233. sda = (v & mask) ? DIRCNTL_SDAC : 0;
  234. out_8(&iic->directcntl, sda);
  235. ndelay(t->low / 2);
  236. out_8(&iic->directcntl, DIRCNTL_SCC | sda);
  237. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  238. goto err;
  239. ndelay(t->high);
  240. }
  241. /* ACK */
  242. out_8(&iic->directcntl, sda);
  243. ndelay(t->low / 2);
  244. out_8(&iic->directcntl, DIRCNTL_SDAC);
  245. ndelay(t->low / 2);
  246. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  247. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  248. goto err;
  249. res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
  250. ndelay(t->high);
  251. /* STOP */
  252. out_8(&iic->directcntl, 0);
  253. ndelay(t->low);
  254. out_8(&iic->directcntl, DIRCNTL_SCC);
  255. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  256. goto err;
  257. ndelay(t->su_sto);
  258. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  259. ndelay(t->buf);
  260. DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
  261. out:
  262. /* Remove reset */
  263. out_8(&iic->xtcntlss, 0);
  264. /* Reinitialize interface */
  265. iic_dev_init(dev);
  266. return res;
  267. err:
  268. DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
  269. res = -EREMOTEIO;
  270. goto out;
  271. }
  272. /*
  273. * IIC interrupt handler
  274. */
  275. static irqreturn_t iic_handler(int irq, void *dev_id)
  276. {
  277. struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
  278. volatile struct iic_regs __iomem *iic = dev->vaddr;
  279. DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
  280. dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
  281. /* Acknowledge IRQ and wakeup iic_wait_for_tc */
  282. out_8(&iic->sts, STS_IRQA | STS_SCMP);
  283. wake_up_interruptible(&dev->wq);
  284. return IRQ_HANDLED;
  285. }
  286. /*
  287. * Get master transfer result and clear errors if any.
  288. * Returns the number of actually transferred bytes or error (<0)
  289. */
  290. static int iic_xfer_result(struct ibm_iic_private* dev)
  291. {
  292. volatile struct iic_regs __iomem *iic = dev->vaddr;
  293. if (unlikely(in_8(&iic->sts) & STS_ERR)){
  294. DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
  295. in_8(&iic->extsts));
  296. /* Clear errors and possible pending IRQs */
  297. out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
  298. EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
  299. /* Flush master data buffer */
  300. out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
  301. /* Is bus free?
  302. * If error happened during combined xfer
  303. * IIC interface is usually stuck in some strange
  304. * state, the only way out - soft reset.
  305. */
  306. if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  307. DBG("%d: bus is stuck, resetting\n", dev->idx);
  308. iic_dev_reset(dev);
  309. }
  310. return -EREMOTEIO;
  311. }
  312. else
  313. return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
  314. }
  315. /*
  316. * Try to abort active transfer.
  317. */
  318. static void iic_abort_xfer(struct ibm_iic_private* dev)
  319. {
  320. volatile struct iic_regs __iomem *iic = dev->vaddr;
  321. unsigned long x;
  322. DBG("%d: iic_abort_xfer\n", dev->idx);
  323. out_8(&iic->cntl, CNTL_HMT);
  324. /*
  325. * Wait for the abort command to complete.
  326. * It's not worth to be optimized, just poll (timeout >= 1 tick)
  327. */
  328. x = jiffies + 2;
  329. while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  330. if (time_after(jiffies, x)){
  331. DBG("%d: abort timeout, resetting...\n", dev->idx);
  332. iic_dev_reset(dev);
  333. return;
  334. }
  335. schedule();
  336. }
  337. /* Just to clear errors */
  338. iic_xfer_result(dev);
  339. }
  340. /*
  341. * Wait for master transfer to complete.
  342. * It puts current process to sleep until we get interrupt or timeout expires.
  343. * Returns the number of transferred bytes or error (<0)
  344. */
  345. static int iic_wait_for_tc(struct ibm_iic_private* dev){
  346. volatile struct iic_regs __iomem *iic = dev->vaddr;
  347. int ret = 0;
  348. if (dev->irq >= 0){
  349. /* Interrupt mode */
  350. ret = wait_event_interruptible_timeout(dev->wq,
  351. !(in_8(&iic->sts) & STS_PT), dev->adap.timeout);
  352. if (unlikely(ret < 0))
  353. DBG("%d: wait interrupted\n", dev->idx);
  354. else if (unlikely(in_8(&iic->sts) & STS_PT)){
  355. DBG("%d: wait timeout\n", dev->idx);
  356. ret = -ETIMEDOUT;
  357. }
  358. }
  359. else {
  360. /* Polling mode */
  361. unsigned long x = jiffies + dev->adap.timeout;
  362. while (in_8(&iic->sts) & STS_PT){
  363. if (unlikely(time_after(jiffies, x))){
  364. DBG("%d: poll timeout\n", dev->idx);
  365. ret = -ETIMEDOUT;
  366. break;
  367. }
  368. if (unlikely(signal_pending(current))){
  369. DBG("%d: poll interrupted\n", dev->idx);
  370. ret = -ERESTARTSYS;
  371. break;
  372. }
  373. schedule();
  374. }
  375. }
  376. if (unlikely(ret < 0))
  377. iic_abort_xfer(dev);
  378. else
  379. ret = iic_xfer_result(dev);
  380. DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
  381. return ret;
  382. }
  383. /*
  384. * Low level master transfer routine
  385. */
  386. static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
  387. int combined_xfer)
  388. {
  389. volatile struct iic_regs __iomem *iic = dev->vaddr;
  390. char* buf = pm->buf;
  391. int i, j, loops, ret = 0;
  392. int len = pm->len;
  393. u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
  394. if (pm->flags & I2C_M_RD)
  395. cntl |= CNTL_RW;
  396. loops = (len + 3) / 4;
  397. for (i = 0; i < loops; ++i, len -= 4){
  398. int count = len > 4 ? 4 : len;
  399. u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
  400. if (!(cntl & CNTL_RW))
  401. for (j = 0; j < count; ++j)
  402. out_8((void __iomem *)&iic->mdbuf, *buf++);
  403. if (i < loops - 1)
  404. cmd |= CNTL_CHT;
  405. else if (combined_xfer)
  406. cmd |= CNTL_RPST;
  407. DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
  408. /* Start transfer */
  409. out_8(&iic->cntl, cmd);
  410. /* Wait for completion */
  411. ret = iic_wait_for_tc(dev);
  412. if (unlikely(ret < 0))
  413. break;
  414. else if (unlikely(ret != count)){
  415. DBG("%d: xfer_bytes, requested %d, transferred %d\n",
  416. dev->idx, count, ret);
  417. /* If it's not a last part of xfer, abort it */
  418. if (combined_xfer || (i < loops - 1))
  419. iic_abort_xfer(dev);
  420. ret = -EREMOTEIO;
  421. break;
  422. }
  423. if (cntl & CNTL_RW)
  424. for (j = 0; j < count; ++j)
  425. *buf++ = in_8((void __iomem *)&iic->mdbuf);
  426. }
  427. return ret > 0 ? 0 : ret;
  428. }
  429. /*
  430. * Set target slave address for master transfer
  431. */
  432. static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
  433. {
  434. volatile struct iic_regs __iomem *iic = dev->vaddr;
  435. u16 addr = msg->addr;
  436. DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
  437. addr, msg->flags & I2C_M_TEN ? 10 : 7);
  438. if (msg->flags & I2C_M_TEN){
  439. out_8(&iic->cntl, CNTL_AMD);
  440. out_8(&iic->lmadr, addr);
  441. out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
  442. }
  443. else {
  444. out_8(&iic->cntl, 0);
  445. out_8(&iic->lmadr, addr << 1);
  446. }
  447. }
  448. static inline int iic_invalid_address(const struct i2c_msg* p)
  449. {
  450. return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
  451. }
  452. static inline int iic_address_neq(const struct i2c_msg* p1,
  453. const struct i2c_msg* p2)
  454. {
  455. return (p1->addr != p2->addr)
  456. || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
  457. }
  458. /*
  459. * Generic master transfer entrypoint.
  460. * Returns the number of processed messages or error (<0)
  461. */
  462. static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  463. {
  464. struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
  465. volatile struct iic_regs __iomem *iic = dev->vaddr;
  466. int i, ret = 0;
  467. DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
  468. if (!num)
  469. return 0;
  470. /* Check the sanity of the passed messages.
  471. * Uhh, generic i2c layer is more suitable place for such code...
  472. */
  473. if (unlikely(iic_invalid_address(&msgs[0]))){
  474. DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
  475. msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
  476. return -EINVAL;
  477. }
  478. for (i = 0; i < num; ++i){
  479. if (unlikely(msgs[i].len <= 0)){
  480. if (num == 1 && !msgs[0].len){
  481. /* Special case for I2C_SMBUS_QUICK emulation.
  482. * IBM IIC doesn't support 0-length transactions
  483. * so we have to emulate them using bit-banging.
  484. */
  485. return iic_smbus_quick(dev, &msgs[0]);
  486. }
  487. DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
  488. msgs[i].len, i);
  489. return -EINVAL;
  490. }
  491. if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
  492. DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
  493. return -EINVAL;
  494. }
  495. }
  496. /* Check bus state */
  497. if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
  498. DBG("%d: iic_xfer, bus is not free\n", dev->idx);
  499. /* Usually it means something serious has happened.
  500. * We *cannot* have unfinished previous transfer
  501. * so it doesn't make any sense to try to stop it.
  502. * Probably we were not able to recover from the
  503. * previous error.
  504. * The only *reasonable* thing I can think of here
  505. * is soft reset. --ebs
  506. */
  507. iic_dev_reset(dev);
  508. if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  509. DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
  510. return -EREMOTEIO;
  511. }
  512. }
  513. else {
  514. /* Flush master data buffer (just in case) */
  515. out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
  516. }
  517. /* Load slave address */
  518. iic_address(dev, &msgs[0]);
  519. /* Do real transfer */
  520. for (i = 0; i < num && !ret; ++i)
  521. ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
  522. return ret < 0 ? ret : num;
  523. }
  524. static u32 iic_func(struct i2c_adapter *adap)
  525. {
  526. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  527. }
  528. static const struct i2c_algorithm iic_algo = {
  529. .master_xfer = iic_xfer,
  530. .functionality = iic_func
  531. };
  532. /*
  533. * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
  534. */
  535. static inline u8 iic_clckdiv(unsigned int opb)
  536. {
  537. /* Compatibility kludge, should go away after all cards
  538. * are fixed to fill correct value for opbfreq.
  539. * Previous driver version used hardcoded divider value 4,
  540. * it corresponds to OPB frequency from the range (40, 50] MHz
  541. */
  542. if (!opb){
  543. printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
  544. " fix your board specific setup\n");
  545. opb = 50000000;
  546. }
  547. /* Convert to MHz */
  548. opb /= 1000000;
  549. if (opb < 20 || opb > 150){
  550. printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n",
  551. opb);
  552. opb = opb < 20 ? 20 : 150;
  553. }
  554. return (u8)((opb + 9) / 10 - 1);
  555. }
  556. static int iic_request_irq(struct platform_device *ofdev,
  557. struct ibm_iic_private *dev)
  558. {
  559. struct device_node *np = ofdev->dev.of_node;
  560. int irq;
  561. if (iic_force_poll)
  562. return 0;
  563. irq = irq_of_parse_and_map(np, 0);
  564. if (!irq) {
  565. dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n");
  566. return 0;
  567. }
  568. /* Disable interrupts until we finish initialization, assumes
  569. * level-sensitive IRQ setup...
  570. */
  571. iic_interrupt_mode(dev, 0);
  572. if (request_irq(irq, iic_handler, 0, "IBM IIC", dev)) {
  573. dev_err(&ofdev->dev, "request_irq %d failed\n", irq);
  574. /* Fallback to the polling mode */
  575. return 0;
  576. }
  577. return irq;
  578. }
  579. /*
  580. * Register single IIC interface
  581. */
  582. static int iic_probe(struct platform_device *ofdev)
  583. {
  584. struct device_node *np = ofdev->dev.of_node;
  585. struct ibm_iic_private *dev;
  586. struct i2c_adapter *adap;
  587. const u32 *freq;
  588. int ret;
  589. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  590. if (!dev) {
  591. dev_err(&ofdev->dev, "failed to allocate device data\n");
  592. return -ENOMEM;
  593. }
  594. platform_set_drvdata(ofdev, dev);
  595. dev->vaddr = of_iomap(np, 0);
  596. if (dev->vaddr == NULL) {
  597. dev_err(&ofdev->dev, "failed to iomap device\n");
  598. ret = -ENXIO;
  599. goto error_cleanup;
  600. }
  601. init_waitqueue_head(&dev->wq);
  602. dev->irq = iic_request_irq(ofdev, dev);
  603. if (!dev->irq)
  604. dev_warn(&ofdev->dev, "using polling mode\n");
  605. /* Board specific settings */
  606. if (iic_force_fast || of_get_property(np, "fast-mode", NULL))
  607. dev->fast_mode = 1;
  608. freq = of_get_property(np, "clock-frequency", NULL);
  609. if (freq == NULL) {
  610. freq = of_get_property(np->parent, "clock-frequency", NULL);
  611. if (freq == NULL) {
  612. dev_err(&ofdev->dev, "Unable to get bus frequency\n");
  613. ret = -EINVAL;
  614. goto error_cleanup;
  615. }
  616. }
  617. dev->clckdiv = iic_clckdiv(*freq);
  618. dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv);
  619. /* Initialize IIC interface */
  620. iic_dev_init(dev);
  621. /* Register it with i2c layer */
  622. adap = &dev->adap;
  623. adap->dev.parent = &ofdev->dev;
  624. adap->dev.of_node = of_node_get(np);
  625. strlcpy(adap->name, "IBM IIC", sizeof(adap->name));
  626. i2c_set_adapdata(adap, dev);
  627. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  628. adap->algo = &iic_algo;
  629. adap->timeout = HZ;
  630. ret = i2c_add_adapter(adap);
  631. if (ret < 0) {
  632. dev_err(&ofdev->dev, "failed to register i2c adapter\n");
  633. goto error_cleanup;
  634. }
  635. dev_info(&ofdev->dev, "using %s mode\n",
  636. dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
  637. return 0;
  638. error_cleanup:
  639. if (dev->irq) {
  640. iic_interrupt_mode(dev, 0);
  641. free_irq(dev->irq, dev);
  642. }
  643. if (dev->vaddr)
  644. iounmap(dev->vaddr);
  645. kfree(dev);
  646. return ret;
  647. }
  648. /*
  649. * Cleanup initialized IIC interface
  650. */
  651. static int iic_remove(struct platform_device *ofdev)
  652. {
  653. struct ibm_iic_private *dev = platform_get_drvdata(ofdev);
  654. i2c_del_adapter(&dev->adap);
  655. if (dev->irq) {
  656. iic_interrupt_mode(dev, 0);
  657. free_irq(dev->irq, dev);
  658. }
  659. iounmap(dev->vaddr);
  660. kfree(dev);
  661. return 0;
  662. }
  663. static const struct of_device_id ibm_iic_match[] = {
  664. { .compatible = "ibm,iic", },
  665. {}
  666. };
  667. MODULE_DEVICE_TABLE(of, ibm_iic_match);
  668. static struct platform_driver ibm_iic_driver = {
  669. .driver = {
  670. .name = "ibm-iic",
  671. .of_match_table = ibm_iic_match,
  672. },
  673. .probe = iic_probe,
  674. .remove = iic_remove,
  675. };
  676. module_platform_driver(ibm_iic_driver);