imx-mipi-csis.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Samsung CSIS MIPI CSI-2 receiver driver.
  4. *
  5. * The Samsung CSIS IP is a MIPI CSI-2 receiver found in various NXP i.MX7 and
  6. * i.MX8 SoCs. The i.MX7 features version 3.3 of the IP, while i.MX8 features
  7. * version 3.6.3.
  8. *
  9. * Copyright (C) 2019 Linaro Ltd
  10. * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. All Rights Reserved.
  11. * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/reset.h>
  28. #include <linux/spinlock.h>
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/v4l2-fwnode.h>
  33. #include <media/v4l2-mc.h>
  34. #include <media/v4l2-subdev.h>
  35. #define CSIS_DRIVER_NAME "imx-mipi-csis"
  36. #define CSIS_PAD_SINK 0
  37. #define CSIS_PAD_SOURCE 1
  38. #define CSIS_PADS_NUM 2
  39. #define MIPI_CSIS_DEF_PIX_WIDTH 640
  40. #define MIPI_CSIS_DEF_PIX_HEIGHT 480
  41. /* Register map definition */
  42. /* CSIS version */
  43. #define MIPI_CSIS_VERSION 0x00
  44. #define MIPI_CSIS_VERSION_IMX7D 0x03030505
  45. #define MIPI_CSIS_VERSION_IMX8MP 0x03060301
  46. /* CSIS common control */
  47. #define MIPI_CSIS_CMN_CTRL 0x04
  48. #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW BIT(16)
  49. #define MIPI_CSIS_CMN_CTRL_INTER_MODE BIT(10)
  50. #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL BIT(2)
  51. #define MIPI_CSIS_CMN_CTRL_RESET BIT(1)
  52. #define MIPI_CSIS_CMN_CTRL_ENABLE BIT(0)
  53. #define MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET 8
  54. #define MIPI_CSIS_CMN_CTRL_LANE_NR_MASK (3 << 8)
  55. /* CSIS clock control */
  56. #define MIPI_CSIS_CLK_CTRL 0x08
  57. #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH3(x) ((x) << 28)
  58. #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH2(x) ((x) << 24)
  59. #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH1(x) ((x) << 20)
  60. #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(x) ((x) << 16)
  61. #define MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK (0xf << 4)
  62. #define MIPI_CSIS_CLK_CTRL_WCLK_SRC BIT(0)
  63. /* CSIS Interrupt mask */
  64. #define MIPI_CSIS_INT_MSK 0x10
  65. #define MIPI_CSIS_INT_MSK_EVEN_BEFORE BIT(31)
  66. #define MIPI_CSIS_INT_MSK_EVEN_AFTER BIT(30)
  67. #define MIPI_CSIS_INT_MSK_ODD_BEFORE BIT(29)
  68. #define MIPI_CSIS_INT_MSK_ODD_AFTER BIT(28)
  69. #define MIPI_CSIS_INT_MSK_FRAME_START BIT(24)
  70. #define MIPI_CSIS_INT_MSK_FRAME_END BIT(20)
  71. #define MIPI_CSIS_INT_MSK_ERR_SOT_HS BIT(16)
  72. #define MIPI_CSIS_INT_MSK_ERR_LOST_FS BIT(12)
  73. #define MIPI_CSIS_INT_MSK_ERR_LOST_FE BIT(8)
  74. #define MIPI_CSIS_INT_MSK_ERR_OVER BIT(4)
  75. #define MIPI_CSIS_INT_MSK_ERR_WRONG_CFG BIT(3)
  76. #define MIPI_CSIS_INT_MSK_ERR_ECC BIT(2)
  77. #define MIPI_CSIS_INT_MSK_ERR_CRC BIT(1)
  78. #define MIPI_CSIS_INT_MSK_ERR_UNKNOWN BIT(0)
  79. /* CSIS Interrupt source */
  80. #define MIPI_CSIS_INT_SRC 0x14
  81. #define MIPI_CSIS_INT_SRC_EVEN_BEFORE BIT(31)
  82. #define MIPI_CSIS_INT_SRC_EVEN_AFTER BIT(30)
  83. #define MIPI_CSIS_INT_SRC_EVEN BIT(30)
  84. #define MIPI_CSIS_INT_SRC_ODD_BEFORE BIT(29)
  85. #define MIPI_CSIS_INT_SRC_ODD_AFTER BIT(28)
  86. #define MIPI_CSIS_INT_SRC_ODD (0x3 << 28)
  87. #define MIPI_CSIS_INT_SRC_NON_IMAGE_DATA (0xf << 28)
  88. #define MIPI_CSIS_INT_SRC_FRAME_START BIT(24)
  89. #define MIPI_CSIS_INT_SRC_FRAME_END BIT(20)
  90. #define MIPI_CSIS_INT_SRC_ERR_SOT_HS BIT(16)
  91. #define MIPI_CSIS_INT_SRC_ERR_LOST_FS BIT(12)
  92. #define MIPI_CSIS_INT_SRC_ERR_LOST_FE BIT(8)
  93. #define MIPI_CSIS_INT_SRC_ERR_OVER BIT(4)
  94. #define MIPI_CSIS_INT_SRC_ERR_WRONG_CFG BIT(3)
  95. #define MIPI_CSIS_INT_SRC_ERR_ECC BIT(2)
  96. #define MIPI_CSIS_INT_SRC_ERR_CRC BIT(1)
  97. #define MIPI_CSIS_INT_SRC_ERR_UNKNOWN BIT(0)
  98. #define MIPI_CSIS_INT_SRC_ERRORS 0xfffff
  99. /* D-PHY status control */
  100. #define MIPI_CSIS_DPHY_STATUS 0x20
  101. #define MIPI_CSIS_DPHY_STATUS_ULPS_DAT BIT(8)
  102. #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_DAT BIT(4)
  103. #define MIPI_CSIS_DPHY_STATUS_ULPS_CLK BIT(1)
  104. #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_CLK BIT(0)
  105. /* D-PHY common control */
  106. #define MIPI_CSIS_DPHY_CMN_CTRL 0x24
  107. #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(n) ((n) << 24)
  108. #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE_MASK GENMASK(31, 24)
  109. #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(n) ((n) << 22)
  110. #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE_MASK GENMASK(23, 22)
  111. #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_CLK BIT(6)
  112. #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_DAT BIT(5)
  113. #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_DAT BIT(1)
  114. #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_CLK BIT(0)
  115. #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE (0x1f << 0)
  116. /* D-PHY Master and Slave Control register Low */
  117. #define MIPI_CSIS_DPHY_BCTRL_L 0x30
  118. #define MIPI_CSIS_DPHY_BCTRL_L_USER_DATA_PATTERN_LOW(n) (((n) & 3U) << 30)
  119. #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV (0 << 28)
  120. #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_724MV (1 << 28)
  121. #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_733MV (2 << 28)
  122. #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_706MV (3 << 28)
  123. #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ (0 << 27)
  124. #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_1_5MHZ (1 << 27)
  125. #define MIPI_CSIS_DPHY_BCTRL_L_VREG12_EXTPWR_EN_CTL BIT(26)
  126. #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V (0 << 24)
  127. #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_23V (1 << 24)
  128. #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_17V (2 << 24)
  129. #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_26V (3 << 24)
  130. #define MIPI_CSIS_DPHY_BCTRL_L_REG_1P2_LVL_SEL BIT(23)
  131. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV (0 << 21)
  132. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_100MV (1 << 21)
  133. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_120MV (2 << 21)
  134. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_140MV (3 << 21)
  135. #define MIPI_CSIS_DPHY_BCTRL_L_VREF_SRC_SEL BIT(20)
  136. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV (0 << 18)
  137. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_743MV (1 << 18)
  138. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_650MV (2 << 18)
  139. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_682MV (3 << 18)
  140. #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_PULSE_REJECT BIT(17)
  141. #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_0 (0 << 15)
  142. #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_15P (1 << 15)
  143. #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_30P (3 << 15)
  144. #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_UP BIT(14)
  145. #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV (0 << 13)
  146. #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_70MV (1 << 13)
  147. #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_EN BIT(12)
  148. #define MIPI_CSIS_DPHY_BCTRL_L_ERRCONTENTION_LP_EN BIT(11)
  149. #define MIPI_CSIS_DPHY_BCTRL_L_TXTRIGGER_CLK_EN BIT(10)
  150. #define MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(n) (((n) * 25 / 1000000) << 0)
  151. /* D-PHY Master and Slave Control register High */
  152. #define MIPI_CSIS_DPHY_BCTRL_H 0x34
  153. /* D-PHY Slave Control register Low */
  154. #define MIPI_CSIS_DPHY_SCTRL_L 0x38
  155. /* D-PHY Slave Control register High */
  156. #define MIPI_CSIS_DPHY_SCTRL_H 0x3c
  157. /* ISP Configuration register */
  158. #define MIPI_CSIS_ISP_CONFIG_CH(n) (0x40 + (n) * 0x10)
  159. #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP_MSK (0xff << 24)
  160. #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP(x) ((x) << 24)
  161. #define MIPI_CSIS_ISPCFG_PIXEL_MODE_SINGLE (0 << 12)
  162. #define MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL (1 << 12)
  163. #define MIPI_CSIS_ISPCFG_PIXEL_MODE_QUAD (2 << 12) /* i.MX8M[MNP] only */
  164. #define MIPI_CSIS_ISPCFG_PIXEL_MASK (3 << 12)
  165. #define MIPI_CSIS_ISPCFG_ALIGN_32BIT BIT(11)
  166. #define MIPI_CSIS_ISPCFG_FMT(fmt) ((fmt) << 2)
  167. #define MIPI_CSIS_ISPCFG_FMT_MASK (0x3f << 2)
  168. /* ISP Image Resolution register */
  169. #define MIPI_CSIS_ISP_RESOL_CH(n) (0x44 + (n) * 0x10)
  170. #define CSIS_MAX_PIX_WIDTH 0xffff
  171. #define CSIS_MAX_PIX_HEIGHT 0xffff
  172. /* ISP SYNC register */
  173. #define MIPI_CSIS_ISP_SYNC_CH(n) (0x48 + (n) * 0x10)
  174. #define MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET 18
  175. #define MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET 12
  176. #define MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET 0
  177. /* ISP shadow registers */
  178. #define MIPI_CSIS_SDW_CONFIG_CH(n) (0x80 + (n) * 0x10)
  179. #define MIPI_CSIS_SDW_RESOL_CH(n) (0x84 + (n) * 0x10)
  180. #define MIPI_CSIS_SDW_SYNC_CH(n) (0x88 + (n) * 0x10)
  181. /* Debug control register */
  182. #define MIPI_CSIS_DBG_CTRL 0xc0
  183. #define MIPI_CSIS_DBG_INTR_MSK 0xc4
  184. #define MIPI_CSIS_DBG_INTR_MSK_DT_NOT_SUPPORT BIT(25)
  185. #define MIPI_CSIS_DBG_INTR_MSK_DT_IGNORE BIT(24)
  186. #define MIPI_CSIS_DBG_INTR_MSK_ERR_FRAME_SIZE BIT(20)
  187. #define MIPI_CSIS_DBG_INTR_MSK_TRUNCATED_FRAME BIT(16)
  188. #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FE BIT(12)
  189. #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FS BIT(8)
  190. #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_FALL BIT(4)
  191. #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_RISE BIT(0)
  192. #define MIPI_CSIS_DBG_INTR_SRC 0xc8
  193. #define MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT BIT(25)
  194. #define MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE BIT(24)
  195. #define MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE BIT(20)
  196. #define MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME BIT(16)
  197. #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FE BIT(12)
  198. #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FS BIT(8)
  199. #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL BIT(4)
  200. #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE BIT(0)
  201. #define MIPI_CSIS_FRAME_COUNTER_CH(n) (0x0100 + (n) * 4)
  202. /* Non-image packet data buffers */
  203. #define MIPI_CSIS_PKTDATA_ODD 0x2000
  204. #define MIPI_CSIS_PKTDATA_EVEN 0x3000
  205. #define MIPI_CSIS_PKTDATA_SIZE SZ_4K
  206. #define DEFAULT_SCLK_CSIS_FREQ 166000000UL
  207. /* MIPI CSI-2 Data Types */
  208. #define MIPI_CSI2_DATA_TYPE_YUV420_8 0x18
  209. #define MIPI_CSI2_DATA_TYPE_YUV420_10 0x19
  210. #define MIPI_CSI2_DATA_TYPE_LE_YUV420_8 0x1a
  211. #define MIPI_CSI2_DATA_TYPE_CS_YUV420_8 0x1c
  212. #define MIPI_CSI2_DATA_TYPE_CS_YUV420_10 0x1d
  213. #define MIPI_CSI2_DATA_TYPE_YUV422_8 0x1e
  214. #define MIPI_CSI2_DATA_TYPE_YUV422_10 0x1f
  215. #define MIPI_CSI2_DATA_TYPE_RGB565 0x22
  216. #define MIPI_CSI2_DATA_TYPE_RGB666 0x23
  217. #define MIPI_CSI2_DATA_TYPE_RGB888 0x24
  218. #define MIPI_CSI2_DATA_TYPE_RAW6 0x28
  219. #define MIPI_CSI2_DATA_TYPE_RAW7 0x29
  220. #define MIPI_CSI2_DATA_TYPE_RAW8 0x2a
  221. #define MIPI_CSI2_DATA_TYPE_RAW10 0x2b
  222. #define MIPI_CSI2_DATA_TYPE_RAW12 0x2c
  223. #define MIPI_CSI2_DATA_TYPE_RAW14 0x2d
  224. #define MIPI_CSI2_DATA_TYPE_USER(x) (0x30 + (x))
  225. struct mipi_csis_event {
  226. bool debug;
  227. u32 mask;
  228. const char * const name;
  229. unsigned int counter;
  230. };
  231. static const struct mipi_csis_event mipi_csis_events[] = {
  232. /* Errors */
  233. { false, MIPI_CSIS_INT_SRC_ERR_SOT_HS, "SOT Error" },
  234. { false, MIPI_CSIS_INT_SRC_ERR_LOST_FS, "Lost Frame Start Error" },
  235. { false, MIPI_CSIS_INT_SRC_ERR_LOST_FE, "Lost Frame End Error" },
  236. { false, MIPI_CSIS_INT_SRC_ERR_OVER, "FIFO Overflow Error" },
  237. { false, MIPI_CSIS_INT_SRC_ERR_WRONG_CFG, "Wrong Configuration Error" },
  238. { false, MIPI_CSIS_INT_SRC_ERR_ECC, "ECC Error" },
  239. { false, MIPI_CSIS_INT_SRC_ERR_CRC, "CRC Error" },
  240. { false, MIPI_CSIS_INT_SRC_ERR_UNKNOWN, "Unknown Error" },
  241. { true, MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT, "Data Type Not Supported" },
  242. { true, MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE, "Data Type Ignored" },
  243. { true, MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE, "Frame Size Error" },
  244. { true, MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME, "Truncated Frame" },
  245. { true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FE, "Early Frame End" },
  246. { true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FS, "Early Frame Start" },
  247. /* Non-image data receive events */
  248. { false, MIPI_CSIS_INT_SRC_EVEN_BEFORE, "Non-image data before even frame" },
  249. { false, MIPI_CSIS_INT_SRC_EVEN_AFTER, "Non-image data after even frame" },
  250. { false, MIPI_CSIS_INT_SRC_ODD_BEFORE, "Non-image data before odd frame" },
  251. { false, MIPI_CSIS_INT_SRC_ODD_AFTER, "Non-image data after odd frame" },
  252. /* Frame start/end */
  253. { false, MIPI_CSIS_INT_SRC_FRAME_START, "Frame Start" },
  254. { false, MIPI_CSIS_INT_SRC_FRAME_END, "Frame End" },
  255. { true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL, "VSYNC Falling Edge" },
  256. { true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE, "VSYNC Rising Edge" },
  257. };
  258. #define MIPI_CSIS_NUM_EVENTS ARRAY_SIZE(mipi_csis_events)
  259. enum mipi_csis_clk {
  260. MIPI_CSIS_CLK_PCLK,
  261. MIPI_CSIS_CLK_WRAP,
  262. MIPI_CSIS_CLK_PHY,
  263. MIPI_CSIS_CLK_AXI,
  264. };
  265. static const char * const mipi_csis_clk_id[] = {
  266. "pclk",
  267. "wrap",
  268. "phy",
  269. "axi",
  270. };
  271. enum mipi_csis_version {
  272. MIPI_CSIS_V3_3,
  273. MIPI_CSIS_V3_6_3,
  274. };
  275. struct mipi_csis_info {
  276. enum mipi_csis_version version;
  277. unsigned int num_clocks;
  278. };
  279. struct mipi_csis_device {
  280. struct device *dev;
  281. void __iomem *regs;
  282. struct clk_bulk_data *clks;
  283. struct reset_control *mrst;
  284. struct regulator *mipi_phy_regulator;
  285. const struct mipi_csis_info *info;
  286. struct v4l2_subdev sd;
  287. struct media_pad pads[CSIS_PADS_NUM];
  288. struct v4l2_async_notifier notifier;
  289. struct {
  290. struct v4l2_subdev *sd;
  291. const struct media_pad *pad;
  292. } source;
  293. struct v4l2_mbus_config_mipi_csi2 bus;
  294. u32 clk_frequency;
  295. u32 hs_settle;
  296. u32 clk_settle;
  297. spinlock_t slock; /* Protect events */
  298. struct mipi_csis_event events[MIPI_CSIS_NUM_EVENTS];
  299. struct dentry *debugfs_root;
  300. struct {
  301. bool enable;
  302. u32 hs_settle;
  303. u32 clk_settle;
  304. } debug;
  305. };
  306. /* -----------------------------------------------------------------------------
  307. * Format helpers
  308. */
  309. struct csis_pix_format {
  310. u32 code;
  311. u32 output;
  312. u32 data_type;
  313. u8 width;
  314. };
  315. static const struct csis_pix_format mipi_csis_formats[] = {
  316. /* YUV formats. */
  317. {
  318. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  319. .output = MEDIA_BUS_FMT_UYVY8_1X16,
  320. .data_type = MIPI_CSI2_DATA_TYPE_YUV422_8,
  321. .width = 16,
  322. },
  323. /* RGB formats. */
  324. {
  325. .code = MEDIA_BUS_FMT_RGB565_1X16,
  326. .output = MEDIA_BUS_FMT_RGB565_1X16,
  327. .data_type = MIPI_CSI2_DATA_TYPE_RGB565,
  328. .width = 16,
  329. }, {
  330. .code = MEDIA_BUS_FMT_BGR888_1X24,
  331. .output = MEDIA_BUS_FMT_RGB888_1X24,
  332. .data_type = MIPI_CSI2_DATA_TYPE_RGB888,
  333. .width = 24,
  334. },
  335. /* RAW (Bayer and greyscale) formats. */
  336. {
  337. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  338. .output = MEDIA_BUS_FMT_SBGGR8_1X8,
  339. .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
  340. .width = 8,
  341. }, {
  342. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  343. .output = MEDIA_BUS_FMT_SGBRG8_1X8,
  344. .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
  345. .width = 8,
  346. }, {
  347. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  348. .output = MEDIA_BUS_FMT_SGRBG8_1X8,
  349. .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
  350. .width = 8,
  351. }, {
  352. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  353. .output = MEDIA_BUS_FMT_SRGGB8_1X8,
  354. .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
  355. .width = 8,
  356. }, {
  357. .code = MEDIA_BUS_FMT_Y8_1X8,
  358. .output = MEDIA_BUS_FMT_Y8_1X8,
  359. .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
  360. .width = 8,
  361. }, {
  362. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  363. .output = MEDIA_BUS_FMT_SBGGR10_1X10,
  364. .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
  365. .width = 10,
  366. }, {
  367. .code = MEDIA_BUS_FMT_SGBRG10_1X10,
  368. .output = MEDIA_BUS_FMT_SGBRG10_1X10,
  369. .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
  370. .width = 10,
  371. }, {
  372. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  373. .output = MEDIA_BUS_FMT_SGRBG10_1X10,
  374. .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
  375. .width = 10,
  376. }, {
  377. .code = MEDIA_BUS_FMT_SRGGB10_1X10,
  378. .output = MEDIA_BUS_FMT_SRGGB10_1X10,
  379. .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
  380. .width = 10,
  381. }, {
  382. .code = MEDIA_BUS_FMT_Y10_1X10,
  383. .output = MEDIA_BUS_FMT_Y10_1X10,
  384. .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
  385. .width = 10,
  386. }, {
  387. .code = MEDIA_BUS_FMT_SBGGR12_1X12,
  388. .output = MEDIA_BUS_FMT_SBGGR12_1X12,
  389. .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
  390. .width = 12,
  391. }, {
  392. .code = MEDIA_BUS_FMT_SGBRG12_1X12,
  393. .output = MEDIA_BUS_FMT_SGBRG12_1X12,
  394. .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
  395. .width = 12,
  396. }, {
  397. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  398. .output = MEDIA_BUS_FMT_SGRBG12_1X12,
  399. .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
  400. .width = 12,
  401. }, {
  402. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  403. .output = MEDIA_BUS_FMT_SRGGB12_1X12,
  404. .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
  405. .width = 12,
  406. }, {
  407. .code = MEDIA_BUS_FMT_Y12_1X12,
  408. .output = MEDIA_BUS_FMT_Y12_1X12,
  409. .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
  410. .width = 12,
  411. }, {
  412. .code = MEDIA_BUS_FMT_SBGGR14_1X14,
  413. .output = MEDIA_BUS_FMT_SBGGR14_1X14,
  414. .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
  415. .width = 14,
  416. }, {
  417. .code = MEDIA_BUS_FMT_SGBRG14_1X14,
  418. .output = MEDIA_BUS_FMT_SGBRG14_1X14,
  419. .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
  420. .width = 14,
  421. }, {
  422. .code = MEDIA_BUS_FMT_SGRBG14_1X14,
  423. .output = MEDIA_BUS_FMT_SGRBG14_1X14,
  424. .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
  425. .width = 14,
  426. }, {
  427. .code = MEDIA_BUS_FMT_SRGGB14_1X14,
  428. .output = MEDIA_BUS_FMT_SRGGB14_1X14,
  429. .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
  430. .width = 14,
  431. },
  432. /* JPEG */
  433. {
  434. .code = MEDIA_BUS_FMT_JPEG_1X8,
  435. .output = MEDIA_BUS_FMT_JPEG_1X8,
  436. /*
  437. * Map JPEG_1X8 to the RAW8 datatype.
  438. *
  439. * The CSI-2 specification suggests in Annex A "JPEG8 Data
  440. * Format (informative)" to transmit JPEG data using one of the
  441. * Data Types aimed to represent arbitrary data, such as the
  442. * "User Defined Data Type 1" (0x30).
  443. *
  444. * However, when configured with a User Defined Data Type, the
  445. * CSIS outputs data in quad pixel mode regardless of the mode
  446. * selected in the MIPI_CSIS_ISP_CONFIG_CH register. Neither of
  447. * the IP cores connected to the CSIS in i.MX SoCs (CSI bridge
  448. * or ISI) support quad pixel mode, so this will never work in
  449. * practice.
  450. *
  451. * Some sensors (such as the OV5640) send JPEG data using the
  452. * RAW8 data type. This is usable and works, so map the JPEG
  453. * format to RAW8. If the CSIS ends up being integrated in an
  454. * SoC that can support quad pixel mode, this will have to be
  455. * revisited.
  456. */
  457. .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
  458. .width = 8,
  459. }
  460. };
  461. static const struct csis_pix_format *find_csis_format(u32 code)
  462. {
  463. unsigned int i;
  464. for (i = 0; i < ARRAY_SIZE(mipi_csis_formats); i++)
  465. if (code == mipi_csis_formats[i].code)
  466. return &mipi_csis_formats[i];
  467. return NULL;
  468. }
  469. /* -----------------------------------------------------------------------------
  470. * Hardware configuration
  471. */
  472. static inline u32 mipi_csis_read(struct mipi_csis_device *csis, u32 reg)
  473. {
  474. return readl(csis->regs + reg);
  475. }
  476. static inline void mipi_csis_write(struct mipi_csis_device *csis, u32 reg,
  477. u32 val)
  478. {
  479. writel(val, csis->regs + reg);
  480. }
  481. static void mipi_csis_enable_interrupts(struct mipi_csis_device *csis, bool on)
  482. {
  483. mipi_csis_write(csis, MIPI_CSIS_INT_MSK, on ? 0xffffffff : 0);
  484. mipi_csis_write(csis, MIPI_CSIS_DBG_INTR_MSK, on ? 0xffffffff : 0);
  485. }
  486. static void mipi_csis_sw_reset(struct mipi_csis_device *csis)
  487. {
  488. u32 val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
  489. mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL,
  490. val | MIPI_CSIS_CMN_CTRL_RESET);
  491. usleep_range(10, 20);
  492. }
  493. static void mipi_csis_system_enable(struct mipi_csis_device *csis, int on)
  494. {
  495. u32 val, mask;
  496. val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
  497. if (on)
  498. val |= MIPI_CSIS_CMN_CTRL_ENABLE;
  499. else
  500. val &= ~MIPI_CSIS_CMN_CTRL_ENABLE;
  501. mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL, val);
  502. val = mipi_csis_read(csis, MIPI_CSIS_DPHY_CMN_CTRL);
  503. val &= ~MIPI_CSIS_DPHY_CMN_CTRL_ENABLE;
  504. if (on) {
  505. mask = (1 << (csis->bus.num_data_lanes + 1)) - 1;
  506. val |= (mask & MIPI_CSIS_DPHY_CMN_CTRL_ENABLE);
  507. }
  508. mipi_csis_write(csis, MIPI_CSIS_DPHY_CMN_CTRL, val);
  509. }
  510. static void __mipi_csis_set_format(struct mipi_csis_device *csis,
  511. const struct v4l2_mbus_framefmt *format,
  512. const struct csis_pix_format *csis_fmt)
  513. {
  514. u32 val;
  515. /* Color format */
  516. val = mipi_csis_read(csis, MIPI_CSIS_ISP_CONFIG_CH(0));
  517. val &= ~(MIPI_CSIS_ISPCFG_ALIGN_32BIT | MIPI_CSIS_ISPCFG_FMT_MASK
  518. | MIPI_CSIS_ISPCFG_PIXEL_MASK);
  519. /*
  520. * YUV 4:2:2 can be transferred with 8 or 16 bits per clock sample
  521. * (referred to in the documentation as single and dual pixel modes
  522. * respectively, although the 8-bit mode transfers half a pixel per
  523. * clock sample and the 16-bit mode one pixel). While both mode work
  524. * when the CSIS is connected to a receiver that supports either option,
  525. * single pixel mode requires clock rates twice as high. As all SoCs
  526. * that integrate the CSIS can operate in 16-bit bit mode, and some do
  527. * not support 8-bit mode (this is the case of the i.MX8MP), use dual
  528. * pixel mode unconditionally.
  529. *
  530. * TODO: Verify which other formats require DUAL (or QUAD) modes.
  531. */
  532. if (csis_fmt->data_type == MIPI_CSI2_DATA_TYPE_YUV422_8)
  533. val |= MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL;
  534. val |= MIPI_CSIS_ISPCFG_FMT(csis_fmt->data_type);
  535. mipi_csis_write(csis, MIPI_CSIS_ISP_CONFIG_CH(0), val);
  536. /* Pixel resolution */
  537. val = format->width | (format->height << 16);
  538. mipi_csis_write(csis, MIPI_CSIS_ISP_RESOL_CH(0), val);
  539. }
  540. static int mipi_csis_calculate_params(struct mipi_csis_device *csis,
  541. const struct csis_pix_format *csis_fmt)
  542. {
  543. s64 link_freq;
  544. u32 lane_rate;
  545. /* Calculate the line rate from the pixel rate. */
  546. link_freq = v4l2_get_link_freq(csis->source.sd->ctrl_handler,
  547. csis_fmt->width,
  548. csis->bus.num_data_lanes * 2);
  549. if (link_freq < 0) {
  550. dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
  551. (int)link_freq);
  552. return link_freq;
  553. }
  554. lane_rate = link_freq * 2;
  555. if (lane_rate < 80000000 || lane_rate > 1500000000) {
  556. dev_dbg(csis->dev, "Out-of-bound lane rate %u\n", lane_rate);
  557. return -EINVAL;
  558. }
  559. /*
  560. * The HSSETTLE counter value is document in a table, but can also
  561. * easily be calculated. Hardcode the CLKSETTLE value to 0 for now
  562. * (which is documented as corresponding to CSI-2 v0.87 to v1.00) until
  563. * we figure out how to compute it correctly.
  564. */
  565. csis->hs_settle = (lane_rate - 5000000) / 45000000;
  566. csis->clk_settle = 0;
  567. dev_dbg(csis->dev, "lane rate %u, Tclk_settle %u, Ths_settle %u\n",
  568. lane_rate, csis->clk_settle, csis->hs_settle);
  569. if (csis->debug.hs_settle < 0xff) {
  570. dev_dbg(csis->dev, "overriding Ths_settle with %u\n",
  571. csis->debug.hs_settle);
  572. csis->hs_settle = csis->debug.hs_settle;
  573. }
  574. if (csis->debug.clk_settle < 4) {
  575. dev_dbg(csis->dev, "overriding Tclk_settle with %u\n",
  576. csis->debug.clk_settle);
  577. csis->clk_settle = csis->debug.clk_settle;
  578. }
  579. return 0;
  580. }
  581. static void mipi_csis_set_params(struct mipi_csis_device *csis,
  582. const struct v4l2_mbus_framefmt *format,
  583. const struct csis_pix_format *csis_fmt)
  584. {
  585. int lanes = csis->bus.num_data_lanes;
  586. u32 val;
  587. val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
  588. val &= ~MIPI_CSIS_CMN_CTRL_LANE_NR_MASK;
  589. val |= (lanes - 1) << MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET;
  590. if (csis->info->version == MIPI_CSIS_V3_3)
  591. val |= MIPI_CSIS_CMN_CTRL_INTER_MODE;
  592. mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL, val);
  593. __mipi_csis_set_format(csis, format, csis_fmt);
  594. mipi_csis_write(csis, MIPI_CSIS_DPHY_CMN_CTRL,
  595. MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(csis->hs_settle) |
  596. MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(csis->clk_settle));
  597. val = (0 << MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET)
  598. | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET)
  599. | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET);
  600. mipi_csis_write(csis, MIPI_CSIS_ISP_SYNC_CH(0), val);
  601. val = mipi_csis_read(csis, MIPI_CSIS_CLK_CTRL);
  602. val |= MIPI_CSIS_CLK_CTRL_WCLK_SRC;
  603. val |= MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(15);
  604. val &= ~MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK;
  605. mipi_csis_write(csis, MIPI_CSIS_CLK_CTRL, val);
  606. mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_L,
  607. MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV |
  608. MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ |
  609. MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V |
  610. MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV |
  611. MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV |
  612. MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV |
  613. MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(20000000));
  614. mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_H, 0);
  615. /* Update the shadow register. */
  616. val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
  617. mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL,
  618. val | MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW |
  619. MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL);
  620. }
  621. static int mipi_csis_clk_enable(struct mipi_csis_device *csis)
  622. {
  623. return clk_bulk_prepare_enable(csis->info->num_clocks, csis->clks);
  624. }
  625. static void mipi_csis_clk_disable(struct mipi_csis_device *csis)
  626. {
  627. clk_bulk_disable_unprepare(csis->info->num_clocks, csis->clks);
  628. }
  629. static int mipi_csis_clk_get(struct mipi_csis_device *csis)
  630. {
  631. unsigned int i;
  632. int ret;
  633. csis->clks = devm_kcalloc(csis->dev, csis->info->num_clocks,
  634. sizeof(*csis->clks), GFP_KERNEL);
  635. if (!csis->clks)
  636. return -ENOMEM;
  637. for (i = 0; i < csis->info->num_clocks; i++)
  638. csis->clks[i].id = mipi_csis_clk_id[i];
  639. ret = devm_clk_bulk_get(csis->dev, csis->info->num_clocks,
  640. csis->clks);
  641. if (ret < 0)
  642. return ret;
  643. /* Set clock rate */
  644. ret = clk_set_rate(csis->clks[MIPI_CSIS_CLK_WRAP].clk,
  645. csis->clk_frequency);
  646. if (ret < 0)
  647. dev_err(csis->dev, "set rate=%d failed: %d\n",
  648. csis->clk_frequency, ret);
  649. return ret;
  650. }
  651. static void mipi_csis_start_stream(struct mipi_csis_device *csis,
  652. const struct v4l2_mbus_framefmt *format,
  653. const struct csis_pix_format *csis_fmt)
  654. {
  655. mipi_csis_sw_reset(csis);
  656. mipi_csis_set_params(csis, format, csis_fmt);
  657. mipi_csis_system_enable(csis, true);
  658. mipi_csis_enable_interrupts(csis, true);
  659. }
  660. static void mipi_csis_stop_stream(struct mipi_csis_device *csis)
  661. {
  662. mipi_csis_enable_interrupts(csis, false);
  663. mipi_csis_system_enable(csis, false);
  664. }
  665. static void mipi_csis_queue_event_sof(struct mipi_csis_device *csis)
  666. {
  667. struct v4l2_event event = {
  668. .type = V4L2_EVENT_FRAME_SYNC,
  669. };
  670. u32 frame;
  671. frame = mipi_csis_read(csis, MIPI_CSIS_FRAME_COUNTER_CH(0));
  672. event.u.frame_sync.frame_sequence = frame;
  673. v4l2_event_queue(csis->sd.devnode, &event);
  674. }
  675. static irqreturn_t mipi_csis_irq_handler(int irq, void *dev_id)
  676. {
  677. struct mipi_csis_device *csis = dev_id;
  678. unsigned long flags;
  679. unsigned int i;
  680. u32 status;
  681. u32 dbg_status;
  682. status = mipi_csis_read(csis, MIPI_CSIS_INT_SRC);
  683. dbg_status = mipi_csis_read(csis, MIPI_CSIS_DBG_INTR_SRC);
  684. spin_lock_irqsave(&csis->slock, flags);
  685. /* Update the event/error counters */
  686. if ((status & MIPI_CSIS_INT_SRC_ERRORS) || csis->debug.enable) {
  687. for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++) {
  688. struct mipi_csis_event *event = &csis->events[i];
  689. if ((!event->debug && (status & event->mask)) ||
  690. (event->debug && (dbg_status & event->mask)))
  691. event->counter++;
  692. }
  693. }
  694. if (status & MIPI_CSIS_INT_SRC_FRAME_START)
  695. mipi_csis_queue_event_sof(csis);
  696. spin_unlock_irqrestore(&csis->slock, flags);
  697. mipi_csis_write(csis, MIPI_CSIS_INT_SRC, status);
  698. mipi_csis_write(csis, MIPI_CSIS_DBG_INTR_SRC, dbg_status);
  699. return IRQ_HANDLED;
  700. }
  701. /* -----------------------------------------------------------------------------
  702. * PHY regulator and reset
  703. */
  704. static int mipi_csis_phy_enable(struct mipi_csis_device *csis)
  705. {
  706. if (csis->info->version != MIPI_CSIS_V3_3)
  707. return 0;
  708. return regulator_enable(csis->mipi_phy_regulator);
  709. }
  710. static int mipi_csis_phy_disable(struct mipi_csis_device *csis)
  711. {
  712. if (csis->info->version != MIPI_CSIS_V3_3)
  713. return 0;
  714. return regulator_disable(csis->mipi_phy_regulator);
  715. }
  716. static void mipi_csis_phy_reset(struct mipi_csis_device *csis)
  717. {
  718. if (csis->info->version != MIPI_CSIS_V3_3)
  719. return;
  720. reset_control_assert(csis->mrst);
  721. msleep(20);
  722. reset_control_deassert(csis->mrst);
  723. }
  724. static int mipi_csis_phy_init(struct mipi_csis_device *csis)
  725. {
  726. if (csis->info->version != MIPI_CSIS_V3_3)
  727. return 0;
  728. /* Get MIPI PHY reset and regulator. */
  729. csis->mrst = devm_reset_control_get_exclusive(csis->dev, NULL);
  730. if (IS_ERR(csis->mrst))
  731. return PTR_ERR(csis->mrst);
  732. csis->mipi_phy_regulator = devm_regulator_get(csis->dev, "phy");
  733. if (IS_ERR(csis->mipi_phy_regulator))
  734. return PTR_ERR(csis->mipi_phy_regulator);
  735. return regulator_set_voltage(csis->mipi_phy_regulator, 1000000,
  736. 1000000);
  737. }
  738. /* -----------------------------------------------------------------------------
  739. * Debug
  740. */
  741. static void mipi_csis_clear_counters(struct mipi_csis_device *csis)
  742. {
  743. unsigned long flags;
  744. unsigned int i;
  745. spin_lock_irqsave(&csis->slock, flags);
  746. for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++)
  747. csis->events[i].counter = 0;
  748. spin_unlock_irqrestore(&csis->slock, flags);
  749. }
  750. static void mipi_csis_log_counters(struct mipi_csis_device *csis, bool non_errors)
  751. {
  752. unsigned int num_events = non_errors ? MIPI_CSIS_NUM_EVENTS
  753. : MIPI_CSIS_NUM_EVENTS - 8;
  754. unsigned int counters[MIPI_CSIS_NUM_EVENTS];
  755. unsigned long flags;
  756. unsigned int i;
  757. spin_lock_irqsave(&csis->slock, flags);
  758. for (i = 0; i < num_events; ++i)
  759. counters[i] = csis->events[i].counter;
  760. spin_unlock_irqrestore(&csis->slock, flags);
  761. for (i = 0; i < num_events; ++i) {
  762. if (counters[i] > 0 || csis->debug.enable)
  763. dev_info(csis->dev, "%s events: %d\n",
  764. csis->events[i].name,
  765. counters[i]);
  766. }
  767. }
  768. static int mipi_csis_dump_regs(struct mipi_csis_device *csis)
  769. {
  770. static const struct {
  771. u32 offset;
  772. const char * const name;
  773. } registers[] = {
  774. { MIPI_CSIS_CMN_CTRL, "CMN_CTRL" },
  775. { MIPI_CSIS_CLK_CTRL, "CLK_CTRL" },
  776. { MIPI_CSIS_INT_MSK, "INT_MSK" },
  777. { MIPI_CSIS_DPHY_STATUS, "DPHY_STATUS" },
  778. { MIPI_CSIS_DPHY_CMN_CTRL, "DPHY_CMN_CTRL" },
  779. { MIPI_CSIS_DPHY_SCTRL_L, "DPHY_SCTRL_L" },
  780. { MIPI_CSIS_DPHY_SCTRL_H, "DPHY_SCTRL_H" },
  781. { MIPI_CSIS_ISP_CONFIG_CH(0), "ISP_CONFIG_CH0" },
  782. { MIPI_CSIS_ISP_RESOL_CH(0), "ISP_RESOL_CH0" },
  783. { MIPI_CSIS_SDW_CONFIG_CH(0), "SDW_CONFIG_CH0" },
  784. { MIPI_CSIS_SDW_RESOL_CH(0), "SDW_RESOL_CH0" },
  785. { MIPI_CSIS_DBG_CTRL, "DBG_CTRL" },
  786. { MIPI_CSIS_FRAME_COUNTER_CH(0), "FRAME_COUNTER_CH0" },
  787. };
  788. unsigned int i;
  789. u32 cfg;
  790. if (!pm_runtime_get_if_in_use(csis->dev))
  791. return 0;
  792. dev_info(csis->dev, "--- REGISTERS ---\n");
  793. for (i = 0; i < ARRAY_SIZE(registers); i++) {
  794. cfg = mipi_csis_read(csis, registers[i].offset);
  795. dev_info(csis->dev, "%14s: 0x%08x\n", registers[i].name, cfg);
  796. }
  797. pm_runtime_put(csis->dev);
  798. return 0;
  799. }
  800. static int mipi_csis_dump_regs_show(struct seq_file *m, void *private)
  801. {
  802. struct mipi_csis_device *csis = m->private;
  803. return mipi_csis_dump_regs(csis);
  804. }
  805. DEFINE_SHOW_ATTRIBUTE(mipi_csis_dump_regs);
  806. static void mipi_csis_debugfs_init(struct mipi_csis_device *csis)
  807. {
  808. csis->debug.hs_settle = UINT_MAX;
  809. csis->debug.clk_settle = UINT_MAX;
  810. csis->debugfs_root = debugfs_create_dir(dev_name(csis->dev), NULL);
  811. debugfs_create_bool("debug_enable", 0600, csis->debugfs_root,
  812. &csis->debug.enable);
  813. debugfs_create_file("dump_regs", 0600, csis->debugfs_root, csis,
  814. &mipi_csis_dump_regs_fops);
  815. debugfs_create_u32("tclk_settle", 0600, csis->debugfs_root,
  816. &csis->debug.clk_settle);
  817. debugfs_create_u32("ths_settle", 0600, csis->debugfs_root,
  818. &csis->debug.hs_settle);
  819. }
  820. static void mipi_csis_debugfs_exit(struct mipi_csis_device *csis)
  821. {
  822. debugfs_remove_recursive(csis->debugfs_root);
  823. }
  824. /* -----------------------------------------------------------------------------
  825. * V4L2 subdev operations
  826. */
  827. static struct mipi_csis_device *sd_to_mipi_csis_device(struct v4l2_subdev *sdev)
  828. {
  829. return container_of(sdev, struct mipi_csis_device, sd);
  830. }
  831. static int mipi_csis_s_stream(struct v4l2_subdev *sd, int enable)
  832. {
  833. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  834. const struct v4l2_mbus_framefmt *format;
  835. const struct csis_pix_format *csis_fmt;
  836. struct v4l2_subdev_state *state;
  837. int ret;
  838. if (!enable) {
  839. v4l2_subdev_disable_streams(csis->source.sd,
  840. csis->source.pad->index, BIT(0));
  841. mipi_csis_stop_stream(csis);
  842. if (csis->debug.enable)
  843. mipi_csis_log_counters(csis, true);
  844. pm_runtime_put(csis->dev);
  845. return 0;
  846. }
  847. state = v4l2_subdev_lock_and_get_active_state(sd);
  848. format = v4l2_subdev_state_get_format(state, CSIS_PAD_SINK);
  849. csis_fmt = find_csis_format(format->code);
  850. ret = mipi_csis_calculate_params(csis, csis_fmt);
  851. if (ret < 0)
  852. goto err_unlock;
  853. mipi_csis_clear_counters(csis);
  854. ret = pm_runtime_resume_and_get(csis->dev);
  855. if (ret < 0)
  856. goto err_unlock;
  857. mipi_csis_start_stream(csis, format, csis_fmt);
  858. ret = v4l2_subdev_enable_streams(csis->source.sd,
  859. csis->source.pad->index, BIT(0));
  860. if (ret < 0)
  861. goto err_stop;
  862. mipi_csis_log_counters(csis, true);
  863. v4l2_subdev_unlock_state(state);
  864. return 0;
  865. err_stop:
  866. mipi_csis_stop_stream(csis);
  867. pm_runtime_put(csis->dev);
  868. err_unlock:
  869. v4l2_subdev_unlock_state(state);
  870. return ret;
  871. }
  872. static int mipi_csis_enum_mbus_code(struct v4l2_subdev *sd,
  873. struct v4l2_subdev_state *sd_state,
  874. struct v4l2_subdev_mbus_code_enum *code)
  875. {
  876. /*
  877. * The CSIS can't transcode in any way, the source format is identical
  878. * to the sink format.
  879. */
  880. if (code->pad == CSIS_PAD_SOURCE) {
  881. struct v4l2_mbus_framefmt *fmt;
  882. if (code->index > 0)
  883. return -EINVAL;
  884. fmt = v4l2_subdev_state_get_format(sd_state, code->pad);
  885. code->code = fmt->code;
  886. return 0;
  887. }
  888. if (code->pad != CSIS_PAD_SINK)
  889. return -EINVAL;
  890. if (code->index >= ARRAY_SIZE(mipi_csis_formats))
  891. return -EINVAL;
  892. code->code = mipi_csis_formats[code->index].code;
  893. return 0;
  894. }
  895. static int mipi_csis_set_fmt(struct v4l2_subdev *sd,
  896. struct v4l2_subdev_state *sd_state,
  897. struct v4l2_subdev_format *sdformat)
  898. {
  899. struct csis_pix_format const *csis_fmt;
  900. struct v4l2_mbus_framefmt *fmt;
  901. unsigned int align;
  902. /*
  903. * The CSIS can't transcode in any way, the source format can't be
  904. * modified.
  905. */
  906. if (sdformat->pad == CSIS_PAD_SOURCE)
  907. return v4l2_subdev_get_fmt(sd, sd_state, sdformat);
  908. if (sdformat->pad != CSIS_PAD_SINK)
  909. return -EINVAL;
  910. /*
  911. * Validate the media bus code and clamp and align the size.
  912. *
  913. * The total number of bits per line must be a multiple of 8. We thus
  914. * need to align the width for formats that are not multiples of 8
  915. * bits.
  916. */
  917. csis_fmt = find_csis_format(sdformat->format.code);
  918. if (!csis_fmt)
  919. csis_fmt = &mipi_csis_formats[0];
  920. switch (csis_fmt->width % 8) {
  921. case 0:
  922. align = 0;
  923. break;
  924. case 4:
  925. align = 1;
  926. break;
  927. case 2:
  928. case 6:
  929. align = 2;
  930. break;
  931. default:
  932. /* 1, 3, 5, 7 */
  933. align = 3;
  934. break;
  935. }
  936. v4l_bound_align_image(&sdformat->format.width, 1,
  937. CSIS_MAX_PIX_WIDTH, align,
  938. &sdformat->format.height, 1,
  939. CSIS_MAX_PIX_HEIGHT, 0, 0);
  940. fmt = v4l2_subdev_state_get_format(sd_state, sdformat->pad);
  941. fmt->code = csis_fmt->code;
  942. fmt->width = sdformat->format.width;
  943. fmt->height = sdformat->format.height;
  944. fmt->field = V4L2_FIELD_NONE;
  945. fmt->colorspace = sdformat->format.colorspace;
  946. fmt->quantization = sdformat->format.quantization;
  947. fmt->xfer_func = sdformat->format.xfer_func;
  948. fmt->ycbcr_enc = sdformat->format.ycbcr_enc;
  949. sdformat->format = *fmt;
  950. /* Propagate the format from sink to source. */
  951. fmt = v4l2_subdev_state_get_format(sd_state, CSIS_PAD_SOURCE);
  952. *fmt = sdformat->format;
  953. /* The format on the source pad might change due to unpacking. */
  954. fmt->code = csis_fmt->output;
  955. return 0;
  956. }
  957. static int mipi_csis_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  958. struct v4l2_mbus_frame_desc *fd)
  959. {
  960. struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[0];
  961. const struct csis_pix_format *csis_fmt;
  962. const struct v4l2_mbus_framefmt *fmt;
  963. struct v4l2_subdev_state *state;
  964. if (pad != CSIS_PAD_SOURCE)
  965. return -EINVAL;
  966. state = v4l2_subdev_lock_and_get_active_state(sd);
  967. fmt = v4l2_subdev_state_get_format(state, CSIS_PAD_SOURCE);
  968. csis_fmt = find_csis_format(fmt->code);
  969. v4l2_subdev_unlock_state(state);
  970. if (!csis_fmt)
  971. return -EPIPE;
  972. fd->type = V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL;
  973. fd->num_entries = 1;
  974. entry->flags = 0;
  975. entry->pixelcode = csis_fmt->code;
  976. entry->bus.csi2.vc = 0;
  977. entry->bus.csi2.dt = csis_fmt->data_type;
  978. return 0;
  979. }
  980. static int mipi_csis_init_state(struct v4l2_subdev *sd,
  981. struct v4l2_subdev_state *sd_state)
  982. {
  983. struct v4l2_subdev_format fmt = {
  984. .pad = CSIS_PAD_SINK,
  985. };
  986. fmt.format.code = mipi_csis_formats[0].code;
  987. fmt.format.width = MIPI_CSIS_DEF_PIX_WIDTH;
  988. fmt.format.height = MIPI_CSIS_DEF_PIX_HEIGHT;
  989. fmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
  990. fmt.format.xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt.format.colorspace);
  991. fmt.format.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt.format.colorspace);
  992. fmt.format.quantization =
  993. V4L2_MAP_QUANTIZATION_DEFAULT(false, fmt.format.colorspace,
  994. fmt.format.ycbcr_enc);
  995. return mipi_csis_set_fmt(sd, sd_state, &fmt);
  996. }
  997. static int mipi_csis_log_status(struct v4l2_subdev *sd)
  998. {
  999. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1000. mipi_csis_log_counters(csis, true);
  1001. if (csis->debug.enable)
  1002. mipi_csis_dump_regs(csis);
  1003. return 0;
  1004. }
  1005. static int mipi_csis_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  1006. struct v4l2_event_subscription *sub)
  1007. {
  1008. if (sub->type != V4L2_EVENT_FRAME_SYNC)
  1009. return -EINVAL;
  1010. /* V4L2_EVENT_FRAME_SYNC doesn't require an id, so zero should be set */
  1011. if (sub->id != 0)
  1012. return -EINVAL;
  1013. return v4l2_event_subscribe(fh, sub, 0, NULL);
  1014. }
  1015. static const struct v4l2_subdev_core_ops mipi_csis_core_ops = {
  1016. .log_status = mipi_csis_log_status,
  1017. .subscribe_event = mipi_csis_subscribe_event,
  1018. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1019. };
  1020. static const struct v4l2_subdev_video_ops mipi_csis_video_ops = {
  1021. .s_stream = mipi_csis_s_stream,
  1022. };
  1023. static const struct v4l2_subdev_pad_ops mipi_csis_pad_ops = {
  1024. .enum_mbus_code = mipi_csis_enum_mbus_code,
  1025. .get_fmt = v4l2_subdev_get_fmt,
  1026. .set_fmt = mipi_csis_set_fmt,
  1027. .get_frame_desc = mipi_csis_get_frame_desc,
  1028. };
  1029. static const struct v4l2_subdev_ops mipi_csis_subdev_ops = {
  1030. .core = &mipi_csis_core_ops,
  1031. .video = &mipi_csis_video_ops,
  1032. .pad = &mipi_csis_pad_ops,
  1033. };
  1034. static const struct v4l2_subdev_internal_ops mipi_csis_internal_ops = {
  1035. .init_state = mipi_csis_init_state,
  1036. };
  1037. /* -----------------------------------------------------------------------------
  1038. * Media entity operations
  1039. */
  1040. static int mipi_csis_link_setup(struct media_entity *entity,
  1041. const struct media_pad *local_pad,
  1042. const struct media_pad *remote_pad, u32 flags)
  1043. {
  1044. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1045. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1046. struct v4l2_subdev *remote_sd;
  1047. dev_dbg(csis->dev, "link setup %s -> %s", remote_pad->entity->name,
  1048. local_pad->entity->name);
  1049. /* We only care about the link to the source. */
  1050. if (!(local_pad->flags & MEDIA_PAD_FL_SINK))
  1051. return 0;
  1052. remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
  1053. if (flags & MEDIA_LNK_FL_ENABLED) {
  1054. if (csis->source.sd)
  1055. return -EBUSY;
  1056. csis->source.sd = remote_sd;
  1057. csis->source.pad = remote_pad;
  1058. } else {
  1059. csis->source.sd = NULL;
  1060. csis->source.pad = NULL;
  1061. }
  1062. return 0;
  1063. }
  1064. static const struct media_entity_operations mipi_csis_entity_ops = {
  1065. .link_setup = mipi_csis_link_setup,
  1066. .link_validate = v4l2_subdev_link_validate,
  1067. .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
  1068. };
  1069. /* -----------------------------------------------------------------------------
  1070. * Async subdev notifier
  1071. */
  1072. static struct mipi_csis_device *
  1073. mipi_notifier_to_csis_state(struct v4l2_async_notifier *n)
  1074. {
  1075. return container_of(n, struct mipi_csis_device, notifier);
  1076. }
  1077. static int mipi_csis_notify_bound(struct v4l2_async_notifier *notifier,
  1078. struct v4l2_subdev *sd,
  1079. struct v4l2_async_connection *asd)
  1080. {
  1081. struct mipi_csis_device *csis = mipi_notifier_to_csis_state(notifier);
  1082. struct media_pad *sink = &csis->sd.entity.pads[CSIS_PAD_SINK];
  1083. return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
  1084. }
  1085. static const struct v4l2_async_notifier_operations mipi_csis_notify_ops = {
  1086. .bound = mipi_csis_notify_bound,
  1087. };
  1088. static int mipi_csis_async_register(struct mipi_csis_device *csis)
  1089. {
  1090. struct v4l2_fwnode_endpoint vep = {
  1091. .bus_type = V4L2_MBUS_CSI2_DPHY,
  1092. };
  1093. struct v4l2_async_connection *asd;
  1094. struct fwnode_handle *ep;
  1095. unsigned int i;
  1096. int ret;
  1097. v4l2_async_subdev_nf_init(&csis->notifier, &csis->sd);
  1098. ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev), 0, 0,
  1099. FWNODE_GRAPH_ENDPOINT_NEXT);
  1100. if (!ep)
  1101. return -ENOTCONN;
  1102. ret = v4l2_fwnode_endpoint_parse(ep, &vep);
  1103. if (ret)
  1104. goto err_parse;
  1105. for (i = 0; i < vep.bus.mipi_csi2.num_data_lanes; ++i) {
  1106. if (vep.bus.mipi_csi2.data_lanes[i] != i + 1) {
  1107. dev_err(csis->dev,
  1108. "data lanes reordering is not supported");
  1109. ret = -EINVAL;
  1110. goto err_parse;
  1111. }
  1112. }
  1113. csis->bus = vep.bus.mipi_csi2;
  1114. dev_dbg(csis->dev, "data lanes: %d\n", csis->bus.num_data_lanes);
  1115. dev_dbg(csis->dev, "flags: 0x%08x\n", csis->bus.flags);
  1116. asd = v4l2_async_nf_add_fwnode_remote(&csis->notifier, ep,
  1117. struct v4l2_async_connection);
  1118. if (IS_ERR(asd)) {
  1119. ret = PTR_ERR(asd);
  1120. goto err_parse;
  1121. }
  1122. fwnode_handle_put(ep);
  1123. csis->notifier.ops = &mipi_csis_notify_ops;
  1124. ret = v4l2_async_nf_register(&csis->notifier);
  1125. if (ret)
  1126. return ret;
  1127. return v4l2_async_register_subdev(&csis->sd);
  1128. err_parse:
  1129. fwnode_handle_put(ep);
  1130. return ret;
  1131. }
  1132. /* -----------------------------------------------------------------------------
  1133. * Suspend/resume
  1134. */
  1135. static int mipi_csis_runtime_suspend(struct device *dev)
  1136. {
  1137. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1138. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1139. int ret;
  1140. ret = mipi_csis_phy_disable(csis);
  1141. if (ret)
  1142. return -EAGAIN;
  1143. mipi_csis_clk_disable(csis);
  1144. return 0;
  1145. }
  1146. static int mipi_csis_runtime_resume(struct device *dev)
  1147. {
  1148. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1149. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1150. int ret;
  1151. ret = mipi_csis_phy_enable(csis);
  1152. if (ret)
  1153. return -EAGAIN;
  1154. ret = mipi_csis_clk_enable(csis);
  1155. if (ret) {
  1156. mipi_csis_phy_disable(csis);
  1157. return ret;
  1158. }
  1159. return 0;
  1160. }
  1161. static const struct dev_pm_ops mipi_csis_pm_ops = {
  1162. RUNTIME_PM_OPS(mipi_csis_runtime_suspend, mipi_csis_runtime_resume,
  1163. NULL)
  1164. };
  1165. /* -----------------------------------------------------------------------------
  1166. * Probe/remove & platform driver
  1167. */
  1168. static int mipi_csis_subdev_init(struct mipi_csis_device *csis)
  1169. {
  1170. struct v4l2_subdev *sd = &csis->sd;
  1171. int ret;
  1172. v4l2_subdev_init(sd, &mipi_csis_subdev_ops);
  1173. sd->internal_ops = &mipi_csis_internal_ops;
  1174. sd->owner = THIS_MODULE;
  1175. snprintf(sd->name, sizeof(sd->name), "csis-%s",
  1176. dev_name(csis->dev));
  1177. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1178. sd->ctrl_handler = NULL;
  1179. sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  1180. sd->entity.ops = &mipi_csis_entity_ops;
  1181. sd->dev = csis->dev;
  1182. csis->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK
  1183. | MEDIA_PAD_FL_MUST_CONNECT;
  1184. csis->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE
  1185. | MEDIA_PAD_FL_MUST_CONNECT;
  1186. ret = media_entity_pads_init(&sd->entity, CSIS_PADS_NUM, csis->pads);
  1187. if (ret)
  1188. return ret;
  1189. ret = v4l2_subdev_init_finalize(sd);
  1190. if (ret) {
  1191. media_entity_cleanup(&sd->entity);
  1192. return ret;
  1193. }
  1194. return 0;
  1195. }
  1196. static int mipi_csis_parse_dt(struct mipi_csis_device *csis)
  1197. {
  1198. struct device_node *node = csis->dev->of_node;
  1199. if (of_property_read_u32(node, "clock-frequency",
  1200. &csis->clk_frequency))
  1201. csis->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
  1202. return 0;
  1203. }
  1204. static int mipi_csis_probe(struct platform_device *pdev)
  1205. {
  1206. struct device *dev = &pdev->dev;
  1207. struct mipi_csis_device *csis;
  1208. int irq;
  1209. int ret;
  1210. csis = devm_kzalloc(dev, sizeof(*csis), GFP_KERNEL);
  1211. if (!csis)
  1212. return -ENOMEM;
  1213. spin_lock_init(&csis->slock);
  1214. csis->dev = dev;
  1215. csis->info = of_device_get_match_data(dev);
  1216. memcpy(csis->events, mipi_csis_events, sizeof(csis->events));
  1217. /* Parse DT properties. */
  1218. ret = mipi_csis_parse_dt(csis);
  1219. if (ret < 0) {
  1220. dev_err(dev, "Failed to parse device tree: %d\n", ret);
  1221. return ret;
  1222. }
  1223. /* Acquire resources. */
  1224. csis->regs = devm_platform_ioremap_resource(pdev, 0);
  1225. if (IS_ERR(csis->regs))
  1226. return PTR_ERR(csis->regs);
  1227. irq = platform_get_irq(pdev, 0);
  1228. if (irq < 0)
  1229. return irq;
  1230. ret = mipi_csis_phy_init(csis);
  1231. if (ret < 0)
  1232. return ret;
  1233. ret = mipi_csis_clk_get(csis);
  1234. if (ret < 0)
  1235. return ret;
  1236. /* Reset PHY and enable the clocks. */
  1237. mipi_csis_phy_reset(csis);
  1238. /* Now that the hardware is initialized, request the interrupt. */
  1239. ret = devm_request_irq(dev, irq, mipi_csis_irq_handler, 0,
  1240. dev_name(dev), csis);
  1241. if (ret) {
  1242. dev_err(dev, "Interrupt request failed\n");
  1243. return ret;
  1244. }
  1245. /* Initialize and register the subdev. */
  1246. ret = mipi_csis_subdev_init(csis);
  1247. if (ret < 0)
  1248. return ret;
  1249. platform_set_drvdata(pdev, &csis->sd);
  1250. ret = mipi_csis_async_register(csis);
  1251. if (ret < 0) {
  1252. dev_err(dev, "async register failed: %d\n", ret);
  1253. goto err_cleanup;
  1254. }
  1255. /* Initialize debugfs. */
  1256. mipi_csis_debugfs_init(csis);
  1257. /* Enable runtime PM. */
  1258. pm_runtime_enable(dev);
  1259. if (!pm_runtime_enabled(dev)) {
  1260. ret = mipi_csis_runtime_resume(dev);
  1261. if (ret < 0)
  1262. goto err_unregister_all;
  1263. }
  1264. dev_info(dev, "lanes: %d, freq: %u\n",
  1265. csis->bus.num_data_lanes, csis->clk_frequency);
  1266. return 0;
  1267. err_unregister_all:
  1268. mipi_csis_debugfs_exit(csis);
  1269. err_cleanup:
  1270. v4l2_subdev_cleanup(&csis->sd);
  1271. media_entity_cleanup(&csis->sd.entity);
  1272. v4l2_async_nf_unregister(&csis->notifier);
  1273. v4l2_async_nf_cleanup(&csis->notifier);
  1274. v4l2_async_unregister_subdev(&csis->sd);
  1275. return ret;
  1276. }
  1277. static void mipi_csis_remove(struct platform_device *pdev)
  1278. {
  1279. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  1280. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1281. mipi_csis_debugfs_exit(csis);
  1282. v4l2_async_nf_unregister(&csis->notifier);
  1283. v4l2_async_nf_cleanup(&csis->notifier);
  1284. v4l2_async_unregister_subdev(&csis->sd);
  1285. if (!pm_runtime_enabled(&pdev->dev))
  1286. mipi_csis_runtime_suspend(&pdev->dev);
  1287. pm_runtime_disable(&pdev->dev);
  1288. v4l2_subdev_cleanup(&csis->sd);
  1289. media_entity_cleanup(&csis->sd.entity);
  1290. pm_runtime_set_suspended(&pdev->dev);
  1291. }
  1292. static const struct of_device_id mipi_csis_of_match[] = {
  1293. {
  1294. .compatible = "fsl,imx7-mipi-csi2",
  1295. .data = &(const struct mipi_csis_info){
  1296. .version = MIPI_CSIS_V3_3,
  1297. .num_clocks = 3,
  1298. },
  1299. }, {
  1300. .compatible = "fsl,imx8mm-mipi-csi2",
  1301. .data = &(const struct mipi_csis_info){
  1302. .version = MIPI_CSIS_V3_6_3,
  1303. .num_clocks = 4,
  1304. },
  1305. },
  1306. { /* sentinel */ },
  1307. };
  1308. MODULE_DEVICE_TABLE(of, mipi_csis_of_match);
  1309. static struct platform_driver mipi_csis_driver = {
  1310. .probe = mipi_csis_probe,
  1311. .remove_new = mipi_csis_remove,
  1312. .driver = {
  1313. .of_match_table = mipi_csis_of_match,
  1314. .name = CSIS_DRIVER_NAME,
  1315. .pm = pm_ptr(&mipi_csis_pm_ops),
  1316. },
  1317. };
  1318. module_platform_driver(mipi_csis_driver);
  1319. MODULE_DESCRIPTION("i.MX7 & i.MX8 MIPI CSI-2 receiver driver");
  1320. MODULE_LICENSE("GPL v2");
  1321. MODULE_ALIAS("platform:imx-mipi-csi2");