mipi-dbi.c 27 KB

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