camif-capture.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. /*
  2. * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
  3. *
  4. * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
  5. * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
  6. *
  7. * Based on drivers/media/platform/s5p-fimc,
  8. * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
  15. #include <linux/bug.h>
  16. #include <linux/clk.h>
  17. #include <linux/device.h>
  18. #include <linux/errno.h>
  19. #include <linux/i2c.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/ratelimit.h>
  28. #include <linux/slab.h>
  29. #include <linux/types.h>
  30. #include <linux/videodev2.h>
  31. #include <media/media-device.h>
  32. #include <media/v4l2-ctrls.h>
  33. #include <media/v4l2-event.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/videobuf2-core.h>
  36. #include <media/videobuf2-dma-contig.h>
  37. #include "camif-core.h"
  38. #include "camif-regs.h"
  39. static int debug;
  40. module_param(debug, int, 0644);
  41. /* Locking: called with vp->camif->slock spinlock held */
  42. static void camif_cfg_video_path(struct camif_vp *vp)
  43. {
  44. WARN_ON(s3c_camif_get_scaler_config(vp, &vp->scaler));
  45. camif_hw_set_scaler(vp);
  46. camif_hw_set_flip(vp);
  47. camif_hw_set_target_format(vp);
  48. camif_hw_set_output_dma(vp);
  49. }
  50. static void camif_prepare_dma_offset(struct camif_vp *vp)
  51. {
  52. struct camif_frame *f = &vp->out_frame;
  53. f->dma_offset.initial = f->rect.top * f->f_width + f->rect.left;
  54. f->dma_offset.line = f->f_width - (f->rect.left + f->rect.width);
  55. pr_debug("dma_offset: initial: %d, line: %d\n",
  56. f->dma_offset.initial, f->dma_offset.line);
  57. }
  58. /* Locking: called with camif->slock spinlock held */
  59. static int s3c_camif_hw_init(struct camif_dev *camif, struct camif_vp *vp)
  60. {
  61. const struct s3c_camif_variant *variant = camif->variant;
  62. if (camif->sensor.sd == NULL || vp->out_fmt == NULL)
  63. return -EINVAL;
  64. if (variant->ip_revision == S3C244X_CAMIF_IP_REV)
  65. camif_hw_clear_fifo_overflow(vp);
  66. camif_hw_set_camera_bus(camif);
  67. camif_hw_set_source_format(camif);
  68. camif_hw_set_camera_crop(camif);
  69. camif_hw_set_test_pattern(camif, camif->test_pattern);
  70. if (variant->has_img_effect)
  71. camif_hw_set_effect(camif, camif->colorfx,
  72. camif->colorfx_cb, camif->colorfx_cr);
  73. if (variant->ip_revision == S3C6410_CAMIF_IP_REV)
  74. camif_hw_set_input_path(vp);
  75. camif_cfg_video_path(vp);
  76. vp->state &= ~ST_VP_CONFIG;
  77. return 0;
  78. }
  79. /*
  80. * Initialize the video path, only up from the scaler stage. The camera
  81. * input interface set up is skipped. This is useful to enable one of the
  82. * video paths when the other is already running.
  83. * Locking: called with camif->slock spinlock held.
  84. */
  85. static int s3c_camif_hw_vp_init(struct camif_dev *camif, struct camif_vp *vp)
  86. {
  87. unsigned int ip_rev = camif->variant->ip_revision;
  88. if (vp->out_fmt == NULL)
  89. return -EINVAL;
  90. camif_prepare_dma_offset(vp);
  91. if (ip_rev == S3C244X_CAMIF_IP_REV)
  92. camif_hw_clear_fifo_overflow(vp);
  93. camif_cfg_video_path(vp);
  94. vp->state &= ~ST_VP_CONFIG;
  95. return 0;
  96. }
  97. static int sensor_set_power(struct camif_dev *camif, int on)
  98. {
  99. struct cam_sensor *sensor = &camif->sensor;
  100. int err = 0;
  101. if (!on == camif->sensor.power_count)
  102. err = v4l2_subdev_call(sensor->sd, core, s_power, on);
  103. if (!err)
  104. sensor->power_count += on ? 1 : -1;
  105. pr_debug("on: %d, power_count: %d, err: %d\n",
  106. on, sensor->power_count, err);
  107. return err;
  108. }
  109. static int sensor_set_streaming(struct camif_dev *camif, int on)
  110. {
  111. struct cam_sensor *sensor = &camif->sensor;
  112. int err = 0;
  113. if (!on == camif->sensor.stream_count)
  114. err = v4l2_subdev_call(sensor->sd, video, s_stream, on);
  115. if (!err)
  116. sensor->stream_count += on ? 1 : -1;
  117. pr_debug("on: %d, stream_count: %d, err: %d\n",
  118. on, sensor->stream_count, err);
  119. return err;
  120. }
  121. /*
  122. * Reinitialize the driver so it is ready to start streaming again.
  123. * Return any buffers to vb2, perform CAMIF software reset and
  124. * turn off streaming at the data pipeline (sensor) if required.
  125. */
  126. static int camif_reinitialize(struct camif_vp *vp)
  127. {
  128. struct camif_dev *camif = vp->camif;
  129. struct camif_buffer *buf;
  130. unsigned long flags;
  131. bool streaming;
  132. spin_lock_irqsave(&camif->slock, flags);
  133. streaming = vp->state & ST_VP_SENSOR_STREAMING;
  134. vp->state &= ~(ST_VP_PENDING | ST_VP_RUNNING | ST_VP_OFF |
  135. ST_VP_ABORTING | ST_VP_STREAMING |
  136. ST_VP_SENSOR_STREAMING | ST_VP_LASTIRQ);
  137. /* Release unused buffers */
  138. while (!list_empty(&vp->pending_buf_q)) {
  139. buf = camif_pending_queue_pop(vp);
  140. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  141. }
  142. while (!list_empty(&vp->active_buf_q)) {
  143. buf = camif_active_queue_pop(vp);
  144. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  145. }
  146. spin_unlock_irqrestore(&camif->slock, flags);
  147. if (!streaming)
  148. return 0;
  149. return sensor_set_streaming(camif, 0);
  150. }
  151. static bool s3c_vp_active(struct camif_vp *vp)
  152. {
  153. struct camif_dev *camif = vp->camif;
  154. unsigned long flags;
  155. bool ret;
  156. spin_lock_irqsave(&camif->slock, flags);
  157. ret = (vp->state & ST_VP_RUNNING) || (vp->state & ST_VP_PENDING);
  158. spin_unlock_irqrestore(&camif->slock, flags);
  159. return ret;
  160. }
  161. static bool camif_is_streaming(struct camif_dev *camif)
  162. {
  163. unsigned long flags;
  164. bool status;
  165. spin_lock_irqsave(&camif->slock, flags);
  166. status = camif->stream_count > 0;
  167. spin_unlock_irqrestore(&camif->slock, flags);
  168. return status;
  169. }
  170. static int camif_stop_capture(struct camif_vp *vp)
  171. {
  172. struct camif_dev *camif = vp->camif;
  173. unsigned long flags;
  174. int ret;
  175. if (!s3c_vp_active(vp))
  176. return 0;
  177. spin_lock_irqsave(&camif->slock, flags);
  178. vp->state &= ~(ST_VP_OFF | ST_VP_LASTIRQ);
  179. vp->state |= ST_VP_ABORTING;
  180. spin_unlock_irqrestore(&camif->slock, flags);
  181. ret = wait_event_timeout(vp->irq_queue,
  182. !(vp->state & ST_VP_ABORTING),
  183. msecs_to_jiffies(CAMIF_STOP_TIMEOUT));
  184. spin_lock_irqsave(&camif->slock, flags);
  185. if (ret == 0 && !(vp->state & ST_VP_OFF)) {
  186. /* Timed out, forcibly stop capture */
  187. vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING |
  188. ST_VP_LASTIRQ);
  189. camif_hw_disable_capture(vp);
  190. camif_hw_enable_scaler(vp, false);
  191. }
  192. spin_unlock_irqrestore(&camif->slock, flags);
  193. return camif_reinitialize(vp);
  194. }
  195. static int camif_prepare_addr(struct camif_vp *vp, struct vb2_buffer *vb,
  196. struct camif_addr *paddr)
  197. {
  198. struct camif_frame *frame = &vp->out_frame;
  199. u32 pix_size;
  200. if (vb == NULL || frame == NULL)
  201. return -EINVAL;
  202. pix_size = frame->rect.width * frame->rect.height;
  203. pr_debug("colplanes: %d, pix_size: %u\n",
  204. vp->out_fmt->colplanes, pix_size);
  205. paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
  206. switch (vp->out_fmt->colplanes) {
  207. case 1:
  208. paddr->cb = 0;
  209. paddr->cr = 0;
  210. break;
  211. case 2:
  212. /* decompose Y into Y/Cb */
  213. paddr->cb = (u32)(paddr->y + pix_size);
  214. paddr->cr = 0;
  215. break;
  216. case 3:
  217. paddr->cb = (u32)(paddr->y + pix_size);
  218. /* decompose Y into Y/Cb/Cr */
  219. if (vp->out_fmt->color == IMG_FMT_YCBCR422P)
  220. paddr->cr = (u32)(paddr->cb + (pix_size >> 1));
  221. else /* 420 */
  222. paddr->cr = (u32)(paddr->cb + (pix_size >> 2));
  223. if (vp->out_fmt->color == IMG_FMT_YCRCB420)
  224. swap(paddr->cb, paddr->cr);
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. pr_debug("DMA address: y: %#x cb: %#x cr: %#x\n",
  230. paddr->y, paddr->cb, paddr->cr);
  231. return 0;
  232. }
  233. irqreturn_t s3c_camif_irq_handler(int irq, void *priv)
  234. {
  235. struct camif_vp *vp = priv;
  236. struct camif_dev *camif = vp->camif;
  237. unsigned int ip_rev = camif->variant->ip_revision;
  238. unsigned int status;
  239. spin_lock(&camif->slock);
  240. if (ip_rev == S3C6410_CAMIF_IP_REV)
  241. camif_hw_clear_pending_irq(vp);
  242. status = camif_hw_get_status(vp);
  243. if (ip_rev == S3C244X_CAMIF_IP_REV && (status & CISTATUS_OVF_MASK)) {
  244. camif_hw_clear_fifo_overflow(vp);
  245. goto unlock;
  246. }
  247. if (vp->state & ST_VP_ABORTING) {
  248. if (vp->state & ST_VP_OFF) {
  249. /* Last IRQ */
  250. vp->state &= ~(ST_VP_OFF | ST_VP_ABORTING |
  251. ST_VP_LASTIRQ);
  252. wake_up(&vp->irq_queue);
  253. goto unlock;
  254. } else if (vp->state & ST_VP_LASTIRQ) {
  255. camif_hw_disable_capture(vp);
  256. camif_hw_enable_scaler(vp, false);
  257. camif_hw_set_lastirq(vp, false);
  258. vp->state |= ST_VP_OFF;
  259. } else {
  260. /* Disable capture, enable last IRQ */
  261. camif_hw_set_lastirq(vp, true);
  262. vp->state |= ST_VP_LASTIRQ;
  263. }
  264. }
  265. if (!list_empty(&vp->pending_buf_q) && (vp->state & ST_VP_RUNNING) &&
  266. !list_empty(&vp->active_buf_q)) {
  267. unsigned int index;
  268. struct camif_buffer *vbuf;
  269. struct timeval *tv;
  270. struct timespec ts;
  271. /*
  272. * Get previous DMA write buffer index:
  273. * 0 => DMA buffer 0, 2;
  274. * 1 => DMA buffer 1, 3.
  275. */
  276. index = (CISTATUS_FRAMECNT(status) + 2) & 1;
  277. ktime_get_ts(&ts);
  278. vbuf = camif_active_queue_peek(vp, index);
  279. if (!WARN_ON(vbuf == NULL)) {
  280. /* Dequeue a filled buffer */
  281. tv = &vbuf->vb.v4l2_buf.timestamp;
  282. tv->tv_sec = ts.tv_sec;
  283. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  284. vbuf->vb.v4l2_buf.sequence = vp->frame_sequence++;
  285. vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE);
  286. /* Set up an empty buffer at the DMA engine */
  287. vbuf = camif_pending_queue_pop(vp);
  288. vbuf->index = index;
  289. camif_hw_set_output_addr(vp, &vbuf->paddr, index);
  290. camif_hw_set_output_addr(vp, &vbuf->paddr, index + 2);
  291. /* Scheduled in H/W, add to the queue */
  292. camif_active_queue_add(vp, vbuf);
  293. }
  294. } else if (!(vp->state & ST_VP_ABORTING) &&
  295. (vp->state & ST_VP_PENDING)) {
  296. vp->state |= ST_VP_RUNNING;
  297. }
  298. if (vp->state & ST_VP_CONFIG) {
  299. camif_prepare_dma_offset(vp);
  300. camif_hw_set_camera_crop(camif);
  301. camif_hw_set_scaler(vp);
  302. camif_hw_set_flip(vp);
  303. camif_hw_set_test_pattern(camif, camif->test_pattern);
  304. if (camif->variant->has_img_effect)
  305. camif_hw_set_effect(camif, camif->colorfx,
  306. camif->colorfx_cb, camif->colorfx_cr);
  307. vp->state &= ~ST_VP_CONFIG;
  308. }
  309. unlock:
  310. spin_unlock(&camif->slock);
  311. return IRQ_HANDLED;
  312. }
  313. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  314. {
  315. struct camif_vp *vp = vb2_get_drv_priv(vq);
  316. struct camif_dev *camif = vp->camif;
  317. unsigned long flags;
  318. int ret;
  319. /*
  320. * We assume the codec capture path is always activated
  321. * first, before the preview path starts streaming.
  322. * This is required to avoid internal FIFO overflow and
  323. * a need for CAMIF software reset.
  324. */
  325. spin_lock_irqsave(&camif->slock, flags);
  326. if (camif->stream_count == 0) {
  327. camif_hw_reset(camif);
  328. ret = s3c_camif_hw_init(camif, vp);
  329. } else {
  330. ret = s3c_camif_hw_vp_init(camif, vp);
  331. }
  332. spin_unlock_irqrestore(&camif->slock, flags);
  333. if (ret < 0) {
  334. camif_reinitialize(vp);
  335. return ret;
  336. }
  337. spin_lock_irqsave(&camif->slock, flags);
  338. vp->frame_sequence = 0;
  339. vp->state |= ST_VP_PENDING;
  340. if (!list_empty(&vp->pending_buf_q) &&
  341. (!(vp->state & ST_VP_STREAMING) ||
  342. !(vp->state & ST_VP_SENSOR_STREAMING))) {
  343. camif_hw_enable_scaler(vp, vp->scaler.enable);
  344. camif_hw_enable_capture(vp);
  345. vp->state |= ST_VP_STREAMING;
  346. if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
  347. vp->state |= ST_VP_SENSOR_STREAMING;
  348. spin_unlock_irqrestore(&camif->slock, flags);
  349. ret = sensor_set_streaming(camif, 1);
  350. if (ret)
  351. v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
  352. if (debug)
  353. camif_hw_dump_regs(camif, __func__);
  354. return ret;
  355. }
  356. }
  357. spin_unlock_irqrestore(&camif->slock, flags);
  358. return 0;
  359. }
  360. static int stop_streaming(struct vb2_queue *vq)
  361. {
  362. struct camif_vp *vp = vb2_get_drv_priv(vq);
  363. return camif_stop_capture(vp);
  364. }
  365. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
  366. unsigned int *num_buffers, unsigned int *num_planes,
  367. unsigned int sizes[], void *allocators[])
  368. {
  369. const struct v4l2_pix_format *pix = NULL;
  370. struct camif_vp *vp = vb2_get_drv_priv(vq);
  371. struct camif_dev *camif = vp->camif;
  372. struct camif_frame *frame = &vp->out_frame;
  373. const struct camif_fmt *fmt = vp->out_fmt;
  374. unsigned int size;
  375. if (pfmt) {
  376. pix = &pfmt->fmt.pix;
  377. fmt = s3c_camif_find_format(vp, &pix->pixelformat, -1);
  378. size = (pix->width * pix->height * fmt->depth) / 8;
  379. } else {
  380. size = (frame->f_width * frame->f_height * fmt->depth) / 8;
  381. }
  382. if (fmt == NULL)
  383. return -EINVAL;
  384. *num_planes = 1;
  385. if (pix)
  386. sizes[0] = max(size, pix->sizeimage);
  387. else
  388. sizes[0] = size;
  389. allocators[0] = camif->alloc_ctx;
  390. pr_debug("size: %u\n", sizes[0]);
  391. return 0;
  392. }
  393. static int buffer_prepare(struct vb2_buffer *vb)
  394. {
  395. struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
  396. if (vp->out_fmt == NULL)
  397. return -EINVAL;
  398. if (vb2_plane_size(vb, 0) < vp->payload) {
  399. v4l2_err(&vp->vdev, "buffer too small: %lu, required: %u\n",
  400. vb2_plane_size(vb, 0), vp->payload);
  401. return -EINVAL;
  402. }
  403. vb2_set_plane_payload(vb, 0, vp->payload);
  404. return 0;
  405. }
  406. static void buffer_queue(struct vb2_buffer *vb)
  407. {
  408. struct camif_buffer *buf = container_of(vb, struct camif_buffer, vb);
  409. struct camif_vp *vp = vb2_get_drv_priv(vb->vb2_queue);
  410. struct camif_dev *camif = vp->camif;
  411. unsigned long flags;
  412. spin_lock_irqsave(&camif->slock, flags);
  413. WARN_ON(camif_prepare_addr(vp, &buf->vb, &buf->paddr));
  414. if (!(vp->state & ST_VP_STREAMING) && vp->active_buffers < 2) {
  415. /* Schedule an empty buffer in H/W */
  416. buf->index = vp->buf_index;
  417. camif_hw_set_output_addr(vp, &buf->paddr, buf->index);
  418. camif_hw_set_output_addr(vp, &buf->paddr, buf->index + 2);
  419. camif_active_queue_add(vp, buf);
  420. vp->buf_index = !vp->buf_index;
  421. } else {
  422. camif_pending_queue_add(vp, buf);
  423. }
  424. if (vb2_is_streaming(&vp->vb_queue) && !list_empty(&vp->pending_buf_q)
  425. && !(vp->state & ST_VP_STREAMING)) {
  426. vp->state |= ST_VP_STREAMING;
  427. camif_hw_enable_scaler(vp, vp->scaler.enable);
  428. camif_hw_enable_capture(vp);
  429. spin_unlock_irqrestore(&camif->slock, flags);
  430. if (!(vp->state & ST_VP_SENSOR_STREAMING)) {
  431. if (sensor_set_streaming(camif, 1) == 0)
  432. vp->state |= ST_VP_SENSOR_STREAMING;
  433. else
  434. v4l2_err(&vp->vdev, "Sensor s_stream failed\n");
  435. if (debug)
  436. camif_hw_dump_regs(camif, __func__);
  437. }
  438. return;
  439. }
  440. spin_unlock_irqrestore(&camif->slock, flags);
  441. }
  442. static void camif_lock(struct vb2_queue *vq)
  443. {
  444. struct camif_vp *vp = vb2_get_drv_priv(vq);
  445. mutex_lock(&vp->camif->lock);
  446. }
  447. static void camif_unlock(struct vb2_queue *vq)
  448. {
  449. struct camif_vp *vp = vb2_get_drv_priv(vq);
  450. mutex_unlock(&vp->camif->lock);
  451. }
  452. static const struct vb2_ops s3c_camif_qops = {
  453. .queue_setup = queue_setup,
  454. .buf_prepare = buffer_prepare,
  455. .buf_queue = buffer_queue,
  456. .wait_prepare = camif_unlock,
  457. .wait_finish = camif_lock,
  458. .start_streaming = start_streaming,
  459. .stop_streaming = stop_streaming,
  460. };
  461. static int s3c_camif_open(struct file *file)
  462. {
  463. struct camif_vp *vp = video_drvdata(file);
  464. struct camif_dev *camif = vp->camif;
  465. int ret;
  466. pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
  467. vp->state, vp->owner, task_pid_nr(current));
  468. if (mutex_lock_interruptible(&camif->lock))
  469. return -ERESTARTSYS;
  470. ret = v4l2_fh_open(file);
  471. if (ret < 0)
  472. goto unlock;
  473. ret = pm_runtime_get_sync(camif->dev);
  474. if (ret < 0)
  475. goto err_pm;
  476. ret = sensor_set_power(camif, 1);
  477. if (!ret)
  478. goto unlock;
  479. pm_runtime_put(camif->dev);
  480. err_pm:
  481. v4l2_fh_release(file);
  482. unlock:
  483. mutex_unlock(&camif->lock);
  484. return ret;
  485. }
  486. static int s3c_camif_close(struct file *file)
  487. {
  488. struct camif_vp *vp = video_drvdata(file);
  489. struct camif_dev *camif = vp->camif;
  490. int ret;
  491. pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
  492. vp->state, vp->owner, task_pid_nr(current));
  493. mutex_lock(&camif->lock);
  494. if (vp->owner == file->private_data) {
  495. camif_stop_capture(vp);
  496. vb2_queue_release(&vp->vb_queue);
  497. vp->owner = NULL;
  498. }
  499. sensor_set_power(camif, 0);
  500. pm_runtime_put(camif->dev);
  501. ret = v4l2_fh_release(file);
  502. mutex_unlock(&camif->lock);
  503. return ret;
  504. }
  505. static unsigned int s3c_camif_poll(struct file *file,
  506. struct poll_table_struct *wait)
  507. {
  508. struct camif_vp *vp = video_drvdata(file);
  509. struct camif_dev *camif = vp->camif;
  510. int ret;
  511. mutex_lock(&camif->lock);
  512. if (vp->owner && vp->owner != file->private_data)
  513. ret = -EBUSY;
  514. else
  515. ret = vb2_poll(&vp->vb_queue, file, wait);
  516. mutex_unlock(&camif->lock);
  517. return ret;
  518. }
  519. static int s3c_camif_mmap(struct file *file, struct vm_area_struct *vma)
  520. {
  521. struct camif_vp *vp = video_drvdata(file);
  522. int ret;
  523. if (vp->owner && vp->owner != file->private_data)
  524. ret = -EBUSY;
  525. else
  526. ret = vb2_mmap(&vp->vb_queue, vma);
  527. return ret;
  528. }
  529. static const struct v4l2_file_operations s3c_camif_fops = {
  530. .owner = THIS_MODULE,
  531. .open = s3c_camif_open,
  532. .release = s3c_camif_close,
  533. .poll = s3c_camif_poll,
  534. .unlocked_ioctl = video_ioctl2,
  535. .mmap = s3c_camif_mmap,
  536. };
  537. /*
  538. * Video node IOCTLs
  539. */
  540. static int s3c_camif_vidioc_querycap(struct file *file, void *priv,
  541. struct v4l2_capability *cap)
  542. {
  543. struct camif_vp *vp = video_drvdata(file);
  544. strlcpy(cap->driver, S3C_CAMIF_DRIVER_NAME, sizeof(cap->driver));
  545. strlcpy(cap->card, S3C_CAMIF_DRIVER_NAME, sizeof(cap->card));
  546. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s.%d",
  547. dev_name(vp->camif->dev), vp->id);
  548. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
  549. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  550. return 0;
  551. }
  552. static int s3c_camif_vidioc_enum_input(struct file *file, void *priv,
  553. struct v4l2_input *input)
  554. {
  555. struct camif_vp *vp = video_drvdata(file);
  556. struct v4l2_subdev *sensor = vp->camif->sensor.sd;
  557. if (input->index || sensor == NULL)
  558. return -EINVAL;
  559. input->type = V4L2_INPUT_TYPE_CAMERA;
  560. strlcpy(input->name, sensor->name, sizeof(input->name));
  561. return 0;
  562. }
  563. static int s3c_camif_vidioc_s_input(struct file *file, void *priv,
  564. unsigned int i)
  565. {
  566. return i == 0 ? 0 : -EINVAL;
  567. }
  568. static int s3c_camif_vidioc_g_input(struct file *file, void *priv,
  569. unsigned int *i)
  570. {
  571. *i = 0;
  572. return 0;
  573. }
  574. static int s3c_camif_vidioc_enum_fmt(struct file *file, void *priv,
  575. struct v4l2_fmtdesc *f)
  576. {
  577. struct camif_vp *vp = video_drvdata(file);
  578. const struct camif_fmt *fmt;
  579. fmt = s3c_camif_find_format(vp, NULL, f->index);
  580. if (!fmt)
  581. return -EINVAL;
  582. strlcpy(f->description, fmt->name, sizeof(f->description));
  583. f->pixelformat = fmt->fourcc;
  584. pr_debug("fmt(%d): %s\n", f->index, f->description);
  585. return 0;
  586. }
  587. static int s3c_camif_vidioc_g_fmt(struct file *file, void *priv,
  588. struct v4l2_format *f)
  589. {
  590. struct camif_vp *vp = video_drvdata(file);
  591. struct v4l2_pix_format *pix = &f->fmt.pix;
  592. struct camif_frame *frame = &vp->out_frame;
  593. const struct camif_fmt *fmt = vp->out_fmt;
  594. pix->bytesperline = frame->f_width * fmt->ybpp;
  595. pix->sizeimage = vp->payload;
  596. pix->pixelformat = fmt->fourcc;
  597. pix->width = frame->f_width;
  598. pix->height = frame->f_height;
  599. pix->field = V4L2_FIELD_NONE;
  600. pix->colorspace = V4L2_COLORSPACE_JPEG;
  601. return 0;
  602. }
  603. static int __camif_video_try_format(struct camif_vp *vp,
  604. struct v4l2_pix_format *pix,
  605. const struct camif_fmt **ffmt)
  606. {
  607. struct camif_dev *camif = vp->camif;
  608. struct v4l2_rect *crop = &camif->camif_crop;
  609. unsigned int wmin, hmin, sc_hrmax, sc_vrmax;
  610. const struct vp_pix_limits *pix_lim;
  611. const struct camif_fmt *fmt;
  612. fmt = s3c_camif_find_format(vp, &pix->pixelformat, 0);
  613. if (WARN_ON(fmt == NULL))
  614. return -EINVAL;
  615. if (ffmt)
  616. *ffmt = fmt;
  617. pix_lim = &camif->variant->vp_pix_limits[vp->id];
  618. pr_debug("fmt: %ux%u, crop: %ux%u, bytesperline: %u\n",
  619. pix->width, pix->height, crop->width, crop->height,
  620. pix->bytesperline);
  621. /*
  622. * Calculate minimum width and height according to the configured
  623. * camera input interface crop rectangle and the resizer's capabilities.
  624. */
  625. sc_hrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->width) - 3));
  626. sc_vrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->height) - 1));
  627. wmin = max_t(u32, pix_lim->min_out_width, crop->width / sc_hrmax);
  628. wmin = round_up(wmin, pix_lim->out_width_align);
  629. hmin = max_t(u32, 8, crop->height / sc_vrmax);
  630. hmin = round_up(hmin, 8);
  631. v4l_bound_align_image(&pix->width, wmin, pix_lim->max_sc_out_width,
  632. ffs(pix_lim->out_width_align) - 1,
  633. &pix->height, hmin, pix_lim->max_height, 0, 0);
  634. pix->bytesperline = pix->width * fmt->ybpp;
  635. pix->sizeimage = (pix->width * pix->height * fmt->depth) / 8;
  636. pix->pixelformat = fmt->fourcc;
  637. pix->colorspace = V4L2_COLORSPACE_JPEG;
  638. pix->field = V4L2_FIELD_NONE;
  639. pr_debug("%ux%u, wmin: %d, hmin: %d, sc_hrmax: %d, sc_vrmax: %d\n",
  640. pix->width, pix->height, wmin, hmin, sc_hrmax, sc_vrmax);
  641. return 0;
  642. }
  643. static int s3c_camif_vidioc_try_fmt(struct file *file, void *priv,
  644. struct v4l2_format *f)
  645. {
  646. struct camif_vp *vp = video_drvdata(file);
  647. return __camif_video_try_format(vp, &f->fmt.pix, NULL);
  648. }
  649. static int s3c_camif_vidioc_s_fmt(struct file *file, void *priv,
  650. struct v4l2_format *f)
  651. {
  652. struct v4l2_pix_format *pix = &f->fmt.pix;
  653. struct camif_vp *vp = video_drvdata(file);
  654. struct camif_frame *out_frame = &vp->out_frame;
  655. const struct camif_fmt *fmt = NULL;
  656. int ret;
  657. pr_debug("[vp%d]\n", vp->id);
  658. if (vb2_is_busy(&vp->vb_queue))
  659. return -EBUSY;
  660. ret = __camif_video_try_format(vp, &f->fmt.pix, &fmt);
  661. if (ret < 0)
  662. return ret;
  663. vp->out_fmt = fmt;
  664. vp->payload = pix->sizeimage;
  665. out_frame->f_width = pix->width;
  666. out_frame->f_height = pix->height;
  667. /* Reset composition rectangle */
  668. out_frame->rect.width = pix->width;
  669. out_frame->rect.height = pix->height;
  670. out_frame->rect.left = 0;
  671. out_frame->rect.top = 0;
  672. if (vp->owner == NULL)
  673. vp->owner = priv;
  674. pr_debug("%ux%u. payload: %u. fmt: %s. %d %d. sizeimage: %d. bpl: %d\n",
  675. out_frame->f_width, out_frame->f_height, vp->payload, fmt->name,
  676. pix->width * pix->height * fmt->depth, fmt->depth,
  677. pix->sizeimage, pix->bytesperline);
  678. return 0;
  679. }
  680. /* Only check pixel formats at the sensor and the camif subdev pads */
  681. static int camif_pipeline_validate(struct camif_dev *camif)
  682. {
  683. struct v4l2_subdev_format src_fmt;
  684. struct media_pad *pad;
  685. int ret;
  686. /* Retrieve format at the sensor subdev source pad */
  687. pad = media_entity_remote_source(&camif->pads[0]);
  688. if (!pad || media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  689. return -EPIPE;
  690. src_fmt.pad = pad->index;
  691. src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  692. ret = v4l2_subdev_call(camif->sensor.sd, pad, get_fmt, NULL, &src_fmt);
  693. if (ret < 0 && ret != -ENOIOCTLCMD)
  694. return -EPIPE;
  695. if (src_fmt.format.width != camif->mbus_fmt.width ||
  696. src_fmt.format.height != camif->mbus_fmt.height ||
  697. src_fmt.format.code != camif->mbus_fmt.code)
  698. return -EPIPE;
  699. return 0;
  700. }
  701. static int s3c_camif_streamon(struct file *file, void *priv,
  702. enum v4l2_buf_type type)
  703. {
  704. struct camif_vp *vp = video_drvdata(file);
  705. struct camif_dev *camif = vp->camif;
  706. struct media_entity *sensor = &camif->sensor.sd->entity;
  707. int ret;
  708. pr_debug("[vp%d]\n", vp->id);
  709. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  710. return -EINVAL;
  711. if (vp->owner && vp->owner != priv)
  712. return -EBUSY;
  713. if (s3c_vp_active(vp))
  714. return 0;
  715. ret = media_entity_pipeline_start(sensor, camif->m_pipeline);
  716. if (ret < 0)
  717. return ret;
  718. ret = camif_pipeline_validate(camif);
  719. if (ret < 0) {
  720. media_entity_pipeline_stop(sensor);
  721. return ret;
  722. }
  723. return vb2_streamon(&vp->vb_queue, type);
  724. }
  725. static int s3c_camif_streamoff(struct file *file, void *priv,
  726. enum v4l2_buf_type type)
  727. {
  728. struct camif_vp *vp = video_drvdata(file);
  729. struct camif_dev *camif = vp->camif;
  730. int ret;
  731. pr_debug("[vp%d]\n", vp->id);
  732. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  733. return -EINVAL;
  734. if (vp->owner && vp->owner != priv)
  735. return -EBUSY;
  736. ret = vb2_streamoff(&vp->vb_queue, type);
  737. if (ret == 0)
  738. media_entity_pipeline_stop(&camif->sensor.sd->entity);
  739. return ret;
  740. }
  741. static int s3c_camif_reqbufs(struct file *file, void *priv,
  742. struct v4l2_requestbuffers *rb)
  743. {
  744. struct camif_vp *vp = video_drvdata(file);
  745. int ret;
  746. pr_debug("[vp%d] rb count: %d, owner: %p, priv: %p\n",
  747. vp->id, rb->count, vp->owner, priv);
  748. if (vp->owner && vp->owner != priv)
  749. return -EBUSY;
  750. if (rb->count)
  751. rb->count = max_t(u32, CAMIF_REQ_BUFS_MIN, rb->count);
  752. else
  753. vp->owner = NULL;
  754. ret = vb2_reqbufs(&vp->vb_queue, rb);
  755. if (!ret) {
  756. vp->reqbufs_count = rb->count;
  757. if (vp->owner == NULL && rb->count > 0)
  758. vp->owner = priv;
  759. }
  760. return ret;
  761. }
  762. static int s3c_camif_querybuf(struct file *file, void *priv,
  763. struct v4l2_buffer *buf)
  764. {
  765. struct camif_vp *vp = video_drvdata(file);
  766. return vb2_querybuf(&vp->vb_queue, buf);
  767. }
  768. static int s3c_camif_qbuf(struct file *file, void *priv,
  769. struct v4l2_buffer *buf)
  770. {
  771. struct camif_vp *vp = video_drvdata(file);
  772. pr_debug("[vp%d]\n", vp->id);
  773. if (vp->owner && vp->owner != priv)
  774. return -EBUSY;
  775. return vb2_qbuf(&vp->vb_queue, buf);
  776. }
  777. static int s3c_camif_dqbuf(struct file *file, void *priv,
  778. struct v4l2_buffer *buf)
  779. {
  780. struct camif_vp *vp = video_drvdata(file);
  781. pr_debug("[vp%d] sequence: %d\n", vp->id, vp->frame_sequence);
  782. if (vp->owner && vp->owner != priv)
  783. return -EBUSY;
  784. return vb2_dqbuf(&vp->vb_queue, buf, file->f_flags & O_NONBLOCK);
  785. }
  786. static int s3c_camif_create_bufs(struct file *file, void *priv,
  787. struct v4l2_create_buffers *create)
  788. {
  789. struct camif_vp *vp = video_drvdata(file);
  790. int ret;
  791. if (vp->owner && vp->owner != priv)
  792. return -EBUSY;
  793. create->count = max_t(u32, 1, create->count);
  794. ret = vb2_create_bufs(&vp->vb_queue, create);
  795. if (!ret && vp->owner == NULL)
  796. vp->owner = priv;
  797. return ret;
  798. }
  799. static int s3c_camif_prepare_buf(struct file *file, void *priv,
  800. struct v4l2_buffer *b)
  801. {
  802. struct camif_vp *vp = video_drvdata(file);
  803. return vb2_prepare_buf(&vp->vb_queue, b);
  804. }
  805. static int s3c_camif_g_selection(struct file *file, void *priv,
  806. struct v4l2_selection *sel)
  807. {
  808. struct camif_vp *vp = video_drvdata(file);
  809. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  810. return -EINVAL;
  811. switch (sel->target) {
  812. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  813. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  814. sel->r.left = 0;
  815. sel->r.top = 0;
  816. sel->r.width = vp->out_frame.f_width;
  817. sel->r.height = vp->out_frame.f_height;
  818. return 0;
  819. case V4L2_SEL_TGT_COMPOSE:
  820. sel->r = vp->out_frame.rect;
  821. return 0;
  822. }
  823. return -EINVAL;
  824. }
  825. static void __camif_try_compose(struct camif_dev *camif, struct camif_vp *vp,
  826. struct v4l2_rect *r)
  827. {
  828. /* s3c244x doesn't support composition */
  829. if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV) {
  830. *r = vp->out_frame.rect;
  831. return;
  832. }
  833. /* TODO: s3c64xx */
  834. }
  835. static int s3c_camif_s_selection(struct file *file, void *priv,
  836. struct v4l2_selection *sel)
  837. {
  838. struct camif_vp *vp = video_drvdata(file);
  839. struct camif_dev *camif = vp->camif;
  840. struct v4l2_rect rect = sel->r;
  841. unsigned long flags;
  842. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  843. sel->target != V4L2_SEL_TGT_COMPOSE)
  844. return -EINVAL;
  845. __camif_try_compose(camif, vp, &rect);
  846. sel->r = rect;
  847. spin_lock_irqsave(&camif->slock, flags);
  848. vp->out_frame.rect = rect;
  849. vp->state |= ST_VP_CONFIG;
  850. spin_unlock_irqrestore(&camif->slock, flags);
  851. pr_debug("type: %#x, target: %#x, flags: %#x, (%d,%d)/%dx%d\n",
  852. sel->type, sel->target, sel->flags,
  853. sel->r.left, sel->r.top, sel->r.width, sel->r.height);
  854. return 0;
  855. }
  856. static const struct v4l2_ioctl_ops s3c_camif_ioctl_ops = {
  857. .vidioc_querycap = s3c_camif_vidioc_querycap,
  858. .vidioc_enum_input = s3c_camif_vidioc_enum_input,
  859. .vidioc_g_input = s3c_camif_vidioc_g_input,
  860. .vidioc_s_input = s3c_camif_vidioc_s_input,
  861. .vidioc_enum_fmt_vid_cap = s3c_camif_vidioc_enum_fmt,
  862. .vidioc_try_fmt_vid_cap = s3c_camif_vidioc_try_fmt,
  863. .vidioc_s_fmt_vid_cap = s3c_camif_vidioc_s_fmt,
  864. .vidioc_g_fmt_vid_cap = s3c_camif_vidioc_g_fmt,
  865. .vidioc_g_selection = s3c_camif_g_selection,
  866. .vidioc_s_selection = s3c_camif_s_selection,
  867. .vidioc_reqbufs = s3c_camif_reqbufs,
  868. .vidioc_querybuf = s3c_camif_querybuf,
  869. .vidioc_prepare_buf = s3c_camif_prepare_buf,
  870. .vidioc_create_bufs = s3c_camif_create_bufs,
  871. .vidioc_qbuf = s3c_camif_qbuf,
  872. .vidioc_dqbuf = s3c_camif_dqbuf,
  873. .vidioc_streamon = s3c_camif_streamon,
  874. .vidioc_streamoff = s3c_camif_streamoff,
  875. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  876. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  877. .vidioc_log_status = v4l2_ctrl_log_status,
  878. };
  879. /*
  880. * Video node controls
  881. */
  882. static int s3c_camif_video_s_ctrl(struct v4l2_ctrl *ctrl)
  883. {
  884. struct camif_vp *vp = ctrl->priv;
  885. struct camif_dev *camif = vp->camif;
  886. unsigned long flags;
  887. pr_debug("[vp%d] ctrl: %s, value: %d\n", vp->id,
  888. ctrl->name, ctrl->val);
  889. spin_lock_irqsave(&camif->slock, flags);
  890. switch (ctrl->id) {
  891. case V4L2_CID_HFLIP:
  892. vp->hflip = ctrl->val;
  893. break;
  894. case V4L2_CID_VFLIP:
  895. vp->vflip = ctrl->val;
  896. break;
  897. }
  898. vp->state |= ST_VP_CONFIG;
  899. spin_unlock_irqrestore(&camif->slock, flags);
  900. return 0;
  901. }
  902. /* Codec and preview video node control ops */
  903. static const struct v4l2_ctrl_ops s3c_camif_video_ctrl_ops = {
  904. .s_ctrl = s3c_camif_video_s_ctrl,
  905. };
  906. int s3c_camif_register_video_node(struct camif_dev *camif, int idx)
  907. {
  908. struct camif_vp *vp = &camif->vp[idx];
  909. struct vb2_queue *q = &vp->vb_queue;
  910. struct video_device *vfd = &vp->vdev;
  911. struct v4l2_ctrl *ctrl;
  912. int ret;
  913. memset(vfd, 0, sizeof(*vfd));
  914. snprintf(vfd->name, sizeof(vfd->name), "camif-%s",
  915. vp->id == 0 ? "codec" : "preview");
  916. vfd->fops = &s3c_camif_fops;
  917. vfd->ioctl_ops = &s3c_camif_ioctl_ops;
  918. vfd->v4l2_dev = &camif->v4l2_dev;
  919. vfd->minor = -1;
  920. vfd->release = video_device_release_empty;
  921. vfd->lock = &camif->lock;
  922. vp->reqbufs_count = 0;
  923. INIT_LIST_HEAD(&vp->pending_buf_q);
  924. INIT_LIST_HEAD(&vp->active_buf_q);
  925. memset(q, 0, sizeof(*q));
  926. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  927. q->io_modes = VB2_MMAP | VB2_USERPTR;
  928. q->ops = &s3c_camif_qops;
  929. q->mem_ops = &vb2_dma_contig_memops;
  930. q->buf_struct_size = sizeof(struct camif_buffer);
  931. q->drv_priv = vp;
  932. ret = vb2_queue_init(q);
  933. if (ret)
  934. goto err_vd_rel;
  935. vp->pad.flags = MEDIA_PAD_FL_SINK;
  936. ret = media_entity_init(&vfd->entity, 1, &vp->pad, 0);
  937. if (ret)
  938. goto err_vd_rel;
  939. video_set_drvdata(vfd, vp);
  940. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  941. v4l2_ctrl_handler_init(&vp->ctrl_handler, 1);
  942. ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
  943. V4L2_CID_HFLIP, 0, 1, 1, 0);
  944. if (ctrl)
  945. ctrl->priv = vp;
  946. ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
  947. V4L2_CID_VFLIP, 0, 1, 1, 0);
  948. if (ctrl)
  949. ctrl->priv = vp;
  950. ret = vp->ctrl_handler.error;
  951. if (ret < 0)
  952. goto err_me_cleanup;
  953. vfd->ctrl_handler = &vp->ctrl_handler;
  954. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  955. if (ret)
  956. goto err_ctrlh_free;
  957. v4l2_info(&camif->v4l2_dev, "registered %s as /dev/%s\n",
  958. vfd->name, video_device_node_name(vfd));
  959. return 0;
  960. err_ctrlh_free:
  961. v4l2_ctrl_handler_free(&vp->ctrl_handler);
  962. err_me_cleanup:
  963. media_entity_cleanup(&vfd->entity);
  964. err_vd_rel:
  965. video_device_release(vfd);
  966. return ret;
  967. }
  968. void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx)
  969. {
  970. struct video_device *vfd = &camif->vp[idx].vdev;
  971. if (video_is_registered(vfd)) {
  972. video_unregister_device(vfd);
  973. media_entity_cleanup(&vfd->entity);
  974. v4l2_ctrl_handler_free(vfd->ctrl_handler);
  975. }
  976. }
  977. /* Media bus pixel formats supported at the camif input */
  978. static const enum v4l2_mbus_pixelcode camif_mbus_formats[] = {
  979. V4L2_MBUS_FMT_YUYV8_2X8,
  980. V4L2_MBUS_FMT_YVYU8_2X8,
  981. V4L2_MBUS_FMT_UYVY8_2X8,
  982. V4L2_MBUS_FMT_VYUY8_2X8,
  983. };
  984. /*
  985. * Camera input interface subdev operations
  986. */
  987. static int s3c_camif_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  988. struct v4l2_subdev_fh *fh,
  989. struct v4l2_subdev_mbus_code_enum *code)
  990. {
  991. if (code->index >= ARRAY_SIZE(camif_mbus_formats))
  992. return -EINVAL;
  993. code->code = camif_mbus_formats[code->index];
  994. return 0;
  995. }
  996. static int s3c_camif_subdev_get_fmt(struct v4l2_subdev *sd,
  997. struct v4l2_subdev_fh *fh,
  998. struct v4l2_subdev_format *fmt)
  999. {
  1000. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1001. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1002. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1003. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  1004. fmt->format = *mf;
  1005. return 0;
  1006. }
  1007. mutex_lock(&camif->lock);
  1008. switch (fmt->pad) {
  1009. case CAMIF_SD_PAD_SINK:
  1010. /* full camera input pixel size */
  1011. *mf = camif->mbus_fmt;
  1012. break;
  1013. case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
  1014. /* crop rectangle at camera interface input */
  1015. mf->width = camif->camif_crop.width;
  1016. mf->height = camif->camif_crop.height;
  1017. mf->code = camif->mbus_fmt.code;
  1018. break;
  1019. }
  1020. mutex_unlock(&camif->lock);
  1021. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1022. return 0;
  1023. }
  1024. static void __camif_subdev_try_format(struct camif_dev *camif,
  1025. struct v4l2_mbus_framefmt *mf, int pad)
  1026. {
  1027. const struct s3c_camif_variant *variant = camif->variant;
  1028. const struct vp_pix_limits *pix_lim;
  1029. int i = ARRAY_SIZE(camif_mbus_formats);
  1030. /* FIXME: constraints against codec or preview path ? */
  1031. pix_lim = &variant->vp_pix_limits[VP_CODEC];
  1032. while (i-- >= 0)
  1033. if (camif_mbus_formats[i] == mf->code)
  1034. break;
  1035. mf->code = camif_mbus_formats[i];
  1036. if (pad == CAMIF_SD_PAD_SINK) {
  1037. v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
  1038. ffs(pix_lim->out_width_align) - 1,
  1039. &mf->height, 8, CAMIF_MAX_PIX_HEIGHT, 0,
  1040. 0);
  1041. } else {
  1042. struct v4l2_rect *crop = &camif->camif_crop;
  1043. v4l_bound_align_image(&mf->width, 8, crop->width,
  1044. ffs(pix_lim->out_width_align) - 1,
  1045. &mf->height, 8, crop->height,
  1046. 0, 0);
  1047. }
  1048. v4l2_dbg(1, debug, &camif->subdev, "%ux%u\n", mf->width, mf->height);
  1049. }
  1050. static int s3c_camif_subdev_set_fmt(struct v4l2_subdev *sd,
  1051. struct v4l2_subdev_fh *fh,
  1052. struct v4l2_subdev_format *fmt)
  1053. {
  1054. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1055. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1056. struct v4l2_rect *crop = &camif->camif_crop;
  1057. int i;
  1058. v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %ux%u\n",
  1059. fmt->pad, mf->code, mf->width, mf->height);
  1060. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1061. mutex_lock(&camif->lock);
  1062. /*
  1063. * No pixel format change at the camera input is allowed
  1064. * while streaming.
  1065. */
  1066. if (vb2_is_busy(&camif->vp[VP_CODEC].vb_queue) ||
  1067. vb2_is_busy(&camif->vp[VP_PREVIEW].vb_queue)) {
  1068. mutex_unlock(&camif->lock);
  1069. return -EBUSY;
  1070. }
  1071. __camif_subdev_try_format(camif, mf, fmt->pad);
  1072. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1073. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  1074. *mf = fmt->format;
  1075. mutex_unlock(&camif->lock);
  1076. return 0;
  1077. }
  1078. switch (fmt->pad) {
  1079. case CAMIF_SD_PAD_SINK:
  1080. camif->mbus_fmt = *mf;
  1081. /* Reset sink crop rectangle. */
  1082. crop->width = mf->width;
  1083. crop->height = mf->height;
  1084. crop->left = 0;
  1085. crop->top = 0;
  1086. /*
  1087. * Reset source format (the camif's crop rectangle)
  1088. * and the video output resolution.
  1089. */
  1090. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1091. struct camif_frame *frame = &camif->vp[i].out_frame;
  1092. frame->rect = *crop;
  1093. frame->f_width = mf->width;
  1094. frame->f_height = mf->height;
  1095. }
  1096. break;
  1097. case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
  1098. /* Pixel format can be only changed on the sink pad. */
  1099. mf->code = camif->mbus_fmt.code;
  1100. mf->width = crop->width;
  1101. mf->height = crop->height;
  1102. break;
  1103. }
  1104. mutex_unlock(&camif->lock);
  1105. return 0;
  1106. }
  1107. static int s3c_camif_subdev_get_selection(struct v4l2_subdev *sd,
  1108. struct v4l2_subdev_fh *fh,
  1109. struct v4l2_subdev_selection *sel)
  1110. {
  1111. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1112. struct v4l2_rect *crop = &camif->camif_crop;
  1113. struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
  1114. if ((sel->target != V4L2_SEL_TGT_CROP &&
  1115. sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
  1116. sel->pad != CAMIF_SD_PAD_SINK)
  1117. return -EINVAL;
  1118. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1119. sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
  1120. return 0;
  1121. }
  1122. mutex_lock(&camif->lock);
  1123. if (sel->target == V4L2_SEL_TGT_CROP) {
  1124. sel->r = *crop;
  1125. } else { /* crop bounds */
  1126. sel->r.width = mf->width;
  1127. sel->r.height = mf->height;
  1128. sel->r.left = 0;
  1129. sel->r.top = 0;
  1130. }
  1131. mutex_unlock(&camif->lock);
  1132. v4l2_dbg(1, debug, sd, "%s: crop: (%d,%d) %dx%d, size: %ux%u\n",
  1133. __func__, crop->left, crop->top, crop->width,
  1134. crop->height, mf->width, mf->height);
  1135. return 0;
  1136. }
  1137. static void __camif_try_crop(struct camif_dev *camif, struct v4l2_rect *r)
  1138. {
  1139. struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
  1140. const struct camif_pix_limits *pix_lim = &camif->variant->pix_limits;
  1141. unsigned int left = 2 * r->left;
  1142. unsigned int top = 2 * r->top;
  1143. /*
  1144. * Following constraints must be met:
  1145. * - r->width + 2 * r->left = mf->width;
  1146. * - r->height + 2 * r->top = mf->height;
  1147. * - crop rectangle size and position must be aligned
  1148. * to 8 or 2 pixels, depending on SoC version.
  1149. */
  1150. v4l_bound_align_image(&r->width, 0, mf->width,
  1151. ffs(pix_lim->win_hor_offset_align) - 1,
  1152. &r->height, 0, mf->height, 1, 0);
  1153. v4l_bound_align_image(&left, 0, mf->width - r->width,
  1154. ffs(pix_lim->win_hor_offset_align),
  1155. &top, 0, mf->height - r->height, 2, 0);
  1156. r->left = left / 2;
  1157. r->top = top / 2;
  1158. r->width = mf->width - left;
  1159. r->height = mf->height - top;
  1160. /*
  1161. * Make sure we either downscale or upscale both the pixel
  1162. * width and height. Just return current crop rectangle if
  1163. * this scaler constraint is not met.
  1164. */
  1165. if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV &&
  1166. camif_is_streaming(camif)) {
  1167. unsigned int i;
  1168. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1169. struct v4l2_rect *or = &camif->vp[i].out_frame.rect;
  1170. if ((or->width > r->width) == (or->height > r->height))
  1171. continue;
  1172. *r = camif->camif_crop;
  1173. pr_debug("Width/height scaling direction limitation\n");
  1174. break;
  1175. }
  1176. }
  1177. v4l2_dbg(1, debug, &camif->v4l2_dev, "crop: (%d,%d)/%dx%d, fmt: %ux%u\n",
  1178. r->left, r->top, r->width, r->height, mf->width, mf->height);
  1179. }
  1180. static int s3c_camif_subdev_set_selection(struct v4l2_subdev *sd,
  1181. struct v4l2_subdev_fh *fh,
  1182. struct v4l2_subdev_selection *sel)
  1183. {
  1184. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1185. struct v4l2_rect *crop = &camif->camif_crop;
  1186. struct camif_scaler scaler;
  1187. if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != CAMIF_SD_PAD_SINK)
  1188. return -EINVAL;
  1189. mutex_lock(&camif->lock);
  1190. __camif_try_crop(camif, &sel->r);
  1191. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1192. *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r;
  1193. } else {
  1194. unsigned long flags;
  1195. unsigned int i;
  1196. spin_lock_irqsave(&camif->slock, flags);
  1197. *crop = sel->r;
  1198. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1199. struct camif_vp *vp = &camif->vp[i];
  1200. scaler = vp->scaler;
  1201. if (s3c_camif_get_scaler_config(vp, &scaler))
  1202. continue;
  1203. vp->scaler = scaler;
  1204. vp->state |= ST_VP_CONFIG;
  1205. }
  1206. spin_unlock_irqrestore(&camif->slock, flags);
  1207. }
  1208. mutex_unlock(&camif->lock);
  1209. v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %u, f_h: %u\n",
  1210. __func__, crop->left, crop->top, crop->width, crop->height,
  1211. camif->mbus_fmt.width, camif->mbus_fmt.height);
  1212. return 0;
  1213. }
  1214. static const struct v4l2_subdev_pad_ops s3c_camif_subdev_pad_ops = {
  1215. .enum_mbus_code = s3c_camif_subdev_enum_mbus_code,
  1216. .get_selection = s3c_camif_subdev_get_selection,
  1217. .set_selection = s3c_camif_subdev_set_selection,
  1218. .get_fmt = s3c_camif_subdev_get_fmt,
  1219. .set_fmt = s3c_camif_subdev_set_fmt,
  1220. };
  1221. static struct v4l2_subdev_ops s3c_camif_subdev_ops = {
  1222. .pad = &s3c_camif_subdev_pad_ops,
  1223. };
  1224. static int s3c_camif_subdev_s_ctrl(struct v4l2_ctrl *ctrl)
  1225. {
  1226. struct camif_dev *camif = container_of(ctrl->handler, struct camif_dev,
  1227. ctrl_handler);
  1228. unsigned long flags;
  1229. spin_lock_irqsave(&camif->slock, flags);
  1230. switch (ctrl->id) {
  1231. case V4L2_CID_COLORFX:
  1232. camif->colorfx = camif->ctrl_colorfx->val;
  1233. /* Set Cb, Cr */
  1234. switch (ctrl->val) {
  1235. case V4L2_COLORFX_SEPIA:
  1236. camif->colorfx_cb = 115;
  1237. camif->colorfx_cr = 145;
  1238. break;
  1239. case V4L2_COLORFX_SET_CBCR:
  1240. camif->colorfx_cb = camif->ctrl_colorfx_cbcr->val >> 8;
  1241. camif->colorfx_cr = camif->ctrl_colorfx_cbcr->val & 0xff;
  1242. break;
  1243. default:
  1244. /* for V4L2_COLORFX_BW and others */
  1245. camif->colorfx_cb = 128;
  1246. camif->colorfx_cr = 128;
  1247. }
  1248. break;
  1249. case V4L2_CID_TEST_PATTERN:
  1250. camif->test_pattern = camif->ctrl_test_pattern->val;
  1251. break;
  1252. default:
  1253. WARN_ON(1);
  1254. }
  1255. camif->vp[VP_CODEC].state |= ST_VP_CONFIG;
  1256. camif->vp[VP_PREVIEW].state |= ST_VP_CONFIG;
  1257. spin_unlock_irqrestore(&camif->slock, flags);
  1258. return 0;
  1259. }
  1260. static const struct v4l2_ctrl_ops s3c_camif_subdev_ctrl_ops = {
  1261. .s_ctrl = s3c_camif_subdev_s_ctrl,
  1262. };
  1263. static const char * const s3c_camif_test_pattern_menu[] = {
  1264. "Disabled",
  1265. "Color bars",
  1266. "Horizontal increment",
  1267. "Vertical increment",
  1268. };
  1269. int s3c_camif_create_subdev(struct camif_dev *camif)
  1270. {
  1271. struct v4l2_ctrl_handler *handler = &camif->ctrl_handler;
  1272. struct v4l2_subdev *sd = &camif->subdev;
  1273. int ret;
  1274. v4l2_subdev_init(sd, &s3c_camif_subdev_ops);
  1275. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1276. strlcpy(sd->name, "S3C-CAMIF", sizeof(sd->name));
  1277. camif->pads[CAMIF_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1278. camif->pads[CAMIF_SD_PAD_SOURCE_C].flags = MEDIA_PAD_FL_SOURCE;
  1279. camif->pads[CAMIF_SD_PAD_SOURCE_P].flags = MEDIA_PAD_FL_SOURCE;
  1280. ret = media_entity_init(&sd->entity, CAMIF_SD_PADS_NUM,
  1281. camif->pads, 0);
  1282. if (ret)
  1283. return ret;
  1284. v4l2_ctrl_handler_init(handler, 3);
  1285. camif->ctrl_test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  1286. &s3c_camif_subdev_ctrl_ops, V4L2_CID_TEST_PATTERN,
  1287. ARRAY_SIZE(s3c_camif_test_pattern_menu) - 1, 0, 0,
  1288. s3c_camif_test_pattern_menu);
  1289. camif->ctrl_colorfx = v4l2_ctrl_new_std_menu(handler,
  1290. &s3c_camif_subdev_ctrl_ops,
  1291. V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR,
  1292. ~0x981f, V4L2_COLORFX_NONE);
  1293. camif->ctrl_colorfx_cbcr = v4l2_ctrl_new_std(handler,
  1294. &s3c_camif_subdev_ctrl_ops,
  1295. V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
  1296. if (handler->error) {
  1297. v4l2_ctrl_handler_free(handler);
  1298. media_entity_cleanup(&sd->entity);
  1299. return handler->error;
  1300. }
  1301. v4l2_ctrl_auto_cluster(2, &camif->ctrl_colorfx,
  1302. V4L2_COLORFX_SET_CBCR, false);
  1303. if (!camif->variant->has_img_effect) {
  1304. camif->ctrl_colorfx->flags |= V4L2_CTRL_FLAG_DISABLED;
  1305. camif->ctrl_colorfx_cbcr->flags |= V4L2_CTRL_FLAG_DISABLED;
  1306. }
  1307. sd->ctrl_handler = handler;
  1308. v4l2_set_subdevdata(sd, camif);
  1309. return 0;
  1310. }
  1311. void s3c_camif_unregister_subdev(struct camif_dev *camif)
  1312. {
  1313. struct v4l2_subdev *sd = &camif->subdev;
  1314. /* Return if not registered */
  1315. if (v4l2_get_subdevdata(sd) == NULL)
  1316. return;
  1317. v4l2_device_unregister_subdev(sd);
  1318. media_entity_cleanup(&sd->entity);
  1319. v4l2_ctrl_handler_free(&camif->ctrl_handler);
  1320. v4l2_set_subdevdata(sd, NULL);
  1321. }
  1322. int s3c_camif_set_defaults(struct camif_dev *camif)
  1323. {
  1324. unsigned int ip_rev = camif->variant->ip_revision;
  1325. int i;
  1326. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1327. struct camif_vp *vp = &camif->vp[i];
  1328. struct camif_frame *f = &vp->out_frame;
  1329. vp->camif = camif;
  1330. vp->id = i;
  1331. vp->offset = camif->variant->vp_offset;
  1332. if (ip_rev == S3C244X_CAMIF_IP_REV)
  1333. vp->fmt_flags = i ? FMT_FL_S3C24XX_PREVIEW :
  1334. FMT_FL_S3C24XX_CODEC;
  1335. else
  1336. vp->fmt_flags = FMT_FL_S3C64XX;
  1337. vp->out_fmt = s3c_camif_find_format(vp, NULL, 0);
  1338. BUG_ON(vp->out_fmt == NULL);
  1339. memset(f, 0, sizeof(*f));
  1340. f->f_width = CAMIF_DEF_WIDTH;
  1341. f->f_height = CAMIF_DEF_HEIGHT;
  1342. f->rect.width = CAMIF_DEF_WIDTH;
  1343. f->rect.height = CAMIF_DEF_HEIGHT;
  1344. /* Scaler is always enabled */
  1345. vp->scaler.enable = 1;
  1346. vp->payload = (f->f_width * f->f_height *
  1347. vp->out_fmt->depth) / 8;
  1348. }
  1349. memset(&camif->mbus_fmt, 0, sizeof(camif->mbus_fmt));
  1350. camif->mbus_fmt.width = CAMIF_DEF_WIDTH;
  1351. camif->mbus_fmt.height = CAMIF_DEF_HEIGHT;
  1352. camif->mbus_fmt.code = camif_mbus_formats[0];
  1353. memset(&camif->camif_crop, 0, sizeof(camif->camif_crop));
  1354. camif->camif_crop.width = CAMIF_DEF_WIDTH;
  1355. camif->camif_crop.height = CAMIF_DEF_HEIGHT;
  1356. return 0;
  1357. }