pxa_camera.c 65 KB

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