camif-capture.c 43 KB

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