summaryrefstree
path: root/kernel/drivers/optics/vd6281.h
blob: 4b2dea81bccdb14f6e39f2c477a27a8b3e6ac156 (plain)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*! \file
* \brief Device driver for monitoring ambient light intensity in (lux)
* proximity detection (prox), and Beam functionality within the
* AMS TMX49xx family of devices.
*/

#ifndef __STALS_VD6281_H
#define __STALS_VD6281_H

#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <linux/leds.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>
#include <linux/spinlock.h>
#include <linux/gpio.h>
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/file.h>
#include <linux/syscalls.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pm_qos.h>
#include <linux/ktime.h>
#include <linux/hrtimer.h>
#include <linux/iio/consumer.h>
#include <linux/iio/iio.h>
#ifdef CONFIG_OF
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#endif

#define HEADER_VERSION		"1"

/*! Constants  */
//!\{
#define STALS_ALS_MAX_CHANNELS 6              //!< Number of channels of the STALS
//!\}

/** 
 * @enum STALS_Channel_Id_t
 * 
 * Constants listing the channels of the device.
 */
enum STALS_Channel_Id_t {
    STALS_CHANNEL_1             = 0x01,        //!< channel 1
    STALS_CHANNEL_2             = 0x02,        //!< channel 2
    STALS_CHANNEL_3             = 0x04,        //!< channel 3
    STALS_CHANNEL_4             = 0x08,        //!< channel 4
    STALS_CHANNEL_5             = 0x10,        //!< channel 5
    STALS_CHANNEL_6             = 0x20,        //!< channel 6
};


/** 
 * @enum STALS_Color_Id_t
 * 
 * Constants listing the color light. this is used by the \ref STALS_GetChannelColor function to get what are color filers set on the channels
 */
enum STALS_Color_Id_t {
    STALS_COLOR_IR              = 0x01,        //!< Color IR
    STALS_COLOR_RED             = 0x02,        //!< Color RED
    STALS_COLOR_GREEN           = 0x03,        //!< Color GREEN
    STALS_COLOR_BLUE            = 0x04,        //!< Color BLUE
    STALS_COLOR_UV              = 0x05,        //!< Color UV
    STALS_COLOR_CLEAR           = 0x06,        //!< white light
};

/** 
 * @enum STALS_ErrCode_t
 * 
 * This enumeration is aimed at defining the different errors that can be returned by the STALS driver
 */
typedef enum  {
    STALS_NO_ERROR = 0,                        //!< No error 
    STALS_ERROR_INVALID_PARAMS,                //!< Provided parameters are invalid
    STALS_ERROR_INIT,                          //!< Error in the initialization of the VD621 device
    STALS_ERROR_TIME_OUT,                      //!< A time out has expired before an operation was completed 
    STALS_ERROR_INVALID_DEVICE_ID,             //!< The Provided device identifier is invalid
    STALS_ERROR_WRITE,                         //!< The trial to write on the I2C bus has failed
    STALS_ERROR_READ,                          //!< The trial to read from the I2C bus has failed
    STALS_ERROR_ALREADY_STARTED,               //!< The device is already started 
    STALS_ERROR_NOT_STARTED,                   //!< The device is not started 
    STALS_ERROR_NOT_SUPPORTED,                 //!< The called function is not supported, likely because not yet implemented
    STALS_ERROR_FNCT_DEPRECATED,               //!< The called function is deprecated
    STALS_ERROR_LAST_ERROR_CODE,
} STALS_ErrCode_t;



/** 
 * @enum STALS_Control_t 
 * 
 * This enumeration is aimed at defining the Enable and disable flags
 */
enum STALS_Control_t
{                                      
    STALS_CONTROL_DISABLE = 0,                  //!< Disable the feature
    STALS_CONTROL_ENABLE = 1,                   //!< Enable the feature
};

/** 
 * @enum STALS_Control_Id_t
 * 
 * This enumeration is aimed at defining the different parameters identifiers 
 */
enum STALS_Control_Id_t {
    /*!
     * Control to be used to enable or disable the pedestal\n
     * To enable the pedestal : \ref STALS_SetControl(pHandle, STALS_PEDESTAL_ENABLE, STALS_CONTROL_ENABLE);\n
     * To disable the pedestal : \ref STALS_SetControl(pHandle, STALS_PEDESTAL_ENABLE, STALS_CONTROL_DISABLE);\n
     * To know is the pedestal is enabled : \n
     * enum  STALS_Control_t Flag; \ref STALS_GetControl(pHandle, STALS_PEDESTAL_ENABLE, &Flag);
     */
    STALS_PEDESTAL_ENABLE       = 0,        

