ispcsi2.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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, 0);
  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. * Return -EIO on Transmission error
  632. */
  633. int omap3isp_csi2_isr(struct isp_csi2_device *csi2)
  634. {
  635. u32 csi2_irqstatus, cpxio1_irqstatus;
  636. struct isp_device *isp = csi2->isp;
  637. int retval = 0;
  638. if (!csi2->available)
  639. return -ENODEV;
  640. csi2_irqstatus = isp_reg_readl(isp, csi2->regs1, ISPCSI2_IRQSTATUS);
  641. isp_reg_writel(isp, csi2_irqstatus, csi2->regs1, ISPCSI2_IRQSTATUS);
  642. /* Failure Cases */
  643. if (csi2_irqstatus & ISPCSI2_IRQSTATUS_COMPLEXIO1_ERR_IRQ) {
  644. cpxio1_irqstatus = isp_reg_readl(isp, csi2->regs1,
  645. ISPCSI2_PHY_IRQSTATUS);
  646. isp_reg_writel(isp, cpxio1_irqstatus,
  647. csi2->regs1, ISPCSI2_PHY_IRQSTATUS);
  648. dev_dbg(isp->dev, "CSI2: ComplexIO Error IRQ "
  649. "%x\n", cpxio1_irqstatus);
  650. retval = -EIO;
  651. }
  652. if (csi2_irqstatus & (ISPCSI2_IRQSTATUS_OCP_ERR_IRQ |
  653. ISPCSI2_IRQSTATUS_SHORT_PACKET_IRQ |
  654. ISPCSI2_IRQSTATUS_ECC_NO_CORRECTION_IRQ |
  655. ISPCSI2_IRQSTATUS_COMPLEXIO2_ERR_IRQ |
  656. ISPCSI2_IRQSTATUS_FIFO_OVF_IRQ)) {
  657. dev_dbg(isp->dev, "CSI2 Err:"
  658. " OCP:%d,"
  659. " Short_pack:%d,"
  660. " ECC:%d,"
  661. " CPXIO2:%d,"
  662. " FIFO_OVF:%d,"
  663. "\n",
  664. (csi2_irqstatus &
  665. ISPCSI2_IRQSTATUS_OCP_ERR_IRQ) ? 1 : 0,
  666. (csi2_irqstatus &
  667. ISPCSI2_IRQSTATUS_SHORT_PACKET_IRQ) ? 1 : 0,
  668. (csi2_irqstatus &
  669. ISPCSI2_IRQSTATUS_ECC_NO_CORRECTION_IRQ) ? 1 : 0,
  670. (csi2_irqstatus &
  671. ISPCSI2_IRQSTATUS_COMPLEXIO2_ERR_IRQ) ? 1 : 0,
  672. (csi2_irqstatus &
  673. ISPCSI2_IRQSTATUS_FIFO_OVF_IRQ) ? 1 : 0);
  674. retval = -EIO;
  675. }
  676. if (omap3isp_module_sync_is_stopping(&csi2->wait, &csi2->stopping))
  677. return 0;
  678. /* Successful cases */
  679. if (csi2_irqstatus & ISPCSI2_IRQSTATUS_CONTEXT(0))
  680. csi2_isr_ctx(csi2, &csi2->contexts[0]);
  681. if (csi2_irqstatus & ISPCSI2_IRQSTATUS_ECC_CORRECTION_IRQ)
  682. dev_dbg(isp->dev, "CSI2: ECC correction done\n");
  683. return retval;
  684. }
  685. /* -----------------------------------------------------------------------------
  686. * ISP video operations
  687. */
  688. /*
  689. * csi2_queue - Queues the first buffer when using memory output
  690. * @video: The video node
  691. * @buffer: buffer to queue
  692. */
  693. static int csi2_queue(struct isp_video *video, struct isp_buffer *buffer)
  694. {
  695. struct isp_device *isp = video->isp;
  696. struct isp_csi2_device *csi2 = &isp->isp_csi2a;
  697. csi2_set_outaddr(csi2, buffer->isp_addr);
  698. /*
  699. * If streaming was enabled before there was a buffer queued
  700. * or underrun happened in the ISR, the hardware was not enabled
  701. * and DMA queue flag ISP_VIDEO_DMAQUEUE_UNDERRUN is still set.
  702. * Enable it now.
  703. */
  704. if (csi2->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_UNDERRUN) {
  705. /* Enable / disable context 0 and IRQs */
  706. csi2_if_enable(isp, csi2, 1);
  707. csi2_ctx_enable(isp, csi2, 0, 1);
  708. isp_video_dmaqueue_flags_clr(&csi2->video_out);
  709. }
  710. return 0;
  711. }
  712. static const struct isp_video_operations csi2_ispvideo_ops = {
  713. .queue = csi2_queue,
  714. };
  715. /* -----------------------------------------------------------------------------
  716. * V4L2 subdev operations
  717. */
  718. static struct v4l2_mbus_framefmt *
  719. __csi2_get_format(struct isp_csi2_device *csi2, struct v4l2_subdev_fh *fh,
  720. unsigned int pad, enum v4l2_subdev_format_whence which)
  721. {
  722. if (which == V4L2_SUBDEV_FORMAT_TRY)
  723. return v4l2_subdev_get_try_format(fh, pad);
  724. else
  725. return &csi2->formats[pad];
  726. }
  727. static void
  728. csi2_try_format(struct isp_csi2_device *csi2, struct v4l2_subdev_fh *fh,
  729. unsigned int pad, struct v4l2_mbus_framefmt *fmt,
  730. enum v4l2_subdev_format_whence which)
  731. {
  732. enum v4l2_mbus_pixelcode pixelcode;
  733. struct v4l2_mbus_framefmt *format;
  734. const struct isp_format_info *info;
  735. unsigned int i;
  736. switch (pad) {
  737. case CSI2_PAD_SINK:
  738. /* Clamp the width and height to valid range (1-8191). */
  739. for (i = 0; i < ARRAY_SIZE(csi2_input_fmts); i++) {
  740. if (fmt->code == csi2_input_fmts[i])
  741. break;
  742. }
  743. /* If not found, use SGRBG10 as default */
  744. if (i >= ARRAY_SIZE(csi2_input_fmts))
  745. fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  746. fmt->width = clamp_t(u32, fmt->width, 1, 8191);
  747. fmt->height = clamp_t(u32, fmt->height, 1, 8191);
  748. break;
  749. case CSI2_PAD_SOURCE:
  750. /* Source format same as sink format, except for DPCM
  751. * compression.
  752. */
  753. pixelcode = fmt->code;
  754. format = __csi2_get_format(csi2, fh, CSI2_PAD_SINK, which);
  755. memcpy(fmt, format, sizeof(*fmt));
  756. /*
  757. * Only Allow DPCM decompression, and check that the
  758. * pattern is preserved
  759. */
  760. info = omap3isp_video_format_info(fmt->code);
  761. if (info->uncompressed == pixelcode)
  762. fmt->code = pixelcode;
  763. break;
  764. }
  765. /* RGB, non-interlaced */
  766. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  767. fmt->field = V4L2_FIELD_NONE;
  768. }
  769. /*
  770. * csi2_enum_mbus_code - Handle pixel format enumeration
  771. * @sd : pointer to v4l2 subdev structure
  772. * @fh : V4L2 subdev file handle
  773. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  774. * return -EINVAL or zero on success
  775. */
  776. static int csi2_enum_mbus_code(struct v4l2_subdev *sd,
  777. struct v4l2_subdev_fh *fh,
  778. struct v4l2_subdev_mbus_code_enum *code)
  779. {
  780. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  781. struct v4l2_mbus_framefmt *format;
  782. const struct isp_format_info *info;
  783. if (code->pad == CSI2_PAD_SINK) {
  784. if (code->index >= ARRAY_SIZE(csi2_input_fmts))
  785. return -EINVAL;
  786. code->code = csi2_input_fmts[code->index];
  787. } else {
  788. format = __csi2_get_format(csi2, fh, CSI2_PAD_SINK,
  789. V4L2_SUBDEV_FORMAT_TRY);
  790. switch (code->index) {
  791. case 0:
  792. /* Passthrough sink pad code */
  793. code->code = format->code;
  794. break;
  795. case 1:
  796. /* Uncompressed code */
  797. info = omap3isp_video_format_info(format->code);
  798. if (info->uncompressed == format->code)
  799. return -EINVAL;
  800. code->code = info->uncompressed;
  801. break;
  802. default:
  803. return -EINVAL;
  804. }
  805. }
  806. return 0;
  807. }
  808. static int csi2_enum_frame_size(struct v4l2_subdev *sd,
  809. struct v4l2_subdev_fh *fh,
  810. struct v4l2_subdev_frame_size_enum *fse)
  811. {
  812. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  813. struct v4l2_mbus_framefmt format;
  814. if (fse->index != 0)
  815. return -EINVAL;
  816. format.code = fse->code;
  817. format.width = 1;
  818. format.height = 1;
  819. csi2_try_format(csi2, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  820. fse->min_width = format.width;
  821. fse->min_height = format.height;
  822. if (format.code != fse->code)
  823. return -EINVAL;
  824. format.code = fse->code;
  825. format.width = -1;
  826. format.height = -1;
  827. csi2_try_format(csi2, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  828. fse->max_width = format.width;
  829. fse->max_height = format.height;
  830. return 0;
  831. }
  832. /*
  833. * csi2_get_format - Handle get format by pads subdev method
  834. * @sd : pointer to v4l2 subdev structure
  835. * @fh : V4L2 subdev file handle
  836. * @fmt: pointer to v4l2 subdev format structure
  837. * return -EINVAL or zero on sucess
  838. */
  839. static int csi2_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  840. struct v4l2_subdev_format *fmt)
  841. {
  842. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  843. struct v4l2_mbus_framefmt *format;
  844. format = __csi2_get_format(csi2, fh, fmt->pad, fmt->which);
  845. if (format == NULL)
  846. return -EINVAL;
  847. fmt->format = *format;
  848. return 0;
  849. }
  850. /*
  851. * csi2_set_format - Handle set format by pads subdev method
  852. * @sd : pointer to v4l2 subdev structure
  853. * @fh : V4L2 subdev file handle
  854. * @fmt: pointer to v4l2 subdev format structure
  855. * return -EINVAL or zero on success
  856. */
  857. static int csi2_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  858. struct v4l2_subdev_format *fmt)
  859. {
  860. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  861. struct v4l2_mbus_framefmt *format;
  862. format = __csi2_get_format(csi2, fh, fmt->pad, fmt->which);
  863. if (format == NULL)
  864. return -EINVAL;
  865. csi2_try_format(csi2, fh, fmt->pad, &fmt->format, fmt->which);
  866. *format = fmt->format;
  867. /* Propagate the format from sink to source */
  868. if (fmt->pad == CSI2_PAD_SINK) {
  869. format = __csi2_get_format(csi2, fh, CSI2_PAD_SOURCE,
  870. fmt->which);
  871. *format = fmt->format;
  872. csi2_try_format(csi2, fh, CSI2_PAD_SOURCE, format, fmt->which);
  873. }
  874. return 0;
  875. }
  876. /*
  877. * csi2_init_formats - Initialize formats on all pads
  878. * @sd: ISP CSI2 V4L2 subdevice
  879. * @fh: V4L2 subdev file handle
  880. *
  881. * Initialize all pad formats with default values. If fh is not NULL, try
  882. * formats are initialized on the file handle. Otherwise active formats are
  883. * initialized on the device.
  884. */
  885. static int csi2_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  886. {
  887. struct v4l2_subdev_format format;
  888. memset(&format, 0, sizeof(format));
  889. format.pad = CSI2_PAD_SINK;
  890. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  891. format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  892. format.format.width = 4096;
  893. format.format.height = 4096;
  894. csi2_set_format(sd, fh, &format);
  895. return 0;
  896. }
  897. /*
  898. * csi2_set_stream - Enable/Disable streaming on the CSI2 module
  899. * @sd: ISP CSI2 V4L2 subdevice
  900. * @enable: ISP pipeline stream state
  901. *
  902. * Return 0 on success or a negative error code otherwise.
  903. */
  904. static int csi2_set_stream(struct v4l2_subdev *sd, int enable)
  905. {
  906. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  907. struct isp_device *isp = csi2->isp;
  908. struct isp_pipeline *pipe = to_isp_pipeline(&csi2->subdev.entity);
  909. struct isp_video *video_out = &csi2->video_out;
  910. switch (enable) {
  911. case ISP_PIPELINE_STREAM_CONTINUOUS:
  912. if (omap3isp_csiphy_acquire(csi2->phy) < 0)
  913. return -ENODEV;
  914. csi2->use_fs_irq = pipe->do_propagation;
  915. if (csi2->output & CSI2_OUTPUT_MEMORY)
  916. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CSI2A_WRITE);
  917. csi2_configure(csi2);
  918. csi2_print_status(csi2);
  919. /*
  920. * When outputting to memory with no buffer available, let the
  921. * buffer queue handler start the hardware. A DMA queue flag
  922. * ISP_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
  923. * a buffer available.
  924. */
  925. if (csi2->output & CSI2_OUTPUT_MEMORY &&
  926. !(video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED))
  927. break;
  928. /* Enable context 0 and IRQs */
  929. atomic_set(&csi2->stopping, 0);
  930. csi2_ctx_enable(isp, csi2, 0, 1);
  931. csi2_if_enable(isp, csi2, 1);
  932. isp_video_dmaqueue_flags_clr(video_out);
  933. break;
  934. case ISP_PIPELINE_STREAM_STOPPED:
  935. if (csi2->state == ISP_PIPELINE_STREAM_STOPPED)
  936. return 0;
  937. if (omap3isp_module_sync_idle(&sd->entity, &csi2->wait,
  938. &csi2->stopping))
  939. dev_dbg(isp->dev, "%s: module stop timeout.\n",
  940. sd->name);
  941. csi2_ctx_enable(isp, csi2, 0, 0);
  942. csi2_if_enable(isp, csi2, 0);
  943. csi2_irq_ctx_set(isp, csi2, 0);
  944. omap3isp_csiphy_release(csi2->phy);
  945. isp_video_dmaqueue_flags_clr(video_out);
  946. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CSI2A_WRITE);
  947. break;
  948. }
  949. csi2->state = enable;
  950. return 0;
  951. }
  952. /* subdev video operations */
  953. static const struct v4l2_subdev_video_ops csi2_video_ops = {
  954. .s_stream = csi2_set_stream,
  955. };
  956. /* subdev pad operations */
  957. static const struct v4l2_subdev_pad_ops csi2_pad_ops = {
  958. .enum_mbus_code = csi2_enum_mbus_code,
  959. .enum_frame_size = csi2_enum_frame_size,
  960. .get_fmt = csi2_get_format,
  961. .set_fmt = csi2_set_format,
  962. };
  963. /* subdev operations */
  964. static const struct v4l2_subdev_ops csi2_ops = {
  965. .video = &csi2_video_ops,
  966. .pad = &csi2_pad_ops,
  967. };
  968. /* subdev internal operations */
  969. static const struct v4l2_subdev_internal_ops csi2_internal_ops = {
  970. .open = csi2_init_formats,
  971. };
  972. /* -----------------------------------------------------------------------------
  973. * Media entity operations
  974. */
  975. /*
  976. * csi2_link_setup - Setup CSI2 connections.
  977. * @entity : Pointer to media entity structure
  978. * @local : Pointer to local pad array
  979. * @remote : Pointer to remote pad array
  980. * @flags : Link flags
  981. * return -EINVAL or zero on success
  982. */
  983. static int csi2_link_setup(struct media_entity *entity,
  984. const struct media_pad *local,
  985. const struct media_pad *remote, u32 flags)
  986. {
  987. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  988. struct isp_csi2_device *csi2 = v4l2_get_subdevdata(sd);
  989. struct isp_csi2_ctrl_cfg *ctrl = &csi2->ctrl;
  990. /*
  991. * The ISP core doesn't support pipelines with multiple video outputs.
  992. * Revisit this when it will be implemented, and return -EBUSY for now.
  993. */
  994. switch (local->index | media_entity_type(remote->entity)) {
  995. case CSI2_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
  996. if (flags & MEDIA_LNK_FL_ENABLED) {
  997. if (csi2->output & ~CSI2_OUTPUT_MEMORY)
  998. return -EBUSY;
  999. csi2->output |= CSI2_OUTPUT_MEMORY;
  1000. } else {
  1001. csi2->output &= ~CSI2_OUTPUT_MEMORY;
  1002. }
  1003. break;
  1004. case CSI2_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
  1005. if (flags & MEDIA_LNK_FL_ENABLED) {
  1006. if (csi2->output & ~CSI2_OUTPUT_CCDC)
  1007. return -EBUSY;
  1008. csi2->output |= CSI2_OUTPUT_CCDC;
  1009. } else {
  1010. csi2->output &= ~CSI2_OUTPUT_CCDC;
  1011. }
  1012. break;
  1013. default:
  1014. /* Link from camera to CSI2 is fixed... */
  1015. return -EINVAL;
  1016. }
  1017. ctrl->vp_only_enable =
  1018. (csi2->output & CSI2_OUTPUT_MEMORY) ? false : true;
  1019. ctrl->vp_clk_enable = !!(csi2->output & CSI2_OUTPUT_CCDC);
  1020. return 0;
  1021. }
  1022. /* media operations */
  1023. static const struct media_entity_operations csi2_media_ops = {
  1024. .link_setup = csi2_link_setup,
  1025. };
  1026. /*
  1027. * csi2_init_entities - Initialize subdev and media entity.
  1028. * @csi2: Pointer to csi2 structure.
  1029. * return -ENOMEM or zero on success
  1030. */
  1031. static int csi2_init_entities(struct isp_csi2_device *csi2)
  1032. {
  1033. struct v4l2_subdev *sd = &csi2->subdev;
  1034. struct media_pad *pads = csi2->pads;
  1035. struct media_entity *me = &sd->entity;
  1036. int ret;
  1037. v4l2_subdev_init(sd, &csi2_ops);
  1038. sd->internal_ops = &csi2_internal_ops;
  1039. strlcpy(sd->name, "OMAP3 ISP CSI2a", sizeof(sd->name));
  1040. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  1041. v4l2_set_subdevdata(sd, csi2);
  1042. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1043. pads[CSI2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1044. pads[CSI2_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1045. me->ops = &csi2_media_ops;
  1046. ret = media_entity_init(me, CSI2_PADS_NUM, pads, 0);
  1047. if (ret < 0)
  1048. return ret;
  1049. csi2_init_formats(sd, NULL);
  1050. /* Video device node */
  1051. csi2->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1052. csi2->video_out.ops = &csi2_ispvideo_ops;
  1053. csi2->video_out.bpl_alignment = 32;
  1054. csi2->video_out.bpl_zero_padding = 1;
  1055. csi2->video_out.bpl_max = 0x1ffe0;
  1056. csi2->video_out.isp = csi2->isp;
  1057. csi2->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
  1058. ret = omap3isp_video_init(&csi2->video_out, "CSI2a");
  1059. if (ret < 0)
  1060. return ret;
  1061. /* Connect the CSI2 subdev to the video node. */
  1062. ret = media_entity_create_link(&csi2->subdev.entity, CSI2_PAD_SOURCE,
  1063. &csi2->video_out.video.entity, 0, 0);
  1064. if (ret < 0)
  1065. return ret;
  1066. return 0;
  1067. }
  1068. void omap3isp_csi2_unregister_entities(struct isp_csi2_device *csi2)
  1069. {
  1070. media_entity_cleanup(&csi2->subdev.entity);
  1071. v4l2_device_unregister_subdev(&csi2->subdev);
  1072. omap3isp_video_unregister(&csi2->video_out);
  1073. }
  1074. int omap3isp_csi2_register_entities(struct isp_csi2_device *csi2,
  1075. struct v4l2_device *vdev)
  1076. {
  1077. int ret;
  1078. /* Register the subdev and video nodes. */
  1079. ret = v4l2_device_register_subdev(vdev, &csi2->subdev);
  1080. if (ret < 0)
  1081. goto error;
  1082. ret = omap3isp_video_register(&csi2->video_out, vdev);
  1083. if (ret < 0)
  1084. goto error;
  1085. return 0;
  1086. error:
  1087. omap3isp_csi2_unregister_entities(csi2);
  1088. return ret;
  1089. }
  1090. /* -----------------------------------------------------------------------------
  1091. * ISP CSI2 initialisation and cleanup
  1092. */
  1093. /*
  1094. * omap3isp_csi2_cleanup - Routine for module driver cleanup
  1095. */
  1096. void omap3isp_csi2_cleanup(struct isp_device *isp)
  1097. {
  1098. }
  1099. /*
  1100. * omap3isp_csi2_init - Routine for module driver init
  1101. */
  1102. int omap3isp_csi2_init(struct isp_device *isp)
  1103. {
  1104. struct isp_csi2_device *csi2a = &isp->isp_csi2a;
  1105. struct isp_csi2_device *csi2c = &isp->isp_csi2c;
  1106. int ret;
  1107. csi2a->isp = isp;
  1108. csi2a->available = 1;
  1109. csi2a->regs1 = OMAP3_ISP_IOMEM_CSI2A_REGS1;
  1110. csi2a->regs2 = OMAP3_ISP_IOMEM_CSI2A_REGS2;
  1111. csi2a->phy = &isp->isp_csiphy2;
  1112. csi2a->state = ISP_PIPELINE_STREAM_STOPPED;
  1113. init_waitqueue_head(&csi2a->wait);
  1114. ret = csi2_init_entities(csi2a);
  1115. if (ret < 0)
  1116. goto fail;
  1117. if (isp->revision == ISP_REVISION_15_0) {
  1118. csi2c->isp = isp;
  1119. csi2c->available = 1;
  1120. csi2c->regs1 = OMAP3_ISP_IOMEM_CSI2C_REGS1;
  1121. csi2c->regs2 = OMAP3_ISP_IOMEM_CSI2C_REGS2;
  1122. csi2c->phy = &isp->isp_csiphy1;
  1123. csi2c->state = ISP_PIPELINE_STREAM_STOPPED;
  1124. init_waitqueue_head(&csi2c->wait);
  1125. }
  1126. return 0;
  1127. fail:
  1128. omap3isp_csi2_cleanup(isp);
  1129. return ret;
  1130. }