Home | History | Annotate | Line # | Download | only in volt
      1 /*	$NetBSD: nouveau_nvkm_subdev_volt_gk20a.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
      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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     22  * DEALINGS IN THE SOFTWARE.
     23  */
     24 #include <sys/cdefs.h>
     25 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_volt_gk20a.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $");
     26 
     27 #define gk20a_volt(p) container_of((p), struct gk20a_volt, base)
     28 #include "priv.h"
     29 
     30 #include <core/tegra.h>
     31 
     32 #include "gk20a.h"
     33 
     34 static const struct cvb_coef gk20a_cvb_coef[] = {
     35 	/* MHz,        c0,     c1,   c2,    c3,     c4,   c5 */
     36 	/*  72 */ { 1209886, -36468,  515,   417, -13123,  203},
     37 	/* 108 */ { 1130804, -27659,  296,   298, -10834,  221},
     38 	/* 180 */ { 1162871, -27110,  247,   238, -10681,  268},
     39 	/* 252 */ { 1220458, -28654,  247,   179, -10376,  298},
     40 	/* 324 */ { 1280953, -30204,  247,   119,  -9766,  304},
     41 	/* 396 */ { 1344547, -31777,  247,   119,  -8545,  292},
     42 	/* 468 */ { 1420168, -34227,  269,    60,  -7172,  256},
     43 	/* 540 */ { 1490757, -35955,  274,    60,  -5188,  197},
     44 	/* 612 */ { 1599112, -42583,  398,     0,  -1831,  119},
     45 	/* 648 */ { 1366986, -16459, -274,     0,  -3204,   72},
     46 	/* 684 */ { 1391884, -17078, -274,   -60,  -1526,   30},
     47 	/* 708 */ { 1415522, -17497, -274,   -60,   -458,    0},
     48 	/* 756 */ { 1464061, -18331, -274,  -119,   1831,  -72},
     49 	/* 804 */ { 1524225, -20064, -254,  -119,   4272, -155},
     50 	/* 852 */ { 1608418, -21643, -269,     0,    763,  -48},
     51 };
     52 
     53 /**
     54  * cvb_mv = ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0)
     55  */
     56 static inline int
     57 gk20a_volt_get_cvb_voltage(int speedo, int s_scale, const struct cvb_coef *coef)
     58 {
     59 	int mv;
     60 
     61 	mv = DIV_ROUND_CLOSEST(coef->c2 * speedo, s_scale);
     62 	mv = DIV_ROUND_CLOSEST((mv + coef->c1) * speedo, s_scale) + coef->c0;
     63 	return mv;
     64 }
     65 
     66 /**
     67  * cvb_t_mv =
     68  * ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0) +
     69  * ((c3 * speedo / s_scale + c4 + c5 * T / t_scale) * T / t_scale)
     70  */
     71 static inline int
     72 gk20a_volt_get_cvb_t_voltage(int speedo, int temp, int s_scale, int t_scale,
     73 			     const struct cvb_coef *coef)
     74 {
     75 	int cvb_mv, mv;
     76 
     77 	cvb_mv = gk20a_volt_get_cvb_voltage(speedo, s_scale, coef);
     78 
     79 	mv = DIV_ROUND_CLOSEST(coef->c3 * speedo, s_scale) + coef->c4 +
     80 		DIV_ROUND_CLOSEST(coef->c5 * temp, t_scale);
     81 	mv = DIV_ROUND_CLOSEST(mv * temp, t_scale) + cvb_mv;
     82 	return mv;
     83 }
     84 
     85 static int
     86 gk20a_volt_calc_voltage(const struct cvb_coef *coef, int speedo)
     87 {
     88 	static const int v_scale = 1000;
     89 	int mv;
     90 
     91 	mv = gk20a_volt_get_cvb_t_voltage(speedo, -10, 100, 10, coef);
     92 	mv = DIV_ROUND_UP(mv, v_scale);
     93 
     94 	return mv * 1000;
     95 }
     96 
     97 static int
     98 gk20a_volt_vid_get(struct nvkm_volt *base)
     99 {
    100 	struct gk20a_volt *volt = gk20a_volt(base);
    101 	int i, uv;
    102 
    103 	uv = regulator_get_voltage(volt->vdd);
    104 
    105 	for (i = 0; i < volt->base.vid_nr; i++)
    106 		if (volt->base.vid[i].uv >= uv)
    107 			return i;
    108 
    109 	return -EINVAL;
    110 }
    111 
    112 static int
    113 gk20a_volt_vid_set(struct nvkm_volt *base, u8 vid)
    114 {
    115 	struct gk20a_volt *volt = gk20a_volt(base);
    116 	struct nvkm_subdev *subdev = &volt->base.subdev;
    117 
    118 	nvkm_debug(subdev, "set voltage as %duv\n", volt->base.vid[vid].uv);
    119 	return regulator_set_voltage(volt->vdd, volt->base.vid[vid].uv, 1200000);
    120 }
    121 
    122 static int
    123 gk20a_volt_set_id(struct nvkm_volt *base, u8 id, int condition)
    124 {
    125 	struct gk20a_volt *volt = gk20a_volt(base);
    126 	struct nvkm_subdev *subdev = &volt->base.subdev;
    127 	int prev_uv = regulator_get_voltage(volt->vdd);
    128 	int target_uv = volt->base.vid[id].uv;
    129 	int ret;
    130 
    131 	nvkm_debug(subdev, "prev=%d, target=%d, condition=%d\n",
    132 		   prev_uv, target_uv, condition);
    133 	if (!condition ||
    134 		(condition < 0 && target_uv < prev_uv) ||
    135 		(condition > 0 && target_uv > prev_uv)) {
    136 		ret = gk20a_volt_vid_set(&volt->base, volt->base.vid[id].vid);
    137 	} else {
    138 		ret = 0;
    139 	}
    140 
    141 	return ret;
    142 }
    143 
    144 static const struct nvkm_volt_func
    145 gk20a_volt = {
    146 	.vid_get = gk20a_volt_vid_get,
    147 	.vid_set = gk20a_volt_vid_set,
    148 	.set_id = gk20a_volt_set_id,
    149 };
    150 
    151 int
    152 gk20a_volt_ctor(struct nvkm_device *device, int index,
    153 		const struct cvb_coef *coefs, int nb_coefs,
    154 		int vmin, struct gk20a_volt *volt)
    155 {
    156 	struct nvkm_device_tegra *tdev = device->func->tegra(device);
    157 	int i, uv;
    158 
    159 	nvkm_volt_ctor(&gk20a_volt, device, index, &volt->base);
    160 
    161 	uv = regulator_get_voltage(tdev->vdd);
    162 	nvkm_debug(&volt->base.subdev, "the default voltage is %duV\n", uv);
    163 
    164 	volt->vdd = tdev->vdd;
    165 
    166 	volt->base.vid_nr = nb_coefs;
    167 	for (i = 0; i < volt->base.vid_nr; i++) {
    168 		volt->base.vid[i].vid = i;
    169 		volt->base.vid[i].uv = max(
    170 			gk20a_volt_calc_voltage(&coefs[i], tdev->gpu_speedo),
    171 			vmin);
    172 		nvkm_debug(&volt->base.subdev, "%2d: vid=%d, uv=%d\n", i,
    173 			   volt->base.vid[i].vid, volt->base.vid[i].uv);
    174 	}
    175 
    176 	return 0;
    177 }
    178 
    179 int
    180 gk20a_volt_new(struct nvkm_device *device, int index, struct nvkm_volt **pvolt)
    181 {
    182 	struct gk20a_volt *volt;
    183 
    184 	volt = kzalloc(sizeof(*volt), GFP_KERNEL);
    185 	if (!volt)
    186 		return -ENOMEM;
    187 	*pvolt = &volt->base;
    188 
    189 	return gk20a_volt_ctor(device, index, gk20a_cvb_coef,
    190 			       ARRAY_SIZE(gk20a_cvb_coef), 0, volt);
    191 }
    192