camif-capture.c 42 KB

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