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