HDMI: modify callback and events for HDCP 2nd auth.
[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         if(pdata->chan != NULL)
127         {//hook adc mode
128                 printk("%s() headset have hook adc mode\n",__FUNCTION__);
129                 ret = rk_headset_adc_probe(pdev,pdata);
130                 if(ret < 0)
131                 {
132                         goto err;
133                 }       
134
135         }
136         else
137         {//hook interrupt mode and not hook
138                 printk("%s() headset have %s mode\n",__FUNCTION__,pdata->hook_gpio?"interrupt hook":"no hook");
139                 ret = rk_headset_probe(pdev,pdata);
140                 if(ret < 0)
141                 {
142                         goto err;
143                 }
144         }
145
146         return 0;
147 err:
148         kfree(pdata);
149         return ret;
150 }
151
152 static int rockchip_headset_remove(struct platform_device *pdev)
153 {
154         if(pdata_info)
155                 kfree(pdata_info);
156         return 0;
157 }
158
159 static int rockchip_headset_suspend(struct platform_device *pdev, pm_message_t state)
160 {
161         if(pdata_info->chan != 0)
162         {
163                 return rk_headset_adc_suspend(pdev,state);
164         }
165         return 0;
166 }
167
168 static int rockchip_headset_resume(struct platform_device *pdev)
169 {
170         if(pdata_info->chan != 0)
171         {
172                 return rk_headset_adc_resume(pdev);
173         }       
174         return 0;
175 }
176
177 static const struct of_device_id rockchip_headset_of_match[] = {
178         { .compatible = "rockchip_headset", },
179         {},
180 };
181 MODULE_DEVICE_TABLE(of, rockchip_headset_of_match);
182
183 static struct platform_driver rockchip_headset_driver = {
184         .probe  = rockchip_headset_probe,
185         .remove = rockchip_headset_remove,
186         .resume =       rockchip_headset_resume,        
187         .suspend =      rockchip_headset_suspend,       
188         .driver = {
189                 .name   = "rockchip_headset",
190                 .owner  = THIS_MODULE,
191                 .of_match_table = of_match_ptr(rockchip_headset_of_match),              
192         },
193 };
194
195 static int __init rockchip_headset_init(void)
196 {
197         platform_driver_register(&rockchip_headset_driver);
198         return 0;
199 }
200
201 static void __exit rockchip_headset_exit(void)
202 {
203         platform_driver_unregister(&rockchip_headset_driver);
204 }
205 late_initcall(rockchip_headset_init);
206 MODULE_DESCRIPTION("Rockchip Headset Core Driver");
207 MODULE_LICENSE("GPL");