Merge tag 'v3.10.55' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / arm / mach-mt8127 / tcl8127_c_mlc / flashlight / constant_flashlight / leds_strobe.c
1 #include <linux/kernel.h> //constant xx
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/types.h>
5 #include <linux/wait.h>
6 #include <linux/slab.h>
7 #include <linux/fs.h>
8 #include <linux/sched.h>
9 #include <linux/poll.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/cdev.h>
15 #include <linux/errno.h>
16 #include <linux/time.h>
17 #include "kd_flashlight.h"
18 #include <asm/io.h>
19 #include <asm/uaccess.h>
20 #include "kd_camera_hw.h"
21 #include <cust_gpio_usage.h>
22 #include <linux/hrtimer.h>
23 #include <linux/ktime.h>
24 #include <linux/xlog.h>
25 #include <linux/version.h>
26
27 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
28 #include <linux/mutex.h>
29 #else
30 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
31 #include <linux/semaphore.h>
32 #else
33 #include <asm/semaphore.h>
34 #endif
35 #endif
36
37
38
39 /******************************************************************************
40 * Debug configuration
41 ******************************************************************************/
42 // availible parameter
43 // ANDROID_LOG_ASSERT
44 // ANDROID_LOG_ERROR
45 // ANDROID_LOG_WARNING
46 // ANDROID_LOG_INFO
47 // ANDROID_LOG_DEBUG
48 // ANDROID_LOG_VERBOSE
49 #define TAG_NAME "leds_strobe.c"
50 #define PK_DBG_NONE(fmt, arg...) do {} while (0)
51 #define PK_DBG_FUNC(fmt, arg...) xlog_printk(ANDROID_LOG_DEBUG , TAG_NAME, KERN_INFO "%s: " fmt, __FUNCTION__ ,##arg)
52 #define PK_WARN(fmt, arg...) xlog_printk(ANDROID_LOG_WARNING, TAG_NAME, KERN_WARNING "%s: " fmt, __FUNCTION__ ,##arg)
53 #define PK_NOTICE(fmt, arg...) xlog_printk(ANDROID_LOG_DEBUG , TAG_NAME, KERN_NOTICE "%s: " fmt, __FUNCTION__ ,##arg)
54 #define PK_INFO(fmt, arg...) xlog_printk(ANDROID_LOG_INFO , TAG_NAME, KERN_INFO "%s: " fmt, __FUNCTION__ ,##arg)
55 #define PK_TRC_FUNC(f) xlog_printk(ANDROID_LOG_DEBUG , TAG_NAME, "<%s>\n", __FUNCTION__);
56 #define PK_TRC_VERBOSE(fmt, arg...) xlog_printk(ANDROID_LOG_VERBOSE, TAG_NAME, fmt, ##arg)
57 #define PK_ERROR(fmt, arg...) xlog_printk(ANDROID_LOG_ERROR , TAG_NAME, KERN_ERR "%s: " fmt, __FUNCTION__ ,##arg)
58
59
60 #define DEBUG_LEDS_STROBE
61 #ifdef DEBUG_LEDS_STROBE
62 #define PK_DBG PK_DBG_FUNC
63 #define PK_VER PK_TRC_VERBOSE
64 #define PK_ERR PK_ERROR
65 #else
66 #define PK_DBG(a,...)
67 #define PK_VER(a,...)
68 #define PK_ERR(a,...)
69 #endif
70
71 /******************************************************************************
72 * local variables
73 ******************************************************************************/
74
75 static DEFINE_SPINLOCK(g_strobeSMPLock); /* cotta-- SMP proection */
76
77
78 static u32 strobe_Res = 0;
79 static u32 strobe_Timeus = 0;
80 static BOOL g_strobe_On = 0;
81
82 static int g_duty=-1;
83 static int g_timeOutTimeMs=0;
84
85 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
86 static DEFINE_MUTEX(g_strobeSem);
87 #else
88 static DECLARE_MUTEX(g_strobeSem);
89 #endif
90
91
92 #define STROBE_DEVICE_ID 0x60
93
94
95 static struct work_struct workTimeOut;
96
97 /*****************************************************************************
98 Functions
99 *****************************************************************************/
100 #ifdef GPIO_CAMERA_FLASH_EN_PIN
101 #define GPIO_ENF GPIO_CAMERA_FLASH_EN_PIN
102 #else
103 #define GPIO_ENF 0xFF
104 #endif
105
106 #ifdef GPIO_CAMERA_FLASH_MODE_PIN
107 #define GPIO_ENT GPIO_CAMERA_FLASH_MODE_PIN
108 #define GPIO_ENT_IS_AVALIABLE 1
109 #else
110 #define GPIO_ENT 0xFF
111 #define GPIO_ENT_IS_AVALIABLE 0
112 #endif
113
114 /*CAMERA-FLASH-EN */
115
116
117 extern int iWriteRegI2C(u8 *a_pSendData , u16 a_sizeSendData, u16 i2cId);
118 extern int iReadRegI2C(u8 *a_pSendData , u16 a_sizeSendData, u8 * a_pRecvData, u16 a_sizeRecvData, u16 i2cId);
119 static void work_timeOutFunc(struct work_struct *data);
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 int FL_Enable(void)
135 {
136 if (g_duty==0)
137 {
138 #if 0
139 mt_set_gpio_out(GPIO_ENT,GPIO_OUT_ONE);
140 mt_set_gpio_out(GPIO_ENF,GPIO_OUT_ZERO);
141 #else
142 #if GPIO_ENT_IS_AVALIABLE
143 mt_set_gpio_out(GPIO_ENT,GPIO_OUT_ZERO);
144 #endif
145 mt_set_gpio_out(GPIO_ENF,GPIO_OUT_ONE);
146 #endif
147 PK_DBG(" FL_Enable line=%d\n",__LINE__);
148 }
149 else
150 {
151 #if GPIO_ENT_IS_AVALIABLE
152 mt_set_gpio_out(GPIO_ENT,GPIO_OUT_ZERO);
153 #endif
154 mt_set_gpio_out(GPIO_ENF,GPIO_OUT_ONE);
155 PK_DBG(" FL_Enable line=%d\n",__LINE__);
156 }
157
158 return 0;
159 }
160
161 int FL_Disable(void)
162 {
163 #if GPIO_ENT_IS_AVALIABLE
164 mt_set_gpio_out(GPIO_ENT,GPIO_OUT_ZERO);
165 #endif
166 mt_set_gpio_out(GPIO_ENF,GPIO_OUT_ZERO);
167 PK_DBG(" FL_Disable line=%d\n",__LINE__);
168 return 0;
169 }
170
171 int FL_dim_duty(kal_uint32 duty)
172 {
173 g_duty=duty;
174 PK_DBG(" FL_dim_duty line=%d\n",__LINE__);
175 return 0;
176 }
177
178
179 int FL_Init(void)
180 {
181
182 PK_DBG(" FL_Init - start line=%d\n",__LINE__);
183 PK_DBG(" GPIO_ENF=0x%x, GPIO_MODE_00=0x%x \n",GPIO_ENF,GPIO_MODE_00);
184
185 if (mt_set_gpio_mode(GPIO_ENF,GPIO_MODE_00))
186 {
187 PK_DBG("[constant_flashlight] set gpio mode failed!! \n");
188 }
189 if (mt_set_gpio_dir(GPIO_ENF,GPIO_DIR_OUT))
190 {
191 PK_DBG("[constant_flashlight] set gpio dir failed!! \n");
192 }
193 if (mt_set_gpio_out(GPIO_ENF,GPIO_OUT_ZERO))
194 {
195 PK_DBG("[constant_flashlight] set gpio failed!! \n");
196 }
197
198 /*Init. to disable*/
199 #if GPIO_ENT_IS_AVALIABLE
200 PK_DBG(" FL_Init - start line=%d\n",__LINE__);
201 if (mt_set_gpio_mode(GPIO_ENT,GPIO_MODE_00))
202 {
203 PK_DBG("[constant_flashlight] set gpio mode failed!! \n");
204 }
205 if (mt_set_gpio_dir(GPIO_ENT,GPIO_DIR_OUT))
206 {
207 PK_DBG("[constant_flashlight] set gpio dir failed!! \n");
208 }
209 if (mt_set_gpio_out(GPIO_ENT,GPIO_OUT_ZERO))
210 {
211 PK_DBG("[constant_flashlight] set gpio failed!! \n");
212 }
213 #endif
214 INIT_WORK(&workTimeOut, work_timeOutFunc);
215 PK_DBG(" FL_Init - end line=%d\n",__LINE__);
216 return 0;
217 }
218
219
220 int FL_Uninit(void)
221 {
222 FL_Disable();
223 return 0;
224 }
225
226 /*****************************************************************************
227 User interface
228 *****************************************************************************/
229
230 static void work_timeOutFunc(struct work_struct *data)
231 {
232 FL_Disable();
233 PK_DBG("ledTimeOut_callback\n");
234 //printk(KERN_ALERT "work handler function./n");
235 }
236
237
238
239 enum hrtimer_restart ledTimeOutCallback(struct hrtimer *timer)
240 {
241 schedule_work(&workTimeOut);
242 return HRTIMER_NORESTART;
243 }
244 static struct hrtimer g_timeOutTimer;
245 void timerInit(void)
246 {
247 g_timeOutTimeMs=1000; //1s
248 hrtimer_init( &g_timeOutTimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
249 g_timeOutTimer.function=ledTimeOutCallback;
250
251 }
252
253
254
255 static int constant_flashlight_ioctl(MUINT32 cmd, MUINT32 arg)
256 {
257 int i4RetValue = 0;
258 int ior_shift;
259 int iow_shift;
260 int iowr_shift;
261 ior_shift = cmd - (_IOR(FLASHLIGHT_MAGIC,0, int));
262 iow_shift = cmd - (_IOW(FLASHLIGHT_MAGIC,0, int));
263 iowr_shift = cmd - (_IOWR(FLASHLIGHT_MAGIC,0, int));
264 PK_DBG("constant_flashlight_ioctl() line=%d ior_shift=%d, iow_shift=%d iowr_shift=%d arg=%d\n",__LINE__, ior_shift, iow_shift, iowr_shift, arg);
265 switch (cmd)
266 {
267
268 case FLASH_IOC_SET_TIME_OUT_TIME_MS:
269 PK_DBG("FLASH_IOC_SET_TIME_OUT_TIME_MS: %d\n",arg);
270 g_timeOutTimeMs=arg;
271 break;
272
273
274 case FLASH_IOC_SET_DUTY :
275 PK_DBG("FLASHLIGHT_DUTY: %d\n",arg);
276 FL_dim_duty(arg);
277 break;
278
279
280 case FLASH_IOC_SET_STEP:
281 PK_DBG("FLASH_IOC_SET_STEP: %d\n",arg);
282
283 break;
284
285 case FLASH_IOC_SET_ONOFF :
286 PK_DBG("FLASHLIGHT_ONOFF: %d\n",arg);
287 if (arg==1)
288 {
289 if (g_timeOutTimeMs!=0)
290 {
291 ktime_t ktime;
292 ktime = ktime_set( 0, g_timeOutTimeMs*1000000 );
293 hrtimer_start( &g_timeOutTimer, ktime, HRTIMER_MODE_REL );
294 }
295 FL_Enable();
296 }
297 else
298 {
299 FL_Disable();
300 hrtimer_cancel( &g_timeOutTimer );
301 }
302 break;
303 default :
304 PK_DBG(" No such command \n");
305 i4RetValue = -EPERM;
306 break;
307 }
308 return i4RetValue;
309 }
310
311
312
313
314 static int constant_flashlight_open(void *pArg)
315 {
316 int i4RetValue = 0;
317 PK_DBG("constant_flashlight_open line=%d\n", __LINE__);
318
319 if (0 == strobe_Res)
320 {
321 FL_Init();
322 timerInit();
323 }
324 PK_DBG("constant_flashlight_open line=%d\n", __LINE__);
325 spin_lock_irq(&g_strobeSMPLock);
326
327
328 if (strobe_Res)
329 {
330 PK_ERR(" busy!\n");
331 i4RetValue = -EBUSY;
332 }
333 else
334 {
335 strobe_Res += 1;
336 }
337
338
339 spin_unlock_irq(&g_strobeSMPLock);
340 PK_DBG("constant_flashlight_open line=%d\n", __LINE__);
341
342 return i4RetValue;
343
344 }
345
346
347 static int constant_flashlight_release(void *pArg)
348 {
349 PK_DBG(" constant_flashlight_release\n");
350
351 if (strobe_Res)
352 {
353 spin_lock_irq(&g_strobeSMPLock);
354
355 strobe_Res = 0;
356 strobe_Timeus = 0;
357
358 /* LED On Status */
359 g_strobe_On = FALSE;
360
361 spin_unlock_irq(&g_strobeSMPLock);
362
363 FL_Uninit();
364 }
365
366 PK_DBG(" Done\n");
367
368 return 0;
369
370 }
371
372
373 FLASHLIGHT_FUNCTION_STRUCT constantFlashlightFunc=
374 {
375 constant_flashlight_open,
376 constant_flashlight_release,
377 constant_flashlight_ioctl
378 };
379
380
381 MUINT32 constantFlashlightInit(PFLASHLIGHT_FUNCTION_STRUCT *pfFunc)
382 {
383 if (pfFunc != NULL)
384 {
385 *pfFunc = &constantFlashlightFunc;
386 }
387 return 0;
388 }
389
390
391
392 /* LED flash control for high current capture mode*/
393 ssize_t strobe_VDIrq(void)
394 {
395
396 return 0;
397 }
398
399 EXPORT_SYMBOL(strobe_VDIrq);
400
401