i2c-octeon-platdrv.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * (C) Copyright 2009-2010
  3. * Nokia Siemens Networks, [email protected]
  4. *
  5. * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
  6. *
  7. * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <asm/octeon/octeon.h>
  25. #include "i2c-octeon-core.h"
  26. #define DRV_NAME "i2c-octeon"
  27. /**
  28. * octeon_i2c_int_enable - enable the CORE interrupt
  29. * @i2c: The struct octeon_i2c
  30. *
  31. * The interrupt will be asserted when there is non-STAT_IDLE state in
  32. * the SW_TWSI_EOP_TWSI_STAT register.
  33. */
  34. static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
  35. {
  36. octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
  37. }
  38. /* disable the CORE interrupt */
  39. static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
  40. {
  41. /* clear TS/ST/IFLG events */
  42. octeon_i2c_write_int(i2c, 0);
  43. }
  44. /**
  45. * octeon_i2c_int_enable78 - enable the CORE interrupt
  46. * @i2c: The struct octeon_i2c
  47. *
  48. * The interrupt will be asserted when there is non-STAT_IDLE state in the
  49. * SW_TWSI_EOP_TWSI_STAT register.
  50. */
  51. static void octeon_i2c_int_enable78(struct octeon_i2c *i2c)
  52. {
  53. atomic_inc_return(&i2c->int_enable_cnt);
  54. enable_irq(i2c->irq);
  55. }
  56. static void __octeon_i2c_irq_disable(atomic_t *cnt, int irq)
  57. {
  58. int count;
  59. /*
  60. * The interrupt can be disabled in two places, but we only
  61. * want to make the disable_irq_nosync() call once, so keep
  62. * track with the atomic variable.
  63. */
  64. count = atomic_dec_if_positive(cnt);
  65. if (count >= 0)
  66. disable_irq_nosync(irq);
  67. }
  68. /* disable the CORE interrupt */
  69. static void octeon_i2c_int_disable78(struct octeon_i2c *i2c)
  70. {
  71. __octeon_i2c_irq_disable(&i2c->int_enable_cnt, i2c->irq);
  72. }
  73. /**
  74. * octeon_i2c_hlc_int_enable78 - enable the ST interrupt
  75. * @i2c: The struct octeon_i2c
  76. *
  77. * The interrupt will be asserted when there is non-STAT_IDLE state in
  78. * the SW_TWSI_EOP_TWSI_STAT register.
  79. */
  80. static void octeon_i2c_hlc_int_enable78(struct octeon_i2c *i2c)
  81. {
  82. atomic_inc_return(&i2c->hlc_int_enable_cnt);
  83. enable_irq(i2c->hlc_irq);
  84. }
  85. /* disable the ST interrupt */
  86. static void octeon_i2c_hlc_int_disable78(struct octeon_i2c *i2c)
  87. {
  88. __octeon_i2c_irq_disable(&i2c->hlc_int_enable_cnt, i2c->hlc_irq);
  89. }
  90. /* HLC interrupt service routine */
  91. static irqreturn_t octeon_i2c_hlc_isr78(int irq, void *dev_id)
  92. {
  93. struct octeon_i2c *i2c = dev_id;
  94. i2c->hlc_int_disable(i2c);
  95. wake_up(&i2c->queue);
  96. return IRQ_HANDLED;
  97. }
  98. static void octeon_i2c_hlc_int_enable(struct octeon_i2c *i2c)
  99. {
  100. octeon_i2c_write_int(i2c, TWSI_INT_ST_EN);
  101. }
  102. static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
  103. {
  104. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  105. I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
  106. }
  107. static const struct i2c_algorithm octeon_i2c_algo = {
  108. .master_xfer = octeon_i2c_xfer,
  109. .functionality = octeon_i2c_functionality,
  110. };
  111. static const struct i2c_adapter octeon_i2c_ops = {
  112. .owner = THIS_MODULE,
  113. .name = "OCTEON adapter",
  114. .algo = &octeon_i2c_algo,
  115. };
  116. static int octeon_i2c_probe(struct platform_device *pdev)
  117. {
  118. struct device_node *node = pdev->dev.of_node;
  119. int irq, result = 0, hlc_irq = 0;
  120. struct octeon_i2c *i2c;
  121. bool cn78xx_style;
  122. cn78xx_style = of_device_is_compatible(node, "cavium,octeon-7890-twsi");
  123. if (cn78xx_style) {
  124. hlc_irq = platform_get_irq(pdev, 0);
  125. if (hlc_irq < 0)
  126. return hlc_irq;
  127. irq = platform_get_irq(pdev, 2);
  128. if (irq < 0)
  129. return irq;
  130. } else {
  131. /* All adaptors have an irq. */
  132. irq = platform_get_irq(pdev, 0);
  133. if (irq < 0)
  134. return irq;
  135. }
  136. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  137. if (!i2c) {
  138. result = -ENOMEM;
  139. goto out;
  140. }
  141. i2c->dev = &pdev->dev;
  142. i2c->roff.sw_twsi = 0x00;
  143. i2c->roff.twsi_int = 0x10;
  144. i2c->roff.sw_twsi_ext = 0x18;
  145. i2c->twsi_base = devm_platform_ioremap_resource(pdev, 0);
  146. if (IS_ERR(i2c->twsi_base)) {
  147. result = PTR_ERR(i2c->twsi_base);
  148. goto out;
  149. }
  150. /*
  151. * "clock-rate" is a legacy binding, the official binding is
  152. * "clock-frequency". Try the official one first and then
  153. * fall back if it doesn't exist.
  154. */
  155. if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
  156. of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
  157. dev_err(i2c->dev,
  158. "no I2C 'clock-rate' or 'clock-frequency' property\n");
  159. result = -ENXIO;
  160. goto out;
  161. }
  162. i2c->sys_freq = octeon_get_io_clock_rate();
  163. init_waitqueue_head(&i2c->queue);
  164. i2c->irq = irq;
  165. if (cn78xx_style) {
  166. i2c->hlc_irq = hlc_irq;
  167. i2c->int_enable = octeon_i2c_int_enable78;
  168. i2c->int_disable = octeon_i2c_int_disable78;
  169. i2c->hlc_int_enable = octeon_i2c_hlc_int_enable78;
  170. i2c->hlc_int_disable = octeon_i2c_hlc_int_disable78;
  171. irq_set_status_flags(i2c->irq, IRQ_NOAUTOEN);
  172. irq_set_status_flags(i2c->hlc_irq, IRQ_NOAUTOEN);
  173. result = devm_request_irq(&pdev->dev, i2c->hlc_irq,
  174. octeon_i2c_hlc_isr78, 0,
  175. DRV_NAME, i2c);
  176. if (result < 0) {
  177. dev_err(i2c->dev, "failed to attach interrupt\n");
  178. goto out;
  179. }
  180. } else {
  181. i2c->int_enable = octeon_i2c_int_enable;
  182. i2c->int_disable = octeon_i2c_int_disable;
  183. i2c->hlc_int_enable = octeon_i2c_hlc_int_enable;
  184. i2c->hlc_int_disable = octeon_i2c_int_disable;
  185. }
  186. result = devm_request_irq(&pdev->dev, i2c->irq,
  187. octeon_i2c_isr, 0, DRV_NAME, i2c);
  188. if (result < 0) {
  189. dev_err(i2c->dev, "failed to attach interrupt\n");
  190. goto out;
  191. }
  192. if (OCTEON_IS_MODEL(OCTEON_CN38XX))
  193. i2c->broken_irq_check = true;
  194. result = octeon_i2c_init_lowlevel(i2c);
  195. if (result) {
  196. dev_err(i2c->dev, "init low level failed\n");
  197. goto out;
  198. }
  199. octeon_i2c_set_clock(i2c);
  200. i2c->adap = octeon_i2c_ops;
  201. i2c->adap.timeout = msecs_to_jiffies(2);
  202. i2c->adap.retries = 5;
  203. i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
  204. i2c->adap.dev.parent = &pdev->dev;
  205. i2c->adap.dev.of_node = node;
  206. i2c_set_adapdata(&i2c->adap, i2c);
  207. platform_set_drvdata(pdev, i2c);
  208. result = i2c_add_adapter(&i2c->adap);
  209. if (result < 0)
  210. goto out;
  211. dev_info(i2c->dev, "probed\n");
  212. return 0;
  213. out:
  214. return result;
  215. };
  216. static int octeon_i2c_remove(struct platform_device *pdev)
  217. {
  218. struct octeon_i2c *i2c = platform_get_drvdata(pdev);
  219. i2c_del_adapter(&i2c->adap);
  220. return 0;
  221. };
  222. static const struct of_device_id octeon_i2c_match[] = {
  223. { .compatible = "cavium,octeon-3860-twsi", },
  224. { .compatible = "cavium,octeon-7890-twsi", },
  225. {},
  226. };
  227. MODULE_DEVICE_TABLE(of, octeon_i2c_match);
  228. static struct platform_driver octeon_i2c_driver = {
  229. .probe = octeon_i2c_probe,
  230. .remove = octeon_i2c_remove,
  231. .driver = {
  232. .name = DRV_NAME,
  233. .of_match_table = octeon_i2c_match,
  234. },
  235. };
  236. module_platform_driver(octeon_i2c_driver);
  237. MODULE_AUTHOR("Michael Lawnick <[email protected]>");
  238. MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
  239. MODULE_LICENSE("GPL");