i2c-mv64xxx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Driver for the i2c controller on the Marvell line of host bridges
  3. * (e.g, gt642[46]0, mv643[46]0, mv644[46]0, and Orion SoC family).
  4. *
  5. * Author: Mark A. Greer <mgreer@mvista.com>
  6. *
  7. * 2005 (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/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mv643xx_i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/io.h>
  21. /* Register defines */
  22. #define MV64XXX_I2C_REG_SLAVE_ADDR 0x00
  23. #define MV64XXX_I2C_REG_DATA 0x04
  24. #define MV64XXX_I2C_REG_CONTROL 0x08
  25. #define MV64XXX_I2C_REG_STATUS 0x0c
  26. #define MV64XXX_I2C_REG_BAUD 0x0c
  27. #define MV64XXX_I2C_REG_EXT_SLAVE_ADDR 0x10
  28. #define MV64XXX_I2C_REG_SOFT_RESET 0x1c
  29. #define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
  30. #define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
  31. #define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
  32. #define MV64XXX_I2C_REG_CONTROL_START 0x00000020
  33. #define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
  34. #define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
  35. /* Ctlr status values */
  36. #define MV64XXX_I2C_STATUS_BUS_ERR 0x00
  37. #define MV64XXX_I2C_STATUS_MAST_START 0x08
  38. #define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
  39. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
  40. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
  41. #define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
  42. #define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
  43. #define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
  44. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
  45. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
  46. #define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
  47. #define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
  48. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
  49. #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
  50. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
  51. #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
  52. #define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
  53. /* Driver states */
  54. enum {
  55. MV64XXX_I2C_STATE_INVALID,
  56. MV64XXX_I2C_STATE_IDLE,
  57. MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
  58. MV64XXX_I2C_STATE_WAITING_FOR_RESTART,
  59. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
  60. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
  61. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
  62. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
  63. };
  64. /* Driver actions */
  65. enum {
  66. MV64XXX_I2C_ACTION_INVALID,
  67. MV64XXX_I2C_ACTION_CONTINUE,
  68. MV64XXX_I2C_ACTION_SEND_START,
  69. MV64XXX_I2C_ACTION_SEND_RESTART,
  70. MV64XXX_I2C_ACTION_SEND_ADDR_1,
  71. MV64XXX_I2C_ACTION_SEND_ADDR_2,
  72. MV64XXX_I2C_ACTION_SEND_DATA,
  73. MV64XXX_I2C_ACTION_RCV_DATA,
  74. MV64XXX_I2C_ACTION_RCV_DATA_STOP,
  75. MV64XXX_I2C_ACTION_SEND_STOP,
  76. };
  77. struct mv64xxx_i2c_data {
  78. int irq;
  79. u32 state;
  80. u32 action;
  81. u32 aborting;
  82. u32 cntl_bits;
  83. void __iomem *reg_base;
  84. u32 reg_base_p;
  85. u32 reg_size;
  86. u32 addr1;
  87. u32 addr2;
  88. u32 bytes_left;
  89. u32 byte_posn;
  90. u32 send_stop;
  91. u32 block;
  92. int rc;
  93. u32 freq_m;
  94. u32 freq_n;
  95. wait_queue_head_t waitq;
  96. spinlock_t lock;
  97. struct i2c_msg *msg;
  98. struct i2c_adapter adapter;
  99. };
  100. /*
  101. *****************************************************************************
  102. *
  103. * Finite State Machine & Interrupt Routines
  104. *
  105. *****************************************************************************
  106. */
  107. /* Reset hardware and initialize FSM */
  108. static void
  109. mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
  110. {
  111. writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SOFT_RESET);
  112. writel((((drv_data->freq_m & 0xf) << 3) | (drv_data->freq_n & 0x7)),
  113. drv_data->reg_base + MV64XXX_I2C_REG_BAUD);
  114. writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SLAVE_ADDR);
  115. writel(0, drv_data->reg_base + MV64XXX_I2C_REG_EXT_SLAVE_ADDR);
  116. writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
  117. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  118. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  119. }
  120. static void
  121. mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
  122. {
  123. /*
  124. * If state is idle, then this is likely the remnants of an old
  125. * operation that driver has given up on or the user has killed.
  126. * If so, issue the stop condition and go to idle.
  127. */
  128. if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
  129. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  130. return;
  131. }
  132. /* The status from the ctlr [mostly] tells us what to do next */
  133. switch (status) {
  134. /* Start condition interrupt */
  135. case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
  136. case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
  137. drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
  138. drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
  139. break;
  140. /* Performing a write */
  141. case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
  142. if (drv_data->msg->flags & I2C_M_TEN) {
  143. drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
  144. drv_data->state =
  145. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
  146. break;
  147. }
  148. /* FALLTHRU */
  149. case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
  150. case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
  151. if ((drv_data->bytes_left == 0)
  152. || (drv_data->aborting
  153. && (drv_data->byte_posn != 0))) {
  154. if (drv_data->send_stop) {
  155. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  156. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  157. } else {
  158. drv_data->action =
  159. MV64XXX_I2C_ACTION_SEND_RESTART;
  160. drv_data->state =
  161. MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
  162. }
  163. } else {
  164. drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
  165. drv_data->state =
  166. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
  167. drv_data->bytes_left--;
  168. }
  169. break;
  170. /* Performing a read */
  171. case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
  172. if (drv_data->msg->flags & I2C_M_TEN) {
  173. drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
  174. drv_data->state =
  175. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
  176. break;
  177. }
  178. /* FALLTHRU */
  179. case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
  180. if (drv_data->bytes_left == 0) {
  181. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  182. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  183. break;
  184. }
  185. /* FALLTHRU */
  186. case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
  187. if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
  188. drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
  189. else {
  190. drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
  191. drv_data->bytes_left--;
  192. }
  193. drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
  194. if ((drv_data->bytes_left == 1) || drv_data->aborting)
  195. drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
  196. break;
  197. case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
  198. drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
  199. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  200. break;
  201. case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
  202. case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
  203. case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
  204. /* Doesn't seem to be a device at other end */
  205. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  206. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  207. drv_data->rc = -ENODEV;
  208. break;
  209. default:
  210. dev_err(&drv_data->adapter.dev,
  211. "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
  212. "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
  213. drv_data->state, status, drv_data->msg->addr,
  214. drv_data->msg->flags);
  215. drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
  216. mv64xxx_i2c_hw_init(drv_data);
  217. drv_data->rc = -EIO;
  218. }
  219. }
  220. static void
  221. mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
  222. {
  223. switch(drv_data->action) {
  224. case MV64XXX_I2C_ACTION_SEND_RESTART:
  225. drv_data->cntl_bits |= MV64XXX_I2C_REG_CONTROL_START;
  226. drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
  227. writel(drv_data->cntl_bits,
  228. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  229. drv_data->block = 0;
  230. wake_up_interruptible(&drv_data->waitq);
  231. break;
  232. case MV64XXX_I2C_ACTION_CONTINUE:
  233. writel(drv_data->cntl_bits,
  234. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  235. break;
  236. case MV64XXX_I2C_ACTION_SEND_START:
  237. writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
  238. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  239. break;
  240. case MV64XXX_I2C_ACTION_SEND_ADDR_1:
  241. writel(drv_data->addr1,
  242. drv_data->reg_base + MV64XXX_I2C_REG_DATA);
  243. writel(drv_data->cntl_bits,
  244. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  245. break;
  246. case MV64XXX_I2C_ACTION_SEND_ADDR_2:
  247. writel(drv_data->addr2,
  248. drv_data->reg_base + MV64XXX_I2C_REG_DATA);
  249. writel(drv_data->cntl_bits,
  250. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  251. break;
  252. case MV64XXX_I2C_ACTION_SEND_DATA:
  253. writel(drv_data->msg->buf[drv_data->byte_posn++],
  254. drv_data->reg_base + MV64XXX_I2C_REG_DATA);
  255. writel(drv_data->cntl_bits,
  256. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  257. break;
  258. case MV64XXX_I2C_ACTION_RCV_DATA:
  259. drv_data->msg->buf[drv_data->byte_posn++] =
  260. readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
  261. writel(drv_data->cntl_bits,
  262. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  263. break;
  264. case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
  265. drv_data->msg->buf[drv_data->byte_posn++] =
  266. readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
  267. drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
  268. writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
  269. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  270. drv_data->block = 0;
  271. wake_up_interruptible(&drv_data->waitq);
  272. break;
  273. case MV64XXX_I2C_ACTION_INVALID:
  274. default:
  275. dev_err(&drv_data->adapter.dev,
  276. "mv64xxx_i2c_do_action: Invalid action: %d\n",
  277. drv_data->action);
  278. drv_data->rc = -EIO;
  279. /* FALLTHRU */
  280. case MV64XXX_I2C_ACTION_SEND_STOP:
  281. drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
  282. writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
  283. drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
  284. drv_data->block = 0;
  285. wake_up_interruptible(&drv_data->waitq);
  286. break;
  287. }
  288. }
  289. static irqreturn_t
  290. mv64xxx_i2c_intr(int irq, void *dev_id)
  291. {
  292. struct mv64xxx_i2c_data *drv_data = dev_id;
  293. unsigned long flags;
  294. u32 status;
  295. irqreturn_t rc = IRQ_NONE;
  296. spin_lock_irqsave(&drv_data->lock, flags);
  297. while (readl(drv_data->reg_base + MV64XXX_I2C_REG_CONTROL) &
  298. MV64XXX_I2C_REG_CONTROL_IFLG) {
  299. status = readl(drv_data->reg_base + MV64XXX_I2C_REG_STATUS);
  300. mv64xxx_i2c_fsm(drv_data, status);
  301. mv64xxx_i2c_do_action(drv_data);
  302. rc = IRQ_HANDLED;
  303. }
  304. spin_unlock_irqrestore(&drv_data->lock, flags);
  305. return rc;
  306. }
  307. /*
  308. *****************************************************************************
  309. *
  310. * I2C Msg Execution Routines
  311. *
  312. *****************************************************************************
  313. */
  314. static void
  315. mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
  316. struct i2c_msg *msg)
  317. {
  318. u32 dir = 0;
  319. drv_data->msg = msg;
  320. drv_data->byte_posn = 0;
  321. drv_data->bytes_left = msg->len;
  322. drv_data->aborting = 0;
  323. drv_data->rc = 0;
  324. drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
  325. MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
  326. if (msg->flags & I2C_M_RD)
  327. dir = 1;
  328. if (msg->flags & I2C_M_TEN) {
  329. drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
  330. drv_data->addr2 = (u32)msg->addr & 0xff;
  331. } else {
  332. drv_data->addr1 = ((u32)msg->addr & 0x7f) << 1 | dir;
  333. drv_data->addr2 = 0;
  334. }
  335. }
  336. static void
  337. mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
  338. {
  339. long time_left;
  340. unsigned long flags;
  341. char abort = 0;
  342. time_left = wait_event_interruptible_timeout(drv_data->waitq,
  343. !drv_data->block, drv_data->adapter.timeout);
  344. spin_lock_irqsave(&drv_data->lock, flags);
  345. if (!time_left) { /* Timed out */
  346. drv_data->rc = -ETIMEDOUT;
  347. abort = 1;
  348. } else if (time_left < 0) { /* Interrupted/Error */
  349. drv_data->rc = time_left; /* errno value */
  350. abort = 1;
  351. }
  352. if (abort && drv_data->block) {
  353. drv_data->aborting = 1;
  354. spin_unlock_irqrestore(&drv_data->lock, flags);
  355. time_left = wait_event_timeout(drv_data->waitq,
  356. !drv_data->block, drv_data->adapter.timeout);
  357. if ((time_left <= 0) && drv_data->block) {
  358. drv_data->state = MV64XXX_I2C_STATE_IDLE;
  359. dev_err(&drv_data->adapter.dev,
  360. "mv64xxx: I2C bus locked, block: %d, "
  361. "time_left: %d\n", drv_data->block,
  362. (int)time_left);
  363. mv64xxx_i2c_hw_init(drv_data);
  364. }
  365. } else
  366. spin_unlock_irqrestore(&drv_data->lock, flags);
  367. }
  368. static int
  369. mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
  370. int is_first, int is_last)
  371. {
  372. unsigned long flags;
  373. spin_lock_irqsave(&drv_data->lock, flags);
  374. mv64xxx_i2c_prepare_for_io(drv_data, msg);
  375. if (unlikely(msg->flags & I2C_M_NOSTART)) { /* Skip start/addr phases */
  376. if (drv_data->msg->flags & I2C_M_RD) {
  377. /* No action to do, wait for slave to send a byte */
  378. drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
  379. drv_data->state =
  380. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
  381. } else {
  382. drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
  383. drv_data->state =
  384. MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
  385. drv_data->bytes_left--;
  386. }
  387. } else {
  388. if (is_first) {
  389. drv_data->action = MV64XXX_I2C_ACTION_SEND_START;
  390. drv_data->state =
  391. MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
  392. } else {
  393. drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
  394. drv_data->state =
  395. MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
  396. }
  397. }
  398. drv_data->send_stop = is_last;
  399. drv_data->block = 1;
  400. mv64xxx_i2c_do_action(drv_data);
  401. spin_unlock_irqrestore(&drv_data->lock, flags);
  402. mv64xxx_i2c_wait_for_completion(drv_data);
  403. return drv_data->rc;
  404. }
  405. /*
  406. *****************************************************************************
  407. *
  408. * I2C Core Support Routines (Interface to higher level I2C code)
  409. *
  410. *****************************************************************************
  411. */
  412. static u32
  413. mv64xxx_i2c_functionality(struct i2c_adapter *adap)
  414. {
  415. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
  416. }
  417. static int
  418. mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  419. {
  420. struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
  421. int i, rc;
  422. for (i = 0; i < num; i++) {
  423. rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i],
  424. i == 0, i + 1 == num);
  425. if (rc < 0)
  426. return rc;
  427. }
  428. return num;
  429. }
  430. static const struct i2c_algorithm mv64xxx_i2c_algo = {
  431. .master_xfer = mv64xxx_i2c_xfer,
  432. .functionality = mv64xxx_i2c_functionality,
  433. };
  434. /*
  435. *****************************************************************************
  436. *
  437. * Driver Interface & Early Init Routines
  438. *
  439. *****************************************************************************
  440. */
  441. static int __devinit
  442. mv64xxx_i2c_map_regs(struct platform_device *pd,
  443. struct mv64xxx_i2c_data *drv_data)
  444. {
  445. int size;
  446. struct resource *r = platform_get_resource(pd, IORESOURCE_MEM, 0);
  447. if (!r)
  448. return -ENODEV;
  449. size = resource_size(r);
  450. if (!request_mem_region(r->start, size, drv_data->adapter.name))
  451. return -EBUSY;
  452. drv_data->reg_base = ioremap(r->start, size);
  453. drv_data->reg_base_p = r->start;
  454. drv_data->reg_size = size;
  455. return 0;
  456. }
  457. static void
  458. mv64xxx_i2c_unmap_regs(struct mv64xxx_i2c_data *drv_data)
  459. {
  460. if (drv_data->reg_base) {
  461. iounmap(drv_data->reg_base);
  462. release_mem_region(drv_data->reg_base_p, drv_data->reg_size);
  463. }
  464. drv_data->reg_base = NULL;
  465. drv_data->reg_base_p = 0;
  466. }
  467. static int __devinit
  468. mv64xxx_i2c_probe(struct platform_device *pd)
  469. {
  470. struct mv64xxx_i2c_data *drv_data;
  471. struct mv64xxx_i2c_pdata *pdata = pd->dev.platform_data;
  472. int rc;
  473. if ((pd->id != 0) || !pdata)
  474. return -ENODEV;
  475. drv_data = kzalloc(sizeof(struct mv64xxx_i2c_data), GFP_KERNEL);
  476. if (!drv_data)
  477. return -ENOMEM;
  478. if (mv64xxx_i2c_map_regs(pd, drv_data)) {
  479. rc = -ENODEV;
  480. goto exit_kfree;
  481. }
  482. strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
  483. sizeof(drv_data->adapter.name));
  484. init_waitqueue_head(&drv_data->waitq);
  485. spin_lock_init(&drv_data->lock);
  486. drv_data->freq_m = pdata->freq_m;
  487. drv_data->freq_n = pdata->freq_n;
  488. drv_data->irq = platform_get_irq(pd, 0);
  489. if (drv_data->irq < 0) {
  490. rc = -ENXIO;
  491. goto exit_unmap_regs;
  492. }
  493. drv_data->adapter.dev.parent = &pd->dev;
  494. drv_data->adapter.algo = &mv64xxx_i2c_algo;
  495. drv_data->adapter.owner = THIS_MODULE;
  496. drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  497. drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
  498. drv_data->adapter.nr = pd->id;
  499. platform_set_drvdata(pd, drv_data);
  500. i2c_set_adapdata(&drv_data->adapter, drv_data);
  501. mv64xxx_i2c_hw_init(drv_data);
  502. if (request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
  503. MV64XXX_I2C_CTLR_NAME, drv_data)) {
  504. dev_err(&drv_data->adapter.dev,
  505. "mv64xxx: Can't register intr handler irq: %d\n",
  506. drv_data->irq);
  507. rc = -EINVAL;
  508. goto exit_unmap_regs;
  509. } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
  510. dev_err(&drv_data->adapter.dev,
  511. "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
  512. goto exit_free_irq;
  513. }
  514. return 0;
  515. exit_free_irq:
  516. free_irq(drv_data->irq, drv_data);
  517. exit_unmap_regs:
  518. mv64xxx_i2c_unmap_regs(drv_data);
  519. exit_kfree:
  520. kfree(drv_data);
  521. return rc;
  522. }
  523. static int __devexit
  524. mv64xxx_i2c_remove(struct platform_device *dev)
  525. {
  526. struct mv64xxx_i2c_data *drv_data = platform_get_drvdata(dev);
  527. int rc;
  528. rc = i2c_del_adapter(&drv_data->adapter);
  529. free_irq(drv_data->irq, drv_data);
  530. mv64xxx_i2c_unmap_regs(drv_data);
  531. kfree(drv_data);
  532. return rc;
  533. }
  534. static struct platform_driver mv64xxx_i2c_driver = {
  535. .probe = mv64xxx_i2c_probe,
  536. .remove = __devexit_p(mv64xxx_i2c_remove),
  537. .driver = {
  538. .owner = THIS_MODULE,
  539. .name = MV64XXX_I2C_CTLR_NAME,
  540. },
  541. };
  542. module_platform_driver(mv64xxx_i2c_driver);
  543. MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
  544. MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
  545. MODULE_LICENSE("GPL");