blob: cea20dcffa582511609c5e0df902643f708aa496 [file] [log] [blame]
Svyatoslav Ryhelec7ac372023-04-25 10:51:43 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2022 Svyatoslav Ryhel <clamor95@gmail.com>
4 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
9#include <log.h>
10#include <misc.h>
11#include <mipi_display.h>
12#include <mipi_dsi.h>
13#include <backlight.h>
14#include <panel.h>
15#include <spi.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <asm/gpio.h>
19
20#define SSD2825_DEVICE_ID_REG 0xB0
21#define SSD2825_RGB_INTERFACE_CTRL_REG_1 0xB1
22#define SSD2825_RGB_INTERFACE_CTRL_REG_2 0xB2
23#define SSD2825_RGB_INTERFACE_CTRL_REG_3 0xB3
24#define SSD2825_RGB_INTERFACE_CTRL_REG_4 0xB4
25#define SSD2825_RGB_INTERFACE_CTRL_REG_5 0xB5
26#define SSD2825_RGB_INTERFACE_CTRL_REG_6 0xB6
27#define SSD2825_NON_BURST BIT(2)
28#define SSD2825_BURST BIT(3)
29#define SSD2825_PCKL_HIGH BIT(13)
30#define SSD2825_HSYNC_HIGH BIT(14)
31#define SSD2825_VSYNC_HIGH BIT(15)
32#define SSD2825_CONFIGURATION_REG 0xB7
33#define SSD2825_CONF_REG_HS BIT(0)
34#define SSD2825_CONF_REG_CKE BIT(1)
35#define SSD2825_CONF_REG_SLP BIT(2)
36#define SSD2825_CONF_REG_VEN BIT(3)
37#define SSD2825_CONF_REG_HCLK BIT(4)
38#define SSD2825_CONF_REG_CSS BIT(5)
39#define SSD2825_CONF_REG_DCS BIT(6)
40#define SSD2825_CONF_REG_REN BIT(7)
41#define SSD2825_CONF_REG_ECD BIT(8)
42#define SSD2825_CONF_REG_EOT BIT(9)
43#define SSD2825_CONF_REG_LPE BIT(10)
44#define SSD2825_VC_CTRL_REG 0xB8
45#define SSD2825_PLL_CTRL_REG 0xB9
46#define SSD2825_PLL_CONFIGURATION_REG 0xBA
47#define SSD2825_CLOCK_CTRL_REG 0xBB
48#define SSD2825_PACKET_SIZE_CTRL_REG_1 0xBC
49#define SSD2825_PACKET_SIZE_CTRL_REG_2 0xBD
50#define SSD2825_PACKET_SIZE_CTRL_REG_3 0xBE
51#define SSD2825_PACKET_DROP_REG 0xBF
52#define SSD2825_OPERATION_CTRL_REG 0xC0
53#define SSD2825_MAX_RETURN_SIZE_REG 0xC1
54#define SSD2825_RETURN_DATA_COUNT_REG 0xC2
55#define SSD2825_ACK_RESPONSE_REG 0xC3
56#define SSD2825_LINE_CTRL_REG 0xC4
57#define SSD2825_INTERRUPT_CTRL_REG 0xC5
58#define SSD2825_INTERRUPT_STATUS_REG 0xC6
59#define SSD2825_ERROR_STATUS_REG 0xC7
60#define SSD2825_DATA_FORMAT_REG 0xC8
61#define SSD2825_DELAY_ADJ_REG_1 0xC9
62#define SSD2825_DELAY_ADJ_REG_2 0xCA
63#define SSD2825_DELAY_ADJ_REG_3 0xCB
64#define SSD2825_DELAY_ADJ_REG_4 0xCC
65#define SSD2825_DELAY_ADJ_REG_5 0xCD
66#define SSD2825_DELAY_ADJ_REG_6 0xCE
67#define SSD2825_HS_TX_TIMER_REG_1 0xCF
68#define SSD2825_HS_TX_TIMER_REG_2 0xD0
69#define SSD2825_LP_RX_TIMER_REG_1 0xD1
70#define SSD2825_LP_RX_TIMER_REG_2 0xD2
71#define SSD2825_TE_STATUS_REG 0xD3
72#define SSD2825_SPI_READ_REG 0xD4
73#define SSD2825_PLL_LOCK_REG 0xD5
74#define SSD2825_TEST_REG 0xD6
75#define SSD2825_TE_COUNT_REG 0xD7
76#define SSD2825_ANALOG_CTRL_REG_1 0xD8
77#define SSD2825_ANALOG_CTRL_REG_2 0xD9
78#define SSD2825_ANALOG_CTRL_REG_3 0xDA
79#define SSD2825_ANALOG_CTRL_REG_4 0xDB
80#define SSD2825_INTERRUPT_OUT_CTRL_REG 0xDC
81#define SSD2825_RGB_INTERFACE_CTRL_REG_7 0xDD
82#define SSD2825_LANE_CONFIGURATION_REG 0xDE
83#define SSD2825_DELAY_ADJ_REG_7 0xDF
84#define SSD2825_INPUT_PIN_CTRL_REG_1 0xE0
85#define SSD2825_INPUT_PIN_CTRL_REG_2 0xE1
86#define SSD2825_BIDIR_PIN_CTRL_REG_1 0xE2
87#define SSD2825_BIDIR_PIN_CTRL_REG_2 0xE3
88#define SSD2825_BIDIR_PIN_CTRL_REG_3 0xE4
89#define SSD2825_BIDIR_PIN_CTRL_REG_4 0xE5
90#define SSD2825_BIDIR_PIN_CTRL_REG_5 0xE6
91#define SSD2825_BIDIR_PIN_CTRL_REG_6 0xE7
92#define SSD2825_BIDIR_PIN_CTRL_REG_7 0xE8
93#define SSD2825_CABC_BRIGHTNESS_CTRL_REG_1 0xE9
94#define SSD2825_CABC_BRIGHTNESS_CTRL_REG_2 0xEA
95#define SSD2825_CABC_BRIGHTNESS_STATUS_REG 0xEB
96#define SSD2825_READ_REG 0xFF
97#define SSD2825_SPI_READ_REG_RESET 0xFA
98
99#define SSD2825_CMD_MASK 0x00
100#define SSD2825_DAT_MASK 0x01
101
102#define SSD2825_CMD_SEND BIT(0)
103#define SSD2825_DAT_SEND BIT(1)
104#define SSD2825_DSI_SEND BIT(2)
105
106#define SSD2828_LP_CLOCK_DIVIDER(n) (((n) - 1) & 0x3F)
107#define SSD2825_LP_MIN_CLK 5000 /* KHz */
108#define SSD2825_REF_MIN_CLK 2000 /* KHz */
109
110struct ssd2825_bridge_priv {
111 struct mipi_dsi_host host;
112 struct mipi_dsi_device device;
113
114 struct udevice *panel;
115 struct display_timing timing;
116
117 struct gpio_desc power_gpio;
118 struct gpio_desc reset_gpio;
119
120 struct clk *tx_clk;
121
122 u32 pll_freq_kbps; /* PLL in kbps */
123};
124
125static int ssd2825_spi_write(struct udevice *dev, int reg,
126 const void *buf, int flags)
127{
128 u8 command[2];
129
130 if (flags & SSD2825_CMD_SEND) {
131 command[0] = SSD2825_CMD_MASK;
132 command[1] = reg;
133 dm_spi_xfer(dev, 9, &command,
134 NULL, SPI_XFER_ONCE);
135 }
136
137 if (flags & SSD2825_DAT_SEND) {
138 u16 data = *(u16 *)buf;
139 u8 cmd1, cmd2;
140
141 /* send low byte first and then high byte */
142 cmd1 = (data & 0x00FF);
143 cmd2 = (data & 0xFF00) >> 8;
144
145 command[0] = SSD2825_DAT_MASK;
146 command[1] = cmd1;
147 dm_spi_xfer(dev, 9, &command,
148 NULL, SPI_XFER_ONCE);
149
150 command[0] = SSD2825_DAT_MASK;
151 command[1] = cmd2;
152 dm_spi_xfer(dev, 9, &command,
153 NULL, SPI_XFER_ONCE);
154 }
155
156 if (flags & SSD2825_DSI_SEND) {
157 u16 data = *(u16 *)buf;
158 data &= 0x00FF;
159
160 debug("%s: dsi command (0x%x)\n",
161 __func__, data);
162
163 command[0] = SSD2825_DAT_MASK;
164 command[1] = data;
165 dm_spi_xfer(dev, 9, &command,
166 NULL, SPI_XFER_ONCE);
167 }
168
169 return 0;
170}
171
172static int ssd2825_spi_read(struct udevice *dev, int reg,
173 void *data, int flags)
174{
175 u8 command[2];
176
177 command[0] = SSD2825_CMD_MASK;
178 command[1] = SSD2825_SPI_READ_REG;
179 dm_spi_xfer(dev, 9, &command,
180 NULL, SPI_XFER_ONCE);
181
182 command[0] = SSD2825_DAT_MASK;
183 command[1] = SSD2825_SPI_READ_REG_RESET;
184 dm_spi_xfer(dev, 9, &command,
185 NULL, SPI_XFER_ONCE);
186
187 command[0] = SSD2825_DAT_MASK;
188 command[1] = 0;
189 dm_spi_xfer(dev, 9, &command,
190 NULL, SPI_XFER_ONCE);
191
192 command[0] = SSD2825_CMD_MASK;
193 command[1] = reg;
194 dm_spi_xfer(dev, 9, &command,
195 NULL, SPI_XFER_ONCE);
196
197 command[0] = SSD2825_CMD_MASK;
198 command[1] = SSD2825_SPI_READ_REG_RESET;
199 dm_spi_xfer(dev, 9, &command,
200 NULL, SPI_XFER_ONCE);
201
202 dm_spi_xfer(dev, 16, NULL,
203 (u8 *)data, SPI_XFER_ONCE);
204
205 return 0;
206}
207
208static void ssd2825_write_register(struct udevice *dev, u8 reg,
209 u16 command)
210{
211 ssd2825_spi_write(dev, reg, &command,
212 SSD2825_CMD_SEND |
213 SSD2825_DAT_SEND);
214}
215
216static void ssd2825_write_dsi(struct udevice *dev, const u8 *command,
217 int len)
218{
219 int i;
220
221 ssd2825_spi_write(dev, SSD2825_PACKET_SIZE_CTRL_REG_1, &len,
222 SSD2825_CMD_SEND | SSD2825_DAT_SEND);
223
224 ssd2825_spi_write(dev, SSD2825_PACKET_DROP_REG, NULL,
225 SSD2825_CMD_SEND);
226
227 for (i = 0; i < len; i++)
228 ssd2825_spi_write(dev, 0, &command[i], SSD2825_DSI_SEND);
229}
230
231static ssize_t ssd2825_bridge_transfer(struct mipi_dsi_host *host,
232 const struct mipi_dsi_msg *msg)
233{
234 struct udevice *dev = (struct udevice *)host->dev;
235 u8 buf = *(u8 *)msg->tx_buf;
236 u16 config;
237 int ret;
238
239 ret = ssd2825_spi_read(dev, SSD2825_CONFIGURATION_REG,
240 &config, 0);
241 if (ret)
242 return ret;
243
244 switch (msg->type) {
245 case MIPI_DSI_DCS_SHORT_WRITE:
246 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
247 case MIPI_DSI_DCS_LONG_WRITE:
248 config |= SSD2825_CONF_REG_DCS;
249 break;
250 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
251 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
252 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
253 case MIPI_DSI_GENERIC_LONG_WRITE:
254 config &= ~SSD2825_CONF_REG_DCS;
255 break;
256 default:
257 return 0;
258 }
259
260 ssd2825_write_register(dev, SSD2825_CONFIGURATION_REG, config);
261 ssd2825_write_register(dev, SSD2825_VC_CTRL_REG, 0x0000);
262 ssd2825_write_dsi(dev, msg->tx_buf, msg->tx_len);
263
264 if (buf == MIPI_DCS_SET_DISPLAY_ON) {
265 ssd2825_write_register(dev, SSD2825_CONFIGURATION_REG,
266 SSD2825_CONF_REG_HS | SSD2825_CONF_REG_VEN |
267 SSD2825_CONF_REG_DCS | SSD2825_CONF_REG_ECD |
268 SSD2825_CONF_REG_EOT);
269 ssd2825_write_register(dev, SSD2825_PLL_CTRL_REG, 0x0001);
270 ssd2825_write_register(dev, SSD2825_VC_CTRL_REG, 0x0000);
271 }
272
273 return 0;
274}
275
276static const struct mipi_dsi_host_ops ssd2825_bridge_host_ops = {
277 .transfer = ssd2825_bridge_transfer,
278};
279
280/*
281 * PLL configuration register settings.
282 *
283 * See the "PLL Configuration Register Description" in the SSD2825 datasheet.
284 */
285static u16 construct_pll_config(struct ssd2825_bridge_priv *priv,
286 u32 desired_pll_freq_kbps, u32 reference_freq_khz)
287{
288 u32 div_factor = 1, mul_factor, fr = 0;
289
290 while (reference_freq_khz / (div_factor + 1) >= SSD2825_REF_MIN_CLK)
291 div_factor++;
292 if (div_factor > 31)
293 div_factor = 31;
294
295 mul_factor = DIV_ROUND_UP(desired_pll_freq_kbps * div_factor,
296 reference_freq_khz);
297
298 priv->pll_freq_kbps = reference_freq_khz * mul_factor / div_factor;
299
300 if (priv->pll_freq_kbps >= 501000)
301 fr = 3;
302 else if (priv->pll_freq_kbps >= 251000)
303 fr = 2;
304 else if (priv->pll_freq_kbps >= 126000)
305 fr = 1;
306
307 return (fr << 14) | (div_factor << 8) | mul_factor;
308}
309
310static void ssd2825_setup_pll(struct udevice *dev)
311{
312 struct ssd2825_bridge_priv *priv = dev_get_priv(dev);
313 struct mipi_dsi_device *device = &priv->device;
314 struct display_timing *dt = &priv->timing;
315 u16 pll_config, lp_div;
316 u32 pclk_mult, tx_freq_khz, pd_lines;
317
318 tx_freq_khz = clk_get_rate(priv->tx_clk) / 1000;
319 pd_lines = mipi_dsi_pixel_format_to_bpp(device->format);
320 pclk_mult = pd_lines / device->lanes + 1;
321
322 pll_config = construct_pll_config(priv, pclk_mult *
323 dt->pixelclock.typ / 1000,
324 tx_freq_khz);
325
326 lp_div = priv->pll_freq_kbps / (SSD2825_LP_MIN_CLK * 8);
327
328 /* Disable PLL */
329 ssd2825_write_register(dev, SSD2825_PLL_CTRL_REG, 0x0000);
330 ssd2825_write_register(dev, SSD2825_LINE_CTRL_REG, 0x0001);
331
332 /* Set delays */
333 ssd2825_write_register(dev, SSD2825_DELAY_ADJ_REG_1, 0x2103);
334
335 /* Set PLL coeficients */
336 ssd2825_write_register(dev, SSD2825_PLL_CONFIGURATION_REG, pll_config);
337
338 /* Clock Control Register */
339 ssd2825_write_register(dev, SSD2825_CLOCK_CTRL_REG,
340 SSD2828_LP_CLOCK_DIVIDER(lp_div));
341
342 /* Enable PLL */
343 ssd2825_write_register(dev, SSD2825_PLL_CTRL_REG, 0x0001);
344 ssd2825_write_register(dev, SSD2825_VC_CTRL_REG, 0x0000);
345}
346
347static int ssd2825_bridge_enable_panel(struct udevice *dev)
348{
349 struct ssd2825_bridge_priv *priv = dev_get_priv(dev);
350 struct mipi_dsi_device *device = &priv->device;
351 struct display_timing *dt = &priv->timing;
352 int ret;
353
354 ret = clk_prepare_enable(priv->tx_clk);
355 if (ret) {
356 log_err("error enabling tx_clk (%d)\n", ret);
357 return ret;
358 }
359
360 ret = dm_gpio_set_value(&priv->power_gpio, 1);
361 if (ret) {
362 log_err("error changing power-gpios (%d)\n", ret);
363 return ret;
364 }
365 mdelay(10);
366
367 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
368 if (ret) {
369 log_err("error changing reset-gpios (%d)\n", ret);
370 return ret;
371 }
372 mdelay(10);
373
374 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
375 if (ret) {
376 log_err("error changing reset-gpios (%d)\n", ret);
377 return ret;
378 }
379 mdelay(10);
380
381 /* Perform panel HW setup */
382 ret = panel_enable_backlight(priv->panel);
383 if (ret)
384 return ret;
385
386 /* Perform SW reset */
387 ssd2825_write_register(dev, SSD2825_OPERATION_CTRL_REG, 0x0100);
388
389 /* Set panel timings */
390 ssd2825_write_register(dev, SSD2825_RGB_INTERFACE_CTRL_REG_1,
391 dt->vsync_len.typ << 8 | dt->hsync_len.typ);
392 ssd2825_write_register(dev, SSD2825_RGB_INTERFACE_CTRL_REG_2,
393 (dt->vsync_len.typ + dt->vback_porch.typ) << 8 |
394 (dt->hsync_len.typ + dt->hback_porch.typ));
395 ssd2825_write_register(dev, SSD2825_RGB_INTERFACE_CTRL_REG_3,
396 dt->vfront_porch.typ << 8 | dt->hfront_porch.typ);
397 ssd2825_write_register(dev, SSD2825_RGB_INTERFACE_CTRL_REG_4,
398 dt->hactive.typ);
399 ssd2825_write_register(dev, SSD2825_RGB_INTERFACE_CTRL_REG_5,
400 dt->vactive.typ);
401 ssd2825_write_register(dev, SSD2825_RGB_INTERFACE_CTRL_REG_6,
402 SSD2825_HSYNC_HIGH | SSD2825_VSYNC_HIGH |
403 SSD2825_PCKL_HIGH | SSD2825_NON_BURST |
404 (3 - device->format));
405 ssd2825_write_register(dev, SSD2825_LANE_CONFIGURATION_REG,
406 device->lanes - 1);
407 ssd2825_write_register(dev, SSD2825_TEST_REG, 0x0004);
408
409 /* Call PLL configuration */
410 ssd2825_setup_pll(dev);
411
412 mdelay(10);
413
414 /* Initial DSI configuration register set */
415 ssd2825_write_register(dev, SSD2825_CONFIGURATION_REG,
416 SSD2825_CONF_REG_CKE | SSD2825_CONF_REG_DCS |
417 SSD2825_CONF_REG_ECD | SSD2825_CONF_REG_EOT);
418 ssd2825_write_register(dev, SSD2825_VC_CTRL_REG, 0x0000);
419
420 /* Set up SW panel configuration */
421 ret = panel_set_backlight(priv->panel, BACKLIGHT_DEFAULT);
422 if (ret)
423 return ret;
424
425 return 0;
426}
427
428static int ssd2825_bridge_set_panel(struct udevice *dev, int percent)
429{
430 return 0;
431}
432
433static int ssd2825_bridge_panel_timings(struct udevice *dev,
434 struct display_timing *timing)
435{
436 struct ssd2825_bridge_priv *priv = dev_get_priv(dev);
437
438 memcpy(timing, &priv->timing, sizeof(*timing));
439
440 return 0;
441}
442
443static int ssd2825_bridge_probe(struct udevice *dev)
444{
445 struct ssd2825_bridge_priv *priv = dev_get_priv(dev);
446 struct spi_slave *slave = dev_get_parent_priv(dev);
447 struct mipi_dsi_device *device = &priv->device;
448 struct mipi_dsi_panel_plat *mipi_plat;
449 int ret;
450
451 ret = spi_claim_bus(slave);
452 if (ret) {
453 log_err("SPI bus allocation failed (%d)\n", ret);
454 return ret;
455 }
456
457 ret = uclass_get_device_by_phandle(UCLASS_PANEL, dev,
458 "panel", &priv->panel);
459 if (ret) {
460 log_err("cannot get panel: ret=%d\n", ret);
461 return ret;
462 }
463
464 panel_get_display_timing(priv->panel, &priv->timing);
465
466 mipi_plat = dev_get_plat(priv->panel);
467 mipi_plat->device = device;
468
469 priv->host.dev = (struct device *)dev;
470 priv->host.ops = &ssd2825_bridge_host_ops;
471
472 device->host = &priv->host;
473 device->lanes = mipi_plat->lanes;
474 device->format = mipi_plat->format;
475 device->mode_flags = mipi_plat->mode_flags;
476
477 /* get panel gpios */
478 ret = gpio_request_by_name(dev, "power-gpios", 0,
479 &priv->power_gpio, GPIOD_IS_OUT);
480 if (ret) {
481 log_err("could not decode power-gpios (%d)\n", ret);
482 return ret;
483 }
484
485 ret = gpio_request_by_name(dev, "reset-gpios", 0,
486 &priv->reset_gpio, GPIOD_IS_OUT);
487 if (ret) {
488 log_err("could not decode reset-gpios (%d)\n", ret);
489 return ret;
490 }
491
492 /* get clk */
493 priv->tx_clk = devm_clk_get(dev, "tx_clk");
494 if (IS_ERR(priv->tx_clk)) {
495 log_err("cannot get tx_clk: %ld\n", PTR_ERR(priv->tx_clk));
496 return PTR_ERR(priv->tx_clk);
497 }
498
499 return 0;
500}
501
502static const struct panel_ops ssd2825_bridge_ops = {
503 .enable_backlight = ssd2825_bridge_enable_panel,
504 .set_backlight = ssd2825_bridge_set_panel,
505 .get_display_timing = ssd2825_bridge_panel_timings,
506};
507
508static const struct udevice_id ssd2825_bridge_ids[] = {
509 { .compatible = "solomon,ssd2825" },
510 { }
511};
512
513U_BOOT_DRIVER(ssd2825) = {
514 .name = "ssd2825",
515 .id = UCLASS_PANEL,
516 .of_match = ssd2825_bridge_ids,
517 .ops = &ssd2825_bridge_ops,
518 .probe = ssd2825_bridge_probe,
519 .priv_auto = sizeof(struct ssd2825_bridge_priv),
520};