18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Driver for ORISE Technology OTM3225A SOC for TFT LCD
38c2ecf20Sopenharmony_ci * Copyright (C) 2017, EETS GmbH, Felix Brack <fb@ltec.ch>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This driver implements a lcd device for the ORISE OTM3225A display
68c2ecf20Sopenharmony_ci * controller. The control interface to the display is SPI and the display's
78c2ecf20Sopenharmony_ci * memory is updated over the 16-bit RGB interface.
88c2ecf20Sopenharmony_ci * The main source of information for writing this driver was provided by the
98c2ecf20Sopenharmony_ci * OTM3225A datasheet from ORISE Technology. Some information arise from the
108c2ecf20Sopenharmony_ci * ILI9328 datasheet from ILITEK as well as from the datasheets and sample code
118c2ecf20Sopenharmony_ci * provided by Crystalfontz America Inc. who sells the CFAF240320A-032T, a 3.2"
128c2ecf20Sopenharmony_ci * TFT LC display using the OTM3225A controller.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/lcd.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define OTM3225A_INDEX_REG	0x70
238c2ecf20Sopenharmony_ci#define OTM3225A_DATA_REG	0x72
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* instruction register list */
268c2ecf20Sopenharmony_ci#define DRIVER_OUTPUT_CTRL_1	0x01
278c2ecf20Sopenharmony_ci#define DRIVER_WAVEFORM_CTRL	0x02
288c2ecf20Sopenharmony_ci#define ENTRY_MODE		0x03
298c2ecf20Sopenharmony_ci#define SCALING_CTRL		0x04
308c2ecf20Sopenharmony_ci#define DISPLAY_CTRL_1		0x07
318c2ecf20Sopenharmony_ci#define DISPLAY_CTRL_2		0x08
328c2ecf20Sopenharmony_ci#define DISPLAY_CTRL_3		0x09
338c2ecf20Sopenharmony_ci#define FRAME_CYCLE_CTRL	0x0A
348c2ecf20Sopenharmony_ci#define EXT_DISP_IFACE_CTRL_1	0x0C
358c2ecf20Sopenharmony_ci#define FRAME_MAKER_POS		0x0D
368c2ecf20Sopenharmony_ci#define EXT_DISP_IFACE_CTRL_2	0x0F
378c2ecf20Sopenharmony_ci#define POWER_CTRL_1		0x10
388c2ecf20Sopenharmony_ci#define POWER_CTRL_2		0x11
398c2ecf20Sopenharmony_ci#define POWER_CTRL_3		0x12
408c2ecf20Sopenharmony_ci#define POWER_CTRL_4		0x13
418c2ecf20Sopenharmony_ci#define GRAM_ADDR_HORIZ_SET	0x20
428c2ecf20Sopenharmony_ci#define GRAM_ADDR_VERT_SET	0x21
438c2ecf20Sopenharmony_ci#define GRAM_READ_WRITE		0x22
448c2ecf20Sopenharmony_ci#define POWER_CTRL_7		0x29
458c2ecf20Sopenharmony_ci#define FRAME_RATE_CTRL		0x2B
468c2ecf20Sopenharmony_ci#define GAMMA_CTRL_1		0x30
478c2ecf20Sopenharmony_ci#define GAMMA_CTRL_2		0x31
488c2ecf20Sopenharmony_ci#define GAMMA_CTRL_3		0x32
498c2ecf20Sopenharmony_ci#define GAMMA_CTRL_4		0x35
508c2ecf20Sopenharmony_ci#define GAMMA_CTRL_5		0x36
518c2ecf20Sopenharmony_ci#define GAMMA_CTRL_6		0x37
528c2ecf20Sopenharmony_ci#define GAMMA_CTRL_7		0x38
538c2ecf20Sopenharmony_ci#define GAMMA_CTRL_8		0x39
548c2ecf20Sopenharmony_ci#define GAMMA_CTRL_9		0x3C
558c2ecf20Sopenharmony_ci#define GAMMA_CTRL_10		0x3D
568c2ecf20Sopenharmony_ci#define WINDOW_HORIZ_RAM_START	0x50
578c2ecf20Sopenharmony_ci#define WINDOW_HORIZ_RAM_END	0x51
588c2ecf20Sopenharmony_ci#define WINDOW_VERT_RAM_START	0x52
598c2ecf20Sopenharmony_ci#define WINDOW_VERT_RAM_END	0x53
608c2ecf20Sopenharmony_ci#define DRIVER_OUTPUT_CTRL_2	0x60
618c2ecf20Sopenharmony_ci#define BASE_IMG_DISPLAY_CTRL	0x61
628c2ecf20Sopenharmony_ci#define VERT_SCROLL_CTRL	0x6A
638c2ecf20Sopenharmony_ci#define PD1_DISPLAY_POS		0x80
648c2ecf20Sopenharmony_ci#define PD1_RAM_START		0x81
658c2ecf20Sopenharmony_ci#define PD1_RAM_END		0x82
668c2ecf20Sopenharmony_ci#define PD2_DISPLAY_POS		0x83
678c2ecf20Sopenharmony_ci#define PD2_RAM_START		0x84
688c2ecf20Sopenharmony_ci#define PD2_RAM_END		0x85
698c2ecf20Sopenharmony_ci#define PANEL_IFACE_CTRL_1	0x90
708c2ecf20Sopenharmony_ci#define PANEL_IFACE_CTRL_2	0x92
718c2ecf20Sopenharmony_ci#define PANEL_IFACE_CTRL_4	0x95
728c2ecf20Sopenharmony_ci#define PANEL_IFACE_CTRL_5	0x97
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct otm3225a_data {
758c2ecf20Sopenharmony_ci	struct spi_device *spi;
768c2ecf20Sopenharmony_ci	struct lcd_device *ld;
778c2ecf20Sopenharmony_ci	int power;
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistruct otm3225a_spi_instruction {
818c2ecf20Sopenharmony_ci	unsigned char reg;	/* register to write */
828c2ecf20Sopenharmony_ci	unsigned short value;	/* data to write to 'reg' */
838c2ecf20Sopenharmony_ci	unsigned short delay;	/* delay in ms after write */
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic struct otm3225a_spi_instruction display_init[] = {
878c2ecf20Sopenharmony_ci	{ DRIVER_OUTPUT_CTRL_1,		0x0000, 0 },
888c2ecf20Sopenharmony_ci	{ DRIVER_WAVEFORM_CTRL,		0x0700, 0 },
898c2ecf20Sopenharmony_ci	{ ENTRY_MODE,			0x50A0, 0 },
908c2ecf20Sopenharmony_ci	{ SCALING_CTRL,			0x0000, 0 },
918c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_2,		0x0606, 0 },
928c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_3,		0x0000, 0 },
938c2ecf20Sopenharmony_ci	{ FRAME_CYCLE_CTRL,		0x0000, 0 },
948c2ecf20Sopenharmony_ci	{ EXT_DISP_IFACE_CTRL_1,	0x0000, 0 },
958c2ecf20Sopenharmony_ci	{ FRAME_MAKER_POS,		0x0000, 0 },
968c2ecf20Sopenharmony_ci	{ EXT_DISP_IFACE_CTRL_2,	0x0002, 0 },
978c2ecf20Sopenharmony_ci	{ POWER_CTRL_2,			0x0007, 0 },
988c2ecf20Sopenharmony_ci	{ POWER_CTRL_3,			0x0000, 0 },
998c2ecf20Sopenharmony_ci	{ POWER_CTRL_4,			0x0000, 200 },
1008c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,		0x0101, 0 },
1018c2ecf20Sopenharmony_ci	{ POWER_CTRL_1,			0x12B0, 0 },
1028c2ecf20Sopenharmony_ci	{ POWER_CTRL_2,			0x0007, 0 },
1038c2ecf20Sopenharmony_ci	{ POWER_CTRL_3,			0x01BB, 50 },
1048c2ecf20Sopenharmony_ci	{ POWER_CTRL_4,			0x0013, 0 },
1058c2ecf20Sopenharmony_ci	{ POWER_CTRL_7,			0x0010, 50 },
1068c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_1,			0x000A, 0 },
1078c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_2,			0x1326, 0 },
1088c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_3,			0x0A29, 0 },
1098c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_4,			0x0A0A, 0 },
1108c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_5,			0x1E03, 0 },
1118c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_6,			0x031E, 0 },
1128c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_7,			0x0706, 0 },
1138c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_8,			0x0303, 0 },
1148c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_9,			0x010E, 0 },
1158c2ecf20Sopenharmony_ci	{ GAMMA_CTRL_10,		0x040E, 0 },
1168c2ecf20Sopenharmony_ci	{ WINDOW_HORIZ_RAM_START,	0x0000, 0 },
1178c2ecf20Sopenharmony_ci	{ WINDOW_HORIZ_RAM_END,		0x00EF, 0 },
1188c2ecf20Sopenharmony_ci	{ WINDOW_VERT_RAM_START,	0x0000, 0 },
1198c2ecf20Sopenharmony_ci	{ WINDOW_VERT_RAM_END,		0x013F, 0 },
1208c2ecf20Sopenharmony_ci	{ DRIVER_OUTPUT_CTRL_2,		0x2700, 0 },
1218c2ecf20Sopenharmony_ci	{ BASE_IMG_DISPLAY_CTRL,	0x0001, 0 },
1228c2ecf20Sopenharmony_ci	{ VERT_SCROLL_CTRL,		0x0000, 0 },
1238c2ecf20Sopenharmony_ci	{ PD1_DISPLAY_POS,		0x0000, 0 },
1248c2ecf20Sopenharmony_ci	{ PD1_RAM_START,		0x0000, 0 },
1258c2ecf20Sopenharmony_ci	{ PD1_RAM_END,			0x0000, 0 },
1268c2ecf20Sopenharmony_ci	{ PD2_DISPLAY_POS,		0x0000, 0 },
1278c2ecf20Sopenharmony_ci	{ PD2_RAM_START,		0x0000, 0 },
1288c2ecf20Sopenharmony_ci	{ PD2_RAM_END,			0x0000, 0 },
1298c2ecf20Sopenharmony_ci	{ PANEL_IFACE_CTRL_1,		0x0010, 0 },
1308c2ecf20Sopenharmony_ci	{ PANEL_IFACE_CTRL_2,		0x0000, 0 },
1318c2ecf20Sopenharmony_ci	{ PANEL_IFACE_CTRL_4,		0x0210, 0 },
1328c2ecf20Sopenharmony_ci	{ PANEL_IFACE_CTRL_5,		0x0000, 0 },
1338c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,		0x0133, 0 },
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic struct otm3225a_spi_instruction display_enable_rgb_interface[] = {
1378c2ecf20Sopenharmony_ci	{ ENTRY_MODE,			0x1080, 0 },
1388c2ecf20Sopenharmony_ci	{ GRAM_ADDR_HORIZ_SET,		0x0000, 0 },
1398c2ecf20Sopenharmony_ci	{ GRAM_ADDR_VERT_SET,		0x0000, 0 },
1408c2ecf20Sopenharmony_ci	{ EXT_DISP_IFACE_CTRL_1,	0x0111, 500 },
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic struct otm3225a_spi_instruction display_off[] = {
1448c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,	0x0131, 100 },
1458c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,	0x0130, 100 },
1468c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,	0x0100, 0 },
1478c2ecf20Sopenharmony_ci	{ POWER_CTRL_1,		0x0280, 0 },
1488c2ecf20Sopenharmony_ci	{ POWER_CTRL_3,		0x018B, 0 },
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic struct otm3225a_spi_instruction display_on[] = {
1528c2ecf20Sopenharmony_ci	{ POWER_CTRL_1,		0x1280, 0 },
1538c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,	0x0101, 100 },
1548c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,	0x0121, 0 },
1558c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,	0x0123, 100 },
1568c2ecf20Sopenharmony_ci	{ DISPLAY_CTRL_1,	0x0133, 10 },
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic void otm3225a_write(struct spi_device *spi,
1608c2ecf20Sopenharmony_ci			   struct otm3225a_spi_instruction *instruction,
1618c2ecf20Sopenharmony_ci			   unsigned int count)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	unsigned char buf[3];
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	while (count--) {
1668c2ecf20Sopenharmony_ci		/* address register using index register */
1678c2ecf20Sopenharmony_ci		buf[0] = OTM3225A_INDEX_REG;
1688c2ecf20Sopenharmony_ci		buf[1] = 0x00;
1698c2ecf20Sopenharmony_ci		buf[2] = instruction->reg;
1708c2ecf20Sopenharmony_ci		spi_write(spi, buf, 3);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci		/* write data to addressed register */
1738c2ecf20Sopenharmony_ci		buf[0] = OTM3225A_DATA_REG;
1748c2ecf20Sopenharmony_ci		buf[1] = (instruction->value >> 8) & 0xff;
1758c2ecf20Sopenharmony_ci		buf[2] = instruction->value & 0xff;
1768c2ecf20Sopenharmony_ci		spi_write(spi, buf, 3);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci		/* execute delay if any */
1798c2ecf20Sopenharmony_ci		if (instruction->delay)
1808c2ecf20Sopenharmony_ci			msleep(instruction->delay);
1818c2ecf20Sopenharmony_ci		instruction++;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int otm3225a_set_power(struct lcd_device *ld, int power)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct otm3225a_data *dd = lcd_get_data(ld);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (power == dd->power)
1908c2ecf20Sopenharmony_ci		return 0;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (power > FB_BLANK_UNBLANK)
1938c2ecf20Sopenharmony_ci		otm3225a_write(dd->spi, display_off, ARRAY_SIZE(display_off));
1948c2ecf20Sopenharmony_ci	else
1958c2ecf20Sopenharmony_ci		otm3225a_write(dd->spi, display_on, ARRAY_SIZE(display_on));
1968c2ecf20Sopenharmony_ci	dd->power = power;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	return 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int otm3225a_get_power(struct lcd_device *ld)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct otm3225a_data *dd = lcd_get_data(ld);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return dd->power;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic struct lcd_ops otm3225a_ops = {
2098c2ecf20Sopenharmony_ci	.set_power = otm3225a_set_power,
2108c2ecf20Sopenharmony_ci	.get_power = otm3225a_get_power,
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int otm3225a_probe(struct spi_device *spi)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct otm3225a_data *dd;
2168c2ecf20Sopenharmony_ci	struct lcd_device *ld;
2178c2ecf20Sopenharmony_ci	struct device *dev = &spi->dev;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	dd = devm_kzalloc(dev, sizeof(struct otm3225a_data), GFP_KERNEL);
2208c2ecf20Sopenharmony_ci	if (dd == NULL)
2218c2ecf20Sopenharmony_ci		return -ENOMEM;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	ld = devm_lcd_device_register(dev, dev_name(dev), dev, dd,
2248c2ecf20Sopenharmony_ci				      &otm3225a_ops);
2258c2ecf20Sopenharmony_ci	if (IS_ERR(ld))
2268c2ecf20Sopenharmony_ci		return PTR_ERR(ld);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	dd->spi = spi;
2298c2ecf20Sopenharmony_ci	dd->ld = ld;
2308c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, dd);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	dev_info(dev, "Initializing and switching to RGB interface");
2338c2ecf20Sopenharmony_ci	otm3225a_write(spi, display_init, ARRAY_SIZE(display_init));
2348c2ecf20Sopenharmony_ci	otm3225a_write(spi, display_enable_rgb_interface,
2358c2ecf20Sopenharmony_ci		       ARRAY_SIZE(display_enable_rgb_interface));
2368c2ecf20Sopenharmony_ci	return 0;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic struct spi_driver otm3225a_driver = {
2408c2ecf20Sopenharmony_ci	.driver = {
2418c2ecf20Sopenharmony_ci		.name = "otm3225a",
2428c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
2438c2ecf20Sopenharmony_ci	},
2448c2ecf20Sopenharmony_ci	.probe = otm3225a_probe,
2458c2ecf20Sopenharmony_ci};
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cimodule_spi_driver(otm3225a_driver);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Felix Brack <fb@ltec.ch>");
2508c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("OTM3225A TFT LCD driver");
2518c2ecf20Sopenharmony_ciMODULE_VERSION("1.0.0");
2528c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
253