isppreview.c 68 KB

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