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 CM3232_CLOSE 0x01
36
37
38 #define CM3232_ADDR_COM 0
39 #define CM3232_ADDR_DATA 50
40
41 #define CM3232_DRV_NAME "cm3232"
42 //command code
43 #define COMMAND_CTRL 0
44 #define COMMAND_ALS_DATA 50 //ALS: 15:8 MSB 8bits data
45 //7:0 LSB 8bits data
46
47 //ctrl bit
48 #define ALS_RESET(x) (((x)&1)<<6) //0 = Reset disable; 1 = Reset enable
49 #define ALS_IT(x) (((x)&7)<<2) //ALS integration time setting
50 #define HIGH_SENSITIVITY(x) (((x)&1)<<1) //0 = Normal mode; 1 = High sensitivity mode
51 #define SHUT_DOWN(x) (((x)&1)<<0) //ALS shut down setting: 0 = ALS Power on ; 1 = ALS Shut down
52
53 #define ALS_IT100MS 0 //100ms
54 #define ALS_IT200MS 1 //200ms
55 #define ALS_IT400MS 2 //400ms
56 #define ALS_IT800MS 3 //800ms
57 #define ALS_IT1600MS 4 //1600ms
58 #define ALS_IT3200MS 5 //3200ms
59
60
61 /****************operate according to sensor chip:start************/
62
sensor_active(struct i2c_client * client,int enable,int rate)63 static int sensor_active(struct i2c_client *client, int enable, int rate)
64 {
65 struct sensor_private_data *sensor =
66 (struct sensor_private_data *) i2c_get_clientdata(client);
67 int result = 0;
68 //int status = 0;
69
70 //sensor->client->addr = sensor->ops->ctrl_reg;
71 //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
72 //printk("%s: client addr = %#x\n\n", __func__, client->addr);
73 //register setting according to chip datasheet
74 if (enable) {
75 sensor->ops->ctrl_data = ALS_RESET(1);
76 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
77 if(result) {
78 printk("%s:fail to active sensor\n",__func__);
79 return -1;
80 }
81 }
82
83 if(enable)
84 {
85 sensor->ops->ctrl_data = ALS_IT(ALS_IT200MS) | HIGH_SENSITIVITY(1);
86 }
87 else
88 {
89 sensor->ops->ctrl_data = SHUT_DOWN(1);
90 }
91
92 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
93
94 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
95 if(result)
96 printk("%s:fail to active sensor\n",__func__);
97
98 return result;
99
100 }
101
102
sensor_init(struct i2c_client * client)103 static int sensor_init(struct i2c_client *client)
104 {
105 struct sensor_private_data *sensor =
106 (struct sensor_private_data *) i2c_get_clientdata(client);
107 int result = 0;
108
109 result = sensor->ops->active(client,0,0);
110 if(result)
111 {
112 printk("%s:line=%d,error\n",__func__,__LINE__);
113 return result;
114 }
115
116 sensor->status_cur = SENSOR_OFF;
117 return result;
118 }
119
120
light_report_value(struct input_dev * input,int data)121 static int light_report_value(struct input_dev *input, int data)
122 {
123 unsigned char index = 0;
124
125 if(data <= 10){
126 index = 0;goto report;
127 }
128 else if(data <= 160){
129 index = 1;goto report;
130 }
131 else if(data <= 225){
132 index = 2;goto report;
133 }
134 else if(data <= 320){
135 index = 3;goto report;
136 }
137 else if(data <= 640){
138 index = 4;goto report;
139 }
140 else if(data <= 1280){
141 index = 5;goto report;
142 }
143 else if(data <= 2600){
144 index = 6;goto report;
145 }
146 else{
147 index = 7;goto report;
148 }
149
150 report:
151 input_report_abs(input, ABS_MISC, index);
152 input_sync(input);
153
154 return index;
155 }
156
157
sensor_report_value(struct i2c_client * client)158 static int sensor_report_value(struct i2c_client *client)
159 {
160 struct sensor_private_data *sensor =
161 (struct sensor_private_data *) i2c_get_clientdata(client);
162 int result = 0;
163 //char msb = 0, lsb = 0;
164 char data[2] = {0};
165 unsigned short value = 0;
166 int index = 0;
167
168 //sensor->client->addr = CM3232_ADDR_DATA;
169 data[0] = CM3232_ADDR_DATA;
170 sensor_rx_data(sensor->client, data, 2);
171 value = (data[1] << 8) | data[0] ;
172
173 DBG("%s:result=%d\n",__func__,value);
174 //printk("%s:result=%d\n",__func__,value);
175 index = light_report_value(sensor->input_dev, value);
176 DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);
177
178 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
179 {
180
181 result= sensor_read_reg(client, sensor->ops->int_status_reg);
182 if(result)
183 {
184 printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);
185 }
186 }
187
188 return result;
189 }
190
191
192 static struct sensor_operate light_cm3232_ops = {
193 .name = "cm3232",
194 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
195 .id_i2c = LIGHT_ID_CM3232, //i2c id number
196 .read_reg = CM3232_ADDR_DATA, //read data
197 .read_len = 2, //data length
198 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
199 .id_data = SENSOR_UNKNOW_DATA, //device id
200 .precision = 8, //8 bits
201 .ctrl_reg = CM3232_ADDR_COM, //enable or disable
202 .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register
203 .range = {100,65535}, //range
204 .brightness = {10,255}, // brightness
205 .trig = SENSOR_UNKNOW_DATA,
206 .active = sensor_active,
207 .init = sensor_init,
208 .report = sensor_report_value,
209 };
210
211 /****************operate according to sensor chip:end************/
light_cm3232_probe(struct i2c_client * client,const struct i2c_device_id * devid)212 static int light_cm3232_probe(struct i2c_client *client,
213 const struct i2c_device_id *devid)
214 {
215 return sensor_register_device(client, NULL, devid, &light_cm3232_ops);
216 }
217
light_cm3232_remove(struct i2c_client * client)218 static int light_cm3232_remove(struct i2c_client *client)
219 {
220 return sensor_unregister_device(client, NULL, &light_cm3232_ops);
221 }
222
223 static const struct i2c_device_id light_cm3232_id[] = {
224 {"light_cm3232", LIGHT_ID_CM3232},
225 {}
226 };
227
228 static struct i2c_driver light_cm3232_driver = {
229 .probe = light_cm3232_probe,
230 .remove = light_cm3232_remove,
231 .shutdown = sensor_shutdown,
232 .id_table = light_cm3232_id,
233 .driver = {
234 .name = "light_cm3232",
235 #ifdef CONFIG_PM
236 .pm = &sensor_pm_ops,
237 #endif
238 },
239 };
240
241 module_i2c_driver(light_cm3232_driver);
242
243 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
244 MODULE_DESCRIPTION("cm3232 light driver");
245 MODULE_LICENSE("GPL");
246