pxa_camera.c 69 KB

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