base.c 6.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright 2011 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */

25
26
#include <subdev/bios.h>
#include <subdev/bios/gpio.h>
27

28
29
#include "priv.h"

30
31
32
static int
nouveau_gpio_drive(struct nouveau_gpio *gpio,
		   int idx, int line, int dir, int out)
33
{
34
35
	const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
	return impl->drive ? impl->drive(gpio, line, dir, out) : -ENODEV;
36
37
}

38
39
static int
nouveau_gpio_sense(struct nouveau_gpio *gpio, int idx, int line)
40
{
41
42
	const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
	return impl->sense ? impl->sense(gpio, line) : -ENODEV;
43
44
}

45
46
47
static int
nouveau_gpio_find(struct nouveau_gpio *gpio, int idx, u8 tag, u8 line,
		  struct dcb_gpio_func *func)
48
{
49
50
51
52
	struct nouveau_bios *bios = nouveau_bios(gpio);
	u8  ver, len;
	u16 data;

53
	if (line == 0xff && tag == 0xff)
54
55
		return -EINVAL;

56
57
	data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func);
	if (data)
58
		return 0;
59
60

	/* Apple iMac G4 NV18 */
61
62
63
	if (nv_device_match(nv_object(gpio), 0x0189, 0x10de, 0x0010)) {
		if (tag == DCB_GPIO_TVDAC0) {
			*func = (struct dcb_gpio_func) {
64
65
66
67
68
69
70
71
72
				.func = DCB_GPIO_TVDAC0,
				.line = 4,
				.log[0] = 0,
				.log[1] = 1,
			};
			return 0;
		}
	}

73
	return -ENOENT;
74
75
}

76
77
static int
nouveau_gpio_set(struct nouveau_gpio *gpio, int idx, u8 tag, u8 line, int state)
78
{
79
	struct dcb_gpio_func func;
80
81
	int ret;

82
	ret = nouveau_gpio_find(gpio, idx, tag, line, &func);
83
	if (ret == 0) {
84
85
86
		int dir = !!(func.log[state] & 0x02);
		int out = !!(func.log[state] & 0x01);
		ret = nouveau_gpio_drive(gpio, idx, func.line, dir, out);
87
88
89
90
91
	}

	return ret;
}

92
93
static int
nouveau_gpio_get(struct nouveau_gpio *gpio, int idx, u8 tag, u8 line)
94
{
95
	struct dcb_gpio_func func;
96
97
	int ret;

98
	ret = nouveau_gpio_find(gpio, idx, tag, line, &func);
99
	if (ret == 0) {
100
		ret = nouveau_gpio_sense(gpio, idx, func.line);
101
		if (ret >= 0)
102
			ret = (ret == (func.log[1] & 1));
103
104
105
106
107
	}

	return ret;
}

108
109
110
111
112
static void
nouveau_gpio_intr_disable(struct nouveau_event *event, int type, int index)
{
	struct nouveau_gpio *gpio = nouveau_gpio(event->priv);
	const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
113
	impl->intr_mask(gpio, type, 1 << index, 0);
114
115
116
117
118
119
120
}

static void
nouveau_gpio_intr_enable(struct nouveau_event *event, int type, int index)
{
	struct nouveau_gpio *gpio = nouveau_gpio(event->priv);
	const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
121
	impl->intr_mask(gpio, type, 1 << index, 1 << index);
122
123
124
125
126
127
128
}

static void
nouveau_gpio_intr(struct nouveau_subdev *subdev)
{
	struct nouveau_gpio *gpio = nouveau_gpio(subdev);
	const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
129
	u32 hi, lo, e, i;
130
131
132

	impl->intr_stat(gpio, &hi, &lo);

133
134
135
136
137
138
	for (i = 0; e = 0, (hi | lo) && i < impl->lines; i++) {
		if (hi & (1 << i))
			e |= NVKM_GPIO_HI;
		if (lo & (1 << i))
			e |= NVKM_GPIO_LO;
		nouveau_event_trigger(gpio->events, e, i);
139
140
141
	}
}

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
int
_nouveau_gpio_fini(struct nouveau_object *object, bool suspend)
{
	const struct nouveau_gpio_impl *impl = (void *)object->oclass;
	struct nouveau_gpio *gpio = nouveau_gpio(object);
	u32 mask = (1 << impl->lines) - 1;

	impl->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0);
	impl->intr_stat(gpio, &mask, &mask);

	return nouveau_subdev_fini(&gpio->base, suspend);
}

static struct dmi_system_id gpio_reset_ids[] = {
	{
		.ident = "Apple Macbook 10,1",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"),
		}
	},
	{ }
};

int
_nouveau_gpio_init(struct nouveau_object *object)
{
	struct nouveau_gpio *gpio = nouveau_gpio(object);
	int ret;

	ret = nouveau_subdev_init(&gpio->base);
	if (ret)
		return ret;

	if (gpio->reset && dmi_check_system(gpio_reset_ids))
		gpio->reset(gpio, DCB_GPIO_UNUSED);

	return ret;
}

182
183
void
_nouveau_gpio_dtor(struct nouveau_object *object)
184
{
185
186
187
	struct nouveau_gpio *gpio = (void *)object;
	nouveau_event_destroy(&gpio->events);
	nouveau_subdev_destroy(&gpio->base);
188
189
190
}

int
191
192
nouveau_gpio_create_(struct nouveau_object *parent,
		     struct nouveau_object *engine,
193
		     struct nouveau_oclass *oclass,
194
		     int length, void **pobject)
195
{
196
	const struct nouveau_gpio_impl *impl = (void *)oclass;
197
198
	struct nouveau_gpio *gpio;
	int ret;
199

200
201
202
203
204
	ret = nouveau_subdev_create_(parent, engine, oclass, 0, "GPIO", "gpio",
				     length, pobject);
	gpio = *pobject;
	if (ret)
		return ret;
205

206
207
208
	gpio->find = nouveau_gpio_find;
	gpio->set  = nouveau_gpio_set;
	gpio->get  = nouveau_gpio_get;
209
	gpio->reset = impl->reset;
210

211
	ret = nouveau_event_create(2, impl->lines, &gpio->events);
212
213
214
	if (ret)
		return ret;

215
216
217
218
	gpio->events->priv = gpio;
	gpio->events->enable = nouveau_gpio_intr_enable;
	gpio->events->disable = nouveau_gpio_intr_disable;
	nv_subdev(gpio)->intr = nouveau_gpio_intr;
219
	return 0;
220
221
222
}

int
223
224
225
_nouveau_gpio_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
		   struct nouveau_oclass *oclass, void *data, u32 size,
		   struct nouveau_object **pobject)
226
{
227
228
229
230
231
232
233
234
235
	struct nouveau_gpio *gpio;
	int ret;

	ret = nouveau_gpio_create(parent, engine, oclass, &gpio);
	*pobject = nv_object(gpio);
	if (ret)
		return ret;

	return 0;
236
}