rcar_vin.c 38 KB

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