fimc-lite.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /*
  2. * Samsung EXYNOS FIMC-LITE (camera host interface) driver
  3. *
  4. * Copyright (C) 2012 Samsung Electronics Co., Ltd.
  5. * Sylwester Nawrocki <s.nawrocki@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 Foundation.
  10. */
  11. #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
  12. #include <linux/bug.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-mem2mem.h>
  27. #include <media/videobuf2-core.h>
  28. #include <media/videobuf2-dma-contig.h>
  29. #include "fimc-mdevice.h"
  30. #include "fimc-core.h"
  31. #include "fimc-lite-reg.h"
  32. static int debug;
  33. module_param(debug, int, 0644);
  34. static const struct fimc_fmt fimc_lite_formats[] = {
  35. {
  36. .name = "YUV 4:2:2 packed, YCbYCr",
  37. .fourcc = V4L2_PIX_FMT_YUYV,
  38. .depth = { 16 },
  39. .color = FIMC_FMT_YCBYCR422,
  40. .memplanes = 1,
  41. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  42. }, {
  43. .name = "YUV 4:2:2 packed, CbYCrY",
  44. .fourcc = V4L2_PIX_FMT_UYVY,
  45. .depth = { 16 },
  46. .color = FIMC_FMT_CBYCRY422,
  47. .memplanes = 1,
  48. .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
  49. }, {
  50. .name = "YUV 4:2:2 packed, CrYCbY",
  51. .fourcc = V4L2_PIX_FMT_VYUY,
  52. .depth = { 16 },
  53. .color = FIMC_FMT_CRYCBY422,
  54. .memplanes = 1,
  55. .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
  56. }, {
  57. .name = "YUV 4:2:2 packed, YCrYCb",
  58. .fourcc = V4L2_PIX_FMT_YVYU,
  59. .depth = { 16 },
  60. .color = FIMC_FMT_YCRYCB422,
  61. .memplanes = 1,
  62. .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
  63. }, {
  64. .name = "RAW8 (GRBG)",
  65. .fourcc = V4L2_PIX_FMT_SGRBG8,
  66. .depth = { 8 },
  67. .color = FIMC_FMT_RAW8,
  68. .memplanes = 1,
  69. .mbus_code = V4L2_MBUS_FMT_SGRBG8_1X8,
  70. }, {
  71. .name = "RAW10 (GRBG)",
  72. .fourcc = V4L2_PIX_FMT_SGRBG10,
  73. .depth = { 10 },
  74. .color = FIMC_FMT_RAW10,
  75. .memplanes = 1,
  76. .mbus_code = V4L2_MBUS_FMT_SGRBG10_1X10,
  77. }, {
  78. .name = "RAW12 (GRBG)",
  79. .fourcc = V4L2_PIX_FMT_SGRBG12,
  80. .depth = { 12 },
  81. .color = FIMC_FMT_RAW12,
  82. .memplanes = 1,
  83. .mbus_code = V4L2_MBUS_FMT_SGRBG12_1X12,
  84. },
  85. };
  86. /**
  87. * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
  88. * @pixelformat: fourcc to match, ignored if null
  89. * @mbus_code: media bus code to match, ignored if null
  90. * @index: index to the fimc_lite_formats array, ignored if negative
  91. */
  92. static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat,
  93. const u32 *mbus_code, int index)
  94. {
  95. const struct fimc_fmt *fmt, *def_fmt = NULL;
  96. unsigned int i;
  97. int id = 0;
  98. if (index >= (int)ARRAY_SIZE(fimc_lite_formats))
  99. return NULL;
  100. for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) {
  101. fmt = &fimc_lite_formats[i];
  102. if (pixelformat && fmt->fourcc == *pixelformat)
  103. return fmt;
  104. if (mbus_code && fmt->mbus_code == *mbus_code)
  105. return fmt;
  106. if (index == id)
  107. def_fmt = fmt;
  108. id++;
  109. }
  110. return def_fmt;
  111. }
  112. static int fimc_lite_hw_init(struct fimc_lite *fimc)
  113. {
  114. struct fimc_pipeline *pipeline = &fimc->pipeline;
  115. struct fimc_sensor_info *sensor;
  116. unsigned long flags;
  117. if (pipeline->subdevs[IDX_SENSOR] == NULL)
  118. return -ENXIO;
  119. if (fimc->fmt == NULL)
  120. return -EINVAL;
  121. sensor = v4l2_get_subdev_hostdata(pipeline->subdevs[IDX_SENSOR]);
  122. spin_lock_irqsave(&fimc->slock, flags);
  123. flite_hw_set_camera_bus(fimc, sensor->pdata);
  124. flite_hw_set_source_format(fimc, &fimc->inp_frame);
  125. flite_hw_set_window_offset(fimc, &fimc->inp_frame);
  126. flite_hw_set_output_dma(fimc, &fimc->out_frame, true);
  127. flite_hw_set_interrupt_mask(fimc);
  128. flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
  129. if (debug > 0)
  130. flite_hw_dump_regs(fimc, __func__);
  131. spin_unlock_irqrestore(&fimc->slock, flags);
  132. return 0;
  133. }
  134. /*
  135. * Reinitialize the driver so it is ready to start the streaming again.
  136. * Set fimc->state to indicate stream off and the hardware shut down state.
  137. * If not suspending (@suspend is false), return any buffers to videobuf2.
  138. * Otherwise put any owned buffers onto the pending buffers queue, so they
  139. * can be re-spun when the device is being resumed. Also perform FIMC
  140. * software reset and disable streaming on the whole pipeline if required.
  141. */
  142. static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend)
  143. {
  144. struct flite_buffer *buf;
  145. unsigned long flags;
  146. bool streaming;
  147. spin_lock_irqsave(&fimc->slock, flags);
  148. streaming = fimc->state & (1 << ST_SENSOR_STREAM);
  149. fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF |
  150. 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM);
  151. if (suspend)
  152. fimc->state |= (1 << ST_FLITE_SUSPENDED);
  153. else
  154. fimc->state &= ~(1 << ST_FLITE_PENDING |
  155. 1 << ST_FLITE_SUSPENDED);
  156. /* Release unused buffers */
  157. while (!suspend && !list_empty(&fimc->pending_buf_q)) {
  158. buf = fimc_lite_pending_queue_pop(fimc);
  159. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  160. }
  161. /* If suspending put unused buffers onto pending queue */
  162. while (!list_empty(&fimc->active_buf_q)) {
  163. buf = fimc_lite_active_queue_pop(fimc);
  164. if (suspend)
  165. fimc_lite_pending_queue_add(fimc, buf);
  166. else
  167. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  168. }
  169. spin_unlock_irqrestore(&fimc->slock, flags);
  170. flite_hw_reset(fimc);
  171. if (!streaming)
  172. return 0;
  173. return fimc_pipeline_s_stream(&fimc->pipeline, 0);
  174. }
  175. static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend)
  176. {
  177. unsigned long flags;
  178. if (!fimc_lite_active(fimc))
  179. return 0;
  180. spin_lock_irqsave(&fimc->slock, flags);
  181. set_bit(ST_FLITE_OFF, &fimc->state);
  182. flite_hw_capture_stop(fimc);
  183. spin_unlock_irqrestore(&fimc->slock, flags);
  184. wait_event_timeout(fimc->irq_queue,
  185. !test_bit(ST_FLITE_OFF, &fimc->state),
  186. (2*HZ/10)); /* 200 ms */
  187. return fimc_lite_reinit(fimc, suspend);
  188. }
  189. /* Must be called with fimc.slock spinlock held. */
  190. static void fimc_lite_config_update(struct fimc_lite *fimc)
  191. {
  192. flite_hw_set_window_offset(fimc, &fimc->inp_frame);
  193. flite_hw_set_dma_window(fimc, &fimc->out_frame);
  194. flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
  195. clear_bit(ST_FLITE_CONFIG, &fimc->state);
  196. }
  197. static irqreturn_t flite_irq_handler(int irq, void *priv)
  198. {
  199. struct fimc_lite *fimc = priv;
  200. struct flite_buffer *vbuf;
  201. unsigned long flags;
  202. struct timeval *tv;
  203. struct timespec ts;
  204. u32 intsrc;
  205. spin_lock_irqsave(&fimc->slock, flags);
  206. intsrc = flite_hw_get_interrupt_source(fimc);
  207. flite_hw_clear_pending_irq(fimc);
  208. if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) {
  209. wake_up(&fimc->irq_queue);
  210. goto done;
  211. }
  212. if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) {
  213. clear_bit(ST_FLITE_RUN, &fimc->state);
  214. fimc->events.data_overflow++;
  215. }
  216. if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) {
  217. flite_hw_clear_last_capture_end(fimc);
  218. clear_bit(ST_FLITE_STREAM, &fimc->state);
  219. wake_up(&fimc->irq_queue);
  220. }
  221. if (fimc->out_path != FIMC_IO_DMA)
  222. goto done;
  223. if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) &&
  224. test_bit(ST_FLITE_RUN, &fimc->state) &&
  225. !list_empty(&fimc->active_buf_q) &&
  226. !list_empty(&fimc->pending_buf_q)) {
  227. vbuf = fimc_lite_active_queue_pop(fimc);
  228. ktime_get_ts(&ts);
  229. tv = &vbuf->vb.v4l2_buf.timestamp;
  230. tv->tv_sec = ts.tv_sec;
  231. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  232. vbuf->vb.v4l2_buf.sequence = fimc->frame_count++;
  233. vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE);
  234. vbuf = fimc_lite_pending_queue_pop(fimc);
  235. flite_hw_set_output_addr(fimc, vbuf->paddr);
  236. fimc_lite_active_queue_add(fimc, vbuf);
  237. }
  238. if (test_bit(ST_FLITE_CONFIG, &fimc->state))
  239. fimc_lite_config_update(fimc);
  240. if (list_empty(&fimc->pending_buf_q)) {
  241. flite_hw_capture_stop(fimc);
  242. clear_bit(ST_FLITE_STREAM, &fimc->state);
  243. }
  244. done:
  245. set_bit(ST_FLITE_RUN, &fimc->state);
  246. spin_unlock_irqrestore(&fimc->slock, flags);
  247. return IRQ_HANDLED;
  248. }
  249. static int start_streaming(struct vb2_queue *q, unsigned int count)
  250. {
  251. struct fimc_lite *fimc = q->drv_priv;
  252. int ret;
  253. fimc->frame_count = 0;
  254. ret = fimc_lite_hw_init(fimc);
  255. if (ret) {
  256. fimc_lite_reinit(fimc, false);
  257. return ret;
  258. }
  259. set_bit(ST_FLITE_PENDING, &fimc->state);
  260. if (!list_empty(&fimc->active_buf_q) &&
  261. !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
  262. flite_hw_capture_start(fimc);
  263. if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
  264. fimc_pipeline_s_stream(&fimc->pipeline, 1);
  265. }
  266. if (debug > 0)
  267. flite_hw_dump_regs(fimc, __func__);
  268. return 0;
  269. }
  270. static int stop_streaming(struct vb2_queue *q)
  271. {
  272. struct fimc_lite *fimc = q->drv_priv;
  273. if (!fimc_lite_active(fimc))
  274. return -EINVAL;
  275. return fimc_lite_stop_capture(fimc, false);
  276. }
  277. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
  278. unsigned int *num_buffers, unsigned int *num_planes,
  279. unsigned int sizes[], void *allocators[])
  280. {
  281. const struct v4l2_pix_format_mplane *pixm = NULL;
  282. struct fimc_lite *fimc = vq->drv_priv;
  283. struct flite_frame *frame = &fimc->out_frame;
  284. const struct fimc_fmt *fmt = fimc->fmt;
  285. unsigned long wh;
  286. int i;
  287. if (pfmt) {
  288. pixm = &pfmt->fmt.pix_mp;
  289. fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, -1);
  290. wh = pixm->width * pixm->height;
  291. } else {
  292. wh = frame->f_width * frame->f_height;
  293. }
  294. if (fmt == NULL)
  295. return -EINVAL;
  296. *num_planes = fmt->memplanes;
  297. for (i = 0; i < fmt->memplanes; i++) {
  298. unsigned int size = (wh * fmt->depth[i]) / 8;
  299. if (pixm)
  300. sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
  301. else
  302. sizes[i] = size;
  303. allocators[i] = fimc->alloc_ctx;
  304. }
  305. return 0;
  306. }
  307. static int buffer_prepare(struct vb2_buffer *vb)
  308. {
  309. struct vb2_queue *vq = vb->vb2_queue;
  310. struct fimc_lite *fimc = vq->drv_priv;
  311. int i;
  312. if (fimc->fmt == NULL)
  313. return -EINVAL;
  314. for (i = 0; i < fimc->fmt->memplanes; i++) {
  315. unsigned long size = fimc->payload[i];
  316. if (vb2_plane_size(vb, i) < size) {
  317. v4l2_err(fimc->vfd,
  318. "User buffer too small (%ld < %ld)\n",
  319. vb2_plane_size(vb, i), size);
  320. return -EINVAL;
  321. }
  322. vb2_set_plane_payload(vb, i, size);
  323. }
  324. return 0;
  325. }
  326. static void buffer_queue(struct vb2_buffer *vb)
  327. {
  328. struct flite_buffer *buf
  329. = container_of(vb, struct flite_buffer, vb);
  330. struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue);
  331. unsigned long flags;
  332. spin_lock_irqsave(&fimc->slock, flags);
  333. buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0);
  334. if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) &&
  335. !test_bit(ST_FLITE_STREAM, &fimc->state) &&
  336. list_empty(&fimc->active_buf_q)) {
  337. flite_hw_set_output_addr(fimc, buf->paddr);
  338. fimc_lite_active_queue_add(fimc, buf);
  339. } else {
  340. fimc_lite_pending_queue_add(fimc, buf);
  341. }
  342. if (vb2_is_streaming(&fimc->vb_queue) &&
  343. !list_empty(&fimc->pending_buf_q) &&
  344. !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
  345. flite_hw_capture_start(fimc);
  346. spin_unlock_irqrestore(&fimc->slock, flags);
  347. if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
  348. fimc_pipeline_s_stream(&fimc->pipeline, 1);
  349. return;
  350. }
  351. spin_unlock_irqrestore(&fimc->slock, flags);
  352. }
  353. static void fimc_lock(struct vb2_queue *vq)
  354. {
  355. struct fimc_lite *fimc = vb2_get_drv_priv(vq);
  356. mutex_lock(&fimc->lock);
  357. }
  358. static void fimc_unlock(struct vb2_queue *vq)
  359. {
  360. struct fimc_lite *fimc = vb2_get_drv_priv(vq);
  361. mutex_unlock(&fimc->lock);
  362. }
  363. static const struct vb2_ops fimc_lite_qops = {
  364. .queue_setup = queue_setup,
  365. .buf_prepare = buffer_prepare,
  366. .buf_queue = buffer_queue,
  367. .wait_prepare = fimc_unlock,
  368. .wait_finish = fimc_lock,
  369. .start_streaming = start_streaming,
  370. .stop_streaming = stop_streaming,
  371. };
  372. static void fimc_lite_clear_event_counters(struct fimc_lite *fimc)
  373. {
  374. unsigned long flags;
  375. spin_lock_irqsave(&fimc->slock, flags);
  376. memset(&fimc->events, 0, sizeof(fimc->events));
  377. spin_unlock_irqrestore(&fimc->slock, flags);
  378. }
  379. static int fimc_lite_open(struct file *file)
  380. {
  381. struct fimc_lite *fimc = video_drvdata(file);
  382. int ret;
  383. set_bit(ST_FLITE_IN_USE, &fimc->state);
  384. ret = pm_runtime_get_sync(&fimc->pdev->dev);
  385. if (ret < 0)
  386. return ret;
  387. if (++fimc->ref_count != 1 || fimc->out_path != FIMC_IO_DMA)
  388. return 0;
  389. ret = v4l2_fh_open(file);
  390. if (ret < 0)
  391. return ret;
  392. ret = fimc_pipeline_initialize(&fimc->pipeline, &fimc->vfd->entity,
  393. true);
  394. if (ret < 0) {
  395. pm_runtime_put_sync(&fimc->pdev->dev);
  396. fimc->ref_count--;
  397. v4l2_fh_release(file);
  398. clear_bit(ST_FLITE_IN_USE, &fimc->state);
  399. }
  400. fimc_lite_clear_event_counters(fimc);
  401. return ret;
  402. }
  403. static int fimc_lite_close(struct file *file)
  404. {
  405. struct fimc_lite *fimc = video_drvdata(file);
  406. if (--fimc->ref_count == 0 && fimc->out_path == FIMC_IO_DMA) {
  407. clear_bit(ST_FLITE_IN_USE, &fimc->state);
  408. fimc_lite_stop_capture(fimc, false);
  409. fimc_pipeline_shutdown(&fimc->pipeline);
  410. clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
  411. }
  412. pm_runtime_put(&fimc->pdev->dev);
  413. if (fimc->ref_count == 0)
  414. vb2_queue_release(&fimc->vb_queue);
  415. return v4l2_fh_release(file);
  416. }
  417. static unsigned int fimc_lite_poll(struct file *file,
  418. struct poll_table_struct *wait)
  419. {
  420. struct fimc_lite *fimc = video_drvdata(file);
  421. return vb2_poll(&fimc->vb_queue, file, wait);
  422. }
  423. static int fimc_lite_mmap(struct file *file, struct vm_area_struct *vma)
  424. {
  425. struct fimc_lite *fimc = video_drvdata(file);
  426. return vb2_mmap(&fimc->vb_queue, vma);
  427. }
  428. static const struct v4l2_file_operations fimc_lite_fops = {
  429. .owner = THIS_MODULE,
  430. .open = fimc_lite_open,
  431. .release = fimc_lite_close,
  432. .poll = fimc_lite_poll,
  433. .unlocked_ioctl = video_ioctl2,
  434. .mmap = fimc_lite_mmap,
  435. };
  436. /*
  437. * Format and crop negotiation helpers
  438. */
  439. static const struct fimc_fmt *fimc_lite_try_format(struct fimc_lite *fimc,
  440. u32 *width, u32 *height,
  441. u32 *code, u32 *fourcc, int pad)
  442. {
  443. struct flite_variant *variant = fimc->variant;
  444. const struct fimc_fmt *fmt;
  445. fmt = fimc_lite_find_format(fourcc, code, 0);
  446. if (WARN_ON(!fmt))
  447. return NULL;
  448. if (code)
  449. *code = fmt->mbus_code;
  450. if (fourcc)
  451. *fourcc = fmt->fourcc;
  452. if (pad == FLITE_SD_PAD_SINK) {
  453. v4l_bound_align_image(width, 8, variant->max_width,
  454. ffs(variant->out_width_align) - 1,
  455. height, 0, variant->max_height, 0, 0);
  456. } else {
  457. v4l_bound_align_image(width, 8, fimc->inp_frame.rect.width,
  458. ffs(variant->out_width_align) - 1,
  459. height, 0, fimc->inp_frame.rect.height,
  460. 0, 0);
  461. }
  462. v4l2_dbg(1, debug, &fimc->subdev, "code: 0x%x, %dx%d\n",
  463. code ? *code : 0, *width, *height);
  464. return fmt;
  465. }
  466. static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r)
  467. {
  468. struct flite_frame *frame = &fimc->inp_frame;
  469. v4l_bound_align_image(&r->width, 0, frame->f_width, 0,
  470. &r->height, 0, frame->f_height, 0, 0);
  471. /* Adjust left/top if cropping rectangle got out of bounds */
  472. r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
  473. r->left = round_down(r->left, fimc->variant->win_hor_offs_align);
  474. r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height);
  475. v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d",
  476. r->left, r->top, r->width, r->height,
  477. frame->f_width, frame->f_height);
  478. }
  479. static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r)
  480. {
  481. struct flite_frame *frame = &fimc->out_frame;
  482. struct v4l2_rect *crop_rect = &fimc->inp_frame.rect;
  483. /* Scaling is not supported so we enforce compose rectangle size
  484. same as size of the sink crop rectangle. */
  485. r->width = crop_rect->width;
  486. r->height = crop_rect->height;
  487. /* Adjust left/top if the composing rectangle got out of bounds */
  488. r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
  489. r->left = round_down(r->left, fimc->variant->out_hor_offs_align);
  490. r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height);
  491. v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d",
  492. r->left, r->top, r->width, r->height,
  493. frame->f_width, frame->f_height);
  494. }
  495. /*
  496. * Video node ioctl operations
  497. */
  498. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  499. struct v4l2_capability *cap)
  500. {
  501. strlcpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver));
  502. cap->bus_info[0] = 0;
  503. cap->card[0] = 0;
  504. cap->capabilities = V4L2_CAP_STREAMING;
  505. return 0;
  506. }
  507. static int fimc_lite_enum_fmt_mplane(struct file *file, void *priv,
  508. struct v4l2_fmtdesc *f)
  509. {
  510. const struct fimc_fmt *fmt;
  511. if (f->index >= ARRAY_SIZE(fimc_lite_formats))
  512. return -EINVAL;
  513. fmt = &fimc_lite_formats[f->index];
  514. strlcpy(f->description, fmt->name, sizeof(f->description));
  515. f->pixelformat = fmt->fourcc;
  516. return 0;
  517. }
  518. static int fimc_lite_g_fmt_mplane(struct file *file, void *fh,
  519. struct v4l2_format *f)
  520. {
  521. struct fimc_lite *fimc = video_drvdata(file);
  522. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  523. struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0];
  524. struct flite_frame *frame = &fimc->out_frame;
  525. const struct fimc_fmt *fmt = fimc->fmt;
  526. plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8;
  527. plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height;
  528. pixm->num_planes = fmt->memplanes;
  529. pixm->pixelformat = fmt->fourcc;
  530. pixm->width = frame->f_width;
  531. pixm->height = frame->f_height;
  532. pixm->field = V4L2_FIELD_NONE;
  533. pixm->colorspace = V4L2_COLORSPACE_JPEG;
  534. return 0;
  535. }
  536. static int fimc_lite_try_fmt(struct fimc_lite *fimc,
  537. struct v4l2_pix_format_mplane *pixm,
  538. const struct fimc_fmt **ffmt)
  539. {
  540. struct flite_variant *variant = fimc->variant;
  541. u32 bpl = pixm->plane_fmt[0].bytesperline;
  542. const struct fimc_fmt *fmt;
  543. fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, 0);
  544. if (WARN_ON(fmt == NULL))
  545. return -EINVAL;
  546. if (ffmt)
  547. *ffmt = fmt;
  548. v4l_bound_align_image(&pixm->width, 8, variant->max_width,
  549. ffs(variant->out_width_align) - 1,
  550. &pixm->height, 0, variant->max_height, 0, 0);
  551. if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width))
  552. pixm->plane_fmt[0].bytesperline = (pixm->width *
  553. fmt->depth[0]) / 8;
  554. if (pixm->plane_fmt[0].sizeimage == 0)
  555. pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height *
  556. fmt->depth[0]) / 8;
  557. pixm->num_planes = fmt->memplanes;
  558. pixm->pixelformat = fmt->fourcc;
  559. pixm->colorspace = V4L2_COLORSPACE_JPEG;
  560. pixm->field = V4L2_FIELD_NONE;
  561. return 0;
  562. }
  563. static int fimc_lite_try_fmt_mplane(struct file *file, void *fh,
  564. struct v4l2_format *f)
  565. {
  566. struct fimc_lite *fimc = video_drvdata(file);
  567. return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL);
  568. }
  569. static int fimc_lite_s_fmt_mplane(struct file *file, void *priv,
  570. struct v4l2_format *f)
  571. {
  572. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  573. struct fimc_lite *fimc = video_drvdata(file);
  574. struct flite_frame *frame = &fimc->out_frame;
  575. const struct fimc_fmt *fmt = NULL;
  576. int ret;
  577. if (vb2_is_busy(&fimc->vb_queue))
  578. return -EBUSY;
  579. ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt);
  580. if (ret < 0)
  581. return ret;
  582. fimc->fmt = fmt;
  583. fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8,
  584. pixm->plane_fmt[0].sizeimage);
  585. frame->f_width = pixm->width;
  586. frame->f_height = pixm->height;
  587. return 0;
  588. }
  589. static int fimc_pipeline_validate(struct fimc_lite *fimc)
  590. {
  591. struct v4l2_subdev *sd = &fimc->subdev;
  592. struct v4l2_subdev_format sink_fmt, src_fmt;
  593. struct media_pad *pad;
  594. int ret;
  595. while (1) {
  596. /* Retrieve format at the sink pad */
  597. pad = &sd->entity.pads[0];
  598. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  599. break;
  600. /* Don't call FIMC subdev operation to avoid nested locking */
  601. if (sd == &fimc->subdev) {
  602. struct flite_frame *ff = &fimc->out_frame;
  603. sink_fmt.format.width = ff->f_width;
  604. sink_fmt.format.height = ff->f_height;
  605. sink_fmt.format.code = fimc->fmt->mbus_code;
  606. } else {
  607. sink_fmt.pad = pad->index;
  608. sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  609. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
  610. &sink_fmt);
  611. if (ret < 0 && ret != -ENOIOCTLCMD)
  612. return -EPIPE;
  613. }
  614. /* Retrieve format at the source pad */
  615. pad = media_entity_remote_source(pad);
  616. if (pad == NULL ||
  617. media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  618. break;
  619. sd = media_entity_to_v4l2_subdev(pad->entity);
  620. src_fmt.pad = pad->index;
  621. src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  622. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
  623. if (ret < 0 && ret != -ENOIOCTLCMD)
  624. return -EPIPE;
  625. if (src_fmt.format.width != sink_fmt.format.width ||
  626. src_fmt.format.height != sink_fmt.format.height ||
  627. src_fmt.format.code != sink_fmt.format.code)
  628. return -EPIPE;
  629. }
  630. return 0;
  631. }
  632. static int fimc_lite_streamon(struct file *file, void *priv,
  633. enum v4l2_buf_type type)
  634. {
  635. struct fimc_lite *fimc = video_drvdata(file);
  636. struct v4l2_subdev *sensor = fimc->pipeline.subdevs[IDX_SENSOR];
  637. struct fimc_pipeline *p = &fimc->pipeline;
  638. int ret;
  639. if (fimc_lite_active(fimc))
  640. return -EBUSY;
  641. media_entity_pipeline_start(&sensor->entity, p->m_pipeline);
  642. ret = fimc_pipeline_validate(fimc);
  643. if (ret) {
  644. media_entity_pipeline_stop(&sensor->entity);
  645. return ret;
  646. }
  647. return vb2_streamon(&fimc->vb_queue, type);
  648. }
  649. static int fimc_lite_streamoff(struct file *file, void *priv,
  650. enum v4l2_buf_type type)
  651. {
  652. struct fimc_lite *fimc = video_drvdata(file);
  653. struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
  654. int ret;
  655. ret = vb2_streamoff(&fimc->vb_queue, type);
  656. if (ret == 0)
  657. media_entity_pipeline_stop(&sd->entity);
  658. return ret;
  659. }
  660. static int fimc_lite_reqbufs(struct file *file, void *priv,
  661. struct v4l2_requestbuffers *reqbufs)
  662. {
  663. struct fimc_lite *fimc = video_drvdata(file);
  664. int ret;
  665. reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count);
  666. ret = vb2_reqbufs(&fimc->vb_queue, reqbufs);
  667. if (!ret < 0)
  668. fimc->reqbufs_count = reqbufs->count;
  669. return ret;
  670. }
  671. static int fimc_lite_querybuf(struct file *file, void *priv,
  672. struct v4l2_buffer *buf)
  673. {
  674. struct fimc_lite *fimc = video_drvdata(file);
  675. return vb2_querybuf(&fimc->vb_queue, buf);
  676. }
  677. static int fimc_lite_qbuf(struct file *file, void *priv,
  678. struct v4l2_buffer *buf)
  679. {
  680. struct fimc_lite *fimc = video_drvdata(file);
  681. return vb2_qbuf(&fimc->vb_queue, buf);
  682. }
  683. static int fimc_lite_dqbuf(struct file *file, void *priv,
  684. struct v4l2_buffer *buf)
  685. {
  686. struct fimc_lite *fimc = video_drvdata(file);
  687. return vb2_dqbuf(&fimc->vb_queue, buf, file->f_flags & O_NONBLOCK);
  688. }
  689. static int fimc_lite_create_bufs(struct file *file, void *priv,
  690. struct v4l2_create_buffers *create)
  691. {
  692. struct fimc_lite *fimc = video_drvdata(file);
  693. return vb2_create_bufs(&fimc->vb_queue, create);
  694. }
  695. static int fimc_lite_prepare_buf(struct file *file, void *priv,
  696. struct v4l2_buffer *b)
  697. {
  698. struct fimc_lite *fimc = video_drvdata(file);
  699. return vb2_prepare_buf(&fimc->vb_queue, b);
  700. }
  701. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  702. static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
  703. {
  704. if (a->left < b->left || a->top < b->top)
  705. return 0;
  706. if (a->left + a->width > b->left + b->width)
  707. return 0;
  708. if (a->top + a->height > b->top + b->height)
  709. return 0;
  710. return 1;
  711. }
  712. static int fimc_lite_g_selection(struct file *file, void *fh,
  713. struct v4l2_selection *sel)
  714. {
  715. struct fimc_lite *fimc = video_drvdata(file);
  716. struct flite_frame *f = &fimc->out_frame;
  717. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  718. return -EINVAL;
  719. switch (sel->target) {
  720. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  721. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  722. sel->r.left = 0;
  723. sel->r.top = 0;
  724. sel->r.width = f->f_width;
  725. sel->r.height = f->f_height;
  726. return 0;
  727. case V4L2_SEL_TGT_COMPOSE_ACTIVE:
  728. sel->r = f->rect;
  729. return 0;
  730. }
  731. return -EINVAL;
  732. }
  733. static int fimc_lite_s_selection(struct file *file, void *fh,
  734. struct v4l2_selection *sel)
  735. {
  736. struct fimc_lite *fimc = video_drvdata(file);
  737. struct flite_frame *f = &fimc->out_frame;
  738. struct v4l2_rect rect = sel->r;
  739. unsigned long flags;
  740. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
  741. sel->target != V4L2_SEL_TGT_COMPOSE_ACTIVE)
  742. return -EINVAL;
  743. fimc_lite_try_compose(fimc, &rect);
  744. if ((sel->flags & V4L2_SEL_FLAG_LE) &&
  745. !enclosed_rectangle(&rect, &sel->r))
  746. return -ERANGE;
  747. if ((sel->flags & V4L2_SEL_FLAG_GE) &&
  748. !enclosed_rectangle(&sel->r, &rect))
  749. return -ERANGE;
  750. sel->r = rect;
  751. spin_lock_irqsave(&fimc->slock, flags);
  752. f->rect = rect;
  753. set_bit(ST_FLITE_CONFIG, &fimc->state);
  754. spin_unlock_irqrestore(&fimc->slock, flags);
  755. return 0;
  756. }
  757. static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = {
  758. .vidioc_querycap = fimc_vidioc_querycap_capture,
  759. .vidioc_enum_fmt_vid_cap_mplane = fimc_lite_enum_fmt_mplane,
  760. .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane,
  761. .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane,
  762. .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane,
  763. .vidioc_g_selection = fimc_lite_g_selection,
  764. .vidioc_s_selection = fimc_lite_s_selection,
  765. .vidioc_reqbufs = fimc_lite_reqbufs,
  766. .vidioc_querybuf = fimc_lite_querybuf,
  767. .vidioc_prepare_buf = fimc_lite_prepare_buf,
  768. .vidioc_create_bufs = fimc_lite_create_bufs,
  769. .vidioc_qbuf = fimc_lite_qbuf,
  770. .vidioc_dqbuf = fimc_lite_dqbuf,
  771. .vidioc_streamon = fimc_lite_streamon,
  772. .vidioc_streamoff = fimc_lite_streamoff,
  773. };
  774. /* Capture subdev media entity operations */
  775. static int fimc_lite_link_setup(struct media_entity *entity,
  776. const struct media_pad *local,
  777. const struct media_pad *remote, u32 flags)
  778. {
  779. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  780. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  781. unsigned int remote_ent_type = media_entity_type(remote->entity);
  782. if (WARN_ON(fimc == NULL))
  783. return 0;
  784. v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x",
  785. __func__, local->entity->name, remote->entity->name,
  786. flags, fimc->source_subdev_grp_id);
  787. switch (local->index) {
  788. case FIMC_SD_PAD_SINK:
  789. if (remote_ent_type != MEDIA_ENT_T_V4L2_SUBDEV)
  790. return -EINVAL;
  791. if (flags & MEDIA_LNK_FL_ENABLED) {
  792. if (fimc->source_subdev_grp_id != 0)
  793. return -EBUSY;
  794. fimc->source_subdev_grp_id = sd->grp_id;
  795. return 0;
  796. }
  797. fimc->source_subdev_grp_id = 0;
  798. break;
  799. case FIMC_SD_PAD_SOURCE:
  800. if (!(flags & MEDIA_LNK_FL_ENABLED)) {
  801. fimc->out_path = FIMC_IO_NONE;
  802. return 0;
  803. }
  804. if (remote_ent_type == MEDIA_ENT_T_V4L2_SUBDEV)
  805. fimc->out_path = FIMC_IO_ISP;
  806. else
  807. fimc->out_path = FIMC_IO_DMA;
  808. break;
  809. default:
  810. v4l2_err(sd, "Invalid pad index\n");
  811. return -EINVAL;
  812. }
  813. return 0;
  814. }
  815. static const struct media_entity_operations fimc_lite_subdev_media_ops = {
  816. .link_setup = fimc_lite_link_setup,
  817. };
  818. static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  819. struct v4l2_subdev_fh *fh,
  820. struct v4l2_subdev_mbus_code_enum *code)
  821. {
  822. const struct fimc_fmt *fmt;
  823. fmt = fimc_lite_find_format(NULL, NULL, code->index);
  824. if (!fmt)
  825. return -EINVAL;
  826. code->code = fmt->mbus_code;
  827. return 0;
  828. }
  829. static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd,
  830. struct v4l2_subdev_fh *fh,
  831. struct v4l2_subdev_format *fmt)
  832. {
  833. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  834. struct v4l2_mbus_framefmt *mf = &fmt->format;
  835. struct flite_frame *f = &fimc->out_frame;
  836. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  837. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  838. fmt->format = *mf;
  839. return 0;
  840. }
  841. mf->colorspace = V4L2_COLORSPACE_JPEG;
  842. mutex_lock(&fimc->lock);
  843. mf->code = fimc->fmt->mbus_code;
  844. if (fmt->pad == FLITE_SD_PAD_SINK) {
  845. /* full camera input frame size */
  846. mf->width = f->f_width;
  847. mf->height = f->f_height;
  848. } else {
  849. /* crop size */
  850. mf->width = f->rect.width;
  851. mf->height = f->rect.height;
  852. }
  853. mutex_unlock(&fimc->lock);
  854. return 0;
  855. }
  856. static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd,
  857. struct v4l2_subdev_fh *fh,
  858. struct v4l2_subdev_format *fmt)
  859. {
  860. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  861. struct v4l2_mbus_framefmt *mf = &fmt->format;
  862. struct flite_frame *sink = &fimc->inp_frame;
  863. const struct fimc_fmt *ffmt;
  864. v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d",
  865. fmt->pad, mf->code, mf->width, mf->height);
  866. mf->colorspace = V4L2_COLORSPACE_JPEG;
  867. mutex_lock(&fimc->lock);
  868. if ((fimc->out_path == FIMC_IO_ISP && sd->entity.stream_count > 0) ||
  869. (fimc->out_path == FIMC_IO_DMA && vb2_is_busy(&fimc->vb_queue))) {
  870. mutex_unlock(&fimc->lock);
  871. return -EBUSY;
  872. }
  873. ffmt = fimc_lite_try_format(fimc, &mf->width, &mf->height,
  874. &mf->code, NULL, fmt->pad);
  875. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  876. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  877. *mf = fmt->format;
  878. mutex_unlock(&fimc->lock);
  879. return 0;
  880. }
  881. if (fmt->pad == FLITE_SD_PAD_SINK) {
  882. sink->f_width = mf->width;
  883. sink->f_height = mf->height;
  884. fimc->fmt = ffmt;
  885. /* Set sink crop rectangle */
  886. sink->rect.width = mf->width;
  887. sink->rect.height = mf->height;
  888. sink->rect.left = 0;
  889. sink->rect.top = 0;
  890. /* Reset source crop rectangle */
  891. fimc->out_frame.rect = sink->rect;
  892. } else {
  893. /* Allow changing format only on sink pad */
  894. mf->code = fimc->fmt->mbus_code;
  895. mf->width = sink->rect.width;
  896. mf->height = sink->rect.height;
  897. }
  898. mutex_unlock(&fimc->lock);
  899. return 0;
  900. }
  901. static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd,
  902. struct v4l2_subdev_fh *fh,
  903. struct v4l2_subdev_selection *sel)
  904. {
  905. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  906. struct flite_frame *f = &fimc->inp_frame;
  907. if ((sel->target != V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL &&
  908. sel->target != V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS) ||
  909. sel->pad != FLITE_SD_PAD_SINK)
  910. return -EINVAL;
  911. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  912. sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
  913. return 0;
  914. }
  915. mutex_lock(&fimc->lock);
  916. if (sel->target == V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL) {
  917. sel->r = f->rect;
  918. } else {
  919. sel->r.left = 0;
  920. sel->r.top = 0;
  921. sel->r.width = f->f_width;
  922. sel->r.height = f->f_height;
  923. }
  924. mutex_unlock(&fimc->lock);
  925. v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d",
  926. __func__, f->rect.left, f->rect.top, f->rect.width,
  927. f->rect.height, f->f_width, f->f_height);
  928. return 0;
  929. }
  930. static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd,
  931. struct v4l2_subdev_fh *fh,
  932. struct v4l2_subdev_selection *sel)
  933. {
  934. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  935. struct flite_frame *f = &fimc->inp_frame;
  936. int ret = 0;
  937. if (sel->target != V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL ||
  938. sel->pad != FLITE_SD_PAD_SINK)
  939. return -EINVAL;
  940. mutex_lock(&fimc->lock);
  941. fimc_lite_try_crop(fimc, &sel->r);
  942. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  943. *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r;
  944. } else {
  945. unsigned long flags;
  946. spin_lock_irqsave(&fimc->slock, flags);
  947. f->rect = sel->r;
  948. /* Same crop rectangle on the source pad */
  949. fimc->out_frame.rect = sel->r;
  950. set_bit(ST_FLITE_CONFIG, &fimc->state);
  951. spin_unlock_irqrestore(&fimc->slock, flags);
  952. }
  953. mutex_unlock(&fimc->lock);
  954. v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d",
  955. __func__, f->rect.left, f->rect.top, f->rect.width,
  956. f->rect.height, f->f_width, f->f_height);
  957. return ret;
  958. }
  959. static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on)
  960. {
  961. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  962. if (fimc->out_path == FIMC_IO_DMA)
  963. return -ENOIOCTLCMD;
  964. /* TODO: */
  965. return 0;
  966. }
  967. static int fimc_lite_subdev_s_power(struct v4l2_subdev *sd, int on)
  968. {
  969. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  970. if (fimc->out_path == FIMC_IO_DMA)
  971. return -ENOIOCTLCMD;
  972. /* TODO: */
  973. return 0;
  974. }
  975. static int fimc_lite_log_status(struct v4l2_subdev *sd)
  976. {
  977. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  978. flite_hw_dump_regs(fimc, __func__);
  979. return 0;
  980. }
  981. static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
  982. {
  983. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  984. struct vb2_queue *q = &fimc->vb_queue;
  985. struct video_device *vfd;
  986. int ret;
  987. fimc->fmt = &fimc_lite_formats[0];
  988. fimc->out_path = FIMC_IO_DMA;
  989. vfd = video_device_alloc();
  990. if (!vfd) {
  991. v4l2_err(sd->v4l2_dev, "Failed to allocate video device\n");
  992. return -ENOMEM;
  993. }
  994. snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture",
  995. fimc->index);
  996. vfd->fops = &fimc_lite_fops;
  997. vfd->ioctl_ops = &fimc_lite_ioctl_ops;
  998. vfd->v4l2_dev = sd->v4l2_dev;
  999. vfd->minor = -1;
  1000. vfd->release = video_device_release;
  1001. vfd->lock = &fimc->lock;
  1002. fimc->vfd = vfd;
  1003. fimc->ref_count = 0;
  1004. fimc->reqbufs_count = 0;
  1005. INIT_LIST_HEAD(&fimc->pending_buf_q);
  1006. INIT_LIST_HEAD(&fimc->active_buf_q);
  1007. memset(q, 0, sizeof(*q));
  1008. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1009. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1010. q->ops = &fimc_lite_qops;
  1011. q->mem_ops = &vb2_dma_contig_memops;
  1012. q->buf_struct_size = sizeof(struct flite_buffer);
  1013. q->drv_priv = fimc;
  1014. vb2_queue_init(q);
  1015. fimc->vd_pad.flags = MEDIA_PAD_FL_SINK;
  1016. ret = media_entity_init(&vfd->entity, 1, &fimc->vd_pad, 0);
  1017. if (ret)
  1018. goto err;
  1019. video_set_drvdata(vfd, fimc);
  1020. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  1021. if (ret)
  1022. goto err_vd;
  1023. v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n",
  1024. vfd->name, video_device_node_name(vfd));
  1025. return 0;
  1026. err_vd:
  1027. media_entity_cleanup(&vfd->entity);
  1028. err:
  1029. video_device_release(vfd);
  1030. return ret;
  1031. }
  1032. static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd)
  1033. {
  1034. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  1035. if (fimc == NULL)
  1036. return;
  1037. if (fimc->vfd) {
  1038. video_unregister_device(fimc->vfd);
  1039. media_entity_cleanup(&fimc->vfd->entity);
  1040. fimc->vfd = NULL;
  1041. }
  1042. }
  1043. static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = {
  1044. .registered = fimc_lite_subdev_registered,
  1045. .unregistered = fimc_lite_subdev_unregistered,
  1046. };
  1047. static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = {
  1048. .enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
  1049. .get_selection = fimc_lite_subdev_get_selection,
  1050. .set_selection = fimc_lite_subdev_set_selection,
  1051. .get_fmt = fimc_lite_subdev_get_fmt,
  1052. .set_fmt = fimc_lite_subdev_set_fmt,
  1053. };
  1054. static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = {
  1055. .s_stream = fimc_lite_subdev_s_stream,
  1056. };
  1057. static const struct v4l2_subdev_core_ops fimc_lite_core_ops = {
  1058. .s_power = fimc_lite_subdev_s_power,
  1059. .log_status = fimc_lite_log_status,
  1060. };
  1061. static struct v4l2_subdev_ops fimc_lite_subdev_ops = {
  1062. .core = &fimc_lite_core_ops,
  1063. .video = &fimc_lite_subdev_video_ops,
  1064. .pad = &fimc_lite_subdev_pad_ops,
  1065. };
  1066. static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl)
  1067. {
  1068. struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite,
  1069. ctrl_handler);
  1070. set_bit(ST_FLITE_CONFIG, &fimc->state);
  1071. return 0;
  1072. }
  1073. static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = {
  1074. .s_ctrl = fimc_lite_s_ctrl,
  1075. };
  1076. static const struct v4l2_ctrl_config fimc_lite_ctrl = {
  1077. .ops = &fimc_lite_ctrl_ops,
  1078. .id = V4L2_CTRL_CLASS_USER | 0x1001,
  1079. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1080. .name = "Test Pattern 640x480",
  1081. };
  1082. static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc)
  1083. {
  1084. struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler;
  1085. struct v4l2_subdev *sd = &fimc->subdev;
  1086. int ret;
  1087. v4l2_subdev_init(sd, &fimc_lite_subdev_ops);
  1088. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  1089. snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index);
  1090. fimc->subdev_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1091. fimc->subdev_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1092. ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
  1093. fimc->subdev_pads, 0);
  1094. if (ret)
  1095. return ret;
  1096. v4l2_ctrl_handler_init(handler, 1);
  1097. fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl,
  1098. NULL);
  1099. if (handler->error) {
  1100. media_entity_cleanup(&sd->entity);
  1101. return handler->error;
  1102. }
  1103. sd->ctrl_handler = handler;
  1104. sd->internal_ops = &fimc_lite_subdev_internal_ops;
  1105. sd->entity.ops = &fimc_lite_subdev_media_ops;
  1106. v4l2_set_subdevdata(sd, fimc);
  1107. return 0;
  1108. }
  1109. static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc)
  1110. {
  1111. struct v4l2_subdev *sd = &fimc->subdev;
  1112. v4l2_device_unregister_subdev(sd);
  1113. media_entity_cleanup(&sd->entity);
  1114. v4l2_ctrl_handler_free(&fimc->ctrl_handler);
  1115. v4l2_set_subdevdata(sd, NULL);
  1116. }
  1117. static void fimc_lite_clk_put(struct fimc_lite *fimc)
  1118. {
  1119. if (IS_ERR_OR_NULL(fimc->clock))
  1120. return;
  1121. clk_unprepare(fimc->clock);
  1122. clk_put(fimc->clock);
  1123. fimc->clock = NULL;
  1124. }
  1125. static int fimc_lite_clk_get(struct fimc_lite *fimc)
  1126. {
  1127. int ret;
  1128. fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME);
  1129. if (IS_ERR(fimc->clock))
  1130. return PTR_ERR(fimc->clock);
  1131. ret = clk_prepare(fimc->clock);
  1132. if (ret < 0) {
  1133. clk_put(fimc->clock);
  1134. fimc->clock = NULL;
  1135. }
  1136. return ret;
  1137. }
  1138. static int __devinit fimc_lite_probe(struct platform_device *pdev)
  1139. {
  1140. struct flite_drvdata *drv_data = fimc_lite_get_drvdata(pdev);
  1141. struct fimc_lite *fimc;
  1142. struct resource *res;
  1143. int ret;
  1144. fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL);
  1145. if (!fimc)
  1146. return -ENOMEM;
  1147. fimc->index = pdev->id;
  1148. fimc->variant = drv_data->variant[fimc->index];
  1149. fimc->pdev = pdev;
  1150. init_waitqueue_head(&fimc->irq_queue);
  1151. spin_lock_init(&fimc->slock);
  1152. mutex_init(&fimc->lock);
  1153. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1154. fimc->regs = devm_request_and_ioremap(&pdev->dev, res);
  1155. if (fimc->regs == NULL) {
  1156. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  1157. return -ENOENT;
  1158. }
  1159. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1160. if (res == NULL) {
  1161. dev_err(&pdev->dev, "Failed to get IRQ resource\n");
  1162. return -ENXIO;
  1163. }
  1164. ret = fimc_lite_clk_get(fimc);
  1165. if (ret)
  1166. return ret;
  1167. ret = devm_request_irq(&pdev->dev, res->start, flite_irq_handler,
  1168. 0, dev_name(&pdev->dev), fimc);
  1169. if (ret) {
  1170. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  1171. goto err_clk;
  1172. }
  1173. /* The video node will be created within the subdev's registered() op */
  1174. ret = fimc_lite_create_capture_subdev(fimc);
  1175. if (ret)
  1176. goto err_clk;
  1177. platform_set_drvdata(pdev, fimc);
  1178. pm_runtime_enable(&pdev->dev);
  1179. ret = pm_runtime_get_sync(&pdev->dev);
  1180. if (ret < 0)
  1181. goto err_sd;
  1182. fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1183. if (IS_ERR(fimc->alloc_ctx)) {
  1184. ret = PTR_ERR(fimc->alloc_ctx);
  1185. goto err_pm;
  1186. }
  1187. pm_runtime_put(&pdev->dev);
  1188. dev_dbg(&pdev->dev, "FIMC-LITE.%d registered successfully\n",
  1189. fimc->index);
  1190. return 0;
  1191. err_pm:
  1192. pm_runtime_put(&pdev->dev);
  1193. err_sd:
  1194. fimc_lite_unregister_capture_subdev(fimc);
  1195. err_clk:
  1196. fimc_lite_clk_put(fimc);
  1197. return ret;
  1198. }
  1199. static int fimc_lite_runtime_resume(struct device *dev)
  1200. {
  1201. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1202. clk_enable(fimc->clock);
  1203. return 0;
  1204. }
  1205. static int fimc_lite_runtime_suspend(struct device *dev)
  1206. {
  1207. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1208. clk_disable(fimc->clock);
  1209. return 0;
  1210. }
  1211. #ifdef CONFIG_PM_SLEEP
  1212. static int fimc_lite_resume(struct device *dev)
  1213. {
  1214. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1215. struct flite_buffer *buf;
  1216. unsigned long flags;
  1217. int i;
  1218. spin_lock_irqsave(&fimc->slock, flags);
  1219. if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
  1220. !test_bit(ST_FLITE_IN_USE, &fimc->state)) {
  1221. spin_unlock_irqrestore(&fimc->slock, flags);
  1222. return 0;
  1223. }
  1224. flite_hw_reset(fimc);
  1225. spin_unlock_irqrestore(&fimc->slock, flags);
  1226. if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state))
  1227. return 0;
  1228. INIT_LIST_HEAD(&fimc->active_buf_q);
  1229. fimc_pipeline_initialize(&fimc->pipeline, &fimc->vfd->entity, false);
  1230. fimc_lite_hw_init(fimc);
  1231. clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
  1232. for (i = 0; i < fimc->reqbufs_count; i++) {
  1233. if (list_empty(&fimc->pending_buf_q))
  1234. break;
  1235. buf = fimc_lite_pending_queue_pop(fimc);
  1236. buffer_queue(&buf->vb);
  1237. }
  1238. return 0;
  1239. }
  1240. static int fimc_lite_suspend(struct device *dev)
  1241. {
  1242. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1243. bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state);
  1244. int ret;
  1245. if (test_and_set_bit(ST_LPM, &fimc->state))
  1246. return 0;
  1247. ret = fimc_lite_stop_capture(fimc, suspend);
  1248. if (ret < 0 || !fimc_lite_active(fimc))
  1249. return ret;
  1250. return fimc_pipeline_shutdown(&fimc->pipeline);
  1251. }
  1252. #endif /* CONFIG_PM_SLEEP */
  1253. static int __devexit fimc_lite_remove(struct platform_device *pdev)
  1254. {
  1255. struct fimc_lite *fimc = platform_get_drvdata(pdev);
  1256. struct device *dev = &pdev->dev;
  1257. pm_runtime_disable(dev);
  1258. pm_runtime_set_suspended(dev);
  1259. fimc_lite_unregister_capture_subdev(fimc);
  1260. vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
  1261. fimc_lite_clk_put(fimc);
  1262. dev_info(dev, "Driver unloaded\n");
  1263. return 0;
  1264. }
  1265. static struct flite_variant fimc_lite0_variant_exynos4 = {
  1266. .max_width = 8192,
  1267. .max_height = 8192,
  1268. .out_width_align = 8,
  1269. .win_hor_offs_align = 2,
  1270. .out_hor_offs_align = 8,
  1271. };
  1272. /* EXYNOS4212, EXYNOS4412 */
  1273. static struct flite_drvdata fimc_lite_drvdata_exynos4 = {
  1274. .variant = {
  1275. [0] = &fimc_lite0_variant_exynos4,
  1276. [1] = &fimc_lite0_variant_exynos4,
  1277. },
  1278. };
  1279. static struct platform_device_id fimc_lite_driver_ids[] = {
  1280. {
  1281. .name = "exynos-fimc-lite",
  1282. .driver_data = (unsigned long)&fimc_lite_drvdata_exynos4,
  1283. },
  1284. { /* sentinel */ },
  1285. };
  1286. MODULE_DEVICE_TABLE(platform, fimc_lite_driver_ids);
  1287. static const struct dev_pm_ops fimc_lite_pm_ops = {
  1288. SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume)
  1289. SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume,
  1290. NULL)
  1291. };
  1292. static struct platform_driver fimc_lite_driver = {
  1293. .probe = fimc_lite_probe,
  1294. .remove = __devexit_p(fimc_lite_remove),
  1295. .id_table = fimc_lite_driver_ids,
  1296. .driver = {
  1297. .name = FIMC_LITE_DRV_NAME,
  1298. .owner = THIS_MODULE,
  1299. .pm = &fimc_lite_pm_ops,
  1300. }
  1301. };
  1302. module_platform_driver(fimc_lite_driver);
  1303. MODULE_LICENSE("GPL");
  1304. MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME);