1238c84f7SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0-only
2238c84f7SMauro Carvalho Chehab /*
3238c84f7SMauro Carvalho Chehab * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
4238c84f7SMauro Carvalho Chehab *
5238c84f7SMauro Carvalho Chehab * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6238c84f7SMauro Carvalho Chehab *
7238c84f7SMauro Carvalho Chehab * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
8238c84f7SMauro Carvalho Chehab */
9238c84f7SMauro Carvalho Chehab
10238c84f7SMauro Carvalho Chehab #include <linux/clk.h>
11238c84f7SMauro Carvalho Chehab #include <linux/module.h>
12238c84f7SMauro Carvalho Chehab #include <linux/i2c.h>
13238c84f7SMauro Carvalho Chehab #include <linux/platform_device.h>
14238c84f7SMauro Carvalho Chehab #include <linux/pm_runtime.h>
15238c84f7SMauro Carvalho Chehab #include <linux/slab.h>
16238c84f7SMauro Carvalho Chehab #include "fimc-is-i2c.h"
17238c84f7SMauro Carvalho Chehab
18238c84f7SMauro Carvalho Chehab struct fimc_is_i2c {
19238c84f7SMauro Carvalho Chehab struct i2c_adapter adapter;
20238c84f7SMauro Carvalho Chehab struct clk *clock;
21238c84f7SMauro Carvalho Chehab };
22238c84f7SMauro Carvalho Chehab
23238c84f7SMauro Carvalho Chehab /*
24238c84f7SMauro Carvalho Chehab * An empty algorithm is used as the actual I2C bus controller driver
25238c84f7SMauro Carvalho Chehab * is implemented in the FIMC-IS subsystem firmware and the host CPU
26238c84f7SMauro Carvalho Chehab * doesn't access the I2C bus controller.
27238c84f7SMauro Carvalho Chehab */
is_i2c_func(struct i2c_adapter * adap)28238c84f7SMauro Carvalho Chehab static u32 is_i2c_func(struct i2c_adapter *adap)
29238c84f7SMauro Carvalho Chehab {
30238c84f7SMauro Carvalho Chehab return I2C_FUNC_I2C;
31238c84f7SMauro Carvalho Chehab }
32238c84f7SMauro Carvalho Chehab
33238c84f7SMauro Carvalho Chehab static const struct i2c_algorithm fimc_is_i2c_algorithm = {
34238c84f7SMauro Carvalho Chehab .functionality = is_i2c_func,
35238c84f7SMauro Carvalho Chehab };
36238c84f7SMauro Carvalho Chehab
fimc_is_i2c_probe(struct platform_device * pdev)37238c84f7SMauro Carvalho Chehab static int fimc_is_i2c_probe(struct platform_device *pdev)
38238c84f7SMauro Carvalho Chehab {
39238c84f7SMauro Carvalho Chehab struct device_node *node = pdev->dev.of_node;
40238c84f7SMauro Carvalho Chehab struct fimc_is_i2c *isp_i2c;
41238c84f7SMauro Carvalho Chehab struct i2c_adapter *i2c_adap;
42238c84f7SMauro Carvalho Chehab int ret;
43238c84f7SMauro Carvalho Chehab
44238c84f7SMauro Carvalho Chehab isp_i2c = devm_kzalloc(&pdev->dev, sizeof(*isp_i2c), GFP_KERNEL);
45238c84f7SMauro Carvalho Chehab if (!isp_i2c)
46238c84f7SMauro Carvalho Chehab return -ENOMEM;
47238c84f7SMauro Carvalho Chehab
48238c84f7SMauro Carvalho Chehab isp_i2c->clock = devm_clk_get(&pdev->dev, "i2c_isp");
49238c84f7SMauro Carvalho Chehab if (IS_ERR(isp_i2c->clock)) {
50238c84f7SMauro Carvalho Chehab dev_err(&pdev->dev, "failed to get the clock\n");
51238c84f7SMauro Carvalho Chehab return PTR_ERR(isp_i2c->clock);
52238c84f7SMauro Carvalho Chehab }
53238c84f7SMauro Carvalho Chehab
54238c84f7SMauro Carvalho Chehab i2c_adap = &isp_i2c->adapter;
55238c84f7SMauro Carvalho Chehab i2c_adap->dev.of_node = node;
56238c84f7SMauro Carvalho Chehab i2c_adap->dev.parent = &pdev->dev;
57238c84f7SMauro Carvalho Chehab strscpy(i2c_adap->name, "exynos4x12-isp-i2c", sizeof(i2c_adap->name));
58238c84f7SMauro Carvalho Chehab i2c_adap->owner = THIS_MODULE;
59238c84f7SMauro Carvalho Chehab i2c_adap->algo = &fimc_is_i2c_algorithm;
60238c84f7SMauro Carvalho Chehab i2c_adap->class = I2C_CLASS_SPD;
61238c84f7SMauro Carvalho Chehab
62238c84f7SMauro Carvalho Chehab platform_set_drvdata(pdev, isp_i2c);
63238c84f7SMauro Carvalho Chehab pm_runtime_enable(&pdev->dev);
64238c84f7SMauro Carvalho Chehab
65238c84f7SMauro Carvalho Chehab ret = i2c_add_adapter(i2c_adap);
66238c84f7SMauro Carvalho Chehab if (ret < 0)
67238c84f7SMauro Carvalho Chehab goto err_pm_dis;
68238c84f7SMauro Carvalho Chehab /*
69238c84f7SMauro Carvalho Chehab * Client drivers of this adapter don't do any I2C transfers as that
70238c84f7SMauro Carvalho Chehab * is handled by the ISP firmware. But we rely on the runtime PM
71238c84f7SMauro Carvalho Chehab * state propagation from the clients up to the adapter driver so
72238c84f7SMauro Carvalho Chehab * clear the ignore_children flags here. PM rutnime calls are not
73238c84f7SMauro Carvalho Chehab * used in probe() handler of clients of this adapter so there is
74238c84f7SMauro Carvalho Chehab * no issues with clearing the flag right after registering the I2C
75238c84f7SMauro Carvalho Chehab * adapter.
76238c84f7SMauro Carvalho Chehab */
77238c84f7SMauro Carvalho Chehab pm_suspend_ignore_children(&i2c_adap->dev, false);
78238c84f7SMauro Carvalho Chehab return 0;
79238c84f7SMauro Carvalho Chehab
80238c84f7SMauro Carvalho Chehab err_pm_dis:
81238c84f7SMauro Carvalho Chehab pm_runtime_disable(&pdev->dev);
82238c84f7SMauro Carvalho Chehab return ret;
83238c84f7SMauro Carvalho Chehab }
84238c84f7SMauro Carvalho Chehab
fimc_is_i2c_remove(struct platform_device * pdev)85*459ce214SUwe Kleine-König static void fimc_is_i2c_remove(struct platform_device *pdev)
86238c84f7SMauro Carvalho Chehab {
87238c84f7SMauro Carvalho Chehab struct fimc_is_i2c *isp_i2c = platform_get_drvdata(pdev);
88238c84f7SMauro Carvalho Chehab
89238c84f7SMauro Carvalho Chehab pm_runtime_disable(&pdev->dev);
90238c84f7SMauro Carvalho Chehab i2c_del_adapter(&isp_i2c->adapter);
91238c84f7SMauro Carvalho Chehab }
92238c84f7SMauro Carvalho Chehab
93238c84f7SMauro Carvalho Chehab #ifdef CONFIG_PM
fimc_is_i2c_runtime_suspend(struct device * dev)94238c84f7SMauro Carvalho Chehab static int fimc_is_i2c_runtime_suspend(struct device *dev)
95238c84f7SMauro Carvalho Chehab {
96238c84f7SMauro Carvalho Chehab struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
97238c84f7SMauro Carvalho Chehab
98238c84f7SMauro Carvalho Chehab clk_disable_unprepare(isp_i2c->clock);
99238c84f7SMauro Carvalho Chehab return 0;
100238c84f7SMauro Carvalho Chehab }
101238c84f7SMauro Carvalho Chehab
fimc_is_i2c_runtime_resume(struct device * dev)102238c84f7SMauro Carvalho Chehab static int fimc_is_i2c_runtime_resume(struct device *dev)
103238c84f7SMauro Carvalho Chehab {
104238c84f7SMauro Carvalho Chehab struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
105238c84f7SMauro Carvalho Chehab
106238c84f7SMauro Carvalho Chehab return clk_prepare_enable(isp_i2c->clock);
107238c84f7SMauro Carvalho Chehab }
108238c84f7SMauro Carvalho Chehab #endif
109238c84f7SMauro Carvalho Chehab
110238c84f7SMauro Carvalho Chehab #ifdef CONFIG_PM_SLEEP
fimc_is_i2c_suspend(struct device * dev)111238c84f7SMauro Carvalho Chehab static int fimc_is_i2c_suspend(struct device *dev)
112238c84f7SMauro Carvalho Chehab {
113238c84f7SMauro Carvalho Chehab if (pm_runtime_suspended(dev))
114238c84f7SMauro Carvalho Chehab return 0;
115238c84f7SMauro Carvalho Chehab
116238c84f7SMauro Carvalho Chehab return fimc_is_i2c_runtime_suspend(dev);
117238c84f7SMauro Carvalho Chehab }
118238c84f7SMauro Carvalho Chehab
fimc_is_i2c_resume(struct device * dev)119238c84f7SMauro Carvalho Chehab static int fimc_is_i2c_resume(struct device *dev)
120238c84f7SMauro Carvalho Chehab {
121238c84f7SMauro Carvalho Chehab if (pm_runtime_suspended(dev))
122238c84f7SMauro Carvalho Chehab return 0;
123238c84f7SMauro Carvalho Chehab
124238c84f7SMauro Carvalho Chehab return fimc_is_i2c_runtime_resume(dev);
125238c84f7SMauro Carvalho Chehab }
126238c84f7SMauro Carvalho Chehab #endif
127238c84f7SMauro Carvalho Chehab
128238c84f7SMauro Carvalho Chehab static const struct dev_pm_ops fimc_is_i2c_pm_ops = {
129238c84f7SMauro Carvalho Chehab SET_RUNTIME_PM_OPS(fimc_is_i2c_runtime_suspend,
130238c84f7SMauro Carvalho Chehab fimc_is_i2c_runtime_resume, NULL)
131238c84f7SMauro Carvalho Chehab SET_SYSTEM_SLEEP_PM_OPS(fimc_is_i2c_suspend, fimc_is_i2c_resume)
132238c84f7SMauro Carvalho Chehab };
133238c84f7SMauro Carvalho Chehab
134238c84f7SMauro Carvalho Chehab static const struct of_device_id fimc_is_i2c_of_match[] = {
135238c84f7SMauro Carvalho Chehab { .compatible = FIMC_IS_I2C_COMPATIBLE },
136238c84f7SMauro Carvalho Chehab { },
137238c84f7SMauro Carvalho Chehab };
138238c84f7SMauro Carvalho Chehab
139238c84f7SMauro Carvalho Chehab static struct platform_driver fimc_is_i2c_driver = {
140238c84f7SMauro Carvalho Chehab .probe = fimc_is_i2c_probe,
141*459ce214SUwe Kleine-König .remove_new = fimc_is_i2c_remove,
142238c84f7SMauro Carvalho Chehab .driver = {
143238c84f7SMauro Carvalho Chehab .of_match_table = fimc_is_i2c_of_match,
144238c84f7SMauro Carvalho Chehab .name = "fimc-isp-i2c",
145238c84f7SMauro Carvalho Chehab .pm = &fimc_is_i2c_pm_ops,
146238c84f7SMauro Carvalho Chehab }
147238c84f7SMauro Carvalho Chehab };
148238c84f7SMauro Carvalho Chehab
fimc_is_register_i2c_driver(void)149238c84f7SMauro Carvalho Chehab int fimc_is_register_i2c_driver(void)
150238c84f7SMauro Carvalho Chehab {
151238c84f7SMauro Carvalho Chehab return platform_driver_register(&fimc_is_i2c_driver);
152238c84f7SMauro Carvalho Chehab }
153238c84f7SMauro Carvalho Chehab
fimc_is_unregister_i2c_driver(void)154238c84f7SMauro Carvalho Chehab void fimc_is_unregister_i2c_driver(void)
155238c84f7SMauro Carvalho Chehab {
156238c84f7SMauro Carvalho Chehab platform_driver_unregister(&fimc_is_i2c_driver);
157238c84f7SMauro Carvalho Chehab }
158