1d219673dSBenjamin Gaignard /* 2d219673dSBenjamin Gaignard * Copyright (C) STMicroelectronics SA 2014 3d219673dSBenjamin Gaignard * Authors: Benjamin Gaignard <benjamin.gaignard@st.com> 4d219673dSBenjamin Gaignard * Fabien Dessenne <fabien.dessenne@st.com> 5d219673dSBenjamin Gaignard * for STMicroelectronics. 6d219673dSBenjamin Gaignard * License terms: GNU General Public License (GPL), version 2 7d219673dSBenjamin Gaignard */ 8d219673dSBenjamin Gaignard 9d219673dSBenjamin Gaignard #include <linux/component.h> 10d219673dSBenjamin Gaignard #include <linux/module.h> 11d219673dSBenjamin Gaignard #include <linux/platform_device.h> 12d219673dSBenjamin Gaignard #include <linux/reset.h> 13d219673dSBenjamin Gaignard 14d219673dSBenjamin Gaignard #include <drm/drmP.h> 15d219673dSBenjamin Gaignard 16d219673dSBenjamin Gaignard #include "sti_compositor.h" 179e1f05b2SVincent Abriou #include "sti_crtc.h" 18871bcdfeSVincent Abriou #include "sti_cursor.h" 199e1f05b2SVincent Abriou #include "sti_drv.h" 20d219673dSBenjamin Gaignard #include "sti_gdp.h" 219e1f05b2SVincent Abriou #include "sti_plane.h" 22871bcdfeSVincent Abriou #include "sti_vid.h" 23d219673dSBenjamin Gaignard #include "sti_vtg.h" 24d219673dSBenjamin Gaignard 25d219673dSBenjamin Gaignard /* 26d219673dSBenjamin Gaignard * stiH407 compositor properties 27d219673dSBenjamin Gaignard */ 28d219673dSBenjamin Gaignard struct sti_compositor_data stih407_compositor_data = { 2996006a77SBenjamin Gaignard .nb_subdev = 8, 30d219673dSBenjamin Gaignard .subdev_desc = { 3196006a77SBenjamin Gaignard {STI_CURSOR_SUBDEV, (int)STI_CURSOR, 0x000}, 32d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_0, 0x100}, 33d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_1, 0x200}, 34d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_2, 0x300}, 35d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_3, 0x400}, 36871bcdfeSVincent Abriou {STI_VID_SUBDEV, (int)STI_HQVDP_0, 0x700}, 375e03abc5SBenjamin Gaignard {STI_MIXER_MAIN_SUBDEV, STI_MIXER_MAIN, 0xC00}, 385e03abc5SBenjamin Gaignard {STI_MIXER_AUX_SUBDEV, STI_MIXER_AUX, 0xD00}, 39d219673dSBenjamin Gaignard }, 40d219673dSBenjamin Gaignard }; 41d219673dSBenjamin Gaignard 42d219673dSBenjamin Gaignard /* 43d219673dSBenjamin Gaignard * stiH416 compositor properties 44d219673dSBenjamin Gaignard * Note: 45d219673dSBenjamin Gaignard * on stih416 MIXER_AUX has a different base address from MIXER_MAIN 46d219673dSBenjamin Gaignard * Moreover, GDPx is different for Main and Aux Mixer. So this subdev map does 47d219673dSBenjamin Gaignard * not fit for stiH416 if we want to enable the MIXER_AUX. 48d219673dSBenjamin Gaignard */ 49d219673dSBenjamin Gaignard struct sti_compositor_data stih416_compositor_data = { 50d219673dSBenjamin Gaignard .nb_subdev = 3, 51d219673dSBenjamin Gaignard .subdev_desc = { 52d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_0, 0x100}, 53d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_1, 0x200}, 54d219673dSBenjamin Gaignard {STI_MIXER_MAIN_SUBDEV, STI_MIXER_MAIN, 0xC00} 55d219673dSBenjamin Gaignard }, 56d219673dSBenjamin Gaignard }; 57d219673dSBenjamin Gaignard 58871bcdfeSVincent Abriou static int sti_compositor_bind(struct device *dev, 59871bcdfeSVincent Abriou struct device *master, 60871bcdfeSVincent Abriou void *data) 61d219673dSBenjamin Gaignard { 62871bcdfeSVincent Abriou struct sti_compositor *compo = dev_get_drvdata(dev); 63871bcdfeSVincent Abriou struct drm_device *drm_dev = data; 6429d1dc62SVincent Abriou unsigned int i, mixer_id = 0, vid_id = 0, crtc_id = 0; 659e1f05b2SVincent Abriou struct sti_private *dev_priv = drm_dev->dev_private; 66871bcdfeSVincent Abriou struct drm_plane *cursor = NULL; 67871bcdfeSVincent Abriou struct drm_plane *primary = NULL; 68871bcdfeSVincent Abriou struct sti_compositor_subdev_descriptor *desc = compo->data.subdev_desc; 69871bcdfeSVincent Abriou unsigned int array_size = compo->data.nb_subdev; 70d219673dSBenjamin Gaignard 71871bcdfeSVincent Abriou dev_priv->compo = compo; 72871bcdfeSVincent Abriou 73871bcdfeSVincent Abriou /* Register mixer subdev and video subdev first */ 74d219673dSBenjamin Gaignard for (i = 0; i < array_size; i++) { 75d219673dSBenjamin Gaignard switch (desc[i].type) { 76871bcdfeSVincent Abriou case STI_VID_SUBDEV: 77871bcdfeSVincent Abriou compo->vid[vid_id++] = 78871bcdfeSVincent Abriou sti_vid_create(compo->dev, desc[i].id, 79871bcdfeSVincent Abriou compo->regs + desc[i].offset); 80871bcdfeSVincent Abriou break; 81d219673dSBenjamin Gaignard case STI_MIXER_MAIN_SUBDEV: 82d219673dSBenjamin Gaignard case STI_MIXER_AUX_SUBDEV: 83d219673dSBenjamin Gaignard compo->mixer[mixer_id++] = 84d219673dSBenjamin Gaignard sti_mixer_create(compo->dev, desc[i].id, 85d219673dSBenjamin Gaignard compo->regs + desc[i].offset); 86d219673dSBenjamin Gaignard break; 87d219673dSBenjamin Gaignard case STI_GPD_SUBDEV: 8896006a77SBenjamin Gaignard case STI_CURSOR_SUBDEV: 89871bcdfeSVincent Abriou /* Nothing to do, wait for the second round */ 90d219673dSBenjamin Gaignard break; 91d219673dSBenjamin Gaignard default: 92d219673dSBenjamin Gaignard DRM_ERROR("Unknow subdev compoment type\n"); 93d219673dSBenjamin Gaignard return 1; 94d219673dSBenjamin Gaignard } 95d219673dSBenjamin Gaignard } 96d219673dSBenjamin Gaignard 97871bcdfeSVincent Abriou /* Register the other subdevs, create crtc and planes */ 98871bcdfeSVincent Abriou for (i = 0; i < array_size; i++) { 999bbf86feSBenjamin Gaignard enum drm_plane_type plane_type = DRM_PLANE_TYPE_OVERLAY; 1009bbf86feSBenjamin Gaignard 101871bcdfeSVincent Abriou if (crtc_id < mixer_id) 1029bbf86feSBenjamin Gaignard plane_type = DRM_PLANE_TYPE_PRIMARY; 1039bbf86feSBenjamin Gaignard 104871bcdfeSVincent Abriou switch (desc[i].type) { 105871bcdfeSVincent Abriou case STI_MIXER_MAIN_SUBDEV: 106871bcdfeSVincent Abriou case STI_MIXER_AUX_SUBDEV: 107871bcdfeSVincent Abriou case STI_VID_SUBDEV: 108871bcdfeSVincent Abriou /* Nothing to do, already done at the first round */ 1099bbf86feSBenjamin Gaignard break; 110871bcdfeSVincent Abriou case STI_CURSOR_SUBDEV: 11129d1dc62SVincent Abriou cursor = sti_cursor_create(drm_dev, compo->dev, 11229d1dc62SVincent Abriou desc[i].id, 11329d1dc62SVincent Abriou compo->regs + desc[i].offset, 11429d1dc62SVincent Abriou 1); 11529d1dc62SVincent Abriou if (!cursor) { 116871bcdfeSVincent Abriou DRM_ERROR("Can't create CURSOR plane\n"); 117871bcdfeSVincent Abriou break; 118871bcdfeSVincent Abriou } 119871bcdfeSVincent Abriou break; 120871bcdfeSVincent Abriou case STI_GPD_SUBDEV: 12129d1dc62SVincent Abriou primary = sti_gdp_create(drm_dev, compo->dev, 12229d1dc62SVincent Abriou desc[i].id, 12329d1dc62SVincent Abriou compo->regs + desc[i].offset, 12429d1dc62SVincent Abriou (1 << mixer_id) - 1, 12529d1dc62SVincent Abriou plane_type); 12629d1dc62SVincent Abriou if (!primary) { 127871bcdfeSVincent Abriou DRM_ERROR("Can't create GDP plane\n"); 128871bcdfeSVincent Abriou break; 129871bcdfeSVincent Abriou } 1309bbf86feSBenjamin Gaignard break; 131871bcdfeSVincent Abriou default: 132871bcdfeSVincent Abriou DRM_ERROR("Unknown subdev compoment type\n"); 133871bcdfeSVincent Abriou return 1; 1349bbf86feSBenjamin Gaignard } 1359bbf86feSBenjamin Gaignard 1369bbf86feSBenjamin Gaignard /* The first planes are reserved for primary planes*/ 137871bcdfeSVincent Abriou if (crtc_id < mixer_id && primary) { 1389e1f05b2SVincent Abriou sti_crtc_init(drm_dev, compo->mixer[crtc_id], 1399bbf86feSBenjamin Gaignard primary, cursor); 140871bcdfeSVincent Abriou crtc_id++; 1419bbf86feSBenjamin Gaignard cursor = NULL; 14296006a77SBenjamin Gaignard primary = NULL; 1439bbf86feSBenjamin Gaignard } 1449bbf86feSBenjamin Gaignard } 145d219673dSBenjamin Gaignard 146871bcdfeSVincent Abriou drm_vblank_init(drm_dev, crtc_id); 147d219673dSBenjamin Gaignard /* Allow usage of vblank without having to call drm_irq_install */ 148d219673dSBenjamin Gaignard drm_dev->irq_enabled = 1; 149d219673dSBenjamin Gaignard 150d219673dSBenjamin Gaignard return 0; 151d219673dSBenjamin Gaignard } 152d219673dSBenjamin Gaignard 153d219673dSBenjamin Gaignard static void sti_compositor_unbind(struct device *dev, struct device *master, 154d219673dSBenjamin Gaignard void *data) 155d219673dSBenjamin Gaignard { 156d219673dSBenjamin Gaignard /* do nothing */ 157d219673dSBenjamin Gaignard } 158d219673dSBenjamin Gaignard 159d219673dSBenjamin Gaignard static const struct component_ops sti_compositor_ops = { 160d219673dSBenjamin Gaignard .bind = sti_compositor_bind, 161d219673dSBenjamin Gaignard .unbind = sti_compositor_unbind, 162d219673dSBenjamin Gaignard }; 163d219673dSBenjamin Gaignard 164d219673dSBenjamin Gaignard static const struct of_device_id compositor_of_match[] = { 165d219673dSBenjamin Gaignard { 166d219673dSBenjamin Gaignard .compatible = "st,stih416-compositor", 167d219673dSBenjamin Gaignard .data = &stih416_compositor_data, 168d219673dSBenjamin Gaignard }, { 169d219673dSBenjamin Gaignard .compatible = "st,stih407-compositor", 170d219673dSBenjamin Gaignard .data = &stih407_compositor_data, 171d219673dSBenjamin Gaignard }, { 172d219673dSBenjamin Gaignard /* end node */ 173d219673dSBenjamin Gaignard } 174d219673dSBenjamin Gaignard }; 175d219673dSBenjamin Gaignard MODULE_DEVICE_TABLE(of, compositor_of_match); 176d219673dSBenjamin Gaignard 177d219673dSBenjamin Gaignard static int sti_compositor_probe(struct platform_device *pdev) 178d219673dSBenjamin Gaignard { 179d219673dSBenjamin Gaignard struct device *dev = &pdev->dev; 180d219673dSBenjamin Gaignard struct device_node *np = dev->of_node; 181d219673dSBenjamin Gaignard struct device_node *vtg_np; 182d219673dSBenjamin Gaignard struct sti_compositor *compo; 183d219673dSBenjamin Gaignard struct resource *res; 184d219673dSBenjamin Gaignard 185d219673dSBenjamin Gaignard compo = devm_kzalloc(dev, sizeof(*compo), GFP_KERNEL); 186d219673dSBenjamin Gaignard if (!compo) { 187d219673dSBenjamin Gaignard DRM_ERROR("Failed to allocate compositor context\n"); 188d219673dSBenjamin Gaignard return -ENOMEM; 189d219673dSBenjamin Gaignard } 190d219673dSBenjamin Gaignard compo->dev = dev; 1919e1f05b2SVincent Abriou compo->vtg_vblank_nb.notifier_call = sti_crtc_vblank_cb; 192d219673dSBenjamin Gaignard 193d219673dSBenjamin Gaignard /* populate data structure depending on compatibility */ 194d219673dSBenjamin Gaignard BUG_ON(!of_match_node(compositor_of_match, np)->data); 195d219673dSBenjamin Gaignard 196d219673dSBenjamin Gaignard memcpy(&compo->data, of_match_node(compositor_of_match, np)->data, 197d219673dSBenjamin Gaignard sizeof(struct sti_compositor_data)); 198d219673dSBenjamin Gaignard 199d219673dSBenjamin Gaignard /* Get Memory ressources */ 200d219673dSBenjamin Gaignard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 201d219673dSBenjamin Gaignard if (res == NULL) { 202d219673dSBenjamin Gaignard DRM_ERROR("Get memory resource failed\n"); 203d219673dSBenjamin Gaignard return -ENXIO; 204d219673dSBenjamin Gaignard } 205d219673dSBenjamin Gaignard compo->regs = devm_ioremap(dev, res->start, resource_size(res)); 206d219673dSBenjamin Gaignard if (compo->regs == NULL) { 207d219673dSBenjamin Gaignard DRM_ERROR("Register mapping failed\n"); 208d219673dSBenjamin Gaignard return -ENXIO; 209d219673dSBenjamin Gaignard } 210d219673dSBenjamin Gaignard 211d219673dSBenjamin Gaignard /* Get clock resources */ 212d219673dSBenjamin Gaignard compo->clk_compo_main = devm_clk_get(dev, "compo_main"); 213d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_compo_main)) { 214d219673dSBenjamin Gaignard DRM_ERROR("Cannot get compo_main clock\n"); 215d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_compo_main); 216d219673dSBenjamin Gaignard } 217d219673dSBenjamin Gaignard 218d219673dSBenjamin Gaignard compo->clk_compo_aux = devm_clk_get(dev, "compo_aux"); 219d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_compo_aux)) { 220d219673dSBenjamin Gaignard DRM_ERROR("Cannot get compo_aux clock\n"); 221d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_compo_aux); 222d219673dSBenjamin Gaignard } 223d219673dSBenjamin Gaignard 224d219673dSBenjamin Gaignard compo->clk_pix_main = devm_clk_get(dev, "pix_main"); 225d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_pix_main)) { 226d219673dSBenjamin Gaignard DRM_ERROR("Cannot get pix_main clock\n"); 227d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_pix_main); 228d219673dSBenjamin Gaignard } 229d219673dSBenjamin Gaignard 230d219673dSBenjamin Gaignard compo->clk_pix_aux = devm_clk_get(dev, "pix_aux"); 231d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_pix_aux)) { 232d219673dSBenjamin Gaignard DRM_ERROR("Cannot get pix_aux clock\n"); 233d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_pix_aux); 234d219673dSBenjamin Gaignard } 235d219673dSBenjamin Gaignard 236d219673dSBenjamin Gaignard /* Get reset resources */ 237d219673dSBenjamin Gaignard compo->rst_main = devm_reset_control_get(dev, "compo-main"); 238d219673dSBenjamin Gaignard /* Take compo main out of reset */ 239d219673dSBenjamin Gaignard if (!IS_ERR(compo->rst_main)) 240d219673dSBenjamin Gaignard reset_control_deassert(compo->rst_main); 241d219673dSBenjamin Gaignard 242d219673dSBenjamin Gaignard compo->rst_aux = devm_reset_control_get(dev, "compo-aux"); 243d219673dSBenjamin Gaignard /* Take compo aux out of reset */ 244d219673dSBenjamin Gaignard if (!IS_ERR(compo->rst_aux)) 245d219673dSBenjamin Gaignard reset_control_deassert(compo->rst_aux); 246d219673dSBenjamin Gaignard 247d219673dSBenjamin Gaignard vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0); 248d219673dSBenjamin Gaignard if (vtg_np) 249d219673dSBenjamin Gaignard compo->vtg_main = of_vtg_find(vtg_np); 250d219673dSBenjamin Gaignard 251d219673dSBenjamin Gaignard vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 1); 252d219673dSBenjamin Gaignard if (vtg_np) 253d219673dSBenjamin Gaignard compo->vtg_aux = of_vtg_find(vtg_np); 254d219673dSBenjamin Gaignard 255d219673dSBenjamin Gaignard platform_set_drvdata(pdev, compo); 256d219673dSBenjamin Gaignard 257d219673dSBenjamin Gaignard return component_add(&pdev->dev, &sti_compositor_ops); 258d219673dSBenjamin Gaignard } 259d219673dSBenjamin Gaignard 260d219673dSBenjamin Gaignard static int sti_compositor_remove(struct platform_device *pdev) 261d219673dSBenjamin Gaignard { 262d219673dSBenjamin Gaignard component_del(&pdev->dev, &sti_compositor_ops); 263d219673dSBenjamin Gaignard return 0; 264d219673dSBenjamin Gaignard } 265d219673dSBenjamin Gaignard 266*dcec16efSThierry Reding struct platform_driver sti_compositor_driver = { 267d219673dSBenjamin Gaignard .driver = { 268d219673dSBenjamin Gaignard .name = "sti-compositor", 269d219673dSBenjamin Gaignard .of_match_table = compositor_of_match, 270d219673dSBenjamin Gaignard }, 271d219673dSBenjamin Gaignard .probe = sti_compositor_probe, 272d219673dSBenjamin Gaignard .remove = sti_compositor_remove, 273d219673dSBenjamin Gaignard }; 274d219673dSBenjamin Gaignard 275d219673dSBenjamin Gaignard MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>"); 276d219673dSBenjamin Gaignard MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver"); 277d219673dSBenjamin Gaignard MODULE_LICENSE("GPL"); 278