i2c-pnx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 <mach/hardware.h>
  25. #include <mach/i2c.h>
  26. #include <asm/irq.h>
  27. #include <asm/uaccess.h>
  28. #define I2C_PNX_TIMEOUT 10 /* msec */
  29. #define I2C_PNX_SPEED_KHZ 100
  30. #define I2C_PNX_REGION_SIZE 0x100
  31. static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data)
  32. {
  33. while (timeout > 0 &&
  34. (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
  35. mdelay(1);
  36. timeout--;
  37. }
  38. return (timeout <= 0);
  39. }
  40. static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data)
  41. {
  42. while (timeout > 0 &&
  43. (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
  44. mdelay(1);
  45. timeout--;
  46. }
  47. return (timeout <= 0);
  48. }
  49. static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
  50. {
  51. struct timer_list *timer = &alg_data->mif.timer;
  52. int expires = I2C_PNX_TIMEOUT / (1000 / HZ);
  53. if (expires <= 1)
  54. expires = 2;
  55. del_timer_sync(timer);
  56. dev_dbg(&alg_data->adapter.dev, "Timer armed at %lu plus %u jiffies.\n",
  57. jiffies, expires);
  58. timer->expires = jiffies + expires;
  59. timer->data = (unsigned long)&alg_data;
  60. add_timer(timer);
  61. }
  62. /**
  63. * i2c_pnx_start - start a device
  64. * @slave_addr: slave address
  65. * @adap: pointer to adapter structure
  66. *
  67. * Generate a START signal in the desired mode.
  68. */
  69. static int i2c_pnx_start(unsigned char slave_addr,
  70. struct i2c_pnx_algo_data *alg_data)
  71. {
  72. dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
  73. slave_addr, alg_data->mif.mode);
  74. /* Check for 7 bit slave addresses only */
  75. if (slave_addr & ~0x7f) {
  76. dev_err(&alg_data->adapter.dev, "%s: Invalid slave address %x. "
  77. "Only 7-bit addresses are supported\n",
  78. alg_data->adapter.name, slave_addr);
  79. return -EINVAL;
  80. }
  81. /* First, make sure bus is idle */
  82. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
  83. /* Somebody else is monopolizing the bus */
  84. dev_err(&alg_data->adapter.dev, "%s: Bus busy. Slave addr = %02x, "
  85. "cntrl = %x, stat = %x\n",
  86. alg_data->adapter.name, slave_addr,
  87. ioread32(I2C_REG_CTL(alg_data)),
  88. ioread32(I2C_REG_STS(alg_data)));
  89. return -EBUSY;
  90. } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
  91. /* Sorry, we lost the bus */
  92. dev_err(&alg_data->adapter.dev, "%s: Arbitration failure. "
  93. "Slave addr = %02x\n", 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. if (!alg_data->last)
  151. val |= start_bit;
  152. }
  153. alg_data->mif.len--;
  154. iowrite32(val, I2C_REG_TX(alg_data));
  155. dev_dbg(&alg_data->adapter.dev, "%s(): xmit %#x [%d]\n", __func__,
  156. val, alg_data->mif.len + 1);
  157. if (alg_data->mif.len == 0) {
  158. if (alg_data->last) {
  159. /* Wait until the STOP is seen. */
  160. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
  161. dev_err(&alg_data->adapter.dev, "The bus is still "
  162. "active after timeout\n");
  163. }
  164. /* Disable master interrupts */
  165. iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
  166. ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
  167. I2C_REG_CTL(alg_data));
  168. del_timer_sync(&alg_data->mif.timer);
  169. dev_dbg(&alg_data->adapter.dev, "%s(): Waking up xfer routine.\n",
  170. __func__);
  171. complete(&alg_data->mif.complete);
  172. }
  173. } else if (alg_data->mif.len == 0) {
  174. /* zero-sized transfer */
  175. i2c_pnx_stop(alg_data);
  176. /* Disable master interrupts. */
  177. iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
  178. ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
  179. I2C_REG_CTL(alg_data));
  180. /* Stop timer. */
  181. del_timer_sync(&alg_data->mif.timer);
  182. dev_dbg(&alg_data->adapter.dev, "%s(): Waking up xfer routine after "
  183. "zero-xfer.\n", __func__);
  184. complete(&alg_data->mif.complete);
  185. }
  186. dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
  187. __func__, ioread32(I2C_REG_STS(alg_data)));
  188. return 0;
  189. }
  190. /**
  191. * i2c_pnx_master_rcv - receive data from slave
  192. * @adap: pointer to I2C adapter structure
  193. *
  194. * Reads one byte data from the slave
  195. */
  196. static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
  197. {
  198. unsigned int val = 0;
  199. u32 ctl = 0;
  200. dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
  201. __func__, ioread32(I2C_REG_STS(alg_data)));
  202. /* Check, whether there is already data,
  203. * or we didn't 'ask' for it yet.
  204. */
  205. if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) {
  206. dev_dbg(&alg_data->adapter.dev, "%s(): Write dummy data to fill "
  207. "Rx-fifo...\n", __func__);
  208. if (alg_data->mif.len == 1) {
  209. /* Last byte, do not acknowledge next rcv. */
  210. val |= stop_bit;
  211. if (!alg_data->last)
  212. val |= start_bit;
  213. /*
  214. * Enable interrupt RFDAIE (data in Rx fifo),
  215. * and disable DRMIE (need data for Tx)
  216. */
  217. ctl = ioread32(I2C_REG_CTL(alg_data));
  218. ctl |= mcntrl_rffie | mcntrl_daie;
  219. ctl &= ~mcntrl_drmie;
  220. iowrite32(ctl, I2C_REG_CTL(alg_data));
  221. }
  222. /*
  223. * Now we'll 'ask' for data:
  224. * For each byte we want to receive, we must
  225. * write a (dummy) byte to the Tx-FIFO.
  226. */
  227. iowrite32(val, I2C_REG_TX(alg_data));
  228. return 0;
  229. }
  230. /* Handle data. */
  231. if (alg_data->mif.len > 0) {
  232. val = ioread32(I2C_REG_RX(alg_data));
  233. *alg_data->mif.buf++ = (u8) (val & 0xff);
  234. dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n", __func__, val,
  235. alg_data->mif.len);
  236. alg_data->mif.len--;
  237. if (alg_data->mif.len == 0) {
  238. if (alg_data->last)
  239. /* Wait until the STOP is seen. */
  240. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
  241. dev_err(&alg_data->adapter.dev, "The bus is still "
  242. "active after timeout\n");
  243. /* Disable master interrupts */
  244. ctl = ioread32(I2C_REG_CTL(alg_data));
  245. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  246. mcntrl_drmie | mcntrl_daie);
  247. iowrite32(ctl, I2C_REG_CTL(alg_data));
  248. /* Kill timer. */
  249. del_timer_sync(&alg_data->mif.timer);
  250. complete(&alg_data->mif.complete);
  251. }
  252. }
  253. dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
  254. __func__, ioread32(I2C_REG_STS(alg_data)));
  255. return 0;
  256. }
  257. static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
  258. {
  259. struct i2c_pnx_algo_data *alg_data = dev_id;
  260. u32 stat, ctl;
  261. dev_dbg(&alg_data->adapter.dev, "%s(): mstat = %x mctrl = %x, mode = %d\n",
  262. __func__,
  263. ioread32(I2C_REG_STS(alg_data)),
  264. ioread32(I2C_REG_CTL(alg_data)),
  265. alg_data->mif.mode);
  266. stat = ioread32(I2C_REG_STS(alg_data));
  267. /* let's see what kind of event this is */
  268. if (stat & mstatus_afi) {
  269. /* We lost arbitration in the midst of a transfer */
  270. alg_data->mif.ret = -EIO;
  271. /* Disable master interrupts. */
  272. ctl = ioread32(I2C_REG_CTL(alg_data));
  273. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  274. mcntrl_drmie);
  275. iowrite32(ctl, I2C_REG_CTL(alg_data));
  276. /* Stop timer, to prevent timeout. */
  277. del_timer_sync(&alg_data->mif.timer);
  278. complete(&alg_data->mif.complete);
  279. } else if (stat & mstatus_nai) {
  280. /* Slave did not acknowledge, generate a STOP */
  281. dev_dbg(&alg_data->adapter.dev, "%s(): "
  282. "Slave did not acknowledge, generating a STOP.\n",
  283. __func__);
  284. i2c_pnx_stop(alg_data);
  285. /* Disable master interrupts. */
  286. ctl = ioread32(I2C_REG_CTL(alg_data));
  287. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  288. mcntrl_drmie);
  289. iowrite32(ctl, I2C_REG_CTL(alg_data));
  290. /* Our return value. */
  291. alg_data->mif.ret = -EIO;
  292. /* Stop timer, to prevent timeout. */
  293. del_timer_sync(&alg_data->mif.timer);
  294. complete(&alg_data->mif.complete);
  295. } else {
  296. /*
  297. * Two options:
  298. * - Master Tx needs data.
  299. * - There is data in the Rx-fifo
  300. * The latter is only the case if we have requested for data,
  301. * via a dummy write. (See 'i2c_pnx_master_rcv'.)
  302. * We therefore check, as a sanity check, whether that interrupt
  303. * has been enabled.
  304. */
  305. if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
  306. if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
  307. i2c_pnx_master_xmit(alg_data);
  308. } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
  309. i2c_pnx_master_rcv(alg_data);
  310. }
  311. }
  312. }
  313. /* Clear TDI and AFI bits */
  314. stat = ioread32(I2C_REG_STS(alg_data));
  315. iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
  316. dev_dbg(&alg_data->adapter.dev, "%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, "Master timed out. stat = %04x, cntrl = %04x. "
  326. "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, "%s(): entering: %d messages, stat = %04x.\n",
  378. __func__, num, ioread32(I2C_REG_STS(alg_data)));
  379. bus_reset_if_active(alg_data);
  380. /* Process transactions in a loop. */
  381. for (i = 0; rc >= 0 && i < num; i++) {
  382. u8 addr;
  383. pmsg = &msgs[i];
  384. addr = pmsg->addr;
  385. if (pmsg->flags & I2C_M_TEN) {
  386. dev_err(&alg_data->adapter.dev,
  387. "%s: 10 bits addr not supported!\n",
  388. alg_data->adapter.name);
  389. rc = -EINVAL;
  390. break;
  391. }
  392. alg_data->mif.buf = pmsg->buf;
  393. alg_data->mif.len = pmsg->len;
  394. alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
  395. I2C_SMBUS_READ : I2C_SMBUS_WRITE;
  396. alg_data->mif.ret = 0;
  397. alg_data->last = (i == num - 1);
  398. dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n", __func__,
  399. alg_data->mif.mode,
  400. 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, "%s(): Complete, return code = %d.\n",
  417. __func__, rc);
  418. /* Clear TDI and AFI bits in case they are set. */
  419. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
  420. dev_dbg(&alg_data->adapter.dev,
  421. "%s: TDI still set... clearing now.\n",
  422. alg_data->adapter.name);
  423. iowrite32(stat, I2C_REG_STS(alg_data));
  424. }
  425. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
  426. dev_dbg(&alg_data->adapter.dev,
  427. "%s: AFI still set... clearing now.\n",
  428. alg_data->adapter.name);
  429. iowrite32(stat, I2C_REG_STS(alg_data));
  430. }
  431. }
  432. bus_reset_if_active(alg_data);
  433. /* Cleanup to be sure... */
  434. alg_data->mif.buf = NULL;
  435. alg_data->mif.len = 0;
  436. dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
  437. __func__, ioread32(I2C_REG_STS(alg_data)));
  438. if (completed != num)
  439. return ((rc < 0) ? rc : -EREMOTEIO);
  440. return num;
  441. }
  442. static u32 i2c_pnx_func(struct i2c_adapter *adapter)
  443. {
  444. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  445. }
  446. static struct i2c_algorithm pnx_algorithm = {
  447. .master_xfer = i2c_pnx_xfer,
  448. .functionality = i2c_pnx_func,
  449. };
  450. #ifdef CONFIG_PM
  451. static int i2c_pnx_controller_suspend(struct platform_device *pdev,
  452. pm_message_t state)
  453. {
  454. struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
  455. /* FIXME: shouldn't this be clk_disable? */
  456. clk_enable(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. iowrite32(tmp, I2C_REG_CKH(alg_data));
  533. iowrite32(tmp, I2C_REG_CKL(alg_data));
  534. iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
  535. if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) {
  536. ret = -ENODEV;
  537. goto out_clock;
  538. }
  539. init_completion(&alg_data->mif.complete);
  540. ret = request_irq(i2c_pnx->irq, i2c_pnx_interrupt,
  541. 0, pdev->name, alg_data);
  542. if (ret)
  543. goto out_clock;
  544. /* Register this adapter with the I2C subsystem */
  545. ret = i2c_add_numbered_adapter(&alg_data->adapter);
  546. if (ret < 0) {
  547. dev_err(&pdev->dev, "I2C: Failed to add bus\n");
  548. goto out_irq;
  549. }
  550. dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
  551. alg_data->adapter.name, i2c_pnx->base, i2c_pnx->irq);
  552. return 0;
  553. out_irq:
  554. free_irq(i2c_pnx->irq, alg_data);
  555. out_clock:
  556. clk_disable(alg_data->clk);
  557. out_unmap:
  558. iounmap(alg_data->ioaddr);
  559. out_release:
  560. release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
  561. out_clkget:
  562. clk_put(alg_data->clk);
  563. out_drvdata:
  564. kfree(alg_data);
  565. err_kzalloc:
  566. platform_set_drvdata(pdev, NULL);
  567. out:
  568. return ret;
  569. }
  570. static int __devexit i2c_pnx_remove(struct platform_device *pdev)
  571. {
  572. struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
  573. struct i2c_pnx_data *i2c_pnx = alg_data->i2c_pnx;
  574. free_irq(i2c_pnx->irq, alg_data);
  575. i2c_del_adapter(&alg_data->adapter);
  576. clk_disable(alg_data->clk);
  577. iounmap(alg_data->ioaddr);
  578. release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
  579. clk_put(alg_data->clk);
  580. kfree(alg_data);
  581. platform_set_drvdata(pdev, NULL);
  582. return 0;
  583. }
  584. static struct platform_driver i2c_pnx_driver = {
  585. .driver = {
  586. .name = "pnx-i2c",
  587. .owner = THIS_MODULE,
  588. },
  589. .probe = i2c_pnx_probe,
  590. .remove = __devexit_p(i2c_pnx_remove),
  591. .suspend = i2c_pnx_controller_suspend,
  592. .resume = i2c_pnx_controller_resume,
  593. };
  594. static int __init i2c_adap_pnx_init(void)
  595. {
  596. return platform_driver_register(&i2c_pnx_driver);
  597. }
  598. static void __exit i2c_adap_pnx_exit(void)
  599. {
  600. platform_driver_unregister(&i2c_pnx_driver);
  601. }
  602. MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
  603. MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
  604. MODULE_LICENSE("GPL");
  605. MODULE_ALIAS("platform:pnx-i2c");
  606. /* We need to make sure I2C is initialized before USB */
  607. subsys_initcall(i2c_adap_pnx_init);
  608. module_exit(i2c_adap_pnx_exit);