drm_mipi_dbi.c 34 KB

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