video: tegra: host: Fix error handling
[linux-3.10.git] / drivers / video / tegra / host / vi / vi.c
index 43a1636..3ddafb6 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Tegra Graphics Host VI
  *
- * Copyright (c) 2012, NVIDIA Corporation.
+ * Copyright (c) 2012-2013, NVIDIA CORPORATION. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU General Public License,
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <linux/export.h>
 #include <linux/module.h>
 #include <linux/resource.h>
+#include <linux/pm_runtime.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/nvhost_vi_ioctl.h>
 
-#include <mach/iomap.h>
+#include <mach/pm_domains.h>
+#include <mach/clk.h>
+#include <media/tegra_v4l2_camera.h>
 
 #include "dev.h"
 #include "bus_client.h"
+#include "nvhost_acm.h"
+#include "t20/t20.h"
+#include "t30/t30.h"
+#include "t114/t114.h"
+#include "t148/t148.h"
+#include "t124/t124.h"
+#include "vi.h"
 
-static int vi_probe(struct nvhost_device *dev,
-       struct nvhost_device_id *id_table)
+#define MAX_DEVID_LENGTH       16
+
+static struct of_device_id tegra_vi_of_match[] = {
+#ifdef TEGRA_2X_OR_HIGHER_CONFIG
+       { .compatible = "nvidia,tegra20-vi",
+               .data = (struct nvhost_device_data *)&t20_vi_info },
+#endif
+#ifdef TEGRA_3X_OR_HIGHER_CONFIG
+       { .compatible = "nvidia,tegra30-vi",
+               .data = (struct nvhost_device_data *)&t30_vi_info },
+#endif
+#ifdef TEGRA_11X_OR_HIGHER_CONFIG
+       { .compatible = "nvidia,tegra114-vi",
+               .data = (struct nvhost_device_data *)&t11_vi_info },
+#endif
+#ifdef TEGRA_14X_OR_HIGHER_CONFIG
+       { .compatible = "nvidia,tegra148-vi",
+               .data = (struct nvhost_device_data *)&t14_vi_info },
+#endif
+#ifdef TEGRA_12X_OR_HIGHER_CONFIG
+       { .compatible = "nvidia,tegra124-vi",
+               .data = (struct nvhost_device_data *)&t124_vi_info },
+#endif
+       { },
+};
+
+#ifdef CONFIG_PM_GENERIC_DOMAINS
+static int vi_unpowergate(struct generic_pm_domain *domain)
+{
+       struct nvhost_device_data *pdata;
+
+       pdata = container_of(domain, struct nvhost_device_data, pd);
+       return nvhost_module_power_on(pdata->pdev);
+}
+
+static int vi_powergate(struct generic_pm_domain *domain)
+{
+       struct nvhost_device_data *pdata;
+
+       pdata = container_of(domain, struct nvhost_device_data, pd);
+       return nvhost_module_power_off(pdata->pdev);
+}
+#endif
+
+static struct i2c_camera_ctrl *i2c_ctrl;
+
+static int vi_probe(struct platform_device *dev)
 {
        int err = 0;
+       struct vi *tegra_vi;
+       struct nvhost_device_data *pdata = NULL;
+
+       if (dev->dev.of_node) {
+               const struct of_device_id *match;
+
+               match = of_match_device(tegra_vi_of_match, &dev->dev);
+               if (match)
+                       pdata = (struct nvhost_device_data *)match->data;
+       } else
+               pdata = (struct nvhost_device_data *)dev->dev.platform_data;
+
+       WARN_ON(!pdata);
+       if (!pdata) {
+               dev_info(&dev->dev, "no platform data\n");
+               return -ENODATA;
+       }
+
+       i2c_ctrl = pdata->private_data;
+
+       dev_info(&dev->dev, "%s: ++\n", __func__);
+
+       tegra_vi = kzalloc(sizeof(struct vi), GFP_KERNEL);
+       if (!tegra_vi) {
+               dev_err(&dev->dev, "can't allocate memory for vi\n");
+               return -ENOMEM;
+       }
+
+       tegra_vi->ndev = dev;
+       pdata->private_data = tegra_vi;
+
+       /* Create I2C Devices according to settings from board file */
+       if (i2c_ctrl && i2c_ctrl->new_devices)
+               i2c_ctrl->new_devices(dev);
+
+#ifdef CONFIG_TEGRA_CAMERA
+       tegra_vi->camera = tegra_camera_register(dev);
+       if (!tegra_vi->camera) {
+               dev_err(&dev->dev, "%s: can't register tegra_camera\n",
+                               __func__);
+               goto camera_i2c_unregister;
+       }
+#endif
+
+       pdata->pdev = dev;
+       mutex_init(&pdata->lock);
+       platform_set_drvdata(dev, pdata);
+       nvhost_module_init(dev);
+
+#ifdef CONFIG_PM_GENERIC_DOMAINS
+       pdata->pd.name = "ve";
+       pdata->pd.power_off = vi_powergate;
+       pdata->pd.power_on = vi_unpowergate;
+       pdata->pd.dev_ops.start = nvhost_module_enable_clk;
+       pdata->pd.dev_ops.stop = nvhost_module_disable_clk;
+
+       /* add module power domain and also add its domain
+        * as sub-domain of MC domain */
+       err = nvhost_module_add_domain(&pdata->pd, dev);
+
+       /* overwrite save/restore fptrs set by pm_genpd_init */
+       pdata->pd.domain.ops.suspend = nvhost_client_device_suspend;
+       pdata->pd.domain.ops.resume = nvhost_client_device_resume;
+#endif
 
        err = nvhost_client_device_get_resources(dev);
        if (err)
-               return err;
+               goto camera_unregister;
+
+       err = nvhost_client_device_init(dev);
+       if (err)
+               goto camera_unregister;
+
+#ifdef CONFIG_PM_RUNTIME
+       if (pdata->clockgate_delay) {
+               pm_runtime_set_autosuspend_delay(&dev->dev,
+                       pdata->clockgate_delay);
+               pm_runtime_use_autosuspend(&dev->dev);
+       }
+       pm_runtime_enable(&dev->dev);
+#else
+       nvhost_module_enable_clk(&dev->dev);
+#endif
+
+       return 0;
 
-       return nvhost_client_device_init(dev);
+camera_unregister:
+#ifdef CONFIG_TEGRA_CAMERA
+       tegra_camera_unregister(tegra_vi->camera);
+#endif
+camera_i2c_unregister:
+       if (i2c_ctrl && i2c_ctrl->remove_devices)
+               i2c_ctrl->remove_devices(dev);
+       pdata->private_data = i2c_ctrl;
+       kfree(tegra_vi);
+       return err;
 }
 
