ili9225.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DRM driver for Ilitek ILI9225 panels
  4. *
  5. * Copyright 2017 David Lechner <david@lechnology.com>
  6. *
  7. * Some code copied from mipi-dbi.c
  8. * Copyright 2016 Noralf Trønnes
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/dma-buf.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/module.h>
  14. #include <linux/property.h>
  15. #include <linux/spi/spi.h>
  16. #include <video/mipi_display.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_damage_helper.h>
  19. #include <drm/drm_drv.h>
  20. #include <drm/drm_fb_dma_helper.h>
  21. #include <drm/drm_fbdev_dma.h>
  22. #include <drm/drm_fourcc.h>
  23. #include <drm/drm_framebuffer.h>
  24. #include <drm/drm_gem_atomic_helper.h>
  25. #include <drm/drm_gem_dma_helper.h>
  26. #include <drm/drm_gem_framebuffer_helper.h>
  27. #include <drm/drm_managed.h>
  28. #include <drm/drm_mipi_dbi.h>
  29. #include <drm/drm_rect.h>
  30. #define ILI9225_DRIVER_READ_CODE 0x00
  31. #define ILI9225_DRIVER_OUTPUT_CONTROL 0x01
  32. #define ILI9225_LCD_AC_DRIVING_CONTROL 0x02
  33. #define ILI9225_ENTRY_MODE 0x03
  34. #define ILI9225_DISPLAY_CONTROL_1 0x07
  35. #define ILI9225_BLANK_PERIOD_CONTROL_1 0x08
  36. #define ILI9225_FRAME_CYCLE_CONTROL 0x0b
  37. #define ILI9225_INTERFACE_CONTROL 0x0c
  38. #define ILI9225_OSCILLATION_CONTROL 0x0f
  39. #define ILI9225_POWER_CONTROL_1 0x10
  40. #define ILI9225_POWER_CONTROL_2 0x11
  41. #define ILI9225_POWER_CONTROL_3 0x12
  42. #define ILI9225_POWER_CONTROL_4 0x13
  43. #define ILI9225_POWER_CONTROL_5 0x14
  44. #define ILI9225_VCI_RECYCLING 0x15
  45. #define ILI9225_RAM_ADDRESS_SET_1 0x20
  46. #define ILI9225_RAM_ADDRESS_SET_2 0x21
  47. #define ILI9225_WRITE_DATA_TO_GRAM 0x22
  48. #define ILI9225_SOFTWARE_RESET 0x28
  49. #define ILI9225_GATE_SCAN_CONTROL 0x30
  50. #define ILI9225_VERTICAL_SCROLL_1 0x31
  51. #define ILI9225_VERTICAL_SCROLL_2 0x32
  52. #define ILI9225_VERTICAL_SCROLL_3 0x33
  53. #define ILI9225_PARTIAL_DRIVING_POS_1 0x34
  54. #define ILI9225_PARTIAL_DRIVING_POS_2 0x35
  55. #define ILI9225_HORIZ_WINDOW_ADDR_1 0x36
  56. #define ILI9225_HORIZ_WINDOW_ADDR_2 0x37
  57. #define ILI9225_VERT_WINDOW_ADDR_1 0x38
  58. #define ILI9225_VERT_WINDOW_ADDR_2 0x39
  59. #define ILI9225_GAMMA_CONTROL_1 0x50
  60. #define ILI9225_GAMMA_CONTROL_2 0x51
  61. #define ILI9225_GAMMA_CONTROL_3 0x52
  62. #define ILI9225_GAMMA_CONTROL_4 0x53
  63. #define ILI9225_GAMMA_CONTROL_5 0x54
  64. #define ILI9225_GAMMA_CONTROL_6 0x55
  65. #define ILI9225_GAMMA_CONTROL_7 0x56
  66. #define ILI9225_GAMMA_CONTROL_8 0x57
  67. #define ILI9225_GAMMA_CONTROL_9 0x58
  68. #define ILI9225_GAMMA_CONTROL_10 0x59
  69. static inline int ili9225_command(struct mipi_dbi *dbi, u8 cmd, u16 data)
  70. {
  71. u8 par[2] = { data >> 8, data & 0xff };
  72. return mipi_dbi_command_buf(dbi, cmd, par, 2);
  73. }
  74. static void ili9225_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb,
  75. struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state)
  76. {
  77. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
  78. unsigned int height = rect->y2 - rect->y1;
  79. unsigned int width = rect->x2 - rect->x1;
  80. struct mipi_dbi *dbi = &dbidev->dbi;
  81. bool swap = dbi->swap_bytes;
  82. u16 x_start, y_start;
  83. u16 x1, x2, y1, y2;
  84. int ret = 0;
  85. bool full;
  86. void *tr;
  87. full = width == fb->width && height == fb->height;
  88. DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
  89. if (!dbi->dc || !full || swap ||
  90. fb->format->format == DRM_FORMAT_XRGB8888) {
  91. tr = dbidev->tx_buf;
  92. ret = mipi_dbi_buf_copy(tr, src, fb, rect, swap, fmtcnv_state);
  93. if (ret)
  94. goto err_msg;
  95. } else {
  96. tr = src->vaddr; /* TODO: Use mapping abstraction properly */
  97. }
  98. switch (dbidev->rotation) {
  99. default:
  100. x1 = rect->x1;
  101. x2 = rect->x2 - 1;
  102. y1 = rect->y1;
  103. y2 = rect->y2 - 1;
  104. x_start = x1;
  105. y_start = y1;
  106. break;
  107. case 90:
  108. x1 = rect->y1;
  109. x2 = rect->y2 - 1;
  110. y1 = fb->width - rect->x2;
  111. y2 = fb->width - rect->x1 - 1;
  112. x_start = x1;
  113. y_start = y2;
  114. break;
  115. case 180:
  116. x1 = fb->width - rect->x2;
  117. x2 = fb->width - rect->x1 - 1;
  118. y1 = fb->height - rect->y2;
  119. y2 = fb->height - rect->y1 - 1;
  120. x_start = x2;
  121. y_start = y2;
  122. break;
  123. case 270:
  124. x1 = fb->height - rect->y2;
  125. x2 = fb->height - rect->y1 - 1;
  126. y1 = rect->x1;
  127. y2 = rect->x2 - 1;
  128. x_start = x2;
  129. y_start = y1;
  130. break;
  131. }
  132. ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
  133. ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
  134. ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_1, y2);
  135. ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_2, y1);
  136. ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, x_start);
  137. ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, y_start);
  138. ret = mipi_dbi_command_buf(dbi, ILI9225_WRITE_DATA_TO_GRAM, tr,
  139. width * height * 2);
  140. err_msg:
  141. if (ret)
  142. dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
  143. }
  144. static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
  145. struct drm_plane_state *old_state)
  146. {
  147. struct drm_plane_state *state = pipe->plane.state;
  148. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
  149. struct drm_framebuffer *fb = state->fb;
  150. struct drm_rect rect;
  151. int idx;
  152. if (!pipe->crtc.state->active)
  153. return;
  154. if (!drm_dev_enter(fb->dev, &idx))
  155. return;
  156. if (drm_atomic_helper_damage_merged(old_state, state, &rect))
  157. ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
  158. &shadow_plane_state->fmtcnv_state);
  159. drm_dev_exit(idx);
  160. }
  161. static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
  162. struct drm_crtc_state *crtc_state,
  163. struct drm_plane_state *plane_state)
  164. {
  165. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  166. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  167. struct drm_framebuffer *fb = plane_state->fb;
  168. struct device *dev = pipe->crtc.dev->dev;
  169. struct mipi_dbi *dbi = &dbidev->dbi;
  170. struct drm_rect rect = {
  171. .x1 = 0,
  172. .x2 = fb->width,
  173. .y1 = 0,
  174. .y2 = fb->height,
  175. };
  176. int ret, idx;
  177. u8 am_id;
  178. if (!drm_dev_enter(pipe->crtc.dev, &idx))
  179. return;
  180. DRM_DEBUG_KMS("\n");
  181. mipi_dbi_hw_reset(dbi);
  182. /*
  183. * There don't seem to be two example init sequences that match, so
  184. * using the one from the popular Arduino library for this display.
  185. * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
  186. */
  187. ret = ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0000);
  188. if (ret) {
  189. DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
  190. goto out_exit;
  191. }
  192. ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0000);
  193. ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x0000);
  194. ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x0000);
  195. ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x0000);
  196. msleep(40);
  197. ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0018);
  198. ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x6121);
  199. ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x006f);
  200. ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x495f);
  201. ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0800);
  202. msleep(10);
  203. ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x103b);
  204. msleep(50);
  205. switch (dbidev->rotation) {
  206. default:
  207. am_id = 0x30;
  208. break;
  209. case 90:
  210. am_id = 0x18;
  211. break;
  212. case 180:
  213. am_id = 0x00;
  214. break;
  215. case 270:
  216. am_id = 0x28;
  217. break;
  218. }
  219. ili9225_command(dbi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
  220. ili9225_command(dbi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
  221. ili9225_command(dbi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
  222. ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
  223. ili9225_command(dbi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
  224. ili9225_command(dbi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
  225. ili9225_command(dbi, ILI9225_INTERFACE_CONTROL, 0x0000);
  226. ili9225_command(dbi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
  227. ili9225_command(dbi, ILI9225_VCI_RECYCLING, 0x0020);
  228. ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
  229. ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
  230. ili9225_command(dbi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
  231. ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
  232. ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
  233. ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
  234. ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
  235. ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
  236. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_1, 0x0000);
  237. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_2, 0x0808);
  238. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_3, 0x080a);
  239. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_4, 0x000a);
  240. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
  241. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_6, 0x0808);
  242. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_7, 0x0000);
  243. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
  244. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_9, 0x0710);
  245. ili9225_command(dbi, ILI9225_GAMMA_CONTROL_10, 0x0710);
  246. ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
  247. msleep(50);
  248. ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
  249. ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
  250. &shadow_plane_state->fmtcnv_state);
  251. out_exit:
  252. drm_dev_exit(idx);
  253. }
  254. static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
  255. {
  256. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  257. struct mipi_dbi *dbi = &dbidev->dbi;
  258. DRM_DEBUG_KMS("\n");
  259. /*
  260. * This callback is not protected by drm_dev_enter/exit since we want to
  261. * turn off the display on regular driver unload. It's highly unlikely
  262. * that the underlying SPI controller is gone should this be called after
  263. * unplug.
  264. */
  265. ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
  266. msleep(50);
  267. ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0007);
  268. msleep(50);
  269. ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0a02);
  270. }
  271. static int ili9225_dbi_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par,
  272. size_t num)
  273. {
  274. struct spi_device *spi = dbi->spi;
  275. unsigned int bpw = 8;
  276. u32 speed_hz;
  277. int ret;
  278. spi_bus_lock(spi->controller);
  279. gpiod_set_value_cansleep(dbi->dc, 0);
  280. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
  281. ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
  282. spi_bus_unlock(spi->controller);
  283. if (ret || !num)
  284. return ret;
  285. if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !dbi->swap_bytes)
  286. bpw = 16;
  287. spi_bus_lock(spi->controller);
  288. gpiod_set_value_cansleep(dbi->dc, 1);
  289. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
  290. ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
  291. spi_bus_unlock(spi->controller);
  292. return ret;
  293. }
  294. static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
  295. .mode_valid = mipi_dbi_pipe_mode_valid,
  296. .enable = ili9225_pipe_enable,
  297. .disable = ili9225_pipe_disable,
  298. .update = ili9225_pipe_update,
  299. .begin_fb_access = mipi_dbi_pipe_begin_fb_access,
  300. .end_fb_access = mipi_dbi_pipe_end_fb_access,
  301. .reset_plane = mipi_dbi_pipe_reset_plane,
  302. .duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state,
  303. .destroy_plane_state = mipi_dbi_pipe_destroy_plane_state,
  304. };
  305. static const struct drm_display_mode ili9225_mode = {
  306. DRM_SIMPLE_MODE(176, 220, 35, 44),
  307. };
  308. DEFINE_DRM_GEM_DMA_FOPS(ili9225_fops);
  309. static const struct drm_driver ili9225_driver = {
  310. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  311. .fops = &ili9225_fops,
  312. DRM_GEM_DMA_DRIVER_OPS_VMAP,
  313. .name = "ili9225",
  314. .desc = "Ilitek ILI9225",
  315. .date = "20171106",
  316. .major = 1,
  317. .minor = 0,
  318. };
  319. static const struct of_device_id ili9225_of_match[] = {
  320. { .compatible = "vot,v220hf01a-t" },
  321. {},
  322. };
  323. MODULE_DEVICE_TABLE(of, ili9225_of_match);
  324. static const struct spi_device_id ili9225_id[] = {
  325. { "v220hf01a-t", 0 },
  326. { },
  327. };
  328. MODULE_DEVICE_TABLE(spi, ili9225_id);
  329. static int ili9225_probe(struct spi_device *spi)
  330. {
  331. struct device *dev = &spi->dev;
  332. struct mipi_dbi_dev *dbidev;
  333. struct drm_device *drm;
  334. struct mipi_dbi *dbi;
  335. struct gpio_desc *rs;
  336. u32 rotation = 0;
  337. int ret;
  338. dbidev = devm_drm_dev_alloc(dev, &ili9225_driver,
  339. struct mipi_dbi_dev, drm);
  340. if (IS_ERR(dbidev))
  341. return PTR_ERR(dbidev);
  342. dbi = &dbidev->dbi;
  343. drm = &dbidev->drm;
  344. dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  345. if (IS_ERR(dbi->reset))
  346. return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
  347. rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
  348. if (IS_ERR(rs))
  349. return dev_err_probe(dev, PTR_ERR(rs), "Failed to get GPIO 'rs'\n");
  350. device_property_read_u32(dev, "rotation", &rotation);
  351. ret = mipi_dbi_spi_init(spi, dbi, rs);
  352. if (ret)
  353. return ret;
  354. /* override the command function set in mipi_dbi_spi_init() */
  355. dbi->command = ili9225_dbi_command;
  356. ret = mipi_dbi_dev_init(dbidev, &ili9225_pipe_funcs, &ili9225_mode, rotation);
  357. if (ret)
  358. return ret;
  359. drm_mode_config_reset(drm);
  360. ret = drm_dev_register(drm, 0);
  361. if (ret)
  362. return ret;
  363. spi_set_drvdata(spi, drm);
  364. drm_fbdev_dma_setup(drm, 0);
  365. return 0;
  366. }
  367. static void ili9225_remove(struct spi_device *spi)
  368. {
  369. struct drm_device *drm = spi_get_drvdata(spi);
  370. drm_dev_unplug(drm);
  371. drm_atomic_helper_shutdown(drm);
  372. }
  373. static void ili9225_shutdown(struct spi_device *spi)
  374. {
  375. drm_atomic_helper_shutdown(spi_get_drvdata(spi));
  376. }
  377. static struct spi_driver ili9225_spi_driver = {
  378. .driver = {
  379. .name = "ili9225",
  380. .of_match_table = ili9225_of_match,
  381. },
  382. .id_table = ili9225_id,
  383. .probe = ili9225_probe,
  384. .remove = ili9225_remove,
  385. .shutdown = ili9225_shutdown,
  386. };
  387. module_spi_driver(ili9225_spi_driver);
  388. MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
  389. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  390. MODULE_LICENSE("GPL");