xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/volt/nouveau_nvkm_subdev_volt_gpio.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_volt_gpio.c,v 1.4 2021/12/18 23:45:42 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2013 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  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_volt_gpio.c,v 1.4 2021/12/18 23:45:42 riastradh Exp $");
28 
29 #include <subdev/volt.h>
30 #include <subdev/bios.h>
31 #include <subdev/bios/gpio.h>
32 #include <subdev/gpio.h>
33 #include "priv.h"
34 
35 #include "priv.h"
36 
37 static const u8 tags[] = {
38 	DCB_GPIO_VID0, DCB_GPIO_VID1, DCB_GPIO_VID2, DCB_GPIO_VID3,
39 	DCB_GPIO_VID4, DCB_GPIO_VID5, DCB_GPIO_VID6, DCB_GPIO_VID7,
40 };
41 
42 int
nvkm_voltgpio_get(struct nvkm_volt * volt)43 nvkm_voltgpio_get(struct nvkm_volt *volt)
44 {
45 	struct nvkm_gpio *gpio = volt->subdev.device->gpio;
46 	u8 vid = 0;
47 	int i;
48 
49 	for (i = 0; i < ARRAY_SIZE(tags); i++) {
50 		if (volt->vid_mask & (1 << i)) {
51 			int ret = nvkm_gpio_get(gpio, 0, tags[i], 0xff);
52 			if (ret < 0)
53 				return ret;
54 			vid |= ret << i;
55 		}
56 	}
57 
58 	return vid;
59 }
60 
61 int
nvkm_voltgpio_set(struct nvkm_volt * volt,u8 vid)62 nvkm_voltgpio_set(struct nvkm_volt *volt, u8 vid)
63 {
64 	struct nvkm_gpio *gpio = volt->subdev.device->gpio;
65 	int i;
66 
67 	for (i = 0; i < ARRAY_SIZE(tags); i++, vid >>= 1) {
68 		if (volt->vid_mask & (1 << i)) {
69 			int ret = nvkm_gpio_set(gpio, 0, tags[i], 0xff, vid & 1);
70 			if (ret < 0)
71 				return ret;
72 		}
73 	}
74 
75 	return 0;
76 }
77 
78 int
nvkm_voltgpio_init(struct nvkm_volt * volt)79 nvkm_voltgpio_init(struct nvkm_volt *volt)
80 {
81 	struct nvkm_subdev *subdev = &volt->subdev;
82 	struct nvkm_gpio *gpio = subdev->device->gpio;
83 	struct dcb_gpio_func func;
84 	int i;
85 
86 	/* check we have gpio function info for each vid bit.  on some
87 	 * boards (ie. nvs295) the vid mask has more bits than there
88 	 * are valid gpio functions... from traces, nvidia appear to
89 	 * just touch the existing ones, so let's mask off the invalid
90 	 * bits and continue with life
91 	 */
92 	for (i = 0; i < ARRAY_SIZE(tags); i++) {
93 		if (volt->vid_mask & (1 << i)) {
94 			int ret = nvkm_gpio_find(gpio, 0, tags[i], 0xff, &func);
95 			if (ret) {
96 				if (ret != -ENOENT)
97 					return ret;
98 				nvkm_debug(subdev, "VID bit %d has no GPIO\n", i);
99 				volt->vid_mask &= ~(1 << i);
100 			}
101 		}
102 	}
103 
104 	return 0;
105 }
106