drm_mipi_dsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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_dcs_set_pixel_format() - sets the pixel format for the RGB image
  838. * data used by the interface
  839. * @dsi: DSI peripheral device
  840. * @format: pixel format
  841. *
  842. * Return: 0 on success or a negative error code on failure.
  843. */
  844. int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
  845. {
  846. ssize_t err;
  847. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
  848. sizeof(format));
  849. if (err < 0)
  850. return err;
  851. return 0;
  852. }
  853. EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
  854. static int mipi_dsi_drv_probe(struct device *dev)
  855. {
  856. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  857. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  858. return drv->probe(dsi);
  859. }
  860. static int mipi_dsi_drv_remove(struct device *dev)
  861. {
  862. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  863. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  864. return drv->remove(dsi);
  865. }
  866. static void mipi_dsi_drv_shutdown(struct device *dev)
  867. {
  868. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  869. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  870. drv->shutdown(dsi);
  871. }
  872. /**
  873. * mipi_dsi_driver_register_full() - register a driver for DSI devices
  874. * @drv: DSI driver structure
  875. * @owner: owner module
  876. *
  877. * Return: 0 on success or a negative error code on failure.
  878. */
  879. int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
  880. struct module *owner)
  881. {
  882. drv->driver.bus = &mipi_dsi_bus_type;
  883. drv->driver.owner = owner;
  884. if (drv->probe)
  885. drv->driver.probe = mipi_dsi_drv_probe;
  886. if (drv->remove)
  887. drv->driver.remove = mipi_dsi_drv_remove;
  888. if (drv->shutdown)
  889. drv->driver.shutdown = mipi_dsi_drv_shutdown;
  890. return driver_register(&drv->driver);
  891. }
  892. EXPORT_SYMBOL(mipi_dsi_driver_register_full);
  893. /**
  894. * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
  895. * @drv: DSI driver structure
  896. *
  897. * Return: 0 on success or a negative error code on failure.
  898. */
  899. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
  900. {
  901. driver_unregister(&drv->driver);
  902. }
  903. EXPORT_SYMBOL(mipi_dsi_driver_unregister);
  904. static int __init mipi_dsi_bus_init(void)
  905. {
  906. return bus_register(&mipi_dsi_bus_type);
  907. }
  908. postcore_initcall(mipi_dsi_bus_init);
  909. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  910. MODULE_DESCRIPTION("MIPI DSI Bus");
  911. MODULE_LICENSE("GPL and additional rights");