1/*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26#include "priv.h"
27
28static int
29nv10_gpio_sense(struct nvkm_gpio *gpio, int line)
30{
31	struct nvkm_device *device = gpio->subdev.device;
32	if (line < 2) {
33		line = line * 16;
34		line = nvkm_rd32(device, 0x600818) >> line;
35		return !!(line & 0x0100);
36	} else
37	if (line < 10) {
38		line = (line - 2) * 4;
39		line = nvkm_rd32(device, 0x60081c) >> line;
40		return !!(line & 0x04);
41	} else
42	if (line < 14) {
43		line = (line - 10) * 4;
44		line = nvkm_rd32(device, 0x600850) >> line;
45		return !!(line & 0x04);
46	}
47
48	return -EINVAL;
49}
50
51static int
52nv10_gpio_drive(struct nvkm_gpio *gpio, int line, int dir, int out)
53{
54	struct nvkm_device *device = gpio->subdev.device;
55	u32 reg, mask, data;
56
57	if (line < 2) {
58		line = line * 16;
59		reg  = 0x600818;
60		mask = 0x00000011;
61		data = (dir << 4) | out;
62	} else
63	if (line < 10) {
64		line = (line - 2) * 4;
65		reg  = 0x60081c;
66		mask = 0x00000003;
67		data = (dir << 1) | out;
68	} else
69	if (line < 14) {
70		line = (line - 10) * 4;
71		reg  = 0x600850;
72		mask = 0x00000003;
73		data = (dir << 1) | out;
74	} else {
75		return -EINVAL;
76	}
77
78	nvkm_mask(device, reg, mask << line, data << line);
79	return 0;
80}
81
82static void
83nv10_gpio_intr_stat(struct nvkm_gpio *gpio, u32 *hi, u32 *lo)
84{
85	struct nvkm_device *device = gpio->subdev.device;
86	u32 intr = nvkm_rd32(device, 0x001104);
87	u32 stat = nvkm_rd32(device, 0x001144) & intr;
88	*lo = (stat & 0xffff0000) >> 16;
89	*hi = (stat & 0x0000ffff);
90	nvkm_wr32(device, 0x001104, intr);
91}
92
93static void
94nv10_gpio_intr_mask(struct nvkm_gpio *gpio, u32 type, u32 mask, u32 data)
95{
96	struct nvkm_device *device = gpio->subdev.device;
97	u32 inte = nvkm_rd32(device, 0x001144);
98	if (type & NVKM_GPIO_LO)
99		inte = (inte & ~(mask << 16)) | (data << 16);
100	if (type & NVKM_GPIO_HI)
101		inte = (inte & ~mask) | data;
102	nvkm_wr32(device, 0x001144, inte);
103}
104
105static const struct nvkm_gpio_func
106nv10_gpio = {
107	.lines = 16,
108	.intr_stat = nv10_gpio_intr_stat,
109	.intr_mask = nv10_gpio_intr_mask,
110	.drive = nv10_gpio_drive,
111	.sense = nv10_gpio_sense,
112};
113
114int
115nv10_gpio_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
116	      struct nvkm_gpio **pgpio)
117{
118	return nvkm_gpio_new_(&nv10_gpio, device, type, inst, pgpio);
119}
120