1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2018 IBM Corporation
3 
4 #include <linux/clk.h>
5 #include <linux/dma-mapping.h>
6 #include <linux/irq.h>
7 #include <linux/mfd/syscon.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_device.h>
11 #include <linux/of_reserved_mem.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/reset.h>
15 
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_device.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <drm/drm_module.h>
24 #include <drm/drm_probe_helper.h>
25 #include <drm/drm_simple_kms_helper.h>
26 #include <drm/drm_vblank.h>
27 #include <drm/drm_drv.h>
28 
29 #include "aspeed_gfx.h"
30 
31 /**
32  * DOC: ASPEED GFX Driver
33  *
34  * This driver is for the ASPEED BMC SoC's 'GFX' display hardware, also called
35  * the 'SOC Display Controller' in the datasheet. This driver runs on the ARM
36  * based BMC systems, unlike the ast driver which runs on a host CPU and is for
37  * a PCIe graphics device.
38  *
39  * The AST2500 supports a total of 3 output paths:
40  *
41  *   1. VGA output, the output target can choose either or both to the DAC
42  *   or DVO interface.
43  *
44  *   2. Graphics CRT output, the output target can choose either or both to
45  *   the DAC or DVO interface.
46  *
47  *   3. Video input from DVO, the video input can be used for video engine
48  *   capture or DAC display output.
49  *
50  * Output options are selected in SCU2C.
51  *
52  * The "VGA mode" device is the PCI attached controller. The "Graphics CRT"
53  * is the ARM's internal display controller.
54  *
55  * The driver only supports a simple configuration consisting of a 40MHz
56  * pixel clock, fixed by hardware limitations, and the VGA output path.
57  *
58  * The driver was written with the 'AST2500 Software Programming Guide' v17,
59  * which is available under NDA from ASPEED.
60  */
61 
62 struct aspeed_gfx_config {
63 	u32 dac_reg;		/* DAC register in SCU */
64 	u32 vga_scratch_reg;	/* VGA scratch register in SCU */
65 	u32 throd_val;		/* Default Threshold Seting */
66 	u32 scan_line_max;	/* Max memory size of one scan line */
67 };
68 
69 static const struct aspeed_gfx_config ast2400_config = {
70 	.dac_reg = 0x2c,
71 	.vga_scratch_reg = 0x50,
72 	.throd_val = CRT_THROD_LOW(0x1e) | CRT_THROD_HIGH(0x12),
73 	.scan_line_max = 64,
74 };
75 
76 static const struct aspeed_gfx_config ast2500_config = {
77 	.dac_reg = 0x2c,
78 	.vga_scratch_reg = 0x50,
79 	.throd_val = CRT_THROD_LOW(0x24) | CRT_THROD_HIGH(0x3c),
80 	.scan_line_max = 128,
81 };
82 
83 static const struct of_device_id aspeed_gfx_match[] = {
84 	{ .compatible = "aspeed,ast2400-gfx", .data = &ast2400_config },
85 	{ .compatible = "aspeed,ast2500-gfx", .data = &ast2500_config },
86 	{ },
87 };
88 MODULE_DEVICE_TABLE(of, aspeed_gfx_match);
89 
90 static const struct drm_mode_config_funcs aspeed_gfx_mode_config_funcs = {
91 	.fb_create		= drm_gem_fb_create,
92 	.atomic_check		= drm_atomic_helper_check,
93 	.atomic_commit		= drm_atomic_helper_commit,
94 };
95 
96 static int aspeed_gfx_setup_mode_config(struct drm_device *drm)
97 {
98 	int ret;
99 
100 	ret = drmm_mode_config_init(drm);
101 	if (ret)
102 		return ret;
103 
104 	drm->mode_config.min_width = 0;
105 	drm->mode_config.min_height = 0;
106 	drm->mode_config.max_width = 800;
107 	drm->mode_config.max_height = 600;
108 	drm->mode_config.funcs = &aspeed_gfx_mode_config_funcs;
109 
110 	return ret;
111 }
112 
113 static irqreturn_t aspeed_gfx_irq_handler(int irq, void *data)
114 {
115 	struct drm_device *drm = data;
116 	struct aspeed_gfx *priv = to_aspeed_gfx(drm);
117 	u32 reg;
118 
119 	reg = readl(priv->base + CRT_CTRL1);
120 
121 	if (reg & CRT_CTRL_VERTICAL_INTR_STS) {
122 		drm_crtc_handle_vblank(&priv->pipe.crtc);
123 		writel(reg, priv->base + CRT_CTRL1);
124 		return IRQ_HANDLED;
125 	}
126 
127 	return IRQ_NONE;
128 }
129 
130 static int aspeed_gfx_load(struct drm_device *drm)
131 {
132 	struct platform_device *pdev = to_platform_device(drm->dev);
133 	struct aspeed_gfx *priv = to_aspeed_gfx(drm);
134 	struct device_node *np = pdev->dev.of_node;
135 	const struct aspeed_gfx_config *config;
136 	const struct of_device_id *match;
137 	struct resource *res;
138 	int ret;
139 
140 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
141 	priv->base = devm_ioremap_resource(drm->dev, res);
142 	if (IS_ERR(priv->base))
143 		return PTR_ERR(priv->base);
144 
145 	match = of_match_device(aspeed_gfx_match, &pdev->dev);
146 	if (!match)
147 		return -EINVAL;
148 	config = match->data;
149 
150 	priv->dac_reg = config->dac_reg;
151 	priv->vga_scratch_reg = config->vga_scratch_reg;
152 	priv->throd_val = config->throd_val;
153 	priv->scan_line_max = config->scan_line_max;
154 
155 	priv->scu = syscon_regmap_lookup_by_phandle(np, "syscon");
156 	if (IS_ERR(priv->scu)) {
157 		priv->scu = syscon_regmap_lookup_by_compatible("aspeed,ast2500-scu");
158 		if (IS_ERR(priv->scu)) {
159 			dev_err(&pdev->dev, "failed to find SCU regmap\n");
160 			return PTR_ERR(priv->scu);
161 		}
162 	}
163 
164 	ret = of_reserved_mem_device_init(drm->dev);
165 	if (ret) {
166 		dev_err(&pdev->dev,
167 			"failed to initialize reserved mem: %d\n", ret);
168 		return ret;
169 	}
170 
171 	ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
172 	if (ret) {
173 		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", ret);
174 		return ret;
175 	}
176 
177 	priv->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
178 	if (IS_ERR(priv->rst)) {
179 		dev_err(&pdev->dev,
180 			"missing or invalid reset controller device tree entry");
181 		return PTR_ERR(priv->rst);
182 	}
183 	reset_control_deassert(priv->rst);
184 
185 	priv->clk = devm_clk_get(drm->dev, NULL);
186 	if (IS_ERR(priv->clk)) {
187 		dev_err(&pdev->dev,
188 			"missing or invalid clk device tree entry");
189 		return PTR_ERR(priv->clk);
190 	}
191 	clk_prepare_enable(priv->clk);
192 
193 	/* Sanitize control registers */
194 	writel(0, priv->base + CRT_CTRL1);
195 	writel(0, priv->base + CRT_CTRL2);
196 
197 	ret = aspeed_gfx_setup_mode_config(drm);
198 	if (ret < 0)
199 		return ret;
200 
201 	ret = drm_vblank_init(drm, 1);
202 	if (ret < 0) {
203 		dev_err(drm->dev, "Failed to initialise vblank\n");
204 		return ret;
205 	}
206 
207 	ret = aspeed_gfx_create_output(drm);
208 	if (ret < 0) {
209 		dev_err(drm->dev, "Failed to create outputs\n");
210 		return ret;
211 	}
212 
213 	ret = aspeed_gfx_create_pipe(drm);
214 	if (ret < 0) {
215 		dev_err(drm->dev, "Cannot setup simple display pipe\n");
216 		return ret;
217 	}
218 
219 	ret = devm_request_irq(drm->dev, platform_get_irq(pdev, 0),
220 			       aspeed_gfx_irq_handler, 0, "aspeed gfx", drm);
221 	if (ret < 0) {
222 		dev_err(drm->dev, "Failed to install IRQ handler\n");
223 		return ret;
224 	}
225 
226 	drm_mode_config_reset(drm);
227 
228 	return 0;
229 }
230 
231 static void aspeed_gfx_unload(struct drm_device *drm)
232 {
233 	drm_kms_helper_poll_fini(drm);
234 }
235 
236 DEFINE_DRM_GEM_CMA_FOPS(fops);
237 
238 static const struct drm_driver aspeed_gfx_driver = {
239 	.driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
240 	DRM_GEM_CMA_DRIVER_OPS,
241 	.fops = &fops,
242 	.name = "aspeed-gfx-drm",
243 	.desc = "ASPEED GFX DRM",
244 	.date = "20180319",
245 	.major = 1,
246 	.minor = 0,
247 };
248 
249 static ssize_t dac_mux_store(struct device *dev, struct device_attribute *attr,
250 			     const char *buf, size_t count)
251 {
252 	struct aspeed_gfx *priv = dev_get_drvdata(dev);
253 	u32 val;
254 	int rc;
255 
256 	rc = kstrtou32(buf, 0, &val);
257 	if (rc)
258 		return rc;
259 
260 	if (val > 3)
261 		return -EINVAL;
262 
263 	rc = regmap_update_bits(priv->scu, priv->dac_reg, 0x30000, val << 16);
264 	if (rc < 0)
265 		return 0;
266 
267 	return count;
268 }
269 
270 static ssize_t dac_mux_show(struct device *dev, struct device_attribute *attr, char *buf)
271 {
272 	struct aspeed_gfx *priv = dev_get_drvdata(dev);
273 	u32 reg;
274 	int rc;
275 
276 	rc = regmap_read(priv->scu, priv->dac_reg, &reg);
277 	if (rc)
278 		return rc;
279 
280 	return sprintf(buf, "%u\n", (reg >> 16) & 0x3);
281 }
282 static DEVICE_ATTR_RW(dac_mux);
283 
284 static ssize_t
285 vga_pw_show(struct device *dev, struct device_attribute *attr, char *buf)
286 {
287 	struct aspeed_gfx *priv = dev_get_drvdata(dev);
288 	u32 reg;
289 	int rc;
290 
291 	rc = regmap_read(priv->scu, priv->vga_scratch_reg, &reg);
292 	if (rc)
293 		return rc;
294 
295 	return sprintf(buf, "%u\n", reg);
296 }
297 static DEVICE_ATTR_RO(vga_pw);
298 
299 static struct attribute *aspeed_sysfs_entries[] = {
300 	&dev_attr_vga_pw.attr,
301 	&dev_attr_dac_mux.attr,
302 	NULL,
303 };
304 
305 static struct attribute_group aspeed_sysfs_attr_group = {
306 	.attrs = aspeed_sysfs_entries,
307 };
308 
309 static int aspeed_gfx_probe(struct platform_device *pdev)
310 {
311 	struct aspeed_gfx *priv;
312 	int ret;
313 
314 	priv = devm_drm_dev_alloc(&pdev->dev, &aspeed_gfx_driver,
315 				  struct aspeed_gfx, drm);
316 	if (IS_ERR(priv))
317 		return PTR_ERR(priv);
318 
319 	ret = aspeed_gfx_load(&priv->drm);
320 	if (ret)
321 		return ret;
322 
323 	platform_set_drvdata(pdev, priv);
324 
325 	ret = sysfs_create_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group);
326 	if (ret)
327 		return ret;
328 
329 	ret = drm_dev_register(&priv->drm, 0);
330 	if (ret)
331 		goto err_unload;
332 
333 	drm_fbdev_generic_setup(&priv->drm, 32);
334 	return 0;
335 
336 err_unload:
337 	sysfs_remove_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group);
338 	aspeed_gfx_unload(&priv->drm);
339 
340 	return ret;
341 }
342 
343 static int aspeed_gfx_remove(struct platform_device *pdev)
344 {
345 	struct drm_device *drm = platform_get_drvdata(pdev);
346 
347 	sysfs_remove_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group);
348 	drm_dev_unregister(drm);
349 	aspeed_gfx_unload(drm);
350 
351 	return 0;
352 }
353 
354 static struct platform_driver aspeed_gfx_platform_driver = {
355 	.probe		= aspeed_gfx_probe,
356 	.remove		= aspeed_gfx_remove,
357 	.driver = {
358 		.name = "aspeed_gfx",
359 		.of_match_table = aspeed_gfx_match,
360 	},
361 };
362 
363 drm_module_platform_driver(aspeed_gfx_platform_driver);
364 
365 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
366 MODULE_DESCRIPTION("ASPEED BMC DRM/KMS driver");
367 MODULE_LICENSE("GPL");
368