1 /* drivers/input/sensors/sensor-i2c.c - sensor i2c handle
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
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.
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.
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>
32 #include <linux/sensor-dev.h>
34 #define SENSOR_I2C_RATE 200*1000
37 static int sensor_i2c_write(struct i2c_adapter *i2c_adap,
38 unsigned char address,
39 unsigned int len, unsigned char const *data)
41 struct i2c_msg msgs[1];
44 if (!data || !i2c_adap) {
45 printk("%s:line=%d,error\n",__func__,__LINE__);
49 msgs[0].addr = address;
50 msgs[0].flags = 0; /* write */
51 msgs[0].buf = (unsigned char *)data;
54 res = i2c_transfer(i2c_adap, msgs, 1);
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)
68 struct i2c_msg msgs[2];
71 if (!data || !i2c_adap) {
72 printk("%s:line=%d,error\n",__func__,__LINE__);
76 msgs[0].addr = address;
77 msgs[0].flags = 0; /* write */
81 msgs[1].addr = address;
82 msgs[1].flags = I2C_M_RD;
86 res = i2c_transfer(i2c_adap, msgs, 2);
97 int sensor_rx_data(struct i2c_client *client, char *rxData, int length)
99 //struct sensor_private_data* sensor =
100 // (struct sensor_private_data *)i2c_get_clientdata(client);
103 char reg = rxData[0];
104 ret = senosr_i2c_read(client->adapter, client->addr, reg, length, rxData);
106 DBG("addr=0x%x,len=%d,rxdata:",reg,length);
107 for(i=0; i<length; i++)
108 DBG("0x%x,",rxData[i]);
112 EXPORT_SYMBOL(sensor_rx_data);
114 int sensor_tx_data(struct i2c_client *client, char *txData, int length)
116 //struct sensor_private_data* sensor =
117 //(struct sensor_private_data *)i2c_get_clientdata(client);
121 DBG("addr=0x%x,len=%d,txdata:",txData[0],length);
122 for(i=1; i<length; i++)
123 DBG("0x%x,",txData[i]);
125 ret = sensor_i2c_write(client->adapter, client->addr, length, txData);
129 EXPORT_SYMBOL(sensor_tx_data);
131 int sensor_write_reg(struct i2c_client *client, int addr, int value)
135 struct sensor_private_data* sensor =
136 (struct sensor_private_data *)i2c_get_clientdata(client);
138 mutex_lock(&sensor->i2c_mutex);
141 ret = sensor_tx_data(client, &buffer[0], 2);
142 mutex_unlock(&sensor->i2c_mutex);
145 EXPORT_SYMBOL(sensor_write_reg);
147 int sensor_read_reg(struct i2c_client *client, int addr)
151 struct sensor_private_data* sensor =
152 (struct sensor_private_data *)i2c_get_clientdata(client);
154 mutex_lock(&sensor->i2c_mutex);
156 ret = sensor_rx_data(client, tmp, 1);
157 mutex_unlock(&sensor->i2c_mutex);
162 EXPORT_SYMBOL(sensor_read_reg);
164 static int i2c_master_normal_recv(const struct i2c_client *client, char *buf, int count, int scl_rate)
166 struct i2c_adapter *adap=client->adapter;
170 msg.addr = client->addr;
171 msg.flags = client->flags | I2C_M_RD;
173 msg.buf = (char *)buf;
174 ret = i2c_transfer(adap, &msg, 1);
176 return (ret == 1) ? count : ret;
179 static int i2c_master_normal_send(const struct i2c_client *client, const char *buf, int count, int scl_rate)
182 struct i2c_adapter *adap=client->adapter;
185 msg.addr = client->addr;
186 msg.flags = client->flags;
188 msg.buf = (char *)buf;
190 ret = i2c_transfer(adap, &msg, 1);
191 return (ret == 1) ? count : ret;
194 int sensor_tx_data_normal(struct i2c_client *client, char *buf, int num)
197 ret = i2c_master_normal_send(client, buf, num, SENSOR_I2C_RATE);
199 return (ret == num) ? 0 : ret;
201 EXPORT_SYMBOL(sensor_tx_data_normal);
204 int sensor_rx_data_normal(struct i2c_client *client, char *buf, int num)
207 ret = i2c_master_normal_recv(client, buf, num, SENSOR_I2C_RATE);
209 return (ret == num) ? 0 : ret;
212 EXPORT_SYMBOL(sensor_rx_data_normal);
215 int sensor_write_reg_normal(struct i2c_client *client, char value)
219 struct sensor_private_data* sensor =
220 (struct sensor_private_data *)i2c_get_clientdata(client);
222 mutex_lock(&sensor->i2c_mutex);
224 ret = sensor_tx_data_normal(client, &buffer[0], 1);
225 mutex_unlock(&sensor->i2c_mutex);
228 EXPORT_SYMBOL(sensor_write_reg_normal);
230 int sensor_read_reg_normal(struct i2c_client *client)
234 struct sensor_private_data* sensor =
235 (struct sensor_private_data *)i2c_get_clientdata(client);
237 mutex_lock(&sensor->i2c_mutex);
238 ret = sensor_rx_data_normal(client, tmp, 1);
239 mutex_unlock(&sensor->i2c_mutex);
244 EXPORT_SYMBOL(sensor_read_reg_normal);