base.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #include <core/notify.h>
  29. #include <core/option.h>
  30. #include <subdev/bios.h>
  31. #include <subdev/bios/dcb.h>
  32. #include <subdev/bios/i2c.h>
  33. static struct nvkm_i2c_pad *
  34. nvkm_i2c_pad_find(struct nvkm_i2c *i2c, int id)
  35. {
  36. struct nvkm_i2c_pad *pad;
  37. list_for_each_entry(pad, &i2c->pad, head) {
  38. if (pad->id == id)
  39. return pad;
  40. }
  41. return NULL;
  42. }
  43. struct nvkm_i2c_bus *
  44. nvkm_i2c_bus_find(struct nvkm_i2c *i2c, int id)
  45. {
  46. struct nvkm_bios *bios = i2c->subdev.device->bios;
  47. struct nvkm_i2c_bus *bus;
  48. if (id == NVKM_I2C_BUS_PRI || id == NVKM_I2C_BUS_SEC) {
  49. u8 ver, hdr, cnt, len;
  50. u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
  51. if (i2c && ver >= 0x30) {
  52. u8 auxidx = nvbios_rd08(bios, i2c + 4);
  53. if (id == NVKM_I2C_BUS_PRI)
  54. id = NVKM_I2C_BUS_CCB((auxidx & 0x0f) >> 0);
  55. else
  56. id = NVKM_I2C_BUS_CCB((auxidx & 0xf0) >> 4);
  57. } else {
  58. id = NVKM_I2C_BUS_CCB(2);
  59. }
  60. }
  61. list_for_each_entry(bus, &i2c->bus, head) {
  62. if (bus->id == id)
  63. return bus;
  64. }
  65. return NULL;
  66. }
  67. struct nvkm_i2c_aux *
  68. nvkm_i2c_aux_find(struct nvkm_i2c *i2c, int id)
  69. {
  70. struct nvkm_i2c_aux *aux;
  71. list_for_each_entry(aux, &i2c->aux, head) {
  72. if (aux->id == id)
  73. return aux;
  74. }
  75. return NULL;
  76. }
  77. static void
  78. nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int id)
  79. {
  80. struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
  81. struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
  82. if (aux)
  83. i2c->func->aux_mask(i2c, type, aux->intr, 0);
  84. }
  85. static void
  86. nvkm_i2c_intr_init(struct nvkm_event *event, int type, int id)
  87. {
  88. struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
  89. struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
  90. if (aux)
  91. i2c->func->aux_mask(i2c, type, aux->intr, aux->intr);
  92. }
  93. static int
  94. nvkm_i2c_intr_ctor(struct nvkm_object *object, void *data, u32 size,
  95. struct nvkm_notify *notify)
  96. {
  97. struct nvkm_i2c_ntfy_req *req = data;
  98. if (!WARN_ON(size != sizeof(*req))) {
  99. notify->size = sizeof(struct nvkm_i2c_ntfy_rep);
  100. notify->types = req->mask;
  101. notify->index = req->port;
  102. return 0;
  103. }
  104. return -EINVAL;
  105. }
  106. static const struct nvkm_event_func
  107. nvkm_i2c_intr_func = {
  108. .ctor = nvkm_i2c_intr_ctor,
  109. .init = nvkm_i2c_intr_init,
  110. .fini = nvkm_i2c_intr_fini,
  111. };
  112. static void
  113. nvkm_i2c_intr(struct nvkm_subdev *subdev)
  114. {
  115. struct nvkm_i2c *i2c = nvkm_i2c(subdev);
  116. struct nvkm_i2c_aux *aux;
  117. u32 hi, lo, rq, tx;
  118. if (!i2c->func->aux_stat)
  119. return;
  120. i2c->func->aux_stat(i2c, &hi, &lo, &rq, &tx);
  121. if (!hi && !lo && !rq && !tx)
  122. return;
  123. list_for_each_entry(aux, &i2c->aux, head) {
  124. u32 mask = 0;
  125. if (hi & aux->intr) mask |= NVKM_I2C_PLUG;
  126. if (lo & aux->intr) mask |= NVKM_I2C_UNPLUG;
  127. if (rq & aux->intr) mask |= NVKM_I2C_IRQ;
  128. if (tx & aux->intr) mask |= NVKM_I2C_DONE;
  129. if (mask) {
  130. struct nvkm_i2c_ntfy_rep rep = {
  131. .mask = mask,
  132. };
  133. nvkm_event_send(&i2c->event, rep.mask, aux->id,
  134. &rep, sizeof(rep));
  135. }
  136. }
  137. }
  138. static int
  139. nvkm_i2c_fini(struct nvkm_subdev *subdev, bool suspend)
  140. {
  141. struct nvkm_i2c *i2c = nvkm_i2c(subdev);
  142. struct nvkm_i2c_pad *pad;
  143. u32 mask;
  144. if ((mask = (1 << i2c->func->aux) - 1), i2c->func->aux_stat) {
  145. i2c->func->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
  146. i2c->func->aux_stat(i2c, &mask, &mask, &mask, &mask);
  147. }
  148. list_for_each_entry(pad, &i2c->pad, head) {
  149. nvkm_i2c_pad_fini(pad);
  150. }
  151. return 0;
  152. }
  153. static int
  154. nvkm_i2c_init(struct nvkm_subdev *subdev)
  155. {
  156. struct nvkm_i2c *i2c = nvkm_i2c(subdev);
  157. struct nvkm_i2c_bus *bus;
  158. struct nvkm_i2c_pad *pad;
  159. list_for_each_entry(pad, &i2c->pad, head) {
  160. nvkm_i2c_pad_init(pad);
  161. }
  162. list_for_each_entry(bus, &i2c->bus, head) {
  163. nvkm_i2c_bus_init(bus);
  164. }
  165. return 0;
  166. }
  167. static void *
  168. nvkm_i2c_dtor(struct nvkm_subdev *subdev)
  169. {
  170. struct nvkm_i2c *i2c = nvkm_i2c(subdev);
  171. nvkm_event_fini(&i2c->event);
  172. while (!list_empty(&i2c->aux)) {
  173. struct nvkm_i2c_aux *aux =
  174. list_first_entry(&i2c->aux, typeof(*aux), head);
  175. nvkm_i2c_aux_del(&aux);
  176. }
  177. while (!list_empty(&i2c->bus)) {
  178. struct nvkm_i2c_bus *bus =
  179. list_first_entry(&i2c->bus, typeof(*bus), head);
  180. nvkm_i2c_bus_del(&bus);
  181. }
  182. while (!list_empty(&i2c->pad)) {
  183. struct nvkm_i2c_pad *pad =
  184. list_first_entry(&i2c->pad, typeof(*pad), head);
  185. nvkm_i2c_pad_del(&pad);
  186. }
  187. return i2c;
  188. }
  189. static const struct nvkm_subdev_func
  190. nvkm_i2c = {
  191. .dtor = nvkm_i2c_dtor,
  192. .init = nvkm_i2c_init,
  193. .fini = nvkm_i2c_fini,
  194. .intr = nvkm_i2c_intr,
  195. };
  196. static const struct nvkm_i2c_drv {
  197. u8 bios;
  198. u8 addr;
  199. int (*pad_new)(struct nvkm_i2c_bus *, int id, u8 addr,
  200. struct nvkm_i2c_pad **);
  201. }
  202. nvkm_i2c_drv[] = {
  203. { 0x0d, 0x39, anx9805_pad_new },
  204. { 0x0e, 0x3b, anx9805_pad_new },
  205. {}
  206. };
  207. int
  208. nvkm_i2c_new_(const struct nvkm_i2c_func *func, struct nvkm_device *device,
  209. int index, struct nvkm_i2c **pi2c)
  210. {
  211. struct nvkm_bios *bios = device->bios;
  212. struct nvkm_i2c *i2c;
  213. struct dcb_i2c_entry ccbE;
  214. struct dcb_output dcbE;
  215. u8 ver, hdr;
  216. int ret, i;
  217. if (!(i2c = *pi2c = kzalloc(sizeof(*i2c), GFP_KERNEL)))
  218. return -ENOMEM;
  219. nvkm_subdev_ctor(&nvkm_i2c, device, index, &i2c->subdev);
  220. i2c->func = func;
  221. INIT_LIST_HEAD(&i2c->pad);
  222. INIT_LIST_HEAD(&i2c->bus);
  223. INIT_LIST_HEAD(&i2c->aux);
  224. i = -1;
  225. while (!dcb_i2c_parse(bios, ++i, &ccbE)) {
  226. struct nvkm_i2c_pad *pad = NULL;
  227. struct nvkm_i2c_bus *bus = NULL;
  228. struct nvkm_i2c_aux *aux = NULL;
  229. nvkm_debug(&i2c->subdev, "ccb %02x: type %02x drive %02x "
  230. "sense %02x share %02x auxch %02x\n", i, ccbE.type,
  231. ccbE.drive, ccbE.sense, ccbE.share, ccbE.auxch);
  232. if (ccbE.share != DCB_I2C_UNUSED) {
  233. const int id = NVKM_I2C_PAD_HYBRID(ccbE.share);
  234. if (!(pad = nvkm_i2c_pad_find(i2c, id)))
  235. ret = func->pad_s_new(i2c, id, &pad);
  236. else
  237. ret = 0;
  238. } else {
  239. ret = func->pad_x_new(i2c, NVKM_I2C_PAD_CCB(i), &pad);
  240. }
  241. if (ret) {
  242. nvkm_error(&i2c->subdev, "ccb %02x pad, %d\n", i, ret);
  243. nvkm_i2c_pad_del(&pad);
  244. continue;
  245. }
  246. if (pad->func->bus_new_0 && ccbE.type == DCB_I2C_NV04_BIT) {
  247. ret = pad->func->bus_new_0(pad, NVKM_I2C_BUS_CCB(i),
  248. ccbE.drive,
  249. ccbE.sense, &bus);
  250. } else
  251. if (pad->func->bus_new_4 &&
  252. ( ccbE.type == DCB_I2C_NV4E_BIT ||
  253. ccbE.type == DCB_I2C_NVIO_BIT ||
  254. (ccbE.type == DCB_I2C_PMGR &&
  255. ccbE.drive != DCB_I2C_UNUSED))) {
  256. ret = pad->func->bus_new_4(pad, NVKM_I2C_BUS_CCB(i),
  257. ccbE.drive, &bus);
  258. }
  259. if (ret) {
  260. nvkm_error(&i2c->subdev, "ccb %02x bus, %d\n", i, ret);
  261. nvkm_i2c_bus_del(&bus);
  262. }
  263. if (pad->func->aux_new_6 &&
  264. ( ccbE.type == DCB_I2C_NVIO_AUX ||
  265. (ccbE.type == DCB_I2C_PMGR &&
  266. ccbE.auxch != DCB_I2C_UNUSED))) {
  267. ret = pad->func->aux_new_6(pad, NVKM_I2C_BUS_CCB(i),
  268. ccbE.auxch, &aux);
  269. } else {
  270. ret = 0;
  271. }
  272. if (ret) {
  273. nvkm_error(&i2c->subdev, "ccb %02x aux, %d\n", i, ret);
  274. nvkm_i2c_aux_del(&aux);
  275. }
  276. if (ccbE.type != DCB_I2C_UNUSED && !bus && !aux) {
  277. nvkm_warn(&i2c->subdev, "ccb %02x was ignored\n", i);
  278. continue;
  279. }
  280. }
  281. i = -1;
  282. while (dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE)) {
  283. const struct nvkm_i2c_drv *drv = nvkm_i2c_drv;
  284. struct nvkm_i2c_bus *bus;
  285. struct nvkm_i2c_pad *pad;
  286. /* internal outputs handled by native i2c busses (above) */
  287. if (!dcbE.location)
  288. continue;
  289. /* we need an i2c bus to talk to the external encoder */
  290. bus = nvkm_i2c_bus_find(i2c, dcbE.i2c_index);
  291. if (!bus) {
  292. nvkm_debug(&i2c->subdev, "dcb %02x no bus\n", i);
  293. continue;
  294. }
  295. /* ... and a driver for it */
  296. while (drv->pad_new) {
  297. if (drv->bios == dcbE.extdev)
  298. break;
  299. drv++;
  300. }
  301. if (!drv->pad_new) {
  302. nvkm_debug(&i2c->subdev, "dcb %02x drv %02x unknown\n",
  303. i, dcbE.extdev);
  304. continue;
  305. }
  306. /* find/create an instance of the driver */
  307. pad = nvkm_i2c_pad_find(i2c, NVKM_I2C_PAD_EXT(dcbE.extdev));
  308. if (!pad) {
  309. const int id = NVKM_I2C_PAD_EXT(dcbE.extdev);
  310. ret = drv->pad_new(bus, id, drv->addr, &pad);
  311. if (ret) {
  312. nvkm_error(&i2c->subdev, "dcb %02x pad, %d\n",
  313. i, ret);
  314. nvkm_i2c_pad_del(&pad);
  315. continue;
  316. }
  317. }
  318. /* create any i2c bus / aux channel required by the output */
  319. if (pad->func->aux_new_6 && dcbE.type == DCB_OUTPUT_DP) {
  320. const int id = NVKM_I2C_AUX_EXT(dcbE.extdev);
  321. struct nvkm_i2c_aux *aux = NULL;
  322. ret = pad->func->aux_new_6(pad, id, 0, &aux);
  323. if (ret) {
  324. nvkm_error(&i2c->subdev, "dcb %02x aux, %d\n",
  325. i, ret);
  326. nvkm_i2c_aux_del(&aux);
  327. }
  328. } else
  329. if (pad->func->bus_new_4) {
  330. const int id = NVKM_I2C_BUS_EXT(dcbE.extdev);
  331. struct nvkm_i2c_bus *bus = NULL;
  332. ret = pad->func->bus_new_4(pad, id, 0, &bus);
  333. if (ret) {
  334. nvkm_error(&i2c->subdev, "dcb %02x bus, %d\n",
  335. i, ret);
  336. nvkm_i2c_bus_del(&bus);
  337. }
  338. }
  339. }
  340. return nvkm_event_init(&nvkm_i2c_intr_func, 4, i, &i2c->event);
  341. }