ispccdc.c 77 KB

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