xref: /linux/drivers/gpu/drm/tiny/sharp-memory.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 
3 #include <drm/clients/drm_client_setup.h>
4 #include <drm/drm_atomic.h>
5 #include <drm/drm_atomic_helper.h>
6 #include <drm/drm_connector.h>
7 #include <drm/drm_damage_helper.h>
8 #include <drm/drm_drv.h>
9 #include <drm/drm_fb_dma_helper.h>
10 #include <drm/drm_fbdev_dma.h>
11 #include <drm/drm_format_helper.h>
12 #include <drm/drm_framebuffer.h>
13 #include <drm/drm_gem_atomic_helper.h>
14 #include <drm/drm_gem_dma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_managed.h>
17 #include <drm/drm_modes.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_rect.h>
20 #include <linux/bitrev.h>
21 #include <linux/delay.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/kthread.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/pwm.h>
27 #include <linux/spi/spi.h>
28 
29 #define SHARP_MODE_PERIOD 8
30 #define SHARP_ADDR_PERIOD 8
31 #define SHARP_DUMMY_PERIOD 8
32 
33 #define SHARP_MEMORY_DISPLAY_MAINTAIN_MODE 0
34 #define SHARP_MEMORY_DISPLAY_UPDATE_MODE 1
35 #define SHARP_MEMORY_DISPLAY_CLEAR_MODE 4
36 
37 enum sharp_memory_model {
38 	LS010B7DH04,
39 	LS011B7DH03,
40 	LS012B7DD01,
41 	LS013B7DH03,
42 	LS013B7DH05,
43 	LS018B7DH02,
44 	LS027B7DH01,
45 	LS027B7DH01A,
46 	LS032B7DD02,
47 	LS044Q7DH01,
48 };
49 
50 enum sharp_memory_vcom_mode {
51 	SHARP_MEMORY_SOFTWARE_VCOM,
52 	SHARP_MEMORY_EXTERNAL_VCOM,
53 	SHARP_MEMORY_PWM_VCOM
54 };
55 
56 struct sharp_memory_device {
57 	struct drm_device drm;
58 	struct spi_device *spi;
59 
60 	const struct drm_display_mode *mode;
61 
62 	struct drm_crtc crtc;
63 	struct drm_plane plane;
64 	struct drm_encoder encoder;
65 	struct drm_connector connector;
66 
67 	struct gpio_desc *enable_gpio;
68 
69 	struct task_struct *sw_vcom_signal;
70 	struct pwm_device *pwm_vcom_signal;
71 
72 	enum sharp_memory_vcom_mode vcom_mode;
73 	u8 vcom;
74 
75 	u32 pitch;
76 	u32 tx_buffer_size;
77 	u8 *tx_buffer;
78 
79 	/* When vcom_mode == "software" a kthread is used to periodically send a
80 	 * 'maintain display' message over spi. This mutex ensures tx_buffer access
81 	 * and spi bus usage is synchronized in this case.
82 	 */
83 	struct mutex tx_mutex;
84 };
85 
sharp_memory_spi_write(struct spi_device * spi,void * buf,size_t len)86 static inline int sharp_memory_spi_write(struct spi_device *spi, void *buf, size_t len)
87 {
88 	/* Reverse the bit order */
89 	for (u8 *b = buf; b < ((u8 *)buf) + len; ++b)
90 		*b = bitrev8(*b);
91 
92 	return spi_write(spi, buf, len);
93 }
94 
drm_to_sharp_memory_device(struct drm_device * drm)95 static inline struct sharp_memory_device *drm_to_sharp_memory_device(struct drm_device *drm)
96 {
97 	return container_of(drm, struct sharp_memory_device, drm);
98 }
99 
100 DEFINE_DRM_GEM_DMA_FOPS(sharp_memory_fops);
101 
102 static const struct drm_driver sharp_memory_drm_driver = {
103 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
104 	.fops			= &sharp_memory_fops,
105 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
106 	DRM_FBDEV_DMA_DRIVER_OPS,
107 	.name			= "sharp_memory_display",
108 	.desc			= "Sharp Display Memory LCD",
109 	.major			= 1,
110 	.minor			= 0,
111 };
112 
sharp_memory_set_tx_buffer_mode(u8 * buffer,u8 mode,u8 vcom)113 static inline void sharp_memory_set_tx_buffer_mode(u8 *buffer, u8 mode, u8 vcom)
114 {
115 	*buffer = mode | (vcom << 1);
116 }
117 
sharp_memory_set_tx_buffer_addresses(u8 * buffer,struct drm_rect clip,u32 pitch)118 static inline void sharp_memory_set_tx_buffer_addresses(u8 *buffer,
119 							struct drm_rect clip,
120 							u32 pitch)
121 {
122 	for (u32 line = 0; line < clip.y2; ++line)
123 		buffer[line * pitch] = line + 1;
124 }
125 
sharp_memory_set_tx_buffer_data(u8 * buffer,struct drm_framebuffer * fb,const struct iosys_map * vmap,struct drm_rect clip,u32 pitch,struct drm_format_conv_state * fmtcnv_state)126 static void sharp_memory_set_tx_buffer_data(u8 *buffer,
127 					    struct drm_framebuffer *fb,
128 					    const struct iosys_map *vmap,
129 					    struct drm_rect clip,
130 					    u32 pitch,
131 					    struct drm_format_conv_state *fmtcnv_state)
132 {
133 	int ret;
134 	struct iosys_map dst;
135 
136 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
137 	if (ret)
138 		return;
139 
140 	iosys_map_set_vaddr(&dst, buffer);
141 
142 	drm_fb_xrgb8888_to_mono(&dst, &pitch, vmap, fb, &clip, fmtcnv_state);
143 
144 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
145 }
146 
sharp_memory_update_display(struct sharp_memory_device * smd,struct drm_framebuffer * fb,const struct iosys_map * vmap,struct drm_rect clip,struct drm_format_conv_state * fmtcnv_state)147 static int sharp_memory_update_display(struct sharp_memory_device *smd,
148 				       struct drm_framebuffer *fb,
149 				       const struct iosys_map *vmap,
150 				       struct drm_rect clip,
151 				       struct drm_format_conv_state *fmtcnv_state)
152 {
153 	int ret;
154 	u32 pitch = smd->pitch;
155 	u8 vcom = smd->vcom;
156 	u8 *tx_buffer = smd->tx_buffer;
157 	u32 tx_buffer_size = smd->tx_buffer_size;
158 
159 	mutex_lock(&smd->tx_mutex);
160 
161 	/* Populate the transmit buffer with frame data */
162 	sharp_memory_set_tx_buffer_mode(&tx_buffer[0],
163 					SHARP_MEMORY_DISPLAY_UPDATE_MODE, vcom);
164 	sharp_memory_set_tx_buffer_addresses(&tx_buffer[1], clip, pitch);
165 	sharp_memory_set_tx_buffer_data(&tx_buffer[2], fb, vmap, clip, pitch, fmtcnv_state);
166 
167 	ret = sharp_memory_spi_write(smd->spi, tx_buffer, tx_buffer_size);
168 
169 	mutex_unlock(&smd->tx_mutex);
170 
171 	return ret;
172 }
173 
sharp_memory_maintain_display(struct sharp_memory_device * smd)174 static int sharp_memory_maintain_display(struct sharp_memory_device *smd)
175 {
176 	int ret;
177 	u8 vcom = smd->vcom;
178 	u8 *tx_buffer = smd->tx_buffer;
179 
180 	mutex_lock(&smd->tx_mutex);
181 
182 	sharp_memory_set_tx_buffer_mode(&tx_buffer[0], SHARP_MEMORY_DISPLAY_MAINTAIN_MODE, vcom);
183 	tx_buffer[1] = 0; /* Write dummy data */
184 	ret = sharp_memory_spi_write(smd->spi, tx_buffer, 2);
185 
186 	mutex_unlock(&smd->tx_mutex);
187 
188 	return ret;
189 }
190 
sharp_memory_clear_display(struct sharp_memory_device * smd)191 static int sharp_memory_clear_display(struct sharp_memory_device *smd)
192 {
193 	int ret;
194 	u8 vcom = smd->vcom;
195 	u8 *tx_buffer = smd->tx_buffer;
196 
197 	mutex_lock(&smd->tx_mutex);
198 
199 	sharp_memory_set_tx_buffer_mode(&tx_buffer[0], SHARP_MEMORY_DISPLAY_CLEAR_MODE, vcom);
200 	tx_buffer[1] = 0; /* write dummy data */
201 	ret = sharp_memory_spi_write(smd->spi, tx_buffer, 2);
202 
203 	mutex_unlock(&smd->tx_mutex);
204 
205 	return ret;
206 }
207 
sharp_memory_fb_dirty(struct drm_framebuffer * fb,const struct iosys_map * vmap,struct drm_rect * rect,struct drm_format_conv_state * fmtconv_state)208 static void sharp_memory_fb_dirty(struct drm_framebuffer *fb, const struct iosys_map *vmap,
209 				  struct drm_rect *rect,
210 				  struct drm_format_conv_state *fmtconv_state)
211 {
212 	struct drm_rect clip;
213 	struct sharp_memory_device *smd = drm_to_sharp_memory_device(fb->dev);
214 
215 	/* Always update a full line regardless of what is dirty */
216 	clip.x1 = 0;
217 	clip.x2 = fb->width;
218 	clip.y1 = rect->y1;
219 	clip.y2 = rect->y2;
220 
221 	sharp_memory_update_display(smd, fb, vmap, clip, fmtconv_state);
222 }
223 
sharp_memory_plane_atomic_check(struct drm_plane * plane,struct drm_atomic_commit * state)224 static int sharp_memory_plane_atomic_check(struct drm_plane *plane,
225 					   struct drm_atomic_commit *state)
226 {
227 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
228 	struct sharp_memory_device *smd;
229 	struct drm_crtc_state *crtc_state;
230 
231 	smd = container_of(plane, struct sharp_memory_device, plane);
232 	crtc_state = drm_atomic_get_new_crtc_state(state, &smd->crtc);
233 
234 	return drm_atomic_helper_check_plane_state(plane_state, crtc_state,
235 						   DRM_PLANE_NO_SCALING,
236 						   DRM_PLANE_NO_SCALING,
237 						   false, false);
238 }
239 
sharp_memory_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_commit * state)240 static void sharp_memory_plane_atomic_update(struct drm_plane *plane,
241 					     struct drm_atomic_commit *state)
242 {
243 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
244 	struct drm_plane_state *plane_state = plane->state;
245 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
246 	struct sharp_memory_device *smd;
247 	struct drm_rect rect;
248 
249 	smd = container_of(plane, struct sharp_memory_device, plane);
250 	if (!smd->crtc.state->active)
251 		return;
252 
253 	if (drm_atomic_helper_damage_merged(old_state, plane_state, &rect))
254 		sharp_memory_fb_dirty(plane_state->fb, shadow_plane_state->data,
255 				      &rect, &shadow_plane_state->fmtcnv_state);
256 }
257 
258 static const struct drm_plane_helper_funcs sharp_memory_plane_helper_funcs = {
259 	.prepare_fb = drm_gem_plane_helper_prepare_fb,
260 	.atomic_check = sharp_memory_plane_atomic_check,
261 	.atomic_update = sharp_memory_plane_atomic_update,
262 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
263 };
264 
sharp_memory_format_mod_supported(struct drm_plane * plane,u32 format,u64 modifier)265 static bool sharp_memory_format_mod_supported(struct drm_plane *plane,
266 					      u32 format,
267 					      u64 modifier)
268 {
269 	return modifier == DRM_FORMAT_MOD_LINEAR;
270 }
271 
272 static const struct drm_plane_funcs sharp_memory_plane_funcs = {
273 	.update_plane = drm_atomic_helper_update_plane,
274 	.disable_plane = drm_atomic_helper_disable_plane,
275 	.destroy = drm_plane_cleanup,
276 	DRM_GEM_SHADOW_PLANE_FUNCS,
277 	.format_mod_supported = sharp_memory_format_mod_supported,
278 };
279 
sharp_memory_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)280 static enum drm_mode_status sharp_memory_crtc_mode_valid(struct drm_crtc *crtc,
281 							 const struct drm_display_mode *mode)
282 {
283 	struct sharp_memory_device *smd = drm_to_sharp_memory_device(crtc->dev);
284 
285 	return drm_crtc_helper_mode_valid_fixed(crtc, mode, smd->mode);
286 }
287 
sharp_memory_crtc_check(struct drm_crtc * crtc,struct drm_atomic_commit * state)288 static int sharp_memory_crtc_check(struct drm_crtc *crtc,
289 				   struct drm_atomic_commit *state)
290 {
291 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
292 	int ret;
293 
294 	if (!crtc_state->enable)
295 		goto out;
296 
297 	ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
298 	if (ret)
299 		return ret;
300 
301 out:
302 	return drm_atomic_add_affected_planes(state, crtc);
303 }
304 
sharp_memory_sw_vcom_signal_thread(void * data)305 static int sharp_memory_sw_vcom_signal_thread(void *data)
306 {
307 	struct sharp_memory_device *smd = data;
308 
309 	while (!kthread_should_stop()) {
310 		smd->vcom ^= 1; /* Toggle vcom */
311 		sharp_memory_maintain_display(smd);
312 		msleep(1000);
313 	}
314 
315 	return 0;
316 }
317 
sharp_memory_crtc_enable(struct drm_crtc * crtc,struct drm_atomic_commit * state)318 static void sharp_memory_crtc_enable(struct drm_crtc *crtc,
319 				     struct drm_atomic_commit *state)
320 {
321 	struct sharp_memory_device *smd = drm_to_sharp_memory_device(crtc->dev);
322 
323 	sharp_memory_clear_display(smd);
324 
325 	if (smd->enable_gpio)
326 		gpiod_set_value(smd->enable_gpio, 1);
327 }
328 
sharp_memory_crtc_disable(struct drm_crtc * crtc,struct drm_atomic_commit * state)329 static void sharp_memory_crtc_disable(struct drm_crtc *crtc,
330 				      struct drm_atomic_commit *state)
331 {
332 	struct sharp_memory_device *smd = drm_to_sharp_memory_device(crtc->dev);
333 
334 	sharp_memory_clear_display(smd);
335 
336 	if (smd->enable_gpio)
337 		gpiod_set_value(smd->enable_gpio, 0);
338 }
339 
340 static const struct drm_crtc_helper_funcs sharp_memory_crtc_helper_funcs = {
341 	.mode_valid = sharp_memory_crtc_mode_valid,
342 	.atomic_check = sharp_memory_crtc_check,
343 	.atomic_enable = sharp_memory_crtc_enable,
344 	.atomic_disable = sharp_memory_crtc_disable,
345 };
346 
347 static const struct drm_crtc_funcs sharp_memory_crtc_funcs = {
348 	.reset = drm_atomic_helper_crtc_reset,
349 	.destroy = drm_crtc_cleanup,
350 	.set_config = drm_atomic_helper_set_config,
351 	.page_flip = drm_atomic_helper_page_flip,
352 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
353 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
354 };
355 
356 static const struct drm_encoder_funcs sharp_memory_encoder_funcs = {
357 	.destroy = drm_encoder_cleanup,
358 };
359 
sharp_memory_connector_get_modes(struct drm_connector * connector)360 static int sharp_memory_connector_get_modes(struct drm_connector *connector)
361 {
362 	struct sharp_memory_device *smd = drm_to_sharp_memory_device(connector->dev);
363 
364 	return drm_connector_helper_get_modes_fixed(connector, smd->mode);
365 }
366 
367 static const struct drm_connector_helper_funcs sharp_memory_connector_hfuncs = {
368 	.get_modes = sharp_memory_connector_get_modes,
369 };
370 
371 static const struct drm_connector_funcs sharp_memory_connector_funcs = {
372 	.reset = drm_atomic_helper_connector_reset,
373 	.fill_modes = drm_helper_probe_single_connector_modes,
374 	.destroy = drm_connector_cleanup,
375 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
376 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
377 
378 };
379 
380 static const struct drm_mode_config_funcs sharp_memory_mode_config_funcs = {
381 	.fb_create = drm_gem_fb_create_with_dirty,
382 	.atomic_check = drm_atomic_helper_check,
383 	.atomic_commit = drm_atomic_helper_commit,
384 };
385 
386 static const struct drm_display_mode sharp_memory_ls010b7dh04_mode = {
387 	DRM_SIMPLE_MODE(128, 128, 18, 18),
388 };
389 
390 static const struct drm_display_mode sharp_memory_ls011b7dh03_mode = {
391 	DRM_SIMPLE_MODE(160, 68, 25, 10),
392 };
393 
394 static const struct drm_display_mode sharp_memory_ls012b7dd01_mode = {
395 	DRM_SIMPLE_MODE(184, 38, 29, 6),
396 };
397 
398 static const struct drm_display_mode sharp_memory_ls013b7dh03_mode = {
399 	DRM_SIMPLE_MODE(128, 128, 23, 23),
400 };
401 
402 static const struct drm_display_mode sharp_memory_ls013b7dh05_mode = {
403 	DRM_SIMPLE_MODE(144, 168, 20, 24),
404 };
405 
406 static const struct drm_display_mode sharp_memory_ls018b7dh02_mode = {
407 	DRM_SIMPLE_MODE(230, 303, 27, 36),
408 };
409 
410 static const struct drm_display_mode sharp_memory_ls027b7dh01_mode = {
411 	DRM_SIMPLE_MODE(400, 240, 58, 35),
412 };
413 
414 static const struct drm_display_mode sharp_memory_ls032b7dd02_mode = {
415 	DRM_SIMPLE_MODE(336, 536, 42, 68),
416 };
417 
418 static const struct drm_display_mode sharp_memory_ls044q7dh01_mode = {
419 	DRM_SIMPLE_MODE(320, 240, 89, 67),
420 };
421 
422 static const struct spi_device_id sharp_memory_ids[] = {
423 	{"ls010b7dh04", (kernel_ulong_t)&sharp_memory_ls010b7dh04_mode},
424 	{"ls011b7dh03", (kernel_ulong_t)&sharp_memory_ls011b7dh03_mode},
425 	{"ls012b7dd01", (kernel_ulong_t)&sharp_memory_ls012b7dd01_mode},
426 	{"ls013b7dh03", (kernel_ulong_t)&sharp_memory_ls013b7dh03_mode},
427 	{"ls013b7dh05", (kernel_ulong_t)&sharp_memory_ls013b7dh05_mode},
428 	{"ls018b7dh02", (kernel_ulong_t)&sharp_memory_ls018b7dh02_mode},
429 	{"ls027b7dh01", (kernel_ulong_t)&sharp_memory_ls027b7dh01_mode},
430 	{"ls027b7dh01a", (kernel_ulong_t)&sharp_memory_ls027b7dh01_mode},
431 	{"ls032b7dd02", (kernel_ulong_t)&sharp_memory_ls032b7dd02_mode},
432 	{"ls044q7dh01", (kernel_ulong_t)&sharp_memory_ls044q7dh01_mode},
433 	{},
434 };
435 MODULE_DEVICE_TABLE(spi, sharp_memory_ids);
436 
437 static const struct of_device_id sharp_memory_of_match[] = {
438 	{.compatible = "sharp,ls010b7dh04", &sharp_memory_ls010b7dh04_mode},
439 	{.compatible = "sharp,ls011b7dh03", &sharp_memory_ls011b7dh03_mode},
440 	{.compatible = "sharp,ls012b7dd01", &sharp_memory_ls012b7dd01_mode},
441 	{.compatible = "sharp,ls013b7dh03", &sharp_memory_ls013b7dh03_mode},
442 	{.compatible = "sharp,ls013b7dh05", &sharp_memory_ls013b7dh05_mode},
443 	{.compatible = "sharp,ls018b7dh02", &sharp_memory_ls018b7dh02_mode},
444 	{.compatible = "sharp,ls027b7dh01", &sharp_memory_ls027b7dh01_mode},
445 	{.compatible = "sharp,ls027b7dh01a", &sharp_memory_ls027b7dh01_mode},
446 	{.compatible = "sharp,ls032b7dd02", &sharp_memory_ls032b7dd02_mode},
447 	{.compatible = "sharp,ls044q7dh01", &sharp_memory_ls044q7dh01_mode},
448 	{},
449 };
450 MODULE_DEVICE_TABLE(of, sharp_memory_of_match);
451 
452 static const u32 sharp_memory_formats[] = {
453 	DRM_FORMAT_XRGB8888,
454 };
455 
sharp_memory_pipe_init(struct drm_device * dev,struct sharp_memory_device * smd,const u32 * formats,unsigned int format_count,const u64 * format_modifiers)456 static int sharp_memory_pipe_init(struct drm_device *dev,
457 				  struct sharp_memory_device *smd,
458 				  const u32 *formats, unsigned int format_count,
459 				  const u64 *format_modifiers)
460 {
461 	int ret;
462 	struct drm_encoder *encoder = &smd->encoder;
463 	struct drm_plane *plane = &smd->plane;
464 	struct drm_crtc *crtc = &smd->crtc;
465 	struct drm_connector *connector = &smd->connector;
466 
467 	drm_plane_helper_add(plane, &sharp_memory_plane_helper_funcs);
468 	ret = drm_universal_plane_init(dev, plane, 0,
469 				       &sharp_memory_plane_funcs,
470 				       formats, format_count,
471 				       format_modifiers,
472 				       DRM_PLANE_TYPE_PRIMARY, NULL);
473 	if (ret)
474 		return ret;
475 
476 	drm_crtc_helper_add(crtc, &sharp_memory_crtc_helper_funcs);
477 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
478 					&sharp_memory_crtc_funcs, NULL);
479 	if (ret)
480 		return ret;
481 
482 	encoder->possible_crtcs = drm_crtc_mask(crtc);
483 	ret = drm_encoder_init(dev, encoder, &sharp_memory_encoder_funcs,
484 			       DRM_MODE_ENCODER_NONE, NULL);
485 	if (ret)
486 		return ret;
487 
488 	ret = drm_connector_init(&smd->drm, &smd->connector,
489 				 &sharp_memory_connector_funcs,
490 				 DRM_MODE_CONNECTOR_SPI);
491 	if (ret)
492 		return ret;
493 
494 	drm_connector_helper_add(&smd->connector,
495 				 &sharp_memory_connector_hfuncs);
496 
497 	return drm_connector_attach_encoder(connector, encoder);
498 }
499 
sharp_memory_init_pwm_vcom_signal(struct sharp_memory_device * smd)500 static int sharp_memory_init_pwm_vcom_signal(struct sharp_memory_device *smd)
501 {
502 	int ret;
503 	struct device *dev = &smd->spi->dev;
504 	struct pwm_state pwm_state;
505 
506 	smd->pwm_vcom_signal = devm_pwm_get(dev, NULL);
507 	if (IS_ERR(smd->pwm_vcom_signal))
508 		return dev_err_probe(dev, PTR_ERR(smd->pwm_vcom_signal),
509 				     "Could not get pwm device\n");
510 
511 	pwm_init_state(smd->pwm_vcom_signal, &pwm_state);
512 	pwm_set_relative_duty_cycle(&pwm_state, 1, 10);
513 	pwm_state.enabled = true;
514 	ret = pwm_apply_might_sleep(smd->pwm_vcom_signal, &pwm_state);
515 	if (ret)
516 		return dev_err_probe(dev, -EINVAL, "Could not apply pwm state\n");
517 
518 	return 0;
519 }
520 
sharp_memory_probe(struct spi_device * spi)521 static int sharp_memory_probe(struct spi_device *spi)
522 {
523 	int ret;
524 	struct device *dev;
525 	struct sharp_memory_device *smd;
526 	struct drm_device *drm;
527 	const char *vcom_mode_str;
528 
529 	dev = &spi->dev;
530 
531 	ret = spi_setup(spi);
532 	if (ret < 0)
533 		return dev_err_probe(dev, ret, "Failed to setup spi device\n");
534 
535 	if (!dev->coherent_dma_mask) {
536 		ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
537 		if (ret)
538 			return dev_err_probe(dev, ret, "Failed to set dma mask\n");
539 	}
540 
541 	smd = devm_drm_dev_alloc(dev, &sharp_memory_drm_driver,
542 				 struct sharp_memory_device, drm);
543 	if (IS_ERR(smd))
544 		return PTR_ERR(smd);
545 
546 	spi_set_drvdata(spi, smd);
547 
548 	smd->spi = spi;
549 	drm = &smd->drm;
550 	ret = drmm_mode_config_init(drm);
551 	if (ret)
552 		return dev_err_probe(dev, ret, "Failed to initialize drm config\n");
553 
554 	smd->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
555 	if (!smd->enable_gpio)
556 		dev_warn(dev, "Enable gpio not defined\n");
557 
558 	drm->mode_config.funcs = &sharp_memory_mode_config_funcs;
559 	smd->mode = spi_get_device_match_data(spi);
560 
561 	smd->pitch = (SHARP_ADDR_PERIOD + smd->mode->hdisplay + SHARP_DUMMY_PERIOD) / 8;
562 	smd->tx_buffer_size = (SHARP_MODE_PERIOD +
563 			       (SHARP_ADDR_PERIOD + (smd->mode->hdisplay) + SHARP_DUMMY_PERIOD) *
564 			       smd->mode->vdisplay) / 8;
565 
566 	smd->tx_buffer = devm_kzalloc(dev, smd->tx_buffer_size, GFP_KERNEL);
567 	if (!smd->tx_buffer)
568 		return -ENOMEM;
569 
570 	mutex_init(&smd->tx_mutex);
571 
572 	/*
573 	 * VCOM is a signal that prevents DC bias from being built up in
574 	 * the panel resulting in pixels being forever stuck in one state.
575 	 *
576 	 * This driver supports three different methods to generate this
577 	 * signal depending on EXTMODE pin:
578 	 *
579 	 * software (EXTMODE = L) - This mode uses a kthread to
580 	 * periodically send a "maintain display" message to the display,
581 	 * toggling the vcom bit on and off with each message
582 	 *
583 	 * external (EXTMODE = H) - This mode relies on an external
584 	 * clock to generate the signal on the EXTCOMM pin
585 	 *
586 	 * pwm (EXTMODE = H) - This mode uses a pwm device to generate
587 	 * the signal on the EXTCOMM pin
588 	 *
589 	 */
590 	if (device_property_read_string(dev, "sharp,vcom-mode", &vcom_mode_str))
591 		return dev_err_probe(dev, -EINVAL,
592 				     "Unable to find sharp,vcom-mode node in device tree\n");
593 
594 	if (!strcmp("software", vcom_mode_str)) {
595 		smd->vcom_mode = SHARP_MEMORY_SOFTWARE_VCOM;
596 		smd->sw_vcom_signal = kthread_run(sharp_memory_sw_vcom_signal_thread,
597 						  smd, "sw_vcom_signal");
598 
599 	} else if (!strcmp("external", vcom_mode_str)) {
600 		smd->vcom_mode = SHARP_MEMORY_EXTERNAL_VCOM;
601 
602 	} else if (!strcmp("pwm", vcom_mode_str)) {
603 		smd->vcom_mode = SHARP_MEMORY_PWM_VCOM;
604 		ret = sharp_memory_init_pwm_vcom_signal(smd);
605 		if (ret)
606 			return ret;
607 	} else {
608 		return dev_err_probe(dev, -EINVAL, "Invalid value set for vcom-mode\n");
609 	}
610 
611 	drm->mode_config.min_width = smd->mode->hdisplay;
612 	drm->mode_config.max_width = smd->mode->hdisplay;
613 	drm->mode_config.min_height = smd->mode->vdisplay;
614 	drm->mode_config.max_height = smd->mode->vdisplay;
615 
616 	ret = sharp_memory_pipe_init(drm, smd, sharp_memory_formats,
617 				     ARRAY_SIZE(sharp_memory_formats),
618 				     NULL);
619 	if (ret)
620 		return dev_err_probe(dev, ret, "Failed to initialize display pipeline.\n");
621 
622 	drm_plane_enable_fb_damage_clips(&smd->plane);
623 	drm_mode_config_reset(drm);
624 
625 	ret = drm_dev_register(drm, 0);
626 	if (ret)
627 		return dev_err_probe(dev, ret, "Failed to register drm device.\n");
628 
629 	drm_client_setup(drm, NULL);
630 
631 	return 0;
632 }
633 
sharp_memory_remove(struct spi_device * spi)634 static void sharp_memory_remove(struct spi_device *spi)
635 {
636 	struct sharp_memory_device *smd = spi_get_drvdata(spi);
637 
638 	drm_dev_unplug(&smd->drm);
639 	drm_atomic_helper_shutdown(&smd->drm);
640 
641 	switch (smd->vcom_mode) {
642 	case SHARP_MEMORY_SOFTWARE_VCOM:
643 		kthread_stop(smd->sw_vcom_signal);
644 		break;
645 
646 	case SHARP_MEMORY_EXTERNAL_VCOM:
647 		break;
648 
649 	case SHARP_MEMORY_PWM_VCOM:
650 		pwm_disable(smd->pwm_vcom_signal);
651 		break;
652 	}
653 }
654 
655 static struct spi_driver sharp_memory_spi_driver = {
656 	.driver = {
657 		.name = "sharp_memory",
658 		.of_match_table = sharp_memory_of_match,
659 	},
660 	.probe = sharp_memory_probe,
661 	.remove = sharp_memory_remove,
662 	.id_table = sharp_memory_ids,
663 };
664 module_spi_driver(sharp_memory_spi_driver);
665 
666 MODULE_AUTHOR("Alex Lanzano <lanzano.alex@gmail.com>");
667 MODULE_DESCRIPTION("SPI Protocol driver for the sharp_memory display");
668 MODULE_LICENSE("GPL");
669