drm_mipi_dsi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. static int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
  35. {
  36. return of_driver_match_device(dev, drv);
  37. }
  38. static const struct dev_pm_ops mipi_dsi_device_pm_ops = {
  39. .runtime_suspend = pm_generic_runtime_suspend,
  40. .runtime_resume = pm_generic_runtime_resume,
  41. .suspend = pm_generic_suspend,
  42. .resume = pm_generic_resume,
  43. .freeze = pm_generic_freeze,
  44. .thaw = pm_generic_thaw,
  45. .poweroff = pm_generic_poweroff,
  46. .restore = pm_generic_restore,
  47. };
  48. static struct bus_type mipi_dsi_bus_type = {
  49. .name = "mipi-dsi",
  50. .match = mipi_dsi_device_match,
  51. .pm = &mipi_dsi_device_pm_ops,
  52. };
  53. static void mipi_dsi_dev_release(struct device *dev)
  54. {
  55. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  56. of_node_put(dev->of_node);
  57. kfree(dsi);
  58. }
  59. static const struct device_type mipi_dsi_device_type = {
  60. .release = mipi_dsi_dev_release,
  61. };
  62. static struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
  63. {
  64. struct mipi_dsi_device *dsi;
  65. dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
  66. if (!dsi)
  67. return ERR_PTR(-ENOMEM);
  68. dsi->host = host;
  69. dsi->dev.bus = &mipi_dsi_bus_type;
  70. dsi->dev.parent = host->dev;
  71. dsi->dev.type = &mipi_dsi_device_type;
  72. device_initialize(&dsi->dev);
  73. return dsi;
  74. }
  75. static int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
  76. {
  77. struct mipi_dsi_host *host = dsi->host;
  78. dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev), dsi->channel);
  79. return device_add(&dsi->dev);
  80. }
  81. static struct mipi_dsi_device *
  82. of_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
  83. {
  84. struct mipi_dsi_device *dsi;
  85. struct device *dev = host->dev;
  86. int ret;
  87. u32 reg;
  88. ret = of_property_read_u32(node, "reg", &reg);
  89. if (ret) {
  90. dev_err(dev, "device node %s has no valid reg property: %d\n",
  91. node->full_name, ret);
  92. return ERR_PTR(-EINVAL);
  93. }
  94. if (reg > 3) {
  95. dev_err(dev, "device node %s has invalid reg property: %u\n",
  96. node->full_name, reg);
  97. return ERR_PTR(-EINVAL);
  98. }
  99. dsi = mipi_dsi_device_alloc(host);
  100. if (IS_ERR(dsi)) {
  101. dev_err(dev, "failed to allocate DSI device %s: %ld\n",
  102. node->full_name, PTR_ERR(dsi));
  103. return dsi;
  104. }
  105. dsi->dev.of_node = of_node_get(node);
  106. dsi->channel = reg;
  107. ret = mipi_dsi_device_add(dsi);
  108. if (ret) {
  109. dev_err(dev, "failed to add DSI device %s: %d\n",
  110. node->full_name, ret);
  111. kfree(dsi);
  112. return ERR_PTR(ret);
  113. }
  114. return dsi;
  115. }
  116. int mipi_dsi_host_register(struct mipi_dsi_host *host)
  117. {
  118. struct device_node *node;
  119. for_each_available_child_of_node(host->dev->of_node, node) {
  120. /* skip nodes without reg property */
  121. if (!of_find_property(node, "reg", NULL))
  122. continue;
  123. of_mipi_dsi_device_add(host, node);
  124. }
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(mipi_dsi_host_register);
  128. static int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
  129. {
  130. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  131. device_unregister(&dsi->dev);
  132. return 0;
  133. }
  134. void mipi_dsi_host_unregister(struct mipi_dsi_host *host)
  135. {
  136. device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
  137. }
  138. EXPORT_SYMBOL(mipi_dsi_host_unregister);
  139. /**
  140. * mipi_dsi_attach - attach a DSI device to its DSI host
  141. * @dsi: DSI peripheral
  142. */
  143. int mipi_dsi_attach(struct mipi_dsi_device *dsi)
  144. {
  145. const struct mipi_dsi_host_ops *ops = dsi->host->ops;
  146. if (!ops || !ops->attach)
  147. return -ENOSYS;
  148. return ops->attach(dsi->host, dsi);
  149. }
  150. EXPORT_SYMBOL(mipi_dsi_attach);
  151. /**
  152. * mipi_dsi_detach - detach a DSI device from its DSI host
  153. * @dsi: DSI peripheral
  154. */
  155. int mipi_dsi_detach(struct mipi_dsi_device *dsi)
  156. {
  157. const struct mipi_dsi_host_ops *ops = dsi->host->ops;
  158. if (!ops || !ops->detach)
  159. return -ENOSYS;
  160. return ops->detach(dsi->host, dsi);
  161. }
  162. EXPORT_SYMBOL(mipi_dsi_detach);
  163. /**
  164. * mipi_dsi_dcs_write - send DCS write command
  165. * @dsi: DSI device
  166. * @channel: virtual channel
  167. * @data: pointer to the command followed by parameters
  168. * @len: length of @data
  169. */
  170. int mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, unsigned int channel,
  171. const void *data, size_t len)
  172. {
  173. const struct mipi_dsi_host_ops *ops = dsi->host->ops;
  174. struct mipi_dsi_msg msg = {
  175. .channel = channel,
  176. .tx_buf = data,
  177. .tx_len = len
  178. };
  179. if (!ops || !ops->transfer)
  180. return -ENOSYS;
  181. switch (len) {
  182. case 0:
  183. return -EINVAL;
  184. case 1:
  185. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  186. break;
  187. case 2:
  188. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  189. break;
  190. default:
  191. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  192. break;
  193. }
  194. return ops->transfer(dsi->host, &msg);
  195. }
  196. EXPORT_SYMBOL(mipi_dsi_dcs_write);
  197. /**
  198. * mipi_dsi_dcs_read - send DCS read request command
  199. * @dsi: DSI device
  200. * @channel: virtual channel
  201. * @cmd: DCS read command
  202. * @data: pointer to read buffer
  203. * @len: length of @data
  204. *
  205. * Function returns number of read bytes or error code.
  206. */
  207. ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, unsigned int channel,
  208. u8 cmd, void *data, size_t len)
  209. {
  210. const struct mipi_dsi_host_ops *ops = dsi->host->ops;
  211. struct mipi_dsi_msg msg = {
  212. .channel = channel,
  213. .type = MIPI_DSI_DCS_READ,
  214. .tx_buf = &cmd,
  215. .tx_len = 1,
  216. .rx_buf = data,
  217. .rx_len = len
  218. };
  219. if (!ops || !ops->transfer)
  220. return -ENOSYS;
  221. return ops->transfer(dsi->host, &msg);
  222. }
  223. EXPORT_SYMBOL(mipi_dsi_dcs_read);
  224. static int mipi_dsi_drv_probe(struct device *dev)
  225. {
  226. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  227. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  228. return drv->probe(dsi);
  229. }
  230. static int mipi_dsi_drv_remove(struct device *dev)
  231. {
  232. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  233. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  234. return drv->remove(dsi);
  235. }
  236. static void mipi_dsi_drv_shutdown(struct device *dev)
  237. {
  238. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  239. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  240. drv->shutdown(dsi);
  241. }
  242. /**
  243. * mipi_dsi_driver_register - register a driver for DSI devices
  244. * @drv: DSI driver structure
  245. */
  246. int mipi_dsi_driver_register(struct mipi_dsi_driver *drv)
  247. {
  248. drv->driver.bus = &mipi_dsi_bus_type;
  249. if (drv->probe)
  250. drv->driver.probe = mipi_dsi_drv_probe;
  251. if (drv->remove)
  252. drv->driver.remove = mipi_dsi_drv_remove;
  253. if (drv->shutdown)
  254. drv->driver.shutdown = mipi_dsi_drv_shutdown;
  255. return driver_register(&drv->driver);
  256. }
  257. EXPORT_SYMBOL(mipi_dsi_driver_register);
  258. /**
  259. * mipi_dsi_driver_unregister - unregister a driver for DSI devices
  260. * @drv: DSI driver structure
  261. */
  262. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
  263. {
  264. driver_unregister(&drv->driver);
  265. }
  266. EXPORT_SYMBOL(mipi_dsi_driver_unregister);
  267. static int __init mipi_dsi_bus_init(void)
  268. {
  269. return bus_register(&mipi_dsi_bus_type);
  270. }
  271. postcore_initcall(mipi_dsi_bus_init);
  272. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  273. MODULE_DESCRIPTION("MIPI DSI Bus");
  274. MODULE_LICENSE("GPL and additional rights");