st7571.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Sitronix ST7571, a 4 level gray scale dot matrix LCD controller
  4. *
  5. * Copyright (C) 2025 Marcus Folkesson <marcus.folkesson@gmail.com>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <drm/clients/drm_client_setup.h>
  14. #include <drm/drm_atomic.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_connector.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_damage_helper.h>
  19. #include <drm/drm_drv.h>
  20. #include <drm/drm_encoder.h>
  21. #include <drm/drm_fb_helper.h>
  22. #include <drm/drm_fbdev_shmem.h>
  23. #include <drm/drm_fourcc.h>
  24. #include <drm/drm_framebuffer.h>
  25. #include <drm/drm_gem_atomic_helper.h>
  26. #include <drm/drm_gem_framebuffer_helper.h>
  27. #include <drm/drm_gem_shmem_helper.h>
  28. #include <drm/drm_modeset_helper_vtables.h>
  29. #include <drm/drm_module.h>
  30. #include <drm/drm_plane.h>
  31. #include <drm/drm_probe_helper.h>
  32. #include <video/display_timing.h>
  33. #include <video/of_display_timing.h>
  34. #include "st7571.h"
  35. #define ST7571_COMMAND_MODE (0x00)
  36. #define ST7571_DATA_MODE (0x40)
  37. /* Normal mode command set */
  38. #define ST7571_DISPLAY_OFF (0xae)
  39. #define ST7571_DISPLAY_ON (0xaf)
  40. #define ST7571_OSC_ON (0xab)
  41. #define ST7571_SET_COLUMN_LSB(c) (0x00 | FIELD_PREP(GENMASK(3, 0), (c)))
  42. #define ST7571_SET_COLUMN_MSB(c) (0x10 | FIELD_PREP(GENMASK(2, 0), (c) >> 4))
  43. #define ST7571_SET_COM0_LSB(x) (FIELD_PREP(GENMASK(6, 0), (x)))
  44. #define ST7571_SET_COM0_MSB (0x44)
  45. #define ST7571_SET_COM_SCAN_DIR(d) (0xc0 | FIELD_PREP(GENMASK(3, 3), (d)))
  46. #define ST7571_SET_CONTRAST_LSB(c) (FIELD_PREP(GENMASK(5, 0), (c)))
  47. #define ST7571_SET_CONTRAST_MSB (0x81)
  48. #define ST7571_SET_DISPLAY_DUTY_LSB(d) (FIELD_PREP(GENMASK(7, 0), (d)))
  49. #define ST7571_SET_DISPLAY_DUTY_MSB (0x48)
  50. #define ST7571_SET_ENTIRE_DISPLAY_ON(p) (0xa4 | FIELD_PREP(GENMASK(0, 0), (p)))
  51. #define ST7571_SET_LCD_BIAS(b) (0x50 | FIELD_PREP(GENMASK(2, 0), (b)))
  52. #define ST7571_SET_MODE_LSB(m) (FIELD_PREP(GENMASK(7, 2), (m)))
  53. #define ST7571_SET_MODE_MSB (0x38)
  54. #define ST7571_SET_PAGE(p) (0xb0 | FIELD_PREP(GENMASK(3, 0), (p)))
  55. #define ST7571_SET_POWER(p) (0x28 | FIELD_PREP(GENMASK(2, 0), (p)))
  56. #define ST7571_SET_REGULATOR_REG(r) (0x20 | FIELD_PREP(GENMASK(2, 0), (r)))
  57. #define ST7571_SET_REVERSE(r) (0xa6 | FIELD_PREP(GENMASK(0, 0), (r)))
  58. #define ST7571_SET_SEG_SCAN_DIR(d) (0xa0 | FIELD_PREP(GENMASK(0, 0), (d)))
  59. #define ST7571_SET_START_LINE_LSB(l) (FIELD_PREP(GENMASK(6, 0), (l)))
  60. #define ST7571_SET_START_LINE_MSB (0x40)
  61. /* Extension command set 3 */
  62. #define ST7571_COMMAND_SET_3 (0x7b)
  63. #define ST7571_SET_COLOR_MODE(c) (0x10 | FIELD_PREP(GENMASK(0, 0), (c)))
  64. #define ST7571_COMMAND_SET_NORMAL (0x00)
  65. /* ST7567 commands */
  66. #define ST7567_SET_LCD_BIAS(m) (0xa2 | FIELD_PREP(GENMASK(0, 0), (m)))
  67. #define ST7571_PAGE_HEIGHT 8
  68. #define DRIVER_NAME "st7571"
  69. #define DRIVER_DESC "ST7571 DRM driver"
  70. #define DRIVER_MAJOR 1
  71. #define DRIVER_MINOR 0
  72. static inline struct st7571_device *drm_to_st7571(struct drm_device *drm)
  73. {
  74. return container_of(drm, struct st7571_device, drm);
  75. }
  76. static int st7571_send_command_list(struct st7571_device *st7571,
  77. const u8 *cmd_list, size_t len)
  78. {
  79. int ret;
  80. for (int i = 0; i < len; i++) {
  81. ret = regmap_write(st7571->regmap, ST7571_COMMAND_MODE, cmd_list[i]);
  82. if (ret < 0)
  83. return ret;
  84. }
  85. return ret;
  86. }
  87. static inline u8 st7571_transform_xy(const char *p, int x, int y, u8 bpp)
  88. {
  89. int xrest = x % 8;
  90. u8 result = 0;
  91. u8 row_len = 16 * bpp;
  92. /*
  93. * Transforms an (x, y) pixel coordinate into a vertical 8-bit
  94. * column from the framebuffer. It calculates the corresponding byte in the
  95. * framebuffer, extracts the bit at the given x position across 8 consecutive
  96. * rows, and packs those bits into a single byte.
  97. *
  98. * Return an 8-bit value representing a vertical column of pixels.
  99. */
  100. x = x / 8;
  101. y = (y / 8) * 8;
  102. for (int i = 0; i < 8; i++) {
  103. int row_idx = y + i;
  104. u8 byte = p[row_idx * row_len + x];
  105. u8 bit = (byte >> xrest) & 1;
  106. result |= (bit << i);
  107. }
  108. return result;
  109. }
  110. static int st7571_set_position(struct st7571_device *st7571, int x, int y)
  111. {
  112. u8 cmd_list[] = {
  113. ST7571_SET_COLUMN_LSB(x),
  114. ST7571_SET_COLUMN_MSB(x),
  115. ST7571_SET_PAGE(y / ST7571_PAGE_HEIGHT),
  116. };
  117. return st7571_send_command_list(st7571, cmd_list, ARRAY_SIZE(cmd_list));
  118. }
  119. static int st7571_fb_clear_screen(struct st7571_device *st7571)
  120. {
  121. u32 npixels = st7571->ncols * round_up(st7571->nlines, ST7571_PAGE_HEIGHT) * st7571->bpp;
  122. char pixelvalue = 0x00;
  123. st7571_set_position(st7571, 0, 0);
  124. for (int i = 0; i < npixels; i++)
  125. regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, &pixelvalue, 1);
  126. return 0;
  127. }
  128. static void st7571_prepare_buffer_monochrome(struct st7571_device *st7571,
  129. const struct iosys_map *vmap,
  130. struct drm_framebuffer *fb,
  131. struct drm_rect *rect,
  132. struct drm_format_conv_state *fmtcnv_state)
  133. {
  134. unsigned int dst_pitch;
  135. struct iosys_map dst;
  136. u32 size;
  137. switch (fb->format->format) {
  138. case DRM_FORMAT_XRGB8888:
  139. dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8);
  140. iosys_map_set_vaddr(&dst, st7571->hwbuf);
  141. drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
  142. break;
  143. case DRM_FORMAT_R1:
  144. size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8;
  145. memcpy(st7571->hwbuf, vmap->vaddr, size);
  146. break;
  147. }
  148. }
  149. static void st7571_prepare_buffer_grayscale(struct st7571_device *st7571,
  150. const struct iosys_map *vmap,
  151. struct drm_framebuffer *fb,
  152. struct drm_rect *rect,
  153. struct drm_format_conv_state *fmtcnv_state)
  154. {
  155. u32 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8;
  156. unsigned int dst_pitch;
  157. struct iosys_map dst;
  158. switch (fb->format->format) {
  159. case DRM_FORMAT_XRGB8888:
  160. dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 4);
  161. iosys_map_set_vaddr(&dst, st7571->hwbuf);
  162. drm_fb_xrgb8888_to_gray2(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
  163. break;
  164. case DRM_FORMAT_R1:
  165. size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8;
  166. memcpy(st7571->hwbuf, vmap->vaddr, size);
  167. break;
  168. case DRM_FORMAT_R2:
  169. size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 4;
  170. memcpy(st7571->hwbuf, vmap->vaddr, size);
  171. break;
  172. }
  173. }
  174. static int st7571_fb_update_rect_monochrome(struct drm_framebuffer *fb, struct drm_rect *rect)
  175. {
  176. struct st7571_device *st7571 = drm_to_st7571(fb->dev);
  177. char *row = st7571->row;
  178. /* Align y to display page boundaries */
  179. rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT);
  180. rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines);
  181. for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) {
  182. for (int x = rect->x1; x < rect->x2; x++)
  183. row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 1);
  184. st7571_set_position(st7571, rect->x1, y);
  185. /* TODO: Investige why we can't write multiple bytes at once */
  186. for (int x = rect->x1; x < rect->x2; x++)
  187. regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1);
  188. }
  189. return 0;
  190. }
  191. static int st7571_fb_update_rect_grayscale(struct drm_framebuffer *fb, struct drm_rect *rect)
  192. {
  193. struct st7571_device *st7571 = drm_to_st7571(fb->dev);
  194. u32 format = fb->format->format;
  195. char *row = st7571->row;
  196. int x1;
  197. int x2;
  198. /* Align y to display page boundaries */
  199. rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT);
  200. rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines);
  201. switch (format) {
  202. case DRM_FORMAT_R1:
  203. x1 = rect->x1 * 1;
  204. x2 = rect->x2 * 1;
  205. break;
  206. case DRM_FORMAT_R2:
  207. fallthrough;
  208. case DRM_FORMAT_XRGB8888:
  209. x1 = rect->x1 * 2;
  210. x2 = rect->x2 * 2;
  211. break;
  212. }
  213. for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) {
  214. for (int x = x1; x < x2; x++)
  215. row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 2);
  216. st7571_set_position(st7571, rect->x1, y);
  217. /* TODO: Investige why we can't write multiple bytes at once */
  218. for (int x = x1; x < x2; x++) {
  219. regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1);
  220. /*
  221. * As the display supports grayscale, all pixels must be written as two bits
  222. * even if the format is monochrome.
  223. *
  224. * The bit values maps to the following grayscale:
  225. * 0 0 = Black
  226. * 0 1 = Dark gray
  227. * 1 0 = Light gray
  228. * 1 1 = White
  229. *
  230. * For monochrome formats, write the same value twice to get
  231. * either a black or white pixel.
  232. */
  233. if (format == DRM_FORMAT_R1)
  234. regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1);
  235. }
  236. }
  237. return 0;
  238. }
  239. static int st7571_connector_get_modes(struct drm_connector *conn)
  240. {
  241. struct st7571_device *st7571 = drm_to_st7571(conn->dev);
  242. return drm_connector_helper_get_modes_fixed(conn, &st7571->mode);
  243. }
  244. static const struct drm_connector_helper_funcs st7571_connector_helper_funcs = {
  245. .get_modes = st7571_connector_get_modes,
  246. };
  247. static const struct st7571_panel_format st7571_monochrome = {
  248. .prepare_buffer = st7571_prepare_buffer_monochrome,
  249. .update_rect = st7571_fb_update_rect_monochrome,
  250. .mode = ST7571_COLOR_MODE_BLACKWHITE,
  251. .formats = {
  252. DRM_FORMAT_XRGB8888,
  253. DRM_FORMAT_R1,
  254. },
  255. .nformats = 2,
  256. };
  257. static const struct st7571_panel_format st7571_grayscale = {
  258. .prepare_buffer = st7571_prepare_buffer_grayscale,
  259. .update_rect = st7571_fb_update_rect_grayscale,
  260. .mode = ST7571_COLOR_MODE_GRAY,
  261. .formats = {
  262. DRM_FORMAT_XRGB8888,
  263. DRM_FORMAT_R1,
  264. DRM_FORMAT_R2,
  265. },
  266. .nformats = 3,
  267. };
  268. static const u64 st7571_primary_plane_fmtmods[] = {
  269. DRM_FORMAT_MOD_LINEAR,
  270. DRM_FORMAT_MOD_INVALID
  271. };
  272. static int st7571_primary_plane_helper_atomic_check(struct drm_plane *plane,
  273. struct drm_atomic_state *state)
  274. {
  275. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  276. struct drm_crtc *new_crtc = new_plane_state->crtc;
  277. struct drm_crtc_state *new_crtc_state = NULL;
  278. if (new_crtc)
  279. new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
  280. return drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
  281. DRM_PLANE_NO_SCALING,
  282. DRM_PLANE_NO_SCALING,
  283. false, false);
  284. }
  285. static void st7571_primary_plane_helper_atomic_update(struct drm_plane *plane,
  286. struct drm_atomic_state *state)
  287. {
  288. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
  289. struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
  290. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  291. struct drm_framebuffer *fb = plane_state->fb;
  292. struct drm_atomic_helper_damage_iter iter;
  293. struct drm_device *drm = plane->dev;
  294. struct drm_rect damage;
  295. struct st7571_device *st7571 = drm_to_st7571(plane->dev);
  296. int ret, idx;
  297. if (!fb)
  298. return; /* no framebuffer; plane is disabled */
  299. ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
  300. if (ret)
  301. return;
  302. if (!drm_dev_enter(drm, &idx))
  303. goto out_drm_gem_fb_end_cpu_access;
  304. drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
  305. drm_atomic_for_each_plane_damage(&iter, &damage) {
  306. st7571->pformat->prepare_buffer(st7571,
  307. &shadow_plane_state->data[0],
  308. fb, &damage,
  309. &shadow_plane_state->fmtcnv_state);
  310. st7571->pformat->update_rect(fb, &damage);
  311. }
  312. drm_dev_exit(idx);
  313. out_drm_gem_fb_end_cpu_access:
  314. drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
  315. }
  316. static void st7571_primary_plane_helper_atomic_disable(struct drm_plane *plane,
  317. struct drm_atomic_state *state)
  318. {
  319. struct drm_device *drm = plane->dev;
  320. struct st7571_device *st7571 = drm_to_st7571(plane->dev);
  321. int idx;
  322. if (!drm_dev_enter(drm, &idx))
  323. return;
  324. st7571_fb_clear_screen(st7571);
  325. drm_dev_exit(idx);
  326. }
  327. static const struct drm_plane_helper_funcs st7571_primary_plane_helper_funcs = {
  328. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  329. .atomic_check = st7571_primary_plane_helper_atomic_check,
  330. .atomic_update = st7571_primary_plane_helper_atomic_update,
  331. .atomic_disable = st7571_primary_plane_helper_atomic_disable,
  332. };
  333. static const struct drm_plane_funcs st7571_primary_plane_funcs = {
  334. .update_plane = drm_atomic_helper_update_plane,
  335. .disable_plane = drm_atomic_helper_disable_plane,
  336. .destroy = drm_plane_cleanup,
  337. DRM_GEM_SHADOW_PLANE_FUNCS,
  338. };
  339. /*
  340. * CRTC
  341. */
  342. static enum drm_mode_status st7571_crtc_mode_valid(struct drm_crtc *crtc,
  343. const struct drm_display_mode *mode)
  344. {
  345. struct st7571_device *st7571 = drm_to_st7571(crtc->dev);
  346. return drm_crtc_helper_mode_valid_fixed(crtc, mode, &st7571->mode);
  347. }
  348. static const struct drm_crtc_helper_funcs st7571_crtc_helper_funcs = {
  349. .atomic_check = drm_crtc_helper_atomic_check,
  350. .mode_valid = st7571_crtc_mode_valid,
  351. };
  352. static const struct drm_crtc_funcs st7571_crtc_funcs = {
  353. .reset = drm_atomic_helper_crtc_reset,
  354. .destroy = drm_crtc_cleanup,
  355. .set_config = drm_atomic_helper_set_config,
  356. .page_flip = drm_atomic_helper_page_flip,
  357. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  358. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  359. };
  360. /*
  361. * Encoder
  362. */
  363. static void st7571_encoder_atomic_enable(struct drm_encoder *encoder,
  364. struct drm_atomic_state *state)
  365. {
  366. struct drm_device *drm = encoder->dev;
  367. struct st7571_device *st7571 = drm_to_st7571(drm);
  368. u8 command = ST7571_DISPLAY_ON;
  369. int ret;
  370. ret = st7571->pdata->init(st7571);
  371. if (ret)
  372. return;
  373. st7571_send_command_list(st7571, &command, 1);
  374. }
  375. static void st7571_encoder_atomic_disable(struct drm_encoder *encoder,
  376. struct drm_atomic_state *state)
  377. {
  378. struct drm_device *drm = encoder->dev;
  379. struct st7571_device *st7571 = drm_to_st7571(drm);
  380. u8 command = ST7571_DISPLAY_OFF;
  381. st7571_send_command_list(st7571, &command, 1);
  382. }
  383. static const struct drm_encoder_funcs st7571_encoder_funcs = {
  384. .destroy = drm_encoder_cleanup,
  385. };
  386. static const struct drm_encoder_helper_funcs st7571_encoder_helper_funcs = {
  387. .atomic_enable = st7571_encoder_atomic_enable,
  388. .atomic_disable = st7571_encoder_atomic_disable,
  389. };
  390. /*
  391. * Connector
  392. */
  393. static const struct drm_connector_funcs st7571_connector_funcs = {
  394. .reset = drm_atomic_helper_connector_reset,
  395. .fill_modes = drm_helper_probe_single_connector_modes,
  396. .destroy = drm_connector_cleanup,
  397. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  398. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  399. };
  400. static const struct drm_mode_config_funcs st7571_mode_config_funcs = {
  401. .fb_create = drm_gem_fb_create_with_dirty,
  402. .atomic_check = drm_atomic_helper_check,
  403. .atomic_commit = drm_atomic_helper_commit,
  404. };
  405. static struct drm_display_mode st7571_mode(struct st7571_device *st7571)
  406. {
  407. struct drm_display_mode mode = {
  408. DRM_SIMPLE_MODE(st7571->ncols, st7571->nlines,
  409. st7571->width_mm, st7571->height_mm),
  410. };
  411. return mode;
  412. }
  413. static int st7571_mode_config_init(struct st7571_device *st7571)
  414. {
  415. struct drm_device *drm = &st7571->drm;
  416. const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints;
  417. int ret;
  418. ret = drmm_mode_config_init(drm);
  419. if (ret)
  420. return ret;
  421. drm->mode_config.min_width = constraints->min_ncols;
  422. drm->mode_config.min_height = constraints->min_nlines;
  423. drm->mode_config.max_width = constraints->max_ncols;
  424. drm->mode_config.max_height = constraints->max_nlines;
  425. drm->mode_config.preferred_depth = 24;
  426. drm->mode_config.funcs = &st7571_mode_config_funcs;
  427. return 0;
  428. }
  429. static int st7571_plane_init(struct st7571_device *st7571,
  430. const struct st7571_panel_format *pformat)
  431. {
  432. struct drm_plane *primary_plane = &st7571->primary_plane;
  433. struct drm_device *drm = &st7571->drm;
  434. int ret;
  435. ret = drm_universal_plane_init(drm, primary_plane, 0,
  436. &st7571_primary_plane_funcs,
  437. pformat->formats,
  438. pformat->nformats,
  439. st7571_primary_plane_fmtmods,
  440. DRM_PLANE_TYPE_PRIMARY, NULL);
  441. if (ret)
  442. return ret;
  443. drm_plane_helper_add(primary_plane, &st7571_primary_plane_helper_funcs);
  444. drm_plane_enable_fb_damage_clips(primary_plane);
  445. return 0;
  446. }
  447. static int st7571_crtc_init(struct st7571_device *st7571)
  448. {
  449. struct drm_plane *primary_plane = &st7571->primary_plane;
  450. struct drm_crtc *crtc = &st7571->crtc;
  451. struct drm_device *drm = &st7571->drm;
  452. int ret;
  453. ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
  454. &st7571_crtc_funcs, NULL);
  455. if (ret)
  456. return ret;
  457. drm_crtc_helper_add(crtc, &st7571_crtc_helper_funcs);
  458. return 0;
  459. }
  460. static int st7571_encoder_init(struct st7571_device *st7571)
  461. {
  462. struct drm_encoder *encoder = &st7571->encoder;
  463. struct drm_crtc *crtc = &st7571->crtc;
  464. struct drm_device *drm = &st7571->drm;
  465. int ret;
  466. ret = drm_encoder_init(drm, encoder, &st7571_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL);
  467. if (ret)
  468. return ret;
  469. drm_encoder_helper_add(encoder, &st7571_encoder_helper_funcs);
  470. encoder->possible_crtcs = drm_crtc_mask(crtc);
  471. return 0;
  472. }
  473. static int st7571_connector_init(struct st7571_device *st7571)
  474. {
  475. struct drm_connector *connector = &st7571->connector;
  476. struct drm_encoder *encoder = &st7571->encoder;
  477. struct drm_device *drm = &st7571->drm;
  478. int ret;
  479. ret = drm_connector_init(drm, connector, &st7571_connector_funcs,
  480. DRM_MODE_CONNECTOR_Unknown);
  481. if (ret)
  482. return ret;
  483. drm_connector_helper_add(connector, &st7571_connector_helper_funcs);
  484. return drm_connector_attach_encoder(connector, encoder);
  485. }
  486. DEFINE_DRM_GEM_FOPS(st7571_fops);
  487. static const struct drm_driver st7571_driver = {
  488. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  489. .name = DRIVER_NAME,
  490. .desc = DRIVER_DESC,
  491. .major = DRIVER_MAJOR,
  492. .minor = DRIVER_MINOR,
  493. .fops = &st7571_fops,
  494. DRM_GEM_SHMEM_DRIVER_OPS,
  495. DRM_FBDEV_SHMEM_DRIVER_OPS,
  496. };
  497. static int st7571_validate_parameters(struct st7571_device *st7571)
  498. {
  499. struct device *dev = st7571->dev;
  500. const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints;
  501. if (st7571->width_mm == 0) {
  502. dev_err(dev, "Invalid panel width\n");
  503. return -EINVAL;
  504. }
  505. if (st7571->height_mm == 0) {
  506. dev_err(dev, "Invalid panel height\n");
  507. return -EINVAL;
  508. }
  509. if (st7571->nlines < constraints->min_nlines ||
  510. st7571->nlines > constraints->max_nlines) {
  511. dev_err(dev, "Invalid timing configuration.\n");
  512. return -EINVAL;
  513. }
  514. if (st7571->startline + st7571->nlines > constraints->max_nlines) {
  515. dev_err(dev, "Invalid timing configuration.\n");
  516. return -EINVAL;
  517. }
  518. if (st7571->ncols < constraints->min_ncols ||
  519. st7571->ncols > constraints->max_ncols) {
  520. dev_err(dev, "Invalid timing configuration.\n");
  521. return -EINVAL;
  522. }
  523. if (st7571->grayscale && !constraints->support_grayscale) {
  524. dev_err(dev, "Grayscale not supported\n");
  525. return -EINVAL;
  526. }
  527. return 0;
  528. }
  529. static int st7567_parse_dt(struct st7571_device *st7567)
  530. {
  531. struct device *dev = st7567->dev;
  532. struct device_node *np = dev->of_node;
  533. struct display_timing dt;
  534. int ret;
  535. ret = of_get_display_timing(np, "panel-timing", &dt);
  536. if (ret) {
  537. dev_err(dev, "Failed to get display timing from DT\n");
  538. return ret;
  539. }
  540. of_property_read_u32(np, "width-mm", &st7567->width_mm);
  541. of_property_read_u32(np, "height-mm", &st7567->height_mm);
  542. st7567->inverted = of_property_read_bool(np, "sitronix,inverted");
  543. st7567->pformat = &st7571_monochrome;
  544. st7567->bpp = 1;
  545. st7567->startline = dt.vfront_porch.typ;
  546. st7567->nlines = dt.vactive.typ;
  547. st7567->ncols = dt.hactive.typ;
  548. return 0;
  549. }
  550. static int st7571_parse_dt(struct st7571_device *st7571)
  551. {
  552. struct device *dev = st7571->dev;
  553. struct device_node *np = dev->of_node;
  554. struct display_timing dt;
  555. int ret;
  556. ret = of_get_display_timing(np, "panel-timing", &dt);
  557. if (ret) {
  558. dev_err(dev, "Failed to get display timing from DT\n");
  559. return ret;
  560. }
  561. of_property_read_u32(np, "width-mm", &st7571->width_mm);
  562. of_property_read_u32(np, "height-mm", &st7571->height_mm);
  563. st7571->grayscale = of_property_read_bool(np, "sitronix,grayscale");
  564. st7571->inverted = of_property_read_bool(np, "sitronix,inverted");
  565. if (st7571->grayscale) {
  566. st7571->pformat = &st7571_grayscale;
  567. st7571->bpp = 2;
  568. } else {
  569. st7571->pformat = &st7571_monochrome;
  570. st7571->bpp = 1;
  571. }
  572. st7571->startline = dt.vfront_porch.typ;
  573. st7571->nlines = dt.vactive.typ;
  574. st7571->ncols = dt.hactive.typ;
  575. st7571->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  576. if (IS_ERR(st7571->reset))
  577. return dev_err_probe(dev, PTR_ERR(st7571->reset),
  578. "Failed to get reset gpio\n");
  579. return 0;
  580. }
  581. static void st7571_reset(struct st7571_device *st7571)
  582. {
  583. gpiod_set_value_cansleep(st7571->reset, 1);
  584. fsleep(20);
  585. gpiod_set_value_cansleep(st7571->reset, 0);
  586. }
  587. static int st7567_lcd_init(struct st7571_device *st7567)
  588. {
  589. /*
  590. * Most of the initialization sequence is taken directly from the
  591. * referential initial code in the ST7567 datasheet.
  592. */
  593. u8 commands[] = {
  594. ST7571_DISPLAY_OFF,
  595. ST7567_SET_LCD_BIAS(1),
  596. ST7571_SET_SEG_SCAN_DIR(0),
  597. ST7571_SET_COM_SCAN_DIR(1),
  598. ST7571_SET_REGULATOR_REG(4),
  599. ST7571_SET_CONTRAST_MSB,
  600. ST7571_SET_CONTRAST_LSB(0x20),
  601. ST7571_SET_START_LINE_MSB,
  602. ST7571_SET_START_LINE_LSB(st7567->startline),
  603. ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */
  604. ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */
  605. ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */
  606. ST7571_SET_REVERSE(st7567->inverted ? 1 : 0),
  607. ST7571_SET_ENTIRE_DISPLAY_ON(0),
  608. };
  609. return st7571_send_command_list(st7567, commands, ARRAY_SIZE(commands));
  610. }
  611. static int st7571_lcd_init(struct st7571_device *st7571)
  612. {
  613. /*
  614. * Most of the initialization sequence is taken directly from the
  615. * referential initial code in the ST7571 datasheet.
  616. */
  617. u8 commands[] = {
  618. ST7571_DISPLAY_OFF,
  619. ST7571_SET_MODE_MSB,
  620. ST7571_SET_MODE_LSB(0x2e),
  621. ST7571_SET_SEG_SCAN_DIR(0),
  622. ST7571_SET_COM_SCAN_DIR(1),
  623. ST7571_SET_COM0_MSB,
  624. ST7571_SET_COM0_LSB(0x00),
  625. ST7571_SET_START_LINE_MSB,
  626. ST7571_SET_START_LINE_LSB(st7571->startline),
  627. ST7571_OSC_ON,
  628. ST7571_SET_REGULATOR_REG(5),
  629. ST7571_SET_CONTRAST_MSB,
  630. ST7571_SET_CONTRAST_LSB(0x33),
  631. ST7571_SET_LCD_BIAS(0x04),
  632. ST7571_SET_DISPLAY_DUTY_MSB,
  633. ST7571_SET_DISPLAY_DUTY_LSB(st7571->nlines),
  634. ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */
  635. ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */
  636. ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */
  637. ST7571_COMMAND_SET_3,
  638. ST7571_SET_COLOR_MODE(st7571->pformat->mode),
  639. ST7571_COMMAND_SET_NORMAL,
  640. ST7571_SET_REVERSE(st7571->inverted ? 1 : 0),
  641. ST7571_SET_ENTIRE_DISPLAY_ON(0),
  642. };
  643. /* Perform a reset before initializing the controller */
  644. st7571_reset(st7571);
  645. return st7571_send_command_list(st7571, commands, ARRAY_SIZE(commands));
  646. }
  647. struct st7571_device *st7571_probe(struct device *dev,
  648. struct regmap *regmap)
  649. {
  650. struct st7571_device *st7571;
  651. struct drm_device *drm;
  652. int ret;
  653. st7571 = devm_drm_dev_alloc(dev, &st7571_driver,
  654. struct st7571_device, drm);
  655. if (IS_ERR(st7571))
  656. return st7571;
  657. drm = &st7571->drm;
  658. st7571->dev = dev;
  659. st7571->pdata = device_get_match_data(st7571->dev);
  660. ret = st7571->pdata->parse_dt(st7571);
  661. if (ret)
  662. return ERR_PTR(ret);
  663. ret = st7571_validate_parameters(st7571);
  664. if (ret)
  665. return ERR_PTR(ret);
  666. st7571->mode = st7571_mode(st7571);
  667. st7571->regmap = regmap;
  668. st7571->hwbuf = devm_kzalloc(st7571->dev,
  669. (st7571->nlines * st7571->ncols * st7571->bpp) / 8,
  670. GFP_KERNEL);
  671. if (!st7571->hwbuf)
  672. return ERR_PTR(-ENOMEM);
  673. st7571->row = devm_kzalloc(st7571->dev,
  674. (st7571->ncols * st7571->bpp),
  675. GFP_KERNEL);
  676. if (!st7571->row)
  677. return ERR_PTR(-ENOMEM);
  678. ret = st7571_mode_config_init(st7571);
  679. if (ret) {
  680. dev_err(st7571->dev, "Failed to initialize mode config\n");
  681. return ERR_PTR(ret);
  682. }
  683. ret = st7571_plane_init(st7571, st7571->pformat);
  684. if (ret) {
  685. dev_err(st7571->dev, "Failed to initialize primary plane\n");
  686. return ERR_PTR(ret);
  687. }
  688. ret = st7571_crtc_init(st7571);
  689. if (ret < 0) {
  690. dev_err(st7571->dev, "Failed to initialize CRTC\n");
  691. return ERR_PTR(ret);
  692. }
  693. ret = st7571_encoder_init(st7571);
  694. if (ret < 0) {
  695. dev_err(st7571->dev, "Failed to initialize encoder\n");
  696. return ERR_PTR(ret);
  697. }
  698. ret = st7571_connector_init(st7571);
  699. if (ret < 0) {
  700. dev_err(st7571->dev, "Failed to initialize connector\n");
  701. return ERR_PTR(ret);
  702. }
  703. drm_mode_config_reset(drm);
  704. ret = drm_dev_register(drm, 0);
  705. if (ret) {
  706. dev_err(st7571->dev, "Failed to register DRM device\n");
  707. return ERR_PTR(ret);
  708. }
  709. drm_client_setup(drm, NULL);
  710. return st7571;
  711. }
  712. EXPORT_SYMBOL_GPL(st7571_probe);
  713. void st7571_remove(struct st7571_device *st7571)
  714. {
  715. drm_dev_unplug(&st7571->drm);
  716. }
  717. EXPORT_SYMBOL_GPL(st7571_remove);
  718. const struct st7571_panel_data st7567_config = {
  719. .init = st7567_lcd_init,
  720. .parse_dt = st7567_parse_dt,
  721. .constraints = {
  722. .min_nlines = 1,
  723. .max_nlines = 64,
  724. .min_ncols = 128,
  725. .max_ncols = 128,
  726. .support_grayscale = false,
  727. },
  728. };
  729. EXPORT_SYMBOL_NS_GPL(st7567_config, "DRM_ST7571");
  730. const struct st7571_panel_data st7571_config = {
  731. .init = st7571_lcd_init,
  732. .parse_dt = st7571_parse_dt,
  733. .constraints = {
  734. .min_nlines = 1,
  735. .max_nlines = 128,
  736. .min_ncols = 128,
  737. .max_ncols = 128,
  738. .support_grayscale = true,
  739. },
  740. };
  741. EXPORT_SYMBOL_NS_GPL(st7571_config, "DRM_ST7571");
  742. MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>");
  743. MODULE_DESCRIPTION("DRM Driver for Sitronix ST7571 LCD controller");
  744. MODULE_LICENSE("GPL");