video: tegra: host: Fix error handling
[linux-3.10.git] / drivers / video / tegra / host / vi / vi.c
index 6b0afce..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"
@@ -81,14 +85,13 @@ 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;
        struct vi *tegra_vi;
        struct nvhost_device_data *pdata = NULL;
-#ifdef TEGRA_12X_OR_HIGHER_CONFIG
-       char devname[MAX_DEVID_LENGTH];
-#endif
 
        if (dev->dev.of_node) {
                const struct of_device_id *match;
@@ -105,6 +108,8 @@ 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);
@@ -114,25 +119,18 @@ static int vi_probe(struct platform_device *dev)
        }
 
        tegra_vi->ndev = dev;
-#ifdef TEGRA_12X_OR_HIGHER_CONFIG
-       snprintf(devname, MAX_DEVID_LENGTH,
-                (dev->id <= 0) ? "tegra_%s" : "tegra_%s.%d",
-                dev->name, dev->id);
-       if (dev->id == 1)
-               tegra_vi->clk_gate = clk_get_sys(devname, "vim2_clk");
-       else
-               tegra_vi->clk_gate = clk_get_sys(devname, "csus");
-       if (IS_ERR(tegra_vi->clk_gate))
-               dev_err(&dev->dev, "clk_get_sys failed for clk_gate of %s",
-                       devname);
-#endif
        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
 
@@ -159,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) {
@@ -178,7 +176,14 @@ 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;
 }
@@ -187,13 +192,15 @@ 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)
@@ -206,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;
 }
@@ -218,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);
@@ -250,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
 
@@ -288,7 +309,7 @@ int nvhost_vi_init(struct platform_device *dev)
        tegra_vi = (struct vi *)nvhost_get_private_data(dev);
 
        tegra_vi->reg = regulator_get(&dev->dev, "avdd_dsi_csi");
-       if (IS_ERR_OR_NULL(tegra_vi->reg)) {
+       if (IS_ERR(tegra_vi->reg)) {
                if (tegra_vi->reg == ERR_PTR(-ENODEV)) {
                        ret = -ENODEV;
                        dev_info(&dev->dev,
@@ -301,8 +322,6 @@ int nvhost_vi_init(struct platform_device *dev)
                }
                tegra_vi->reg = NULL;
        }
-       if (tegra_vi->clk_gate)
-               clk_prepare_enable(tegra_vi->clk_gate);
        return ret;
 }
 
@@ -313,8 +332,6 @@ void nvhost_vi_deinit(struct platform_device *dev)
 
        if (tegra_vi->reg)
                regulator_put(tegra_vi->reg);
-       if (tegra_vi->clk_gate)
-               clk_disable_unprepare(tegra_vi->clk_gate);
 }
 
 int nvhost_vi_finalize_poweron(struct platform_device *dev)
@@ -348,6 +365,77 @@ int nvhost_vi_prepare_poweroff(struct platform_device *dev)
        }
        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");