pxa_camera.c 69 KB

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