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 AP3212B_NUM_CACHABLE_REGS 23
37 #define AP3216C_NUM_CACHABLE_REGS 26
\r
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
\r
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};
\r
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);
\r
84 return (val & mask) >> shift;
\r
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);
\r
97 ret = i2c_smbus_write_byte_data(client, reg, tmp);
\r
104 * internally used functions
107 static int ap321xx_set_range(struct i2c_client *client, int range)
\r
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 =
\r
118 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
121 ret = __ap321xx_read_reg(client, sensor->ops->ctrl_reg,
\r
122 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT);
125 static int ap321xx_set_mode(struct i2c_client *client, int mode)
\r
127 struct sensor_private_data *sensor =
\r
128 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
131 ret = __ap321xx_write_reg(client, sensor->ops->ctrl_reg,
\r
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;
\r
141 lsb = i2c_smbus_read_byte_data(client, AL3212_ADC_LSB);
\r
146 msb = i2c_smbus_read_byte_data(client, AL3212_ADC_MSB);
\r
150 val = msb << 8 | lsb;
\r
151 for(index = 0; index < 7 && val > ap321xx_threshole[index];index++)
157 /* ALS low threshold */
\r
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 */
\r
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 =
\r
198 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
201 val = i2c_smbus_read_byte_data(client, sensor->ops->int_status_reg);
\r
202 val &= AP3212B_INT_MASK;
204 return val >> AP3212B_INT_SHIFT;
207 static int ap321xx_product_detect(struct i2c_client *client)
\r
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);
\r
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);
\r
225 //printk("MakeID[%d] ProductID[%d] RevID[%d] .... can't detect ... bad reversion!!!\n", mid, pid, rid);
\r
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 =
\r
272 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
275 value = ap321xx_get_adc_value(client);
276 DBG("ALS lux index: %u\n", value);
\r
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);
\r
287 input_sync(sensor->input_dev);
\r
291 /****************operate according to sensor chip:start************/
\r
293 static int sensor_active(struct i2c_client *client, int enable, int rate)
\r
297 //register setting according to chip datasheet
\r
299 result = ap321xx_lsensor_enable(client);
\r
302 ap321xx_change_ls_threshold(client);
\r
306 result = ap321xx_lsensor_disable(client);
\r
309 printk("%s:fail to active sensor\n",__func__);
\r
316 static int sensor_init(struct i2c_client *client)
\r
318 struct sensor_private_data *sensor =
\r
319 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
322 result = ap321xx_product_detect(client);
\r
325 dev_err(&client->dev, "ret: %d, product version detect failed.\n",result);
\r
329 /* initialize the AP3212B chip */
330 result = ap321xx_init_client(client);
\r
334 result = sensor->ops->active(client,0,0);
\r
337 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
341 sensor->status_cur = SENSOR_OFF;
\r
346 static int sensor_report_value(struct i2c_client *client)
\r
351 int_stat = ap321xx_get_intstat(client);
\r
353 if (int_stat & AP3212B_INT_AMASK)
\r
355 ap321xx_change_ls_threshold(client);
\r
361 struct sensor_operate light_ap321xx_ops = {
\r
362 .name = "ls_ap321xx",
\r
363 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
\r
364 .id_i2c = LIGHT_ID_AP321XX, //i2c id number
\r
365 .read_reg = SENSOR_UNKNOW_DATA, //read data //there are two regs, we fix them in code.
\r
366 .read_len = 1, //data length
\r
367 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register //there are 3 regs, we fix them in code.
\r
368 .id_data = SENSOR_UNKNOW_DATA, //device id
\r
369 .precision = 16, //8 bits
\r
370 .ctrl_reg = AP3212B_MODE_COMMAND, //enable or disable
\r
371 .int_status_reg = AP3212B_INT_COMMAND, //intterupt status register
\r
372 .range = {100,65535}, //range
\r
373 .brightness ={10,255}, // brightness
\r
374 .trig = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
\r
375 .active = sensor_active,
\r
376 .init = sensor_init,
\r
377 .report = sensor_report_value,
\r
380 /****************operate according to sensor chip:end************/
\r
382 //function name should not be changed
\r
383 static struct sensor_operate *light_get_ops(void)
\r
385 return &light_ap321xx_ops;
\r
389 static int __init light_ap321xx_init(void)
\r
391 struct sensor_operate *ops = light_get_ops();
\r
393 int type = ops->type;
\r
394 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
\r
398 static void __exit light_ap321xx_exit(void)
\r
400 struct sensor_operate *ops = light_get_ops();
\r
401 int type = ops->type;
\r
402 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
\r
406 module_init(light_ap321xx_init);
\r
407 module_exit(light_ap321xx_exit);
\r