rcar_vin.c 38 KB

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