update rkwifi driver
[firefly-linux-kernel-4.4.55.git] / drivers / misc / ste.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/i2c.h>
4 #include <linux/irq.h>
5 #include <linux/gpio.h>
6 #include <linux/input.h>
7 #include <linux/platform_device.h>
8 #include <linux/ste.h>
9 #include <linux/fs.h>
10 #include <linux/uaccess.h>
11 #include <linux/miscdevice.h>
12 #include <linux/circ_buf.h>
13 #include <linux/interrupt.h>
14 #include <mach/spi_fpga.h>
15 #include <linux/delay.h>
16
17 #if 1
18 #define D(x...) printk(x)
19 #else
20 #define D(x...)
21 #endif
22
23 static int misc_opened;
24
25 #define AP_TD_UNDEFINED_GBIN5           FPGA_PIO2_02
26 #define AP_RESET_TD                             FPGA_PIO2_04
27 #define AP_SHUTDOWN_TD_PMU              FPGA_PIO2_05
28 #define AP_PW_EN_TD                             FPGA_PIO2_03
29
30 #define PIN_BPSEND_ACK                          RK2818_PIN_PE0
31 #define PIN_APSEND_ACK                          RK2818_PIN_PF7
32
33 static int bp_power_on(void)
34 {
35         int ret=0;
36         
37         ret = gpio_request(AP_TD_UNDEFINED_GBIN5, NULL);
38         if (ret) {
39                 printk("%s:failed to request fpga s %d\n",__FUNCTION__,__LINE__);
40                 goto err;
41         }
42         ret = gpio_request(AP_RESET_TD, NULL);
43         if (ret) {
44                 printk("%s:failed to request fpga s %d\n",__FUNCTION__,__LINE__);
45                 goto err0;
46         }
47         
48
49         ret = gpio_request(AP_SHUTDOWN_TD_PMU, NULL);
50         if (ret) {
51                 printk("%s:failed to request fpga %d\n",__FUNCTION__,__LINE__);
52                 goto err1;
53         }
54
55         ret = gpio_request(AP_PW_EN_TD, NULL);
56         if (ret) {
57                 printk("%s:failed to request fpga  %d\n",__FUNCTION__,__LINE__);
58                 goto err2;
59         }
60
61         gpio_set_value(AP_TD_UNDEFINED_GBIN5, 1);
62        gpio_direction_output(AP_TD_UNDEFINED_GBIN5, 1);   
63         gpio_direction_input(AP_RESET_TD);
64
65          gpio_set_value(AP_SHUTDOWN_TD_PMU, 0);
66         gpio_direction_output(AP_SHUTDOWN_TD_PMU, 0);  
67
68         gpio_set_value(AP_PW_EN_TD, 0);
69         gpio_direction_output(AP_PW_EN_TD, 0);  
70         mdelay(1);
71         gpio_set_value(AP_PW_EN_TD, 1);
72         mdelay(1200);
73         gpio_set_value(AP_PW_EN_TD, 0);
74
75         return true;
76 err2:
77         gpio_free(AP_SHUTDOWN_TD_PMU);
78 err1:
79         gpio_free(AP_RESET_TD);
80 err0:
81         gpio_free(AP_TD_UNDEFINED_GBIN5);
82 err:    
83         return false;
84 }
85
86
87
88 static int bp_power_off(void)
89 {
90         D("+++--++++++%s_________ \r\n",__FUNCTION__);
91
92          gpio_set_value(AP_TD_UNDEFINED_GBIN5, 0);
93         
94         gpio_set_value(AP_PW_EN_TD, 0);
95         //gpio_direction_output(AP_PW_EN_TD, 0);  
96         mdelay(1);
97         gpio_set_value(AP_PW_EN_TD, 1);
98         mdelay(1200);
99         gpio_set_value(AP_PW_EN_TD, 0);
100
101         mdelay(5000);
102          gpio_set_value(AP_SHUTDOWN_TD_PMU, 1);
103         mdelay(1200);
104         // gpio_free(AP_PW_EN_TD);
105         D("++++--+++++%s   ok_________\r\n",__FUNCTION__);
106          return 0;
107 }
108 //add end
109
110 static int ste_open(struct inode *inode, struct file *file)
111 {
112         D("%s\n", __func__);
113         if (misc_opened)
114                 return -EBUSY;
115         misc_opened = 1;
116         return 0;
117 }
118
119 static int ste_release(struct inode *inode, struct file *file)
120 {
121         D("%s\n", __func__);
122         misc_opened = 0;
123         return 0;
124 }
125
126 static int ste_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg)
127 {
128         int val;
129         D("%s cmd %d\n", __func__, _IOC_NR(cmd));
130         switch (cmd) {
131         case STE_IOCTL_EN_APSEND_ACK:
132                 D("%s:STE_IOCTL_EN_APSEND_ACK\n");
133                 gpio_direction_output(PIN_APSEND_ACK,GPIO_LOW);
134                 msleep(50); 
135                 gpio_direction_output(PIN_APSEND_ACK,GPIO_HIGH);
136                 msleep(50); 
137                 break;
138         case STE_IOCTL_GET_ACK:
139                 val = gpio_get_value(PIN_BPSEND_ACK);
140                 D("%s:STE_IOCTL_GET_ACK pin status is %d\n",__func__,val);
141                 return put_user(val, (unsigned long __user *)arg);
142                 break;
143         case STE_IOCTL_POWER_ON:
144                 D("%s:STE_IOCTL_POWER_ON\n",__func__);
145                 bp_power_on();
146                 break;
147         case STE_IOCTL_POWER_OFF:
148                 D("%s:STE_IOCTL_POWER_OFF\n",__func__);
149                 bp_power_off();
150                 break;
151         default:
152                 pr_err("%s: invalid cmd %d\n", __func__, _IOC_NR(cmd));
153                 return -EINVAL;
154         }
155         return 0;
156 }
157
158 static struct file_operations ste_fops = {
159         .owner = THIS_MODULE,
160         .open = ste_open,
161         .release = ste_release,
162         .ioctl = ste_ioctl
163 };
164
165 static struct miscdevice ste_misc = {
166         .minor = MISC_DYNAMIC_MINOR,
167         .name = STE_NAME,
168         .fops = &ste_fops
169 };
170
171 static int ste_probe(struct platform_device *pdev)
172 {
173         int rc = -EIO;
174         D("%s-----------\n",__FUNCTION__);
175
176         rc = misc_register(&ste_misc);
177         if (rc < 0) {
178                 pr_err("%s: could not register misc device\n", __func__);
179         //      goto err_unregister_input_device;
180         }
181         return rc;
182 }
183
184 static struct platform_driver ste_driver = {
185         .probe = ste_probe,
186         .driver = {
187                 .name = "ste",
188                 .owner = THIS_MODULE
189         },
190 };
191
192 static int __init ste_init(void)
193 {
194         return platform_driver_register(&ste_driver);
195 }
196
197 static void __exit ste_exit(void)
198 {
199         platform_driver_unregister(&ste_driver);
200 }
201
202 module_init(ste_init);
203 module_exit(ste_exit);