camif-capture.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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: %pad cb: %pad cr: %pad\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 void stop_streaming(struct vb2_queue *vq)
  361. {
  362. struct camif_vp *vp = vb2_get_drv_priv(vq);
  363. 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 const struct vb2_ops s3c_camif_qops = {
  443. .queue_setup = queue_setup,
  444. .buf_prepare = buffer_prepare,
  445. .buf_queue = buffer_queue,
  446. .wait_prepare = vb2_ops_wait_prepare,
  447. .wait_finish = vb2_ops_wait_finish,
  448. .start_streaming = start_streaming,
  449. .stop_streaming = stop_streaming,
  450. };
  451. static int s3c_camif_open(struct file *file)
  452. {
  453. struct camif_vp *vp = video_drvdata(file);
  454. struct camif_dev *camif = vp->camif;
  455. int ret;
  456. pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
  457. vp->state, vp->owner, task_pid_nr(current));
  458. if (mutex_lock_interruptible(&camif->lock))
  459. return -ERESTARTSYS;
  460. ret = v4l2_fh_open(file);
  461. if (ret < 0)
  462. goto unlock;
  463. ret = pm_runtime_get_sync(camif->dev);
  464. if (ret < 0)
  465. goto err_pm;
  466. ret = sensor_set_power(camif, 1);
  467. if (!ret)
  468. goto unlock;
  469. pm_runtime_put(camif->dev);
  470. err_pm:
  471. v4l2_fh_release(file);
  472. unlock:
  473. mutex_unlock(&camif->lock);
  474. return ret;
  475. }
  476. static int s3c_camif_close(struct file *file)
  477. {
  478. struct camif_vp *vp = video_drvdata(file);
  479. struct camif_dev *camif = vp->camif;
  480. int ret;
  481. pr_debug("[vp%d] state: %#x, owner: %p, pid: %d\n", vp->id,
  482. vp->state, vp->owner, task_pid_nr(current));
  483. mutex_lock(&camif->lock);
  484. if (vp->owner == file->private_data) {
  485. camif_stop_capture(vp);
  486. vb2_queue_release(&vp->vb_queue);
  487. vp->owner = NULL;
  488. }
  489. sensor_set_power(camif, 0);
  490. pm_runtime_put(camif->dev);
  491. ret = v4l2_fh_release(file);
  492. mutex_unlock(&camif->lock);
  493. return ret;
  494. }
  495. static unsigned int s3c_camif_poll(struct file *file,
  496. struct poll_table_struct *wait)
  497. {
  498. struct camif_vp *vp = video_drvdata(file);
  499. struct camif_dev *camif = vp->camif;
  500. int ret;
  501. mutex_lock(&camif->lock);
  502. if (vp->owner && vp->owner != file->private_data)
  503. ret = -EBUSY;
  504. else
  505. ret = vb2_poll(&vp->vb_queue, file, wait);
  506. mutex_unlock(&camif->lock);
  507. return ret;
  508. }
  509. static int s3c_camif_mmap(struct file *file, struct vm_area_struct *vma)
  510. {
  511. struct camif_vp *vp = video_drvdata(file);
  512. int ret;
  513. if (vp->owner && vp->owner != file->private_data)
  514. ret = -EBUSY;
  515. else
  516. ret = vb2_mmap(&vp->vb_queue, vma);
  517. return ret;
  518. }
  519. static const struct v4l2_file_operations s3c_camif_fops = {
  520. .owner = THIS_MODULE,
  521. .open = s3c_camif_open,
  522. .release = s3c_camif_close,
  523. .poll = s3c_camif_poll,
  524. .unlocked_ioctl = video_ioctl2,
  525. .mmap = s3c_camif_mmap,
  526. };
  527. /*
  528. * Video node IOCTLs
  529. */
  530. static int s3c_camif_vidioc_querycap(struct file *file, void *priv,
  531. struct v4l2_capability *cap)
  532. {
  533. struct camif_vp *vp = video_drvdata(file);
  534. strlcpy(cap->driver, S3C_CAMIF_DRIVER_NAME, sizeof(cap->driver));
  535. strlcpy(cap->card, S3C_CAMIF_DRIVER_NAME, sizeof(cap->card));
  536. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s.%d",
  537. dev_name(vp->camif->dev), vp->id);
  538. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
  539. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  540. return 0;
  541. }
  542. static int s3c_camif_vidioc_enum_input(struct file *file, void *priv,
  543. struct v4l2_input *input)
  544. {
  545. struct camif_vp *vp = video_drvdata(file);
  546. struct v4l2_subdev *sensor = vp->camif->sensor.sd;
  547. if (input->index || sensor == NULL)
  548. return -EINVAL;
  549. input->type = V4L2_INPUT_TYPE_CAMERA;
  550. strlcpy(input->name, sensor->name, sizeof(input->name));
  551. return 0;
  552. }
  553. static int s3c_camif_vidioc_s_input(struct file *file, void *priv,
  554. unsigned int i)
  555. {
  556. return i == 0 ? 0 : -EINVAL;
  557. }
  558. static int s3c_camif_vidioc_g_input(struct file *file, void *priv,
  559. unsigned int *i)
  560. {
  561. *i = 0;
  562. return 0;
  563. }
  564. static int s3c_camif_vidioc_enum_fmt(struct file *file, void *priv,
  565. struct v4l2_fmtdesc *f)
  566. {
  567. struct camif_vp *vp = video_drvdata(file);
  568. const struct camif_fmt *fmt;
  569. fmt = s3c_camif_find_format(vp, NULL, f->index);
  570. if (!fmt)
  571. return -EINVAL;
  572. strlcpy(f->description, fmt->name, sizeof(f->description));
  573. f->pixelformat = fmt->fourcc;
  574. pr_debug("fmt(%d): %s\n", f->index, f->description);
  575. return 0;
  576. }
  577. static int s3c_camif_vidioc_g_fmt(struct file *file, void *priv,
  578. struct v4l2_format *f)
  579. {
  580. struct camif_vp *vp = video_drvdata(file);
  581. struct v4l2_pix_format *pix = &f->fmt.pix;
  582. struct camif_frame *frame = &vp->out_frame;
  583. const struct camif_fmt *fmt = vp->out_fmt;
  584. pix->bytesperline = frame->f_width * fmt->ybpp;
  585. pix->sizeimage = vp->payload;
  586. pix->pixelformat = fmt->fourcc;
  587. pix->width = frame->f_width;
  588. pix->height = frame->f_height;
  589. pix->field = V4L2_FIELD_NONE;
  590. pix->colorspace = V4L2_COLORSPACE_JPEG;
  591. return 0;
  592. }
  593. static int __camif_video_try_format(struct camif_vp *vp,
  594. struct v4l2_pix_format *pix,
  595. const struct camif_fmt **ffmt)
  596. {
  597. struct camif_dev *camif = vp->camif;
  598. struct v4l2_rect *crop = &camif->camif_crop;
  599. unsigned int wmin, hmin, sc_hrmax, sc_vrmax;
  600. const struct vp_pix_limits *pix_lim;
  601. const struct camif_fmt *fmt;
  602. fmt = s3c_camif_find_format(vp, &pix->pixelformat, 0);
  603. if (WARN_ON(fmt == NULL))
  604. return -EINVAL;
  605. if (ffmt)
  606. *ffmt = fmt;
  607. pix_lim = &camif->variant->vp_pix_limits[vp->id];
  608. pr_debug("fmt: %ux%u, crop: %ux%u, bytesperline: %u\n",
  609. pix->width, pix->height, crop->width, crop->height,
  610. pix->bytesperline);
  611. /*
  612. * Calculate minimum width and height according to the configured
  613. * camera input interface crop rectangle and the resizer's capabilities.
  614. */
  615. sc_hrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->width) - 3));
  616. sc_vrmax = min(SCALER_MAX_RATIO, 1 << (ffs(crop->height) - 1));
  617. wmin = max_t(u32, pix_lim->min_out_width, crop->width / sc_hrmax);
  618. wmin = round_up(wmin, pix_lim->out_width_align);
  619. hmin = max_t(u32, 8, crop->height / sc_vrmax);
  620. hmin = round_up(hmin, 8);
  621. v4l_bound_align_image(&pix->width, wmin, pix_lim->max_sc_out_width,
  622. ffs(pix_lim->out_width_align) - 1,
  623. &pix->height, hmin, pix_lim->max_height, 0, 0);
  624. pix->bytesperline = pix->width * fmt->ybpp;
  625. pix->sizeimage = (pix->width * pix->height * fmt->depth) / 8;
  626. pix->pixelformat = fmt->fourcc;
  627. pix->colorspace = V4L2_COLORSPACE_JPEG;
  628. pix->field = V4L2_FIELD_NONE;
  629. pr_debug("%ux%u, wmin: %d, hmin: %d, sc_hrmax: %d, sc_vrmax: %d\n",
  630. pix->width, pix->height, wmin, hmin, sc_hrmax, sc_vrmax);
  631. return 0;
  632. }
  633. static int s3c_camif_vidioc_try_fmt(struct file *file, void *priv,
  634. struct v4l2_format *f)
  635. {
  636. struct camif_vp *vp = video_drvdata(file);
  637. return __camif_video_try_format(vp, &f->fmt.pix, NULL);
  638. }
  639. static int s3c_camif_vidioc_s_fmt(struct file *file, void *priv,
  640. struct v4l2_format *f)
  641. {
  642. struct v4l2_pix_format *pix = &f->fmt.pix;
  643. struct camif_vp *vp = video_drvdata(file);
  644. struct camif_frame *out_frame = &vp->out_frame;
  645. const struct camif_fmt *fmt = NULL;
  646. int ret;
  647. pr_debug("[vp%d]\n", vp->id);
  648. if (vb2_is_busy(&vp->vb_queue))
  649. return -EBUSY;
  650. ret = __camif_video_try_format(vp, &f->fmt.pix, &fmt);
  651. if (ret < 0)
  652. return ret;
  653. vp->out_fmt = fmt;
  654. vp->payload = pix->sizeimage;
  655. out_frame->f_width = pix->width;
  656. out_frame->f_height = pix->height;
  657. /* Reset composition rectangle */
  658. out_frame->rect.width = pix->width;
  659. out_frame->rect.height = pix->height;
  660. out_frame->rect.left = 0;
  661. out_frame->rect.top = 0;
  662. if (vp->owner == NULL)
  663. vp->owner = priv;
  664. pr_debug("%ux%u. payload: %u. fmt: %s. %d %d. sizeimage: %d. bpl: %d\n",
  665. out_frame->f_width, out_frame->f_height, vp->payload, fmt->name,
  666. pix->width * pix->height * fmt->depth, fmt->depth,
  667. pix->sizeimage, pix->bytesperline);
  668. return 0;
  669. }
  670. /* Only check pixel formats at the sensor and the camif subdev pads */
  671. static int camif_pipeline_validate(struct camif_dev *camif)
  672. {
  673. struct v4l2_subdev_format src_fmt;
  674. struct media_pad *pad;
  675. int ret;
  676. /* Retrieve format at the sensor subdev source pad */
  677. pad = media_entity_remote_pad(&camif->pads[0]);
  678. if (!pad || media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  679. return -EPIPE;
  680. src_fmt.pad = pad->index;
  681. src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  682. ret = v4l2_subdev_call(camif->sensor.sd, pad, get_fmt, NULL, &src_fmt);
  683. if (ret < 0 && ret != -ENOIOCTLCMD)
  684. return -EPIPE;
  685. if (src_fmt.format.width != camif->mbus_fmt.width ||
  686. src_fmt.format.height != camif->mbus_fmt.height ||
  687. src_fmt.format.code != camif->mbus_fmt.code)
  688. return -EPIPE;
  689. return 0;
  690. }
  691. static int s3c_camif_streamon(struct file *file, void *priv,
  692. enum v4l2_buf_type type)
  693. {
  694. struct camif_vp *vp = video_drvdata(file);
  695. struct camif_dev *camif = vp->camif;
  696. struct media_entity *sensor = &camif->sensor.sd->entity;
  697. int ret;
  698. pr_debug("[vp%d]\n", vp->id);
  699. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  700. return -EINVAL;
  701. if (vp->owner && vp->owner != priv)
  702. return -EBUSY;
  703. if (s3c_vp_active(vp))
  704. return 0;
  705. ret = media_entity_pipeline_start(sensor, camif->m_pipeline);
  706. if (ret < 0)
  707. return ret;
  708. ret = camif_pipeline_validate(camif);
  709. if (ret < 0) {
  710. media_entity_pipeline_stop(sensor);
  711. return ret;
  712. }
  713. return vb2_streamon(&vp->vb_queue, type);
  714. }
  715. static int s3c_camif_streamoff(struct file *file, void *priv,
  716. enum v4l2_buf_type type)
  717. {
  718. struct camif_vp *vp = video_drvdata(file);
  719. struct camif_dev *camif = vp->camif;
  720. int ret;
  721. pr_debug("[vp%d]\n", vp->id);
  722. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  723. return -EINVAL;
  724. if (vp->owner && vp->owner != priv)
  725. return -EBUSY;
  726. ret = vb2_streamoff(&vp->vb_queue, type);
  727. if (ret == 0)
  728. media_entity_pipeline_stop(&camif->sensor.sd->entity);
  729. return ret;
  730. }
  731. static int s3c_camif_reqbufs(struct file *file, void *priv,
  732. struct v4l2_requestbuffers *rb)
  733. {
  734. struct camif_vp *vp = video_drvdata(file);
  735. int ret;
  736. pr_debug("[vp%d] rb count: %d, owner: %p, priv: %p\n",
  737. vp->id, rb->count, vp->owner, priv);
  738. if (vp->owner && vp->owner != priv)
  739. return -EBUSY;
  740. if (rb->count)
  741. rb->count = max_t(u32, CAMIF_REQ_BUFS_MIN, rb->count);
  742. else
  743. vp->owner = NULL;
  744. ret = vb2_reqbufs(&vp->vb_queue, rb);
  745. if (ret < 0)
  746. return ret;
  747. if (rb->count && rb->count < CAMIF_REQ_BUFS_MIN) {
  748. rb->count = 0;
  749. vb2_reqbufs(&vp->vb_queue, rb);
  750. ret = -ENOMEM;
  751. }
  752. vp->reqbufs_count = rb->count;
  753. if (vp->owner == NULL && rb->count > 0)
  754. vp->owner = priv;
  755. return ret;
  756. }
  757. static int s3c_camif_querybuf(struct file *file, void *priv,
  758. struct v4l2_buffer *buf)
  759. {
  760. struct camif_vp *vp = video_drvdata(file);
  761. return vb2_querybuf(&vp->vb_queue, buf);
  762. }
  763. static int s3c_camif_qbuf(struct file *file, void *priv,
  764. struct v4l2_buffer *buf)
  765. {
  766. struct camif_vp *vp = video_drvdata(file);
  767. pr_debug("[vp%d]\n", vp->id);
  768. if (vp->owner && vp->owner != priv)
  769. return -EBUSY;
  770. return vb2_qbuf(&vp->vb_queue, buf);
  771. }
  772. static int s3c_camif_dqbuf(struct file *file, void *priv,
  773. struct v4l2_buffer *buf)
  774. {
  775. struct camif_vp *vp = video_drvdata(file);
  776. pr_debug("[vp%d] sequence: %d\n", vp->id, vp->frame_sequence);
  777. if (vp->owner && vp->owner != priv)
  778. return -EBUSY;
  779. return vb2_dqbuf(&vp->vb_queue, buf, file->f_flags & O_NONBLOCK);
  780. }
  781. static int s3c_camif_create_bufs(struct file *file, void *priv,
  782. struct v4l2_create_buffers *create)
  783. {
  784. struct camif_vp *vp = video_drvdata(file);
  785. int ret;
  786. if (vp->owner && vp->owner != priv)
  787. return -EBUSY;
  788. create->count = max_t(u32, 1, create->count);
  789. ret = vb2_create_bufs(&vp->vb_queue, create);
  790. if (!ret && vp->owner == NULL)
  791. vp->owner = priv;
  792. return ret;
  793. }
  794. static int s3c_camif_prepare_buf(struct file *file, void *priv,
  795. struct v4l2_buffer *b)
  796. {
  797. struct camif_vp *vp = video_drvdata(file);
  798. return vb2_prepare_buf(&vp->vb_queue, b);
  799. }
  800. static int s3c_camif_g_selection(struct file *file, void *priv,
  801. struct v4l2_selection *sel)
  802. {
  803. struct camif_vp *vp = video_drvdata(file);
  804. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  805. return -EINVAL;
  806. switch (sel->target) {
  807. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  808. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  809. sel->r.left = 0;
  810. sel->r.top = 0;
  811. sel->r.width = vp->out_frame.f_width;
  812. sel->r.height = vp->out_frame.f_height;
  813. return 0;
  814. case V4L2_SEL_TGT_COMPOSE:
  815. sel->r = vp->out_frame.rect;
  816. return 0;
  817. }
  818. return -EINVAL;
  819. }
  820. static void __camif_try_compose(struct camif_dev *camif, struct camif_vp *vp,
  821. struct v4l2_rect *r)
  822. {
  823. /* s3c244x doesn't support composition */
  824. if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV) {
  825. *r = vp->out_frame.rect;
  826. return;
  827. }
  828. /* TODO: s3c64xx */
  829. }
  830. static int s3c_camif_s_selection(struct file *file, void *priv,
  831. struct v4l2_selection *sel)
  832. {
  833. struct camif_vp *vp = video_drvdata(file);
  834. struct camif_dev *camif = vp->camif;
  835. struct v4l2_rect rect = sel->r;
  836. unsigned long flags;
  837. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  838. sel->target != V4L2_SEL_TGT_COMPOSE)
  839. return -EINVAL;
  840. __camif_try_compose(camif, vp, &rect);
  841. sel->r = rect;
  842. spin_lock_irqsave(&camif->slock, flags);
  843. vp->out_frame.rect = rect;
  844. vp->state |= ST_VP_CONFIG;
  845. spin_unlock_irqrestore(&camif->slock, flags);
  846. pr_debug("type: %#x, target: %#x, flags: %#x, (%d,%d)/%dx%d\n",
  847. sel->type, sel->target, sel->flags,
  848. sel->r.left, sel->r.top, sel->r.width, sel->r.height);
  849. return 0;
  850. }
  851. static const struct v4l2_ioctl_ops s3c_camif_ioctl_ops = {
  852. .vidioc_querycap = s3c_camif_vidioc_querycap,
  853. .vidioc_enum_input = s3c_camif_vidioc_enum_input,
  854. .vidioc_g_input = s3c_camif_vidioc_g_input,
  855. .vidioc_s_input = s3c_camif_vidioc_s_input,
  856. .vidioc_enum_fmt_vid_cap = s3c_camif_vidioc_enum_fmt,
  857. .vidioc_try_fmt_vid_cap = s3c_camif_vidioc_try_fmt,
  858. .vidioc_s_fmt_vid_cap = s3c_camif_vidioc_s_fmt,
  859. .vidioc_g_fmt_vid_cap = s3c_camif_vidioc_g_fmt,
  860. .vidioc_g_selection = s3c_camif_g_selection,
  861. .vidioc_s_selection = s3c_camif_s_selection,
  862. .vidioc_reqbufs = s3c_camif_reqbufs,
  863. .vidioc_querybuf = s3c_camif_querybuf,
  864. .vidioc_prepare_buf = s3c_camif_prepare_buf,
  865. .vidioc_create_bufs = s3c_camif_create_bufs,
  866. .vidioc_qbuf = s3c_camif_qbuf,
  867. .vidioc_dqbuf = s3c_camif_dqbuf,
  868. .vidioc_streamon = s3c_camif_streamon,
  869. .vidioc_streamoff = s3c_camif_streamoff,
  870. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  871. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  872. .vidioc_log_status = v4l2_ctrl_log_status,
  873. };
  874. /*
  875. * Video node controls
  876. */
  877. static int s3c_camif_video_s_ctrl(struct v4l2_ctrl *ctrl)
  878. {
  879. struct camif_vp *vp = ctrl->priv;
  880. struct camif_dev *camif = vp->camif;
  881. unsigned long flags;
  882. pr_debug("[vp%d] ctrl: %s, value: %d\n", vp->id,
  883. ctrl->name, ctrl->val);
  884. spin_lock_irqsave(&camif->slock, flags);
  885. switch (ctrl->id) {
  886. case V4L2_CID_HFLIP:
  887. vp->hflip = ctrl->val;
  888. break;
  889. case V4L2_CID_VFLIP:
  890. vp->vflip = ctrl->val;
  891. break;
  892. }
  893. vp->state |= ST_VP_CONFIG;
  894. spin_unlock_irqrestore(&camif->slock, flags);
  895. return 0;
  896. }
  897. /* Codec and preview video node control ops */
  898. static const struct v4l2_ctrl_ops s3c_camif_video_ctrl_ops = {
  899. .s_ctrl = s3c_camif_video_s_ctrl,
  900. };
  901. int s3c_camif_register_video_node(struct camif_dev *camif, int idx)
  902. {
  903. struct camif_vp *vp = &camif->vp[idx];
  904. struct vb2_queue *q = &vp->vb_queue;
  905. struct video_device *vfd = &vp->vdev;
  906. struct v4l2_ctrl *ctrl;
  907. int ret;
  908. memset(vfd, 0, sizeof(*vfd));
  909. snprintf(vfd->name, sizeof(vfd->name), "camif-%s",
  910. vp->id == 0 ? "codec" : "preview");
  911. vfd->fops = &s3c_camif_fops;
  912. vfd->ioctl_ops = &s3c_camif_ioctl_ops;
  913. vfd->v4l2_dev = &camif->v4l2_dev;
  914. vfd->minor = -1;
  915. vfd->release = video_device_release_empty;
  916. vfd->lock = &camif->lock;
  917. vp->reqbufs_count = 0;
  918. INIT_LIST_HEAD(&vp->pending_buf_q);
  919. INIT_LIST_HEAD(&vp->active_buf_q);
  920. memset(q, 0, sizeof(*q));
  921. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  922. q->io_modes = VB2_MMAP | VB2_USERPTR;
  923. q->ops = &s3c_camif_qops;
  924. q->mem_ops = &vb2_dma_contig_memops;
  925. q->buf_struct_size = sizeof(struct camif_buffer);
  926. q->drv_priv = vp;
  927. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  928. q->lock = &vp->camif->lock;
  929. ret = vb2_queue_init(q);
  930. if (ret)
  931. goto err_vd_rel;
  932. vp->pad.flags = MEDIA_PAD_FL_SINK;
  933. ret = media_entity_init(&vfd->entity, 1, &vp->pad, 0);
  934. if (ret)
  935. goto err_vd_rel;
  936. video_set_drvdata(vfd, vp);
  937. v4l2_ctrl_handler_init(&vp->ctrl_handler, 1);
  938. ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
  939. V4L2_CID_HFLIP, 0, 1, 1, 0);
  940. if (ctrl)
  941. ctrl->priv = vp;
  942. ctrl = v4l2_ctrl_new_std(&vp->ctrl_handler, &s3c_camif_video_ctrl_ops,
  943. V4L2_CID_VFLIP, 0, 1, 1, 0);
  944. if (ctrl)
  945. ctrl->priv = vp;
  946. ret = vp->ctrl_handler.error;
  947. if (ret < 0)
  948. goto err_me_cleanup;
  949. vfd->ctrl_handler = &vp->ctrl_handler;
  950. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  951. if (ret)
  952. goto err_ctrlh_free;
  953. v4l2_info(&camif->v4l2_dev, "registered %s as /dev/%s\n",
  954. vfd->name, video_device_node_name(vfd));
  955. return 0;
  956. err_ctrlh_free:
  957. v4l2_ctrl_handler_free(&vp->ctrl_handler);
  958. err_me_cleanup:
  959. media_entity_cleanup(&vfd->entity);
  960. err_vd_rel:
  961. video_device_release(vfd);
  962. return ret;
  963. }
  964. void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx)
  965. {
  966. struct video_device *vfd = &camif->vp[idx].vdev;
  967. if (video_is_registered(vfd)) {
  968. video_unregister_device(vfd);
  969. media_entity_cleanup(&vfd->entity);
  970. v4l2_ctrl_handler_free(vfd->ctrl_handler);
  971. }
  972. }
  973. /* Media bus pixel formats supported at the camif input */
  974. static const u32 camif_mbus_formats[] = {
  975. MEDIA_BUS_FMT_YUYV8_2X8,
  976. MEDIA_BUS_FMT_YVYU8_2X8,
  977. MEDIA_BUS_FMT_UYVY8_2X8,
  978. MEDIA_BUS_FMT_VYUY8_2X8,
  979. };
  980. /*
  981. * Camera input interface subdev operations
  982. */
  983. static int s3c_camif_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  984. struct v4l2_subdev_pad_config *cfg,
  985. struct v4l2_subdev_mbus_code_enum *code)
  986. {
  987. if (code->index >= ARRAY_SIZE(camif_mbus_formats))
  988. return -EINVAL;
  989. code->code = camif_mbus_formats[code->index];
  990. return 0;
  991. }
  992. static int s3c_camif_subdev_get_fmt(struct v4l2_subdev *sd,
  993. struct v4l2_subdev_pad_config *cfg,
  994. struct v4l2_subdev_format *fmt)
  995. {
  996. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  997. struct v4l2_mbus_framefmt *mf = &fmt->format;
  998. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  999. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1000. fmt->format = *mf;
  1001. return 0;
  1002. }
  1003. mutex_lock(&camif->lock);
  1004. switch (fmt->pad) {
  1005. case CAMIF_SD_PAD_SINK:
  1006. /* full camera input pixel size */
  1007. *mf = camif->mbus_fmt;
  1008. break;
  1009. case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
  1010. /* crop rectangle at camera interface input */
  1011. mf->width = camif->camif_crop.width;
  1012. mf->height = camif->camif_crop.height;
  1013. mf->code = camif->mbus_fmt.code;
  1014. break;
  1015. }
  1016. mutex_unlock(&camif->lock);
  1017. mf->field = V4L2_FIELD_NONE;
  1018. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1019. return 0;
  1020. }
  1021. static void __camif_subdev_try_format(struct camif_dev *camif,
  1022. struct v4l2_mbus_framefmt *mf, int pad)
  1023. {
  1024. const struct s3c_camif_variant *variant = camif->variant;
  1025. const struct vp_pix_limits *pix_lim;
  1026. int i = ARRAY_SIZE(camif_mbus_formats);
  1027. /* FIXME: constraints against codec or preview path ? */
  1028. pix_lim = &variant->vp_pix_limits[VP_CODEC];
  1029. while (i-- >= 0)
  1030. if (camif_mbus_formats[i] == mf->code)
  1031. break;
  1032. mf->code = camif_mbus_formats[i];
  1033. if (pad == CAMIF_SD_PAD_SINK) {
  1034. v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
  1035. ffs(pix_lim->out_width_align) - 1,
  1036. &mf->height, 8, CAMIF_MAX_PIX_HEIGHT, 0,
  1037. 0);
  1038. } else {
  1039. struct v4l2_rect *crop = &camif->camif_crop;
  1040. v4l_bound_align_image(&mf->width, 8, crop->width,
  1041. ffs(pix_lim->out_width_align) - 1,
  1042. &mf->height, 8, crop->height,
  1043. 0, 0);
  1044. }
  1045. v4l2_dbg(1, debug, &camif->subdev, "%ux%u\n", mf->width, mf->height);
  1046. }
  1047. static int s3c_camif_subdev_set_fmt(struct v4l2_subdev *sd,
  1048. struct v4l2_subdev_pad_config *cfg,
  1049. struct v4l2_subdev_format *fmt)
  1050. {
  1051. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1052. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1053. struct v4l2_rect *crop = &camif->camif_crop;
  1054. int i;
  1055. v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %ux%u\n",
  1056. fmt->pad, mf->code, mf->width, mf->height);
  1057. mf->field = V4L2_FIELD_NONE;
  1058. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1059. mutex_lock(&camif->lock);
  1060. /*
  1061. * No pixel format change at the camera input is allowed
  1062. * while streaming.
  1063. */
  1064. if (vb2_is_busy(&camif->vp[VP_CODEC].vb_queue) ||
  1065. vb2_is_busy(&camif->vp[VP_PREVIEW].vb_queue)) {
  1066. mutex_unlock(&camif->lock);
  1067. return -EBUSY;
  1068. }
  1069. __camif_subdev_try_format(camif, mf, fmt->pad);
  1070. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1071. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1072. *mf = fmt->format;
  1073. mutex_unlock(&camif->lock);
  1074. return 0;
  1075. }
  1076. switch (fmt->pad) {
  1077. case CAMIF_SD_PAD_SINK:
  1078. camif->mbus_fmt = *mf;
  1079. /* Reset sink crop rectangle. */
  1080. crop->width = mf->width;
  1081. crop->height = mf->height;
  1082. crop->left = 0;
  1083. crop->top = 0;
  1084. /*
  1085. * Reset source format (the camif's crop rectangle)
  1086. * and the video output resolution.
  1087. */
  1088. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1089. struct camif_frame *frame = &camif->vp[i].out_frame;
  1090. frame->rect = *crop;
  1091. frame->f_width = mf->width;
  1092. frame->f_height = mf->height;
  1093. }
  1094. break;
  1095. case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
  1096. /* Pixel format can be only changed on the sink pad. */
  1097. mf->code = camif->mbus_fmt.code;
  1098. mf->width = crop->width;
  1099. mf->height = crop->height;
  1100. break;
  1101. }
  1102. mutex_unlock(&camif->lock);
  1103. return 0;
  1104. }
  1105. static int s3c_camif_subdev_get_selection(struct v4l2_subdev *sd,
  1106. struct v4l2_subdev_pad_config *cfg,
  1107. struct v4l2_subdev_selection *sel)
  1108. {
  1109. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1110. struct v4l2_rect *crop = &camif->camif_crop;
  1111. struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
  1112. if ((sel->target != V4L2_SEL_TGT_CROP &&
  1113. sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
  1114. sel->pad != CAMIF_SD_PAD_SINK)
  1115. return -EINVAL;
  1116. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1117. sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
  1118. return 0;
  1119. }
  1120. mutex_lock(&camif->lock);
  1121. if (sel->target == V4L2_SEL_TGT_CROP) {
  1122. sel->r = *crop;
  1123. } else { /* crop bounds */
  1124. sel->r.width = mf->width;
  1125. sel->r.height = mf->height;
  1126. sel->r.left = 0;
  1127. sel->r.top = 0;
  1128. }
  1129. mutex_unlock(&camif->lock);
  1130. v4l2_dbg(1, debug, sd, "%s: crop: (%d,%d) %dx%d, size: %ux%u\n",
  1131. __func__, crop->left, crop->top, crop->width,
  1132. crop->height, mf->width, mf->height);
  1133. return 0;
  1134. }
  1135. static void __camif_try_crop(struct camif_dev *camif, struct v4l2_rect *r)
  1136. {
  1137. struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
  1138. const struct camif_pix_limits *pix_lim = &camif->variant->pix_limits;
  1139. unsigned int left = 2 * r->left;
  1140. unsigned int top = 2 * r->top;
  1141. /*
  1142. * Following constraints must be met:
  1143. * - r->width + 2 * r->left = mf->width;
  1144. * - r->height + 2 * r->top = mf->height;
  1145. * - crop rectangle size and position must be aligned
  1146. * to 8 or 2 pixels, depending on SoC version.
  1147. */
  1148. v4l_bound_align_image(&r->width, 0, mf->width,
  1149. ffs(pix_lim->win_hor_offset_align) - 1,
  1150. &r->height, 0, mf->height, 1, 0);
  1151. v4l_bound_align_image(&left, 0, mf->width - r->width,
  1152. ffs(pix_lim->win_hor_offset_align),
  1153. &top, 0, mf->height - r->height, 2, 0);
  1154. r->left = left / 2;
  1155. r->top = top / 2;
  1156. r->width = mf->width - left;
  1157. r->height = mf->height - top;
  1158. /*
  1159. * Make sure we either downscale or upscale both the pixel
  1160. * width and height. Just return current crop rectangle if
  1161. * this scaler constraint is not met.
  1162. */
  1163. if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV &&
  1164. camif_is_streaming(camif)) {
  1165. unsigned int i;
  1166. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1167. struct v4l2_rect *or = &camif->vp[i].out_frame.rect;
  1168. if ((or->width > r->width) == (or->height > r->height))
  1169. continue;
  1170. *r = camif->camif_crop;
  1171. pr_debug("Width/height scaling direction limitation\n");
  1172. break;
  1173. }
  1174. }
  1175. v4l2_dbg(1, debug, &camif->v4l2_dev, "crop: (%d,%d)/%dx%d, fmt: %ux%u\n",
  1176. r->left, r->top, r->width, r->height, mf->width, mf->height);
  1177. }
  1178. static int s3c_camif_subdev_set_selection(struct v4l2_subdev *sd,
  1179. struct v4l2_subdev_pad_config *cfg,
  1180. struct v4l2_subdev_selection *sel)
  1181. {
  1182. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1183. struct v4l2_rect *crop = &camif->camif_crop;
  1184. struct camif_scaler scaler;
  1185. if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != CAMIF_SD_PAD_SINK)
  1186. return -EINVAL;
  1187. mutex_lock(&camif->lock);
  1188. __camif_try_crop(camif, &sel->r);
  1189. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1190. *v4l2_subdev_get_try_crop(sd, cfg, sel->pad) = sel->r;
  1191. } else {
  1192. unsigned long flags;
  1193. unsigned int i;
  1194. spin_lock_irqsave(&camif->slock, flags);
  1195. *crop = sel->r;
  1196. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1197. struct camif_vp *vp = &camif->vp[i];
  1198. scaler = vp->scaler;
  1199. if (s3c_camif_get_scaler_config(vp, &scaler))
  1200. continue;
  1201. vp->scaler = scaler;
  1202. vp->state |= ST_VP_CONFIG;
  1203. }
  1204. spin_unlock_irqrestore(&camif->slock, flags);
  1205. }
  1206. mutex_unlock(&camif->lock);
  1207. v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %u, f_h: %u\n",
  1208. __func__, crop->left, crop->top, crop->width, crop->height,
  1209. camif->mbus_fmt.width, camif->mbus_fmt.height);
  1210. return 0;
  1211. }
  1212. static const struct v4l2_subdev_pad_ops s3c_camif_subdev_pad_ops = {
  1213. .enum_mbus_code = s3c_camif_subdev_enum_mbus_code,
  1214. .get_selection = s3c_camif_subdev_get_selection,
  1215. .set_selection = s3c_camif_subdev_set_selection,
  1216. .get_fmt = s3c_camif_subdev_get_fmt,
  1217. .set_fmt = s3c_camif_subdev_set_fmt,
  1218. };
  1219. static struct v4l2_subdev_ops s3c_camif_subdev_ops = {
  1220. .pad = &s3c_camif_subdev_pad_ops,
  1221. };
  1222. static int s3c_camif_subdev_s_ctrl(struct v4l2_ctrl *ctrl)
  1223. {
  1224. struct camif_dev *camif = container_of(ctrl->handler, struct camif_dev,
  1225. ctrl_handler);
  1226. unsigned long flags;
  1227. spin_lock_irqsave(&camif->slock, flags);
  1228. switch (ctrl->id) {
  1229. case V4L2_CID_COLORFX:
  1230. camif->colorfx = camif->ctrl_colorfx->val;
  1231. /* Set Cb, Cr */
  1232. switch (ctrl->val) {
  1233. case V4L2_COLORFX_SEPIA:
  1234. camif->colorfx_cb = 115;
  1235. camif->colorfx_cr = 145;
  1236. break;
  1237. case V4L2_COLORFX_SET_CBCR:
  1238. camif->colorfx_cb = camif->ctrl_colorfx_cbcr->val >> 8;
  1239. camif->colorfx_cr = camif->ctrl_colorfx_cbcr->val & 0xff;
  1240. break;
  1241. default:
  1242. /* for V4L2_COLORFX_BW and others */
  1243. camif->colorfx_cb = 128;
  1244. camif->colorfx_cr = 128;
  1245. }
  1246. break;
  1247. case V4L2_CID_TEST_PATTERN:
  1248. camif->test_pattern = camif->ctrl_test_pattern->val;
  1249. break;
  1250. default:
  1251. WARN_ON(1);
  1252. }
  1253. camif->vp[VP_CODEC].state |= ST_VP_CONFIG;
  1254. camif->vp[VP_PREVIEW].state |= ST_VP_CONFIG;
  1255. spin_unlock_irqrestore(&camif->slock, flags);
  1256. return 0;
  1257. }
  1258. static const struct v4l2_ctrl_ops s3c_camif_subdev_ctrl_ops = {
  1259. .s_ctrl = s3c_camif_subdev_s_ctrl,
  1260. };
  1261. static const char * const s3c_camif_test_pattern_menu[] = {
  1262. "Disabled",
  1263. "Color bars",
  1264. "Horizontal increment",
  1265. "Vertical increment",
  1266. };
  1267. int s3c_camif_create_subdev(struct camif_dev *camif)
  1268. {
  1269. struct v4l2_ctrl_handler *handler = &camif->ctrl_handler;
  1270. struct v4l2_subdev *sd = &camif->subdev;
  1271. int ret;
  1272. v4l2_subdev_init(sd, &s3c_camif_subdev_ops);
  1273. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1274. strlcpy(sd->name, "S3C-CAMIF", sizeof(sd->name));
  1275. camif->pads[CAMIF_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1276. camif->pads[CAMIF_SD_PAD_SOURCE_C].flags = MEDIA_PAD_FL_SOURCE;
  1277. camif->pads[CAMIF_SD_PAD_SOURCE_P].flags = MEDIA_PAD_FL_SOURCE;
  1278. ret = media_entity_init(&sd->entity, CAMIF_SD_PADS_NUM,
  1279. camif->pads, 0);
  1280. if (ret)
  1281. return ret;
  1282. v4l2_ctrl_handler_init(handler, 3);
  1283. camif->ctrl_test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  1284. &s3c_camif_subdev_ctrl_ops, V4L2_CID_TEST_PATTERN,
  1285. ARRAY_SIZE(s3c_camif_test_pattern_menu) - 1, 0, 0,
  1286. s3c_camif_test_pattern_menu);
  1287. if (camif->variant->has_img_effect) {
  1288. camif->ctrl_colorfx = v4l2_ctrl_new_std_menu(handler,
  1289. &s3c_camif_subdev_ctrl_ops,
  1290. V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR,
  1291. ~0x981f, V4L2_COLORFX_NONE);
  1292. camif->ctrl_colorfx_cbcr = v4l2_ctrl_new_std(handler,
  1293. &s3c_camif_subdev_ctrl_ops,
  1294. V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
  1295. }
  1296. if (handler->error) {
  1297. v4l2_ctrl_handler_free(handler);
  1298. media_entity_cleanup(&sd->entity);
  1299. return handler->error;
  1300. }
  1301. if (camif->variant->has_img_effect)
  1302. v4l2_ctrl_auto_cluster(2, &camif->ctrl_colorfx,
  1303. V4L2_COLORFX_SET_CBCR, false);
  1304. sd->ctrl_handler = handler;
  1305. v4l2_set_subdevdata(sd, camif);
  1306. return 0;
  1307. }
  1308. void s3c_camif_unregister_subdev(struct camif_dev *camif)
  1309. {
  1310. struct v4l2_subdev *sd = &camif->subdev;
  1311. /* Return if not registered */
  1312. if (v4l2_get_subdevdata(sd) == NULL)
  1313. return;
  1314. v4l2_device_unregister_subdev(sd);
  1315. media_entity_cleanup(&sd->entity);
  1316. v4l2_ctrl_handler_free(&camif->ctrl_handler);
  1317. v4l2_set_subdevdata(sd, NULL);
  1318. }
  1319. int s3c_camif_set_defaults(struct camif_dev *camif)
  1320. {
  1321. unsigned int ip_rev = camif->variant->ip_revision;
  1322. int i;
  1323. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1324. struct camif_vp *vp = &camif->vp[i];
  1325. struct camif_frame *f = &vp->out_frame;
  1326. vp->camif = camif;
  1327. vp->id = i;
  1328. vp->offset = camif->variant->vp_offset;
  1329. if (ip_rev == S3C244X_CAMIF_IP_REV)
  1330. vp->fmt_flags = i ? FMT_FL_S3C24XX_PREVIEW :
  1331. FMT_FL_S3C24XX_CODEC;
  1332. else
  1333. vp->fmt_flags = FMT_FL_S3C64XX;
  1334. vp->out_fmt = s3c_camif_find_format(vp, NULL, 0);
  1335. BUG_ON(vp->out_fmt == NULL);
  1336. memset(f, 0, sizeof(*f));
  1337. f->f_width = CAMIF_DEF_WIDTH;
  1338. f->f_height = CAMIF_DEF_HEIGHT;
  1339. f->rect.width = CAMIF_DEF_WIDTH;
  1340. f->rect.height = CAMIF_DEF_HEIGHT;
  1341. /* Scaler is always enabled */
  1342. vp->scaler.enable = 1;
  1343. vp->payload = (f->f_width * f->f_height *
  1344. vp->out_fmt->depth) / 8;
  1345. }
  1346. memset(&camif->mbus_fmt, 0, sizeof(camif->mbus_fmt));
  1347. camif->mbus_fmt.width = CAMIF_DEF_WIDTH;
  1348. camif->mbus_fmt.height = CAMIF_DEF_HEIGHT;
  1349. camif->mbus_fmt.code = camif_mbus_formats[0];
  1350. memset(&camif->camif_crop, 0, sizeof(camif->camif_crop));
  1351. camif->camif_crop.width = CAMIF_DEF_WIDTH;
  1352. camif->camif_crop.height = CAMIF_DEF_HEIGHT;
  1353. return 0;
  1354. }