i2c-pnx.c 19 KB

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