ispcsi2.c 36 KB

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