ili9225.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * DRM driver for Ilitek ILI9225 panels
  3. *
  4. * Copyright 2017 David Lechner <david@lechnology.com>
  5. *
  6. * Some code copied from mipi-dbi.c
  7. * Copyright 2016 Noralf Trønnes
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/dma-buf.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/module.h>
  18. #include <linux/property.h>
  19. #include <linux/spi/spi.h>
  20. #include <video/mipi_display.h>
  21. #include <drm/drm_fb_helper.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include <drm/tinydrm/mipi-dbi.h>
  24. #include <drm/tinydrm/tinydrm-helpers.h>
  25. #define ILI9225_DRIVER_READ_CODE 0x00
  26. #define ILI9225_DRIVER_OUTPUT_CONTROL 0x01
  27. #define ILI9225_LCD_AC_DRIVING_CONTROL 0x02
  28. #define ILI9225_ENTRY_MODE 0x03
  29. #define ILI9225_DISPLAY_CONTROL_1 0x07
  30. #define ILI9225_BLANK_PERIOD_CONTROL_1 0x08
  31. #define ILI9225_FRAME_CYCLE_CONTROL 0x0b
  32. #define ILI9225_INTERFACE_CONTROL 0x0c
  33. #define ILI9225_OSCILLATION_CONTROL 0x0f
  34. #define ILI9225_POWER_CONTROL_1 0x10
  35. #define ILI9225_POWER_CONTROL_2 0x11
  36. #define ILI9225_POWER_CONTROL_3 0x12
  37. #define ILI9225_POWER_CONTROL_4 0x13
  38. #define ILI9225_POWER_CONTROL_5 0x14
  39. #define ILI9225_VCI_RECYCLING 0x15
  40. #define ILI9225_RAM_ADDRESS_SET_1 0x20
  41. #define ILI9225_RAM_ADDRESS_SET_2 0x21
  42. #define ILI9225_WRITE_DATA_TO_GRAM 0x22
  43. #define ILI9225_SOFTWARE_RESET 0x28
  44. #define ILI9225_GATE_SCAN_CONTROL 0x30
  45. #define ILI9225_VERTICAL_SCROLL_1 0x31
  46. #define ILI9225_VERTICAL_SCROLL_2 0x32
  47. #define ILI9225_VERTICAL_SCROLL_3 0x33
  48. #define ILI9225_PARTIAL_DRIVING_POS_1 0x34
  49. #define ILI9225_PARTIAL_DRIVING_POS_2 0x35
  50. #define ILI9225_HORIZ_WINDOW_ADDR_1 0x36
  51. #define ILI9225_HORIZ_WINDOW_ADDR_2 0x37
  52. #define ILI9225_VERT_WINDOW_ADDR_1 0x38
  53. #define ILI9225_VERT_WINDOW_ADDR_2 0x39
  54. #define ILI9225_GAMMA_CONTROL_1 0x50
  55. #define ILI9225_GAMMA_CONTROL_2 0x51
  56. #define ILI9225_GAMMA_CONTROL_3 0x52
  57. #define ILI9225_GAMMA_CONTROL_4 0x53
  58. #define ILI9225_GAMMA_CONTROL_5 0x54
  59. #define ILI9225_GAMMA_CONTROL_6 0x55
  60. #define ILI9225_GAMMA_CONTROL_7 0x56
  61. #define ILI9225_GAMMA_CONTROL_8 0x57
  62. #define ILI9225_GAMMA_CONTROL_9 0x58
  63. #define ILI9225_GAMMA_CONTROL_10 0x59
  64. static inline int ili9225_command(struct mipi_dbi *mipi, u8 cmd, u16 data)
  65. {
  66. u8 par[2] = { data >> 8, data & 0xff };
  67. return mipi_dbi_command_buf(mipi, cmd, par, 2);
  68. }
  69. static int ili9225_fb_dirty(struct drm_framebuffer *fb,
  70. struct drm_file *file_priv, unsigned int flags,
  71. unsigned int color, struct drm_clip_rect *clips,
  72. unsigned int num_clips)
  73. {
  74. struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  75. struct tinydrm_device *tdev = fb->dev->dev_private;
  76. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  77. bool swap = mipi->swap_bytes;
  78. struct drm_clip_rect clip;
  79. u16 x_start, y_start;
  80. u16 x1, x2, y1, y2;
  81. int ret = 0;
  82. bool full;
  83. void *tr;
  84. if (!mipi->enabled)
  85. return 0;
  86. full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
  87. fb->width, fb->height);
  88. DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
  89. clip.x1, clip.x2, clip.y1, clip.y2);
  90. if (!mipi->dc || !full || swap ||
  91. fb->format->format == DRM_FORMAT_XRGB8888) {
  92. tr = mipi->tx_buf;
  93. ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
  94. if (ret)
  95. return ret;
  96. } else {
  97. tr = cma_obj->vaddr;
  98. }
  99. switch (mipi->rotation) {
  100. default:
  101. x1 = clip.x1;
  102. x2 = clip.x2 - 1;
  103. y1 = clip.y1;
  104. y2 = clip.y2 - 1;
  105. x_start = x1;
  106. y_start = y1;
  107. break;
  108. case 90:
  109. x1 = clip.y1;
  110. x2 = clip.y2 - 1;
  111. y1 = fb->width - clip.x2;
  112. y2 = fb->width - clip.x1 - 1;
  113. x_start = x1;
  114. y_start = y2;
  115. break;
  116. case 180:
  117. x1 = fb->width - clip.x2;
  118. x2 = fb->width - clip.x1 - 1;
  119. y1 = fb->height - clip.y2;
  120. y2 = fb->height - clip.y1 - 1;
  121. x_start = x2;
  122. y_start = y2;
  123. break;
  124. case 270:
  125. x1 = fb->height - clip.y2;
  126. x2 = fb->height - clip.y1 - 1;
  127. y1 = clip.x1;
  128. y2 = clip.x2 - 1;
  129. x_start = x2;
  130. y_start = y1;
  131. break;
  132. }
  133. ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
  134. ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
  135. ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_1, y2);
  136. ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_2, y1);
  137. ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, x_start);
  138. ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, y_start);
  139. ret = mipi_dbi_command_buf(mipi, ILI9225_WRITE_DATA_TO_GRAM, tr,
  140. (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
  141. return ret;
  142. }
  143. static const struct drm_framebuffer_funcs ili9225_fb_funcs = {
  144. .destroy = drm_gem_fb_destroy,
  145. .create_handle = drm_gem_fb_create_handle,
  146. .dirty = tinydrm_fb_dirty,
  147. };
  148. static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
  149. struct drm_crtc_state *crtc_state,
  150. struct drm_plane_state *plane_state)
  151. {
  152. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  153. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  154. struct device *dev = tdev->drm->dev;
  155. int ret;
  156. u8 am_id;
  157. DRM_DEBUG_KMS("\n");
  158. mipi_dbi_hw_reset(mipi);
  159. /*
  160. * There don't seem to be two example init sequences that match, so
  161. * using the one from the popular Arduino library for this display.
  162. * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
  163. */
  164. ret = ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0000);
  165. if (ret) {
  166. DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
  167. return;
  168. }
  169. ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0000);
  170. ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x0000);
  171. ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x0000);
  172. ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x0000);
  173. msleep(40);
  174. ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0018);
  175. ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x6121);
  176. ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x006f);
  177. ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x495f);
  178. ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0800);
  179. msleep(10);
  180. ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
  181. msleep(50);
  182. switch (mipi->rotation) {
  183. default:
  184. am_id = 0x30;
  185. break;
  186. case 90:
  187. am_id = 0x18;
  188. break;
  189. case 180:
  190. am_id = 0x00;
  191. break;
  192. case 270:
  193. am_id = 0x28;
  194. break;
  195. }
  196. ili9225_command(mipi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
  197. ili9225_command(mipi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
  198. ili9225_command(mipi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
  199. ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
  200. ili9225_command(mipi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
  201. ili9225_command(mipi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
  202. ili9225_command(mipi, ILI9225_INTERFACE_CONTROL, 0x0000);
  203. ili9225_command(mipi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
  204. ili9225_command(mipi, ILI9225_VCI_RECYCLING, 0x0020);
  205. ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
  206. ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
  207. ili9225_command(mipi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
  208. ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
  209. ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
  210. ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
  211. ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
  212. ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
  213. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_1, 0x0000);
  214. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_2, 0x0808);
  215. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_3, 0x080a);
  216. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_4, 0x000a);
  217. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
  218. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_6, 0x0808);
  219. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_7, 0x0000);
  220. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
  221. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_9, 0x0710);
  222. ili9225_command(mipi, ILI9225_GAMMA_CONTROL_10, 0x0710);
  223. ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
  224. msleep(50);
  225. ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
  226. mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
  227. }
  228. static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
  229. {
  230. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  231. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  232. DRM_DEBUG_KMS("\n");
  233. if (!mipi->enabled)
  234. return;
  235. ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
  236. msleep(50);
  237. ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
  238. msleep(50);
  239. ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0a02);
  240. mipi->enabled = false;
  241. }
  242. static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
  243. size_t num)
  244. {
  245. struct spi_device *spi = mipi->spi;
  246. unsigned int bpw = 8;
  247. u32 speed_hz;
  248. int ret;
  249. gpiod_set_value_cansleep(mipi->dc, 0);
  250. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
  251. ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, cmd, 1);
  252. if (ret || !num)
  253. return ret;
  254. if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
  255. bpw = 16;
  256. gpiod_set_value_cansleep(mipi->dc, 1);
  257. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
  258. return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
  259. }
  260. static const u32 ili9225_formats[] = {
  261. DRM_FORMAT_RGB565,
  262. DRM_FORMAT_XRGB8888,
  263. };
  264. static int ili9225_init(struct device *dev, struct mipi_dbi *mipi,
  265. const struct drm_simple_display_pipe_funcs *pipe_funcs,
  266. struct drm_driver *driver,
  267. const struct drm_display_mode *mode,
  268. unsigned int rotation)
  269. {
  270. size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
  271. struct tinydrm_device *tdev = &mipi->tinydrm;
  272. int ret;
  273. if (!mipi->command)
  274. return -EINVAL;
  275. mutex_init(&mipi->cmdlock);
  276. mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
  277. if (!mipi->tx_buf)
  278. return -ENOMEM;
  279. ret = devm_tinydrm_init(dev, tdev, &ili9225_fb_funcs, driver);
  280. if (ret)
  281. return ret;
  282. tdev->fb_dirty = ili9225_fb_dirty;
  283. ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
  284. DRM_MODE_CONNECTOR_VIRTUAL,
  285. ili9225_formats,
  286. ARRAY_SIZE(ili9225_formats), mode,
  287. rotation);
  288. if (ret)
  289. return ret;
  290. tdev->drm->mode_config.preferred_depth = 16;
  291. mipi->rotation = rotation;
  292. drm_mode_config_reset(tdev->drm);
  293. DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
  294. tdev->drm->mode_config.preferred_depth, rotation);
  295. return 0;
  296. }
  297. static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
  298. .enable = ili9225_pipe_enable,
  299. .disable = ili9225_pipe_disable,
  300. .update = tinydrm_display_pipe_update,
  301. .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
  302. };
  303. static const struct drm_display_mode ili9225_mode = {
  304. TINYDRM_MODE(176, 220, 35, 44),
  305. };
  306. DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
  307. static struct drm_driver ili9225_driver = {
  308. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  309. DRIVER_ATOMIC,
  310. .fops = &ili9225_fops,
  311. TINYDRM_GEM_DRIVER_OPS,
  312. .name = "ili9225",
  313. .desc = "Ilitek ILI9225",
  314. .date = "20171106",
  315. .major = 1,
  316. .minor = 0,
  317. };
  318. static const struct of_device_id ili9225_of_match[] = {
  319. { .compatible = "vot,v220hf01a-t" },
  320. {},
  321. };
  322. MODULE_DEVICE_TABLE(of, ili9225_of_match);
  323. static const struct spi_device_id ili9225_id[] = {
  324. { "v220hf01a-t", 0 },
  325. { },
  326. };
  327. MODULE_DEVICE_TABLE(spi, ili9225_id);
  328. static int ili9225_probe(struct spi_device *spi)
  329. {
  330. struct device *dev = &spi->dev;
  331. struct mipi_dbi *mipi;
  332. struct gpio_desc *rs;
  333. u32 rotation = 0;
  334. int ret;
  335. mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
  336. if (!mipi)
  337. return -ENOMEM;
  338. mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  339. if (IS_ERR(mipi->reset)) {
  340. DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
  341. return PTR_ERR(mipi->reset);
  342. }
  343. rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
  344. if (IS_ERR(rs)) {
  345. DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
  346. return PTR_ERR(rs);
  347. }
  348. device_property_read_u32(dev, "rotation", &rotation);
  349. ret = mipi_dbi_spi_init(spi, mipi, rs);
  350. if (ret)
  351. return ret;
  352. /* override the command function set in mipi_dbi_spi_init() */
  353. mipi->command = ili9225_dbi_command;
  354. ret = ili9225_init(&spi->dev, mipi, &ili9225_pipe_funcs,
  355. &ili9225_driver, &ili9225_mode, rotation);
  356. if (ret)
  357. return ret;
  358. spi_set_drvdata(spi, mipi);
  359. return devm_tinydrm_register(&mipi->tinydrm);
  360. }
  361. static void ili9225_shutdown(struct spi_device *spi)
  362. {
  363. struct mipi_dbi *mipi = spi_get_drvdata(spi);
  364. tinydrm_shutdown(&mipi->tinydrm);
  365. }
  366. static struct spi_driver ili9225_spi_driver = {
  367. .driver = {
  368. .name = "ili9225",
  369. .owner = THIS_MODULE,
  370. .of_match_table = ili9225_of_match,
  371. },
  372. .id_table = ili9225_id,
  373. .probe = ili9225_probe,
  374. .shutdown = ili9225_shutdown,
  375. };
  376. module_spi_driver(ili9225_spi_driver);
  377. MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
  378. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  379. MODULE_LICENSE("GPL");