blob: 8dba4a48f06a0a439aedc18ee2253c0bb1e1ae95 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * sound/soc/codecs/88pm800_headset.c
3 *
4 * headset & hook detect driver for pm800
5 *
6 * Copyright (C) 2013, Marvell Corporation (zhouqiao@Marvell.com)
7 * Author: Qiao Zhou <zhouqiao@marvell.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13#include <linux/module.h>
14#include <linux/i2c.h>
15#include <linux/platform_device.h>
16#include <linux/mfd/88pm80x.h>
17#include <linux/mfd/88pm8xxx-headset.h>
18#include <linux/regulator/consumer.h>
19#include <linux/workqueue.h>
20#include <linux/delay.h>
21#include <linux/uaccess.h>
22#include <linux/of.h>
23#include <linux/mutex.h>
24#include <linux/kernel.h>
25#include <linux/irq.h>
26#include <linux/mfd/core.h>
27#include <linux/mfd/88pm80x.h>
28#include <linux/slab.h>
29#include <linux/delay.h>
30#include <linux/of_device.h>
31#include <linux/uaccess.h>
32#include <linux/proc_fs.h>
33#include <linux/fs.h>
34#include <linux/debugfs.h>
35#include <sound/pcm.h>
36#include <sound/soc.h>
37#include <linux/gpio.h>
38//#include <plat/mfp.h>
39#include <linux/regulator/consumer.h>
40#include <linux/pinctrl/consumer.h>
41#include <linux/platform_device.h>
42#include <linux/mutex.h>
43#include <linux/of_gpio.h>
44#include <asm/io.h>
45#include <linux/interrupt.h>
46
47
48
49static struct i2c_client *g_alc5616_client = NULL;
50static struct device_node *g_alc5616_node = NULL;
51static int gpio_CODEC_IRQ = 0; //GPIO[1]
52static int irq_codec = 0;
53static int hs_status = 3;
54static int hs_plugged = 0;
55static int hs_withmic = 0;
56static int g_enable_headset_detection = 0;
57
58extern void enable_5616_MClock(void);
59extern void alc5616_get_headset_status(void);
60extern int alc5616_get_headset_plugged_status(void);
61extern int alc5616_get_headset_mic_status(void);
62extern void alc5616_Enable_Headsetdetection(void);
63extern void alc5616_Disable_Headsetdetection(void);
64
65struct alc5616_hs_info {
66 struct device *dev;
67 struct regmap *map;
68 struct regmap *map_gpadc;
69 struct regmap *map_codec;
70 struct regmap *map_test;
71 int chip_id;
72 int irq_headset;
73 int irq_hook;
74 struct work_struct work_release, work_init;
75 struct headset_switch_data *psw_data_headset;
76 struct snd_soc_jack *hs_jack, *hk_jack;
77
78 struct regulator *mic_bias;
79 int mic_bias_volt;
80 int headset_flag;
81 int ground_detect;
82 int hook_press_th;
83 int vol_up_press_th;
84 int vol_down_press_th;
85 int mic_det_th;
86 int press_release_th;
87 int hs_status, hk_status;
88 unsigned int hk_avg, hk_num;
89 u32 hook_count;
90 struct mutex hs_mutex;
91 struct timer_list hook_timer;
92 struct device *hsdetect_dev;
93 int headset_gpio_num;
94 int mic_gpadc_num;
95};
96
97static struct alc5616_hs_info *hs_info;
98
99static ssize_t hs_status_show(struct device *dev, struct device_attribute *attr,
100 char *buf)
101{
102 int s = 0;
103
104 s += sprintf(buf, "%d", hs_status);
105 return s;
106}
107static DEVICE_ATTR(hs_status, S_IRUGO, hs_status_show, NULL);
108
109static ssize_t enable_headset_detection_show(struct device *dev, struct device_attribute *attr,
110 char *buf)
111{
112 int s = 0;
113
114 s += sprintf(buf, "%d", g_enable_headset_detection);
115 return s;
116}
117
118static ssize_t enable_headset_detection_set(struct device *dev,
119 struct device_attribute *attr,
120 const char *buf, size_t count)
121{
122 int ret;
123
124 ret = kstrtoint(buf, 10, &g_enable_headset_detection);
125 if (ret)
126 return ret;
127
128 if (g_enable_headset_detection) {
129 alc5616_Enable_Headsetdetection();
130 }
131
132 return count;
133}
134static DEVICE_ATTR(enable_headset_detection, 0644, enable_headset_detection_show, enable_headset_detection_set);
135
136static int alc5616_headset_suspend(struct platform_device *pdev,
137 pm_message_t state)
138{
139 return 0;
140}
141
142static int alc5616_headset_resume(struct platform_device *pdev)
143{
144 return 0;
145}
146
147static irqreturn_t codec_irq_handler(int irq, void *dev_id)
148{
149 printk(KERN_INFO"%s/L%d.in alc5616_headset.\n", __FUNCTION__, __LINE__);
150
151 /* check the headset plug in/out */
152 alc5616_get_headset_status();
153
154 hs_plugged = alc5616_get_headset_plugged_status();
155
156 hs_withmic = alc5616_get_headset_mic_status();
157
158 /*************************************************************************************
159 * Set correct audio device according to hs_status, keep identical with app.
160 * -----------------------------------------------------------------------------------
161 *
162 * hs_status=0, hk_status=0 ==>no headset
163 * hs_status=1, hk_status=0 ==>headphone(without mic)
164 * hs_status=3, hk_status=0 ==>headset(with mic)
165 *************************************************************************************/
166 if (0 == hs_plugged)
167 {
168 /* no headset */
169 hs_status = 0;
170 }
171 else if ((1 == hs_plugged) && (0 == hs_withmic))
172 {
173 /* headphone(without mic)*/
174 hs_status = 1;
175 }
176 else if ((1 == hs_plugged) && (1 == hs_withmic))
177 {
178 /* headset(with mic)*/
179 hs_status = 3;
180 }
181
182 printk(KERN_INFO"%s/L%d.in alc5616_headset.hs_plugged=%d, hs_withmic=%d, status=%d.\n", __FUNCTION__, __LINE__, hs_plugged, hs_withmic, hs_status);
183
184 /* for telephony */
185 if (hs_status)
186 kobject_uevent(&hs_info->hsdetect_dev->kobj, KOBJ_ADD);
187 else
188 kobject_uevent(&hs_info->hsdetect_dev->kobj, KOBJ_REMOVE);
189
190 /* for telephony */
191 // kobject_uevent(&info->hsdetect_dev->kobj, KOBJ_ONLINE);
192 return IRQ_HANDLED;
193}
194
195static int alc5616_headset_probe(struct platform_device *pdev)
196{
197 int ret = -1;
198
199 printk(KERN_INFO"%s/L%d.enter alc5616_headset_probe.\n", __FUNCTION__, __LINE__);
200
201 if ((NULL == pdev) || (NULL == pdev->dev.platform_data)){
202 printk(KERN_INFO"parameter is NULL!");
203 return -1;
204 }
205
206 g_alc5616_client = pdev->dev.platform_data;
207
208 g_alc5616_node = g_alc5616_client->dev.of_node;
209 if (NULL == g_alc5616_node){
210 printk(KERN_INFO"Please check codec input parameter for g_alc5616_node.\n");
211 return 0;
212 }
213
214 /* CODEC_IRQ */
215 gpio_CODEC_IRQ = of_get_named_gpio(g_alc5616_node, "irq-gpio", 0); //GPIO[1]
216 printk(KERN_INFO"%s/L%d, gpio_CODEC_IRQ=%d.\n", __FUNCTION__, __LINE__, gpio_CODEC_IRQ);
217
218 if (gpio_CODEC_IRQ > 0) {
219 gpio_request(gpio_CODEC_IRQ, "CODEC_IRQ");
220 gpio_direction_input(gpio_CODEC_IRQ);
221
222 irq_codec = gpio_to_irq(gpio_CODEC_IRQ);
223 printk(KERN_INFO"%s/L%d, irq_codec=%d.\n", __FUNCTION__, __LINE__, irq_codec);
224
225 /* request irq */
226 ret = request_threaded_irq(irq_codec, NULL, codec_irq_handler,
227 IRQF_SHARED | IRQF_TRIGGER_RISING |
228 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "alc5616-headset_detection",
229 g_alc5616_client);
230 if (ret < 0) {
231 printk(KERN_INFO"%s: request irq failed!\n",__FUNCTION__);
232 }
233 }
234
235 ret = device_create_file(&pdev->dev, &dev_attr_hs_status);
236 if (ret < 0) {
237 dev_err(&pdev->dev, "attr hs_status create fail: %d\n", ret);
238 return ret;
239 }
240
241 ret = device_create_file(&pdev->dev, &dev_attr_enable_headset_detection);
242 if (ret < 0) {
243 dev_err(&pdev->dev, "attr enable_headset_detection create fail: %d\n", ret);
244 return ret;
245 }
246
247 hs_info = devm_kzalloc(&pdev->dev, sizeof(struct alc5616_hs_info), GFP_KERNEL);
248 if (!hs_info)
249 return -ENOMEM;
250
251 hs_info->hsdetect_dev = &pdev->dev;
252 return 0;
253}
254
255static int alc5616_headset_remove(struct platform_device *pdev)
256{
257 gpio_free(gpio_CODEC_IRQ);
258 free_irq(irq_codec, g_alc5616_client);
259 return 0;
260}
261
262static void alc5616_headset_shutdown(struct platform_device *pdev)
263{
264 return;
265}
266
267static struct platform_driver alc5616_headset_driver = {
268 .probe = alc5616_headset_probe,
269 .remove = alc5616_headset_remove,
270 .shutdown = alc5616_headset_shutdown,
271 .suspend = alc5616_headset_suspend,
272 .resume = alc5616_headset_resume,
273 .driver = {
274 .name = "alc5616-headset",
275 },
276};
277
278module_platform_driver(alc5616_headset_driver);
279
280MODULE_DESCRIPTION("ASR ALC5616 Headset driver");
281MODULE_AUTHOR("wenchen@asrmicro.com");
282MODULE_LICENSE("GPL");