Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
0004  * Author: James.Qian.Wang <james.qian.wang@arm.com>
0005  *
0006  */
0007 #include <linux/io.h>
0008 #include <linux/iommu.h>
0009 #include <linux/of_device.h>
0010 #include <linux/of_graph.h>
0011 #include <linux/of_reserved_mem.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/pm_runtime.h>
0014 #include <linux/dma-mapping.h>
0015 #ifdef CONFIG_DEBUG_FS
0016 #include <linux/debugfs.h>
0017 #include <linux/seq_file.h>
0018 #endif
0019 
0020 #include <drm/drm_print.h>
0021 
0022 #include "komeda_dev.h"
0023 
0024 static int komeda_register_show(struct seq_file *sf, void *x)
0025 {
0026     struct komeda_dev *mdev = sf->private;
0027     int i;
0028 
0029     seq_puts(sf, "\n====== Komeda register dump =========\n");
0030 
0031     pm_runtime_get_sync(mdev->dev);
0032 
0033     if (mdev->funcs->dump_register)
0034         mdev->funcs->dump_register(mdev, sf);
0035 
0036     for (i = 0; i < mdev->n_pipelines; i++)
0037         komeda_pipeline_dump_register(mdev->pipelines[i], sf);
0038 
0039     pm_runtime_put(mdev->dev);
0040 
0041     return 0;
0042 }
0043 
0044 DEFINE_SHOW_ATTRIBUTE(komeda_register);
0045 
0046 #ifdef CONFIG_DEBUG_FS
0047 static void komeda_debugfs_init(struct komeda_dev *mdev)
0048 {
0049     if (!debugfs_initialized())
0050         return;
0051 
0052     mdev->debugfs_root = debugfs_create_dir("komeda", NULL);
0053     debugfs_create_file("register", 0444, mdev->debugfs_root,
0054                 mdev, &komeda_register_fops);
0055     debugfs_create_x16("err_verbosity", 0664, mdev->debugfs_root,
0056                &mdev->err_verbosity);
0057 }
0058 #endif
0059 
0060 static ssize_t
0061 core_id_show(struct device *dev, struct device_attribute *attr, char *buf)
0062 {
0063     struct komeda_dev *mdev = dev_to_mdev(dev);
0064 
0065     return sysfs_emit(buf, "0x%08x\n", mdev->chip.core_id);
0066 }
0067 static DEVICE_ATTR_RO(core_id);
0068 
0069 static ssize_t
0070 config_id_show(struct device *dev, struct device_attribute *attr, char *buf)
0071 {
0072     struct komeda_dev *mdev = dev_to_mdev(dev);
0073     struct komeda_pipeline *pipe = mdev->pipelines[0];
0074     union komeda_config_id config_id;
0075     int i;
0076 
0077     memset(&config_id, 0, sizeof(config_id));
0078 
0079     config_id.max_line_sz = pipe->layers[0]->hsize_in.end;
0080     config_id.n_pipelines = mdev->n_pipelines;
0081     config_id.n_scalers = pipe->n_scalers;
0082     config_id.n_layers = pipe->n_layers;
0083     config_id.n_richs = 0;
0084     for (i = 0; i < pipe->n_layers; i++) {
0085         if (pipe->layers[i]->layer_type == KOMEDA_FMT_RICH_LAYER)
0086             config_id.n_richs++;
0087     }
0088     return sysfs_emit(buf, "0x%08x\n", config_id.value);
0089 }
0090 static DEVICE_ATTR_RO(config_id);
0091 
0092 static ssize_t
0093 aclk_hz_show(struct device *dev, struct device_attribute *attr, char *buf)
0094 {
0095     struct komeda_dev *mdev = dev_to_mdev(dev);
0096 
0097     return sysfs_emit(buf, "%lu\n", clk_get_rate(mdev->aclk));
0098 }
0099 static DEVICE_ATTR_RO(aclk_hz);
0100 
0101 static struct attribute *komeda_sysfs_entries[] = {
0102     &dev_attr_core_id.attr,
0103     &dev_attr_config_id.attr,
0104     &dev_attr_aclk_hz.attr,
0105     NULL,
0106 };
0107 
0108 static struct attribute_group komeda_sysfs_attr_group = {
0109     .attrs = komeda_sysfs_entries,
0110 };
0111 
0112 static int komeda_parse_pipe_dt(struct komeda_pipeline *pipe)
0113 {
0114     struct device_node *np = pipe->of_node;
0115     struct clk *clk;
0116 
0117     clk = of_clk_get_by_name(np, "pxclk");
0118     if (IS_ERR(clk)) {
0119         DRM_ERROR("get pxclk for pipeline %d failed!\n", pipe->id);
0120         return PTR_ERR(clk);
0121     }
0122     pipe->pxlclk = clk;
0123 
0124     /* enum ports */
0125     pipe->of_output_links[0] =
0126         of_graph_get_remote_node(np, KOMEDA_OF_PORT_OUTPUT, 0);
0127     pipe->of_output_links[1] =
0128         of_graph_get_remote_node(np, KOMEDA_OF_PORT_OUTPUT, 1);
0129     pipe->of_output_port =
0130         of_graph_get_port_by_id(np, KOMEDA_OF_PORT_OUTPUT);
0131 
0132     pipe->dual_link = pipe->of_output_links[0] && pipe->of_output_links[1];
0133 
0134     return 0;
0135 }
0136 
0137 static int komeda_parse_dt(struct device *dev, struct komeda_dev *mdev)
0138 {
0139     struct platform_device *pdev = to_platform_device(dev);
0140     struct device_node *child, *np = dev->of_node;
0141     struct komeda_pipeline *pipe;
0142     u32 pipe_id = U32_MAX;
0143     int ret = -1;
0144 
0145     mdev->irq  = platform_get_irq(pdev, 0);
0146     if (mdev->irq < 0) {
0147         DRM_ERROR("could not get IRQ number.\n");
0148         return mdev->irq;
0149     }
0150 
0151     /* Get the optional framebuffer memory resource */
0152     ret = of_reserved_mem_device_init(dev);
0153     if (ret && ret != -ENODEV)
0154         return ret;
0155 
0156     for_each_available_child_of_node(np, child) {
0157         if (of_node_name_eq(child, "pipeline")) {
0158             of_property_read_u32(child, "reg", &pipe_id);
0159             if (pipe_id >= mdev->n_pipelines) {
0160                 DRM_WARN("Skip the redundant DT node: pipeline-%u.\n",
0161                      pipe_id);
0162                 continue;
0163             }
0164             mdev->pipelines[pipe_id]->of_node = of_node_get(child);
0165         }
0166     }
0167 
0168     for (pipe_id = 0; pipe_id < mdev->n_pipelines; pipe_id++) {
0169         pipe = mdev->pipelines[pipe_id];
0170 
0171         if (!pipe->of_node) {
0172             DRM_ERROR("Pipeline-%d doesn't have a DT node.\n",
0173                   pipe->id);
0174             return -EINVAL;
0175         }
0176         ret = komeda_parse_pipe_dt(pipe);
0177         if (ret)
0178             return ret;
0179     }
0180 
0181     return 0;
0182 }
0183 
0184 struct komeda_dev *komeda_dev_create(struct device *dev)
0185 {
0186     struct platform_device *pdev = to_platform_device(dev);
0187     komeda_identify_func komeda_identify;
0188     struct komeda_dev *mdev;
0189     int err = 0;
0190 
0191     komeda_identify = of_device_get_match_data(dev);
0192     if (!komeda_identify)
0193         return ERR_PTR(-ENODEV);
0194 
0195     mdev = devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL);
0196     if (!mdev)
0197         return ERR_PTR(-ENOMEM);
0198 
0199     mutex_init(&mdev->lock);
0200 
0201     mdev->dev = dev;
0202     mdev->reg_base = devm_platform_ioremap_resource(pdev, 0);
0203     if (IS_ERR(mdev->reg_base)) {
0204         DRM_ERROR("Map register space failed.\n");
0205         err = PTR_ERR(mdev->reg_base);
0206         mdev->reg_base = NULL;
0207         goto err_cleanup;
0208     }
0209 
0210     mdev->aclk = devm_clk_get(dev, "aclk");
0211     if (IS_ERR(mdev->aclk)) {
0212         DRM_ERROR("Get engine clk failed.\n");
0213         err = PTR_ERR(mdev->aclk);
0214         mdev->aclk = NULL;
0215         goto err_cleanup;
0216     }
0217 
0218     clk_prepare_enable(mdev->aclk);
0219 
0220     mdev->funcs = komeda_identify(mdev->reg_base, &mdev->chip);
0221     if (!mdev->funcs) {
0222         DRM_ERROR("Failed to identify the HW.\n");
0223         err = -ENODEV;
0224         goto disable_clk;
0225     }
0226 
0227     DRM_INFO("Found ARM Mali-D%x version r%dp%d\n",
0228          MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id),
0229          MALIDP_CORE_ID_MAJOR(mdev->chip.core_id),
0230          MALIDP_CORE_ID_MINOR(mdev->chip.core_id));
0231 
0232     mdev->funcs->init_format_table(mdev);
0233 
0234     err = mdev->funcs->enum_resources(mdev);
0235     if (err) {
0236         DRM_ERROR("enumerate display resource failed.\n");
0237         goto disable_clk;
0238     }
0239 
0240     err = komeda_parse_dt(dev, mdev);
0241     if (err) {
0242         DRM_ERROR("parse device tree failed.\n");
0243         goto disable_clk;
0244     }
0245 
0246     err = komeda_assemble_pipelines(mdev);
0247     if (err) {
0248         DRM_ERROR("assemble display pipelines failed.\n");
0249         goto disable_clk;
0250     }
0251 
0252     dma_set_max_seg_size(dev, U32_MAX);
0253 
0254     mdev->iommu = iommu_get_domain_for_dev(mdev->dev);
0255     if (!mdev->iommu)
0256         DRM_INFO("continue without IOMMU support!\n");
0257 
0258     clk_disable_unprepare(mdev->aclk);
0259 
0260     err = sysfs_create_group(&dev->kobj, &komeda_sysfs_attr_group);
0261     if (err) {
0262         DRM_ERROR("create sysfs group failed.\n");
0263         goto err_cleanup;
0264     }
0265 
0266     mdev->err_verbosity = KOMEDA_DEV_PRINT_ERR_EVENTS;
0267 
0268 #ifdef CONFIG_DEBUG_FS
0269     komeda_debugfs_init(mdev);
0270 #endif
0271 
0272     return mdev;
0273 
0274 disable_clk:
0275     clk_disable_unprepare(mdev->aclk);
0276 err_cleanup:
0277     komeda_dev_destroy(mdev);
0278     return ERR_PTR(err);
0279 }
0280 
0281 void komeda_dev_destroy(struct komeda_dev *mdev)
0282 {
0283     struct device *dev = mdev->dev;
0284     const struct komeda_dev_funcs *funcs = mdev->funcs;
0285     int i;
0286 
0287     sysfs_remove_group(&dev->kobj, &komeda_sysfs_attr_group);
0288 
0289 #ifdef CONFIG_DEBUG_FS
0290     debugfs_remove_recursive(mdev->debugfs_root);
0291 #endif
0292 
0293     if (mdev->aclk)
0294         clk_prepare_enable(mdev->aclk);
0295 
0296     for (i = 0; i < mdev->n_pipelines; i++) {
0297         komeda_pipeline_destroy(mdev, mdev->pipelines[i]);
0298         mdev->pipelines[i] = NULL;
0299     }
0300 
0301     mdev->n_pipelines = 0;
0302 
0303     of_reserved_mem_device_release(dev);
0304 
0305     if (funcs && funcs->cleanup)
0306         funcs->cleanup(mdev);
0307 
0308     if (mdev->reg_base) {
0309         devm_iounmap(dev, mdev->reg_base);
0310         mdev->reg_base = NULL;
0311     }
0312 
0313     if (mdev->aclk) {
0314         clk_disable_unprepare(mdev->aclk);
0315         devm_clk_put(dev, mdev->aclk);
0316         mdev->aclk = NULL;
0317     }
0318 
0319     devm_kfree(dev, mdev);
0320 }
0321 
0322 int komeda_dev_resume(struct komeda_dev *mdev)
0323 {
0324     clk_prepare_enable(mdev->aclk);
0325 
0326     mdev->funcs->enable_irq(mdev);
0327 
0328     if (mdev->iommu && mdev->funcs->connect_iommu)
0329         if (mdev->funcs->connect_iommu(mdev))
0330             DRM_ERROR("connect iommu failed.\n");
0331 
0332     return 0;
0333 }
0334 
0335 int komeda_dev_suspend(struct komeda_dev *mdev)
0336 {
0337     if (mdev->iommu && mdev->funcs->disconnect_iommu)
0338         if (mdev->funcs->disconnect_iommu(mdev))
0339             DRM_ERROR("disconnect iommu failed.\n");
0340 
0341     mdev->funcs->disable_irq(mdev);
0342 
0343     clk_disable_unprepare(mdev->aclk);
0344 
0345     return 0;
0346 }