bt832.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Driver for Bt832 CMOS Camera Video Processor
  2. i2c-addresses: 0x88 or 0x8a
  3. The BT832 interfaces to a Quartzsight Digital Camera (352x288, 25 or 30 fps)
  4. via a 9 pin connector ( 4-wire SDATA, 2-wire i2c, SCLK, VCC, GND).
  5. It outputs an 8-bit 4:2:2 YUV or YCrCb video signal which can be directly
  6. connected to bt848/bt878 GPIO pins on this purpose.
  7. (see: VLSI Vision Ltd. www.vvl.co.uk for camera datasheets)
  8. Supported Cards:
  9. - Pixelview Rev.4E: 0x8a
  10. GPIO 0x400000 toggles Bt832 RESET, and the chip changes to i2c 0x88 !
  11. (c) Gunther Mayer, 2002
  12. STATUS:
  13. - detect chip and hexdump
  14. - reset chip and leave low power mode
  15. - detect camera present
  16. TODO:
  17. - make it work (find correct setup for Bt832 and Bt878)
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/i2c.h>
  22. #include <linux/types.h>
  23. #include <linux/videodev.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <media/v4l2-common.h>
  28. #include "bttv.h"
  29. #include "bt832.h"
  30. MODULE_LICENSE("GPL");
  31. /* Addresses to scan */
  32. static unsigned short normal_i2c[] = { I2C_ADDR_BT832_ALT1>>1, I2C_ADDR_BT832_ALT2>>1,
  33. I2C_CLIENT_END };
  34. I2C_CLIENT_INSMOD;
  35. int debug; /* debug output */
  36. module_param(debug, int, 0644);
  37. /* ---------------------------------------------------------------------- */
  38. static int bt832_detach(struct i2c_client *client);
  39. static struct i2c_driver driver;
  40. static struct i2c_client client_template;
  41. struct bt832 {
  42. struct i2c_client client;
  43. };
  44. int bt832_hexdump(struct i2c_client *i2c_client_s, unsigned char *buf)
  45. {
  46. int i,rc;
  47. buf[0]=0x80; // start at register 0 with auto-increment
  48. if (1 != (rc = i2c_master_send(i2c_client_s,buf,1)))
  49. v4l_err(i2c_client_s,"i2c i/o error: rc == %d (should be 1)\n",rc);
  50. for(i=0;i<65;i++)
  51. buf[i]=0;
  52. if (65 != (rc=i2c_master_recv(i2c_client_s,buf,65)))
  53. v4l_err(i2c_client_s,"i2c i/o error: rc == %d (should be 65)\n",rc);
  54. // Note: On READ the first byte is the current index
  55. // (e.g. 0x80, what we just wrote)
  56. if(debug>1) {
  57. int i;
  58. v4l_dbg(2, debug,i2c_client_s,"hexdump:");
  59. for(i=1;i<65;i++) {
  60. if(i!=1) {
  61. if(((i-1)%8)==0) printk(" ");
  62. if(((i-1)%16)==0) {
  63. printk("\n");
  64. v4l_dbg(2, debug,i2c_client_s,"hexdump:");
  65. }
  66. }
  67. printk(" %02x",buf[i]);
  68. }
  69. printk("\n");
  70. }
  71. return 0;
  72. }
  73. // Return: 1 (is a bt832), 0 (No bt832 here)
  74. int bt832_init(struct i2c_client *i2c_client_s)
  75. {
  76. unsigned char *buf;
  77. int rc;
  78. buf=kmalloc(65,GFP_KERNEL);
  79. bt832_hexdump(i2c_client_s,buf);
  80. if(buf[0x40] != 0x31) {
  81. v4l_err(i2c_client_s,"This i2c chip is no bt832 (id=%02x). Detaching.\n",buf[0x40]);
  82. kfree(buf);
  83. return 0;
  84. }
  85. v4l_err(i2c_client_s,"Write 0 tp VPSTATUS\n");
  86. buf[0]=BT832_VP_STATUS; // Reg.52
  87. buf[1]= 0x00;
  88. if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
  89. v4l_err(i2c_client_s,"i2c i/o error VPS: rc == %d (should be 2)\n",rc);
  90. bt832_hexdump(i2c_client_s,buf);
  91. // Leave low power mode:
  92. v4l_err(i2c_client_s,"leave low power mode.\n");
  93. buf[0]=BT832_CAM_SETUP0; //0x39 57
  94. buf[1]=0x08;
  95. if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
  96. v4l_err(i2c_client_s,"i2c i/o error LLPM: rc == %d (should be 2)\n",rc);
  97. bt832_hexdump(i2c_client_s,buf);
  98. v4l_info(i2c_client_s,"Write 0 tp VPSTATUS\n");
  99. buf[0]=BT832_VP_STATUS; // Reg.52
  100. buf[1]= 0x00;
  101. if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
  102. v4l_err(i2c_client_s,"i2c i/o error VPS: rc == %d (should be 2)\n",rc);
  103. bt832_hexdump(i2c_client_s,buf);
  104. // Enable Output
  105. v4l_info(i2c_client_s,"Enable Output\n");
  106. buf[0]=BT832_VP_CONTROL1; // Reg.40
  107. buf[1]= 0x27 & (~0x01); // Default | !skip
  108. if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
  109. v4l_err(i2c_client_s,"i2c i/o error EO: rc == %d (should be 2)\n",rc);
  110. bt832_hexdump(i2c_client_s,buf);
  111. // for testing (even works when no camera attached)
  112. v4l_info(i2c_client_s,"*** Generate NTSC M Bars *****\n");
  113. buf[0]=BT832_VP_TESTCONTROL0; // Reg. 42
  114. buf[1]=3; // Generate NTSC System M bars, Generate Frame timing internally
  115. if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
  116. v4l_info(i2c_client_s,"i2c i/o error MBAR: rc == %d (should be 2)\n",rc);
  117. v4l_info(i2c_client_s,"Camera Present: %s\n",
  118. (buf[1+BT832_CAM_STATUS] & BT832_56_CAMERA_PRESENT) ? "yes":"no");
  119. bt832_hexdump(i2c_client_s,buf);
  120. kfree(buf);
  121. return 1;
  122. }
  123. static int bt832_attach(struct i2c_adapter *adap, int addr, int kind)
  124. {
  125. struct bt832 *t;
  126. client_template.adapter = adap;
  127. client_template.addr = addr;
  128. if (NULL == (t = kzalloc(sizeof(*t), GFP_KERNEL)))
  129. return -ENOMEM;
  130. t->client = client_template;
  131. i2c_set_clientdata(&t->client, t);
  132. i2c_attach_client(&t->client);
  133. v4l_info(&t->client,"chip found @ 0x%x\n", addr<<1);
  134. if(! bt832_init(&t->client)) {
  135. bt832_detach(&t->client);
  136. return -1;
  137. }
  138. return 0;
  139. }
  140. static int bt832_probe(struct i2c_adapter *adap)
  141. {
  142. if (adap->class & I2C_CLASS_TV_ANALOG)
  143. return i2c_probe(adap, &addr_data, bt832_attach);
  144. return 0;
  145. }
  146. static int bt832_detach(struct i2c_client *client)
  147. {
  148. struct bt832 *t = i2c_get_clientdata(client);
  149. v4l_info(&t->client,"dettach\n");
  150. i2c_detach_client(client);
  151. kfree(t);
  152. return 0;
  153. }
  154. static int
  155. bt832_command(struct i2c_client *client, unsigned int cmd, void *arg)
  156. {
  157. struct bt832 *t = i2c_get_clientdata(client);
  158. if (debug>1)
  159. v4l_i2c_print_ioctl(&t->client,cmd);
  160. switch (cmd) {
  161. case BT832_HEXDUMP: {
  162. unsigned char *buf;
  163. buf=kmalloc(65,GFP_KERNEL);
  164. bt832_hexdump(&t->client,buf);
  165. kfree(buf);
  166. }
  167. break;
  168. case BT832_REATTACH:
  169. v4l_info(&t->client,"re-attach\n");
  170. i2c_del_driver(&driver);
  171. i2c_add_driver(&driver);
  172. break;
  173. }
  174. return 0;
  175. }
  176. /* ----------------------------------------------------------------------- */
  177. static struct i2c_driver driver = {
  178. .driver = {
  179. .name = "bt832",
  180. },
  181. .id = 0, /* FIXME */
  182. .attach_adapter = bt832_probe,
  183. .detach_client = bt832_detach,
  184. .command = bt832_command,
  185. };
  186. static struct i2c_client client_template =
  187. {
  188. .name = "bt832",
  189. .driver = &driver,
  190. };
  191. static int __init bt832_init_module(void)
  192. {
  193. return i2c_add_driver(&driver);
  194. }
  195. static void __exit bt832_cleanup_module(void)
  196. {
  197. i2c_del_driver(&driver);
  198. }
  199. module_init(bt832_init_module);
  200. module_exit(bt832_cleanup_module);
  201. /*
  202. * Overrides for Emacs so that we follow Linus's tabbing style.
  203. * ---------------------------------------------------------------------------
  204. * Local variables:
  205. * c-basic-offset: 8
  206. * End:
  207. */