drm_mipi_dbi.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MIPI Display Bus Interface (DBI) LCD controller support
  4. *
  5. * Copyright 2016 Noralf Trønnes
  6. */
  7. #include <linux/backlight.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/delay.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/module.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/spi/spi.h>
  14. #include <drm/drm_connector.h>
  15. #include <drm/drm_damage_helper.h>
  16. #include <drm/drm_drv.h>
  17. #include <drm/drm_file.h>
  18. #include <drm/drm_format_helper.h>
  19. #include <drm/drm_fourcc.h>
  20. #include <drm/drm_framebuffer.h>
  21. #include <drm/drm_gem.h>
  22. #include <drm/drm_gem_atomic_helper.h>
  23. #include <drm/drm_gem_framebuffer_helper.h>
  24. #include <drm/drm_mipi_dbi.h>
  25. #include <drm/drm_modes.h>
  26. #include <drm/drm_probe_helper.h>
  27. #include <drm/drm_rect.h>
  28. #include <video/mipi_display.h>
  29. #define MIPI_DBI_MAX_SPI_READ_SPEED 2000000 /* 2MHz */
  30. #define DCS_POWER_MODE_DISPLAY BIT(2)
  31. #define DCS_POWER_MODE_DISPLAY_NORMAL_MODE BIT(3)
  32. #define DCS_POWER_MODE_SLEEP_MODE BIT(4)
  33. #define DCS_POWER_MODE_PARTIAL_MODE BIT(5)
  34. #define DCS_POWER_MODE_IDLE_MODE BIT(6)
  35. #define DCS_POWER_MODE_RESERVED_MASK (BIT(0) | BIT(1) | BIT(7))
  36. /**
  37. * DOC: overview
  38. *
  39. * This library provides helpers for MIPI Display Bus Interface (DBI)
  40. * compatible display controllers.
  41. *
  42. * Many controllers for tiny lcd displays are MIPI compliant and can use this
  43. * library. If a controller uses registers 0x2A and 0x2B to set the area to
  44. * update and uses register 0x2C to write to frame memory, it is most likely
  45. * MIPI compliant.
  46. *
  47. * Only MIPI Type 1 displays are supported since a full frame memory is needed.
  48. *
  49. * There are 3 MIPI DBI implementation types:
  50. *
  51. * A. Motorola 6800 type parallel bus
  52. *
  53. * B. Intel 8080 type parallel bus
  54. *
  55. * C. SPI type with 3 options:
  56. *
  57. * 1. 9-bit with the Data/Command signal as the ninth bit
  58. * 2. Same as above except it's sent as 16 bits
  59. * 3. 8-bit with the Data/Command signal as a separate D/CX pin
  60. *
  61. * Currently mipi_dbi only supports Type C options 1 and 3 with
  62. * mipi_dbi_spi_init().
  63. */
  64. #define MIPI_DBI_DEBUG_COMMAND(cmd, data, len) \
  65. ({ \
  66. if (!len) \
  67. DRM_DEBUG_DRIVER("cmd=%02x\n", cmd); \
  68. else if (len <= 32) \
  69. DRM_DEBUG_DRIVER("cmd=%02x, par=%*ph\n", cmd, (int)len, data);\
  70. else \
  71. DRM_DEBUG_DRIVER("cmd=%02x, len=%zu\n", cmd, len); \
  72. })
  73. static const u8 mipi_dbi_dcs_read_commands[] = {
  74. MIPI_DCS_GET_DISPLAY_ID,
  75. MIPI_DCS_GET_RED_CHANNEL,
  76. MIPI_DCS_GET_GREEN_CHANNEL,
  77. MIPI_DCS_GET_BLUE_CHANNEL,
  78. MIPI_DCS_GET_DISPLAY_STATUS,
  79. MIPI_DCS_GET_POWER_MODE,
  80. MIPI_DCS_GET_ADDRESS_MODE,
  81. MIPI_DCS_GET_PIXEL_FORMAT,
  82. MIPI_DCS_GET_DISPLAY_MODE,
  83. MIPI_DCS_GET_SIGNAL_MODE,
  84. MIPI_DCS_GET_DIAGNOSTIC_RESULT,
  85. MIPI_DCS_READ_MEMORY_START,
  86. MIPI_DCS_READ_MEMORY_CONTINUE,
  87. MIPI_DCS_GET_SCANLINE,
  88. MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
  89. MIPI_DCS_GET_CONTROL_DISPLAY,
  90. MIPI_DCS_GET_POWER_SAVE,
  91. MIPI_DCS_GET_CABC_MIN_BRIGHTNESS,
  92. MIPI_DCS_READ_DDB_START,
  93. MIPI_DCS_READ_DDB_CONTINUE,
  94. 0, /* sentinel */
  95. };
  96. static bool mipi_dbi_command_is_read(struct mipi_dbi *dbi, u8 cmd)
  97. {
  98. unsigned int i;
  99. if (!dbi->read_commands)
  100. return false;
  101. for (i = 0; i < 0xff; i++) {
  102. if (!dbi->read_commands[i])
  103. return false;
  104. if (cmd == dbi->read_commands[i])
  105. return true;
  106. }
  107. return false;
  108. }
  109. /**
  110. * mipi_dbi_command_read - MIPI DCS read command
  111. * @dbi: MIPI DBI structure
  112. * @cmd: Command
  113. * @val: Value read
  114. *
  115. * Send MIPI DCS read command to the controller.
  116. *
  117. * Returns:
  118. * Zero on success, negative error code on failure.
  119. */
  120. int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val)
  121. {
  122. if (!dbi->read_commands)
  123. return -EACCES;
  124. if (!mipi_dbi_command_is_read(dbi, cmd))
  125. return -EINVAL;
  126. return mipi_dbi_command_buf(dbi, cmd, val, 1);
  127. }
  128. EXPORT_SYMBOL(mipi_dbi_command_read);
  129. /**
  130. * mipi_dbi_command_buf - MIPI DCS command with parameter(s) in an array
  131. * @dbi: MIPI DBI structure
  132. * @cmd: Command
  133. * @data: Parameter buffer
  134. * @len: Buffer length
  135. *
  136. * Returns:
  137. * Zero on success, negative error code on failure.
  138. */
  139. int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len)
  140. {
  141. u8 *cmdbuf;
  142. int ret;
  143. /* SPI requires dma-safe buffers */
  144. cmdbuf = kmemdup(&cmd, 1, GFP_KERNEL);
  145. if (!cmdbuf)
  146. return -ENOMEM;
  147. mutex_lock(&dbi->cmdlock);
  148. ret = dbi->command(dbi, cmdbuf, data, len);
  149. mutex_unlock(&dbi->cmdlock);
  150. kfree(cmdbuf);
  151. return ret;
  152. }
  153. EXPORT_SYMBOL(mipi_dbi_command_buf);
  154. /* This should only be used by mipi_dbi_command() */
  155. int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data,
  156. size_t len)
  157. {
  158. u8 *buf;
  159. int ret;
  160. buf = kmemdup(data, len, GFP_KERNEL);
  161. if (!buf)
  162. return -ENOMEM;
  163. ret = mipi_dbi_command_buf(dbi, cmd, buf, len);
  164. kfree(buf);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(mipi_dbi_command_stackbuf);
  168. /**
  169. * mipi_dbi_buf_copy - Copy a framebuffer, transforming it if necessary
  170. * @dst: The destination buffer
  171. * @src: The source buffer
  172. * @fb: The source framebuffer
  173. * @clip: Clipping rectangle of the area to be copied
  174. * @swap: When true, swap MSB/LSB of 16-bit values
  175. * @fmtcnv_state: Format-conversion state
  176. *
  177. * Returns:
  178. * Zero on success, negative error code on failure.
  179. */
  180. int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb,
  181. struct drm_rect *clip, bool swap,
  182. struct drm_format_conv_state *fmtcnv_state)
  183. {
  184. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
  185. struct drm_gem_object *gem = drm_gem_fb_get_obj(fb, 0);
  186. struct iosys_map dst_map = IOSYS_MAP_INIT_VADDR(dst);
  187. int ret;
  188. ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
  189. if (ret)
  190. return ret;
  191. switch (fb->format->format) {
  192. case DRM_FORMAT_RGB565:
  193. if (swap)
  194. drm_fb_swab(&dst_map, NULL, src, fb, clip, !gem->import_attach,
  195. fmtcnv_state);
  196. else
  197. drm_fb_memcpy(&dst_map, NULL, src, fb, clip);
  198. break;
  199. case DRM_FORMAT_RGB888:
  200. drm_fb_memcpy(&dst_map, NULL, src, fb, clip);
  201. break;
  202. case DRM_FORMAT_XRGB8888:
  203. switch (dbidev->pixel_format) {
  204. case DRM_FORMAT_RGB565:
  205. drm_fb_xrgb8888_to_rgb565(&dst_map, NULL, src, fb, clip, fmtcnv_state, swap);
  206. break;
  207. case DRM_FORMAT_RGB888:
  208. drm_fb_xrgb8888_to_rgb888(&dst_map, NULL, src, fb, clip, fmtcnv_state);
  209. break;
  210. }
  211. break;
  212. default:
  213. drm_err_once(fb->dev, "Format is not supported: %p4cc\n",
  214. &fb->format->format);
  215. ret = -EINVAL;
  216. }
  217. drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL(mipi_dbi_buf_copy);
  221. static void mipi_dbi_set_window_address(struct mipi_dbi_dev *dbidev,
  222. unsigned int xs, unsigned int xe,
  223. unsigned int ys, unsigned int ye)
  224. {
  225. struct mipi_dbi *dbi = &dbidev->dbi;
  226. xs += dbidev->left_offset;
  227. xe += dbidev->left_offset;
  228. ys += dbidev->top_offset;
  229. ye += dbidev->top_offset;
  230. mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS, (xs >> 8) & 0xff,
  231. xs & 0xff, (xe >> 8) & 0xff, xe & 0xff);
  232. mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS, (ys >> 8) & 0xff,
  233. ys & 0xff, (ye >> 8) & 0xff, ye & 0xff);
  234. }
  235. static void mipi_dbi_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb,
  236. struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state)
  237. {
  238. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
  239. unsigned int height = rect->y2 - rect->y1;
  240. unsigned int width = rect->x2 - rect->x1;
  241. const struct drm_format_info *dst_format;
  242. struct mipi_dbi *dbi = &dbidev->dbi;
  243. bool swap = dbi->swap_bytes;
  244. int ret = 0;
  245. size_t len;
  246. bool full;
  247. void *tr;
  248. full = width == fb->width && height == fb->height;
  249. DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
  250. if (!dbi->dc || !full || swap ||
  251. fb->format->format == DRM_FORMAT_XRGB8888) {
  252. tr = dbidev->tx_buf;
  253. ret = mipi_dbi_buf_copy(tr, src, fb, rect, swap, fmtcnv_state);
  254. if (ret)
  255. goto err_msg;
  256. } else {
  257. tr = src->vaddr; /* TODO: Use mapping abstraction properly */
  258. }
  259. mipi_dbi_set_window_address(dbidev, rect->x1, rect->x2 - 1, rect->y1,
  260. rect->y2 - 1);
  261. if (fb->format->format == DRM_FORMAT_XRGB8888)
  262. dst_format = drm_format_info(dbidev->pixel_format);
  263. else
  264. dst_format = fb->format;
  265. len = drm_format_info_min_pitch(dst_format, 0, width) * height;
  266. ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START, tr, len);
  267. err_msg:
  268. if (ret)
  269. drm_err_once(fb->dev, "Failed to update display %d\n", ret);
  270. }
  271. /**
  272. * mipi_dbi_pipe_mode_valid - MIPI DBI mode-valid helper
  273. * @pipe: Simple display pipe
  274. * @mode: The mode to test
  275. *
  276. * This function validates a given display mode against the MIPI DBI's hardware
  277. * display. Drivers can use this as their &drm_simple_display_pipe_funcs->mode_valid
  278. * callback.
  279. */
  280. enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
  281. const struct drm_display_mode *mode)
  282. {
  283. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  284. return drm_crtc_helper_mode_valid_fixed(&pipe->crtc, mode, &dbidev->mode);
  285. }
  286. EXPORT_SYMBOL(mipi_dbi_pipe_mode_valid);
  287. /**
  288. * mipi_dbi_pipe_update - Display pipe update helper
  289. * @pipe: Simple display pipe
  290. * @old_state: Old plane state
  291. *
  292. * This function handles framebuffer flushing and vblank events. Drivers can use
  293. * this as their &drm_simple_display_pipe_funcs->update callback.
  294. */
  295. void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe,
  296. struct drm_plane_state *old_state)
  297. {
  298. struct drm_plane_state *state = pipe->plane.state;
  299. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
  300. struct drm_framebuffer *fb = state->fb;
  301. struct drm_rect rect;
  302. int idx;
  303. if (!pipe->crtc.state->active)
  304. return;
  305. if (WARN_ON(!fb))
  306. return;
  307. if (!drm_dev_enter(fb->dev, &idx))
  308. return;
  309. if (drm_atomic_helper_damage_merged(old_state, state, &rect))
  310. mipi_dbi_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
  311. &shadow_plane_state->fmtcnv_state);
  312. drm_dev_exit(idx);
  313. }
  314. EXPORT_SYMBOL(mipi_dbi_pipe_update);
  315. /**
  316. * mipi_dbi_enable_flush - MIPI DBI enable helper
  317. * @dbidev: MIPI DBI device structure
  318. * @crtc_state: CRTC state
  319. * @plane_state: Plane state
  320. *
  321. * Flushes the whole framebuffer and enables the backlight. Drivers can use this
  322. * in their &drm_simple_display_pipe_funcs->enable callback.
  323. *
  324. * Note: Drivers which don't use mipi_dbi_pipe_update() because they have custom
  325. * framebuffer flushing, can't use this function since they both use the same
  326. * flushing code.
  327. */
  328. void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev,
  329. struct drm_crtc_state *crtc_state,
  330. struct drm_plane_state *plane_state)
  331. {
  332. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  333. struct drm_framebuffer *fb = plane_state->fb;
  334. struct drm_rect rect = {
  335. .x1 = 0,
  336. .x2 = fb->width,
  337. .y1 = 0,
  338. .y2 = fb->height,
  339. };
  340. int idx;
  341. if (!drm_dev_enter(&dbidev->drm, &idx))
  342. return;
  343. mipi_dbi_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
  344. &shadow_plane_state->fmtcnv_state);
  345. backlight_enable(dbidev->backlight);
  346. drm_dev_exit(idx);
  347. }
  348. EXPORT_SYMBOL(mipi_dbi_enable_flush);
  349. static void mipi_dbi_blank(struct mipi_dbi_dev *dbidev)
  350. {
  351. struct drm_device *drm = &dbidev->drm;
  352. u16 height = drm->mode_config.min_height;
  353. u16 width = drm->mode_config.min_width;
  354. struct mipi_dbi *dbi = &dbidev->dbi;
  355. const struct drm_format_info *dst_format;
  356. size_t len;
  357. int idx;
  358. if (!drm_dev_enter(drm, &idx))
  359. return;
  360. dst_format = drm_format_info(dbidev->pixel_format);
  361. len = drm_format_info_min_pitch(dst_format, 0, width) * height;
  362. memset(dbidev->tx_buf, 0, len);
  363. mipi_dbi_set_window_address(dbidev, 0, width - 1, 0, height - 1);
  364. mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START,
  365. (u8 *)dbidev->tx_buf, len);
  366. drm_dev_exit(idx);
  367. }
  368. /**
  369. * mipi_dbi_pipe_disable - MIPI DBI pipe disable helper
  370. * @pipe: Display pipe
  371. *
  372. * This function disables backlight if present, if not the display memory is
  373. * blanked. The regulator is disabled if in use. Drivers can use this as their
  374. * &drm_simple_display_pipe_funcs->disable callback.
  375. */
  376. void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe)
  377. {
  378. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  379. DRM_DEBUG_KMS("\n");
  380. if (dbidev->backlight)
  381. backlight_disable(dbidev->backlight);
  382. else
  383. mipi_dbi_blank(dbidev);
  384. if (dbidev->regulator)
  385. regulator_disable(dbidev->regulator);
  386. if (dbidev->io_regulator)
  387. regulator_disable(dbidev->io_regulator);
  388. }
  389. EXPORT_SYMBOL(mipi_dbi_pipe_disable);
  390. /**
  391. * mipi_dbi_pipe_begin_fb_access - MIPI DBI pipe begin-access helper
  392. * @pipe: Display pipe
  393. * @plane_state: Plane state
  394. *
  395. * This function implements struct &drm_simple_display_funcs.begin_fb_access.
  396. *
  397. * See drm_gem_begin_shadow_fb_access() for details and mipi_dbi_pipe_cleanup_fb()
  398. * for cleanup.
  399. *
  400. * Returns:
  401. * 0 on success, or a negative errno code otherwise.
  402. */
  403. int mipi_dbi_pipe_begin_fb_access(struct drm_simple_display_pipe *pipe,
  404. struct drm_plane_state *plane_state)
  405. {
  406. return drm_gem_begin_shadow_fb_access(&pipe->plane, plane_state);
  407. }
  408. EXPORT_SYMBOL(mipi_dbi_pipe_begin_fb_access);
  409. /**
  410. * mipi_dbi_pipe_end_fb_access - MIPI DBI pipe end-access helper
  411. * @pipe: Display pipe
  412. * @plane_state: Plane state
  413. *
  414. * This function implements struct &drm_simple_display_funcs.end_fb_access.
  415. *
  416. * See mipi_dbi_pipe_begin_fb_access().
  417. */
  418. void mipi_dbi_pipe_end_fb_access(struct drm_simple_display_pipe *pipe,
  419. struct drm_plane_state *plane_state)
  420. {
  421. drm_gem_end_shadow_fb_access(&pipe->plane, plane_state);
  422. }
  423. EXPORT_SYMBOL(mipi_dbi_pipe_end_fb_access);
  424. /**
  425. * mipi_dbi_pipe_reset_plane - MIPI DBI plane-reset helper
  426. * @pipe: Display pipe
  427. *
  428. * This function implements struct &drm_simple_display_funcs.reset_plane
  429. * for MIPI DBI planes.
  430. */
  431. void mipi_dbi_pipe_reset_plane(struct drm_simple_display_pipe *pipe)
  432. {
  433. drm_gem_reset_shadow_plane(&pipe->plane);
  434. }
  435. EXPORT_SYMBOL(mipi_dbi_pipe_reset_plane);
  436. /**
  437. * mipi_dbi_pipe_duplicate_plane_state - duplicates MIPI DBI plane state
  438. * @pipe: Display pipe
  439. *
  440. * This function implements struct &drm_simple_display_funcs.duplicate_plane_state
  441. * for MIPI DBI planes.
  442. *
  443. * See drm_gem_duplicate_shadow_plane_state() for additional details.
  444. *
  445. * Returns:
  446. * A pointer to a new plane state on success, or NULL otherwise.
  447. */
  448. struct drm_plane_state *mipi_dbi_pipe_duplicate_plane_state(struct drm_simple_display_pipe *pipe)
  449. {
  450. return drm_gem_duplicate_shadow_plane_state(&pipe->plane);
  451. }
  452. EXPORT_SYMBOL(mipi_dbi_pipe_duplicate_plane_state);
  453. /**
  454. * mipi_dbi_pipe_destroy_plane_state - cleans up MIPI DBI plane state
  455. * @pipe: Display pipe
  456. * @plane_state: Plane state
  457. *
  458. * This function implements struct drm_simple_display_funcs.destroy_plane_state
  459. * for MIPI DBI planes.
  460. *
  461. * See drm_gem_destroy_shadow_plane_state() for additional details.
  462. */
  463. void mipi_dbi_pipe_destroy_plane_state(struct drm_simple_display_pipe *pipe,
  464. struct drm_plane_state *plane_state)
  465. {
  466. drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state);
  467. }
  468. EXPORT_SYMBOL(mipi_dbi_pipe_destroy_plane_state);
  469. static int mipi_dbi_connector_get_modes(struct drm_connector *connector)
  470. {
  471. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(connector->dev);
  472. return drm_connector_helper_get_modes_fixed(connector, &dbidev->mode);
  473. }
  474. static const struct drm_connector_helper_funcs mipi_dbi_connector_hfuncs = {
  475. .get_modes = mipi_dbi_connector_get_modes,
  476. };
  477. static const struct drm_connector_funcs mipi_dbi_connector_funcs = {
  478. .reset = drm_atomic_helper_connector_reset,
  479. .fill_modes = drm_helper_probe_single_connector_modes,
  480. .destroy = drm_connector_cleanup,
  481. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  482. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  483. };
  484. static int mipi_dbi_rotate_mode(struct drm_display_mode *mode,
  485. unsigned int rotation)
  486. {
  487. if (rotation == 0 || rotation == 180) {
  488. return 0;
  489. } else if (rotation == 90 || rotation == 270) {
  490. swap(mode->hdisplay, mode->vdisplay);
  491. swap(mode->hsync_start, mode->vsync_start);
  492. swap(mode->hsync_end, mode->vsync_end);
  493. swap(mode->htotal, mode->vtotal);
  494. swap(mode->width_mm, mode->height_mm);
  495. return 0;
  496. } else {
  497. return -EINVAL;
  498. }
  499. }
  500. static const struct drm_mode_config_funcs mipi_dbi_mode_config_funcs = {
  501. .fb_create = drm_gem_fb_create_with_dirty,
  502. .atomic_check = drm_atomic_helper_check,
  503. .atomic_commit = drm_atomic_helper_commit,
  504. };
  505. static const uint32_t mipi_dbi_formats[] = {
  506. DRM_FORMAT_RGB565,
  507. DRM_FORMAT_XRGB8888,
  508. };
  509. /**
  510. * mipi_dbi_dev_init_with_formats - MIPI DBI device initialization with custom formats
  511. * @dbidev: MIPI DBI device structure to initialize
  512. * @funcs: Display pipe functions
  513. * @formats: Array of supported formats (DRM_FORMAT\_\*).
  514. * @format_count: Number of elements in @formats
  515. * @mode: Display mode
  516. * @rotation: Initial rotation in degrees Counter Clock Wise
  517. * @tx_buf_size: Allocate a transmit buffer of this size.
  518. *
  519. * This function sets up a &drm_simple_display_pipe with a &drm_connector that
  520. * has one fixed &drm_display_mode which is rotated according to @rotation.
  521. * This mode is used to set the mode config min/max width/height properties.
  522. *
  523. * Use mipi_dbi_dev_init() if you want native RGB565 and emulated XRGB8888 format.
  524. *
  525. * Note:
  526. * Some of the helper functions expects RGB565 to be the default format and the
  527. * transmit buffer sized to fit that.
  528. *
  529. * Returns:
  530. * Zero on success, negative error code on failure.
  531. */
  532. int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev,
  533. const struct drm_simple_display_pipe_funcs *funcs,
  534. const uint32_t *formats, unsigned int format_count,
  535. const struct drm_display_mode *mode,
  536. unsigned int rotation, size_t tx_buf_size)
  537. {
  538. static const uint64_t modifiers[] = {
  539. DRM_FORMAT_MOD_LINEAR,
  540. DRM_FORMAT_MOD_INVALID
  541. };
  542. struct drm_device *drm = &dbidev->drm;
  543. int ret;
  544. if (!dbidev->dbi.command)
  545. return -EINVAL;
  546. ret = drmm_mode_config_init(drm);
  547. if (ret)
  548. return ret;
  549. dbidev->tx_buf = devm_kmalloc(drm->dev, tx_buf_size, GFP_KERNEL);
  550. if (!dbidev->tx_buf)
  551. return -ENOMEM;
  552. drm_mode_copy(&dbidev->mode, mode);
  553. ret = mipi_dbi_rotate_mode(&dbidev->mode, rotation);
  554. if (ret) {
  555. DRM_ERROR("Illegal rotation value %u\n", rotation);
  556. return -EINVAL;
  557. }
  558. drm_connector_helper_add(&dbidev->connector, &mipi_dbi_connector_hfuncs);
  559. ret = drm_connector_init(drm, &dbidev->connector, &mipi_dbi_connector_funcs,
  560. DRM_MODE_CONNECTOR_SPI);
  561. if (ret)
  562. return ret;
  563. ret = drm_simple_display_pipe_init(drm, &dbidev->pipe, funcs, formats, format_count,
  564. modifiers, &dbidev->connector);
  565. if (ret)
  566. return ret;
  567. drm_plane_enable_fb_damage_clips(&dbidev->pipe.plane);
  568. drm->mode_config.funcs = &mipi_dbi_mode_config_funcs;
  569. drm->mode_config.min_width = dbidev->mode.hdisplay;
  570. drm->mode_config.max_width = dbidev->mode.hdisplay;
  571. drm->mode_config.min_height = dbidev->mode.vdisplay;
  572. drm->mode_config.max_height = dbidev->mode.vdisplay;
  573. dbidev->rotation = rotation;
  574. dbidev->pixel_format = formats[0];
  575. if (formats[0] == DRM_FORMAT_RGB888)
  576. dbidev->dbi.write_memory_bpw = 8;
  577. DRM_DEBUG_KMS("rotation = %u\n", rotation);
  578. return 0;
  579. }
  580. EXPORT_SYMBOL(mipi_dbi_dev_init_with_formats);
  581. /**
  582. * mipi_dbi_dev_init - MIPI DBI device initialization
  583. * @dbidev: MIPI DBI device structure to initialize
  584. * @funcs: Display pipe functions
  585. * @mode: Display mode
  586. * @rotation: Initial rotation in degrees Counter Clock Wise
  587. *
  588. * This function sets up a &drm_simple_display_pipe with a &drm_connector that
  589. * has one fixed &drm_display_mode which is rotated according to @rotation.
  590. * This mode is used to set the mode config min/max width/height properties.
  591. * Additionally &mipi_dbi.tx_buf is allocated.
  592. *
  593. * Supported formats: Native RGB565 and emulated XRGB8888.
  594. *
  595. * Returns:
  596. * Zero on success, negative error code on failure.
  597. */
  598. int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev,
  599. const struct drm_simple_display_pipe_funcs *funcs,
  600. const struct drm_display_mode *mode, unsigned int rotation)
  601. {
  602. size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
  603. dbidev->drm.mode_config.preferred_depth = 16;
  604. return mipi_dbi_dev_init_with_formats(dbidev, funcs, mipi_dbi_formats,
  605. ARRAY_SIZE(mipi_dbi_formats), mode,
  606. rotation, bufsize);
  607. }
  608. EXPORT_SYMBOL(mipi_dbi_dev_init);
  609. /**
  610. * mipi_dbi_hw_reset - Hardware reset of controller
  611. * @dbi: MIPI DBI structure
  612. *
  613. * Reset controller if the &mipi_dbi->reset gpio is set.
  614. */
  615. void mipi_dbi_hw_reset(struct mipi_dbi *dbi)
  616. {
  617. if (!dbi->reset)
  618. return;
  619. gpiod_set_value_cansleep(dbi->reset, 0);
  620. usleep_range(20, 1000);
  621. gpiod_set_value_cansleep(dbi->reset, 1);
  622. msleep(120);
  623. }
  624. EXPORT_SYMBOL(mipi_dbi_hw_reset);
  625. /**
  626. * mipi_dbi_display_is_on - Check if display is on
  627. * @dbi: MIPI DBI structure
  628. *
  629. * This function checks the Power Mode register (if readable) to see if
  630. * display output is turned on. This can be used to see if the bootloader
  631. * has already turned on the display avoiding flicker when the pipeline is
  632. * enabled.
  633. *
  634. * Returns:
  635. * true if the display can be verified to be on, false otherwise.
  636. */
  637. bool mipi_dbi_display_is_on(struct mipi_dbi *dbi)
  638. {
  639. u8 val;
  640. if (mipi_dbi_command_read(dbi, MIPI_DCS_GET_POWER_MODE, &val))
  641. return false;
  642. val &= ~DCS_POWER_MODE_RESERVED_MASK;
  643. /* The poweron/reset value is 08h DCS_POWER_MODE_DISPLAY_NORMAL_MODE */
  644. if (val != (DCS_POWER_MODE_DISPLAY |
  645. DCS_POWER_MODE_DISPLAY_NORMAL_MODE | DCS_POWER_MODE_SLEEP_MODE))
  646. return false;
  647. DRM_DEBUG_DRIVER("Display is ON\n");
  648. return true;
  649. }
  650. EXPORT_SYMBOL(mipi_dbi_display_is_on);
  651. static int mipi_dbi_poweron_reset_conditional(struct mipi_dbi_dev *dbidev, bool cond)
  652. {
  653. struct device *dev = dbidev->drm.dev;
  654. struct mipi_dbi *dbi = &dbidev->dbi;
  655. int ret;
  656. if (dbidev->regulator) {
  657. ret = regulator_enable(dbidev->regulator);
  658. if (ret) {
  659. DRM_DEV_ERROR(dev, "Failed to enable regulator (%d)\n", ret);
  660. return ret;
  661. }
  662. }
  663. if (dbidev->io_regulator) {
  664. ret = regulator_enable(dbidev->io_regulator);
  665. if (ret) {
  666. DRM_DEV_ERROR(dev, "Failed to enable I/O regulator (%d)\n", ret);
  667. if (dbidev->regulator)
  668. regulator_disable(dbidev->regulator);
  669. return ret;
  670. }
  671. }
  672. if (cond && mipi_dbi_display_is_on(dbi))
  673. return 1;
  674. mipi_dbi_hw_reset(dbi);
  675. ret = mipi_dbi_command(dbi, MIPI_DCS_SOFT_RESET);
  676. if (ret) {
  677. DRM_DEV_ERROR(dev, "Failed to send reset command (%d)\n", ret);
  678. if (dbidev->regulator)
  679. regulator_disable(dbidev->regulator);
  680. if (dbidev->io_regulator)
  681. regulator_disable(dbidev->io_regulator);
  682. return ret;
  683. }
  684. /*
  685. * If we did a hw reset, we know the controller is in Sleep mode and
  686. * per MIPI DSC spec should wait 5ms after soft reset. If we didn't,
  687. * we assume worst case and wait 120ms.
  688. */
  689. if (dbi->reset)
  690. usleep_range(5000, 20000);
  691. else
  692. msleep(120);
  693. return 0;
  694. }
  695. /**
  696. * mipi_dbi_poweron_reset - MIPI DBI poweron and reset
  697. * @dbidev: MIPI DBI device structure
  698. *
  699. * This function enables the regulator if used and does a hardware and software
  700. * reset.
  701. *
  702. * Returns:
  703. * Zero on success, or a negative error code.
  704. */
  705. int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev)
  706. {
  707. return mipi_dbi_poweron_reset_conditional(dbidev, false);
  708. }
  709. EXPORT_SYMBOL(mipi_dbi_poweron_reset);
  710. /**
  711. * mipi_dbi_poweron_conditional_reset - MIPI DBI poweron and conditional reset
  712. * @dbidev: MIPI DBI device structure
  713. *
  714. * This function enables the regulator if used and if the display is off, it
  715. * does a hardware and software reset. If mipi_dbi_display_is_on() determines
  716. * that the display is on, no reset is performed.
  717. *
  718. * Returns:
  719. * Zero if the controller was reset, 1 if the display was already on, or a
  720. * negative error code.
  721. */
  722. int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev)
  723. {
  724. return mipi_dbi_poweron_reset_conditional(dbidev, true);
  725. }
  726. EXPORT_SYMBOL(mipi_dbi_poweron_conditional_reset);
  727. #if IS_ENABLED(CONFIG_SPI)
  728. /**
  729. * mipi_dbi_spi_cmd_max_speed - get the maximum SPI bus speed
  730. * @spi: SPI device
  731. * @len: The transfer buffer length.
  732. *
  733. * Many controllers have a max speed of 10MHz, but can be pushed way beyond
  734. * that. Increase reliability by running pixel data at max speed and the rest
  735. * at 10MHz, preventing transfer glitches from messing up the init settings.
  736. */
  737. u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len)
  738. {
  739. if (len > 64)
  740. return 0; /* use default */
  741. return min_t(u32, 10000000, spi->max_speed_hz);
  742. }
  743. EXPORT_SYMBOL(mipi_dbi_spi_cmd_max_speed);
  744. /*
  745. * MIPI DBI Type C Option 1
  746. *
  747. * If the SPI controller doesn't have 9 bits per word support,
  748. * use blocks of 9 bytes to send 8x 9-bit words using a 8-bit SPI transfer.
  749. * Pad partial blocks with MIPI_DCS_NOP (zero).
  750. * This is how the D/C bit (x) is added:
  751. * x7654321
  752. * 0x765432
  753. * 10x76543
  754. * 210x7654
  755. * 3210x765
  756. * 43210x76
  757. * 543210x7
  758. * 6543210x
  759. * 76543210
  760. */
  761. static int mipi_dbi_spi1e_transfer(struct mipi_dbi *dbi, int dc,
  762. const void *buf, size_t len,
  763. unsigned int bpw)
  764. {
  765. bool swap_bytes = (bpw == 16);
  766. size_t chunk, max_chunk = dbi->tx_buf9_len;
  767. struct spi_device *spi = dbi->spi;
  768. struct spi_transfer tr = {
  769. .tx_buf = dbi->tx_buf9,
  770. .bits_per_word = 8,
  771. };
  772. struct spi_message m;
  773. const u8 *src = buf;
  774. int i, ret;
  775. u8 *dst;
  776. if (drm_debug_enabled(DRM_UT_DRIVER))
  777. pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
  778. __func__, dc, max_chunk);
  779. tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
  780. spi_message_init_with_transfers(&m, &tr, 1);
  781. if (!dc) {
  782. if (WARN_ON_ONCE(len != 1))
  783. return -EINVAL;
  784. /* Command: pad no-op's (zeroes) at beginning of block */
  785. dst = dbi->tx_buf9;
  786. memset(dst, 0, 9);
  787. dst[8] = *src;
  788. tr.len = 9;
  789. return spi_sync(spi, &m);
  790. }
  791. /* max with room for adding one bit per byte */
  792. max_chunk = max_chunk / 9 * 8;
  793. /* but no bigger than len */
  794. max_chunk = min(max_chunk, len);
  795. /* 8 byte blocks */
  796. max_chunk = max_t(size_t, 8, max_chunk & ~0x7);
  797. while (len) {
  798. size_t added = 0;
  799. chunk = min(len, max_chunk);
  800. len -= chunk;
  801. dst = dbi->tx_buf9;
  802. if (chunk < 8) {
  803. u8 val, carry = 0;
  804. /* Data: pad no-op's (zeroes) at end of block */
  805. memset(dst, 0, 9);
  806. if (swap_bytes) {
  807. for (i = 1; i < (chunk + 1); i++) {
  808. val = src[1];
  809. *dst++ = carry | BIT(8 - i) | (val >> i);
  810. carry = val << (8 - i);
  811. i++;
  812. val = src[0];
  813. *dst++ = carry | BIT(8 - i) | (val >> i);
  814. carry = val << (8 - i);
  815. src += 2;
  816. }
  817. *dst++ = carry;
  818. } else {
  819. for (i = 1; i < (chunk + 1); i++) {
  820. val = *src++;
  821. *dst++ = carry | BIT(8 - i) | (val >> i);
  822. carry = val << (8 - i);
  823. }
  824. *dst++ = carry;
  825. }
  826. chunk = 8;
  827. added = 1;
  828. } else {
  829. for (i = 0; i < chunk; i += 8) {
  830. if (swap_bytes) {
  831. *dst++ = BIT(7) | (src[1] >> 1);
  832. *dst++ = (src[1] << 7) | BIT(6) | (src[0] >> 2);
  833. *dst++ = (src[0] << 6) | BIT(5) | (src[3] >> 3);
  834. *dst++ = (src[3] << 5) | BIT(4) | (src[2] >> 4);
  835. *dst++ = (src[2] << 4) | BIT(3) | (src[5] >> 5);
  836. *dst++ = (src[5] << 3) | BIT(2) | (src[4] >> 6);
  837. *dst++ = (src[4] << 2) | BIT(1) | (src[7] >> 7);
  838. *dst++ = (src[7] << 1) | BIT(0);
  839. *dst++ = src[6];
  840. } else {
  841. *dst++ = BIT(7) | (src[0] >> 1);
  842. *dst++ = (src[0] << 7) | BIT(6) | (src[1] >> 2);
  843. *dst++ = (src[1] << 6) | BIT(5) | (src[2] >> 3);
  844. *dst++ = (src[2] << 5) | BIT(4) | (src[3] >> 4);
  845. *dst++ = (src[3] << 4) | BIT(3) | (src[4] >> 5);
  846. *dst++ = (src[4] << 3) | BIT(2) | (src[5] >> 6);
  847. *dst++ = (src[5] << 2) | BIT(1) | (src[6] >> 7);
  848. *dst++ = (src[6] << 1) | BIT(0);
  849. *dst++ = src[7];
  850. }
  851. src += 8;
  852. added++;
  853. }
  854. }
  855. tr.len = chunk + added;
  856. ret = spi_sync(spi, &m);
  857. if (ret)
  858. return ret;
  859. }
  860. return 0;
  861. }
  862. static int mipi_dbi_spi1_transfer(struct mipi_dbi *dbi, int dc,
  863. const void *buf, size_t len,
  864. unsigned int bpw)
  865. {
  866. struct spi_device *spi = dbi->spi;
  867. struct spi_transfer tr = {
  868. .bits_per_word = 9,
  869. };
  870. const u16 *src16 = buf;
  871. const u8 *src8 = buf;
  872. struct spi_message m;
  873. size_t max_chunk;
  874. u16 *dst16;
  875. int ret;
  876. if (!spi_is_bpw_supported(spi, 9))
  877. return mipi_dbi_spi1e_transfer(dbi, dc, buf, len, bpw);
  878. tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
  879. max_chunk = dbi->tx_buf9_len;
  880. dst16 = dbi->tx_buf9;
  881. if (drm_debug_enabled(DRM_UT_DRIVER))
  882. pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
  883. __func__, dc, max_chunk);
  884. max_chunk = min(max_chunk / 2, len);
  885. spi_message_init_with_transfers(&m, &tr, 1);
  886. tr.tx_buf = dst16;
  887. while (len) {
  888. size_t chunk = min(len, max_chunk);
  889. unsigned int i;
  890. if (bpw == 16) {
  891. for (i = 0; i < (chunk * 2); i += 2) {
  892. dst16[i] = *src16 >> 8;
  893. dst16[i + 1] = *src16++ & 0xFF;
  894. if (dc) {
  895. dst16[i] |= 0x0100;
  896. dst16[i + 1] |= 0x0100;
  897. }
  898. }
  899. } else {
  900. for (i = 0; i < chunk; i++) {
  901. dst16[i] = *src8++;
  902. if (dc)
  903. dst16[i] |= 0x0100;
  904. }
  905. }
  906. tr.len = chunk * 2;
  907. len -= chunk;
  908. ret = spi_sync(spi, &m);
  909. if (ret)
  910. return ret;
  911. }
  912. return 0;
  913. }
  914. static int mipi_dbi_typec1_command_read(struct mipi_dbi *dbi, u8 *cmd,
  915. u8 *data, size_t len)
  916. {
  917. struct spi_device *spi = dbi->spi;
  918. u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
  919. spi->max_speed_hz / 2);
  920. struct spi_transfer tr[2] = {
  921. {
  922. .speed_hz = speed_hz,
  923. .bits_per_word = 9,
  924. .tx_buf = dbi->tx_buf9,
  925. .len = 2,
  926. }, {
  927. .speed_hz = speed_hz,
  928. .bits_per_word = 8,
  929. .len = len,
  930. .rx_buf = data,
  931. },
  932. };
  933. struct spi_message m;
  934. u16 *dst16;
  935. int ret;
  936. if (!len)
  937. return -EINVAL;
  938. if (!spi_is_bpw_supported(spi, 9)) {
  939. /*
  940. * FIXME: implement something like mipi_dbi_spi1e_transfer() but
  941. * for reads using emulation.
  942. */
  943. dev_err(&spi->dev,
  944. "reading on host not supporting 9 bpw not yet implemented\n");
  945. return -EOPNOTSUPP;
  946. }
  947. /*
  948. * Turn the 8bit command into a 16bit version of the command in the
  949. * buffer. Only 9 bits of this will be used when executing the actual
  950. * transfer.
  951. */
  952. dst16 = dbi->tx_buf9;
  953. dst16[0] = *cmd;
  954. spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
  955. ret = spi_sync(spi, &m);
  956. if (!ret)
  957. MIPI_DBI_DEBUG_COMMAND(*cmd, data, len);
  958. return ret;
  959. }
  960. static int mipi_dbi_typec1_command(struct mipi_dbi *dbi, u8 *cmd,
  961. u8 *parameters, size_t num)
  962. {
  963. unsigned int bpw = 8;
  964. int ret;
  965. if (mipi_dbi_command_is_read(dbi, *cmd))
  966. return mipi_dbi_typec1_command_read(dbi, cmd, parameters, num);
  967. MIPI_DBI_DEBUG_COMMAND(*cmd, parameters, num);
  968. ret = mipi_dbi_spi1_transfer(dbi, 0, cmd, 1, 8);
  969. if (ret || !num)
  970. return ret;
  971. if (*cmd == MIPI_DCS_WRITE_MEMORY_START)
  972. bpw = dbi->write_memory_bpw;
  973. return mipi_dbi_spi1_transfer(dbi, 1, parameters, num, bpw);
  974. }
  975. /* MIPI DBI Type C Option 3 */
  976. static int mipi_dbi_typec3_command_read(struct mipi_dbi *dbi, u8 *cmd,
  977. u8 *data, size_t len)
  978. {
  979. struct spi_device *spi = dbi->spi;
  980. u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
  981. spi->max_speed_hz / 2);
  982. struct spi_transfer tr[2] = {
  983. {
  984. .speed_hz = speed_hz,
  985. .tx_buf = cmd,
  986. .len = 1,
  987. }, {
  988. .speed_hz = speed_hz,
  989. .len = len,
  990. },
  991. };
  992. struct spi_message m;
  993. u8 *buf;
  994. int ret;
  995. if (!len)
  996. return -EINVAL;
  997. /*
  998. * Support non-standard 24-bit and 32-bit Nokia read commands which
  999. * start with a dummy clock, so we need to read an extra byte.
  1000. */
  1001. if (*cmd == MIPI_DCS_GET_DISPLAY_ID ||
  1002. *cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
  1003. if (!(len == 3 || len == 4))
  1004. return -EINVAL;
  1005. tr[1].len = len + 1;
  1006. }
  1007. buf = kmalloc(tr[1].len, GFP_KERNEL);
  1008. if (!buf)
  1009. return -ENOMEM;
  1010. tr[1].rx_buf = buf;
  1011. spi_bus_lock(spi->controller);
  1012. gpiod_set_value_cansleep(dbi->dc, 0);
  1013. spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
  1014. ret = spi_sync_locked(spi, &m);
  1015. spi_bus_unlock(spi->controller);
  1016. if (ret)
  1017. goto err_free;
  1018. if (tr[1].len == len) {
  1019. memcpy(data, buf, len);
  1020. } else {
  1021. unsigned int i;
  1022. for (i = 0; i < len; i++)
  1023. data[i] = (buf[i] << 1) | (buf[i + 1] >> 7);
  1024. }
  1025. MIPI_DBI_DEBUG_COMMAND(*cmd, data, len);
  1026. err_free:
  1027. kfree(buf);
  1028. return ret;
  1029. }
  1030. static int mipi_dbi_typec3_command(struct mipi_dbi *dbi, u8 *cmd,
  1031. u8 *par, size_t num)
  1032. {
  1033. struct spi_device *spi = dbi->spi;
  1034. unsigned int bpw = 8;
  1035. u32 speed_hz;
  1036. int ret;
  1037. if (mipi_dbi_command_is_read(dbi, *cmd))
  1038. return mipi_dbi_typec3_command_read(dbi, cmd, par, num);
  1039. MIPI_DBI_DEBUG_COMMAND(*cmd, par, num);
  1040. spi_bus_lock(spi->controller);
  1041. gpiod_set_value_cansleep(dbi->dc, 0);
  1042. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
  1043. ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
  1044. spi_bus_unlock(spi->controller);
  1045. if (ret || !num)
  1046. return ret;
  1047. if (*cmd == MIPI_DCS_WRITE_MEMORY_START)
  1048. bpw = dbi->write_memory_bpw;
  1049. spi_bus_lock(spi->controller);
  1050. gpiod_set_value_cansleep(dbi->dc, 1);
  1051. speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
  1052. ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
  1053. spi_bus_unlock(spi->controller);
  1054. return ret;
  1055. }
  1056. /**
  1057. * mipi_dbi_spi_init - Initialize MIPI DBI SPI interface
  1058. * @spi: SPI device
  1059. * @dbi: MIPI DBI structure to initialize
  1060. * @dc: D/C gpio (optional)
  1061. *
  1062. * This function sets &mipi_dbi->command, enables &mipi_dbi->read_commands for the
  1063. * usual read commands. It should be followed by a call to mipi_dbi_dev_init() or
  1064. * a driver-specific init.
  1065. *
  1066. * If @dc is set, a Type C Option 3 interface is assumed, if not
  1067. * Type C Option 1.
  1068. *
  1069. * If the command is %MIPI_DCS_WRITE_MEMORY_START and the pixel format is RGB565, endianness has
  1070. * to be taken into account. The MIPI DBI serial interface is big endian and framebuffers are
  1071. * assumed stored in memory as little endian (%DRM_FORMAT_BIG_ENDIAN is not supported).
  1072. *
  1073. * This is how endianness is handled:
  1074. *
  1075. * Option 1 (D/C as a bit): The buffer is sent on the wire byte by byte so the 16-bit buffer is
  1076. * byteswapped before transfer.
  1077. *
  1078. * Option 3 (D/C as a gpio): If the SPI controller supports 16 bits per word the buffer can be
  1079. * sent as-is. If not the caller is responsible for swapping the bytes
  1080. * before calling mipi_dbi_command_buf() and the buffer is sent 8 bpw.
  1081. *
  1082. * This handling is optimised for %DRM_FORMAT_RGB565 framebuffers.
  1083. *
  1084. * If the interface is Option 1 and the SPI controller doesn't support 9 bits per word,
  1085. * the buffer is sent as 9x 8-bit words, padded with MIPI DCS no-op commands if necessary.
  1086. *
  1087. * Returns:
  1088. * Zero on success, negative error code on failure.
  1089. */
  1090. int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi,
  1091. struct gpio_desc *dc)
  1092. {
  1093. struct device *dev = &spi->dev;
  1094. int ret;
  1095. /*
  1096. * Even though it's not the SPI device that does DMA (the master does),
  1097. * the dma mask is necessary for the dma_alloc_wc() in the GEM code
  1098. * (e.g., drm_gem_dma_create()). The dma_addr returned will be a physical
  1099. * address which might be different from the bus address, but this is
  1100. * not a problem since the address will not be used.
  1101. * The virtual address is used in the transfer and the SPI core
  1102. * re-maps it on the SPI master device using the DMA streaming API
  1103. * (spi_map_buf()).
  1104. */
  1105. if (!dev->coherent_dma_mask) {
  1106. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  1107. if (ret) {
  1108. dev_warn(dev, "Failed to set dma mask %d\n", ret);
  1109. return ret;
  1110. }
  1111. }
  1112. dbi->spi = spi;
  1113. dbi->read_commands = mipi_dbi_dcs_read_commands;
  1114. dbi->write_memory_bpw = 16;
  1115. if (dc) {
  1116. dbi->command = mipi_dbi_typec3_command;
  1117. dbi->dc = dc;
  1118. if (!spi_is_bpw_supported(spi, 16)) {
  1119. dbi->write_memory_bpw = 8;
  1120. dbi->swap_bytes = true;
  1121. }
  1122. } else {
  1123. dbi->command = mipi_dbi_typec1_command;
  1124. dbi->tx_buf9_len = SZ_16K;
  1125. dbi->tx_buf9 = devm_kmalloc(dev, dbi->tx_buf9_len, GFP_KERNEL);
  1126. if (!dbi->tx_buf9)
  1127. return -ENOMEM;
  1128. }
  1129. mutex_init(&dbi->cmdlock);
  1130. DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL(mipi_dbi_spi_init);
  1134. /**
  1135. * mipi_dbi_spi_transfer - SPI transfer helper
  1136. * @spi: SPI device
  1137. * @speed_hz: Override speed (optional)
  1138. * @bpw: Bits per word
  1139. * @buf: Buffer to transfer
  1140. * @len: Buffer length
  1141. *
  1142. * This SPI transfer helper breaks up the transfer of @buf into chunks which
  1143. * the SPI controller driver can handle. The SPI bus must be locked when
  1144. * calling this.
  1145. *
  1146. * Returns:
  1147. * Zero on success, negative error code on failure.
  1148. */
  1149. int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
  1150. u8 bpw, const void *buf, size_t len)
  1151. {
  1152. size_t max_chunk = spi_max_transfer_size(spi);
  1153. struct spi_transfer tr = {
  1154. .bits_per_word = bpw,
  1155. .speed_hz = speed_hz,
  1156. };
  1157. struct spi_message m;
  1158. size_t chunk;
  1159. int ret;
  1160. /* In __spi_validate, there's a validation that no partial transfers
  1161. * are accepted (xfer->len % w_size must be zero).
  1162. * Here we align max_chunk to multiple of 2 (16bits),
  1163. * to prevent transfers from being rejected.
  1164. */
  1165. max_chunk = ALIGN_DOWN(max_chunk, 2);
  1166. spi_message_init_with_transfers(&m, &tr, 1);
  1167. while (len) {
  1168. chunk = min(len, max_chunk);
  1169. tr.tx_buf = buf;
  1170. tr.len = chunk;
  1171. buf += chunk;
  1172. len -= chunk;
  1173. ret = spi_sync_locked(spi, &m);
  1174. if (ret)
  1175. return ret;
  1176. }
  1177. return 0;
  1178. }
  1179. EXPORT_SYMBOL(mipi_dbi_spi_transfer);
  1180. #endif /* CONFIG_SPI */
  1181. #ifdef CONFIG_DEBUG_FS
  1182. static ssize_t mipi_dbi_debugfs_command_write(struct file *file,
  1183. const char __user *ubuf,
  1184. size_t count, loff_t *ppos)
  1185. {
  1186. struct seq_file *m = file->private_data;
  1187. struct mipi_dbi_dev *dbidev = m->private;
  1188. u8 val, cmd = 0, parameters[64];
  1189. char *buf, *pos, *token;
  1190. int i, ret, idx;
  1191. if (!drm_dev_enter(&dbidev->drm, &idx))
  1192. return -ENODEV;
  1193. buf = memdup_user_nul(ubuf, count);
  1194. if (IS_ERR(buf)) {
  1195. ret = PTR_ERR(buf);
  1196. goto err_exit;
  1197. }
  1198. /* strip trailing whitespace */
  1199. for (i = count - 1; i > 0; i--)
  1200. if (isspace(buf[i]))
  1201. buf[i] = '\0';
  1202. else
  1203. break;
  1204. i = 0;
  1205. pos = buf;
  1206. while (pos) {
  1207. token = strsep(&pos, " ");
  1208. if (!token) {
  1209. ret = -EINVAL;
  1210. goto err_free;
  1211. }
  1212. ret = kstrtou8(token, 16, &val);
  1213. if (ret < 0)
  1214. goto err_free;
  1215. if (token == buf)
  1216. cmd = val;
  1217. else
  1218. parameters[i++] = val;
  1219. if (i == 64) {
  1220. ret = -E2BIG;
  1221. goto err_free;
  1222. }
  1223. }
  1224. ret = mipi_dbi_command_buf(&dbidev->dbi, cmd, parameters, i);
  1225. err_free:
  1226. kfree(buf);
  1227. err_exit:
  1228. drm_dev_exit(idx);
  1229. return ret < 0 ? ret : count;
  1230. }
  1231. static int mipi_dbi_debugfs_command_show(struct seq_file *m, void *unused)
  1232. {
  1233. struct mipi_dbi_dev *dbidev = m->private;
  1234. struct mipi_dbi *dbi = &dbidev->dbi;
  1235. u8 cmd, val[4];
  1236. int ret, idx;
  1237. size_t len;
  1238. if (!drm_dev_enter(&dbidev->drm, &idx))
  1239. return -ENODEV;
  1240. for (cmd = 0; cmd < 255; cmd++) {
  1241. if (!mipi_dbi_command_is_read(dbi, cmd))
  1242. continue;
  1243. switch (cmd) {
  1244. case MIPI_DCS_READ_MEMORY_START:
  1245. case MIPI_DCS_READ_MEMORY_CONTINUE:
  1246. len = 2;
  1247. break;
  1248. case MIPI_DCS_GET_DISPLAY_ID:
  1249. len = 3;
  1250. break;
  1251. case MIPI_DCS_GET_DISPLAY_STATUS:
  1252. len = 4;
  1253. break;
  1254. default:
  1255. len = 1;
  1256. break;
  1257. }
  1258. seq_printf(m, "%02x: ", cmd);
  1259. ret = mipi_dbi_command_buf(dbi, cmd, val, len);
  1260. if (ret) {
  1261. seq_puts(m, "XX\n");
  1262. continue;
  1263. }
  1264. seq_printf(m, "%*phN\n", (int)len, val);
  1265. }
  1266. drm_dev_exit(idx);
  1267. return 0;
  1268. }
  1269. static int mipi_dbi_debugfs_command_open(struct inode *inode,
  1270. struct file *file)
  1271. {
  1272. return single_open(file, mipi_dbi_debugfs_command_show,
  1273. inode->i_private);
  1274. }
  1275. static const struct file_operations mipi_dbi_debugfs_command_fops = {
  1276. .owner = THIS_MODULE,
  1277. .open = mipi_dbi_debugfs_command_open,
  1278. .read = seq_read,
  1279. .llseek = seq_lseek,
  1280. .release = single_release,
  1281. .write = mipi_dbi_debugfs_command_write,
  1282. };
  1283. /**
  1284. * mipi_dbi_debugfs_init - Create debugfs entries
  1285. * @minor: DRM minor
  1286. *
  1287. * This function creates a 'command' debugfs file for sending commands to the
  1288. * controller or getting the read command values.
  1289. * Drivers can use this as their &drm_driver->debugfs_init callback.
  1290. *
  1291. */
  1292. void mipi_dbi_debugfs_init(struct drm_minor *minor)
  1293. {
  1294. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(minor->dev);
  1295. umode_t mode = S_IFREG | S_IWUSR;
  1296. if (dbidev->dbi.read_commands)
  1297. mode |= S_IRUGO;
  1298. debugfs_create_file("command", mode, minor->debugfs_root, dbidev,
  1299. &mipi_dbi_debugfs_command_fops);
  1300. }
  1301. EXPORT_SYMBOL(mipi_dbi_debugfs_init);
  1302. #endif
  1303. MODULE_DESCRIPTION("MIPI Display Bus Interface (DBI) LCD controller support");
  1304. MODULE_LICENSE("GPL");