GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / platform / exynos4-is / fimc-is-i2c.c
1 /*
2  * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
3  *
4  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5  *
6  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include "fimc-is-i2c.h"
20
21 struct fimc_is_i2c {
22         struct i2c_adapter adapter;
23         struct clk *clock;
24 };
25
26 /*
27  * An empty algorithm is used as the actual I2C bus controller driver
28  * is implemented in the FIMC-IS subsystem firmware and the host CPU
29  * doesn't access the I2C bus controller.
30  */
31 static u32 is_i2c_func(struct i2c_adapter *adap)
32 {
33         return I2C_FUNC_I2C;
34 }
35
36 static const struct i2c_algorithm fimc_is_i2c_algorithm = {
37         .functionality  = is_i2c_func,
38 };
39
40 static int fimc_is_i2c_probe(struct platform_device *pdev)
41 {
42         struct device_node *node = pdev->dev.of_node;
43         struct fimc_is_i2c *isp_i2c;
44         struct i2c_adapter *i2c_adap;
45         int ret;
46
47         isp_i2c = devm_kzalloc(&pdev->dev, sizeof(*isp_i2c), GFP_KERNEL);
48         if (!isp_i2c)
49                 return -ENOMEM;
50
51         isp_i2c->clock = devm_clk_get(&pdev->dev, "i2c_isp");
52         if (IS_ERR(isp_i2c->clock)) {
53                 dev_err(&pdev->dev, "failed to get the clock\n");
54                 return PTR_ERR(isp_i2c->clock);
55         }
56
57         i2c_adap = &isp_i2c->adapter;
58         i2c_adap->dev.of_node = node;
59         i2c_adap->dev.parent = &pdev->dev;
60         strlcpy(i2c_adap->name, "exynos4x12-isp-i2c", sizeof(i2c_adap->name));
61         i2c_adap->owner = THIS_MODULE;
62         i2c_adap->algo = &fimc_is_i2c_algorithm;
63         i2c_adap->class = I2C_CLASS_SPD;
64
65         platform_set_drvdata(pdev, isp_i2c);
66         pm_runtime_enable(&pdev->dev);
67
68         ret = i2c_add_adapter(i2c_adap);
69         if (ret < 0)
70                 goto err_pm_dis;
71         /*
72          * Client drivers of this adapter don't do any I2C transfers as that
73          * is handled by the ISP firmware.  But we rely on the runtime PM
74          * state propagation from the clients up to the adapter driver so
75          * clear the ignore_children flags here.  PM rutnime calls are not
76          * used in probe() handler of clients of this adapter so there is
77          * no issues with clearing the flag right after registering the I2C
78          * adapter.
79          */
80         pm_suspend_ignore_children(&i2c_adap->dev, false);
81         return 0;
82
83 err_pm_dis:
84         pm_runtime_disable(&pdev->dev);
85         return ret;
86 }
87
88 static int fimc_is_i2c_remove(struct platform_device *pdev)
89 {
90         struct fimc_is_i2c *isp_i2c = platform_get_drvdata(pdev);
91
92         pm_runtime_disable(&pdev->dev);
93         i2c_del_adapter(&isp_i2c->adapter);
94
95         return 0;
96 }
97
98 #ifdef CONFIG_PM
99 static int fimc_is_i2c_runtime_suspend(struct device *dev)
100 {
101         struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
102
103         clk_disable_unprepare(isp_i2c->clock);
104         return 0;
105 }
106
107 static int fimc_is_i2c_runtime_resume(struct device *dev)
108 {
109         struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
110
111         return clk_prepare_enable(isp_i2c->clock);
112 }
113 #endif
114
115 #ifdef CONFIG_PM_SLEEP
116 static int fimc_is_i2c_suspend(struct device *dev)
117 {
118         if (pm_runtime_suspended(dev))
119                 return 0;
120
121         return fimc_is_i2c_runtime_suspend(dev);
122 }
123
124 static int fimc_is_i2c_resume(struct device *dev)
125 {
126         if (pm_runtime_suspended(dev))
127                 return 0;
128
129         return fimc_is_i2c_runtime_resume(dev);
130 }
131 #endif
132
133 static const struct dev_pm_ops fimc_is_i2c_pm_ops = {
134         SET_RUNTIME_PM_OPS(fimc_is_i2c_runtime_suspend,
135                                         fimc_is_i2c_runtime_resume, NULL)
136         SET_SYSTEM_SLEEP_PM_OPS(fimc_is_i2c_suspend, fimc_is_i2c_resume)
137 };
138
139 static const struct of_device_id fimc_is_i2c_of_match[] = {
140         { .compatible = FIMC_IS_I2C_COMPATIBLE },
141         { },
142 };
143
144 static struct platform_driver fimc_is_i2c_driver = {
145         .probe          = fimc_is_i2c_probe,
146         .remove         = fimc_is_i2c_remove,
147         .driver = {
148                 .of_match_table = fimc_is_i2c_of_match,
149                 .name           = "fimc-isp-i2c",
150                 .pm             = &fimc_is_i2c_pm_ops,
151         }
152 };
153
154 int fimc_is_register_i2c_driver(void)
155 {
156         return platform_driver_register(&fimc_is_i2c_driver);
157 }
158
159 void fimc_is_unregister_i2c_driver(void)
160 {
161         platform_driver_unregister(&fimc_is_i2c_driver);
162 }