mipi-dbi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * MIPI Display Bus Interface (DBI) LCD controller support
  3. *
  4. * Copyright 2016 Noralf Trønnes
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <drm/tinydrm/mipi-dbi.h>
  12. #include <drm/tinydrm/tinydrm-helpers.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/dma-buf.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/module.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/spi/spi.h>
  19. #include <video/mipi_display.h>
  20. #define MIPI_DBI_MAX_SPI_READ_SPEED 2000000 /* 2MHz */
  21. #define DCS_POWER_MODE_DISPLAY BIT(2)
  22. #define DCS_POWER_MODE_DISPLAY_NORMAL_MODE BIT(3)
  23. #define DCS_POWER_MODE_SLEEP_MODE BIT(4)
  24. #define DCS_POWER_MODE_PARTIAL_MODE BIT(5)
  25. #define DCS_POWER_MODE_IDLE_MODE BIT(6)
  26. #define DCS_POWER_MODE_RESERVED_MASK (BIT(0) | BIT(1) | BIT(7))
  27. /**
  28. * DOC: overview
  29. *
  30. * This library provides helpers for MIPI Display Bus Interface (DBI)
  31. * compatible display controllers.
  32. *
  33. * Many controllers for tiny lcd displays are MIPI compliant and can use this
  34. * library. If a controller uses registers 0x2A and 0x2B to set the area to
  35. * update and uses register 0x2C to write to frame memory, it is most likely
  36. * MIPI compliant.
  37. *
  38. * Only MIPI Type 1 displays are supported since a full frame memory is needed.
  39. *
  40. * There are 3 MIPI DBI implementation types:
  41. *
  42. * A. Motorola 6800 type parallel bus
  43. *
  44. * B. Intel 8080 type parallel bus
  45. *
  46. * C. SPI type with 3 options:
  47. *
  48. * 1. 9-bit with the Data/Command signal as the ninth bit
  49. * 2. Same as above except it's sent as 16 bits
  50. * 3. 8-bit with the Data/Command signal as a separate D/CX pin
  51. *
  52. * Currently mipi_dbi only supports Type C options 1 and 3 with
  53. * mipi_dbi_spi_init().
  54. */
  55. #define MIPI_DBI_DEBUG_COMMAND(cmd, data, len) \
  56. ({ \
  57. if (!len) \
  58. DRM_DEBUG_DRIVER("cmd=%02x\n", cmd); \
  59. else if (len <= 32) \
  60. DRM_DEBUG_DRIVER("cmd=%02x, par=%*ph\n", cmd, (int)len, data);\
  61. else \
  62. DRM_DEBUG_DRIVER("cmd=%02x, len=%zu\n", cmd, len); \
  63. })
  64. static const u8 mipi_dbi_dcs_read_commands[] = {
  65. MIPI_DCS_GET_DISPLAY_ID,
  66. MIPI_DCS_GET_RED_CHANNEL,
  67. MIPI_DCS_GET_GREEN_CHANNEL,
  68. MIPI_DCS_GET_BLUE_CHANNEL,
  69. MIPI_DCS_GET_DISPLAY_STATUS,
  70. MIPI_DCS_GET_POWER_MODE,
  71. MIPI_DCS_GET_ADDRESS_MODE,
  72. MIPI_DCS_GET_PIXEL_FORMAT,
  73. MIPI_DCS_GET_DISPLAY_MODE,
  74. MIPI_DCS_GET_SIGNAL_MODE,
  75. MIPI_DCS_GET_DIAGNOSTIC_RESULT,
  76. MIPI_DCS_READ_MEMORY_START,
  77. MIPI_DCS_READ_MEMORY_CONTINUE,
  78. MIPI_DCS_GET_SCANLINE,
  79. MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
  80. MIPI_DCS_GET_CONTROL_DISPLAY,
  81. MIPI_DCS_GET_POWER_SAVE,
  82. MIPI_DCS_GET_CABC_MIN_BRIGHTNESS,
  83. MIPI_DCS_READ_DDB_START,
  84. MIPI_DCS_READ_DDB_CONTINUE,
  85. 0, /* sentinel */
  86. };
  87. static bool mipi_dbi_command_is_read(struct mipi_dbi *mipi, u8 cmd)
  88. {
  89. unsigned int i;
  90. if (!mipi->read_commands)
  91. return false;
  92. for (i = 0; i < 0xff; i++) {
  93. if (!mipi->read_commands[i])
  94. return false;
  95. if (cmd == mipi->read_commands[i])
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. * mipi_dbi_command_read - MIPI DCS read command
  102. * @mipi: MIPI structure
  103. * @cmd: Command
  104. * @val: Value read
  105. *
  106. * Send MIPI DCS read command to the controller.
  107. *
  108. * Returns:
  109. * Zero on success, negative error code on failure.
  110. */
  111. int mipi_dbi_command_read(struct mipi_dbi *mipi, u8 cmd, u8 *val)
  112. {
  113. if (!mipi->read_commands)
  114. return -EACCES;
  115. if (!mipi_dbi_command_is_read(mipi, cmd))
  116. return -EINVAL;
  117. return mipi_dbi_command_buf(mipi, cmd, val, 1);
  118. }
  119. EXPORT_SYMBOL(mipi_dbi_command_read);
  120. /**
  121. * mipi_dbi_command_buf - MIPI DCS command with parameter(s) in an array
  122. * @mipi: MIPI structure
  123. * @cmd: Command
  124. * @data: Parameter buffer
  125. * @len: Buffer length
  126. *
  127. * Returns:
  128. * Zero on success, negative error code on failure.
  129. */
  130. int mipi_dbi_command_buf(struct mipi_dbi *mipi, u8 cmd, u8 *data, size_t len)
  131. {
  132. int ret;
  133. mutex_lock(&mipi->cmdlock);
  134. ret = mipi->command(mipi, cmd, data, len);
  135. mutex_unlock(&mipi->cmdlock);
  136. return ret;
  137. }
  138. EXPORT_SYMBOL(mipi_dbi_command_buf);
  139. static int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb,
  140. struct drm_clip_rect *clip, bool swap)
  141. {
  142. struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  143. struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
  144. struct drm_format_name_buf format_name;
  145. void *src = cma_obj->vaddr;
  146. int ret = 0;
  147. if (import_attach) {
  148. ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
  149. DMA_FROM_DEVICE);
  150. if (ret)
  151. return ret;
  152. }
  153. switch (fb->format->format) {
  154. case DRM_FORMAT_RGB565:
  155. if (swap)
  156. tinydrm_swab16(dst, src, fb, clip);
  157. else
  158. tinydrm_memcpy(dst, src, fb, clip);
  159. break;
  160. case DRM_FORMAT_XRGB8888:
  161. tinydrm_xrgb8888_to_rgb565(dst, src, fb, clip, swap);
  162. break;
  163. default:
  164. dev_err_once(fb->dev->dev, "Format is not supported: %s\n",
  165. drm_get_format_name(fb->format->format,
  166. &format_name));
  167. return -EINVAL;
  168. }
  169. if (import_attach)
  170. ret = dma_buf_end_cpu_access(import_attach->dmabuf,
  171. DMA_FROM_DEVICE);
  172. return ret;
  173. }
  174. static int mipi_dbi_fb_dirty(struct drm_framebuffer *fb,
  175. struct drm_file *file_priv,
  176. unsigned int flags, unsigned int color,
  177. struct drm_clip_rect *clips,
  178. unsigned int num_clips)
  179. {
  180. struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  181. struct tinydrm_device *tdev = fb->dev->dev_private;
  182. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  183. bool swap = mipi->swap_bytes;
  184. struct drm_clip_rect clip;
  185. int ret = 0;
  186. bool full;
  187. void *tr;
  188. mutex_lock(&tdev->dirty_lock);
  189. if (!mipi->enabled)
  190. goto out_unlock;
  191. /* fbdev can flush even when we're not interested */
  192. if (tdev->pipe.plane.fb != fb)
  193. goto out_unlock;
  194. full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
  195. fb->width, fb->height);
  196. DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
  197. clip.x1, clip.x2, clip.y1, clip.y2);
  198. if (!mipi->dc || !full || swap ||
  199. fb->format->format == DRM_FORMAT_XRGB8888) {
  200. tr = mipi->tx_buf;
  201. ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
  202. if (ret)
  203. goto out_unlock;
  204. } else {
  205. tr = cma_obj->vaddr;
  206. }
  207. mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
  208. (clip.x1 >> 8) & 0xFF, clip.x1 & 0xFF,
  209. (clip.x2 >> 8) & 0xFF, (clip.x2 - 1) & 0xFF);
  210. mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
  211. (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
  212. (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
  213. ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START, tr,
  214. (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
  215. out_unlock:
  216. mutex_unlock(&tdev->dirty_lock);
  217. if (ret)
  218. dev_err_once(fb->dev->dev, "Failed to update display %d\n",
  219. ret);
  220. return ret;
  221. }
  222. static const struct drm_framebuffer_funcs mipi_dbi_fb_funcs = {
  223. .destroy = drm_fb_cma_destroy,
  224. .create_handle = drm_fb_cma_create_handle,
  225. .dirty = mipi_dbi_fb_dirty,
  226. };
  227. /**
  228. * mipi_dbi_pipe_enable - MIPI DBI pipe enable helper
  229. * @pipe: Display pipe
  230. * @crtc_state: CRTC state
  231. *
  232. * This function enables backlight. Drivers can use this as their
  233. * &drm_simple_display_pipe_funcs->enable callback.
  234. */
  235. void mipi_dbi_pipe_enable(struct drm_simple_display_pipe *pipe,
  236. struct drm_crtc_state *crtc_state)
  237. {
  238. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  239. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  240. struct drm_framebuffer *fb = pipe->plane.fb;
  241. DRM_DEBUG_KMS("\n");
  242. mipi->enabled = true;
  243. if (fb)
  244. fb->funcs->dirty(fb, NULL, 0, 0, NULL, 0);
  245. tinydrm_enable_backlight(mipi->backlight);
  246. }
  247. EXPORT_SYMBOL(mipi_dbi_pipe_enable);
  248. static void mipi_dbi_blank(struct mipi_dbi *mipi)
  249. {
  250. struct drm_device *drm = mipi->tinydrm.drm;
  251. u16 height = drm->mode_config.min_height;
  252. u16 width = drm->mode_config.min_width;
  253. size_t len = width * height * 2;
  254. memset(mipi->tx_buf, 0, len);
  255. mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS, 0, 0,
  256. (width >> 8) & 0xFF, (width - 1) & 0xFF);
  257. mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS, 0, 0,
  258. (height >> 8) & 0xFF, (height - 1) & 0xFF);
  259. mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
  260. (u8 *)mipi->tx_buf, len);
  261. }
  262. /**
  263. * mipi_dbi_pipe_disable - MIPI DBI pipe disable helper
  264. * @pipe: Display pipe
  265. *
  266. * This function disables backlight if present or if not the
  267. * display memory is blanked. Drivers can use this as their
  268. * &drm_simple_display_pipe_funcs->disable callback.
  269. */
  270. void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe)
  271. {
  272. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  273. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  274. DRM_DEBUG_KMS("\n");
  275. mipi->enabled = false;
  276. if (mipi->backlight)
  277. tinydrm_disable_backlight(mipi->backlight);
  278. else
  279. mipi_dbi_blank(mipi);
  280. }
  281. EXPORT_SYMBOL(mipi_dbi_pipe_disable);
  282. static const uint32_t mipi_dbi_formats[] = {
  283. DRM_FORMAT_RGB565,
  284. DRM_FORMAT_XRGB8888,
  285. };
  286. /**
  287. * mipi_dbi_init - MIPI DBI initialization
  288. * @dev: Parent device
  289. * @mipi: &mipi_dbi structure to initialize
  290. * @pipe_funcs: Display pipe functions
  291. * @driver: DRM driver
  292. * @mode: Display mode
  293. * @rotation: Initial rotation in degrees Counter Clock Wise
  294. *
  295. * This function initializes a &mipi_dbi structure and it's underlying
  296. * @tinydrm_device. It also sets up the display pipeline.
  297. *
  298. * Supported formats: Native RGB565 and emulated XRGB8888.
  299. *
  300. * Objects created by this function will be automatically freed on driver
  301. * detach (devres).
  302. *
  303. * Returns:
  304. * Zero on success, negative error code on failure.
  305. */
  306. int mipi_dbi_init(struct device *dev, struct mipi_dbi *mipi,
  307. const struct drm_simple_display_pipe_funcs *pipe_funcs,
  308. struct drm_driver *driver,
  309. const struct drm_display_mode *mode, unsigned int rotation)
  310. {
  311. size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
  312. struct tinydrm_device *tdev = &mipi->tinydrm;
  313. int ret;
  314. if (!mipi->command)
  315. return -EINVAL;
  316. mutex_init(&mipi->cmdlock);
  317. mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
  318. if (!mipi->tx_buf)
  319. return -ENOMEM;
  320. ret = devm_tinydrm_init(dev, tdev, &mipi_dbi_fb_funcs, driver);
  321. if (ret)
  322. return ret;
  323. /* TODO: Maybe add DRM_MODE_CONNECTOR_SPI */
  324. ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
  325. DRM_MODE_CONNECTOR_VIRTUAL,
  326. mipi_dbi_formats,
  327. ARRAY_SIZE(mipi_dbi_formats), mode,
  328. rotation);
  329. if (ret)
  330. return ret;
  331. tdev->drm->mode_config.preferred_depth = 16;
  332. mipi->rotation = rotation;
  333. drm_mode_config_reset(tdev->drm);
  334. DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
  335. tdev->drm->mode_config.preferred_depth, rotation);
  336. return 0;
  337. }
  338. EXPORT_SYMBOL(mipi_dbi_init);
  339. /**
  340. * mipi_dbi_hw_reset - Hardware reset of controller
  341. * @mipi: MIPI DBI structure
  342. *
  343. * Reset controller if the &mipi_dbi->reset gpio is set.
  344. */
  345. void mipi_dbi_hw_reset(struct mipi_dbi *mipi)
  346. {
  347. if (!mipi->reset)
  348. return;
  349. gpiod_set_value_cansleep(mipi->reset, 0);
  350. msleep(20);
  351. gpiod_set_value_cansleep(mipi->reset, 1);
  352. msleep(120);
  353. }
  354. EXPORT_SYMBOL(mipi_dbi_hw_reset);
  355. /**
  356. * mipi_dbi_display_is_on - Check if display is on
  357. * @mipi: MIPI DBI structure
  358. *
  359. * This function checks the Power Mode register (if readable) to see if
  360. * display output is turned on. This can be used to see if the bootloader
  361. * has already turned on the display avoiding flicker when the pipeline is
  362. * enabled.
  363. *
  364. * Returns:
  365. * true if the display can be verified to be on, false otherwise.
  366. */
  367. bool mipi_dbi_display_is_on(struct mipi_dbi *mipi)
  368. {
  369. u8 val;
  370. if (mipi_dbi_command_read(mipi, MIPI_DCS_GET_POWER_MODE, &val))
  371. return false;
  372. val &= ~DCS_POWER_MODE_RESERVED_MASK;
  373. if (val != (DCS_POWER_MODE_DISPLAY |
  374. DCS_POWER_MODE_DISPLAY_NORMAL_MODE | DCS_POWER_MODE_SLEEP_MODE))
  375. return false;
  376. DRM_DEBUG_DRIVER("Display is ON\n");
  377. return true;
  378. }
  379. EXPORT_SYMBOL(mipi_dbi_display_is_on);
  380. #if IS_ENABLED(CONFIG_SPI)
  381. /*
  382. * Many controllers have a max speed of 10MHz, but can be pushed way beyond
  383. * that. Increase reliability by running pixel data at max speed and the rest
  384. * at 10MHz, preventing transfer glitches from messing up the init settings.
  385. */
  386. static u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len)
  387. {
  388. if (len > 64)
  389. return 0; /* use default */
  390. return min_t(u32, 10000000, spi->max_speed_hz);
  391. }
  392. /*
  393. * MIPI DBI Type C Option 1
  394. *
  395. * If the SPI controller doesn't have 9 bits per word support,
  396. * use blocks of 9 bytes to send 8x 9-bit words using a 8-bit SPI transfer.
  397. * Pad partial blocks with MIPI_DCS_NOP (zero).
  398. * This is how the D/C bit (x) is added:
  399. * x7654321
  400. * 0x765432
  401. * 10x76543
  402. * 210x7654
  403. * 3210x765
  404. * 43210x76
  405. * 543210x7
  406. * 6543210x
  407. * 76543210
  408. */
  409. static int mipi_dbi_spi1e_transfer(struct mipi_dbi *mipi, int dc,
  410. const void *buf, size_t len,
  411. unsigned int bpw)
  412. {
  413. bool swap_bytes = (bpw == 16 && tinydrm_machine_little_endian());
  414. size_t chunk, max_chunk = mipi->tx_buf9_len;
  415. struct spi_device *spi = mipi->spi;
  416. struct spi_transfer tr = {
  417. .tx_buf = mipi->tx_buf9,
  418. .bits_per_word = 8,
  419. };
  420. struct spi_message m;
  421. const u8 *src = buf;
  422. int i, ret;
  423. u8 *dst;
  424. if (drm_debug & DRM_UT_DRIVER)
  425. pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
  426. __func__, dc, max_chunk);
  427. tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
  428. spi_message_init_with_transfers(&m, &tr, 1);
  429. if (!dc) {
  430. if (WARN_ON_ONCE(len != 1))
  431. return -EINVAL;
  432. /* Command: pad no-op's (zeroes) at beginning of block */
  433. dst = mipi->tx_buf9;
  434. memset(dst, 0, 9);
  435. dst[8] = *src;
  436. tr.len = 9;
  437. tinydrm_dbg_spi_message(spi, &m);
  438. return spi_sync(spi, &m);
  439. }
  440. /* max with room for adding one bit per byte */
  441. max_chunk = max_chunk / 9 * 8;
  442. /* but no bigger than len */
  443. max_chunk = min(max_chunk, len);
  444. /* 8 byte blocks */
  445. max_chunk = max_t(size_t, 8, max_chunk & ~0x7);
  446. while (len) {
  447. size_t added = 0;
  448. chunk = min(len, max_chunk);
  449. len -= chunk;
  450. dst = mipi->tx_buf9;
  451. if (chunk < 8) {
  452. u8 val, carry = 0;
  453. /* Data: pad no-op's (zeroes) at end of block */
  454. memset(dst, 0, 9);
  455. if (swap_bytes) {
  456. for (i = 1; i < (chunk + 1); i++) {
  457. val = src[1];
  458. *dst++ = carry | BIT(8 - i) | (val >> i);
  459. carry = val << (8 - i);
  460. i++;
  461. val = src[0];
  462. *dst++ = carry | BIT(8 - i) | (val >> i);
  463. carry = val << (8 - i);
  464. src += 2;
  465. }
  466. *dst++ = carry;
  467. } else {
  468. for (i = 1; i < (chunk + 1); i++) {
  469. val = *src++;
  470. *dst++ = carry | BIT(8 - i) | (val >> i);
  471. carry = val << (8 - i);
  472. }
  473. *dst++ = carry;
  474. }
  475. chunk = 8;
  476. added = 1;
  477. } else {
  478. for (i = 0; i < chunk; i += 8) {
  479. if (swap_bytes) {
  480. *dst++ = BIT(7) | (src[1] >> 1);
  481. *dst++ = (src[1] << 7) | BIT(6) | (src[0] >> 2);
  482. *dst++ = (src[0] << 6) | BIT(5) | (src[3] >> 3);
  483. *dst++ = (src[3] << 5) | BIT(4) | (src[2] >> 4);
  484. *dst++ = (src[2] << 4) | BIT(3) | (src[5] >> 5);
  485. *dst++ = (src[5] << 3) | BIT(2) | (src[4] >> 6);
  486. *dst++ = (src[4] << 2) | BIT(1) | (src[7] >> 7);
  487. *dst++ = (src[7] << 1) | BIT(0);
  488. *dst++ = src[6];
  489. } else {
  490. *dst++ = BIT(7) | (src[0] >> 1);
  491. *dst++ = (src[0] << 7) | BIT(6) | (src[1] >> 2);
  492. *dst++ = (src[1] << 6) | BIT(5) | (src[2] >> 3);
  493. *dst++ = (src[2] << 5) | BIT(4) | (src[3] >> 4);
  494. *dst++ = (src[3] << 4) | BIT(3) | (src[4] >> 5);
  495. *dst++ = (src[4] << 3) | BIT(2) | (src[5] >> 6);
  496. *dst++ = (src[5] << 2) | BIT(1) | (src[6] >> 7);
  497. *dst++ = (src[6] << 1) | BIT(0);
  498. *dst++ = src[7];
  499. }
  500. src += 8;
  501. added++;
  502. }
  503. }
  504. tr.len = chunk + added;
  505. tinydrm_dbg_spi_message(spi, &m);
  506. ret = spi_sync(spi, &m);
  507. if (ret)
  508. return ret;
  509. }
  510. return 0;
  511. }
  512. static int mipi_dbi_spi1_transfer(struct mipi_dbi *mipi, int dc,
  513. const void *buf, size_t len,
  514. unsigned int bpw)
  515. {
  516. struct spi_device *spi = mipi->spi;
  517. struct spi_transfer tr = {
  518. .bits_per_word = 9,
  519. };
  520. const u16 *src16 = buf;
  521. const u8 *src8 = buf;
  522. struct spi_message m;
  523. size_t max_chunk;
  524. u16 *dst16;
  525. int ret;
  526. if (!tinydrm_spi_bpw_supported(spi, 9))
  527. return mipi_dbi_spi1e_transfer(mipi, dc, buf, len, bpw);
  528. tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
  529. max_chunk = mipi->tx_buf9_len;
  530. dst16 = mipi->tx_buf9;
  531. if (drm_debug & DRM_UT_DRIVER)
  532. pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
  533. __func__, dc, max_chunk);
  534. max_chunk = min(max_chunk / 2, len);
  535. spi_message_init_with_transfers(&m, &tr, 1);
  536. tr.tx_buf = dst16;
  537. while (len) {
  538. size_t chunk = min(len, max_chunk);
  539. unsigned int i;
  540. if (bpw == 16 && tinydrm_machine_little_endian()) {
  541. for (i = 0; i < (chunk * 2); i += 2) {
  542. dst16[i] = *src16 >> 8;
  543. dst16[i + 1] = *src16++ & 0xFF;
  544. if (dc) {
  545. dst16[i] |= 0x0100;
  546. dst16[i + 1] |= 0x0100;
  547. }
  548. }
  549. } else {
  550. for (i = 0; i < chunk; i++) {
  551. dst16[i] = *src8++;
  552. if (dc)
  553. dst16[i] |= 0x0100;
  554. }
  555. }
  556. tr.len = chunk;
  557. len -= chunk;
  558. tinydrm_dbg_spi_message(spi, &m);
  559. ret = spi_sync(spi, &m);
  560. if (ret)
  561. return ret;
  562. }
  563. return 0;
  564. }
  565. static int mipi_dbi_typec1_command(struct mipi_dbi *mipi, u8 cmd,
  566. u8 *parameters, size_t num)
  567. {
  568. unsigned int bpw = (cmd == MIPI_DCS_WRITE_MEMORY_START) ? 16 : 8;
  569. int ret;
  570. if (mipi_dbi_command_is_read(mipi, cmd))
  571. return -ENOTSUPP;
  572. MIPI_DBI_DEBUG_COMMAND(cmd, parameters, num);
  573. ret = mipi_dbi_spi1_transfer(mipi, 0, &cmd, 1, 8);
  574. if (ret || !num)
  575. return ret;
  576. return mipi_dbi_spi1_transfer(mipi, 1, parameters, num, bpw);
  577. }
  578. /* MIPI DBI Type C Option 3 */
  579. static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
  580. u8 *data, size_t len)
  581. {
  582. struct spi_device *spi = mipi->spi;
  583. u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
  584. spi->max_speed_hz / 2);
  585. struct spi_transfer tr[2] = {
  586. {
  587. .speed_hz = speed_hz,
  588. .tx_buf = &cmd,
  589. .len = 1,
  590. }, {
  591. .speed_hz = speed_hz,
  592. .len = len,
  593. },
  594. };
  595. struct spi_message m;
  596. u8 *buf;
  597. int ret;
  598. if (!len)
  599. return -EINVAL;
  600. /*
  601. * Support non-standard 24-bit and 32-bit Nokia read commands which
  602. * start with a dummy clock, so we need to read an extra byte.
  603. */
  604. if (cmd == MIPI_DCS_GET_DISPLAY_ID ||
  605. cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
  606. if (!(len == 3 || len == 4))
  607. return -EINVAL;
  608. tr[1].len = len + 1;
  609. }
  610. buf = kmalloc(tr[1].len, GFP_KERNEL);
  611. if (!buf)
  612. return -ENOMEM;
  613. tr[1].rx_buf = buf;
  614. gpiod_set_value_cansleep(mipi->dc, 0);
  615. spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
  616. ret = spi_sync(spi, &m);
  617. if (ret)
  618. goto err_free;
  619. tinydrm_dbg_spi_message(spi, &m);
  620. if (tr[1].len == len) {
  621. memcpy(data, buf, len);
  622. } else {
  623. unsigned int i;
  624. for (i = 0; i < len; i++)
  625. data[i] = (buf[i] << 1) | !!(buf[i + 1] & BIT(7));
  626. }
  627. MIPI_DBI_DEBUG_COMMAND(cmd, data, len);
  628. err_free:
  629. kfree(buf);
  630. return ret;
  631. }
  632. static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 cmd,
  633. u8 *par, size_t num)
  634. {
  635. struct spi_device *spi = mipi->spi;
  636. unsigned int bpw = 8;
  637. u32 speed_hz;
  638. int ret;
  639. if (mipi_dbi_command_is_read(mipi, cmd))
  640. return mipi_dbi_typec3_command_read(mipi, cmd, par, num);
  641. MIPI_DBI_DEBUG_COMMAND(cmd, par, num);
  642. gpiod_set_value_cansleep(mipi->dc, 0);
  643. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
  644. ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
  645. if (ret || !num)
  646. return ret;
  647. if (cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
  648. bpw = 16;
  649. gpiod_set_value_cansleep(mipi->dc, 1);
  650. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
  651. return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
  652. }
  653. /**
  654. * mipi_dbi_spi_init - Initialize MIPI DBI SPI interfaced controller
  655. * @spi: SPI device
  656. * @mipi: &mipi_dbi structure to initialize
  657. * @dc: D/C gpio (optional)
  658. *
  659. * This function sets &mipi_dbi->command, enables &mipi->read_commands for the
  660. * usual read commands. It should be followed by a call to mipi_dbi_init() or
  661. * a driver-specific init.
  662. *
  663. * If @dc is set, a Type C Option 3 interface is assumed, if not
  664. * Type C Option 1.
  665. *
  666. * If the SPI master driver doesn't support the necessary bits per word,
  667. * the following transformation is used:
  668. *
  669. * - 9-bit: reorder buffer as 9x 8-bit words, padded with no-op command.
  670. * - 16-bit: if big endian send as 8-bit, if little endian swap bytes
  671. *
  672. * Returns:
  673. * Zero on success, negative error code on failure.
  674. */
  675. int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *mipi,
  676. struct gpio_desc *dc)
  677. {
  678. size_t tx_size = tinydrm_spi_max_transfer_size(spi, 0);
  679. struct device *dev = &spi->dev;
  680. int ret;
  681. if (tx_size < 16) {
  682. DRM_ERROR("SPI transmit buffer too small: %zu\n", tx_size);
  683. return -EINVAL;
  684. }
  685. /*
  686. * Even though it's not the SPI device that does DMA (the master does),
  687. * the dma mask is necessary for the dma_alloc_wc() in
  688. * drm_gem_cma_create(). The dma_addr returned will be a physical
  689. * adddress which might be different from the bus address, but this is
  690. * not a problem since the address will not be used.
  691. * The virtual address is used in the transfer and the SPI core
  692. * re-maps it on the SPI master device using the DMA streaming API
  693. * (spi_map_buf()).
  694. */
  695. if (!dev->coherent_dma_mask) {
  696. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  697. if (ret) {
  698. dev_warn(dev, "Failed to set dma mask %d\n", ret);
  699. return ret;
  700. }
  701. }
  702. mipi->spi = spi;
  703. mipi->read_commands = mipi_dbi_dcs_read_commands;
  704. if (dc) {
  705. mipi->command = mipi_dbi_typec3_command;
  706. mipi->dc = dc;
  707. if (tinydrm_machine_little_endian() &&
  708. !tinydrm_spi_bpw_supported(spi, 16))
  709. mipi->swap_bytes = true;
  710. } else {
  711. mipi->command = mipi_dbi_typec1_command;
  712. mipi->tx_buf9_len = tx_size;
  713. mipi->tx_buf9 = devm_kmalloc(dev, tx_size, GFP_KERNEL);
  714. if (!mipi->tx_buf9)
  715. return -ENOMEM;
  716. }
  717. return 0;
  718. }
  719. EXPORT_SYMBOL(mipi_dbi_spi_init);
  720. #endif /* CONFIG_SPI */
  721. #ifdef CONFIG_DEBUG_FS
  722. static ssize_t mipi_dbi_debugfs_command_write(struct file *file,
  723. const char __user *ubuf,
  724. size_t count, loff_t *ppos)
  725. {
  726. struct seq_file *m = file->private_data;
  727. struct mipi_dbi *mipi = m->private;
  728. u8 val, cmd = 0, parameters[64];
  729. char *buf, *pos, *token;
  730. unsigned int i;
  731. int ret;
  732. buf = memdup_user_nul(ubuf, count);
  733. if (IS_ERR(buf))
  734. return PTR_ERR(buf);
  735. /* strip trailing whitespace */
  736. for (i = count - 1; i > 0; i--)
  737. if (isspace(buf[i]))
  738. buf[i] = '\0';
  739. else
  740. break;
  741. i = 0;
  742. pos = buf;
  743. while (pos) {
  744. token = strsep(&pos, " ");
  745. if (!token) {
  746. ret = -EINVAL;
  747. goto err_free;
  748. }
  749. ret = kstrtou8(token, 16, &val);
  750. if (ret < 0)
  751. goto err_free;
  752. if (token == buf)
  753. cmd = val;
  754. else
  755. parameters[i++] = val;
  756. if (i == 64) {
  757. ret = -E2BIG;
  758. goto err_free;
  759. }
  760. }
  761. ret = mipi_dbi_command_buf(mipi, cmd, parameters, i);
  762. err_free:
  763. kfree(buf);
  764. return ret < 0 ? ret : count;
  765. }
  766. static int mipi_dbi_debugfs_command_show(struct seq_file *m, void *unused)
  767. {
  768. struct mipi_dbi *mipi = m->private;
  769. u8 cmd, val[4];
  770. size_t len;
  771. int ret;
  772. for (cmd = 0; cmd < 255; cmd++) {
  773. if (!mipi_dbi_command_is_read(mipi, cmd))
  774. continue;
  775. switch (cmd) {
  776. case MIPI_DCS_READ_MEMORY_START:
  777. case MIPI_DCS_READ_MEMORY_CONTINUE:
  778. len = 2;
  779. break;
  780. case MIPI_DCS_GET_DISPLAY_ID:
  781. len = 3;
  782. break;
  783. case MIPI_DCS_GET_DISPLAY_STATUS:
  784. len = 4;
  785. break;
  786. default:
  787. len = 1;
  788. break;
  789. }
  790. seq_printf(m, "%02x: ", cmd);
  791. ret = mipi_dbi_command_buf(mipi, cmd, val, len);
  792. if (ret) {
  793. seq_puts(m, "XX\n");
  794. continue;
  795. }
  796. seq_printf(m, "%*phN\n", (int)len, val);
  797. }
  798. return 0;
  799. }
  800. static int mipi_dbi_debugfs_command_open(struct inode *inode,
  801. struct file *file)
  802. {
  803. return single_open(file, mipi_dbi_debugfs_command_show,
  804. inode->i_private);
  805. }
  806. static const struct file_operations mipi_dbi_debugfs_command_fops = {
  807. .owner = THIS_MODULE,
  808. .open = mipi_dbi_debugfs_command_open,
  809. .read = seq_read,
  810. .llseek = seq_lseek,
  811. .release = single_release,
  812. .write = mipi_dbi_debugfs_command_write,
  813. };
  814. static const struct drm_info_list mipi_dbi_debugfs_list[] = {
  815. { "fb", drm_fb_cma_debugfs_show, 0 },
  816. };
  817. /**
  818. * mipi_dbi_debugfs_init - Create debugfs entries
  819. * @minor: DRM minor
  820. *
  821. * This function creates a 'command' debugfs file for sending commands to the
  822. * controller or getting the read command values.
  823. * Drivers can use this as their &drm_driver->debugfs_init callback.
  824. *
  825. * Returns:
  826. * Zero on success, negative error code on failure.
  827. */
  828. int mipi_dbi_debugfs_init(struct drm_minor *minor)
  829. {
  830. struct tinydrm_device *tdev = minor->dev->dev_private;
  831. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  832. umode_t mode = S_IFREG | S_IWUSR;
  833. if (mipi->read_commands)
  834. mode |= S_IRUGO;
  835. debugfs_create_file("command", mode, minor->debugfs_root, mipi,
  836. &mipi_dbi_debugfs_command_fops);
  837. return drm_debugfs_create_files(mipi_dbi_debugfs_list,
  838. ARRAY_SIZE(mipi_dbi_debugfs_list),
  839. minor->debugfs_root, minor);
  840. }
  841. EXPORT_SYMBOL(mipi_dbi_debugfs_init);
  842. #endif
  843. MODULE_LICENSE("GPL");