i2c-octeon-platdrv.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * (C) Copyright 2009-2010
  3. * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
  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 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 resource *res_mem;
  121. struct octeon_i2c *i2c;
  122. bool cn78xx_style;
  123. cn78xx_style = of_device_is_compatible(node, "cavium,octeon-7890-twsi");
  124. if (cn78xx_style) {
  125. hlc_irq = platform_get_irq(pdev, 0);
  126. if (hlc_irq < 0)
  127. return hlc_irq;
  128. irq = platform_get_irq(pdev, 2);
  129. if (irq < 0)
  130. return irq;
  131. } else {
  132. /* All adaptors have an irq. */
  133. irq = platform_get_irq(pdev, 0);
  134. if (irq < 0)
  135. return irq;
  136. }
  137. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  138. if (!i2c) {
  139. result = -ENOMEM;
  140. goto out;
  141. }
  142. i2c->dev = &pdev->dev;
  143. i2c->roff.sw_twsi = 0x00;
  144. i2c->roff.twsi_int = 0x10;
  145. i2c->roff.sw_twsi_ext = 0x18;
  146. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
  148. if (IS_ERR(i2c->twsi_base)) {
  149. result = PTR_ERR(i2c->twsi_base);
  150. goto out;
  151. }
  152. /*
  153. * "clock-rate" is a legacy binding, the official binding is
  154. * "clock-frequency". Try the official one first and then
  155. * fall back if it doesn't exist.
  156. */
  157. if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
  158. of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
  159. dev_err(i2c->dev,
  160. "no I2C 'clock-rate' or 'clock-frequency' property\n");
  161. result = -ENXIO;
  162. goto out;
  163. }
  164. i2c->sys_freq = octeon_get_io_clock_rate();
  165. init_waitqueue_head(&i2c->queue);
  166. i2c->irq = irq;
  167. if (cn78xx_style) {
  168. i2c->hlc_irq = hlc_irq;
  169. i2c->int_enable = octeon_i2c_int_enable78;
  170. i2c->int_disable = octeon_i2c_int_disable78;
  171. i2c->hlc_int_enable = octeon_i2c_hlc_int_enable78;
  172. i2c->hlc_int_disable = octeon_i2c_hlc_int_disable78;
  173. irq_set_status_flags(i2c->irq, IRQ_NOAUTOEN);
  174. irq_set_status_flags(i2c->hlc_irq, IRQ_NOAUTOEN);
  175. result = devm_request_irq(&pdev->dev, i2c->hlc_irq,
  176. octeon_i2c_hlc_isr78, 0,
  177. DRV_NAME, i2c);
  178. if (result < 0) {
  179. dev_err(i2c->dev, "failed to attach interrupt\n");
  180. goto out;
  181. }
  182. } else {
  183. i2c->int_enable = octeon_i2c_int_enable;
  184. i2c->int_disable = octeon_i2c_int_disable;
  185. i2c->hlc_int_enable = octeon_i2c_hlc_int_enable;
  186. i2c->hlc_int_disable = octeon_i2c_int_disable;
  187. }
  188. result = devm_request_irq(&pdev->dev, i2c->irq,
  189. octeon_i2c_isr, 0, DRV_NAME, i2c);
  190. if (result < 0) {
  191. dev_err(i2c->dev, "failed to attach interrupt\n");
  192. goto out;
  193. }
  194. if (OCTEON_IS_MODEL(OCTEON_CN38XX))
  195. i2c->broken_irq_check = true;
  196. result = octeon_i2c_init_lowlevel(i2c);
  197. if (result) {
  198. dev_err(i2c->dev, "init low level failed\n");
  199. goto out;
  200. }
  201. octeon_i2c_set_clock(i2c);
  202. i2c->adap = octeon_i2c_ops;
  203. i2c->adap.timeout = msecs_to_jiffies(2);
  204. i2c->adap.retries = 5;
  205. i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
  206. i2c->adap.dev.parent = &pdev->dev;
  207. i2c->adap.dev.of_node = node;
  208. i2c_set_adapdata(&i2c->adap, i2c);
  209. platform_set_drvdata(pdev, i2c);
  210. result = i2c_add_adapter(&i2c->adap);
  211. if (result < 0)
  212. goto out;
  213. dev_info(i2c->dev, "probed\n");
  214. return 0;
  215. out:
  216. return result;
  217. };
  218. static int octeon_i2c_remove(struct platform_device *pdev)
  219. {
  220. struct octeon_i2c *i2c = platform_get_drvdata(pdev);
  221. i2c_del_adapter(&i2c->adap);
  222. return 0;
  223. };
  224. static const struct of_device_id octeon_i2c_match[] = {
  225. { .compatible = "cavium,octeon-3860-twsi", },
  226. { .compatible = "cavium,octeon-7890-twsi", },
  227. {},
  228. };
  229. MODULE_DEVICE_TABLE(of, octeon_i2c_match);
  230. static struct platform_driver octeon_i2c_driver = {
  231. .probe = octeon_i2c_probe,
  232. .remove = octeon_i2c_remove,
  233. .driver = {
  234. .name = DRV_NAME,
  235. .of_match_table = octeon_i2c_match,
  236. },
  237. };
  238. module_platform_driver(octeon_i2c_driver);
  239. MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
  240. MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
  241. MODULE_LICENSE("GPL");