mx2_camera.c 49 KB

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