panel-samsung-ld9040.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ld9040 AMOLED LCD drm_panel driver.
  4. *
  5. * Copyright (c) 2014 Samsung Electronics Co., Ltd
  6. * Derived from drivers/video/backlight/ld9040.c
  7. *
  8. * Andrzej Hajda <a.hajda@samsung.com>
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/spi/spi.h>
  16. #include <video/mipi_display.h>
  17. #include <video/of_videomode.h>
  18. #include <video/videomode.h>
  19. #include <drm/drm_modes.h>
  20. #include <drm/drm_panel.h>
  21. /* Manufacturer Command Set */
  22. #define MCS_MANPWR 0xb0
  23. #define MCS_ELVSS_ON 0xb1
  24. #define MCS_USER_SETTING 0xf0
  25. #define MCS_DISPCTL 0xf2
  26. #define MCS_POWER_CTRL 0xf4
  27. #define MCS_GTCON 0xf7
  28. #define MCS_PANEL_CONDITION 0xf8
  29. #define MCS_GAMMA_SET1 0xf9
  30. #define MCS_GAMMA_CTRL 0xfb
  31. /* array of gamma tables for gamma value 2.2 */
  32. static u8 const ld9040_gammas[25][22] = {
  33. { 0xf9, 0x00, 0x13, 0xb2, 0xba, 0xd2, 0x00, 0x30, 0x00, 0xaf, 0xc0,
  34. 0xb8, 0xcd, 0x00, 0x3d, 0x00, 0xa8, 0xb8, 0xb7, 0xcd, 0x00, 0x44 },
  35. { 0xf9, 0x00, 0x13, 0xb9, 0xb9, 0xd0, 0x00, 0x3c, 0x00, 0xaf, 0xbf,
  36. 0xb6, 0xcb, 0x00, 0x4b, 0x00, 0xa8, 0xb9, 0xb5, 0xcc, 0x00, 0x52 },
  37. { 0xf9, 0x00, 0x13, 0xba, 0xb9, 0xcd, 0x00, 0x41, 0x00, 0xb0, 0xbe,
  38. 0xb5, 0xc9, 0x00, 0x51, 0x00, 0xa9, 0xb9, 0xb5, 0xca, 0x00, 0x57 },
  39. { 0xf9, 0x00, 0x13, 0xb9, 0xb8, 0xcd, 0x00, 0x46, 0x00, 0xb1, 0xbc,
  40. 0xb5, 0xc8, 0x00, 0x56, 0x00, 0xaa, 0xb8, 0xb4, 0xc9, 0x00, 0x5d },
  41. { 0xf9, 0x00, 0x13, 0xba, 0xb8, 0xcb, 0x00, 0x4b, 0x00, 0xb3, 0xbc,
  42. 0xb4, 0xc7, 0x00, 0x5c, 0x00, 0xac, 0xb8, 0xb4, 0xc8, 0x00, 0x62 },
  43. { 0xf9, 0x00, 0x13, 0xbb, 0xb7, 0xca, 0x00, 0x4f, 0x00, 0xb4, 0xbb,
  44. 0xb3, 0xc7, 0x00, 0x60, 0x00, 0xad, 0xb8, 0xb4, 0xc7, 0x00, 0x67 },
  45. { 0xf9, 0x00, 0x47, 0xba, 0xb6, 0xca, 0x00, 0x53, 0x00, 0xb5, 0xbb,
  46. 0xb3, 0xc6, 0x00, 0x65, 0x00, 0xae, 0xb8, 0xb3, 0xc7, 0x00, 0x6c },
  47. { 0xf9, 0x00, 0x71, 0xbb, 0xb5, 0xc8, 0x00, 0x57, 0x00, 0xb5, 0xbb,
  48. 0xb0, 0xc5, 0x00, 0x6a, 0x00, 0xae, 0xb9, 0xb1, 0xc6, 0x00, 0x70 },
  49. { 0xf9, 0x00, 0x7b, 0xbb, 0xb4, 0xc8, 0x00, 0x5b, 0x00, 0xb5, 0xba,
  50. 0xb1, 0xc4, 0x00, 0x6e, 0x00, 0xae, 0xb9, 0xb0, 0xc5, 0x00, 0x75 },
  51. { 0xf9, 0x00, 0x82, 0xba, 0xb4, 0xc7, 0x00, 0x5f, 0x00, 0xb5, 0xba,
  52. 0xb0, 0xc3, 0x00, 0x72, 0x00, 0xae, 0xb8, 0xb0, 0xc3, 0x00, 0x7a },
  53. { 0xf9, 0x00, 0x89, 0xba, 0xb3, 0xc8, 0x00, 0x62, 0x00, 0xb6, 0xba,
  54. 0xaf, 0xc3, 0x00, 0x76, 0x00, 0xaf, 0xb7, 0xae, 0xc4, 0x00, 0x7e },
  55. { 0xf9, 0x00, 0x8b, 0xb9, 0xb3, 0xc7, 0x00, 0x65, 0x00, 0xb7, 0xb8,
  56. 0xaf, 0xc3, 0x00, 0x7a, 0x00, 0x80, 0xb6, 0xae, 0xc4, 0x00, 0x81 },
  57. { 0xf9, 0x00, 0x93, 0xba, 0xb3, 0xc5, 0x00, 0x69, 0x00, 0xb8, 0xb9,
  58. 0xae, 0xc1, 0x00, 0x7f, 0x00, 0xb0, 0xb6, 0xae, 0xc3, 0x00, 0x85 },
  59. { 0xf9, 0x00, 0x97, 0xba, 0xb2, 0xc5, 0x00, 0x6c, 0x00, 0xb8, 0xb8,
  60. 0xae, 0xc1, 0x00, 0x82, 0x00, 0xb0, 0xb6, 0xae, 0xc2, 0x00, 0x89 },
  61. { 0xf9, 0x00, 0x9a, 0xba, 0xb1, 0xc4, 0x00, 0x6f, 0x00, 0xb8, 0xb8,
  62. 0xad, 0xc0, 0x00, 0x86, 0x00, 0xb0, 0xb7, 0xad, 0xc0, 0x00, 0x8d },
  63. { 0xf9, 0x00, 0x9c, 0xb9, 0xb0, 0xc4, 0x00, 0x72, 0x00, 0xb8, 0xb8,
  64. 0xac, 0xbf, 0x00, 0x8a, 0x00, 0xb0, 0xb6, 0xac, 0xc0, 0x00, 0x91 },
  65. { 0xf9, 0x00, 0x9e, 0xba, 0xb0, 0xc2, 0x00, 0x75, 0x00, 0xb9, 0xb8,
  66. 0xab, 0xbe, 0x00, 0x8e, 0x00, 0xb0, 0xb6, 0xac, 0xbf, 0x00, 0x94 },
  67. { 0xf9, 0x00, 0xa0, 0xb9, 0xaf, 0xc3, 0x00, 0x77, 0x00, 0xb9, 0xb7,
  68. 0xab, 0xbe, 0x00, 0x90, 0x00, 0xb0, 0xb6, 0xab, 0xbf, 0x00, 0x97 },
  69. { 0xf9, 0x00, 0xa2, 0xb9, 0xaf, 0xc2, 0x00, 0x7a, 0x00, 0xb9, 0xb7,
  70. 0xaa, 0xbd, 0x00, 0x94, 0x00, 0xb0, 0xb5, 0xab, 0xbf, 0x00, 0x9a },
  71. { 0xf9, 0x00, 0xa4, 0xb9, 0xaf, 0xc1, 0x00, 0x7d, 0x00, 0xb9, 0xb6,
  72. 0xaa, 0xbb, 0x00, 0x97, 0x00, 0xb1, 0xb5, 0xaa, 0xbf, 0x00, 0x9d },
  73. { 0xf9, 0x00, 0xa4, 0xb8, 0xb0, 0xbf, 0x00, 0x80, 0x00, 0xb8, 0xb6,
  74. 0xaa, 0xbc, 0x00, 0x9a, 0x00, 0xb0, 0xb5, 0xab, 0xbd, 0x00, 0xa0 },
  75. { 0xf9, 0x00, 0xa8, 0xb8, 0xae, 0xbe, 0x00, 0x84, 0x00, 0xb9, 0xb7,
  76. 0xa8, 0xbc, 0x00, 0x9d, 0x00, 0xb2, 0xb5, 0xaa, 0xbc, 0x00, 0xa4 },
  77. { 0xf9, 0x00, 0xa9, 0xb6, 0xad, 0xbf, 0x00, 0x86, 0x00, 0xb8, 0xb5,
  78. 0xa8, 0xbc, 0x00, 0xa0, 0x00, 0xb3, 0xb3, 0xa9, 0xbc, 0x00, 0xa7 },
  79. { 0xf9, 0x00, 0xa9, 0xb7, 0xae, 0xbd, 0x00, 0x89, 0x00, 0xb7, 0xb6,
  80. 0xa8, 0xba, 0x00, 0xa4, 0x00, 0xb1, 0xb4, 0xaa, 0xbb, 0x00, 0xaa },
  81. { 0xf9, 0x00, 0xa7, 0xb4, 0xae, 0xbf, 0x00, 0x91, 0x00, 0xb2, 0xb4,
  82. 0xaa, 0xbb, 0x00, 0xac, 0x00, 0xb3, 0xb1, 0xaa, 0xbc, 0x00, 0xb3 },
  83. };
  84. struct ld9040 {
  85. struct device *dev;
  86. struct drm_panel panel;
  87. struct regulator_bulk_data supplies[2];
  88. struct gpio_desc *reset_gpio;
  89. u32 power_on_delay;
  90. u32 reset_delay;
  91. struct videomode vm;
  92. u32 width_mm;
  93. u32 height_mm;
  94. int brightness;
  95. /* This field is tested by functions directly accessing bus before
  96. * transfer, transfer is skipped if it is set. In case of transfer
  97. * failure or unexpected response the field is set to error value.
  98. * Such construct allows to eliminate many checks in higher level
  99. * functions.
  100. */
  101. int error;
  102. };
  103. static inline struct ld9040 *panel_to_ld9040(struct drm_panel *panel)
  104. {
  105. return container_of(panel, struct ld9040, panel);
  106. }
  107. static int ld9040_clear_error(struct ld9040 *ctx)
  108. {
  109. int ret = ctx->error;
  110. ctx->error = 0;
  111. return ret;
  112. }
  113. static int ld9040_spi_write_word(struct ld9040 *ctx, u16 data)
  114. {
  115. struct spi_device *spi = to_spi_device(ctx->dev);
  116. struct spi_transfer xfer = {
  117. .len = 2,
  118. .tx_buf = &data,
  119. };
  120. struct spi_message msg;
  121. spi_message_init(&msg);
  122. spi_message_add_tail(&xfer, &msg);
  123. return spi_sync(spi, &msg);
  124. }
  125. static void ld9040_dcs_write(struct ld9040 *ctx, const u8 *data, size_t len)
  126. {
  127. int ret = 0;
  128. if (ctx->error < 0 || len == 0)
  129. return;
  130. dev_dbg(ctx->dev, "writing dcs seq: %*ph\n", (int)len, data);
  131. ret = ld9040_spi_write_word(ctx, *data);
  132. while (!ret && --len) {
  133. ++data;
  134. ret = ld9040_spi_write_word(ctx, *data | 0x100);
  135. }
  136. if (ret) {
  137. dev_err(ctx->dev, "error %d writing dcs seq: %*ph\n", ret,
  138. (int)len, data);
  139. ctx->error = ret;
  140. }
  141. usleep_range(300, 310);
  142. }
  143. #define ld9040_dcs_write_seq_static(ctx, seq...) \
  144. ({\
  145. static const u8 d[] = { seq };\
  146. ld9040_dcs_write(ctx, d, ARRAY_SIZE(d));\
  147. })
  148. static void ld9040_brightness_set(struct ld9040 *ctx)
  149. {
  150. ld9040_dcs_write(ctx, ld9040_gammas[ctx->brightness],
  151. ARRAY_SIZE(ld9040_gammas[ctx->brightness]));
  152. ld9040_dcs_write_seq_static(ctx, MCS_GAMMA_CTRL, 0x02, 0x5a);
  153. }
  154. static void ld9040_init(struct ld9040 *ctx)
  155. {
  156. ld9040_dcs_write_seq_static(ctx, MCS_USER_SETTING, 0x5a, 0x5a);
  157. ld9040_dcs_write_seq_static(ctx, MCS_PANEL_CONDITION,
  158. 0x05, 0x65, 0x96, 0x71, 0x7d, 0x19, 0x3b, 0x0d,
  159. 0x19, 0x7e, 0x0d, 0xe2, 0x00, 0x00, 0x7e, 0x7d,
  160. 0x07, 0x07, 0x20, 0x20, 0x20, 0x02, 0x02);
  161. ld9040_dcs_write_seq_static(ctx, MCS_DISPCTL,
  162. 0x02, 0x08, 0x08, 0x10, 0x10);
  163. ld9040_dcs_write_seq_static(ctx, MCS_MANPWR, 0x04);
  164. ld9040_dcs_write_seq_static(ctx, MCS_POWER_CTRL,
  165. 0x0a, 0x87, 0x25, 0x6a, 0x44, 0x02, 0x88);
  166. ld9040_dcs_write_seq_static(ctx, MCS_ELVSS_ON, 0x0d, 0x00, 0x16);
  167. ld9040_dcs_write_seq_static(ctx, MCS_GTCON, 0x09, 0x00, 0x00);
  168. ld9040_brightness_set(ctx);
  169. ld9040_dcs_write_seq_static(ctx, MIPI_DCS_EXIT_SLEEP_MODE);
  170. ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_ON);
  171. }
  172. static int ld9040_power_on(struct ld9040 *ctx)
  173. {
  174. int ret;
  175. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  176. if (ret < 0)
  177. return ret;
  178. msleep(ctx->power_on_delay);
  179. gpiod_set_value(ctx->reset_gpio, 0);
  180. msleep(ctx->reset_delay);
  181. gpiod_set_value(ctx->reset_gpio, 1);
  182. msleep(ctx->reset_delay);
  183. return 0;
  184. }
  185. static int ld9040_power_off(struct ld9040 *ctx)
  186. {
  187. return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  188. }
  189. static int ld9040_disable(struct drm_panel *panel)
  190. {
  191. return 0;
  192. }
  193. static int ld9040_unprepare(struct drm_panel *panel)
  194. {
  195. struct ld9040 *ctx = panel_to_ld9040(panel);
  196. msleep(120);
  197. ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_OFF);
  198. ld9040_dcs_write_seq_static(ctx, MIPI_DCS_ENTER_SLEEP_MODE);
  199. msleep(40);
  200. ld9040_clear_error(ctx);
  201. return ld9040_power_off(ctx);
  202. }
  203. static int ld9040_prepare(struct drm_panel *panel)
  204. {
  205. struct ld9040 *ctx = panel_to_ld9040(panel);
  206. int ret;
  207. ret = ld9040_power_on(ctx);
  208. if (ret < 0)
  209. return ret;
  210. ld9040_init(ctx);
  211. ret = ld9040_clear_error(ctx);
  212. if (ret < 0)
  213. ld9040_unprepare(panel);
  214. return ret;
  215. }
  216. static int ld9040_enable(struct drm_panel *panel)
  217. {
  218. return 0;
  219. }
  220. static int ld9040_get_modes(struct drm_panel *panel,
  221. struct drm_connector *connector)
  222. {
  223. struct ld9040 *ctx = panel_to_ld9040(panel);
  224. struct drm_display_mode *mode;
  225. mode = drm_mode_create(connector->dev);
  226. if (!mode) {
  227. dev_err(panel->dev, "failed to create a new display mode\n");
  228. return 0;
  229. }
  230. drm_display_mode_from_videomode(&ctx->vm, mode);
  231. mode->width_mm = ctx->width_mm;
  232. mode->height_mm = ctx->height_mm;
  233. connector->display_info.width_mm = mode->width_mm;
  234. connector->display_info.height_mm = mode->height_mm;
  235. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  236. drm_mode_probed_add(connector, mode);
  237. return 1;
  238. }
  239. static const struct drm_panel_funcs ld9040_drm_funcs = {
  240. .disable = ld9040_disable,
  241. .unprepare = ld9040_unprepare,
  242. .prepare = ld9040_prepare,
  243. .enable = ld9040_enable,
  244. .get_modes = ld9040_get_modes,
  245. };
  246. static int ld9040_parse_dt(struct ld9040 *ctx)
  247. {
  248. struct device *dev = ctx->dev;
  249. struct device_node *np = dev->of_node;
  250. int ret;
  251. ret = of_get_videomode(np, &ctx->vm, 0);
  252. if (ret < 0)
  253. return ret;
  254. of_property_read_u32(np, "power-on-delay", &ctx->power_on_delay);
  255. of_property_read_u32(np, "reset-delay", &ctx->reset_delay);
  256. of_property_read_u32(np, "panel-width-mm", &ctx->width_mm);
  257. of_property_read_u32(np, "panel-height-mm", &ctx->height_mm);
  258. return 0;
  259. }
  260. static int ld9040_probe(struct spi_device *spi)
  261. {
  262. struct device *dev = &spi->dev;
  263. struct ld9040 *ctx;
  264. int ret;
  265. ctx = devm_kzalloc(dev, sizeof(struct ld9040), GFP_KERNEL);
  266. if (!ctx)
  267. return -ENOMEM;
  268. spi_set_drvdata(spi, ctx);
  269. ctx->dev = dev;
  270. ctx->brightness = ARRAY_SIZE(ld9040_gammas) - 1;
  271. ret = ld9040_parse_dt(ctx);
  272. if (ret < 0)
  273. return ret;
  274. ctx->supplies[0].supply = "vdd3";
  275. ctx->supplies[1].supply = "vci";
  276. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
  277. ctx->supplies);
  278. if (ret < 0)
  279. return ret;
  280. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  281. if (IS_ERR(ctx->reset_gpio)) {
  282. dev_err(dev, "cannot get reset-gpios %ld\n",
  283. PTR_ERR(ctx->reset_gpio));
  284. return PTR_ERR(ctx->reset_gpio);
  285. }
  286. spi->bits_per_word = 9;
  287. ret = spi_setup(spi);
  288. if (ret < 0) {
  289. dev_err(dev, "spi setup failed.\n");
  290. return ret;
  291. }
  292. drm_panel_init(&ctx->panel, dev, &ld9040_drm_funcs,
  293. DRM_MODE_CONNECTOR_DPI);
  294. drm_panel_add(&ctx->panel);
  295. return 0;
  296. }
  297. static int ld9040_remove(struct spi_device *spi)
  298. {
  299. struct ld9040 *ctx = spi_get_drvdata(spi);
  300. ld9040_power_off(ctx);
  301. drm_panel_remove(&ctx->panel);
  302. return 0;
  303. }
  304. static const struct of_device_id ld9040_of_match[] = {
  305. { .compatible = "samsung,ld9040" },
  306. { }
  307. };
  308. MODULE_DEVICE_TABLE(of, ld9040_of_match);
  309. static const struct spi_device_id ld9040_ids[] = {
  310. { "ld9040", },
  311. { /* sentinel */ }
  312. };
  313. MODULE_DEVICE_TABLE(spi, ld9040_ids);
  314. static struct spi_driver ld9040_driver = {
  315. .probe = ld9040_probe,
  316. .remove = ld9040_remove,
  317. .driver = {
  318. .name = "panel-samsung-ld9040",
  319. .of_match_table = ld9040_of_match,
  320. },
  321. };
  322. module_spi_driver(ld9040_driver);
  323. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  324. MODULE_DESCRIPTION("ld9040 LCD Driver");
  325. MODULE_LICENSE("GPL v2");