i2c-at91.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
  3. *
  4. * Copyright (C) 2011 Weinmann Medical GmbH
  5. * Author: Nikolaus Voss <n.voss@weinmann.de>
  6. *
  7. * Evolved from original work by:
  8. * Copyright (C) 2004 Rick Bronson
  9. * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
  10. *
  11. * Borrowed heavily from original work by:
  12. * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/completion.h>
  21. #include <linux/err.h>
  22. #include <linux/i2c.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #define TWI_CLK_HZ 100000 /* max 400 Kbits/s */
  29. #define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */
  30. /* AT91 TWI register definitions */
  31. #define AT91_TWI_CR 0x0000 /* Control Register */
  32. #define AT91_TWI_START 0x0001 /* Send a Start Condition */
  33. #define AT91_TWI_STOP 0x0002 /* Send a Stop Condition */
  34. #define AT91_TWI_MSEN 0x0004 /* Master Transfer Enable */
  35. #define AT91_TWI_SVDIS 0x0020 /* Slave Transfer Disable */
  36. #define AT91_TWI_SWRST 0x0080 /* Software Reset */
  37. #define AT91_TWI_MMR 0x0004 /* Master Mode Register */
  38. #define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */
  39. #define AT91_TWI_MREAD 0x1000 /* Master Read Direction */
  40. #define AT91_TWI_IADR 0x000c /* Internal Address Register */
  41. #define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */
  42. #define AT91_TWI_SR 0x0020 /* Status Register */
  43. #define AT91_TWI_TXCOMP 0x0001 /* Transmission Complete */
  44. #define AT91_TWI_RXRDY 0x0002 /* Receive Holding Register Ready */
  45. #define AT91_TWI_TXRDY 0x0004 /* Transmit Holding Register Ready */
  46. #define AT91_TWI_OVRE 0x0040 /* Overrun Error */
  47. #define AT91_TWI_UNRE 0x0080 /* Underrun Error */
  48. #define AT91_TWI_NACK 0x0100 /* Not Acknowledged */
  49. #define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */
  50. #define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */
  51. #define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */
  52. #define AT91_TWI_RHR 0x0030 /* Receive Holding Register */
  53. #define AT91_TWI_THR 0x0034 /* Transmit Holding Register */
  54. struct at91_twi_pdata {
  55. unsigned clk_max_div;
  56. unsigned clk_offset;
  57. bool has_unre_flag;
  58. };
  59. struct at91_twi_dev {
  60. struct device *dev;
  61. void __iomem *base;
  62. struct completion cmd_complete;
  63. struct clk *clk;
  64. u8 *buf;
  65. size_t buf_len;
  66. struct i2c_msg *msg;
  67. int irq;
  68. unsigned transfer_status;
  69. struct i2c_adapter adapter;
  70. unsigned twi_cwgr_reg;
  71. struct at91_twi_pdata *pdata;
  72. };
  73. static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
  74. {
  75. return readl_relaxed(dev->base + reg);
  76. }
  77. static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val)
  78. {
  79. writel_relaxed(val, dev->base + reg);
  80. }
  81. static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
  82. {
  83. at91_twi_write(dev, AT91_TWI_IDR,
  84. AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY);
  85. }
  86. static void at91_init_twi_bus(struct at91_twi_dev *dev)
  87. {
  88. at91_disable_twi_interrupts(dev);
  89. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
  90. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
  91. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
  92. at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
  93. }
  94. /*
  95. * Calculate symmetric clock as stated in datasheet:
  96. * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
  97. */
  98. static void __devinit at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
  99. {
  100. int ckdiv, cdiv, div;
  101. struct at91_twi_pdata *pdata = dev->pdata;
  102. int offset = pdata->clk_offset;
  103. int max_ckdiv = pdata->clk_max_div;
  104. div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
  105. 2 * twi_clk) - offset);
  106. ckdiv = fls(div >> 8);
  107. cdiv = div >> ckdiv;
  108. if (ckdiv > max_ckdiv) {
  109. dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
  110. ckdiv, max_ckdiv);
  111. ckdiv = max_ckdiv;
  112. cdiv = 255;
  113. }
  114. dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv;
  115. dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv);
  116. }
  117. static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
  118. {
  119. if (dev->buf_len <= 0)
  120. return;
  121. at91_twi_write(dev, AT91_TWI_THR, *dev->buf);
  122. /* send stop when last byte has been written */
  123. if (--dev->buf_len == 0)
  124. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  125. dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len);
  126. ++dev->buf;
  127. }
  128. static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
  129. {
  130. if (dev->buf_len <= 0)
  131. return;
  132. *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff;
  133. --dev->buf_len;
  134. /* handle I2C_SMBUS_BLOCK_DATA */
  135. if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
  136. dev->msg->flags &= ~I2C_M_RECV_LEN;
  137. dev->buf_len += *dev->buf;
  138. dev->msg->len = dev->buf_len + 1;
  139. dev_dbg(dev->dev, "received block length %d\n", dev->buf_len);
  140. }
  141. /* send stop if second but last byte has been read */
  142. if (dev->buf_len == 1)
  143. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  144. dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len);
  145. ++dev->buf;
  146. }
  147. static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
  148. {
  149. struct at91_twi_dev *dev = dev_id;
  150. const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
  151. const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
  152. if (!irqstatus)
  153. return IRQ_NONE;
  154. else if (irqstatus & AT91_TWI_RXRDY)
  155. at91_twi_read_next_byte(dev);
  156. else if (irqstatus & AT91_TWI_TXRDY)
  157. at91_twi_write_next_byte(dev);
  158. /* catch error flags */
  159. dev->transfer_status |= status;
  160. if (irqstatus & AT91_TWI_TXCOMP) {
  161. at91_disable_twi_interrupts(dev);
  162. complete(&dev->cmd_complete);
  163. }
  164. return IRQ_HANDLED;
  165. }
  166. static int at91_do_twi_transfer(struct at91_twi_dev *dev)
  167. {
  168. int ret;
  169. bool has_unre_flag = dev->pdata->has_unre_flag;
  170. dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
  171. (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
  172. INIT_COMPLETION(dev->cmd_complete);
  173. dev->transfer_status = 0;
  174. if (dev->msg->flags & I2C_M_RD) {
  175. unsigned start_flags = AT91_TWI_START;
  176. if (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY) {
  177. dev_err(dev->dev, "RXRDY still set!");
  178. at91_twi_read(dev, AT91_TWI_RHR);
  179. }
  180. /* if only one byte is to be read, immediately stop transfer */
  181. if (dev->buf_len <= 1 && !(dev->msg->flags & I2C_M_RECV_LEN))
  182. start_flags |= AT91_TWI_STOP;
  183. at91_twi_write(dev, AT91_TWI_CR, start_flags);
  184. at91_twi_write(dev, AT91_TWI_IER,
  185. AT91_TWI_TXCOMP | AT91_TWI_RXRDY);
  186. } else {
  187. at91_twi_write_next_byte(dev);
  188. at91_twi_write(dev, AT91_TWI_IER,
  189. AT91_TWI_TXCOMP | AT91_TWI_TXRDY);
  190. }
  191. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  192. dev->adapter.timeout);
  193. if (ret == 0) {
  194. dev_err(dev->dev, "controller timed out\n");
  195. at91_init_twi_bus(dev);
  196. return -ETIMEDOUT;
  197. }
  198. if (dev->transfer_status & AT91_TWI_NACK) {
  199. dev_dbg(dev->dev, "received nack\n");
  200. return -EREMOTEIO;
  201. }
  202. if (dev->transfer_status & AT91_TWI_OVRE) {
  203. dev_err(dev->dev, "overrun while reading\n");
  204. return -EIO;
  205. }
  206. if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
  207. dev_err(dev->dev, "underrun while writing\n");
  208. return -EIO;
  209. }
  210. dev_dbg(dev->dev, "transfer complete\n");
  211. return 0;
  212. }
  213. static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
  214. {
  215. struct at91_twi_dev *dev = i2c_get_adapdata(adap);
  216. int ret;
  217. unsigned int_addr_flag = 0;
  218. struct i2c_msg *m_start = msg;
  219. dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
  220. /*
  221. * The hardware can handle at most two messages concatenated by a
  222. * repeated start via it's internal address feature.
  223. */
  224. if (num > 2) {
  225. dev_err(dev->dev,
  226. "cannot handle more than two concatenated messages.\n");
  227. return 0;
  228. } else if (num == 2) {
  229. int internal_address = 0;
  230. int i;
  231. if (msg->flags & I2C_M_RD) {
  232. dev_err(dev->dev, "first transfer must be write.\n");
  233. return -EINVAL;
  234. }
  235. if (msg->len > 3) {
  236. dev_err(dev->dev, "first message size must be <= 3.\n");
  237. return -EINVAL;
  238. }
  239. /* 1st msg is put into the internal address, start with 2nd */
  240. m_start = &msg[1];
  241. for (i = 0; i < msg->len; ++i) {
  242. const unsigned addr = msg->buf[msg->len - 1 - i];
  243. internal_address |= addr << (8 * i);
  244. int_addr_flag += AT91_TWI_IADRSZ_1;
  245. }
  246. at91_twi_write(dev, AT91_TWI_IADR, internal_address);
  247. }
  248. at91_twi_write(dev, AT91_TWI_MMR, (m_start->addr << 16) | int_addr_flag
  249. | ((m_start->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0));
  250. dev->buf_len = m_start->len;
  251. dev->buf = m_start->buf;
  252. dev->msg = m_start;
  253. ret = at91_do_twi_transfer(dev);
  254. return (ret < 0) ? ret : num;
  255. }
  256. static u32 at91_twi_func(struct i2c_adapter *adapter)
  257. {
  258. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  259. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  260. }
  261. static struct i2c_algorithm at91_twi_algorithm = {
  262. .master_xfer = at91_twi_xfer,
  263. .functionality = at91_twi_func,
  264. };
  265. static struct at91_twi_pdata at91rm9200_config = {
  266. .clk_max_div = 5,
  267. .clk_offset = 3,
  268. .has_unre_flag = true,
  269. };
  270. static struct at91_twi_pdata at91sam9261_config = {
  271. .clk_max_div = 5,
  272. .clk_offset = 4,
  273. .has_unre_flag = false,
  274. };
  275. static struct at91_twi_pdata at91sam9260_config = {
  276. .clk_max_div = 7,
  277. .clk_offset = 4,
  278. .has_unre_flag = false,
  279. };
  280. static struct at91_twi_pdata at91sam9g20_config = {
  281. .clk_max_div = 7,
  282. .clk_offset = 4,
  283. .has_unre_flag = false,
  284. };
  285. static struct at91_twi_pdata at91sam9g10_config = {
  286. .clk_max_div = 7,
  287. .clk_offset = 4,
  288. .has_unre_flag = false,
  289. };
  290. static const struct platform_device_id at91_twi_devtypes[] = {
  291. {
  292. .name = "i2c-at91rm9200",
  293. .driver_data = (unsigned long) &at91rm9200_config,
  294. }, {
  295. .name = "i2c-at91sam9261",
  296. .driver_data = (unsigned long) &at91sam9261_config,
  297. }, {
  298. .name = "i2c-at91sam9260",
  299. .driver_data = (unsigned long) &at91sam9260_config,
  300. }, {
  301. .name = "i2c-at91sam9g20",
  302. .driver_data = (unsigned long) &at91sam9g20_config,
  303. }, {
  304. .name = "i2c-at91sam9g10",
  305. .driver_data = (unsigned long) &at91sam9g10_config,
  306. }, {
  307. /* sentinel */
  308. }
  309. };
  310. static int __devinit at91_twi_probe(struct platform_device *pdev)
  311. {
  312. struct at91_twi_dev *dev;
  313. struct resource *mem;
  314. int rc;
  315. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  316. if (!dev)
  317. return -ENOMEM;
  318. init_completion(&dev->cmd_complete);
  319. dev->dev = &pdev->dev;
  320. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  321. if (!mem)
  322. return -ENODEV;
  323. dev->pdata = at91_twi_get_driver_data(pdev);
  324. if (!dev->pdata)
  325. return -ENODEV;
  326. dev->base = devm_request_and_ioremap(&pdev->dev, mem);
  327. if (!dev->base)
  328. return -EBUSY;
  329. dev->irq = platform_get_irq(pdev, 0);
  330. if (dev->irq < 0)
  331. return dev->irq;
  332. rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
  333. dev_name(dev->dev), dev);
  334. if (rc) {
  335. dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
  336. return rc;
  337. }
  338. platform_set_drvdata(pdev, dev);
  339. dev->clk = devm_clk_get(dev->dev, NULL);
  340. if (IS_ERR(dev->clk)) {
  341. dev_err(dev->dev, "no clock defined\n");
  342. return -ENODEV;
  343. }
  344. clk_prepare_enable(dev->clk);
  345. at91_calc_twi_clock(dev, TWI_CLK_HZ);
  346. at91_init_twi_bus(dev);
  347. snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
  348. i2c_set_adapdata(&dev->adapter, dev);
  349. dev->adapter.owner = THIS_MODULE;
  350. dev->adapter.class = I2C_CLASS_HWMON;
  351. dev->adapter.algo = &at91_twi_algorithm;
  352. dev->adapter.dev.parent = dev->dev;
  353. dev->adapter.nr = pdev->id;
  354. dev->adapter.timeout = AT91_I2C_TIMEOUT;
  355. rc = i2c_add_numbered_adapter(&dev->adapter);
  356. if (rc) {
  357. dev_err(dev->dev, "Adapter %s registration failed\n",
  358. dev->adapter.name);
  359. clk_disable_unprepare(dev->clk);
  360. return rc;
  361. }
  362. dev_info(dev->dev, "AT91 i2c bus driver.\n");
  363. return 0;
  364. }
  365. static int __devexit at91_twi_remove(struct platform_device *pdev)
  366. {
  367. struct at91_twi_dev *dev = platform_get_drvdata(pdev);
  368. int rc;
  369. rc = i2c_del_adapter(&dev->adapter);
  370. clk_disable_unprepare(dev->clk);
  371. return rc;
  372. }
  373. #ifdef CONFIG_PM
  374. static int at91_twi_runtime_suspend(struct device *dev)
  375. {
  376. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  377. clk_disable(twi_dev->clk);
  378. return 0;
  379. }
  380. static int at91_twi_runtime_resume(struct device *dev)
  381. {
  382. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  383. return clk_enable(twi_dev->clk);
  384. }
  385. static const struct dev_pm_ops at91_twi_pm = {
  386. .runtime_suspend = at91_twi_runtime_suspend,
  387. .runtime_resume = at91_twi_runtime_resume,
  388. };
  389. #define at91_twi_pm_ops (&at91_twi_pm)
  390. #else
  391. #define at91_twi_pm_ops NULL
  392. #endif
  393. static struct platform_driver at91_twi_driver = {
  394. .probe = at91_twi_probe,
  395. .remove = __devexit_p(at91_twi_remove),
  396. .id_table = at91_twi_devtypes,
  397. .driver = {
  398. .name = "at91_i2c",
  399. .owner = THIS_MODULE,
  400. .pm = at91_twi_pm_ops,
  401. },
  402. };
  403. static int __init at91_twi_init(void)
  404. {
  405. return platform_driver_register(&at91_twi_driver);
  406. }
  407. static void __exit at91_twi_exit(void)
  408. {
  409. platform_driver_unregister(&at91_twi_driver);
  410. }
  411. subsys_initcall(at91_twi_init);
  412. module_exit(at91_twi_exit);
  413. MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
  414. MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
  415. MODULE_LICENSE("GPL");
  416. MODULE_ALIAS("platform:at91_i2c");