1 /*
2 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/mfd/core.h>
22 #include <linux/mfd/rk618.h>
23
24 static const struct mfd_cell rk618_devs[] = {
25 {
26 .name = "rk618-codec",
27 .of_compatible = "rockchip,rk618-codec",
28 }, {
29 .name = "rk618-cru",
30 .of_compatible = "rockchip,rk618-cru",
31 }, {
32 .name = "rk618-dsi",
33 .of_compatible = "rockchip,rk618-dsi",
34 }, {
35 .name = "rk618-hdmi",
36 .of_compatible = "rockchip,rk618-hdmi",
37 }, {
38 .name = "rk618-lvds",
39 .of_compatible = "rockchip,rk618-lvds",
40 }, {
41 .name = "rk618-rgb",
42 .of_compatible = "rockchip,rk618-rgb",
43 }, {
44 .name = "rk618-scaler",
45 .of_compatible = "rockchip,rk618-scaler",
46 }, {
47 .name = "rk618-vif",
48 .of_compatible = "rockchip,rk618-vif",
49 },
50 };
51
rk618_power_on(struct rk618 * rk618)52 static int rk618_power_on(struct rk618 *rk618)
53 {
54 u32 reg;
55 int ret;
56
57 ret = regulator_enable(rk618->supply);
58 if (ret < 0) {
59 dev_err(rk618->dev, "failed to enable supply: %d\n", ret);
60 return ret;
61 }
62
63 if (rk618->enable_gpio)
64 gpiod_direction_output(rk618->enable_gpio, 1);
65
66 usleep_range(1000, 2000);
67
68 ret = regmap_read(rk618->regmap, 0x0000, ®);
69 if (ret) {
70 gpiod_direction_output(rk618->reset_gpio, 0);
71 usleep_range(2000, 4000);
72 gpiod_direction_output(rk618->reset_gpio, 1);
73 usleep_range(50000, 60000);
74 gpiod_direction_output(rk618->reset_gpio, 0);
75 }
76
77 return 0;
78 }
79
rk618_power_off(struct rk618 * rk618)80 static void rk618_power_off(struct rk618 *rk618)
81 {
82 gpiod_direction_output(rk618->reset_gpio, 1);
83
84 if (rk618->enable_gpio)
85 gpiod_direction_output(rk618->enable_gpio, 0);
86
87 regulator_disable(rk618->supply);
88 }
89
90 static const struct regmap_config rk618_regmap_config = {
91 .name = "core",
92 .reg_bits = 16,
93 .val_bits = 32,
94 .reg_stride = 4,
95 .max_register = 0x9c,
96 .reg_format_endian = REGMAP_ENDIAN_NATIVE,
97 .val_format_endian = REGMAP_ENDIAN_NATIVE,
98 };
99
100 static int
rk618_probe(struct i2c_client * client,const struct i2c_device_id * id)101 rk618_probe(struct i2c_client *client, const struct i2c_device_id *id)
102 {
103 struct device *dev = &client->dev;
104 struct rk618 *rk618;
105 int ret;
106
107 rk618 = devm_kzalloc(dev, sizeof(*rk618), GFP_KERNEL);
108 if (!rk618)
109 return -ENOMEM;
110
111 rk618->dev = dev;
112 rk618->client = client;
113 i2c_set_clientdata(client, rk618);
114
115 rk618->supply = devm_regulator_get(dev, "power");
116 if (IS_ERR(rk618->supply))
117 return PTR_ERR(rk618->supply);
118
119 rk618->enable_gpio = devm_gpiod_get_optional(dev, "enable", 0);
120 if (IS_ERR(rk618->enable_gpio)) {
121 ret = PTR_ERR(rk618->enable_gpio);
122 dev_err(dev, "failed to request enable GPIO: %d\n", ret);
123 return ret;
124 }
125
126 rk618->reset_gpio = devm_gpiod_get(dev, "reset", 0);
127 if (IS_ERR(rk618->reset_gpio)) {
128 ret = PTR_ERR(rk618->reset_gpio);
129 dev_err(dev, "failed to request reset GPIO: %d\n", ret);
130 return ret;
131 }
132
133 rk618->clkin = devm_clk_get(dev, "clkin");
134 if (IS_ERR(rk618->clkin)) {
135 dev_err(dev, "failed to get clock\n");
136 return PTR_ERR(rk618->clkin);
137 }
138
139 ret = clk_prepare_enable(rk618->clkin);
140 if (ret) {
141 dev_err(dev, "unable to enable clock: %d\n", ret);
142 return ret;
143 }
144
145 rk618->regmap = devm_regmap_init_i2c(client, &rk618_regmap_config);
146 if (IS_ERR(rk618->regmap)) {
147 ret = PTR_ERR(rk618->regmap);
148 dev_err(dev, "failed to allocate register map: %d\n", ret);
149 goto err_clk_disable;
150 }
151
152 ret = rk618_power_on(rk618);
153 if (ret)
154 goto err_clk_disable;
155
156 ret = mfd_add_devices(dev, -1, rk618_devs, ARRAY_SIZE(rk618_devs),
157 NULL, 0, NULL);
158 if (ret) {
159 dev_err(dev, "failed to add subdev: %d\n", ret);
160 goto err_clk_disable;
161 }
162
163 return 0;
164
165 err_clk_disable:
166 clk_disable_unprepare(rk618->clkin);
167 return ret;
168 }
169
rk618_remove(struct i2c_client * client)170 static int rk618_remove(struct i2c_client *client)
171 {
172 struct rk618 *rk618 = i2c_get_clientdata(client);
173
174 mfd_remove_devices(rk618->dev);
175 rk618_power_off(rk618);
176 clk_disable_unprepare(rk618->clkin);
177
178 return 0;
179 }
180
rk618_shutdown(struct i2c_client * client)181 static void rk618_shutdown(struct i2c_client *client)
182 {
183 struct rk618 *rk618 = i2c_get_clientdata(client);
184
185 rk618_power_off(rk618);
186 clk_disable_unprepare(rk618->clkin);
187 }
188
rk618_suspend(struct device * dev)189 static int __maybe_unused rk618_suspend(struct device *dev)
190 {
191 pinctrl_pm_select_sleep_state(dev);
192
193 return 0;
194 }
195
rk618_resume(struct device * dev)196 static int __maybe_unused rk618_resume(struct device *dev)
197 {
198 pinctrl_pm_select_default_state(dev);
199
200 return 0;
201 }
202
203 static const struct dev_pm_ops rk618_pm_ops = {
204 SET_SYSTEM_SLEEP_PM_OPS(rk618_suspend, rk618_resume)
205 };
206
207 static const struct of_device_id rk618_of_match[] = {
208 { .compatible = "rockchip,rk618", },
209 {}
210 };
211 MODULE_DEVICE_TABLE(of, rk618_of_match);
212
213 static const struct i2c_device_id rk618_i2c_id[] = {
214 { "rk618", 0 },
215 {}
216 };
217 MODULE_DEVICE_TABLE(i2c, rk618_i2c_id);
218
219 static struct i2c_driver rk618_driver = {
220 .driver = {
221 .name = "rk618",
222 .of_match_table = of_match_ptr(rk618_of_match),
223 .pm = &rk618_pm_ops,
224 },
225 .probe = rk618_probe,
226 .remove = rk618_remove,
227 .shutdown = rk618_shutdown,
228 .id_table = rk618_i2c_id,
229 };
230 module_i2c_driver(rk618_driver);
231
232 MODULE_AUTHOR("Wyon Bi <bivvy.bi@rock-chips.com>");
233 MODULE_DESCRIPTION("Rockchip rk618 i2c driver");
234 MODULE_LICENSE("GPL v2");
235