panel-samsung-db7430.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Panel driver for the Samsung LMS397KF04 480x800 DPI RGB panel.
  4. * According to the data sheet the display controller is called DB7430.
  5. * Found in the Samsung Galaxy Beam GT-I8350 mobile phone.
  6. * Linus Walleij <[email protected]>
  7. */
  8. #include <drm/drm_mipi_dbi.h>
  9. #include <drm/drm_modes.h>
  10. #include <drm/drm_panel.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/media-bus-format.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/spi/spi.h>
  20. #include <video/mipi_display.h>
  21. #define DB7430_ACCESS_PROT_OFF 0xb0
  22. #define DB7430_UNKNOWN_B4 0xb4
  23. #define DB7430_USER_SELECT 0xb5
  24. #define DB7430_UNKNOWN_B7 0xb7
  25. #define DB7430_UNKNOWN_B8 0xb8
  26. #define DB7430_PANEL_DRIVING 0xc0
  27. #define DB7430_SOURCE_CONTROL 0xc1
  28. #define DB7430_GATE_INTERFACE 0xc4
  29. #define DB7430_DISPLAY_H_TIMING 0xc5
  30. #define DB7430_RGB_SYNC_OPTION 0xc6
  31. #define DB7430_GAMMA_SET_RED 0xc8
  32. #define DB7430_GAMMA_SET_GREEN 0xc9
  33. #define DB7430_GAMMA_SET_BLUE 0xca
  34. #define DB7430_BIAS_CURRENT_CTRL 0xd1
  35. #define DB7430_DDV_CTRL 0xd2
  36. #define DB7430_GAMMA_CTRL_REF 0xd3
  37. #define DB7430_UNKNOWN_D4 0xd4
  38. #define DB7430_DCDC_CTRL 0xd5
  39. #define DB7430_VCL_CTRL 0xd6
  40. #define DB7430_UNKNOWN_F8 0xf8
  41. #define DB7430_UNKNOWN_FC 0xfc
  42. #define DATA_MASK 0x100
  43. /**
  44. * struct db7430 - state container for a panel controlled by the DB7430
  45. * controller
  46. */
  47. struct db7430 {
  48. /** @dev: the container device */
  49. struct device *dev;
  50. /** @dbi: the DBI bus abstraction handle */
  51. struct mipi_dbi dbi;
  52. /** @panel: the DRM panel instance for this device */
  53. struct drm_panel panel;
  54. /** @width: the width of this panel in mm */
  55. u32 width;
  56. /** @height: the height of this panel in mm */
  57. u32 height;
  58. /** @reset: reset GPIO line */
  59. struct gpio_desc *reset;
  60. /** @regulators: VCCIO and VIO supply regulators */
  61. struct regulator_bulk_data regulators[2];
  62. };
  63. static const struct drm_display_mode db7430_480_800_mode = {
  64. /*
  65. * 31 ns period min (htotal*vtotal*vrefresh)/1000
  66. * gives a Vrefresh of ~71 Hz.
  67. */
  68. .clock = 32258,
  69. .hdisplay = 480,
  70. .hsync_start = 480 + 10,
  71. .hsync_end = 480 + 10 + 4,
  72. .htotal = 480 + 10 + 4 + 40,
  73. .vdisplay = 800,
  74. .vsync_start = 800 + 6,
  75. .vsync_end = 800 + 6 + 1,
  76. .vtotal = 800 + 6 + 1 + 7,
  77. .width_mm = 53,
  78. .height_mm = 87,
  79. .flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC,
  80. };
  81. static inline struct db7430 *to_db7430(struct drm_panel *panel)
  82. {
  83. return container_of(panel, struct db7430, panel);
  84. }
  85. static int db7430_power_on(struct db7430 *db)
  86. {
  87. struct mipi_dbi *dbi = &db->dbi;
  88. int ret;
  89. /* Power up */
  90. ret = regulator_bulk_enable(ARRAY_SIZE(db->regulators),
  91. db->regulators);
  92. if (ret) {
  93. dev_err(db->dev, "failed to enable regulators: %d\n", ret);
  94. return ret;
  95. }
  96. msleep(50);
  97. /* Assert reset >=1 ms */
  98. gpiod_set_value_cansleep(db->reset, 1);
  99. usleep_range(1000, 5000);
  100. /* De-assert reset */
  101. gpiod_set_value_cansleep(db->reset, 0);
  102. /* Wait >= 10 ms */
  103. msleep(10);
  104. dev_dbg(db->dev, "de-asserted RESET\n");
  105. /*
  106. * This is set to 0x0a (RGB/BGR order + horizontal flip) in order
  107. * to make the display behave normally. If this is not set the displays
  108. * normal output behaviour is horizontally flipped and BGR ordered. Do
  109. * it twice because the first message doesn't always "take".
  110. */
  111. mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, 0x0a);
  112. mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, 0x0a);
  113. mipi_dbi_command(dbi, DB7430_ACCESS_PROT_OFF, 0x00);
  114. mipi_dbi_command(dbi, DB7430_PANEL_DRIVING, 0x28, 0x08);
  115. mipi_dbi_command(dbi, DB7430_SOURCE_CONTROL,
  116. 0x01, 0x30, 0x15, 0x05, 0x22);
  117. mipi_dbi_command(dbi, DB7430_GATE_INTERFACE,
  118. 0x10, 0x01, 0x00);
  119. mipi_dbi_command(dbi, DB7430_DISPLAY_H_TIMING,
  120. 0x06, 0x55, 0x03, 0x07, 0x0b,
  121. 0x33, 0x00, 0x01, 0x03);
  122. /*
  123. * 0x00 in datasheet 0x01 in vendor code 0x00, it seems 0x01 means
  124. * DE active high and 0x00 means DE active low.
  125. */
  126. mipi_dbi_command(dbi, DB7430_RGB_SYNC_OPTION, 0x01);
  127. mipi_dbi_command(dbi, DB7430_GAMMA_SET_RED,
  128. /* R positive gamma */ 0x00,
  129. 0x0A, 0x31, 0x3B, 0x4E, 0x58, 0x59, 0x5B, 0x58, 0x5E, 0x62,
  130. 0x60, 0x61, 0x5E, 0x62, 0x55, 0x55, 0x7F, 0x08,
  131. /* R negative gamma */ 0x00,
  132. 0x0A, 0x31, 0x3B, 0x4E, 0x58, 0x59, 0x5B, 0x58, 0x5E, 0x62,
  133. 0x60, 0x61, 0x5E, 0x62, 0x55, 0x55, 0x7F, 0x08);
  134. mipi_dbi_command(dbi, DB7430_GAMMA_SET_GREEN,
  135. /* G positive gamma */ 0x00,
  136. 0x25, 0x15, 0x28, 0x3D, 0x4A, 0x48, 0x4C, 0x4A, 0x52, 0x59,
  137. 0x59, 0x5B, 0x56, 0x60, 0x5D, 0x55, 0x7F, 0x0A,
  138. /* G negative gamma */ 0x00,
  139. 0x25, 0x15, 0x28, 0x3D, 0x4A, 0x48, 0x4C, 0x4A, 0x52, 0x59,
  140. 0x59, 0x5B, 0x56, 0x60, 0x5D, 0x55, 0x7F, 0x0A);
  141. mipi_dbi_command(dbi, DB7430_GAMMA_SET_BLUE,
  142. /* B positive gamma */ 0x00,
  143. 0x48, 0x10, 0x1F, 0x2F, 0x35, 0x38, 0x3D, 0x3C, 0x45, 0x4D,
  144. 0x4E, 0x52, 0x51, 0x60, 0x7F, 0x7E, 0x7F, 0x0C,
  145. /* B negative gamma */ 0x00,
  146. 0x48, 0x10, 0x1F, 0x2F, 0x35, 0x38, 0x3D, 0x3C, 0x45, 0x4D,
  147. 0x4E, 0x52, 0x51, 0x60, 0x7F, 0x7E, 0x7F, 0x0C);
  148. mipi_dbi_command(dbi, DB7430_BIAS_CURRENT_CTRL, 0x33, 0x13);
  149. mipi_dbi_command(dbi, DB7430_DDV_CTRL, 0x11, 0x00, 0x00);
  150. mipi_dbi_command(dbi, DB7430_GAMMA_CTRL_REF, 0x50, 0x50);
  151. mipi_dbi_command(dbi, DB7430_DCDC_CTRL, 0x2f, 0x11, 0x1e, 0x46);
  152. mipi_dbi_command(dbi, DB7430_VCL_CTRL, 0x11, 0x0a);
  153. return 0;
  154. }
  155. static int db7430_power_off(struct db7430 *db)
  156. {
  157. /* Go into RESET and disable regulators */
  158. gpiod_set_value_cansleep(db->reset, 1);
  159. return regulator_bulk_disable(ARRAY_SIZE(db->regulators),
  160. db->regulators);
  161. }
  162. static int db7430_unprepare(struct drm_panel *panel)
  163. {
  164. return db7430_power_off(to_db7430(panel));
  165. }
  166. static int db7430_disable(struct drm_panel *panel)
  167. {
  168. struct db7430 *db = to_db7430(panel);
  169. struct mipi_dbi *dbi = &db->dbi;
  170. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
  171. msleep(25);
  172. mipi_dbi_command(dbi, MIPI_DCS_ENTER_SLEEP_MODE);
  173. msleep(120);
  174. return 0;
  175. }
  176. static int db7430_prepare(struct drm_panel *panel)
  177. {
  178. return db7430_power_on(to_db7430(panel));
  179. }
  180. static int db7430_enable(struct drm_panel *panel)
  181. {
  182. struct db7430 *db = to_db7430(panel);
  183. struct mipi_dbi *dbi = &db->dbi;
  184. /* Exit sleep mode */
  185. mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
  186. msleep(20);
  187. /* NVM (non-volatile memory) load sequence */
  188. mipi_dbi_command(dbi, DB7430_UNKNOWN_D4, 0x52, 0x5e);
  189. mipi_dbi_command(dbi, DB7430_UNKNOWN_F8, 0x01, 0xf5, 0xf2, 0x71, 0x44);
  190. mipi_dbi_command(dbi, DB7430_UNKNOWN_FC, 0x00, 0x08);
  191. msleep(150);
  192. /* CABC turn on sequence (BC = backlight control) */
  193. mipi_dbi_command(dbi, DB7430_UNKNOWN_B4, 0x0f, 0x00, 0x50);
  194. mipi_dbi_command(dbi, DB7430_USER_SELECT, 0x80);
  195. mipi_dbi_command(dbi, DB7430_UNKNOWN_B7, 0x24);
  196. mipi_dbi_command(dbi, DB7430_UNKNOWN_B8, 0x01);
  197. /* Turn on display */
  198. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
  199. return 0;
  200. }
  201. /**
  202. * db7430_get_modes() - return the mode
  203. * @panel: the panel to get the mode for
  204. * @connector: reference to the central DRM connector control structure
  205. */
  206. static int db7430_get_modes(struct drm_panel *panel,
  207. struct drm_connector *connector)
  208. {
  209. struct db7430 *db = to_db7430(panel);
  210. struct drm_display_mode *mode;
  211. static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  212. mode = drm_mode_duplicate(connector->dev, &db7430_480_800_mode);
  213. if (!mode) {
  214. dev_err(db->dev, "failed to add mode\n");
  215. return -ENOMEM;
  216. }
  217. connector->display_info.bpc = 8;
  218. connector->display_info.width_mm = mode->width_mm;
  219. connector->display_info.height_mm = mode->height_mm;
  220. connector->display_info.bus_flags =
  221. DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
  222. drm_display_info_set_bus_formats(&connector->display_info,
  223. &bus_format, 1);
  224. drm_mode_set_name(mode);
  225. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  226. drm_mode_probed_add(connector, mode);
  227. return 1;
  228. }
  229. static const struct drm_panel_funcs db7430_drm_funcs = {
  230. .disable = db7430_disable,
  231. .unprepare = db7430_unprepare,
  232. .prepare = db7430_prepare,
  233. .enable = db7430_enable,
  234. .get_modes = db7430_get_modes,
  235. };
  236. static int db7430_probe(struct spi_device *spi)
  237. {
  238. struct device *dev = &spi->dev;
  239. struct db7430 *db;
  240. int ret;
  241. db = devm_kzalloc(dev, sizeof(*db), GFP_KERNEL);
  242. if (!db)
  243. return -ENOMEM;
  244. db->dev = dev;
  245. /*
  246. * VCI is the analog voltage supply
  247. * VCCIO is the digital I/O voltage supply
  248. */
  249. db->regulators[0].supply = "vci";
  250. db->regulators[1].supply = "vccio";
  251. ret = devm_regulator_bulk_get(dev,
  252. ARRAY_SIZE(db->regulators),
  253. db->regulators);
  254. if (ret)
  255. return dev_err_probe(dev, ret, "failed to get regulators\n");
  256. db->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  257. if (IS_ERR(db->reset)) {
  258. ret = PTR_ERR(db->reset);
  259. return dev_err_probe(dev, ret, "no RESET GPIO\n");
  260. }
  261. ret = mipi_dbi_spi_init(spi, &db->dbi, NULL);
  262. if (ret)
  263. return dev_err_probe(dev, ret, "MIPI DBI init failed\n");
  264. drm_panel_init(&db->panel, dev, &db7430_drm_funcs,
  265. DRM_MODE_CONNECTOR_DPI);
  266. /* FIXME: if no external backlight, use internal backlight */
  267. ret = drm_panel_of_backlight(&db->panel);
  268. if (ret)
  269. return dev_err_probe(dev, ret, "failed to add backlight\n");
  270. spi_set_drvdata(spi, db);
  271. drm_panel_add(&db->panel);
  272. dev_dbg(dev, "added panel\n");
  273. return 0;
  274. }
  275. static void db7430_remove(struct spi_device *spi)
  276. {
  277. struct db7430 *db = spi_get_drvdata(spi);
  278. drm_panel_remove(&db->panel);
  279. }
  280. /*
  281. * The DB7430 display controller may be used in several display products,
  282. * so list the different variants here and add per-variant data if needed.
  283. */
  284. static const struct of_device_id db7430_match[] = {
  285. { .compatible = "samsung,lms397kf04", },
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(of, db7430_match);
  289. static struct spi_driver db7430_driver = {
  290. .probe = db7430_probe,
  291. .remove = db7430_remove,
  292. .driver = {
  293. .name = "db7430-panel",
  294. .of_match_table = db7430_match,
  295. },
  296. };
  297. module_spi_driver(db7430_driver);
  298. MODULE_AUTHOR("Linus Walleij <[email protected]>");
  299. MODULE_DESCRIPTION("Samsung DB7430 panel driver");
  300. MODULE_LICENSE("GPL v2");