drm_mipi_dsi.c 23 KB

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