drm_mipi_dsi.c 27 KB

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