ARM64: dts: rk3399: adjust box temperature patameters
[firefly-linux-kernel-4.4.55.git] / drivers / misc / ac_usb_switch.c
1 #include <linux/input.h>\r
2 #include <linux/module.h>\r
3 #include <linux/init.h>\r
4 #include <linux/interrupt.h>\r
5 #include <linux/kernel.h>\r
6 #include <linux/fcntl.h>\r
7 #include <linux/delay.h>\r
8 #include <linux/device.h>\r
9 #include <linux/miscdevice.h>\r
10 #include <asm/types.h>\r
11 #include <mach/gpio.h>\r
12 #include <mach/iomux.h>\r
13 #include <linux/platform_device.h>\r
14 #include <asm/uaccess.h>\r
15 #include <linux/wait.h>\r
16 #include <mach/board.h>\r
17 \r
18 #if 1\r
19 #define DBG(x...)       printk(KERN_INFO x)\r
20 #else\r
21 #define DBG(x...)\r
22 #endif\r
23 #define PC_DISPLAY_MODE 2\r
24 #define ANDROID_DISPLAY_MODE   1\r
25 \r
26 #define USB_SWITCH_IOCTL_BASE 'u'\r
27 #define USB_SWITCH_IOCTL_SET_USB_SWITCH_MODE            _IOW(USB_SWITCH_IOCTL_BASE, 0x01, int)\r
28 #define USB_SWITCH_IOCTL_GET_PC_STATE           _IOR(USB_SWITCH_IOCTL_BASE, 0x02, int)\r
29 \r
30 static struct ac_usb_switch_platform_data *ac_usb_switch;\r
31 \r
32 \r
33 static int setUsbSwitchMode(int mode){\r
34         struct ac_usb_switch_platform_data *pdata = ac_usb_switch;\r
35         if(pdata->usb_switch_pin == INVALID_GPIO){\r
36                 DBG(" error ac_usb_switch_pin is null !!!\n");\r
37                 return -1;\r
38         }\r
39         switch(mode){\r
40                 case PC_DISPLAY_MODE:\r
41                         gpio_set_value(pdata->usb_switch_pin,GPIO_LOW);                 \r
42                         break;\r
43                 case ANDROID_DISPLAY_MODE:\r
44                         gpio_set_value(pdata->usb_switch_pin,GPIO_HIGH);\r
45                         break;\r
46                 default:\r
47                 break;\r
48         }\r
49         return 0;\r
50 }\r
51 static int getPCstate(){\r
52         return 1;\r
53 }\r
54 \r
55 static int ac_usb_switch_open(struct inode *inode, struct file *filp)\r
56 {\r
57     DBG("ac_usb_switch_open\n");\r
58 \r
59         return 0;\r
60 }\r
61 \r
62 static ssize_t ac_usb_switch_read(struct file *filp, char __user *ptr, size_t size, loff_t *pos)\r
63 {\r
64         if (ptr == NULL)\r
65                 printk("%s: user space address is NULL\n", __func__);\r
66         return sizeof(int);\r
67 }\r
68 \r
69 static long ac_usb_switch_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)\r
70 {\r
71         long ret = 0;   \r
72         int mode;\r
73         struct ac_usb_switch_platform_data *pdata = ac_usb_switch;\r
74 \r
75         DBG("ac_usb_switch_ioctl: cmd = %d arg = %ld\n",cmd, arg);\r
76         \r
77         switch (cmd){\r
78                 case USB_SWITCH_IOCTL_SET_USB_SWITCH_MODE:\r
79                         DBG("ac_usb_switch_ioctl: USB_SWITCH_IOCTL_SET_USB_SWITCH_MODE\n");\r
80                         mode = arg;\r
81                         setUsbSwitchMode(mode);\r
82                         break;\r
83                 case USB_SWITCH_IOCTL_GET_PC_STATE:\r
84                         DBG("ac_usb_switch_ioctl: USB_SWITCH_IOCTL_GET_PC_STATE\n");\r
85                         return getPCstate();\r
86                         \r
87                 default:\r
88                         printk("unknown ioctl cmd!\n");\r
89                         ret = -EINVAL;\r
90                         break;\r
91         }\r
92         return ret;\r
93 }\r
94 \r
95 static int ac_usb_switch_release(struct inode *inode, struct file *filp)\r
96 {\r
97     DBG("ac_usb_switch_release\n");\r
98     \r
99         return 0;\r
100 }\r
101 \r
102 static struct file_operations ac_usb_switch_fops = {\r
103         .owner   = THIS_MODULE,\r
104         .open    = ac_usb_switch_open,\r
105         .read    = ac_usb_switch_read,\r
106         .unlocked_ioctl   = ac_usb_switch_ioctl,\r
107         .release = ac_usb_switch_release,\r
108 };\r
109 \r
110 static struct miscdevice ac_usb_switch_dev = \r
111 {\r
112     .minor = MISC_DYNAMIC_MINOR,\r
113     .name = "ac_usb_switch",\r
114     .fops = &ac_usb_switch_fops,\r
115 };\r
116 \r
117 static int ac_usb_switch_probe(struct platform_device *pdev)\r
118 {\r
119         int ret = 0;\r
120         int result;\r
121         struct ac_usb_switch_platform_data *pdata = pdev->dev.platform_data;\r
122         if(!pdata)\r
123                 return -1;\r
124         ac_usb_switch = pdata;\r
125         ret = misc_register(&ac_usb_switch_dev);\r
126         if (ret < 0){\r
127                 printk("ac_usb_switch register err!\n");\r
128                 return ret;\r
129         }\r
130         \r
131         if(pdata->usb_switch_pin != INVALID_GPIO){\r
132                 result = gpio_request(pdata->usb_switch_pin, "ac_usb_switch");\r
133                 if(result)\r
134                 {\r
135                         printk("%s:fail to request gpio %d\n",__func__, pdata->usb_switch_pin);\r
136                         //return -1;\r
137                 }else{\r
138                         gpio_direction_output(pdata->usb_switch_pin,GPIO_HIGH);\r
139                 }\r
140         }\r
141 \r
142         if(pdata->pc_state_pin != INVALID_GPIO){\r
143                 result = gpio_request(pdata->pc_state_pin, "ac_usb_switch");\r
144                 if(result)\r
145                 {\r
146                         printk("%s:fail to request gpio %d\n",__func__, pdata->pc_state_pin);\r
147                         //return -1;\r
148                 }else{\r
149                         gpio_direction_input(pdata->pc_state_pin);\r
150                 }\r
151         }\r
152         printk("%s: ok ...... \n", __func__);\r
153 \r
154         return ret;\r
155 }\r
156 \r
157 static int ac_usb_switch_suspend(struct platform_device *pdev,  pm_message_t state)\r
158 {\r
159         struct ac_usb_switch_platform_data *pdata = pdev->dev.platform_data;\r
160 \r
161         if(!pdata) {\r
162                 printk("%s: pdata = NULL ...... \n", __func__);\r
163                 return -1;\r
164         }\r
165         printk("%s\n",__FUNCTION__);\r
166         return 0;       \r
167 }\r
168 \r
169 static int ac_usb_switch_resume(struct platform_device *pdev)\r
170 {\r
171         struct ac_usb_switch_platform_data *pdata = pdev->dev.platform_data;\r
172 \r
173         if(!pdata) {\r
174                 printk("%s: pdata = NULL ...... \n", __func__);\r
175                 return -1;\r
176         }\r
177         printk("%s\n",__FUNCTION__);\r
178         return 0;\r
179 }\r
180 \r
181 static int ac_usb_switch_remove(struct platform_device *pdev)\r
182 {\r
183         struct ac_usb_switch_platform_data *pdata = pdev->dev.platform_data;\r
184         if(!pdata)\r
185                 return -1;\r
186 \r
187         misc_deregister(&ac_usb_switch_dev);\r
188 \r
189         return 0;\r
190 }\r
191 \r
192 static struct platform_driver ac_usb_switch_driver = {\r
193         .probe  = ac_usb_switch_probe,\r
194         .remove = ac_usb_switch_remove,\r
195         .suspend        = ac_usb_switch_suspend,\r
196         .resume         = ac_usb_switch_resume,\r
197         .driver = {\r
198                 .name   = "ac_usb_switch",\r
199                 .owner  = THIS_MODULE,\r
200         },\r
201 };\r
202 \r
203 static int __init ac_usb_switch_init(void)\r
204 {\r
205         return platform_driver_register(&ac_usb_switch_driver);\r
206 }\r
207 \r
208 static void __exit ac_usb_switch_exit(void)\r
209 {\r
210         platform_driver_unregister(&ac_usb_switch_driver);\r
211 }\r
212 \r
213 module_init(ac_usb_switch_init);\r
214 module_exit(ac_usb_switch_exit);\r
215 MODULE_DESCRIPTION ("ac usb switch driver");\r
216 MODULE_LICENSE("GPL");\r
217 \r