1 // SPDX-License-Identifier: GPL-2.0
3 * Panel driver for the Samsung LMS397KF04 480x800 DPI RGB panel.
4 * According to the data sheet the display controller is called DB7430.
5 * Found in the Samsung Galaxy Beam GT-I8350 mobile phone.
8 #include <drm/drm_mipi_dbi.h>
9 #include <drm/drm_modes.h>
10 #include <drm/drm_panel.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/media-bus-format.h>
17 #include <linux/module.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/spi/spi.h>
22 #include <video/mipi_display.h>
24 #define DB7430_ACCESS_PROT_OFF 0xb0
25 #define DB7430_UNKNOWN_B4 0xb4
26 #define DB7430_USER_SELECT 0xb5
27 #define DB7430_UNKNOWN_B7 0xb7
28 #define DB7430_UNKNOWN_B8 0xb8
29 #define DB7430_PANEL_DRIVING 0xc0
30 #define DB7430_SOURCE_CONTROL 0xc1
31 #define DB7430_GATE_INTERFACE 0xc4
32 #define DB7430_DISPLAY_H_TIMING 0xc5
33 #define DB7430_RGB_SYNC_OPTION 0xc6
34 #define DB7430_GAMMA_SET_RED 0xc8
35 #define DB7430_GAMMA_SET_GREEN 0xc9
36 #define DB7430_GAMMA_SET_BLUE 0xca
37 #define DB7430_BIAS_CURRENT_CTRL 0xd1
38 #define DB7430_DDV_CTRL 0xd2
39 #define DB7430_GAMMA_CTRL_REF 0xd3
40 #define DB7430_UNKNOWN_D4 0xd4
41 #define DB7430_DCDC_CTRL 0xd5
42 #define DB7430_VCL_CTRL 0xd6
43 #define DB7430_UNKNOWN_F8 0xf8
44 #define DB7430_UNKNOWN_FC 0xfc
46 #define DATA_MASK 0x100
49 * struct db7430 - state container for a panel controlled by the DB7430
53 /** @dev: the container device */
55 /** @dbi: the DBI bus abstraction handle */
57 /** @panel: the DRM panel instance for this device */
58 struct drm_panel panel;
59 /** @reset: reset GPIO line */
60 struct gpio_desc *reset;
61 /** @regulators: VCCIO and VIO supply regulators */
62 struct regulator_bulk_data regulators[2];
65 static const struct drm_display_mode db7430_480_800_mode = {
67 * 31 ns period min (htotal*vtotal*vrefresh)/1000
68 * gives a Vrefresh of ~71 Hz.
72 .hsync_start = 480 + 10,
73 .hsync_end = 480 + 10 + 4,
74 .htotal = 480 + 10 + 4 + 40,
76 .vsync_start = 800 + 6,
77 .vsync_end = 800 + 6 + 1,
78 .vtotal = 800 + 6 + 1 + 7,
81 .flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC,
84 static inline struct db7430 *to_db7430(struct drm_panel *panel)
86 return container_of(panel, struct db7430, panel);
89 static int db7430_power_on(struct db7430 *db)
91 struct mipi_dbi *dbi = &db->dbi;
95 ret = regulator_bulk_enable(ARRAY_SIZE(db->regulators),
98 dev_err(db->dev, "failed to enable regulators: %d\n", ret);
103 /* Assert reset >=1 ms */
104 gpiod_set_value_cansleep(db->reset, 1);
105 usleep_range(1000, 5000);
106 /* De-assert reset */
107 gpiod_set_value_cansleep(db->reset, 0);
110 dev_dbg(db->dev, "de-asserted RESET\n");
113 * This is set to 0x0a (RGB/BGR order + horizontal flip) in order
114 * to make the display behave normally. If this is not set the displays
115 * normal output behaviour is horizontally flipped and BGR ordered. Do
116 * it twice because the first message doesn't always "take".
118 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, 0x0a);
119 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, 0x0a);
120 mipi_dbi_command(dbi, DB7430_ACCESS_PROT_OFF, 0x00);
121 mipi_dbi_command(dbi, DB7430_PANEL_DRIVING, 0x28, 0x08);
122 mipi_dbi_command(dbi, DB7430_SOURCE_CONTROL,
123 0x01, 0x30, 0x15, 0x05, 0x22);
124 mipi_dbi_command(dbi, DB7430_GATE_INTERFACE,
126 mipi_dbi_command(dbi, DB7430_DISPLAY_H_TIMING,
127 0x06, 0x55, 0x03, 0x07, 0x0b,
128 0x33, 0x00, 0x01, 0x03);
130 * 0x00 in datasheet 0x01 in vendor code 0x00, it seems 0x01 means
131 * DE active high and 0x00 means DE active low.
133 mipi_dbi_command(dbi, DB7430_RGB_SYNC_OPTION, 0x01);
134 mipi_dbi_command(dbi, DB7430_GAMMA_SET_RED,
135 /* R positive gamma */ 0x00,
136 0x0A, 0x31, 0x3B, 0x4E, 0x58, 0x59, 0x5B, 0x58, 0x5E, 0x62,
137 0x60, 0x61, 0x5E, 0x62, 0x55, 0x55, 0x7F, 0x08,
138 /* R negative gamma */ 0x00,
139 0x0A, 0x31, 0x3B, 0x4E, 0x58, 0x59, 0x5B, 0x58, 0x5E, 0x62,
140 0x60, 0x61, 0x5E, 0x62, 0x55, 0x55, 0x7F, 0x08);
141 mipi_dbi_command(dbi, DB7430_GAMMA_SET_GREEN,
142 /* G positive gamma */ 0x00,
143 0x25, 0x15, 0x28, 0x3D, 0x4A, 0x48, 0x4C, 0x4A, 0x52, 0x59,
144 0x59, 0x5B, 0x56, 0x60, 0x5D, 0x55, 0x7F, 0x0A,
145 /* G negative gamma */ 0x00,
146 0x25, 0x15, 0x28, 0x3D, 0x4A, 0x48, 0x4C, 0x4A, 0x52, 0x59,
147 0x59, 0x5B, 0x56, 0x60, 0x5D, 0x55, 0x7F, 0x0A);
148 mipi_dbi_command(dbi, DB7430_GAMMA_SET_BLUE,
149 /* B positive gamma */ 0x00,
150 0x48, 0x10, 0x1F, 0x2F, 0x35, 0x38, 0x3D, 0x3C, 0x45, 0x4D,
151 0x4E, 0x52, 0x51, 0x60, 0x7F, 0x7E, 0x7F, 0x0C,
152 /* B negative gamma */ 0x00,
153 0x48, 0x10, 0x1F, 0x2F, 0x35, 0x38, 0x3D, 0x3C, 0x45, 0x4D,
154 0x4E, 0x52, 0x51, 0x60, 0x7F, 0x7E, 0x7F, 0x0C);
155 mipi_dbi_command(dbi, DB7430_BIAS_CURRENT_CTRL, 0x33, 0x13);
156 mipi_dbi_command(dbi, DB7430_DDV_CTRL, 0x11, 0x00, 0x00);
157 mipi_dbi_command(dbi, DB7430_GAMMA_CTRL_REF, 0x50, 0x50);
158 mipi_dbi_command(dbi, DB7430_DCDC_CTRL, 0x2f, 0x11, 0x1e, 0x46);
159 mipi_dbi_command(dbi, DB7430_VCL_CTRL, 0x11, 0x0a);
164 static int db7430_power_off(struct db7430 *db)
166 /* Go into RESET and disable regulators */
167 gpiod_set_value_cansleep(db->reset, 1);
168 return regulator_bulk_disable(ARRAY_SIZE(db->regulators),
172 static int db7430_unprepare(struct drm_panel *panel)
174 return db7430_power_off(to_db7430(panel));
177 static int db7430_disable(struct drm_panel *panel)
179 struct db7430 *db = to_db7430(panel);
180 struct mipi_dbi *dbi = &db->dbi;
182 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
184 mipi_dbi_command(dbi, MIPI_DCS_ENTER_SLEEP_MODE);
190 static int db7430_prepare(struct drm_panel *panel)
192 return db7430_power_on(to_db7430(panel));
195 static int db7430_enable(struct drm_panel *panel)
197 struct db7430 *db = to_db7430(panel);
198 struct mipi_dbi *dbi = &db->dbi;
200 /* Exit sleep mode */
201 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
204 /* NVM (non-volatile memory) load sequence */
205 mipi_dbi_command(dbi, DB7430_UNKNOWN_D4, 0x52, 0x5e);
206 mipi_dbi_command(dbi, DB7430_UNKNOWN_F8, 0x01, 0xf5, 0xf2, 0x71, 0x44);
207 mipi_dbi_command(dbi, DB7430_UNKNOWN_FC, 0x00, 0x08);
210 /* CABC turn on sequence (BC = backlight control) */
211 mipi_dbi_command(dbi, DB7430_UNKNOWN_B4, 0x0f, 0x00, 0x50);
212 mipi_dbi_command(dbi, DB7430_USER_SELECT, 0x80);
213 mipi_dbi_command(dbi, DB7430_UNKNOWN_B7, 0x24);
214 mipi_dbi_command(dbi, DB7430_UNKNOWN_B8, 0x01);
216 /* Turn on display */
217 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
223 * db7430_get_modes() - return the mode
224 * @panel: the panel to get the mode for
225 * @connector: reference to the central DRM connector control structure
227 static int db7430_get_modes(struct drm_panel *panel,
228 struct drm_connector *connector)
230 struct db7430 *db = to_db7430(panel);
231 struct drm_display_mode *mode;
232 static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
234 mode = drm_mode_duplicate(connector->dev, &db7430_480_800_mode);
236 dev_err(db->dev, "failed to add mode\n");
240 connector->display_info.bpc = 8;
241 connector->display_info.width_mm = mode->width_mm;
242 connector->display_info.height_mm = mode->height_mm;
243 connector->display_info.bus_flags =
244 DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
245 drm_display_info_set_bus_formats(&connector->display_info,
248 drm_mode_set_name(mode);
249 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
251 drm_mode_probed_add(connector, mode);
256 static const struct drm_panel_funcs db7430_drm_funcs = {
257 .disable = db7430_disable,
258 .unprepare = db7430_unprepare,
259 .prepare = db7430_prepare,
260 .enable = db7430_enable,
261 .get_modes = db7430_get_modes,
264 static int db7430_probe(struct spi_device *spi)
266 struct device *dev = &spi->dev;
270 db = devm_kzalloc(dev, sizeof(*db), GFP_KERNEL);
276 * VCI is the analog voltage supply
277 * VCCIO is the digital I/O voltage supply
279 db->regulators[0].supply = "vci";
280 db->regulators[1].supply = "vccio";
281 ret = devm_regulator_bulk_get(dev,
282 ARRAY_SIZE(db->regulators),
285 return dev_err_probe(dev, ret, "failed to get regulators\n");
287 db->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
288 if (IS_ERR(db->reset)) {
289 ret = PTR_ERR(db->reset);
290 return dev_err_probe(dev, ret, "no RESET GPIO\n");
293 ret = mipi_dbi_spi_init(spi, &db->dbi, NULL);
295 return dev_err_probe(dev, ret, "MIPI DBI init failed\n");
297 drm_panel_init(&db->panel, dev, &db7430_drm_funcs,
298 DRM_MODE_CONNECTOR_DPI);
300 /* FIXME: if no external backlight, use internal backlight */
301 ret = drm_panel_of_backlight(&db->panel);
303 return dev_err_probe(dev, ret, "failed to add backlight\n");
305 spi_set_drvdata(spi, db);
307 drm_panel_add(&db->panel);
308 dev_dbg(dev, "added panel\n");
313 static void db7430_remove(struct spi_device *spi)
315 struct db7430 *db = spi_get_drvdata(spi);
317 drm_panel_remove(&db->panel);
321 * The DB7430 display controller may be used in several display products,
322 * so list the different variants here and add per-variant data if needed.
324 static const struct of_device_id db7430_match[] = {
325 { .compatible = "samsung,lms397kf04", },
328 MODULE_DEVICE_TABLE(of, db7430_match);
330 static const struct spi_device_id db7430_ids[] = {
334 MODULE_DEVICE_TABLE(spi, db7430_ids);
336 static struct spi_driver db7430_driver = {
337 .probe = db7430_probe,
338 .remove = db7430_remove,
339 .id_table = db7430_ids,
341 .name = "db7430-panel",
342 .of_match_table = db7430_match,
345 module_spi_driver(db7430_driver);
348 MODULE_DESCRIPTION("Samsung DB7430 panel driver");
349 MODULE_LICENSE("GPL v2");