    /*!
     * Control to set the value of the pedestal\n
     * For example : \ref STALS_SetControl(pHandle, STALS_PEDESTAL_VALUE, 6);
     */
    STALS_PEDESTAL_VALUE        = 1,           

    /*!
     * Control to set if dark is output. For VD6281 dark count will be output on channel 2.
     */
    STALS_OUTPUT_DARK_ENABLE    = 3,

    /*!
     * Control to set drive current in sda pad in mA when device is driving sda line.
     * Possible values for vd6281 are 4, 8, 12, 16, 20 mA. 
    */
    STALS_SDA_DRIVE_VALUE_MA    = 4,

    /*!
     * Control to set if OTP information is use by driver. It is enable by default. Unless you know what you are doing don't change this value\n
     * For example : \ref STALS_SetControl(pHandle, STALS_OTP_USAGE_ENABLE, STALS_CONTROL_DISABLE).
     */
    STALS_OTP_USAGE_ENABLE      = 128,

    /*!
     * Control to get/set workaround state:
     * - use STALS_SetControl() to enable/disable given workaround. Msb bit of
     *   ControlValue control if wa is enable or disable. Others bits are the wa
     *   number.
     * - use STALS_GetControl() to get wa status. pControlValue is then an inout
     *   parameter. you call it with the wa for which you want to know state. on
     *   exit you read msb bit to know wa status.
     */
    STALS_WA_STATE              = 129
};


/** 
 * @struct STALS_FlickerInfo_t
 * 
 * This structure contains the fields filled by the driver and that contains the discovered information related to the flicker 
 */
struct STALS_FlickerInfo_t {
    uint32_t Frequency;                          //!< Value of the frequency
    uint8_t  ConfidenceLevel;                    //!< Confidence on the relevance of the measured value.
    uint8_t  IsMeasureFinish;                    //!< Value is 1 if measure is finish
};

/** 
 * @enum STALS_Mode_t 
 * 
 * This enumeration is aimed at defining the different behavior modes of the STALS device
 */
enum STALS_Mode_t {
    /*!
     * Single shot mode. In this mode, the STALS records and accumulates the light for 1 single period. \n
     * The STALS driver client is responsible for starting the next integration period, by calling the STALS_Start function again 
     */    
    STALS_MODE_ALS_SINGLE_SHOT  = 0,

    /*!
     * Synchronous mode. This mode, is a continuous measurement mode meaning that this needs to be stopped by calling \ref STALS_Stop.
     * But an handshake is necessary to have the registers updated with the values of the on going measurement. This handshake is performed by 
     * the \ref STALS_GetAlsValues function\n
     * Two options are available for the handshake \n
     * 1. by I2C reading. in this case the AC data is available on the GPIO pin
     * 2. by interrupt acknowledgment. in this case the AC data is NOT available 
     */    
    STALS_MODE_ALS_SYNCHRONOUS  = 1,

    /*!
     * Flicker mode. This mode outputs PDM on the GPIO or CLKIN pin.
     */    
    STALS_MODE_FLICKER          = 2
};


/** 
 * @enum STALS_Mode_t 
 * 
 * This enumeration is aimed at defining the different behavior modes of the STALS device
 */
enum STALS_FlickerOutputType_t
{
    STALS_FLICKER_OUTPUT_ANALOG = 0,            //!< Analog. DEPRECATED, use STALS_FLICKER_OUTPUT_ANALOG_CFG_1 instead.
    STALS_FLICKER_OUTPUT_ANALOG_CFG_1 = 0,      //!< Analog. PDM is output to GPIO through pad internal resistance.
    STALS_FLICKER_OUTPUT_DIGITAL_PDM = 1,       //!< PDM Digital. This modes needs an external clock to feed the device through the corresponding pin.
    STALS_FLICKER_OUTPUT_ANALOG_CFG_2 = 2,      //!< Analog. PDM is output to CLKIN. Not supported by vd6281 cut2.0.
};


