Home | History | Annotate | Line # | Download | only in volt
      1 /*	$NetBSD: nouveau_nvkm_subdev_volt_gk104.c,v 1.4 2021/12/18 23:45:42 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2015 Martin Peres
      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_volt_gk104.c,v 1.4 2021/12/18 23:45:42 riastradh Exp $");
     28 
     29 #include "priv.h"
     30 
     31 #include <subdev/volt.h>
     32 #include <subdev/gpio.h>
     33 #include <subdev/bios.h>
     34 #include <subdev/bios/volt.h>
     35 #include <subdev/fuse.h>
     36 
     37 #define gk104_volt(p) container_of((p), struct gk104_volt, base)
     38 struct gk104_volt {
     39 	struct nvkm_volt base;
     40 	struct nvbios_volt bios;
     41 };
     42 
     43 static int
     44 gk104_volt_get(struct nvkm_volt *base)
     45 {
     46 	struct nvbios_volt *bios = &gk104_volt(base)->bios;
     47 	struct nvkm_device *device = base->subdev.device;
     48 	u32 div, duty;
     49 
     50 	div  = nvkm_rd32(device, 0x20340);
     51 	duty = nvkm_rd32(device, 0x20344);
     52 
     53 	return bios->base + bios->pwm_range * duty / div;
     54 }
     55 
     56 static int
     57 gk104_volt_set(struct nvkm_volt *base, u32 uv)
     58 {
     59 	struct nvbios_volt *bios = &gk104_volt(base)->bios;
     60 	struct nvkm_device *device = base->subdev.device;
     61 	u32 div, duty;
     62 
     63 	/* the blob uses this crystal frequency, let's use it too. */
     64 	div = 27648000 / bios->pwm_freq;
     65 	duty = DIV_ROUND_UP((uv - bios->base) * div, bios->pwm_range);
     66 
     67 	nvkm_wr32(device, 0x20340, div);
     68 	nvkm_wr32(device, 0x20344, 0x80000000 | duty);
     69 
     70 	return 0;
     71 }
     72 
     73 static int
     74 gk104_volt_speedo_read(struct nvkm_volt *volt)
     75 {
     76 	struct nvkm_device *device = volt->subdev.device;
     77 	struct nvkm_fuse *fuse = device->fuse;
     78 	int ret;
     79 
     80 	if (!fuse)
     81 		return -EINVAL;
     82 
     83 	nvkm_wr32(device, 0x122634, 0x0);
     84 	ret = nvkm_fuse_read(fuse, 0x3a8);
     85 	nvkm_wr32(device, 0x122634, 0x41);
     86 	return ret;
     87 }
     88 
     89 static const struct nvkm_volt_func
     90 gk104_volt_pwm = {
     91 	.oneinit = gf100_volt_oneinit,
     92 	.volt_get = gk104_volt_get,
     93 	.volt_set = gk104_volt_set,
     94 	.speedo_read = gk104_volt_speedo_read,
     95 }, gk104_volt_gpio = {
     96 	.oneinit = gf100_volt_oneinit,
     97 	.vid_get = nvkm_voltgpio_get,
     98 	.vid_set = nvkm_voltgpio_set,
     99 	.speedo_read = gk104_volt_speedo_read,
    100 };
    101 
    102 int
    103 gk104_volt_new(struct nvkm_device *device, int index, struct nvkm_volt **pvolt)
    104 {
    105 	const struct nvkm_volt_func *volt_func = &gk104_volt_gpio;
    106 	struct dcb_gpio_func gpio;
    107 	struct nvbios_volt bios;
    108 	struct gk104_volt *volt;
    109 	u8 ver, hdr, cnt, len;
    110 	const char *mode;
    111 
    112 	if (!nvbios_volt_parse(device->bios, &ver, &hdr, &cnt, &len, &bios))
    113 		return 0;
    114 
    115 	if (!nvkm_gpio_find(device->gpio, 0, DCB_GPIO_VID_PWM, 0xff, &gpio) &&
    116 	    bios.type == NVBIOS_VOLT_PWM) {
    117 		volt_func = &gk104_volt_pwm;
    118 	}
    119 
    120 	if (!(volt = kzalloc(sizeof(*volt), GFP_KERNEL)))
    121 		return -ENOMEM;
    122 	nvkm_volt_ctor(volt_func, device, index, &volt->base);
    123 	*pvolt = &volt->base;
    124 	volt->bios = bios;
    125 
    126 	/* now that we have a subdev, we can show an error if we found through
    127 	 * the voltage table that we were supposed to use the PWN mode but we
    128 	 * did not find the right GPIO for it.
    129 	 */
    130 	if (bios.type == NVBIOS_VOLT_PWM && volt_func != &gk104_volt_pwm) {
    131 		nvkm_error(&volt->base.subdev,
    132 			   "Type mismatch between the voltage table type and "
    133 			   "the GPIO table. Fallback to GPIO mode.\n");
    134 	}
    135 
    136 	if (volt_func == &gk104_volt_gpio) {
    137 		nvkm_voltgpio_init(&volt->base);
    138 		mode = "GPIO";
    139 	} else
    140 		mode = "PWM";
    141 
    142 	nvkm_debug(&volt->base.subdev, "Using %s mode\n", mode);
    143 
    144 	return 0;
    145 }
    146