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