i2c-pnx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * Provides I2C support for Philips PNX010x/PNX4008 boards.
  3. *
  4. * Authors: Dennis Kovalev <dkovalev@ru.mvista.com>
  5. * Vitaly Wool <vwool@ru.mvista.com>
  6. *
  7. * 2004-2006 (c) MontaVista Software, Inc. This file is licensed under
  8. * the terms of the GNU General Public License version 2. This program
  9. * is licensed "as is" without any warranty of any kind, whether express
  10. * or implied.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/ioport.h>
  15. #include <linux/delay.h>
  16. #include <linux/i2c.h>
  17. #include <linux/timer.h>
  18. #include <linux/completion.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/i2c-pnx.h>
  21. #include <linux/io.h>
  22. #include <linux/err.h>
  23. #include <linux/clk.h>
  24. #include <linux/slab.h>
  25. #include <mach/hardware.h>
  26. #include <mach/i2c.h>
  27. #define I2C_PNX_TIMEOUT 10 /* msec */
  28. #define I2C_PNX_SPEED_KHZ 100
  29. #define I2C_PNX_REGION_SIZE 0x100
  30. static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data)
  31. {
  32. while (timeout > 0 &&
  33. (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
  34. mdelay(1);
  35. timeout--;
  36. }
  37. return (timeout <= 0);
  38. }
  39. static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data)
  40. {
  41. while (timeout > 0 &&
  42. (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
  43. mdelay(1);
  44. timeout--;
  45. }
  46. return (timeout <= 0);
  47. }
  48. static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
  49. {
  50. struct timer_list *timer = &alg_data->mif.timer;
  51. unsigned long expires = msecs_to_jiffies(I2C_PNX_TIMEOUT);
  52. if (expires <= 1)
  53. expires = 2;
  54. del_timer_sync(timer);
  55. dev_dbg(&alg_data->adapter.dev, "Timer armed at %lu plus %lu jiffies.\n",
  56. jiffies, expires);
  57. timer->expires = jiffies + expires;
  58. timer->data = (unsigned long)alg_data;
  59. add_timer(timer);
  60. }
  61. /**
  62. * i2c_pnx_start - start a device
  63. * @slave_addr: slave address
  64. * @adap: pointer to adapter structure
  65. *
  66. * Generate a START signal in the desired mode.
  67. */
  68. static int i2c_pnx_start(unsigned char slave_addr,
  69. struct i2c_pnx_algo_data *alg_data)
  70. {
  71. dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
  72. slave_addr, alg_data->mif.mode);
  73. /* Check for 7 bit slave addresses only */
  74. if (slave_addr & ~0x7f) {
  75. dev_err(&alg_data->adapter.dev,
  76. "%s: Invalid slave address %x. Only 7-bit addresses are supported\n",
  77. alg_data->adapter.name, slave_addr);
  78. return -EINVAL;
  79. }
  80. /* First, make sure bus is idle */
  81. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
  82. /* Somebody else is monopolizing the bus */
  83. dev_err(&alg_data->adapter.dev,
  84. "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
  85. alg_data->adapter.name, slave_addr,
  86. ioread32(I2C_REG_CTL(alg_data)),
  87. ioread32(I2C_REG_STS(alg_data)));
  88. return -EBUSY;
  89. } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
  90. /* Sorry, we lost the bus */
  91. dev_err(&alg_data->adapter.dev,
  92. "%s: Arbitration failure. Slave addr = %02x\n",
  93. alg_data->adapter.name, slave_addr);
  94. return -EIO;
  95. }
  96. /*
  97. * OK, I2C is enabled and we have the bus.
  98. * Clear the current TDI and AFI status flags.
  99. */
  100. iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi,
  101. I2C_REG_STS(alg_data));
  102. dev_dbg(&alg_data->adapter.dev, "%s(): sending %#x\n", __func__,
  103. (slave_addr << 1) | start_bit | alg_data->mif.mode);
  104. /* Write the slave address, START bit and R/W bit */
  105. iowrite32((slave_addr << 1) | start_bit | alg_data->mif.mode,
  106. I2C_REG_TX(alg_data));
  107. dev_dbg(&alg_data->adapter.dev, "%s(): exit\n", __func__);
  108. return 0;
  109. }
  110. /**
  111. * i2c_pnx_stop - stop a device
  112. * @adap: pointer to I2C adapter structure
  113. *
  114. * Generate a STOP signal to terminate the master transaction.
  115. */
  116. static void i2c_pnx_stop(struct i2c_pnx_algo_data *alg_data)
  117. {
  118. /* Only 1 msec max timeout due to interrupt context */
  119. long timeout = 1000;
  120. dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
  121. __func__, ioread32(I2C_REG_STS(alg_data)));
  122. /* Write a STOP bit to TX FIFO */
  123. iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data));
  124. /* Wait until the STOP is seen. */
  125. while (timeout > 0 &&
  126. (ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) {
  127. /* may be called from interrupt context */
  128. udelay(1);
  129. timeout--;
  130. }
  131. dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
  132. __func__, ioread32(I2C_REG_STS(alg_data)));
  133. }
  134. /**
  135. * i2c_pnx_master_xmit - transmit data to slave
  136. * @adap: pointer to I2C adapter structure
  137. *
  138. * Sends one byte of data to the slave
  139. */
  140. static int i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
  141. {
  142. u32 val;
  143. dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
  144. __func__, ioread32(I2C_REG_STS(alg_data)));
  145. if (alg_data->mif.len > 0) {
  146. /* We still have something to talk about... */
  147. val = *alg_data->mif.buf++;
  148. if (alg_data->mif.len == 1)
  149. val |= stop_bit;
  150. alg_data->mif.len--;
  151. iowrite32(val, I2C_REG_TX(alg_data));
  152. dev_dbg(&alg_data->adapter.dev, "%s(): xmit %#x [%d]\n",
  153. __func__, val, alg_data->mif.len + 1);
  154. if (alg_data->mif.len == 0) {
  155. if (alg_data->last) {
  156. /* Wait until the STOP is seen. */
  157. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
  158. dev_err(&alg_data->adapter.dev,
  159. "The bus is still active after timeout\n");
  160. }
  161. /* Disable master interrupts */
  162. iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
  163. ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
  164. I2C_REG_CTL(alg_data));
  165. del_timer_sync(&alg_data->mif.timer);
  166. dev_dbg(&alg_data->adapter.dev,
  167. "%s(): Waking up xfer routine.\n",
  168. __func__);
  169. complete(&alg_data->mif.complete);
  170. }
  171. } else if (alg_data->mif.len == 0) {
  172. /* zero-sized transfer */
  173. i2c_pnx_stop(alg_data);
  174. /* Disable master interrupts. */
  175. iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
  176. ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
  177. I2C_REG_CTL(alg_data));
  178. /* Stop timer. */
  179. del_timer_sync(&alg_data->mif.timer);
  180. dev_dbg(&alg_data->adapter.dev,
  181. "%s(): Waking up xfer routine after zero-xfer.\n",
  182. __func__);
  183. complete(&alg_data->mif.complete);
  184. }
  185. dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
  186. __func__, ioread32(I2C_REG_STS(alg_data)));
  187. return 0;
  188. }
  189. /**
  190. * i2c_pnx_master_rcv - receive data from slave
  191. * @adap: pointer to I2C adapter structure
  192. *
  193. * Reads one byte data from the slave
  194. */
  195. static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
  196. {
  197. unsigned int val = 0;
  198. u32 ctl = 0;
  199. dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
  200. __func__, ioread32(I2C_REG_STS(alg_data)));
  201. /* Check, whether there is already data,
  202. * or we didn't 'ask' for it yet.
  203. */
  204. if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) {
  205. dev_dbg(&alg_data->adapter.dev,
  206. "%s(): Write dummy data to fill Rx-fifo...\n",
  207. __func__);
  208. if (alg_data->mif.len == 1) {
  209. /* Last byte, do not acknowledge next rcv. */
  210. val |= stop_bit;
  211. /*
  212. * Enable interrupt RFDAIE (data in Rx fifo),
  213. * and disable DRMIE (need data for Tx)
  214. */
  215. ctl = ioread32(I2C_REG_CTL(alg_data));
  216. ctl |= mcntrl_rffie | mcntrl_daie;
  217. ctl &= ~mcntrl_drmie;
  218. iowrite32(ctl, I2C_REG_CTL(alg_data));
  219. }
  220. /*
  221. * Now we'll 'ask' for data:
  222. * For each byte we want to receive, we must
  223. * write a (dummy) byte to the Tx-FIFO.
  224. */
  225. iowrite32(val, I2C_REG_TX(alg_data));
  226. return 0;
  227. }
  228. /* Handle data. */
  229. if (alg_data->mif.len > 0) {
  230. val = ioread32(I2C_REG_RX(alg_data));
  231. *alg_data->mif.buf++ = (u8) (val & 0xff);
  232. dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n",
  233. __func__, val, alg_data->mif.len);
  234. alg_data->mif.len--;
  235. if (alg_data->mif.len == 0) {
  236. if (alg_data->last)
  237. /* Wait until the STOP is seen. */
  238. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
  239. dev_err(&alg_data->adapter.dev,
  240. "The bus is still active after timeout\n");
  241. /* Disable master interrupts */
  242. ctl = ioread32(I2C_REG_CTL(alg_data));
  243. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  244. mcntrl_drmie | mcntrl_daie);
  245. iowrite32(ctl, I2C_REG_CTL(alg_data));
  246. /* Kill timer. */
  247. del_timer_sync(&alg_data->mif.timer);
  248. complete(&alg_data->mif.complete);
  249. }
  250. }
  251. dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
  252. __func__, ioread32(I2C_REG_STS(alg_data)));
  253. return 0;
  254. }
  255. static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
  256. {
  257. struct i2c_pnx_algo_data *alg_data = dev_id;
  258. u32 stat, ctl;
  259. dev_dbg(&alg_data->adapter.dev,
  260. "%s(): mstat = %x mctrl = %x, mode = %d\n",
  261. __func__,
  262. ioread32(I2C_REG_STS(alg_data)),
  263. ioread32(I2C_REG_CTL(alg_data)),
  264. alg_data->mif.mode);
  265. stat = ioread32(I2C_REG_STS(alg_data));
  266. /* let's see what kind of event this is */
  267. if (stat & mstatus_afi) {
  268. /* We lost arbitration in the midst of a transfer */
  269. alg_data->mif.ret = -EIO;
  270. /* Disable master interrupts. */
  271. ctl = ioread32(I2C_REG_CTL(alg_data));
  272. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  273. mcntrl_drmie);
  274. iowrite32(ctl, I2C_REG_CTL(alg_data));
  275. /* Stop timer, to prevent timeout. */
  276. del_timer_sync(&alg_data->mif.timer);
  277. complete(&alg_data->mif.complete);
  278. } else if (stat & mstatus_nai) {
  279. /* Slave did not acknowledge, generate a STOP */
  280. dev_dbg(&alg_data->adapter.dev,
  281. "%s(): Slave did not acknowledge, generating a STOP.\n",
  282. __func__);
  283. i2c_pnx_stop(alg_data);
  284. /* Disable master interrupts. */
  285. ctl = ioread32(I2C_REG_CTL(alg_data));
  286. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  287. mcntrl_drmie);
  288. iowrite32(ctl, I2C_REG_CTL(alg_data));
  289. /* Our return value. */
  290. alg_data->mif.ret = -EIO;
  291. /* Stop timer, to prevent timeout. */
  292. del_timer_sync(&alg_data->mif.timer);
  293. complete(&alg_data->mif.complete);
  294. } else {
  295. /*
  296. * Two options:
  297. * - Master Tx needs data.
  298. * - There is data in the Rx-fifo
  299. * The latter is only the case if we have requested for data,
  300. * via a dummy write. (See 'i2c_pnx_master_rcv'.)
  301. * We therefore check, as a sanity check, whether that interrupt
  302. * has been enabled.
  303. */
  304. if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
  305. if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
  306. i2c_pnx_master_xmit(alg_data);
  307. } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
  308. i2c_pnx_master_rcv(alg_data);
  309. }
  310. }
  311. }
  312. /* Clear TDI and AFI bits */
  313. stat = ioread32(I2C_REG_STS(alg_data));
  314. iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
  315. dev_dbg(&alg_data->adapter.dev,
  316. "%s(): exiting, stat = %x ctrl = %x.\n",
  317. __func__, ioread32(I2C_REG_STS(alg_data)),
  318. ioread32(I2C_REG_CTL(alg_data)));
  319. return IRQ_HANDLED;
  320. }
  321. static void i2c_pnx_timeout(unsigned long data)
  322. {
  323. struct i2c_pnx_algo_data *alg_data = (struct i2c_pnx_algo_data *)data;
  324. u32 ctl;
  325. dev_err(&alg_data->adapter.dev,
  326. "Master timed out. stat = %04x, cntrl = %04x. Resetting master...\n",
  327. ioread32(I2C_REG_STS(alg_data)),
  328. ioread32(I2C_REG_CTL(alg_data)));
  329. /* Reset master and disable interrupts */
  330. ctl = ioread32(I2C_REG_CTL(alg_data));
  331. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
  332. iowrite32(ctl, I2C_REG_CTL(alg_data));
  333. ctl |= mcntrl_reset;
  334. iowrite32(ctl, I2C_REG_CTL(alg_data));
  335. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  336. alg_data->mif.ret = -EIO;
  337. complete(&alg_data->mif.complete);
  338. }
  339. static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
  340. {
  341. u32 stat;
  342. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
  343. dev_err(&alg_data->adapter.dev,
  344. "%s: Bus is still active after xfer. Reset it...\n",
  345. alg_data->adapter.name);
  346. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  347. I2C_REG_CTL(alg_data));
  348. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  349. } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
  350. /* If there is data in the fifo's after transfer,
  351. * flush fifo's by reset.
  352. */
  353. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  354. I2C_REG_CTL(alg_data));
  355. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  356. } else if (stat & mstatus_nai) {
  357. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  358. I2C_REG_CTL(alg_data));
  359. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  360. }
  361. }
  362. /**
  363. * i2c_pnx_xfer - generic transfer entry point
  364. * @adap: pointer to I2C adapter structure
  365. * @msgs: array of messages
  366. * @num: number of messages
  367. *
  368. * Initiates the transfer
  369. */
  370. static int
  371. i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  372. {
  373. struct i2c_msg *pmsg;
  374. int rc = 0, completed = 0, i;
  375. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  376. u32 stat = ioread32(I2C_REG_STS(alg_data));
  377. dev_dbg(&alg_data->adapter.dev,
  378. "%s(): entering: %d messages, stat = %04x.\n",
  379. __func__, num, ioread32(I2C_REG_STS(alg_data)));
  380. bus_reset_if_active(alg_data);
  381. /* Process transactions in a loop. */
  382. for (i = 0; rc >= 0 && i < num; i++) {
  383. u8 addr;
  384. pmsg = &msgs[i];
  385. addr = pmsg->addr;
  386. if (pmsg->flags & I2C_M_TEN) {
  387. dev_err(&alg_data->adapter.dev,
  388. "%s: 10 bits addr not supported!\n",
  389. alg_data->adapter.name);
  390. rc = -EINVAL;
  391. break;
  392. }
  393. alg_data->mif.buf = pmsg->buf;
  394. alg_data->mif.len = pmsg->len;
  395. alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
  396. I2C_SMBUS_READ : I2C_SMBUS_WRITE;
  397. alg_data->mif.ret = 0;
  398. alg_data->last = (i == num - 1);
  399. dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n",
  400. __func__, alg_data->mif.mode, alg_data->mif.len);
  401. i2c_pnx_arm_timer(alg_data);
  402. /* initialize the completion var */
  403. init_completion(&alg_data->mif.complete);
  404. /* Enable master interrupt */
  405. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
  406. mcntrl_naie | mcntrl_drmie,
  407. I2C_REG_CTL(alg_data));
  408. /* Put start-code and slave-address on the bus. */
  409. rc = i2c_pnx_start(addr, alg_data);
  410. if (rc < 0)
  411. break;
  412. /* Wait for completion */
  413. wait_for_completion(&alg_data->mif.complete);
  414. if (!(rc = alg_data->mif.ret))
  415. completed++;
  416. dev_dbg(&alg_data->adapter.dev,
  417. "%s(): Complete, return code = %d.\n",
  418. __func__, rc);
  419. /* Clear TDI and AFI bits in case they are set. */
  420. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
  421. dev_dbg(&alg_data->adapter.dev,
  422. "%s: TDI still set... clearing now.\n",
  423. alg_data->adapter.name);
  424. iowrite32(stat, I2C_REG_STS(alg_data));
  425. }
  426. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
  427. dev_dbg(&alg_data->adapter.dev,
  428. "%s: AFI still set... clearing now.\n",
  429. alg_data->adapter.name);
  430. iowrite32(stat, I2C_REG_STS(alg_data));
  431. }
  432. }
  433. bus_reset_if_active(alg_data);
  434. /* Cleanup to be sure... */
  435. alg_data->mif.buf = NULL;
  436. alg_data->mif.len = 0;
  437. dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
  438. __func__, ioread32(I2C_REG_STS(alg_data)));
  439. if (completed != num)
  440. return ((rc < 0) ? rc : -EREMOTEIO);
  441. return num;
  442. }
  443. static u32 i2c_pnx_func(struct i2c_adapter *adapter)
  444. {
  445. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  446. }
  447. static struct i2c_algorithm pnx_algorithm = {
  448. .master_xfer = i2c_pnx_xfer,
  449. .functionality = i2c_pnx_func,
  450. };
  451. #ifdef CONFIG_PM
  452. static int i2c_pnx_controller_suspend(struct platform_device *pdev,
  453. pm_message_t state)
  454. {
  455. struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
  456. clk_disable(alg_data->clk);
  457. return 0;
  458. }
  459. static int i2c_pnx_controller_resume(struct platform_device *pdev)
  460. {
  461. struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
  462. return clk_enable(alg_data->clk);
  463. }
  464. #else
  465. #define i2c_pnx_controller_suspend NULL
  466. #define i2c_pnx_controller_resume NULL
  467. #endif
  468. static int __devinit i2c_pnx_probe(struct platform_device *pdev)
  469. {
  470. unsigned long tmp;
  471. int ret = 0;
  472. struct i2c_pnx_algo_data *alg_data;
  473. unsigned long freq;
  474. struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data;
  475. if (!i2c_pnx || !i2c_pnx->name) {
  476. dev_err(&pdev->dev, "%s: no platform data supplied\n",
  477. __func__);
  478. ret = -EINVAL;
  479. goto out;
  480. }
  481. alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
  482. if (!alg_data) {
  483. ret = -ENOMEM;
  484. goto err_kzalloc;
  485. }
  486. platform_set_drvdata(pdev, alg_data);
  487. strlcpy(alg_data->adapter.name, i2c_pnx->name,
  488. sizeof(alg_data->adapter.name));
  489. alg_data->adapter.dev.parent = &pdev->dev;
  490. alg_data->adapter.algo = &pnx_algorithm;
  491. alg_data->adapter.algo_data = alg_data;
  492. alg_data->adapter.nr = pdev->id;
  493. alg_data->i2c_pnx = i2c_pnx;
  494. alg_data->clk = clk_get(&pdev->dev, NULL);
  495. if (IS_ERR(alg_data->clk)) {
  496. ret = PTR_ERR(alg_data->clk);
  497. goto out_drvdata;
  498. }
  499. init_timer(&alg_data->mif.timer);
  500. alg_data->mif.timer.function = i2c_pnx_timeout;
  501. alg_data->mif.timer.data = (unsigned long)alg_data;
  502. /* Register I/O resource */
  503. if (!request_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE,
  504. pdev->name)) {
  505. dev_err(&pdev->dev,
  506. "I/O region 0x%08x for I2C already in use.\n",
  507. i2c_pnx->base);
  508. ret = -ENODEV;
  509. goto out_clkget;
  510. }
  511. alg_data->ioaddr = ioremap(i2c_pnx->base, I2C_PNX_REGION_SIZE);
  512. if (!alg_data->ioaddr) {
  513. dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
  514. ret = -ENOMEM;
  515. goto out_release;
  516. }
  517. ret = clk_enable(alg_data->clk);
  518. if (ret)
  519. goto out_unmap;
  520. freq = clk_get_rate(alg_data->clk);
  521. /*
  522. * Clock Divisor High This value is the number of system clocks
  523. * the serial clock (SCL) will be high.
  524. * For example, if the system clock period is 50 ns and the maximum
  525. * desired serial period is 10000 ns (100 kHz), then CLKHI would be
  526. * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
  527. * programmed into CLKHI will vary from this slightly due to
  528. * variations in the output pad's rise and fall times as well as
  529. * the deglitching filter length.
  530. */
  531. tmp = ((freq / 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2;
  532. if (tmp > 0x3FF)
  533. tmp = 0x3FF;
  534. iowrite32(tmp, I2C_REG_CKH(alg_data));
  535. iowrite32(tmp, I2C_REG_CKL(alg_data));
  536. iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
  537. if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) {
  538. ret = -ENODEV;
  539. goto out_clock;
  540. }
  541. init_completion(&alg_data->mif.complete);
  542. ret = request_irq(i2c_pnx->irq, i2c_pnx_interrupt,
  543. 0, pdev->name, alg_data);
  544. if (ret)
  545. goto out_clock;
  546. /* Register this adapter with the I2C subsystem */
  547. ret = i2c_add_numbered_adapter(&alg_data->adapter);
  548. if (ret < 0) {
  549. dev_err(&pdev->dev, "I2C: Failed to add bus\n");
  550. goto out_irq;
  551. }
  552. dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
  553. alg_data->adapter.name, i2c_pnx->base, i2c_pnx->irq);
  554. return 0;
  555. out_irq:
  556. free_irq(i2c_pnx->irq, alg_data);
  557. out_clock:
  558. clk_disable(alg_data->clk);
  559. out_unmap:
  560. iounmap(alg_data->ioaddr);
  561. out_release:
  562. release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
  563. out_clkget:
  564. clk_put(alg_data->clk);
  565. out_drvdata:
  566. kfree(alg_data);
  567. err_kzalloc:
  568. platform_set_drvdata(pdev, NULL);
  569. out:
  570. return ret;
  571. }
  572. static int __devexit i2c_pnx_remove(struct platform_device *pdev)
  573. {
  574. struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
  575. struct i2c_pnx_data *i2c_pnx = alg_data->i2c_pnx;
  576. free_irq(i2c_pnx->irq, alg_data);
  577. i2c_del_adapter(&alg_data->adapter);
  578. clk_disable(alg_data->clk);
  579. iounmap(alg_data->ioaddr);
  580. release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
  581. clk_put(alg_data->clk);
  582. kfree(alg_data);
  583. platform_set_drvdata(pdev, NULL);
  584. return 0;
  585. }
  586. static struct platform_driver i2c_pnx_driver = {
  587. .driver = {
  588. .name = "pnx-i2c",
  589. .owner = THIS_MODULE,
  590. },
  591. .probe = i2c_pnx_probe,
  592. .remove = __devexit_p(i2c_pnx_remove),
  593. .suspend = i2c_pnx_controller_suspend,
  594. .resume = i2c_pnx_controller_resume,
  595. };
  596. static int __init i2c_adap_pnx_init(void)
  597. {
  598. return platform_driver_register(&i2c_pnx_driver);
  599. }
  600. static void __exit i2c_adap_pnx_exit(void)
  601. {
  602. platform_driver_unregister(&i2c_pnx_driver);
  603. }
  604. MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
  605. MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
  606. MODULE_LICENSE("GPL");
  607. MODULE_ALIAS("platform:pnx-i2c");
  608. /* We need to make sure I2C is initialized before USB */
  609. subsys_initcall(i2c_adap_pnx_init);
  610. module_exit(i2c_adap_pnx_exit);