rk: reset drivers/net/wireless drivers/video/display/display-sysfs.c sound/soc/codecs...
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / wm831x-i2c.c
1 /*
2  * wm831x-i2c.c  --  I2C access for Wolfson WM831x PMICs
3  *
4  * Copyright 2009,2010 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/mfd/core.h>
20 #include <linux/slab.h>
21 #include <linux/gpio.h>
22
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/pdata.h>
25
26 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
27                                   int bytes, void *dest)
28 {
29 #if defined(CONFIG_ARCH_RK30)
30         const struct i2c_client *client = wm831x->control_data;
31         struct i2c_adapter *adap=client->adapter;
32         struct i2c_msg msgs[2];
33         int ret;
34         char reg_buf[2];
35         const short regs = reg;
36         int scl_rate= 100 * 1000;
37         short *buf = dest;
38         int count = bytes/2;
39
40         reg_buf[0] = (regs & 0xff00) >> 8;
41         reg_buf[1] = regs & 0x00ff;
42
43         msgs[0].addr = client->addr;
44         msgs[0].flags = client->flags;
45         msgs[0].len = 2;
46         msgs[0].buf = reg_buf;
47         msgs[0].scl_rate = scl_rate;
48
49         msgs[1].addr = client->addr;
50         msgs[1].flags = client->flags | I2C_M_RD;
51         msgs[1].len = count * 2;
52         msgs[1].buf = (char *)buf;
53         msgs[1].scl_rate = scl_rate;
54
55         ret = i2c_transfer(adap, msgs, 2);
56
57         return (ret == 2)? 0 : ret;
58 #else
59         struct i2c_client *i2c = wm831x->control_data;
60         int ret;
61         u16 r = cpu_to_be16(reg);
62
63         ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
64         if (ret < 0)
65                 return ret;
66         if (ret != 2)
67                 return -EIO;
68
69         ret = i2c_master_recv(i2c, dest, bytes);
70         if (ret < 0)
71                 return ret;
72         if (ret != bytes)
73                 return -EIO;
74         return 0;
75 #endif
76 }
77
78 /* Currently we allocate the write buffer on the stack; this is OK for
79  * small writes - if we need to do large writes this will need to be
80  * revised.
81  */
82 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
83                                    int bytes, void *src)
84 {
85 #if defined(CONFIG_ARCH_RK30)
86         const struct i2c_client *client = wm831x->control_data;
87         struct i2c_adapter *adap=client->adapter;
88         struct i2c_msg msg;
89         int ret;
90         const short regs = reg;
91         const short *buf = src;
92         int count = bytes/2;
93         int scl_rate = 100 * 1000;
94         int i;
95         
96         char *tx_buf = (char *)kmalloc(2 * (count + 1), GFP_KERNEL);
97         if(!tx_buf)
98                 return -ENOMEM;
99         tx_buf[0] = (regs & 0xff00) >> 8;
100         tx_buf[1] = regs & 0x00ff;
101         for(i = 0; i < count; i++){
102                 tx_buf[i*2+3] = (buf[i] & 0xff00) >> 8;
103                 tx_buf[i*2+2] = buf[i] & 0x00ff;
104         }
105
106         msg.addr = client->addr;
107         msg.flags = client->flags;
108         msg.len = 2 * (count + 1);
109         msg.buf = (char *)tx_buf;
110         msg.scl_rate = scl_rate;
111
112         ret = i2c_transfer(adap, &msg, 1);
113         kfree(tx_buf);
114         return (ret == 1) ? 0 : ret;
115 #else
116         struct i2c_client *i2c = wm831x->control_data;
117         unsigned char msg[bytes + 2];
118         int ret;
119
120         reg = cpu_to_be16(reg);
121         memcpy(&msg[0], &reg, 2);
122         memcpy(&msg[2], src, bytes);
123
124         ret = i2c_master_send(i2c, msg, bytes + 2);
125         if (ret < 0)
126                 return ret;
127         if (ret < bytes + 2)
128                 return -EIO;
129
130         return 0;
131 #endif 
132 }
133
134 static int wm831x_i2c_probe(struct i2c_client *i2c,
135                             const struct i2c_device_id *id)
136 {
137         struct wm831x *wm831x;
138         int ret,gpio,irq;
139
140         wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
141         if (wm831x == NULL)
142                 return -ENOMEM;
143
144         i2c_set_clientdata(i2c, wm831x);
145         
146         gpio = i2c->irq;
147         ret = gpio_request(gpio, "wm831x");
148         if (ret) {
149                 printk( "failed to request rk gpio irq for wm831x \n");
150                 return ret;
151         }
152         gpio_pull_updown(gpio, GPIOPullUp);
153         if (ret) {
154             printk("failed to pull up gpio irq for wm831x \n");
155                 return ret;
156         }       
157         irq = gpio_to_irq(gpio);
158         
159         wm831x->dev = &i2c->dev;
160         wm831x->control_data = i2c;
161         wm831x->read_dev = wm831x_i2c_read_device;
162         wm831x->write_dev = wm831x_i2c_write_device;
163
164         return wm831x_device_init(wm831x, id->driver_data, irq);
165 }
166
167 static int wm831x_i2c_remove(struct i2c_client *i2c)
168 {
169         struct wm831x *wm831x = i2c_get_clientdata(i2c);
170
171         wm831x_device_exit(wm831x);
172
173         return 0;
174 }
175
176 static int wm831x_i2c_suspend(struct device *dev)
177 {
178         struct wm831x *wm831x = dev_get_drvdata(dev);
179
180         spin_lock(&wm831x->flag_lock);
181         wm831x->flag_suspend = 1;
182         spin_unlock(&wm831x->flag_lock);
183
184         return wm831x_device_suspend(wm831x);
185 }
186
187 static int wm831x_i2c_resume(struct device *dev)
188 {
189         struct wm831x *wm831x = dev_get_drvdata(dev);
190         int i;
191         //set some intterupt again while resume 
192         for (i = 0; i < ARRAY_SIZE(wm831x->irq_masks_cur); i++) {
193                 //printk("irq_masks_cur[%d]=0x%x\n",i,wm831x->irq_masks_cur[i]);
194
195                 if (wm831x->irq_masks_cur[i] != wm831x->irq_masks_cache[i]) {
196                         wm831x->irq_masks_cache[i] = wm831x->irq_masks_cur[i];
197                         wm831x_reg_write(wm831x,
198                                          WM831X_INTERRUPT_STATUS_1_MASK + i,
199                                          wm831x->irq_masks_cur[i]);
200                 }
201         
202         }
203
204         return 0;
205 }
206
207 void wm831x_i2c_shutdown(struct i2c_client *i2c)
208 {
209         struct wm831x *wm831x = i2c_get_clientdata(i2c);
210 //      printk("%s\n", __FUNCTION__);
211 //      wm831x_device_shutdown(wm831x);
212 }
213
214 static const struct i2c_device_id wm831x_i2c_id[] = {
215         { "wm8310", WM8310 },
216         { "wm8311", WM8311 },
217         { "wm8312", WM8312 },
218         { "wm8320", WM8320 },
219         { "wm8321", WM8321 },
220         { "wm8325", WM8325 },
221         { "wm8326", WM8326 },
222         { }
223 };
224 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
225
226 static const struct dev_pm_ops wm831x_pm_ops = {
227         .suspend = wm831x_i2c_suspend,
228         .resume = wm831x_i2c_resume,
229 };
230
231 static struct i2c_driver wm831x_i2c_driver = {
232         .driver = {
233                 .name = "wm831x",
234                 .owner = THIS_MODULE,
235                 .pm = &wm831x_pm_ops,
236         },
237         .probe = wm831x_i2c_probe,
238         .remove = wm831x_i2c_remove,
239         .shutdown = wm831x_i2c_shutdown,
240         .id_table = wm831x_i2c_id,
241 };
242
243 static int __init wm831x_i2c_init(void)
244 {
245         int ret;
246
247         printk("%s\n", __FUNCTION__);
248         ret = i2c_add_driver(&wm831x_i2c_driver);
249         if (ret != 0)
250                 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
251
252         return ret;
253 }
254 subsys_initcall_sync(wm831x_i2c_init);
255
256 static void __exit wm831x_i2c_exit(void)
257 {
258         i2c_del_driver(&wm831x_i2c_driver);
259 }
260 module_exit(wm831x_i2c_exit);