ispccdc.c 63 KB

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