gsc-core.c 30 KB

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