mipi-dbi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. if (!mipi->enabled)
  201. return 0;
  202. full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
  203. fb->width, fb->height);
  204. DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
  205. clip.x1, clip.x2, clip.y1, clip.y2);
  206. if (!mipi->dc || !full || swap ||
  207. fb->format->format == DRM_FORMAT_XRGB8888) {
  208. tr = mipi->tx_buf;
  209. ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
  210. if (ret)
  211. return ret;
  212. } else {
  213. tr = cma_obj->vaddr;
  214. }
  215. mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
  216. (clip.x1 >> 8) & 0xFF, clip.x1 & 0xFF,
  217. (clip.x2 >> 8) & 0xFF, (clip.x2 - 1) & 0xFF);
  218. mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
  219. (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
  220. (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
  221. ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START, tr,
  222. (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
  223. return ret;
  224. }
  225. static const struct drm_framebuffer_funcs mipi_dbi_fb_funcs = {
  226. .destroy = drm_gem_fb_destroy,
  227. .create_handle = drm_gem_fb_create_handle,
  228. .dirty = tinydrm_fb_dirty,
  229. };
  230. /**
  231. * mipi_dbi_enable_flush - MIPI DBI enable helper
  232. * @mipi: MIPI DBI structure
  233. *
  234. * This function sets &mipi_dbi->enabled, flushes the whole framebuffer and
  235. * enables the backlight. Drivers can use this in their
  236. * &drm_simple_display_pipe_funcs->enable callback.
  237. */
  238. void mipi_dbi_enable_flush(struct mipi_dbi *mipi,
  239. struct drm_crtc_state *crtc_state,
  240. struct drm_plane_state *plane_state)
  241. {
  242. struct tinydrm_device *tdev = &mipi->tinydrm;
  243. struct drm_framebuffer *fb = plane_state->fb;
  244. mipi->enabled = true;
  245. if (fb)
  246. tdev->fb_dirty(fb, NULL, 0, 0, NULL, 0);
  247. backlight_enable(mipi->backlight);
  248. }
  249. EXPORT_SYMBOL(mipi_dbi_enable_flush);
  250. static void mipi_dbi_blank(struct mipi_dbi *mipi)
  251. {
  252. struct drm_device *drm = mipi->tinydrm.drm;
  253. u16 height = drm->mode_config.min_height;
  254. u16 width = drm->mode_config.min_width;
  255. size_t len = width * height * 2;
  256. memset(mipi->tx_buf, 0, len);
  257. mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS, 0, 0,
  258. (width >> 8) & 0xFF, (width - 1) & 0xFF);
  259. mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS, 0, 0,
  260. (height >> 8) & 0xFF, (height - 1) & 0xFF);
  261. mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
  262. (u8 *)mipi->tx_buf, len);
  263. }
  264. /**
  265. * mipi_dbi_pipe_disable - MIPI DBI pipe disable helper
  266. * @pipe: Display pipe
  267. *
  268. * This function disables backlight if present, if not the display memory is
  269. * blanked. The regulator is disabled if in use. Drivers can use this as their
  270. * &drm_simple_display_pipe_funcs->disable callback.
  271. */
  272. void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe)
  273. {
  274. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  275. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  276. DRM_DEBUG_KMS("\n");
  277. mipi->enabled = false;
  278. if (mipi->backlight)
  279. backlight_disable(mipi->backlight);
  280. else
  281. mipi_dbi_blank(mipi);
  282. if (mipi->regulator)
  283. regulator_disable(mipi->regulator);
  284. }
  285. EXPORT_SYMBOL(mipi_dbi_pipe_disable);
  286. static const uint32_t mipi_dbi_formats[] = {
  287. DRM_FORMAT_RGB565,
  288. DRM_FORMAT_XRGB8888,
  289. };
  290. /**
  291. * mipi_dbi_init - MIPI DBI initialization
  292. * @dev: Parent device
  293. * @mipi: &mipi_dbi structure to initialize
  294. * @pipe_funcs: Display pipe functions
  295. * @driver: DRM driver
  296. * @mode: Display mode
  297. * @rotation: Initial rotation in degrees Counter Clock Wise
  298. *
  299. * This function initializes a &mipi_dbi structure and it's underlying
  300. * @tinydrm_device. It also sets up the display pipeline.
  301. *
  302. * Supported formats: Native RGB565 and emulated XRGB8888.
  303. *
  304. * Objects created by this function will be automatically freed on driver
  305. * detach (devres).
  306. *
  307. * Returns:
  308. * Zero on success, negative error code on failure.
  309. */
  310. int mipi_dbi_init(struct device *dev, struct mipi_dbi *mipi,
  311. const struct drm_simple_display_pipe_funcs *pipe_funcs,
  312. struct drm_driver *driver,
  313. const struct drm_display_mode *mode, unsigned int rotation)
  314. {
  315. size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
  316. struct tinydrm_device *tdev = &mipi->tinydrm;
  317. int ret;
  318. if (!mipi->command)
  319. return -EINVAL;
  320. mutex_init(&mipi->cmdlock);
  321. mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
  322. if (!mipi->tx_buf)
  323. return -ENOMEM;
  324. ret = devm_tinydrm_init(dev, tdev, &mipi_dbi_fb_funcs, driver);
  325. if (ret)
  326. return ret;
  327. tdev->fb_dirty = mipi_dbi_fb_dirty;
  328. /* TODO: Maybe add DRM_MODE_CONNECTOR_SPI */
  329. ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
  330. DRM_MODE_CONNECTOR_VIRTUAL,
  331. mipi_dbi_formats,
  332. ARRAY_SIZE(mipi_dbi_formats), mode,
  333. rotation);
  334. if (ret)
  335. return ret;
  336. tdev->drm->mode_config.preferred_depth = 16;
  337. mipi->rotation = rotation;
  338. drm_mode_config_reset(tdev->drm);
  339. DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
  340. tdev->drm->mode_config.preferred_depth, rotation);
  341. return 0;
  342. }
  343. EXPORT_SYMBOL(mipi_dbi_init);
  344. /**
  345. * mipi_dbi_hw_reset - Hardware reset of controller
  346. * @mipi: MIPI DBI structure
  347. *
  348. * Reset controller if the &mipi_dbi->reset gpio is set.
  349. */
  350. void mipi_dbi_hw_reset(struct mipi_dbi *mipi)
  351. {
  352. if (!mipi->reset)
  353. return;
  354. gpiod_set_value_cansleep(mipi->reset, 0);
  355. usleep_range(20, 1000);
  356. gpiod_set_value_cansleep(mipi->reset, 1);
  357. msleep(120);
  358. }
  359. EXPORT_SYMBOL(mipi_dbi_hw_reset);
  360. /**
  361. * mipi_dbi_display_is_on - Check if display is on
  362. * @mipi: MIPI DBI structure
  363. *
  364. * This function checks the Power Mode register (if readable) to see if
  365. * display output is turned on. This can be used to see if the bootloader
  366. * has already turned on the display avoiding flicker when the pipeline is
  367. * enabled.
  368. *
  369. * Returns:
  370. * true if the display can be verified to be on, false otherwise.
  371. */
  372. bool mipi_dbi_display_is_on(struct mipi_dbi *mipi)
  373. {
  374. u8 val;
  375. if (mipi_dbi_command_read(mipi, MIPI_DCS_GET_POWER_MODE, &val))
  376. return false;
  377. val &= ~DCS_POWER_MODE_RESERVED_MASK;
  378. /* The poweron/reset value is 08h DCS_POWER_MODE_DISPLAY_NORMAL_MODE */
  379. if (val != (DCS_POWER_MODE_DISPLAY |
  380. DCS_POWER_MODE_DISPLAY_NORMAL_MODE | DCS_POWER_MODE_SLEEP_MODE))
  381. return false;
  382. DRM_DEBUG_DRIVER("Display is ON\n");
  383. return true;
  384. }
  385. EXPORT_SYMBOL(mipi_dbi_display_is_on);
  386. static int mipi_dbi_poweron_reset_conditional(struct mipi_dbi *mipi, bool cond)
  387. {
  388. struct device *dev = mipi->tinydrm.drm->dev;
  389. int ret;
  390. if (mipi->regulator) {
  391. ret = regulator_enable(mipi->regulator);
  392. if (ret) {
  393. DRM_DEV_ERROR(dev, "Failed to enable regulator (%d)\n", ret);
  394. return ret;
  395. }
  396. }
  397. if (cond && mipi_dbi_display_is_on(mipi))
  398. return 1;
  399. mipi_dbi_hw_reset(mipi);
  400. ret = mipi_dbi_command(mipi, MIPI_DCS_SOFT_RESET);
  401. if (ret) {
  402. DRM_DEV_ERROR(dev, "Failed to send reset command (%d)\n", ret);
  403. if (mipi->regulator)
  404. regulator_disable(mipi->regulator);
  405. return ret;
  406. }
  407. /*
  408. * If we did a hw reset, we know the controller is in Sleep mode and
  409. * per MIPI DSC spec should wait 5ms after soft reset. If we didn't,
  410. * we assume worst case and wait 120ms.
  411. */
  412. if (mipi->reset)
  413. usleep_range(5000, 20000);
  414. else
  415. msleep(120);
  416. return 0;
  417. }
  418. /**
  419. * mipi_dbi_poweron_reset - MIPI DBI poweron and reset
  420. * @mipi: MIPI DBI structure
  421. *
  422. * This function enables the regulator if used and does a hardware and software
  423. * reset.
  424. *
  425. * Returns:
  426. * Zero on success, or a negative error code.
  427. */
  428. int mipi_dbi_poweron_reset(struct mipi_dbi *mipi)
  429. {
  430. return mipi_dbi_poweron_reset_conditional(mipi, false);
  431. }
  432. EXPORT_SYMBOL(mipi_dbi_poweron_reset);
  433. /**
  434. * mipi_dbi_poweron_conditional_reset - MIPI DBI poweron and conditional reset
  435. * @mipi: MIPI DBI structure
  436. *
  437. * This function enables the regulator if used and if the display is off, it
  438. * does a hardware and software reset. If mipi_dbi_display_is_on() determines
  439. * that the display is on, no reset is performed.
  440. *
  441. * Returns:
  442. * Zero if the controller was reset, 1 if the display was already on, or a
  443. * negative error code.
  444. */
  445. int mipi_dbi_poweron_conditional_reset(struct mipi_dbi *mipi)
  446. {
  447. return mipi_dbi_poweron_reset_conditional(mipi, true);
  448. }
  449. EXPORT_SYMBOL(mipi_dbi_poweron_conditional_reset);
  450. #if IS_ENABLED(CONFIG_SPI)
  451. /**
  452. * mipi_dbi_spi_cmd_max_speed - get the maximum SPI bus speed
  453. * @spi: SPI device
  454. * @len: The transfer buffer length.
  455. *
  456. * Many controllers have a max speed of 10MHz, but can be pushed way beyond
  457. * that. Increase reliability by running pixel data at max speed and the rest
  458. * at 10MHz, preventing transfer glitches from messing up the init settings.
  459. */
  460. u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len)
  461. {
  462. if (len > 64)
  463. return 0; /* use default */
  464. return min_t(u32, 10000000, spi->max_speed_hz);
  465. }
  466. EXPORT_SYMBOL(mipi_dbi_spi_cmd_max_speed);
  467. /*
  468. * MIPI DBI Type C Option 1
  469. *
  470. * If the SPI controller doesn't have 9 bits per word support,
  471. * use blocks of 9 bytes to send 8x 9-bit words using a 8-bit SPI transfer.
  472. * Pad partial blocks with MIPI_DCS_NOP (zero).
  473. * This is how the D/C bit (x) is added:
  474. * x7654321
  475. * 0x765432
  476. * 10x76543
  477. * 210x7654
  478. * 3210x765
  479. * 43210x76
  480. * 543210x7
  481. * 6543210x
  482. * 76543210
  483. */
  484. static int mipi_dbi_spi1e_transfer(struct mipi_dbi *mipi, int dc,
  485. const void *buf, size_t len,
  486. unsigned int bpw)
  487. {
  488. bool swap_bytes = (bpw == 16 && tinydrm_machine_little_endian());
  489. size_t chunk, max_chunk = mipi->tx_buf9_len;
  490. struct spi_device *spi = mipi->spi;
  491. struct spi_transfer tr = {
  492. .tx_buf = mipi->tx_buf9,
  493. .bits_per_word = 8,
  494. };
  495. struct spi_message m;
  496. const u8 *src = buf;
  497. int i, ret;
  498. u8 *dst;
  499. if (drm_debug & DRM_UT_DRIVER)
  500. pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
  501. __func__, dc, max_chunk);
  502. tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
  503. spi_message_init_with_transfers(&m, &tr, 1);
  504. if (!dc) {
  505. if (WARN_ON_ONCE(len != 1))
  506. return -EINVAL;
  507. /* Command: pad no-op's (zeroes) at beginning of block */
  508. dst = mipi->tx_buf9;
  509. memset(dst, 0, 9);
  510. dst[8] = *src;
  511. tr.len = 9;
  512. tinydrm_dbg_spi_message(spi, &m);
  513. return spi_sync(spi, &m);
  514. }
  515. /* max with room for adding one bit per byte */
  516. max_chunk = max_chunk / 9 * 8;
  517. /* but no bigger than len */
  518. max_chunk = min(max_chunk, len);
  519. /* 8 byte blocks */
  520. max_chunk = max_t(size_t, 8, max_chunk & ~0x7);
  521. while (len) {
  522. size_t added = 0;
  523. chunk = min(len, max_chunk);
  524. len -= chunk;
  525. dst = mipi->tx_buf9;
  526. if (chunk < 8) {
  527. u8 val, carry = 0;
  528. /* Data: pad no-op's (zeroes) at end of block */
  529. memset(dst, 0, 9);
  530. if (swap_bytes) {
  531. for (i = 1; i < (chunk + 1); i++) {
  532. val = src[1];
  533. *dst++ = carry | BIT(8 - i) | (val >> i);
  534. carry = val << (8 - i);
  535. i++;
  536. val = src[0];
  537. *dst++ = carry | BIT(8 - i) | (val >> i);
  538. carry = val << (8 - i);
  539. src += 2;
  540. }
  541. *dst++ = carry;
  542. } else {
  543. for (i = 1; i < (chunk + 1); i++) {
  544. val = *src++;
  545. *dst++ = carry | BIT(8 - i) | (val >> i);
  546. carry = val << (8 - i);
  547. }
  548. *dst++ = carry;
  549. }
  550. chunk = 8;
  551. added = 1;
  552. } else {
  553. for (i = 0; i < chunk; i += 8) {
  554. if (swap_bytes) {
  555. *dst++ = BIT(7) | (src[1] >> 1);
  556. *dst++ = (src[1] << 7) | BIT(6) | (src[0] >> 2);
  557. *dst++ = (src[0] << 6) | BIT(5) | (src[3] >> 3);
  558. *dst++ = (src[3] << 5) | BIT(4) | (src[2] >> 4);
  559. *dst++ = (src[2] << 4) | BIT(3) | (src[5] >> 5);
  560. *dst++ = (src[5] << 3) | BIT(2) | (src[4] >> 6);
  561. *dst++ = (src[4] << 2) | BIT(1) | (src[7] >> 7);
  562. *dst++ = (src[7] << 1) | BIT(0);
  563. *dst++ = src[6];
  564. } else {
  565. *dst++ = BIT(7) | (src[0] >> 1);
  566. *dst++ = (src[0] << 7) | BIT(6) | (src[1] >> 2);
  567. *dst++ = (src[1] << 6) | BIT(5) | (src[2] >> 3);
  568. *dst++ = (src[2] << 5) | BIT(4) | (src[3] >> 4);
  569. *dst++ = (src[3] << 4) | BIT(3) | (src[4] >> 5);
  570. *dst++ = (src[4] << 3) | BIT(2) | (src[5] >> 6);
  571. *dst++ = (src[5] << 2) | BIT(1) | (src[6] >> 7);
  572. *dst++ = (src[6] << 1) | BIT(0);
  573. *dst++ = src[7];
  574. }
  575. src += 8;
  576. added++;
  577. }
  578. }
  579. tr.len = chunk + added;
  580. tinydrm_dbg_spi_message(spi, &m);
  581. ret = spi_sync(spi, &m);
  582. if (ret)
  583. return ret;
  584. }
  585. return 0;
  586. }
  587. static int mipi_dbi_spi1_transfer(struct mipi_dbi *mipi, int dc,
  588. const void *buf, size_t len,
  589. unsigned int bpw)
  590. {
  591. struct spi_device *spi = mipi->spi;
  592. struct spi_transfer tr = {
  593. .bits_per_word = 9,
  594. };
  595. const u16 *src16 = buf;
  596. const u8 *src8 = buf;
  597. struct spi_message m;
  598. size_t max_chunk;
  599. u16 *dst16;
  600. int ret;
  601. if (!tinydrm_spi_bpw_supported(spi, 9))
  602. return mipi_dbi_spi1e_transfer(mipi, dc, buf, len, bpw);
  603. tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
  604. max_chunk = mipi->tx_buf9_len;
  605. dst16 = mipi->tx_buf9;
  606. if (drm_debug & DRM_UT_DRIVER)
  607. pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
  608. __func__, dc, max_chunk);
  609. max_chunk = min(max_chunk / 2, len);
  610. spi_message_init_with_transfers(&m, &tr, 1);
  611. tr.tx_buf = dst16;
  612. while (len) {
  613. size_t chunk = min(len, max_chunk);
  614. unsigned int i;
  615. if (bpw == 16 && tinydrm_machine_little_endian()) {
  616. for (i = 0; i < (chunk * 2); i += 2) {
  617. dst16[i] = *src16 >> 8;
  618. dst16[i + 1] = *src16++ & 0xFF;
  619. if (dc) {
  620. dst16[i] |= 0x0100;
  621. dst16[i + 1] |= 0x0100;
  622. }
  623. }
  624. } else {
  625. for (i = 0; i < chunk; i++) {
  626. dst16[i] = *src8++;
  627. if (dc)
  628. dst16[i] |= 0x0100;
  629. }
  630. }
  631. tr.len = chunk;
  632. len -= chunk;
  633. tinydrm_dbg_spi_message(spi, &m);
  634. ret = spi_sync(spi, &m);
  635. if (ret)
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. static int mipi_dbi_typec1_command(struct mipi_dbi *mipi, u8 cmd,
  641. u8 *parameters, size_t num)
  642. {
  643. unsigned int bpw = (cmd == MIPI_DCS_WRITE_MEMORY_START) ? 16 : 8;
  644. int ret;
  645. if (mipi_dbi_command_is_read(mipi, cmd))
  646. return -ENOTSUPP;
  647. MIPI_DBI_DEBUG_COMMAND(cmd, parameters, num);
  648. ret = mipi_dbi_spi1_transfer(mipi, 0, &cmd, 1, 8);
  649. if (ret || !num)
  650. return ret;
  651. return mipi_dbi_spi1_transfer(mipi, 1, parameters, num, bpw);
  652. }
  653. /* MIPI DBI Type C Option 3 */
  654. static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
  655. u8 *data, size_t len)
  656. {
  657. struct spi_device *spi = mipi->spi;
  658. u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
  659. spi->max_speed_hz / 2);
  660. struct spi_transfer tr[2] = {
  661. {
  662. .speed_hz = speed_hz,
  663. .tx_buf = &cmd,
  664. .len = 1,
  665. }, {
  666. .speed_hz = speed_hz,
  667. .len = len,
  668. },
  669. };
  670. struct spi_message m;
  671. u8 *buf;
  672. int ret;
  673. if (!len)
  674. return -EINVAL;
  675. /*
  676. * Support non-standard 24-bit and 32-bit Nokia read commands which
  677. * start with a dummy clock, so we need to read an extra byte.
  678. */
  679. if (cmd == MIPI_DCS_GET_DISPLAY_ID ||
  680. cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
  681. if (!(len == 3 || len == 4))
  682. return -EINVAL;
  683. tr[1].len = len + 1;
  684. }
  685. buf = kmalloc(tr[1].len, GFP_KERNEL);
  686. if (!buf)
  687. return -ENOMEM;
  688. tr[1].rx_buf = buf;
  689. gpiod_set_value_cansleep(mipi->dc, 0);
  690. spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
  691. ret = spi_sync(spi, &m);
  692. if (ret)
  693. goto err_free;
  694. tinydrm_dbg_spi_message(spi, &m);
  695. if (tr[1].len == len) {
  696. memcpy(data, buf, len);
  697. } else {
  698. unsigned int i;
  699. for (i = 0; i < len; i++)
  700. data[i] = (buf[i] << 1) | !!(buf[i + 1] & BIT(7));
  701. }
  702. MIPI_DBI_DEBUG_COMMAND(cmd, data, len);
  703. err_free:
  704. kfree(buf);
  705. return ret;
  706. }
  707. static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 cmd,
  708. u8 *par, size_t num)
  709. {
  710. struct spi_device *spi = mipi->spi;
  711. unsigned int bpw = 8;
  712. u32 speed_hz;
  713. int ret;
  714. if (mipi_dbi_command_is_read(mipi, cmd))
  715. return mipi_dbi_typec3_command_read(mipi, cmd, par, num);
  716. MIPI_DBI_DEBUG_COMMAND(cmd, par, num);
  717. gpiod_set_value_cansleep(mipi->dc, 0);
  718. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
  719. ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
  720. if (ret || !num)
  721. return ret;
  722. if (cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
  723. bpw = 16;
  724. gpiod_set_value_cansleep(mipi->dc, 1);
  725. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
  726. return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
  727. }
  728. /**
  729. * mipi_dbi_spi_init - Initialize MIPI DBI SPI interfaced controller
  730. * @spi: SPI device
  731. * @mipi: &mipi_dbi structure to initialize
  732. * @dc: D/C gpio (optional)
  733. *
  734. * This function sets &mipi_dbi->command, enables &mipi->read_commands for the
  735. * usual read commands. It should be followed by a call to mipi_dbi_init() or
  736. * a driver-specific init.
  737. *
  738. * If @dc is set, a Type C Option 3 interface is assumed, if not
  739. * Type C Option 1.
  740. *
  741. * If the SPI master driver doesn't support the necessary bits per word,
  742. * the following transformation is used:
  743. *
  744. * - 9-bit: reorder buffer as 9x 8-bit words, padded with no-op command.
  745. * - 16-bit: if big endian send as 8-bit, if little endian swap bytes
  746. *
  747. * Returns:
  748. * Zero on success, negative error code on failure.
  749. */
  750. int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *mipi,
  751. struct gpio_desc *dc)
  752. {
  753. size_t tx_size = tinydrm_spi_max_transfer_size(spi, 0);
  754. struct device *dev = &spi->dev;
  755. int ret;
  756. if (tx_size < 16) {
  757. DRM_ERROR("SPI transmit buffer too small: %zu\n", tx_size);
  758. return -EINVAL;
  759. }
  760. /*
  761. * Even though it's not the SPI device that does DMA (the master does),
  762. * the dma mask is necessary for the dma_alloc_wc() in
  763. * drm_gem_cma_create(). The dma_addr returned will be a physical
  764. * adddress which might be different from the bus address, but this is
  765. * not a problem since the address will not be used.
  766. * The virtual address is used in the transfer and the SPI core
  767. * re-maps it on the SPI master device using the DMA streaming API
  768. * (spi_map_buf()).
  769. */
  770. if (!dev->coherent_dma_mask) {
  771. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  772. if (ret) {
  773. dev_warn(dev, "Failed to set dma mask %d\n", ret);
  774. return ret;
  775. }
  776. }
  777. mipi->spi = spi;
  778. mipi->read_commands = mipi_dbi_dcs_read_commands;
  779. if (dc) {
  780. mipi->command = mipi_dbi_typec3_command;
  781. mipi->dc = dc;
  782. if (tinydrm_machine_little_endian() &&
  783. !tinydrm_spi_bpw_supported(spi, 16))
  784. mipi->swap_bytes = true;
  785. } else {
  786. mipi->command = mipi_dbi_typec1_command;
  787. mipi->tx_buf9_len = tx_size;
  788. mipi->tx_buf9 = devm_kmalloc(dev, tx_size, GFP_KERNEL);
  789. if (!mipi->tx_buf9)
  790. return -ENOMEM;
  791. }
  792. DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(mipi_dbi_spi_init);
  796. #endif /* CONFIG_SPI */
  797. #ifdef CONFIG_DEBUG_FS
  798. static ssize_t mipi_dbi_debugfs_command_write(struct file *file,
  799. const char __user *ubuf,
  800. size_t count, loff_t *ppos)
  801. {
  802. struct seq_file *m = file->private_data;
  803. struct mipi_dbi *mipi = m->private;
  804. u8 val, cmd = 0, parameters[64];
  805. char *buf, *pos, *token;
  806. unsigned int i;
  807. int ret;
  808. buf = memdup_user_nul(ubuf, count);
  809. if (IS_ERR(buf))
  810. return PTR_ERR(buf);
  811. /* strip trailing whitespace */
  812. for (i = count - 1; i > 0; i--)
  813. if (isspace(buf[i]))
  814. buf[i] = '\0';
  815. else
  816. break;
  817. i = 0;
  818. pos = buf;
  819. while (pos) {
  820. token = strsep(&pos, " ");
  821. if (!token) {
  822. ret = -EINVAL;
  823. goto err_free;
  824. }
  825. ret = kstrtou8(token, 16, &val);
  826. if (ret < 0)
  827. goto err_free;
  828. if (token == buf)
  829. cmd = val;
  830. else
  831. parameters[i++] = val;
  832. if (i == 64) {
  833. ret = -E2BIG;
  834. goto err_free;
  835. }
  836. }
  837. ret = mipi_dbi_command_buf(mipi, cmd, parameters, i);
  838. err_free:
  839. kfree(buf);
  840. return ret < 0 ? ret : count;
  841. }
  842. static int mipi_dbi_debugfs_command_show(struct seq_file *m, void *unused)
  843. {
  844. struct mipi_dbi *mipi = m->private;
  845. u8 cmd, val[4];
  846. size_t len;
  847. int ret;
  848. for (cmd = 0; cmd < 255; cmd++) {
  849. if (!mipi_dbi_command_is_read(mipi, cmd))
  850. continue;
  851. switch (cmd) {
  852. case MIPI_DCS_READ_MEMORY_START:
  853. case MIPI_DCS_READ_MEMORY_CONTINUE:
  854. len = 2;
  855. break;
  856. case MIPI_DCS_GET_DISPLAY_ID:
  857. len = 3;
  858. break;
  859. case MIPI_DCS_GET_DISPLAY_STATUS:
  860. len = 4;
  861. break;
  862. default:
  863. len = 1;
  864. break;
  865. }
  866. seq_printf(m, "%02x: ", cmd);
  867. ret = mipi_dbi_command_buf(mipi, cmd, val, len);
  868. if (ret) {
  869. seq_puts(m, "XX\n");
  870. continue;
  871. }
  872. seq_printf(m, "%*phN\n", (int)len, val);
  873. }
  874. return 0;
  875. }
  876. static int mipi_dbi_debugfs_command_open(struct inode *inode,
  877. struct file *file)
  878. {
  879. return single_open(file, mipi_dbi_debugfs_command_show,
  880. inode->i_private);
  881. }
  882. static const struct file_operations mipi_dbi_debugfs_command_fops = {
  883. .owner = THIS_MODULE,
  884. .open = mipi_dbi_debugfs_command_open,
  885. .read = seq_read,
  886. .llseek = seq_lseek,
  887. .release = single_release,
  888. .write = mipi_dbi_debugfs_command_write,
  889. };
  890. /**
  891. * mipi_dbi_debugfs_init - Create debugfs entries
  892. * @minor: DRM minor
  893. *
  894. * This function creates a 'command' debugfs file for sending commands to the
  895. * controller or getting the read command values.
  896. * Drivers can use this as their &drm_driver->debugfs_init callback.
  897. *
  898. * Returns:
  899. * Zero on success, negative error code on failure.
  900. */
  901. int mipi_dbi_debugfs_init(struct drm_minor *minor)
  902. {
  903. struct tinydrm_device *tdev = minor->dev->dev_private;
  904. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  905. umode_t mode = S_IFREG | S_IWUSR;
  906. if (mipi->read_commands)
  907. mode |= S_IRUGO;
  908. debugfs_create_file("command", mode, minor->debugfs_root, mipi,
  909. &mipi_dbi_debugfs_command_fops);
  910. return 0;
  911. }
  912. EXPORT_SYMBOL(mipi_dbi_debugfs_init);
  913. #endif
  914. MODULE_LICENSE("GPL");