ispccdc.c 69 KB

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