drm_mipi_dsi.c 28 KB

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