Merge tag lsk-v3.10-15.03-android
[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
41 void rk1000_reset_ctrl(int enable)
42 {
43         DBG("rk1000_reset_ctrl\n");
44         if (rk1000 && gpio_is_valid(rk1000->io_reset.gpio)) {
45                 if (enable) {
46                         gpio_set_value(rk1000->io_reset.gpio,
47                                        !(rk1000->io_reset.active));
48                 } else {
49                         DBG("rk1000 reset pull low\n");
50                         gpio_set_value(rk1000->io_reset.gpio,
51                                        (rk1000->io_reset.active));
52                 }
53         }
54 }
55
56 int rk1000_i2c_send(const u8 addr, const u8 reg, const u8 value)
57 {
58         struct i2c_adapter *adap;
59         struct i2c_msg msg;
60         int ret;
61         char buf[2];
62
63         if (rk1000 == NULL || rk1000->client == NULL) {
64                 DBG("rk1000 not init!\n");
65                 return -1;
66         }
67         adap = rk1000->client->adapter;
68         buf[0] = reg;
69         buf[1] = value;
70         msg.addr = addr;
71         msg.flags = rk1000->client->flags;
72         msg.len = 2;
73         msg.buf = buf;
74         msg.scl_rate = RK1000_I2C_RATE;
75         ret = i2c_transfer(adap, &msg, 1);
76         if (ret != 1) {
77                 DBG("rk1000 control i2c write err,ret =%d\n", ret);
78                 return -1;
79         }
80         return 0;
81 }
82
83 int rk1000_i2c_recv(const u8 addr, const u8 reg, const char *buf)
84 {
85         struct i2c_adapter *adap;
86         struct i2c_msg msgs[2];
87         int ret;
88
89         if (rk1000 == NULL || rk1000->client == NULL) {
90                 DBG("rk1000 not init!\n");
91                 return -1;
92         }
93         adap = rk1000->client->adapter;
94         msgs[0].addr = addr;
95         msgs[0].flags = rk1000->client->flags;
96         msgs[0].len = 1;
97         msgs[0].buf = (unsigned char *)(&reg);
98         msgs[0].scl_rate = RK1000_I2C_RATE;
99         msgs[1].addr = addr;
100         msgs[1].flags = rk1000->client->flags | I2C_M_RD;
101         msgs[1].len = 1;
102         msgs[1].buf = (unsigned char *)buf;
103         msgs[1].scl_rate = RK1000_I2C_RATE;
104         ret = i2c_transfer(adap, msgs, 2);
105         return (ret == 2) ? 0 : -1;
106 }
107
108
109 #ifdef CONFIG_PM
110 static int rk1000_control_suspend(struct device *dev)
111 {
112         int ret;
113
114         DBG("rk1000_control_suspend\n");
115         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x22);
116         DBG("ret=0x%x\n", ret);
117         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x00);
118         DBG("ret=0x%x\n", ret);
119         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x07);
120         DBG("ret=0x%x\n", ret);
121         rk1000_reset_ctrl(0);
122         return 0;
123 }
124
125 static int rk1000_control_resume(struct device *dev)
126 {
127         int ret;
128
129         rk1000_reset_ctrl(1);
130         DBG("rk1000_control_resume\n");
131         /* ADC power off */
132         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_ADC, 0x88);
133         DBG("ret=0x%x\n", ret);
134         #ifdef CONFIG_SND_SOC_RK1000
135         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x00);
136         #else
137         ret = rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x0d);
138         #endif
139         DBG("ret=0x%x\n", ret);
140         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_I2C, 0x22);
141         DBG("ret=0x%x\n", ret);
142         /* rk1000_codec_reg_set(); */
143         return 0;
144 }
145 #endif
146
147
148
149 static int rk1000_probe(struct i2c_client *client,
150                         const struct i2c_device_id *id)
151 {
152         struct device_node *rk1000_np;
153         enum of_gpio_flags flags;
154         int ret;
155
156         DBG("[%s] start\n", __func__);
157         rk1000 = kmalloc(sizeof(*rk1000), GFP_KERNEL);
158         if (!rk1000) {
159                 dev_err(&client->dev, ">> rk1000 core inf kmalloc fail!");
160                 return -ENOMEM;
161         }
162         memset(rk1000, 0, sizeof(struct rk1000));
163         rk1000->client = client;
164         rk1000->dev = &client->dev;
165         rk1000_np = rk1000->dev->of_node;
166         /********Get reset pin***********/
167         rk1000->io_reset.gpio = of_get_named_gpio_flags(rk1000_np,
168                                                         "gpio-reset",
169                                                         0, &flags);
170         if (!gpio_is_valid(rk1000->io_reset.gpio)) {
171                 DBG("invalid rk1000->io_reset.gpio: %d\n",
172                     rk1000->io_reset.gpio);
173                 ret = -1;
174                 goto err;
175         }
176         ret = gpio_request(rk1000->io_reset.gpio, "rk1000-reset-io");
177         if (ret != 0) {
178                 DBG("gpio_request rk1000->io_reset.gpio invalid: %d\n",
179                     rk1000->io_reset.gpio);
180                 goto err;
181         }
182         rk1000->io_reset.active = !(flags & OF_GPIO_ACTIVE_LOW);
183         gpio_direction_output(rk1000->io_reset.gpio,
184                               !(rk1000->io_reset.active));
185         msleep(20);
186         /********Get power pin***********/
187         rk1000->io_power.gpio = of_get_named_gpio_flags(rk1000_np,
188                                                         "gpio-power",
189                                                         0, &flags);
190         if (gpio_is_valid(rk1000->io_power.gpio)) {
191                 ret = gpio_request(rk1000->io_power.gpio, "rk1000-power-io");
192                 if (ret != 0) {
193                         DBG("gpio_request rk1000->io_power.gpio invalid: %d\n",
194                             rk1000->io_power.gpio);
195                         goto err;
196                 }
197                 rk1000->io_power.active = !(flags & OF_GPIO_ACTIVE_LOW);
198                 gpio_direction_output(rk1000->io_power.gpio,
199                                       rk1000->io_power.active);
200         }
201         /********rk1000 reset***********/
202         gpio_set_value(rk1000->io_reset.gpio, rk1000->io_reset.active);
203         msleep(100);
204         gpio_set_value(rk1000->io_reset.gpio, !(rk1000->io_reset.active));
205         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_ADC, 0x88);
206         #ifdef CONFIG_SND_SOC_RK1000
207         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x00);
208         #else
209         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_CODEC, 0x0d);
210         #endif
211         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_I2C, 0x22);
212         rk1000_i2c_send(I2C_ADDR_CTRL, CTRL_TVE, 0x00);
213         DBG("rk1000 probe ok\n");
214         return 0;
215 err:
216         kfree(rk1000);
217         rk1000 = NULL;
218         return ret;
219 }
220
221 static int rk1000_remove(struct i2c_client *client)
222 {
223         return 0;
224 }
225
226 static const struct i2c_device_id rk1000_id[] = {
227         { "rk1000_control", 0 },
228         { }
229 };
230 MODULE_DEVICE_TABLE(i2c, rk1000_id);
231
232 static const struct dev_pm_ops rockchip_rk1000_pm_ops = {
233         .suspend_late = rk1000_control_suspend,
234         .resume_early = rk1000_control_resume,
235 };
236
237 static struct i2c_driver rk1000_driver = {
238         .driver = {
239                 .name = "rk1000_control",
240                 #ifdef CONFIG_PM
241                 .pm     = &rockchip_rk1000_pm_ops,
242                 #endif
243         },
244         .probe = rk1000_probe,
245         .remove = rk1000_remove,
246         .id_table = rk1000_id,
247 };
248
249
250 static int __init rk1000_init(void)
251 {
252         return i2c_add_driver(&rk1000_driver);
253 }
254
255 static void __exit rk1000_exit(void)
256 {
257         i2c_del_driver(&rk1000_driver);
258 }
259
260 fs_initcall_sync(rk1000_init);
261 module_exit(rk1000_exit);
262
263
264 MODULE_DESCRIPTION("RK1000 control driver");
265 MODULE_AUTHOR("Rock-chips, <www.rock-chips.com>");
266 MODULE_LICENSE("GPL");