/** 
 * @struct STALS_Als_t 
 * 
 * This structure is aimed at defining the parameters providing the event counts values of the last light integration, for the selected channels
 */
struct STALS_Als_t {
    uint8_t Channels;                                //!< Flag to be ORED by the driver client in order to understand what are the channels that provide a valid value. 0x3F means that all the channels are valid
    uint32_t CountValue[STALS_ALS_MAX_CHANNELS];     //!< Array providing the event counts value for each of the selected channels. This is value after per device calibration.
    uint32_t CountValueRaw[STALS_ALS_MAX_CHANNELS];  //!< Array providing the event counts value for each of the selected channels. This is value before per device calibration.
};


/**
 * This function Initializes the STALS driver 
 *
 * @param pDeviceName                      Name of the device. Shall be VD6281
 * @param pClient                          Pointer on an client specific plaform specific structure, provided up to the underlying plaform adaptation layers
 * @param pHandle                          Pointer on an opaque pointer to be used as the id of the instance of the driver
 *
 * @note SlaveAddress. If set to the default address of the device, then the irq pin does not need to be set to low.\n
 * WARNING : to set a new I2C slave address, the GPIO pin MUST be set to low and the Init function will then perform an I2C transaction with this I2C slave address. This transaction, being the first one after the power up of the device,
 * will set this new slave address and all further I2C address shall be performed with this I2C slave address.
 * 
 * @note A call to the \ref STALS_Init function shall be done to set the VD6281 device in IDLE mode.
 * @note The VD6281 device needs a delay between powering it and calling \ref STALS_Init. Please refer to the device user manual for more details.
 * 
 * \retval  STALS_NO_ERROR                Success
 * \retval  STALS_ERROR_INIT              Could not initialize the driver
 * \retval  STALS_ERROR_INVALID_PARAMS    At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_WRITE             Could not write any data into the device through I2C
 */
STALS_ErrCode_t STALS_Init(char * pDeviceName, void * pClient, void ** pHandle);

/**
 * This function terminates the provided STALS driver instance
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 */
STALS_ErrCode_t STALS_Term(void * pHandle);

/**
 * This function .......
 *
 * @param pVersion                         Pointer on a value that contains the version of the driver once this function is called
 * @param pRevision                        Pointer on a value that contains the revision of the driver once this function is called
 *
 * @note The MAJOR number of version sits in the upper 16 bits of *pVersion, the MINOR number of version sits in the lower 16 bits of *pVersion
 * The *pRevision value contains the revision of the driver
 * 
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 */
STALS_ErrCode_t STALS_GetVersion(uint32_t *pVersion, uint32_t *pRevision);


/**
 * This function returns the color filter of the provided channel identifier
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param ChannelId                        Channel Id. Permitted values are 0 up to \ref STALS_ALS_MAX_CHANNELS - 1
 * @param pColor                           Pointer on a value in which the color of the channel is returned
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 */
STALS_ErrCode_t STALS_GetChannelColor(void * pHandle, enum STALS_Channel_Id_t ChannelId, enum STALS_Color_Id_t * pColor);

/**
 * This function sets the exposure time into the device, after having tuned to the closest value that the device can support.
 * Note that a fixed readout period of ~6 ms takes place just after exposure time, needed by the device to set the event count values in the registers
 * It also returns the actual applied value in the device
 *
 * For VD6281 cut 2.0 possible values are multiples of 1.6 ms with a range of 1.6 ms to 1.6 s.
 *
 * @note Note Exposure time is irrelevant for flicker detection.
 * 
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param ExpoTimeInUs                     Exposure time in microseconds
 * @param pAppliedExpoTimeUs               Pointer on in which the value of the actual exposure time is returned
 * 
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_ALREADY_STARTED    Exposure can not be set when the device is running
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 */
STALS_ErrCode_t STALS_SetExposureTime(void * pHandle, uint32_t ExpoTimeInUs, uint32_t *pAppliedExpoTimeUs);

/**
 * This function returns the actual exposure time
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param pAppliedExpoTimeUs               Pointer in which the value of the actual exposure time is returned
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 */
STALS_ErrCode_t STALS_GetExposureTime(void * pHandle, uint32_t *pAppliedExpoTimeUs);

