1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4  * Author: James.Qian.Wang <james.qian.wang@arm.com>
5  *
6  */
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/platform_device.h>
10 #include <linux/component.h>
11 #include <linux/pm_runtime.h>
12 #include <drm/drm_of.h>
13 #include "komeda_dev.h"
14 #include "komeda_kms.h"
15 
16 struct komeda_drv {
17 	struct komeda_dev *mdev;
18 	struct komeda_kms_dev *kms;
19 };
20 
21 struct komeda_dev *dev_to_mdev(struct device *dev)
22 {
23 	struct komeda_drv *mdrv = dev_get_drvdata(dev);
24 
25 	return mdrv ? mdrv->mdev : NULL;
26 }
27 
28 static void komeda_unbind(struct device *dev)
29 {
30 	struct komeda_drv *mdrv = dev_get_drvdata(dev);
31 
32 	if (!mdrv)
33 		return;
34 
35 	komeda_kms_detach(mdrv->kms);
36 	komeda_dev_destroy(mdrv->mdev);
37 
38 	dev_set_drvdata(dev, NULL);
39 	devm_kfree(dev, mdrv);
40 }
41 
42 static int komeda_bind(struct device *dev)
43 {
44 	struct komeda_drv *mdrv;
45 	int err;
46 
47 	mdrv = devm_kzalloc(dev, sizeof(*mdrv), GFP_KERNEL);
48 	if (!mdrv)
49 		return -ENOMEM;
50 
51 	mdrv->mdev = komeda_dev_create(dev);
52 	if (IS_ERR(mdrv->mdev)) {
53 		err = PTR_ERR(mdrv->mdev);
54 		goto free_mdrv;
55 	}
56 
57 	mdrv->kms = komeda_kms_attach(mdrv->mdev);
58 	if (IS_ERR(mdrv->kms)) {
59 		err = PTR_ERR(mdrv->kms);
60 		goto destroy_mdev;
61 	}
62 
63 	dev_set_drvdata(dev, mdrv);
64 
65 	return 0;
66 
67 destroy_mdev:
68 	komeda_dev_destroy(mdrv->mdev);
69 
70 free_mdrv:
71 	devm_kfree(dev, mdrv);
72 	return err;
73 }
74 
75 static const struct component_master_ops komeda_master_ops = {
76 	.bind	= komeda_bind,
77 	.unbind	= komeda_unbind,
78 };
79 
80 static int compare_of(struct device *dev, void *data)
81 {
82 	return dev->of_node == data;
83 }
84 
85 static void komeda_add_slave(struct device *master,
86 			     struct component_match **match,
87 			     struct device_node *np,
88 			     u32 port, u32 endpoint)
89 {
90 	struct device_node *remote;
91 
92 	remote = of_graph_get_remote_node(np, port, endpoint);
93 	if (remote) {
94 		drm_of_component_match_add(master, match, compare_of, remote);
95 		of_node_put(remote);
96 	}
97 }
98 
99 static int komeda_platform_probe(struct platform_device *pdev)
100 {
101 	struct device *dev = &pdev->dev;
102 	struct component_match *match = NULL;
103 	struct device_node *child;
104 
105 	if (!dev->of_node)
106 		return -ENODEV;
107 
108 	for_each_available_child_of_node(dev->of_node, child) {
109 		if (of_node_cmp(child->name, "pipeline") != 0)
110 			continue;
111 
112 		/* add connector */
113 		komeda_add_slave(dev, &match, child, KOMEDA_OF_PORT_OUTPUT, 0);
114 		komeda_add_slave(dev, &match, child, KOMEDA_OF_PORT_OUTPUT, 1);
115 	}
116 
117 	return component_master_add_with_match(dev, &komeda_master_ops, match);
118 }
119 
120 static int komeda_platform_remove(struct platform_device *pdev)
121 {
122 	component_master_del(&pdev->dev, &komeda_master_ops);
123 	return 0;
124 }
125 
126 static const struct komeda_product_data komeda_products[] = {
127 	[MALI_D71] = {
128 		.product_id = MALIDP_D71_PRODUCT_ID,
129 		.identify = d71_identify,
130 	},
131 };
132 
133 static const struct of_device_id komeda_of_match[] = {
134 	{ .compatible = "arm,mali-d71", .data = &komeda_products[MALI_D71], },
135 	{},
136 };
137 
138 MODULE_DEVICE_TABLE(of, komeda_of_match);
139 
140 static int __maybe_unused komeda_pm_suspend(struct device *dev)
141 {
142 	struct komeda_drv *mdrv = dev_get_drvdata(dev);
143 	struct drm_device *drm = &mdrv->kms->base;
144 	int res;
145 
146 	res = drm_mode_config_helper_suspend(drm);
147 
148 	komeda_dev_suspend(mdrv->mdev);
149 
150 	return res;
151 }
152 
153 static int __maybe_unused komeda_pm_resume(struct device *dev)
154 {
155 	struct komeda_drv *mdrv = dev_get_drvdata(dev);
156 	struct drm_device *drm = &mdrv->kms->base;
157 
158 	komeda_dev_resume(mdrv->mdev);
159 
160 	return drm_mode_config_helper_resume(drm);
161 }
162 
163 static const struct dev_pm_ops komeda_pm_ops = {
164 	SET_SYSTEM_SLEEP_PM_OPS(komeda_pm_suspend, komeda_pm_resume)
165 };
166 
167 static struct platform_driver komeda_platform_driver = {
168 	.probe	= komeda_platform_probe,
169 	.remove	= komeda_platform_remove,
170 	.driver	= {
171 		.name = "komeda",
172 		.of_match_table	= komeda_of_match,
173 		.pm = &komeda_pm_ops,
174 	},
175 };
176 
177 module_platform_driver(komeda_platform_driver);
178 
179 MODULE_AUTHOR("James.Qian.Wang <james.qian.wang@arm.com>");
180 MODULE_DESCRIPTION("Komeda KMS driver");
181 MODULE_LICENSE("GPL v2");
182