ispccdc.c 76 KB

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