1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Sharp LS037V7DW01 LCD Panel Driver
4  *
5  * Copyright (C) 2019 Texas Instruments Incorporated
6  *
7  * Based on the omapdrm-specific panel-sharp-ls037v7dw01 driver
8  *
9  * Copyright (C) 2013 Texas Instruments Incorporated
10  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 
20 #include <drm/drm_connector.h>
21 #include <drm/drm_modes.h>
22 #include <drm/drm_panel.h>
23 
24 struct ls037v7dw01_panel {
25 	struct drm_panel panel;
26 	struct platform_device *pdev;
27 
28 	struct regulator *vdd;
29 	struct gpio_desc *resb_gpio;	/* low = reset active min 20 us */
30 	struct gpio_desc *ini_gpio;	/* high = power on */
31 	struct gpio_desc *mo_gpio;	/* low = 480x640, high = 240x320 */
32 	struct gpio_desc *lr_gpio;	/* high = conventional horizontal scanning */
33 	struct gpio_desc *ud_gpio;	/* high = conventional vertical scanning */
34 };
35 
36 #define to_ls037v7dw01_device(p) \
37 	container_of(p, struct ls037v7dw01_panel, panel)
38 
39 static int ls037v7dw01_disable(struct drm_panel *panel)
40 {
41 	struct ls037v7dw01_panel *lcd = to_ls037v7dw01_device(panel);
42 
43 	gpiod_set_value_cansleep(lcd->ini_gpio, 0);
44 	gpiod_set_value_cansleep(lcd->resb_gpio, 0);
45 
46 	/* Wait at least 5 vsyncs after disabling the LCD. */
47 	msleep(100);
48 
49 	return 0;
50 }
51 
52 static int ls037v7dw01_unprepare(struct drm_panel *panel)
53 {
54 	struct ls037v7dw01_panel *lcd = to_ls037v7dw01_device(panel);
55 
56 	regulator_disable(lcd->vdd);
57 	return 0;
58 }
59 
60 static int ls037v7dw01_prepare(struct drm_panel *panel)
61 {
62 	struct ls037v7dw01_panel *lcd = to_ls037v7dw01_device(panel);
63 	int ret;
64 
65 	ret = regulator_enable(lcd->vdd);
66 	if (ret < 0)
67 		dev_err(&lcd->pdev->dev, "%s: failed to enable regulator\n",
68 			__func__);
69 
70 	return ret;
71 }
72 
73 static int ls037v7dw01_enable(struct drm_panel *panel)
74 {
75 	struct ls037v7dw01_panel *lcd = to_ls037v7dw01_device(panel);
76 
77 	/* Wait couple of vsyncs before enabling the LCD. */
78 	msleep(50);
79 
80 	gpiod_set_value_cansleep(lcd->resb_gpio, 1);
81 	gpiod_set_value_cansleep(lcd->ini_gpio, 1);
82 
83 	return 0;
84 }
85 
86 static const struct drm_display_mode ls037v7dw01_mode = {
87 	.clock = 19200,
88 	.hdisplay = 480,
89 	.hsync_start = 480 + 1,
90 	.hsync_end = 480 + 1 + 2,
91 	.htotal = 480 + 1 + 2 + 28,
92 	.vdisplay = 640,
93 	.vsync_start = 640 + 1,
94 	.vsync_end = 640 + 1 + 1,
95 	.vtotal = 640 + 1 + 1 + 1,
96 	.vrefresh = 58,
97 	.type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
98 	.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
99 	.width_mm = 56,
100 	.height_mm = 75,
101 };
102 
103 static int ls037v7dw01_get_modes(struct drm_panel *panel)
104 {
105 	struct drm_connector *connector = panel->connector;
106 	struct drm_display_mode *mode;
107 
108 	mode = drm_mode_duplicate(panel->drm, &ls037v7dw01_mode);
109 	if (!mode)
110 		return -ENOMEM;
111 
112 	drm_mode_set_name(mode);
113 	drm_mode_probed_add(connector, mode);
114 
115 	connector->display_info.width_mm = ls037v7dw01_mode.width_mm;
116 	connector->display_info.height_mm = ls037v7dw01_mode.height_mm;
117 	/*
118 	 * FIXME: According to the datasheet pixel data is sampled on the
119 	 * rising edge of the clock, but the code running on the SDP3430
120 	 * indicates sampling on the negative edge. This should be tested on a
121 	 * real device.
122 	 */
123 	connector->display_info.bus_flags = DRM_BUS_FLAG_DE_HIGH
124 					  | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE
125 					  | DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
126 
127 	return 1;
128 }
129 
130 static const struct drm_panel_funcs ls037v7dw01_funcs = {
131 	.disable = ls037v7dw01_disable,
132 	.unprepare = ls037v7dw01_unprepare,
133 	.prepare = ls037v7dw01_prepare,
134 	.enable = ls037v7dw01_enable,
135 	.get_modes = ls037v7dw01_get_modes,
136 };
137 
138 static int ls037v7dw01_probe(struct platform_device *pdev)
139 {
140 	struct ls037v7dw01_panel *lcd;
141 
142 	lcd = devm_kzalloc(&pdev->dev, sizeof(*lcd), GFP_KERNEL);
143 	if (!lcd)
144 		return -ENOMEM;
145 
146 	platform_set_drvdata(pdev, lcd);
147 	lcd->pdev = pdev;
148 
149 	lcd->vdd = devm_regulator_get(&pdev->dev, "envdd");
150 	if (IS_ERR(lcd->vdd)) {
151 		dev_err(&pdev->dev, "failed to get regulator\n");
152 		return PTR_ERR(lcd->vdd);
153 	}
154 
155 	lcd->ini_gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
156 	if (IS_ERR(lcd->ini_gpio)) {
157 		dev_err(&pdev->dev, "failed to get enable gpio\n");
158 		return PTR_ERR(lcd->ini_gpio);
159 	}
160 
161 	lcd->resb_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
162 	if (IS_ERR(lcd->resb_gpio)) {
163 		dev_err(&pdev->dev, "failed to get reset gpio\n");
164 		return PTR_ERR(lcd->resb_gpio);
165 	}
166 
167 	lcd->mo_gpio = devm_gpiod_get_index(&pdev->dev, "mode", 0,
168 					    GPIOD_OUT_LOW);
169 	if (IS_ERR(lcd->mo_gpio)) {
170 		dev_err(&pdev->dev, "failed to get mode[0] gpio\n");
171 		return PTR_ERR(lcd->mo_gpio);
172 	}
173 
174 	lcd->lr_gpio = devm_gpiod_get_index(&pdev->dev, "mode", 1,
175 					    GPIOD_OUT_LOW);
176 	if (IS_ERR(lcd->lr_gpio)) {
177 		dev_err(&pdev->dev, "failed to get mode[1] gpio\n");
178 		return PTR_ERR(lcd->lr_gpio);
179 	}
180 
181 	lcd->ud_gpio = devm_gpiod_get_index(&pdev->dev, "mode", 2,
182 					    GPIOD_OUT_LOW);
183 	if (IS_ERR(lcd->ud_gpio)) {
184 		dev_err(&pdev->dev, "failed to get mode[2] gpio\n");
185 		return PTR_ERR(lcd->ud_gpio);
186 	}
187 
188 	drm_panel_init(&lcd->panel, &pdev->dev, &ls037v7dw01_funcs,
189 		       DRM_MODE_CONNECTOR_DPI);
190 
191 	return drm_panel_add(&lcd->panel);
192 }
193 
194 static int ls037v7dw01_remove(struct platform_device *pdev)
195 {
196 	struct ls037v7dw01_panel *lcd = platform_get_drvdata(pdev);
197 
198 	drm_panel_remove(&lcd->panel);
199 	drm_panel_disable(&lcd->panel);
200 	drm_panel_unprepare(&lcd->panel);
201 
202 	return 0;
203 }
204 
205 static const struct of_device_id ls037v7dw01_of_match[] = {
206 	{ .compatible = "sharp,ls037v7dw01", },
207 	{ /* sentinel */ },
208 };
209 
210 MODULE_DEVICE_TABLE(of, ls037v7dw01_of_match);
211 
212 static struct platform_driver ls037v7dw01_driver = {
213 	.probe		= ls037v7dw01_probe,
214 	.remove		= ls037v7dw01_remove,
215 	.driver		= {
216 		.name = "panel-sharp-ls037v7dw01",
217 		.of_match_table = ls037v7dw01_of_match,
218 	},
219 };
220 
221 module_platform_driver(ls037v7dw01_driver);
222 
223 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
224 MODULE_DESCRIPTION("Sharp LS037V7DW01 Panel Driver");
225 MODULE_LICENSE("GPL");
226