fimc-capture.c 49 KB

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