Linux Audio

Check our new training course

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