base.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2011 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 "priv.h"
  25. #include <core/notify.h>
  26. static int
  27. nvkm_gpio_drive(struct nvkm_gpio *gpio, int idx, int line, int dir, int out)
  28. {
  29. return gpio->func->drive(gpio, line, dir, out);
  30. }
  31. static int
  32. nvkm_gpio_sense(struct nvkm_gpio *gpio, int idx, int line)
  33. {
  34. return gpio->func->sense(gpio, line);
  35. }
  36. void
  37. nvkm_gpio_reset(struct nvkm_gpio *gpio, u8 func)
  38. {
  39. if (gpio->func->reset)
  40. gpio->func->reset(gpio, func);
  41. }
  42. int
  43. nvkm_gpio_find(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line,
  44. struct dcb_gpio_func *func)
  45. {
  46. struct nvkm_device *device = gpio->subdev.device;
  47. struct nvkm_bios *bios = device->bios;
  48. u8 ver, len;
  49. u16 data;
  50. if (line == 0xff && tag == 0xff)
  51. return -EINVAL;
  52. data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func);
  53. if (data)
  54. return 0;
  55. /* Apple iMac G4 NV18 */
  56. if (device->quirk && device->quirk->tv_gpio) {
  57. if (tag == DCB_GPIO_TVDAC0) {
  58. *func = (struct dcb_gpio_func) {
  59. .func = DCB_GPIO_TVDAC0,
  60. .line = device->quirk->tv_gpio,
  61. .log[0] = 0,
  62. .log[1] = 1,
  63. };
  64. return 0;
  65. }
  66. }
  67. return -ENOENT;
  68. }
  69. int
  70. nvkm_gpio_set(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, int state)
  71. {
  72. struct dcb_gpio_func func;
  73. int ret;
  74. ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
  75. if (ret == 0) {
  76. int dir = !!(func.log[state] & 0x02);
  77. int out = !!(func.log[state] & 0x01);
  78. ret = nvkm_gpio_drive(gpio, idx, func.line, dir, out);
  79. }
  80. return ret;
  81. }
  82. int
  83. nvkm_gpio_get(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line)
  84. {
  85. struct dcb_gpio_func func;
  86. int ret;
  87. ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
  88. if (ret == 0) {
  89. ret = nvkm_gpio_sense(gpio, idx, func.line);
  90. if (ret >= 0)
  91. ret = (ret == (func.log[1] & 1));
  92. }
  93. return ret;
  94. }
  95. static void
  96. nvkm_gpio_intr_fini(struct nvkm_event *event, int type, int index)
  97. {
  98. struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
  99. gpio->func->intr_mask(gpio, type, 1 << index, 0);
  100. }
  101. static void
  102. nvkm_gpio_intr_init(struct nvkm_event *event, int type, int index)
  103. {
  104. struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
  105. gpio->func->intr_mask(gpio, type, 1 << index, 1 << index);
  106. }
  107. static int
  108. nvkm_gpio_intr_ctor(struct nvkm_object *object, void *data, u32 size,
  109. struct nvkm_notify *notify)
  110. {
  111. struct nvkm_gpio_ntfy_req *req = data;
  112. if (!WARN_ON(size != sizeof(*req))) {
  113. notify->size = sizeof(struct nvkm_gpio_ntfy_rep);
  114. notify->types = req->mask;
  115. notify->index = req->line;
  116. return 0;
  117. }
  118. return -EINVAL;
  119. }
  120. static const struct nvkm_event_func
  121. nvkm_gpio_intr_func = {
  122. .ctor = nvkm_gpio_intr_ctor,
  123. .init = nvkm_gpio_intr_init,
  124. .fini = nvkm_gpio_intr_fini,
  125. };
  126. static void
  127. nvkm_gpio_intr(struct nvkm_subdev *subdev)
  128. {
  129. struct nvkm_gpio *gpio = nvkm_gpio(subdev);
  130. u32 hi, lo, i;
  131. gpio->func->intr_stat(gpio, &hi, &lo);
  132. for (i = 0; (hi | lo) && i < gpio->func->lines; i++) {
  133. struct nvkm_gpio_ntfy_rep rep = {
  134. .mask = (NVKM_GPIO_HI * !!(hi & (1 << i))) |
  135. (NVKM_GPIO_LO * !!(lo & (1 << i))),
  136. };
  137. nvkm_event_send(&gpio->event, rep.mask, i, &rep, sizeof(rep));
  138. }
  139. }
  140. static int
  141. nvkm_gpio_fini(struct nvkm_subdev *subdev, bool suspend)
  142. {
  143. struct nvkm_gpio *gpio = nvkm_gpio(subdev);
  144. u32 mask = (1ULL << gpio->func->lines) - 1;
  145. gpio->func->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0);
  146. gpio->func->intr_stat(gpio, &mask, &mask);
  147. return 0;
  148. }
  149. static const struct dmi_system_id gpio_reset_ids[] = {
  150. {
  151. .ident = "Apple Macbook 10,1",
  152. .matches = {
  153. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  154. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"),
  155. }
  156. },
  157. { }
  158. };
  159. static int
  160. nvkm_gpio_init(struct nvkm_subdev *subdev)
  161. {
  162. struct nvkm_gpio *gpio = nvkm_gpio(subdev);
  163. if (dmi_check_system(gpio_reset_ids))
  164. nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
  165. return 0;
  166. }
  167. static void *
  168. nvkm_gpio_dtor(struct nvkm_subdev *subdev)
  169. {
  170. struct nvkm_gpio *gpio = nvkm_gpio(subdev);
  171. nvkm_event_fini(&gpio->event);
  172. return gpio;
  173. }
  174. static const struct nvkm_subdev_func
  175. nvkm_gpio = {
  176. .dtor = nvkm_gpio_dtor,
  177. .init = nvkm_gpio_init,
  178. .fini = nvkm_gpio_fini,
  179. .intr = nvkm_gpio_intr,
  180. };
  181. int
  182. nvkm_gpio_new_(const struct nvkm_gpio_func *func, struct nvkm_device *device,
  183. int index, struct nvkm_gpio **pgpio)
  184. {
  185. struct nvkm_gpio *gpio;
  186. if (!(gpio = *pgpio = kzalloc(sizeof(*gpio), GFP_KERNEL)))
  187. return -ENOMEM;
  188. nvkm_subdev_ctor(&nvkm_gpio, device, index, &gpio->subdev);
  189. gpio->func = func;
  190. return nvkm_event_init(&nvkm_gpio_intr_func, 2, func->lines,
  191. &gpio->event);
  192. }