Loading...
// SPDX-License-Identifier: GPL-2.0+
/*
 * OMAP panel support
 *
 * Copyright (C) 2020 Dario Binacchi <dariobin@libero.it>
 */

#include <backlight.h>
#include <clk.h>
#include <display.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <log.h>
#include <panel.h>
#include <asm/gpio.h>
#include <linux/err.h>
#include "tilcdc.h"

struct tilcdc_panel_priv {
	struct tilcdc_panel_info info;
	struct display_timing timing;
	struct udevice *backlight;
	struct gpio_desc enable;
};

static int tilcdc_panel_enable_backlight(struct udevice *dev)
{
	struct tilcdc_panel_priv *priv = dev_get_priv(dev);

	if (dm_gpio_is_valid(&priv->enable))
		dm_gpio_set_value(&priv->enable, 1);

	if (priv->backlight)
		return backlight_enable(priv->backlight);

	return 0;
}

static int tilcdc_panel_set_backlight(struct udevice *dev, int percent)
{
	struct tilcdc_panel_priv *priv = dev_get_priv(dev);

	if (dm_gpio_is_valid(&priv->enable))
		dm_gpio_set_value(&priv->enable, 1);

	if (priv->backlight)
		return backlight_set_brightness(priv->backlight, percent);

	return 0;
}

int tilcdc_panel_get_display_info(struct udevice *dev,
				  struct tilcdc_panel_info *info)
{
	struct tilcdc_panel_priv *priv = dev_get_priv(dev);

	memcpy(info, &priv->info, sizeof(*info));
	return 0;
}

static int tilcdc_panel_get_display_timing(struct udevice *dev,
					   struct display_timing *timing)
{
	struct tilcdc_panel_priv *priv = dev_get_priv(dev);

	memcpy(timing, &priv->timing, sizeof(*timing));
	return 0;
}

static int tilcdc_panel_remove(struct udevice *dev)
{
	struct tilcdc_panel_priv *priv = dev_get_priv(dev);

	if (dm_gpio_is_valid(&priv->enable))
		dm_gpio_free(dev, &priv->enable);

	return 0;
}

static int tilcdc_panel_probe(struct udevice *dev)
{
	struct tilcdc_panel_priv *priv = dev_get_priv(dev);
	int err;

	err = uclass_get_device_by_phandle(UCLASS_PANEL_BACKLIGHT, dev,
					   "backlight", &priv->backlight);
	if (err)
		dev_warn(dev, "failed to get backlight\n");

	err = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
				   GPIOD_IS_OUT);
	if (err) {
		dev_warn(dev, "failed to get enable GPIO\n");
		if (err != -ENOENT)
			return err;
	}

	return 0;
}

static int tilcdc_panel_of_to_plat(struct udevice *dev)
{
	struct tilcdc_panel_priv *priv = dev_get_priv(dev);
	ofnode node;
	int err;

	err = ofnode_decode_display_timing(dev_ofnode(dev), 0, &priv->timing);
	if (err) {
		dev_err(dev, "failed to get display timing\n");
		return err;
	}

	node = dev_read_subnode(dev, "panel-info");
	if (!ofnode_valid(node)) {
		dev_err(dev, "missing 'panel-info' node\n");
		return -ENXIO;
	}

	err |= ofnode_read_u32(node, "ac-bias", &priv->info.ac_bias);
	err |= ofnode_read_u32(node, "ac-bias-intrpt",
			       &priv->info.ac_bias_intrpt);
	err |= ofnode_read_u32(node, "dma-burst-sz", &priv->info.dma_burst_sz);
	err |= ofnode_read_u32(node, "bpp", &priv->info.bpp);
	err |= ofnode_read_u32(node, "fdd", &priv->info.fdd);
	err |= ofnode_read_u32(node, "sync-edge", &priv->info.sync_edge);
	err |= ofnode_read_u32(node, "sync-ctrl", &priv->info.sync_ctrl);
	err |= ofnode_read_u32(node, "raster-order", &priv->info.raster_order);
	err |= ofnode_read_u32(node, "fifo-th", &priv->info.fifo_th);
	if (err) {
		dev_err(dev, "failed to get panel info\n");
		return err;
	}

	/* optional */
	priv->info.tft_alt_mode = ofnode_read_bool(node, "tft-alt-mode");
	priv->info.invert_pxl_clk = ofnode_read_bool(node, "invert-pxl-clk");

	dev_dbg(dev, "LCD: %dx%d, bpp=%d, clk=%d Hz\n",
		priv->timing.hactive.typ, priv->timing.vactive.typ,
		priv->info.bpp, priv->timing.pixelclock.typ);
	dev_dbg(dev, "     hbp=%d, hfp=%d, hsw=%d\n",
		priv->timing.hback_porch.typ, priv->timing.hfront_porch.typ,
		priv->timing.hsync_len.typ);
	dev_dbg(dev, "     vbp=%d, vfp=%d, vsw=%d\n",
		priv->timing.vback_porch.typ, priv->timing.vfront_porch.typ,
		priv->timing.vsync_len.typ);

	return 0;
}

static const struct panel_ops tilcdc_panel_ops = {
	.enable_backlight = tilcdc_panel_enable_backlight,
	.set_backlight = tilcdc_panel_set_backlight,
	.get_display_timing = tilcdc_panel_get_display_timing,
};

static const struct udevice_id tilcdc_panel_ids[] = {
	{.compatible = "ti,tilcdc,panel"},
	{}
};

U_BOOT_DRIVER(tilcdc_panel) = {
	.name = "tilcdc_panel",
	.id = UCLASS_PANEL,
	.of_match = tilcdc_panel_ids,
	.ops = &tilcdc_panel_ops,
	.of_to_plat = tilcdc_panel_of_to_plat,
	.probe = tilcdc_panel_probe,
	.remove = tilcdc_panel_remove,
	.priv_auto = sizeof(struct tilcdc_panel_priv),
};