pxa_camera.c 69 KB

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