exynos_drm_rotator.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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.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,
  136. (struct work_struct *)event_work);
  137. } else
  138. DRM_ERROR("the SFR is set illegally\n");
  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;
  395. prop_list = devm_kzalloc(ippdrv->dev, sizeof(*prop_list), GFP_KERNEL);
  396. if (!prop_list) {
  397. DRM_ERROR("failed to alloc property list.\n");
  398. return -ENOMEM;
  399. }
  400. prop_list->version = 1;
  401. prop_list->flip = (1 << EXYNOS_DRM_FLIP_VERTICAL) |
  402. (1 << EXYNOS_DRM_FLIP_HORIZONTAL);
  403. prop_list->degree = (1 << EXYNOS_DRM_DEGREE_0) |
  404. (1 << EXYNOS_DRM_DEGREE_90) |
  405. (1 << EXYNOS_DRM_DEGREE_180) |
  406. (1 << EXYNOS_DRM_DEGREE_270);
  407. prop_list->csc = 0;
  408. prop_list->crop = 0;
  409. prop_list->scale = 0;
  410. ippdrv->prop_list = prop_list;
  411. return 0;
  412. }
  413. static inline bool rotator_check_drm_fmt(u32 fmt)
  414. {
  415. switch (fmt) {
  416. case DRM_FORMAT_XRGB8888:
  417. case DRM_FORMAT_NV12:
  418. return true;
  419. default:
  420. DRM_DEBUG_KMS("not support format\n");
  421. return false;
  422. }
  423. }
  424. static inline bool rotator_check_drm_flip(enum drm_exynos_flip flip)
  425. {
  426. switch (flip) {
  427. case EXYNOS_DRM_FLIP_NONE:
  428. case EXYNOS_DRM_FLIP_VERTICAL:
  429. case EXYNOS_DRM_FLIP_HORIZONTAL:
  430. case EXYNOS_DRM_FLIP_BOTH:
  431. return true;
  432. default:
  433. DRM_DEBUG_KMS("invalid flip\n");
  434. return false;
  435. }
  436. }
  437. static int rotator_ippdrv_check_property(struct device *dev,
  438. struct drm_exynos_ipp_property *property)
  439. {
  440. struct drm_exynos_ipp_config *src_config =
  441. &property->config[EXYNOS_DRM_OPS_SRC];
  442. struct drm_exynos_ipp_config *dst_config =
  443. &property->config[EXYNOS_DRM_OPS_DST];
  444. struct drm_exynos_pos *src_pos = &src_config->pos;
  445. struct drm_exynos_pos *dst_pos = &dst_config->pos;
  446. struct drm_exynos_sz *src_sz = &src_config->sz;
  447. struct drm_exynos_sz *dst_sz = &dst_config->sz;
  448. bool swap = false;
  449. /* Check format configuration */
  450. if (src_config->fmt != dst_config->fmt) {
  451. DRM_DEBUG_KMS("not support csc feature\n");
  452. return -EINVAL;
  453. }
  454. if (!rotator_check_drm_fmt(dst_config->fmt)) {
  455. DRM_DEBUG_KMS("invalid format\n");
  456. return -EINVAL;
  457. }
  458. /* Check transform configuration */
  459. if (src_config->degree != EXYNOS_DRM_DEGREE_0) {
  460. DRM_DEBUG_KMS("not support source-side rotation\n");
  461. return -EINVAL;
  462. }
  463. switch (dst_config->degree) {
  464. case EXYNOS_DRM_DEGREE_90:
  465. case EXYNOS_DRM_DEGREE_270:
  466. swap = true;
  467. case EXYNOS_DRM_DEGREE_0:
  468. case EXYNOS_DRM_DEGREE_180:
  469. /* No problem */
  470. break;
  471. default:
  472. DRM_DEBUG_KMS("invalid degree\n");
  473. return -EINVAL;
  474. }
  475. if (src_config->flip != EXYNOS_DRM_FLIP_NONE) {
  476. DRM_DEBUG_KMS("not support source-side flip\n");
  477. return -EINVAL;
  478. }
  479. if (!rotator_check_drm_flip(dst_config->flip)) {
  480. DRM_DEBUG_KMS("invalid flip\n");
  481. return -EINVAL;
  482. }
  483. /* Check size configuration */
  484. if ((src_pos->x + src_pos->w > src_sz->hsize) ||
  485. (src_pos->y + src_pos->h > src_sz->vsize)) {
  486. DRM_DEBUG_KMS("out of source buffer bound\n");
  487. return -EINVAL;
  488. }
  489. if (swap) {
  490. if ((dst_pos->x + dst_pos->h > dst_sz->vsize) ||
  491. (dst_pos->y + dst_pos->w > dst_sz->hsize)) {
  492. DRM_DEBUG_KMS("out of destination buffer bound\n");
  493. return -EINVAL;
  494. }
  495. if ((src_pos->w != dst_pos->h) || (src_pos->h != dst_pos->w)) {
  496. DRM_DEBUG_KMS("not support scale feature\n");
  497. return -EINVAL;
  498. }
  499. } else {
  500. if ((dst_pos->x + dst_pos->w > dst_sz->hsize) ||
  501. (dst_pos->y + dst_pos->h > dst_sz->vsize)) {
  502. DRM_DEBUG_KMS("out of destination buffer bound\n");
  503. return -EINVAL;
  504. }
  505. if ((src_pos->w != dst_pos->w) || (src_pos->h != dst_pos->h)) {
  506. DRM_DEBUG_KMS("not support scale feature\n");
  507. return -EINVAL;
  508. }
  509. }
  510. return 0;
  511. }
  512. static int rotator_ippdrv_start(struct device *dev, enum drm_exynos_ipp_cmd cmd)
  513. {
  514. struct rot_context *rot = dev_get_drvdata(dev);
  515. u32 val;
  516. if (rot->suspended) {
  517. DRM_ERROR("suspended state\n");
  518. return -EPERM;
  519. }
  520. if (cmd != IPP_CMD_M2M) {
  521. DRM_ERROR("not support cmd: %d\n", cmd);
  522. return -EINVAL;
  523. }
  524. /* Set interrupt enable */
  525. rotator_reg_set_irq(rot, true);
  526. val = rot_read(ROT_CONTROL);
  527. val |= ROT_CONTROL_START;
  528. rot_write(val, ROT_CONTROL);
  529. return 0;
  530. }
  531. static int rotator_probe(struct platform_device *pdev)
  532. {
  533. struct device *dev = &pdev->dev;
  534. struct rot_context *rot;
  535. struct exynos_drm_ippdrv *ippdrv;
  536. int ret;
  537. rot = devm_kzalloc(dev, sizeof(*rot), GFP_KERNEL);
  538. if (!rot) {
  539. dev_err(dev, "failed to allocate rot\n");
  540. return -ENOMEM;
  541. }
  542. rot->limit_tbl = (struct rot_limit_table *)
  543. platform_get_device_id(pdev)->driver_data;
  544. rot->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  545. rot->regs = devm_ioremap_resource(dev, rot->regs_res);
  546. if (IS_ERR(rot->regs))
  547. return PTR_ERR(rot->regs);
  548. rot->irq = platform_get_irq(pdev, 0);
  549. if (rot->irq < 0) {
  550. dev_err(dev, "failed to get irq\n");
  551. return rot->irq;
  552. }
  553. ret = devm_request_threaded_irq(dev, rot->irq, NULL,
  554. rotator_irq_handler, IRQF_ONESHOT, "drm_rotator", rot);
  555. if (ret < 0) {
  556. dev_err(dev, "failed to request irq\n");
  557. return ret;
  558. }
  559. rot->clock = devm_clk_get(dev, "rotator");
  560. if (IS_ERR(rot->clock)) {
  561. dev_err(dev, "failed to get clock\n");
  562. return PTR_ERR(rot->clock);
  563. }
  564. pm_runtime_enable(dev);
  565. ippdrv = &rot->ippdrv;
  566. ippdrv->dev = dev;
  567. ippdrv->ops[EXYNOS_DRM_OPS_SRC] = &rot_src_ops;
  568. ippdrv->ops[EXYNOS_DRM_OPS_DST] = &rot_dst_ops;
  569. ippdrv->check_property = rotator_ippdrv_check_property;
  570. ippdrv->start = rotator_ippdrv_start;
  571. ret = rotator_init_prop_list(ippdrv);
  572. if (ret < 0) {
  573. dev_err(dev, "failed to init property list.\n");
  574. goto err_ippdrv_register;
  575. }
  576. DRM_DEBUG_KMS("ippdrv[0x%x]\n", (int)ippdrv);
  577. platform_set_drvdata(pdev, rot);
  578. ret = exynos_drm_ippdrv_register(ippdrv);
  579. if (ret < 0) {
  580. dev_err(dev, "failed to register drm rotator device\n");
  581. goto err_ippdrv_register;
  582. }
  583. dev_info(dev, "The exynos rotator is probed successfully\n");
  584. return 0;
  585. err_ippdrv_register:
  586. pm_runtime_disable(dev);
  587. return ret;
  588. }
  589. static int rotator_remove(struct platform_device *pdev)
  590. {
  591. struct device *dev = &pdev->dev;
  592. struct rot_context *rot = dev_get_drvdata(dev);
  593. struct exynos_drm_ippdrv *ippdrv = &rot->ippdrv;
  594. exynos_drm_ippdrv_unregister(ippdrv);
  595. pm_runtime_disable(dev);
  596. return 0;
  597. }
  598. static struct rot_limit_table rot_limit_tbl = {
  599. .ycbcr420_2p = {
  600. .min_w = 32,
  601. .min_h = 32,
  602. .max_w = SZ_32K,
  603. .max_h = SZ_32K,
  604. .align = 3,
  605. },
  606. .rgb888 = {
  607. .min_w = 8,
  608. .min_h = 8,
  609. .max_w = SZ_8K,
  610. .max_h = SZ_8K,
  611. .align = 2,
  612. },
  613. };
  614. static struct platform_device_id rotator_driver_ids[] = {
  615. {
  616. .name = "exynos-rot",
  617. .driver_data = (unsigned long)&rot_limit_tbl,
  618. },
  619. {},
  620. };
  621. static int rotator_clk_crtl(struct rot_context *rot, bool enable)
  622. {
  623. if (enable) {
  624. clk_enable(rot->clock);
  625. rot->suspended = false;
  626. } else {
  627. clk_disable(rot->clock);
  628. rot->suspended = true;
  629. }
  630. return 0;
  631. }
  632. #ifdef CONFIG_PM_SLEEP
  633. static int rotator_suspend(struct device *dev)
  634. {
  635. struct rot_context *rot = dev_get_drvdata(dev);
  636. if (pm_runtime_suspended(dev))
  637. return 0;
  638. return rotator_clk_crtl(rot, false);
  639. }
  640. static int rotator_resume(struct device *dev)
  641. {
  642. struct rot_context *rot = dev_get_drvdata(dev);
  643. if (!pm_runtime_suspended(dev))
  644. return rotator_clk_crtl(rot, true);
  645. return 0;
  646. }
  647. #endif
  648. #ifdef CONFIG_PM_RUNTIME
  649. static int rotator_runtime_suspend(struct device *dev)
  650. {
  651. struct rot_context *rot = dev_get_drvdata(dev);
  652. return rotator_clk_crtl(rot, false);
  653. }
  654. static int rotator_runtime_resume(struct device *dev)
  655. {
  656. struct rot_context *rot = dev_get_drvdata(dev);
  657. return rotator_clk_crtl(rot, true);
  658. }
  659. #endif
  660. static const struct dev_pm_ops rotator_pm_ops = {
  661. SET_SYSTEM_SLEEP_PM_OPS(rotator_suspend, rotator_resume)
  662. SET_RUNTIME_PM_OPS(rotator_runtime_suspend, rotator_runtime_resume,
  663. NULL)
  664. };
  665. struct platform_driver rotator_driver = {
  666. .probe = rotator_probe,
  667. .remove = rotator_remove,
  668. .id_table = rotator_driver_ids,
  669. .driver = {
  670. .name = "exynos-rot",
  671. .owner = THIS_MODULE,
  672. .pm = &rotator_pm_ops,
  673. },
  674. };