1 /*
2  * Copyright 2013 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 #include "priv.h"
25 #include "aux.h"
26 #include "bus.h"
27 #include "pad.h"
28 
29 #include <core/notify.h>
30 #include <core/option.h>
31 #include <subdev/bios.h>
32 #include <subdev/bios/dcb.h>
33 #include <subdev/bios/i2c.h>
34 
35 static struct nvkm_i2c_pad *
36 nvkm_i2c_pad_find(struct nvkm_i2c *i2c, int id)
37 {
38 	struct nvkm_i2c_pad *pad;
39 
40 	list_for_each_entry(pad, &i2c->pad, head) {
41 		if (pad->id == id)
42 			return pad;
43 	}
44 
45 	return NULL;
46 }
47 
48 struct nvkm_i2c_bus *
49 nvkm_i2c_bus_find(struct nvkm_i2c *i2c, int id)
50 {
51 	struct nvkm_bios *bios = i2c->subdev.device->bios;
52 	struct nvkm_i2c_bus *bus;
53 
54 	if (id == NVKM_I2C_BUS_PRI || id == NVKM_I2C_BUS_SEC) {
55 		u8  ver, hdr, cnt, len;
56 		u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
57 		if (i2c && ver >= 0x30) {
58 			u8 auxidx = nvbios_rd08(bios, i2c + 4);
59 			if (id == NVKM_I2C_BUS_PRI)
60 				id = NVKM_I2C_BUS_CCB((auxidx & 0x0f) >> 0);
61 			else
62 				id = NVKM_I2C_BUS_CCB((auxidx & 0xf0) >> 4);
63 		} else {
64 			id = NVKM_I2C_BUS_CCB(2);
65 		}
66 	}
67 
68 	list_for_each_entry(bus, &i2c->bus, head) {
69 		if (bus->id == id)
70 			return bus;
71 	}
72 
73 	return NULL;
74 }
75 
76 struct nvkm_i2c_aux *
77 nvkm_i2c_aux_find(struct nvkm_i2c *i2c, int id)
78 {
79 	struct nvkm_i2c_aux *aux;
80 
81 	list_for_each_entry(aux, &i2c->aux, head) {
82 		if (aux->id == id)
83 			return aux;
84 	}
85 
86 	return NULL;
87 }
88 
89 static void
90 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int id)
91 {
92 	struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
93 	struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
94 	if (aux)
95 		i2c->func->aux_mask(i2c, type, aux->intr, 0);
96 }
97 
98 static void
99 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int id)
100 {
101 	struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
102 	struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
103 	if (aux)
104 		i2c->func->aux_mask(i2c, type, aux->intr, aux->intr);
105 }
106 
107 static int
108 nvkm_i2c_intr_ctor(struct nvkm_object *object, void *data, u32 size,
109 		   struct nvkm_notify *notify)
110 {
111 	struct nvkm_i2c_ntfy_req *req = data;
112 	if (!WARN_ON(size != sizeof(*req))) {
113 		notify->size  = sizeof(struct nvkm_i2c_ntfy_rep);
114 		notify->types = req->mask;
115 		notify->index = req->port;
116 		return 0;
117 	}
118 	return -EINVAL;
119 }
120 
121 static const struct nvkm_event_func
122 nvkm_i2c_intr_func = {
123 	.ctor = nvkm_i2c_intr_ctor,
124 	.init = nvkm_i2c_intr_init,
125 	.fini = nvkm_i2c_intr_fini,
126 };
127 
128 static void
129 nvkm_i2c_intr(struct nvkm_subdev *subdev)
130 {
131 	struct nvkm_i2c *i2c = nvkm_i2c(subdev);
132 	struct nvkm_i2c_aux *aux;
133 	u32 hi, lo, rq, tx;
134 
135 	if (!i2c->func->aux_stat)
136 		return;
137 
138 	i2c->func->aux_stat(i2c, &hi, &lo, &rq, &tx);
139 	if (!hi && !lo && !rq && !tx)
140 		return;
141 
142 	list_for_each_entry(aux, &i2c->aux, head) {
143 		u32 mask = 0;
144 		if (hi & aux->intr) mask |= NVKM_I2C_PLUG;
145 		if (lo & aux->intr) mask |= NVKM_I2C_UNPLUG;
146 		if (rq & aux->intr) mask |= NVKM_I2C_IRQ;
147 		if (tx & aux->intr) mask |= NVKM_I2C_DONE;
148 		if (mask) {
149 			struct nvkm_i2c_ntfy_rep rep = {
150 				.mask = mask,
151 			};
152 			nvkm_event_send(&i2c->event, rep.mask, aux->id,
153 					&rep, sizeof(rep));
154 		}
155 	}
156 }
157 
158 static int
159 nvkm_i2c_fini(struct nvkm_subdev *subdev, bool suspend)
160 {
161 	struct nvkm_i2c *i2c = nvkm_i2c(subdev);
162 	struct nvkm_i2c_pad *pad;
163 	u32 mask;
164 
165 	if ((mask = (1 << i2c->func->aux) - 1), i2c->func->aux_stat) {
166 		i2c->func->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
167 		i2c->func->aux_stat(i2c, &mask, &mask, &mask, &mask);
168 	}
169 
170 	list_for_each_entry(pad, &i2c->pad, head) {
171 		nvkm_i2c_pad_fini(pad);
172 	}
173 
174 	return 0;
175 }
176 
177 static int
178 nvkm_i2c_init(struct nvkm_subdev *subdev)
179 {
180 	struct nvkm_i2c *i2c = nvkm_i2c(subdev);
181 	struct nvkm_i2c_bus *bus;
182 	struct nvkm_i2c_pad *pad;
183 
184 	list_for_each_entry(pad, &i2c->pad, head) {
185 		nvkm_i2c_pad_init(pad);
186 	}
187 
188 	list_for_each_entry(bus, &i2c->bus, head) {
189 		nvkm_i2c_bus_init(bus);
190 	}
191 
192 	return 0;
193 }
194 
195 static void *
196 nvkm_i2c_dtor(struct nvkm_subdev *subdev)
197 {
198 	struct nvkm_i2c *i2c = nvkm_i2c(subdev);
199 
200 	nvkm_event_fini(&i2c->event);
201 
202 	while (!list_empty(&i2c->aux)) {
203 		struct nvkm_i2c_aux *aux =
204 			list_first_entry(&i2c->aux, typeof(*aux), head);
205 		nvkm_i2c_aux_del(&aux);
206 	}
207 
208 	while (!list_empty(&i2c->bus)) {
209 		struct nvkm_i2c_bus *bus =
210 			list_first_entry(&i2c->bus, typeof(*bus), head);
211 		nvkm_i2c_bus_del(&bus);
212 	}
213 
214 	while (!list_empty(&i2c->pad)) {
215 		struct nvkm_i2c_pad *pad =
216 			list_first_entry(&i2c->pad, typeof(*pad), head);
217 		nvkm_i2c_pad_del(&pad);
218 	}
219 
220 	return i2c;
221 }
222 
223 static const struct nvkm_subdev_func
224 nvkm_i2c = {
225 	.dtor = nvkm_i2c_dtor,
226 	.init = nvkm_i2c_init,
227 	.fini = nvkm_i2c_fini,
228 	.intr = nvkm_i2c_intr,
229 };
230 
231 static const struct nvkm_i2c_drv {
232 	u8 bios;
233 	u8 addr;
234 	int (*pad_new)(struct nvkm_i2c_bus *, int id, u8 addr,
235 		       struct nvkm_i2c_pad **);
236 }
237 nvkm_i2c_drv[] = {
238 	{ 0x0d, 0x39, anx9805_pad_new },
239 	{ 0x0e, 0x3b, anx9805_pad_new },
240 	{}
241 };
242 
243 int
244 nvkm_i2c_new_(const struct nvkm_i2c_func *func, struct nvkm_device *device,
245 	      int index, struct nvkm_i2c **pi2c)
246 {
247 	struct nvkm_bios *bios = device->bios;
248 	struct nvkm_i2c *i2c;
249 	struct dcb_i2c_entry ccbE;
250 	struct dcb_output dcbE;
251 	u8 ver, hdr;
252 	int ret, i;
253 
254 	if (!(i2c = *pi2c = kzalloc(sizeof(*i2c), GFP_KERNEL)))
255 		return -ENOMEM;
256 
257 	nvkm_subdev_ctor(&nvkm_i2c, device, index, &i2c->subdev);
258 	i2c->func = func;
259 	INIT_LIST_HEAD(&i2c->pad);
260 	INIT_LIST_HEAD(&i2c->bus);
261 	INIT_LIST_HEAD(&i2c->aux);
262 
263 	i = -1;
264 	while (!dcb_i2c_parse(bios, ++i, &ccbE)) {
265 		struct nvkm_i2c_pad *pad = NULL;
266 		struct nvkm_i2c_bus *bus = NULL;
267 		struct nvkm_i2c_aux *aux = NULL;
268 
269 		nvkm_debug(&i2c->subdev, "ccb %02x: type %02x drive %02x "
270 			   "sense %02x share %02x auxch %02x\n", i, ccbE.type,
271 			   ccbE.drive, ccbE.sense, ccbE.share, ccbE.auxch);
272 
273 		if (ccbE.share != DCB_I2C_UNUSED) {
274 			const int id = NVKM_I2C_PAD_HYBRID(ccbE.share);
275 			if (!(pad = nvkm_i2c_pad_find(i2c, id)))
276 				ret = func->pad_s_new(i2c, id, &pad);
277 			else
278 				ret = 0;
279 		} else {
280 			ret = func->pad_x_new(i2c, NVKM_I2C_PAD_CCB(i), &pad);
281 		}
282 
283 		if (ret) {
284 			nvkm_error(&i2c->subdev, "ccb %02x pad, %d\n", i, ret);
285 			nvkm_i2c_pad_del(&pad);
286 			continue;
287 		}
288 
289 		if (pad->func->bus_new_0 && ccbE.type == DCB_I2C_NV04_BIT) {
290 			ret = pad->func->bus_new_0(pad, NVKM_I2C_BUS_CCB(i),
291 						   ccbE.drive,
292 						   ccbE.sense, &bus);
293 		} else
294 		if (pad->func->bus_new_4 &&
295 		    ( ccbE.type == DCB_I2C_NV4E_BIT ||
296 		      ccbE.type == DCB_I2C_NVIO_BIT ||
297 		     (ccbE.type == DCB_I2C_PMGR &&
298 		      ccbE.drive != DCB_I2C_UNUSED))) {
299 			ret = pad->func->bus_new_4(pad, NVKM_I2C_BUS_CCB(i),
300 						   ccbE.drive, &bus);
301 		}
302 
303 		if (ret) {
304 			nvkm_error(&i2c->subdev, "ccb %02x bus, %d\n", i, ret);
305 			nvkm_i2c_bus_del(&bus);
306 		}
307 
308 		if (pad->func->aux_new_6 &&
309 		    ( ccbE.type == DCB_I2C_NVIO_AUX ||
310 		     (ccbE.type == DCB_I2C_PMGR &&
311 		      ccbE.auxch != DCB_I2C_UNUSED))) {
312 			ret = pad->func->aux_new_6(pad, NVKM_I2C_BUS_CCB(i),
313 						   ccbE.auxch, &aux);
314 		} else {
315 			ret = 0;
316 		}
317 
318 		if (ret) {
319 			nvkm_error(&i2c->subdev, "ccb %02x aux, %d\n", i, ret);
320 			nvkm_i2c_aux_del(&aux);
321 		}
322 
323 		if (ccbE.type != DCB_I2C_UNUSED && !bus && !aux) {
324 			nvkm_warn(&i2c->subdev, "ccb %02x was ignored\n", i);
325 			continue;
326 		}
327 	}
328 
329 	i = -1;
330 	while (dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE)) {
331 		const struct nvkm_i2c_drv *drv = nvkm_i2c_drv;
332 		struct nvkm_i2c_bus *bus;
333 		struct nvkm_i2c_pad *pad;
334 
335 		/* internal outputs handled by native i2c busses (above) */
336 		if (!dcbE.location)
337 			continue;
338 
339 		/* we need an i2c bus to talk to the external encoder */
340 		bus = nvkm_i2c_bus_find(i2c, dcbE.i2c_index);
341 		if (!bus) {
342 			nvkm_debug(&i2c->subdev, "dcb %02x no bus\n", i);
343 			continue;
344 		}
345 
346 		/* ... and a driver for it */
347 		while (drv->pad_new) {
348 			if (drv->bios == dcbE.extdev)
349 				break;
350 			drv++;
351 		}
352 
353 		if (!drv->pad_new) {
354 			nvkm_debug(&i2c->subdev, "dcb %02x drv %02x unknown\n",
355 				   i, dcbE.extdev);
356 			continue;
357 		}
358 
359 		/* find/create an instance of the driver */
360 		pad = nvkm_i2c_pad_find(i2c, NVKM_I2C_PAD_EXT(dcbE.extdev));
361 		if (!pad) {
362 			const int id = NVKM_I2C_PAD_EXT(dcbE.extdev);
363 			ret = drv->pad_new(bus, id, drv->addr, &pad);
364 			if (ret) {
365 				nvkm_error(&i2c->subdev, "dcb %02x pad, %d\n",
366 					   i, ret);
367 				nvkm_i2c_pad_del(&pad);
368 				continue;
369 			}
370 		}
371 
372 		/* create any i2c bus / aux channel required by the output */
373 		if (pad->func->aux_new_6 && dcbE.type == DCB_OUTPUT_DP) {
374 			const int id = NVKM_I2C_AUX_EXT(dcbE.extdev);
375 			struct nvkm_i2c_aux *aux = NULL;
376 			ret = pad->func->aux_new_6(pad, id, 0, &aux);
377 			if (ret) {
378 				nvkm_error(&i2c->subdev, "dcb %02x aux, %d\n",
379 					   i, ret);
380 				nvkm_i2c_aux_del(&aux);
381 			}
382 		} else
383 		if (pad->func->bus_new_4) {
384 			const int id = NVKM_I2C_BUS_EXT(dcbE.extdev);
385 			struct nvkm_i2c_bus *bus = NULL;
386 			ret = pad->func->bus_new_4(pad, id, 0, &bus);
387 			if (ret) {
388 				nvkm_error(&i2c->subdev, "dcb %02x bus, %d\n",
389 					   i, ret);
390 				nvkm_i2c_bus_del(&bus);
391 			}
392 		}
393 	}
394 
395 	return nvkm_event_init(&nvkm_i2c_intr_func, 4, i, &i2c->event);
396 }
397