gsc-core.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /*
  2. * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Samsung EXYNOS5 SoC series G-Scaler driver
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation, either version 2 of the License,
  10. * or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/bug.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/list.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <linux/of.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include "gsc-core.h"
  28. #define GSC_CLOCK_GATE_NAME "gscl"
  29. static const struct gsc_fmt gsc_formats[] = {
  30. {
  31. .name = "RGB565",
  32. .pixelformat = V4L2_PIX_FMT_RGB565X,
  33. .depth = { 16 },
  34. .color = GSC_RGB,
  35. .num_planes = 1,
  36. .num_comp = 1,
  37. }, {
  38. .name = "XRGB-8-8-8-8, 32 bpp",
  39. .pixelformat = V4L2_PIX_FMT_RGB32,
  40. .depth = { 32 },
  41. .color = GSC_RGB,
  42. .num_planes = 1,
  43. .num_comp = 1,
  44. }, {
  45. .name = "YUV 4:2:2 packed, YCbYCr",
  46. .pixelformat = V4L2_PIX_FMT_YUYV,
  47. .depth = { 16 },
  48. .color = GSC_YUV422,
  49. .yorder = GSC_LSB_Y,
  50. .corder = GSC_CBCR,
  51. .num_planes = 1,
  52. .num_comp = 1,
  53. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  54. }, {
  55. .name = "YUV 4:2:2 packed, CbYCrY",
  56. .pixelformat = V4L2_PIX_FMT_UYVY,
  57. .depth = { 16 },
  58. .color = GSC_YUV422,
  59. .yorder = GSC_LSB_C,
  60. .corder = GSC_CBCR,
  61. .num_planes = 1,
  62. .num_comp = 1,
  63. .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
  64. }, {
  65. .name = "YUV 4:2:2 packed, CrYCbY",
  66. .pixelformat = V4L2_PIX_FMT_VYUY,
  67. .depth = { 16 },
  68. .color = GSC_YUV422,
  69. .yorder = GSC_LSB_C,
  70. .corder = GSC_CRCB,
  71. .num_planes = 1,
  72. .num_comp = 1,
  73. .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
  74. }, {
  75. .name = "YUV 4:2:2 packed, YCrYCb",
  76. .pixelformat = V4L2_PIX_FMT_YVYU,
  77. .depth = { 16 },
  78. .color = GSC_YUV422,
  79. .yorder = GSC_LSB_Y,
  80. .corder = GSC_CRCB,
  81. .num_planes = 1,
  82. .num_comp = 1,
  83. .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
  84. }, {
  85. .name = "YUV 4:4:4 planar, YCbYCr",
  86. .pixelformat = V4L2_PIX_FMT_YUV32,
  87. .depth = { 32 },
  88. .color = GSC_YUV444,
  89. .yorder = GSC_LSB_Y,
  90. .corder = GSC_CBCR,
  91. .num_planes = 1,
  92. .num_comp = 1,
  93. }, {
  94. .name = "YUV 4:2:2 planar, Y/Cb/Cr",
  95. .pixelformat = V4L2_PIX_FMT_YUV422P,
  96. .depth = { 16 },
  97. .color = GSC_YUV422,
  98. .yorder = GSC_LSB_Y,
  99. .corder = GSC_CBCR,
  100. .num_planes = 1,
  101. .num_comp = 3,
  102. }, {
  103. .name = "YUV 4:2:2 planar, Y/CbCr",
  104. .pixelformat = V4L2_PIX_FMT_NV16,
  105. .depth = { 16 },
  106. .color = GSC_YUV422,
  107. .yorder = GSC_LSB_Y,
  108. .corder = GSC_CBCR,
  109. .num_planes = 1,
  110. .num_comp = 2,
  111. }, {
  112. .name = "YUV 4:2:2 planar, Y/CrCb",
  113. .pixelformat = V4L2_PIX_FMT_NV61,
  114. .depth = { 16 },
  115. .color = GSC_YUV422,
  116. .yorder = GSC_LSB_Y,
  117. .corder = GSC_CRCB,
  118. .num_planes = 1,
  119. .num_comp = 2,
  120. }, {
  121. .name = "YUV 4:2:0 planar, YCbCr",
  122. .pixelformat = V4L2_PIX_FMT_YUV420,
  123. .depth = { 12 },
  124. .color = GSC_YUV420,
  125. .yorder = GSC_LSB_Y,
  126. .corder = GSC_CBCR,
  127. .num_planes = 1,
  128. .num_comp = 3,
  129. }, {
  130. .name = "YUV 4:2:0 planar, YCrCb",
  131. .pixelformat = V4L2_PIX_FMT_YVU420,
  132. .depth = { 12 },
  133. .color = GSC_YUV420,
  134. .yorder = GSC_LSB_Y,
  135. .corder = GSC_CRCB,
  136. .num_planes = 1,
  137. .num_comp = 3,
  138. }, {
  139. .name = "YUV 4:2:0 planar, Y/CbCr",
  140. .pixelformat = V4L2_PIX_FMT_NV12,
  141. .depth = { 12 },
  142. .color = GSC_YUV420,
  143. .yorder = GSC_LSB_Y,
  144. .corder = GSC_CBCR,
  145. .num_planes = 1,
  146. .num_comp = 2,
  147. }, {
  148. .name = "YUV 4:2:0 planar, Y/CrCb",
  149. .pixelformat = V4L2_PIX_FMT_NV21,
  150. .depth = { 12 },
  151. .color = GSC_YUV420,
  152. .yorder = GSC_LSB_Y,
  153. .corder = GSC_CRCB,
  154. .num_planes = 1,
  155. .num_comp = 2,
  156. }, {
  157. .name = "YUV 4:2:0 non-contig. 2p, Y/CbCr",
  158. .pixelformat = V4L2_PIX_FMT_NV12M,
  159. .depth = { 8, 4 },
  160. .color = GSC_YUV420,
  161. .yorder = GSC_LSB_Y,
  162. .corder = GSC_CBCR,
  163. .num_planes = 2,
  164. .num_comp = 2,
  165. }, {
  166. .name = "YUV 4:2:0 non-contig. 3p, Y/Cb/Cr",
  167. .pixelformat = V4L2_PIX_FMT_YUV420M,
  168. .depth = { 8, 2, 2 },
  169. .color = GSC_YUV420,
  170. .yorder = GSC_LSB_Y,
  171. .corder = GSC_CBCR,
  172. .num_planes = 3,
  173. .num_comp = 3,
  174. }, {
  175. .name = "YUV 4:2:0 non-contig. 3p, Y/Cr/Cb",
  176. .pixelformat = V4L2_PIX_FMT_YVU420M,
  177. .depth = { 8, 2, 2 },
  178. .color = GSC_YUV420,
  179. .yorder = GSC_LSB_Y,
  180. .corder = GSC_CRCB,
  181. .num_planes = 3,
  182. .num_comp = 3,
  183. }, {
  184. .name = "YUV 4:2:0 n.c. 2p, Y/CbCr tiled",
  185. .pixelformat = V4L2_PIX_FMT_NV12MT_16X16,
  186. .depth = { 8, 4 },
  187. .color = GSC_YUV420,
  188. .yorder = GSC_LSB_Y,
  189. .corder = GSC_CBCR,
  190. .num_planes = 2,
  191. .num_comp = 2,
  192. }
  193. };
  194. const struct gsc_fmt *get_format(int index)
  195. {
  196. if (index >= ARRAY_SIZE(gsc_formats))
  197. return NULL;
  198. return (struct gsc_fmt *)&gsc_formats[index];
  199. }
  200. const struct gsc_fmt *find_fmt(u32 *pixelformat, u32 *mbus_code, u32 index)
  201. {
  202. const struct gsc_fmt *fmt, *def_fmt = NULL;
  203. unsigned int i;
  204. if (index >= ARRAY_SIZE(gsc_formats))
  205. return NULL;
  206. for (i = 0; i < ARRAY_SIZE(gsc_formats); ++i) {
  207. fmt = get_format(i);
  208. if (pixelformat && fmt->pixelformat == *pixelformat)
  209. return fmt;
  210. if (mbus_code && fmt->mbus_code == *mbus_code)
  211. return fmt;
  212. if (index == i)
  213. def_fmt = fmt;
  214. }
  215. return def_fmt;
  216. }
  217. void gsc_set_frame_size(struct gsc_frame *frame, int width, int height)
  218. {
  219. frame->f_width = width;
  220. frame->f_height = height;
  221. frame->crop.width = width;
  222. frame->crop.height = height;
  223. frame->crop.left = 0;
  224. frame->crop.top = 0;
  225. }
  226. int gsc_cal_prescaler_ratio(struct gsc_variant *var, u32 src, u32 dst,
  227. u32 *ratio)
  228. {
  229. if ((dst > src) || (dst >= src / var->poly_sc_down_max)) {
  230. *ratio = 1;
  231. return 0;
  232. }
  233. if ((src / var->poly_sc_down_max / var->pre_sc_down_max) > dst) {
  234. pr_err("Exceeded maximum downscaling ratio (1/16))");
  235. return -EINVAL;
  236. }
  237. *ratio = (dst > (src / 8)) ? 2 : 4;
  238. return 0;
  239. }
  240. void gsc_get_prescaler_shfactor(u32 hratio, u32 vratio, u32 *sh)
  241. {
  242. if (hratio == 4 && vratio == 4)
  243. *sh = 4;
  244. else if ((hratio == 4 && vratio == 2) ||
  245. (hratio == 2 && vratio == 4))
  246. *sh = 3;
  247. else if ((hratio == 4 && vratio == 1) ||
  248. (hratio == 1 && vratio == 4) ||
  249. (hratio == 2 && vratio == 2))
  250. *sh = 2;
  251. else if (hratio == 1 && vratio == 1)
  252. *sh = 0;
  253. else
  254. *sh = 1;
  255. }
  256. void gsc_check_src_scale_info(struct gsc_variant *var,
  257. struct gsc_frame *s_frame, u32 *wratio,
  258. u32 tx, u32 ty, u32 *hratio)
  259. {
  260. int remainder = 0, walign, halign;
  261. if (is_yuv420(s_frame->fmt->color)) {
  262. walign = GSC_SC_ALIGN_4;
  263. halign = GSC_SC_ALIGN_4;
  264. } else if (is_yuv422(s_frame->fmt->color)) {
  265. walign = GSC_SC_ALIGN_4;
  266. halign = GSC_SC_ALIGN_2;
  267. } else {
  268. walign = GSC_SC_ALIGN_2;
  269. halign = GSC_SC_ALIGN_2;
  270. }
  271. remainder = s_frame->crop.width % (*wratio * walign);
  272. if (remainder) {
  273. s_frame->crop.width -= remainder;
  274. gsc_cal_prescaler_ratio(var, s_frame->crop.width, tx, wratio);
  275. pr_info("cropped src width size is recalculated from %d to %d",
  276. s_frame->crop.width + remainder, s_frame->crop.width);
  277. }
  278. remainder = s_frame->crop.height % (*hratio * halign);
  279. if (remainder) {
  280. s_frame->crop.height -= remainder;
  281. gsc_cal_prescaler_ratio(var, s_frame->crop.height, ty, hratio);
  282. pr_info("cropped src height size is recalculated from %d to %d",
  283. s_frame->crop.height + remainder, s_frame->crop.height);
  284. }
  285. }
  286. int gsc_enum_fmt_mplane(struct v4l2_fmtdesc *f)
  287. {
  288. const struct gsc_fmt *fmt;
  289. fmt = find_fmt(NULL, NULL, f->index);
  290. if (!fmt)
  291. return -EINVAL;
  292. strlcpy(f->description, fmt->name, sizeof(f->description));
  293. f->pixelformat = fmt->pixelformat;
  294. return 0;
  295. }
  296. static u32 get_plane_info(struct gsc_frame *frm, u32 addr, u32 *index)
  297. {
  298. if (frm->addr.y == addr) {
  299. *index = 0;
  300. return frm->addr.y;
  301. } else if (frm->addr.cb == addr) {
  302. *index = 1;
  303. return frm->addr.cb;
  304. } else if (frm->addr.cr == addr) {
  305. *index = 2;
  306. return frm->addr.cr;
  307. } else {
  308. pr_err("Plane address is wrong");
  309. return -EINVAL;
  310. }
  311. }
  312. void gsc_set_prefbuf(struct gsc_dev *gsc, struct gsc_frame *frm)
  313. {
  314. u32 f_chk_addr, f_chk_len, s_chk_addr, s_chk_len;
  315. f_chk_addr = f_chk_len = s_chk_addr = s_chk_len = 0;
  316. f_chk_addr = frm->addr.y;
  317. f_chk_len = frm->payload[0];
  318. if (frm->fmt->num_planes == 2) {
  319. s_chk_addr = frm->addr.cb;
  320. s_chk_len = frm->payload[1];
  321. } else if (frm->fmt->num_planes == 3) {
  322. u32 low_addr, low_plane, mid_addr, mid_plane;
  323. u32 high_addr, high_plane;
  324. u32 t_min, t_max;
  325. t_min = min3(frm->addr.y, frm->addr.cb, frm->addr.cr);
  326. low_addr = get_plane_info(frm, t_min, &low_plane);
  327. t_max = max3(frm->addr.y, frm->addr.cb, frm->addr.cr);
  328. high_addr = get_plane_info(frm, t_max, &high_plane);
  329. mid_plane = 3 - (low_plane + high_plane);
  330. if (mid_plane == 0)
  331. mid_addr = frm->addr.y;
  332. else if (mid_plane == 1)
  333. mid_addr = frm->addr.cb;
  334. else if (mid_plane == 2)
  335. mid_addr = frm->addr.cr;
  336. else
  337. return;
  338. f_chk_addr = low_addr;
  339. if (mid_addr + frm->payload[mid_plane] - low_addr >
  340. high_addr + frm->payload[high_plane] - mid_addr) {
  341. f_chk_len = frm->payload[low_plane];
  342. s_chk_addr = mid_addr;
  343. s_chk_len = high_addr +
  344. frm->payload[high_plane] - mid_addr;
  345. } else {
  346. f_chk_len = mid_addr +
  347. frm->payload[mid_plane] - low_addr;
  348. s_chk_addr = high_addr;
  349. s_chk_len = frm->payload[high_plane];
  350. }
  351. }
  352. pr_debug("f_addr = 0x%08x, f_len = %d, s_addr = 0x%08x, s_len = %d\n",
  353. f_chk_addr, f_chk_len, s_chk_addr, s_chk_len);
  354. }
  355. int gsc_try_fmt_mplane(struct gsc_ctx *ctx, struct v4l2_format *f)
  356. {
  357. struct gsc_dev *gsc = ctx->gsc_dev;
  358. struct gsc_variant *variant = gsc->variant;
  359. struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  360. const struct gsc_fmt *fmt;
  361. u32 max_w, max_h, mod_x, mod_y;
  362. u32 min_w, min_h, tmp_w, tmp_h;
  363. int i;
  364. pr_debug("user put w: %d, h: %d", pix_mp->width, pix_mp->height);
  365. fmt = find_fmt(&pix_mp->pixelformat, NULL, 0);
  366. if (!fmt) {
  367. pr_err("pixelformat format (0x%X) invalid\n",
  368. pix_mp->pixelformat);
  369. return -EINVAL;
  370. }
  371. if (pix_mp->field == V4L2_FIELD_ANY)
  372. pix_mp->field = V4L2_FIELD_NONE;
  373. else if (pix_mp->field != V4L2_FIELD_NONE) {
  374. pr_err("Not supported field order(%d)\n", pix_mp->field);
  375. return -EINVAL;
  376. }
  377. max_w = variant->pix_max->target_rot_dis_w;
  378. max_h = variant->pix_max->target_rot_dis_h;
  379. mod_x = ffs(variant->pix_align->org_w) - 1;
  380. if (is_yuv420(fmt->color))
  381. mod_y = ffs(variant->pix_align->org_h) - 1;
  382. else
  383. mod_y = ffs(variant->pix_align->org_h) - 2;
  384. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  385. min_w = variant->pix_min->org_w;
  386. min_h = variant->pix_min->org_h;
  387. } else {
  388. min_w = variant->pix_min->target_rot_dis_w;
  389. min_h = variant->pix_min->target_rot_dis_h;
  390. }
  391. pr_debug("mod_x: %d, mod_y: %d, max_w: %d, max_h = %d",
  392. mod_x, mod_y, max_w, max_h);
  393. /* To check if image size is modified to adjust parameter against
  394. hardware abilities */
  395. tmp_w = pix_mp->width;
  396. tmp_h = pix_mp->height;
  397. v4l_bound_align_image(&pix_mp->width, min_w, max_w, mod_x,
  398. &pix_mp->height, min_h, max_h, mod_y, 0);
  399. if (tmp_w != pix_mp->width || tmp_h != pix_mp->height)
  400. pr_info("Image size has been modified from %dx%d to %dx%d",
  401. tmp_w, tmp_h, pix_mp->width, pix_mp->height);
  402. pix_mp->num_planes = fmt->num_planes;
  403. if (pix_mp->width >= 1280) /* HD */
  404. pix_mp->colorspace = V4L2_COLORSPACE_REC709;
  405. else /* SD */
  406. pix_mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
  407. for (i = 0; i < pix_mp->num_planes; ++i) {
  408. int bpl = (pix_mp->width * fmt->depth[i]) >> 3;
  409. pix_mp->plane_fmt[i].bytesperline = bpl;
  410. pix_mp->plane_fmt[i].sizeimage = bpl * pix_mp->height;
  411. pr_debug("[%d]: bpl: %d, sizeimage: %d",
  412. i, bpl, pix_mp->plane_fmt[i].sizeimage);
  413. }
  414. return 0;
  415. }
  416. int gsc_g_fmt_mplane(struct gsc_ctx *ctx, struct v4l2_format *f)
  417. {
  418. struct gsc_frame *frame;
  419. struct v4l2_pix_format_mplane *pix_mp;
  420. int i;
  421. frame = ctx_get_frame(ctx, f->type);
  422. if (IS_ERR(frame))
  423. return PTR_ERR(frame);
  424. pix_mp = &f->fmt.pix_mp;
  425. pix_mp->width = frame->f_width;
  426. pix_mp->height = frame->f_height;
  427. pix_mp->field = V4L2_FIELD_NONE;
  428. pix_mp->pixelformat = frame->fmt->pixelformat;
  429. pix_mp->colorspace = V4L2_COLORSPACE_REC709;
  430. pix_mp->num_planes = frame->fmt->num_planes;
  431. for (i = 0; i < pix_mp->num_planes; ++i) {
  432. pix_mp->plane_fmt[i].bytesperline = (frame->f_width *
  433. frame->fmt->depth[i]) / 8;
  434. pix_mp->plane_fmt[i].sizeimage =
  435. pix_mp->plane_fmt[i].bytesperline * frame->f_height;
  436. }
  437. return 0;
  438. }
  439. void gsc_check_crop_change(u32 tmp_w, u32 tmp_h, u32 *w, u32 *h)
  440. {
  441. if (tmp_w != *w || tmp_h != *h) {
  442. pr_info("Cropped size has been modified from %dx%d to %dx%d",
  443. *w, *h, tmp_w, tmp_h);
  444. *w = tmp_w;
  445. *h = tmp_h;
  446. }
  447. }
  448. int gsc_g_crop(struct gsc_ctx *ctx, struct v4l2_crop *cr)
  449. {
  450. struct gsc_frame *frame;
  451. frame = ctx_get_frame(ctx, cr->type);
  452. if (IS_ERR(frame))
  453. return PTR_ERR(frame);
  454. cr->c = frame->crop;
  455. return 0;
  456. }
  457. int gsc_try_crop(struct gsc_ctx *ctx, struct v4l2_crop *cr)
  458. {
  459. struct gsc_frame *f;
  460. struct gsc_dev *gsc = ctx->gsc_dev;
  461. struct gsc_variant *variant = gsc->variant;
  462. u32 mod_x = 0, mod_y = 0, tmp_w, tmp_h;
  463. u32 min_w, min_h, max_w, max_h;
  464. if (cr->c.top < 0 || cr->c.left < 0) {
  465. pr_err("doesn't support negative values for top & left\n");
  466. return -EINVAL;
  467. }
  468. pr_debug("user put w: %d, h: %d", cr->c.width, cr->c.height);
  469. if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  470. f = &ctx->d_frame;
  471. else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  472. f = &ctx->s_frame;
  473. else
  474. return -EINVAL;
  475. max_w = f->f_width;
  476. max_h = f->f_height;
  477. tmp_w = cr->c.width;
  478. tmp_h = cr->c.height;
  479. if (V4L2_TYPE_IS_OUTPUT(cr->type)) {
  480. if ((is_yuv422(f->fmt->color) && f->fmt->num_comp == 1) ||
  481. is_rgb(f->fmt->color))
  482. min_w = 32;
  483. else
  484. min_w = 64;
  485. if ((is_yuv422(f->fmt->color) && f->fmt->num_comp == 3) ||
  486. is_yuv420(f->fmt->color))
  487. min_h = 32;
  488. else
  489. min_h = 16;
  490. } else {
  491. if (is_yuv420(f->fmt->color) || is_yuv422(f->fmt->color))
  492. mod_x = ffs(variant->pix_align->target_w) - 1;
  493. if (is_yuv420(f->fmt->color))
  494. mod_y = ffs(variant->pix_align->target_h) - 1;
  495. if (ctx->gsc_ctrls.rotate->val == 90 ||
  496. ctx->gsc_ctrls.rotate->val == 270) {
  497. max_w = f->f_height;
  498. max_h = f->f_width;
  499. min_w = variant->pix_min->target_rot_en_w;
  500. min_h = variant->pix_min->target_rot_en_h;
  501. tmp_w = cr->c.height;
  502. tmp_h = cr->c.width;
  503. } else {
  504. min_w = variant->pix_min->target_rot_dis_w;
  505. min_h = variant->pix_min->target_rot_dis_h;
  506. }
  507. }
  508. pr_debug("mod_x: %d, mod_y: %d, min_w: %d, min_h = %d",
  509. mod_x, mod_y, min_w, min_h);
  510. pr_debug("tmp_w : %d, tmp_h : %d", tmp_w, tmp_h);
  511. v4l_bound_align_image(&tmp_w, min_w, max_w, mod_x,
  512. &tmp_h, min_h, max_h, mod_y, 0);
  513. if (!V4L2_TYPE_IS_OUTPUT(cr->type) &&
  514. (ctx->gsc_ctrls.rotate->val == 90 ||
  515. ctx->gsc_ctrls.rotate->val == 270))
  516. gsc_check_crop_change(tmp_h, tmp_w,
  517. &cr->c.width, &cr->c.height);
  518. else
  519. gsc_check_crop_change(tmp_w, tmp_h,
  520. &cr->c.width, &cr->c.height);
  521. /* adjust left/top if cropping rectangle is out of bounds */
  522. /* Need to add code to algin left value with 2's multiple */
  523. if (cr->c.left + tmp_w > max_w)
  524. cr->c.left = max_w - tmp_w;
  525. if (cr->c.top + tmp_h > max_h)
  526. cr->c.top = max_h - tmp_h;
  527. if ((is_yuv420(f->fmt->color) || is_yuv422(f->fmt->color)) &&
  528. cr->c.left & 1)
  529. cr->c.left -= 1;
  530. pr_debug("Aligned l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
  531. cr->c.left, cr->c.top, cr->c.width, cr->c.height, max_w, max_h);
  532. return 0;
  533. }
  534. int gsc_check_scaler_ratio(struct gsc_variant *var, int sw, int sh, int dw,
  535. int dh, int rot, int out_path)
  536. {
  537. int tmp_w, tmp_h, sc_down_max;
  538. if (out_path == GSC_DMA)
  539. sc_down_max = var->sc_down_max;
  540. else
  541. sc_down_max = var->local_sc_down;
  542. if (rot == 90 || rot == 270) {
  543. tmp_w = dh;
  544. tmp_h = dw;
  545. } else {
  546. tmp_w = dw;
  547. tmp_h = dh;
  548. }
  549. if ((sw / tmp_w) > sc_down_max ||
  550. (sh / tmp_h) > sc_down_max ||
  551. (tmp_w / sw) > var->sc_up_max ||
  552. (tmp_h / sh) > var->sc_up_max)
  553. return -EINVAL;
  554. return 0;
  555. }
  556. int gsc_set_scaler_info(struct gsc_ctx *ctx)
  557. {
  558. struct gsc_scaler *sc = &ctx->scaler;
  559. struct gsc_frame *s_frame = &ctx->s_frame;
  560. struct gsc_frame *d_frame = &ctx->d_frame;
  561. struct gsc_variant *variant = ctx->gsc_dev->variant;
  562. struct device *dev = &ctx->gsc_dev->pdev->dev;
  563. int tx, ty;
  564. int ret;
  565. ret = gsc_check_scaler_ratio(variant, s_frame->crop.width,
  566. s_frame->crop.height, d_frame->crop.width, d_frame->crop.height,
  567. ctx->gsc_ctrls.rotate->val, ctx->out_path);
  568. if (ret) {
  569. pr_err("out of scaler range");
  570. return ret;
  571. }
  572. if (ctx->gsc_ctrls.rotate->val == 90 ||
  573. ctx->gsc_ctrls.rotate->val == 270) {
  574. ty = d_frame->crop.width;
  575. tx = d_frame->crop.height;
  576. } else {
  577. tx = d_frame->crop.width;
  578. ty = d_frame->crop.height;
  579. }
  580. if (tx <= 0 || ty <= 0) {
  581. dev_err(dev, "Invalid target size: %dx%d", tx, ty);
  582. return -EINVAL;
  583. }
  584. ret = gsc_cal_prescaler_ratio(variant, s_frame->crop.width,
  585. tx, &sc->pre_hratio);
  586. if (ret) {
  587. pr_err("Horizontal scale ratio is out of range");
  588. return ret;
  589. }
  590. ret = gsc_cal_prescaler_ratio(variant, s_frame->crop.height,
  591. ty, &sc->pre_vratio);
  592. if (ret) {
  593. pr_err("Vertical scale ratio is out of range");
  594. return ret;
  595. }
  596. gsc_check_src_scale_info(variant, s_frame, &sc->pre_hratio,
  597. tx, ty, &sc->pre_vratio);
  598. gsc_get_prescaler_shfactor(sc->pre_hratio, sc->pre_vratio,
  599. &sc->pre_shfactor);
  600. sc->main_hratio = (s_frame->crop.width << 16) / tx;
  601. sc->main_vratio = (s_frame->crop.height << 16) / ty;
  602. pr_debug("scaler input/output size : sx = %d, sy = %d, tx = %d, ty = %d",
  603. s_frame->crop.width, s_frame->crop.height, tx, ty);
  604. pr_debug("scaler ratio info : pre_shfactor : %d, pre_h : %d",
  605. sc->pre_shfactor, sc->pre_hratio);
  606. pr_debug("pre_v :%d, main_h : %d, main_v : %d",
  607. sc->pre_vratio, sc->main_hratio, sc->main_vratio);
  608. return 0;
  609. }
  610. static int __gsc_s_ctrl(struct gsc_ctx *ctx, struct v4l2_ctrl *ctrl)
  611. {
  612. struct gsc_dev *gsc = ctx->gsc_dev;
  613. struct gsc_variant *variant = gsc->variant;
  614. unsigned int flags = GSC_DST_FMT | GSC_SRC_FMT;
  615. int ret = 0;
  616. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  617. return 0;
  618. switch (ctrl->id) {
  619. case V4L2_CID_HFLIP:
  620. ctx->hflip = ctrl->val;
  621. break;
  622. case V4L2_CID_VFLIP:
  623. ctx->vflip = ctrl->val;
  624. break;
  625. case V4L2_CID_ROTATE:
  626. if ((ctx->state & flags) == flags) {
  627. ret = gsc_check_scaler_ratio(variant,
  628. ctx->s_frame.crop.width,
  629. ctx->s_frame.crop.height,
  630. ctx->d_frame.crop.width,
  631. ctx->d_frame.crop.height,
  632. ctx->gsc_ctrls.rotate->val,
  633. ctx->out_path);
  634. if (ret)
  635. return -EINVAL;
  636. }
  637. ctx->rotation = ctrl->val;
  638. break;
  639. case V4L2_CID_ALPHA_COMPONENT:
  640. ctx->d_frame.alpha = ctrl->val;
  641. break;
  642. }
  643. ctx->state |= GSC_PARAMS;
  644. return 0;
  645. }
  646. static int gsc_s_ctrl(struct v4l2_ctrl *ctrl)
  647. {
  648. struct gsc_ctx *ctx = ctrl_to_ctx(ctrl);
  649. unsigned long flags;
  650. int ret;
  651. spin_lock_irqsave(&ctx->gsc_dev->slock, flags);
  652. ret = __gsc_s_ctrl(ctx, ctrl);
  653. spin_unlock_irqrestore(&ctx->gsc_dev->slock, flags);
  654. return ret;
  655. }
  656. static const struct v4l2_ctrl_ops gsc_ctrl_ops = {
  657. .s_ctrl = gsc_s_ctrl,
  658. };
  659. int gsc_ctrls_create(struct gsc_ctx *ctx)
  660. {
  661. if (ctx->ctrls_rdy) {
  662. pr_err("Control handler of this context was created already");
  663. return 0;
  664. }
  665. v4l2_ctrl_handler_init(&ctx->ctrl_handler, GSC_MAX_CTRL_NUM);
  666. ctx->gsc_ctrls.rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  667. &gsc_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
  668. ctx->gsc_ctrls.hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  669. &gsc_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  670. ctx->gsc_ctrls.vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  671. &gsc_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  672. ctx->gsc_ctrls.global_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  673. &gsc_ctrl_ops, V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 0);
  674. ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
  675. if (ctx->ctrl_handler.error) {
  676. int err = ctx->ctrl_handler.error;
  677. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  678. pr_err("Failed to create G-Scaler control handlers");
  679. return err;
  680. }
  681. return 0;
  682. }
  683. void gsc_ctrls_delete(struct gsc_ctx *ctx)
  684. {
  685. if (ctx->ctrls_rdy) {
  686. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  687. ctx->ctrls_rdy = false;
  688. }
  689. }
  690. /* The color format (num_comp, num_planes) must be already configured. */
  691. int gsc_prepare_addr(struct gsc_ctx *ctx, struct vb2_buffer *vb,
  692. struct gsc_frame *frame, struct gsc_addr *addr)
  693. {
  694. int ret = 0;
  695. u32 pix_size;
  696. if ((vb == NULL) || (frame == NULL))
  697. return -EINVAL;
  698. pix_size = frame->f_width * frame->f_height;
  699. pr_debug("num_planes= %d, num_comp= %d, pix_size= %d",
  700. frame->fmt->num_planes, frame->fmt->num_comp, pix_size);
  701. addr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
  702. if (frame->fmt->num_planes == 1) {
  703. switch (frame->fmt->num_comp) {
  704. case 1:
  705. addr->cb = 0;
  706. addr->cr = 0;
  707. break;
  708. case 2:
  709. /* decompose Y into Y/Cb */
  710. addr->cb = (dma_addr_t)(addr->y + pix_size);
  711. addr->cr = 0;
  712. break;
  713. case 3:
  714. /* decompose Y into Y/Cb/Cr */
  715. addr->cb = (dma_addr_t)(addr->y + pix_size);
  716. if (GSC_YUV420 == frame->fmt->color)
  717. addr->cr = (dma_addr_t)(addr->cb
  718. + (pix_size >> 2));
  719. else /* 422 */
  720. addr->cr = (dma_addr_t)(addr->cb
  721. + (pix_size >> 1));
  722. break;
  723. default:
  724. pr_err("Invalid the number of color planes");
  725. return -EINVAL;
  726. }
  727. } else {
  728. if (frame->fmt->num_planes >= 2)
  729. addr->cb = vb2_dma_contig_plane_dma_addr(vb, 1);
  730. if (frame->fmt->num_planes == 3)
  731. addr->cr = vb2_dma_contig_plane_dma_addr(vb, 2);
  732. }
  733. if ((frame->fmt->pixelformat == V4L2_PIX_FMT_VYUY) ||
  734. (frame->fmt->pixelformat == V4L2_PIX_FMT_YVYU) ||
  735. (frame->fmt->pixelformat == V4L2_PIX_FMT_NV61) ||
  736. (frame->fmt->pixelformat == V4L2_PIX_FMT_YVU420) ||
  737. (frame->fmt->pixelformat == V4L2_PIX_FMT_NV21) ||
  738. (frame->fmt->pixelformat == V4L2_PIX_FMT_YVU420M))
  739. swap(addr->cb, addr->cr);
  740. pr_debug("ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
  741. addr->y, addr->cb, addr->cr, ret);
  742. return ret;
  743. }
  744. static irqreturn_t gsc_irq_handler(int irq, void *priv)
  745. {
  746. struct gsc_dev *gsc = priv;
  747. struct gsc_ctx *ctx;
  748. int gsc_irq;
  749. gsc_irq = gsc_hw_get_irq_status(gsc);
  750. gsc_hw_clear_irq(gsc, gsc_irq);
  751. if (gsc_irq == GSC_IRQ_OVERRUN) {
  752. pr_err("Local path input over-run interrupt has occurred!\n");
  753. return IRQ_HANDLED;
  754. }
  755. spin_lock(&gsc->slock);
  756. if (test_and_clear_bit(ST_M2M_PEND, &gsc->state)) {
  757. gsc_hw_enable_control(gsc, false);
  758. if (test_and_clear_bit(ST_M2M_SUSPENDING, &gsc->state)) {
  759. set_bit(ST_M2M_SUSPENDED, &gsc->state);
  760. wake_up(&gsc->irq_queue);
  761. goto isr_unlock;
  762. }
  763. ctx = v4l2_m2m_get_curr_priv(gsc->m2m.m2m_dev);
  764. if (!ctx || !ctx->m2m_ctx)
  765. goto isr_unlock;
  766. spin_unlock(&gsc->slock);
  767. gsc_m2m_job_finish(ctx, VB2_BUF_STATE_DONE);
  768. /* wake_up job_abort, stop_streaming */
  769. if (ctx->state & GSC_CTX_STOP_REQ) {
  770. ctx->state &= ~GSC_CTX_STOP_REQ;
  771. wake_up(&gsc->irq_queue);
  772. }
  773. return IRQ_HANDLED;
  774. }
  775. isr_unlock:
  776. spin_unlock(&gsc->slock);
  777. return IRQ_HANDLED;
  778. }
  779. static struct gsc_pix_max gsc_v_100_max = {
  780. .org_scaler_bypass_w = 8192,
  781. .org_scaler_bypass_h = 8192,
  782. .org_scaler_input_w = 4800,
  783. .org_scaler_input_h = 3344,
  784. .real_rot_dis_w = 4800,
  785. .real_rot_dis_h = 3344,
  786. .real_rot_en_w = 2047,
  787. .real_rot_en_h = 2047,
  788. .target_rot_dis_w = 4800,
  789. .target_rot_dis_h = 3344,
  790. .target_rot_en_w = 2016,
  791. .target_rot_en_h = 2016,
  792. };
  793. static struct gsc_pix_min gsc_v_100_min = {
  794. .org_w = 64,
  795. .org_h = 32,
  796. .real_w = 64,
  797. .real_h = 32,
  798. .target_rot_dis_w = 64,
  799. .target_rot_dis_h = 32,
  800. .target_rot_en_w = 32,
  801. .target_rot_en_h = 16,
  802. };
  803. static struct gsc_pix_align gsc_v_100_align = {
  804. .org_h = 16,
  805. .org_w = 16, /* yuv420 : 16, others : 8 */
  806. .offset_h = 2, /* yuv420/422 : 2, others : 1 */
  807. .real_w = 16, /* yuv420/422 : 4~16, others : 2~8 */
  808. .real_h = 16, /* yuv420 : 4~16, others : 1 */
  809. .target_w = 2, /* yuv420/422 : 2, others : 1 */
  810. .target_h = 2, /* yuv420 : 2, others : 1 */
  811. };
  812. static struct gsc_variant gsc_v_100_variant = {
  813. .pix_max = &gsc_v_100_max,
  814. .pix_min = &gsc_v_100_min,
  815. .pix_align = &gsc_v_100_align,
  816. .in_buf_cnt = 32,
  817. .out_buf_cnt = 32,
  818. .sc_up_max = 8,
  819. .sc_down_max = 16,
  820. .poly_sc_down_max = 4,
  821. .pre_sc_down_max = 4,
  822. .local_sc_down = 2,
  823. };
  824. static struct gsc_driverdata gsc_v_100_drvdata = {
  825. .variant = {
  826. [0] = &gsc_v_100_variant,
  827. [1] = &gsc_v_100_variant,
  828. [2] = &gsc_v_100_variant,
  829. [3] = &gsc_v_100_variant,
  830. },
  831. .num_entities = 4,
  832. .lclk_frequency = 266000000UL,
  833. };
  834. static struct platform_device_id gsc_driver_ids[] = {
  835. {
  836. .name = "exynos-gsc",
  837. .driver_data = (unsigned long)&gsc_v_100_drvdata,
  838. },
  839. {},
  840. };
  841. MODULE_DEVICE_TABLE(platform, gsc_driver_ids);
  842. static const struct of_device_id exynos_gsc_match[] = {
  843. {
  844. .compatible = "samsung,exynos5-gsc",
  845. .data = &gsc_v_100_drvdata,
  846. },
  847. {},
  848. };
  849. MODULE_DEVICE_TABLE(of, exynos_gsc_match);
  850. static void *gsc_get_drv_data(struct platform_device *pdev)
  851. {
  852. struct gsc_driverdata *driver_data = NULL;
  853. if (pdev->dev.of_node) {
  854. const struct of_device_id *match;
  855. match = of_match_node(of_match_ptr(exynos_gsc_match),
  856. pdev->dev.of_node);
  857. if (match)
  858. driver_data = (struct gsc_driverdata *)match->data;
  859. } else {
  860. driver_data = (struct gsc_driverdata *)
  861. platform_get_device_id(pdev)->driver_data;
  862. }
  863. return driver_data;
  864. }
  865. static void gsc_clk_put(struct gsc_dev *gsc)
  866. {
  867. if (!IS_ERR(gsc->clock))
  868. clk_unprepare(gsc->clock);
  869. }
  870. static int gsc_clk_get(struct gsc_dev *gsc)
  871. {
  872. int ret;
  873. dev_dbg(&gsc->pdev->dev, "gsc_clk_get Called\n");
  874. gsc->clock = devm_clk_get(&gsc->pdev->dev, GSC_CLOCK_GATE_NAME);
  875. if (IS_ERR(gsc->clock)) {
  876. dev_err(&gsc->pdev->dev, "failed to get clock~~~: %s\n",
  877. GSC_CLOCK_GATE_NAME);
  878. return PTR_ERR(gsc->clock);
  879. }
  880. ret = clk_prepare(gsc->clock);
  881. if (ret < 0) {
  882. dev_err(&gsc->pdev->dev, "clock prepare failed for clock: %s\n",
  883. GSC_CLOCK_GATE_NAME);
  884. gsc->clock = ERR_PTR(-EINVAL);
  885. return ret;
  886. }
  887. return 0;
  888. }
  889. static int gsc_m2m_suspend(struct gsc_dev *gsc)
  890. {
  891. unsigned long flags;
  892. int timeout;
  893. spin_lock_irqsave(&gsc->slock, flags);
  894. if (!gsc_m2m_pending(gsc)) {
  895. spin_unlock_irqrestore(&gsc->slock, flags);
  896. return 0;
  897. }
  898. clear_bit(ST_M2M_SUSPENDED, &gsc->state);
  899. set_bit(ST_M2M_SUSPENDING, &gsc->state);
  900. spin_unlock_irqrestore(&gsc->slock, flags);
  901. timeout = wait_event_timeout(gsc->irq_queue,
  902. test_bit(ST_M2M_SUSPENDED, &gsc->state),
  903. GSC_SHUTDOWN_TIMEOUT);
  904. clear_bit(ST_M2M_SUSPENDING, &gsc->state);
  905. return timeout == 0 ? -EAGAIN : 0;
  906. }
  907. static int gsc_m2m_resume(struct gsc_dev *gsc)
  908. {
  909. struct gsc_ctx *ctx;
  910. unsigned long flags;
  911. spin_lock_irqsave(&gsc->slock, flags);
  912. /* Clear for full H/W setup in first run after resume */
  913. ctx = gsc->m2m.ctx;
  914. gsc->m2m.ctx = NULL;
  915. spin_unlock_irqrestore(&gsc->slock, flags);
  916. if (test_and_clear_bit(ST_M2M_SUSPENDED, &gsc->state))
  917. gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  918. return 0;
  919. }
  920. static int gsc_probe(struct platform_device *pdev)
  921. {
  922. struct gsc_dev *gsc;
  923. struct resource *res;
  924. struct gsc_driverdata *drv_data = gsc_get_drv_data(pdev);
  925. struct device *dev = &pdev->dev;
  926. int ret = 0;
  927. gsc = devm_kzalloc(dev, sizeof(struct gsc_dev), GFP_KERNEL);
  928. if (!gsc)
  929. return -ENOMEM;
  930. if (dev->of_node)
  931. gsc->id = of_alias_get_id(pdev->dev.of_node, "gsc");
  932. else
  933. gsc->id = pdev->id;
  934. if (gsc->id < 0 || gsc->id >= drv_data->num_entities) {
  935. dev_err(dev, "Invalid platform device id: %d\n", gsc->id);
  936. return -EINVAL;
  937. }
  938. gsc->variant = drv_data->variant[gsc->id];
  939. gsc->pdev = pdev;
  940. gsc->pdata = dev->platform_data;
  941. init_waitqueue_head(&gsc->irq_queue);
  942. spin_lock_init(&gsc->slock);
  943. mutex_init(&gsc->lock);
  944. gsc->clock = ERR_PTR(-EINVAL);
  945. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  946. gsc->regs = devm_ioremap_resource(dev, res);
  947. if (IS_ERR(gsc->regs))
  948. return PTR_ERR(gsc->regs);
  949. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  950. if (!res) {
  951. dev_err(dev, "failed to get IRQ resource\n");
  952. return -ENXIO;
  953. }
  954. ret = gsc_clk_get(gsc);
  955. if (ret)
  956. return ret;
  957. ret = devm_request_irq(dev, res->start, gsc_irq_handler,
  958. 0, pdev->name, gsc);
  959. if (ret) {
  960. dev_err(dev, "failed to install irq (%d)\n", ret);
  961. goto err_clk;
  962. }
  963. ret = gsc_register_m2m_device(gsc);
  964. if (ret)
  965. goto err_clk;
  966. platform_set_drvdata(pdev, gsc);
  967. pm_runtime_enable(dev);
  968. ret = pm_runtime_get_sync(&pdev->dev);
  969. if (ret < 0)
  970. goto err_m2m;
  971. /* Initialize continious memory allocator */
  972. gsc->alloc_ctx = vb2_dma_contig_init_ctx(dev);
  973. if (IS_ERR(gsc->alloc_ctx)) {
  974. ret = PTR_ERR(gsc->alloc_ctx);
  975. goto err_pm;
  976. }
  977. dev_dbg(dev, "gsc-%d registered successfully\n", gsc->id);
  978. pm_runtime_put(dev);
  979. return 0;
  980. err_pm:
  981. pm_runtime_put(dev);
  982. err_m2m:
  983. gsc_unregister_m2m_device(gsc);
  984. err_clk:
  985. gsc_clk_put(gsc);
  986. return ret;
  987. }
  988. static int gsc_remove(struct platform_device *pdev)
  989. {
  990. struct gsc_dev *gsc = platform_get_drvdata(pdev);
  991. gsc_unregister_m2m_device(gsc);
  992. vb2_dma_contig_cleanup_ctx(gsc->alloc_ctx);
  993. pm_runtime_disable(&pdev->dev);
  994. gsc_clk_put(gsc);
  995. dev_dbg(&pdev->dev, "%s driver unloaded\n", pdev->name);
  996. return 0;
  997. }
  998. static int gsc_runtime_resume(struct device *dev)
  999. {
  1000. struct gsc_dev *gsc = dev_get_drvdata(dev);
  1001. int ret = 0;
  1002. pr_debug("gsc%d: state: 0x%lx", gsc->id, gsc->state);
  1003. ret = clk_enable(gsc->clock);
  1004. if (ret)
  1005. return ret;
  1006. gsc_hw_set_sw_reset(gsc);
  1007. gsc_wait_reset(gsc);
  1008. return gsc_m2m_resume(gsc);
  1009. }
  1010. static int gsc_runtime_suspend(struct device *dev)
  1011. {
  1012. struct gsc_dev *gsc = dev_get_drvdata(dev);
  1013. int ret = 0;
  1014. ret = gsc_m2m_suspend(gsc);
  1015. if (!ret)
  1016. clk_disable(gsc->clock);
  1017. pr_debug("gsc%d: state: 0x%lx", gsc->id, gsc->state);
  1018. return ret;
  1019. }
  1020. static int gsc_resume(struct device *dev)
  1021. {
  1022. struct gsc_dev *gsc = dev_get_drvdata(dev);
  1023. unsigned long flags;
  1024. pr_debug("gsc%d: state: 0x%lx", gsc->id, gsc->state);
  1025. /* Do not resume if the device was idle before system suspend */
  1026. spin_lock_irqsave(&gsc->slock, flags);
  1027. if (!test_and_clear_bit(ST_SUSPEND, &gsc->state) ||
  1028. !gsc_m2m_opened(gsc)) {
  1029. spin_unlock_irqrestore(&gsc->slock, flags);
  1030. return 0;
  1031. }
  1032. gsc_hw_set_sw_reset(gsc);
  1033. gsc_wait_reset(gsc);
  1034. spin_unlock_irqrestore(&gsc->slock, flags);
  1035. return gsc_m2m_resume(gsc);
  1036. }
  1037. static int gsc_suspend(struct device *dev)
  1038. {
  1039. struct gsc_dev *gsc = dev_get_drvdata(dev);
  1040. pr_debug("gsc%d: state: 0x%lx", gsc->id, gsc->state);
  1041. if (test_and_set_bit(ST_SUSPEND, &gsc->state))
  1042. return 0;
  1043. return gsc_m2m_suspend(gsc);
  1044. }
  1045. static const struct dev_pm_ops gsc_pm_ops = {
  1046. .suspend = gsc_suspend,
  1047. .resume = gsc_resume,
  1048. .runtime_suspend = gsc_runtime_suspend,
  1049. .runtime_resume = gsc_runtime_resume,
  1050. };
  1051. static struct platform_driver gsc_driver = {
  1052. .probe = gsc_probe,
  1053. .remove = gsc_remove,
  1054. .id_table = gsc_driver_ids,
  1055. .driver = {
  1056. .name = GSC_MODULE_NAME,
  1057. .owner = THIS_MODULE,
  1058. .pm = &gsc_pm_ops,
  1059. .of_match_table = exynos_gsc_match,
  1060. }
  1061. };
  1062. module_platform_driver(gsc_driver);
  1063. MODULE_AUTHOR("Hyunwong Kim <khw0178.kim@samsung.com>");
  1064. MODULE_DESCRIPTION("Samsung EXYNOS5 Soc series G-Scaler driver");
  1065. MODULE_LICENSE("GPL");