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