ispcsi2.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /*
  2. * ispcsi2.c
  3. *
  4. * TI OMAP3 ISP - CSI2 module
  5. *
  6. * Copyright (C) 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/delay.h>
  27. #include <media/v4l2-common.h>
  28. #include <linux/v4l2-mediabus.h>
  29. #include <linux/mm.h>
  30. #include "isp.h"
  31. #include "ispreg.h"
  32. #include "ispcsi2.h"
  33. /*
  34. * csi2_if_enable - Enable CSI2 Receiver interface.
  35. * @enable: enable flag
  36. *
  37. */
  38. static void csi2_if_enable(struct isp_device *isp,
  39. struct isp_csi2_device *csi2, u8 enable)
  40. {
  41. struct isp_csi2_ctrl_cfg *currctrl = &csi2->ctrl;
  42. isp_reg_clr_set(isp, csi2->regs1, ISPCSI2_CTRL, ISPCSI2_CTRL_IF_EN,
  43. enable ? ISPCSI2_CTRL_IF_EN : 0);
  44. currctrl->if_enable = enable;
  45. }
  46. /*
  47. * csi2_recv_config - CSI2 receiver module configuration.
  48. * @currctrl: isp_csi2_ctrl_cfg structure
  49. *
  50. */
  51. static void csi2_recv_config(struct isp_device *isp,
  52. struct isp_csi2_device *csi2,
  53. struct isp_csi2_ctrl_cfg *currctrl)
  54. {
  55. u32 reg;
  56. reg = isp_reg_readl(isp, csi2->regs1, ISPCSI2_CTRL);
  57. if (currctrl->frame_mode)
  58. reg |= ISPCSI2_CTRL_FRAME;
  59. else
  60. reg &= ~ISPCSI2_CTRL_FRAME;
  61. if (currctrl->vp_clk_enable)
  62. reg |= ISPCSI2_CTRL_VP_CLK_EN;
  63. else
  64. reg &= ~ISPCSI2_CTRL_VP_CLK_EN;
  65. if (currctrl->vp_only_enable)
  66. reg |= ISPCSI2_CTRL_VP_ONLY_EN;
  67. else
  68. reg &= ~ISPCSI2_CTRL_VP_ONLY_EN;
  69. reg &= ~ISPCSI2_CTRL_VP_OUT_CTRL_MASK;
  70. reg |= currctrl->vp_out_ctrl << ISPCSI2_CTRL_VP_OUT_CTRL_SHIFT;
  71. if (currctrl->ecc_enable)
  72. reg |= ISPCSI2_CTRL_ECC_EN;
  73. else
  74. reg &= ~ISPCSI2_CTRL_ECC_EN;
  75. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_CTRL);
  76. }
  77. static const unsigned int csi2_input_fmts[] = {
  78. V4L2_MBUS_FMT_SGRBG10_1X10,
  79. V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
  80. V4L2_MBUS_FMT_SRGGB10_1X10,
  81. V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8,
  82. V4L2_MBUS_FMT_SBGGR10_1X10,
  83. V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8,
  84. V4L2_MBUS_FMT_SGBRG10_1X10,
  85. V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8,
  86. };
  87. /* To set the format on the CSI2 requires a mapping function that takes
  88. * the following inputs:
  89. * - 2 different formats (at this time)
  90. * - 2 destinations (mem, vp+mem) (vp only handled separately)
  91. * - 2 decompression options (on, off)
  92. * - 2 isp revisions (certain format must be handled differently on OMAP3630)
  93. * Output should be CSI2 frame format code
  94. * Array indices as follows: [format][dest][decompr][is_3630]
  95. * Not all combinations are valid. 0 means invalid.
  96. */
  97. static const u16 __csi2_fmt_map[2][2][2][2] = {
  98. /* RAW10 formats */
  99. {
  100. /* Output to memory */
  101. {
  102. /* No DPCM decompression */
  103. { CSI2_PIX_FMT_RAW10_EXP16, CSI2_PIX_FMT_RAW10_EXP16 },
  104. /* DPCM decompression */
  105. { 0, 0 },
  106. },
  107. /* Output to both */
  108. {
  109. /* No DPCM decompression */
  110. { CSI2_PIX_FMT_RAW10_EXP16_VP,
  111. CSI2_PIX_FMT_RAW10_EXP16_VP },
  112. /* DPCM decompression */
  113. { 0, 0 },
  114. },
  115. },
  116. /* RAW10 DPCM8 formats */
  117. {
  118. /* Output to memory */
  119. {
  120. /* No DPCM decompression */
  121. { CSI2_PIX_FMT_RAW8, CSI2_USERDEF_8BIT_DATA1 },
  122. /* DPCM decompression */
  123. { CSI2_PIX_FMT_RAW8_DPCM10_EXP16,
  124. CSI2_USERDEF_8BIT_DATA1_DPCM10 },
  125. },
  126. /* Output to both */
  127. {
  128. /* No DPCM decompression */
  129. { CSI2_PIX_FMT_RAW8_VP,
  130. CSI2_PIX_FMT_RAW8_VP },
  131. /* DPCM decompression */
  132. { CSI2_PIX_FMT_RAW8_DPCM10_VP,
  133. CSI2_USERDEF_8BIT_DATA1_DPCM10_VP },
  134. },
  135. },
  136. };
  137. /*
  138. * csi2_ctx_map_format - Map CSI2 sink media bus format to CSI2 format ID
  139. * @csi2: ISP CSI2 device
  140. *
  141. * Returns CSI2 physical format id
  142. */
  143. static u16 csi2_ctx_map_format(struct isp_csi2_device *csi2)
  144. {
  145. const struct v4l2_mbus_framefmt *fmt = &csi2->formats[CSI2_PAD_SINK];
  146. int fmtidx, destidx, is_3630;
  147. switch (fmt->code) {
  148. case V4L2_MBUS_FMT_SGRBG10_1X10:
  149. case V4L2_MBUS_FMT_SRGGB10_1X10:
  150. case V4L2_MBUS_FMT_SBGGR10_1X10:
  151. case V4L2_MBUS_FMT_SGBRG10_1X10:
  152. fmtidx = 0;
  153. break;
  154. case V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8:
  155. case V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8:
  156. case V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8:
  157. case V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8:
  158. fmtidx = 1;
  159. break;
  160. default:
  161. WARN(1, KERN_ERR "CSI2: pixel format %08x unsupported!\n",
  162. fmt->code);
  163. return 0;
  164. }
  165. if (!(csi2->output & CSI2_OUTPUT_CCDC) &&
  166. !(csi2->output & CSI2_OUTPUT_MEMORY)) {
  167. /* Neither output enabled is a valid combination */
  168. return CSI2_PIX_FMT_OTHERS;
  169. }
  170. /* If we need to skip frames at the beginning of the stream disable the
  171. * video port to avoid sending the skipped frames to the CCDC.
  172. */
  173. destidx = csi2->frame_skip ? 0 : !!(csi2->output & CSI2_OUTPUT_CCDC);
  174. is_3630 = csi2->isp->revision == ISP_REVISION_15_0;
  175. return __csi2_fmt_map[fmtidx][destidx][csi2->dpcm_decompress][is_3630];
  176. }
  177. /*
  178. * csi2_set_outaddr - Set memory address to save output image
  179. * @csi2: Pointer to ISP CSI2a device.
  180. * @addr: ISP MMU Mapped 32-bit memory address aligned on 32 byte boundary.
  181. *
  182. * Sets the memory address where the output will be saved.
  183. *
  184. * Returns 0 if successful, or -EINVAL if the address is not in the 32 byte
  185. * boundary.
  186. */
  187. static void csi2_set_outaddr(struct isp_csi2_device *csi2, u32 addr)
  188. {
  189. struct isp_device *isp = csi2->isp;
  190. struct isp_csi2_ctx_cfg *ctx = &csi2->contexts[0];
  191. ctx->ping_addr = addr;
  192. ctx->pong_addr = addr;
  193. isp_reg_writel(isp, ctx->ping_addr,
  194. csi2->regs1, ISPCSI2_CTX_DAT_PING_ADDR(ctx->ctxnum));
  195. isp_reg_writel(isp, ctx->pong_addr,
  196. csi2->regs1, ISPCSI2_CTX_DAT_PONG_ADDR(ctx->ctxnum));
  197. }
  198. /*
  199. * is_usr_def_mapping - Checks whether USER_DEF_MAPPING should
  200. * be enabled by CSI2.
  201. * @format_id: mapped format id
  202. *
  203. */
  204. static inline int is_usr_def_mapping(u32 format_id)
  205. {
  206. return (format_id & 0x40) ? 1 : 0;
  207. }
  208. /*
  209. * csi2_ctx_enable - Enable specified CSI2 context
  210. * @ctxnum: Context number, valid between 0 and 7 values.
  211. * @enable: enable
  212. *
  213. */
  214. static void csi2_ctx_enable(struct isp_device *isp,
  215. struct isp_csi2_device *csi2, u8 ctxnum, u8 enable)
  216. {
  217. struct isp_csi2_ctx_cfg *ctx = &csi2->contexts[ctxnum];
  218. unsigned int skip = 0;
  219. u32 reg;
  220. reg = isp_reg_readl(isp, csi2->regs1, ISPCSI2_CTX_CTRL1(ctxnum));
  221. if (enable) {
  222. if (csi2->frame_skip)
  223. skip = csi2->frame_skip;
  224. else if (csi2->output & CSI2_OUTPUT_MEMORY)
  225. skip = 1;
  226. reg &= ~ISPCSI2_CTX_CTRL1_COUNT_MASK;
  227. reg |= ISPCSI2_CTX_CTRL1_COUNT_UNLOCK
  228. | (skip << ISPCSI2_CTX_CTRL1_COUNT_SHIFT)
  229. | ISPCSI2_CTX_CTRL1_CTX_EN;
  230. } else {
  231. reg &= ~ISPCSI2_CTX_CTRL1_CTX_EN;
  232. }
  233. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_CTX_CTRL1(ctxnum));
  234. ctx->enabled = enable;
  235. }
  236. /*
  237. * csi2_ctx_config - CSI2 context configuration.
  238. * @ctx: context configuration
  239. *
  240. */
  241. static void csi2_ctx_config(struct isp_device *isp,
  242. struct isp_csi2_device *csi2,
  243. struct isp_csi2_ctx_cfg *ctx)
  244. {
  245. u32 reg;
  246. /* Set up CSI2_CTx_CTRL1 */
  247. reg = isp_reg_readl(isp, csi2->regs1, ISPCSI2_CTX_CTRL1(ctx->ctxnum));
  248. if (ctx->eof_enabled)
  249. reg |= ISPCSI2_CTX_CTRL1_EOF_EN;
  250. else
  251. reg &= ~ISPCSI2_CTX_CTRL1_EOF_EN;
  252. if (ctx->eol_enabled)
  253. reg |= ISPCSI2_CTX_CTRL1_EOL_EN;
  254. else
  255. reg &= ~ISPCSI2_CTX_CTRL1_EOL_EN;
  256. if (ctx->checksum_enabled)
  257. reg |= ISPCSI2_CTX_CTRL1_CS_EN;
  258. else
  259. reg &= ~ISPCSI2_CTX_CTRL1_CS_EN;
  260. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_CTX_CTRL1(ctx->ctxnum));
  261. /* Set up CSI2_CTx_CTRL2 */
  262. reg = isp_reg_readl(isp, csi2->regs1, ISPCSI2_CTX_CTRL2(ctx->ctxnum));
  263. reg &= ~(ISPCSI2_CTX_CTRL2_VIRTUAL_ID_MASK);
  264. reg |= ctx->virtual_id << ISPCSI2_CTX_CTRL2_VIRTUAL_ID_SHIFT;
  265. reg &= ~(ISPCSI2_CTX_CTRL2_FORMAT_MASK);
  266. reg |= ctx->format_id << ISPCSI2_CTX_CTRL2_FORMAT_SHIFT;
  267. if (ctx->dpcm_decompress) {
  268. if (ctx->dpcm_predictor)
  269. reg |= ISPCSI2_CTX_CTRL2_DPCM_PRED;
  270. else
  271. reg &= ~ISPCSI2_CTX_CTRL2_DPCM_PRED;
  272. }
  273. if (is_usr_def_mapping(ctx->format_id)) {
  274. reg &= ~ISPCSI2_CTX_CTRL2_USER_DEF_MAP_MASK;
  275. reg |= 2 << ISPCSI2_CTX_CTRL2_USER_DEF_MAP_SHIFT;
  276. }
  277. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_CTX_CTRL2(ctx->ctxnum));
  278. /* Set up CSI2_CTx_CTRL3 */
  279. reg = isp_reg_readl(isp, csi2->regs1, ISPCSI2_CTX_CTRL3(ctx->ctxnum));
  280. reg &= ~(ISPCSI2_CTX_CTRL3_ALPHA_MASK);
  281. reg |= (ctx->alpha << ISPCSI2_CTX_CTRL3_ALPHA_SHIFT);
  282. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_CTX_CTRL3(ctx->ctxnum));
  283. /* Set up CSI2_CTx_DAT_OFST */
  284. reg = isp_reg_readl(isp, csi2->regs1,
  285. ISPCSI2_CTX_DAT_OFST(ctx->ctxnum));
  286. reg &= ~ISPCSI2_CTX_DAT_OFST_OFST_MASK;
  287. reg |= ctx->data_offset << ISPCSI2_CTX_DAT_OFST_OFST_SHIFT;
  288. isp_reg_writel(isp, reg, csi2->regs1,
  289. ISPCSI2_CTX_DAT_OFST(ctx->ctxnum));
  290. isp_reg_writel(isp, ctx->ping_addr,
  291. csi2->regs1, ISPCSI2_CTX_DAT_PING_ADDR(ctx->ctxnum));
  292. isp_reg_writel(isp, ctx->pong_addr,
  293. csi2->regs1, ISPCSI2_CTX_DAT_PONG_ADDR(ctx->ctxnum));
  294. }
  295. /*
  296. * csi2_timing_config - CSI2 timing configuration.
  297. * @timing: csi2_timing_cfg structure
  298. */
  299. static void csi2_timing_config(struct isp_device *isp,
  300. struct isp_csi2_device *csi2,
  301. struct isp_csi2_timing_cfg *timing)
  302. {
  303. u32 reg;
  304. reg = isp_reg_readl(isp, csi2->regs1, ISPCSI2_TIMING);
  305. if (timing->force_rx_mode)
  306. reg |= ISPCSI2_TIMING_FORCE_RX_MODE_IO(timing->ionum);
  307. else
  308. reg &= ~ISPCSI2_TIMING_FORCE_RX_MODE_IO(timing->ionum);
  309. if (timing->stop_state_16x)
  310. reg |= ISPCSI2_TIMING_STOP_STATE_X16_IO(timing->ionum);
  311. else
  312. reg &= ~ISPCSI2_TIMING_STOP_STATE_X16_IO(timing->ionum);
  313. if (timing->stop_state_4x)
  314. reg |= ISPCSI2_TIMING_STOP_STATE_X4_IO(timing->ionum);
  315. else
  316. reg &= ~ISPCSI2_TIMING_STOP_STATE_X4_IO(timing->ionum);
  317. reg &= ~ISPCSI2_TIMING_STOP_STATE_COUNTER_IO_MASK(timing->ionum);
  318. reg |= timing->stop_state_counter <<
  319. ISPCSI2_TIMING_STOP_STATE_COUNTER_IO_SHIFT(timing->ionum);
  320. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_TIMING);
  321. }
  322. /*
  323. * csi2_irq_ctx_set - Enables CSI2 Context IRQs.
  324. * @enable: Enable/disable CSI2 Context interrupts
  325. */
  326. static void csi2_irq_ctx_set(struct isp_device *isp,
  327. struct isp_csi2_device *csi2, int enable)
  328. {
  329. u32 reg = ISPCSI2_CTX_IRQSTATUS_FE_IRQ;
  330. int i;
  331. if (csi2->use_fs_irq)
  332. reg |= ISPCSI2_CTX_IRQSTATUS_FS_IRQ;
  333. for (i = 0; i < 8; i++) {
  334. isp_reg_writel(isp, reg, csi2->regs1,
  335. ISPCSI2_CTX_IRQSTATUS(i));
  336. if (enable)
  337. isp_reg_set(isp, csi2->regs1, ISPCSI2_CTX_IRQENABLE(i),
  338. reg);
  339. else
  340. isp_reg_clr(isp, csi2->regs1, ISPCSI2_CTX_IRQENABLE(i),
  341. reg);
  342. }
  343. }
  344. /*
  345. * csi2_irq_complexio1_set - Enables CSI2 ComplexIO IRQs.
  346. * @enable: Enable/disable CSI2 ComplexIO #1 interrupts
  347. */
  348. static void csi2_irq_complexio1_set(struct isp_device *isp,
  349. struct isp_csi2_device *csi2, int enable)
  350. {
  351. u32 reg;
  352. reg = ISPCSI2_PHY_IRQENABLE_STATEALLULPMEXIT |
  353. ISPCSI2_PHY_IRQENABLE_STATEALLULPMENTER |
  354. ISPCSI2_PHY_IRQENABLE_STATEULPM5 |
  355. ISPCSI2_PHY_IRQENABLE_ERRCONTROL5 |
  356. ISPCSI2_PHY_IRQENABLE_ERRESC5 |
  357. ISPCSI2_PHY_IRQENABLE_ERRSOTSYNCHS5 |
  358. ISPCSI2_PHY_IRQENABLE_ERRSOTHS5 |
  359. ISPCSI2_PHY_IRQENABLE_STATEULPM4 |
  360. ISPCSI2_PHY_IRQENABLE_ERRCONTROL4 |
  361. ISPCSI2_PHY_IRQENABLE_ERRESC4 |
  362. ISPCSI2_PHY_IRQENABLE_ERRSOTSYNCHS4 |
  363. ISPCSI2_PHY_IRQENABLE_ERRSOTHS4 |
  364. ISPCSI2_PHY_IRQENABLE_STATEULPM3 |
  365. ISPCSI2_PHY_IRQENABLE_ERRCONTROL3 |
  366. ISPCSI2_PHY_IRQENABLE_ERRESC3 |
  367. ISPCSI2_PHY_IRQENABLE_ERRSOTSYNCHS3 |
  368. ISPCSI2_PHY_IRQENABLE_ERRSOTHS3 |
  369. ISPCSI2_PHY_IRQENABLE_STATEULPM2 |
  370. ISPCSI2_PHY_IRQENABLE_ERRCONTROL2 |
  371. ISPCSI2_PHY_IRQENABLE_ERRESC2 |
  372. ISPCSI2_PHY_IRQENABLE_ERRSOTSYNCHS2 |
  373. ISPCSI2_PHY_IRQENABLE_ERRSOTHS2 |
  374. ISPCSI2_PHY_IRQENABLE_STATEULPM1 |
  375. ISPCSI2_PHY_IRQENABLE_ERRCONTROL1 |
  376. ISPCSI2_PHY_IRQENABLE_ERRESC1 |
  377. ISPCSI2_PHY_IRQENABLE_ERRSOTSYNCHS1 |
  378. ISPCSI2_PHY_IRQENABLE_ERRSOTHS1;
  379. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_PHY_IRQSTATUS);
  380. if (enable)
  381. reg |= isp_reg_readl(isp, csi2->regs1, ISPCSI2_PHY_IRQENABLE);
  382. else
  383. reg = 0;
  384. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_PHY_IRQENABLE);
  385. }
  386. /*
  387. * csi2_irq_status_set - Enables CSI2 Status IRQs.
  388. * @enable: Enable/disable CSI2 Status interrupts
  389. */
  390. static void csi2_irq_status_set(struct isp_device *isp,
  391. struct isp_csi2_device *csi2, int enable)
  392. {
  393. u32 reg;
  394. reg = ISPCSI2_IRQSTATUS_OCP_ERR_IRQ |
  395. ISPCSI2_IRQSTATUS_SHORT_PACKET_IRQ |
  396. ISPCSI2_IRQSTATUS_ECC_CORRECTION_IRQ |
  397. ISPCSI2_IRQSTATUS_ECC_NO_CORRECTION_IRQ |
  398. ISPCSI2_IRQSTATUS_COMPLEXIO2_ERR_IRQ |
  399. ISPCSI2_IRQSTATUS_COMPLEXIO1_ERR_IRQ |
  400. ISPCSI2_IRQSTATUS_FIFO_OVF_IRQ |
  401. ISPCSI2_IRQSTATUS_CONTEXT(0);
  402. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_IRQSTATUS);
  403. if (enable)
  404. reg |= isp_reg_readl(isp, csi2->regs1, ISPCSI2_IRQENABLE);
  405. else
  406. reg = 0;
  407. isp_reg_writel(isp, reg, csi2->regs1, ISPCSI2_IRQENABLE);
  408. }
  409. /*
  410. * omap3isp_csi2_reset - Resets the CSI2 module.
  411. *
  412. * Must be called with the phy lock held.
  413. *
  414. * Returns 0 if successful, or -EBUSY if power command didn't respond.
  415. */
  416. int omap3isp_csi2_reset(struct isp_csi2_device *csi2)
  417. {
  418. struct isp_device *isp = csi2->isp;
  419. u8 soft_reset_retries = 0;
  420. u32 reg;
  421. int i;
  422. if (!csi2->available)
  423. return -ENODEV;
  424. if (csi2->phy->phy_in_use)
  425. return -EBUSY;
  426. isp_reg_set(isp, csi2->regs1, ISPCSI2_SYSCONFIG,
  427. ISPCSI2_SYSCONFIG_SOFT_RESET);
  428. do {
  429. reg = isp_reg_readl(isp, csi2->regs1, ISPCSI2_SYSSTATUS) &
  430. ISPCSI2_SYSSTATUS_RESET_DONE;
  431. if (reg == ISPCSI2_SYSSTATUS_RESET_DONE)
  432. break;
  433. soft_reset_retries++;
  434. if (soft_reset_retries < 5)
  435. udelay(100);
  436. } while (soft_reset_retries < 5);
  437. if (soft_reset_retries == 5) {
  438. printk(KERN_ERR "CSI2: Soft reset try count exceeded!\n");
  439. return -EBUSY;
  440. }
  441. if (isp->revision == ISP_REVISION_15_0)
  442. isp_reg_set(isp, csi2->regs1, ISPCSI2_PHY_CFG,
  443. ISPCSI2_PHY_CFG_RESET_CTRL);
  444. i = 100;
  445. do {
  446. reg = isp_reg_readl(isp, csi2->phy->phy_regs, ISPCSIPHY_REG1)
  447. & ISPCSIPHY_REG1_RESET_DONE_CTRLCLK;
  448. if (reg == ISPCSIPHY_REG1_RESET_DONE_CTRLCLK)
  449. break;
  450. udelay(100);
  451. } while (--i > 0);
  452. if (i == 0) {
  453. printk(KERN_ERR
  454. "CSI2: Reset for CSI2_96M_FCLK domain Failed!\n");
  455. return -EBUSY;
  456. }
  457. if (isp->autoidle)
  458. isp_reg_clr_set(isp, csi2->regs1, ISPCSI2_SYSCONFIG,
  459. ISPCSI2_SYSCONFIG_MSTANDBY_MODE_MASK |
  460. ISPCSI2_SYSCONFIG_AUTO_IDLE,
  461. ISPCSI2_SYSCONFIG_MSTANDBY_MODE_SMART |
  462. ((isp->revision == ISP_REVISION_15_0) ?
  463. ISPCSI2_SYSCONFIG_AUTO_IDLE : 0));
  464. else
  465. isp_reg_clr_set(isp, csi2->regs1, ISPCSI2_SYSCONFIG,
  466. ISPCSI2_SYSCONFIG_MSTANDBY_MODE_MASK |
  467. ISPCSI2_SYSCONFIG_AUTO_IDLE,
  468. ISPCSI2_SYSCONFIG_MSTANDBY_MODE_NO);
  469. return 0;
  470. }
  471. static int csi2_configure(struct isp_csi2_device *csi2)
  472. {
  473. const struct isp_v4l2_subdevs_group *pdata;
  474. struct isp_device *isp = csi2->isp;
  475. struct isp_csi2_timing_cfg *timing = &csi2->timing[0];
  476. struct v4l2_subdev *sensor;
  477. struct media_pad *pad;
  478. /*
  479. * CSI2 fields that can be updated while the context has
  480. * been enabled or the interface has been enabled are not
  481. * updated dynamically currently. So we do not allow to
  482. * reconfigure if either has been enabled
  483. */
  484. if (csi2->contexts[0].enabled || csi2->ctrl.if_enable)
  485. return -EBUSY;
  486. pad = media_entity_remote_source(&csi2->pads[CSI2_PAD_SINK]);
  487. sensor = media_entity_to_v4l2_subdev(pad->entity);
  488. pdata = sensor->host_priv;
  489. csi2->frame_skip = 0;
  490. v4l2_subdev_call(sensor, sensor, g_skip_frames, &csi2->frame_skip);
  491. csi2->ctrl.vp_out_ctrl = pdata->bus.csi2.vpclk_div;
  492. csi2->ctrl.frame_mode = ISP_CSI2_FRAME_IMMEDIATE;
  493. csi2->ctrl.ecc_enable = pdata->bus.csi2.crc;
  494. timing->ionum = 1;
  495. timing->force_rx_mode = 1;
  496. timing->stop_state_16x = 1;
  497. timing->stop_state_4x = 1;
  498. timing->stop_state_counter = 0x1FF;
  499. /*
  500. * The CSI2 receiver can't do any format conversion except DPCM
  501. * decompression, so every set_format call configures both pads
  502. * and enables DPCM decompression as a special case:
  503. */
  504. if (csi2->formats[CSI2_PAD_SINK].code !=
  505. csi2->formats[CSI2_PAD_SOURCE].code)
  506. csi2->dpcm_decompress = true;
  507. else
  508. csi2->dpcm_decompress = false;
  509. csi2->contexts[0].format_id = csi2_ctx_map_format(csi2);
  510. if (csi2->video_out.bpl_padding == 0)
  511. csi2->contexts[0].data_offset = 0;
  512. else
  513. csi2->contexts[0].data_offset = csi2->video_out.bpl_value;
  514. /*
  515. * Enable end of frame and end of line signals generation for
  516. * context 0. These signals are generated from CSI2 receiver to
  517. * qualify the last pixel of a frame and the last pixel of a line.
  518. * Without enabling the signals CSI2 receiver writes data to memory
  519. * beyond buffer size and/or data line offset is not handled correctly.
  520. */
  521. csi2->contexts[0].eof_enabled = 1;
  522. csi2->contexts[0].eol_enabled = 1;
  523. csi2_irq_complexio1_set(isp, csi2, 1);
  524. csi2_irq_ctx_set(isp, csi2, 1);
  525. csi2_irq_status_set(isp, csi2, 1);
  526. /* Set configuration (timings, format and links) */
  527. csi2_timing_config(isp, csi2, timing);
  528. csi2_recv_config(isp, csi2, &csi2->ctrl);
  529. csi2_ctx_config(isp, csi2, &csi2->contexts[0]);
  530. return 0;
  531. }
  532. /*
  533. * csi2_print_status - Prints CSI2 debug information.
  534. */
  535. #define CSI2_PRINT_REGISTER(isp, regs, name)\
  536. dev_dbg(isp->dev, "###CSI2 " #name "=0x%08x\n", \
  537. isp_reg_readl(isp, regs, ISPCSI2_##name))
  538. static void csi2_print_status(struct isp_csi2_device *csi2)
  539. {
  540. struct isp_device *isp = csi2->isp;
  541. if (!csi2->available)
  542. return;
  543. dev_dbg(isp->dev, "-------------CSI2 Register dump-------------\n");
  544. CSI2_PRINT_REGISTER(isp, csi2->regs1, SYSCONFIG);
  545. CSI2_PRINT_REGISTER(isp, csi2->regs1, SYSSTATUS);
  546. CSI2_PRINT_REGISTER(isp, csi2->regs1, IRQENABLE);
  547. CSI2_PRINT_REGISTER(isp, csi2->regs1, IRQSTATUS);
  548. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTRL);
  549. CSI2_PRINT_REGISTER(isp, csi2->regs1, DBG_H);
  550. CSI2_PRINT_REGISTER(isp, csi2->regs1, GNQ);
  551. CSI2_PRINT_REGISTER(isp, csi2->regs1, PHY_CFG);
  552. CSI2_PRINT_REGISTER(isp, csi2->regs1, PHY_IRQSTATUS);
  553. CSI2_PRINT_REGISTER(isp, csi2->regs1, SHORT_PACKET);
  554. CSI2_PRINT_REGISTER(isp, csi2->regs1, PHY_IRQENABLE);
  555. CSI2_PRINT_REGISTER(isp, csi2->regs1, DBG_P);
  556. CSI2_PRINT_REGISTER(isp, csi2->regs1, TIMING);
  557. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_CTRL1(0));
  558. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_CTRL2(0));
  559. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_DAT_OFST(0));
  560. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_DAT_PING_ADDR(0));
  561. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_DAT_PONG_ADDR(0));
  562. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_IRQENABLE(0));
  563. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_IRQSTATUS(0));
  564. CSI2_PRINT_REGISTER(isp, csi2->regs1, CTX_CTRL3(0));
  565. dev_dbg(isp->dev, "--------------------------------------------\n");
  566. }
  567. /* -----------------------------------------------------------------------------
  568. * Interrupt handling
  569. */
  570. /*
  571. * csi2_isr_buffer - Does buffer handling at end-of-frame
  572. * when writing to memory.
  573. */
  574. static void csi2_isr_buffer(struct isp_csi2_device *csi2)
  575. {
  576. struct isp_device *isp = csi2->isp;
  577. struct isp_buffer *buffer;
  578. csi2_ctx_enable(isp, csi2, 0, 0);
  579. buffer = omap3isp_video_buffer_next(&csi2->video_out);
  580. /*
  581. * Let video queue operation restart engine if there is an underrun
  582. * condition.
  583. */
  584. if (buffer == NULL)
  585. return;
  586. csi2_set_outaddr(csi2, buffer->isp_addr);
  587. csi2_ctx_enable(isp, csi2, 0, 1);
  588. }
  589. static void csi2_isr_ctx(struct isp_csi2_device *csi2,
  590. struct isp_csi2_ctx_cfg *ctx)
  591. {
  592. struct isp_device *isp = csi2->isp;
  593. unsigned int n = ctx->ctxnum;
  594. u32 status;
  595. status = isp_reg_readl(isp, csi2->regs1, ISPCSI2_CTX_IRQSTATUS(n));
  596. isp_reg_writel(isp, status, csi2->regs1, ISPCSI2_CTX_IRQSTATUS(n));
  597. /* Propagate frame number */
  598. if (status & ISPCSI2_CTX_IRQSTATUS_FS_IRQ) {
  599. struct isp_pipeline *pipe =
  600. to_isp_pipeline(&csi2->subdev.entity);
  601. if (pipe->do_propagation)
  602. atomic_inc(&pipe->frame_number);
  603. }
  604. if (!(status & ISPCSI2_CTX_IRQSTATUS_FE_IRQ))
  605. return;
  606. /* Skip interrupts until we reach the frame skip count. The CSI2 will be
  607. * automatically disabled, as the frame skip count has been programmed
  608. * in the CSI2_CTx_CTRL1::COUNT field, so reenable it.
  609. *
  610. * It would have been nice to rely on the FRAME_NUMBER interrupt instead
  611. * but it turned out that the interrupt is only generated when the CSI2
  612. * writes to memory (the CSI2_CTx_CTRL1::COUNT field is decreased
  613. * correctly and reaches 0 when data is forwarded to the video port only
  614. * but no interrupt arrives). Maybe a CSI2 hardware bug.
  615. */
  616. if (csi2->frame_skip) {
  617. csi2->frame_skip--;
  618. if (csi2->frame_skip == 0) {
  619. ctx->format_id = csi2_ctx_map_format(csi2);
  620. csi2_ctx_config(isp, csi2, ctx);
  621. csi2_ctx_enable(isp, csi2, n, 1);
  622. }
  623. return;
  624. }
  625. if (csi2->output & CSI2_OUTPUT_MEMORY)
  626. csi2_isr_buffer(csi2);
  627. }
  628. /*
  629. * omap3isp_csi2_isr - CSI2 interrupt handling.
  630. */
  631. void omap3isp_csi2_isr(struct isp_csi2_device *csi2)
  632. {
  633. struct isp_pipeline *pipe = to_isp_pipeline(&csi2->subdev.entity);
  634. u32 csi2_irqstatus, cpxio1_irqstatus;
  635. struct isp_device *isp = csi2->isp;
  636. if (!csi2->available)
  637. return;
  638. csi2_irqstatus = isp_reg_readl(isp, csi2->regs1, ISPCSI2_IRQSTATUS);
  639. isp_reg_writel(isp, csi2_irqstatus, csi2->regs1, ISPCSI2_IRQSTATUS);
  640. /* Failure Cases */
  641. if (csi2_irqstatus & ISPCSI2_IRQSTATUS_COMPLEXIO1_ERR_IRQ) {
  642. cpxio1_irqstatus = isp_reg_readl(isp, csi2->regs1,
  643. ISPCSI2_PHY_IRQSTATUS);
  644. isp_reg_writel(isp, cpxio1_irqstatus,
  645. csi2->regs1, ISPCSI2_PHY_IRQSTATUS);
  646. dev_dbg(isp->dev, "CSI2: ComplexIO Error IRQ "
  647. "%x\n", cpxio1_irqstatus);
  648. pipe->error = true;
  649. }
  650. if (csi2_irqstatus & (ISPCSI2_IRQSTATUS_OCP_ERR_IRQ |
  651. ISPCSI2_IRQSTATUS_SHORT_PACKET_IRQ |
  652. ISPCSI2_IRQSTATUS_ECC_NO_CORRECTION_IRQ |
  653. ISPCSI2_IRQSTATUS_COMPLEXIO2_ERR_IRQ |
  654. ISPCSI2_IRQSTATUS_FIFO_OVF_IRQ)) {
  655. dev_dbg(isp->dev, "CSI2 Err:"
  656. " OCP:%d,"
  657. " Short_pack:%d,"
  658. " ECC:%d,"
  659. " CPXIO2:%d,"
  660. " FIFO_OVF:%d,"
  661. "\n",
  662. (csi2_irqstatus &
  663. ISPCSI2_IRQSTATUS_OCP_ERR_IRQ) ? 1 : 0,
  664. (csi2_irqstatus &
  665. ISPCSI2_IRQSTATUS_SHORT_PACKET_IRQ) ? 1 : 0,
  666. (csi2_irqstatus &
  667. ISPCSI2_IRQSTATUS_ECC_NO_CORRECTION_IRQ) ? 1 : 0,
  668. (csi2_irqstatus &
  669. ISPCSI2_IRQSTATUS_COMPLEXIO2_ERR_IRQ) ? 1 : 0,
  670. (csi2_irqstatus &
  671. ISPCSI2_IRQSTATUS_FIFO_OVF_IRQ) ? 1 : 0);
  672. pipe->error = true;
  673. }
  674. if (omap3isp_module_sync_is_stopping(&csi2->wait, &csi2->stopping))
  675. return;
  676. /* Successful cases */
  677. if (csi2_irqstatus & ISPCSI2_IRQSTATUS_CONTEXT(0))
  678. csi2_isr_ctx(csi2, &csi2->contexts[0]);
  679. if (csi2_irqstatus & ISPCSI2_IRQSTATUS_ECC_CORRECTION_IRQ)
  680. dev_dbg(isp->dev, "CSI2: ECC correction done\n");
  681. }
  682. /* -----------------------------------------------------------------------------
  683. * ISP video operations
  684. */
  685. /*
  686. * csi2_queue - Queues the first buffer when using memory output
  687. * @video: The video node
  688. * @buffer: buffer to queue
  689. */
  690. static int csi2_queue(struct isp_video *video, struct isp_buffer *buffer)
  691. {
  692. struct isp_device *isp = video->isp;
  693. struct isp_csi2_device *csi2 = &isp->isp_csi2a;
  694. csi2_set_outaddr(csi2, buffer->isp_addr);
  695. /*
  696. * If streaming was enabled before there was a buffer queued
  697. * or underrun happened in the ISR, the hardware was not enabled
  698. * and DMA queue flag ISP_VIDEO_DMAQUEUE_UNDERRUN is still set.
  699. * Enable it now.
  700. */
  701. if (csi2->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_UNDERRUN) {
  702. /* Enable / disable context 0 and IRQs */
  703. csi2_if_enable(isp, csi2, 1);
  704. csi2_ctx_enable(isp, csi2, 0, 1);
  705. isp_video_dmaqueue_flags_clr(&csi2->video_out);
  706. }
  707. return 0;
  708. }
  709. static const struct isp_video_operations csi2_ispvideo_ops = {
  710. .queue = csi2_queue,
  711. };
  712. /* -----------------------------------------------------------------------------
  713. * V4L2 subdev operations
  714. */
  715. static struct v4l2_mbus_framefmt *
  716. __csi2_get_format(struct isp_csi2_device *csi2, struct v4l2_subdev_fh *fh,
  717. unsigned int pad, enum v4l2_subdev_format_whence which)
  718. {
  719. if (which == V4L2_SUBDEV_FORMAT_TRY)
  720. return v4l2_subdev_get_try_format(fh, pad);
  721. else
  722. return &csi2->formats[pad];
  723. }
  724. static void
  725. csi2_try_format(struct isp_csi2_device *csi2, struct v4l2_subdev_fh *fh,
  726. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  727. enum v4l2_subdev_format_whence which)
  728. {
  729. enum v4l2_mbus_pixelcode pixelcode;
  730. struct v4l2_mbus_framefmt *format;
  731. const struct isp_format_info *info;
  732. unsigned int i;
  733. switch (pad) {
  734. case CSI2_PAD_SINK:
  735. /* Clamp the width and height to valid range (1-8191). */
  736. for (i = 0; i < ARRAY_SIZE(csi2_input_fmts); i++) {
  737. if (fmt->code == csi2_input_fmts[i])
  738. break;
  739. }
  740. /* If not found, use SGRBG10 as default */
  741. if (i >= ARRAY_SIZE(csi2_input_fmts))
  742. fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  743. fmt->width = clamp_t(u32, fmt->width, 1, 8191);
  744. fmt->height = clamp_t(u32, fmt->height, 1, 8191);
  745. break;
  746. case CSI2_PAD_SOURCE:
  747. /* Source format same as sink format, except for DPCM
  748. * compression.
  749. */
  750. pixelcode = fmt->code;
  751. format = __csi2_get_format(csi2, fh, CSI2_PAD_SINK, which);
  752. memcpy(fmt, format, sizeof(*fmt));
  753. /*
  754. * Only Allow DPCM decompression, and check that the
  755. * pattern is preserved
  756. */
  757. info = omap3isp_video_format_info(fmt->code);
  758. if (info->uncompressed == pixelcode)
  759. fmt->code = pixelcode;
  760. break;
  761. }
  762. /* RGB, non-interlaced */
  763. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  764. fmt->field = V4L2_FIELD_NONE;
  765. }
  766. /*
  767. * csi2_enum_mbus_code - Handle pixel format enumeration
  768. * @sd : pointer to v4l2 subdev structure
  769. * @fh : V4L2 subdev file handle
  770. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  771. * return -EINVAL or zero on success
  772. */
  773. static int csi2_enum_mbus_code(struct v4l2_subdev *sd,
  774. struct v4l2_subdev_fh *fh,
  775. struct v4l2_subdev_mbus_code_enum *code)
  776. {
  777. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  778. struct v4l2_mbus_framefmt *format;
  779. const struct isp_format_info *info;
  780. if (code->pad == CSI2_PAD_SINK) {
  781. if (code->index >= ARRAY_SIZE(csi2_input_fmts))
  782. return -EINVAL;
  783. code->code = csi2_input_fmts[code->index];
  784. } else {
  785. format = __csi2_get_format(csi2, fh, CSI2_PAD_SINK,
  786. V4L2_SUBDEV_FORMAT_TRY);
  787. switch (code->index) {
  788. case 0:
  789. /* Passthrough sink pad code */
  790. code->code = format->code;
  791. break;
  792. case 1:
  793. /* Uncompressed code */
  794. info = omap3isp_video_format_info(format->code);
  795. if (info->uncompressed == format->code)
  796. return -EINVAL;
  797. code->code = info->uncompressed;
  798. break;
  799. default:
  800. return -EINVAL;
  801. }
  802. }
  803. return 0;
  804. }
  805. static int csi2_enum_frame_size(struct v4l2_subdev *sd,
  806. struct v4l2_subdev_fh *fh,
  807. struct v4l2_subdev_frame_size_enum *fse)
  808. {
  809. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  810. struct v4l2_mbus_framefmt format;
  811. if (fse->index != 0)
  812. return -EINVAL;
  813. format.code = fse->code;
  814. format.width = 1;
  815. format.height = 1;
  816. csi2_try_format(csi2, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  817. fse->min_width = format.width;
  818. fse->min_height = format.height;
  819. if (format.code != fse->code)
  820. return -EINVAL;
  821. format.code = fse->code;
  822. format.width = -1;
  823. format.height = -1;
  824. csi2_try_format(csi2, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  825. fse->max_width = format.width;
  826. fse->max_height = format.height;
  827. return 0;
  828. }
  829. /*
  830. * csi2_get_format - Handle get format by pads subdev method
  831. * @sd : pointer to v4l2 subdev structure
  832. * @fh : V4L2 subdev file handle
  833. * @fmt: pointer to v4l2 subdev format structure
  834. * return -EINVAL or zero on success
  835. */
  836. static int csi2_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  837. struct v4l2_subdev_format *fmt)
  838. {
  839. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  840. struct v4l2_mbus_framefmt *format;
  841. format = __csi2_get_format(csi2, fh, fmt->pad, fmt->which);
  842. if (format == NULL)
  843. return -EINVAL;
  844. fmt->format = *format;
  845. return 0;
  846. }
  847. /*
  848. * csi2_set_format - Handle set format by pads subdev method
  849. * @sd : pointer to v4l2 subdev structure
  850. * @fh : V4L2 subdev file handle
  851. * @fmt: pointer to v4l2 subdev format structure
  852. * return -EINVAL or zero on success
  853. */
  854. static int csi2_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  855. struct v4l2_subdev_format *fmt)
  856. {
  857. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  858. struct v4l2_mbus_framefmt *format;
  859. format = __csi2_get_format(csi2, fh, fmt->pad, fmt->which);
  860. if (format == NULL)
  861. return -EINVAL;
  862. csi2_try_format(csi2, fh, fmt->pad, &fmt->format, fmt->which);
  863. *format = fmt->format;
  864. /* Propagate the format from sink to source */
  865. if (fmt->pad == CSI2_PAD_SINK) {
  866. format = __csi2_get_format(csi2, fh, CSI2_PAD_SOURCE,
  867. fmt->which);
  868. *format = fmt->format;
  869. csi2_try_format(csi2, fh, CSI2_PAD_SOURCE, format, fmt->which);
  870. }
  871. return 0;
  872. }
  873. /*
  874. * csi2_init_formats - Initialize formats on all pads
  875. * @sd: ISP CSI2 V4L2 subdevice
  876. * @fh: V4L2 subdev file handle
  877. *
  878. * Initialize all pad formats with default values. If fh is not NULL, try
  879. * formats are initialized on the file handle. Otherwise active formats are
  880. * initialized on the device.
  881. */
  882. static int csi2_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  883. {
  884. struct v4l2_subdev_format format;
  885. memset(&format, 0, sizeof(format));
  886. format.pad = CSI2_PAD_SINK;
  887. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  888. format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  889. format.format.width = 4096;
  890. format.format.height = 4096;
  891. csi2_set_format(sd, fh, &format);
  892. return 0;
  893. }
  894. /*
  895. * csi2_set_stream - Enable/Disable streaming on the CSI2 module
  896. * @sd: ISP CSI2 V4L2 subdevice
  897. * @enable: ISP pipeline stream state
  898. *
  899. * Return 0 on success or a negative error code otherwise.
  900. */
  901. static int csi2_set_stream(struct v4l2_subdev *sd, int enable)
  902. {
  903. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  904. struct isp_device *isp = csi2->isp;
  905. struct isp_pipeline *pipe = to_isp_pipeline(&csi2->subdev.entity);
  906. struct isp_video *video_out = &csi2->video_out;
  907. switch (enable) {
  908. case ISP_PIPELINE_STREAM_CONTINUOUS:
  909. if (omap3isp_csiphy_acquire(csi2->phy) < 0)
  910. return -ENODEV;
  911. csi2->use_fs_irq = pipe->do_propagation;
  912. if (csi2->output & CSI2_OUTPUT_MEMORY)
  913. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CSI2A_WRITE);
  914. csi2_configure(csi2);
  915. csi2_print_status(csi2);
  916. /*
  917. * When outputting to memory with no buffer available, let the
  918. * buffer queue handler start the hardware. A DMA queue flag
  919. * ISP_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
  920. * a buffer available.
  921. */
  922. if (csi2->output & CSI2_OUTPUT_MEMORY &&
  923. !(video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED))
  924. break;
  925. /* Enable context 0 and IRQs */
  926. atomic_set(&csi2->stopping, 0);
  927. csi2_ctx_enable(isp, csi2, 0, 1);
  928. csi2_if_enable(isp, csi2, 1);
  929. isp_video_dmaqueue_flags_clr(video_out);
  930. break;
  931. case ISP_PIPELINE_STREAM_STOPPED:
  932. if (csi2->state == ISP_PIPELINE_STREAM_STOPPED)
  933. return 0;
  934. if (omap3isp_module_sync_idle(&sd->entity, &csi2->wait,
  935. &csi2->stopping))
  936. dev_dbg(isp->dev, "%s: module stop timeout.\n",
  937. sd->name);
  938. csi2_ctx_enable(isp, csi2, 0, 0);
  939. csi2_if_enable(isp, csi2, 0);
  940. csi2_irq_ctx_set(isp, csi2, 0);
  941. omap3isp_csiphy_release(csi2->phy);
  942. isp_video_dmaqueue_flags_clr(video_out);
  943. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CSI2A_WRITE);
  944. break;
  945. }
  946. csi2->state = enable;
  947. return 0;
  948. }
  949. /* subdev video operations */
  950. static const struct v4l2_subdev_video_ops csi2_video_ops = {
  951. .s_stream = csi2_set_stream,
  952. };
  953. /* subdev pad operations */
  954. static const struct v4l2_subdev_pad_ops csi2_pad_ops = {
  955. .enum_mbus_code = csi2_enum_mbus_code,
  956. .enum_frame_size = csi2_enum_frame_size,
  957. .get_fmt = csi2_get_format,
  958. .set_fmt = csi2_set_format,
  959. };
  960. /* subdev operations */
  961. static const struct v4l2_subdev_ops csi2_ops = {
  962. .video = &csi2_video_ops,
  963. .pad = &csi2_pad_ops,
  964. };
  965. /* subdev internal operations */
  966. static const struct v4l2_subdev_internal_ops csi2_internal_ops = {
  967. .open = csi2_init_formats,
  968. };
  969. /* -----------------------------------------------------------------------------
  970. * Media entity operations
  971. */
  972. /*
  973. * csi2_link_setup - Setup CSI2 connections.
  974. * @entity : Pointer to media entity structure
  975. * @local : Pointer to local pad array
  976. * @remote : Pointer to remote pad array
  977. * @flags : Link flags
  978. * return -EINVAL or zero on success
  979. */
  980. static int csi2_link_setup(struct media_entity *entity,
  981. const struct media_pad *local,
  982. const struct media_pad *remote, u32 flags)
  983. {
  984. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  985. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  986. struct isp_csi2_ctrl_cfg *ctrl = &csi2->ctrl;
  987. /*
  988. * The ISP core doesn't support pipelines with multiple video outputs.
  989. * Revisit this when it will be implemented, and return -EBUSY for now.
  990. */
  991. switch (local->index | media_entity_type(remote->entity)) {
  992. case CSI2_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
  993. if (flags & MEDIA_LNK_FL_ENABLED) {
  994. if (csi2->output & ~CSI2_OUTPUT_MEMORY)
  995. return -EBUSY;
  996. csi2->output |= CSI2_OUTPUT_MEMORY;
  997. } else {
  998. csi2->output &= ~CSI2_OUTPUT_MEMORY;
  999. }
  1000. break;
  1001. case CSI2_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
  1002. if (flags & MEDIA_LNK_FL_ENABLED) {
  1003. if (csi2->output & ~CSI2_OUTPUT_CCDC)
  1004. return -EBUSY;
  1005. csi2->output |= CSI2_OUTPUT_CCDC;
  1006. } else {
  1007. csi2->output &= ~CSI2_OUTPUT_CCDC;
  1008. }
  1009. break;
  1010. default:
  1011. /* Link from camera to CSI2 is fixed... */
  1012. return -EINVAL;
  1013. }
  1014. ctrl->vp_only_enable =
  1015. (csi2->output & CSI2_OUTPUT_MEMORY) ? false : true;
  1016. ctrl->vp_clk_enable = !!(csi2->output & CSI2_OUTPUT_CCDC);
  1017. return 0;
  1018. }
  1019. /* media operations */
  1020. static const struct media_entity_operations csi2_media_ops = {
  1021. .link_setup = csi2_link_setup,
  1022. };
  1023. void omap3isp_csi2_unregister_entities(struct isp_csi2_device *csi2)
  1024. {
  1025. v4l2_device_unregister_subdev(&csi2->subdev);
  1026. omap3isp_video_unregister(&csi2->video_out);
  1027. }
  1028. int omap3isp_csi2_register_entities(struct isp_csi2_device *csi2,
  1029. struct v4l2_device *vdev)
  1030. {
  1031. int ret;
  1032. /* Register the subdev and video nodes. */
  1033. ret = v4l2_device_register_subdev(vdev, &csi2->subdev);
  1034. if (ret < 0)
  1035. goto error;
  1036. ret = omap3isp_video_register(&csi2->video_out, vdev);
  1037. if (ret < 0)
  1038. goto error;
  1039. return 0;
  1040. error:
  1041. omap3isp_csi2_unregister_entities(csi2);
  1042. return ret;
  1043. }
  1044. /* -----------------------------------------------------------------------------
  1045. * ISP CSI2 initialisation and cleanup
  1046. */
  1047. /*
  1048. * csi2_init_entities - Initialize subdev and media entity.
  1049. * @csi2: Pointer to csi2 structure.
  1050. * return -ENOMEM or zero on success
  1051. */
  1052. static int csi2_init_entities(struct isp_csi2_device *csi2)
  1053. {
  1054. struct v4l2_subdev *sd = &csi2->subdev;
  1055. struct media_pad *pads = csi2->pads;
  1056. struct media_entity *me = &sd->entity;
  1057. int ret;
  1058. v4l2_subdev_init(sd, &csi2_ops);
  1059. sd->internal_ops = &csi2_internal_ops;
  1060. strlcpy(sd->name, "OMAP3 ISP CSI2a", sizeof(sd->name));
  1061. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  1062. v4l2_set_subdevdata(sd, csi2);
  1063. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1064. pads[CSI2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1065. pads[CSI2_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1066. me->ops = &csi2_media_ops;
  1067. ret = media_entity_init(me, CSI2_PADS_NUM, pads, 0);
  1068. if (ret < 0)
  1069. return ret;
  1070. csi2_init_formats(sd, NULL);
  1071. /* Video device node */
  1072. csi2->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1073. csi2->video_out.ops = &csi2_ispvideo_ops;
  1074. csi2->video_out.bpl_alignment = 32;
  1075. csi2->video_out.bpl_zero_padding = 1;
  1076. csi2->video_out.bpl_max = 0x1ffe0;
  1077. csi2->video_out.isp = csi2->isp;
  1078. csi2->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
  1079. ret = omap3isp_video_init(&csi2->video_out, "CSI2a");
  1080. if (ret < 0)
  1081. goto error_video;
  1082. /* Connect the CSI2 subdev to the video node. */
  1083. ret = media_entity_create_link(&csi2->subdev.entity, CSI2_PAD_SOURCE,
  1084. &csi2->video_out.video.entity, 0, 0);
  1085. if (ret < 0)
  1086. goto error_link;
  1087. return 0;
  1088. error_link:
  1089. omap3isp_video_cleanup(&csi2->video_out);
  1090. error_video:
  1091. media_entity_cleanup(&csi2->subdev.entity);
  1092. return ret;
  1093. }
  1094. /*
  1095. * omap3isp_csi2_init - Routine for module driver init
  1096. */
  1097. int omap3isp_csi2_init(struct isp_device *isp)
  1098. {
  1099. struct isp_csi2_device *csi2a = &isp->isp_csi2a;
  1100. struct isp_csi2_device *csi2c = &isp->isp_csi2c;
  1101. int ret;
  1102. csi2a->isp = isp;
  1103. csi2a->available = 1;
  1104. csi2a->regs1 = OMAP3_ISP_IOMEM_CSI2A_REGS1;
  1105. csi2a->regs2 = OMAP3_ISP_IOMEM_CSI2A_REGS2;
  1106. csi2a->phy = &isp->isp_csiphy2;
  1107. csi2a->state = ISP_PIPELINE_STREAM_STOPPED;
  1108. init_waitqueue_head(&csi2a->wait);
  1109. ret = csi2_init_entities(csi2a);
  1110. if (ret < 0)
  1111. return ret;
  1112. if (isp->revision == ISP_REVISION_15_0) {
  1113. csi2c->isp = isp;
  1114. csi2c->available = 1;
  1115. csi2c->regs1 = OMAP3_ISP_IOMEM_CSI2C_REGS1;
  1116. csi2c->regs2 = OMAP3_ISP_IOMEM_CSI2C_REGS2;
  1117. csi2c->phy = &isp->isp_csiphy1;
  1118. csi2c->state = ISP_PIPELINE_STREAM_STOPPED;
  1119. init_waitqueue_head(&csi2c->wait);
  1120. }
  1121. return 0;
  1122. }
  1123. /*
  1124. * omap3isp_csi2_cleanup - Routine for module driver cleanup
  1125. */
  1126. void omap3isp_csi2_cleanup(struct isp_device *isp)
  1127. {
  1128. struct isp_csi2_device *csi2a = &isp->isp_csi2a;
  1129. omap3isp_video_cleanup(&csi2a->video_out);
  1130. media_entity_cleanup(&csi2a->subdev.entity);
  1131. }