video: tegra: host: Fix error handling
[linux-3.10.git] / drivers / video / tegra / host / vi / vi.c
index 9b2fc40..3ddafb6 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Tegra Graphics Host VI
  *
- * Copyright (c) 2012-2013, 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,
  */
 
 #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/pm_domains.h>
+#include <mach/clk.h>
+#include <media/tegra_v4l2_camera.h>
 
 #include "dev.h"
 #include "bus_client.h"
@@ -37,6 +41,8 @@
 #include "t124/t124.h"
 #include "vi.h"
 
+#define MAX_DEVID_LENGTH       16
+
 static struct of_device_id tegra_vi_of_match[] = {
 #ifdef TEGRA_2X_OR_HIGHER_CONFIG
        { .compatible = "nvidia,tegra20-vi",
@@ -79,6 +85,8 @@ static int vi_powergate(struct generic_pm_domain *domain)
 }
 #endif
 
+static struct i2c_camera_ctrl *i2c_ctrl;
+
 static int vi_probe(struct platform_device *dev)
 {
        int err = 0;
@@ -100,7 +108,10 @@ static int vi_probe(struct platform_device *dev)
                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");
@@ -110,14 +121,19 @@ static int vi_probe(struct platform_device *dev)
        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_register_fail;
+               goto camera_i2c_unregister;
        }
 #endif
+
        pdata->pdev = dev;
        mutex_init(&pdata->lock);
        platform_set_drvdata(dev, pdata);
@@ -141,11 +157,11 @@ static int vi_probe(struct platform_device *dev)
 
        err = nvhost_client_device_get_resources(dev);
        if (err)
-               goto camera_register_fail;
+               goto camera_unregister;
 
        err = nvhost_client_device_init(dev);
        if (err)
-               goto camera_register_fail;
+               goto camera_unregister;
 
 #ifdef CONFIG_PM_RUNTIME
        if (pdata->clockgate_delay) {
@@ -160,23 +176,31 @@ static int vi_probe(struct platform_device *dev)
 
        return 0;
 
-camera_register_fail:
+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 platform_device *dev)
 {
 #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;
-#endif
 
        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)
@@ -189,6 +213,12 @@ static int __exit vi_remove(struct platform_device *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;
 }
@@ -201,12 +231,18 @@ static int vi_suspend(struct 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
-       tegra_camera_suspend(tegra_vi->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);
@@ -233,8 +269,10 @@ static int vi_resume(struct device *dev)
 static const struct dev_pm_ops vi_pm_ops = {
        .suspend = vi_suspend,
        .resume = vi_resume,
-       SET_RUNTIME_PM_OPS(nvhost_module_disable_clk,
-               nvhost_module_enable_clk, NULL)
+#if defined(CONFIG_PM_RUNTIME) && !defined(CONFIG_PM_GENERIC_DOMAINS)
+       .runtime_suspend = nvhost_module_disable_clk,
+       .runtime_resume = nvhost_module_enable_clk,
+#endif
 };
 #endif
 
@@ -263,5 +301,141 @@ static void __exit vi_exit(void)
        platform_driver_unregister(&vi_driver);
 }
 
+#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);
+
+       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;
+}
+
+void nvhost_vi_deinit(struct platform_device *dev)
+{
+       struct vi *tegra_vi;
+       tegra_vi = (struct vi *)nvhost_get_private_data(dev);
+
+       if (tegra_vi->reg)
+               regulator_put(tegra_vi->reg);
+}
+
+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");