isppreview.c 69 KB

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