i2c-efm32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright (C) 2014 Uwe Kleine-Koenig for Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU General Public License version 2 as published by the
  6. * Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/i2c.h>
  11. #include <linux/io.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/err.h>
  14. #include <linux/clk.h>
  15. #define DRIVER_NAME "efm32-i2c"
  16. #define MASK_VAL(mask, val) ((val << __ffs(mask)) & mask)
  17. #define REG_CTRL 0x00
  18. #define REG_CTRL_EN 0x00001
  19. #define REG_CTRL_SLAVE 0x00002
  20. #define REG_CTRL_AUTOACK 0x00004
  21. #define REG_CTRL_AUTOSE 0x00008
  22. #define REG_CTRL_AUTOSN 0x00010
  23. #define REG_CTRL_ARBDIS 0x00020
  24. #define REG_CTRL_GCAMEN 0x00040
  25. #define REG_CTRL_CLHR__MASK 0x00300
  26. #define REG_CTRL_BITO__MASK 0x03000
  27. #define REG_CTRL_BITO_OFF 0x00000
  28. #define REG_CTRL_BITO_40PCC 0x01000
  29. #define REG_CTRL_BITO_80PCC 0x02000
  30. #define REG_CTRL_BITO_160PCC 0x03000
  31. #define REG_CTRL_GIBITO 0x08000
  32. #define REG_CTRL_CLTO__MASK 0x70000
  33. #define REG_CTRL_CLTO_OFF 0x00000
  34. #define REG_CMD 0x04
  35. #define REG_CMD_START 0x00001
  36. #define REG_CMD_STOP 0x00002
  37. #define REG_CMD_ACK 0x00004
  38. #define REG_CMD_NACK 0x00008
  39. #define REG_CMD_CONT 0x00010
  40. #define REG_CMD_ABORT 0x00020
  41. #define REG_CMD_CLEARTX 0x00040
  42. #define REG_CMD_CLEARPC 0x00080
  43. #define REG_STATE 0x08
  44. #define REG_STATE_BUSY 0x00001
  45. #define REG_STATE_MASTER 0x00002
  46. #define REG_STATE_TRANSMITTER 0x00004
  47. #define REG_STATE_NACKED 0x00008
  48. #define REG_STATE_BUSHOLD 0x00010
  49. #define REG_STATE_STATE__MASK 0x000e0
  50. #define REG_STATE_STATE_IDLE 0x00000
  51. #define REG_STATE_STATE_WAIT 0x00020
  52. #define REG_STATE_STATE_START 0x00040
  53. #define REG_STATE_STATE_ADDR 0x00060
  54. #define REG_STATE_STATE_ADDRACK 0x00080
  55. #define REG_STATE_STATE_DATA 0x000a0
  56. #define REG_STATE_STATE_DATAACK 0x000c0
  57. #define REG_STATUS 0x0c
  58. #define REG_STATUS_PSTART 0x00001
  59. #define REG_STATUS_PSTOP 0x00002
  60. #define REG_STATUS_PACK 0x00004
  61. #define REG_STATUS_PNACK 0x00008
  62. #define REG_STATUS_PCONT 0x00010
  63. #define REG_STATUS_PABORT 0x00020
  64. #define REG_STATUS_TXC 0x00040
  65. #define REG_STATUS_TXBL 0x00080
  66. #define REG_STATUS_RXDATAV 0x00100
  67. #define REG_CLKDIV 0x10
  68. #define REG_CLKDIV_DIV__MASK 0x001ff
  69. #define REG_CLKDIV_DIV(div) MASK_VAL(REG_CLKDIV_DIV__MASK, (div))
  70. #define REG_SADDR 0x14
  71. #define REG_SADDRMASK 0x18
  72. #define REG_RXDATA 0x1c
  73. #define REG_RXDATAP 0x20
  74. #define REG_TXDATA 0x24
  75. #define REG_IF 0x28
  76. #define REG_IF_START 0x00001
  77. #define REG_IF_RSTART 0x00002
  78. #define REG_IF_ADDR 0x00004
  79. #define REG_IF_TXC 0x00008
  80. #define REG_IF_TXBL 0x00010
  81. #define REG_IF_RXDATAV 0x00020
  82. #define REG_IF_ACK 0x00040
  83. #define REG_IF_NACK 0x00080
  84. #define REG_IF_MSTOP 0x00100
  85. #define REG_IF_ARBLOST 0x00200
  86. #define REG_IF_BUSERR 0x00400
  87. #define REG_IF_BUSHOLD 0x00800
  88. #define REG_IF_TXOF 0x01000
  89. #define REG_IF_RXUF 0x02000
  90. #define REG_IF_BITO 0x04000
  91. #define REG_IF_CLTO 0x08000
  92. #define REG_IF_SSTOP 0x10000
  93. #define REG_IFS 0x2c
  94. #define REG_IFC 0x30
  95. #define REG_IFC__MASK 0x1ffcf
  96. #define REG_IEN 0x34
  97. #define REG_ROUTE 0x38
  98. #define REG_ROUTE_SDAPEN 0x00001
  99. #define REG_ROUTE_SCLPEN 0x00002
  100. #define REG_ROUTE_LOCATION__MASK 0x00700
  101. #define REG_ROUTE_LOCATION(n) MASK_VAL(REG_ROUTE_LOCATION__MASK, (n))
  102. struct efm32_i2c_ddata {
  103. struct i2c_adapter adapter;
  104. struct clk *clk;
  105. void __iomem *base;
  106. unsigned int irq;
  107. u8 location;
  108. unsigned long frequency;
  109. /* transfer data */
  110. struct completion done;
  111. struct i2c_msg *msgs;
  112. size_t num_msgs;
  113. size_t current_word, current_msg;
  114. int retval;
  115. };
  116. static u32 efm32_i2c_read32(struct efm32_i2c_ddata *ddata, unsigned offset)
  117. {
  118. return readl(ddata->base + offset);
  119. }
  120. static void efm32_i2c_write32(struct efm32_i2c_ddata *ddata,
  121. unsigned offset, u32 value)
  122. {
  123. writel(value, ddata->base + offset);
  124. }
  125. static void efm32_i2c_send_next_msg(struct efm32_i2c_ddata *ddata)
  126. {
  127. struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg];
  128. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_START);
  129. efm32_i2c_write32(ddata, REG_TXDATA, cur_msg->addr << 1 |
  130. (cur_msg->flags & I2C_M_RD ? 1 : 0));
  131. }
  132. static void efm32_i2c_send_next_byte(struct efm32_i2c_ddata *ddata)
  133. {
  134. struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg];
  135. if (ddata->current_word >= cur_msg->len) {
  136. /* cur_msg completely transferred */
  137. ddata->current_word = 0;
  138. ddata->current_msg += 1;
  139. if (ddata->current_msg >= ddata->num_msgs) {
  140. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP);
  141. complete(&ddata->done);
  142. } else {
  143. efm32_i2c_send_next_msg(ddata);
  144. }
  145. } else {
  146. efm32_i2c_write32(ddata, REG_TXDATA,
  147. cur_msg->buf[ddata->current_word++]);
  148. }
  149. }
  150. static void efm32_i2c_recv_next_byte(struct efm32_i2c_ddata *ddata)
  151. {
  152. struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg];
  153. cur_msg->buf[ddata->current_word] = efm32_i2c_read32(ddata, REG_RXDATA);
  154. ddata->current_word += 1;
  155. if (ddata->current_word >= cur_msg->len) {
  156. /* cur_msg completely transferred */
  157. ddata->current_word = 0;
  158. ddata->current_msg += 1;
  159. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_NACK);
  160. if (ddata->current_msg >= ddata->num_msgs) {
  161. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP);
  162. complete(&ddata->done);
  163. } else {
  164. efm32_i2c_send_next_msg(ddata);
  165. }
  166. } else {
  167. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_ACK);
  168. }
  169. }
  170. static irqreturn_t efm32_i2c_irq(int irq, void *dev_id)
  171. {
  172. struct efm32_i2c_ddata *ddata = dev_id;
  173. struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg];
  174. u32 irqflag = efm32_i2c_read32(ddata, REG_IF);
  175. u32 state = efm32_i2c_read32(ddata, REG_STATE);
  176. efm32_i2c_write32(ddata, REG_IFC, irqflag & REG_IFC__MASK);
  177. switch (state & REG_STATE_STATE__MASK) {
  178. case REG_STATE_STATE_IDLE:
  179. /* arbitration lost? */
  180. ddata->retval = -EAGAIN;
  181. complete(&ddata->done);
  182. break;
  183. case REG_STATE_STATE_WAIT:
  184. /*
  185. * huh, this shouldn't happen.
  186. * Reset hardware state and get out
  187. */
  188. ddata->retval = -EIO;
  189. efm32_i2c_write32(ddata, REG_CMD,
  190. REG_CMD_STOP | REG_CMD_ABORT |
  191. REG_CMD_CLEARTX | REG_CMD_CLEARPC);
  192. complete(&ddata->done);
  193. break;
  194. case REG_STATE_STATE_START:
  195. /* "caller" is expected to send an address */
  196. break;
  197. case REG_STATE_STATE_ADDR:
  198. /* wait for Ack or NAck of slave */
  199. break;
  200. case REG_STATE_STATE_ADDRACK:
  201. if (state & REG_STATE_NACKED) {
  202. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP);
  203. ddata->retval = -ENXIO;
  204. complete(&ddata->done);
  205. } else if (cur_msg->flags & I2C_M_RD) {
  206. /* wait for slave to send first data byte */
  207. } else {
  208. efm32_i2c_send_next_byte(ddata);
  209. }
  210. break;
  211. case REG_STATE_STATE_DATA:
  212. if (cur_msg->flags & I2C_M_RD) {
  213. efm32_i2c_recv_next_byte(ddata);
  214. } else {
  215. /* wait for Ack or Nack of slave */
  216. }
  217. break;
  218. case REG_STATE_STATE_DATAACK:
  219. if (state & REG_STATE_NACKED) {
  220. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP);
  221. complete(&ddata->done);
  222. } else {
  223. efm32_i2c_send_next_byte(ddata);
  224. }
  225. }
  226. return IRQ_HANDLED;
  227. }
  228. static int efm32_i2c_master_xfer(struct i2c_adapter *adap,
  229. struct i2c_msg *msgs, int num)
  230. {
  231. struct efm32_i2c_ddata *ddata = i2c_get_adapdata(adap);
  232. int ret;
  233. if (ddata->msgs)
  234. return -EBUSY;
  235. ddata->msgs = msgs;
  236. ddata->num_msgs = num;
  237. ddata->current_word = 0;
  238. ddata->current_msg = 0;
  239. ddata->retval = -EIO;
  240. reinit_completion(&ddata->done);
  241. dev_dbg(&ddata->adapter.dev, "state: %08x, status: %08x\n",
  242. efm32_i2c_read32(ddata, REG_STATE),
  243. efm32_i2c_read32(ddata, REG_STATUS));
  244. efm32_i2c_send_next_msg(ddata);
  245. wait_for_completion(&ddata->done);
  246. if (ddata->current_msg >= ddata->num_msgs)
  247. ret = ddata->num_msgs;
  248. else
  249. ret = ddata->retval;
  250. return ret;
  251. }
  252. static u32 efm32_i2c_functionality(struct i2c_adapter *adap)
  253. {
  254. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  255. }
  256. static const struct i2c_algorithm efm32_i2c_algo = {
  257. .master_xfer = efm32_i2c_master_xfer,
  258. .functionality = efm32_i2c_functionality,
  259. };
  260. static u32 efm32_i2c_get_configured_location(struct efm32_i2c_ddata *ddata)
  261. {
  262. u32 reg = efm32_i2c_read32(ddata, REG_ROUTE);
  263. return (reg & REG_ROUTE_LOCATION__MASK) >>
  264. __ffs(REG_ROUTE_LOCATION__MASK);
  265. }
  266. static int efm32_i2c_probe(struct platform_device *pdev)
  267. {
  268. struct efm32_i2c_ddata *ddata;
  269. struct resource *res;
  270. unsigned long rate;
  271. struct device_node *np = pdev->dev.of_node;
  272. u32 location, frequency;
  273. int ret;
  274. u32 clkdiv;
  275. if (!np)
  276. return -EINVAL;
  277. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  278. if (!ddata)
  279. return -ENOMEM;
  280. platform_set_drvdata(pdev, ddata);
  281. init_completion(&ddata->done);
  282. strlcpy(ddata->adapter.name, pdev->name, sizeof(ddata->adapter.name));
  283. ddata->adapter.owner = THIS_MODULE;
  284. ddata->adapter.algo = &efm32_i2c_algo;
  285. ddata->adapter.dev.parent = &pdev->dev;
  286. ddata->adapter.dev.of_node = pdev->dev.of_node;
  287. i2c_set_adapdata(&ddata->adapter, ddata);
  288. ddata->clk = devm_clk_get(&pdev->dev, NULL);
  289. if (IS_ERR(ddata->clk)) {
  290. ret = PTR_ERR(ddata->clk);
  291. dev_err(&pdev->dev, "failed to get clock: %d\n", ret);
  292. return ret;
  293. }
  294. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. if (!res) {
  296. dev_err(&pdev->dev, "failed to determine base address\n");
  297. return -ENODEV;
  298. }
  299. if (resource_size(res) < 0x42) {
  300. dev_err(&pdev->dev, "memory resource too small\n");
  301. return -EINVAL;
  302. }
  303. ddata->base = devm_ioremap_resource(&pdev->dev, res);
  304. if (IS_ERR(ddata->base))
  305. return PTR_ERR(ddata->base);
  306. ret = platform_get_irq(pdev, 0);
  307. if (ret <= 0) {
  308. dev_err(&pdev->dev, "failed to get irq (%d)\n", ret);
  309. if (!ret)
  310. ret = -EINVAL;
  311. return ret;
  312. }
  313. ddata->irq = ret;
  314. ret = clk_prepare_enable(ddata->clk);
  315. if (ret < 0) {
  316. dev_err(&pdev->dev, "failed to enable clock (%d)\n", ret);
  317. return ret;
  318. }
  319. ret = of_property_read_u32(np, "energymicro,location", &location);
  320. if (ret)
  321. /* fall back to wrongly namespaced property */
  322. ret = of_property_read_u32(np, "efm32,location", &location);
  323. if (!ret) {
  324. dev_dbg(&pdev->dev, "using location %u\n", location);
  325. } else {
  326. /* default to location configured in hardware */
  327. location = efm32_i2c_get_configured_location(ddata);
  328. dev_info(&pdev->dev, "fall back to location %u\n", location);
  329. }
  330. ddata->location = location;
  331. ret = of_property_read_u32(np, "clock-frequency", &frequency);
  332. if (!ret) {
  333. dev_dbg(&pdev->dev, "using frequency %u\n", frequency);
  334. } else {
  335. frequency = 100000;
  336. dev_info(&pdev->dev, "defaulting to 100 kHz\n");
  337. }
  338. ddata->frequency = frequency;
  339. rate = clk_get_rate(ddata->clk);
  340. if (!rate) {
  341. dev_err(&pdev->dev, "there is no input clock available\n");
  342. ret = -EINVAL;
  343. goto err_disable_clk;
  344. }
  345. clkdiv = DIV_ROUND_UP(rate, 8 * ddata->frequency) - 1;
  346. if (clkdiv >= 0x200) {
  347. dev_err(&pdev->dev,
  348. "input clock too fast (%lu) to divide down to bus freq (%lu)",
  349. rate, ddata->frequency);
  350. ret = -EINVAL;
  351. goto err_disable_clk;
  352. }
  353. dev_dbg(&pdev->dev, "input clock = %lu, bus freq = %lu, clkdiv = %lu\n",
  354. rate, ddata->frequency, (unsigned long)clkdiv);
  355. efm32_i2c_write32(ddata, REG_CLKDIV, REG_CLKDIV_DIV(clkdiv));
  356. efm32_i2c_write32(ddata, REG_ROUTE, REG_ROUTE_SDAPEN |
  357. REG_ROUTE_SCLPEN |
  358. REG_ROUTE_LOCATION(ddata->location));
  359. efm32_i2c_write32(ddata, REG_CTRL, REG_CTRL_EN |
  360. REG_CTRL_BITO_160PCC | 0 * REG_CTRL_GIBITO);
  361. efm32_i2c_write32(ddata, REG_IFC, REG_IFC__MASK);
  362. efm32_i2c_write32(ddata, REG_IEN, REG_IF_TXC | REG_IF_ACK | REG_IF_NACK
  363. | REG_IF_ARBLOST | REG_IF_BUSERR | REG_IF_RXDATAV);
  364. /* to make bus idle */
  365. efm32_i2c_write32(ddata, REG_CMD, REG_CMD_ABORT);
  366. ret = request_irq(ddata->irq, efm32_i2c_irq, 0, DRIVER_NAME, ddata);
  367. if (ret < 0) {
  368. dev_err(&pdev->dev, "failed to request irq (%d)\n", ret);
  369. goto err_disable_clk;
  370. }
  371. ret = i2c_add_adapter(&ddata->adapter);
  372. if (ret) {
  373. dev_err(&pdev->dev, "failed to add i2c adapter (%d)\n", ret);
  374. free_irq(ddata->irq, ddata);
  375. err_disable_clk:
  376. clk_disable_unprepare(ddata->clk);
  377. }
  378. return ret;
  379. }
  380. static int efm32_i2c_remove(struct platform_device *pdev)
  381. {
  382. struct efm32_i2c_ddata *ddata = platform_get_drvdata(pdev);
  383. i2c_del_adapter(&ddata->adapter);
  384. free_irq(ddata->irq, ddata);
  385. clk_disable_unprepare(ddata->clk);
  386. return 0;
  387. }
  388. static const struct of_device_id efm32_i2c_dt_ids[] = {
  389. {
  390. .compatible = "energymicro,efm32-i2c",
  391. }, {
  392. /* sentinel */
  393. }
  394. };
  395. MODULE_DEVICE_TABLE(of, efm32_i2c_dt_ids);
  396. static struct platform_driver efm32_i2c_driver = {
  397. .probe = efm32_i2c_probe,
  398. .remove = efm32_i2c_remove,
  399. .driver = {
  400. .name = DRIVER_NAME,
  401. .of_match_table = efm32_i2c_dt_ids,
  402. },
  403. };
  404. module_platform_driver(efm32_i2c_driver);
  405. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  406. MODULE_DESCRIPTION("EFM32 i2c driver");
  407. MODULE_LICENSE("GPL v2");
  408. MODULE_ALIAS("platform:" DRIVER_NAME);