1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021, Linaro Limited
4  *
5  * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree:
6  *   Copyright (c) 2013, The Linux Foundation. All rights reserved.
7  */
8 
9 #include <linux/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_modes.h>
18 #include <drm/drm_panel.h>
19 
20 struct truly_nt35521 {
21 	struct drm_panel panel;
22 	struct mipi_dsi_device *dsi;
23 	struct regulator_bulk_data supplies[2];
24 	struct gpio_desc *reset_gpio;
25 	struct gpio_desc *blen_gpio;
26 	bool prepared;
27 	bool enabled;
28 };
29 
30 static inline
31 struct truly_nt35521 *to_truly_nt35521(struct drm_panel *panel)
32 {
33 	return container_of(panel, struct truly_nt35521, panel);
34 }
35 
36 static void truly_nt35521_reset(struct truly_nt35521 *ctx)
37 {
38 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
39 	usleep_range(1000, 2000);
40 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
41 	usleep_range(10000, 11000);
42 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
43 	msleep(150);
44 }
45 
46 static int truly_nt35521_on(struct truly_nt35521 *ctx)
47 {
48 	struct mipi_dsi_device *dsi = ctx->dsi;
49 	struct device *dev = &dsi->dev;
50 	int ret;
51 
52 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
53 
54 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x00);
55 	mipi_dsi_generic_write_seq(dsi, 0xff, 0xaa, 0x55, 0xa5, 0x80);
56 	mipi_dsi_generic_write_seq(dsi, 0x6f, 0x11, 0x00);
57 	mipi_dsi_generic_write_seq(dsi, 0xf7, 0x20, 0x00);
58 	mipi_dsi_generic_write_seq(dsi, 0x6f, 0x01);
59 	mipi_dsi_generic_write_seq(dsi, 0xb1, 0x21);
60 	mipi_dsi_generic_write_seq(dsi, 0xbd, 0x01, 0xa0, 0x10, 0x08, 0x01);
61 	mipi_dsi_generic_write_seq(dsi, 0xb8, 0x01, 0x02, 0x0c, 0x02);
62 	mipi_dsi_generic_write_seq(dsi, 0xbb, 0x11, 0x11);
63 	mipi_dsi_generic_write_seq(dsi, 0xbc, 0x00, 0x00);
64 	mipi_dsi_generic_write_seq(dsi, 0xb6, 0x02);
65 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x01);
66 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x09, 0x09);
67 	mipi_dsi_generic_write_seq(dsi, 0xb1, 0x09, 0x09);
68 	mipi_dsi_generic_write_seq(dsi, 0xbc, 0x8c, 0x00);
69 	mipi_dsi_generic_write_seq(dsi, 0xbd, 0x8c, 0x00);
70 	mipi_dsi_generic_write_seq(dsi, 0xca, 0x00);
71 	mipi_dsi_generic_write_seq(dsi, 0xc0, 0x04);
72 	mipi_dsi_generic_write_seq(dsi, 0xbe, 0xb5);
73 	mipi_dsi_generic_write_seq(dsi, 0xb3, 0x35, 0x35);
74 	mipi_dsi_generic_write_seq(dsi, 0xb4, 0x25, 0x25);
75 	mipi_dsi_generic_write_seq(dsi, 0xb9, 0x43, 0x43);
76 	mipi_dsi_generic_write_seq(dsi, 0xba, 0x24, 0x24);
77 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x02);
78 	mipi_dsi_generic_write_seq(dsi, 0xee, 0x03);
79 	mipi_dsi_generic_write_seq(dsi, 0xb0,
80 				   0x00, 0xb2, 0x00, 0xb3, 0x00, 0xb6, 0x00, 0xc3,
81 				   0x00, 0xce, 0x00, 0xe1, 0x00, 0xf3, 0x01, 0x11);
82 	mipi_dsi_generic_write_seq(dsi, 0xb1,
83 				   0x01, 0x2e, 0x01, 0x5c, 0x01, 0x82, 0x01, 0xc3,
84 				   0x01, 0xfe, 0x02, 0x00, 0x02, 0x37, 0x02, 0x77);
85 	mipi_dsi_generic_write_seq(dsi, 0xb2,
86 				   0x02, 0xa1, 0x02, 0xd7, 0x02, 0xfe, 0x03, 0x2c,
87 				   0x03, 0x4b, 0x03, 0x63, 0x03, 0x8f, 0x03, 0x90);
88 	mipi_dsi_generic_write_seq(dsi, 0xb3, 0x03, 0x96, 0x03, 0x98);
89 	mipi_dsi_generic_write_seq(dsi, 0xb4,
90 				   0x00, 0x81, 0x00, 0x8b, 0x00, 0x9c, 0x00, 0xa9,
91 				   0x00, 0xb5, 0x00, 0xcb, 0x00, 0xdf, 0x01, 0x02);
92 	mipi_dsi_generic_write_seq(dsi, 0xb5,
93 				   0x01, 0x1f, 0x01, 0x51, 0x01, 0x7a, 0x01, 0xbf,
94 				   0x01, 0xfa, 0x01, 0xfc, 0x02, 0x34, 0x02, 0x76);
95 	mipi_dsi_generic_write_seq(dsi, 0xb6,
96 				   0x02, 0x9f, 0x02, 0xd7, 0x02, 0xfc, 0x03, 0x2c,
97 				   0x03, 0x4a, 0x03, 0x63, 0x03, 0x8f, 0x03, 0xa2);
98 	mipi_dsi_generic_write_seq(dsi, 0xb7, 0x03, 0xb8, 0x03, 0xba);
99 	mipi_dsi_generic_write_seq(dsi, 0xb8,
100 				   0x00, 0x01, 0x00, 0x02, 0x00, 0x0e, 0x00, 0x2a,
101 				   0x00, 0x41, 0x00, 0x67, 0x00, 0x87, 0x00, 0xb9);
102 	mipi_dsi_generic_write_seq(dsi, 0xb9,
103 				   0x00, 0xe2, 0x01, 0x22, 0x01, 0x54, 0x01, 0xa3,
104 				   0x01, 0xe6, 0x01, 0xe7, 0x02, 0x24, 0x02, 0x67);
105 	mipi_dsi_generic_write_seq(dsi, 0xba,
106 				   0x02, 0x93, 0x02, 0xcd, 0x02, 0xf6, 0x03, 0x31,
107 				   0x03, 0x6c, 0x03, 0xe9, 0x03, 0xef, 0x03, 0xf4);
108 	mipi_dsi_generic_write_seq(dsi, 0xbb, 0x03, 0xf6, 0x03, 0xf7);
109 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x03);
110 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x22, 0x00);
111 	mipi_dsi_generic_write_seq(dsi, 0xb1, 0x22, 0x00);
112 	mipi_dsi_generic_write_seq(dsi, 0xb2, 0x05, 0x00, 0x60, 0x00, 0x00);
113 	mipi_dsi_generic_write_seq(dsi, 0xb3, 0x05, 0x00, 0x60, 0x00, 0x00);
114 	mipi_dsi_generic_write_seq(dsi, 0xb4, 0x05, 0x00, 0x60, 0x00, 0x00);
115 	mipi_dsi_generic_write_seq(dsi, 0xb5, 0x05, 0x00, 0x60, 0x00, 0x00);
116 	mipi_dsi_generic_write_seq(dsi, 0xba, 0x53, 0x00, 0x60, 0x00, 0x00);
117 	mipi_dsi_generic_write_seq(dsi, 0xbb, 0x53, 0x00, 0x60, 0x00, 0x00);
118 	mipi_dsi_generic_write_seq(dsi, 0xbc, 0x53, 0x00, 0x60, 0x00, 0x00);
119 	mipi_dsi_generic_write_seq(dsi, 0xbd, 0x53, 0x00, 0x60, 0x00, 0x00);
120 	mipi_dsi_generic_write_seq(dsi, 0xc0, 0x00, 0x34, 0x00, 0x00);
121 	mipi_dsi_generic_write_seq(dsi, 0xc1, 0x00, 0x00, 0x34, 0x00);
122 	mipi_dsi_generic_write_seq(dsi, 0xc2, 0x00, 0x00, 0x34, 0x00);
123 	mipi_dsi_generic_write_seq(dsi, 0xc3, 0x00, 0x00, 0x34, 0x00);
124 	mipi_dsi_generic_write_seq(dsi, 0xc4, 0x60);
125 	mipi_dsi_generic_write_seq(dsi, 0xc5, 0xc0);
126 	mipi_dsi_generic_write_seq(dsi, 0xc6, 0x00);
127 	mipi_dsi_generic_write_seq(dsi, 0xc7, 0x00);
128 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x05);
129 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x17, 0x06);
130 	mipi_dsi_generic_write_seq(dsi, 0xb1, 0x17, 0x06);
131 	mipi_dsi_generic_write_seq(dsi, 0xb2, 0x17, 0x06);
132 	mipi_dsi_generic_write_seq(dsi, 0xb3, 0x17, 0x06);
133 	mipi_dsi_generic_write_seq(dsi, 0xb4, 0x17, 0x06);
134 	mipi_dsi_generic_write_seq(dsi, 0xb5, 0x17, 0x06);
135 	mipi_dsi_generic_write_seq(dsi, 0xb6, 0x17, 0x06);
136 	mipi_dsi_generic_write_seq(dsi, 0xb7, 0x17, 0x06);
137 	mipi_dsi_generic_write_seq(dsi, 0xb8, 0x00);
138 	mipi_dsi_generic_write_seq(dsi, 0xb9, 0x00, 0x03);
139 	mipi_dsi_generic_write_seq(dsi, 0xba, 0x00, 0x00);
140 	mipi_dsi_generic_write_seq(dsi, 0xbb, 0x02, 0x03);
141 	mipi_dsi_generic_write_seq(dsi, 0xbc, 0x02, 0x03);
142 	mipi_dsi_generic_write_seq(dsi, 0xbd, 0x03, 0x03, 0x00, 0x03, 0x03);
143 	mipi_dsi_generic_write_seq(dsi, 0xc0, 0x0b);
144 	mipi_dsi_generic_write_seq(dsi, 0xc1, 0x09);
145 	mipi_dsi_generic_write_seq(dsi, 0xc2, 0xa6);
146 	mipi_dsi_generic_write_seq(dsi, 0xc3, 0x05);
147 	mipi_dsi_generic_write_seq(dsi, 0xc4, 0x00);
148 	mipi_dsi_generic_write_seq(dsi, 0xc5, 0x02);
149 	mipi_dsi_generic_write_seq(dsi, 0xc6, 0x22);
150 	mipi_dsi_generic_write_seq(dsi, 0xc7, 0x03);
151 	mipi_dsi_generic_write_seq(dsi, 0xc8, 0x07, 0x20);
152 	mipi_dsi_generic_write_seq(dsi, 0xc9, 0x03, 0x20);
153 	mipi_dsi_generic_write_seq(dsi, 0xca, 0x01, 0x60);
154 	mipi_dsi_generic_write_seq(dsi, 0xcb, 0x01, 0x60);
155 	mipi_dsi_generic_write_seq(dsi, 0xcc, 0x00, 0x00, 0x02);
156 	mipi_dsi_generic_write_seq(dsi, 0xcd, 0x00, 0x00, 0x02);
157 	mipi_dsi_generic_write_seq(dsi, 0xce, 0x00, 0x00, 0x02);
158 	mipi_dsi_generic_write_seq(dsi, 0xcf, 0x00, 0x00, 0x02);
159 	mipi_dsi_generic_write_seq(dsi, 0xd1, 0x00, 0x05, 0x01, 0x07, 0x10);
160 	mipi_dsi_generic_write_seq(dsi, 0xd2, 0x10, 0x05, 0x05, 0x03, 0x10);
161 	mipi_dsi_generic_write_seq(dsi, 0xd3, 0x20, 0x00, 0x43, 0x07, 0x10);
162 	mipi_dsi_generic_write_seq(dsi, 0xd4, 0x30, 0x00, 0x43, 0x07, 0x10);
163 	mipi_dsi_generic_write_seq(dsi, 0xd0,
164 				   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
165 	mipi_dsi_generic_write_seq(dsi, 0xd5,
166 				   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167 				   0x00, 0x00, 0x00);
168 	mipi_dsi_generic_write_seq(dsi, 0xd6,
169 				   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170 				   0x00, 0x00, 0x00);
171 	mipi_dsi_generic_write_seq(dsi, 0xd7,
172 				   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173 				   0x00, 0x00, 0x00);
174 	mipi_dsi_generic_write_seq(dsi, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00);
175 	mipi_dsi_generic_write_seq(dsi, 0xe5, 0x06);
176 	mipi_dsi_generic_write_seq(dsi, 0xe6, 0x06);
177 	mipi_dsi_generic_write_seq(dsi, 0xe7, 0x00);
178 	mipi_dsi_generic_write_seq(dsi, 0xe8, 0x06);
179 	mipi_dsi_generic_write_seq(dsi, 0xe9, 0x06);
180 	mipi_dsi_generic_write_seq(dsi, 0xea, 0x06);
181 	mipi_dsi_generic_write_seq(dsi, 0xeb, 0x00);
182 	mipi_dsi_generic_write_seq(dsi, 0xec, 0x00);
183 	mipi_dsi_generic_write_seq(dsi, 0xed, 0x30);
184 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x06);
185 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x31, 0x31);
186 	mipi_dsi_generic_write_seq(dsi, 0xb1, 0x31, 0x31);
187 	mipi_dsi_generic_write_seq(dsi, 0xb2, 0x2d, 0x2e);
188 	mipi_dsi_generic_write_seq(dsi, 0xb3, 0x31, 0x34);
189 	mipi_dsi_generic_write_seq(dsi, 0xb4, 0x29, 0x2a);
190 	mipi_dsi_generic_write_seq(dsi, 0xb5, 0x12, 0x10);
191 	mipi_dsi_generic_write_seq(dsi, 0xb6, 0x18, 0x16);
192 	mipi_dsi_generic_write_seq(dsi, 0xb7, 0x00, 0x02);
193 	mipi_dsi_generic_write_seq(dsi, 0xb8, 0x08, 0x31);
194 	mipi_dsi_generic_write_seq(dsi, 0xb9, 0x31, 0x31);
195 	mipi_dsi_generic_write_seq(dsi, 0xba, 0x31, 0x31);
196 	mipi_dsi_generic_write_seq(dsi, 0xbb, 0x31, 0x08);
197 	mipi_dsi_generic_write_seq(dsi, 0xbc, 0x03, 0x01);
198 	mipi_dsi_generic_write_seq(dsi, 0xbd, 0x17, 0x19);
199 	mipi_dsi_generic_write_seq(dsi, 0xbe, 0x11, 0x13);
200 	mipi_dsi_generic_write_seq(dsi, 0xbf, 0x2a, 0x29);
201 	mipi_dsi_generic_write_seq(dsi, 0xc0, 0x34, 0x31);
202 	mipi_dsi_generic_write_seq(dsi, 0xc1, 0x2e, 0x2d);
203 	mipi_dsi_generic_write_seq(dsi, 0xc2, 0x31, 0x31);
204 	mipi_dsi_generic_write_seq(dsi, 0xc3, 0x31, 0x31);
205 	mipi_dsi_generic_write_seq(dsi, 0xc4, 0x31, 0x31);
206 	mipi_dsi_generic_write_seq(dsi, 0xc5, 0x31, 0x31);
207 	mipi_dsi_generic_write_seq(dsi, 0xc6, 0x2e, 0x2d);
208 	mipi_dsi_generic_write_seq(dsi, 0xc7, 0x31, 0x34);
209 	mipi_dsi_generic_write_seq(dsi, 0xc8, 0x29, 0x2a);
210 	mipi_dsi_generic_write_seq(dsi, 0xc9, 0x17, 0x19);
211 	mipi_dsi_generic_write_seq(dsi, 0xca, 0x11, 0x13);
212 	mipi_dsi_generic_write_seq(dsi, 0xcb, 0x03, 0x01);
213 	mipi_dsi_generic_write_seq(dsi, 0xcc, 0x08, 0x31);
214 	mipi_dsi_generic_write_seq(dsi, 0xcd, 0x31, 0x31);
215 	mipi_dsi_generic_write_seq(dsi, 0xce, 0x31, 0x31);
216 	mipi_dsi_generic_write_seq(dsi, 0xcf, 0x31, 0x08);
217 	mipi_dsi_generic_write_seq(dsi, 0xd0, 0x00, 0x02);
218 	mipi_dsi_generic_write_seq(dsi, 0xd1, 0x12, 0x10);
219 	mipi_dsi_generic_write_seq(dsi, 0xd2, 0x18, 0x16);
220 	mipi_dsi_generic_write_seq(dsi, 0xd3, 0x2a, 0x29);
221 	mipi_dsi_generic_write_seq(dsi, 0xd4, 0x34, 0x31);
222 	mipi_dsi_generic_write_seq(dsi, 0xd5, 0x2d, 0x2e);
223 	mipi_dsi_generic_write_seq(dsi, 0xd6, 0x31, 0x31);
224 	mipi_dsi_generic_write_seq(dsi, 0xd7, 0x31, 0x31);
225 	mipi_dsi_generic_write_seq(dsi, 0xe5, 0x31, 0x31);
226 	mipi_dsi_generic_write_seq(dsi, 0xe6, 0x31, 0x31);
227 	mipi_dsi_generic_write_seq(dsi, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00);
228 	mipi_dsi_generic_write_seq(dsi, 0xd9, 0x00, 0x00, 0x00, 0x00, 0x00);
229 	mipi_dsi_generic_write_seq(dsi, 0xe7, 0x00);
230 	mipi_dsi_generic_write_seq(dsi, 0x6f, 0x02);
231 	mipi_dsi_generic_write_seq(dsi, 0xf7, 0x47);
232 	mipi_dsi_generic_write_seq(dsi, 0x6f, 0x0a);
233 	mipi_dsi_generic_write_seq(dsi, 0xf7, 0x02);
234 	mipi_dsi_generic_write_seq(dsi, 0x6f, 0x17);
235 	mipi_dsi_generic_write_seq(dsi, 0xf4, 0x60);
236 	mipi_dsi_generic_write_seq(dsi, 0x6f, 0x01);
237 	mipi_dsi_generic_write_seq(dsi, 0xf9, 0x46);
238 	mipi_dsi_generic_write_seq(dsi, 0x6f, 0x11);
239 	mipi_dsi_generic_write_seq(dsi, 0xf3, 0x01);
240 	mipi_dsi_generic_write_seq(dsi, 0x35, 0x00);
241 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x00);
242 	mipi_dsi_generic_write_seq(dsi, 0xd9, 0x02, 0x03, 0x00);
243 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x00, 0x00);
244 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x08, 0x00);
245 	mipi_dsi_generic_write_seq(dsi, 0xb1, 0x6c, 0x21);
246 	mipi_dsi_generic_write_seq(dsi, 0xf0, 0x55, 0xaa, 0x52, 0x00, 0x00);
247 	mipi_dsi_generic_write_seq(dsi, 0x35, 0x00);
248 
249 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
250 	if (ret < 0) {
251 		dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
252 		return ret;
253 	}
254 	msleep(120);
255 
256 	ret = mipi_dsi_dcs_set_display_on(dsi);
257 	if (ret < 0) {
258 		dev_err(dev, "Failed to set display on: %d\n", ret);
259 		return ret;
260 	}
261 	usleep_range(1000, 2000);
262 
263 	mipi_dsi_generic_write_seq(dsi, 0x53, 0x24);
264 
265 	return 0;
266 }
267 
268 static int truly_nt35521_off(struct truly_nt35521 *ctx)
269 {
270 	struct mipi_dsi_device *dsi = ctx->dsi;
271 	struct device *dev = &dsi->dev;
272 	int ret;
273 
274 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
275 
276 	ret = mipi_dsi_dcs_set_display_off(dsi);
277 	if (ret < 0) {
278 		dev_err(dev, "Failed to set display off: %d\n", ret);
279 		return ret;
280 	}
281 	msleep(50);
282 
283 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
284 	if (ret < 0) {
285 		dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
286 		return ret;
287 	}
288 	msleep(150);
289 
290 	return 0;
291 }
292 
293 static int truly_nt35521_prepare(struct drm_panel *panel)
294 {
295 	struct truly_nt35521 *ctx = to_truly_nt35521(panel);
296 	struct device *dev = &ctx->dsi->dev;
297 	int ret;
298 
299 	if (ctx->prepared)
300 		return 0;
301 
302 	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
303 	if (ret < 0) {
304 		dev_err(dev, "Failed to enable regulators: %d\n", ret);
305 		return ret;
306 	}
307 
308 	truly_nt35521_reset(ctx);
309 
310 	ret = truly_nt35521_on(ctx);
311 	if (ret < 0) {
312 		dev_err(dev, "Failed to initialize panel: %d\n", ret);
313 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
314 		return ret;
315 	}
316 
317 	ctx->prepared = true;
318 	return 0;
319 }
320 
321 static int truly_nt35521_unprepare(struct drm_panel *panel)
322 {
323 	struct truly_nt35521 *ctx = to_truly_nt35521(panel);
324 	struct device *dev = &ctx->dsi->dev;
325 	int ret;
326 
327 	if (!ctx->prepared)
328 		return 0;
329 
330 	ret = truly_nt35521_off(ctx);
331 	if (ret < 0)
332 		dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
333 
334 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
335 	regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
336 			       ctx->supplies);
337 
338 	ctx->prepared = false;
339 	return 0;
340 }
341 
342 static int truly_nt35521_enable(struct drm_panel *panel)
343 {
344 	struct truly_nt35521 *ctx = to_truly_nt35521(panel);
345 
346 	if (ctx->enabled)
347 		return 0;
348 
349 	gpiod_set_value_cansleep(ctx->blen_gpio, 1);
350 
351 	ctx->enabled = true;
352 	return 0;
353 }
354 
355 static int truly_nt35521_disable(struct drm_panel *panel)
356 {
357 	struct truly_nt35521 *ctx = to_truly_nt35521(panel);
358 
359 	if (!ctx->enabled)
360 		return 0;
361 
362 	gpiod_set_value_cansleep(ctx->blen_gpio, 0);
363 
364 	ctx->enabled = false;
365 	return 0;
366 }
367 
368 static const struct drm_display_mode truly_nt35521_mode = {
369 	.clock = (720 + 232 + 20 + 112) * (1280 + 18 + 1 + 18) * 60 / 1000,
370 	.hdisplay = 720,
371 	.hsync_start = 720 + 232,
372 	.hsync_end = 720 + 232 + 20,
373 	.htotal = 720 + 232 + 20 + 112,
374 	.vdisplay = 1280,
375 	.vsync_start = 1280 + 18,
376 	.vsync_end = 1280 + 18 + 1,
377 	.vtotal = 1280 + 18 + 1 + 18,
378 	.width_mm = 65,
379 	.height_mm = 116,
380 };
381 
382 static int truly_nt35521_get_modes(struct drm_panel *panel,
383 				   struct drm_connector *connector)
384 {
385 	struct drm_display_mode *mode;
386 
387 	mode = drm_mode_duplicate(connector->dev, &truly_nt35521_mode);
388 	if (!mode)
389 		return -ENOMEM;
390 
391 	drm_mode_set_name(mode);
392 
393 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
394 	connector->display_info.width_mm = mode->width_mm;
395 	connector->display_info.height_mm = mode->height_mm;
396 	drm_mode_probed_add(connector, mode);
397 
398 	return 1;
399 }
400 
401 static const struct drm_panel_funcs truly_nt35521_panel_funcs = {
402 	.prepare = truly_nt35521_prepare,
403 	.unprepare = truly_nt35521_unprepare,
404 	.enable = truly_nt35521_enable,
405 	.disable = truly_nt35521_disable,
406 	.get_modes = truly_nt35521_get_modes,
407 };
408 
409 static int truly_nt35521_bl_update_status(struct backlight_device *bl)
410 {
411 	struct mipi_dsi_device *dsi = bl_get_data(bl);
412 	u16 brightness = backlight_get_brightness(bl);
413 	int ret;
414 
415 	ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
416 	if (ret < 0)
417 		return ret;
418 
419 	return 0;
420 }
421 
422 static int truly_nt35521_bl_get_brightness(struct backlight_device *bl)
423 {
424 	struct mipi_dsi_device *dsi = bl_get_data(bl);
425 	u16 brightness;
426 	int ret;
427 
428 	ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
429 	if (ret < 0)
430 		return ret;
431 
432 	return brightness & 0xff;
433 }
434 
435 static const struct backlight_ops truly_nt35521_bl_ops = {
436 	.update_status = truly_nt35521_bl_update_status,
437 	.get_brightness = truly_nt35521_bl_get_brightness,
438 };
439 
440 static struct backlight_device *
441 truly_nt35521_create_backlight(struct mipi_dsi_device *dsi)
442 {
443 	struct device *dev = &dsi->dev;
444 	const struct backlight_properties props = {
445 		.type = BACKLIGHT_RAW,
446 		.brightness = 255,
447 		.max_brightness = 255,
448 	};
449 
450 	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
451 					      &truly_nt35521_bl_ops, &props);
452 }
453 
454 static int truly_nt35521_probe(struct mipi_dsi_device *dsi)
455 {
456 	struct device *dev = &dsi->dev;
457 	struct truly_nt35521 *ctx;
458 	int ret;
459 
460 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
461 	if (!ctx)
462 		return -ENOMEM;
463 
464 	ctx->supplies[0].supply = "positive5";
465 	ctx->supplies[1].supply = "negative5";
466 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
467 				      ctx->supplies);
468 	if (ret < 0) {
469 		dev_err(dev, "Failed to get regulators: %d\n", ret);
470 		return ret;
471 	}
472 
473 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
474 	if (IS_ERR(ctx->reset_gpio))
475 		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
476 				     "Failed to get reset-gpios\n");
477 
478 	ctx->blen_gpio = devm_gpiod_get(dev, "backlight", GPIOD_OUT_LOW);
479 	if (IS_ERR(ctx->blen_gpio))
480 		return dev_err_probe(dev, PTR_ERR(ctx->blen_gpio),
481 				     "Failed to get backlight-gpios\n");
482 
483 	ctx->dsi = dsi;
484 	mipi_dsi_set_drvdata(dsi, ctx);
485 
486 	dsi->lanes = 4;
487 	dsi->format = MIPI_DSI_FMT_RGB888;
488 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
489 			  MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_NO_EOT_PACKET |
490 			  MIPI_DSI_CLOCK_NON_CONTINUOUS;
491 
492 	drm_panel_init(&ctx->panel, dev, &truly_nt35521_panel_funcs,
493 		       DRM_MODE_CONNECTOR_DSI);
494 
495 	ctx->panel.backlight = truly_nt35521_create_backlight(dsi);
496 	if (IS_ERR(ctx->panel.backlight))
497 		return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
498 				     "Failed to create backlight\n");
499 
500 	drm_panel_add(&ctx->panel);
501 
502 	ret = mipi_dsi_attach(dsi);
503 	if (ret < 0) {
504 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
505 		drm_panel_remove(&ctx->panel);
506 		return ret;
507 	}
508 
509 	return 0;
510 }
511 
512 static void truly_nt35521_remove(struct mipi_dsi_device *dsi)
513 {
514 	struct truly_nt35521 *ctx = mipi_dsi_get_drvdata(dsi);
515 	int ret;
516 
517 	ret = mipi_dsi_detach(dsi);
518 	if (ret < 0)
519 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
520 
521 	drm_panel_remove(&ctx->panel);
522 }
523 
524 static const struct of_device_id truly_nt35521_of_match[] = {
525 	{ .compatible = "sony,tulip-truly-nt35521" },
526 	{ /* sentinel */ }
527 };
528 MODULE_DEVICE_TABLE(of, truly_nt35521_of_match);
529 
530 static struct mipi_dsi_driver truly_nt35521_driver = {
531 	.probe = truly_nt35521_probe,
532 	.remove = truly_nt35521_remove,
533 	.driver = {
534 		.name = "panel-truly-nt35521",
535 		.of_match_table = truly_nt35521_of_match,
536 	},
537 };
538 module_mipi_dsi_driver(truly_nt35521_driver);
539 
540 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
541 MODULE_DESCRIPTION("DRM driver for Sony Tulip Truly NT35521 panel");
542 MODULE_LICENSE("GPL v2");
543