base.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. struct nvkm_i2c_bus *bus;
  144. struct nvkm_i2c_aux *aux;
  145. u32 mask;
  146. list_for_each_entry(aux, &i2c->aux, head) {
  147. nvkm_i2c_aux_fini(aux);
  148. }
  149. list_for_each_entry(bus, &i2c->bus, head) {
  150. nvkm_i2c_bus_fini(bus);
  151. }
  152. if ((mask = (1 << i2c->func->aux) - 1), i2c->func->aux_stat) {
  153. i2c->func->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
  154. i2c->func->aux_stat(i2c, &mask, &mask, &mask, &mask);
  155. }
  156. list_for_each_entry(pad, &i2c->pad, head) {
  157. nvkm_i2c_pad_fini(pad);
  158. }
  159. return 0;
  160. }
  161. static int
  162. nvkm_i2c_preinit(struct nvkm_subdev *subdev)
  163. {
  164. struct nvkm_i2c *i2c = nvkm_i2c(subdev);
  165. struct nvkm_i2c_bus *bus;
  166. struct nvkm_i2c_pad *pad;
  167. /*
  168. * We init our i2c busses as early as possible, since they may be
  169. * needed by the vbios init scripts on some cards
  170. */
  171. list_for_each_entry(pad, &i2c->pad, head)
  172. nvkm_i2c_pad_init(pad);
  173. list_for_each_entry(bus, &i2c->bus, head)
  174. nvkm_i2c_bus_init(bus);
  175. return 0;
  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. struct nvkm_i2c_aux *aux;
  184. list_for_each_entry(pad, &i2c->pad, head) {
  185. nvkm_i2c_pad_init(pad);
  186. }
  187. list_for_each_entry(bus, &i2c->bus, head) {
  188. nvkm_i2c_bus_init(bus);
  189. }
  190. list_for_each_entry(aux, &i2c->aux, head) {
  191. nvkm_i2c_aux_init(aux);
  192. }
  193. return 0;
  194. }
  195. static void *
  196. nvkm_i2c_dtor(struct nvkm_subdev *subdev)
  197. {
  198. struct nvkm_i2c *i2c = nvkm_i2c(subdev);
  199. nvkm_event_fini(&i2c->event);
  200. while (!list_empty(&i2c->aux)) {
  201. struct nvkm_i2c_aux *aux =
  202. list_first_entry(&i2c->aux, typeof(*aux), head);
  203. nvkm_i2c_aux_del(&aux);
  204. }
  205. while (!list_empty(&i2c->bus)) {
  206. struct nvkm_i2c_bus *bus =
  207. list_first_entry(&i2c->bus, typeof(*bus), head);
  208. nvkm_i2c_bus_del(&bus);
  209. }
  210. while (!list_empty(&i2c->pad)) {
  211. struct nvkm_i2c_pad *pad =
  212. list_first_entry(&i2c->pad, typeof(*pad), head);
  213. nvkm_i2c_pad_del(&pad);
  214. }
  215. return i2c;
  216. }
  217. static const struct nvkm_subdev_func
  218. nvkm_i2c = {
  219. .dtor = nvkm_i2c_dtor,
  220. .preinit = nvkm_i2c_preinit,
  221. .init = nvkm_i2c_init,
  222. .fini = nvkm_i2c_fini,
  223. .intr = nvkm_i2c_intr,
  224. };
  225. static const struct nvkm_i2c_drv {
  226. u8 bios;
  227. u8 addr;
  228. int (*pad_new)(struct nvkm_i2c_bus *, int id, u8 addr,
  229. struct nvkm_i2c_pad **);
  230. }
  231. nvkm_i2c_drv[] = {
  232. { 0x0d, 0x39, anx9805_pad_new },
  233. { 0x0e, 0x3b, anx9805_pad_new },
  234. {}
  235. };
  236. int
  237. nvkm_i2c_new_(const struct nvkm_i2c_func *func, struct nvkm_device *device,
  238. int index, struct nvkm_i2c **pi2c)
  239. {
  240. struct nvkm_bios *bios = device->bios;
  241. struct nvkm_i2c *i2c;
  242. struct dcb_i2c_entry ccbE;
  243. struct dcb_output dcbE;
  244. u8 ver, hdr;
  245. int ret, i;
  246. if (!(i2c = *pi2c = kzalloc(sizeof(*i2c), GFP_KERNEL)))
  247. return -ENOMEM;
  248. nvkm_subdev_ctor(&nvkm_i2c, device, index, &i2c->subdev);
  249. i2c->func = func;
  250. INIT_LIST_HEAD(&i2c->pad);
  251. INIT_LIST_HEAD(&i2c->bus);
  252. INIT_LIST_HEAD(&i2c->aux);
  253. i = -1;
  254. while (!dcb_i2c_parse(bios, ++i, &ccbE)) {
  255. struct nvkm_i2c_pad *pad = NULL;
  256. struct nvkm_i2c_bus *bus = NULL;
  257. struct nvkm_i2c_aux *aux = NULL;
  258. nvkm_debug(&i2c->subdev, "ccb %02x: type %02x drive %02x "
  259. "sense %02x share %02x auxch %02x\n", i, ccbE.type,
  260. ccbE.drive, ccbE.sense, ccbE.share, ccbE.auxch);
  261. if (ccbE.share != DCB_I2C_UNUSED) {
  262. const int id = NVKM_I2C_PAD_HYBRID(ccbE.share);
  263. if (!(pad = nvkm_i2c_pad_find(i2c, id)))
  264. ret = func->pad_s_new(i2c, id, &pad);
  265. else
  266. ret = 0;
  267. } else {
  268. ret = func->pad_x_new(i2c, NVKM_I2C_PAD_CCB(i), &pad);
  269. }
  270. if (ret) {
  271. nvkm_error(&i2c->subdev, "ccb %02x pad, %d\n", i, ret);
  272. nvkm_i2c_pad_del(&pad);
  273. continue;
  274. }
  275. if (pad->func->bus_new_0 && ccbE.type == DCB_I2C_NV04_BIT) {
  276. ret = pad->func->bus_new_0(pad, NVKM_I2C_BUS_CCB(i),
  277. ccbE.drive,
  278. ccbE.sense, &bus);
  279. } else
  280. if (pad->func->bus_new_4 &&
  281. ( ccbE.type == DCB_I2C_NV4E_BIT ||
  282. ccbE.type == DCB_I2C_NVIO_BIT ||
  283. (ccbE.type == DCB_I2C_PMGR &&
  284. ccbE.drive != DCB_I2C_UNUSED))) {
  285. ret = pad->func->bus_new_4(pad, NVKM_I2C_BUS_CCB(i),
  286. ccbE.drive, &bus);
  287. }
  288. if (ret) {
  289. nvkm_error(&i2c->subdev, "ccb %02x bus, %d\n", i, ret);
  290. nvkm_i2c_bus_del(&bus);
  291. }
  292. if (pad->func->aux_new_6 &&
  293. ( ccbE.type == DCB_I2C_NVIO_AUX ||
  294. (ccbE.type == DCB_I2C_PMGR &&
  295. ccbE.auxch != DCB_I2C_UNUSED))) {
  296. ret = pad->func->aux_new_6(pad, NVKM_I2C_BUS_CCB(i),
  297. ccbE.auxch, &aux);
  298. } else {
  299. ret = 0;
  300. }
  301. if (ret) {
  302. nvkm_error(&i2c->subdev, "ccb %02x aux, %d\n", i, ret);
  303. nvkm_i2c_aux_del(&aux);
  304. }
  305. if (ccbE.type != DCB_I2C_UNUSED && !bus && !aux) {
  306. nvkm_warn(&i2c->subdev, "ccb %02x was ignored\n", i);
  307. continue;
  308. }
  309. }
  310. i = -1;
  311. while (dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE)) {
  312. const struct nvkm_i2c_drv *drv = nvkm_i2c_drv;
  313. struct nvkm_i2c_bus *bus;
  314. struct nvkm_i2c_pad *pad;
  315. /* internal outputs handled by native i2c busses (above) */
  316. if (!dcbE.location)
  317. continue;
  318. /* we need an i2c bus to talk to the external encoder */
  319. bus = nvkm_i2c_bus_find(i2c, dcbE.i2c_index);
  320. if (!bus) {
  321. nvkm_debug(&i2c->subdev, "dcb %02x no bus\n", i);
  322. continue;
  323. }
  324. /* ... and a driver for it */
  325. while (drv->pad_new) {
  326. if (drv->bios == dcbE.extdev)
  327. break;
  328. drv++;
  329. }
  330. if (!drv->pad_new) {
  331. nvkm_debug(&i2c->subdev, "dcb %02x drv %02x unknown\n",
  332. i, dcbE.extdev);
  333. continue;
  334. }
  335. /* find/create an instance of the driver */
  336. pad = nvkm_i2c_pad_find(i2c, NVKM_I2C_PAD_EXT(dcbE.extdev));
  337. if (!pad) {
  338. const int id = NVKM_I2C_PAD_EXT(dcbE.extdev);
  339. ret = drv->pad_new(bus, id, drv->addr, &pad);
  340. if (ret) {
  341. nvkm_error(&i2c->subdev, "dcb %02x pad, %d\n",
  342. i, ret);
  343. nvkm_i2c_pad_del(&pad);
  344. continue;
  345. }
  346. }
  347. /* create any i2c bus / aux channel required by the output */
  348. if (pad->func->aux_new_6 && dcbE.type == DCB_OUTPUT_DP) {
  349. const int id = NVKM_I2C_AUX_EXT(dcbE.extdev);
  350. struct nvkm_i2c_aux *aux = NULL;
  351. ret = pad->func->aux_new_6(pad, id, 0, &aux);
  352. if (ret) {
  353. nvkm_error(&i2c->subdev, "dcb %02x aux, %d\n",
  354. i, ret);
  355. nvkm_i2c_aux_del(&aux);
  356. }
  357. } else
  358. if (pad->func->bus_new_4) {
  359. const int id = NVKM_I2C_BUS_EXT(dcbE.extdev);
  360. struct nvkm_i2c_bus *bus = NULL;
  361. ret = pad->func->bus_new_4(pad, id, 0, &bus);
  362. if (ret) {
  363. nvkm_error(&i2c->subdev, "dcb %02x bus, %d\n",
  364. i, ret);
  365. nvkm_i2c_bus_del(&bus);
  366. }
  367. }
  368. }
  369. return nvkm_event_init(&nvkm_i2c_intr_func, 4, i, &i2c->event);
  370. }