input: sensors: fromdos and remove trailing whitespace
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / sensor-i2c.c
1 /* drivers/input/sensors/sensor-i2c.c - sensor i2c handle
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: luowei <lw@rock-chips.com>
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <asm/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32 #include <linux/sensor-dev.h>
33
34 #define SENSOR_I2C_RATE 200*1000
35
36
37 static int sensor_i2c_write(struct i2c_adapter *i2c_adap,
38                             unsigned char address,
39                             unsigned int len, unsigned char const *data)
40 {
41         struct i2c_msg msgs[1];
42         int res;
43
44         if (!data || !i2c_adap) {
45                 printk("%s:line=%d,error\n",__func__,__LINE__);
46                 return -EINVAL;
47         }
48
49         msgs[0].addr = address;
50         msgs[0].flags = 0;      /* write */
51         msgs[0].buf = (unsigned char *)data;
52         msgs[0].len = len;
53
54         res = i2c_transfer(i2c_adap, msgs, 1);
55         if (res == 1)
56                 return 0;
57         else if(res == 0)
58                 return -EBUSY;
59         else
60                 return res;
61
62 }
63
64 static int senosr_i2c_read(struct i2c_adapter *i2c_adap,
65                            unsigned char address, unsigned char reg,
66                            unsigned int len, unsigned char *data)
67 {
68         struct i2c_msg msgs[2];
69         int res;
70
71         if (!data || !i2c_adap) {
72                 printk("%s:line=%d,error\n",__func__,__LINE__);
73                 return -EINVAL;
74         }
75
76         msgs[0].addr = address;
77         msgs[0].flags = 0;      /* write */
78         msgs[0].buf = &reg;
79         msgs[0].len = 1;
80
81         msgs[1].addr = address;
82         msgs[1].flags = I2C_M_RD;
83         msgs[1].buf = data;
84         msgs[1].len = len;
85
86         res = i2c_transfer(i2c_adap, msgs, 2);
87         if (res == 2)
88                 return 0;
89         else if(res == 0)
90                 return -EBUSY;
91         else
92                 return res;
93
94 }
95
96
97 int sensor_rx_data(struct i2c_client *client, char *rxData, int length)
98 {
99         //struct sensor_private_data* sensor =
100         //      (struct sensor_private_data *)i2c_get_clientdata(client);
101         int i = 0;
102         int ret = 0;
103         char reg = rxData[0];
104         ret = senosr_i2c_read(client->adapter, client->addr, reg, length, rxData);
105
106         DBG("addr=0x%x,len=%d,rxdata:",reg,length);
107         for(i=0; i<length; i++)
108                 DBG("0x%x,",rxData[i]);
109         DBG("\n");
110         return ret;
111 }
112 EXPORT_SYMBOL(sensor_rx_data);
113
114 int sensor_tx_data(struct i2c_client *client, char *txData, int length)
115 {
116         //struct sensor_private_data* sensor =
117                 //(struct sensor_private_data *)i2c_get_clientdata(client);
118         int i = 0;
119         int ret = 0;
120
121         DBG("addr=0x%x,len=%d,txdata:",txData[0],length);
122         for(i=1; i<length; i++)
123                 DBG("0x%x,",txData[i]);
124         DBG("\n");
125         ret = sensor_i2c_write(client->adapter, client->addr, length, txData);
126         return ret;
127
128 }
129 EXPORT_SYMBOL(sensor_tx_data);
130
131 int sensor_write_reg(struct i2c_client *client, int addr, int value)
132 {
133         char buffer[2];
134         int ret = 0;
135         struct sensor_private_data* sensor =
136                 (struct sensor_private_data *)i2c_get_clientdata(client);
137
138         mutex_lock(&sensor->i2c_mutex);
139         buffer[0] = addr;
140         buffer[1] = value;
141         ret = sensor_tx_data(client, &buffer[0], 2);
142         mutex_unlock(&sensor->i2c_mutex);
143         return ret;
144 }
145 EXPORT_SYMBOL(sensor_write_reg);
146
147 int sensor_read_reg(struct i2c_client *client, int addr)
148 {
149         char tmp[1] = {0};
150         int ret = 0;
151         struct sensor_private_data* sensor =
152                 (struct sensor_private_data *)i2c_get_clientdata(client);
153
154         mutex_lock(&sensor->i2c_mutex);
155         tmp[0] = addr;
156         ret = sensor_rx_data(client, tmp, 1);
157         mutex_unlock(&sensor->i2c_mutex);
158
159         return tmp[0];
160 }
161
162 EXPORT_SYMBOL(sensor_read_reg);
163
164 static int i2c_master_normal_recv(const struct i2c_client *client, char *buf, int count, int scl_rate)
165  {
166      struct i2c_adapter *adap=client->adapter;
167      struct i2c_msg msg;
168     int ret;
169
170     msg.addr = client->addr;
171     msg.flags = client->flags | I2C_M_RD;
172         msg.len = count;
173         msg.buf = (char *)buf;
174         ret = i2c_transfer(adap, &msg, 1);
175
176                  return (ret == 1) ? count : ret;
177 }
178
179 static int i2c_master_normal_send(const struct i2c_client *client, const char *buf, int count, int scl_rate)
180 {
181         int ret;
182         struct i2c_adapter *adap=client->adapter;
183         struct i2c_msg msg;
184
185         msg.addr = client->addr;
186         msg.flags = client->flags;
187         msg.len = count;
188         msg.buf = (char *)buf;
189
190         ret = i2c_transfer(adap, &msg, 1);
191         return (ret == 1) ? count : ret;
192 }
193
194 int sensor_tx_data_normal(struct i2c_client *client, char *buf, int num)
195 {
196         int ret = 0;
197         ret = i2c_master_normal_send(client, buf, num, SENSOR_I2C_RATE);
198
199         return (ret == num) ? 0 : ret;
200 }
201 EXPORT_SYMBOL(sensor_tx_data_normal);
202
203
204 int sensor_rx_data_normal(struct i2c_client *client, char *buf, int num)
205 {
206         int ret = 0;
207         ret = i2c_master_normal_recv(client, buf, num, SENSOR_I2C_RATE);
208
209         return (ret == num) ? 0 : ret;
210 }
211
212 EXPORT_SYMBOL(sensor_rx_data_normal);
213
214
215 int sensor_write_reg_normal(struct i2c_client *client, char value)
216 {
217         char buffer[2];
218         int ret = 0;
219         struct sensor_private_data* sensor =
220                 (struct sensor_private_data *)i2c_get_clientdata(client);
221
222         mutex_lock(&sensor->i2c_mutex);
223         buffer[0] = value;
224         ret = sensor_tx_data_normal(client, &buffer[0], 1);
225         mutex_unlock(&sensor->i2c_mutex);
226         return ret;
227 }
228 EXPORT_SYMBOL(sensor_write_reg_normal);
229
230 int sensor_read_reg_normal(struct i2c_client *client)
231 {
232         char tmp[1] = {0};
233         int ret = 0;
234         struct sensor_private_data* sensor =
235                 (struct sensor_private_data *)i2c_get_clientdata(client);
236
237         mutex_lock(&sensor->i2c_mutex);
238         ret = sensor_rx_data_normal(client, tmp, 1);
239         mutex_unlock(&sensor->i2c_mutex);
240
241         return tmp[0];
242 }
243
244 EXPORT_SYMBOL(sensor_read_reg_normal);
245