xref: /linux/drivers/gpu/drm/imx/dc/dc-cf.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2024 NXP
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
8 #include <linux/component.h>
9 #include <linux/ioport.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 
15 #include "dc-drv.h"
16 #include "dc-pe.h"
17 
18 #define STATICCONTROL		0x8
19 
20 #define FRAMEDIMENSIONS		0xc
21 #define  HEIGHT(x)		FIELD_PREP(GENMASK(29, 16), ((x) - 1))
22 #define  WIDTH(x)		FIELD_PREP(GENMASK(13, 0), ((x) - 1))
23 
24 #define CONSTANTCOLOR		0x10
25 #define  BLUE(x)		FIELD_PREP(GENMASK(15, 8), (x))
26 
27 static const struct dc_subdev_info dc_cf_info[] = {
28 	{ .reg_start = 0x56180960, .id = 0, },
29 	{ .reg_start = 0x561809e0, .id = 1, },
30 	{ .reg_start = 0x561809a0, .id = 4, },
31 	{ .reg_start = 0x56180a20, .id = 5, },
32 };
33 
34 static const struct regmap_range dc_cf_regmap_ranges[] = {
35 	regmap_reg_range(STATICCONTROL, CONSTANTCOLOR),
36 };
37 
38 static const struct regmap_access_table dc_cf_regmap_access_table = {
39 	.yes_ranges = dc_cf_regmap_ranges,
40 	.n_yes_ranges = ARRAY_SIZE(dc_cf_regmap_ranges),
41 };
42 
43 static const struct regmap_config dc_cf_cfg_regmap_config = {
44 	.name = "cfg",
45 	.reg_bits = 32,
46 	.reg_stride = 4,
47 	.val_bits = 32,
48 	.fast_io = true,
49 	.wr_table = &dc_cf_regmap_access_table,
50 	.rd_table = &dc_cf_regmap_access_table,
51 	.max_register = CONSTANTCOLOR,
52 };
53 
54 static inline void dc_cf_enable_shden(struct dc_cf *cf)
55 {
56 	regmap_write(cf->reg_cfg, STATICCONTROL, SHDEN);
57 }
58 
59 enum dc_link_id dc_cf_get_link_id(struct dc_cf *cf)
60 {
61 	return cf->link;
62 }
63 
64 void dc_cf_framedimensions(struct dc_cf *cf, unsigned int w,
65 			   unsigned int h)
66 {
67 	regmap_write(cf->reg_cfg, FRAMEDIMENSIONS, WIDTH(w) | HEIGHT(h));
68 }
69 
70 void dc_cf_constantcolor_black(struct dc_cf *cf)
71 {
72 	regmap_write(cf->reg_cfg, CONSTANTCOLOR, 0);
73 }
74 
75 void dc_cf_constantcolor_blue(struct dc_cf *cf)
76 {
77 	regmap_write(cf->reg_cfg, CONSTANTCOLOR, BLUE(0xff));
78 }
79 
80 void dc_cf_init(struct dc_cf *cf)
81 {
82 	dc_cf_enable_shden(cf);
83 }
84 
85 static int dc_cf_bind(struct device *dev, struct device *master, void *data)
86 {
87 	struct platform_device *pdev = to_platform_device(dev);
88 	struct dc_drm_device *dc_drm = data;
89 	struct resource *res_pec;
90 	void __iomem *base_cfg;
91 	struct dc_cf *cf;
92 	int id;
93 
94 	cf = devm_kzalloc(dev, sizeof(*cf), GFP_KERNEL);
95 	if (!cf)
96 		return -ENOMEM;
97 
98 	res_pec = platform_get_resource(pdev, IORESOURCE_MEM, 0);
99 
100 	base_cfg = devm_platform_ioremap_resource_byname(pdev, "cfg");
101 	if (IS_ERR(base_cfg))
102 		return PTR_ERR(base_cfg);
103 
104 	cf->reg_cfg = devm_regmap_init_mmio(dev, base_cfg,
105 					    &dc_cf_cfg_regmap_config);
106 	if (IS_ERR(cf->reg_cfg))
107 		return PTR_ERR(cf->reg_cfg);
108 
109 	id = dc_subdev_get_id(dc_cf_info, ARRAY_SIZE(dc_cf_info), res_pec);
110 	if (id < 0) {
111 		dev_err(dev, "failed to get instance number: %d\n", id);
112 		return id;
113 	}
114 
115 	switch (id) {
116 	case 0:
117 		cf->link = LINK_ID_CONSTFRAME0;
118 		dc_drm->cf_cont[0] = cf;
119 		break;
120 	case 1:
121 		cf->link = LINK_ID_CONSTFRAME1;
122 		dc_drm->cf_cont[1] = cf;
123 		break;
124 	case 4:
125 		cf->link = LINK_ID_CONSTFRAME4;
126 		dc_drm->cf_safe[0] = cf;
127 		break;
128 	case 5:
129 		cf->link = LINK_ID_CONSTFRAME5;
130 		dc_drm->cf_safe[1] = cf;
131 		break;
132 	}
133 
134 	return 0;
135 }
136 
137 static const struct component_ops dc_cf_ops = {
138 	.bind = dc_cf_bind,
139 };
140 
141 static int dc_cf_probe(struct platform_device *pdev)
142 {
143 	int ret;
144 
145 	ret = component_add(&pdev->dev, &dc_cf_ops);
146 	if (ret)
147 		return dev_err_probe(&pdev->dev, ret,
148 				     "failed to add component\n");
149 
150 	return 0;
151 }
152 
153 static void dc_cf_remove(struct platform_device *pdev)
154 {
155 	component_del(&pdev->dev, &dc_cf_ops);
156 }
157 
158 static const struct of_device_id dc_cf_dt_ids[] = {
159 	{ .compatible = "fsl,imx8qxp-dc-constframe" },
160 	{ /* sentinel */ }
161 };
162 MODULE_DEVICE_TABLE(of, dc_cf_dt_ids);
163 
164 struct platform_driver dc_cf_driver = {
165 	.probe = dc_cf_probe,
166 	.remove = dc_cf_remove,
167 	.driver = {
168 		.name = "imx8-dc-constframe",
169 		.suppress_bind_attrs = true,
170 		.of_match_table = dc_cf_dt_ids,
171 	},
172 };
173