WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / panel / panel-nec-nl8048hl11.c
blob6e5ab1debc8b43534d77be6251d70cea8d576a53
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * NEC NL8048HL11 Panel Driver
5 * Copyright (C) 2019 Texas Instruments Incorporated
7 * Based on the omapdrm-specific panel-nec-nl8048hl11 driver
9 * Copyright (C) 2010 Texas Instruments Incorporated
10 * Author: Erik Gilling <konkers@android.com>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/pm.h>
17 #include <linux/spi/spi.h>
19 #include <drm/drm_connector.h>
20 #include <drm/drm_modes.h>
21 #include <drm/drm_panel.h>
23 struct nl8048_panel {
24 struct drm_panel panel;
26 struct spi_device *spi;
27 struct gpio_desc *reset_gpio;
30 #define to_nl8048_device(p) container_of(p, struct nl8048_panel, panel)
32 static int nl8048_write(struct nl8048_panel *lcd, unsigned char addr,
33 unsigned char value)
35 u8 data[4] = { value, 0x01, addr, 0x00 };
36 int ret;
38 ret = spi_write(lcd->spi, data, sizeof(data));
39 if (ret)
40 dev_err(&lcd->spi->dev, "SPI write to %u failed: %d\n",
41 addr, ret);
43 return ret;
46 static int nl8048_init(struct nl8048_panel *lcd)
48 static const struct {
49 unsigned char addr;
50 unsigned char data;
51 } nl8048_init_seq[] = {
52 { 3, 0x01 }, { 0, 0x00 }, { 1, 0x01 }, { 4, 0x00 },
53 { 5, 0x14 }, { 6, 0x24 }, { 16, 0xd7 }, { 17, 0x00 },
54 { 18, 0x00 }, { 19, 0x55 }, { 20, 0x01 }, { 21, 0x70 },
55 { 22, 0x1e }, { 23, 0x25 }, { 24, 0x25 }, { 25, 0x02 },
56 { 26, 0x02 }, { 27, 0xa0 }, { 32, 0x2f }, { 33, 0x0f },
57 { 34, 0x0f }, { 35, 0x0f }, { 36, 0x0f }, { 37, 0x0f },
58 { 38, 0x0f }, { 39, 0x00 }, { 40, 0x02 }, { 41, 0x02 },
59 { 42, 0x02 }, { 43, 0x0f }, { 44, 0x0f }, { 45, 0x0f },
60 { 46, 0x0f }, { 47, 0x0f }, { 48, 0x0f }, { 49, 0x0f },
61 { 50, 0x00 }, { 51, 0x02 }, { 52, 0x02 }, { 53, 0x02 },
62 { 80, 0x0c }, { 83, 0x42 }, { 84, 0x42 }, { 85, 0x41 },
63 { 86, 0x14 }, { 89, 0x88 }, { 90, 0x01 }, { 91, 0x00 },
64 { 92, 0x02 }, { 93, 0x0c }, { 94, 0x1c }, { 95, 0x27 },
65 { 98, 0x49 }, { 99, 0x27 }, { 102, 0x76 }, { 103, 0x27 },
66 { 112, 0x01 }, { 113, 0x0e }, { 114, 0x02 }, { 115, 0x0c },
67 { 118, 0x0c }, { 121, 0x30 }, { 130, 0x00 }, { 131, 0x00 },
68 { 132, 0xfc }, { 134, 0x00 }, { 136, 0x00 }, { 138, 0x00 },
69 { 139, 0x00 }, { 140, 0x00 }, { 141, 0xfc }, { 143, 0x00 },
70 { 145, 0x00 }, { 147, 0x00 }, { 148, 0x00 }, { 149, 0x00 },
71 { 150, 0xfc }, { 152, 0x00 }, { 154, 0x00 }, { 156, 0x00 },
72 { 157, 0x00 },
75 unsigned int i;
76 int ret;
78 for (i = 0; i < ARRAY_SIZE(nl8048_init_seq); ++i) {
79 ret = nl8048_write(lcd, nl8048_init_seq[i].addr,
80 nl8048_init_seq[i].data);
81 if (ret < 0)
82 return ret;
85 udelay(20);
87 return nl8048_write(lcd, 2, 0x00);
90 static int nl8048_disable(struct drm_panel *panel)
92 struct nl8048_panel *lcd = to_nl8048_device(panel);
94 gpiod_set_value_cansleep(lcd->reset_gpio, 0);
96 return 0;
99 static int nl8048_enable(struct drm_panel *panel)
101 struct nl8048_panel *lcd = to_nl8048_device(panel);
103 gpiod_set_value_cansleep(lcd->reset_gpio, 1);
105 return 0;
108 static const struct drm_display_mode nl8048_mode = {
109 /* NEC PIX Clock Ratings MIN:21.8MHz TYP:23.8MHz MAX:25.7MHz */
110 .clock = 23800,
111 .hdisplay = 800,
112 .hsync_start = 800 + 6,
113 .hsync_end = 800 + 6 + 1,
114 .htotal = 800 + 6 + 1 + 4,
115 .vdisplay = 480,
116 .vsync_start = 480 + 3,
117 .vsync_end = 480 + 3 + 1,
118 .vtotal = 480 + 3 + 1 + 4,
119 .type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
120 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
121 .width_mm = 89,
122 .height_mm = 53,
125 static int nl8048_get_modes(struct drm_panel *panel,
126 struct drm_connector *connector)
128 struct drm_display_mode *mode;
130 mode = drm_mode_duplicate(connector->dev, &nl8048_mode);
131 if (!mode)
132 return -ENOMEM;
134 drm_mode_set_name(mode);
135 drm_mode_probed_add(connector, mode);
137 connector->display_info.width_mm = nl8048_mode.width_mm;
138 connector->display_info.height_mm = nl8048_mode.height_mm;
139 connector->display_info.bus_flags = DRM_BUS_FLAG_DE_HIGH
140 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE
141 | DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
143 return 1;
146 static const struct drm_panel_funcs nl8048_funcs = {
147 .disable = nl8048_disable,
148 .enable = nl8048_enable,
149 .get_modes = nl8048_get_modes,
152 static int __maybe_unused nl8048_suspend(struct device *dev)
154 struct nl8048_panel *lcd = dev_get_drvdata(dev);
156 nl8048_write(lcd, 2, 0x01);
157 msleep(40);
159 return 0;
162 static int __maybe_unused nl8048_resume(struct device *dev)
164 struct nl8048_panel *lcd = dev_get_drvdata(dev);
166 /* Reinitialize the panel. */
167 spi_setup(lcd->spi);
168 nl8048_write(lcd, 2, 0x00);
169 nl8048_init(lcd);
171 return 0;
174 static SIMPLE_DEV_PM_OPS(nl8048_pm_ops, nl8048_suspend, nl8048_resume);
176 static int nl8048_probe(struct spi_device *spi)
178 struct nl8048_panel *lcd;
179 int ret;
181 lcd = devm_kzalloc(&spi->dev, sizeof(*lcd), GFP_KERNEL);
182 if (!lcd)
183 return -ENOMEM;
185 spi_set_drvdata(spi, lcd);
186 lcd->spi = spi;
188 lcd->reset_gpio = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_LOW);
189 if (IS_ERR(lcd->reset_gpio)) {
190 dev_err(&spi->dev, "failed to parse reset gpio\n");
191 return PTR_ERR(lcd->reset_gpio);
194 spi->mode = SPI_MODE_0;
195 spi->bits_per_word = 32;
197 ret = spi_setup(spi);
198 if (ret < 0) {
199 dev_err(&spi->dev, "failed to setup SPI: %d\n", ret);
200 return ret;
203 ret = nl8048_init(lcd);
204 if (ret < 0)
205 return ret;
207 drm_panel_init(&lcd->panel, &lcd->spi->dev, &nl8048_funcs,
208 DRM_MODE_CONNECTOR_DPI);
210 drm_panel_add(&lcd->panel);
212 return 0;
215 static int nl8048_remove(struct spi_device *spi)
217 struct nl8048_panel *lcd = spi_get_drvdata(spi);
219 drm_panel_remove(&lcd->panel);
220 drm_panel_disable(&lcd->panel);
221 drm_panel_unprepare(&lcd->panel);
223 return 0;
226 static const struct of_device_id nl8048_of_match[] = {
227 { .compatible = "nec,nl8048hl11", },
228 { /* sentinel */ },
231 MODULE_DEVICE_TABLE(of, nl8048_of_match);
233 static const struct spi_device_id nl8048_ids[] = {
234 { "nl8048hl11", 0 },
235 { /* sentinel */ }
238 MODULE_DEVICE_TABLE(spi, nl8048_ids);
240 static struct spi_driver nl8048_driver = {
241 .probe = nl8048_probe,
242 .remove = nl8048_remove,
243 .id_table = nl8048_ids,
244 .driver = {
245 .name = "panel-nec-nl8048hl11",
246 .pm = &nl8048_pm_ops,
247 .of_match_table = nl8048_of_match,
251 module_spi_driver(nl8048_driver);
253 MODULE_AUTHOR("Erik Gilling <konkers@android.com>");
254 MODULE_DESCRIPTION("NEC-NL8048HL11 Driver");
255 MODULE_LICENSE("GPL");