Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * DRM driver for Ilitek ILI9225 panels
  4 *
  5 * Copyright 2017 David Lechner <david@lechnology.com>
  6 *
  7 * Some code copied from mipi-dbi.c
  8 * Copyright 2016 Noralf Trønnes
  9 */
 10
 11#include <linux/delay.h>
 12#include <linux/dma-buf.h>
 13#include <linux/gpio/consumer.h>
 14#include <linux/module.h>
 15#include <linux/property.h>
 16#include <linux/spi/spi.h>
 17#include <video/mipi_display.h>
 18
 19#include <drm/drm_atomic_helper.h>
 20#include <drm/drm_client_setup.h>
 21#include <drm/drm_damage_helper.h>
 22#include <drm/drm_drv.h>
 23#include <drm/drm_fb_dma_helper.h>
 24#include <drm/drm_fbdev_dma.h>
 25#include <drm/drm_fourcc.h>
 26#include <drm/drm_framebuffer.h>
 27#include <drm/drm_gem_atomic_helper.h>
 28#include <drm/drm_gem_dma_helper.h>
 29#include <drm/drm_gem_framebuffer_helper.h>
 30#include <drm/drm_managed.h>
 31#include <drm/drm_mipi_dbi.h>
 32#include <drm/drm_rect.h>
 33
 34#define ILI9225_DRIVER_READ_CODE	0x00
 35#define ILI9225_DRIVER_OUTPUT_CONTROL	0x01
 36#define ILI9225_LCD_AC_DRIVING_CONTROL	0x02
 37#define ILI9225_ENTRY_MODE		0x03
 38#define ILI9225_DISPLAY_CONTROL_1	0x07
 39#define ILI9225_BLANK_PERIOD_CONTROL_1	0x08
 40#define ILI9225_FRAME_CYCLE_CONTROL	0x0b
 41#define ILI9225_INTERFACE_CONTROL	0x0c
 42#define ILI9225_OSCILLATION_CONTROL	0x0f
 43#define ILI9225_POWER_CONTROL_1		0x10
 44#define ILI9225_POWER_CONTROL_2		0x11
 45#define ILI9225_POWER_CONTROL_3		0x12
 46#define ILI9225_POWER_CONTROL_4		0x13
 47#define ILI9225_POWER_CONTROL_5		0x14
 48#define ILI9225_VCI_RECYCLING		0x15
 49#define ILI9225_RAM_ADDRESS_SET_1	0x20
 50#define ILI9225_RAM_ADDRESS_SET_2	0x21
 51#define ILI9225_WRITE_DATA_TO_GRAM	0x22
 52#define ILI9225_SOFTWARE_RESET		0x28
 53#define ILI9225_GATE_SCAN_CONTROL	0x30
 54#define ILI9225_VERTICAL_SCROLL_1	0x31
 55#define ILI9225_VERTICAL_SCROLL_2	0x32
 56#define ILI9225_VERTICAL_SCROLL_3	0x33
 57#define ILI9225_PARTIAL_DRIVING_POS_1	0x34
 58#define ILI9225_PARTIAL_DRIVING_POS_2	0x35
 59#define ILI9225_HORIZ_WINDOW_ADDR_1	0x36
 60#define ILI9225_HORIZ_WINDOW_ADDR_2	0x37
 61#define ILI9225_VERT_WINDOW_ADDR_1	0x38
 62#define ILI9225_VERT_WINDOW_ADDR_2	0x39
 63#define ILI9225_GAMMA_CONTROL_1		0x50
 64#define ILI9225_GAMMA_CONTROL_2		0x51
 65#define ILI9225_GAMMA_CONTROL_3		0x52
 66#define ILI9225_GAMMA_CONTROL_4		0x53
 67#define ILI9225_GAMMA_CONTROL_5		0x54
 68#define ILI9225_GAMMA_CONTROL_6		0x55
 69#define ILI9225_GAMMA_CONTROL_7		0x56
 70#define ILI9225_GAMMA_CONTROL_8		0x57
 71#define ILI9225_GAMMA_CONTROL_9		0x58
 72#define ILI9225_GAMMA_CONTROL_10	0x59
 73
 74static inline int ili9225_command(struct mipi_dbi *dbi, u8 cmd, u16 data)
 75{
 76	u8 par[2] = { data >> 8, data & 0xff };
 77
 78	return mipi_dbi_command_buf(dbi, cmd, par, 2);
 79}
 80
 81static void ili9225_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb,
 82			     struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state)
 83{
 84	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
 85	unsigned int height = rect->y2 - rect->y1;
 86	unsigned int width = rect->x2 - rect->x1;
 87	struct mipi_dbi *dbi = &dbidev->dbi;
 88	bool swap = dbi->swap_bytes;
 89	u16 x_start, y_start;
 90	u16 x1, x2, y1, y2;
 91	int ret = 0;
 92	bool full;
 93	void *tr;
 94
 95	full = width == fb->width && height == fb->height;
 96
 97	DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
 98
 99	if (!dbi->dc || !full || swap ||
100	    fb->format->format == DRM_FORMAT_XRGB8888) {
101		tr = dbidev->tx_buf;
102		ret = mipi_dbi_buf_copy(tr, src, fb, rect, swap, fmtcnv_state);
103		if (ret)
104			goto err_msg;
105	} else {
106		tr = src->vaddr; /* TODO: Use mapping abstraction properly */
107	}
108
109	switch (dbidev->rotation) {
110	default:
111		x1 = rect->x1;
112		x2 = rect->x2 - 1;
113		y1 = rect->y1;
114		y2 = rect->y2 - 1;
115		x_start = x1;
116		y_start = y1;
117		break;
118	case 90:
119		x1 = rect->y1;
120		x2 = rect->y2 - 1;
121		y1 = fb->width - rect->x2;
122		y2 = fb->width - rect->x1 - 1;
123		x_start = x1;
124		y_start = y2;
125		break;
126	case 180:
127		x1 = fb->width - rect->x2;
128		x2 = fb->width - rect->x1 - 1;
129		y1 = fb->height - rect->y2;
130		y2 = fb->height - rect->y1 - 1;
131		x_start = x2;
132		y_start = y2;
133		break;
134	case 270:
135		x1 = fb->height - rect->y2;
136		x2 = fb->height - rect->y1 - 1;
137		y1 = rect->x1;
138		y2 = rect->x2 - 1;
139		x_start = x2;
140		y_start = y1;
141		break;
142	}
143
144	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
145	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
146	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_1, y2);
147	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_2, y1);
148
149	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, x_start);
150	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, y_start);
151
152	ret = mipi_dbi_command_buf(dbi, ILI9225_WRITE_DATA_TO_GRAM, tr,
153				   width * height * 2);
154err_msg:
155	if (ret)
156		dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
157}
158
159static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
160				struct drm_plane_state *old_state)
161{
162	struct drm_plane_state *state = pipe->plane.state;
163	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
164	struct drm_framebuffer *fb = state->fb;
165	struct drm_rect rect;
166	int idx;
167
168	if (!pipe->crtc.state->active)
169		return;
170
171	if (!drm_dev_enter(fb->dev, &idx))
172		return;
173
174	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
175		ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
176				 &shadow_plane_state->fmtcnv_state);
177
178	drm_dev_exit(idx);
179}
180
181static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
182				struct drm_crtc_state *crtc_state,
183				struct drm_plane_state *plane_state)
184{
185	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
186	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
187	struct drm_framebuffer *fb = plane_state->fb;
188	struct device *dev = pipe->crtc.dev->dev;
189	struct mipi_dbi *dbi = &dbidev->dbi;
190	struct drm_rect rect = {
191		.x1 = 0,
192		.x2 = fb->width,
193		.y1 = 0,
194		.y2 = fb->height,
195	};
196	int ret, idx;
197	u8 am_id;
198
199	if (!drm_dev_enter(pipe->crtc.dev, &idx))
200		return;
201
202	DRM_DEBUG_KMS("\n");
203
204	mipi_dbi_hw_reset(dbi);
205
206	/*
207	 * There don't seem to be two example init sequences that match, so
208	 * using the one from the popular Arduino library for this display.
209	 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
210	 */
211
212	ret = ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0000);
213	if (ret) {
214		DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
215		goto out_exit;
216	}
217	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0000);
218	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x0000);
219	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x0000);
220	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x0000);
221
222	msleep(40);
223
224	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0018);
225	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x6121);
226	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x006f);
227	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x495f);
228	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0800);
229
230	msleep(10);
231
232	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x103b);
233
234	msleep(50);
235
236	switch (dbidev->rotation) {
237	default:
238		am_id = 0x30;
239		break;
240	case 90:
241		am_id = 0x18;
242		break;
243	case 180:
244		am_id = 0x00;
245		break;
246	case 270:
247		am_id = 0x28;
248		break;
249	}
250	ili9225_command(dbi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
251	ili9225_command(dbi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
252	ili9225_command(dbi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
253	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
254	ili9225_command(dbi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
255	ili9225_command(dbi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
256	ili9225_command(dbi, ILI9225_INTERFACE_CONTROL, 0x0000);
257	ili9225_command(dbi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
258	ili9225_command(dbi, ILI9225_VCI_RECYCLING, 0x0020);
259	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
260	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
261
262	ili9225_command(dbi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
263	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
264	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
265	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
266	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
267	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
268
269	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_1, 0x0000);
270	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_2, 0x0808);
271	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_3, 0x080a);
272	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_4, 0x000a);
273	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
274	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_6, 0x0808);
275	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_7, 0x0000);
276	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
277	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_9, 0x0710);
278	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_10, 0x0710);
279
280	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
281
282	msleep(50);
283
284	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
285
286	ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
287			 &shadow_plane_state->fmtcnv_state);
288
289out_exit:
290	drm_dev_exit(idx);
291}
292
293static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
294{
295	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
296	struct mipi_dbi *dbi = &dbidev->dbi;
297
298	DRM_DEBUG_KMS("\n");
299
300	/*
301	 * This callback is not protected by drm_dev_enter/exit since we want to
302	 * turn off the display on regular driver unload. It's highly unlikely
303	 * that the underlying SPI controller is gone should this be called after
304	 * unplug.
305	 */
306
307	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
308	msleep(50);
309	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0007);
310	msleep(50);
311	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0a02);
312}
313
314static int ili9225_dbi_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par,
315			       size_t num)
316{
317	struct spi_device *spi = dbi->spi;
318	unsigned int bpw = 8;
319	u32 speed_hz;
320	int ret;
321
322	spi_bus_lock(spi->controller);
323	gpiod_set_value_cansleep(dbi->dc, 0);
324	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
325	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
326	spi_bus_unlock(spi->controller);
327	if (ret || !num)
328		return ret;
329
330	if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !dbi->swap_bytes)
331		bpw = 16;
332
333	spi_bus_lock(spi->controller);
334	gpiod_set_value_cansleep(dbi->dc, 1);
335	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
336	ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
337	spi_bus_unlock(spi->controller);
338
339	return ret;
340}
341
342static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
343	.mode_valid	= mipi_dbi_pipe_mode_valid,
344	.enable		= ili9225_pipe_enable,
345	.disable	= ili9225_pipe_disable,
346	.update		= ili9225_pipe_update,
347	.begin_fb_access = mipi_dbi_pipe_begin_fb_access,
348	.end_fb_access	= mipi_dbi_pipe_end_fb_access,
349	.reset_plane	= mipi_dbi_pipe_reset_plane,
350	.duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state,
351	.destroy_plane_state = mipi_dbi_pipe_destroy_plane_state,
352};
353
354static const struct drm_display_mode ili9225_mode = {
355	DRM_SIMPLE_MODE(176, 220, 35, 44),
356};
357
358DEFINE_DRM_GEM_DMA_FOPS(ili9225_fops);
359
360static const struct drm_driver ili9225_driver = {
361	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
362	.fops			= &ili9225_fops,
363	DRM_GEM_DMA_DRIVER_OPS_VMAP,
364	DRM_FBDEV_DMA_DRIVER_OPS,
365	.name			= "ili9225",
366	.desc			= "Ilitek ILI9225",
367	.date			= "20171106",
368	.major			= 1,
369	.minor			= 0,
370};
371
372static const struct of_device_id ili9225_of_match[] = {
373	{ .compatible = "vot,v220hf01a-t" },
374	{},
375};
376MODULE_DEVICE_TABLE(of, ili9225_of_match);
377
378static const struct spi_device_id ili9225_id[] = {
379	{ "v220hf01a-t", 0 },
380	{ },
381};
382MODULE_DEVICE_TABLE(spi, ili9225_id);
383
384static int ili9225_probe(struct spi_device *spi)
385{
386	struct device *dev = &spi->dev;
387	struct mipi_dbi_dev *dbidev;
388	struct drm_device *drm;
389	struct mipi_dbi *dbi;
390	struct gpio_desc *rs;
391	u32 rotation = 0;
392	int ret;
393
394	dbidev = devm_drm_dev_alloc(dev, &ili9225_driver,
395				    struct mipi_dbi_dev, drm);
396	if (IS_ERR(dbidev))
397		return PTR_ERR(dbidev);
398
399	dbi = &dbidev->dbi;
400	drm = &dbidev->drm;
401
402	dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
403	if (IS_ERR(dbi->reset))
404		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
405
406	rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
407	if (IS_ERR(rs))
408		return dev_err_probe(dev, PTR_ERR(rs), "Failed to get GPIO 'rs'\n");
409
410	device_property_read_u32(dev, "rotation", &rotation);
411
412	ret = mipi_dbi_spi_init(spi, dbi, rs);
413	if (ret)
414		return ret;
415
416	/* override the command function set in  mipi_dbi_spi_init() */
417	dbi->command = ili9225_dbi_command;
418
419	ret = mipi_dbi_dev_init(dbidev, &ili9225_pipe_funcs, &ili9225_mode, rotation);
420	if (ret)
421		return ret;
422
423	drm_mode_config_reset(drm);
424
425	ret = drm_dev_register(drm, 0);
426	if (ret)
427		return ret;
428
429	spi_set_drvdata(spi, drm);
430
431	drm_client_setup(drm, NULL);
432
433	return 0;
434}
435
436static void ili9225_remove(struct spi_device *spi)
437{
438	struct drm_device *drm = spi_get_drvdata(spi);
439
440	drm_dev_unplug(drm);
441	drm_atomic_helper_shutdown(drm);
442}
443
444static void ili9225_shutdown(struct spi_device *spi)
445{
446	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
447}
448
449static struct spi_driver ili9225_spi_driver = {
450	.driver = {
451		.name = "ili9225",
 
452		.of_match_table = ili9225_of_match,
453	},
454	.id_table = ili9225_id,
455	.probe = ili9225_probe,
456	.remove = ili9225_remove,
457	.shutdown = ili9225_shutdown,
458};
459module_spi_driver(ili9225_spi_driver);
460
461MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
462MODULE_AUTHOR("David Lechner <david@lechnology.com>");
463MODULE_LICENSE("GPL");
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * DRM driver for Ilitek ILI9225 panels
  4 *
  5 * Copyright 2017 David Lechner <david@lechnology.com>
  6 *
  7 * Some code copied from mipi-dbi.c
  8 * Copyright 2016 Noralf Trønnes
  9 */
 10
 11#include <linux/delay.h>
 12#include <linux/dma-buf.h>
 13#include <linux/gpio/consumer.h>
 14#include <linux/module.h>
 15#include <linux/property.h>
 16#include <linux/spi/spi.h>
 17#include <video/mipi_display.h>
 18
 19#include <drm/drm_atomic_helper.h>
 
 20#include <drm/drm_damage_helper.h>
 21#include <drm/drm_drv.h>
 22#include <drm/drm_fb_dma_helper.h>
 23#include <drm/drm_fbdev_generic.h>
 24#include <drm/drm_fourcc.h>
 25#include <drm/drm_framebuffer.h>
 26#include <drm/drm_gem_atomic_helper.h>
 27#include <drm/drm_gem_dma_helper.h>
 28#include <drm/drm_gem_framebuffer_helper.h>
 29#include <drm/drm_managed.h>
 30#include <drm/drm_mipi_dbi.h>
 31#include <drm/drm_rect.h>
 32
 33#define ILI9225_DRIVER_READ_CODE	0x00
 34#define ILI9225_DRIVER_OUTPUT_CONTROL	0x01
 35#define ILI9225_LCD_AC_DRIVING_CONTROL	0x02
 36#define ILI9225_ENTRY_MODE		0x03
 37#define ILI9225_DISPLAY_CONTROL_1	0x07
 38#define ILI9225_BLANK_PERIOD_CONTROL_1	0x08
 39#define ILI9225_FRAME_CYCLE_CONTROL	0x0b
 40#define ILI9225_INTERFACE_CONTROL	0x0c
 41#define ILI9225_OSCILLATION_CONTROL	0x0f
 42#define ILI9225_POWER_CONTROL_1		0x10
 43#define ILI9225_POWER_CONTROL_2		0x11
 44#define ILI9225_POWER_CONTROL_3		0x12
 45#define ILI9225_POWER_CONTROL_4		0x13
 46#define ILI9225_POWER_CONTROL_5		0x14
 47#define ILI9225_VCI_RECYCLING		0x15
 48#define ILI9225_RAM_ADDRESS_SET_1	0x20
 49#define ILI9225_RAM_ADDRESS_SET_2	0x21
 50#define ILI9225_WRITE_DATA_TO_GRAM	0x22
 51#define ILI9225_SOFTWARE_RESET		0x28
 52#define ILI9225_GATE_SCAN_CONTROL	0x30
 53#define ILI9225_VERTICAL_SCROLL_1	0x31
 54#define ILI9225_VERTICAL_SCROLL_2	0x32
 55#define ILI9225_VERTICAL_SCROLL_3	0x33
 56#define ILI9225_PARTIAL_DRIVING_POS_1	0x34
 57#define ILI9225_PARTIAL_DRIVING_POS_2	0x35
 58#define ILI9225_HORIZ_WINDOW_ADDR_1	0x36
 59#define ILI9225_HORIZ_WINDOW_ADDR_2	0x37
 60#define ILI9225_VERT_WINDOW_ADDR_1	0x38
 61#define ILI9225_VERT_WINDOW_ADDR_2	0x39
 62#define ILI9225_GAMMA_CONTROL_1		0x50
 63#define ILI9225_GAMMA_CONTROL_2		0x51
 64#define ILI9225_GAMMA_CONTROL_3		0x52
 65#define ILI9225_GAMMA_CONTROL_4		0x53
 66#define ILI9225_GAMMA_CONTROL_5		0x54
 67#define ILI9225_GAMMA_CONTROL_6		0x55
 68#define ILI9225_GAMMA_CONTROL_7		0x56
 69#define ILI9225_GAMMA_CONTROL_8		0x57
 70#define ILI9225_GAMMA_CONTROL_9		0x58
 71#define ILI9225_GAMMA_CONTROL_10	0x59
 72
 73static inline int ili9225_command(struct mipi_dbi *dbi, u8 cmd, u16 data)
 74{
 75	u8 par[2] = { data >> 8, data & 0xff };
 76
 77	return mipi_dbi_command_buf(dbi, cmd, par, 2);
 78}
 79
 80static void ili9225_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb,
 81			     struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state)
 82{
 83	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
 84	unsigned int height = rect->y2 - rect->y1;
 85	unsigned int width = rect->x2 - rect->x1;
 86	struct mipi_dbi *dbi = &dbidev->dbi;
 87	bool swap = dbi->swap_bytes;
 88	u16 x_start, y_start;
 89	u16 x1, x2, y1, y2;
 90	int ret = 0;
 91	bool full;
 92	void *tr;
 93
 94	full = width == fb->width && height == fb->height;
 95
 96	DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
 97
 98	if (!dbi->dc || !full || swap ||
 99	    fb->format->format == DRM_FORMAT_XRGB8888) {
100		tr = dbidev->tx_buf;
101		ret = mipi_dbi_buf_copy(tr, src, fb, rect, swap, fmtcnv_state);
102		if (ret)
103			goto err_msg;
104	} else {
105		tr = src->vaddr; /* TODO: Use mapping abstraction properly */
106	}
107
108	switch (dbidev->rotation) {
109	default:
110		x1 = rect->x1;
111		x2 = rect->x2 - 1;
112		y1 = rect->y1;
113		y2 = rect->y2 - 1;
114		x_start = x1;
115		y_start = y1;
116		break;
117	case 90:
118		x1 = rect->y1;
119		x2 = rect->y2 - 1;
120		y1 = fb->width - rect->x2;
121		y2 = fb->width - rect->x1 - 1;
122		x_start = x1;
123		y_start = y2;
124		break;
125	case 180:
126		x1 = fb->width - rect->x2;
127		x2 = fb->width - rect->x1 - 1;
128		y1 = fb->height - rect->y2;
129		y2 = fb->height - rect->y1 - 1;
130		x_start = x2;
131		y_start = y2;
132		break;
133	case 270:
134		x1 = fb->height - rect->y2;
135		x2 = fb->height - rect->y1 - 1;
136		y1 = rect->x1;
137		y2 = rect->x2 - 1;
138		x_start = x2;
139		y_start = y1;
140		break;
141	}
142
143	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
144	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
145	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_1, y2);
146	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_2, y1);
147
148	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, x_start);
149	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, y_start);
150
151	ret = mipi_dbi_command_buf(dbi, ILI9225_WRITE_DATA_TO_GRAM, tr,
152				   width * height * 2);
153err_msg:
154	if (ret)
155		dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
156}
157
158static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
159				struct drm_plane_state *old_state)
160{
161	struct drm_plane_state *state = pipe->plane.state;
162	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
163	struct drm_framebuffer *fb = state->fb;
164	struct drm_rect rect;
165	int idx;
166
167	if (!pipe->crtc.state->active)
168		return;
169
170	if (!drm_dev_enter(fb->dev, &idx))
171		return;
172
173	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
174		ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
175				 &shadow_plane_state->fmtcnv_state);
176
177	drm_dev_exit(idx);
178}
179
180static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
181				struct drm_crtc_state *crtc_state,
182				struct drm_plane_state *plane_state)
183{
184	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
185	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
186	struct drm_framebuffer *fb = plane_state->fb;
187	struct device *dev = pipe->crtc.dev->dev;
188	struct mipi_dbi *dbi = &dbidev->dbi;
189	struct drm_rect rect = {
190		.x1 = 0,
191		.x2 = fb->width,
192		.y1 = 0,
193		.y2 = fb->height,
194	};
195	int ret, idx;
196	u8 am_id;
197
198	if (!drm_dev_enter(pipe->crtc.dev, &idx))
199		return;
200
201	DRM_DEBUG_KMS("\n");
202
203	mipi_dbi_hw_reset(dbi);
204
205	/*
206	 * There don't seem to be two example init sequences that match, so
207	 * using the one from the popular Arduino library for this display.
208	 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
209	 */
210
211	ret = ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0000);
212	if (ret) {
213		DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
214		goto out_exit;
215	}
216	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0000);
217	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x0000);
218	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x0000);
219	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x0000);
220
221	msleep(40);
222
223	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0018);
224	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x6121);
225	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x006f);
226	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x495f);
227	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0800);
228
229	msleep(10);
230
231	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x103b);
232
233	msleep(50);
234
235	switch (dbidev->rotation) {
236	default:
237		am_id = 0x30;
238		break;
239	case 90:
240		am_id = 0x18;
241		break;
242	case 180:
243		am_id = 0x00;
244		break;
245	case 270:
246		am_id = 0x28;
247		break;
248	}
249	ili9225_command(dbi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
250	ili9225_command(dbi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
251	ili9225_command(dbi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
252	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
253	ili9225_command(dbi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
254	ili9225_command(dbi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
255	ili9225_command(dbi, ILI9225_INTERFACE_CONTROL, 0x0000);
256	ili9225_command(dbi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
257	ili9225_command(dbi, ILI9225_VCI_RECYCLING, 0x0020);
258	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
259	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
260
261	ili9225_command(dbi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
262	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
263	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
264	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
265	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
266	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
267
268	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_1, 0x0000);
269	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_2, 0x0808);
270	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_3, 0x080a);
271	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_4, 0x000a);
272	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
273	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_6, 0x0808);
274	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_7, 0x0000);
275	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
276	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_9, 0x0710);
277	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_10, 0x0710);
278
279	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
280
281	msleep(50);
282
283	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
284
285	ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
286			 &shadow_plane_state->fmtcnv_state);
287
288out_exit:
289	drm_dev_exit(idx);
290}
291
292static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
293{
294	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
295	struct mipi_dbi *dbi = &dbidev->dbi;
296
297	DRM_DEBUG_KMS("\n");
298
299	/*
300	 * This callback is not protected by drm_dev_enter/exit since we want to
301	 * turn off the display on regular driver unload. It's highly unlikely
302	 * that the underlying SPI controller is gone should this be called after
303	 * unplug.
304	 */
305
306	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
307	msleep(50);
308	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0007);
309	msleep(50);
310	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0a02);
311}
312
313static int ili9225_dbi_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par,
314			       size_t num)
315{
316	struct spi_device *spi = dbi->spi;
317	unsigned int bpw = 8;
318	u32 speed_hz;
319	int ret;
320
321	spi_bus_lock(spi->controller);
322	gpiod_set_value_cansleep(dbi->dc, 0);
323	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
324	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
325	spi_bus_unlock(spi->controller);
326	if (ret || !num)
327		return ret;
328
329	if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !dbi->swap_bytes)
330		bpw = 16;
331
332	spi_bus_lock(spi->controller);
333	gpiod_set_value_cansleep(dbi->dc, 1);
334	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
335	ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
336	spi_bus_unlock(spi->controller);
337
338	return ret;
339}
340
341static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
342	.mode_valid	= mipi_dbi_pipe_mode_valid,
343	.enable		= ili9225_pipe_enable,
344	.disable	= ili9225_pipe_disable,
345	.update		= ili9225_pipe_update,
346	.begin_fb_access = mipi_dbi_pipe_begin_fb_access,
347	.end_fb_access	= mipi_dbi_pipe_end_fb_access,
348	.reset_plane	= mipi_dbi_pipe_reset_plane,
349	.duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state,
350	.destroy_plane_state = mipi_dbi_pipe_destroy_plane_state,
351};
352
353static const struct drm_display_mode ili9225_mode = {
354	DRM_SIMPLE_MODE(176, 220, 35, 44),
355};
356
357DEFINE_DRM_GEM_DMA_FOPS(ili9225_fops);
358
359static const struct drm_driver ili9225_driver = {
360	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
361	.fops			= &ili9225_fops,
362	DRM_GEM_DMA_DRIVER_OPS_VMAP,
 
363	.name			= "ili9225",
364	.desc			= "Ilitek ILI9225",
365	.date			= "20171106",
366	.major			= 1,
367	.minor			= 0,
368};
369
370static const struct of_device_id ili9225_of_match[] = {
371	{ .compatible = "vot,v220hf01a-t" },
372	{},
373};
374MODULE_DEVICE_TABLE(of, ili9225_of_match);
375
376static const struct spi_device_id ili9225_id[] = {
377	{ "v220hf01a-t", 0 },
378	{ },
379};
380MODULE_DEVICE_TABLE(spi, ili9225_id);
381
382static int ili9225_probe(struct spi_device *spi)
383{
384	struct device *dev = &spi->dev;
385	struct mipi_dbi_dev *dbidev;
386	struct drm_device *drm;
387	struct mipi_dbi *dbi;
388	struct gpio_desc *rs;
389	u32 rotation = 0;
390	int ret;
391
392	dbidev = devm_drm_dev_alloc(dev, &ili9225_driver,
393				    struct mipi_dbi_dev, drm);
394	if (IS_ERR(dbidev))
395		return PTR_ERR(dbidev);
396
397	dbi = &dbidev->dbi;
398	drm = &dbidev->drm;
399
400	dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
401	if (IS_ERR(dbi->reset))
402		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
403
404	rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
405	if (IS_ERR(rs))
406		return dev_err_probe(dev, PTR_ERR(rs), "Failed to get GPIO 'rs'\n");
407
408	device_property_read_u32(dev, "rotation", &rotation);
409
410	ret = mipi_dbi_spi_init(spi, dbi, rs);
411	if (ret)
412		return ret;
413
414	/* override the command function set in  mipi_dbi_spi_init() */
415	dbi->command = ili9225_dbi_command;
416
417	ret = mipi_dbi_dev_init(dbidev, &ili9225_pipe_funcs, &ili9225_mode, rotation);
418	if (ret)
419		return ret;
420
421	drm_mode_config_reset(drm);
422
423	ret = drm_dev_register(drm, 0);
424	if (ret)
425		return ret;
426
427	spi_set_drvdata(spi, drm);
428
429	drm_fbdev_generic_setup(drm, 0);
430
431	return 0;
432}
433
434static void ili9225_remove(struct spi_device *spi)
435{
436	struct drm_device *drm = spi_get_drvdata(spi);
437
438	drm_dev_unplug(drm);
439	drm_atomic_helper_shutdown(drm);
440}
441
442static void ili9225_shutdown(struct spi_device *spi)
443{
444	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
445}
446
447static struct spi_driver ili9225_spi_driver = {
448	.driver = {
449		.name = "ili9225",
450		.owner = THIS_MODULE,
451		.of_match_table = ili9225_of_match,
452	},
453	.id_table = ili9225_id,
454	.probe = ili9225_probe,
455	.remove = ili9225_remove,
456	.shutdown = ili9225_shutdown,
457};
458module_spi_driver(ili9225_spi_driver);
459
460MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
461MODULE_AUTHOR("David Lechner <david@lechnology.com>");
462MODULE_LICENSE("GPL");