camif-capture.c 43 KB

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