cobalt-driver.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cobalt driver initialization and card probing
  4. *
  5. * Derived from cx18-driver.c
  6. *
  7. * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
  8. * All rights reserved.
  9. */
  10. #include <linux/delay.h>
  11. #include <media/i2c/adv7604.h>
  12. #include <media/i2c/adv7842.h>
  13. #include <media/i2c/adv7511.h>
  14. #include <media/v4l2-event.h>
  15. #include <media/v4l2-ctrls.h>
  16. #include "cobalt-driver.h"
  17. #include "cobalt-irq.h"
  18. #include "cobalt-i2c.h"
  19. #include "cobalt-v4l2.h"
  20. #include "cobalt-flash.h"
  21. #include "cobalt-alsa.h"
  22. #include "cobalt-omnitek.h"
  23. /* add your revision and whatnot here */
  24. static const struct pci_device_id cobalt_pci_tbl[] = {
  25. {PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_COBALT,
  26. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  27. {0,}
  28. };
  29. MODULE_DEVICE_TABLE(pci, cobalt_pci_tbl);
  30. static atomic_t cobalt_instance = ATOMIC_INIT(0);
  31. int cobalt_debug;
  32. module_param_named(debug, cobalt_debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "Debug level. Default: 0\n");
  34. int cobalt_ignore_err;
  35. module_param_named(ignore_err, cobalt_ignore_err, int, 0644);
  36. MODULE_PARM_DESC(ignore_err,
  37. "If set then ignore missing i2c adapters/receivers. Default: 0\n");
  38. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com> & Morten Hestnes");
  39. MODULE_DESCRIPTION("cobalt driver");
  40. MODULE_LICENSE("GPL");
  41. static u8 edid[256] = {
  42. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  43. 0x50, 0x21, 0x32, 0x27, 0x00, 0x00, 0x00, 0x00,
  44. 0x22, 0x1a, 0x01, 0x03, 0x80, 0x30, 0x1b, 0x78,
  45. 0x0f, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
  46. 0x0f, 0x50, 0x54, 0x2f, 0xcf, 0x00, 0x31, 0x59,
  47. 0x45, 0x59, 0x61, 0x59, 0x81, 0x99, 0x01, 0x01,
  48. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x3a,
  49. 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  50. 0x46, 0x00, 0xe0, 0x0e, 0x11, 0x00, 0x00, 0x1e,
  51. 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
  52. 0x5e, 0x11, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
  53. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x63,
  54. 0x6f, 0x62, 0x61, 0x6c, 0x74, 0x0a, 0x20, 0x20,
  55. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x10,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9c,
  58. 0x02, 0x03, 0x1f, 0xf0, 0x4a, 0x90, 0x1f, 0x04,
  59. 0x13, 0x22, 0x21, 0x20, 0x02, 0x11, 0x01, 0x23,
  60. 0x09, 0x07, 0x07, 0x68, 0x03, 0x0c, 0x00, 0x10,
  61. 0x00, 0x00, 0x22, 0x0f, 0xe2, 0x00, 0xea, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa7,
  74. };
  75. static void cobalt_set_interrupt(struct cobalt *cobalt, bool enable)
  76. {
  77. if (enable) {
  78. unsigned irqs = COBALT_SYSSTAT_VI0_INT1_MSK |
  79. COBALT_SYSSTAT_VI1_INT1_MSK |
  80. COBALT_SYSSTAT_VI2_INT1_MSK |
  81. COBALT_SYSSTAT_VI3_INT1_MSK |
  82. COBALT_SYSSTAT_VI0_INT2_MSK |
  83. COBALT_SYSSTAT_VI1_INT2_MSK |
  84. COBALT_SYSSTAT_VI2_INT2_MSK |
  85. COBALT_SYSSTAT_VI3_INT2_MSK |
  86. COBALT_SYSSTAT_VI0_LOST_DATA_MSK |
  87. COBALT_SYSSTAT_VI1_LOST_DATA_MSK |
  88. COBALT_SYSSTAT_VI2_LOST_DATA_MSK |
  89. COBALT_SYSSTAT_VI3_LOST_DATA_MSK |
  90. COBALT_SYSSTAT_AUD_IN_LOST_DATA_MSK;
  91. if (cobalt->have_hsma_rx)
  92. irqs |= COBALT_SYSSTAT_VIHSMA_INT1_MSK |
  93. COBALT_SYSSTAT_VIHSMA_INT2_MSK |
  94. COBALT_SYSSTAT_VIHSMA_LOST_DATA_MSK;
  95. if (cobalt->have_hsma_tx)
  96. irqs |= COBALT_SYSSTAT_VOHSMA_INT1_MSK |
  97. COBALT_SYSSTAT_VOHSMA_LOST_DATA_MSK |
  98. COBALT_SYSSTAT_AUD_OUT_LOST_DATA_MSK;
  99. /* Clear any existing interrupts */
  100. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_EDGE, 0xffffffff);
  101. /* PIO Core interrupt mask register.
  102. Enable ADV7604 INT1 interrupts */
  103. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK, irqs);
  104. } else {
  105. /* Disable all ADV7604 interrupts */
  106. cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK, 0);
  107. }
  108. }
  109. static unsigned cobalt_get_sd_nr(struct v4l2_subdev *sd)
  110. {
  111. struct cobalt *cobalt = to_cobalt(sd->v4l2_dev);
  112. unsigned i;
  113. for (i = 0; i < COBALT_NUM_NODES; i++)
  114. if (sd == cobalt->streams[i].sd)
  115. return i;
  116. cobalt_err("Invalid adv7604 subdev pointer!\n");
  117. return 0;
  118. }
  119. static void cobalt_notify(struct v4l2_subdev *sd,
  120. unsigned int notification, void *arg)
  121. {
  122. struct cobalt *cobalt = to_cobalt(sd->v4l2_dev);
  123. unsigned sd_nr = cobalt_get_sd_nr(sd);
  124. struct cobalt_stream *s = &cobalt->streams[sd_nr];
  125. bool hotplug = arg ? *((int *)arg) : false;
  126. if (s->is_output)
  127. return;
  128. switch (notification) {
  129. case ADV76XX_HOTPLUG:
  130. cobalt_s_bit_sysctrl(cobalt,
  131. COBALT_SYS_CTRL_HPD_TO_CONNECTOR_BIT(sd_nr), hotplug);
  132. cobalt_dbg(1, "Set hotplug for adv %d to %d\n", sd_nr, hotplug);
  133. break;
  134. case V4L2_DEVICE_NOTIFY_EVENT:
  135. cobalt_dbg(1, "Format changed for adv %d\n", sd_nr);
  136. v4l2_event_queue(&s->vdev, arg);
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. static int get_payload_size(u16 code)
  143. {
  144. switch (code) {
  145. case 0: return 128;
  146. case 1: return 256;
  147. case 2: return 512;
  148. case 3: return 1024;
  149. case 4: return 2048;
  150. case 5: return 4096;
  151. default: return 0;
  152. }
  153. return 0;
  154. }
  155. static const char *get_link_speed(u16 stat)
  156. {
  157. switch (stat & PCI_EXP_LNKSTA_CLS) {
  158. case 1: return "2.5 Gbit/s";
  159. case 2: return "5 Gbit/s";
  160. case 3: return "10 Gbit/s";
  161. }
  162. return "Unknown speed";
  163. }
  164. void cobalt_pcie_status_show(struct cobalt *cobalt)
  165. {
  166. struct pci_dev *pci_dev = cobalt->pci_dev;
  167. struct pci_dev *pci_bus_dev = cobalt->pci_dev->bus->self;
  168. int offset;
  169. int bus_offset;
  170. u32 capa;
  171. u16 stat, ctrl;
  172. offset = pci_find_capability(pci_dev, PCI_CAP_ID_EXP);
  173. bus_offset = pci_find_capability(pci_bus_dev, PCI_CAP_ID_EXP);
  174. if (!offset || !bus_offset)
  175. return;
  176. /* Device */
  177. pci_read_config_dword(pci_dev, offset + PCI_EXP_DEVCAP, &capa);
  178. pci_read_config_word(pci_dev, offset + PCI_EXP_DEVCTL, &ctrl);
  179. pci_read_config_word(pci_dev, offset + PCI_EXP_DEVSTA, &stat);
  180. cobalt_info("PCIe device capability 0x%08x: Max payload %d\n",
  181. capa, get_payload_size(capa & PCI_EXP_DEVCAP_PAYLOAD));
  182. cobalt_info("PCIe device control 0x%04x: Max payload %d. Max read request %d\n",
  183. ctrl,
  184. get_payload_size((ctrl & PCI_EXP_DEVCTL_PAYLOAD) >> 5),
  185. get_payload_size((ctrl & PCI_EXP_DEVCTL_READRQ) >> 12));
  186. cobalt_info("PCIe device status 0x%04x\n", stat);
  187. /* Link */
  188. pci_read_config_dword(pci_dev, offset + PCI_EXP_LNKCAP, &capa);
  189. pci_read_config_word(pci_dev, offset + PCI_EXP_LNKCTL, &ctrl);
  190. pci_read_config_word(pci_dev, offset + PCI_EXP_LNKSTA, &stat);
  191. cobalt_info("PCIe link capability 0x%08x: %s per lane and %u lanes\n",
  192. capa, get_link_speed(capa),
  193. (capa & PCI_EXP_LNKCAP_MLW) >> 4);
  194. cobalt_info("PCIe link control 0x%04x\n", ctrl);
  195. cobalt_info("PCIe link status 0x%04x: %s per lane and %u lanes\n",
  196. stat, get_link_speed(stat),
  197. (stat & PCI_EXP_LNKSTA_NLW) >> 4);
  198. /* Bus */
  199. pci_read_config_dword(pci_bus_dev, bus_offset + PCI_EXP_LNKCAP, &capa);
  200. cobalt_info("PCIe bus link capability 0x%08x: %s per lane and %u lanes\n",
  201. capa, get_link_speed(capa),
  202. (capa & PCI_EXP_LNKCAP_MLW) >> 4);
  203. /* Slot */
  204. pci_read_config_dword(pci_dev, offset + PCI_EXP_SLTCAP, &capa);
  205. pci_read_config_word(pci_dev, offset + PCI_EXP_SLTCTL, &ctrl);
  206. pci_read_config_word(pci_dev, offset + PCI_EXP_SLTSTA, &stat);
  207. cobalt_info("PCIe slot capability 0x%08x\n", capa);
  208. cobalt_info("PCIe slot control 0x%04x\n", ctrl);
  209. cobalt_info("PCIe slot status 0x%04x\n", stat);
  210. }
  211. static unsigned pcie_link_get_lanes(struct cobalt *cobalt)
  212. {
  213. struct pci_dev *pci_dev = cobalt->pci_dev;
  214. unsigned offset;
  215. u16 link;
  216. offset = pci_find_capability(pci_dev, PCI_CAP_ID_EXP);
  217. if (!offset)
  218. return 0;
  219. pci_read_config_word(pci_dev, offset + PCI_EXP_LNKSTA, &link);
  220. return (link & PCI_EXP_LNKSTA_NLW) >> 4;
  221. }
  222. static unsigned pcie_bus_link_get_lanes(struct cobalt *cobalt)
  223. {
  224. struct pci_dev *pci_dev = cobalt->pci_dev->bus->self;
  225. unsigned offset;
  226. u32 link;
  227. offset = pci_find_capability(pci_dev, PCI_CAP_ID_EXP);
  228. if (!offset)
  229. return 0;
  230. pci_read_config_dword(pci_dev, offset + PCI_EXP_LNKCAP, &link);
  231. return (link & PCI_EXP_LNKCAP_MLW) >> 4;
  232. }
  233. static void msi_config_show(struct cobalt *cobalt, struct pci_dev *pci_dev)
  234. {
  235. u16 ctrl, data;
  236. u32 adrs_l, adrs_h;
  237. pci_read_config_word(pci_dev, 0x52, &ctrl);
  238. cobalt_info("MSI %s\n", ctrl & 1 ? "enable" : "disable");
  239. cobalt_info("MSI multiple message: Capable %u. Enable %u\n",
  240. (1 << ((ctrl >> 1) & 7)), (1 << ((ctrl >> 4) & 7)));
  241. if (ctrl & 0x80)
  242. cobalt_info("MSI: 64-bit address capable\n");
  243. pci_read_config_dword(pci_dev, 0x54, &adrs_l);
  244. pci_read_config_dword(pci_dev, 0x58, &adrs_h);
  245. pci_read_config_word(pci_dev, 0x5c, &data);
  246. if (ctrl & 0x80)
  247. cobalt_info("MSI: Address 0x%08x%08x. Data 0x%04x\n",
  248. adrs_h, adrs_l, data);
  249. else
  250. cobalt_info("MSI: Address 0x%08x. Data 0x%04x\n",
  251. adrs_l, data);
  252. }
  253. static void cobalt_pci_iounmap(struct cobalt *cobalt, struct pci_dev *pci_dev)
  254. {
  255. if (cobalt->bar0) {
  256. pci_iounmap(pci_dev, cobalt->bar0);
  257. cobalt->bar0 = NULL;
  258. }
  259. if (cobalt->bar1) {
  260. pci_iounmap(pci_dev, cobalt->bar1);
  261. cobalt->bar1 = NULL;
  262. }
  263. }
  264. static void cobalt_free_msi(struct cobalt *cobalt, struct pci_dev *pci_dev)
  265. {
  266. free_irq(pci_dev->irq, (void *)cobalt);
  267. pci_free_irq_vectors(pci_dev);
  268. }
  269. static int cobalt_setup_pci(struct cobalt *cobalt, struct pci_dev *pci_dev,
  270. const struct pci_device_id *pci_id)
  271. {
  272. u32 ctrl;
  273. int ret;
  274. cobalt_dbg(1, "enabling pci device\n");
  275. ret = pci_enable_device(pci_dev);
  276. if (ret) {
  277. cobalt_err("can't enable device\n");
  278. return ret;
  279. }
  280. pci_set_master(pci_dev);
  281. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cobalt->card_rev);
  282. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &cobalt->device_id);
  283. switch (cobalt->device_id) {
  284. case PCI_DEVICE_ID_COBALT:
  285. cobalt_info("PCI Express interface from Omnitek\n");
  286. break;
  287. default:
  288. cobalt_info("PCI Express interface provider is unknown!\n");
  289. break;
  290. }
  291. if (pcie_link_get_lanes(cobalt) != 8) {
  292. cobalt_warn("PCI Express link width is %d lanes.\n",
  293. pcie_link_get_lanes(cobalt));
  294. if (pcie_bus_link_get_lanes(cobalt) < 8)
  295. cobalt_warn("The current slot only supports %d lanes, for best performance 8 are needed\n",
  296. pcie_bus_link_get_lanes(cobalt));
  297. if (pcie_link_get_lanes(cobalt) != pcie_bus_link_get_lanes(cobalt)) {
  298. cobalt_err("The card is most likely not seated correctly in the PCIe slot\n");
  299. ret = -EIO;
  300. goto err_disable;
  301. }
  302. }
  303. if (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(64))) {
  304. ret = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
  305. if (ret) {
  306. cobalt_err("no suitable DMA available\n");
  307. goto err_disable;
  308. }
  309. }
  310. ret = pci_request_regions(pci_dev, "cobalt");
  311. if (ret) {
  312. cobalt_err("error requesting regions\n");
  313. goto err_disable;
  314. }
  315. cobalt_pcie_status_show(cobalt);
  316. cobalt->bar0 = pci_iomap(pci_dev, 0, 0);
  317. cobalt->bar1 = pci_iomap(pci_dev, 1, 0);
  318. if (cobalt->bar1 == NULL) {
  319. cobalt->bar1 = pci_iomap(pci_dev, 2, 0);
  320. cobalt_info("64-bit BAR\n");
  321. }
  322. if (!cobalt->bar0 || !cobalt->bar1) {
  323. ret = -EIO;
  324. goto err_release;
  325. }
  326. /* Reset the video inputs before enabling any interrupts */
  327. ctrl = cobalt_read_bar1(cobalt, COBALT_SYS_CTRL_BASE);
  328. cobalt_write_bar1(cobalt, COBALT_SYS_CTRL_BASE, ctrl & ~0xf00);
  329. /* Disable interrupts to prevent any spurious interrupts
  330. from being generated. */
  331. cobalt_set_interrupt(cobalt, false);
  332. if (pci_alloc_irq_vectors(pci_dev, 1, 1, PCI_IRQ_MSI) < 1) {
  333. cobalt_err("Could not enable MSI\n");
  334. ret = -EIO;
  335. goto err_release;
  336. }
  337. msi_config_show(cobalt, pci_dev);
  338. /* Register IRQ */
  339. if (request_irq(pci_dev->irq, cobalt_irq_handler, IRQF_SHARED,
  340. cobalt->v4l2_dev.name, (void *)cobalt)) {
  341. cobalt_err("Failed to register irq %d\n", pci_dev->irq);
  342. ret = -EIO;
  343. goto err_msi;
  344. }
  345. omni_sg_dma_init(cobalt);
  346. return 0;
  347. err_msi:
  348. pci_disable_msi(pci_dev);
  349. err_release:
  350. cobalt_pci_iounmap(cobalt, pci_dev);
  351. pci_release_regions(pci_dev);
  352. err_disable:
  353. pci_disable_device(cobalt->pci_dev);
  354. return ret;
  355. }
  356. static int cobalt_hdl_info_get(struct cobalt *cobalt)
  357. {
  358. int i;
  359. for (i = 0; i < COBALT_HDL_INFO_SIZE; i++)
  360. cobalt->hdl_info[i] =
  361. ioread8(cobalt->bar1 + COBALT_HDL_INFO_BASE + i);
  362. cobalt->hdl_info[COBALT_HDL_INFO_SIZE - 1] = '\0';
  363. if (strstr(cobalt->hdl_info, COBALT_HDL_SEARCH_STR))
  364. return 0;
  365. return 1;
  366. }
  367. static void cobalt_stream_struct_init(struct cobalt *cobalt)
  368. {
  369. int i;
  370. for (i = 0; i < COBALT_NUM_STREAMS; i++) {
  371. struct cobalt_stream *s = &cobalt->streams[i];
  372. s->cobalt = cobalt;
  373. s->flags = 0;
  374. s->is_audio = false;
  375. s->is_output = false;
  376. s->is_dummy = true;
  377. /* The Memory DMA channels will always get a lower channel
  378. * number than the FIFO DMA. Video input should map to the
  379. * stream 0-3. The other can use stream struct from 4 and
  380. * higher */
  381. if (i <= COBALT_HSMA_IN_NODE) {
  382. s->dma_channel = i + cobalt->first_fifo_channel;
  383. s->video_channel = i;
  384. s->dma_fifo_mask =
  385. COBALT_SYSSTAT_VI0_LOST_DATA_MSK << (4 * i);
  386. s->adv_irq_mask =
  387. COBALT_SYSSTAT_VI0_INT1_MSK << (4 * i);
  388. } else if (i >= COBALT_AUDIO_IN_STREAM &&
  389. i <= COBALT_AUDIO_IN_STREAM + 4) {
  390. unsigned idx = i - COBALT_AUDIO_IN_STREAM;
  391. s->dma_channel = 6 + idx;
  392. s->is_audio = true;
  393. s->video_channel = idx;
  394. s->dma_fifo_mask = COBALT_SYSSTAT_AUD_IN_LOST_DATA_MSK;
  395. } else if (i == COBALT_HSMA_OUT_NODE) {
  396. s->dma_channel = 11;
  397. s->is_output = true;
  398. s->video_channel = 5;
  399. s->dma_fifo_mask = COBALT_SYSSTAT_VOHSMA_LOST_DATA_MSK;
  400. s->adv_irq_mask = COBALT_SYSSTAT_VOHSMA_INT1_MSK;
  401. } else if (i == COBALT_AUDIO_OUT_STREAM) {
  402. s->dma_channel = 12;
  403. s->is_audio = true;
  404. s->is_output = true;
  405. s->video_channel = 5;
  406. s->dma_fifo_mask = COBALT_SYSSTAT_AUD_OUT_LOST_DATA_MSK;
  407. } else {
  408. /* FIXME: Memory DMA for debug purpose */
  409. s->dma_channel = i - COBALT_NUM_NODES;
  410. }
  411. cobalt_info("stream #%d -> dma channel #%d <- video channel %d\n",
  412. i, s->dma_channel, s->video_channel);
  413. }
  414. }
  415. static int cobalt_subdevs_init(struct cobalt *cobalt)
  416. {
  417. static struct adv76xx_platform_data adv7604_pdata = {
  418. .disable_pwrdnb = 1,
  419. .ain_sel = ADV7604_AIN7_8_9_NC_SYNC_3_1,
  420. .bus_order = ADV7604_BUS_ORDER_BRG,
  421. .blank_data = 1,
  422. .op_format_mode_sel = ADV7604_OP_FORMAT_MODE0,
  423. .int1_config = ADV76XX_INT1_CONFIG_ACTIVE_HIGH,
  424. .dr_str_data = ADV76XX_DR_STR_HIGH,
  425. .dr_str_clk = ADV76XX_DR_STR_HIGH,
  426. .dr_str_sync = ADV76XX_DR_STR_HIGH,
  427. .hdmi_free_run_mode = 1,
  428. .inv_vs_pol = 1,
  429. .inv_hs_pol = 1,
  430. };
  431. static struct i2c_board_info adv7604_info = {
  432. .type = "adv7604",
  433. .addr = 0x20,
  434. .platform_data = &adv7604_pdata,
  435. };
  436. struct cobalt_stream *s = cobalt->streams;
  437. int i;
  438. for (i = 0; i < COBALT_NUM_INPUTS; i++) {
  439. struct v4l2_subdev_format sd_fmt = {
  440. .pad = ADV7604_PAD_SOURCE,
  441. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  442. .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
  443. };
  444. struct v4l2_subdev_edid cobalt_edid = {
  445. .pad = ADV76XX_PAD_HDMI_PORT_A,
  446. .start_block = 0,
  447. .blocks = 2,
  448. .edid = edid,
  449. };
  450. int err;
  451. s[i].pad_source = ADV7604_PAD_SOURCE;
  452. s[i].i2c_adap = &cobalt->i2c_adap[i];
  453. if (s[i].i2c_adap->dev.parent == NULL)
  454. continue;
  455. cobalt_s_bit_sysctrl(cobalt,
  456. COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(i), 1);
  457. s[i].sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
  458. s[i].i2c_adap, &adv7604_info, NULL);
  459. if (!s[i].sd) {
  460. if (cobalt_ignore_err)
  461. continue;
  462. return -ENODEV;
  463. }
  464. err = v4l2_subdev_call(s[i].sd, video, s_routing,
  465. ADV76XX_PAD_HDMI_PORT_A, 0, 0);
  466. if (err)
  467. return err;
  468. err = v4l2_subdev_call(s[i].sd, pad, set_edid,
  469. &cobalt_edid);
  470. if (err)
  471. return err;
  472. err = v4l2_subdev_call(s[i].sd, pad, set_fmt, NULL,
  473. &sd_fmt);
  474. if (err)
  475. return err;
  476. /* Reset channel video module */
  477. cobalt_s_bit_sysctrl(cobalt,
  478. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(i), 0);
  479. mdelay(2);
  480. cobalt_s_bit_sysctrl(cobalt,
  481. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(i), 1);
  482. mdelay(1);
  483. s[i].is_dummy = false;
  484. cobalt->streams[i + COBALT_AUDIO_IN_STREAM].is_dummy = false;
  485. }
  486. return 0;
  487. }
  488. static int cobalt_subdevs_hsma_init(struct cobalt *cobalt)
  489. {
  490. static struct adv7842_platform_data adv7842_pdata = {
  491. .disable_pwrdnb = 1,
  492. .ain_sel = ADV7842_AIN1_2_3_NC_SYNC_1_2,
  493. .bus_order = ADV7842_BUS_ORDER_RBG,
  494. .op_format_mode_sel = ADV7842_OP_FORMAT_MODE0,
  495. .blank_data = 1,
  496. .dr_str_data = 3,
  497. .dr_str_clk = 3,
  498. .dr_str_sync = 3,
  499. .mode = ADV7842_MODE_HDMI,
  500. .hdmi_free_run_enable = 1,
  501. .vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P,
  502. .i2c_sdp_io = 0x4a,
  503. .i2c_sdp = 0x48,
  504. .i2c_cp = 0x22,
  505. .i2c_vdp = 0x24,
  506. .i2c_afe = 0x26,
  507. .i2c_hdmi = 0x34,
  508. .i2c_repeater = 0x32,
  509. .i2c_edid = 0x36,
  510. .i2c_infoframe = 0x3e,
  511. .i2c_cec = 0x40,
  512. .i2c_avlink = 0x42,
  513. };
  514. static struct i2c_board_info adv7842_info = {
  515. .type = "adv7842",
  516. .addr = 0x20,
  517. .platform_data = &adv7842_pdata,
  518. };
  519. static struct v4l2_subdev_format sd_fmt = {
  520. .pad = ADV7842_PAD_SOURCE,
  521. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  522. .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
  523. };
  524. static struct adv7511_platform_data adv7511_pdata = {
  525. .i2c_edid = 0x7e >> 1,
  526. .i2c_cec = 0x7c >> 1,
  527. .i2c_pktmem = 0x70 >> 1,
  528. .cec_clk = 12000000,
  529. };
  530. static struct i2c_board_info adv7511_info = {
  531. .type = "adv7511",
  532. .addr = 0x39, /* 0x39 or 0x3d */
  533. .platform_data = &adv7511_pdata,
  534. };
  535. struct v4l2_subdev_edid cobalt_edid = {
  536. .pad = ADV7842_EDID_PORT_A,
  537. .start_block = 0,
  538. .blocks = 2,
  539. .edid = edid,
  540. };
  541. struct cobalt_stream *s = &cobalt->streams[COBALT_HSMA_IN_NODE];
  542. s->i2c_adap = &cobalt->i2c_adap[COBALT_NUM_ADAPTERS - 1];
  543. if (s->i2c_adap->dev.parent == NULL)
  544. return 0;
  545. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(4), 1);
  546. s->sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
  547. s->i2c_adap, &adv7842_info, NULL);
  548. if (s->sd) {
  549. int err = v4l2_subdev_call(s->sd, pad, set_edid, &cobalt_edid);
  550. if (err)
  551. return err;
  552. err = v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
  553. &sd_fmt);
  554. if (err)
  555. return err;
  556. cobalt->have_hsma_rx = true;
  557. s->pad_source = ADV7842_PAD_SOURCE;
  558. s->is_dummy = false;
  559. cobalt->streams[4 + COBALT_AUDIO_IN_STREAM].is_dummy = false;
  560. /* Reset channel video module */
  561. cobalt_s_bit_sysctrl(cobalt,
  562. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 0);
  563. mdelay(2);
  564. cobalt_s_bit_sysctrl(cobalt,
  565. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 1);
  566. mdelay(1);
  567. return err;
  568. }
  569. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(4), 0);
  570. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_PWRDN0_TO_HSMA_TX_BIT, 0);
  571. s++;
  572. s->i2c_adap = &cobalt->i2c_adap[COBALT_NUM_ADAPTERS - 1];
  573. s->sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
  574. s->i2c_adap, &adv7511_info, NULL);
  575. if (s->sd) {
  576. /* A transmitter is hooked up, so we can set this bit */
  577. cobalt_s_bit_sysctrl(cobalt,
  578. COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 1);
  579. cobalt_s_bit_sysctrl(cobalt,
  580. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 0);
  581. cobalt_s_bit_sysctrl(cobalt,
  582. COBALT_SYS_CTRL_VIDEO_TX_RESETN_BIT, 1);
  583. cobalt->have_hsma_tx = true;
  584. v4l2_subdev_call(s->sd, core, s_power, 1);
  585. v4l2_subdev_call(s->sd, video, s_stream, 1);
  586. v4l2_subdev_call(s->sd, audio, s_stream, 1);
  587. v4l2_ctrl_s_ctrl(v4l2_ctrl_find(s->sd->ctrl_handler,
  588. V4L2_CID_DV_TX_MODE), V4L2_DV_TX_MODE_HDMI);
  589. s->is_dummy = false;
  590. cobalt->streams[COBALT_AUDIO_OUT_STREAM].is_dummy = false;
  591. return 0;
  592. }
  593. return -ENODEV;
  594. }
  595. static int cobalt_probe(struct pci_dev *pci_dev,
  596. const struct pci_device_id *pci_id)
  597. {
  598. struct cobalt *cobalt;
  599. int retval = 0;
  600. int i;
  601. /* FIXME - module parameter arrays constrain max instances */
  602. i = atomic_inc_return(&cobalt_instance) - 1;
  603. cobalt = kzalloc(sizeof(struct cobalt), GFP_KERNEL);
  604. if (cobalt == NULL)
  605. return -ENOMEM;
  606. cobalt->pci_dev = pci_dev;
  607. cobalt->instance = i;
  608. retval = v4l2_device_register(&pci_dev->dev, &cobalt->v4l2_dev);
  609. if (retval) {
  610. pr_err("cobalt: v4l2_device_register of card %d failed\n",
  611. cobalt->instance);
  612. kfree(cobalt);
  613. return retval;
  614. }
  615. snprintf(cobalt->v4l2_dev.name, sizeof(cobalt->v4l2_dev.name),
  616. "cobalt-%d", cobalt->instance);
  617. cobalt->v4l2_dev.notify = cobalt_notify;
  618. cobalt_info("Initializing card %d\n", cobalt->instance);
  619. cobalt->irq_work_queues =
  620. create_singlethread_workqueue(cobalt->v4l2_dev.name);
  621. if (cobalt->irq_work_queues == NULL) {
  622. cobalt_err("Could not create workqueue\n");
  623. retval = -ENOMEM;
  624. goto err;
  625. }
  626. INIT_WORK(&cobalt->irq_work_queue, cobalt_irq_work_handler);
  627. /* PCI Device Setup */
  628. retval = cobalt_setup_pci(cobalt, pci_dev, pci_id);
  629. if (retval != 0)
  630. goto err_wq;
  631. /* Show HDL version info */
  632. if (cobalt_hdl_info_get(cobalt))
  633. cobalt_info("Not able to read the HDL info\n");
  634. else
  635. cobalt_info("%s", cobalt->hdl_info);
  636. retval = cobalt_i2c_init(cobalt);
  637. if (retval)
  638. goto err_pci;
  639. cobalt_stream_struct_init(cobalt);
  640. retval = cobalt_subdevs_init(cobalt);
  641. if (retval)
  642. goto err_i2c;
  643. if (!(cobalt_read_bar1(cobalt, COBALT_SYS_STAT_BASE) &
  644. COBALT_SYSSTAT_HSMA_PRSNTN_MSK)) {
  645. retval = cobalt_subdevs_hsma_init(cobalt);
  646. if (retval)
  647. goto err_i2c;
  648. }
  649. retval = cobalt_nodes_register(cobalt);
  650. if (retval) {
  651. cobalt_err("Error %d registering device nodes\n", retval);
  652. goto err_i2c;
  653. }
  654. cobalt_set_interrupt(cobalt, true);
  655. v4l2_device_call_all(&cobalt->v4l2_dev, 0, core,
  656. interrupt_service_routine, 0, NULL);
  657. cobalt_info("Initialized cobalt card\n");
  658. cobalt_flash_probe(cobalt);
  659. return 0;
  660. err_i2c:
  661. cobalt_i2c_exit(cobalt);
  662. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 0);
  663. err_pci:
  664. cobalt_free_msi(cobalt, pci_dev);
  665. cobalt_pci_iounmap(cobalt, pci_dev);
  666. pci_release_regions(cobalt->pci_dev);
  667. pci_disable_device(cobalt->pci_dev);
  668. err_wq:
  669. destroy_workqueue(cobalt->irq_work_queues);
  670. err:
  671. cobalt_err("error %d on initialization\n", retval);
  672. v4l2_device_unregister(&cobalt->v4l2_dev);
  673. kfree(cobalt);
  674. return retval;
  675. }
  676. static void cobalt_remove(struct pci_dev *pci_dev)
  677. {
  678. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  679. struct cobalt *cobalt = to_cobalt(v4l2_dev);
  680. int i;
  681. cobalt_flash_remove(cobalt);
  682. cobalt_set_interrupt(cobalt, false);
  683. flush_workqueue(cobalt->irq_work_queues);
  684. cobalt_nodes_unregister(cobalt);
  685. for (i = 0; i < COBALT_NUM_ADAPTERS; i++) {
  686. struct v4l2_subdev *sd = cobalt->streams[i].sd;
  687. struct i2c_client *client;
  688. if (sd == NULL)
  689. continue;
  690. client = v4l2_get_subdevdata(sd);
  691. v4l2_device_unregister_subdev(sd);
  692. i2c_unregister_device(client);
  693. }
  694. cobalt_i2c_exit(cobalt);
  695. cobalt_free_msi(cobalt, pci_dev);
  696. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 0);
  697. cobalt_pci_iounmap(cobalt, pci_dev);
  698. pci_release_regions(cobalt->pci_dev);
  699. pci_disable_device(cobalt->pci_dev);
  700. destroy_workqueue(cobalt->irq_work_queues);
  701. cobalt_info("removed cobalt card\n");
  702. v4l2_device_unregister(v4l2_dev);
  703. kfree(cobalt);
  704. }
  705. /* define a pci_driver for card detection */
  706. static struct pci_driver cobalt_pci_driver = {
  707. .name = "cobalt",
  708. .id_table = cobalt_pci_tbl,
  709. .probe = cobalt_probe,
  710. .remove = cobalt_remove,
  711. };
  712. module_pci_driver(cobalt_pci_driver);