hc
2024-08-16 62c46c9150c4afde7e5b25436263fddf79d66f0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/uaccess.h>
#include <asm/atomic.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/freezer.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include <linux/sensor-dev.h>
#include <linux/types.h>
 
 
#define DRIVER_VERSION                "1.0"
 
#define PWR_MODE_DOWN_MASK             0x80
#define PWR_MODE_OPERATE_MASK     0x7F
 
 
/*us5152 Slave Addr*/
#define LIGHT_ADDR      0x72
 
/*Interrupt PIN for S3C6410*/
#define IRQ_LIGHT_INT IRQ_EINT(6)
 
/*Register Set*/
#define REGS_CR0              0x00
#define REGS_CR1              0x01
#define REGS_CR2              0x02
#define REGS_CR3              0x03
//ALS
#define REGS_INT_LSB_TH_LO      0x04
#define REGS_INT_MSB_TH_LO      0x05
#define REGS_INT_LSB_TH_HI      0x06
#define REGS_INT_MSB_TH_HI      0x07
//ALS data
#define REGS_LBS_SENSOR         0x0C
#define REGS_MBS_SENSOR         0x0D
 
#define REGS_CR10              0x10
#define REGS_CR11              0x11
#define REGS_VERSION_ID          0x1F
#define REGS_CHIP_ID          0xB2
 
/*ShutDown_EN*/
#define CR0_OPERATION        0x0
#define CR0_SHUTDOWN_EN        0x1
 
#define CR0_SHUTDOWN_SHIFT       (7)
#define CR0_SHUTDOWN_MASK        (0x1 << CR0_SHUTDOWN_SHIFT)
 
/*OneShot_EN*/
#define CR0_ONESHOT_EN        0x01
 
#define CR0_ONESHOT_SHIFT       (6)
#define CR0_ONESHOT_MASK        (0x1 << CR0_ONESHOT_SHIFT)
 
/*Operation Mode*/
#define CR0_OPMODE_ALSANDPS    0x0
#define CR0_OPMODE_ALSONLY    0x1
#define CR0_OPMODE_IRONLY        0x2
 
#define CR0_OPMODE_SHIFT           (4)
#define CR0_OPMODE_MASK            (0x3 << CR0_OPMODE_SHIFT)
 
/*all int flag (PROX, INT_A, INT_P)*/
#define CR0_ALL_INT_CLEAR    0x0
 
#define CR0_ALL_INT_SHIFT       (1)
#define CR0_ALL_INT_MASK        (0x7 << CR0_ALL_INT_SHIFT)
 
 
/*indicator of object proximity detection*/
#define CR0_PROX_CLEAR        0x0
 
#define CR0_PROX_SHIFT           (3)
#define CR0_PROX_MASK            (0x1 << CR0_PROX_SHIFT)
 
/*interrupt status of proximity sensor*/
#define CR0_INTP_CLEAR        0x0
 
#define CR0_INTP_SHIFT           (2)
#define CR0_INTP_MASK            (0x1 << CR0_INTP_SHIFT)
 
/*interrupt status of ambient sensor*/
#define CR0_INTA_CLEAR        0x0
 
#define CR0_INTA_SHIFT           (1)
#define CR0_INTA_MASK            (0x1 << CR0_INTA_SHIFT)
 
/*Word mode enable*/
#define CR0_WORD_EN        0x1
 
#define CR0_WORD_SHIFT           (0)
#define CR0_WORD_MASK            (0x1 << CR0_WORD_SHIFT)
 
 
/*ALS fault queue depth for interrupt enent output*/
#define CR1_ALS_FQ_1        0x0
#define CR1_ALS_FQ_4        0x1
#define CR1_ALS_FQ_8        0x2
#define CR1_ALS_FQ_16        0x3
#define CR1_ALS_FQ_24        0x4
#define CR1_ALS_FQ_32        0x5
#define CR1_ALS_FQ_48        0x6
#define CR1_ALS_FQ_63        0x7
 
#define CR1_ALS_FQ_SHIFT           (5)
#define CR1_ALS_FQ_MASK            (0x7 << CR1_ALS_FQ_SHIFT)
 
/*resolution for ALS*/
#define CR1_ALS_RES_12BIT    0x0
#define CR1_ALS_RES_14BIT    0x1
#define CR1_ALS_RES_16BIT    0x2
#define CR1_ALS_RES_16BIT_2    0x3
 
#define CR1_ALS_RES_SHIFT          (3)
#define CR1_ALS_RES_MASK           (0x3 << CR1_ALS_RES_SHIFT)
 
/*sensing amplifier selection for ALS*/
#define CR1_ALS_GAIN_X1        0x0
#define CR1_ALS_GAIN_X2        0x1
#define CR1_ALS_GAIN_X4        0x2
#define CR1_ALS_GAIN_X8        0x3
#define CR1_ALS_GAIN_X16    0x4
#define CR1_ALS_GAIN_X32    0x5
#define CR1_ALS_GAIN_X64    0x6
#define CR1_ALS_GAIN_X128    0x7
 
