ispccdc.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691
  1. /*
  2. * ispccdc.c
  3. *
  4. * TI OMAP3 ISP - CCDC module
  5. *
  6. * Copyright (C) 2009-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. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/mm.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <media/v4l2-event.h>
  25. #include "isp.h"
  26. #include "ispreg.h"
  27. #include "ispccdc.h"
  28. #define CCDC_MIN_WIDTH 32
  29. #define CCDC_MIN_HEIGHT 32
  30. static struct v4l2_mbus_framefmt *
  31. __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
  32. unsigned int pad, enum v4l2_subdev_format_whence which);
  33. static const unsigned int ccdc_fmts[] = {
  34. V4L2_MBUS_FMT_Y8_1X8,
  35. V4L2_MBUS_FMT_Y10_1X10,
  36. V4L2_MBUS_FMT_Y12_1X12,
  37. V4L2_MBUS_FMT_SGRBG8_1X8,
  38. V4L2_MBUS_FMT_SRGGB8_1X8,
  39. V4L2_MBUS_FMT_SBGGR8_1X8,
  40. V4L2_MBUS_FMT_SGBRG8_1X8,
  41. V4L2_MBUS_FMT_SGRBG10_1X10,
  42. V4L2_MBUS_FMT_SRGGB10_1X10,
  43. V4L2_MBUS_FMT_SBGGR10_1X10,
  44. V4L2_MBUS_FMT_SGBRG10_1X10,
  45. V4L2_MBUS_FMT_SGRBG12_1X12,
  46. V4L2_MBUS_FMT_SRGGB12_1X12,
  47. V4L2_MBUS_FMT_SBGGR12_1X12,
  48. V4L2_MBUS_FMT_SGBRG12_1X12,
  49. V4L2_MBUS_FMT_YUYV8_2X8,
  50. V4L2_MBUS_FMT_UYVY8_2X8,
  51. };
  52. /*
  53. * ccdc_print_status - Print current CCDC Module register values.
  54. * @ccdc: Pointer to ISP CCDC device.
  55. *
  56. * Also prints other debug information stored in the CCDC module.
  57. */
  58. #define CCDC_PRINT_REGISTER(isp, name)\
  59. dev_dbg(isp->dev, "###CCDC " #name "=0x%08x\n", \
  60. isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_##name))
  61. static void ccdc_print_status(struct isp_ccdc_device *ccdc)
  62. {
  63. struct isp_device *isp = to_isp_device(ccdc);
  64. dev_dbg(isp->dev, "-------------CCDC Register dump-------------\n");
  65. CCDC_PRINT_REGISTER(isp, PCR);
  66. CCDC_PRINT_REGISTER(isp, SYN_MODE);
  67. CCDC_PRINT_REGISTER(isp, HD_VD_WID);
  68. CCDC_PRINT_REGISTER(isp, PIX_LINES);
  69. CCDC_PRINT_REGISTER(isp, HORZ_INFO);
  70. CCDC_PRINT_REGISTER(isp, VERT_START);
  71. CCDC_PRINT_REGISTER(isp, VERT_LINES);
  72. CCDC_PRINT_REGISTER(isp, CULLING);
  73. CCDC_PRINT_REGISTER(isp, HSIZE_OFF);
  74. CCDC_PRINT_REGISTER(isp, SDOFST);
  75. CCDC_PRINT_REGISTER(isp, SDR_ADDR);
  76. CCDC_PRINT_REGISTER(isp, CLAMP);
  77. CCDC_PRINT_REGISTER(isp, DCSUB);
  78. CCDC_PRINT_REGISTER(isp, COLPTN);
  79. CCDC_PRINT_REGISTER(isp, BLKCMP);
  80. CCDC_PRINT_REGISTER(isp, FPC);
  81. CCDC_PRINT_REGISTER(isp, FPC_ADDR);
  82. CCDC_PRINT_REGISTER(isp, VDINT);
  83. CCDC_PRINT_REGISTER(isp, ALAW);
  84. CCDC_PRINT_REGISTER(isp, REC656IF);
  85. CCDC_PRINT_REGISTER(isp, CFG);
  86. CCDC_PRINT_REGISTER(isp, FMTCFG);
  87. CCDC_PRINT_REGISTER(isp, FMT_HORZ);
  88. CCDC_PRINT_REGISTER(isp, FMT_VERT);
  89. CCDC_PRINT_REGISTER(isp, PRGEVEN0);
  90. CCDC_PRINT_REGISTER(isp, PRGEVEN1);
  91. CCDC_PRINT_REGISTER(isp, PRGODD0);
  92. CCDC_PRINT_REGISTER(isp, PRGODD1);
  93. CCDC_PRINT_REGISTER(isp, VP_OUT);
  94. CCDC_PRINT_REGISTER(isp, LSC_CONFIG);
  95. CCDC_PRINT_REGISTER(isp, LSC_INITIAL);
  96. CCDC_PRINT_REGISTER(isp, LSC_TABLE_BASE);
  97. CCDC_PRINT_REGISTER(isp, LSC_TABLE_OFFSET);
  98. dev_dbg(isp->dev, "--------------------------------------------\n");
  99. }
  100. /*
  101. * omap3isp_ccdc_busy - Get busy state of the CCDC.
  102. * @ccdc: Pointer to ISP CCDC device.
  103. */
  104. int omap3isp_ccdc_busy(struct isp_ccdc_device *ccdc)
  105. {
  106. struct isp_device *isp = to_isp_device(ccdc);
  107. return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR) &
  108. ISPCCDC_PCR_BUSY;
  109. }
  110. /* -----------------------------------------------------------------------------
  111. * Lens Shading Compensation
  112. */
  113. /*
  114. * ccdc_lsc_validate_config - Check that LSC configuration is valid.
  115. * @ccdc: Pointer to ISP CCDC device.
  116. * @lsc_cfg: the LSC configuration to check.
  117. *
  118. * Returns 0 if the LSC configuration is valid, or -EINVAL if invalid.
  119. */
  120. static int ccdc_lsc_validate_config(struct isp_ccdc_device *ccdc,
  121. struct omap3isp_ccdc_lsc_config *lsc_cfg)
  122. {
  123. struct isp_device *isp = to_isp_device(ccdc);
  124. struct v4l2_mbus_framefmt *format;
  125. unsigned int paxel_width, paxel_height;
  126. unsigned int paxel_shift_x, paxel_shift_y;
  127. unsigned int min_width, min_height, min_size;
  128. unsigned int input_width, input_height;
  129. paxel_shift_x = lsc_cfg->gain_mode_m;
  130. paxel_shift_y = lsc_cfg->gain_mode_n;
  131. if ((paxel_shift_x < 2) || (paxel_shift_x > 6) ||
  132. (paxel_shift_y < 2) || (paxel_shift_y > 6)) {
  133. dev_dbg(isp->dev, "CCDC: LSC: Invalid paxel size\n");
  134. return -EINVAL;
  135. }
  136. if (lsc_cfg->offset & 3) {
  137. dev_dbg(isp->dev, "CCDC: LSC: Offset must be a multiple of "
  138. "4\n");
  139. return -EINVAL;
  140. }
  141. if ((lsc_cfg->initial_x & 1) || (lsc_cfg->initial_y & 1)) {
  142. dev_dbg(isp->dev, "CCDC: LSC: initial_x and y must be even\n");
  143. return -EINVAL;
  144. }
  145. format = __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
  146. V4L2_SUBDEV_FORMAT_ACTIVE);
  147. input_width = format->width;
  148. input_height = format->height;
  149. /* Calculate minimum bytesize for validation */
  150. paxel_width = 1 << paxel_shift_x;
  151. min_width = ((input_width + lsc_cfg->initial_x + paxel_width - 1)
  152. >> paxel_shift_x) + 1;
  153. paxel_height = 1 << paxel_shift_y;
  154. min_height = ((input_height + lsc_cfg->initial_y + paxel_height - 1)
  155. >> paxel_shift_y) + 1;
  156. min_size = 4 * min_width * min_height;
  157. if (min_size > lsc_cfg->size) {
  158. dev_dbg(isp->dev, "CCDC: LSC: too small table\n");
  159. return -EINVAL;
  160. }
  161. if (lsc_cfg->offset < (min_width * 4)) {
  162. dev_dbg(isp->dev, "CCDC: LSC: Offset is too small\n");
  163. return -EINVAL;
  164. }
  165. if ((lsc_cfg->size / lsc_cfg->offset) < min_height) {
  166. dev_dbg(isp->dev, "CCDC: LSC: Wrong size/offset combination\n");
  167. return -EINVAL;
  168. }
  169. return 0;
  170. }
  171. /*
  172. * ccdc_lsc_program_table - Program Lens Shading Compensation table address.
  173. * @ccdc: Pointer to ISP CCDC device.
  174. */
  175. static void ccdc_lsc_program_table(struct isp_ccdc_device *ccdc,
  176. dma_addr_t addr)
  177. {
  178. isp_reg_writel(to_isp_device(ccdc), addr,
  179. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_TABLE_BASE);
  180. }
  181. /*
  182. * ccdc_lsc_setup_regs - Configures the lens shading compensation module
  183. * @ccdc: Pointer to ISP CCDC device.
  184. */
  185. static void ccdc_lsc_setup_regs(struct isp_ccdc_device *ccdc,
  186. struct omap3isp_ccdc_lsc_config *cfg)
  187. {
  188. struct isp_device *isp = to_isp_device(ccdc);
  189. int reg;
  190. isp_reg_writel(isp, cfg->offset, OMAP3_ISP_IOMEM_CCDC,
  191. ISPCCDC_LSC_TABLE_OFFSET);
  192. reg = 0;
  193. reg |= cfg->gain_mode_n << ISPCCDC_LSC_GAIN_MODE_N_SHIFT;
  194. reg |= cfg->gain_mode_m << ISPCCDC_LSC_GAIN_MODE_M_SHIFT;
  195. reg |= cfg->gain_format << ISPCCDC_LSC_GAIN_FORMAT_SHIFT;
  196. isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG);
  197. reg = 0;
  198. reg &= ~ISPCCDC_LSC_INITIAL_X_MASK;
  199. reg |= cfg->initial_x << ISPCCDC_LSC_INITIAL_X_SHIFT;
  200. reg &= ~ISPCCDC_LSC_INITIAL_Y_MASK;
  201. reg |= cfg->initial_y << ISPCCDC_LSC_INITIAL_Y_SHIFT;
  202. isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC,
  203. ISPCCDC_LSC_INITIAL);
  204. }
  205. static int ccdc_lsc_wait_prefetch(struct isp_ccdc_device *ccdc)
  206. {
  207. struct isp_device *isp = to_isp_device(ccdc);
  208. unsigned int wait;
  209. isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
  210. OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
  211. /* timeout 1 ms */
  212. for (wait = 0; wait < 1000; wait++) {
  213. if (isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS) &
  214. IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ) {
  215. isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
  216. OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
  217. return 0;
  218. }
  219. rmb();
  220. udelay(1);
  221. }
  222. return -ETIMEDOUT;
  223. }
  224. /*
  225. * __ccdc_lsc_enable - Enables/Disables the Lens Shading Compensation module.
  226. * @ccdc: Pointer to ISP CCDC device.
  227. * @enable: 0 Disables LSC, 1 Enables LSC.
  228. */
  229. static int __ccdc_lsc_enable(struct isp_ccdc_device *ccdc, int enable)
  230. {
  231. struct isp_device *isp = to_isp_device(ccdc);
  232. const struct v4l2_mbus_framefmt *format =
  233. __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
  234. V4L2_SUBDEV_FORMAT_ACTIVE);
  235. if ((format->code != V4L2_MBUS_FMT_SGRBG10_1X10) &&
  236. (format->code != V4L2_MBUS_FMT_SRGGB10_1X10) &&
  237. (format->code != V4L2_MBUS_FMT_SBGGR10_1X10) &&
  238. (format->code != V4L2_MBUS_FMT_SGBRG10_1X10))
  239. return -EINVAL;
  240. if (enable)
  241. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_LSC_READ);
  242. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
  243. ISPCCDC_LSC_ENABLE, enable ? ISPCCDC_LSC_ENABLE : 0);
  244. if (enable) {
  245. if (ccdc_lsc_wait_prefetch(ccdc) < 0) {
  246. isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC,
  247. ISPCCDC_LSC_CONFIG, ISPCCDC_LSC_ENABLE);
  248. ccdc->lsc.state = LSC_STATE_STOPPED;
  249. dev_warn(to_device(ccdc), "LSC prefetch timeout\n");
  250. return -ETIMEDOUT;
  251. }
  252. ccdc->lsc.state = LSC_STATE_RUNNING;
  253. } else {
  254. ccdc->lsc.state = LSC_STATE_STOPPING;
  255. }
  256. return 0;
  257. }
  258. static int ccdc_lsc_busy(struct isp_ccdc_device *ccdc)
  259. {
  260. struct isp_device *isp = to_isp_device(ccdc);
  261. return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG) &
  262. ISPCCDC_LSC_BUSY;
  263. }
  264. /* __ccdc_lsc_configure - Apply a new configuration to the LSC engine
  265. * @ccdc: Pointer to ISP CCDC device
  266. * @req: New configuration request
  267. *
  268. * context: in_interrupt()
  269. */
  270. static int __ccdc_lsc_configure(struct isp_ccdc_device *ccdc,
  271. struct ispccdc_lsc_config_req *req)
  272. {
  273. if (!req->enable)
  274. return -EINVAL;
  275. if (ccdc_lsc_validate_config(ccdc, &req->config) < 0) {
  276. dev_dbg(to_device(ccdc), "Discard LSC configuration\n");
  277. return -EINVAL;
  278. }
  279. if (ccdc_lsc_busy(ccdc))
  280. return -EBUSY;
  281. ccdc_lsc_setup_regs(ccdc, &req->config);
  282. ccdc_lsc_program_table(ccdc, req->table.dma);
  283. return 0;
  284. }
  285. /*
  286. * ccdc_lsc_error_handler - Handle LSC prefetch error scenario.
  287. * @ccdc: Pointer to ISP CCDC device.
  288. *
  289. * Disables LSC, and defers enablement to shadow registers update time.
  290. */
  291. static void ccdc_lsc_error_handler(struct isp_ccdc_device *ccdc)
  292. {
  293. struct isp_device *isp = to_isp_device(ccdc);
  294. /*
  295. * From OMAP3 TRM: When this event is pending, the module
  296. * goes into transparent mode (output =input). Normal
  297. * operation can be resumed at the start of the next frame
  298. * after:
  299. * 1) Clearing this event
  300. * 2) Disabling the LSC module
  301. * 3) Enabling it
  302. */
  303. isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
  304. ISPCCDC_LSC_ENABLE);
  305. ccdc->lsc.state = LSC_STATE_STOPPED;
  306. }
  307. static void ccdc_lsc_free_request(struct isp_ccdc_device *ccdc,
  308. struct ispccdc_lsc_config_req *req)
  309. {
  310. struct isp_device *isp = to_isp_device(ccdc);
  311. if (req == NULL)
  312. return;
  313. if (req->table.addr) {
  314. sg_free_table(&req->table.sgt);
  315. dma_free_coherent(isp->dev, req->config.size, req->table.addr,
  316. req->table.dma);
  317. }
  318. kfree(req);
  319. }
  320. static void ccdc_lsc_free_queue(struct isp_ccdc_device *ccdc,
  321. struct list_head *queue)
  322. {
  323. struct ispccdc_lsc_config_req *req, *n;
  324. unsigned long flags;
  325. spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
  326. list_for_each_entry_safe(req, n, queue, list) {
  327. list_del(&req->list);
  328. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  329. ccdc_lsc_free_request(ccdc, req);
  330. spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
  331. }
  332. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  333. }
  334. static void ccdc_lsc_free_table_work(struct work_struct *work)
  335. {
  336. struct isp_ccdc_device *ccdc;
  337. struct ispccdc_lsc *lsc;
  338. lsc = container_of(work, struct ispccdc_lsc, table_work);
  339. ccdc = container_of(lsc, struct isp_ccdc_device, lsc);
  340. ccdc_lsc_free_queue(ccdc, &lsc->free_queue);
  341. }
  342. /*
  343. * ccdc_lsc_config - Configure the LSC module from a userspace request
  344. *
  345. * Store the request LSC configuration in the LSC engine request pointer. The
  346. * configuration will be applied to the hardware when the CCDC will be enabled,
  347. * or at the next LSC interrupt if the CCDC is already running.
  348. */
  349. static int ccdc_lsc_config(struct isp_ccdc_device *ccdc,
  350. struct omap3isp_ccdc_update_config *config)
  351. {
  352. struct isp_device *isp = to_isp_device(ccdc);
  353. struct ispccdc_lsc_config_req *req;
  354. unsigned long flags;
  355. u16 update;
  356. int ret;
  357. update = config->update &
  358. (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC);
  359. if (!update)
  360. return 0;
  361. if (update != (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC)) {
  362. dev_dbg(to_device(ccdc), "%s: Both LSC configuration and table "
  363. "need to be supplied\n", __func__);
  364. return -EINVAL;
  365. }
  366. req = kzalloc(sizeof(*req), GFP_KERNEL);
  367. if (req == NULL)
  368. return -ENOMEM;
  369. if (config->flag & OMAP3ISP_CCDC_CONFIG_LSC) {
  370. if (copy_from_user(&req->config, config->lsc_cfg,
  371. sizeof(req->config))) {
  372. ret = -EFAULT;
  373. goto done;
  374. }
  375. req->enable = 1;
  376. req->table.addr = dma_alloc_coherent(isp->dev, req->config.size,
  377. &req->table.dma,
  378. GFP_KERNEL);
  379. if (req->table.addr == NULL) {
  380. ret = -ENOMEM;
  381. goto done;
  382. }
  383. ret = dma_get_sgtable(isp->dev, &req->table.sgt,
  384. req->table.addr, req->table.dma,
  385. req->config.size);
  386. if (ret < 0)
  387. goto done;
  388. dma_sync_sg_for_cpu(isp->dev, req->table.sgt.sgl,
  389. req->table.sgt.nents, DMA_TO_DEVICE);
  390. if (copy_from_user(req->table.addr, config->lsc,
  391. req->config.size)) {
  392. ret = -EFAULT;
  393. goto done;
  394. }
  395. dma_sync_sg_for_device(isp->dev, req->table.sgt.sgl,
  396. req->table.sgt.nents, DMA_TO_DEVICE);
  397. }
  398. spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
  399. if (ccdc->lsc.request) {
  400. list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
  401. schedule_work(&ccdc->lsc.table_work);
  402. }
  403. ccdc->lsc.request = req;
  404. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  405. ret = 0;
  406. done:
  407. if (ret < 0)
  408. ccdc_lsc_free_request(ccdc, req);
  409. return ret;
  410. }
  411. static inline int ccdc_lsc_is_configured(struct isp_ccdc_device *ccdc)
  412. {
  413. unsigned long flags;
  414. spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
  415. if (ccdc->lsc.active) {
  416. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  417. return 1;
  418. }
  419. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  420. return 0;
  421. }
  422. static int ccdc_lsc_enable(struct isp_ccdc_device *ccdc)
  423. {
  424. struct ispccdc_lsc *lsc = &ccdc->lsc;
  425. if (lsc->state != LSC_STATE_STOPPED)
  426. return -EINVAL;
  427. if (lsc->active) {
  428. list_add_tail(&lsc->active->list, &lsc->free_queue);
  429. lsc->active = NULL;
  430. }
  431. if (__ccdc_lsc_configure(ccdc, lsc->request) < 0) {
  432. omap3isp_sbl_disable(to_isp_device(ccdc),
  433. OMAP3_ISP_SBL_CCDC_LSC_READ);
  434. list_add_tail(&lsc->request->list, &lsc->free_queue);
  435. lsc->request = NULL;
  436. goto done;
  437. }
  438. lsc->active = lsc->request;
  439. lsc->request = NULL;
  440. __ccdc_lsc_enable(ccdc, 1);
  441. done:
  442. if (!list_empty(&lsc->free_queue))
  443. schedule_work(&lsc->table_work);
  444. return 0;
  445. }
  446. /* -----------------------------------------------------------------------------
  447. * Parameters configuration
  448. */
  449. /*
  450. * ccdc_configure_clamp - Configure optical-black or digital clamping
  451. * @ccdc: Pointer to ISP CCDC device.
  452. *
  453. * The CCDC performs either optical-black or digital clamp. Configure and enable
  454. * the selected clamp method.
  455. */
  456. static void ccdc_configure_clamp(struct isp_ccdc_device *ccdc)
  457. {
  458. struct isp_device *isp = to_isp_device(ccdc);
  459. u32 clamp;
  460. if (ccdc->obclamp) {
  461. clamp = ccdc->clamp.obgain << ISPCCDC_CLAMP_OBGAIN_SHIFT;
  462. clamp |= ccdc->clamp.oblen << ISPCCDC_CLAMP_OBSLEN_SHIFT;
  463. clamp |= ccdc->clamp.oblines << ISPCCDC_CLAMP_OBSLN_SHIFT;
  464. clamp |= ccdc->clamp.obstpixel << ISPCCDC_CLAMP_OBST_SHIFT;
  465. isp_reg_writel(isp, clamp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP);
  466. } else {
  467. isp_reg_writel(isp, ccdc->clamp.dcsubval,
  468. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_DCSUB);
  469. }
  470. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP,
  471. ISPCCDC_CLAMP_CLAMPEN,
  472. ccdc->obclamp ? ISPCCDC_CLAMP_CLAMPEN : 0);
  473. }
  474. /*
  475. * ccdc_configure_fpc - Configure Faulty Pixel Correction
  476. * @ccdc: Pointer to ISP CCDC device.
  477. */
  478. static void ccdc_configure_fpc(struct isp_ccdc_device *ccdc)
  479. {
  480. struct isp_device *isp = to_isp_device(ccdc);
  481. isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC, ISPCCDC_FPC_FPCEN);
  482. if (!ccdc->fpc_en)
  483. return;
  484. isp_reg_writel(isp, ccdc->fpc.dma, OMAP3_ISP_IOMEM_CCDC,
  485. ISPCCDC_FPC_ADDR);
  486. /* The FPNUM field must be set before enabling FPC. */
  487. isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT),
  488. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
  489. isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT) |
  490. ISPCCDC_FPC_FPCEN, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
  491. }
  492. /*
  493. * ccdc_configure_black_comp - Configure Black Level Compensation.
  494. * @ccdc: Pointer to ISP CCDC device.
  495. */
  496. static void ccdc_configure_black_comp(struct isp_ccdc_device *ccdc)
  497. {
  498. struct isp_device *isp = to_isp_device(ccdc);
  499. u32 blcomp;
  500. blcomp = ccdc->blcomp.b_mg << ISPCCDC_BLKCMP_B_MG_SHIFT;
  501. blcomp |= ccdc->blcomp.gb_g << ISPCCDC_BLKCMP_GB_G_SHIFT;
  502. blcomp |= ccdc->blcomp.gr_cy << ISPCCDC_BLKCMP_GR_CY_SHIFT;
  503. blcomp |= ccdc->blcomp.r_ye << ISPCCDC_BLKCMP_R_YE_SHIFT;
  504. isp_reg_writel(isp, blcomp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_BLKCMP);
  505. }
  506. /*
  507. * ccdc_configure_lpf - Configure Low-Pass Filter (LPF).
  508. * @ccdc: Pointer to ISP CCDC device.
  509. */
  510. static void ccdc_configure_lpf(struct isp_ccdc_device *ccdc)
  511. {
  512. struct isp_device *isp = to_isp_device(ccdc);
  513. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE,
  514. ISPCCDC_SYN_MODE_LPF,
  515. ccdc->lpf ? ISPCCDC_SYN_MODE_LPF : 0);
  516. }
  517. /*
  518. * ccdc_configure_alaw - Configure A-law compression.
  519. * @ccdc: Pointer to ISP CCDC device.
  520. */
  521. static void ccdc_configure_alaw(struct isp_ccdc_device *ccdc)
  522. {
  523. struct isp_device *isp = to_isp_device(ccdc);
  524. const struct isp_format_info *info;
  525. u32 alaw = 0;
  526. info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
  527. switch (info->width) {
  528. case 8:
  529. return;
  530. case 10:
  531. alaw = ISPCCDC_ALAW_GWDI_9_0;
  532. break;
  533. case 11:
  534. alaw = ISPCCDC_ALAW_GWDI_10_1;
  535. break;
  536. case 12:
  537. alaw = ISPCCDC_ALAW_GWDI_11_2;
  538. break;
  539. case 13:
  540. alaw = ISPCCDC_ALAW_GWDI_12_3;
  541. break;
  542. }
  543. if (ccdc->alaw)
  544. alaw |= ISPCCDC_ALAW_CCDTBL;
  545. isp_reg_writel(isp, alaw, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_ALAW);
  546. }
  547. /*
  548. * ccdc_config_imgattr - Configure sensor image specific attributes.
  549. * @ccdc: Pointer to ISP CCDC device.
  550. * @colptn: Color pattern of the sensor.
  551. */
  552. static void ccdc_config_imgattr(struct isp_ccdc_device *ccdc, u32 colptn)
  553. {
  554. struct isp_device *isp = to_isp_device(ccdc);
  555. isp_reg_writel(isp, colptn, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_COLPTN);
  556. }
  557. /*
  558. * ccdc_config - Set CCDC configuration from userspace
  559. * @ccdc: Pointer to ISP CCDC device.
  560. * @ccdc_struct: Structure containing CCDC configuration sent from userspace.
  561. *
  562. * Returns 0 if successful, -EINVAL if the pointer to the configuration
  563. * structure is null, or the copy_from_user function fails to copy user space
  564. * memory to kernel space memory.
  565. */
  566. static int ccdc_config(struct isp_ccdc_device *ccdc,
  567. struct omap3isp_ccdc_update_config *ccdc_struct)
  568. {
  569. struct isp_device *isp = to_isp_device(ccdc);
  570. unsigned long flags;
  571. spin_lock_irqsave(&ccdc->lock, flags);
  572. ccdc->shadow_update = 1;
  573. spin_unlock_irqrestore(&ccdc->lock, flags);
  574. if (OMAP3ISP_CCDC_ALAW & ccdc_struct->update) {
  575. ccdc->alaw = !!(OMAP3ISP_CCDC_ALAW & ccdc_struct->flag);
  576. ccdc->update |= OMAP3ISP_CCDC_ALAW;
  577. }
  578. if (OMAP3ISP_CCDC_LPF & ccdc_struct->update) {
  579. ccdc->lpf = !!(OMAP3ISP_CCDC_LPF & ccdc_struct->flag);
  580. ccdc->update |= OMAP3ISP_CCDC_LPF;
  581. }
  582. if (OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->update) {
  583. if (copy_from_user(&ccdc->clamp, ccdc_struct->bclamp,
  584. sizeof(ccdc->clamp))) {
  585. ccdc->shadow_update = 0;
  586. return -EFAULT;
  587. }
  588. ccdc->obclamp = !!(OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->flag);
  589. ccdc->update |= OMAP3ISP_CCDC_BLCLAMP;
  590. }
  591. if (OMAP3ISP_CCDC_BCOMP & ccdc_struct->update) {
  592. if (copy_from_user(&ccdc->blcomp, ccdc_struct->blcomp,
  593. sizeof(ccdc->blcomp))) {
  594. ccdc->shadow_update = 0;
  595. return -EFAULT;
  596. }
  597. ccdc->update |= OMAP3ISP_CCDC_BCOMP;
  598. }
  599. ccdc->shadow_update = 0;
  600. if (OMAP3ISP_CCDC_FPC & ccdc_struct->update) {
  601. struct omap3isp_ccdc_fpc fpc;
  602. struct ispccdc_fpc fpc_old = { .addr = NULL, };
  603. struct ispccdc_fpc fpc_new;
  604. u32 size;
  605. if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
  606. return -EBUSY;
  607. ccdc->fpc_en = !!(OMAP3ISP_CCDC_FPC & ccdc_struct->flag);
  608. if (ccdc->fpc_en) {
  609. if (copy_from_user(&fpc, ccdc_struct->fpc, sizeof(fpc)))
  610. return -EFAULT;
  611. size = fpc.fpnum * 4;
  612. /*
  613. * The table address must be 64-bytes aligned, which is
  614. * guaranteed by dma_alloc_coherent().
  615. */
  616. fpc_new.fpnum = fpc.fpnum;
  617. fpc_new.addr = dma_alloc_coherent(isp->dev, size,
  618. &fpc_new.dma,
  619. GFP_KERNEL);
  620. if (fpc_new.addr == NULL)
  621. return -ENOMEM;
  622. if (copy_from_user(fpc_new.addr,
  623. (__force void __user *)fpc.fpcaddr,
  624. size)) {
  625. dma_free_coherent(isp->dev, size, fpc_new.addr,
  626. fpc_new.dma);
  627. return -EFAULT;
  628. }
  629. fpc_old = ccdc->fpc;
  630. ccdc->fpc = fpc_new;
  631. }
  632. ccdc_configure_fpc(ccdc);
  633. if (fpc_old.addr != NULL)
  634. dma_free_coherent(isp->dev, fpc_old.fpnum * 4,
  635. fpc_old.addr, fpc_old.dma);
  636. }
  637. return ccdc_lsc_config(ccdc, ccdc_struct);
  638. }
  639. static void ccdc_apply_controls(struct isp_ccdc_device *ccdc)
  640. {
  641. if (ccdc->update & OMAP3ISP_CCDC_ALAW) {
  642. ccdc_configure_alaw(ccdc);
  643. ccdc->update &= ~OMAP3ISP_CCDC_ALAW;
  644. }
  645. if (ccdc->update & OMAP3ISP_CCDC_LPF) {
  646. ccdc_configure_lpf(ccdc);
  647. ccdc->update &= ~OMAP3ISP_CCDC_LPF;
  648. }
  649. if (ccdc->update & OMAP3ISP_CCDC_BLCLAMP) {
  650. ccdc_configure_clamp(ccdc);
  651. ccdc->update &= ~OMAP3ISP_CCDC_BLCLAMP;
  652. }
  653. if (ccdc->update & OMAP3ISP_CCDC_BCOMP) {
  654. ccdc_configure_black_comp(ccdc);
  655. ccdc->update &= ~OMAP3ISP_CCDC_BCOMP;
  656. }
  657. }
  658. /*
  659. * omap3isp_ccdc_restore_context - Restore values of the CCDC module registers
  660. * @isp: Pointer to ISP device
  661. */
  662. void omap3isp_ccdc_restore_context(struct isp_device *isp)
  663. {
  664. struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
  665. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG, ISPCCDC_CFG_VDLC);
  666. ccdc->update = OMAP3ISP_CCDC_ALAW | OMAP3ISP_CCDC_LPF
  667. | OMAP3ISP_CCDC_BLCLAMP | OMAP3ISP_CCDC_BCOMP;
  668. ccdc_apply_controls(ccdc);
  669. ccdc_configure_fpc(ccdc);
  670. }
  671. /* -----------------------------------------------------------------------------
  672. * Format- and pipeline-related configuration helpers
  673. */
  674. /*
  675. * ccdc_config_vp - Configure the Video Port.
  676. * @ccdc: Pointer to ISP CCDC device.
  677. */
  678. static void ccdc_config_vp(struct isp_ccdc_device *ccdc)
  679. {
  680. struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
  681. struct isp_device *isp = to_isp_device(ccdc);
  682. const struct isp_format_info *info;
  683. unsigned long l3_ick = pipe->l3_ick;
  684. unsigned int max_div = isp->revision == ISP_REVISION_15_0 ? 64 : 8;
  685. unsigned int div = 0;
  686. u32 fmtcfg_vp;
  687. fmtcfg_vp = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG)
  688. & ~(ISPCCDC_FMTCFG_VPIN_MASK | ISPCCDC_FMTCFG_VPIF_FRQ_MASK);
  689. info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
  690. switch (info->width) {
  691. case 8:
  692. case 10:
  693. fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_9_0;
  694. break;
  695. case 11:
  696. fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_10_1;
  697. break;
  698. case 12:
  699. fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_11_2;
  700. break;
  701. case 13:
  702. fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_12_3;
  703. break;
  704. }
  705. if (pipe->input)
  706. div = DIV_ROUND_UP(l3_ick, pipe->max_rate);
  707. else if (pipe->external_rate)
  708. div = l3_ick / pipe->external_rate;
  709. div = clamp(div, 2U, max_div);
  710. fmtcfg_vp |= (div - 2) << ISPCCDC_FMTCFG_VPIF_FRQ_SHIFT;
  711. isp_reg_writel(isp, fmtcfg_vp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
  712. }
  713. /*
  714. * ccdc_enable_vp - Enable Video Port.
  715. * @ccdc: Pointer to ISP CCDC device.
  716. * @enable: 0 Disables VP, 1 Enables VP
  717. *
  718. * This is needed for outputting image to Preview, H3A and HIST ISP submodules.
  719. */
  720. static void ccdc_enable_vp(struct isp_ccdc_device *ccdc, u8 enable)
  721. {
  722. struct isp_device *isp = to_isp_device(ccdc);
  723. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG,
  724. ISPCCDC_FMTCFG_VPEN, enable ? ISPCCDC_FMTCFG_VPEN : 0);
  725. }
  726. /*
  727. * ccdc_config_outlineoffset - Configure memory saving output line offset
  728. * @ccdc: Pointer to ISP CCDC device.
  729. * @bpl: Number of bytes per line when stored in memory.
  730. * @field: Field order when storing interlaced formats in memory.
  731. *
  732. * Configure the offsets for the line output control:
  733. *
  734. * - The horizontal line offset is defined as the number of bytes between the
  735. * start of two consecutive lines in memory. Set it to the given bytes per
  736. * line value.
  737. *
  738. * - The field offset value is defined as the number of lines to offset the
  739. * start of the field identified by FID = 1. Set it to one.
  740. *
  741. * - The line offset values are defined as the number of lines (as defined by
  742. * the horizontal line offset) between the start of two consecutive lines for
  743. * all combinations of odd/even lines in odd/even fields. When interleaving
  744. * fields set them all to two lines, and to one line otherwise.
  745. */
  746. static void ccdc_config_outlineoffset(struct isp_ccdc_device *ccdc,
  747. unsigned int bpl,
  748. enum v4l2_field field)
  749. {
  750. struct isp_device *isp = to_isp_device(ccdc);
  751. u32 sdofst = 0;
  752. isp_reg_writel(isp, bpl & 0xffff, OMAP3_ISP_IOMEM_CCDC,
  753. ISPCCDC_HSIZE_OFF);
  754. switch (field) {
  755. case V4L2_FIELD_INTERLACED_TB:
  756. case V4L2_FIELD_INTERLACED_BT:
  757. /* When interleaving fields in memory offset field one by one
  758. * line and set the line offset to two lines.
  759. */
  760. sdofst |= (1 << ISPCCDC_SDOFST_LOFST0_SHIFT)
  761. | (1 << ISPCCDC_SDOFST_LOFST1_SHIFT)
  762. | (1 << ISPCCDC_SDOFST_LOFST2_SHIFT)
  763. | (1 << ISPCCDC_SDOFST_LOFST3_SHIFT);
  764. break;
  765. default:
  766. /* In all other cases set the line offsets to one line. */
  767. break;
  768. }
  769. isp_reg_writel(isp, sdofst, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST);
  770. }
  771. /*
  772. * ccdc_set_outaddr - Set memory address to save output image
  773. * @ccdc: Pointer to ISP CCDC device.
  774. * @addr: ISP MMU Mapped 32-bit memory address aligned on 32 byte boundary.
  775. *
  776. * Sets the memory address where the output will be saved.
  777. */
  778. static void ccdc_set_outaddr(struct isp_ccdc_device *ccdc, u32 addr)
  779. {
  780. struct isp_device *isp = to_isp_device(ccdc);
  781. isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDR_ADDR);
  782. }
  783. /*
  784. * omap3isp_ccdc_max_rate - Calculate maximum input data rate based on the input
  785. * @ccdc: Pointer to ISP CCDC device.
  786. * @max_rate: Maximum calculated data rate.
  787. *
  788. * Returns in *max_rate less value between calculated and passed
  789. */
  790. void omap3isp_ccdc_max_rate(struct isp_ccdc_device *ccdc,
  791. unsigned int *max_rate)
  792. {
  793. struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
  794. unsigned int rate;
  795. if (pipe == NULL)
  796. return;
  797. /*
  798. * TRM says that for parallel sensors the maximum data rate
  799. * should be 90% form L3/2 clock, otherwise just L3/2.
  800. */
  801. if (ccdc->input == CCDC_INPUT_PARALLEL)
  802. rate = pipe->l3_ick / 2 * 9 / 10;
  803. else
  804. rate = pipe->l3_ick / 2;
  805. *max_rate = min(*max_rate, rate);
  806. }
  807. /*
  808. * ccdc_config_sync_if - Set CCDC sync interface configuration
  809. * @ccdc: Pointer to ISP CCDC device.
  810. * @pdata: Parallel interface platform data (may be NULL)
  811. * @data_size: Data size
  812. */
  813. static void ccdc_config_sync_if(struct isp_ccdc_device *ccdc,
  814. struct isp_parallel_platform_data *pdata,
  815. unsigned int data_size)
  816. {
  817. struct isp_device *isp = to_isp_device(ccdc);
  818. const struct v4l2_mbus_framefmt *format;
  819. u32 syn_mode = ISPCCDC_SYN_MODE_VDHDEN;
  820. format = &ccdc->formats[CCDC_PAD_SINK];
  821. if (format->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
  822. format->code == V4L2_MBUS_FMT_UYVY8_2X8) {
  823. /* According to the OMAP3 TRM the input mode only affects SYNC
  824. * mode, enabling BT.656 mode should take precedence. However,
  825. * in practice setting the input mode to YCbCr data on 8 bits
  826. * seems to be required in BT.656 mode. In SYNC mode set it to
  827. * YCbCr on 16 bits as the bridge is enabled in that case.
  828. */
  829. if (ccdc->bt656)
  830. syn_mode |= ISPCCDC_SYN_MODE_INPMOD_YCBCR8;
  831. else
  832. syn_mode |= ISPCCDC_SYN_MODE_INPMOD_YCBCR16;
  833. }
  834. switch (data_size) {
  835. case 8:
  836. syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_8;
  837. break;
  838. case 10:
  839. syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_10;
  840. break;
  841. case 11:
  842. syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_11;
  843. break;
  844. case 12:
  845. syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_12;
  846. break;
  847. }
  848. if (pdata && pdata->data_pol)
  849. syn_mode |= ISPCCDC_SYN_MODE_DATAPOL;
  850. if (pdata && pdata->hs_pol)
  851. syn_mode |= ISPCCDC_SYN_MODE_HDPOL;
  852. /* The polarity of the vertical sync signal output by the BT.656
  853. * decoder is not documented and seems to be active low.
  854. */
  855. if ((pdata && pdata->vs_pol) || ccdc->bt656)
  856. syn_mode |= ISPCCDC_SYN_MODE_VDPOL;
  857. if (pdata && pdata->fld_pol)
  858. syn_mode |= ISPCCDC_SYN_MODE_FLDPOL;
  859. isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
  860. /* The CCDC_CFG.Y8POS bit is used in YCbCr8 input mode only. The
  861. * hardware seems to ignore it in all other input modes.
  862. */
  863. if (format->code == V4L2_MBUS_FMT_UYVY8_2X8)
  864. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
  865. ISPCCDC_CFG_Y8POS);
  866. else
  867. isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
  868. ISPCCDC_CFG_Y8POS);
  869. /* Enable or disable BT.656 mode, including error correction for the
  870. * synchronization codes.
  871. */
  872. if (ccdc->bt656)
  873. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
  874. ISPCCDC_REC656IF_R656ON | ISPCCDC_REC656IF_ECCFVH);
  875. else
  876. isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
  877. ISPCCDC_REC656IF_R656ON | ISPCCDC_REC656IF_ECCFVH);
  878. }
  879. /* CCDC formats descriptions */
  880. static const u32 ccdc_sgrbg_pattern =
  881. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
  882. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
  883. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
  884. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
  885. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
  886. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
  887. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
  888. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
  889. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
  890. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
  891. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
  892. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
  893. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
  894. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
  895. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
  896. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
  897. static const u32 ccdc_srggb_pattern =
  898. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
  899. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
  900. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
  901. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
  902. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
  903. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
  904. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
  905. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
  906. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
  907. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
  908. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
  909. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
  910. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
  911. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
  912. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
  913. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
  914. static const u32 ccdc_sbggr_pattern =
  915. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
  916. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
  917. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
  918. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
  919. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
  920. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
  921. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
  922. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
  923. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
  924. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
  925. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
  926. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
  927. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
  928. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
  929. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
  930. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
  931. static const u32 ccdc_sgbrg_pattern =
  932. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
  933. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
  934. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
  935. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
  936. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
  937. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
  938. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
  939. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
  940. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
  941. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
  942. ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
  943. ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
  944. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
  945. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
  946. ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
  947. ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
  948. static void ccdc_configure(struct isp_ccdc_device *ccdc)
  949. {
  950. struct isp_device *isp = to_isp_device(ccdc);
  951. struct isp_parallel_platform_data *pdata = NULL;
  952. struct v4l2_subdev *sensor;
  953. struct v4l2_mbus_framefmt *format;
  954. const struct v4l2_rect *crop;
  955. const struct isp_format_info *fmt_info;
  956. struct v4l2_subdev_format fmt_src;
  957. unsigned int depth_out;
  958. unsigned int depth_in = 0;
  959. struct media_pad *pad;
  960. unsigned long flags;
  961. unsigned int bridge;
  962. unsigned int shift;
  963. unsigned int nph;
  964. unsigned int sph;
  965. u32 syn_mode;
  966. u32 ccdc_pattern;
  967. ccdc->bt656 = false;
  968. pad = media_entity_remote_pad(&ccdc->pads[CCDC_PAD_SINK]);
  969. sensor = media_entity_to_v4l2_subdev(pad->entity);
  970. if (ccdc->input == CCDC_INPUT_PARALLEL) {
  971. struct v4l2_mbus_config cfg;
  972. int ret;
  973. ret = v4l2_subdev_call(sensor, video, g_mbus_config, &cfg);
  974. if (!ret)
  975. ccdc->bt656 = cfg.type == V4L2_MBUS_BT656;
  976. pdata = &((struct isp_v4l2_subdevs_group *)sensor->host_priv)
  977. ->bus.parallel;
  978. }
  979. /* CCDC_PAD_SINK */
  980. format = &ccdc->formats[CCDC_PAD_SINK];
  981. /* Compute the lane shifter shift value and enable the bridge when the
  982. * input format is a non-BT.656 YUV variant.
  983. */
  984. fmt_src.pad = pad->index;
  985. fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  986. if (!v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt_src)) {
  987. fmt_info = omap3isp_video_format_info(fmt_src.format.code);
  988. depth_in = fmt_info->width;
  989. }
  990. fmt_info = omap3isp_video_format_info(format->code);
  991. depth_out = fmt_info->width;
  992. shift = depth_in - depth_out;
  993. if (ccdc->bt656)
  994. bridge = ISPCTRL_PAR_BRIDGE_DISABLE;
  995. else if (fmt_info->code == V4L2_MBUS_FMT_YUYV8_2X8)
  996. bridge = ISPCTRL_PAR_BRIDGE_LENDIAN;
  997. else if (fmt_info->code == V4L2_MBUS_FMT_UYVY8_2X8)
  998. bridge = ISPCTRL_PAR_BRIDGE_BENDIAN;
  999. else
  1000. bridge = ISPCTRL_PAR_BRIDGE_DISABLE;
  1001. omap3isp_configure_bridge(isp, ccdc->input, pdata, shift, bridge);
  1002. /* Configure the sync interface. */
  1003. ccdc_config_sync_if(ccdc, pdata, depth_out);
  1004. syn_mode = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
  1005. /* Use the raw, unprocessed data when writing to memory. The H3A and
  1006. * histogram modules are still fed with lens shading corrected data.
  1007. */
  1008. syn_mode &= ~ISPCCDC_SYN_MODE_VP2SDR;
  1009. if (ccdc->output & CCDC_OUTPUT_MEMORY)
  1010. syn_mode |= ISPCCDC_SYN_MODE_WEN;
  1011. else
  1012. syn_mode &= ~ISPCCDC_SYN_MODE_WEN;
  1013. if (ccdc->output & CCDC_OUTPUT_RESIZER)
  1014. syn_mode |= ISPCCDC_SYN_MODE_SDR2RSZ;
  1015. else
  1016. syn_mode &= ~ISPCCDC_SYN_MODE_SDR2RSZ;
  1017. /* Mosaic filter */
  1018. switch (format->code) {
  1019. case V4L2_MBUS_FMT_SRGGB10_1X10:
  1020. case V4L2_MBUS_FMT_SRGGB12_1X12:
  1021. ccdc_pattern = ccdc_srggb_pattern;
  1022. break;
  1023. case V4L2_MBUS_FMT_SBGGR10_1X10:
  1024. case V4L2_MBUS_FMT_SBGGR12_1X12:
  1025. ccdc_pattern = ccdc_sbggr_pattern;
  1026. break;
  1027. case V4L2_MBUS_FMT_SGBRG10_1X10:
  1028. case V4L2_MBUS_FMT_SGBRG12_1X12:
  1029. ccdc_pattern = ccdc_sgbrg_pattern;
  1030. break;
  1031. default:
  1032. /* Use GRBG */
  1033. ccdc_pattern = ccdc_sgrbg_pattern;
  1034. break;
  1035. }
  1036. ccdc_config_imgattr(ccdc, ccdc_pattern);
  1037. /* Generate VD0 on the last line of the image and VD1 on the
  1038. * 2/3 height line.
  1039. */
  1040. isp_reg_writel(isp, ((format->height - 2) << ISPCCDC_VDINT_0_SHIFT) |
  1041. ((format->height * 2 / 3) << ISPCCDC_VDINT_1_SHIFT),
  1042. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VDINT);
  1043. /* CCDC_PAD_SOURCE_OF */
  1044. format = &ccdc->formats[CCDC_PAD_SOURCE_OF];
  1045. crop = &ccdc->crop;
  1046. /* The horizontal coordinates are expressed in pixel clock cycles. We
  1047. * need two cycles per pixel in BT.656 mode, and one cycle per pixel in
  1048. * SYNC mode regardless of the format as the bridge is enabled for YUV
  1049. * formats in that case.
  1050. */
  1051. if (ccdc->bt656) {
  1052. sph = crop->left * 2;
  1053. nph = crop->width * 2 - 1;
  1054. } else {
  1055. sph = crop->left;
  1056. nph = crop->width - 1;
  1057. }
  1058. isp_reg_writel(isp, (sph << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
  1059. (nph << ISPCCDC_HORZ_INFO_NPH_SHIFT),
  1060. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HORZ_INFO);
  1061. isp_reg_writel(isp, (crop->top << ISPCCDC_VERT_START_SLV0_SHIFT) |
  1062. (crop->top << ISPCCDC_VERT_START_SLV1_SHIFT),
  1063. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_START);
  1064. isp_reg_writel(isp, (crop->height - 1)
  1065. << ISPCCDC_VERT_LINES_NLV_SHIFT,
  1066. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_LINES);
  1067. ccdc_config_outlineoffset(ccdc, ccdc->video_out.bpl_value,
  1068. format->field);
  1069. /* When interleaving fields enable processing of the field input signal.
  1070. * This will cause the line output control module to apply the field
  1071. * offset to field 1.
  1072. */
  1073. if (ccdc->formats[CCDC_PAD_SINK].field == V4L2_FIELD_ALTERNATE &&
  1074. (format->field == V4L2_FIELD_INTERLACED_TB ||
  1075. format->field == V4L2_FIELD_INTERLACED_BT))
  1076. syn_mode |= ISPCCDC_SYN_MODE_FLDMODE;
  1077. /* The CCDC outputs data in UYVY order by default. Swap bytes to get
  1078. * YUYV.
  1079. */
  1080. if (format->code == V4L2_MBUS_FMT_YUYV8_1X16)
  1081. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
  1082. ISPCCDC_CFG_BSWD);
  1083. else
  1084. isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
  1085. ISPCCDC_CFG_BSWD);
  1086. /* Use PACK8 mode for 1byte per pixel formats. Check for BT.656 mode
  1087. * explicitly as the driver reports 1X16 instead of 2X8 at the OF pad
  1088. * for simplicity.
  1089. */
  1090. if (omap3isp_video_format_info(format->code)->width <= 8 || ccdc->bt656)
  1091. syn_mode |= ISPCCDC_SYN_MODE_PACK8;
  1092. else
  1093. syn_mode &= ~ISPCCDC_SYN_MODE_PACK8;
  1094. isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
  1095. /* CCDC_PAD_SOURCE_VP */
  1096. format = &ccdc->formats[CCDC_PAD_SOURCE_VP];
  1097. isp_reg_writel(isp, (0 << ISPCCDC_FMT_HORZ_FMTSPH_SHIFT) |
  1098. (format->width << ISPCCDC_FMT_HORZ_FMTLNH_SHIFT),
  1099. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_HORZ);
  1100. isp_reg_writel(isp, (0 << ISPCCDC_FMT_VERT_FMTSLV_SHIFT) |
  1101. ((format->height + 1) << ISPCCDC_FMT_VERT_FMTLNV_SHIFT),
  1102. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_VERT);
  1103. isp_reg_writel(isp, (format->width << ISPCCDC_VP_OUT_HORZ_NUM_SHIFT) |
  1104. (format->height << ISPCCDC_VP_OUT_VERT_NUM_SHIFT),
  1105. OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VP_OUT);
  1106. /* Lens shading correction. */
  1107. spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
  1108. if (ccdc->lsc.request == NULL)
  1109. goto unlock;
  1110. WARN_ON(ccdc->lsc.active);
  1111. /* Get last good LSC configuration. If it is not supported for
  1112. * the current active resolution discard it.
  1113. */
  1114. if (ccdc->lsc.active == NULL &&
  1115. __ccdc_lsc_configure(ccdc, ccdc->lsc.request) == 0) {
  1116. ccdc->lsc.active = ccdc->lsc.request;
  1117. } else {
  1118. list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
  1119. schedule_work(&ccdc->lsc.table_work);
  1120. }
  1121. ccdc->lsc.request = NULL;
  1122. unlock:
  1123. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  1124. ccdc_apply_controls(ccdc);
  1125. }
  1126. static void __ccdc_enable(struct isp_ccdc_device *ccdc, int enable)
  1127. {
  1128. struct isp_device *isp = to_isp_device(ccdc);
  1129. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR,
  1130. ISPCCDC_PCR_EN, enable ? ISPCCDC_PCR_EN : 0);
  1131. }
  1132. static int ccdc_disable(struct isp_ccdc_device *ccdc)
  1133. {
  1134. unsigned long flags;
  1135. int ret = 0;
  1136. spin_lock_irqsave(&ccdc->lock, flags);
  1137. if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS)
  1138. ccdc->stopping = CCDC_STOP_REQUEST;
  1139. spin_unlock_irqrestore(&ccdc->lock, flags);
  1140. ret = wait_event_timeout(ccdc->wait,
  1141. ccdc->stopping == CCDC_STOP_FINISHED,
  1142. msecs_to_jiffies(2000));
  1143. if (ret == 0) {
  1144. ret = -ETIMEDOUT;
  1145. dev_warn(to_device(ccdc), "CCDC stop timeout!\n");
  1146. }
  1147. omap3isp_sbl_disable(to_isp_device(ccdc), OMAP3_ISP_SBL_CCDC_LSC_READ);
  1148. mutex_lock(&ccdc->ioctl_lock);
  1149. ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
  1150. ccdc->lsc.request = ccdc->lsc.active;
  1151. ccdc->lsc.active = NULL;
  1152. cancel_work_sync(&ccdc->lsc.table_work);
  1153. ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
  1154. mutex_unlock(&ccdc->ioctl_lock);
  1155. ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
  1156. return ret > 0 ? 0 : ret;
  1157. }
  1158. static void ccdc_enable(struct isp_ccdc_device *ccdc)
  1159. {
  1160. if (ccdc_lsc_is_configured(ccdc))
  1161. __ccdc_lsc_enable(ccdc, 1);
  1162. __ccdc_enable(ccdc, 1);
  1163. }
  1164. /* -----------------------------------------------------------------------------
  1165. * Interrupt handling
  1166. */
  1167. /*
  1168. * ccdc_sbl_busy - Poll idle state of CCDC and related SBL memory write bits
  1169. * @ccdc: Pointer to ISP CCDC device.
  1170. *
  1171. * Returns zero if the CCDC is idle and the image has been written to
  1172. * memory, too.
  1173. */
  1174. static int ccdc_sbl_busy(struct isp_ccdc_device *ccdc)
  1175. {
  1176. struct isp_device *isp = to_isp_device(ccdc);
  1177. return omap3isp_ccdc_busy(ccdc)
  1178. | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_0) &
  1179. ISPSBL_CCDC_WR_0_DATA_READY)
  1180. | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_1) &
  1181. ISPSBL_CCDC_WR_0_DATA_READY)
  1182. | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_2) &
  1183. ISPSBL_CCDC_WR_0_DATA_READY)
  1184. | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_3) &
  1185. ISPSBL_CCDC_WR_0_DATA_READY);
  1186. }
  1187. /*
  1188. * ccdc_sbl_wait_idle - Wait until the CCDC and related SBL are idle
  1189. * @ccdc: Pointer to ISP CCDC device.
  1190. * @max_wait: Max retry count in us for wait for idle/busy transition.
  1191. */
  1192. static int ccdc_sbl_wait_idle(struct isp_ccdc_device *ccdc,
  1193. unsigned int max_wait)
  1194. {
  1195. unsigned int wait = 0;
  1196. if (max_wait == 0)
  1197. max_wait = 10000; /* 10 ms */
  1198. for (wait = 0; wait <= max_wait; wait++) {
  1199. if (!ccdc_sbl_busy(ccdc))
  1200. return 0;
  1201. rmb();
  1202. udelay(1);
  1203. }
  1204. return -EBUSY;
  1205. }
  1206. /* __ccdc_handle_stopping - Handle CCDC and/or LSC stopping sequence
  1207. * @ccdc: Pointer to ISP CCDC device.
  1208. * @event: Pointing which event trigger handler
  1209. *
  1210. * Return 1 when the event and stopping request combination is satisfied,
  1211. * zero otherwise.
  1212. */
  1213. static int __ccdc_handle_stopping(struct isp_ccdc_device *ccdc, u32 event)
  1214. {
  1215. int rval = 0;
  1216. switch ((ccdc->stopping & 3) | event) {
  1217. case CCDC_STOP_REQUEST | CCDC_EVENT_VD1:
  1218. if (ccdc->lsc.state != LSC_STATE_STOPPED)
  1219. __ccdc_lsc_enable(ccdc, 0);
  1220. __ccdc_enable(ccdc, 0);
  1221. ccdc->stopping = CCDC_STOP_EXECUTED;
  1222. return 1;
  1223. case CCDC_STOP_EXECUTED | CCDC_EVENT_VD0:
  1224. ccdc->stopping |= CCDC_STOP_CCDC_FINISHED;
  1225. if (ccdc->lsc.state == LSC_STATE_STOPPED)
  1226. ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
  1227. rval = 1;
  1228. break;
  1229. case CCDC_STOP_EXECUTED | CCDC_EVENT_LSC_DONE:
  1230. ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
  1231. rval = 1;
  1232. break;
  1233. case CCDC_STOP_EXECUTED | CCDC_EVENT_VD1:
  1234. return 1;
  1235. }
  1236. if (ccdc->stopping == CCDC_STOP_FINISHED) {
  1237. wake_up(&ccdc->wait);
  1238. rval = 1;
  1239. }
  1240. return rval;
  1241. }
  1242. static void ccdc_hs_vs_isr(struct isp_ccdc_device *ccdc)
  1243. {
  1244. struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
  1245. struct video_device *vdev = ccdc->subdev.devnode;
  1246. struct v4l2_event event;
  1247. /* Frame number propagation */
  1248. atomic_inc(&pipe->frame_number);
  1249. memset(&event, 0, sizeof(event));
  1250. event.type = V4L2_EVENT_FRAME_SYNC;
  1251. event.u.frame_sync.frame_sequence = atomic_read(&pipe->frame_number);
  1252. v4l2_event_queue(vdev, &event);
  1253. }
  1254. /*
  1255. * ccdc_lsc_isr - Handle LSC events
  1256. * @ccdc: Pointer to ISP CCDC device.
  1257. * @events: LSC events
  1258. */
  1259. static void ccdc_lsc_isr(struct isp_ccdc_device *ccdc, u32 events)
  1260. {
  1261. unsigned long flags;
  1262. if (events & IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ) {
  1263. struct isp_pipeline *pipe =
  1264. to_isp_pipeline(&ccdc->subdev.entity);
  1265. ccdc_lsc_error_handler(ccdc);
  1266. pipe->error = true;
  1267. dev_dbg(to_device(ccdc), "lsc prefetch error\n");
  1268. }
  1269. if (!(events & IRQ0STATUS_CCDC_LSC_DONE_IRQ))
  1270. return;
  1271. /* LSC_DONE interrupt occur, there are two cases
  1272. * 1. stopping for reconfiguration
  1273. * 2. stopping because of STREAM OFF command
  1274. */
  1275. spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
  1276. if (ccdc->lsc.state == LSC_STATE_STOPPING)
  1277. ccdc->lsc.state = LSC_STATE_STOPPED;
  1278. if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_LSC_DONE))
  1279. goto done;
  1280. if (ccdc->lsc.state != LSC_STATE_RECONFIG)
  1281. goto done;
  1282. /* LSC is in STOPPING state, change to the new state */
  1283. ccdc->lsc.state = LSC_STATE_STOPPED;
  1284. /* This is an exception. Start of frame and LSC_DONE interrupt
  1285. * have been received on the same time. Skip this event and wait
  1286. * for better times.
  1287. */
  1288. if (events & IRQ0STATUS_HS_VS_IRQ)
  1289. goto done;
  1290. /* The LSC engine is stopped at this point. Enable it if there's a
  1291. * pending request.
  1292. */
  1293. if (ccdc->lsc.request == NULL)
  1294. goto done;
  1295. ccdc_lsc_enable(ccdc);
  1296. done:
  1297. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  1298. }
  1299. static int ccdc_isr_buffer(struct isp_ccdc_device *ccdc)
  1300. {
  1301. struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
  1302. struct isp_device *isp = to_isp_device(ccdc);
  1303. struct isp_buffer *buffer;
  1304. enum v4l2_field field;
  1305. /* The CCDC generates VD0 interrupts even when disabled (the datasheet
  1306. * doesn't explicitly state if that's supposed to happen or not, so it
  1307. * can be considered as a hardware bug or as a feature, but we have to
  1308. * deal with it anyway). Disabling the CCDC when no buffer is available
  1309. * would thus not be enough, we need to handle the situation explicitly.
  1310. */
  1311. if (list_empty(&ccdc->video_out.dmaqueue))
  1312. return 0;
  1313. /* We're in continuous mode, and memory writes were disabled due to a
  1314. * buffer underrun. Reenable them now that we have a buffer. The buffer
  1315. * address has been set in ccdc_video_queue.
  1316. */
  1317. if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && ccdc->underrun) {
  1318. ccdc->underrun = 0;
  1319. return 1;
  1320. }
  1321. /* Read the current field identifier. */
  1322. field = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE)
  1323. & ISPCCDC_SYN_MODE_FLDSTAT
  1324. ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
  1325. /* Wait for the CCDC to become idle. */
  1326. if (ccdc_sbl_wait_idle(ccdc, 1000)) {
  1327. dev_info(isp->dev, "CCDC won't become idle!\n");
  1328. isp->crashed |= 1U << ccdc->subdev.entity.id;
  1329. omap3isp_pipeline_cancel_stream(pipe);
  1330. return 0;
  1331. }
  1332. switch (ccdc->formats[CCDC_PAD_SOURCE_OF].field) {
  1333. case V4L2_FIELD_ALTERNATE:
  1334. /* When capturing fields in alternate order store the current
  1335. * field identifier in the pipeline.
  1336. */
  1337. pipe->field = field;
  1338. break;
  1339. case V4L2_FIELD_INTERLACED_TB:
  1340. /* When interleaving fields only complete the buffer after
  1341. * capturing the second field.
  1342. */
  1343. if (field == V4L2_FIELD_TOP)
  1344. return 1;
  1345. break;
  1346. case V4L2_FIELD_INTERLACED_BT:
  1347. if (field == V4L2_FIELD_BOTTOM)
  1348. return 1;
  1349. break;
  1350. }
  1351. buffer = omap3isp_video_buffer_next(&ccdc->video_out);
  1352. if (buffer != NULL)
  1353. ccdc_set_outaddr(ccdc, buffer->dma);
  1354. pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
  1355. if (ccdc->state == ISP_PIPELINE_STREAM_SINGLESHOT &&
  1356. isp_pipeline_ready(pipe))
  1357. omap3isp_pipeline_set_stream(pipe,
  1358. ISP_PIPELINE_STREAM_SINGLESHOT);
  1359. return buffer != NULL;
  1360. }
  1361. /*
  1362. * ccdc_vd0_isr - Handle VD0 event
  1363. * @ccdc: Pointer to ISP CCDC device.
  1364. *
  1365. * Executes LSC deferred enablement before next frame starts.
  1366. */
  1367. static void ccdc_vd0_isr(struct isp_ccdc_device *ccdc)
  1368. {
  1369. unsigned long flags;
  1370. int restart = 0;
  1371. if (ccdc->output & CCDC_OUTPUT_MEMORY)
  1372. restart = ccdc_isr_buffer(ccdc);
  1373. spin_lock_irqsave(&ccdc->lock, flags);
  1374. if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD0)) {
  1375. spin_unlock_irqrestore(&ccdc->lock, flags);
  1376. return;
  1377. }
  1378. if (!ccdc->shadow_update)
  1379. ccdc_apply_controls(ccdc);
  1380. spin_unlock_irqrestore(&ccdc->lock, flags);
  1381. if (restart)
  1382. ccdc_enable(ccdc);
  1383. }
  1384. /*
  1385. * ccdc_vd1_isr - Handle VD1 event
  1386. * @ccdc: Pointer to ISP CCDC device.
  1387. */
  1388. static void ccdc_vd1_isr(struct isp_ccdc_device *ccdc)
  1389. {
  1390. unsigned long flags;
  1391. /* In BT.656 mode the CCDC doesn't generate an HS/VS interrupt. We thus
  1392. * need to increment the frame counter here.
  1393. */
  1394. if (ccdc->bt656) {
  1395. struct isp_pipeline *pipe =
  1396. to_isp_pipeline(&ccdc->subdev.entity);
  1397. atomic_inc(&pipe->frame_number);
  1398. }
  1399. spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
  1400. /*
  1401. * Depending on the CCDC pipeline state, CCDC stopping should be
  1402. * handled differently. In SINGLESHOT we emulate an internal CCDC
  1403. * stopping because the CCDC hw works only in continuous mode.
  1404. * When CONTINUOUS pipeline state is used and the CCDC writes it's
  1405. * data to memory the CCDC and LSC are stopped immediately but
  1406. * without change the CCDC stopping state machine. The CCDC
  1407. * stopping state machine should be used only when user request
  1408. * for stopping is received (SINGLESHOT is an exeption).
  1409. */
  1410. switch (ccdc->state) {
  1411. case ISP_PIPELINE_STREAM_SINGLESHOT:
  1412. ccdc->stopping = CCDC_STOP_REQUEST;
  1413. break;
  1414. case ISP_PIPELINE_STREAM_CONTINUOUS:
  1415. if (ccdc->output & CCDC_OUTPUT_MEMORY) {
  1416. if (ccdc->lsc.state != LSC_STATE_STOPPED)
  1417. __ccdc_lsc_enable(ccdc, 0);
  1418. __ccdc_enable(ccdc, 0);
  1419. }
  1420. break;
  1421. case ISP_PIPELINE_STREAM_STOPPED:
  1422. break;
  1423. }
  1424. if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1))
  1425. goto done;
  1426. if (ccdc->lsc.request == NULL)
  1427. goto done;
  1428. /*
  1429. * LSC need to be reconfigured. Stop it here and on next LSC_DONE IRQ
  1430. * do the appropriate changes in registers
  1431. */
  1432. if (ccdc->lsc.state == LSC_STATE_RUNNING) {
  1433. __ccdc_lsc_enable(ccdc, 0);
  1434. ccdc->lsc.state = LSC_STATE_RECONFIG;
  1435. goto done;
  1436. }
  1437. /* LSC has been in STOPPED state, enable it */
  1438. if (ccdc->lsc.state == LSC_STATE_STOPPED)
  1439. ccdc_lsc_enable(ccdc);
  1440. done:
  1441. spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
  1442. }
  1443. /*
  1444. * omap3isp_ccdc_isr - Configure CCDC during interframe time.
  1445. * @ccdc: Pointer to ISP CCDC device.
  1446. * @events: CCDC events
  1447. */
  1448. int omap3isp_ccdc_isr(struct isp_ccdc_device *ccdc, u32 events)
  1449. {
  1450. if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED)
  1451. return 0;
  1452. if (events & IRQ0STATUS_CCDC_VD1_IRQ)
  1453. ccdc_vd1_isr(ccdc);
  1454. ccdc_lsc_isr(ccdc, events);
  1455. if (events & IRQ0STATUS_CCDC_VD0_IRQ)
  1456. ccdc_vd0_isr(ccdc);
  1457. if (events & IRQ0STATUS_HS_VS_IRQ)
  1458. ccdc_hs_vs_isr(ccdc);
  1459. return 0;
  1460. }
  1461. /* -----------------------------------------------------------------------------
  1462. * ISP video operations
  1463. */
  1464. static int ccdc_video_queue(struct isp_video *video, struct isp_buffer *buffer)
  1465. {
  1466. struct isp_ccdc_device *ccdc = &video->isp->isp_ccdc;
  1467. if (!(ccdc->output & CCDC_OUTPUT_MEMORY))
  1468. return -ENODEV;
  1469. ccdc_set_outaddr(ccdc, buffer->dma);
  1470. /* We now have a buffer queued on the output, restart the pipeline
  1471. * on the next CCDC interrupt if running in continuous mode (or when
  1472. * starting the stream).
  1473. */
  1474. ccdc->underrun = 1;
  1475. return 0;
  1476. }
  1477. static const struct isp_video_operations ccdc_video_ops = {
  1478. .queue = ccdc_video_queue,
  1479. };
  1480. /* -----------------------------------------------------------------------------
  1481. * V4L2 subdev operations
  1482. */
  1483. /*
  1484. * ccdc_ioctl - CCDC module private ioctl's
  1485. * @sd: ISP CCDC V4L2 subdevice
  1486. * @cmd: ioctl command
  1487. * @arg: ioctl argument
  1488. *
  1489. * Return 0 on success or a negative error code otherwise.
  1490. */
  1491. static long ccdc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  1492. {
  1493. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1494. int ret;
  1495. switch (cmd) {
  1496. case VIDIOC_OMAP3ISP_CCDC_CFG:
  1497. mutex_lock(&ccdc->ioctl_lock);
  1498. ret = ccdc_config(ccdc, arg);
  1499. mutex_unlock(&ccdc->ioctl_lock);
  1500. break;
  1501. default:
  1502. return -ENOIOCTLCMD;
  1503. }
  1504. return ret;
  1505. }
  1506. static int ccdc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  1507. struct v4l2_event_subscription *sub)
  1508. {
  1509. if (sub->type != V4L2_EVENT_FRAME_SYNC)
  1510. return -EINVAL;
  1511. /* line number is zero at frame start */
  1512. if (sub->id != 0)
  1513. return -EINVAL;
  1514. return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS, NULL);
  1515. }
  1516. static int ccdc_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  1517. struct v4l2_event_subscription *sub)
  1518. {
  1519. return v4l2_event_unsubscribe(fh, sub);
  1520. }
  1521. /*
  1522. * ccdc_set_stream - Enable/Disable streaming on the CCDC module
  1523. * @sd: ISP CCDC V4L2 subdevice
  1524. * @enable: Enable/disable stream
  1525. *
  1526. * When writing to memory, the CCDC hardware can't be enabled without a memory
  1527. * buffer to write to. As the s_stream operation is called in response to a
  1528. * STREAMON call without any buffer queued yet, just update the enabled field
  1529. * and return immediately. The CCDC will be enabled in ccdc_isr_buffer().
  1530. *
  1531. * When not writing to memory enable the CCDC immediately.
  1532. */
  1533. static int ccdc_set_stream(struct v4l2_subdev *sd, int enable)
  1534. {
  1535. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1536. struct isp_device *isp = to_isp_device(ccdc);
  1537. int ret = 0;
  1538. if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED) {
  1539. if (enable == ISP_PIPELINE_STREAM_STOPPED)
  1540. return 0;
  1541. omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_CCDC);
  1542. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
  1543. ISPCCDC_CFG_VDLC);
  1544. ccdc_configure(ccdc);
  1545. /* TODO: Don't configure the video port if all of its output
  1546. * links are inactive.
  1547. */
  1548. ccdc_config_vp(ccdc);
  1549. ccdc_enable_vp(ccdc, 1);
  1550. ccdc_print_status(ccdc);
  1551. }
  1552. switch (enable) {
  1553. case ISP_PIPELINE_STREAM_CONTINUOUS:
  1554. if (ccdc->output & CCDC_OUTPUT_MEMORY)
  1555. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
  1556. if (ccdc->underrun || !(ccdc->output & CCDC_OUTPUT_MEMORY))
  1557. ccdc_enable(ccdc);
  1558. ccdc->underrun = 0;
  1559. break;
  1560. case ISP_PIPELINE_STREAM_SINGLESHOT:
  1561. if (ccdc->output & CCDC_OUTPUT_MEMORY &&
  1562. ccdc->state != ISP_PIPELINE_STREAM_SINGLESHOT)
  1563. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
  1564. ccdc_enable(ccdc);
  1565. break;
  1566. case ISP_PIPELINE_STREAM_STOPPED:
  1567. ret = ccdc_disable(ccdc);
  1568. if (ccdc->output & CCDC_OUTPUT_MEMORY)
  1569. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
  1570. omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_CCDC);
  1571. ccdc->underrun = 0;
  1572. break;
  1573. }
  1574. ccdc->state = enable;
  1575. return ret;
  1576. }
  1577. static struct v4l2_mbus_framefmt *
  1578. __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
  1579. unsigned int pad, enum v4l2_subdev_format_whence which)
  1580. {
  1581. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1582. return v4l2_subdev_get_try_format(fh, pad);
  1583. else
  1584. return &ccdc->formats[pad];
  1585. }
  1586. static struct v4l2_rect *
  1587. __ccdc_get_crop(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
  1588. enum v4l2_subdev_format_whence which)
  1589. {
  1590. if (which == V4L2_SUBDEV_FORMAT_TRY)
  1591. return v4l2_subdev_get_try_crop(fh, CCDC_PAD_SOURCE_OF);
  1592. else
  1593. return &ccdc->crop;
  1594. }
  1595. /*
  1596. * ccdc_try_format - Try video format on a pad
  1597. * @ccdc: ISP CCDC device
  1598. * @fh : V4L2 subdev file handle
  1599. * @pad: Pad number
  1600. * @fmt: Format
  1601. */
  1602. static void
  1603. ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
  1604. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  1605. enum v4l2_subdev_format_whence which)
  1606. {
  1607. const struct isp_format_info *info;
  1608. enum v4l2_mbus_pixelcode pixelcode;
  1609. unsigned int width = fmt->width;
  1610. unsigned int height = fmt->height;
  1611. struct v4l2_rect *crop;
  1612. enum v4l2_field field;
  1613. unsigned int i;
  1614. switch (pad) {
  1615. case CCDC_PAD_SINK:
  1616. for (i = 0; i < ARRAY_SIZE(ccdc_fmts); i++) {
  1617. if (fmt->code == ccdc_fmts[i])
  1618. break;
  1619. }
  1620. /* If not found, use SGRBG10 as default */
  1621. if (i >= ARRAY_SIZE(ccdc_fmts))
  1622. fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  1623. /* Clamp the input size. */
  1624. fmt->width = clamp_t(u32, width, 32, 4096);
  1625. fmt->height = clamp_t(u32, height, 32, 4096);
  1626. /* Default to progressive field order. */
  1627. if (fmt->field == V4L2_FIELD_ANY)
  1628. fmt->field = V4L2_FIELD_NONE;
  1629. break;
  1630. case CCDC_PAD_SOURCE_OF:
  1631. pixelcode = fmt->code;
  1632. field = fmt->field;
  1633. *fmt = *__ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
  1634. /* In SYNC mode the bridge converts YUV formats from 2X8 to
  1635. * 1X16. In BT.656 no such conversion occurs. As we don't know
  1636. * at this point whether the source will use SYNC or BT.656 mode
  1637. * let's pretend the conversion always occurs. The CCDC will be
  1638. * configured to pack bytes in BT.656, hiding the inaccuracy.
  1639. * In all cases bytes can be swapped.
  1640. */
  1641. if (fmt->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
  1642. fmt->code == V4L2_MBUS_FMT_UYVY8_2X8) {
  1643. /* Use the user requested format if YUV. */
  1644. if (pixelcode == V4L2_MBUS_FMT_YUYV8_2X8 ||
  1645. pixelcode == V4L2_MBUS_FMT_UYVY8_2X8 ||
  1646. pixelcode == V4L2_MBUS_FMT_YUYV8_1X16 ||
  1647. pixelcode == V4L2_MBUS_FMT_UYVY8_1X16)
  1648. fmt->code = pixelcode;
  1649. if (fmt->code == V4L2_MBUS_FMT_YUYV8_2X8)
  1650. fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
  1651. else if (fmt->code == V4L2_MBUS_FMT_UYVY8_2X8)
  1652. fmt->code = V4L2_MBUS_FMT_UYVY8_1X16;
  1653. }
  1654. /* Hardcode the output size to the crop rectangle size. */
  1655. crop = __ccdc_get_crop(ccdc, fh, which);
  1656. fmt->width = crop->width;
  1657. fmt->height = crop->height;
  1658. /* When input format is interlaced with alternating fields the
  1659. * CCDC can interleave the fields.
  1660. */
  1661. if (fmt->field == V4L2_FIELD_ALTERNATE &&
  1662. (field == V4L2_FIELD_INTERLACED_TB ||
  1663. field == V4L2_FIELD_INTERLACED_BT)) {
  1664. fmt->field = field;
  1665. fmt->height *= 2;
  1666. }
  1667. break;
  1668. case CCDC_PAD_SOURCE_VP:
  1669. *fmt = *__ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
  1670. /* The video port interface truncates the data to 10 bits. */
  1671. info = omap3isp_video_format_info(fmt->code);
  1672. fmt->code = info->truncated;
  1673. /* YUV formats are not supported by the video port. */
  1674. if (fmt->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
  1675. fmt->code == V4L2_MBUS_FMT_UYVY8_2X8)
  1676. fmt->code = 0;
  1677. /* The number of lines that can be clocked out from the video
  1678. * port output must be at least one line less than the number
  1679. * of input lines.
  1680. */
  1681. fmt->width = clamp_t(u32, width, 32, fmt->width);
  1682. fmt->height = clamp_t(u32, height, 32, fmt->height - 1);
  1683. break;
  1684. }
  1685. /* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
  1686. * stored on 2 bytes.
  1687. */
  1688. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  1689. }
  1690. /*
  1691. * ccdc_try_crop - Validate a crop rectangle
  1692. * @ccdc: ISP CCDC device
  1693. * @sink: format on the sink pad
  1694. * @crop: crop rectangle to be validated
  1695. */
  1696. static void ccdc_try_crop(struct isp_ccdc_device *ccdc,
  1697. const struct v4l2_mbus_framefmt *sink,
  1698. struct v4l2_rect *crop)
  1699. {
  1700. const struct isp_format_info *info;
  1701. unsigned int max_width;
  1702. /* For Bayer formats, restrict left/top and width/height to even values
  1703. * to keep the Bayer pattern.
  1704. */
  1705. info = omap3isp_video_format_info(sink->code);
  1706. if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
  1707. crop->left &= ~1;
  1708. crop->top &= ~1;
  1709. }
  1710. crop->left = clamp_t(u32, crop->left, 0, sink->width - CCDC_MIN_WIDTH);
  1711. crop->top = clamp_t(u32, crop->top, 0, sink->height - CCDC_MIN_HEIGHT);
  1712. /* The data formatter truncates the number of horizontal output pixels
  1713. * to a multiple of 16. To avoid clipping data, allow callers to request
  1714. * an output size bigger than the input size up to the nearest multiple
  1715. * of 16.
  1716. */
  1717. max_width = (sink->width - crop->left + 15) & ~15;
  1718. crop->width = clamp_t(u32, crop->width, CCDC_MIN_WIDTH, max_width)
  1719. & ~15;
  1720. crop->height = clamp_t(u32, crop->height, CCDC_MIN_HEIGHT,
  1721. sink->height - crop->top);
  1722. /* Odd width/height values don't make sense for Bayer formats. */
  1723. if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
  1724. crop->width &= ~1;
  1725. crop->height &= ~1;
  1726. }
  1727. }
  1728. /*
  1729. * ccdc_enum_mbus_code - Handle pixel format enumeration
  1730. * @sd : pointer to v4l2 subdev structure
  1731. * @fh : V4L2 subdev file handle
  1732. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  1733. * return -EINVAL or zero on success
  1734. */
  1735. static int ccdc_enum_mbus_code(struct v4l2_subdev *sd,
  1736. struct v4l2_subdev_fh *fh,
  1737. struct v4l2_subdev_mbus_code_enum *code)
  1738. {
  1739. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1740. struct v4l2_mbus_framefmt *format;
  1741. switch (code->pad) {
  1742. case CCDC_PAD_SINK:
  1743. if (code->index >= ARRAY_SIZE(ccdc_fmts))
  1744. return -EINVAL;
  1745. code->code = ccdc_fmts[code->index];
  1746. break;
  1747. case CCDC_PAD_SOURCE_OF:
  1748. format = __ccdc_get_format(ccdc, fh, code->pad,
  1749. V4L2_SUBDEV_FORMAT_TRY);
  1750. if (format->code == V4L2_MBUS_FMT_YUYV8_2X8 ||
  1751. format->code == V4L2_MBUS_FMT_UYVY8_2X8) {
  1752. /* In YUV mode the CCDC can swap bytes. */
  1753. if (code->index == 0)
  1754. code->code = V4L2_MBUS_FMT_YUYV8_1X16;
  1755. else if (code->index == 1)
  1756. code->code = V4L2_MBUS_FMT_UYVY8_1X16;
  1757. else
  1758. return -EINVAL;
  1759. } else {
  1760. /* In raw mode, no configurable format confversion is
  1761. * available.
  1762. */
  1763. if (code->index == 0)
  1764. code->code = format->code;
  1765. else
  1766. return -EINVAL;
  1767. }
  1768. break;
  1769. case CCDC_PAD_SOURCE_VP:
  1770. /* The CCDC supports no configurable format conversion
  1771. * compatible with the video port. Enumerate a single output
  1772. * format code.
  1773. */
  1774. if (code->index != 0)
  1775. return -EINVAL;
  1776. format = __ccdc_get_format(ccdc, fh, code->pad,
  1777. V4L2_SUBDEV_FORMAT_TRY);
  1778. /* A pixel code equal to 0 means that the video port doesn't
  1779. * support the input format. Don't enumerate any pixel code.
  1780. */
  1781. if (format->code == 0)
  1782. return -EINVAL;
  1783. code->code = format->code;
  1784. break;
  1785. default:
  1786. return -EINVAL;
  1787. }
  1788. return 0;
  1789. }
  1790. static int ccdc_enum_frame_size(struct v4l2_subdev *sd,
  1791. struct v4l2_subdev_fh *fh,
  1792. struct v4l2_subdev_frame_size_enum *fse)
  1793. {
  1794. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1795. struct v4l2_mbus_framefmt format;
  1796. if (fse->index != 0)
  1797. return -EINVAL;
  1798. format.code = fse->code;
  1799. format.width = 1;
  1800. format.height = 1;
  1801. ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1802. fse->min_width = format.width;
  1803. fse->min_height = format.height;
  1804. if (format.code != fse->code)
  1805. return -EINVAL;
  1806. format.code = fse->code;
  1807. format.width = -1;
  1808. format.height = -1;
  1809. ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1810. fse->max_width = format.width;
  1811. fse->max_height = format.height;
  1812. return 0;
  1813. }
  1814. /*
  1815. * ccdc_get_selection - Retrieve a selection rectangle on a pad
  1816. * @sd: ISP CCDC V4L2 subdevice
  1817. * @fh: V4L2 subdev file handle
  1818. * @sel: Selection rectangle
  1819. *
  1820. * The only supported rectangles are the crop rectangles on the output formatter
  1821. * source pad.
  1822. *
  1823. * Return 0 on success or a negative error code otherwise.
  1824. */
  1825. static int ccdc_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1826. struct v4l2_subdev_selection *sel)
  1827. {
  1828. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1829. struct v4l2_mbus_framefmt *format;
  1830. if (sel->pad != CCDC_PAD_SOURCE_OF)
  1831. return -EINVAL;
  1832. switch (sel->target) {
  1833. case V4L2_SEL_TGT_CROP_BOUNDS:
  1834. sel->r.left = 0;
  1835. sel->r.top = 0;
  1836. sel->r.width = INT_MAX;
  1837. sel->r.height = INT_MAX;
  1838. format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
  1839. ccdc_try_crop(ccdc, format, &sel->r);
  1840. break;
  1841. case V4L2_SEL_TGT_CROP:
  1842. sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
  1843. break;
  1844. default:
  1845. return -EINVAL;
  1846. }
  1847. return 0;
  1848. }
  1849. /*
  1850. * ccdc_set_selection - Set a selection rectangle on a pad
  1851. * @sd: ISP CCDC V4L2 subdevice
  1852. * @fh: V4L2 subdev file handle
  1853. * @sel: Selection rectangle
  1854. *
  1855. * The only supported rectangle is the actual crop rectangle on the output
  1856. * formatter source pad.
  1857. *
  1858. * Return 0 on success or a negative error code otherwise.
  1859. */
  1860. static int ccdc_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1861. struct v4l2_subdev_selection *sel)
  1862. {
  1863. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1864. struct v4l2_mbus_framefmt *format;
  1865. if (sel->target != V4L2_SEL_TGT_CROP ||
  1866. sel->pad != CCDC_PAD_SOURCE_OF)
  1867. return -EINVAL;
  1868. /* The crop rectangle can't be changed while streaming. */
  1869. if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
  1870. return -EBUSY;
  1871. /* Modifying the crop rectangle always changes the format on the source
  1872. * pad. If the KEEP_CONFIG flag is set, just return the current crop
  1873. * rectangle.
  1874. */
  1875. if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
  1876. sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
  1877. return 0;
  1878. }
  1879. format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
  1880. ccdc_try_crop(ccdc, format, &sel->r);
  1881. *__ccdc_get_crop(ccdc, fh, sel->which) = sel->r;
  1882. /* Update the source format. */
  1883. format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF, sel->which);
  1884. ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format, sel->which);
  1885. return 0;
  1886. }
  1887. /*
  1888. * ccdc_get_format - Retrieve the video format on a pad
  1889. * @sd : ISP CCDC V4L2 subdevice
  1890. * @fh : V4L2 subdev file handle
  1891. * @fmt: Format
  1892. *
  1893. * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
  1894. * to the format type.
  1895. */
  1896. static int ccdc_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1897. struct v4l2_subdev_format *fmt)
  1898. {
  1899. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1900. struct v4l2_mbus_framefmt *format;
  1901. format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
  1902. if (format == NULL)
  1903. return -EINVAL;
  1904. fmt->format = *format;
  1905. return 0;
  1906. }
  1907. /*
  1908. * ccdc_set_format - Set the video format on a pad
  1909. * @sd : ISP CCDC V4L2 subdevice
  1910. * @fh : V4L2 subdev file handle
  1911. * @fmt: Format
  1912. *
  1913. * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
  1914. * to the format type.
  1915. */
  1916. static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1917. struct v4l2_subdev_format *fmt)
  1918. {
  1919. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1920. struct v4l2_mbus_framefmt *format;
  1921. struct v4l2_rect *crop;
  1922. format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
  1923. if (format == NULL)
  1924. return -EINVAL;
  1925. ccdc_try_format(ccdc, fh, fmt->pad, &fmt->format, fmt->which);
  1926. *format = fmt->format;
  1927. /* Propagate the format from sink to source */
  1928. if (fmt->pad == CCDC_PAD_SINK) {
  1929. /* Reset the crop rectangle. */
  1930. crop = __ccdc_get_crop(ccdc, fh, fmt->which);
  1931. crop->left = 0;
  1932. crop->top = 0;
  1933. crop->width = fmt->format.width;
  1934. crop->height = fmt->format.height;
  1935. ccdc_try_crop(ccdc, &fmt->format, crop);
  1936. /* Update the source formats. */
  1937. format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF,
  1938. fmt->which);
  1939. *format = fmt->format;
  1940. ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format,
  1941. fmt->which);
  1942. format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_VP,
  1943. fmt->which);
  1944. *format = fmt->format;
  1945. ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_VP, format,
  1946. fmt->which);
  1947. }
  1948. return 0;
  1949. }
  1950. /*
  1951. * Decide whether desired output pixel code can be obtained with
  1952. * the lane shifter by shifting the input pixel code.
  1953. * @in: input pixelcode to shifter
  1954. * @out: output pixelcode from shifter
  1955. * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
  1956. *
  1957. * return true if the combination is possible
  1958. * return false otherwise
  1959. */
  1960. static bool ccdc_is_shiftable(enum v4l2_mbus_pixelcode in,
  1961. enum v4l2_mbus_pixelcode out,
  1962. unsigned int additional_shift)
  1963. {
  1964. const struct isp_format_info *in_info, *out_info;
  1965. if (in == out)
  1966. return true;
  1967. in_info = omap3isp_video_format_info(in);
  1968. out_info = omap3isp_video_format_info(out);
  1969. if ((in_info->flavor == 0) || (out_info->flavor == 0))
  1970. return false;
  1971. if (in_info->flavor != out_info->flavor)
  1972. return false;
  1973. return in_info->width - out_info->width + additional_shift <= 6;
  1974. }
  1975. static int ccdc_link_validate(struct v4l2_subdev *sd,
  1976. struct media_link *link,
  1977. struct v4l2_subdev_format *source_fmt,
  1978. struct v4l2_subdev_format *sink_fmt)
  1979. {
  1980. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  1981. unsigned long parallel_shift;
  1982. /* Check if the two ends match */
  1983. if (source_fmt->format.width != sink_fmt->format.width ||
  1984. source_fmt->format.height != sink_fmt->format.height)
  1985. return -EPIPE;
  1986. /* We've got a parallel sensor here. */
  1987. if (ccdc->input == CCDC_INPUT_PARALLEL) {
  1988. struct isp_parallel_platform_data *pdata =
  1989. &((struct isp_v4l2_subdevs_group *)
  1990. media_entity_to_v4l2_subdev(link->source->entity)
  1991. ->host_priv)->bus.parallel;
  1992. parallel_shift = pdata->data_lane_shift * 2;
  1993. } else {
  1994. parallel_shift = 0;
  1995. }
  1996. /* Lane shifter may be used to drop bits on CCDC sink pad */
  1997. if (!ccdc_is_shiftable(source_fmt->format.code,
  1998. sink_fmt->format.code, parallel_shift))
  1999. return -EPIPE;
  2000. return 0;
  2001. }
  2002. /*
  2003. * ccdc_init_formats - Initialize formats on all pads
  2004. * @sd: ISP CCDC V4L2 subdevice
  2005. * @fh: V4L2 subdev file handle
  2006. *
  2007. * Initialize all pad formats with default values. If fh is not NULL, try
  2008. * formats are initialized on the file handle. Otherwise active formats are
  2009. * initialized on the device.
  2010. */
  2011. static int ccdc_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  2012. {
  2013. struct v4l2_subdev_format format;
  2014. memset(&format, 0, sizeof(format));
  2015. format.pad = CCDC_PAD_SINK;
  2016. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  2017. format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  2018. format.format.width = 4096;
  2019. format.format.height = 4096;
  2020. ccdc_set_format(sd, fh, &format);
  2021. return 0;
  2022. }
  2023. /* V4L2 subdev core operations */
  2024. static const struct v4l2_subdev_core_ops ccdc_v4l2_core_ops = {
  2025. .ioctl = ccdc_ioctl,
  2026. .subscribe_event = ccdc_subscribe_event,
  2027. .unsubscribe_event = ccdc_unsubscribe_event,
  2028. };
  2029. /* V4L2 subdev video operations */
  2030. static const struct v4l2_subdev_video_ops ccdc_v4l2_video_ops = {
  2031. .s_stream = ccdc_set_stream,
  2032. };
  2033. /* V4L2 subdev pad operations */
  2034. static const struct v4l2_subdev_pad_ops ccdc_v4l2_pad_ops = {
  2035. .enum_mbus_code = ccdc_enum_mbus_code,
  2036. .enum_frame_size = ccdc_enum_frame_size,
  2037. .get_fmt = ccdc_get_format,
  2038. .set_fmt = ccdc_set_format,
  2039. .get_selection = ccdc_get_selection,
  2040. .set_selection = ccdc_set_selection,
  2041. .link_validate = ccdc_link_validate,
  2042. };
  2043. /* V4L2 subdev operations */
  2044. static const struct v4l2_subdev_ops ccdc_v4l2_ops = {
  2045. .core = &ccdc_v4l2_core_ops,
  2046. .video = &ccdc_v4l2_video_ops,
  2047. .pad = &ccdc_v4l2_pad_ops,
  2048. };
  2049. /* V4L2 subdev internal operations */
  2050. static const struct v4l2_subdev_internal_ops ccdc_v4l2_internal_ops = {
  2051. .open = ccdc_init_formats,
  2052. };
  2053. /* -----------------------------------------------------------------------------
  2054. * Media entity operations
  2055. */
  2056. /*
  2057. * ccdc_link_setup - Setup CCDC connections
  2058. * @entity: CCDC media entity
  2059. * @local: Pad at the local end of the link
  2060. * @remote: Pad at the remote end of the link
  2061. * @flags: Link flags
  2062. *
  2063. * return -EINVAL or zero on success
  2064. */
  2065. static int ccdc_link_setup(struct media_entity *entity,
  2066. const struct media_pad *local,
  2067. const struct media_pad *remote, u32 flags)
  2068. {
  2069. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  2070. struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
  2071. struct isp_device *isp = to_isp_device(ccdc);
  2072. switch (local->index | media_entity_type(remote->entity)) {
  2073. case CCDC_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
  2074. /* Read from the sensor (parallel interface), CCP2, CSI2a or
  2075. * CSI2c.
  2076. */
  2077. if (!(flags & MEDIA_LNK_FL_ENABLED)) {
  2078. ccdc->input = CCDC_INPUT_NONE;
  2079. break;
  2080. }
  2081. if (ccdc->input != CCDC_INPUT_NONE)
  2082. return -EBUSY;
  2083. if (remote->entity == &isp->isp_ccp2.subdev.entity)
  2084. ccdc->input = CCDC_INPUT_CCP2B;
  2085. else if (remote->entity == &isp->isp_csi2a.subdev.entity)
  2086. ccdc->input = CCDC_INPUT_CSI2A;
  2087. else if (remote->entity == &isp->isp_csi2c.subdev.entity)
  2088. ccdc->input = CCDC_INPUT_CSI2C;
  2089. else
  2090. ccdc->input = CCDC_INPUT_PARALLEL;
  2091. break;
  2092. /*
  2093. * The ISP core doesn't support pipelines with multiple video outputs.
  2094. * Revisit this when it will be implemented, and return -EBUSY for now.
  2095. */
  2096. case CCDC_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
  2097. /* Write to preview engine, histogram and H3A. When none of
  2098. * those links are active, the video port can be disabled.
  2099. */
  2100. if (flags & MEDIA_LNK_FL_ENABLED) {
  2101. if (ccdc->output & ~CCDC_OUTPUT_PREVIEW)
  2102. return -EBUSY;
  2103. ccdc->output |= CCDC_OUTPUT_PREVIEW;
  2104. } else {
  2105. ccdc->output &= ~CCDC_OUTPUT_PREVIEW;
  2106. }
  2107. break;
  2108. case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_DEVNODE:
  2109. /* Write to memory */
  2110. if (flags & MEDIA_LNK_FL_ENABLED) {
  2111. if (ccdc->output & ~CCDC_OUTPUT_MEMORY)
  2112. return -EBUSY;
  2113. ccdc->output |= CCDC_OUTPUT_MEMORY;
  2114. } else {
  2115. ccdc->output &= ~CCDC_OUTPUT_MEMORY;
  2116. }
  2117. break;
  2118. case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_V4L2_SUBDEV:
  2119. /* Write to resizer */
  2120. if (flags & MEDIA_LNK_FL_ENABLED) {
  2121. if (ccdc->output & ~CCDC_OUTPUT_RESIZER)
  2122. return -EBUSY;
  2123. ccdc->output |= CCDC_OUTPUT_RESIZER;
  2124. } else {
  2125. ccdc->output &= ~CCDC_OUTPUT_RESIZER;
  2126. }
  2127. break;
  2128. default:
  2129. return -EINVAL;
  2130. }
  2131. return 0;
  2132. }
  2133. /* media operations */
  2134. static const struct media_entity_operations ccdc_media_ops = {
  2135. .link_setup = ccdc_link_setup,
  2136. .link_validate = v4l2_subdev_link_validate,
  2137. };
  2138. void omap3isp_ccdc_unregister_entities(struct isp_ccdc_device *ccdc)
  2139. {
  2140. v4l2_device_unregister_subdev(&ccdc->subdev);
  2141. omap3isp_video_unregister(&ccdc->video_out);
  2142. }
  2143. int omap3isp_ccdc_register_entities(struct isp_ccdc_device *ccdc,
  2144. struct v4l2_device *vdev)
  2145. {
  2146. int ret;
  2147. /* Register the subdev and video node. */
  2148. ret = v4l2_device_register_subdev(vdev, &ccdc->subdev);
  2149. if (ret < 0)
  2150. goto error;
  2151. ret = omap3isp_video_register(&ccdc->video_out, vdev);
  2152. if (ret < 0)
  2153. goto error;
  2154. return 0;
  2155. error:
  2156. omap3isp_ccdc_unregister_entities(ccdc);
  2157. return ret;
  2158. }
  2159. /* -----------------------------------------------------------------------------
  2160. * ISP CCDC initialisation and cleanup
  2161. */
  2162. /*
  2163. * ccdc_init_entities - Initialize V4L2 subdev and media entity
  2164. * @ccdc: ISP CCDC module
  2165. *
  2166. * Return 0 on success and a negative error code on failure.
  2167. */
  2168. static int ccdc_init_entities(struct isp_ccdc_device *ccdc)
  2169. {
  2170. struct v4l2_subdev *sd = &ccdc->subdev;
  2171. struct media_pad *pads = ccdc->pads;
  2172. struct media_entity *me = &sd->entity;
  2173. int ret;
  2174. ccdc->input = CCDC_INPUT_NONE;
  2175. v4l2_subdev_init(sd, &ccdc_v4l2_ops);
  2176. sd->internal_ops = &ccdc_v4l2_internal_ops;
  2177. strlcpy(sd->name, "OMAP3 ISP CCDC", sizeof(sd->name));
  2178. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  2179. v4l2_set_subdevdata(sd, ccdc);
  2180. sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
  2181. pads[CCDC_PAD_SINK].flags = MEDIA_PAD_FL_SINK
  2182. | MEDIA_PAD_FL_MUST_CONNECT;
  2183. pads[CCDC_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
  2184. pads[CCDC_PAD_SOURCE_OF].flags = MEDIA_PAD_FL_SOURCE;
  2185. me->ops = &ccdc_media_ops;
  2186. ret = media_entity_init(me, CCDC_PADS_NUM, pads, 0);
  2187. if (ret < 0)
  2188. return ret;
  2189. ccdc_init_formats(sd, NULL);
  2190. ccdc->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2191. ccdc->video_out.ops = &ccdc_video_ops;
  2192. ccdc->video_out.isp = to_isp_device(ccdc);
  2193. ccdc->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
  2194. ccdc->video_out.bpl_alignment = 32;
  2195. ret = omap3isp_video_init(&ccdc->video_out, "CCDC");
  2196. if (ret < 0)
  2197. goto error_video;
  2198. /* Connect the CCDC subdev to the video node. */
  2199. ret = media_entity_create_link(&ccdc->subdev.entity, CCDC_PAD_SOURCE_OF,
  2200. &ccdc->video_out.video.entity, 0, 0);
  2201. if (ret < 0)
  2202. goto error_link;
  2203. return 0;
  2204. error_link:
  2205. omap3isp_video_cleanup(&ccdc->video_out);
  2206. error_video:
  2207. media_entity_cleanup(me);
  2208. return ret;
  2209. }
  2210. /*
  2211. * omap3isp_ccdc_init - CCDC module initialization.
  2212. * @isp: Device pointer specific to the OMAP3 ISP.
  2213. *
  2214. * TODO: Get the initialisation values from platform data.
  2215. *
  2216. * Return 0 on success or a negative error code otherwise.
  2217. */
  2218. int omap3isp_ccdc_init(struct isp_device *isp)
  2219. {
  2220. struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
  2221. int ret;
  2222. spin_lock_init(&ccdc->lock);
  2223. init_waitqueue_head(&ccdc->wait);
  2224. mutex_init(&ccdc->ioctl_lock);
  2225. ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
  2226. INIT_WORK(&ccdc->lsc.table_work, ccdc_lsc_free_table_work);
  2227. ccdc->lsc.state = LSC_STATE_STOPPED;
  2228. INIT_LIST_HEAD(&ccdc->lsc.free_queue);
  2229. spin_lock_init(&ccdc->lsc.req_lock);
  2230. ccdc->clamp.oblen = 0;
  2231. ccdc->clamp.dcsubval = 0;
  2232. ccdc->update = OMAP3ISP_CCDC_BLCLAMP;
  2233. ccdc_apply_controls(ccdc);
  2234. ret = ccdc_init_entities(ccdc);
  2235. if (ret < 0) {
  2236. mutex_destroy(&ccdc->ioctl_lock);
  2237. return ret;
  2238. }
  2239. return 0;
  2240. }
  2241. /*
  2242. * omap3isp_ccdc_cleanup - CCDC module cleanup.
  2243. * @isp: Device pointer specific to the OMAP3 ISP.
  2244. */
  2245. void omap3isp_ccdc_cleanup(struct isp_device *isp)
  2246. {
  2247. struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
  2248. omap3isp_video_cleanup(&ccdc->video_out);
  2249. media_entity_cleanup(&ccdc->subdev.entity);
  2250. /* Free LSC requests. As the CCDC is stopped there's no active request,
  2251. * so only the pending request and the free queue need to be handled.
  2252. */
  2253. ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
  2254. cancel_work_sync(&ccdc->lsc.table_work);
  2255. ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
  2256. if (ccdc->fpc.addr != NULL)
  2257. dma_free_coherent(isp->dev, ccdc->fpc.fpnum * 4, ccdc->fpc.addr,
  2258. ccdc->fpc.dma);
  2259. mutex_destroy(&ccdc->ioctl_lock);
  2260. }