/**
 * This function sets an inter measurement time into the device, after having tuned to the closest value that the device can support.
 * It also returns the actual applied value in the device
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param InterMeasurmentInUs              Inter measurement
 * @param pAppliedInterMeasurmentInUs      Pointer on in which the value of the actual inter measurement time is returned
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_ALREADY_STARTED    Inter measurement can not be set when the device is running
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 */
STALS_ErrCode_t STALS_SetInterMeasurementTime(void * pHandle, uint32_t InterMeasurmentInUs, uint32_t *pAppliedInterMeasurmentInUs);

/**
 * This function returns the actual inter measurement
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param pAppliedInterMeasurmentInUs      Pointer on in which the value of the actual inter measurement time is returned
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 */
STALS_ErrCode_t STALS_GetInterMeasurementTime(void * pHandle, uint32_t *pAppliedInterMeasurmentInUs);


/**
 * This function returns the version of the device
 *
 * @param Handle                           Handle on the driver instance
 * @param pDeviceID                        Pointer in which the ID of the device is returned 
 * @param pRevisionID                      Pointer in which the revision of the device is returned 
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 */
STALS_ErrCode_t STALS_GetProductVersion(void * pHandle, uint8_t *pDeviceID, uint8_t *pRevisionID );

/**
 * This function sets an analog gain on the provided channel id.
 *
 * @note The gain impacts the signal output amplitude, but not the values returned by STALS_GetFlicker, 
 * unless the gain is exceeds a maximum value that will flatten the signal output because of its impact on saturation
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param ChannelId                        this id idenfiies the channel number. See \ref STALS_Channel_Id_t 
 * @param Gain                             Gain in 8.8 fixed point unit
 * @param pAppliedGain                     Pointer in which the value of the actual gain applied in the device is returned. Value in 8.8 fixed point unit
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_ALREADY_STARTED    Gain can not be set when the device is running
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 * \retval  STALS_ERROR_READ               Could not read any data from the device through I2C
 */
STALS_ErrCode_t STALS_SetGain(void * pHandle, enum STALS_Channel_Id_t ChannelId, uint16_t Gain, uint16_t *pAppliedGain);

/**
 * This function gets the actual gain applied in the device
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param ChannelId                        this Id idenfiies the channel number. see \ref STALS_Channel_Id_t 
 * @param pAppliedGain                     Pointer in which the value of the actual gain applied in the device is returned. Value in 8.8 fixed point unit
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 */
STALS_ErrCode_t STALS_GetGain(void * pHandle, enum STALS_Channel_Id_t ChannelId, uint16_t *pAppliedGain);

/**
 * This function set CLKIN and GPIO gpio's settings according to FlickerOutputType. Note that those parameters are
 * effectively set when flicker is started.
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param FlickerOutputType                Data ouptut type for the flicker mode. see \ref STALS_FlickerOutputType_t
 * 
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 * \retval  STALS_ERROR_READ               Could not read any data from the device through I2C
 */
STALS_ErrCode_t STALS_SetFlickerOutputType(void * pHandle, enum STALS_FlickerOutputType_t FlickerOutputType);
 
/**
 * This function starts the device
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param Mode                             Mode. shall be \ref STALS_MODE_ALS_SINGLE_SHOT, \ref STALS_MODE_ALS_SYNCHRONOUS or \ref STALS_MODE_FLICKER\n
 * @param Channels.                        For the ALS modes, this is an ORED value of the \ref STALS_Channel_Id_t channels.
 *                                         For the flicker mode, this is one of the \ref STALS_Channel_Id_t channels.\n 
 *
 * @note As the ALS and FLICKER modes can run independently, Two consecutive calls to this START function with \ref STALS_MODE_ALS_SYNCHRONOUS and \ref STALS_MODE_FLICKER modes are permitted.
 * 
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ALREADY_STARTED          The device is already running in the provided mode or in an incompatible mode
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 */
STALS_ErrCode_t STALS_Start(void * pHandle, enum STALS_Mode_t Mode, uint8_t Channels);

/**
 * This function stops the device
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param Mode                             Mode. shall be \ref STALS_MODE_ALS_SINGLE_SHOT, \ref STALS_MODE_ALS_SYNCHRONOUS or \ref STALS_MODE_FLICKER.
 *
 * @note note
 * 
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ALREADY_NOT_STARTED      The device is not running the provided mode
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 */
STALS_ErrCode_t STALS_Stop(void * pHandle, enum STALS_Mode_t Mode);

