mipi-dbi.c 26 KB

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