#define CR1_ALS_GAIN_SHIFT      (0)
#define CR1_ALS_GAIN_MASK       (0x7 << CR1_ALS_GAIN_SHIFT)
 
 
/*PS fault queue depth for interrupt event output*/
#define CR2_PS_FQ_1        0x0
#define CR2_PS_FQ_4        0x1
#define CR2_PS_FQ_8        0x2
#define CR2_PS_FQ_15        0x3
 
#define CR2_PS_FQ_SHIFT          (6)
#define CR2_PS_FQ_MASK           (0x3 << CR2_PS_FQ_SHIFT)
 
/*interrupt type setting */
/*low active*/
#define CR2_INT_LEVEL        0x0
/*low pulse*/
#define CR2_INT_PULSE        0x1
 
#define CR2_INT_SHIFT          (5)
#define CR2_INT_MASK           (0x1 << CR2_INT_SHIFT)
 
/*resolution for PS*/
#define CR2_PS_RES_12        0x0
#define CR2_PS_RES_14        0x1
#define CR2_PS_RES_16        0x2
#define CR2_PS_RES_16_2        0x3
 
#define CR2_PS_RES_SHIFT          (3)
#define CR2_PS_RES_MASK           (0x3 << CR2_PS_RES_SHIFT)
 
/*sensing amplifier selection for PS*/
#define CR2_PS_GAIN_1        0x0
#define CR2_PS_GAIN_2        0x1
#define CR2_PS_GAIN_4        0x2
#define CR2_PS_GAIN_8        0x3
#define CR2_PS_GAIN_16        0x4
#define CR2_PS_GAIN_32        0x5
#define CR2_PS_GAIN_64        0x6
#define CR2_PS_GAIN_128        0x7
 
#define CR2_PS_GAIN_SHIFT          (0)
#define CR2_PS_GAIN_MASK           (0x7 << CR2_PS_GAIN_SHIFT)
 
/*wait-time slot selection*/
#define CR3_WAIT_SEL_0        0x0
#define CR3_WAIT_SEL_4        0x1
#define CR3_WAIT_SEL_8        0x2
#define CR3_WAIT_SEL_16        0x3
 
#define CR3_WAIT_SEL_SHIFT      (6)
#define CR3_WAIT_SEL_MASK       (0x3 << CR3_WAIT_SEL_SHIFT)
 
/*IR-LED drive peak current setting*/
#define CR3_LEDDR_12_5        0x0
#define CR3_LEDDR_25        0x1
#define CR3_LEDDR_50        0x2
#define CR3_LEDDR_100        0x3
 
#define CR3_LEDDR_SHIFT          (4)
#define CR3_LEDDR_MASK           (0x3 << CR3_LEDDR_SHIFT)
 
/*INT pin source selection*/
#define CR3_INT_SEL_BATH    0x0
#define CR3_INT_SEL_ALS        0x1
#define CR3_INT_SEL_PS        0x2
#define CR3_INT_SEL_PSAPP    0x3
 
#define CR3_INT_SEL_SHIFT          (2)
#define CR3_INT_SEL_MASK           (0x3 << CR3_INT_SEL_SHIFT)
 
/*software reset for register and core*/
#define CR3_SOFTRST_EN        0x1
 
#define CR3_SOFTRST_SHIFT          (0)
#define CR3_SOFTRST_MASK           (0x1 << CR3_SOFTRST_SHIFT)
 
/*modulation frequency of LED driver*/
#define CR10_FREQ_DIV2        0x0
#define CR10_FREQ_DIV4        0x1
#define CR10_FREQ_DIV8        0x2
#define CR10_FREQ_DIV16        0x3
 
#define CR10_FREQ_SHIFT          (1)
#define CR10_FREQ_MASK           (0x3 << CR10_FREQ_SHIFT)
 
/*50/60 Rejection enable*/
#define CR10_REJ_5060_DIS    0x00
#define CR10_REJ_5060_EN    0x01
 
#define CR10_REJ_5060_SHIFT     (0)
#define CR10_REJ_5060_MASK      (0x1 << CR10_REJ_5060_SHIFT)
 
