tc358768.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com
  4. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/i2c.h>
  10. #include <linux/kernel.h>
  11. #include <linux/math64.h>
  12. #include <linux/media-bus-format.h>
  13. #include <linux/minmax.h>
  14. #include <linux/module.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/units.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_drv.h>
  21. #include <drm/drm_mipi_dsi.h>
  22. #include <drm/drm_of.h>
  23. #include <drm/drm_panel.h>
  24. #include <video/mipi_display.h>
  25. #include <video/videomode.h>
  26. /* Global (16-bit addressable) */
  27. #define TC358768_CHIPID 0x0000
  28. #define TC358768_SYSCTL 0x0002
  29. #define TC358768_CONFCTL 0x0004
  30. #define TC358768_VSDLY 0x0006
  31. #define TC358768_DATAFMT 0x0008
  32. #define TC358768_GPIOEN 0x000E
  33. #define TC358768_GPIODIR 0x0010
  34. #define TC358768_GPIOIN 0x0012
  35. #define TC358768_GPIOOUT 0x0014
  36. #define TC358768_PLLCTL0 0x0016
  37. #define TC358768_PLLCTL1 0x0018
  38. #define TC358768_CMDBYTE 0x0022
  39. #define TC358768_PP_MISC 0x0032
  40. #define TC358768_DSITX_DT 0x0050
  41. #define TC358768_FIFOSTATUS 0x00F8
  42. /* Debug (16-bit addressable) */
  43. #define TC358768_VBUFCTRL 0x00E0
  44. #define TC358768_DBG_WIDTH 0x00E2
  45. #define TC358768_DBG_VBLANK 0x00E4
  46. #define TC358768_DBG_DATA 0x00E8
  47. /* TX PHY (32-bit addressable) */
  48. #define TC358768_CLW_DPHYCONTTX 0x0100
  49. #define TC358768_D0W_DPHYCONTTX 0x0104
  50. #define TC358768_D1W_DPHYCONTTX 0x0108
  51. #define TC358768_D2W_DPHYCONTTX 0x010C
  52. #define TC358768_D3W_DPHYCONTTX 0x0110
  53. #define TC358768_CLW_CNTRL 0x0140
  54. #define TC358768_D0W_CNTRL 0x0144
  55. #define TC358768_D1W_CNTRL 0x0148
  56. #define TC358768_D2W_CNTRL 0x014C
  57. #define TC358768_D3W_CNTRL 0x0150
  58. /* TX PPI (32-bit addressable) */
  59. #define TC358768_STARTCNTRL 0x0204
  60. #define TC358768_DSITXSTATUS 0x0208
  61. #define TC358768_LINEINITCNT 0x0210
  62. #define TC358768_LPTXTIMECNT 0x0214
  63. #define TC358768_TCLK_HEADERCNT 0x0218
  64. #define TC358768_TCLK_TRAILCNT 0x021C
  65. #define TC358768_THS_HEADERCNT 0x0220
  66. #define TC358768_TWAKEUP 0x0224
  67. #define TC358768_TCLK_POSTCNT 0x0228
  68. #define TC358768_THS_TRAILCNT 0x022C
  69. #define TC358768_HSTXVREGCNT 0x0230
  70. #define TC358768_HSTXVREGEN 0x0234
  71. #define TC358768_TXOPTIONCNTRL 0x0238
  72. #define TC358768_BTACNTRL1 0x023C
  73. /* TX CTRL (32-bit addressable) */
  74. #define TC358768_DSI_CONTROL 0x040C
  75. #define TC358768_DSI_STATUS 0x0410
  76. #define TC358768_DSI_INT 0x0414
  77. #define TC358768_DSI_INT_ENA 0x0418
  78. #define TC358768_DSICMD_RDFIFO 0x0430
  79. #define TC358768_DSI_ACKERR 0x0434
  80. #define TC358768_DSI_ACKERR_INTENA 0x0438
  81. #define TC358768_DSI_ACKERR_HALT 0x043c
  82. #define TC358768_DSI_RXERR 0x0440
  83. #define TC358768_DSI_RXERR_INTENA 0x0444
  84. #define TC358768_DSI_RXERR_HALT 0x0448
  85. #define TC358768_DSI_ERR 0x044C
  86. #define TC358768_DSI_ERR_INTENA 0x0450
  87. #define TC358768_DSI_ERR_HALT 0x0454
  88. #define TC358768_DSI_CONFW 0x0500
  89. #define TC358768_DSI_LPCMD 0x0500
  90. #define TC358768_DSI_RESET 0x0504
  91. #define TC358768_DSI_INT_CLR 0x050C
  92. #define TC358768_DSI_START 0x0518
  93. /* DSITX CTRL (16-bit addressable) */
  94. #define TC358768_DSICMD_TX 0x0600
  95. #define TC358768_DSICMD_TYPE 0x0602
  96. #define TC358768_DSICMD_WC 0x0604
  97. #define TC358768_DSICMD_WD0 0x0610
  98. #define TC358768_DSICMD_WD1 0x0612
  99. #define TC358768_DSICMD_WD2 0x0614
  100. #define TC358768_DSICMD_WD3 0x0616
  101. #define TC358768_DSI_EVENT 0x0620
  102. #define TC358768_DSI_VSW 0x0622
  103. #define TC358768_DSI_VBPR 0x0624
  104. #define TC358768_DSI_VACT 0x0626
  105. #define TC358768_DSI_HSW 0x0628
  106. #define TC358768_DSI_HBPR 0x062A
  107. #define TC358768_DSI_HACT 0x062C
  108. /* TC358768_DSI_CONTROL (0x040C) register */
  109. #define TC358768_DSI_CONTROL_DIS_MODE BIT(15)
  110. #define TC358768_DSI_CONTROL_TXMD BIT(7)
  111. #define TC358768_DSI_CONTROL_HSCKMD BIT(5)
  112. #define TC358768_DSI_CONTROL_EOTDIS BIT(0)
  113. /* TC358768_DSI_CONFW (0x0500) register */
  114. #define TC358768_DSI_CONFW_MODE_SET (5 << 29)
  115. #define TC358768_DSI_CONFW_MODE_CLR (6 << 29)
  116. #define TC358768_DSI_CONFW_ADDR_DSI_CONTROL (0x3 << 24)
  117. /* TC358768_DSICMD_TX (0x0600) register */
  118. #define TC358768_DSI_CMDTX_DC_START BIT(0)
  119. static const char * const tc358768_supplies[] = {
  120. "vddc", "vddmipi", "vddio"
  121. };
  122. struct tc358768_dsi_output {
  123. struct mipi_dsi_device *dev;
  124. struct drm_panel *panel;
  125. struct drm_bridge *bridge;
  126. };
  127. struct tc358768_priv {
  128. struct device *dev;
  129. struct regmap *regmap;
  130. struct gpio_desc *reset_gpio;
  131. struct regulator_bulk_data supplies[ARRAY_SIZE(tc358768_supplies)];
  132. struct clk *refclk;
  133. int enabled;
  134. int error;
  135. struct mipi_dsi_host dsi_host;
  136. struct drm_bridge bridge;
  137. struct tc358768_dsi_output output;
  138. u32 pd_lines; /* number of Parallel Port Input Data Lines */
  139. u32 dsi_lanes; /* number of DSI Lanes */
  140. u32 dsi_bpp; /* number of Bits Per Pixel over DSI */
  141. /* Parameters for PLL programming */
  142. u32 fbd; /* PLL feedback divider */
  143. u32 prd; /* PLL input divider */
  144. u32 frs; /* PLL Freqency range for HSCK (post divider) */
  145. u32 dsiclk; /* pll_clk / 2 */
  146. u32 pclk; /* incoming pclk rate */
  147. };
  148. static inline struct tc358768_priv *dsi_host_to_tc358768(struct mipi_dsi_host
  149. *host)
  150. {
  151. return container_of(host, struct tc358768_priv, dsi_host);
  152. }
  153. static inline struct tc358768_priv *bridge_to_tc358768(struct drm_bridge
  154. *bridge)
  155. {
  156. return container_of(bridge, struct tc358768_priv, bridge);
  157. }
  158. static int tc358768_clear_error(struct tc358768_priv *priv)
  159. {
  160. int ret = priv->error;
  161. priv->error = 0;
  162. return ret;
  163. }
  164. static void tc358768_write(struct tc358768_priv *priv, u32 reg, u32 val)
  165. {
  166. /* work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
  167. int tmpval = val;
  168. size_t count = 2;
  169. if (priv->error)
  170. return;
  171. /* 16-bit register? */
  172. if (reg < 0x100 || reg >= 0x600)
  173. count = 1;
  174. priv->error = regmap_bulk_write(priv->regmap, reg, &tmpval, count);
  175. }
  176. static void tc358768_read(struct tc358768_priv *priv, u32 reg, u32 *val)
  177. {
  178. size_t count = 2;
  179. if (priv->error)
  180. return;
  181. /* 16-bit register? */
  182. if (reg < 0x100 || reg >= 0x600) {
  183. *val = 0;
  184. count = 1;
  185. }
  186. priv->error = regmap_bulk_read(priv->regmap, reg, val, count);
  187. }
  188. static void tc358768_update_bits(struct tc358768_priv *priv, u32 reg, u32 mask,
  189. u32 val)
  190. {
  191. u32 tmp, orig;
  192. tc358768_read(priv, reg, &orig);
  193. if (priv->error)
  194. return;
  195. tmp = orig & ~mask;
  196. tmp |= val & mask;
  197. if (tmp != orig)
  198. tc358768_write(priv, reg, tmp);
  199. }
  200. static void tc358768_dsicmd_tx(struct tc358768_priv *priv)
  201. {
  202. u32 val;
  203. /* start transfer */
  204. tc358768_write(priv, TC358768_DSICMD_TX, TC358768_DSI_CMDTX_DC_START);
  205. if (priv->error)
  206. return;
  207. /* wait transfer completion */
  208. priv->error = regmap_read_poll_timeout(priv->regmap, TC358768_DSICMD_TX, val,
  209. (val & TC358768_DSI_CMDTX_DC_START) == 0,
  210. 100, 100000);
  211. }
  212. static int tc358768_sw_reset(struct tc358768_priv *priv)
  213. {
  214. /* Assert Reset */
  215. tc358768_write(priv, TC358768_SYSCTL, 1);
  216. /* Release Reset, Exit Sleep */
  217. tc358768_write(priv, TC358768_SYSCTL, 0);
  218. return tc358768_clear_error(priv);
  219. }
  220. static void tc358768_hw_enable(struct tc358768_priv *priv)
  221. {
  222. int ret;
  223. if (priv->enabled)
  224. return;
  225. ret = clk_prepare_enable(priv->refclk);
  226. if (ret < 0)
  227. dev_err(priv->dev, "error enabling refclk (%d)\n", ret);
  228. ret = regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
  229. if (ret < 0)
  230. dev_err(priv->dev, "error enabling regulators (%d)\n", ret);
  231. if (priv->reset_gpio)
  232. usleep_range(200, 300);
  233. /*
  234. * The RESX is active low (GPIO_ACTIVE_LOW).
  235. * DEASSERT (value = 0) the reset_gpio to enable the chip
  236. */
  237. gpiod_set_value_cansleep(priv->reset_gpio, 0);
  238. /* wait for encoder clocks to stabilize */
  239. usleep_range(1000, 2000);
  240. priv->enabled = true;
  241. }
  242. static void tc358768_hw_disable(struct tc358768_priv *priv)
  243. {
  244. int ret;
  245. if (!priv->enabled)
  246. return;
  247. /*
  248. * The RESX is active low (GPIO_ACTIVE_LOW).
  249. * ASSERT (value = 1) the reset_gpio to disable the chip
  250. */
  251. gpiod_set_value_cansleep(priv->reset_gpio, 1);
  252. ret = regulator_bulk_disable(ARRAY_SIZE(priv->supplies),
  253. priv->supplies);
  254. if (ret < 0)
  255. dev_err(priv->dev, "error disabling regulators (%d)\n", ret);
  256. clk_disable_unprepare(priv->refclk);
  257. priv->enabled = false;
  258. }
  259. static u32 tc358768_pll_to_pclk(struct tc358768_priv *priv, u32 pll_clk)
  260. {
  261. return (u32)div_u64((u64)pll_clk * priv->dsi_lanes, priv->dsi_bpp);
  262. }
  263. static u32 tc358768_pclk_to_pll(struct tc358768_priv *priv, u32 pclk)
  264. {
  265. return (u32)div_u64((u64)pclk * priv->dsi_bpp, priv->dsi_lanes);
  266. }
  267. static int tc358768_calc_pll(struct tc358768_priv *priv,
  268. const struct drm_display_mode *mode,
  269. bool verify_only)
  270. {
  271. static const u32 frs_limits[] = {
  272. 1000000000,
  273. 500000000,
  274. 250000000,
  275. 125000000,
  276. 62500000
  277. };
  278. unsigned long refclk;
  279. u32 prd, target_pll, i, max_pll, min_pll;
  280. u32 frs, best_diff, best_pll, best_prd, best_fbd;
  281. target_pll = tc358768_pclk_to_pll(priv, mode->clock * 1000);
  282. /* pll_clk = RefClk * FBD / PRD * (1 / (2^FRS)) */
  283. for (i = 0; i < ARRAY_SIZE(frs_limits); i++)
  284. if (target_pll >= frs_limits[i])
  285. break;
  286. if (i == ARRAY_SIZE(frs_limits) || i == 0)
  287. return -EINVAL;
  288. frs = i - 1;
  289. max_pll = frs_limits[i - 1];
  290. min_pll = frs_limits[i];
  291. refclk = clk_get_rate(priv->refclk);
  292. best_diff = UINT_MAX;
  293. best_pll = 0;
  294. best_prd = 0;
  295. best_fbd = 0;
  296. for (prd = 1; prd <= 16; ++prd) {
  297. u32 divisor = prd * (1 << frs);
  298. u32 fbd;
  299. for (fbd = 1; fbd <= 512; ++fbd) {
  300. u32 pll, diff, pll_in;
  301. pll = (u32)div_u64((u64)refclk * fbd, divisor);
  302. if (pll >= max_pll || pll < min_pll)
  303. continue;
  304. pll_in = (u32)div_u64((u64)refclk, prd);
  305. if (pll_in < 4000000)
  306. continue;
  307. diff = max(pll, target_pll) - min(pll, target_pll);
  308. if (diff < best_diff) {
  309. best_diff = diff;
  310. best_pll = pll;
  311. best_prd = prd;
  312. best_fbd = fbd;
  313. if (best_diff == 0)
  314. goto found;
  315. }
  316. }
  317. }
  318. if (best_diff == UINT_MAX) {
  319. dev_err(priv->dev, "could not find suitable PLL setup\n");
  320. return -EINVAL;
  321. }
  322. found:
  323. if (verify_only)
  324. return 0;
  325. priv->fbd = best_fbd;
  326. priv->prd = best_prd;
  327. priv->frs = frs;
  328. priv->dsiclk = best_pll / 2;
  329. priv->pclk = mode->clock * 1000;
  330. return 0;
  331. }
  332. static int tc358768_dsi_host_attach(struct mipi_dsi_host *host,
  333. struct mipi_dsi_device *dev)
  334. {
  335. struct tc358768_priv *priv = dsi_host_to_tc358768(host);
  336. struct drm_bridge *bridge;
  337. struct drm_panel *panel;
  338. struct device_node *ep;
  339. int ret;
  340. if (dev->lanes > 4) {
  341. dev_err(priv->dev, "unsupported number of data lanes(%u)\n",
  342. dev->lanes);
  343. return -EINVAL;
  344. }
  345. /*
  346. * tc358768 supports both Video and Pulse mode, but the driver only
  347. * implements Video (event) mode currently
  348. */
  349. if (!(dev->mode_flags & MIPI_DSI_MODE_VIDEO)) {
  350. dev_err(priv->dev, "Only MIPI_DSI_MODE_VIDEO is supported\n");
  351. return -ENOTSUPP;
  352. }
  353. /*
  354. * tc358768 supports RGB888, RGB666, RGB666_PACKED and RGB565, but only
  355. * RGB888 is verified.
  356. */
  357. if (dev->format != MIPI_DSI_FMT_RGB888) {
  358. dev_warn(priv->dev, "Only MIPI_DSI_FMT_RGB888 tested!\n");
  359. return -ENOTSUPP;
  360. }
  361. ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0, &panel,
  362. &bridge);
  363. if (ret)
  364. return ret;
  365. if (panel) {
  366. bridge = drm_panel_bridge_add_typed(panel,
  367. DRM_MODE_CONNECTOR_DSI);
  368. if (IS_ERR(bridge))
  369. return PTR_ERR(bridge);
  370. }
  371. priv->output.dev = dev;
  372. priv->output.bridge = bridge;
  373. priv->output.panel = panel;
  374. priv->dsi_lanes = dev->lanes;
  375. priv->dsi_bpp = mipi_dsi_pixel_format_to_bpp(dev->format);
  376. /* get input ep (port0/endpoint0) */
  377. ret = -EINVAL;
  378. ep = of_graph_get_endpoint_by_regs(host->dev->of_node, 0, 0);
  379. if (ep) {
  380. ret = of_property_read_u32(ep, "data-lines", &priv->pd_lines);
  381. of_node_put(ep);
  382. }
  383. if (ret)
  384. priv->pd_lines = priv->dsi_bpp;
  385. drm_bridge_add(&priv->bridge);
  386. return 0;
  387. }
  388. static int tc358768_dsi_host_detach(struct mipi_dsi_host *host,
  389. struct mipi_dsi_device *dev)
  390. {
  391. struct tc358768_priv *priv = dsi_host_to_tc358768(host);
  392. drm_bridge_remove(&priv->bridge);
  393. if (priv->output.panel)
  394. drm_panel_bridge_remove(priv->output.bridge);
  395. return 0;
  396. }
  397. static ssize_t tc358768_dsi_host_transfer(struct mipi_dsi_host *host,
  398. const struct mipi_dsi_msg *msg)
  399. {
  400. struct tc358768_priv *priv = dsi_host_to_tc358768(host);
  401. struct mipi_dsi_packet packet;
  402. int ret;
  403. if (!priv->enabled) {
  404. dev_err(priv->dev, "Bridge is not enabled\n");
  405. return -ENODEV;
  406. }
  407. if (msg->rx_len) {
  408. dev_warn(priv->dev, "MIPI rx is not supported\n");
  409. return -ENOTSUPP;
  410. }
  411. if (msg->tx_len > 8) {
  412. dev_warn(priv->dev, "Maximum 8 byte MIPI tx is supported\n");
  413. return -ENOTSUPP;
  414. }
  415. ret = mipi_dsi_create_packet(&packet, msg);
  416. if (ret)
  417. return ret;
  418. if (mipi_dsi_packet_format_is_short(msg->type)) {
  419. tc358768_write(priv, TC358768_DSICMD_TYPE,
  420. (0x10 << 8) | (packet.header[0] & 0x3f));
  421. tc358768_write(priv, TC358768_DSICMD_WC, 0);
  422. tc358768_write(priv, TC358768_DSICMD_WD0,
  423. (packet.header[2] << 8) | packet.header[1]);
  424. } else {
  425. int i;
  426. tc358768_write(priv, TC358768_DSICMD_TYPE,
  427. (0x40 << 8) | (packet.header[0] & 0x3f));
  428. tc358768_write(priv, TC358768_DSICMD_WC, packet.payload_length);
  429. for (i = 0; i < packet.payload_length; i += 2) {
  430. u16 val = packet.payload[i];
  431. if (i + 1 < packet.payload_length)
  432. val |= packet.payload[i + 1] << 8;
  433. tc358768_write(priv, TC358768_DSICMD_WD0 + i, val);
  434. }
  435. }
  436. tc358768_dsicmd_tx(priv);
  437. ret = tc358768_clear_error(priv);
  438. if (ret)
  439. dev_warn(priv->dev, "Software disable failed: %d\n", ret);
  440. else
  441. ret = packet.size;
  442. return ret;
  443. }
  444. static const struct mipi_dsi_host_ops tc358768_dsi_host_ops = {
  445. .attach = tc358768_dsi_host_attach,
  446. .detach = tc358768_dsi_host_detach,
  447. .transfer = tc358768_dsi_host_transfer,
  448. };
  449. static int tc358768_bridge_attach(struct drm_bridge *bridge,
  450. enum drm_bridge_attach_flags flags)
  451. {
  452. struct tc358768_priv *priv = bridge_to_tc358768(bridge);
  453. if (!drm_core_check_feature(bridge->dev, DRIVER_ATOMIC)) {
  454. dev_err(priv->dev, "needs atomic updates support\n");
  455. return -ENOTSUPP;
  456. }
  457. return drm_bridge_attach(bridge->encoder, priv->output.bridge, bridge,
  458. flags);
  459. }
  460. static enum drm_mode_status
  461. tc358768_bridge_mode_valid(struct drm_bridge *bridge,
  462. const struct drm_display_info *info,
  463. const struct drm_display_mode *mode)
  464. {
  465. struct tc358768_priv *priv = bridge_to_tc358768(bridge);
  466. if (tc358768_calc_pll(priv, mode, true))
  467. return MODE_CLOCK_RANGE;
  468. return MODE_OK;
  469. }
  470. static void tc358768_bridge_disable(struct drm_bridge *bridge)
  471. {
  472. struct tc358768_priv *priv = bridge_to_tc358768(bridge);
  473. int ret;
  474. /* set FrmStop */
  475. tc358768_update_bits(priv, TC358768_PP_MISC, BIT(15), BIT(15));
  476. /* wait at least for one frame */
  477. msleep(50);
  478. /* clear PP_en */
  479. tc358768_update_bits(priv, TC358768_CONFCTL, BIT(6), 0);
  480. /* set RstPtr */
  481. tc358768_update_bits(priv, TC358768_PP_MISC, BIT(14), BIT(14));
  482. ret = tc358768_clear_error(priv);
  483. if (ret)
  484. dev_warn(priv->dev, "Software disable failed: %d\n", ret);
  485. }
  486. static void tc358768_bridge_post_disable(struct drm_bridge *bridge)
  487. {
  488. struct tc358768_priv *priv = bridge_to_tc358768(bridge);
  489. tc358768_hw_disable(priv);
  490. }
  491. static int tc358768_setup_pll(struct tc358768_priv *priv,
  492. const struct drm_display_mode *mode)
  493. {
  494. u32 fbd, prd, frs;
  495. int ret;
  496. ret = tc358768_calc_pll(priv, mode, false);
  497. if (ret) {
  498. dev_err(priv->dev, "PLL calculation failed: %d\n", ret);
  499. return ret;
  500. }
  501. fbd = priv->fbd;
  502. prd = priv->prd;
  503. frs = priv->frs;
  504. dev_dbg(priv->dev, "PLL: refclk %lu, fbd %u, prd %u, frs %u\n",
  505. clk_get_rate(priv->refclk), fbd, prd, frs);
  506. dev_dbg(priv->dev, "PLL: pll_clk: %u, DSIClk %u, HSByteClk %u\n",
  507. priv->dsiclk * 2, priv->dsiclk, priv->dsiclk / 4);
  508. dev_dbg(priv->dev, "PLL: pclk %u (panel: %u)\n",
  509. tc358768_pll_to_pclk(priv, priv->dsiclk * 2),
  510. mode->clock * 1000);
  511. /* PRD[15:12] FBD[8:0] */
  512. tc358768_write(priv, TC358768_PLLCTL0, ((prd - 1) << 12) | (fbd - 1));
  513. /* FRS[11:10] LBWS[9:8] CKEN[4] RESETB[1] EN[0] */
  514. tc358768_write(priv, TC358768_PLLCTL1,
  515. (frs << 10) | (0x2 << 8) | BIT(1) | BIT(0));
  516. /* wait for lock */
  517. usleep_range(1000, 2000);
  518. /* FRS[11:10] LBWS[9:8] CKEN[4] PLL_CKEN[4] RESETB[1] EN[0] */
  519. tc358768_write(priv, TC358768_PLLCTL1,
  520. (frs << 10) | (0x2 << 8) | BIT(4) | BIT(1) | BIT(0));
  521. return tc358768_clear_error(priv);
  522. }
  523. static u32 tc358768_ns_to_cnt(u32 ns, u32 period_ps)
  524. {
  525. return DIV_ROUND_UP(ns * 1000, period_ps);
  526. }
  527. static u32 tc358768_ps_to_ns(u32 ps)
  528. {
  529. return ps / 1000;
  530. }
  531. static u32 tc358768_dpi_to_ns(u32 val, u32 pclk)
  532. {
  533. return (u32)div_u64((u64)val * NANO, pclk);
  534. }
  535. /* Convert value in DPI pixel clock units to DSI byte count */
  536. static u32 tc358768_dpi_to_dsi_bytes(struct tc358768_priv *priv, u32 val)
  537. {
  538. u64 m = (u64)val * priv->dsiclk / 4 * priv->dsi_lanes;
  539. u64 n = priv->pclk;
  540. return (u32)div_u64(m + n - 1, n);
  541. }
  542. static u32 tc358768_dsi_bytes_to_ns(struct tc358768_priv *priv, u32 val)
  543. {
  544. u64 m = (u64)val * NANO;
  545. u64 n = priv->dsiclk / 4 * priv->dsi_lanes;
  546. return (u32)div_u64(m, n);
  547. }
  548. static void tc358768_bridge_pre_enable(struct drm_bridge *bridge)
  549. {
  550. struct tc358768_priv *priv = bridge_to_tc358768(bridge);
  551. struct mipi_dsi_device *dsi_dev = priv->output.dev;
  552. unsigned long mode_flags = dsi_dev->mode_flags;
  553. u32 val, val2, lptxcnt, hact, data_type;
  554. s32 raw_val;
  555. const struct drm_display_mode *mode;
  556. u32 hsbyteclk_ps, dsiclk_ps, ui_ps;
  557. u32 dsiclk, hsbyteclk;
  558. int ret, i;
  559. struct videomode vm;
  560. struct device *dev = priv->dev;
  561. /* In pixelclock units */
  562. u32 dpi_htot, dpi_data_start;
  563. /* In byte units */
  564. u32 dsi_dpi_htot, dsi_dpi_data_start;
  565. u32 dsi_hsw, dsi_hbp, dsi_hact, dsi_hfp;
  566. const u32 dsi_hss = 4; /* HSS is a short packet (4 bytes) */
  567. /* In hsbyteclk units */
  568. u32 dsi_vsdly;
  569. const u32 internal_dly = 40;
  570. if (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
  571. dev_warn_once(dev, "Non-continuous mode unimplemented, falling back to continuous\n");
  572. mode_flags &= ~MIPI_DSI_CLOCK_NON_CONTINUOUS;
  573. }
  574. tc358768_hw_enable(priv);
  575. ret = tc358768_sw_reset(priv);
  576. if (ret) {
  577. dev_err(dev, "Software reset failed: %d\n", ret);
  578. tc358768_hw_disable(priv);
  579. return;
  580. }
  581. mode = &bridge->encoder->crtc->state->adjusted_mode;
  582. ret = tc358768_setup_pll(priv, mode);
  583. if (ret) {
  584. dev_err(dev, "PLL setup failed: %d\n", ret);
  585. tc358768_hw_disable(priv);
  586. return;
  587. }
  588. drm_display_mode_to_videomode(mode, &vm);
  589. dsiclk = priv->dsiclk;
  590. hsbyteclk = dsiclk / 4;
  591. /* Data Format Control Register */
  592. val = BIT(2) | BIT(1) | BIT(0); /* rdswap_en | dsitx_en | txdt_en */
  593. switch (dsi_dev->format) {
  594. case MIPI_DSI_FMT_RGB888:
  595. val |= (0x3 << 4);
  596. hact = vm.hactive * 3;
  597. data_type = MIPI_DSI_PACKED_PIXEL_STREAM_24;
  598. break;
  599. case MIPI_DSI_FMT_RGB666:
  600. val |= (0x4 << 4);
  601. hact = vm.hactive * 3;
  602. data_type = MIPI_DSI_PACKED_PIXEL_STREAM_18;
  603. break;
  604. case MIPI_DSI_FMT_RGB666_PACKED:
  605. val |= (0x4 << 4) | BIT(3);
  606. hact = vm.hactive * 18 / 8;
  607. data_type = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
  608. break;
  609. case MIPI_DSI_FMT_RGB565:
  610. val |= (0x5 << 4);
  611. hact = vm.hactive * 2;
  612. data_type = MIPI_DSI_PACKED_PIXEL_STREAM_16;
  613. break;
  614. default:
  615. dev_err(dev, "Invalid data format (%u)\n",
  616. dsi_dev->format);
  617. tc358768_hw_disable(priv);
  618. return;
  619. }
  620. /*
  621. * There are three important things to make TC358768 work correctly,
  622. * which are not trivial to manage:
  623. *
  624. * 1. Keep the DPI line-time and the DSI line-time as close to each
  625. * other as possible.
  626. * 2. TC358768 goes to LP mode after each line's active area. The DSI
  627. * HFP period has to be long enough for entering and exiting LP mode.
  628. * But it is not clear how to calculate this.
  629. * 3. VSDly (video start delay) has to be long enough to ensure that the
  630. * DSI TX does not start transmitting until we have started receiving
  631. * pixel data from the DPI input. It is not clear how to calculate
  632. * this either.
  633. */
  634. dpi_htot = vm.hactive + vm.hfront_porch + vm.hsync_len + vm.hback_porch;
  635. dpi_data_start = vm.hsync_len + vm.hback_porch;
  636. dev_dbg(dev, "dpi horiz timing (pclk): %u + %u + %u + %u = %u\n",
  637. vm.hsync_len, vm.hback_porch, vm.hactive, vm.hfront_porch,
  638. dpi_htot);
  639. dev_dbg(dev, "dpi horiz timing (ns): %u + %u + %u + %u = %u\n",
  640. tc358768_dpi_to_ns(vm.hsync_len, vm.pixelclock),
  641. tc358768_dpi_to_ns(vm.hback_porch, vm.pixelclock),
  642. tc358768_dpi_to_ns(vm.hactive, vm.pixelclock),
  643. tc358768_dpi_to_ns(vm.hfront_porch, vm.pixelclock),
  644. tc358768_dpi_to_ns(dpi_htot, vm.pixelclock));
  645. dev_dbg(dev, "dpi data start (ns): %u + %u = %u\n",
  646. tc358768_dpi_to_ns(vm.hsync_len, vm.pixelclock),
  647. tc358768_dpi_to_ns(vm.hback_porch, vm.pixelclock),
  648. tc358768_dpi_to_ns(dpi_data_start, vm.pixelclock));
  649. dsi_dpi_htot = tc358768_dpi_to_dsi_bytes(priv, dpi_htot);
  650. dsi_dpi_data_start = tc358768_dpi_to_dsi_bytes(priv, dpi_data_start);
  651. if (dsi_dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
  652. dsi_hsw = tc358768_dpi_to_dsi_bytes(priv, vm.hsync_len);
  653. dsi_hbp = tc358768_dpi_to_dsi_bytes(priv, vm.hback_porch);
  654. } else {
  655. /* HBP is included in HSW in event mode */
  656. dsi_hbp = 0;
  657. dsi_hsw = tc358768_dpi_to_dsi_bytes(priv,
  658. vm.hsync_len +
  659. vm.hback_porch);
  660. /*
  661. * The pixel packet includes the actual pixel data, and:
  662. * DSI packet header = 4 bytes
  663. * DCS code = 1 byte
  664. * DSI packet footer = 2 bytes
  665. */
  666. dsi_hact = hact + 4 + 1 + 2;
  667. dsi_hfp = dsi_dpi_htot - dsi_hact - dsi_hsw - dsi_hss;
  668. /*
  669. * Here we should check if HFP is long enough for entering LP
  670. * and exiting LP, but it's not clear how to calculate that.
  671. * Instead, this is a naive algorithm that just adjusts the HFP
  672. * and HSW so that HFP is (at least) roughly 2/3 of the total
  673. * blanking time.
  674. */
  675. if (dsi_hfp < (dsi_hfp + dsi_hsw + dsi_hss) * 2 / 3) {
  676. u32 old_hfp = dsi_hfp;
  677. u32 old_hsw = dsi_hsw;
  678. u32 tot = dsi_hfp + dsi_hsw + dsi_hss;
  679. dsi_hsw = tot / 3;
  680. /*
  681. * Seems like sometimes HSW has to be divisible by num-lanes, but
  682. * not always...
  683. */
  684. dsi_hsw = roundup(dsi_hsw, priv->dsi_lanes);
  685. dsi_hfp = dsi_dpi_htot - dsi_hact - dsi_hsw - dsi_hss;
  686. dev_dbg(dev,
  687. "hfp too short, adjusting dsi hfp and dsi hsw from %u, %u to %u, %u\n",
  688. old_hfp, old_hsw, dsi_hfp, dsi_hsw);
  689. }
  690. dev_dbg(dev,
  691. "dsi horiz timing (bytes): %u, %u + %u + %u + %u = %u\n",
  692. dsi_hss, dsi_hsw, dsi_hbp, dsi_hact, dsi_hfp,
  693. dsi_hss + dsi_hsw + dsi_hbp + dsi_hact + dsi_hfp);
  694. dev_dbg(dev, "dsi horiz timing (ns): %u + %u + %u + %u + %u = %u\n",
  695. tc358768_dsi_bytes_to_ns(priv, dsi_hss),
  696. tc358768_dsi_bytes_to_ns(priv, dsi_hsw),
  697. tc358768_dsi_bytes_to_ns(priv, dsi_hbp),
  698. tc358768_dsi_bytes_to_ns(priv, dsi_hact),
  699. tc358768_dsi_bytes_to_ns(priv, dsi_hfp),
  700. tc358768_dsi_bytes_to_ns(priv, dsi_hss + dsi_hsw +
  701. dsi_hbp + dsi_hact + dsi_hfp));
  702. }
  703. /* VSDly calculation */
  704. /* Start with the HW internal delay */
  705. dsi_vsdly = internal_dly;
  706. /* Convert to byte units as the other variables are in byte units */
  707. dsi_vsdly *= priv->dsi_lanes;
  708. /* Do we need more delay, in addition to the internal? */
  709. if (dsi_dpi_data_start > dsi_vsdly + dsi_hss + dsi_hsw + dsi_hbp) {
  710. dsi_vsdly = dsi_dpi_data_start - dsi_hss - dsi_hsw - dsi_hbp;
  711. dsi_vsdly = roundup(dsi_vsdly, priv->dsi_lanes);
  712. }
  713. dev_dbg(dev, "dsi data start (bytes) %u + %u + %u + %u = %u\n",
  714. dsi_vsdly, dsi_hss, dsi_hsw, dsi_hbp,
  715. dsi_vsdly + dsi_hss + dsi_hsw + dsi_hbp);
  716. dev_dbg(dev, "dsi data start (ns) %u + %u + %u + %u = %u\n",
  717. tc358768_dsi_bytes_to_ns(priv, dsi_vsdly),
  718. tc358768_dsi_bytes_to_ns(priv, dsi_hss),
  719. tc358768_dsi_bytes_to_ns(priv, dsi_hsw),
  720. tc358768_dsi_bytes_to_ns(priv, dsi_hbp),
  721. tc358768_dsi_bytes_to_ns(priv, dsi_vsdly + dsi_hss + dsi_hsw + dsi_hbp));
  722. /* Convert back to hsbyteclk */
  723. dsi_vsdly /= priv->dsi_lanes;
  724. /*
  725. * The docs say that there is an internal delay of 40 cycles.
  726. * However, we get underflows if we follow that rule. If we
  727. * instead ignore the internal delay, things work. So either
  728. * the docs are wrong or the calculations are wrong.
  729. *
  730. * As a temporary fix, add the internal delay here, to counter
  731. * the subtraction when writing the register.
  732. */
  733. dsi_vsdly += internal_dly;
  734. /* Clamp to the register max */
  735. if (dsi_vsdly - internal_dly > 0x3ff) {
  736. dev_warn(dev, "VSDly too high, underflows likely\n");
  737. dsi_vsdly = 0x3ff + internal_dly;
  738. }
  739. /* VSDly[9:0] */
  740. tc358768_write(priv, TC358768_VSDLY, dsi_vsdly - internal_dly);
  741. tc358768_write(priv, TC358768_DATAFMT, val);
  742. tc358768_write(priv, TC358768_DSITX_DT, data_type);
  743. /* Enable D-PHY (HiZ->LP11) */
  744. tc358768_write(priv, TC358768_CLW_CNTRL, 0x0000);
  745. /* Enable lanes */
  746. for (i = 0; i < dsi_dev->lanes; i++)
  747. tc358768_write(priv, TC358768_D0W_CNTRL + i * 4, 0x0000);
  748. /* DSI Timings */
  749. hsbyteclk_ps = (u32)div_u64(PICO, hsbyteclk);
  750. dsiclk_ps = (u32)div_u64(PICO, dsiclk);
  751. ui_ps = dsiclk_ps / 2;
  752. dev_dbg(dev, "dsiclk: %u ps, ui %u ps, hsbyteclk %u ps\n", dsiclk_ps,
  753. ui_ps, hsbyteclk_ps);
  754. /* LP11 > 100us for D-PHY Rx Init */
  755. val = tc358768_ns_to_cnt(100 * 1000, hsbyteclk_ps) - 1;
  756. dev_dbg(dev, "LINEINITCNT: %u\n", val);
  757. tc358768_write(priv, TC358768_LINEINITCNT, val);
  758. /* LPTimeCnt > 50ns */
  759. val = tc358768_ns_to_cnt(50, hsbyteclk_ps) - 1;
  760. lptxcnt = val;
  761. dev_dbg(dev, "LPTXTIMECNT: %u\n", val);
  762. tc358768_write(priv, TC358768_LPTXTIMECNT, val);
  763. /* 38ns < TCLK_PREPARE < 95ns */
  764. val = tc358768_ns_to_cnt(65, hsbyteclk_ps) - 1;
  765. dev_dbg(dev, "TCLK_PREPARECNT %u\n", val);
  766. /* TCLK_PREPARE + TCLK_ZERO > 300ns */
  767. val2 = tc358768_ns_to_cnt(300 - tc358768_ps_to_ns(2 * ui_ps),
  768. hsbyteclk_ps) - 2;
  769. dev_dbg(dev, "TCLK_ZEROCNT %u\n", val2);
  770. val |= val2 << 8;
  771. tc358768_write(priv, TC358768_TCLK_HEADERCNT, val);
  772. /* TCLK_TRAIL > 60ns AND TEOT <= 105 ns + 12*UI */
  773. raw_val = tc358768_ns_to_cnt(60 + tc358768_ps_to_ns(2 * ui_ps), hsbyteclk_ps) - 5;
  774. val = clamp(raw_val, 0, 127);
  775. dev_dbg(dev, "TCLK_TRAILCNT: %u\n", val);
  776. tc358768_write(priv, TC358768_TCLK_TRAILCNT, val);
  777. /* 40ns + 4*UI < THS_PREPARE < 85ns + 6*UI */
  778. val = 50 + tc358768_ps_to_ns(4 * ui_ps);
  779. val = tc358768_ns_to_cnt(val, hsbyteclk_ps) - 1;
  780. dev_dbg(dev, "THS_PREPARECNT %u\n", val);
  781. /* THS_PREPARE + THS_ZERO > 145ns + 10*UI */
  782. raw_val = tc358768_ns_to_cnt(145 - tc358768_ps_to_ns(3 * ui_ps), hsbyteclk_ps) - 10;
  783. val2 = clamp(raw_val, 0, 127);
  784. dev_dbg(dev, "THS_ZEROCNT %u\n", val2);
  785. val |= val2 << 8;
  786. tc358768_write(priv, TC358768_THS_HEADERCNT, val);
  787. /* TWAKEUP > 1ms in lptxcnt steps */
  788. val = tc358768_ns_to_cnt(1020000, hsbyteclk_ps);
  789. val = val / (lptxcnt + 1) - 1;
  790. dev_dbg(dev, "TWAKEUP: %u\n", val);
  791. tc358768_write(priv, TC358768_TWAKEUP, val);
  792. /* TCLK_POSTCNT > 60ns + 52*UI */
  793. val = tc358768_ns_to_cnt(60 + tc358768_ps_to_ns(52 * ui_ps),
  794. hsbyteclk_ps) - 3;
  795. dev_dbg(dev, "TCLK_POSTCNT: %u\n", val);
  796. tc358768_write(priv, TC358768_TCLK_POSTCNT, val);
  797. /* max(60ns + 4*UI, 8*UI) < THS_TRAILCNT < 105ns + 12*UI */
  798. raw_val = tc358768_ns_to_cnt(60 + tc358768_ps_to_ns(18 * ui_ps),
  799. hsbyteclk_ps) - 4;
  800. val = clamp(raw_val, 0, 15);
  801. dev_dbg(dev, "THS_TRAILCNT: %u\n", val);
  802. tc358768_write(priv, TC358768_THS_TRAILCNT, val);
  803. val = BIT(0);
  804. for (i = 0; i < dsi_dev->lanes; i++)
  805. val |= BIT(i + 1);
  806. tc358768_write(priv, TC358768_HSTXVREGEN, val);
  807. tc358768_write(priv, TC358768_TXOPTIONCNTRL,
  808. (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) ? 0 : BIT(0));
  809. /* TXTAGOCNT[26:16] RXTASURECNT[10:0] */
  810. val = tc358768_ps_to_ns((lptxcnt + 1) * hsbyteclk_ps * 4);
  811. val = tc358768_ns_to_cnt(val, hsbyteclk_ps) / 4 - 1;
  812. dev_dbg(dev, "TXTAGOCNT: %u\n", val);
  813. val2 = tc358768_ns_to_cnt(tc358768_ps_to_ns((lptxcnt + 1) * hsbyteclk_ps),
  814. hsbyteclk_ps) - 2;
  815. dev_dbg(dev, "RXTASURECNT: %u\n", val2);
  816. val = val << 16 | val2;
  817. tc358768_write(priv, TC358768_BTACNTRL1, val);
  818. /* START[0] */
  819. tc358768_write(priv, TC358768_STARTCNTRL, 1);
  820. if (dsi_dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
  821. /* Set pulse mode */
  822. tc358768_write(priv, TC358768_DSI_EVENT, 0);
  823. /* vact */
  824. tc358768_write(priv, TC358768_DSI_VACT, vm.vactive);
  825. /* vsw */
  826. tc358768_write(priv, TC358768_DSI_VSW, vm.vsync_len);
  827. /* vbp */
  828. tc358768_write(priv, TC358768_DSI_VBPR, vm.vback_porch);
  829. } else {
  830. /* Set event mode */
  831. tc358768_write(priv, TC358768_DSI_EVENT, 1);
  832. /* vact */
  833. tc358768_write(priv, TC358768_DSI_VACT, vm.vactive);
  834. /* vsw (+ vbp) */
  835. tc358768_write(priv, TC358768_DSI_VSW,
  836. vm.vsync_len + vm.vback_porch);
  837. /* vbp (not used in event mode) */
  838. tc358768_write(priv, TC358768_DSI_VBPR, 0);
  839. }
  840. /* hsw (bytes) */
  841. tc358768_write(priv, TC358768_DSI_HSW, dsi_hsw);
  842. /* hbp (bytes) */
  843. tc358768_write(priv, TC358768_DSI_HBPR, dsi_hbp);
  844. /* hact (bytes) */
  845. tc358768_write(priv, TC358768_DSI_HACT, hact);
  846. /* VSYNC polarity */
  847. tc358768_update_bits(priv, TC358768_CONFCTL, BIT(5),
  848. (mode->flags & DRM_MODE_FLAG_PVSYNC) ? BIT(5) : 0);
  849. /* HSYNC polarity */
  850. tc358768_update_bits(priv, TC358768_PP_MISC, BIT(0),
  851. (mode->flags & DRM_MODE_FLAG_PHSYNC) ? BIT(0) : 0);
  852. /* Start DSI Tx */
  853. tc358768_write(priv, TC358768_DSI_START, 0x1);
  854. /* Configure DSI_Control register */
  855. val = TC358768_DSI_CONFW_MODE_CLR | TC358768_DSI_CONFW_ADDR_DSI_CONTROL;
  856. val |= TC358768_DSI_CONTROL_TXMD | TC358768_DSI_CONTROL_HSCKMD |
  857. 0x3 << 1 | TC358768_DSI_CONTROL_EOTDIS;
  858. tc358768_write(priv, TC358768_DSI_CONFW, val);
  859. val = TC358768_DSI_CONFW_MODE_SET | TC358768_DSI_CONFW_ADDR_DSI_CONTROL;
  860. val |= (dsi_dev->lanes - 1) << 1;
  861. val |= TC358768_DSI_CONTROL_TXMD;
  862. if (!(mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
  863. val |= TC358768_DSI_CONTROL_HSCKMD;
  864. if (dsi_dev->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)
  865. val |= TC358768_DSI_CONTROL_EOTDIS;
  866. tc358768_write(priv, TC358768_DSI_CONFW, val);
  867. val = TC358768_DSI_CONFW_MODE_CLR | TC358768_DSI_CONFW_ADDR_DSI_CONTROL;
  868. val |= TC358768_DSI_CONTROL_DIS_MODE; /* DSI mode */
  869. tc358768_write(priv, TC358768_DSI_CONFW, val);
  870. ret = tc358768_clear_error(priv);
  871. if (ret) {
  872. dev_err(dev, "Bridge pre_enable failed: %d\n", ret);
  873. tc358768_bridge_disable(bridge);
  874. tc358768_bridge_post_disable(bridge);
  875. }
  876. }
  877. static void tc358768_bridge_enable(struct drm_bridge *bridge)
  878. {
  879. struct tc358768_priv *priv = bridge_to_tc358768(bridge);
  880. int ret;
  881. if (!priv->enabled) {
  882. dev_err(priv->dev, "Bridge is not enabled\n");
  883. return;
  884. }
  885. /* clear FrmStop and RstPtr */
  886. tc358768_update_bits(priv, TC358768_PP_MISC, 0x3 << 14, 0);
  887. /* set PP_en */
  888. tc358768_update_bits(priv, TC358768_CONFCTL, BIT(6), BIT(6));
  889. ret = tc358768_clear_error(priv);
  890. if (ret) {
  891. dev_err(priv->dev, "Bridge enable failed: %d\n", ret);
  892. tc358768_bridge_disable(bridge);
  893. tc358768_bridge_post_disable(bridge);
  894. }
  895. }
  896. #define MAX_INPUT_SEL_FORMATS 1
  897. static u32 *
  898. tc358768_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
  899. struct drm_bridge_state *bridge_state,
  900. struct drm_crtc_state *crtc_state,
  901. struct drm_connector_state *conn_state,
  902. u32 output_fmt,
  903. unsigned int *num_input_fmts)
  904. {
  905. struct tc358768_priv *priv = bridge_to_tc358768(bridge);
  906. u32 *input_fmts;
  907. *num_input_fmts = 0;
  908. input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
  909. GFP_KERNEL);
  910. if (!input_fmts)
  911. return NULL;
  912. switch (priv->pd_lines) {
  913. case 16:
  914. input_fmts[0] = MEDIA_BUS_FMT_RGB565_1X16;
  915. break;
  916. case 18:
  917. input_fmts[0] = MEDIA_BUS_FMT_RGB666_1X18;
  918. break;
  919. default:
  920. case 24:
  921. input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
  922. break;
  923. }
  924. *num_input_fmts = MAX_INPUT_SEL_FORMATS;
  925. return input_fmts;
  926. }
  927. static bool tc358768_mode_fixup(struct drm_bridge *bridge,
  928. const struct drm_display_mode *mode,
  929. struct drm_display_mode *adjusted_mode)
  930. {
  931. /* Default to positive sync */
  932. if (!(adjusted_mode->flags &
  933. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  934. adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
  935. if (!(adjusted_mode->flags &
  936. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  937. adjusted_mode->flags |= DRM_MODE_FLAG_PVSYNC;
  938. return true;
  939. }
  940. static const struct drm_bridge_funcs tc358768_bridge_funcs = {
  941. .attach = tc358768_bridge_attach,
  942. .mode_valid = tc358768_bridge_mode_valid,
  943. .mode_fixup = tc358768_mode_fixup,
  944. .pre_enable = tc358768_bridge_pre_enable,
  945. .enable = tc358768_bridge_enable,
  946. .disable = tc358768_bridge_disable,
  947. .post_disable = tc358768_bridge_post_disable,
  948. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  949. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  950. .atomic_reset = drm_atomic_helper_bridge_reset,
  951. .atomic_get_input_bus_fmts = tc358768_atomic_get_input_bus_fmts,
  952. };
  953. static const struct drm_bridge_timings default_tc358768_timings = {
  954. .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
  955. | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE
  956. | DRM_BUS_FLAG_DE_HIGH,
  957. };
  958. static bool tc358768_is_reserved_reg(unsigned int reg)
  959. {
  960. switch (reg) {
  961. case 0x114 ... 0x13f:
  962. case 0x200:
  963. case 0x20c:
  964. case 0x400 ... 0x408:
  965. case 0x41c ... 0x42f:
  966. return true;
  967. default:
  968. return false;
  969. }
  970. }
  971. static bool tc358768_writeable_reg(struct device *dev, unsigned int reg)
  972. {
  973. if (tc358768_is_reserved_reg(reg))
  974. return false;
  975. switch (reg) {
  976. case TC358768_CHIPID:
  977. case TC358768_FIFOSTATUS:
  978. case TC358768_DSITXSTATUS ... (TC358768_DSITXSTATUS + 2):
  979. case TC358768_DSI_CONTROL ... (TC358768_DSI_INT_ENA + 2):
  980. case TC358768_DSICMD_RDFIFO ... (TC358768_DSI_ERR_HALT + 2):
  981. return false;
  982. default:
  983. return true;
  984. }
  985. }
  986. static bool tc358768_readable_reg(struct device *dev, unsigned int reg)
  987. {
  988. if (tc358768_is_reserved_reg(reg))
  989. return false;
  990. switch (reg) {
  991. case TC358768_STARTCNTRL:
  992. case TC358768_DSI_CONFW ... (TC358768_DSI_CONFW + 2):
  993. case TC358768_DSI_INT_CLR ... (TC358768_DSI_INT_CLR + 2):
  994. case TC358768_DSI_START ... (TC358768_DSI_START + 2):
  995. case TC358768_DBG_DATA:
  996. return false;
  997. default:
  998. return true;
  999. }
  1000. }
  1001. static const struct regmap_config tc358768_regmap_config = {
  1002. .name = "tc358768",
  1003. .reg_bits = 16,
  1004. .val_bits = 16,
  1005. .max_register = TC358768_DSI_HACT,
  1006. .cache_type = REGCACHE_NONE,
  1007. .writeable_reg = tc358768_writeable_reg,
  1008. .readable_reg = tc358768_readable_reg,
  1009. .reg_format_endian = REGMAP_ENDIAN_BIG,
  1010. .val_format_endian = REGMAP_ENDIAN_BIG,
  1011. };
  1012. static const struct i2c_device_id tc358768_i2c_ids[] = {
  1013. { "tc358768", 0 },
  1014. { "tc358778", 0 },
  1015. { }
  1016. };
  1017. MODULE_DEVICE_TABLE(i2c, tc358768_i2c_ids);
  1018. static const struct of_device_id tc358768_of_ids[] = {
  1019. { .compatible = "toshiba,tc358768", },
  1020. { .compatible = "toshiba,tc358778", },
  1021. { }
  1022. };
  1023. MODULE_DEVICE_TABLE(of, tc358768_of_ids);
  1024. static int tc358768_get_regulators(struct tc358768_priv *priv)
  1025. {
  1026. int i, ret;
  1027. for (i = 0; i < ARRAY_SIZE(priv->supplies); ++i)
  1028. priv->supplies[i].supply = tc358768_supplies[i];
  1029. ret = devm_regulator_bulk_get(priv->dev, ARRAY_SIZE(priv->supplies),
  1030. priv->supplies);
  1031. if (ret < 0)
  1032. dev_err(priv->dev, "failed to get regulators: %d\n", ret);
  1033. return ret;
  1034. }
  1035. static int tc358768_i2c_probe(struct i2c_client *client)
  1036. {
  1037. struct tc358768_priv *priv;
  1038. struct device *dev = &client->dev;
  1039. struct device_node *np = dev->of_node;
  1040. int ret;
  1041. if (!np)
  1042. return -ENODEV;
  1043. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1044. if (!priv)
  1045. return -ENOMEM;
  1046. dev_set_drvdata(dev, priv);
  1047. priv->dev = dev;
  1048. ret = tc358768_get_regulators(priv);
  1049. if (ret)
  1050. return ret;
  1051. priv->refclk = devm_clk_get(dev, "refclk");
  1052. if (IS_ERR(priv->refclk))
  1053. return PTR_ERR(priv->refclk);
  1054. /*
  1055. * RESX is low active, to disable tc358768 initially (keep in reset)
  1056. * the gpio line must be LOW. This is the ASSERTED state of
  1057. * GPIO_ACTIVE_LOW (GPIOD_OUT_HIGH == ASSERTED).
  1058. */
  1059. priv->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1060. GPIOD_OUT_HIGH);
  1061. if (IS_ERR(priv->reset_gpio))
  1062. return PTR_ERR(priv->reset_gpio);
  1063. priv->regmap = devm_regmap_init_i2c(client, &tc358768_regmap_config);
  1064. if (IS_ERR(priv->regmap)) {
  1065. dev_err(dev, "Failed to init regmap\n");
  1066. return PTR_ERR(priv->regmap);
  1067. }
  1068. priv->dsi_host.dev = dev;
  1069. priv->dsi_host.ops = &tc358768_dsi_host_ops;
  1070. priv->bridge.funcs = &tc358768_bridge_funcs;
  1071. priv->bridge.timings = &default_tc358768_timings;
  1072. priv->bridge.of_node = np;
  1073. i2c_set_clientdata(client, priv);
  1074. return mipi_dsi_host_register(&priv->dsi_host);
  1075. }
  1076. static void tc358768_i2c_remove(struct i2c_client *client)
  1077. {
  1078. struct tc358768_priv *priv = i2c_get_clientdata(client);
  1079. mipi_dsi_host_unregister(&priv->dsi_host);
  1080. }
  1081. static struct i2c_driver tc358768_driver = {
  1082. .driver = {
  1083. .name = "tc358768",
  1084. .of_match_table = tc358768_of_ids,
  1085. },
  1086. .id_table = tc358768_i2c_ids,
  1087. .probe = tc358768_i2c_probe,
  1088. .remove = tc358768_i2c_remove,
  1089. };
  1090. module_i2c_driver(tc358768_driver);
  1091. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  1092. MODULE_DESCRIPTION("TC358768AXBG/TC358778XBG DSI bridge");
  1093. MODULE_LICENSE("GPL v2");