isppreview.c 69 KB

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