treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / panel / panel-nec-nl8048hl11.c
blobc4f83f6384e1a930bd64e81a89967c57dca2165a
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 .vrefresh = 60,
120 .type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
121 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
122 .width_mm = 89,
123 .height_mm = 53,
126 static int nl8048_get_modes(struct drm_panel *panel,
127 struct drm_connector *connector)
129 struct drm_display_mode *mode;
131 mode = drm_mode_duplicate(connector->dev, &nl8048_mode);
132 if (!mode)
133 return -ENOMEM;
135 drm_mode_set_name(mode);
136 drm_mode_probed_add(connector, mode);
138 connector->display_info.width_mm = nl8048_mode.width_mm;
139 connector->display_info.height_mm = nl8048_mode.height_mm;
140 connector->display_info.bus_flags = DRM_BUS_FLAG_DE_HIGH
141 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE
142 | DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
144 return 1;
147 static const struct drm_panel_funcs nl8048_funcs = {
148 .disable = nl8048_disable,
149 .enable = nl8048_enable,
150 .get_modes = nl8048_get_modes,
153 static int __maybe_unused nl8048_suspend(struct device *dev)
155 struct nl8048_panel *lcd = dev_get_drvdata(dev);
157 nl8048_write(lcd, 2, 0x01);
158 msleep(40);
160 return 0;
163 static int __maybe_unused nl8048_resume(struct device *dev)
165 struct nl8048_panel *lcd = dev_get_drvdata(dev);
167 /* Reinitialize the panel. */
168 spi_setup(lcd->spi);
169 nl8048_write(lcd, 2, 0x00);
170 nl8048_init(lcd);
172 return 0;
175 static SIMPLE_DEV_PM_OPS(nl8048_pm_ops, nl8048_suspend, nl8048_resume);
177 static int nl8048_probe(struct spi_device *spi)
179 struct nl8048_panel *lcd;
180 int ret;
182 lcd = devm_kzalloc(&spi->dev, sizeof(*lcd), GFP_KERNEL);
183 if (!lcd)
184 return -ENOMEM;
186 spi_set_drvdata(spi, lcd);
187 lcd->spi = spi;
189 lcd->reset_gpio = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_LOW);
190 if (IS_ERR(lcd->reset_gpio)) {
191 dev_err(&spi->dev, "failed to parse reset gpio\n");
192 return PTR_ERR(lcd->reset_gpio);
195 spi->mode = SPI_MODE_0;
196 spi->bits_per_word = 32;
198 ret = spi_setup(spi);
199 if (ret < 0) {
200 dev_err(&spi->dev, "failed to setup SPI: %d\n", ret);
201 return ret;
204 ret = nl8048_init(lcd);
205 if (ret < 0)
206 return ret;
208 drm_panel_init(&lcd->panel, &lcd->spi->dev, &nl8048_funcs,
209 DRM_MODE_CONNECTOR_DPI);
211 return drm_panel_add(&lcd->panel);
214 static int nl8048_remove(struct spi_device *spi)
216 struct nl8048_panel *lcd = spi_get_drvdata(spi);
218 drm_panel_remove(&lcd->panel);
219 drm_panel_disable(&lcd->panel);
220 drm_panel_unprepare(&lcd->panel);
222 return 0;
225 static const struct of_device_id nl8048_of_match[] = {
226 { .compatible = "nec,nl8048hl11", },
227 { /* sentinel */ },
230 MODULE_DEVICE_TABLE(of, nl8048_of_match);
232 static const struct spi_device_id nl8048_ids[] = {
233 { "nl8048hl11", 0 },
234 { /* sentinel */ }
237 MODULE_DEVICE_TABLE(spi, nl8048_ids);
239 static struct spi_driver nl8048_driver = {
240 .probe = nl8048_probe,
241 .remove = nl8048_remove,
242 .id_table = nl8048_ids,
243 .driver = {
244 .name = "panel-nec-nl8048hl11",
245 .pm = &nl8048_pm_ops,
246 .of_match_table = nl8048_of_match,
250 module_spi_driver(nl8048_driver);
252 MODULE_AUTHOR("Erik Gilling <konkers@android.com>");
253 MODULE_DESCRIPTION("NEC-NL8048HL11 Driver");
254 MODULE_LICENSE("GPL");