File Index
Files and directories
Sign in
FILE

ili9225.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/tiny/ili9225.c C
  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/clients/drm_client_setup.h>
 20#include <drm/drm_atomic_helper.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_print.h>
 33#include <drm/drm_rect.h>
 34
 35#define ILI9225_DRIVER_READ_CODE	0x00
 36#define ILI9225_DRIVER_OUTPUT_CONTROL	0x01
 37#define ILI9225_LCD_AC_DRIVING_CONTROL	0x02
 38#define ILI9225_ENTRY_MODE		0x03
 39#define ILI9225_DISPLAY_CONTROL_1	0x07
 40#define ILI9225_BLANK_PERIOD_CONTROL_1	0x08
 41#define ILI9225_FRAME_CYCLE_CONTROL	0x0b
 42#define ILI9225_INTERFACE_CONTROL	0x0c
 43#define ILI9225_OSCILLATION_CONTROL	0x0f
 44#define ILI9225_POWER_CONTROL_1		0x10
 45#define ILI9225_POWER_CONTROL_2		0x11
 46#define ILI9225_POWER_CONTROL_3		0x12
 47#define ILI9225_POWER_CONTROL_4		0x13
 48#define ILI9225_POWER_CONTROL_5		0x14
 49#define ILI9225_VCI_RECYCLING		0x15
 50#define ILI9225_RAM_ADDRESS_SET_1	0x20
 51#define ILI9225_RAM_ADDRESS_SET_2	0x21
 52#define ILI9225_WRITE_DATA_TO_GRAM	0x22
 53#define ILI9225_SOFTWARE_RESET		0x28
 54#define ILI9225_GATE_SCAN_CONTROL	0x30
 55#define ILI9225_VERTICAL_SCROLL_1	0x31
 56#define ILI9225_VERTICAL_SCROLL_2	0x32
 57#define ILI9225_VERTICAL_SCROLL_3	0x33
 58#define ILI9225_PARTIAL_DRIVING_POS_1	0x34
 59#define ILI9225_PARTIAL_DRIVING_POS_2	0x35
 60#define ILI9225_HORIZ_WINDOW_ADDR_1	0x36
 61#define ILI9225_HORIZ_WINDOW_ADDR_2	0x37
 62#define ILI9225_VERT_WINDOW_ADDR_1	0x38
 63#define ILI9225_VERT_WINDOW_ADDR_2	0x39
 64#define ILI9225_GAMMA_CONTROL_1		0x50
 65#define ILI9225_GAMMA_CONTROL_2		0x51
 66#define ILI9225_GAMMA_CONTROL_3		0x52
 67#define ILI9225_GAMMA_CONTROL_4		0x53
 68#define ILI9225_GAMMA_CONTROL_5		0x54
 69#define ILI9225_GAMMA_CONTROL_6		0x55
 70#define ILI9225_GAMMA_CONTROL_7		0x56
 71#define ILI9225_GAMMA_CONTROL_8		0x57
 72#define ILI9225_GAMMA_CONTROL_9		0x58
 73#define ILI9225_GAMMA_CONTROL_10	0x59
 74
 75static inline int ili9225_command(struct mipi_dbi *dbi, u8 cmd, u16 data)
 76{
 77	u8 par[2] = { data >> 8, data & 0xff };
 78
 79	return mipi_dbi_command_buf(dbi, cmd, par, 2);
 80}
 81
 82static void ili9225_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb,
 83			     struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state)
 84{
 85	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
 86	unsigned int height = rect->y2 - rect->y1;
 87	unsigned int width = rect->x2 - rect->x1;
 88	struct mipi_dbi *dbi = &dbidev->dbi;
 89	bool swap = dbi->swap_bytes;
 90	u16 x_start, y_start;
 91	u16 x1, x2, y1, y2;
 92	int ret = 0;
 93	bool full;
 94	void *tr;
 95
 96	full = width == fb->width && height == fb->height;
 97
 98	DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
 99
100	if (!dbi->dc || !full || swap ||
101	    fb->format->format == DRM_FORMAT_XRGB8888) {
102		tr = dbidev->tx_buf;
103		ret = mipi_dbi_buf_copy(tr, src, fb, rect, swap, fmtcnv_state);
104		if (ret)
105			goto err_msg;
106	} else {
107		tr = src->vaddr; /* TODO: Use mapping abstraction properly */
108	}
109
110	switch (dbidev->rotation) {
111	default:
112		x1 = rect->x1;
113		x2 = rect->x2 - 1;
114		y1 = rect->y1;
115		y2 = rect->y2 - 1;
116		x_start = x1;
117		y_start = y1;
118		break;
119	case 90:
120		x1 = rect->y1;
121		x2 = rect->y2 - 1;
122		y1 = fb->width - rect->x2;
123		y2 = fb->width - rect->x1 - 1;
124		x_start = x1;
125		y_start = y2;
126		break;
127	case 180:
128		x1 = fb->width - rect->x2;
129		x2 = fb->width - rect->x1 - 1;
130		y1 = fb->height - rect->y2;
131		y2 = fb->height - rect->y1 - 1;
132		x_start = x2;
133		y_start = y2;
134		break;
135	case 270:
136		x1 = fb->height - rect->y2;
137		x2 = fb->height - rect->y1 - 1;
138		y1 = rect->x1;
139		y2 = rect->x2 - 1;
140		x_start = x2;
141		y_start = y1;
142		break;
143	}
144
145	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
146	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
147	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_1, y2);
148	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_2, y1);
149
150	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, x_start);
151	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, y_start);
152
153	ret = mipi_dbi_command_buf(dbi, ILI9225_WRITE_DATA_TO_GRAM, tr,
154				   width * height * 2);
155err_msg:
156	if (ret)
157		dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
158}
159
160static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
161				struct drm_plane_state *old_state)
162{
163	struct drm_plane_state *state = pipe->plane.state;
164	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
165	struct drm_framebuffer *fb = state->fb;
166	struct drm_rect rect;
167	int idx;
168
169	if (!pipe->crtc.state->active)
170		return;
171
172	if (!drm_dev_enter(fb->dev, &idx))
173		return;
174
175	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
176		ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
177				 &shadow_plane_state->fmtcnv_state);
178
179	drm_dev_exit(idx);
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_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
188	struct drm_framebuffer *fb = plane_state->fb;
189	struct device *dev = pipe->crtc.dev->dev;
190	struct mipi_dbi *dbi = &dbidev->dbi;
191	struct drm_rect rect = {
192		.x1 = 0,
193		.x2 = fb->width,
194		.y1 = 0,
195		.y2 = fb->height,
196	};
197	int ret, idx;
198	u8 am_id;
199
200	if (!drm_dev_enter(pipe->crtc.dev, &idx))
201		return;
202
203	DRM_DEBUG_KMS("\n");
204
205	mipi_dbi_hw_reset(dbi);
206
207	/*
208	 * There don't seem to be two example init sequences that match, so
209	 * using the one from the popular Arduino library for this display.
210	 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
211	 */
212
213	ret = ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0000);
214	if (ret) {
215		DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
216		goto out_exit;
217	}
218	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0000);
219	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x0000);
220	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x0000);
221	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x0000);
222
223	msleep(40);
224
225	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0018);
226	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x6121);
227	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x006f);
228	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x495f);
229	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0800);
230
231	msleep(10);
232
233	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x103b);
234
235	msleep(50);
236
237	switch (dbidev->rotation) {
238	default:
239		am_id = 0x30;
240		break;
241	case 90:
242		am_id = 0x18;
243		break;
244	case 180:
245		am_id = 0x00;
246		break;
247	case 270:
248		am_id = 0x28;
249		break;
250	}
251	ili9225_command(dbi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
252	ili9225_command(dbi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
253	ili9225_command(dbi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
254	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
255	ili9225_command(dbi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
256	ili9225_command(dbi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
257	ili9225_command(dbi, ILI9225_INTERFACE_CONTROL, 0x0000);
258	ili9225_command(dbi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
259	ili9225_command(dbi, ILI9225_VCI_RECYCLING, 0x0020);
260	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
261	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
262
263	ili9225_command(dbi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
264	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
265	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
266	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
267	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
268	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
269
270	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_1, 0x0000);
271	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_2, 0x0808);
272	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_3, 0x080a);
273	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_4, 0x000a);
274	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
275	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_6, 0x0808);
276	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_7, 0x0000);
277	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
278	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_9, 0x0710);
279	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_10, 0x0710);
280
281	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
282
283	msleep(50);
284
285	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
286
287	ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
288			 &shadow_plane_state->fmtcnv_state);
289
290out_exit:
291	drm_dev_exit(idx);
292}
293
294static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
295{
296	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
297	struct mipi_dbi *dbi = &dbidev->dbi;
298
299	DRM_DEBUG_KMS("\n");
300
301	/*
302	 * This callback is not protected by drm_dev_enter/exit since we want to
303	 * turn off the display on regular driver unload. It's highly unlikely
304	 * that the underlying SPI controller is gone should this be called after
305	 * unplug.
306	 */
307
308	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
309	msleep(50);
310	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0007);
311	msleep(50);
312	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0a02);
313}
314
315static int ili9225_dbi_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par,
316			       size_t num)
317{
318	struct spi_device *spi = dbi->spi;
319	unsigned int bpw = 8;
320	u32 speed_hz;
321	int ret;
322
323	spi_bus_lock(spi->controller);
324	gpiod_set_value_cansleep(dbi->dc, 0);
325	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
326	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
327	spi_bus_unlock(spi->controller);
328	if (ret || !num)
329		return ret;
330
331	if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !dbi->swap_bytes)
332		bpw = 16;
333
334	spi_bus_lock(spi->controller);
335	gpiod_set_value_cansleep(dbi->dc, 1);
336	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
337	ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
338	spi_bus_unlock(spi->controller);
339
340	return ret;
341}
342
343static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
344	.mode_valid	= mipi_dbi_pipe_mode_valid,
345	.enable		= ili9225_pipe_enable,
346	.disable	= ili9225_pipe_disable,
347	.update		= ili9225_pipe_update,
348	.begin_fb_access = mipi_dbi_pipe_begin_fb_access,
349	.end_fb_access	= mipi_dbi_pipe_end_fb_access,
350	.reset_plane	= mipi_dbi_pipe_reset_plane,
351	.duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state,
352	.destroy_plane_state = mipi_dbi_pipe_destroy_plane_state,
353};
354
355static const struct drm_display_mode ili9225_mode = {
356	DRM_SIMPLE_MODE(176, 220, 35, 44),
357};
358
359DEFINE_DRM_GEM_DMA_FOPS(ili9225_fops);
360
361static const struct drm_driver ili9225_driver = {
362	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
363	.fops			= &ili9225_fops,
364	DRM_GEM_DMA_DRIVER_OPS_VMAP,
365	DRM_FBDEV_DMA_DRIVER_OPS,
366	.name			= "ili9225",
367	.desc			= "Ilitek ILI9225",
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");
~/ Read-only file preview.