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