mx2_camera.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * V4L2 Driver for i.MX27/i.MX25 camera host
  3. *
  4. * Copyright (C) 2008, Sascha Hauer, Pengutronix
  5. * Copyright (C) 2010, Baruch Siach, Orex Computed Radiography
  6. * Copyright (C) 2012, Javier Martin, Vista Silicon S.L.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/time.h>
  26. #include <linux/device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/mutex.h>
  29. #include <linux/clk.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/videobuf2-core.h>
  33. #include <media/videobuf2-dma-contig.h>
  34. #include <media/soc_camera.h>
  35. #include <media/soc_mediabus.h>
  36. #include <linux/videodev2.h>
  37. #include <mach/mx2_cam.h>
  38. #include <mach/hardware.h>
  39. #include <asm/dma.h>
  40. #define MX2_CAM_DRV_NAME "mx2-camera"
  41. #define MX2_CAM_VERSION "0.0.6"
  42. #define MX2_CAM_DRIVER_DESCRIPTION "i.MX2x_Camera"
  43. /* reset values */
  44. #define CSICR1_RESET_VAL 0x40000800
  45. #define CSICR2_RESET_VAL 0x0
  46. #define CSICR3_RESET_VAL 0x0
  47. /* csi control reg 1 */
  48. #define CSICR1_SWAP16_EN (1 << 31)
  49. #define CSICR1_EXT_VSYNC (1 << 30)
  50. #define CSICR1_EOF_INTEN (1 << 29)
  51. #define CSICR1_PRP_IF_EN (1 << 28)
  52. #define CSICR1_CCIR_MODE (1 << 27)
  53. #define CSICR1_COF_INTEN (1 << 26)
  54. #define CSICR1_SF_OR_INTEN (1 << 25)
  55. #define CSICR1_RF_OR_INTEN (1 << 24)
  56. #define CSICR1_STATFF_LEVEL (3 << 22)
  57. #define CSICR1_STATFF_INTEN (1 << 21)
  58. #define CSICR1_RXFF_LEVEL(l) (((l) & 3) << 19) /* MX27 */
  59. #define CSICR1_FB2_DMA_INTEN (1 << 20) /* MX25 */
  60. #define CSICR1_FB1_DMA_INTEN (1 << 19) /* MX25 */
  61. #define CSICR1_RXFF_INTEN (1 << 18)
  62. #define CSICR1_SOF_POL (1 << 17)
  63. #define CSICR1_SOF_INTEN (1 << 16)
  64. #define CSICR1_MCLKDIV(d) (((d) & 0xF) << 12)
  65. #define CSICR1_HSYNC_POL (1 << 11)
  66. #define CSICR1_CCIR_EN (1 << 10)
  67. #define CSICR1_MCLKEN (1 << 9)
  68. #define CSICR1_FCC (1 << 8)
  69. #define CSICR1_PACK_DIR (1 << 7)
  70. #define CSICR1_CLR_STATFIFO (1 << 6)
  71. #define CSICR1_CLR_RXFIFO (1 << 5)
  72. #define CSICR1_GCLK_MODE (1 << 4)
  73. #define CSICR1_INV_DATA (1 << 3)
  74. #define CSICR1_INV_PCLK (1 << 2)
  75. #define CSICR1_REDGE (1 << 1)
  76. #define SHIFT_STATFF_LEVEL 22
  77. #define SHIFT_RXFF_LEVEL 19
  78. #define SHIFT_MCLKDIV 12
  79. /* control reg 3 */
  80. #define CSICR3_FRMCNT (0xFFFF << 16)
  81. #define CSICR3_FRMCNT_RST (1 << 15)
  82. #define CSICR3_DMA_REFLASH_RFF (1 << 14)
  83. #define CSICR3_DMA_REFLASH_SFF (1 << 13)
  84. #define CSICR3_DMA_REQ_EN_RFF (1 << 12)
  85. #define CSICR3_DMA_REQ_EN_SFF (1 << 11)
  86. #define CSICR3_RXFF_LEVEL(l) (((l) & 7) << 4) /* MX25 */
  87. #define CSICR3_CSI_SUP (1 << 3)
  88. #define CSICR3_ZERO_PACK_EN (1 << 2)
  89. #define CSICR3_ECC_INT_EN (1 << 1)
  90. #define CSICR3_ECC_AUTO_EN (1 << 0)
  91. #define SHIFT_FRMCNT 16
  92. /* csi status reg */
  93. #define CSISR_SFF_OR_INT (1 << 25)
  94. #define CSISR_RFF_OR_INT (1 << 24)
  95. #define CSISR_STATFF_INT (1 << 21)
  96. #define CSISR_DMA_TSF_FB2_INT (1 << 20) /* MX25 */
  97. #define CSISR_DMA_TSF_FB1_INT (1 << 19) /* MX25 */
  98. #define CSISR_RXFF_INT (1 << 18)
  99. #define CSISR_EOF_INT (1 << 17)
  100. #define CSISR_SOF_INT (1 << 16)
  101. #define CSISR_F2_INT (1 << 15)
  102. #define CSISR_F1_INT (1 << 14)
  103. #define CSISR_COF_INT (1 << 13)
  104. #define CSISR_ECC_INT (1 << 1)
  105. #define CSISR_DRDY (1 << 0)
  106. #define CSICR1 0x00
  107. #define CSICR2 0x04
  108. #define CSISR (cpu_is_mx27() ? 0x08 : 0x18)
  109. #define CSISTATFIFO 0x0c
  110. #define CSIRFIFO 0x10
  111. #define CSIRXCNT 0x14
  112. #define CSICR3 (cpu_is_mx27() ? 0x1C : 0x08)
  113. #define CSIDMASA_STATFIFO 0x20
  114. #define CSIDMATA_STATFIFO 0x24
  115. #define CSIDMASA_FB1 0x28
  116. #define CSIDMASA_FB2 0x2c
  117. #define CSIFBUF_PARA 0x30
  118. #define CSIIMAG_PARA 0x34
  119. /* EMMA PrP */
  120. #define PRP_CNTL 0x00
  121. #define PRP_INTR_CNTL 0x04
  122. #define PRP_INTRSTATUS 0x08
  123. #define PRP_SOURCE_Y_PTR 0x0c
  124. #define PRP_SOURCE_CB_PTR 0x10
  125. #define PRP_SOURCE_CR_PTR 0x14
  126. #define PRP_DEST_RGB1_PTR 0x18
  127. #define PRP_DEST_RGB2_PTR 0x1c
  128. #define PRP_DEST_Y_PTR 0x20
  129. #define PRP_DEST_CB_PTR 0x24
  130. #define PRP_DEST_CR_PTR 0x28
  131. #define PRP_SRC_FRAME_SIZE 0x2c
  132. #define PRP_DEST_CH1_LINE_STRIDE 0x30
  133. #define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
  134. #define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
  135. #define PRP_CH1_OUT_IMAGE_SIZE 0x3c
  136. #define PRP_CH2_OUT_IMAGE_SIZE 0x40
  137. #define PRP_SRC_LINE_STRIDE 0x44
  138. #define PRP_CSC_COEF_012 0x48
  139. #define PRP_CSC_COEF_345 0x4c
  140. #define PRP_CSC_COEF_678 0x50
  141. #define PRP_CH1_RZ_HORI_COEF1 0x54
  142. #define PRP_CH1_RZ_HORI_COEF2 0x58
  143. #define PRP_CH1_RZ_HORI_VALID 0x5c
  144. #define PRP_CH1_RZ_VERT_COEF1 0x60
  145. #define PRP_CH1_RZ_VERT_COEF2 0x64
  146. #define PRP_CH1_RZ_VERT_VALID 0x68
  147. #define PRP_CH2_RZ_HORI_COEF1 0x6c
  148. #define PRP_CH2_RZ_HORI_COEF2 0x70
  149. #define PRP_CH2_RZ_HORI_VALID 0x74
  150. #define PRP_CH2_RZ_VERT_COEF1 0x78
  151. #define PRP_CH2_RZ_VERT_COEF2 0x7c
  152. #define PRP_CH2_RZ_VERT_VALID 0x80
  153. #define PRP_CNTL_CH1EN (1 << 0)
  154. #define PRP_CNTL_CH2EN (1 << 1)
  155. #define PRP_CNTL_CSIEN (1 << 2)
  156. #define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
  157. #define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
  158. #define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
  159. #define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
  160. #define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
  161. #define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
  162. #define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
  163. #define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
  164. #define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
  165. #define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
  166. #define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
  167. #define PRP_CNTL_CH1_LEN (1 << 9)
  168. #define PRP_CNTL_CH2_LEN (1 << 10)
  169. #define PRP_CNTL_SKIP_FRAME (1 << 11)
  170. #define PRP_CNTL_SWRST (1 << 12)
  171. #define PRP_CNTL_CLKEN (1 << 13)
  172. #define PRP_CNTL_WEN (1 << 14)
  173. #define PRP_CNTL_CH1BYP (1 << 15)
  174. #define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
  175. #define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
  176. #define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
  177. #define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
  178. #define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
  179. #define PRP_CNTL_CH2B1EN (1 << 29)
  180. #define PRP_CNTL_CH2B2EN (1 << 30)
  181. #define PRP_CNTL_CH2FEN (1 << 31)
  182. /* IRQ Enable and status register */
  183. #define PRP_INTR_RDERR (1 << 0)
  184. #define PRP_INTR_CH1WERR (1 << 1)
  185. #define PRP_INTR_CH2WERR (1 << 2)
  186. #define PRP_INTR_CH1FC (1 << 3)
  187. #define PRP_INTR_CH2FC (1 << 5)
  188. #define PRP_INTR_LBOVF (1 << 7)
  189. #define PRP_INTR_CH2OVF (1 << 8)
  190. #define MAX_VIDEO_MEM 16
  191. struct mx2_prp_cfg {
  192. int channel;
  193. u32 in_fmt;
  194. u32 out_fmt;
  195. u32 src_pixel;
  196. u32 ch1_pixel;
  197. u32 irq_flags;
  198. };
  199. /* prp configuration for a client-host fmt pair */
  200. struct mx2_fmt_cfg {
  201. enum v4l2_mbus_pixelcode in_fmt;
  202. u32 out_fmt;
  203. struct mx2_prp_cfg cfg;
  204. };
  205. enum mx2_buffer_state {
  206. MX2_STATE_QUEUED,
  207. MX2_STATE_ACTIVE,
  208. MX2_STATE_DONE,
  209. };
  210. struct mx2_buf_internal {
  211. struct list_head queue;
  212. int bufnum;
  213. bool discard;
  214. };
  215. /* buffer for one video frame */
  216. struct mx2_buffer {
  217. /* common v4l buffer stuff -- must be first */
  218. struct vb2_buffer vb;
  219. enum mx2_buffer_state state;
  220. struct mx2_buf_internal internal;
  221. };
  222. struct mx2_camera_dev {
  223. struct device *dev;
  224. struct soc_camera_host soc_host;
  225. struct soc_camera_device *icd;
  226. struct clk *clk_csi, *clk_emma;
  227. unsigned int irq_csi, irq_emma;
  228. void __iomem *base_csi, *base_emma;
  229. unsigned long base_dma;
  230. struct mx2_camera_platform_data *pdata;
  231. struct resource *res_csi, *res_emma;
  232. unsigned long platform_flags;
  233. struct list_head capture;
  234. struct list_head active_bufs;
  235. struct list_head discard;
  236. spinlock_t lock;
  237. int dma;
  238. struct mx2_buffer *active;
  239. struct mx2_buffer *fb1_active;
  240. struct mx2_buffer *fb2_active;
  241. u32 csicr1;
  242. struct mx2_buf_internal buf_discard[2];
  243. void *discard_buffer;
  244. dma_addr_t discard_buffer_dma;
  245. size_t discard_size;
  246. struct mx2_fmt_cfg *emma_prp;
  247. u32 frame_count;
  248. struct vb2_alloc_ctx *alloc_ctx;
  249. };
  250. static struct mx2_buffer *mx2_ibuf_to_buf(struct mx2_buf_internal *int_buf)
  251. {
  252. return container_of(int_buf, struct mx2_buffer, internal);
  253. }
  254. static struct mx2_fmt_cfg mx27_emma_prp_table[] = {
  255. /*
  256. * This is a generic configuration which is valid for most
  257. * prp input-output format combinations.
  258. * We set the incomming and outgoing pixelformat to a
  259. * 16 Bit wide format and adjust the bytesperline
  260. * accordingly. With this configuration the inputdata
  261. * will not be changed by the emma and could be any type
  262. * of 16 Bit Pixelformat.
  263. */
  264. {
  265. .in_fmt = 0,
  266. .out_fmt = 0,
  267. .cfg = {
  268. .channel = 1,
  269. .in_fmt = PRP_CNTL_DATA_IN_RGB16,
  270. .out_fmt = PRP_CNTL_CH1_OUT_RGB16,
  271. .src_pixel = 0x2ca00565, /* RGB565 */
  272. .ch1_pixel = 0x2ca00565, /* RGB565 */
  273. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
  274. PRP_INTR_CH1FC | PRP_INTR_LBOVF,
  275. }
  276. },
  277. {
  278. .in_fmt = V4L2_MBUS_FMT_YUYV8_2X8,
  279. .out_fmt = V4L2_PIX_FMT_YUV420,
  280. .cfg = {
  281. .channel = 2,
  282. .in_fmt = PRP_CNTL_DATA_IN_YUV422,
  283. .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
  284. .src_pixel = 0x22000888, /* YUV422 (YUYV) */
  285. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
  286. PRP_INTR_CH2FC | PRP_INTR_LBOVF |
  287. PRP_INTR_CH2OVF,
  288. }
  289. },
  290. };
  291. static struct mx2_fmt_cfg *mx27_emma_prp_get_format(
  292. enum v4l2_mbus_pixelcode in_fmt,
  293. u32 out_fmt)
  294. {
  295. int i;
  296. for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++)
  297. if ((mx27_emma_prp_table[i].in_fmt == in_fmt) &&
  298. (mx27_emma_prp_table[i].out_fmt == out_fmt)) {
  299. return &mx27_emma_prp_table[i];
  300. }
  301. /* If no match return the most generic configuration */
  302. return &mx27_emma_prp_table[0];
  303. };
  304. static void mx27_update_emma_buf(struct mx2_camera_dev *pcdev,
  305. unsigned long phys, int bufnum)
  306. {
  307. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  308. if (prp->cfg.channel == 1) {
  309. writel(phys, pcdev->base_emma +
  310. PRP_DEST_RGB1_PTR + 4 * bufnum);
  311. } else {
  312. writel(phys, pcdev->base_emma +
  313. PRP_DEST_Y_PTR - 0x14 * bufnum);
  314. if (prp->out_fmt == V4L2_PIX_FMT_YUV420) {
  315. u32 imgsize = pcdev->icd->user_height *
  316. pcdev->icd->user_width;
  317. writel(phys + imgsize, pcdev->base_emma +
  318. PRP_DEST_CB_PTR - 0x14 * bufnum);
  319. writel(phys + ((5 * imgsize) / 4), pcdev->base_emma +
  320. PRP_DEST_CR_PTR - 0x14 * bufnum);
  321. }
  322. }
  323. }
  324. static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
  325. {
  326. unsigned long flags;
  327. clk_disable(pcdev->clk_csi);
  328. writel(0, pcdev->base_csi + CSICR1);
  329. if (cpu_is_mx27()) {
  330. writel(0, pcdev->base_emma + PRP_CNTL);
  331. } else if (cpu_is_mx25()) {
  332. spin_lock_irqsave(&pcdev->lock, flags);
  333. pcdev->fb1_active = NULL;
  334. pcdev->fb2_active = NULL;
  335. writel(0, pcdev->base_csi + CSIDMASA_FB1);
  336. writel(0, pcdev->base_csi + CSIDMASA_FB2);
  337. spin_unlock_irqrestore(&pcdev->lock, flags);
  338. }
  339. }
  340. /*
  341. * The following two functions absolutely depend on the fact, that
  342. * there can be only one camera on mx2 camera sensor interface
  343. */
  344. static int mx2_camera_add_device(struct soc_camera_device *icd)
  345. {
  346. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  347. struct mx2_camera_dev *pcdev = ici->priv;
  348. int ret;
  349. u32 csicr1;
  350. if (pcdev->icd)
  351. return -EBUSY;
  352. ret = clk_enable(pcdev->clk_csi);
  353. if (ret < 0)
  354. return ret;
  355. csicr1 = CSICR1_MCLKEN;
  356. if (cpu_is_mx27()) {
  357. csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC |
  358. CSICR1_RXFF_LEVEL(0);
  359. } else if (cpu_is_mx27())
  360. csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2);
  361. pcdev->csicr1 = csicr1;
  362. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  363. pcdev->icd = icd;
  364. pcdev->frame_count = 0;
  365. dev_info(icd->parent, "Camera driver attached to camera %d\n",
  366. icd->devnum);
  367. return 0;
  368. }
  369. static void mx2_camera_remove_device(struct soc_camera_device *icd)
  370. {
  371. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  372. struct mx2_camera_dev *pcdev = ici->priv;
  373. BUG_ON(icd != pcdev->icd);
  374. dev_info(icd->parent, "Camera driver detached from camera %d\n",
  375. icd->devnum);
  376. mx2_camera_deactivate(pcdev);
  377. pcdev->icd = NULL;
  378. }
  379. static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb,
  380. int state)
  381. {
  382. struct vb2_buffer *vb;
  383. struct mx2_buffer *buf;
  384. struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active :
  385. &pcdev->fb2_active;
  386. u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2;
  387. unsigned long flags;
  388. spin_lock_irqsave(&pcdev->lock, flags);
  389. if (*fb_active == NULL)
  390. goto out;
  391. vb = &(*fb_active)->vb;
  392. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  393. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  394. do_gettimeofday(&vb->v4l2_buf.timestamp);
  395. vb->v4l2_buf.sequence++;
  396. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  397. if (list_empty(&pcdev->capture)) {
  398. buf = NULL;
  399. writel(0, pcdev->base_csi + fb_reg);
  400. } else {
  401. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  402. internal.queue);
  403. vb = &buf->vb;
  404. list_del(&buf->internal.queue);
  405. buf->state = MX2_STATE_ACTIVE;
  406. writel(vb2_dma_contig_plane_dma_addr(vb, 0),
  407. pcdev->base_csi + fb_reg);
  408. }
  409. *fb_active = buf;
  410. out:
  411. spin_unlock_irqrestore(&pcdev->lock, flags);
  412. }
  413. static irqreturn_t mx25_camera_irq(int irq_csi, void *data)
  414. {
  415. struct mx2_camera_dev *pcdev = data;
  416. u32 status = readl(pcdev->base_csi + CSISR);
  417. if (status & CSISR_DMA_TSF_FB1_INT)
  418. mx25_camera_frame_done(pcdev, 1, MX2_STATE_DONE);
  419. else if (status & CSISR_DMA_TSF_FB2_INT)
  420. mx25_camera_frame_done(pcdev, 2, MX2_STATE_DONE);
  421. /* FIXME: handle CSISR_RFF_OR_INT */
  422. writel(status, pcdev->base_csi + CSISR);
  423. return IRQ_HANDLED;
  424. }
  425. /*
  426. * Videobuf operations
  427. */
  428. static int mx2_videobuf_setup(struct vb2_queue *vq,
  429. const struct v4l2_format *fmt,
  430. unsigned int *count, unsigned int *num_planes,
  431. unsigned int sizes[], void *alloc_ctxs[])
  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 mx2_camera_dev *pcdev = ici->priv;
  436. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  437. icd->current_fmt->host_fmt);
  438. dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]);
  439. /* TODO: support for VIDIOC_CREATE_BUFS not ready */
  440. if (fmt != NULL)
  441. return -ENOTTY;
  442. if (bytes_per_line < 0)
  443. return bytes_per_line;
  444. alloc_ctxs[0] = pcdev->alloc_ctx;
  445. sizes[0] = bytes_per_line * icd->user_height;
  446. if (0 == *count)
  447. *count = 32;
  448. if (!*num_planes &&
  449. sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
  450. *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0];
  451. *num_planes = 1;
  452. return 0;
  453. }
  454. static int mx2_videobuf_prepare(struct vb2_buffer *vb)
  455. {
  456. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  457. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  458. icd->current_fmt->host_fmt);
  459. int ret = 0;
  460. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  461. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  462. if (bytes_per_line < 0)
  463. return bytes_per_line;
  464. #ifdef DEBUG
  465. /*
  466. * This can be useful if you want to see if we actually fill
  467. * the buffer with something
  468. */
  469. memset((void *)vb2_plane_vaddr(vb, 0),
  470. 0xaa, vb2_get_plane_payload(vb, 0));
  471. #endif
  472. vb2_set_plane_payload(vb, 0, bytes_per_line * icd->user_height);
  473. if (vb2_plane_vaddr(vb, 0) &&
  474. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  475. ret = -EINVAL;
  476. goto out;
  477. }
  478. return 0;
  479. out:
  480. return ret;
  481. }
  482. static void mx2_videobuf_queue(struct vb2_buffer *vb)
  483. {
  484. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  485. struct soc_camera_host *ici =
  486. to_soc_camera_host(icd->parent);
  487. struct mx2_camera_dev *pcdev = ici->priv;
  488. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  489. unsigned long flags;
  490. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  491. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  492. spin_lock_irqsave(&pcdev->lock, flags);
  493. buf->state = MX2_STATE_QUEUED;
  494. list_add_tail(&buf->internal.queue, &pcdev->capture);
  495. if (cpu_is_mx25()) {
  496. u32 csicr3, dma_inten = 0;
  497. if (pcdev->fb1_active == NULL) {
  498. writel(vb2_dma_contig_plane_dma_addr(vb, 0),
  499. pcdev->base_csi + CSIDMASA_FB1);
  500. pcdev->fb1_active = buf;
  501. dma_inten = CSICR1_FB1_DMA_INTEN;
  502. } else if (pcdev->fb2_active == NULL) {
  503. writel(vb2_dma_contig_plane_dma_addr(vb, 0),
  504. pcdev->base_csi + CSIDMASA_FB2);
  505. pcdev->fb2_active = buf;
  506. dma_inten = CSICR1_FB2_DMA_INTEN;
  507. }
  508. if (dma_inten) {
  509. list_del(&buf->internal.queue);
  510. buf->state = MX2_STATE_ACTIVE;
  511. csicr3 = readl(pcdev->base_csi + CSICR3);
  512. /* Reflash DMA */
  513. writel(csicr3 | CSICR3_DMA_REFLASH_RFF,
  514. pcdev->base_csi + CSICR3);
  515. /* clear & enable interrupts */
  516. writel(dma_inten, pcdev->base_csi + CSISR);
  517. pcdev->csicr1 |= dma_inten;
  518. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  519. /* enable DMA */
  520. csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1);
  521. writel(csicr3, pcdev->base_csi + CSICR3);
  522. }
  523. }
  524. spin_unlock_irqrestore(&pcdev->lock, flags);
  525. }
  526. static void mx2_videobuf_release(struct vb2_buffer *vb)
  527. {
  528. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  529. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  530. struct mx2_camera_dev *pcdev = ici->priv;
  531. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  532. unsigned long flags;
  533. #ifdef DEBUG
  534. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  535. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  536. switch (buf->state) {
  537. case MX2_STATE_ACTIVE:
  538. dev_info(icd->parent, "%s (active)\n", __func__);
  539. break;
  540. case MX2_STATE_QUEUED:
  541. dev_info(icd->parent, "%s (queued)\n", __func__);
  542. break;
  543. default:
  544. dev_info(icd->parent, "%s (unknown) %d\n", __func__,
  545. buf->state);
  546. break;
  547. }
  548. #endif
  549. /*
  550. * Terminate only queued but inactive buffers. Active buffers are
  551. * released when they become inactive after videobuf_waiton().
  552. *
  553. * FIXME: implement forced termination of active buffers for mx27 and
  554. * mx27 eMMA, so that the user won't get stuck in an uninterruptible
  555. * state. This requires a specific handling for each of the these DMA
  556. * types.
  557. */
  558. spin_lock_irqsave(&pcdev->lock, flags);
  559. if (cpu_is_mx25() && buf->state == MX2_STATE_ACTIVE) {
  560. if (pcdev->fb1_active == buf) {
  561. pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN;
  562. writel(0, pcdev->base_csi + CSIDMASA_FB1);
  563. pcdev->fb1_active = NULL;
  564. } else if (pcdev->fb2_active == buf) {
  565. pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN;
  566. writel(0, pcdev->base_csi + CSIDMASA_FB2);
  567. pcdev->fb2_active = NULL;
  568. }
  569. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  570. }
  571. spin_unlock_irqrestore(&pcdev->lock, flags);
  572. }
  573. static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
  574. int bytesperline)
  575. {
  576. struct soc_camera_host *ici =
  577. to_soc_camera_host(icd->parent);
  578. struct mx2_camera_dev *pcdev = ici->priv;
  579. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  580. writel((icd->user_width << 16) | icd->user_height,
  581. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  582. writel(prp->cfg.src_pixel,
  583. pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
  584. if (prp->cfg.channel == 1) {
  585. writel((icd->user_width << 16) | icd->user_height,
  586. pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
  587. writel(bytesperline,
  588. pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
  589. writel(prp->cfg.ch1_pixel,
  590. pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
  591. } else { /* channel 2 */
  592. writel((icd->user_width << 16) | icd->user_height,
  593. pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
  594. }
  595. /* Enable interrupts */
  596. writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
  597. }
  598. static int mx2_start_streaming(struct vb2_queue *q, unsigned int count)
  599. {
  600. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  601. struct soc_camera_host *ici =
  602. to_soc_camera_host(icd->parent);
  603. struct mx2_camera_dev *pcdev = ici->priv;
  604. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  605. struct vb2_buffer *vb;
  606. struct mx2_buffer *buf;
  607. unsigned long phys;
  608. int bytesperline;
  609. if (cpu_is_mx27()) {
  610. unsigned long flags;
  611. if (count < 2)
  612. return -EINVAL;
  613. spin_lock_irqsave(&pcdev->lock, flags);
  614. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  615. internal.queue);
  616. buf->internal.bufnum = 0;
  617. vb = &buf->vb;
  618. buf->state = MX2_STATE_ACTIVE;
  619. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  620. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  621. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  622. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  623. internal.queue);
  624. buf->internal.bufnum = 1;
  625. vb = &buf->vb;
  626. buf->state = MX2_STATE_ACTIVE;
  627. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  628. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  629. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  630. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  631. icd->current_fmt->host_fmt);
  632. if (bytesperline < 0)
  633. return bytesperline;
  634. /*
  635. * I didn't manage to properly enable/disable the prp
  636. * on a per frame basis during running transfers,
  637. * thus we allocate a buffer here and use it to
  638. * discard frames when no buffer is available.
  639. * Feel free to work on this ;)
  640. */
  641. pcdev->discard_size = icd->user_height * bytesperline;
  642. pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
  643. pcdev->discard_size, &pcdev->discard_buffer_dma,
  644. GFP_KERNEL);
  645. if (!pcdev->discard_buffer)
  646. return -ENOMEM;
  647. pcdev->buf_discard[0].discard = true;
  648. list_add_tail(&pcdev->buf_discard[0].queue,
  649. &pcdev->discard);
  650. pcdev->buf_discard[1].discard = true;
  651. list_add_tail(&pcdev->buf_discard[1].queue,
  652. &pcdev->discard);
  653. mx27_camera_emma_buf_init(icd, bytesperline);
  654. if (prp->cfg.channel == 1) {
  655. writel(PRP_CNTL_CH1EN |
  656. PRP_CNTL_CSIEN |
  657. prp->cfg.in_fmt |
  658. prp->cfg.out_fmt |
  659. PRP_CNTL_CH1_LEN |
  660. PRP_CNTL_CH1BYP |
  661. PRP_CNTL_CH1_TSKIP(0) |
  662. PRP_CNTL_IN_TSKIP(0),
  663. pcdev->base_emma + PRP_CNTL);
  664. } else {
  665. writel(PRP_CNTL_CH2EN |
  666. PRP_CNTL_CSIEN |
  667. prp->cfg.in_fmt |
  668. prp->cfg.out_fmt |
  669. PRP_CNTL_CH2_LEN |
  670. PRP_CNTL_CH2_TSKIP(0) |
  671. PRP_CNTL_IN_TSKIP(0),
  672. pcdev->base_emma + PRP_CNTL);
  673. }
  674. spin_unlock_irqrestore(&pcdev->lock, flags);
  675. }
  676. return 0;
  677. }
  678. static int mx2_stop_streaming(struct vb2_queue *q)
  679. {
  680. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  681. struct soc_camera_host *ici =
  682. to_soc_camera_host(icd->parent);
  683. struct mx2_camera_dev *pcdev = ici->priv;
  684. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  685. unsigned long flags;
  686. void *b;
  687. u32 cntl;
  688. if (cpu_is_mx27()) {
  689. spin_lock_irqsave(&pcdev->lock, flags);
  690. cntl = readl(pcdev->base_emma + PRP_CNTL);
  691. if (prp->cfg.channel == 1) {
  692. writel(cntl & ~PRP_CNTL_CH1EN,
  693. pcdev->base_emma + PRP_CNTL);
  694. } else {
  695. writel(cntl & ~PRP_CNTL_CH2EN,
  696. pcdev->base_emma + PRP_CNTL);
  697. }
  698. INIT_LIST_HEAD(&pcdev->capture);
  699. INIT_LIST_HEAD(&pcdev->active_bufs);
  700. INIT_LIST_HEAD(&pcdev->discard);
  701. b = pcdev->discard_buffer;
  702. pcdev->discard_buffer = NULL;
  703. spin_unlock_irqrestore(&pcdev->lock, flags);
  704. dma_free_coherent(ici->v4l2_dev.dev,
  705. pcdev->discard_size, b, pcdev->discard_buffer_dma);
  706. }
  707. return 0;
  708. }
  709. static struct vb2_ops mx2_videobuf_ops = {
  710. .queue_setup = mx2_videobuf_setup,
  711. .buf_prepare = mx2_videobuf_prepare,
  712. .buf_queue = mx2_videobuf_queue,
  713. .buf_cleanup = mx2_videobuf_release,
  714. .start_streaming = mx2_start_streaming,
  715. .stop_streaming = mx2_stop_streaming,
  716. };
  717. static int mx2_camera_init_videobuf(struct vb2_queue *q,
  718. struct soc_camera_device *icd)
  719. {
  720. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  721. q->io_modes = VB2_MMAP | VB2_USERPTR;
  722. q->drv_priv = icd;
  723. q->ops = &mx2_videobuf_ops;
  724. q->mem_ops = &vb2_dma_contig_memops;
  725. q->buf_struct_size = sizeof(struct mx2_buffer);
  726. return vb2_queue_init(q);
  727. }
  728. #define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \
  729. V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
  730. V4L2_MBUS_VSYNC_ACTIVE_LOW | \
  731. V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
  732. V4L2_MBUS_HSYNC_ACTIVE_LOW | \
  733. V4L2_MBUS_PCLK_SAMPLE_RISING | \
  734. V4L2_MBUS_PCLK_SAMPLE_FALLING | \
  735. V4L2_MBUS_DATA_ACTIVE_HIGH | \
  736. V4L2_MBUS_DATA_ACTIVE_LOW)
  737. static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
  738. {
  739. u32 cntl;
  740. int count = 0;
  741. cntl = readl(pcdev->base_emma + PRP_CNTL);
  742. writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
  743. while (count++ < 100) {
  744. if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
  745. return 0;
  746. barrier();
  747. udelay(1);
  748. }
  749. return -ETIMEDOUT;
  750. }
  751. static int mx2_camera_set_bus_param(struct soc_camera_device *icd)
  752. {
  753. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  754. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  755. struct mx2_camera_dev *pcdev = ici->priv;
  756. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  757. unsigned long common_flags;
  758. int ret;
  759. int bytesperline;
  760. u32 csicr1 = pcdev->csicr1;
  761. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  762. if (!ret) {
  763. common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS);
  764. if (!common_flags) {
  765. dev_warn(icd->parent,
  766. "Flags incompatible: camera 0x%x, host 0x%x\n",
  767. cfg.flags, MX2_BUS_FLAGS);
  768. return -EINVAL;
  769. }
  770. } else if (ret != -ENOIOCTLCMD) {
  771. return ret;
  772. } else {
  773. common_flags = MX2_BUS_FLAGS;
  774. }
  775. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  776. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  777. if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
  778. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  779. else
  780. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  781. }
  782. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  783. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  784. if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
  785. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  786. else
  787. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  788. }
  789. cfg.flags = common_flags;
  790. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  791. if (ret < 0 && ret != -ENOIOCTLCMD) {
  792. dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
  793. common_flags, ret);
  794. return ret;
  795. }
  796. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  797. csicr1 |= CSICR1_REDGE;
  798. if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  799. csicr1 |= CSICR1_SOF_POL;
  800. if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  801. csicr1 |= CSICR1_HSYNC_POL;
  802. if (pcdev->platform_flags & MX2_CAMERA_SWAP16)
  803. csicr1 |= CSICR1_SWAP16_EN;
  804. if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
  805. csicr1 |= CSICR1_EXT_VSYNC;
  806. if (pcdev->platform_flags & MX2_CAMERA_CCIR)
  807. csicr1 |= CSICR1_CCIR_EN;
  808. if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
  809. csicr1 |= CSICR1_CCIR_MODE;
  810. if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
  811. csicr1 |= CSICR1_GCLK_MODE;
  812. if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
  813. csicr1 |= CSICR1_INV_DATA;
  814. if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB)
  815. csicr1 |= CSICR1_PACK_DIR;
  816. pcdev->csicr1 = csicr1;
  817. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  818. icd->current_fmt->host_fmt);
  819. if (bytesperline < 0)
  820. return bytesperline;
  821. if (cpu_is_mx27()) {
  822. ret = mx27_camera_emma_prp_reset(pcdev);
  823. if (ret)
  824. return ret;
  825. } else if (cpu_is_mx25()) {
  826. writel((bytesperline * icd->user_height) >> 2,
  827. pcdev->base_csi + CSIRXCNT);
  828. writel((bytesperline << 16) | icd->user_height,
  829. pcdev->base_csi + CSIIMAG_PARA);
  830. }
  831. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  832. return 0;
  833. }
  834. static int mx2_camera_set_crop(struct soc_camera_device *icd,
  835. struct v4l2_crop *a)
  836. {
  837. struct v4l2_rect *rect = &a->c;
  838. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  839. struct v4l2_mbus_framefmt mf;
  840. int ret;
  841. soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
  842. soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
  843. ret = v4l2_subdev_call(sd, video, s_crop, a);
  844. if (ret < 0)
  845. return ret;
  846. /* The capture device might have changed its output */
  847. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  848. if (ret < 0)
  849. return ret;
  850. dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
  851. mf.width, mf.height);
  852. icd->user_width = mf.width;
  853. icd->user_height = mf.height;
  854. return ret;
  855. }
  856. static int mx2_camera_get_formats(struct soc_camera_device *icd,
  857. unsigned int idx,
  858. struct soc_camera_format_xlate *xlate)
  859. {
  860. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  861. const struct soc_mbus_pixelfmt *fmt;
  862. struct device *dev = icd->parent;
  863. enum v4l2_mbus_pixelcode code;
  864. int ret, formats = 0;
  865. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  866. if (ret < 0)
  867. /* no more formats */
  868. return 0;
  869. fmt = soc_mbus_get_fmtdesc(code);
  870. if (!fmt) {
  871. dev_err(dev, "Invalid format code #%u: %d\n", idx, code);
  872. return 0;
  873. }
  874. if (code == V4L2_MBUS_FMT_YUYV8_2X8) {
  875. formats++;
  876. if (xlate) {
  877. /*
  878. * CH2 can output YUV420 which is a standard format in
  879. * soc_mediabus.c
  880. */
  881. xlate->host_fmt =
  882. soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8);
  883. xlate->code = code;
  884. dev_dbg(dev, "Providing host format %s for sensor code %d\n",
  885. xlate->host_fmt->name, code);
  886. xlate++;
  887. }
  888. }
  889. /* Generic pass-trough */
  890. formats++;
  891. if (xlate) {
  892. xlate->host_fmt = fmt;
  893. xlate->code = code;
  894. xlate++;
  895. }
  896. return formats;
  897. }
  898. static int mx2_camera_set_fmt(struct soc_camera_device *icd,
  899. struct v4l2_format *f)
  900. {
  901. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  902. struct mx2_camera_dev *pcdev = ici->priv;
  903. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  904. const struct soc_camera_format_xlate *xlate;
  905. struct v4l2_pix_format *pix = &f->fmt.pix;
  906. struct v4l2_mbus_framefmt mf;
  907. int ret;
  908. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  909. if (!xlate) {
  910. dev_warn(icd->parent, "Format %x not found\n",
  911. pix->pixelformat);
  912. return -EINVAL;
  913. }
  914. mf.width = pix->width;
  915. mf.height = pix->height;
  916. mf.field = pix->field;
  917. mf.colorspace = pix->colorspace;
  918. mf.code = xlate->code;
  919. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  920. if (ret < 0 && ret != -ENOIOCTLCMD)
  921. return ret;
  922. if (mf.code != xlate->code)
  923. return -EINVAL;
  924. pix->width = mf.width;
  925. pix->height = mf.height;
  926. pix->field = mf.field;
  927. pix->colorspace = mf.colorspace;
  928. icd->current_fmt = xlate;
  929. if (cpu_is_mx27())
  930. pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
  931. xlate->host_fmt->fourcc);
  932. return 0;
  933. }
  934. static int mx2_camera_try_fmt(struct soc_camera_device *icd,
  935. struct v4l2_format *f)
  936. {
  937. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  938. const struct soc_camera_format_xlate *xlate;
  939. struct v4l2_pix_format *pix = &f->fmt.pix;
  940. struct v4l2_mbus_framefmt mf;
  941. __u32 pixfmt = pix->pixelformat;
  942. unsigned int width_limit;
  943. int ret;
  944. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  945. if (pixfmt && !xlate) {
  946. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  947. return -EINVAL;
  948. }
  949. /* FIXME: implement MX27 limits */
  950. /* limit to MX25 hardware capabilities */
  951. if (cpu_is_mx25()) {
  952. if (xlate->host_fmt->bits_per_sample <= 8)
  953. width_limit = 0xffff * 4;
  954. else
  955. width_limit = 0xffff * 2;
  956. /* CSIIMAG_PARA limit */
  957. if (pix->width > width_limit)
  958. pix->width = width_limit;
  959. if (pix->height > 0xffff)
  960. pix->height = 0xffff;
  961. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  962. xlate->host_fmt);
  963. if (pix->bytesperline < 0)
  964. return pix->bytesperline;
  965. pix->sizeimage = pix->height * pix->bytesperline;
  966. /* Check against the CSIRXCNT limit */
  967. if (pix->sizeimage > 4 * 0x3ffff) {
  968. /* Adjust geometry, preserve aspect ratio */
  969. unsigned int new_height = int_sqrt(4 * 0x3ffff *
  970. pix->height / pix->bytesperline);
  971. pix->width = new_height * pix->width / pix->height;
  972. pix->height = new_height;
  973. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  974. xlate->host_fmt);
  975. BUG_ON(pix->bytesperline < 0);
  976. }
  977. }
  978. /* limit to sensor capabilities */
  979. mf.width = pix->width;
  980. mf.height = pix->height;
  981. mf.field = pix->field;
  982. mf.colorspace = pix->colorspace;
  983. mf.code = xlate->code;
  984. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  985. if (ret < 0)
  986. return ret;
  987. if (mf.field == V4L2_FIELD_ANY)
  988. mf.field = V4L2_FIELD_NONE;
  989. /*
  990. * Driver supports interlaced images provided they have
  991. * both fields so that they can be processed as if they
  992. * were progressive.
  993. */
  994. if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) {
  995. dev_err(icd->parent, "Field type %d unsupported.\n",
  996. mf.field);
  997. return -EINVAL;
  998. }
  999. pix->width = mf.width;
  1000. pix->height = mf.height;
  1001. pix->field = mf.field;
  1002. pix->colorspace = mf.colorspace;
  1003. return 0;
  1004. }
  1005. static int mx2_camera_querycap(struct soc_camera_host *ici,
  1006. struct v4l2_capability *cap)
  1007. {
  1008. /* cap->name is set by the friendly caller:-> */
  1009. strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
  1010. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1011. return 0;
  1012. }
  1013. static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
  1014. {
  1015. struct soc_camera_device *icd = file->private_data;
  1016. return vb2_poll(&icd->vb2_vidq, file, pt);
  1017. }
  1018. static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
  1019. .owner = THIS_MODULE,
  1020. .add = mx2_camera_add_device,
  1021. .remove = mx2_camera_remove_device,
  1022. .set_fmt = mx2_camera_set_fmt,
  1023. .set_crop = mx2_camera_set_crop,
  1024. .get_formats = mx2_camera_get_formats,
  1025. .try_fmt = mx2_camera_try_fmt,
  1026. .init_videobuf2 = mx2_camera_init_videobuf,
  1027. .poll = mx2_camera_poll,
  1028. .querycap = mx2_camera_querycap,
  1029. .set_bus_param = mx2_camera_set_bus_param,
  1030. };
  1031. static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
  1032. int bufnum, bool err)
  1033. {
  1034. #ifdef DEBUG
  1035. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  1036. #endif
  1037. struct mx2_buf_internal *ibuf;
  1038. struct mx2_buffer *buf;
  1039. struct vb2_buffer *vb;
  1040. unsigned long phys;
  1041. ibuf = list_first_entry(&pcdev->active_bufs, struct mx2_buf_internal,
  1042. queue);
  1043. BUG_ON(ibuf->bufnum != bufnum);
  1044. if (ibuf->discard) {
  1045. /*
  1046. * Discard buffer must not be returned to user space.
  1047. * Just return it to the discard queue.
  1048. */
  1049. list_move_tail(pcdev->active_bufs.next, &pcdev->discard);
  1050. } else {
  1051. buf = mx2_ibuf_to_buf(ibuf);
  1052. vb = &buf->vb;
  1053. #ifdef DEBUG
  1054. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1055. if (prp->cfg.channel == 1) {
  1056. if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
  1057. 4 * bufnum) != phys) {
  1058. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1059. readl(pcdev->base_emma +
  1060. PRP_DEST_RGB1_PTR + 4 * bufnum));
  1061. }
  1062. } else {
  1063. if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
  1064. 0x14 * bufnum) != phys) {
  1065. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1066. readl(pcdev->base_emma +
  1067. PRP_DEST_Y_PTR - 0x14 * bufnum));
  1068. }
  1069. }
  1070. #endif
  1071. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb,
  1072. vb2_plane_vaddr(vb, 0),
  1073. vb2_get_plane_payload(vb, 0));
  1074. list_del_init(&buf->internal.queue);
  1075. do_gettimeofday(&vb->v4l2_buf.timestamp);
  1076. vb->v4l2_buf.sequence = pcdev->frame_count;
  1077. if (err)
  1078. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1079. else
  1080. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  1081. }
  1082. pcdev->frame_count++;
  1083. if (list_empty(&pcdev->capture)) {
  1084. if (list_empty(&pcdev->discard)) {
  1085. dev_warn(pcdev->dev, "%s: trying to access empty discard list\n",
  1086. __func__);
  1087. return;
  1088. }
  1089. ibuf = list_first_entry(&pcdev->discard,
  1090. struct mx2_buf_internal, queue);
  1091. ibuf->bufnum = bufnum;
  1092. list_move_tail(pcdev->discard.next, &pcdev->active_bufs);
  1093. mx27_update_emma_buf(pcdev, pcdev->discard_buffer_dma, bufnum);
  1094. return;
  1095. }
  1096. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  1097. internal.queue);
  1098. buf->internal.bufnum = bufnum;
  1099. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  1100. vb = &buf->vb;
  1101. buf->state = MX2_STATE_ACTIVE;
  1102. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1103. mx27_update_emma_buf(pcdev, phys, bufnum);
  1104. }
  1105. static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
  1106. {
  1107. struct mx2_camera_dev *pcdev = data;
  1108. unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
  1109. struct mx2_buf_internal *ibuf;
  1110. spin_lock(&pcdev->lock);
  1111. if (list_empty(&pcdev->active_bufs)) {
  1112. dev_warn(pcdev->dev, "%s: called while active list is empty\n",
  1113. __func__);
  1114. if (!status) {
  1115. spin_unlock(&pcdev->lock);
  1116. return IRQ_NONE;
  1117. }
  1118. }
  1119. if (status & (1 << 7)) { /* overflow */
  1120. u32 cntl = readl(pcdev->base_emma + PRP_CNTL);
  1121. writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
  1122. pcdev->base_emma + PRP_CNTL);
  1123. writel(cntl, pcdev->base_emma + PRP_CNTL);
  1124. ibuf = list_first_entry(&pcdev->active_bufs,
  1125. struct mx2_buf_internal, queue);
  1126. mx27_camera_frame_done_emma(pcdev,
  1127. ibuf->bufnum, true);
  1128. status &= ~(1 << 7);
  1129. } else if (((status & (3 << 5)) == (3 << 5)) ||
  1130. ((status & (3 << 3)) == (3 << 3))) {
  1131. /*
  1132. * Both buffers have triggered, process the one we're expecting
  1133. * to first
  1134. */
  1135. ibuf = list_first_entry(&pcdev->active_bufs,
  1136. struct mx2_buf_internal, queue);
  1137. mx27_camera_frame_done_emma(pcdev, ibuf->bufnum, false);
  1138. status &= ~(1 << (6 - ibuf->bufnum)); /* mark processed */
  1139. } else if ((status & (1 << 6)) || (status & (1 << 4))) {
  1140. mx27_camera_frame_done_emma(pcdev, 0, false);
  1141. } else if ((status & (1 << 5)) || (status & (1 << 3))) {
  1142. mx27_camera_frame_done_emma(pcdev, 1, false);
  1143. }
  1144. spin_unlock(&pcdev->lock);
  1145. writel(status, pcdev->base_emma + PRP_INTRSTATUS);
  1146. return IRQ_HANDLED;
  1147. }
  1148. static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
  1149. {
  1150. struct resource *res_emma = pcdev->res_emma;
  1151. int err = 0;
  1152. if (!request_mem_region(res_emma->start, resource_size(res_emma),
  1153. MX2_CAM_DRV_NAME)) {
  1154. err = -EBUSY;
  1155. goto out;
  1156. }
  1157. pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
  1158. if (!pcdev->base_emma) {
  1159. err = -ENOMEM;
  1160. goto exit_release;
  1161. }
  1162. err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
  1163. MX2_CAM_DRV_NAME, pcdev);
  1164. if (err) {
  1165. dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
  1166. goto exit_iounmap;
  1167. }
  1168. pcdev->clk_emma = clk_get(NULL, "emma");
  1169. if (IS_ERR(pcdev->clk_emma)) {
  1170. err = PTR_ERR(pcdev->clk_emma);
  1171. goto exit_free_irq;
  1172. }
  1173. clk_enable(pcdev->clk_emma);
  1174. err = mx27_camera_emma_prp_reset(pcdev);
  1175. if (err)
  1176. goto exit_clk_emma_put;
  1177. return err;
  1178. exit_clk_emma_put:
  1179. clk_disable(pcdev->clk_emma);
  1180. clk_put(pcdev->clk_emma);
  1181. exit_free_irq:
  1182. free_irq(pcdev->irq_emma, pcdev);
  1183. exit_iounmap:
  1184. iounmap(pcdev->base_emma);
  1185. exit_release:
  1186. release_mem_region(res_emma->start, resource_size(res_emma));
  1187. out:
  1188. return err;
  1189. }
  1190. static int __devinit mx2_camera_probe(struct platform_device *pdev)
  1191. {
  1192. struct mx2_camera_dev *pcdev;
  1193. struct resource *res_csi, *res_emma;
  1194. void __iomem *base_csi;
  1195. int irq_csi, irq_emma;
  1196. int err = 0;
  1197. dev_dbg(&pdev->dev, "initialising\n");
  1198. res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1199. irq_csi = platform_get_irq(pdev, 0);
  1200. if (res_csi == NULL || irq_csi < 0) {
  1201. dev_err(&pdev->dev, "Missing platform resources data\n");
  1202. err = -ENODEV;
  1203. goto exit;
  1204. }
  1205. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1206. if (!pcdev) {
  1207. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1208. err = -ENOMEM;
  1209. goto exit;
  1210. }
  1211. pcdev->clk_csi = clk_get(&pdev->dev, NULL);
  1212. if (IS_ERR(pcdev->clk_csi)) {
  1213. dev_err(&pdev->dev, "Could not get csi clock\n");
  1214. err = PTR_ERR(pcdev->clk_csi);
  1215. goto exit_kfree;
  1216. }
  1217. dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n",
  1218. clk_get_rate(pcdev->clk_csi));
  1219. pcdev->res_csi = res_csi;
  1220. pcdev->pdata = pdev->dev.platform_data;
  1221. if (pcdev->pdata) {
  1222. long rate;
  1223. pcdev->platform_flags = pcdev->pdata->flags;
  1224. rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
  1225. if (rate <= 0) {
  1226. err = -ENODEV;
  1227. goto exit_dma_free;
  1228. }
  1229. err = clk_set_rate(pcdev->clk_csi, rate);
  1230. if (err < 0)
  1231. goto exit_dma_free;
  1232. }
  1233. INIT_LIST_HEAD(&pcdev->capture);
  1234. INIT_LIST_HEAD(&pcdev->active_bufs);
  1235. INIT_LIST_HEAD(&pcdev->discard);
  1236. spin_lock_init(&pcdev->lock);
  1237. /*
  1238. * Request the regions.
  1239. */
  1240. if (!request_mem_region(res_csi->start, resource_size(res_csi),
  1241. MX2_CAM_DRV_NAME)) {
  1242. err = -EBUSY;
  1243. goto exit_dma_free;
  1244. }
  1245. base_csi = ioremap(res_csi->start, resource_size(res_csi));
  1246. if (!base_csi) {
  1247. err = -ENOMEM;
  1248. goto exit_release;
  1249. }
  1250. pcdev->irq_csi = irq_csi;
  1251. pcdev->base_csi = base_csi;
  1252. pcdev->base_dma = res_csi->start;
  1253. pcdev->dev = &pdev->dev;
  1254. if (cpu_is_mx25()) {
  1255. err = request_irq(pcdev->irq_csi, mx25_camera_irq, 0,
  1256. MX2_CAM_DRV_NAME, pcdev);
  1257. if (err) {
  1258. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  1259. goto exit_iounmap;
  1260. }
  1261. }
  1262. if (cpu_is_mx27()) {
  1263. /* EMMA support */
  1264. res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1265. irq_emma = platform_get_irq(pdev, 1);
  1266. if (!res_emma || !irq_emma) {
  1267. dev_err(&pdev->dev, "no EMMA resources\n");
  1268. goto exit_free_irq;
  1269. }
  1270. pcdev->res_emma = res_emma;
  1271. pcdev->irq_emma = irq_emma;
  1272. if (mx27_camera_emma_init(pcdev))
  1273. goto exit_free_irq;
  1274. }
  1275. pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
  1276. pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
  1277. pcdev->soc_host.priv = pcdev;
  1278. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1279. pcdev->soc_host.nr = pdev->id;
  1280. pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1281. if (IS_ERR(pcdev->alloc_ctx)) {
  1282. err = PTR_ERR(pcdev->alloc_ctx);
  1283. goto eallocctx;
  1284. }
  1285. err = soc_camera_host_register(&pcdev->soc_host);
  1286. if (err)
  1287. goto exit_free_emma;
  1288. dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
  1289. clk_get_rate(pcdev->clk_csi));
  1290. return 0;
  1291. exit_free_emma:
  1292. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1293. eallocctx:
  1294. if (cpu_is_mx27()) {
  1295. free_irq(pcdev->irq_emma, pcdev);
  1296. clk_disable(pcdev->clk_emma);
  1297. clk_put(pcdev->clk_emma);
  1298. iounmap(pcdev->base_emma);
  1299. release_mem_region(pcdev->res_emma->start, resource_size(pcdev->res_emma));
  1300. }
  1301. exit_free_irq:
  1302. if (cpu_is_mx25())
  1303. free_irq(pcdev->irq_csi, pcdev);
  1304. exit_iounmap:
  1305. iounmap(base_csi);
  1306. exit_release:
  1307. release_mem_region(res_csi->start, resource_size(res_csi));
  1308. exit_dma_free:
  1309. clk_put(pcdev->clk_csi);
  1310. exit_kfree:
  1311. kfree(pcdev);
  1312. exit:
  1313. return err;
  1314. }
  1315. static int __devexit mx2_camera_remove(struct platform_device *pdev)
  1316. {
  1317. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1318. struct mx2_camera_dev *pcdev = container_of(soc_host,
  1319. struct mx2_camera_dev, soc_host);
  1320. struct resource *res;
  1321. clk_put(pcdev->clk_csi);
  1322. if (cpu_is_mx25())
  1323. free_irq(pcdev->irq_csi, pcdev);
  1324. if (cpu_is_mx27())
  1325. free_irq(pcdev->irq_emma, pcdev);
  1326. soc_camera_host_unregister(&pcdev->soc_host);
  1327. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1328. iounmap(pcdev->base_csi);
  1329. if (cpu_is_mx27()) {
  1330. clk_disable(pcdev->clk_emma);
  1331. clk_put(pcdev->clk_emma);
  1332. iounmap(pcdev->base_emma);
  1333. res = pcdev->res_emma;
  1334. release_mem_region(res->start, resource_size(res));
  1335. }
  1336. res = pcdev->res_csi;
  1337. release_mem_region(res->start, resource_size(res));
  1338. kfree(pcdev);
  1339. dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
  1340. return 0;
  1341. }
  1342. static struct platform_driver mx2_camera_driver = {
  1343. .driver = {
  1344. .name = MX2_CAM_DRV_NAME,
  1345. },
  1346. .remove = __devexit_p(mx2_camera_remove),
  1347. };
  1348. static int __init mx2_camera_init(void)
  1349. {
  1350. return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
  1351. }
  1352. static void __exit mx2_camera_exit(void)
  1353. {
  1354. return platform_driver_unregister(&mx2_camera_driver);
  1355. }
  1356. module_init(mx2_camera_init);
  1357. module_exit(mx2_camera_exit);
  1358. MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
  1359. MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
  1360. MODULE_LICENSE("GPL");
  1361. MODULE_VERSION(MX2_CAM_VERSION);