plat_ctrl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. *gt2005 - This code emulates a real video device with v4l2 api
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD Licence, GNU General Public License
  7. * as published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version
  9. */
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/wait.h>
  16. #include <linux/slab.h>
  17. #include <linux/mm.h>
  18. #include <linux/ioport.h>
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/pci.h>
  22. #include <linux/random.h>
  23. #include <linux/version.h>
  24. #include <linux/mutex.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kthread.h>
  29. #include <linux/highmem.h>
  30. #include <linux/freezer.h>
  31. #include <media/videobuf-vmalloc.h>
  32. #include <linux/i2c.h>
  33. /****************************************************************
  34. * i2c functions
  35. * **************************************************************/
  36. static int camera_read_buff(struct i2c_client *client,char *buf, int addr_len, int data_len)
  37. {
  38. int i2c_flag = -1;
  39. struct i2c_msg msgs[] = {
  40. {
  41. .addr = client->addr,
  42. .flags = 0,
  43. .len = addr_len,
  44. .buf = buf,
  45. },
  46. {
  47. .addr = client->addr,
  48. .flags = I2C_M_RD,
  49. .len = data_len,
  50. .buf = buf,
  51. }
  52. };
  53. i2c_flag = i2c_transfer(client->adapter, msgs, 2);
  54. return i2c_flag;
  55. }
  56. static int camera_write_buff(struct i2c_client *client,char *buf, int len)
  57. {
  58. struct i2c_msg msg[] = {
  59. {
  60. .addr = client->addr,
  61. .flags = 0, //|I2C_M_TEN,
  62. .len = len,
  63. .buf = buf,
  64. }
  65. };
  66. if (i2c_transfer(client->adapter, msg, 1) < 0)
  67. {
  68. return -1;
  69. }
  70. else
  71. return 0;
  72. }
  73. int i2c_get_byte(struct i2c_client *client,unsigned short addr)
  74. {
  75. unsigned char buff[4];
  76. buff[0] = (unsigned char)((addr >> 8) & 0xff);
  77. buff[1] = (unsigned char)(addr & 0xff);
  78. if (camera_read_buff(client, buff, 2, 1) <0)
  79. return -1;
  80. return buff[0];
  81. }
  82. int i2c_get_byte_add8(struct i2c_client *client,unsigned short addr)
  83. {
  84. unsigned char buff[4];
  85. buff[0] = (unsigned char)(addr & 0xff);
  86. if (camera_read_buff(client, buff, 1, 1) <0)
  87. return -1;
  88. return buff[0];
  89. }
  90. int i2c_get_word(struct i2c_client *client,unsigned short addr)
  91. {
  92. unsigned short data;
  93. unsigned char buff[4];
  94. buff[0] = (unsigned char)((addr >> 8) & 0xff);
  95. buff[1] = (unsigned char)(addr & 0xff);
  96. if (camera_read_buff(client, buff, 2, 2) <0)
  97. return -1;
  98. else
  99. {
  100. data = buff[1];
  101. data = (data << 8) | buff[0];
  102. return data;
  103. }
  104. }
  105. int i2c_put_byte(struct i2c_client *client, unsigned short addr, unsigned char data)
  106. {
  107. unsigned char buff[4];
  108. buff[0] = (unsigned char)((addr >> 8) & 0xff);
  109. buff[1] = (unsigned char)(addr & 0xff);
  110. buff[2] = data;
  111. if (camera_write_buff(client, buff, 3) <0)
  112. return -1;
  113. return 0;
  114. }
  115. int i2c_put_word(struct i2c_client *client, unsigned short addr, unsigned short data)
  116. {
  117. unsigned char buff[4];
  118. buff[0] = (unsigned char)((addr >> 8) & 0xff);
  119. buff[1] = (unsigned char)(addr & 0xff);
  120. buff[2] = (unsigned char)(data & 0xff);
  121. buff[3] = (unsigned char)((data >> 8) & 0xff);
  122. if (camera_write_buff(client, buff, 4) <0)
  123. return -1;
  124. return 0;
  125. }
  126. int i2c_put_byte_add8(struct i2c_client *client,char *buf, int len)
  127. {
  128. if (camera_write_buff(client, buf, len) <0)
  129. return -1;
  130. return 0;
  131. }