m5602_po1030.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 struct v4l2_pix_format po1030_modes[] = {
  20. {
  21. 640,
  22. 480,
  23. V4L2_PIX_FMT_SBGGR8,
  24. V4L2_FIELD_NONE,
  25. .sizeimage = 640 * 480,
  26. .bytesperline = 640,
  27. .colorspace = V4L2_COLORSPACE_SRGB,
  28. .priv = 0
  29. }
  30. };
  31. const static struct ctrl po1030_ctrls[] = {
  32. {
  33. {
  34. .id = V4L2_CID_GAIN,
  35. .type = V4L2_CTRL_TYPE_INTEGER,
  36. .name = "gain",
  37. .minimum = 0x00,
  38. .maximum = 0x4f,
  39. .step = 0x1,
  40. .default_value = PO1030_GLOBAL_GAIN_DEFAULT,
  41. .flags = V4L2_CTRL_FLAG_SLIDER
  42. },
  43. .set = po1030_set_gain,
  44. .get = po1030_get_gain
  45. }, {
  46. {
  47. .id = V4L2_CID_EXPOSURE,
  48. .type = V4L2_CTRL_TYPE_INTEGER,
  49. .name = "exposure",
  50. .minimum = 0x00,
  51. .maximum = 0x02ff,
  52. .step = 0x1,
  53. .default_value = PO1030_EXPOSURE_DEFAULT,
  54. .flags = V4L2_CTRL_FLAG_SLIDER
  55. },
  56. .set = po1030_set_exposure,
  57. .get = po1030_get_exposure
  58. }, {
  59. {
  60. .id = V4L2_CID_RED_BALANCE,
  61. .type = V4L2_CTRL_TYPE_INTEGER,
  62. .name = "red balance",
  63. .minimum = 0x00,
  64. .maximum = 0xff,
  65. .step = 0x1,
  66. .default_value = PO1030_RED_GAIN_DEFAULT,
  67. .flags = V4L2_CTRL_FLAG_SLIDER
  68. },
  69. .set = po1030_set_red_balance,
  70. .get = po1030_get_red_balance
  71. }, {
  72. {
  73. .id = V4L2_CID_BLUE_BALANCE,
  74. .type = V4L2_CTRL_TYPE_INTEGER,
  75. .name = "blue balance",
  76. .minimum = 0x00,
  77. .maximum = 0xff,
  78. .step = 0x1,
  79. .default_value = PO1030_BLUE_GAIN_DEFAULT,
  80. .flags = V4L2_CTRL_FLAG_SLIDER
  81. },
  82. .set = po1030_set_blue_balance,
  83. .get = po1030_get_blue_balance
  84. }, {
  85. {
  86. .id = V4L2_CID_HFLIP,
  87. .type = V4L2_CTRL_TYPE_BOOLEAN,
  88. .name = "horizontal flip",
  89. .minimum = 0,
  90. .maximum = 1,
  91. .step = 1,
  92. .default_value = 0,
  93. },
  94. .set = po1030_set_hflip,
  95. .get = po1030_get_hflip
  96. }, {
  97. {
  98. .id = V4L2_CID_VFLIP,
  99. .type = V4L2_CTRL_TYPE_BOOLEAN,
  100. .name = "vertical flip",
  101. .minimum = 0,
  102. .maximum = 1,
  103. .step = 1,
  104. .default_value = 0,
  105. },
  106. .set = po1030_set_vflip,
  107. .get = po1030_get_vflip
  108. }
  109. };
  110. static void po1030_dump_registers(struct sd *sd);
  111. int po1030_probe(struct sd *sd)
  112. {
  113. u8 prod_id = 0, ver_id = 0, i;
  114. if (force_sensor) {
  115. if (force_sensor == PO1030_SENSOR) {
  116. info("Forcing a %s sensor", po1030.name);
  117. goto sensor_found;
  118. }
  119. /* If we want to force another sensor, don't try to probe this
  120. * one */
  121. return -ENODEV;
  122. }
  123. info("Probing for a po1030 sensor");
  124. /* Run the pre-init to actually probe the unit */
  125. for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
  126. u8 data = preinit_po1030[i][2];
  127. if (preinit_po1030[i][0] == SENSOR)
  128. m5602_write_sensor(sd,
  129. preinit_po1030[i][1], &data, 1);
  130. else
  131. m5602_write_bridge(sd, preinit_po1030[i][1], data);
  132. }
  133. if (m5602_read_sensor(sd, 0x3, &prod_id, 1))
  134. return -ENODEV;
  135. if (m5602_read_sensor(sd, 0x4, &ver_id, 1))
  136. return -ENODEV;
  137. if ((prod_id == 0x02) && (ver_id == 0xef)) {
  138. info("Detected a po1030 sensor");
  139. goto sensor_found;
  140. }
  141. return -ENODEV;
  142. sensor_found:
  143. sd->gspca_dev.cam.cam_mode = po1030_modes;
  144. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(po1030_modes);
  145. sd->desc->ctrls = po1030_ctrls;
  146. sd->desc->nctrls = ARRAY_SIZE(po1030_ctrls);
  147. return 0;
  148. }
  149. int po1030_init(struct sd *sd)
  150. {
  151. int i, err = 0;
  152. /* Init the sensor */
  153. for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
  154. u8 data[2] = {0x00, 0x00};
  155. switch (init_po1030[i][0]) {
  156. case BRIDGE:
  157. err = m5602_write_bridge(sd,
  158. init_po1030[i][1],
  159. init_po1030[i][2]);
  160. break;
  161. case SENSOR:
  162. data[0] = init_po1030[i][2];
  163. err = m5602_write_sensor(sd,
  164. init_po1030[i][1], data, 1);
  165. break;
  166. default:
  167. info("Invalid stream command, exiting init");
  168. return -EINVAL;
  169. }
  170. }
  171. if (dump_sensor)
  172. po1030_dump_registers(sd);
  173. return err;
  174. }
  175. int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  176. {
  177. struct sd *sd = (struct sd *) gspca_dev;
  178. u8 i2c_data;
  179. int err;
  180. err = m5602_read_sensor(sd, PO1030_REG_INTEGLINES_H,
  181. &i2c_data, 1);
  182. if (err < 0)
  183. return err;
  184. *val = (i2c_data << 8);
  185. err = m5602_read_sensor(sd, PO1030_REG_INTEGLINES_M,
  186. &i2c_data, 1);
  187. *val |= i2c_data;
  188. PDEBUG(D_V4L2, "Exposure read as %d", *val);
  189. return err;
  190. }
  191. int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  192. {
  193. struct sd *sd = (struct sd *) gspca_dev;
  194. u8 i2c_data;
  195. int err;
  196. PDEBUG(D_V4L2, "Set exposure to %d", val & 0xffff);
  197. i2c_data = ((val & 0xff00) >> 8);
  198. PDEBUG(D_V4L2, "Set exposure to high byte to 0x%x",
  199. i2c_data);
  200. err = m5602_write_sensor(sd, PO1030_REG_INTEGLINES_H,
  201. &i2c_data, 1);
  202. if (err < 0)
  203. return err;
  204. i2c_data = (val & 0xff);
  205. PDEBUG(D_V4L2, "Set exposure to low byte to 0x%x",
  206. i2c_data);
  207. err = m5602_write_sensor(sd, PO1030_REG_INTEGLINES_M,
  208. &i2c_data, 1);
  209. return err;
  210. }
  211. int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  212. {
  213. struct sd *sd = (struct sd *) gspca_dev;
  214. u8 i2c_data;
  215. int err;
  216. err = m5602_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  217. &i2c_data, 1);
  218. *val = i2c_data;
  219. PDEBUG(D_V4L2, "Read global gain %d", *val);
  220. return err;
  221. }
  222. int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  223. {
  224. struct sd *sd = (struct sd *) gspca_dev;
  225. u8 i2c_data;
  226. int err;
  227. err = m5602_read_sensor(sd, PO1030_REG_CONTROL2,
  228. &i2c_data, 1);
  229. *val = (i2c_data >> 7) & 0x01 ;
  230. PDEBUG(D_V4L2, "Read hflip %d", *val);
  231. return err;
  232. }
  233. int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  234. {
  235. struct sd *sd = (struct sd *) gspca_dev;
  236. u8 i2c_data;
  237. int err;
  238. PDEBUG(D_V4L2, "Set hflip %d", val);
  239. err = m5602_read_sensor(sd, PO1030_REG_CONTROL2, &i2c_data, 1);
  240. if (err < 0)
  241. return err;
  242. i2c_data = (0x7f & i2c_data) | ((val & 0x01) << 7);
  243. err = m5602_write_sensor(sd, PO1030_REG_CONTROL2,
  244. &i2c_data, 1);
  245. return err;
  246. }
  247. int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  248. {
  249. struct sd *sd = (struct sd *) gspca_dev;
  250. u8 i2c_data;
  251. int err;
  252. err = m5602_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  253. &i2c_data, 1);
  254. *val = (i2c_data >> 6) & 0x01;
  255. PDEBUG(D_V4L2, "Read vflip %d", *val);
  256. return err;
  257. }
  258. int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  259. {
  260. struct sd *sd = (struct sd *) gspca_dev;
  261. u8 i2c_data;
  262. int err;
  263. PDEBUG(D_V4L2, "Set vflip %d", val);
  264. err = m5602_read_sensor(sd, PO1030_REG_CONTROL2, &i2c_data, 1);
  265. if (err < 0)
  266. return err;
  267. i2c_data = (i2c_data & 0xbf) | ((val & 0x01) << 6);
  268. err = m5602_write_sensor(sd, PO1030_REG_CONTROL2,
  269. &i2c_data, 1);
  270. return err;
  271. }
  272. int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  273. {
  274. struct sd *sd = (struct sd *) gspca_dev;
  275. u8 i2c_data;
  276. int err;
  277. i2c_data = val & 0xff;
  278. PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
  279. err = m5602_write_sensor(sd, PO1030_REG_GLOBALGAIN,
  280. &i2c_data, 1);
  281. return err;
  282. }
  283. int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  284. {
  285. struct sd *sd = (struct sd *) gspca_dev;
  286. u8 i2c_data;
  287. int err;
  288. err = m5602_read_sensor(sd, PO1030_REG_RED_GAIN,
  289. &i2c_data, 1);
  290. *val = i2c_data;
  291. PDEBUG(D_V4L2, "Read red gain %d", *val);
  292. return err;
  293. }
  294. int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  295. {
  296. struct sd *sd = (struct sd *) gspca_dev;
  297. u8 i2c_data;
  298. int err;
  299. i2c_data = val & 0xff;
  300. PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
  301. err = m5602_write_sensor(sd, PO1030_REG_RED_GAIN,
  302. &i2c_data, 1);
  303. return err;
  304. }
  305. int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  306. {
  307. struct sd *sd = (struct sd *) gspca_dev;
  308. u8 i2c_data;
  309. int err;
  310. err = m5602_read_sensor(sd, PO1030_REG_BLUE_GAIN,
  311. &i2c_data, 1);
  312. *val = i2c_data;
  313. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  314. return err;
  315. }
  316. int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  317. {
  318. struct sd *sd = (struct sd *) gspca_dev;
  319. u8 i2c_data;
  320. int err;
  321. i2c_data = val & 0xff;
  322. PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
  323. err = m5602_write_sensor(sd, PO1030_REG_BLUE_GAIN,
  324. &i2c_data, 1);
  325. return err;
  326. }
  327. int po1030_power_down(struct sd *sd)
  328. {
  329. return 0;
  330. }
  331. static void po1030_dump_registers(struct sd *sd)
  332. {
  333. int address;
  334. u8 value = 0;
  335. info("Dumping the po1030 sensor core registers");
  336. for (address = 0; address < 0x7f; address++) {
  337. m5602_read_sensor(sd, address, &value, 1);
  338. info("register 0x%x contains 0x%x",
  339. address, value);
  340. }
  341. info("po1030 register state dump complete");
  342. info("Probing for which registers that are read/write");
  343. for (address = 0; address < 0xff; address++) {
  344. u8 old_value, ctrl_value;
  345. u8 test_value[2] = {0xff, 0xff};
  346. m5602_read_sensor(sd, address, &old_value, 1);
  347. m5602_write_sensor(sd, address, test_value, 1);
  348. m5602_read_sensor(sd, address, &ctrl_value, 1);
  349. if (ctrl_value == test_value[0])
  350. info("register 0x%x is writeable", address);
  351. else
  352. info("register 0x%x is read only", address);
  353. /* Restore original value */
  354. m5602_write_sensor(sd, address, &old_value, 1);
  355. }
  356. }