GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / gpu / drm / panel / panel-nec-nl8048hl11.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NEC NL8048HL11 Panel Driver
4  *
5  * Copyright (C) 2019 Texas Instruments Incorporated
6  *
7  * Based on the omapdrm-specific panel-nec-nl8048hl11 driver
8  *
9  * Copyright (C) 2010 Texas Instruments Incorporated
10  * Author: Erik Gilling <konkers@android.com>
11  */
12
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>
18
19 #include <drm/drm_connector.h>
20 #include <drm/drm_modes.h>
21 #include <drm/drm_panel.h>
22
23 struct nl8048_panel {
24         struct drm_panel panel;
25
26         struct spi_device *spi;
27         struct gpio_desc *reset_gpio;
28 };
29
30 #define to_nl8048_device(p) container_of(p, struct nl8048_panel, panel)
31
32 static int nl8048_write(struct nl8048_panel *lcd, unsigned char addr,
33                         unsigned char value)
34 {
35         u8 data[4] = { value, 0x01, addr, 0x00 };
36         int ret;
37
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);
42
43         return ret;
44 }
45
46 static int nl8048_init(struct nl8048_panel *lcd)
47 {
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 },
73         };
74
75         unsigned int i;
76         int ret;
77
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;
83         }
84
85         udelay(20);
86
87         return nl8048_write(lcd, 2, 0x00);
88 }
89
90 static int nl8048_disable(struct drm_panel *panel)
91 {
92         struct nl8048_panel *lcd = to_nl8048_device(panel);
93
94         gpiod_set_value_cansleep(lcd->reset_gpio, 0);
95
96         return 0;
97 }
98
99 static int nl8048_enable(struct drm_panel *panel)
100 {
101         struct nl8048_panel *lcd = to_nl8048_device(panel);
102
103         gpiod_set_value_cansleep(lcd->reset_gpio, 1);
104
105         return 0;
106 }
107
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,
123 };
124
125 static int nl8048_get_modes(struct drm_panel *panel,
126                             struct drm_connector *connector)
127 {
128         struct drm_display_mode *mode;
129
130         mode = drm_mode_duplicate(connector->dev, &nl8048_mode);
131         if (!mode)
132                 return -ENOMEM;
133
134         drm_mode_set_name(mode);
135         drm_mode_probed_add(connector, mode);
136
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;
142
143         return 1;
144 }
145
146 static const struct drm_panel_funcs nl8048_funcs = {
147         .disable = nl8048_disable,
148         .enable = nl8048_enable,
149         .get_modes = nl8048_get_modes,
150 };
151
152 static int __maybe_unused nl8048_suspend(struct device *dev)
153 {
154         struct nl8048_panel *lcd = dev_get_drvdata(dev);
155
156         nl8048_write(lcd, 2, 0x01);
157         msleep(40);
158
159         return 0;
160 }
161
162 static int __maybe_unused nl8048_resume(struct device *dev)
163 {
164         struct nl8048_panel *lcd = dev_get_drvdata(dev);
165
166         /* Reinitialize the panel. */
167         spi_setup(lcd->spi);
168         nl8048_write(lcd, 2, 0x00);
169         nl8048_init(lcd);
170
171         return 0;
172 }
173
174 static SIMPLE_DEV_PM_OPS(nl8048_pm_ops, nl8048_suspend, nl8048_resume);
175
176 static int nl8048_probe(struct spi_device *spi)
177 {
178         struct nl8048_panel *lcd;
179         int ret;
180
181         lcd = devm_kzalloc(&spi->dev, sizeof(*lcd), GFP_KERNEL);
182         if (!lcd)
183                 return -ENOMEM;
184
185         spi_set_drvdata(spi, lcd);
186         lcd->spi = spi;
187
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);
192         }
193
194         spi->mode = SPI_MODE_0;
195         spi->bits_per_word = 32;
196
197         ret = spi_setup(spi);
198         if (ret < 0) {
199                 dev_err(&spi->dev, "failed to setup SPI: %d\n", ret);
200                 return ret;
201         }
202
203         ret = nl8048_init(lcd);
204         if (ret < 0)
205                 return ret;
206
207         drm_panel_init(&lcd->panel, &lcd->spi->dev, &nl8048_funcs,
208                        DRM_MODE_CONNECTOR_DPI);
209
210         drm_panel_add(&lcd->panel);
211
212         return 0;
213 }
214
215 static void nl8048_remove(struct spi_device *spi)
216 {
217         struct nl8048_panel *lcd = spi_get_drvdata(spi);
218
219         drm_panel_remove(&lcd->panel);
220         drm_panel_disable(&lcd->panel);
221         drm_panel_unprepare(&lcd->panel);
222 }
223
224 static const struct of_device_id nl8048_of_match[] = {
225         { .compatible = "nec,nl8048hl11", },
226         { /* sentinel */ },
227 };
228
229 MODULE_DEVICE_TABLE(of, nl8048_of_match);
230
231 static const struct spi_device_id nl8048_ids[] = {
232         { "nl8048hl11", 0 },
233         { /* sentinel */ }
234 };
235
236 MODULE_DEVICE_TABLE(spi, nl8048_ids);
237
238 static struct spi_driver nl8048_driver = {
239         .probe          = nl8048_probe,
240         .remove         = nl8048_remove,
241         .id_table       = nl8048_ids,
242         .driver         = {
243                 .name   = "panel-nec-nl8048hl11",
244                 .pm     = &nl8048_pm_ops,
245                 .of_match_table = nl8048_of_match,
246         },
247 };
248
249 module_spi_driver(nl8048_driver);
250
251 MODULE_AUTHOR("Erik Gilling <konkers@android.com>");
252 MODULE_DESCRIPTION("NEC-NL8048HL11 Driver");
253 MODULE_LICENSE("GPL");