fimc-capture.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  1. /*
  2. * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
  3. *
  4. * Copyright (C) 2010 - 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. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/bug.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/list.h>
  20. #include <linux/slab.h>
  21. #include <linux/videodev2.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-mem2mem.h>
  25. #include <media/videobuf2-core.h>
  26. #include <media/videobuf2-dma-contig.h>
  27. #include "fimc-mdevice.h"
  28. #include "fimc-core.h"
  29. #include "fimc-reg.h"
  30. static int fimc_capture_hw_init(struct fimc_dev *fimc)
  31. {
  32. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  33. struct fimc_pipeline *p = &fimc->pipeline;
  34. struct fimc_sensor_info *sensor;
  35. unsigned long flags;
  36. int ret = 0;
  37. if (p->subdevs[IDX_SENSOR] == NULL || ctx == NULL)
  38. return -ENXIO;
  39. if (ctx->s_frame.fmt == NULL)
  40. return -EINVAL;
  41. sensor = v4l2_get_subdev_hostdata(p->subdevs[IDX_SENSOR]);
  42. spin_lock_irqsave(&fimc->slock, flags);
  43. fimc_prepare_dma_offset(ctx, &ctx->d_frame);
  44. fimc_set_yuv_order(ctx);
  45. fimc_hw_set_camera_polarity(fimc, sensor->pdata);
  46. fimc_hw_set_camera_type(fimc, sensor->pdata);
  47. fimc_hw_set_camera_source(fimc, sensor->pdata);
  48. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  49. ret = fimc_set_scaler_info(ctx);
  50. if (!ret) {
  51. fimc_hw_set_input_path(ctx);
  52. fimc_hw_set_prescaler(ctx);
  53. fimc_hw_set_mainscaler(ctx);
  54. fimc_hw_set_target_format(ctx);
  55. fimc_hw_set_rotation(ctx);
  56. fimc_hw_set_effect(ctx);
  57. fimc_hw_set_output_path(ctx);
  58. fimc_hw_set_out_dma(ctx);
  59. if (fimc->variant->has_alpha)
  60. fimc_hw_set_rgb_alpha(ctx);
  61. clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  62. }
  63. spin_unlock_irqrestore(&fimc->slock, flags);
  64. return ret;
  65. }
  66. /*
  67. * Reinitialize the driver so it is ready to start the streaming again.
  68. * Set fimc->state to indicate stream off and the hardware shut down state.
  69. * If not suspending (@suspend is false), return any buffers to videobuf2.
  70. * Otherwise put any owned buffers onto the pending buffers queue, so they
  71. * can be re-spun when the device is being resumed. Also perform FIMC
  72. * software reset and disable streaming on the whole pipeline if required.
  73. */
  74. static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
  75. {
  76. struct fimc_vid_cap *cap = &fimc->vid_cap;
  77. struct fimc_vid_buffer *buf;
  78. unsigned long flags;
  79. bool streaming;
  80. spin_lock_irqsave(&fimc->slock, flags);
  81. streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
  82. fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
  83. 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
  84. if (suspend)
  85. fimc->state |= (1 << ST_CAPT_SUSPENDED);
  86. else
  87. fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
  88. /* Release unused buffers */
  89. while (!suspend && !list_empty(&cap->pending_buf_q)) {
  90. buf = fimc_pending_queue_pop(cap);
  91. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  92. }
  93. /* If suspending put unused buffers onto pending queue */
  94. while (!list_empty(&cap->active_buf_q)) {
  95. buf = fimc_active_queue_pop(cap);
  96. if (suspend)
  97. fimc_pending_queue_add(cap, buf);
  98. else
  99. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  100. }
  101. fimc_hw_reset(fimc);
  102. cap->buf_index = 0;
  103. spin_unlock_irqrestore(&fimc->slock, flags);
  104. if (streaming)
  105. return fimc_pipeline_s_stream(&fimc->pipeline, 0);
  106. else
  107. return 0;
  108. }
  109. static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
  110. {
  111. unsigned long flags;
  112. if (!fimc_capture_active(fimc))
  113. return 0;
  114. spin_lock_irqsave(&fimc->slock, flags);
  115. set_bit(ST_CAPT_SHUT, &fimc->state);
  116. fimc_deactivate_capture(fimc);
  117. spin_unlock_irqrestore(&fimc->slock, flags);
  118. wait_event_timeout(fimc->irq_queue,
  119. !test_bit(ST_CAPT_SHUT, &fimc->state),
  120. (2*HZ/10)); /* 200 ms */
  121. return fimc_capture_state_cleanup(fimc, suspend);
  122. }
  123. /**
  124. * fimc_capture_config_update - apply the camera interface configuration
  125. *
  126. * To be called from within the interrupt handler with fimc.slock
  127. * spinlock held. It updates the camera pixel crop, rotation and
  128. * image flip in H/W.
  129. */
  130. static int fimc_capture_config_update(struct fimc_ctx *ctx)
  131. {
  132. struct fimc_dev *fimc = ctx->fimc_dev;
  133. int ret;
  134. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  135. ret = fimc_set_scaler_info(ctx);
  136. if (ret)
  137. return ret;
  138. fimc_hw_set_prescaler(ctx);
  139. fimc_hw_set_mainscaler(ctx);
  140. fimc_hw_set_target_format(ctx);
  141. fimc_hw_set_rotation(ctx);
  142. fimc_hw_set_effect(ctx);
  143. fimc_prepare_dma_offset(ctx, &ctx->d_frame);
  144. fimc_hw_set_out_dma(ctx);
  145. if (fimc->variant->has_alpha)
  146. fimc_hw_set_rgb_alpha(ctx);
  147. clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  148. return ret;
  149. }
  150. void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
  151. {
  152. struct fimc_vid_cap *cap = &fimc->vid_cap;
  153. struct fimc_vid_buffer *v_buf;
  154. struct timeval *tv;
  155. struct timespec ts;
  156. if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
  157. wake_up(&fimc->irq_queue);
  158. goto done;
  159. }
  160. if (!list_empty(&cap->active_buf_q) &&
  161. test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
  162. ktime_get_real_ts(&ts);
  163. v_buf = fimc_active_queue_pop(cap);
  164. tv = &v_buf->vb.v4l2_buf.timestamp;
  165. tv->tv_sec = ts.tv_sec;
  166. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  167. v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
  168. vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
  169. }
  170. if (!list_empty(&cap->pending_buf_q)) {
  171. v_buf = fimc_pending_queue_pop(cap);
  172. fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
  173. v_buf->index = cap->buf_index;
  174. /* Move the buffer to the capture active queue */
  175. fimc_active_queue_add(cap, v_buf);
  176. dbg("next frame: %d, done frame: %d",
  177. fimc_hw_get_frame_index(fimc), v_buf->index);
  178. if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
  179. cap->buf_index = 0;
  180. }
  181. if (cap->active_buf_cnt == 0) {
  182. if (deq_buf)
  183. clear_bit(ST_CAPT_RUN, &fimc->state);
  184. if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
  185. cap->buf_index = 0;
  186. } else {
  187. set_bit(ST_CAPT_RUN, &fimc->state);
  188. }
  189. if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
  190. fimc_capture_config_update(cap->ctx);
  191. done:
  192. if (cap->active_buf_cnt == 1) {
  193. fimc_deactivate_capture(fimc);
  194. clear_bit(ST_CAPT_STREAM, &fimc->state);
  195. }
  196. dbg("frame: %d, active_buf_cnt: %d",
  197. fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
  198. }
  199. static int start_streaming(struct vb2_queue *q, unsigned int count)
  200. {
  201. struct fimc_ctx *ctx = q->drv_priv;
  202. struct fimc_dev *fimc = ctx->fimc_dev;
  203. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  204. int min_bufs;
  205. int ret;
  206. vid_cap->frame_count = 0;
  207. ret = fimc_capture_hw_init(fimc);
  208. if (ret) {
  209. fimc_capture_state_cleanup(fimc, false);
  210. return ret;
  211. }
  212. set_bit(ST_CAPT_PEND, &fimc->state);
  213. min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
  214. if (vid_cap->active_buf_cnt >= min_bufs &&
  215. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
  216. fimc_activate_capture(ctx);
  217. if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
  218. fimc_pipeline_s_stream(&fimc->pipeline, 1);
  219. }
  220. return 0;
  221. }
  222. static int stop_streaming(struct vb2_queue *q)
  223. {
  224. struct fimc_ctx *ctx = q->drv_priv;
  225. struct fimc_dev *fimc = ctx->fimc_dev;
  226. if (!fimc_capture_active(fimc))
  227. return -EINVAL;
  228. return fimc_stop_capture(fimc, false);
  229. }
  230. int fimc_capture_suspend(struct fimc_dev *fimc)
  231. {
  232. bool suspend = fimc_capture_busy(fimc);
  233. int ret = fimc_stop_capture(fimc, suspend);
  234. if (ret)
  235. return ret;
  236. return fimc_pipeline_shutdown(&fimc->pipeline);
  237. }
  238. static void buffer_queue(struct vb2_buffer *vb);
  239. int fimc_capture_resume(struct fimc_dev *fimc)
  240. {
  241. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  242. struct fimc_vid_buffer *buf;
  243. int i;
  244. if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
  245. return 0;
  246. INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
  247. vid_cap->buf_index = 0;
  248. fimc_pipeline_initialize(&fimc->pipeline, &vid_cap->vfd->entity,
  249. false);
  250. fimc_capture_hw_init(fimc);
  251. clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
  252. for (i = 0; i < vid_cap->reqbufs_count; i++) {
  253. if (list_empty(&vid_cap->pending_buf_q))
  254. break;
  255. buf = fimc_pending_queue_pop(vid_cap);
  256. buffer_queue(&buf->vb);
  257. }
  258. return 0;
  259. }
  260. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
  261. unsigned int *num_buffers, unsigned int *num_planes,
  262. unsigned int sizes[], void *allocators[])
  263. {
  264. const struct v4l2_pix_format_mplane *pixm = NULL;
  265. struct fimc_ctx *ctx = vq->drv_priv;
  266. struct fimc_frame *frame = &ctx->d_frame;
  267. struct fimc_fmt *fmt = frame->fmt;
  268. unsigned long wh;
  269. int i;
  270. if (pfmt) {
  271. pixm = &pfmt->fmt.pix_mp;
  272. fmt = fimc_find_format(&pixm->pixelformat, NULL,
  273. FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
  274. wh = pixm->width * pixm->height;
  275. } else {
  276. wh = frame->f_width * frame->f_height;
  277. }
  278. if (fmt == NULL)
  279. return -EINVAL;
  280. *num_planes = fmt->memplanes;
  281. for (i = 0; i < fmt->memplanes; i++) {
  282. unsigned int size = (wh * fmt->depth[i]) / 8;
  283. if (pixm)
  284. sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
  285. else
  286. sizes[i] = max_t(u32, size, frame->payload[i]);
  287. allocators[i] = ctx->fimc_dev->alloc_ctx;
  288. }
  289. return 0;
  290. }
  291. static int buffer_prepare(struct vb2_buffer *vb)
  292. {
  293. struct vb2_queue *vq = vb->vb2_queue;
  294. struct fimc_ctx *ctx = vq->drv_priv;
  295. int i;
  296. if (ctx->d_frame.fmt == NULL)
  297. return -EINVAL;
  298. for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
  299. unsigned long size = ctx->d_frame.payload[i];
  300. if (vb2_plane_size(vb, i) < size) {
  301. v4l2_err(ctx->fimc_dev->vid_cap.vfd,
  302. "User buffer too small (%ld < %ld)\n",
  303. vb2_plane_size(vb, i), size);
  304. return -EINVAL;
  305. }
  306. vb2_set_plane_payload(vb, i, size);
  307. }
  308. return 0;
  309. }
  310. static void buffer_queue(struct vb2_buffer *vb)
  311. {
  312. struct fimc_vid_buffer *buf
  313. = container_of(vb, struct fimc_vid_buffer, vb);
  314. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  315. struct fimc_dev *fimc = ctx->fimc_dev;
  316. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  317. unsigned long flags;
  318. int min_bufs;
  319. spin_lock_irqsave(&fimc->slock, flags);
  320. fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
  321. if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
  322. !test_bit(ST_CAPT_STREAM, &fimc->state) &&
  323. vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
  324. /* Setup the buffer directly for processing. */
  325. int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
  326. vid_cap->buf_index;
  327. fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
  328. buf->index = vid_cap->buf_index;
  329. fimc_active_queue_add(vid_cap, buf);
  330. if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
  331. vid_cap->buf_index = 0;
  332. } else {
  333. fimc_pending_queue_add(vid_cap, buf);
  334. }
  335. min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
  336. if (vb2_is_streaming(&vid_cap->vbq) &&
  337. vid_cap->active_buf_cnt >= min_bufs &&
  338. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
  339. fimc_activate_capture(ctx);
  340. spin_unlock_irqrestore(&fimc->slock, flags);
  341. if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
  342. fimc_pipeline_s_stream(&fimc->pipeline, 1);
  343. return;
  344. }
  345. spin_unlock_irqrestore(&fimc->slock, flags);
  346. }
  347. static void fimc_lock(struct vb2_queue *vq)
  348. {
  349. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  350. mutex_lock(&ctx->fimc_dev->lock);
  351. }
  352. static void fimc_unlock(struct vb2_queue *vq)
  353. {
  354. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  355. mutex_unlock(&ctx->fimc_dev->lock);
  356. }
  357. static struct vb2_ops fimc_capture_qops = {
  358. .queue_setup = queue_setup,
  359. .buf_prepare = buffer_prepare,
  360. .buf_queue = buffer_queue,
  361. .wait_prepare = fimc_unlock,
  362. .wait_finish = fimc_lock,
  363. .start_streaming = start_streaming,
  364. .stop_streaming = stop_streaming,
  365. };
  366. /**
  367. * fimc_capture_ctrls_create - initialize the control handler
  368. * Initialize the capture video node control handler and fill it
  369. * with the FIMC controls. Inherit any sensor's controls if the
  370. * 'user_subdev_api' flag is false (default behaviour).
  371. * This function need to be called with the graph mutex held.
  372. */
  373. int fimc_capture_ctrls_create(struct fimc_dev *fimc)
  374. {
  375. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  376. int ret;
  377. if (WARN_ON(vid_cap->ctx == NULL))
  378. return -ENXIO;
  379. if (vid_cap->ctx->ctrls.ready)
  380. return 0;
  381. ret = fimc_ctrls_create(vid_cap->ctx);
  382. if (ret || vid_cap->user_subdev_api || !vid_cap->ctx->ctrls.ready)
  383. return ret;
  384. return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrls.handler,
  385. fimc->pipeline.subdevs[IDX_SENSOR]->ctrl_handler);
  386. }
  387. static int fimc_capture_set_default_format(struct fimc_dev *fimc);
  388. static int fimc_capture_open(struct file *file)
  389. {
  390. struct fimc_dev *fimc = video_drvdata(file);
  391. int ret;
  392. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  393. if (fimc_m2m_active(fimc))
  394. return -EBUSY;
  395. set_bit(ST_CAPT_BUSY, &fimc->state);
  396. ret = pm_runtime_get_sync(&fimc->pdev->dev);
  397. if (ret < 0)
  398. return ret;
  399. ret = v4l2_fh_open(file);
  400. if (ret)
  401. return ret;
  402. if (++fimc->vid_cap.refcnt != 1)
  403. return 0;
  404. ret = fimc_pipeline_initialize(&fimc->pipeline,
  405. &fimc->vid_cap.vfd->entity, true);
  406. if (ret < 0) {
  407. clear_bit(ST_CAPT_BUSY, &fimc->state);
  408. pm_runtime_put_sync(&fimc->pdev->dev);
  409. fimc->vid_cap.refcnt--;
  410. v4l2_fh_release(file);
  411. return ret;
  412. }
  413. ret = fimc_capture_ctrls_create(fimc);
  414. if (!ret && !fimc->vid_cap.user_subdev_api)
  415. ret = fimc_capture_set_default_format(fimc);
  416. return ret;
  417. }
  418. static int fimc_capture_close(struct file *file)
  419. {
  420. struct fimc_dev *fimc = video_drvdata(file);
  421. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  422. if (--fimc->vid_cap.refcnt == 0) {
  423. clear_bit(ST_CAPT_BUSY, &fimc->state);
  424. fimc_stop_capture(fimc, false);
  425. fimc_pipeline_shutdown(&fimc->pipeline);
  426. clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
  427. }
  428. pm_runtime_put(&fimc->pdev->dev);
  429. if (fimc->vid_cap.refcnt == 0) {
  430. vb2_queue_release(&fimc->vid_cap.vbq);
  431. fimc_ctrls_delete(fimc->vid_cap.ctx);
  432. }
  433. return v4l2_fh_release(file);
  434. }
  435. static unsigned int fimc_capture_poll(struct file *file,
  436. struct poll_table_struct *wait)
  437. {
  438. struct fimc_dev *fimc = video_drvdata(file);
  439. return vb2_poll(&fimc->vid_cap.vbq, file, wait);
  440. }
  441. static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
  442. {
  443. struct fimc_dev *fimc = video_drvdata(file);
  444. return vb2_mmap(&fimc->vid_cap.vbq, vma);
  445. }
  446. static const struct v4l2_file_operations fimc_capture_fops = {
  447. .owner = THIS_MODULE,
  448. .open = fimc_capture_open,
  449. .release = fimc_capture_close,
  450. .poll = fimc_capture_poll,
  451. .unlocked_ioctl = video_ioctl2,
  452. .mmap = fimc_capture_mmap,
  453. };
  454. /*
  455. * Format and crop negotiation helpers
  456. */
  457. static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
  458. u32 *width, u32 *height,
  459. u32 *code, u32 *fourcc, int pad)
  460. {
  461. bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
  462. struct fimc_dev *fimc = ctx->fimc_dev;
  463. struct fimc_variant *var = fimc->variant;
  464. struct fimc_pix_limit *pl = var->pix_limit;
  465. struct fimc_frame *dst = &ctx->d_frame;
  466. u32 depth, min_w, max_w, min_h, align_h = 3;
  467. u32 mask = FMT_FLAGS_CAM;
  468. struct fimc_fmt *ffmt;
  469. /* Color conversion from/to JPEG is not supported */
  470. if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
  471. fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
  472. *code = V4L2_MBUS_FMT_JPEG_1X8;
  473. if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
  474. mask |= FMT_FLAGS_M2M;
  475. ffmt = fimc_find_format(fourcc, code, mask, 0);
  476. if (WARN_ON(!ffmt))
  477. return NULL;
  478. if (code)
  479. *code = ffmt->mbus_code;
  480. if (fourcc)
  481. *fourcc = ffmt->fourcc;
  482. if (pad == FIMC_SD_PAD_SINK) {
  483. max_w = fimc_fmt_is_jpeg(ffmt->color) ?
  484. pl->scaler_dis_w : pl->scaler_en_w;
  485. /* Apply the camera input interface pixel constraints */
  486. v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
  487. height, max_t(u32, *height, 32),
  488. FIMC_CAMIF_MAX_HEIGHT,
  489. fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
  490. 0);
  491. return ffmt;
  492. }
  493. /* Can't scale or crop in transparent (JPEG) transfer mode */
  494. if (fimc_fmt_is_jpeg(ffmt->color)) {
  495. *width = ctx->s_frame.f_width;
  496. *height = ctx->s_frame.f_height;
  497. return ffmt;
  498. }
  499. /* Apply the scaler and the output DMA constraints */
  500. max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
  501. if (ctx->state & FIMC_COMPOSE) {
  502. min_w = dst->offs_h + dst->width;
  503. min_h = dst->offs_v + dst->height;
  504. } else {
  505. min_w = var->min_out_pixsize;
  506. min_h = var->min_out_pixsize;
  507. }
  508. if (var->min_vsize_align == 1 && !rotation)
  509. align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
  510. depth = fimc_get_format_depth(ffmt);
  511. v4l_bound_align_image(width, min_w, max_w,
  512. ffs(var->min_out_pixsize) - 1,
  513. height, min_h, FIMC_CAMIF_MAX_HEIGHT,
  514. align_h,
  515. 64/(ALIGN(depth, 8)));
  516. dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
  517. pad, code ? *code : 0, *width, *height,
  518. dst->f_width, dst->f_height);
  519. return ffmt;
  520. }
  521. static void fimc_capture_try_selection(struct fimc_ctx *ctx,
  522. struct v4l2_rect *r,
  523. int target)
  524. {
  525. bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
  526. struct fimc_dev *fimc = ctx->fimc_dev;
  527. struct fimc_variant *var = fimc->variant;
  528. struct fimc_pix_limit *pl = var->pix_limit;
  529. struct fimc_frame *sink = &ctx->s_frame;
  530. u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
  531. u32 align_sz = 0, align_h = 4;
  532. u32 max_sc_h, max_sc_v;
  533. /* In JPEG transparent transfer mode cropping is not supported */
  534. if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
  535. r->width = sink->f_width;
  536. r->height = sink->f_height;
  537. r->left = r->top = 0;
  538. return;
  539. }
  540. if (target == V4L2_SEL_TGT_COMPOSE_ACTIVE) {
  541. if (ctx->rotation != 90 && ctx->rotation != 270)
  542. align_h = 1;
  543. max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
  544. max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
  545. min_sz = var->min_out_pixsize;
  546. } else {
  547. u32 depth = fimc_get_format_depth(sink->fmt);
  548. align_sz = 64/ALIGN(depth, 8);
  549. min_sz = var->min_inp_pixsize;
  550. min_w = min_h = min_sz;
  551. max_sc_h = max_sc_v = 1;
  552. }
  553. /*
  554. * For the compose rectangle the following constraints must be met:
  555. * - it must fit in the sink pad format rectangle (f_width/f_height);
  556. * - maximum downscaling ratio is 64;
  557. * - maximum crop size depends if the rotator is used or not;
  558. * - the sink pad format width/height must be 4 multiple of the
  559. * prescaler ratios determined by sink pad size and source pad crop,
  560. * the prescaler ratio is returned by fimc_get_scaler_factor().
  561. */
  562. max_w = min_t(u32,
  563. rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
  564. rotate ? sink->f_height : sink->f_width);
  565. max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
  566. if (target == V4L2_SEL_TGT_COMPOSE_ACTIVE) {
  567. min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
  568. min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
  569. if (rotate) {
  570. swap(max_sc_h, max_sc_v);
  571. swap(min_w, min_h);
  572. }
  573. }
  574. v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
  575. &r->height, min_h, max_h, align_h,
  576. align_sz);
  577. /* Adjust left/top if crop/compose rectangle is out of bounds */
  578. r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
  579. r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
  580. r->left = round_down(r->left, var->hor_offs_align);
  581. dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
  582. target, r->left, r->top, r->width, r->height,
  583. sink->f_width, sink->f_height);
  584. }
  585. /*
  586. * The video node ioctl operations
  587. */
  588. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  589. struct v4l2_capability *cap)
  590. {
  591. struct fimc_dev *fimc = video_drvdata(file);
  592. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  593. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  594. cap->bus_info[0] = 0;
  595. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  596. return 0;
  597. }
  598. static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
  599. struct v4l2_fmtdesc *f)
  600. {
  601. struct fimc_fmt *fmt;
  602. fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
  603. f->index);
  604. if (!fmt)
  605. return -EINVAL;
  606. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  607. f->pixelformat = fmt->fourcc;
  608. if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
  609. f->flags |= V4L2_FMT_FLAG_COMPRESSED;
  610. return 0;
  611. }
  612. /**
  613. * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
  614. * elements
  615. * @ctx: FIMC capture context
  616. * @tfmt: media bus format to try/set on subdevs
  617. * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
  618. * @set: true to set format on subdevs, false to try only
  619. */
  620. static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
  621. struct v4l2_mbus_framefmt *tfmt,
  622. struct fimc_fmt **fmt_id,
  623. bool set)
  624. {
  625. struct fimc_dev *fimc = ctx->fimc_dev;
  626. struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
  627. struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
  628. struct v4l2_subdev_format sfmt;
  629. struct v4l2_mbus_framefmt *mf = &sfmt.format;
  630. struct fimc_fmt *ffmt = NULL;
  631. int ret, i = 0;
  632. if (WARN_ON(!sd || !tfmt))
  633. return -EINVAL;
  634. memset(&sfmt, 0, sizeof(sfmt));
  635. sfmt.format = *tfmt;
  636. sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
  637. while (1) {
  638. ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
  639. FMT_FLAGS_CAM, i++);
  640. if (ffmt == NULL) {
  641. /*
  642. * Notify user-space if common pixel code for
  643. * host and sensor does not exist.
  644. */
  645. return -EINVAL;
  646. }
  647. mf->code = tfmt->code = ffmt->mbus_code;
  648. ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
  649. if (ret)
  650. return ret;
  651. if (mf->code != tfmt->code) {
  652. mf->code = 0;
  653. continue;
  654. }
  655. if (mf->width != tfmt->width || mf->height != tfmt->height) {
  656. u32 fcc = ffmt->fourcc;
  657. tfmt->width = mf->width;
  658. tfmt->height = mf->height;
  659. ffmt = fimc_capture_try_format(ctx,
  660. &tfmt->width, &tfmt->height,
  661. NULL, &fcc, FIMC_SD_PAD_SOURCE);
  662. if (ffmt && ffmt->mbus_code)
  663. mf->code = ffmt->mbus_code;
  664. if (mf->width != tfmt->width ||
  665. mf->height != tfmt->height)
  666. continue;
  667. tfmt->code = mf->code;
  668. }
  669. if (csis)
  670. ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
  671. if (mf->code == tfmt->code &&
  672. mf->width == tfmt->width && mf->height == tfmt->height)
  673. break;
  674. }
  675. if (fmt_id && ffmt)
  676. *fmt_id = ffmt;
  677. *tfmt = *mf;
  678. dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
  679. return 0;
  680. }
  681. static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
  682. struct v4l2_format *f)
  683. {
  684. struct fimc_dev *fimc = video_drvdata(file);
  685. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  686. return fimc_fill_format(&ctx->d_frame, f);
  687. }
  688. static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
  689. struct v4l2_format *f)
  690. {
  691. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  692. struct fimc_dev *fimc = video_drvdata(file);
  693. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  694. struct v4l2_mbus_framefmt mf;
  695. struct fimc_fmt *ffmt = NULL;
  696. if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
  697. fimc_capture_try_format(ctx, &pix->width, &pix->height,
  698. NULL, &pix->pixelformat,
  699. FIMC_SD_PAD_SINK);
  700. ctx->s_frame.f_width = pix->width;
  701. ctx->s_frame.f_height = pix->height;
  702. }
  703. ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
  704. NULL, &pix->pixelformat,
  705. FIMC_SD_PAD_SOURCE);
  706. if (!ffmt)
  707. return -EINVAL;
  708. if (!fimc->vid_cap.user_subdev_api) {
  709. mf.width = pix->width;
  710. mf.height = pix->height;
  711. mf.code = ffmt->mbus_code;
  712. fimc_md_graph_lock(fimc);
  713. fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
  714. fimc_md_graph_unlock(fimc);
  715. pix->width = mf.width;
  716. pix->height = mf.height;
  717. if (ffmt)
  718. pix->pixelformat = ffmt->fourcc;
  719. }
  720. fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
  721. return 0;
  722. }
  723. static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
  724. {
  725. ctx->scaler.enabled = !jpeg;
  726. fimc_ctrls_activate(ctx, !jpeg);
  727. if (jpeg)
  728. set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
  729. else
  730. clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
  731. }
  732. static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
  733. {
  734. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  735. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  736. struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
  737. struct fimc_frame *ff = &ctx->d_frame;
  738. struct fimc_fmt *s_fmt = NULL;
  739. int ret, i;
  740. if (vb2_is_busy(&fimc->vid_cap.vbq))
  741. return -EBUSY;
  742. /* Pre-configure format at camera interface input, for JPEG only */
  743. if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
  744. fimc_capture_try_format(ctx, &pix->width, &pix->height,
  745. NULL, &pix->pixelformat,
  746. FIMC_SD_PAD_SINK);
  747. ctx->s_frame.f_width = pix->width;
  748. ctx->s_frame.f_height = pix->height;
  749. }
  750. /* Try the format at the scaler and the DMA output */
  751. ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
  752. NULL, &pix->pixelformat,
  753. FIMC_SD_PAD_SOURCE);
  754. if (!ff->fmt)
  755. return -EINVAL;
  756. /* Update RGB Alpha control state and value range */
  757. fimc_alpha_ctrl_update(ctx);
  758. /* Try to match format at the host and the sensor */
  759. if (!fimc->vid_cap.user_subdev_api) {
  760. mf->code = ff->fmt->mbus_code;
  761. mf->width = pix->width;
  762. mf->height = pix->height;
  763. fimc_md_graph_lock(fimc);
  764. ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
  765. fimc_md_graph_unlock(fimc);
  766. if (ret)
  767. return ret;
  768. pix->width = mf->width;
  769. pix->height = mf->height;
  770. }
  771. fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
  772. for (i = 0; i < ff->fmt->colplanes; i++)
  773. ff->payload[i] = pix->plane_fmt[i].sizeimage;
  774. set_frame_bounds(ff, pix->width, pix->height);
  775. /* Reset the composition rectangle if not yet configured */
  776. if (!(ctx->state & FIMC_COMPOSE))
  777. set_frame_crop(ff, 0, 0, pix->width, pix->height);
  778. fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
  779. /* Reset cropping and set format at the camera interface input */
  780. if (!fimc->vid_cap.user_subdev_api) {
  781. ctx->s_frame.fmt = s_fmt;
  782. set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
  783. set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
  784. }
  785. return ret;
  786. }
  787. static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
  788. struct v4l2_format *f)
  789. {
  790. struct fimc_dev *fimc = video_drvdata(file);
  791. return fimc_capture_set_format(fimc, f);
  792. }
  793. static int fimc_cap_enum_input(struct file *file, void *priv,
  794. struct v4l2_input *i)
  795. {
  796. struct fimc_dev *fimc = video_drvdata(file);
  797. struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
  798. if (i->index != 0)
  799. return -EINVAL;
  800. i->type = V4L2_INPUT_TYPE_CAMERA;
  801. if (sd)
  802. strlcpy(i->name, sd->name, sizeof(i->name));
  803. return 0;
  804. }
  805. static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
  806. {
  807. return i == 0 ? i : -EINVAL;
  808. }
  809. static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
  810. {
  811. *i = 0;
  812. return 0;
  813. }
  814. /**
  815. * fimc_pipeline_validate - check for formats inconsistencies
  816. * between source and sink pad of each link
  817. *
  818. * Return 0 if all formats match or -EPIPE otherwise.
  819. */
  820. static int fimc_pipeline_validate(struct fimc_dev *fimc)
  821. {
  822. struct v4l2_subdev_format sink_fmt, src_fmt;
  823. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  824. struct v4l2_subdev *sd;
  825. struct media_pad *pad;
  826. int ret;
  827. /* Start with the video capture node pad */
  828. pad = media_entity_remote_source(&vid_cap->vd_pad);
  829. if (pad == NULL)
  830. return -EPIPE;
  831. /* FIMC.{N} subdevice */
  832. sd = media_entity_to_v4l2_subdev(pad->entity);
  833. while (1) {
  834. /* Retrieve format at the sink pad */
  835. pad = &sd->entity.pads[0];
  836. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  837. break;
  838. /* Don't call FIMC subdev operation to avoid nested locking */
  839. if (sd == &fimc->vid_cap.subdev) {
  840. struct fimc_frame *ff = &vid_cap->ctx->s_frame;
  841. sink_fmt.format.width = ff->f_width;
  842. sink_fmt.format.height = ff->f_height;
  843. sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
  844. } else {
  845. sink_fmt.pad = pad->index;
  846. sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  847. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
  848. if (ret < 0 && ret != -ENOIOCTLCMD)
  849. return -EPIPE;
  850. }
  851. /* Retrieve format at the source pad */
  852. pad = media_entity_remote_source(pad);
  853. if (pad == NULL ||
  854. media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  855. break;
  856. sd = media_entity_to_v4l2_subdev(pad->entity);
  857. src_fmt.pad = pad->index;
  858. src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  859. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
  860. if (ret < 0 && ret != -ENOIOCTLCMD)
  861. return -EPIPE;
  862. if (src_fmt.format.width != sink_fmt.format.width ||
  863. src_fmt.format.height != sink_fmt.format.height ||
  864. src_fmt.format.code != sink_fmt.format.code)
  865. return -EPIPE;
  866. }
  867. return 0;
  868. }
  869. static int fimc_cap_streamon(struct file *file, void *priv,
  870. enum v4l2_buf_type type)
  871. {
  872. struct fimc_dev *fimc = video_drvdata(file);
  873. struct fimc_pipeline *p = &fimc->pipeline;
  874. struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
  875. int ret;
  876. if (fimc_capture_active(fimc))
  877. return -EBUSY;
  878. ret = media_entity_pipeline_start(&sd->entity, p->m_pipeline);
  879. if (ret < 0)
  880. return ret;
  881. if (fimc->vid_cap.user_subdev_api) {
  882. ret = fimc_pipeline_validate(fimc);
  883. if (ret < 0) {
  884. media_entity_pipeline_stop(&sd->entity);
  885. return ret;
  886. }
  887. }
  888. return vb2_streamon(&fimc->vid_cap.vbq, type);
  889. }
  890. static int fimc_cap_streamoff(struct file *file, void *priv,
  891. enum v4l2_buf_type type)
  892. {
  893. struct fimc_dev *fimc = video_drvdata(file);
  894. struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
  895. int ret;
  896. ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
  897. if (ret == 0)
  898. media_entity_pipeline_stop(&sd->entity);
  899. return ret;
  900. }
  901. static int fimc_cap_reqbufs(struct file *file, void *priv,
  902. struct v4l2_requestbuffers *reqbufs)
  903. {
  904. struct fimc_dev *fimc = video_drvdata(file);
  905. int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
  906. if (!ret)
  907. fimc->vid_cap.reqbufs_count = reqbufs->count;
  908. return ret;
  909. }
  910. static int fimc_cap_querybuf(struct file *file, void *priv,
  911. struct v4l2_buffer *buf)
  912. {
  913. struct fimc_dev *fimc = video_drvdata(file);
  914. return vb2_querybuf(&fimc->vid_cap.vbq, buf);
  915. }
  916. static int fimc_cap_qbuf(struct file *file, void *priv,
  917. struct v4l2_buffer *buf)
  918. {
  919. struct fimc_dev *fimc = video_drvdata(file);
  920. return vb2_qbuf(&fimc->vid_cap.vbq, buf);
  921. }
  922. static int fimc_cap_dqbuf(struct file *file, void *priv,
  923. struct v4l2_buffer *buf)
  924. {
  925. struct fimc_dev *fimc = video_drvdata(file);
  926. return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
  927. }
  928. static int fimc_cap_create_bufs(struct file *file, void *priv,
  929. struct v4l2_create_buffers *create)
  930. {
  931. struct fimc_dev *fimc = video_drvdata(file);
  932. return vb2_create_bufs(&fimc->vid_cap.vbq, create);
  933. }
  934. static int fimc_cap_prepare_buf(struct file *file, void *priv,
  935. struct v4l2_buffer *b)
  936. {
  937. struct fimc_dev *fimc = video_drvdata(file);
  938. return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
  939. }
  940. static int fimc_cap_g_selection(struct file *file, void *fh,
  941. struct v4l2_selection *s)
  942. {
  943. struct fimc_dev *fimc = video_drvdata(file);
  944. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  945. struct fimc_frame *f = &ctx->s_frame;
  946. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  947. return -EINVAL;
  948. switch (s->target) {
  949. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  950. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  951. f = &ctx->d_frame;
  952. case V4L2_SEL_TGT_CROP_BOUNDS:
  953. case V4L2_SEL_TGT_CROP_DEFAULT:
  954. s->r.left = 0;
  955. s->r.top = 0;
  956. s->r.width = f->o_width;
  957. s->r.height = f->o_height;
  958. return 0;
  959. case V4L2_SEL_TGT_COMPOSE_ACTIVE:
  960. f = &ctx->d_frame;
  961. case V4L2_SEL_TGT_CROP_ACTIVE:
  962. s->r.left = f->offs_h;
  963. s->r.top = f->offs_v;
  964. s->r.width = f->width;
  965. s->r.height = f->height;
  966. return 0;
  967. }
  968. return -EINVAL;
  969. }
  970. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  971. int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
  972. {
  973. if (a->left < b->left || a->top < b->top)
  974. return 0;
  975. if (a->left + a->width > b->left + b->width)
  976. return 0;
  977. if (a->top + a->height > b->top + b->height)
  978. return 0;
  979. return 1;
  980. }
  981. static int fimc_cap_s_selection(struct file *file, void *fh,
  982. struct v4l2_selection *s)
  983. {
  984. struct fimc_dev *fimc = video_drvdata(file);
  985. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  986. struct v4l2_rect rect = s->r;
  987. struct fimc_frame *f;
  988. unsigned long flags;
  989. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  990. return -EINVAL;
  991. if (s->target == V4L2_SEL_TGT_COMPOSE_ACTIVE)
  992. f = &ctx->d_frame;
  993. else if (s->target == V4L2_SEL_TGT_CROP_ACTIVE)
  994. f = &ctx->s_frame;
  995. else
  996. return -EINVAL;
  997. fimc_capture_try_selection(ctx, &rect, s->target);
  998. if (s->flags & V4L2_SEL_FLAG_LE &&
  999. !enclosed_rectangle(&rect, &s->r))
  1000. return -ERANGE;
  1001. if (s->flags & V4L2_SEL_FLAG_GE &&
  1002. !enclosed_rectangle(&s->r, &rect))
  1003. return -ERANGE;
  1004. s->r = rect;
  1005. spin_lock_irqsave(&fimc->slock, flags);
  1006. set_frame_crop(f, s->r.left, s->r.top, s->r.width,
  1007. s->r.height);
  1008. spin_unlock_irqrestore(&fimc->slock, flags);
  1009. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  1010. return 0;
  1011. }
  1012. static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
  1013. .vidioc_querycap = fimc_vidioc_querycap_capture,
  1014. .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
  1015. .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
  1016. .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
  1017. .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
  1018. .vidioc_reqbufs = fimc_cap_reqbufs,
  1019. .vidioc_querybuf = fimc_cap_querybuf,
  1020. .vidioc_qbuf = fimc_cap_qbuf,
  1021. .vidioc_dqbuf = fimc_cap_dqbuf,
  1022. .vidioc_prepare_buf = fimc_cap_prepare_buf,
  1023. .vidioc_create_bufs = fimc_cap_create_bufs,
  1024. .vidioc_streamon = fimc_cap_streamon,
  1025. .vidioc_streamoff = fimc_cap_streamoff,
  1026. .vidioc_g_selection = fimc_cap_g_selection,
  1027. .vidioc_s_selection = fimc_cap_s_selection,
  1028. .vidioc_enum_input = fimc_cap_enum_input,
  1029. .vidioc_s_input = fimc_cap_s_input,
  1030. .vidioc_g_input = fimc_cap_g_input,
  1031. };
  1032. /* Capture subdev media entity operations */
  1033. static int fimc_link_setup(struct media_entity *entity,
  1034. const struct media_pad *local,
  1035. const struct media_pad *remote, u32 flags)
  1036. {
  1037. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1038. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1039. if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  1040. return -EINVAL;
  1041. if (WARN_ON(fimc == NULL))
  1042. return 0;
  1043. dbg("%s --> %s, flags: 0x%x. input: 0x%x",
  1044. local->entity->name, remote->entity->name, flags,
  1045. fimc->vid_cap.input);
  1046. if (flags & MEDIA_LNK_FL_ENABLED) {
  1047. if (fimc->vid_cap.input != 0)
  1048. return -EBUSY;
  1049. fimc->vid_cap.input = sd->grp_id;
  1050. return 0;
  1051. }
  1052. fimc->vid_cap.input = 0;
  1053. return 0;
  1054. }
  1055. static const struct media_entity_operations fimc_sd_media_ops = {
  1056. .link_setup = fimc_link_setup,
  1057. };
  1058. /**
  1059. * fimc_sensor_notify - v4l2_device notification from a sensor subdev
  1060. * @sd: pointer to a subdev generating the notification
  1061. * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
  1062. * @arg: pointer to an u32 type integer that stores the frame payload value
  1063. *
  1064. * The End Of Frame notification sent by sensor subdev in its still capture
  1065. * mode. If there is only a single VSYNC generated by the sensor at the
  1066. * beginning of a frame transmission, FIMC does not issue the LastIrq
  1067. * (end of frame) interrupt. And this notification is used to complete the
  1068. * frame capture and returning a buffer to user-space. Subdev drivers should
  1069. * call this notification from their last 'End of frame capture' interrupt.
  1070. */
  1071. void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
  1072. void *arg)
  1073. {
  1074. struct fimc_sensor_info *sensor;
  1075. struct fimc_vid_buffer *buf;
  1076. struct fimc_md *fmd;
  1077. struct fimc_dev *fimc;
  1078. unsigned long flags;
  1079. if (sd == NULL)
  1080. return;
  1081. sensor = v4l2_get_subdev_hostdata(sd);
  1082. fmd = entity_to_fimc_mdev(&sd->entity);
  1083. spin_lock_irqsave(&fmd->slock, flags);
  1084. fimc = sensor ? sensor->host : NULL;
  1085. if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
  1086. test_bit(ST_CAPT_PEND, &fimc->state)) {
  1087. unsigned long irq_flags;
  1088. spin_lock_irqsave(&fimc->slock, irq_flags);
  1089. if (!list_empty(&fimc->vid_cap.active_buf_q)) {
  1090. buf = list_entry(fimc->vid_cap.active_buf_q.next,
  1091. struct fimc_vid_buffer, list);
  1092. vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
  1093. }
  1094. fimc_capture_irq_handler(fimc, 1);
  1095. fimc_deactivate_capture(fimc);
  1096. spin_unlock_irqrestore(&fimc->slock, irq_flags);
  1097. }
  1098. spin_unlock_irqrestore(&fmd->slock, flags);
  1099. }
  1100. static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  1101. struct v4l2_subdev_fh *fh,
  1102. struct v4l2_subdev_mbus_code_enum *code)
  1103. {
  1104. struct fimc_fmt *fmt;
  1105. fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
  1106. if (!fmt)
  1107. return -EINVAL;
  1108. code->code = fmt->mbus_code;
  1109. return 0;
  1110. }
  1111. static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
  1112. struct v4l2_subdev_fh *fh,
  1113. struct v4l2_subdev_format *fmt)
  1114. {
  1115. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1116. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1117. struct v4l2_mbus_framefmt *mf;
  1118. struct fimc_frame *ff;
  1119. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1120. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  1121. fmt->format = *mf;
  1122. return 0;
  1123. }
  1124. mf = &fmt->format;
  1125. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1126. ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
  1127. mutex_lock(&fimc->lock);
  1128. /* The pixel code is same on both input and output pad */
  1129. if (!WARN_ON(ctx->s_frame.fmt == NULL))
  1130. mf->code = ctx->s_frame.fmt->mbus_code;
  1131. mf->width = ff->f_width;
  1132. mf->height = ff->f_height;
  1133. mutex_unlock(&fimc->lock);
  1134. return 0;
  1135. }
  1136. static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
  1137. struct v4l2_subdev_fh *fh,
  1138. struct v4l2_subdev_format *fmt)
  1139. {
  1140. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1141. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1142. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1143. struct fimc_frame *ff;
  1144. struct fimc_fmt *ffmt;
  1145. dbg("pad%d: code: 0x%x, %dx%d",
  1146. fmt->pad, mf->code, mf->width, mf->height);
  1147. if (fmt->pad == FIMC_SD_PAD_SOURCE &&
  1148. vb2_is_busy(&fimc->vid_cap.vbq))
  1149. return -EBUSY;
  1150. mutex_lock(&fimc->lock);
  1151. ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
  1152. &mf->code, NULL, fmt->pad);
  1153. mutex_unlock(&fimc->lock);
  1154. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1155. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1156. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  1157. *mf = fmt->format;
  1158. return 0;
  1159. }
  1160. /* Update RGB Alpha control state and value range */
  1161. fimc_alpha_ctrl_update(ctx);
  1162. fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
  1163. ff = fmt->pad == FIMC_SD_PAD_SINK ?
  1164. &ctx->s_frame : &ctx->d_frame;
  1165. mutex_lock(&fimc->lock);
  1166. set_frame_bounds(ff, mf->width, mf->height);
  1167. fimc->vid_cap.mf = *mf;
  1168. ff->fmt = ffmt;
  1169. /* Reset the crop rectangle if required. */
  1170. if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
  1171. set_frame_crop(ff, 0, 0, mf->width, mf->height);
  1172. if (fmt->pad == FIMC_SD_PAD_SINK)
  1173. ctx->state &= ~FIMC_COMPOSE;
  1174. mutex_unlock(&fimc->lock);
  1175. return 0;
  1176. }
  1177. static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
  1178. struct v4l2_subdev_fh *fh,
  1179. struct v4l2_subdev_selection *sel)
  1180. {
  1181. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1182. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1183. struct fimc_frame *f = &ctx->s_frame;
  1184. struct v4l2_rect *r = &sel->r;
  1185. struct v4l2_rect *try_sel;
  1186. if (sel->pad != FIMC_SD_PAD_SINK)
  1187. return -EINVAL;
  1188. mutex_lock(&fimc->lock);
  1189. switch (sel->target) {
  1190. case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
  1191. f = &ctx->d_frame;
  1192. case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
  1193. r->width = f->o_width;
  1194. r->height = f->o_height;
  1195. r->left = 0;
  1196. r->top = 0;
  1197. mutex_unlock(&fimc->lock);
  1198. return 0;
  1199. case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
  1200. try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
  1201. break;
  1202. case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
  1203. try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
  1204. f = &ctx->d_frame;
  1205. break;
  1206. default:
  1207. mutex_unlock(&fimc->lock);
  1208. return -EINVAL;
  1209. }
  1210. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1211. sel->r = *try_sel;
  1212. } else {
  1213. r->left = f->offs_h;
  1214. r->top = f->offs_v;
  1215. r->width = f->width;
  1216. r->height = f->height;
  1217. }
  1218. dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
  1219. sel->pad, r->left, r->top, r->width, r->height,
  1220. f->f_width, f->f_height);
  1221. mutex_unlock(&fimc->lock);
  1222. return 0;
  1223. }
  1224. static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
  1225. struct v4l2_subdev_fh *fh,
  1226. struct v4l2_subdev_selection *sel)
  1227. {
  1228. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1229. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1230. struct fimc_frame *f = &ctx->s_frame;
  1231. struct v4l2_rect *r = &sel->r;
  1232. struct v4l2_rect *try_sel;
  1233. unsigned long flags;
  1234. if (sel->pad != FIMC_SD_PAD_SINK)
  1235. return -EINVAL;
  1236. mutex_lock(&fimc->lock);
  1237. fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP_ACTIVE);
  1238. switch (sel->target) {
  1239. case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
  1240. f = &ctx->d_frame;
  1241. case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
  1242. r->width = f->o_width;
  1243. r->height = f->o_height;
  1244. r->left = 0;
  1245. r->top = 0;
  1246. mutex_unlock(&fimc->lock);
  1247. return 0;
  1248. case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
  1249. try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
  1250. break;
  1251. case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
  1252. try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
  1253. f = &ctx->d_frame;
  1254. break;
  1255. default:
  1256. mutex_unlock(&fimc->lock);
  1257. return -EINVAL;
  1258. }
  1259. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1260. *try_sel = sel->r;
  1261. } else {
  1262. spin_lock_irqsave(&fimc->slock, flags);
  1263. set_frame_crop(f, r->left, r->top, r->width, r->height);
  1264. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  1265. spin_unlock_irqrestore(&fimc->slock, flags);
  1266. if (sel->target == V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL)
  1267. ctx->state |= FIMC_COMPOSE;
  1268. }
  1269. dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
  1270. r->width, r->height);
  1271. mutex_unlock(&fimc->lock);
  1272. return 0;
  1273. }
  1274. static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
  1275. .enum_mbus_code = fimc_subdev_enum_mbus_code,
  1276. .get_selection = fimc_subdev_get_selection,
  1277. .set_selection = fimc_subdev_set_selection,
  1278. .get_fmt = fimc_subdev_get_fmt,
  1279. .set_fmt = fimc_subdev_set_fmt,
  1280. };
  1281. static struct v4l2_subdev_ops fimc_subdev_ops = {
  1282. .pad = &fimc_subdev_pad_ops,
  1283. };
  1284. /* Set default format at the sensor and host interface */
  1285. static int fimc_capture_set_default_format(struct fimc_dev *fimc)
  1286. {
  1287. struct v4l2_format fmt = {
  1288. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  1289. .fmt.pix_mp = {
  1290. .width = 640,
  1291. .height = 480,
  1292. .pixelformat = V4L2_PIX_FMT_YUYV,
  1293. .field = V4L2_FIELD_NONE,
  1294. .colorspace = V4L2_COLORSPACE_JPEG,
  1295. },
  1296. };
  1297. return fimc_capture_set_format(fimc, &fmt);
  1298. }
  1299. /* fimc->lock must be already initialized */
  1300. static int fimc_register_capture_device(struct fimc_dev *fimc,
  1301. struct v4l2_device *v4l2_dev)
  1302. {
  1303. struct video_device *vfd;
  1304. struct fimc_vid_cap *vid_cap;
  1305. struct fimc_ctx *ctx;
  1306. struct vb2_queue *q;
  1307. int ret = -ENOMEM;
  1308. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  1309. if (!ctx)
  1310. return -ENOMEM;
  1311. ctx->fimc_dev = fimc;
  1312. ctx->in_path = FIMC_IO_CAMERA;
  1313. ctx->out_path = FIMC_IO_DMA;
  1314. ctx->state = FIMC_CTX_CAP;
  1315. ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
  1316. ctx->d_frame.fmt = ctx->s_frame.fmt;
  1317. vfd = video_device_alloc();
  1318. if (!vfd) {
  1319. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  1320. goto err_vd_alloc;
  1321. }
  1322. snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
  1323. vfd->fops = &fimc_capture_fops;
  1324. vfd->ioctl_ops = &fimc_capture_ioctl_ops;
  1325. vfd->v4l2_dev = v4l2_dev;
  1326. vfd->minor = -1;
  1327. vfd->release = video_device_release;
  1328. vfd->lock = &fimc->lock;
  1329. /* Locking in file operations other than ioctl should be done
  1330. by the driver, not the V4L2 core.
  1331. This driver needs auditing so that this flag can be removed. */
  1332. set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
  1333. video_set_drvdata(vfd, fimc);
  1334. vid_cap = &fimc->vid_cap;
  1335. vid_cap->vfd = vfd;
  1336. vid_cap->active_buf_cnt = 0;
  1337. vid_cap->reqbufs_count = 0;
  1338. vid_cap->refcnt = 0;
  1339. INIT_LIST_HEAD(&vid_cap->pending_buf_q);
  1340. INIT_LIST_HEAD(&vid_cap->active_buf_q);
  1341. vid_cap->ctx = ctx;
  1342. q = &fimc->vid_cap.vbq;
  1343. memset(q, 0, sizeof(*q));
  1344. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1345. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1346. q->drv_priv = fimc->vid_cap.ctx;
  1347. q->ops = &fimc_capture_qops;
  1348. q->mem_ops = &vb2_dma_contig_memops;
  1349. q->buf_struct_size = sizeof(struct fimc_vid_buffer);
  1350. vb2_queue_init(q);
  1351. vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
  1352. ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
  1353. if (ret)
  1354. goto err_ent;
  1355. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  1356. if (ret)
  1357. goto err_vd;
  1358. v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
  1359. vfd->name, video_device_node_name(vfd));
  1360. vfd->ctrl_handler = &ctx->ctrls.handler;
  1361. return 0;
  1362. err_vd:
  1363. media_entity_cleanup(&vfd->entity);
  1364. err_ent:
  1365. video_device_release(vfd);
  1366. err_vd_alloc:
  1367. kfree(ctx);
  1368. return ret;
  1369. }
  1370. static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
  1371. {
  1372. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1373. int ret;
  1374. ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
  1375. if (ret)
  1376. return ret;
  1377. ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
  1378. if (ret)
  1379. fimc_unregister_m2m_device(fimc);
  1380. return ret;
  1381. }
  1382. static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
  1383. {
  1384. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1385. if (fimc == NULL)
  1386. return;
  1387. fimc_unregister_m2m_device(fimc);
  1388. if (fimc->vid_cap.vfd) {
  1389. media_entity_cleanup(&fimc->vid_cap.vfd->entity);
  1390. video_unregister_device(fimc->vid_cap.vfd);
  1391. fimc->vid_cap.vfd = NULL;
  1392. }
  1393. kfree(fimc->vid_cap.ctx);
  1394. fimc->vid_cap.ctx = NULL;
  1395. }
  1396. static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
  1397. .registered = fimc_capture_subdev_registered,
  1398. .unregistered = fimc_capture_subdev_unregistered,
  1399. };
  1400. int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
  1401. {
  1402. struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
  1403. int ret;
  1404. v4l2_subdev_init(sd, &fimc_subdev_ops);
  1405. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  1406. snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
  1407. fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1408. fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1409. ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
  1410. fimc->vid_cap.sd_pads, 0);
  1411. if (ret)
  1412. return ret;
  1413. sd->entity.ops = &fimc_sd_media_ops;
  1414. sd->internal_ops = &fimc_capture_sd_internal_ops;
  1415. v4l2_set_subdevdata(sd, fimc);
  1416. return 0;
  1417. }
  1418. void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
  1419. {
  1420. struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
  1421. v4l2_device_unregister_subdev(sd);
  1422. media_entity_cleanup(&sd->entity);
  1423. v4l2_set_subdevdata(sd, NULL);
  1424. }