drm_mipi_dsi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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 %pOF\n", node);
  138. return ERR_PTR(-EINVAL);
  139. }
  140. ret = of_property_read_u32(node, "reg", &reg);
  141. if (ret) {
  142. dev_err(dev, "device node %pOF has no valid reg property: %d\n",
  143. node, 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_DCS_COMPRESSION_MODE:
  328. case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
  329. return true;
  330. }
  331. return false;
  332. }
  333. EXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
  334. /**
  335. * mipi_dsi_packet_format_is_long - check if a packet is of the long format
  336. * @type: MIPI DSI data type of the packet
  337. *
  338. * Return: true if the packet for the given data type is a long packet, false
  339. * otherwise.
  340. */
  341. bool mipi_dsi_packet_format_is_long(u8 type)
  342. {
  343. switch (type) {
  344. case MIPI_DSI_PPS_LONG_WRITE:
  345. case MIPI_DSI_NULL_PACKET:
  346. case MIPI_DSI_BLANKING_PACKET:
  347. case MIPI_DSI_GENERIC_LONG_WRITE:
  348. case MIPI_DSI_DCS_LONG_WRITE:
  349. case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
  350. case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
  351. case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
  352. case MIPI_DSI_PACKED_PIXEL_STREAM_30:
  353. case MIPI_DSI_PACKED_PIXEL_STREAM_36:
  354. case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
  355. case MIPI_DSI_PACKED_PIXEL_STREAM_16:
  356. case MIPI_DSI_PACKED_PIXEL_STREAM_18:
  357. case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
  358. case MIPI_DSI_PACKED_PIXEL_STREAM_24:
  359. return true;
  360. }
  361. return false;
  362. }
  363. EXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
  364. /**
  365. * mipi_dsi_create_packet - create a packet from a message according to the
  366. * DSI protocol
  367. * @packet: pointer to a DSI packet structure
  368. * @msg: message to translate into a packet
  369. *
  370. * Return: 0 on success or a negative error code on failure.
  371. */
  372. int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
  373. const struct mipi_dsi_msg *msg)
  374. {
  375. if (!packet || !msg)
  376. return -EINVAL;
  377. /* do some minimum sanity checking */
  378. if (!mipi_dsi_packet_format_is_short(msg->type) &&
  379. !mipi_dsi_packet_format_is_long(msg->type))
  380. return -EINVAL;
  381. if (msg->channel > 3)
  382. return -EINVAL;
  383. memset(packet, 0, sizeof(*packet));
  384. packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
  385. /* TODO: compute ECC if hardware support is not available */
  386. /*
  387. * Long write packets contain the word count in header bytes 1 and 2.
  388. * The payload follows the header and is word count bytes long.
  389. *
  390. * Short write packets encode up to two parameters in header bytes 1
  391. * and 2.
  392. */
  393. if (mipi_dsi_packet_format_is_long(msg->type)) {
  394. packet->header[1] = (msg->tx_len >> 0) & 0xff;
  395. packet->header[2] = (msg->tx_len >> 8) & 0xff;
  396. packet->payload_length = msg->tx_len;
  397. packet->payload = msg->tx_buf;
  398. } else {
  399. const u8 *tx = msg->tx_buf;
  400. packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
  401. packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
  402. }
  403. packet->size = sizeof(packet->header) + packet->payload_length;
  404. return 0;
  405. }
  406. EXPORT_SYMBOL(mipi_dsi_create_packet);
  407. /**
  408. * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
  409. * @dsi: DSI peripheral device
  410. *
  411. * Return: 0 on success or a negative error code on failure.
  412. */
  413. int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
  414. {
  415. struct mipi_dsi_msg msg = {
  416. .channel = dsi->channel,
  417. .type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
  418. .tx_buf = (u8 [2]) { 0, 0 },
  419. .tx_len = 2,
  420. };
  421. int ret = mipi_dsi_device_transfer(dsi, &msg);
  422. return (ret < 0) ? ret : 0;
  423. }
  424. EXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
  425. /**
  426. * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
  427. * @dsi: DSI peripheral device
  428. *
  429. * Return: 0 on success or a negative error code on failure.
  430. */
  431. int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
  432. {
  433. struct mipi_dsi_msg msg = {
  434. .channel = dsi->channel,
  435. .type = MIPI_DSI_TURN_ON_PERIPHERAL,
  436. .tx_buf = (u8 [2]) { 0, 0 },
  437. .tx_len = 2,
  438. };
  439. int ret = mipi_dsi_device_transfer(dsi, &msg);
  440. return (ret < 0) ? ret : 0;
  441. }
  442. EXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
  443. /*
  444. * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
  445. * the payload in a long packet transmitted from the peripheral back to the
  446. * host processor
  447. * @dsi: DSI peripheral device
  448. * @value: the maximum size of the payload
  449. *
  450. * Return: 0 on success or a negative error code on failure.
  451. */
  452. int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
  453. u16 value)
  454. {
  455. u8 tx[2] = { value & 0xff, value >> 8 };
  456. struct mipi_dsi_msg msg = {
  457. .channel = dsi->channel,
  458. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  459. .tx_len = sizeof(tx),
  460. .tx_buf = tx,
  461. };
  462. int ret = mipi_dsi_device_transfer(dsi, &msg);
  463. return (ret < 0) ? ret : 0;
  464. }
  465. EXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
  466. /**
  467. * mipi_dsi_generic_write() - transmit data using a generic write packet
  468. * @dsi: DSI peripheral device
  469. * @payload: buffer containing the payload
  470. * @size: size of payload buffer
  471. *
  472. * This function will automatically choose the right data type depending on
  473. * the payload length.
  474. *
  475. * Return: The number of bytes transmitted on success or a negative error code
  476. * on failure.
  477. */
  478. ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
  479. size_t size)
  480. {
  481. struct mipi_dsi_msg msg = {
  482. .channel = dsi->channel,
  483. .tx_buf = payload,
  484. .tx_len = size
  485. };
  486. switch (size) {
  487. case 0:
  488. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
  489. break;
  490. case 1:
  491. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
  492. break;
  493. case 2:
  494. msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
  495. break;
  496. default:
  497. msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
  498. break;
  499. }
  500. return mipi_dsi_device_transfer(dsi, &msg);
  501. }
  502. EXPORT_SYMBOL(mipi_dsi_generic_write);
  503. /**
  504. * mipi_dsi_generic_read() - receive data using a generic read packet
  505. * @dsi: DSI peripheral device
  506. * @params: buffer containing the request parameters
  507. * @num_params: number of request parameters
  508. * @data: buffer in which to return the received data
  509. * @size: size of receive buffer
  510. *
  511. * This function will automatically choose the right data type depending on
  512. * the number of parameters passed in.
  513. *
  514. * Return: The number of bytes successfully read or a negative error code on
  515. * failure.
  516. */
  517. ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
  518. size_t num_params, void *data, size_t size)
  519. {
  520. struct mipi_dsi_msg msg = {
  521. .channel = dsi->channel,
  522. .tx_len = num_params,
  523. .tx_buf = params,
  524. .rx_len = size,
  525. .rx_buf = data
  526. };
  527. switch (num_params) {
  528. case 0:
  529. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
  530. break;
  531. case 1:
  532. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
  533. break;
  534. case 2:
  535. msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
  536. break;
  537. default:
  538. return -EINVAL;
  539. }
  540. return mipi_dsi_device_transfer(dsi, &msg);
  541. }
  542. EXPORT_SYMBOL(mipi_dsi_generic_read);
  543. /**
  544. * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
  545. * @dsi: DSI peripheral device
  546. * @data: buffer containing data to be transmitted
  547. * @len: size of transmission buffer
  548. *
  549. * This function will automatically choose the right data type depending on
  550. * the command payload length.
  551. *
  552. * Return: The number of bytes successfully transmitted or a negative error
  553. * code on failure.
  554. */
  555. ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
  556. const void *data, size_t len)
  557. {
  558. struct mipi_dsi_msg msg = {
  559. .channel = dsi->channel,
  560. .tx_buf = data,
  561. .tx_len = len
  562. };
  563. switch (len) {
  564. case 0:
  565. return -EINVAL;
  566. case 1:
  567. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  568. break;
  569. case 2:
  570. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  571. break;
  572. default:
  573. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  574. break;
  575. }
  576. return mipi_dsi_device_transfer(dsi, &msg);
  577. }
  578. EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
  579. /**
  580. * mipi_dsi_dcs_write() - send DCS write command
  581. * @dsi: DSI peripheral device
  582. * @cmd: DCS command
  583. * @data: buffer containing the command payload
  584. * @len: command payload length
  585. *
  586. * This function will automatically choose the right data type depending on
  587. * the command payload length.
  588. *
  589. * Return: The number of bytes successfully transmitted or a negative error
  590. * code on failure.
  591. */
  592. ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
  593. const void *data, size_t len)
  594. {
  595. ssize_t err;
  596. size_t size;
  597. u8 *tx;
  598. if (len > 0) {
  599. size = 1 + len;
  600. tx = kmalloc(size, GFP_KERNEL);
  601. if (!tx)
  602. return -ENOMEM;
  603. /* concatenate the DCS command byte and the payload */
  604. tx[0] = cmd;
  605. memcpy(&tx[1], data, len);
  606. } else {
  607. tx = &cmd;
  608. size = 1;
  609. }
  610. err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
  611. if (len > 0)
  612. kfree(tx);
  613. return err;
  614. }
  615. EXPORT_SYMBOL(mipi_dsi_dcs_write);
  616. /**
  617. * mipi_dsi_dcs_read() - send DCS read request command
  618. * @dsi: DSI peripheral device
  619. * @cmd: DCS command
  620. * @data: buffer in which to receive data
  621. * @len: size of receive buffer
  622. *
  623. * Return: The number of bytes read or a negative error code on failure.
  624. */
  625. ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
  626. size_t len)
  627. {
  628. struct mipi_dsi_msg msg = {
  629. .channel = dsi->channel,
  630. .type = MIPI_DSI_DCS_READ,
  631. .tx_buf = &cmd,
  632. .tx_len = 1,
  633. .rx_buf = data,
  634. .rx_len = len
  635. };
  636. return mipi_dsi_device_transfer(dsi, &msg);
  637. }
  638. EXPORT_SYMBOL(mipi_dsi_dcs_read);
  639. /**
  640. * mipi_dsi_dcs_nop() - send DCS nop packet
  641. * @dsi: DSI peripheral device
  642. *
  643. * Return: 0 on success or a negative error code on failure.
  644. */
  645. int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
  646. {
  647. ssize_t err;
  648. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
  649. if (err < 0)
  650. return err;
  651. return 0;
  652. }
  653. EXPORT_SYMBOL(mipi_dsi_dcs_nop);
  654. /**
  655. * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
  656. * @dsi: DSI peripheral device
  657. *
  658. * Return: 0 on success or a negative error code on failure.
  659. */
  660. int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
  661. {
  662. ssize_t err;
  663. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
  664. if (err < 0)
  665. return err;
  666. return 0;
  667. }
  668. EXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
  669. /**
  670. * mipi_dsi_dcs_get_power_mode() - query the display module's current power
  671. * mode
  672. * @dsi: DSI peripheral device
  673. * @mode: return location for the current power mode
  674. *
  675. * Return: 0 on success or a negative error code on failure.
  676. */
  677. int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
  678. {
  679. ssize_t err;
  680. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
  681. sizeof(*mode));
  682. if (err <= 0) {
  683. if (err == 0)
  684. err = -ENODATA;
  685. return err;
  686. }
  687. return 0;
  688. }
  689. EXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
  690. /**
  691. * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
  692. * data used by the interface
  693. * @dsi: DSI peripheral device
  694. * @format: return location for the pixel format
  695. *
  696. * Return: 0 on success or a negative error code on failure.
  697. */
  698. int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
  699. {
  700. ssize_t err;
  701. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
  702. sizeof(*format));
  703. if (err <= 0) {
  704. if (err == 0)
  705. err = -ENODATA;
  706. return err;
  707. }
  708. return 0;
  709. }
  710. EXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
  711. /**
  712. * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
  713. * display module except interface communication
  714. * @dsi: DSI peripheral device
  715. *
  716. * Return: 0 on success or a negative error code on failure.
  717. */
  718. int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
  719. {
  720. ssize_t err;
  721. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
  722. if (err < 0)
  723. return err;
  724. return 0;
  725. }
  726. EXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
  727. /**
  728. * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
  729. * module
  730. * @dsi: DSI peripheral device
  731. *
  732. * Return: 0 on success or a negative error code on failure.
  733. */
  734. int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
  735. {
  736. ssize_t err;
  737. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
  738. if (err < 0)
  739. return err;
  740. return 0;
  741. }
  742. EXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
  743. /**
  744. * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
  745. * display device
  746. * @dsi: DSI peripheral device
  747. *
  748. * Return: 0 on success or a negative error code on failure.
  749. */
  750. int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
  751. {
  752. ssize_t err;
  753. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
  754. if (err < 0)
  755. return err;
  756. return 0;
  757. }
  758. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
  759. /**
  760. * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
  761. * display device
  762. * @dsi: DSI peripheral device
  763. *
  764. * Return: 0 on success or a negative error code on failure
  765. */
  766. int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
  767. {
  768. ssize_t err;
  769. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
  770. if (err < 0)
  771. return err;
  772. return 0;
  773. }
  774. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
  775. /**
  776. * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
  777. * memory accessed by the host processor
  778. * @dsi: DSI peripheral device
  779. * @start: first column of frame memory
  780. * @end: last column of frame memory
  781. *
  782. * Return: 0 on success or a negative error code on failure.
  783. */
  784. int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
  785. u16 end)
  786. {
  787. u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
  788. ssize_t err;
  789. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
  790. sizeof(payload));
  791. if (err < 0)
  792. return err;
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
  796. /**
  797. * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
  798. * memory accessed by the host processor
  799. * @dsi: DSI peripheral device
  800. * @start: first page of frame memory
  801. * @end: last page of frame memory
  802. *
  803. * Return: 0 on success or a negative error code on failure.
  804. */
  805. int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
  806. u16 end)
  807. {
  808. u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
  809. ssize_t err;
  810. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
  811. sizeof(payload));
  812. if (err < 0)
  813. return err;
  814. return 0;
  815. }
  816. EXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
  817. /**
  818. * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
  819. * output signal on the TE signal line
  820. * @dsi: DSI peripheral device
  821. *
  822. * Return: 0 on success or a negative error code on failure
  823. */
  824. int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
  825. {
  826. ssize_t err;
  827. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
  828. if (err < 0)
  829. return err;
  830. return 0;
  831. }
  832. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
  833. /**
  834. * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
  835. * output signal on the TE signal line.
  836. * @dsi: DSI peripheral device
  837. * @mode: the Tearing Effect Output Line mode
  838. *
  839. * Return: 0 on success or a negative error code on failure
  840. */
  841. int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
  842. enum mipi_dsi_dcs_tear_mode mode)
  843. {
  844. u8 value = mode;
  845. ssize_t err;
  846. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
  847. sizeof(value));
  848. if (err < 0)
  849. return err;
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
  853. /**
  854. * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
  855. * data used by the interface
  856. * @dsi: DSI peripheral device
  857. * @format: pixel format
  858. *
  859. * Return: 0 on success or a negative error code on failure.
  860. */
  861. int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
  862. {
  863. ssize_t err;
  864. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
  865. sizeof(format));
  866. if (err < 0)
  867. return err;
  868. return 0;
  869. }
  870. EXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
  871. /**
  872. * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
  873. * the Tearing Effect output signal of the display module
  874. * @dsi: DSI peripheral device
  875. * @scanline: scanline to use as trigger
  876. *
  877. * Return: 0 on success or a negative error code on failure
  878. */
  879. int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
  880. {
  881. u8 payload[3] = { MIPI_DCS_SET_TEAR_SCANLINE, scanline >> 8,
  882. scanline & 0xff };
  883. ssize_t err;
  884. err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
  885. if (err < 0)
  886. return err;
  887. return 0;
  888. }
  889. EXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
  890. /**
  891. * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
  892. * display
  893. * @dsi: DSI peripheral device
  894. * @brightness: brightness value
  895. *
  896. * Return: 0 on success or a negative error code on failure.
  897. */
  898. int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
  899. u16 brightness)
  900. {
  901. u8 payload[2] = { brightness & 0xff, brightness >> 8 };
  902. ssize_t err;
  903. err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
  904. payload, sizeof(payload));
  905. if (err < 0)
  906. return err;
  907. return 0;
  908. }
  909. EXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
  910. /**
  911. * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
  912. * of the display
  913. * @dsi: DSI peripheral device
  914. * @brightness: brightness value
  915. *
  916. * Return: 0 on success or a negative error code on failure.
  917. */
  918. int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
  919. u16 *brightness)
  920. {
  921. ssize_t err;
  922. err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
  923. brightness, sizeof(*brightness));
  924. if (err <= 0) {
  925. if (err == 0)
  926. err = -ENODATA;
  927. return err;
  928. }
  929. return 0;
  930. }
  931. EXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
  932. static int mipi_dsi_drv_probe(struct device *dev)
  933. {
  934. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  935. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  936. return drv->probe(dsi);
  937. }
  938. static int mipi_dsi_drv_remove(struct device *dev)
  939. {
  940. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  941. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  942. return drv->remove(dsi);
  943. }
  944. static void mipi_dsi_drv_shutdown(struct device *dev)
  945. {
  946. struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
  947. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  948. drv->shutdown(dsi);
  949. }
  950. /**
  951. * mipi_dsi_driver_register_full() - register a driver for DSI devices
  952. * @drv: DSI driver structure
  953. * @owner: owner module
  954. *
  955. * Return: 0 on success or a negative error code on failure.
  956. */
  957. int mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
  958. struct module *owner)
  959. {
  960. drv->driver.bus = &mipi_dsi_bus_type;
  961. drv->driver.owner = owner;
  962. if (drv->probe)
  963. drv->driver.probe = mipi_dsi_drv_probe;
  964. if (drv->remove)
  965. drv->driver.remove = mipi_dsi_drv_remove;
  966. if (drv->shutdown)
  967. drv->driver.shutdown = mipi_dsi_drv_shutdown;
  968. return driver_register(&drv->driver);
  969. }
  970. EXPORT_SYMBOL(mipi_dsi_driver_register_full);
  971. /**
  972. * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
  973. * @drv: DSI driver structure
  974. *
  975. * Return: 0 on success or a negative error code on failure.
  976. */
  977. void mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
  978. {
  979. driver_unregister(&drv->driver);
  980. }
  981. EXPORT_SYMBOL(mipi_dsi_driver_unregister);
  982. static int __init mipi_dsi_bus_init(void)
  983. {
  984. return bus_register(&mipi_dsi_bus_type);
  985. }
  986. postcore_initcall(mipi_dsi_bus_init);
  987. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  988. MODULE_DESCRIPTION("MIPI DSI Bus");
  989. MODULE_LICENSE("GPL and additional rights");