isppreview.c 69 KB

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