drm: support ignore drm ioctl permission
[firefly-linux-kernel-4.4.55.git] / drivers / headset_observe / rockchip_headset_core.c
1 /* 
2  * Copyright (C) 2014 Rockchip Corporation.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/pm.h>
19 #include <linux/i2c.h>
20 #include <linux/spi/spi.h>
21 #include <linux/platform_device.h>
22 #include <linux/errno.h>
23 #include <linux/err.h>
24 #include <linux/debugfs.h>
25 #include "rk_headset.h"
26 #include <linux/of_gpio.h>
27 #include <linux/gpio.h>
28 #include <linux/iio/consumer.h>
29
30 /* Debug */
31 #if 0
32 #define DBG(x...) printk(x)
33 #else
34 #define DBG(x...) do { } while (0)
35 #endif
36
37 struct rk_headset_pdata *pdata_info;
38
39 static int rockchip_headset_probe(struct platform_device *pdev)
40 {
41         struct device_node *node = pdev->dev.of_node;
42         struct rk_headset_pdata *pdata;
43         int ret;
44         enum of_gpio_flags flags;
45
46         pdata = kzalloc(sizeof(struct rk_headset_pdata), GFP_KERNEL);
47         if (pdata == NULL) {
48                 printk("%s failed to allocate driver data\n",__FUNCTION__);
49                 return -ENOMEM;
50         }
51         memset(pdata,0,sizeof(struct rk_headset_pdata));
52         pdata_info = pdata;
53
54         //headset
55         ret = of_get_named_gpio_flags(node, "headset_gpio", 0, &flags);
56         if (ret < 0) {
57                 printk("%s() Can not read property headset_gpio\n", __FUNCTION__);
58                 goto err;
59         } else {
60                 pdata->headset_gpio = ret;
61                 ret = devm_gpio_request(&pdev->dev, pdata->headset_gpio, "headset_gpio");
62                 if(ret < 0){
63                         printk("%s() devm_gpio_request headset_gpio request ERROR\n", __FUNCTION__);
64                         goto err;
65                 }
66
67                 ret = gpio_direction_input(pdata->headset_gpio); 
68                 if(ret < 0){
69                         printk("%s() gpio_direction_input headset_gpio set ERROR\n", __FUNCTION__);
70                         goto err;
71                 }
72
73                 pdata->headset_insert_type = (flags & OF_GPIO_ACTIVE_LOW) ? HEADSET_IN_LOW : HEADSET_IN_HIGH;
74         }
75
76         //hook
77         ret = of_get_named_gpio_flags(node, "hook_gpio", 0, &pdata->hook_gpio);
78         if (ret < 0) {
79                 DBG("%s() Can not read property hook_gpio\n", __FUNCTION__);
80                 pdata->hook_gpio = 0;
81                 //adc mode
82                 pdata->chan = iio_channel_get(&pdev->dev, NULL);
83                 if (IS_ERR(pdata->chan))
84                 {
85                         pdata->chan = NULL;
86                         printk("%s() have not set adc chan\n", __FUNCTION__);
87                 }
88         } else {
89                 ret = of_property_read_u32(node, "hook_down_type", &pdata->hook_down_type);
90                 if (ret < 0) {
91                         DBG("%s() have not set hook_down_type,set >hook< insert type low level default\n", __FUNCTION__);
92                         pdata->hook_down_type = 0;
93                 }
94                 ret = devm_gpio_request(&pdev->dev, pdata->hook_gpio, "hook_gpio");
95                 if(ret < 0){
96                         printk("%s() devm_gpio_request hook_gpio request ERROR\n", __FUNCTION__);
97                         goto err;
98                 }
99                 ret = gpio_direction_input(pdata->hook_gpio); 
100                 if(ret < 0){
101                         printk("%s() gpio_direction_input hook_gpio set ERROR\n", __FUNCTION__);
102                         goto err;
103                 }
104         }
105
106         #ifdef CONFIG_MODEM_MIC_SWITCH
107         //mic
108         ret = of_get_named_gpio_flags(node, "mic_switch_gpio", 0, &flags);
109         if (ret < 0) {
110                 DBG("%s() Can not read property mic_switch_gpio\n", __FUNCTION__);
111         } else {
112                 pdata->headset_gpio = ret;
113                 ret = of_property_read_u32(node, "hp_mic_io_value", &pdata->hp_mic_io_value);
114                 if (ret < 0) {
115                         DBG("%s() have not set hp_mic_io_value ,so default set pull down low level\n", __FUNCTION__);
116                         pdata->hp_mic_io_value = 0;
117                 }
118                 ret = of_property_read_u32(node, "main_mic_io_value", &pdata->main_mic_io_value);
119                 if (ret < 0) {
120                         DBG("%s() have not set main_mic_io_value ,so default set pull down low level\n", __FUNCTION__);
121                         pdata->main_mic_io_value = 1;
122                 }
123         }
124         #endif
125
126         ret = of_property_read_u32(node, "rockchip,headset_wakeup", &pdata->headset_wakeup);
127         if (ret < 0)
128                 pdata->headset_wakeup = 1;
129
130         if(pdata->chan != NULL)
131         {//hook adc mode
132                 printk("%s() headset have hook adc mode\n",__FUNCTION__);
133                 ret = rk_headset_adc_probe(pdev,pdata);
134                 if(ret < 0)
135                 {
136                         goto err;
137                 }       
138
139         }
140         else
141         {//hook interrupt mode and not hook
142                 printk("%s() headset have %s mode\n",__FUNCTION__,pdata->hook_gpio?"interrupt hook":"no hook");
143                 ret = rk_headset_probe(pdev,pdata);
144                 if(ret < 0)
145                 {
146                         goto err;
147                 }
148         }
149
150         return 0;
151 err:
152         kfree(pdata);
153         return ret;
154 }
155
156 static int rockchip_headset_remove(struct platform_device *pdev)
157 {
158         if(pdata_info)
159                 kfree(pdata_info);
160         return 0;
161 }
162
163 static int rockchip_headset_suspend(struct platform_device *pdev, pm_message_t state)
164 {
165         if(pdata_info->chan != 0)
166         {
167                 return rk_headset_adc_suspend(pdev,state);
168         }
169         return 0;
170 }
171
172 static int rockchip_headset_resume(struct platform_device *pdev)
173 {
174         if(pdata_info->chan != 0)
175         {
176                 return rk_headset_adc_resume(pdev);
177         }       
178         return 0;
179 }
180
181 static const struct of_device_id rockchip_headset_of_match[] = {
182         { .compatible = "rockchip_headset", },
183         {},
184 };
185 MODULE_DEVICE_TABLE(of, rockchip_headset_of_match);
186
187 static struct platform_driver rockchip_headset_driver = {
188         .probe  = rockchip_headset_probe,
189         .remove = rockchip_headset_remove,
190         .resume =       rockchip_headset_resume,        
191         .suspend =      rockchip_headset_suspend,       
192         .driver = {
193                 .name   = "rockchip_headset",
194                 .owner  = THIS_MODULE,
195                 .of_match_table = of_match_ptr(rockchip_headset_of_match),              
196         },
197 };
198
199 static int __init rockchip_headset_init(void)
200 {
201         platform_driver_register(&rockchip_headset_driver);
202         return 0;
203 }
204
205 static void __exit rockchip_headset_exit(void)
206 {
207         platform_driver_unregister(&rockchip_headset_driver);
208 }
209 late_initcall(rockchip_headset_init);
210 MODULE_DESCRIPTION("Rockchip Headset Core Driver");
211 MODULE_LICENSE("GPL");