isppreview.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360
  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. if (update == 0)
  821. return;
  822. for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
  823. const struct preview_update *attr = &update_attrs[i];
  824. struct prev_params *params;
  825. unsigned int bit = 1 << i;
  826. if (!(update & bit))
  827. continue;
  828. params = &prev->params.params[!(active & bit)];
  829. if (params->features & bit) {
  830. if (attr->config)
  831. attr->config(prev, params);
  832. if (attr->enable)
  833. attr->enable(prev, true);
  834. } else {
  835. if (attr->enable)
  836. attr->enable(prev, false);
  837. }
  838. }
  839. }
  840. /*
  841. * preview_config_ycpos - Configure byte layout of YUV image.
  842. * @prev: pointer to previewer private structure
  843. * @pixelcode: pixel code
  844. */
  845. static void preview_config_ycpos(struct isp_prev_device *prev, u32 pixelcode)
  846. {
  847. struct isp_device *isp = to_isp_device(prev);
  848. enum preview_ycpos_mode mode;
  849. switch (pixelcode) {
  850. case MEDIA_BUS_FMT_YUYV8_1X16:
  851. mode = YCPOS_CrYCbY;
  852. break;
  853. case MEDIA_BUS_FMT_UYVY8_1X16:
  854. mode = YCPOS_YCrYCb;
  855. break;
  856. default:
  857. return;
  858. }
  859. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  860. ISPPRV_PCR_YCPOS_CrYCbY,
  861. mode << ISPPRV_PCR_YCPOS_SHIFT);
  862. }
  863. /*
  864. * preview_config_averager - Enable / disable / configure averager
  865. * @average: Average value to be configured.
  866. */
  867. static void preview_config_averager(struct isp_prev_device *prev, u8 average)
  868. {
  869. struct isp_device *isp = to_isp_device(prev);
  870. isp_reg_writel(isp, ISPPRV_AVE_EVENDIST_2 << ISPPRV_AVE_EVENDIST_SHIFT |
  871. ISPPRV_AVE_ODDDIST_2 << ISPPRV_AVE_ODDDIST_SHIFT |
  872. average, OMAP3_ISP_IOMEM_PREV, ISPPRV_AVE);
  873. }
  874. /*
  875. * preview_config_input_format - Configure the input format
  876. * @prev: The preview engine
  877. * @info: Sink pad format information
  878. *
  879. * Enable and configure CFA interpolation for Bayer formats and disable it for
  880. * greyscale formats.
  881. *
  882. * The CFA table is organised in four blocks, one per Bayer component. The
  883. * hardware expects blocks to follow the Bayer order of the input data, while
  884. * the driver stores the table in GRBG order in memory. The blocks need to be
  885. * reordered to support non-GRBG Bayer patterns.
  886. */
  887. static void preview_config_input_format(struct isp_prev_device *prev,
  888. const struct isp_format_info *info)
  889. {
  890. struct isp_device *isp = to_isp_device(prev);
  891. struct prev_params *params;
  892. if (info->width == 8)
  893. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  894. ISPPRV_PCR_WIDTH);
  895. else
  896. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  897. ISPPRV_PCR_WIDTH);
  898. switch (info->flavor) {
  899. case MEDIA_BUS_FMT_SGRBG8_1X8:
  900. prev->params.cfa_order = 0;
  901. break;
  902. case MEDIA_BUS_FMT_SRGGB8_1X8:
  903. prev->params.cfa_order = 1;
  904. break;
  905. case MEDIA_BUS_FMT_SBGGR8_1X8:
  906. prev->params.cfa_order = 2;
  907. break;
  908. case MEDIA_BUS_FMT_SGBRG8_1X8:
  909. prev->params.cfa_order = 3;
  910. break;
  911. default:
  912. /* Disable CFA for non-Bayer formats. */
  913. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  914. ISPPRV_PCR_CFAEN);
  915. return;
  916. }
  917. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR, ISPPRV_PCR_CFAEN);
  918. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  919. ISPPRV_PCR_CFAFMT_MASK, ISPPRV_PCR_CFAFMT_BAYER);
  920. params = (prev->params.active & OMAP3ISP_PREV_CFA)
  921. ? &prev->params.params[0] : &prev->params.params[1];
  922. preview_config_cfa(prev, params);
  923. }
  924. /*
  925. * preview_config_input_size - Configure the input frame size
  926. *
  927. * The preview engine crops several rows and columns internally depending on
  928. * which processing blocks are enabled. The driver assumes all those blocks are
  929. * enabled when reporting source pad formats to userspace. If this assumption is
  930. * not true, rows and columns must be manually cropped at the preview engine
  931. * input to avoid overflows at the end of lines and frames.
  932. *
  933. * See the explanation at the PREV_MARGIN_* definitions for more details.
  934. */
  935. static void preview_config_input_size(struct isp_prev_device *prev, u32 active)
  936. {
  937. const struct v4l2_mbus_framefmt *format = &prev->formats[PREV_PAD_SINK];
  938. struct isp_device *isp = to_isp_device(prev);
  939. unsigned int sph = prev->crop.left;
  940. unsigned int eph = prev->crop.left + prev->crop.width - 1;
  941. unsigned int slv = prev->crop.top;
  942. unsigned int elv = prev->crop.top + prev->crop.height - 1;
  943. u32 features;
  944. if (format->code != MEDIA_BUS_FMT_Y8_1X8 &&
  945. format->code != MEDIA_BUS_FMT_Y10_1X10) {
  946. sph -= 2;
  947. eph += 2;
  948. slv -= 2;
  949. elv += 2;
  950. }
  951. features = (prev->params.params[0].features & active)
  952. | (prev->params.params[1].features & ~active);
  953. if (features & (OMAP3ISP_PREV_DEFECT_COR | OMAP3ISP_PREV_NF)) {
  954. sph -= 2;
  955. eph += 2;
  956. slv -= 2;
  957. elv += 2;
  958. }
  959. if (features & OMAP3ISP_PREV_HRZ_MED) {
  960. sph -= 2;
  961. eph += 2;
  962. }
  963. if (features & (OMAP3ISP_PREV_CHROMA_SUPP | OMAP3ISP_PREV_LUMAENH))
  964. sph -= 2;
  965. isp_reg_writel(isp, (sph << ISPPRV_HORZ_INFO_SPH_SHIFT) | eph,
  966. OMAP3_ISP_IOMEM_PREV, ISPPRV_HORZ_INFO);
  967. isp_reg_writel(isp, (slv << ISPPRV_VERT_INFO_SLV_SHIFT) | elv,
  968. OMAP3_ISP_IOMEM_PREV, ISPPRV_VERT_INFO);
  969. }
  970. /*
  971. * preview_config_inlineoffset - Configures the Read address line offset.
  972. * @prev: Preview module
  973. * @offset: Line offset
  974. *
  975. * According to the TRM, the line offset must be aligned on a 32 bytes boundary.
  976. * However, a hardware bug requires the memory start address to be aligned on a
  977. * 64 bytes boundary, so the offset probably should be aligned on 64 bytes as
  978. * well.
  979. */
  980. static void
  981. preview_config_inlineoffset(struct isp_prev_device *prev, u32 offset)
  982. {
  983. struct isp_device *isp = to_isp_device(prev);
  984. isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
  985. ISPPRV_RADR_OFFSET);
  986. }
  987. /*
  988. * preview_set_inaddr - Sets memory address of input frame.
  989. * @addr: 32bit memory address aligned on 32byte boundary.
  990. *
  991. * Configures the memory address from which the input frame is to be read.
  992. */
  993. static void preview_set_inaddr(struct isp_prev_device *prev, u32 addr)
  994. {
  995. struct isp_device *isp = to_isp_device(prev);
  996. isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_RSDR_ADDR);
  997. }
  998. /*
  999. * preview_config_outlineoffset - Configures the Write address line offset.
  1000. * @offset: Line Offset for the preview output.
  1001. *
  1002. * The offset must be a multiple of 32 bytes.
  1003. */
  1004. static void preview_config_outlineoffset(struct isp_prev_device *prev,
  1005. u32 offset)
  1006. {
  1007. struct isp_device *isp = to_isp_device(prev);
  1008. isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
  1009. ISPPRV_WADD_OFFSET);
  1010. }
  1011. /*
  1012. * preview_set_outaddr - Sets the memory address to store output frame
  1013. * @addr: 32bit memory address aligned on 32byte boundary.
  1014. *
  1015. * Configures the memory address to which the output frame is written.
  1016. */
  1017. static void preview_set_outaddr(struct isp_prev_device *prev, u32 addr)
  1018. {
  1019. struct isp_device *isp = to_isp_device(prev);
  1020. isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_WSDR_ADDR);
  1021. }
  1022. static void preview_adjust_bandwidth(struct isp_prev_device *prev)
  1023. {
  1024. struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
  1025. struct isp_device *isp = to_isp_device(prev);
  1026. const struct v4l2_mbus_framefmt *ifmt = &prev->formats[PREV_PAD_SINK];
  1027. unsigned long l3_ick = pipe->l3_ick;
  1028. struct v4l2_fract *timeperframe;
  1029. unsigned int cycles_per_frame;
  1030. unsigned int requests_per_frame;
  1031. unsigned int cycles_per_request;
  1032. unsigned int minimum;
  1033. unsigned int maximum;
  1034. unsigned int value;
  1035. if (prev->input != PREVIEW_INPUT_MEMORY) {
  1036. isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
  1037. ISPSBL_SDR_REQ_PRV_EXP_MASK);
  1038. return;
  1039. }
  1040. /* Compute the minimum number of cycles per request, based on the
  1041. * pipeline maximum data rate. This is an absolute lower bound if we
  1042. * don't want SBL overflows, so round the value up.
  1043. */
  1044. cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
  1045. pipe->max_rate);
  1046. minimum = DIV_ROUND_UP(cycles_per_request, 32);
  1047. /* Compute the maximum number of cycles per request, based on the
  1048. * requested frame rate. This is a soft upper bound to achieve a frame
  1049. * rate equal or higher than the requested value, so round the value
  1050. * down.
  1051. */
  1052. timeperframe = &pipe->max_timeperframe;
  1053. requests_per_frame = DIV_ROUND_UP(ifmt->width * 2, 256) * ifmt->height;
  1054. cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
  1055. timeperframe->denominator);
  1056. cycles_per_request = cycles_per_frame / requests_per_frame;
  1057. maximum = cycles_per_request / 32;
  1058. value = max(minimum, maximum);
  1059. dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
  1060. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
  1061. ISPSBL_SDR_REQ_PRV_EXP_MASK,
  1062. value << ISPSBL_SDR_REQ_PRV_EXP_SHIFT);
  1063. }
  1064. /*
  1065. * omap3isp_preview_busy - Gets busy state of preview module.
  1066. */
  1067. int omap3isp_preview_busy(struct isp_prev_device *prev)
  1068. {
  1069. struct isp_device *isp = to_isp_device(prev);
  1070. return isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR)
  1071. & ISPPRV_PCR_BUSY;
  1072. }
  1073. /*
  1074. * omap3isp_preview_restore_context - Restores the values of preview registers
  1075. */
  1076. void omap3isp_preview_restore_context(struct isp_device *isp)
  1077. {
  1078. struct isp_prev_device *prev = &isp->isp_prev;
  1079. const u32 update = OMAP3ISP_PREV_FEATURES_END - 1;
  1080. prev->params.params[0].update = prev->params.active & update;
  1081. prev->params.params[1].update = ~prev->params.active & update;
  1082. preview_setup_hw(prev, update, prev->params.active);
  1083. prev->params.params[0].update = 0;
  1084. prev->params.params[1].update = 0;
  1085. }
  1086. /*
  1087. * preview_print_status - Dump preview module registers to the kernel log
  1088. */
  1089. #define PREV_PRINT_REGISTER(isp, name)\
  1090. dev_dbg(isp->dev, "###PRV " #name "=0x%08x\n", \
  1091. isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_##name))
  1092. static void preview_print_status(struct isp_prev_device *prev)
  1093. {
  1094. struct isp_device *isp = to_isp_device(prev);
  1095. dev_dbg(isp->dev, "-------------Preview Register dump----------\n");
  1096. PREV_PRINT_REGISTER(isp, PCR);
  1097. PREV_PRINT_REGISTER(isp, HORZ_INFO);
  1098. PREV_PRINT_REGISTER(isp, VERT_INFO);
  1099. PREV_PRINT_REGISTER(isp, RSDR_ADDR);
  1100. PREV_PRINT_REGISTER(isp, RADR_OFFSET);
  1101. PREV_PRINT_REGISTER(isp, DSDR_ADDR);
  1102. PREV_PRINT_REGISTER(isp, DRKF_OFFSET);
  1103. PREV_PRINT_REGISTER(isp, WSDR_ADDR);
  1104. PREV_PRINT_REGISTER(isp, WADD_OFFSET);
  1105. PREV_PRINT_REGISTER(isp, AVE);
  1106. PREV_PRINT_REGISTER(isp, HMED);
  1107. PREV_PRINT_REGISTER(isp, NF);
  1108. PREV_PRINT_REGISTER(isp, WB_DGAIN);
  1109. PREV_PRINT_REGISTER(isp, WBGAIN);
  1110. PREV_PRINT_REGISTER(isp, WBSEL);
  1111. PREV_PRINT_REGISTER(isp, CFA);
  1112. PREV_PRINT_REGISTER(isp, BLKADJOFF);
  1113. PREV_PRINT_REGISTER(isp, RGB_MAT1);
  1114. PREV_PRINT_REGISTER(isp, RGB_MAT2);
  1115. PREV_PRINT_REGISTER(isp, RGB_MAT3);
  1116. PREV_PRINT_REGISTER(isp, RGB_MAT4);
  1117. PREV_PRINT_REGISTER(isp, RGB_MAT5);
  1118. PREV_PRINT_REGISTER(isp, RGB_OFF1);
  1119. PREV_PRINT_REGISTER(isp, RGB_OFF2);
  1120. PREV_PRINT_REGISTER(isp, CSC0);
  1121. PREV_PRINT_REGISTER(isp, CSC1);
  1122. PREV_PRINT_REGISTER(isp, CSC2);
  1123. PREV_PRINT_REGISTER(isp, CSC_OFFSET);
  1124. PREV_PRINT_REGISTER(isp, CNT_BRT);
  1125. PREV_PRINT_REGISTER(isp, CSUP);
  1126. PREV_PRINT_REGISTER(isp, SETUP_YC);
  1127. PREV_PRINT_REGISTER(isp, SET_TBL_ADDR);
  1128. PREV_PRINT_REGISTER(isp, CDC_THR0);
  1129. PREV_PRINT_REGISTER(isp, CDC_THR1);
  1130. PREV_PRINT_REGISTER(isp, CDC_THR2);
  1131. PREV_PRINT_REGISTER(isp, CDC_THR3);
  1132. dev_dbg(isp->dev, "--------------------------------------------\n");
  1133. }
  1134. /*
  1135. * preview_init_params - init image processing parameters.
  1136. * @prev: pointer to previewer private structure
  1137. */
  1138. static void preview_init_params(struct isp_prev_device *prev)
  1139. {
  1140. struct prev_params *params;
  1141. unsigned int i;
  1142. spin_lock_init(&prev->params.lock);
  1143. prev->params.active = ~0;
  1144. prev->params.params[0].busy = 0;
  1145. prev->params.params[0].update = OMAP3ISP_PREV_FEATURES_END - 1;
  1146. prev->params.params[1].busy = 0;
  1147. prev->params.params[1].update = 0;
  1148. params = &prev->params.params[0];
  1149. /* Init values */
  1150. params->contrast = ISPPRV_CONTRAST_DEF * ISPPRV_CONTRAST_UNITS;
  1151. params->brightness = ISPPRV_BRIGHT_DEF * ISPPRV_BRIGHT_UNITS;
  1152. params->cfa.format = OMAP3ISP_CFAFMT_BAYER;
  1153. memcpy(params->cfa.table, cfa_coef_table,
  1154. sizeof(params->cfa.table));
  1155. params->cfa.gradthrs_horz = FLR_CFA_GRADTHRS_HORZ;
  1156. params->cfa.gradthrs_vert = FLR_CFA_GRADTHRS_VERT;
  1157. params->csup.gain = FLR_CSUP_GAIN;
  1158. params->csup.thres = FLR_CSUP_THRES;
  1159. params->csup.hypf_en = 0;
  1160. memcpy(params->luma.table, luma_enhance_table,
  1161. sizeof(params->luma.table));
  1162. params->nf.spread = FLR_NF_STRGTH;
  1163. memcpy(params->nf.table, noise_filter_table, sizeof(params->nf.table));
  1164. params->dcor.couplet_mode_en = 1;
  1165. for (i = 0; i < OMAP3ISP_PREV_DETECT_CORRECT_CHANNELS; i++)
  1166. params->dcor.detect_correct[i] = DEF_DETECT_CORRECT_VAL;
  1167. memcpy(params->gamma.blue, gamma_table, sizeof(params->gamma.blue));
  1168. memcpy(params->gamma.green, gamma_table, sizeof(params->gamma.green));
  1169. memcpy(params->gamma.red, gamma_table, sizeof(params->gamma.red));
  1170. params->wbal.dgain = FLR_WBAL_DGAIN;
  1171. params->wbal.coef0 = FLR_WBAL_COEF;
  1172. params->wbal.coef1 = FLR_WBAL_COEF;
  1173. params->wbal.coef2 = FLR_WBAL_COEF;
  1174. params->wbal.coef3 = FLR_WBAL_COEF;
  1175. params->blkadj.red = FLR_BLKADJ_RED;
  1176. params->blkadj.green = FLR_BLKADJ_GREEN;
  1177. params->blkadj.blue = FLR_BLKADJ_BLUE;
  1178. params->rgb2rgb = flr_rgb2rgb;
  1179. params->csc = flr_prev_csc;
  1180. params->yclimit.minC = ISPPRV_YC_MIN;
  1181. params->yclimit.maxC = ISPPRV_YC_MAX;
  1182. params->yclimit.minY = ISPPRV_YC_MIN;
  1183. params->yclimit.maxY = ISPPRV_YC_MAX;
  1184. params->features = OMAP3ISP_PREV_CFA | OMAP3ISP_PREV_DEFECT_COR
  1185. | OMAP3ISP_PREV_NF | OMAP3ISP_PREV_GAMMA
  1186. | OMAP3ISP_PREV_BLKADJ | OMAP3ISP_PREV_YC_LIMIT
  1187. | OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV
  1188. | OMAP3ISP_PREV_WB | OMAP3ISP_PREV_BRIGHTNESS
  1189. | OMAP3ISP_PREV_CONTRAST;
  1190. }
  1191. /*
  1192. * preview_max_out_width - Handle previewer hardware output limitations
  1193. * @prev: pointer to previewer private structure
  1194. * returns maximum width output for current isp revision
  1195. */
  1196. static unsigned int preview_max_out_width(struct isp_prev_device *prev)
  1197. {
  1198. struct isp_device *isp = to_isp_device(prev);
  1199. switch (isp->revision) {
  1200. case ISP_REVISION_1_0:
  1201. return PREV_MAX_OUT_WIDTH_REV_1;
  1202. case ISP_REVISION_2_0:
  1203. default:
  1204. return PREV_MAX_OUT_WIDTH_REV_2;
  1205. case ISP_REVISION_15_0:
  1206. return PREV_MAX_OUT_WIDTH_REV_15;
  1207. }
  1208. }
  1209. static void preview_configure(struct isp_prev_device *prev)
  1210. {
  1211. struct isp_device *isp = to_isp_device(prev);
  1212. const struct isp_format_info *info;
  1213. struct v4l2_mbus_framefmt *format;
  1214. unsigned long flags;
  1215. u32 update;
  1216. u32 active;
  1217. spin_lock_irqsave(&prev->params.lock, flags);
  1218. /* Mark all active parameters we are going to touch as busy. */
  1219. update = preview_params_lock(prev, 0, false);
  1220. active = prev->params.active;
  1221. spin_unlock_irqrestore(&prev->params.lock, flags);
  1222. /* PREV_PAD_SINK */
  1223. format = &prev->formats[PREV_PAD_SINK];
  1224. info = omap3isp_video_format_info(format->code);
  1225. preview_adjust_bandwidth(prev);
  1226. preview_config_input_format(prev, info);
  1227. preview_config_input_size(prev, active);
  1228. if (prev->input == PREVIEW_INPUT_CCDC)
  1229. preview_config_inlineoffset(prev, 0);
  1230. else
  1231. preview_config_inlineoffset(prev, ALIGN(format->width, 0x20) *
  1232. info->bpp);
  1233. preview_setup_hw(prev, update, active);
  1234. /* PREV_PAD_SOURCE */
  1235. format = &prev->formats[PREV_PAD_SOURCE];
  1236. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1237. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1238. ISPPRV_PCR_SDRPORT);
  1239. else
  1240. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1241. ISPPRV_PCR_SDRPORT);
  1242. if (prev->output & PREVIEW_OUTPUT_RESIZER)
  1243. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1244. ISPPRV_PCR_RSZPORT);
  1245. else
  1246. isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1247. ISPPRV_PCR_RSZPORT);
  1248. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1249. preview_config_outlineoffset(prev,
  1250. ALIGN(format->width, 0x10) * 2);
  1251. preview_config_averager(prev, 0);
  1252. preview_config_ycpos(prev, format->code);
  1253. spin_lock_irqsave(&prev->params.lock, flags);
  1254. preview_params_unlock(prev, update, false);
  1255. spin_unlock_irqrestore(&prev->params.lock, flags);
  1256. }
  1257. /* -----------------------------------------------------------------------------
  1258. * Interrupt handling
  1259. */
  1260. static void preview_enable_oneshot(struct isp_prev_device *prev)
  1261. {
  1262. struct isp_device *isp = to_isp_device(prev);
  1263. /* The PCR.SOURCE bit is automatically reset to 0 when the PCR.ENABLE
  1264. * bit is set. As the preview engine is used in single-shot mode, we
  1265. * need to set PCR.SOURCE before enabling the preview engine.
  1266. */
  1267. if (prev->input == PREVIEW_INPUT_MEMORY)
  1268. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1269. ISPPRV_PCR_SOURCE);
  1270. isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
  1271. ISPPRV_PCR_EN | ISPPRV_PCR_ONESHOT);
  1272. }
  1273. void omap3isp_preview_isr_frame_sync(struct isp_prev_device *prev)
  1274. {
  1275. /*
  1276. * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
  1277. * condition, the module was paused and now we have a buffer queued
  1278. * on the output again. Restart the pipeline if running in continuous
  1279. * mode.
  1280. */
  1281. if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
  1282. prev->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
  1283. preview_enable_oneshot(prev);
  1284. isp_video_dmaqueue_flags_clr(&prev->video_out);
  1285. }
  1286. }
  1287. static void preview_isr_buffer(struct isp_prev_device *prev)
  1288. {
  1289. struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
  1290. struct isp_buffer *buffer;
  1291. int restart = 0;
  1292. if (prev->input == PREVIEW_INPUT_MEMORY) {
  1293. buffer = omap3isp_video_buffer_next(&prev->video_in);
  1294. if (buffer != NULL)
  1295. preview_set_inaddr(prev, buffer->dma);
  1296. pipe->state |= ISP_PIPELINE_IDLE_INPUT;
  1297. }
  1298. if (prev->output & PREVIEW_OUTPUT_MEMORY) {
  1299. buffer = omap3isp_video_buffer_next(&prev->video_out);
  1300. if (buffer != NULL) {
  1301. preview_set_outaddr(prev, buffer->dma);
  1302. restart = 1;
  1303. }
  1304. pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
  1305. }
  1306. switch (prev->state) {
  1307. case ISP_PIPELINE_STREAM_SINGLESHOT:
  1308. if (isp_pipeline_ready(pipe))
  1309. omap3isp_pipeline_set_stream(pipe,
  1310. ISP_PIPELINE_STREAM_SINGLESHOT);
  1311. break;
  1312. case ISP_PIPELINE_STREAM_CONTINUOUS:
  1313. /* If an underrun occurs, the video queue operation handler will
  1314. * restart the preview engine. Otherwise restart it immediately.
  1315. */
  1316. if (restart)
  1317. preview_enable_oneshot(prev);
  1318. break;
  1319. case ISP_PIPELINE_STREAM_STOPPED:
  1320. default:
  1321. return;
  1322. }
  1323. }
  1324. /*
  1325. * omap3isp_preview_isr - ISP preview engine interrupt handler
  1326. *
  1327. * Manage the preview engine video buffers and configure shadowed registers.
  1328. */
  1329. void omap3isp_preview_isr(struct isp_prev_device *prev)
  1330. {
  1331. unsigned long flags;
  1332. u32 update;
  1333. u32 active;
  1334. if (omap3isp_module_sync_is_stopping(&prev->wait, &prev->stopping))
  1335. return;
  1336. spin_lock_irqsave(&prev->params.lock, flags);
  1337. preview_params_switch(prev);
  1338. update = preview_params_lock(prev, 0, false);
  1339. active = prev->params.active;
  1340. spin_unlock_irqrestore(&prev->params.lock, flags);
  1341. preview_setup_hw(prev, update, active);
  1342. preview_config_input_size(prev, active);
  1343. if (prev->input == PREVIEW_INPUT_MEMORY ||
  1344. prev->output & PREVIEW_OUTPUT_MEMORY)
  1345. preview_isr_buffer(prev);
  1346. else if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS)
  1347. preview_enable_oneshot(prev);
  1348. spin_lock_irqsave(&prev->params.lock, flags);
  1349. preview_params_unlock(prev, update, false);
  1350. spin_unlock_irqrestore(&prev->params.lock, flags);
  1351. }
  1352. /* -----------------------------------------------------------------------------
  1353. * ISP video operations
  1354. */
  1355. static int preview_video_queue(struct isp_video *video,
  1356. struct isp_buffer *buffer)
  1357. {
  1358. struct isp_prev_device *prev = &video->isp->isp_prev;
  1359. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1360. preview_set_inaddr(prev, buffer->dma);
  1361. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1362. preview_set_outaddr(prev, buffer->dma);
  1363. return 0;
  1364. }
  1365. static const struct isp_video_operations preview_video_ops = {
  1366. .queue = preview_video_queue,
  1367. };
  1368. /* -----------------------------------------------------------------------------
  1369. * V4L2 subdev operations
  1370. */
  1371. /*
  1372. * preview_s_ctrl - Handle set control subdev method
  1373. * @ctrl: pointer to v4l2 control structure
  1374. */
  1375. static int preview_s_ctrl(struct v4l2_ctrl *ctrl)
  1376. {
  1377. struct isp_prev_device *prev =
  1378. container_of(ctrl->handler, struct isp_prev_device, ctrls);
  1379. switch (ctrl->id) {
  1380. case V4L2_CID_BRIGHTNESS:
  1381. preview_update_brightness(prev, ctrl->val);
  1382. break;
  1383. case V4L2_CID_CONTRAST:
  1384. preview_update_contrast(prev, ctrl->val);
  1385. break;
  1386. }
  1387. return 0;
  1388. }
  1389. static const struct v4l2_ctrl_ops preview_ctrl_ops = {
  1390. .s_ctrl = preview_s_ctrl,
  1391. };
  1392. /*
  1393. * preview_ioctl - Handle preview module private ioctl's
  1394. * @sd: pointer to v4l2 subdev structure
  1395. * @cmd: configuration command
  1396. * @arg: configuration argument
  1397. * return -EINVAL or zero on success
  1398. */
  1399. static long preview_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  1400. {
  1401. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1402. switch (cmd) {
  1403. case VIDIOC_OMAP3ISP_PRV_CFG:
  1404. return preview_config(prev, arg);
  1405. default:
  1406. return -ENOIOCTLCMD;
  1407. }
  1408. }
  1409. /*
  1410. * preview_set_stream - Enable/Disable streaming on preview subdev
  1411. * @sd : pointer to v4l2 subdev structure
  1412. * @enable: 1 == Enable, 0 == Disable
  1413. * return -EINVAL or zero on success
  1414. */
  1415. static int preview_set_stream(struct v4l2_subdev *sd, int enable)
  1416. {
  1417. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1418. struct isp_video *video_out = &prev->video_out;
  1419. struct isp_device *isp = to_isp_device(prev);
  1420. struct device *dev = to_device(prev);
  1421. if (prev->state == ISP_PIPELINE_STREAM_STOPPED) {
  1422. if (enable == ISP_PIPELINE_STREAM_STOPPED)
  1423. return 0;
  1424. omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
  1425. preview_configure(prev);
  1426. atomic_set(&prev->stopping, 0);
  1427. preview_print_status(prev);
  1428. }
  1429. switch (enable) {
  1430. case ISP_PIPELINE_STREAM_CONTINUOUS:
  1431. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1432. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
  1433. if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED ||
  1434. !(prev->output & PREVIEW_OUTPUT_MEMORY))
  1435. preview_enable_oneshot(prev);
  1436. isp_video_dmaqueue_flags_clr(video_out);
  1437. break;
  1438. case ISP_PIPELINE_STREAM_SINGLESHOT:
  1439. if (prev->input == PREVIEW_INPUT_MEMORY)
  1440. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
  1441. if (prev->output & PREVIEW_OUTPUT_MEMORY)
  1442. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
  1443. preview_enable_oneshot(prev);
  1444. break;
  1445. case ISP_PIPELINE_STREAM_STOPPED:
  1446. if (omap3isp_module_sync_idle(&sd->entity, &prev->wait,
  1447. &prev->stopping))
  1448. dev_dbg(dev, "%s: stop timeout.\n", sd->name);
  1449. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
  1450. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
  1451. omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
  1452. isp_video_dmaqueue_flags_clr(video_out);
  1453. break;
  1454. }
  1455. prev->state = enable;
  1456. return 0;
  1457. }
  1458. static struct v4l2_mbus_framefmt *
  1459. __preview_get_format(struct isp_prev_device *prev, struct v4l2_subdev_pad_config *cfg,
  1460. unsigned int pad, enum v4l2_subdev_format_whence which)
  1461. {
  1462. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1463. return v4l2_subdev_get_try_format(&prev->subdev, cfg, pad);
  1464. else
  1465. return &prev->formats[pad];
  1466. }
  1467. static struct v4l2_rect *
  1468. __preview_get_crop(struct isp_prev_device *prev, struct v4l2_subdev_pad_config *cfg,
  1469. enum v4l2_subdev_format_whence which)
  1470. {
  1471. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1472. return v4l2_subdev_get_try_crop(&prev->subdev, cfg, PREV_PAD_SINK);
  1473. else
  1474. return &prev->crop;
  1475. }
  1476. /* previewer format descriptions */
  1477. static const unsigned int preview_input_fmts[] = {
  1478. MEDIA_BUS_FMT_Y8_1X8,
  1479. MEDIA_BUS_FMT_SGRBG8_1X8,
  1480. MEDIA_BUS_FMT_SRGGB8_1X8,
  1481. MEDIA_BUS_FMT_SBGGR8_1X8,
  1482. MEDIA_BUS_FMT_SGBRG8_1X8,
  1483. MEDIA_BUS_FMT_Y10_1X10,
  1484. MEDIA_BUS_FMT_SGRBG10_1X10,
  1485. MEDIA_BUS_FMT_SRGGB10_1X10,
  1486. MEDIA_BUS_FMT_SBGGR10_1X10,
  1487. MEDIA_BUS_FMT_SGBRG10_1X10,
  1488. };
  1489. static const unsigned int preview_output_fmts[] = {
  1490. MEDIA_BUS_FMT_UYVY8_1X16,
  1491. MEDIA_BUS_FMT_YUYV8_1X16,
  1492. };
  1493. /*
  1494. * preview_try_format - Validate a format
  1495. * @prev: ISP preview engine
  1496. * @cfg: V4L2 subdev pad configuration
  1497. * @pad: pad number
  1498. * @fmt: format to be validated
  1499. * @which: try/active format selector
  1500. *
  1501. * Validate and adjust the given format for the given pad based on the preview
  1502. * engine limits and the format and crop rectangles on other pads.
  1503. */
  1504. static void preview_try_format(struct isp_prev_device *prev,
  1505. struct v4l2_subdev_pad_config *cfg, unsigned int pad,
  1506. struct v4l2_mbus_framefmt *fmt,
  1507. enum v4l2_subdev_format_whence which)
  1508. {
  1509. u32 pixelcode;
  1510. struct v4l2_rect *crop;
  1511. unsigned int i;
  1512. switch (pad) {
  1513. case PREV_PAD_SINK:
  1514. /* When reading data from the CCDC, the input size has already
  1515. * been mangled by the CCDC output pad so it can be accepted
  1516. * as-is.
  1517. *
  1518. * When reading data from memory, clamp the requested width and
  1519. * height. The TRM doesn't specify a minimum input height, make
  1520. * sure we got enough lines to enable the noise filter and color
  1521. * filter array interpolation.
  1522. */
  1523. if (prev->input == PREVIEW_INPUT_MEMORY) {
  1524. fmt->width = clamp_t(u32, fmt->width, PREV_MIN_IN_WIDTH,
  1525. preview_max_out_width(prev));
  1526. fmt->height = clamp_t(u32, fmt->height,
  1527. PREV_MIN_IN_HEIGHT,
  1528. PREV_MAX_IN_HEIGHT);
  1529. }
  1530. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  1531. for (i = 0; i < ARRAY_SIZE(preview_input_fmts); i++) {
  1532. if (fmt->code == preview_input_fmts[i])
  1533. break;
  1534. }
  1535. /* If not found, use SGRBG10 as default */
  1536. if (i >= ARRAY_SIZE(preview_input_fmts))
  1537. fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  1538. break;
  1539. case PREV_PAD_SOURCE:
  1540. pixelcode = fmt->code;
  1541. *fmt = *__preview_get_format(prev, cfg, PREV_PAD_SINK, which);
  1542. switch (pixelcode) {
  1543. case MEDIA_BUS_FMT_YUYV8_1X16:
  1544. case MEDIA_BUS_FMT_UYVY8_1X16:
  1545. fmt->code = pixelcode;
  1546. break;
  1547. default:
  1548. fmt->code = MEDIA_BUS_FMT_YUYV8_1X16;
  1549. break;
  1550. }
  1551. /* The preview module output size is configurable through the
  1552. * averager (horizontal scaling by 1/1, 1/2, 1/4 or 1/8). This
  1553. * is not supported yet, hardcode the output size to the crop
  1554. * rectangle size.
  1555. */
  1556. crop = __preview_get_crop(prev, cfg, which);
  1557. fmt->width = crop->width;
  1558. fmt->height = crop->height;
  1559. fmt->colorspace = V4L2_COLORSPACE_JPEG;
  1560. break;
  1561. }
  1562. fmt->field = V4L2_FIELD_NONE;
  1563. }
  1564. /*
  1565. * preview_try_crop - Validate a crop rectangle
  1566. * @prev: ISP preview engine
  1567. * @sink: format on the sink pad
  1568. * @crop: crop rectangle to be validated
  1569. *
  1570. * The preview engine crops lines and columns for its internal operation,
  1571. * depending on which filters are enabled. Enforce minimum crop margins to
  1572. * handle that transparently for userspace.
  1573. *
  1574. * See the explanation at the PREV_MARGIN_* definitions for more details.
  1575. */
  1576. static void preview_try_crop(struct isp_prev_device *prev,
  1577. const struct v4l2_mbus_framefmt *sink,
  1578. struct v4l2_rect *crop)
  1579. {
  1580. unsigned int left = PREV_MARGIN_LEFT;
  1581. unsigned int right = sink->width - PREV_MARGIN_RIGHT;
  1582. unsigned int top = PREV_MARGIN_TOP;
  1583. unsigned int bottom = sink->height - PREV_MARGIN_BOTTOM;
  1584. /* When processing data on-the-fly from the CCDC, at least 2 pixels must
  1585. * be cropped from the left and right sides of the image. As we don't
  1586. * know which filters will be enabled, increase the left and right
  1587. * margins by two.
  1588. */
  1589. if (prev->input == PREVIEW_INPUT_CCDC) {
  1590. left += 2;
  1591. right -= 2;
  1592. }
  1593. /* The CFA filter crops 4 lines and 4 columns in Bayer mode, and 2 lines
  1594. * and no columns in other modes. Increase the margins based on the sink
  1595. * format.
  1596. */
  1597. if (sink->code != MEDIA_BUS_FMT_Y8_1X8 &&
  1598. sink->code != MEDIA_BUS_FMT_Y10_1X10) {
  1599. left += 2;
  1600. right -= 2;
  1601. top += 2;
  1602. bottom -= 2;
  1603. }
  1604. /* Restrict left/top to even values to keep the Bayer pattern. */
  1605. crop->left &= ~1;
  1606. crop->top &= ~1;
  1607. crop->left = clamp_t(u32, crop->left, left, right - PREV_MIN_OUT_WIDTH);
  1608. crop->top = clamp_t(u32, crop->top, top, bottom - PREV_MIN_OUT_HEIGHT);
  1609. crop->width = clamp_t(u32, crop->width, PREV_MIN_OUT_WIDTH,
  1610. right - crop->left);
  1611. crop->height = clamp_t(u32, crop->height, PREV_MIN_OUT_HEIGHT,
  1612. bottom - crop->top);
  1613. }
  1614. /*
  1615. * preview_enum_mbus_code - Handle pixel format enumeration
  1616. * @sd : pointer to v4l2 subdev structure
  1617. * @cfg: V4L2 subdev pad configuration
  1618. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  1619. * return -EINVAL or zero on success
  1620. */
  1621. static int preview_enum_mbus_code(struct v4l2_subdev *sd,
  1622. struct v4l2_subdev_pad_config *cfg,
  1623. struct v4l2_subdev_mbus_code_enum *code)
  1624. {
  1625. switch (code->pad) {
  1626. case PREV_PAD_SINK:
  1627. if (code->index >= ARRAY_SIZE(preview_input_fmts))
  1628. return -EINVAL;
  1629. code->code = preview_input_fmts[code->index];
  1630. break;
  1631. case PREV_PAD_SOURCE:
  1632. if (code->index >= ARRAY_SIZE(preview_output_fmts))
  1633. return -EINVAL;
  1634. code->code = preview_output_fmts[code->index];
  1635. break;
  1636. default:
  1637. return -EINVAL;
  1638. }
  1639. return 0;
  1640. }
  1641. static int preview_enum_frame_size(struct v4l2_subdev *sd,
  1642. struct v4l2_subdev_pad_config *cfg,
  1643. struct v4l2_subdev_frame_size_enum *fse)
  1644. {
  1645. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1646. struct v4l2_mbus_framefmt format;
  1647. if (fse->index != 0)
  1648. return -EINVAL;
  1649. format.code = fse->code;
  1650. format.width = 1;
  1651. format.height = 1;
  1652. preview_try_format(prev, cfg, fse->pad, &format, fse->which);
  1653. fse->min_width = format.width;
  1654. fse->min_height = format.height;
  1655. if (format.code != fse->code)
  1656. return -EINVAL;
  1657. format.code = fse->code;
  1658. format.width = -1;
  1659. format.height = -1;
  1660. preview_try_format(prev, cfg, fse->pad, &format, fse->which);
  1661. fse->max_width = format.width;
  1662. fse->max_height = format.height;
  1663. return 0;
  1664. }
  1665. /*
  1666. * preview_get_selection - Retrieve a selection rectangle on a pad
  1667. * @sd: ISP preview V4L2 subdevice
  1668. * @cfg: V4L2 subdev pad configuration
  1669. * @sel: Selection rectangle
  1670. *
  1671. * The only supported rectangles are the crop rectangles on the sink pad.
  1672. *
  1673. * Return 0 on success or a negative error code otherwise.
  1674. */
  1675. static int preview_get_selection(struct v4l2_subdev *sd,
  1676. struct v4l2_subdev_pad_config *cfg,
  1677. struct v4l2_subdev_selection *sel)
  1678. {
  1679. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1680. struct v4l2_mbus_framefmt *format;
  1681. if (sel->pad != PREV_PAD_SINK)
  1682. return -EINVAL;
  1683. switch (sel->target) {
  1684. case V4L2_SEL_TGT_CROP_BOUNDS:
  1685. sel->r.left = 0;
  1686. sel->r.top = 0;
  1687. sel->r.width = INT_MAX;
  1688. sel->r.height = INT_MAX;
  1689. format = __preview_get_format(prev, cfg, PREV_PAD_SINK,
  1690. sel->which);
  1691. preview_try_crop(prev, format, &sel->r);
  1692. break;
  1693. case V4L2_SEL_TGT_CROP:
  1694. sel->r = *__preview_get_crop(prev, cfg, sel->which);
  1695. break;
  1696. default:
  1697. return -EINVAL;
  1698. }
  1699. return 0;
  1700. }
  1701. /*
  1702. * preview_set_selection - Set a selection rectangle on a pad
  1703. * @sd: ISP preview V4L2 subdevice
  1704. * @cfg: V4L2 subdev pad configuration
  1705. * @sel: Selection rectangle
  1706. *
  1707. * The only supported rectangle is the actual crop rectangle on the sink pad.
  1708. *
  1709. * Return 0 on success or a negative error code otherwise.
  1710. */
  1711. static int preview_set_selection(struct v4l2_subdev *sd,
  1712. struct v4l2_subdev_pad_config *cfg,
  1713. struct v4l2_subdev_selection *sel)
  1714. {
  1715. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1716. struct v4l2_mbus_framefmt *format;
  1717. if (sel->target != V4L2_SEL_TGT_CROP ||
  1718. sel->pad != PREV_PAD_SINK)
  1719. return -EINVAL;
  1720. /* The crop rectangle can't be changed while streaming. */
  1721. if (prev->state != ISP_PIPELINE_STREAM_STOPPED)
  1722. return -EBUSY;
  1723. /* Modifying the crop rectangle always changes the format on the source
  1724. * pad. If the KEEP_CONFIG flag is set, just return the current crop
  1725. * rectangle.
  1726. */
  1727. if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
  1728. sel->r = *__preview_get_crop(prev, cfg, sel->which);
  1729. return 0;
  1730. }
  1731. format = __preview_get_format(prev, cfg, PREV_PAD_SINK, sel->which);
  1732. preview_try_crop(prev, format, &sel->r);
  1733. *__preview_get_crop(prev, cfg, sel->which) = sel->r;
  1734. /* Update the source format. */
  1735. format = __preview_get_format(prev, cfg, PREV_PAD_SOURCE, sel->which);
  1736. preview_try_format(prev, cfg, PREV_PAD_SOURCE, format, sel->which);
  1737. return 0;
  1738. }
  1739. /*
  1740. * preview_get_format - Handle get format by pads subdev method
  1741. * @sd : pointer to v4l2 subdev structure
  1742. * @cfg: V4L2 subdev pad configuration
  1743. * @fmt: pointer to v4l2 subdev format structure
  1744. * return -EINVAL or zero on success
  1745. */
  1746. static int preview_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  1747. struct v4l2_subdev_format *fmt)
  1748. {
  1749. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1750. struct v4l2_mbus_framefmt *format;
  1751. format = __preview_get_format(prev, cfg, fmt->pad, fmt->which);
  1752. if (format == NULL)
  1753. return -EINVAL;
  1754. fmt->format = *format;
  1755. return 0;
  1756. }
  1757. /*
  1758. * preview_set_format - Handle set format by pads subdev method
  1759. * @sd : pointer to v4l2 subdev structure
  1760. * @cfg: V4L2 subdev pad configuration
  1761. * @fmt: pointer to v4l2 subdev format structure
  1762. * return -EINVAL or zero on success
  1763. */
  1764. static int preview_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  1765. struct v4l2_subdev_format *fmt)
  1766. {
  1767. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1768. struct v4l2_mbus_framefmt *format;
  1769. struct v4l2_rect *crop;
  1770. format = __preview_get_format(prev, cfg, fmt->pad, fmt->which);
  1771. if (format == NULL)
  1772. return -EINVAL;
  1773. preview_try_format(prev, cfg, fmt->pad, &fmt->format, fmt->which);
  1774. *format = fmt->format;
  1775. /* Propagate the format from sink to source */
  1776. if (fmt->pad == PREV_PAD_SINK) {
  1777. /* Reset the crop rectangle. */
  1778. crop = __preview_get_crop(prev, cfg, fmt->which);
  1779. crop->left = 0;
  1780. crop->top = 0;
  1781. crop->width = fmt->format.width;
  1782. crop->height = fmt->format.height;
  1783. preview_try_crop(prev, &fmt->format, crop);
  1784. /* Update the source format. */
  1785. format = __preview_get_format(prev, cfg, PREV_PAD_SOURCE,
  1786. fmt->which);
  1787. preview_try_format(prev, cfg, PREV_PAD_SOURCE, format,
  1788. fmt->which);
  1789. }
  1790. return 0;
  1791. }
  1792. /*
  1793. * preview_init_formats - Initialize formats on all pads
  1794. * @sd: ISP preview V4L2 subdevice
  1795. * @fh: V4L2 subdev file handle
  1796. *
  1797. * Initialize all pad formats with default values. If fh is not NULL, try
  1798. * formats are initialized on the file handle. Otherwise active formats are
  1799. * initialized on the device.
  1800. */
  1801. static int preview_init_formats(struct v4l2_subdev *sd,
  1802. struct v4l2_subdev_fh *fh)
  1803. {
  1804. struct v4l2_subdev_format format;
  1805. memset(&format, 0, sizeof(format));
  1806. format.pad = PREV_PAD_SINK;
  1807. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  1808. format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  1809. format.format.width = 4096;
  1810. format.format.height = 4096;
  1811. preview_set_format(sd, fh ? fh->pad : NULL, &format);
  1812. return 0;
  1813. }
  1814. /* subdev core operations */
  1815. static const struct v4l2_subdev_core_ops preview_v4l2_core_ops = {
  1816. .ioctl = preview_ioctl,
  1817. };
  1818. /* subdev video operations */
  1819. static const struct v4l2_subdev_video_ops preview_v4l2_video_ops = {
  1820. .s_stream = preview_set_stream,
  1821. };
  1822. /* subdev pad operations */
  1823. static const struct v4l2_subdev_pad_ops preview_v4l2_pad_ops = {
  1824. .enum_mbus_code = preview_enum_mbus_code,
  1825. .enum_frame_size = preview_enum_frame_size,
  1826. .get_fmt = preview_get_format,
  1827. .set_fmt = preview_set_format,
  1828. .get_selection = preview_get_selection,
  1829. .set_selection = preview_set_selection,
  1830. };
  1831. /* subdev operations */
  1832. static const struct v4l2_subdev_ops preview_v4l2_ops = {
  1833. .core = &preview_v4l2_core_ops,
  1834. .video = &preview_v4l2_video_ops,
  1835. .pad = &preview_v4l2_pad_ops,
  1836. };
  1837. /* subdev internal operations */
  1838. static const struct v4l2_subdev_internal_ops preview_v4l2_internal_ops = {
  1839. .open = preview_init_formats,
  1840. };
  1841. /* -----------------------------------------------------------------------------
  1842. * Media entity operations
  1843. */
  1844. /*
  1845. * preview_link_setup - Setup previewer connections.
  1846. * @entity : Pointer to media entity structure
  1847. * @local : Pointer to local pad array
  1848. * @remote : Pointer to remote pad array
  1849. * @flags : Link flags
  1850. * return -EINVAL or zero on success
  1851. */
  1852. static int preview_link_setup(struct media_entity *entity,
  1853. const struct media_pad *local,
  1854. const struct media_pad *remote, u32 flags)
  1855. {
  1856. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1857. struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
  1858. switch (local->index | media_entity_type(remote->entity)) {
  1859. case PREV_PAD_SINK | MEDIA_ENT_T_DEVNODE:
  1860. /* read from memory */
  1861. if (flags & MEDIA_LNK_FL_ENABLED) {
  1862. if (prev->input == PREVIEW_INPUT_CCDC)
  1863. return -EBUSY;
  1864. prev->input = PREVIEW_INPUT_MEMORY;
  1865. } else {
  1866. if (prev->input == PREVIEW_INPUT_MEMORY)
  1867. prev->input = PREVIEW_INPUT_NONE;
  1868. }
  1869. break;
  1870. case PREV_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
  1871. /* read from ccdc */
  1872. if (flags & MEDIA_LNK_FL_ENABLED) {
  1873. if (prev->input == PREVIEW_INPUT_MEMORY)
  1874. return -EBUSY;
  1875. prev->input = PREVIEW_INPUT_CCDC;
  1876. } else {
  1877. if (prev->input == PREVIEW_INPUT_CCDC)
  1878. prev->input = PREVIEW_INPUT_NONE;
  1879. }
  1880. break;
  1881. /*
  1882. * The ISP core doesn't support pipelines with multiple video outputs.
  1883. * Revisit this when it will be implemented, and return -EBUSY for now.
  1884. */
  1885. case PREV_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
  1886. /* write to memory */
  1887. if (flags & MEDIA_LNK_FL_ENABLED) {
  1888. if (prev->output & ~PREVIEW_OUTPUT_MEMORY)
  1889. return -EBUSY;
  1890. prev->output |= PREVIEW_OUTPUT_MEMORY;
  1891. } else {
  1892. prev->output &= ~PREVIEW_OUTPUT_MEMORY;
  1893. }
  1894. break;
  1895. case PREV_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
  1896. /* write to resizer */
  1897. if (flags & MEDIA_LNK_FL_ENABLED) {
  1898. if (prev->output & ~PREVIEW_OUTPUT_RESIZER)
  1899. return -EBUSY;
  1900. prev->output |= PREVIEW_OUTPUT_RESIZER;
  1901. } else {
  1902. prev->output &= ~PREVIEW_OUTPUT_RESIZER;
  1903. }
  1904. break;
  1905. default:
  1906. return -EINVAL;
  1907. }
  1908. return 0;
  1909. }
  1910. /* media operations */
  1911. static const struct media_entity_operations preview_media_ops = {
  1912. .link_setup = preview_link_setup,
  1913. .link_validate = v4l2_subdev_link_validate,
  1914. };
  1915. void omap3isp_preview_unregister_entities(struct isp_prev_device *prev)
  1916. {
  1917. v4l2_device_unregister_subdev(&prev->subdev);
  1918. omap3isp_video_unregister(&prev->video_in);
  1919. omap3isp_video_unregister(&prev->video_out);
  1920. }
  1921. int omap3isp_preview_register_entities(struct isp_prev_device *prev,
  1922. struct v4l2_device *vdev)
  1923. {
  1924. int ret;
  1925. /* Register the subdev and video nodes. */
  1926. ret = v4l2_device_register_subdev(vdev, &prev->subdev);
  1927. if (ret < 0)
  1928. goto error;
  1929. ret = omap3isp_video_register(&prev->video_in, vdev);
  1930. if (ret < 0)
  1931. goto error;
  1932. ret = omap3isp_video_register(&prev->video_out, vdev);
  1933. if (ret < 0)
  1934. goto error;
  1935. return 0;
  1936. error:
  1937. omap3isp_preview_unregister_entities(prev);
  1938. return ret;
  1939. }
  1940. /* -----------------------------------------------------------------------------
  1941. * ISP previewer initialisation and cleanup
  1942. */
  1943. /*
  1944. * preview_init_entities - Initialize subdev and media entity.
  1945. * @prev : Pointer to preview structure
  1946. * return -ENOMEM or zero on success
  1947. */
  1948. static int preview_init_entities(struct isp_prev_device *prev)
  1949. {
  1950. struct v4l2_subdev *sd = &prev->subdev;
  1951. struct media_pad *pads = prev->pads;
  1952. struct media_entity *me = &sd->entity;
  1953. int ret;
  1954. prev->input = PREVIEW_INPUT_NONE;
  1955. v4l2_subdev_init(sd, &preview_v4l2_ops);
  1956. sd->internal_ops = &preview_v4l2_internal_ops;
  1957. strlcpy(sd->name, "OMAP3 ISP preview", sizeof(sd->name));
  1958. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  1959. v4l2_set_subdevdata(sd, prev);
  1960. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1961. v4l2_ctrl_handler_init(&prev->ctrls, 2);
  1962. v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_BRIGHTNESS,
  1963. ISPPRV_BRIGHT_LOW, ISPPRV_BRIGHT_HIGH,
  1964. ISPPRV_BRIGHT_STEP, ISPPRV_BRIGHT_DEF);
  1965. v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_CONTRAST,
  1966. ISPPRV_CONTRAST_LOW, ISPPRV_CONTRAST_HIGH,
  1967. ISPPRV_CONTRAST_STEP, ISPPRV_CONTRAST_DEF);
  1968. v4l2_ctrl_handler_setup(&prev->ctrls);
  1969. sd->ctrl_handler = &prev->ctrls;
  1970. pads[PREV_PAD_SINK].flags = MEDIA_PAD_FL_SINK
  1971. | MEDIA_PAD_FL_MUST_CONNECT;
  1972. pads[PREV_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1973. me->ops = &preview_media_ops;
  1974. ret = media_entity_init(me, PREV_PADS_NUM, pads, 0);
  1975. if (ret < 0)
  1976. return ret;
  1977. preview_init_formats(sd, NULL);
  1978. /* According to the OMAP34xx TRM, video buffers need to be aligned on a
  1979. * 32 bytes boundary. However, an undocumented hardware bug requires a
  1980. * 64 bytes boundary at the preview engine input.
  1981. */
  1982. prev->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1983. prev->video_in.ops = &preview_video_ops;
  1984. prev->video_in.isp = to_isp_device(prev);
  1985. prev->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1986. prev->video_in.bpl_alignment = 64;
  1987. prev->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1988. prev->video_out.ops = &preview_video_ops;
  1989. prev->video_out.isp = to_isp_device(prev);
  1990. prev->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1991. prev->video_out.bpl_alignment = 32;
  1992. ret = omap3isp_video_init(&prev->video_in, "preview");
  1993. if (ret < 0)
  1994. goto error_video_in;
  1995. ret = omap3isp_video_init(&prev->video_out, "preview");
  1996. if (ret < 0)
  1997. goto error_video_out;
  1998. /* Connect the video nodes to the previewer subdev. */
  1999. ret = media_entity_create_link(&prev->video_in.video.entity, 0,
  2000. &prev->subdev.entity, PREV_PAD_SINK, 0);
  2001. if (ret < 0)
  2002. goto error_link;
  2003. ret = media_entity_create_link(&prev->subdev.entity, PREV_PAD_SOURCE,
  2004. &prev->video_out.video.entity, 0, 0);
  2005. if (ret < 0)
  2006. goto error_link;
  2007. return 0;
  2008. error_link:
  2009. omap3isp_video_cleanup(&prev->video_out);
  2010. error_video_out:
  2011. omap3isp_video_cleanup(&prev->video_in);
  2012. error_video_in:
  2013. media_entity_cleanup(&prev->subdev.entity);
  2014. return ret;
  2015. }
  2016. /*
  2017. * omap3isp_preview_init - Previewer initialization.
  2018. * @isp : Pointer to ISP device
  2019. * return -ENOMEM or zero on success
  2020. */
  2021. int omap3isp_preview_init(struct isp_device *isp)
  2022. {
  2023. struct isp_prev_device *prev = &isp->isp_prev;
  2024. init_waitqueue_head(&prev->wait);
  2025. preview_init_params(prev);
  2026. return preview_init_entities(prev);
  2027. }
  2028. void omap3isp_preview_cleanup(struct isp_device *isp)
  2029. {
  2030. struct isp_prev_device *prev = &isp->isp_prev;
  2031. v4l2_ctrl_handler_free(&prev->ctrls);
  2032. omap3isp_video_cleanup(&prev->video_in);
  2033. omap3isp_video_cleanup(&prev->video_out);
  2034. media_entity_cleanup(&prev->subdev.entity);
  2035. }