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 <linux/of_gpio.h>
\r
29 #ifdef CONFIG_HAS_EARLYSUSPEND
\r
30 #include <linux/earlysuspend.h>
\r
32 #include <linux/sensor-dev.h>
\r
35 #define CM3232_CLOSE 0x01
\r
38 #define CM3232_ADDR_COM 0
\r
39 #define CM3232_ADDR_DATA 50
\r
41 #define CM3232_DRV_NAME "cm3232"
\r
43 #define COMMAND_CTRL 0
\r
44 #define COMMAND_ALS_DATA 50 //ALS: 15:8 MSB 8bits data
\r
45 //7:0 LSB 8bits data
\r
48 #define ALS_RESET(x) (((x)&1)<<6) //0 = Reset disable; 1 = Reset enable
\r
49 #define ALS_IT(x) (((x)&7)<<2) //ALS integration time setting
\r
50 #define HIGH_SENSITIVITY(x) (((x)&1)<<1) //0 = Normal mode; 1 = High sensitivity mode
\r
51 #define SHUT_DOWN(x) (((x)&1)<<0) //ALS shut down setting: 0 = ALS Power on ; 1 = ALS Shut down
\r
53 #define ALS_IT100MS 0 //100ms
\r
54 #define ALS_IT200MS 1 //200ms
\r
55 #define ALS_IT400MS 2 //400ms
\r
56 #define ALS_IT800MS 3 //800ms
\r
57 #define ALS_IT1600MS 4 //1600ms
\r
58 #define ALS_IT3200MS 5 //3200ms
\r
61 /****************operate according to sensor chip:start************/
\r
63 static int sensor_active(struct i2c_client *client, int enable, int rate)
\r
65 struct sensor_private_data *sensor =
\r
66 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
70 //sensor->client->addr = sensor->ops->ctrl_reg;
\r
71 //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
\r
72 //printk("%s: client addr = %#x\n\n", __func__, client->addr);
\r
73 //register setting according to chip datasheet
\r
75 sensor->ops->ctrl_data = ALS_RESET(1);
\r
76 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
\r
78 printk("%s:fail to active sensor\n",__func__);
\r
85 sensor->ops->ctrl_data = ALS_IT(ALS_IT200MS) | HIGH_SENSITIVITY(1);
\r
89 sensor->ops->ctrl_data = SHUT_DOWN(1);
\r
92 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
\r
94 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
\r
96 printk("%s:fail to active sensor\n",__func__);
\r
103 static int sensor_init(struct i2c_client *client)
\r
105 struct sensor_private_data *sensor =
\r
106 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
109 result = sensor->ops->active(client,0,0);
\r
112 printk("%s:line=%d,error\n",__func__,__LINE__);
\r
116 sensor->status_cur = SENSOR_OFF;
\r
121 static int light_report_value(struct input_dev *input, int data)
\r
123 unsigned char index = 0;
\r
126 index = 0;goto report;
\r
128 else if(data <= 160){
\r
129 index = 1;goto report;
\r
131 else if(data <= 225){
\r
132 index = 2;goto report;
\r
134 else if(data <= 320){
\r
135 index = 3;goto report;
\r
137 else if(data <= 640){
\r
138 index = 4;goto report;
\r
140 else if(data <= 1280){
\r
141 index = 5;goto report;
\r
143 else if(data <= 2600){
\r
144 index = 6;goto report;
\r
147 index = 7;goto report;
\r
151 input_report_abs(input, ABS_MISC, index);
\r
158 static int sensor_report_value(struct i2c_client *client)
\r
160 struct sensor_private_data *sensor =
\r
161 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
163 //char msb = 0, lsb = 0;
\r
164 char data[2] = {0};
\r
165 unsigned short value = 0;
\r
168 //sensor->client->addr = CM3232_ADDR_DATA;
\r
169 data[0] = CM3232_ADDR_DATA;
\r
170 sensor_rx_data(sensor->client, data, 2);
\r
171 value = (data[1] << 8) | data[0] ;
\r
173 DBG("%s:result=%d\n",__func__,value);
\r
174 //printk("%s:result=%d\n",__func__,value);
\r
175 index = light_report_value(sensor->input_dev, value);
\r
176 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);
\r
178 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
\r
181 result= sensor_read_reg(client, sensor->ops->int_status_reg);
\r
184 printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);
\r
192 struct sensor_operate light_cm3232_ops = {
\r
194 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
\r
195 .id_i2c = LIGHT_ID_CM3232, //i2c id number
\r
196 .read_reg = CM3232_ADDR_DATA, //read data
\r
197 .read_len = 2, //data length
\r
198 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
\r
199 .id_data = SENSOR_UNKNOW_DATA, //device id
\r
200 .precision = 8, //8 bits
\r
201 .ctrl_reg = CM3232_ADDR_COM, //enable or disable
\r
202 .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register
\r
203 .range = {100,65535}, //range
\r
204 .brightness = {10,255}, // brightness
\r
205 .trig = SENSOR_UNKNOW_DATA,
\r
206 .active = sensor_active,
\r
207 .init = sensor_init,
\r
208 .report = sensor_report_value,
\r
211 /****************operate according to sensor chip:end************/
\r
213 //function name should not be changed
\r
214 static struct sensor_operate *light_get_ops(void)
\r
216 return &light_cm3232_ops;
\r
220 static int __init light_cm3232_init(void)
\r
222 struct sensor_operate *ops = light_get_ops();
\r
224 int type = ops->type;
\r
225 result = sensor_register_slave(type, NULL, NULL, light_get_ops);
\r
229 static void __exit light_cm3232_exit(void)
\r
231 struct sensor_operate *ops = light_get_ops();
\r
232 int type = ops->type;
\r
233 sensor_unregister_slave(type, NULL, NULL, light_get_ops);
\r
237 module_init(light_cm3232_init);
\r
238 module_exit(light_cm3232_exit);
\r