m5602_po1030.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. int po1030_probe(struct sd *sd)
  20. {
  21. u8 prod_id = 0, ver_id = 0, i;
  22. if (force_sensor) {
  23. if (force_sensor == PO1030_SENSOR) {
  24. info("Forcing a %s sensor", po1030.name);
  25. goto sensor_found;
  26. }
  27. /* If we want to force another sensor, don't try to probe this
  28. * one */
  29. return -ENODEV;
  30. }
  31. info("Probing for a po1030 sensor");
  32. /* Run the pre-init to actually probe the unit */
  33. for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
  34. u8 data = preinit_po1030[i][2];
  35. if (preinit_po1030[i][0] == SENSOR)
  36. po1030_write_sensor(sd,
  37. preinit_po1030[i][1], &data, 1);
  38. else
  39. m5602_write_bridge(sd, preinit_po1030[i][1], data);
  40. }
  41. if (po1030_read_sensor(sd, 0x3, &prod_id, 1))
  42. return -ENODEV;
  43. if (po1030_read_sensor(sd, 0x4, &ver_id, 1))
  44. return -ENODEV;
  45. if ((prod_id == 0x02) && (ver_id == 0xef)) {
  46. info("Detected a po1030 sensor");
  47. goto sensor_found;
  48. }
  49. return -ENODEV;
  50. sensor_found:
  51. sd->gspca_dev.cam.cam_mode = po1030.modes;
  52. sd->gspca_dev.cam.nmodes = po1030.nmodes;
  53. sd->desc->ctrls = po1030.ctrls;
  54. sd->desc->nctrls = po1030.nctrls;
  55. return 0;
  56. }
  57. int po1030_read_sensor(struct sd *sd, const u8 address,
  58. u8 *i2c_data, const u8 len)
  59. {
  60. int err, i;
  61. do {
  62. err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
  63. } while ((*i2c_data & I2C_BUSY) && !err);
  64. m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
  65. sd->sensor->i2c_slave_id);
  66. m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
  67. m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x10 + len);
  68. m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
  69. for (i = 0; i < len; i++) {
  70. err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
  71. PDEBUG(D_CONF, "Reading sensor register "
  72. "0x%x containing 0x%x ", address, *i2c_data);
  73. }
  74. return (err < 0) ? err : 0;
  75. }
  76. int po1030_write_sensor(struct sd *sd, const u8 address,
  77. u8 *i2c_data, const u8 len)
  78. {
  79. int err, i;
  80. u8 *p;
  81. struct usb_device *udev = sd->gspca_dev.dev;
  82. __u8 *buf = sd->gspca_dev.usb_buf;
  83. /* The po1030 only supports one byte writes */
  84. if (len > 1 || !len)
  85. return -EINVAL;
  86. memcpy(buf, sensor_urb_skeleton, sizeof(sensor_urb_skeleton));
  87. buf[11] = sd->sensor->i2c_slave_id;
  88. buf[15] = address;
  89. p = buf + 16;
  90. /* Copy a four byte write sequence for each byte to be written to */
  91. for (i = 0; i < len; i++) {
  92. memcpy(p, sensor_urb_skeleton + 16, 4);
  93. p[3] = i2c_data[i];
  94. p += 4;
  95. PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
  96. address, i2c_data[i]);
  97. }
  98. /* Copy the footer */
  99. memcpy(p, sensor_urb_skeleton + 20, 4);
  100. /* Set the total length */
  101. p[3] = 0x10 + len;
  102. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  103. 0x04, 0x40, 0x19,
  104. 0x0000, buf,
  105. 20 + len * 4, M5602_URB_MSG_TIMEOUT);
  106. return (err < 0) ? err : 0;
  107. }
  108. int po1030_init(struct sd *sd)
  109. {
  110. int i, err = 0;
  111. /* Init the sensor */
  112. for (i = 0; i < ARRAY_SIZE(init_po1030); i++) {
  113. u8 data[2] = {0x00, 0x00};
  114. switch (init_po1030[i][0]) {
  115. case BRIDGE:
  116. err = m5602_write_bridge(sd,
  117. init_po1030[i][1],
  118. init_po1030[i][2]);
  119. break;
  120. case SENSOR:
  121. data[0] = init_po1030[i][2];
  122. err = po1030_write_sensor(sd,
  123. init_po1030[i][1], data, 1);
  124. break;
  125. case SENSOR_LONG:
  126. data[0] = init_po1030[i][2];
  127. data[1] = init_po1030[i][3];
  128. err = po1030_write_sensor(sd,
  129. init_po1030[i][1], data, 2);
  130. break;
  131. default:
  132. info("Invalid stream command, exiting init");
  133. return -EINVAL;
  134. }
  135. }
  136. if (dump_sensor)
  137. po1030_dump_registers(sd);
  138. return (err < 0) ? err : 0;
  139. }
  140. int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  141. {
  142. struct sd *sd = (struct sd *) gspca_dev;
  143. u8 i2c_data;
  144. int err;
  145. err = po1030_read_sensor(sd, PO1030_REG_INTEGLINES_H,
  146. &i2c_data, 1);
  147. if (err < 0)
  148. goto out;
  149. *val = (i2c_data << 8);
  150. err = po1030_read_sensor(sd, PO1030_REG_INTEGLINES_M,
  151. &i2c_data, 1);
  152. *val |= i2c_data;
  153. PDEBUG(D_V4L2, "Exposure read as %d", *val);
  154. out:
  155. return (err < 0) ? err : 0;
  156. }
  157. int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  158. {
  159. struct sd *sd = (struct sd *) gspca_dev;
  160. u8 i2c_data;
  161. int err;
  162. PDEBUG(D_V4L2, "Set exposure to %d", val & 0xffff);
  163. i2c_data = ((val & 0xff00) >> 8);
  164. PDEBUG(D_V4L2, "Set exposure to high byte to 0x%x",
  165. i2c_data);
  166. err = po1030_write_sensor(sd, PO1030_REG_INTEGLINES_H,
  167. &i2c_data, 1);
  168. if (err < 0)
  169. goto out;
  170. i2c_data = (val & 0xff);
  171. PDEBUG(D_V4L2, "Set exposure to low byte to 0x%x",
  172. i2c_data);
  173. err = po1030_write_sensor(sd, PO1030_REG_INTEGLINES_M,
  174. &i2c_data, 1);
  175. out:
  176. return (err < 0) ? err : 0;
  177. }
  178. int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  179. {
  180. struct sd *sd = (struct sd *) gspca_dev;
  181. u8 i2c_data;
  182. int err;
  183. err = po1030_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  184. &i2c_data, 1);
  185. *val = i2c_data;
  186. PDEBUG(D_V4L2, "Read global gain %d", *val);
  187. return (err < 0) ? err : 0;
  188. }
  189. int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  190. {
  191. struct sd *sd = (struct sd *) gspca_dev;
  192. u8 i2c_data;
  193. int err;
  194. err = po1030_read_sensor(sd, PO1030_REG_CONTROL2,
  195. &i2c_data, 1);
  196. *val = (i2c_data >> 7) & 0x01 ;
  197. PDEBUG(D_V4L2, "Read hflip %d", *val);
  198. return (err < 0) ? err : 0;
  199. }
  200. int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  201. {
  202. struct sd *sd = (struct sd *) gspca_dev;
  203. u8 i2c_data;
  204. int err;
  205. PDEBUG(D_V4L2, "Set hflip %d", val);
  206. i2c_data = (val & 0x01) << 7;
  207. err = po1030_write_sensor(sd, PO1030_REG_CONTROL2,
  208. &i2c_data, 1);
  209. return (err < 0) ? err : 0;
  210. }
  211. int po1030_get_vflip(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 = po1030_read_sensor(sd, PO1030_REG_GLOBALGAIN,
  217. &i2c_data, 1);
  218. *val = (i2c_data >> 6) & 0x01;
  219. PDEBUG(D_V4L2, "Read vflip %d", *val);
  220. return (err < 0) ? err : 0;
  221. }
  222. int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  223. {
  224. struct sd *sd = (struct sd *) gspca_dev;
  225. u8 i2c_data;
  226. int err;
  227. PDEBUG(D_V4L2, "Set vflip %d", val);
  228. i2c_data = (val & 0x01) << 6;
  229. err = po1030_write_sensor(sd, PO1030_REG_CONTROL2,
  230. &i2c_data, 1);
  231. return (err < 0) ? err : 0;
  232. }
  233. int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  234. {
  235. struct sd *sd = (struct sd *) gspca_dev;
  236. u8 i2c_data;
  237. int err;
  238. i2c_data = val & 0xff;
  239. PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
  240. err = po1030_write_sensor(sd, PO1030_REG_GLOBALGAIN,
  241. &i2c_data, 1);
  242. return (err < 0) ? err : 0;
  243. }
  244. int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  245. {
  246. struct sd *sd = (struct sd *) gspca_dev;
  247. u8 i2c_data;
  248. int err;
  249. err = po1030_read_sensor(sd, PO1030_REG_RED_GAIN,
  250. &i2c_data, 1);
  251. *val = i2c_data;
  252. PDEBUG(D_V4L2, "Read red gain %d", *val);
  253. return (err < 0) ? err : 0;
  254. }
  255. int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  256. {
  257. struct sd *sd = (struct sd *) gspca_dev;
  258. u8 i2c_data;
  259. int err;
  260. i2c_data = val & 0xff;
  261. PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
  262. err = po1030_write_sensor(sd, PO1030_REG_RED_GAIN,
  263. &i2c_data, 1);
  264. return (err < 0) ? err : 0;
  265. }
  266. int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  267. {
  268. struct sd *sd = (struct sd *) gspca_dev;
  269. u8 i2c_data;
  270. int err;
  271. err = po1030_read_sensor(sd, PO1030_REG_BLUE_GAIN,
  272. &i2c_data, 1);
  273. *val = i2c_data;
  274. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  275. return (err < 0) ? err : 0;
  276. }
  277. int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  278. {
  279. struct sd *sd = (struct sd *) gspca_dev;
  280. u8 i2c_data;
  281. int err;
  282. i2c_data = val & 0xff;
  283. PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
  284. err = po1030_write_sensor(sd, PO1030_REG_BLUE_GAIN,
  285. &i2c_data, 1);
  286. return (err < 0) ? err : 0;
  287. }
  288. int po1030_power_down(struct sd *sd)
  289. {
  290. return 0;
  291. }
  292. void po1030_dump_registers(struct sd *sd)
  293. {
  294. int address;
  295. u8 value = 0;
  296. info("Dumping the po1030 sensor core registers");
  297. for (address = 0; address < 0x7f; address++) {
  298. po1030_read_sensor(sd, address, &value, 1);
  299. info("register 0x%x contains 0x%x",
  300. address, value);
  301. }
  302. info("po1030 register state dump complete");
  303. info("Probing for which registers that are read/write");
  304. for (address = 0; address < 0xff; address++) {
  305. u8 old_value, ctrl_value;
  306. u8 test_value[2] = {0xff, 0xff};
  307. po1030_read_sensor(sd, address, &old_value, 1);
  308. po1030_write_sensor(sd, address, test_value, 1);
  309. po1030_read_sensor(sd, address, &ctrl_value, 1);
  310. if (ctrl_value == test_value[0])
  311. info("register 0x%x is writeable", address);
  312. else
  313. info("register 0x%x is read only", address);
  314. /* Restore original value */
  315. po1030_write_sensor(sd, address, &old_value, 1);
  316. }
  317. }