#define us5152_NUM_CACHABLE_REGS 0x12
 
 
static int sensor_active(struct i2c_client *client, int enable, int rate)
{
   //struct sensor_private_data *sensor =
      // (struct sensor_private_data *) i2c_get_clientdata(client);
   int result = 0;
   char value = 0;
   int i = 0;
 
   for(i=0; i<3; i++)
       {
           if(!enable)
           {
               value = sensor_read_reg(client, REGS_CR0);
               value |= PWR_MODE_DOWN_MASK;    //ShutDown_EN=1
               result = sensor_write_reg(client, REGS_CR0, value);
               if(result)
                   return result;
           }
           else
           {
               value = sensor_read_reg(client, REGS_CR0);
               value &= PWR_MODE_OPERATE_MASK ; //Operation_EN=0
               result = sensor_write_reg(client, REGS_CR0, value);
               if(result)
                   return result;
           }
 
           if(!result)
           break;
       }
 
       if(i>1)
       printk("%s:set %d times",__func__,i);
 
 
   //TODO:? function to be added here
 
   return result;
 
}
 
 
static int sensor_init(struct i2c_client *client)
{
   struct sensor_private_data *sensor =
       (struct sensor_private_data *) i2c_get_clientdata(client);
   int result = 0;
   char value = 0;
 
   result = sensor->ops->active(client,0,0);
   if(result)
   {
       printk("%s:line=%d,error\n",__func__,__LINE__);
       return result;
   }
 
   sensor->status_cur = SENSOR_OFF;
 
   value = sensor_read_reg(client, REGS_CHIP_ID); //read chip ids
   printk("us5152 chip id is %x!\n", value);
 
   value = 0x01;//word accessing
 
   result = sensor_write_reg(client, REGS_CR0, value);
   if(result)
   {
       printk("%s:line=%d,error\n",__func__,__LINE__);
       return result;
   }
 
   return result;
}
 
 
static int us5152_value_report(struct input_dev *input, int data)
{
   unsigned char index = 0;
   if(data <= 10){
       index = 0;goto report;
   }
   else if(data <= 160){
       index = 1;goto report;
   }
   else if(data <= 225){
       index = 2;goto report;
   }
   else if(data <= 320){
       index = 3;goto report;
   }
   else if(data <= 640){
       index = 4;goto report;
   }
   else if(data <= 1280){
       index = 5;goto report;
   }
   else if(data <= 2600){
       index = 6;goto report;
   }
   else{
       index = 7;goto report;
   }
 
report:
   input_report_abs(input, ABS_MISC, index);
   input_sync(input);
   return index;
}
 
static int sensor_report_value(struct i2c_client *client)
{
   struct sensor_private_data *sensor =
       (struct sensor_private_data *) i2c_get_clientdata(client);
   int result = 0;
   int value = 0;
   char index = 0;
   char buffer[2]= { 0 } ;
   int ret=0;
 
   if(sensor->pdata->irq_enable)
   {
       if(sensor->ops->int_status_reg >= 0)
       {
           value = sensor_read_reg(client, sensor->ops->int_status_reg);
       }
 
   }
 
   //value = sensor_read_reg(client, sensor->ops->read_reg);  //TODO:? to be changed
   if(sensor->ops->read_len< 2) //12bit
   {
       printk("us5152 data read para num error ; len = %d\n ",sensor->ops->read_len);
       return -1;
   }
   memset(buffer , 0 , 2);
   do
   {
       *buffer = sensor->ops->read_reg;
       ret=sensor_rx_data(client,buffer,sensor->ops->read_len);
       if(ret<0)
           return ret;
   }
   while(0);
   value=buffer[1];
   value =((value << 8) | buffer[0]) & 0xffff;
   index = us5152_value_report(sensor->input_dev, value);  //now is 12bit
 
   //printk("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);
   DBG("%s:%s result=%d,index=%d buffer[1]=0x%x , buffer[0]=0x%x \n",__func__,sensor->ops->name, value,index,buffer[1],buffer[0]);
 
   return result;
}
 
 
static struct sensor_operate light_us5152_ops = {
   .name                = "ls_us5152",
   .type                = SENSOR_TYPE_LIGHT,    //sensor type and it should be correct
   .id_i2c                = LIGHT_ID_US5152,    //i2c id number
   .read_reg            = REGS_LBS_SENSOR,    //read data
   .read_len            = 2,            //data length
   .id_reg                = REGS_CHIP_ID,        //read device id from this register
   .id_data             = 0x26,            //device id
   .precision            = 12,            //12 bits
   .ctrl_reg             = REGS_CR0,        //enable or disable
   .int_status_reg         = SENSOR_UNKNOW_DATA,    //intterupt status register
   .range                = {0,10},        //range
   .brightness                     = {10,4095},                          // brightness
   .trig                = IRQF_TRIGGER_LOW | IRQF_ONESHOT ,
   .active                = sensor_active,
   .init                = sensor_init,
   .report                = sensor_report_value,
};
/****************operate according to sensor chip:end************/
static int light_us5152_probe(struct i2c_client *client,
                 const struct i2c_device_id *devid)
{
   return sensor_register_device(client, NULL, devid, &light_us5152_ops);
}
 
static int light_us5152_remove(struct i2c_client *client)
{
   return sensor_unregister_device(client, NULL, &light_us5152_ops);
}
 
static const struct i2c_device_id light_us5152_id[] = {
   {"ls_us5152", LIGHT_ID_US5152},
   {}
};
 
static struct i2c_driver light_us5152_driver = {
   .probe = light_us5152_probe,
   .remove = light_us5152_remove,
   .shutdown = sensor_shutdown,
   .id_table = light_us5152_id,
   .driver = {
       .name = "light_us5152",
   #ifdef CONFIG_PM
       .pm = &sensor_pm_ops,
   #endif
   },
};
 
 
module_i2c_driver(light_us5152_driver);
 
MODULE_AUTHOR("Finley Huang finley_huang@upi-semi.com");
MODULE_DESCRIPTION("us5152 ambient light sensor driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);