drm_mipi_dsi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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_shutdown_peripheral() - sends a Shutdown Peripheral command
  309. * @dsi: DSI peripheral device
  310. *
  311. * Return: 0 on success or a negative error code on failure.
  312. */
  313. int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
  314. {
  315. struct mipi_dsi_msg msg = {
  316. .channel = dsi->channel,
  317. .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
  318. .tx_buf = (u8 [2]) { 0, 0 },
  319. .tx_len = 2,
  320. };
  321. return mipi_dsi_device_transfer(dsi, &msg);
  322. }
  323. EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
  324. /**
  325. * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
  326. * @dsi: DSI peripheral device
  327. *
  328. * Return: 0 on success or a negative error code on failure.
  329. */
  330. int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
  331. {
  332. struct mipi_dsi_msg msg = {
  333. .channel = dsi->channel,
  334. .type = MIPI_DSI_TURN_ON_PERIPHERAL,
  335. .tx_buf = (u8 [2]) { 0, 0 },
  336. .tx_len = 2,
  337. };
  338. return mipi_dsi_device_transfer(dsi, &msg);
  339. }
  340. EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
  341. /*
  342. * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
  343. * the payload in a long packet transmitted from the peripheral back to the
  344. * host processor
  345. * @dsi: DSI peripheral device
  346. * @value: the maximum size of the payload
  347. *
  348. * Return: 0 on success or a negative error code on failure.
  349. */
  350. int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
  351. u16 value)
  352. {
  353. u8 tx[2] = { value & 0xff, value >> 8 };
  354. struct mipi_dsi_msg msg = {
  355. .channel = dsi->channel,
  356. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  357. .tx_len = sizeof(tx),
  358. .tx_buf = tx,
  359. };
  360. return mipi_dsi_device_transfer(dsi, &msg);
  361. }
  362. EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
  363. /**
  364. * mipi_dsi_generic_write() - transmit data using a generic write packet
  365. * @dsi: DSI peripheral device
  366. * @payload: buffer containing the payload
  367. * @size: size of payload buffer
  368. *
  369. * This function will automatically choose the right data type depending on
  370. * the payload length.
  371. *
  372. * Return: The number of bytes transmitted on success or a negative error code
  373. * on failure.
  374. */
  375. ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
  376. size_t size)
  377. {
  378. struct mipi_dsi_msg msg = {
  379. .channel = dsi->channel,
  380. .tx_buf = payload,
  381. .tx_len = size
  382. };
  383. switch (size) {
  384. case 0:
  385. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
  386. break;
  387. case 1:
  388. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
  389. break;
  390. case 2:
  391. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
  392. break;
  393. default:
  394. msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
  395. break;
  396. }
  397. return mipi_dsi_device_transfer(dsi, &msg);
  398. }
  399. EXPORT_SYMBOL(mipi_dsi_generic_write);
  400. /**
  401. * mipi_dsi_generic_read() - receive data using a generic read packet
  402. * @dsi: DSI peripheral device
  403. * @params: buffer containing the request parameters
  404. * @num_params: number of request parameters
  405. * @data: buffer in which to return the received data
  406. * @size: size of receive buffer
  407. *
  408. * This function will automatically choose the right data type depending on
  409. * the number of parameters passed in.
  410. *
  411. * Return: The number of bytes successfully read or a negative error code on
  412. * failure.
  413. */
  414. ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
  415. size_t num_params, void *data, size_t size)
  416. {
  417. struct mipi_dsi_msg msg = {
  418. .channel = dsi->channel,
  419. .tx_len = num_params,
  420. .tx_buf = params,
  421. .rx_len = size,
  422. .rx_buf = data
  423. };
  424. switch (num_params) {
  425. case 0:
  426. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
  427. break;
  428. case 1:
  429. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
  430. break;
  431. case 2:
  432. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. return mipi_dsi_device_transfer(dsi, &msg);
  438. }
  439. EXPORT_SYMBOL(mipi_dsi_generic_read);
  440. /**
  441. * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
  442. * @dsi: DSI peripheral device
  443. * @data: buffer containing data to be transmitted
  444. * @len: size of transmission buffer
  445. *
  446. * This function will automatically choose the right data type depending on
  447. * the command payload length.
  448. *
  449. * Return: The number of bytes successfully transmitted or a negative error
  450. * code on failure.
  451. */
  452. ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
  453. const void *data, size_t len)
  454. {
  455. struct mipi_dsi_msg msg = {
  456. .channel = dsi->channel,
  457. .tx_buf = data,
  458. .tx_len = len
  459. };
  460. switch (len) {
  461. case 0:
  462. return -EINVAL;
  463. case 1:
  464. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  465. break;
  466. case 2:
  467. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  468. break;
  469. default:
  470. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  471. break;
  472. }
  473. return mipi_dsi_device_transfer(dsi, &msg);
  474. }
  475. EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
  476. /**
  477. * mipi_dsi_dcs_write() - send DCS write command
  478. * @dsi: DSI peripheral device
  479. * @cmd: DCS command
  480. * @data: buffer containing the command payload
  481. * @len: command payload length
  482. *
  483. * This function will automatically choose the right data type depending on
  484. * the command payload length.
  485. *
  486. * Return: The number of bytes successfully transmitted or a negative error
  487. * code on failure.
  488. */
  489. ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
  490. const void *data, size_t len)
  491. {
  492. ssize_t err;
  493. size_t size;
  494. u8 *tx;
  495. if (len > 0) {
  496. size = 1 + len;
  497. tx = kmalloc(size, GFP_KERNEL);
  498. if (!tx)
  499. return -ENOMEM;
  500. /* concatenate the DCS command byte and the payload */
  501. tx[0] = cmd;
  502. memcpy(&tx[1], data, len);
  503. } else {
  504. tx = &cmd;
  505. size = 1;
  506. }
  507. err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
  508. if (len > 0)
  509. kfree(tx);
  510. return err;
  511. }
  512. EXPORT_SYMBOL(mipi_dsi_dcs_write);
  513. /**
  514. * mipi_dsi_dcs_read() - send DCS read request command
  515. * @dsi: DSI peripheral device
  516. * @cmd: DCS command
  517. * @data: buffer in which to receive data
  518. * @len: size of receive buffer
  519. *
  520. * Return: The number of bytes read or a negative error code on failure.
  521. */
  522. ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
  523. size_t len)
  524. {
  525. struct mipi_dsi_msg msg = {
  526. .channel = dsi->channel,
  527. .type = MIPI_DSI_DCS_READ,
  528. .tx_buf = &cmd,
  529. .tx_len = 1,
  530. .rx_buf = data,
  531. .rx_len = len
  532. };
  533. return mipi_dsi_device_transfer(dsi, &msg);
  534. }
  535. EXPORT_SYMBOL(mipi_dsi_dcs_read);
  536. /**
  537. * mipi_dsi_dcs_nop() - send DCS nop packet
  538. * @dsi: DSI peripheral device
  539. *
  540. * Return: 0 on success or a negative error code on failure.
  541. */
  542. int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
  543. {
  544. ssize_t err;
  545. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
  546. if (err < 0)
  547. return err;
  548. return 0;
  549. }
  550. EXPORT_SYMBOL(mipi_dsi_dcs_nop);
  551. /**
  552. * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
  553. * @dsi: DSI peripheral device
  554. *
  555. * Return: 0 on success or a negative error code on failure.
  556. */
  557. int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
  558. {
  559. ssize_t err;
  560. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
  561. if (err < 0)
  562. return err;
  563. return 0;
  564. }
  565. EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
  566. /**
  567. * mipi_dsi_dcs_get_power_mode() - query the display module's current power
  568. * mode
  569. * @dsi: DSI peripheral device
  570. * @mode: return location for the current power mode
  571. *
  572. * Return: 0 on success or a negative error code on failure.
  573. */
  574. int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
  575. {
  576. ssize_t err;
  577. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
  578. sizeof(*mode));
  579. if (err <= 0) {
  580. if (err == 0)
  581. err = -ENODATA;
  582. return err;
  583. }
  584. return 0;
  585. }
  586. EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
  587. /**
  588. * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
  589. * data used by the interface
  590. * @dsi: DSI peripheral device
  591. * @format: return location for the pixel format
  592. *
  593. * Return: 0 on success or a negative error code on failure.
  594. */
  595. int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
  596. {
  597. ssize_t err;
  598. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
  599. sizeof(*format));
  600. if (err <= 0) {
  601. if (err == 0)
  602. err = -ENODATA;
  603. return err;
  604. }
  605. return 0;
  606. }
  607. EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
  608. /**
  609. * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
  610. * display module except interface communication
  611. * @dsi: DSI peripheral device
  612. *
  613. * Return: 0 on success or a negative error code on failure.
  614. */
  615. int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
  616. {
  617. ssize_t err;
  618. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
  619. if (err < 0)
  620. return err;
  621. return 0;
  622. }
  623. EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
  624. /**
  625. * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
  626. * module
  627. * @dsi: DSI peripheral device
  628. *
  629. * Return: 0 on success or a negative error code on failure.
  630. */
  631. int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
  632. {
  633. ssize_t err;
  634. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
  635. if (err < 0)
  636. return err;
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
  640. /**
  641. * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
  642. * display device
  643. * @dsi: DSI peripheral device
  644. *
  645. * Return: 0 on success or a negative error code on failure.
  646. */
  647. int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
  648. {
  649. ssize_t err;
  650. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
  651. if (err < 0)
  652. return err;
  653. return 0;
  654. }
  655. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
  656. /**
  657. * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
  658. * display device
  659. * @dsi: DSI peripheral device
  660. *
  661. * Return: 0 on success or a negative error code on failure
  662. */
  663. int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
  664. {
  665. ssize_t err;
  666. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
  667. if (err < 0)
  668. return err;
  669. return 0;
  670. }
  671. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
  672. /**
  673. * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
  674. * memory accessed by the host processor
  675. * @dsi: DSI peripheral device
  676. * @start: first column of frame memory
  677. * @end: last column of frame memory
  678. *
  679. * Return: 0 on success or a negative error code on failure.
  680. */
  681. int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
  682. u16 end)
  683. {
  684. u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
  685. ssize_t err;
  686. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
  687. sizeof(payload));
  688. if (err < 0)
  689. return err;
  690. return 0;
  691. }
  692. EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
  693. /**
  694. * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
  695. * memory accessed by the host processor
  696. * @dsi: DSI peripheral device
  697. * @start: first page of frame memory
  698. * @end: last page of frame memory
  699. *
  700. * Return: 0 on success or a negative error code on failure.
  701. */
  702. int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
  703. u16 end)
  704. {
  705. u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
  706. ssize_t err;
  707. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
  708. sizeof(payload));
  709. if (err < 0)
  710. return err;
  711. return 0;
  712. }
  713. EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
  714. /**
  715. * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
  716. * output signal on the TE signal line
  717. * @dsi: DSI peripheral device
  718. *
  719. * Return: 0 on success or a negative error code on failure
  720. */
  721. int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
  722. {
  723. ssize_t err;
  724. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
  725. if (err < 0)
  726. return err;
  727. return 0;
  728. }
  729. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
  730. /**
  731. * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
  732. * output signal on the TE signal line.
  733. * @dsi: DSI peripheral device
  734. * @mode: the Tearing Effect Output Line mode
  735. *
  736. * Return: 0 on success or a negative error code on failure
  737. */
  738. int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
  739. enum mipi_dsi_dcs_tear_mode mode)
  740. {
  741. u8 value = mode;
  742. ssize_t err;
  743. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
  744. sizeof(value));
  745. if (err < 0)
  746. return err;
  747. return 0;
  748. }
  749. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
  750. /**
  751. * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
  752. * data used by the interface
  753. * @dsi: DSI peripheral device
  754. * @format: pixel format
  755. *
  756. * Return: 0 on success or a negative error code on failure.
  757. */
  758. int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
  759. {
  760. ssize_t err;
  761. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
  762. sizeof(format));
  763. if (err < 0)
  764. return err;
  765. return 0;
  766. }
  767. EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
  768. static int mipi_dsi_drv_probe(struct device *dev)
  769. {
  770. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  771. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  772. return drv->probe(dsi);
  773. }
  774. static int mipi_dsi_drv_remove(struct device *dev)
  775. {
  776. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  777. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  778. return drv->remove(dsi);
  779. }
  780. static void mipi_dsi_drv_shutdown(struct device *dev)
  781. {
  782. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  783. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  784. drv->shutdown(dsi);
  785. }
  786. /**
  787. * mipi_dsi_driver_register_full() - register a driver for DSI devices
  788. * @drv: DSI driver structure
  789. * @owner: owner module
  790. *
  791. * Return: 0 on success or a negative error code on failure.
  792. */
  793. int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
  794. struct module *owner)
  795. {
  796. drv->driver.bus = &mipi_dsi_bus_type;
  797. drv->driver.owner = owner;
  798. if (drv->probe)
  799. drv->driver.probe = mipi_dsi_drv_probe;
  800. if (drv->remove)
  801. drv->driver.remove = mipi_dsi_drv_remove;
  802. if (drv->shutdown)
  803. drv->driver.shutdown = mipi_dsi_drv_shutdown;
  804. return driver_register(&drv->driver);
  805. }
  806. EXPORT_SYMBOL(mipi_dsi_driver_register_full);
  807. /**
  808. * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
  809. * @drv: DSI driver structure
  810. *
  811. * Return: 0 on success or a negative error code on failure.
  812. */
  813. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
  814. {
  815. driver_unregister(&drv->driver);
  816. }
  817. EXPORT_SYMBOL(mipi_dsi_driver_unregister);
  818. static int __init mipi_dsi_bus_init(void)
  819. {
  820. return bus_register(&mipi_dsi_bus_type);
  821. }
  822. postcore_initcall(mipi_dsi_bus_init);
  823. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  824. MODULE_DESCRIPTION("MIPI DSI Bus");
  825. MODULE_LICENSE("GPL and additional rights");