camif-capture.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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)
  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_cb, camif->colorfx_cr);
  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 unsigned int 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. int 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_entity_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_entity_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_entity_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. int i = ARRAY_SIZE(camif_mbus_formats);
  1014. /* FIXME: constraints against codec or preview path ? */
  1015. pix_lim = &variant->vp_pix_limits[VP_CODEC];
  1016. while (i-- >= 0)
  1017. if (camif_mbus_formats[i] == mf->code)
  1018. break;
  1019. mf->code = camif_mbus_formats[i];
  1020. if (pad == CAMIF_SD_PAD_SINK) {
  1021. v4l_bound_align_image(&mf->width, 8, CAMIF_MAX_PIX_WIDTH,
  1022. ffs(pix_lim->out_width_align) - 1,
  1023. &mf->height, 8, CAMIF_MAX_PIX_HEIGHT, 0,
  1024. 0);
  1025. } else {
  1026. struct v4l2_rect *crop = &camif->camif_crop;
  1027. v4l_bound_align_image(&mf->width, 8, crop->width,
  1028. ffs(pix_lim->out_width_align) - 1,
  1029. &mf->height, 8, crop->height,
  1030. 0, 0);
  1031. }
  1032. v4l2_dbg(1, debug, &camif->subdev, "%ux%u\n", mf->width, mf->height);
  1033. }
  1034. static int s3c_camif_subdev_set_fmt(struct v4l2_subdev *sd,
  1035. struct v4l2_subdev_pad_config *cfg,
  1036. struct v4l2_subdev_format *fmt)
  1037. {
  1038. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1039. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1040. struct v4l2_rect *crop = &camif->camif_crop;
  1041. int i;
  1042. v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %ux%u\n",
  1043. fmt->pad, mf->code, mf->width, mf->height);
  1044. mf->field = V4L2_FIELD_NONE;
  1045. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1046. mutex_lock(&camif->lock);
  1047. /*
  1048. * No pixel format change at the camera input is allowed
  1049. * while streaming.
  1050. */
  1051. if (vb2_is_busy(&camif->vp[VP_CODEC].vb_queue) ||
  1052. vb2_is_busy(&camif->vp[VP_PREVIEW].vb_queue)) {
  1053. mutex_unlock(&camif->lock);
  1054. return -EBUSY;
  1055. }
  1056. __camif_subdev_try_format(camif, mf, fmt->pad);
  1057. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1058. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1059. *mf = fmt->format;
  1060. mutex_unlock(&camif->lock);
  1061. return 0;
  1062. }
  1063. switch (fmt->pad) {
  1064. case CAMIF_SD_PAD_SINK:
  1065. camif->mbus_fmt = *mf;
  1066. /* Reset sink crop rectangle. */
  1067. crop->width = mf->width;
  1068. crop->height = mf->height;
  1069. crop->left = 0;
  1070. crop->top = 0;
  1071. /*
  1072. * Reset source format (the camif's crop rectangle)
  1073. * and the video output resolution.
  1074. */
  1075. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1076. struct camif_frame *frame = &camif->vp[i].out_frame;
  1077. frame->rect = *crop;
  1078. frame->f_width = mf->width;
  1079. frame->f_height = mf->height;
  1080. }
  1081. break;
  1082. case CAMIF_SD_PAD_SOURCE_C...CAMIF_SD_PAD_SOURCE_P:
  1083. /* Pixel format can be only changed on the sink pad. */
  1084. mf->code = camif->mbus_fmt.code;
  1085. mf->width = crop->width;
  1086. mf->height = crop->height;
  1087. break;
  1088. }
  1089. mutex_unlock(&camif->lock);
  1090. return 0;
  1091. }
  1092. static int s3c_camif_subdev_get_selection(struct v4l2_subdev *sd,
  1093. struct v4l2_subdev_pad_config *cfg,
  1094. struct v4l2_subdev_selection *sel)
  1095. {
  1096. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1097. struct v4l2_rect *crop = &camif->camif_crop;
  1098. struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
  1099. if ((sel->target != V4L2_SEL_TGT_CROP &&
  1100. sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
  1101. sel->pad != CAMIF_SD_PAD_SINK)
  1102. return -EINVAL;
  1103. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1104. sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
  1105. return 0;
  1106. }
  1107. mutex_lock(&camif->lock);
  1108. if (sel->target == V4L2_SEL_TGT_CROP) {
  1109. sel->r = *crop;
  1110. } else { /* crop bounds */
  1111. sel->r.width = mf->width;
  1112. sel->r.height = mf->height;
  1113. sel->r.left = 0;
  1114. sel->r.top = 0;
  1115. }
  1116. mutex_unlock(&camif->lock);
  1117. v4l2_dbg(1, debug, sd, "%s: crop: (%d,%d) %dx%d, size: %ux%u\n",
  1118. __func__, crop->left, crop->top, crop->width,
  1119. crop->height, mf->width, mf->height);
  1120. return 0;
  1121. }
  1122. static void __camif_try_crop(struct camif_dev *camif, struct v4l2_rect *r)
  1123. {
  1124. struct v4l2_mbus_framefmt *mf = &camif->mbus_fmt;
  1125. const struct camif_pix_limits *pix_lim = &camif->variant->pix_limits;
  1126. unsigned int left = 2 * r->left;
  1127. unsigned int top = 2 * r->top;
  1128. /*
  1129. * Following constraints must be met:
  1130. * - r->width + 2 * r->left = mf->width;
  1131. * - r->height + 2 * r->top = mf->height;
  1132. * - crop rectangle size and position must be aligned
  1133. * to 8 or 2 pixels, depending on SoC version.
  1134. */
  1135. v4l_bound_align_image(&r->width, 0, mf->width,
  1136. ffs(pix_lim->win_hor_offset_align) - 1,
  1137. &r->height, 0, mf->height, 1, 0);
  1138. v4l_bound_align_image(&left, 0, mf->width - r->width,
  1139. ffs(pix_lim->win_hor_offset_align),
  1140. &top, 0, mf->height - r->height, 2, 0);
  1141. r->left = left / 2;
  1142. r->top = top / 2;
  1143. r->width = mf->width - left;
  1144. r->height = mf->height - top;
  1145. /*
  1146. * Make sure we either downscale or upscale both the pixel
  1147. * width and height. Just return current crop rectangle if
  1148. * this scaler constraint is not met.
  1149. */
  1150. if (camif->variant->ip_revision == S3C244X_CAMIF_IP_REV &&
  1151. camif_is_streaming(camif)) {
  1152. unsigned int i;
  1153. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1154. struct v4l2_rect *or = &camif->vp[i].out_frame.rect;
  1155. if ((or->width > r->width) == (or->height > r->height))
  1156. continue;
  1157. *r = camif->camif_crop;
  1158. pr_debug("Width/height scaling direction limitation\n");
  1159. break;
  1160. }
  1161. }
  1162. v4l2_dbg(1, debug, &camif->v4l2_dev, "crop: (%d,%d)/%dx%d, fmt: %ux%u\n",
  1163. r->left, r->top, r->width, r->height, mf->width, mf->height);
  1164. }
  1165. static int s3c_camif_subdev_set_selection(struct v4l2_subdev *sd,
  1166. struct v4l2_subdev_pad_config *cfg,
  1167. struct v4l2_subdev_selection *sel)
  1168. {
  1169. struct camif_dev *camif = v4l2_get_subdevdata(sd);
  1170. struct v4l2_rect *crop = &camif->camif_crop;
  1171. struct camif_scaler scaler;
  1172. if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != CAMIF_SD_PAD_SINK)
  1173. return -EINVAL;
  1174. mutex_lock(&camif->lock);
  1175. __camif_try_crop(camif, &sel->r);
  1176. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1177. *v4l2_subdev_get_try_crop(sd, cfg, sel->pad) = sel->r;
  1178. } else {
  1179. unsigned long flags;
  1180. unsigned int i;
  1181. spin_lock_irqsave(&camif->slock, flags);
  1182. *crop = sel->r;
  1183. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1184. struct camif_vp *vp = &camif->vp[i];
  1185. scaler = vp->scaler;
  1186. if (s3c_camif_get_scaler_config(vp, &scaler))
  1187. continue;
  1188. vp->scaler = scaler;
  1189. vp->state |= ST_VP_CONFIG;
  1190. }
  1191. spin_unlock_irqrestore(&camif->slock, flags);
  1192. }
  1193. mutex_unlock(&camif->lock);
  1194. v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %u, f_h: %u\n",
  1195. __func__, crop->left, crop->top, crop->width, crop->height,
  1196. camif->mbus_fmt.width, camif->mbus_fmt.height);
  1197. return 0;
  1198. }
  1199. static const struct v4l2_subdev_pad_ops s3c_camif_subdev_pad_ops = {
  1200. .enum_mbus_code = s3c_camif_subdev_enum_mbus_code,
  1201. .get_selection = s3c_camif_subdev_get_selection,
  1202. .set_selection = s3c_camif_subdev_set_selection,
  1203. .get_fmt = s3c_camif_subdev_get_fmt,
  1204. .set_fmt = s3c_camif_subdev_set_fmt,
  1205. };
  1206. static struct v4l2_subdev_ops s3c_camif_subdev_ops = {
  1207. .pad = &s3c_camif_subdev_pad_ops,
  1208. };
  1209. static int s3c_camif_subdev_s_ctrl(struct v4l2_ctrl *ctrl)
  1210. {
  1211. struct camif_dev *camif = container_of(ctrl->handler, struct camif_dev,
  1212. ctrl_handler);
  1213. unsigned long flags;
  1214. spin_lock_irqsave(&camif->slock, flags);
  1215. switch (ctrl->id) {
  1216. case V4L2_CID_COLORFX:
  1217. camif->colorfx = camif->ctrl_colorfx->val;
  1218. /* Set Cb, Cr */
  1219. switch (ctrl->val) {
  1220. case V4L2_COLORFX_SEPIA:
  1221. camif->colorfx_cb = 115;
  1222. camif->colorfx_cr = 145;
  1223. break;
  1224. case V4L2_COLORFX_SET_CBCR:
  1225. camif->colorfx_cb = camif->ctrl_colorfx_cbcr->val >> 8;
  1226. camif->colorfx_cr = camif->ctrl_colorfx_cbcr->val & 0xff;
  1227. break;
  1228. default:
  1229. /* for V4L2_COLORFX_BW and others */
  1230. camif->colorfx_cb = 128;
  1231. camif->colorfx_cr = 128;
  1232. }
  1233. break;
  1234. case V4L2_CID_TEST_PATTERN:
  1235. camif->test_pattern = camif->ctrl_test_pattern->val;
  1236. break;
  1237. default:
  1238. WARN_ON(1);
  1239. }
  1240. camif->vp[VP_CODEC].state |= ST_VP_CONFIG;
  1241. camif->vp[VP_PREVIEW].state |= ST_VP_CONFIG;
  1242. spin_unlock_irqrestore(&camif->slock, flags);
  1243. return 0;
  1244. }
  1245. static const struct v4l2_ctrl_ops s3c_camif_subdev_ctrl_ops = {
  1246. .s_ctrl = s3c_camif_subdev_s_ctrl,
  1247. };
  1248. static const char * const s3c_camif_test_pattern_menu[] = {
  1249. "Disabled",
  1250. "Color bars",
  1251. "Horizontal increment",
  1252. "Vertical increment",
  1253. };
  1254. int s3c_camif_create_subdev(struct camif_dev *camif)
  1255. {
  1256. struct v4l2_ctrl_handler *handler = &camif->ctrl_handler;
  1257. struct v4l2_subdev *sd = &camif->subdev;
  1258. int ret;
  1259. v4l2_subdev_init(sd, &s3c_camif_subdev_ops);
  1260. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1261. strlcpy(sd->name, "S3C-CAMIF", sizeof(sd->name));
  1262. camif->pads[CAMIF_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1263. camif->pads[CAMIF_SD_PAD_SOURCE_C].flags = MEDIA_PAD_FL_SOURCE;
  1264. camif->pads[CAMIF_SD_PAD_SOURCE_P].flags = MEDIA_PAD_FL_SOURCE;
  1265. ret = media_entity_pads_init(&sd->entity, CAMIF_SD_PADS_NUM,
  1266. camif->pads);
  1267. if (ret)
  1268. return ret;
  1269. v4l2_ctrl_handler_init(handler, 3);
  1270. camif->ctrl_test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  1271. &s3c_camif_subdev_ctrl_ops, V4L2_CID_TEST_PATTERN,
  1272. ARRAY_SIZE(s3c_camif_test_pattern_menu) - 1, 0, 0,
  1273. s3c_camif_test_pattern_menu);
  1274. if (camif->variant->has_img_effect) {
  1275. camif->ctrl_colorfx = v4l2_ctrl_new_std_menu(handler,
  1276. &s3c_camif_subdev_ctrl_ops,
  1277. V4L2_CID_COLORFX, V4L2_COLORFX_SET_CBCR,
  1278. ~0x981f, V4L2_COLORFX_NONE);
  1279. camif->ctrl_colorfx_cbcr = v4l2_ctrl_new_std(handler,
  1280. &s3c_camif_subdev_ctrl_ops,
  1281. V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
  1282. }
  1283. if (handler->error) {
  1284. v4l2_ctrl_handler_free(handler);
  1285. media_entity_cleanup(&sd->entity);
  1286. return handler->error;
  1287. }
  1288. if (camif->variant->has_img_effect)
  1289. v4l2_ctrl_auto_cluster(2, &camif->ctrl_colorfx,
  1290. V4L2_COLORFX_SET_CBCR, false);
  1291. sd->ctrl_handler = handler;
  1292. v4l2_set_subdevdata(sd, camif);
  1293. return 0;
  1294. }
  1295. void s3c_camif_unregister_subdev(struct camif_dev *camif)
  1296. {
  1297. struct v4l2_subdev *sd = &camif->subdev;
  1298. /* Return if not registered */
  1299. if (v4l2_get_subdevdata(sd) == NULL)
  1300. return;
  1301. v4l2_device_unregister_subdev(sd);
  1302. media_entity_cleanup(&sd->entity);
  1303. v4l2_ctrl_handler_free(&camif->ctrl_handler);
  1304. v4l2_set_subdevdata(sd, NULL);
  1305. }
  1306. int s3c_camif_set_defaults(struct camif_dev *camif)
  1307. {
  1308. unsigned int ip_rev = camif->variant->ip_revision;
  1309. int i;
  1310. for (i = 0; i < CAMIF_VP_NUM; i++) {
  1311. struct camif_vp *vp = &camif->vp[i];
  1312. struct camif_frame *f = &vp->out_frame;
  1313. vp->camif = camif;
  1314. vp->id = i;
  1315. vp->offset = camif->variant->vp_offset;
  1316. if (ip_rev == S3C244X_CAMIF_IP_REV)
  1317. vp->fmt_flags = i ? FMT_FL_S3C24XX_PREVIEW :
  1318. FMT_FL_S3C24XX_CODEC;
  1319. else
  1320. vp->fmt_flags = FMT_FL_S3C64XX;
  1321. vp->out_fmt = s3c_camif_find_format(vp, NULL, 0);
  1322. BUG_ON(vp->out_fmt == NULL);
  1323. memset(f, 0, sizeof(*f));
  1324. f->f_width = CAMIF_DEF_WIDTH;
  1325. f->f_height = CAMIF_DEF_HEIGHT;
  1326. f->rect.width = CAMIF_DEF_WIDTH;
  1327. f->rect.height = CAMIF_DEF_HEIGHT;
  1328. /* Scaler is always enabled */
  1329. vp->scaler.enable = 1;
  1330. vp->payload = (f->f_width * f->f_height *
  1331. vp->out_fmt->depth) / 8;
  1332. }
  1333. memset(&camif->mbus_fmt, 0, sizeof(camif->mbus_fmt));
  1334. camif->mbus_fmt.width = CAMIF_DEF_WIDTH;
  1335. camif->mbus_fmt.height = CAMIF_DEF_HEIGHT;
  1336. camif->mbus_fmt.code = camif_mbus_formats[0];
  1337. memset(&camif->camif_crop, 0, sizeof(camif->camif_crop));
  1338. camif->camif_crop.width = CAMIF_DEF_WIDTH;
  1339. camif->camif_crop.height = CAMIF_DEF_HEIGHT;
  1340. return 0;
  1341. }