drm_mipi_dsi.c 33 KB

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