panel-samsung-s6e63m0.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S6E63M0 AMOLED LCD drm_panel driver.
  4. *
  5. * Copyright (C) 2019 Paweł Chmiel <[email protected]>
  6. * Derived from drivers/gpu/drm/panel-samsung-ld9040.c
  7. *
  8. * Andrzej Hajda <[email protected]>
  9. */
  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/module.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/media-bus-format.h>
  18. #include <video/mipi_display.h>
  19. #include "panel-samsung-s6e63m0.h"
  20. #define S6E63M0_LCD_ID_VALUE_M2 0xA4
  21. #define S6E63M0_LCD_ID_VALUE_SM2 0xB4
  22. #define S6E63M0_LCD_ID_VALUE_SM2_1 0xB6
  23. #define NUM_GAMMA_LEVELS 28
  24. #define GAMMA_TABLE_COUNT 23
  25. #define MAX_BRIGHTNESS (NUM_GAMMA_LEVELS - 1)
  26. /* array of gamma tables for gamma value 2.2 */
  27. static u8 const s6e63m0_gamma_22[NUM_GAMMA_LEVELS][GAMMA_TABLE_COUNT] = {
  28. /* 30 cd */
  29. { MCS_PGAMMACTL, 0x02,
  30. 0x18, 0x08, 0x24, 0xA1, 0x51, 0x7B, 0xCE,
  31. 0xCB, 0xC2, 0xC7, 0xCB, 0xBC, 0xDA, 0xDD,
  32. 0xD3, 0x00, 0x53, 0x00, 0x52, 0x00, 0x6F, },
  33. /* 40 cd */
  34. { MCS_PGAMMACTL, 0x02,
  35. 0x18, 0x08, 0x24, 0x97, 0x58, 0x71, 0xCC,
  36. 0xCB, 0xC0, 0xC5, 0xC9, 0xBA, 0xD9, 0xDC,
  37. 0xD1, 0x00, 0x5B, 0x00, 0x5A, 0x00, 0x7A, },
  38. /* 50 cd */
  39. { MCS_PGAMMACTL, 0x02,
  40. 0x18, 0x08, 0x24, 0x96, 0x58, 0x72, 0xCB,
  41. 0xCA, 0xBF, 0xC6, 0xC9, 0xBA, 0xD6, 0xD9,
  42. 0xCD, 0x00, 0x61, 0x00, 0x61, 0x00, 0x83, },
  43. /* 60 cd */
  44. { MCS_PGAMMACTL, 0x02,
  45. 0x18, 0x08, 0x24, 0x91, 0x5E, 0x6E, 0xC9,
  46. 0xC9, 0xBD, 0xC4, 0xC9, 0xB8, 0xD3, 0xD7,
  47. 0xCA, 0x00, 0x69, 0x00, 0x67, 0x00, 0x8D, },
  48. /* 70 cd */
  49. { MCS_PGAMMACTL, 0x02,
  50. 0x18, 0x08, 0x24, 0x8E, 0x62, 0x6B, 0xC7,
  51. 0xC9, 0xBB, 0xC3, 0xC7, 0xB7, 0xD3, 0xD7,
  52. 0xCA, 0x00, 0x6E, 0x00, 0x6C, 0x00, 0x94, },
  53. /* 80 cd */
  54. { MCS_PGAMMACTL, 0x02,
  55. 0x18, 0x08, 0x24, 0x89, 0x68, 0x65, 0xC9,
  56. 0xC9, 0xBC, 0xC1, 0xC5, 0xB6, 0xD2, 0xD5,
  57. 0xC9, 0x00, 0x73, 0x00, 0x72, 0x00, 0x9A, },
  58. /* 90 cd */
  59. { MCS_PGAMMACTL, 0x02,
  60. 0x18, 0x08, 0x24, 0x89, 0x69, 0x64, 0xC7,
  61. 0xC8, 0xBB, 0xC0, 0xC5, 0xB4, 0xD2, 0xD5,
  62. 0xC9, 0x00, 0x77, 0x00, 0x76, 0x00, 0xA0, },
  63. /* 100 cd */
  64. { MCS_PGAMMACTL, 0x02,
  65. 0x18, 0x08, 0x24, 0x86, 0x69, 0x60, 0xC6,
  66. 0xC8, 0xBA, 0xBF, 0xC4, 0xB4, 0xD0, 0xD4,
  67. 0xC6, 0x00, 0x7C, 0x00, 0x7A, 0x00, 0xA7, },
  68. /* 110 cd */
  69. { MCS_PGAMMACTL, 0x02,
  70. 0x18, 0x08, 0x24, 0x86, 0x6A, 0x60, 0xC5,
  71. 0xC7, 0xBA, 0xBD, 0xC3, 0xB2, 0xD0, 0xD4,
  72. 0xC5, 0x00, 0x80, 0x00, 0x7E, 0x00, 0xAD, },
  73. /* 120 cd */
  74. { MCS_PGAMMACTL, 0x02,
  75. 0x18, 0x08, 0x24, 0x82, 0x6B, 0x5E, 0xC4,
  76. 0xC8, 0xB9, 0xBD, 0xC2, 0xB1, 0xCE, 0xD2,
  77. 0xC4, 0x00, 0x85, 0x00, 0x82, 0x00, 0xB3, },
  78. /* 130 cd */
  79. { MCS_PGAMMACTL, 0x02,
  80. 0x18, 0x08, 0x24, 0x8C, 0x6C, 0x60, 0xC3,
  81. 0xC7, 0xB9, 0xBC, 0xC1, 0xAF, 0xCE, 0xD2,
  82. 0xC3, 0x00, 0x88, 0x00, 0x86, 0x00, 0xB8, },
  83. /* 140 cd */
  84. { MCS_PGAMMACTL, 0x02,
  85. 0x18, 0x08, 0x24, 0x80, 0x6C, 0x5F, 0xC1,
  86. 0xC6, 0xB7, 0xBC, 0xC1, 0xAE, 0xCD, 0xD0,
  87. 0xC2, 0x00, 0x8C, 0x00, 0x8A, 0x00, 0xBE, },
  88. /* 150 cd */
  89. { MCS_PGAMMACTL, 0x02,
  90. 0x18, 0x08, 0x24, 0x80, 0x6E, 0x5F, 0xC1,
  91. 0xC6, 0xB6, 0xBC, 0xC0, 0xAE, 0xCC, 0xD0,
  92. 0xC2, 0x00, 0x8F, 0x00, 0x8D, 0x00, 0xC2, },
  93. /* 160 cd */
  94. { MCS_PGAMMACTL, 0x02,
  95. 0x18, 0x08, 0x24, 0x7F, 0x6E, 0x5F, 0xC0,
  96. 0xC6, 0xB5, 0xBA, 0xBF, 0xAD, 0xCB, 0xCF,
  97. 0xC0, 0x00, 0x94, 0x00, 0x91, 0x00, 0xC8, },
  98. /* 170 cd */
  99. { MCS_PGAMMACTL, 0x02,
  100. 0x18, 0x08, 0x24, 0x7C, 0x6D, 0x5C, 0xC0,
  101. 0xC6, 0xB4, 0xBB, 0xBE, 0xAD, 0xCA, 0xCF,
  102. 0xC0, 0x00, 0x96, 0x00, 0x94, 0x00, 0xCC, },
  103. /* 180 cd */
  104. { MCS_PGAMMACTL, 0x02,
  105. 0x18, 0x08, 0x24, 0x7B, 0x6D, 0x5B, 0xC0,
  106. 0xC5, 0xB3, 0xBA, 0xBE, 0xAD, 0xCA, 0xCE,
  107. 0xBF, 0x00, 0x99, 0x00, 0x97, 0x00, 0xD0, },
  108. /* 190 cd */
  109. { MCS_PGAMMACTL, 0x02,
  110. 0x18, 0x08, 0x24, 0x7A, 0x6D, 0x59, 0xC1,
  111. 0xC5, 0xB4, 0xB8, 0xBD, 0xAC, 0xC9, 0xCE,
  112. 0xBE, 0x00, 0x9D, 0x00, 0x9A, 0x00, 0xD5, },
  113. /* 200 cd */
  114. { MCS_PGAMMACTL, 0x02,
  115. 0x18, 0x08, 0x24, 0x79, 0x6D, 0x58, 0xC1,
  116. 0xC4, 0xB4, 0xB6, 0xBD, 0xAA, 0xCA, 0xCD,
  117. 0xBE, 0x00, 0x9F, 0x00, 0x9D, 0x00, 0xD9, },
  118. /* 210 cd */
  119. { MCS_PGAMMACTL, 0x02,
  120. 0x18, 0x08, 0x24, 0x79, 0x6D, 0x57, 0xC0,
  121. 0xC4, 0xB4, 0xB7, 0xBD, 0xAA, 0xC8, 0xCC,
  122. 0xBD, 0x00, 0xA2, 0x00, 0xA0, 0x00, 0xDD, },
  123. /* 220 cd */
  124. { MCS_PGAMMACTL, 0x02,
  125. 0x18, 0x08, 0x24, 0x78, 0x6F, 0x58, 0xBF,
  126. 0xC4, 0xB3, 0xB5, 0xBB, 0xA9, 0xC8, 0xCC,
  127. 0xBC, 0x00, 0xA6, 0x00, 0xA3, 0x00, 0xE2, },
  128. /* 230 cd */
  129. { MCS_PGAMMACTL, 0x02,
  130. 0x18, 0x08, 0x24, 0x75, 0x6F, 0x56, 0xBF,
  131. 0xC3, 0xB2, 0xB6, 0xBB, 0xA8, 0xC7, 0xCB,
  132. 0xBC, 0x00, 0xA8, 0x00, 0xA6, 0x00, 0xE6, },
  133. /* 240 cd */
  134. { MCS_PGAMMACTL, 0x02,
  135. 0x18, 0x08, 0x24, 0x76, 0x6F, 0x56, 0xC0,
  136. 0xC3, 0xB2, 0xB5, 0xBA, 0xA8, 0xC6, 0xCB,
  137. 0xBB, 0x00, 0xAA, 0x00, 0xA8, 0x00, 0xE9, },
  138. /* 250 cd */
  139. { MCS_PGAMMACTL, 0x02,
  140. 0x18, 0x08, 0x24, 0x74, 0x6D, 0x54, 0xBF,
  141. 0xC3, 0xB2, 0xB4, 0xBA, 0xA7, 0xC6, 0xCA,
  142. 0xBA, 0x00, 0xAD, 0x00, 0xAB, 0x00, 0xED, },
  143. /* 260 cd */
  144. { MCS_PGAMMACTL, 0x02,
  145. 0x18, 0x08, 0x24, 0x74, 0x6E, 0x54, 0xBD,
  146. 0xC2, 0xB0, 0xB5, 0xBA, 0xA7, 0xC5, 0xC9,
  147. 0xBA, 0x00, 0xB0, 0x00, 0xAE, 0x00, 0xF1, },
  148. /* 270 cd */
  149. { MCS_PGAMMACTL, 0x02,
  150. 0x18, 0x08, 0x24, 0x71, 0x6C, 0x50, 0xBD,
  151. 0xC3, 0xB0, 0xB4, 0xB8, 0xA6, 0xC6, 0xC9,
  152. 0xBB, 0x00, 0xB2, 0x00, 0xB1, 0x00, 0xF4, },
  153. /* 280 cd */
  154. { MCS_PGAMMACTL, 0x02,
  155. 0x18, 0x08, 0x24, 0x6E, 0x6C, 0x4D, 0xBE,
  156. 0xC3, 0xB1, 0xB3, 0xB8, 0xA5, 0xC6, 0xC8,
  157. 0xBB, 0x00, 0xB4, 0x00, 0xB3, 0x00, 0xF7, },
  158. /* 290 cd */
  159. { MCS_PGAMMACTL, 0x02,
  160. 0x18, 0x08, 0x24, 0x71, 0x70, 0x50, 0xBD,
  161. 0xC1, 0xB0, 0xB2, 0xB8, 0xA4, 0xC6, 0xC7,
  162. 0xBB, 0x00, 0xB6, 0x00, 0xB6, 0x00, 0xFA, },
  163. /* 300 cd */
  164. { MCS_PGAMMACTL, 0x02,
  165. 0x18, 0x08, 0x24, 0x70, 0x6E, 0x4E, 0xBC,
  166. 0xC0, 0xAF, 0xB3, 0xB8, 0xA5, 0xC5, 0xC7,
  167. 0xBB, 0x00, 0xB9, 0x00, 0xB8, 0x00, 0xFC, },
  168. };
  169. #define NUM_ACL_LEVELS 7
  170. #define ACL_TABLE_COUNT 28
  171. static u8 const s6e63m0_acl[NUM_ACL_LEVELS][ACL_TABLE_COUNT] = {
  172. /* NULL ACL */
  173. { MCS_BCMODE,
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00 },
  178. /* 40P ACL */
  179. { MCS_BCMODE,
  180. 0x4D, 0x96, 0x1D, 0x00, 0x00, 0x01, 0xDF, 0x00,
  181. 0x00, 0x03, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00,
  182. 0x01, 0x06, 0x0C, 0x11, 0x16, 0x1C, 0x21, 0x26,
  183. 0x2B, 0x31, 0x36 },
  184. /* 43P ACL */
  185. { MCS_BCMODE,
  186. 0x4D, 0x96, 0x1D, 0x00, 0x00, 0x01, 0xDF, 0x00,
  187. 0x00, 0x03, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. 0x01, 0x07, 0x0C, 0x12, 0x18, 0x1E, 0x23, 0x29,
  189. 0x2F, 0x34, 0x3A },
  190. /* 45P ACL */
  191. { MCS_BCMODE,
  192. 0x4D, 0x96, 0x1D, 0x00, 0x00, 0x01, 0xDF, 0x00,
  193. 0x00, 0x03, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. 0x01, 0x07, 0x0D, 0x13, 0x19, 0x1F, 0x25, 0x2B,
  195. 0x31, 0x37, 0x3D },
  196. /* 47P ACL */
  197. { MCS_BCMODE,
  198. 0x4D, 0x96, 0x1D, 0x00, 0x00, 0x01, 0xDF, 0x00,
  199. 0x00, 0x03, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00,
  200. 0x01, 0x07, 0x0E, 0x14, 0x1B, 0x21, 0x27, 0x2E,
  201. 0x34, 0x3B, 0x41 },
  202. /* 48P ACL */
  203. { MCS_BCMODE,
  204. 0x4D, 0x96, 0x1D, 0x00, 0x00, 0x01, 0xDF, 0x00,
  205. 0x00, 0x03, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00,
  206. 0x01, 0x08, 0x0E, 0x15, 0x1B, 0x22, 0x29, 0x2F,
  207. 0x36, 0x3C, 0x43 },
  208. /* 50P ACL */
  209. { MCS_BCMODE,
  210. 0x4D, 0x96, 0x1D, 0x00, 0x00, 0x01, 0xDF, 0x00,
  211. 0x00, 0x03, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x01, 0x08, 0x0F, 0x16, 0x1D, 0x24, 0x2A, 0x31,
  213. 0x38, 0x3F, 0x46 },
  214. };
  215. /* This tells us which ACL level goes with which gamma */
  216. static u8 const s6e63m0_acl_per_gamma[NUM_GAMMA_LEVELS] = {
  217. /* 30 - 60 cd: ACL off/NULL */
  218. 0, 0, 0, 0,
  219. /* 70 - 250 cd: 40P ACL */
  220. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  221. /* 260 - 300 cd: 50P ACL */
  222. 6, 6, 6, 6, 6,
  223. };
  224. /* The ELVSS backlight regulator has 5 levels */
  225. #define S6E63M0_ELVSS_LEVELS 5
  226. static u8 const s6e63m0_elvss_offsets[S6E63M0_ELVSS_LEVELS] = {
  227. 0x00, /* not set */
  228. 0x0D, /* 30 cd - 100 cd */
  229. 0x09, /* 110 cd - 160 cd */
  230. 0x07, /* 170 cd - 200 cd */
  231. 0x00, /* 210 cd - 300 cd */
  232. };
  233. /* This tells us which ELVSS level goes with which gamma */
  234. static u8 const s6e63m0_elvss_per_gamma[NUM_GAMMA_LEVELS] = {
  235. /* 30 - 100 cd */
  236. 1, 1, 1, 1, 1, 1, 1, 1,
  237. /* 110 - 160 cd */
  238. 2, 2, 2, 2, 2, 2,
  239. /* 170 - 200 cd */
  240. 3, 3, 3, 3,
  241. /* 210 - 300 cd */
  242. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  243. };
  244. struct s6e63m0 {
  245. struct device *dev;
  246. void *transport_data;
  247. int (*dcs_read)(struct device *dev, void *trsp, const u8 cmd, u8 *val);
  248. int (*dcs_write)(struct device *dev, void *trsp, const u8 *data, size_t len);
  249. struct drm_panel panel;
  250. struct backlight_device *bl_dev;
  251. u8 lcd_type;
  252. u8 elvss_pulse;
  253. bool dsi_mode;
  254. struct regulator_bulk_data supplies[2];
  255. struct gpio_desc *reset_gpio;
  256. bool prepared;
  257. bool enabled;
  258. /*
  259. * This field is tested by functions directly accessing bus before
  260. * transfer, transfer is skipped if it is set. In case of transfer
  261. * failure or unexpected response the field is set to error value.
  262. * Such construct allows to eliminate many checks in higher level
  263. * functions.
  264. */
  265. int error;
  266. };
  267. static const struct drm_display_mode default_mode = {
  268. .clock = 25628,
  269. .hdisplay = 480,
  270. .hsync_start = 480 + 16,
  271. .hsync_end = 480 + 16 + 2,
  272. .htotal = 480 + 16 + 2 + 16,
  273. .vdisplay = 800,
  274. .vsync_start = 800 + 28,
  275. .vsync_end = 800 + 28 + 2,
  276. .vtotal = 800 + 28 + 2 + 1,
  277. .width_mm = 53,
  278. .height_mm = 89,
  279. .flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC,
  280. };
  281. static inline struct s6e63m0 *panel_to_s6e63m0(struct drm_panel *panel)
  282. {
  283. return container_of(panel, struct s6e63m0, panel);
  284. }
  285. static int s6e63m0_clear_error(struct s6e63m0 *ctx)
  286. {
  287. int ret = ctx->error;
  288. ctx->error = 0;
  289. return ret;
  290. }
  291. static void s6e63m0_dcs_read(struct s6e63m0 *ctx, const u8 cmd, u8 *data)
  292. {
  293. if (ctx->error < 0)
  294. return;
  295. ctx->error = ctx->dcs_read(ctx->dev, ctx->transport_data, cmd, data);
  296. }
  297. static void s6e63m0_dcs_write(struct s6e63m0 *ctx, const u8 *data, size_t len)
  298. {
  299. if (ctx->error < 0 || len == 0)
  300. return;
  301. ctx->error = ctx->dcs_write(ctx->dev, ctx->transport_data, data, len);
  302. }
  303. #define s6e63m0_dcs_write_seq_static(ctx, seq ...) \
  304. ({ \
  305. static const u8 d[] = { seq }; \
  306. s6e63m0_dcs_write(ctx, d, ARRAY_SIZE(d)); \
  307. })
  308. static int s6e63m0_check_lcd_type(struct s6e63m0 *ctx)
  309. {
  310. u8 id1, id2, id3;
  311. int ret;
  312. s6e63m0_dcs_read(ctx, MCS_READ_ID1, &id1);
  313. s6e63m0_dcs_read(ctx, MCS_READ_ID2, &id2);
  314. s6e63m0_dcs_read(ctx, MCS_READ_ID3, &id3);
  315. ret = s6e63m0_clear_error(ctx);
  316. if (ret) {
  317. dev_err(ctx->dev, "error checking LCD type (%d)\n", ret);
  318. ctx->lcd_type = 0x00;
  319. return ret;
  320. }
  321. dev_info(ctx->dev, "MTP ID: %02x %02x %02x\n", id1, id2, id3);
  322. /*
  323. * We attempt to detect what panel is mounted on the controller.
  324. * The third ID byte represents the desired ELVSS pulse for
  325. * some displays.
  326. */
  327. switch (id2) {
  328. case S6E63M0_LCD_ID_VALUE_M2:
  329. dev_info(ctx->dev, "detected LCD panel AMS397GE MIPI M2\n");
  330. ctx->elvss_pulse = id3;
  331. break;
  332. case S6E63M0_LCD_ID_VALUE_SM2:
  333. case S6E63M0_LCD_ID_VALUE_SM2_1:
  334. dev_info(ctx->dev, "detected LCD panel AMS397GE MIPI SM2\n");
  335. ctx->elvss_pulse = id3;
  336. break;
  337. default:
  338. dev_info(ctx->dev, "unknown LCD panel type %02x\n", id2);
  339. /* Default ELVSS pulse level */
  340. ctx->elvss_pulse = 0x16;
  341. break;
  342. }
  343. ctx->lcd_type = id2;
  344. return 0;
  345. }
  346. static void s6e63m0_init(struct s6e63m0 *ctx)
  347. {
  348. /*
  349. * We do not know why there is a difference in the DSI mode.
  350. * (No datasheet.)
  351. *
  352. * In the vendor driver this sequence is called
  353. * "SEQ_PANEL_CONDITION_SET" or "DCS_CMD_SEQ_PANEL_COND_SET".
  354. */
  355. if (ctx->dsi_mode)
  356. s6e63m0_dcs_write_seq_static(ctx, MCS_PANELCTL,
  357. 0x01, 0x2c, 0x2c, 0x07, 0x07, 0x5f, 0xb3,
  358. 0x6d, 0x97, 0x1d, 0x3a, 0x0f, 0x00, 0x00);
  359. else
  360. s6e63m0_dcs_write_seq_static(ctx, MCS_PANELCTL,
  361. 0x01, 0x27, 0x27, 0x07, 0x07, 0x54, 0x9f,
  362. 0x63, 0x8f, 0x1a, 0x33, 0x0d, 0x00, 0x00);
  363. s6e63m0_dcs_write_seq_static(ctx, MCS_DISCTL,
  364. 0x02, 0x03, 0x1c, 0x10, 0x10);
  365. s6e63m0_dcs_write_seq_static(ctx, MCS_IFCTL,
  366. 0x03, 0x00, 0x00);
  367. s6e63m0_dcs_write_seq_static(ctx, MCS_PGAMMACTL,
  368. 0x00, 0x18, 0x08, 0x24, 0x64, 0x56, 0x33,
  369. 0xb6, 0xba, 0xa8, 0xac, 0xb1, 0x9d, 0xc1,
  370. 0xc1, 0xb7, 0x00, 0x9c, 0x00, 0x9f, 0x00,
  371. 0xd6);
  372. s6e63m0_dcs_write_seq_static(ctx, MCS_PGAMMACTL,
  373. 0x01);
  374. s6e63m0_dcs_write_seq_static(ctx, MCS_SRCCTL,
  375. 0x00, 0x8e, 0x07);
  376. s6e63m0_dcs_write_seq_static(ctx, MCS_PENTILE_1, 0x6c);
  377. s6e63m0_dcs_write_seq_static(ctx, MCS_GAMMA_DELTA_Y_RED,
  378. 0x2c, 0x12, 0x0c, 0x0a, 0x10, 0x0e, 0x17,
  379. 0x13, 0x1f, 0x1a, 0x2a, 0x24, 0x1f, 0x1b,
  380. 0x1a, 0x17, 0x2b, 0x26, 0x22, 0x20, 0x3a,
  381. 0x34, 0x30, 0x2c, 0x29, 0x26, 0x25, 0x23,
  382. 0x21, 0x20, 0x1e, 0x1e);
  383. s6e63m0_dcs_write_seq_static(ctx, MCS_GAMMA_DELTA_X_RED,
  384. 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x44,
  385. 0x44, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66,
  386. 0x66, 0x66);
  387. s6e63m0_dcs_write_seq_static(ctx, MCS_GAMMA_DELTA_Y_GREEN,
  388. 0x2c, 0x12, 0x0c, 0x0a, 0x10, 0x0e, 0x17,
  389. 0x13, 0x1f, 0x1a, 0x2a, 0x24, 0x1f, 0x1b,
  390. 0x1a, 0x17, 0x2b, 0x26, 0x22, 0x20, 0x3a,
  391. 0x34, 0x30, 0x2c, 0x29, 0x26, 0x25, 0x23,
  392. 0x21, 0x20, 0x1e, 0x1e);
  393. s6e63m0_dcs_write_seq_static(ctx, MCS_GAMMA_DELTA_X_GREEN,
  394. 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x44,
  395. 0x44, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66,
  396. 0x66, 0x66);
  397. s6e63m0_dcs_write_seq_static(ctx, MCS_GAMMA_DELTA_Y_BLUE,
  398. 0x2c, 0x12, 0x0c, 0x0a, 0x10, 0x0e, 0x17,
  399. 0x13, 0x1f, 0x1a, 0x2a, 0x24, 0x1f, 0x1b,
  400. 0x1a, 0x17, 0x2b, 0x26, 0x22, 0x20, 0x3a,
  401. 0x34, 0x30, 0x2c, 0x29, 0x26, 0x25, 0x23,
  402. 0x21, 0x20, 0x1e, 0x1e);
  403. s6e63m0_dcs_write_seq_static(ctx, MCS_GAMMA_DELTA_X_BLUE,
  404. 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x44,
  405. 0x44, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66,
  406. 0x66, 0x66);
  407. s6e63m0_dcs_write_seq_static(ctx, MCS_BCMODE,
  408. 0x4d, 0x96, 0x1d, 0x00, 0x00, 0x01, 0xdf,
  409. 0x00, 0x00, 0x03, 0x1f, 0x00, 0x00, 0x00,
  410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x06,
  411. 0x09, 0x0d, 0x0f, 0x12, 0x15, 0x18);
  412. s6e63m0_dcs_write_seq_static(ctx, MCS_TEMP_SWIRE,
  413. 0x10, 0x10, 0x0b, 0x05);
  414. s6e63m0_dcs_write_seq_static(ctx, MCS_MIECTL1,
  415. 0x01);
  416. s6e63m0_dcs_write_seq_static(ctx, MCS_ELVSS_ON,
  417. 0x0b);
  418. }
  419. static int s6e63m0_power_on(struct s6e63m0 *ctx)
  420. {
  421. int ret;
  422. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  423. if (ret < 0)
  424. return ret;
  425. msleep(25);
  426. /* Be sure to send a reset pulse */
  427. gpiod_set_value(ctx->reset_gpio, 1);
  428. msleep(5);
  429. gpiod_set_value(ctx->reset_gpio, 0);
  430. msleep(120);
  431. return 0;
  432. }
  433. static int s6e63m0_power_off(struct s6e63m0 *ctx)
  434. {
  435. int ret;
  436. gpiod_set_value(ctx->reset_gpio, 1);
  437. msleep(120);
  438. ret = regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  439. if (ret < 0)
  440. return ret;
  441. return 0;
  442. }
  443. static int s6e63m0_disable(struct drm_panel *panel)
  444. {
  445. struct s6e63m0 *ctx = panel_to_s6e63m0(panel);
  446. if (!ctx->enabled)
  447. return 0;
  448. backlight_disable(ctx->bl_dev);
  449. s6e63m0_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_OFF);
  450. msleep(10);
  451. s6e63m0_dcs_write_seq_static(ctx, MIPI_DCS_ENTER_SLEEP_MODE);
  452. msleep(120);
  453. ctx->enabled = false;
  454. return 0;
  455. }
  456. static int s6e63m0_unprepare(struct drm_panel *panel)
  457. {
  458. struct s6e63m0 *ctx = panel_to_s6e63m0(panel);
  459. int ret;
  460. if (!ctx->prepared)
  461. return 0;
  462. s6e63m0_clear_error(ctx);
  463. ret = s6e63m0_power_off(ctx);
  464. if (ret < 0)
  465. return ret;
  466. ctx->prepared = false;
  467. return 0;
  468. }
  469. static int s6e63m0_prepare(struct drm_panel *panel)
  470. {
  471. struct s6e63m0 *ctx = panel_to_s6e63m0(panel);
  472. int ret;
  473. if (ctx->prepared)
  474. return 0;
  475. ret = s6e63m0_power_on(ctx);
  476. if (ret < 0)
  477. return ret;
  478. /* Magic to unlock level 2 control of the display */
  479. s6e63m0_dcs_write_seq_static(ctx, MCS_LEVEL_2_KEY, 0x5a, 0x5a);
  480. /* Magic to unlock MTP reading */
  481. s6e63m0_dcs_write_seq_static(ctx, MCS_MTP_KEY, 0x5a, 0x5a);
  482. ret = s6e63m0_check_lcd_type(ctx);
  483. if (ret < 0)
  484. return ret;
  485. s6e63m0_init(ctx);
  486. ret = s6e63m0_clear_error(ctx);
  487. if (ret < 0)
  488. s6e63m0_unprepare(panel);
  489. ctx->prepared = true;
  490. return ret;
  491. }
  492. static int s6e63m0_enable(struct drm_panel *panel)
  493. {
  494. struct s6e63m0 *ctx = panel_to_s6e63m0(panel);
  495. if (ctx->enabled)
  496. return 0;
  497. s6e63m0_dcs_write_seq_static(ctx, MIPI_DCS_EXIT_SLEEP_MODE);
  498. msleep(120);
  499. s6e63m0_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_ON);
  500. msleep(10);
  501. s6e63m0_dcs_write_seq_static(ctx, MCS_ERROR_CHECK,
  502. 0xE7, 0x14, 0x60, 0x17, 0x0A, 0x49, 0xC3,
  503. 0x8F, 0x19, 0x64, 0x91, 0x84, 0x76, 0x20,
  504. 0x0F, 0x00);
  505. backlight_enable(ctx->bl_dev);
  506. ctx->enabled = true;
  507. return 0;
  508. }
  509. static int s6e63m0_get_modes(struct drm_panel *panel,
  510. struct drm_connector *connector)
  511. {
  512. struct drm_display_mode *mode;
  513. static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  514. mode = drm_mode_duplicate(connector->dev, &default_mode);
  515. if (!mode) {
  516. dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
  517. default_mode.hdisplay, default_mode.vdisplay,
  518. drm_mode_vrefresh(&default_mode));
  519. return -ENOMEM;
  520. }
  521. connector->display_info.width_mm = mode->width_mm;
  522. connector->display_info.height_mm = mode->height_mm;
  523. drm_display_info_set_bus_formats(&connector->display_info,
  524. &bus_format, 1);
  525. connector->display_info.bus_flags = DRM_BUS_FLAG_DE_LOW |
  526. DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
  527. drm_mode_set_name(mode);
  528. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  529. drm_mode_probed_add(connector, mode);
  530. return 1;
  531. }
  532. static const struct drm_panel_funcs s6e63m0_drm_funcs = {
  533. .disable = s6e63m0_disable,
  534. .unprepare = s6e63m0_unprepare,
  535. .prepare = s6e63m0_prepare,
  536. .enable = s6e63m0_enable,
  537. .get_modes = s6e63m0_get_modes,
  538. };
  539. static int s6e63m0_set_brightness(struct backlight_device *bd)
  540. {
  541. struct s6e63m0 *ctx = bl_get_data(bd);
  542. int brightness = bd->props.brightness;
  543. u8 elvss_val;
  544. u8 elvss_cmd_set[5];
  545. int i;
  546. /* Adjust ELVSS to candela level */
  547. i = s6e63m0_elvss_per_gamma[brightness];
  548. elvss_val = ctx->elvss_pulse + s6e63m0_elvss_offsets[i];
  549. if (elvss_val > 0x1f)
  550. elvss_val = 0x1f;
  551. elvss_cmd_set[0] = MCS_TEMP_SWIRE;
  552. elvss_cmd_set[1] = elvss_val;
  553. elvss_cmd_set[2] = elvss_val;
  554. elvss_cmd_set[3] = elvss_val;
  555. elvss_cmd_set[4] = elvss_val;
  556. s6e63m0_dcs_write(ctx, elvss_cmd_set, 5);
  557. /* Update the ACL per gamma value */
  558. i = s6e63m0_acl_per_gamma[brightness];
  559. s6e63m0_dcs_write(ctx, s6e63m0_acl[i],
  560. ARRAY_SIZE(s6e63m0_acl[i]));
  561. /* Update gamma table */
  562. s6e63m0_dcs_write(ctx, s6e63m0_gamma_22[brightness],
  563. ARRAY_SIZE(s6e63m0_gamma_22[brightness]));
  564. s6e63m0_dcs_write_seq_static(ctx, MCS_PGAMMACTL, 0x03);
  565. return s6e63m0_clear_error(ctx);
  566. }
  567. static const struct backlight_ops s6e63m0_backlight_ops = {
  568. .update_status = s6e63m0_set_brightness,
  569. };
  570. static int s6e63m0_backlight_register(struct s6e63m0 *ctx, u32 max_brightness)
  571. {
  572. struct backlight_properties props = {
  573. .type = BACKLIGHT_RAW,
  574. .brightness = max_brightness,
  575. .max_brightness = max_brightness,
  576. };
  577. struct device *dev = ctx->dev;
  578. int ret = 0;
  579. ctx->bl_dev = devm_backlight_device_register(dev, "panel", dev, ctx,
  580. &s6e63m0_backlight_ops,
  581. &props);
  582. if (IS_ERR(ctx->bl_dev)) {
  583. ret = PTR_ERR(ctx->bl_dev);
  584. dev_err(dev, "error registering backlight device (%d)\n", ret);
  585. }
  586. return ret;
  587. }
  588. int s6e63m0_probe(struct device *dev, void *trsp,
  589. int (*dcs_read)(struct device *dev, void *trsp, const u8 cmd, u8 *val),
  590. int (*dcs_write)(struct device *dev, void *trsp, const u8 *data, size_t len),
  591. bool dsi_mode)
  592. {
  593. struct s6e63m0 *ctx;
  594. u32 max_brightness;
  595. int ret;
  596. ctx = devm_kzalloc(dev, sizeof(struct s6e63m0), GFP_KERNEL);
  597. if (!ctx)
  598. return -ENOMEM;
  599. ctx->transport_data = trsp;
  600. ctx->dsi_mode = dsi_mode;
  601. ctx->dcs_read = dcs_read;
  602. ctx->dcs_write = dcs_write;
  603. dev_set_drvdata(dev, ctx);
  604. ctx->dev = dev;
  605. ctx->enabled = false;
  606. ctx->prepared = false;
  607. ret = device_property_read_u32(dev, "max-brightness", &max_brightness);
  608. if (ret)
  609. max_brightness = MAX_BRIGHTNESS;
  610. if (max_brightness > MAX_BRIGHTNESS) {
  611. dev_err(dev, "illegal max brightness specified\n");
  612. max_brightness = MAX_BRIGHTNESS;
  613. }
  614. ctx->supplies[0].supply = "vdd3";
  615. ctx->supplies[1].supply = "vci";
  616. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
  617. ctx->supplies);
  618. if (ret < 0) {
  619. dev_err(dev, "failed to get regulators: %d\n", ret);
  620. return ret;
  621. }
  622. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  623. if (IS_ERR(ctx->reset_gpio)) {
  624. dev_err(dev, "cannot get reset-gpios %ld\n", PTR_ERR(ctx->reset_gpio));
  625. return PTR_ERR(ctx->reset_gpio);
  626. }
  627. drm_panel_init(&ctx->panel, dev, &s6e63m0_drm_funcs,
  628. dsi_mode ? DRM_MODE_CONNECTOR_DSI :
  629. DRM_MODE_CONNECTOR_DPI);
  630. ret = s6e63m0_backlight_register(ctx, max_brightness);
  631. if (ret < 0)
  632. return ret;
  633. drm_panel_add(&ctx->panel);
  634. return 0;
  635. }
  636. EXPORT_SYMBOL_GPL(s6e63m0_probe);
  637. void s6e63m0_remove(struct device *dev)
  638. {
  639. struct s6e63m0 *ctx = dev_get_drvdata(dev);
  640. drm_panel_remove(&ctx->panel);
  641. }
  642. EXPORT_SYMBOL_GPL(s6e63m0_remove);
  643. MODULE_AUTHOR("Paweł Chmiel <[email protected]>");
  644. MODULE_DESCRIPTION("s6e63m0 LCD Driver");
  645. MODULE_LICENSE("GPL v2");