ti-sn65dsi83.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * TI SN65DSI83,84,85 driver
  4. *
  5. * Currently supported:
  6. * - SN65DSI83
  7. * = 1x Single-link DSI ~ 1x Single-link LVDS
  8. * - Supported
  9. * - Single-link LVDS mode tested
  10. * - SN65DSI84
  11. * = 1x Single-link DSI ~ 2x Single-link or 1x Dual-link LVDS
  12. * - Supported
  13. * - Dual-link LVDS mode tested
  14. * - 2x Single-link LVDS mode unsupported
  15. * (should be easy to add by someone who has the HW)
  16. * - SN65DSI85
  17. * = 2x Single-link or 1x Dual-link DSI ~ 2x Single-link or 1x Dual-link LVDS
  18. * - Unsupported
  19. * (should be easy to add by someone who has the HW)
  20. *
  21. * Copyright (C) 2021 Marek Vasut <marex@denx.de>
  22. *
  23. * Based on previous work of:
  24. * Valentin Raevsky <valentin@compulab.co.il>
  25. * Philippe Schenker <philippe.schenker@toradex.com>
  26. */
  27. #include <linux/bits.h>
  28. #include <linux/clk.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <linux/i2c.h>
  31. #include <linux/media-bus-format.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/of_graph.h>
  35. #include <linux/regmap.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <linux/timer.h>
  38. #include <linux/workqueue.h>
  39. #include <drm/drm_atomic_helper.h>
  40. #include <drm/drm_bridge.h>
  41. #include <drm/drm_bridge_helper.h>
  42. #include <drm/drm_mipi_dsi.h>
  43. #include <drm/drm_of.h>
  44. #include <drm/drm_print.h>
  45. #include <drm/drm_probe_helper.h>
  46. /* ID registers */
  47. #define REG_ID(n) (0x00 + (n))
  48. /* Reset and clock registers */
  49. #define REG_RC_RESET 0x09
  50. #define REG_RC_RESET_SOFT_RESET BIT(0)
  51. #define REG_RC_LVDS_PLL 0x0a
  52. #define REG_RC_LVDS_PLL_PLL_EN_STAT BIT(7)
  53. #define REG_RC_LVDS_PLL_LVDS_CLK_RANGE(n) (((n) & 0x7) << 1)
  54. #define REG_RC_LVDS_PLL_HS_CLK_SRC_DPHY BIT(0)
  55. #define REG_RC_DSI_CLK 0x0b
  56. #define REG_RC_DSI_CLK_DSI_CLK_DIVIDER(n) (((n) & 0x1f) << 3)
  57. #define REG_RC_DSI_CLK_REFCLK_MULTIPLIER(n) ((n) & 0x3)
  58. #define REG_RC_PLL_EN 0x0d
  59. #define REG_RC_PLL_EN_PLL_EN BIT(0)
  60. /* DSI registers */
  61. #define REG_DSI_LANE 0x10
  62. #define REG_DSI_LANE_LEFT_RIGHT_PIXELS BIT(7) /* DSI85-only */
  63. #define REG_DSI_LANE_DSI_CHANNEL_MODE_DUAL 0 /* DSI85-only */
  64. #define REG_DSI_LANE_DSI_CHANNEL_MODE_2SINGLE BIT(6) /* DSI85-only */
  65. #define REG_DSI_LANE_DSI_CHANNEL_MODE_SINGLE BIT(5)
  66. #define REG_DSI_LANE_CHA_DSI_LANES(n) (((n) & 0x3) << 3)
  67. #define REG_DSI_LANE_CHB_DSI_LANES(n) (((n) & 0x3) << 1)
  68. #define REG_DSI_LANE_SOT_ERR_TOL_DIS BIT(0)
  69. #define REG_DSI_EQ 0x11
  70. #define REG_DSI_EQ_CHA_DSI_DATA_EQ(n) (((n) & 0x3) << 6)
  71. #define REG_DSI_EQ_CHA_DSI_CLK_EQ(n) (((n) & 0x3) << 2)
  72. #define REG_DSI_CLK 0x12
  73. #define REG_DSI_CLK_CHA_DSI_CLK_RANGE(n) ((n) & 0xff)
  74. /* LVDS registers */
  75. #define REG_LVDS_FMT 0x18
  76. #define REG_LVDS_FMT_DE_NEG_POLARITY BIT(7)
  77. #define REG_LVDS_FMT_HS_NEG_POLARITY BIT(6)
  78. #define REG_LVDS_FMT_VS_NEG_POLARITY BIT(5)
  79. #define REG_LVDS_FMT_LVDS_LINK_CFG BIT(4) /* 0:AB 1:A-only */
  80. #define REG_LVDS_FMT_CHA_24BPP_MODE BIT(3)
  81. #define REG_LVDS_FMT_CHB_24BPP_MODE BIT(2)
  82. #define REG_LVDS_FMT_CHA_24BPP_FORMAT1 BIT(1)
  83. #define REG_LVDS_FMT_CHB_24BPP_FORMAT1 BIT(0)
  84. #define REG_LVDS_VCOM 0x19
  85. #define REG_LVDS_VCOM_CHA_LVDS_VOCM BIT(6)
  86. #define REG_LVDS_VCOM_CHB_LVDS_VOCM BIT(4)
  87. #define REG_LVDS_VCOM_CHA_LVDS_VOD_SWING(n) (((n) & 0x3) << 2)
  88. #define REG_LVDS_VCOM_CHB_LVDS_VOD_SWING(n) ((n) & 0x3)
  89. #define REG_LVDS_LANE 0x1a
  90. #define REG_LVDS_LANE_EVEN_ODD_SWAP BIT(6)
  91. #define REG_LVDS_LANE_CHA_REVERSE_LVDS BIT(5)
  92. #define REG_LVDS_LANE_CHB_REVERSE_LVDS BIT(4)
  93. #define REG_LVDS_LANE_CHA_LVDS_TERM BIT(1)
  94. #define REG_LVDS_LANE_CHB_LVDS_TERM BIT(0)
  95. #define REG_LVDS_CM 0x1b
  96. #define REG_LVDS_CM_CHA_LVDS_CM_ADJUST(n) (((n) & 0x3) << 4)
  97. #define REG_LVDS_CM_CHB_LVDS_CM_ADJUST(n) ((n) & 0x3)
  98. /* Video registers */
  99. #define REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW 0x20
  100. #define REG_VID_CHA_ACTIVE_LINE_LENGTH_HIGH 0x21
  101. #define REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW 0x24
  102. #define REG_VID_CHA_VERTICAL_DISPLAY_SIZE_HIGH 0x25
  103. #define REG_VID_CHA_SYNC_DELAY_LOW 0x28
  104. #define REG_VID_CHA_SYNC_DELAY_HIGH 0x29
  105. #define REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW 0x2c
  106. #define REG_VID_CHA_HSYNC_PULSE_WIDTH_HIGH 0x2d
  107. #define REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW 0x30
  108. #define REG_VID_CHA_VSYNC_PULSE_WIDTH_HIGH 0x31
  109. #define REG_VID_CHA_HORIZONTAL_BACK_PORCH 0x34
  110. #define REG_VID_CHA_VERTICAL_BACK_PORCH 0x36
  111. #define REG_VID_CHA_HORIZONTAL_FRONT_PORCH 0x38
  112. #define REG_VID_CHA_VERTICAL_FRONT_PORCH 0x3a
  113. #define REG_VID_CHA_TEST_PATTERN 0x3c
  114. /* IRQ registers */
  115. #define REG_IRQ_GLOBAL 0xe0
  116. #define REG_IRQ_GLOBAL_IRQ_EN BIT(0)
  117. #define REG_IRQ_EN 0xe1
  118. #define REG_IRQ_EN_CHA_SYNCH_ERR_EN BIT(7)
  119. #define REG_IRQ_EN_CHA_CRC_ERR_EN BIT(6)
  120. #define REG_IRQ_EN_CHA_UNC_ECC_ERR_EN BIT(5)
  121. #define REG_IRQ_EN_CHA_COR_ECC_ERR_EN BIT(4)
  122. #define REG_IRQ_EN_CHA_LLP_ERR_EN BIT(3)
  123. #define REG_IRQ_EN_CHA_SOT_BIT_ERR_EN BIT(2)
  124. #define REG_IRQ_EN_CHA_PLL_UNLOCK_EN BIT(0)
  125. #define REG_IRQ_STAT 0xe5
  126. #define REG_IRQ_STAT_CHA_SYNCH_ERR BIT(7)
  127. #define REG_IRQ_STAT_CHA_CRC_ERR BIT(6)
  128. #define REG_IRQ_STAT_CHA_UNC_ECC_ERR BIT(5)
  129. #define REG_IRQ_STAT_CHA_COR_ECC_ERR BIT(4)
  130. #define REG_IRQ_STAT_CHA_LLP_ERR BIT(3)
  131. #define REG_IRQ_STAT_CHA_SOT_BIT_ERR BIT(2)
  132. #define REG_IRQ_STAT_CHA_PLL_UNLOCK BIT(0)
  133. enum sn65dsi83_channel {
  134. CHANNEL_A,
  135. CHANNEL_B
  136. };
  137. enum sn65dsi83_lvds_term {
  138. OHM_100,
  139. OHM_200
  140. };
  141. enum sn65dsi83_model {
  142. MODEL_SN65DSI83,
  143. MODEL_SN65DSI84,
  144. };
  145. struct sn65dsi83 {
  146. struct drm_bridge bridge;
  147. struct device *dev;
  148. struct regmap *regmap;
  149. struct mipi_dsi_device *dsi;
  150. struct drm_bridge *panel_bridge;
  151. struct gpio_desc *enable_gpio;
  152. struct regulator *vcc;
  153. bool lvds_dual_link;
  154. bool lvds_dual_link_even_odd_swap;
  155. int lvds_vod_swing_conf[2];
  156. int lvds_term_conf[2];
  157. int irq;
  158. struct delayed_work monitor_work;
  159. struct work_struct reset_work;
  160. };
  161. static const struct regmap_range sn65dsi83_readable_ranges[] = {
  162. regmap_reg_range(REG_ID(0), REG_ID(8)),
  163. regmap_reg_range(REG_RC_LVDS_PLL, REG_RC_DSI_CLK),
  164. regmap_reg_range(REG_RC_PLL_EN, REG_RC_PLL_EN),
  165. regmap_reg_range(REG_DSI_LANE, REG_DSI_CLK),
  166. regmap_reg_range(REG_LVDS_FMT, REG_LVDS_CM),
  167. regmap_reg_range(REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW,
  168. REG_VID_CHA_ACTIVE_LINE_LENGTH_HIGH),
  169. regmap_reg_range(REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW,
  170. REG_VID_CHA_VERTICAL_DISPLAY_SIZE_HIGH),
  171. regmap_reg_range(REG_VID_CHA_SYNC_DELAY_LOW,
  172. REG_VID_CHA_SYNC_DELAY_HIGH),
  173. regmap_reg_range(REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW,
  174. REG_VID_CHA_HSYNC_PULSE_WIDTH_HIGH),
  175. regmap_reg_range(REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW,
  176. REG_VID_CHA_VSYNC_PULSE_WIDTH_HIGH),
  177. regmap_reg_range(REG_VID_CHA_HORIZONTAL_BACK_PORCH,
  178. REG_VID_CHA_HORIZONTAL_BACK_PORCH),
  179. regmap_reg_range(REG_VID_CHA_VERTICAL_BACK_PORCH,
  180. REG_VID_CHA_VERTICAL_BACK_PORCH),
  181. regmap_reg_range(REG_VID_CHA_HORIZONTAL_FRONT_PORCH,
  182. REG_VID_CHA_HORIZONTAL_FRONT_PORCH),
  183. regmap_reg_range(REG_VID_CHA_VERTICAL_FRONT_PORCH,
  184. REG_VID_CHA_VERTICAL_FRONT_PORCH),
  185. regmap_reg_range(REG_VID_CHA_TEST_PATTERN, REG_VID_CHA_TEST_PATTERN),
  186. regmap_reg_range(REG_IRQ_GLOBAL, REG_IRQ_EN),
  187. regmap_reg_range(REG_IRQ_STAT, REG_IRQ_STAT),
  188. };
  189. static const struct regmap_access_table sn65dsi83_readable_table = {
  190. .yes_ranges = sn65dsi83_readable_ranges,
  191. .n_yes_ranges = ARRAY_SIZE(sn65dsi83_readable_ranges),
  192. };
  193. static const struct regmap_range sn65dsi83_writeable_ranges[] = {
  194. regmap_reg_range(REG_RC_RESET, REG_RC_DSI_CLK),
  195. regmap_reg_range(REG_RC_PLL_EN, REG_RC_PLL_EN),
  196. regmap_reg_range(REG_DSI_LANE, REG_DSI_CLK),
  197. regmap_reg_range(REG_LVDS_FMT, REG_LVDS_CM),
  198. regmap_reg_range(REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW,
  199. REG_VID_CHA_ACTIVE_LINE_LENGTH_HIGH),
  200. regmap_reg_range(REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW,
  201. REG_VID_CHA_VERTICAL_DISPLAY_SIZE_HIGH),
  202. regmap_reg_range(REG_VID_CHA_SYNC_DELAY_LOW,
  203. REG_VID_CHA_SYNC_DELAY_HIGH),
  204. regmap_reg_range(REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW,
  205. REG_VID_CHA_HSYNC_PULSE_WIDTH_HIGH),
  206. regmap_reg_range(REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW,
  207. REG_VID_CHA_VSYNC_PULSE_WIDTH_HIGH),
  208. regmap_reg_range(REG_VID_CHA_HORIZONTAL_BACK_PORCH,
  209. REG_VID_CHA_HORIZONTAL_BACK_PORCH),
  210. regmap_reg_range(REG_VID_CHA_VERTICAL_BACK_PORCH,
  211. REG_VID_CHA_VERTICAL_BACK_PORCH),
  212. regmap_reg_range(REG_VID_CHA_HORIZONTAL_FRONT_PORCH,
  213. REG_VID_CHA_HORIZONTAL_FRONT_PORCH),
  214. regmap_reg_range(REG_VID_CHA_VERTICAL_FRONT_PORCH,
  215. REG_VID_CHA_VERTICAL_FRONT_PORCH),
  216. regmap_reg_range(REG_VID_CHA_TEST_PATTERN, REG_VID_CHA_TEST_PATTERN),
  217. regmap_reg_range(REG_IRQ_GLOBAL, REG_IRQ_EN),
  218. regmap_reg_range(REG_IRQ_STAT, REG_IRQ_STAT),
  219. };
  220. static const struct regmap_access_table sn65dsi83_writeable_table = {
  221. .yes_ranges = sn65dsi83_writeable_ranges,
  222. .n_yes_ranges = ARRAY_SIZE(sn65dsi83_writeable_ranges),
  223. };
  224. static const struct regmap_range sn65dsi83_volatile_ranges[] = {
  225. regmap_reg_range(REG_RC_RESET, REG_RC_RESET),
  226. regmap_reg_range(REG_RC_LVDS_PLL, REG_RC_LVDS_PLL),
  227. regmap_reg_range(REG_IRQ_STAT, REG_IRQ_STAT),
  228. };
  229. static const struct regmap_access_table sn65dsi83_volatile_table = {
  230. .yes_ranges = sn65dsi83_volatile_ranges,
  231. .n_yes_ranges = ARRAY_SIZE(sn65dsi83_volatile_ranges),
  232. };
  233. static const struct regmap_config sn65dsi83_regmap_config = {
  234. .reg_bits = 8,
  235. .val_bits = 8,
  236. .rd_table = &sn65dsi83_readable_table,
  237. .wr_table = &sn65dsi83_writeable_table,
  238. .volatile_table = &sn65dsi83_volatile_table,
  239. .cache_type = REGCACHE_MAPLE,
  240. .max_register = REG_IRQ_STAT,
  241. };
  242. static const int lvds_vod_swing_data_table[2][4][2] = {
  243. { /* 100 Ohm */
  244. { 180000, 313000 },
  245. { 215000, 372000 },
  246. { 250000, 430000 },
  247. { 290000, 488000 },
  248. },
  249. { /* 200 Ohm */
  250. { 150000, 261000 },
  251. { 200000, 346000 },
  252. { 250000, 428000 },
  253. { 300000, 511000 },
  254. },
  255. };
  256. static const int lvds_vod_swing_clock_table[2][4][2] = {
  257. { /* 100 Ohm */
  258. { 140000, 244000 },
  259. { 168000, 290000 },
  260. { 195000, 335000 },
  261. { 226000, 381000 },
  262. },
  263. { /* 200 Ohm */
  264. { 117000, 204000 },
  265. { 156000, 270000 },
  266. { 195000, 334000 },
  267. { 234000, 399000 },
  268. },
  269. };
  270. static struct sn65dsi83 *bridge_to_sn65dsi83(struct drm_bridge *bridge)
  271. {
  272. return container_of(bridge, struct sn65dsi83, bridge);
  273. }
  274. static int sn65dsi83_attach(struct drm_bridge *bridge,
  275. struct drm_encoder *encoder,
  276. enum drm_bridge_attach_flags flags)
  277. {
  278. struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);
  279. return drm_bridge_attach(encoder, ctx->panel_bridge,
  280. &ctx->bridge, flags);
  281. }
  282. static void sn65dsi83_detach(struct drm_bridge *bridge)
  283. {
  284. struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);
  285. if (!ctx->dsi)
  286. return;
  287. ctx->dsi = NULL;
  288. }
  289. static u8 sn65dsi83_get_lvds_range(struct sn65dsi83 *ctx,
  290. const struct drm_display_mode *mode)
  291. {
  292. /*
  293. * The encoding of the LVDS_CLK_RANGE is as follows:
  294. * 000 - 25 MHz <= LVDS_CLK < 37.5 MHz
  295. * 001 - 37.5 MHz <= LVDS_CLK < 62.5 MHz
  296. * 010 - 62.5 MHz <= LVDS_CLK < 87.5 MHz
  297. * 011 - 87.5 MHz <= LVDS_CLK < 112.5 MHz
  298. * 100 - 112.5 MHz <= LVDS_CLK < 137.5 MHz
  299. * 101 - 137.5 MHz <= LVDS_CLK <= 154 MHz
  300. * which is a range of 12.5MHz..162.5MHz in 50MHz steps, except that
  301. * the ends of the ranges are clamped to the supported range. Since
  302. * sn65dsi83_mode_valid() already filters the valid modes and limits
  303. * the clock to 25..154 MHz, the range calculation can be simplified
  304. * as follows:
  305. */
  306. int mode_clock = mode->clock;
  307. if (ctx->lvds_dual_link)
  308. mode_clock /= 2;
  309. return (mode_clock - 12500) / 25000;
  310. }
  311. static u8 sn65dsi83_get_dsi_range(struct sn65dsi83 *ctx,
  312. const struct drm_display_mode *mode)
  313. {
  314. /*
  315. * The encoding of the CHA_DSI_CLK_RANGE is as follows:
  316. * 0x00 through 0x07 - Reserved
  317. * 0x08 - 40 <= DSI_CLK < 45 MHz
  318. * 0x09 - 45 <= DSI_CLK < 50 MHz
  319. * ...
  320. * 0x63 - 495 <= DSI_CLK < 500 MHz
  321. * 0x64 - 500 MHz
  322. * 0x65 through 0xFF - Reserved
  323. * which is DSI clock in 5 MHz steps, clamped to 40..500 MHz.
  324. * The DSI clock are calculated as:
  325. * DSI_CLK = mode clock * bpp / dsi_data_lanes / 2
  326. * the 2 is there because the bus is DDR.
  327. */
  328. return clamp((unsigned int)mode->clock *
  329. mipi_dsi_pixel_format_to_bpp(ctx->dsi->format) /
  330. ctx->dsi->lanes / 2, 40000U, 500000U) / 5000U;
  331. }
  332. static u8 sn65dsi83_get_dsi_div(struct sn65dsi83 *ctx)
  333. {
  334. /* The divider is (DSI_CLK / LVDS_CLK) - 1, which really is: */
  335. unsigned int dsi_div = mipi_dsi_pixel_format_to_bpp(ctx->dsi->format);
  336. dsi_div /= ctx->dsi->lanes;
  337. if (!ctx->lvds_dual_link)
  338. dsi_div /= 2;
  339. return dsi_div - 1;
  340. }
  341. static int sn65dsi83_reset_pipe(struct sn65dsi83 *sn65dsi83)
  342. {
  343. struct drm_modeset_acquire_ctx ctx;
  344. int err;
  345. /*
  346. * Reset active outputs of the related CRTC.
  347. *
  348. * This way, drm core will reconfigure each components in the CRTC
  349. * outputs path. In our case, this will force the previous component to
  350. * go back in LP11 mode and so allow the reconfiguration of SN65DSI83
  351. * bridge.
  352. *
  353. * Keep the lock during the whole operation to be atomic.
  354. */
  355. drm_modeset_acquire_init(&ctx, 0);
  356. dev_warn(sn65dsi83->dev, "reset the pipe\n");
  357. retry:
  358. err = drm_bridge_helper_reset_crtc(&sn65dsi83->bridge, &ctx);
  359. if (err == -EDEADLK) {
  360. drm_modeset_backoff(&ctx);
  361. goto retry;
  362. }
  363. drm_modeset_drop_locks(&ctx);
  364. drm_modeset_acquire_fini(&ctx);
  365. return 0;
  366. }
  367. static void sn65dsi83_reset_work(struct work_struct *ws)
  368. {
  369. struct sn65dsi83 *ctx = container_of(ws, struct sn65dsi83, reset_work);
  370. int ret;
  371. int idx;
  372. if (!drm_bridge_enter(&ctx->bridge, &idx))
  373. return;
  374. /* Reset the pipe */
  375. ret = sn65dsi83_reset_pipe(ctx);
  376. if (ret) {
  377. dev_err(ctx->dev, "reset pipe failed %pe\n", ERR_PTR(ret));
  378. return;
  379. }
  380. if (ctx->irq)
  381. enable_irq(ctx->irq);
  382. drm_bridge_exit(idx);
  383. }
  384. static void sn65dsi83_handle_errors(struct sn65dsi83 *ctx)
  385. {
  386. unsigned int irq_stat;
  387. int ret;
  388. int idx;
  389. if (!drm_bridge_enter(&ctx->bridge, &idx))
  390. return;
  391. /*
  392. * Schedule a reset in case of:
  393. * - the bridge doesn't answer
  394. * - the bridge signals an error
  395. */
  396. ret = regmap_read(ctx->regmap, REG_IRQ_STAT, &irq_stat);
  397. /*
  398. * Some hardware (Toradex Verdin AM62) is known to report the
  399. * PLL_UNLOCK error interrupt while working without visible
  400. * problems. In lack of a reliable way to discriminate such cases
  401. * from user-visible PLL_UNLOCK cases, ignore that bit entirely.
  402. */
  403. if (ret || irq_stat & ~REG_IRQ_STAT_CHA_PLL_UNLOCK) {
  404. /*
  405. * IRQ acknowledged is not always possible (the bridge can be in
  406. * a state where it doesn't answer anymore). To prevent an
  407. * interrupt storm, disable interrupt. The interrupt will be
  408. * after the reset.
  409. */
  410. if (ctx->irq)
  411. disable_irq_nosync(ctx->irq);
  412. schedule_work(&ctx->reset_work);
  413. }
  414. drm_bridge_exit(idx);
  415. }
  416. static void sn65dsi83_monitor_work(struct work_struct *work)
  417. {
  418. struct sn65dsi83 *ctx = container_of(to_delayed_work(work),
  419. struct sn65dsi83, monitor_work);
  420. sn65dsi83_handle_errors(ctx);
  421. schedule_delayed_work(&ctx->monitor_work, msecs_to_jiffies(1000));
  422. }
  423. static void sn65dsi83_monitor_start(struct sn65dsi83 *ctx)
  424. {
  425. schedule_delayed_work(&ctx->monitor_work, msecs_to_jiffies(1000));
  426. }
  427. static void sn65dsi83_monitor_stop(struct sn65dsi83 *ctx)
  428. {
  429. cancel_delayed_work_sync(&ctx->monitor_work);
  430. }
  431. /*
  432. * Release resources taken by sn65dsi83_atomic_pre_enable().
  433. *
  434. * Invoked by sn65dsi83_atomic_disable() normally, or by devres after
  435. * sn65dsi83_remove() in case this happens befora atomic_disable.
  436. */
  437. static void sn65dsi83_release_resources(void *data)
  438. {
  439. struct sn65dsi83 *ctx = (struct sn65dsi83 *)data;
  440. int ret;
  441. if (ctx->irq) {
  442. /* Disable irq */
  443. regmap_write(ctx->regmap, REG_IRQ_EN, 0x0);
  444. regmap_write(ctx->regmap, REG_IRQ_GLOBAL, 0x0);
  445. } else {
  446. /* Stop the polling task */
  447. sn65dsi83_monitor_stop(ctx);
  448. }
  449. /* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */
  450. gpiod_set_value_cansleep(ctx->enable_gpio, 0);
  451. usleep_range(10000, 11000);
  452. ret = regulator_disable(ctx->vcc);
  453. if (ret)
  454. dev_err(ctx->dev, "Failed to disable vcc: %d\n", ret);
  455. regcache_mark_dirty(ctx->regmap);
  456. }
  457. static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge,
  458. struct drm_atomic_state *state)
  459. {
  460. struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);
  461. const unsigned int dual_factor = ctx->lvds_dual_link ? 2 : 1;
  462. const struct drm_bridge_state *bridge_state;
  463. const struct drm_crtc_state *crtc_state;
  464. const struct drm_display_mode *mode;
  465. struct drm_connector *connector;
  466. struct drm_crtc *crtc;
  467. bool lvds_format_24bpp;
  468. bool lvds_format_jeida;
  469. unsigned int pval;
  470. __le16 le16val;
  471. u16 val;
  472. int ret;
  473. int idx;
  474. if (!drm_bridge_enter(bridge, &idx))
  475. return;
  476. ret = regulator_enable(ctx->vcc);
  477. if (ret) {
  478. dev_err(ctx->dev, "Failed to enable vcc: %d\n", ret);
  479. goto err_exit;
  480. }
  481. /* Deassert reset */
  482. gpiod_set_value_cansleep(ctx->enable_gpio, 1);
  483. usleep_range(10000, 11000);
  484. /* Get the LVDS format from the bridge state. */
  485. bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
  486. switch (bridge_state->output_bus_cfg.format) {
  487. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  488. lvds_format_24bpp = false;
  489. lvds_format_jeida = true;
  490. break;
  491. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  492. lvds_format_24bpp = true;
  493. lvds_format_jeida = true;
  494. break;
  495. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  496. lvds_format_24bpp = true;
  497. lvds_format_jeida = false;
  498. break;
  499. default:
  500. /*
  501. * Some bridges still don't set the correct
  502. * LVDS bus pixel format, use SPWG24 default
  503. * format until those are fixed.
  504. */
  505. lvds_format_24bpp = true;
  506. lvds_format_jeida = false;
  507. dev_warn(ctx->dev,
  508. "Unsupported LVDS bus format 0x%04x, please check output bridge driver. Falling back to SPWG24.\n",
  509. bridge_state->output_bus_cfg.format);
  510. break;
  511. }
  512. /*
  513. * Retrieve the CRTC adjusted mode. This requires a little dance to go
  514. * from the bridge to the encoder, to the connector and to the CRTC.
  515. */
  516. connector = drm_atomic_get_new_connector_for_encoder(state,
  517. bridge->encoder);
  518. crtc = drm_atomic_get_new_connector_state(state, connector)->crtc;
  519. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  520. mode = &crtc_state->adjusted_mode;
  521. /* Clear reset, disable PLL */
  522. regmap_write(ctx->regmap, REG_RC_RESET, 0x00);
  523. regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00);
  524. /* Reference clock derived from DSI link clock. */
  525. regmap_write(ctx->regmap, REG_RC_LVDS_PLL,
  526. REG_RC_LVDS_PLL_LVDS_CLK_RANGE(sn65dsi83_get_lvds_range(ctx, mode)) |
  527. REG_RC_LVDS_PLL_HS_CLK_SRC_DPHY);
  528. regmap_write(ctx->regmap, REG_DSI_CLK,
  529. REG_DSI_CLK_CHA_DSI_CLK_RANGE(sn65dsi83_get_dsi_range(ctx, mode)));
  530. regmap_write(ctx->regmap, REG_RC_DSI_CLK,
  531. REG_RC_DSI_CLK_DSI_CLK_DIVIDER(sn65dsi83_get_dsi_div(ctx)));
  532. /* Set number of DSI lanes and LVDS link config. */
  533. regmap_write(ctx->regmap, REG_DSI_LANE,
  534. REG_DSI_LANE_DSI_CHANNEL_MODE_SINGLE |
  535. REG_DSI_LANE_CHA_DSI_LANES(~(ctx->dsi->lanes - 1)) |
  536. /* CHB is DSI85-only, set to default on DSI83/DSI84 */
  537. REG_DSI_LANE_CHB_DSI_LANES(3));
  538. /* No equalization. */
  539. regmap_write(ctx->regmap, REG_DSI_EQ, 0x00);
  540. /* Set up sync signal polarity. */
  541. val = (mode->flags & DRM_MODE_FLAG_NHSYNC ?
  542. REG_LVDS_FMT_HS_NEG_POLARITY : 0) |
  543. (mode->flags & DRM_MODE_FLAG_NVSYNC ?
  544. REG_LVDS_FMT_VS_NEG_POLARITY : 0);
  545. val |= bridge_state->output_bus_cfg.flags & DRM_BUS_FLAG_DE_LOW ?
  546. REG_LVDS_FMT_DE_NEG_POLARITY : 0;
  547. /* Set up bits-per-pixel, 18bpp or 24bpp. */
  548. if (lvds_format_24bpp) {
  549. val |= REG_LVDS_FMT_CHA_24BPP_MODE;
  550. if (ctx->lvds_dual_link)
  551. val |= REG_LVDS_FMT_CHB_24BPP_MODE;
  552. }
  553. /* Set up LVDS format, JEIDA/Format 1 or SPWG/Format 2 */
  554. if (lvds_format_jeida) {
  555. val |= REG_LVDS_FMT_CHA_24BPP_FORMAT1;
  556. if (ctx->lvds_dual_link)
  557. val |= REG_LVDS_FMT_CHB_24BPP_FORMAT1;
  558. }
  559. /* Set up LVDS output config (DSI84,DSI85) */
  560. if (!ctx->lvds_dual_link)
  561. val |= REG_LVDS_FMT_LVDS_LINK_CFG;
  562. regmap_write(ctx->regmap, REG_LVDS_FMT, val);
  563. regmap_write(ctx->regmap, REG_LVDS_VCOM,
  564. REG_LVDS_VCOM_CHA_LVDS_VOD_SWING(ctx->lvds_vod_swing_conf[CHANNEL_A]) |
  565. REG_LVDS_VCOM_CHB_LVDS_VOD_SWING(ctx->lvds_vod_swing_conf[CHANNEL_B]));
  566. regmap_write(ctx->regmap, REG_LVDS_LANE,
  567. (ctx->lvds_dual_link_even_odd_swap ?
  568. REG_LVDS_LANE_EVEN_ODD_SWAP : 0) |
  569. (ctx->lvds_term_conf[CHANNEL_A] ?
  570. REG_LVDS_LANE_CHA_LVDS_TERM : 0) |
  571. (ctx->lvds_term_conf[CHANNEL_B] ?
  572. REG_LVDS_LANE_CHB_LVDS_TERM : 0));
  573. regmap_write(ctx->regmap, REG_LVDS_CM, 0x00);
  574. le16val = cpu_to_le16(mode->hdisplay);
  575. regmap_bulk_write(ctx->regmap, REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW,
  576. &le16val, 2);
  577. le16val = cpu_to_le16(mode->vdisplay);
  578. regmap_bulk_write(ctx->regmap, REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW,
  579. &le16val, 2);
  580. /* 32 + 1 pixel clock to ensure proper operation */
  581. le16val = cpu_to_le16(32 + 1);
  582. regmap_bulk_write(ctx->regmap, REG_VID_CHA_SYNC_DELAY_LOW, &le16val, 2);
  583. le16val = cpu_to_le16((mode->hsync_end - mode->hsync_start) / dual_factor);
  584. regmap_bulk_write(ctx->regmap, REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW,
  585. &le16val, 2);
  586. le16val = cpu_to_le16(mode->vsync_end - mode->vsync_start);
  587. regmap_bulk_write(ctx->regmap, REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW,
  588. &le16val, 2);
  589. regmap_write(ctx->regmap, REG_VID_CHA_HORIZONTAL_BACK_PORCH,
  590. (mode->htotal - mode->hsync_end) / dual_factor);
  591. regmap_write(ctx->regmap, REG_VID_CHA_VERTICAL_BACK_PORCH,
  592. mode->vtotal - mode->vsync_end);
  593. regmap_write(ctx->regmap, REG_VID_CHA_HORIZONTAL_FRONT_PORCH,
  594. (mode->hsync_start - mode->hdisplay) / dual_factor);
  595. regmap_write(ctx->regmap, REG_VID_CHA_VERTICAL_FRONT_PORCH,
  596. mode->vsync_start - mode->vdisplay);
  597. regmap_write(ctx->regmap, REG_VID_CHA_TEST_PATTERN, 0x00);
  598. /* Enable PLL */
  599. regmap_write(ctx->regmap, REG_RC_PLL_EN, REG_RC_PLL_EN_PLL_EN);
  600. usleep_range(3000, 4000);
  601. ret = regmap_read_poll_timeout(ctx->regmap, REG_RC_LVDS_PLL, pval,
  602. pval & REG_RC_LVDS_PLL_PLL_EN_STAT,
  603. 1000, 100000);
  604. if (ret) {
  605. dev_err(ctx->dev, "failed to lock PLL, ret=%i\n", ret);
  606. /* On failure, disable PLL again and exit. */
  607. regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00);
  608. goto err_add_action;
  609. }
  610. /* Trigger reset after CSR register update. */
  611. regmap_write(ctx->regmap, REG_RC_RESET, REG_RC_RESET_SOFT_RESET);
  612. /* Wait for 10ms after soft reset as specified in datasheet */
  613. usleep_range(10000, 12000);
  614. err_add_action:
  615. devm_add_action(ctx->dev, sn65dsi83_release_resources, ctx);
  616. err_exit:
  617. drm_bridge_exit(idx);
  618. }
  619. static void sn65dsi83_atomic_enable(struct drm_bridge *bridge,
  620. struct drm_atomic_state *state)
  621. {
  622. struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);
  623. unsigned int pval;
  624. int idx;
  625. if (!drm_bridge_enter(bridge, &idx))
  626. return;
  627. /* Clear all errors that got asserted during initialization. */
  628. regmap_read(ctx->regmap, REG_IRQ_STAT, &pval);
  629. regmap_write(ctx->regmap, REG_IRQ_STAT, pval);
  630. /* Wait for 1ms and check for errors in status register */
  631. usleep_range(1000, 1100);
  632. regmap_read(ctx->regmap, REG_IRQ_STAT, &pval);
  633. if (pval)
  634. dev_err(ctx->dev, "Unexpected link status 0x%02x\n", pval);
  635. if (ctx->irq) {
  636. /* Enable irq to detect errors */
  637. regmap_write(ctx->regmap, REG_IRQ_GLOBAL, REG_IRQ_GLOBAL_IRQ_EN);
  638. regmap_write(ctx->regmap, REG_IRQ_EN, 0xff & ~REG_IRQ_EN_CHA_PLL_UNLOCK_EN);
  639. } else {
  640. /* Use the polling task */
  641. sn65dsi83_monitor_start(ctx);
  642. }
  643. drm_bridge_exit(idx);
  644. }
  645. static void sn65dsi83_atomic_disable(struct drm_bridge *bridge,
  646. struct drm_atomic_state *state)
  647. {
  648. struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);
  649. int idx;
  650. if (!drm_bridge_enter(bridge, &idx))
  651. return;
  652. devm_release_action(ctx->dev, sn65dsi83_release_resources, ctx);
  653. drm_bridge_exit(idx);
  654. }
  655. static enum drm_mode_status
  656. sn65dsi83_mode_valid(struct drm_bridge *bridge,
  657. const struct drm_display_info *info,
  658. const struct drm_display_mode *mode)
  659. {
  660. /* LVDS output clock range 25..154 MHz */
  661. if (mode->clock < 25000)
  662. return MODE_CLOCK_LOW;
  663. if (mode->clock > 154000)
  664. return MODE_CLOCK_HIGH;
  665. return MODE_OK;
  666. }
  667. #define MAX_INPUT_SEL_FORMATS 1
  668. static u32 *
  669. sn65dsi83_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
  670. struct drm_bridge_state *bridge_state,
  671. struct drm_crtc_state *crtc_state,
  672. struct drm_connector_state *conn_state,
  673. u32 output_fmt,
  674. unsigned int *num_input_fmts)
  675. {
  676. u32 *input_fmts;
  677. *num_input_fmts = 0;
  678. input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
  679. GFP_KERNEL);
  680. if (!input_fmts)
  681. return NULL;
  682. /* This is the DSI-end bus format */
  683. input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
  684. *num_input_fmts = 1;
  685. return input_fmts;
  686. }
  687. static const struct drm_bridge_funcs sn65dsi83_funcs = {
  688. .attach = sn65dsi83_attach,
  689. .detach = sn65dsi83_detach,
  690. .atomic_enable = sn65dsi83_atomic_enable,
  691. .atomic_pre_enable = sn65dsi83_atomic_pre_enable,
  692. .atomic_disable = sn65dsi83_atomic_disable,
  693. .mode_valid = sn65dsi83_mode_valid,
  694. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  695. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  696. .atomic_reset = drm_atomic_helper_bridge_reset,
  697. .atomic_get_input_bus_fmts = sn65dsi83_atomic_get_input_bus_fmts,
  698. };
  699. static int sn65dsi83_select_lvds_vod_swing(struct device *dev,
  700. u32 lvds_vod_swing_data[2], u32 lvds_vod_swing_clk[2], u8 lvds_term)
  701. {
  702. int i;
  703. for (i = 0; i <= 3; i++) {
  704. if (lvds_vod_swing_data_table[lvds_term][i][0] >= lvds_vod_swing_data[0] &&
  705. lvds_vod_swing_data_table[lvds_term][i][1] <= lvds_vod_swing_data[1] &&
  706. lvds_vod_swing_clock_table[lvds_term][i][0] >= lvds_vod_swing_clk[0] &&
  707. lvds_vod_swing_clock_table[lvds_term][i][1] <= lvds_vod_swing_clk[1])
  708. return i;
  709. }
  710. dev_err(dev, "failed to find appropriate LVDS_VOD_SWING configuration\n");
  711. return -EINVAL;
  712. }
  713. static int sn65dsi83_parse_lvds_endpoint(struct sn65dsi83 *ctx, int channel)
  714. {
  715. struct device *dev = ctx->dev;
  716. struct device_node *endpoint;
  717. int endpoint_reg;
  718. /* Set so the property can be freely selected if not defined */
  719. u32 lvds_vod_swing_data[2] = { 0, 1000000 };
  720. u32 lvds_vod_swing_clk[2] = { 0, 1000000 };
  721. /* Set default near end terminataion to 200 Ohm */
  722. u32 lvds_term = 200;
  723. int lvds_vod_swing_conf;
  724. int ret = 0;
  725. int ret_data;
  726. int ret_clock;
  727. if (channel == CHANNEL_A)
  728. endpoint_reg = 2;
  729. else
  730. endpoint_reg = 3;
  731. endpoint = of_graph_get_endpoint_by_regs(dev->of_node, endpoint_reg, -1);
  732. of_property_read_u32(endpoint, "ti,lvds-termination-ohms", &lvds_term);
  733. if (lvds_term == 100)
  734. ctx->lvds_term_conf[channel] = OHM_100;
  735. else if (lvds_term == 200)
  736. ctx->lvds_term_conf[channel] = OHM_200;
  737. else {
  738. ret = -EINVAL;
  739. goto exit;
  740. }
  741. ret_data = of_property_read_u32_array(endpoint, "ti,lvds-vod-swing-data-microvolt",
  742. lvds_vod_swing_data, ARRAY_SIZE(lvds_vod_swing_data));
  743. if (ret_data != 0 && ret_data != -EINVAL) {
  744. ret = ret_data;
  745. goto exit;
  746. }
  747. ret_clock = of_property_read_u32_array(endpoint, "ti,lvds-vod-swing-clock-microvolt",
  748. lvds_vod_swing_clk, ARRAY_SIZE(lvds_vod_swing_clk));
  749. if (ret_clock != 0 && ret_clock != -EINVAL) {
  750. ret = ret_clock;
  751. goto exit;
  752. }
  753. /* Use default value if both properties are NOT defined. */
  754. if (ret_data == -EINVAL && ret_clock == -EINVAL)
  755. lvds_vod_swing_conf = 0x1;
  756. /* Use lookup table if any of the two properties is defined. */
  757. if (!ret_data || !ret_clock) {
  758. lvds_vod_swing_conf = sn65dsi83_select_lvds_vod_swing(dev, lvds_vod_swing_data,
  759. lvds_vod_swing_clk, ctx->lvds_term_conf[channel]);
  760. if (lvds_vod_swing_conf < 0) {
  761. ret = lvds_vod_swing_conf;
  762. goto exit;
  763. }
  764. }
  765. ctx->lvds_vod_swing_conf[channel] = lvds_vod_swing_conf;
  766. ret = 0;
  767. exit:
  768. of_node_put(endpoint);
  769. return ret;
  770. }
  771. static int sn65dsi83_parse_dt(struct sn65dsi83 *ctx, enum sn65dsi83_model model)
  772. {
  773. struct drm_bridge *panel_bridge;
  774. struct device *dev = ctx->dev;
  775. int ret;
  776. ret = sn65dsi83_parse_lvds_endpoint(ctx, CHANNEL_A);
  777. if (ret < 0)
  778. return ret;
  779. ret = sn65dsi83_parse_lvds_endpoint(ctx, CHANNEL_B);
  780. if (ret < 0)
  781. return ret;
  782. ctx->lvds_dual_link = false;
  783. ctx->lvds_dual_link_even_odd_swap = false;
  784. if (model != MODEL_SN65DSI83) {
  785. struct device_node *port2, *port3;
  786. int dual_link;
  787. port2 = of_graph_get_port_by_id(dev->of_node, 2);
  788. port3 = of_graph_get_port_by_id(dev->of_node, 3);
  789. dual_link = drm_of_lvds_get_dual_link_pixel_order(port2, port3);
  790. of_node_put(port2);
  791. of_node_put(port3);
  792. if (dual_link == DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS) {
  793. ctx->lvds_dual_link = true;
  794. /* Odd pixels to LVDS Channel A, even pixels to B */
  795. ctx->lvds_dual_link_even_odd_swap = false;
  796. } else if (dual_link == DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS) {
  797. ctx->lvds_dual_link = true;
  798. /* Even pixels to LVDS Channel A, odd pixels to B */
  799. ctx->lvds_dual_link_even_odd_swap = true;
  800. }
  801. }
  802. panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 2, 0);
  803. if (IS_ERR(panel_bridge))
  804. return dev_err_probe(dev, PTR_ERR(panel_bridge), "Failed to get panel bridge\n");
  805. ctx->panel_bridge = panel_bridge;
  806. ctx->vcc = devm_regulator_get(dev, "vcc");
  807. if (IS_ERR(ctx->vcc))
  808. return dev_err_probe(dev, PTR_ERR(ctx->vcc),
  809. "Failed to get supply 'vcc'\n");
  810. return 0;
  811. }
  812. static int sn65dsi83_host_attach(struct sn65dsi83 *ctx)
  813. {
  814. struct device *dev = ctx->dev;
  815. struct device_node *host_node;
  816. struct device_node *endpoint;
  817. struct mipi_dsi_device *dsi;
  818. struct mipi_dsi_host *host;
  819. const struct mipi_dsi_device_info info = {
  820. .type = "sn65dsi83",
  821. .channel = 0,
  822. .node = NULL,
  823. };
  824. int dsi_lanes, ret;
  825. endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
  826. dsi_lanes = drm_of_get_data_lanes_count(endpoint, 1, 4);
  827. host_node = of_graph_get_remote_port_parent(endpoint);
  828. host = of_find_mipi_dsi_host_by_node(host_node);
  829. of_node_put(host_node);
  830. of_node_put(endpoint);
  831. if (!host)
  832. return -EPROBE_DEFER;
  833. if (dsi_lanes < 0)
  834. return dsi_lanes;
  835. dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
  836. if (IS_ERR(dsi))
  837. return dev_err_probe(dev, PTR_ERR(dsi),
  838. "failed to create dsi device\n");
  839. ctx->dsi = dsi;
  840. dsi->lanes = dsi_lanes;
  841. dsi->format = MIPI_DSI_FMT_RGB888;
  842. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
  843. MIPI_DSI_MODE_VIDEO_NO_HFP | MIPI_DSI_MODE_VIDEO_NO_HBP |
  844. MIPI_DSI_MODE_VIDEO_NO_HSA | MIPI_DSI_MODE_NO_EOT_PACKET;
  845. ret = devm_mipi_dsi_attach(dev, dsi);
  846. if (ret < 0) {
  847. dev_err(dev, "failed to attach dsi to host: %d\n", ret);
  848. return ret;
  849. }
  850. return 0;
  851. }
  852. static irqreturn_t sn65dsi83_irq(int irq, void *data)
  853. {
  854. struct sn65dsi83 *ctx = data;
  855. sn65dsi83_handle_errors(ctx);
  856. return IRQ_HANDLED;
  857. }
  858. static int sn65dsi83_probe(struct i2c_client *client)
  859. {
  860. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  861. struct device *dev = &client->dev;
  862. enum sn65dsi83_model model;
  863. struct sn65dsi83 *ctx;
  864. int ret;
  865. ctx = devm_drm_bridge_alloc(dev, struct sn65dsi83, bridge, &sn65dsi83_funcs);
  866. if (IS_ERR(ctx))
  867. return PTR_ERR(ctx);
  868. ctx->dev = dev;
  869. INIT_WORK(&ctx->reset_work, sn65dsi83_reset_work);
  870. INIT_DELAYED_WORK(&ctx->monitor_work, sn65dsi83_monitor_work);
  871. if (dev->of_node) {
  872. model = (enum sn65dsi83_model)(uintptr_t)
  873. of_device_get_match_data(dev);
  874. } else {
  875. model = id->driver_data;
  876. }
  877. /* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */
  878. ctx->enable_gpio = devm_gpiod_get_optional(ctx->dev, "enable",
  879. GPIOD_OUT_LOW);
  880. if (IS_ERR(ctx->enable_gpio))
  881. return dev_err_probe(dev, PTR_ERR(ctx->enable_gpio), "failed to get enable GPIO\n");
  882. usleep_range(10000, 11000);
  883. ret = sn65dsi83_parse_dt(ctx, model);
  884. if (ret)
  885. return ret;
  886. ctx->regmap = devm_regmap_init_i2c(client, &sn65dsi83_regmap_config);
  887. if (IS_ERR(ctx->regmap))
  888. return dev_err_probe(dev, PTR_ERR(ctx->regmap), "failed to get regmap\n");
  889. if (client->irq) {
  890. ctx->irq = client->irq;
  891. ret = devm_request_threaded_irq(ctx->dev, ctx->irq, NULL, sn65dsi83_irq,
  892. IRQF_ONESHOT, dev_name(ctx->dev), ctx);
  893. if (ret)
  894. return dev_err_probe(dev, ret, "failed to request irq\n");
  895. }
  896. dev_set_drvdata(dev, ctx);
  897. i2c_set_clientdata(client, ctx);
  898. ctx->bridge.of_node = dev->of_node;
  899. ctx->bridge.pre_enable_prev_first = true;
  900. ctx->bridge.type = DRM_MODE_CONNECTOR_LVDS;
  901. drm_bridge_add(&ctx->bridge);
  902. ret = sn65dsi83_host_attach(ctx);
  903. if (ret) {
  904. dev_err_probe(dev, ret, "failed to attach DSI host\n");
  905. goto err_remove_bridge;
  906. }
  907. return 0;
  908. err_remove_bridge:
  909. drm_bridge_remove(&ctx->bridge);
  910. return ret;
  911. }
  912. static void sn65dsi83_remove(struct i2c_client *client)
  913. {
  914. struct sn65dsi83 *ctx = i2c_get_clientdata(client);
  915. drm_bridge_unplug(&ctx->bridge);
  916. }
  917. static const struct i2c_device_id sn65dsi83_id[] = {
  918. { "ti,sn65dsi83", MODEL_SN65DSI83 },
  919. { "ti,sn65dsi84", MODEL_SN65DSI84 },
  920. {},
  921. };
  922. MODULE_DEVICE_TABLE(i2c, sn65dsi83_id);
  923. static const struct of_device_id sn65dsi83_match_table[] = {
  924. { .compatible = "ti,sn65dsi83", .data = (void *)MODEL_SN65DSI83 },
  925. { .compatible = "ti,sn65dsi84", .data = (void *)MODEL_SN65DSI84 },
  926. {},
  927. };
  928. MODULE_DEVICE_TABLE(of, sn65dsi83_match_table);
  929. static struct i2c_driver sn65dsi83_driver = {
  930. .probe = sn65dsi83_probe,
  931. .remove = sn65dsi83_remove,
  932. .id_table = sn65dsi83_id,
  933. .driver = {
  934. .name = "sn65dsi83",
  935. .of_match_table = sn65dsi83_match_table,
  936. },
  937. };
  938. module_i2c_driver(sn65dsi83_driver);
  939. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  940. MODULE_DESCRIPTION("TI SN65DSI83 DSI to LVDS bridge driver");
  941. MODULE_LICENSE("GPL v2");