1 /* drivers/input/sensors/access/kxtik.c
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 <mach/gpio.h>
29 #include <mach/board.h>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
33 #include <linux/sensor-dev.h>
36 #define AP3212B_NUM_CACHABLE_REGS 23
37 #define AP3216C_NUM_CACHABLE_REGS 26
39 #define AP3212B_RAN_COMMAND 0x10
40 #define AP3212B_RAN_MASK 0x30
41 #define AP3212B_RAN_SHIFT (4)
43 #define AP3212B_MODE_COMMAND 0x00
44 #define AP3212B_MODE_SHIFT (0)
45 #define AP3212B_MODE_MASK 0x07
47 #define AP3212B_INT_COMMAND 0x01
48 #define AP3212B_INT_SHIFT (0)
49 #define AP3212B_INT_MASK 0x03
50 #define AP3212B_INT_PMASK 0x02
51 #define AP3212B_INT_AMASK 0x01
53 #define AL3212_ADC_LSB 0x0c
54 #define AL3212_ADC_MSB 0x0d
56 #define AP3212B_ALS_LTHL 0x1a
57 #define AP3212B_ALS_LTHL_SHIFT (0)
58 #define AP3212B_ALS_LTHL_MASK 0xff
60 #define AP3212B_ALS_LTHH 0x1b
61 #define AP3212B_ALS_LTHH_SHIFT (0)
62 #define AP3212B_ALS_LTHH_MASK 0xff
64 #define AP3212B_ALS_HTHL 0x1c
65 #define AP3212B_ALS_HTHL_SHIFT (0)
66 #define AP3212B_ALS_HTHL_MASK 0xff
68 #define AP3212B_ALS_HTHH 0x1d
69 #define AP3212B_ALS_HTHH_SHIFT (0)
70 #define AP3212B_ALS_HTHH_MASK 0xff
72 static u16 ap321xx_threshole[8] = {28,444,625,888,1778,3555,7222,0xffff};
75 * register access helpers
78 static int __ap321xx_read_reg(struct i2c_client *client,
79 u32 reg, u8 mask, u8 shift)
83 val = i2c_smbus_read_byte_data(client, reg);
84 return (val & mask) >> shift;
87 static int __ap321xx_write_reg(struct i2c_client *client,
88 u32 reg, u8 mask, u8 shift, u8 val)
93 tmp = i2c_smbus_read_byte_data(client, reg);
97 ret = i2c_smbus_write_byte_data(client, reg, tmp);
104 * internally used functions
107 static int ap321xx_set_range(struct i2c_client *client, int range)
109 return __ap321xx_write_reg(client, AP3212B_RAN_COMMAND,
110 AP3212B_RAN_MASK, AP3212B_RAN_SHIFT, range);;
115 static int ap321xx_get_mode(struct i2c_client *client)
117 struct sensor_private_data *sensor =
118 (struct sensor_private_data *) i2c_get_clientdata(client);
121 ret = __ap321xx_read_reg(client, sensor->ops->ctrl_reg,
122 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT);
125 static int ap321xx_set_mode(struct i2c_client *client, int mode)
127 struct sensor_private_data *sensor =
128 (struct sensor_private_data *) i2c_get_clientdata(client);
131 ret = __ap321xx_write_reg(client, sensor->ops->ctrl_reg,
132 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT, mode);
136 static int ap321xx_get_adc_value(struct i2c_client *client)
138 unsigned int lsb, msb, val;
139 unsigned char index=0;
141 lsb = i2c_smbus_read_byte_data(client, AL3212_ADC_LSB);
146 msb = i2c_smbus_read_byte_data(client, AL3212_ADC_MSB);
150 val = msb << 8 | lsb;
151 for(index = 0; index < 7 && val > ap321xx_threshole[index];index++)
157 /* ALS low threshold */
158 static int ap321xx_set_althres(struct i2c_client *client, int val)
163 lsb = val & AP3212B_ALS_LTHL_MASK;
165 err = __ap321xx_write_reg(client, AP3212B_ALS_LTHL,
166 AP3212B_ALS_LTHL_MASK, AP3212B_ALS_LTHL_SHIFT, lsb);
170 err = __ap321xx_write_reg(client, AP3212B_ALS_LTHH,
171 AP3212B_ALS_LTHH_MASK, AP3212B_ALS_LTHH_SHIFT, msb);
176 /* ALS high threshold */
177 static int ap321xx_set_ahthres(struct i2c_client *client, int val)
182 lsb = val & AP3212B_ALS_HTHL_MASK;
184 err = __ap321xx_write_reg(client, AP3212B_ALS_HTHL,
185 AP3212B_ALS_HTHL_MASK, AP3212B_ALS_HTHL_SHIFT, lsb);
189 err = __ap321xx_write_reg(client, AP3212B_ALS_HTHH,
190 AP3212B_ALS_HTHH_MASK, AP3212B_ALS_HTHH_SHIFT, msb);
195 static int ap321xx_get_intstat(struct i2c_client *client)
197 struct sensor_private_data *sensor =
198 (struct sensor_private_data *) i2c_get_clientdata(client);
201 val = i2c_smbus_read_byte_data(client, sensor->ops->int_status_reg);
202 val &= AP3212B_INT_MASK;
204 return val >> AP3212B_INT_SHIFT;
207 static int ap321xx_product_detect(struct i2c_client *client)
209 int mid = i2c_smbus_read_byte_data(client, 0x03);
210 int pid = i2c_smbus_read_byte_data(client, 0x04);
211 int rid = i2c_smbus_read_byte_data(client, 0x05);
213 if ( mid == 0x01 && pid == 0x01 &&
214 (rid == 0x03 || rid == 0x04) )
216 //printk("RevID [%d], ==> DA3212 v1.5~1.8 ...... AP3212B detected\n", rid);
218 else if ( (mid == 0x01 && pid == 0x02 && rid == 0x00) ||
219 (mid == 0x02 && pid == 0x02 && rid == 0x01))
221 //printk("RevID [%d], ==> DA3212 v2.0 ...... AP3212C/AP3216C detected\n", rid);
225 //printk("MakeID[%d] ProductID[%d] RevID[%d] .... can't detect ... bad reversion!!!\n", mid, pid, rid);
232 static int ap321xx_init_client(struct i2c_client *client)
235 ap321xx_set_range(client, 0);
236 ap321xx_set_mode(client, 0);
241 static int ap321xx_lsensor_enable(struct i2c_client *client)
245 mode = ap321xx_get_mode(client);
246 if((mode & 0x01) == 0){
248 ret = ap321xx_set_mode(client,mode);
254 static int ap321xx_lsensor_disable(struct i2c_client *client)
258 mode = ap321xx_get_mode(client);
263 ret = ap321xx_set_mode(client,mode);
269 static void ap321xx_change_ls_threshold(struct i2c_client *client)
271 struct sensor_private_data *sensor =
272 (struct sensor_private_data *) i2c_get_clientdata(client);
275 value = ap321xx_get_adc_value(client);
276 DBG("ALS lux index: %u\n", value);
278 ap321xx_set_althres(client,ap321xx_threshole[value-1]);
279 ap321xx_set_ahthres(client,ap321xx_threshole[value]);
282 ap321xx_set_althres(client,0);
283 ap321xx_set_ahthres(client,ap321xx_threshole[value]);
286 input_report_abs(sensor->input_dev, ABS_MISC, value);
287 input_sync(sensor->input_dev);
291 /****************operate according to sensor chip:start************/
293 static int sensor_active(struct i2c_client *client, int enable, int rate)
297 //register setting according to chip datasheet
299 result = ap321xx_lsensor_enable(client);
302 ap321xx_change_ls_threshold(client);
306 result = ap321xx_lsensor_disable(client);
309 printk("%s:fail to active sensor\n",__func__);
316 static int sensor_init(struct i2c_client *client)
318 struct sensor_private_data *sensor =
319 (struct sensor_private_data *) i2c_get_clientdata(client);
322 result = ap321xx_product_detect(client);
325 dev_err(&client->dev, "ret: %d, product version detect failed.\n",result);
329 /* initialize the AP3212B chip */
330 result = ap321xx_init_client(client);
334 result = sensor->ops->active(client,0,0);
337 printk("%s:line=%d,error\n",__func__,__LINE__);
341 sensor->status_cur = SENSOR_OFF;
346 static int sensor_report_value(struct i2c_client *client)
351 int_stat = ap321xx_get_intstat(client);
353 if (int_stat & AP3212B_INT_AMASK)
355 ap321xx_change_ls_threshold(client);
361 struct sensor_operate light_ap321xx_ops = {
362 .name = "ls_ap321xx",
363 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
364 .id_i2c = LIGHT_ID_AP321XX, //i2c id number
365 .read_reg = SENSOR_UNKNOW_DATA, //read data //there are two regs, we fix them in code.
366 .read_len = 1, //data length
367 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register //there are 3 regs, we fix them in code.
368 .id_data = SENSOR_UNKNOW_DATA, //device id
369 .precision = 16, //8 bits
370 .ctrl_reg = AP3212B_MODE_COMMAND, //enable or disable
371 .int_status_reg = AP3212B_INT_COMMAND, //intterupt status register
372 .range = {100,65535}, //range
373 .brightness ={10,255}, // brightness
374 .trig = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
375 .active = sensor_active,
377 .report = sensor_report_value,
380 /****************operate according to sensor chip:end************/
382 //function name should not be changed
383 static struct sensor_operate *light_get_ops(void)
385 return &light_ap321xx_ops;
389 static int __init light_ap321xx_init(void)
391 struct sensor_operate *ops = light_get_ops();
393 int type = ops->type;
394 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
398 static void __exit light_ap321xx_exit(void)
400 struct sensor_operate *ops = light_get_ops();
401 int type = ops->type;
402 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
406 module_init(light_ap321xx_init);
407 module_exit(light_ap321xx_exit);