gpio.c 2.8 KB

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