panel-widechips-ws2401.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Panel driver for the WideChips WS2401 480x800 DPI RGB panel, used in
  4. * the Samsung Mobile Display (SMD) LMS380KF01.
  5. * Found in the Samsung Galaxy Ace 2 GT-I8160 mobile phone.
  6. * Linus Walleij <[email protected]>
  7. * Inspired by code and know-how in the vendor driver by Gareth Phillips.
  8. */
  9. #include <drm/drm_mipi_dbi.h>
  10. #include <drm/drm_modes.h>
  11. #include <drm/drm_panel.h>
  12. #include <linux/backlight.h>
  13. #include <linux/delay.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/media-bus-format.h>
  18. #include <linux/module.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/spi/spi.h>
  21. #include <video/mipi_display.h>
  22. #define WS2401_RESCTL 0xb8 /* Resolution select control */
  23. #define WS2401_PSMPS 0xbd /* SMPS positive control */
  24. #define WS2401_NSMPS 0xbe /* SMPS negative control */
  25. #define WS2401_SMPS 0xbf
  26. #define WS2401_BCMODE 0xc1 /* Backlight control mode */
  27. #define WS2401_WRBLCTL 0xc3 /* Backlight control */
  28. #define WS2401_WRDISBV 0xc4 /* Write manual brightness */
  29. #define WS2401_WRCTRLD 0xc6 /* Write BL control */
  30. #define WS2401_WRMIE 0xc7 /* Write MIE mode */
  31. #define WS2401_READ_ID1 0xda /* Read panel ID 1 */
  32. #define WS2401_READ_ID2 0xdb /* Read panel ID 2 */
  33. #define WS2401_READ_ID3 0xdc /* Read panel ID 3 */
  34. #define WS2401_GAMMA_R1 0xe7 /* Gamma red 1 */
  35. #define WS2401_GAMMA_G1 0xe8 /* Gamma green 1 */
  36. #define WS2401_GAMMA_B1 0xe9 /* Gamma blue 1 */
  37. #define WS2401_GAMMA_R2 0xea /* Gamma red 2 */
  38. #define WS2401_GAMMA_G2 0xeb /* Gamma green 2 */
  39. #define WS2401_GAMMA_B2 0xec /* Gamma blue 2 */
  40. #define WS2401_PASSWD1 0xf0 /* Password command for level 2 */
  41. #define WS2401_DISCTL 0xf2 /* Display control */
  42. #define WS2401_PWRCTL 0xf3 /* Power control */
  43. #define WS2401_VCOMCTL 0xf4 /* VCOM control */
  44. #define WS2401_SRCCTL 0xf5 /* Source control */
  45. #define WS2401_PANELCTL 0xf6 /* Panel control */
  46. static const u8 ws2401_dbi_read_commands[] = {
  47. WS2401_READ_ID1,
  48. WS2401_READ_ID2,
  49. WS2401_READ_ID3,
  50. 0, /* sentinel */
  51. };
  52. /**
  53. * struct ws2401 - state container for a panel controlled by the WS2401
  54. * controller
  55. */
  56. struct ws2401 {
  57. /** @dev: the container device */
  58. struct device *dev;
  59. /** @dbi: the DBI bus abstraction handle */
  60. struct mipi_dbi dbi;
  61. /** @panel: the DRM panel instance for this device */
  62. struct drm_panel panel;
  63. /** @width: the width of this panel in mm */
  64. u32 width;
  65. /** @height: the height of this panel in mm */
  66. u32 height;
  67. /** @reset: reset GPIO line */
  68. struct gpio_desc *reset;
  69. /** @regulators: VCCIO and VIO supply regulators */
  70. struct regulator_bulk_data regulators[2];
  71. /** @internal_bl: If using internal backlight */
  72. bool internal_bl;
  73. };
  74. static const struct drm_display_mode lms380kf01_480_800_mode = {
  75. /*
  76. * The vendor driver states that the "SMD panel" has a clock
  77. * frequency of 49920000 Hz / 2 = 24960000 Hz.
  78. */
  79. .clock = 24960,
  80. .hdisplay = 480,
  81. .hsync_start = 480 + 8,
  82. .hsync_end = 480 + 8 + 10,
  83. .htotal = 480 + 8 + 10 + 8,
  84. .vdisplay = 800,
  85. .vsync_start = 800 + 8,
  86. .vsync_end = 800 + 8 + 2,
  87. .vtotal = 800 + 8 + 2 + 18,
  88. .width_mm = 50,
  89. .height_mm = 84,
  90. .flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC,
  91. };
  92. static inline struct ws2401 *to_ws2401(struct drm_panel *panel)
  93. {
  94. return container_of(panel, struct ws2401, panel);
  95. }
  96. static void ws2401_read_mtp_id(struct ws2401 *ws)
  97. {
  98. struct mipi_dbi *dbi = &ws->dbi;
  99. u8 id1, id2, id3;
  100. int ret;
  101. ret = mipi_dbi_command_read(dbi, WS2401_READ_ID1, &id1);
  102. if (ret) {
  103. dev_err(ws->dev, "unable to read MTP ID 1\n");
  104. return;
  105. }
  106. ret = mipi_dbi_command_read(dbi, WS2401_READ_ID2, &id2);
  107. if (ret) {
  108. dev_err(ws->dev, "unable to read MTP ID 2\n");
  109. return;
  110. }
  111. ret = mipi_dbi_command_read(dbi, WS2401_READ_ID3, &id3);
  112. if (ret) {
  113. dev_err(ws->dev, "unable to read MTP ID 3\n");
  114. return;
  115. }
  116. dev_info(ws->dev, "MTP ID: %02x %02x %02x\n", id1, id2, id3);
  117. }
  118. static int ws2401_power_on(struct ws2401 *ws)
  119. {
  120. struct mipi_dbi *dbi = &ws->dbi;
  121. int ret;
  122. /* Power up */
  123. ret = regulator_bulk_enable(ARRAY_SIZE(ws->regulators),
  124. ws->regulators);
  125. if (ret) {
  126. dev_err(ws->dev, "failed to enable regulators: %d\n", ret);
  127. return ret;
  128. }
  129. msleep(10);
  130. /* Assert reset >=1 ms */
  131. gpiod_set_value_cansleep(ws->reset, 1);
  132. usleep_range(1000, 5000);
  133. /* De-assert reset */
  134. gpiod_set_value_cansleep(ws->reset, 0);
  135. /* Wait >= 10 ms */
  136. msleep(10);
  137. dev_dbg(ws->dev, "de-asserted RESET\n");
  138. /*
  139. * Exit sleep mode and initialize display - some hammering is
  140. * necessary.
  141. */
  142. mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
  143. mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
  144. msleep(50);
  145. /* Magic to unlock level 2 control of the display */
  146. mipi_dbi_command(dbi, WS2401_PASSWD1, 0x5a, 0x5a);
  147. /* Configure resolution to 480RGBx800 */
  148. mipi_dbi_command(dbi, WS2401_RESCTL, 0x12);
  149. /* Set addressing mode Flip V(d0), Flip H(d1) RGB/BGR(d3) */
  150. mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, 0x01);
  151. /* Set pixel format: 24 bpp */
  152. mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 0x70);
  153. mipi_dbi_command(dbi, WS2401_SMPS, 0x00, 0x0f);
  154. mipi_dbi_command(dbi, WS2401_PSMPS, 0x06, 0x03, /* DDVDH: 4.6v */
  155. 0x7e, 0x03, 0x12, 0x37);
  156. mipi_dbi_command(dbi, WS2401_NSMPS, 0x06, 0x03, /* DDVDH: -4.6v */
  157. 0x7e, 0x02, 0x15, 0x37);
  158. mipi_dbi_command(dbi, WS2401_SMPS, 0x02, 0x0f);
  159. mipi_dbi_command(dbi, WS2401_PWRCTL, 0x10, 0xA9, 0x00, 0x01, 0x44,
  160. 0xb4, /* VGH:16.1v, VGL:-13.8v */
  161. 0x50, /* GREFP:4.2v (default) */
  162. 0x50, /* GREFN:-4.2v (default) */
  163. 0x00,
  164. 0x44); /* VOUTL:-10v (default) */
  165. mipi_dbi_command(dbi, WS2401_DISCTL, 0x01, 0x00, 0x00, 0x00, 0x14,
  166. 0x16);
  167. mipi_dbi_command(dbi, WS2401_VCOMCTL, 0x30, 0x53, 0x53);
  168. mipi_dbi_command(dbi, WS2401_SRCCTL, 0x03, 0x0C, 0x00, 0x00, 0x00,
  169. 0x01, /* 2 dot inversion */
  170. 0x01, 0x06, 0x03);
  171. mipi_dbi_command(dbi, WS2401_PANELCTL, 0x14, 0x00, 0x80, 0x00);
  172. mipi_dbi_command(dbi, WS2401_WRMIE, 0x01);
  173. /* Set up gamma, probably these are P-gamma and N-gamma for each color */
  174. mipi_dbi_command(dbi, WS2401_GAMMA_R1, 0x00,
  175. 0x5b, 0x42, 0x41, 0x3f, 0x42, 0x3d, 0x38, 0x2e,
  176. 0x2b, 0x2a, 0x27, 0x22, 0x27, 0x0f, 0x00, 0x00);
  177. mipi_dbi_command(dbi, WS2401_GAMMA_R2, 0x00,
  178. 0x5b, 0x42, 0x41, 0x3f, 0x42, 0x3d, 0x38, 0x2e,
  179. 0x2b, 0x2a, 0x27, 0x22, 0x27, 0x0f, 0x00, 0x00);
  180. mipi_dbi_command(dbi, WS2401_GAMMA_G1, 0x00,
  181. 0x59, 0x40, 0x3f, 0x3e, 0x41, 0x3d, 0x39, 0x2f,
  182. 0x2c, 0x2b, 0x29, 0x25, 0x29, 0x19, 0x08, 0x00);
  183. mipi_dbi_command(dbi, WS2401_GAMMA_G2, 0x00,
  184. 0x59, 0x40, 0x3f, 0x3e, 0x41, 0x3d, 0x39, 0x2f,
  185. 0x2c, 0x2b, 0x29, 0x25, 0x29, 0x19, 0x08, 0x00);
  186. mipi_dbi_command(dbi, WS2401_GAMMA_B1, 0x00,
  187. 0x57, 0x3b, 0x3a, 0x3b, 0x3f, 0x3b, 0x38, 0x27,
  188. 0x38, 0x2a, 0x26, 0x22, 0x34, 0x0c, 0x09, 0x00);
  189. mipi_dbi_command(dbi, WS2401_GAMMA_B2, 0x00,
  190. 0x57, 0x3b, 0x3a, 0x3b, 0x3f, 0x3b, 0x38, 0x27,
  191. 0x38, 0x2a, 0x26, 0x22, 0x34, 0x0c, 0x09, 0x00);
  192. if (ws->internal_bl) {
  193. mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x2c);
  194. } else {
  195. mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x00);
  196. /*
  197. * When not using internal backlight we do not need any further
  198. * L2 accesses to the panel so we close the door on our way out.
  199. * Otherwise we need to leave the L2 door open.
  200. */
  201. mipi_dbi_command(dbi, WS2401_PASSWD1, 0xa5, 0xa5);
  202. }
  203. return 0;
  204. }
  205. static int ws2401_power_off(struct ws2401 *ws)
  206. {
  207. /* Go into RESET and disable regulators */
  208. gpiod_set_value_cansleep(ws->reset, 1);
  209. return regulator_bulk_disable(ARRAY_SIZE(ws->regulators),
  210. ws->regulators);
  211. }
  212. static int ws2401_unprepare(struct drm_panel *panel)
  213. {
  214. struct ws2401 *ws = to_ws2401(panel);
  215. struct mipi_dbi *dbi = &ws->dbi;
  216. /* Make sure we disable backlight, if any */
  217. if (ws->internal_bl)
  218. mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x00);
  219. mipi_dbi_command(dbi, MIPI_DCS_ENTER_SLEEP_MODE);
  220. msleep(120);
  221. return ws2401_power_off(to_ws2401(panel));
  222. }
  223. static int ws2401_disable(struct drm_panel *panel)
  224. {
  225. struct ws2401 *ws = to_ws2401(panel);
  226. struct mipi_dbi *dbi = &ws->dbi;
  227. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
  228. msleep(25);
  229. return 0;
  230. }
  231. static int ws2401_prepare(struct drm_panel *panel)
  232. {
  233. return ws2401_power_on(to_ws2401(panel));
  234. }
  235. static int ws2401_enable(struct drm_panel *panel)
  236. {
  237. struct ws2401 *ws = to_ws2401(panel);
  238. struct mipi_dbi *dbi = &ws->dbi;
  239. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
  240. return 0;
  241. }
  242. /**
  243. * ws2401_get_modes() - return the mode
  244. * @panel: the panel to get the mode for
  245. * @connector: reference to the central DRM connector control structure
  246. */
  247. static int ws2401_get_modes(struct drm_panel *panel,
  248. struct drm_connector *connector)
  249. {
  250. struct ws2401 *ws = to_ws2401(panel);
  251. struct drm_display_mode *mode;
  252. static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  253. /*
  254. * We just support the LMS380KF01 so far, if we implement more panels
  255. * this mode, the following connector display_info settings and
  256. * probably the custom DCS sequences needs to selected based on what
  257. * the target panel needs.
  258. */
  259. mode = drm_mode_duplicate(connector->dev, &lms380kf01_480_800_mode);
  260. if (!mode) {
  261. dev_err(ws->dev, "failed to add mode\n");
  262. return -ENOMEM;
  263. }
  264. connector->display_info.bpc = 8;
  265. connector->display_info.width_mm = mode->width_mm;
  266. connector->display_info.height_mm = mode->height_mm;
  267. connector->display_info.bus_flags =
  268. DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
  269. drm_display_info_set_bus_formats(&connector->display_info,
  270. &bus_format, 1);
  271. drm_mode_set_name(mode);
  272. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  273. drm_mode_probed_add(connector, mode);
  274. return 1;
  275. }
  276. static const struct drm_panel_funcs ws2401_drm_funcs = {
  277. .disable = ws2401_disable,
  278. .unprepare = ws2401_unprepare,
  279. .prepare = ws2401_prepare,
  280. .enable = ws2401_enable,
  281. .get_modes = ws2401_get_modes,
  282. };
  283. static int ws2401_set_brightness(struct backlight_device *bl)
  284. {
  285. struct ws2401 *ws = bl_get_data(bl);
  286. struct mipi_dbi *dbi = &ws->dbi;
  287. u8 brightness = backlight_get_brightness(bl);
  288. if (backlight_is_blank(bl)) {
  289. mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x00);
  290. } else {
  291. mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x2c);
  292. mipi_dbi_command(dbi, WS2401_WRDISBV, brightness);
  293. }
  294. return 0;
  295. }
  296. static const struct backlight_ops ws2401_bl_ops = {
  297. .update_status = ws2401_set_brightness,
  298. };
  299. static const struct backlight_properties ws2401_bl_props = {
  300. .type = BACKLIGHT_PLATFORM,
  301. .brightness = 120,
  302. .max_brightness = U8_MAX,
  303. };
  304. static int ws2401_probe(struct spi_device *spi)
  305. {
  306. struct device *dev = &spi->dev;
  307. struct ws2401 *ws;
  308. int ret;
  309. ws = devm_kzalloc(dev, sizeof(*ws), GFP_KERNEL);
  310. if (!ws)
  311. return -ENOMEM;
  312. ws->dev = dev;
  313. /*
  314. * VCI is the analog voltage supply
  315. * VCCIO is the digital I/O voltage supply
  316. */
  317. ws->regulators[0].supply = "vci";
  318. ws->regulators[1].supply = "vccio";
  319. ret = devm_regulator_bulk_get(dev,
  320. ARRAY_SIZE(ws->regulators),
  321. ws->regulators);
  322. if (ret)
  323. return dev_err_probe(dev, ret, "failed to get regulators\n");
  324. ws->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  325. if (IS_ERR(ws->reset)) {
  326. ret = PTR_ERR(ws->reset);
  327. return dev_err_probe(dev, ret, "no RESET GPIO\n");
  328. }
  329. ret = mipi_dbi_spi_init(spi, &ws->dbi, NULL);
  330. if (ret)
  331. return dev_err_probe(dev, ret, "MIPI DBI init failed\n");
  332. ws->dbi.read_commands = ws2401_dbi_read_commands;
  333. ws2401_power_on(ws);
  334. ws2401_read_mtp_id(ws);
  335. ws2401_power_off(ws);
  336. drm_panel_init(&ws->panel, dev, &ws2401_drm_funcs,
  337. DRM_MODE_CONNECTOR_DPI);
  338. ret = drm_panel_of_backlight(&ws->panel);
  339. if (ret)
  340. return dev_err_probe(dev, ret,
  341. "failed to get external backlight device\n");
  342. if (!ws->panel.backlight) {
  343. dev_dbg(dev, "no external backlight, using internal backlight\n");
  344. ws->panel.backlight =
  345. devm_backlight_device_register(dev, "ws2401", dev, ws,
  346. &ws2401_bl_ops, &ws2401_bl_props);
  347. if (IS_ERR(ws->panel.backlight))
  348. return dev_err_probe(dev, PTR_ERR(ws->panel.backlight),
  349. "failed to register backlight device\n");
  350. } else {
  351. dev_dbg(dev, "using external backlight\n");
  352. }
  353. spi_set_drvdata(spi, ws);
  354. drm_panel_add(&ws->panel);
  355. dev_dbg(dev, "added panel\n");
  356. return 0;
  357. }
  358. static void ws2401_remove(struct spi_device *spi)
  359. {
  360. struct ws2401 *ws = spi_get_drvdata(spi);
  361. drm_panel_remove(&ws->panel);
  362. }
  363. /*
  364. * Samsung LMS380KF01 is the one instance of this display controller that we
  365. * know about, but if more are found, the controller can be parameterized
  366. * here and used for other configurations.
  367. */
  368. static const struct of_device_id ws2401_match[] = {
  369. { .compatible = "samsung,lms380kf01", },
  370. {},
  371. };
  372. MODULE_DEVICE_TABLE(of, ws2401_match);
  373. static struct spi_driver ws2401_driver = {
  374. .probe = ws2401_probe,
  375. .remove = ws2401_remove,
  376. .driver = {
  377. .name = "ws2401-panel",
  378. .of_match_table = ws2401_match,
  379. },
  380. };
  381. module_spi_driver(ws2401_driver);
  382. MODULE_AUTHOR("Linus Walleij <[email protected]>");
  383. MODULE_DESCRIPTION("Samsung WS2401 panel driver");
  384. MODULE_LICENSE("GPL v2");