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