/**
 * This function that provides the event counts values for the selected channels
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param Channels                         an ORED value of the \ref STALS_Channel_Id_t that permits to select the channels from which the event counts values are to be retrieved.
 * @param pAlsValue                        Pointer on a structure storing the counted events values
 * @param pMeasureValid                    Pointer on a flag telling if the measurement is valid. 
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_WRITE              Could not write any data into the device through I2C
 * \retval  STALS_ERROR_READ               Could not read any data from the device through I2C
 */
STALS_ErrCode_t STALS_GetAlsValues(void * pHandle, uint8_t Channels, struct STALS_Als_t * pAlsValue, uint8_t * pMeasureValid);


/**
 * This function Gets the flicker main harmonic frequency
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param pFlickerInfo                     A pointer on an \ref STALS_FlickerInfo_t structure 
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 * \retval  STALS_ERROR_READ               Could not read any data from the device through I2C
 */
STALS_ErrCode_t STALS_GetFlickerFrequency(void * pHandle, struct STALS_FlickerInfo_t * pFlickerInfo);


/**
 * This function sets a control to the STALS driver
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param ParamId                          Identifier of the param provided
 * @param ControlValue                     The value of the control 
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
 */
STALS_ErrCode_t STALS_SetControl(void * pHandle, enum STALS_Control_Id_t ControlId, uint32_t ControlValue);

/**
 * This function gets a control from the STALS driver
 *
 * @param pHandle                          Opaque pointer used as the id of the instance of the driver
 * @param ParamId                          Identifier of the param provided
 * @param pControlValue                    Pointer on a parameter in which the parameter value is set
 *
 * \retval  STALS_NO_ERROR                 Success
 * \retval  STALS_ERROR_INVALID_PARAMS     At least one of the provided parameters to the function is invalid
*/
STALS_ErrCode_t STALS_GetControl(void * pHandle, enum STALS_Control_Id_t ControlId, uint32_t * pControlValue);

/* following functions must be implemented by platform integrator */
STALS_ErrCode_t STALS_WrByte(void *pClient, uint8_t index, uint8_t data);
STALS_ErrCode_t STALS_RdByte(void *pClient, uint8_t index, uint8_t *data);
/* following function is optional. There is a default weak implementation */
STALS_ErrCode_t STALS_RdMultipleBytes(void *pClient, uint8_t index, uint8_t *data, int nb);

#ifndef BIT
#define BIT(b)							(1UL << (b))
#endif

#define VD6281_DEVICE_ID					0x00
#define VD6281_REVISION_ID					0x01
#define VD6281_IRQ_CTRL_ST					0x02
#define VD6281_ALS_CTRL						0x03
#define VD6281_ALS_START					BIT(0)
#define VD6281_ALS_CONTINUOUS					BIT(1)
#define VD6281_ALS_CONTINUOUS_SLAVED				BIT(2)
#define VD6281_CONTINUOUS_PERIOD				0x04

#define VD6281_CHANNELx_MM(c)					(0x06 + 4 * (c))
#define VD6281_CHANNELx_LM(c)					(0x07 + 4 * (c))
#define VD6281_CHANNELx_LL(c)					(0x08 + 4 * (c))

#define VD6281_EXPOSURE_M					0x1d
#define VD6281_EXPOSURE_L					0x1e
#define VD6281_CHANNEL_VREF(chan)				(0x25 + (chan))

#define VD6281_AC_EN						0x2D
#define VD6281_DC_EN						0x2E
#define VD6281_AC_CLAMP_EN					0x2F
#define VD6281_DC_CLAMP_EN					0x30
#define VD6281_AC_MODE						0x31
#define AC_EXTRACTOR						BIT(0)
#define AC_EXTRACTOR_ENABLE					BIT(0)
#define AC_EXTRACTOR_DISABLE					0
#define AC_CHANNEL_SELECT					0x0e
#define PDM_SELECT_OUTPUT					BIT(4)
#define PDM_SELECT_GPIO						0
#define PDM_SELECT_CLKIN					BIT(4)
#define PDM_SELECT_CLK						BIT(5)
#define PDM_SELECT_INTERNAL_CLK					0
#define PDM_SELECT_EXTERNAL_CLK					BIT(5)
#define AC_PEDESTAL						BIT(6)
#define AC_PEDESTAL_ENABLE					0
#define AC_PEDESTAL_DISABLE					BIT(6)
#define VD6281_AC_PEDESTAL					0x32
#define VD6281_PEDESTAL_VALUE_MASK				0x07
#define VD6281_AC_SAT_METRIC_M					0x33
#define VD6281_AC_SAT_METRIC_L					0x34
#define VD6281_AC_ACC_PERIODS_M					0x35
#define VD6281_AC_ACC_PERIODS_L					0x36
#define VD6281_AC_NB_PERIODS					0x37
#define VD6281_AC_AMPLITUDE_M					0x38
#define VD6281_AC_AMPLITUDE_L					0x39

