mipi-dbi.c 27 KB

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