xref: /openbmc/linux/drivers/gpu/drm/imx/dcss/dcss-drv.c (revision 5b4fc395)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 NXP.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <drm/drm_module.h>
11 #include <drm/drm_of.h>
12 
13 #include "dcss-dev.h"
14 #include "dcss-kms.h"
15 
16 struct dcss_drv {
17 	struct dcss_dev *dcss;
18 	struct dcss_kms_dev *kms;
19 };
20 
21 struct dcss_dev *dcss_drv_dev_to_dcss(struct device *dev)
22 {
23 	struct dcss_drv *mdrv = dev_get_drvdata(dev);
24 
25 	return mdrv ? mdrv->dcss : NULL;
26 }
27 
28 struct drm_device *dcss_drv_dev_to_drm(struct device *dev)
29 {
30 	struct dcss_drv *mdrv = dev_get_drvdata(dev);
31 
32 	return mdrv ? &mdrv->kms->base : NULL;
33 }
34 
35 static int dcss_drv_platform_probe(struct platform_device *pdev)
36 {
37 	struct device *dev = &pdev->dev;
38 	struct device_node *remote;
39 	struct dcss_drv *mdrv;
40 	int err = 0;
41 	bool hdmi_output = true;
42 
43 	if (!dev->of_node)
44 		return -ENODEV;
45 
46 	remote = of_graph_get_remote_node(dev->of_node, 0, 0);
47 	if (!remote)
48 		return -ENODEV;
49 
50 	hdmi_output = !of_device_is_compatible(remote, "fsl,imx8mq-nwl-dsi");
51 
52 	of_node_put(remote);
53 
54 	mdrv = kzalloc(sizeof(*mdrv), GFP_KERNEL);
55 	if (!mdrv)
56 		return -ENOMEM;
57 
58 	mdrv->dcss = dcss_dev_create(dev, hdmi_output);
59 	if (IS_ERR(mdrv->dcss)) {
60 		err = PTR_ERR(mdrv->dcss);
61 		goto err;
62 	}
63 
64 	dev_set_drvdata(dev, mdrv);
65 
66 	mdrv->kms = dcss_kms_attach(mdrv->dcss);
67 	if (IS_ERR(mdrv->kms)) {
68 		err = PTR_ERR(mdrv->kms);
69 		goto dcss_shutoff;
70 	}
71 
72 	return 0;
73 
74 dcss_shutoff:
75 	dcss_dev_destroy(mdrv->dcss);
76 
77 	dev_set_drvdata(dev, NULL);
78 
79 err:
80 	kfree(mdrv);
81 	return err;
82 }
83 
84 static int dcss_drv_platform_remove(struct platform_device *pdev)
85 {
86 	struct dcss_drv *mdrv = dev_get_drvdata(&pdev->dev);
87 
88 	if (!mdrv)
89 		return 0;
90 
91 	dcss_kms_detach(mdrv->kms);
92 	dcss_dev_destroy(mdrv->dcss);
93 
94 	dev_set_drvdata(&pdev->dev, NULL);
95 
96 	kfree(mdrv);
97 
98 	return 0;
99 }
100 
101 static struct dcss_type_data dcss_types[] = {
102 	[DCSS_IMX8MQ] = {
103 		.name = "DCSS_IMX8MQ",
104 		.blkctl_ofs = 0x2F000,
105 		.ctxld_ofs = 0x23000,
106 		.dtg_ofs = 0x20000,
107 		.scaler_ofs = 0x1C000,
108 		.ss_ofs = 0x1B000,
109 		.dpr_ofs = 0x18000,
110 	},
111 };
112 
113 static const struct of_device_id dcss_of_match[] = {
114 	{ .compatible = "nxp,imx8mq-dcss", .data = &dcss_types[DCSS_IMX8MQ], },
115 	{},
116 };
117 
118 MODULE_DEVICE_TABLE(of, dcss_of_match);
119 
120 static const struct dev_pm_ops dcss_dev_pm = {
121 	SET_SYSTEM_SLEEP_PM_OPS(dcss_dev_suspend, dcss_dev_resume)
122 	SET_RUNTIME_PM_OPS(dcss_dev_runtime_suspend,
123 			   dcss_dev_runtime_resume, NULL)
124 };
125 
126 static struct platform_driver dcss_platform_driver = {
127 	.probe	= dcss_drv_platform_probe,
128 	.remove	= dcss_drv_platform_remove,
129 	.driver	= {
130 		.name = "imx-dcss",
131 		.of_match_table	= dcss_of_match,
132 		.pm = &dcss_dev_pm,
133 	},
134 };
135 
136 drm_module_platform_driver(dcss_platform_driver);
137 
138 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@nxp.com>");
139 MODULE_DESCRIPTION("DCSS driver for i.MX8MQ");
140 MODULE_LICENSE("GPL v2");
141