drm_mipi_dbi.c 39 KB

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