-static int __exit vi_remove(struct nvhost_device *dev)
+static int __exit vi_remove(struct platform_device *dev)
 {
-       /* Add clean-up */
+#ifdef CONFIG_TEGRA_CAMERA
+       int err = 0;
+#endif
+       struct nvhost_device_data *pdata =
+               (struct nvhost_device_data *)platform_get_drvdata(dev);
+       struct vi *tegra_vi = (struct vi *)pdata->private_data;
+
+       dev_info(&dev->dev, "%s: ++\n", __func__);
+
+       nvhost_client_device_release(dev);
+
+#ifdef CONFIG_TEGRA_CAMERA
+       err = tegra_camera_unregister(tegra_vi->camera);
+       if (err)
+               return err;
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+       pm_runtime_put(&dev->dev);
+       pm_runtime_disable(&dev->dev);
+#else
+       nvhost_module_disable_clk(&dev->dev);
+#endif
+       /* Remove I2C Devices according to settings from board file */
+       if (i2c_ctrl && i2c_ctrl->remove_devices)
+               i2c_ctrl->remove_devices(dev);
+
+       pdata->private_data = i2c_ctrl;
+       kfree(tegra_vi);
+
        return 0;
 }
 
-static int vi_suspend(struct nvhost_device *dev, pm_message_t state)
+#ifdef CONFIG_PM
+static int vi_suspend(struct device *dev)
 {
+#ifdef CONFIG_TEGRA_CAMERA
+       struct platform_device *pdev = to_platform_device(dev);
+       struct nvhost_device_data *pdata =
+               (struct nvhost_device_data *)platform_get_drvdata(pdev);
+       struct vi *tegra_vi = (struct vi *)pdata->private_data;
+       int ret;
+#endif
+
+       dev_info(dev, "%s: ++\n", __func__);
+
+#ifdef CONFIG_TEGRA_CAMERA
+       ret = tegra_camera_suspend(tegra_vi->camera);
+       if (ret) {
+               dev_info(dev, "%s: tegra_camera_suspend error=%d\n",
+               __func__, ret);
+               return ret;
+       }
+#endif
+
        return nvhost_client_device_suspend(dev);
 }
 
