Home | History | Annotate | Line # | Download | only in therm
      1 /*	$NetBSD: nouveau_nvkm_subdev_therm_nv50.c,v 1.3 2021/12/18 23:45:41 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2012 Red Hat Inc.
      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: Ben Skeggs
     25  * 	    Martin Peres
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_therm_nv50.c,v 1.3 2021/12/18 23:45:41 riastradh Exp $");
     29 
     30 #include "priv.h"
     31 
     32 static int
     33 pwm_info(struct nvkm_therm *therm, int *line, int *ctrl, int *indx)
     34 {
     35 	struct nvkm_subdev *subdev = &therm->subdev;
     36 
     37 	if (*line == 0x04) {
     38 		*ctrl = 0x00e100;
     39 		*line = 4;
     40 		*indx = 0;
     41 	} else
     42 	if (*line == 0x09) {
     43 		*ctrl = 0x00e100;
     44 		*line = 9;
     45 		*indx = 1;
     46 	} else
     47 	if (*line == 0x10) {
     48 		*ctrl = 0x00e28c;
     49 		*line = 0;
     50 		*indx = 0;
     51 	} else {
     52 		nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", *line);
     53 		return -ENODEV;
     54 	}
     55 
     56 	return 0;
     57 }
     58 
     59 int
     60 nv50_fan_pwm_ctrl(struct nvkm_therm *therm, int line, bool enable)
     61 {
     62 	struct nvkm_device *device = therm->subdev.device;
     63 	u32 data = enable ? 0x00000001 : 0x00000000;
     64 	int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
     65 	if (ret == 0)
     66 		nvkm_mask(device, ctrl, 0x00010001 << line, data << line);
     67 	return ret;
     68 }
     69 
     70 int
     71 nv50_fan_pwm_get(struct nvkm_therm *therm, int line, u32 *divs, u32 *duty)
     72 {
     73 	struct nvkm_device *device = therm->subdev.device;
     74 	int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
     75 	if (ret)
     76 		return ret;
     77 
     78 	if (nvkm_rd32(device, ctrl) & (1 << line)) {
     79 		*divs = nvkm_rd32(device, 0x00e114 + (id * 8));
     80 		*duty = nvkm_rd32(device, 0x00e118 + (id * 8));
     81 		return 0;
     82 	}
     83 
     84 	return -EINVAL;
     85 }
     86 
     87 int
     88 nv50_fan_pwm_set(struct nvkm_therm *therm, int line, u32 divs, u32 duty)
     89 {
     90 	struct nvkm_device *device = therm->subdev.device;
     91 	int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
     92 	if (ret)
     93 		return ret;
     94 
     95 	nvkm_wr32(device, 0x00e114 + (id * 8), divs);
     96 	nvkm_wr32(device, 0x00e118 + (id * 8), duty | 0x80000000);
     97 	return 0;
     98 }
     99 
    100 int
    101 nv50_fan_pwm_clock(struct nvkm_therm *therm, int line)
    102 {
    103 	struct nvkm_device *device = therm->subdev.device;
    104 	int pwm_clock;
    105 
    106 	/* determine the PWM source clock */
    107 	if (device->chipset > 0x50 && device->chipset < 0x94) {
    108 		u8 pwm_div = nvkm_rd32(device, 0x410c);
    109 		if (nvkm_rd32(device, 0xc040) & 0x800000) {
    110 			/* Use the HOST clock (100 MHz)
    111 			* Where does this constant(2.4) comes from? */
    112 			pwm_clock = (100000000 >> pwm_div) * 10 / 24;
    113 		} else {
    114 			/* Where does this constant(20) comes from? */
    115 			pwm_clock = (device->crystal * 1000) >> pwm_div;
    116 			pwm_clock /= 20;
    117 		}
    118 	} else {
    119 		pwm_clock = (device->crystal * 1000) / 20;
    120 	}
    121 
    122 	return pwm_clock;
    123 }
    124 
    125 static void
    126 nv50_sensor_setup(struct nvkm_therm *therm)
    127 {
    128 	struct nvkm_device *device = therm->subdev.device;
    129 	nvkm_mask(device, 0x20010, 0x40000000, 0x0);
    130 	mdelay(20); /* wait for the temperature to stabilize */
    131 }
    132 
    133 static int
    134 nv50_temp_get(struct nvkm_therm *therm)
    135 {
    136 	struct nvkm_device *device = therm->subdev.device;
    137 	struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
    138 	int core_temp;
    139 
    140 	core_temp = nvkm_rd32(device, 0x20014) & 0x3fff;
    141 
    142 	/* if the slope or the offset is unset, do no use the sensor */
    143 	if (!sensor->slope_div || !sensor->slope_mult ||
    144 	    !sensor->offset_num || !sensor->offset_den)
    145 	    return -ENODEV;
    146 
    147 	core_temp = core_temp * sensor->slope_mult / sensor->slope_div;
    148 	core_temp = core_temp + sensor->offset_num / sensor->offset_den;
    149 	core_temp = core_temp + sensor->offset_constant - 8;
    150 
    151 	/* reserve negative temperatures for errors */
    152 	if (core_temp < 0)
    153 		core_temp = 0;
    154 
    155 	return core_temp;
    156 }
    157 
    158 static void
    159 nv50_therm_init(struct nvkm_therm *therm)
    160 {
    161 	nv50_sensor_setup(therm);
    162 }
    163 
    164 static const struct nvkm_therm_func
    165 nv50_therm = {
    166 	.init = nv50_therm_init,
    167 	.intr = nv40_therm_intr,
    168 	.pwm_ctrl = nv50_fan_pwm_ctrl,
    169 	.pwm_get = nv50_fan_pwm_get,
    170 	.pwm_set = nv50_fan_pwm_set,
    171 	.pwm_clock = nv50_fan_pwm_clock,
    172 	.temp_get = nv50_temp_get,
    173 	.program_alarms = nvkm_therm_program_alarms_polling,
    174 };
    175 
    176 int
    177 nv50_therm_new(struct nvkm_device *device, int index,
    178 	       struct nvkm_therm **ptherm)
    179 {
    180 	return nvkm_therm_new_(&nv50_therm, device, index, ptherm);
    181 }
    182