18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * l4f00242t03.c -- support for Epson L4F00242T03 LCD
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2007-2009 Freescale Semiconductor, Inc. All Rights Reserved.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
88c2ecf20Sopenharmony_ci *	Inspired by Marek Vasut work in l4f00242t03.c
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/device.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
188c2ecf20Sopenharmony_ci#include <linux/lcd.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
218c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct l4f00242t03_priv {
248c2ecf20Sopenharmony_ci	struct spi_device	*spi;
258c2ecf20Sopenharmony_ci	struct lcd_device	*ld;
268c2ecf20Sopenharmony_ci	int lcd_state;
278c2ecf20Sopenharmony_ci	struct regulator *io_reg;
288c2ecf20Sopenharmony_ci	struct regulator *core_reg;
298c2ecf20Sopenharmony_ci	struct gpio_desc *reset;
308c2ecf20Sopenharmony_ci	struct gpio_desc *enable;
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void l4f00242t03_reset(struct gpio_desc *gpiod)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	pr_debug("l4f00242t03_reset.\n");
368c2ecf20Sopenharmony_ci	gpiod_set_value(gpiod, 1);
378c2ecf20Sopenharmony_ci	mdelay(100);
388c2ecf20Sopenharmony_ci	gpiod_set_value(gpiod, 0);
398c2ecf20Sopenharmony_ci	mdelay(10);	/* tRES >= 100us */
408c2ecf20Sopenharmony_ci	gpiod_set_value(gpiod, 1);
418c2ecf20Sopenharmony_ci	mdelay(20);
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define param(x) ((x) | 0x100)
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic void l4f00242t03_lcd_init(struct spi_device *spi)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct l4f00242t03_priv *priv = spi_get_drvdata(spi);
498c2ecf20Sopenharmony_ci	const u16 cmd[] = { 0x36, param(0), 0x3A, param(0x60) };
508c2ecf20Sopenharmony_ci	int ret;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	dev_dbg(&spi->dev, "initializing LCD\n");
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	ret = regulator_set_voltage(priv->io_reg, 1800000, 1800000);
558c2ecf20Sopenharmony_ci	if (ret) {
568c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to set the IO regulator voltage.\n");
578c2ecf20Sopenharmony_ci		return;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci	ret = regulator_enable(priv->io_reg);
608c2ecf20Sopenharmony_ci	if (ret) {
618c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to enable the IO regulator.\n");
628c2ecf20Sopenharmony_ci		return;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ret = regulator_set_voltage(priv->core_reg, 2800000, 2800000);
668c2ecf20Sopenharmony_ci	if (ret) {
678c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to set the core regulator voltage.\n");
688c2ecf20Sopenharmony_ci		regulator_disable(priv->io_reg);
698c2ecf20Sopenharmony_ci		return;
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci	ret = regulator_enable(priv->core_reg);
728c2ecf20Sopenharmony_ci	if (ret) {
738c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to enable the core regulator.\n");
748c2ecf20Sopenharmony_ci		regulator_disable(priv->io_reg);
758c2ecf20Sopenharmony_ci		return;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	l4f00242t03_reset(priv->reset);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	gpiod_set_value(priv->enable, 1);
818c2ecf20Sopenharmony_ci	msleep(60);
828c2ecf20Sopenharmony_ci	spi_write(spi, (const u8 *)cmd, ARRAY_SIZE(cmd) * sizeof(u16));
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void l4f00242t03_lcd_powerdown(struct spi_device *spi)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct l4f00242t03_priv *priv = spi_get_drvdata(spi);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	dev_dbg(&spi->dev, "Powering down LCD\n");
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	gpiod_set_value(priv->enable, 0);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	regulator_disable(priv->io_reg);
948c2ecf20Sopenharmony_ci	regulator_disable(priv->core_reg);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int l4f00242t03_lcd_power_get(struct lcd_device *ld)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct l4f00242t03_priv *priv = lcd_get_data(ld);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return priv->lcd_state;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int l4f00242t03_lcd_power_set(struct lcd_device *ld, int power)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct l4f00242t03_priv *priv = lcd_get_data(ld);
1078c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	const u16 slpout = 0x11;
1108c2ecf20Sopenharmony_ci	const u16 dison = 0x29;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	const u16 slpin = 0x10;
1138c2ecf20Sopenharmony_ci	const u16 disoff = 0x28;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (power <= FB_BLANK_NORMAL) {
1168c2ecf20Sopenharmony_ci		if (priv->lcd_state <= FB_BLANK_NORMAL) {
1178c2ecf20Sopenharmony_ci			/* Do nothing, the LCD is running */
1188c2ecf20Sopenharmony_ci		} else if (priv->lcd_state < FB_BLANK_POWERDOWN) {
1198c2ecf20Sopenharmony_ci			dev_dbg(&spi->dev, "Resuming LCD\n");
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci			spi_write(spi, (const u8 *)&slpout, sizeof(u16));
1228c2ecf20Sopenharmony_ci			msleep(60);
1238c2ecf20Sopenharmony_ci			spi_write(spi, (const u8 *)&dison, sizeof(u16));
1248c2ecf20Sopenharmony_ci		} else {
1258c2ecf20Sopenharmony_ci			/* priv->lcd_state == FB_BLANK_POWERDOWN */
1268c2ecf20Sopenharmony_ci			l4f00242t03_lcd_init(spi);
1278c2ecf20Sopenharmony_ci			priv->lcd_state = FB_BLANK_VSYNC_SUSPEND;
1288c2ecf20Sopenharmony_ci			l4f00242t03_lcd_power_set(priv->ld, power);
1298c2ecf20Sopenharmony_ci		}
1308c2ecf20Sopenharmony_ci	} else if (power < FB_BLANK_POWERDOWN) {
1318c2ecf20Sopenharmony_ci		if (priv->lcd_state <= FB_BLANK_NORMAL) {
1328c2ecf20Sopenharmony_ci			/* Send the display in standby */
1338c2ecf20Sopenharmony_ci			dev_dbg(&spi->dev, "Standby the LCD\n");
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci			spi_write(spi, (const u8 *)&disoff, sizeof(u16));
1368c2ecf20Sopenharmony_ci			msleep(60);
1378c2ecf20Sopenharmony_ci			spi_write(spi, (const u8 *)&slpin, sizeof(u16));
1388c2ecf20Sopenharmony_ci		} else if (priv->lcd_state < FB_BLANK_POWERDOWN) {
1398c2ecf20Sopenharmony_ci			/* Do nothing, the LCD is already in standby */
1408c2ecf20Sopenharmony_ci		} else {
1418c2ecf20Sopenharmony_ci			/* priv->lcd_state == FB_BLANK_POWERDOWN */
1428c2ecf20Sopenharmony_ci			l4f00242t03_lcd_init(spi);
1438c2ecf20Sopenharmony_ci			priv->lcd_state = FB_BLANK_UNBLANK;
1448c2ecf20Sopenharmony_ci			l4f00242t03_lcd_power_set(ld, power);
1458c2ecf20Sopenharmony_ci		}
1468c2ecf20Sopenharmony_ci	} else {
1478c2ecf20Sopenharmony_ci		/* power == FB_BLANK_POWERDOWN */
1488c2ecf20Sopenharmony_ci		if (priv->lcd_state != FB_BLANK_POWERDOWN) {
1498c2ecf20Sopenharmony_ci			/* Clear the screen before shutting down */
1508c2ecf20Sopenharmony_ci			spi_write(spi, (const u8 *)&disoff, sizeof(u16));
1518c2ecf20Sopenharmony_ci			msleep(60);
1528c2ecf20Sopenharmony_ci			l4f00242t03_lcd_powerdown(spi);
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	priv->lcd_state = power;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return 0;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic struct lcd_ops l4f_ops = {
1628c2ecf20Sopenharmony_ci	.set_power	= l4f00242t03_lcd_power_set,
1638c2ecf20Sopenharmony_ci	.get_power	= l4f00242t03_lcd_power_get,
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic int l4f00242t03_probe(struct spi_device *spi)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct l4f00242t03_priv *priv;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&spi->dev, sizeof(struct l4f00242t03_priv),
1718c2ecf20Sopenharmony_ci				GFP_KERNEL);
1728c2ecf20Sopenharmony_ci	if (priv == NULL)
1738c2ecf20Sopenharmony_ci		return -ENOMEM;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, priv);
1768c2ecf20Sopenharmony_ci	spi->bits_per_word = 9;
1778c2ecf20Sopenharmony_ci	spi_setup(spi);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	priv->spi = spi;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	priv->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_HIGH);
1828c2ecf20Sopenharmony_ci	if (IS_ERR(priv->reset)) {
1838c2ecf20Sopenharmony_ci		dev_err(&spi->dev,
1848c2ecf20Sopenharmony_ci			"Unable to get the lcd l4f00242t03 reset gpio.\n");
1858c2ecf20Sopenharmony_ci		return PTR_ERR(priv->reset);
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci	gpiod_set_consumer_name(priv->reset, "lcd l4f00242t03 reset");
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	priv->enable = devm_gpiod_get(&spi->dev, "enable", GPIOD_OUT_LOW);
1908c2ecf20Sopenharmony_ci	if (IS_ERR(priv->enable)) {
1918c2ecf20Sopenharmony_ci		dev_err(&spi->dev,
1928c2ecf20Sopenharmony_ci			"Unable to get the lcd l4f00242t03 data en gpio.\n");
1938c2ecf20Sopenharmony_ci		return PTR_ERR(priv->enable);
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci	gpiod_set_consumer_name(priv->enable, "lcd l4f00242t03 data enable");
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	priv->io_reg = devm_regulator_get(&spi->dev, "vdd");
1988c2ecf20Sopenharmony_ci	if (IS_ERR(priv->io_reg)) {
1998c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "%s: Unable to get the IO regulator\n",
2008c2ecf20Sopenharmony_ci		       __func__);
2018c2ecf20Sopenharmony_ci		return PTR_ERR(priv->io_reg);
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	priv->core_reg = devm_regulator_get(&spi->dev, "vcore");
2058c2ecf20Sopenharmony_ci	if (IS_ERR(priv->core_reg)) {
2068c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "%s: Unable to get the core regulator\n",
2078c2ecf20Sopenharmony_ci		       __func__);
2088c2ecf20Sopenharmony_ci		return PTR_ERR(priv->core_reg);
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	priv->ld = devm_lcd_device_register(&spi->dev, "l4f00242t03", &spi->dev,
2128c2ecf20Sopenharmony_ci					priv, &l4f_ops);
2138c2ecf20Sopenharmony_ci	if (IS_ERR(priv->ld))
2148c2ecf20Sopenharmony_ci		return PTR_ERR(priv->ld);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* Init the LCD */
2178c2ecf20Sopenharmony_ci	l4f00242t03_lcd_init(spi);
2188c2ecf20Sopenharmony_ci	priv->lcd_state = FB_BLANK_VSYNC_SUSPEND;
2198c2ecf20Sopenharmony_ci	l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_UNBLANK);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	dev_info(&spi->dev, "Epson l4f00242t03 lcd probed.\n");
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return 0;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic int l4f00242t03_remove(struct spi_device *spi)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct l4f00242t03_priv *priv = spi_get_drvdata(spi);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_POWERDOWN);
2318c2ecf20Sopenharmony_ci	return 0;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic void l4f00242t03_shutdown(struct spi_device *spi)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct l4f00242t03_priv *priv = spi_get_drvdata(spi);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	if (priv)
2398c2ecf20Sopenharmony_ci		l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_POWERDOWN);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic struct spi_driver l4f00242t03_driver = {
2448c2ecf20Sopenharmony_ci	.driver = {
2458c2ecf20Sopenharmony_ci		.name	= "l4f00242t03",
2468c2ecf20Sopenharmony_ci	},
2478c2ecf20Sopenharmony_ci	.probe		= l4f00242t03_probe,
2488c2ecf20Sopenharmony_ci	.remove		= l4f00242t03_remove,
2498c2ecf20Sopenharmony_ci	.shutdown	= l4f00242t03_shutdown,
2508c2ecf20Sopenharmony_ci};
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cimodule_spi_driver(l4f00242t03_driver);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
2558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("EPSON L4F00242T03 LCD");
2568c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
257