-static int vi_resume(struct nvhost_device *dev)
+static int vi_resume(struct device *dev)
 {
-       dev_info(&dev->dev, "resuming\n");
+#ifdef CONFIG_TEGRA_CAMERA
+       struct platform_device *pdev = to_platform_device(dev);
+       struct nvhost_device_data *pdata =
+               (struct nvhost_device_data *)platform_get_drvdata(pdev);
+       struct vi *tegra_vi = (struct vi *)pdata->private_data;
+#endif
+
+       dev_info(dev, "%s: ++\n", __func__);
+
+#ifdef CONFIG_TEGRA_CAMERA
+       tegra_camera_resume(tegra_vi->camera);
+#endif
+
        return 0;
 }
 
-static struct resource vi_resources = {
-       .name = "regs",
-       .start = TEGRA_VI_BASE,
-       .end = TEGRA_VI_BASE + TEGRA_VI_SIZE - 1,
-       .flags = IORESOURCE_MEM,
+static const struct dev_pm_ops vi_pm_ops = {
+       .suspend = vi_suspend,
+       .resume = vi_resume,
+#if defined(CONFIG_PM_RUNTIME) && !defined(CONFIG_PM_GENERIC_DOMAINS)
+       .runtime_suspend = nvhost_module_disable_clk,
+       .runtime_resume = nvhost_module_enable_clk,
+#endif
 };
+#endif
 
-struct nvhost_device *vi_device;
-
-static struct nvhost_driver vi_driver = {
+static struct platform_driver vi_driver = {
        .probe = vi_probe,
        .remove = __exit_p(vi_remove),
-#ifdef CONFIG_PM
-       .suspend = vi_suspend,
-       .resume = vi_resume,
-#endif
        .driver = {
                .owner = THIS_MODULE,
                .name = "vi",
+#ifdef CONFIG_PM
+               .pm = &vi_pm_ops,
+#endif
+#ifdef CONFIG_OF
+               .of_match_table = tegra_vi_of_match,
+#endif
        }
 };
 
 static int __init vi_init(void)
 {
-       int err;
+       return platform_driver_register(&vi_driver);
+}
 
-       vi_device = nvhost_get_device("vi");
-       if (!vi_device)
-               return -ENXIO;
+static void __exit vi_exit(void)
+{
+       platform_driver_unregister(&vi_driver);
+}
 
-       vi_device->resource = &vi_resources;
-       vi_device->num_resources = 1;
-       err = nvhost_device_register(vi_device);
-       if (err)
-               return err;
+#ifdef TEGRA_12X_OR_HIGHER_CONFIG
+int nvhost_vi_init(struct platform_device *dev)
+{
+       int ret = 0;
+       struct vi *tegra_vi;
+       tegra_vi = (struct vi *)nvhost_get_private_data(dev);
 
-       return nvhost_driver_register(&vi_driver);
+       tegra_vi->reg = regulator_get(&dev->dev, "avdd_dsi_csi");
+       if (IS_ERR(tegra_vi->reg)) {
+               if (tegra_vi->reg == ERR_PTR(-ENODEV)) {
+                       ret = -ENODEV;
+                       dev_info(&dev->dev,
+                               "%s: no regulator device\n",
+                               __func__);
+               } else {
+                       dev_err(&dev->dev,
+                               "%s: couldn't get regulator\n",
+                               __func__);
+               }
+               tegra_vi->reg = NULL;
+       }
+       return ret;
 }
 
-static void __exit vi_exit(void)
+void nvhost_vi_deinit(struct platform_device *dev)
 {
-       nvhost_driver_unregister(&vi_driver);
+       struct vi *tegra_vi;
+       tegra_vi = (struct vi *)nvhost_get_private_data(dev);
+
+       if (tegra_vi->reg)
+               regulator_put(tegra_vi->reg);
 }
 
-module_init(vi_init);
+int nvhost_vi_finalize_poweron(struct platform_device *dev)
+{
+       int ret = 0;
+       struct vi *tegra_vi;
+       tegra_vi = (struct vi *)nvhost_get_private_data(dev);
+
+       if (tegra_vi->reg) {
+               ret = regulator_enable(tegra_vi->reg);
+               if (ret)
+                       dev_err(&dev->dev,
+                               "%s: enable csi regulator failed.\n",
+                               __func__);
+       }
+       return ret;
+}
+
+int nvhost_vi_prepare_poweroff(struct platform_device *dev)
+{
+       int ret = 0;
+       struct vi *tegra_vi;
+       tegra_vi = (struct vi *)nvhost_get_private_data(dev);
+
+       if (tegra_vi->reg) {
+               ret = regulator_disable(tegra_vi->reg);
+               if (ret)
+                       dev_err(&dev->dev,
+                               "%s: disable csi regulator failed.\n",
+                               __func__);
+       }
+       return ret;
+}
+
+long tegra_vi_ioctl(struct file *file,
+               unsigned int cmd, unsigned long arg)
+{
+       struct vi *tegra_vi;
+
+       if (_IOC_TYPE(cmd) != NVHOST_VI_IOCTL_MAGIC)
+               return -EFAULT;
+
+       tegra_vi = file->private_data;
+       switch (cmd) {
+       case NVHOST_VI_IOCTL_ENABLE_TPG: {
+               uint enable;
+               int ret;
+               struct clk *clk;
+
+               if (copy_from_user(&enable,
+                       (const void __user *)arg, sizeof(uint))) {
+                       dev_err(&tegra_vi->ndev->dev,
+                               "%s: Failed to copy arg from user\n", __func__);
+                       return -EFAULT;
+               }
+
+               clk = clk_get(&tegra_vi->ndev->dev, "pll_d");
+               if (enable)
+                       ret = tegra_clk_cfg_ex(clk,
+                               TEGRA_CLK_PLLD_CSI_OUT_ENB, 1);
+               else
+                       ret = tegra_clk_cfg_ex(clk,
+                               TEGRA_CLK_MIPI_CSI_OUT_ENB, 1);
+               clk_put(clk);
+
+               return ret;
+       }
+       default:
+               dev_err(&tegra_vi->ndev->dev,
+                       "%s: Unknown vi ioctl.\n", __func__);
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int tegra_vi_open(struct inode *inode, struct file *file)
+{
+       struct nvhost_device_data *pdata;
+       struct vi *vi;
+
+       pdata = container_of(inode->i_cdev,
+               struct nvhost_device_data, ctrl_cdev);
+       BUG_ON(pdata == NULL);
+
+       vi = (struct vi *)pdata->private_data;
+       BUG_ON(vi == NULL);
+
+       file->private_data = vi;
+       return 0;
+}
+
+static int tegra_vi_release(struct inode *inode, struct file *file)
+{
+       return 0;
+}
+
+const struct file_operations tegra_vi_ctrl_ops = {
+       .owner = THIS_MODULE,
+       .open = tegra_vi_open,
+       .unlocked_ioctl = tegra_vi_ioctl,
+       .release = tegra_vi_release,
+};
+#endif
+
+late_initcall(vi_init);
 module_exit(vi_exit);
+MODULE_LICENSE("GPL v2");