panel-mipi-dbi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DRM driver for MIPI DBI compatible display panels
  4. *
  5. * Copyright 2022 Noralf Trønnes
  6. */
  7. #include <linux/backlight.h>
  8. #include <linux/delay.h>
  9. #include <linux/firmware.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/module.h>
  12. #include <linux/property.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/spi/spi.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_client_setup.h>
  17. #include <drm/drm_drv.h>
  18. #include <drm/drm_fbdev_dma.h>
  19. #include <drm/drm_gem_atomic_helper.h>
  20. #include <drm/drm_gem_dma_helper.h>
  21. #include <drm/drm_managed.h>
  22. #include <drm/drm_mipi_dbi.h>
  23. #include <drm/drm_modes.h>
  24. #include <drm/drm_modeset_helper.h>
  25. #include <video/mipi_display.h>
  26. struct panel_mipi_dbi_format {
  27. const char *name;
  28. u32 fourcc;
  29. unsigned int bpp;
  30. };
  31. static const struct panel_mipi_dbi_format panel_mipi_dbi_formats[] = {
  32. { "r5g6b5", DRM_FORMAT_RGB565, 16 },
  33. { "b6x2g6x2r6x2", DRM_FORMAT_RGB888, 24 },
  34. };
  35. static int panel_mipi_dbi_get_format(struct device *dev, u32 *formats, unsigned int *bpp)
  36. {
  37. const char *format_name;
  38. unsigned int i;
  39. int ret;
  40. formats[1] = DRM_FORMAT_XRGB8888;
  41. ret = device_property_read_string(dev, "format", &format_name);
  42. if (ret) {
  43. /* Old Device Trees don't have this property */
  44. formats[0] = DRM_FORMAT_RGB565;
  45. *bpp = 16;
  46. return 0;
  47. }
  48. for (i = 0; i < ARRAY_SIZE(panel_mipi_dbi_formats); i++) {
  49. const struct panel_mipi_dbi_format *format = &panel_mipi_dbi_formats[i];
  50. if (strcmp(format_name, format->name))
  51. continue;
  52. formats[0] = format->fourcc;
  53. *bpp = format->bpp;
  54. return 0;
  55. }
  56. dev_err(dev, "Pixel format is not supported: '%s'\n", format_name);
  57. return -EINVAL;
  58. }
  59. static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
  60. 0, 0, 0, 0, 0, 0, 0 };
  61. /*
  62. * The display controller configuration is stored in a firmware file.
  63. * The Device Tree 'compatible' property value with a '.bin' suffix is passed
  64. * to request_firmware() to fetch this file.
  65. */
  66. struct panel_mipi_dbi_config {
  67. /* Magic string: panel_mipi_dbi_magic */
  68. u8 magic[15];
  69. /* Config file format version */
  70. u8 file_format_version;
  71. /*
  72. * MIPI commands to execute when the display pipeline is enabled.
  73. * This is used to configure the display controller.
  74. *
  75. * The commands are stored in a byte array with the format:
  76. * command, num_parameters, [ parameter, ...], command, ...
  77. *
  78. * Some commands require a pause before the next command can be received.
  79. * Inserting a delay in the command sequence is done by using the NOP command with one
  80. * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
  81. * Command Set where it has no parameters).
  82. *
  83. * Example:
  84. * command 0x11
  85. * sleep 120ms
  86. * command 0xb1 parameters 0x01, 0x2c, 0x2d
  87. * command 0x29
  88. *
  89. * Byte sequence:
  90. * 0x11 0x00
  91. * 0x00 0x01 0x78
  92. * 0xb1 0x03 0x01 0x2c 0x2d
  93. * 0x29 0x00
  94. */
  95. u8 commands[];
  96. };
  97. struct panel_mipi_dbi_commands {
  98. const u8 *buf;
  99. size_t len;
  100. };
  101. static struct panel_mipi_dbi_commands *
  102. panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
  103. {
  104. const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
  105. struct panel_mipi_dbi_commands *commands;
  106. size_t size = fw->size, commands_len;
  107. unsigned int i = 0;
  108. if (size < sizeof(*config) + 2) { /* At least 1 command */
  109. dev_err(dev, "config: file size=%zu is too small\n", size);
  110. return ERR_PTR(-EINVAL);
  111. }
  112. if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
  113. dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
  114. return ERR_PTR(-EINVAL);
  115. }
  116. if (config->file_format_version != 1) {
  117. dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
  118. return ERR_PTR(-EINVAL);
  119. }
  120. drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
  121. commands_len = size - sizeof(*config);
  122. while ((i + 1) < commands_len) {
  123. u8 command = config->commands[i++];
  124. u8 num_parameters = config->commands[i++];
  125. const u8 *parameters = &config->commands[i];
  126. i += num_parameters;
  127. if (i > commands_len) {
  128. dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
  129. command, num_parameters);
  130. return ERR_PTR(-EINVAL);
  131. }
  132. if (command == 0x00 && num_parameters == 1)
  133. drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
  134. else
  135. drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
  136. command, num_parameters, parameters);
  137. }
  138. if (i != commands_len) {
  139. dev_err(dev, "config: malformed command array\n");
  140. return ERR_PTR(-EINVAL);
  141. }
  142. commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
  143. if (!commands)
  144. return ERR_PTR(-ENOMEM);
  145. commands->len = commands_len;
  146. commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
  147. if (!commands->buf)
  148. return ERR_PTR(-ENOMEM);
  149. return commands;
  150. }
  151. static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
  152. {
  153. struct panel_mipi_dbi_commands *commands;
  154. const struct firmware *fw;
  155. const char *compatible;
  156. char fw_name[40];
  157. int ret;
  158. ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
  159. if (ret)
  160. return ERR_PTR(ret);
  161. snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
  162. ret = request_firmware(&fw, fw_name, dev);
  163. if (ret) {
  164. dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
  165. compatible, ret);
  166. return ERR_PTR(ret);
  167. }
  168. commands = panel_mipi_dbi_check_commands(dev, fw);
  169. release_firmware(fw);
  170. return commands;
  171. }
  172. static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
  173. struct panel_mipi_dbi_commands *commands)
  174. {
  175. unsigned int i = 0;
  176. if (!commands)
  177. return;
  178. while (i < commands->len) {
  179. u8 command = commands->buf[i++];
  180. u8 num_parameters = commands->buf[i++];
  181. const u8 *parameters = &commands->buf[i];
  182. if (command == 0x00 && num_parameters == 1)
  183. msleep(parameters[0]);
  184. else if (num_parameters)
  185. mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
  186. else
  187. mipi_dbi_command(dbi, command);
  188. i += num_parameters;
  189. }
  190. }
  191. static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
  192. struct drm_crtc_state *crtc_state,
  193. struct drm_plane_state *plane_state)
  194. {
  195. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  196. struct mipi_dbi *dbi = &dbidev->dbi;
  197. int ret, idx;
  198. if (!drm_dev_enter(pipe->crtc.dev, &idx))
  199. return;
  200. drm_dbg(pipe->crtc.dev, "\n");
  201. ret = mipi_dbi_poweron_conditional_reset(dbidev);
  202. if (ret < 0)
  203. goto out_exit;
  204. if (!ret)
  205. panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
  206. mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
  207. out_exit:
  208. drm_dev_exit(idx);
  209. }
  210. static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
  211. DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(panel_mipi_dbi_enable),
  212. };
  213. DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops);
  214. static const struct drm_driver panel_mipi_dbi_driver = {
  215. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  216. .fops = &panel_mipi_dbi_fops,
  217. DRM_GEM_DMA_DRIVER_OPS_VMAP,
  218. DRM_FBDEV_DMA_DRIVER_OPS,
  219. .debugfs_init = mipi_dbi_debugfs_init,
  220. .name = "panel-mipi-dbi",
  221. .desc = "MIPI DBI compatible display panel",
  222. .date = "20220103",
  223. .major = 1,
  224. .minor = 0,
  225. };
  226. static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
  227. {
  228. struct device *dev = dbidev->drm.dev;
  229. u16 hback_porch, vback_porch;
  230. int ret;
  231. ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
  232. if (ret) {
  233. dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
  234. return ret;
  235. }
  236. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  237. hback_porch = mode->htotal - mode->hsync_end;
  238. vback_porch = mode->vtotal - mode->vsync_end;
  239. /*
  240. * Make sure width and height are set and that only back porch and
  241. * pixelclock are set in the other timing values. Also check that
  242. * width and height don't exceed the 16-bit value specified by MIPI DCS.
  243. */
  244. if (!mode->hdisplay || !mode->vdisplay || mode->flags ||
  245. mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff ||
  246. mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) {
  247. dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node);
  248. return -EINVAL;
  249. }
  250. /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
  251. if (!mode->clock)
  252. mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
  253. dbidev->top_offset = vback_porch;
  254. dbidev->left_offset = hback_porch;
  255. return 0;
  256. }
  257. static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
  258. {
  259. struct device *dev = &spi->dev;
  260. struct drm_display_mode mode;
  261. struct mipi_dbi_dev *dbidev;
  262. struct drm_device *drm;
  263. struct mipi_dbi *dbi;
  264. struct gpio_desc *dc;
  265. unsigned int bpp;
  266. size_t buf_size;
  267. u32 formats[2];
  268. int ret;
  269. dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
  270. if (IS_ERR(dbidev))
  271. return PTR_ERR(dbidev);
  272. dbi = &dbidev->dbi;
  273. drm = &dbidev->drm;
  274. ret = panel_mipi_dbi_get_mode(dbidev, &mode);
  275. if (ret)
  276. return ret;
  277. dbidev->regulator = devm_regulator_get(dev, "power");
  278. if (IS_ERR(dbidev->regulator))
  279. return dev_err_probe(dev, PTR_ERR(dbidev->regulator),
  280. "Failed to get regulator 'power'\n");
  281. dbidev->io_regulator = devm_regulator_get(dev, "io");
  282. if (IS_ERR(dbidev->io_regulator))
  283. return dev_err_probe(dev, PTR_ERR(dbidev->io_regulator),
  284. "Failed to get regulator 'io'\n");
  285. dbidev->backlight = devm_of_find_backlight(dev);
  286. if (IS_ERR(dbidev->backlight))
  287. return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n");
  288. dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  289. if (IS_ERR(dbi->reset))
  290. return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
  291. /* Multiple panels can share the "dc" GPIO, but only if they are on the same SPI bus! */
  292. dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  293. if (IS_ERR(dc))
  294. return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
  295. ret = mipi_dbi_spi_init(spi, dbi, dc);
  296. if (ret)
  297. return ret;
  298. if (device_property_present(dev, "write-only"))
  299. dbi->read_commands = NULL;
  300. dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev);
  301. if (IS_ERR(dbidev->driver_private))
  302. return PTR_ERR(dbidev->driver_private);
  303. ret = panel_mipi_dbi_get_format(dev, formats, &bpp);
  304. if (ret)
  305. return ret;
  306. buf_size = DIV_ROUND_UP(mode.hdisplay * mode.vdisplay * bpp, 8);
  307. ret = mipi_dbi_dev_init_with_formats(dbidev, &panel_mipi_dbi_pipe_funcs,
  308. formats, ARRAY_SIZE(formats),
  309. &mode, 0, buf_size);
  310. if (ret)
  311. return ret;
  312. drm_mode_config_reset(drm);
  313. ret = drm_dev_register(drm, 0);
  314. if (ret)
  315. return ret;
  316. spi_set_drvdata(spi, drm);
  317. if (bpp == 16)
  318. drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB565);
  319. else
  320. drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB888);
  321. return 0;
  322. }
  323. static void panel_mipi_dbi_spi_remove(struct spi_device *spi)
  324. {
  325. struct drm_device *drm = spi_get_drvdata(spi);
  326. drm_dev_unplug(drm);
  327. drm_atomic_helper_shutdown(drm);
  328. }
  329. static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
  330. {
  331. drm_atomic_helper_shutdown(spi_get_drvdata(spi));
  332. }
  333. static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
  334. {
  335. return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
  336. }
  337. static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
  338. {
  339. drm_mode_config_helper_resume(dev_get_drvdata(dev));
  340. return 0;
  341. }
  342. static const struct dev_pm_ops panel_mipi_dbi_pm_ops = {
  343. SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume)
  344. };
  345. static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
  346. { .compatible = "panel-mipi-dbi-spi" },
  347. {},
  348. };
  349. MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
  350. static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
  351. { "panel-mipi-dbi-spi", 0 },
  352. { },
  353. };
  354. MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
  355. static struct spi_driver panel_mipi_dbi_spi_driver = {
  356. .driver = {
  357. .name = "panel-mipi-dbi-spi",
  358. .of_match_table = panel_mipi_dbi_spi_of_match,
  359. .pm = &panel_mipi_dbi_pm_ops,
  360. },
  361. .id_table = panel_mipi_dbi_spi_id,
  362. .probe = panel_mipi_dbi_spi_probe,
  363. .remove = panel_mipi_dbi_spi_remove,
  364. .shutdown = panel_mipi_dbi_spi_shutdown,
  365. };
  366. module_spi_driver(panel_mipi_dbi_spi_driver);
  367. MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
  368. MODULE_AUTHOR("Noralf Trønnes");
  369. MODULE_LICENSE("GPL");