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 AL3212_ADC_LSB 0x0c
52 #define AL3212_ADC_MSB 0x0d
53
54 #define AP3212B_ALS_LTHL 0x1a
55 #define AP3212B_ALS_LTHL_SHIFT (0)
56 #define AP3212B_ALS_LTHL_MASK 0xff
57
58 #define AP3212B_ALS_LTHH 0x1b
59 #define AP3212B_ALS_LTHH_SHIFT (0)
60 #define AP3212B_ALS_LTHH_MASK 0xff
61
62 #define AP3212B_ALS_HTHL 0x1c
63 #define AP3212B_ALS_HTHL_SHIFT (0)
64 #define AP3212B_ALS_HTHL_MASK 0xff
65
66 #define AP3212B_ALS_HTHH 0x1d
67 #define AP3212B_ALS_HTHH_SHIFT (0)
68 #define AP3212B_ALS_HTHH_MASK 0xff
69
70 static u16 ap321xx_threshole[8] = {28,444,625,888,1778,3555,7222,0xffff};
71
72 /*
73 * register access helpers
74 */
75
__ap321xx_read_reg(struct i2c_client * client,u32 reg,u8 mask,u8 shift)76 static int __ap321xx_read_reg(struct i2c_client *client,
77 u32 reg, u8 mask, u8 shift)
78 {
79 u8 val;
80
81 val = i2c_smbus_read_byte_data(client, reg);
82 return (val & mask) >> shift;
83 }
84
__ap321xx_write_reg(struct i2c_client * client,u32 reg,u8 mask,u8 shift,u8 val)85 static int __ap321xx_write_reg(struct i2c_client *client,
86 u32 reg, u8 mask, u8 shift, u8 val)
87 {
88 int ret = 0;
89 u8 tmp;
90
91 tmp = i2c_smbus_read_byte_data(client, reg);
92 tmp &= ~mask;
93 tmp |= val << shift;
94
95 ret = i2c_smbus_write_byte_data(client, reg, tmp);
96
97 return ret;
98 }
99
100
101 /*
102 * internally used functions
103 */
104 /* range */
ap321xx_set_range(struct i2c_client * client,int range)105 static int ap321xx_set_range(struct i2c_client *client, int range)
106 {
107 return __ap321xx_write_reg(client, AP3212B_RAN_COMMAND,
108 AP3212B_RAN_MASK, AP3212B_RAN_SHIFT, range);;
109 }
110
111
112 /* mode */
ap321xx_get_mode(struct i2c_client * client)113 static int ap321xx_get_mode(struct i2c_client *client)
114 {
115 struct sensor_private_data *sensor =
116 (struct sensor_private_data *) i2c_get_clientdata(client);
117 int ret;
118
119 ret = __ap321xx_read_reg(client, sensor->ops->ctrl_reg,
120 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT);
121 return ret;
122 }
ap321xx_set_mode(struct i2c_client * client,int mode)123 static int ap321xx_set_mode(struct i2c_client *client, int mode)
124 {
125 struct sensor_private_data *sensor =
126 (struct sensor_private_data *) i2c_get_clientdata(client);
127 int ret;
128
129 ret = __ap321xx_write_reg(client, sensor->ops->ctrl_reg,
130 AP3212B_MODE_MASK, AP3212B_MODE_SHIFT, mode);
131 return ret;
132 }
133
ap321xx_get_adc_value(struct i2c_client * client)134 static int ap321xx_get_adc_value(struct i2c_client *client)
135 {
136 unsigned int lsb, msb, val;
137 unsigned char index=0;
138
139 lsb = i2c_smbus_read_byte_data(client, AL3212_ADC_LSB);
140 if (lsb < 0) {
141 return lsb;
142 }
143
144 msb = i2c_smbus_read_byte_data(client, AL3212_ADC_MSB);
145 if (msb < 0)
146 return msb;
147
148 val = msb << 8 | lsb;
149 for(index = 0; index < 7 && val > ap321xx_threshole[index];index++)
150 ;
151
152 return index;
153 }
154
155 /* ALS low threshold */
ap321xx_set_althres(struct i2c_client * client,int val)156 static int ap321xx_set_althres(struct i2c_client *client, int val)
157 {
158 int lsb, msb, err;
159
160 msb = val >> 8;
161 lsb = val & AP3212B_ALS_LTHL_MASK;
162
163 err = __ap321xx_write_reg(client, AP3212B_ALS_LTHL,
164 AP3212B_ALS_LTHL_MASK, AP3212B_ALS_LTHL_SHIFT, lsb);
165 if (err)
166 return err;
167
168 err = __ap321xx_write_reg(client, AP3212B_ALS_LTHH,
169 AP3212B_ALS_LTHH_MASK, AP3212B_ALS_LTHH_SHIFT, msb);
170
171 return err;
172 }
173
174 /* ALS high threshold */
ap321xx_set_ahthres(struct i2c_client * client,int val)175 static int ap321xx_set_ahthres(struct i2c_client *client, int val)
176 {
177 int lsb, msb, err;
178
179 msb = val >> 8;
180 lsb = val & AP3212B_ALS_HTHL_MASK;
181
182 err = __ap321xx_write_reg(client, AP3212B_ALS_HTHL,
183 AP3212B_ALS_HTHL_MASK, AP3212B_ALS_HTHL_SHIFT, lsb);
184 if (err)
185 return err;
186
187 err = __ap321xx_write_reg(client, AP3212B_ALS_HTHH,
188 AP3212B_ALS_HTHH_MASK, AP3212B_ALS_HTHH_SHIFT, msb);
189
190 return err;
191 }
192
ap321xx_get_intstat(struct i2c_client * client)193 static int ap321xx_get_intstat(struct i2c_client *client)
194 {
195 struct sensor_private_data *sensor =
196 (struct sensor_private_data *) i2c_get_clientdata(client);
197 int val;
198
199 val = i2c_smbus_read_byte_data(client, sensor->ops->int_status_reg);
200 val &= AP3212B_INT_MASK;
201
202 return val >> AP3212B_INT_SHIFT;
203 }
204
ap321xx_product_detect(struct i2c_client * client)205 static int ap321xx_product_detect(struct i2c_client *client)
206 {
207 int mid = i2c_smbus_read_byte_data(client, 0x03);
208 int pid = i2c_smbus_read_byte_data(client, 0x04);
209 int rid = i2c_smbus_read_byte_data(client, 0x05);
210
211 if ( mid == 0x01 && pid == 0x01 &&
212 (rid == 0x03 || rid == 0x04) )
213 {
214 //printk("RevID [%d], ==> DA3212 v1.5~1.8 ...... AP3212B detected\n", rid);
215 }
216 else if ( (mid == 0x01 && pid == 0x02 && rid == 0x00) ||
217 (mid == 0x02 && pid == 0x02 && rid == 0x01))
218 {
219 //printk("RevID [%d], ==> DA3212 v2.0 ...... AP3212C/AP3216C detected\n", rid);
220 }
221 else
222 {
223 //printk("MakeID[%d] ProductID[%d] RevID[%d] .... can't detect ... bad reversion!!!\n", mid, pid, rid);
224 return -EIO;
225 }
226
227 return 0;
228 }
229
ap321xx_init_client(struct i2c_client * client)230 static int ap321xx_init_client(struct i2c_client *client)
231 {
232 /* set defaults */
233 ap321xx_set_range(client, 0);
234 ap321xx_set_mode(client, 0);
235
236 return 0;
237 }
238
ap321xx_lsensor_enable(struct i2c_client * client)239 static int ap321xx_lsensor_enable(struct i2c_client *client)
240 {
241 int ret = 0,mode;
242
243 mode = ap321xx_get_mode(client);
244 if((mode & 0x01) == 0){
245 mode |= 0x01;
246 ret = ap321xx_set_mode(client,mode);
247 }
248
249 return ret;
250 }
251
ap321xx_lsensor_disable(struct i2c_client * client)252 static int ap321xx_lsensor_disable(struct i2c_client *client)
253 {
254 int ret = 0,mode;
255
256 mode = ap321xx_get_mode(client);
257 if(mode & 0x01){
258 mode &= ~0x01;
259 if(mode == 0x04)
260 mode = 0;
261 ret = ap321xx_set_mode(client,mode);
262 }
263
264 return ret;
265 }
266
ap321xx_change_ls_threshold(struct i2c_client * client)267 static void ap321xx_change_ls_threshold(struct i2c_client *client)
268 {
269 struct sensor_private_data *sensor =
270 (struct sensor_private_data *) i2c_get_clientdata(client);
271 int value;
272
273 value = ap321xx_get_adc_value(client);
274 DBG("ALS lux index: %u\n", value);
275 if(value > 0){
276 ap321xx_set_althres(client,ap321xx_threshole[value-1]);
277 ap321xx_set_ahthres(client,ap321xx_threshole[value]);
278 }
279 else{
280 ap321xx_set_althres(client,0);
281 ap321xx_set_ahthres(client,ap321xx_threshole[value]);
282 }
283
284 input_report_abs(sensor->input_dev, ABS_MISC, value);
285 input_sync(sensor->input_dev);
286 }
287
288
289 /****************operate according to sensor chip:start************/
290
sensor_active(struct i2c_client * client,int enable,int rate)291 static int sensor_active(struct i2c_client *client, int enable, int rate)
292 {
293 int result = 0;
294
295 //register setting according to chip datasheet
296 if (enable){
297 result = ap321xx_lsensor_enable(client);
298 if(!result){
299 msleep(200);
300 ap321xx_change_ls_threshold(client);
301 }
302 }
303 else
304 result = ap321xx_lsensor_disable(client);
305
306 if(result)
307 printk("%s:fail to active sensor\n",__func__);
308
309 return result;
310
311 }
312
313
sensor_init(struct i2c_client * client)314 static int sensor_init(struct i2c_client *client)
315 {
316 struct sensor_private_data *sensor =
317 (struct sensor_private_data *) i2c_get_clientdata(client);
318 int result = 0;
319
320 result = ap321xx_product_detect(client);
321 if (result)
322 {
323 dev_err(&client->dev, "ret: %d, product version detect failed.\n",result);
324 return result;
325 }
326
327 /* initialize the AP3212B chip */
328 result = ap321xx_init_client(client);
329 if (result)
330 return result;
331
332 result = sensor->ops->active(client,0,0);
333 if(result)
334 {
335 printk("%s:line=%d,error\n",__func__,__LINE__);
336 return result;
337 }
338
339 sensor->status_cur = SENSOR_OFF;
340
341 return result;
342 }
343
sensor_report_value(struct i2c_client * client)344 static int sensor_report_value(struct i2c_client *client)
345 {
346 int result = 0;
347 u8 int_stat;
348
349 int_stat = ap321xx_get_intstat(client);
350 // ALS int
351 if (int_stat & AP3212B_INT_AMASK)
352 {
353 ap321xx_change_ls_threshold(client);
354 }
355
356 return result;
357 }
358
359 static struct sensor_operate light_ap321xx_ops = {
360 .name = "ls_ap321xx",
361 .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct
362 .id_i2c = LIGHT_ID_AP321XX, //i2c id number
363 .read_reg = SENSOR_UNKNOW_DATA, //read data //there are two regs, we fix them in code.
364 .read_len = 1, //data length
365 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register //there are 3 regs, we fix them in code.
366 .id_data = SENSOR_UNKNOW_DATA, //device id
367 .precision = 16, //8 bits
368 .ctrl_reg = AP3212B_MODE_COMMAND, //enable or disable
369 .int_status_reg = AP3212B_INT_COMMAND, //intterupt status register
370 .range = {100,65535}, //range
371 .brightness ={10,255}, // brightness
372 .trig = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
373 .active = sensor_active,
374 .init = sensor_init,
375 .report = sensor_report_value,
376 };
377
378 /****************operate according to sensor chip:end************/
379
light_ap321xx_probe(struct i2c_client * client,const struct i2c_device_id * devid)380 static int light_ap321xx_probe(struct i2c_client *client,
381 const struct i2c_device_id *devid)
382 {
383 return sensor_register_device(client, NULL, devid, &light_ap321xx_ops);
384 }
385
light_ap321xx_remove(struct i2c_client * client)386 static int light_ap321xx_remove(struct i2c_client *client)
387 {
388 return sensor_unregister_device(client, NULL, &light_ap321xx_ops);
389 }
390
391 static const struct i2c_device_id light_ap321xx_id[] = {
392 {"ls_ap321xx", LIGHT_ID_AP321XX},
393 {}
394 };
395
396 static struct i2c_driver light_ap321xx_driver = {
397 .probe = light_ap321xx_probe,
398 .remove = light_ap321xx_remove,
399 .shutdown = sensor_shutdown,
400 .id_table = light_ap321xx_id,
401 .driver = {
402 .name = "light_ap321xx",
403 #ifdef CONFIG_PM
404 .pm = &sensor_pm_ops,
405 #endif
406 },
407 };
408
409 module_i2c_driver(light_ap321xx_driver);
410
411 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
412 MODULE_DESCRIPTION("ap321xx light driver");
413 MODULE_LICENSE("GPL");
414
415
416