i2c-davinci.c 17 KB

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