imx-mipi-csis.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/reset.h>
  29. #include <linux/spinlock.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-device.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 v4l2_subdev *src_sd;
  290. struct v4l2_mbus_config_mipi_csi2 bus;
  291. u32 clk_frequency;
  292. u32 hs_settle;
  293. u32 clk_settle;
  294. struct mutex lock; /* Protect csis_fmt and format_mbus */
  295. const struct csis_pix_format *csis_fmt;
  296. struct v4l2_mbus_framefmt format_mbus[CSIS_PADS_NUM];
  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. /* Called with the csis.lock mutex held */
  511. static void __mipi_csis_set_format(struct mipi_csis_device *csis)
  512. {
  513. struct v4l2_mbus_framefmt *mf = &csis->format_mbus[CSIS_PAD_SINK];
  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->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->csis_fmt->data_type);
  535. mipi_csis_write(csis, MIPI_CSIS_ISP_CONFIG_CH(0), val);
  536. /* Pixel resolution */
  537. val = mf->width | (mf->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. {
  542. s64 link_freq;
  543. u32 lane_rate;
  544. /* Calculate the line rate from the pixel rate. */
  545. link_freq = v4l2_get_link_freq(csis->src_sd->ctrl_handler,
  546. csis->csis_fmt->width,
  547. csis->bus.num_data_lanes * 2);
  548. if (link_freq < 0) {
  549. dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
  550. (int)link_freq);
  551. return link_freq;
  552. }
  553. lane_rate = link_freq * 2;
  554. if (lane_rate < 80000000 || lane_rate > 1500000000) {
  555. dev_dbg(csis->dev, "Out-of-bound lane rate %u\n", lane_rate);
  556. return -EINVAL;
  557. }
  558. /*
  559. * The HSSETTLE counter value is document in a table, but can also
  560. * easily be calculated. Hardcode the CLKSETTLE value to 0 for now
  561. * (which is documented as corresponding to CSI-2 v0.87 to v1.00) until
  562. * we figure out how to compute it correctly.
  563. */
  564. csis->hs_settle = (lane_rate - 5000000) / 45000000;
  565. csis->clk_settle = 0;
  566. dev_dbg(csis->dev, "lane rate %u, Tclk_settle %u, Ths_settle %u\n",
  567. lane_rate, csis->clk_settle, csis->hs_settle);
  568. if (csis->debug.hs_settle < 0xff) {
  569. dev_dbg(csis->dev, "overriding Ths_settle with %u\n",
  570. csis->debug.hs_settle);
  571. csis->hs_settle = csis->debug.hs_settle;
  572. }
  573. if (csis->debug.clk_settle < 4) {
  574. dev_dbg(csis->dev, "overriding Tclk_settle with %u\n",
  575. csis->debug.clk_settle);
  576. csis->clk_settle = csis->debug.clk_settle;
  577. }
  578. return 0;
  579. }
  580. static void mipi_csis_set_params(struct mipi_csis_device *csis)
  581. {
  582. int lanes = csis->bus.num_data_lanes;
  583. u32 val;
  584. val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
  585. val &= ~MIPI_CSIS_CMN_CTRL_LANE_NR_MASK;
  586. val |= (lanes - 1) << MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET;
  587. if (csis->info->version == MIPI_CSIS_V3_3)
  588. val |= MIPI_CSIS_CMN_CTRL_INTER_MODE;
  589. mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL, val);
  590. __mipi_csis_set_format(csis);
  591. mipi_csis_write(csis, MIPI_CSIS_DPHY_CMN_CTRL,
  592. MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(csis->hs_settle) |
  593. MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(csis->clk_settle));
  594. val = (0 << MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET)
  595. | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET)
  596. | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET);
  597. mipi_csis_write(csis, MIPI_CSIS_ISP_SYNC_CH(0), val);
  598. val = mipi_csis_read(csis, MIPI_CSIS_CLK_CTRL);
  599. val |= MIPI_CSIS_CLK_CTRL_WCLK_SRC;
  600. val |= MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(15);
  601. val &= ~MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK;
  602. mipi_csis_write(csis, MIPI_CSIS_CLK_CTRL, val);
  603. mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_L,
  604. MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV |
  605. MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ |
  606. MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V |
  607. MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV |
  608. MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV |
  609. MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV |
  610. MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(20000000));
  611. mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_H, 0);
  612. /* Update the shadow register. */
  613. val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
  614. mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL,
  615. val | MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW |
  616. MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL);
  617. }
  618. static int mipi_csis_clk_enable(struct mipi_csis_device *csis)
  619. {
  620. return clk_bulk_prepare_enable(csis->info->num_clocks, csis->clks);
  621. }
  622. static void mipi_csis_clk_disable(struct mipi_csis_device *csis)
  623. {
  624. clk_bulk_disable_unprepare(csis->info->num_clocks, csis->clks);
  625. }
  626. static int mipi_csis_clk_get(struct mipi_csis_device *csis)
  627. {
  628. unsigned int i;
  629. int ret;
  630. csis->clks = devm_kcalloc(csis->dev, csis->info->num_clocks,
  631. sizeof(*csis->clks), GFP_KERNEL);
  632. if (!csis->clks)
  633. return -ENOMEM;
  634. for (i = 0; i < csis->info->num_clocks; i++)
  635. csis->clks[i].id = mipi_csis_clk_id[i];
  636. ret = devm_clk_bulk_get(csis->dev, csis->info->num_clocks,
  637. csis->clks);
  638. if (ret < 0)
  639. return ret;
  640. /* Set clock rate */
  641. ret = clk_set_rate(csis->clks[MIPI_CSIS_CLK_WRAP].clk,
  642. csis->clk_frequency);
  643. if (ret < 0)
  644. dev_err(csis->dev, "set rate=%d failed: %d\n",
  645. csis->clk_frequency, ret);
  646. return ret;
  647. }
  648. static void mipi_csis_start_stream(struct mipi_csis_device *csis)
  649. {
  650. mipi_csis_sw_reset(csis);
  651. mipi_csis_set_params(csis);
  652. mipi_csis_system_enable(csis, true);
  653. mipi_csis_enable_interrupts(csis, true);
  654. }
  655. static void mipi_csis_stop_stream(struct mipi_csis_device *csis)
  656. {
  657. mipi_csis_enable_interrupts(csis, false);
  658. mipi_csis_system_enable(csis, false);
  659. }
  660. static irqreturn_t mipi_csis_irq_handler(int irq, void *dev_id)
  661. {
  662. struct mipi_csis_device *csis = dev_id;
  663. unsigned long flags;
  664. unsigned int i;
  665. u32 status;
  666. u32 dbg_status;
  667. status = mipi_csis_read(csis, MIPI_CSIS_INT_SRC);
  668. dbg_status = mipi_csis_read(csis, MIPI_CSIS_DBG_INTR_SRC);
  669. spin_lock_irqsave(&csis->slock, flags);
  670. /* Update the event/error counters */
  671. if ((status & MIPI_CSIS_INT_SRC_ERRORS) || csis->debug.enable) {
  672. for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++) {
  673. struct mipi_csis_event *event = &csis->events[i];
  674. if ((!event->debug && (status & event->mask)) ||
  675. (event->debug && (dbg_status & event->mask)))
  676. event->counter++;
  677. }
  678. }
  679. spin_unlock_irqrestore(&csis->slock, flags);
  680. mipi_csis_write(csis, MIPI_CSIS_INT_SRC, status);
  681. mipi_csis_write(csis, MIPI_CSIS_DBG_INTR_SRC, dbg_status);
  682. return IRQ_HANDLED;
  683. }
  684. /* -----------------------------------------------------------------------------
  685. * PHY regulator and reset
  686. */
  687. static int mipi_csis_phy_enable(struct mipi_csis_device *csis)
  688. {
  689. if (csis->info->version != MIPI_CSIS_V3_3)
  690. return 0;
  691. return regulator_enable(csis->mipi_phy_regulator);
  692. }
  693. static int mipi_csis_phy_disable(struct mipi_csis_device *csis)
  694. {
  695. if (csis->info->version != MIPI_CSIS_V3_3)
  696. return 0;
  697. return regulator_disable(csis->mipi_phy_regulator);
  698. }
  699. static void mipi_csis_phy_reset(struct mipi_csis_device *csis)
  700. {
  701. if (csis->info->version != MIPI_CSIS_V3_3)
  702. return;
  703. reset_control_assert(csis->mrst);
  704. msleep(20);
  705. reset_control_deassert(csis->mrst);
  706. }
  707. static int mipi_csis_phy_init(struct mipi_csis_device *csis)
  708. {
  709. if (csis->info->version != MIPI_CSIS_V3_3)
  710. return 0;
  711. /* Get MIPI PHY reset and regulator. */
  712. csis->mrst = devm_reset_control_get_exclusive(csis->dev, NULL);
  713. if (IS_ERR(csis->mrst))
  714. return PTR_ERR(csis->mrst);
  715. csis->mipi_phy_regulator = devm_regulator_get(csis->dev, "phy");
  716. if (IS_ERR(csis->mipi_phy_regulator))
  717. return PTR_ERR(csis->mipi_phy_regulator);
  718. return regulator_set_voltage(csis->mipi_phy_regulator, 1000000,
  719. 1000000);
  720. }
  721. /* -----------------------------------------------------------------------------
  722. * Debug
  723. */
  724. static void mipi_csis_clear_counters(struct mipi_csis_device *csis)
  725. {
  726. unsigned long flags;
  727. unsigned int i;
  728. spin_lock_irqsave(&csis->slock, flags);
  729. for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++)
  730. csis->events[i].counter = 0;
  731. spin_unlock_irqrestore(&csis->slock, flags);
  732. }
  733. static void mipi_csis_log_counters(struct mipi_csis_device *csis, bool non_errors)
  734. {
  735. unsigned int num_events = non_errors ? MIPI_CSIS_NUM_EVENTS
  736. : MIPI_CSIS_NUM_EVENTS - 8;
  737. unsigned long flags;
  738. unsigned int i;
  739. spin_lock_irqsave(&csis->slock, flags);
  740. for (i = 0; i < num_events; ++i) {
  741. if (csis->events[i].counter > 0 || csis->debug.enable)
  742. dev_info(csis->dev, "%s events: %d\n",
  743. csis->events[i].name,
  744. csis->events[i].counter);
  745. }
  746. spin_unlock_irqrestore(&csis->slock, flags);
  747. }
  748. static int mipi_csis_dump_regs(struct mipi_csis_device *csis)
  749. {
  750. static const struct {
  751. u32 offset;
  752. const char * const name;
  753. } registers[] = {
  754. { MIPI_CSIS_CMN_CTRL, "CMN_CTRL" },
  755. { MIPI_CSIS_CLK_CTRL, "CLK_CTRL" },
  756. { MIPI_CSIS_INT_MSK, "INT_MSK" },
  757. { MIPI_CSIS_DPHY_STATUS, "DPHY_STATUS" },
  758. { MIPI_CSIS_DPHY_CMN_CTRL, "DPHY_CMN_CTRL" },
  759. { MIPI_CSIS_DPHY_SCTRL_L, "DPHY_SCTRL_L" },
  760. { MIPI_CSIS_DPHY_SCTRL_H, "DPHY_SCTRL_H" },
  761. { MIPI_CSIS_ISP_CONFIG_CH(0), "ISP_CONFIG_CH0" },
  762. { MIPI_CSIS_ISP_RESOL_CH(0), "ISP_RESOL_CH0" },
  763. { MIPI_CSIS_SDW_CONFIG_CH(0), "SDW_CONFIG_CH0" },
  764. { MIPI_CSIS_SDW_RESOL_CH(0), "SDW_RESOL_CH0" },
  765. { MIPI_CSIS_DBG_CTRL, "DBG_CTRL" },
  766. { MIPI_CSIS_FRAME_COUNTER_CH(0), "FRAME_COUNTER_CH0" },
  767. };
  768. unsigned int i;
  769. u32 cfg;
  770. if (!pm_runtime_get_if_in_use(csis->dev))
  771. return 0;
  772. dev_info(csis->dev, "--- REGISTERS ---\n");
  773. for (i = 0; i < ARRAY_SIZE(registers); i++) {
  774. cfg = mipi_csis_read(csis, registers[i].offset);
  775. dev_info(csis->dev, "%14s: 0x%08x\n", registers[i].name, cfg);
  776. }
  777. pm_runtime_put(csis->dev);
  778. return 0;
  779. }
  780. static int mipi_csis_dump_regs_show(struct seq_file *m, void *private)
  781. {
  782. struct mipi_csis_device *csis = m->private;
  783. return mipi_csis_dump_regs(csis);
  784. }
  785. DEFINE_SHOW_ATTRIBUTE(mipi_csis_dump_regs);
  786. static void mipi_csis_debugfs_init(struct mipi_csis_device *csis)
  787. {
  788. csis->debug.hs_settle = UINT_MAX;
  789. csis->debug.clk_settle = UINT_MAX;
  790. csis->debugfs_root = debugfs_create_dir(dev_name(csis->dev), NULL);
  791. debugfs_create_bool("debug_enable", 0600, csis->debugfs_root,
  792. &csis->debug.enable);
  793. debugfs_create_file("dump_regs", 0600, csis->debugfs_root, csis,
  794. &mipi_csis_dump_regs_fops);
  795. debugfs_create_u32("tclk_settle", 0600, csis->debugfs_root,
  796. &csis->debug.clk_settle);
  797. debugfs_create_u32("ths_settle", 0600, csis->debugfs_root,
  798. &csis->debug.hs_settle);
  799. }
  800. static void mipi_csis_debugfs_exit(struct mipi_csis_device *csis)
  801. {
  802. debugfs_remove_recursive(csis->debugfs_root);
  803. }
  804. /* -----------------------------------------------------------------------------
  805. * V4L2 subdev operations
  806. */
  807. static struct mipi_csis_device *sd_to_mipi_csis_device(struct v4l2_subdev *sdev)
  808. {
  809. return container_of(sdev, struct mipi_csis_device, sd);
  810. }
  811. static int mipi_csis_s_stream(struct v4l2_subdev *sd, int enable)
  812. {
  813. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  814. int ret;
  815. if (!enable) {
  816. mutex_lock(&csis->lock);
  817. v4l2_subdev_call(csis->src_sd, video, s_stream, 0);
  818. mipi_csis_stop_stream(csis);
  819. if (csis->debug.enable)
  820. mipi_csis_log_counters(csis, true);
  821. mutex_unlock(&csis->lock);
  822. pm_runtime_put(csis->dev);
  823. return 0;
  824. }
  825. ret = mipi_csis_calculate_params(csis);
  826. if (ret < 0)
  827. return ret;
  828. mipi_csis_clear_counters(csis);
  829. ret = pm_runtime_resume_and_get(csis->dev);
  830. if (ret < 0)
  831. return ret;
  832. mutex_lock(&csis->lock);
  833. mipi_csis_start_stream(csis);
  834. ret = v4l2_subdev_call(csis->src_sd, video, s_stream, 1);
  835. if (ret < 0)
  836. goto error;
  837. mipi_csis_log_counters(csis, true);
  838. mutex_unlock(&csis->lock);
  839. return 0;
  840. error:
  841. mipi_csis_stop_stream(csis);
  842. mutex_unlock(&csis->lock);
  843. pm_runtime_put(csis->dev);
  844. return ret;
  845. }
  846. static struct v4l2_mbus_framefmt *
  847. mipi_csis_get_format(struct mipi_csis_device *csis,
  848. struct v4l2_subdev_state *sd_state,
  849. enum v4l2_subdev_format_whence which,
  850. unsigned int pad)
  851. {
  852. if (which == V4L2_SUBDEV_FORMAT_TRY)
  853. return v4l2_subdev_get_try_format(&csis->sd, sd_state, pad);
  854. return &csis->format_mbus[pad];
  855. }
  856. static int mipi_csis_init_cfg(struct v4l2_subdev *sd,
  857. struct v4l2_subdev_state *sd_state)
  858. {
  859. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  860. struct v4l2_mbus_framefmt *fmt_sink;
  861. struct v4l2_mbus_framefmt *fmt_source;
  862. enum v4l2_subdev_format_whence which;
  863. which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  864. fmt_sink = mipi_csis_get_format(csis, sd_state, which, CSIS_PAD_SINK);
  865. fmt_sink->code = MEDIA_BUS_FMT_UYVY8_1X16;
  866. fmt_sink->width = MIPI_CSIS_DEF_PIX_WIDTH;
  867. fmt_sink->height = MIPI_CSIS_DEF_PIX_HEIGHT;
  868. fmt_sink->field = V4L2_FIELD_NONE;
  869. fmt_sink->colorspace = V4L2_COLORSPACE_SMPTE170M;
  870. fmt_sink->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt_sink->colorspace);
  871. fmt_sink->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt_sink->colorspace);
  872. fmt_sink->quantization =
  873. V4L2_MAP_QUANTIZATION_DEFAULT(false, fmt_sink->colorspace,
  874. fmt_sink->ycbcr_enc);
  875. fmt_source = mipi_csis_get_format(csis, sd_state, which,
  876. CSIS_PAD_SOURCE);
  877. *fmt_source = *fmt_sink;
  878. return 0;
  879. }
  880. static int mipi_csis_get_fmt(struct v4l2_subdev *sd,
  881. struct v4l2_subdev_state *sd_state,
  882. struct v4l2_subdev_format *sdformat)
  883. {
  884. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  885. struct v4l2_mbus_framefmt *fmt;
  886. fmt = mipi_csis_get_format(csis, sd_state, sdformat->which,
  887. sdformat->pad);
  888. mutex_lock(&csis->lock);
  889. sdformat->format = *fmt;
  890. mutex_unlock(&csis->lock);
  891. return 0;
  892. }
  893. static int mipi_csis_enum_mbus_code(struct v4l2_subdev *sd,
  894. struct v4l2_subdev_state *sd_state,
  895. struct v4l2_subdev_mbus_code_enum *code)
  896. {
  897. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  898. /*
  899. * The CSIS can't transcode in any way, the source format is identical
  900. * to the sink format.
  901. */
  902. if (code->pad == CSIS_PAD_SOURCE) {
  903. struct v4l2_mbus_framefmt *fmt;
  904. if (code->index > 0)
  905. return -EINVAL;
  906. fmt = mipi_csis_get_format(csis, sd_state, code->which,
  907. code->pad);
  908. code->code = fmt->code;
  909. return 0;
  910. }
  911. if (code->pad != CSIS_PAD_SINK)
  912. return -EINVAL;
  913. if (code->index >= ARRAY_SIZE(mipi_csis_formats))
  914. return -EINVAL;
  915. code->code = mipi_csis_formats[code->index].code;
  916. return 0;
  917. }
  918. static int mipi_csis_set_fmt(struct v4l2_subdev *sd,
  919. struct v4l2_subdev_state *sd_state,
  920. struct v4l2_subdev_format *sdformat)
  921. {
  922. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  923. struct csis_pix_format const *csis_fmt;
  924. struct v4l2_mbus_framefmt *fmt;
  925. unsigned int align;
  926. /*
  927. * The CSIS can't transcode in any way, the source format can't be
  928. * modified.
  929. */
  930. if (sdformat->pad == CSIS_PAD_SOURCE)
  931. return mipi_csis_get_fmt(sd, sd_state, sdformat);
  932. if (sdformat->pad != CSIS_PAD_SINK)
  933. return -EINVAL;
  934. /*
  935. * Validate the media bus code and clamp and align the size.
  936. *
  937. * The total number of bits per line must be a multiple of 8. We thus
  938. * need to align the width for formats that are not multiples of 8
  939. * bits.
  940. */
  941. csis_fmt = find_csis_format(sdformat->format.code);
  942. if (!csis_fmt)
  943. csis_fmt = &mipi_csis_formats[0];
  944. switch (csis_fmt->width % 8) {
  945. case 0:
  946. align = 0;
  947. break;
  948. case 4:
  949. align = 1;
  950. break;
  951. case 2:
  952. case 6:
  953. align = 2;
  954. break;
  955. default:
  956. /* 1, 3, 5, 7 */
  957. align = 3;
  958. break;
  959. }
  960. v4l_bound_align_image(&sdformat->format.width, 1,
  961. CSIS_MAX_PIX_WIDTH, align,
  962. &sdformat->format.height, 1,
  963. CSIS_MAX_PIX_HEIGHT, 0, 0);
  964. fmt = mipi_csis_get_format(csis, sd_state, sdformat->which,
  965. sdformat->pad);
  966. mutex_lock(&csis->lock);
  967. fmt->code = csis_fmt->code;
  968. fmt->width = sdformat->format.width;
  969. fmt->height = sdformat->format.height;
  970. fmt->colorspace = sdformat->format.colorspace;
  971. fmt->quantization = sdformat->format.quantization;
  972. fmt->xfer_func = sdformat->format.xfer_func;
  973. fmt->ycbcr_enc = sdformat->format.ycbcr_enc;
  974. sdformat->format = *fmt;
  975. /* Propagate the format from sink to source. */
  976. fmt = mipi_csis_get_format(csis, sd_state, sdformat->which,
  977. CSIS_PAD_SOURCE);
  978. *fmt = sdformat->format;
  979. /* The format on the source pad might change due to unpacking. */
  980. fmt->code = csis_fmt->output;
  981. /* Store the CSIS format descriptor for active formats. */
  982. if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  983. csis->csis_fmt = csis_fmt;
  984. mutex_unlock(&csis->lock);
  985. return 0;
  986. }
  987. static int mipi_csis_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  988. struct v4l2_mbus_frame_desc *fd)
  989. {
  990. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  991. struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[0];
  992. if (pad != CSIS_PAD_SOURCE)
  993. return -EINVAL;
  994. fd->type = V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL;
  995. fd->num_entries = 1;
  996. memset(entry, 0, sizeof(*entry));
  997. mutex_lock(&csis->lock);
  998. entry->flags = 0;
  999. entry->pixelcode = csis->csis_fmt->code;
  1000. entry->bus.csi2.vc = 0;
  1001. entry->bus.csi2.dt = csis->csis_fmt->data_type;
  1002. mutex_unlock(&csis->lock);
  1003. return 0;
  1004. }
  1005. static int mipi_csis_log_status(struct v4l2_subdev *sd)
  1006. {
  1007. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1008. mipi_csis_log_counters(csis, true);
  1009. if (csis->debug.enable)
  1010. mipi_csis_dump_regs(csis);
  1011. return 0;
  1012. }
  1013. static const struct v4l2_subdev_core_ops mipi_csis_core_ops = {
  1014. .log_status = mipi_csis_log_status,
  1015. };
  1016. static const struct v4l2_subdev_video_ops mipi_csis_video_ops = {
  1017. .s_stream = mipi_csis_s_stream,
  1018. };
  1019. static const struct v4l2_subdev_pad_ops mipi_csis_pad_ops = {
  1020. .init_cfg = mipi_csis_init_cfg,
  1021. .enum_mbus_code = mipi_csis_enum_mbus_code,
  1022. .get_fmt = mipi_csis_get_fmt,
  1023. .set_fmt = mipi_csis_set_fmt,
  1024. .get_frame_desc = mipi_csis_get_frame_desc,
  1025. };
  1026. static const struct v4l2_subdev_ops mipi_csis_subdev_ops = {
  1027. .core = &mipi_csis_core_ops,
  1028. .video = &mipi_csis_video_ops,
  1029. .pad = &mipi_csis_pad_ops,
  1030. };
  1031. /* -----------------------------------------------------------------------------
  1032. * Media entity operations
  1033. */
  1034. static int mipi_csis_link_setup(struct media_entity *entity,
  1035. const struct media_pad *local_pad,
  1036. const struct media_pad *remote_pad, u32 flags)
  1037. {
  1038. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1039. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1040. struct v4l2_subdev *remote_sd;
  1041. dev_dbg(csis->dev, "link setup %s -> %s", remote_pad->entity->name,
  1042. local_pad->entity->name);
  1043. /* We only care about the link to the source. */
  1044. if (!(local_pad->flags & MEDIA_PAD_FL_SINK))
  1045. return 0;
  1046. remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
  1047. if (flags & MEDIA_LNK_FL_ENABLED) {
  1048. if (csis->src_sd)
  1049. return -EBUSY;
  1050. csis->src_sd = remote_sd;
  1051. } else {
  1052. csis->src_sd = NULL;
  1053. }
  1054. return 0;
  1055. }
  1056. static const struct media_entity_operations mipi_csis_entity_ops = {
  1057. .link_setup = mipi_csis_link_setup,
  1058. .link_validate = v4l2_subdev_link_validate,
  1059. .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
  1060. };
  1061. /* -----------------------------------------------------------------------------
  1062. * Async subdev notifier
  1063. */
  1064. static struct mipi_csis_device *
  1065. mipi_notifier_to_csis_state(struct v4l2_async_notifier *n)
  1066. {
  1067. return container_of(n, struct mipi_csis_device, notifier);
  1068. }
  1069. static int mipi_csis_notify_bound(struct v4l2_async_notifier *notifier,
  1070. struct v4l2_subdev *sd,
  1071. struct v4l2_async_subdev *asd)
  1072. {
  1073. struct mipi_csis_device *csis = mipi_notifier_to_csis_state(notifier);
  1074. struct media_pad *sink = &csis->sd.entity.pads[CSIS_PAD_SINK];
  1075. return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
  1076. }
  1077. static const struct v4l2_async_notifier_operations mipi_csis_notify_ops = {
  1078. .bound = mipi_csis_notify_bound,
  1079. };
  1080. static int mipi_csis_async_register(struct mipi_csis_device *csis)
  1081. {
  1082. struct v4l2_fwnode_endpoint vep = {
  1083. .bus_type = V4L2_MBUS_CSI2_DPHY,
  1084. };
  1085. struct v4l2_async_subdev *asd;
  1086. struct fwnode_handle *ep;
  1087. unsigned int i;
  1088. int ret;
  1089. v4l2_async_nf_init(&csis->notifier);
  1090. ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev), 0, 0,
  1091. FWNODE_GRAPH_ENDPOINT_NEXT);
  1092. if (!ep)
  1093. return -ENOTCONN;
  1094. ret = v4l2_fwnode_endpoint_parse(ep, &vep);
  1095. if (ret)
  1096. goto err_parse;
  1097. for (i = 0; i < vep.bus.mipi_csi2.num_data_lanes; ++i) {
  1098. if (vep.bus.mipi_csi2.data_lanes[i] != i + 1) {
  1099. dev_err(csis->dev,
  1100. "data lanes reordering is not supported");
  1101. ret = -EINVAL;
  1102. goto err_parse;
  1103. }
  1104. }
  1105. csis->bus = vep.bus.mipi_csi2;
  1106. dev_dbg(csis->dev, "data lanes: %d\n", csis->bus.num_data_lanes);
  1107. dev_dbg(csis->dev, "flags: 0x%08x\n", csis->bus.flags);
  1108. asd = v4l2_async_nf_add_fwnode_remote(&csis->notifier, ep,
  1109. struct v4l2_async_subdev);
  1110. if (IS_ERR(asd)) {
  1111. ret = PTR_ERR(asd);
  1112. goto err_parse;
  1113. }
  1114. fwnode_handle_put(ep);
  1115. csis->notifier.ops = &mipi_csis_notify_ops;
  1116. ret = v4l2_async_subdev_nf_register(&csis->sd, &csis->notifier);
  1117. if (ret)
  1118. return ret;
  1119. return v4l2_async_register_subdev(&csis->sd);
  1120. err_parse:
  1121. fwnode_handle_put(ep);
  1122. return ret;
  1123. }
  1124. /* -----------------------------------------------------------------------------
  1125. * Suspend/resume
  1126. */
  1127. static int __maybe_unused mipi_csis_runtime_suspend(struct device *dev)
  1128. {
  1129. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1130. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1131. int ret = 0;
  1132. mutex_lock(&csis->lock);
  1133. ret = mipi_csis_phy_disable(csis);
  1134. if (ret)
  1135. goto unlock;
  1136. mipi_csis_clk_disable(csis);
  1137. unlock:
  1138. mutex_unlock(&csis->lock);
  1139. return ret ? -EAGAIN : 0;
  1140. }
  1141. static int __maybe_unused mipi_csis_runtime_resume(struct device *dev)
  1142. {
  1143. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1144. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1145. int ret = 0;
  1146. mutex_lock(&csis->lock);
  1147. ret = mipi_csis_phy_enable(csis);
  1148. if (ret)
  1149. goto unlock;
  1150. mipi_csis_clk_enable(csis);
  1151. unlock:
  1152. mutex_unlock(&csis->lock);
  1153. return ret ? -EAGAIN : 0;
  1154. }
  1155. static const struct dev_pm_ops mipi_csis_pm_ops = {
  1156. SET_RUNTIME_PM_OPS(mipi_csis_runtime_suspend, mipi_csis_runtime_resume,
  1157. NULL)
  1158. };
  1159. /* -----------------------------------------------------------------------------
  1160. * Probe/remove & platform driver
  1161. */
  1162. static int mipi_csis_subdev_init(struct mipi_csis_device *csis)
  1163. {
  1164. struct v4l2_subdev *sd = &csis->sd;
  1165. v4l2_subdev_init(sd, &mipi_csis_subdev_ops);
  1166. sd->owner = THIS_MODULE;
  1167. snprintf(sd->name, sizeof(sd->name), "csis-%s",
  1168. dev_name(csis->dev));
  1169. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1170. sd->ctrl_handler = NULL;
  1171. sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  1172. sd->entity.ops = &mipi_csis_entity_ops;
  1173. sd->dev = csis->dev;
  1174. sd->fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev),
  1175. 1, 0, 0);
  1176. if (!sd->fwnode) {
  1177. dev_err(csis->dev, "Unable to retrieve endpoint for port@1\n");
  1178. return -ENOENT;
  1179. }
  1180. csis->csis_fmt = &mipi_csis_formats[0];
  1181. mipi_csis_init_cfg(sd, NULL);
  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. return media_entity_pads_init(&sd->entity, CSIS_PADS_NUM,
  1187. csis->pads);
  1188. }
  1189. static int mipi_csis_parse_dt(struct mipi_csis_device *csis)
  1190. {
  1191. struct device_node *node = csis->dev->of_node;
  1192. if (of_property_read_u32(node, "clock-frequency",
  1193. &csis->clk_frequency))
  1194. csis->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
  1195. return 0;
  1196. }
  1197. static int mipi_csis_probe(struct platform_device *pdev)
  1198. {
  1199. struct device *dev = &pdev->dev;
  1200. struct mipi_csis_device *csis;
  1201. int irq;
  1202. int ret;
  1203. csis = devm_kzalloc(dev, sizeof(*csis), GFP_KERNEL);
  1204. if (!csis)
  1205. return -ENOMEM;
  1206. mutex_init(&csis->lock);
  1207. spin_lock_init(&csis->slock);
  1208. csis->dev = dev;
  1209. csis->info = of_device_get_match_data(dev);
  1210. memcpy(csis->events, mipi_csis_events, sizeof(csis->events));
  1211. /* Parse DT properties. */
  1212. ret = mipi_csis_parse_dt(csis);
  1213. if (ret < 0) {
  1214. dev_err(dev, "Failed to parse device tree: %d\n", ret);
  1215. return ret;
  1216. }
  1217. /* Acquire resources. */
  1218. csis->regs = devm_platform_ioremap_resource(pdev, 0);
  1219. if (IS_ERR(csis->regs))
  1220. return PTR_ERR(csis->regs);
  1221. irq = platform_get_irq(pdev, 0);
  1222. if (irq < 0)
  1223. return irq;
  1224. ret = mipi_csis_phy_init(csis);
  1225. if (ret < 0)
  1226. return ret;
  1227. ret = mipi_csis_clk_get(csis);
  1228. if (ret < 0)
  1229. return ret;
  1230. /* Reset PHY and enable the clocks. */
  1231. mipi_csis_phy_reset(csis);
  1232. ret = mipi_csis_clk_enable(csis);
  1233. if (ret < 0) {
  1234. dev_err(csis->dev, "failed to enable clocks: %d\n", ret);
  1235. return ret;
  1236. }
  1237. /* Now that the hardware is initialized, request the interrupt. */
  1238. ret = devm_request_irq(dev, irq, mipi_csis_irq_handler, 0,
  1239. dev_name(dev), csis);
  1240. if (ret) {
  1241. dev_err(dev, "Interrupt request failed\n");
  1242. goto disable_clock;
  1243. }
  1244. /* Initialize and register the subdev. */
  1245. ret = mipi_csis_subdev_init(csis);
  1246. if (ret < 0)
  1247. goto disable_clock;
  1248. platform_set_drvdata(pdev, &csis->sd);
  1249. ret = mipi_csis_async_register(csis);
  1250. if (ret < 0) {
  1251. dev_err(dev, "async register failed: %d\n", ret);
  1252. goto cleanup;
  1253. }
  1254. /* Initialize debugfs. */
  1255. mipi_csis_debugfs_init(csis);
  1256. /* Enable runtime PM. */
  1257. pm_runtime_enable(dev);
  1258. if (!pm_runtime_enabled(dev)) {
  1259. ret = mipi_csis_runtime_resume(dev);
  1260. if (ret < 0)
  1261. goto unregister_all;
  1262. }
  1263. dev_info(dev, "lanes: %d, freq: %u\n",
  1264. csis->bus.num_data_lanes, csis->clk_frequency);
  1265. return 0;
  1266. unregister_all:
  1267. mipi_csis_debugfs_exit(csis);
  1268. cleanup:
  1269. media_entity_cleanup(&csis->sd.entity);
  1270. v4l2_async_nf_unregister(&csis->notifier);
  1271. v4l2_async_nf_cleanup(&csis->notifier);
  1272. v4l2_async_unregister_subdev(&csis->sd);
  1273. disable_clock:
  1274. mipi_csis_clk_disable(csis);
  1275. fwnode_handle_put(csis->sd.fwnode);
  1276. mutex_destroy(&csis->lock);
  1277. return ret;
  1278. }
  1279. static int mipi_csis_remove(struct platform_device *pdev)
  1280. {
  1281. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  1282. struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
  1283. mipi_csis_debugfs_exit(csis);
  1284. v4l2_async_nf_unregister(&csis->notifier);
  1285. v4l2_async_nf_cleanup(&csis->notifier);
  1286. v4l2_async_unregister_subdev(&csis->sd);
  1287. pm_runtime_disable(&pdev->dev);
  1288. mipi_csis_runtime_suspend(&pdev->dev);
  1289. mipi_csis_clk_disable(csis);
  1290. media_entity_cleanup(&csis->sd.entity);
  1291. fwnode_handle_put(csis->sd.fwnode);
  1292. mutex_destroy(&csis->lock);
  1293. pm_runtime_set_suspended(&pdev->dev);
  1294. return 0;
  1295. }
  1296. static const struct of_device_id mipi_csis_of_match[] = {
  1297. {
  1298. .compatible = "fsl,imx7-mipi-csi2",
  1299. .data = &(const struct mipi_csis_info){
  1300. .version = MIPI_CSIS_V3_3,
  1301. .num_clocks = 3,
  1302. },
  1303. }, {
  1304. .compatible = "fsl,imx8mm-mipi-csi2",
  1305. .data = &(const struct mipi_csis_info){
  1306. .version = MIPI_CSIS_V3_6_3,
  1307. .num_clocks = 4,
  1308. },
  1309. },
  1310. { /* sentinel */ },
  1311. };
  1312. MODULE_DEVICE_TABLE(of, mipi_csis_of_match);
  1313. static struct platform_driver mipi_csis_driver = {
  1314. .probe = mipi_csis_probe,
  1315. .remove = mipi_csis_remove,
  1316. .driver = {
  1317. .of_match_table = mipi_csis_of_match,
  1318. .name = CSIS_DRIVER_NAME,
  1319. .pm = &mipi_csis_pm_ops,
  1320. },
  1321. };
  1322. module_platform_driver(mipi_csis_driver);
  1323. MODULE_DESCRIPTION("i.MX7 & i.MX8 MIPI CSI-2 receiver driver");
  1324. MODULE_LICENSE("GPL v2");
  1325. MODULE_ALIAS("platform:imx-mipi-csi2");