i2c-davinci.c 16 KB

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