i2c-at91.c 14 KB

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