mx2_camera.c 49 KB

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