fimc-capture.c 50 KB

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