gsc-core.c 33 KB

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