blob: 5d8453fd79657898e6f51dc7b2c07cbd5df898c3 [file] [log] [blame]
Svyatoslav Ryhelecb12092023-12-23 21:50:31 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Sharp LQ101R1SX01 DSI panel driver
4 *
5 * Copyright (C) 2014 NVIDIA Corporation
6 * Copyright (c) 2023 Svyatoslav Ryhel <clamor95@gmail.com>
7 */
8
9#include <backlight.h>
10#include <dm.h>
11#include <panel.h>
12#include <log.h>
13#include <mipi_dsi.h>
14#include <linux/delay.h>
15#include <power/regulator.h>
16
17struct sharp_lq101r1sx01_priv {
18 struct udevice *backlight;
19 struct udevice *panel_sec;
20 struct udevice *vcc;
21};
22
23static struct display_timing default_timing = {
24 .pixelclock.typ = 278000000,
25 .hactive.typ = 2560,
26 .hfront_porch.typ = 128,
27 .hback_porch.typ = 64,
28 .hsync_len.typ = 64,
29 .vactive.typ = 1600,
30 .vfront_porch.typ = 4,
31 .vback_porch.typ = 8,
32 .vsync_len.typ = 32,
33};
34
35static int sharp_lq101r1sx01_write(struct mipi_dsi_device *dsi,
36 u16 offset, u8 value)
37{
38 u8 payload[3] = { offset >> 8, offset & 0xff, value };
39 int ret;
40
41 ret = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
42 if (ret < 0) {
43 log_debug("%s: failed to write %02x to %04x: %zd\n",
44 __func__, value, offset, ret);
45 return ret;
46 }
47
48 ret = mipi_dsi_dcs_nop(dsi);
49 if (ret < 0) {
50 log_debug("%s: failed to send DCS nop: %zd\n",
51 __func__, ret);
52 return ret;
53 }
54
55 udelay(20);
56
57 return 0;
58}
59
60static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
61 struct mipi_dsi_device *right,
62 struct display_timing *timing)
63{
64 int ret;
65
66 ret = mipi_dsi_dcs_set_column_address(left, 0,
67 timing->hactive.typ / 2 - 1);
68 if (ret < 0) {
69 log_debug("%s: failed to set column address: %d\n",
70 __func__, ret);
71 return ret;
72 }
73
74 ret = mipi_dsi_dcs_set_page_address(left, 0, timing->vactive.typ - 1);
75 if (ret < 0) {
76 log_debug("%s: failed to set page address: %d\n",
77 __func__, ret);
78 return ret;
79 }
80
81 ret = mipi_dsi_dcs_set_column_address(right, timing->hactive.typ / 2,
82 timing->hactive.typ - 1);
83 if (ret < 0) {
84 log_debug("%s: failed to set column address: %d\n",
85 __func__, ret);
86 return ret;
87 }
88
89 ret = mipi_dsi_dcs_set_page_address(right, 0, timing->vactive.typ - 1);
90 if (ret < 0) {
91 log_debug("%s: failed to set page address: %d\n",
92 __func__, ret);
93 return ret;
94 }
95
96 return 0;
97}
98
99static int sharp_lq101r1sx01_enable_backlight(struct udevice *dev)
100{
101 struct sharp_lq101r1sx01_priv *priv = dev_get_priv(dev);
102
103 if (!priv->panel_sec)
104 return 0;
105
106 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
107 struct mipi_dsi_panel_plat *plat_sec = dev_get_plat(priv->panel_sec);
108 struct mipi_dsi_device *link1 = plat->device;
109 struct mipi_dsi_device *link2 = plat_sec->device;
110 int ret;
111
112 ret = mipi_dsi_dcs_exit_sleep_mode(link1);
113 if (ret < 0) {
114 log_debug("%s: failed to exit sleep mode: %d\n",
115 __func__, ret);
116 return ret;
117 }
118
119 /* set left-right mode */
120 ret = sharp_lq101r1sx01_write(link1, 0x1000, 0x2a);
121 if (ret < 0) {
122 log_debug("%s: failed to set left-right mode: %d\n",
123 __func__, ret);
124 return ret;
125 }
126
127 /* enable command mode */
128 ret = sharp_lq101r1sx01_write(link1, 0x1001, 0x01);
129 if (ret < 0) {
130 log_debug("%s: failed to enable command mode: %d\n",
131 __func__, ret);
132 return ret;
133 }
134
135 ret = mipi_dsi_dcs_set_pixel_format(link1, MIPI_DCS_PIXEL_FMT_24BIT);
136 if (ret < 0) {
137 log_debug("%s: failed to set pixel format: %d\n",
138 __func__, ret);
139 return ret;
140 }
141
142 /*
143 * TODO: The device supports both left-right and even-odd split
144 * configurations, but this driver currently supports only the left-
145 * right split. To support a different mode a mechanism needs to be
146 * put in place to communicate the configuration back to the DSI host
147 * controller.
148 */
149 ret = sharp_setup_symmetrical_split(link1, link2, &default_timing);
150 if (ret < 0) {
151 log_debug("%s: failed to set up symmetrical split: %d\n",
152 __func__, ret);
153 return ret;
154 }
155
156 ret = mipi_dsi_dcs_set_display_on(link1);
157 if (ret < 0) {
158 log_debug("%s: failed to set panel on: %d\n",
159 __func__, ret);
160 return ret;
161 }
162 mdelay(20);
163
164 return 0;
165}
166
167static int sharp_lq101r1sx01_set_backlight(struct udevice *dev, int percent)
168{
169 struct sharp_lq101r1sx01_priv *priv = dev_get_priv(dev);
170 int ret;
171
172 if (!priv->panel_sec)
173 return 0;
174
175 ret = backlight_enable(priv->backlight);
176 if (ret)
177 return ret;
178
179 return backlight_set_brightness(priv->backlight, percent);
180}
181
182static int sharp_lq101r1sx01_timings(struct udevice *dev,
183 struct display_timing *timing)
184{
185 memcpy(timing, &default_timing, sizeof(*timing));
186 return 0;
187}
188
189static int sharp_lq101r1sx01_of_to_plat(struct udevice *dev)
190{
191 struct sharp_lq101r1sx01_priv *priv = dev_get_priv(dev);
192 int ret;
193
194 /* If node has no link2 it is secondary panel */
195 if (!dev_read_bool(dev, "link2"))
196 return 0;
197
198 ret = uclass_get_device_by_phandle(UCLASS_PANEL, dev,
199 "link2", &priv->panel_sec);
200 if (ret) {
201 log_debug("%s: cannot get secondary panel: ret = %d\n",
202 __func__, ret);
203 return ret;
204 }
205
206 ret = uclass_get_device_by_phandle(UCLASS_PANEL_BACKLIGHT, dev,
207 "backlight", &priv->backlight);
208 if (ret) {
209 log_debug("%s: cannot get backlight: ret = %d\n",
210 __func__, ret);
211 return ret;
212 }
213
214 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
215 "power-supply", &priv->vcc);
216 if (ret) {
217 log_debug("%s: cannot get power-supply: ret = %d\n",
218 __func__, ret);
219 return ret;
220 }
221
222 return 0;
223}
224
225static int sharp_lq101r1sx01_hw_init(struct udevice *dev)
226{
227 struct sharp_lq101r1sx01_priv *priv = dev_get_priv(dev);
228 int ret;
229
230 if (!priv->panel_sec)
231 return 0;
232
233 ret = regulator_set_enable_if_allowed(priv->vcc, 1);
234 if (ret) {
235 log_debug("%s: enabling power-supply failed (%d)\n",
236 __func__, ret);
237 return ret;
238 }
239
240 /*
241 * According to the datasheet, the panel needs around 10 ms to fully
242 * power up. At least another 120 ms is required before exiting sleep
243 * mode to make sure the panel is ready. Throw in another 20 ms for
244 * good measure.
245 */
246 mdelay(150);
247
248 return 0;
249}
250
251static int sharp_lq101r1sx01_probe(struct udevice *dev)
252{
253 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
254
255 /* fill characteristics of DSI data link */
256 plat->lanes = 4;
257 plat->format = MIPI_DSI_FMT_RGB888;
258
259 return sharp_lq101r1sx01_hw_init(dev);
260}
261
262static const struct panel_ops sharp_lq101r1sx01_ops = {
263 .enable_backlight = sharp_lq101r1sx01_enable_backlight,
264 .set_backlight = sharp_lq101r1sx01_set_backlight,
265 .get_display_timing = sharp_lq101r1sx01_timings,
266};
267
268static const struct udevice_id sharp_lq101r1sx01_ids[] = {
269 { .compatible = "sharp,lq101r1sx01" },
270 { }
271};
272
273U_BOOT_DRIVER(sharp_lq101r1sx01) = {
274 .name = "sharp_lq101r1sx01",
275 .id = UCLASS_PANEL,
276 .of_match = sharp_lq101r1sx01_ids,
277 .ops = &sharp_lq101r1sx01_ops,
278 .of_to_plat = sharp_lq101r1sx01_of_to_plat,
279 .probe = sharp_lq101r1sx01_probe,
280 .plat_auto = sizeof(struct mipi_dsi_panel_plat),
281 .priv_auto = sizeof(struct sharp_lq101r1sx01_priv),
282};