Benjamin Gaignard | d219673 | 2014-07-30 19:28:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) STMicroelectronics SA 2014 |
| 3 | * Authors: Benjamin Gaignard <benjamin.gaignard@st.com> |
| 4 | * Fabien Dessenne <fabien.dessenne@st.com> |
| 5 | * for STMicroelectronics. |
| 6 | * License terms: GNU General Public License (GPL), version 2 |
| 7 | */ |
| 8 | |
| 9 | #include <linux/component.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/reset.h> |
| 13 | |
| 14 | #include <drm/drmP.h> |
| 15 | |
| 16 | #include "sti_compositor.h" |
Benjamin Gaignard | 9bbf86f | 2014-07-31 09:39:11 +0200 | [diff] [blame] | 17 | #include "sti_drm_crtc.h" |
| 18 | #include "sti_drm_drv.h" |
| 19 | #include "sti_drm_plane.h" |
Benjamin Gaignard | d219673 | 2014-07-30 19:28:27 +0200 | [diff] [blame] | 20 | #include "sti_gdp.h" |
| 21 | #include "sti_vtg.h" |
| 22 | |
| 23 | /* |
| 24 | * stiH407 compositor properties |
| 25 | */ |
| 26 | struct sti_compositor_data stih407_compositor_data = { |
| 27 | .nb_subdev = 6, |
| 28 | .subdev_desc = { |
| 29 | {STI_GPD_SUBDEV, (int)STI_GDP_0, 0x100}, |
| 30 | {STI_GPD_SUBDEV, (int)STI_GDP_1, 0x200}, |
| 31 | {STI_GPD_SUBDEV, (int)STI_GDP_2, 0x300}, |
| 32 | {STI_GPD_SUBDEV, (int)STI_GDP_3, 0x400}, |
| 33 | {STI_VID_SUBDEV, (int)STI_VID_0, 0x700}, |
| 34 | {STI_MIXER_MAIN_SUBDEV, STI_MIXER_MAIN, 0xC00} |
| 35 | }, |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * stiH416 compositor properties |
| 40 | * Note: |
| 41 | * on stih416 MIXER_AUX has a different base address from MIXER_MAIN |
| 42 | * Moreover, GDPx is different for Main and Aux Mixer. So this subdev map does |
| 43 | * not fit for stiH416 if we want to enable the MIXER_AUX. |
| 44 | */ |
| 45 | struct sti_compositor_data stih416_compositor_data = { |
| 46 | .nb_subdev = 3, |
| 47 | .subdev_desc = { |
| 48 | {STI_GPD_SUBDEV, (int)STI_GDP_0, 0x100}, |
| 49 | {STI_GPD_SUBDEV, (int)STI_GDP_1, 0x200}, |
| 50 | {STI_MIXER_MAIN_SUBDEV, STI_MIXER_MAIN, 0xC00} |
| 51 | }, |
| 52 | }; |
| 53 | |
| 54 | static int sti_compositor_init_subdev(struct sti_compositor *compo, |
| 55 | struct sti_compositor_subdev_descriptor *desc, |
| 56 | unsigned int array_size) |
| 57 | { |
| 58 | unsigned int i, mixer_id = 0, layer_id = 0; |
| 59 | |
| 60 | for (i = 0; i < array_size; i++) { |
| 61 | switch (desc[i].type) { |
| 62 | case STI_MIXER_MAIN_SUBDEV: |
| 63 | case STI_MIXER_AUX_SUBDEV: |
| 64 | compo->mixer[mixer_id++] = |
| 65 | sti_mixer_create(compo->dev, desc[i].id, |
| 66 | compo->regs + desc[i].offset); |
| 67 | break; |
| 68 | case STI_GPD_SUBDEV: |
| 69 | case STI_VID_SUBDEV: |
| 70 | compo->layer[layer_id++] = |
| 71 | sti_layer_create(compo->dev, desc[i].id, |
| 72 | compo->regs + desc[i].offset); |
| 73 | break; |
| 74 | /* case STI_CURSOR_SUBDEV : TODO */ |
| 75 | default: |
| 76 | DRM_ERROR("Unknow subdev compoment type\n"); |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | } |
| 81 | compo->nb_mixers = mixer_id; |
| 82 | compo->nb_layers = layer_id; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int sti_compositor_bind(struct device *dev, struct device *master, |
| 88 | void *data) |
| 89 | { |
| 90 | struct sti_compositor *compo = dev_get_drvdata(dev); |
| 91 | struct drm_device *drm_dev = data; |
| 92 | unsigned int i, crtc = 0, plane = 0; |
Benjamin Gaignard | 9bbf86f | 2014-07-31 09:39:11 +0200 | [diff] [blame] | 93 | struct sti_drm_private *dev_priv = drm_dev->dev_private; |
| 94 | struct drm_plane *cursor = NULL; |
| 95 | struct drm_plane *primary = NULL; |
| 96 | |
| 97 | dev_priv->compo = compo; |
| 98 | |
| 99 | for (i = 0; i < compo->nb_layers; i++) { |
| 100 | if (compo->layer[i]) { |
| 101 | enum sti_layer_desc desc = compo->layer[i]->desc; |
| 102 | enum sti_layer_type type = desc & STI_LAYER_TYPE_MASK; |
| 103 | enum drm_plane_type plane_type = DRM_PLANE_TYPE_OVERLAY; |
| 104 | |
| 105 | if (compo->mixer[crtc]) |
| 106 | plane_type = DRM_PLANE_TYPE_PRIMARY; |
| 107 | |
| 108 | switch (type) { |
| 109 | case STI_CUR: |
| 110 | cursor = sti_drm_plane_init(drm_dev, |
| 111 | compo->layer[i], |
| 112 | (1 << crtc) - 1, |
| 113 | DRM_PLANE_TYPE_CURSOR); |
| 114 | break; |
| 115 | case STI_GDP: |
| 116 | case STI_VID: |
| 117 | primary = sti_drm_plane_init(drm_dev, |
| 118 | compo->layer[i], |
| 119 | (1 << crtc) - 1, plane_type); |
| 120 | plane++; |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | /* The first planes are reserved for primary planes*/ |
| 125 | if (compo->mixer[crtc]) { |
| 126 | sti_drm_crtc_init(drm_dev, compo->mixer[crtc], |
| 127 | primary, cursor); |
| 128 | crtc++; |
| 129 | cursor = NULL; |
| 130 | } |
| 131 | } |
| 132 | } |
Benjamin Gaignard | d219673 | 2014-07-30 19:28:27 +0200 | [diff] [blame] | 133 | |
| 134 | drm_vblank_init(drm_dev, crtc); |
| 135 | /* Allow usage of vblank without having to call drm_irq_install */ |
| 136 | drm_dev->irq_enabled = 1; |
| 137 | |
Benjamin Gaignard | d219673 | 2014-07-30 19:28:27 +0200 | [diff] [blame] | 138 | DRM_DEBUG_DRIVER("Initialized %d DRM CRTC(s) and %d DRM plane(s)\n", |
| 139 | crtc, plane); |
| 140 | DRM_DEBUG_DRIVER("DRM plane(s) for VID/VDP not created yet\n"); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void sti_compositor_unbind(struct device *dev, struct device *master, |
| 146 | void *data) |
| 147 | { |
| 148 | /* do nothing */ |
| 149 | } |
| 150 | |
| 151 | static const struct component_ops sti_compositor_ops = { |
| 152 | .bind = sti_compositor_bind, |
| 153 | .unbind = sti_compositor_unbind, |
| 154 | }; |
| 155 | |
| 156 | static const struct of_device_id compositor_of_match[] = { |
| 157 | { |
| 158 | .compatible = "st,stih416-compositor", |
| 159 | .data = &stih416_compositor_data, |
| 160 | }, { |
| 161 | .compatible = "st,stih407-compositor", |
| 162 | .data = &stih407_compositor_data, |
| 163 | }, { |
| 164 | /* end node */ |
| 165 | } |
| 166 | }; |
| 167 | MODULE_DEVICE_TABLE(of, compositor_of_match); |
| 168 | |
| 169 | static int sti_compositor_probe(struct platform_device *pdev) |
| 170 | { |
| 171 | struct device *dev = &pdev->dev; |
| 172 | struct device_node *np = dev->of_node; |
| 173 | struct device_node *vtg_np; |
| 174 | struct sti_compositor *compo; |
| 175 | struct resource *res; |
| 176 | int err; |
| 177 | |
| 178 | compo = devm_kzalloc(dev, sizeof(*compo), GFP_KERNEL); |
| 179 | if (!compo) { |
| 180 | DRM_ERROR("Failed to allocate compositor context\n"); |
| 181 | return -ENOMEM; |
| 182 | } |
| 183 | compo->dev = dev; |
Benjamin Gaignard | 9bbf86f | 2014-07-31 09:39:11 +0200 | [diff] [blame] | 184 | compo->vtg_vblank_nb.notifier_call = sti_drm_crtc_vblank_cb; |
Benjamin Gaignard | d219673 | 2014-07-30 19:28:27 +0200 | [diff] [blame] | 185 | |
| 186 | /* populate data structure depending on compatibility */ |
| 187 | BUG_ON(!of_match_node(compositor_of_match, np)->data); |
| 188 | |
| 189 | memcpy(&compo->data, of_match_node(compositor_of_match, np)->data, |
| 190 | sizeof(struct sti_compositor_data)); |
| 191 | |
| 192 | /* Get Memory ressources */ |
| 193 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 194 | if (res == NULL) { |
| 195 | DRM_ERROR("Get memory resource failed\n"); |
| 196 | return -ENXIO; |
| 197 | } |
| 198 | compo->regs = devm_ioremap(dev, res->start, resource_size(res)); |
| 199 | if (compo->regs == NULL) { |
| 200 | DRM_ERROR("Register mapping failed\n"); |
| 201 | return -ENXIO; |
| 202 | } |
| 203 | |
| 204 | /* Get clock resources */ |
| 205 | compo->clk_compo_main = devm_clk_get(dev, "compo_main"); |
| 206 | if (IS_ERR(compo->clk_compo_main)) { |
| 207 | DRM_ERROR("Cannot get compo_main clock\n"); |
| 208 | return PTR_ERR(compo->clk_compo_main); |
| 209 | } |
| 210 | |
| 211 | compo->clk_compo_aux = devm_clk_get(dev, "compo_aux"); |
| 212 | if (IS_ERR(compo->clk_compo_aux)) { |
| 213 | DRM_ERROR("Cannot get compo_aux clock\n"); |
| 214 | return PTR_ERR(compo->clk_compo_aux); |
| 215 | } |
| 216 | |
| 217 | compo->clk_pix_main = devm_clk_get(dev, "pix_main"); |
| 218 | if (IS_ERR(compo->clk_pix_main)) { |
| 219 | DRM_ERROR("Cannot get pix_main clock\n"); |
| 220 | return PTR_ERR(compo->clk_pix_main); |
| 221 | } |
| 222 | |
| 223 | compo->clk_pix_aux = devm_clk_get(dev, "pix_aux"); |
| 224 | if (IS_ERR(compo->clk_pix_aux)) { |
| 225 | DRM_ERROR("Cannot get pix_aux clock\n"); |
| 226 | return PTR_ERR(compo->clk_pix_aux); |
| 227 | } |
| 228 | |
| 229 | /* Get reset resources */ |
| 230 | compo->rst_main = devm_reset_control_get(dev, "compo-main"); |
| 231 | /* Take compo main out of reset */ |
| 232 | if (!IS_ERR(compo->rst_main)) |
| 233 | reset_control_deassert(compo->rst_main); |
| 234 | |
| 235 | compo->rst_aux = devm_reset_control_get(dev, "compo-aux"); |
| 236 | /* Take compo aux out of reset */ |
| 237 | if (!IS_ERR(compo->rst_aux)) |
| 238 | reset_control_deassert(compo->rst_aux); |
| 239 | |
| 240 | vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0); |
| 241 | if (vtg_np) |
| 242 | compo->vtg_main = of_vtg_find(vtg_np); |
| 243 | |
| 244 | vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 1); |
| 245 | if (vtg_np) |
| 246 | compo->vtg_aux = of_vtg_find(vtg_np); |
| 247 | |
| 248 | /* Initialize compositor subdevices */ |
| 249 | err = sti_compositor_init_subdev(compo, compo->data.subdev_desc, |
| 250 | compo->data.nb_subdev); |
| 251 | if (err) |
| 252 | return err; |
| 253 | |
| 254 | platform_set_drvdata(pdev, compo); |
| 255 | |
| 256 | return component_add(&pdev->dev, &sti_compositor_ops); |
| 257 | } |
| 258 | |
| 259 | static int sti_compositor_remove(struct platform_device *pdev) |
| 260 | { |
| 261 | component_del(&pdev->dev, &sti_compositor_ops); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static struct platform_driver sti_compositor_driver = { |
| 266 | .driver = { |
| 267 | .name = "sti-compositor", |
| 268 | .owner = THIS_MODULE, |
| 269 | .of_match_table = compositor_of_match, |
| 270 | }, |
| 271 | .probe = sti_compositor_probe, |
| 272 | .remove = sti_compositor_remove, |
| 273 | }; |
| 274 | |
| 275 | module_platform_driver(sti_compositor_driver); |
| 276 | |
| 277 | MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>"); |
| 278 | MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver"); |
| 279 | MODULE_LICENSE("GPL"); |