fantog.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2012 The Nouveau community
  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: Martin Peres
  23. */
  24. #include "priv.h"
  25. #include <subdev/gpio.h>
  26. #include <subdev/timer.h>
  27. struct nvkm_fantog {
  28. struct nvkm_fan base;
  29. struct nvkm_alarm alarm;
  30. spinlock_t lock;
  31. u32 period_us;
  32. u32 percent;
  33. struct dcb_gpio_func func;
  34. };
  35. static void
  36. nvkm_fantog_update(struct nvkm_fantog *fan, int percent)
  37. {
  38. struct nvkm_therm *therm = fan->base.parent;
  39. struct nvkm_device *device = therm->subdev.device;
  40. struct nvkm_timer *tmr = device->timer;
  41. struct nvkm_gpio *gpio = device->gpio;
  42. unsigned long flags;
  43. int duty;
  44. spin_lock_irqsave(&fan->lock, flags);
  45. if (percent < 0)
  46. percent = fan->percent;
  47. fan->percent = percent;
  48. duty = !nvkm_gpio_get(gpio, 0, DCB_GPIO_FAN, 0xff);
  49. nvkm_gpio_set(gpio, 0, DCB_GPIO_FAN, 0xff, duty);
  50. if (list_empty(&fan->alarm.head) && percent != (duty * 100)) {
  51. u64 next_change = (percent * fan->period_us) / 100;
  52. if (!duty)
  53. next_change = fan->period_us - next_change;
  54. nvkm_timer_alarm(tmr, next_change * 1000, &fan->alarm);
  55. }
  56. spin_unlock_irqrestore(&fan->lock, flags);
  57. }
  58. static void
  59. nvkm_fantog_alarm(struct nvkm_alarm *alarm)
  60. {
  61. struct nvkm_fantog *fan =
  62. container_of(alarm, struct nvkm_fantog, alarm);
  63. nvkm_fantog_update(fan, -1);
  64. }
  65. static int
  66. nvkm_fantog_get(struct nvkm_therm *therm)
  67. {
  68. struct nvkm_fantog *fan = (void *)therm->fan;
  69. return fan->percent;
  70. }
  71. static int
  72. nvkm_fantog_set(struct nvkm_therm *therm, int percent)
  73. {
  74. struct nvkm_fantog *fan = (void *)therm->fan;
  75. if (therm->func->pwm_ctrl)
  76. therm->func->pwm_ctrl(therm, fan->func.line, false);
  77. nvkm_fantog_update(fan, percent);
  78. return 0;
  79. }
  80. int
  81. nvkm_fantog_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
  82. {
  83. struct nvkm_fantog *fan;
  84. int ret;
  85. if (therm->func->pwm_ctrl) {
  86. ret = therm->func->pwm_ctrl(therm, func->line, false);
  87. if (ret)
  88. return ret;
  89. }
  90. fan = kzalloc(sizeof(*fan), GFP_KERNEL);
  91. therm->fan = &fan->base;
  92. if (!fan)
  93. return -ENOMEM;
  94. fan->base.type = "toggle";
  95. fan->base.get = nvkm_fantog_get;
  96. fan->base.set = nvkm_fantog_set;
  97. nvkm_alarm_init(&fan->alarm, nvkm_fantog_alarm);
  98. fan->period_us = 100000; /* 10Hz */
  99. fan->percent = 100;
  100. fan->func = *func;
  101. spin_lock_init(&fan->lock);
  102. return 0;
  103. }