rcar_vin.c 38 KB

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