mipi-dbi.c 25 KB

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