1 /* drivers/input/sensors/access/kxtik.c
\r
3 * Copyright (C) 2012-2015 ROCKCHIP.
\r
4 * Author: luowei <lw@rock-chips.com>
\r
6 * This software is licensed under the terms of the GNU General Public
\r
7 * License version 2, as published by the Free Software Foundation, and
\r
8 * may be copied, distributed, and modified under those terms.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
16 #include <linux/interrupt.h>
\r
17 #include <linux/i2c.h>
\r
18 #include <linux/slab.h>
\r
19 #include <linux/irq.h>
\r
20 #include <linux/miscdevice.h>
\r
21 #include <linux/gpio.h>
\r
22 #include <asm/uaccess.h>
\r
23 #include <asm/atomic.h>
\r
24 #include <linux/delay.h>
\r
25 #include <linux/input.h>
\r
26 #include <linux/workqueue.h>
\r
27 #include <linux/freezer.h>
\r
28 #include <mach/gpio.h>
\r
29 #include <mach/board.h>
\r
30 #ifdef CONFIG_HAS_EARLYSUSPEND
\r
31 #include <linux/earlysuspend.h>
\r
33 #include <linux/sensor-dev.h>
\r
36 #define CONFIG_REG (0x00)
\r
37 #define TIM_CTL_REG (0x01)
\r
38 #define ALS_CTL_REG (0x02)
\r
39 #define INT_STATUS_REG (0x03)
\r
40 #define PS_CTL_REG (0x04)
\r
41 #define PS_ALS_DATA_REG (0x05)
\r
42 #define ALS_WINDOWS_REG (0x08)
\r
44 //enable bit[ 0-1], in register CONFIG_REG
\r
45 #define ONLY_ALS_EN (0x00)
\r
46 #define ONLY_PROX_EN (0x01)
\r
47 #define ALL_PROX_ALS_EN (0x02)
\r
48 #define ALL_IDLE (0x03)
\r
50 #define POWER_MODE_MASK (0x0C)
\r
51 #define POWER_UP_MODE (0x00)
\r
52 #define POWER_DOWN_MODE (0x08)
\r
53 #define POWER_RESET_MODE (0x0C)
\r
55 static int sensor_power_updown(struct i2c_client *client, int on)
\r
64 value = sensor_read_reg(client, CONFIG_REG);
\r
65 value &= ~POWER_MODE_MASK;
\r
66 value |= POWER_DOWN_MODE;
\r
67 result = sensor_write_reg(client, CONFIG_REG, value);
\r
73 value = sensor_read_reg(client, CONFIG_REG);
\r
74 value &= ~POWER_MODE_MASK;
\r
75 value |= POWER_UP_MODE;
\r
76 result = sensor_write_reg(client, CONFIG_REG, value);
\r
86 printk("%s:set %d times",__func__,i);
\r
92 /****************operate according to sensor chip:start************/
\r
94 static int sensor_active(struct i2c_client *client, int enable, int rate)
\r
96 struct sensor_private_data *sensor =
\r
97 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
102 sensor_power_updown(client, 1);
\r
104 value = sensor_read_reg(client, sensor->ops->ctrl_reg);
\r
106 //register setting according to chip datasheet
\r
109 if( (value & 0x03) == ONLY_ALS_EN )
\r
112 value |= ALL_PROX_ALS_EN;
\r
114 else if((value & 0x03) == ALL_IDLE )
\r
117 value |= ONLY_PROX_EN;
\r
123 if( (value & 0x03) == ONLY_PROX_EN )
\r
128 else if((value & 0x03) == ALL_PROX_ALS_EN )
\r
131 value |= ONLY_ALS_EN;
\r
135 sensor->ops->ctrl_data = value;
\r
137 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
\r
138 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
\r
140 printk("%s:fail to active sensor\n",__func__);
\r
147 static int sensor_init(struct i2c_client *client)
\r
149 struct sensor_private_data *sensor =
\r
150 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
154 sensor_power_updown(client, 0);
\r
156 result = sensor->ops->active(client,0,0);
\r
159 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
163 sensor->status_cur = SENSOR_OFF;
\r
165 value = 0x41;//The ADC effective resolution = 9; Low lux threshold level = 1;
\r
166 //value = 0x69; //The ADC effective resolution = 17; Low lux threshold level = 9;
167 result = sensor_write_reg(client, ALS_CTL_REG, value);
\r
170 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
174 //value = 0x04;//0x01-0x0f; 17%->93.5% if value = 0x04,then Compensate Loss 52%
175 value = 0x02;//0x01-0x0f; 17%->93.5% if value = 0x02,then Compensate Loss 31%
\r
176 result = sensor_write_reg(client, ALS_WINDOWS_REG, value);
\r
179 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
186 static int sensor_report_value(struct i2c_client *client)
\r
188 struct sensor_private_data *sensor =
\r
189 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
193 if(sensor->pdata->irq_enable)
\r
195 if(sensor->ops->int_status_reg)
\r
197 value = sensor_read_reg(client, sensor->ops->int_status_reg);
\r
202 value = sensor_read_reg(client, sensor->ops->read_reg);
\r
203 input_report_abs(sensor->input_dev, ABS_DISTANCE, (value>>7)?0:1);
\r
204 input_sync(sensor->input_dev);
\r
205 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,(value>>7)?0:1);
\r
210 struct sensor_operate proximity_al3006_ops = {
\r
211 .name = "ps_al3006",
\r
212 .type = SENSOR_TYPE_PROXIMITY,//sensor type and it should be correct
\r
213 .id_i2c = PROXIMITY_ID_AL3006, //i2c id number
\r
214 .read_reg = PS_ALS_DATA_REG, //read data
\r
215 .read_len = 1, //data length
\r
216 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
\r
217 .id_data = SENSOR_UNKNOW_DATA, //device id
\r
218 .precision = 8, //8 bits
\r
219 .ctrl_reg = CONFIG_REG, //enable or disable
\r
220 .int_status_reg = INT_STATUS_REG, //intterupt status register
\r
221 .range = {0,10}, //range
\r
222 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
\r
223 .active = sensor_active,
\r
224 .init = sensor_init,
\r
225 .report = sensor_report_value,
\r
228 /****************operate according to sensor chip:end************/
\r
230 //function name should not be changed
\r
231 static struct sensor_operate *proximity_get_ops(void)
\r
233 return &proximity_al3006_ops;
\r
237 static int __init proximity_al3006_init(void)
\r
239 struct sensor_operate *ops = proximity_get_ops();
\r
241 int type = ops->type;
\r
242 result = sensor_register_slave(type, NULL, NULL, proximity_get_ops);
\r
246 static void __exit proximity_al3006_exit(void)
\r
248 struct sensor_operate *ops = proximity_get_ops();
\r
249 int type = ops->type;
\r
250 sensor_unregister_slave(type, NULL, NULL, proximity_get_ops);
\r
254 module_init(proximity_al3006_init);
\r
255 module_exit(proximity_al3006_exit);
\r