camif-capture.c 42 KB

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