pxa_camera.c 69 KB

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