nv50_gpio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_hw.h"
  27. #include "nv50_display.h"
  28. static void nv50_gpio_isr(struct drm_device *dev);
  29. static void nv50_gpio_isr_bh(struct work_struct *work);
  30. struct nv50_gpio_priv {
  31. struct list_head handlers;
  32. spinlock_t lock;
  33. };
  34. struct nv50_gpio_handler {
  35. struct drm_device *dev;
  36. struct list_head head;
  37. struct work_struct work;
  38. bool inhibit;
  39. struct dcb_gpio_entry *gpio;
  40. void (*handler)(void *data, int state);
  41. void *data;
  42. };
  43. static int
  44. nv50_gpio_location(struct dcb_gpio_entry *gpio, uint32_t *reg, uint32_t *shift)
  45. {
  46. const uint32_t nv50_gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
  47. if (gpio->line >= 32)
  48. return -EINVAL;
  49. *reg = nv50_gpio_reg[gpio->line >> 3];
  50. *shift = (gpio->line & 7) << 2;
  51. return 0;
  52. }
  53. int
  54. nv50_gpio_get(struct drm_device *dev, enum dcb_gpio_tag tag)
  55. {
  56. struct dcb_gpio_entry *gpio;
  57. uint32_t r, s, v;
  58. gpio = nouveau_bios_gpio_entry(dev, tag);
  59. if (!gpio)
  60. return -ENOENT;
  61. if (nv50_gpio_location(gpio, &r, &s))
  62. return -EINVAL;
  63. v = nv_rd32(dev, r) >> (s + 2);
  64. return ((v & 1) == (gpio->state[1] & 1));
  65. }
  66. int
  67. nv50_gpio_set(struct drm_device *dev, enum dcb_gpio_tag tag, int state)
  68. {
  69. struct dcb_gpio_entry *gpio;
  70. uint32_t r, s, v;
  71. gpio = nouveau_bios_gpio_entry(dev, tag);
  72. if (!gpio)
  73. return -ENOENT;
  74. if (nv50_gpio_location(gpio, &r, &s))
  75. return -EINVAL;
  76. v = nv_rd32(dev, r) & ~(0x3 << s);
  77. v |= (gpio->state[state] ^ 2) << s;
  78. nv_wr32(dev, r, v);
  79. return 0;
  80. }
  81. int
  82. nv50_gpio_irq_register(struct drm_device *dev, enum dcb_gpio_tag tag,
  83. void (*handler)(void *, int), void *data)
  84. {
  85. struct drm_nouveau_private *dev_priv = dev->dev_private;
  86. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  87. struct nv50_gpio_priv *priv = pgpio->priv;
  88. struct nv50_gpio_handler *gpioh;
  89. struct dcb_gpio_entry *gpio;
  90. unsigned long flags;
  91. gpio = nouveau_bios_gpio_entry(dev, tag);
  92. if (!gpio)
  93. return -ENOENT;
  94. gpioh = kzalloc(sizeof(*gpioh), GFP_KERNEL);
  95. if (!gpioh)
  96. return -ENOMEM;
  97. INIT_WORK(&gpioh->work, nv50_gpio_isr_bh);
  98. gpioh->dev = dev;
  99. gpioh->gpio = gpio;
  100. gpioh->handler = handler;
  101. gpioh->data = data;
  102. spin_lock_irqsave(&priv->lock, flags);
  103. list_add(&gpioh->head, &priv->handlers);
  104. spin_unlock_irqrestore(&priv->lock, flags);
  105. return 0;
  106. }
  107. void
  108. nv50_gpio_irq_unregister(struct drm_device *dev, enum dcb_gpio_tag tag,
  109. void (*handler)(void *, int), void *data)
  110. {
  111. struct drm_nouveau_private *dev_priv = dev->dev_private;
  112. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  113. struct nv50_gpio_priv *priv = pgpio->priv;
  114. struct nv50_gpio_handler *gpioh, *tmp;
  115. struct dcb_gpio_entry *gpio;
  116. LIST_HEAD(tofree);
  117. unsigned long flags;
  118. gpio = nouveau_bios_gpio_entry(dev, tag);
  119. if (!gpio)
  120. return;
  121. spin_lock_irqsave(&priv->lock, flags);
  122. list_for_each_entry_safe(gpioh, tmp, &priv->handlers, head) {
  123. if (gpioh->gpio != gpio ||
  124. gpioh->handler != handler ||
  125. gpioh->data != data)
  126. continue;
  127. list_move(&gpioh->head, &tofree);
  128. }
  129. spin_unlock_irqrestore(&priv->lock, flags);
  130. list_for_each_entry_safe(gpioh, tmp, &tofree, head) {
  131. flush_work_sync(&gpioh->work);
  132. kfree(gpioh);
  133. }
  134. }
  135. bool
  136. nv50_gpio_irq_enable(struct drm_device *dev, enum dcb_gpio_tag tag, bool on)
  137. {
  138. struct dcb_gpio_entry *gpio;
  139. u32 reg, mask;
  140. gpio = nouveau_bios_gpio_entry(dev, tag);
  141. if (!gpio)
  142. return false;
  143. reg = gpio->line < 16 ? 0xe050 : 0xe070;
  144. mask = 0x00010001 << (gpio->line & 0xf);
  145. nv_wr32(dev, reg + 4, mask);
  146. reg = nv_mask(dev, reg + 0, mask, on ? mask : 0);
  147. return (reg & mask) == mask;
  148. }
  149. static int
  150. nv50_gpio_create(struct drm_device *dev)
  151. {
  152. struct drm_nouveau_private *dev_priv = dev->dev_private;
  153. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  154. struct nv50_gpio_priv *priv;
  155. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  156. if (!priv)
  157. return -ENOMEM;
  158. INIT_LIST_HEAD(&priv->handlers);
  159. spin_lock_init(&priv->lock);
  160. pgpio->priv = priv;
  161. return 0;
  162. }
  163. static void
  164. nv50_gpio_destroy(struct drm_device *dev)
  165. {
  166. struct drm_nouveau_private *dev_priv = dev->dev_private;
  167. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  168. kfree(pgpio->priv);
  169. pgpio->priv = NULL;
  170. }
  171. int
  172. nv50_gpio_init(struct drm_device *dev)
  173. {
  174. struct drm_nouveau_private *dev_priv = dev->dev_private;
  175. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  176. int ret;
  177. if (!pgpio->priv) {
  178. ret = nv50_gpio_create(dev);
  179. if (ret)
  180. return ret;
  181. }
  182. /* disable, and ack any pending gpio interrupts */
  183. nv_wr32(dev, 0xe050, 0x00000000);
  184. nv_wr32(dev, 0xe054, 0xffffffff);
  185. if (dev_priv->chipset >= 0x90) {
  186. nv_wr32(dev, 0xe070, 0x00000000);
  187. nv_wr32(dev, 0xe074, 0xffffffff);
  188. }
  189. nouveau_irq_register(dev, 21, nv50_gpio_isr);
  190. return 0;
  191. }
  192. void
  193. nv50_gpio_fini(struct drm_device *dev)
  194. {
  195. struct drm_nouveau_private *dev_priv = dev->dev_private;
  196. nv_wr32(dev, 0xe050, 0x00000000);
  197. if (dev_priv->chipset >= 0x90)
  198. nv_wr32(dev, 0xe070, 0x00000000);
  199. nouveau_irq_unregister(dev, 21);
  200. nv50_gpio_destroy(dev);
  201. }
  202. static void
  203. nv50_gpio_isr_bh(struct work_struct *work)
  204. {
  205. struct nv50_gpio_handler *gpioh =
  206. container_of(work, struct nv50_gpio_handler, work);
  207. struct drm_nouveau_private *dev_priv = gpioh->dev->dev_private;
  208. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  209. struct nv50_gpio_priv *priv = pgpio->priv;
  210. unsigned long flags;
  211. int state;
  212. state = pgpio->get(gpioh->dev, gpioh->gpio->tag);
  213. if (state < 0)
  214. return;
  215. gpioh->handler(gpioh->data, state);
  216. spin_lock_irqsave(&priv->lock, flags);
  217. gpioh->inhibit = false;
  218. spin_unlock_irqrestore(&priv->lock, flags);
  219. }
  220. static void
  221. nv50_gpio_isr(struct drm_device *dev)
  222. {
  223. struct drm_nouveau_private *dev_priv = dev->dev_private;
  224. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  225. struct nv50_gpio_priv *priv = pgpio->priv;
  226. struct nv50_gpio_handler *gpioh;
  227. u32 intr0, intr1 = 0;
  228. u32 hi, lo, ch;
  229. intr0 = nv_rd32(dev, 0xe054) & nv_rd32(dev, 0xe050);
  230. if (dev_priv->chipset >= 0x90)
  231. intr1 = nv_rd32(dev, 0xe074) & nv_rd32(dev, 0xe070);
  232. hi = (intr0 & 0x0000ffff) | (intr1 << 16);
  233. lo = (intr0 >> 16) | (intr1 & 0xffff0000);
  234. ch = hi | lo;
  235. nv_wr32(dev, 0xe054, intr0);
  236. if (dev_priv->chipset >= 0x90)
  237. nv_wr32(dev, 0xe074, intr1);
  238. spin_lock(&priv->lock);
  239. list_for_each_entry(gpioh, &priv->handlers, head) {
  240. if (!(ch & (1 << gpioh->gpio->line)))
  241. continue;
  242. if (gpioh->inhibit)
  243. continue;
  244. gpioh->inhibit = true;
  245. schedule_work(&gpioh->work);
  246. }
  247. spin_unlock(&priv->lock);
  248. }