cobalt-driver.c 24 KB

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