adv7533.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/of_graph.h>
  14. #include "adv7511.h"
  15. static const struct reg_sequence adv7533_fixed_registers[] = {
  16. { 0x16, 0x20 },
  17. { 0x9a, 0xe0 },
  18. { 0xba, 0x70 },
  19. { 0xde, 0x82 },
  20. { 0xe4, 0x40 },
  21. { 0xe5, 0x80 },
  22. };
  23. static const struct reg_sequence adv7533_cec_fixed_registers[] = {
  24. { 0x15, 0xd0 },
  25. { 0x17, 0xd0 },
  26. { 0x24, 0x20 },
  27. { 0x57, 0x11 },
  28. { 0x05, 0xc8 },
  29. };
  30. static void adv7511_dsi_config_timing_gen(struct adv7511 *adv)
  31. {
  32. struct mipi_dsi_device *dsi = adv->dsi;
  33. struct drm_display_mode *mode = &adv->curr_mode;
  34. unsigned int hsw, hfp, hbp, vsw, vfp, vbp;
  35. u8 clock_div_by_lanes[] = { 6, 4, 3 }; /* 2, 3, 4 lanes */
  36. hsw = mode->hsync_end - mode->hsync_start;
  37. hfp = mode->hsync_start - mode->hdisplay;
  38. hbp = mode->htotal - mode->hsync_end;
  39. vsw = mode->vsync_end - mode->vsync_start;
  40. vfp = mode->vsync_start - mode->vdisplay;
  41. vbp = mode->vtotal - mode->vsync_end;
  42. /* set pixel clock divider mode */
  43. regmap_write(adv->regmap_cec, 0x16,
  44. clock_div_by_lanes[dsi->lanes - 2] << 3);
  45. /* horizontal porch params */
  46. regmap_write(adv->regmap_cec, 0x28, mode->htotal >> 4);
  47. regmap_write(adv->regmap_cec, 0x29, (mode->htotal << 4) & 0xff);
  48. regmap_write(adv->regmap_cec, 0x2a, hsw >> 4);
  49. regmap_write(adv->regmap_cec, 0x2b, (hsw << 4) & 0xff);
  50. regmap_write(adv->regmap_cec, 0x2c, hfp >> 4);
  51. regmap_write(adv->regmap_cec, 0x2d, (hfp << 4) & 0xff);
  52. regmap_write(adv->regmap_cec, 0x2e, hbp >> 4);
  53. regmap_write(adv->regmap_cec, 0x2f, (hbp << 4) & 0xff);
  54. /* vertical porch params */
  55. regmap_write(adv->regmap_cec, 0x30, mode->vtotal >> 4);
  56. regmap_write(adv->regmap_cec, 0x31, (mode->vtotal << 4) & 0xff);
  57. regmap_write(adv->regmap_cec, 0x32, vsw >> 4);
  58. regmap_write(adv->regmap_cec, 0x33, (vsw << 4) & 0xff);
  59. regmap_write(adv->regmap_cec, 0x34, vfp >> 4);
  60. regmap_write(adv->regmap_cec, 0x35, (vfp << 4) & 0xff);
  61. regmap_write(adv->regmap_cec, 0x36, vbp >> 4);
  62. regmap_write(adv->regmap_cec, 0x37, (vbp << 4) & 0xff);
  63. }
  64. void adv7533_dsi_power_on(struct adv7511 *adv)
  65. {
  66. struct mipi_dsi_device *dsi = adv->dsi;
  67. if (adv->use_timing_gen)
  68. adv7511_dsi_config_timing_gen(adv);
  69. /* set number of dsi lanes */
  70. regmap_write(adv->regmap_cec, 0x1c, dsi->lanes << 4);
  71. if (adv->use_timing_gen) {
  72. /* reset internal timing generator */
  73. regmap_write(adv->regmap_cec, 0x27, 0xcb);
  74. regmap_write(adv->regmap_cec, 0x27, 0x8b);
  75. regmap_write(adv->regmap_cec, 0x27, 0xcb);
  76. } else {
  77. /* disable internal timing generator */
  78. regmap_write(adv->regmap_cec, 0x27, 0x0b);
  79. }
  80. /* enable hdmi */
  81. regmap_write(adv->regmap_cec, 0x03, 0x89);
  82. /* disable test mode */
  83. regmap_write(adv->regmap_cec, 0x55, 0x00);
  84. regmap_register_patch(adv->regmap_cec, adv7533_cec_fixed_registers,
  85. ARRAY_SIZE(adv7533_cec_fixed_registers));
  86. }
  87. void adv7533_dsi_power_off(struct adv7511 *adv)
  88. {
  89. /* disable hdmi */
  90. regmap_write(adv->regmap_cec, 0x03, 0x0b);
  91. /* disable internal timing generator */
  92. regmap_write(adv->regmap_cec, 0x27, 0x0b);
  93. }
  94. void adv7533_mode_set(struct adv7511 *adv, struct drm_display_mode *mode)
  95. {
  96. struct mipi_dsi_device *dsi = adv->dsi;
  97. int lanes, ret;
  98. if (adv->num_dsi_lanes != 4)
  99. return;
  100. if (mode->clock > 80000)
  101. lanes = 4;
  102. else
  103. lanes = 3;
  104. if (lanes != dsi->lanes) {
  105. mipi_dsi_detach(dsi);
  106. dsi->lanes = lanes;
  107. ret = mipi_dsi_attach(dsi);
  108. if (ret)
  109. dev_err(&dsi->dev, "failed to change host lanes\n");
  110. }
  111. }
  112. int adv7533_patch_registers(struct adv7511 *adv)
  113. {
  114. return regmap_register_patch(adv->regmap,
  115. adv7533_fixed_registers,
  116. ARRAY_SIZE(adv7533_fixed_registers));
  117. }
  118. int adv7533_patch_cec_registers(struct adv7511 *adv)
  119. {
  120. return regmap_register_patch(adv->regmap_cec,
  121. adv7533_cec_fixed_registers,
  122. ARRAY_SIZE(adv7533_cec_fixed_registers));
  123. }
  124. int adv7533_attach_dsi(struct adv7511 *adv)
  125. {
  126. struct device *dev = &adv->i2c_main->dev;
  127. struct mipi_dsi_host *host;
  128. struct mipi_dsi_device *dsi;
  129. int ret = 0;
  130. const struct mipi_dsi_device_info info = { .type = "adv7533",
  131. .channel = 0,
  132. .node = NULL,
  133. };
  134. host = of_find_mipi_dsi_host_by_node(adv->host_node);
  135. if (!host) {
  136. dev_err(dev, "failed to find dsi host\n");
  137. return -EPROBE_DEFER;
  138. }
  139. dsi = mipi_dsi_device_register_full(host, &info);
  140. if (IS_ERR(dsi)) {
  141. dev_err(dev, "failed to create dsi device\n");
  142. ret = PTR_ERR(dsi);
  143. goto err_dsi_device;
  144. }
  145. adv->dsi = dsi;
  146. dsi->lanes = adv->num_dsi_lanes;
  147. dsi->format = MIPI_DSI_FMT_RGB888;
  148. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  149. MIPI_DSI_MODE_EOT_PACKET | MIPI_DSI_MODE_VIDEO_HSE;
  150. ret = mipi_dsi_attach(dsi);
  151. if (ret < 0) {
  152. dev_err(dev, "failed to attach dsi to host\n");
  153. goto err_dsi_attach;
  154. }
  155. return 0;
  156. err_dsi_attach:
  157. mipi_dsi_device_unregister(dsi);
  158. err_dsi_device:
  159. return ret;
  160. }
  161. void adv7533_detach_dsi(struct adv7511 *adv)
  162. {
  163. mipi_dsi_detach(adv->dsi);
  164. mipi_dsi_device_unregister(adv->dsi);
  165. }
  166. int adv7533_parse_dt(struct device_node *np, struct adv7511 *adv)
  167. {
  168. u32 num_lanes;
  169. of_property_read_u32(np, "adi,dsi-lanes", &num_lanes);
  170. if (num_lanes < 1 || num_lanes > 4)
  171. return -EINVAL;
  172. adv->num_dsi_lanes = num_lanes;
  173. adv->host_node = of_graph_get_remote_node(np, 0, 0);
  174. if (!adv->host_node)
  175. return -ENODEV;
  176. of_node_put(adv->host_node);
  177. adv->use_timing_gen = !of_property_read_bool(np,
  178. "adi,disable-timing-generator");
  179. /* TODO: Check if these need to be parsed by DT or not */
  180. adv->rgb = true;
  181. adv->embedded_sync = false;
  182. return 0;
  183. }