1/*	$NetBSD: nouveau_nvkm_subdev_gpio_gf119.c,v 1.3 2021/12/18 23:45:39 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 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_gpio_gf119.c,v 1.3 2021/12/18 23:45:39 riastradh Exp $");
28
29#include "priv.h"
30
31void
32gf119_gpio_reset(struct nvkm_gpio *gpio, u8 match)
33{
34	struct nvkm_device *device = gpio->subdev.device;
35	struct nvkm_bios *bios = device->bios;
36	u8 ver, len;
37	u16 entry;
38	int ent = -1;
39
40	while ((entry = dcb_gpio_entry(bios, 0, ++ent, &ver, &len))) {
41		u32 data = nvbios_rd32(bios, entry);
42		u8  line =   (data & 0x0000003f);
43		u8  defs = !!(data & 0x00000080);
44		u8  func =   (data & 0x0000ff00) >> 8;
45		u8  unk0 =   (data & 0x00ff0000) >> 16;
46		u8  unk1 =   (data & 0x1f000000) >> 24;
47
48		if ( func  == DCB_GPIO_UNUSED ||
49		    (match != DCB_GPIO_UNUSED && match != func))
50			continue;
51
52		nvkm_gpio_set(gpio, 0, func, line, defs);
53
54		nvkm_mask(device, 0x00d610 + (line * 4), 0xff, unk0);
55		if (unk1--)
56			nvkm_mask(device, 0x00d740 + (unk1 * 4), 0xff, line);
57	}
58}
59
60int
61gf119_gpio_drive(struct nvkm_gpio *gpio, int line, int dir, int out)
62{
63	struct nvkm_device *device = gpio->subdev.device;
64	u32 data = ((dir ^ 1) << 13) | (out << 12);
65	nvkm_mask(device, 0x00d610 + (line * 4), 0x00003000, data);
66	nvkm_mask(device, 0x00d604, 0x00000001, 0x00000001); /* update? */
67	return 0;
68}
69
70int
71gf119_gpio_sense(struct nvkm_gpio *gpio, int line)
72{
73	struct nvkm_device *device = gpio->subdev.device;
74	return !!(nvkm_rd32(device, 0x00d610 + (line * 4)) & 0x00004000);
75}
76
77static const struct nvkm_gpio_func
78gf119_gpio = {
79	.lines = 32,
80	.intr_stat = g94_gpio_intr_stat,
81	.intr_mask = g94_gpio_intr_mask,
82	.drive = gf119_gpio_drive,
83	.sense = gf119_gpio_sense,
84	.reset = gf119_gpio_reset,
85};
86
87int
88gf119_gpio_new(struct nvkm_device *device, int index, struct nvkm_gpio **pgpio)
89{
90	return nvkm_gpio_new_(&gf119_gpio, device, index, pgpio);
91}
92