isppreview.c 69 KB

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