pxa_camera.c 69 KB

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