xref: /linux/drivers/gpu/drm/sitronix/st7571-i2c.c (revision 220994d61cebfc04f071d69049127657c7e8191b)
19b8f3200SMarcus Folkesson // SPDX-License-Identifier: GPL-2.0-or-later
29b8f3200SMarcus Folkesson /*
39b8f3200SMarcus Folkesson  * Driver for Sitronix ST7571, a 4 level gray scale dot matrix LCD controller
49b8f3200SMarcus Folkesson  *
59b8f3200SMarcus Folkesson  * Copyright (C) 2025 Marcus Folkesson <marcus.folkesson@gmail.com>
69b8f3200SMarcus Folkesson  */
79b8f3200SMarcus Folkesson 
89b8f3200SMarcus Folkesson #include <linux/bitfield.h>
99b8f3200SMarcus Folkesson #include <linux/delay.h>
109b8f3200SMarcus Folkesson #include <linux/gpio/consumer.h>
119b8f3200SMarcus Folkesson #include <linux/i2c.h>
129b8f3200SMarcus Folkesson #include <linux/module.h>
139b8f3200SMarcus Folkesson #include <linux/regmap.h>
149b8f3200SMarcus Folkesson 
159b8f3200SMarcus Folkesson #include <drm/clients/drm_client_setup.h>
169b8f3200SMarcus Folkesson #include <drm/drm_atomic.h>
179b8f3200SMarcus Folkesson #include <drm/drm_atomic_helper.h>
189b8f3200SMarcus Folkesson #include <drm/drm_connector.h>
199b8f3200SMarcus Folkesson #include <drm/drm_crtc_helper.h>
209b8f3200SMarcus Folkesson #include <drm/drm_damage_helper.h>
219b8f3200SMarcus Folkesson #include <drm/drm_drv.h>
229b8f3200SMarcus Folkesson #include <drm/drm_encoder.h>
239b8f3200SMarcus Folkesson #include <drm/drm_fb_helper.h>
249b8f3200SMarcus Folkesson #include <drm/drm_fbdev_shmem.h>
259b8f3200SMarcus Folkesson #include <drm/drm_fourcc.h>
269b8f3200SMarcus Folkesson #include <drm/drm_framebuffer.h>
279b8f3200SMarcus Folkesson #include <drm/drm_gem_atomic_helper.h>
289b8f3200SMarcus Folkesson #include <drm/drm_gem_framebuffer_helper.h>
299b8f3200SMarcus Folkesson #include <drm/drm_gem_shmem_helper.h>
309b8f3200SMarcus Folkesson #include <drm/drm_modeset_helper_vtables.h>
319b8f3200SMarcus Folkesson #include <drm/drm_module.h>
329b8f3200SMarcus Folkesson #include <drm/drm_plane.h>
339b8f3200SMarcus Folkesson #include <drm/drm_probe_helper.h>
349b8f3200SMarcus Folkesson 
359b8f3200SMarcus Folkesson #include <video/display_timing.h>
369b8f3200SMarcus Folkesson #include <video/of_display_timing.h>
379b8f3200SMarcus Folkesson 
389b8f3200SMarcus Folkesson #define ST7571_COMMAND_MODE			(0x00)
399b8f3200SMarcus Folkesson #define ST7571_DATA_MODE			(0x40)
409b8f3200SMarcus Folkesson 
419b8f3200SMarcus Folkesson /* Normal mode command set */
429b8f3200SMarcus Folkesson #define ST7571_DISPLAY_OFF			(0xae)
439b8f3200SMarcus Folkesson #define ST7571_DISPLAY_ON			(0xaf)
449b8f3200SMarcus Folkesson #define ST7571_OSC_ON				(0xab)
459b8f3200SMarcus Folkesson #define ST7571_SET_COLUMN_LSB(c)		(0x00 | FIELD_PREP(GENMASK(3, 0), (c)))
469b8f3200SMarcus Folkesson #define ST7571_SET_COLUMN_MSB(c)		(0x10 | FIELD_PREP(GENMASK(2, 0), (c) >> 4))
479b8f3200SMarcus Folkesson #define ST7571_SET_COM0_LSB(x)			(FIELD_PREP(GENMASK(6, 0), (x)))
489b8f3200SMarcus Folkesson #define ST7571_SET_COM0_MSB			(0x44)
499b8f3200SMarcus Folkesson #define ST7571_SET_COM_SCAN_DIR(d)		(0xc0 | FIELD_PREP(GENMASK(3, 3), (d)))
509b8f3200SMarcus Folkesson #define ST7571_SET_CONTRAST_LSB(c)		(FIELD_PREP(GENMASK(5, 0), (c)))
519b8f3200SMarcus Folkesson #define ST7571_SET_CONTRAST_MSB			(0x81)
529b8f3200SMarcus Folkesson #define ST7571_SET_DISPLAY_DUTY_LSB(d)		(FIELD_PREP(GENMASK(7, 0), (d)))
539b8f3200SMarcus Folkesson #define ST7571_SET_DISPLAY_DUTY_MSB		(0x48)
549b8f3200SMarcus Folkesson #define ST7571_SET_ENTIRE_DISPLAY_ON(p)		(0xa4 | FIELD_PREP(GENMASK(0, 0), (p)))
559b8f3200SMarcus Folkesson #define ST7571_SET_LCD_BIAS(b)			(0x50 | FIELD_PREP(GENMASK(2, 0), (b)))
569b8f3200SMarcus Folkesson #define ST7571_SET_MODE_LSB(m)			(FIELD_PREP(GENMASK(7, 2), (m)))
579b8f3200SMarcus Folkesson #define ST7571_SET_MODE_MSB			(0x38)
589b8f3200SMarcus Folkesson #define ST7571_SET_PAGE(p)			(0xb0 | FIELD_PREP(GENMASK(3, 0), (p)))
599b8f3200SMarcus Folkesson #define ST7571_SET_POWER(p)			(0x28 | FIELD_PREP(GENMASK(2, 0), (p)))
609b8f3200SMarcus Folkesson #define ST7571_SET_REGULATOR_REG(r)		(0x20 | FIELD_PREP(GENMASK(2, 0), (r)))
619b8f3200SMarcus Folkesson #define ST7571_SET_REVERSE(r)			(0xa6 | FIELD_PREP(GENMASK(0, 0), (r)))
629b8f3200SMarcus Folkesson #define ST7571_SET_SEG_SCAN_DIR(d)		(0xa0 | FIELD_PREP(GENMASK(0, 0), (d)))
639b8f3200SMarcus Folkesson #define ST7571_SET_START_LINE_LSB(l)		(FIELD_PREP(GENMASK(6, 0), (l)))
649b8f3200SMarcus Folkesson #define ST7571_SET_START_LINE_MSB		(0x40)
659b8f3200SMarcus Folkesson 
669b8f3200SMarcus Folkesson /* Extension command set 3 */
679b8f3200SMarcus Folkesson #define ST7571_COMMAND_SET_3			(0x7b)
689b8f3200SMarcus Folkesson #define ST7571_SET_COLOR_MODE(c)		(0x10 | FIELD_PREP(GENMASK(0, 0), (c)))
699b8f3200SMarcus Folkesson #define ST7571_COMMAND_SET_NORMAL		(0x00)
709b8f3200SMarcus Folkesson 
71*a55863baSJavier Martinez Canillas /* ST7567 commands */
72*a55863baSJavier Martinez Canillas #define ST7567_SET_LCD_BIAS(m) (0xa2 | FIELD_PREP(GENMASK(0, 0), (m)))
73*a55863baSJavier Martinez Canillas 
749b8f3200SMarcus Folkesson #define ST7571_PAGE_HEIGHT 8
759b8f3200SMarcus Folkesson 
769b8f3200SMarcus Folkesson #define DRIVER_NAME "st7571"
779b8f3200SMarcus Folkesson #define DRIVER_DESC "ST7571 DRM driver"
789b8f3200SMarcus Folkesson #define DRIVER_MAJOR 1
799b8f3200SMarcus Folkesson #define DRIVER_MINOR 0
809b8f3200SMarcus Folkesson 
819b8f3200SMarcus Folkesson enum st7571_color_mode {
829b8f3200SMarcus Folkesson 	ST7571_COLOR_MODE_GRAY = 0,
839b8f3200SMarcus Folkesson 	ST7571_COLOR_MODE_BLACKWHITE = 1,
849b8f3200SMarcus Folkesson };
859b8f3200SMarcus Folkesson 
869b8f3200SMarcus Folkesson struct st7571_device;
879b8f3200SMarcus Folkesson 
889b8f3200SMarcus Folkesson struct st7571_panel_constraints {
899b8f3200SMarcus Folkesson 	u32 min_nlines;
909b8f3200SMarcus Folkesson 	u32 max_nlines;
919b8f3200SMarcus Folkesson 	u32 min_ncols;
929b8f3200SMarcus Folkesson 	u32 max_ncols;
939b8f3200SMarcus Folkesson 	bool support_grayscale;
949b8f3200SMarcus Folkesson };
959b8f3200SMarcus Folkesson 
969b8f3200SMarcus Folkesson struct st7571_panel_data {
979b8f3200SMarcus Folkesson 	int (*init)(struct st7571_device *st7571);
98d9ace6d5SJavier Martinez Canillas 	int (*parse_dt)(struct st7571_device *st7571);
999b8f3200SMarcus Folkesson 	struct st7571_panel_constraints constraints;
1009b8f3200SMarcus Folkesson };
1019b8f3200SMarcus Folkesson 
1029b8f3200SMarcus Folkesson struct st7571_panel_format {
1039b8f3200SMarcus Folkesson 	void (*prepare_buffer)(struct st7571_device *st7571,
1049b8f3200SMarcus Folkesson 			       const struct iosys_map *vmap,
1059b8f3200SMarcus Folkesson 			       struct drm_framebuffer *fb,
1069b8f3200SMarcus Folkesson 			       struct drm_rect *rect,
1079b8f3200SMarcus Folkesson 			       struct drm_format_conv_state *fmtcnv_state);
1089b8f3200SMarcus Folkesson 	int (*update_rect)(struct drm_framebuffer *fb, struct drm_rect *rect);
1099b8f3200SMarcus Folkesson 	enum st7571_color_mode mode;
1109b8f3200SMarcus Folkesson 	const u8 nformats;
1119b8f3200SMarcus Folkesson 	const u32 formats[];
1129b8f3200SMarcus Folkesson };
1139b8f3200SMarcus Folkesson 
1149b8f3200SMarcus Folkesson struct st7571_device {
1159b8f3200SMarcus Folkesson 	struct drm_device dev;
1169b8f3200SMarcus Folkesson 
1179b8f3200SMarcus Folkesson 	struct drm_plane primary_plane;
1189b8f3200SMarcus Folkesson 	struct drm_crtc crtc;
1199b8f3200SMarcus Folkesson 	struct drm_encoder encoder;
1209b8f3200SMarcus Folkesson 	struct drm_connector connector;
1219b8f3200SMarcus Folkesson 
1229b8f3200SMarcus Folkesson 	struct drm_display_mode mode;
1239b8f3200SMarcus Folkesson 
1249b8f3200SMarcus Folkesson 	const struct st7571_panel_format *pformat;
1259b8f3200SMarcus Folkesson 	const struct st7571_panel_data *pdata;
1269b8f3200SMarcus Folkesson 	struct i2c_client *client;
1279b8f3200SMarcus Folkesson 	struct gpio_desc *reset;
1289b8f3200SMarcus Folkesson 	struct regmap *regmap;
1299b8f3200SMarcus Folkesson 
1309b8f3200SMarcus Folkesson 	/*
1319b8f3200SMarcus Folkesson 	 * Depending on the hardware design, the acknowledge signal may be hard to
1329b8f3200SMarcus Folkesson 	 * recognize as a valid logic "0" level.
1339b8f3200SMarcus Folkesson 	 * Therefor, ignore NAK if possible to stay compatible with most hardware designs
1349b8f3200SMarcus Folkesson 	 * and off-the-shelf panels out there.
1359b8f3200SMarcus Folkesson 	 *
1369b8f3200SMarcus Folkesson 	 * From section 6.4 MICROPOCESSOR INTERFACE section in the datasheet:
1379b8f3200SMarcus Folkesson 	 *
1389b8f3200SMarcus Folkesson 	 * "By connecting SDA_OUT to SDA_IN externally, the SDA line becomes fully
1399b8f3200SMarcus Folkesson 	 * I2C interface compatible.
1409b8f3200SMarcus Folkesson 	 * Separating acknowledge-output from serial data
1419b8f3200SMarcus Folkesson 	 * input is advantageous for chip-on-glass (COG) applications. In COG
1429b8f3200SMarcus Folkesson 	 * applications, the ITO resistance and the pull-up resistor will form a
1439b8f3200SMarcus Folkesson 	 * voltage  divider, which affects acknowledge-signal level. Larger ITO
1449b8f3200SMarcus Folkesson 	 * resistance will raise the acknowledged-signal level and system cannot
1459b8f3200SMarcus Folkesson 	 * recognize this level as a valid logic “0” level. By separating SDA_IN from
1469b8f3200SMarcus Folkesson 	 * SDA_OUT, the IC can be used in a mode that ignores the acknowledge-bit.
1479b8f3200SMarcus Folkesson 	 * For applications which check acknowledge-bit, it is necessary to minimize
1489b8f3200SMarcus Folkesson 	 * the ITO resistance of the SDA_OUT trace to guarantee a valid low level."
1499b8f3200SMarcus Folkesson 	 *
1509b8f3200SMarcus Folkesson 	 */
1519b8f3200SMarcus Folkesson 	bool ignore_nak;
1529b8f3200SMarcus Folkesson 
1539b8f3200SMarcus Folkesson 	bool grayscale;
1549b8f3200SMarcus Folkesson 	u32 height_mm;
1559b8f3200SMarcus Folkesson 	u32 width_mm;
1569b8f3200SMarcus Folkesson 	u32 startline;
1579b8f3200SMarcus Folkesson 	u32 nlines;
1589b8f3200SMarcus Folkesson 	u32 ncols;
1599b8f3200SMarcus Folkesson 	u32 bpp;
1609b8f3200SMarcus Folkesson 
1619b8f3200SMarcus Folkesson 	/* Intermediate buffer in LCD friendly format */
1629b8f3200SMarcus Folkesson 	u8 *hwbuf;
1639b8f3200SMarcus Folkesson 
1649b8f3200SMarcus Folkesson 	/* Row of (transformed) pixels ready to be written to the display */
1659b8f3200SMarcus Folkesson 	u8 *row;
1669b8f3200SMarcus Folkesson };
1679b8f3200SMarcus Folkesson 
drm_to_st7571(struct drm_device * dev)1689b8f3200SMarcus Folkesson static inline struct st7571_device *drm_to_st7571(struct drm_device *dev)
1699b8f3200SMarcus Folkesson {
1709b8f3200SMarcus Folkesson 	return container_of(dev, struct st7571_device, dev);
1719b8f3200SMarcus Folkesson }
1729b8f3200SMarcus Folkesson 
st7571_regmap_write(void * context,const void * data,size_t count)1739b8f3200SMarcus Folkesson static int st7571_regmap_write(void *context, const void *data, size_t count)
1749b8f3200SMarcus Folkesson {
1759b8f3200SMarcus Folkesson 	struct i2c_client *client = context;
1769b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = i2c_get_clientdata(client);
1779b8f3200SMarcus Folkesson 	int ret;
1789b8f3200SMarcus Folkesson 
1799b8f3200SMarcus Folkesson 	struct i2c_msg msg = {
1809b8f3200SMarcus Folkesson 		.addr = st7571->client->addr,
1819b8f3200SMarcus Folkesson 		.flags = st7571->ignore_nak ? I2C_M_IGNORE_NAK : 0,
1829b8f3200SMarcus Folkesson 		.len = count,
1839b8f3200SMarcus Folkesson 		.buf = (u8 *)data
1849b8f3200SMarcus Folkesson 	};
1859b8f3200SMarcus Folkesson 
1869b8f3200SMarcus Folkesson 	ret = i2c_transfer(st7571->client->adapter, &msg, 1);
1879b8f3200SMarcus Folkesson 
1889b8f3200SMarcus Folkesson 	/*
1899b8f3200SMarcus Folkesson 	 * Unfortunately, there is no way to check if the transfer failed because of
1909b8f3200SMarcus Folkesson 	 * a NAK or something else as I2C bus drivers use different return values for NAK.
1919b8f3200SMarcus Folkesson 	 *
1929b8f3200SMarcus Folkesson 	 * However, if the transfer fails and ignore_nak is set, we know it is an error.
1939b8f3200SMarcus Folkesson 	 */
1949b8f3200SMarcus Folkesson 	if (ret < 0 && st7571->ignore_nak)
1959b8f3200SMarcus Folkesson 		return ret;
1969b8f3200SMarcus Folkesson 
1979b8f3200SMarcus Folkesson 	return 0;
1989b8f3200SMarcus Folkesson }
1999b8f3200SMarcus Folkesson 
2009b8f3200SMarcus Folkesson /* The st7571 driver does not read registers but regmap expects a .read */
st7571_regmap_read(void * context,const void * reg_buf,size_t reg_size,void * val_buf,size_t val_size)2019b8f3200SMarcus Folkesson static int st7571_regmap_read(void *context, const void *reg_buf,
2029b8f3200SMarcus Folkesson 			      size_t reg_size, void *val_buf, size_t val_size)
2039b8f3200SMarcus Folkesson {
2049b8f3200SMarcus Folkesson 	return -EOPNOTSUPP;
2059b8f3200SMarcus Folkesson }
2069b8f3200SMarcus Folkesson 
st7571_send_command_list(struct st7571_device * st7571,const u8 * cmd_list,size_t len)2079b8f3200SMarcus Folkesson static int st7571_send_command_list(struct st7571_device *st7571,
2089b8f3200SMarcus Folkesson 				    const u8 *cmd_list, size_t len)
2099b8f3200SMarcus Folkesson {
2109b8f3200SMarcus Folkesson 	int ret;
2119b8f3200SMarcus Folkesson 
2129b8f3200SMarcus Folkesson 	for (int i = 0; i < len; i++) {
2139b8f3200SMarcus Folkesson 		ret = regmap_write(st7571->regmap, ST7571_COMMAND_MODE, cmd_list[i]);
2149b8f3200SMarcus Folkesson 		if (ret < 0)
2159b8f3200SMarcus Folkesson 			return ret;
2169b8f3200SMarcus Folkesson 	}
2179b8f3200SMarcus Folkesson 
2189b8f3200SMarcus Folkesson 	return ret;
2199b8f3200SMarcus Folkesson }
2209b8f3200SMarcus Folkesson 
st7571_transform_xy(const char * p,int x,int y)2219b8f3200SMarcus Folkesson static inline u8 st7571_transform_xy(const char *p, int x, int y)
2229b8f3200SMarcus Folkesson {
2239b8f3200SMarcus Folkesson 	int xrest = x % 8;
2249b8f3200SMarcus Folkesson 	u8 result = 0;
2259b8f3200SMarcus Folkesson 
2269b8f3200SMarcus Folkesson 	/*
2279b8f3200SMarcus Folkesson 	 * Transforms an (x, y) pixel coordinate into a vertical 8-bit
2289b8f3200SMarcus Folkesson 	 * column from the framebuffer. It calculates the corresponding byte in the
2299b8f3200SMarcus Folkesson 	 * framebuffer, extracts the bit at the given x position across 8 consecutive
2309b8f3200SMarcus Folkesson 	 * rows, and packs those bits into a single byte.
2319b8f3200SMarcus Folkesson 	 *
2329b8f3200SMarcus Folkesson 	 * Return an 8-bit value representing a vertical column of pixels.
2339b8f3200SMarcus Folkesson 	 */
2349b8f3200SMarcus Folkesson 	x = x / 8;
2359b8f3200SMarcus Folkesson 	y = (y / 8) * 8;
2369b8f3200SMarcus Folkesson 
2379b8f3200SMarcus Folkesson 	for (int i = 0; i < 8; i++) {
2389b8f3200SMarcus Folkesson 		int row_idx = y + i;
2399b8f3200SMarcus Folkesson 		u8 byte = p[row_idx * 16 + x];
2409b8f3200SMarcus Folkesson 		u8 bit = (byte >> xrest) & 1;
2419b8f3200SMarcus Folkesson 
2429b8f3200SMarcus Folkesson 		result |= (bit << i);
2439b8f3200SMarcus Folkesson 	}
2449b8f3200SMarcus Folkesson 
2459b8f3200SMarcus Folkesson 	return result;
2469b8f3200SMarcus Folkesson }
2479b8f3200SMarcus Folkesson 
st7571_set_position(struct st7571_device * st7571,int x,int y)2489b8f3200SMarcus Folkesson static int st7571_set_position(struct st7571_device *st7571, int x, int y)
2499b8f3200SMarcus Folkesson {
2509b8f3200SMarcus Folkesson 	u8 cmd_list[] = {
2519b8f3200SMarcus Folkesson 		ST7571_SET_COLUMN_LSB(x),
2529b8f3200SMarcus Folkesson 		ST7571_SET_COLUMN_MSB(x),
2539b8f3200SMarcus Folkesson 		ST7571_SET_PAGE(y / ST7571_PAGE_HEIGHT),
2549b8f3200SMarcus Folkesson 	};
2559b8f3200SMarcus Folkesson 
2569b8f3200SMarcus Folkesson 	return st7571_send_command_list(st7571, cmd_list, ARRAY_SIZE(cmd_list));
2579b8f3200SMarcus Folkesson }
2589b8f3200SMarcus Folkesson 
st7571_fb_clear_screen(struct st7571_device * st7571)2599b8f3200SMarcus Folkesson static int st7571_fb_clear_screen(struct st7571_device *st7571)
2609b8f3200SMarcus Folkesson {
2619b8f3200SMarcus Folkesson 	u32 npixels = st7571->ncols * round_up(st7571->nlines, ST7571_PAGE_HEIGHT) * st7571->bpp;
2629b8f3200SMarcus Folkesson 	char pixelvalue = 0x00;
2639b8f3200SMarcus Folkesson 
2649b8f3200SMarcus Folkesson 	for (int i = 0; i < npixels; i++)
2659b8f3200SMarcus Folkesson 		regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, &pixelvalue, 1);
2669b8f3200SMarcus Folkesson 
2679b8f3200SMarcus Folkesson 	return 0;
2689b8f3200SMarcus Folkesson }
2699b8f3200SMarcus Folkesson 
st7571_prepare_buffer_monochrome(struct st7571_device * st7571,const struct iosys_map * vmap,struct drm_framebuffer * fb,struct drm_rect * rect,struct drm_format_conv_state * fmtcnv_state)2709b8f3200SMarcus Folkesson static void st7571_prepare_buffer_monochrome(struct st7571_device *st7571,
2719b8f3200SMarcus Folkesson 					     const struct iosys_map *vmap,
2729b8f3200SMarcus Folkesson 					     struct drm_framebuffer *fb,
2739b8f3200SMarcus Folkesson 					     struct drm_rect *rect,
2749b8f3200SMarcus Folkesson 					     struct drm_format_conv_state *fmtcnv_state)
2759b8f3200SMarcus Folkesson {
2769b8f3200SMarcus Folkesson 	unsigned int dst_pitch;
2779b8f3200SMarcus Folkesson 	struct iosys_map dst;
2789b8f3200SMarcus Folkesson 	u32 size;
2799b8f3200SMarcus Folkesson 
2809b8f3200SMarcus Folkesson 	switch (fb->format->format) {
2819b8f3200SMarcus Folkesson 	case DRM_FORMAT_XRGB8888:
2829b8f3200SMarcus Folkesson 		dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8);
2839b8f3200SMarcus Folkesson 		iosys_map_set_vaddr(&dst, st7571->hwbuf);
2849b8f3200SMarcus Folkesson 
2859b8f3200SMarcus Folkesson 		drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
2869b8f3200SMarcus Folkesson 		break;
2879b8f3200SMarcus Folkesson 
2889b8f3200SMarcus Folkesson 	case DRM_FORMAT_R1:
2899b8f3200SMarcus Folkesson 		size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8;
2909b8f3200SMarcus Folkesson 		memcpy(st7571->hwbuf, vmap->vaddr, size);
2919b8f3200SMarcus Folkesson 		break;
2929b8f3200SMarcus Folkesson 	}
2939b8f3200SMarcus Folkesson }
2949b8f3200SMarcus Folkesson 
st7571_prepare_buffer_grayscale(struct st7571_device * st7571,const struct iosys_map * vmap,struct drm_framebuffer * fb,struct drm_rect * rect,struct drm_format_conv_state * fmtcnv_state)2959b8f3200SMarcus Folkesson static void st7571_prepare_buffer_grayscale(struct st7571_device *st7571,
2969b8f3200SMarcus Folkesson 					    const struct iosys_map *vmap,
2979b8f3200SMarcus Folkesson 					    struct drm_framebuffer *fb,
2989b8f3200SMarcus Folkesson 					    struct drm_rect *rect,
2999b8f3200SMarcus Folkesson 					    struct drm_format_conv_state *fmtcnv_state)
3009b8f3200SMarcus Folkesson {
3019b8f3200SMarcus Folkesson 	u32 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8;
3029b8f3200SMarcus Folkesson 	unsigned int dst_pitch;
3039b8f3200SMarcus Folkesson 	struct iosys_map dst;
3049b8f3200SMarcus Folkesson 
3059b8f3200SMarcus Folkesson 	switch (fb->format->format) {
3069b8f3200SMarcus Folkesson 	case DRM_FORMAT_XRGB8888: /* Only support XRGB8888 in monochrome mode */
3079b8f3200SMarcus Folkesson 		dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8);
3089b8f3200SMarcus Folkesson 		iosys_map_set_vaddr(&dst, st7571->hwbuf);
3099b8f3200SMarcus Folkesson 
3109b8f3200SMarcus Folkesson 		drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state);
3119b8f3200SMarcus Folkesson 		break;
3129b8f3200SMarcus Folkesson 
3139b8f3200SMarcus Folkesson 	case DRM_FORMAT_R1:
3149b8f3200SMarcus Folkesson 		size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8;
3159b8f3200SMarcus Folkesson 		memcpy(st7571->hwbuf, vmap->vaddr, size);
3169b8f3200SMarcus Folkesson 		break;
3179b8f3200SMarcus Folkesson 
3189b8f3200SMarcus Folkesson 	case DRM_FORMAT_R2:
3199b8f3200SMarcus Folkesson 		size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 4;
3209b8f3200SMarcus Folkesson 		memcpy(st7571->hwbuf, vmap->vaddr, size);
3219b8f3200SMarcus Folkesson 		break;
3229b8f3200SMarcus Folkesson 	};
3239b8f3200SMarcus Folkesson }
3249b8f3200SMarcus Folkesson 
st7571_fb_update_rect_monochrome(struct drm_framebuffer * fb,struct drm_rect * rect)3259b8f3200SMarcus Folkesson static int st7571_fb_update_rect_monochrome(struct drm_framebuffer *fb, struct drm_rect *rect)
3269b8f3200SMarcus Folkesson {
3279b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(fb->dev);
3289b8f3200SMarcus Folkesson 	char *row = st7571->row;
3299b8f3200SMarcus Folkesson 
3309b8f3200SMarcus Folkesson 	/* Align y to display page boundaries */
3319b8f3200SMarcus Folkesson 	rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT);
3329b8f3200SMarcus Folkesson 	rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines);
3339b8f3200SMarcus Folkesson 
3349b8f3200SMarcus Folkesson 	for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) {
3359b8f3200SMarcus Folkesson 		for (int x = rect->x1; x < rect->x2; x++)
3369b8f3200SMarcus Folkesson 			row[x] = st7571_transform_xy(st7571->hwbuf, x, y);
3379b8f3200SMarcus Folkesson 
3389b8f3200SMarcus Folkesson 		st7571_set_position(st7571, rect->x1, y);
3399b8f3200SMarcus Folkesson 
3409b8f3200SMarcus Folkesson 		/* TODO: Investige why we can't write multiple bytes at once */
3419b8f3200SMarcus Folkesson 		for (int x = rect->x1; x < rect->x2; x++)
3429b8f3200SMarcus Folkesson 			regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1);
3439b8f3200SMarcus Folkesson 	}
3449b8f3200SMarcus Folkesson 
3459b8f3200SMarcus Folkesson 	return 0;
3469b8f3200SMarcus Folkesson }
3479b8f3200SMarcus Folkesson 
st7571_fb_update_rect_grayscale(struct drm_framebuffer * fb,struct drm_rect * rect)3489b8f3200SMarcus Folkesson static int st7571_fb_update_rect_grayscale(struct drm_framebuffer *fb, struct drm_rect *rect)
3499b8f3200SMarcus Folkesson {
3509b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(fb->dev);
3519b8f3200SMarcus Folkesson 	u32 format = fb->format->format;
3529b8f3200SMarcus Folkesson 	char *row = st7571->row;
3539b8f3200SMarcus Folkesson 	int x1;
3549b8f3200SMarcus Folkesson 	int x2;
3559b8f3200SMarcus Folkesson 
3569b8f3200SMarcus Folkesson 	/* Align y to display page boundaries */
3579b8f3200SMarcus Folkesson 	rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT);
3589b8f3200SMarcus Folkesson 	rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines);
3599b8f3200SMarcus Folkesson 
3609b8f3200SMarcus Folkesson 	switch (format) {
3619b8f3200SMarcus Folkesson 	case DRM_FORMAT_XRGB8888:
3629b8f3200SMarcus Folkesson 		/* Threated as monochrome (R1) */
3639b8f3200SMarcus Folkesson 		fallthrough;
3649b8f3200SMarcus Folkesson 	case DRM_FORMAT_R1:
3659b8f3200SMarcus Folkesson 		x1 = rect->x1;
3669b8f3200SMarcus Folkesson 		x2 = rect->x2;
3679b8f3200SMarcus Folkesson 		break;
3689b8f3200SMarcus Folkesson 	case DRM_FORMAT_R2:
3699b8f3200SMarcus Folkesson 		x1 = rect->x1 * 2;
3709b8f3200SMarcus Folkesson 		x2 = rect->x2 * 2;
3719b8f3200SMarcus Folkesson 		break;
3729b8f3200SMarcus Folkesson 	}
3739b8f3200SMarcus Folkesson 
3749b8f3200SMarcus Folkesson 	for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) {
3759b8f3200SMarcus Folkesson 		for (int x = x1; x < x2; x++)
3769b8f3200SMarcus Folkesson 			row[x] = st7571_transform_xy(st7571->hwbuf, x, y);
3779b8f3200SMarcus Folkesson 
3789b8f3200SMarcus Folkesson 		st7571_set_position(st7571, rect->x1, y);
3799b8f3200SMarcus Folkesson 
3809b8f3200SMarcus Folkesson 		/* TODO: Investige why we can't write multiple bytes at once */
3819b8f3200SMarcus Folkesson 		for (int x = x1; x < x2; x++) {
3829b8f3200SMarcus Folkesson 			regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1);
3839b8f3200SMarcus Folkesson 
3849b8f3200SMarcus Folkesson 			/*
3859b8f3200SMarcus Folkesson 			 * As the display supports grayscale, all pixels must be written as two bits
3869b8f3200SMarcus Folkesson 			 * even if the format is monochrome.
3879b8f3200SMarcus Folkesson 			 *
3889b8f3200SMarcus Folkesson 			 * The bit values maps to the following grayscale:
3899b8f3200SMarcus Folkesson 			 * 0 0 = White
3909b8f3200SMarcus Folkesson 			 * 0 1 = Light gray
3919b8f3200SMarcus Folkesson 			 * 1 0 = Dark gray
3929b8f3200SMarcus Folkesson 			 * 1 1 = Black
3939b8f3200SMarcus Folkesson 			 *
3949b8f3200SMarcus Folkesson 			 * For monochrome formats, write the same value twice to get
3959b8f3200SMarcus Folkesson 			 * either a black or white pixel.
3969b8f3200SMarcus Folkesson 			 */
3979b8f3200SMarcus Folkesson 			if (format == DRM_FORMAT_R1 || format == DRM_FORMAT_XRGB8888)
3989b8f3200SMarcus Folkesson 				regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1);
3999b8f3200SMarcus Folkesson 		}
4009b8f3200SMarcus Folkesson 	}
4019b8f3200SMarcus Folkesson 
4029b8f3200SMarcus Folkesson 	return 0;
4039b8f3200SMarcus Folkesson }
4049b8f3200SMarcus Folkesson 
st7571_connector_get_modes(struct drm_connector * conn)4059b8f3200SMarcus Folkesson static int st7571_connector_get_modes(struct drm_connector *conn)
4069b8f3200SMarcus Folkesson {
4079b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(conn->dev);
4089b8f3200SMarcus Folkesson 
4099b8f3200SMarcus Folkesson 	return drm_connector_helper_get_modes_fixed(conn, &st7571->mode);
4109b8f3200SMarcus Folkesson }
4119b8f3200SMarcus Folkesson 
4129b8f3200SMarcus Folkesson static const struct drm_connector_helper_funcs st7571_connector_helper_funcs = {
4139b8f3200SMarcus Folkesson 	.get_modes = st7571_connector_get_modes,
4149b8f3200SMarcus Folkesson };
4159b8f3200SMarcus Folkesson 
4169b8f3200SMarcus Folkesson static const struct st7571_panel_format st7571_monochrome = {
4179b8f3200SMarcus Folkesson 	.prepare_buffer = st7571_prepare_buffer_monochrome,
4189b8f3200SMarcus Folkesson 	.update_rect = st7571_fb_update_rect_monochrome,
4199b8f3200SMarcus Folkesson 	.mode = ST7571_COLOR_MODE_BLACKWHITE,
4209b8f3200SMarcus Folkesson 	.formats = {
4219b8f3200SMarcus Folkesson 		DRM_FORMAT_XRGB8888,
4229b8f3200SMarcus Folkesson 		DRM_FORMAT_R1,
4239b8f3200SMarcus Folkesson 	},
4249b8f3200SMarcus Folkesson 	.nformats = 2,
4259b8f3200SMarcus Folkesson };
4269b8f3200SMarcus Folkesson 
4279b8f3200SMarcus Folkesson static const struct st7571_panel_format st7571_grayscale = {
4289b8f3200SMarcus Folkesson 	.prepare_buffer = st7571_prepare_buffer_grayscale,
4299b8f3200SMarcus Folkesson 	.update_rect = st7571_fb_update_rect_grayscale,
4309b8f3200SMarcus Folkesson 	.mode = ST7571_COLOR_MODE_GRAY,
4319b8f3200SMarcus Folkesson 	.formats = {
4329b8f3200SMarcus Folkesson 		DRM_FORMAT_XRGB8888,
4339b8f3200SMarcus Folkesson 		DRM_FORMAT_R1,
4349b8f3200SMarcus Folkesson 		DRM_FORMAT_R2,
4359b8f3200SMarcus Folkesson 	},
4369b8f3200SMarcus Folkesson 	.nformats = 3,
4379b8f3200SMarcus Folkesson };
4389b8f3200SMarcus Folkesson 
4399b8f3200SMarcus Folkesson static const u64 st7571_primary_plane_fmtmods[] = {
4409b8f3200SMarcus Folkesson 	DRM_FORMAT_MOD_LINEAR,
4419b8f3200SMarcus Folkesson 	DRM_FORMAT_MOD_INVALID
4429b8f3200SMarcus Folkesson };
4439b8f3200SMarcus Folkesson 
st7571_primary_plane_helper_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)4449b8f3200SMarcus Folkesson static int st7571_primary_plane_helper_atomic_check(struct drm_plane *plane,
4459b8f3200SMarcus Folkesson 						    struct drm_atomic_state *state)
4469b8f3200SMarcus Folkesson {
4479b8f3200SMarcus Folkesson 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
4489b8f3200SMarcus Folkesson 	struct drm_crtc *new_crtc = new_plane_state->crtc;
4499b8f3200SMarcus Folkesson 	struct drm_crtc_state *new_crtc_state = NULL;
4509b8f3200SMarcus Folkesson 
4519b8f3200SMarcus Folkesson 	if (new_crtc)
4529b8f3200SMarcus Folkesson 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
4539b8f3200SMarcus Folkesson 
4549b8f3200SMarcus Folkesson 	return drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
4559b8f3200SMarcus Folkesson 						   DRM_PLANE_NO_SCALING,
4569b8f3200SMarcus Folkesson 						   DRM_PLANE_NO_SCALING,
4579b8f3200SMarcus Folkesson 						   false, false);
4589b8f3200SMarcus Folkesson }
4599b8f3200SMarcus Folkesson 
st7571_primary_plane_helper_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)4609b8f3200SMarcus Folkesson static void st7571_primary_plane_helper_atomic_update(struct drm_plane *plane,
4619b8f3200SMarcus Folkesson 						      struct drm_atomic_state *state)
4629b8f3200SMarcus Folkesson {
4639b8f3200SMarcus Folkesson 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
4649b8f3200SMarcus Folkesson 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
4659b8f3200SMarcus Folkesson 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
4669b8f3200SMarcus Folkesson 	struct drm_framebuffer *fb = plane_state->fb;
4679b8f3200SMarcus Folkesson 	struct drm_atomic_helper_damage_iter iter;
4689b8f3200SMarcus Folkesson 	struct drm_device *dev = plane->dev;
4699b8f3200SMarcus Folkesson 	struct drm_rect damage;
4709b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(plane->dev);
4719b8f3200SMarcus Folkesson 	int ret, idx;
4729b8f3200SMarcus Folkesson 
4739b8f3200SMarcus Folkesson 	if (!fb)
4749b8f3200SMarcus Folkesson 		return; /* no framebuffer; plane is disabled */
4759b8f3200SMarcus Folkesson 
4769b8f3200SMarcus Folkesson 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
4779b8f3200SMarcus Folkesson 	if (ret)
4789b8f3200SMarcus Folkesson 		return;
4799b8f3200SMarcus Folkesson 
4809b8f3200SMarcus Folkesson 	if (!drm_dev_enter(dev, &idx))
4819b8f3200SMarcus Folkesson 		goto out_drm_gem_fb_end_cpu_access;
4829b8f3200SMarcus Folkesson 
4839b8f3200SMarcus Folkesson 	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
4849b8f3200SMarcus Folkesson 	drm_atomic_for_each_plane_damage(&iter, &damage) {
4859b8f3200SMarcus Folkesson 		st7571->pformat->prepare_buffer(st7571,
4869b8f3200SMarcus Folkesson 						&shadow_plane_state->data[0],
4879b8f3200SMarcus Folkesson 						fb, &damage,
4889b8f3200SMarcus Folkesson 						&shadow_plane_state->fmtcnv_state);
4899b8f3200SMarcus Folkesson 
4909b8f3200SMarcus Folkesson 		st7571->pformat->update_rect(fb, &damage);
4919b8f3200SMarcus Folkesson 	}
4929b8f3200SMarcus Folkesson 
4939b8f3200SMarcus Folkesson 	drm_dev_exit(idx);
4949b8f3200SMarcus Folkesson 
4959b8f3200SMarcus Folkesson out_drm_gem_fb_end_cpu_access:
4969b8f3200SMarcus Folkesson 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
4979b8f3200SMarcus Folkesson }
4989b8f3200SMarcus Folkesson 
st7571_primary_plane_helper_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)4999b8f3200SMarcus Folkesson static void st7571_primary_plane_helper_atomic_disable(struct drm_plane *plane,
5009b8f3200SMarcus Folkesson 						       struct drm_atomic_state *state)
5019b8f3200SMarcus Folkesson {
5029b8f3200SMarcus Folkesson 	struct drm_device *dev = plane->dev;
5039b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(plane->dev);
5049b8f3200SMarcus Folkesson 	int idx;
5059b8f3200SMarcus Folkesson 
5069b8f3200SMarcus Folkesson 	if (!drm_dev_enter(dev, &idx))
5079b8f3200SMarcus Folkesson 		return;
5089b8f3200SMarcus Folkesson 
5099b8f3200SMarcus Folkesson 	st7571_fb_clear_screen(st7571);
5109b8f3200SMarcus Folkesson 	drm_dev_exit(idx);
5119b8f3200SMarcus Folkesson }
5129b8f3200SMarcus Folkesson 
5139b8f3200SMarcus Folkesson static const struct drm_plane_helper_funcs st7571_primary_plane_helper_funcs = {
5149b8f3200SMarcus Folkesson 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
5159b8f3200SMarcus Folkesson 	.atomic_check = st7571_primary_plane_helper_atomic_check,
5169b8f3200SMarcus Folkesson 	.atomic_update = st7571_primary_plane_helper_atomic_update,
5179b8f3200SMarcus Folkesson 	.atomic_disable = st7571_primary_plane_helper_atomic_disable,
5189b8f3200SMarcus Folkesson };
5199b8f3200SMarcus Folkesson 
5209b8f3200SMarcus Folkesson static const struct drm_plane_funcs st7571_primary_plane_funcs = {
5219b8f3200SMarcus Folkesson 	.update_plane = drm_atomic_helper_update_plane,
5229b8f3200SMarcus Folkesson 	.disable_plane = drm_atomic_helper_disable_plane,
5239b8f3200SMarcus Folkesson 	.destroy = drm_plane_cleanup,
5249b8f3200SMarcus Folkesson 	DRM_GEM_SHADOW_PLANE_FUNCS,
5259b8f3200SMarcus Folkesson };
5269b8f3200SMarcus Folkesson 
5279b8f3200SMarcus Folkesson /*
5289b8f3200SMarcus Folkesson  * CRTC
5299b8f3200SMarcus Folkesson  */
5309b8f3200SMarcus Folkesson 
st7571_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)5319b8f3200SMarcus Folkesson static enum drm_mode_status st7571_crtc_mode_valid(struct drm_crtc *crtc,
5329b8f3200SMarcus Folkesson 						   const struct drm_display_mode *mode)
5339b8f3200SMarcus Folkesson {
5349b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(crtc->dev);
5359b8f3200SMarcus Folkesson 
5369b8f3200SMarcus Folkesson 	return drm_crtc_helper_mode_valid_fixed(crtc, mode, &st7571->mode);
5379b8f3200SMarcus Folkesson }
5389b8f3200SMarcus Folkesson 
5399b8f3200SMarcus Folkesson static const struct drm_crtc_helper_funcs st7571_crtc_helper_funcs = {
5409b8f3200SMarcus Folkesson 	.atomic_check = drm_crtc_helper_atomic_check,
5419b8f3200SMarcus Folkesson 	.mode_valid = st7571_crtc_mode_valid,
5429b8f3200SMarcus Folkesson };
5439b8f3200SMarcus Folkesson 
5449b8f3200SMarcus Folkesson static const struct drm_crtc_funcs st7571_crtc_funcs = {
5459b8f3200SMarcus Folkesson 	.reset = drm_atomic_helper_crtc_reset,
5469b8f3200SMarcus Folkesson 	.destroy = drm_crtc_cleanup,
5479b8f3200SMarcus Folkesson 	.set_config = drm_atomic_helper_set_config,
5489b8f3200SMarcus Folkesson 	.page_flip = drm_atomic_helper_page_flip,
5499b8f3200SMarcus Folkesson 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
5509b8f3200SMarcus Folkesson 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
5519b8f3200SMarcus Folkesson };
5529b8f3200SMarcus Folkesson 
5539b8f3200SMarcus Folkesson /*
5549b8f3200SMarcus Folkesson  * Encoder
5559b8f3200SMarcus Folkesson  */
5569b8f3200SMarcus Folkesson 
st7571_encoder_atomic_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)5571d043d6cSJavier Martinez Canillas static void st7571_encoder_atomic_enable(struct drm_encoder *encoder,
5589b8f3200SMarcus Folkesson 					 struct drm_atomic_state *state)
5599b8f3200SMarcus Folkesson {
5609b8f3200SMarcus Folkesson 	struct drm_device *drm = encoder->dev;
5619b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(drm);
5629b8f3200SMarcus Folkesson 	u8 command = ST7571_DISPLAY_ON;
5639b8f3200SMarcus Folkesson 	int ret;
5649b8f3200SMarcus Folkesson 
5659b8f3200SMarcus Folkesson 	ret = st7571->pdata->init(st7571);
5669b8f3200SMarcus Folkesson 	if (ret)
5679b8f3200SMarcus Folkesson 		return;
5689b8f3200SMarcus Folkesson 
5699b8f3200SMarcus Folkesson 	st7571_send_command_list(st7571, &command, 1);
5709b8f3200SMarcus Folkesson }
5719b8f3200SMarcus Folkesson 
st7571_encoder_atomic_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)5721d043d6cSJavier Martinez Canillas static void st7571_encoder_atomic_disable(struct drm_encoder *encoder,
5739b8f3200SMarcus Folkesson 					  struct drm_atomic_state *state)
5749b8f3200SMarcus Folkesson {
5759b8f3200SMarcus Folkesson 	struct drm_device *drm = encoder->dev;
5769b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = drm_to_st7571(drm);
5779b8f3200SMarcus Folkesson 	u8 command = ST7571_DISPLAY_OFF;
5789b8f3200SMarcus Folkesson 
5799b8f3200SMarcus Folkesson 	st7571_send_command_list(st7571, &command, 1);
5809b8f3200SMarcus Folkesson }
5819b8f3200SMarcus Folkesson 
5829b8f3200SMarcus Folkesson static const struct drm_encoder_funcs st7571_encoder_funcs = {
5839b8f3200SMarcus Folkesson 	.destroy = drm_encoder_cleanup,
5849b8f3200SMarcus Folkesson 
5859b8f3200SMarcus Folkesson };
5869b8f3200SMarcus Folkesson 
5879b8f3200SMarcus Folkesson static const struct drm_encoder_helper_funcs st7571_encoder_helper_funcs = {
5881d043d6cSJavier Martinez Canillas 	.atomic_enable = st7571_encoder_atomic_enable,
5891d043d6cSJavier Martinez Canillas 	.atomic_disable = st7571_encoder_atomic_disable,
5909b8f3200SMarcus Folkesson };
5919b8f3200SMarcus Folkesson 
5929b8f3200SMarcus Folkesson /*
5939b8f3200SMarcus Folkesson  * Connector
5949b8f3200SMarcus Folkesson  */
5959b8f3200SMarcus Folkesson 
5969b8f3200SMarcus Folkesson static const struct drm_connector_funcs st7571_connector_funcs = {
5979b8f3200SMarcus Folkesson 	.reset = drm_atomic_helper_connector_reset,
5989b8f3200SMarcus Folkesson 	.fill_modes = drm_helper_probe_single_connector_modes,
5999b8f3200SMarcus Folkesson 	.destroy = drm_connector_cleanup,
6009b8f3200SMarcus Folkesson 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
6019b8f3200SMarcus Folkesson 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6029b8f3200SMarcus Folkesson };
6039b8f3200SMarcus Folkesson 
6049b8f3200SMarcus Folkesson static const struct drm_mode_config_funcs st7571_mode_config_funcs = {
6059b8f3200SMarcus Folkesson 	.fb_create = drm_gem_fb_create_with_dirty,
6069b8f3200SMarcus Folkesson 	.atomic_check = drm_atomic_helper_check,
6079b8f3200SMarcus Folkesson 	.atomic_commit = drm_atomic_helper_commit,
6089b8f3200SMarcus Folkesson };
6099b8f3200SMarcus Folkesson 
st7571_mode(struct st7571_device * st7571)6109b8f3200SMarcus Folkesson static struct drm_display_mode st7571_mode(struct st7571_device *st7571)
6119b8f3200SMarcus Folkesson {
6129b8f3200SMarcus Folkesson 	struct drm_display_mode mode = {
6139b8f3200SMarcus Folkesson 		DRM_SIMPLE_MODE(st7571->ncols, st7571->nlines,
6149b8f3200SMarcus Folkesson 				st7571->width_mm, st7571->height_mm),
6159b8f3200SMarcus Folkesson 	};
6169b8f3200SMarcus Folkesson 
6179b8f3200SMarcus Folkesson 	return mode;
6189b8f3200SMarcus Folkesson }
6199b8f3200SMarcus Folkesson 
st7571_mode_config_init(struct st7571_device * st7571)6209b8f3200SMarcus Folkesson static int st7571_mode_config_init(struct st7571_device *st7571)
6219b8f3200SMarcus Folkesson {
6229b8f3200SMarcus Folkesson 	struct drm_device *dev = &st7571->dev;
6239b8f3200SMarcus Folkesson 	const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints;
6249b8f3200SMarcus Folkesson 	int ret;
6259b8f3200SMarcus Folkesson 
6269b8f3200SMarcus Folkesson 	ret = drmm_mode_config_init(dev);
6279b8f3200SMarcus Folkesson 	if (ret)
6289b8f3200SMarcus Folkesson 		return ret;
6299b8f3200SMarcus Folkesson 
6309b8f3200SMarcus Folkesson 	dev->mode_config.min_width = constraints->min_ncols;
6319b8f3200SMarcus Folkesson 	dev->mode_config.min_height = constraints->min_nlines;
6329b8f3200SMarcus Folkesson 	dev->mode_config.max_width = constraints->max_ncols;
6339b8f3200SMarcus Folkesson 	dev->mode_config.max_height = constraints->max_nlines;
6349b8f3200SMarcus Folkesson 	dev->mode_config.preferred_depth = 24;
6359b8f3200SMarcus Folkesson 	dev->mode_config.funcs = &st7571_mode_config_funcs;
6369b8f3200SMarcus Folkesson 
6379b8f3200SMarcus Folkesson 	return 0;
6389b8f3200SMarcus Folkesson }
6399b8f3200SMarcus Folkesson 
st7571_plane_init(struct st7571_device * st7571,const struct st7571_panel_format * pformat)6409b8f3200SMarcus Folkesson static int st7571_plane_init(struct st7571_device *st7571,
6419b8f3200SMarcus Folkesson 			     const struct st7571_panel_format *pformat)
6429b8f3200SMarcus Folkesson {
6439b8f3200SMarcus Folkesson 	struct drm_plane *primary_plane = &st7571->primary_plane;
6449b8f3200SMarcus Folkesson 	struct drm_device *dev = &st7571->dev;
6459b8f3200SMarcus Folkesson 	int ret;
6469b8f3200SMarcus Folkesson 
6479b8f3200SMarcus Folkesson 	ret = drm_universal_plane_init(dev, primary_plane, 0,
6489b8f3200SMarcus Folkesson 				       &st7571_primary_plane_funcs,
6499b8f3200SMarcus Folkesson 				       pformat->formats,
6509b8f3200SMarcus Folkesson 				       pformat->nformats,
6519b8f3200SMarcus Folkesson 				       st7571_primary_plane_fmtmods,
6529b8f3200SMarcus Folkesson 				       DRM_PLANE_TYPE_PRIMARY, NULL);
6539b8f3200SMarcus Folkesson 	if (ret)
6549b8f3200SMarcus Folkesson 		return ret;
6559b8f3200SMarcus Folkesson 
6569b8f3200SMarcus Folkesson 	drm_plane_helper_add(primary_plane, &st7571_primary_plane_helper_funcs);
6579b8f3200SMarcus Folkesson 	drm_plane_enable_fb_damage_clips(primary_plane);
6589b8f3200SMarcus Folkesson 
6599b8f3200SMarcus Folkesson 	return 0;
6609b8f3200SMarcus Folkesson }
6619b8f3200SMarcus Folkesson 
st7571_crtc_init(struct st7571_device * st7571)6629b8f3200SMarcus Folkesson static int st7571_crtc_init(struct st7571_device *st7571)
6639b8f3200SMarcus Folkesson {
6649b8f3200SMarcus Folkesson 	struct drm_plane *primary_plane = &st7571->primary_plane;
6659b8f3200SMarcus Folkesson 	struct drm_crtc *crtc = &st7571->crtc;
6669b8f3200SMarcus Folkesson 	struct drm_device *dev = &st7571->dev;
6679b8f3200SMarcus Folkesson 	int ret;
6689b8f3200SMarcus Folkesson 
6699b8f3200SMarcus Folkesson 	ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
6709b8f3200SMarcus Folkesson 					&st7571_crtc_funcs, NULL);
6719b8f3200SMarcus Folkesson 	if (ret)
6729b8f3200SMarcus Folkesson 		return ret;
6739b8f3200SMarcus Folkesson 
6749b8f3200SMarcus Folkesson 	drm_crtc_helper_add(crtc, &st7571_crtc_helper_funcs);
6759b8f3200SMarcus Folkesson 
6769b8f3200SMarcus Folkesson 	return 0;
6779b8f3200SMarcus Folkesson }
6789b8f3200SMarcus Folkesson 
st7571_encoder_init(struct st7571_device * st7571)6799b8f3200SMarcus Folkesson static int st7571_encoder_init(struct st7571_device *st7571)
6809b8f3200SMarcus Folkesson {
6819b8f3200SMarcus Folkesson 	struct drm_encoder *encoder = &st7571->encoder;
6829b8f3200SMarcus Folkesson 	struct drm_crtc *crtc = &st7571->crtc;
6839b8f3200SMarcus Folkesson 	struct drm_device *dev = &st7571->dev;
6849b8f3200SMarcus Folkesson 	int ret;
6859b8f3200SMarcus Folkesson 
6869b8f3200SMarcus Folkesson 	ret = drm_encoder_init(dev, encoder, &st7571_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL);
6879b8f3200SMarcus Folkesson 	if (ret)
6889b8f3200SMarcus Folkesson 		return ret;
6899b8f3200SMarcus Folkesson 
6909b8f3200SMarcus Folkesson 	drm_encoder_helper_add(encoder, &st7571_encoder_helper_funcs);
6919b8f3200SMarcus Folkesson 
6929b8f3200SMarcus Folkesson 	encoder->possible_crtcs = drm_crtc_mask(crtc);
6939b8f3200SMarcus Folkesson 
6949b8f3200SMarcus Folkesson 	return 0;
6959b8f3200SMarcus Folkesson }
6969b8f3200SMarcus Folkesson 
st7571_connector_init(struct st7571_device * st7571)6979b8f3200SMarcus Folkesson static int st7571_connector_init(struct st7571_device *st7571)
6989b8f3200SMarcus Folkesson {
6999b8f3200SMarcus Folkesson 	struct drm_connector *connector = &st7571->connector;
7009b8f3200SMarcus Folkesson 	struct drm_encoder *encoder = &st7571->encoder;
7019b8f3200SMarcus Folkesson 	struct drm_device *dev = &st7571->dev;
7029b8f3200SMarcus Folkesson 	int ret;
7039b8f3200SMarcus Folkesson 
7049b8f3200SMarcus Folkesson 	ret = drm_connector_init(dev, connector, &st7571_connector_funcs,
7059b8f3200SMarcus Folkesson 				 DRM_MODE_CONNECTOR_Unknown);
7069b8f3200SMarcus Folkesson 	if (ret)
7079b8f3200SMarcus Folkesson 		return ret;
7089b8f3200SMarcus Folkesson 
7099b8f3200SMarcus Folkesson 	drm_connector_helper_add(connector, &st7571_connector_helper_funcs);
7109b8f3200SMarcus Folkesson 
7119b8f3200SMarcus Folkesson 	return drm_connector_attach_encoder(connector, encoder);
7129b8f3200SMarcus Folkesson }
7139b8f3200SMarcus Folkesson 
7149b8f3200SMarcus Folkesson DEFINE_DRM_GEM_FOPS(st7571_fops);
7159b8f3200SMarcus Folkesson 
7169b8f3200SMarcus Folkesson static const struct drm_driver st7571_driver = {
7179b8f3200SMarcus Folkesson 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
7189b8f3200SMarcus Folkesson 
7199b8f3200SMarcus Folkesson 	.name		 = DRIVER_NAME,
7209b8f3200SMarcus Folkesson 	.desc		 = DRIVER_DESC,
7219b8f3200SMarcus Folkesson 	.major		 = DRIVER_MAJOR,
7229b8f3200SMarcus Folkesson 	.minor		 = DRIVER_MINOR,
7239b8f3200SMarcus Folkesson 
7249b8f3200SMarcus Folkesson 	.fops		 = &st7571_fops,
7259b8f3200SMarcus Folkesson 	DRM_GEM_SHMEM_DRIVER_OPS,
7269b8f3200SMarcus Folkesson 	DRM_FBDEV_SHMEM_DRIVER_OPS,
7279b8f3200SMarcus Folkesson };
7289b8f3200SMarcus Folkesson 
7299b8f3200SMarcus Folkesson static const struct regmap_bus st7571_regmap_bus = {
7309b8f3200SMarcus Folkesson 	.read = st7571_regmap_read,
7319b8f3200SMarcus Folkesson 	.write = st7571_regmap_write,
7329b8f3200SMarcus Folkesson };
7339b8f3200SMarcus Folkesson 
7349b8f3200SMarcus Folkesson static const struct regmap_config st7571_regmap_config = {
7359b8f3200SMarcus Folkesson 	.reg_bits = 8,
7369b8f3200SMarcus Folkesson 	.val_bits = 8,
7379b8f3200SMarcus Folkesson 	.use_single_write = true,
7389b8f3200SMarcus Folkesson };
7399b8f3200SMarcus Folkesson 
st7571_validate_parameters(struct st7571_device * st7571)7409b8f3200SMarcus Folkesson static int st7571_validate_parameters(struct st7571_device *st7571)
7419b8f3200SMarcus Folkesson {
7429b8f3200SMarcus Folkesson 	struct device *dev = st7571->dev.dev;
7439b8f3200SMarcus Folkesson 	const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints;
7449b8f3200SMarcus Folkesson 
7459b8f3200SMarcus Folkesson 	if (st7571->width_mm  == 0) {
7469b8f3200SMarcus Folkesson 		dev_err(dev, "Invalid panel width\n");
7479b8f3200SMarcus Folkesson 		return -EINVAL;
7489b8f3200SMarcus Folkesson 	}
7499b8f3200SMarcus Folkesson 
7509b8f3200SMarcus Folkesson 	if (st7571->height_mm == 0) {
7519b8f3200SMarcus Folkesson 		dev_err(dev, "Invalid panel height\n");
7529b8f3200SMarcus Folkesson 		return -EINVAL;
7539b8f3200SMarcus Folkesson 	}
7549b8f3200SMarcus Folkesson 
7559b8f3200SMarcus Folkesson 	if (st7571->nlines < constraints->min_nlines ||
7569b8f3200SMarcus Folkesson 	    st7571->nlines > constraints->max_nlines) {
7579b8f3200SMarcus Folkesson 		dev_err(dev, "Invalid timing configuration.\n");
7589b8f3200SMarcus Folkesson 		return -EINVAL;
7599b8f3200SMarcus Folkesson 	}
7609b8f3200SMarcus Folkesson 
7619b8f3200SMarcus Folkesson 	if (st7571->startline + st7571->nlines > constraints->max_nlines) {
7629b8f3200SMarcus Folkesson 		dev_err(dev, "Invalid timing configuration.\n");
7639b8f3200SMarcus Folkesson 		return -EINVAL;
7649b8f3200SMarcus Folkesson 	}
7659b8f3200SMarcus Folkesson 
7669b8f3200SMarcus Folkesson 	if (st7571->ncols < constraints->min_ncols ||
7679b8f3200SMarcus Folkesson 	    st7571->ncols > constraints->max_ncols) {
7689b8f3200SMarcus Folkesson 		dev_err(dev, "Invalid timing configuration.\n");
7699b8f3200SMarcus Folkesson 		return -EINVAL;
7709b8f3200SMarcus Folkesson 	}
7719b8f3200SMarcus Folkesson 
7729b8f3200SMarcus Folkesson 	if (st7571->grayscale && !constraints->support_grayscale) {
7739b8f3200SMarcus Folkesson 		dev_err(dev, "Grayscale not supported\n");
7749b8f3200SMarcus Folkesson 		return -EINVAL;
7759b8f3200SMarcus Folkesson 	}
7769b8f3200SMarcus Folkesson 
7779b8f3200SMarcus Folkesson 	return 0;
7789b8f3200SMarcus Folkesson }
7799b8f3200SMarcus Folkesson 
st7567_parse_dt(struct st7571_device * st7567)780*a55863baSJavier Martinez Canillas static int st7567_parse_dt(struct st7571_device *st7567)
781*a55863baSJavier Martinez Canillas {
782*a55863baSJavier Martinez Canillas 	struct device *dev = &st7567->client->dev;
783*a55863baSJavier Martinez Canillas 	struct device_node *np = dev->of_node;
784*a55863baSJavier Martinez Canillas 	struct display_timing dt;
785*a55863baSJavier Martinez Canillas 	int ret;
786*a55863baSJavier Martinez Canillas 
787*a55863baSJavier Martinez Canillas 	ret = of_get_display_timing(np, "panel-timing", &dt);
788*a55863baSJavier Martinez Canillas 	if (ret) {
789*a55863baSJavier Martinez Canillas 		dev_err(dev, "Failed to get display timing from DT\n");
790*a55863baSJavier Martinez Canillas 		return ret;
791*a55863baSJavier Martinez Canillas 	}
792*a55863baSJavier Martinez Canillas 
793*a55863baSJavier Martinez Canillas 	of_property_read_u32(np, "width-mm", &st7567->width_mm);
794*a55863baSJavier Martinez Canillas 	of_property_read_u32(np, "height-mm", &st7567->height_mm);
795*a55863baSJavier Martinez Canillas 
796*a55863baSJavier Martinez Canillas 	st7567->pformat = &st7571_monochrome;
797*a55863baSJavier Martinez Canillas 	st7567->bpp = 1;
798*a55863baSJavier Martinez Canillas 
799*a55863baSJavier Martinez Canillas 	st7567->startline = dt.vfront_porch.typ;
800*a55863baSJavier Martinez Canillas 	st7567->nlines = dt.vactive.typ;
801*a55863baSJavier Martinez Canillas 	st7567->ncols = dt.hactive.typ;
802*a55863baSJavier Martinez Canillas 
803*a55863baSJavier Martinez Canillas 	return 0;
804*a55863baSJavier Martinez Canillas }
805*a55863baSJavier Martinez Canillas 
st7571_parse_dt(struct st7571_device * st7571)8069b8f3200SMarcus Folkesson static int st7571_parse_dt(struct st7571_device *st7571)
8079b8f3200SMarcus Folkesson {
8089b8f3200SMarcus Folkesson 	struct device *dev = &st7571->client->dev;
8099b8f3200SMarcus Folkesson 	struct device_node *np = dev->of_node;
8109b8f3200SMarcus Folkesson 	struct display_timing dt;
8119b8f3200SMarcus Folkesson 	int ret;
8129b8f3200SMarcus Folkesson 
8139b8f3200SMarcus Folkesson 	ret = of_get_display_timing(np, "panel-timing", &dt);
8149b8f3200SMarcus Folkesson 	if (ret) {
8159b8f3200SMarcus Folkesson 		dev_err(dev, "Failed to get display timing from DT\n");
8169b8f3200SMarcus Folkesson 		return ret;
8179b8f3200SMarcus Folkesson 	}
8189b8f3200SMarcus Folkesson 
8199b8f3200SMarcus Folkesson 	of_property_read_u32(np, "width-mm", &st7571->width_mm);
8209b8f3200SMarcus Folkesson 	of_property_read_u32(np, "height-mm", &st7571->height_mm);
8219b8f3200SMarcus Folkesson 	st7571->grayscale = of_property_read_bool(np, "sitronix,grayscale");
8229b8f3200SMarcus Folkesson 
8239b8f3200SMarcus Folkesson 	if (st7571->grayscale) {
8249b8f3200SMarcus Folkesson 		st7571->pformat = &st7571_grayscale;
8259b8f3200SMarcus Folkesson 		st7571->bpp = 2;
8269b8f3200SMarcus Folkesson 	} else {
8279b8f3200SMarcus Folkesson 		st7571->pformat = &st7571_monochrome;
8289b8f3200SMarcus Folkesson 		st7571->bpp = 1;
8299b8f3200SMarcus Folkesson 	}
8309b8f3200SMarcus Folkesson 
8319b8f3200SMarcus Folkesson 	st7571->startline = dt.vfront_porch.typ;
8329b8f3200SMarcus Folkesson 	st7571->nlines = dt.vactive.typ;
8339b8f3200SMarcus Folkesson 	st7571->ncols = dt.hactive.typ;
8349b8f3200SMarcus Folkesson 
8359b8f3200SMarcus Folkesson 	st7571->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
8369b8f3200SMarcus Folkesson 	if (IS_ERR(st7571->reset))
837720799d9SJavier Martinez Canillas 		return dev_err_probe(dev, PTR_ERR(st7571->reset),
838720799d9SJavier Martinez Canillas 				     "Failed to get reset gpio\n");
839720799d9SJavier Martinez Canillas 
8409b8f3200SMarcus Folkesson 
8419b8f3200SMarcus Folkesson 	return 0;
8429b8f3200SMarcus Folkesson }
8439b8f3200SMarcus Folkesson 
st7571_reset(struct st7571_device * st7571)8449b8f3200SMarcus Folkesson static void st7571_reset(struct st7571_device *st7571)
8459b8f3200SMarcus Folkesson {
8469b8f3200SMarcus Folkesson 	gpiod_set_value_cansleep(st7571->reset, 1);
8479b8f3200SMarcus Folkesson 	fsleep(20);
8489b8f3200SMarcus Folkesson 	gpiod_set_value_cansleep(st7571->reset, 0);
8499b8f3200SMarcus Folkesson }
8509b8f3200SMarcus Folkesson 
st7567_lcd_init(struct st7571_device * st7567)851*a55863baSJavier Martinez Canillas static int st7567_lcd_init(struct st7571_device *st7567)
852*a55863baSJavier Martinez Canillas {
853*a55863baSJavier Martinez Canillas 	/*
854*a55863baSJavier Martinez Canillas 	 * Most of the initialization sequence is taken directly from the
855*a55863baSJavier Martinez Canillas 	 * referential initial code in the ST7567 datasheet.
856*a55863baSJavier Martinez Canillas 	 */
857*a55863baSJavier Martinez Canillas 	u8 commands[] = {
858*a55863baSJavier Martinez Canillas 		ST7571_DISPLAY_OFF,
859*a55863baSJavier Martinez Canillas 
860*a55863baSJavier Martinez Canillas 		ST7567_SET_LCD_BIAS(1),
861*a55863baSJavier Martinez Canillas 
862*a55863baSJavier Martinez Canillas 		ST7571_SET_SEG_SCAN_DIR(0),
863*a55863baSJavier Martinez Canillas 		ST7571_SET_COM_SCAN_DIR(1),
864*a55863baSJavier Martinez Canillas 
865*a55863baSJavier Martinez Canillas 		ST7571_SET_REGULATOR_REG(4),
866*a55863baSJavier Martinez Canillas 		ST7571_SET_CONTRAST_MSB,
867*a55863baSJavier Martinez Canillas 		ST7571_SET_CONTRAST_LSB(0x20),
868*a55863baSJavier Martinez Canillas 
869*a55863baSJavier Martinez Canillas 		ST7571_SET_START_LINE_MSB,
870*a55863baSJavier Martinez Canillas 		ST7571_SET_START_LINE_LSB(st7567->startline),
871*a55863baSJavier Martinez Canillas 
872*a55863baSJavier Martinez Canillas 		ST7571_SET_POWER(0x4),	/* Power Control, VC: ON, VR: OFF, VF: OFF */
873*a55863baSJavier Martinez Canillas 		ST7571_SET_POWER(0x6),	/* Power Control, VC: ON, VR: ON, VF: OFF */
874*a55863baSJavier Martinez Canillas 		ST7571_SET_POWER(0x7),	/* Power Control, VC: ON, VR: ON, VF: ON */
875*a55863baSJavier Martinez Canillas 
876*a55863baSJavier Martinez Canillas 		ST7571_SET_REVERSE(0),
877*a55863baSJavier Martinez Canillas 		ST7571_SET_ENTIRE_DISPLAY_ON(0),
878*a55863baSJavier Martinez Canillas 	};
879*a55863baSJavier Martinez Canillas 
880*a55863baSJavier Martinez Canillas 	return st7571_send_command_list(st7567, commands, ARRAY_SIZE(commands));
881*a55863baSJavier Martinez Canillas }
882*a55863baSJavier Martinez Canillas 
st7571_lcd_init(struct st7571_device * st7571)8839b8f3200SMarcus Folkesson static int st7571_lcd_init(struct st7571_device *st7571)
8849b8f3200SMarcus Folkesson {
8859b8f3200SMarcus Folkesson 	/*
8869b8f3200SMarcus Folkesson 	 * Most of the initialization sequence is taken directly from the
8879b8f3200SMarcus Folkesson 	 * referential initial code in the ST7571 datasheet.
8889b8f3200SMarcus Folkesson 	 */
8899b8f3200SMarcus Folkesson 	u8 commands[] = {
8909b8f3200SMarcus Folkesson 		ST7571_DISPLAY_OFF,
8919b8f3200SMarcus Folkesson 
8929b8f3200SMarcus Folkesson 		ST7571_SET_MODE_MSB,
8939b8f3200SMarcus Folkesson 		ST7571_SET_MODE_LSB(0x2e),
8949b8f3200SMarcus Folkesson 
8959b8f3200SMarcus Folkesson 		ST7571_SET_SEG_SCAN_DIR(0),
8969b8f3200SMarcus Folkesson 		ST7571_SET_COM_SCAN_DIR(1),
8979b8f3200SMarcus Folkesson 
8989b8f3200SMarcus Folkesson 		ST7571_SET_COM0_MSB,
8999b8f3200SMarcus Folkesson 		ST7571_SET_COM0_LSB(0x00),
9009b8f3200SMarcus Folkesson 
9019b8f3200SMarcus Folkesson 		ST7571_SET_START_LINE_MSB,
9029b8f3200SMarcus Folkesson 		ST7571_SET_START_LINE_LSB(st7571->startline),
9039b8f3200SMarcus Folkesson 
9049b8f3200SMarcus Folkesson 		ST7571_OSC_ON,
9059b8f3200SMarcus Folkesson 		ST7571_SET_REGULATOR_REG(5),
9069b8f3200SMarcus Folkesson 		ST7571_SET_CONTRAST_MSB,
9079b8f3200SMarcus Folkesson 		ST7571_SET_CONTRAST_LSB(0x33),
9089b8f3200SMarcus Folkesson 		ST7571_SET_LCD_BIAS(0x04),
9099b8f3200SMarcus Folkesson 		ST7571_SET_DISPLAY_DUTY_MSB,
9109b8f3200SMarcus Folkesson 		ST7571_SET_DISPLAY_DUTY_LSB(st7571->nlines),
9119b8f3200SMarcus Folkesson 
9129b8f3200SMarcus Folkesson 		ST7571_SET_POWER(0x4),	/* Power Control, VC: ON, VR: OFF, VF: OFF */
9139b8f3200SMarcus Folkesson 		ST7571_SET_POWER(0x6),	/* Power Control, VC: ON, VR: ON, VF: OFF */
9149b8f3200SMarcus Folkesson 		ST7571_SET_POWER(0x7),	/* Power Control, VC: ON, VR: ON, VF: ON */
9159b8f3200SMarcus Folkesson 
9169b8f3200SMarcus Folkesson 		ST7571_COMMAND_SET_3,
9179b8f3200SMarcus Folkesson 		ST7571_SET_COLOR_MODE(st7571->pformat->mode),
9189b8f3200SMarcus Folkesson 		ST7571_COMMAND_SET_NORMAL,
9199b8f3200SMarcus Folkesson 
9209b8f3200SMarcus Folkesson 		ST7571_SET_REVERSE(0),
9219b8f3200SMarcus Folkesson 		ST7571_SET_ENTIRE_DISPLAY_ON(0),
9229b8f3200SMarcus Folkesson 	};
9239b8f3200SMarcus Folkesson 
9249b8f3200SMarcus Folkesson 	/* Perform a reset before initializing the controller */
9259b8f3200SMarcus Folkesson 	st7571_reset(st7571);
9269b8f3200SMarcus Folkesson 
9279b8f3200SMarcus Folkesson 	return st7571_send_command_list(st7571, commands, ARRAY_SIZE(commands));
9289b8f3200SMarcus Folkesson }
9299b8f3200SMarcus Folkesson 
st7571_probe(struct i2c_client * client)9309b8f3200SMarcus Folkesson static int st7571_probe(struct i2c_client *client)
9319b8f3200SMarcus Folkesson {
9329b8f3200SMarcus Folkesson 	struct st7571_device *st7571;
9339b8f3200SMarcus Folkesson 	struct drm_device *dev;
9349b8f3200SMarcus Folkesson 	int ret;
9359b8f3200SMarcus Folkesson 
9369b8f3200SMarcus Folkesson 	st7571 = devm_drm_dev_alloc(&client->dev, &st7571_driver,
9379b8f3200SMarcus Folkesson 				    struct st7571_device, dev);
9389b8f3200SMarcus Folkesson 	if (IS_ERR(st7571))
9399b8f3200SMarcus Folkesson 		return PTR_ERR(st7571);
9409b8f3200SMarcus Folkesson 
9419b8f3200SMarcus Folkesson 	dev = &st7571->dev;
9429b8f3200SMarcus Folkesson 	st7571->client = client;
9439b8f3200SMarcus Folkesson 	i2c_set_clientdata(client, st7571);
9449b8f3200SMarcus Folkesson 	st7571->pdata = device_get_match_data(&client->dev);
9459b8f3200SMarcus Folkesson 
946d9ace6d5SJavier Martinez Canillas 	ret = st7571->pdata->parse_dt(st7571);
9479b8f3200SMarcus Folkesson 	if (ret)
9489b8f3200SMarcus Folkesson 		return ret;
9499b8f3200SMarcus Folkesson 
9509b8f3200SMarcus Folkesson 	ret = st7571_validate_parameters(st7571);
9519b8f3200SMarcus Folkesson 	if (ret)
9529b8f3200SMarcus Folkesson 		return ret;
9539b8f3200SMarcus Folkesson 
9549b8f3200SMarcus Folkesson 	st7571->mode = st7571_mode(st7571);
9559b8f3200SMarcus Folkesson 
9569b8f3200SMarcus Folkesson 	/*
9579b8f3200SMarcus Folkesson 	 * The hardware design could make it hard to detect a NAK on the I2C bus.
9589b8f3200SMarcus Folkesson 	 * If the adapter does not support protocol mangling do
9599b8f3200SMarcus Folkesson 	 * not set the I2C_M_IGNORE_NAK flag at the expense * of possible
9609b8f3200SMarcus Folkesson 	 * cruft in the logs.
9619b8f3200SMarcus Folkesson 	 */
9629b8f3200SMarcus Folkesson 	if (i2c_check_functionality(client->adapter, I2C_FUNC_PROTOCOL_MANGLING))
9639b8f3200SMarcus Folkesson 		st7571->ignore_nak = true;
9649b8f3200SMarcus Folkesson 
9659b8f3200SMarcus Folkesson 	st7571->regmap = devm_regmap_init(&client->dev, &st7571_regmap_bus,
9669b8f3200SMarcus Folkesson 					  client, &st7571_regmap_config);
9679b8f3200SMarcus Folkesson 	if (IS_ERR(st7571->regmap)) {
9689b8f3200SMarcus Folkesson 		return dev_err_probe(&client->dev, PTR_ERR(st7571->regmap),
9699b8f3200SMarcus Folkesson 				     "Failed to initialize regmap\n");
9709b8f3200SMarcus Folkesson 	}
9719b8f3200SMarcus Folkesson 
9729b8f3200SMarcus Folkesson 	st7571->hwbuf = devm_kzalloc(&client->dev,
9739b8f3200SMarcus Folkesson 				     (st7571->nlines * st7571->ncols * st7571->bpp) / 8,
9749b8f3200SMarcus Folkesson 				     GFP_KERNEL);
9759b8f3200SMarcus Folkesson 	if (!st7571->hwbuf)
9769b8f3200SMarcus Folkesson 		return -ENOMEM;
9779b8f3200SMarcus Folkesson 
9789b8f3200SMarcus Folkesson 	st7571->row = devm_kzalloc(&client->dev,
9799b8f3200SMarcus Folkesson 				   (st7571->ncols * st7571->bpp),
9809b8f3200SMarcus Folkesson 				   GFP_KERNEL);
9819b8f3200SMarcus Folkesson 	if (!st7571->row)
9829b8f3200SMarcus Folkesson 		return -ENOMEM;
9839b8f3200SMarcus Folkesson 
9849b8f3200SMarcus Folkesson 	ret = st7571_mode_config_init(st7571);
9859b8f3200SMarcus Folkesson 	if (ret)
9869b8f3200SMarcus Folkesson 		return dev_err_probe(&client->dev, ret,
9879b8f3200SMarcus Folkesson 				     "Failed to initialize mode config\n");
9889b8f3200SMarcus Folkesson 
9899b8f3200SMarcus Folkesson 	ret = st7571_plane_init(st7571, st7571->pformat);
9909b8f3200SMarcus Folkesson 	if (ret)
9919b8f3200SMarcus Folkesson 		return dev_err_probe(&client->dev, ret,
9929b8f3200SMarcus Folkesson 				     "Failed to initialize primary plane\n");
9939b8f3200SMarcus Folkesson 
9949b8f3200SMarcus Folkesson 	ret = st7571_crtc_init(st7571);
9959b8f3200SMarcus Folkesson 	if (ret < 0)
9969b8f3200SMarcus Folkesson 		return dev_err_probe(&client->dev, ret,
9979b8f3200SMarcus Folkesson 				     "Failed to initialize CRTC\n");
9989b8f3200SMarcus Folkesson 
9999b8f3200SMarcus Folkesson 	ret = st7571_encoder_init(st7571);
10009b8f3200SMarcus Folkesson 	if (ret < 0)
10019b8f3200SMarcus Folkesson 		return dev_err_probe(&client->dev, ret,
10029b8f3200SMarcus Folkesson 				     "Failed to initialize encoder\n");
10039b8f3200SMarcus Folkesson 
10049b8f3200SMarcus Folkesson 	ret = st7571_connector_init(st7571);
10059b8f3200SMarcus Folkesson 	if (ret < 0)
10069b8f3200SMarcus Folkesson 		return dev_err_probe(&client->dev, ret,
10079b8f3200SMarcus Folkesson 				     "Failed to initialize connector\n");
10089b8f3200SMarcus Folkesson 
10099b8f3200SMarcus Folkesson 	drm_mode_config_reset(dev);
10109b8f3200SMarcus Folkesson 
10119b8f3200SMarcus Folkesson 	ret = drm_dev_register(dev, 0);
10129b8f3200SMarcus Folkesson 	if (ret)
10139b8f3200SMarcus Folkesson 		return dev_err_probe(&client->dev, ret,
10149b8f3200SMarcus Folkesson 				     "Failed to register DRM device\n");
10159b8f3200SMarcus Folkesson 
10169b8f3200SMarcus Folkesson 	drm_client_setup(dev, NULL);
10179b8f3200SMarcus Folkesson 	return 0;
10189b8f3200SMarcus Folkesson }
10199b8f3200SMarcus Folkesson 
st7571_remove(struct i2c_client * client)10209b8f3200SMarcus Folkesson static void st7571_remove(struct i2c_client *client)
10219b8f3200SMarcus Folkesson {
10229b8f3200SMarcus Folkesson 	struct st7571_device *st7571 = i2c_get_clientdata(client);
10239b8f3200SMarcus Folkesson 
10249b8f3200SMarcus Folkesson 	drm_dev_unplug(&st7571->dev);
10259b8f3200SMarcus Folkesson }
10269b8f3200SMarcus Folkesson 
1027*a55863baSJavier Martinez Canillas struct st7571_panel_data st7567_config = {
1028*a55863baSJavier Martinez Canillas 	.init = st7567_lcd_init,
1029*a55863baSJavier Martinez Canillas 	.parse_dt = st7567_parse_dt,
1030*a55863baSJavier Martinez Canillas 	.constraints = {
1031*a55863baSJavier Martinez Canillas 		.min_nlines = 1,
1032*a55863baSJavier Martinez Canillas 		.max_nlines = 64,
1033*a55863baSJavier Martinez Canillas 		.min_ncols = 128,
1034*a55863baSJavier Martinez Canillas 		.max_ncols = 128,
1035*a55863baSJavier Martinez Canillas 		.support_grayscale = false,
1036*a55863baSJavier Martinez Canillas 	},
1037*a55863baSJavier Martinez Canillas };
1038*a55863baSJavier Martinez Canillas 
10399b8f3200SMarcus Folkesson struct st7571_panel_data st7571_config = {
10409b8f3200SMarcus Folkesson 	.init = st7571_lcd_init,
1041d9ace6d5SJavier Martinez Canillas 	.parse_dt = st7571_parse_dt,
10429b8f3200SMarcus Folkesson 	.constraints = {
10439b8f3200SMarcus Folkesson 		.min_nlines = 1,
10449b8f3200SMarcus Folkesson 		.max_nlines = 128,
10459b8f3200SMarcus Folkesson 		.min_ncols = 128,
10469b8f3200SMarcus Folkesson 		.max_ncols = 128,
10479b8f3200SMarcus Folkesson 		.support_grayscale = true,
10489b8f3200SMarcus Folkesson 	},
10499b8f3200SMarcus Folkesson };
10509b8f3200SMarcus Folkesson 
10519b8f3200SMarcus Folkesson static const struct of_device_id st7571_of_match[] = {
1052*a55863baSJavier Martinez Canillas 	{ .compatible = "sitronix,st7567", .data = &st7567_config },
10539b8f3200SMarcus Folkesson 	{ .compatible = "sitronix,st7571", .data = &st7571_config },
10549b8f3200SMarcus Folkesson 	{},
10559b8f3200SMarcus Folkesson };
10569b8f3200SMarcus Folkesson MODULE_DEVICE_TABLE(of, st7571_of_match);
10579b8f3200SMarcus Folkesson 
10589b8f3200SMarcus Folkesson static const struct i2c_device_id st7571_id[] = {
1059*a55863baSJavier Martinez Canillas 	{ "st7567", 0 },
10609b8f3200SMarcus Folkesson 	{ "st7571", 0 },
10619b8f3200SMarcus Folkesson 	{ }
10629b8f3200SMarcus Folkesson };
10639b8f3200SMarcus Folkesson MODULE_DEVICE_TABLE(i2c, st7571_id);
10649b8f3200SMarcus Folkesson 
10659b8f3200SMarcus Folkesson static struct i2c_driver st7571_i2c_driver = {
10669b8f3200SMarcus Folkesson 	.driver = {
10679b8f3200SMarcus Folkesson 		.name = "st7571",
10689b8f3200SMarcus Folkesson 		.of_match_table = st7571_of_match,
10699b8f3200SMarcus Folkesson 	},
10709b8f3200SMarcus Folkesson 	.probe = st7571_probe,
10719b8f3200SMarcus Folkesson 	.remove = st7571_remove,
10729b8f3200SMarcus Folkesson 	.id_table = st7571_id,
10739b8f3200SMarcus Folkesson };
10749b8f3200SMarcus Folkesson 
10759b8f3200SMarcus Folkesson module_i2c_driver(st7571_i2c_driver);
10769b8f3200SMarcus Folkesson 
10779b8f3200SMarcus Folkesson MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>");
10789b8f3200SMarcus Folkesson MODULE_DESCRIPTION("DRM Driver for Sitronix ST7571 LCD controller");
10799b8f3200SMarcus Folkesson MODULE_LICENSE("GPL");
1080