1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
6  * Linus Walleij <linus.walleij@linaro.org>
7  */
8 #include <drm/drm_mipi_dbi.h>
9 #include <drm/drm_modes.h>
10 #include <drm/drm_panel.h>
11 
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>
18 #include <linux/of.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/spi/spi.h>
21 
22 #include <video/mipi_display.h>
23 
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
45 
46 #define DATA_MASK	0x100
47 
48 /**
49  * struct db7430 - state container for a panel controlled by the DB7430
50  * controller
51  */
52 struct db7430 {
53 	/** @dev: the container device */
54 	struct device *dev;
55 	/** @dbi: the DBI bus abstraction handle */
56 	struct mipi_dbi dbi;
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];
63 };
64 
65 static const struct drm_display_mode db7430_480_800_mode = {
66 	/*
67 	 * 31 ns period min (htotal*vtotal*vrefresh)/1000
68 	 * gives a Vrefresh of ~71 Hz.
69 	 */
70 	.clock = 32258,
71 	.hdisplay = 480,
72 	.hsync_start = 480 + 10,
73 	.hsync_end = 480 + 10 + 4,
74 	.htotal = 480 + 10 + 4 + 40,
75 	.vdisplay = 800,
76 	.vsync_start = 800 + 6,
77 	.vsync_end = 800 + 6 + 1,
78 	.vtotal = 800 + 6 + 1 + 7,
79 	.width_mm = 53,
80 	.height_mm = 87,
81 	.flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC,
82 };
83 
84 static inline struct db7430 *to_db7430(struct drm_panel *panel)
85 {
86 	return container_of(panel, struct db7430, panel);
87 }
88 
89 static int db7430_power_on(struct db7430 *db)
90 {
91 	struct mipi_dbi *dbi = &db->dbi;
92 	int ret;
93 
94 	/* Power up */
95 	ret = regulator_bulk_enable(ARRAY_SIZE(db->regulators),
96 				    db->regulators);
97 	if (ret) {
98 		dev_err(db->dev, "failed to enable regulators: %d\n", ret);
99 		return ret;
100 	}
101 	msleep(50);
102 
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);
108 	/* Wait >= 10 ms */
109 	msleep(10);
110 	dev_dbg(db->dev, "de-asserted RESET\n");
111 
112 	/*
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".
117 	 */
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,
125 			 0x10, 0x01, 0x00);
126 	mipi_dbi_command(dbi, DB7430_DISPLAY_H_TIMING,
127 			 0x06, 0x55, 0x03, 0x07, 0x0b,
128 			 0x33, 0x00, 0x01, 0x03);
129 	/*
130 	 * 0x00 in datasheet 0x01 in vendor code 0x00, it seems 0x01 means
131 	 * DE active high and 0x00 means DE active low.
132 	 */
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);
160 
161 	return 0;
162 }
163 
164 static int db7430_power_off(struct db7430 *db)
165 {
166 	/* Go into RESET and disable regulators */
167 	gpiod_set_value_cansleep(db->reset, 1);
168 	return regulator_bulk_disable(ARRAY_SIZE(db->regulators),
169 				      db->regulators);
170 }
171 
172 static int db7430_unprepare(struct drm_panel *panel)
173 {
174 	return db7430_power_off(to_db7430(panel));
175 }
176 
177 static int db7430_disable(struct drm_panel *panel)
178 {
179 	struct db7430 *db = to_db7430(panel);
180 	struct mipi_dbi *dbi = &db->dbi;
181 
182 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
183 	msleep(25);
184 	mipi_dbi_command(dbi, MIPI_DCS_ENTER_SLEEP_MODE);
185 	msleep(120);
186 
187 	return 0;
188 }
189 
190 static int db7430_prepare(struct drm_panel *panel)
191 {
192 	return db7430_power_on(to_db7430(panel));
193 }
194 
195 static int db7430_enable(struct drm_panel *panel)
196 {
197 	struct db7430 *db = to_db7430(panel);
198 	struct mipi_dbi *dbi = &db->dbi;
199 
200 	/* Exit sleep mode */
201 	mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
202 	msleep(20);
203 
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);
208 	msleep(150);
209 
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);
215 
216 	/* Turn on display */
217 	mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
218 
219 	return 0;
220 }
221 
222 /**
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
226  */
227 static int db7430_get_modes(struct drm_panel *panel,
228 			    struct drm_connector *connector)
229 {
230 	struct db7430 *db = to_db7430(panel);
231 	struct drm_display_mode *mode;
232 	static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
233 
234 	mode = drm_mode_duplicate(connector->dev, &db7430_480_800_mode);
235 	if (!mode) {
236 		dev_err(db->dev, "failed to add mode\n");
237 		return -ENOMEM;
238 	}
239 
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,
246 					 &bus_format, 1);
247 
248 	drm_mode_set_name(mode);
249 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
250 
251 	drm_mode_probed_add(connector, mode);
252 
253 	return 1;
254 }
255 
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,
262 };
263 
264 static int db7430_probe(struct spi_device *spi)
265 {
266 	struct device *dev = &spi->dev;
267 	struct db7430 *db;
268 	int ret;
269 
270 	db = devm_kzalloc(dev, sizeof(*db), GFP_KERNEL);
271 	if (!db)
272 		return -ENOMEM;
273 	db->dev = dev;
274 
275 	/*
276 	 * VCI   is the analog voltage supply
277 	 * VCCIO is the digital I/O voltage supply
278 	 */
279 	db->regulators[0].supply = "vci";
280 	db->regulators[1].supply = "vccio";
281 	ret = devm_regulator_bulk_get(dev,
282 				      ARRAY_SIZE(db->regulators),
283 				      db->regulators);
284 	if (ret)
285 		return dev_err_probe(dev, ret, "failed to get regulators\n");
286 
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");
291 	}
292 
293 	ret = mipi_dbi_spi_init(spi, &db->dbi, NULL);
294 	if (ret)
295 		return dev_err_probe(dev, ret, "MIPI DBI init failed\n");
296 
297 	drm_panel_init(&db->panel, dev, &db7430_drm_funcs,
298 		       DRM_MODE_CONNECTOR_DPI);
299 
300 	/* FIXME: if no external backlight, use internal backlight */
301 	ret = drm_panel_of_backlight(&db->panel);
302 	if (ret)
303 		return dev_err_probe(dev, ret, "failed to add backlight\n");
304 
305 	spi_set_drvdata(spi, db);
306 
307 	drm_panel_add(&db->panel);
308 	dev_dbg(dev, "added panel\n");
309 
310 	return 0;
311 }
312 
313 static void db7430_remove(struct spi_device *spi)
314 {
315 	struct db7430 *db = spi_get_drvdata(spi);
316 
317 	drm_panel_remove(&db->panel);
318 }
319 
320 /*
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.
323  */
324 static const struct of_device_id db7430_match[] = {
325 	{ .compatible = "samsung,lms397kf04", },
326 	{},
327 };
328 MODULE_DEVICE_TABLE(of, db7430_match);
329 
330 static const struct spi_device_id db7430_ids[] = {
331 	{ "lms397kf04" },
332 	{ },
333 };
334 MODULE_DEVICE_TABLE(spi, db7430_ids);
335 
336 static struct spi_driver db7430_driver = {
337 	.probe		= db7430_probe,
338 	.remove		= db7430_remove,
339 	.id_table	= db7430_ids,
340 	.driver		= {
341 		.name	= "db7430-panel",
342 		.of_match_table = db7430_match,
343 	},
344 };
345 module_spi_driver(db7430_driver);
346 
347 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
348 MODULE_DESCRIPTION("Samsung DB7430 panel driver");
349 MODULE_LICENSE("GPL v2");
350