lontium-lt9611.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2019-2020. Linaro Limited.
  5. */
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/i2c.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/media-bus-format.h>
  10. #include <linux/module.h>
  11. #include <linux/of_graph.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <sound/hdmi-codec.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_bridge.h>
  18. #include <drm/drm_edid.h>
  19. #include <drm/drm_mipi_dsi.h>
  20. #include <drm/drm_of.h>
  21. #include <drm/drm_print.h>
  22. #include <drm/drm_probe_helper.h>
  23. #define EDID_SEG_SIZE 256
  24. #define EDID_LEN 32
  25. #define EDID_LOOP 8
  26. #define KEY_DDC_ACCS_DONE 0x02
  27. #define DDC_NO_ACK 0x50
  28. #define LT9611_4LANES 0
  29. struct lt9611 {
  30. struct device *dev;
  31. struct drm_bridge bridge;
  32. struct drm_bridge *next_bridge;
  33. struct regmap *regmap;
  34. struct device_node *dsi0_node;
  35. struct device_node *dsi1_node;
  36. struct mipi_dsi_device *dsi0;
  37. struct mipi_dsi_device *dsi1;
  38. struct platform_device *audio_pdev;
  39. bool ac_mode;
  40. struct gpio_desc *reset_gpio;
  41. struct gpio_desc *enable_gpio;
  42. bool power_on;
  43. bool sleep;
  44. struct regulator_bulk_data supplies[2];
  45. struct i2c_client *client;
  46. enum drm_connector_status status;
  47. u8 edid_buf[EDID_SEG_SIZE];
  48. };
  49. #define LT9611_PAGE_CONTROL 0xff
  50. static const struct regmap_range_cfg lt9611_ranges[] = {
  51. {
  52. .name = "register_range",
  53. .range_min = 0,
  54. .range_max = 0x85ff,
  55. .selector_reg = LT9611_PAGE_CONTROL,
  56. .selector_mask = 0xff,
  57. .selector_shift = 0,
  58. .window_start = 0,
  59. .window_len = 0x100,
  60. },
  61. };
  62. static const struct regmap_config lt9611_regmap_config = {
  63. .reg_bits = 8,
  64. .val_bits = 8,
  65. .max_register = 0xffff,
  66. .ranges = lt9611_ranges,
  67. .num_ranges = ARRAY_SIZE(lt9611_ranges),
  68. };
  69. static struct lt9611 *bridge_to_lt9611(struct drm_bridge *bridge)
  70. {
  71. return container_of(bridge, struct lt9611, bridge);
  72. }
  73. static int lt9611_mipi_input_analog(struct lt9611 *lt9611)
  74. {
  75. const struct reg_sequence reg_cfg[] = {
  76. { 0x8106, 0x40 }, /* port A rx current */
  77. { 0x810a, 0xfe }, /* port A ldo voltage set */
  78. { 0x810b, 0xbf }, /* enable port A lprx */
  79. { 0x8111, 0x40 }, /* port B rx current */
  80. { 0x8115, 0xfe }, /* port B ldo voltage set */
  81. { 0x8116, 0xbf }, /* enable port B lprx */
  82. { 0x811c, 0x03 }, /* PortA clk lane no-LP mode */
  83. { 0x8120, 0x03 }, /* PortB clk lane with-LP mode */
  84. };
  85. return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
  86. }
  87. static int lt9611_mipi_input_digital(struct lt9611 *lt9611,
  88. const struct drm_display_mode *mode)
  89. {
  90. struct reg_sequence reg_cfg[] = {
  91. { 0x8300, LT9611_4LANES },
  92. { 0x830a, 0x00 },
  93. { 0x824f, 0x80 },
  94. { 0x8250, 0x10 },
  95. { 0x8302, 0x0a },
  96. { 0x8306, 0x0a },
  97. };
  98. if (lt9611->dsi1_node)
  99. reg_cfg[1].def = 0x03;
  100. return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
  101. }
  102. static void lt9611_mipi_video_setup(struct lt9611 *lt9611,
  103. const struct drm_display_mode *mode)
  104. {
  105. u32 h_total, hactive, hsync_len, hfront_porch, hsync_porch;
  106. u32 v_total, vactive, vsync_len, vfront_porch, vsync_porch;
  107. h_total = mode->htotal;
  108. v_total = mode->vtotal;
  109. hactive = mode->hdisplay;
  110. hsync_len = mode->hsync_end - mode->hsync_start;
  111. hfront_porch = mode->hsync_start - mode->hdisplay;
  112. hsync_porch = mode->htotal - mode->hsync_start;
  113. vactive = mode->vdisplay;
  114. vsync_len = mode->vsync_end - mode->vsync_start;
  115. vfront_porch = mode->vsync_start - mode->vdisplay;
  116. vsync_porch = mode->vtotal - mode->vsync_start;
  117. regmap_write(lt9611->regmap, 0x830d, (u8)(v_total / 256));
  118. regmap_write(lt9611->regmap, 0x830e, (u8)(v_total % 256));
  119. regmap_write(lt9611->regmap, 0x830f, (u8)(vactive / 256));
  120. regmap_write(lt9611->regmap, 0x8310, (u8)(vactive % 256));
  121. regmap_write(lt9611->regmap, 0x8311, (u8)(h_total / 256));
  122. regmap_write(lt9611->regmap, 0x8312, (u8)(h_total % 256));
  123. regmap_write(lt9611->regmap, 0x8313, (u8)(hactive / 256));
  124. regmap_write(lt9611->regmap, 0x8314, (u8)(hactive % 256));
  125. regmap_write(lt9611->regmap, 0x8315, (u8)(vsync_len % 256));
  126. regmap_write(lt9611->regmap, 0x8316, (u8)(hsync_len % 256));
  127. regmap_write(lt9611->regmap, 0x8317, (u8)(vfront_porch % 256));
  128. regmap_write(lt9611->regmap, 0x8318, (u8)(vsync_porch % 256));
  129. regmap_write(lt9611->regmap, 0x8319, (u8)(hfront_porch % 256));
  130. regmap_write(lt9611->regmap, 0x831a, (u8)(hsync_porch / 256) |
  131. ((hfront_porch / 256) << 4));
  132. regmap_write(lt9611->regmap, 0x831b, (u8)(hsync_porch % 256));
  133. }
  134. static void lt9611_pcr_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode, unsigned int postdiv)
  135. {
  136. unsigned int pcr_m = mode->clock * 5 * postdiv / 27000;
  137. const struct reg_sequence reg_cfg[] = {
  138. { 0x830b, 0x01 },
  139. { 0x830c, 0x10 },
  140. { 0x8348, 0x00 },
  141. { 0x8349, 0x81 },
  142. /* stage 1 */
  143. { 0x8321, 0x4a },
  144. { 0x8324, 0x71 },
  145. { 0x8325, 0x30 },
  146. { 0x832a, 0x01 },
  147. /* stage 2 */
  148. { 0x834a, 0x40 },
  149. /* MK limit */
  150. { 0x832d, 0x38 },
  151. { 0x8331, 0x08 },
  152. };
  153. u8 pol = 0x10;
  154. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  155. pol |= 0x2;
  156. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  157. pol |= 0x1;
  158. regmap_write(lt9611->regmap, 0x831d, pol);
  159. regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
  160. if (lt9611->dsi1_node) {
  161. unsigned int hact = mode->hdisplay;
  162. hact >>= 2;
  163. hact += 0x50;
  164. hact = min(hact, 0x3e0U);
  165. regmap_write(lt9611->regmap, 0x830b, hact / 256);
  166. regmap_write(lt9611->regmap, 0x830c, hact % 256);
  167. regmap_write(lt9611->regmap, 0x8348, hact / 256);
  168. regmap_write(lt9611->regmap, 0x8349, hact % 256);
  169. }
  170. regmap_write(lt9611->regmap, 0x8326, pcr_m);
  171. /* pcr rst */
  172. regmap_write(lt9611->regmap, 0x8011, 0x5a);
  173. regmap_write(lt9611->regmap, 0x8011, 0xfa);
  174. }
  175. static int lt9611_pll_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode, unsigned int *postdiv)
  176. {
  177. unsigned int pclk = mode->clock;
  178. const struct reg_sequence reg_cfg[] = {
  179. /* txpll init */
  180. { 0x8123, 0x40 },
  181. { 0x8124, 0x64 },
  182. { 0x8125, 0x80 },
  183. { 0x8126, 0x55 },
  184. { 0x812c, 0x37 },
  185. { 0x812f, 0x01 },
  186. { 0x8126, 0x55 },
  187. { 0x8127, 0x66 },
  188. { 0x8128, 0x88 },
  189. { 0x812a, 0x20 },
  190. };
  191. regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
  192. if (pclk > 150000) {
  193. regmap_write(lt9611->regmap, 0x812d, 0x88);
  194. *postdiv = 1;
  195. } else if (pclk > 70000) {
  196. regmap_write(lt9611->regmap, 0x812d, 0x99);
  197. *postdiv = 2;
  198. } else {
  199. regmap_write(lt9611->regmap, 0x812d, 0xaa);
  200. *postdiv = 4;
  201. }
  202. /*
  203. * first divide pclk by 2 first
  204. * - write divide by 64k to 19:16 bits which means shift by 17
  205. * - write divide by 256 to 15:8 bits which means shift by 9
  206. * - write remainder to 7:0 bits, which means shift by 1
  207. */
  208. regmap_write(lt9611->regmap, 0x82e3, pclk >> 17); /* pclk[19:16] */
  209. regmap_write(lt9611->regmap, 0x82e4, pclk >> 9); /* pclk[15:8] */
  210. regmap_write(lt9611->regmap, 0x82e5, pclk >> 1); /* pclk[7:0] */
  211. regmap_write(lt9611->regmap, 0x82de, 0x20);
  212. regmap_write(lt9611->regmap, 0x82de, 0xe0);
  213. regmap_write(lt9611->regmap, 0x8016, 0xf1);
  214. regmap_write(lt9611->regmap, 0x8016, 0xf3);
  215. return 0;
  216. }
  217. static int lt9611_read_video_check(struct lt9611 *lt9611, unsigned int reg)
  218. {
  219. unsigned int temp, temp2;
  220. int ret;
  221. ret = regmap_read(lt9611->regmap, reg, &temp);
  222. if (ret)
  223. return ret;
  224. temp <<= 8;
  225. ret = regmap_read(lt9611->regmap, reg + 1, &temp2);
  226. if (ret)
  227. return ret;
  228. return (temp + temp2);
  229. }
  230. static int lt9611_video_check(struct lt9611 *lt9611)
  231. {
  232. u32 v_total, vactive, hactive_a, hactive_b, h_total_sysclk;
  233. int temp;
  234. /* top module video check */
  235. /* vactive */
  236. temp = lt9611_read_video_check(lt9611, 0x8282);
  237. if (temp < 0)
  238. goto end;
  239. vactive = temp;
  240. /* v_total */
  241. temp = lt9611_read_video_check(lt9611, 0x826c);
  242. if (temp < 0)
  243. goto end;
  244. v_total = temp;
  245. /* h_total_sysclk */
  246. temp = lt9611_read_video_check(lt9611, 0x8286);
  247. if (temp < 0)
  248. goto end;
  249. h_total_sysclk = temp;
  250. /* hactive_a */
  251. temp = lt9611_read_video_check(lt9611, 0x8382);
  252. if (temp < 0)
  253. goto end;
  254. hactive_a = temp / 3;
  255. /* hactive_b */
  256. temp = lt9611_read_video_check(lt9611, 0x8386);
  257. if (temp < 0)
  258. goto end;
  259. hactive_b = temp / 3;
  260. dev_info(lt9611->dev,
  261. "video check: hactive_a=%d, hactive_b=%d, vactive=%d, v_total=%d, h_total_sysclk=%d\n",
  262. hactive_a, hactive_b, vactive, v_total, h_total_sysclk);
  263. return 0;
  264. end:
  265. dev_err(lt9611->dev, "read video check error\n");
  266. return temp;
  267. }
  268. static void lt9611_hdmi_set_infoframes(struct lt9611 *lt9611,
  269. struct drm_connector *connector,
  270. struct drm_display_mode *mode)
  271. {
  272. union hdmi_infoframe infoframe;
  273. ssize_t len;
  274. u8 iframes = 0x0a; /* UD1 infoframe */
  275. u8 buf[32];
  276. int ret;
  277. int i;
  278. ret = drm_hdmi_avi_infoframe_from_display_mode(&infoframe.avi,
  279. connector,
  280. mode);
  281. if (ret < 0)
  282. goto out;
  283. len = hdmi_infoframe_pack(&infoframe, buf, sizeof(buf));
  284. if (len < 0)
  285. goto out;
  286. for (i = 0; i < len; i++)
  287. regmap_write(lt9611->regmap, 0x8440 + i, buf[i]);
  288. ret = drm_hdmi_vendor_infoframe_from_display_mode(&infoframe.vendor.hdmi,
  289. connector,
  290. mode);
  291. if (ret < 0)
  292. goto out;
  293. len = hdmi_infoframe_pack(&infoframe, buf, sizeof(buf));
  294. if (len < 0)
  295. goto out;
  296. for (i = 0; i < len; i++)
  297. regmap_write(lt9611->regmap, 0x8474 + i, buf[i]);
  298. iframes |= 0x20;
  299. out:
  300. regmap_write(lt9611->regmap, 0x843d, iframes); /* UD1 infoframe */
  301. }
  302. static void lt9611_hdmi_tx_digital(struct lt9611 *lt9611, bool is_hdmi)
  303. {
  304. if (is_hdmi)
  305. regmap_write(lt9611->regmap, 0x82d6, 0x8c);
  306. else
  307. regmap_write(lt9611->regmap, 0x82d6, 0x0c);
  308. regmap_write(lt9611->regmap, 0x82d7, 0x04);
  309. }
  310. static void lt9611_hdmi_tx_phy(struct lt9611 *lt9611)
  311. {
  312. struct reg_sequence reg_cfg[] = {
  313. { 0x8130, 0x6a },
  314. { 0x8131, 0x44 }, /* HDMI DC mode */
  315. { 0x8132, 0x4a },
  316. { 0x8133, 0x0b },
  317. { 0x8134, 0x00 },
  318. { 0x8135, 0x00 },
  319. { 0x8136, 0x00 },
  320. { 0x8137, 0x44 },
  321. { 0x813f, 0x0f },
  322. { 0x8140, 0xa0 },
  323. { 0x8141, 0xa0 },
  324. { 0x8142, 0xa0 },
  325. { 0x8143, 0xa0 },
  326. { 0x8144, 0x0a },
  327. };
  328. /* HDMI AC mode */
  329. if (lt9611->ac_mode)
  330. reg_cfg[2].def = 0x73;
  331. regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
  332. }
  333. static irqreturn_t lt9611_irq_thread_handler(int irq, void *dev_id)
  334. {
  335. struct lt9611 *lt9611 = dev_id;
  336. unsigned int irq_flag0 = 0;
  337. unsigned int irq_flag3 = 0;
  338. regmap_read(lt9611->regmap, 0x820f, &irq_flag3);
  339. regmap_read(lt9611->regmap, 0x820c, &irq_flag0);
  340. /* hpd changed low */
  341. if (irq_flag3 & 0x80) {
  342. dev_info(lt9611->dev, "hdmi cable disconnected\n");
  343. regmap_write(lt9611->regmap, 0x8207, 0xbf);
  344. regmap_write(lt9611->regmap, 0x8207, 0x3f);
  345. }
  346. /* hpd changed high */
  347. if (irq_flag3 & 0x40) {
  348. dev_info(lt9611->dev, "hdmi cable connected\n");
  349. regmap_write(lt9611->regmap, 0x8207, 0x7f);
  350. regmap_write(lt9611->regmap, 0x8207, 0x3f);
  351. }
  352. if (irq_flag3 & 0xc0 && lt9611->bridge.dev)
  353. drm_kms_helper_hotplug_event(lt9611->bridge.dev);
  354. /* video input changed */
  355. if (irq_flag0 & 0x01) {
  356. dev_info(lt9611->dev, "video input changed\n");
  357. regmap_write(lt9611->regmap, 0x829e, 0xff);
  358. regmap_write(lt9611->regmap, 0x829e, 0xf7);
  359. regmap_write(lt9611->regmap, 0x8204, 0xff);
  360. regmap_write(lt9611->regmap, 0x8204, 0xfe);
  361. }
  362. return IRQ_HANDLED;
  363. }
  364. static void lt9611_enable_hpd_interrupts(struct lt9611 *lt9611)
  365. {
  366. unsigned int val;
  367. regmap_read(lt9611->regmap, 0x8203, &val);
  368. val &= ~0xc0;
  369. regmap_write(lt9611->regmap, 0x8203, val);
  370. regmap_write(lt9611->regmap, 0x8207, 0xff); /* clear */
  371. regmap_write(lt9611->regmap, 0x8207, 0x3f);
  372. }
  373. static void lt9611_sleep_setup(struct lt9611 *lt9611)
  374. {
  375. const struct reg_sequence sleep_setup[] = {
  376. { 0x8024, 0x76 },
  377. { 0x8023, 0x01 },
  378. { 0x8157, 0x03 }, /* set addr pin as output */
  379. { 0x8149, 0x0b },
  380. { 0x8102, 0x48 }, /* MIPI Rx power down */
  381. { 0x8123, 0x80 },
  382. { 0x8130, 0x00 },
  383. { 0x8011, 0x0a },
  384. };
  385. regmap_multi_reg_write(lt9611->regmap,
  386. sleep_setup, ARRAY_SIZE(sleep_setup));
  387. lt9611->sleep = true;
  388. }
  389. static int lt9611_power_on(struct lt9611 *lt9611)
  390. {
  391. int ret;
  392. const struct reg_sequence seq[] = {
  393. /* LT9611_System_Init */
  394. { 0x8101, 0x18 }, /* sel xtal clock */
  395. /* timer for frequency meter */
  396. { 0x821b, 0x69 }, /* timer 2 */
  397. { 0x821c, 0x78 },
  398. { 0x82cb, 0x69 }, /* timer 1 */
  399. { 0x82cc, 0x78 },
  400. /* irq init */
  401. { 0x8251, 0x01 },
  402. { 0x8258, 0x0a }, /* hpd irq */
  403. { 0x8259, 0x80 }, /* hpd debounce width */
  404. { 0x829e, 0xf7 }, /* video check irq */
  405. /* power consumption for work */
  406. { 0x8004, 0xf0 },
  407. { 0x8006, 0xf0 },
  408. { 0x800a, 0x80 },
  409. { 0x800b, 0x40 },
  410. { 0x800d, 0xef },
  411. { 0x8011, 0xfa },
  412. };
  413. if (lt9611->power_on)
  414. return 0;
  415. ret = regmap_multi_reg_write(lt9611->regmap, seq, ARRAY_SIZE(seq));
  416. if (!ret)
  417. lt9611->power_on = true;
  418. return ret;
  419. }
  420. static int lt9611_power_off(struct lt9611 *lt9611)
  421. {
  422. int ret;
  423. ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
  424. if (!ret)
  425. lt9611->power_on = false;
  426. return ret;
  427. }
  428. static void lt9611_reset(struct lt9611 *lt9611)
  429. {
  430. gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
  431. msleep(20);
  432. gpiod_set_value_cansleep(lt9611->reset_gpio, 0);
  433. msleep(20);
  434. gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
  435. msleep(100);
  436. }
  437. static void lt9611_assert_5v(struct lt9611 *lt9611)
  438. {
  439. if (!lt9611->enable_gpio)
  440. return;
  441. gpiod_set_value_cansleep(lt9611->enable_gpio, 1);
  442. msleep(20);
  443. }
  444. static int lt9611_regulator_init(struct lt9611 *lt9611)
  445. {
  446. int ret;
  447. lt9611->supplies[0].supply = "vdd";
  448. lt9611->supplies[1].supply = "vcc";
  449. ret = devm_regulator_bulk_get(lt9611->dev, 2, lt9611->supplies);
  450. if (ret < 0)
  451. return ret;
  452. return regulator_set_load(lt9611->supplies[0].consumer, 300000);
  453. }
  454. static int lt9611_regulator_enable(struct lt9611 *lt9611)
  455. {
  456. int ret;
  457. ret = regulator_enable(lt9611->supplies[0].consumer);
  458. if (ret < 0)
  459. return ret;
  460. usleep_range(1000, 10000);
  461. ret = regulator_enable(lt9611->supplies[1].consumer);
  462. if (ret < 0) {
  463. regulator_disable(lt9611->supplies[0].consumer);
  464. return ret;
  465. }
  466. return 0;
  467. }
  468. static enum drm_connector_status lt9611_bridge_detect(struct drm_bridge *bridge)
  469. {
  470. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  471. unsigned int reg_val = 0;
  472. int connected = 0;
  473. regmap_read(lt9611->regmap, 0x825e, &reg_val);
  474. connected = (reg_val & (BIT(2) | BIT(0)));
  475. lt9611->status = connected ? connector_status_connected :
  476. connector_status_disconnected;
  477. return lt9611->status;
  478. }
  479. static int lt9611_read_edid(struct lt9611 *lt9611)
  480. {
  481. unsigned int temp;
  482. int ret = 0;
  483. int i, j;
  484. /* memset to clear old buffer, if any */
  485. memset(lt9611->edid_buf, 0, sizeof(lt9611->edid_buf));
  486. regmap_write(lt9611->regmap, 0x8503, 0xc9);
  487. /* 0xA0 is EDID device address */
  488. regmap_write(lt9611->regmap, 0x8504, 0xa0);
  489. /* 0x00 is EDID offset address */
  490. regmap_write(lt9611->regmap, 0x8505, 0x00);
  491. /* length for read */
  492. regmap_write(lt9611->regmap, 0x8506, EDID_LEN);
  493. regmap_write(lt9611->regmap, 0x8514, 0x7f);
  494. for (i = 0; i < EDID_LOOP; i++) {
  495. /* offset address */
  496. regmap_write(lt9611->regmap, 0x8505, i * EDID_LEN);
  497. regmap_write(lt9611->regmap, 0x8507, 0x36);
  498. regmap_write(lt9611->regmap, 0x8507, 0x31);
  499. regmap_write(lt9611->regmap, 0x8507, 0x37);
  500. usleep_range(5000, 10000);
  501. regmap_read(lt9611->regmap, 0x8540, &temp);
  502. if (temp & KEY_DDC_ACCS_DONE) {
  503. for (j = 0; j < EDID_LEN; j++) {
  504. regmap_read(lt9611->regmap, 0x8583, &temp);
  505. lt9611->edid_buf[i * EDID_LEN + j] = temp;
  506. }
  507. } else if (temp & DDC_NO_ACK) { /* DDC No Ack or Abitration lost */
  508. dev_err(lt9611->dev, "read edid failed: no ack\n");
  509. ret = -EIO;
  510. goto end;
  511. } else {
  512. dev_err(lt9611->dev, "read edid failed: access not done\n");
  513. ret = -EIO;
  514. goto end;
  515. }
  516. }
  517. end:
  518. regmap_write(lt9611->regmap, 0x8507, 0x1f);
  519. return ret;
  520. }
  521. static int
  522. lt9611_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
  523. {
  524. struct lt9611 *lt9611 = data;
  525. int ret;
  526. if (len > 128)
  527. return -EINVAL;
  528. /* supports up to 1 extension block */
  529. /* TODO: add support for more extension blocks */
  530. if (block > 1)
  531. return -EINVAL;
  532. if (block == 0) {
  533. ret = lt9611_read_edid(lt9611);
  534. if (ret) {
  535. dev_err(lt9611->dev, "edid read failed\n");
  536. return ret;
  537. }
  538. }
  539. block %= 2;
  540. memcpy(buf, lt9611->edid_buf + (block * 128), len);
  541. return 0;
  542. }
  543. /* bridge funcs */
  544. static void
  545. lt9611_bridge_atomic_enable(struct drm_bridge *bridge,
  546. struct drm_bridge_state *old_bridge_state)
  547. {
  548. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  549. struct drm_atomic_state *state = old_bridge_state->base.state;
  550. struct drm_connector *connector;
  551. struct drm_connector_state *conn_state;
  552. struct drm_crtc_state *crtc_state;
  553. struct drm_display_mode *mode;
  554. unsigned int postdiv;
  555. connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
  556. if (WARN_ON(!connector))
  557. return;
  558. conn_state = drm_atomic_get_new_connector_state(state, connector);
  559. if (WARN_ON(!conn_state))
  560. return;
  561. crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
  562. if (WARN_ON(!crtc_state))
  563. return;
  564. mode = &crtc_state->adjusted_mode;
  565. lt9611_mipi_input_digital(lt9611, mode);
  566. lt9611_pll_setup(lt9611, mode, &postdiv);
  567. lt9611_mipi_video_setup(lt9611, mode);
  568. lt9611_pcr_setup(lt9611, mode, postdiv);
  569. if (lt9611_power_on(lt9611)) {
  570. dev_err(lt9611->dev, "power on failed\n");
  571. return;
  572. }
  573. lt9611_mipi_input_analog(lt9611);
  574. lt9611_hdmi_set_infoframes(lt9611, connector, mode);
  575. lt9611_hdmi_tx_digital(lt9611, connector->display_info.is_hdmi);
  576. lt9611_hdmi_tx_phy(lt9611);
  577. msleep(500);
  578. lt9611_video_check(lt9611);
  579. /* Enable HDMI output */
  580. regmap_write(lt9611->regmap, 0x8130, 0xea);
  581. }
  582. static void
  583. lt9611_bridge_atomic_disable(struct drm_bridge *bridge,
  584. struct drm_bridge_state *old_bridge_state)
  585. {
  586. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  587. int ret;
  588. /* Disable HDMI output */
  589. ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
  590. if (ret) {
  591. dev_err(lt9611->dev, "video on failed\n");
  592. return;
  593. }
  594. if (lt9611_power_off(lt9611)) {
  595. dev_err(lt9611->dev, "power on failed\n");
  596. return;
  597. }
  598. }
  599. static struct mipi_dsi_device *lt9611_attach_dsi(struct lt9611 *lt9611,
  600. struct device_node *dsi_node)
  601. {
  602. const struct mipi_dsi_device_info info = { "lt9611", 0, lt9611->dev->of_node};
  603. struct mipi_dsi_device *dsi;
  604. struct mipi_dsi_host *host;
  605. struct device *dev = lt9611->dev;
  606. int ret;
  607. host = of_find_mipi_dsi_host_by_node(dsi_node);
  608. if (!host)
  609. return ERR_PTR(dev_err_probe(lt9611->dev, -EPROBE_DEFER, "failed to find dsi host\n"));
  610. dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
  611. if (IS_ERR(dsi)) {
  612. dev_err(lt9611->dev, "failed to create dsi device\n");
  613. return dsi;
  614. }
  615. dsi->lanes = 4;
  616. dsi->format = MIPI_DSI_FMT_RGB888;
  617. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  618. MIPI_DSI_MODE_VIDEO_HSE;
  619. ret = devm_mipi_dsi_attach(dev, dsi);
  620. if (ret < 0) {
  621. dev_err(dev, "failed to attach dsi to host\n");
  622. return ERR_PTR(ret);
  623. }
  624. return dsi;
  625. }
  626. static int lt9611_bridge_attach(struct drm_bridge *bridge,
  627. enum drm_bridge_attach_flags flags)
  628. {
  629. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  630. return drm_bridge_attach(bridge->encoder, lt9611->next_bridge,
  631. bridge, flags);
  632. }
  633. static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge,
  634. const struct drm_display_info *info,
  635. const struct drm_display_mode *mode)
  636. {
  637. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  638. if (mode->hdisplay > 3840)
  639. return MODE_BAD_HVALUE;
  640. if (mode->vdisplay > 2160)
  641. return MODE_BAD_VVALUE;
  642. if (mode->hdisplay == 3840 &&
  643. mode->vdisplay == 2160 &&
  644. drm_mode_vrefresh(mode) > 30)
  645. return MODE_CLOCK_HIGH;
  646. if (mode->hdisplay > 2000 && !lt9611->dsi1_node)
  647. return MODE_PANEL;
  648. else
  649. return MODE_OK;
  650. }
  651. static void lt9611_bridge_atomic_pre_enable(struct drm_bridge *bridge,
  652. struct drm_bridge_state *old_bridge_state)
  653. {
  654. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  655. static const struct reg_sequence reg_cfg[] = {
  656. { 0x8102, 0x12 },
  657. { 0x8123, 0x40 },
  658. { 0x8130, 0xea },
  659. { 0x8011, 0xfa },
  660. };
  661. if (!lt9611->sleep)
  662. return;
  663. regmap_multi_reg_write(lt9611->regmap,
  664. reg_cfg, ARRAY_SIZE(reg_cfg));
  665. lt9611->sleep = false;
  666. }
  667. static void
  668. lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge,
  669. struct drm_bridge_state *old_bridge_state)
  670. {
  671. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  672. lt9611_sleep_setup(lt9611);
  673. }
  674. static const struct drm_edid *lt9611_bridge_edid_read(struct drm_bridge *bridge,
  675. struct drm_connector *connector)
  676. {
  677. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  678. lt9611_power_on(lt9611);
  679. return drm_edid_read_custom(connector, lt9611_get_edid_block, lt9611);
  680. }
  681. static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge)
  682. {
  683. struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
  684. lt9611_enable_hpd_interrupts(lt9611);
  685. }
  686. #define MAX_INPUT_SEL_FORMATS 1
  687. static u32 *
  688. lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
  689. struct drm_bridge_state *bridge_state,
  690. struct drm_crtc_state *crtc_state,
  691. struct drm_connector_state *conn_state,
  692. u32 output_fmt,
  693. unsigned int *num_input_fmts)
  694. {
  695. u32 *input_fmts;
  696. *num_input_fmts = 0;
  697. input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
  698. GFP_KERNEL);
  699. if (!input_fmts)
  700. return NULL;
  701. /* This is the DSI-end bus format */
  702. input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
  703. *num_input_fmts = 1;
  704. return input_fmts;
  705. }
  706. static const struct drm_bridge_funcs lt9611_bridge_funcs = {
  707. .attach = lt9611_bridge_attach,
  708. .mode_valid = lt9611_bridge_mode_valid,
  709. .detect = lt9611_bridge_detect,
  710. .edid_read = lt9611_bridge_edid_read,
  711. .hpd_enable = lt9611_bridge_hpd_enable,
  712. .atomic_pre_enable = lt9611_bridge_atomic_pre_enable,
  713. .atomic_enable = lt9611_bridge_atomic_enable,
  714. .atomic_disable = lt9611_bridge_atomic_disable,
  715. .atomic_post_disable = lt9611_bridge_atomic_post_disable,
  716. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  717. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  718. .atomic_reset = drm_atomic_helper_bridge_reset,
  719. .atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts,
  720. };
  721. static int lt9611_parse_dt(struct device *dev,
  722. struct lt9611 *lt9611)
  723. {
  724. lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
  725. if (!lt9611->dsi0_node) {
  726. dev_err(lt9611->dev, "failed to get remote node for primary dsi\n");
  727. return -ENODEV;
  728. }
  729. lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
  730. lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode");
  731. return drm_of_find_panel_or_bridge(dev->of_node, 2, -1, NULL, &lt9611->next_bridge);
  732. }
  733. static int lt9611_gpio_init(struct lt9611 *lt9611)
  734. {
  735. struct device *dev = lt9611->dev;
  736. lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  737. if (IS_ERR(lt9611->reset_gpio)) {
  738. dev_err(dev, "failed to acquire reset gpio\n");
  739. return PTR_ERR(lt9611->reset_gpio);
  740. }
  741. lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable",
  742. GPIOD_OUT_LOW);
  743. if (IS_ERR(lt9611->enable_gpio)) {
  744. dev_err(dev, "failed to acquire enable gpio\n");
  745. return PTR_ERR(lt9611->enable_gpio);
  746. }
  747. return 0;
  748. }
  749. static int lt9611_read_device_rev(struct lt9611 *lt9611)
  750. {
  751. unsigned int rev;
  752. int ret;
  753. regmap_write(lt9611->regmap, 0x80ee, 0x01);
  754. ret = regmap_read(lt9611->regmap, 0x8002, &rev);
  755. if (ret)
  756. dev_err(lt9611->dev, "failed to read revision: %d\n", ret);
  757. else
  758. dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev);
  759. return ret;
  760. }
  761. static int lt9611_hdmi_hw_params(struct device *dev, void *data,
  762. struct hdmi_codec_daifmt *fmt,
  763. struct hdmi_codec_params *hparms)
  764. {
  765. struct lt9611 *lt9611 = data;
  766. if (hparms->sample_rate == 48000)
  767. regmap_write(lt9611->regmap, 0x840f, 0x2b);
  768. else if (hparms->sample_rate == 96000)
  769. regmap_write(lt9611->regmap, 0x840f, 0xab);
  770. else
  771. return -EINVAL;
  772. regmap_write(lt9611->regmap, 0x8435, 0x00);
  773. regmap_write(lt9611->regmap, 0x8436, 0x18);
  774. regmap_write(lt9611->regmap, 0x8437, 0x00);
  775. return 0;
  776. }
  777. static int lt9611_audio_startup(struct device *dev, void *data)
  778. {
  779. struct lt9611 *lt9611 = data;
  780. regmap_write(lt9611->regmap, 0x82d6, 0x8c);
  781. regmap_write(lt9611->regmap, 0x82d7, 0x04);
  782. regmap_write(lt9611->regmap, 0x8406, 0x08);
  783. regmap_write(lt9611->regmap, 0x8407, 0x10);
  784. regmap_write(lt9611->regmap, 0x8434, 0xd5);
  785. return 0;
  786. }
  787. static void lt9611_audio_shutdown(struct device *dev, void *data)
  788. {
  789. struct lt9611 *lt9611 = data;
  790. regmap_write(lt9611->regmap, 0x8406, 0x00);
  791. regmap_write(lt9611->regmap, 0x8407, 0x00);
  792. }
  793. static int lt9611_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
  794. struct device_node *endpoint)
  795. {
  796. struct of_endpoint of_ep;
  797. int ret;
  798. ret = of_graph_parse_endpoint(endpoint, &of_ep);
  799. if (ret < 0)
  800. return ret;
  801. /*
  802. * HDMI sound should be located as reg = <2>
  803. * Then, it is sound port 0
  804. */
  805. if (of_ep.port == 2)
  806. return 0;
  807. return -EINVAL;
  808. }
  809. static const struct hdmi_codec_ops lt9611_codec_ops = {
  810. .hw_params = lt9611_hdmi_hw_params,
  811. .audio_shutdown = lt9611_audio_shutdown,
  812. .audio_startup = lt9611_audio_startup,
  813. .get_dai_id = lt9611_hdmi_i2s_get_dai_id,
  814. };
  815. static struct hdmi_codec_pdata codec_data = {
  816. .ops = &lt9611_codec_ops,
  817. .max_i2s_channels = 8,
  818. .i2s = 1,
  819. };
  820. static int lt9611_audio_init(struct device *dev, struct lt9611 *lt9611)
  821. {
  822. codec_data.data = lt9611;
  823. lt9611->audio_pdev =
  824. platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
  825. PLATFORM_DEVID_AUTO,
  826. &codec_data, sizeof(codec_data));
  827. return PTR_ERR_OR_ZERO(lt9611->audio_pdev);
  828. }
  829. static void lt9611_audio_exit(struct lt9611 *lt9611)
  830. {
  831. if (lt9611->audio_pdev) {
  832. platform_device_unregister(lt9611->audio_pdev);
  833. lt9611->audio_pdev = NULL;
  834. }
  835. }
  836. static int lt9611_probe(struct i2c_client *client)
  837. {
  838. struct lt9611 *lt9611;
  839. struct device *dev = &client->dev;
  840. int ret;
  841. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  842. dev_err(dev, "device doesn't support I2C\n");
  843. return -ENODEV;
  844. }
  845. lt9611 = devm_kzalloc(dev, sizeof(*lt9611), GFP_KERNEL);
  846. if (!lt9611)
  847. return -ENOMEM;
  848. lt9611->dev = dev;
  849. lt9611->client = client;
  850. lt9611->sleep = false;
  851. lt9611->regmap = devm_regmap_init_i2c(client, &lt9611_regmap_config);
  852. if (IS_ERR(lt9611->regmap)) {
  853. dev_err(lt9611->dev, "regmap i2c init failed\n");
  854. return PTR_ERR(lt9611->regmap);
  855. }
  856. ret = lt9611_parse_dt(dev, lt9611);
  857. if (ret) {
  858. dev_err(dev, "failed to parse device tree\n");
  859. return ret;
  860. }
  861. ret = lt9611_gpio_init(lt9611);
  862. if (ret < 0)
  863. goto err_of_put;
  864. ret = lt9611_regulator_init(lt9611);
  865. if (ret < 0)
  866. goto err_of_put;
  867. lt9611_assert_5v(lt9611);
  868. ret = lt9611_regulator_enable(lt9611);
  869. if (ret)
  870. goto err_of_put;
  871. lt9611_reset(lt9611);
  872. ret = lt9611_read_device_rev(lt9611);
  873. if (ret) {
  874. dev_err(dev, "failed to read chip rev\n");
  875. goto err_disable_regulators;
  876. }
  877. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  878. lt9611_irq_thread_handler,
  879. IRQF_ONESHOT, "lt9611", lt9611);
  880. if (ret) {
  881. dev_err(dev, "failed to request irq\n");
  882. goto err_disable_regulators;
  883. }
  884. i2c_set_clientdata(client, lt9611);
  885. lt9611->bridge.funcs = &lt9611_bridge_funcs;
  886. lt9611->bridge.of_node = client->dev.of_node;
  887. lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
  888. DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES;
  889. lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
  890. drm_bridge_add(&lt9611->bridge);
  891. /* Attach primary DSI */
  892. lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node);
  893. if (IS_ERR(lt9611->dsi0)) {
  894. ret = PTR_ERR(lt9611->dsi0);
  895. goto err_remove_bridge;
  896. }
  897. /* Attach secondary DSI, if specified */
  898. if (lt9611->dsi1_node) {
  899. lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node);
  900. if (IS_ERR(lt9611->dsi1)) {
  901. ret = PTR_ERR(lt9611->dsi1);
  902. goto err_remove_bridge;
  903. }
  904. }
  905. lt9611_enable_hpd_interrupts(lt9611);
  906. ret = lt9611_audio_init(dev, lt9611);
  907. if (ret)
  908. goto err_remove_bridge;
  909. return 0;
  910. err_remove_bridge:
  911. drm_bridge_remove(&lt9611->bridge);
  912. err_disable_regulators:
  913. regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
  914. err_of_put:
  915. of_node_put(lt9611->dsi1_node);
  916. of_node_put(lt9611->dsi0_node);
  917. return ret;
  918. }
  919. static void lt9611_remove(struct i2c_client *client)
  920. {
  921. struct lt9611 *lt9611 = i2c_get_clientdata(client);
  922. disable_irq(client->irq);
  923. lt9611_audio_exit(lt9611);
  924. drm_bridge_remove(&lt9611->bridge);
  925. regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
  926. of_node_put(lt9611->dsi1_node);
  927. of_node_put(lt9611->dsi0_node);
  928. }
  929. static struct i2c_device_id lt9611_id[] = {
  930. { "lontium,lt9611", 0 },
  931. {}
  932. };
  933. MODULE_DEVICE_TABLE(i2c, lt9611_id);
  934. static const struct of_device_id lt9611_match_table[] = {
  935. { .compatible = "lontium,lt9611" },
  936. { }
  937. };
  938. MODULE_DEVICE_TABLE(of, lt9611_match_table);
  939. static struct i2c_driver lt9611_driver = {
  940. .driver = {
  941. .name = "lt9611",
  942. .of_match_table = lt9611_match_table,
  943. },
  944. .probe = lt9611_probe,
  945. .remove = lt9611_remove,
  946. .id_table = lt9611_id,
  947. };
  948. module_i2c_driver(lt9611_driver);
  949. MODULE_DESCRIPTION("Lontium LT9611 DSI/HDMI bridge driver");
  950. MODULE_LICENSE("GPL v2");