i2c-davinci.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * TI DAVINCI I2C adapter driver.
  3. *
  4. * Copyright (C) 2006 Texas Instruments.
  5. * Copyright (C) 2007 MontaVista Software Inc.
  6. *
  7. * Updated by Vinod & Sudhakar Feb 2005
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. * ----------------------------------------------------------------------------
  25. *
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/delay.h>
  30. #include <linux/i2c.h>
  31. #include <linux/clk.h>
  32. #include <linux/errno.h>
  33. #include <linux/sched.h>
  34. #include <linux/err.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/io.h>
  38. #include <linux/slab.h>
  39. #include <mach/hardware.h>
  40. #include <mach/i2c.h>
  41. /* ----- global defines ----------------------------------------------- */
  42. #define DAVINCI_I2C_TIMEOUT (1*HZ)
  43. #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
  44. DAVINCI_I2C_IMR_SCD | \
  45. DAVINCI_I2C_IMR_ARDY | \
  46. DAVINCI_I2C_IMR_NACK | \
  47. DAVINCI_I2C_IMR_AL)
  48. #define DAVINCI_I2C_OAR_REG 0x00
  49. #define DAVINCI_I2C_IMR_REG 0x04
  50. #define DAVINCI_I2C_STR_REG 0x08
  51. #define DAVINCI_I2C_CLKL_REG 0x0c
  52. #define DAVINCI_I2C_CLKH_REG 0x10
  53. #define DAVINCI_I2C_CNT_REG 0x14
  54. #define DAVINCI_I2C_DRR_REG 0x18
  55. #define DAVINCI_I2C_SAR_REG 0x1c
  56. #define DAVINCI_I2C_DXR_REG 0x20
  57. #define DAVINCI_I2C_MDR_REG 0x24
  58. #define DAVINCI_I2C_IVR_REG 0x28
  59. #define DAVINCI_I2C_EMDR_REG 0x2c
  60. #define DAVINCI_I2C_PSC_REG 0x30
  61. #define DAVINCI_I2C_IVR_AAS 0x07
  62. #define DAVINCI_I2C_IVR_SCD 0x06
  63. #define DAVINCI_I2C_IVR_XRDY 0x05
  64. #define DAVINCI_I2C_IVR_RDR 0x04
  65. #define DAVINCI_I2C_IVR_ARDY 0x03
  66. #define DAVINCI_I2C_IVR_NACK 0x02
  67. #define DAVINCI_I2C_IVR_AL 0x01
  68. #define DAVINCI_I2C_STR_BB BIT(12)
  69. #define DAVINCI_I2C_STR_RSFULL BIT(11)
  70. #define DAVINCI_I2C_STR_SCD BIT(5)
  71. #define DAVINCI_I2C_STR_ARDY BIT(2)
  72. #define DAVINCI_I2C_STR_NACK BIT(1)
  73. #define DAVINCI_I2C_STR_AL BIT(0)
  74. #define DAVINCI_I2C_MDR_NACK BIT(15)
  75. #define DAVINCI_I2C_MDR_STT BIT(13)
  76. #define DAVINCI_I2C_MDR_STP BIT(11)
  77. #define DAVINCI_I2C_MDR_MST BIT(10)
  78. #define DAVINCI_I2C_MDR_TRX BIT(9)
  79. #define DAVINCI_I2C_MDR_XA BIT(8)
  80. #define DAVINCI_I2C_MDR_RM BIT(7)
  81. #define DAVINCI_I2C_MDR_IRS BIT(5)
  82. #define DAVINCI_I2C_IMR_AAS BIT(6)
  83. #define DAVINCI_I2C_IMR_SCD BIT(5)
  84. #define DAVINCI_I2C_IMR_XRDY BIT(4)
  85. #define DAVINCI_I2C_IMR_RRDY BIT(3)
  86. #define DAVINCI_I2C_IMR_ARDY BIT(2)
  87. #define DAVINCI_I2C_IMR_NACK BIT(1)
  88. #define DAVINCI_I2C_IMR_AL BIT(0)
  89. struct davinci_i2c_dev {
  90. struct device *dev;
  91. void __iomem *base;
  92. struct completion cmd_complete;
  93. struct clk *clk;
  94. int cmd_err;
  95. u8 *buf;
  96. size_t buf_len;
  97. int irq;
  98. int stop;
  99. u8 terminate;
  100. struct i2c_adapter adapter;
  101. };
  102. /* default platform data to use if not supplied in the platform_device */
  103. static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
  104. .bus_freq = 100,
  105. .bus_delay = 0,
  106. };
  107. static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
  108. int reg, u16 val)
  109. {
  110. __raw_writew(val, i2c_dev->base + reg);
  111. }
  112. static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
  113. {
  114. return __raw_readw(i2c_dev->base + reg);
  115. }
  116. static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
  117. int val)
  118. {
  119. u16 w;
  120. w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
  121. if (!val) /* put I2C into reset */
  122. w &= ~DAVINCI_I2C_MDR_IRS;
  123. else /* take I2C out of reset */
  124. w |= DAVINCI_I2C_MDR_IRS;
  125. davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
  126. }
  127. static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
  128. {
  129. struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
  130. u16 psc;
  131. u32 clk;
  132. u32 d;
  133. u32 clkh;
  134. u32 clkl;
  135. u32 input_clock = clk_get_rate(dev->clk);
  136. /* NOTE: I2C Clock divider programming info
  137. * As per I2C specs the following formulas provide prescaler
  138. * and low/high divider values
  139. * input clk --> PSC Div -----------> ICCL/H Div --> output clock
  140. * module clk
  141. *
  142. * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
  143. *
  144. * Thus,
  145. * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
  146. *
  147. * where if PSC == 0, d = 7,
  148. * if PSC == 1, d = 6
  149. * if PSC > 1 , d = 5
  150. */
  151. /* get minimum of 7 MHz clock, but max of 12 MHz */
  152. psc = (input_clock / 7000000) - 1;
  153. if ((input_clock / (psc + 1)) > 12000000)
  154. psc++; /* better to run under spec than over */
  155. d = (psc >= 2) ? 5 : 7 - psc;
  156. clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
  157. clkh = clk >> 1;
  158. clkl = clk - clkh;
  159. davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
  160. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
  161. davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
  162. dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
  163. }
  164. /*
  165. * This function configures I2C and brings I2C out of reset.
  166. * This function is called during I2C init function. This function
  167. * also gets called if I2C encounters any errors.
  168. */
  169. static int i2c_davinci_init(struct davinci_i2c_dev *dev)
  170. {
  171. struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
  172. if (!pdata)
  173. pdata = &davinci_i2c_platform_data_default;
  174. /* put I2C into reset */
  175. davinci_i2c_reset_ctrl(dev, 0);
  176. /* compute clock dividers */
  177. i2c_davinci_calc_clk_dividers(dev);
  178. /* Respond at reserved "SMBus Host" slave address" (and zero);
  179. * we seem to have no option to not respond...
  180. */
  181. davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
  182. dev_dbg(dev->dev, "PSC = %d\n",
  183. davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
  184. dev_dbg(dev->dev, "CLKL = %d\n",
  185. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
  186. dev_dbg(dev->dev, "CLKH = %d\n",
  187. davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
  188. dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
  189. pdata->bus_freq, pdata->bus_delay);
  190. /* Take the I2C module out of reset: */
  191. davinci_i2c_reset_ctrl(dev, 1);
  192. /* Enable interrupts */
  193. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
  194. return 0;
  195. }
  196. /*
  197. * Waiting for bus not busy
  198. */
  199. static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
  200. char allow_sleep)
  201. {
  202. unsigned long timeout;
  203. timeout = jiffies + dev->adapter.timeout;
  204. while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
  205. & DAVINCI_I2C_STR_BB) {
  206. if (time_after(jiffies, timeout)) {
  207. dev_warn(dev->dev,
  208. "timeout waiting for bus ready\n");
  209. return -ETIMEDOUT;
  210. }
  211. if (allow_sleep)
  212. schedule_timeout(1);
  213. }
  214. return 0;
  215. }
  216. /*
  217. * Low level master read/write transaction. This function is called
  218. * from i2c_davinci_xfer.
  219. */
  220. static int
  221. i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
  222. {
  223. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  224. struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
  225. u32 flag;
  226. u16 w;
  227. int r;
  228. if (!pdata)
  229. pdata = &davinci_i2c_platform_data_default;
  230. /* Introduce a delay, required for some boards (e.g Davinci EVM) */
  231. if (pdata->bus_delay)
  232. udelay(pdata->bus_delay);
  233. /* set the slave address */
  234. davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
  235. dev->buf = msg->buf;
  236. dev->buf_len = msg->len;
  237. dev->stop = stop;
  238. davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
  239. INIT_COMPLETION(dev->cmd_complete);
  240. dev->cmd_err = 0;
  241. /* Take I2C out of reset, configure it as master and set the
  242. * start bit */
  243. flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST | DAVINCI_I2C_MDR_STT;
  244. /* if the slave address is ten bit address, enable XA bit */
  245. if (msg->flags & I2C_M_TEN)
  246. flag |= DAVINCI_I2C_MDR_XA;
  247. if (!(msg->flags & I2C_M_RD))
  248. flag |= DAVINCI_I2C_MDR_TRX;
  249. if (stop)
  250. flag |= DAVINCI_I2C_MDR_STP;
  251. if (msg->len == 0) {
  252. flag |= DAVINCI_I2C_MDR_RM;
  253. flag &= ~DAVINCI_I2C_MDR_STP;
  254. }
  255. /* Enable receive or transmit interrupts */
  256. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
  257. if (msg->flags & I2C_M_RD)
  258. w |= DAVINCI_I2C_IMR_RRDY;
  259. else
  260. w |= DAVINCI_I2C_IMR_XRDY;
  261. davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
  262. dev->terminate = 0;
  263. /* write the data into mode register */
  264. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
  265. /*
  266. * First byte should be set here, not after interrupt,
  267. * because transmit-data-ready interrupt can come before
  268. * NACK-interrupt during sending of previous message and
  269. * ICDXR may have wrong data
  270. */
  271. if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
  272. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
  273. dev->buf_len--;
  274. }
  275. r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  276. dev->adapter.timeout);
  277. if (r == 0) {
  278. dev_err(dev->dev, "controller timed out\n");
  279. i2c_davinci_init(dev);
  280. dev->buf_len = 0;
  281. return -ETIMEDOUT;
  282. }
  283. if (dev->buf_len) {
  284. /* This should be 0 if all bytes were transferred
  285. * or dev->cmd_err denotes an error.
  286. * A signal may have aborted the transfer.
  287. */
  288. if (r >= 0) {
  289. dev_err(dev->dev, "abnormal termination buf_len=%i\n",
  290. dev->buf_len);
  291. r = -EREMOTEIO;
  292. }
  293. dev->terminate = 1;
  294. wmb();
  295. dev->buf_len = 0;
  296. }
  297. if (r < 0)
  298. return r;
  299. /* no error */
  300. if (likely(!dev->cmd_err))
  301. return msg->len;
  302. /* We have an error */
  303. if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
  304. i2c_davinci_init(dev);
  305. return -EIO;
  306. }
  307. if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
  308. if (msg->flags & I2C_M_IGNORE_NAK)
  309. return msg->len;
  310. if (stop) {
  311. w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  312. w |= DAVINCI_I2C_MDR_STP;
  313. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  314. }
  315. return -EREMOTEIO;
  316. }
  317. return -EIO;
  318. }
  319. /*
  320. * Prepare controller for a transaction and call i2c_davinci_xfer_msg
  321. */
  322. static int
  323. i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  324. {
  325. struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
  326. int i;
  327. int ret;
  328. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  329. ret = i2c_davinci_wait_bus_not_busy(dev, 1);
  330. if (ret < 0) {
  331. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  332. return ret;
  333. }
  334. for (i = 0; i < num; i++) {
  335. ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  336. dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
  337. ret);
  338. if (ret < 0)
  339. return ret;
  340. }
  341. return num;
  342. }
  343. static u32 i2c_davinci_func(struct i2c_adapter *adap)
  344. {
  345. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  346. }
  347. static void terminate_read(struct davinci_i2c_dev *dev)
  348. {
  349. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  350. w |= DAVINCI_I2C_MDR_NACK;
  351. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  352. /* Throw away data */
  353. davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
  354. if (!dev->terminate)
  355. dev_err(dev->dev, "RDR IRQ while no data requested\n");
  356. }
  357. static void terminate_write(struct davinci_i2c_dev *dev)
  358. {
  359. u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
  360. w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
  361. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
  362. if (!dev->terminate)
  363. dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
  364. }
  365. /*
  366. * Interrupt service routine. This gets called whenever an I2C interrupt
  367. * occurs.
  368. */
  369. static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
  370. {
  371. struct davinci_i2c_dev *dev = dev_id;
  372. u32 stat;
  373. int count = 0;
  374. u16 w;
  375. while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
  376. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  377. if (count++ == 100) {
  378. dev_warn(dev->dev, "Too much work in one IRQ\n");
  379. break;
  380. }
  381. switch (stat) {
  382. case DAVINCI_I2C_IVR_AL:
  383. /* Arbitration lost, must retry */
  384. dev->cmd_err |= DAVINCI_I2C_STR_AL;
  385. dev->buf_len = 0;
  386. complete(&dev->cmd_complete);
  387. break;
  388. case DAVINCI_I2C_IVR_NACK:
  389. dev->cmd_err |= DAVINCI_I2C_STR_NACK;
  390. dev->buf_len = 0;
  391. complete(&dev->cmd_complete);
  392. break;
  393. case DAVINCI_I2C_IVR_ARDY:
  394. davinci_i2c_write_reg(dev,
  395. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
  396. if (((dev->buf_len == 0) && (dev->stop != 0)) ||
  397. (dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
  398. w = davinci_i2c_read_reg(dev,
  399. DAVINCI_I2C_MDR_REG);
  400. w |= DAVINCI_I2C_MDR_STP;
  401. davinci_i2c_write_reg(dev,
  402. DAVINCI_I2C_MDR_REG, w);
  403. }
  404. complete(&dev->cmd_complete);
  405. break;
  406. case DAVINCI_I2C_IVR_RDR:
  407. if (dev->buf_len) {
  408. *dev->buf++ =
  409. davinci_i2c_read_reg(dev,
  410. DAVINCI_I2C_DRR_REG);
  411. dev->buf_len--;
  412. if (dev->buf_len)
  413. continue;
  414. davinci_i2c_write_reg(dev,
  415. DAVINCI_I2C_STR_REG,
  416. DAVINCI_I2C_IMR_RRDY);
  417. } else {
  418. /* signal can terminate transfer */
  419. terminate_read(dev);
  420. }
  421. break;
  422. case DAVINCI_I2C_IVR_XRDY:
  423. if (dev->buf_len) {
  424. davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
  425. *dev->buf++);
  426. dev->buf_len--;
  427. if (dev->buf_len)
  428. continue;
  429. w = davinci_i2c_read_reg(dev,
  430. DAVINCI_I2C_IMR_REG);
  431. w &= ~DAVINCI_I2C_IMR_XRDY;
  432. davinci_i2c_write_reg(dev,
  433. DAVINCI_I2C_IMR_REG,
  434. w);
  435. } else {
  436. /* signal can terminate transfer */
  437. terminate_write(dev);
  438. }
  439. break;
  440. case DAVINCI_I2C_IVR_SCD:
  441. davinci_i2c_write_reg(dev,
  442. DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
  443. complete(&dev->cmd_complete);
  444. break;
  445. case DAVINCI_I2C_IVR_AAS:
  446. dev_dbg(dev->dev, "Address as slave interrupt\n");
  447. break;
  448. default:
  449. dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
  450. break;
  451. }
  452. }
  453. return count ? IRQ_HANDLED : IRQ_NONE;
  454. }
  455. static struct i2c_algorithm i2c_davinci_algo = {
  456. .master_xfer = i2c_davinci_xfer,
  457. .functionality = i2c_davinci_func,
  458. };
  459. static int davinci_i2c_probe(struct platform_device *pdev)
  460. {
  461. struct davinci_i2c_dev *dev;
  462. struct i2c_adapter *adap;
  463. struct resource *mem, *irq, *ioarea;
  464. int r;
  465. /* NOTE: driver uses the static register mapping */
  466. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  467. if (!mem) {
  468. dev_err(&pdev->dev, "no mem resource?\n");
  469. return -ENODEV;
  470. }
  471. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  472. if (!irq) {
  473. dev_err(&pdev->dev, "no irq resource?\n");
  474. return -ENODEV;
  475. }
  476. ioarea = request_mem_region(mem->start, resource_size(mem),
  477. pdev->name);
  478. if (!ioarea) {
  479. dev_err(&pdev->dev, "I2C region already claimed\n");
  480. return -EBUSY;
  481. }
  482. dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
  483. if (!dev) {
  484. r = -ENOMEM;
  485. goto err_release_region;
  486. }
  487. init_completion(&dev->cmd_complete);
  488. dev->dev = get_device(&pdev->dev);
  489. dev->irq = irq->start;
  490. platform_set_drvdata(pdev, dev);
  491. dev->clk = clk_get(&pdev->dev, NULL);
  492. if (IS_ERR(dev->clk)) {
  493. r = -ENODEV;
  494. goto err_free_mem;
  495. }
  496. clk_enable(dev->clk);
  497. dev->base = ioremap(mem->start, resource_size(mem));
  498. if (!dev->base) {
  499. r = -EBUSY;
  500. goto err_mem_ioremap;
  501. }
  502. i2c_davinci_init(dev);
  503. r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
  504. if (r) {
  505. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  506. goto err_unuse_clocks;
  507. }
  508. adap = &dev->adapter;
  509. i2c_set_adapdata(adap, dev);
  510. adap->owner = THIS_MODULE;
  511. adap->class = I2C_CLASS_HWMON;
  512. strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
  513. adap->algo = &i2c_davinci_algo;
  514. adap->dev.parent = &pdev->dev;
  515. adap->timeout = DAVINCI_I2C_TIMEOUT;
  516. adap->nr = pdev->id;
  517. r = i2c_add_numbered_adapter(adap);
  518. if (r) {
  519. dev_err(&pdev->dev, "failure adding adapter\n");
  520. goto err_free_irq;
  521. }
  522. return 0;
  523. err_free_irq:
  524. free_irq(dev->irq, dev);
  525. err_unuse_clocks:
  526. iounmap(dev->base);
  527. err_mem_ioremap:
  528. clk_disable(dev->clk);
  529. clk_put(dev->clk);
  530. dev->clk = NULL;
  531. err_free_mem:
  532. platform_set_drvdata(pdev, NULL);
  533. put_device(&pdev->dev);
  534. kfree(dev);
  535. err_release_region:
  536. release_mem_region(mem->start, resource_size(mem));
  537. return r;
  538. }
  539. static int davinci_i2c_remove(struct platform_device *pdev)
  540. {
  541. struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
  542. struct resource *mem;
  543. platform_set_drvdata(pdev, NULL);
  544. i2c_del_adapter(&dev->adapter);
  545. put_device(&pdev->dev);
  546. clk_disable(dev->clk);
  547. clk_put(dev->clk);
  548. dev->clk = NULL;
  549. davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
  550. free_irq(IRQ_I2C, dev);
  551. iounmap(dev->base);
  552. kfree(dev);
  553. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  554. release_mem_region(mem->start, resource_size(mem));
  555. return 0;
  556. }
  557. /* work with hotplug and coldplug */
  558. MODULE_ALIAS("platform:i2c_davinci");
  559. static struct platform_driver davinci_i2c_driver = {
  560. .probe = davinci_i2c_probe,
  561. .remove = davinci_i2c_remove,
  562. .driver = {
  563. .name = "i2c_davinci",
  564. .owner = THIS_MODULE,
  565. },
  566. };
  567. /* I2C may be needed to bring up other drivers */
  568. static int __init davinci_i2c_init_driver(void)
  569. {
  570. return platform_driver_register(&davinci_i2c_driver);
  571. }
  572. subsys_initcall(davinci_i2c_init_driver);
  573. static void __exit davinci_i2c_exit_driver(void)
  574. {
  575. platform_driver_unregister(&davinci_i2c_driver);
  576. }
  577. module_exit(davinci_i2c_exit_driver);
  578. MODULE_AUTHOR("Texas Instruments India");
  579. MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
  580. MODULE_LICENSE("GPL");