i2c-davinci.c 16 KB

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