isppreview.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366
  1. /*
  2. * isppreview.c
  3. *
  4. * TI OMAP3 ISP driver - Preview module
  5. *
  6. * Copyright (C) 2010 Nokia Corporation
  7. * Copyright (C) 2009 Texas Instruments, Inc.
  8. *
  9. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. * Sakari Ailus <sakari.ailus@iki.fi>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/uaccess.h>
  21. #include "isp.h"
  22. #include "ispreg.h"
  23. #include "isppreview.h"
  24. /* Default values in Office Fluorescent Light for RGBtoRGB Blending */
  25. static struct omap3isp_prev_rgbtorgb flr_rgb2rgb = {
  26. { /* RGB-RGB Matrix */
  27. {0x01E2, 0x0F30, 0x0FEE},
  28. {0x0F9B, 0x01AC, 0x0FB9},
  29. {0x0FE0, 0x0EC0, 0x0260}
  30. }, /* RGB Offset */
  31. {0x0000, 0x0000, 0x0000}
  32. };
  33. /* Default values in Office Fluorescent Light for RGB to YUV Conversion*/
  34. static struct omap3isp_prev_csc flr_prev_csc = {
  35. { /* CSC Coef Matrix */
  36. {66, 129, 25},
  37. {-38, -75, 112},
  38. {112, -94 , -18}
  39. }, /* CSC Offset */
  40. {0x0, 0x0, 0x0}
  41. };
  42. /* Default values in Office Fluorescent Light for CFA Gradient*/
  43. #define FLR_CFA_GRADTHRS_HORZ 0x28
  44. #define FLR_CFA_GRADTHRS_VERT 0x28
  45. /* Default values in Office Fluorescent Light for Chroma Suppression*/
  46. #define FLR_CSUP_GAIN 0x0D
  47. #define FLR_CSUP_THRES 0xEB
  48. /* Default values in Office Fluorescent Light for Noise Filter*/
  49. #define FLR_NF_STRGTH 0x03
  50. /* Default values for White Balance */
  51. #define FLR_WBAL_DGAIN 0x100
  52. #define FLR_WBAL_COEF 0x20
  53. /* Default values in Office Fluorescent Light for Black Adjustment*/
  54. #define FLR_BLKADJ_BLUE 0x0
  55. #define FLR_BLKADJ_GREEN 0x0
  56. #define FLR_BLKADJ_RED 0x0
  57. #define DEF_DETECT_CORRECT_VAL 0xe
  58. /*
  59. * Margins and image size limits.
  60. *
  61. * The preview engine crops several rows and columns internally depending on
  62. * which filters are enabled. To avoid format changes when the filters are
  63. * enabled or disabled (which would prevent them from being turned on or off
  64. * during streaming), the driver assumes all filters that can be configured
  65. * during streaming are enabled when computing sink crop and source format
  66. * limits.
  67. *
  68. * If a filter is disabled, additional cropping is automatically added at the
  69. * preview engine input by the driver to avoid overflow at line and frame end.
  70. * This is completely transparent for applications.
  71. *
  72. * Median filter 4 pixels
  73. * Noise filter,
  74. * Faulty pixels correction 4 pixels, 4 lines
  75. * Color suppression 2 pixels
  76. * or luma enhancement
  77. * -------------------------------------------------------------
  78. * Maximum total 10 pixels, 4 lines
  79. *
  80. * The color suppression and luma enhancement filters are applied after bayer to
  81. * YUV conversion. They thus can crop one pixel on the left and one pixel on the
  82. * right side of the image without changing the color pattern. When both those
  83. * filters are disabled, the driver must crop the two pixels on the same side of
  84. * the image to avoid changing the bayer pattern. The left margin is thus set to
  85. * 6 pixels and the right margin to 4 pixels.
  86. */
  87. #define PREV_MARGIN_LEFT 6
  88. #define PREV_MARGIN_RIGHT 4
  89. #define PREV_MARGIN_TOP 2
  90. #define PREV_MARGIN_BOTTOM 2
  91. #define PREV_MIN_IN_WIDTH 64
  92. #define PREV_MIN_IN_HEIGHT 8
  93. #define PREV_MAX_IN_HEIGHT 16384
  94. #define PREV_MIN_OUT_WIDTH 0
  95. #define PREV_MIN_OUT_HEIGHT 0
  96. #define PREV_MAX_OUT_WIDTH_REV_1 1280
  97. #define PREV_MAX_OUT_WIDTH_REV_2 3300
  98. #define PREV_MAX_OUT_WIDTH_REV_15 4096
  99. /*
  100. * Coefficient Tables for the submodules in Preview.
  101. * Array is initialised with the values from.the tables text file.
  102. */
  103. /*
  104. * CFA Filter Coefficient Table
  105. *
  106. */
  107. static u32 cfa_coef_table[4][OMAP3ISP_PREV_CFA_BLK_SIZE] = {
  108. #include "cfa_coef_table.h"
  109. };
  110. /*
  111. * Default Gamma Correction Table - All components
  112. */
  113. static u32 gamma_table[] = {
  114. #include "gamma_table.h"
  115. };
  116. /*
  117. * Noise Filter Threshold table
  118. */
  119. static u32 noise_filter_table[] = {
  120. #include "noise_filter_table.h"
  121. };
  122. /*
  123. * Luminance Enhancement Table
  124. */
  125. static u32 luma_enhance_table[] = {
  126. #include "luma_enhance_table.h"
  127. };
  128. /*
  129. * preview_config_luma_enhancement - Configure the Luminance Enhancement table
  130. */
  131. static void
  132. preview_config_luma_enhancement(struct isp_prev_device *prev,
  133. const struct prev_params *params)
  134. {
  135. struct isp_device *isp = to_isp_device(prev);
  136. const struct omap3isp_prev_luma *yt = &params->luma;
  137. unsigned int i;
  138. isp_reg_writel(isp, ISPPRV_YENH_TABLE_ADDR,
  139. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
  140. for (i = 0; i < OMAP3ISP_PREV_YENH_TBL_SIZE; i++) {
  141. isp_reg_writel(isp, yt->table[i],
  142. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
  143. }
  144. }
  145. /*
  146. * preview_enable_luma_enhancement - Enable/disable Luminance Enhancement
  147. */
  148. static void
  149. preview_enable_luma_enhancement(struct isp_prev_device *prev, bool enable)
  150. {
  151. struct isp_device *isp = to_isp_device(prev);
  152. if (enable)
  153. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  154. ISPPRV_PCR_YNENHEN);
  155. else
  156. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  157. ISPPRV_PCR_YNENHEN);
  158. }
  159. /*
  160. * preview_enable_invalaw - Enable/disable Inverse A-Law decompression
  161. */
  162. static void preview_enable_invalaw(struct isp_prev_device *prev, bool enable)
  163. {
  164. struct isp_device *isp = to_isp_device(prev);
  165. if (enable)
  166. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  167. ISPPRV_PCR_INVALAW);
  168. else
  169. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  170. ISPPRV_PCR_INVALAW);
  171. }
  172. /*
  173. * preview_config_hmed - Configure the Horizontal Median Filter
  174. */
  175. static void preview_config_hmed(struct isp_prev_device *prev,
  176. const struct prev_params *params)
  177. {
  178. struct isp_device *isp = to_isp_device(prev);
  179. const struct omap3isp_prev_hmed *hmed = &params->hmed;
  180. isp_reg_writel(isp, (hmed->odddist == 1 ? 0 : ISPPRV_HMED_ODDDIST) |
  181. (hmed->evendist == 1 ? 0 : ISPPRV_HMED_EVENDIST) |
  182. (hmed->thres << ISPPRV_HMED_THRESHOLD_SHIFT),
  183. OMAP3_ISP_IOMEM_PREV, ISPPRV_HMED);
  184. }
  185. /*
  186. * preview_enable_hmed - Enable/disable the Horizontal Median Filter
  187. */
  188. static void preview_enable_hmed(struct isp_prev_device *prev, bool enable)
  189. {
  190. struct isp_device *isp = to_isp_device(prev);
  191. if (enable)
  192. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  193. ISPPRV_PCR_HMEDEN);
  194. else
  195. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  196. ISPPRV_PCR_HMEDEN);
  197. }
  198. /*
  199. * preview_config_cfa - Configure CFA Interpolation for Bayer formats
  200. *
  201. * The CFA table is organised in four blocks, one per Bayer component. The
  202. * hardware expects blocks to follow the Bayer order of the input data, while
  203. * the driver stores the table in GRBG order in memory. The blocks need to be
  204. * reordered to support non-GRBG Bayer patterns.
  205. */
  206. static void preview_config_cfa(struct isp_prev_device *prev,
  207. const struct prev_params *params)
  208. {
  209. static const unsigned int cfa_coef_order[4][4] = {
  210. { 0, 1, 2, 3 }, /* GRBG */
  211. { 1, 0, 3, 2 }, /* RGGB */
  212. { 2, 3, 0, 1 }, /* BGGR */
  213. { 3, 2, 1, 0 }, /* GBRG */
  214. };
  215. const unsigned int *order = cfa_coef_order[prev->params.cfa_order];
  216. const struct omap3isp_prev_cfa *cfa = &params->cfa;
  217. struct isp_device *isp = to_isp_device(prev);
  218. unsigned int i;
  219. unsigned int j;
  220. isp_reg_writel(isp,
  221. (cfa->gradthrs_vert << ISPPRV_CFA_GRADTH_VER_SHIFT) |
  222. (cfa->gradthrs_horz << ISPPRV_CFA_GRADTH_HOR_SHIFT),
  223. OMAP3_ISP_IOMEM_PREV, ISPPRV_CFA);
  224. isp_reg_writel(isp, ISPPRV_CFA_TABLE_ADDR,
  225. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
  226. for (i = 0; i < 4; ++i) {
  227. const __u32 *block = cfa->table[order[i]];
  228. for (j = 0; j < OMAP3ISP_PREV_CFA_BLK_SIZE; ++j)
  229. isp_reg_writel(isp, block[j], OMAP3_ISP_IOMEM_PREV,
  230. ISPPRV_SET_TBL_DATA);
  231. }
  232. }
  233. /*
  234. * preview_config_chroma_suppression - Configure Chroma Suppression
  235. */
  236. static void
  237. preview_config_chroma_suppression(struct isp_prev_device *prev,
  238. const struct prev_params *params)
  239. {
  240. struct isp_device *isp = to_isp_device(prev);
  241. const struct omap3isp_prev_csup *cs = &params->csup;
  242. isp_reg_writel(isp,
  243. cs->gain | (cs->thres << ISPPRV_CSUP_THRES_SHIFT) |
  244. (cs->hypf_en << ISPPRV_CSUP_HPYF_SHIFT),
  245. OMAP3_ISP_IOMEM_PREV, ISPPRV_CSUP);
  246. }
  247. /*
  248. * preview_enable_chroma_suppression - Enable/disable Chrominance Suppression
  249. */
  250. static void
  251. preview_enable_chroma_suppression(struct isp_prev_device *prev, bool enable)
  252. {
  253. struct isp_device *isp = to_isp_device(prev);
  254. if (enable)
  255. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  256. ISPPRV_PCR_SUPEN);
  257. else
  258. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  259. ISPPRV_PCR_SUPEN);
  260. }
  261. /*
  262. * preview_config_whitebalance - Configure White Balance parameters
  263. *
  264. * Coefficient matrix always with default values.
  265. */
  266. static void
  267. preview_config_whitebalance(struct isp_prev_device *prev,
  268. const struct prev_params *params)
  269. {
  270. struct isp_device *isp = to_isp_device(prev);
  271. const struct omap3isp_prev_wbal *wbal = &params->wbal;
  272. u32 val;
  273. isp_reg_writel(isp, wbal->dgain, OMAP3_ISP_IOMEM_PREV, ISPPRV_WB_DGAIN);
  274. val = wbal->coef0 << ISPPRV_WBGAIN_COEF0_SHIFT;
  275. val |= wbal->coef1 << ISPPRV_WBGAIN_COEF1_SHIFT;
  276. val |= wbal->coef2 << ISPPRV_WBGAIN_COEF2_SHIFT;
  277. val |= wbal->coef3 << ISPPRV_WBGAIN_COEF3_SHIFT;
  278. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_WBGAIN);
  279. isp_reg_writel(isp,
  280. ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_0_SHIFT |
  281. ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_1_SHIFT |
  282. ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_2_SHIFT |
  283. ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_3_SHIFT |
  284. ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_0_SHIFT |
  285. ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_1_SHIFT |
  286. ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_2_SHIFT |
  287. ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_3_SHIFT |
  288. ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_0_SHIFT |
  289. ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_1_SHIFT |
  290. ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_2_SHIFT |
  291. ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_3_SHIFT |
  292. ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_0_SHIFT |
  293. ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_1_SHIFT |
  294. ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_2_SHIFT |
  295. ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_3_SHIFT,
  296. OMAP3_ISP_IOMEM_PREV, ISPPRV_WBSEL);
  297. }
  298. /*
  299. * preview_config_blkadj - Configure Black Adjustment
  300. */
  301. static void
  302. preview_config_blkadj(struct isp_prev_device *prev,
  303. const struct prev_params *params)
  304. {
  305. struct isp_device *isp = to_isp_device(prev);
  306. const struct omap3isp_prev_blkadj *blkadj = &params->blkadj;
  307. isp_reg_writel(isp, (blkadj->blue << ISPPRV_BLKADJOFF_B_SHIFT) |
  308. (blkadj->green << ISPPRV_BLKADJOFF_G_SHIFT) |
  309. (blkadj->red << ISPPRV_BLKADJOFF_R_SHIFT),
  310. OMAP3_ISP_IOMEM_PREV, ISPPRV_BLKADJOFF);
  311. }
  312. /*
  313. * preview_config_rgb_blending - Configure RGB-RGB Blending
  314. */
  315. static void
  316. preview_config_rgb_blending(struct isp_prev_device *prev,
  317. const struct prev_params *params)
  318. {
  319. struct isp_device *isp = to_isp_device(prev);
  320. const struct omap3isp_prev_rgbtorgb *rgbrgb = &params->rgb2rgb;
  321. u32 val;
  322. val = (rgbrgb->matrix[0][0] & 0xfff) << ISPPRV_RGB_MAT1_MTX_RR_SHIFT;
  323. val |= (rgbrgb->matrix[0][1] & 0xfff) << ISPPRV_RGB_MAT1_MTX_GR_SHIFT;
  324. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT1);
  325. val = (rgbrgb->matrix[0][2] & 0xfff) << ISPPRV_RGB_MAT2_MTX_BR_SHIFT;
  326. val |= (rgbrgb->matrix[1][0] & 0xfff) << ISPPRV_RGB_MAT2_MTX_RG_SHIFT;
  327. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT2);
  328. val = (rgbrgb->matrix[1][1] & 0xfff) << ISPPRV_RGB_MAT3_MTX_GG_SHIFT;
  329. val |= (rgbrgb->matrix[1][2] & 0xfff) << ISPPRV_RGB_MAT3_MTX_BG_SHIFT;
  330. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT3);
  331. val = (rgbrgb->matrix[2][0] & 0xfff) << ISPPRV_RGB_MAT4_MTX_RB_SHIFT;
  332. val |= (rgbrgb->matrix[2][1] & 0xfff) << ISPPRV_RGB_MAT4_MTX_GB_SHIFT;
  333. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT4);
  334. val = (rgbrgb->matrix[2][2] & 0xfff) << ISPPRV_RGB_MAT5_MTX_BB_SHIFT;
  335. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT5);
  336. val = (rgbrgb->offset[0] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFR_SHIFT;
  337. val |= (rgbrgb->offset[1] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFG_SHIFT;
  338. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF1);
  339. val = (rgbrgb->offset[2] & 0x3ff) << ISPPRV_RGB_OFF2_MTX_OFFB_SHIFT;
  340. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF2);
  341. }
  342. /*
  343. * preview_config_csc - Configure Color Space Conversion (RGB to YCbYCr)
  344. */
  345. static void
  346. preview_config_csc(struct isp_prev_device *prev,
  347. const struct prev_params *params)
  348. {
  349. struct isp_device *isp = to_isp_device(prev);
  350. const struct omap3isp_prev_csc *csc = &params->csc;
  351. u32 val;
  352. val = (csc->matrix[0][0] & 0x3ff) << ISPPRV_CSC0_RY_SHIFT;
  353. val |= (csc->matrix[0][1] & 0x3ff) << ISPPRV_CSC0_GY_SHIFT;
  354. val |= (csc->matrix[0][2] & 0x3ff) << ISPPRV_CSC0_BY_SHIFT;
  355. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC0);
  356. val = (csc->matrix[1][0] & 0x3ff) << ISPPRV_CSC1_RCB_SHIFT;
  357. val |= (csc->matrix[1][1] & 0x3ff) << ISPPRV_CSC1_GCB_SHIFT;
  358. val |= (csc->matrix[1][2] & 0x3ff) << ISPPRV_CSC1_BCB_SHIFT;
  359. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC1);
  360. val = (csc->matrix[2][0] & 0x3ff) << ISPPRV_CSC2_RCR_SHIFT;
  361. val |= (csc->matrix[2][1] & 0x3ff) << ISPPRV_CSC2_GCR_SHIFT;
  362. val |= (csc->matrix[2][2] & 0x3ff) << ISPPRV_CSC2_BCR_SHIFT;
  363. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC2);
  364. val = (csc->offset[0] & 0xff) << ISPPRV_CSC_OFFSET_Y_SHIFT;
  365. val |= (csc->offset[1] & 0xff) << ISPPRV_CSC_OFFSET_CB_SHIFT;
  366. val |= (csc->offset[2] & 0xff) << ISPPRV_CSC_OFFSET_CR_SHIFT;
  367. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC_OFFSET);
  368. }
  369. /*
  370. * preview_config_yc_range - Configure the max and min Y and C values
  371. */
  372. static void
  373. preview_config_yc_range(struct isp_prev_device *prev,
  374. const struct prev_params *params)
  375. {
  376. struct isp_device *isp = to_isp_device(prev);
  377. const struct omap3isp_prev_yclimit *yc = &params->yclimit;
  378. isp_reg_writel(isp,
  379. yc->maxC << ISPPRV_SETUP_YC_MAXC_SHIFT |
  380. yc->maxY << ISPPRV_SETUP_YC_MAXY_SHIFT |
  381. yc->minC << ISPPRV_SETUP_YC_MINC_SHIFT |
  382. yc->minY << ISPPRV_SETUP_YC_MINY_SHIFT,
  383. OMAP3_ISP_IOMEM_PREV, ISPPRV_SETUP_YC);
  384. }
  385. /*
  386. * preview_config_dcor - Configure Couplet Defect Correction
  387. */
  388. static void
  389. preview_config_dcor(struct isp_prev_device *prev,
  390. const struct prev_params *params)
  391. {
  392. struct isp_device *isp = to_isp_device(prev);
  393. const struct omap3isp_prev_dcor *dcor = &params->dcor;
  394. isp_reg_writel(isp, dcor->detect_correct[0],
  395. OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR0);
  396. isp_reg_writel(isp, dcor->detect_correct[1],
  397. OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR1);
  398. isp_reg_writel(isp, dcor->detect_correct[2],
  399. OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR2);
  400. isp_reg_writel(isp, dcor->detect_correct[3],
  401. OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR3);
  402. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  403. ISPPRV_PCR_DCCOUP,
  404. dcor->couplet_mode_en ? ISPPRV_PCR_DCCOUP : 0);
  405. }
  406. /*
  407. * preview_enable_dcor - Enable/disable Couplet Defect Correction
  408. */
  409. static void preview_enable_dcor(struct isp_prev_device *prev, bool enable)
  410. {
  411. struct isp_device *isp = to_isp_device(prev);
  412. if (enable)
  413. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  414. ISPPRV_PCR_DCOREN);
  415. else
  416. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  417. ISPPRV_PCR_DCOREN);
  418. }
  419. /*
  420. * preview_enable_drkframe_capture - Enable/disable Dark Frame Capture
  421. */
  422. static void
  423. preview_enable_drkframe_capture(struct isp_prev_device *prev, bool enable)
  424. {
  425. struct isp_device *isp = to_isp_device(prev);
  426. if (enable)
  427. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  428. ISPPRV_PCR_DRKFCAP);
  429. else
  430. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  431. ISPPRV_PCR_DRKFCAP);
  432. }
  433. /*
  434. * preview_enable_drkframe - Enable/disable Dark Frame Subtraction
  435. */
  436. static void preview_enable_drkframe(struct isp_prev_device *prev, bool enable)
  437. {
  438. struct isp_device *isp = to_isp_device(prev);
  439. if (enable)
  440. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  441. ISPPRV_PCR_DRKFEN);
  442. else
  443. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  444. ISPPRV_PCR_DRKFEN);
  445. }
  446. /*
  447. * preview_config_noisefilter - Configure the Noise Filter
  448. */
  449. static void
  450. preview_config_noisefilter(struct isp_prev_device *prev,
  451. const struct prev_params *params)
  452. {
  453. struct isp_device *isp = to_isp_device(prev);
  454. const struct omap3isp_prev_nf *nf = &params->nf;
  455. unsigned int i;
  456. isp_reg_writel(isp, nf->spread, OMAP3_ISP_IOMEM_PREV, ISPPRV_NF);
  457. isp_reg_writel(isp, ISPPRV_NF_TABLE_ADDR,
  458. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
  459. for (i = 0; i < OMAP3ISP_PREV_NF_TBL_SIZE; i++) {
  460. isp_reg_writel(isp, nf->table[i],
  461. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
  462. }
  463. }
  464. /*
  465. * preview_enable_noisefilter - Enable/disable the Noise Filter
  466. */
  467. static void
  468. preview_enable_noisefilter(struct isp_prev_device *prev, bool enable)
  469. {
  470. struct isp_device *isp = to_isp_device(prev);
  471. if (enable)
  472. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  473. ISPPRV_PCR_NFEN);
  474. else
  475. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  476. ISPPRV_PCR_NFEN);
  477. }
  478. /*
  479. * preview_config_gammacorrn - Configure the Gamma Correction tables
  480. */
  481. static void
  482. preview_config_gammacorrn(struct isp_prev_device *prev,
  483. const struct prev_params *params)
  484. {
  485. struct isp_device *isp = to_isp_device(prev);
  486. const struct omap3isp_prev_gtables *gt = &params->gamma;
  487. unsigned int i;
  488. isp_reg_writel(isp, ISPPRV_REDGAMMA_TABLE_ADDR,
  489. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
  490. for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
  491. isp_reg_writel(isp, gt->red[i], OMAP3_ISP_IOMEM_PREV,
  492. ISPPRV_SET_TBL_DATA);
  493. isp_reg_writel(isp, ISPPRV_GREENGAMMA_TABLE_ADDR,
  494. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
  495. for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
  496. isp_reg_writel(isp, gt->green[i], OMAP3_ISP_IOMEM_PREV,
  497. ISPPRV_SET_TBL_DATA);
  498. isp_reg_writel(isp, ISPPRV_BLUEGAMMA_TABLE_ADDR,
  499. OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
  500. for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
  501. isp_reg_writel(isp, gt->blue[i], OMAP3_ISP_IOMEM_PREV,
  502. ISPPRV_SET_TBL_DATA);
  503. }
  504. /*
  505. * preview_enable_gammacorrn - Enable/disable Gamma Correction
  506. *
  507. * When gamma correction is disabled, the module is bypassed and its output is
  508. * the 8 MSB of the 10-bit input .
  509. */
  510. static void
  511. preview_enable_gammacorrn(struct isp_prev_device *prev, bool enable)
  512. {
  513. struct isp_device *isp = to_isp_device(prev);
  514. if (enable)
  515. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  516. ISPPRV_PCR_GAMMA_BYPASS);
  517. else
  518. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  519. ISPPRV_PCR_GAMMA_BYPASS);
  520. }
  521. /*
  522. * preview_config_contrast - Configure the Contrast
  523. *
  524. * Value should be programmed before enabling the module.
  525. */
  526. static void
  527. preview_config_contrast(struct isp_prev_device *prev,
  528. const struct prev_params *params)
  529. {
  530. struct isp_device *isp = to_isp_device(prev);
  531. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
  532. 0xff << ISPPRV_CNT_BRT_CNT_SHIFT,
  533. params->contrast << ISPPRV_CNT_BRT_CNT_SHIFT);
  534. }
  535. /*
  536. * preview_config_brightness - Configure the Brightness
  537. */
  538. static void
  539. preview_config_brightness(struct isp_prev_device *prev,
  540. const struct prev_params *params)
  541. {
  542. struct isp_device *isp = to_isp_device(prev);
  543. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
  544. 0xff << ISPPRV_CNT_BRT_BRT_SHIFT,
  545. params->brightness << ISPPRV_CNT_BRT_BRT_SHIFT);
  546. }
  547. /*
  548. * preview_update_contrast - Updates the contrast.
  549. * @contrast: Pointer to hold the current programmed contrast value.
  550. *
  551. * Value should be programmed before enabling the module.
  552. */
  553. static void
  554. preview_update_contrast(struct isp_prev_device *prev, u8 contrast)
  555. {
  556. struct prev_params *params;
  557. unsigned long flags;
  558. spin_lock_irqsave(&prev->params.lock, flags);
  559. params = (prev->params.active & OMAP3ISP_PREV_CONTRAST)
  560. ? &prev->params.params[0] : &prev->params.params[1];
  561. if (params->contrast != (contrast * ISPPRV_CONTRAST_UNITS)) {
  562. params->contrast = contrast * ISPPRV_CONTRAST_UNITS;
  563. params->update |= OMAP3ISP_PREV_CONTRAST;
  564. }
  565. spin_unlock_irqrestore(&prev->params.lock, flags);
  566. }
  567. /*
  568. * preview_update_brightness - Updates the brightness in preview module.
  569. * @brightness: Pointer to hold the current programmed brightness value.
  570. *
  571. */
  572. static void
  573. preview_update_brightness(struct isp_prev_device *prev, u8 brightness)
  574. {
  575. struct prev_params *params;
  576. unsigned long flags;
  577. spin_lock_irqsave(&prev->params.lock, flags);
  578. params = (prev->params.active & OMAP3ISP_PREV_BRIGHTNESS)
  579. ? &prev->params.params[0] : &prev->params.params[1];
  580. if (params->brightness != (brightness * ISPPRV_BRIGHT_UNITS)) {
  581. params->brightness = brightness * ISPPRV_BRIGHT_UNITS;
  582. params->update |= OMAP3ISP_PREV_BRIGHTNESS;
  583. }
  584. spin_unlock_irqrestore(&prev->params.lock, flags);
  585. }
  586. static u32
  587. preview_params_lock(struct isp_prev_device *prev, u32 update, bool shadow)
  588. {
  589. u32 active = prev->params.active;
  590. if (shadow) {
  591. /* Mark all shadow parameters we are going to touch as busy. */
  592. prev->params.params[0].busy |= ~active & update;
  593. prev->params.params[1].busy |= active & update;
  594. } else {
  595. /* Mark all active parameters we are going to touch as busy. */
  596. update = (prev->params.params[0].update & active)
  597. | (prev->params.params[1].update & ~active);
  598. prev->params.params[0].busy |= active & update;
  599. prev->params.params[1].busy |= ~active & update;
  600. }
  601. return update;
  602. }
  603. static void
  604. preview_params_unlock(struct isp_prev_device *prev, u32 update, bool shadow)
  605. {
  606. u32 active = prev->params.active;
  607. if (shadow) {
  608. /* Set the update flag for shadow parameters that have been
  609. * updated and clear the busy flag for all shadow parameters.
  610. */
  611. prev->params.params[0].update |= (~active & update);
  612. prev->params.params[1].update |= (active & update);
  613. prev->params.params[0].busy &= active;
  614. prev->params.params[1].busy &= ~active;
  615. } else {
  616. /* Clear the update flag for active parameters that have been
  617. * applied and the busy flag for all active parameters.
  618. */
  619. prev->params.params[0].update &= ~(active & update);
  620. prev->params.params[1].update &= ~(~active & update);
  621. prev->params.params[0].busy &= ~active;
  622. prev->params.params[1].busy &= active;
  623. }
  624. }
  625. static void preview_params_switch(struct isp_prev_device *prev)
  626. {
  627. u32 to_switch;
  628. /* Switch active parameters with updated shadow parameters when the
  629. * shadow parameter has been updated and neither the active not the
  630. * shadow parameter is busy.
  631. */
  632. to_switch = (prev->params.params[0].update & ~prev->params.active)
  633. | (prev->params.params[1].update & prev->params.active);
  634. to_switch &= ~(prev->params.params[0].busy |
  635. prev->params.params[1].busy);
  636. if (to_switch == 0)
  637. return;
  638. prev->params.active ^= to_switch;
  639. /* Remove the update flag for the shadow copy of parameters we have
  640. * switched.
  641. */
  642. prev->params.params[0].update &= ~(~prev->params.active & to_switch);
  643. prev->params.params[1].update &= ~(prev->params.active & to_switch);
  644. }
  645. /* preview parameters update structure */
  646. struct preview_update {
  647. void (*config)(struct isp_prev_device *, const struct prev_params *);
  648. void (*enable)(struct isp_prev_device *, bool);
  649. unsigned int param_offset;
  650. unsigned int param_size;
  651. unsigned int config_offset;
  652. bool skip;
  653. };
  654. /* Keep the array indexed by the OMAP3ISP_PREV_* bit number. */
  655. static const struct preview_update update_attrs[] = {
  656. /* OMAP3ISP_PREV_LUMAENH */ {
  657. preview_config_luma_enhancement,
  658. preview_enable_luma_enhancement,
  659. offsetof(struct prev_params, luma),
  660. FIELD_SIZEOF(struct prev_params, luma),
  661. offsetof(struct omap3isp_prev_update_config, luma),
  662. }, /* OMAP3ISP_PREV_INVALAW */ {
  663. NULL,
  664. preview_enable_invalaw,
  665. }, /* OMAP3ISP_PREV_HRZ_MED */ {
  666. preview_config_hmed,
  667. preview_enable_hmed,
  668. offsetof(struct prev_params, hmed),
  669. FIELD_SIZEOF(struct prev_params, hmed),
  670. offsetof(struct omap3isp_prev_update_config, hmed),
  671. }, /* OMAP3ISP_PREV_CFA */ {
  672. preview_config_cfa,
  673. NULL,
  674. offsetof(struct prev_params, cfa),
  675. FIELD_SIZEOF(struct prev_params, cfa),
  676. offsetof(struct omap3isp_prev_update_config, cfa),
  677. }, /* OMAP3ISP_PREV_CHROMA_SUPP */ {
  678. preview_config_chroma_suppression,
  679. preview_enable_chroma_suppression,
  680. offsetof(struct prev_params, csup),
  681. FIELD_SIZEOF(struct prev_params, csup),
  682. offsetof(struct omap3isp_prev_update_config, csup),
  683. }, /* OMAP3ISP_PREV_WB */ {
  684. preview_config_whitebalance,
  685. NULL,
  686. offsetof(struct prev_params, wbal),
  687. FIELD_SIZEOF(struct prev_params, wbal),
  688. offsetof(struct omap3isp_prev_update_config, wbal),
  689. }, /* OMAP3ISP_PREV_BLKADJ */ {
  690. preview_config_blkadj,
  691. NULL,
  692. offsetof(struct prev_params, blkadj),
  693. FIELD_SIZEOF(struct prev_params, blkadj),
  694. offsetof(struct omap3isp_prev_update_config, blkadj),
  695. }, /* OMAP3ISP_PREV_RGB2RGB */ {
  696. preview_config_rgb_blending,
  697. NULL,
  698. offsetof(struct prev_params, rgb2rgb),
  699. FIELD_SIZEOF(struct prev_params, rgb2rgb),
  700. offsetof(struct omap3isp_prev_update_config, rgb2rgb),
  701. }, /* OMAP3ISP_PREV_COLOR_CONV */ {
  702. preview_config_csc,
  703. NULL,
  704. offsetof(struct prev_params, csc),
  705. FIELD_SIZEOF(struct prev_params, csc),
  706. offsetof(struct omap3isp_prev_update_config, csc),
  707. }, /* OMAP3ISP_PREV_YC_LIMIT */ {
  708. preview_config_yc_range,
  709. NULL,
  710. offsetof(struct prev_params, yclimit),
  711. FIELD_SIZEOF(struct prev_params, yclimit),
  712. offsetof(struct omap3isp_prev_update_config, yclimit),
  713. }, /* OMAP3ISP_PREV_DEFECT_COR */ {
  714. preview_config_dcor,
  715. preview_enable_dcor,
  716. offsetof(struct prev_params, dcor),
  717. FIELD_SIZEOF(struct prev_params, dcor),
  718. offsetof(struct omap3isp_prev_update_config, dcor),
  719. }, /* Previously OMAP3ISP_PREV_GAMMABYPASS, not used anymore */ {
  720. NULL,
  721. NULL,
  722. }, /* OMAP3ISP_PREV_DRK_FRM_CAPTURE */ {
  723. NULL,
  724. preview_enable_drkframe_capture,
  725. }, /* OMAP3ISP_PREV_DRK_FRM_SUBTRACT */ {
  726. NULL,
  727. preview_enable_drkframe,
  728. }, /* OMAP3ISP_PREV_LENS_SHADING */ {
  729. NULL,
  730. preview_enable_drkframe,
  731. }, /* OMAP3ISP_PREV_NF */ {
  732. preview_config_noisefilter,
  733. preview_enable_noisefilter,
  734. offsetof(struct prev_params, nf),
  735. FIELD_SIZEOF(struct prev_params, nf),
  736. offsetof(struct omap3isp_prev_update_config, nf),
  737. }, /* OMAP3ISP_PREV_GAMMA */ {
  738. preview_config_gammacorrn,
  739. preview_enable_gammacorrn,
  740. offsetof(struct prev_params, gamma),
  741. FIELD_SIZEOF(struct prev_params, gamma),
  742. offsetof(struct omap3isp_prev_update_config, gamma),
  743. }, /* OMAP3ISP_PREV_CONTRAST */ {
  744. preview_config_contrast,
  745. NULL,
  746. 0, 0, 0, true,
  747. }, /* OMAP3ISP_PREV_BRIGHTNESS */ {
  748. preview_config_brightness,
  749. NULL,
  750. 0, 0, 0, true,
  751. },
  752. };
  753. /*
  754. * preview_config - Copy and update local structure with userspace preview
  755. * configuration.
  756. * @prev: ISP preview engine
  757. * @cfg: Configuration
  758. *
  759. * Return zero if success or -EFAULT if the configuration can't be copied from
  760. * userspace.
  761. */
  762. static int preview_config(struct isp_prev_device *prev,
  763. struct omap3isp_prev_update_config *cfg)
  764. {
  765. unsigned long flags;
  766. unsigned int i;
  767. int rval = 0;
  768. u32 update;
  769. u32 active;
  770. if (cfg->update == 0)
  771. return 0;
  772. /* Mark the shadow parameters we're going to update as busy. */
  773. spin_lock_irqsave(&prev->params.lock, flags);
  774. preview_params_lock(prev, cfg->update, true);
  775. active = prev->params.active;
  776. spin_unlock_irqrestore(&prev->params.lock, flags);
  777. update = 0;
  778. for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
  779. const struct preview_update *attr = &update_attrs[i];
  780. struct prev_params *params;
  781. unsigned int bit = 1 << i;
  782. if (attr->skip || !(cfg->update & bit))
  783. continue;
  784. params = &prev->params.params[!!(active & bit)];
  785. if (cfg->flag & bit) {
  786. void __user *from = *(void * __user *)
  787. ((void *)cfg + attr->config_offset);
  788. void *to = (void *)params + attr->param_offset;
  789. size_t size = attr->param_size;
  790. if (to && from && size) {
  791. if (copy_from_user(to, from, size)) {
  792. rval = -EFAULT;
  793. break;
  794. }
  795. }
  796. params->features |= bit;
  797. } else {
  798. params->features &= ~bit;
  799. }
  800. update |= bit;
  801. }
  802. spin_lock_irqsave(&prev->params.lock, flags);
  803. preview_params_unlock(prev, update, true);
  804. preview_params_switch(prev);
  805. spin_unlock_irqrestore(&prev->params.lock, flags);
  806. return rval;
  807. }
  808. /*
  809. * preview_setup_hw - Setup preview registers and/or internal memory
  810. * @prev: pointer to preview private structure
  811. * @update: Bitmask of parameters to setup
  812. * @active: Bitmask of parameters active in set 0
  813. * Note: can be called from interrupt context
  814. * Return none
  815. */
  816. static void preview_setup_hw(struct isp_prev_device *prev, u32 update,
  817. u32 active)
  818. {
  819. unsigned int i;
  820. u32 features;
  821. if (update == 0)
  822. return;
  823. features = (prev->params.params[0].features & active)
  824. | (prev->params.params[1].features & ~active);
  825. for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
  826. const struct preview_update *attr = &update_attrs[i];
  827. struct prev_params *params;
  828. unsigned int bit = 1 << i;
  829. if (!(update & bit))
  830. continue;
  831. params = &prev->params.params[!(active & bit)];
  832. if (params->features & bit) {
  833. if (attr->config)
  834. attr->config(prev, params);
  835. if (attr->enable)
  836. attr->enable(prev, true);
  837. } else {
  838. if (attr->enable)
  839. attr->enable(prev, false);
  840. }
  841. }
  842. }
  843. /*
  844. * preview_config_ycpos - Configure byte layout of YUV image.
  845. * @prev: pointer to previewer private structure
  846. * @pixelcode: pixel code
  847. */
  848. static void
  849. preview_config_ycpos(struct isp_prev_device *prev,
  850. enum v4l2_mbus_pixelcode pixelcode)
  851. {
  852. struct isp_device *isp = to_isp_device(prev);
  853. enum preview_ycpos_mode mode;
  854. switch (pixelcode) {
  855. case V4L2_MBUS_FMT_YUYV8_1X16:
  856. mode = YCPOS_CrYCbY;
  857. break;
  858. case V4L2_MBUS_FMT_UYVY8_1X16:
  859. mode = YCPOS_YCrYCb;
  860. break;
  861. default:
  862. return;
  863. }
  864. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  865. ISPPRV_PCR_YCPOS_CrYCbY,
  866. mode << ISPPRV_PCR_YCPOS_SHIFT);
  867. }
  868. /*
  869. * preview_config_averager - Enable / disable / configure averager
  870. * @average: Average value to be configured.
  871. */
  872. static void preview_config_averager(struct isp_prev_device *prev, u8 average)
  873. {
  874. struct isp_device *isp = to_isp_device(prev);
  875. isp_reg_writel(isp, ISPPRV_AVE_EVENDIST_2 << ISPPRV_AVE_EVENDIST_SHIFT |
  876. ISPPRV_AVE_ODDDIST_2 << ISPPRV_AVE_ODDDIST_SHIFT |
  877. average, OMAP3_ISP_IOMEM_PREV, ISPPRV_AVE);
  878. }
  879. /*
  880. * preview_config_input_format - Configure the input format
  881. * @prev: The preview engine
  882. * @info: Sink pad format information
  883. *
  884. * Enable and configure CFA interpolation for Bayer formats and disable it for
  885. * greyscale formats.
  886. *
  887. * The CFA table is organised in four blocks, one per Bayer component. The
  888. * hardware expects blocks to follow the Bayer order of the input data, while
  889. * the driver stores the table in GRBG order in memory. The blocks need to be
  890. * reordered to support non-GRBG Bayer patterns.
  891. */
  892. static void preview_config_input_format(struct isp_prev_device *prev,
  893. const struct isp_format_info *info)
  894. {
  895. struct isp_device *isp = to_isp_device(prev);
  896. struct prev_params *params;
  897. if (info->width == 8)
  898. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  899. ISPPRV_PCR_WIDTH);
  900. else
  901. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  902. ISPPRV_PCR_WIDTH);
  903. switch (info->flavor) {
  904. case V4L2_MBUS_FMT_SGRBG8_1X8:
  905. prev->params.cfa_order = 0;
  906. break;
  907. case V4L2_MBUS_FMT_SRGGB8_1X8:
  908. prev->params.cfa_order = 1;
  909. break;
  910. case V4L2_MBUS_FMT_SBGGR8_1X8:
  911. prev->params.cfa_order = 2;
  912. break;
  913. case V4L2_MBUS_FMT_SGBRG8_1X8:
  914. prev->params.cfa_order = 3;
  915. break;
  916. default:
  917. /* Disable CFA for non-Bayer formats. */
  918. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  919. ISPPRV_PCR_CFAEN);
  920. return;
  921. }
  922. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR, ISPPRV_PCR_CFAEN);
  923. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  924. ISPPRV_PCR_CFAFMT_MASK, ISPPRV_PCR_CFAFMT_BAYER);
  925. params = (prev->params.active & OMAP3ISP_PREV_CFA)
  926. ? &prev->params.params[0] : &prev->params.params[1];
  927. preview_config_cfa(prev, params);
  928. }
  929. /*
  930. * preview_config_input_size - Configure the input frame size
  931. *
  932. * The preview engine crops several rows and columns internally depending on
  933. * which processing blocks are enabled. The driver assumes all those blocks are
  934. * enabled when reporting source pad formats to userspace. If this assumption is
  935. * not true, rows and columns must be manually cropped at the preview engine
  936. * input to avoid overflows at the end of lines and frames.
  937. *
  938. * See the explanation at the PREV_MARGIN_* definitions for more details.
  939. */
  940. static void preview_config_input_size(struct isp_prev_device *prev, u32 active)
  941. {
  942. const struct v4l2_mbus_framefmt *format = &prev->formats[PREV_PAD_SINK];
  943. struct isp_device *isp = to_isp_device(prev);
  944. unsigned int sph = prev->crop.left;
  945. unsigned int eph = prev->crop.left + prev->crop.width - 1;
  946. unsigned int slv = prev->crop.top;
  947. unsigned int elv = prev->crop.top + prev->crop.height - 1;
  948. u32 features;
  949. if (format->code != V4L2_MBUS_FMT_Y8_1X8 &&
  950. format->code != V4L2_MBUS_FMT_Y10_1X10) {
  951. sph -= 2;
  952. eph += 2;
  953. slv -= 2;
  954. elv += 2;
  955. }
  956. features = (prev->params.params[0].features & active)
  957. | (prev->params.params[1].features & ~active);
  958. if (features & (OMAP3ISP_PREV_DEFECT_COR | OMAP3ISP_PREV_NF)) {
  959. sph -= 2;
  960. eph += 2;
  961. slv -= 2;
  962. elv += 2;
  963. }
  964. if (features & OMAP3ISP_PREV_HRZ_MED) {
  965. sph -= 2;
  966. eph += 2;
  967. }
  968. if (features & (OMAP3ISP_PREV_CHROMA_SUPP | OMAP3ISP_PREV_LUMAENH))
  969. sph -= 2;
  970. isp_reg_writel(isp, (sph << ISPPRV_HORZ_INFO_SPH_SHIFT) | eph,
  971. OMAP3_ISP_IOMEM_PREV, ISPPRV_HORZ_INFO);
  972. isp_reg_writel(isp, (slv << ISPPRV_VERT_INFO_SLV_SHIFT) | elv,
  973. OMAP3_ISP_IOMEM_PREV, ISPPRV_VERT_INFO);
  974. }
  975. /*
  976. * preview_config_inlineoffset - Configures the Read address line offset.
  977. * @prev: Preview module
  978. * @offset: Line offset
  979. *
  980. * According to the TRM, the line offset must be aligned on a 32 bytes boundary.
  981. * However, a hardware bug requires the memory start address to be aligned on a
  982. * 64 bytes boundary, so the offset probably should be aligned on 64 bytes as
  983. * well.
  984. */
  985. static void
  986. preview_config_inlineoffset(struct isp_prev_device *prev, u32 offset)
  987. {
  988. struct isp_device *isp = to_isp_device(prev);
  989. isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
  990. ISPPRV_RADR_OFFSET);
  991. }
  992. /*
  993. * preview_set_inaddr - Sets memory address of input frame.
  994. * @addr: 32bit memory address aligned on 32byte boundary.
  995. *
  996. * Configures the memory address from which the input frame is to be read.
  997. */
  998. static void preview_set_inaddr(struct isp_prev_device *prev, u32 addr)
  999. {
  1000. struct isp_device *isp = to_isp_device(prev);
  1001. isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_RSDR_ADDR);
  1002. }
  1003. /*
  1004. * preview_config_outlineoffset - Configures the Write address line offset.
  1005. * @offset: Line Offset for the preview output.
  1006. *
  1007. * The offset must be a multiple of 32 bytes.
  1008. */
  1009. static void preview_config_outlineoffset(struct isp_prev_device *prev,
  1010. u32 offset)
  1011. {
  1012. struct isp_device *isp = to_isp_device(prev);
  1013. isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
  1014. ISPPRV_WADD_OFFSET);
  1015. }
  1016. /*
  1017. * preview_set_outaddr - Sets the memory address to store output frame
  1018. * @addr: 32bit memory address aligned on 32byte boundary.
  1019. *
  1020. * Configures the memory address to which the output frame is written.
  1021. */
  1022. static void preview_set_outaddr(struct isp_prev_device *prev, u32 addr)
  1023. {
  1024. struct isp_device *isp = to_isp_device(prev);
  1025. isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_WSDR_ADDR);
  1026. }
  1027. static void preview_adjust_bandwidth(struct isp_prev_device *prev)
  1028. {
  1029. struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
  1030. struct isp_device *isp = to_isp_device(prev);
  1031. const struct v4l2_mbus_framefmt *ifmt = &prev->formats[PREV_PAD_SINK];
  1032. unsigned long l3_ick = pipe->l3_ick;
  1033. struct v4l2_fract *timeperframe;
  1034. unsigned int cycles_per_frame;
  1035. unsigned int requests_per_frame;
  1036. unsigned int cycles_per_request;
  1037. unsigned int minimum;
  1038. unsigned int maximum;
  1039. unsigned int value;
  1040. if (prev->input != PREVIEW_INPUT_MEMORY) {
  1041. isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
  1042. ISPSBL_SDR_REQ_PRV_EXP_MASK);
  1043. return;
  1044. }
  1045. /* Compute the minimum number of cycles per request, based on the
  1046. * pipeline maximum data rate. This is an absolute lower bound if we
  1047. * don't want SBL overflows, so round the value up.
  1048. */
  1049. cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
  1050. pipe->max_rate);
  1051. minimum = DIV_ROUND_UP(cycles_per_request, 32);
  1052. /* Compute the maximum number of cycles per request, based on the
  1053. * requested frame rate. This is a soft upper bound to achieve a frame
  1054. * rate equal or higher than the requested value, so round the value
  1055. * down.
  1056. */
  1057. timeperframe = &pipe->max_timeperframe;
  1058. requests_per_frame = DIV_ROUND_UP(ifmt->width * 2, 256) * ifmt->height;
  1059. cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
  1060. timeperframe->denominator);
  1061. cycles_per_request = cycles_per_frame / requests_per_frame;
  1062. maximum = cycles_per_request / 32;
  1063. value = max(minimum, maximum);
  1064. dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
  1065. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
  1066. ISPSBL_SDR_REQ_PRV_EXP_MASK,
  1067. value << ISPSBL_SDR_REQ_PRV_EXP_SHIFT);
  1068. }
  1069. /*
  1070. * omap3isp_preview_busy - Gets busy state of preview module.
  1071. */
  1072. int omap3isp_preview_busy(struct isp_prev_device *prev)
  1073. {
  1074. struct isp_device *isp = to_isp_device(prev);
  1075. return isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR)
  1076. & ISPPRV_PCR_BUSY;
  1077. }
  1078. /*
  1079. * omap3isp_preview_restore_context - Restores the values of preview registers
  1080. */
  1081. void omap3isp_preview_restore_context(struct isp_device *isp)
  1082. {
  1083. struct isp_prev_device *prev = &isp->isp_prev;
  1084. const u32 update = OMAP3ISP_PREV_FEATURES_END - 1;
  1085. prev->params.params[0].update = prev->params.active & update;
  1086. prev->params.params[1].update = ~prev->params.active & update;
  1087. preview_setup_hw(prev, update, prev->params.active);
  1088. prev->params.params[0].update = 0;
  1089. prev->params.params[1].update = 0;
  1090. }
  1091. /*
  1092. * preview_print_status - Dump preview module registers to the kernel log
  1093. */
  1094. #define PREV_PRINT_REGISTER(isp, name)\
  1095. dev_dbg(isp->dev, "###PRV " #name "=0x%08x\n", \
  1096. isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_##name))
  1097. static void preview_print_status(struct isp_prev_device *prev)
  1098. {
  1099. struct isp_device *isp = to_isp_device(prev);
  1100. dev_dbg(isp->dev, "-------------Preview Register dump----------\n");
  1101. PREV_PRINT_REGISTER(isp, PCR);
  1102. PREV_PRINT_REGISTER(isp, HORZ_INFO);
  1103. PREV_PRINT_REGISTER(isp, VERT_INFO);
  1104. PREV_PRINT_REGISTER(isp, RSDR_ADDR);
  1105. PREV_PRINT_REGISTER(isp, RADR_OFFSET);
  1106. PREV_PRINT_REGISTER(isp, DSDR_ADDR);
  1107. PREV_PRINT_REGISTER(isp, DRKF_OFFSET);
  1108. PREV_PRINT_REGISTER(isp, WSDR_ADDR);
  1109. PREV_PRINT_REGISTER(isp, WADD_OFFSET);
  1110. PREV_PRINT_REGISTER(isp, AVE);
  1111. PREV_PRINT_REGISTER(isp, HMED);
  1112. PREV_PRINT_REGISTER(isp, NF);
  1113. PREV_PRINT_REGISTER(isp, WB_DGAIN);
  1114. PREV_PRINT_REGISTER(isp, WBGAIN);
  1115. PREV_PRINT_REGISTER(isp, WBSEL);
  1116. PREV_PRINT_REGISTER(isp, CFA);
  1117. PREV_PRINT_REGISTER(isp, BLKADJOFF);
  1118. PREV_PRINT_REGISTER(isp, RGB_MAT1);
  1119. PREV_PRINT_REGISTER(isp, RGB_MAT2);
  1120. PREV_PRINT_REGISTER(isp, RGB_MAT3);
  1121. PREV_PRINT_REGISTER(isp, RGB_MAT4);
  1122. PREV_PRINT_REGISTER(isp, RGB_MAT5);
  1123. PREV_PRINT_REGISTER(isp, RGB_OFF1);
  1124. PREV_PRINT_REGISTER(isp, RGB_OFF2);
  1125. PREV_PRINT_REGISTER(isp, CSC0);
  1126. PREV_PRINT_REGISTER(isp, CSC1);
  1127. PREV_PRINT_REGISTER(isp, CSC2);
  1128. PREV_PRINT_REGISTER(isp, CSC_OFFSET);
  1129. PREV_PRINT_REGISTER(isp, CNT_BRT);
  1130. PREV_PRINT_REGISTER(isp, CSUP);
  1131. PREV_PRINT_REGISTER(isp, SETUP_YC);
  1132. PREV_PRINT_REGISTER(isp, SET_TBL_ADDR);
  1133. PREV_PRINT_REGISTER(isp, CDC_THR0);
  1134. PREV_PRINT_REGISTER(isp, CDC_THR1);
  1135. PREV_PRINT_REGISTER(isp, CDC_THR2);
  1136. PREV_PRINT_REGISTER(isp, CDC_THR3);
  1137. dev_dbg(isp->dev, "--------------------------------------------\n");
  1138. }
  1139. /*
  1140. * preview_init_params - init image processing parameters.
  1141. * @prev: pointer to previewer private structure
  1142. */
  1143. static void preview_init_params(struct isp_prev_device *prev)
  1144. {
  1145. struct prev_params *params;
  1146. unsigned int i;
  1147. spin_lock_init(&prev->params.lock);
  1148. prev->params.active = ~0;
  1149. prev->params.params[0].busy = 0;
  1150. prev->params.params[0].update = OMAP3ISP_PREV_FEATURES_END - 1;
  1151. prev->params.params[1].busy = 0;
  1152. prev->params.params[1].update = 0;
  1153. params = &prev->params.params[0];
  1154. /* Init values */
  1155. params->contrast = ISPPRV_CONTRAST_DEF * ISPPRV_CONTRAST_UNITS;
  1156. params->brightness = ISPPRV_BRIGHT_DEF * ISPPRV_BRIGHT_UNITS;
  1157. params->cfa.format = OMAP3ISP_CFAFMT_BAYER;
  1158. memcpy(params->cfa.table, cfa_coef_table,
  1159. sizeof(params->cfa.table));
  1160. params->cfa.gradthrs_horz = FLR_CFA_GRADTHRS_HORZ;
  1161. params->cfa.gradthrs_vert = FLR_CFA_GRADTHRS_VERT;
  1162. params->csup.gain = FLR_CSUP_GAIN;
  1163. params->csup.thres = FLR_CSUP_THRES;
  1164. params->csup.hypf_en = 0;
  1165. memcpy(params->luma.table, luma_enhance_table,
  1166. sizeof(params->luma.table));
  1167. params->nf.spread = FLR_NF_STRGTH;
  1168. memcpy(params->nf.table, noise_filter_table, sizeof(params->nf.table));
  1169. params->dcor.couplet_mode_en = 1;
  1170. for (i = 0; i < OMAP3ISP_PREV_DETECT_CORRECT_CHANNELS; i++)
  1171. params->dcor.detect_correct[i] = DEF_DETECT_CORRECT_VAL;
  1172. memcpy(params->gamma.blue, gamma_table, sizeof(params->gamma.blue));
  1173. memcpy(params->gamma.green, gamma_table, sizeof(params->gamma.green));
  1174. memcpy(params->gamma.red, gamma_table, sizeof(params->gamma.red));
  1175. params->wbal.dgain = FLR_WBAL_DGAIN;
  1176. params->wbal.coef0 = FLR_WBAL_COEF;
  1177. params->wbal.coef1 = FLR_WBAL_COEF;
  1178. params->wbal.coef2 = FLR_WBAL_COEF;
  1179. params->wbal.coef3 = FLR_WBAL_COEF;
  1180. params->blkadj.red = FLR_BLKADJ_RED;
  1181. params->blkadj.green = FLR_BLKADJ_GREEN;
  1182. params->blkadj.blue = FLR_BLKADJ_BLUE;
  1183. params->rgb2rgb = flr_rgb2rgb;
  1184. params->csc = flr_prev_csc;
  1185. params->yclimit.minC = ISPPRV_YC_MIN;
  1186. params->yclimit.maxC = ISPPRV_YC_MAX;
  1187. params->yclimit.minY = ISPPRV_YC_MIN;
  1188. params->yclimit.maxY = ISPPRV_YC_MAX;
  1189. params->features = OMAP3ISP_PREV_CFA | OMAP3ISP_PREV_DEFECT_COR
  1190. | OMAP3ISP_PREV_NF | OMAP3ISP_PREV_GAMMA
  1191. | OMAP3ISP_PREV_BLKADJ | OMAP3ISP_PREV_YC_LIMIT
  1192. | OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV
  1193. | OMAP3ISP_PREV_WB | OMAP3ISP_PREV_BRIGHTNESS
  1194. | OMAP3ISP_PREV_CONTRAST;
  1195. }
  1196. /*
  1197. * preview_max_out_width - Handle previewer hardware output limitations
  1198. * @prev: pointer to previewer private structure
  1199. * returns maximum width output for current isp revision
  1200. */
  1201. static unsigned int preview_max_out_width(struct isp_prev_device *prev)
  1202. {
  1203. struct isp_device *isp = to_isp_device(prev);
  1204. switch (isp->revision) {
  1205. case ISP_REVISION_1_0:
  1206. return PREV_MAX_OUT_WIDTH_REV_1;
  1207. case ISP_REVISION_2_0:
  1208. default:
  1209. return PREV_MAX_OUT_WIDTH_REV_2;
  1210. case ISP_REVISION_15_0:
  1211. return PREV_MAX_OUT_WIDTH_REV_15;
  1212. }
  1213. }
  1214. static void preview_configure(struct isp_prev_device *prev)
  1215. {
  1216. struct isp_device *isp = to_isp_device(prev);
  1217. const struct isp_format_info *info;
  1218. struct v4l2_mbus_framefmt *format;
  1219. unsigned long flags;
  1220. u32 update;
  1221. u32 active;
  1222. spin_lock_irqsave(&prev->params.lock, flags);
  1223. /* Mark all active parameters we are going to touch as busy. */
  1224. update = preview_params_lock(prev, 0, false);
  1225. active = prev->params.active;
  1226. spin_unlock_irqrestore(&prev->params.lock, flags);
  1227. /* PREV_PAD_SINK */
  1228. format = &prev->formats[PREV_PAD_SINK];
  1229. info = omap3isp_video_format_info(format->code);
  1230. preview_adjust_bandwidth(prev);
  1231. preview_config_input_format(prev, info);
  1232. preview_config_input_size(prev, active);
  1233. if (prev->input == PREVIEW_INPUT_CCDC)
  1234. preview_config_inlineoffset(prev, 0);
  1235. else
  1236. preview_config_inlineoffset(prev, ALIGN(format->width, 0x20) *
  1237. info->bpp);
  1238. preview_setup_hw(prev, update, active);
  1239. /* PREV_PAD_SOURCE */
  1240. format = &prev->formats[PREV_PAD_SOURCE];
  1241. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1242. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1243. ISPPRV_PCR_SDRPORT);
  1244. else
  1245. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1246. ISPPRV_PCR_SDRPORT);
  1247. if (prev->output & PREVIEW_OUTPUT_RESIZER)
  1248. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1249. ISPPRV_PCR_RSZPORT);
  1250. else
  1251. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1252. ISPPRV_PCR_RSZPORT);
  1253. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1254. preview_config_outlineoffset(prev,
  1255. ALIGN(format->width, 0x10) * 2);
  1256. preview_config_averager(prev, 0);
  1257. preview_config_ycpos(prev, format->code);
  1258. spin_lock_irqsave(&prev->params.lock, flags);
  1259. preview_params_unlock(prev, update, false);
  1260. spin_unlock_irqrestore(&prev->params.lock, flags);
  1261. }
  1262. /* -----------------------------------------------------------------------------
  1263. * Interrupt handling
  1264. */
  1265. static void preview_enable_oneshot(struct isp_prev_device *prev)
  1266. {
  1267. struct isp_device *isp = to_isp_device(prev);
  1268. /* The PCR.SOURCE bit is automatically reset to 0 when the PCR.ENABLE
  1269. * bit is set. As the preview engine is used in single-shot mode, we
  1270. * need to set PCR.SOURCE before enabling the preview engine.
  1271. */
  1272. if (prev->input == PREVIEW_INPUT_MEMORY)
  1273. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1274. ISPPRV_PCR_SOURCE);
  1275. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1276. ISPPRV_PCR_EN | ISPPRV_PCR_ONESHOT);
  1277. }
  1278. void omap3isp_preview_isr_frame_sync(struct isp_prev_device *prev)
  1279. {
  1280. /*
  1281. * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
  1282. * condition, the module was paused and now we have a buffer queued
  1283. * on the output again. Restart the pipeline if running in continuous
  1284. * mode.
  1285. */
  1286. if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
  1287. prev->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
  1288. preview_enable_oneshot(prev);
  1289. isp_video_dmaqueue_flags_clr(&prev->video_out);
  1290. }
  1291. }
  1292. static void preview_isr_buffer(struct isp_prev_device *prev)
  1293. {
  1294. struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
  1295. struct isp_buffer *buffer;
  1296. int restart = 0;
  1297. if (prev->input == PREVIEW_INPUT_MEMORY) {
  1298. buffer = omap3isp_video_buffer_next(&prev->video_in);
  1299. if (buffer != NULL)
  1300. preview_set_inaddr(prev, buffer->dma);
  1301. pipe->state |= ISP_PIPELINE_IDLE_INPUT;
  1302. }
  1303. if (prev->output & PREVIEW_OUTPUT_MEMORY) {
  1304. buffer = omap3isp_video_buffer_next(&prev->video_out);
  1305. if (buffer != NULL) {
  1306. preview_set_outaddr(prev, buffer->dma);
  1307. restart = 1;
  1308. }
  1309. pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
  1310. }
  1311. switch (prev->state) {
  1312. case ISP_PIPELINE_STREAM_SINGLESHOT:
  1313. if (isp_pipeline_ready(pipe))
  1314. omap3isp_pipeline_set_stream(pipe,
  1315. ISP_PIPELINE_STREAM_SINGLESHOT);
  1316. break;
  1317. case ISP_PIPELINE_STREAM_CONTINUOUS:
  1318. /* If an underrun occurs, the video queue operation handler will
  1319. * restart the preview engine. Otherwise restart it immediately.
  1320. */
  1321. if (restart)
  1322. preview_enable_oneshot(prev);
  1323. break;
  1324. case ISP_PIPELINE_STREAM_STOPPED:
  1325. default:
  1326. return;
  1327. }
  1328. }
  1329. /*
  1330. * omap3isp_preview_isr - ISP preview engine interrupt handler
  1331. *
  1332. * Manage the preview engine video buffers and configure shadowed registers.
  1333. */
  1334. void omap3isp_preview_isr(struct isp_prev_device *prev)
  1335. {
  1336. unsigned long flags;
  1337. u32 update;
  1338. u32 active;
  1339. if (omap3isp_module_sync_is_stopping(&prev->wait, &prev->stopping))
  1340. return;
  1341. spin_lock_irqsave(&prev->params.lock, flags);
  1342. preview_params_switch(prev);
  1343. update = preview_params_lock(prev, 0, false);
  1344. active = prev->params.active;
  1345. spin_unlock_irqrestore(&prev->params.lock, flags);
  1346. preview_setup_hw(prev, update, active);
  1347. preview_config_input_size(prev, active);
  1348. if (prev->input == PREVIEW_INPUT_MEMORY ||
  1349. prev->output & PREVIEW_OUTPUT_MEMORY)
  1350. preview_isr_buffer(prev);
  1351. else if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS)
  1352. preview_enable_oneshot(prev);
  1353. spin_lock_irqsave(&prev->params.lock, flags);
  1354. preview_params_unlock(prev, update, false);
  1355. spin_unlock_irqrestore(&prev->params.lock, flags);
  1356. }
  1357. /* -----------------------------------------------------------------------------
  1358. * ISP video operations
  1359. */
  1360. static int preview_video_queue(struct isp_video *video,
  1361. struct isp_buffer *buffer)
  1362. {
  1363. struct isp_prev_device *prev = &video->isp->isp_prev;
  1364. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1365. preview_set_inaddr(prev, buffer->dma);
  1366. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1367. preview_set_outaddr(prev, buffer->dma);
  1368. return 0;
  1369. }
  1370. static const struct isp_video_operations preview_video_ops = {
  1371. .queue = preview_video_queue,
  1372. };
  1373. /* -----------------------------------------------------------------------------
  1374. * V4L2 subdev operations
  1375. */
  1376. /*
  1377. * preview_s_ctrl - Handle set control subdev method
  1378. * @ctrl: pointer to v4l2 control structure
  1379. */
  1380. static int preview_s_ctrl(struct v4l2_ctrl *ctrl)
  1381. {
  1382. struct isp_prev_device *prev =
  1383. container_of(ctrl->handler, struct isp_prev_device, ctrls);
  1384. switch (ctrl->id) {
  1385. case V4L2_CID_BRIGHTNESS:
  1386. preview_update_brightness(prev, ctrl->val);
  1387. break;
  1388. case V4L2_CID_CONTRAST:
  1389. preview_update_contrast(prev, ctrl->val);
  1390. break;
  1391. }
  1392. return 0;
  1393. }
  1394. static const struct v4l2_ctrl_ops preview_ctrl_ops = {
  1395. .s_ctrl = preview_s_ctrl,
  1396. };
  1397. /*
  1398. * preview_ioctl - Handle preview module private ioctl's
  1399. * @sd: pointer to v4l2 subdev structure
  1400. * @cmd: configuration command
  1401. * @arg: configuration argument
  1402. * return -EINVAL or zero on success
  1403. */
  1404. static long preview_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  1405. {
  1406. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1407. switch (cmd) {
  1408. case VIDIOC_OMAP3ISP_PRV_CFG:
  1409. return preview_config(prev, arg);
  1410. default:
  1411. return -ENOIOCTLCMD;
  1412. }
  1413. }
  1414. /*
  1415. * preview_set_stream - Enable/Disable streaming on preview subdev
  1416. * @sd : pointer to v4l2 subdev structure
  1417. * @enable: 1 == Enable, 0 == Disable
  1418. * return -EINVAL or zero on success
  1419. */
  1420. static int preview_set_stream(struct v4l2_subdev *sd, int enable)
  1421. {
  1422. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1423. struct isp_video *video_out = &prev->video_out;
  1424. struct isp_device *isp = to_isp_device(prev);
  1425. struct device *dev = to_device(prev);
  1426. if (prev->state == ISP_PIPELINE_STREAM_STOPPED) {
  1427. if (enable == ISP_PIPELINE_STREAM_STOPPED)
  1428. return 0;
  1429. omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
  1430. preview_configure(prev);
  1431. atomic_set(&prev->stopping, 0);
  1432. preview_print_status(prev);
  1433. }
  1434. switch (enable) {
  1435. case ISP_PIPELINE_STREAM_CONTINUOUS:
  1436. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1437. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
  1438. if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED ||
  1439. !(prev->output & PREVIEW_OUTPUT_MEMORY))
  1440. preview_enable_oneshot(prev);
  1441. isp_video_dmaqueue_flags_clr(video_out);
  1442. break;
  1443. case ISP_PIPELINE_STREAM_SINGLESHOT:
  1444. if (prev->input == PREVIEW_INPUT_MEMORY)
  1445. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
  1446. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1447. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
  1448. preview_enable_oneshot(prev);
  1449. break;
  1450. case ISP_PIPELINE_STREAM_STOPPED:
  1451. if (omap3isp_module_sync_idle(&sd->entity, &prev->wait,
  1452. &prev->stopping))
  1453. dev_dbg(dev, "%s: stop timeout.\n", sd->name);
  1454. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
  1455. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
  1456. omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
  1457. isp_video_dmaqueue_flags_clr(video_out);
  1458. break;
  1459. }
  1460. prev->state = enable;
  1461. return 0;
  1462. }
  1463. static struct v4l2_mbus_framefmt *
  1464. __preview_get_format(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
  1465. unsigned int pad, enum v4l2_subdev_format_whence which)
  1466. {
  1467. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1468. return v4l2_subdev_get_try_format(fh, pad);
  1469. else
  1470. return &prev->formats[pad];
  1471. }
  1472. static struct v4l2_rect *
  1473. __preview_get_crop(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
  1474. enum v4l2_subdev_format_whence which)
  1475. {
  1476. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1477. return v4l2_subdev_get_try_crop(fh, PREV_PAD_SINK);
  1478. else
  1479. return &prev->crop;
  1480. }
  1481. /* previewer format descriptions */
  1482. static const unsigned int preview_input_fmts[] = {
  1483. V4L2_MBUS_FMT_Y8_1X8,
  1484. V4L2_MBUS_FMT_SGRBG8_1X8,
  1485. V4L2_MBUS_FMT_SRGGB8_1X8,
  1486. V4L2_MBUS_FMT_SBGGR8_1X8,
  1487. V4L2_MBUS_FMT_SGBRG8_1X8,
  1488. V4L2_MBUS_FMT_Y10_1X10,
  1489. V4L2_MBUS_FMT_SGRBG10_1X10,
  1490. V4L2_MBUS_FMT_SRGGB10_1X10,
  1491. V4L2_MBUS_FMT_SBGGR10_1X10,
  1492. V4L2_MBUS_FMT_SGBRG10_1X10,
  1493. };
  1494. static const unsigned int preview_output_fmts[] = {
  1495. V4L2_MBUS_FMT_UYVY8_1X16,
  1496. V4L2_MBUS_FMT_YUYV8_1X16,
  1497. };
  1498. /*
  1499. * preview_try_format - Validate a format
  1500. * @prev: ISP preview engine
  1501. * @fh: V4L2 subdev file handle
  1502. * @pad: pad number
  1503. * @fmt: format to be validated
  1504. * @which: try/active format selector
  1505. *
  1506. * Validate and adjust the given format for the given pad based on the preview
  1507. * engine limits and the format and crop rectangles on other pads.
  1508. */
  1509. static void preview_try_format(struct isp_prev_device *prev,
  1510. struct v4l2_subdev_fh *fh, unsigned int pad,
  1511. struct v4l2_mbus_framefmt *fmt,
  1512. enum v4l2_subdev_format_whence which)
  1513. {
  1514. enum v4l2_mbus_pixelcode pixelcode;
  1515. struct v4l2_rect *crop;
  1516. unsigned int i;
  1517. switch (pad) {
  1518. case PREV_PAD_SINK:
  1519. /* When reading data from the CCDC, the input size has already
  1520. * been mangled by the CCDC output pad so it can be accepted
  1521. * as-is.
  1522. *
  1523. * When reading data from memory, clamp the requested width and
  1524. * height. The TRM doesn't specify a minimum input height, make
  1525. * sure we got enough lines to enable the noise filter and color
  1526. * filter array interpolation.
  1527. */
  1528. if (prev->input == PREVIEW_INPUT_MEMORY) {
  1529. fmt->width = clamp_t(u32, fmt->width, PREV_MIN_IN_WIDTH,
  1530. preview_max_out_width(prev));
  1531. fmt->height = clamp_t(u32, fmt->height,
  1532. PREV_MIN_IN_HEIGHT,
  1533. PREV_MAX_IN_HEIGHT);
  1534. }
  1535. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  1536. for (i = 0; i < ARRAY_SIZE(preview_input_fmts); i++) {
  1537. if (fmt->code == preview_input_fmts[i])
  1538. break;
  1539. }
  1540. /* If not found, use SGRBG10 as default */
  1541. if (i >= ARRAY_SIZE(preview_input_fmts))
  1542. fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  1543. break;
  1544. case PREV_PAD_SOURCE:
  1545. pixelcode = fmt->code;
  1546. *fmt = *__preview_get_format(prev, fh, PREV_PAD_SINK, which);
  1547. switch (pixelcode) {
  1548. case V4L2_MBUS_FMT_YUYV8_1X16:
  1549. case V4L2_MBUS_FMT_UYVY8_1X16:
  1550. fmt->code = pixelcode;
  1551. break;
  1552. default:
  1553. fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
  1554. break;
  1555. }
  1556. /* The preview module output size is configurable through the
  1557. * averager (horizontal scaling by 1/1, 1/2, 1/4 or 1/8). This
  1558. * is not supported yet, hardcode the output size to the crop
  1559. * rectangle size.
  1560. */
  1561. crop = __preview_get_crop(prev, fh, which);
  1562. fmt->width = crop->width;
  1563. fmt->height = crop->height;
  1564. fmt->colorspace = V4L2_COLORSPACE_JPEG;
  1565. break;
  1566. }
  1567. fmt->field = V4L2_FIELD_NONE;
  1568. }
  1569. /*
  1570. * preview_try_crop - Validate a crop rectangle
  1571. * @prev: ISP preview engine
  1572. * @sink: format on the sink pad
  1573. * @crop: crop rectangle to be validated
  1574. *
  1575. * The preview engine crops lines and columns for its internal operation,
  1576. * depending on which filters are enabled. Enforce minimum crop margins to
  1577. * handle that transparently for userspace.
  1578. *
  1579. * See the explanation at the PREV_MARGIN_* definitions for more details.
  1580. */
  1581. static void preview_try_crop(struct isp_prev_device *prev,
  1582. const struct v4l2_mbus_framefmt *sink,
  1583. struct v4l2_rect *crop)
  1584. {
  1585. unsigned int left = PREV_MARGIN_LEFT;
  1586. unsigned int right = sink->width - PREV_MARGIN_RIGHT;
  1587. unsigned int top = PREV_MARGIN_TOP;
  1588. unsigned int bottom = sink->height - PREV_MARGIN_BOTTOM;
  1589. /* When processing data on-the-fly from the CCDC, at least 2 pixels must
  1590. * be cropped from the left and right sides of the image. As we don't
  1591. * know which filters will be enabled, increase the left and right
  1592. * margins by two.
  1593. */
  1594. if (prev->input == PREVIEW_INPUT_CCDC) {
  1595. left += 2;
  1596. right -= 2;
  1597. }
  1598. /* The CFA filter crops 4 lines and 4 columns in Bayer mode, and 2 lines
  1599. * and no columns in other modes. Increase the margins based on the sink
  1600. * format.
  1601. */
  1602. if (sink->code != V4L2_MBUS_FMT_Y8_1X8 &&
  1603. sink->code != V4L2_MBUS_FMT_Y10_1X10) {
  1604. left += 2;
  1605. right -= 2;
  1606. top += 2;
  1607. bottom -= 2;
  1608. }
  1609. /* Restrict left/top to even values to keep the Bayer pattern. */
  1610. crop->left &= ~1;
  1611. crop->top &= ~1;
  1612. crop->left = clamp_t(u32, crop->left, left, right - PREV_MIN_OUT_WIDTH);
  1613. crop->top = clamp_t(u32, crop->top, top, bottom - PREV_MIN_OUT_HEIGHT);
  1614. crop->width = clamp_t(u32, crop->width, PREV_MIN_OUT_WIDTH,
  1615. right - crop->left);
  1616. crop->height = clamp_t(u32, crop->height, PREV_MIN_OUT_HEIGHT,
  1617. bottom - crop->top);
  1618. }
  1619. /*
  1620. * preview_enum_mbus_code - Handle pixel format enumeration
  1621. * @sd : pointer to v4l2 subdev structure
  1622. * @fh : V4L2 subdev file handle
  1623. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  1624. * return -EINVAL or zero on success
  1625. */
  1626. static int preview_enum_mbus_code(struct v4l2_subdev *sd,
  1627. struct v4l2_subdev_fh *fh,
  1628. struct v4l2_subdev_mbus_code_enum *code)
  1629. {
  1630. switch (code->pad) {
  1631. case PREV_PAD_SINK:
  1632. if (code->index >= ARRAY_SIZE(preview_input_fmts))
  1633. return -EINVAL;
  1634. code->code = preview_input_fmts[code->index];
  1635. break;
  1636. case PREV_PAD_SOURCE:
  1637. if (code->index >= ARRAY_SIZE(preview_output_fmts))
  1638. return -EINVAL;
  1639. code->code = preview_output_fmts[code->index];
  1640. break;
  1641. default:
  1642. return -EINVAL;
  1643. }
  1644. return 0;
  1645. }
  1646. static int preview_enum_frame_size(struct v4l2_subdev *sd,
  1647. struct v4l2_subdev_fh *fh,
  1648. struct v4l2_subdev_frame_size_enum *fse)
  1649. {
  1650. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1651. struct v4l2_mbus_framefmt format;
  1652. if (fse->index != 0)
  1653. return -EINVAL;
  1654. format.code = fse->code;
  1655. format.width = 1;
  1656. format.height = 1;
  1657. preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1658. fse->min_width = format.width;
  1659. fse->min_height = format.height;
  1660. if (format.code != fse->code)
  1661. return -EINVAL;
  1662. format.code = fse->code;
  1663. format.width = -1;
  1664. format.height = -1;
  1665. preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1666. fse->max_width = format.width;
  1667. fse->max_height = format.height;
  1668. return 0;
  1669. }
  1670. /*
  1671. * preview_get_selection - Retrieve a selection rectangle on a pad
  1672. * @sd: ISP preview V4L2 subdevice
  1673. * @fh: V4L2 subdev file handle
  1674. * @sel: Selection rectangle
  1675. *
  1676. * The only supported rectangles are the crop rectangles on the sink pad.
  1677. *
  1678. * Return 0 on success or a negative error code otherwise.
  1679. */
  1680. static int preview_get_selection(struct v4l2_subdev *sd,
  1681. struct v4l2_subdev_fh *fh,
  1682. struct v4l2_subdev_selection *sel)
  1683. {
  1684. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1685. struct v4l2_mbus_framefmt *format;
  1686. if (sel->pad != PREV_PAD_SINK)
  1687. return -EINVAL;
  1688. switch (sel->target) {
  1689. case V4L2_SEL_TGT_CROP_BOUNDS:
  1690. sel->r.left = 0;
  1691. sel->r.top = 0;
  1692. sel->r.width = INT_MAX;
  1693. sel->r.height = INT_MAX;
  1694. format = __preview_get_format(prev, fh, PREV_PAD_SINK,
  1695. sel->which);
  1696. preview_try_crop(prev, format, &sel->r);
  1697. break;
  1698. case V4L2_SEL_TGT_CROP:
  1699. sel->r = *__preview_get_crop(prev, fh, sel->which);
  1700. break;
  1701. default:
  1702. return -EINVAL;
  1703. }
  1704. return 0;
  1705. }
  1706. /*
  1707. * preview_set_selection - Set a selection rectangle on a pad
  1708. * @sd: ISP preview V4L2 subdevice
  1709. * @fh: V4L2 subdev file handle
  1710. * @sel: Selection rectangle
  1711. *
  1712. * The only supported rectangle is the actual crop rectangle on the sink pad.
  1713. *
  1714. * Return 0 on success or a negative error code otherwise.
  1715. */
  1716. static int preview_set_selection(struct v4l2_subdev *sd,
  1717. struct v4l2_subdev_fh *fh,
  1718. struct v4l2_subdev_selection *sel)
  1719. {
  1720. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1721. struct v4l2_mbus_framefmt *format;
  1722. if (sel->target != V4L2_SEL_TGT_CROP ||
  1723. sel->pad != PREV_PAD_SINK)
  1724. return -EINVAL;
  1725. /* The crop rectangle can't be changed while streaming. */
  1726. if (prev->state != ISP_PIPELINE_STREAM_STOPPED)
  1727. return -EBUSY;
  1728. /* Modifying the crop rectangle always changes the format on the source
  1729. * pad. If the KEEP_CONFIG flag is set, just return the current crop
  1730. * rectangle.
  1731. */
  1732. if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
  1733. sel->r = *__preview_get_crop(prev, fh, sel->which);
  1734. return 0;
  1735. }
  1736. format = __preview_get_format(prev, fh, PREV_PAD_SINK, sel->which);
  1737. preview_try_crop(prev, format, &sel->r);
  1738. *__preview_get_crop(prev, fh, sel->which) = sel->r;
  1739. /* Update the source format. */
  1740. format = __preview_get_format(prev, fh, PREV_PAD_SOURCE, sel->which);
  1741. preview_try_format(prev, fh, PREV_PAD_SOURCE, format, sel->which);
  1742. return 0;
  1743. }
  1744. /*
  1745. * preview_get_format - Handle get format by pads subdev method
  1746. * @sd : pointer to v4l2 subdev structure
  1747. * @fh : V4L2 subdev file handle
  1748. * @fmt: pointer to v4l2 subdev format structure
  1749. * return -EINVAL or zero on success
  1750. */
  1751. static int preview_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1752. struct v4l2_subdev_format *fmt)
  1753. {
  1754. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1755. struct v4l2_mbus_framefmt *format;
  1756. format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
  1757. if (format == NULL)
  1758. return -EINVAL;
  1759. fmt->format = *format;
  1760. return 0;
  1761. }
  1762. /*
  1763. * preview_set_format - Handle set format by pads subdev method
  1764. * @sd : pointer to v4l2 subdev structure
  1765. * @fh : V4L2 subdev file handle
  1766. * @fmt: pointer to v4l2 subdev format structure
  1767. * return -EINVAL or zero on success
  1768. */
  1769. static int preview_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1770. struct v4l2_subdev_format *fmt)
  1771. {
  1772. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1773. struct v4l2_mbus_framefmt *format;
  1774. struct v4l2_rect *crop;
  1775. format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
  1776. if (format == NULL)
  1777. return -EINVAL;
  1778. preview_try_format(prev, fh, fmt->pad, &fmt->format, fmt->which);
  1779. *format = fmt->format;
  1780. /* Propagate the format from sink to source */
  1781. if (fmt->pad == PREV_PAD_SINK) {
  1782. /* Reset the crop rectangle. */
  1783. crop = __preview_get_crop(prev, fh, fmt->which);
  1784. crop->left = 0;
  1785. crop->top = 0;
  1786. crop->width = fmt->format.width;
  1787. crop->height = fmt->format.height;
  1788. preview_try_crop(prev, &fmt->format, crop);
  1789. /* Update the source format. */
  1790. format = __preview_get_format(prev, fh, PREV_PAD_SOURCE,
  1791. fmt->which);
  1792. preview_try_format(prev, fh, PREV_PAD_SOURCE, format,
  1793. fmt->which);
  1794. }
  1795. return 0;
  1796. }
  1797. /*
  1798. * preview_init_formats - Initialize formats on all pads
  1799. * @sd: ISP preview V4L2 subdevice
  1800. * @fh: V4L2 subdev file handle
  1801. *
  1802. * Initialize all pad formats with default values. If fh is not NULL, try
  1803. * formats are initialized on the file handle. Otherwise active formats are
  1804. * initialized on the device.
  1805. */
  1806. static int preview_init_formats(struct v4l2_subdev *sd,
  1807. struct v4l2_subdev_fh *fh)
  1808. {
  1809. struct v4l2_subdev_format format;
  1810. memset(&format, 0, sizeof(format));
  1811. format.pad = PREV_PAD_SINK;
  1812. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  1813. format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  1814. format.format.width = 4096;
  1815. format.format.height = 4096;
  1816. preview_set_format(sd, fh, &format);
  1817. return 0;
  1818. }
  1819. /* subdev core operations */
  1820. static const struct v4l2_subdev_core_ops preview_v4l2_core_ops = {
  1821. .ioctl = preview_ioctl,
  1822. };
  1823. /* subdev video operations */
  1824. static const struct v4l2_subdev_video_ops preview_v4l2_video_ops = {
  1825. .s_stream = preview_set_stream,
  1826. };
  1827. /* subdev pad operations */
  1828. static const struct v4l2_subdev_pad_ops preview_v4l2_pad_ops = {
  1829. .enum_mbus_code = preview_enum_mbus_code,
  1830. .enum_frame_size = preview_enum_frame_size,
  1831. .get_fmt = preview_get_format,
  1832. .set_fmt = preview_set_format,
  1833. .get_selection = preview_get_selection,
  1834. .set_selection = preview_set_selection,
  1835. };
  1836. /* subdev operations */
  1837. static const struct v4l2_subdev_ops preview_v4l2_ops = {
  1838. .core = &preview_v4l2_core_ops,
  1839. .video = &preview_v4l2_video_ops,
  1840. .pad = &preview_v4l2_pad_ops,
  1841. };
  1842. /* subdev internal operations */
  1843. static const struct v4l2_subdev_internal_ops preview_v4l2_internal_ops = {
  1844. .open = preview_init_formats,
  1845. };
  1846. /* -----------------------------------------------------------------------------
  1847. * Media entity operations
  1848. */
  1849. /*
  1850. * preview_link_setup - Setup previewer connections.
  1851. * @entity : Pointer to media entity structure
  1852. * @local : Pointer to local pad array
  1853. * @remote : Pointer to remote pad array
  1854. * @flags : Link flags
  1855. * return -EINVAL or zero on success
  1856. */
  1857. static int preview_link_setup(struct media_entity *entity,
  1858. const struct media_pad *local,
  1859. const struct media_pad *remote, u32 flags)
  1860. {
  1861. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1862. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1863. switch (local->index | media_entity_type(remote->entity)) {
  1864. case PREV_PAD_SINK | MEDIA_ENT_T_DEVNODE:
  1865. /* read from memory */
  1866. if (flags & MEDIA_LNK_FL_ENABLED) {
  1867. if (prev->input == PREVIEW_INPUT_CCDC)
  1868. return -EBUSY;
  1869. prev->input = PREVIEW_INPUT_MEMORY;
  1870. } else {
  1871. if (prev->input == PREVIEW_INPUT_MEMORY)
  1872. prev->input = PREVIEW_INPUT_NONE;
  1873. }
  1874. break;
  1875. case PREV_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
  1876. /* read from ccdc */
  1877. if (flags & MEDIA_LNK_FL_ENABLED) {
  1878. if (prev->input == PREVIEW_INPUT_MEMORY)
  1879. return -EBUSY;
  1880. prev->input = PREVIEW_INPUT_CCDC;
  1881. } else {
  1882. if (prev->input == PREVIEW_INPUT_CCDC)
  1883. prev->input = PREVIEW_INPUT_NONE;
  1884. }
  1885. break;
  1886. /*
  1887. * The ISP core doesn't support pipelines with multiple video outputs.
  1888. * Revisit this when it will be implemented, and return -EBUSY for now.
  1889. */
  1890. case PREV_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
  1891. /* write to memory */
  1892. if (flags & MEDIA_LNK_FL_ENABLED) {
  1893. if (prev->output & ~PREVIEW_OUTPUT_MEMORY)
  1894. return -EBUSY;
  1895. prev->output |= PREVIEW_OUTPUT_MEMORY;
  1896. } else {
  1897. prev->output &= ~PREVIEW_OUTPUT_MEMORY;
  1898. }
  1899. break;
  1900. case PREV_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
  1901. /* write to resizer */
  1902. if (flags & MEDIA_LNK_FL_ENABLED) {
  1903. if (prev->output & ~PREVIEW_OUTPUT_RESIZER)
  1904. return -EBUSY;
  1905. prev->output |= PREVIEW_OUTPUT_RESIZER;
  1906. } else {
  1907. prev->output &= ~PREVIEW_OUTPUT_RESIZER;
  1908. }
  1909. break;
  1910. default:
  1911. return -EINVAL;
  1912. }
  1913. return 0;
  1914. }
  1915. /* media operations */
  1916. static const struct media_entity_operations preview_media_ops = {
  1917. .link_setup = preview_link_setup,
  1918. .link_validate = v4l2_subdev_link_validate,
  1919. };
  1920. void omap3isp_preview_unregister_entities(struct isp_prev_device *prev)
  1921. {
  1922. v4l2_device_unregister_subdev(&prev->subdev);
  1923. omap3isp_video_unregister(&prev->video_in);
  1924. omap3isp_video_unregister(&prev->video_out);
  1925. }
  1926. int omap3isp_preview_register_entities(struct isp_prev_device *prev,
  1927. struct v4l2_device *vdev)
  1928. {
  1929. int ret;
  1930. /* Register the subdev and video nodes. */
  1931. ret = v4l2_device_register_subdev(vdev, &prev->subdev);
  1932. if (ret < 0)
  1933. goto error;
  1934. ret = omap3isp_video_register(&prev->video_in, vdev);
  1935. if (ret < 0)
  1936. goto error;
  1937. ret = omap3isp_video_register(&prev->video_out, vdev);
  1938. if (ret < 0)
  1939. goto error;
  1940. return 0;
  1941. error:
  1942. omap3isp_preview_unregister_entities(prev);
  1943. return ret;
  1944. }
  1945. /* -----------------------------------------------------------------------------
  1946. * ISP previewer initialisation and cleanup
  1947. */
  1948. /*
  1949. * preview_init_entities - Initialize subdev and media entity.
  1950. * @prev : Pointer to preview structure
  1951. * return -ENOMEM or zero on success
  1952. */
  1953. static int preview_init_entities(struct isp_prev_device *prev)
  1954. {
  1955. struct v4l2_subdev *sd = &prev->subdev;
  1956. struct media_pad *pads = prev->pads;
  1957. struct media_entity *me = &sd->entity;
  1958. int ret;
  1959. prev->input = PREVIEW_INPUT_NONE;
  1960. v4l2_subdev_init(sd, &preview_v4l2_ops);
  1961. sd->internal_ops = &preview_v4l2_internal_ops;
  1962. strlcpy(sd->name, "OMAP3 ISP preview", sizeof(sd->name));
  1963. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  1964. v4l2_set_subdevdata(sd, prev);
  1965. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1966. v4l2_ctrl_handler_init(&prev->ctrls, 2);
  1967. v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_BRIGHTNESS,
  1968. ISPPRV_BRIGHT_LOW, ISPPRV_BRIGHT_HIGH,
  1969. ISPPRV_BRIGHT_STEP, ISPPRV_BRIGHT_DEF);
  1970. v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_CONTRAST,
  1971. ISPPRV_CONTRAST_LOW, ISPPRV_CONTRAST_HIGH,
  1972. ISPPRV_CONTRAST_STEP, ISPPRV_CONTRAST_DEF);
  1973. v4l2_ctrl_handler_setup(&prev->ctrls);
  1974. sd->ctrl_handler = &prev->ctrls;
  1975. pads[PREV_PAD_SINK].flags = MEDIA_PAD_FL_SINK
  1976. | MEDIA_PAD_FL_MUST_CONNECT;
  1977. pads[PREV_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1978. me->ops = &preview_media_ops;
  1979. ret = media_entity_init(me, PREV_PADS_NUM, pads, 0);
  1980. if (ret < 0)
  1981. return ret;
  1982. preview_init_formats(sd, NULL);
  1983. /* According to the OMAP34xx TRM, video buffers need to be aligned on a
  1984. * 32 bytes boundary. However, an undocumented hardware bug requires a
  1985. * 64 bytes boundary at the preview engine input.
  1986. */
  1987. prev->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1988. prev->video_in.ops = &preview_video_ops;
  1989. prev->video_in.isp = to_isp_device(prev);
  1990. prev->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1991. prev->video_in.bpl_alignment = 64;
  1992. prev->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1993. prev->video_out.ops = &preview_video_ops;
  1994. prev->video_out.isp = to_isp_device(prev);
  1995. prev->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1996. prev->video_out.bpl_alignment = 32;
  1997. ret = omap3isp_video_init(&prev->video_in, "preview");
  1998. if (ret < 0)
  1999. goto error_video_in;
  2000. ret = omap3isp_video_init(&prev->video_out, "preview");
  2001. if (ret < 0)
  2002. goto error_video_out;
  2003. /* Connect the video nodes to the previewer subdev. */
  2004. ret = media_entity_create_link(&prev->video_in.video.entity, 0,
  2005. &prev->subdev.entity, PREV_PAD_SINK, 0);
  2006. if (ret < 0)
  2007. goto error_link;
  2008. ret = media_entity_create_link(&prev->subdev.entity, PREV_PAD_SOURCE,
  2009. &prev->video_out.video.entity, 0, 0);
  2010. if (ret < 0)
  2011. goto error_link;
  2012. return 0;
  2013. error_link:
  2014. omap3isp_video_cleanup(&prev->video_out);
  2015. error_video_out:
  2016. omap3isp_video_cleanup(&prev->video_in);
  2017. error_video_in:
  2018. media_entity_cleanup(&prev->subdev.entity);
  2019. return ret;
  2020. }
  2021. /*
  2022. * omap3isp_preview_init - Previewer initialization.
  2023. * @isp : Pointer to ISP device
  2024. * return -ENOMEM or zero on success
  2025. */
  2026. int omap3isp_preview_init(struct isp_device *isp)
  2027. {
  2028. struct isp_prev_device *prev = &isp->isp_prev;
  2029. init_waitqueue_head(&prev->wait);
  2030. preview_init_params(prev);
  2031. return preview_init_entities(prev);
  2032. }
  2033. void omap3isp_preview_cleanup(struct isp_device *isp)
  2034. {
  2035. struct isp_prev_device *prev = &isp->isp_prev;
  2036. v4l2_ctrl_handler_free(&prev->ctrls);
  2037. omap3isp_video_cleanup(&prev->video_in);
  2038. omap3isp_video_cleanup(&prev->video_out);
  2039. media_entity_cleanup(&prev->subdev.entity);
  2040. }