cobalt-driver.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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/i2c/adv7604.h>
  24. #include <media/i2c/adv7842.h>
  25. #include <media/i2c/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, 0x32, 0x27, 0x00, 0x00, 0x00, 0x00,
  56. 0x22, 0x1a, 0x01, 0x03, 0x80, 0x30, 0x1b, 0x78,
  57. 0x0f, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
  58. 0x0f, 0x50, 0x54, 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, 0xe0, 0x0e, 0x11, 0x00, 0x00, 0x1e,
  63. 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
  64. 0x5e, 0x11, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
  65. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x63,
  66. 0x6f, 0x62, 0x61, 0x6c, 0x74, 0x0a, 0x20, 0x20,
  67. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x10,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9c,
  70. 0x02, 0x03, 0x1f, 0xf0, 0x4a, 0x90, 0x1f, 0x04,
  71. 0x13, 0x22, 0x21, 0x20, 0x02, 0x11, 0x01, 0x23,
  72. 0x09, 0x07, 0x07, 0x68, 0x03, 0x0c, 0x00, 0x10,
  73. 0x00, 0x00, 0x22, 0x0f, 0xe2, 0x00, 0xea, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0xa7,
  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_format_mode_sel = ADV7604_OP_FORMAT_MODE0,
  436. .int1_config = ADV76XX_INT1_CONFIG_ACTIVE_HIGH,
  437. .dr_str_data = ADV76XX_DR_STR_HIGH,
  438. .dr_str_clk = ADV76XX_DR_STR_HIGH,
  439. .dr_str_sync = ADV76XX_DR_STR_HIGH,
  440. .hdmi_free_run_mode = 1,
  441. .inv_vs_pol = 1,
  442. .inv_hs_pol = 1,
  443. };
  444. static struct i2c_board_info adv7604_info = {
  445. .type = "adv7604",
  446. .addr = 0x20,
  447. .platform_data = &adv7604_pdata,
  448. };
  449. struct cobalt_stream *s = cobalt->streams;
  450. int i;
  451. for (i = 0; i < COBALT_NUM_INPUTS; i++) {
  452. struct v4l2_subdev_format sd_fmt = {
  453. .pad = ADV7604_PAD_SOURCE,
  454. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  455. .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
  456. };
  457. struct v4l2_subdev_edid cobalt_edid = {
  458. .pad = ADV76XX_PAD_HDMI_PORT_A,
  459. .start_block = 0,
  460. .blocks = 2,
  461. .edid = edid,
  462. };
  463. int err;
  464. s[i].pad_source = ADV7604_PAD_SOURCE;
  465. s[i].i2c_adap = &cobalt->i2c_adap[i];
  466. if (s[i].i2c_adap->dev.parent == NULL)
  467. continue;
  468. cobalt_s_bit_sysctrl(cobalt,
  469. COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(i), 1);
  470. s[i].sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
  471. s[i].i2c_adap, &adv7604_info, NULL);
  472. if (!s[i].sd) {
  473. if (cobalt_ignore_err)
  474. continue;
  475. return -ENODEV;
  476. }
  477. err = v4l2_subdev_call(s[i].sd, video, s_routing,
  478. ADV76XX_PAD_HDMI_PORT_A, 0, 0);
  479. if (err)
  480. return err;
  481. err = v4l2_subdev_call(s[i].sd, pad, set_edid,
  482. &cobalt_edid);
  483. if (err)
  484. return err;
  485. err = v4l2_subdev_call(s[i].sd, pad, set_fmt, NULL,
  486. &sd_fmt);
  487. if (err)
  488. return err;
  489. /* Reset channel video module */
  490. cobalt_s_bit_sysctrl(cobalt,
  491. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(i), 0);
  492. mdelay(2);
  493. cobalt_s_bit_sysctrl(cobalt,
  494. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(i), 1);
  495. mdelay(1);
  496. s[i].is_dummy = false;
  497. cobalt->streams[i + COBALT_AUDIO_IN_STREAM].is_dummy = false;
  498. }
  499. return 0;
  500. }
  501. static int cobalt_subdevs_hsma_init(struct cobalt *cobalt)
  502. {
  503. static struct adv7842_platform_data adv7842_pdata = {
  504. .disable_pwrdnb = 1,
  505. .ain_sel = ADV7842_AIN1_2_3_NC_SYNC_1_2,
  506. .bus_order = ADV7842_BUS_ORDER_RBG,
  507. .op_format_mode_sel = ADV7842_OP_FORMAT_MODE0,
  508. .blank_data = 1,
  509. .dr_str_data = 3,
  510. .dr_str_clk = 3,
  511. .dr_str_sync = 3,
  512. .mode = ADV7842_MODE_HDMI,
  513. .hdmi_free_run_enable = 1,
  514. .vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P,
  515. .i2c_sdp_io = 0x4a,
  516. .i2c_sdp = 0x48,
  517. .i2c_cp = 0x22,
  518. .i2c_vdp = 0x24,
  519. .i2c_afe = 0x26,
  520. .i2c_hdmi = 0x34,
  521. .i2c_repeater = 0x32,
  522. .i2c_edid = 0x36,
  523. .i2c_infoframe = 0x3e,
  524. .i2c_cec = 0x40,
  525. .i2c_avlink = 0x42,
  526. };
  527. static struct i2c_board_info adv7842_info = {
  528. .type = "adv7842",
  529. .addr = 0x20,
  530. .platform_data = &adv7842_pdata,
  531. };
  532. static struct v4l2_subdev_format sd_fmt = {
  533. .pad = ADV7842_PAD_SOURCE,
  534. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  535. .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
  536. };
  537. static struct adv7511_platform_data adv7511_pdata = {
  538. .i2c_edid = 0x7e >> 1,
  539. .i2c_cec = 0x7c >> 1,
  540. .i2c_pktmem = 0x70 >> 1,
  541. .cec_clk = 12000000,
  542. };
  543. static struct i2c_board_info adv7511_info = {
  544. .type = "adv7511",
  545. .addr = 0x39, /* 0x39 or 0x3d */
  546. .platform_data = &adv7511_pdata,
  547. };
  548. struct v4l2_subdev_edid cobalt_edid = {
  549. .pad = ADV7842_EDID_PORT_A,
  550. .start_block = 0,
  551. .blocks = 2,
  552. .edid = edid,
  553. };
  554. struct cobalt_stream *s = &cobalt->streams[COBALT_HSMA_IN_NODE];
  555. s->i2c_adap = &cobalt->i2c_adap[COBALT_NUM_ADAPTERS - 1];
  556. if (s->i2c_adap->dev.parent == NULL)
  557. return 0;
  558. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(4), 1);
  559. s->sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
  560. s->i2c_adap, &adv7842_info, NULL);
  561. if (s->sd) {
  562. int err = v4l2_subdev_call(s->sd, pad, set_edid, &cobalt_edid);
  563. if (err)
  564. return err;
  565. err = v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
  566. &sd_fmt);
  567. if (err)
  568. return err;
  569. cobalt->have_hsma_rx = true;
  570. s->pad_source = ADV7842_PAD_SOURCE;
  571. s->is_dummy = false;
  572. cobalt->streams[4 + COBALT_AUDIO_IN_STREAM].is_dummy = false;
  573. /* Reset channel video module */
  574. cobalt_s_bit_sysctrl(cobalt,
  575. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 0);
  576. mdelay(2);
  577. cobalt_s_bit_sysctrl(cobalt,
  578. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 1);
  579. mdelay(1);
  580. return err;
  581. }
  582. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(4), 0);
  583. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_PWRDN0_TO_HSMA_TX_BIT, 0);
  584. s++;
  585. s->i2c_adap = &cobalt->i2c_adap[COBALT_NUM_ADAPTERS - 1];
  586. s->sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
  587. s->i2c_adap, &adv7511_info, NULL);
  588. if (s->sd) {
  589. /* A transmitter is hooked up, so we can set this bit */
  590. cobalt_s_bit_sysctrl(cobalt,
  591. COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 1);
  592. cobalt_s_bit_sysctrl(cobalt,
  593. COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 0);
  594. cobalt_s_bit_sysctrl(cobalt,
  595. COBALT_SYS_CTRL_VIDEO_TX_RESETN_BIT, 1);
  596. cobalt->have_hsma_tx = true;
  597. v4l2_subdev_call(s->sd, core, s_power, 1);
  598. v4l2_subdev_call(s->sd, video, s_stream, 1);
  599. v4l2_subdev_call(s->sd, audio, s_stream, 1);
  600. v4l2_ctrl_s_ctrl(v4l2_ctrl_find(s->sd->ctrl_handler,
  601. V4L2_CID_DV_TX_MODE), V4L2_DV_TX_MODE_HDMI);
  602. s->is_dummy = false;
  603. cobalt->streams[COBALT_AUDIO_OUT_STREAM].is_dummy = false;
  604. return 0;
  605. }
  606. return -ENODEV;
  607. }
  608. static int cobalt_probe(struct pci_dev *pci_dev,
  609. const struct pci_device_id *pci_id)
  610. {
  611. struct cobalt *cobalt;
  612. int retval = 0;
  613. int i;
  614. /* FIXME - module parameter arrays constrain max instances */
  615. i = atomic_inc_return(&cobalt_instance) - 1;
  616. cobalt = kzalloc(sizeof(struct cobalt), GFP_ATOMIC);
  617. if (cobalt == NULL)
  618. return -ENOMEM;
  619. cobalt->pci_dev = pci_dev;
  620. cobalt->instance = i;
  621. retval = v4l2_device_register(&pci_dev->dev, &cobalt->v4l2_dev);
  622. if (retval) {
  623. pr_err("cobalt: v4l2_device_register of card %d failed\n",
  624. cobalt->instance);
  625. kfree(cobalt);
  626. return retval;
  627. }
  628. snprintf(cobalt->v4l2_dev.name, sizeof(cobalt->v4l2_dev.name),
  629. "cobalt-%d", cobalt->instance);
  630. cobalt->v4l2_dev.notify = cobalt_notify;
  631. cobalt_info("Initializing card %d\n", cobalt->instance);
  632. cobalt->irq_work_queues =
  633. create_singlethread_workqueue(cobalt->v4l2_dev.name);
  634. if (cobalt->irq_work_queues == NULL) {
  635. cobalt_err("Could not create workqueue\n");
  636. retval = -ENOMEM;
  637. goto err;
  638. }
  639. INIT_WORK(&cobalt->irq_work_queue, cobalt_irq_work_handler);
  640. /* PCI Device Setup */
  641. retval = cobalt_setup_pci(cobalt, pci_dev, pci_id);
  642. if (retval != 0)
  643. goto err_wq;
  644. /* Show HDL version info */
  645. if (cobalt_hdl_info_get(cobalt))
  646. cobalt_info("Not able to read the HDL info\n");
  647. else
  648. cobalt_info("%s", cobalt->hdl_info);
  649. retval = cobalt_i2c_init(cobalt);
  650. if (retval)
  651. goto err_pci;
  652. cobalt_stream_struct_init(cobalt);
  653. retval = cobalt_subdevs_init(cobalt);
  654. if (retval)
  655. goto err_i2c;
  656. if (!(cobalt_read_bar1(cobalt, COBALT_SYS_STAT_BASE) &
  657. COBALT_SYSSTAT_HSMA_PRSNTN_MSK)) {
  658. retval = cobalt_subdevs_hsma_init(cobalt);
  659. if (retval)
  660. goto err_i2c;
  661. }
  662. retval = v4l2_device_register_subdev_nodes(&cobalt->v4l2_dev);
  663. if (retval)
  664. goto err_i2c;
  665. retval = cobalt_nodes_register(cobalt);
  666. if (retval) {
  667. cobalt_err("Error %d registering device nodes\n", retval);
  668. goto err_i2c;
  669. }
  670. cobalt_set_interrupt(cobalt, true);
  671. v4l2_device_call_all(&cobalt->v4l2_dev, 0, core,
  672. interrupt_service_routine, 0, NULL);
  673. cobalt_info("Initialized cobalt card\n");
  674. cobalt_flash_probe(cobalt);
  675. return 0;
  676. err_i2c:
  677. cobalt_i2c_exit(cobalt);
  678. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 0);
  679. err_pci:
  680. cobalt_free_msi(cobalt, pci_dev);
  681. cobalt_pci_iounmap(cobalt, pci_dev);
  682. pci_release_regions(cobalt->pci_dev);
  683. pci_disable_device(cobalt->pci_dev);
  684. err_wq:
  685. destroy_workqueue(cobalt->irq_work_queues);
  686. err:
  687. if (retval == 0)
  688. retval = -ENODEV;
  689. cobalt_err("error %d on initialization\n", retval);
  690. v4l2_device_unregister(&cobalt->v4l2_dev);
  691. kfree(cobalt);
  692. return retval;
  693. }
  694. static void cobalt_remove(struct pci_dev *pci_dev)
  695. {
  696. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  697. struct cobalt *cobalt = to_cobalt(v4l2_dev);
  698. int i;
  699. cobalt_flash_remove(cobalt);
  700. cobalt_set_interrupt(cobalt, false);
  701. flush_workqueue(cobalt->irq_work_queues);
  702. cobalt_nodes_unregister(cobalt);
  703. for (i = 0; i < COBALT_NUM_ADAPTERS; i++) {
  704. struct v4l2_subdev *sd = cobalt->streams[i].sd;
  705. struct i2c_client *client;
  706. if (sd == NULL)
  707. continue;
  708. client = v4l2_get_subdevdata(sd);
  709. v4l2_device_unregister_subdev(sd);
  710. i2c_unregister_device(client);
  711. }
  712. cobalt_i2c_exit(cobalt);
  713. cobalt_free_msi(cobalt, pci_dev);
  714. cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 0);
  715. cobalt_pci_iounmap(cobalt, pci_dev);
  716. pci_release_regions(cobalt->pci_dev);
  717. pci_disable_device(cobalt->pci_dev);
  718. destroy_workqueue(cobalt->irq_work_queues);
  719. cobalt_info("removed cobalt card\n");
  720. v4l2_device_unregister(v4l2_dev);
  721. kfree(cobalt);
  722. }
  723. /* define a pci_driver for card detection */
  724. static struct pci_driver cobalt_pci_driver = {
  725. .name = "cobalt",
  726. .id_table = cobalt_pci_tbl,
  727. .probe = cobalt_probe,
  728. .remove = cobalt_remove,
  729. };
  730. module_pci_driver(cobalt_pci_driver);