drm_mipi_dsi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /*
  2. * MIPI DSI Bus
  3. *
  4. * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
  5. * Andrzej Hajda <a.hajda@samsung.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <drm/drm_mipi_dsi.h>
  28. #include <linux/device.h>
  29. #include <linux/module.h>
  30. #include <linux/of_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/slab.h>
  33. #include <drm/drm_dsc.h>
  34. #include <drm/drm_print.h>
  35. #include <video/mipi_display.h>
  36. /**
  37. * DOC: dsi helpers
  38. *
  39. * These functions contain some common logic and helpers to deal with MIPI DSI
  40. * peripherals.
  41. *
  42. * Helpers are provided for a number of standard MIPI DSI command as well as a
  43. * subset of the MIPI DCS command set.
  44. */
  45. static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
  46. {
  47. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  48. /* attempt OF style match */
  49. if (of_driver_match_device(dev, drv))
  50. return 1;
  51. /* compare DSI device and driver names */
  52. if (!strcmp(dsi->name, drv->name))
  53. return 1;
  54. return 0;
  55. }
  56. static int mipi_dsi_uevent(struct device *dev, struct kobj_uevent_env *env)
  57. {
  58. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  59. int err;
  60. err = of_device_uevent_modalias(dev, env);
  61. if (err != -ENODEV)
  62. return err;
  63. add_uevent_var(env, "MODALIAS=%s%s", MIPI_DSI_MODULE_PREFIX,
  64. dsi->name);
  65. return 0;
  66. }
  67. static const struct dev_pm_ops mipi_dsi_device_pm_ops = {
  68. .runtime_suspend = pm_generic_runtime_suspend,
  69. .runtime_resume = pm_generic_runtime_resume,
  70. .suspend = pm_generic_suspend,
  71. .resume = pm_generic_resume,
  72. .freeze = pm_generic_freeze,
  73. .thaw = pm_generic_thaw,
  74. .poweroff = pm_generic_poweroff,
  75. .restore = pm_generic_restore,
  76. };
  77. static struct bus_type mipi_dsi_bus_type = {
  78. .name = "mipi-dsi",
  79. .match = mipi_dsi_device_match,
  80. .uevent = mipi_dsi_uevent,
  81. .pm = &mipi_dsi_device_pm_ops,
  82. };
  83. /**
  84. * of_find_mipi_dsi_device_by_node() - find the MIPI DSI device matching a
  85. * device tree node
  86. * @np: device tree node
  87. *
  88. * Return: A pointer to the MIPI DSI device corresponding to @np or NULL if no
  89. * such device exists (or has not been registered yet).
  90. */
  91. struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np)
  92. {
  93. struct device *dev;
  94. dev = bus_find_device_by_of_node(&mipi_dsi_bus_type, np);
  95. return dev ? to_mipi_dsi_device(dev) : NULL;
  96. }
  97. EXPORT_SYMBOL(of_find_mipi_dsi_device_by_node);
  98. static void mipi_dsi_dev_release(struct device *dev)
  99. {
  100. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  101. of_node_put(dev->of_node);
  102. kfree(dsi);
  103. }
  104. static const struct device_type mipi_dsi_device_type = {
  105. .release = mipi_dsi_dev_release,
  106. };
  107. static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
  108. {
  109. struct mipi_dsi_device *dsi;
  110. dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
  111. if (!dsi)
  112. return ERR_PTR(-ENOMEM);
  113. dsi->host = host;
  114. dsi->dev.bus = &mipi_dsi_bus_type;
  115. dsi->dev.parent = host->dev;
  116. dsi->dev.type = &mipi_dsi_device_type;
  117. device_initialize(&dsi->dev);
  118. return dsi;
  119. }
  120. static int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
  121. {
  122. struct mipi_dsi_host *host = dsi->host;
  123. dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev), dsi->channel);
  124. return device_add(&dsi->dev);
  125. }
  126. #if IS_ENABLED(CONFIG_OF)
  127. static struct mipi_dsi_device *
  128. of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
  129. {
  130. struct mipi_dsi_device_info info = { };
  131. int ret;
  132. u32 reg;
  133. if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
  134. drm_err(host, "modalias failure on %pOF\n", node);
  135. return ERR_PTR(-EINVAL);
  136. }
  137. ret = of_property_read_u32(node, "reg", &reg);
  138. if (ret) {
  139. drm_err(host, "device node %pOF has no valid reg property: %d\n",
  140. node, ret);
  141. return ERR_PTR(-EINVAL);
  142. }
  143. info.channel = reg;
  144. info.node = of_node_get(node);
  145. return mipi_dsi_device_register_full(host, &info);
  146. }
  147. #else
  148. static struct mipi_dsi_device *
  149. of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
  150. {
  151. return ERR_PTR(-ENODEV);
  152. }
  153. #endif
  154. /**
  155. * mipi_dsi_device_register_full - create a MIPI DSI device
  156. * @host: DSI host to which this device is connected
  157. * @info: pointer to template containing DSI device information
  158. *
  159. * Create a MIPI DSI device by using the device information provided by
  160. * mipi_dsi_device_info template
  161. *
  162. * Returns:
  163. * A pointer to the newly created MIPI DSI device, or, a pointer encoded
  164. * with an error
  165. */
  166. struct mipi_dsi_device *
  167. mipi_dsi_device_register_full(struct mipi_dsi_host *host,
  168. const struct mipi_dsi_device_info *info)
  169. {
  170. struct mipi_dsi_device *dsi;
  171. int ret;
  172. if (!info) {
  173. drm_err(host, "invalid mipi_dsi_device_info pointer\n");
  174. return ERR_PTR(-EINVAL);
  175. }
  176. if (info->channel > 3) {
  177. drm_err(host, "invalid virtual channel: %u\n", info->channel);
  178. return ERR_PTR(-EINVAL);
  179. }
  180. dsi = mipi_dsi_device_alloc(host);
  181. if (IS_ERR(dsi)) {
  182. drm_err(host, "failed to allocate DSI device %ld\n",
  183. PTR_ERR(dsi));
  184. return dsi;
  185. }
  186. dsi->dev.of_node = info->node;
  187. dsi->channel = info->channel;
  188. strlcpy(dsi->name, info->type, sizeof(dsi->name));
  189. ret = mipi_dsi_device_add(dsi);
  190. if (ret) {
  191. drm_err(host, "failed to add DSI device %d\n", ret);
  192. kfree(dsi);
  193. return ERR_PTR(ret);
  194. }
  195. return dsi;
  196. }
  197. EXPORT_SYMBOL(mipi_dsi_device_register_full);
  198. /**
  199. * mipi_dsi_device_unregister - unregister MIPI DSI device
  200. * @dsi: DSI peripheral device
  201. */
  202. void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi)
  203. {
  204. device_unregister(&dsi->dev);
  205. }
  206. EXPORT_SYMBOL(mipi_dsi_device_unregister);
  207. static DEFINE_MUTEX(host_lock);
  208. static LIST_HEAD(host_list);
  209. /**
  210. * of_find_mipi_dsi_host_by_node() - find the MIPI DSI host matching a
  211. * device tree node
  212. * @node: device tree node
  213. *
  214. * Returns:
  215. * A pointer to the MIPI DSI host corresponding to @node or NULL if no
  216. * such device exists (or has not been registered yet).
  217. */
  218. struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node)
  219. {
  220. struct mipi_dsi_host *host;
  221. mutex_lock(&host_lock);
  222. list_for_each_entry(host, &host_list, list) {
  223. if (host->dev->of_node == node) {
  224. mutex_unlock(&host_lock);
  225. return host;
  226. }
  227. }
  228. mutex_unlock(&host_lock);
  229. return NULL;
  230. }
  231. EXPORT_SYMBOL(of_find_mipi_dsi_host_by_node);
  232. int mipi_dsi_host_register(struct mipi_dsi_host *host)
  233. {
  234. struct device_node *node;
  235. for_each_available_child_of_node(host->dev->of_node, node) {
  236. /* skip nodes without reg property */
  237. if (!of_find_property(node, "reg", NULL))
  238. continue;
  239. of_mipi_dsi_device_add(host, node);
  240. }
  241. mutex_lock(&host_lock);
  242. list_add_tail(&host->list, &host_list);
  243. mutex_unlock(&host_lock);
  244. return 0;
  245. }
  246. EXPORT_SYMBOL(mipi_dsi_host_register);
  247. static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
  248. {
  249. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  250. mipi_dsi_device_unregister(dsi);
  251. return 0;
  252. }
  253. void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
  254. {
  255. device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
  256. mutex_lock(&host_lock);
  257. list_del_init(&host->list);
  258. mutex_unlock(&host_lock);
  259. }
  260. EXPORT_SYMBOL(mipi_dsi_host_unregister);
  261. /**
  262. * mipi_dsi_attach - attach a DSI device to its DSI host
  263. * @dsi: DSI peripheral
  264. */
  265. int mipi_dsi_attach(struct mipi_dsi_device *dsi)
  266. {
  267. const struct mipi_dsi_host_ops *ops = dsi->host->ops;
  268. if (!ops || !ops->attach)
  269. return -ENOSYS;
  270. return ops->attach(dsi->host, dsi);
  271. }
  272. EXPORT_SYMBOL(mipi_dsi_attach);
  273. /**
  274. * mipi_dsi_detach - detach a DSI device from its DSI host
  275. * @dsi: DSI peripheral
  276. */
  277. int mipi_dsi_detach(struct mipi_dsi_device *dsi)
  278. {
  279. const struct mipi_dsi_host_ops *ops = dsi->host->ops;
  280. if (!ops || !ops->detach)
  281. return -ENOSYS;
  282. return ops->detach(dsi->host, dsi);
  283. }
  284. EXPORT_SYMBOL(mipi_dsi_detach);
  285. static ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
  286. struct mipi_dsi_msg *msg)
  287. {
  288. const struct mipi_dsi_host_ops *ops = dsi->host->ops;
  289. if (!ops || !ops->transfer)
  290. return -ENOSYS;
  291. if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
  292. msg->flags |= MIPI_DSI_MSG_USE_LPM;
  293. msg->flags |= MIPI_DSI_MSG_LASTCOMMAND;
  294. return ops->transfer(dsi->host, msg);
  295. }
  296. /**
  297. * mipi_dsi_packet_format_is_short - check if a packet is of the short format
  298. * @type: MIPI DSI data type of the packet
  299. *
  300. * Return: true if the packet for the given data type is a short packet, false
  301. * otherwise.
  302. */
  303. bool mipi_dsi_packet_format_is_short(u8 type)
  304. {
  305. switch (type) {
  306. case MIPI_DSI_V_SYNC_START:
  307. case MIPI_DSI_V_SYNC_END:
  308. case MIPI_DSI_H_SYNC_START:
  309. case MIPI_DSI_H_SYNC_END:
  310. case MIPI_DSI_COMPRESSION_MODE:
  311. case MIPI_DSI_END_OF_TRANSMISSION:
  312. case MIPI_DSI_COLOR_MODE_OFF:
  313. case MIPI_DSI_COLOR_MODE_ON:
  314. case MIPI_DSI_SHUTDOWN_PERIPHERAL:
  315. case MIPI_DSI_TURN_ON_PERIPHERAL:
  316. case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
  317. case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
  318. case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
  319. case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
  320. case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
  321. case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
  322. case MIPI_DSI_DCS_SHORT_WRITE:
  323. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  324. case MIPI_DSI_DCS_READ:
  325. case MIPI_DSI_EXECUTE_QUEUE:
  326. case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
  327. return true;
  328. }
  329. return false;
  330. }
  331. EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
  332. /**
  333. * mipi_dsi_packet_format_is_long - check if a packet is of the long format
  334. * @type: MIPI DSI data type of the packet
  335. *
  336. * Return: true if the packet for the given data type is a long packet, false
  337. * otherwise.
  338. */
  339. bool mipi_dsi_packet_format_is_long(u8 type)
  340. {
  341. switch (type) {
  342. case MIPI_DSI_NULL_PACKET:
  343. case MIPI_DSI_BLANKING_PACKET:
  344. case MIPI_DSI_GENERIC_LONG_WRITE:
  345. case MIPI_DSI_DCS_LONG_WRITE:
  346. case MIPI_DSI_PICTURE_PARAMETER_SET:
  347. case MIPI_DSI_COMPRESSED_PIXEL_STREAM:
  348. case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
  349. case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
  350. case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
  351. case MIPI_DSI_PACKED_PIXEL_STREAM_30:
  352. case MIPI_DSI_PACKED_PIXEL_STREAM_36:
  353. case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
  354. case MIPI_DSI_PACKED_PIXEL_STREAM_16:
  355. case MIPI_DSI_PACKED_PIXEL_STREAM_18:
  356. case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
  357. case MIPI_DSI_PACKED_PIXEL_STREAM_24:
  358. return true;
  359. }
  360. return false;
  361. }
  362. EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
  363. /**
  364. * mipi_dsi_create_packet - create a packet from a message according to the
  365. * DSI protocol
  366. * @packet: pointer to a DSI packet structure
  367. * @msg: message to translate into a packet
  368. *
  369. * Return: 0 on success or a negative error code on failure.
  370. */
  371. int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
  372. const struct mipi_dsi_msg *msg)
  373. {
  374. if (!packet || !msg)
  375. return -EINVAL;
  376. /* do some minimum sanity checking */
  377. if (!mipi_dsi_packet_format_is_short(msg->type) &&
  378. !mipi_dsi_packet_format_is_long(msg->type))
  379. return -EINVAL;
  380. if (msg->channel > 3)
  381. return -EINVAL;
  382. memset(packet, 0, sizeof(*packet));
  383. packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
  384. /* TODO: compute ECC if hardware support is not available */
  385. /*
  386. * Long write packets contain the word count in header bytes 1 and 2.
  387. * The payload follows the header and is word count bytes long.
  388. *
  389. * Short write packets encode up to two parameters in header bytes 1
  390. * and 2.
  391. */
  392. if (mipi_dsi_packet_format_is_long(msg->type)) {
  393. packet->header[1] = (msg->tx_len >> 0) & 0xff;
  394. packet->header[2] = (msg->tx_len >> 8) & 0xff;
  395. packet->payload_length = msg->tx_len;
  396. packet->payload = msg->tx_buf;
  397. } else {
  398. const u8 *tx = msg->tx_buf;
  399. packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
  400. packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
  401. }
  402. packet->size = sizeof(packet->header) + packet->payload_length;
  403. return 0;
  404. }
  405. EXPORT_SYMBOL(mipi_dsi_create_packet);
  406. /**
  407. * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
  408. * @dsi: DSI peripheral device
  409. *
  410. * Return: 0 on success or a negative error code on failure.
  411. */
  412. int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
  413. {
  414. struct mipi_dsi_msg msg = {
  415. .channel = dsi->channel,
  416. .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
  417. .tx_buf = (u8 [2]) { 0, 0 },
  418. .tx_len = 2,
  419. };
  420. int ret = mipi_dsi_device_transfer(dsi, &msg);
  421. return (ret < 0) ? ret : 0;
  422. }
  423. EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
  424. /**
  425. * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
  426. * @dsi: DSI peripheral device
  427. *
  428. * Return: 0 on success or a negative error code on failure.
  429. */
  430. int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
  431. {
  432. struct mipi_dsi_msg msg = {
  433. .channel = dsi->channel,
  434. .type = MIPI_DSI_TURN_ON_PERIPHERAL,
  435. .tx_buf = (u8 [2]) { 0, 0 },
  436. .tx_len = 2,
  437. };
  438. int ret = mipi_dsi_device_transfer(dsi, &msg);
  439. return (ret < 0) ? ret : 0;
  440. }
  441. EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
  442. /*
  443. * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
  444. * the payload in a long packet transmitted from the peripheral back to the
  445. * host processor
  446. * @dsi: DSI peripheral device
  447. * @value: the maximum size of the payload
  448. *
  449. * Return: 0 on success or a negative error code on failure.
  450. */
  451. int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
  452. u16 value)
  453. {
  454. u8 tx[2] = { value & 0xff, value >> 8 };
  455. struct mipi_dsi_msg msg = {
  456. .channel = dsi->channel,
  457. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  458. .tx_len = sizeof(tx),
  459. .tx_buf = tx,
  460. };
  461. int ret = mipi_dsi_device_transfer(dsi, &msg);
  462. return (ret < 0) ? ret : 0;
  463. }
  464. EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
  465. /**
  466. * mipi_dsi_compression_mode() - enable/disable DSC on the peripheral
  467. * @dsi: DSI peripheral device
  468. * @enable: Whether to enable or disable the DSC
  469. *
  470. * Enable or disable Display Stream Compression on the peripheral using the
  471. * default Picture Parameter Set and VESA DSC 1.1 algorithm.
  472. *
  473. * Return: 0 on success or a negative error code on failure.
  474. */
  475. ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable)
  476. {
  477. /* Note: Needs updating for non-default PPS or algorithm */
  478. u8 tx[2] = { enable << 0, 0 };
  479. struct mipi_dsi_msg msg = {
  480. .channel = dsi->channel,
  481. .type = MIPI_DSI_COMPRESSION_MODE,
  482. .tx_len = sizeof(tx),
  483. .tx_buf = tx,
  484. };
  485. int ret = mipi_dsi_device_transfer(dsi, &msg);
  486. return (ret < 0) ? ret : 0;
  487. }
  488. EXPORT_SYMBOL(mipi_dsi_compression_mode);
  489. /**
  490. * mipi_dsi_picture_parameter_set() - transmit the DSC PPS to the peripheral
  491. * @dsi: DSI peripheral device
  492. * @pps: VESA DSC 1.1 Picture Parameter Set
  493. *
  494. * Transmit the VESA DSC 1.1 Picture Parameter Set to the peripheral.
  495. *
  496. * Return: 0 on success or a negative error code on failure.
  497. */
  498. ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
  499. const struct drm_dsc_picture_parameter_set *pps)
  500. {
  501. struct mipi_dsi_msg msg = {
  502. .channel = dsi->channel,
  503. .type = MIPI_DSI_PICTURE_PARAMETER_SET,
  504. .tx_len = sizeof(*pps),
  505. .tx_buf = pps,
  506. };
  507. int ret = mipi_dsi_device_transfer(dsi, &msg);
  508. return (ret < 0) ? ret : 0;
  509. }
  510. EXPORT_SYMBOL(mipi_dsi_picture_parameter_set);
  511. /**
  512. * mipi_dsi_generic_write() - transmit data using a generic write packet
  513. * @dsi: DSI peripheral device
  514. * @payload: buffer containing the payload
  515. * @size: size of payload buffer
  516. *
  517. * This function will automatically choose the right data type depending on
  518. * the payload length.
  519. *
  520. * Return: The number of bytes transmitted on success or a negative error code
  521. * on failure.
  522. */
  523. ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
  524. size_t size)
  525. {
  526. struct mipi_dsi_msg msg = {
  527. .channel = dsi->channel,
  528. .tx_buf = payload,
  529. .tx_len = size
  530. };
  531. switch (size) {
  532. case 0:
  533. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
  534. break;
  535. case 1:
  536. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
  537. break;
  538. case 2:
  539. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
  540. break;
  541. default:
  542. msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
  543. break;
  544. }
  545. return mipi_dsi_device_transfer(dsi, &msg);
  546. }
  547. EXPORT_SYMBOL(mipi_dsi_generic_write);
  548. /**
  549. * mipi_dsi_generic_read() - receive data using a generic read packet
  550. * @dsi: DSI peripheral device
  551. * @params: buffer containing the request parameters
  552. * @num_params: number of request parameters
  553. * @data: buffer in which to return the received data
  554. * @size: size of receive buffer
  555. *
  556. * This function will automatically choose the right data type depending on
  557. * the number of parameters passed in.
  558. *
  559. * Return: The number of bytes successfully read or a negative error code on
  560. * failure.
  561. */
  562. ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
  563. size_t num_params, void *data, size_t size)
  564. {
  565. struct mipi_dsi_msg msg = {
  566. .channel = dsi->channel,
  567. .tx_len = num_params,
  568. .tx_buf = params,
  569. .rx_len = size,
  570. .rx_buf = data
  571. };
  572. switch (num_params) {
  573. case 0:
  574. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
  575. break;
  576. case 1:
  577. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
  578. break;
  579. case 2:
  580. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
  581. break;
  582. default:
  583. return -EINVAL;
  584. }
  585. return mipi_dsi_device_transfer(dsi, &msg);
  586. }
  587. EXPORT_SYMBOL(mipi_dsi_generic_read);
  588. /**
  589. * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
  590. * @dsi: DSI peripheral device
  591. * @data: buffer containing data to be transmitted
  592. * @len: size of transmission buffer
  593. *
  594. * This function will automatically choose the right data type depending on
  595. * the command payload length.
  596. *
  597. * Return: The number of bytes successfully transmitted or a negative error
  598. * code on failure.
  599. */
  600. ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
  601. const void *data, size_t len)
  602. {
  603. struct mipi_dsi_msg msg = {
  604. .channel = dsi->channel,
  605. .tx_buf = data,
  606. .tx_len = len
  607. };
  608. switch (len) {
  609. case 0:
  610. return -EINVAL;
  611. case 1:
  612. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  613. break;
  614. case 2:
  615. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  616. break;
  617. default:
  618. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  619. break;
  620. }
  621. return mipi_dsi_device_transfer(dsi, &msg);
  622. }
  623. EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
  624. /**
  625. * mipi_dsi_dcs_write() - send DCS write command
  626. * @dsi: DSI peripheral device
  627. * @cmd: DCS command
  628. * @data: buffer containing the command payload
  629. * @len: command payload length
  630. *
  631. * This function will automatically choose the right data type depending on
  632. * the command payload length.
  633. *
  634. * Return: The number of bytes successfully transmitted or a negative error
  635. * code on failure.
  636. */
  637. ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
  638. const void *data, size_t len)
  639. {
  640. ssize_t err;
  641. size_t size;
  642. u8 stack_tx[8];
  643. u8 *tx;
  644. size = 1 + len;
  645. if (len > ARRAY_SIZE(stack_tx) - 1) {
  646. tx = kmalloc(size, GFP_KERNEL);
  647. if (!tx)
  648. return -ENOMEM;
  649. } else {
  650. tx = stack_tx;
  651. }
  652. /* concatenate the DCS command byte and the payload */
  653. tx[0] = cmd;
  654. if (data)
  655. memcpy(&tx[1], data, len);
  656. err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
  657. if (tx != stack_tx)
  658. kfree(tx);
  659. return err;
  660. }
  661. EXPORT_SYMBOL(mipi_dsi_dcs_write);
  662. /**
  663. * mipi_dsi_dcs_read() - send DCS read request command
  664. * @dsi: DSI peripheral device
  665. * @cmd: DCS command
  666. * @data: buffer in which to receive data
  667. * @len: size of receive buffer
  668. *
  669. * Return: The number of bytes read or a negative error code on failure.
  670. */
  671. ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
  672. size_t len)
  673. {
  674. struct mipi_dsi_msg msg = {
  675. .channel = dsi->channel,
  676. .type = MIPI_DSI_DCS_READ,
  677. .tx_buf = &cmd,
  678. .tx_len = 1,
  679. .rx_buf = data,
  680. .rx_len = len
  681. };
  682. return mipi_dsi_device_transfer(dsi, &msg);
  683. }
  684. EXPORT_SYMBOL(mipi_dsi_dcs_read);
  685. /**
  686. * mipi_dsi_dcs_nop() - send DCS nop packet
  687. * @dsi: DSI peripheral device
  688. *
  689. * Return: 0 on success or a negative error code on failure.
  690. */
  691. int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
  692. {
  693. ssize_t err;
  694. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
  695. if (err < 0)
  696. return err;
  697. return 0;
  698. }
  699. EXPORT_SYMBOL(mipi_dsi_dcs_nop);
  700. /**
  701. * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
  702. * @dsi: DSI peripheral device
  703. *
  704. * Return: 0 on success or a negative error code on failure.
  705. */
  706. int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
  707. {
  708. ssize_t err;
  709. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
  710. if (err < 0)
  711. return err;
  712. return 0;
  713. }
  714. EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
  715. /**
  716. * mipi_dsi_dcs_get_power_mode() - query the display module's current power
  717. * mode
  718. * @dsi: DSI peripheral device
  719. * @mode: return location for the current power mode
  720. *
  721. * Return: 0 on success or a negative error code on failure.
  722. */
  723. int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
  724. {
  725. ssize_t err;
  726. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
  727. sizeof(*mode));
  728. if (err <= 0) {
  729. if (err == 0)
  730. err = -ENODATA;
  731. return err;
  732. }
  733. return 0;
  734. }
  735. EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
  736. /**
  737. * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
  738. * data used by the interface
  739. * @dsi: DSI peripheral device
  740. * @format: return location for the pixel format
  741. *
  742. * Return: 0 on success or a negative error code on failure.
  743. */
  744. int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
  745. {
  746. ssize_t err;
  747. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
  748. sizeof(*format));
  749. if (err <= 0) {
  750. if (err == 0)
  751. err = -ENODATA;
  752. return err;
  753. }
  754. return 0;
  755. }
  756. EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
  757. /**
  758. * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
  759. * display module except interface communication
  760. * @dsi: DSI peripheral device
  761. *
  762. * Return: 0 on success or a negative error code on failure.
  763. */
  764. int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
  765. {
  766. ssize_t err;
  767. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
  768. if (err < 0)
  769. return err;
  770. return 0;
  771. }
  772. EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
  773. /**
  774. * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
  775. * module
  776. * @dsi: DSI peripheral device
  777. *
  778. * Return: 0 on success or a negative error code on failure.
  779. */
  780. int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
  781. {
  782. ssize_t err;
  783. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
  784. if (err < 0)
  785. return err;
  786. return 0;
  787. }
  788. EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
  789. /**
  790. * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
  791. * display device
  792. * @dsi: DSI peripheral device
  793. *
  794. * Return: 0 on success or a negative error code on failure.
  795. */
  796. int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
  797. {
  798. ssize_t err;
  799. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
  800. if (err < 0)
  801. return err;
  802. return 0;
  803. }
  804. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
  805. /**
  806. * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
  807. * display device
  808. * @dsi: DSI peripheral device
  809. *
  810. * Return: 0 on success or a negative error code on failure
  811. */
  812. int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
  813. {
  814. ssize_t err;
  815. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
  816. if (err < 0)
  817. return err;
  818. return 0;
  819. }
  820. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
  821. /**
  822. * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
  823. * memory accessed by the host processor
  824. * @dsi: DSI peripheral device
  825. * @start: first column of frame memory
  826. * @end: last column of frame memory
  827. *
  828. * Return: 0 on success or a negative error code on failure.
  829. */
  830. int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
  831. u16 end)
  832. {
  833. u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
  834. ssize_t err;
  835. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
  836. sizeof(payload));
  837. if (err < 0)
  838. return err;
  839. return 0;
  840. }
  841. EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
  842. /**
  843. * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
  844. * memory accessed by the host processor
  845. * @dsi: DSI peripheral device
  846. * @start: first page of frame memory
  847. * @end: last page of frame memory
  848. *
  849. * Return: 0 on success or a negative error code on failure.
  850. */
  851. int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
  852. u16 end)
  853. {
  854. u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
  855. ssize_t err;
  856. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
  857. sizeof(payload));
  858. if (err < 0)
  859. return err;
  860. return 0;
  861. }
  862. EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
  863. /**
  864. * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
  865. * output signal on the TE signal line
  866. * @dsi: DSI peripheral device
  867. *
  868. * Return: 0 on success or a negative error code on failure
  869. */
  870. int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
  871. {
  872. ssize_t err;
  873. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
  874. if (err < 0)
  875. return err;
  876. return 0;
  877. }
  878. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
  879. /**
  880. * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
  881. * output signal on the TE signal line.
  882. * @dsi: DSI peripheral device
  883. * @mode: the Tearing Effect Output Line mode
  884. *
  885. * Return: 0 on success or a negative error code on failure
  886. */
  887. int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
  888. enum mipi_dsi_dcs_tear_mode mode)
  889. {
  890. u8 value = mode;
  891. ssize_t err;
  892. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
  893. sizeof(value));
  894. if (err < 0)
  895. return err;
  896. return 0;
  897. }
  898. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
  899. /**
  900. * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
  901. * data used by the interface
  902. * @dsi: DSI peripheral device
  903. * @format: pixel format
  904. *
  905. * Return: 0 on success or a negative error code on failure.
  906. */
  907. int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
  908. {
  909. ssize_t err;
  910. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
  911. sizeof(format));
  912. if (err < 0)
  913. return err;
  914. return 0;
  915. }
  916. EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
  917. /**
  918. * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
  919. * the Tearing Effect output signal of the display module
  920. * @dsi: DSI peripheral device
  921. * @scanline: scanline to use as trigger
  922. *
  923. * Return: 0 on success or a negative error code on failure
  924. */
  925. int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
  926. {
  927. u8 payload[2] = { scanline >> 8, scanline & 0xff };
  928. ssize_t err;
  929. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_SCANLINE, payload,
  930. sizeof(payload));
  931. if (err < 0)
  932. return err;
  933. return 0;
  934. }
  935. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
  936. /**
  937. * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
  938. * display
  939. * @dsi: DSI peripheral device
  940. * @brightness: brightness value
  941. *
  942. * Return: 0 on success or a negative error code on failure.
  943. */
  944. int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
  945. u16 brightness)
  946. {
  947. u8 payload[2] = { brightness & 0xff, brightness >> 8 };
  948. ssize_t err;
  949. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
  950. payload, sizeof(payload));
  951. if (err < 0)
  952. return err;
  953. return 0;
  954. }
  955. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
  956. /**
  957. * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
  958. * of the display
  959. * @dsi: DSI peripheral device
  960. * @brightness: brightness value
  961. *
  962. * Return: 0 on success or a negative error code on failure.
  963. */
  964. int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
  965. u16 *brightness)
  966. {
  967. ssize_t err;
  968. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
  969. brightness, sizeof(*brightness));
  970. if (err <= 0) {
  971. if (err == 0)
  972. err = -ENODATA;
  973. return err;
  974. }
  975. return 0;
  976. }
  977. EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
  978. static int mipi_dsi_drv_probe(struct device *dev)
  979. {
  980. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  981. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  982. return drv->probe(dsi);
  983. }
  984. static int mipi_dsi_drv_remove(struct device *dev)
  985. {
  986. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  987. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  988. return drv->remove(dsi);
  989. }
  990. static void mipi_dsi_drv_shutdown(struct device *dev)
  991. {
  992. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  993. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  994. drv->shutdown(dsi);
  995. }
  996. /**
  997. * mipi_dsi_driver_register_full() - register a driver for DSI devices
  998. * @drv: DSI driver structure
  999. * @owner: owner module
  1000. *
  1001. * Return: 0 on success or a negative error code on failure.
  1002. */
  1003. int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
  1004. struct module *owner)
  1005. {
  1006. drv->driver.bus = &mipi_dsi_bus_type;
  1007. drv->driver.owner = owner;
  1008. if (drv->probe)
  1009. drv->driver.probe = mipi_dsi_drv_probe;
  1010. if (drv->remove)
  1011. drv->driver.remove = mipi_dsi_drv_remove;
  1012. if (drv->shutdown)
  1013. drv->driver.shutdown = mipi_dsi_drv_shutdown;
  1014. return driver_register(&drv->driver);
  1015. }
  1016. EXPORT_SYMBOL(mipi_dsi_driver_register_full);
  1017. /**
  1018. * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
  1019. * @drv: DSI driver structure
  1020. *
  1021. * Return: 0 on success or a negative error code on failure.
  1022. */
  1023. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
  1024. {
  1025. driver_unregister(&drv->driver);
  1026. }
  1027. EXPORT_SYMBOL(mipi_dsi_driver_unregister);
  1028. static int __init mipi_dsi_bus_init(void)
  1029. {
  1030. return bus_register(&mipi_dsi_bus_type);
  1031. }
  1032. postcore_initcall(mipi_dsi_bus_init);
  1033. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  1034. MODULE_DESCRIPTION("MIPI DSI Bus");
  1035. MODULE_LICENSE("GPL and additional rights");