rcar_vin.c 40 KB

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