drm_mipi_dsi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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_set_tear_scanline() - turn on the display module's Tearing Effect
  850. * output signal on the TE signal line when display module reaches line N
  851. * defined by STS[n:0].
  852. * @dsi: DSI peripheral device
  853. * @param: STS[10:0]
  854. * Return: 0 on success or a negative error code on failure
  855. */
  856. int mipi_dsi_set_tear_scanline(struct mipi_dsi_device *dsi, u16 param)
  857. {
  858. u8 payload[3] = { MIPI_DCS_SET_TEAR_SCANLINE, param >> 8,
  859. param & 0xff };
  860. ssize_t err;
  861. err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
  862. if (err < 0)
  863. return err;
  864. return 0;
  865. }
  866. EXPORT_SYMBOL(mipi_dsi_set_tear_scanline);
  867. /**
  868. * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
  869. * data used by the interface
  870. * @dsi: DSI peripheral device
  871. * @format: pixel format
  872. *
  873. * Return: 0 on success or a negative error code on failure.
  874. */
  875. int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
  876. {
  877. ssize_t err;
  878. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
  879. sizeof(format));
  880. if (err < 0)
  881. return err;
  882. return 0;
  883. }
  884. EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
  885. static int mipi_dsi_drv_probe(struct device *dev)
  886. {
  887. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  888. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  889. return drv->probe(dsi);
  890. }
  891. static int mipi_dsi_drv_remove(struct device *dev)
  892. {
  893. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  894. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  895. return drv->remove(dsi);
  896. }
  897. static void mipi_dsi_drv_shutdown(struct device *dev)
  898. {
  899. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  900. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  901. drv->shutdown(dsi);
  902. }
  903. /**
  904. * mipi_dsi_driver_register_full() - register a driver for DSI devices
  905. * @drv: DSI driver structure
  906. * @owner: owner module
  907. *
  908. * Return: 0 on success or a negative error code on failure.
  909. */
  910. int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
  911. struct module *owner)
  912. {
  913. drv->driver.bus = &mipi_dsi_bus_type;
  914. drv->driver.owner = owner;
  915. if (drv->probe)
  916. drv->driver.probe = mipi_dsi_drv_probe;
  917. if (drv->remove)
  918. drv->driver.remove = mipi_dsi_drv_remove;
  919. if (drv->shutdown)
  920. drv->driver.shutdown = mipi_dsi_drv_shutdown;
  921. return driver_register(&drv->driver);
  922. }
  923. EXPORT_SYMBOL(mipi_dsi_driver_register_full);
  924. /**
  925. * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
  926. * @drv: DSI driver structure
  927. *
  928. * Return: 0 on success or a negative error code on failure.
  929. */
  930. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
  931. {
  932. driver_unregister(&drv->driver);
  933. }
  934. EXPORT_SYMBOL(mipi_dsi_driver_unregister);
  935. static int __init mipi_dsi_bus_init(void)
  936. {
  937. return bus_register(&mipi_dsi_bus_type);
  938. }
  939. postcore_initcall(mipi_dsi_bus_init);
  940. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  941. MODULE_DESCRIPTION("MIPI DSI Bus");
  942. MODULE_LICENSE("GPL and additional rights");