hexium_gemini.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. hexium_gemini.c - v4l2 driver for Hexium Gemini 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/drv-intf/saa7146_vv.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. static int debug;
  24. module_param(debug, int, 0);
  25. MODULE_PARM_DESC(debug, "debug verbosity");
  26. /* global variables */
  27. static int hexium_num;
  28. #define HEXIUM_GEMINI 4
  29. #define HEXIUM_GEMINI_DUAL 5
  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. #define HEXIUM_GEMINI_V_1_0 1
  49. #define HEXIUM_GEMINI_DUAL_V_1_0 2
  50. struct hexium
  51. {
  52. int type;
  53. struct video_device video_dev;
  54. struct i2c_adapter i2c_adapter;
  55. int cur_input; /* current input */
  56. v4l2_std_id cur_std; /* current standard */
  57. };
  58. /* Samsung KS0127B decoder default registers */
  59. static u8 hexium_ks0127b[0x100]={
  60. /*00*/ 0x00,0x52,0x30,0x40,0x01,0x0C,0x2A,0x10,
  61. /*08*/ 0x00,0x00,0x00,0x60,0x00,0x00,0x0F,0x06,
  62. /*10*/ 0x00,0x00,0xE4,0xC0,0x00,0x00,0x00,0x00,
  63. /*18*/ 0x14,0x9B,0xFE,0xFF,0xFC,0xFF,0x03,0x22,
  64. /*20*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  65. /*28*/ 0x00,0x00,0x00,0x00,0x00,0x2C,0x9B,0x00,
  66. /*30*/ 0x00,0x00,0x10,0x80,0x80,0x10,0x80,0x80,
  67. /*38*/ 0x01,0x04,0x00,0x00,0x00,0x29,0xC0,0x00,
  68. /*40*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  69. /*48*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  70. /*50*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  71. /*58*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  72. /*60*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  73. /*68*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  74. /*70*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  75. /*78*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  76. /*80*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  77. /*88*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  78. /*90*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  79. /*98*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  80. /*A0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  81. /*A8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  82. /*B0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  83. /*B8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  84. /*C0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  85. /*C8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  86. /*D0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  87. /*D8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  88. /*E0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  89. /*E8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  90. /*F0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  91. /*F8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  92. };
  93. static struct hexium_data hexium_pal[] = {
  94. { 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }
  95. };
  96. static struct hexium_data hexium_ntsc[] = {
  97. { 0x01, 0x53 }, { 0x12, 0x04 }, { 0x2D, 0x23 }, { 0x2E, 0x81 }, { -1 , 0xFF }
  98. };
  99. static struct hexium_data hexium_secam[] = {
  100. { 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }
  101. };
  102. static struct hexium_data hexium_input_select[] = {
  103. { 0x02, 0x60 },
  104. { 0x02, 0x64 },
  105. { 0x02, 0x61 },
  106. { 0x02, 0x65 },
  107. { 0x02, 0x62 },
  108. { 0x02, 0x66 },
  109. { 0x02, 0x68 },
  110. { 0x02, 0x69 },
  111. { 0x02, 0x6A },
  112. };
  113. /* fixme: h_offset = 0 for Hexium Gemini *Dual*, which
  114. are currently *not* supported*/
  115. static struct saa7146_standard hexium_standards[] = {
  116. {
  117. .name = "PAL", .id = V4L2_STD_PAL,
  118. .v_offset = 28, .v_field = 288,
  119. .h_offset = 1, .h_pixels = 680,
  120. .v_max_out = 576, .h_max_out = 768,
  121. }, {
  122. .name = "NTSC", .id = V4L2_STD_NTSC,
  123. .v_offset = 28, .v_field = 240,
  124. .h_offset = 1, .h_pixels = 640,
  125. .v_max_out = 480, .h_max_out = 640,
  126. }, {
  127. .name = "SECAM", .id = V4L2_STD_SECAM,
  128. .v_offset = 28, .v_field = 288,
  129. .h_offset = 1, .h_pixels = 720,
  130. .v_max_out = 576, .h_max_out = 768,
  131. }
  132. };
  133. /* bring hardware to a sane state. this has to be done, just in case someone
  134. wants to capture from this device before it has been properly initialized.
  135. the capture engine would badly fail, because no valid signal arrives on the
  136. saa7146, thus leading to timeouts and stuff. */
  137. static int hexium_init_done(struct saa7146_dev *dev)
  138. {
  139. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  140. union i2c_smbus_data data;
  141. int i = 0;
  142. DEB_D("hexium_init_done called\n");
  143. /* initialize the helper ics to useful values */
  144. for (i = 0; i < sizeof(hexium_ks0127b); i++) {
  145. data.byte = hexium_ks0127b[i];
  146. if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, i, I2C_SMBUS_BYTE_DATA, &data)) {
  147. pr_err("hexium_init_done() failed for address 0x%02x\n",
  148. i);
  149. }
  150. }
  151. return 0;
  152. }
  153. static int hexium_set_input(struct hexium *hexium, int input)
  154. {
  155. union i2c_smbus_data data;
  156. DEB_D("\n");
  157. data.byte = hexium_input_select[input].byte;
  158. if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, hexium_input_select[input].adr, I2C_SMBUS_BYTE_DATA, &data)) {
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. static int hexium_set_standard(struct hexium *hexium, struct hexium_data *vdec)
  164. {
  165. union i2c_smbus_data data;
  166. int i = 0;
  167. DEB_D("\n");
  168. while (vdec[i].adr != -1) {
  169. data.byte = vdec[i].byte;
  170. if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, vdec[i].adr, I2C_SMBUS_BYTE_DATA, &data)) {
  171. pr_err("hexium_init_done: hexium_set_standard() failed for address 0x%02x\n",
  172. i);
  173. return -1;
  174. }
  175. i++;
  176. }
  177. return 0;
  178. }
  179. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  180. {
  181. DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
  182. if (i->index >= HEXIUM_INPUTS)
  183. return -EINVAL;
  184. memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));
  185. DEB_D("v4l2_ioctl: VIDIOC_ENUMINPUT %d\n", i->index);
  186. return 0;
  187. }
  188. static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
  189. {
  190. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  191. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  192. *input = hexium->cur_input;
  193. DEB_D("VIDIOC_G_INPUT: %d\n", *input);
  194. return 0;
  195. }
  196. static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
  197. {
  198. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  199. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  200. DEB_EE("VIDIOC_S_INPUT %d\n", input);
  201. if (input >= HEXIUM_INPUTS)
  202. return -EINVAL;
  203. hexium->cur_input = input;
  204. hexium_set_input(hexium, input);
  205. return 0;
  206. }
  207. static struct saa7146_ext_vv vv_data;
  208. /* this function only gets called when the probing was successful */
  209. static int hexium_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
  210. {
  211. struct hexium *hexium;
  212. int ret;
  213. DEB_EE("\n");
  214. hexium = kzalloc(sizeof(*hexium), GFP_KERNEL);
  215. if (!hexium)
  216. return -ENOMEM;
  217. dev->ext_priv = hexium;
  218. /* enable i2c-port pins */
  219. saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
  220. strscpy(hexium->i2c_adapter.name, "hexium gemini",
  221. sizeof(hexium->i2c_adapter.name));
  222. saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
  223. if (i2c_add_adapter(&hexium->i2c_adapter) < 0) {
  224. DEB_S("cannot register i2c-device. skipping.\n");
  225. kfree(hexium);
  226. return -EFAULT;
  227. }
  228. /* set HWControl GPIO number 2 */
  229. saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
  230. saa7146_write(dev, DD1_INIT, 0x07000700);
  231. saa7146_write(dev, DD1_STREAM_B, 0x00000000);
  232. saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
  233. /* the rest */
  234. hexium->cur_input = 0;
  235. hexium_init_done(dev);
  236. hexium_set_standard(hexium, hexium_pal);
  237. hexium->cur_std = V4L2_STD_PAL;
  238. hexium_set_input(hexium, 0);
  239. hexium->cur_input = 0;
  240. saa7146_vv_init(dev, &vv_data);
  241. vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
  242. vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
  243. vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
  244. ret = saa7146_register_device(&hexium->video_dev, dev, "hexium gemini", VFL_TYPE_GRABBER);
  245. if (ret < 0) {
  246. pr_err("cannot register capture v4l2 device. skipping.\n");
  247. saa7146_vv_release(dev);
  248. i2c_del_adapter(&hexium->i2c_adapter);
  249. kfree(hexium);
  250. return ret;
  251. }
  252. pr_info("found 'hexium gemini' frame grabber-%d\n", hexium_num);
  253. hexium_num++;
  254. return 0;
  255. }
  256. static int hexium_detach(struct saa7146_dev *dev)
  257. {
  258. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  259. DEB_EE("dev:%p\n", dev);
  260. saa7146_unregister_device(&hexium->video_dev, dev);
  261. saa7146_vv_release(dev);
  262. hexium_num--;
  263. i2c_del_adapter(&hexium->i2c_adapter);
  264. kfree(hexium);
  265. return 0;
  266. }
  267. static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *std)
  268. {
  269. struct hexium *hexium = (struct hexium *) dev->ext_priv;
  270. if (V4L2_STD_PAL == std->id) {
  271. hexium_set_standard(hexium, hexium_pal);
  272. hexium->cur_std = V4L2_STD_PAL;
  273. return 0;
  274. } else if (V4L2_STD_NTSC == std->id) {
  275. hexium_set_standard(hexium, hexium_ntsc);
  276. hexium->cur_std = V4L2_STD_NTSC;
  277. return 0;
  278. } else if (V4L2_STD_SECAM == std->id) {
  279. hexium_set_standard(hexium, hexium_secam);
  280. hexium->cur_std = V4L2_STD_SECAM;
  281. return 0;
  282. }
  283. return -1;
  284. }
  285. static struct saa7146_extension hexium_extension;
  286. static struct saa7146_pci_extension_data hexium_gemini_4bnc = {
  287. .ext_priv = "Hexium Gemini (4 BNC)",
  288. .ext = &hexium_extension,
  289. };
  290. static struct saa7146_pci_extension_data hexium_gemini_dual_4bnc = {
  291. .ext_priv = "Hexium Gemini Dual (4 BNC)",
  292. .ext = &hexium_extension,
  293. };
  294. static const struct pci_device_id pci_tbl[] = {
  295. {
  296. .vendor = PCI_VENDOR_ID_PHILIPS,
  297. .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
  298. .subvendor = 0x17c8,
  299. .subdevice = 0x2401,
  300. .driver_data = (unsigned long) &hexium_gemini_4bnc,
  301. },
  302. {
  303. .vendor = PCI_VENDOR_ID_PHILIPS,
  304. .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
  305. .subvendor = 0x17c8,
  306. .subdevice = 0x2402,
  307. .driver_data = (unsigned long) &hexium_gemini_dual_4bnc,
  308. },
  309. {
  310. .vendor = 0,
  311. }
  312. };
  313. MODULE_DEVICE_TABLE(pci, pci_tbl);
  314. static struct saa7146_ext_vv vv_data = {
  315. .inputs = HEXIUM_INPUTS,
  316. .capabilities = 0,
  317. .stds = &hexium_standards[0],
  318. .num_stds = ARRAY_SIZE(hexium_standards),
  319. .std_callback = &std_callback,
  320. };
  321. static struct saa7146_extension hexium_extension = {
  322. .name = "hexium gemini",
  323. .flags = SAA7146_USE_I2C_IRQ,
  324. .pci_tbl = &pci_tbl[0],
  325. .module = THIS_MODULE,
  326. .attach = hexium_attach,
  327. .detach = hexium_detach,
  328. .irq_mask = 0,
  329. .irq_func = NULL,
  330. };
  331. static int __init hexium_init_module(void)
  332. {
  333. if (0 != saa7146_register_extension(&hexium_extension)) {
  334. DEB_S("failed to register extension\n");
  335. return -ENODEV;
  336. }
  337. return 0;
  338. }
  339. static void __exit hexium_cleanup_module(void)
  340. {
  341. saa7146_unregister_extension(&hexium_extension);
  342. }
  343. module_init(hexium_init_module);
  344. module_exit(hexium_cleanup_module);
  345. MODULE_DESCRIPTION("video4linux-2 driver for Hexium Gemini frame grabber cards");
  346. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  347. MODULE_LICENSE("GPL");