m5602_po1030.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. err = m5602_read_sensor(sd, PO1030_REG_CONTROL2, &i2c_data, 1);
  151. if (err < 0)
  152. goto out;
  153. i2c_data = (0x7f & i2c_data) | ((val & 0x01) << 7);
  154. err = m5602_write_sensor(sd, PO1030_REG_CONTROL2,
  155. &i2c_data, 1);
  156. out:
  157. return err;
  158. }
  159. int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  160. {
  161. struct sd *sd = (struct sd *) gspca_dev;
  162. u8 i2c_data;
  163. int err;
  164. err = m5602_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  165. &i2c_data, 1);
  166. *val = (i2c_data >> 6) & 0x01;
  167. PDEBUG(D_V4L2, "Read vflip %d", *val);
  168. return err;
  169. }
  170. int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  171. {
  172. struct sd *sd = (struct sd *) gspca_dev;
  173. u8 i2c_data;
  174. int err;
  175. PDEBUG(D_V4L2, "Set vflip %d", val);
  176. err = m5602_read_sensor(sd, PO1030_REG_CONTROL2, &i2c_data, 1);
  177. if (err < 0)
  178. goto out;
  179. i2c_data = (i2c_data & 0xbf) | ((val & 0x01) << 6);
  180. err = m5602_write_sensor(sd, PO1030_REG_CONTROL2,
  181. &i2c_data, 1);
  182. out:
  183. return err;
  184. }
  185. int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  186. {
  187. struct sd *sd = (struct sd *) gspca_dev;
  188. u8 i2c_data;
  189. int err;
  190. i2c_data = val & 0xff;
  191. PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
  192. err = m5602_write_sensor(sd, PO1030_REG_GLOBALGAIN,
  193. &i2c_data, 1);
  194. return err;
  195. }
  196. int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  197. {
  198. struct sd *sd = (struct sd *) gspca_dev;
  199. u8 i2c_data;
  200. int err;
  201. err = m5602_read_sensor(sd, PO1030_REG_RED_GAIN,
  202. &i2c_data, 1);
  203. *val = i2c_data;
  204. PDEBUG(D_V4L2, "Read red gain %d", *val);
  205. return err;
  206. }
  207. int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  208. {
  209. struct sd *sd = (struct sd *) gspca_dev;
  210. u8 i2c_data;
  211. int err;
  212. i2c_data = val & 0xff;
  213. PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
  214. err = m5602_write_sensor(sd, PO1030_REG_RED_GAIN,
  215. &i2c_data, 1);
  216. return err;
  217. }
  218. int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  219. {
  220. struct sd *sd = (struct sd *) gspca_dev;
  221. u8 i2c_data;
  222. int err;
  223. err = m5602_read_sensor(sd, PO1030_REG_BLUE_GAIN,
  224. &i2c_data, 1);
  225. *val = i2c_data;
  226. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  227. return err;
  228. }
  229. int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  230. {
  231. struct sd *sd = (struct sd *) gspca_dev;
  232. u8 i2c_data;
  233. int err;
  234. i2c_data = val & 0xff;
  235. PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
  236. err = m5602_write_sensor(sd, PO1030_REG_BLUE_GAIN,
  237. &i2c_data, 1);
  238. return err;
  239. }
  240. int po1030_power_down(struct sd *sd)
  241. {
  242. return 0;
  243. }
  244. static void po1030_dump_registers(struct sd *sd)
  245. {
  246. int address;
  247. u8 value = 0;
  248. info("Dumping the po1030 sensor core registers");
  249. for (address = 0; address < 0x7f; address++) {
  250. m5602_read_sensor(sd, address, &value, 1);
  251. info("register 0x%x contains 0x%x",
  252. address, value);
  253. }
  254. info("po1030 register state dump complete");
  255. info("Probing for which registers that are read/write");
  256. for (address = 0; address < 0xff; address++) {
  257. u8 old_value, ctrl_value;
  258. u8 test_value[2] = {0xff, 0xff};
  259. m5602_read_sensor(sd, address, &old_value, 1);
  260. m5602_write_sensor(sd, address, test_value, 1);
  261. m5602_read_sensor(sd, address, &ctrl_value, 1);
  262. if (ctrl_value == test_value[0])
  263. info("register 0x%x is writeable", address);
  264. else
  265. info("register 0x%x is read only", address);
  266. /* Restore original value */
  267. m5602_write_sensor(sd, address, &old_value, 1);
  268. }
  269. }