cpia2_core.c 72 KB

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