m5602_po1030.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Driver for the po1030 sensor
  3. *
  4. * Copyright (c) 2008 Erik Andrén
  5. * Copyright (c) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
  6. * Copyright (c) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
  7. *
  8. * Portions of code to USB interface and ALi driver software,
  9. * Copyright (c) 2006 Willem Duinker
  10. * v4l2 interface modeled after the V4L2 driver
  11. * for SN9C10x PC Camera Controllers
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation, version 2.
  16. *
  17. */
  18. #include "m5602_po1030.h"
  19. static void po1030_dump_registers(struct sd *sd);
  20. int po1030_probe(struct sd *sd)
  21. {
  22. u8 prod_id = 0, ver_id = 0, i;
  23. if (force_sensor) {
  24. if (force_sensor == PO1030_SENSOR) {
  25. info("Forcing a %s sensor", po1030.name);
  26. goto sensor_found;
  27. }
  28. /* If we want to force another sensor, don't try to probe this
  29. * one */
  30. return -ENODEV;
  31. }
  32. info("Probing for a po1030 sensor");
  33. /* Run the pre-init to actually probe the unit */
  34. for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
  35. u8 data = preinit_po1030[i][2];
  36. if (preinit_po1030[i][0] == SENSOR)
  37. m5602_write_sensor(sd,
  38. preinit_po1030[i][1], &data, 1);
  39. else
  40. m5602_write_bridge(sd, preinit_po1030[i][1], data);
  41. }
  42. if (m5602_read_sensor(sd, 0x3, &prod_id, 1))
  43. return -ENODEV;
  44. if (m5602_read_sensor(sd, 0x4, &ver_id, 1))
  45. return -ENODEV;
  46. if ((prod_id == 0x02) && (ver_id == 0xef)) {
  47. info("Detected a po1030 sensor");
  48. goto sensor_found;
  49. }
  50. return -ENODEV;
  51. sensor_found:
  52. sd->gspca_dev.cam.cam_mode = po1030.modes;
  53. sd->gspca_dev.cam.nmodes = po1030.nmodes;
  54. sd->desc->ctrls = po1030.ctrls;
  55. sd->desc->nctrls = po1030.nctrls;
  56. return 0;
  57. }
  58. int po1030_init(struct sd *sd)
  59. {
  60. int i, err = 0;
  61. /* Init the sensor */
  62. for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
  63. u8 data[2] = {0x00, 0x00};
  64. switch (init_po1030[i][0]) {
  65. case BRIDGE:
  66. err = m5602_write_bridge(sd,
  67. init_po1030[i][1],
  68. init_po1030[i][2]);
  69. break;
  70. case SENSOR:
  71. data[0] = init_po1030[i][2];
  72. err = m5602_write_sensor(sd,
  73. init_po1030[i][1], data, 1);
  74. break;
  75. default:
  76. info("Invalid stream command, exiting init");
  77. return -EINVAL;
  78. }
  79. }
  80. if (dump_sensor)
  81. po1030_dump_registers(sd);
  82. return err;
  83. }
  84. int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  85. {
  86. struct sd *sd = (struct sd *) gspca_dev;
  87. u8 i2c_data;
  88. int err;
  89. err = m5602_read_sensor(sd, PO1030_REG_INTEGLINES_H,
  90. &i2c_data, 1);
  91. if (err < 0)
  92. goto out;
  93. *val = (i2c_data << 8);
  94. err = m5602_read_sensor(sd, PO1030_REG_INTEGLINES_M,
  95. &i2c_data, 1);
  96. *val |= i2c_data;
  97. PDEBUG(D_V4L2, "Exposure read as %d", *val);
  98. out:
  99. return err;
  100. }
  101. int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  102. {
  103. struct sd *sd = (struct sd *) gspca_dev;
  104. u8 i2c_data;
  105. int err;
  106. PDEBUG(D_V4L2, "Set exposure to %d", val & 0xffff);
  107. i2c_data = ((val & 0xff00) >> 8);
  108. PDEBUG(D_V4L2, "Set exposure to high byte to 0x%x",
  109. i2c_data);
  110. err = m5602_write_sensor(sd, PO1030_REG_INTEGLINES_H,
  111. &i2c_data, 1);
  112. if (err < 0)
  113. goto out;
  114. i2c_data = (val & 0xff);
  115. PDEBUG(D_V4L2, "Set exposure to low byte to 0x%x",
  116. i2c_data);
  117. err = m5602_write_sensor(sd, PO1030_REG_INTEGLINES_M,
  118. &i2c_data, 1);
  119. out:
  120. return err;
  121. }
  122. int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  123. {
  124. struct sd *sd = (struct sd *) gspca_dev;
  125. u8 i2c_data;
  126. int err;
  127. err = m5602_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  128. &i2c_data, 1);
  129. *val = i2c_data;
  130. PDEBUG(D_V4L2, "Read global gain %d", *val);
  131. return err;
  132. }
  133. int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  134. {
  135. struct sd *sd = (struct sd *) gspca_dev;
  136. u8 i2c_data;
  137. int err;
  138. err = m5602_read_sensor(sd, PO1030_REG_CONTROL2,
  139. &i2c_data, 1);
  140. *val = (i2c_data >> 7) & 0x01 ;
  141. PDEBUG(D_V4L2, "Read hflip %d", *val);
  142. return err;
  143. }
  144. int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  145. {
  146. struct sd *sd = (struct sd *) gspca_dev;
  147. u8 i2c_data;
  148. int err;
  149. PDEBUG(D_V4L2, "Set hflip %d", val);
  150. i2c_data = (val & 0x01) << 7;
  151. err = m5602_write_sensor(sd, PO1030_REG_CONTROL2,
  152. &i2c_data, 1);
  153. return err;
  154. }
  155. int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  156. {
  157. struct sd *sd = (struct sd *) gspca_dev;
  158. u8 i2c_data;
  159. int err;
  160. err = m5602_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  161. &i2c_data, 1);
  162. *val = (i2c_data >> 6) & 0x01;
  163. PDEBUG(D_V4L2, "Read vflip %d", *val);
  164. return err;
  165. }
  166. int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  167. {
  168. struct sd *sd = (struct sd *) gspca_dev;
  169. u8 i2c_data;
  170. int err;
  171. PDEBUG(D_V4L2, "Set vflip %d", val);
  172. i2c_data = (val & 0x01) << 6;
  173. err = m5602_write_sensor(sd, PO1030_REG_CONTROL2,
  174. &i2c_data, 1);
  175. return err;
  176. }
  177. int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  178. {
  179. struct sd *sd = (struct sd *) gspca_dev;
  180. u8 i2c_data;
  181. int err;
  182. i2c_data = val & 0xff;
  183. PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
  184. err = m5602_write_sensor(sd, PO1030_REG_GLOBALGAIN,
  185. &i2c_data, 1);
  186. return err;
  187. }
  188. int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  189. {
  190. struct sd *sd = (struct sd *) gspca_dev;
  191. u8 i2c_data;
  192. int err;
  193. err = m5602_read_sensor(sd, PO1030_REG_RED_GAIN,
  194. &i2c_data, 1);
  195. *val = i2c_data;
  196. PDEBUG(D_V4L2, "Read red gain %d", *val);
  197. return err;
  198. }
  199. int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  200. {
  201. struct sd *sd = (struct sd *) gspca_dev;
  202. u8 i2c_data;
  203. int err;
  204. i2c_data = val & 0xff;
  205. PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
  206. err = m5602_write_sensor(sd, PO1030_REG_RED_GAIN,
  207. &i2c_data, 1);
  208. return err;
  209. }
  210. int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  211. {
  212. struct sd *sd = (struct sd *) gspca_dev;
  213. u8 i2c_data;
  214. int err;
  215. err = m5602_read_sensor(sd, PO1030_REG_BLUE_GAIN,
  216. &i2c_data, 1);
  217. *val = i2c_data;
  218. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  219. return err;
  220. }
  221. int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  222. {
  223. struct sd *sd = (struct sd *) gspca_dev;
  224. u8 i2c_data;
  225. int err;
  226. i2c_data = val & 0xff;
  227. PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
  228. err = m5602_write_sensor(sd, PO1030_REG_BLUE_GAIN,
  229. &i2c_data, 1);
  230. return err;
  231. }
  232. int po1030_power_down(struct sd *sd)
  233. {
  234. return 0;
  235. }
  236. static void po1030_dump_registers(struct sd *sd)
  237. {
  238. int address;
  239. u8 value = 0;
  240. info("Dumping the po1030 sensor core registers");
  241. for (address = 0; address < 0x7f; address++) {
  242. m5602_read_sensor(sd, address, &value, 1);
  243. info("register 0x%x contains 0x%x",
  244. address, value);
  245. }
  246. info("po1030 register state dump complete");
  247. info("Probing for which registers that are read/write");
  248. for (address = 0; address < 0xff; address++) {
  249. u8 old_value, ctrl_value;
  250. u8 test_value[2] = {0xff, 0xff};
  251. m5602_read_sensor(sd, address, &old_value, 1);
  252. m5602_write_sensor(sd, address, test_value, 1);
  253. m5602_read_sensor(sd, address, &ctrl_value, 1);
  254. if (ctrl_value == test_value[0])
  255. info("register 0x%x is writeable", address);
  256. else
  257. info("register 0x%x is read only", address);
  258. /* Restore original value */
  259. m5602_write_sensor(sd, address, &old_value, 1);
  260. }
  261. }