#define VD6281_SDA_DRIVE					0x3c
#define VD6281_SDA_DRIVE_MASK					0x07
#define VD6281_OSC10M						0x3d
#define VD6281_OSC10M_TRIM_M					0x3e
#define VD6281_OSC10M_TRIM_L					0x3f
#define VD6281_OSC50K_TRIM					0x40

#define VD6281_INTR_CFG						0x41
#define VD6281_DTEST_SELECTION					0x47

#define VD6281_SEL_PD_x(c)					(0x6B + (c))

#define VD6281_OTP_CTRL1					0x58

#define VD6281_OTP_STATUS					0x5a
#define VD6281_OTP1_DATA_READY					BIT(1)
#define VD6281_OTP2_DATA_READY					BIT(3)
#define VD6281_OTP_DATA_READY					(VD6281_OTP1_DATA_READY | VD6281_OTP2_DATA_READY)

#define VD6281_OTP_BANK_0					0x5b
#define VD6281_OTP_BANK_1					0x63

#define VD6281_SPARE_1						0x72

#define VD6281_GLOBAL_RESET					0xFE

#define VD6281_CHANNEL_NB			STALS_ALS_MAX_CHANNELS
#define PWR_ON		1
#define PWR_OFF		0
#define PM_RESUME	1
#define PM_SUSPEND	0
#define NAME_LEN		32

#ifndef VD6281_CONFIG_DEVICES_MAX
#define VD6281_CONFIG_DEVICES_MAX		1
#endif

#define FLICKER_DATA_CNT	200
#define VD6281_IOCTL_MAGIC		0xFD
#define VD6281_IOCTL_READ_FLICKER	_IOR(VD6281_IOCTL_MAGIC, 0x01, int *)

#define VD6281_ADC_TIMEOUT	(msecs_to_jiffies(100))


/* #define ALS_DBG */
/* #define ALS_INFO */

