dpc7146.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. dpc7146.c - v4l2 driver for the dpc7146 demonstration board
  3. Copyright (C) 2000-2003 Michael Hunold <michael@mihu.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #define DEBUG_VARIABLE debug
  17. #include <media/saa7146_vv.h>
  18. #include <linux/video_decoder.h> /* for saa7111a */
  19. #define I2C_SAA7111A 0x24
  20. /* All unused bytes are reserverd. */
  21. #define SAA711X_CHIP_VERSION 0x00
  22. #define SAA711X_ANALOG_INPUT_CONTROL_1 0x02
  23. #define SAA711X_ANALOG_INPUT_CONTROL_2 0x03
  24. #define SAA711X_ANALOG_INPUT_CONTROL_3 0x04
  25. #define SAA711X_ANALOG_INPUT_CONTROL_4 0x05
  26. #define SAA711X_HORIZONTAL_SYNC_START 0x06
  27. #define SAA711X_HORIZONTAL_SYNC_STOP 0x07
  28. #define SAA711X_SYNC_CONTROL 0x08
  29. #define SAA711X_LUMINANCE_CONTROL 0x09
  30. #define SAA711X_LUMINANCE_BRIGHTNESS 0x0A
  31. #define SAA711X_LUMINANCE_CONTRAST 0x0B
  32. #define SAA711X_CHROMA_SATURATION 0x0C
  33. #define SAA711X_CHROMA_HUE_CONTROL 0x0D
  34. #define SAA711X_CHROMA_CONTROL 0x0E
  35. #define SAA711X_FORMAT_DELAY_CONTROL 0x10
  36. #define SAA711X_OUTPUT_CONTROL_1 0x11
  37. #define SAA711X_OUTPUT_CONTROL_2 0x12
  38. #define SAA711X_OUTPUT_CONTROL_3 0x13
  39. #define SAA711X_V_GATE_1_START 0x15
  40. #define SAA711X_V_GATE_1_STOP 0x16
  41. #define SAA711X_V_GATE_1_MSB 0x17
  42. #define SAA711X_TEXT_SLICER_STATUS 0x1A
  43. #define SAA711X_DECODED_BYTES_OF_TS_1 0x1B
  44. #define SAA711X_DECODED_BYTES_OF_TS_2 0x1C
  45. #define SAA711X_STATUS_BYTE 0x1F
  46. #define DPC_BOARD_CAN_DO_VBI(dev) (dev->revision != 0)
  47. static int debug = 0;
  48. module_param(debug, int, 0);
  49. MODULE_PARM_DESC(debug, "debug verbosity");
  50. static int dpc_num = 0;
  51. #define DPC_INPUTS 2
  52. static struct v4l2_input dpc_inputs[DPC_INPUTS] = {
  53. { 0, "Port A", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0 },
  54. { 1, "Port B", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0 },
  55. };
  56. #define DPC_AUDIOS 0
  57. static struct saa7146_extension_ioctls ioctls[] = {
  58. { VIDIOC_G_INPUT, SAA7146_EXCLUSIVE },
  59. { VIDIOC_S_INPUT, SAA7146_EXCLUSIVE },
  60. { VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE },
  61. { VIDIOC_S_STD, SAA7146_AFTER },
  62. { 0, 0 }
  63. };
  64. struct dpc
  65. {
  66. struct video_device *video_dev;
  67. struct video_device *vbi_dev;
  68. struct i2c_adapter i2c_adapter;
  69. struct i2c_client *saa7111a;
  70. int cur_input; /* current input */
  71. };
  72. /* fixme: add vbi stuff here */
  73. static int dpc_probe(struct saa7146_dev* dev)
  74. {
  75. struct dpc* dpc = NULL;
  76. struct i2c_client *client;
  77. struct list_head *item;
  78. dpc = kzalloc(sizeof(struct dpc), GFP_KERNEL);
  79. if( NULL == dpc ) {
  80. printk("dpc_v4l2.o: dpc_probe: not enough kernel memory.\n");
  81. return -ENOMEM;
  82. }
  83. /* FIXME: enable i2c-port pins, video-port-pins
  84. video port pins should be enabled here ?! */
  85. saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
  86. dpc->i2c_adapter = (struct i2c_adapter) {
  87. .class = I2C_CLASS_TV_ANALOG,
  88. .name = "dpc7146",
  89. };
  90. saa7146_i2c_adapter_prepare(dev, &dpc->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
  91. if(i2c_add_adapter(&dpc->i2c_adapter) < 0) {
  92. DEB_S(("cannot register i2c-device. skipping.\n"));
  93. kfree(dpc);
  94. return -EFAULT;
  95. }
  96. /* loop through all i2c-devices on the bus and look who is there */
  97. list_for_each(item,&dpc->i2c_adapter.clients) {
  98. client = list_entry(item, struct i2c_client, list);
  99. if( I2C_SAA7111A == client->addr )
  100. dpc->saa7111a = client;
  101. }
  102. /* check if all devices are present */
  103. if( 0 == dpc->saa7111a ) {
  104. DEB_D(("dpc_v4l2.o: dpc_attach failed for this device.\n"));
  105. i2c_del_adapter(&dpc->i2c_adapter);
  106. kfree(dpc);
  107. return -ENODEV;
  108. }
  109. /* all devices are present, probe was successful */
  110. DEB_D(("dpc_v4l2.o: dpc_probe succeeded for this device.\n"));
  111. /* we store the pointer in our private data field */
  112. dev->ext_priv = dpc;
  113. return 0;
  114. }
  115. /* bring hardware to a sane state. this has to be done, just in case someone
  116. wants to capture from this device before it has been properly initialized.
  117. the capture engine would badly fail, because no valid signal arrives on the
  118. saa7146, thus leading to timeouts and stuff. */
  119. static int dpc_init_done(struct saa7146_dev* dev)
  120. {
  121. struct dpc* dpc = (struct dpc*)dev->ext_priv;
  122. DEB_D(("dpc_v4l2.o: dpc_init_done called.\n"));
  123. /* initialize the helper ics to useful values */
  124. i2c_smbus_write_byte_data(dpc->saa7111a, 0x00, 0x11);
  125. i2c_smbus_write_byte_data(dpc->saa7111a, 0x02, 0xc0);
  126. i2c_smbus_write_byte_data(dpc->saa7111a, 0x03, 0x30);
  127. i2c_smbus_write_byte_data(dpc->saa7111a, 0x04, 0x00);
  128. i2c_smbus_write_byte_data(dpc->saa7111a, 0x05, 0x00);
  129. i2c_smbus_write_byte_data(dpc->saa7111a, 0x06, 0xde);
  130. i2c_smbus_write_byte_data(dpc->saa7111a, 0x07, 0xad);
  131. i2c_smbus_write_byte_data(dpc->saa7111a, 0x08, 0xa8);
  132. i2c_smbus_write_byte_data(dpc->saa7111a, 0x09, 0x00);
  133. i2c_smbus_write_byte_data(dpc->saa7111a, 0x0a, 0x80);
  134. i2c_smbus_write_byte_data(dpc->saa7111a, 0x0b, 0x47);
  135. i2c_smbus_write_byte_data(dpc->saa7111a, 0x0c, 0x40);
  136. i2c_smbus_write_byte_data(dpc->saa7111a, 0x0d, 0x00);
  137. i2c_smbus_write_byte_data(dpc->saa7111a, 0x0e, 0x03);
  138. i2c_smbus_write_byte_data(dpc->saa7111a, 0x10, 0xd0);
  139. i2c_smbus_write_byte_data(dpc->saa7111a, 0x11, 0x1c);
  140. i2c_smbus_write_byte_data(dpc->saa7111a, 0x12, 0xc1);
  141. i2c_smbus_write_byte_data(dpc->saa7111a, 0x13, 0x30);
  142. i2c_smbus_write_byte_data(dpc->saa7111a, 0x1f, 0x81);
  143. return 0;
  144. }
  145. static struct saa7146_ext_vv vv_data;
  146. /* this function only gets called when the probing was successful */
  147. static int dpc_attach(struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
  148. {
  149. struct dpc* dpc = (struct dpc*)dev->ext_priv;
  150. DEB_D(("dpc_v4l2.o: dpc_attach called.\n"));
  151. /* checking for i2c-devices can be omitted here, because we
  152. already did this in "dpc_vl42_probe" */
  153. saa7146_vv_init(dev,&vv_data);
  154. if( 0 != saa7146_register_device(&dpc->video_dev, dev, "dpc", VFL_TYPE_GRABBER)) {
  155. ERR(("cannot register capture v4l2 device. skipping.\n"));
  156. return -1;
  157. }
  158. /* initialization stuff (vbi) (only for revision > 0 and for extensions which want it)*/
  159. if( 0 != DPC_BOARD_CAN_DO_VBI(dev)) {
  160. if( 0 != saa7146_register_device(&dpc->vbi_dev, dev, "dpc", VFL_TYPE_VBI)) {
  161. ERR(("cannot register vbi v4l2 device. skipping.\n"));
  162. }
  163. }
  164. i2c_use_client(dpc->saa7111a);
  165. printk("dpc: found 'dpc7146 demonstration board'-%d.\n",dpc_num);
  166. dpc_num++;
  167. /* the rest */
  168. dpc->cur_input = 0;
  169. dpc_init_done(dev);
  170. return 0;
  171. }
  172. static int dpc_detach(struct saa7146_dev* dev)
  173. {
  174. struct dpc* dpc = (struct dpc*)dev->ext_priv;
  175. DEB_EE(("dev:%p\n",dev));
  176. i2c_release_client(dpc->saa7111a);
  177. saa7146_unregister_device(&dpc->video_dev,dev);
  178. if( 0 != DPC_BOARD_CAN_DO_VBI(dev)) {
  179. saa7146_unregister_device(&dpc->vbi_dev,dev);
  180. }
  181. saa7146_vv_release(dev);
  182. dpc_num--;
  183. i2c_del_adapter(&dpc->i2c_adapter);
  184. kfree(dpc);
  185. return 0;
  186. }
  187. #ifdef axa
  188. int dpc_vbi_bypass(struct saa7146_dev* dev)
  189. {
  190. struct dpc* dpc = (struct dpc*)dev->ext_priv;
  191. int i = 1;
  192. /* switch bypass in saa7111a */
  193. if ( 0 != dpc->saa7111a->driver->command(dpc->saa7111a,SAA711X_VBI_BYPASS, &i)) {
  194. printk("dpc_v4l2.o: VBI_BYPASS: could not address saa7111a.\n");
  195. return -1;
  196. }
  197. return 0;
  198. }
  199. #endif
  200. static int dpc_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
  201. {
  202. struct saa7146_dev *dev = fh->dev;
  203. struct dpc* dpc = (struct dpc*)dev->ext_priv;
  204. /*
  205. struct saa7146_vv *vv = dev->vv_data;
  206. */
  207. switch(cmd)
  208. {
  209. case VIDIOC_ENUMINPUT:
  210. {
  211. struct v4l2_input *i = arg;
  212. DEB_EE(("VIDIOC_ENUMINPUT %d.\n",i->index));
  213. if( i->index < 0 || i->index >= DPC_INPUTS) {
  214. return -EINVAL;
  215. }
  216. memcpy(i, &dpc_inputs[i->index], sizeof(struct v4l2_input));
  217. DEB_D(("dpc_v4l2.o: v4l2_ioctl: VIDIOC_ENUMINPUT %d.\n",i->index));
  218. return 0;
  219. }
  220. case VIDIOC_G_INPUT:
  221. {
  222. int *input = (int *)arg;
  223. *input = dpc->cur_input;
  224. DEB_D(("dpc_v4l2.o: VIDIOC_G_INPUT: %d\n",*input));
  225. return 0;
  226. }
  227. case VIDIOC_S_INPUT:
  228. {
  229. int input = *(int *)arg;
  230. if (input < 0 || input >= DPC_INPUTS) {
  231. return -EINVAL;
  232. }
  233. dpc->cur_input = input;
  234. /* fixme: switch input here, switch audio, too! */
  235. // saa7146_set_hps_source_and_sync(dev, input_port_selection[input].hps_source, input_port_selection[input].hps_sync);
  236. printk("dpc_v4l2.o: VIDIOC_S_INPUT: fixme switch input.\n");
  237. return 0;
  238. }
  239. default:
  240. /*
  241. DEB_D(("dpc_v4l2.o: v4l2_ioctl does not handle this ioctl.\n"));
  242. */
  243. return -ENOIOCTLCMD;
  244. }
  245. return 0;
  246. }
  247. static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
  248. {
  249. return 0;
  250. }
  251. static struct saa7146_standard standard[] = {
  252. {
  253. .name = "PAL", .id = V4L2_STD_PAL,
  254. .v_offset = 0x17, .v_field = 288,
  255. .h_offset = 0x14, .h_pixels = 680,
  256. .v_max_out = 576, .h_max_out = 768,
  257. }, {
  258. .name = "NTSC", .id = V4L2_STD_NTSC,
  259. .v_offset = 0x16, .v_field = 240,
  260. .h_offset = 0x06, .h_pixels = 708,
  261. .v_max_out = 480, .h_max_out = 640,
  262. }, {
  263. .name = "SECAM", .id = V4L2_STD_SECAM,
  264. .v_offset = 0x14, .v_field = 288,
  265. .h_offset = 0x14, .h_pixels = 720,
  266. .v_max_out = 576, .h_max_out = 768,
  267. }
  268. };
  269. static struct saa7146_extension extension;
  270. static struct saa7146_pci_extension_data dpc = {
  271. .ext_priv = "Multimedia eXtension Board",
  272. .ext = &extension,
  273. };
  274. static struct pci_device_id pci_tbl[] = {
  275. {
  276. .vendor = PCI_VENDOR_ID_PHILIPS,
  277. .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
  278. .subvendor = 0x0000,
  279. .subdevice = 0x0000,
  280. .driver_data = (unsigned long)&dpc,
  281. }, {
  282. .vendor = 0,
  283. }
  284. };
  285. MODULE_DEVICE_TABLE(pci, pci_tbl);
  286. static struct saa7146_ext_vv vv_data = {
  287. .inputs = DPC_INPUTS,
  288. .capabilities = V4L2_CAP_VBI_CAPTURE,
  289. .stds = &standard[0],
  290. .num_stds = sizeof(standard)/sizeof(struct saa7146_standard),
  291. .std_callback = &std_callback,
  292. .ioctls = &ioctls[0],
  293. .ioctl = dpc_ioctl,
  294. };
  295. static struct saa7146_extension extension = {
  296. .name = "dpc7146 demonstration board",
  297. .flags = SAA7146_USE_I2C_IRQ,
  298. .pci_tbl = &pci_tbl[0],
  299. .module = THIS_MODULE,
  300. .probe = dpc_probe,
  301. .attach = dpc_attach,
  302. .detach = dpc_detach,
  303. .irq_mask = 0,
  304. .irq_func = NULL,
  305. };
  306. static int __init dpc_init_module(void)
  307. {
  308. if( 0 != saa7146_register_extension(&extension)) {
  309. DEB_S(("failed to register extension.\n"));
  310. return -ENODEV;
  311. }
  312. return 0;
  313. }
  314. static void __exit dpc_cleanup_module(void)
  315. {
  316. saa7146_unregister_extension(&extension);
  317. }
  318. module_init(dpc_init_module);
  319. module_exit(dpc_cleanup_module);
  320. MODULE_DESCRIPTION("video4linux-2 driver for the 'dpc7146 demonstration board'");
  321. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  322. MODULE_LICENSE("GPL");