18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * MIPI DSI Bus
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
58c2ecf20Sopenharmony_ci * Andrzej Hajda <a.hajda@samsung.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
88c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the
98c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
108c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
118c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
128c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
138c2ecf20Sopenharmony_ci * the following conditions:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
168c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
178c2ecf20Sopenharmony_ci * of the Software.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
208c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
228c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
238c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
248c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
258c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <drm/drm_mipi_dsi.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/device.h>
318c2ecf20Sopenharmony_ci#include <linux/module.h>
328c2ecf20Sopenharmony_ci#include <linux/of_device.h>
338c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
348c2ecf20Sopenharmony_ci#include <linux/slab.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <drm/drm_dsc.h>
378c2ecf20Sopenharmony_ci#include <drm/drm_print.h>
388c2ecf20Sopenharmony_ci#include <video/mipi_display.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/**
418c2ecf20Sopenharmony_ci * DOC: dsi helpers
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * These functions contain some common logic and helpers to deal with MIPI DSI
448c2ecf20Sopenharmony_ci * peripherals.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * Helpers are provided for a number of standard MIPI DSI command as well as a
478c2ecf20Sopenharmony_ci * subset of the MIPI DCS command set.
488c2ecf20Sopenharmony_ci */
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int mipi_dsi_device_match(struct device *dev, struct device_driver *drv)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* attempt OF style match */
558c2ecf20Sopenharmony_ci	if (of_driver_match_device(dev, drv))
568c2ecf20Sopenharmony_ci		return 1;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/* compare DSI device and driver names */
598c2ecf20Sopenharmony_ci	if (!strcmp(dsi->name, drv->name))
608c2ecf20Sopenharmony_ci		return 1;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	return 0;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int mipi_dsi_uevent(struct device *dev, struct kobj_uevent_env *env)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
688c2ecf20Sopenharmony_ci	int err;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	err = of_device_uevent_modalias(dev, env);
718c2ecf20Sopenharmony_ci	if (err != -ENODEV)
728c2ecf20Sopenharmony_ci		return err;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	add_uevent_var(env, "MODALIAS=%s%s", MIPI_DSI_MODULE_PREFIX,
758c2ecf20Sopenharmony_ci		       dsi->name);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic const struct dev_pm_ops mipi_dsi_device_pm_ops = {
818c2ecf20Sopenharmony_ci	.runtime_suspend = pm_generic_runtime_suspend,
828c2ecf20Sopenharmony_ci	.runtime_resume = pm_generic_runtime_resume,
838c2ecf20Sopenharmony_ci	.suspend = pm_generic_suspend,
848c2ecf20Sopenharmony_ci	.resume = pm_generic_resume,
858c2ecf20Sopenharmony_ci	.freeze = pm_generic_freeze,
868c2ecf20Sopenharmony_ci	.thaw = pm_generic_thaw,
878c2ecf20Sopenharmony_ci	.poweroff = pm_generic_poweroff,
888c2ecf20Sopenharmony_ci	.restore = pm_generic_restore,
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic struct bus_type mipi_dsi_bus_type = {
928c2ecf20Sopenharmony_ci	.name = "mipi-dsi",
938c2ecf20Sopenharmony_ci	.match = mipi_dsi_device_match,
948c2ecf20Sopenharmony_ci	.uevent = mipi_dsi_uevent,
958c2ecf20Sopenharmony_ci	.pm = &mipi_dsi_device_pm_ops,
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/**
998c2ecf20Sopenharmony_ci * of_find_mipi_dsi_device_by_node() - find the MIPI DSI device matching a
1008c2ecf20Sopenharmony_ci *    device tree node
1018c2ecf20Sopenharmony_ci * @np: device tree node
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * Return: A pointer to the MIPI DSI device corresponding to @np or NULL if no
1048c2ecf20Sopenharmony_ci *    such device exists (or has not been registered yet).
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_cistruct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct device *dev;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	dev = bus_find_device_by_of_node(&mipi_dsi_bus_type, np);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return dev ? to_mipi_dsi_device(dev) : NULL;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(of_find_mipi_dsi_device_by_node);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void mipi_dsi_dev_release(struct device *dev)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	of_node_put(dev->of_node);
1218c2ecf20Sopenharmony_ci	kfree(dsi);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic const struct device_type mipi_dsi_device_type = {
1258c2ecf20Sopenharmony_ci	.release = mipi_dsi_dev_release,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic struct mipi_dsi_device *mipi_dsi_device_alloc(struct mipi_dsi_host *host)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
1338c2ecf20Sopenharmony_ci	if (!dsi)
1348c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	dsi->host = host;
1378c2ecf20Sopenharmony_ci	dsi->dev.bus = &mipi_dsi_bus_type;
1388c2ecf20Sopenharmony_ci	dsi->dev.parent = host->dev;
1398c2ecf20Sopenharmony_ci	dsi->dev.type = &mipi_dsi_device_type;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	device_initialize(&dsi->dev);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return dsi;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int mipi_dsi_device_add(struct mipi_dsi_device *dsi)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct mipi_dsi_host *host = dsi->host;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	dev_set_name(&dsi->dev, "%s.%d", dev_name(host->dev),  dsi->channel);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return device_add(&dsi->dev);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_OF)
1568c2ecf20Sopenharmony_cistatic struct mipi_dsi_device *
1578c2ecf20Sopenharmony_ciof_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct mipi_dsi_device_info info = { };
1608c2ecf20Sopenharmony_ci	int ret;
1618c2ecf20Sopenharmony_ci	u32 reg;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1648c2ecf20Sopenharmony_ci		drm_err(host, "modalias failure on %pOF\n", node);
1658c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	ret = of_property_read_u32(node, "reg", &reg);
1698c2ecf20Sopenharmony_ci	if (ret) {
1708c2ecf20Sopenharmony_ci		drm_err(host, "device node %pOF has no valid reg property: %d\n",
1718c2ecf20Sopenharmony_ci			node, ret);
1728c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	info.channel = reg;
1768c2ecf20Sopenharmony_ci	info.node = of_node_get(node);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	return mipi_dsi_device_register_full(host, &info);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci#else
1818c2ecf20Sopenharmony_cistatic struct mipi_dsi_device *
1828c2ecf20Sopenharmony_ciof_mipi_dsi_device_add(struct mipi_dsi_host *host, struct device_node *node)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	return ERR_PTR(-ENODEV);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci#endif
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/**
1898c2ecf20Sopenharmony_ci * mipi_dsi_device_register_full - create a MIPI DSI device
1908c2ecf20Sopenharmony_ci * @host: DSI host to which this device is connected
1918c2ecf20Sopenharmony_ci * @info: pointer to template containing DSI device information
1928c2ecf20Sopenharmony_ci *
1938c2ecf20Sopenharmony_ci * Create a MIPI DSI device by using the device information provided by
1948c2ecf20Sopenharmony_ci * mipi_dsi_device_info template
1958c2ecf20Sopenharmony_ci *
1968c2ecf20Sopenharmony_ci * Returns:
1978c2ecf20Sopenharmony_ci * A pointer to the newly created MIPI DSI device, or, a pointer encoded
1988c2ecf20Sopenharmony_ci * with an error
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_cistruct mipi_dsi_device *
2018c2ecf20Sopenharmony_cimipi_dsi_device_register_full(struct mipi_dsi_host *host,
2028c2ecf20Sopenharmony_ci			      const struct mipi_dsi_device_info *info)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi;
2058c2ecf20Sopenharmony_ci	int ret;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (!info) {
2088c2ecf20Sopenharmony_ci		drm_err(host, "invalid mipi_dsi_device_info pointer\n");
2098c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	if (info->channel > 3) {
2138c2ecf20Sopenharmony_ci		drm_err(host, "invalid virtual channel: %u\n", info->channel);
2148c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	dsi = mipi_dsi_device_alloc(host);
2188c2ecf20Sopenharmony_ci	if (IS_ERR(dsi)) {
2198c2ecf20Sopenharmony_ci		drm_err(host, "failed to allocate DSI device %ld\n",
2208c2ecf20Sopenharmony_ci			PTR_ERR(dsi));
2218c2ecf20Sopenharmony_ci		return dsi;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	device_set_node(&dsi->dev, of_fwnode_handle(info->node));
2258c2ecf20Sopenharmony_ci	dsi->channel = info->channel;
2268c2ecf20Sopenharmony_ci	strlcpy(dsi->name, info->type, sizeof(dsi->name));
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	ret = mipi_dsi_device_add(dsi);
2298c2ecf20Sopenharmony_ci	if (ret) {
2308c2ecf20Sopenharmony_ci		drm_err(host, "failed to add DSI device %d\n", ret);
2318c2ecf20Sopenharmony_ci		kfree(dsi);
2328c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	return dsi;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_device_register_full);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/**
2408c2ecf20Sopenharmony_ci * mipi_dsi_device_unregister - unregister MIPI DSI device
2418c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
2428c2ecf20Sopenharmony_ci */
2438c2ecf20Sopenharmony_civoid mipi_dsi_device_unregister(struct mipi_dsi_device *dsi)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	device_unregister(&dsi->dev);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_device_unregister);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(host_lock);
2508c2ecf20Sopenharmony_cistatic LIST_HEAD(host_list);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/**
2538c2ecf20Sopenharmony_ci * of_find_mipi_dsi_host_by_node() - find the MIPI DSI host matching a
2548c2ecf20Sopenharmony_ci *				     device tree node
2558c2ecf20Sopenharmony_ci * @node: device tree node
2568c2ecf20Sopenharmony_ci *
2578c2ecf20Sopenharmony_ci * Returns:
2588c2ecf20Sopenharmony_ci * A pointer to the MIPI DSI host corresponding to @node or NULL if no
2598c2ecf20Sopenharmony_ci * such device exists (or has not been registered yet).
2608c2ecf20Sopenharmony_ci */
2618c2ecf20Sopenharmony_cistruct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct mipi_dsi_host *host;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	mutex_lock(&host_lock);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	list_for_each_entry(host, &host_list, list) {
2688c2ecf20Sopenharmony_ci		if (host->dev->of_node == node) {
2698c2ecf20Sopenharmony_ci			mutex_unlock(&host_lock);
2708c2ecf20Sopenharmony_ci			return host;
2718c2ecf20Sopenharmony_ci		}
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	mutex_unlock(&host_lock);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	return NULL;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ciEXPORT_SYMBOL(of_find_mipi_dsi_host_by_node);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ciint mipi_dsi_host_register(struct mipi_dsi_host *host)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	struct device_node *node;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	for_each_available_child_of_node(host->dev->of_node, node) {
2858c2ecf20Sopenharmony_ci		/* skip nodes without reg property */
2868c2ecf20Sopenharmony_ci		if (!of_find_property(node, "reg", NULL))
2878c2ecf20Sopenharmony_ci			continue;
2888c2ecf20Sopenharmony_ci		of_mipi_dsi_device_add(host, node);
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	mutex_lock(&host_lock);
2928c2ecf20Sopenharmony_ci	list_add_tail(&host->list, &host_list);
2938c2ecf20Sopenharmony_ci	mutex_unlock(&host_lock);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	return 0;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_host_register);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int mipi_dsi_remove_device_fn(struct device *dev, void *priv)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (dsi->attached)
3048c2ecf20Sopenharmony_ci		mipi_dsi_detach(dsi);
3058c2ecf20Sopenharmony_ci	mipi_dsi_device_unregister(dsi);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return 0;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_civoid mipi_dsi_host_unregister(struct mipi_dsi_host *host)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	device_for_each_child(host->dev, NULL, mipi_dsi_remove_device_fn);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	mutex_lock(&host_lock);
3158c2ecf20Sopenharmony_ci	list_del_init(&host->list);
3168c2ecf20Sopenharmony_ci	mutex_unlock(&host_lock);
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_host_unregister);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci/**
3218c2ecf20Sopenharmony_ci * mipi_dsi_attach - attach a DSI device to its DSI host
3228c2ecf20Sopenharmony_ci * @dsi: DSI peripheral
3238c2ecf20Sopenharmony_ci */
3248c2ecf20Sopenharmony_ciint mipi_dsi_attach(struct mipi_dsi_device *dsi)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
3278c2ecf20Sopenharmony_ci	int ret;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (!ops || !ops->attach)
3308c2ecf20Sopenharmony_ci		return -ENOSYS;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	ret = ops->attach(dsi->host, dsi);
3338c2ecf20Sopenharmony_ci	if (ret)
3348c2ecf20Sopenharmony_ci		return ret;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	dsi->attached = true;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	return 0;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_attach);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci/**
3438c2ecf20Sopenharmony_ci * mipi_dsi_detach - detach a DSI device from its DSI host
3448c2ecf20Sopenharmony_ci * @dsi: DSI peripheral
3458c2ecf20Sopenharmony_ci */
3468c2ecf20Sopenharmony_ciint mipi_dsi_detach(struct mipi_dsi_device *dsi)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (WARN_ON(!dsi->attached))
3518c2ecf20Sopenharmony_ci		return -EINVAL;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (!ops || !ops->detach)
3548c2ecf20Sopenharmony_ci		return -ENOSYS;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	dsi->attached = false;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	return ops->detach(dsi->host, dsi);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_detach);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic ssize_t mipi_dsi_device_transfer(struct mipi_dsi_device *dsi,
3638c2ecf20Sopenharmony_ci					struct mipi_dsi_msg *msg)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	const struct mipi_dsi_host_ops *ops = dsi->host->ops;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	if (!ops || !ops->transfer)
3688c2ecf20Sopenharmony_ci		return -ENOSYS;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (dsi->mode_flags & MIPI_DSI_MODE_LPM)
3718c2ecf20Sopenharmony_ci		msg->flags |= MIPI_DSI_MSG_USE_LPM;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	return ops->transfer(dsi->host, msg);
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci/**
3778c2ecf20Sopenharmony_ci * mipi_dsi_packet_format_is_short - check if a packet is of the short format
3788c2ecf20Sopenharmony_ci * @type: MIPI DSI data type of the packet
3798c2ecf20Sopenharmony_ci *
3808c2ecf20Sopenharmony_ci * Return: true if the packet for the given data type is a short packet, false
3818c2ecf20Sopenharmony_ci * otherwise.
3828c2ecf20Sopenharmony_ci */
3838c2ecf20Sopenharmony_cibool mipi_dsi_packet_format_is_short(u8 type)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	switch (type) {
3868c2ecf20Sopenharmony_ci	case MIPI_DSI_V_SYNC_START:
3878c2ecf20Sopenharmony_ci	case MIPI_DSI_V_SYNC_END:
3888c2ecf20Sopenharmony_ci	case MIPI_DSI_H_SYNC_START:
3898c2ecf20Sopenharmony_ci	case MIPI_DSI_H_SYNC_END:
3908c2ecf20Sopenharmony_ci	case MIPI_DSI_COMPRESSION_MODE:
3918c2ecf20Sopenharmony_ci	case MIPI_DSI_END_OF_TRANSMISSION:
3928c2ecf20Sopenharmony_ci	case MIPI_DSI_COLOR_MODE_OFF:
3938c2ecf20Sopenharmony_ci	case MIPI_DSI_COLOR_MODE_ON:
3948c2ecf20Sopenharmony_ci	case MIPI_DSI_SHUTDOWN_PERIPHERAL:
3958c2ecf20Sopenharmony_ci	case MIPI_DSI_TURN_ON_PERIPHERAL:
3968c2ecf20Sopenharmony_ci	case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
3978c2ecf20Sopenharmony_ci	case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
3988c2ecf20Sopenharmony_ci	case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
3998c2ecf20Sopenharmony_ci	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
4008c2ecf20Sopenharmony_ci	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
4018c2ecf20Sopenharmony_ci	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
4028c2ecf20Sopenharmony_ci	case MIPI_DSI_DCS_SHORT_WRITE:
4038c2ecf20Sopenharmony_ci	case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
4048c2ecf20Sopenharmony_ci	case MIPI_DSI_DCS_READ:
4058c2ecf20Sopenharmony_ci	case MIPI_DSI_EXECUTE_QUEUE:
4068c2ecf20Sopenharmony_ci	case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
4078c2ecf20Sopenharmony_ci		return true;
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	return false;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_packet_format_is_short);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci/**
4158c2ecf20Sopenharmony_ci * mipi_dsi_packet_format_is_long - check if a packet is of the long format
4168c2ecf20Sopenharmony_ci * @type: MIPI DSI data type of the packet
4178c2ecf20Sopenharmony_ci *
4188c2ecf20Sopenharmony_ci * Return: true if the packet for the given data type is a long packet, false
4198c2ecf20Sopenharmony_ci * otherwise.
4208c2ecf20Sopenharmony_ci */
4218c2ecf20Sopenharmony_cibool mipi_dsi_packet_format_is_long(u8 type)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	switch (type) {
4248c2ecf20Sopenharmony_ci	case MIPI_DSI_NULL_PACKET:
4258c2ecf20Sopenharmony_ci	case MIPI_DSI_BLANKING_PACKET:
4268c2ecf20Sopenharmony_ci	case MIPI_DSI_GENERIC_LONG_WRITE:
4278c2ecf20Sopenharmony_ci	case MIPI_DSI_DCS_LONG_WRITE:
4288c2ecf20Sopenharmony_ci	case MIPI_DSI_PICTURE_PARAMETER_SET:
4298c2ecf20Sopenharmony_ci	case MIPI_DSI_COMPRESSED_PIXEL_STREAM:
4308c2ecf20Sopenharmony_ci	case MIPI_DSI_LOOSELY_PACKED_PIXEL_STREAM_YCBCR20:
4318c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR24:
4328c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16:
4338c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_30:
4348c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_36:
4358c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12:
4368c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_16:
4378c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_18:
4388c2ecf20Sopenharmony_ci	case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
4398c2ecf20Sopenharmony_ci	case MIPI_DSI_PACKED_PIXEL_STREAM_24:
4408c2ecf20Sopenharmony_ci		return true;
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	return false;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_packet_format_is_long);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci/**
4488c2ecf20Sopenharmony_ci * mipi_dsi_create_packet - create a packet from a message according to the
4498c2ecf20Sopenharmony_ci *     DSI protocol
4508c2ecf20Sopenharmony_ci * @packet: pointer to a DSI packet structure
4518c2ecf20Sopenharmony_ci * @msg: message to translate into a packet
4528c2ecf20Sopenharmony_ci *
4538c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
4548c2ecf20Sopenharmony_ci */
4558c2ecf20Sopenharmony_ciint mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
4568c2ecf20Sopenharmony_ci			   const struct mipi_dsi_msg *msg)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	if (!packet || !msg)
4598c2ecf20Sopenharmony_ci		return -EINVAL;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	/* do some minimum sanity checking */
4628c2ecf20Sopenharmony_ci	if (!mipi_dsi_packet_format_is_short(msg->type) &&
4638c2ecf20Sopenharmony_ci	    !mipi_dsi_packet_format_is_long(msg->type))
4648c2ecf20Sopenharmony_ci		return -EINVAL;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (msg->channel > 3)
4678c2ecf20Sopenharmony_ci		return -EINVAL;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	memset(packet, 0, sizeof(*packet));
4708c2ecf20Sopenharmony_ci	packet->header[0] = ((msg->channel & 0x3) << 6) | (msg->type & 0x3f);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* TODO: compute ECC if hardware support is not available */
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/*
4758c2ecf20Sopenharmony_ci	 * Long write packets contain the word count in header bytes 1 and 2.
4768c2ecf20Sopenharmony_ci	 * The payload follows the header and is word count bytes long.
4778c2ecf20Sopenharmony_ci	 *
4788c2ecf20Sopenharmony_ci	 * Short write packets encode up to two parameters in header bytes 1
4798c2ecf20Sopenharmony_ci	 * and 2.
4808c2ecf20Sopenharmony_ci	 */
4818c2ecf20Sopenharmony_ci	if (mipi_dsi_packet_format_is_long(msg->type)) {
4828c2ecf20Sopenharmony_ci		packet->header[1] = (msg->tx_len >> 0) & 0xff;
4838c2ecf20Sopenharmony_ci		packet->header[2] = (msg->tx_len >> 8) & 0xff;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci		packet->payload_length = msg->tx_len;
4868c2ecf20Sopenharmony_ci		packet->payload = msg->tx_buf;
4878c2ecf20Sopenharmony_ci	} else {
4888c2ecf20Sopenharmony_ci		const u8 *tx = msg->tx_buf;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		packet->header[1] = (msg->tx_len > 0) ? tx[0] : 0;
4918c2ecf20Sopenharmony_ci		packet->header[2] = (msg->tx_len > 1) ? tx[1] : 0;
4928c2ecf20Sopenharmony_ci	}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	packet->size = sizeof(packet->header) + packet->payload_length;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	return 0;
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_create_packet);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci/**
5018c2ecf20Sopenharmony_ci * mipi_dsi_shutdown_peripheral() - sends a Shutdown Peripheral command
5028c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
5038c2ecf20Sopenharmony_ci *
5048c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
5058c2ecf20Sopenharmony_ci */
5068c2ecf20Sopenharmony_ciint mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
5098c2ecf20Sopenharmony_ci		.channel = dsi->channel,
5108c2ecf20Sopenharmony_ci		.type = MIPI_DSI_SHUTDOWN_PERIPHERAL,
5118c2ecf20Sopenharmony_ci		.tx_buf = (u8 [2]) { 0, 0 },
5128c2ecf20Sopenharmony_ci		.tx_len = 2,
5138c2ecf20Sopenharmony_ci	};
5148c2ecf20Sopenharmony_ci	int ret = mipi_dsi_device_transfer(dsi, &msg);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	return (ret < 0) ? ret : 0;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_shutdown_peripheral);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci/**
5218c2ecf20Sopenharmony_ci * mipi_dsi_turn_on_peripheral() - sends a Turn On Peripheral command
5228c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
5238c2ecf20Sopenharmony_ci *
5248c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
5258c2ecf20Sopenharmony_ci */
5268c2ecf20Sopenharmony_ciint mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
5298c2ecf20Sopenharmony_ci		.channel = dsi->channel,
5308c2ecf20Sopenharmony_ci		.type = MIPI_DSI_TURN_ON_PERIPHERAL,
5318c2ecf20Sopenharmony_ci		.tx_buf = (u8 [2]) { 0, 0 },
5328c2ecf20Sopenharmony_ci		.tx_len = 2,
5338c2ecf20Sopenharmony_ci	};
5348c2ecf20Sopenharmony_ci	int ret = mipi_dsi_device_transfer(dsi, &msg);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	return (ret < 0) ? ret : 0;
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_turn_on_peripheral);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci/*
5418c2ecf20Sopenharmony_ci * mipi_dsi_set_maximum_return_packet_size() - specify the maximum size of the
5428c2ecf20Sopenharmony_ci *    the payload in a long packet transmitted from the peripheral back to the
5438c2ecf20Sopenharmony_ci *    host processor
5448c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
5458c2ecf20Sopenharmony_ci * @value: the maximum size of the payload
5468c2ecf20Sopenharmony_ci *
5478c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
5488c2ecf20Sopenharmony_ci */
5498c2ecf20Sopenharmony_ciint mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
5508c2ecf20Sopenharmony_ci					    u16 value)
5518c2ecf20Sopenharmony_ci{
5528c2ecf20Sopenharmony_ci	u8 tx[2] = { value & 0xff, value >> 8 };
5538c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
5548c2ecf20Sopenharmony_ci		.channel = dsi->channel,
5558c2ecf20Sopenharmony_ci		.type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
5568c2ecf20Sopenharmony_ci		.tx_len = sizeof(tx),
5578c2ecf20Sopenharmony_ci		.tx_buf = tx,
5588c2ecf20Sopenharmony_ci	};
5598c2ecf20Sopenharmony_ci	int ret = mipi_dsi_device_transfer(dsi, &msg);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	return (ret < 0) ? ret : 0;
5628c2ecf20Sopenharmony_ci}
5638c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_set_maximum_return_packet_size);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci/**
5668c2ecf20Sopenharmony_ci * mipi_dsi_compression_mode() - enable/disable DSC on the peripheral
5678c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
5688c2ecf20Sopenharmony_ci * @enable: Whether to enable or disable the DSC
5698c2ecf20Sopenharmony_ci *
5708c2ecf20Sopenharmony_ci * Enable or disable Display Stream Compression on the peripheral using the
5718c2ecf20Sopenharmony_ci * default Picture Parameter Set and VESA DSC 1.1 algorithm.
5728c2ecf20Sopenharmony_ci *
5738c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
5748c2ecf20Sopenharmony_ci */
5758c2ecf20Sopenharmony_cissize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	/* Note: Needs updating for non-default PPS or algorithm */
5788c2ecf20Sopenharmony_ci	u8 tx[2] = { enable << 0, 0 };
5798c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
5808c2ecf20Sopenharmony_ci		.channel = dsi->channel,
5818c2ecf20Sopenharmony_ci		.type = MIPI_DSI_COMPRESSION_MODE,
5828c2ecf20Sopenharmony_ci		.tx_len = sizeof(tx),
5838c2ecf20Sopenharmony_ci		.tx_buf = tx,
5848c2ecf20Sopenharmony_ci	};
5858c2ecf20Sopenharmony_ci	int ret = mipi_dsi_device_transfer(dsi, &msg);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	return (ret < 0) ? ret : 0;
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_compression_mode);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci/**
5928c2ecf20Sopenharmony_ci * mipi_dsi_picture_parameter_set() - transmit the DSC PPS to the peripheral
5938c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
5948c2ecf20Sopenharmony_ci * @pps: VESA DSC 1.1 Picture Parameter Set
5958c2ecf20Sopenharmony_ci *
5968c2ecf20Sopenharmony_ci * Transmit the VESA DSC 1.1 Picture Parameter Set to the peripheral.
5978c2ecf20Sopenharmony_ci *
5988c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
5998c2ecf20Sopenharmony_ci */
6008c2ecf20Sopenharmony_cissize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
6018c2ecf20Sopenharmony_ci				       const struct drm_dsc_picture_parameter_set *pps)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
6048c2ecf20Sopenharmony_ci		.channel = dsi->channel,
6058c2ecf20Sopenharmony_ci		.type = MIPI_DSI_PICTURE_PARAMETER_SET,
6068c2ecf20Sopenharmony_ci		.tx_len = sizeof(*pps),
6078c2ecf20Sopenharmony_ci		.tx_buf = pps,
6088c2ecf20Sopenharmony_ci	};
6098c2ecf20Sopenharmony_ci	int ret = mipi_dsi_device_transfer(dsi, &msg);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	return (ret < 0) ? ret : 0;
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_picture_parameter_set);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci/**
6168c2ecf20Sopenharmony_ci * mipi_dsi_generic_write() - transmit data using a generic write packet
6178c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
6188c2ecf20Sopenharmony_ci * @payload: buffer containing the payload
6198c2ecf20Sopenharmony_ci * @size: size of payload buffer
6208c2ecf20Sopenharmony_ci *
6218c2ecf20Sopenharmony_ci * This function will automatically choose the right data type depending on
6228c2ecf20Sopenharmony_ci * the payload length.
6238c2ecf20Sopenharmony_ci *
6248c2ecf20Sopenharmony_ci * Return: The number of bytes transmitted on success or a negative error code
6258c2ecf20Sopenharmony_ci * on failure.
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_cissize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
6288c2ecf20Sopenharmony_ci			       size_t size)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
6318c2ecf20Sopenharmony_ci		.channel = dsi->channel,
6328c2ecf20Sopenharmony_ci		.tx_buf = payload,
6338c2ecf20Sopenharmony_ci		.tx_len = size
6348c2ecf20Sopenharmony_ci	};
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	switch (size) {
6378c2ecf20Sopenharmony_ci	case 0:
6388c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM;
6398c2ecf20Sopenharmony_ci		break;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	case 1:
6428c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM;
6438c2ecf20Sopenharmony_ci		break;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	case 2:
6468c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM;
6478c2ecf20Sopenharmony_ci		break;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	default:
6508c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_GENERIC_LONG_WRITE;
6518c2ecf20Sopenharmony_ci		break;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	return mipi_dsi_device_transfer(dsi, &msg);
6558c2ecf20Sopenharmony_ci}
6568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_generic_write);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci/**
6598c2ecf20Sopenharmony_ci * mipi_dsi_generic_read() - receive data using a generic read packet
6608c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
6618c2ecf20Sopenharmony_ci * @params: buffer containing the request parameters
6628c2ecf20Sopenharmony_ci * @num_params: number of request parameters
6638c2ecf20Sopenharmony_ci * @data: buffer in which to return the received data
6648c2ecf20Sopenharmony_ci * @size: size of receive buffer
6658c2ecf20Sopenharmony_ci *
6668c2ecf20Sopenharmony_ci * This function will automatically choose the right data type depending on
6678c2ecf20Sopenharmony_ci * the number of parameters passed in.
6688c2ecf20Sopenharmony_ci *
6698c2ecf20Sopenharmony_ci * Return: The number of bytes successfully read or a negative error code on
6708c2ecf20Sopenharmony_ci * failure.
6718c2ecf20Sopenharmony_ci */
6728c2ecf20Sopenharmony_cissize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
6738c2ecf20Sopenharmony_ci			      size_t num_params, void *data, size_t size)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
6768c2ecf20Sopenharmony_ci		.channel = dsi->channel,
6778c2ecf20Sopenharmony_ci		.tx_len = num_params,
6788c2ecf20Sopenharmony_ci		.tx_buf = params,
6798c2ecf20Sopenharmony_ci		.rx_len = size,
6808c2ecf20Sopenharmony_ci		.rx_buf = data
6818c2ecf20Sopenharmony_ci	};
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	switch (num_params) {
6848c2ecf20Sopenharmony_ci	case 0:
6858c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
6868c2ecf20Sopenharmony_ci		break;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	case 1:
6898c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
6908c2ecf20Sopenharmony_ci		break;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	case 2:
6938c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
6948c2ecf20Sopenharmony_ci		break;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	default:
6978c2ecf20Sopenharmony_ci		return -EINVAL;
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	return mipi_dsi_device_transfer(dsi, &msg);
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_generic_read);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci/**
7058c2ecf20Sopenharmony_ci * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
7068c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
7078c2ecf20Sopenharmony_ci * @data: buffer containing data to be transmitted
7088c2ecf20Sopenharmony_ci * @len: size of transmission buffer
7098c2ecf20Sopenharmony_ci *
7108c2ecf20Sopenharmony_ci * This function will automatically choose the right data type depending on
7118c2ecf20Sopenharmony_ci * the command payload length.
7128c2ecf20Sopenharmony_ci *
7138c2ecf20Sopenharmony_ci * Return: The number of bytes successfully transmitted or a negative error
7148c2ecf20Sopenharmony_ci * code on failure.
7158c2ecf20Sopenharmony_ci */
7168c2ecf20Sopenharmony_cissize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
7178c2ecf20Sopenharmony_ci				  const void *data, size_t len)
7188c2ecf20Sopenharmony_ci{
7198c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
7208c2ecf20Sopenharmony_ci		.channel = dsi->channel,
7218c2ecf20Sopenharmony_ci		.tx_buf = data,
7228c2ecf20Sopenharmony_ci		.tx_len = len
7238c2ecf20Sopenharmony_ci	};
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	switch (len) {
7268c2ecf20Sopenharmony_ci	case 0:
7278c2ecf20Sopenharmony_ci		return -EINVAL;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	case 1:
7308c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_DCS_SHORT_WRITE;
7318c2ecf20Sopenharmony_ci		break;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	case 2:
7348c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
7358c2ecf20Sopenharmony_ci		break;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	default:
7388c2ecf20Sopenharmony_ci		msg.type = MIPI_DSI_DCS_LONG_WRITE;
7398c2ecf20Sopenharmony_ci		break;
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	return mipi_dsi_device_transfer(dsi, &msg);
7438c2ecf20Sopenharmony_ci}
7448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci/**
7478c2ecf20Sopenharmony_ci * mipi_dsi_dcs_write() - send DCS write command
7488c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
7498c2ecf20Sopenharmony_ci * @cmd: DCS command
7508c2ecf20Sopenharmony_ci * @data: buffer containing the command payload
7518c2ecf20Sopenharmony_ci * @len: command payload length
7528c2ecf20Sopenharmony_ci *
7538c2ecf20Sopenharmony_ci * This function will automatically choose the right data type depending on
7548c2ecf20Sopenharmony_ci * the command payload length.
7558c2ecf20Sopenharmony_ci *
7568c2ecf20Sopenharmony_ci * Return: The number of bytes successfully transmitted or a negative error
7578c2ecf20Sopenharmony_ci * code on failure.
7588c2ecf20Sopenharmony_ci */
7598c2ecf20Sopenharmony_cissize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
7608c2ecf20Sopenharmony_ci			   const void *data, size_t len)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci	ssize_t err;
7638c2ecf20Sopenharmony_ci	size_t size;
7648c2ecf20Sopenharmony_ci	u8 stack_tx[8];
7658c2ecf20Sopenharmony_ci	u8 *tx;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	size = 1 + len;
7688c2ecf20Sopenharmony_ci	if (len > ARRAY_SIZE(stack_tx) - 1) {
7698c2ecf20Sopenharmony_ci		tx = kmalloc(size, GFP_KERNEL);
7708c2ecf20Sopenharmony_ci		if (!tx)
7718c2ecf20Sopenharmony_ci			return -ENOMEM;
7728c2ecf20Sopenharmony_ci	} else {
7738c2ecf20Sopenharmony_ci		tx = stack_tx;
7748c2ecf20Sopenharmony_ci	}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	/* concatenate the DCS command byte and the payload */
7778c2ecf20Sopenharmony_ci	tx[0] = cmd;
7788c2ecf20Sopenharmony_ci	if (data)
7798c2ecf20Sopenharmony_ci		memcpy(&tx[1], data, len);
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write_buffer(dsi, tx, size);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (tx != stack_tx)
7848c2ecf20Sopenharmony_ci		kfree(tx);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	return err;
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_write);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci/**
7918c2ecf20Sopenharmony_ci * mipi_dsi_dcs_read() - send DCS read request command
7928c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
7938c2ecf20Sopenharmony_ci * @cmd: DCS command
7948c2ecf20Sopenharmony_ci * @data: buffer in which to receive data
7958c2ecf20Sopenharmony_ci * @len: size of receive buffer
7968c2ecf20Sopenharmony_ci *
7978c2ecf20Sopenharmony_ci * Return: The number of bytes read or a negative error code on failure.
7988c2ecf20Sopenharmony_ci */
7998c2ecf20Sopenharmony_cissize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
8008c2ecf20Sopenharmony_ci			  size_t len)
8018c2ecf20Sopenharmony_ci{
8028c2ecf20Sopenharmony_ci	struct mipi_dsi_msg msg = {
8038c2ecf20Sopenharmony_ci		.channel = dsi->channel,
8048c2ecf20Sopenharmony_ci		.type = MIPI_DSI_DCS_READ,
8058c2ecf20Sopenharmony_ci		.tx_buf = &cmd,
8068c2ecf20Sopenharmony_ci		.tx_len = 1,
8078c2ecf20Sopenharmony_ci		.rx_buf = data,
8088c2ecf20Sopenharmony_ci		.rx_len = len
8098c2ecf20Sopenharmony_ci	};
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	return mipi_dsi_device_transfer(dsi, &msg);
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_read);
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci/**
8168c2ecf20Sopenharmony_ci * mipi_dsi_dcs_nop() - send DCS nop packet
8178c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
8188c2ecf20Sopenharmony_ci *
8198c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
8208c2ecf20Sopenharmony_ci */
8218c2ecf20Sopenharmony_ciint mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi)
8228c2ecf20Sopenharmony_ci{
8238c2ecf20Sopenharmony_ci	ssize_t err;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_NOP, NULL, 0);
8268c2ecf20Sopenharmony_ci	if (err < 0)
8278c2ecf20Sopenharmony_ci		return err;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	return 0;
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_nop);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci/**
8348c2ecf20Sopenharmony_ci * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
8358c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
8368c2ecf20Sopenharmony_ci *
8378c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
8388c2ecf20Sopenharmony_ci */
8398c2ecf20Sopenharmony_ciint mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci	ssize_t err;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SOFT_RESET, NULL, 0);
8448c2ecf20Sopenharmony_ci	if (err < 0)
8458c2ecf20Sopenharmony_ci		return err;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	return 0;
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_soft_reset);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci/**
8528c2ecf20Sopenharmony_ci * mipi_dsi_dcs_get_power_mode() - query the display module's current power
8538c2ecf20Sopenharmony_ci *    mode
8548c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
8558c2ecf20Sopenharmony_ci * @mode: return location for the current power mode
8568c2ecf20Sopenharmony_ci *
8578c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
8588c2ecf20Sopenharmony_ci */
8598c2ecf20Sopenharmony_ciint mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	ssize_t err;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_POWER_MODE, mode,
8648c2ecf20Sopenharmony_ci				sizeof(*mode));
8658c2ecf20Sopenharmony_ci	if (err <= 0) {
8668c2ecf20Sopenharmony_ci		if (err == 0)
8678c2ecf20Sopenharmony_ci			err = -ENODATA;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci		return err;
8708c2ecf20Sopenharmony_ci	}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	return 0;
8738c2ecf20Sopenharmony_ci}
8748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_get_power_mode);
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci/**
8778c2ecf20Sopenharmony_ci * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
8788c2ecf20Sopenharmony_ci *    data used by the interface
8798c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
8808c2ecf20Sopenharmony_ci * @format: return location for the pixel format
8818c2ecf20Sopenharmony_ci *
8828c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
8838c2ecf20Sopenharmony_ci */
8848c2ecf20Sopenharmony_ciint mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format)
8858c2ecf20Sopenharmony_ci{
8868c2ecf20Sopenharmony_ci	ssize_t err;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_PIXEL_FORMAT, format,
8898c2ecf20Sopenharmony_ci				sizeof(*format));
8908c2ecf20Sopenharmony_ci	if (err <= 0) {
8918c2ecf20Sopenharmony_ci		if (err == 0)
8928c2ecf20Sopenharmony_ci			err = -ENODATA;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci		return err;
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	return 0;
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_get_pixel_format);
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci/**
9028c2ecf20Sopenharmony_ci * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
9038c2ecf20Sopenharmony_ci *    display module except interface communication
9048c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
9058c2ecf20Sopenharmony_ci *
9068c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
9078c2ecf20Sopenharmony_ci */
9088c2ecf20Sopenharmony_ciint mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi)
9098c2ecf20Sopenharmony_ci{
9108c2ecf20Sopenharmony_ci	ssize_t err;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
9138c2ecf20Sopenharmony_ci	if (err < 0)
9148c2ecf20Sopenharmony_ci		return err;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	return 0;
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_enter_sleep_mode);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci/**
9218c2ecf20Sopenharmony_ci * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
9228c2ecf20Sopenharmony_ci *    module
9238c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
9248c2ecf20Sopenharmony_ci *
9258c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
9268c2ecf20Sopenharmony_ci */
9278c2ecf20Sopenharmony_ciint mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi)
9288c2ecf20Sopenharmony_ci{
9298c2ecf20Sopenharmony_ci	ssize_t err;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
9328c2ecf20Sopenharmony_ci	if (err < 0)
9338c2ecf20Sopenharmony_ci		return err;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	return 0;
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_exit_sleep_mode);
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci/**
9408c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
9418c2ecf20Sopenharmony_ci *    display device
9428c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
9438c2ecf20Sopenharmony_ci *
9448c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
9458c2ecf20Sopenharmony_ci */
9468c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi)
9478c2ecf20Sopenharmony_ci{
9488c2ecf20Sopenharmony_ci	ssize_t err;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
9518c2ecf20Sopenharmony_ci	if (err < 0)
9528c2ecf20Sopenharmony_ci		return err;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	return 0;
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_display_off);
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci/**
9598c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
9608c2ecf20Sopenharmony_ci *    display device
9618c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
9628c2ecf20Sopenharmony_ci *
9638c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure
9648c2ecf20Sopenharmony_ci */
9658c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi)
9668c2ecf20Sopenharmony_ci{
9678c2ecf20Sopenharmony_ci	ssize_t err;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
9708c2ecf20Sopenharmony_ci	if (err < 0)
9718c2ecf20Sopenharmony_ci		return err;
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	return 0;
9748c2ecf20Sopenharmony_ci}
9758c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_display_on);
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci/**
9788c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
9798c2ecf20Sopenharmony_ci *    memory accessed by the host processor
9808c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
9818c2ecf20Sopenharmony_ci * @start: first column of frame memory
9828c2ecf20Sopenharmony_ci * @end: last column of frame memory
9838c2ecf20Sopenharmony_ci *
9848c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
9858c2ecf20Sopenharmony_ci */
9868c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
9878c2ecf20Sopenharmony_ci				    u16 end)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
9908c2ecf20Sopenharmony_ci	ssize_t err;
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_COLUMN_ADDRESS, payload,
9938c2ecf20Sopenharmony_ci				 sizeof(payload));
9948c2ecf20Sopenharmony_ci	if (err < 0)
9958c2ecf20Sopenharmony_ci		return err;
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	return 0;
9988c2ecf20Sopenharmony_ci}
9998c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_column_address);
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci/**
10028c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
10038c2ecf20Sopenharmony_ci *    memory accessed by the host processor
10048c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
10058c2ecf20Sopenharmony_ci * @start: first page of frame memory
10068c2ecf20Sopenharmony_ci * @end: last page of frame memory
10078c2ecf20Sopenharmony_ci *
10088c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
10098c2ecf20Sopenharmony_ci */
10108c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
10118c2ecf20Sopenharmony_ci				  u16 end)
10128c2ecf20Sopenharmony_ci{
10138c2ecf20Sopenharmony_ci	u8 payload[4] = { start >> 8, start & 0xff, end >> 8, end & 0xff };
10148c2ecf20Sopenharmony_ci	ssize_t err;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PAGE_ADDRESS, payload,
10178c2ecf20Sopenharmony_ci				 sizeof(payload));
10188c2ecf20Sopenharmony_ci	if (err < 0)
10198c2ecf20Sopenharmony_ci		return err;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	return 0;
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_page_address);
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci/**
10268c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
10278c2ecf20Sopenharmony_ci *    output signal on the TE signal line
10288c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
10298c2ecf20Sopenharmony_ci *
10308c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure
10318c2ecf20Sopenharmony_ci */
10328c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi)
10338c2ecf20Sopenharmony_ci{
10348c2ecf20Sopenharmony_ci	ssize_t err;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_OFF, NULL, 0);
10378c2ecf20Sopenharmony_ci	if (err < 0)
10388c2ecf20Sopenharmony_ci		return err;
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	return 0;
10418c2ecf20Sopenharmony_ci}
10428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_tear_off);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci/**
10458c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
10468c2ecf20Sopenharmony_ci *    output signal on the TE signal line.
10478c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
10488c2ecf20Sopenharmony_ci * @mode: the Tearing Effect Output Line mode
10498c2ecf20Sopenharmony_ci *
10508c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure
10518c2ecf20Sopenharmony_ci */
10528c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
10538c2ecf20Sopenharmony_ci			     enum mipi_dsi_dcs_tear_mode mode)
10548c2ecf20Sopenharmony_ci{
10558c2ecf20Sopenharmony_ci	u8 value = mode;
10568c2ecf20Sopenharmony_ci	ssize_t err;
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_ON, &value,
10598c2ecf20Sopenharmony_ci				 sizeof(value));
10608c2ecf20Sopenharmony_ci	if (err < 0)
10618c2ecf20Sopenharmony_ci		return err;
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	return 0;
10648c2ecf20Sopenharmony_ci}
10658c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_tear_on);
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci/**
10688c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
10698c2ecf20Sopenharmony_ci *    data used by the interface
10708c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
10718c2ecf20Sopenharmony_ci * @format: pixel format
10728c2ecf20Sopenharmony_ci *
10738c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
10748c2ecf20Sopenharmony_ci */
10758c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format)
10768c2ecf20Sopenharmony_ci{
10778c2ecf20Sopenharmony_ci	ssize_t err;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_PIXEL_FORMAT, &format,
10808c2ecf20Sopenharmony_ci				 sizeof(format));
10818c2ecf20Sopenharmony_ci	if (err < 0)
10828c2ecf20Sopenharmony_ci		return err;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	return 0;
10858c2ecf20Sopenharmony_ci}
10868c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_pixel_format);
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci/**
10898c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
10908c2ecf20Sopenharmony_ci *    the Tearing Effect output signal of the display module
10918c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
10928c2ecf20Sopenharmony_ci * @scanline: scanline to use as trigger
10938c2ecf20Sopenharmony_ci *
10948c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure
10958c2ecf20Sopenharmony_ci */
10968c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline)
10978c2ecf20Sopenharmony_ci{
10988c2ecf20Sopenharmony_ci	u8 payload[2] = { scanline >> 8, scanline & 0xff };
10998c2ecf20Sopenharmony_ci	ssize_t err;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_TEAR_SCANLINE, payload,
11028c2ecf20Sopenharmony_ci				 sizeof(payload));
11038c2ecf20Sopenharmony_ci	if (err < 0)
11048c2ecf20Sopenharmony_ci		return err;
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	return 0;
11078c2ecf20Sopenharmony_ci}
11088c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_tear_scanline);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci/**
11118c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
11128c2ecf20Sopenharmony_ci *    display
11138c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
11148c2ecf20Sopenharmony_ci * @brightness: brightness value
11158c2ecf20Sopenharmony_ci *
11168c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
11178c2ecf20Sopenharmony_ci */
11188c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
11198c2ecf20Sopenharmony_ci					u16 brightness)
11208c2ecf20Sopenharmony_ci{
11218c2ecf20Sopenharmony_ci	u8 payload[2] = { brightness & 0xff, brightness >> 8 };
11228c2ecf20Sopenharmony_ci	ssize_t err;
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
11258c2ecf20Sopenharmony_ci				 payload, sizeof(payload));
11268c2ecf20Sopenharmony_ci	if (err < 0)
11278c2ecf20Sopenharmony_ci		return err;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	return 0;
11308c2ecf20Sopenharmony_ci}
11318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness);
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci/**
11348c2ecf20Sopenharmony_ci * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
11358c2ecf20Sopenharmony_ci *    of the display
11368c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
11378c2ecf20Sopenharmony_ci * @brightness: brightness value
11388c2ecf20Sopenharmony_ci *
11398c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
11408c2ecf20Sopenharmony_ci */
11418c2ecf20Sopenharmony_ciint mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
11428c2ecf20Sopenharmony_ci					u16 *brightness)
11438c2ecf20Sopenharmony_ci{
11448c2ecf20Sopenharmony_ci	ssize_t err;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
11478c2ecf20Sopenharmony_ci				brightness, sizeof(*brightness));
11488c2ecf20Sopenharmony_ci	if (err <= 0) {
11498c2ecf20Sopenharmony_ci		if (err == 0)
11508c2ecf20Sopenharmony_ci			err = -ENODATA;
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci		return err;
11538c2ecf20Sopenharmony_ci	}
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	return 0;
11568c2ecf20Sopenharmony_ci}
11578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness);
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci/**
11608c2ecf20Sopenharmony_ci * mipi_dsi_dcs_set_display_brightness_large() - sets the 16-bit brightness value
11618c2ecf20Sopenharmony_ci *    of the display
11628c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
11638c2ecf20Sopenharmony_ci * @brightness: brightness value
11648c2ecf20Sopenharmony_ci *
11658c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
11668c2ecf20Sopenharmony_ci */
11678c2ecf20Sopenharmony_ciint mipi_dsi_dcs_set_display_brightness_large(struct mipi_dsi_device *dsi,
11688c2ecf20Sopenharmony_ci					     u16 brightness)
11698c2ecf20Sopenharmony_ci{
11708c2ecf20Sopenharmony_ci	u8 payload[2] = { brightness >> 8, brightness & 0xff };
11718c2ecf20Sopenharmony_ci	ssize_t err;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
11748c2ecf20Sopenharmony_ci				 payload, sizeof(payload));
11758c2ecf20Sopenharmony_ci	if (err < 0)
11768c2ecf20Sopenharmony_ci		return err;
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci	return 0;
11798c2ecf20Sopenharmony_ci}
11808c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_set_display_brightness_large);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci/**
11838c2ecf20Sopenharmony_ci * mipi_dsi_dcs_get_display_brightness_large() - gets the current 16-bit
11848c2ecf20Sopenharmony_ci *    brightness value of the display
11858c2ecf20Sopenharmony_ci * @dsi: DSI peripheral device
11868c2ecf20Sopenharmony_ci * @brightness: brightness value
11878c2ecf20Sopenharmony_ci *
11888c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
11898c2ecf20Sopenharmony_ci */
11908c2ecf20Sopenharmony_ciint mipi_dsi_dcs_get_display_brightness_large(struct mipi_dsi_device *dsi,
11918c2ecf20Sopenharmony_ci					     u16 *brightness)
11928c2ecf20Sopenharmony_ci{
11938c2ecf20Sopenharmony_ci	u8 brightness_be[2];
11948c2ecf20Sopenharmony_ci	ssize_t err;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	err = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
11978c2ecf20Sopenharmony_ci				brightness_be, sizeof(brightness_be));
11988c2ecf20Sopenharmony_ci	if (err <= 0) {
11998c2ecf20Sopenharmony_ci		if (err == 0)
12008c2ecf20Sopenharmony_ci			err = -ENODATA;
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci		return err;
12038c2ecf20Sopenharmony_ci	}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	*brightness = (brightness_be[0] << 8) | brightness_be[1];
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	return 0;
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_dcs_get_display_brightness_large);
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_cistatic int mipi_dsi_drv_probe(struct device *dev)
12128c2ecf20Sopenharmony_ci{
12138c2ecf20Sopenharmony_ci	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
12148c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	return drv->probe(dsi);
12178c2ecf20Sopenharmony_ci}
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_cistatic int mipi_dsi_drv_remove(struct device *dev)
12208c2ecf20Sopenharmony_ci{
12218c2ecf20Sopenharmony_ci	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
12228c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	return drv->remove(dsi);
12258c2ecf20Sopenharmony_ci}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_cistatic void mipi_dsi_drv_shutdown(struct device *dev)
12288c2ecf20Sopenharmony_ci{
12298c2ecf20Sopenharmony_ci	struct mipi_dsi_driver *drv = to_mipi_dsi_driver(dev->driver);
12308c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	drv->shutdown(dsi);
12338c2ecf20Sopenharmony_ci}
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci/**
12368c2ecf20Sopenharmony_ci * mipi_dsi_driver_register_full() - register a driver for DSI devices
12378c2ecf20Sopenharmony_ci * @drv: DSI driver structure
12388c2ecf20Sopenharmony_ci * @owner: owner module
12398c2ecf20Sopenharmony_ci *
12408c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
12418c2ecf20Sopenharmony_ci */
12428c2ecf20Sopenharmony_ciint mipi_dsi_driver_register_full(struct mipi_dsi_driver *drv,
12438c2ecf20Sopenharmony_ci				  struct module *owner)
12448c2ecf20Sopenharmony_ci{
12458c2ecf20Sopenharmony_ci	drv->driver.bus = &mipi_dsi_bus_type;
12468c2ecf20Sopenharmony_ci	drv->driver.owner = owner;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	if (drv->probe)
12498c2ecf20Sopenharmony_ci		drv->driver.probe = mipi_dsi_drv_probe;
12508c2ecf20Sopenharmony_ci	if (drv->remove)
12518c2ecf20Sopenharmony_ci		drv->driver.remove = mipi_dsi_drv_remove;
12528c2ecf20Sopenharmony_ci	if (drv->shutdown)
12538c2ecf20Sopenharmony_ci		drv->driver.shutdown = mipi_dsi_drv_shutdown;
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	return driver_register(&drv->driver);
12568c2ecf20Sopenharmony_ci}
12578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_driver_register_full);
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci/**
12608c2ecf20Sopenharmony_ci * mipi_dsi_driver_unregister() - unregister a driver for DSI devices
12618c2ecf20Sopenharmony_ci * @drv: DSI driver structure
12628c2ecf20Sopenharmony_ci *
12638c2ecf20Sopenharmony_ci * Return: 0 on success or a negative error code on failure.
12648c2ecf20Sopenharmony_ci */
12658c2ecf20Sopenharmony_civoid mipi_dsi_driver_unregister(struct mipi_dsi_driver *drv)
12668c2ecf20Sopenharmony_ci{
12678c2ecf20Sopenharmony_ci	driver_unregister(&drv->driver);
12688c2ecf20Sopenharmony_ci}
12698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mipi_dsi_driver_unregister);
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_cistatic int __init mipi_dsi_bus_init(void)
12728c2ecf20Sopenharmony_ci{
12738c2ecf20Sopenharmony_ci	return bus_register(&mipi_dsi_bus_type);
12748c2ecf20Sopenharmony_ci}
12758c2ecf20Sopenharmony_cipostcore_initcall(mipi_dsi_bus_init);
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
12788c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MIPI DSI Bus");
12798c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL and additional rights");
1280