1 /* drivers/input/sensors/access/kxtik.c
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 <linux/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
35 #define CONFIG_REG (0x00)
36 #define TIM_CTL_REG (0x01)
37 #define ALS_CTL_REG (0x02)
38 #define INT_STATUS_REG (0x03)
39 #define PS_CTL_REG (0x04)
40 #define PS_ALS_DATA_REG (0x05)
41 #define ALS_WINDOWS_REG (0x08)
42
43 //enable bit[ 0-1], in register CONFIG_REG
44 #define ONLY_ALS_EN (0x00)
45 #define ONLY_PROX_EN (0x01)
46 #define ALL_PROX_ALS_EN (0x02)
47 #define ALL_IDLE (0x03)
48
49 #define POWER_MODE_MASK (0x0C)
50 #define POWER_UP_MODE (0x00)
51 #define POWER_DOWN_MODE (0x08)
52 #define POWER_RESET_MODE (0x0C)
53
sensor_power_updown(struct i2c_client * client,int on)54 static int sensor_power_updown(struct i2c_client *client, int on)
55 {
56 int result = 0;
57 char value = 0;
58 int i = 0;
59 for(i=0; i<3; i++)
60 {
61 if(!on)
62 {
63 value = sensor_read_reg(client, CONFIG_REG);
64 value &= ~POWER_MODE_MASK;
65 value |= POWER_DOWN_MODE;
66 result = sensor_write_reg(client, CONFIG_REG, value);
67 if(result)
68 return result;
69 }
70 else
71 {
72 value = sensor_read_reg(client, CONFIG_REG);
73 value &= ~POWER_MODE_MASK;
74 value |= POWER_UP_MODE;
75 result = sensor_write_reg(client, CONFIG_REG, value);
76 if(result)
77 return result;
78 }
79
80 if(!result)
81 break;
82 }
83
84 if(i>1)
85 printk("%s:set %d times",__func__,i);
86
87 return result;
88 }
89
90
91 /****************operate according to sensor chip:start************/
92
sensor_active(struct i2c_client * client,int enable,int rate)93 static int sensor_active(struct i2c_client *client, int enable, int rate)
94 {
95 struct sensor_private_data *sensor =
96 (struct sensor_private_data *) i2c_get_clientdata(client);
97 int result = 0;
98 char value = 0;
99
100 if(enable)
101 sensor_power_updown(client, 1);
102
103 value = sensor_read_reg(client, sensor->ops->ctrl_reg);
104
105 //register setting according to chip datasheet
106 if(enable)
107 {
108 if( (value & 0x03) == ONLY_ALS_EN )
109 {
110 value &= ~0x03;
111 value |= ALL_PROX_ALS_EN;
112 }
113 else if((value & 0x03) == ALL_IDLE )
114 {
115 value &= ~0x03;
116 value |= ONLY_PROX_EN;
117 }
118
119 }
120 else
121 {
122 if( (value & 0x03) == ONLY_PROX_EN )
123 {
124 value &= ~0x03;
125 value |= ALL_IDLE;
126 }
127 else if((value & 0x03) == ALL_PROX_ALS_EN )
128 {
129 value &= ~0x03;
130 value |= ONLY_ALS_EN;
131 }
132 }
133
134 sensor->ops->ctrl_data = value;
135
136 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
137 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
138 if(result)
139 printk("%s:fail to active sensor\n",__func__);
140
141 return result;
142
143 }
144
145
sensor_init(struct i2c_client * client)146 static int sensor_init(struct i2c_client *client)
147 {
148 struct sensor_private_data *sensor =
149 (struct sensor_private_data *) i2c_get_clientdata(client);
150 int result = 0;
151 char value = 0;
152
153 sensor_power_updown(client, 0);
154
155 result = sensor->ops->active(client,0,0);
156 if(result)
157 {
158 printk("%s:line=%d,error\n",__func__,__LINE__);
159 return result;
160 }
161
162 sensor->status_cur = SENSOR_OFF;
163
164 value = 0x41;//The ADC effective resolution = 9; Low lux threshold level = 1;
165 //value = 0x69; //The ADC effective resolution = 17; Low lux threshold level = 9;
166 result = sensor_write_reg(client, ALS_CTL_REG, value);
167 if(result)
168 {
169 printk("%s:line=%d,error\n",__func__,__LINE__);
170 return result;
171 }
172
173 //value = 0x04;//0x01-0x0f; 17%->93.5% if value = 0x04,then Compensate Loss 52%
174 value = 0x02;//0x01-0x0f; 17%->93.5% if value = 0x02,then Compensate Loss 31%
175 result = sensor_write_reg(client, ALS_WINDOWS_REG, value);
176 if(result)
177 {
178 printk("%s:line=%d,error\n",__func__,__LINE__);
179 return result;
180 }
181
182 return result;
183 }
184
sensor_report_value(struct i2c_client * client)185 static int sensor_report_value(struct i2c_client *client)
186 {
187 struct sensor_private_data *sensor =
188 (struct sensor_private_data *) i2c_get_clientdata(client);
189 int result = 0;
190 char value = 0;
191
192 if(sensor->pdata->irq_enable)
193 {
194 if(sensor->ops->int_status_reg)
195 {
196 value = sensor_read_reg(client, sensor->ops->int_status_reg);
197 }
198
199 }
200
201 value = sensor_read_reg(client, sensor->ops->read_reg);
202 input_report_abs(sensor->input_dev, ABS_DISTANCE, (value>>7)?0:1);
203 input_sync(sensor->input_dev);
204 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,(value>>7)?0:1);
205
206 return result;
207 }
208
209 static struct sensor_operate proximity_al3006_ops = {
210 .name = "ps_al3006",
211 .type = SENSOR_TYPE_PROXIMITY,//sensor type and it should be correct
212 .id_i2c = PROXIMITY_ID_AL3006, //i2c id number
213 .read_reg = PS_ALS_DATA_REG, //read data
214 .read_len = 1, //data length
215 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
216 .id_data = SENSOR_UNKNOW_DATA, //device id
217 .precision = 8, //8 bits
218 .ctrl_reg = CONFIG_REG, //enable or disable
219 .int_status_reg = INT_STATUS_REG, //intterupt status register
220 .range = {0,10}, //range
221 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
222 .active = sensor_active,
223 .init = sensor_init,
224 .report = sensor_report_value,
225 };
226
227 /****************operate according to sensor chip:end************/
proximity_al3006_probe(struct i2c_client * client,const struct i2c_device_id * devid)228 static int proximity_al3006_probe(struct i2c_client *client,
229 const struct i2c_device_id *devid)
230 {
231 return sensor_register_device(client, NULL, devid, &proximity_al3006_ops);
232 }
233
proximity_al3006_remove(struct i2c_client * client)234 static int proximity_al3006_remove(struct i2c_client *client)
235 {
236 return sensor_unregister_device(client, NULL, &proximity_al3006_ops);
237 }
238
239 static const struct i2c_device_id proximity_al3006_id[] = {
240 {"proximity_al3006", PROXIMITY_ID_AL3006},
241 {}
242 };
243
244 static struct i2c_driver proximity_al3006_driver = {
245 .probe = proximity_al3006_probe,
246 .remove = proximity_al3006_remove,
247 .shutdown = sensor_shutdown,
248 .id_table = proximity_al3006_id,
249 .driver = {
250 .name = "proximity_al3006",
251 #ifdef CONFIG_PM
252 .pm = &sensor_pm_ops,
253 #endif
254 },
255 };
256
257 module_i2c_driver(proximity_al3006_driver);
258
259 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
260 MODULE_DESCRIPTION("al3006 proximity driver");
261 MODULE_LICENSE("GPL");
262