isppreview.c 69 KB

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