mfd: fusb302: change to host when connect type-c to standard-a cable
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / rk1000-core.c
1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/i2c.h>
4 #include <linux/device.h>
5 #include <linux/delay.h>
6 #include <linux/err.h>
7 #include <linux/clk.h>
8 #include <linux/gpio.h>
9 #include <linux/slab.h>
10 #include <linux/mfd/core.h>
11 #include <linux/mfd/rk1000.h>
12 #include <linux/of.h>
13 #include <linux/of_gpio.h>
14 #include <linux/regulator/consumer.h>
15
16 #define RK1000_CORE_DBG 0
17
18 #if RK1000_CORE_DBG
19 #define DBG(x...)       pr_info(x)
20 #else
21 #define DBG(x...)
22 #endif
23
24 #define CTRL_ADC        0x00
25 #define CTRL_CODEC      0x01
26 #define CTRL_I2C        0x02
27 #define CTRL_TVE        0x03
28 #define RGB2CCIR_RESET  0x04
29 #define ADC_START       0x05
30
31 struct rk1000 {
32         struct i2c_client *client;
33         struct device *dev;
34         struct dentry *debugfs_dir;
35         struct ioctrl io_power;
36         struct ioctrl io_reset;
37 };
38
39 static struct rk1000 *rk1000;
40 int cvbsmode = -1;
41
42 void rk1000_reset_ctrl(int enable)
43 {
44         DBG("rk1000_reset_ctrl\n");
45         if (rk1000 && gpio_is_valid(rk1000->io_reset.gpio)) {
46                 if (enable) {
47                         gpio_set_value(rk1000->io_reset.gpio,
48                                        !(rk1000->io_reset.active));
49                 } else {
50                         DBG("rk1000 reset pull low\n");
51                         gpio_set_value(rk1000->io_reset.gpio,
52                                        (rk1000->io_reset.active));
53                 }
54         }
55 }
56
57 int rk1000_i2c_send(const u8 addr, const u8 reg, const u8 value)
58 {
59         struct i2c_adapter *adap;
60         struct i2c_msg msg;
61         int ret;
62         char buf[2];
63
64         if (rk1000 == NULL || rk1000->client == NULL) {
65                 DBG("rk1000 not init!\n");
66                 return -1;
67         }
68         adap = rk1000->client->adapter;
69         buf[0] = reg;
70         buf[1] = value;
71         msg.addr = addr;
72         msg.flags = rk1000->client->flags;
73         msg.len = 2;
74         msg.buf = buf;
75         msg.scl_rate = RK1000_I2C_RATE;
76         ret = i2c_transfer(adap, &msg, 1);
77         if (ret != 1) {
78                 DBG("rk1000 control i2c write err,ret =%d\n", ret);
79                 return -1;
80         }
81         return 0;
82 }
83
84 int rk1000_i2c_recv(const u8 addr, const u8 reg, const char *buf)
85 {
86         struct i2c_adapter *adap;
87         struct i2c_msg msgs[2];
88         int ret;
89
90         if (rk1000 == NULL || rk1000->client == NULL) {
91                 DBG("rk1000 not init!\n");
92                 return -1;
93         }
94         adap = rk1000->client->adapter;
95         msgs[0].addr = addr;
96         msgs[0].flags = rk1000->client->flags;
97         msgs[0].len = 1;
98         msgs[0].buf = (unsigned char *)(&reg);
99         msgs[0].scl_rate = RK1000_I2C_RATE;
100         msgs[1].addr = addr;
101         msgs[1].flags = rk1000->client->flags | I2C_M_RD;
102         msgs[1].len = 1;
103         msgs[1].buf = (unsigned char *)buf;
104         msgs[1].scl_rate = RK1000_I2C_RATE;
105         ret = i2c_transfer(adap, msgs, 2);
106         return (ret == 2) ? 0 : -1;
107 }
108
109 static ssize_t rk1000_show(struct device *dev,
110                            struct device_attribute *attr,
111                            char *buf)
112 {
113         int ret = -1;
114         int i = 0;
115         unsigned char tv_encoder_regs[] = {0x00, 0x00, 0x00, 0x03, 0x00, 0x00};
116         unsigned char tv_encoder_control_regs[] = {0x43, 0x01};
117
118         for (i = 0; i < sizeof(tv_encoder_regs); i++) {
119                 ret = rk1000_i2c_recv(I2C_ADDR_TVE, i, buf);
120                 pr_info("---%x--\n", buf[0]);
121                 if (ret < 0) {
122                         pr_err("rk1000_tv_write_block err!\n");
123                         return ret;
124                 }
125         }
126
127         for (i = 0; i < sizeof(tv_encoder_control_regs); i++) {
128                 ret = rk1000_i2c_recv(I2C_ADDR_CTRL, i + 3, buf);
129                 pr_info("cntrl---%x--\n", buf[0]);
130                 if (ret < 0) {
131                         pr_err("rk1000_control_write_block err!\n");
132                         return ret;
133                 }
134         }
135         return 0;
136 }
137
138 static DEVICE_ATTR(rkcontrl, S_IRUGO, rk1000_show, NULL);
139
140
141 static int __init bootloader_cvbs_setup(char *str)
142 {
143         static int ret;
144
145         if (str) {
146                 pr_info("cvbs init tve.format is %s\n", str);
147                 ret = kstrtoint(str, 0, &cvbsmode);
148         }
149         return 0;
150 }
151 early_param("tve.format", bootloader_cvbs_setup);
152
153 #ifdef CONFIG_PM
154 static int rk1000_control_suspend(struct device *dev)
155 {
156         int ret;
157
158         DBG("rk1000_control_suspend\n");
159         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x22);
160         DBG("ret=0x%x\n", ret);
161         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x00);
162         DBG("ret=0x%x\n", ret);
163         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x07);
164         DBG("ret=0x%x\n", ret);
165         /* rk1000_reset_ctrl(0); */
166         return 0;
167 }
168
169 static int rk1000_control_resume(struct device *dev)
170 {
171         int ret;
172
173         /* rk1000_reset_ctrl(1); */
174         DBG("rk1000_control_resume\n");
175         /* ADC power off */
176         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_ADC, 0x88);
177         DBG("ret=0x%x\n", ret);
178         #ifdef CONFIG_SND_SOC_RK1000
179         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x00);
180         #else
181         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x0d);
182         #endif
183         DBG("ret=0x%x\n", ret);
184         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_I2C, 0x22);
185         DBG("ret=0x%x\n", ret);
186         /* rk1000_codec_reg_set(); */
187         return 0;
188 }
189 #endif
190
191
192
193 static int rk1000_probe(struct i2c_client *client,
194                         const struct i2c_device_id *id)
195 {
196         struct device_node *rk1000_np;
197         enum of_gpio_flags flags;
198         int ret;
199
200         DBG("[%s] start\n", __func__);
201         rk1000 = kmalloc(sizeof(*rk1000), GFP_KERNEL);
202         if (!rk1000) {
203                 dev_err(&client->dev, ">> rk1000 core inf kmalloc fail!");
204                 return -ENOMEM;
205         }
206
207         memset(rk1000, 0, sizeof(struct rk1000));
208         rk1000->client = client;
209         rk1000->dev = &client->dev;
210         rk1000_np = rk1000->dev->of_node;
211
212         if (cvbsmode < 0) {
213                 /********Get reset pin***********/
214                 rk1000->io_reset.gpio = of_get_named_gpio_flags(rk1000_np,
215                                                                 "gpio-reset",
216                                                                 0, &flags);
217                 if (!gpio_is_valid(rk1000->io_reset.gpio)) {
218                         DBG("invalid rk1000->io_reset.gpio: %d\n",
219                             rk1000->io_reset.gpio);
220                         ret = -1;
221                         goto err;
222                 }
223                 ret = gpio_request(rk1000->io_reset.gpio, "rk1000-reset-io");
224                 if (ret != 0) {
225                         DBG("gpio_request rk1000->io_reset.gpio invalid: %d\n",
226                             rk1000->io_reset.gpio);
227                         goto err;
228                 }
229                 rk1000->io_reset.active = !(flags & OF_GPIO_ACTIVE_LOW);
230                 gpio_direction_output(rk1000->io_reset.gpio,
231                                       !(rk1000->io_reset.active));
232                 usleep_range(500, 1000);
233                 /********Get power pin***********/
234                 rk1000->io_power.gpio = of_get_named_gpio_flags(rk1000_np,
235                                                                 "gpio-power",
236                                                                 0, &flags);
237                 if (gpio_is_valid(rk1000->io_power.gpio)) {
238                         ret = gpio_request(rk1000->io_power.gpio,
239                                            "rk1000-power-io");
240                         if (ret != 0) {
241                                 DBG("request gpio for power invalid: %d\n",
242                                     rk1000->io_power.gpio);
243                                 goto err;
244                         }
245                         rk1000->io_power.active =
246                                         !(flags & OF_GPIO_ACTIVE_LOW);
247                         gpio_direction_output(rk1000->io_power.gpio,
248                                               rk1000->io_power.active);
249                 }
250                 /********rk1000 reset***********/
251                 gpio_set_value(rk1000->io_reset.gpio,
252                                rk1000->io_reset.active);
253                 usleep_range(5000, 10000);
254                 gpio_set_value(rk1000->io_reset.gpio,
255                                !(rk1000->io_reset.active));
256         }
257         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_ADC, 0x88);
258         #ifdef CONFIG_SND_SOC_RK1000
259         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x00);
260         #else
261         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x0d);
262         #endif
263         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_I2C, 0x22);
264
265         if (cvbsmode < 0)
266                 rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x00);
267
268         device_create_file(&client->dev, &dev_attr_rkcontrl);
269         DBG("rk1000 probe ok\n");
270         return 0;
271 err:
272         kfree(rk1000);
273         rk1000 = NULL;
274         return ret;
275 }
276
277 static int rk1000_remove(struct i2c_client *client)
278 {
279         return 0;
280 }
281
282 static const struct i2c_device_id rk1000_id[] = {
283         { "rk1000_control", 0 },
284         { }
285 };
286 MODULE_DEVICE_TABLE(i2c, rk1000_id);
287
288 static const struct dev_pm_ops rockchip_rk1000_pm_ops = {
289         .suspend_late = rk1000_control_suspend,
290         .resume_early = rk1000_control_resume,
291 };
292
293 static struct i2c_driver rk1000_driver = {
294         .driver = {
295                 .name = "rk1000_control",
296                 #ifdef CONFIG_PM
297                 .pm     = &rockchip_rk1000_pm_ops,
298                 #endif
299         },
300         .probe = rk1000_probe,
301         .remove = rk1000_remove,
302         .id_table = rk1000_id,
303 };
304
305
306 static int __init rk1000_init(void)
307 {
308         return i2c_add_driver(&rk1000_driver);
309 }
310
311 static void __exit rk1000_exit(void)
312 {
313         i2c_del_driver(&rk1000_driver);
314 }
315
316 fs_initcall_sync(rk1000_init);
317 module_exit(rk1000_exit);
318
319
320 MODULE_DESCRIPTION("RK1000 control driver");
321 MODULE_AUTHOR("Rock-chips, <www.rock-chips.com>");
322 MODULE_LICENSE("GPL");