gpio.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2013 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 <subdev/volt.h>
  25. #include <subdev/bios.h>
  26. #include <subdev/bios/gpio.h>
  27. #include <subdev/gpio.h>
  28. static const u8 tags[] = {
  29. DCB_GPIO_VID0, DCB_GPIO_VID1, DCB_GPIO_VID2, DCB_GPIO_VID3,
  30. DCB_GPIO_VID4, DCB_GPIO_VID5, DCB_GPIO_VID6, DCB_GPIO_VID7,
  31. };
  32. int
  33. nvkm_voltgpio_get(struct nvkm_volt *volt)
  34. {
  35. struct nvkm_gpio *gpio = volt->subdev.device->gpio;
  36. u8 vid = 0;
  37. int i;
  38. for (i = 0; i < ARRAY_SIZE(tags); i++) {
  39. if (volt->vid_mask & (1 << i)) {
  40. int ret = nvkm_gpio_get(gpio, 0, tags[i], 0xff);
  41. if (ret < 0)
  42. return ret;
  43. vid |= ret << i;
  44. }
  45. }
  46. return vid;
  47. }
  48. int
  49. nvkm_voltgpio_set(struct nvkm_volt *volt, u8 vid)
  50. {
  51. struct nvkm_gpio *gpio = volt->subdev.device->gpio;
  52. int i;
  53. for (i = 0; i < ARRAY_SIZE(tags); i++, vid >>= 1) {
  54. if (volt->vid_mask & (1 << i)) {
  55. int ret = nvkm_gpio_set(gpio, 0, tags[i], 0xff, vid & 1);
  56. if (ret < 0)
  57. return ret;
  58. }
  59. }
  60. return 0;
  61. }
  62. int
  63. nvkm_voltgpio_init(struct nvkm_volt *volt)
  64. {
  65. struct nvkm_subdev *subdev = &volt->subdev;
  66. struct nvkm_gpio *gpio = subdev->device->gpio;
  67. struct dcb_gpio_func func;
  68. int i;
  69. /* check we have gpio function info for each vid bit. on some
  70. * boards (ie. nvs295) the vid mask has more bits than there
  71. * are valid gpio functions... from traces, nvidia appear to
  72. * just touch the existing ones, so let's mask off the invalid
  73. * bits and continue with life
  74. */
  75. for (i = 0; i < ARRAY_SIZE(tags); i++) {
  76. if (volt->vid_mask & (1 << i)) {
  77. int ret = nvkm_gpio_find(gpio, 0, tags[i], 0xff, &func);
  78. if (ret) {
  79. if (ret != -ENOENT)
  80. return ret;
  81. nvkm_debug(subdev, "VID bit %d has no GPIO\n", i);
  82. volt->vid_mask &= ~(1 << i);
  83. }
  84. }
  85. }
  86. return 0;
  87. }