mi0283qt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * DRM driver for Multi-Inno MI0283QT panels
  3. *
  4. * Copyright 2016 Noralf Trønnes
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/backlight.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/module.h>
  15. #include <linux/property.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/spi/spi.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include <drm/drm_modeset_helper.h>
  20. #include <drm/tinydrm/mipi-dbi.h>
  21. #include <drm/tinydrm/tinydrm-helpers.h>
  22. #include <video/mipi_display.h>
  23. #define ILI9341_FRMCTR1 0xb1
  24. #define ILI9341_DISCTRL 0xb6
  25. #define ILI9341_ETMOD 0xb7
  26. #define ILI9341_PWCTRL1 0xc0
  27. #define ILI9341_PWCTRL2 0xc1
  28. #define ILI9341_VMCTRL1 0xc5
  29. #define ILI9341_VMCTRL2 0xc7
  30. #define ILI9341_PWCTRLA 0xcb
  31. #define ILI9341_PWCTRLB 0xcf
  32. #define ILI9341_PGAMCTRL 0xe0
  33. #define ILI9341_NGAMCTRL 0xe1
  34. #define ILI9341_DTCTRLA 0xe8
  35. #define ILI9341_DTCTRLB 0xea
  36. #define ILI9341_PWRSEQ 0xed
  37. #define ILI9341_EN3GAM 0xf2
  38. #define ILI9341_PUMPCTRL 0xf7
  39. #define ILI9341_MADCTL_BGR BIT(3)
  40. #define ILI9341_MADCTL_MV BIT(5)
  41. #define ILI9341_MADCTL_MX BIT(6)
  42. #define ILI9341_MADCTL_MY BIT(7)
  43. static void mi0283qt_enable(struct drm_simple_display_pipe *pipe,
  44. struct drm_crtc_state *crtc_state)
  45. {
  46. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  47. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  48. u8 addr_mode;
  49. int ret;
  50. DRM_DEBUG_KMS("\n");
  51. ret = mipi_dbi_poweron_conditional_reset(mipi);
  52. if (ret < 0)
  53. return;
  54. if (ret == 1)
  55. goto out_enable;
  56. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
  57. mipi_dbi_command(mipi, ILI9341_PWCTRLB, 0x00, 0x83, 0x30);
  58. mipi_dbi_command(mipi, ILI9341_PWRSEQ, 0x64, 0x03, 0x12, 0x81);
  59. mipi_dbi_command(mipi, ILI9341_DTCTRLA, 0x85, 0x01, 0x79);
  60. mipi_dbi_command(mipi, ILI9341_PWCTRLA, 0x39, 0x2c, 0x00, 0x34, 0x02);
  61. mipi_dbi_command(mipi, ILI9341_PUMPCTRL, 0x20);
  62. mipi_dbi_command(mipi, ILI9341_DTCTRLB, 0x00, 0x00);
  63. /* Power Control */
  64. mipi_dbi_command(mipi, ILI9341_PWCTRL1, 0x26);
  65. mipi_dbi_command(mipi, ILI9341_PWCTRL2, 0x11);
  66. /* VCOM */
  67. mipi_dbi_command(mipi, ILI9341_VMCTRL1, 0x35, 0x3e);
  68. mipi_dbi_command(mipi, ILI9341_VMCTRL2, 0xbe);
  69. /* Memory Access Control */
  70. mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
  71. switch (mipi->rotation) {
  72. default:
  73. addr_mode = ILI9341_MADCTL_MV | ILI9341_MADCTL_MY |
  74. ILI9341_MADCTL_MX;
  75. break;
  76. case 90:
  77. addr_mode = ILI9341_MADCTL_MY;
  78. break;
  79. case 180:
  80. addr_mode = ILI9341_MADCTL_MV;
  81. break;
  82. case 270:
  83. addr_mode = ILI9341_MADCTL_MX;
  84. break;
  85. }
  86. addr_mode |= ILI9341_MADCTL_BGR;
  87. mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  88. /* Frame Rate */
  89. mipi_dbi_command(mipi, ILI9341_FRMCTR1, 0x00, 0x1b);
  90. /* Gamma */
  91. mipi_dbi_command(mipi, ILI9341_EN3GAM, 0x08);
  92. mipi_dbi_command(mipi, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
  93. mipi_dbi_command(mipi, ILI9341_PGAMCTRL,
  94. 0x1f, 0x1a, 0x18, 0x0a, 0x0f, 0x06, 0x45, 0x87,
  95. 0x32, 0x0a, 0x07, 0x02, 0x07, 0x05, 0x00);
  96. mipi_dbi_command(mipi, ILI9341_NGAMCTRL,
  97. 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3a, 0x78,
  98. 0x4d, 0x05, 0x18, 0x0d, 0x38, 0x3a, 0x1f);
  99. /* DDRAM */
  100. mipi_dbi_command(mipi, ILI9341_ETMOD, 0x07);
  101. /* Display */
  102. mipi_dbi_command(mipi, ILI9341_DISCTRL, 0x0a, 0x82, 0x27, 0x00);
  103. mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
  104. msleep(100);
  105. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
  106. msleep(100);
  107. out_enable:
  108. mipi_dbi_enable_flush(mipi);
  109. }
  110. static const struct drm_simple_display_pipe_funcs mi0283qt_pipe_funcs = {
  111. .enable = mi0283qt_enable,
  112. .disable = mipi_dbi_pipe_disable,
  113. .update = tinydrm_display_pipe_update,
  114. .prepare_fb = tinydrm_display_pipe_prepare_fb,
  115. };
  116. static const struct drm_display_mode mi0283qt_mode = {
  117. TINYDRM_MODE(320, 240, 58, 43),
  118. };
  119. DEFINE_DRM_GEM_CMA_FOPS(mi0283qt_fops);
  120. static struct drm_driver mi0283qt_driver = {
  121. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  122. DRIVER_ATOMIC,
  123. .fops = &mi0283qt_fops,
  124. TINYDRM_GEM_DRIVER_OPS,
  125. .lastclose = drm_fb_helper_lastclose,
  126. .debugfs_init = mipi_dbi_debugfs_init,
  127. .name = "mi0283qt",
  128. .desc = "Multi-Inno MI0283QT",
  129. .date = "20160614",
  130. .major = 1,
  131. .minor = 0,
  132. };
  133. static const struct of_device_id mi0283qt_of_match[] = {
  134. { .compatible = "multi-inno,mi0283qt" },
  135. {},
  136. };
  137. MODULE_DEVICE_TABLE(of, mi0283qt_of_match);
  138. static const struct spi_device_id mi0283qt_id[] = {
  139. { "mi0283qt", 0 },
  140. { },
  141. };
  142. MODULE_DEVICE_TABLE(spi, mi0283qt_id);
  143. static int mi0283qt_probe(struct spi_device *spi)
  144. {
  145. struct device *dev = &spi->dev;
  146. struct mipi_dbi *mipi;
  147. struct gpio_desc *dc;
  148. u32 rotation = 0;
  149. int ret;
  150. mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
  151. if (!mipi)
  152. return -ENOMEM;
  153. mipi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  154. if (IS_ERR(mipi->reset)) {
  155. DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
  156. return PTR_ERR(mipi->reset);
  157. }
  158. dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
  159. if (IS_ERR(dc)) {
  160. DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
  161. return PTR_ERR(dc);
  162. }
  163. mipi->regulator = devm_regulator_get(dev, "power");
  164. if (IS_ERR(mipi->regulator))
  165. return PTR_ERR(mipi->regulator);
  166. mipi->backlight = devm_of_find_backlight(dev);
  167. if (IS_ERR(mipi->backlight))
  168. return PTR_ERR(mipi->backlight);
  169. device_property_read_u32(dev, "rotation", &rotation);
  170. ret = mipi_dbi_spi_init(spi, mipi, dc);
  171. if (ret)
  172. return ret;
  173. ret = mipi_dbi_init(&spi->dev, mipi, &mi0283qt_pipe_funcs,
  174. &mi0283qt_driver, &mi0283qt_mode, rotation);
  175. if (ret)
  176. return ret;
  177. spi_set_drvdata(spi, mipi);
  178. return devm_tinydrm_register(&mipi->tinydrm);
  179. }
  180. static void mi0283qt_shutdown(struct spi_device *spi)
  181. {
  182. struct mipi_dbi *mipi = spi_get_drvdata(spi);
  183. tinydrm_shutdown(&mipi->tinydrm);
  184. }
  185. static int __maybe_unused mi0283qt_pm_suspend(struct device *dev)
  186. {
  187. struct mipi_dbi *mipi = dev_get_drvdata(dev);
  188. return drm_mode_config_helper_suspend(mipi->tinydrm.drm);
  189. }
  190. static int __maybe_unused mi0283qt_pm_resume(struct device *dev)
  191. {
  192. struct mipi_dbi *mipi = dev_get_drvdata(dev);
  193. drm_mode_config_helper_resume(mipi->tinydrm.drm);
  194. return 0;
  195. }
  196. static const struct dev_pm_ops mi0283qt_pm_ops = {
  197. SET_SYSTEM_SLEEP_PM_OPS(mi0283qt_pm_suspend, mi0283qt_pm_resume)
  198. };
  199. static struct spi_driver mi0283qt_spi_driver = {
  200. .driver = {
  201. .name = "mi0283qt",
  202. .owner = THIS_MODULE,
  203. .of_match_table = mi0283qt_of_match,
  204. .pm = &mi0283qt_pm_ops,
  205. },
  206. .id_table = mi0283qt_id,
  207. .probe = mi0283qt_probe,
  208. .shutdown = mi0283qt_shutdown,
  209. };
  210. module_spi_driver(mi0283qt_spi_driver);
  211. MODULE_DESCRIPTION("Multi-Inno MI0283QT DRM driver");
  212. MODULE_AUTHOR("Noralf Trønnes");
  213. MODULE_LICENSE("GPL");