exynos_drm_rotator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * YoungJun Cho <yj44.cho@samsung.com>
  5. * Eunchul Kim <chulspro.kim@samsung.com>
  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 version 2 as
  9. * published by the Free Software Foundationr
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/err.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/clk.h>
  17. #include <linux/pm_runtime.h>
  18. #include <drm/drmP.h>
  19. #include <drm/exynos_drm.h>
  20. #include "regs-rotator.h"
  21. #include "exynos_drm_drv.h"
  22. #include "exynos_drm_ipp.h"
  23. /*
  24. * Rotator supports image crop/rotator and input/output DMA operations.
  25. * input DMA reads image data from the memory.
  26. * output DMA writes image data to memory.
  27. *
  28. * M2M operation : supports crop/scale/rotation/csc so on.
  29. * Memory ----> Rotator H/W ----> Memory.
  30. */
  31. /*
  32. * TODO
  33. * 1. check suspend/resume api if needed.
  34. * 2. need to check use case platform_device_id.
  35. * 3. check src/dst size with, height.
  36. * 4. need to add supported list in prop_list.
  37. */
  38. #define get_rot_context(dev) platform_get_drvdata(to_platform_device(dev))
  39. #define get_ctx_from_ippdrv(ippdrv) container_of(ippdrv,\
  40. struct rot_context, ippdrv);
  41. #define rot_read(offset) readl(rot->regs + (offset))
  42. #define rot_write(cfg, offset) writel(cfg, rot->regs + (offset))
  43. enum rot_irq_status {
  44. ROT_IRQ_STATUS_COMPLETE = 8,
  45. ROT_IRQ_STATUS_ILLEGAL = 9,
  46. };
  47. /*
  48. * A structure of limitation.
  49. *
  50. * @min_w: minimum width.
  51. * @min_h: minimum height.
  52. * @max_w: maximum width.
  53. * @max_h: maximum height.
  54. * @align: align size.
  55. */
  56. struct rot_limit {
  57. u32 min_w;
  58. u32 min_h;
  59. u32 max_w;
  60. u32 max_h;
  61. u32 align;
  62. };
  63. /*
  64. * A structure of limitation table.
  65. *
  66. * @ycbcr420_2p: case of YUV.
  67. * @rgb888: case of RGB.
  68. */
  69. struct rot_limit_table {
  70. struct rot_limit ycbcr420_2p;
  71. struct rot_limit rgb888;
  72. };
  73. /*
  74. * A structure of rotator context.
  75. * @ippdrv: prepare initialization using ippdrv.
  76. * @regs_res: register resources.
  77. * @regs: memory mapped io registers.
  78. * @clock: rotator gate clock.
  79. * @limit_tbl: limitation of rotator.
  80. * @irq: irq number.
  81. * @cur_buf_id: current operation buffer id.
  82. * @suspended: suspended state.
  83. */
  84. struct rot_context {
  85. struct exynos_drm_ippdrv ippdrv;
  86. struct resource *regs_res;
  87. void __iomem *regs;
  88. struct clk *clock;
  89. struct rot_limit_table *limit_tbl;
  90. int irq;
  91. int cur_buf_id[EXYNOS_DRM_OPS_MAX];
  92. bool suspended;
  93. };
  94. static void rotator_reg_set_irq(struct rot_context *rot, bool enable)
  95. {
  96. u32 val = rot_read(ROT_CONFIG);
  97. if (enable == true)
  98. val |= ROT_CONFIG_IRQ;
  99. else
  100. val &= ~ROT_CONFIG_IRQ;
  101. rot_write(val, ROT_CONFIG);
  102. }
  103. static u32 rotator_reg_get_fmt(struct rot_context *rot)
  104. {
  105. u32 val = rot_read(ROT_CONTROL);
  106. val &= ROT_CONTROL_FMT_MASK;
  107. return val;
  108. }
  109. static enum rot_irq_status rotator_reg_get_irq_status(struct rot_context *rot)
  110. {
  111. u32 val = rot_read(ROT_STATUS);
  112. val = ROT_STATUS_IRQ(val);
  113. if (val == ROT_STATUS_IRQ_VAL_COMPLETE)
  114. return ROT_IRQ_STATUS_COMPLETE;
  115. return ROT_IRQ_STATUS_ILLEGAL;
  116. }
  117. static irqreturn_t rotator_irq_handler(int irq, void *arg)
  118. {
  119. struct rot_context *rot = arg;
  120. struct exynos_drm_ippdrv *ippdrv = &rot->ippdrv;
  121. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  122. struct drm_exynos_ipp_event_work *event_work = c_node->event_work;
  123. enum rot_irq_status irq_status;
  124. u32 val;
  125. /* Get execution result */
  126. irq_status = rotator_reg_get_irq_status(rot);
  127. /* clear status */
  128. val = rot_read(ROT_STATUS);
  129. val |= ROT_STATUS_IRQ_PENDING((u32)irq_status);
  130. rot_write(val, ROT_STATUS);
  131. if (irq_status == ROT_IRQ_STATUS_COMPLETE) {
  132. event_work->ippdrv = ippdrv;
  133. event_work->buf_id[EXYNOS_DRM_OPS_DST] =
  134. rot->cur_buf_id[EXYNOS_DRM_OPS_DST];
  135. queue_work(ippdrv->event_workq, &event_work->work);
  136. } else {
  137. DRM_ERROR("the SFR is set illegally\n");
  138. }
  139. return IRQ_HANDLED;
  140. }
  141. static void rotator_align_size(struct rot_context *rot, u32 fmt, u32 *hsize,
  142. u32 *vsize)
  143. {
  144. struct rot_limit_table *limit_tbl = rot->limit_tbl;
  145. struct rot_limit *limit;
  146. u32 mask, val;
  147. /* Get size limit */
  148. if (fmt == ROT_CONTROL_FMT_RGB888)
  149. limit = &limit_tbl->rgb888;
  150. else
  151. limit = &limit_tbl->ycbcr420_2p;
  152. /* Get mask for rounding to nearest aligned val */
  153. mask = ~((1 << limit->align) - 1);
  154. /* Set aligned width */
  155. val = ROT_ALIGN(*hsize, limit->align, mask);
  156. if (val < limit->min_w)
  157. *hsize = ROT_MIN(limit->min_w, mask);
  158. else if (val > limit->max_w)
  159. *hsize = ROT_MAX(limit->max_w, mask);
  160. else
  161. *hsize = val;
  162. /* Set aligned height */
  163. val = ROT_ALIGN(*vsize, limit->align, mask);
  164. if (val < limit->min_h)
  165. *vsize = ROT_MIN(limit->min_h, mask);
  166. else if (val > limit->max_h)
  167. *vsize = ROT_MAX(limit->max_h, mask);
  168. else
  169. *vsize = val;
  170. }
  171. static int rotator_src_set_fmt(struct device *dev, u32 fmt)
  172. {
  173. struct rot_context *rot = dev_get_drvdata(dev);
  174. u32 val;
  175. val = rot_read(ROT_CONTROL);
  176. val &= ~ROT_CONTROL_FMT_MASK;
  177. switch (fmt) {
  178. case DRM_FORMAT_NV12:
  179. val |= ROT_CONTROL_FMT_YCBCR420_2P;
  180. break;
  181. case DRM_FORMAT_XRGB8888:
  182. val |= ROT_CONTROL_FMT_RGB888;
  183. break;
  184. default:
  185. DRM_ERROR("invalid image format\n");
  186. return -EINVAL;
  187. }
  188. rot_write(val, ROT_CONTROL);
  189. return 0;
  190. }
  191. static inline bool rotator_check_reg_fmt(u32 fmt)
  192. {
  193. if ((fmt == ROT_CONTROL_FMT_YCBCR420_2P) ||
  194. (fmt == ROT_CONTROL_FMT_RGB888))
  195. return true;
  196. return false;
  197. }
  198. static int rotator_src_set_size(struct device *dev, int swap,
  199. struct drm_exynos_pos *pos,
  200. struct drm_exynos_sz *sz)
  201. {
  202. struct rot_context *rot = dev_get_drvdata(dev);
  203. u32 fmt, hsize, vsize;
  204. u32 val;
  205. /* Get format */
  206. fmt = rotator_reg_get_fmt(rot);
  207. if (!rotator_check_reg_fmt(fmt)) {
  208. DRM_ERROR("invalid format.\n");
  209. return -EINVAL;
  210. }
  211. /* Align buffer size */
  212. hsize = sz->hsize;
  213. vsize = sz->vsize;
  214. rotator_align_size(rot, fmt, &hsize, &vsize);
  215. /* Set buffer size configuration */
  216. val = ROT_SET_BUF_SIZE_H(vsize) | ROT_SET_BUF_SIZE_W(hsize);
  217. rot_write(val, ROT_SRC_BUF_SIZE);
  218. /* Set crop image position configuration */
  219. val = ROT_CROP_POS_Y(pos->y) | ROT_CROP_POS_X(pos->x);
  220. rot_write(val, ROT_SRC_CROP_POS);
  221. val = ROT_SRC_CROP_SIZE_H(pos->h) | ROT_SRC_CROP_SIZE_W(pos->w);
  222. rot_write(val, ROT_SRC_CROP_SIZE);
  223. return 0;
  224. }
  225. static int rotator_src_set_addr(struct device *dev,
  226. struct drm_exynos_ipp_buf_info *buf_info,
  227. u32 buf_id, enum drm_exynos_ipp_buf_type buf_type)
  228. {
  229. struct rot_context *rot = dev_get_drvdata(dev);
  230. dma_addr_t addr[EXYNOS_DRM_PLANAR_MAX];
  231. u32 val, fmt, hsize, vsize;
  232. int i;
  233. /* Set current buf_id */
  234. rot->cur_buf_id[EXYNOS_DRM_OPS_SRC] = buf_id;
  235. switch (buf_type) {
  236. case IPP_BUF_ENQUEUE:
  237. /* Set address configuration */
  238. for_each_ipp_planar(i)
  239. addr[i] = buf_info->base[i];
  240. /* Get format */
  241. fmt = rotator_reg_get_fmt(rot);
  242. if (!rotator_check_reg_fmt(fmt)) {
  243. DRM_ERROR("invalid format.\n");
  244. return -EINVAL;
  245. }
  246. /* Re-set cb planar for NV12 format */
  247. if ((fmt == ROT_CONTROL_FMT_YCBCR420_2P) &&
  248. !addr[EXYNOS_DRM_PLANAR_CB]) {
  249. val = rot_read(ROT_SRC_BUF_SIZE);
  250. hsize = ROT_GET_BUF_SIZE_W(val);
  251. vsize = ROT_GET_BUF_SIZE_H(val);
  252. /* Set cb planar */
  253. addr[EXYNOS_DRM_PLANAR_CB] =
  254. addr[EXYNOS_DRM_PLANAR_Y] + hsize * vsize;
  255. }
  256. for_each_ipp_planar(i)
  257. rot_write(addr[i], ROT_SRC_BUF_ADDR(i));
  258. break;
  259. case IPP_BUF_DEQUEUE:
  260. for_each_ipp_planar(i)
  261. rot_write(0x0, ROT_SRC_BUF_ADDR(i));
  262. break;
  263. default:
  264. /* Nothing to do */
  265. break;
  266. }
  267. return 0;
  268. }
  269. static int rotator_dst_set_transf(struct device *dev,
  270. enum drm_exynos_degree degree,
  271. enum drm_exynos_flip flip, bool *swap)
  272. {
  273. struct rot_context *rot = dev_get_drvdata(dev);
  274. u32 val;
  275. /* Set transform configuration */
  276. val = rot_read(ROT_CONTROL);
  277. val &= ~ROT_CONTROL_FLIP_MASK;
  278. switch (flip) {
  279. case EXYNOS_DRM_FLIP_VERTICAL:
  280. val |= ROT_CONTROL_FLIP_VERTICAL;
  281. break;
  282. case EXYNOS_DRM_FLIP_HORIZONTAL:
  283. val |= ROT_CONTROL_FLIP_HORIZONTAL;
  284. break;
  285. default:
  286. /* Flip None */
  287. break;
  288. }
  289. val &= ~ROT_CONTROL_ROT_MASK;
  290. switch (degree) {
  291. case EXYNOS_DRM_DEGREE_90:
  292. val |= ROT_CONTROL_ROT_90;
  293. break;
  294. case EXYNOS_DRM_DEGREE_180:
  295. val |= ROT_CONTROL_ROT_180;
  296. break;
  297. case EXYNOS_DRM_DEGREE_270:
  298. val |= ROT_CONTROL_ROT_270;
  299. break;
  300. default:
  301. /* Rotation 0 Degree */
  302. break;
  303. }
  304. rot_write(val, ROT_CONTROL);
  305. /* Check degree for setting buffer size swap */
  306. if ((degree == EXYNOS_DRM_DEGREE_90) ||
  307. (degree == EXYNOS_DRM_DEGREE_270))
  308. *swap = true;
  309. else
  310. *swap = false;
  311. return 0;
  312. }
  313. static int rotator_dst_set_size(struct device *dev, int swap,
  314. struct drm_exynos_pos *pos,
  315. struct drm_exynos_sz *sz)
  316. {
  317. struct rot_context *rot = dev_get_drvdata(dev);
  318. u32 val, fmt, hsize, vsize;
  319. /* Get format */
  320. fmt = rotator_reg_get_fmt(rot);
  321. if (!rotator_check_reg_fmt(fmt)) {
  322. DRM_ERROR("invalid format.\n");
  323. return -EINVAL;
  324. }
  325. /* Align buffer size */
  326. hsize = sz->hsize;
  327. vsize = sz->vsize;
  328. rotator_align_size(rot, fmt, &hsize, &vsize);
  329. /* Set buffer size configuration */
  330. val = ROT_SET_BUF_SIZE_H(vsize) | ROT_SET_BUF_SIZE_W(hsize);
  331. rot_write(val, ROT_DST_BUF_SIZE);
  332. /* Set crop image position configuration */
  333. val = ROT_CROP_POS_Y(pos->y) | ROT_CROP_POS_X(pos->x);
  334. rot_write(val, ROT_DST_CROP_POS);
  335. return 0;
  336. }
  337. static int rotator_dst_set_addr(struct device *dev,
  338. struct drm_exynos_ipp_buf_info *buf_info,
  339. u32 buf_id, enum drm_exynos_ipp_buf_type buf_type)
  340. {
  341. struct rot_context *rot = dev_get_drvdata(dev);
  342. dma_addr_t addr[EXYNOS_DRM_PLANAR_MAX];
  343. u32 val, fmt, hsize, vsize;
  344. int i;
  345. /* Set current buf_id */
  346. rot->cur_buf_id[EXYNOS_DRM_OPS_DST] = buf_id;
  347. switch (buf_type) {
  348. case IPP_BUF_ENQUEUE:
  349. /* Set address configuration */
  350. for_each_ipp_planar(i)
  351. addr[i] = buf_info->base[i];
  352. /* Get format */
  353. fmt = rotator_reg_get_fmt(rot);
  354. if (!rotator_check_reg_fmt(fmt)) {
  355. DRM_ERROR("invalid format.\n");
  356. return -EINVAL;
  357. }
  358. /* Re-set cb planar for NV12 format */
  359. if ((fmt == ROT_CONTROL_FMT_YCBCR420_2P) &&
  360. !addr[EXYNOS_DRM_PLANAR_CB]) {
  361. /* Get buf size */
  362. val = rot_read(ROT_DST_BUF_SIZE);
  363. hsize = ROT_GET_BUF_SIZE_W(val);
  364. vsize = ROT_GET_BUF_SIZE_H(val);
  365. /* Set cb planar */
  366. addr[EXYNOS_DRM_PLANAR_CB] =
  367. addr[EXYNOS_DRM_PLANAR_Y] + hsize * vsize;
  368. }
  369. for_each_ipp_planar(i)
  370. rot_write(addr[i], ROT_DST_BUF_ADDR(i));
  371. break;
  372. case IPP_BUF_DEQUEUE:
  373. for_each_ipp_planar(i)
  374. rot_write(0x0, ROT_DST_BUF_ADDR(i));
  375. break;
  376. default:
  377. /* Nothing to do */
  378. break;
  379. }
  380. return 0;
  381. }
  382. static struct exynos_drm_ipp_ops rot_src_ops = {
  383. .set_fmt = rotator_src_set_fmt,
  384. .set_size = rotator_src_set_size,
  385. .set_addr = rotator_src_set_addr,
  386. };
  387. static struct exynos_drm_ipp_ops rot_dst_ops = {
  388. .set_transf = rotator_dst_set_transf,
  389. .set_size = rotator_dst_set_size,
  390. .set_addr = rotator_dst_set_addr,
  391. };
  392. static int rotator_init_prop_list(struct exynos_drm_ippdrv *ippdrv)
  393. {
  394. struct drm_exynos_ipp_prop_list *prop_list = &ippdrv->prop_list;
  395. prop_list->version = 1;
  396. prop_list->flip = (1 << EXYNOS_DRM_FLIP_VERTICAL) |
  397. (1 << EXYNOS_DRM_FLIP_HORIZONTAL);
  398. prop_list->degree = (1 << EXYNOS_DRM_DEGREE_0) |
  399. (1 << EXYNOS_DRM_DEGREE_90) |
  400. (1 << EXYNOS_DRM_DEGREE_180) |
  401. (1 << EXYNOS_DRM_DEGREE_270);
  402. prop_list->csc = 0;
  403. prop_list->crop = 0;
  404. prop_list->scale = 0;
  405. return 0;
  406. }
  407. static inline bool rotator_check_drm_fmt(u32 fmt)
  408. {
  409. switch (fmt) {
  410. case DRM_FORMAT_XRGB8888:
  411. case DRM_FORMAT_NV12:
  412. return true;
  413. default:
  414. DRM_DEBUG_KMS("not support format\n");
  415. return false;
  416. }
  417. }
  418. static inline bool rotator_check_drm_flip(enum drm_exynos_flip flip)
  419. {
  420. switch (flip) {
  421. case EXYNOS_DRM_FLIP_NONE:
  422. case EXYNOS_DRM_FLIP_VERTICAL:
  423. case EXYNOS_DRM_FLIP_HORIZONTAL:
  424. case EXYNOS_DRM_FLIP_BOTH:
  425. return true;
  426. default:
  427. DRM_DEBUG_KMS("invalid flip\n");
  428. return false;
  429. }
  430. }
  431. static int rotator_ippdrv_check_property(struct device *dev,
  432. struct drm_exynos_ipp_property *property)
  433. {
  434. struct drm_exynos_ipp_config *src_config =
  435. &property->config[EXYNOS_DRM_OPS_SRC];
  436. struct drm_exynos_ipp_config *dst_config =
  437. &property->config[EXYNOS_DRM_OPS_DST];
  438. struct drm_exynos_pos *src_pos = &src_config->pos;
  439. struct drm_exynos_pos *dst_pos = &dst_config->pos;
  440. struct drm_exynos_sz *src_sz = &src_config->sz;
  441. struct drm_exynos_sz *dst_sz = &dst_config->sz;
  442. bool swap = false;
  443. /* Check format configuration */
  444. if (src_config->fmt != dst_config->fmt) {
  445. DRM_DEBUG_KMS("not support csc feature\n");
  446. return -EINVAL;
  447. }
  448. if (!rotator_check_drm_fmt(dst_config->fmt)) {
  449. DRM_DEBUG_KMS("invalid format\n");
  450. return -EINVAL;
  451. }
  452. /* Check transform configuration */
  453. if (src_config->degree != EXYNOS_DRM_DEGREE_0) {
  454. DRM_DEBUG_KMS("not support source-side rotation\n");
  455. return -EINVAL;
  456. }
  457. switch (dst_config->degree) {
  458. case EXYNOS_DRM_DEGREE_90:
  459. case EXYNOS_DRM_DEGREE_270:
  460. swap = true;
  461. case EXYNOS_DRM_DEGREE_0:
  462. case EXYNOS_DRM_DEGREE_180:
  463. /* No problem */
  464. break;
  465. default:
  466. DRM_DEBUG_KMS("invalid degree\n");
  467. return -EINVAL;
  468. }
  469. if (src_config->flip != EXYNOS_DRM_FLIP_NONE) {
  470. DRM_DEBUG_KMS("not support source-side flip\n");
  471. return -EINVAL;
  472. }
  473. if (!rotator_check_drm_flip(dst_config->flip)) {
  474. DRM_DEBUG_KMS("invalid flip\n");
  475. return -EINVAL;
  476. }
  477. /* Check size configuration */
  478. if ((src_pos->x + src_pos->w > src_sz->hsize) ||
  479. (src_pos->y + src_pos->h > src_sz->vsize)) {
  480. DRM_DEBUG_KMS("out of source buffer bound\n");
  481. return -EINVAL;
  482. }
  483. if (swap) {
  484. if ((dst_pos->x + dst_pos->h > dst_sz->vsize) ||
  485. (dst_pos->y + dst_pos->w > dst_sz->hsize)) {
  486. DRM_DEBUG_KMS("out of destination buffer bound\n");
  487. return -EINVAL;
  488. }
  489. if ((src_pos->w != dst_pos->h) || (src_pos->h != dst_pos->w)) {
  490. DRM_DEBUG_KMS("not support scale feature\n");
  491. return -EINVAL;
  492. }
  493. } else {
  494. if ((dst_pos->x + dst_pos->w > dst_sz->hsize) ||
  495. (dst_pos->y + dst_pos->h > dst_sz->vsize)) {
  496. DRM_DEBUG_KMS("out of destination buffer bound\n");
  497. return -EINVAL;
  498. }
  499. if ((src_pos->w != dst_pos->w) || (src_pos->h != dst_pos->h)) {
  500. DRM_DEBUG_KMS("not support scale feature\n");
  501. return -EINVAL;
  502. }
  503. }
  504. return 0;
  505. }
  506. static int rotator_ippdrv_start(struct device *dev, enum drm_exynos_ipp_cmd cmd)
  507. {
  508. struct rot_context *rot = dev_get_drvdata(dev);
  509. u32 val;
  510. if (rot->suspended) {
  511. DRM_ERROR("suspended state\n");
  512. return -EPERM;
  513. }
  514. if (cmd != IPP_CMD_M2M) {
  515. DRM_ERROR("not support cmd: %d\n", cmd);
  516. return -EINVAL;
  517. }
  518. /* Set interrupt enable */
  519. rotator_reg_set_irq(rot, true);
  520. val = rot_read(ROT_CONTROL);
  521. val |= ROT_CONTROL_START;
  522. rot_write(val, ROT_CONTROL);
  523. return 0;
  524. }
  525. static struct rot_limit_table rot_limit_tbl_4210 = {
  526. .ycbcr420_2p = {
  527. .min_w = 32,
  528. .min_h = 32,
  529. .max_w = SZ_64K,
  530. .max_h = SZ_64K,
  531. .align = 3,
  532. },
  533. .rgb888 = {
  534. .min_w = 8,
  535. .min_h = 8,
  536. .max_w = SZ_16K,
  537. .max_h = SZ_16K,
  538. .align = 2,
  539. },
  540. };
  541. static struct rot_limit_table rot_limit_tbl_4x12 = {
  542. .ycbcr420_2p = {
  543. .min_w = 32,
  544. .min_h = 32,
  545. .max_w = SZ_32K,
  546. .max_h = SZ_32K,
  547. .align = 3,
  548. },
  549. .rgb888 = {
  550. .min_w = 8,
  551. .min_h = 8,
  552. .max_w = SZ_8K,
  553. .max_h = SZ_8K,
  554. .align = 2,
  555. },
  556. };
  557. static struct rot_limit_table rot_limit_tbl_5250 = {
  558. .ycbcr420_2p = {
  559. .min_w = 32,
  560. .min_h = 32,
  561. .max_w = SZ_32K,
  562. .max_h = SZ_32K,
  563. .align = 3,
  564. },
  565. .rgb888 = {
  566. .min_w = 8,
  567. .min_h = 8,
  568. .max_w = SZ_8K,
  569. .max_h = SZ_8K,
  570. .align = 1,
  571. },
  572. };
  573. static const struct of_device_id exynos_rotator_match[] = {
  574. {
  575. .compatible = "samsung,exynos4210-rotator",
  576. .data = &rot_limit_tbl_4210,
  577. },
  578. {
  579. .compatible = "samsung,exynos4212-rotator",
  580. .data = &rot_limit_tbl_4x12,
  581. },
  582. {
  583. .compatible = "samsung,exynos5250-rotator",
  584. .data = &rot_limit_tbl_5250,
  585. },
  586. {},
  587. };
  588. MODULE_DEVICE_TABLE(of, exynos_rotator_match);
  589. static int rotator_probe(struct platform_device *pdev)
  590. {
  591. struct device *dev = &pdev->dev;
  592. struct rot_context *rot;
  593. struct exynos_drm_ippdrv *ippdrv;
  594. const struct of_device_id *match;
  595. int ret;
  596. if (!dev->of_node) {
  597. dev_err(dev, "cannot find of_node.\n");
  598. return -ENODEV;
  599. }
  600. rot = devm_kzalloc(dev, sizeof(*rot), GFP_KERNEL);
  601. if (!rot)
  602. return -ENOMEM;
  603. match = of_match_node(exynos_rotator_match, dev->of_node);
  604. if (!match) {
  605. dev_err(dev, "failed to match node\n");
  606. return -ENODEV;
  607. }
  608. rot->limit_tbl = (struct rot_limit_table *)match->data;
  609. rot->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  610. rot->regs = devm_ioremap_resource(dev, rot->regs_res);
  611. if (IS_ERR(rot->regs))
  612. return PTR_ERR(rot->regs);
  613. rot->irq = platform_get_irq(pdev, 0);
  614. if (rot->irq < 0) {
  615. dev_err(dev, "failed to get irq\n");
  616. return rot->irq;
  617. }
  618. ret = devm_request_threaded_irq(dev, rot->irq, NULL,
  619. rotator_irq_handler, IRQF_ONESHOT, "drm_rotator", rot);
  620. if (ret < 0) {
  621. dev_err(dev, "failed to request irq\n");
  622. return ret;
  623. }
  624. rot->clock = devm_clk_get(dev, "rotator");
  625. if (IS_ERR(rot->clock)) {
  626. dev_err(dev, "failed to get clock\n");
  627. return PTR_ERR(rot->clock);
  628. }
  629. pm_runtime_enable(dev);
  630. ippdrv = &rot->ippdrv;
  631. ippdrv->dev = dev;
  632. ippdrv->ops[EXYNOS_DRM_OPS_SRC] = &rot_src_ops;
  633. ippdrv->ops[EXYNOS_DRM_OPS_DST] = &rot_dst_ops;
  634. ippdrv->check_property = rotator_ippdrv_check_property;
  635. ippdrv->start = rotator_ippdrv_start;
  636. ret = rotator_init_prop_list(ippdrv);
  637. if (ret < 0) {
  638. dev_err(dev, "failed to init property list.\n");
  639. goto err_ippdrv_register;
  640. }
  641. DRM_DEBUG_KMS("ippdrv[%p]\n", ippdrv);
  642. platform_set_drvdata(pdev, rot);
  643. ret = exynos_drm_ippdrv_register(ippdrv);
  644. if (ret < 0) {
  645. dev_err(dev, "failed to register drm rotator device\n");
  646. goto err_ippdrv_register;
  647. }
  648. dev_info(dev, "The exynos rotator is probed successfully\n");
  649. return 0;
  650. err_ippdrv_register:
  651. pm_runtime_disable(dev);
  652. return ret;
  653. }
  654. static int rotator_remove(struct platform_device *pdev)
  655. {
  656. struct device *dev = &pdev->dev;
  657. struct rot_context *rot = dev_get_drvdata(dev);
  658. struct exynos_drm_ippdrv *ippdrv = &rot->ippdrv;
  659. exynos_drm_ippdrv_unregister(ippdrv);
  660. pm_runtime_disable(dev);
  661. return 0;
  662. }
  663. #ifdef CONFIG_PM
  664. static int rotator_clk_crtl(struct rot_context *rot, bool enable)
  665. {
  666. if (enable) {
  667. clk_prepare_enable(rot->clock);
  668. rot->suspended = false;
  669. } else {
  670. clk_disable_unprepare(rot->clock);
  671. rot->suspended = true;
  672. }
  673. return 0;
  674. }
  675. #ifdef CONFIG_PM_SLEEP
  676. static int rotator_suspend(struct device *dev)
  677. {
  678. struct rot_context *rot = dev_get_drvdata(dev);
  679. if (pm_runtime_suspended(dev))
  680. return 0;
  681. return rotator_clk_crtl(rot, false);
  682. }
  683. static int rotator_resume(struct device *dev)
  684. {
  685. struct rot_context *rot = dev_get_drvdata(dev);
  686. if (!pm_runtime_suspended(dev))
  687. return rotator_clk_crtl(rot, true);
  688. return 0;
  689. }
  690. #endif
  691. static int rotator_runtime_suspend(struct device *dev)
  692. {
  693. struct rot_context *rot = dev_get_drvdata(dev);
  694. return rotator_clk_crtl(rot, false);
  695. }
  696. static int rotator_runtime_resume(struct device *dev)
  697. {
  698. struct rot_context *rot = dev_get_drvdata(dev);
  699. return rotator_clk_crtl(rot, true);
  700. }
  701. #endif
  702. static const struct dev_pm_ops rotator_pm_ops = {
  703. SET_SYSTEM_SLEEP_PM_OPS(rotator_suspend, rotator_resume)
  704. SET_RUNTIME_PM_OPS(rotator_runtime_suspend, rotator_runtime_resume,
  705. NULL)
  706. };
  707. struct platform_driver rotator_driver = {
  708. .probe = rotator_probe,
  709. .remove = rotator_remove,
  710. .driver = {
  711. .name = "exynos-rot",
  712. .owner = THIS_MODULE,
  713. .pm = &rotator_pm_ops,
  714. .of_match_table = exynos_rotator_match,
  715. },
  716. };