ARM: dts: rockchip: enable vpu for rk3288-miniarm
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rockchip / rk_camera.c
1 #include "rk_camera.h"
2 #include "rk30_camera.h"
3 #include <linux/gpio.h>
4 #include <linux/delay.h>
5 #include <linux/version.h>
6 #include <linux/moduleparam.h>
7 #include <linux/of_gpio.h>
8 #include <linux/of.h>
9 #include <linux/of_irq.h>
10 #include <linux/kernel.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13 #include <linux/of_fdt.h>
14 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
16
17
18 static int camio_version = KERNEL_VERSION(0,1,9); 
19 module_param(camio_version, int, S_IRUGO);
20
21 static int camera_debug = 0; 
22 module_param(camera_debug, int, S_IRUGO|S_IWUSR);    
23
24 #undef  CAMMODULE_NAME
25 #define CAMMODULE_NAME   "rk_cam_io"
26
27 #define ddprintk(level, fmt, arg...) do {                       \
28         if (camera_debug >= level)                                      \
29             printk(KERN_WARNING"%s(%d):" fmt"\n", CAMMODULE_NAME,__LINE__,## arg); } while (0)
30
31 #define dprintk(format, ...) ddprintk(1, format, ## __VA_ARGS__)  
32 #define eprintk(format, ...) printk(KERN_ERR "%s(%d):" format"\n",CAMMODULE_NAME,__LINE__,## __VA_ARGS__)  
33 #define debug_printk(format, ...) ddprintk(3, format, ## __VA_ARGS__)  
34
35 static int rk_sensor_io_init(void);
36 static int rk_sensor_io_deinit(int sensor);
37 static int rk_sensor_ioctrl(struct device *dev,enum rk29camera_ioctrl_cmd cmd, int on);
38 static int rk_sensor_power(struct device *dev, int on);
39 static int rk_sensor_register(void);
40 /*static int rk_sensor_reset(struct device *dev);*/
41
42 static int rk_dts_sensor_probe(struct platform_device *pdev);
43 static int rk_dts_sensor_remove(struct platform_device *pdev);
44 static int rk_dts_cif_probe(struct platform_device *pdev);
45 static int rk_dts_cif_remove(struct platform_device *pdev);
46
47 static int rk_sensor_powerdown(struct device *dev, int on);
48
49 static struct rkcamera_platform_data *new_camera_head;  
50
51 static struct rk29camera_platform_data rk_camera_platform_data = {
52     .io_init = rk_sensor_io_init,
53     .io_deinit = rk_sensor_io_deinit,
54     .sensor_ioctrl = rk_sensor_ioctrl,
55     .sensor_register = rk_sensor_register,
56
57 };
58
59 struct rk29camera_platform_ioctl_cb     sensor_ioctl_cb = {
60         .sensor_power_cb = NULL,
61         .sensor_reset_cb = NULL,
62         .sensor_powerdown_cb = NULL,
63         .sensor_flash_cb = NULL,
64         .sensor_af_cb = NULL,
65 };
66
67
68 static u64 rockchip_device_camera_dmamask = 0xffffffffUL;
69 #if RK_SUPPORT_CIF0
70 static struct resource rk_camera_resource_host_0[2] = {};
71 #endif
72 #if RK_SUPPORT_CIF1
73 static struct resource rk_camera_resource_host_1[2] = {};
74 #endif
75
76 #if RK_SUPPORT_CIF0
77  struct platform_device rk_device_camera_host_0 = {
78         .name             = RK29_CAM_DRV_NAME,
79         .id       = RK_CAM_PLATFORM_DEV_ID_0,                           /* This is used to put cameras on this interface*/ 
80         .num_resources= 2,
81         .resource         = rk_camera_resource_host_0,
82         .dev                    = {
83                 .dma_mask = &rockchip_device_camera_dmamask,
84                 .coherent_dma_mask = 0xffffffffUL,
85                 .platform_data  = &rk_camera_platform_data,
86         }
87 };
88 #endif
89
90 #if RK_SUPPORT_CIF1
91  struct platform_device rk_device_camera_host_1 = {
92         .name             = RK29_CAM_DRV_NAME,
93         .id       = RK_CAM_PLATFORM_DEV_ID_1,                           /* This is used to put cameras on this interface */
94         .num_resources    = ARRAY_SIZE(rk_camera_resource_host_1),
95         .resource         = rk_camera_resource_host_1,
96         .dev                    = {
97                 .dma_mask = &rockchip_device_camera_dmamask,
98                 .coherent_dma_mask = 0xffffffffUL,
99                 .platform_data  = &rk_camera_platform_data,
100         }
101 };
102 #endif
103
104
105
106 static const struct of_device_id of_match_cif[] = {
107     { .compatible = "rockchip,cif" },
108         {},
109 };
110
111 MODULE_DEVICE_TABLE(of,of_match_cif);
112 static struct platform_driver rk_cif_driver =
113 {
114     .driver     = {
115         .name   = RK_CIF_NAME,              
116                 .owner = THIS_MODULE,
117         .of_match_table = of_match_ptr(of_match_cif),
118     },
119     .probe              = rk_dts_cif_probe,
120     .remove             = rk_dts_cif_remove,
121 };
122
123 static const struct of_device_id of_match_sensor[] = {
124     { .compatible = "rockchip,sensor" },
125 };
126 MODULE_DEVICE_TABLE(of,of_match_sensor);
127 static struct platform_driver rk_sensor_driver =
128 {
129     .driver     = {
130         .name   = RK_SENSOR_NAME,              
131                 .owner  = THIS_MODULE,
132         .of_match_table = of_match_ptr(of_match_sensor),
133     },
134     .probe              = rk_dts_sensor_probe,
135     .remove             = rk_dts_sensor_remove,
136 };
137
138 unsigned long rk_cif_grf_base;
139 unsigned long rk_cif_cru_base;
140
141 static int rk_dts_sensor_remove(struct platform_device *pdev)
142 {
143         return 0;
144 }
145 static int      rk_dts_sensor_probe(struct platform_device *pdev)
146 {
147         struct device_node *np, *cp;
148         int sensor_num = 0,err;
149         struct device *dev = &pdev->dev;
150         struct rkcamera_platform_data *new_camera_list;
151
152         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
153         
154         np = dev->of_node;
155         if (!np)
156                 return -ENODEV;
157         for_each_child_of_node(np, cp) {
158                 u32 flash_attach = 0,mir = 0,i2c_rata = 0,i2c_chl = 0,i2c_add = 0;
159                 u32 cif_chl = 0, mclk_rate = 0, is_front = 0;
160                 u32 resolution = 0, powerup_sequence = 0;
161                 
162                 u32     powerdown = INVALID_GPIO,power = INVALID_GPIO,reset = INVALID_GPIO;
163                 u32 af = INVALID_GPIO,flash = INVALID_GPIO;
164
165                 int pwr_active = 0, rst_active = 0, pwdn_active = 0,flash_active = 0;
166                 int orientation = 0;
167                 struct rkcamera_platform_data *new_camera; 
168                 
169                 char sensor_name[20] = {0};
170                 char *name = NULL;
171                 
172                 strcpy(sensor_name,cp->name);
173                 name = sensor_name;
174                 if(strstr(sensor_name,"_") != NULL){                    
175                         name = strsep(&name,"_");
176                 }
177
178                 new_camera = kzalloc(sizeof(struct rkcamera_platform_data),GFP_KERNEL);
179                 if(!sensor_num)
180                 {                       
181                         new_camera_head = new_camera;
182                         rk_camera_platform_data.register_dev_new = new_camera_head;
183                         new_camera_list = new_camera;
184                 }
185                 sensor_num ++;
186                 new_camera_list->next_camera = new_camera;
187                 new_camera_list = new_camera;
188
189                 if (of_property_read_u32(cp, "flash_attach", &flash_attach)) {
190                         dprintk("%s:Get %s rockchip,flash_attach failed!\n",__func__, cp->name);                                
191                 }
192                 if (of_property_read_u32(cp, "mir", &mir)) {
193                         dprintk("%s:Get %s rockchip,mir failed!\n",__func__, cp->name);                         
194                 }
195                 if (of_property_read_u32(cp, "i2c_rata", &i2c_rata)) {
196                         dprintk("%s:Get %s rockchip,i2c_rata failed!\n",__func__, cp->name);                            
197                 }
198                 if (of_property_read_u32(cp, "i2c_chl", &i2c_chl)) {
199                         dprintk("%s:Get %s rockchip,i2c_chl failed!\n",__func__, cp->name);                             
200                 }
201                 if (of_property_read_u32(cp, "cif_chl", &cif_chl)) {
202                         dprintk("%s:Get %s rockchip,cif_chl failed!\n",__func__, cp->name);                             
203                 }
204                 if (of_property_read_u32(cp, "mclk_rate", &mclk_rate)) {
205                         dprintk("%s:Get %s rockchip,mclk_rate failed!\n",__func__, cp->name);                           
206                 }
207                 if (of_property_read_u32(cp, "is_front", &is_front)) {
208                         dprintk("%s:Get %s rockchip,is_front failed!\n",__func__, cp->name);                            
209                 }
210                 if (of_property_read_u32(cp, "rockchip,powerdown", &powerdown)) {
211                                 printk("%s:Get %s rockchip,powerdown failed!\n",__func__, cp->name);                            
212                 }
213                 if (of_property_read_u32(cp, "pwdn_active", &pwdn_active)) {
214                                 dprintk("%s:Get %s pwdn_active failed!\n",__func__, cp->name);                          
215                 }
216                 if (of_property_read_u32(cp, "rockchip,power", &power)) {
217                                 printk("%s:Get %s rockchip,power failed!\n",__func__, cp->name);                                
218                 }
219                 if (of_property_read_u32(cp, "pwr_active", &pwr_active)) {
220                                 dprintk("%s:Get %s pwr_active failed!\n",__func__, cp->name);                           
221                 }
222                 if (of_property_read_u32(cp, "rockchip,reset", &reset)) {
223                                 dprintk("%s:Get %s rockchip,reset failed!\n",__func__, cp->name);                               
224                 }
225                 if (of_property_read_u32(cp, "rst_active", &rst_active)) {
226                                 dprintk("%s:Get %s rst_active failed!\n",__func__, cp->name);                           
227                 }
228                 if (of_property_read_u32(cp, "rockchip,af", &af)) {
229                                 dprintk("%s:Get %s rockchip,af failed!\n",__func__, cp->name);                          
230                 }
231                 if (of_property_read_u32(cp, "rockchip,flash", &flash)) {
232                                 dprintk("%s:Get %s rockchip,flash failed!\n",__func__, cp->name);                               
233                 }               
234                 if (of_property_read_u32(cp, "flash_active", &flash_active)) {
235                                 dprintk("%s:Get %s flash_active failed!\n",__func__, cp->name);                         
236                 }
237                 if (of_property_read_u32(cp, "i2c_add", &i2c_add)) {
238                         printk("%s:Get %s rockchip,i2c_add failed!\n",__func__, cp->name);                              
239                 }
240                 if (of_property_read_u32(cp, "resolution", &resolution)) {
241                         printk("%s:Get %s rockchip,resolution failed!\n",__func__, cp->name);                           
242                 }
243                 if (of_property_read_u32(cp, "powerup_sequence", &powerup_sequence)) {
244                         printk("%s:Get %s rockchip,powerup_sequence failed!\n",__func__, cp->name);                             
245                 }
246                 if (of_property_read_u32(cp, "orientation", &orientation)) {
247                         printk("%s:Get %s rockchip,orientation failed!\n",__func__, cp->name);                          
248                 }
249                 
250                 strcpy(new_camera->dev.i2c_cam_info.type, name);
251                 new_camera->dev.i2c_cam_info.addr = i2c_add>>1;
252                 new_camera->dev.desc_info.host_desc.bus_id = RK29_CAM_PLATFORM_DEV_ID+cif_chl;
253                 new_camera->dev.desc_info.host_desc.i2c_adapter_id = i2c_chl;
254                 new_camera->dev.desc_info.host_desc.module_name = name;
255                 new_camera->dev.device_info.name = "soc-camera-pdrv";
256                 if(is_front)
257                         sprintf(new_camera->dev_name,"%s_%s",name,"front");
258                 else
259                         sprintf(new_camera->dev_name,"%s_%s",name,"back");
260                 new_camera->dev.device_info.dev.init_name =(const char*)&new_camera->dev_name[0];
261                 new_camera->io.gpio_reset = reset;
262                 new_camera->io.gpio_powerdown = powerdown;
263                 new_camera->io.gpio_power = power;
264                 new_camera->io.gpio_af = af;
265                 new_camera->io.gpio_flash = flash;
266                 new_camera->io.gpio_flag = ((pwr_active&0x01)<<RK29_CAM_POWERACTIVE_BITPOS)
267                                                                         |((rst_active&0x01)<<RK29_CAM_RESETACTIVE_BITPOS)
268                                                                         |((pwdn_active&0x01)<<RK29_CAM_POWERDNACTIVE_BITPOS)
269                                                                         |((flash_active&0x01)<<RK29_CAM_FLASHACTIVE_BITPOS);
270                 new_camera->orientation = orientation;
271                 new_camera->resolution = resolution;
272                 new_camera->mirror = mir;
273                 new_camera->i2c_rate = i2c_rata;
274                 new_camera->flash = flash_attach;
275                 new_camera->pwdn_info = ((pwdn_active&0x10)|0x01);
276                 new_camera->powerup_sequence = powerup_sequence;
277                 new_camera->mclk_rate = mclk_rate;
278                 new_camera->of_node = cp;
279                 
280                 new_camera->powerdown_pmu_name = NULL;
281                 new_camera->power_pmu_name1 = NULL;
282                 new_camera->power_pmu_name2 = NULL;
283                 new_camera->powerdown_pmu_voltage = 0;
284                 new_camera->power_pmu_name1 = 0;
285                 new_camera->power_pmu_name2 = 0;
286                 err = of_property_read_string(cp,"rockchip,powerdown_pmu",&(new_camera->powerdown_pmu_name));   
287                 if(err < 0)     {
288                         dprintk("Get rockchip,powerdown_pmu failed\n");
289                 }
290                 err = of_property_read_string(cp,"rockchip,power_pmu_name1",&(new_camera->power_pmu_name1));    
291                 if(err < 0)     {
292                         dprintk("Get rockchip,power_pmu_name1 failed\n");
293                 }
294                 err = of_property_read_string(cp,"rockchip,power_pmu_name2",&(new_camera->power_pmu_name2));    
295                 if(err < 0){
296                         dprintk("rockchip,power_pmu_name2 failed\n");
297                 }
298                 
299                 if (of_property_read_u32(cp, "rockchip,powerdown_pmu_voltage", &(new_camera->powerdown_pmu_voltage))) {
300                         dprintk("%s:Get %s rockchip,resolution failed!\n",__func__, cp->name);                          
301                 }
302                 if (of_property_read_u32(cp, "rockchip,power_pmu_voltage1", &(new_camera->power_pmu_voltage1))) {
303                         dprintk("%s:Get %s rockchip,resolution failed!\n",__func__, cp->name);                          
304                 }
305                 if (of_property_read_u32(cp, "rockchip,power_pmu_voltage2", &(new_camera->power_pmu_voltage2))) {
306                         dprintk("%s:Get %s rockchip,resolution failed!\n",__func__, cp->name);                          
307                 }
308                         debug_printk( "******************* /n power = %x\n", power);
309                         debug_printk( "******************* /n powerdown = %x\n", powerdown);
310                         debug_printk( "******************* /n i2c_add = %x\n", new_camera->dev.i2c_cam_info.addr << 1);
311                         debug_printk( "******************* /n i2c_chl = %d\n", new_camera->dev.desc_info.host_desc.i2c_adapter_id);
312                         debug_printk( "******************* /n init_name = %s\n", new_camera->dev.device_info.dev.init_name);
313                         debug_printk( "******************* /n dev_name = %s\n", new_camera->dev_name);
314                         debug_printk( "******************* /n module_name = %s\n", new_camera->dev.desc_info.host_desc.module_name);
315         };
316         new_camera_list->next_camera = NULL;
317         return 0;
318 }
319         
320 static int rk_dts_cif_remove(struct platform_device *pdev)
321 {
322          return 0;
323 }
324         
325 static int rk_dts_cif_probe(struct platform_device *pdev)
326 {
327         int irq,err;
328         struct device *dev = &pdev->dev;
329         const char *compatible = NULL;  
330         struct device_node * vpu_node =NULL;    
331     int vpu_iommu_enabled = 0;
332
333         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
334         
335         rk_camera_platform_data.cif_dev = &pdev->dev;
336         
337         err = of_address_to_resource(dev->of_node, 0, &rk_camera_resource_host_0[0]);
338         if (err < 0){
339                 printk(KERN_EMERG "Get register resource from %s platform device failed!",pdev->name);
340                 return -ENODEV;
341         }
342         rk_camera_resource_host_0[0].flags = IORESOURCE_MEM;
343         /*map irqs*/
344         irq = irq_of_parse_and_map(dev->of_node, 0);
345         if (irq < 0) {
346                 printk(KERN_EMERG "Get irq resource from %s platform device failed!",pdev->name);
347                 return -ENODEV;;
348         }
349         err = of_property_read_string(dev->of_node->parent,"compatible",&compatible);   
350         rk_camera_platform_data.rockchip_name = compatible;
351
352     vpu_node = of_find_node_by_name(NULL, "vpu_service");
353     if(vpu_node){
354         err = of_property_read_u32(vpu_node, "iommu_enabled", &vpu_iommu_enabled);
355                 rk_camera_platform_data.iommu_enabled = vpu_iommu_enabled;
356         of_node_put(vpu_node);
357         }else{
358                 printk("get vpu_node failed,vpu_iommu_enabled == 0 !!!!!!\n");
359         }
360         
361         if(strstr(rk_camera_platform_data.rockchip_name,"3368")){
362                 //get cru base
363             vpu_node = of_parse_phandle(dev->of_node, "rockchip,cru", 0);
364             rk_cif_cru_base = (unsigned long)of_iomap(vpu_node, 0);
365                 debug_printk(">>>>>>>rk_cif_cru_base=0x%lx",rk_cif_cru_base);
366                 
367                 //get grf base
368             vpu_node = of_parse_phandle(dev->of_node, "rockchip,grf", 0);
369             rk_cif_grf_base = (unsigned long)of_iomap(vpu_node, 0);
370                 debug_printk(">>>>>>>rk_cif_grf_base=0x%lx",rk_cif_grf_base);
371         }
372         
373         if (err < 0){
374                 printk(KERN_EMERG "Get rockchip compatible failed!!!!!!");
375                 return -ENODEV;
376         }
377         
378         //printk(KERN_ERR "***************%s*************\n", rk_camera_platform_data.rockchip_name);
379         rk_camera_resource_host_0[1].start = irq;
380         rk_camera_resource_host_0[1].end   = irq;
381         rk_camera_resource_host_0[1].flags = IORESOURCE_IRQ;
382         return 0;
383 }
384         
385 static int rk_cif_sensor_init(void)
386 {
387         
388         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
389         platform_driver_register(&rk_cif_driver);       
390                 
391         platform_driver_register(&rk_sensor_driver);    
392
393         return 0;
394 }
395
396 static int sensor_power_default_cb (struct rk29camera_gpio_res *res, int on)
397 {
398     int camera_power = res->gpio_power;
399     int camera_ioflag = res->gpio_flag;
400     int camera_io_init = res->gpio_init;    
401     int ret = 0;
402         
403     struct regulator *ldo_18,*ldo_28;
404         struct rkcamera_platform_data *dev = container_of(res,struct rkcamera_platform_data,io);
405         
406         int power_pmu_voltage1 = dev->power_pmu_voltage1;
407         int power_pmu_voltage2 = dev->power_pmu_voltage2;
408         const char *camera_power_pmu_name1 = dev->power_pmu_name1;
409     const char *camera_power_pmu_name2 = dev->power_pmu_name2;;
410         
411         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
412
413         if(camera_power_pmu_name1 != NULL)      {
414                 ldo_28 = regulator_get(NULL, camera_power_pmu_name1);   // vcc28_cif
415         if (on) {
416                         regulator_set_voltage(ldo_28, power_pmu_voltage1, power_pmu_voltage1);
417                         ret = regulator_enable(ldo_28);
418                         //printk("%s set ldo7 vcc28_cif=%dmV end\n", __func__, regulator_get_voltage(ldo_28));
419                         regulator_put(ldo_28);
420                         
421                         msleep(10);
422                 } else {
423                         while(regulator_is_enabled(ldo_28)>0)   
424                                 regulator_disable(ldo_28);
425                         regulator_put(ldo_28);
426                 }               
427         }
428         
429         if(camera_power_pmu_name2 != NULL)      {               
430                 ldo_18 = regulator_get(NULL, camera_power_pmu_name2);   // vcc18_cif
431         if (on) {
432                         regulator_set_voltage(ldo_18, power_pmu_voltage2, power_pmu_voltage2);
433                         //regulator_set_suspend_voltage(ldo, 1800000);
434                         ret = regulator_enable(ldo_18);
435                         //printk("%s set ldo1 vcc18_cif=%dmV end\n", __func__, regulator_get_voltage(ldo_18));
436                         regulator_put(ldo_18);
437                         
438                         msleep(10);
439                 } else {
440                         while(regulator_is_enabled(ldo_18)>0)
441                                 regulator_disable(ldo_18);
442                         regulator_put(ldo_18);
443                 }               
444         }
445         
446     if (camera_power != INVALID_GPIO)  {
447                 if (camera_io_init & RK29_CAM_POWERACTIVE_MASK) {
448             if (on) {
449                 gpio_set_value(camera_power, ((camera_ioflag&RK29_CAM_POWERACTIVE_MASK)>>RK29_CAM_POWERACTIVE_BITPOS));
450                                 dprintk("%s PowerPin=%d ..PinLevel = %x",res->dev_name, camera_power, ((camera_ioflag&RK29_CAM_POWERACTIVE_MASK)>>RK29_CAM_POWERACTIVE_BITPOS));
451                                 msleep(10);
452                 } else {
453                         gpio_set_value(camera_power, (((~camera_ioflag)&RK29_CAM_POWERACTIVE_MASK)>>RK29_CAM_POWERACTIVE_BITPOS));
454                                 dprintk("%s PowerPin=%d ..PinLevel = %x",res->dev_name, camera_power, (((~camera_ioflag)&RK29_CAM_POWERACTIVE_MASK)>>RK29_CAM_POWERACTIVE_BITPOS));
455                         }
456                 } else {
457                         ret = RK29_CAM_EIO_REQUESTFAIL;
458                         eprintk("%s PowerPin=%d request failed!", res->dev_name,camera_power);
459             }        
460     }
461         else {
462                 ret = RK29_CAM_EIO_INVALID;
463     } 
464
465     return ret;
466 }
467
468 static int sensor_reset_default_cb (struct rk29camera_gpio_res *res, int on)
469 {
470     int camera_reset = res->gpio_reset;
471     int camera_ioflag = res->gpio_flag;
472     int camera_io_init = res->gpio_init;  
473     int ret = 0;
474
475         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
476
477     
478     if (camera_reset != INVALID_GPIO) {
479                 if (camera_io_init & RK29_CAM_RESETACTIVE_MASK) {
480                         if (on) {
481                         gpio_set_value(camera_reset, ((camera_ioflag&RK29_CAM_RESETACTIVE_MASK)>>RK29_CAM_RESETACTIVE_BITPOS));
482                         dprintk("%s ResetPin=%d ..PinLevel = %x",res->dev_name,camera_reset, ((camera_ioflag&RK29_CAM_RESETACTIVE_MASK)>>RK29_CAM_RESETACTIVE_BITPOS));
483                         } else {
484                                 gpio_set_value(camera_reset,(((~camera_ioflag)&RK29_CAM_RESETACTIVE_MASK)>>RK29_CAM_RESETACTIVE_BITPOS));
485                         dprintk("%s ResetPin= %d..PinLevel = %x",res->dev_name, camera_reset, (((~camera_ioflag)&RK29_CAM_RESETACTIVE_MASK)>>RK29_CAM_RESETACTIVE_BITPOS));
486                         }
487                 } else {
488                         ret = RK29_CAM_EIO_REQUESTFAIL;
489                         eprintk("%s ResetPin=%d request failed!", res->dev_name,camera_reset);
490                 }
491     } else {
492                 ret = RK29_CAM_EIO_INVALID;
493     }
494
495     return ret;
496 }
497
498 static int sensor_powerdown_default_cb (struct rk29camera_gpio_res *res, int on)
499 {
500     int camera_powerdown = res->gpio_powerdown;
501     int camera_ioflag = res->gpio_flag;
502     int camera_io_init = res->gpio_init;  
503     int ret = 0; 
504         
505     struct regulator *powerdown_pmu;    
506         struct rkcamera_platform_data *dev = container_of(res,struct rkcamera_platform_data,io);
507         int powerdown_pmu_voltage = dev->powerdown_pmu_voltage;
508         const char *powerdown_pmu_name = dev->powerdown_pmu_name;
509
510         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
511
512         if(powerdown_pmu_name != NULL)  {               
513                 powerdown_pmu = regulator_get(NULL, powerdown_pmu_name);
514                 if (on) {
515                         regulator_set_voltage(powerdown_pmu, powerdown_pmu_voltage, powerdown_pmu_voltage);
516                         ret = regulator_enable(powerdown_pmu);
517                         regulator_put(powerdown_pmu);
518                 } else {
519                         while(regulator_is_enabled(powerdown_pmu)>0)    
520                                 regulator_disable(powerdown_pmu);
521                         regulator_put(powerdown_pmu);           
522                 }               
523         }
524         
525     if (camera_powerdown != INVALID_GPIO) {
526                 if (camera_io_init & RK29_CAM_POWERDNACTIVE_MASK) {
527                         if (on) {
528                         gpio_set_value(camera_powerdown, ((camera_ioflag&RK29_CAM_POWERDNACTIVE_MASK)>>RK29_CAM_POWERDNACTIVE_BITPOS));
529                         dprintk("%s PowerDownPin=%d ..PinLevel = %x" ,res->dev_name,camera_powerdown, ((camera_ioflag&RK29_CAM_POWERDNACTIVE_MASK)>>RK29_CAM_POWERDNACTIVE_BITPOS));
530                         } else {
531                                 gpio_set_value(camera_powerdown,(((~camera_ioflag)&RK29_CAM_POWERDNACTIVE_MASK)>>RK29_CAM_POWERDNACTIVE_BITPOS));
532                         dprintk("%s PowerDownPin= %d..PinLevel = %x" ,res->dev_name, camera_powerdown, (((~camera_ioflag)&RK29_CAM_POWERDNACTIVE_MASK)>>RK29_CAM_POWERDNACTIVE_BITPOS));
533                         }
534                 } else {
535                         ret = RK29_CAM_EIO_REQUESTFAIL;
536                         dprintk("%s PowerDownPin=%d request failed!", res->dev_name,camera_powerdown);
537                 }
538     }else {
539                 ret = RK29_CAM_EIO_INVALID;
540     }
541     return ret;
542 }
543
544
545 static int sensor_flash_default_cb (struct rk29camera_gpio_res *res, int on)
546 {
547     int camera_flash = res->gpio_flash;
548     int camera_ioflag = res->gpio_flag;
549     int camera_io_init = res->gpio_init;  
550     int ret = 0;    
551
552         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
553
554     if (camera_flash != INVALID_GPIO) {
555                 if (camera_io_init & RK29_CAM_FLASHACTIVE_MASK) {
556             switch (on)
557             {
558                 case Flash_Off:
559                 {
560                     gpio_set_value(camera_flash,(((~camera_ioflag)&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));
561                             dprintk("%s FlashPin= %d..PinLevel = %x", res->dev_name, camera_flash, (((~camera_ioflag)&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS)); 
562                             break;
563                 }
564
565                 case Flash_On:
566                 {
567                     gpio_set_value(camera_flash, ((camera_ioflag&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));
568                             dprintk("%s FlashPin=%d ..PinLevel = %x", res->dev_name,camera_flash, ((camera_ioflag&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));
569                             break;
570                 }
571
572                 case Flash_Torch:
573                 {
574                     gpio_set_value(camera_flash, ((camera_ioflag&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));
575                             dprintk("%s FlashPin=%d ..PinLevel = %x", res->dev_name,camera_flash, ((camera_ioflag&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));
576                             break;
577                 }
578
579                 default:
580                 {
581                     eprintk("%s Flash command(%d) is invalidate", res->dev_name,on);
582                     break;
583                 }
584             }
585                 } else {
586                         ret = RK29_CAM_EIO_REQUESTFAIL;
587                         eprintk("%s FlashPin=%d request failed!", res->dev_name,camera_flash);
588                 }
589     } else {
590                 ret = RK29_CAM_EIO_INVALID;
591     }
592     return ret;
593 }
594
595 static int sensor_afpower_default_cb (struct rk29camera_gpio_res *res, int on)
596 {
597         int ret = 0;   
598         int camera_af = res->gpio_af;
599         
600         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
601
602         
603         if (camera_af != INVALID_GPIO) {
604                 gpio_set_value(camera_af, on);
605         }
606
607         return ret;
608 }
609
610 static int _rk_sensor_io_init_(struct rk29camera_gpio_res *gpio_res,struct device_node *of_node)
611 {
612     int ret = 0;
613     unsigned int camera_reset = INVALID_GPIO, camera_power = INVALID_GPIO;
614         unsigned int camera_powerdown = INVALID_GPIO, camera_flash = INVALID_GPIO;
615         unsigned int camera_af = INVALID_GPIO,camera_ioflag;
616     struct rk29camera_gpio_res *io_res;
617     bool io_requested_in_camera;
618         enum of_gpio_flags flags;
619         
620         struct rkcamera_platform_data *new_camera;
621         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
622
623
624     camera_reset = gpio_res->gpio_reset;
625         camera_power = gpio_res->gpio_power;
626         camera_powerdown = gpio_res->gpio_powerdown;
627         camera_flash = gpio_res->gpio_flash;
628         camera_af = gpio_res->gpio_af;  
629         camera_ioflag = gpio_res->gpio_flag;
630         gpio_res->gpio_init = 0;
631
632     if (camera_power != INVALID_GPIO) {
633                 debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$/ camera_power  = %x\n", camera_power );
634
635                 camera_power = of_get_named_gpio_flags(of_node,"rockchip,power",0,&flags);
636                 gpio_res->gpio_power = camera_power;/* information back to the IO*/
637
638                 debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$/ camera_power  = %x\n", camera_power );  
639
640                 ret = gpio_request(camera_power, "camera power"); 
641         if (ret) {
642                         
643             io_requested_in_camera = false;
644
645             if (io_requested_in_camera==false) {
646
647                                 new_camera = new_camera_head;
648                 while (new_camera != NULL) {
649                     io_res = &new_camera->io;
650                     if (io_res->gpio_init & RK29_CAM_POWERACTIVE_MASK) {
651                         if (io_res->gpio_power == camera_power)
652                             io_requested_in_camera = true;    
653                     }
654                     new_camera = new_camera->next_camera;
655                 }
656
657             }
658             
659             if (io_requested_in_camera==false) {
660                 printk( "%s power pin(%d) init failed\n", gpio_res->dev_name,camera_power);
661                 goto _rk_sensor_io_init_end_;
662             } else {
663                 ret =0;
664             }
665         }
666       
667                 gpio_res->gpio_init |= RK29_CAM_POWERACTIVE_MASK;
668         gpio_set_value(camera_power, (((~camera_ioflag)&RK29_CAM_POWERACTIVE_MASK)>>RK29_CAM_POWERACTIVE_BITPOS));
669         gpio_direction_output(camera_power, (((~camera_ioflag)&RK29_CAM_POWERACTIVE_MASK)>>RK29_CAM_POWERACTIVE_BITPOS));
670
671                 dprintk("%s power pin(%d) init success(0x%x)" ,gpio_res->dev_name,camera_power,(((~camera_ioflag)&RK29_CAM_POWERACTIVE_MASK)>>RK29_CAM_POWERACTIVE_BITPOS));
672
673     }
674
675     if (camera_reset != INVALID_GPIO) {
676                 
677                 camera_power = of_get_named_gpio_flags(of_node,"rockchip,reset",0,&flags);
678                 gpio_res->gpio_reset = camera_reset;/* information back to the IO*/
679         ret = gpio_request(camera_reset, "camera reset");
680         if (ret) {
681             io_requested_in_camera = false;
682
683             if (io_requested_in_camera==false) {
684                                 
685                 new_camera = new_camera_head;
686                 while (new_camera != NULL) {
687                     io_res = &new_camera->io;
688                     if (io_res->gpio_init & RK29_CAM_RESETACTIVE_MASK) {
689                         if (io_res->gpio_reset == camera_reset)
690                             io_requested_in_camera = true;    
691                     }
692                     new_camera = new_camera->next_camera;
693                 }
694             }
695             
696             if (io_requested_in_camera==false) {
697                 eprintk("%s reset pin(%d) init failed" ,gpio_res->dev_name,camera_reset);
698                 goto _rk_sensor_io_init_end_;
699             } else {
700                 ret =0;
701             }
702         }
703        
704                 gpio_res->gpio_init |= RK29_CAM_RESETACTIVE_MASK;
705         gpio_set_value(camera_reset, ((camera_ioflag&RK29_CAM_RESETACTIVE_MASK)>>RK29_CAM_RESETACTIVE_BITPOS));
706         gpio_direction_output(camera_reset, ((camera_ioflag&RK29_CAM_RESETACTIVE_MASK)>>RK29_CAM_RESETACTIVE_BITPOS));
707
708                 dprintk("%s reset pin(%d) init success(0x%x)" ,gpio_res->dev_name,camera_reset,((camera_ioflag&RK29_CAM_RESETACTIVE_MASK)>>RK29_CAM_RESETACTIVE_BITPOS));
709
710     }
711
712         if (camera_powerdown != INVALID_GPIO) {
713                 debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$/ camera_powerdown  = %x\n", camera_powerdown );
714
715                 camera_powerdown = of_get_named_gpio_flags(of_node,"rockchip,powerdown",0,&flags);
716                 gpio_res->gpio_powerdown = camera_powerdown;/*information back to the IO*/
717
718                 debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$/ camera_powerdown  = %x\n", camera_powerdown );  
719                 ret = gpio_request(camera_powerdown, "camera powerdown");
720         if (ret) {
721             io_requested_in_camera = false;
722
723             if (io_requested_in_camera==false) {
724                                 
725                 new_camera = new_camera_head;
726                 while (new_camera != NULL) {
727                     io_res = &new_camera->io;
728                     if (io_res->gpio_init & RK29_CAM_POWERDNACTIVE_MASK) {
729                         if (io_res->gpio_powerdown == camera_powerdown)
730                             io_requested_in_camera = true;    
731                     }
732                     new_camera = new_camera->next_camera;
733                 }
734             }
735             
736             if (io_requested_in_camera==false) {
737                 eprintk("%s powerdown pin(%d) init failed",gpio_res->dev_name,camera_powerdown);
738                 goto _rk_sensor_io_init_end_;
739             } else {
740                 ret =0;
741             }
742         }
743                 debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s(),iomux is ok\n", __FILE__, __LINE__,__FUNCTION__);
744         
745                 gpio_res->gpio_init |= RK29_CAM_POWERDNACTIVE_MASK;
746         gpio_set_value(camera_powerdown, ((camera_ioflag&RK29_CAM_POWERDNACTIVE_MASK)>>RK29_CAM_POWERDNACTIVE_BITPOS));
747         gpio_direction_output(camera_powerdown, ((camera_ioflag&RK29_CAM_POWERDNACTIVE_MASK)>>RK29_CAM_POWERDNACTIVE_BITPOS));
748
749                 dprintk("%s powerdown pin(%d) init success(0x%x)" ,gpio_res->dev_name,camera_powerdown,((camera_ioflag&RK29_CAM_POWERDNACTIVE_BITPOS)>>RK29_CAM_POWERDNACTIVE_BITPOS));
750
751     }
752
753         if (camera_flash != INVALID_GPIO) {
754
755                 camera_flash = of_get_named_gpio_flags(of_node,"rockchip,flash",0,&flags);
756                 gpio_res->gpio_flash = camera_flash;/* information back to the IO*/
757         ret = gpio_request(camera_flash, "camera flash");
758         if (ret) {
759             io_requested_in_camera = false;
760
761             if (io_requested_in_camera==false) {
762
763                                 new_camera = new_camera_head;
764                 while (new_camera != NULL) {
765                     io_res = &new_camera->io;
766                     if (io_res->gpio_init & RK29_CAM_POWERDNACTIVE_MASK) {
767                         if (io_res->gpio_powerdown == camera_powerdown)
768                             io_requested_in_camera = true;    
769                     }
770                     new_camera = new_camera->next_camera;
771                 }
772             }
773             
774             ret = 0;        //ddl@rock-chips.com : flash is only a function, sensor is also run;
775             if (io_requested_in_camera==false) {
776                 eprintk("%s flash pin(%d) init failed",gpio_res->dev_name,camera_flash);
777                 goto _rk_sensor_io_init_end_;
778             }
779         }
780
781
782         
783                 gpio_res->gpio_init |= RK29_CAM_FLASHACTIVE_MASK;
784         gpio_set_value(camera_flash, ((~camera_ioflag&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));   //  falsh off 
785         gpio_direction_output(camera_flash, ((~camera_ioflag&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));
786
787                 dprintk("%s flash pin(%d) init success(0x%x)",gpio_res->dev_name, camera_flash,((camera_ioflag&RK29_CAM_FLASHACTIVE_MASK)>>RK29_CAM_FLASHACTIVE_BITPOS));
788
789     }  
790
791         if (camera_af != INVALID_GPIO) {
792                 
793                 camera_af = of_get_named_gpio_flags(of_node,"rockchip,af",0,&flags);
794                 gpio_res->gpio_af = camera_af;/* information back to the IO*/
795                 ret = gpio_request(camera_af, "camera af");
796                 if (ret) {
797                         io_requested_in_camera = false;
798
799                         if (io_requested_in_camera==false) {
800
801                                 new_camera = new_camera_head;
802                                 while (new_camera != NULL) {
803                                         io_res = &new_camera->io;
804                                         if (io_res->gpio_init & RK29_CAM_AFACTIVE_MASK) {
805                                                 if (io_res->gpio_af == camera_af)
806                                                         io_requested_in_camera = true;    
807                                         }
808                                         new_camera = new_camera->next_camera;
809                                 }
810                         }
811                         
812                         if (io_requested_in_camera==false) {
813                                 eprintk("%s af pin(%d) init failed",gpio_res->dev_name,camera_af);
814                                 goto _rk_sensor_io_init_end_;
815                         } else {
816                 ret =0;
817             }
818                         
819                 }
820                 
821                 gpio_res->gpio_init |= RK29_CAM_AFACTIVE_MASK;
822                 //gpio_direction_output(camera_af, ((camera_ioflag&RK29_CAM_AFACTIVE_MASK)>>RK29_CAM_AFACTIVE_BITPOS));
823                 dprintk("%s af pin(%d) init success",gpio_res->dev_name, camera_af);
824
825         }
826
827         
828 _rk_sensor_io_init_end_:
829     return ret;
830
831 }
832
833 static int _rk_sensor_io_deinit_(struct rk29camera_gpio_res *gpio_res)
834 {
835     unsigned int camera_reset = INVALID_GPIO, camera_power = INVALID_GPIO;
836         unsigned int camera_powerdown = INVALID_GPIO, camera_flash = INVALID_GPIO,camera_af = INVALID_GPIO;
837
838         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
839
840     
841     camera_reset = gpio_res->gpio_reset;
842     camera_power = gpio_res->gpio_power;
843         camera_powerdown = gpio_res->gpio_powerdown;
844     camera_flash = gpio_res->gpio_flash;
845     camera_af = gpio_res->gpio_af;
846
847         if (gpio_res->gpio_init & RK29_CAM_POWERACTIVE_MASK) {
848             if (camera_power != INVALID_GPIO) {
849                 gpio_direction_input(camera_power);
850                 gpio_free(camera_power);
851             }
852         }
853
854         if (gpio_res->gpio_init & RK29_CAM_RESETACTIVE_MASK) {
855             if (camera_reset != INVALID_GPIO)  {
856                 gpio_direction_input(camera_reset);
857                 gpio_free(camera_reset);
858             }
859         }
860
861         if (gpio_res->gpio_init & RK29_CAM_POWERDNACTIVE_MASK) {
862             if (camera_powerdown != INVALID_GPIO)  {
863                 gpio_direction_input(camera_powerdown);
864                 gpio_free(camera_powerdown);
865             }
866         }
867
868         if (gpio_res->gpio_init & RK29_CAM_FLASHACTIVE_MASK) {
869             if (camera_flash != INVALID_GPIO)  {
870                 gpio_direction_input(camera_flash);
871                 gpio_free(camera_flash);
872             }
873         }
874         if (gpio_res->gpio_init & RK29_CAM_AFACTIVE_MASK) {
875             if (camera_af != INVALID_GPIO)  {
876                /* gpio_direction_input(camera_af);*/
877                 gpio_free(camera_af);
878             }
879         }       
880         gpio_res->gpio_init = 0;
881         
882     return 0;
883 }
884
885 static int rk_sensor_io_init(void)
886 {
887         static bool is_init = false;
888         
889         struct rkcamera_platform_data *new_camera;
890         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
891
892     if(is_init) {               
893                 return 0;
894         } else {
895                 is_init = true;
896         }
897     
898     if (sensor_ioctl_cb.sensor_power_cb == NULL)
899         sensor_ioctl_cb.sensor_power_cb = sensor_power_default_cb;
900     if (sensor_ioctl_cb.sensor_reset_cb == NULL)
901         sensor_ioctl_cb.sensor_reset_cb = sensor_reset_default_cb;
902     if (sensor_ioctl_cb.sensor_powerdown_cb == NULL)
903         sensor_ioctl_cb.sensor_powerdown_cb = sensor_powerdown_default_cb;
904     if (sensor_ioctl_cb.sensor_flash_cb == NULL)
905         sensor_ioctl_cb.sensor_flash_cb = sensor_flash_default_cb;
906     if (sensor_ioctl_cb.sensor_af_cb == NULL)
907         sensor_ioctl_cb.sensor_af_cb = sensor_afpower_default_cb;       
908
909         new_camera = new_camera_head;
910         while(new_camera != NULL)
911         {
912                 if (_rk_sensor_io_init_(&new_camera->io,new_camera->of_node)<0)
913             _rk_sensor_io_deinit_(&new_camera->io);
914                 new_camera = new_camera->next_camera;
915         }
916         return 0;
917 }
918
919 static int rk_sensor_io_deinit(int sensor)
920 {
921         struct rkcamera_platform_data *new_camera;
922
923         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
924
925         new_camera = new_camera_head;
926         while(new_camera != NULL)
927         {
928                 _rk_sensor_io_deinit_(&new_camera->io);
929                 new_camera = new_camera->next_camera;
930         }
931
932     return 0;
933 }
934 static int rk_sensor_ioctrl(struct device *dev,enum rk29camera_ioctrl_cmd cmd, int on)
935 {
936     struct rk29camera_gpio_res *res = NULL;
937     struct rkcamera_platform_data *new_cam_dev = NULL;
938         struct rk29camera_platform_data* plat_data = &rk_camera_platform_data;
939     int ret = RK29_CAM_IO_SUCCESS,i = 0;
940         struct soc_camera_desc *dev_icl = NULL;
941         struct rkcamera_platform_data *new_camera;
942         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
943
944     if (res == NULL) {
945                 new_camera = new_camera_head;
946                 while(new_camera != NULL)
947                 {
948             if (strcmp(new_camera->dev_name, dev_name(dev)) == 0) {
949                 res = (struct rk29camera_gpio_res *)&new_camera->io; 
950                 new_cam_dev = &new_camera[i];
951                                  dev_icl = &new_camera->dev.desc_info;
952                 break;
953             }
954             new_camera = new_camera->next_camera;;
955         }               
956     }
957     
958     if (res == NULL) {
959         eprintk("%s is not regisiterd in rk29_camera_platform_data!!",dev_name(dev));
960         ret = RK29_CAM_EIO_INVALID;
961         goto rk_sensor_ioctrl_end;
962     }
963         
964         switch (cmd)
965         {
966                 case Cam_Power:
967                 {
968                         if (sensor_ioctl_cb.sensor_power_cb) {
969                 ret = sensor_ioctl_cb.sensor_power_cb(res, on);   
970                 ret = (ret != RK29_CAM_EIO_INVALID)?ret:0;     /* ddl@rock-chips.com: v0.1.1 */ 
971                         } else {
972                 eprintk("sensor_ioctl_cb.sensor_power_cb is NULL");
973                 WARN_ON(1);
974                         }
975
976                         printk("ret: %d\n",ret);
977                         break;
978                 }
979                 case Cam_Reset:
980                 {
981                         if (sensor_ioctl_cb.sensor_reset_cb) {
982                 ret = sensor_ioctl_cb.sensor_reset_cb(res, on);
983
984                 ret = (ret != RK29_CAM_EIO_INVALID)?ret:0;
985                         } else {
986                 eprintk( "sensor_ioctl_cb.sensor_reset_cb is NULL");
987                 WARN_ON(1);
988                         }
989                         break;
990                 }
991
992                 case Cam_PowerDown:
993                 {
994                         if (sensor_ioctl_cb.sensor_powerdown_cb) {
995                 ret = sensor_ioctl_cb.sensor_powerdown_cb(res, on);
996                         } else {
997                 eprintk( "sensor_ioctl_cb.sensor_powerdown_cb is NULL");
998                 WARN_ON(1);
999                         }
1000                         break;
1001                 }
1002
1003                 case Cam_Flash:
1004                 {
1005                         if (sensor_ioctl_cb.sensor_flash_cb) {
1006                 ret = sensor_ioctl_cb.sensor_flash_cb(res, on);
1007                         } else {
1008                 eprintk( "sensor_ioctl_cb.sensor_flash_cb is NULL!");
1009                 WARN_ON(1);
1010                         }
1011                         break;
1012                 }
1013                 
1014                 case Cam_Af:
1015                 {
1016                         if (sensor_ioctl_cb.sensor_af_cb) {
1017                 ret = sensor_ioctl_cb.sensor_af_cb(res, on);
1018                         } else {
1019                 eprintk( "sensor_ioctl_cb.sensor_af_cb is NULL!");
1020                 WARN_ON(1);
1021                         }
1022                         break;
1023                 }
1024
1025         case Cam_Mclk:
1026         {
1027             if (plat_data->sensor_mclk && dev_icl) {
1028                                 plat_data->sensor_mclk(dev_icl->host_desc.bus_id,(on!=0)?1:0,on);
1029             } else { 
1030                 eprintk( "%s(%d): sensor_mclk(%p) or dev_icl(%p) is NULL",
1031                     __FUNCTION__,__LINE__,plat_data->sensor_mclk,dev_icl);
1032             }
1033             break;
1034         }
1035         
1036                 default:
1037                 {
1038                         eprintk("%s cmd(0x%x) is unknown!",__FUNCTION__, cmd);
1039                         break;
1040                 }
1041         }
1042 rk_sensor_ioctrl_end:
1043     return ret;
1044 }
1045
1046 static int rk_sensor_pwrseq(struct device *dev,int powerup_sequence, int on, int mclk_rate)
1047 {
1048     int ret =0;
1049     int i,powerup_type;
1050
1051         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
1052
1053     
1054     for (i=0; i<8; i++) {
1055
1056         if (on == 1)
1057             powerup_type = SENSOR_PWRSEQ_GET(powerup_sequence,i);
1058         else
1059             powerup_type = SENSOR_PWRSEQ_GET(powerup_sequence,(7-i));
1060         
1061         switch (powerup_type)
1062         {
1063             case SENSOR_PWRSEQ_AVDD:
1064             case SENSOR_PWRSEQ_DOVDD:
1065             case SENSOR_PWRSEQ_DVDD:
1066             case SENSOR_PWRSEQ_PWR:
1067             {  
1068                 ret = rk_sensor_ioctrl(dev,Cam_Power, on);
1069                 if (ret<0) {
1070                     eprintk("SENSOR_PWRSEQ_PWR failed");
1071                 } else { 
1072                     msleep(10);
1073                     dprintk("SensorPwrSeq-power: %d",on);
1074                 }
1075                 break;
1076             }
1077
1078             case SENSOR_PWRSEQ_HWRST:
1079             {
1080                 if(!on){
1081                     rk_sensor_ioctrl(dev,Cam_Reset, 1);
1082                 }else{
1083                     ret = rk_sensor_ioctrl(dev,Cam_Reset, 1);
1084                     msleep(2);
1085                     ret |= rk_sensor_ioctrl(dev,Cam_Reset, 0); 
1086                 }
1087                 if (ret<0) {
1088                     eprintk("SENSOR_PWRSEQ_HWRST failed");
1089                 } else {
1090                     dprintk("SensorPwrSeq-reset: %d",on);
1091                 }
1092                 break;
1093             }
1094
1095             case SENSOR_PWRSEQ_PWRDN:
1096             {     
1097                 ret = rk_sensor_ioctrl(dev,Cam_PowerDown, !on);
1098                 if (ret<0) {
1099                     eprintk("SENSOR_PWRSEQ_PWRDN failed");
1100                 } else {
1101                     dprintk("SensorPwrSeq-power down: %d",!on);
1102                 }
1103                 break;
1104             }
1105
1106             case SENSOR_PWRSEQ_CLKIN:
1107             {
1108                 ret = rk_sensor_ioctrl(dev,Cam_Mclk, (on?mclk_rate:on));
1109                 if (ret<0) {
1110                     eprintk("SENSOR_PWRSEQ_CLKIN failed");
1111                 } else {
1112                     dprintk("SensorPwrSeq-clock: %d",on);
1113                 }
1114                 break;
1115             }
1116
1117             default:
1118                 break;
1119         }
1120         
1121     } 
1122
1123     return ret;
1124 }
1125
1126 static int rk_sensor_power(struct device *dev, int on)
1127 {
1128     int powerup_sequence,mclk_rate;
1129     
1130     struct rk29camera_platform_data* plat_data = &rk_camera_platform_data;
1131     struct rk29camera_gpio_res *dev_io = NULL;
1132     struct rkcamera_platform_data *new_camera=NULL, *new_device=NULL;
1133     bool real_pwroff = true;
1134     int ret = 0;
1135
1136         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
1137
1138     new_camera = plat_data->register_dev_new;
1139     
1140         while (new_camera != NULL) {
1141
1142         if (new_camera->io.gpio_powerdown != INVALID_GPIO) {            
1143             gpio_direction_output(new_camera->io.gpio_powerdown,
1144                 ((new_camera->io.gpio_flag&RK29_CAM_POWERDNACTIVE_MASK)>>RK29_CAM_POWERDNACTIVE_BITPOS));            
1145         }
1146
1147                 debug_printk( "new_camera->dev_name= %s \n", new_camera->dev_name);
1148                 debug_printk( "dev_name(dev)= %s \n", dev_name(dev));
1149                 
1150         if (strcmp(new_camera->dev_name,dev_name(dev))) {               
1151                         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i\n", __FILE__, __LINE__);
1152             if (sensor_ioctl_cb.sensor_powerdown_cb && on)
1153                 {
1154                         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i\n", __FILE__, __LINE__);
1155                         sensor_ioctl_cb.sensor_powerdown_cb(&new_camera->io,1);
1156                 }
1157         } else {
1158             new_device = new_camera;
1159             dev_io = &new_camera->io;
1160             debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i\n", __FILE__, __LINE__);
1161             if (!Sensor_Support_DirectResume(new_camera->pwdn_info))
1162                 real_pwroff = true;                     
1163             else
1164                 real_pwroff = false;
1165         }
1166         new_camera = new_camera->next_camera;
1167     }
1168
1169     if (new_device != NULL) {
1170         powerup_sequence = new_device->powerup_sequence;
1171         if ((new_device->mclk_rate == 24) || (new_device->mclk_rate == 48))
1172             mclk_rate = new_device->mclk_rate*1000000;
1173         else 
1174             mclk_rate = 24000000;
1175     } else {
1176         powerup_sequence = sensor_PWRSEQ_DEFAULT;
1177         mclk_rate = 24000000;
1178     }
1179         
1180     if (on) {
1181                 debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i\n", __FILE__, __LINE__);
1182         rk_sensor_pwrseq(dev, powerup_sequence, on,mclk_rate);  
1183     } else {
1184         if (real_pwroff) {
1185             if (rk_sensor_pwrseq(dev, powerup_sequence, on,mclk_rate)<0)    /* ddl@rock-chips.com: v0.1.5 */
1186                 goto PowerDown;
1187             
1188             /*ddl@rock-chips.com: all power down switch to Hi-Z after power off*/
1189             new_camera = plat_data->register_dev_new;
1190                         while (new_camera != NULL) {
1191                 if (new_camera->io.gpio_powerdown != INVALID_GPIO) {
1192                     gpio_direction_input(new_camera->io.gpio_powerdown);            
1193                 }
1194                 new_camera->pwdn_info |= 0x01;
1195                 new_camera = new_camera->next_camera;
1196             }
1197         } else {  
1198 PowerDown:
1199             rk_sensor_ioctrl(dev,Cam_PowerDown, !on);
1200
1201             rk_sensor_ioctrl(dev,Cam_Mclk, 0);
1202         }
1203
1204         mdelay(10);/* ddl@rock-chips.com: v0.1.3 */
1205     }
1206     return ret;
1207 }
1208 #if 0
1209 static int rk_sensor_reset(struct device *dev)
1210 {
1211 #if 0
1212         rk_sensor_ioctrl(dev,Cam_Reset,1);
1213         msleep(2);
1214         rk_sensor_ioctrl(dev,Cam_Reset,0);
1215 #else
1216     /*
1217     *ddl@rock-chips.com : the rest function invalidate, because this operate is put together in rk_sensor_power;
1218     */
1219 #endif
1220         return 0;
1221 }
1222 #endif
1223 static int rk_sensor_powerdown(struct device *dev, int on)
1224 {
1225
1226         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()/n", __FILE__, __LINE__,__FUNCTION__);
1227
1228         return rk_sensor_ioctrl(dev,Cam_PowerDown,on);
1229 }
1230
1231 int rk_sensor_register(void)
1232 {
1233     int i;    
1234         struct rkcamera_platform_data *new_camera;      
1235         
1236     i = 0;
1237         debug_printk( "/$$$$$$$$$$$$$$$$$$$$$$//n Here I am: %s:%i-------%s()\n", __FILE__, __LINE__,__FUNCTION__);
1238
1239         new_camera = new_camera_head;
1240         
1241         while (new_camera != NULL) {    
1242         if (new_camera->dev.i2c_cam_info.addr == INVALID_VALUE) {
1243             WARN(1, 
1244                 KERN_ERR "%s(%d): new_camera[%d] i2c addr is invalidate!",
1245                 __FUNCTION__,__LINE__,i);
1246             continue;
1247         }
1248         sprintf(new_camera->dev_name,"%s_%d",new_camera->dev.device_info.dev.init_name,i+3);
1249         new_camera->dev.device_info.dev.init_name =(const char*)&new_camera->dev_name[0];
1250         new_camera->io.dev_name =(const char*)&new_camera->dev_name[0];
1251         if (new_camera->orientation == INVALID_VALUE) {
1252             if (strstr(new_camera->dev_name,"back")) {                     
1253                 new_camera->orientation = 90;
1254             } else {
1255                 new_camera->orientation = 270;
1256             }
1257         }
1258         /* ddl@rock-chips.com: v0.1.3 */
1259         if ((new_camera->fov_h <= 0) || (new_camera->fov_h>360))
1260             new_camera->fov_h = 100;
1261         
1262         if ((new_camera->fov_v <= 0) || (new_camera->fov_v>360))
1263             new_camera->fov_v = 100;        
1264
1265                 new_camera->dev.desc_info.subdev_desc.power = rk_sensor_power;
1266                 new_camera->dev.desc_info.subdev_desc.powerdown = rk_sensor_powerdown;
1267                 new_camera->dev.desc_info.host_desc.board_info =&new_camera->dev.i2c_cam_info; 
1268
1269         new_camera->dev.device_info.id = i+6;
1270                 new_camera->dev.device_info.dev.platform_data = &new_camera->dev.desc_info;
1271                 new_camera->dev.desc_info.subdev_desc.drv_priv = &rk_camera_platform_data;
1272
1273         platform_device_register(&(new_camera->dev.device_info));
1274                 i++;
1275         new_camera = new_camera->next_camera;
1276     }
1277         
1278                 return 0;
1279 }
1280 #include "../../../drivers/media/video/rk30_camera.c"