pxa_camera.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. /*
  2. * V4L2 Driver for PXA camera host
  3. *
  4. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  5. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  6. * Copyright (C) 2016, Robert Jarzmik <robert.jarzmik@free.fr>
  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/device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/of.h>
  27. #include <linux/time.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/clk.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/dmaengine.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/dma/pxa-dma.h>
  35. #include <media/v4l2-async.h>
  36. #include <media/v4l2-clk.h>
  37. #include <media/v4l2-common.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-of.h>
  41. #include <media/videobuf2-dma-sg.h>
  42. #include <linux/videodev2.h>
  43. #include <linux/platform_data/media/camera-pxa.h>
  44. #define PXA_CAM_VERSION "0.0.6"
  45. #define PXA_CAM_DRV_NAME "pxa27x-camera"
  46. #define DEFAULT_WIDTH 640
  47. #define DEFAULT_HEIGHT 480
  48. /* Camera Interface */
  49. #define CICR0 0x0000
  50. #define CICR1 0x0004
  51. #define CICR2 0x0008
  52. #define CICR3 0x000C
  53. #define CICR4 0x0010
  54. #define CISR 0x0014
  55. #define CIFR 0x0018
  56. #define CITOR 0x001C
  57. #define CIBR0 0x0028
  58. #define CIBR1 0x0030
  59. #define CIBR2 0x0038
  60. #define CICR0_DMAEN (1 << 31) /* DMA request enable */
  61. #define CICR0_PAR_EN (1 << 30) /* Parity enable */
  62. #define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
  63. #define CICR0_ENB (1 << 28) /* Camera interface enable */
  64. #define CICR0_DIS (1 << 27) /* Camera interface disable */
  65. #define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
  66. #define CICR0_TOM (1 << 9) /* Time-out mask */
  67. #define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
  68. #define CICR0_FEM (1 << 7) /* FIFO-empty mask */
  69. #define CICR0_EOLM (1 << 6) /* End-of-line mask */
  70. #define CICR0_PERRM (1 << 5) /* Parity-error mask */
  71. #define CICR0_QDM (1 << 4) /* Quick-disable mask */
  72. #define CICR0_CDM (1 << 3) /* Disable-done mask */
  73. #define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
  74. #define CICR0_EOFM (1 << 1) /* End-of-frame mask */
  75. #define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
  76. #define CICR1_TBIT (1 << 31) /* Transparency bit */
  77. #define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
  78. #define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
  79. #define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
  80. #define CICR1_RGB_F (1 << 11) /* RGB format */
  81. #define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
  82. #define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
  83. #define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
  84. #define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
  85. #define CICR1_DW (0x7 << 0) /* Data width mask */
  86. #define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
  87. wait count mask */
  88. #define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
  89. wait count mask */
  90. #define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
  91. #define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
  92. wait count mask */
  93. #define CICR2_FSW (0x7 << 0) /* Frame stabilization
  94. wait count mask */
  95. #define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
  96. wait count mask */
  97. #define CICR3_EFW (0xff << 16) /* End-of-frame line clock
  98. wait count mask */
  99. #define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
  100. #define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
  101. wait count mask */
  102. #define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
  103. #define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
  104. #define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
  105. #define CICR4_PCP (1 << 22) /* Pixel clock polarity */
  106. #define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
  107. #define CICR4_VSP (1 << 20) /* Vertical sync polarity */
  108. #define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
  109. #define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
  110. #define CICR4_DIV (0xff << 0) /* Clock divisor mask */
  111. #define CISR_FTO (1 << 15) /* FIFO time-out */
  112. #define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
  113. #define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
  114. #define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
  115. #define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
  116. #define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
  117. #define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
  118. #define CISR_EOL (1 << 8) /* End of line */
  119. #define CISR_PAR_ERR (1 << 7) /* Parity error */
  120. #define CISR_CQD (1 << 6) /* Camera interface quick disable */
  121. #define CISR_CDD (1 << 5) /* Camera interface disable done */
  122. #define CISR_SOF (1 << 4) /* Start of frame */
  123. #define CISR_EOF (1 << 3) /* End of frame */
  124. #define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
  125. #define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
  126. #define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
  127. #define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
  128. #define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
  129. #define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
  130. #define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
  131. #define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
  132. #define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
  133. #define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
  134. #define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
  135. #define CICR0_SIM_MP (0 << 24)
  136. #define CICR0_SIM_SP (1 << 24)
  137. #define CICR0_SIM_MS (2 << 24)
  138. #define CICR0_SIM_EP (3 << 24)
  139. #define CICR0_SIM_ES (4 << 24)
  140. #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
  141. #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
  142. #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
  143. #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
  144. #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
  145. #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
  146. #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
  147. #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
  148. #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
  149. #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
  150. #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
  151. #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
  152. #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
  153. #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
  154. #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
  155. CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
  156. CICR0_EOFM | CICR0_FOM)
  157. #define sensor_call(cam, o, f, args...) \
  158. v4l2_subdev_call(cam->sensor, o, f, ##args)
  159. /*
  160. * Format handling
  161. */
  162. /**
  163. * enum pxa_mbus_packing - data packing types on the media-bus
  164. * @PXA_MBUS_PACKING_NONE: no packing, bit-for-bit transfer to RAM, one
  165. * sample represents one pixel
  166. * @PXA_MBUS_PACKING_2X8_PADHI: 16 bits transferred in 2 8-bit samples, in the
  167. * possibly incomplete byte high bits are padding
  168. * @PXA_MBUS_PACKING_EXTEND16: sample width (e.g., 10 bits) has to be extended
  169. * to 16 bits
  170. */
  171. enum pxa_mbus_packing {
  172. PXA_MBUS_PACKING_NONE,
  173. PXA_MBUS_PACKING_2X8_PADHI,
  174. PXA_MBUS_PACKING_EXTEND16,
  175. };
  176. /**
  177. * enum pxa_mbus_order - sample order on the media bus
  178. * @PXA_MBUS_ORDER_LE: least significant sample first
  179. * @PXA_MBUS_ORDER_BE: most significant sample first
  180. */
  181. enum pxa_mbus_order {
  182. PXA_MBUS_ORDER_LE,
  183. PXA_MBUS_ORDER_BE,
  184. };
  185. /**
  186. * enum pxa_mbus_layout - planes layout in memory
  187. * @PXA_MBUS_LAYOUT_PACKED: color components packed
  188. * @PXA_MBUS_LAYOUT_PLANAR_2Y_U_V: YUV components stored in 3 planes (4:2:2)
  189. * @PXA_MBUS_LAYOUT_PLANAR_2Y_C: YUV components stored in a luma and a
  190. * chroma plane (C plane is half the size
  191. * of Y plane)
  192. * @PXA_MBUS_LAYOUT_PLANAR_Y_C: YUV components stored in a luma and a
  193. * chroma plane (C plane is the same size
  194. * as Y plane)
  195. */
  196. enum pxa_mbus_layout {
  197. PXA_MBUS_LAYOUT_PACKED = 0,
  198. PXA_MBUS_LAYOUT_PLANAR_2Y_U_V,
  199. PXA_MBUS_LAYOUT_PLANAR_2Y_C,
  200. PXA_MBUS_LAYOUT_PLANAR_Y_C,
  201. };
  202. /**
  203. * struct pxa_mbus_pixelfmt - Data format on the media bus
  204. * @name: Name of the format
  205. * @fourcc: Fourcc code, that will be obtained if the data is
  206. * stored in memory in the following way:
  207. * @packing: Type of sample-packing, that has to be used
  208. * @order: Sample order when storing in memory
  209. * @bits_per_sample: How many bits the bridge has to sample
  210. */
  211. struct pxa_mbus_pixelfmt {
  212. const char *name;
  213. u32 fourcc;
  214. enum pxa_mbus_packing packing;
  215. enum pxa_mbus_order order;
  216. enum pxa_mbus_layout layout;
  217. u8 bits_per_sample;
  218. };
  219. /**
  220. * struct pxa_mbus_lookup - Lookup FOURCC IDs by mediabus codes for pass-through
  221. * @code: mediabus pixel-code
  222. * @fmt: pixel format description
  223. */
  224. struct pxa_mbus_lookup {
  225. u32 code;
  226. struct pxa_mbus_pixelfmt fmt;
  227. };
  228. static const struct pxa_mbus_lookup mbus_fmt[] = {
  229. {
  230. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  231. .fmt = {
  232. .fourcc = V4L2_PIX_FMT_YUYV,
  233. .name = "YUYV",
  234. .bits_per_sample = 8,
  235. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  236. .order = PXA_MBUS_ORDER_LE,
  237. .layout = PXA_MBUS_LAYOUT_PACKED,
  238. },
  239. }, {
  240. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  241. .fmt = {
  242. .fourcc = V4L2_PIX_FMT_YVYU,
  243. .name = "YVYU",
  244. .bits_per_sample = 8,
  245. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  246. .order = PXA_MBUS_ORDER_LE,
  247. .layout = PXA_MBUS_LAYOUT_PACKED,
  248. },
  249. }, {
  250. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  251. .fmt = {
  252. .fourcc = V4L2_PIX_FMT_UYVY,
  253. .name = "UYVY",
  254. .bits_per_sample = 8,
  255. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  256. .order = PXA_MBUS_ORDER_LE,
  257. .layout = PXA_MBUS_LAYOUT_PACKED,
  258. },
  259. }, {
  260. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  261. .fmt = {
  262. .fourcc = V4L2_PIX_FMT_VYUY,
  263. .name = "VYUY",
  264. .bits_per_sample = 8,
  265. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  266. .order = PXA_MBUS_ORDER_LE,
  267. .layout = PXA_MBUS_LAYOUT_PACKED,
  268. },
  269. }, {
  270. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
  271. .fmt = {
  272. .fourcc = V4L2_PIX_FMT_RGB555,
  273. .name = "RGB555",
  274. .bits_per_sample = 8,
  275. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  276. .order = PXA_MBUS_ORDER_LE,
  277. .layout = PXA_MBUS_LAYOUT_PACKED,
  278. },
  279. }, {
  280. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
  281. .fmt = {
  282. .fourcc = V4L2_PIX_FMT_RGB555X,
  283. .name = "RGB555X",
  284. .bits_per_sample = 8,
  285. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  286. .order = PXA_MBUS_ORDER_BE,
  287. .layout = PXA_MBUS_LAYOUT_PACKED,
  288. },
  289. }, {
  290. .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  291. .fmt = {
  292. .fourcc = V4L2_PIX_FMT_RGB565,
  293. .name = "RGB565",
  294. .bits_per_sample = 8,
  295. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  296. .order = PXA_MBUS_ORDER_LE,
  297. .layout = PXA_MBUS_LAYOUT_PACKED,
  298. },
  299. }, {
  300. .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
  301. .fmt = {
  302. .fourcc = V4L2_PIX_FMT_RGB565X,
  303. .name = "RGB565X",
  304. .bits_per_sample = 8,
  305. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  306. .order = PXA_MBUS_ORDER_BE,
  307. .layout = PXA_MBUS_LAYOUT_PACKED,
  308. },
  309. }, {
  310. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  311. .fmt = {
  312. .fourcc = V4L2_PIX_FMT_SBGGR8,
  313. .name = "Bayer 8 BGGR",
  314. .bits_per_sample = 8,
  315. .packing = PXA_MBUS_PACKING_NONE,
  316. .order = PXA_MBUS_ORDER_LE,
  317. .layout = PXA_MBUS_LAYOUT_PACKED,
  318. },
  319. }, {
  320. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  321. .fmt = {
  322. .fourcc = V4L2_PIX_FMT_SBGGR10,
  323. .name = "Bayer 10 BGGR",
  324. .bits_per_sample = 10,
  325. .packing = PXA_MBUS_PACKING_EXTEND16,
  326. .order = PXA_MBUS_ORDER_LE,
  327. .layout = PXA_MBUS_LAYOUT_PACKED,
  328. },
  329. }, {
  330. .code = MEDIA_BUS_FMT_Y8_1X8,
  331. .fmt = {
  332. .fourcc = V4L2_PIX_FMT_GREY,
  333. .name = "Grey",
  334. .bits_per_sample = 8,
  335. .packing = PXA_MBUS_PACKING_NONE,
  336. .order = PXA_MBUS_ORDER_LE,
  337. .layout = PXA_MBUS_LAYOUT_PACKED,
  338. },
  339. }, {
  340. .code = MEDIA_BUS_FMT_Y10_1X10,
  341. .fmt = {
  342. .fourcc = V4L2_PIX_FMT_Y10,
  343. .name = "Grey 10bit",
  344. .bits_per_sample = 10,
  345. .packing = PXA_MBUS_PACKING_EXTEND16,
  346. .order = PXA_MBUS_ORDER_LE,
  347. .layout = PXA_MBUS_LAYOUT_PACKED,
  348. },
  349. }, {
  350. .code = MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE,
  351. .fmt = {
  352. .fourcc = V4L2_PIX_FMT_SBGGR10,
  353. .name = "Bayer 10 BGGR",
  354. .bits_per_sample = 8,
  355. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  356. .order = PXA_MBUS_ORDER_LE,
  357. .layout = PXA_MBUS_LAYOUT_PACKED,
  358. },
  359. }, {
  360. .code = MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE,
  361. .fmt = {
  362. .fourcc = V4L2_PIX_FMT_SBGGR10,
  363. .name = "Bayer 10 BGGR",
  364. .bits_per_sample = 8,
  365. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  366. .order = PXA_MBUS_ORDER_BE,
  367. .layout = PXA_MBUS_LAYOUT_PACKED,
  368. },
  369. }, {
  370. .code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE,
  371. .fmt = {
  372. .fourcc = V4L2_PIX_FMT_RGB444,
  373. .name = "RGB444",
  374. .bits_per_sample = 8,
  375. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  376. .order = PXA_MBUS_ORDER_BE,
  377. .layout = PXA_MBUS_LAYOUT_PACKED,
  378. },
  379. }, {
  380. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  381. .fmt = {
  382. .fourcc = V4L2_PIX_FMT_UYVY,
  383. .name = "UYVY 16bit",
  384. .bits_per_sample = 16,
  385. .packing = PXA_MBUS_PACKING_EXTEND16,
  386. .order = PXA_MBUS_ORDER_LE,
  387. .layout = PXA_MBUS_LAYOUT_PACKED,
  388. },
  389. }, {
  390. .code = MEDIA_BUS_FMT_VYUY8_1X16,
  391. .fmt = {
  392. .fourcc = V4L2_PIX_FMT_VYUY,
  393. .name = "VYUY 16bit",
  394. .bits_per_sample = 16,
  395. .packing = PXA_MBUS_PACKING_EXTEND16,
  396. .order = PXA_MBUS_ORDER_LE,
  397. .layout = PXA_MBUS_LAYOUT_PACKED,
  398. },
  399. }, {
  400. .code = MEDIA_BUS_FMT_YUYV8_1X16,
  401. .fmt = {
  402. .fourcc = V4L2_PIX_FMT_YUYV,
  403. .name = "YUYV 16bit",
  404. .bits_per_sample = 16,
  405. .packing = PXA_MBUS_PACKING_EXTEND16,
  406. .order = PXA_MBUS_ORDER_LE,
  407. .layout = PXA_MBUS_LAYOUT_PACKED,
  408. },
  409. }, {
  410. .code = MEDIA_BUS_FMT_YVYU8_1X16,
  411. .fmt = {
  412. .fourcc = V4L2_PIX_FMT_YVYU,
  413. .name = "YVYU 16bit",
  414. .bits_per_sample = 16,
  415. .packing = PXA_MBUS_PACKING_EXTEND16,
  416. .order = PXA_MBUS_ORDER_LE,
  417. .layout = PXA_MBUS_LAYOUT_PACKED,
  418. },
  419. }, {
  420. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  421. .fmt = {
  422. .fourcc = V4L2_PIX_FMT_SGRBG8,
  423. .name = "Bayer 8 GRBG",
  424. .bits_per_sample = 8,
  425. .packing = PXA_MBUS_PACKING_NONE,
  426. .order = PXA_MBUS_ORDER_LE,
  427. .layout = PXA_MBUS_LAYOUT_PACKED,
  428. },
  429. }, {
  430. .code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
  431. .fmt = {
  432. .fourcc = V4L2_PIX_FMT_SGRBG10DPCM8,
  433. .name = "Bayer 10 BGGR DPCM 8",
  434. .bits_per_sample = 8,
  435. .packing = PXA_MBUS_PACKING_NONE,
  436. .order = PXA_MBUS_ORDER_LE,
  437. .layout = PXA_MBUS_LAYOUT_PACKED,
  438. },
  439. }, {
  440. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  441. .fmt = {
  442. .fourcc = V4L2_PIX_FMT_SGBRG10,
  443. .name = "Bayer 10 GBRG",
  444. .bits_per_sample = 10,
  445. .packing = PXA_MBUS_PACKING_EXTEND16,
  446. .order = PXA_MBUS_ORDER_LE,
  447. .layout = PXA_MBUS_LAYOUT_PACKED,
  448. },
  449. }, {
  450. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  451. .fmt = {
  452. .fourcc = V4L2_PIX_FMT_SGRBG10,
  453. .name = "Bayer 10 GRBG",
  454. .bits_per_sample = 10,
  455. .packing = PXA_MBUS_PACKING_EXTEND16,
  456. .order = PXA_MBUS_ORDER_LE,
  457. .layout = PXA_MBUS_LAYOUT_PACKED,
  458. },
  459. }, {
  460. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  461. .fmt = {
  462. .fourcc = V4L2_PIX_FMT_SRGGB10,
  463. .name = "Bayer 10 RGGB",
  464. .bits_per_sample = 10,
  465. .packing = PXA_MBUS_PACKING_EXTEND16,
  466. .order = PXA_MBUS_ORDER_LE,
  467. .layout = PXA_MBUS_LAYOUT_PACKED,
  468. },
  469. }, {
  470. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  471. .fmt = {
  472. .fourcc = V4L2_PIX_FMT_SBGGR12,
  473. .name = "Bayer 12 BGGR",
  474. .bits_per_sample = 12,
  475. .packing = PXA_MBUS_PACKING_EXTEND16,
  476. .order = PXA_MBUS_ORDER_LE,
  477. .layout = PXA_MBUS_LAYOUT_PACKED,
  478. },
  479. }, {
  480. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  481. .fmt = {
  482. .fourcc = V4L2_PIX_FMT_SGBRG12,
  483. .name = "Bayer 12 GBRG",
  484. .bits_per_sample = 12,
  485. .packing = PXA_MBUS_PACKING_EXTEND16,
  486. .order = PXA_MBUS_ORDER_LE,
  487. .layout = PXA_MBUS_LAYOUT_PACKED,
  488. },
  489. }, {
  490. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  491. .fmt = {
  492. .fourcc = V4L2_PIX_FMT_SGRBG12,
  493. .name = "Bayer 12 GRBG",
  494. .bits_per_sample = 12,
  495. .packing = PXA_MBUS_PACKING_EXTEND16,
  496. .order = PXA_MBUS_ORDER_LE,
  497. .layout = PXA_MBUS_LAYOUT_PACKED,
  498. },
  499. }, {
  500. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  501. .fmt = {
  502. .fourcc = V4L2_PIX_FMT_SRGGB12,
  503. .name = "Bayer 12 RGGB",
  504. .bits_per_sample = 12,
  505. .packing = PXA_MBUS_PACKING_EXTEND16,
  506. .order = PXA_MBUS_ORDER_LE,
  507. .layout = PXA_MBUS_LAYOUT_PACKED,
  508. },
  509. },
  510. };
  511. static s32 pxa_mbus_bytes_per_line(u32 width, const struct pxa_mbus_pixelfmt *mf)
  512. {
  513. if (mf->layout != PXA_MBUS_LAYOUT_PACKED)
  514. return width * mf->bits_per_sample / 8;
  515. switch (mf->packing) {
  516. case PXA_MBUS_PACKING_NONE:
  517. return width * mf->bits_per_sample / 8;
  518. case PXA_MBUS_PACKING_2X8_PADHI:
  519. case PXA_MBUS_PACKING_EXTEND16:
  520. return width * 2;
  521. }
  522. return -EINVAL;
  523. }
  524. static s32 pxa_mbus_image_size(const struct pxa_mbus_pixelfmt *mf,
  525. u32 bytes_per_line, u32 height)
  526. {
  527. switch (mf->packing) {
  528. case PXA_MBUS_PACKING_2X8_PADHI:
  529. return bytes_per_line * height * 2;
  530. default:
  531. return -EINVAL;
  532. }
  533. }
  534. static const struct pxa_mbus_pixelfmt *pxa_mbus_find_fmtdesc(
  535. u32 code,
  536. const struct pxa_mbus_lookup *lookup,
  537. int n)
  538. {
  539. int i;
  540. for (i = 0; i < n; i++)
  541. if (lookup[i].code == code)
  542. return &lookup[i].fmt;
  543. return NULL;
  544. }
  545. static const struct pxa_mbus_pixelfmt *pxa_mbus_get_fmtdesc(
  546. u32 code)
  547. {
  548. return pxa_mbus_find_fmtdesc(code, mbus_fmt, ARRAY_SIZE(mbus_fmt));
  549. }
  550. static unsigned int pxa_mbus_config_compatible(const struct v4l2_mbus_config *cfg,
  551. unsigned int flags)
  552. {
  553. unsigned long common_flags;
  554. bool hsync = true, vsync = true, pclk, data, mode;
  555. bool mipi_lanes, mipi_clock;
  556. common_flags = cfg->flags & flags;
  557. switch (cfg->type) {
  558. case V4L2_MBUS_PARALLEL:
  559. hsync = common_flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  560. V4L2_MBUS_HSYNC_ACTIVE_LOW);
  561. vsync = common_flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  562. V4L2_MBUS_VSYNC_ACTIVE_LOW);
  563. /* fall through */
  564. case V4L2_MBUS_BT656:
  565. pclk = common_flags & (V4L2_MBUS_PCLK_SAMPLE_RISING |
  566. V4L2_MBUS_PCLK_SAMPLE_FALLING);
  567. data = common_flags & (V4L2_MBUS_DATA_ACTIVE_HIGH |
  568. V4L2_MBUS_DATA_ACTIVE_LOW);
  569. mode = common_flags & (V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE);
  570. return (!hsync || !vsync || !pclk || !data || !mode) ?
  571. 0 : common_flags;
  572. case V4L2_MBUS_CSI2:
  573. mipi_lanes = common_flags & V4L2_MBUS_CSI2_LANES;
  574. mipi_clock = common_flags & (V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK |
  575. V4L2_MBUS_CSI2_CONTINUOUS_CLOCK);
  576. return (!mipi_lanes || !mipi_clock) ? 0 : common_flags;
  577. }
  578. return 0;
  579. }
  580. /**
  581. * struct soc_camera_format_xlate - match between host and sensor formats
  582. * @code: code of a sensor provided format
  583. * @host_fmt: host format after host translation from code
  584. *
  585. * Host and sensor translation structure. Used in table of host and sensor
  586. * formats matchings in soc_camera_device. A host can override the generic list
  587. * generation by implementing get_formats(), and use it for format checks and
  588. * format setup.
  589. */
  590. struct soc_camera_format_xlate {
  591. u32 code;
  592. const struct pxa_mbus_pixelfmt *host_fmt;
  593. };
  594. /*
  595. * Structures
  596. */
  597. enum pxa_camera_active_dma {
  598. DMA_Y = 0x1,
  599. DMA_U = 0x2,
  600. DMA_V = 0x4,
  601. };
  602. /* buffer for one video frame */
  603. struct pxa_buffer {
  604. /* common v4l buffer stuff -- must be first */
  605. struct vb2_v4l2_buffer vbuf;
  606. struct list_head queue;
  607. u32 code;
  608. int nb_planes;
  609. /* our descriptor lists for Y, U and V channels */
  610. struct dma_async_tx_descriptor *descs[3];
  611. dma_cookie_t cookie[3];
  612. struct scatterlist *sg[3];
  613. int sg_len[3];
  614. size_t plane_sizes[3];
  615. int inwork;
  616. enum pxa_camera_active_dma active_dma;
  617. };
  618. struct pxa_camera_dev {
  619. struct v4l2_device v4l2_dev;
  620. struct video_device vdev;
  621. struct v4l2_async_notifier notifier;
  622. struct vb2_queue vb2_vq;
  623. struct v4l2_subdev *sensor;
  624. struct soc_camera_format_xlate *user_formats;
  625. const struct soc_camera_format_xlate *current_fmt;
  626. struct v4l2_pix_format current_pix;
  627. struct v4l2_async_subdev asd;
  628. struct v4l2_async_subdev *asds[1];
  629. /*
  630. * PXA27x is only supposed to handle one camera on its Quick Capture
  631. * interface. If anyone ever builds hardware to enable more than
  632. * one camera, they will have to modify this driver too
  633. */
  634. struct clk *clk;
  635. unsigned int irq;
  636. void __iomem *base;
  637. int channels;
  638. struct dma_chan *dma_chans[3];
  639. struct pxacamera_platform_data *pdata;
  640. struct resource *res;
  641. unsigned long platform_flags;
  642. unsigned long ciclk;
  643. unsigned long mclk;
  644. u32 mclk_divisor;
  645. struct v4l2_clk *mclk_clk;
  646. u16 width_flags; /* max 10 bits */
  647. struct list_head capture;
  648. spinlock_t lock;
  649. struct mutex mlock;
  650. unsigned int buf_sequence;
  651. struct pxa_buffer *active;
  652. struct tasklet_struct task_eof;
  653. u32 save_cicr[5];
  654. };
  655. struct pxa_cam {
  656. unsigned long flags;
  657. };
  658. static const char *pxa_cam_driver_description = "PXA_Camera";
  659. /*
  660. * Format translation functions
  661. */
  662. static const struct soc_camera_format_xlate
  663. *pxa_mbus_xlate_by_fourcc(struct soc_camera_format_xlate *user_formats,
  664. unsigned int fourcc)
  665. {
  666. unsigned int i;
  667. for (i = 0; user_formats[i].code; i++)
  668. if (user_formats[i].host_fmt->fourcc == fourcc)
  669. return user_formats + i;
  670. return NULL;
  671. }
  672. static struct soc_camera_format_xlate *pxa_mbus_build_fmts_xlate(
  673. struct v4l2_device *v4l2_dev, struct v4l2_subdev *subdev,
  674. int (*get_formats)(struct v4l2_device *, unsigned int,
  675. struct soc_camera_format_xlate *xlate))
  676. {
  677. unsigned int i, fmts = 0, raw_fmts = 0;
  678. int ret;
  679. struct v4l2_subdev_mbus_code_enum code = {
  680. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  681. };
  682. struct soc_camera_format_xlate *user_formats;
  683. while (!v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
  684. raw_fmts++;
  685. code.index++;
  686. }
  687. /*
  688. * First pass - only count formats this host-sensor
  689. * configuration can provide
  690. */
  691. for (i = 0; i < raw_fmts; i++) {
  692. ret = get_formats(v4l2_dev, i, NULL);
  693. if (ret < 0)
  694. return ERR_PTR(ret);
  695. fmts += ret;
  696. }
  697. if (!fmts)
  698. return ERR_PTR(-ENXIO);
  699. user_formats = kcalloc(fmts + 1, sizeof(*user_formats), GFP_KERNEL);
  700. if (!user_formats)
  701. return ERR_PTR(-ENOMEM);
  702. /* Second pass - actually fill data formats */
  703. fmts = 0;
  704. for (i = 0; i < raw_fmts; i++) {
  705. ret = get_formats(v4l2_dev, i, user_formats + fmts);
  706. if (ret < 0)
  707. goto egfmt;
  708. fmts += ret;
  709. }
  710. user_formats[fmts].code = 0;
  711. return user_formats;
  712. egfmt:
  713. kfree(user_formats);
  714. return ERR_PTR(ret);
  715. }
  716. /*
  717. * Videobuf operations
  718. */
  719. static struct pxa_buffer *vb2_to_pxa_buffer(struct vb2_buffer *vb)
  720. {
  721. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  722. return container_of(vbuf, struct pxa_buffer, vbuf);
  723. }
  724. static struct device *pcdev_to_dev(struct pxa_camera_dev *pcdev)
  725. {
  726. return pcdev->v4l2_dev.dev;
  727. }
  728. static struct pxa_camera_dev *v4l2_dev_to_pcdev(struct v4l2_device *v4l2_dev)
  729. {
  730. return container_of(v4l2_dev, struct pxa_camera_dev, v4l2_dev);
  731. }
  732. static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev,
  733. enum pxa_camera_active_dma act_dma);
  734. static void pxa_camera_dma_irq_y(void *data)
  735. {
  736. struct pxa_camera_dev *pcdev = data;
  737. pxa_camera_dma_irq(pcdev, DMA_Y);
  738. }
  739. static void pxa_camera_dma_irq_u(void *data)
  740. {
  741. struct pxa_camera_dev *pcdev = data;
  742. pxa_camera_dma_irq(pcdev, DMA_U);
  743. }
  744. static void pxa_camera_dma_irq_v(void *data)
  745. {
  746. struct pxa_camera_dev *pcdev = data;
  747. pxa_camera_dma_irq(pcdev, DMA_V);
  748. }
  749. /**
  750. * pxa_init_dma_channel - init dma descriptors
  751. * @pcdev: pxa camera device
  752. * @vb: videobuffer2 buffer
  753. * @dma: dma video buffer
  754. * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
  755. * @cibr: camera Receive Buffer Register
  756. *
  757. * Prepares the pxa dma descriptors to transfer one camera channel.
  758. *
  759. * Returns 0 if success or -ENOMEM if no memory is available
  760. */
  761. static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
  762. struct pxa_buffer *buf, int channel,
  763. struct scatterlist *sg, int sglen)
  764. {
  765. struct dma_chan *dma_chan = pcdev->dma_chans[channel];
  766. struct dma_async_tx_descriptor *tx;
  767. tx = dmaengine_prep_slave_sg(dma_chan, sg, sglen, DMA_DEV_TO_MEM,
  768. DMA_PREP_INTERRUPT | DMA_CTRL_REUSE);
  769. if (!tx) {
  770. dev_err(pcdev_to_dev(pcdev),
  771. "dmaengine_prep_slave_sg failed\n");
  772. goto fail;
  773. }
  774. tx->callback_param = pcdev;
  775. switch (channel) {
  776. case 0:
  777. tx->callback = pxa_camera_dma_irq_y;
  778. break;
  779. case 1:
  780. tx->callback = pxa_camera_dma_irq_u;
  781. break;
  782. case 2:
  783. tx->callback = pxa_camera_dma_irq_v;
  784. break;
  785. }
  786. buf->descs[channel] = tx;
  787. return 0;
  788. fail:
  789. dev_dbg(pcdev_to_dev(pcdev),
  790. "%s (vb=%p) dma_tx=%p\n",
  791. __func__, buf, tx);
  792. return -ENOMEM;
  793. }
  794. static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev,
  795. struct pxa_buffer *buf)
  796. {
  797. buf->active_dma = DMA_Y;
  798. if (buf->nb_planes == 3)
  799. buf->active_dma |= DMA_U | DMA_V;
  800. }
  801. /**
  802. * pxa_dma_start_channels - start DMA channel for active buffer
  803. * @pcdev: pxa camera device
  804. *
  805. * Initialize DMA channels to the beginning of the active video buffer, and
  806. * start these channels.
  807. */
  808. static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev)
  809. {
  810. int i;
  811. for (i = 0; i < pcdev->channels; i++) {
  812. dev_dbg(pcdev_to_dev(pcdev),
  813. "%s (channel=%d)\n", __func__, i);
  814. dma_async_issue_pending(pcdev->dma_chans[i]);
  815. }
  816. }
  817. static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev)
  818. {
  819. int i;
  820. for (i = 0; i < pcdev->channels; i++) {
  821. dev_dbg(pcdev_to_dev(pcdev),
  822. "%s (channel=%d)\n", __func__, i);
  823. dmaengine_terminate_all(pcdev->dma_chans[i]);
  824. }
  825. }
  826. static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev,
  827. struct pxa_buffer *buf)
  828. {
  829. int i;
  830. for (i = 0; i < pcdev->channels; i++) {
  831. buf->cookie[i] = dmaengine_submit(buf->descs[i]);
  832. dev_dbg(pcdev_to_dev(pcdev),
  833. "%s (channel=%d) : submit vb=%p cookie=%d\n",
  834. __func__, i, buf, buf->descs[i]->cookie);
  835. }
  836. }
  837. /**
  838. * pxa_camera_start_capture - start video capturing
  839. * @pcdev: camera device
  840. *
  841. * Launch capturing. DMA channels should not be active yet. They should get
  842. * activated at the end of frame interrupt, to capture only whole frames, and
  843. * never begin the capture of a partial frame.
  844. */
  845. static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev)
  846. {
  847. unsigned long cicr0;
  848. dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__);
  849. __raw_writel(__raw_readl(pcdev->base + CISR), pcdev->base + CISR);
  850. /* Enable End-Of-Frame Interrupt */
  851. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
  852. cicr0 &= ~CICR0_EOFM;
  853. __raw_writel(cicr0, pcdev->base + CICR0);
  854. }
  855. static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev)
  856. {
  857. unsigned long cicr0;
  858. pxa_dma_stop_channels(pcdev);
  859. cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
  860. __raw_writel(cicr0, pcdev->base + CICR0);
  861. pcdev->active = NULL;
  862. dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__);
  863. }
  864. static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
  865. struct pxa_buffer *buf,
  866. enum vb2_buffer_state state)
  867. {
  868. struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
  869. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  870. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  871. list_del_init(&buf->queue);
  872. vb->timestamp = ktime_get_ns();
  873. vbuf->sequence = pcdev->buf_sequence++;
  874. vbuf->field = V4L2_FIELD_NONE;
  875. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  876. dev_dbg(pcdev_to_dev(pcdev), "%s dequeued buffer (buf=0x%p)\n",
  877. __func__, buf);
  878. if (list_empty(&pcdev->capture)) {
  879. pxa_camera_stop_capture(pcdev);
  880. return;
  881. }
  882. pcdev->active = list_entry(pcdev->capture.next,
  883. struct pxa_buffer, queue);
  884. }
  885. /**
  886. * pxa_camera_check_link_miss - check missed DMA linking
  887. * @pcdev: camera device
  888. *
  889. * The DMA chaining is done with DMA running. This means a tiny temporal window
  890. * remains, where a buffer is queued on the chain, while the chain is already
  891. * stopped. This means the tailed buffer would never be transferred by DMA.
  892. * This function restarts the capture for this corner case, where :
  893. * - DADR() == DADDR_STOP
  894. * - a videobuffer is queued on the pcdev->capture list
  895. *
  896. * Please check the "DMA hot chaining timeslice issue" in
  897. * Documentation/video4linux/pxa_camera.txt
  898. *
  899. * Context: should only be called within the dma irq handler
  900. */
  901. static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev,
  902. dma_cookie_t last_submitted,
  903. dma_cookie_t last_issued)
  904. {
  905. bool is_dma_stopped = last_submitted != last_issued;
  906. dev_dbg(pcdev_to_dev(pcdev),
  907. "%s : top queued buffer=%p, is_dma_stopped=%d\n",
  908. __func__, pcdev->active, is_dma_stopped);
  909. if (pcdev->active && is_dma_stopped)
  910. pxa_camera_start_capture(pcdev);
  911. }
  912. static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev,
  913. enum pxa_camera_active_dma act_dma)
  914. {
  915. struct pxa_buffer *buf, *last_buf;
  916. unsigned long flags;
  917. u32 camera_status, overrun;
  918. int chan;
  919. enum dma_status last_status;
  920. dma_cookie_t last_issued;
  921. spin_lock_irqsave(&pcdev->lock, flags);
  922. camera_status = __raw_readl(pcdev->base + CISR);
  923. dev_dbg(pcdev_to_dev(pcdev), "camera dma irq, cisr=0x%x dma=%d\n",
  924. camera_status, act_dma);
  925. overrun = CISR_IFO_0;
  926. if (pcdev->channels == 3)
  927. overrun |= CISR_IFO_1 | CISR_IFO_2;
  928. /*
  929. * pcdev->active should not be NULL in DMA irq handler.
  930. *
  931. * But there is one corner case : if capture was stopped due to an
  932. * overrun of channel 1, and at that same channel 2 was completed.
  933. *
  934. * When handling the overrun in DMA irq for channel 1, we'll stop the
  935. * capture and restart it (and thus set pcdev->active to NULL). But the
  936. * DMA irq handler will already be pending for channel 2. So on entering
  937. * the DMA irq handler for channel 2 there will be no active buffer, yet
  938. * that is normal.
  939. */
  940. if (!pcdev->active)
  941. goto out;
  942. buf = pcdev->active;
  943. WARN_ON(buf->inwork || list_empty(&buf->queue));
  944. /*
  945. * It's normal if the last frame creates an overrun, as there
  946. * are no more DMA descriptors to fetch from QCI fifos
  947. */
  948. switch (act_dma) {
  949. case DMA_U:
  950. chan = 1;
  951. break;
  952. case DMA_V:
  953. chan = 2;
  954. break;
  955. default:
  956. chan = 0;
  957. break;
  958. }
  959. last_buf = list_entry(pcdev->capture.prev,
  960. struct pxa_buffer, queue);
  961. last_status = dma_async_is_tx_complete(pcdev->dma_chans[chan],
  962. last_buf->cookie[chan],
  963. NULL, &last_issued);
  964. if (camera_status & overrun &&
  965. last_status != DMA_COMPLETE) {
  966. dev_dbg(pcdev_to_dev(pcdev), "FIFO overrun! CISR: %x\n",
  967. camera_status);
  968. pxa_camera_stop_capture(pcdev);
  969. list_for_each_entry(buf, &pcdev->capture, queue)
  970. pxa_dma_add_tail_buf(pcdev, buf);
  971. pxa_camera_start_capture(pcdev);
  972. goto out;
  973. }
  974. buf->active_dma &= ~act_dma;
  975. if (!buf->active_dma) {
  976. pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_DONE);
  977. pxa_camera_check_link_miss(pcdev, last_buf->cookie[chan],
  978. last_issued);
  979. }
  980. out:
  981. spin_unlock_irqrestore(&pcdev->lock, flags);
  982. }
  983. static u32 mclk_get_divisor(struct platform_device *pdev,
  984. struct pxa_camera_dev *pcdev)
  985. {
  986. unsigned long mclk = pcdev->mclk;
  987. u32 div;
  988. unsigned long lcdclk;
  989. lcdclk = clk_get_rate(pcdev->clk);
  990. pcdev->ciclk = lcdclk;
  991. /* mclk <= ciclk / 4 (27.4.2) */
  992. if (mclk > lcdclk / 4) {
  993. mclk = lcdclk / 4;
  994. dev_warn(pcdev_to_dev(pcdev),
  995. "Limiting master clock to %lu\n", mclk);
  996. }
  997. /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
  998. div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
  999. /* If we're not supplying MCLK, leave it at 0 */
  1000. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1001. pcdev->mclk = lcdclk / (2 * (div + 1));
  1002. dev_dbg(pcdev_to_dev(pcdev), "LCD clock %luHz, target freq %luHz, divisor %u\n",
  1003. lcdclk, mclk, div);
  1004. return div;
  1005. }
  1006. static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
  1007. unsigned long pclk)
  1008. {
  1009. /* We want a timeout > 1 pixel time, not ">=" */
  1010. u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
  1011. __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
  1012. }
  1013. static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
  1014. {
  1015. u32 cicr4 = 0;
  1016. /* disable all interrupts */
  1017. __raw_writel(0x3ff, pcdev->base + CICR0);
  1018. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1019. cicr4 |= CICR4_PCLK_EN;
  1020. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1021. cicr4 |= CICR4_MCLK_EN;
  1022. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  1023. cicr4 |= CICR4_PCP;
  1024. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  1025. cicr4 |= CICR4_HSP;
  1026. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  1027. cicr4 |= CICR4_VSP;
  1028. __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
  1029. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1030. /* Initialise the timeout under the assumption pclk = mclk */
  1031. recalculate_fifo_timeout(pcdev, pcdev->mclk);
  1032. else
  1033. /* "Safe default" - 13MHz */
  1034. recalculate_fifo_timeout(pcdev, 13000000);
  1035. clk_prepare_enable(pcdev->clk);
  1036. }
  1037. static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
  1038. {
  1039. clk_disable_unprepare(pcdev->clk);
  1040. }
  1041. static void pxa_camera_eof(unsigned long arg)
  1042. {
  1043. struct pxa_camera_dev *pcdev = (struct pxa_camera_dev *)arg;
  1044. unsigned long cifr;
  1045. struct pxa_buffer *buf;
  1046. dev_dbg(pcdev_to_dev(pcdev),
  1047. "Camera interrupt status 0x%x\n",
  1048. __raw_readl(pcdev->base + CISR));
  1049. /* Reset the FIFOs */
  1050. cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
  1051. __raw_writel(cifr, pcdev->base + CIFR);
  1052. pcdev->active = list_first_entry(&pcdev->capture,
  1053. struct pxa_buffer, queue);
  1054. buf = pcdev->active;
  1055. pxa_videobuf_set_actdma(pcdev, buf);
  1056. pxa_dma_start_channels(pcdev);
  1057. }
  1058. static irqreturn_t pxa_camera_irq(int irq, void *data)
  1059. {
  1060. struct pxa_camera_dev *pcdev = data;
  1061. unsigned long status, cicr0;
  1062. status = __raw_readl(pcdev->base + CISR);
  1063. dev_dbg(pcdev_to_dev(pcdev),
  1064. "Camera interrupt status 0x%lx\n", status);
  1065. if (!status)
  1066. return IRQ_NONE;
  1067. __raw_writel(status, pcdev->base + CISR);
  1068. if (status & CISR_EOF) {
  1069. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
  1070. __raw_writel(cicr0, pcdev->base + CICR0);
  1071. tasklet_schedule(&pcdev->task_eof);
  1072. }
  1073. return IRQ_HANDLED;
  1074. }
  1075. static int test_platform_param(struct pxa_camera_dev *pcdev,
  1076. unsigned char buswidth, unsigned long *flags)
  1077. {
  1078. /*
  1079. * Platform specified synchronization and pixel clock polarities are
  1080. * only a recommendation and are only used during probing. The PXA270
  1081. * quick capture interface supports both.
  1082. */
  1083. *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  1084. V4L2_MBUS_MASTER : V4L2_MBUS_SLAVE) |
  1085. V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  1086. V4L2_MBUS_HSYNC_ACTIVE_LOW |
  1087. V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  1088. V4L2_MBUS_VSYNC_ACTIVE_LOW |
  1089. V4L2_MBUS_DATA_ACTIVE_HIGH |
  1090. V4L2_MBUS_PCLK_SAMPLE_RISING |
  1091. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  1092. /* If requested data width is supported by the platform, use it */
  1093. if ((1 << (buswidth - 1)) & pcdev->width_flags)
  1094. return 0;
  1095. return -EINVAL;
  1096. }
  1097. static void pxa_camera_setup_cicr(struct pxa_camera_dev *pcdev,
  1098. unsigned long flags, __u32 pixfmt)
  1099. {
  1100. unsigned long dw, bpp;
  1101. u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0, y_skip_top;
  1102. int ret = sensor_call(pcdev, sensor, g_skip_top_lines, &y_skip_top);
  1103. if (ret < 0)
  1104. y_skip_top = 0;
  1105. /*
  1106. * Datawidth is now guaranteed to be equal to one of the three values.
  1107. * We fix bit-per-pixel equal to data-width...
  1108. */
  1109. switch (pcdev->current_fmt->host_fmt->bits_per_sample) {
  1110. case 10:
  1111. dw = 4;
  1112. bpp = 0x40;
  1113. break;
  1114. case 9:
  1115. dw = 3;
  1116. bpp = 0x20;
  1117. break;
  1118. default:
  1119. /*
  1120. * Actually it can only be 8 now,
  1121. * default is just to silence compiler warnings
  1122. */
  1123. case 8:
  1124. dw = 2;
  1125. bpp = 0;
  1126. }
  1127. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1128. cicr4 |= CICR4_PCLK_EN;
  1129. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1130. cicr4 |= CICR4_MCLK_EN;
  1131. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  1132. cicr4 |= CICR4_PCP;
  1133. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  1134. cicr4 |= CICR4_HSP;
  1135. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  1136. cicr4 |= CICR4_VSP;
  1137. cicr0 = __raw_readl(pcdev->base + CICR0);
  1138. if (cicr0 & CICR0_ENB)
  1139. __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
  1140. cicr1 = CICR1_PPL_VAL(pcdev->current_pix.width - 1) | bpp | dw;
  1141. switch (pixfmt) {
  1142. case V4L2_PIX_FMT_YUV422P:
  1143. pcdev->channels = 3;
  1144. cicr1 |= CICR1_YCBCR_F;
  1145. /*
  1146. * Normally, pxa bus wants as input UYVY format. We allow all
  1147. * reorderings of the YUV422 format, as no processing is done,
  1148. * and the YUV stream is just passed through without any
  1149. * transformation. Note that UYVY is the only format that
  1150. * should be used if pxa framebuffer Overlay2 is used.
  1151. */
  1152. case V4L2_PIX_FMT_UYVY:
  1153. case V4L2_PIX_FMT_VYUY:
  1154. case V4L2_PIX_FMT_YUYV:
  1155. case V4L2_PIX_FMT_YVYU:
  1156. cicr1 |= CICR1_COLOR_SP_VAL(2);
  1157. break;
  1158. case V4L2_PIX_FMT_RGB555:
  1159. cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
  1160. CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
  1161. break;
  1162. case V4L2_PIX_FMT_RGB565:
  1163. cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
  1164. break;
  1165. }
  1166. cicr2 = 0;
  1167. cicr3 = CICR3_LPF_VAL(pcdev->current_pix.height - 1) |
  1168. CICR3_BFW_VAL(min((u32)255, y_skip_top));
  1169. cicr4 |= pcdev->mclk_divisor;
  1170. __raw_writel(cicr1, pcdev->base + CICR1);
  1171. __raw_writel(cicr2, pcdev->base + CICR2);
  1172. __raw_writel(cicr3, pcdev->base + CICR3);
  1173. __raw_writel(cicr4, pcdev->base + CICR4);
  1174. /* CIF interrupts are not used, only DMA */
  1175. cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  1176. CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
  1177. cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
  1178. __raw_writel(cicr0, pcdev->base + CICR0);
  1179. }
  1180. /*
  1181. * Videobuf2 section
  1182. */
  1183. static void pxa_buffer_cleanup(struct pxa_buffer *buf)
  1184. {
  1185. int i;
  1186. for (i = 0; i < 3 && buf->descs[i]; i++) {
  1187. dmaengine_desc_free(buf->descs[i]);
  1188. kfree(buf->sg[i]);
  1189. buf->descs[i] = NULL;
  1190. buf->sg[i] = NULL;
  1191. buf->sg_len[i] = 0;
  1192. buf->plane_sizes[i] = 0;
  1193. }
  1194. buf->nb_planes = 0;
  1195. }
  1196. static int pxa_buffer_init(struct pxa_camera_dev *pcdev,
  1197. struct pxa_buffer *buf)
  1198. {
  1199. struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
  1200. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  1201. int nb_channels = pcdev->channels;
  1202. int i, ret = 0;
  1203. unsigned long size = vb2_plane_size(vb, 0);
  1204. switch (nb_channels) {
  1205. case 1:
  1206. buf->plane_sizes[0] = size;
  1207. break;
  1208. case 3:
  1209. buf->plane_sizes[0] = size / 2;
  1210. buf->plane_sizes[1] = size / 4;
  1211. buf->plane_sizes[2] = size / 4;
  1212. break;
  1213. default:
  1214. return -EINVAL;
  1215. };
  1216. buf->nb_planes = nb_channels;
  1217. ret = sg_split(sgt->sgl, sgt->nents, 0, nb_channels,
  1218. buf->plane_sizes, buf->sg, buf->sg_len, GFP_KERNEL);
  1219. if (ret < 0) {
  1220. dev_err(pcdev_to_dev(pcdev),
  1221. "sg_split failed: %d\n", ret);
  1222. return ret;
  1223. }
  1224. for (i = 0; i < nb_channels; i++) {
  1225. ret = pxa_init_dma_channel(pcdev, buf, i,
  1226. buf->sg[i], buf->sg_len[i]);
  1227. if (ret) {
  1228. pxa_buffer_cleanup(buf);
  1229. return ret;
  1230. }
  1231. }
  1232. INIT_LIST_HEAD(&buf->queue);
  1233. return ret;
  1234. }
  1235. static void pxac_vb2_cleanup(struct vb2_buffer *vb)
  1236. {
  1237. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1238. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1239. dev_dbg(pcdev_to_dev(pcdev),
  1240. "%s(vb=%p)\n", __func__, vb);
  1241. pxa_buffer_cleanup(buf);
  1242. }
  1243. static void pxac_vb2_queue(struct vb2_buffer *vb)
  1244. {
  1245. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1246. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1247. dev_dbg(pcdev_to_dev(pcdev),
  1248. "%s(vb=%p) nb_channels=%d size=%lu active=%p\n",
  1249. __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0),
  1250. pcdev->active);
  1251. list_add_tail(&buf->queue, &pcdev->capture);
  1252. pxa_dma_add_tail_buf(pcdev, buf);
  1253. }
  1254. /*
  1255. * Please check the DMA prepared buffer structure in :
  1256. * Documentation/video4linux/pxa_camera.txt
  1257. * Please check also in pxa_camera_check_link_miss() to understand why DMA chain
  1258. * modification while DMA chain is running will work anyway.
  1259. */
  1260. static int pxac_vb2_prepare(struct vb2_buffer *vb)
  1261. {
  1262. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1263. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1264. int ret = 0;
  1265. switch (pcdev->channels) {
  1266. case 1:
  1267. case 3:
  1268. vb2_set_plane_payload(vb, 0, pcdev->current_pix.sizeimage);
  1269. break;
  1270. default:
  1271. return -EINVAL;
  1272. }
  1273. dev_dbg(pcdev_to_dev(pcdev),
  1274. "%s (vb=%p) nb_channels=%d size=%lu\n",
  1275. __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0));
  1276. WARN_ON(!pcdev->current_fmt);
  1277. #ifdef DEBUG
  1278. /*
  1279. * This can be useful if you want to see if we actually fill
  1280. * the buffer with something
  1281. */
  1282. for (i = 0; i < vb->num_planes; i++)
  1283. memset((void *)vb2_plane_vaddr(vb, i),
  1284. 0xaa, vb2_get_plane_payload(vb, i));
  1285. #endif
  1286. /*
  1287. * I think, in buf_prepare you only have to protect global data,
  1288. * the actual buffer is yours
  1289. */
  1290. buf->inwork = 0;
  1291. pxa_videobuf_set_actdma(pcdev, buf);
  1292. return ret;
  1293. }
  1294. static int pxac_vb2_init(struct vb2_buffer *vb)
  1295. {
  1296. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1297. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1298. dev_dbg(pcdev_to_dev(pcdev),
  1299. "%s(nb_channels=%d)\n",
  1300. __func__, pcdev->channels);
  1301. return pxa_buffer_init(pcdev, buf);
  1302. }
  1303. static int pxac_vb2_queue_setup(struct vb2_queue *vq,
  1304. unsigned int *nbufs,
  1305. unsigned int *num_planes, unsigned int sizes[],
  1306. struct device *alloc_devs[])
  1307. {
  1308. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
  1309. int size = pcdev->current_pix.sizeimage;
  1310. dev_dbg(pcdev_to_dev(pcdev),
  1311. "%s(vq=%p nbufs=%d num_planes=%d size=%d)\n",
  1312. __func__, vq, *nbufs, *num_planes, size);
  1313. /*
  1314. * Called from VIDIOC_REQBUFS or in compatibility mode For YUV422P
  1315. * format, even if there are 3 planes Y, U and V, we reply there is only
  1316. * one plane, containing Y, U and V data, one after the other.
  1317. */
  1318. if (*num_planes)
  1319. return sizes[0] < size ? -EINVAL : 0;
  1320. *num_planes = 1;
  1321. switch (pcdev->channels) {
  1322. case 1:
  1323. case 3:
  1324. sizes[0] = size;
  1325. break;
  1326. default:
  1327. return -EINVAL;
  1328. }
  1329. if (!*nbufs)
  1330. *nbufs = 1;
  1331. return 0;
  1332. }
  1333. static int pxac_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
  1334. {
  1335. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
  1336. dev_dbg(pcdev_to_dev(pcdev), "%s(count=%d) active=%p\n",
  1337. __func__, count, pcdev->active);
  1338. pcdev->buf_sequence = 0;
  1339. if (!pcdev->active)
  1340. pxa_camera_start_capture(pcdev);
  1341. return 0;
  1342. }
  1343. static void pxac_vb2_stop_streaming(struct vb2_queue *vq)
  1344. {
  1345. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
  1346. struct pxa_buffer *buf, *tmp;
  1347. dev_dbg(pcdev_to_dev(pcdev), "%s active=%p\n",
  1348. __func__, pcdev->active);
  1349. pxa_camera_stop_capture(pcdev);
  1350. list_for_each_entry_safe(buf, tmp, &pcdev->capture, queue)
  1351. pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_ERROR);
  1352. }
  1353. static struct vb2_ops pxac_vb2_ops = {
  1354. .queue_setup = pxac_vb2_queue_setup,
  1355. .buf_init = pxac_vb2_init,
  1356. .buf_prepare = pxac_vb2_prepare,
  1357. .buf_queue = pxac_vb2_queue,
  1358. .buf_cleanup = pxac_vb2_cleanup,
  1359. .start_streaming = pxac_vb2_start_streaming,
  1360. .stop_streaming = pxac_vb2_stop_streaming,
  1361. .wait_prepare = vb2_ops_wait_prepare,
  1362. .wait_finish = vb2_ops_wait_finish,
  1363. };
  1364. static int pxa_camera_init_videobuf2(struct pxa_camera_dev *pcdev)
  1365. {
  1366. int ret;
  1367. struct vb2_queue *vq = &pcdev->vb2_vq;
  1368. memset(vq, 0, sizeof(*vq));
  1369. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1370. vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1371. vq->drv_priv = pcdev;
  1372. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1373. vq->buf_struct_size = sizeof(struct pxa_buffer);
  1374. vq->dev = pcdev->v4l2_dev.dev;
  1375. vq->ops = &pxac_vb2_ops;
  1376. vq->mem_ops = &vb2_dma_sg_memops;
  1377. vq->lock = &pcdev->mlock;
  1378. ret = vb2_queue_init(vq);
  1379. dev_dbg(pcdev_to_dev(pcdev),
  1380. "vb2_queue_init(vq=%p): %d\n", vq, ret);
  1381. return ret;
  1382. }
  1383. /*
  1384. * Video ioctls section
  1385. */
  1386. static int pxa_camera_set_bus_param(struct pxa_camera_dev *pcdev)
  1387. {
  1388. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  1389. u32 pixfmt = pcdev->current_fmt->host_fmt->fourcc;
  1390. unsigned long bus_flags, common_flags;
  1391. int ret;
  1392. ret = test_platform_param(pcdev,
  1393. pcdev->current_fmt->host_fmt->bits_per_sample,
  1394. &bus_flags);
  1395. if (ret < 0)
  1396. return ret;
  1397. ret = sensor_call(pcdev, video, g_mbus_config, &cfg);
  1398. if (!ret) {
  1399. common_flags = pxa_mbus_config_compatible(&cfg,
  1400. bus_flags);
  1401. if (!common_flags) {
  1402. dev_warn(pcdev_to_dev(pcdev),
  1403. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  1404. cfg.flags, bus_flags);
  1405. return -EINVAL;
  1406. }
  1407. } else if (ret != -ENOIOCTLCMD) {
  1408. return ret;
  1409. } else {
  1410. common_flags = bus_flags;
  1411. }
  1412. pcdev->channels = 1;
  1413. /* Make choises, based on platform preferences */
  1414. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  1415. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  1416. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  1417. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  1418. else
  1419. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  1420. }
  1421. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  1422. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  1423. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  1424. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  1425. else
  1426. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  1427. }
  1428. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  1429. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  1430. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  1431. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  1432. else
  1433. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  1434. }
  1435. cfg.flags = common_flags;
  1436. ret = sensor_call(pcdev, video, s_mbus_config, &cfg);
  1437. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1438. dev_dbg(pcdev_to_dev(pcdev),
  1439. "camera s_mbus_config(0x%lx) returned %d\n",
  1440. common_flags, ret);
  1441. return ret;
  1442. }
  1443. pxa_camera_setup_cicr(pcdev, common_flags, pixfmt);
  1444. return 0;
  1445. }
  1446. static int pxa_camera_try_bus_param(struct pxa_camera_dev *pcdev,
  1447. unsigned char buswidth)
  1448. {
  1449. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  1450. unsigned long bus_flags, common_flags;
  1451. int ret = test_platform_param(pcdev, buswidth, &bus_flags);
  1452. if (ret < 0)
  1453. return ret;
  1454. ret = sensor_call(pcdev, video, g_mbus_config, &cfg);
  1455. if (!ret) {
  1456. common_flags = pxa_mbus_config_compatible(&cfg,
  1457. bus_flags);
  1458. if (!common_flags) {
  1459. dev_warn(pcdev_to_dev(pcdev),
  1460. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  1461. cfg.flags, bus_flags);
  1462. return -EINVAL;
  1463. }
  1464. } else if (ret == -ENOIOCTLCMD) {
  1465. ret = 0;
  1466. }
  1467. return ret;
  1468. }
  1469. static const struct pxa_mbus_pixelfmt pxa_camera_formats[] = {
  1470. {
  1471. .fourcc = V4L2_PIX_FMT_YUV422P,
  1472. .name = "Planar YUV422 16 bit",
  1473. .bits_per_sample = 8,
  1474. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  1475. .order = PXA_MBUS_ORDER_LE,
  1476. .layout = PXA_MBUS_LAYOUT_PLANAR_2Y_U_V,
  1477. },
  1478. };
  1479. /* This will be corrected as we get more formats */
  1480. static bool pxa_camera_packing_supported(const struct pxa_mbus_pixelfmt *fmt)
  1481. {
  1482. return fmt->packing == PXA_MBUS_PACKING_NONE ||
  1483. (fmt->bits_per_sample == 8 &&
  1484. fmt->packing == PXA_MBUS_PACKING_2X8_PADHI) ||
  1485. (fmt->bits_per_sample > 8 &&
  1486. fmt->packing == PXA_MBUS_PACKING_EXTEND16);
  1487. }
  1488. static int pxa_camera_get_formats(struct v4l2_device *v4l2_dev,
  1489. unsigned int idx,
  1490. struct soc_camera_format_xlate *xlate)
  1491. {
  1492. struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev);
  1493. int formats = 0, ret;
  1494. struct v4l2_subdev_mbus_code_enum code = {
  1495. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1496. .index = idx,
  1497. };
  1498. const struct pxa_mbus_pixelfmt *fmt;
  1499. ret = sensor_call(pcdev, pad, enum_mbus_code, NULL, &code);
  1500. if (ret < 0)
  1501. /* No more formats */
  1502. return 0;
  1503. fmt = pxa_mbus_get_fmtdesc(code.code);
  1504. if (!fmt) {
  1505. dev_err(pcdev_to_dev(pcdev),
  1506. "Invalid format code #%u: %d\n", idx, code.code);
  1507. return 0;
  1508. }
  1509. /* This also checks support for the requested bits-per-sample */
  1510. ret = pxa_camera_try_bus_param(pcdev, fmt->bits_per_sample);
  1511. if (ret < 0)
  1512. return 0;
  1513. switch (code.code) {
  1514. case MEDIA_BUS_FMT_UYVY8_2X8:
  1515. formats++;
  1516. if (xlate) {
  1517. xlate->host_fmt = &pxa_camera_formats[0];
  1518. xlate->code = code.code;
  1519. xlate++;
  1520. dev_dbg(pcdev_to_dev(pcdev),
  1521. "Providing format %s using code %d\n",
  1522. pxa_camera_formats[0].name, code.code);
  1523. }
  1524. /* fall through */
  1525. case MEDIA_BUS_FMT_VYUY8_2X8:
  1526. case MEDIA_BUS_FMT_YUYV8_2X8:
  1527. case MEDIA_BUS_FMT_YVYU8_2X8:
  1528. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  1529. case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
  1530. if (xlate)
  1531. dev_dbg(pcdev_to_dev(pcdev),
  1532. "Providing format %s packed\n",
  1533. fmt->name);
  1534. break;
  1535. default:
  1536. if (!pxa_camera_packing_supported(fmt))
  1537. return 0;
  1538. if (xlate)
  1539. dev_dbg(pcdev_to_dev(pcdev),
  1540. "Providing format %s in pass-through mode\n",
  1541. fmt->name);
  1542. break;
  1543. }
  1544. /* Generic pass-through */
  1545. formats++;
  1546. if (xlate) {
  1547. xlate->host_fmt = fmt;
  1548. xlate->code = code.code;
  1549. xlate++;
  1550. }
  1551. return formats;
  1552. }
  1553. static int pxa_camera_build_formats(struct pxa_camera_dev *pcdev)
  1554. {
  1555. struct soc_camera_format_xlate *xlate;
  1556. xlate = pxa_mbus_build_fmts_xlate(&pcdev->v4l2_dev, pcdev->sensor,
  1557. pxa_camera_get_formats);
  1558. if (IS_ERR(xlate))
  1559. return PTR_ERR(xlate);
  1560. pcdev->user_formats = xlate;
  1561. return 0;
  1562. }
  1563. static void pxa_camera_destroy_formats(struct pxa_camera_dev *pcdev)
  1564. {
  1565. kfree(pcdev->user_formats);
  1566. }
  1567. static int pxa_camera_check_frame(u32 width, u32 height)
  1568. {
  1569. /* limit to pxa hardware capabilities */
  1570. return height < 32 || height > 2048 || width < 48 || width > 2048 ||
  1571. (width & 0x01);
  1572. }
  1573. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1574. static int pxac_vidioc_g_register(struct file *file, void *priv,
  1575. struct v4l2_dbg_register *reg)
  1576. {
  1577. struct pxa_camera_dev *pcdev = video_drvdata(file);
  1578. if (reg->reg > CIBR2)
  1579. return -ERANGE;
  1580. reg->val = __raw_readl(pcdev->base + reg->reg);
  1581. reg->size = sizeof(__u32);
  1582. return 0;
  1583. }
  1584. static int pxac_vidioc_s_register(struct file *file, void *priv,
  1585. const struct v4l2_dbg_register *reg)
  1586. {
  1587. struct pxa_camera_dev *pcdev = video_drvdata(file);
  1588. if (reg->reg > CIBR2)
  1589. return -ERANGE;
  1590. if (reg->size != sizeof(__u32))
  1591. return -EINVAL;
  1592. __raw_writel(reg->val, pcdev->base + reg->reg);
  1593. return 0;
  1594. }
  1595. #endif
  1596. static int pxac_vidioc_enum_fmt_vid_cap(struct file *filp, void *priv,
  1597. struct v4l2_fmtdesc *f)
  1598. {
  1599. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1600. const struct pxa_mbus_pixelfmt *format;
  1601. unsigned int idx;
  1602. for (idx = 0; pcdev->user_formats[idx].code; idx++);
  1603. if (f->index >= idx)
  1604. return -EINVAL;
  1605. format = pcdev->user_formats[f->index].host_fmt;
  1606. f->pixelformat = format->fourcc;
  1607. return 0;
  1608. }
  1609. static int pxac_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  1610. struct v4l2_format *f)
  1611. {
  1612. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1613. struct v4l2_pix_format *pix = &f->fmt.pix;
  1614. pix->width = pcdev->current_pix.width;
  1615. pix->height = pcdev->current_pix.height;
  1616. pix->bytesperline = pcdev->current_pix.bytesperline;
  1617. pix->sizeimage = pcdev->current_pix.sizeimage;
  1618. pix->field = pcdev->current_pix.field;
  1619. pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc;
  1620. pix->colorspace = pcdev->current_pix.colorspace;
  1621. dev_dbg(pcdev_to_dev(pcdev), "current_fmt->fourcc: 0x%08x\n",
  1622. pcdev->current_fmt->host_fmt->fourcc);
  1623. return 0;
  1624. }
  1625. static int pxac_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  1626. struct v4l2_format *f)
  1627. {
  1628. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1629. const struct soc_camera_format_xlate *xlate;
  1630. struct v4l2_pix_format *pix = &f->fmt.pix;
  1631. struct v4l2_subdev_pad_config pad_cfg;
  1632. struct v4l2_subdev_format format = {
  1633. .which = V4L2_SUBDEV_FORMAT_TRY,
  1634. };
  1635. struct v4l2_mbus_framefmt *mf = &format.format;
  1636. __u32 pixfmt = pix->pixelformat;
  1637. int ret;
  1638. xlate = pxa_mbus_xlate_by_fourcc(pcdev->user_formats, pixfmt);
  1639. if (!xlate) {
  1640. dev_warn(pcdev_to_dev(pcdev), "Format %x not found\n", pixfmt);
  1641. return -EINVAL;
  1642. }
  1643. /*
  1644. * Limit to pxa hardware capabilities. YUV422P planar format requires
  1645. * images size to be a multiple of 16 bytes. If not, zeros will be
  1646. * inserted between Y and U planes, and U and V planes, which violates
  1647. * the YUV422P standard.
  1648. */
  1649. v4l_bound_align_image(&pix->width, 48, 2048, 1,
  1650. &pix->height, 32, 2048, 0,
  1651. pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
  1652. v4l2_fill_mbus_format(mf, pix, xlate->code);
  1653. ret = sensor_call(pcdev, pad, set_fmt, &pad_cfg, &format);
  1654. if (ret < 0)
  1655. return ret;
  1656. v4l2_fill_pix_format(pix, mf);
  1657. /* Only progressive video supported so far */
  1658. switch (mf->field) {
  1659. case V4L2_FIELD_ANY:
  1660. case V4L2_FIELD_NONE:
  1661. pix->field = V4L2_FIELD_NONE;
  1662. break;
  1663. default:
  1664. /* TODO: support interlaced at least in pass-through mode */
  1665. dev_err(pcdev_to_dev(pcdev), "Field type %d unsupported.\n",
  1666. mf->field);
  1667. return -EINVAL;
  1668. }
  1669. ret = pxa_mbus_bytes_per_line(pix->width, xlate->host_fmt);
  1670. if (ret < 0)
  1671. return ret;
  1672. pix->bytesperline = ret;
  1673. ret = pxa_mbus_image_size(xlate->host_fmt, pix->bytesperline,
  1674. pix->height);
  1675. if (ret < 0)
  1676. return ret;
  1677. pix->sizeimage = ret;
  1678. return 0;
  1679. }
  1680. static int pxac_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  1681. struct v4l2_format *f)
  1682. {
  1683. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1684. const struct soc_camera_format_xlate *xlate;
  1685. struct v4l2_pix_format *pix = &f->fmt.pix;
  1686. struct v4l2_subdev_format format = {
  1687. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1688. };
  1689. unsigned long flags;
  1690. int ret, is_busy;
  1691. dev_dbg(pcdev_to_dev(pcdev),
  1692. "s_fmt_vid_cap(pix=%dx%d:%x)\n",
  1693. pix->width, pix->height, pix->pixelformat);
  1694. spin_lock_irqsave(&pcdev->lock, flags);
  1695. is_busy = pcdev->active || vb2_is_busy(&pcdev->vb2_vq);
  1696. spin_unlock_irqrestore(&pcdev->lock, flags);
  1697. if (is_busy)
  1698. return -EBUSY;
  1699. ret = pxac_vidioc_try_fmt_vid_cap(filp, priv, f);
  1700. if (ret)
  1701. return ret;
  1702. xlate = pxa_mbus_xlate_by_fourcc(pcdev->user_formats,
  1703. pix->pixelformat);
  1704. v4l2_fill_mbus_format(&format.format, pix, xlate->code);
  1705. ret = sensor_call(pcdev, pad, set_fmt, NULL, &format);
  1706. if (ret < 0) {
  1707. dev_warn(pcdev_to_dev(pcdev),
  1708. "Failed to configure for format %x\n",
  1709. pix->pixelformat);
  1710. } else if (pxa_camera_check_frame(pix->width, pix->height)) {
  1711. dev_warn(pcdev_to_dev(pcdev),
  1712. "Camera driver produced an unsupported frame %dx%d\n",
  1713. pix->width, pix->height);
  1714. return -EINVAL;
  1715. }
  1716. pcdev->current_fmt = xlate;
  1717. pcdev->current_pix = *pix;
  1718. ret = pxa_camera_set_bus_param(pcdev);
  1719. return ret;
  1720. }
  1721. static int pxac_vidioc_querycap(struct file *file, void *priv,
  1722. struct v4l2_capability *cap)
  1723. {
  1724. strlcpy(cap->bus_info, "platform:pxa-camera", sizeof(cap->bus_info));
  1725. strlcpy(cap->driver, PXA_CAM_DRV_NAME, sizeof(cap->driver));
  1726. strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
  1727. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1728. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1729. return 0;
  1730. }
  1731. static int pxac_vidioc_enum_input(struct file *file, void *priv,
  1732. struct v4l2_input *i)
  1733. {
  1734. if (i->index > 0)
  1735. return -EINVAL;
  1736. i->type = V4L2_INPUT_TYPE_CAMERA;
  1737. strlcpy(i->name, "Camera", sizeof(i->name));
  1738. return 0;
  1739. }
  1740. static int pxac_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1741. {
  1742. *i = 0;
  1743. return 0;
  1744. }
  1745. static int pxac_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1746. {
  1747. if (i > 0)
  1748. return -EINVAL;
  1749. return 0;
  1750. }
  1751. static int pxac_fops_camera_open(struct file *filp)
  1752. {
  1753. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1754. int ret;
  1755. mutex_lock(&pcdev->mlock);
  1756. ret = v4l2_fh_open(filp);
  1757. if (ret < 0)
  1758. goto out;
  1759. ret = sensor_call(pcdev, core, s_power, 1);
  1760. if (ret)
  1761. v4l2_fh_release(filp);
  1762. out:
  1763. mutex_unlock(&pcdev->mlock);
  1764. return ret;
  1765. }
  1766. static int pxac_fops_camera_release(struct file *filp)
  1767. {
  1768. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1769. int ret;
  1770. ret = vb2_fop_release(filp);
  1771. if (ret < 0)
  1772. return ret;
  1773. mutex_lock(&pcdev->mlock);
  1774. ret = sensor_call(pcdev, core, s_power, 0);
  1775. mutex_unlock(&pcdev->mlock);
  1776. return ret;
  1777. }
  1778. static const struct v4l2_file_operations pxa_camera_fops = {
  1779. .owner = THIS_MODULE,
  1780. .open = pxac_fops_camera_open,
  1781. .release = pxac_fops_camera_release,
  1782. .read = vb2_fop_read,
  1783. .poll = vb2_fop_poll,
  1784. .mmap = vb2_fop_mmap,
  1785. .unlocked_ioctl = video_ioctl2,
  1786. };
  1787. static const struct v4l2_ioctl_ops pxa_camera_ioctl_ops = {
  1788. .vidioc_querycap = pxac_vidioc_querycap,
  1789. .vidioc_enum_input = pxac_vidioc_enum_input,
  1790. .vidioc_g_input = pxac_vidioc_g_input,
  1791. .vidioc_s_input = pxac_vidioc_s_input,
  1792. .vidioc_enum_fmt_vid_cap = pxac_vidioc_enum_fmt_vid_cap,
  1793. .vidioc_g_fmt_vid_cap = pxac_vidioc_g_fmt_vid_cap,
  1794. .vidioc_s_fmt_vid_cap = pxac_vidioc_s_fmt_vid_cap,
  1795. .vidioc_try_fmt_vid_cap = pxac_vidioc_try_fmt_vid_cap,
  1796. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1797. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1798. .vidioc_querybuf = vb2_ioctl_querybuf,
  1799. .vidioc_qbuf = vb2_ioctl_qbuf,
  1800. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1801. .vidioc_expbuf = vb2_ioctl_expbuf,
  1802. .vidioc_streamon = vb2_ioctl_streamon,
  1803. .vidioc_streamoff = vb2_ioctl_streamoff,
  1804. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1805. .vidioc_g_register = pxac_vidioc_g_register,
  1806. .vidioc_s_register = pxac_vidioc_s_register,
  1807. #endif
  1808. };
  1809. static struct v4l2_clk_ops pxa_camera_mclk_ops = {
  1810. };
  1811. static const struct video_device pxa_camera_videodev_template = {
  1812. .name = "pxa-camera",
  1813. .minor = -1,
  1814. .fops = &pxa_camera_fops,
  1815. .ioctl_ops = &pxa_camera_ioctl_ops,
  1816. .release = video_device_release_empty,
  1817. .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING,
  1818. };
  1819. static int pxa_camera_sensor_bound(struct v4l2_async_notifier *notifier,
  1820. struct v4l2_subdev *subdev,
  1821. struct v4l2_async_subdev *asd)
  1822. {
  1823. int err;
  1824. struct v4l2_device *v4l2_dev = notifier->v4l2_dev;
  1825. struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev);
  1826. struct video_device *vdev = &pcdev->vdev;
  1827. struct v4l2_pix_format *pix = &pcdev->current_pix;
  1828. struct v4l2_subdev_format format = {
  1829. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1830. };
  1831. struct v4l2_mbus_framefmt *mf = &format.format;
  1832. dev_info(pcdev_to_dev(pcdev), "%s(): trying to bind a device\n",
  1833. __func__);
  1834. mutex_lock(&pcdev->mlock);
  1835. *vdev = pxa_camera_videodev_template;
  1836. vdev->v4l2_dev = v4l2_dev;
  1837. vdev->lock = &pcdev->mlock;
  1838. pcdev->sensor = subdev;
  1839. pcdev->vdev.queue = &pcdev->vb2_vq;
  1840. pcdev->vdev.v4l2_dev = &pcdev->v4l2_dev;
  1841. pcdev->vdev.ctrl_handler = subdev->ctrl_handler;
  1842. video_set_drvdata(&pcdev->vdev, pcdev);
  1843. err = pxa_camera_build_formats(pcdev);
  1844. if (err) {
  1845. dev_err(pcdev_to_dev(pcdev), "building formats failed: %d\n",
  1846. err);
  1847. goto out;
  1848. }
  1849. pcdev->current_fmt = pcdev->user_formats;
  1850. pix->field = V4L2_FIELD_NONE;
  1851. pix->width = DEFAULT_WIDTH;
  1852. pix->height = DEFAULT_HEIGHT;
  1853. pix->bytesperline =
  1854. pxa_mbus_bytes_per_line(pix->width,
  1855. pcdev->current_fmt->host_fmt);
  1856. pix->sizeimage =
  1857. pxa_mbus_image_size(pcdev->current_fmt->host_fmt,
  1858. pix->bytesperline, pix->height);
  1859. pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc;
  1860. v4l2_fill_mbus_format(mf, pix, pcdev->current_fmt->code);
  1861. err = sensor_call(pcdev, pad, set_fmt, NULL, &format);
  1862. if (err)
  1863. goto out;
  1864. v4l2_fill_pix_format(pix, mf);
  1865. pr_info("%s(): colorspace=0x%x pixfmt=0x%x\n",
  1866. __func__, pix->colorspace, pix->pixelformat);
  1867. err = pxa_camera_init_videobuf2(pcdev);
  1868. if (err)
  1869. goto out;
  1870. err = video_register_device(&pcdev->vdev, VFL_TYPE_GRABBER, -1);
  1871. if (err) {
  1872. v4l2_err(v4l2_dev, "register video device failed: %d\n", err);
  1873. pcdev->sensor = NULL;
  1874. } else {
  1875. dev_info(pcdev_to_dev(pcdev),
  1876. "PXA Camera driver attached to camera %s\n",
  1877. subdev->name);
  1878. }
  1879. out:
  1880. mutex_unlock(&pcdev->mlock);
  1881. return err;
  1882. }
  1883. static void pxa_camera_sensor_unbind(struct v4l2_async_notifier *notifier,
  1884. struct v4l2_subdev *subdev,
  1885. struct v4l2_async_subdev *asd)
  1886. {
  1887. struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(notifier->v4l2_dev);
  1888. mutex_lock(&pcdev->mlock);
  1889. dev_info(pcdev_to_dev(pcdev),
  1890. "PXA Camera driver detached from camera %s\n",
  1891. subdev->name);
  1892. /* disable capture, disable interrupts */
  1893. __raw_writel(0x3ff, pcdev->base + CICR0);
  1894. /* Stop DMA engine */
  1895. pxa_dma_stop_channels(pcdev);
  1896. pxa_camera_destroy_formats(pcdev);
  1897. if (pcdev->mclk_clk) {
  1898. v4l2_clk_unregister(pcdev->mclk_clk);
  1899. pcdev->mclk_clk = NULL;
  1900. }
  1901. video_unregister_device(&pcdev->vdev);
  1902. pcdev->sensor = NULL;
  1903. mutex_unlock(&pcdev->mlock);
  1904. }
  1905. /*
  1906. * Driver probe, remove, suspend and resume operations
  1907. */
  1908. static int pxa_camera_suspend(struct device *dev)
  1909. {
  1910. struct pxa_camera_dev *pcdev = dev_get_drvdata(dev);
  1911. int i = 0, ret = 0;
  1912. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
  1913. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
  1914. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
  1915. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
  1916. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
  1917. if (pcdev->sensor) {
  1918. ret = sensor_call(pcdev, core, s_power, 0);
  1919. if (ret == -ENOIOCTLCMD)
  1920. ret = 0;
  1921. }
  1922. return ret;
  1923. }
  1924. static int pxa_camera_resume(struct device *dev)
  1925. {
  1926. struct pxa_camera_dev *pcdev = dev_get_drvdata(dev);
  1927. int i = 0, ret = 0;
  1928. __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
  1929. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
  1930. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
  1931. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
  1932. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
  1933. if (pcdev->sensor) {
  1934. ret = sensor_call(pcdev, core, s_power, 1);
  1935. if (ret == -ENOIOCTLCMD)
  1936. ret = 0;
  1937. }
  1938. /* Restart frame capture if active buffer exists */
  1939. if (!ret && pcdev->active)
  1940. pxa_camera_start_capture(pcdev);
  1941. return ret;
  1942. }
  1943. static int pxa_camera_pdata_from_dt(struct device *dev,
  1944. struct pxa_camera_dev *pcdev,
  1945. struct v4l2_async_subdev *asd)
  1946. {
  1947. u32 mclk_rate;
  1948. struct device_node *remote, *np = dev->of_node;
  1949. struct v4l2_of_endpoint ep;
  1950. int err = of_property_read_u32(np, "clock-frequency",
  1951. &mclk_rate);
  1952. if (!err) {
  1953. pcdev->platform_flags |= PXA_CAMERA_MCLK_EN;
  1954. pcdev->mclk = mclk_rate;
  1955. }
  1956. np = of_graph_get_next_endpoint(np, NULL);
  1957. if (!np) {
  1958. dev_err(dev, "could not find endpoint\n");
  1959. return -EINVAL;
  1960. }
  1961. err = v4l2_of_parse_endpoint(np, &ep);
  1962. if (err) {
  1963. dev_err(dev, "could not parse endpoint\n");
  1964. goto out;
  1965. }
  1966. switch (ep.bus.parallel.bus_width) {
  1967. case 4:
  1968. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_4;
  1969. break;
  1970. case 5:
  1971. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_5;
  1972. break;
  1973. case 8:
  1974. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_8;
  1975. break;
  1976. case 9:
  1977. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_9;
  1978. break;
  1979. case 10:
  1980. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  1981. break;
  1982. default:
  1983. break;
  1984. }
  1985. if (ep.bus.parallel.flags & V4L2_MBUS_MASTER)
  1986. pcdev->platform_flags |= PXA_CAMERA_MASTER;
  1987. if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  1988. pcdev->platform_flags |= PXA_CAMERA_HSP;
  1989. if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  1990. pcdev->platform_flags |= PXA_CAMERA_VSP;
  1991. if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  1992. pcdev->platform_flags |= PXA_CAMERA_PCLK_EN | PXA_CAMERA_PCP;
  1993. if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  1994. pcdev->platform_flags |= PXA_CAMERA_PCLK_EN;
  1995. asd->match_type = V4L2_ASYNC_MATCH_OF;
  1996. remote = of_graph_get_remote_port(np);
  1997. if (remote) {
  1998. asd->match.of.node = remote;
  1999. of_node_put(remote);
  2000. } else {
  2001. dev_notice(dev, "no remote for %s\n", of_node_full_name(np));
  2002. }
  2003. out:
  2004. of_node_put(np);
  2005. return err;
  2006. }
  2007. static int pxa_camera_probe(struct platform_device *pdev)
  2008. {
  2009. struct pxa_camera_dev *pcdev;
  2010. struct resource *res;
  2011. void __iomem *base;
  2012. struct dma_slave_config config = {
  2013. .src_addr_width = 0,
  2014. .src_maxburst = 8,
  2015. .direction = DMA_DEV_TO_MEM,
  2016. };
  2017. dma_cap_mask_t mask;
  2018. struct pxad_param params;
  2019. char clk_name[V4L2_CLK_NAME_SIZE];
  2020. int irq;
  2021. int err = 0, i;
  2022. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2023. irq = platform_get_irq(pdev, 0);
  2024. if (!res || irq < 0)
  2025. return -ENODEV;
  2026. pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
  2027. if (!pcdev) {
  2028. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  2029. return -ENOMEM;
  2030. }
  2031. pcdev->clk = devm_clk_get(&pdev->dev, NULL);
  2032. if (IS_ERR(pcdev->clk))
  2033. return PTR_ERR(pcdev->clk);
  2034. pcdev->res = res;
  2035. pcdev->pdata = pdev->dev.platform_data;
  2036. if (&pdev->dev.of_node && !pcdev->pdata) {
  2037. err = pxa_camera_pdata_from_dt(&pdev->dev, pcdev, &pcdev->asd);
  2038. } else {
  2039. pcdev->platform_flags = pcdev->pdata->flags;
  2040. pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
  2041. pcdev->asd.match_type = V4L2_ASYNC_MATCH_I2C;
  2042. pcdev->asd.match.i2c.adapter_id =
  2043. pcdev->pdata->sensor_i2c_adapter_id;
  2044. pcdev->asd.match.i2c.address = pcdev->pdata->sensor_i2c_address;
  2045. }
  2046. if (err < 0)
  2047. return err;
  2048. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  2049. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  2050. /*
  2051. * Platform hasn't set available data widths. This is bad.
  2052. * Warn and use a default.
  2053. */
  2054. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  2055. "data widths, using default 10 bit\n");
  2056. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  2057. }
  2058. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8)
  2059. pcdev->width_flags = 1 << 7;
  2060. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9)
  2061. pcdev->width_flags |= 1 << 8;
  2062. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10)
  2063. pcdev->width_flags |= 1 << 9;
  2064. if (!pcdev->mclk) {
  2065. dev_warn(&pdev->dev,
  2066. "mclk == 0! Please, fix your platform data. "
  2067. "Using default 20MHz\n");
  2068. pcdev->mclk = 20000000;
  2069. }
  2070. pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev);
  2071. INIT_LIST_HEAD(&pcdev->capture);
  2072. spin_lock_init(&pcdev->lock);
  2073. mutex_init(&pcdev->mlock);
  2074. /*
  2075. * Request the regions.
  2076. */
  2077. base = devm_ioremap_resource(&pdev->dev, res);
  2078. if (IS_ERR(base))
  2079. return PTR_ERR(base);
  2080. pcdev->irq = irq;
  2081. pcdev->base = base;
  2082. /* request dma */
  2083. dma_cap_zero(mask);
  2084. dma_cap_set(DMA_SLAVE, mask);
  2085. dma_cap_set(DMA_PRIVATE, mask);
  2086. params.prio = 0;
  2087. params.drcmr = 68;
  2088. pcdev->dma_chans[0] =
  2089. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  2090. &params, &pdev->dev, "CI_Y");
  2091. if (!pcdev->dma_chans[0]) {
  2092. dev_err(&pdev->dev, "Can't request DMA for Y\n");
  2093. return -ENODEV;
  2094. }
  2095. params.drcmr = 69;
  2096. pcdev->dma_chans[1] =
  2097. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  2098. &params, &pdev->dev, "CI_U");
  2099. if (!pcdev->dma_chans[1]) {
  2100. dev_err(&pdev->dev, "Can't request DMA for Y\n");
  2101. err = -ENODEV;
  2102. goto exit_free_dma_y;
  2103. }
  2104. params.drcmr = 70;
  2105. pcdev->dma_chans[2] =
  2106. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  2107. &params, &pdev->dev, "CI_V");
  2108. if (!pcdev->dma_chans[2]) {
  2109. dev_err(&pdev->dev, "Can't request DMA for V\n");
  2110. err = -ENODEV;
  2111. goto exit_free_dma_u;
  2112. }
  2113. for (i = 0; i < 3; i++) {
  2114. config.src_addr = pcdev->res->start + CIBR0 + i * 8;
  2115. err = dmaengine_slave_config(pcdev->dma_chans[i], &config);
  2116. if (err < 0) {
  2117. dev_err(&pdev->dev, "dma slave config failed: %d\n",
  2118. err);
  2119. goto exit_free_dma;
  2120. }
  2121. }
  2122. /* request irq */
  2123. err = devm_request_irq(&pdev->dev, pcdev->irq, pxa_camera_irq, 0,
  2124. PXA_CAM_DRV_NAME, pcdev);
  2125. if (err) {
  2126. dev_err(&pdev->dev, "Camera interrupt register failed\n");
  2127. goto exit_free_dma;
  2128. }
  2129. tasklet_init(&pcdev->task_eof, pxa_camera_eof, (unsigned long)pcdev);
  2130. pxa_camera_activate(pcdev);
  2131. dev_set_drvdata(&pdev->dev, pcdev);
  2132. err = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
  2133. if (err)
  2134. goto exit_free_dma;
  2135. pcdev->asds[0] = &pcdev->asd;
  2136. pcdev->notifier.subdevs = pcdev->asds;
  2137. pcdev->notifier.num_subdevs = 1;
  2138. pcdev->notifier.bound = pxa_camera_sensor_bound;
  2139. pcdev->notifier.unbind = pxa_camera_sensor_unbind;
  2140. if (!of_have_populated_dt())
  2141. pcdev->asd.match_type = V4L2_ASYNC_MATCH_I2C;
  2142. err = pxa_camera_init_videobuf2(pcdev);
  2143. if (err)
  2144. goto exit_free_v4l2dev;
  2145. if (pcdev->mclk) {
  2146. v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
  2147. pcdev->asd.match.i2c.adapter_id,
  2148. pcdev->asd.match.i2c.address);
  2149. pcdev->mclk_clk = v4l2_clk_register(&pxa_camera_mclk_ops,
  2150. clk_name, NULL);
  2151. if (IS_ERR(pcdev->mclk_clk)) {
  2152. err = PTR_ERR(pcdev->mclk_clk);
  2153. goto exit_free_v4l2dev;
  2154. }
  2155. }
  2156. err = v4l2_async_notifier_register(&pcdev->v4l2_dev, &pcdev->notifier);
  2157. if (err)
  2158. goto exit_free_clk;
  2159. return 0;
  2160. exit_free_clk:
  2161. v4l2_clk_unregister(pcdev->mclk_clk);
  2162. exit_free_v4l2dev:
  2163. v4l2_device_unregister(&pcdev->v4l2_dev);
  2164. exit_free_dma:
  2165. dma_release_channel(pcdev->dma_chans[2]);
  2166. exit_free_dma_u:
  2167. dma_release_channel(pcdev->dma_chans[1]);
  2168. exit_free_dma_y:
  2169. dma_release_channel(pcdev->dma_chans[0]);
  2170. return err;
  2171. }
  2172. static int pxa_camera_remove(struct platform_device *pdev)
  2173. {
  2174. struct pxa_camera_dev *pcdev = dev_get_drvdata(&pdev->dev);
  2175. pxa_camera_deactivate(pcdev);
  2176. dma_release_channel(pcdev->dma_chans[0]);
  2177. dma_release_channel(pcdev->dma_chans[1]);
  2178. dma_release_channel(pcdev->dma_chans[2]);
  2179. v4l2_async_notifier_unregister(&pcdev->notifier);
  2180. if (pcdev->mclk_clk) {
  2181. v4l2_clk_unregister(pcdev->mclk_clk);
  2182. pcdev->mclk_clk = NULL;
  2183. }
  2184. v4l2_device_unregister(&pcdev->v4l2_dev);
  2185. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  2186. return 0;
  2187. }
  2188. static const struct dev_pm_ops pxa_camera_pm = {
  2189. .suspend = pxa_camera_suspend,
  2190. .resume = pxa_camera_resume,
  2191. };
  2192. static const struct of_device_id pxa_camera_of_match[] = {
  2193. { .compatible = "marvell,pxa270-qci", },
  2194. {},
  2195. };
  2196. MODULE_DEVICE_TABLE(of, pxa_camera_of_match);
  2197. static struct platform_driver pxa_camera_driver = {
  2198. .driver = {
  2199. .name = PXA_CAM_DRV_NAME,
  2200. .pm = &pxa_camera_pm,
  2201. .of_match_table = of_match_ptr(pxa_camera_of_match),
  2202. },
  2203. .probe = pxa_camera_probe,
  2204. .remove = pxa_camera_remove,
  2205. };
  2206. module_platform_driver(pxa_camera_driver);
  2207. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  2208. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  2209. MODULE_LICENSE("GPL");
  2210. MODULE_VERSION(PXA_CAM_VERSION);
  2211. MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME);