xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/psensor/ps_ap321xx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #ifdef CONFIG_HAS_EARLYSUSPEND
29 #include <linux/earlysuspend.h>
30 #endif
31 #include <linux/sensor-dev.h>
32 
33 
34 #define AP3212B_NUM_CACHABLE_REGS	23
35 #define AP3216C_NUM_CACHABLE_REGS	26
36 
37 #define AP3212B_RAN_COMMAND	0x10
38 #define AP3212B_RAN_MASK		0x30
39 #define AP3212B_RAN_SHIFT	(4)
40 
41 #define AP3212B_MODE_COMMAND	0x00
42 #define AP3212B_MODE_SHIFT	(0)
43 #define AP3212B_MODE_MASK	0x07
44 
45 #define AP3212B_INT_COMMAND	0x01
46 #define AP3212B_INT_SHIFT	(0)
47 #define AP3212B_INT_MASK		0x03
48 #define AP3212B_INT_PMASK		0x02
49 #define AP3212B_INT_AMASK		0x01
50 
51 #define AP3212B_OBJ_COMMAND	0x0f
52 #define AP3212B_OBJ_MASK		0x80
53 #define AP3212B_OBJ_SHIFT	(7)
54 
55 
56 /*
57  * register access helpers
58  */
59 
__ap321xx_read_reg(struct i2c_client * client,u32 reg,u8 mask,u8 shift)60 static int __ap321xx_read_reg(struct i2c_client *client,
61 			       u32 reg, u8 mask, u8 shift)
62 {
63 	u8 val;
64 
65 	val = i2c_smbus_read_byte_data(client, reg);
66 	return (val & mask) >> shift;
67 }
68 
__ap321xx_write_reg(struct i2c_client * client,u32 reg,u8 mask,u8 shift,u8 val)69 static int __ap321xx_write_reg(struct i2c_client *client,
70 				u32 reg, u8 mask, u8 shift, u8 val)
71 {
72 	int ret = 0;
73 	u8 tmp;
74 
75 	tmp = i2c_smbus_read_byte_data(client, reg);
76 	tmp &= ~mask;
77 	tmp |= val << shift;
78 
79 	ret = i2c_smbus_write_byte_data(client, reg, tmp);
80 
81 	return ret;
82 }
83 
84 
85 /*
86  * internally used functions
87  */
88 /* range */
ap321xx_set_range(struct i2c_client * client,int range)89 static int ap321xx_set_range(struct i2c_client *client, int range)
90 {
91 	return __ap321xx_write_reg(client, AP3212B_RAN_COMMAND,
92 		AP3212B_RAN_MASK, AP3212B_RAN_SHIFT, range);;
93 }
94 
95 
96 /* mode */
ap321xx_get_mode(struct i2c_client * client)97 static int ap321xx_get_mode(struct i2c_client *client)
98 {
99 	struct sensor_private_data *sensor =
100 	    (struct sensor_private_data *) i2c_get_clientdata(client);
101 	int ret;
102 
103 	ret = __ap321xx_read_reg(client, sensor->ops->ctrl_reg,
104 			AP3212B_MODE_MASK, AP3212B_MODE_SHIFT);
105 	return ret;
106 }
ap321xx_set_mode(struct i2c_client * client,int mode)107 static int ap321xx_set_mode(struct i2c_client *client, int mode)
108 {
109 	struct sensor_private_data *sensor =
110 	    (struct sensor_private_data *) i2c_get_clientdata(client);
111 	int ret;
112 
113 	ret = __ap321xx_write_reg(client, sensor->ops->ctrl_reg,
114 				AP3212B_MODE_MASK, AP3212B_MODE_SHIFT, mode);
115 	return ret;
116 }
ap321xx_get_intstat(struct i2c_client * client)117 static int ap321xx_get_intstat(struct i2c_client *client)
118 {
119 	struct sensor_private_data *sensor =
120 	    (struct sensor_private_data *) i2c_get_clientdata(client);
121 	int val;
122 
123 	val = i2c_smbus_read_byte_data(client, sensor->ops->int_status_reg);
124 	val &= AP3212B_INT_MASK;
125 
126 	return val >> AP3212B_INT_SHIFT;
127 }
128 
ap321xx_get_object(struct i2c_client * client)129 static int ap321xx_get_object(struct i2c_client *client)
130 {
131 	int val;
132 
133 	val = i2c_smbus_read_byte_data(client, AP3212B_OBJ_COMMAND);
134 	val &= AP3212B_OBJ_MASK;
135 
136 	return val >> AP3212B_OBJ_SHIFT;
137 }
138 
139 
ap321xx_product_detect(struct i2c_client * client)140 static int ap321xx_product_detect(struct i2c_client *client)
141 {
142 	int mid = i2c_smbus_read_byte_data(client, 0x03);
143 	int pid = i2c_smbus_read_byte_data(client, 0x04);
144 	int rid = i2c_smbus_read_byte_data(client, 0x05);
145 
146 	if ( mid == 0x01 && pid == 0x01 &&
147 	    (rid == 0x03 || rid == 0x04) )
148 	{
149 		//printk("RevID [%d], ==> DA3212 v1.5~1.8 ...... AP3212B detected\n", rid);
150 	}
151 	else if ( (mid == 0x01 && pid == 0x02 && rid == 0x00) ||
152 		      (mid == 0x02 && pid == 0x02 && rid == 0x01))
153 	{
154 		//printk("RevID [%d], ==> DA3212 v2.0 ...... AP3212C/AP3216C detected\n", rid);
155 	}
156 	else
157 	{
158 		printk("MakeID[%d] ProductID[%d] RevID[%d] .... can't detect ... bad reversion!!!\n", mid, pid, rid);
159 		return -EIO;
160 	}
161 
162 	return 0;
163 }
164 
ap321xx_init_client(struct i2c_client * client)165 static int ap321xx_init_client(struct i2c_client *client)
166 {
167 	/* set defaults */
168 	ap321xx_set_range(client, 0);
169 	ap321xx_set_mode(client, 0);
170 
171 	return 0;
172 }
173 
ap321xx_psensor_enable(struct i2c_client * client)174 static int ap321xx_psensor_enable(struct i2c_client *client)
175 {
176 	int ret = 0,mode;
177 
178 	mode = ap321xx_get_mode(client);
179 	if((mode & 0x02) == 0){
180 		mode |= 0x02;
181 		ret = ap321xx_set_mode(client,mode);
182 	}
183 
184 	return ret;
185 }
186 
ap321xx_psensor_disable(struct i2c_client * client)187 static int ap321xx_psensor_disable(struct i2c_client *client)
188 {
189 	int ret = 0,mode;
190 
191 	mode = ap321xx_get_mode(client);
192 	if(mode & 0x02){
193 		mode &= ~0x02;
194 		if(mode == 0x04)
195 			mode = 0x00;
196 		ret = ap321xx_set_mode(client,mode);
197 	}
198 	return ret;
199 }
200 
201 /****************operate according to sensor chip:start************/
202 
sensor_active(struct i2c_client * client,int enable,int rate)203 static int sensor_active(struct i2c_client *client, int enable, int rate)
204 {
205 	int result = 0;
206 
207 	//register setting according to chip datasheet
208 	if (enable){
209 		result = ap321xx_psensor_enable(client);
210 	}
211 	else
212 		result = ap321xx_psensor_disable(client);
213 
214 	if(result)
215 		printk("%s:fail to active sensor\n",__func__);
216 
217 	return result;
218 
219 }
220 
221 
sensor_init(struct i2c_client * client)222 static int sensor_init(struct i2c_client *client)
223 {
224 	struct sensor_private_data *sensor =
225 	    (struct sensor_private_data *) i2c_get_clientdata(client);
226 	int result = 0;
227 
228 	result = ap321xx_product_detect(client);
229 	if (result)
230 	{
231 		dev_err(&client->dev, "ret: %d, product version detect failed.\n",result);
232 		return result;
233 	}
234 
235 	/* initialize the AP3212B chip */
236 	result = ap321xx_init_client(client);
237 	if (result)
238 		return result;
239 
240 	result = sensor->ops->active(client,0,0);
241 	if(result)
242 	{
243 		printk("%s:line=%d,error\n",__func__,__LINE__);
244 		return result;
245 	}
246 
247 	sensor->status_cur = SENSOR_OFF;
248 
249 	return result;
250 }
251 
sensor_report_value(struct i2c_client * client)252 static int sensor_report_value(struct i2c_client *client)
253 {
254 	struct sensor_private_data *sensor =
255 	    (struct sensor_private_data *) i2c_get_clientdata(client);
256 	int result = 0;
257 	char value = 0;
258 	u8 int_stat;
259 
260 	int_stat = ap321xx_get_intstat(client);
261 	// ALS int
262 	if (int_stat & AP3212B_INT_PMASK)
263 	{
264 		value = ap321xx_get_object(client);
265 		input_report_abs(sensor->input_dev, ABS_DISTANCE, value);
266 		input_sync(sensor->input_dev);
267 	}
268 
269 	return result;
270 }
271 
272 static struct sensor_operate proximity_ap321xx_ops = {
273 	.name				= "ps_ap321xx",
274 	.type				= SENSOR_TYPE_PROXIMITY,	//sensor type and it should be correct
275 	.id_i2c				= PROXIMITY_ID_AP321XX,	//i2c id number
276 	.read_reg			= SENSOR_UNKNOW_DATA,	//read data		//there are two regs, we fix them in code.
277 	.read_len			= 1,			//data length
278 	.id_reg				= SENSOR_UNKNOW_DATA,	//read device id from this register   //there are 3 regs, we fix them in code.
279 	.id_data 			= SENSOR_UNKNOW_DATA,	//device id
280 	.precision			= 8,			//8 bits
281 	.ctrl_reg 			= AP3212B_MODE_COMMAND,		//enable or disable
282 	.int_status_reg 		= AP3212B_INT_COMMAND,	//intterupt status register
283 	.range				= {0,10},		//range
284 	.brightness                                        ={10,255},                          // brightness
285 	.trig				= IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
286 	.active				= sensor_active,
287 	.init				= sensor_init,
288 	.report				= sensor_report_value,
289 };
290 
291 /****************operate according to sensor chip:end************/
proximity_ap321xx_probe(struct i2c_client * client,const struct i2c_device_id * devid)292 static int proximity_ap321xx_probe(struct i2c_client *client,
293 				   const struct i2c_device_id *devid)
294 {
295 	return sensor_register_device(client, NULL, devid, &proximity_ap321xx_ops);
296 }
297 
proximity_ap321xx_remove(struct i2c_client * client)298 static int proximity_ap321xx_remove(struct i2c_client *client)
299 {
300 	return sensor_unregister_device(client, NULL, &proximity_ap321xx_ops);
301 }
302 
303 static const struct i2c_device_id proximity_ap321xx_id[] = {
304 	{"ps_ap321xx", PROXIMITY_ID_AP321XX},
305 	{}
306 };
307 
308 static struct i2c_driver proximity_ap321xx_driver = {
309 	.probe = proximity_ap321xx_probe,
310 	.remove = proximity_ap321xx_remove,
311 	.shutdown = sensor_shutdown,
312 	.id_table = proximity_ap321xx_id,
313 	.driver = {
314 		.name = "proximity_ap321xx",
315 	#ifdef CONFIG_PM
316 		.pm = &sensor_pm_ops,
317 	#endif
318 	},
319 };
320 
321 module_i2c_driver(proximity_ap321xx_driver);
322 
323 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
324 MODULE_DESCRIPTION("ps_ap321xx proximity driver");
325 MODULE_LICENSE("GPL");
326