ispccp2.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * ispccp2.c
  3. *
  4. * TI OMAP3 ISP - CCP2 module
  5. *
  6. * Copyright (C) 2010 Nokia Corporation
  7. * Copyright (C) 2010 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 <linux/device.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/mutex.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/regulator/consumer.h>
  33. #include "isp.h"
  34. #include "ispreg.h"
  35. #include "ispccp2.h"
  36. /* Number of LCX channels */
  37. #define CCP2_LCx_CHANS_NUM 3
  38. /* Max/Min size for CCP2 video port */
  39. #define ISPCCP2_DAT_START_MIN 0
  40. #define ISPCCP2_DAT_START_MAX 4095
  41. #define ISPCCP2_DAT_SIZE_MIN 0
  42. #define ISPCCP2_DAT_SIZE_MAX 4095
  43. #define ISPCCP2_VPCLK_FRACDIV 65536
  44. #define ISPCCP2_LCx_CTRL_FORMAT_RAW8_DPCM10_VP 0x12
  45. #define ISPCCP2_LCx_CTRL_FORMAT_RAW10_VP 0x16
  46. /* Max/Min size for CCP2 memory channel */
  47. #define ISPCCP2_LCM_HSIZE_COUNT_MIN 16
  48. #define ISPCCP2_LCM_HSIZE_COUNT_MAX 8191
  49. #define ISPCCP2_LCM_HSIZE_SKIP_MIN 0
  50. #define ISPCCP2_LCM_HSIZE_SKIP_MAX 8191
  51. #define ISPCCP2_LCM_VSIZE_MIN 1
  52. #define ISPCCP2_LCM_VSIZE_MAX 8191
  53. #define ISPCCP2_LCM_HWORDS_MIN 1
  54. #define ISPCCP2_LCM_HWORDS_MAX 4095
  55. #define ISPCCP2_LCM_CTRL_BURST_SIZE_32X 5
  56. #define ISPCCP2_LCM_CTRL_READ_THROTTLE_FULL 0
  57. #define ISPCCP2_LCM_CTRL_SRC_DECOMPR_DPCM10 2
  58. #define ISPCCP2_LCM_CTRL_SRC_FORMAT_RAW8 2
  59. #define ISPCCP2_LCM_CTRL_SRC_FORMAT_RAW10 3
  60. #define ISPCCP2_LCM_CTRL_DST_FORMAT_RAW10 3
  61. #define ISPCCP2_LCM_CTRL_DST_PORT_VP 0
  62. #define ISPCCP2_LCM_CTRL_DST_PORT_MEM 1
  63. /* Set only the required bits */
  64. #define BIT_SET(var, shift, mask, val) \
  65. do { \
  66. var = ((var) & ~((mask) << (shift))) \
  67. | ((val) << (shift)); \
  68. } while (0)
  69. /*
  70. * ccp2_print_status - Print current CCP2 module register values.
  71. */
  72. #define CCP2_PRINT_REGISTER(isp, name)\
  73. dev_dbg(isp->dev, "###CCP2 " #name "=0x%08x\n", \
  74. isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_##name))
  75. static void ccp2_print_status(struct isp_ccp2_device *ccp2)
  76. {
  77. struct isp_device *isp = to_isp_device(ccp2);
  78. dev_dbg(isp->dev, "-------------CCP2 Register dump-------------\n");
  79. CCP2_PRINT_REGISTER(isp, SYSCONFIG);
  80. CCP2_PRINT_REGISTER(isp, SYSSTATUS);
  81. CCP2_PRINT_REGISTER(isp, LC01_IRQENABLE);
  82. CCP2_PRINT_REGISTER(isp, LC01_IRQSTATUS);
  83. CCP2_PRINT_REGISTER(isp, LC23_IRQENABLE);
  84. CCP2_PRINT_REGISTER(isp, LC23_IRQSTATUS);
  85. CCP2_PRINT_REGISTER(isp, LCM_IRQENABLE);
  86. CCP2_PRINT_REGISTER(isp, LCM_IRQSTATUS);
  87. CCP2_PRINT_REGISTER(isp, CTRL);
  88. CCP2_PRINT_REGISTER(isp, LCx_CTRL(0));
  89. CCP2_PRINT_REGISTER(isp, LCx_CODE(0));
  90. CCP2_PRINT_REGISTER(isp, LCx_STAT_START(0));
  91. CCP2_PRINT_REGISTER(isp, LCx_STAT_SIZE(0));
  92. CCP2_PRINT_REGISTER(isp, LCx_SOF_ADDR(0));
  93. CCP2_PRINT_REGISTER(isp, LCx_EOF_ADDR(0));
  94. CCP2_PRINT_REGISTER(isp, LCx_DAT_START(0));
  95. CCP2_PRINT_REGISTER(isp, LCx_DAT_SIZE(0));
  96. CCP2_PRINT_REGISTER(isp, LCx_DAT_PING_ADDR(0));
  97. CCP2_PRINT_REGISTER(isp, LCx_DAT_PONG_ADDR(0));
  98. CCP2_PRINT_REGISTER(isp, LCx_DAT_OFST(0));
  99. CCP2_PRINT_REGISTER(isp, LCM_CTRL);
  100. CCP2_PRINT_REGISTER(isp, LCM_VSIZE);
  101. CCP2_PRINT_REGISTER(isp, LCM_HSIZE);
  102. CCP2_PRINT_REGISTER(isp, LCM_PREFETCH);
  103. CCP2_PRINT_REGISTER(isp, LCM_SRC_ADDR);
  104. CCP2_PRINT_REGISTER(isp, LCM_SRC_OFST);
  105. CCP2_PRINT_REGISTER(isp, LCM_DST_ADDR);
  106. CCP2_PRINT_REGISTER(isp, LCM_DST_OFST);
  107. dev_dbg(isp->dev, "--------------------------------------------\n");
  108. }
  109. /*
  110. * ccp2_reset - Reset the CCP2
  111. * @ccp2: pointer to ISP CCP2 device
  112. */
  113. static void ccp2_reset(struct isp_ccp2_device *ccp2)
  114. {
  115. struct isp_device *isp = to_isp_device(ccp2);
  116. int i = 0;
  117. /* Reset the CSI1/CCP2B and wait for reset to complete */
  118. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_SYSCONFIG,
  119. ISPCCP2_SYSCONFIG_SOFT_RESET);
  120. while (!(isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_SYSSTATUS) &
  121. ISPCCP2_SYSSTATUS_RESET_DONE)) {
  122. udelay(10);
  123. if (i++ > 10) { /* try read 10 times */
  124. dev_warn(isp->dev,
  125. "omap3_isp: timeout waiting for ccp2 reset\n");
  126. break;
  127. }
  128. }
  129. }
  130. /*
  131. * ccp2_pwr_cfg - Configure the power mode settings
  132. * @ccp2: pointer to ISP CCP2 device
  133. */
  134. static void ccp2_pwr_cfg(struct isp_ccp2_device *ccp2)
  135. {
  136. struct isp_device *isp = to_isp_device(ccp2);
  137. isp_reg_writel(isp, ISPCCP2_SYSCONFIG_MSTANDBY_MODE_SMART |
  138. ((isp->revision == ISP_REVISION_15_0 && isp->autoidle) ?
  139. ISPCCP2_SYSCONFIG_AUTO_IDLE : 0),
  140. OMAP3_ISP_IOMEM_CCP2, ISPCCP2_SYSCONFIG);
  141. }
  142. /*
  143. * ccp2_if_enable - Enable CCP2 interface.
  144. * @ccp2: pointer to ISP CCP2 device
  145. * @enable: enable/disable flag
  146. */
  147. static void ccp2_if_enable(struct isp_ccp2_device *ccp2, u8 enable)
  148. {
  149. struct isp_device *isp = to_isp_device(ccp2);
  150. struct isp_pipeline *pipe = to_isp_pipeline(&ccp2->subdev.entity);
  151. int i;
  152. if (enable && ccp2->vdds_csib)
  153. regulator_enable(ccp2->vdds_csib);
  154. /* Enable/Disable all the LCx channels */
  155. for (i = 0; i < CCP2_LCx_CHANS_NUM; i++)
  156. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCx_CTRL(i),
  157. ISPCCP2_LCx_CTRL_CHAN_EN,
  158. enable ? ISPCCP2_LCx_CTRL_CHAN_EN : 0);
  159. /* Enable/Disable ccp2 interface in ccp2 mode */
  160. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL,
  161. ISPCCP2_CTRL_MODE | ISPCCP2_CTRL_IF_EN,
  162. enable ? (ISPCCP2_CTRL_MODE | ISPCCP2_CTRL_IF_EN) : 0);
  163. /* For frame count propagation */
  164. if (pipe->do_propagation) {
  165. /* We may want the Frame Start IRQ from LC0 */
  166. if (enable)
  167. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCP2,
  168. ISPCCP2_LC01_IRQENABLE,
  169. ISPCCP2_LC01_IRQSTATUS_LC0_FS_IRQ);
  170. else
  171. isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCP2,
  172. ISPCCP2_LC01_IRQENABLE,
  173. ISPCCP2_LC01_IRQSTATUS_LC0_FS_IRQ);
  174. }
  175. if (!enable && ccp2->vdds_csib)
  176. regulator_disable(ccp2->vdds_csib);
  177. }
  178. /*
  179. * ccp2_mem_enable - Enable CCP2 memory interface.
  180. * @ccp2: pointer to ISP CCP2 device
  181. * @enable: enable/disable flag
  182. */
  183. static void ccp2_mem_enable(struct isp_ccp2_device *ccp2, u8 enable)
  184. {
  185. struct isp_device *isp = to_isp_device(ccp2);
  186. if (enable)
  187. ccp2_if_enable(ccp2, 0);
  188. /* Enable/Disable ccp2 interface in ccp2 mode */
  189. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL,
  190. ISPCCP2_CTRL_MODE, enable ? ISPCCP2_CTRL_MODE : 0);
  191. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_CTRL,
  192. ISPCCP2_LCM_CTRL_CHAN_EN,
  193. enable ? ISPCCP2_LCM_CTRL_CHAN_EN : 0);
  194. }
  195. /*
  196. * ccp2_phyif_config - Initialize CCP2 phy interface config
  197. * @ccp2: Pointer to ISP CCP2 device
  198. * @config: CCP2 platform data
  199. *
  200. * Configure the CCP2 physical interface module from platform data.
  201. *
  202. * Returns -EIO if strobe is chosen in CSI1 mode, or 0 on success.
  203. */
  204. static int ccp2_phyif_config(struct isp_ccp2_device *ccp2,
  205. const struct isp_ccp2_platform_data *pdata)
  206. {
  207. struct isp_device *isp = to_isp_device(ccp2);
  208. u32 val;
  209. /* CCP2B mode */
  210. val = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL) |
  211. ISPCCP2_CTRL_IO_OUT_SEL | ISPCCP2_CTRL_MODE;
  212. /* Data/strobe physical layer */
  213. BIT_SET(val, ISPCCP2_CTRL_PHY_SEL_SHIFT, ISPCCP2_CTRL_PHY_SEL_MASK,
  214. pdata->phy_layer);
  215. BIT_SET(val, ISPCCP2_CTRL_INV_SHIFT, ISPCCP2_CTRL_INV_MASK,
  216. pdata->strobe_clk_pol);
  217. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL);
  218. val = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL);
  219. if (!(val & ISPCCP2_CTRL_MODE)) {
  220. if (pdata->ccp2_mode == ISP_CCP2_MODE_CCP2)
  221. dev_warn(isp->dev, "OMAP3 CCP2 bus not available\n");
  222. if (pdata->phy_layer == ISP_CCP2_PHY_DATA_STROBE)
  223. /* Strobe mode requires CCP2 */
  224. return -EIO;
  225. }
  226. return 0;
  227. }
  228. /*
  229. * ccp2_vp_config - Initialize CCP2 video port interface.
  230. * @ccp2: Pointer to ISP CCP2 device
  231. * @vpclk_div: Video port divisor
  232. *
  233. * Configure the CCP2 video port with the given clock divisor. The valid divisor
  234. * values depend on the ISP revision:
  235. *
  236. * - revision 1.0 and 2.0 1 to 4
  237. * - revision 15.0 1 to 65536
  238. *
  239. * The exact divisor value used might differ from the requested value, as ISP
  240. * revision 15.0 represent the divisor by 65536 divided by an integer.
  241. */
  242. static void ccp2_vp_config(struct isp_ccp2_device *ccp2,
  243. unsigned int vpclk_div)
  244. {
  245. struct isp_device *isp = to_isp_device(ccp2);
  246. u32 val;
  247. /* ISPCCP2_CTRL Video port */
  248. val = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL);
  249. val |= ISPCCP2_CTRL_VP_ONLY_EN; /* Disable the memory write port */
  250. if (isp->revision == ISP_REVISION_15_0) {
  251. vpclk_div = clamp_t(unsigned int, vpclk_div, 1, 65536);
  252. vpclk_div = min(ISPCCP2_VPCLK_FRACDIV / vpclk_div, 65535U);
  253. BIT_SET(val, ISPCCP2_CTRL_VPCLK_DIV_SHIFT,
  254. ISPCCP2_CTRL_VPCLK_DIV_MASK, vpclk_div);
  255. } else {
  256. vpclk_div = clamp_t(unsigned int, vpclk_div, 1, 4);
  257. BIT_SET(val, ISPCCP2_CTRL_VP_OUT_CTRL_SHIFT,
  258. ISPCCP2_CTRL_VP_OUT_CTRL_MASK, vpclk_div - 1);
  259. }
  260. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL);
  261. }
  262. /*
  263. * ccp2_lcx_config - Initialize CCP2 logical channel interface.
  264. * @ccp2: Pointer to ISP CCP2 device
  265. * @config: Pointer to ISP LCx config structure.
  266. *
  267. * This will analyze the parameters passed by the interface config
  268. * and configure CSI1/CCP2 logical channel
  269. *
  270. */
  271. static void ccp2_lcx_config(struct isp_ccp2_device *ccp2,
  272. struct isp_interface_lcx_config *config)
  273. {
  274. struct isp_device *isp = to_isp_device(ccp2);
  275. u32 val, format;
  276. switch (config->format) {
  277. case V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8:
  278. format = ISPCCP2_LCx_CTRL_FORMAT_RAW8_DPCM10_VP;
  279. break;
  280. case V4L2_MBUS_FMT_SGRBG10_1X10:
  281. default:
  282. format = ISPCCP2_LCx_CTRL_FORMAT_RAW10_VP; /* RAW10+VP */
  283. break;
  284. }
  285. /* ISPCCP2_LCx_CTRL logical channel #0 */
  286. val = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCx_CTRL(0))
  287. | (ISPCCP2_LCx_CTRL_REGION_EN); /* Region */
  288. if (isp->revision == ISP_REVISION_15_0) {
  289. /* CRC */
  290. BIT_SET(val, ISPCCP2_LCx_CTRL_CRC_SHIFT_15_0,
  291. ISPCCP2_LCx_CTRL_CRC_MASK,
  292. config->crc);
  293. /* Format = RAW10+VP or RAW8+DPCM10+VP*/
  294. BIT_SET(val, ISPCCP2_LCx_CTRL_FORMAT_SHIFT_15_0,
  295. ISPCCP2_LCx_CTRL_FORMAT_MASK_15_0, format);
  296. } else {
  297. BIT_SET(val, ISPCCP2_LCx_CTRL_CRC_SHIFT,
  298. ISPCCP2_LCx_CTRL_CRC_MASK,
  299. config->crc);
  300. BIT_SET(val, ISPCCP2_LCx_CTRL_FORMAT_SHIFT,
  301. ISPCCP2_LCx_CTRL_FORMAT_MASK, format);
  302. }
  303. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCx_CTRL(0));
  304. /* ISPCCP2_DAT_START for logical channel #0 */
  305. isp_reg_writel(isp, config->data_start << ISPCCP2_LCx_DAT_SHIFT,
  306. OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCx_DAT_START(0));
  307. /* ISPCCP2_DAT_SIZE for logical channel #0 */
  308. isp_reg_writel(isp, config->data_size << ISPCCP2_LCx_DAT_SHIFT,
  309. OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCx_DAT_SIZE(0));
  310. /* Enable error IRQs for logical channel #0 */
  311. val = ISPCCP2_LC01_IRQSTATUS_LC0_FIFO_OVF_IRQ |
  312. ISPCCP2_LC01_IRQSTATUS_LC0_CRC_IRQ |
  313. ISPCCP2_LC01_IRQSTATUS_LC0_FSP_IRQ |
  314. ISPCCP2_LC01_IRQSTATUS_LC0_FW_IRQ |
  315. ISPCCP2_LC01_IRQSTATUS_LC0_FS_IRQ |
  316. ISPCCP2_LC01_IRQSTATUS_LC0_FSC_IRQ |
  317. ISPCCP2_LC01_IRQSTATUS_LC0_SSC_IRQ;
  318. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LC01_IRQSTATUS);
  319. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LC01_IRQENABLE, val);
  320. }
  321. /*
  322. * ccp2_if_configure - Configure ccp2 with data from sensor
  323. * @ccp2: Pointer to ISP CCP2 device
  324. *
  325. * Return 0 on success or a negative error code
  326. */
  327. static int ccp2_if_configure(struct isp_ccp2_device *ccp2)
  328. {
  329. const struct isp_v4l2_subdevs_group *pdata;
  330. struct v4l2_mbus_framefmt *format;
  331. struct media_pad *pad;
  332. struct v4l2_subdev *sensor;
  333. u32 lines = 0;
  334. int ret;
  335. ccp2_pwr_cfg(ccp2);
  336. pad = media_entity_remote_source(&ccp2->pads[CCP2_PAD_SINK]);
  337. sensor = media_entity_to_v4l2_subdev(pad->entity);
  338. pdata = sensor->host_priv;
  339. ret = ccp2_phyif_config(ccp2, &pdata->bus.ccp2);
  340. if (ret < 0)
  341. return ret;
  342. ccp2_vp_config(ccp2, pdata->bus.ccp2.vpclk_div + 1);
  343. v4l2_subdev_call(sensor, sensor, g_skip_top_lines, &lines);
  344. format = &ccp2->formats[CCP2_PAD_SINK];
  345. ccp2->if_cfg.data_start = lines;
  346. ccp2->if_cfg.crc = pdata->bus.ccp2.crc;
  347. ccp2->if_cfg.format = format->code;
  348. ccp2->if_cfg.data_size = format->height;
  349. ccp2_lcx_config(ccp2, &ccp2->if_cfg);
  350. return 0;
  351. }
  352. static int ccp2_adjust_bandwidth(struct isp_ccp2_device *ccp2)
  353. {
  354. struct isp_pipeline *pipe = to_isp_pipeline(&ccp2->subdev.entity);
  355. struct isp_device *isp = to_isp_device(ccp2);
  356. const struct v4l2_mbus_framefmt *ofmt = &ccp2->formats[CCP2_PAD_SOURCE];
  357. unsigned long l3_ick = pipe->l3_ick;
  358. struct v4l2_fract *timeperframe;
  359. unsigned int vpclk_div = 2;
  360. unsigned int value;
  361. u64 bound;
  362. u64 area;
  363. /* Compute the minimum clock divisor, based on the pipeline maximum
  364. * data rate. This is an absolute lower bound if we don't want SBL
  365. * overflows, so round the value up.
  366. */
  367. vpclk_div = max_t(unsigned int, DIV_ROUND_UP(l3_ick, pipe->max_rate),
  368. vpclk_div);
  369. /* Compute the maximum clock divisor, based on the requested frame rate.
  370. * This is a soft lower bound to achieve a frame rate equal or higher
  371. * than the requested value, so round the value down.
  372. */
  373. timeperframe = &pipe->max_timeperframe;
  374. if (timeperframe->numerator) {
  375. area = ofmt->width * ofmt->height;
  376. bound = div_u64(area * timeperframe->denominator,
  377. timeperframe->numerator);
  378. value = min_t(u64, bound, l3_ick);
  379. vpclk_div = max_t(unsigned int, l3_ick / value, vpclk_div);
  380. }
  381. dev_dbg(isp->dev, "%s: minimum clock divisor = %u\n", __func__,
  382. vpclk_div);
  383. return vpclk_div;
  384. }
  385. /*
  386. * ccp2_mem_configure - Initialize CCP2 memory input/output interface
  387. * @ccp2: Pointer to ISP CCP2 device
  388. * @config: Pointer to ISP mem interface config structure
  389. *
  390. * This will analyze the parameters passed by the interface config
  391. * structure, and configure the respective registers for proper
  392. * CSI1/CCP2 memory input.
  393. */
  394. static void ccp2_mem_configure(struct isp_ccp2_device *ccp2,
  395. struct isp_interface_mem_config *config)
  396. {
  397. struct isp_device *isp = to_isp_device(ccp2);
  398. u32 sink_pixcode = ccp2->formats[CCP2_PAD_SINK].code;
  399. u32 source_pixcode = ccp2->formats[CCP2_PAD_SOURCE].code;
  400. unsigned int dpcm_decompress = 0;
  401. u32 val, hwords;
  402. if (sink_pixcode != source_pixcode &&
  403. sink_pixcode == V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8)
  404. dpcm_decompress = 1;
  405. ccp2_pwr_cfg(ccp2);
  406. /* Hsize, Skip */
  407. isp_reg_writel(isp, ISPCCP2_LCM_HSIZE_SKIP_MIN |
  408. (config->hsize_count << ISPCCP2_LCM_HSIZE_SHIFT),
  409. OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_HSIZE);
  410. /* Vsize, no. of lines */
  411. isp_reg_writel(isp, config->vsize_count << ISPCCP2_LCM_VSIZE_SHIFT,
  412. OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_VSIZE);
  413. if (ccp2->video_in.bpl_padding == 0)
  414. config->src_ofst = 0;
  415. else
  416. config->src_ofst = ccp2->video_in.bpl_value;
  417. isp_reg_writel(isp, config->src_ofst, OMAP3_ISP_IOMEM_CCP2,
  418. ISPCCP2_LCM_SRC_OFST);
  419. /* Source and Destination formats */
  420. val = ISPCCP2_LCM_CTRL_DST_FORMAT_RAW10 <<
  421. ISPCCP2_LCM_CTRL_DST_FORMAT_SHIFT;
  422. if (dpcm_decompress) {
  423. /* source format is RAW8 */
  424. val |= ISPCCP2_LCM_CTRL_SRC_FORMAT_RAW8 <<
  425. ISPCCP2_LCM_CTRL_SRC_FORMAT_SHIFT;
  426. /* RAW8 + DPCM10 - simple predictor */
  427. val |= ISPCCP2_LCM_CTRL_SRC_DPCM_PRED;
  428. /* enable source DPCM decompression */
  429. val |= ISPCCP2_LCM_CTRL_SRC_DECOMPR_DPCM10 <<
  430. ISPCCP2_LCM_CTRL_SRC_DECOMPR_SHIFT;
  431. } else {
  432. /* source format is RAW10 */
  433. val |= ISPCCP2_LCM_CTRL_SRC_FORMAT_RAW10 <<
  434. ISPCCP2_LCM_CTRL_SRC_FORMAT_SHIFT;
  435. }
  436. /* Burst size to 32x64 */
  437. val |= ISPCCP2_LCM_CTRL_BURST_SIZE_32X <<
  438. ISPCCP2_LCM_CTRL_BURST_SIZE_SHIFT;
  439. isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_CTRL);
  440. /* Prefetch setup */
  441. if (dpcm_decompress)
  442. hwords = (ISPCCP2_LCM_HSIZE_SKIP_MIN +
  443. config->hsize_count) >> 3;
  444. else
  445. hwords = (ISPCCP2_LCM_HSIZE_SKIP_MIN +
  446. config->hsize_count) >> 2;
  447. isp_reg_writel(isp, hwords << ISPCCP2_LCM_PREFETCH_SHIFT,
  448. OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_PREFETCH);
  449. /* Video port */
  450. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_CTRL,
  451. ISPCCP2_CTRL_IO_OUT_SEL | ISPCCP2_CTRL_MODE);
  452. ccp2_vp_config(ccp2, ccp2_adjust_bandwidth(ccp2));
  453. /* Clear LCM interrupts */
  454. isp_reg_writel(isp, ISPCCP2_LCM_IRQSTATUS_OCPERROR_IRQ |
  455. ISPCCP2_LCM_IRQSTATUS_EOF_IRQ,
  456. OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_IRQSTATUS);
  457. /* Enable LCM interupts */
  458. isp_reg_set(isp, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_IRQENABLE,
  459. ISPCCP2_LCM_IRQSTATUS_EOF_IRQ |
  460. ISPCCP2_LCM_IRQSTATUS_OCPERROR_IRQ);
  461. }
  462. /*
  463. * ccp2_set_inaddr - Sets memory address of input frame.
  464. * @ccp2: Pointer to ISP CCP2 device
  465. * @addr: 32bit memory address aligned on 32byte boundary.
  466. *
  467. * Configures the memory address from which the input frame is to be read.
  468. */
  469. static void ccp2_set_inaddr(struct isp_ccp2_device *ccp2, u32 addr)
  470. {
  471. struct isp_device *isp = to_isp_device(ccp2);
  472. isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_CCP2, ISPCCP2_LCM_SRC_ADDR);
  473. }
  474. /* -----------------------------------------------------------------------------
  475. * Interrupt handling
  476. */
  477. static void ccp2_isr_buffer(struct isp_ccp2_device *ccp2)
  478. {
  479. struct isp_pipeline *pipe = to_isp_pipeline(&ccp2->subdev.entity);
  480. struct isp_buffer *buffer;
  481. buffer = omap3isp_video_buffer_next(&ccp2->video_in);
  482. if (buffer != NULL)
  483. ccp2_set_inaddr(ccp2, buffer->isp_addr);
  484. pipe->state |= ISP_PIPELINE_IDLE_INPUT;
  485. if (ccp2->state == ISP_PIPELINE_STREAM_SINGLESHOT) {
  486. if (isp_pipeline_ready(pipe))
  487. omap3isp_pipeline_set_stream(pipe,
  488. ISP_PIPELINE_STREAM_SINGLESHOT);
  489. }
  490. }
  491. /*
  492. * omap3isp_ccp2_isr - Handle ISP CCP2 interrupts
  493. * @ccp2: Pointer to ISP CCP2 device
  494. *
  495. * This will handle the CCP2 interrupts
  496. */
  497. void omap3isp_ccp2_isr(struct isp_ccp2_device *ccp2)
  498. {
  499. struct isp_pipeline *pipe = to_isp_pipeline(&ccp2->subdev.entity);
  500. struct isp_device *isp = to_isp_device(ccp2);
  501. static const u32 ISPCCP2_LC01_ERROR =
  502. ISPCCP2_LC01_IRQSTATUS_LC0_FIFO_OVF_IRQ |
  503. ISPCCP2_LC01_IRQSTATUS_LC0_CRC_IRQ |
  504. ISPCCP2_LC01_IRQSTATUS_LC0_FSP_IRQ |
  505. ISPCCP2_LC01_IRQSTATUS_LC0_FW_IRQ |
  506. ISPCCP2_LC01_IRQSTATUS_LC0_FSC_IRQ |
  507. ISPCCP2_LC01_IRQSTATUS_LC0_SSC_IRQ;
  508. u32 lcx_irqstatus, lcm_irqstatus;
  509. /* First clear the interrupts */
  510. lcx_irqstatus = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2,
  511. ISPCCP2_LC01_IRQSTATUS);
  512. isp_reg_writel(isp, lcx_irqstatus, OMAP3_ISP_IOMEM_CCP2,
  513. ISPCCP2_LC01_IRQSTATUS);
  514. lcm_irqstatus = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCP2,
  515. ISPCCP2_LCM_IRQSTATUS);
  516. isp_reg_writel(isp, lcm_irqstatus, OMAP3_ISP_IOMEM_CCP2,
  517. ISPCCP2_LCM_IRQSTATUS);
  518. /* Errors */
  519. if (lcx_irqstatus & ISPCCP2_LC01_ERROR) {
  520. pipe->error = true;
  521. dev_dbg(isp->dev, "CCP2 err:%x\n", lcx_irqstatus);
  522. return;
  523. }
  524. if (lcm_irqstatus & ISPCCP2_LCM_IRQSTATUS_OCPERROR_IRQ) {
  525. pipe->error = true;
  526. dev_dbg(isp->dev, "CCP2 OCP err:%x\n", lcm_irqstatus);
  527. }
  528. if (omap3isp_module_sync_is_stopping(&ccp2->wait, &ccp2->stopping))
  529. return;
  530. /* Frame number propagation */
  531. if (lcx_irqstatus & ISPCCP2_LC01_IRQSTATUS_LC0_FS_IRQ) {
  532. struct isp_pipeline *pipe =
  533. to_isp_pipeline(&ccp2->subdev.entity);
  534. if (pipe->do_propagation)
  535. atomic_inc(&pipe->frame_number);
  536. }
  537. /* Handle queued buffers on frame end interrupts */
  538. if (lcm_irqstatus & ISPCCP2_LCM_IRQSTATUS_EOF_IRQ)
  539. ccp2_isr_buffer(ccp2);
  540. }
  541. /* -----------------------------------------------------------------------------
  542. * V4L2 subdev operations
  543. */
  544. static const unsigned int ccp2_fmts[] = {
  545. V4L2_MBUS_FMT_SGRBG10_1X10,
  546. V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
  547. };
  548. /*
  549. * __ccp2_get_format - helper function for getting ccp2 format
  550. * @ccp2 : Pointer to ISP CCP2 device
  551. * @fh : V4L2 subdev file handle
  552. * @pad : pad number
  553. * @which : wanted subdev format
  554. * return format structure or NULL on error
  555. */
  556. static struct v4l2_mbus_framefmt *
  557. __ccp2_get_format(struct isp_ccp2_device *ccp2, struct v4l2_subdev_fh *fh,
  558. unsigned int pad, enum v4l2_subdev_format_whence which)
  559. {
  560. if (which == V4L2_SUBDEV_FORMAT_TRY)
  561. return v4l2_subdev_get_try_format(fh, pad);
  562. else
  563. return &ccp2->formats[pad];
  564. }
  565. /*
  566. * ccp2_try_format - Handle try format by pad subdev method
  567. * @ccp2 : Pointer to ISP CCP2 device
  568. * @fh : V4L2 subdev file handle
  569. * @pad : pad num
  570. * @fmt : pointer to v4l2 mbus format structure
  571. * @which : wanted subdev format
  572. */
  573. static void ccp2_try_format(struct isp_ccp2_device *ccp2,
  574. struct v4l2_subdev_fh *fh, unsigned int pad,
  575. struct v4l2_mbus_framefmt *fmt,
  576. enum v4l2_subdev_format_whence which)
  577. {
  578. struct v4l2_mbus_framefmt *format;
  579. switch (pad) {
  580. case CCP2_PAD_SINK:
  581. if (fmt->code != V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8)
  582. fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  583. if (ccp2->input == CCP2_INPUT_SENSOR) {
  584. fmt->width = clamp_t(u32, fmt->width,
  585. ISPCCP2_DAT_START_MIN,
  586. ISPCCP2_DAT_START_MAX);
  587. fmt->height = clamp_t(u32, fmt->height,
  588. ISPCCP2_DAT_SIZE_MIN,
  589. ISPCCP2_DAT_SIZE_MAX);
  590. } else if (ccp2->input == CCP2_INPUT_MEMORY) {
  591. fmt->width = clamp_t(u32, fmt->width,
  592. ISPCCP2_LCM_HSIZE_COUNT_MIN,
  593. ISPCCP2_LCM_HSIZE_COUNT_MAX);
  594. fmt->height = clamp_t(u32, fmt->height,
  595. ISPCCP2_LCM_VSIZE_MIN,
  596. ISPCCP2_LCM_VSIZE_MAX);
  597. }
  598. break;
  599. case CCP2_PAD_SOURCE:
  600. /* Source format - copy sink format and change pixel code
  601. * to SGRBG10_1X10 as we don't support CCP2 write to memory.
  602. * When CCP2 write to memory feature will be added this
  603. * should be changed properly.
  604. */
  605. format = __ccp2_get_format(ccp2, fh, CCP2_PAD_SINK, which);
  606. memcpy(fmt, format, sizeof(*fmt));
  607. fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  608. break;
  609. }
  610. fmt->field = V4L2_FIELD_NONE;
  611. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  612. }
  613. /*
  614. * ccp2_enum_mbus_code - Handle pixel format enumeration
  615. * @sd : pointer to v4l2 subdev structure
  616. * @fh : V4L2 subdev file handle
  617. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  618. * return -EINVAL or zero on success
  619. */
  620. static int ccp2_enum_mbus_code(struct v4l2_subdev *sd,
  621. struct v4l2_subdev_fh *fh,
  622. struct v4l2_subdev_mbus_code_enum *code)
  623. {
  624. struct isp_ccp2_device *ccp2 = v4l2_get_subdevdata(sd);
  625. struct v4l2_mbus_framefmt *format;
  626. if (code->pad == CCP2_PAD_SINK) {
  627. if (code->index >= ARRAY_SIZE(ccp2_fmts))
  628. return -EINVAL;
  629. code->code = ccp2_fmts[code->index];
  630. } else {
  631. if (code->index != 0)
  632. return -EINVAL;
  633. format = __ccp2_get_format(ccp2, fh, CCP2_PAD_SINK,
  634. V4L2_SUBDEV_FORMAT_TRY);
  635. code->code = format->code;
  636. }
  637. return 0;
  638. }
  639. static int ccp2_enum_frame_size(struct v4l2_subdev *sd,
  640. struct v4l2_subdev_fh *fh,
  641. struct v4l2_subdev_frame_size_enum *fse)
  642. {
  643. struct isp_ccp2_device *ccp2 = v4l2_get_subdevdata(sd);
  644. struct v4l2_mbus_framefmt format;
  645. if (fse->index != 0)
  646. return -EINVAL;
  647. format.code = fse->code;
  648. format.width = 1;
  649. format.height = 1;
  650. ccp2_try_format(ccp2, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  651. fse->min_width = format.width;
  652. fse->min_height = format.height;
  653. if (format.code != fse->code)
  654. return -EINVAL;
  655. format.code = fse->code;
  656. format.width = -1;
  657. format.height = -1;
  658. ccp2_try_format(ccp2, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  659. fse->max_width = format.width;
  660. fse->max_height = format.height;
  661. return 0;
  662. }
  663. /*
  664. * ccp2_get_format - Handle get format by pads subdev method
  665. * @sd : pointer to v4l2 subdev structure
  666. * @fh : V4L2 subdev file handle
  667. * @fmt : pointer to v4l2 subdev format structure
  668. * return -EINVAL or zero on success
  669. */
  670. static int ccp2_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  671. struct v4l2_subdev_format *fmt)
  672. {
  673. struct isp_ccp2_device *ccp2 = v4l2_get_subdevdata(sd);
  674. struct v4l2_mbus_framefmt *format;
  675. format = __ccp2_get_format(ccp2, fh, fmt->pad, fmt->which);
  676. if (format == NULL)
  677. return -EINVAL;
  678. fmt->format = *format;
  679. return 0;
  680. }
  681. /*
  682. * ccp2_set_format - Handle set format by pads subdev method
  683. * @sd : pointer to v4l2 subdev structure
  684. * @fh : V4L2 subdev file handle
  685. * @fmt : pointer to v4l2 subdev format structure
  686. * returns zero
  687. */
  688. static int ccp2_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  689. struct v4l2_subdev_format *fmt)
  690. {
  691. struct isp_ccp2_device *ccp2 = v4l2_get_subdevdata(sd);
  692. struct v4l2_mbus_framefmt *format;
  693. format = __ccp2_get_format(ccp2, fh, fmt->pad, fmt->which);
  694. if (format == NULL)
  695. return -EINVAL;
  696. ccp2_try_format(ccp2, fh, fmt->pad, &fmt->format, fmt->which);
  697. *format = fmt->format;
  698. /* Propagate the format from sink to source */
  699. if (fmt->pad == CCP2_PAD_SINK) {
  700. format = __ccp2_get_format(ccp2, fh, CCP2_PAD_SOURCE,
  701. fmt->which);
  702. *format = fmt->format;
  703. ccp2_try_format(ccp2, fh, CCP2_PAD_SOURCE, format, fmt->which);
  704. }
  705. return 0;
  706. }
  707. /*
  708. * ccp2_init_formats - Initialize formats on all pads
  709. * @sd: ISP CCP2 V4L2 subdevice
  710. * @fh: V4L2 subdev file handle
  711. *
  712. * Initialize all pad formats with default values. If fh is not NULL, try
  713. * formats are initialized on the file handle. Otherwise active formats are
  714. * initialized on the device.
  715. */
  716. static int ccp2_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  717. {
  718. struct v4l2_subdev_format format;
  719. memset(&format, 0, sizeof(format));
  720. format.pad = CCP2_PAD_SINK;
  721. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  722. format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  723. format.format.width = 4096;
  724. format.format.height = 4096;
  725. ccp2_set_format(sd, fh, &format);
  726. return 0;
  727. }
  728. /*
  729. * ccp2_s_stream - Enable/Disable streaming on ccp2 subdev
  730. * @sd : pointer to v4l2 subdev structure
  731. * @enable: 1 == Enable, 0 == Disable
  732. * return zero
  733. */
  734. static int ccp2_s_stream(struct v4l2_subdev *sd, int enable)
  735. {
  736. struct isp_ccp2_device *ccp2 = v4l2_get_subdevdata(sd);
  737. struct isp_device *isp = to_isp_device(ccp2);
  738. struct device *dev = to_device(ccp2);
  739. int ret;
  740. if (ccp2->state == ISP_PIPELINE_STREAM_STOPPED) {
  741. if (enable == ISP_PIPELINE_STREAM_STOPPED)
  742. return 0;
  743. atomic_set(&ccp2->stopping, 0);
  744. }
  745. switch (enable) {
  746. case ISP_PIPELINE_STREAM_CONTINUOUS:
  747. if (ccp2->phy) {
  748. ret = omap3isp_csiphy_acquire(ccp2->phy);
  749. if (ret < 0)
  750. return ret;
  751. }
  752. ccp2_if_configure(ccp2);
  753. ccp2_print_status(ccp2);
  754. /* Enable CSI1/CCP2 interface */
  755. ccp2_if_enable(ccp2, 1);
  756. break;
  757. case ISP_PIPELINE_STREAM_SINGLESHOT:
  758. if (ccp2->state != ISP_PIPELINE_STREAM_SINGLESHOT) {
  759. struct v4l2_mbus_framefmt *format;
  760. format = &ccp2->formats[CCP2_PAD_SINK];
  761. ccp2->mem_cfg.hsize_count = format->width;
  762. ccp2->mem_cfg.vsize_count = format->height;
  763. ccp2->mem_cfg.src_ofst = 0;
  764. ccp2_mem_configure(ccp2, &ccp2->mem_cfg);
  765. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CSI1_READ);
  766. ccp2_print_status(ccp2);
  767. }
  768. ccp2_mem_enable(ccp2, 1);
  769. break;
  770. case ISP_PIPELINE_STREAM_STOPPED:
  771. if (omap3isp_module_sync_idle(&sd->entity, &ccp2->wait,
  772. &ccp2->stopping))
  773. dev_dbg(dev, "%s: module stop timeout.\n", sd->name);
  774. if (ccp2->input == CCP2_INPUT_MEMORY) {
  775. ccp2_mem_enable(ccp2, 0);
  776. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CSI1_READ);
  777. } else if (ccp2->input == CCP2_INPUT_SENSOR) {
  778. /* Disable CSI1/CCP2 interface */
  779. ccp2_if_enable(ccp2, 0);
  780. if (ccp2->phy)
  781. omap3isp_csiphy_release(ccp2->phy);
  782. }
  783. break;
  784. }
  785. ccp2->state = enable;
  786. return 0;
  787. }
  788. /* subdev video operations */
  789. static const struct v4l2_subdev_video_ops ccp2_sd_video_ops = {
  790. .s_stream = ccp2_s_stream,
  791. };
  792. /* subdev pad operations */
  793. static const struct v4l2_subdev_pad_ops ccp2_sd_pad_ops = {
  794. .enum_mbus_code = ccp2_enum_mbus_code,
  795. .enum_frame_size = ccp2_enum_frame_size,
  796. .get_fmt = ccp2_get_format,
  797. .set_fmt = ccp2_set_format,
  798. };
  799. /* subdev operations */
  800. static const struct v4l2_subdev_ops ccp2_sd_ops = {
  801. .video = &ccp2_sd_video_ops,
  802. .pad = &ccp2_sd_pad_ops,
  803. };
  804. /* subdev internal operations */
  805. static const struct v4l2_subdev_internal_ops ccp2_sd_internal_ops = {
  806. .open = ccp2_init_formats,
  807. };
  808. /* --------------------------------------------------------------------------
  809. * ISP ccp2 video device node
  810. */
  811. /*
  812. * ccp2_video_queue - Queue video buffer.
  813. * @video : Pointer to isp video structure
  814. * @buffer: Pointer to isp_buffer structure
  815. * return -EIO or zero on success
  816. */
  817. static int ccp2_video_queue(struct isp_video *video, struct isp_buffer *buffer)
  818. {
  819. struct isp_ccp2_device *ccp2 = &video->isp->isp_ccp2;
  820. ccp2_set_inaddr(ccp2, buffer->isp_addr);
  821. return 0;
  822. }
  823. static const struct isp_video_operations ccp2_video_ops = {
  824. .queue = ccp2_video_queue,
  825. };
  826. /* -----------------------------------------------------------------------------
  827. * Media entity operations
  828. */
  829. /*
  830. * ccp2_link_setup - Setup ccp2 connections.
  831. * @entity : Pointer to media entity structure
  832. * @local : Pointer to local pad array
  833. * @remote : Pointer to remote pad array
  834. * @flags : Link flags
  835. * return -EINVAL on error or zero on success
  836. */
  837. static int ccp2_link_setup(struct media_entity *entity,
  838. const struct media_pad *local,
  839. const struct media_pad *remote, u32 flags)
  840. {
  841. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  842. struct isp_ccp2_device *ccp2 = v4l2_get_subdevdata(sd);
  843. switch (local->index | media_entity_type(remote->entity)) {
  844. case CCP2_PAD_SINK | MEDIA_ENT_T_DEVNODE:
  845. /* read from memory */
  846. if (flags & MEDIA_LNK_FL_ENABLED) {
  847. if (ccp2->input == CCP2_INPUT_SENSOR)
  848. return -EBUSY;
  849. ccp2->input = CCP2_INPUT_MEMORY;
  850. } else {
  851. if (ccp2->input == CCP2_INPUT_MEMORY)
  852. ccp2->input = CCP2_INPUT_NONE;
  853. }
  854. break;
  855. case CCP2_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
  856. /* read from sensor/phy */
  857. if (flags & MEDIA_LNK_FL_ENABLED) {
  858. if (ccp2->input == CCP2_INPUT_MEMORY)
  859. return -EBUSY;
  860. ccp2->input = CCP2_INPUT_SENSOR;
  861. } else {
  862. if (ccp2->input == CCP2_INPUT_SENSOR)
  863. ccp2->input = CCP2_INPUT_NONE;
  864. } break;
  865. case CCP2_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
  866. /* write to video port/ccdc */
  867. if (flags & MEDIA_LNK_FL_ENABLED)
  868. ccp2->output = CCP2_OUTPUT_CCDC;
  869. else
  870. ccp2->output = CCP2_OUTPUT_NONE;
  871. break;
  872. default:
  873. return -EINVAL;
  874. }
  875. return 0;
  876. }
  877. /* media operations */
  878. static const struct media_entity_operations ccp2_media_ops = {
  879. .link_setup = ccp2_link_setup,
  880. };
  881. /*
  882. * omap3isp_ccp2_unregister_entities - Unregister media entities: subdev
  883. * @ccp2: Pointer to ISP CCP2 device
  884. */
  885. void omap3isp_ccp2_unregister_entities(struct isp_ccp2_device *ccp2)
  886. {
  887. v4l2_device_unregister_subdev(&ccp2->subdev);
  888. omap3isp_video_unregister(&ccp2->video_in);
  889. }
  890. /*
  891. * omap3isp_ccp2_register_entities - Register the subdev media entity
  892. * @ccp2: Pointer to ISP CCP2 device
  893. * @vdev: Pointer to v4l device
  894. * return negative error code or zero on success
  895. */
  896. int omap3isp_ccp2_register_entities(struct isp_ccp2_device *ccp2,
  897. struct v4l2_device *vdev)
  898. {
  899. int ret;
  900. /* Register the subdev and video nodes. */
  901. ret = v4l2_device_register_subdev(vdev, &ccp2->subdev);
  902. if (ret < 0)
  903. goto error;
  904. ret = omap3isp_video_register(&ccp2->video_in, vdev);
  905. if (ret < 0)
  906. goto error;
  907. return 0;
  908. error:
  909. omap3isp_ccp2_unregister_entities(ccp2);
  910. return ret;
  911. }
  912. /* -----------------------------------------------------------------------------
  913. * ISP ccp2 initialisation and cleanup
  914. */
  915. /*
  916. * ccp2_init_entities - Initialize ccp2 subdev and media entity.
  917. * @ccp2: Pointer to ISP CCP2 device
  918. * return negative error code or zero on success
  919. */
  920. static int ccp2_init_entities(struct isp_ccp2_device *ccp2)
  921. {
  922. struct v4l2_subdev *sd = &ccp2->subdev;
  923. struct media_pad *pads = ccp2->pads;
  924. struct media_entity *me = &sd->entity;
  925. int ret;
  926. ccp2->input = CCP2_INPUT_NONE;
  927. ccp2->output = CCP2_OUTPUT_NONE;
  928. v4l2_subdev_init(sd, &ccp2_sd_ops);
  929. sd->internal_ops = &ccp2_sd_internal_ops;
  930. strlcpy(sd->name, "OMAP3 ISP CCP2", sizeof(sd->name));
  931. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  932. v4l2_set_subdevdata(sd, ccp2);
  933. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  934. pads[CCP2_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  935. pads[CCP2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  936. me->ops = &ccp2_media_ops;
  937. ret = media_entity_init(me, CCP2_PADS_NUM, pads, 0);
  938. if (ret < 0)
  939. return ret;
  940. ccp2_init_formats(sd, NULL);
  941. /*
  942. * The CCP2 has weird line alignment requirements, possibly caused by
  943. * DPCM8 decompression. Line length for data read from memory must be a
  944. * multiple of 128 bits (16 bytes) in continuous mode (when no padding
  945. * is present at end of lines). Additionally, if padding is used, the
  946. * padded line length must be a multiple of 32 bytes. To simplify the
  947. * implementation we use a fixed 32 bytes alignment regardless of the
  948. * input format and width. If strict 128 bits alignment support is
  949. * required ispvideo will need to be made aware of this special dual
  950. * alignement requirements.
  951. */
  952. ccp2->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  953. ccp2->video_in.bpl_alignment = 32;
  954. ccp2->video_in.bpl_max = 0xffffffe0;
  955. ccp2->video_in.isp = to_isp_device(ccp2);
  956. ccp2->video_in.ops = &ccp2_video_ops;
  957. ccp2->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
  958. ret = omap3isp_video_init(&ccp2->video_in, "CCP2");
  959. if (ret < 0)
  960. goto error_video;
  961. /* Connect the video node to the ccp2 subdev. */
  962. ret = media_entity_create_link(&ccp2->video_in.video.entity, 0,
  963. &ccp2->subdev.entity, CCP2_PAD_SINK, 0);
  964. if (ret < 0)
  965. goto error_link;
  966. return 0;
  967. error_link:
  968. omap3isp_video_cleanup(&ccp2->video_in);
  969. error_video:
  970. media_entity_cleanup(&ccp2->subdev.entity);
  971. return ret;
  972. }
  973. /*
  974. * omap3isp_ccp2_init - CCP2 initialization.
  975. * @isp : Pointer to ISP device
  976. * return negative error code or zero on success
  977. */
  978. int omap3isp_ccp2_init(struct isp_device *isp)
  979. {
  980. struct isp_ccp2_device *ccp2 = &isp->isp_ccp2;
  981. int ret;
  982. init_waitqueue_head(&ccp2->wait);
  983. /*
  984. * On the OMAP34xx the CSI1 receiver is operated in the CSIb IO
  985. * complex, which is powered by vdds_csib power rail. Hence the
  986. * request for the regulator.
  987. *
  988. * On the OMAP36xx, the CCP2 uses the CSI PHY1 or PHY2, shared with
  989. * the CSI2c or CSI2a receivers. The PHY then needs to be explicitly
  990. * configured.
  991. *
  992. * TODO: Don't hardcode the usage of PHY1 (shared with CSI2c).
  993. */
  994. if (isp->revision == ISP_REVISION_2_0) {
  995. ccp2->vdds_csib = regulator_get(isp->dev, "vdds_csib");
  996. if (IS_ERR(ccp2->vdds_csib)) {
  997. dev_dbg(isp->dev,
  998. "Could not get regulator vdds_csib\n");
  999. ccp2->vdds_csib = NULL;
  1000. }
  1001. } else if (isp->revision == ISP_REVISION_15_0) {
  1002. ccp2->phy = &isp->isp_csiphy1;
  1003. }
  1004. ret = ccp2_init_entities(ccp2);
  1005. if (ret < 0) {
  1006. regulator_put(ccp2->vdds_csib);
  1007. return ret;
  1008. }
  1009. ccp2_reset(ccp2);
  1010. return 0;
  1011. }
  1012. /*
  1013. * omap3isp_ccp2_cleanup - CCP2 un-initialization
  1014. * @isp : Pointer to ISP device
  1015. */
  1016. void omap3isp_ccp2_cleanup(struct isp_device *isp)
  1017. {
  1018. struct isp_ccp2_device *ccp2 = &isp->isp_ccp2;
  1019. omap3isp_video_cleanup(&ccp2->video_in);
  1020. media_entity_cleanup(&ccp2->subdev.entity);
  1021. regulator_put(ccp2->vdds_csib);
  1022. }