cpia2_core.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  1. /****************************************************************************
  2. *
  3. * Filename: cpia2_core.c
  4. *
  5. * Copyright 2001, STMicrolectronics, Inc.
  6. * Contact: steve.miller@st.com
  7. *
  8. * Description:
  9. * This is a USB driver for CPia2 based video cameras.
  10. * The infrastructure of this driver is based on the cpia usb driver by
  11. * Jochen Scharrlach and Johannes Erdfeldt.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. *
  27. * Stripped of 2.4 stuff ready for main kernel submit by
  28. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  29. *
  30. ****************************************************************************/
  31. #include "cpia2.h"
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <linux/mm.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/firmware.h>
  37. #define FIRMWARE "/*(DEBLOBBED)*/"
  38. /*(DEBLOBBED)*/
  39. /* #define _CPIA2_DEBUG_ */
  40. #ifdef _CPIA2_DEBUG_
  41. static const char *block_name[] = {
  42. "System",
  43. "VC",
  44. "VP",
  45. "IDATA"
  46. };
  47. #endif
  48. static unsigned int debugs_on; /* default 0 - DEBUG_REG */
  49. /******************************************************************************
  50. *
  51. * Forward Declarations
  52. *
  53. *****************************************************************************/
  54. static int apply_vp_patch(struct camera_data *cam);
  55. static int set_default_user_mode(struct camera_data *cam);
  56. static int set_vw_size(struct camera_data *cam, int size);
  57. static int configure_sensor(struct camera_data *cam,
  58. int reqwidth, int reqheight);
  59. static int config_sensor_410(struct camera_data *cam,
  60. int reqwidth, int reqheight);
  61. static int config_sensor_500(struct camera_data *cam,
  62. int reqwidth, int reqheight);
  63. static int set_all_properties(struct camera_data *cam);
  64. static void wake_system(struct camera_data *cam);
  65. static void set_lowlight_boost(struct camera_data *cam);
  66. static void reset_camera_struct(struct camera_data *cam);
  67. static int cpia2_set_high_power(struct camera_data *cam);
  68. /* Here we want the physical address of the memory.
  69. * This is used when initializing the contents of the
  70. * area and marking the pages as reserved.
  71. */
  72. static inline unsigned long kvirt_to_pa(unsigned long adr)
  73. {
  74. unsigned long kva, ret;
  75. kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
  76. kva |= adr & (PAGE_SIZE-1); /* restore the offset */
  77. ret = __pa(kva);
  78. return ret;
  79. }
  80. static void *rvmalloc(unsigned long size)
  81. {
  82. void *mem;
  83. unsigned long adr;
  84. /* Round it off to PAGE_SIZE */
  85. size = PAGE_ALIGN(size);
  86. mem = vmalloc_32(size);
  87. if (!mem)
  88. return NULL;
  89. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  90. adr = (unsigned long) mem;
  91. while ((long)size > 0) {
  92. SetPageReserved(vmalloc_to_page((void *)adr));
  93. adr += PAGE_SIZE;
  94. size -= PAGE_SIZE;
  95. }
  96. return mem;
  97. }
  98. static void rvfree(void *mem, unsigned long size)
  99. {
  100. unsigned long adr;
  101. if (!mem)
  102. return;
  103. size = PAGE_ALIGN(size);
  104. adr = (unsigned long) mem;
  105. while ((long)size > 0) {
  106. ClearPageReserved(vmalloc_to_page((void *)adr));
  107. adr += PAGE_SIZE;
  108. size -= PAGE_SIZE;
  109. }
  110. vfree(mem);
  111. }
  112. /******************************************************************************
  113. *
  114. * cpia2_do_command
  115. *
  116. * Send an arbitrary command to the camera. For commands that read from
  117. * the camera, copy the buffers into the proper param structures.
  118. *****************************************************************************/
  119. int cpia2_do_command(struct camera_data *cam,
  120. u32 command, u8 direction, u8 param)
  121. {
  122. int retval = 0;
  123. struct cpia2_command cmd;
  124. unsigned int device = cam->params.pnp_id.device_type;
  125. cmd.command = command;
  126. cmd.reg_count = 2; /* default */
  127. cmd.direction = direction;
  128. /***
  129. * Set up the command.
  130. ***/
  131. switch (command) {
  132. case CPIA2_CMD_GET_VERSION:
  133. cmd.req_mode =
  134. CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  135. cmd.start = CPIA2_SYSTEM_DEVICE_HI;
  136. break;
  137. case CPIA2_CMD_GET_PNP_ID:
  138. cmd.req_mode =
  139. CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  140. cmd.reg_count = 8;
  141. cmd.start = CPIA2_SYSTEM_DESCRIP_VID_HI;
  142. break;
  143. case CPIA2_CMD_GET_ASIC_TYPE:
  144. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
  145. cmd.start = CPIA2_VC_ASIC_ID;
  146. break;
  147. case CPIA2_CMD_GET_SENSOR:
  148. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  149. cmd.start = CPIA2_VP_SENSOR_FLAGS;
  150. break;
  151. case CPIA2_CMD_GET_VP_DEVICE:
  152. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  153. cmd.start = CPIA2_VP_DEVICEH;
  154. break;
  155. case CPIA2_CMD_SET_VP_BRIGHTNESS:
  156. cmd.buffer.block_data[0] = param; /* Then fall through */
  157. case CPIA2_CMD_GET_VP_BRIGHTNESS:
  158. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  159. cmd.reg_count = 1;
  160. if (device == DEVICE_STV_672)
  161. cmd.start = CPIA2_VP4_EXPOSURE_TARGET;
  162. else
  163. cmd.start = CPIA2_VP5_EXPOSURE_TARGET;
  164. break;
  165. case CPIA2_CMD_SET_CONTRAST:
  166. cmd.buffer.block_data[0] = param; /* Then fall through */
  167. case CPIA2_CMD_GET_CONTRAST:
  168. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  169. cmd.reg_count = 1;
  170. cmd.start = CPIA2_VP_YRANGE;
  171. break;
  172. case CPIA2_CMD_SET_VP_SATURATION:
  173. cmd.buffer.block_data[0] = param; /* Then fall through */
  174. case CPIA2_CMD_GET_VP_SATURATION:
  175. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  176. cmd.reg_count = 1;
  177. if (device == DEVICE_STV_672)
  178. cmd.start = CPIA2_VP_SATURATION;
  179. else
  180. cmd.start = CPIA2_VP5_MCUVSATURATION;
  181. break;
  182. case CPIA2_CMD_SET_VP_GPIO_DATA:
  183. cmd.buffer.block_data[0] = param; /* Then fall through */
  184. case CPIA2_CMD_GET_VP_GPIO_DATA:
  185. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  186. cmd.reg_count = 1;
  187. cmd.start = CPIA2_VP_GPIO_DATA;
  188. break;
  189. case CPIA2_CMD_SET_VP_GPIO_DIRECTION:
  190. cmd.buffer.block_data[0] = param; /* Then fall through */
  191. case CPIA2_CMD_GET_VP_GPIO_DIRECTION:
  192. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  193. cmd.reg_count = 1;
  194. cmd.start = CPIA2_VP_GPIO_DIRECTION;
  195. break;
  196. case CPIA2_CMD_SET_VC_MP_GPIO_DATA:
  197. cmd.buffer.block_data[0] = param; /* Then fall through */
  198. case CPIA2_CMD_GET_VC_MP_GPIO_DATA:
  199. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
  200. cmd.reg_count = 1;
  201. cmd.start = CPIA2_VC_MP_DATA;
  202. break;
  203. case CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION:
  204. cmd.buffer.block_data[0] = param; /* Then fall through */
  205. case CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION:
  206. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
  207. cmd.reg_count = 1;
  208. cmd.start = CPIA2_VC_MP_DIR;
  209. break;
  210. case CPIA2_CMD_ENABLE_PACKET_CTRL:
  211. cmd.req_mode =
  212. CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  213. cmd.start = CPIA2_SYSTEM_INT_PACKET_CTRL;
  214. cmd.reg_count = 1;
  215. cmd.buffer.block_data[0] = param;
  216. break;
  217. case CPIA2_CMD_SET_FLICKER_MODES:
  218. cmd.buffer.block_data[0] = param; /* Then fall through */
  219. case CPIA2_CMD_GET_FLICKER_MODES:
  220. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  221. cmd.reg_count = 1;
  222. cmd.start = CPIA2_VP_FLICKER_MODES;
  223. break;
  224. case CPIA2_CMD_RESET_FIFO: /* clear fifo and enable stream block */
  225. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
  226. cmd.reg_count = 2;
  227. cmd.start = 0;
  228. cmd.buffer.registers[0].index = CPIA2_VC_ST_CTRL;
  229. cmd.buffer.registers[0].value = CPIA2_VC_ST_CTRL_SRC_VC |
  230. CPIA2_VC_ST_CTRL_DST_USB | CPIA2_VC_ST_CTRL_EOF_DETECT;
  231. cmd.buffer.registers[1].index = CPIA2_VC_ST_CTRL;
  232. cmd.buffer.registers[1].value = CPIA2_VC_ST_CTRL_SRC_VC |
  233. CPIA2_VC_ST_CTRL_DST_USB |
  234. CPIA2_VC_ST_CTRL_EOF_DETECT |
  235. CPIA2_VC_ST_CTRL_FIFO_ENABLE;
  236. break;
  237. case CPIA2_CMD_SET_HI_POWER:
  238. cmd.req_mode =
  239. CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_SYSTEM;
  240. cmd.reg_count = 2;
  241. cmd.buffer.registers[0].index =
  242. CPIA2_SYSTEM_SYSTEM_CONTROL;
  243. cmd.buffer.registers[1].index =
  244. CPIA2_SYSTEM_SYSTEM_CONTROL;
  245. cmd.buffer.registers[0].value = CPIA2_SYSTEM_CONTROL_CLEAR_ERR;
  246. cmd.buffer.registers[1].value =
  247. CPIA2_SYSTEM_CONTROL_HIGH_POWER;
  248. break;
  249. case CPIA2_CMD_SET_LOW_POWER:
  250. cmd.req_mode =
  251. CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  252. cmd.reg_count = 1;
  253. cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
  254. cmd.buffer.block_data[0] = 0;
  255. break;
  256. case CPIA2_CMD_CLEAR_V2W_ERR:
  257. cmd.req_mode =
  258. CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  259. cmd.reg_count = 1;
  260. cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
  261. cmd.buffer.block_data[0] = CPIA2_SYSTEM_CONTROL_CLEAR_ERR;
  262. break;
  263. case CPIA2_CMD_SET_USER_MODE: /* Then fall through */
  264. cmd.buffer.block_data[0] = param;
  265. case CPIA2_CMD_GET_USER_MODE:
  266. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  267. cmd.reg_count = 1;
  268. if (device == DEVICE_STV_672)
  269. cmd.start = CPIA2_VP4_USER_MODE;
  270. else
  271. cmd.start = CPIA2_VP5_USER_MODE;
  272. break;
  273. case CPIA2_CMD_FRAMERATE_REQ:
  274. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  275. cmd.reg_count = 1;
  276. if (device == DEVICE_STV_672)
  277. cmd.start = CPIA2_VP4_FRAMERATE_REQUEST;
  278. else
  279. cmd.start = CPIA2_VP5_FRAMERATE_REQUEST;
  280. cmd.buffer.block_data[0] = param;
  281. break;
  282. case CPIA2_CMD_SET_WAKEUP:
  283. cmd.buffer.block_data[0] = param; /* Then fall through */
  284. case CPIA2_CMD_GET_WAKEUP:
  285. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
  286. cmd.reg_count = 1;
  287. cmd.start = CPIA2_VC_WAKEUP;
  288. break;
  289. case CPIA2_CMD_SET_PW_CONTROL:
  290. cmd.buffer.block_data[0] = param; /* Then fall through */
  291. case CPIA2_CMD_GET_PW_CONTROL:
  292. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
  293. cmd.reg_count = 1;
  294. cmd.start = CPIA2_VC_PW_CTRL;
  295. break;
  296. case CPIA2_CMD_GET_VP_SYSTEM_STATE:
  297. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  298. cmd.reg_count = 1;
  299. cmd.start = CPIA2_VP_SYSTEMSTATE;
  300. break;
  301. case CPIA2_CMD_SET_SYSTEM_CTRL:
  302. cmd.buffer.block_data[0] = param; /* Then fall through */
  303. case CPIA2_CMD_GET_SYSTEM_CTRL:
  304. cmd.req_mode =
  305. CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  306. cmd.reg_count = 1;
  307. cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
  308. break;
  309. case CPIA2_CMD_SET_VP_SYSTEM_CTRL:
  310. cmd.buffer.block_data[0] = param; /* Then fall through */
  311. case CPIA2_CMD_GET_VP_SYSTEM_CTRL:
  312. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  313. cmd.reg_count = 1;
  314. cmd.start = CPIA2_VP_SYSTEMCTRL;
  315. break;
  316. case CPIA2_CMD_SET_VP_EXP_MODES:
  317. cmd.buffer.block_data[0] = param; /* Then fall through */
  318. case CPIA2_CMD_GET_VP_EXP_MODES:
  319. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  320. cmd.reg_count = 1;
  321. cmd.start = CPIA2_VP_EXPOSURE_MODES;
  322. break;
  323. case CPIA2_CMD_SET_DEVICE_CONFIG:
  324. cmd.buffer.block_data[0] = param; /* Then fall through */
  325. case CPIA2_CMD_GET_DEVICE_CONFIG:
  326. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  327. cmd.reg_count = 1;
  328. cmd.start = CPIA2_VP_DEVICE_CONFIG;
  329. break;
  330. case CPIA2_CMD_SET_SERIAL_ADDR:
  331. cmd.buffer.block_data[0] = param;
  332. cmd.req_mode =
  333. CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  334. cmd.reg_count = 1;
  335. cmd.start = CPIA2_SYSTEM_VP_SERIAL_ADDR;
  336. break;
  337. case CPIA2_CMD_SET_SENSOR_CR1:
  338. cmd.buffer.block_data[0] = param;
  339. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  340. cmd.reg_count = 1;
  341. cmd.start = CPIA2_SENSOR_CR1;
  342. break;
  343. case CPIA2_CMD_SET_VC_CONTROL:
  344. cmd.buffer.block_data[0] = param; /* Then fall through */
  345. case CPIA2_CMD_GET_VC_CONTROL:
  346. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
  347. cmd.reg_count = 1;
  348. cmd.start = CPIA2_VC_VC_CTRL;
  349. break;
  350. case CPIA2_CMD_SET_TARGET_KB:
  351. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
  352. cmd.reg_count = 1;
  353. cmd.buffer.registers[0].index = CPIA2_VC_VC_TARGET_KB;
  354. cmd.buffer.registers[0].value = param;
  355. break;
  356. case CPIA2_CMD_SET_DEF_JPEG_OPT:
  357. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
  358. cmd.reg_count = 4;
  359. cmd.buffer.registers[0].index = CPIA2_VC_VC_JPEG_OPT;
  360. cmd.buffer.registers[0].value =
  361. CPIA2_VC_VC_JPEG_OPT_DOUBLE_SQUEEZE;
  362. cmd.buffer.registers[1].index = CPIA2_VC_VC_USER_SQUEEZE;
  363. cmd.buffer.registers[1].value = 20;
  364. cmd.buffer.registers[2].index = CPIA2_VC_VC_CREEP_PERIOD;
  365. cmd.buffer.registers[2].value = 2;
  366. cmd.buffer.registers[3].index = CPIA2_VC_VC_JPEG_OPT;
  367. cmd.buffer.registers[3].value = CPIA2_VC_VC_JPEG_OPT_DEFAULT;
  368. break;
  369. case CPIA2_CMD_REHASH_VP4:
  370. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  371. cmd.reg_count = 1;
  372. cmd.start = CPIA2_VP_REHASH_VALUES;
  373. cmd.buffer.block_data[0] = param;
  374. break;
  375. case CPIA2_CMD_SET_USER_EFFECTS: /* Note: Be careful with this as
  376. this register can also affect
  377. flicker modes */
  378. cmd.buffer.block_data[0] = param; /* Then fall through */
  379. case CPIA2_CMD_GET_USER_EFFECTS:
  380. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  381. cmd.reg_count = 1;
  382. if (device == DEVICE_STV_672)
  383. cmd.start = CPIA2_VP4_USER_EFFECTS;
  384. else
  385. cmd.start = CPIA2_VP5_USER_EFFECTS;
  386. break;
  387. default:
  388. LOG("DoCommand received invalid command\n");
  389. return -EINVAL;
  390. }
  391. retval = cpia2_send_command(cam, &cmd);
  392. if (retval) {
  393. return retval;
  394. }
  395. /***
  396. * Now copy any results from a read into the appropriate param struct.
  397. ***/
  398. switch (command) {
  399. case CPIA2_CMD_GET_VERSION:
  400. cam->params.version.firmware_revision_hi =
  401. cmd.buffer.block_data[0];
  402. cam->params.version.firmware_revision_lo =
  403. cmd.buffer.block_data[1];
  404. break;
  405. case CPIA2_CMD_GET_PNP_ID:
  406. cam->params.pnp_id.vendor = (cmd.buffer.block_data[0] << 8) |
  407. cmd.buffer.block_data[1];
  408. cam->params.pnp_id.product = (cmd.buffer.block_data[2] << 8) |
  409. cmd.buffer.block_data[3];
  410. cam->params.pnp_id.device_revision =
  411. (cmd.buffer.block_data[4] << 8) |
  412. cmd.buffer.block_data[5];
  413. if (cam->params.pnp_id.vendor == 0x553) {
  414. if (cam->params.pnp_id.product == 0x100) {
  415. cam->params.pnp_id.device_type = DEVICE_STV_672;
  416. } else if (cam->params.pnp_id.product == 0x140 ||
  417. cam->params.pnp_id.product == 0x151) {
  418. cam->params.pnp_id.device_type = DEVICE_STV_676;
  419. }
  420. }
  421. break;
  422. case CPIA2_CMD_GET_ASIC_TYPE:
  423. cam->params.version.asic_id = cmd.buffer.block_data[0];
  424. cam->params.version.asic_rev = cmd.buffer.block_data[1];
  425. break;
  426. case CPIA2_CMD_GET_SENSOR:
  427. cam->params.version.sensor_flags = cmd.buffer.block_data[0];
  428. cam->params.version.sensor_rev = cmd.buffer.block_data[1];
  429. break;
  430. case CPIA2_CMD_GET_VP_DEVICE:
  431. cam->params.version.vp_device_hi = cmd.buffer.block_data[0];
  432. cam->params.version.vp_device_lo = cmd.buffer.block_data[1];
  433. break;
  434. case CPIA2_CMD_GET_VP_GPIO_DATA:
  435. cam->params.vp_params.gpio_data = cmd.buffer.block_data[0];
  436. break;
  437. case CPIA2_CMD_GET_VP_GPIO_DIRECTION:
  438. cam->params.vp_params.gpio_direction = cmd.buffer.block_data[0];
  439. break;
  440. case CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION:
  441. cam->params.vc_params.vc_mp_direction =cmd.buffer.block_data[0];
  442. break;
  443. case CPIA2_CMD_GET_VC_MP_GPIO_DATA:
  444. cam->params.vc_params.vc_mp_data = cmd.buffer.block_data[0];
  445. break;
  446. case CPIA2_CMD_GET_FLICKER_MODES:
  447. cam->params.flicker_control.cam_register =
  448. cmd.buffer.block_data[0];
  449. break;
  450. case CPIA2_CMD_GET_WAKEUP:
  451. cam->params.vc_params.wakeup = cmd.buffer.block_data[0];
  452. break;
  453. case CPIA2_CMD_GET_PW_CONTROL:
  454. cam->params.vc_params.pw_control = cmd.buffer.block_data[0];
  455. break;
  456. case CPIA2_CMD_GET_SYSTEM_CTRL:
  457. cam->params.camera_state.system_ctrl = cmd.buffer.block_data[0];
  458. break;
  459. case CPIA2_CMD_GET_VP_SYSTEM_STATE:
  460. cam->params.vp_params.system_state = cmd.buffer.block_data[0];
  461. break;
  462. case CPIA2_CMD_GET_VP_SYSTEM_CTRL:
  463. cam->params.vp_params.system_ctrl = cmd.buffer.block_data[0];
  464. break;
  465. case CPIA2_CMD_GET_VP_EXP_MODES:
  466. cam->params.vp_params.exposure_modes = cmd.buffer.block_data[0];
  467. break;
  468. case CPIA2_CMD_GET_DEVICE_CONFIG:
  469. cam->params.vp_params.device_config = cmd.buffer.block_data[0];
  470. break;
  471. case CPIA2_CMD_GET_VC_CONTROL:
  472. cam->params.vc_params.vc_control = cmd.buffer.block_data[0];
  473. break;
  474. case CPIA2_CMD_GET_USER_MODE:
  475. cam->params.vp_params.video_mode = cmd.buffer.block_data[0];
  476. break;
  477. case CPIA2_CMD_GET_USER_EFFECTS:
  478. cam->params.vp_params.user_effects = cmd.buffer.block_data[0];
  479. break;
  480. default:
  481. break;
  482. }
  483. return retval;
  484. }
  485. /******************************************************************************
  486. *
  487. * cpia2_send_command
  488. *
  489. *****************************************************************************/
  490. #define DIR(cmd) ((cmd->direction == TRANSFER_WRITE) ? "Write" : "Read")
  491. #define BINDEX(cmd) (cmd->req_mode & 0x03)
  492. int cpia2_send_command(struct camera_data *cam, struct cpia2_command *cmd)
  493. {
  494. u8 count;
  495. u8 start;
  496. u8 *buffer;
  497. int retval;
  498. switch (cmd->req_mode & 0x0c) {
  499. case CAMERAACCESS_TYPE_RANDOM:
  500. count = cmd->reg_count * sizeof(struct cpia2_register);
  501. start = 0;
  502. buffer = (u8 *) & cmd->buffer;
  503. if (debugs_on & DEBUG_REG)
  504. DBG("%s Random: Register block %s\n", DIR(cmd),
  505. block_name[BINDEX(cmd)]);
  506. break;
  507. case CAMERAACCESS_TYPE_BLOCK:
  508. count = cmd->reg_count;
  509. start = cmd->start;
  510. buffer = cmd->buffer.block_data;
  511. if (debugs_on & DEBUG_REG)
  512. DBG("%s Block: Register block %s\n", DIR(cmd),
  513. block_name[BINDEX(cmd)]);
  514. break;
  515. case CAMERAACCESS_TYPE_MASK:
  516. count = cmd->reg_count * sizeof(struct cpia2_reg_mask);
  517. start = 0;
  518. buffer = (u8 *) & cmd->buffer;
  519. if (debugs_on & DEBUG_REG)
  520. DBG("%s Mask: Register block %s\n", DIR(cmd),
  521. block_name[BINDEX(cmd)]);
  522. break;
  523. case CAMERAACCESS_TYPE_REPEAT: /* For patch blocks only */
  524. count = cmd->reg_count;
  525. start = cmd->start;
  526. buffer = cmd->buffer.block_data;
  527. if (debugs_on & DEBUG_REG)
  528. DBG("%s Repeat: Register block %s\n", DIR(cmd),
  529. block_name[BINDEX(cmd)]);
  530. break;
  531. default:
  532. LOG("%s: invalid request mode\n",__func__);
  533. return -EINVAL;
  534. }
  535. retval = cpia2_usb_transfer_cmd(cam,
  536. buffer,
  537. cmd->req_mode,
  538. start, count, cmd->direction);
  539. #ifdef _CPIA2_DEBUG_
  540. if (debugs_on & DEBUG_REG) {
  541. int i;
  542. for (i = 0; i < cmd->reg_count; i++) {
  543. if((cmd->req_mode & 0x0c) == CAMERAACCESS_TYPE_BLOCK)
  544. KINFO("%s Block: [0x%02X] = 0x%02X\n",
  545. DIR(cmd), start + i, buffer[i]);
  546. if((cmd->req_mode & 0x0c) == CAMERAACCESS_TYPE_RANDOM)
  547. KINFO("%s Random: [0x%02X] = 0x%02X\n",
  548. DIR(cmd), cmd->buffer.registers[i].index,
  549. cmd->buffer.registers[i].value);
  550. }
  551. }
  552. #endif
  553. return retval;
  554. };
  555. /*************
  556. * Functions to implement camera functionality
  557. *************/
  558. /******************************************************************************
  559. *
  560. * cpia2_get_version_info
  561. *
  562. *****************************************************************************/
  563. static void cpia2_get_version_info(struct camera_data *cam)
  564. {
  565. cpia2_do_command(cam, CPIA2_CMD_GET_VERSION, TRANSFER_READ, 0);
  566. cpia2_do_command(cam, CPIA2_CMD_GET_PNP_ID, TRANSFER_READ, 0);
  567. cpia2_do_command(cam, CPIA2_CMD_GET_ASIC_TYPE, TRANSFER_READ, 0);
  568. cpia2_do_command(cam, CPIA2_CMD_GET_SENSOR, TRANSFER_READ, 0);
  569. cpia2_do_command(cam, CPIA2_CMD_GET_VP_DEVICE, TRANSFER_READ, 0);
  570. }
  571. /******************************************************************************
  572. *
  573. * cpia2_reset_camera
  574. *
  575. * Called at least during the open process, sets up initial params.
  576. *****************************************************************************/
  577. int cpia2_reset_camera(struct camera_data *cam)
  578. {
  579. u8 tmp_reg;
  580. int retval = 0;
  581. int target_kb;
  582. int i;
  583. struct cpia2_command cmd;
  584. /***
  585. * VC setup
  586. ***/
  587. retval = configure_sensor(cam,
  588. cam->params.roi.width,
  589. cam->params.roi.height);
  590. if (retval < 0) {
  591. ERR("Couldn't configure sensor, error=%d\n", retval);
  592. return retval;
  593. }
  594. /* Clear FIFO and route/enable stream block */
  595. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
  596. cmd.direction = TRANSFER_WRITE;
  597. cmd.reg_count = 2;
  598. cmd.buffer.registers[0].index = CPIA2_VC_ST_CTRL;
  599. cmd.buffer.registers[0].value = CPIA2_VC_ST_CTRL_SRC_VC |
  600. CPIA2_VC_ST_CTRL_DST_USB | CPIA2_VC_ST_CTRL_EOF_DETECT;
  601. cmd.buffer.registers[1].index = CPIA2_VC_ST_CTRL;
  602. cmd.buffer.registers[1].value = CPIA2_VC_ST_CTRL_SRC_VC |
  603. CPIA2_VC_ST_CTRL_DST_USB |
  604. CPIA2_VC_ST_CTRL_EOF_DETECT | CPIA2_VC_ST_CTRL_FIFO_ENABLE;
  605. cpia2_send_command(cam, &cmd);
  606. cpia2_set_high_power(cam);
  607. if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
  608. /* Enable button notification */
  609. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_SYSTEM;
  610. cmd.buffer.registers[0].index = CPIA2_SYSTEM_INT_PACKET_CTRL;
  611. cmd.buffer.registers[0].value =
  612. CPIA2_SYSTEM_INT_PACKET_CTRL_ENABLE_SW_XX;
  613. cmd.reg_count = 1;
  614. cpia2_send_command(cam, &cmd);
  615. }
  616. schedule_timeout_interruptible(msecs_to_jiffies(100));
  617. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  618. retval = apply_vp_patch(cam);
  619. /* wait for vp to go to sleep */
  620. schedule_timeout_interruptible(msecs_to_jiffies(100));
  621. /***
  622. * If this is a 676, apply VP5 fixes before we start streaming
  623. ***/
  624. if (cam->params.pnp_id.device_type == DEVICE_STV_676) {
  625. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
  626. /* The following writes improve the picture */
  627. cmd.buffer.registers[0].index = CPIA2_VP5_MYBLACK_LEVEL;
  628. cmd.buffer.registers[0].value = 0; /* reduce from the default
  629. * rec 601 pedestal of 16 */
  630. cmd.buffer.registers[1].index = CPIA2_VP5_MCYRANGE;
  631. cmd.buffer.registers[1].value = 0x92; /* increase from 100% to
  632. * (256/256 - 31) to fill
  633. * available range */
  634. cmd.buffer.registers[2].index = CPIA2_VP5_MYCEILING;
  635. cmd.buffer.registers[2].value = 0xFF; /* Increase from the
  636. * default rec 601 ceiling
  637. * of 240 */
  638. cmd.buffer.registers[3].index = CPIA2_VP5_MCUVSATURATION;
  639. cmd.buffer.registers[3].value = 0xFF; /* Increase from the rec
  640. * 601 100% level (128)
  641. * to 145-192 */
  642. cmd.buffer.registers[4].index = CPIA2_VP5_ANTIFLKRSETUP;
  643. cmd.buffer.registers[4].value = 0x80; /* Inhibit the
  644. * anti-flicker */
  645. /* The following 4 writes are a fix to allow QVGA to work at 30 fps */
  646. cmd.buffer.registers[5].index = CPIA2_VP_RAM_ADDR_H;
  647. cmd.buffer.registers[5].value = 0x01;
  648. cmd.buffer.registers[6].index = CPIA2_VP_RAM_ADDR_L;
  649. cmd.buffer.registers[6].value = 0xE3;
  650. cmd.buffer.registers[7].index = CPIA2_VP_RAM_DATA;
  651. cmd.buffer.registers[7].value = 0x02;
  652. cmd.buffer.registers[8].index = CPIA2_VP_RAM_DATA;
  653. cmd.buffer.registers[8].value = 0xFC;
  654. cmd.direction = TRANSFER_WRITE;
  655. cmd.reg_count = 9;
  656. cpia2_send_command(cam, &cmd);
  657. }
  658. /* Activate all settings and start the data stream */
  659. /* Set user mode */
  660. set_default_user_mode(cam);
  661. /* Give VP time to wake up */
  662. schedule_timeout_interruptible(msecs_to_jiffies(100));
  663. set_all_properties(cam);
  664. cpia2_do_command(cam, CPIA2_CMD_GET_USER_MODE, TRANSFER_READ, 0);
  665. DBG("After SetAllProperties(cam), user mode is 0x%0X\n",
  666. cam->params.vp_params.video_mode);
  667. /***
  668. * Set audio regulator off. This and the code to set the compresison
  669. * state are too complex to form a CPIA2_CMD_, and seem to be somewhat
  670. * intertwined. This stuff came straight from the windows driver.
  671. ***/
  672. /* Turn AutoExposure off in VP and enable the serial bridge to the sensor */
  673. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_CTRL, TRANSFER_READ, 0);
  674. tmp_reg = cam->params.vp_params.system_ctrl;
  675. cmd.buffer.registers[0].value = tmp_reg &
  676. (tmp_reg & (CPIA2_VP_SYSTEMCTRL_HK_CONTROL ^ 0xFF));
  677. cpia2_do_command(cam, CPIA2_CMD_GET_DEVICE_CONFIG, TRANSFER_READ, 0);
  678. cmd.buffer.registers[1].value = cam->params.vp_params.device_config |
  679. CPIA2_VP_DEVICE_CONFIG_SERIAL_BRIDGE;
  680. cmd.buffer.registers[0].index = CPIA2_VP_SYSTEMCTRL;
  681. cmd.buffer.registers[1].index = CPIA2_VP_DEVICE_CONFIG;
  682. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
  683. cmd.reg_count = 2;
  684. cmd.direction = TRANSFER_WRITE;
  685. cmd.start = 0;
  686. cpia2_send_command(cam, &cmd);
  687. /* Set the correct I2C address in the CPiA-2 system register */
  688. cpia2_do_command(cam,
  689. CPIA2_CMD_SET_SERIAL_ADDR,
  690. TRANSFER_WRITE,
  691. CPIA2_SYSTEM_VP_SERIAL_ADDR_SENSOR);
  692. /* Now have sensor access - set bit to turn the audio regulator off */
  693. cpia2_do_command(cam,
  694. CPIA2_CMD_SET_SENSOR_CR1,
  695. TRANSFER_WRITE, CPIA2_SENSOR_CR1_DOWN_AUDIO_REGULATOR);
  696. /* Set the correct I2C address in the CPiA-2 system register */
  697. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  698. cpia2_do_command(cam,
  699. CPIA2_CMD_SET_SERIAL_ADDR,
  700. TRANSFER_WRITE,
  701. CPIA2_SYSTEM_VP_SERIAL_ADDR_VP); // 0x88
  702. else
  703. cpia2_do_command(cam,
  704. CPIA2_CMD_SET_SERIAL_ADDR,
  705. TRANSFER_WRITE,
  706. CPIA2_SYSTEM_VP_SERIAL_ADDR_676_VP); // 0x8a
  707. /* increase signal drive strength */
  708. if (cam->params.pnp_id.device_type == DEVICE_STV_676)
  709. cpia2_do_command(cam,
  710. CPIA2_CMD_SET_VP_EXP_MODES,
  711. TRANSFER_WRITE,
  712. CPIA2_VP_EXPOSURE_MODES_COMPILE_EXP);
  713. /* Start autoexposure */
  714. cpia2_do_command(cam, CPIA2_CMD_GET_DEVICE_CONFIG, TRANSFER_READ, 0);
  715. cmd.buffer.registers[0].value = cam->params.vp_params.device_config &
  716. (CPIA2_VP_DEVICE_CONFIG_SERIAL_BRIDGE ^ 0xFF);
  717. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_CTRL, TRANSFER_READ, 0);
  718. cmd.buffer.registers[1].value =
  719. cam->params.vp_params.system_ctrl | CPIA2_VP_SYSTEMCTRL_HK_CONTROL;
  720. cmd.buffer.registers[0].index = CPIA2_VP_DEVICE_CONFIG;
  721. cmd.buffer.registers[1].index = CPIA2_VP_SYSTEMCTRL;
  722. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
  723. cmd.reg_count = 2;
  724. cmd.direction = TRANSFER_WRITE;
  725. cpia2_send_command(cam, &cmd);
  726. /* Set compression state */
  727. cpia2_do_command(cam, CPIA2_CMD_GET_VC_CONTROL, TRANSFER_READ, 0);
  728. if (cam->params.compression.inhibit_htables) {
  729. tmp_reg = cam->params.vc_params.vc_control |
  730. CPIA2_VC_VC_CTRL_INHIBIT_H_TABLES;
  731. } else {
  732. tmp_reg = cam->params.vc_params.vc_control &
  733. ~CPIA2_VC_VC_CTRL_INHIBIT_H_TABLES;
  734. }
  735. cpia2_do_command(cam, CPIA2_CMD_SET_VC_CONTROL, TRANSFER_WRITE,tmp_reg);
  736. /* Set target size (kb) on vc
  737. This is a heuristic based on the quality parameter and the raw
  738. framesize in kB divided by 16 (the compression factor when the
  739. quality is 100%) */
  740. target_kb = (cam->width * cam->height * 2 / 16384) *
  741. cam->params.vc_params.quality / 100;
  742. if (target_kb < 1)
  743. target_kb = 1;
  744. cpia2_do_command(cam, CPIA2_CMD_SET_TARGET_KB,
  745. TRANSFER_WRITE, target_kb);
  746. /* Wiggle VC Reset */
  747. /***
  748. * First read and wait a bit.
  749. ***/
  750. for (i = 0; i < 50; i++) {
  751. cpia2_do_command(cam, CPIA2_CMD_GET_PW_CONTROL,
  752. TRANSFER_READ, 0);
  753. }
  754. tmp_reg = cam->params.vc_params.pw_control;
  755. tmp_reg &= ~CPIA2_VC_PW_CTRL_VC_RESET_N;
  756. cpia2_do_command(cam, CPIA2_CMD_SET_PW_CONTROL, TRANSFER_WRITE,tmp_reg);
  757. tmp_reg |= CPIA2_VC_PW_CTRL_VC_RESET_N;
  758. cpia2_do_command(cam, CPIA2_CMD_SET_PW_CONTROL, TRANSFER_WRITE,tmp_reg);
  759. cpia2_do_command(cam, CPIA2_CMD_SET_DEF_JPEG_OPT, TRANSFER_WRITE, 0);
  760. cpia2_do_command(cam, CPIA2_CMD_GET_USER_MODE, TRANSFER_READ, 0);
  761. DBG("After VC RESET, user mode is 0x%0X\n",
  762. cam->params.vp_params.video_mode);
  763. return retval;
  764. }
  765. /******************************************************************************
  766. *
  767. * cpia2_set_high_power
  768. *
  769. *****************************************************************************/
  770. static int cpia2_set_high_power(struct camera_data *cam)
  771. {
  772. int i;
  773. for (i = 0; i <= 50; i++) {
  774. /* Read system status */
  775. cpia2_do_command(cam,CPIA2_CMD_GET_SYSTEM_CTRL,TRANSFER_READ,0);
  776. /* If there is an error, clear it */
  777. if(cam->params.camera_state.system_ctrl &
  778. CPIA2_SYSTEM_CONTROL_V2W_ERR)
  779. cpia2_do_command(cam, CPIA2_CMD_CLEAR_V2W_ERR,
  780. TRANSFER_WRITE, 0);
  781. /* Try to set high power mode */
  782. cpia2_do_command(cam, CPIA2_CMD_SET_SYSTEM_CTRL,
  783. TRANSFER_WRITE, 1);
  784. /* Try to read something in VP to check if everything is awake */
  785. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_STATE,
  786. TRANSFER_READ, 0);
  787. if (cam->params.vp_params.system_state &
  788. CPIA2_VP_SYSTEMSTATE_HK_ALIVE) {
  789. break;
  790. } else if (i == 50) {
  791. cam->params.camera_state.power_mode = LO_POWER_MODE;
  792. ERR("Camera did not wake up\n");
  793. return -EIO;
  794. }
  795. }
  796. DBG("System now in high power state\n");
  797. cam->params.camera_state.power_mode = HI_POWER_MODE;
  798. return 0;
  799. }
  800. /******************************************************************************
  801. *
  802. * cpia2_set_low_power
  803. *
  804. *****************************************************************************/
  805. int cpia2_set_low_power(struct camera_data *cam)
  806. {
  807. cam->params.camera_state.power_mode = LO_POWER_MODE;
  808. cpia2_do_command(cam, CPIA2_CMD_SET_SYSTEM_CTRL, TRANSFER_WRITE, 0);
  809. return 0;
  810. }
  811. /******************************************************************************
  812. *
  813. * apply_vp_patch
  814. *
  815. *****************************************************************************/
  816. static int cpia2_send_onebyte_command(struct camera_data *cam,
  817. struct cpia2_command *cmd,
  818. u8 start, u8 datum)
  819. {
  820. cmd->buffer.block_data[0] = datum;
  821. cmd->start = start;
  822. cmd->reg_count = 1;
  823. return cpia2_send_command(cam, cmd);
  824. }
  825. static int apply_vp_patch(struct camera_data *cam)
  826. {
  827. const struct firmware *fw;
  828. const char fw_name[] = FIRMWARE;
  829. int i, ret;
  830. struct cpia2_command cmd;
  831. ret = reject_firmware(&fw, fw_name, &cam->dev->dev);
  832. if (ret) {
  833. printk(KERN_ERR "cpia2: failed to load VP patch \"%s\"\n",
  834. fw_name);
  835. return ret;
  836. }
  837. cmd.req_mode = CAMERAACCESS_TYPE_REPEAT | CAMERAACCESS_VP;
  838. cmd.direction = TRANSFER_WRITE;
  839. /* First send the start address... */
  840. cpia2_send_onebyte_command(cam, &cmd, 0x0A, fw->data[0]); /* hi */
  841. cpia2_send_onebyte_command(cam, &cmd, 0x0B, fw->data[1]); /* lo */
  842. /* ... followed by the data payload */
  843. for (i = 2; i < fw->size; i += 64) {
  844. cmd.start = 0x0C; /* Data */
  845. cmd.reg_count = min_t(uint, 64, fw->size - i);
  846. memcpy(cmd.buffer.block_data, &fw->data[i], cmd.reg_count);
  847. cpia2_send_command(cam, &cmd);
  848. }
  849. /* Next send the start address... */
  850. cpia2_send_onebyte_command(cam, &cmd, 0x0A, fw->data[0]); /* hi */
  851. cpia2_send_onebyte_command(cam, &cmd, 0x0B, fw->data[1]); /* lo */
  852. /* ... followed by the 'goto' command */
  853. cpia2_send_onebyte_command(cam, &cmd, 0x0D, 1);
  854. release_firmware(fw);
  855. return 0;
  856. }
  857. /******************************************************************************
  858. *
  859. * set_default_user_mode
  860. *
  861. *****************************************************************************/
  862. static int set_default_user_mode(struct camera_data *cam)
  863. {
  864. unsigned char user_mode;
  865. unsigned char frame_rate;
  866. int width = cam->params.roi.width;
  867. int height = cam->params.roi.height;
  868. switch (cam->params.version.sensor_flags) {
  869. case CPIA2_VP_SENSOR_FLAGS_404:
  870. case CPIA2_VP_SENSOR_FLAGS_407:
  871. case CPIA2_VP_SENSOR_FLAGS_409:
  872. case CPIA2_VP_SENSOR_FLAGS_410:
  873. if ((width > STV_IMAGE_QCIF_COLS)
  874. || (height > STV_IMAGE_QCIF_ROWS)) {
  875. user_mode = CPIA2_VP_USER_MODE_CIF;
  876. } else {
  877. user_mode = CPIA2_VP_USER_MODE_QCIFDS;
  878. }
  879. frame_rate = CPIA2_VP_FRAMERATE_30;
  880. break;
  881. case CPIA2_VP_SENSOR_FLAGS_500:
  882. if ((width > STV_IMAGE_CIF_COLS)
  883. || (height > STV_IMAGE_CIF_ROWS)) {
  884. user_mode = CPIA2_VP_USER_MODE_VGA;
  885. } else {
  886. user_mode = CPIA2_VP_USER_MODE_QVGADS;
  887. }
  888. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  889. frame_rate = CPIA2_VP_FRAMERATE_15;
  890. else
  891. frame_rate = CPIA2_VP_FRAMERATE_30;
  892. break;
  893. default:
  894. LOG("%s: Invalid sensor flag value 0x%0X\n",__func__,
  895. cam->params.version.sensor_flags);
  896. return -EINVAL;
  897. }
  898. DBG("Sensor flag = 0x%0x, user mode = 0x%0x, frame rate = 0x%X\n",
  899. cam->params.version.sensor_flags, user_mode, frame_rate);
  900. cpia2_do_command(cam, CPIA2_CMD_SET_USER_MODE, TRANSFER_WRITE,
  901. user_mode);
  902. if(cam->params.vp_params.frame_rate > 0 &&
  903. frame_rate > cam->params.vp_params.frame_rate)
  904. frame_rate = cam->params.vp_params.frame_rate;
  905. cpia2_set_fps(cam, frame_rate);
  906. // if (cam->params.pnp_id.device_type == DEVICE_STV_676)
  907. // cpia2_do_command(cam,
  908. // CPIA2_CMD_SET_VP_SYSTEM_CTRL,
  909. // TRANSFER_WRITE,
  910. // CPIA2_VP_SYSTEMCTRL_HK_CONTROL |
  911. // CPIA2_VP_SYSTEMCTRL_POWER_CONTROL);
  912. return 0;
  913. }
  914. /******************************************************************************
  915. *
  916. * cpia2_match_video_size
  917. *
  918. * return the best match, where 'best' is as always
  919. * the largest that is not bigger than what is requested.
  920. *****************************************************************************/
  921. int cpia2_match_video_size(int width, int height)
  922. {
  923. if (width >= STV_IMAGE_VGA_COLS && height >= STV_IMAGE_VGA_ROWS)
  924. return VIDEOSIZE_VGA;
  925. if (width >= STV_IMAGE_CIF_COLS && height >= STV_IMAGE_CIF_ROWS)
  926. return VIDEOSIZE_CIF;
  927. if (width >= STV_IMAGE_QVGA_COLS && height >= STV_IMAGE_QVGA_ROWS)
  928. return VIDEOSIZE_QVGA;
  929. if (width >= 288 && height >= 216)
  930. return VIDEOSIZE_288_216;
  931. if (width >= 256 && height >= 192)
  932. return VIDEOSIZE_256_192;
  933. if (width >= 224 && height >= 168)
  934. return VIDEOSIZE_224_168;
  935. if (width >= 192 && height >= 144)
  936. return VIDEOSIZE_192_144;
  937. if (width >= STV_IMAGE_QCIF_COLS && height >= STV_IMAGE_QCIF_ROWS)
  938. return VIDEOSIZE_QCIF;
  939. return -1;
  940. }
  941. /******************************************************************************
  942. *
  943. * SetVideoSize
  944. *
  945. *****************************************************************************/
  946. static int set_vw_size(struct camera_data *cam, int size)
  947. {
  948. int retval = 0;
  949. cam->params.vp_params.video_size = size;
  950. switch (size) {
  951. case VIDEOSIZE_VGA:
  952. DBG("Setting size to VGA\n");
  953. cam->params.roi.width = STV_IMAGE_VGA_COLS;
  954. cam->params.roi.height = STV_IMAGE_VGA_ROWS;
  955. cam->width = STV_IMAGE_VGA_COLS;
  956. cam->height = STV_IMAGE_VGA_ROWS;
  957. break;
  958. case VIDEOSIZE_CIF:
  959. DBG("Setting size to CIF\n");
  960. cam->params.roi.width = STV_IMAGE_CIF_COLS;
  961. cam->params.roi.height = STV_IMAGE_CIF_ROWS;
  962. cam->width = STV_IMAGE_CIF_COLS;
  963. cam->height = STV_IMAGE_CIF_ROWS;
  964. break;
  965. case VIDEOSIZE_QVGA:
  966. DBG("Setting size to QVGA\n");
  967. cam->params.roi.width = STV_IMAGE_QVGA_COLS;
  968. cam->params.roi.height = STV_IMAGE_QVGA_ROWS;
  969. cam->width = STV_IMAGE_QVGA_COLS;
  970. cam->height = STV_IMAGE_QVGA_ROWS;
  971. break;
  972. case VIDEOSIZE_288_216:
  973. cam->params.roi.width = 288;
  974. cam->params.roi.height = 216;
  975. cam->width = 288;
  976. cam->height = 216;
  977. break;
  978. case VIDEOSIZE_256_192:
  979. cam->width = 256;
  980. cam->height = 192;
  981. cam->params.roi.width = 256;
  982. cam->params.roi.height = 192;
  983. break;
  984. case VIDEOSIZE_224_168:
  985. cam->width = 224;
  986. cam->height = 168;
  987. cam->params.roi.width = 224;
  988. cam->params.roi.height = 168;
  989. break;
  990. case VIDEOSIZE_192_144:
  991. cam->width = 192;
  992. cam->height = 144;
  993. cam->params.roi.width = 192;
  994. cam->params.roi.height = 144;
  995. break;
  996. case VIDEOSIZE_QCIF:
  997. DBG("Setting size to QCIF\n");
  998. cam->params.roi.width = STV_IMAGE_QCIF_COLS;
  999. cam->params.roi.height = STV_IMAGE_QCIF_ROWS;
  1000. cam->width = STV_IMAGE_QCIF_COLS;
  1001. cam->height = STV_IMAGE_QCIF_ROWS;
  1002. break;
  1003. default:
  1004. retval = -EINVAL;
  1005. }
  1006. return retval;
  1007. }
  1008. /******************************************************************************
  1009. *
  1010. * configure_sensor
  1011. *
  1012. *****************************************************************************/
  1013. static int configure_sensor(struct camera_data *cam,
  1014. int req_width, int req_height)
  1015. {
  1016. int retval;
  1017. switch (cam->params.version.sensor_flags) {
  1018. case CPIA2_VP_SENSOR_FLAGS_404:
  1019. case CPIA2_VP_SENSOR_FLAGS_407:
  1020. case CPIA2_VP_SENSOR_FLAGS_409:
  1021. case CPIA2_VP_SENSOR_FLAGS_410:
  1022. retval = config_sensor_410(cam, req_width, req_height);
  1023. break;
  1024. case CPIA2_VP_SENSOR_FLAGS_500:
  1025. retval = config_sensor_500(cam, req_width, req_height);
  1026. break;
  1027. default:
  1028. return -EINVAL;
  1029. }
  1030. return retval;
  1031. }
  1032. /******************************************************************************
  1033. *
  1034. * config_sensor_410
  1035. *
  1036. *****************************************************************************/
  1037. static int config_sensor_410(struct camera_data *cam,
  1038. int req_width, int req_height)
  1039. {
  1040. struct cpia2_command cmd;
  1041. int i = 0;
  1042. int image_size;
  1043. int image_type;
  1044. int width = req_width;
  1045. int height = req_height;
  1046. /***
  1047. * Make sure size doesn't exceed CIF.
  1048. ***/
  1049. if (width > STV_IMAGE_CIF_COLS)
  1050. width = STV_IMAGE_CIF_COLS;
  1051. if (height > STV_IMAGE_CIF_ROWS)
  1052. height = STV_IMAGE_CIF_ROWS;
  1053. image_size = cpia2_match_video_size(width, height);
  1054. DBG("Config 410: width = %d, height = %d\n", width, height);
  1055. DBG("Image size returned is %d\n", image_size);
  1056. if (image_size >= 0) {
  1057. set_vw_size(cam, image_size);
  1058. width = cam->params.roi.width;
  1059. height = cam->params.roi.height;
  1060. DBG("After set_vw_size(), width = %d, height = %d\n",
  1061. width, height);
  1062. if (width <= 176 && height <= 144) {
  1063. DBG("image type = VIDEOSIZE_QCIF\n");
  1064. image_type = VIDEOSIZE_QCIF;
  1065. }
  1066. else if (width <= 320 && height <= 240) {
  1067. DBG("image type = VIDEOSIZE_QVGA\n");
  1068. image_type = VIDEOSIZE_QVGA;
  1069. }
  1070. else {
  1071. DBG("image type = VIDEOSIZE_CIF\n");
  1072. image_type = VIDEOSIZE_CIF;
  1073. }
  1074. } else {
  1075. ERR("ConfigSensor410 failed\n");
  1076. return -EINVAL;
  1077. }
  1078. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
  1079. cmd.direction = TRANSFER_WRITE;
  1080. /* VC Format */
  1081. cmd.buffer.registers[i].index = CPIA2_VC_VC_FORMAT;
  1082. if (image_type == VIDEOSIZE_CIF) {
  1083. cmd.buffer.registers[i++].value =
  1084. (u8) (CPIA2_VC_VC_FORMAT_UFIRST |
  1085. CPIA2_VC_VC_FORMAT_SHORTLINE);
  1086. } else {
  1087. cmd.buffer.registers[i++].value =
  1088. (u8) CPIA2_VC_VC_FORMAT_UFIRST;
  1089. }
  1090. /* VC Clocks */
  1091. cmd.buffer.registers[i].index = CPIA2_VC_VC_CLOCKS;
  1092. if (image_type == VIDEOSIZE_QCIF) {
  1093. if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
  1094. cmd.buffer.registers[i++].value=
  1095. (u8)(CPIA2_VC_VC_672_CLOCKS_CIF_DIV_BY_3 |
  1096. CPIA2_VC_VC_672_CLOCKS_SCALING |
  1097. CPIA2_VC_VC_CLOCKS_LOGDIV2);
  1098. DBG("VC_Clocks (0xc4) should be B\n");
  1099. }
  1100. else {
  1101. cmd.buffer.registers[i++].value=
  1102. (u8)(CPIA2_VC_VC_676_CLOCKS_CIF_DIV_BY_3 |
  1103. CPIA2_VC_VC_CLOCKS_LOGDIV2);
  1104. }
  1105. } else {
  1106. if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
  1107. cmd.buffer.registers[i++].value =
  1108. (u8) (CPIA2_VC_VC_672_CLOCKS_CIF_DIV_BY_3 |
  1109. CPIA2_VC_VC_CLOCKS_LOGDIV0);
  1110. }
  1111. else {
  1112. cmd.buffer.registers[i++].value =
  1113. (u8) (CPIA2_VC_VC_676_CLOCKS_CIF_DIV_BY_3 |
  1114. CPIA2_VC_VC_676_CLOCKS_SCALING |
  1115. CPIA2_VC_VC_CLOCKS_LOGDIV0);
  1116. }
  1117. }
  1118. DBG("VC_Clocks (0xc4) = 0x%0X\n", cmd.buffer.registers[i-1].value);
  1119. /* Input reqWidth from VC */
  1120. cmd.buffer.registers[i].index = CPIA2_VC_VC_IHSIZE_LO;
  1121. if (image_type == VIDEOSIZE_QCIF)
  1122. cmd.buffer.registers[i++].value =
  1123. (u8) (STV_IMAGE_QCIF_COLS / 4);
  1124. else
  1125. cmd.buffer.registers[i++].value =
  1126. (u8) (STV_IMAGE_CIF_COLS / 4);
  1127. /* Timings */
  1128. cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_HI;
  1129. if (image_type == VIDEOSIZE_QCIF)
  1130. cmd.buffer.registers[i++].value = (u8) 0;
  1131. else
  1132. cmd.buffer.registers[i++].value = (u8) 1;
  1133. cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_LO;
  1134. if (image_type == VIDEOSIZE_QCIF)
  1135. cmd.buffer.registers[i++].value = (u8) 208;
  1136. else
  1137. cmd.buffer.registers[i++].value = (u8) 160;
  1138. cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_HI;
  1139. if (image_type == VIDEOSIZE_QCIF)
  1140. cmd.buffer.registers[i++].value = (u8) 0;
  1141. else
  1142. cmd.buffer.registers[i++].value = (u8) 1;
  1143. cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_LO;
  1144. if (image_type == VIDEOSIZE_QCIF)
  1145. cmd.buffer.registers[i++].value = (u8) 160;
  1146. else
  1147. cmd.buffer.registers[i++].value = (u8) 64;
  1148. /* Output Image Size */
  1149. cmd.buffer.registers[i].index = CPIA2_VC_VC_OHSIZE;
  1150. cmd.buffer.registers[i++].value = cam->params.roi.width / 4;
  1151. cmd.buffer.registers[i].index = CPIA2_VC_VC_OVSIZE;
  1152. cmd.buffer.registers[i++].value = cam->params.roi.height / 4;
  1153. /* Cropping */
  1154. cmd.buffer.registers[i].index = CPIA2_VC_VC_HCROP;
  1155. if (image_type == VIDEOSIZE_QCIF)
  1156. cmd.buffer.registers[i++].value =
  1157. (u8) (((STV_IMAGE_QCIF_COLS / 4) - (width / 4)) / 2);
  1158. else
  1159. cmd.buffer.registers[i++].value =
  1160. (u8) (((STV_IMAGE_CIF_COLS / 4) - (width / 4)) / 2);
  1161. cmd.buffer.registers[i].index = CPIA2_VC_VC_VCROP;
  1162. if (image_type == VIDEOSIZE_QCIF)
  1163. cmd.buffer.registers[i++].value =
  1164. (u8) (((STV_IMAGE_QCIF_ROWS / 4) - (height / 4)) / 2);
  1165. else
  1166. cmd.buffer.registers[i++].value =
  1167. (u8) (((STV_IMAGE_CIF_ROWS / 4) - (height / 4)) / 2);
  1168. /* Scaling registers (defaults) */
  1169. cmd.buffer.registers[i].index = CPIA2_VC_VC_HPHASE;
  1170. cmd.buffer.registers[i++].value = (u8) 0;
  1171. cmd.buffer.registers[i].index = CPIA2_VC_VC_VPHASE;
  1172. cmd.buffer.registers[i++].value = (u8) 0;
  1173. cmd.buffer.registers[i].index = CPIA2_VC_VC_HISPAN;
  1174. cmd.buffer.registers[i++].value = (u8) 31;
  1175. cmd.buffer.registers[i].index = CPIA2_VC_VC_VISPAN;
  1176. cmd.buffer.registers[i++].value = (u8) 31;
  1177. cmd.buffer.registers[i].index = CPIA2_VC_VC_HICROP;
  1178. cmd.buffer.registers[i++].value = (u8) 0;
  1179. cmd.buffer.registers[i].index = CPIA2_VC_VC_VICROP;
  1180. cmd.buffer.registers[i++].value = (u8) 0;
  1181. cmd.buffer.registers[i].index = CPIA2_VC_VC_HFRACT;
  1182. cmd.buffer.registers[i++].value = (u8) 0x81; /* = 8/1 = 8 (HIBYTE/LOBYTE) */
  1183. cmd.buffer.registers[i].index = CPIA2_VC_VC_VFRACT;
  1184. cmd.buffer.registers[i++].value = (u8) 0x81; /* = 8/1 = 8 (HIBYTE/LOBYTE) */
  1185. cmd.reg_count = i;
  1186. cpia2_send_command(cam, &cmd);
  1187. return i;
  1188. }
  1189. /******************************************************************************
  1190. *
  1191. * config_sensor_500(cam)
  1192. *
  1193. *****************************************************************************/
  1194. static int config_sensor_500(struct camera_data *cam,
  1195. int req_width, int req_height)
  1196. {
  1197. struct cpia2_command cmd;
  1198. int i = 0;
  1199. int image_size = VIDEOSIZE_CIF;
  1200. int image_type = VIDEOSIZE_VGA;
  1201. int width = req_width;
  1202. int height = req_height;
  1203. unsigned int device = cam->params.pnp_id.device_type;
  1204. image_size = cpia2_match_video_size(width, height);
  1205. if (width > STV_IMAGE_CIF_COLS || height > STV_IMAGE_CIF_ROWS)
  1206. image_type = VIDEOSIZE_VGA;
  1207. else if (width > STV_IMAGE_QVGA_COLS || height > STV_IMAGE_QVGA_ROWS)
  1208. image_type = VIDEOSIZE_CIF;
  1209. else if (width > STV_IMAGE_QCIF_COLS || height > STV_IMAGE_QCIF_ROWS)
  1210. image_type = VIDEOSIZE_QVGA;
  1211. else
  1212. image_type = VIDEOSIZE_QCIF;
  1213. if (image_size >= 0) {
  1214. set_vw_size(cam, image_size);
  1215. width = cam->params.roi.width;
  1216. height = cam->params.roi.height;
  1217. } else {
  1218. ERR("ConfigSensor500 failed\n");
  1219. return -EINVAL;
  1220. }
  1221. DBG("image_size = %d, width = %d, height = %d, type = %d\n",
  1222. image_size, width, height, image_type);
  1223. cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
  1224. cmd.direction = TRANSFER_WRITE;
  1225. i = 0;
  1226. /* VC Format */
  1227. cmd.buffer.registers[i].index = CPIA2_VC_VC_FORMAT;
  1228. cmd.buffer.registers[i].value = (u8) CPIA2_VC_VC_FORMAT_UFIRST;
  1229. if (image_type == VIDEOSIZE_QCIF)
  1230. cmd.buffer.registers[i].value |= (u8) CPIA2_VC_VC_FORMAT_DECIMATING;
  1231. i++;
  1232. /* VC Clocks */
  1233. cmd.buffer.registers[i].index = CPIA2_VC_VC_CLOCKS;
  1234. if (device == DEVICE_STV_672) {
  1235. if (image_type == VIDEOSIZE_VGA)
  1236. cmd.buffer.registers[i].value =
  1237. (u8)CPIA2_VC_VC_CLOCKS_LOGDIV1;
  1238. else
  1239. cmd.buffer.registers[i].value =
  1240. (u8)(CPIA2_VC_VC_672_CLOCKS_SCALING |
  1241. CPIA2_VC_VC_CLOCKS_LOGDIV3);
  1242. } else {
  1243. if (image_type == VIDEOSIZE_VGA)
  1244. cmd.buffer.registers[i].value =
  1245. (u8)CPIA2_VC_VC_CLOCKS_LOGDIV0;
  1246. else
  1247. cmd.buffer.registers[i].value =
  1248. (u8)(CPIA2_VC_VC_676_CLOCKS_SCALING |
  1249. CPIA2_VC_VC_CLOCKS_LOGDIV2);
  1250. }
  1251. i++;
  1252. DBG("VC_CLOCKS = 0x%X\n", cmd.buffer.registers[i-1].value);
  1253. /* Input width from VP */
  1254. cmd.buffer.registers[i].index = CPIA2_VC_VC_IHSIZE_LO;
  1255. if (image_type == VIDEOSIZE_VGA)
  1256. cmd.buffer.registers[i].value =
  1257. (u8) (STV_IMAGE_VGA_COLS / 4);
  1258. else
  1259. cmd.buffer.registers[i].value =
  1260. (u8) (STV_IMAGE_QVGA_COLS / 4);
  1261. i++;
  1262. DBG("Input width = %d\n", cmd.buffer.registers[i-1].value);
  1263. /* Timings */
  1264. cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_HI;
  1265. if (image_type == VIDEOSIZE_VGA)
  1266. cmd.buffer.registers[i++].value = (u8) 2;
  1267. else
  1268. cmd.buffer.registers[i++].value = (u8) 1;
  1269. cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_LO;
  1270. if (image_type == VIDEOSIZE_VGA)
  1271. cmd.buffer.registers[i++].value = (u8) 250;
  1272. else if (image_type == VIDEOSIZE_QVGA)
  1273. cmd.buffer.registers[i++].value = (u8) 125;
  1274. else
  1275. cmd.buffer.registers[i++].value = (u8) 160;
  1276. cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_HI;
  1277. if (image_type == VIDEOSIZE_VGA)
  1278. cmd.buffer.registers[i++].value = (u8) 2;
  1279. else
  1280. cmd.buffer.registers[i++].value = (u8) 1;
  1281. cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_LO;
  1282. if (image_type == VIDEOSIZE_VGA)
  1283. cmd.buffer.registers[i++].value = (u8) 12;
  1284. else if (image_type == VIDEOSIZE_QVGA)
  1285. cmd.buffer.registers[i++].value = (u8) 64;
  1286. else
  1287. cmd.buffer.registers[i++].value = (u8) 6;
  1288. /* Output Image Size */
  1289. cmd.buffer.registers[i].index = CPIA2_VC_VC_OHSIZE;
  1290. if (image_type == VIDEOSIZE_QCIF)
  1291. cmd.buffer.registers[i++].value = STV_IMAGE_CIF_COLS / 4;
  1292. else
  1293. cmd.buffer.registers[i++].value = width / 4;
  1294. cmd.buffer.registers[i].index = CPIA2_VC_VC_OVSIZE;
  1295. if (image_type == VIDEOSIZE_QCIF)
  1296. cmd.buffer.registers[i++].value = STV_IMAGE_CIF_ROWS / 4;
  1297. else
  1298. cmd.buffer.registers[i++].value = height / 4;
  1299. /* Cropping */
  1300. cmd.buffer.registers[i].index = CPIA2_VC_VC_HCROP;
  1301. if (image_type == VIDEOSIZE_VGA)
  1302. cmd.buffer.registers[i++].value =
  1303. (u8) (((STV_IMAGE_VGA_COLS / 4) - (width / 4)) / 2);
  1304. else if (image_type == VIDEOSIZE_QVGA)
  1305. cmd.buffer.registers[i++].value =
  1306. (u8) (((STV_IMAGE_QVGA_COLS / 4) - (width / 4)) / 2);
  1307. else if (image_type == VIDEOSIZE_CIF)
  1308. cmd.buffer.registers[i++].value =
  1309. (u8) (((STV_IMAGE_CIF_COLS / 4) - (width / 4)) / 2);
  1310. else /*if (image_type == VIDEOSIZE_QCIF)*/
  1311. cmd.buffer.registers[i++].value =
  1312. (u8) (((STV_IMAGE_QCIF_COLS / 4) - (width / 4)) / 2);
  1313. cmd.buffer.registers[i].index = CPIA2_VC_VC_VCROP;
  1314. if (image_type == VIDEOSIZE_VGA)
  1315. cmd.buffer.registers[i++].value =
  1316. (u8) (((STV_IMAGE_VGA_ROWS / 4) - (height / 4)) / 2);
  1317. else if (image_type == VIDEOSIZE_QVGA)
  1318. cmd.buffer.registers[i++].value =
  1319. (u8) (((STV_IMAGE_QVGA_ROWS / 4) - (height / 4)) / 2);
  1320. else if (image_type == VIDEOSIZE_CIF)
  1321. cmd.buffer.registers[i++].value =
  1322. (u8) (((STV_IMAGE_CIF_ROWS / 4) - (height / 4)) / 2);
  1323. else /*if (image_type == VIDEOSIZE_QCIF)*/
  1324. cmd.buffer.registers[i++].value =
  1325. (u8) (((STV_IMAGE_QCIF_ROWS / 4) - (height / 4)) / 2);
  1326. /* Scaling registers (defaults) */
  1327. cmd.buffer.registers[i].index = CPIA2_VC_VC_HPHASE;
  1328. if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
  1329. cmd.buffer.registers[i++].value = (u8) 36;
  1330. else
  1331. cmd.buffer.registers[i++].value = (u8) 0;
  1332. cmd.buffer.registers[i].index = CPIA2_VC_VC_VPHASE;
  1333. if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
  1334. cmd.buffer.registers[i++].value = (u8) 32;
  1335. else
  1336. cmd.buffer.registers[i++].value = (u8) 0;
  1337. cmd.buffer.registers[i].index = CPIA2_VC_VC_HISPAN;
  1338. if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
  1339. cmd.buffer.registers[i++].value = (u8) 26;
  1340. else
  1341. cmd.buffer.registers[i++].value = (u8) 31;
  1342. cmd.buffer.registers[i].index = CPIA2_VC_VC_VISPAN;
  1343. if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
  1344. cmd.buffer.registers[i++].value = (u8) 21;
  1345. else
  1346. cmd.buffer.registers[i++].value = (u8) 31;
  1347. cmd.buffer.registers[i].index = CPIA2_VC_VC_HICROP;
  1348. cmd.buffer.registers[i++].value = (u8) 0;
  1349. cmd.buffer.registers[i].index = CPIA2_VC_VC_VICROP;
  1350. cmd.buffer.registers[i++].value = (u8) 0;
  1351. cmd.buffer.registers[i].index = CPIA2_VC_VC_HFRACT;
  1352. if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
  1353. cmd.buffer.registers[i++].value = (u8) 0x2B; /* 2/11 */
  1354. else
  1355. cmd.buffer.registers[i++].value = (u8) 0x81; /* 8/1 */
  1356. cmd.buffer.registers[i].index = CPIA2_VC_VC_VFRACT;
  1357. if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
  1358. cmd.buffer.registers[i++].value = (u8) 0x13; /* 1/3 */
  1359. else
  1360. cmd.buffer.registers[i++].value = (u8) 0x81; /* 8/1 */
  1361. cmd.reg_count = i;
  1362. cpia2_send_command(cam, &cmd);
  1363. return i;
  1364. }
  1365. /******************************************************************************
  1366. *
  1367. * setallproperties
  1368. *
  1369. * This sets all user changeable properties to the values in cam->params.
  1370. *****************************************************************************/
  1371. static int set_all_properties(struct camera_data *cam)
  1372. {
  1373. /**
  1374. * Don't set target_kb here, it will be set later.
  1375. * framerate and user_mode were already set (set_default_user_mode).
  1376. **/
  1377. cpia2_usb_change_streaming_alternate(cam,
  1378. cam->params.camera_state.stream_mode);
  1379. cpia2_do_command(cam,
  1380. CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
  1381. TRANSFER_WRITE, cam->params.vp_params.gpio_direction);
  1382. cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DATA, TRANSFER_WRITE,
  1383. cam->params.vp_params.gpio_data);
  1384. v4l2_ctrl_handler_setup(&cam->hdl);
  1385. wake_system(cam);
  1386. set_lowlight_boost(cam);
  1387. return 0;
  1388. }
  1389. /******************************************************************************
  1390. *
  1391. * cpia2_save_camera_state
  1392. *
  1393. *****************************************************************************/
  1394. void cpia2_save_camera_state(struct camera_data *cam)
  1395. {
  1396. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
  1397. cpia2_do_command(cam, CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION, TRANSFER_READ,
  1398. 0);
  1399. cpia2_do_command(cam, CPIA2_CMD_GET_VC_MP_GPIO_DATA, TRANSFER_READ, 0);
  1400. /* Don't get framerate or target_kb. Trust the values we already have */
  1401. }
  1402. /******************************************************************************
  1403. *
  1404. * cpia2_set_flicker_mode
  1405. *
  1406. *****************************************************************************/
  1407. int cpia2_set_flicker_mode(struct camera_data *cam, int mode)
  1408. {
  1409. unsigned char cam_reg;
  1410. int err = 0;
  1411. if(cam->params.pnp_id.device_type != DEVICE_STV_672)
  1412. return -EINVAL;
  1413. /* Set the appropriate bits in FLICKER_MODES, preserving the rest */
  1414. if((err = cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
  1415. TRANSFER_READ, 0)))
  1416. return err;
  1417. cam_reg = cam->params.flicker_control.cam_register;
  1418. switch(mode) {
  1419. case NEVER_FLICKER:
  1420. cam_reg |= CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
  1421. cam_reg &= ~CPIA2_VP_FLICKER_MODES_50HZ;
  1422. break;
  1423. case FLICKER_60:
  1424. cam_reg &= ~CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
  1425. cam_reg &= ~CPIA2_VP_FLICKER_MODES_50HZ;
  1426. break;
  1427. case FLICKER_50:
  1428. cam_reg &= ~CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
  1429. cam_reg |= CPIA2_VP_FLICKER_MODES_50HZ;
  1430. break;
  1431. default:
  1432. return -EINVAL;
  1433. }
  1434. if((err = cpia2_do_command(cam, CPIA2_CMD_SET_FLICKER_MODES,
  1435. TRANSFER_WRITE, cam_reg)))
  1436. return err;
  1437. /* Set the appropriate bits in EXP_MODES, preserving the rest */
  1438. if((err = cpia2_do_command(cam, CPIA2_CMD_GET_VP_EXP_MODES,
  1439. TRANSFER_READ, 0)))
  1440. return err;
  1441. cam_reg = cam->params.vp_params.exposure_modes;
  1442. if (mode == NEVER_FLICKER) {
  1443. cam_reg |= CPIA2_VP_EXPOSURE_MODES_INHIBIT_FLICKER;
  1444. } else {
  1445. cam_reg &= ~CPIA2_VP_EXPOSURE_MODES_INHIBIT_FLICKER;
  1446. }
  1447. if((err = cpia2_do_command(cam, CPIA2_CMD_SET_VP_EXP_MODES,
  1448. TRANSFER_WRITE, cam_reg)))
  1449. return err;
  1450. if((err = cpia2_do_command(cam, CPIA2_CMD_REHASH_VP4,
  1451. TRANSFER_WRITE, 1)))
  1452. return err;
  1453. switch(mode) {
  1454. case NEVER_FLICKER:
  1455. case FLICKER_60:
  1456. case FLICKER_50:
  1457. cam->params.flicker_control.flicker_mode_req = mode;
  1458. break;
  1459. default:
  1460. err = -EINVAL;
  1461. }
  1462. return err;
  1463. }
  1464. /******************************************************************************
  1465. *
  1466. * cpia2_set_property_flip
  1467. *
  1468. *****************************************************************************/
  1469. void cpia2_set_property_flip(struct camera_data *cam, int prop_val)
  1470. {
  1471. unsigned char cam_reg;
  1472. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
  1473. cam_reg = cam->params.vp_params.user_effects;
  1474. if (prop_val)
  1475. {
  1476. cam_reg |= CPIA2_VP_USER_EFFECTS_FLIP;
  1477. }
  1478. else
  1479. {
  1480. cam_reg &= ~CPIA2_VP_USER_EFFECTS_FLIP;
  1481. }
  1482. cam->params.vp_params.user_effects = cam_reg;
  1483. cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
  1484. cam_reg);
  1485. }
  1486. /******************************************************************************
  1487. *
  1488. * cpia2_set_property_mirror
  1489. *
  1490. *****************************************************************************/
  1491. void cpia2_set_property_mirror(struct camera_data *cam, int prop_val)
  1492. {
  1493. unsigned char cam_reg;
  1494. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
  1495. cam_reg = cam->params.vp_params.user_effects;
  1496. if (prop_val)
  1497. {
  1498. cam_reg |= CPIA2_VP_USER_EFFECTS_MIRROR;
  1499. }
  1500. else
  1501. {
  1502. cam_reg &= ~CPIA2_VP_USER_EFFECTS_MIRROR;
  1503. }
  1504. cam->params.vp_params.user_effects = cam_reg;
  1505. cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
  1506. cam_reg);
  1507. }
  1508. /******************************************************************************
  1509. *
  1510. * cpia2_set_gpio
  1511. *
  1512. *****************************************************************************/
  1513. int cpia2_set_gpio(struct camera_data *cam, unsigned char setting)
  1514. {
  1515. int ret;
  1516. /* Set the microport direction (register 0x90, should be defined
  1517. * already) to 1 (user output), and set the microport data (0x91) to
  1518. * the value in the ioctl argument.
  1519. */
  1520. ret = cpia2_do_command(cam,
  1521. CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
  1522. CPIA2_VC_MP_DIR_OUTPUT,
  1523. 255);
  1524. if (ret < 0)
  1525. return ret;
  1526. cam->params.vp_params.gpio_direction = 255;
  1527. ret = cpia2_do_command(cam,
  1528. CPIA2_CMD_SET_VC_MP_GPIO_DATA,
  1529. CPIA2_VC_MP_DIR_OUTPUT,
  1530. setting);
  1531. if (ret < 0)
  1532. return ret;
  1533. cam->params.vp_params.gpio_data = setting;
  1534. return 0;
  1535. }
  1536. /******************************************************************************
  1537. *
  1538. * cpia2_set_fps
  1539. *
  1540. *****************************************************************************/
  1541. int cpia2_set_fps(struct camera_data *cam, int framerate)
  1542. {
  1543. int retval;
  1544. switch(framerate) {
  1545. case CPIA2_VP_FRAMERATE_30:
  1546. case CPIA2_VP_FRAMERATE_25:
  1547. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  1548. cam->params.version.sensor_flags ==
  1549. CPIA2_VP_SENSOR_FLAGS_500) {
  1550. return -EINVAL;
  1551. }
  1552. /* Fall through */
  1553. case CPIA2_VP_FRAMERATE_15:
  1554. case CPIA2_VP_FRAMERATE_12_5:
  1555. case CPIA2_VP_FRAMERATE_7_5:
  1556. case CPIA2_VP_FRAMERATE_6_25:
  1557. break;
  1558. default:
  1559. return -EINVAL;
  1560. }
  1561. if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  1562. framerate == CPIA2_VP_FRAMERATE_15)
  1563. framerate = 0; /* Work around bug in VP4 */
  1564. retval = cpia2_do_command(cam,
  1565. CPIA2_CMD_FRAMERATE_REQ,
  1566. TRANSFER_WRITE,
  1567. framerate);
  1568. if(retval == 0)
  1569. cam->params.vp_params.frame_rate = framerate;
  1570. return retval;
  1571. }
  1572. /******************************************************************************
  1573. *
  1574. * cpia2_set_brightness
  1575. *
  1576. *****************************************************************************/
  1577. void cpia2_set_brightness(struct camera_data *cam, unsigned char value)
  1578. {
  1579. /***
  1580. * Don't let the register be set to zero - bug in VP4 - flash of full
  1581. * brightness
  1582. ***/
  1583. if (cam->params.pnp_id.device_type == DEVICE_STV_672 && value == 0)
  1584. value++;
  1585. DBG("Setting brightness to %d (0x%0x)\n", value, value);
  1586. cpia2_do_command(cam, CPIA2_CMD_SET_VP_BRIGHTNESS, TRANSFER_WRITE, value);
  1587. }
  1588. /******************************************************************************
  1589. *
  1590. * cpia2_set_contrast
  1591. *
  1592. *****************************************************************************/
  1593. void cpia2_set_contrast(struct camera_data *cam, unsigned char value)
  1594. {
  1595. DBG("Setting contrast to %d (0x%0x)\n", value, value);
  1596. cpia2_do_command(cam, CPIA2_CMD_SET_CONTRAST, TRANSFER_WRITE, value);
  1597. }
  1598. /******************************************************************************
  1599. *
  1600. * cpia2_set_saturation
  1601. *
  1602. *****************************************************************************/
  1603. void cpia2_set_saturation(struct camera_data *cam, unsigned char value)
  1604. {
  1605. DBG("Setting saturation to %d (0x%0x)\n", value, value);
  1606. cpia2_do_command(cam,CPIA2_CMD_SET_VP_SATURATION, TRANSFER_WRITE,value);
  1607. }
  1608. /******************************************************************************
  1609. *
  1610. * wake_system
  1611. *
  1612. *****************************************************************************/
  1613. static void wake_system(struct camera_data *cam)
  1614. {
  1615. cpia2_do_command(cam, CPIA2_CMD_SET_WAKEUP, TRANSFER_WRITE, 0);
  1616. }
  1617. /******************************************************************************
  1618. *
  1619. * set_lowlight_boost
  1620. *
  1621. * Valid for STV500 sensor only
  1622. *****************************************************************************/
  1623. static void set_lowlight_boost(struct camera_data *cam)
  1624. {
  1625. struct cpia2_command cmd;
  1626. if (cam->params.pnp_id.device_type != DEVICE_STV_672 ||
  1627. cam->params.version.sensor_flags != CPIA2_VP_SENSOR_FLAGS_500)
  1628. return;
  1629. cmd.direction = TRANSFER_WRITE;
  1630. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  1631. cmd.reg_count = 3;
  1632. cmd.start = CPIA2_VP_RAM_ADDR_H;
  1633. cmd.buffer.block_data[0] = 0; /* High byte of address to write to */
  1634. cmd.buffer.block_data[1] = 0x59; /* Low byte of address to write to */
  1635. cmd.buffer.block_data[2] = 0; /* High byte of data to write */
  1636. cpia2_send_command(cam, &cmd);
  1637. if (cam->params.vp_params.lowlight_boost) {
  1638. cmd.buffer.block_data[0] = 0x02; /* Low byte data to write */
  1639. } else {
  1640. cmd.buffer.block_data[0] = 0x06;
  1641. }
  1642. cmd.start = CPIA2_VP_RAM_DATA;
  1643. cmd.reg_count = 1;
  1644. cpia2_send_command(cam, &cmd);
  1645. /* Rehash the VP4 values */
  1646. cpia2_do_command(cam, CPIA2_CMD_REHASH_VP4, TRANSFER_WRITE, 1);
  1647. }
  1648. /******************************************************************************
  1649. *
  1650. * cpia2_set_format
  1651. *
  1652. * Assumes that new size is already set in param struct.
  1653. *****************************************************************************/
  1654. void cpia2_set_format(struct camera_data *cam)
  1655. {
  1656. cam->flush = true;
  1657. cpia2_usb_stream_pause(cam);
  1658. /* reset camera to new size */
  1659. cpia2_set_low_power(cam);
  1660. cpia2_reset_camera(cam);
  1661. cam->flush = false;
  1662. cpia2_dbg_dump_registers(cam);
  1663. cpia2_usb_stream_resume(cam);
  1664. }
  1665. /******************************************************************************
  1666. *
  1667. * cpia2_dbg_dump_registers
  1668. *
  1669. *****************************************************************************/
  1670. void cpia2_dbg_dump_registers(struct camera_data *cam)
  1671. {
  1672. #ifdef _CPIA2_DEBUG_
  1673. struct cpia2_command cmd;
  1674. if (!(debugs_on & DEBUG_DUMP_REGS))
  1675. return;
  1676. cmd.direction = TRANSFER_READ;
  1677. /* Start with bank 0 (SYSTEM) */
  1678. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
  1679. cmd.reg_count = 3;
  1680. cmd.start = 0;
  1681. cpia2_send_command(cam, &cmd);
  1682. printk(KERN_DEBUG "System Device Hi = 0x%X\n",
  1683. cmd.buffer.block_data[0]);
  1684. printk(KERN_DEBUG "System Device Lo = 0x%X\n",
  1685. cmd.buffer.block_data[1]);
  1686. printk(KERN_DEBUG "System_system control = 0x%X\n",
  1687. cmd.buffer.block_data[2]);
  1688. /* Bank 1 (VC) */
  1689. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
  1690. cmd.reg_count = 4;
  1691. cmd.start = 0x80;
  1692. cpia2_send_command(cam, &cmd);
  1693. printk(KERN_DEBUG "ASIC_ID = 0x%X\n",
  1694. cmd.buffer.block_data[0]);
  1695. printk(KERN_DEBUG "ASIC_REV = 0x%X\n",
  1696. cmd.buffer.block_data[1]);
  1697. printk(KERN_DEBUG "PW_CONTRL = 0x%X\n",
  1698. cmd.buffer.block_data[2]);
  1699. printk(KERN_DEBUG "WAKEUP = 0x%X\n",
  1700. cmd.buffer.block_data[3]);
  1701. cmd.start = 0xA0; /* ST_CTRL */
  1702. cmd.reg_count = 1;
  1703. cpia2_send_command(cam, &cmd);
  1704. printk(KERN_DEBUG "Stream ctrl = 0x%X\n",
  1705. cmd.buffer.block_data[0]);
  1706. cmd.start = 0xA4; /* Stream status */
  1707. cpia2_send_command(cam, &cmd);
  1708. printk(KERN_DEBUG "Stream status = 0x%X\n",
  1709. cmd.buffer.block_data[0]);
  1710. cmd.start = 0xA8; /* USB status */
  1711. cmd.reg_count = 3;
  1712. cpia2_send_command(cam, &cmd);
  1713. printk(KERN_DEBUG "USB_CTRL = 0x%X\n",
  1714. cmd.buffer.block_data[0]);
  1715. printk(KERN_DEBUG "USB_STRM = 0x%X\n",
  1716. cmd.buffer.block_data[1]);
  1717. printk(KERN_DEBUG "USB_STATUS = 0x%X\n",
  1718. cmd.buffer.block_data[2]);
  1719. cmd.start = 0xAF; /* USB settings */
  1720. cmd.reg_count = 1;
  1721. cpia2_send_command(cam, &cmd);
  1722. printk(KERN_DEBUG "USB settings = 0x%X\n",
  1723. cmd.buffer.block_data[0]);
  1724. cmd.start = 0xC0; /* VC stuff */
  1725. cmd.reg_count = 26;
  1726. cpia2_send_command(cam, &cmd);
  1727. printk(KERN_DEBUG "VC Control = 0x%0X\n",
  1728. cmd.buffer.block_data[0]);
  1729. printk(KERN_DEBUG "VC Format = 0x%0X\n",
  1730. cmd.buffer.block_data[3]);
  1731. printk(KERN_DEBUG "VC Clocks = 0x%0X\n",
  1732. cmd.buffer.block_data[4]);
  1733. printk(KERN_DEBUG "VC IHSize = 0x%0X\n",
  1734. cmd.buffer.block_data[5]);
  1735. printk(KERN_DEBUG "VC Xlim Hi = 0x%0X\n",
  1736. cmd.buffer.block_data[6]);
  1737. printk(KERN_DEBUG "VC XLim Lo = 0x%0X\n",
  1738. cmd.buffer.block_data[7]);
  1739. printk(KERN_DEBUG "VC YLim Hi = 0x%0X\n",
  1740. cmd.buffer.block_data[8]);
  1741. printk(KERN_DEBUG "VC YLim Lo = 0x%0X\n",
  1742. cmd.buffer.block_data[9]);
  1743. printk(KERN_DEBUG "VC OHSize = 0x%0X\n",
  1744. cmd.buffer.block_data[10]);
  1745. printk(KERN_DEBUG "VC OVSize = 0x%0X\n",
  1746. cmd.buffer.block_data[11]);
  1747. printk(KERN_DEBUG "VC HCrop = 0x%0X\n",
  1748. cmd.buffer.block_data[12]);
  1749. printk(KERN_DEBUG "VC VCrop = 0x%0X\n",
  1750. cmd.buffer.block_data[13]);
  1751. printk(KERN_DEBUG "VC HPhase = 0x%0X\n",
  1752. cmd.buffer.block_data[14]);
  1753. printk(KERN_DEBUG "VC VPhase = 0x%0X\n",
  1754. cmd.buffer.block_data[15]);
  1755. printk(KERN_DEBUG "VC HIspan = 0x%0X\n",
  1756. cmd.buffer.block_data[16]);
  1757. printk(KERN_DEBUG "VC VIspan = 0x%0X\n",
  1758. cmd.buffer.block_data[17]);
  1759. printk(KERN_DEBUG "VC HiCrop = 0x%0X\n",
  1760. cmd.buffer.block_data[18]);
  1761. printk(KERN_DEBUG "VC ViCrop = 0x%0X\n",
  1762. cmd.buffer.block_data[19]);
  1763. printk(KERN_DEBUG "VC HiFract = 0x%0X\n",
  1764. cmd.buffer.block_data[20]);
  1765. printk(KERN_DEBUG "VC ViFract = 0x%0X\n",
  1766. cmd.buffer.block_data[21]);
  1767. printk(KERN_DEBUG "VC JPeg Opt = 0x%0X\n",
  1768. cmd.buffer.block_data[22]);
  1769. printk(KERN_DEBUG "VC Creep Per = 0x%0X\n",
  1770. cmd.buffer.block_data[23]);
  1771. printk(KERN_DEBUG "VC User Sq. = 0x%0X\n",
  1772. cmd.buffer.block_data[24]);
  1773. printk(KERN_DEBUG "VC Target KB = 0x%0X\n",
  1774. cmd.buffer.block_data[25]);
  1775. /*** VP ***/
  1776. cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
  1777. cmd.reg_count = 14;
  1778. cmd.start = 0;
  1779. cpia2_send_command(cam, &cmd);
  1780. printk(KERN_DEBUG "VP Dev Hi = 0x%0X\n",
  1781. cmd.buffer.block_data[0]);
  1782. printk(KERN_DEBUG "VP Dev Lo = 0x%0X\n",
  1783. cmd.buffer.block_data[1]);
  1784. printk(KERN_DEBUG "VP Sys State = 0x%0X\n",
  1785. cmd.buffer.block_data[2]);
  1786. printk(KERN_DEBUG "VP Sys Ctrl = 0x%0X\n",
  1787. cmd.buffer.block_data[3]);
  1788. printk(KERN_DEBUG "VP Sensor flg = 0x%0X\n",
  1789. cmd.buffer.block_data[5]);
  1790. printk(KERN_DEBUG "VP Sensor Rev = 0x%0X\n",
  1791. cmd.buffer.block_data[6]);
  1792. printk(KERN_DEBUG "VP Dev Config = 0x%0X\n",
  1793. cmd.buffer.block_data[7]);
  1794. printk(KERN_DEBUG "VP GPIO_DIR = 0x%0X\n",
  1795. cmd.buffer.block_data[8]);
  1796. printk(KERN_DEBUG "VP GPIO_DATA = 0x%0X\n",
  1797. cmd.buffer.block_data[9]);
  1798. printk(KERN_DEBUG "VP Ram ADDR H = 0x%0X\n",
  1799. cmd.buffer.block_data[10]);
  1800. printk(KERN_DEBUG "VP Ram ADDR L = 0x%0X\n",
  1801. cmd.buffer.block_data[11]);
  1802. printk(KERN_DEBUG "VP RAM Data = 0x%0X\n",
  1803. cmd.buffer.block_data[12]);
  1804. printk(KERN_DEBUG "Do Call = 0x%0X\n",
  1805. cmd.buffer.block_data[13]);
  1806. if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
  1807. cmd.reg_count = 9;
  1808. cmd.start = 0x0E;
  1809. cpia2_send_command(cam, &cmd);
  1810. printk(KERN_DEBUG "VP Clock Ctrl = 0x%0X\n",
  1811. cmd.buffer.block_data[0]);
  1812. printk(KERN_DEBUG "VP Patch Rev = 0x%0X\n",
  1813. cmd.buffer.block_data[1]);
  1814. printk(KERN_DEBUG "VP Vid Mode = 0x%0X\n",
  1815. cmd.buffer.block_data[2]);
  1816. printk(KERN_DEBUG "VP Framerate = 0x%0X\n",
  1817. cmd.buffer.block_data[3]);
  1818. printk(KERN_DEBUG "VP UserEffect = 0x%0X\n",
  1819. cmd.buffer.block_data[4]);
  1820. printk(KERN_DEBUG "VP White Bal = 0x%0X\n",
  1821. cmd.buffer.block_data[5]);
  1822. printk(KERN_DEBUG "VP WB thresh = 0x%0X\n",
  1823. cmd.buffer.block_data[6]);
  1824. printk(KERN_DEBUG "VP Exp Modes = 0x%0X\n",
  1825. cmd.buffer.block_data[7]);
  1826. printk(KERN_DEBUG "VP Exp Target = 0x%0X\n",
  1827. cmd.buffer.block_data[8]);
  1828. cmd.reg_count = 1;
  1829. cmd.start = 0x1B;
  1830. cpia2_send_command(cam, &cmd);
  1831. printk(KERN_DEBUG "VP FlickerMds = 0x%0X\n",
  1832. cmd.buffer.block_data[0]);
  1833. } else {
  1834. cmd.reg_count = 8 ;
  1835. cmd.start = 0x0E;
  1836. cpia2_send_command(cam, &cmd);
  1837. printk(KERN_DEBUG "VP Clock Ctrl = 0x%0X\n",
  1838. cmd.buffer.block_data[0]);
  1839. printk(KERN_DEBUG "VP Patch Rev = 0x%0X\n",
  1840. cmd.buffer.block_data[1]);
  1841. printk(KERN_DEBUG "VP Vid Mode = 0x%0X\n",
  1842. cmd.buffer.block_data[5]);
  1843. printk(KERN_DEBUG "VP Framerate = 0x%0X\n",
  1844. cmd.buffer.block_data[6]);
  1845. printk(KERN_DEBUG "VP UserEffect = 0x%0X\n",
  1846. cmd.buffer.block_data[7]);
  1847. cmd.reg_count = 1;
  1848. cmd.start = CPIA2_VP5_EXPOSURE_TARGET;
  1849. cpia2_send_command(cam, &cmd);
  1850. printk(KERN_DEBUG "VP5 Exp Target= 0x%0X\n",
  1851. cmd.buffer.block_data[0]);
  1852. cmd.reg_count = 4;
  1853. cmd.start = 0x3A;
  1854. cpia2_send_command(cam, &cmd);
  1855. printk(KERN_DEBUG "VP5 MY Black = 0x%0X\n",
  1856. cmd.buffer.block_data[0]);
  1857. printk(KERN_DEBUG "VP5 MCY Range = 0x%0X\n",
  1858. cmd.buffer.block_data[1]);
  1859. printk(KERN_DEBUG "VP5 MYCEILING = 0x%0X\n",
  1860. cmd.buffer.block_data[2]);
  1861. printk(KERN_DEBUG "VP5 MCUV Sat = 0x%0X\n",
  1862. cmd.buffer.block_data[3]);
  1863. }
  1864. #endif
  1865. }
  1866. /******************************************************************************
  1867. *
  1868. * reset_camera_struct
  1869. *
  1870. * Sets all values to the defaults
  1871. *****************************************************************************/
  1872. static void reset_camera_struct(struct camera_data *cam)
  1873. {
  1874. /***
  1875. * The following parameter values are the defaults from the register map.
  1876. ***/
  1877. cam->params.vp_params.lowlight_boost = 0;
  1878. /* FlickerModes */
  1879. cam->params.flicker_control.flicker_mode_req = NEVER_FLICKER;
  1880. /* jpeg params */
  1881. cam->params.compression.jpeg_options = CPIA2_VC_VC_JPEG_OPT_DEFAULT;
  1882. cam->params.compression.creep_period = 2;
  1883. cam->params.compression.user_squeeze = 20;
  1884. cam->params.compression.inhibit_htables = false;
  1885. /* gpio params */
  1886. cam->params.vp_params.gpio_direction = 0; /* write, the default safe mode */
  1887. cam->params.vp_params.gpio_data = 0;
  1888. /* Target kb params */
  1889. cam->params.vc_params.quality = 100;
  1890. /***
  1891. * Set Sensor FPS as fast as possible.
  1892. ***/
  1893. if(cam->params.pnp_id.device_type == DEVICE_STV_672) {
  1894. if(cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
  1895. cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_15;
  1896. else
  1897. cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_30;
  1898. } else {
  1899. cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_30;
  1900. }
  1901. /***
  1902. * Set default video mode as large as possible :
  1903. * for vga sensor set to vga, for cif sensor set to CIF.
  1904. ***/
  1905. if (cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500) {
  1906. cam->sensor_type = CPIA2_SENSOR_500;
  1907. cam->video_size = VIDEOSIZE_VGA;
  1908. cam->params.roi.width = STV_IMAGE_VGA_COLS;
  1909. cam->params.roi.height = STV_IMAGE_VGA_ROWS;
  1910. } else {
  1911. cam->sensor_type = CPIA2_SENSOR_410;
  1912. cam->video_size = VIDEOSIZE_CIF;
  1913. cam->params.roi.width = STV_IMAGE_CIF_COLS;
  1914. cam->params.roi.height = STV_IMAGE_CIF_ROWS;
  1915. }
  1916. cam->width = cam->params.roi.width;
  1917. cam->height = cam->params.roi.height;
  1918. }
  1919. /******************************************************************************
  1920. *
  1921. * cpia2_init_camera_struct
  1922. *
  1923. * Initializes camera struct, does not call reset to fill in defaults.
  1924. *****************************************************************************/
  1925. struct camera_data *cpia2_init_camera_struct(struct usb_interface *intf)
  1926. {
  1927. struct camera_data *cam;
  1928. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  1929. if (!cam) {
  1930. ERR("couldn't kmalloc cpia2 struct\n");
  1931. return NULL;
  1932. }
  1933. cam->v4l2_dev.release = cpia2_camera_release;
  1934. if (v4l2_device_register(&intf->dev, &cam->v4l2_dev) < 0) {
  1935. v4l2_err(&cam->v4l2_dev, "couldn't register v4l2_device\n");
  1936. kfree(cam);
  1937. return NULL;
  1938. }
  1939. mutex_init(&cam->v4l2_lock);
  1940. init_waitqueue_head(&cam->wq_stream);
  1941. return cam;
  1942. }
  1943. /******************************************************************************
  1944. *
  1945. * cpia2_init_camera
  1946. *
  1947. * Initializes camera.
  1948. *****************************************************************************/
  1949. int cpia2_init_camera(struct camera_data *cam)
  1950. {
  1951. DBG("Start\n");
  1952. cam->mmapped = false;
  1953. /* Get sensor and asic types before reset. */
  1954. cpia2_set_high_power(cam);
  1955. cpia2_get_version_info(cam);
  1956. if (cam->params.version.asic_id != CPIA2_ASIC_672) {
  1957. ERR("Device IO error (asicID has incorrect value of 0x%X\n",
  1958. cam->params.version.asic_id);
  1959. return -ENODEV;
  1960. }
  1961. /* Set GPIO direction and data to a safe state. */
  1962. cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
  1963. TRANSFER_WRITE, 0);
  1964. cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DATA,
  1965. TRANSFER_WRITE, 0);
  1966. /* resetting struct requires version info for sensor and asic types */
  1967. reset_camera_struct(cam);
  1968. cpia2_set_low_power(cam);
  1969. DBG("End\n");
  1970. return 0;
  1971. }
  1972. /******************************************************************************
  1973. *
  1974. * cpia2_allocate_buffers
  1975. *
  1976. *****************************************************************************/
  1977. int cpia2_allocate_buffers(struct camera_data *cam)
  1978. {
  1979. int i;
  1980. if(!cam->buffers) {
  1981. u32 size = cam->num_frames*sizeof(struct framebuf);
  1982. cam->buffers = kmalloc(size, GFP_KERNEL);
  1983. if(!cam->buffers) {
  1984. ERR("couldn't kmalloc frame buffer structures\n");
  1985. return -ENOMEM;
  1986. }
  1987. }
  1988. if(!cam->frame_buffer) {
  1989. cam->frame_buffer = rvmalloc(cam->frame_size*cam->num_frames);
  1990. if (!cam->frame_buffer) {
  1991. ERR("couldn't vmalloc frame buffer data area\n");
  1992. kfree(cam->buffers);
  1993. cam->buffers = NULL;
  1994. return -ENOMEM;
  1995. }
  1996. }
  1997. for(i=0; i<cam->num_frames-1; ++i) {
  1998. cam->buffers[i].next = &cam->buffers[i+1];
  1999. cam->buffers[i].data = cam->frame_buffer +i*cam->frame_size;
  2000. cam->buffers[i].status = FRAME_EMPTY;
  2001. cam->buffers[i].length = 0;
  2002. cam->buffers[i].max_length = 0;
  2003. cam->buffers[i].num = i;
  2004. }
  2005. cam->buffers[i].next = cam->buffers;
  2006. cam->buffers[i].data = cam->frame_buffer +i*cam->frame_size;
  2007. cam->buffers[i].status = FRAME_EMPTY;
  2008. cam->buffers[i].length = 0;
  2009. cam->buffers[i].max_length = 0;
  2010. cam->buffers[i].num = i;
  2011. cam->curbuff = cam->buffers;
  2012. cam->workbuff = cam->curbuff->next;
  2013. DBG("buffers=%p, curbuff=%p, workbuff=%p\n", cam->buffers, cam->curbuff,
  2014. cam->workbuff);
  2015. return 0;
  2016. }
  2017. /******************************************************************************
  2018. *
  2019. * cpia2_free_buffers
  2020. *
  2021. *****************************************************************************/
  2022. void cpia2_free_buffers(struct camera_data *cam)
  2023. {
  2024. if(cam->buffers) {
  2025. kfree(cam->buffers);
  2026. cam->buffers = NULL;
  2027. }
  2028. if(cam->frame_buffer) {
  2029. rvfree(cam->frame_buffer, cam->frame_size*cam->num_frames);
  2030. cam->frame_buffer = NULL;
  2031. }
  2032. }
  2033. /******************************************************************************
  2034. *
  2035. * cpia2_read
  2036. *
  2037. *****************************************************************************/
  2038. long cpia2_read(struct camera_data *cam,
  2039. char __user *buf, unsigned long count, int noblock)
  2040. {
  2041. struct framebuf *frame;
  2042. if (!count)
  2043. return 0;
  2044. if (!buf) {
  2045. ERR("%s: buffer NULL\n",__func__);
  2046. return -EINVAL;
  2047. }
  2048. if (!cam) {
  2049. ERR("%s: Internal error, camera_data NULL!\n",__func__);
  2050. return -EINVAL;
  2051. }
  2052. if (!cam->streaming) {
  2053. /* Start streaming */
  2054. cpia2_usb_stream_start(cam,
  2055. cam->params.camera_state.stream_mode);
  2056. }
  2057. /* Copy cam->curbuff in case it changes while we're processing */
  2058. frame = cam->curbuff;
  2059. if (noblock && frame->status != FRAME_READY) {
  2060. return -EAGAIN;
  2061. }
  2062. if (frame->status != FRAME_READY) {
  2063. mutex_unlock(&cam->v4l2_lock);
  2064. wait_event_interruptible(cam->wq_stream,
  2065. !video_is_registered(&cam->vdev) ||
  2066. (frame = cam->curbuff)->status == FRAME_READY);
  2067. mutex_lock(&cam->v4l2_lock);
  2068. if (signal_pending(current))
  2069. return -ERESTARTSYS;
  2070. if (!video_is_registered(&cam->vdev))
  2071. return 0;
  2072. }
  2073. /* copy data to user space */
  2074. if (frame->length > count)
  2075. return -EFAULT;
  2076. if (copy_to_user(buf, frame->data, frame->length))
  2077. return -EFAULT;
  2078. count = frame->length;
  2079. frame->status = FRAME_EMPTY;
  2080. return count;
  2081. }
  2082. /******************************************************************************
  2083. *
  2084. * cpia2_poll
  2085. *
  2086. *****************************************************************************/
  2087. unsigned int cpia2_poll(struct camera_data *cam, struct file *filp,
  2088. poll_table *wait)
  2089. {
  2090. unsigned int status = v4l2_ctrl_poll(filp, wait);
  2091. if ((poll_requested_events(wait) & (POLLIN | POLLRDNORM)) &&
  2092. !cam->streaming) {
  2093. /* Start streaming */
  2094. cpia2_usb_stream_start(cam,
  2095. cam->params.camera_state.stream_mode);
  2096. }
  2097. poll_wait(filp, &cam->wq_stream, wait);
  2098. if (cam->curbuff->status == FRAME_READY)
  2099. status |= POLLIN | POLLRDNORM;
  2100. return status;
  2101. }
  2102. /******************************************************************************
  2103. *
  2104. * cpia2_remap_buffer
  2105. *
  2106. *****************************************************************************/
  2107. int cpia2_remap_buffer(struct camera_data *cam, struct vm_area_struct *vma)
  2108. {
  2109. const char *adr = (const char *)vma->vm_start;
  2110. unsigned long size = vma->vm_end-vma->vm_start;
  2111. unsigned long start_offset = vma->vm_pgoff << PAGE_SHIFT;
  2112. unsigned long start = (unsigned long) adr;
  2113. unsigned long page, pos;
  2114. DBG("mmap offset:%ld size:%ld\n", start_offset, size);
  2115. if (!video_is_registered(&cam->vdev))
  2116. return -ENODEV;
  2117. if (size > cam->frame_size*cam->num_frames ||
  2118. (start_offset % cam->frame_size) != 0 ||
  2119. (start_offset+size > cam->frame_size*cam->num_frames))
  2120. return -EINVAL;
  2121. pos = ((unsigned long) (cam->frame_buffer)) + start_offset;
  2122. while (size > 0) {
  2123. page = kvirt_to_pa(pos);
  2124. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT, PAGE_SIZE, PAGE_SHARED))
  2125. return -EAGAIN;
  2126. start += PAGE_SIZE;
  2127. pos += PAGE_SIZE;
  2128. if (size > PAGE_SIZE)
  2129. size -= PAGE_SIZE;
  2130. else
  2131. size = 0;
  2132. }
  2133. cam->mmapped = true;
  2134. return 0;
  2135. }