i2c-at91.c 14 KB

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