hexium_orion.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. hexium_orion.c - v4l2 driver for the Hexium Orion frame grabber cards
  3. Visit http://www.mihu.de/linux/saa7146/ and follow the link
  4. to "hexium" for further details about this card.
  5. Copyright (C) 2003 Michael Hunold <michael@mihu.de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #define DEBUG_VARIABLE debug
  20. #include <media/saa7146_vv.h>
  21. #include <linux/module.h>
  22. static int debug;
  23. module_param(debug, int, 0);
  24. MODULE_PARM_DESC(debug, "debug verbosity");
  25. /* global variables */
  26. static int hexium_num;
  27. #define HEXIUM_HV_PCI6_ORION 1
  28. #define HEXIUM_ORION_1SVHS_3BNC 2
  29. #define HEXIUM_ORION_4BNC 3
  30. #define HEXIUM_INPUTS 9
  31. static struct v4l2_input hexium_inputs[HEXIUM_INPUTS] = {
  32. { 0, "CVBS 1", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  33. { 1, "CVBS 2", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  34. { 2, "CVBS 3", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  35. { 3, "CVBS 4", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  36. { 4, "CVBS 5", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  37. { 5, "CVBS 6", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  38. { 6, "Y/C 1", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  39. { 7, "Y/C 2", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  40. { 8, "Y/C 3", V4L2_INPUT_TYPE_CAMERA, 0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
  41. };
  42. #define HEXIUM_AUDIOS 0
  43. struct hexium_data
  44. {
  45. s8 adr;
  46. u8 byte;
  47. };
  48. struct hexium
  49. {
  50. int type;
  51. struct video_device video_dev;
  52. struct i2c_adapter i2c_adapter;
  53. int cur_input; /* current input */
  54. };
  55. /* Philips SAA7110 decoder default registers */
  56. static u8 hexium_saa7110[53]={
  57. /*00*/ 0x4C,0x3C,0x0D,0xEF,0xBD,0xF0,0x00,0x00,
  58. /*08*/ 0xF8,0xF8,0x60,0x60,0x40,0x86,0x18,0x90,
  59. /*10*/ 0x00,0x2C,0x40,0x46,0x42,0x1A,0xFF,0xDA,
  60. /*18*/ 0xF0,0x8B,0x00,0x00,0x00,0x00,0x00,0x00,
  61. /*20*/ 0xD9,0x17,0x40,0x41,0x80,0x41,0x80,0x4F,
  62. /*28*/ 0xFE,0x01,0x0F,0x0F,0x03,0x01,0x81,0x03,
  63. /*30*/ 0x44,0x75,0x01,0x8C,0x03
  64. };
  65. static struct {
  66. struct hexium_data data[8];
  67. } hexium_input_select[] = {
  68. {
  69. { /* cvbs 1 */
  70. { 0x06, 0x00 },
  71. { 0x20, 0xD9 },
  72. { 0x21, 0x17 }, // 0x16,
  73. { 0x22, 0x40 },
  74. { 0x2C, 0x03 },
  75. { 0x30, 0x44 },
  76. { 0x31, 0x75 }, // ??
  77. { 0x21, 0x16 }, // 0x03,
  78. }
  79. }, {
  80. { /* cvbs 2 */
  81. { 0x06, 0x00 },
  82. { 0x20, 0x78 },
  83. { 0x21, 0x07 }, // 0x03,
  84. { 0x22, 0xD2 },
  85. { 0x2C, 0x83 },
  86. { 0x30, 0x60 },
  87. { 0x31, 0xB5 }, // ?
  88. { 0x21, 0x03 },
  89. }
  90. }, {
  91. { /* cvbs 3 */
  92. { 0x06, 0x00 },
  93. { 0x20, 0xBA },
  94. { 0x21, 0x07 }, // 0x05,
  95. { 0x22, 0x91 },
  96. { 0x2C, 0x03 },
  97. { 0x30, 0x60 },
  98. { 0x31, 0xB5 }, // ??
  99. { 0x21, 0x05 }, // 0x03,
  100. }
  101. }, {
  102. { /* cvbs 4 */
  103. { 0x06, 0x00 },
  104. { 0x20, 0xD8 },
  105. { 0x21, 0x17 }, // 0x16,
  106. { 0x22, 0x40 },
  107. { 0x2C, 0x03 },
  108. { 0x30, 0x44 },
  109. { 0x31, 0x75 }, // ??
  110. { 0x21, 0x16 }, // 0x03,
  111. }
  112. }, {
  113. { /* cvbs 5 */
  114. { 0x06, 0x00 },
  115. { 0x20, 0xB8 },
  116. { 0x21, 0x07 }, // 0x05,
  117. { 0x22, 0x91 },
  118. { 0x2C, 0x03 },
  119. { 0x30, 0x60 },
  120. { 0x31, 0xB5 }, // ??
  121. { 0x21, 0x05 }, // 0x03,
  122. }
  123. }, {
  124. { /* cvbs 6 */
  125. { 0x06, 0x00 },
  126. { 0x20, 0x7C },
  127. { 0x21, 0x07 }, // 0x03
  128. { 0x22, 0xD2 },
  129. { 0x2C, 0x83 },
  130. { 0x30, 0x60 },
  131. { 0x31, 0xB5 }, // ??
  132. { 0x21, 0x03 },
  133. }
  134. }, {
  135. { /* y/c 1 */
  136. { 0x06, 0x80 },
  137. { 0x20, 0x59 },
  138. { 0x21, 0x17 },
  139. { 0x22, 0x42 },
  140. { 0x2C, 0xA3 },
  141. { 0x30, 0x44 },
  142. { 0x31, 0x75 },
  143. { 0x21, 0x12 },
  144. }
  145. }, {
  146. { /* y/c 2 */
  147. { 0x06, 0x80 },
  148. { 0x20, 0x9A },
  149. { 0x21, 0x17 },
  150. { 0x22, 0xB1 },
  151. { 0x2C, 0x13 },
  152. { 0x30, 0x60 },
  153. { 0x31, 0xB5 },
  154. { 0x21, 0x14 },
  155. }
  156. }, {
  157. { /* y/c 3 */
  158. { 0x06, 0x80 },
  159. { 0x20, 0x3C },
  160. { 0x21, 0x27 },
  161. { 0x22, 0xC1 },
  162. { 0x2C, 0x23 },
  163. { 0x30, 0x44 },
  164. { 0x31, 0x75 },
  165. { 0x21, 0x21 },
  166. }
  167. }
  168. };
  169. static struct saa7146_standard hexium_standards[] = {
  170. {
  171. .name = "PAL", .id = V4L2_STD_PAL,
  172. .v_offset = 16, .v_field = 288,
  173. .h_offset = 1, .h_pixels = 680,
  174. .v_max_out = 576, .h_max_out = 768,
  175. }, {
  176. .name = "NTSC", .id = V4L2_STD_NTSC,
  177. .v_offset = 16, .v_field = 240,
  178. .h_offset = 1, .h_pixels = 640,
  179. .v_max_out = 480, .h_max_out = 640,
  180. }, {
  181. .name = "SECAM", .id = V4L2_STD_SECAM,
  182. .v_offset = 16, .v_field = 288,
  183. .h_offset = 1, .h_pixels = 720,
  184. .v_max_out = 576, .h_max_out = 768,
  185. }
  186. };
  187. /* this is only called for old HV-PCI6/Orion cards
  188. without eeprom */
  189. static int hexium_probe(struct saa7146_dev *dev)
  190. {
  191. struct hexium *hexium = NULL;
  192. union i2c_smbus_data data;
  193. int err = 0;
  194. DEB_EE("\n");
  195. /* there are no hexium orion cards with revision 0 saa7146s */
  196. if (0 == dev->revision) {
  197. return -EFAULT;
  198. }
  199. hexium = kzalloc(sizeof(struct hexium), GFP_KERNEL);
  200. if (NULL == hexium) {
  201. pr_err("hexium_probe: not enough kernel memory\n");
  202. return -ENOMEM;
  203. }
  204. /* enable i2c-port pins */
  205. saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
  206. saa7146_write(dev, DD1_INIT, 0x01000100);
  207. saa7146_write(dev, DD1_STREAM_B, 0x00000000);
  208. saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
  209. hexium->i2c_adapter = (struct i2c_adapter) {
  210. .name = "hexium orion",
  211. };
  212. saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
  213. if (i2c_add_adapter(&hexium->i2c_adapter) < 0) {
  214. DEB_S("cannot register i2c-device. skipping.\n");
  215. kfree(hexium);
  216. return -EFAULT;
  217. }
  218. /* set SAA7110 control GPIO 0 */
  219. saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTHI);
  220. /* set HWControl GPIO number 2 */
  221. saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
  222. mdelay(10);
  223. /* detect newer Hexium Orion cards by subsystem ids */
  224. if (0x17c8 == dev->pci->subsystem_vendor && 0x0101 == dev->pci->subsystem_device) {
  225. pr_info("device is a Hexium Orion w/ 1 SVHS + 3 BNC inputs\n");
  226. /* we store the pointer in our private data field */
  227. dev->ext_priv = hexium;
  228. hexium->type = HEXIUM_ORION_1SVHS_3BNC;
  229. return 0;
  230. }
  231. if (0x17c8 == dev->pci->subsystem_vendor && 0x2101 == dev->pci->subsystem_device) {
  232. pr_info("device is a Hexium Orion w/ 4 BNC inputs\n");
  233. /* we store the pointer in our private data field */
  234. dev->ext_priv = hexium;
  235. hexium->type = HEXIUM_ORION_4BNC;
  236. return 0;
  237. }
  238. /* check if this is an old hexium Orion card by looking at
  239. a saa7110 at address 0x4e */
  240. if (0 == (err = i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_READ, 0x00, I2C_SMBUS_BYTE_DATA, &data))) {
  241. pr_info("device is a Hexium HV-PCI6/Orion (old)\n");
  242. /* we store the pointer in our private data field */
  243. dev->ext_priv = hexium;
  244. hexium->type = HEXIUM_HV_PCI6_ORION;
  245. return 0;
  246. }
  247. i2c_del_adapter(&hexium->i2c_adapter);
  248. kfree(hexium);
  249. return -EFAULT;
  250. }
  251. /* bring hardware to a sane state. this has to be done, just in case someone
  252. wants to capture from this device before it has been properly initialized.
  253. the capture engine would badly fail, because no valid signal arrives on the
  254. saa7146, thus leading to timeouts and stuff. */
  255. static int hexium_init_done(struct saa7146_dev *dev)
  256. {
  257. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  258. union i2c_smbus_data data;
  259. int i = 0;
  260. DEB_D("hexium_init_done called\n");
  261. /* initialize the helper ics to useful values */
  262. for (i = 0; i < sizeof(hexium_saa7110); i++) {
  263. data.byte = hexium_saa7110[i];
  264. if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_WRITE, i, I2C_SMBUS_BYTE_DATA, &data)) {
  265. pr_err("failed for address 0x%02x\n", i);
  266. }
  267. }
  268. return 0;
  269. }
  270. static int hexium_set_input(struct hexium *hexium, int input)
  271. {
  272. union i2c_smbus_data data;
  273. int i = 0;
  274. DEB_D("\n");
  275. for (i = 0; i < 8; i++) {
  276. int adr = hexium_input_select[input].data[i].adr;
  277. data.byte = hexium_input_select[input].data[i].byte;
  278. if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_WRITE, adr, I2C_SMBUS_BYTE_DATA, &data)) {
  279. return -1;
  280. }
  281. pr_debug("%d: 0x%02x => 0x%02x\n", input, adr, data.byte);
  282. }
  283. return 0;
  284. }
  285. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  286. {
  287. DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
  288. if (i->index >= HEXIUM_INPUTS)
  289. return -EINVAL;
  290. memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));
  291. DEB_D("v4l2_ioctl: VIDIOC_ENUMINPUT %d\n", i->index);
  292. return 0;
  293. }
  294. static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
  295. {
  296. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  297. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  298. *input = hexium->cur_input;
  299. DEB_D("VIDIOC_G_INPUT: %d\n", *input);
  300. return 0;
  301. }
  302. static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
  303. {
  304. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  305. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  306. if (input >= HEXIUM_INPUTS)
  307. return -EINVAL;
  308. hexium->cur_input = input;
  309. hexium_set_input(hexium, input);
  310. return 0;
  311. }
  312. static struct saa7146_ext_vv vv_data;
  313. /* this function only gets called when the probing was successful */
  314. static int hexium_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
  315. {
  316. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  317. DEB_EE("\n");
  318. saa7146_vv_init(dev, &vv_data);
  319. vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
  320. vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
  321. vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
  322. if (0 != saa7146_register_device(&hexium->video_dev, dev, "hexium orion", VFL_TYPE_GRABBER)) {
  323. pr_err("cannot register capture v4l2 device. skipping.\n");
  324. return -1;
  325. }
  326. pr_err("found 'hexium orion' frame grabber-%d\n", hexium_num);
  327. hexium_num++;
  328. /* the rest */
  329. hexium->cur_input = 0;
  330. hexium_init_done(dev);
  331. return 0;
  332. }
  333. static int hexium_detach(struct saa7146_dev *dev)
  334. {
  335. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  336. DEB_EE("dev:%p\n", dev);
  337. saa7146_unregister_device(&hexium->video_dev, dev);
  338. saa7146_vv_release(dev);
  339. hexium_num--;
  340. i2c_del_adapter(&hexium->i2c_adapter);
  341. kfree(hexium);
  342. return 0;
  343. }
  344. static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *std)
  345. {
  346. return 0;
  347. }
  348. static struct saa7146_extension extension;
  349. static struct saa7146_pci_extension_data hexium_hv_pci6 = {
  350. .ext_priv = "Hexium HV-PCI6 / Orion",
  351. .ext = &extension,
  352. };
  353. static struct saa7146_pci_extension_data hexium_orion_1svhs_3bnc = {
  354. .ext_priv = "Hexium HV-PCI6 / Orion (1 SVHS/3 BNC)",
  355. .ext = &extension,
  356. };
  357. static struct saa7146_pci_extension_data hexium_orion_4bnc = {
  358. .ext_priv = "Hexium HV-PCI6 / Orion (4 BNC)",
  359. .ext = &extension,
  360. };
  361. static struct pci_device_id pci_tbl[] = {
  362. {
  363. .vendor = PCI_VENDOR_ID_PHILIPS,
  364. .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
  365. .subvendor = 0x0000,
  366. .subdevice = 0x0000,
  367. .driver_data = (unsigned long) &hexium_hv_pci6,
  368. },
  369. {
  370. .vendor = PCI_VENDOR_ID_PHILIPS,
  371. .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
  372. .subvendor = 0x17c8,
  373. .subdevice = 0x0101,
  374. .driver_data = (unsigned long) &hexium_orion_1svhs_3bnc,
  375. },
  376. {
  377. .vendor = PCI_VENDOR_ID_PHILIPS,
  378. .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
  379. .subvendor = 0x17c8,
  380. .subdevice = 0x2101,
  381. .driver_data = (unsigned long) &hexium_orion_4bnc,
  382. },
  383. {
  384. .vendor = 0,
  385. }
  386. };
  387. MODULE_DEVICE_TABLE(pci, pci_tbl);
  388. static struct saa7146_ext_vv vv_data = {
  389. .inputs = HEXIUM_INPUTS,
  390. .capabilities = 0,
  391. .stds = &hexium_standards[0],
  392. .num_stds = sizeof(hexium_standards) / sizeof(struct saa7146_standard),
  393. .std_callback = &std_callback,
  394. };
  395. static struct saa7146_extension extension = {
  396. .name = "hexium HV-PCI6 Orion",
  397. .flags = 0, // SAA7146_USE_I2C_IRQ,
  398. .pci_tbl = &pci_tbl[0],
  399. .module = THIS_MODULE,
  400. .probe = hexium_probe,
  401. .attach = hexium_attach,
  402. .detach = hexium_detach,
  403. .irq_mask = 0,
  404. .irq_func = NULL,
  405. };
  406. static int __init hexium_init_module(void)
  407. {
  408. if (0 != saa7146_register_extension(&extension)) {
  409. DEB_S("failed to register extension\n");
  410. return -ENODEV;
  411. }
  412. return 0;
  413. }
  414. static void __exit hexium_cleanup_module(void)
  415. {
  416. saa7146_unregister_extension(&extension);
  417. }
  418. module_init(hexium_init_module);
  419. module_exit(hexium_cleanup_module);
  420. MODULE_DESCRIPTION("video4linux-2 driver for Hexium Orion frame grabber cards");
  421. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  422. MODULE_LICENSE("GPL");