rcar_vin.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. /*
  2. * SoC-camera host driver for Renesas R-Car VIN unit
  3. *
  4. * Copyright (C) 2011-2013 Renesas Solutions Corp.
  5. * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
  6. *
  7. * Based on V4L2 Driver for SuperH Mobile CEU interface "sh_mobile_ceu_camera.c"
  8. *
  9. * Copyright (C) 2008 Magnus Damm
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_data/camera-rcar.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/slab.h>
  25. #include <linux/videodev2.h>
  26. #include <media/soc_camera.h>
  27. #include <media/soc_mediabus.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/v4l2-dev.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-mediabus.h>
  32. #include <media/v4l2-subdev.h>
  33. #include <media/videobuf2-dma-contig.h>
  34. #include "soc_scale_crop.h"
  35. #define DRV_NAME "rcar_vin"
  36. /* Register offsets for R-Car VIN */
  37. #define VNMC_REG 0x00 /* Video n Main Control Register */
  38. #define VNMS_REG 0x04 /* Video n Module Status Register */
  39. #define VNFC_REG 0x08 /* Video n Frame Capture Register */
  40. #define VNSLPRC_REG 0x0C /* Video n Start Line Pre-Clip Register */
  41. #define VNELPRC_REG 0x10 /* Video n End Line Pre-Clip Register */
  42. #define VNSPPRC_REG 0x14 /* Video n Start Pixel Pre-Clip Register */
  43. #define VNEPPRC_REG 0x18 /* Video n End Pixel Pre-Clip Register */
  44. #define VNSLPOC_REG 0x1C /* Video n Start Line Post-Clip Register */
  45. #define VNELPOC_REG 0x20 /* Video n End Line Post-Clip Register */
  46. #define VNSPPOC_REG 0x24 /* Video n Start Pixel Post-Clip Register */
  47. #define VNEPPOC_REG 0x28 /* Video n End Pixel Post-Clip Register */
  48. #define VNIS_REG 0x2C /* Video n Image Stride Register */
  49. #define VNMB_REG(m) (0x30 + ((m) << 2)) /* Video n Memory Base m Register */
  50. #define VNIE_REG 0x40 /* Video n Interrupt Enable Register */
  51. #define VNINTS_REG 0x44 /* Video n Interrupt Status Register */
  52. #define VNSI_REG 0x48 /* Video n Scanline Interrupt Register */
  53. #define VNMTC_REG 0x4C /* Video n Memory Transfer Control Register */
  54. #define VNYS_REG 0x50 /* Video n Y Scale Register */
  55. #define VNXS_REG 0x54 /* Video n X Scale Register */
  56. #define VNDMR_REG 0x58 /* Video n Data Mode Register */
  57. #define VNDMR2_REG 0x5C /* Video n Data Mode Register 2 */
  58. #define VNUVAOF_REG 0x60 /* Video n UV Address Offset Register */
  59. /* Register bit fields for R-Car VIN */
  60. /* Video n Main Control Register bits */
  61. #define VNMC_FOC (1 << 21)
  62. #define VNMC_YCAL (1 << 19)
  63. #define VNMC_INF_YUV8_BT656 (0 << 16)
  64. #define VNMC_INF_YUV8_BT601 (1 << 16)
  65. #define VNMC_INF_YUV16 (5 << 16)
  66. #define VNMC_VUP (1 << 10)
  67. #define VNMC_IM_ODD (0 << 3)
  68. #define VNMC_IM_ODD_EVEN (1 << 3)
  69. #define VNMC_IM_EVEN (2 << 3)
  70. #define VNMC_IM_FULL (3 << 3)
  71. #define VNMC_BPS (1 << 1)
  72. #define VNMC_ME (1 << 0)
  73. /* Video n Module Status Register bits */
  74. #define VNMS_FBS_MASK (3 << 3)
  75. #define VNMS_FBS_SHIFT 3
  76. #define VNMS_AV (1 << 1)
  77. #define VNMS_CA (1 << 0)
  78. /* Video n Frame Capture Register bits */
  79. #define VNFC_C_FRAME (1 << 1)
  80. #define VNFC_S_FRAME (1 << 0)
  81. /* Video n Interrupt Enable Register bits */
  82. #define VNIE_FIE (1 << 4)
  83. #define VNIE_EFE (1 << 1)
  84. /* Video n Data Mode Register bits */
  85. #define VNDMR_EXRGB (1 << 8)
  86. #define VNDMR_BPSM (1 << 4)
  87. #define VNDMR_DTMD_YCSEP (1 << 1)
  88. #define VNDMR_DTMD_ARGB1555 (1 << 0)
  89. /* Video n Data Mode Register 2 bits */
  90. #define VNDMR2_VPS (1 << 30)
  91. #define VNDMR2_HPS (1 << 29)
  92. #define VNDMR2_FTEV (1 << 17)
  93. #define VIN_MAX_WIDTH 2048
  94. #define VIN_MAX_HEIGHT 2048
  95. enum chip_id {
  96. RCAR_H2,
  97. RCAR_H1,
  98. RCAR_M1,
  99. RCAR_E1,
  100. };
  101. enum rcar_vin_state {
  102. STOPPED = 0,
  103. RUNNING,
  104. STOPPING,
  105. };
  106. struct rcar_vin_priv {
  107. void __iomem *base;
  108. spinlock_t lock;
  109. int sequence;
  110. /* State of the VIN module in capturing mode */
  111. enum rcar_vin_state state;
  112. struct rcar_vin_platform_data *pdata;
  113. struct soc_camera_host ici;
  114. struct list_head capture;
  115. #define MAX_BUFFER_NUM 3
  116. struct vb2_buffer *queue_buf[MAX_BUFFER_NUM];
  117. struct vb2_alloc_ctx *alloc_ctx;
  118. enum v4l2_field field;
  119. unsigned int vb_count;
  120. unsigned int nr_hw_slots;
  121. bool request_to_stop;
  122. struct completion capture_stop;
  123. enum chip_id chip;
  124. };
  125. #define is_continuous_transfer(priv) (priv->vb_count > MAX_BUFFER_NUM)
  126. struct rcar_vin_buffer {
  127. struct vb2_buffer vb;
  128. struct list_head list;
  129. };
  130. #define to_buf_list(vb2_buffer) (&container_of(vb2_buffer, \
  131. struct rcar_vin_buffer, \
  132. vb)->list)
  133. struct rcar_vin_cam {
  134. /* VIN offsets within the camera output, before the VIN scaler */
  135. unsigned int vin_left;
  136. unsigned int vin_top;
  137. /* Client output, as seen by the VIN */
  138. unsigned int width;
  139. unsigned int height;
  140. /*
  141. * User window from S_CROP / G_CROP, produced by client cropping and
  142. * scaling, VIN scaling and VIN cropping, mapped back onto the client
  143. * input window
  144. */
  145. struct v4l2_rect subrect;
  146. /* Camera cropping rectangle */
  147. struct v4l2_rect rect;
  148. const struct soc_mbus_pixelfmt *extra_fmt;
  149. };
  150. /*
  151. * .queue_setup() is called to check whether the driver can accept the requested
  152. * number of buffers and to fill in plane sizes for the current frame format if
  153. * required
  154. */
  155. static int rcar_vin_videobuf_setup(struct vb2_queue *vq,
  156. const struct v4l2_format *fmt,
  157. unsigned int *count,
  158. unsigned int *num_planes,
  159. unsigned int sizes[], void *alloc_ctxs[])
  160. {
  161. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  162. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  163. struct rcar_vin_priv *priv = ici->priv;
  164. if (fmt) {
  165. const struct soc_camera_format_xlate *xlate;
  166. unsigned int bytes_per_line;
  167. int ret;
  168. xlate = soc_camera_xlate_by_fourcc(icd,
  169. fmt->fmt.pix.pixelformat);
  170. if (!xlate)
  171. return -EINVAL;
  172. ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
  173. xlate->host_fmt);
  174. if (ret < 0)
  175. return ret;
  176. bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
  177. ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
  178. fmt->fmt.pix.height);
  179. if (ret < 0)
  180. return ret;
  181. sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
  182. } else {
  183. /* Called from VIDIOC_REQBUFS or in compatibility mode */
  184. sizes[0] = icd->sizeimage;
  185. }
  186. alloc_ctxs[0] = priv->alloc_ctx;
  187. if (!vq->num_buffers)
  188. priv->sequence = 0;
  189. if (!*count)
  190. *count = 2;
  191. priv->vb_count = *count;
  192. *num_planes = 1;
  193. /* Number of hardware slots */
  194. if (is_continuous_transfer(priv))
  195. priv->nr_hw_slots = MAX_BUFFER_NUM;
  196. else
  197. priv->nr_hw_slots = 1;
  198. dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
  199. return 0;
  200. }
  201. static int rcar_vin_setup(struct rcar_vin_priv *priv)
  202. {
  203. struct soc_camera_device *icd = priv->ici.icd;
  204. struct rcar_vin_cam *cam = icd->host_priv;
  205. u32 vnmc, dmr, interrupts;
  206. bool progressive = false, output_is_yuv = false;
  207. switch (priv->field) {
  208. case V4L2_FIELD_TOP:
  209. vnmc = VNMC_IM_ODD;
  210. break;
  211. case V4L2_FIELD_BOTTOM:
  212. vnmc = VNMC_IM_EVEN;
  213. break;
  214. case V4L2_FIELD_INTERLACED:
  215. case V4L2_FIELD_INTERLACED_TB:
  216. vnmc = VNMC_IM_FULL;
  217. break;
  218. case V4L2_FIELD_INTERLACED_BT:
  219. vnmc = VNMC_IM_FULL | VNMC_FOC;
  220. break;
  221. case V4L2_FIELD_NONE:
  222. if (is_continuous_transfer(priv)) {
  223. vnmc = VNMC_IM_ODD_EVEN;
  224. progressive = true;
  225. } else {
  226. vnmc = VNMC_IM_ODD;
  227. }
  228. break;
  229. default:
  230. vnmc = VNMC_IM_ODD;
  231. break;
  232. }
  233. /* input interface */
  234. switch (icd->current_fmt->code) {
  235. case V4L2_MBUS_FMT_YUYV8_1X16:
  236. /* BT.601/BT.1358 16bit YCbCr422 */
  237. vnmc |= VNMC_INF_YUV16;
  238. break;
  239. case V4L2_MBUS_FMT_YUYV8_2X8:
  240. /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
  241. vnmc |= priv->pdata->flags & RCAR_VIN_BT656 ?
  242. VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601;
  243. default:
  244. break;
  245. }
  246. /* output format */
  247. switch (icd->current_fmt->host_fmt->fourcc) {
  248. case V4L2_PIX_FMT_NV16:
  249. iowrite32(ALIGN(cam->width * cam->height, 0x80),
  250. priv->base + VNUVAOF_REG);
  251. dmr = VNDMR_DTMD_YCSEP;
  252. output_is_yuv = true;
  253. break;
  254. case V4L2_PIX_FMT_YUYV:
  255. dmr = VNDMR_BPSM;
  256. output_is_yuv = true;
  257. break;
  258. case V4L2_PIX_FMT_UYVY:
  259. dmr = 0;
  260. output_is_yuv = true;
  261. break;
  262. case V4L2_PIX_FMT_RGB555X:
  263. dmr = VNDMR_DTMD_ARGB1555;
  264. break;
  265. case V4L2_PIX_FMT_RGB565:
  266. dmr = 0;
  267. break;
  268. case V4L2_PIX_FMT_RGB32:
  269. if (priv->chip == RCAR_H2 || priv->chip == RCAR_H1 ||
  270. priv->chip == RCAR_E1) {
  271. dmr = VNDMR_EXRGB;
  272. break;
  273. }
  274. default:
  275. dev_warn(icd->parent, "Invalid fourcc format (0x%x)\n",
  276. icd->current_fmt->host_fmt->fourcc);
  277. return -EINVAL;
  278. }
  279. /* Always update on field change */
  280. vnmc |= VNMC_VUP;
  281. /* If input and output use the same colorspace, use bypass mode */
  282. if (output_is_yuv)
  283. vnmc |= VNMC_BPS;
  284. /* progressive or interlaced mode */
  285. interrupts = progressive ? VNIE_FIE | VNIE_EFE : VNIE_EFE;
  286. /* ack interrupts */
  287. iowrite32(interrupts, priv->base + VNINTS_REG);
  288. /* enable interrupts */
  289. iowrite32(interrupts, priv->base + VNIE_REG);
  290. /* start capturing */
  291. iowrite32(dmr, priv->base + VNDMR_REG);
  292. iowrite32(vnmc | VNMC_ME, priv->base + VNMC_REG);
  293. return 0;
  294. }
  295. static void rcar_vin_capture(struct rcar_vin_priv *priv)
  296. {
  297. if (is_continuous_transfer(priv))
  298. /* Continuous Frame Capture Mode */
  299. iowrite32(VNFC_C_FRAME, priv->base + VNFC_REG);
  300. else
  301. /* Single Frame Capture Mode */
  302. iowrite32(VNFC_S_FRAME, priv->base + VNFC_REG);
  303. }
  304. static void rcar_vin_request_capture_stop(struct rcar_vin_priv *priv)
  305. {
  306. priv->state = STOPPING;
  307. /* set continuous & single transfer off */
  308. iowrite32(0, priv->base + VNFC_REG);
  309. /* disable capture (release DMA buffer), reset */
  310. iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
  311. priv->base + VNMC_REG);
  312. /* update the status if stopped already */
  313. if (!(ioread32(priv->base + VNMS_REG) & VNMS_CA))
  314. priv->state = STOPPED;
  315. }
  316. static int rcar_vin_get_free_hw_slot(struct rcar_vin_priv *priv)
  317. {
  318. int slot;
  319. for (slot = 0; slot < priv->nr_hw_slots; slot++)
  320. if (priv->queue_buf[slot] == NULL)
  321. return slot;
  322. return -1;
  323. }
  324. static int rcar_vin_hw_ready(struct rcar_vin_priv *priv)
  325. {
  326. /* Ensure all HW slots are filled */
  327. return rcar_vin_get_free_hw_slot(priv) < 0 ? 1 : 0;
  328. }
  329. /* Moves a buffer from the queue to the HW slots */
  330. static int rcar_vin_fill_hw_slot(struct rcar_vin_priv *priv)
  331. {
  332. struct vb2_buffer *vb;
  333. dma_addr_t phys_addr_top;
  334. int slot;
  335. if (list_empty(&priv->capture))
  336. return 0;
  337. /* Find a free HW slot */
  338. slot = rcar_vin_get_free_hw_slot(priv);
  339. if (slot < 0)
  340. return 0;
  341. vb = &list_entry(priv->capture.next, struct rcar_vin_buffer, list)->vb;
  342. list_del_init(to_buf_list(vb));
  343. priv->queue_buf[slot] = vb;
  344. phys_addr_top = vb2_dma_contig_plane_dma_addr(vb, 0);
  345. iowrite32(phys_addr_top, priv->base + VNMB_REG(slot));
  346. return 1;
  347. }
  348. static void rcar_vin_videobuf_queue(struct vb2_buffer *vb)
  349. {
  350. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  351. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  352. struct rcar_vin_priv *priv = ici->priv;
  353. unsigned long size;
  354. size = icd->sizeimage;
  355. if (vb2_plane_size(vb, 0) < size) {
  356. dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
  357. vb->v4l2_buf.index, vb2_plane_size(vb, 0), size);
  358. goto error;
  359. }
  360. vb2_set_plane_payload(vb, 0, size);
  361. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  362. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  363. spin_lock_irq(&priv->lock);
  364. list_add_tail(to_buf_list(vb), &priv->capture);
  365. rcar_vin_fill_hw_slot(priv);
  366. /* If we weren't running, and have enough buffers, start capturing! */
  367. if (priv->state != RUNNING && rcar_vin_hw_ready(priv)) {
  368. if (rcar_vin_setup(priv)) {
  369. /* Submit error */
  370. list_del_init(to_buf_list(vb));
  371. spin_unlock_irq(&priv->lock);
  372. goto error;
  373. }
  374. priv->request_to_stop = false;
  375. init_completion(&priv->capture_stop);
  376. priv->state = RUNNING;
  377. rcar_vin_capture(priv);
  378. }
  379. spin_unlock_irq(&priv->lock);
  380. return;
  381. error:
  382. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  383. }
  384. static void rcar_vin_videobuf_release(struct vb2_buffer *vb)
  385. {
  386. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  387. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  388. struct rcar_vin_priv *priv = ici->priv;
  389. unsigned int i;
  390. int buf_in_use = 0;
  391. spin_lock_irq(&priv->lock);
  392. /* Is the buffer in use by the VIN hardware? */
  393. for (i = 0; i < MAX_BUFFER_NUM; i++) {
  394. if (priv->queue_buf[i] == vb) {
  395. buf_in_use = 1;
  396. break;
  397. }
  398. }
  399. if (buf_in_use) {
  400. while (priv->state != STOPPED) {
  401. /* issue stop if running */
  402. if (priv->state == RUNNING)
  403. rcar_vin_request_capture_stop(priv);
  404. /* wait until capturing has been stopped */
  405. if (priv->state == STOPPING) {
  406. priv->request_to_stop = true;
  407. spin_unlock_irq(&priv->lock);
  408. wait_for_completion(&priv->capture_stop);
  409. spin_lock_irq(&priv->lock);
  410. }
  411. }
  412. /*
  413. * Capturing has now stopped. The buffer we have been asked
  414. * to release could be any of the current buffers in use, so
  415. * release all buffers that are in use by HW
  416. */
  417. for (i = 0; i < MAX_BUFFER_NUM; i++) {
  418. if (priv->queue_buf[i]) {
  419. vb2_buffer_done(priv->queue_buf[i],
  420. VB2_BUF_STATE_ERROR);
  421. priv->queue_buf[i] = NULL;
  422. }
  423. }
  424. } else {
  425. list_del_init(to_buf_list(vb));
  426. }
  427. spin_unlock_irq(&priv->lock);
  428. }
  429. static int rcar_vin_videobuf_init(struct vb2_buffer *vb)
  430. {
  431. INIT_LIST_HEAD(to_buf_list(vb));
  432. return 0;
  433. }
  434. static int rcar_vin_stop_streaming(struct vb2_queue *vq)
  435. {
  436. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  437. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  438. struct rcar_vin_priv *priv = ici->priv;
  439. struct list_head *buf_head, *tmp;
  440. spin_lock_irq(&priv->lock);
  441. list_for_each_safe(buf_head, tmp, &priv->capture)
  442. list_del_init(buf_head);
  443. spin_unlock_irq(&priv->lock);
  444. return 0;
  445. }
  446. static struct vb2_ops rcar_vin_vb2_ops = {
  447. .queue_setup = rcar_vin_videobuf_setup,
  448. .buf_init = rcar_vin_videobuf_init,
  449. .buf_cleanup = rcar_vin_videobuf_release,
  450. .buf_queue = rcar_vin_videobuf_queue,
  451. .stop_streaming = rcar_vin_stop_streaming,
  452. .wait_prepare = soc_camera_unlock,
  453. .wait_finish = soc_camera_lock,
  454. };
  455. static irqreturn_t rcar_vin_irq(int irq, void *data)
  456. {
  457. struct rcar_vin_priv *priv = data;
  458. u32 int_status;
  459. bool can_run = false, hw_stopped;
  460. int slot;
  461. unsigned int handled = 0;
  462. spin_lock(&priv->lock);
  463. int_status = ioread32(priv->base + VNINTS_REG);
  464. if (!int_status)
  465. goto done;
  466. /* ack interrupts */
  467. iowrite32(int_status, priv->base + VNINTS_REG);
  468. handled = 1;
  469. /* nothing to do if capture status is 'STOPPED' */
  470. if (priv->state == STOPPED)
  471. goto done;
  472. hw_stopped = !(ioread32(priv->base + VNMS_REG) & VNMS_CA);
  473. if (!priv->request_to_stop) {
  474. if (is_continuous_transfer(priv))
  475. slot = (ioread32(priv->base + VNMS_REG) &
  476. VNMS_FBS_MASK) >> VNMS_FBS_SHIFT;
  477. else
  478. slot = 0;
  479. priv->queue_buf[slot]->v4l2_buf.field = priv->field;
  480. priv->queue_buf[slot]->v4l2_buf.sequence = priv->sequence++;
  481. do_gettimeofday(&priv->queue_buf[slot]->v4l2_buf.timestamp);
  482. vb2_buffer_done(priv->queue_buf[slot], VB2_BUF_STATE_DONE);
  483. priv->queue_buf[slot] = NULL;
  484. if (priv->state != STOPPING)
  485. can_run = rcar_vin_fill_hw_slot(priv);
  486. if (hw_stopped || !can_run) {
  487. priv->state = STOPPED;
  488. } else if (is_continuous_transfer(priv) &&
  489. list_empty(&priv->capture) &&
  490. priv->state == RUNNING) {
  491. /*
  492. * The continuous capturing requires an explicit stop
  493. * operation when there is no buffer to be set into
  494. * the VnMBm registers.
  495. */
  496. rcar_vin_request_capture_stop(priv);
  497. } else {
  498. rcar_vin_capture(priv);
  499. }
  500. } else if (hw_stopped) {
  501. priv->state = STOPPED;
  502. priv->request_to_stop = false;
  503. complete(&priv->capture_stop);
  504. }
  505. done:
  506. spin_unlock(&priv->lock);
  507. return IRQ_RETVAL(handled);
  508. }
  509. static int rcar_vin_add_device(struct soc_camera_device *icd)
  510. {
  511. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  512. struct rcar_vin_priv *priv = ici->priv;
  513. int i;
  514. for (i = 0; i < MAX_BUFFER_NUM; i++)
  515. priv->queue_buf[i] = NULL;
  516. pm_runtime_get_sync(ici->v4l2_dev.dev);
  517. dev_dbg(icd->parent, "R-Car VIN driver attached to camera %d\n",
  518. icd->devnum);
  519. return 0;
  520. }
  521. static void rcar_vin_remove_device(struct soc_camera_device *icd)
  522. {
  523. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  524. struct rcar_vin_priv *priv = ici->priv;
  525. struct vb2_buffer *vb;
  526. int i;
  527. /* disable capture, disable interrupts */
  528. iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME,
  529. priv->base + VNMC_REG);
  530. iowrite32(0, priv->base + VNIE_REG);
  531. priv->state = STOPPED;
  532. priv->request_to_stop = false;
  533. /* make sure active buffer is cancelled */
  534. spin_lock_irq(&priv->lock);
  535. for (i = 0; i < MAX_BUFFER_NUM; i++) {
  536. vb = priv->queue_buf[i];
  537. if (vb) {
  538. list_del_init(to_buf_list(vb));
  539. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  540. }
  541. }
  542. spin_unlock_irq(&priv->lock);
  543. pm_runtime_put(ici->v4l2_dev.dev);
  544. dev_dbg(icd->parent, "R-Car VIN driver detached from camera %d\n",
  545. icd->devnum);
  546. }
  547. /* Called with .host_lock held */
  548. static int rcar_vin_clock_start(struct soc_camera_host *ici)
  549. {
  550. /* VIN does not have "mclk" */
  551. return 0;
  552. }
  553. /* Called with .host_lock held */
  554. static void rcar_vin_clock_stop(struct soc_camera_host *ici)
  555. {
  556. /* VIN does not have "mclk" */
  557. }
  558. /* rect is guaranteed to not exceed the scaled camera rectangle */
  559. static int rcar_vin_set_rect(struct soc_camera_device *icd)
  560. {
  561. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  562. struct rcar_vin_cam *cam = icd->host_priv;
  563. struct rcar_vin_priv *priv = ici->priv;
  564. unsigned int left_offset, top_offset;
  565. unsigned char dsize = 0;
  566. struct v4l2_rect *cam_subrect = &cam->subrect;
  567. dev_dbg(icd->parent, "Crop %ux%u@%u:%u\n",
  568. icd->user_width, icd->user_height, cam->vin_left, cam->vin_top);
  569. left_offset = cam->vin_left;
  570. top_offset = cam->vin_top;
  571. if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_RGB32 &&
  572. priv->chip == RCAR_E1)
  573. dsize = 1;
  574. dev_dbg(icd->parent, "Cam %ux%u@%u:%u\n",
  575. cam->width, cam->height, cam->vin_left, cam->vin_top);
  576. dev_dbg(icd->parent, "Cam subrect %ux%u@%u:%u\n",
  577. cam_subrect->width, cam_subrect->height,
  578. cam_subrect->left, cam_subrect->top);
  579. /* Set Start/End Pixel/Line Pre-Clip */
  580. iowrite32(left_offset << dsize, priv->base + VNSPPRC_REG);
  581. iowrite32((left_offset + cam->width - 1) << dsize,
  582. priv->base + VNEPPRC_REG);
  583. switch (priv->field) {
  584. case V4L2_FIELD_INTERLACED:
  585. case V4L2_FIELD_INTERLACED_TB:
  586. case V4L2_FIELD_INTERLACED_BT:
  587. iowrite32(top_offset / 2, priv->base + VNSLPRC_REG);
  588. iowrite32((top_offset + cam->height) / 2 - 1,
  589. priv->base + VNELPRC_REG);
  590. break;
  591. default:
  592. iowrite32(top_offset, priv->base + VNSLPRC_REG);
  593. iowrite32(top_offset + cam->height - 1,
  594. priv->base + VNELPRC_REG);
  595. break;
  596. }
  597. /* Set Start/End Pixel/Line Post-Clip */
  598. iowrite32(0, priv->base + VNSPPOC_REG);
  599. iowrite32(0, priv->base + VNSLPOC_REG);
  600. iowrite32((cam_subrect->width - 1) << dsize, priv->base + VNEPPOC_REG);
  601. switch (priv->field) {
  602. case V4L2_FIELD_INTERLACED:
  603. case V4L2_FIELD_INTERLACED_TB:
  604. case V4L2_FIELD_INTERLACED_BT:
  605. iowrite32(cam_subrect->height / 2 - 1,
  606. priv->base + VNELPOC_REG);
  607. break;
  608. default:
  609. iowrite32(cam_subrect->height - 1, priv->base + VNELPOC_REG);
  610. break;
  611. }
  612. iowrite32(ALIGN(cam->width, 0x10), priv->base + VNIS_REG);
  613. return 0;
  614. }
  615. static void capture_stop_preserve(struct rcar_vin_priv *priv, u32 *vnmc)
  616. {
  617. *vnmc = ioread32(priv->base + VNMC_REG);
  618. /* module disable */
  619. iowrite32(*vnmc & ~VNMC_ME, priv->base + VNMC_REG);
  620. }
  621. static void capture_restore(struct rcar_vin_priv *priv, u32 vnmc)
  622. {
  623. unsigned long timeout = jiffies + 10 * HZ;
  624. /*
  625. * Wait until the end of the current frame. It can take a long time,
  626. * but if it has been aborted by a MRST1 reset, it should exit sooner.
  627. */
  628. while ((ioread32(priv->base + VNMS_REG) & VNMS_AV) &&
  629. time_before(jiffies, timeout))
  630. msleep(1);
  631. if (time_after(jiffies, timeout)) {
  632. dev_err(priv->ici.v4l2_dev.dev,
  633. "Timeout waiting for frame end! Interface problem?\n");
  634. return;
  635. }
  636. iowrite32(vnmc, priv->base + VNMC_REG);
  637. }
  638. #define VIN_MBUS_FLAGS (V4L2_MBUS_MASTER | \
  639. V4L2_MBUS_PCLK_SAMPLE_RISING | \
  640. V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
  641. V4L2_MBUS_HSYNC_ACTIVE_LOW | \
  642. V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
  643. V4L2_MBUS_VSYNC_ACTIVE_LOW | \
  644. V4L2_MBUS_DATA_ACTIVE_HIGH)
  645. static int rcar_vin_set_bus_param(struct soc_camera_device *icd)
  646. {
  647. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  648. struct rcar_vin_priv *priv = ici->priv;
  649. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  650. struct v4l2_mbus_config cfg;
  651. unsigned long common_flags;
  652. u32 vnmc;
  653. u32 val;
  654. int ret;
  655. capture_stop_preserve(priv, &vnmc);
  656. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  657. if (!ret) {
  658. common_flags = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
  659. if (!common_flags) {
  660. dev_warn(icd->parent,
  661. "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
  662. cfg.flags, VIN_MBUS_FLAGS);
  663. return -EINVAL;
  664. }
  665. } else if (ret != -ENOIOCTLCMD) {
  666. return ret;
  667. } else {
  668. common_flags = VIN_MBUS_FLAGS;
  669. }
  670. /* Make choises, based on platform preferences */
  671. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  672. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  673. if (priv->pdata->flags & RCAR_VIN_HSYNC_ACTIVE_LOW)
  674. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  675. else
  676. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  677. }
  678. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  679. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  680. if (priv->pdata->flags & RCAR_VIN_VSYNC_ACTIVE_LOW)
  681. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  682. else
  683. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  684. }
  685. cfg.flags = common_flags;
  686. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  687. if (ret < 0 && ret != -ENOIOCTLCMD)
  688. return ret;
  689. val = priv->field == V4L2_FIELD_NONE ? VNDMR2_FTEV : 0;
  690. if (!(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
  691. val |= VNDMR2_VPS;
  692. if (!(common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
  693. val |= VNDMR2_HPS;
  694. iowrite32(val, priv->base + VNDMR2_REG);
  695. ret = rcar_vin_set_rect(icd);
  696. if (ret < 0)
  697. return ret;
  698. capture_restore(priv, vnmc);
  699. return 0;
  700. }
  701. static int rcar_vin_try_bus_param(struct soc_camera_device *icd,
  702. unsigned char buswidth)
  703. {
  704. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  705. struct v4l2_mbus_config cfg;
  706. int ret;
  707. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  708. if (ret == -ENOIOCTLCMD)
  709. return 0;
  710. else if (ret)
  711. return ret;
  712. if (buswidth > 24)
  713. return -EINVAL;
  714. /* check is there common mbus flags */
  715. ret = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS);
  716. if (ret)
  717. return 0;
  718. dev_warn(icd->parent,
  719. "MBUS flags incompatible: camera 0x%x, host 0x%x\n",
  720. cfg.flags, VIN_MBUS_FLAGS);
  721. return -EINVAL;
  722. }
  723. static bool rcar_vin_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  724. {
  725. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  726. (fmt->bits_per_sample > 8 &&
  727. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  728. }
  729. static const struct soc_mbus_pixelfmt rcar_vin_formats[] = {
  730. {
  731. .fourcc = V4L2_PIX_FMT_NV16,
  732. .name = "NV16",
  733. .bits_per_sample = 8,
  734. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  735. .order = SOC_MBUS_ORDER_LE,
  736. .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
  737. },
  738. {
  739. .fourcc = V4L2_PIX_FMT_UYVY,
  740. .name = "UYVY",
  741. .bits_per_sample = 16,
  742. .packing = SOC_MBUS_PACKING_NONE,
  743. .order = SOC_MBUS_ORDER_LE,
  744. .layout = SOC_MBUS_LAYOUT_PACKED,
  745. },
  746. {
  747. .fourcc = V4L2_PIX_FMT_RGB565,
  748. .name = "RGB565",
  749. .bits_per_sample = 16,
  750. .packing = SOC_MBUS_PACKING_NONE,
  751. .order = SOC_MBUS_ORDER_LE,
  752. .layout = SOC_MBUS_LAYOUT_PACKED,
  753. },
  754. {
  755. .fourcc = V4L2_PIX_FMT_RGB555X,
  756. .name = "ARGB1555",
  757. .bits_per_sample = 16,
  758. .packing = SOC_MBUS_PACKING_NONE,
  759. .order = SOC_MBUS_ORDER_LE,
  760. .layout = SOC_MBUS_LAYOUT_PACKED,
  761. },
  762. {
  763. .fourcc = V4L2_PIX_FMT_RGB32,
  764. .name = "RGB888",
  765. .bits_per_sample = 32,
  766. .packing = SOC_MBUS_PACKING_NONE,
  767. .order = SOC_MBUS_ORDER_LE,
  768. .layout = SOC_MBUS_LAYOUT_PACKED,
  769. },
  770. };
  771. static int rcar_vin_get_formats(struct soc_camera_device *icd, unsigned int idx,
  772. struct soc_camera_format_xlate *xlate)
  773. {
  774. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  775. struct device *dev = icd->parent;
  776. int ret, k, n;
  777. int formats = 0;
  778. struct rcar_vin_cam *cam;
  779. enum v4l2_mbus_pixelcode code;
  780. const struct soc_mbus_pixelfmt *fmt;
  781. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  782. if (ret < 0)
  783. return 0;
  784. fmt = soc_mbus_get_fmtdesc(code);
  785. if (!fmt) {
  786. dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
  787. return 0;
  788. }
  789. ret = rcar_vin_try_bus_param(icd, fmt->bits_per_sample);
  790. if (ret < 0)
  791. return 0;
  792. if (!icd->host_priv) {
  793. struct v4l2_mbus_framefmt mf;
  794. struct v4l2_rect rect;
  795. struct device *dev = icd->parent;
  796. int shift;
  797. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  798. if (ret < 0)
  799. return ret;
  800. /* Cache current client geometry */
  801. ret = soc_camera_client_g_rect(sd, &rect);
  802. if (ret == -ENOIOCTLCMD) {
  803. /* Sensor driver doesn't support cropping */
  804. rect.left = 0;
  805. rect.top = 0;
  806. rect.width = mf.width;
  807. rect.height = mf.height;
  808. } else if (ret < 0) {
  809. return ret;
  810. }
  811. /*
  812. * If sensor proposes too large format then try smaller ones:
  813. * 1280x960, 640x480, 320x240
  814. */
  815. for (shift = 0; shift < 3; shift++) {
  816. if (mf.width <= VIN_MAX_WIDTH &&
  817. mf.height <= VIN_MAX_HEIGHT)
  818. break;
  819. mf.width = 1280 >> shift;
  820. mf.height = 960 >> shift;
  821. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  822. soc_camera_grp_id(icd),
  823. video, s_mbus_fmt,
  824. &mf);
  825. if (ret < 0)
  826. return ret;
  827. }
  828. if (shift == 3) {
  829. dev_err(dev,
  830. "Failed to configure the client below %ux%x\n",
  831. mf.width, mf.height);
  832. return -EIO;
  833. }
  834. dev_dbg(dev, "camera fmt %ux%u\n", mf.width, mf.height);
  835. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  836. if (!cam)
  837. return -ENOMEM;
  838. /*
  839. * We are called with current camera crop,
  840. * initialise subrect with it
  841. */
  842. cam->rect = rect;
  843. cam->subrect = rect;
  844. cam->width = mf.width;
  845. cam->height = mf.height;
  846. icd->host_priv = cam;
  847. } else {
  848. cam = icd->host_priv;
  849. }
  850. /* Beginning of a pass */
  851. if (!idx)
  852. cam->extra_fmt = NULL;
  853. switch (code) {
  854. case V4L2_MBUS_FMT_YUYV8_1X16:
  855. case V4L2_MBUS_FMT_YUYV8_2X8:
  856. if (cam->extra_fmt)
  857. break;
  858. /* Add all our formats that can be generated by VIN */
  859. cam->extra_fmt = rcar_vin_formats;
  860. n = ARRAY_SIZE(rcar_vin_formats);
  861. formats += n;
  862. for (k = 0; xlate && k < n; k++, xlate++) {
  863. xlate->host_fmt = &rcar_vin_formats[k];
  864. xlate->code = code;
  865. dev_dbg(dev, "Providing format %s using code %d\n",
  866. rcar_vin_formats[k].name, code);
  867. }
  868. break;
  869. default:
  870. if (!rcar_vin_packing_supported(fmt))
  871. return 0;
  872. dev_dbg(dev, "Providing format %s in pass-through mode\n",
  873. fmt->name);
  874. break;
  875. }
  876. /* Generic pass-through */
  877. formats++;
  878. if (xlate) {
  879. xlate->host_fmt = fmt;
  880. xlate->code = code;
  881. xlate++;
  882. }
  883. return formats;
  884. }
  885. static void rcar_vin_put_formats(struct soc_camera_device *icd)
  886. {
  887. kfree(icd->host_priv);
  888. icd->host_priv = NULL;
  889. }
  890. static int rcar_vin_set_crop(struct soc_camera_device *icd,
  891. const struct v4l2_crop *a)
  892. {
  893. struct v4l2_crop a_writable = *a;
  894. const struct v4l2_rect *rect = &a_writable.c;
  895. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  896. struct rcar_vin_priv *priv = ici->priv;
  897. struct v4l2_crop cam_crop;
  898. struct rcar_vin_cam *cam = icd->host_priv;
  899. struct v4l2_rect *cam_rect = &cam_crop.c;
  900. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  901. struct device *dev = icd->parent;
  902. struct v4l2_mbus_framefmt mf;
  903. u32 vnmc;
  904. int ret, i;
  905. dev_dbg(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
  906. rect->left, rect->top);
  907. /* During camera cropping its output window can change too, stop VIN */
  908. capture_stop_preserve(priv, &vnmc);
  909. dev_dbg(dev, "VNMC_REG 0x%x\n", vnmc);
  910. /* Apply iterative camera S_CROP for new input window. */
  911. ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop,
  912. &cam->rect, &cam->subrect);
  913. if (ret < 0)
  914. return ret;
  915. dev_dbg(dev, "camera cropped to %ux%u@%u:%u\n",
  916. cam_rect->width, cam_rect->height,
  917. cam_rect->left, cam_rect->top);
  918. /* On success cam_crop contains current camera crop */
  919. /* Retrieve camera output window */
  920. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  921. if (ret < 0)
  922. return ret;
  923. if (mf.width > VIN_MAX_WIDTH || mf.height > VIN_MAX_HEIGHT)
  924. return -EINVAL;
  925. /* Cache camera output window */
  926. cam->width = mf.width;
  927. cam->height = mf.height;
  928. icd->user_width = cam->width;
  929. icd->user_height = cam->height;
  930. cam->vin_left = rect->left & ~1;
  931. cam->vin_top = rect->top & ~1;
  932. /* Use VIN cropping to crop to the new window. */
  933. ret = rcar_vin_set_rect(icd);
  934. if (ret < 0)
  935. return ret;
  936. cam->subrect = *rect;
  937. dev_dbg(dev, "VIN cropped to %ux%u@%u:%u\n",
  938. icd->user_width, icd->user_height,
  939. cam->vin_left, cam->vin_top);
  940. /* Restore capture */
  941. for (i = 0; i < MAX_BUFFER_NUM; i++) {
  942. if (priv->queue_buf[i] && priv->state == STOPPED) {
  943. vnmc |= VNMC_ME;
  944. break;
  945. }
  946. }
  947. capture_restore(priv, vnmc);
  948. /* Even if only camera cropping succeeded */
  949. return ret;
  950. }
  951. static int rcar_vin_get_crop(struct soc_camera_device *icd,
  952. struct v4l2_crop *a)
  953. {
  954. struct rcar_vin_cam *cam = icd->host_priv;
  955. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  956. a->c = cam->subrect;
  957. return 0;
  958. }
  959. /* Similar to set_crop multistage iterative algorithm */
  960. static int rcar_vin_set_fmt(struct soc_camera_device *icd,
  961. struct v4l2_format *f)
  962. {
  963. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  964. struct rcar_vin_priv *priv = ici->priv;
  965. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  966. struct rcar_vin_cam *cam = icd->host_priv;
  967. struct v4l2_pix_format *pix = &f->fmt.pix;
  968. struct v4l2_mbus_framefmt mf;
  969. struct device *dev = icd->parent;
  970. __u32 pixfmt = pix->pixelformat;
  971. const struct soc_camera_format_xlate *xlate;
  972. unsigned int vin_sub_width = 0, vin_sub_height = 0;
  973. int ret;
  974. bool can_scale;
  975. enum v4l2_field field;
  976. v4l2_std_id std;
  977. dev_dbg(dev, "S_FMT(pix=0x%x, %ux%u)\n",
  978. pixfmt, pix->width, pix->height);
  979. switch (pix->field) {
  980. default:
  981. pix->field = V4L2_FIELD_NONE;
  982. /* fall-through */
  983. case V4L2_FIELD_NONE:
  984. case V4L2_FIELD_TOP:
  985. case V4L2_FIELD_BOTTOM:
  986. case V4L2_FIELD_INTERLACED_TB:
  987. case V4L2_FIELD_INTERLACED_BT:
  988. field = pix->field;
  989. break;
  990. case V4L2_FIELD_INTERLACED:
  991. /* Query for standard if not explicitly mentioned _TB/_BT */
  992. ret = v4l2_subdev_call(sd, video, querystd, &std);
  993. if (ret < 0)
  994. std = V4L2_STD_625_50;
  995. field = std & V4L2_STD_625_50 ? V4L2_FIELD_INTERLACED_TB :
  996. V4L2_FIELD_INTERLACED_BT;
  997. break;
  998. }
  999. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1000. if (!xlate) {
  1001. dev_warn(dev, "Format %x not found\n", pixfmt);
  1002. return -EINVAL;
  1003. }
  1004. /* Calculate client output geometry */
  1005. soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf,
  1006. 12);
  1007. mf.field = pix->field;
  1008. mf.colorspace = pix->colorspace;
  1009. mf.code = xlate->code;
  1010. switch (pixfmt) {
  1011. case V4L2_PIX_FMT_RGB32:
  1012. can_scale = priv->chip != RCAR_E1;
  1013. break;
  1014. case V4L2_PIX_FMT_UYVY:
  1015. case V4L2_PIX_FMT_YUYV:
  1016. case V4L2_PIX_FMT_RGB565:
  1017. case V4L2_PIX_FMT_RGB555X:
  1018. can_scale = true;
  1019. break;
  1020. default:
  1021. can_scale = false;
  1022. break;
  1023. }
  1024. dev_dbg(dev, "request camera output %ux%u\n", mf.width, mf.height);
  1025. ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect,
  1026. &mf, &vin_sub_width, &vin_sub_height,
  1027. can_scale, 12);
  1028. /* Done with the camera. Now see if we can improve the result */
  1029. dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
  1030. ret, mf.width, mf.height, pix->width, pix->height);
  1031. if (ret == -ENOIOCTLCMD)
  1032. dev_dbg(dev, "Sensor doesn't support scaling\n");
  1033. else if (ret < 0)
  1034. return ret;
  1035. if (mf.code != xlate->code)
  1036. return -EINVAL;
  1037. /* Prepare VIN crop */
  1038. cam->width = mf.width;
  1039. cam->height = mf.height;
  1040. /* Use VIN scaling to scale to the requested user window. */
  1041. /* We cannot scale up */
  1042. if (pix->width > vin_sub_width)
  1043. vin_sub_width = pix->width;
  1044. if (pix->height > vin_sub_height)
  1045. vin_sub_height = pix->height;
  1046. pix->colorspace = mf.colorspace;
  1047. if (!can_scale) {
  1048. pix->width = vin_sub_width;
  1049. pix->height = vin_sub_height;
  1050. }
  1051. /*
  1052. * We have calculated CFLCR, the actual configuration will be performed
  1053. * in rcar_vin_set_bus_param()
  1054. */
  1055. dev_dbg(dev, "W: %u : %u, H: %u : %u\n",
  1056. vin_sub_width, pix->width, vin_sub_height, pix->height);
  1057. icd->current_fmt = xlate;
  1058. priv->field = field;
  1059. return 0;
  1060. }
  1061. static int rcar_vin_try_fmt(struct soc_camera_device *icd,
  1062. struct v4l2_format *f)
  1063. {
  1064. const struct soc_camera_format_xlate *xlate;
  1065. struct v4l2_pix_format *pix = &f->fmt.pix;
  1066. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1067. struct v4l2_mbus_framefmt mf;
  1068. __u32 pixfmt = pix->pixelformat;
  1069. int width, height;
  1070. int ret;
  1071. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1072. if (!xlate) {
  1073. xlate = icd->current_fmt;
  1074. dev_dbg(icd->parent, "Format %x not found, keeping %x\n",
  1075. pixfmt, xlate->host_fmt->fourcc);
  1076. pixfmt = xlate->host_fmt->fourcc;
  1077. pix->pixelformat = pixfmt;
  1078. pix->colorspace = icd->colorspace;
  1079. }
  1080. /* FIXME: calculate using depth and bus width */
  1081. v4l_bound_align_image(&pix->width, 2, VIN_MAX_WIDTH, 1,
  1082. &pix->height, 4, VIN_MAX_HEIGHT, 2, 0);
  1083. width = pix->width;
  1084. height = pix->height;
  1085. /* let soc-camera calculate these values */
  1086. pix->bytesperline = 0;
  1087. pix->sizeimage = 0;
  1088. /* limit to sensor capabilities */
  1089. mf.width = pix->width;
  1090. mf.height = pix->height;
  1091. mf.field = pix->field;
  1092. mf.code = xlate->code;
  1093. mf.colorspace = pix->colorspace;
  1094. ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
  1095. video, try_mbus_fmt, &mf);
  1096. if (ret < 0)
  1097. return ret;
  1098. pix->width = mf.width;
  1099. pix->height = mf.height;
  1100. pix->field = mf.field;
  1101. pix->colorspace = mf.colorspace;
  1102. if (pixfmt == V4L2_PIX_FMT_NV16) {
  1103. /* FIXME: check against rect_max after converting soc-camera */
  1104. /* We can scale precisely, need a bigger image from camera */
  1105. if (pix->width < width || pix->height < height) {
  1106. /*
  1107. * We presume, the sensor behaves sanely, i.e. if
  1108. * requested a bigger rectangle, it will not return a
  1109. * smaller one.
  1110. */
  1111. mf.width = VIN_MAX_WIDTH;
  1112. mf.height = VIN_MAX_HEIGHT;
  1113. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  1114. soc_camera_grp_id(icd),
  1115. video, try_mbus_fmt,
  1116. &mf);
  1117. if (ret < 0) {
  1118. dev_err(icd->parent,
  1119. "client try_fmt() = %d\n", ret);
  1120. return ret;
  1121. }
  1122. }
  1123. /* We will scale exactly */
  1124. if (mf.width > width)
  1125. pix->width = width;
  1126. if (mf.height > height)
  1127. pix->height = height;
  1128. }
  1129. return ret;
  1130. }
  1131. static unsigned int rcar_vin_poll(struct file *file, poll_table *pt)
  1132. {
  1133. struct soc_camera_device *icd = file->private_data;
  1134. return vb2_poll(&icd->vb2_vidq, file, pt);
  1135. }
  1136. static int rcar_vin_querycap(struct soc_camera_host *ici,
  1137. struct v4l2_capability *cap)
  1138. {
  1139. strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
  1140. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1141. return 0;
  1142. }
  1143. static int rcar_vin_init_videobuf2(struct vb2_queue *vq,
  1144. struct soc_camera_device *icd)
  1145. {
  1146. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1147. vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1148. vq->drv_priv = icd;
  1149. vq->ops = &rcar_vin_vb2_ops;
  1150. vq->mem_ops = &vb2_dma_contig_memops;
  1151. vq->buf_struct_size = sizeof(struct rcar_vin_buffer);
  1152. vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1153. return vb2_queue_init(vq);
  1154. }
  1155. static struct soc_camera_host_ops rcar_vin_host_ops = {
  1156. .owner = THIS_MODULE,
  1157. .add = rcar_vin_add_device,
  1158. .remove = rcar_vin_remove_device,
  1159. .clock_start = rcar_vin_clock_start,
  1160. .clock_stop = rcar_vin_clock_stop,
  1161. .get_formats = rcar_vin_get_formats,
  1162. .put_formats = rcar_vin_put_formats,
  1163. .get_crop = rcar_vin_get_crop,
  1164. .set_crop = rcar_vin_set_crop,
  1165. .try_fmt = rcar_vin_try_fmt,
  1166. .set_fmt = rcar_vin_set_fmt,
  1167. .poll = rcar_vin_poll,
  1168. .querycap = rcar_vin_querycap,
  1169. .set_bus_param = rcar_vin_set_bus_param,
  1170. .init_videobuf2 = rcar_vin_init_videobuf2,
  1171. };
  1172. static struct platform_device_id rcar_vin_id_table[] = {
  1173. { "r8a7790-vin", RCAR_H2 },
  1174. { "r8a7779-vin", RCAR_H1 },
  1175. { "r8a7778-vin", RCAR_M1 },
  1176. { "uPD35004-vin", RCAR_E1 },
  1177. {},
  1178. };
  1179. MODULE_DEVICE_TABLE(platform, rcar_vin_id_table);
  1180. static int rcar_vin_probe(struct platform_device *pdev)
  1181. {
  1182. struct rcar_vin_priv *priv;
  1183. struct resource *mem;
  1184. struct rcar_vin_platform_data *pdata;
  1185. int irq, ret;
  1186. pdata = pdev->dev.platform_data;
  1187. if (!pdata || !pdata->flags) {
  1188. dev_err(&pdev->dev, "platform data not set\n");
  1189. return -EINVAL;
  1190. }
  1191. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1192. if (mem == NULL)
  1193. return -EINVAL;
  1194. irq = platform_get_irq(pdev, 0);
  1195. if (irq <= 0)
  1196. return -EINVAL;
  1197. priv = devm_kzalloc(&pdev->dev, sizeof(struct rcar_vin_priv),
  1198. GFP_KERNEL);
  1199. if (!priv)
  1200. return -ENOMEM;
  1201. priv->base = devm_ioremap_resource(&pdev->dev, mem);
  1202. if (IS_ERR(priv->base))
  1203. return PTR_ERR(priv->base);
  1204. ret = devm_request_irq(&pdev->dev, irq, rcar_vin_irq, IRQF_SHARED,
  1205. dev_name(&pdev->dev), priv);
  1206. if (ret)
  1207. return ret;
  1208. priv->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1209. if (IS_ERR(priv->alloc_ctx))
  1210. return PTR_ERR(priv->alloc_ctx);
  1211. priv->ici.priv = priv;
  1212. priv->ici.v4l2_dev.dev = &pdev->dev;
  1213. priv->ici.nr = pdev->id;
  1214. priv->ici.drv_name = dev_name(&pdev->dev);
  1215. priv->ici.ops = &rcar_vin_host_ops;
  1216. priv->pdata = pdata;
  1217. priv->chip = pdev->id_entry->driver_data;
  1218. spin_lock_init(&priv->lock);
  1219. INIT_LIST_HEAD(&priv->capture);
  1220. priv->state = STOPPED;
  1221. pm_suspend_ignore_children(&pdev->dev, true);
  1222. pm_runtime_enable(&pdev->dev);
  1223. ret = soc_camera_host_register(&priv->ici);
  1224. if (ret)
  1225. goto cleanup;
  1226. return 0;
  1227. cleanup:
  1228. pm_runtime_disable(&pdev->dev);
  1229. vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
  1230. return ret;
  1231. }
  1232. static int rcar_vin_remove(struct platform_device *pdev)
  1233. {
  1234. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1235. struct rcar_vin_priv *priv = container_of(soc_host,
  1236. struct rcar_vin_priv, ici);
  1237. soc_camera_host_unregister(soc_host);
  1238. pm_runtime_disable(&pdev->dev);
  1239. vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
  1240. return 0;
  1241. }
  1242. static struct platform_driver rcar_vin_driver = {
  1243. .probe = rcar_vin_probe,
  1244. .remove = rcar_vin_remove,
  1245. .driver = {
  1246. .name = DRV_NAME,
  1247. .owner = THIS_MODULE,
  1248. },
  1249. .id_table = rcar_vin_id_table,
  1250. };
  1251. module_platform_driver(rcar_vin_driver);
  1252. MODULE_LICENSE("GPL");
  1253. MODULE_ALIAS("platform:rcar_vin");
  1254. MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");