#ifndef ALS_dbg
#ifdef ALS_DBG
#define ALS_dbg(format, arg...)		\
				printk(KERN_DEBUG "ALS_dbg : "format, ##arg)
#define ALS_err(format, arg...)		\
				printk(KERN_DEBUG "ALS_err : "format, ##arg)
#else
#define ALS_dbg(format, arg...)		{if (als_debug)\
				printk(KERN_DEBUG "ALS_dbg : "format, ##arg);\
					}
#define ALS_err(format, arg...)		{if (als_debug)\
				printk(KERN_DEBUG "ALS_err : "format, ##arg);\
					}
#endif
#endif

#ifndef ALS_info
#ifdef ALS_INFO
#define ALS_info(format, arg...)	\
				printk(KERN_INFO "ALS_info : "format, ##arg)
#else
#define ALS_info(format, arg...)	{if (als_info)\
				printk(KERN_INFO "ALS_info : "format, ##arg);\
					}
#endif
#endif

enum {
	DEBUG_REG_STATUS = 1,
	DEBUG_VAR,
};

struct mode_count {
	s32 hrm_cnt;
	s32 amb_cnt;
	s32 prox_cnt;
	s32 sdk_cnt;
	s32 cgm_cnt;
	s32 unkn_cnt;
};

enum platform_pwr_state {
	POWER_ON,
	POWER_OFF,
	POWER_STANDBY,
};

enum dev_state {
	DEV_FREE = 0,
	DEV_INIT,
	DEV_ALS_RUN,
	DEV_FLICKER_RUN,
	DEV_BOTH_RUN
};

enum  {
	EOL_STATE_INIT = -1,
	EOL_STATE_100,
	EOL_STATE_120,
	EOL_STATE_DONE
};

struct VD6281_device {
	enum dev_state st;
	enum STALS_Mode_t als_started_mode;
	void *client;
	void *hdl;
	uint8_t device_id;
	uint8_t revision_id;
	uint8_t dc_chan_en;
	uint8_t ac_chan_en;
	struct {
		uint8_t chan;
	} als;
	struct {
		uint8_t chan;
	} flk;
	uint16_t gains[VD6281_CHANNEL_NB];
	uint32_t exposure;
	enum STALS_FlickerOutputType_t flicker_output_type;
	enum STALS_Control_t is_otp_usage_enable;
	enum STALS_Control_t is_output_dark_enable;
	uint64_t otp_bits[2];
	struct {
		uint16_t hf_trim;
		uint8_t lf_trim;
		uint8_t filter_config;
		uint8_t gains[STALS_ALS_MAX_CHANNELS];
	} otp;
	uint8_t wa_codex_460857;
	uint8_t wa_codex_462997;
	uint8_t wa_codex_461428;
	uint8_t wa_codex_464174;
	uint8_t wa_codex_478654;
};

struct vd6281_device_data {
	struct i2c_client *client;
	struct device *dev;
	struct input_dev *als_input_dev;
	struct mutex i2clock;
	struct mutex activelock;
	struct mutex suspendlock;
	struct mutex flickerdatalock;
	struct miscdevice miscdev;
	struct pinctrl *als_pinctrl;
	struct pinctrl_state *pins_sleep;
	struct pinctrl_state *pins_idle;
	struct delayed_work rear_als_work;
	struct workqueue_struct *flicker_work;
	struct iio_channel *chan;
	struct tasklet_hrtimer timer;
	struct VD6281_device devices;
	struct completion	completion;
	char *vdd_1p8;
	char *i2c_1p8;
	u8 enabled;
	u32 sampling_period_ns;
	u8 regulator_state;
	s32 pin_als_int;
	s32 pin_als_en;
	s32 dev_irq;
	u8 irq_state;
	u32 reg_read_buf;
	u8 debug_mode;
	struct mode_count mode_cnt;
#ifdef CONFIG_ARCH_QCOM
	struct pm_qos_request pm_qos_req_fpm;
#endif
	bool pm_state;
	int isTrimmed;

	u8 part_type;
	u8 part_revision;
	u32 i2c_err_cnt;
	u32 user_ir_data;
	u32 user_flicker_data;
	u8 is_als_start;
	u8 is_flk_start;
	char *eol_result;
	u8 eol_enable;
	u8 eol_result_status;
	s16 eol_state;
	u32 eol_count;
	u32 eol_awb;
	u32 eol_clear;
	u32 eol_flicker;
	u8 eol_flicker_count;
	u32 eol_flicker_awb[6][3];
	u32 eol_pulse_duty[2];
	u32 eol_pulse_count;
	u32 eol_ir_spec[4];
	u32 eol_clear_spec[4];
	s32 pin_led_en;
	int flicker_data[300];
	int flicker_cnt;

#ifdef CONFIG_STM_ALS_EOL_MODE
	u16 awb_sample_cnt;
//	int *flicker_data;
	int flicker_data_cnt;
	u8 fifodata[256];
#endif
};



STALS_ErrCode_t set_channel_gain(struct VD6281_device *dev, int c, uint16_t Gain);
STALS_ErrCode_t set_pedestal_value(struct VD6281_device *dev, uint32_t value);
void vd6281_put_device(struct VD6281_device *dev);
struct VD6281_device *vd6281_get_device(void **pHandle);
void vd6281_flicker_work_cb(struct work_struct *w);

#ifdef CONFIG_ARCH_QCOM
extern int sensors_create_symlink(struct kobject *target, const char *name);
extern void sensors_remove_symlink(struct kobject *target, const char *name);
extern int sensors_register(struct device **dev, void *drvdata,
	struct device_attribute *attributes[], char *name);
#else
extern int sensors_create_symlink(struct input_dev *inputdev);
extern void sensors_remove_symlink(struct input_dev *inputdev);
extern int sensors_register(struct device *dev, void *drvdata,
	struct device_attribute *attributes[], char *name);
#endif
extern void sensors_unregister(struct device *dev,
	struct device_attribute *attributes[]);

extern unsigned int lpcharge;

#endif /* __STALS_VD6281_H */