ispresizer.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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 calculating 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 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 capability 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_try_crop - mangles crop parameters.
  1064. */
  1065. static void resizer_try_crop(const struct v4l2_mbus_framefmt *sink,
  1066. const struct v4l2_mbus_framefmt *source,
  1067. struct v4l2_rect *crop)
  1068. {
  1069. const unsigned int spv = DEFAULT_PHASE;
  1070. const unsigned int sph = DEFAULT_PHASE;
  1071. /* Crop rectangle is constrained by the output size so that zoom ratio
  1072. * cannot exceed +/-4.0.
  1073. */
  1074. unsigned int min_width =
  1075. ((32 * sph + (source->width - 1) * 64 + 16) >> 8) + 7;
  1076. unsigned int min_height =
  1077. ((32 * spv + (source->height - 1) * 64 + 16) >> 8) + 4;
  1078. unsigned int max_width =
  1079. ((64 * sph + (source->width - 1) * 1024 + 32) >> 8) + 7;
  1080. unsigned int max_height =
  1081. ((64 * spv + (source->height - 1) * 1024 + 32) >> 8) + 7;
  1082. crop->width = clamp_t(u32, crop->width, min_width, max_width);
  1083. crop->height = clamp_t(u32, crop->height, min_height, max_height);
  1084. /* Crop can not go beyond of the input rectangle */
  1085. crop->left = clamp_t(u32, crop->left, 0, sink->width - MIN_IN_WIDTH);
  1086. crop->width = clamp_t(u32, crop->width, MIN_IN_WIDTH,
  1087. sink->width - crop->left);
  1088. crop->top = clamp_t(u32, crop->top, 0, sink->height - MIN_IN_HEIGHT);
  1089. crop->height = clamp_t(u32, crop->height, MIN_IN_HEIGHT,
  1090. sink->height - crop->top);
  1091. }
  1092. /*
  1093. * resizer_get_selection - Retrieve a selection rectangle on a pad
  1094. * @sd: ISP resizer V4L2 subdevice
  1095. * @fh: V4L2 subdev file handle
  1096. * @sel: Selection rectangle
  1097. *
  1098. * The only supported rectangles are the crop rectangles on the sink pad.
  1099. *
  1100. * Return 0 on success or a negative error code otherwise.
  1101. */
  1102. static int resizer_get_selection(struct v4l2_subdev *sd,
  1103. struct v4l2_subdev_fh *fh,
  1104. struct v4l2_subdev_selection *sel)
  1105. {
  1106. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1107. struct v4l2_mbus_framefmt *format_source;
  1108. struct v4l2_mbus_framefmt *format_sink;
  1109. struct resizer_ratio ratio;
  1110. if (sel->pad != RESZ_PAD_SINK)
  1111. return -EINVAL;
  1112. format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
  1113. sel->which);
  1114. format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
  1115. sel->which);
  1116. switch (sel->target) {
  1117. case V4L2_SEL_TGT_CROP_BOUNDS:
  1118. sel->r.left = 0;
  1119. sel->r.top = 0;
  1120. sel->r.width = INT_MAX;
  1121. sel->r.height = INT_MAX;
  1122. resizer_try_crop(format_sink, format_source, &sel->r);
  1123. resizer_calc_ratios(res, &sel->r, format_source, &ratio);
  1124. break;
  1125. case V4L2_SEL_TGT_CROP:
  1126. sel->r = *__resizer_get_crop(res, fh, sel->which);
  1127. resizer_calc_ratios(res, &sel->r, format_source, &ratio);
  1128. break;
  1129. default:
  1130. return -EINVAL;
  1131. }
  1132. return 0;
  1133. }
  1134. /*
  1135. * resizer_set_selection - Set a selection rectangle on a pad
  1136. * @sd: ISP resizer V4L2 subdevice
  1137. * @fh: V4L2 subdev file handle
  1138. * @sel: Selection rectangle
  1139. *
  1140. * The only supported rectangle is the actual crop rectangle on the sink pad.
  1141. *
  1142. * FIXME: This function currently behaves as if the KEEP_CONFIG selection flag
  1143. * was always set.
  1144. *
  1145. * Return 0 on success or a negative error code otherwise.
  1146. */
  1147. static int resizer_set_selection(struct v4l2_subdev *sd,
  1148. struct v4l2_subdev_fh *fh,
  1149. struct v4l2_subdev_selection *sel)
  1150. {
  1151. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1152. struct isp_device *isp = to_isp_device(res);
  1153. struct v4l2_mbus_framefmt *format_sink, *format_source;
  1154. struct resizer_ratio ratio;
  1155. if (sel->target != V4L2_SEL_TGT_CROP ||
  1156. sel->pad != RESZ_PAD_SINK)
  1157. return -EINVAL;
  1158. format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
  1159. sel->which);
  1160. format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
  1161. sel->which);
  1162. dev_dbg(isp->dev, "%s: L=%d,T=%d,W=%d,H=%d,which=%d\n", __func__,
  1163. sel->r.left, sel->r.top, sel->r.width, sel->r.height,
  1164. sel->which);
  1165. dev_dbg(isp->dev, "%s: input=%dx%d, output=%dx%d\n", __func__,
  1166. format_sink->width, format_sink->height,
  1167. format_source->width, format_source->height);
  1168. /* Clamp the crop rectangle to the bounds, and then mangle it further to
  1169. * fulfill the TRM equations. Store the clamped but otherwise unmangled
  1170. * rectangle to avoid cropping the input multiple times: when an
  1171. * application sets the output format, the current crop rectangle is
  1172. * mangled during crop rectangle computation, which would lead to a new,
  1173. * smaller input crop rectangle every time the output size is set if we
  1174. * stored the mangled rectangle.
  1175. */
  1176. resizer_try_crop(format_sink, format_source, &sel->r);
  1177. *__resizer_get_crop(res, fh, sel->which) = sel->r;
  1178. resizer_calc_ratios(res, &sel->r, format_source, &ratio);
  1179. if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
  1180. return 0;
  1181. res->ratio = ratio;
  1182. res->crop.active = sel->r;
  1183. /*
  1184. * set_selection can be called while streaming is on. In this case the
  1185. * crop values will be set in the next IRQ.
  1186. */
  1187. if (res->state != ISP_PIPELINE_STREAM_STOPPED)
  1188. res->applycrop = 1;
  1189. return 0;
  1190. }
  1191. /* resizer pixel formats */
  1192. static const unsigned int resizer_formats[] = {
  1193. V4L2_MBUS_FMT_UYVY8_1X16,
  1194. V4L2_MBUS_FMT_YUYV8_1X16,
  1195. };
  1196. static unsigned int resizer_max_in_width(struct isp_res_device *res)
  1197. {
  1198. struct isp_device *isp = to_isp_device(res);
  1199. if (res->input == RESIZER_INPUT_MEMORY) {
  1200. return MAX_IN_WIDTH_MEMORY_MODE;
  1201. } else {
  1202. if (isp->revision == ISP_REVISION_1_0)
  1203. return MAX_IN_WIDTH_ONTHEFLY_MODE_ES1;
  1204. else
  1205. return MAX_IN_WIDTH_ONTHEFLY_MODE_ES2;
  1206. }
  1207. }
  1208. /*
  1209. * resizer_try_format - Handle try format by pad subdev method
  1210. * @res : ISP resizer device
  1211. * @fh : V4L2 subdev file handle
  1212. * @pad : pad num
  1213. * @fmt : pointer to v4l2 format structure
  1214. * @which : wanted subdev format
  1215. */
  1216. static void resizer_try_format(struct isp_res_device *res,
  1217. struct v4l2_subdev_fh *fh, unsigned int pad,
  1218. struct v4l2_mbus_framefmt *fmt,
  1219. enum v4l2_subdev_format_whence which)
  1220. {
  1221. struct v4l2_mbus_framefmt *format;
  1222. struct resizer_ratio ratio;
  1223. struct v4l2_rect crop;
  1224. switch (pad) {
  1225. case RESZ_PAD_SINK:
  1226. if (fmt->code != V4L2_MBUS_FMT_YUYV8_1X16 &&
  1227. fmt->code != V4L2_MBUS_FMT_UYVY8_1X16)
  1228. fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
  1229. fmt->width = clamp_t(u32, fmt->width, MIN_IN_WIDTH,
  1230. resizer_max_in_width(res));
  1231. fmt->height = clamp_t(u32, fmt->height, MIN_IN_HEIGHT,
  1232. MAX_IN_HEIGHT);
  1233. break;
  1234. case RESZ_PAD_SOURCE:
  1235. format = __resizer_get_format(res, fh, RESZ_PAD_SINK, which);
  1236. fmt->code = format->code;
  1237. crop = *__resizer_get_crop(res, fh, which);
  1238. resizer_calc_ratios(res, &crop, fmt, &ratio);
  1239. break;
  1240. }
  1241. fmt->colorspace = V4L2_COLORSPACE_JPEG;
  1242. fmt->field = V4L2_FIELD_NONE;
  1243. }
  1244. /*
  1245. * resizer_enum_mbus_code - Handle pixel format enumeration
  1246. * @sd : pointer to v4l2 subdev structure
  1247. * @fh : V4L2 subdev file handle
  1248. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  1249. * return -EINVAL or zero on success
  1250. */
  1251. static int resizer_enum_mbus_code(struct v4l2_subdev *sd,
  1252. struct v4l2_subdev_fh *fh,
  1253. struct v4l2_subdev_mbus_code_enum *code)
  1254. {
  1255. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1256. struct v4l2_mbus_framefmt *format;
  1257. if (code->pad == RESZ_PAD_SINK) {
  1258. if (code->index >= ARRAY_SIZE(resizer_formats))
  1259. return -EINVAL;
  1260. code->code = resizer_formats[code->index];
  1261. } else {
  1262. if (code->index != 0)
  1263. return -EINVAL;
  1264. format = __resizer_get_format(res, fh, RESZ_PAD_SINK,
  1265. V4L2_SUBDEV_FORMAT_TRY);
  1266. code->code = format->code;
  1267. }
  1268. return 0;
  1269. }
  1270. static int resizer_enum_frame_size(struct v4l2_subdev *sd,
  1271. struct v4l2_subdev_fh *fh,
  1272. struct v4l2_subdev_frame_size_enum *fse)
  1273. {
  1274. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1275. struct v4l2_mbus_framefmt format;
  1276. if (fse->index != 0)
  1277. return -EINVAL;
  1278. format.code = fse->code;
  1279. format.width = 1;
  1280. format.height = 1;
  1281. resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1282. fse->min_width = format.width;
  1283. fse->min_height = format.height;
  1284. if (format.code != fse->code)
  1285. return -EINVAL;
  1286. format.code = fse->code;
  1287. format.width = -1;
  1288. format.height = -1;
  1289. resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
  1290. fse->max_width = format.width;
  1291. fse->max_height = format.height;
  1292. return 0;
  1293. }
  1294. /*
  1295. * resizer_get_format - Handle get format by pads subdev method
  1296. * @sd : pointer to v4l2 subdev structure
  1297. * @fh : V4L2 subdev file handle
  1298. * @fmt : pointer to v4l2 subdev format structure
  1299. * return -EINVAL or zero on success
  1300. */
  1301. static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1302. struct v4l2_subdev_format *fmt)
  1303. {
  1304. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1305. struct v4l2_mbus_framefmt *format;
  1306. format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
  1307. if (format == NULL)
  1308. return -EINVAL;
  1309. fmt->format = *format;
  1310. return 0;
  1311. }
  1312. /*
  1313. * resizer_set_format - Handle set format by pads subdev method
  1314. * @sd : pointer to v4l2 subdev structure
  1315. * @fh : V4L2 subdev file handle
  1316. * @fmt : pointer to v4l2 subdev format structure
  1317. * return -EINVAL or zero on success
  1318. */
  1319. static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  1320. struct v4l2_subdev_format *fmt)
  1321. {
  1322. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1323. struct v4l2_mbus_framefmt *format;
  1324. struct v4l2_rect *crop;
  1325. format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
  1326. if (format == NULL)
  1327. return -EINVAL;
  1328. resizer_try_format(res, fh, fmt->pad, &fmt->format, fmt->which);
  1329. *format = fmt->format;
  1330. if (fmt->pad == RESZ_PAD_SINK) {
  1331. /* reset crop rectangle */
  1332. crop = __resizer_get_crop(res, fh, fmt->which);
  1333. crop->left = 0;
  1334. crop->top = 0;
  1335. crop->width = fmt->format.width;
  1336. crop->height = fmt->format.height;
  1337. /* Propagate the format from sink to source */
  1338. format = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
  1339. fmt->which);
  1340. *format = fmt->format;
  1341. resizer_try_format(res, fh, RESZ_PAD_SOURCE, format,
  1342. fmt->which);
  1343. }
  1344. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1345. /* Compute and store the active crop rectangle and resizer
  1346. * ratios. format already points to the source pad active
  1347. * format.
  1348. */
  1349. res->crop.active = res->crop.request;
  1350. resizer_calc_ratios(res, &res->crop.active, format,
  1351. &res->ratio);
  1352. }
  1353. return 0;
  1354. }
  1355. /*
  1356. * resizer_init_formats - Initialize formats on all pads
  1357. * @sd: ISP resizer V4L2 subdevice
  1358. * @fh: V4L2 subdev file handle
  1359. *
  1360. * Initialize all pad formats with default values. If fh is not NULL, try
  1361. * formats are initialized on the file handle. Otherwise active formats are
  1362. * initialized on the device.
  1363. */
  1364. static int resizer_init_formats(struct v4l2_subdev *sd,
  1365. struct v4l2_subdev_fh *fh)
  1366. {
  1367. struct v4l2_subdev_format format;
  1368. memset(&format, 0, sizeof(format));
  1369. format.pad = RESZ_PAD_SINK;
  1370. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  1371. format.format.code = V4L2_MBUS_FMT_YUYV8_1X16;
  1372. format.format.width = 4096;
  1373. format.format.height = 4096;
  1374. resizer_set_format(sd, fh, &format);
  1375. return 0;
  1376. }
  1377. /* subdev video operations */
  1378. static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
  1379. .s_stream = resizer_set_stream,
  1380. };
  1381. /* subdev pad operations */
  1382. static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = {
  1383. .enum_mbus_code = resizer_enum_mbus_code,
  1384. .enum_frame_size = resizer_enum_frame_size,
  1385. .get_fmt = resizer_get_format,
  1386. .set_fmt = resizer_set_format,
  1387. .get_selection = resizer_get_selection,
  1388. .set_selection = resizer_set_selection,
  1389. };
  1390. /* subdev operations */
  1391. static const struct v4l2_subdev_ops resizer_v4l2_ops = {
  1392. .video = &resizer_v4l2_video_ops,
  1393. .pad = &resizer_v4l2_pad_ops,
  1394. };
  1395. /* subdev internal operations */
  1396. static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
  1397. .open = resizer_init_formats,
  1398. };
  1399. /* -----------------------------------------------------------------------------
  1400. * Media entity operations
  1401. */
  1402. /*
  1403. * resizer_link_setup - Setup resizer connections.
  1404. * @entity : Pointer to media entity structure
  1405. * @local : Pointer to local pad array
  1406. * @remote : Pointer to remote pad array
  1407. * @flags : Link flags
  1408. * return -EINVAL or zero on success
  1409. */
  1410. static int resizer_link_setup(struct media_entity *entity,
  1411. const struct media_pad *local,
  1412. const struct media_pad *remote, u32 flags)
  1413. {
  1414. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1415. struct isp_res_device *res = v4l2_get_subdevdata(sd);
  1416. switch (local->index | media_entity_type(remote->entity)) {
  1417. case RESZ_PAD_SINK | MEDIA_ENT_T_DEVNODE:
  1418. /* read from memory */
  1419. if (flags & MEDIA_LNK_FL_ENABLED) {
  1420. if (res->input == RESIZER_INPUT_VP)
  1421. return -EBUSY;
  1422. res->input = RESIZER_INPUT_MEMORY;
  1423. } else {
  1424. if (res->input == RESIZER_INPUT_MEMORY)
  1425. res->input = RESIZER_INPUT_NONE;
  1426. }
  1427. break;
  1428. case RESZ_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
  1429. /* read from ccdc or previewer */
  1430. if (flags & MEDIA_LNK_FL_ENABLED) {
  1431. if (res->input == RESIZER_INPUT_MEMORY)
  1432. return -EBUSY;
  1433. res->input = RESIZER_INPUT_VP;
  1434. } else {
  1435. if (res->input == RESIZER_INPUT_VP)
  1436. res->input = RESIZER_INPUT_NONE;
  1437. }
  1438. break;
  1439. case RESZ_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
  1440. /* resizer always write to memory */
  1441. break;
  1442. default:
  1443. return -EINVAL;
  1444. }
  1445. return 0;
  1446. }
  1447. /* media operations */
  1448. static const struct media_entity_operations resizer_media_ops = {
  1449. .link_setup = resizer_link_setup,
  1450. .link_validate = v4l2_subdev_link_validate,
  1451. };
  1452. void omap3isp_resizer_unregister_entities(struct isp_res_device *res)
  1453. {
  1454. v4l2_device_unregister_subdev(&res->subdev);
  1455. omap3isp_video_unregister(&res->video_in);
  1456. omap3isp_video_unregister(&res->video_out);
  1457. }
  1458. int omap3isp_resizer_register_entities(struct isp_res_device *res,
  1459. struct v4l2_device *vdev)
  1460. {
  1461. int ret;
  1462. /* Register the subdev and video nodes. */
  1463. ret = v4l2_device_register_subdev(vdev, &res->subdev);
  1464. if (ret < 0)
  1465. goto error;
  1466. ret = omap3isp_video_register(&res->video_in, vdev);
  1467. if (ret < 0)
  1468. goto error;
  1469. ret = omap3isp_video_register(&res->video_out, vdev);
  1470. if (ret < 0)
  1471. goto error;
  1472. return 0;
  1473. error:
  1474. omap3isp_resizer_unregister_entities(res);
  1475. return ret;
  1476. }
  1477. /* -----------------------------------------------------------------------------
  1478. * ISP resizer initialization and cleanup
  1479. */
  1480. /*
  1481. * resizer_init_entities - Initialize resizer subdev and media entity.
  1482. * @res : Pointer to resizer device structure
  1483. * return -ENOMEM or zero on success
  1484. */
  1485. static int resizer_init_entities(struct isp_res_device *res)
  1486. {
  1487. struct v4l2_subdev *sd = &res->subdev;
  1488. struct media_pad *pads = res->pads;
  1489. struct media_entity *me = &sd->entity;
  1490. int ret;
  1491. res->input = RESIZER_INPUT_NONE;
  1492. v4l2_subdev_init(sd, &resizer_v4l2_ops);
  1493. sd->internal_ops = &resizer_v4l2_internal_ops;
  1494. strlcpy(sd->name, "OMAP3 ISP resizer", sizeof(sd->name));
  1495. sd->grp_id = 1 << 16; /* group ID for isp subdevs */
  1496. v4l2_set_subdevdata(sd, res);
  1497. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1498. pads[RESZ_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1499. pads[RESZ_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1500. me->ops = &resizer_media_ops;
  1501. ret = media_entity_init(me, RESZ_PADS_NUM, pads, 0);
  1502. if (ret < 0)
  1503. return ret;
  1504. resizer_init_formats(sd, NULL);
  1505. res->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1506. res->video_in.ops = &resizer_video_ops;
  1507. res->video_in.isp = to_isp_device(res);
  1508. res->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1509. res->video_in.bpl_alignment = 32;
  1510. res->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1511. res->video_out.ops = &resizer_video_ops;
  1512. res->video_out.isp = to_isp_device(res);
  1513. res->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
  1514. res->video_out.bpl_alignment = 32;
  1515. ret = omap3isp_video_init(&res->video_in, "resizer");
  1516. if (ret < 0)
  1517. goto error_video_in;
  1518. ret = omap3isp_video_init(&res->video_out, "resizer");
  1519. if (ret < 0)
  1520. goto error_video_out;
  1521. res->video_out.video.entity.flags |= MEDIA_ENT_FL_DEFAULT;
  1522. /* Connect the video nodes to the resizer subdev. */
  1523. ret = media_entity_create_link(&res->video_in.video.entity, 0,
  1524. &res->subdev.entity, RESZ_PAD_SINK, 0);
  1525. if (ret < 0)
  1526. goto error_link;
  1527. ret = media_entity_create_link(&res->subdev.entity, RESZ_PAD_SOURCE,
  1528. &res->video_out.video.entity, 0, 0);
  1529. if (ret < 0)
  1530. goto error_link;
  1531. return 0;
  1532. error_link:
  1533. omap3isp_video_cleanup(&res->video_out);
  1534. error_video_out:
  1535. omap3isp_video_cleanup(&res->video_in);
  1536. error_video_in:
  1537. media_entity_cleanup(&res->subdev.entity);
  1538. return ret;
  1539. }
  1540. /*
  1541. * isp_resizer_init - Resizer initialization.
  1542. * @isp : Pointer to ISP device
  1543. * return -ENOMEM or zero on success
  1544. */
  1545. int omap3isp_resizer_init(struct isp_device *isp)
  1546. {
  1547. struct isp_res_device *res = &isp->isp_res;
  1548. init_waitqueue_head(&res->wait);
  1549. atomic_set(&res->stopping, 0);
  1550. return resizer_init_entities(res);
  1551. }
  1552. void omap3isp_resizer_cleanup(struct isp_device *isp)
  1553. {
  1554. struct isp_res_device *res = &isp->isp_res;
  1555. omap3isp_video_cleanup(&res->video_in);
  1556. omap3isp_video_cleanup(&res->video_out);
  1557. media_entity_cleanup(&res->subdev.entity);
  1558. }