ispresizer.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. /*
  2. * ispresizer.c
  3. *
  4. * TI OMAP3 ISP - Resizer 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/device.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include "isp.h"
  30. #include "ispreg.h"
  31. #include "ispresizer.h"
  32. /*
  33. * Resizer Constants
  34. */
  35. #define MIN_RESIZE_VALUE 64
  36. #define MID_RESIZE_VALUE 512
  37. #define MAX_RESIZE_VALUE 1024
  38. #define MIN_IN_WIDTH 32
  39. #define MIN_IN_HEIGHT 32
  40. #define MAX_IN_WIDTH_MEMORY_MODE 4095
  41. #define MAX_IN_WIDTH_ONTHEFLY_MODE_ES1 1280
  42. #define MAX_IN_WIDTH_ONTHEFLY_MODE_ES2 4095
  43. #define MAX_IN_HEIGHT 4095
  44. #define MIN_OUT_WIDTH 16
  45. #define MIN_OUT_HEIGHT 2
  46. #define MAX_OUT_HEIGHT 4095
  47. /*
  48. * Resizer Use Constraints
  49. * "TRM ES3.1, table 12-46"
  50. */
  51. #define MAX_4TAP_OUT_WIDTH_ES1 1280
  52. #define MAX_7TAP_OUT_WIDTH_ES1 640
  53. #define MAX_4TAP_OUT_WIDTH_ES2 3312
  54. #define MAX_7TAP_OUT_WIDTH_ES2 1650
  55. #define MAX_4TAP_OUT_WIDTH_3630 4096
  56. #define MAX_7TAP_OUT_WIDTH_3630 2048
  57. /*
  58. * Constants for ratio calculation
  59. */
  60. #define RESIZE_DIVISOR 256
  61. #define DEFAULT_PHASE 1
  62. /*
  63. * Default (and only) configuration of filter coefficients.
  64. * 7-tap mode is for scale factors 0.25x to 0.5x.
  65. * 4-tap mode is for scale factors 0.5x to 4.0x.
  66. * There shouldn't be any reason to recalculate these, EVER.
  67. */
  68. static const struct isprsz_coef filter_coefs = {
  69. /* For 8-phase 4-tap horizontal filter: */
  70. {
  71. 0x0000, 0x0100, 0x0000, 0x0000,
  72. 0x03FA, 0x00F6, 0x0010, 0x0000,
  73. 0x03F9, 0x00DB, 0x002C, 0x0000,
  74. 0x03FB, 0x00B3, 0x0053, 0x03FF,
  75. 0x03FD, 0x0082, 0x0084, 0x03FD,
  76. 0x03FF, 0x0053, 0x00B3, 0x03FB,
  77. 0x0000, 0x002C, 0x00DB, 0x03F9,
  78. 0x0000, 0x0010, 0x00F6, 0x03FA
  79. },
  80. /* For 8-phase 4-tap vertical filter: */
  81. {
  82. 0x0000, 0x0100, 0x0000, 0x0000,
  83. 0x03FA, 0x00F6, 0x0010, 0x0000,
  84. 0x03F9, 0x00DB, 0x002C, 0x0000,
  85. 0x03FB, 0x00B3, 0x0053, 0x03FF,
  86. 0x03FD, 0x0082, 0x0084, 0x03FD,
  87. 0x03FF, 0x0053, 0x00B3, 0x03FB,
  88. 0x0000, 0x002C, 0x00DB, 0x03F9,
  89. 0x0000, 0x0010, 0x00F6, 0x03FA
  90. },
  91. /* For 4-phase 7-tap horizontal filter: */
  92. #define DUMMY 0
  93. {
  94. 0x0004, 0x0023, 0x005A, 0x0058, 0x0023, 0x0004, 0x0000, DUMMY,
  95. 0x0002, 0x0018, 0x004d, 0x0060, 0x0031, 0x0008, 0x0000, DUMMY,
  96. 0x0001, 0x000f, 0x003f, 0x0062, 0x003f, 0x000f, 0x0001, DUMMY,
  97. 0x0000, 0x0008, 0x0031, 0x0060, 0x004d, 0x0018, 0x0002, DUMMY
  98. },
  99. /* For 4-phase 7-tap vertical filter: */
  100. {
  101. 0x0004, 0x0023, 0x005A, 0x0058, 0x0023, 0x0004, 0x0000, DUMMY,
  102. 0x0002, 0x0018, 0x004d, 0x0060, 0x0031, 0x0008, 0x0000, DUMMY,
  103. 0x0001, 0x000f, 0x003f, 0x0062, 0x003f, 0x000f, 0x0001, DUMMY,
  104. 0x0000, 0x0008, 0x0031, 0x0060, 0x004d, 0x0018, 0x0002, DUMMY
  105. }
  106. /*
  107. * The dummy padding is required in 7-tap mode because of how the
  108. * registers are arranged physically.
  109. */
  110. #undef DUMMY
  111. };
  112. /*
  113. * __resizer_get_format - helper function for getting resizer format
  114. * @res : pointer to resizer private structure
  115. * @pad : pad number
  116. * @fh : V4L2 subdev file handle
  117. * @which : wanted subdev format
  118. * return zero
  119. */
  120. static struct v4l2_mbus_framefmt *
  121. __resizer_get_format(struct isp_res_device *res, struct v4l2_subdev_fh *fh,
  122. unsigned int pad, enum v4l2_subdev_format_whence which)
  123. {
  124. if (which == V4L2_SUBDEV_FORMAT_TRY)
  125. return v4l2_subdev_get_try_format(fh, pad);
  126. else
  127. return &res->formats[pad];
  128. }
  129. /*
  130. * __resizer_get_crop - helper function for getting resizer crop rectangle
  131. * @res : pointer to resizer private structure
  132. * @fh : V4L2 subdev file handle
  133. * @which : wanted subdev crop rectangle
  134. */
  135. static struct v4l2_rect *
  136. __resizer_get_crop(struct isp_res_device *res, struct v4l2_subdev_fh *fh,
  137. enum v4l2_subdev_format_whence which)
  138. {
  139. if (which == V4L2_SUBDEV_FORMAT_TRY)
  140. return v4l2_subdev_get_try_crop(fh, RESZ_PAD_SINK);
  141. else
  142. return &res->crop.request;
  143. }
  144. /*
  145. * resizer_set_filters - Set resizer filters
  146. * @res: Device context.
  147. * @h_coeff: horizontal coefficient
  148. * @v_coeff: vertical coefficient
  149. * Return none
  150. */
  151. static void resizer_set_filters(struct isp_res_device *res, const u16 *h_coeff,
  152. const u16 *v_coeff)
  153. {
  154. struct isp_device *isp = to_isp_device(res);
  155. u32 startaddr_h, startaddr_v, tmp_h, tmp_v;
  156. int i;
  157. startaddr_h = ISPRSZ_HFILT10;
  158. startaddr_v = ISPRSZ_VFILT10;
  159. for (i = 0; i < COEFF_CNT; i += 2) {
  160. tmp_h = h_coeff[i] |
  161. (h_coeff[i + 1] << ISPRSZ_HFILT_COEF1_SHIFT);
  162. tmp_v = v_coeff[i] |
  163. (v_coeff[i + 1] << ISPRSZ_VFILT_COEF1_SHIFT);
  164. isp_reg_writel(isp, tmp_h, OMAP3_ISP_IOMEM_RESZ, startaddr_h);
  165. isp_reg_writel(isp, tmp_v, OMAP3_ISP_IOMEM_RESZ, startaddr_v);
  166. startaddr_h += 4;
  167. startaddr_v += 4;
  168. }
  169. }
  170. /*
  171. * resizer_set_bilinear - Chrominance horizontal algorithm select
  172. * @res: Device context.
  173. * @type: Filtering interpolation type.
  174. *
  175. * Filtering that is same as luminance processing is
  176. * intended only for downsampling, and bilinear interpolation
  177. * is intended only for upsampling.
  178. */
  179. static void resizer_set_bilinear(struct isp_res_device *res,
  180. enum resizer_chroma_algo type)
  181. {
  182. struct isp_device *isp = to_isp_device(res);
  183. if (type == RSZ_BILINEAR)
  184. isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  185. ISPRSZ_CNT_CBILIN);
  186. else
  187. isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  188. ISPRSZ_CNT_CBILIN);
  189. }
  190. /*
  191. * resizer_set_ycpos - Luminance and chrominance order
  192. * @res: Device context.
  193. * @order: order type.
  194. */
  195. static void resizer_set_ycpos(struct isp_res_device *res,
  196. enum v4l2_mbus_pixelcode pixelcode)
  197. {
  198. struct isp_device *isp = to_isp_device(res);
  199. switch (pixelcode) {
  200. case V4L2_MBUS_FMT_YUYV8_1X16:
  201. isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  202. ISPRSZ_CNT_YCPOS);
  203. break;
  204. case V4L2_MBUS_FMT_UYVY8_1X16:
  205. isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  206. ISPRSZ_CNT_YCPOS);
  207. break;
  208. default:
  209. return;
  210. }
  211. }
  212. /*
  213. * resizer_set_phase - Setup horizontal and vertical starting phase
  214. * @res: Device context.
  215. * @h_phase: horizontal phase parameters.
  216. * @v_phase: vertical phase parameters.
  217. *
  218. * Horizontal and vertical phase range is 0 to 7
  219. */
  220. static void resizer_set_phase(struct isp_res_device *res, u32 h_phase,
  221. u32 v_phase)
  222. {
  223. struct isp_device *isp = to_isp_device(res);
  224. u32 rgval = 0;
  225. rgval = isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT) &
  226. ~(ISPRSZ_CNT_HSTPH_MASK | ISPRSZ_CNT_VSTPH_MASK);
  227. rgval |= (h_phase << ISPRSZ_CNT_HSTPH_SHIFT) & ISPRSZ_CNT_HSTPH_MASK;
  228. rgval |= (v_phase << ISPRSZ_CNT_VSTPH_SHIFT) & ISPRSZ_CNT_VSTPH_MASK;
  229. isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT);
  230. }
  231. /*
  232. * resizer_set_luma - Setup luminance enhancer parameters
  233. * @res: Device context.
  234. * @luma: Structure for luminance enhancer parameters.
  235. *
  236. * Algorithm select:
  237. * 0x0: Disable
  238. * 0x1: [-1 2 -1]/2 high-pass filter
  239. * 0x2: [-1 -2 6 -2 -1]/4 high-pass filter
  240. *
  241. * Maximum gain:
  242. * The data is coded in U4Q4 representation.
  243. *
  244. * Slope:
  245. * The data is coded in U4Q4 representation.
  246. *
  247. * Coring offset:
  248. * The data is coded in U8Q0 representation.
  249. *
  250. * The new luminance value is computed as:
  251. * Y += HPF(Y) x max(GAIN, (HPF(Y) - CORE) x SLOP + 8) >> 4.
  252. */
  253. static void resizer_set_luma(struct isp_res_device *res,
  254. struct resizer_luma_yenh *luma)
  255. {
  256. struct isp_device *isp = to_isp_device(res);
  257. u32 rgval = 0;
  258. rgval = (luma->algo << ISPRSZ_YENH_ALGO_SHIFT)
  259. & ISPRSZ_YENH_ALGO_MASK;
  260. rgval |= (luma->gain << ISPRSZ_YENH_GAIN_SHIFT)
  261. & ISPRSZ_YENH_GAIN_MASK;
  262. rgval |= (luma->slope << ISPRSZ_YENH_SLOP_SHIFT)
  263. & ISPRSZ_YENH_SLOP_MASK;
  264. rgval |= (luma->core << ISPRSZ_YENH_CORE_SHIFT)
  265. & ISPRSZ_YENH_CORE_MASK;
  266. isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_YENH);
  267. }
  268. /*
  269. * resizer_set_source - Input source select
  270. * @res: Device context.
  271. * @source: Input source type
  272. *
  273. * If this field is set to RESIZER_INPUT_VP, the resizer input is fed from
  274. * Preview/CCDC engine, otherwise from memory.
  275. */
  276. static void resizer_set_source(struct isp_res_device *res,
  277. enum resizer_input_entity source)
  278. {
  279. struct isp_device *isp = to_isp_device(res);
  280. if (source == RESIZER_INPUT_MEMORY)
  281. isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  282. ISPRSZ_CNT_INPSRC);
  283. else
  284. isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  285. ISPRSZ_CNT_INPSRC);
  286. }
  287. /*
  288. * resizer_set_ratio - Setup horizontal and vertical resizing value
  289. * @res: Device context.
  290. * @ratio: Structure for ratio parameters.
  291. *
  292. * Resizing range from 64 to 1024
  293. */
  294. static void resizer_set_ratio(struct isp_res_device *res,
  295. const struct resizer_ratio *ratio)
  296. {
  297. struct isp_device *isp = to_isp_device(res);
  298. const u16 *h_filter, *v_filter;
  299. u32 rgval = 0;
  300. rgval = isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT) &
  301. ~(ISPRSZ_CNT_HRSZ_MASK | ISPRSZ_CNT_VRSZ_MASK);
  302. rgval |= ((ratio->horz - 1) << ISPRSZ_CNT_HRSZ_SHIFT)
  303. & ISPRSZ_CNT_HRSZ_MASK;
  304. rgval |= ((ratio->vert - 1) << ISPRSZ_CNT_VRSZ_SHIFT)
  305. & ISPRSZ_CNT_VRSZ_MASK;
  306. isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT);
  307. /* prepare horizontal filter coefficients */
  308. if (ratio->horz > MID_RESIZE_VALUE)
  309. h_filter = &filter_coefs.h_filter_coef_7tap[0];
  310. else
  311. h_filter = &filter_coefs.h_filter_coef_4tap[0];
  312. /* prepare vertical filter coefficients */
  313. if (ratio->vert > MID_RESIZE_VALUE)
  314. v_filter = &filter_coefs.v_filter_coef_7tap[0];
  315. else
  316. v_filter = &filter_coefs.v_filter_coef_4tap[0];
  317. resizer_set_filters(res, h_filter, v_filter);
  318. }
  319. /*
  320. * resizer_set_dst_size - Setup the output height and width
  321. * @res: Device context.
  322. * @width: Output width.
  323. * @height: Output height.
  324. *
  325. * Width :
  326. * The value must be EVEN.
  327. *
  328. * Height:
  329. * The number of bytes written to SDRAM must be
  330. * a multiple of 16-bytes if the vertical resizing factor
  331. * is greater than 1x (upsizing)
  332. */
  333. static void resizer_set_output_size(struct isp_res_device *res,
  334. u32 width, u32 height)
  335. {
  336. struct isp_device *isp = to_isp_device(res);
  337. u32 rgval = 0;
  338. dev_dbg(isp->dev, "Output size[w/h]: %dx%d\n", width, height);
  339. rgval = (width << ISPRSZ_OUT_SIZE_HORZ_SHIFT)
  340. & ISPRSZ_OUT_SIZE_HORZ_MASK;
  341. rgval |= (height << ISPRSZ_OUT_SIZE_VERT_SHIFT)
  342. & ISPRSZ_OUT_SIZE_VERT_MASK;
  343. isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_OUT_SIZE);
  344. }
  345. /*
  346. * resizer_set_output_offset - Setup memory offset for the output lines.
  347. * @res: Device context.
  348. * @offset: Memory offset.
  349. *
  350. * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
  351. * boundary; the 5 LSBs are read-only. For optimal use of SDRAM bandwidth,
  352. * the SDRAM line offset must be set on a 256-byte boundary
  353. */
  354. static void resizer_set_output_offset(struct isp_res_device *res, u32 offset)
  355. {
  356. struct isp_device *isp = to_isp_device(res);
  357. isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTOFF);
  358. }
  359. /*
  360. * resizer_set_start - Setup vertical and horizontal start position
  361. * @res: Device context.
  362. * @left: Horizontal start position.
  363. * @top: Vertical start position.
  364. *
  365. * Vertical start line:
  366. * This field makes sense only when the resizer obtains its input
  367. * from the preview engine/CCDC
  368. *
  369. * Horizontal start pixel:
  370. * Pixels are coded on 16 bits for YUV and 8 bits for color separate data.
  371. * When the resizer gets its input from SDRAM, this field must be set
  372. * to <= 15 for YUV 16-bit data and <= 31 for 8-bit color separate data
  373. */
  374. static void resizer_set_start(struct isp_res_device *res, u32 left, u32 top)
  375. {
  376. struct isp_device *isp = to_isp_device(res);
  377. u32 rgval = 0;
  378. rgval = (left << ISPRSZ_IN_START_HORZ_ST_SHIFT)
  379. & ISPRSZ_IN_START_HORZ_ST_MASK;
  380. rgval |= (top << ISPRSZ_IN_START_VERT_ST_SHIFT)
  381. & ISPRSZ_IN_START_VERT_ST_MASK;
  382. isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_START);
  383. }
  384. /*
  385. * resizer_set_input_size - Setup the input size
  386. * @res: Device context.
  387. * @width: The range is 0 to 4095 pixels
  388. * @height: The range is 0 to 4095 lines
  389. */
  390. static void resizer_set_input_size(struct isp_res_device *res,
  391. u32 width, u32 height)
  392. {
  393. struct isp_device *isp = to_isp_device(res);
  394. u32 rgval = 0;
  395. dev_dbg(isp->dev, "Input size[w/h]: %dx%d\n", width, height);
  396. rgval = (width << ISPRSZ_IN_SIZE_HORZ_SHIFT)
  397. & ISPRSZ_IN_SIZE_HORZ_MASK;
  398. rgval |= (height << ISPRSZ_IN_SIZE_VERT_SHIFT)
  399. & ISPRSZ_IN_SIZE_VERT_MASK;
  400. isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_SIZE);
  401. }
  402. /*
  403. * resizer_set_src_offs - Setup the memory offset for the input lines
  404. * @res: Device context.
  405. * @offset: Memory offset.
  406. *
  407. * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
  408. * boundary; the 5 LSBs are read-only. This field must be programmed to be
  409. * 0x0 if the resizer input is from preview engine/CCDC.
  410. */
  411. static void resizer_set_input_offset(struct isp_res_device *res, u32 offset)
  412. {
  413. struct isp_device *isp = to_isp_device(res);
  414. isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INOFF);
  415. }
  416. /*
  417. * resizer_set_intype - Input type select
  418. * @res: Device context.
  419. * @type: Pixel format type.
  420. */
  421. static void resizer_set_intype(struct isp_res_device *res,
  422. enum resizer_colors_type type)
  423. {
  424. struct isp_device *isp = to_isp_device(res);
  425. if (type == RSZ_COLOR8)
  426. isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  427. ISPRSZ_CNT_INPTYP);
  428. else
  429. isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
  430. ISPRSZ_CNT_INPTYP);
  431. }
  432. /*
  433. * __resizer_set_inaddr - Helper function for set input address
  434. * @res : pointer to resizer private data structure
  435. * @addr: input address
  436. * return none
  437. */
  438. static void __resizer_set_inaddr(struct isp_res_device *res, u32 addr)
  439. {
  440. struct isp_device *isp = to_isp_device(res);
  441. isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INADD);
  442. }
  443. /*
  444. * The data rate at the horizontal resizer output must not exceed half the
  445. * functional clock or 100 MP/s, whichever is lower. According to the TRM
  446. * there's no similar requirement for the vertical resizer output. However
  447. * experience showed that vertical upscaling by 4 leads to SBL overflows (with
  448. * data rates at the resizer output exceeding 300 MP/s). Limiting the resizer
  449. * output data rate to the functional clock or 200 MP/s, whichever is lower,
  450. * seems to get rid of SBL overflows.
  451. *
  452. * The maximum data rate at the output of the horizontal resizer can thus be
  453. * computed with
  454. *
  455. * max intermediate rate <= L3 clock * input height / output height
  456. * max intermediate rate <= L3 clock / 2
  457. *
  458. * The maximum data rate at the resizer input is then
  459. *
  460. * max input rate <= max intermediate rate * input width / output width
  461. *
  462. * where the input width and height are the resizer input crop rectangle size.
  463. * The TRM doesn't clearly explain if that's a maximum instant data rate or a
  464. * maximum average data rate.
  465. */
  466. void omap3isp_resizer_max_rate(struct isp_res_device *res,
  467. unsigned int *max_rate)
  468. {
  469. struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
  470. const struct v4l2_mbus_framefmt *ofmt = &res->formats[RESZ_PAD_SOURCE];
  471. unsigned long limit = min(pipe->l3_ick, 200000000UL);
  472. unsigned long clock;
  473. clock = div_u64((u64)limit * res->crop.active.height, ofmt->height);
  474. clock = min(clock, limit / 2);
  475. *max_rate = div_u64((u64)clock * res->crop.active.width, ofmt->width);
  476. }
  477. /*
  478. * When the resizer processes images from memory, the driver must slow down read
  479. * requests on the input to at least comply with the internal data rate
  480. * requirements. If the application real-time requirements can cope with slower
  481. * processing, the resizer can be slowed down even more to put less pressure on
  482. * the overall system.
  483. *
  484. * When the resizer processes images on the fly (either from the CCDC or the
  485. * preview module), the same data rate requirements apply but they can't be
  486. * enforced at the resizer level. The image input module (sensor, CCP2 or
  487. * preview module) must not provide image data faster than the resizer can
  488. * process.
  489. *
  490. * For live image pipelines, the data rate is set by the frame format, size and
  491. * rate. The sensor output frame rate must not exceed the maximum resizer data
  492. * rate.
  493. *
  494. * The resizer slows down read requests by inserting wait cycles in the SBL
  495. * requests. The maximum number of 256-byte requests per second can be computed
  496. * as (the data rate is multiplied by 2 to convert from pixels per second to
  497. * bytes per second)
  498. *
  499. * request per second = data rate * 2 / 256
  500. * cycles per request = cycles per second / requests per second
  501. *
  502. * The number of cycles per second is controlled by the L3 clock, leading to
  503. *
  504. * cycles per request = L3 frequency / 2 * 256 / data rate
  505. */
  506. static void resizer_adjust_bandwidth(struct isp_res_device *res)
  507. {
  508. struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
  509. struct isp_device *isp = to_isp_device(res);
  510. unsigned long l3_ick = pipe->l3_ick;
  511. struct v4l2_fract *timeperframe;
  512. unsigned int cycles_per_frame;
  513. unsigned int requests_per_frame;
  514. unsigned int cycles_per_request;
  515. unsigned int granularity;
  516. unsigned int minimum;
  517. unsigned int maximum;
  518. unsigned int value;
  519. if (res->input != RESIZER_INPUT_MEMORY) {
  520. isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
  521. ISPSBL_SDR_REQ_RSZ_EXP_MASK);
  522. return;
  523. }
  524. switch (isp->revision) {
  525. case ISP_REVISION_1_0:
  526. case ISP_REVISION_2_0:
  527. default:
  528. granularity = 1024;
  529. break;
  530. case ISP_REVISION_15_0:
  531. granularity = 32;
  532. break;
  533. }
  534. /* Compute the minimum number of cycles per request, based on the
  535. * pipeline maximum data rate. This is an absolute lower bound if we
  536. * don't want SBL overflows, so round the value up.
  537. */
  538. cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
  539. pipe->max_rate);
  540. minimum = DIV_ROUND_UP(cycles_per_request, granularity);
  541. /* Compute the maximum number of cycles per request, based on the
  542. * requested frame rate. This is a soft upper bound to achieve a frame
  543. * rate equal or higher than the requested value, so round the value
  544. * down.
  545. */
  546. timeperframe = &pipe->max_timeperframe;
  547. requests_per_frame = DIV_ROUND_UP(res->crop.active.width * 2, 256)
  548. * res->crop.active.height;
  549. cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
  550. timeperframe->denominator);
  551. cycles_per_request = cycles_per_frame / requests_per_frame;
  552. maximum = cycles_per_request / granularity;
  553. value = max(minimum, maximum);
  554. dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
  555. isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
  556. ISPSBL_SDR_REQ_RSZ_EXP_MASK,
  557. value << ISPSBL_SDR_REQ_RSZ_EXP_SHIFT);
  558. }
  559. /*
  560. * omap3isp_resizer_busy - Checks if ISP resizer is busy.
  561. *
  562. * Returns busy field from ISPRSZ_PCR register.
  563. */
  564. int omap3isp_resizer_busy(struct isp_res_device *res)
  565. {
  566. struct isp_device *isp = to_isp_device(res);
  567. return isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR) &
  568. ISPRSZ_PCR_BUSY;
  569. }
  570. /*
  571. * resizer_set_inaddr - Sets the memory address of the input frame.
  572. * @addr: 32bit memory address aligned on 32byte boundary.
  573. */
  574. static void resizer_set_inaddr(struct isp_res_device *res, u32 addr)
  575. {
  576. res->addr_base = addr;
  577. /* This will handle crop settings in stream off state */
  578. if (res->crop_offset)
  579. addr += res->crop_offset & ~0x1f;
  580. __resizer_set_inaddr(res, addr);
  581. }
  582. /*
  583. * Configures the memory address to which the output frame is written.
  584. * @addr: 32bit memory address aligned on 32byte boundary.
  585. * Note: For SBL efficiency reasons the address should be on a 256-byte
  586. * boundary.
  587. */
  588. static void resizer_set_outaddr(struct isp_res_device *res, u32 addr)
  589. {
  590. struct isp_device *isp = to_isp_device(res);
  591. /*
  592. * Set output address. This needs to be in its own function
  593. * because it changes often.
  594. */
  595. isp_reg_writel(isp, addr << ISPRSZ_SDR_OUTADD_ADDR_SHIFT,
  596. OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTADD);
  597. }
  598. /*
  599. * resizer_print_status - Prints the values of the resizer module registers.
  600. */
  601. #define RSZ_PRINT_REGISTER(isp, name)\
  602. dev_dbg(isp->dev, "###RSZ " #name "=0x%08x\n", \
  603. isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_##name))
  604. static void resizer_print_status(struct isp_res_device *res)
  605. {
  606. struct isp_device *isp = to_isp_device(res);
  607. dev_dbg(isp->dev, "-------------Resizer Register dump----------\n");
  608. RSZ_PRINT_REGISTER(isp, PCR);
  609. RSZ_PRINT_REGISTER(isp, CNT);
  610. RSZ_PRINT_REGISTER(isp, OUT_SIZE);
  611. RSZ_PRINT_REGISTER(isp, IN_START);
  612. RSZ_PRINT_REGISTER(isp, IN_SIZE);
  613. RSZ_PRINT_REGISTER(isp, SDR_INADD);
  614. RSZ_PRINT_REGISTER(isp, SDR_INOFF);
  615. RSZ_PRINT_REGISTER(isp, SDR_OUTADD);
  616. RSZ_PRINT_REGISTER(isp, SDR_OUTOFF);
  617. RSZ_PRINT_REGISTER(isp, YENH);
  618. dev_dbg(isp->dev, "--------------------------------------------\n");
  619. }
  620. /*
  621. * resizer_calc_ratios - Helper function for calculate resizer ratios
  622. * @res: pointer to resizer private data structure
  623. * @input: input frame size
  624. * @output: output frame size
  625. * @ratio : return calculated ratios
  626. * return none
  627. *
  628. * The resizer uses a polyphase sample rate converter. The upsampling filter
  629. * has a fixed number of phases that depend on the resizing ratio. As the ratio
  630. * computation depends on the number of phases, we need to compute a first
  631. * approximation and then refine it.
  632. *
  633. * The input/output/ratio relationship is given by the OMAP34xx TRM:
  634. *
  635. * - 8-phase, 4-tap mode (RSZ = 64 ~ 512)
  636. * iw = (32 * sph + (ow - 1) * hrsz + 16) >> 8 + 7
  637. * ih = (32 * spv + (oh - 1) * vrsz + 16) >> 8 + 4
  638. * - 4-phase, 7-tap mode (RSZ = 513 ~ 1024)
  639. * iw = (64 * sph + (ow - 1) * hrsz + 32) >> 8 + 7
  640. * ih = (64 * spv + (oh - 1) * vrsz + 32) >> 8 + 7
  641. *
  642. * iw and ih are the input width and height after cropping. Those equations need
  643. * to be satisfied exactly for the resizer to work correctly.
  644. *
  645. * The equations can't be easily reverted, as the >> 8 operation is not linear.
  646. * In addition, not all input sizes can be achieved for a given output size. To
  647. * get the highest input size lower than or equal to the requested input size,
  648. * we need to compute the highest resizing ratio that satisfies the following
  649. * inequality (taking the 4-tap mode width equation as an example)
  650. *
  651. * iw >= (32 * sph + (ow - 1) * hrsz + 16) >> 8 - 7
  652. *
  653. * (where iw is the requested input width) which can be rewritten as
  654. *
  655. * iw - 7 >= (32 * sph + (ow - 1) * hrsz + 16) >> 8
  656. * (iw - 7) << 8 >= 32 * sph + (ow - 1) * hrsz + 16 - b
  657. * ((iw - 7) << 8) + b >= 32 * sph + (ow - 1) * hrsz + 16
  658. *
  659. * where b is the value of the 8 least significant bits of the right hand side
  660. * expression of the last inequality. The highest resizing ratio value will be
  661. * achieved when b is equal to its maximum value of 255. That resizing ratio
  662. * value will still satisfy the original inequality, as b will disappear when
  663. * the expression will be shifted right by 8.
  664. *
  665. * The reverted the equations thus become
  666. *
  667. * - 8-phase, 4-tap mode
  668. * hrsz = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / (ow - 1)
  669. * vrsz = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / (oh - 1)
  670. * - 4-phase, 7-tap mode
  671. * hrsz = ((iw - 7) * 256 + 255 - 32 - 64 * sph) / (ow - 1)
  672. * vrsz = ((ih - 7) * 256 + 255 - 32 - 64 * spv) / (oh - 1)
  673. *
  674. * The ratios are integer values, and are rounded down to ensure that the
  675. * cropped input size is not bigger than the uncropped input size.
  676. *
  677. * As the number of phases/taps, used to select the correct equations to compute
  678. * the ratio, depends on the ratio, we start with the 4-tap mode equations to
  679. * compute an approximation of the ratio, and switch to the 7-tap mode equations
  680. * if the approximation is higher than the ratio threshold.
  681. *
  682. * As the 7-tap mode equations will return a ratio smaller than or equal to the
  683. * 4-tap mode equations, the resulting ratio could become lower than or equal to
  684. * the ratio threshold. This 'equations loop' isn't an issue as long as the
  685. * correct equations are used to compute the final input size. Starting with the
  686. * 4-tap mode equations ensure that, in case of values resulting in a 'ratio
  687. * loop', the smallest of the ratio values will be used, never exceeding the
  688. * requested input size.
  689. *
  690. * We first clamp the output size according to the hardware capabilitie to avoid
  691. * auto-cropping the input more than required to satisfy the TRM equations. The
  692. * minimum output size is achieved with a scaling factor of 1024. It is thus
  693. * computed using the 7-tap equations.
  694. *
  695. * min ow = ((iw - 7) * 256 - 32 - 64 * sph) / 1024 + 1
  696. * min oh = ((ih - 7) * 256 - 32 - 64 * spv) / 1024 + 1
  697. *
  698. * Similarly, the maximum output size is achieved with a scaling factor of 64
  699. * and computed using the 4-tap equations.
  700. *
  701. * max ow = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / 64 + 1
  702. * max oh = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1
  703. *
  704. * The additional +255 term compensates for the round down operation performed
  705. * by the TRM equations when shifting the value right by 8 bits.
  706. *
  707. * We then compute and clamp the ratios (x1/4 ~ x4). Clamping the output size to
  708. * the maximum value guarantees that the ratio value will never be smaller than
  709. * the minimum, but it could still slightly exceed the maximum. Clamping the
  710. * ratio will thus result in a resizing factor slightly larger than the
  711. * requested value.
  712. *
  713. * To accommodate that, and make sure the TRM equations are satisfied exactly, we
  714. * compute the input crop rectangle as the last step.
  715. *
  716. * As if the situation wasn't complex enough, the maximum output width depends
  717. * on the vertical resizing ratio. Fortunately, the output height doesn't
  718. * depend on the horizontal resizing ratio. We can then start by computing the
  719. * output height and the vertical ratio, and then move to computing the output
  720. * width and the horizontal ratio.
  721. */
  722. static void resizer_calc_ratios(struct isp_res_device *res,
  723. struct v4l2_rect *input,
  724. struct v4l2_mbus_framefmt *output,
  725. struct resizer_ratio *ratio)
  726. {
  727. struct isp_device *isp = to_isp_device(res);
  728. const unsigned int spv = DEFAULT_PHASE;
  729. const unsigned int sph = DEFAULT_PHASE;
  730. unsigned int upscaled_width;
  731. unsigned int upscaled_height;
  732. unsigned int min_width;
  733. unsigned int min_height;
  734. unsigned int max_width;
  735. unsigned int max_height;
  736. unsigned int width_alignment;
  737. unsigned int width;
  738. unsigned int height;
  739. /*
  740. * Clamp the output height based on the hardware capabilities and
  741. * compute the vertical resizing ratio.
  742. */
  743. min_height = ((input->height - 7) * 256 - 32 - 64 * spv) / 1024 + 1;
  744. min_height = max_t(unsigned int, min_height, MIN_OUT_HEIGHT);
  745. max_height = ((input->height - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1;
  746. max_height = min_t(unsigned int, max_height, MAX_OUT_HEIGHT);
  747. output->height = clamp(output->height, min_height, max_height);
  748. ratio->vert = ((input->height - 4) * 256 + 255 - 16 - 32 * spv)
  749. / (output->height - 1);
  750. if (ratio->vert > MID_RESIZE_VALUE)
  751. ratio->vert = ((input->height - 7) * 256 + 255 - 32 - 64 * spv)
  752. / (output->height - 1);
  753. ratio->vert = clamp_t(unsigned int, ratio->vert,
  754. MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
  755. if (ratio->vert <= MID_RESIZE_VALUE) {
  756. upscaled_height = (output->height - 1) * ratio->vert
  757. + 32 * spv + 16;
  758. height = (upscaled_height >> 8) + 4;
  759. } else {
  760. upscaled_height = (output->height - 1) * ratio->vert
  761. + 64 * spv + 32;
  762. height = (upscaled_height >> 8) + 7;
  763. }
  764. /*
  765. * Compute the minimum and maximum output widths based on the hardware
  766. * capabilities. The maximum depends on the vertical resizing ratio.
  767. */
  768. min_width = ((input->width - 7) * 256 - 32 - 64 * sph) / 1024 + 1;
  769. min_width = max_t(unsigned int, min_width, MIN_OUT_WIDTH);
  770. if (ratio->vert <= MID_RESIZE_VALUE) {
  771. switch (isp->revision) {
  772. case ISP_REVISION_1_0:
  773. max_width = MAX_4TAP_OUT_WIDTH_ES1;
  774. break;
  775. case ISP_REVISION_2_0:
  776. default:
  777. max_width = MAX_4TAP_OUT_WIDTH_ES2;
  778. break;
  779. case ISP_REVISION_15_0:
  780. max_width = MAX_4TAP_OUT_WIDTH_3630;
  781. break;
  782. }
  783. } else {
  784. switch (isp->revision) {
  785. case ISP_REVISION_1_0:
  786. max_width = MAX_7TAP_OUT_WIDTH_ES1;
  787. break;
  788. case ISP_REVISION_2_0:
  789. default:
  790. max_width = MAX_7TAP_OUT_WIDTH_ES2;
  791. break;
  792. case ISP_REVISION_15_0:
  793. max_width = MAX_7TAP_OUT_WIDTH_3630;
  794. break;
  795. }
  796. }
  797. max_width = min(((input->width - 7) * 256 + 255 - 16 - 32 * sph) / 64
  798. + 1, max_width);
  799. /*
  800. * The output width must be even, and must be a multiple of 16 bytes
  801. * when upscaling vertically. Clamp the output width to the valid range.
  802. * Take the alignment into account (the maximum width in 7-tap mode on
  803. * ES2 isn't a multiple of 8) and align the result up to make sure it
  804. * won't be smaller than the minimum.
  805. */
  806. width_alignment = ratio->vert < 256 ? 8 : 2;
  807. output->width = clamp(output->width, min_width,
  808. max_width & ~(width_alignment - 1));
  809. output->width = ALIGN(output->width, width_alignment);
  810. ratio->horz = ((input->width - 7) * 256 + 255 - 16 - 32 * sph)
  811. / (output->width - 1);
  812. if (ratio->horz > MID_RESIZE_VALUE)
  813. ratio->horz = ((input->width - 7) * 256 + 255 - 32 - 64 * sph)
  814. / (output->width - 1);
  815. ratio->horz = clamp_t(unsigned int, ratio->horz,
  816. MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
  817. if (ratio->horz <= MID_RESIZE_VALUE) {
  818. upscaled_width = (output->width - 1) * ratio->horz
  819. + 32 * sph + 16;
  820. width = (upscaled_width >> 8) + 7;
  821. } else {
  822. upscaled_width = (output->width - 1) * ratio->horz
  823. + 64 * sph + 32;
  824. width = (upscaled_width >> 8) + 7;
  825. }
  826. /* Center the new crop rectangle. */
  827. input->left += (input->width - width) / 2;
  828. input->top += (input->height - height) / 2;
  829. input->width = width;
  830. input->height = height;
  831. }
  832. /*
  833. * resizer_set_crop_params - Setup hardware with cropping parameters
  834. * @res : resizer private structure
  835. * @crop_rect : current crop rectangle
  836. * @ratio : resizer ratios
  837. * return none
  838. */
  839. static void resizer_set_crop_params(struct isp_res_device *res,
  840. const struct v4l2_mbus_framefmt *input,
  841. const struct v4l2_mbus_framefmt *output)
  842. {
  843. resizer_set_ratio(res, &res->ratio);
  844. /* Set chrominance horizontal algorithm */
  845. if (res->ratio.horz >= RESIZE_DIVISOR)
  846. resizer_set_bilinear(res, RSZ_THE_SAME);
  847. else
  848. resizer_set_bilinear(res, RSZ_BILINEAR);
  849. resizer_adjust_bandwidth(res);
  850. if (res->input == RESIZER_INPUT_MEMORY) {
  851. /* Calculate additional offset for crop */
  852. res->crop_offset = (res->crop.active.top * input->width +
  853. res->crop.active.left) * 2;
  854. /*
  855. * Write lowest 4 bits of horizontal pixel offset (in pixels),
  856. * vertical start must be 0.
  857. */
  858. resizer_set_start(res, (res->crop_offset / 2) & 0xf, 0);
  859. /*
  860. * Set start (read) address for cropping, in bytes.
  861. * Lowest 5 bits must be zero.
  862. */
  863. __resizer_set_inaddr(res,
  864. res->addr_base + (res->crop_offset & ~0x1f));
  865. } else {
  866. /*
  867. * Set vertical start line and horizontal starting pixel.
  868. * If the input is from CCDC/PREV, horizontal start field is
  869. * in bytes (twice number of pixels).
  870. */
  871. resizer_set_start(res, res->crop.active.left * 2,
  872. res->crop.active.top);
  873. /* Input address and offset must be 0 for preview/ccdc input */
  874. __resizer_set_inaddr(res, 0);
  875. resizer_set_input_offset(res, 0);
  876. }
  877. /* Set the input size */
  878. resizer_set_input_size(res, res->crop.active.width,
  879. res->crop.active.height);
  880. }
  881. static void resizer_configure(struct isp_res_device *res)
  882. {
  883. struct v4l2_mbus_framefmt *informat, *outformat;
  884. struct resizer_luma_yenh luma = {0, 0, 0, 0};
  885. resizer_set_source(res, res->input);
  886. informat = &res->formats[RESZ_PAD_SINK];
  887. outformat = &res->formats[RESZ_PAD_SOURCE];
  888. /* RESZ_PAD_SINK */
  889. if (res->input == RESIZER_INPUT_VP)
  890. resizer_set_input_offset(res, 0);
  891. else
  892. resizer_set_input_offset(res, ALIGN(informat->width, 0x10) * 2);
  893. /* YUV422 interleaved, default phase, no luma enhancement */
  894. resizer_set_intype(res, RSZ_YUV422);
  895. resizer_set_ycpos(res, informat->code);
  896. resizer_set_phase(res, DEFAULT_PHASE, DEFAULT_PHASE);
  897. resizer_set_luma(res, &luma);
  898. /* RESZ_PAD_SOURCE */
  899. resizer_set_output_offset(res, ALIGN(outformat->width * 2, 32));
  900. resizer_set_output_size(res, outformat->width, outformat->height);
  901. resizer_set_crop_params(res, informat, outformat);
  902. }
  903. /* -----------------------------------------------------------------------------
  904. * Interrupt handling
  905. */
  906. static void resizer_enable_oneshot(struct isp_res_device *res)
  907. {
  908. struct isp_device *isp = to_isp_device(res);
  909. isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR,
  910. ISPRSZ_PCR_ENABLE | ISPRSZ_PCR_ONESHOT);
  911. }
  912. void omap3isp_resizer_isr_frame_sync(struct isp_res_device *res)
  913. {
  914. /*
  915. * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
  916. * condition, the module was paused and now we have a buffer queued
  917. * on the output again. Restart the pipeline if running in continuous
  918. * mode.
  919. */
  920. if (res->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
  921. res->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
  922. resizer_enable_oneshot(res);
  923. isp_video_dmaqueue_flags_clr(&res->video_out);
  924. }
  925. }
  926. static void resizer_isr_buffer(struct isp_res_device *res)
  927. {
  928. struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
  929. struct isp_buffer *buffer;
  930. int restart = 0;
  931. if (res->state == ISP_PIPELINE_STREAM_STOPPED)
  932. return;
  933. /* Complete the output buffer and, if reading from memory, the input
  934. * buffer.
  935. */
  936. buffer = omap3isp_video_buffer_next(&res->video_out);
  937. if (buffer != NULL) {
  938. resizer_set_outaddr(res, buffer->isp_addr);
  939. restart = 1;
  940. }
  941. pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
  942. if (res->input == RESIZER_INPUT_MEMORY) {
  943. buffer = omap3isp_video_buffer_next(&res->video_in);
  944. if (buffer != NULL)
  945. resizer_set_inaddr(res, buffer->isp_addr);
  946. pipe->state |= ISP_PIPELINE_IDLE_INPUT;
  947. }
  948. if (res->state == ISP_PIPELINE_STREAM_SINGLESHOT) {
  949. if (isp_pipeline_ready(pipe))
  950. omap3isp_pipeline_set_stream(pipe,
  951. ISP_PIPELINE_STREAM_SINGLESHOT);
  952. } else {
  953. /* If an underrun occurs, the video queue operation handler will
  954. * restart the resizer. Otherwise restart it immediately.
  955. */
  956. if (restart)
  957. resizer_enable_oneshot(res);
  958. }
  959. }
  960. /*
  961. * omap3isp_resizer_isr - ISP resizer interrupt handler
  962. *
  963. * Manage the resizer video buffers and configure shadowed and busy-locked
  964. * registers.
  965. */
  966. void omap3isp_resizer_isr(struct isp_res_device *res)
  967. {
  968. struct v4l2_mbus_framefmt *informat, *outformat;
  969. if (omap3isp_module_sync_is_stopping(&res->wait, &res->stopping))
  970. return;
  971. if (res->applycrop) {
  972. outformat = __resizer_get_format(res, NULL, RESZ_PAD_SOURCE,
  973. V4L2_SUBDEV_FORMAT_ACTIVE);
  974. informat = __resizer_get_format(res, NULL, RESZ_PAD_SINK,
  975. V4L2_SUBDEV_FORMAT_ACTIVE);
  976. resizer_set_crop_params(res, informat, outformat);
  977. res->applycrop = 0;
  978. }
  979. resizer_isr_buffer(res);
  980. }
  981. /* -----------------------------------------------------------------------------
  982. * ISP video operations
  983. */
  984. static int resizer_video_queue(struct isp_video *video,
  985. struct isp_buffer *buffer)
  986. {
  987. struct isp_res_device *res = &video->isp->isp_res;
  988. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  989. resizer_set_inaddr(res, buffer->isp_addr);
  990. /*
  991. * We now have a buffer queued on the output. Despite what the
  992. * TRM says, the resizer can't be restarted immediately.
  993. * Enabling it in one shot mode in the middle of a frame (or at
  994. * least asynchronously to the frame) results in the output
  995. * being shifted randomly left/right and up/down, as if the
  996. * hardware didn't synchronize itself to the beginning of the
  997. * frame correctly.
  998. *
  999. * Restart the resizer on the next sync interrupt if running in
  1000. * continuous mode or when starting the stream.
  1001. */
  1002. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1003. resizer_set_outaddr(res, buffer->isp_addr);
  1004. return 0;
  1005. }
  1006. static const struct isp_video_operations resizer_video_ops = {
  1007. .queue = resizer_video_queue,
  1008. };
  1009. /* -----------------------------------------------------------------------------
  1010. * V4L2 subdev operations
  1011. */
  1012. /*
  1013. * resizer_set_stream - Enable/Disable streaming on resizer subdev
  1014. * @sd: ISP resizer V4L2 subdev
  1015. * @enable: 1 == Enable, 0 == Disable
  1016. *
  1017. * The resizer hardware can't be enabled without a memory buffer to write to.
  1018. * As the s_stream operation is called in response to a STREAMON call without
  1019. * any buffer queued yet, just update the state field and return immediately.
  1020. * The resizer will be enabled in resizer_video_queue().
  1021. */
  1022. static int resizer_set_stream(struct v4l2_subdev *sd, int enable)
  1023. {
  1024. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1025. struct isp_video *video_out = &res->video_out;
  1026. struct isp_device *isp = to_isp_device(res);
  1027. struct device *dev = to_device(res);
  1028. if (res->state == ISP_PIPELINE_STREAM_STOPPED) {
  1029. if (enable == ISP_PIPELINE_STREAM_STOPPED)
  1030. return 0;
  1031. omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_RESIZER);
  1032. resizer_configure(res);
  1033. resizer_print_status(res);
  1034. }
  1035. switch (enable) {
  1036. case ISP_PIPELINE_STREAM_CONTINUOUS:
  1037. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
  1038. if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
  1039. resizer_enable_oneshot(res);
  1040. isp_video_dmaqueue_flags_clr(video_out);
  1041. }
  1042. break;
  1043. case ISP_PIPELINE_STREAM_SINGLESHOT:
  1044. if (res->input == RESIZER_INPUT_MEMORY)
  1045. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_READ);
  1046. omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
  1047. resizer_enable_oneshot(res);
  1048. break;
  1049. case ISP_PIPELINE_STREAM_STOPPED:
  1050. if (omap3isp_module_sync_idle(&sd->entity, &res->wait,
  1051. &res->stopping))
  1052. dev_dbg(dev, "%s: module stop timeout.\n", sd->name);
  1053. omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_RESIZER_READ |
  1054. OMAP3_ISP_SBL_RESIZER_WRITE);
  1055. omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_RESIZER);
  1056. isp_video_dmaqueue_flags_clr(video_out);
  1057. break;
  1058. }
  1059. res->state = enable;
  1060. return 0;
  1061. }
  1062. /*
  1063. * resizer_g_crop - handle get crop subdev operation
  1064. * @sd : pointer to v4l2 subdev structure
  1065. * @pad : subdev pad
  1066. * @crop : pointer to crop structure
  1067. * @which : active or try format
  1068. * return zero
  1069. */
  1070. static int resizer_g_crop(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1071. struct v4l2_subdev_crop *crop)
  1072. {
  1073. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1074. struct v4l2_mbus_framefmt *format;
  1075. struct resizer_ratio ratio;
  1076. /* Only sink pad has crop capability */
  1077. if (crop->pad != RESZ_PAD_SINK)
  1078. return -EINVAL;
  1079. format = __resizer_get_format(res, fh, RESZ_PAD_SOURCE, crop->which);
  1080. crop->rect = *__resizer_get_crop(res, fh, crop->which);
  1081. resizer_calc_ratios(res, &crop->rect, format, &ratio);
  1082. return 0;
  1083. }
  1084. /*
  1085. * resizer_try_crop - mangles crop parameters.
  1086. */
  1087. static void resizer_try_crop(const struct v4l2_mbus_framefmt *sink,
  1088. const struct v4l2_mbus_framefmt *source,
  1089. struct v4l2_rect *crop)
  1090. {
  1091. const unsigned int spv = DEFAULT_PHASE;
  1092. const unsigned int sph = DEFAULT_PHASE;
  1093. /* Crop rectangle is constrained to the output size so that zoom ratio
  1094. * cannot exceed +/-4.0.
  1095. */
  1096. unsigned int min_width =
  1097. ((32 * sph + (source->width - 1) * 64 + 16) >> 8) + 7;
  1098. unsigned int min_height =
  1099. ((32 * spv + (source->height - 1) * 64 + 16) >> 8) + 4;
  1100. unsigned int max_width =
  1101. ((64 * sph + (source->width - 1) * 1024 + 32) >> 8) + 7;
  1102. unsigned int max_height =
  1103. ((64 * spv + (source->height - 1) * 1024 + 32) >> 8) + 7;
  1104. crop->width = clamp_t(u32, crop->width, min_width, max_width);
  1105. crop->height = clamp_t(u32, crop->height, min_height, max_height);
  1106. /* Crop can not go beyond of the input rectangle */
  1107. crop->left = clamp_t(u32, crop->left, 0, sink->width - MIN_IN_WIDTH);
  1108. crop->width = clamp_t(u32, crop->width, MIN_IN_WIDTH,
  1109. sink->width - crop->left);
  1110. crop->top = clamp_t(u32, crop->top, 0, sink->height - MIN_IN_HEIGHT);
  1111. crop->height = clamp_t(u32, crop->height, MIN_IN_HEIGHT,
  1112. sink->height - crop->top);
  1113. }
  1114. /*
  1115. * resizer_s_crop - handle set crop subdev operation
  1116. * @sd : pointer to v4l2 subdev structure
  1117. * @pad : subdev pad
  1118. * @crop : pointer to crop structure
  1119. * @which : active or try format
  1120. * return -EINVAL or zero when succeed
  1121. */
  1122. static int resizer_s_crop(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1123. struct v4l2_subdev_crop *crop)
  1124. {
  1125. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1126. struct isp_device *isp = to_isp_device(res);
  1127. struct v4l2_mbus_framefmt *format_sink, *format_source;
  1128. struct resizer_ratio ratio;
  1129. /* Only sink pad has crop capability */
  1130. if (crop->pad != RESZ_PAD_SINK)
  1131. return -EINVAL;
  1132. format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
  1133. crop->which);
  1134. format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
  1135. crop->which);
  1136. dev_dbg(isp->dev, "%s: L=%d,T=%d,W=%d,H=%d,which=%d\n", __func__,
  1137. crop->rect.left, crop->rect.top, crop->rect.width,
  1138. crop->rect.height, crop->which);
  1139. dev_dbg(isp->dev, "%s: input=%dx%d, output=%dx%d\n", __func__,
  1140. format_sink->width, format_sink->height,
  1141. format_source->width, format_source->height);
  1142. resizer_try_crop(format_sink, format_source, &crop->rect);
  1143. *__resizer_get_crop(res, fh, crop->which) = crop->rect;
  1144. resizer_calc_ratios(res, &crop->rect, format_source, &ratio);
  1145. if (crop->which == V4L2_SUBDEV_FORMAT_TRY)
  1146. return 0;
  1147. res->ratio = ratio;
  1148. res->crop.active = crop->rect;
  1149. /*
  1150. * s_crop can be called while streaming is on. In this case
  1151. * the crop values will be set in the next IRQ.
  1152. */
  1153. if (res->state != ISP_PIPELINE_STREAM_STOPPED)
  1154. res->applycrop = 1;
  1155. return 0;
  1156. }
  1157. /* resizer pixel formats */
  1158. static const unsigned int resizer_formats[] = {
  1159. V4L2_MBUS_FMT_UYVY8_1X16,
  1160. V4L2_MBUS_FMT_YUYV8_1X16,
  1161. };
  1162. static unsigned int resizer_max_in_width(struct isp_res_device *res)
  1163. {
  1164. struct isp_device *isp = to_isp_device(res);
  1165. if (res->input == RESIZER_INPUT_MEMORY) {
  1166. return MAX_IN_WIDTH_MEMORY_MODE;
  1167. } else {
  1168. if (isp->revision == ISP_REVISION_1_0)
  1169. return MAX_IN_WIDTH_ONTHEFLY_MODE_ES1;
  1170. else
  1171. return MAX_IN_WIDTH_ONTHEFLY_MODE_ES2;
  1172. }
  1173. }
  1174. /*
  1175. * resizer_try_format - Handle try format by pad subdev method
  1176. * @res : ISP resizer device
  1177. * @fh : V4L2 subdev file handle
  1178. * @pad : pad num
  1179. * @fmt : pointer to v4l2 format structure
  1180. * @which : wanted subdev format
  1181. */
  1182. static void resizer_try_format(struct isp_res_device *res,
  1183. struct v4l2_subdev_fh *fh, unsigned int pad,
  1184. struct v4l2_mbus_framefmt *fmt,
  1185. enum v4l2_subdev_format_whence which)
  1186. {
  1187. struct v4l2_mbus_framefmt *format;
  1188. struct resizer_ratio ratio;
  1189. struct v4l2_rect crop;
  1190. switch (pad) {
  1191. case RESZ_PAD_SINK:
  1192. if (fmt->code != V4L2_MBUS_FMT_YUYV8_1X16 &&
  1193. fmt->code != V4L2_MBUS_FMT_UYVY8_1X16)
  1194. fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
  1195. fmt->width = clamp_t(u32, fmt->width, MIN_IN_WIDTH,
  1196. resizer_max_in_width(res));
  1197. fmt->height = clamp_t(u32, fmt->height, MIN_IN_HEIGHT,
  1198. MAX_IN_HEIGHT);
  1199. break;
  1200. case RESZ_PAD_SOURCE:
  1201. format = __resizer_get_format(res, fh, RESZ_PAD_SINK, which);
  1202. fmt->code = format->code;
  1203. crop = *__resizer_get_crop(res, fh, which);
  1204. resizer_calc_ratios(res, &crop, fmt, &ratio);
  1205. break;
  1206. }
  1207. fmt->colorspace = V4L2_COLORSPACE_JPEG;
  1208. fmt->field = V4L2_FIELD_NONE;
  1209. }
  1210. /*
  1211. * resizer_enum_mbus_code - Handle pixel format enumeration
  1212. * @sd : pointer to v4l2 subdev structure
  1213. * @fh : V4L2 subdev file handle
  1214. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  1215. * return -EINVAL or zero on success
  1216. */
  1217. static int resizer_enum_mbus_code(struct v4l2_subdev *sd,
  1218. struct v4l2_subdev_fh *fh,
  1219. struct v4l2_subdev_mbus_code_enum *code)
  1220. {
  1221. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1222. struct v4l2_mbus_framefmt *format;
  1223. if (code->pad == RESZ_PAD_SINK) {
  1224. if (code->index >= ARRAY_SIZE(resizer_formats))
  1225. return -EINVAL;
  1226. code->code = resizer_formats[code->index];
  1227. } else {
  1228. if (code->index != 0)
  1229. return -EINVAL;
  1230. format = __resizer_get_format(res, fh, RESZ_PAD_SINK,
  1231. V4L2_SUBDEV_FORMAT_TRY);
  1232. code->code = format->code;
  1233. }
  1234. return 0;
  1235. }
  1236. static int resizer_enum_frame_size(struct v4l2_subdev *sd,
  1237. struct v4l2_subdev_fh *fh,
  1238. struct v4l2_subdev_frame_size_enum *fse)
  1239. {
  1240. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1241. struct v4l2_mbus_framefmt format;
  1242. if (fse->index != 0)
  1243. return -EINVAL;
  1244. format.code = fse->code;
  1245. format.width = 1;
  1246. format.height = 1;
  1247. resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1248. fse->min_width = format.width;
  1249. fse->min_height = format.height;
  1250. if (format.code != fse->code)
  1251. return -EINVAL;
  1252. format.code = fse->code;
  1253. format.width = -1;
  1254. format.height = -1;
  1255. resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1256. fse->max_width = format.width;
  1257. fse->max_height = format.height;
  1258. return 0;
  1259. }
  1260. /*
  1261. * resizer_get_format - Handle get format by pads subdev method
  1262. * @sd : pointer to v4l2 subdev structure
  1263. * @fh : V4L2 subdev file handle
  1264. * @fmt : pointer to v4l2 subdev format structure
  1265. * return -EINVAL or zero on success
  1266. */
  1267. static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1268. struct v4l2_subdev_format *fmt)
  1269. {
  1270. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1271. struct v4l2_mbus_framefmt *format;
  1272. format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
  1273. if (format == NULL)
  1274. return -EINVAL;
  1275. fmt->format = *format;
  1276. return 0;
  1277. }
  1278. /*
  1279. * resizer_set_format - Handle set format by pads subdev method
  1280. * @sd : pointer to v4l2 subdev structure
  1281. * @fh : V4L2 subdev file handle
  1282. * @fmt : pointer to v4l2 subdev format structure
  1283. * return -EINVAL or zero on success
  1284. */
  1285. static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1286. struct v4l2_subdev_format *fmt)
  1287. {
  1288. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1289. struct v4l2_mbus_framefmt *format;
  1290. struct v4l2_rect *crop;
  1291. format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
  1292. if (format == NULL)
  1293. return -EINVAL;
  1294. resizer_try_format(res, fh, fmt->pad, &fmt->format, fmt->which);
  1295. *format = fmt->format;
  1296. if (fmt->pad == RESZ_PAD_SINK) {
  1297. /* reset crop rectangle */
  1298. crop = __resizer_get_crop(res, fh, fmt->which);
  1299. crop->left = 0;
  1300. crop->top = 0;
  1301. crop->width = fmt->format.width;
  1302. crop->height = fmt->format.height;
  1303. /* Propagate the format from sink to source */
  1304. format = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
  1305. fmt->which);
  1306. *format = fmt->format;
  1307. resizer_try_format(res, fh, RESZ_PAD_SOURCE, format,
  1308. fmt->which);
  1309. }
  1310. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1311. /* Compute and store the active crop rectangle and resizer
  1312. * ratios. format already points to the source pad active
  1313. * format.
  1314. */
  1315. res->crop.active = res->crop.request;
  1316. resizer_calc_ratios(res, &res->crop.active, format,
  1317. &res->ratio);
  1318. }
  1319. return 0;
  1320. }
  1321. /*
  1322. * resizer_init_formats - Initialize formats on all pads
  1323. * @sd: ISP resizer V4L2 subdevice
  1324. * @fh: V4L2 subdev file handle
  1325. *
  1326. * Initialize all pad formats with default values. If fh is not NULL, try
  1327. * formats are initialized on the file handle. Otherwise active formats are
  1328. * initialized on the device.
  1329. */
  1330. static int resizer_init_formats(struct v4l2_subdev *sd,
  1331. struct v4l2_subdev_fh *fh)
  1332. {
  1333. struct v4l2_subdev_format format;
  1334. memset(&format, 0, sizeof(format));
  1335. format.pad = RESZ_PAD_SINK;
  1336. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  1337. format.format.code = V4L2_MBUS_FMT_YUYV8_1X16;
  1338. format.format.width = 4096;
  1339. format.format.height = 4096;
  1340. resizer_set_format(sd, fh, &format);
  1341. return 0;
  1342. }
  1343. /* subdev video operations */
  1344. static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
  1345. .s_stream = resizer_set_stream,
  1346. };
  1347. /* subdev pad operations */
  1348. static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = {
  1349. .enum_mbus_code = resizer_enum_mbus_code,
  1350. .enum_frame_size = resizer_enum_frame_size,
  1351. .get_fmt = resizer_get_format,
  1352. .set_fmt = resizer_set_format,
  1353. .get_crop = resizer_g_crop,
  1354. .set_crop = resizer_s_crop,
  1355. };
  1356. /* subdev operations */
  1357. static const struct v4l2_subdev_ops resizer_v4l2_ops = {
  1358. .video = &resizer_v4l2_video_ops,
  1359. .pad = &resizer_v4l2_pad_ops,
  1360. };
  1361. /* subdev internal operations */
  1362. static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
  1363. .open = resizer_init_formats,
  1364. };
  1365. /* -----------------------------------------------------------------------------
  1366. * Media entity operations
  1367. */
  1368. /*
  1369. * resizer_link_setup - Setup resizer connections.
  1370. * @entity : Pointer to media entity structure
  1371. * @local : Pointer to local pad array
  1372. * @remote : Pointer to remote pad array
  1373. * @flags : Link flags
  1374. * return -EINVAL or zero on success
  1375. */
  1376. static int resizer_link_setup(struct media_entity *entity,
  1377. const struct media_pad *local,
  1378. const struct media_pad *remote, u32 flags)
  1379. {
  1380. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1381. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1382. switch (local->index | media_entity_type(remote->entity)) {
  1383. case RESZ_PAD_SINK | MEDIA_ENT_T_DEVNODE:
  1384. /* read from memory */
  1385. if (flags & MEDIA_LNK_FL_ENABLED) {
  1386. if (res->input == RESIZER_INPUT_VP)
  1387. return -EBUSY;
  1388. res->input = RESIZER_INPUT_MEMORY;
  1389. } else {
  1390. if (res->input == RESIZER_INPUT_MEMORY)
  1391. res->input = RESIZER_INPUT_NONE;
  1392. }
  1393. break;
  1394. case RESZ_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
  1395. /* read from ccdc or previewer */
  1396. if (flags & MEDIA_LNK_FL_ENABLED) {
  1397. if (res->input == RESIZER_INPUT_MEMORY)
  1398. return -EBUSY;
  1399. res->input = RESIZER_INPUT_VP;
  1400. } else {
  1401. if (res->input == RESIZER_INPUT_VP)
  1402. res->input = RESIZER_INPUT_NONE;
  1403. }
  1404. break;
  1405. case RESZ_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
  1406. /* resizer always write to memory */
  1407. break;
  1408. default:
  1409. return -EINVAL;
  1410. }
  1411. return 0;
  1412. }
  1413. /* media operations */
  1414. static const struct media_entity_operations resizer_media_ops = {
  1415. .link_setup = resizer_link_setup,
  1416. };
  1417. void omap3isp_resizer_unregister_entities(struct isp_res_device *res)
  1418. {
  1419. v4l2_device_unregister_subdev(&res->subdev);
  1420. omap3isp_video_unregister(&res->video_in);
  1421. omap3isp_video_unregister(&res->video_out);
  1422. }
  1423. int omap3isp_resizer_register_entities(struct isp_res_device *res,
  1424. struct v4l2_device *vdev)
  1425. {
  1426. int ret;
  1427. /* Register the subdev and video nodes. */
  1428. ret = v4l2_device_register_subdev(vdev, &res->subdev);
  1429. if (ret < 0)
  1430. goto error;
  1431. ret = omap3isp_video_register(&res->video_in, vdev);
  1432. if (ret < 0)
  1433. goto error;
  1434. ret = omap3isp_video_register(&res->video_out, vdev);
  1435. if (ret < 0)
  1436. goto error;
  1437. return 0;
  1438. error:
  1439. omap3isp_resizer_unregister_entities(res);
  1440. return ret;
  1441. }
  1442. /* -----------------------------------------------------------------------------
  1443. * ISP resizer initialization and cleanup
  1444. */
  1445. /*
  1446. * resizer_init_entities - Initialize resizer subdev and media entity.
  1447. * @res : Pointer to resizer device structure
  1448. * return -ENOMEM or zero on success
  1449. */
  1450. static int resizer_init_entities(struct isp_res_device *res)
  1451. {
  1452. struct v4l2_subdev *sd = &res->subdev;
  1453. struct media_pad *pads = res->pads;
  1454. struct media_entity *me = &sd->entity;
  1455. int ret;
  1456. res->input = RESIZER_INPUT_NONE;
  1457. v4l2_subdev_init(sd, &resizer_v4l2_ops);
  1458. sd->internal_ops = &resizer_v4l2_internal_ops;
  1459. strlcpy(sd->name, "OMAP3 ISP resizer", sizeof(sd->name));
  1460. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  1461. v4l2_set_subdevdata(sd, res);
  1462. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1463. pads[RESZ_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1464. pads[RESZ_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1465. me->ops = &resizer_media_ops;
  1466. ret = media_entity_init(me, RESZ_PADS_NUM, pads, 0);
  1467. if (ret < 0)
  1468. return ret;
  1469. resizer_init_formats(sd, NULL);
  1470. res->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1471. res->video_in.ops = &resizer_video_ops;
  1472. res->video_in.isp = to_isp_device(res);
  1473. res->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1474. res->video_in.bpl_alignment = 32;
  1475. res->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1476. res->video_out.ops = &resizer_video_ops;
  1477. res->video_out.isp = to_isp_device(res);
  1478. res->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1479. res->video_out.bpl_alignment = 32;
  1480. ret = omap3isp_video_init(&res->video_in, "resizer");
  1481. if (ret < 0)
  1482. goto error_video_in;
  1483. ret = omap3isp_video_init(&res->video_out, "resizer");
  1484. if (ret < 0)
  1485. goto error_video_out;
  1486. /* Connect the video nodes to the resizer subdev. */
  1487. ret = media_entity_create_link(&res->video_in.video.entity, 0,
  1488. &res->subdev.entity, RESZ_PAD_SINK, 0);
  1489. if (ret < 0)
  1490. goto error_link;
  1491. ret = media_entity_create_link(&res->subdev.entity, RESZ_PAD_SOURCE,
  1492. &res->video_out.video.entity, 0, 0);
  1493. if (ret < 0)
  1494. goto error_link;
  1495. return 0;
  1496. error_link:
  1497. omap3isp_video_cleanup(&res->video_out);
  1498. error_video_out:
  1499. omap3isp_video_cleanup(&res->video_in);
  1500. error_video_in:
  1501. media_entity_cleanup(&res->subdev.entity);
  1502. return ret;
  1503. }
  1504. /*
  1505. * isp_resizer_init - Resizer initialization.
  1506. * @isp : Pointer to ISP device
  1507. * return -ENOMEM or zero on success
  1508. */
  1509. int omap3isp_resizer_init(struct isp_device *isp)
  1510. {
  1511. struct isp_res_device *res = &isp->isp_res;
  1512. init_waitqueue_head(&res->wait);
  1513. atomic_set(&res->stopping, 0);
  1514. return resizer_init_entities(res);
  1515. }
  1516. void omap3isp_resizer_cleanup(struct isp_device *isp)
  1517. {
  1518. struct isp_res_device *res = &isp->isp_res;
  1519. omap3isp_video_cleanup(&res->video_in);
  1520. omap3isp_video_cleanup(&res->video_out);
  1521. media_entity_cleanup(&res->subdev.entity);
  1522. }