Home | History | Annotate | Line # | Download | only in therm
      1 /*	$NetBSD: nouveau_nvkm_subdev_therm_fantog.c,v 1.4 2021/12/19 11:34:46 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2012 The Nouveau community
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  * Authors: Martin Peres
     25  */
     26 #include <sys/cdefs.h>
     27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_therm_fantog.c,v 1.4 2021/12/19 11:34:46 riastradh Exp $");
     28 
     29 #include "priv.h"
     30 
     31 #include <subdev/gpio.h>
     32 #include <subdev/timer.h>
     33 
     34 struct nvkm_fantog {
     35 	struct nvkm_fan base;
     36 	struct nvkm_alarm alarm;
     37 	spinlock_t lock;
     38 	u32 period_us;
     39 	u32 percent;
     40 	struct dcb_gpio_func func;
     41 };
     42 
     43 static void
     44 nvkm_fantog_update(struct nvkm_fantog *fan, int percent)
     45 {
     46 	struct nvkm_therm *therm = fan->base.parent;
     47 	struct nvkm_device *device = therm->subdev.device;
     48 	struct nvkm_timer *tmr = device->timer;
     49 	struct nvkm_gpio *gpio = device->gpio;
     50 	unsigned long flags;
     51 	int duty;
     52 
     53 	spin_lock_irqsave(&fan->lock, flags);
     54 	if (percent < 0)
     55 		percent = fan->percent;
     56 	fan->percent = percent;
     57 
     58 	duty = !nvkm_gpio_get(gpio, 0, DCB_GPIO_FAN, 0xff);
     59 	nvkm_gpio_set(gpio, 0, DCB_GPIO_FAN, 0xff, duty);
     60 
     61 	if (percent != (duty * 100)) {
     62 		u64 next_change = (percent * fan->period_us) / 100;
     63 		if (!duty)
     64 			next_change = fan->period_us - next_change;
     65 		nvkm_timer_alarm(tmr, next_change * 1000, &fan->alarm);
     66 	}
     67 	spin_unlock_irqrestore(&fan->lock, flags);
     68 }
     69 
     70 static void
     71 nvkm_fantog_alarm(struct nvkm_alarm *alarm)
     72 {
     73 	struct nvkm_fantog *fan =
     74 	       container_of(alarm, struct nvkm_fantog, alarm);
     75 	nvkm_fantog_update(fan, -1);
     76 }
     77 
     78 static int
     79 nvkm_fantog_get(struct nvkm_therm *therm)
     80 {
     81 	struct nvkm_fantog *fan = (void *)therm->fan;
     82 	return fan->percent;
     83 }
     84 
     85 static int
     86 nvkm_fantog_set(struct nvkm_therm *therm, int percent)
     87 {
     88 	struct nvkm_fantog *fan = (void *)therm->fan;
     89 	if (therm->func->pwm_ctrl)
     90 		therm->func->pwm_ctrl(therm, fan->func.line, false);
     91 	nvkm_fantog_update(fan, percent);
     92 	return 0;
     93 }
     94 
     95 static void
     96 nvkm_fantog_dtor(struct nvkm_fan *base)
     97 {
     98 	struct nvkm_fantog *fan = container_of(base, struct nvkm_fantog, base);
     99 	spin_lock_destroy(&fan->lock);
    100 }
    101 
    102 int
    103 nvkm_fantog_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
    104 {
    105 	struct nvkm_fantog *fan;
    106 	int ret;
    107 
    108 	if (therm->func->pwm_ctrl) {
    109 		ret = therm->func->pwm_ctrl(therm, func->line, false);
    110 		if (ret)
    111 			return ret;
    112 	}
    113 
    114 	fan = kzalloc(sizeof(*fan), GFP_KERNEL);
    115 	therm->fan = &fan->base;
    116 	if (!fan)
    117 		return -ENOMEM;
    118 
    119 	fan->base.type = "toggle";
    120 	fan->base.get = nvkm_fantog_get;
    121 	fan->base.set = nvkm_fantog_set;
    122 	fan->base.dtor = nvkm_fantog_dtor;
    123 	nvkm_alarm_init(&fan->alarm, nvkm_fantog_alarm);
    124 	fan->period_us = 100000; /* 10Hz */
    125 	fan->percent = 100;
    126 	fan->func = *func;
    127 	spin_lock_init(&fan->lock);
    128 	return 0;
    129 }
    130