pxa_camera.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598
  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_DPHY:
  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. /*
  657. * PXA27x is only supposed to handle one camera on its Quick Capture
  658. * interface. If anyone ever builds hardware to enable more than
  659. * one camera, they will have to modify this driver too
  660. */
  661. struct clk *clk;
  662. unsigned int irq;
  663. void __iomem *base;
  664. int channels;
  665. struct dma_chan *dma_chans[3];
  666. struct pxacamera_platform_data *pdata;
  667. struct resource *res;
  668. unsigned long platform_flags;
  669. unsigned long ciclk;
  670. unsigned long mclk;
  671. u32 mclk_divisor;
  672. struct v4l2_clk *mclk_clk;
  673. u16 width_flags; /* max 10 bits */
  674. struct list_head capture;
  675. spinlock_t lock;
  676. struct mutex mlock;
  677. unsigned int buf_sequence;
  678. struct pxa_buffer *active;
  679. struct tasklet_struct task_eof;
  680. u32 save_cicr[5];
  681. };
  682. struct pxa_cam {
  683. unsigned long flags;
  684. };
  685. static const char *pxa_cam_driver_description = "PXA_Camera";
  686. /*
  687. * Format translation functions
  688. */
  689. static const struct pxa_camera_format_xlate
  690. *pxa_mbus_xlate_by_fourcc(struct pxa_camera_format_xlate *user_formats,
  691. unsigned int fourcc)
  692. {
  693. unsigned int i;
  694. for (i = 0; user_formats[i].code; i++)
  695. if (user_formats[i].host_fmt->fourcc == fourcc)
  696. return user_formats + i;
  697. return NULL;
  698. }
  699. static struct pxa_camera_format_xlate *pxa_mbus_build_fmts_xlate(
  700. struct v4l2_device *v4l2_dev, struct v4l2_subdev *subdev,
  701. int (*get_formats)(struct v4l2_device *, unsigned int,
  702. struct pxa_camera_format_xlate *xlate))
  703. {
  704. unsigned int i, fmts = 0, raw_fmts = 0;
  705. int ret;
  706. struct v4l2_subdev_mbus_code_enum code = {
  707. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  708. };
  709. struct pxa_camera_format_xlate *user_formats;
  710. while (!v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
  711. raw_fmts++;
  712. code.index++;
  713. }
  714. /*
  715. * First pass - only count formats this host-sensor
  716. * configuration can provide
  717. */
  718. for (i = 0; i < raw_fmts; i++) {
  719. ret = get_formats(v4l2_dev, i, NULL);
  720. if (ret < 0)
  721. return ERR_PTR(ret);
  722. fmts += ret;
  723. }
  724. if (!fmts)
  725. return ERR_PTR(-ENXIO);
  726. user_formats = kcalloc(fmts + 1, sizeof(*user_formats), GFP_KERNEL);
  727. if (!user_formats)
  728. return ERR_PTR(-ENOMEM);
  729. /* Second pass - actually fill data formats */
  730. fmts = 0;
  731. for (i = 0; i < raw_fmts; i++) {
  732. ret = get_formats(v4l2_dev, i, user_formats + fmts);
  733. if (ret < 0)
  734. goto egfmt;
  735. fmts += ret;
  736. }
  737. user_formats[fmts].code = 0;
  738. return user_formats;
  739. egfmt:
  740. kfree(user_formats);
  741. return ERR_PTR(ret);
  742. }
  743. /*
  744. * Videobuf operations
  745. */
  746. static struct pxa_buffer *vb2_to_pxa_buffer(struct vb2_buffer *vb)
  747. {
  748. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  749. return container_of(vbuf, struct pxa_buffer, vbuf);
  750. }
  751. static struct device *pcdev_to_dev(struct pxa_camera_dev *pcdev)
  752. {
  753. return pcdev->v4l2_dev.dev;
  754. }
  755. static struct pxa_camera_dev *v4l2_dev_to_pcdev(struct v4l2_device *v4l2_dev)
  756. {
  757. return container_of(v4l2_dev, struct pxa_camera_dev, v4l2_dev);
  758. }
  759. static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev,
  760. enum pxa_camera_active_dma act_dma);
  761. static void pxa_camera_dma_irq_y(void *data)
  762. {
  763. struct pxa_camera_dev *pcdev = data;
  764. pxa_camera_dma_irq(pcdev, DMA_Y);
  765. }
  766. static void pxa_camera_dma_irq_u(void *data)
  767. {
  768. struct pxa_camera_dev *pcdev = data;
  769. pxa_camera_dma_irq(pcdev, DMA_U);
  770. }
  771. static void pxa_camera_dma_irq_v(void *data)
  772. {
  773. struct pxa_camera_dev *pcdev = data;
  774. pxa_camera_dma_irq(pcdev, DMA_V);
  775. }
  776. /**
  777. * pxa_init_dma_channel - init dma descriptors
  778. * @pcdev: pxa camera device
  779. * @buf: pxa camera buffer
  780. * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
  781. * @sg: dma scatter list
  782. * @sglen: dma scatter list length
  783. *
  784. * Prepares the pxa dma descriptors to transfer one camera channel.
  785. *
  786. * Returns 0 if success or -ENOMEM if no memory is available
  787. */
  788. static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
  789. struct pxa_buffer *buf, int channel,
  790. struct scatterlist *sg, int sglen)
  791. {
  792. struct dma_chan *dma_chan = pcdev->dma_chans[channel];
  793. struct dma_async_tx_descriptor *tx;
  794. tx = dmaengine_prep_slave_sg(dma_chan, sg, sglen, DMA_DEV_TO_MEM,
  795. DMA_PREP_INTERRUPT | DMA_CTRL_REUSE);
  796. if (!tx) {
  797. dev_err(pcdev_to_dev(pcdev),
  798. "dmaengine_prep_slave_sg failed\n");
  799. goto fail;
  800. }
  801. tx->callback_param = pcdev;
  802. switch (channel) {
  803. case 0:
  804. tx->callback = pxa_camera_dma_irq_y;
  805. break;
  806. case 1:
  807. tx->callback = pxa_camera_dma_irq_u;
  808. break;
  809. case 2:
  810. tx->callback = pxa_camera_dma_irq_v;
  811. break;
  812. }
  813. buf->descs[channel] = tx;
  814. return 0;
  815. fail:
  816. dev_dbg(pcdev_to_dev(pcdev),
  817. "%s (vb=%p) dma_tx=%p\n",
  818. __func__, buf, tx);
  819. return -ENOMEM;
  820. }
  821. static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev,
  822. struct pxa_buffer *buf)
  823. {
  824. buf->active_dma = DMA_Y;
  825. if (buf->nb_planes == 3)
  826. buf->active_dma |= DMA_U | DMA_V;
  827. }
  828. /**
  829. * pxa_dma_start_channels - start DMA channel for active buffer
  830. * @pcdev: pxa camera device
  831. *
  832. * Initialize DMA channels to the beginning of the active video buffer, and
  833. * start these channels.
  834. */
  835. static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev)
  836. {
  837. int i;
  838. for (i = 0; i < pcdev->channels; i++) {
  839. dev_dbg(pcdev_to_dev(pcdev),
  840. "%s (channel=%d)\n", __func__, i);
  841. dma_async_issue_pending(pcdev->dma_chans[i]);
  842. }
  843. }
  844. static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev)
  845. {
  846. int i;
  847. for (i = 0; i < pcdev->channels; i++) {
  848. dev_dbg(pcdev_to_dev(pcdev),
  849. "%s (channel=%d)\n", __func__, i);
  850. dmaengine_terminate_all(pcdev->dma_chans[i]);
  851. }
  852. }
  853. static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev,
  854. struct pxa_buffer *buf)
  855. {
  856. int i;
  857. for (i = 0; i < pcdev->channels; i++) {
  858. buf->cookie[i] = dmaengine_submit(buf->descs[i]);
  859. dev_dbg(pcdev_to_dev(pcdev),
  860. "%s (channel=%d) : submit vb=%p cookie=%d\n",
  861. __func__, i, buf, buf->descs[i]->cookie);
  862. }
  863. }
  864. /**
  865. * pxa_camera_start_capture - start video capturing
  866. * @pcdev: camera device
  867. *
  868. * Launch capturing. DMA channels should not be active yet. They should get
  869. * activated at the end of frame interrupt, to capture only whole frames, and
  870. * never begin the capture of a partial frame.
  871. */
  872. static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev)
  873. {
  874. unsigned long cicr0;
  875. dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__);
  876. __raw_writel(__raw_readl(pcdev->base + CISR), pcdev->base + CISR);
  877. /* Enable End-Of-Frame Interrupt */
  878. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
  879. cicr0 &= ~CICR0_EOFM;
  880. __raw_writel(cicr0, pcdev->base + CICR0);
  881. }
  882. static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev)
  883. {
  884. unsigned long cicr0;
  885. pxa_dma_stop_channels(pcdev);
  886. cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
  887. __raw_writel(cicr0, pcdev->base + CICR0);
  888. pcdev->active = NULL;
  889. dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__);
  890. }
  891. static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
  892. struct pxa_buffer *buf,
  893. enum vb2_buffer_state state)
  894. {
  895. struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
  896. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  897. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  898. list_del_init(&buf->queue);
  899. vb->timestamp = ktime_get_ns();
  900. vbuf->sequence = pcdev->buf_sequence++;
  901. vbuf->field = V4L2_FIELD_NONE;
  902. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  903. dev_dbg(pcdev_to_dev(pcdev), "%s dequeued buffer (buf=0x%p)\n",
  904. __func__, buf);
  905. if (list_empty(&pcdev->capture)) {
  906. pxa_camera_stop_capture(pcdev);
  907. return;
  908. }
  909. pcdev->active = list_entry(pcdev->capture.next,
  910. struct pxa_buffer, queue);
  911. }
  912. /**
  913. * pxa_camera_check_link_miss - check missed DMA linking
  914. * @pcdev: camera device
  915. * @last_submitted: an opaque DMA cookie for last submitted
  916. * @last_issued: an opaque DMA cookie for last issued
  917. *
  918. * The DMA chaining is done with DMA running. This means a tiny temporal window
  919. * remains, where a buffer is queued on the chain, while the chain is already
  920. * stopped. This means the tailed buffer would never be transferred by DMA.
  921. * This function restarts the capture for this corner case, where :
  922. * - DADR() == DADDR_STOP
  923. * - a videobuffer is queued on the pcdev->capture list
  924. *
  925. * Please check the "DMA hot chaining timeslice issue" in
  926. * Documentation/media/v4l-drivers/pxa_camera.rst
  927. *
  928. * Context: should only be called within the dma irq handler
  929. */
  930. static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev,
  931. dma_cookie_t last_submitted,
  932. dma_cookie_t last_issued)
  933. {
  934. bool is_dma_stopped = last_submitted != last_issued;
  935. dev_dbg(pcdev_to_dev(pcdev),
  936. "%s : top queued buffer=%p, is_dma_stopped=%d\n",
  937. __func__, pcdev->active, is_dma_stopped);
  938. if (pcdev->active && is_dma_stopped)
  939. pxa_camera_start_capture(pcdev);
  940. }
  941. static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev,
  942. enum pxa_camera_active_dma act_dma)
  943. {
  944. struct pxa_buffer *buf, *last_buf;
  945. unsigned long flags;
  946. u32 camera_status, overrun;
  947. int chan;
  948. enum dma_status last_status;
  949. dma_cookie_t last_issued;
  950. spin_lock_irqsave(&pcdev->lock, flags);
  951. camera_status = __raw_readl(pcdev->base + CISR);
  952. dev_dbg(pcdev_to_dev(pcdev), "camera dma irq, cisr=0x%x dma=%d\n",
  953. camera_status, act_dma);
  954. overrun = CISR_IFO_0;
  955. if (pcdev->channels == 3)
  956. overrun |= CISR_IFO_1 | CISR_IFO_2;
  957. /*
  958. * pcdev->active should not be NULL in DMA irq handler.
  959. *
  960. * But there is one corner case : if capture was stopped due to an
  961. * overrun of channel 1, and at that same channel 2 was completed.
  962. *
  963. * When handling the overrun in DMA irq for channel 1, we'll stop the
  964. * capture and restart it (and thus set pcdev->active to NULL). But the
  965. * DMA irq handler will already be pending for channel 2. So on entering
  966. * the DMA irq handler for channel 2 there will be no active buffer, yet
  967. * that is normal.
  968. */
  969. if (!pcdev->active)
  970. goto out;
  971. buf = pcdev->active;
  972. WARN_ON(buf->inwork || list_empty(&buf->queue));
  973. /*
  974. * It's normal if the last frame creates an overrun, as there
  975. * are no more DMA descriptors to fetch from QCI fifos
  976. */
  977. switch (act_dma) {
  978. case DMA_U:
  979. chan = 1;
  980. break;
  981. case DMA_V:
  982. chan = 2;
  983. break;
  984. default:
  985. chan = 0;
  986. break;
  987. }
  988. last_buf = list_entry(pcdev->capture.prev,
  989. struct pxa_buffer, queue);
  990. last_status = dma_async_is_tx_complete(pcdev->dma_chans[chan],
  991. last_buf->cookie[chan],
  992. NULL, &last_issued);
  993. if (camera_status & overrun &&
  994. last_status != DMA_COMPLETE) {
  995. dev_dbg(pcdev_to_dev(pcdev), "FIFO overrun! CISR: %x\n",
  996. camera_status);
  997. pxa_camera_stop_capture(pcdev);
  998. list_for_each_entry(buf, &pcdev->capture, queue)
  999. pxa_dma_add_tail_buf(pcdev, buf);
  1000. pxa_camera_start_capture(pcdev);
  1001. goto out;
  1002. }
  1003. buf->active_dma &= ~act_dma;
  1004. if (!buf->active_dma) {
  1005. pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_DONE);
  1006. pxa_camera_check_link_miss(pcdev, last_buf->cookie[chan],
  1007. last_issued);
  1008. }
  1009. out:
  1010. spin_unlock_irqrestore(&pcdev->lock, flags);
  1011. }
  1012. static u32 mclk_get_divisor(struct platform_device *pdev,
  1013. struct pxa_camera_dev *pcdev)
  1014. {
  1015. unsigned long mclk = pcdev->mclk;
  1016. u32 div;
  1017. unsigned long lcdclk;
  1018. lcdclk = clk_get_rate(pcdev->clk);
  1019. pcdev->ciclk = lcdclk;
  1020. /* mclk <= ciclk / 4 (27.4.2) */
  1021. if (mclk > lcdclk / 4) {
  1022. mclk = lcdclk / 4;
  1023. dev_warn(&pdev->dev,
  1024. "Limiting master clock to %lu\n", mclk);
  1025. }
  1026. /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
  1027. div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
  1028. /* If we're not supplying MCLK, leave it at 0 */
  1029. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1030. pcdev->mclk = lcdclk / (2 * (div + 1));
  1031. dev_dbg(&pdev->dev, "LCD clock %luHz, target freq %luHz, divisor %u\n",
  1032. lcdclk, mclk, div);
  1033. return div;
  1034. }
  1035. static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
  1036. unsigned long pclk)
  1037. {
  1038. /* We want a timeout > 1 pixel time, not ">=" */
  1039. u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
  1040. __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
  1041. }
  1042. static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
  1043. {
  1044. u32 cicr4 = 0;
  1045. /* disable all interrupts */
  1046. __raw_writel(0x3ff, pcdev->base + CICR0);
  1047. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1048. cicr4 |= CICR4_PCLK_EN;
  1049. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1050. cicr4 |= CICR4_MCLK_EN;
  1051. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  1052. cicr4 |= CICR4_PCP;
  1053. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  1054. cicr4 |= CICR4_HSP;
  1055. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  1056. cicr4 |= CICR4_VSP;
  1057. __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
  1058. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1059. /* Initialise the timeout under the assumption pclk = mclk */
  1060. recalculate_fifo_timeout(pcdev, pcdev->mclk);
  1061. else
  1062. /* "Safe default" - 13MHz */
  1063. recalculate_fifo_timeout(pcdev, 13000000);
  1064. clk_prepare_enable(pcdev->clk);
  1065. }
  1066. static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
  1067. {
  1068. clk_disable_unprepare(pcdev->clk);
  1069. }
  1070. static void pxa_camera_eof(unsigned long arg)
  1071. {
  1072. struct pxa_camera_dev *pcdev = (struct pxa_camera_dev *)arg;
  1073. unsigned long cifr;
  1074. struct pxa_buffer *buf;
  1075. dev_dbg(pcdev_to_dev(pcdev),
  1076. "Camera interrupt status 0x%x\n",
  1077. __raw_readl(pcdev->base + CISR));
  1078. /* Reset the FIFOs */
  1079. cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
  1080. __raw_writel(cifr, pcdev->base + CIFR);
  1081. pcdev->active = list_first_entry(&pcdev->capture,
  1082. struct pxa_buffer, queue);
  1083. buf = pcdev->active;
  1084. pxa_videobuf_set_actdma(pcdev, buf);
  1085. pxa_dma_start_channels(pcdev);
  1086. }
  1087. static irqreturn_t pxa_camera_irq(int irq, void *data)
  1088. {
  1089. struct pxa_camera_dev *pcdev = data;
  1090. unsigned long status, cicr0;
  1091. status = __raw_readl(pcdev->base + CISR);
  1092. dev_dbg(pcdev_to_dev(pcdev),
  1093. "Camera interrupt status 0x%lx\n", status);
  1094. if (!status)
  1095. return IRQ_NONE;
  1096. __raw_writel(status, pcdev->base + CISR);
  1097. if (status & CISR_EOF) {
  1098. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
  1099. __raw_writel(cicr0, pcdev->base + CICR0);
  1100. tasklet_schedule(&pcdev->task_eof);
  1101. }
  1102. return IRQ_HANDLED;
  1103. }
  1104. static int test_platform_param(struct pxa_camera_dev *pcdev,
  1105. unsigned char buswidth, unsigned long *flags)
  1106. {
  1107. /*
  1108. * Platform specified synchronization and pixel clock polarities are
  1109. * only a recommendation and are only used during probing. The PXA270
  1110. * quick capture interface supports both.
  1111. */
  1112. *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  1113. V4L2_MBUS_MASTER : V4L2_MBUS_SLAVE) |
  1114. V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  1115. V4L2_MBUS_HSYNC_ACTIVE_LOW |
  1116. V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  1117. V4L2_MBUS_VSYNC_ACTIVE_LOW |
  1118. V4L2_MBUS_DATA_ACTIVE_HIGH |
  1119. V4L2_MBUS_PCLK_SAMPLE_RISING |
  1120. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  1121. /* If requested data width is supported by the platform, use it */
  1122. if ((1 << (buswidth - 1)) & pcdev->width_flags)
  1123. return 0;
  1124. return -EINVAL;
  1125. }
  1126. static void pxa_camera_setup_cicr(struct pxa_camera_dev *pcdev,
  1127. unsigned long flags, __u32 pixfmt)
  1128. {
  1129. unsigned long dw, bpp;
  1130. u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0, y_skip_top;
  1131. int ret = sensor_call(pcdev, sensor, g_skip_top_lines, &y_skip_top);
  1132. if (ret < 0)
  1133. y_skip_top = 0;
  1134. /*
  1135. * Datawidth is now guaranteed to be equal to one of the three values.
  1136. * We fix bit-per-pixel equal to data-width...
  1137. */
  1138. switch (pcdev->current_fmt->host_fmt->bits_per_sample) {
  1139. case 10:
  1140. dw = 4;
  1141. bpp = 0x40;
  1142. break;
  1143. case 9:
  1144. dw = 3;
  1145. bpp = 0x20;
  1146. break;
  1147. default:
  1148. /*
  1149. * Actually it can only be 8 now,
  1150. * default is just to silence compiler warnings
  1151. */
  1152. case 8:
  1153. dw = 2;
  1154. bpp = 0;
  1155. }
  1156. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1157. cicr4 |= CICR4_PCLK_EN;
  1158. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  1159. cicr4 |= CICR4_MCLK_EN;
  1160. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  1161. cicr4 |= CICR4_PCP;
  1162. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  1163. cicr4 |= CICR4_HSP;
  1164. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  1165. cicr4 |= CICR4_VSP;
  1166. cicr0 = __raw_readl(pcdev->base + CICR0);
  1167. if (cicr0 & CICR0_ENB)
  1168. __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
  1169. cicr1 = CICR1_PPL_VAL(pcdev->current_pix.width - 1) | bpp | dw;
  1170. switch (pixfmt) {
  1171. case V4L2_PIX_FMT_YUV422P:
  1172. pcdev->channels = 3;
  1173. cicr1 |= CICR1_YCBCR_F;
  1174. /*
  1175. * Normally, pxa bus wants as input UYVY format. We allow all
  1176. * reorderings of the YUV422 format, as no processing is done,
  1177. * and the YUV stream is just passed through without any
  1178. * transformation. Note that UYVY is the only format that
  1179. * should be used if pxa framebuffer Overlay2 is used.
  1180. */
  1181. /* fall through */
  1182. case V4L2_PIX_FMT_UYVY:
  1183. case V4L2_PIX_FMT_VYUY:
  1184. case V4L2_PIX_FMT_YUYV:
  1185. case V4L2_PIX_FMT_YVYU:
  1186. cicr1 |= CICR1_COLOR_SP_VAL(2);
  1187. break;
  1188. case V4L2_PIX_FMT_RGB555:
  1189. cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
  1190. CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
  1191. break;
  1192. case V4L2_PIX_FMT_RGB565:
  1193. cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
  1194. break;
  1195. }
  1196. cicr2 = 0;
  1197. cicr3 = CICR3_LPF_VAL(pcdev->current_pix.height - 1) |
  1198. CICR3_BFW_VAL(min((u32)255, y_skip_top));
  1199. cicr4 |= pcdev->mclk_divisor;
  1200. __raw_writel(cicr1, pcdev->base + CICR1);
  1201. __raw_writel(cicr2, pcdev->base + CICR2);
  1202. __raw_writel(cicr3, pcdev->base + CICR3);
  1203. __raw_writel(cicr4, pcdev->base + CICR4);
  1204. /* CIF interrupts are not used, only DMA */
  1205. cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  1206. CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
  1207. cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
  1208. __raw_writel(cicr0, pcdev->base + CICR0);
  1209. }
  1210. /*
  1211. * Videobuf2 section
  1212. */
  1213. static void pxa_buffer_cleanup(struct pxa_buffer *buf)
  1214. {
  1215. int i;
  1216. for (i = 0; i < 3 && buf->descs[i]; i++) {
  1217. dmaengine_desc_free(buf->descs[i]);
  1218. kfree(buf->sg[i]);
  1219. buf->descs[i] = NULL;
  1220. buf->sg[i] = NULL;
  1221. buf->sg_len[i] = 0;
  1222. buf->plane_sizes[i] = 0;
  1223. }
  1224. buf->nb_planes = 0;
  1225. }
  1226. static int pxa_buffer_init(struct pxa_camera_dev *pcdev,
  1227. struct pxa_buffer *buf)
  1228. {
  1229. struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
  1230. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  1231. int nb_channels = pcdev->channels;
  1232. int i, ret = 0;
  1233. unsigned long size = vb2_plane_size(vb, 0);
  1234. switch (nb_channels) {
  1235. case 1:
  1236. buf->plane_sizes[0] = size;
  1237. break;
  1238. case 3:
  1239. buf->plane_sizes[0] = size / 2;
  1240. buf->plane_sizes[1] = size / 4;
  1241. buf->plane_sizes[2] = size / 4;
  1242. break;
  1243. default:
  1244. return -EINVAL;
  1245. };
  1246. buf->nb_planes = nb_channels;
  1247. ret = sg_split(sgt->sgl, sgt->nents, 0, nb_channels,
  1248. buf->plane_sizes, buf->sg, buf->sg_len, GFP_KERNEL);
  1249. if (ret < 0) {
  1250. dev_err(pcdev_to_dev(pcdev),
  1251. "sg_split failed: %d\n", ret);
  1252. return ret;
  1253. }
  1254. for (i = 0; i < nb_channels; i++) {
  1255. ret = pxa_init_dma_channel(pcdev, buf, i,
  1256. buf->sg[i], buf->sg_len[i]);
  1257. if (ret) {
  1258. pxa_buffer_cleanup(buf);
  1259. return ret;
  1260. }
  1261. }
  1262. INIT_LIST_HEAD(&buf->queue);
  1263. return ret;
  1264. }
  1265. static void pxac_vb2_cleanup(struct vb2_buffer *vb)
  1266. {
  1267. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1268. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1269. dev_dbg(pcdev_to_dev(pcdev),
  1270. "%s(vb=%p)\n", __func__, vb);
  1271. pxa_buffer_cleanup(buf);
  1272. }
  1273. static void pxac_vb2_queue(struct vb2_buffer *vb)
  1274. {
  1275. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1276. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1277. dev_dbg(pcdev_to_dev(pcdev),
  1278. "%s(vb=%p) nb_channels=%d size=%lu active=%p\n",
  1279. __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0),
  1280. pcdev->active);
  1281. list_add_tail(&buf->queue, &pcdev->capture);
  1282. pxa_dma_add_tail_buf(pcdev, buf);
  1283. }
  1284. /*
  1285. * Please check the DMA prepared buffer structure in :
  1286. * Documentation/media/v4l-drivers/pxa_camera.rst
  1287. * Please check also in pxa_camera_check_link_miss() to understand why DMA chain
  1288. * modification while DMA chain is running will work anyway.
  1289. */
  1290. static int pxac_vb2_prepare(struct vb2_buffer *vb)
  1291. {
  1292. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1293. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1294. int ret = 0;
  1295. switch (pcdev->channels) {
  1296. case 1:
  1297. case 3:
  1298. vb2_set_plane_payload(vb, 0, pcdev->current_pix.sizeimage);
  1299. break;
  1300. default:
  1301. return -EINVAL;
  1302. }
  1303. dev_dbg(pcdev_to_dev(pcdev),
  1304. "%s (vb=%p) nb_channels=%d size=%lu\n",
  1305. __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0));
  1306. WARN_ON(!pcdev->current_fmt);
  1307. #ifdef DEBUG
  1308. /*
  1309. * This can be useful if you want to see if we actually fill
  1310. * the buffer with something
  1311. */
  1312. for (i = 0; i < vb->num_planes; i++)
  1313. memset((void *)vb2_plane_vaddr(vb, i),
  1314. 0xaa, vb2_get_plane_payload(vb, i));
  1315. #endif
  1316. /*
  1317. * I think, in buf_prepare you only have to protect global data,
  1318. * the actual buffer is yours
  1319. */
  1320. buf->inwork = 0;
  1321. pxa_videobuf_set_actdma(pcdev, buf);
  1322. return ret;
  1323. }
  1324. static int pxac_vb2_init(struct vb2_buffer *vb)
  1325. {
  1326. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
  1327. struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
  1328. dev_dbg(pcdev_to_dev(pcdev),
  1329. "%s(nb_channels=%d)\n",
  1330. __func__, pcdev->channels);
  1331. return pxa_buffer_init(pcdev, buf);
  1332. }
  1333. static int pxac_vb2_queue_setup(struct vb2_queue *vq,
  1334. unsigned int *nbufs,
  1335. unsigned int *num_planes, unsigned int sizes[],
  1336. struct device *alloc_devs[])
  1337. {
  1338. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
  1339. int size = pcdev->current_pix.sizeimage;
  1340. dev_dbg(pcdev_to_dev(pcdev),
  1341. "%s(vq=%p nbufs=%d num_planes=%d size=%d)\n",
  1342. __func__, vq, *nbufs, *num_planes, size);
  1343. /*
  1344. * Called from VIDIOC_REQBUFS or in compatibility mode For YUV422P
  1345. * format, even if there are 3 planes Y, U and V, we reply there is only
  1346. * one plane, containing Y, U and V data, one after the other.
  1347. */
  1348. if (*num_planes)
  1349. return sizes[0] < size ? -EINVAL : 0;
  1350. *num_planes = 1;
  1351. switch (pcdev->channels) {
  1352. case 1:
  1353. case 3:
  1354. sizes[0] = size;
  1355. break;
  1356. default:
  1357. return -EINVAL;
  1358. }
  1359. if (!*nbufs)
  1360. *nbufs = 1;
  1361. return 0;
  1362. }
  1363. static int pxac_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
  1364. {
  1365. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
  1366. dev_dbg(pcdev_to_dev(pcdev), "%s(count=%d) active=%p\n",
  1367. __func__, count, pcdev->active);
  1368. pcdev->buf_sequence = 0;
  1369. if (!pcdev->active)
  1370. pxa_camera_start_capture(pcdev);
  1371. return 0;
  1372. }
  1373. static void pxac_vb2_stop_streaming(struct vb2_queue *vq)
  1374. {
  1375. struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
  1376. struct pxa_buffer *buf, *tmp;
  1377. dev_dbg(pcdev_to_dev(pcdev), "%s active=%p\n",
  1378. __func__, pcdev->active);
  1379. pxa_camera_stop_capture(pcdev);
  1380. list_for_each_entry_safe(buf, tmp, &pcdev->capture, queue)
  1381. pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_ERROR);
  1382. }
  1383. static const struct vb2_ops pxac_vb2_ops = {
  1384. .queue_setup = pxac_vb2_queue_setup,
  1385. .buf_init = pxac_vb2_init,
  1386. .buf_prepare = pxac_vb2_prepare,
  1387. .buf_queue = pxac_vb2_queue,
  1388. .buf_cleanup = pxac_vb2_cleanup,
  1389. .start_streaming = pxac_vb2_start_streaming,
  1390. .stop_streaming = pxac_vb2_stop_streaming,
  1391. .wait_prepare = vb2_ops_wait_prepare,
  1392. .wait_finish = vb2_ops_wait_finish,
  1393. };
  1394. static int pxa_camera_init_videobuf2(struct pxa_camera_dev *pcdev)
  1395. {
  1396. int ret;
  1397. struct vb2_queue *vq = &pcdev->vb2_vq;
  1398. memset(vq, 0, sizeof(*vq));
  1399. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1400. vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1401. vq->drv_priv = pcdev;
  1402. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1403. vq->buf_struct_size = sizeof(struct pxa_buffer);
  1404. vq->dev = pcdev->v4l2_dev.dev;
  1405. vq->ops = &pxac_vb2_ops;
  1406. vq->mem_ops = &vb2_dma_sg_memops;
  1407. vq->lock = &pcdev->mlock;
  1408. ret = vb2_queue_init(vq);
  1409. dev_dbg(pcdev_to_dev(pcdev),
  1410. "vb2_queue_init(vq=%p): %d\n", vq, ret);
  1411. return ret;
  1412. }
  1413. /*
  1414. * Video ioctls section
  1415. */
  1416. static int pxa_camera_set_bus_param(struct pxa_camera_dev *pcdev)
  1417. {
  1418. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  1419. u32 pixfmt = pcdev->current_fmt->host_fmt->fourcc;
  1420. unsigned long bus_flags, common_flags;
  1421. int ret;
  1422. ret = test_platform_param(pcdev,
  1423. pcdev->current_fmt->host_fmt->bits_per_sample,
  1424. &bus_flags);
  1425. if (ret < 0)
  1426. return ret;
  1427. ret = sensor_call(pcdev, video, g_mbus_config, &cfg);
  1428. if (!ret) {
  1429. common_flags = pxa_mbus_config_compatible(&cfg,
  1430. bus_flags);
  1431. if (!common_flags) {
  1432. dev_warn(pcdev_to_dev(pcdev),
  1433. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  1434. cfg.flags, bus_flags);
  1435. return -EINVAL;
  1436. }
  1437. } else if (ret != -ENOIOCTLCMD) {
  1438. return ret;
  1439. } else {
  1440. common_flags = bus_flags;
  1441. }
  1442. pcdev->channels = 1;
  1443. /* Make choises, based on platform preferences */
  1444. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  1445. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  1446. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  1447. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  1448. else
  1449. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  1450. }
  1451. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  1452. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  1453. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  1454. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  1455. else
  1456. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  1457. }
  1458. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  1459. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  1460. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  1461. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  1462. else
  1463. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  1464. }
  1465. cfg.flags = common_flags;
  1466. ret = sensor_call(pcdev, video, s_mbus_config, &cfg);
  1467. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1468. dev_dbg(pcdev_to_dev(pcdev),
  1469. "camera s_mbus_config(0x%lx) returned %d\n",
  1470. common_flags, ret);
  1471. return ret;
  1472. }
  1473. pxa_camera_setup_cicr(pcdev, common_flags, pixfmt);
  1474. return 0;
  1475. }
  1476. static int pxa_camera_try_bus_param(struct pxa_camera_dev *pcdev,
  1477. unsigned char buswidth)
  1478. {
  1479. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  1480. unsigned long bus_flags, common_flags;
  1481. int ret = test_platform_param(pcdev, buswidth, &bus_flags);
  1482. if (ret < 0)
  1483. return ret;
  1484. ret = sensor_call(pcdev, video, g_mbus_config, &cfg);
  1485. if (!ret) {
  1486. common_flags = pxa_mbus_config_compatible(&cfg,
  1487. bus_flags);
  1488. if (!common_flags) {
  1489. dev_warn(pcdev_to_dev(pcdev),
  1490. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  1491. cfg.flags, bus_flags);
  1492. return -EINVAL;
  1493. }
  1494. } else if (ret == -ENOIOCTLCMD) {
  1495. ret = 0;
  1496. }
  1497. return ret;
  1498. }
  1499. static const struct pxa_mbus_pixelfmt pxa_camera_formats[] = {
  1500. {
  1501. .fourcc = V4L2_PIX_FMT_YUV422P,
  1502. .name = "Planar YUV422 16 bit",
  1503. .bits_per_sample = 8,
  1504. .packing = PXA_MBUS_PACKING_2X8_PADHI,
  1505. .order = PXA_MBUS_ORDER_LE,
  1506. .layout = PXA_MBUS_LAYOUT_PLANAR_2Y_U_V,
  1507. },
  1508. };
  1509. /* This will be corrected as we get more formats */
  1510. static bool pxa_camera_packing_supported(const struct pxa_mbus_pixelfmt *fmt)
  1511. {
  1512. return fmt->packing == PXA_MBUS_PACKING_NONE ||
  1513. (fmt->bits_per_sample == 8 &&
  1514. fmt->packing == PXA_MBUS_PACKING_2X8_PADHI) ||
  1515. (fmt->bits_per_sample > 8 &&
  1516. fmt->packing == PXA_MBUS_PACKING_EXTEND16);
  1517. }
  1518. static int pxa_camera_get_formats(struct v4l2_device *v4l2_dev,
  1519. unsigned int idx,
  1520. struct pxa_camera_format_xlate *xlate)
  1521. {
  1522. struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev);
  1523. int formats = 0, ret;
  1524. struct v4l2_subdev_mbus_code_enum code = {
  1525. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1526. .index = idx,
  1527. };
  1528. const struct pxa_mbus_pixelfmt *fmt;
  1529. ret = sensor_call(pcdev, pad, enum_mbus_code, NULL, &code);
  1530. if (ret < 0)
  1531. /* No more formats */
  1532. return 0;
  1533. fmt = pxa_mbus_get_fmtdesc(code.code);
  1534. if (!fmt) {
  1535. dev_err(pcdev_to_dev(pcdev),
  1536. "Invalid format code #%u: %d\n", idx, code.code);
  1537. return 0;
  1538. }
  1539. /* This also checks support for the requested bits-per-sample */
  1540. ret = pxa_camera_try_bus_param(pcdev, fmt->bits_per_sample);
  1541. if (ret < 0)
  1542. return 0;
  1543. switch (code.code) {
  1544. case MEDIA_BUS_FMT_UYVY8_2X8:
  1545. formats++;
  1546. if (xlate) {
  1547. xlate->host_fmt = &pxa_camera_formats[0];
  1548. xlate->code = code.code;
  1549. xlate++;
  1550. dev_dbg(pcdev_to_dev(pcdev),
  1551. "Providing format %s using code %d\n",
  1552. pxa_camera_formats[0].name, code.code);
  1553. }
  1554. /* fall through */
  1555. case MEDIA_BUS_FMT_VYUY8_2X8:
  1556. case MEDIA_BUS_FMT_YUYV8_2X8:
  1557. case MEDIA_BUS_FMT_YVYU8_2X8:
  1558. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  1559. case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
  1560. if (xlate)
  1561. dev_dbg(pcdev_to_dev(pcdev),
  1562. "Providing format %s packed\n",
  1563. fmt->name);
  1564. break;
  1565. default:
  1566. if (!pxa_camera_packing_supported(fmt))
  1567. return 0;
  1568. if (xlate)
  1569. dev_dbg(pcdev_to_dev(pcdev),
  1570. "Providing format %s in pass-through mode\n",
  1571. fmt->name);
  1572. break;
  1573. }
  1574. /* Generic pass-through */
  1575. formats++;
  1576. if (xlate) {
  1577. xlate->host_fmt = fmt;
  1578. xlate->code = code.code;
  1579. xlate++;
  1580. }
  1581. return formats;
  1582. }
  1583. static int pxa_camera_build_formats(struct pxa_camera_dev *pcdev)
  1584. {
  1585. struct pxa_camera_format_xlate *xlate;
  1586. xlate = pxa_mbus_build_fmts_xlate(&pcdev->v4l2_dev, pcdev->sensor,
  1587. pxa_camera_get_formats);
  1588. if (IS_ERR(xlate))
  1589. return PTR_ERR(xlate);
  1590. pcdev->user_formats = xlate;
  1591. return 0;
  1592. }
  1593. static void pxa_camera_destroy_formats(struct pxa_camera_dev *pcdev)
  1594. {
  1595. kfree(pcdev->user_formats);
  1596. }
  1597. static int pxa_camera_check_frame(u32 width, u32 height)
  1598. {
  1599. /* limit to pxa hardware capabilities */
  1600. return height < 32 || height > 2048 || width < 48 || width > 2048 ||
  1601. (width & 0x01);
  1602. }
  1603. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1604. static int pxac_vidioc_g_register(struct file *file, void *priv,
  1605. struct v4l2_dbg_register *reg)
  1606. {
  1607. struct pxa_camera_dev *pcdev = video_drvdata(file);
  1608. if (reg->reg > CIBR2)
  1609. return -ERANGE;
  1610. reg->val = __raw_readl(pcdev->base + reg->reg);
  1611. reg->size = sizeof(__u32);
  1612. return 0;
  1613. }
  1614. static int pxac_vidioc_s_register(struct file *file, void *priv,
  1615. const struct v4l2_dbg_register *reg)
  1616. {
  1617. struct pxa_camera_dev *pcdev = video_drvdata(file);
  1618. if (reg->reg > CIBR2)
  1619. return -ERANGE;
  1620. if (reg->size != sizeof(__u32))
  1621. return -EINVAL;
  1622. __raw_writel(reg->val, pcdev->base + reg->reg);
  1623. return 0;
  1624. }
  1625. #endif
  1626. static int pxac_vidioc_enum_fmt_vid_cap(struct file *filp, void *priv,
  1627. struct v4l2_fmtdesc *f)
  1628. {
  1629. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1630. const struct pxa_mbus_pixelfmt *format;
  1631. unsigned int idx;
  1632. for (idx = 0; pcdev->user_formats[idx].code; idx++);
  1633. if (f->index >= idx)
  1634. return -EINVAL;
  1635. format = pcdev->user_formats[f->index].host_fmt;
  1636. f->pixelformat = format->fourcc;
  1637. return 0;
  1638. }
  1639. static int pxac_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  1640. struct v4l2_format *f)
  1641. {
  1642. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1643. struct v4l2_pix_format *pix = &f->fmt.pix;
  1644. pix->width = pcdev->current_pix.width;
  1645. pix->height = pcdev->current_pix.height;
  1646. pix->bytesperline = pcdev->current_pix.bytesperline;
  1647. pix->sizeimage = pcdev->current_pix.sizeimage;
  1648. pix->field = pcdev->current_pix.field;
  1649. pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc;
  1650. pix->colorspace = pcdev->current_pix.colorspace;
  1651. dev_dbg(pcdev_to_dev(pcdev), "current_fmt->fourcc: 0x%08x\n",
  1652. pcdev->current_fmt->host_fmt->fourcc);
  1653. return 0;
  1654. }
  1655. static int pxac_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  1656. struct v4l2_format *f)
  1657. {
  1658. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1659. const struct pxa_camera_format_xlate *xlate;
  1660. struct v4l2_pix_format *pix = &f->fmt.pix;
  1661. struct v4l2_subdev_pad_config pad_cfg;
  1662. struct v4l2_subdev_format format = {
  1663. .which = V4L2_SUBDEV_FORMAT_TRY,
  1664. };
  1665. struct v4l2_mbus_framefmt *mf = &format.format;
  1666. __u32 pixfmt = pix->pixelformat;
  1667. int ret;
  1668. xlate = pxa_mbus_xlate_by_fourcc(pcdev->user_formats, pixfmt);
  1669. if (!xlate) {
  1670. dev_warn(pcdev_to_dev(pcdev), "Format %x not found\n", pixfmt);
  1671. return -EINVAL;
  1672. }
  1673. /*
  1674. * Limit to pxa hardware capabilities. YUV422P planar format requires
  1675. * images size to be a multiple of 16 bytes. If not, zeros will be
  1676. * inserted between Y and U planes, and U and V planes, which violates
  1677. * the YUV422P standard.
  1678. */
  1679. v4l_bound_align_image(&pix->width, 48, 2048, 1,
  1680. &pix->height, 32, 2048, 0,
  1681. pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
  1682. v4l2_fill_mbus_format(mf, pix, xlate->code);
  1683. ret = sensor_call(pcdev, pad, set_fmt, &pad_cfg, &format);
  1684. if (ret < 0)
  1685. return ret;
  1686. v4l2_fill_pix_format(pix, mf);
  1687. /* Only progressive video supported so far */
  1688. switch (mf->field) {
  1689. case V4L2_FIELD_ANY:
  1690. case V4L2_FIELD_NONE:
  1691. pix->field = V4L2_FIELD_NONE;
  1692. break;
  1693. default:
  1694. /* TODO: support interlaced at least in pass-through mode */
  1695. dev_err(pcdev_to_dev(pcdev), "Field type %d unsupported.\n",
  1696. mf->field);
  1697. return -EINVAL;
  1698. }
  1699. ret = pxa_mbus_bytes_per_line(pix->width, xlate->host_fmt);
  1700. if (ret < 0)
  1701. return ret;
  1702. pix->bytesperline = ret;
  1703. ret = pxa_mbus_image_size(xlate->host_fmt, pix->bytesperline,
  1704. pix->height);
  1705. if (ret < 0)
  1706. return ret;
  1707. pix->sizeimage = ret;
  1708. return 0;
  1709. }
  1710. static int pxac_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  1711. struct v4l2_format *f)
  1712. {
  1713. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1714. const struct pxa_camera_format_xlate *xlate;
  1715. struct v4l2_pix_format *pix = &f->fmt.pix;
  1716. struct v4l2_subdev_format format = {
  1717. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1718. };
  1719. unsigned long flags;
  1720. int ret, is_busy;
  1721. dev_dbg(pcdev_to_dev(pcdev),
  1722. "s_fmt_vid_cap(pix=%dx%d:%x)\n",
  1723. pix->width, pix->height, pix->pixelformat);
  1724. spin_lock_irqsave(&pcdev->lock, flags);
  1725. is_busy = pcdev->active || vb2_is_busy(&pcdev->vb2_vq);
  1726. spin_unlock_irqrestore(&pcdev->lock, flags);
  1727. if (is_busy)
  1728. return -EBUSY;
  1729. ret = pxac_vidioc_try_fmt_vid_cap(filp, priv, f);
  1730. if (ret)
  1731. return ret;
  1732. xlate = pxa_mbus_xlate_by_fourcc(pcdev->user_formats,
  1733. pix->pixelformat);
  1734. v4l2_fill_mbus_format(&format.format, pix, xlate->code);
  1735. ret = sensor_call(pcdev, pad, set_fmt, NULL, &format);
  1736. if (ret < 0) {
  1737. dev_warn(pcdev_to_dev(pcdev),
  1738. "Failed to configure for format %x\n",
  1739. pix->pixelformat);
  1740. } else if (pxa_camera_check_frame(pix->width, pix->height)) {
  1741. dev_warn(pcdev_to_dev(pcdev),
  1742. "Camera driver produced an unsupported frame %dx%d\n",
  1743. pix->width, pix->height);
  1744. return -EINVAL;
  1745. }
  1746. pcdev->current_fmt = xlate;
  1747. pcdev->current_pix = *pix;
  1748. ret = pxa_camera_set_bus_param(pcdev);
  1749. return ret;
  1750. }
  1751. static int pxac_vidioc_querycap(struct file *file, void *priv,
  1752. struct v4l2_capability *cap)
  1753. {
  1754. strscpy(cap->bus_info, "platform:pxa-camera", sizeof(cap->bus_info));
  1755. strscpy(cap->driver, PXA_CAM_DRV_NAME, sizeof(cap->driver));
  1756. strscpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
  1757. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1758. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1759. return 0;
  1760. }
  1761. static int pxac_vidioc_enum_input(struct file *file, void *priv,
  1762. struct v4l2_input *i)
  1763. {
  1764. if (i->index > 0)
  1765. return -EINVAL;
  1766. i->type = V4L2_INPUT_TYPE_CAMERA;
  1767. strscpy(i->name, "Camera", sizeof(i->name));
  1768. return 0;
  1769. }
  1770. static int pxac_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1771. {
  1772. *i = 0;
  1773. return 0;
  1774. }
  1775. static int pxac_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1776. {
  1777. if (i > 0)
  1778. return -EINVAL;
  1779. return 0;
  1780. }
  1781. static int pxac_sensor_set_power(struct pxa_camera_dev *pcdev, int on)
  1782. {
  1783. int ret;
  1784. ret = sensor_call(pcdev, core, s_power, on);
  1785. if (ret == -ENOIOCTLCMD)
  1786. ret = 0;
  1787. if (ret) {
  1788. dev_warn(pcdev_to_dev(pcdev),
  1789. "Failed to put subdevice in %s mode: %d\n",
  1790. on ? "normal operation" : "power saving", ret);
  1791. }
  1792. return ret;
  1793. }
  1794. static int pxac_fops_camera_open(struct file *filp)
  1795. {
  1796. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1797. int ret;
  1798. mutex_lock(&pcdev->mlock);
  1799. ret = v4l2_fh_open(filp);
  1800. if (ret < 0)
  1801. goto out;
  1802. if (!v4l2_fh_is_singular_file(filp))
  1803. goto out;
  1804. ret = pxac_sensor_set_power(pcdev, 1);
  1805. if (ret)
  1806. v4l2_fh_release(filp);
  1807. out:
  1808. mutex_unlock(&pcdev->mlock);
  1809. return ret;
  1810. }
  1811. static int pxac_fops_camera_release(struct file *filp)
  1812. {
  1813. struct pxa_camera_dev *pcdev = video_drvdata(filp);
  1814. int ret;
  1815. bool fh_singular;
  1816. mutex_lock(&pcdev->mlock);
  1817. fh_singular = v4l2_fh_is_singular_file(filp);
  1818. ret = _vb2_fop_release(filp, NULL);
  1819. if (fh_singular)
  1820. ret = pxac_sensor_set_power(pcdev, 0);
  1821. mutex_unlock(&pcdev->mlock);
  1822. return ret;
  1823. }
  1824. static const struct v4l2_file_operations pxa_camera_fops = {
  1825. .owner = THIS_MODULE,
  1826. .open = pxac_fops_camera_open,
  1827. .release = pxac_fops_camera_release,
  1828. .read = vb2_fop_read,
  1829. .poll = vb2_fop_poll,
  1830. .mmap = vb2_fop_mmap,
  1831. .unlocked_ioctl = video_ioctl2,
  1832. };
  1833. static const struct v4l2_ioctl_ops pxa_camera_ioctl_ops = {
  1834. .vidioc_querycap = pxac_vidioc_querycap,
  1835. .vidioc_enum_input = pxac_vidioc_enum_input,
  1836. .vidioc_g_input = pxac_vidioc_g_input,
  1837. .vidioc_s_input = pxac_vidioc_s_input,
  1838. .vidioc_enum_fmt_vid_cap = pxac_vidioc_enum_fmt_vid_cap,
  1839. .vidioc_g_fmt_vid_cap = pxac_vidioc_g_fmt_vid_cap,
  1840. .vidioc_s_fmt_vid_cap = pxac_vidioc_s_fmt_vid_cap,
  1841. .vidioc_try_fmt_vid_cap = pxac_vidioc_try_fmt_vid_cap,
  1842. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1843. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1844. .vidioc_querybuf = vb2_ioctl_querybuf,
  1845. .vidioc_qbuf = vb2_ioctl_qbuf,
  1846. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1847. .vidioc_expbuf = vb2_ioctl_expbuf,
  1848. .vidioc_streamon = vb2_ioctl_streamon,
  1849. .vidioc_streamoff = vb2_ioctl_streamoff,
  1850. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1851. .vidioc_g_register = pxac_vidioc_g_register,
  1852. .vidioc_s_register = pxac_vidioc_s_register,
  1853. #endif
  1854. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1855. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1856. };
  1857. static const struct v4l2_clk_ops pxa_camera_mclk_ops = {
  1858. };
  1859. static const struct video_device pxa_camera_videodev_template = {
  1860. .name = "pxa-camera",
  1861. .minor = -1,
  1862. .fops = &pxa_camera_fops,
  1863. .ioctl_ops = &pxa_camera_ioctl_ops,
  1864. .release = video_device_release_empty,
  1865. .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING,
  1866. };
  1867. static int pxa_camera_sensor_bound(struct v4l2_async_notifier *notifier,
  1868. struct v4l2_subdev *subdev,
  1869. struct v4l2_async_subdev *asd)
  1870. {
  1871. int err;
  1872. struct v4l2_device *v4l2_dev = notifier->v4l2_dev;
  1873. struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev);
  1874. struct video_device *vdev = &pcdev->vdev;
  1875. struct v4l2_pix_format *pix = &pcdev->current_pix;
  1876. struct v4l2_subdev_format format = {
  1877. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1878. };
  1879. struct v4l2_mbus_framefmt *mf = &format.format;
  1880. dev_info(pcdev_to_dev(pcdev), "%s(): trying to bind a device\n",
  1881. __func__);
  1882. mutex_lock(&pcdev->mlock);
  1883. *vdev = pxa_camera_videodev_template;
  1884. vdev->v4l2_dev = v4l2_dev;
  1885. vdev->lock = &pcdev->mlock;
  1886. pcdev->sensor = subdev;
  1887. pcdev->vdev.queue = &pcdev->vb2_vq;
  1888. pcdev->vdev.v4l2_dev = &pcdev->v4l2_dev;
  1889. pcdev->vdev.ctrl_handler = subdev->ctrl_handler;
  1890. video_set_drvdata(&pcdev->vdev, pcdev);
  1891. err = pxa_camera_build_formats(pcdev);
  1892. if (err) {
  1893. dev_err(pcdev_to_dev(pcdev), "building formats failed: %d\n",
  1894. err);
  1895. goto out;
  1896. }
  1897. pcdev->current_fmt = pcdev->user_formats;
  1898. pix->field = V4L2_FIELD_NONE;
  1899. pix->width = DEFAULT_WIDTH;
  1900. pix->height = DEFAULT_HEIGHT;
  1901. pix->bytesperline =
  1902. pxa_mbus_bytes_per_line(pix->width,
  1903. pcdev->current_fmt->host_fmt);
  1904. pix->sizeimage =
  1905. pxa_mbus_image_size(pcdev->current_fmt->host_fmt,
  1906. pix->bytesperline, pix->height);
  1907. pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc;
  1908. v4l2_fill_mbus_format(mf, pix, pcdev->current_fmt->code);
  1909. err = pxac_sensor_set_power(pcdev, 1);
  1910. if (err)
  1911. goto out;
  1912. err = sensor_call(pcdev, pad, set_fmt, NULL, &format);
  1913. if (err)
  1914. goto out_sensor_poweroff;
  1915. v4l2_fill_pix_format(pix, mf);
  1916. pr_info("%s(): colorspace=0x%x pixfmt=0x%x\n",
  1917. __func__, pix->colorspace, pix->pixelformat);
  1918. err = pxa_camera_init_videobuf2(pcdev);
  1919. if (err)
  1920. goto out_sensor_poweroff;
  1921. err = video_register_device(&pcdev->vdev, VFL_TYPE_GRABBER, -1);
  1922. if (err) {
  1923. v4l2_err(v4l2_dev, "register video device failed: %d\n", err);
  1924. pcdev->sensor = NULL;
  1925. } else {
  1926. dev_info(pcdev_to_dev(pcdev),
  1927. "PXA Camera driver attached to camera %s\n",
  1928. subdev->name);
  1929. }
  1930. out_sensor_poweroff:
  1931. err = pxac_sensor_set_power(pcdev, 0);
  1932. out:
  1933. mutex_unlock(&pcdev->mlock);
  1934. return err;
  1935. }
  1936. static void pxa_camera_sensor_unbind(struct v4l2_async_notifier *notifier,
  1937. struct v4l2_subdev *subdev,
  1938. struct v4l2_async_subdev *asd)
  1939. {
  1940. struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(notifier->v4l2_dev);
  1941. mutex_lock(&pcdev->mlock);
  1942. dev_info(pcdev_to_dev(pcdev),
  1943. "PXA Camera driver detached from camera %s\n",
  1944. subdev->name);
  1945. /* disable capture, disable interrupts */
  1946. __raw_writel(0x3ff, pcdev->base + CICR0);
  1947. /* Stop DMA engine */
  1948. pxa_dma_stop_channels(pcdev);
  1949. pxa_camera_destroy_formats(pcdev);
  1950. if (pcdev->mclk_clk) {
  1951. v4l2_clk_unregister(pcdev->mclk_clk);
  1952. pcdev->mclk_clk = NULL;
  1953. }
  1954. video_unregister_device(&pcdev->vdev);
  1955. pcdev->sensor = NULL;
  1956. mutex_unlock(&pcdev->mlock);
  1957. }
  1958. static const struct v4l2_async_notifier_operations pxa_camera_sensor_ops = {
  1959. .bound = pxa_camera_sensor_bound,
  1960. .unbind = pxa_camera_sensor_unbind,
  1961. };
  1962. /*
  1963. * Driver probe, remove, suspend and resume operations
  1964. */
  1965. static int pxa_camera_suspend(struct device *dev)
  1966. {
  1967. struct pxa_camera_dev *pcdev = dev_get_drvdata(dev);
  1968. int i = 0, ret = 0;
  1969. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
  1970. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
  1971. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
  1972. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
  1973. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
  1974. if (pcdev->sensor)
  1975. ret = pxac_sensor_set_power(pcdev, 0);
  1976. return ret;
  1977. }
  1978. static int pxa_camera_resume(struct device *dev)
  1979. {
  1980. struct pxa_camera_dev *pcdev = dev_get_drvdata(dev);
  1981. int i = 0, ret = 0;
  1982. __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
  1983. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
  1984. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
  1985. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
  1986. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
  1987. if (pcdev->sensor) {
  1988. ret = pxac_sensor_set_power(pcdev, 1);
  1989. }
  1990. /* Restart frame capture if active buffer exists */
  1991. if (!ret && pcdev->active)
  1992. pxa_camera_start_capture(pcdev);
  1993. return ret;
  1994. }
  1995. static int pxa_camera_pdata_from_dt(struct device *dev,
  1996. struct pxa_camera_dev *pcdev,
  1997. struct v4l2_async_subdev *asd)
  1998. {
  1999. u32 mclk_rate;
  2000. struct device_node *remote, *np = dev->of_node;
  2001. struct v4l2_fwnode_endpoint ep = { .bus_type = 0 };
  2002. int err = of_property_read_u32(np, "clock-frequency",
  2003. &mclk_rate);
  2004. if (!err) {
  2005. pcdev->platform_flags |= PXA_CAMERA_MCLK_EN;
  2006. pcdev->mclk = mclk_rate;
  2007. }
  2008. np = of_graph_get_next_endpoint(np, NULL);
  2009. if (!np) {
  2010. dev_err(dev, "could not find endpoint\n");
  2011. return -EINVAL;
  2012. }
  2013. err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep);
  2014. if (err) {
  2015. dev_err(dev, "could not parse endpoint\n");
  2016. goto out;
  2017. }
  2018. switch (ep.bus.parallel.bus_width) {
  2019. case 4:
  2020. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_4;
  2021. break;
  2022. case 5:
  2023. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_5;
  2024. break;
  2025. case 8:
  2026. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_8;
  2027. break;
  2028. case 9:
  2029. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_9;
  2030. break;
  2031. case 10:
  2032. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  2033. break;
  2034. default:
  2035. break;
  2036. }
  2037. if (ep.bus.parallel.flags & V4L2_MBUS_MASTER)
  2038. pcdev->platform_flags |= PXA_CAMERA_MASTER;
  2039. if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  2040. pcdev->platform_flags |= PXA_CAMERA_HSP;
  2041. if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  2042. pcdev->platform_flags |= PXA_CAMERA_VSP;
  2043. if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  2044. pcdev->platform_flags |= PXA_CAMERA_PCLK_EN | PXA_CAMERA_PCP;
  2045. if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  2046. pcdev->platform_flags |= PXA_CAMERA_PCLK_EN;
  2047. asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
  2048. remote = of_graph_get_remote_port(np);
  2049. if (remote)
  2050. asd->match.fwnode = of_fwnode_handle(remote);
  2051. else
  2052. dev_notice(dev, "no remote for %pOF\n", np);
  2053. out:
  2054. of_node_put(np);
  2055. return err;
  2056. }
  2057. static int pxa_camera_probe(struct platform_device *pdev)
  2058. {
  2059. struct pxa_camera_dev *pcdev;
  2060. struct resource *res;
  2061. void __iomem *base;
  2062. struct dma_slave_config config = {
  2063. .src_addr_width = 0,
  2064. .src_maxburst = 8,
  2065. .direction = DMA_DEV_TO_MEM,
  2066. };
  2067. char clk_name[V4L2_CLK_NAME_SIZE];
  2068. int irq;
  2069. int err = 0, i;
  2070. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2071. irq = platform_get_irq(pdev, 0);
  2072. if (!res || irq < 0)
  2073. return -ENODEV;
  2074. pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
  2075. if (!pcdev) {
  2076. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  2077. return -ENOMEM;
  2078. }
  2079. pcdev->clk = devm_clk_get(&pdev->dev, NULL);
  2080. if (IS_ERR(pcdev->clk))
  2081. return PTR_ERR(pcdev->clk);
  2082. pcdev->res = res;
  2083. pcdev->pdata = pdev->dev.platform_data;
  2084. if (pdev->dev.of_node && !pcdev->pdata) {
  2085. err = pxa_camera_pdata_from_dt(&pdev->dev, pcdev, &pcdev->asd);
  2086. } else {
  2087. pcdev->platform_flags = pcdev->pdata->flags;
  2088. pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
  2089. pcdev->asd.match_type = V4L2_ASYNC_MATCH_I2C;
  2090. pcdev->asd.match.i2c.adapter_id =
  2091. pcdev->pdata->sensor_i2c_adapter_id;
  2092. pcdev->asd.match.i2c.address = pcdev->pdata->sensor_i2c_address;
  2093. }
  2094. if (err < 0)
  2095. return err;
  2096. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  2097. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  2098. /*
  2099. * Platform hasn't set available data widths. This is bad.
  2100. * Warn and use a default.
  2101. */
  2102. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available data widths, using default 10 bit\n");
  2103. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  2104. }
  2105. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8)
  2106. pcdev->width_flags = 1 << 7;
  2107. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9)
  2108. pcdev->width_flags |= 1 << 8;
  2109. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10)
  2110. pcdev->width_flags |= 1 << 9;
  2111. if (!pcdev->mclk) {
  2112. dev_warn(&pdev->dev,
  2113. "mclk == 0! Please, fix your platform data. Using default 20MHz\n");
  2114. pcdev->mclk = 20000000;
  2115. }
  2116. pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev);
  2117. INIT_LIST_HEAD(&pcdev->capture);
  2118. spin_lock_init(&pcdev->lock);
  2119. mutex_init(&pcdev->mlock);
  2120. /*
  2121. * Request the regions.
  2122. */
  2123. base = devm_ioremap_resource(&pdev->dev, res);
  2124. if (IS_ERR(base))
  2125. return PTR_ERR(base);
  2126. pcdev->irq = irq;
  2127. pcdev->base = base;
  2128. /* request dma */
  2129. pcdev->dma_chans[0] = dma_request_slave_channel(&pdev->dev, "CI_Y");
  2130. if (!pcdev->dma_chans[0]) {
  2131. dev_err(&pdev->dev, "Can't request DMA for Y\n");
  2132. return -ENODEV;
  2133. }
  2134. pcdev->dma_chans[1] = dma_request_slave_channel(&pdev->dev, "CI_U");
  2135. if (!pcdev->dma_chans[1]) {
  2136. dev_err(&pdev->dev, "Can't request DMA for Y\n");
  2137. err = -ENODEV;
  2138. goto exit_free_dma_y;
  2139. }
  2140. pcdev->dma_chans[2] = dma_request_slave_channel(&pdev->dev, "CI_V");
  2141. if (!pcdev->dma_chans[2]) {
  2142. dev_err(&pdev->dev, "Can't request DMA for V\n");
  2143. err = -ENODEV;
  2144. goto exit_free_dma_u;
  2145. }
  2146. for (i = 0; i < 3; i++) {
  2147. config.src_addr = pcdev->res->start + CIBR0 + i * 8;
  2148. err = dmaengine_slave_config(pcdev->dma_chans[i], &config);
  2149. if (err < 0) {
  2150. dev_err(&pdev->dev, "dma slave config failed: %d\n",
  2151. err);
  2152. goto exit_free_dma;
  2153. }
  2154. }
  2155. /* request irq */
  2156. err = devm_request_irq(&pdev->dev, pcdev->irq, pxa_camera_irq, 0,
  2157. PXA_CAM_DRV_NAME, pcdev);
  2158. if (err) {
  2159. dev_err(&pdev->dev, "Camera interrupt register failed\n");
  2160. goto exit_free_dma;
  2161. }
  2162. tasklet_init(&pcdev->task_eof, pxa_camera_eof, (unsigned long)pcdev);
  2163. pxa_camera_activate(pcdev);
  2164. dev_set_drvdata(&pdev->dev, pcdev);
  2165. err = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
  2166. if (err)
  2167. goto exit_deactivate;
  2168. v4l2_async_notifier_init(&pcdev->notifier);
  2169. err = v4l2_async_notifier_add_subdev(&pcdev->notifier, &pcdev->asd);
  2170. if (err) {
  2171. fwnode_handle_put(pcdev->asd.match.fwnode);
  2172. goto exit_free_v4l2dev;
  2173. }
  2174. pcdev->notifier.ops = &pxa_camera_sensor_ops;
  2175. if (!of_have_populated_dt())
  2176. pcdev->asd.match_type = V4L2_ASYNC_MATCH_I2C;
  2177. err = pxa_camera_init_videobuf2(pcdev);
  2178. if (err)
  2179. goto exit_notifier_cleanup;
  2180. if (pcdev->mclk) {
  2181. v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
  2182. pcdev->asd.match.i2c.adapter_id,
  2183. pcdev->asd.match.i2c.address);
  2184. pcdev->mclk_clk = v4l2_clk_register(&pxa_camera_mclk_ops,
  2185. clk_name, NULL);
  2186. if (IS_ERR(pcdev->mclk_clk)) {
  2187. err = PTR_ERR(pcdev->mclk_clk);
  2188. goto exit_notifier_cleanup;
  2189. }
  2190. }
  2191. err = v4l2_async_notifier_register(&pcdev->v4l2_dev, &pcdev->notifier);
  2192. if (err)
  2193. goto exit_free_clk;
  2194. return 0;
  2195. exit_free_clk:
  2196. v4l2_clk_unregister(pcdev->mclk_clk);
  2197. exit_notifier_cleanup:
  2198. v4l2_async_notifier_cleanup(&pcdev->notifier);
  2199. exit_free_v4l2dev:
  2200. v4l2_device_unregister(&pcdev->v4l2_dev);
  2201. exit_deactivate:
  2202. pxa_camera_deactivate(pcdev);
  2203. exit_free_dma:
  2204. dma_release_channel(pcdev->dma_chans[2]);
  2205. exit_free_dma_u:
  2206. dma_release_channel(pcdev->dma_chans[1]);
  2207. exit_free_dma_y:
  2208. dma_release_channel(pcdev->dma_chans[0]);
  2209. return err;
  2210. }
  2211. static int pxa_camera_remove(struct platform_device *pdev)
  2212. {
  2213. struct pxa_camera_dev *pcdev = dev_get_drvdata(&pdev->dev);
  2214. pxa_camera_deactivate(pcdev);
  2215. dma_release_channel(pcdev->dma_chans[0]);
  2216. dma_release_channel(pcdev->dma_chans[1]);
  2217. dma_release_channel(pcdev->dma_chans[2]);
  2218. v4l2_async_notifier_unregister(&pcdev->notifier);
  2219. v4l2_async_notifier_cleanup(&pcdev->notifier);
  2220. if (pcdev->mclk_clk) {
  2221. v4l2_clk_unregister(pcdev->mclk_clk);
  2222. pcdev->mclk_clk = NULL;
  2223. }
  2224. v4l2_device_unregister(&pcdev->v4l2_dev);
  2225. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  2226. return 0;
  2227. }
  2228. static const struct dev_pm_ops pxa_camera_pm = {
  2229. .suspend = pxa_camera_suspend,
  2230. .resume = pxa_camera_resume,
  2231. };
  2232. static const struct of_device_id pxa_camera_of_match[] = {
  2233. { .compatible = "marvell,pxa270-qci", },
  2234. {},
  2235. };
  2236. MODULE_DEVICE_TABLE(of, pxa_camera_of_match);
  2237. static struct platform_driver pxa_camera_driver = {
  2238. .driver = {
  2239. .name = PXA_CAM_DRV_NAME,
  2240. .pm = &pxa_camera_pm,
  2241. .of_match_table = of_match_ptr(pxa_camera_of_match),
  2242. },
  2243. .probe = pxa_camera_probe,
  2244. .remove = pxa_camera_remove,
  2245. };
  2246. module_platform_driver(pxa_camera_driver);
  2247. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  2248. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  2249. MODULE_LICENSE("GPL");
  2250. MODULE_VERSION(PXA_CAM_VERSION);
  2251. MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME);