ili9225.c 13 KB

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