ASR_BASE

Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/marvell/linux/sound/soc/codecs/alc5616-headset.c b/marvell/linux/sound/soc/codecs/alc5616-headset.c
new file mode 100644
index 0000000..8dba4a4
--- /dev/null
+++ b/marvell/linux/sound/soc/codecs/alc5616-headset.c
@@ -0,0 +1,282 @@
+/*
+ *	sound/soc/codecs/88pm800_headset.c
+ *
+ *	headset & hook detect driver for pm800
+ *
+ *	Copyright (C) 2013, Marvell Corporation (zhouqiao@Marvell.com)
+ *	Author: Qiao Zhou <zhouqiao@marvell.com>
+ *
+ *	This program is free software; you can redistribute it and/or modify
+ *	it under the terms of the GNU General Public License version 2 as
+ *	published by the Free Software Foundation.
+ */
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/88pm80x.h>
+#include <linux/mfd/88pm8xxx-headset.h>
+#include <linux/regulator/consumer.h>
+#include <linux/workqueue.h>
+#include <linux/delay.h>
+#include <linux/uaccess.h>
+#include <linux/of.h>
+#include <linux/mutex.h>
+#include <linux/kernel.h>
+#include <linux/irq.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/88pm80x.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+#include <linux/of_device.h>
+#include <linux/uaccess.h>
+#include <linux/proc_fs.h>
+#include <linux/fs.h>
+#include <linux/debugfs.h>
+#include <sound/pcm.h>
+#include <sound/soc.h>
+#include <linux/gpio.h>
+//#include <plat/mfp.h>
+#include <linux/regulator/consumer.h>
+#include <linux/pinctrl/consumer.h>
+#include <linux/platform_device.h>
+#include <linux/mutex.h>
+#include <linux/of_gpio.h>
+#include <asm/io.h>                                                                                                                                                                  
+#include <linux/interrupt.h>
+
+
+
+static struct i2c_client  *g_alc5616_client = NULL;
+static struct device_node *g_alc5616_node = NULL;
+static int gpio_CODEC_IRQ = 0; //GPIO[1]
+static int irq_codec = 0;
+static int hs_status = 3;
+static int hs_plugged = 0;
+static int hs_withmic = 0;
+static int g_enable_headset_detection = 0;
+
+extern void enable_5616_MClock(void);
+extern void alc5616_get_headset_status(void);
+extern int alc5616_get_headset_plugged_status(void);
+extern int alc5616_get_headset_mic_status(void);
+extern void alc5616_Enable_Headsetdetection(void);
+extern void alc5616_Disable_Headsetdetection(void);
+
+struct alc5616_hs_info {
+    struct device *dev;
+    struct regmap *map;
+    struct regmap *map_gpadc;
+    struct regmap *map_codec;
+    struct regmap *map_test;
+    int chip_id;
+    int irq_headset;
+    int irq_hook;
+    struct work_struct work_release, work_init;
+    struct headset_switch_data *psw_data_headset;
+    struct snd_soc_jack *hs_jack, *hk_jack;
+
+    struct regulator *mic_bias;
+    int mic_bias_volt;
+    int headset_flag;
+    int ground_detect;
+    int hook_press_th;
+    int vol_up_press_th;
+    int vol_down_press_th;
+    int mic_det_th;
+    int press_release_th;
+    int hs_status, hk_status;
+    unsigned int hk_avg, hk_num;
+    u32 hook_count;
+    struct mutex hs_mutex;
+    struct timer_list hook_timer;
+    struct device *hsdetect_dev;
+    int headset_gpio_num;
+    int mic_gpadc_num;
+};
+
+static struct alc5616_hs_info *hs_info;
+
+static ssize_t hs_status_show(struct device *dev, struct device_attribute *attr,
+        char *buf)
+{
+    int s = 0;
+
+    s += sprintf(buf, "%d", hs_status);
+    return s;
+}
+static DEVICE_ATTR(hs_status, S_IRUGO, hs_status_show, NULL);
+
+static ssize_t enable_headset_detection_show(struct device *dev, struct device_attribute *attr,
+        char *buf)
+{
+    int s = 0;
+
+    s += sprintf(buf, "%d", g_enable_headset_detection);
+    return s;
+}
+
+static ssize_t enable_headset_detection_set(struct device *dev,
+        struct device_attribute *attr,
+        const char *buf, size_t count)
+{
+    int ret;
+
+    ret = kstrtoint(buf, 10, &g_enable_headset_detection);
+    if (ret)
+        return ret;
+
+    if (g_enable_headset_detection) {
+        alc5616_Enable_Headsetdetection();
+    }
+
+    return count;
+}
+static DEVICE_ATTR(enable_headset_detection, 0644, enable_headset_detection_show, enable_headset_detection_set);
+
+static int alc5616_headset_suspend(struct platform_device *pdev,
+        pm_message_t state)
+{
+    return 0;
+}
+
+static int alc5616_headset_resume(struct platform_device *pdev)
+{
+    return 0;
+}
+
+static irqreturn_t codec_irq_handler(int irq, void *dev_id)
+{
+    printk(KERN_INFO"%s/L%d.in alc5616_headset.\n", __FUNCTION__, __LINE__);
+
+    /* check the headset plug in/out */
+    alc5616_get_headset_status();
+
+    hs_plugged = alc5616_get_headset_plugged_status();
+
+    hs_withmic = alc5616_get_headset_mic_status();
+
+    /************************************************************************************* 
+     * Set correct audio device according to hs_status, keep identical with app.
+     * -----------------------------------------------------------------------------------
+     *
+     * hs_status=0, hk_status=0    ==>no headset
+     * hs_status=1, hk_status=0    ==>headphone(without mic)
+     * hs_status=3, hk_status=0    ==>headset(with mic)
+     *************************************************************************************/
+    if (0 == hs_plugged)
+    {
+        /* no headset */
+        hs_status = 0;
+    }
+    else if ((1 == hs_plugged) && (0 == hs_withmic))
+    {
+        /* headphone(without mic)*/ 
+        hs_status = 1;
+    }
+    else if ((1 == hs_plugged) && (1 == hs_withmic))
+    {
+        /* headset(with mic)*/ 
+        hs_status = 3;
+    }
+
+    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);
+
+    /* for telephony */
+    if (hs_status)
+        kobject_uevent(&hs_info->hsdetect_dev->kobj, KOBJ_ADD);
+    else
+        kobject_uevent(&hs_info->hsdetect_dev->kobj, KOBJ_REMOVE);
+
+    /* for telephony */
+    // kobject_uevent(&info->hsdetect_dev->kobj, KOBJ_ONLINE);
+    return IRQ_HANDLED;
+}
+
+static int alc5616_headset_probe(struct platform_device *pdev)
+{
+    int ret = -1;
+
+    printk(KERN_INFO"%s/L%d.enter alc5616_headset_probe.\n", __FUNCTION__, __LINE__);
+
+    if ((NULL == pdev) || (NULL == pdev->dev.platform_data)){                                                                                                        
+        printk(KERN_INFO"parameter is NULL!");
+        return -1;
+    }
+
+    g_alc5616_client = pdev->dev.platform_data;
+
+    g_alc5616_node = g_alc5616_client->dev.of_node;
+    if (NULL == g_alc5616_node){
+        printk(KERN_INFO"Please check codec input parameter for g_alc5616_node.\n");                                                                                                 
+        return 0;
+    }
+
+    /* CODEC_IRQ */
+    gpio_CODEC_IRQ = of_get_named_gpio(g_alc5616_node, "irq-gpio", 0); //GPIO[1]
+    printk(KERN_INFO"%s/L%d, gpio_CODEC_IRQ=%d.\n", __FUNCTION__, __LINE__, gpio_CODEC_IRQ);
+
+    if (gpio_CODEC_IRQ > 0) {
+        gpio_request(gpio_CODEC_IRQ, "CODEC_IRQ");
+        gpio_direction_input(gpio_CODEC_IRQ);
+
+        irq_codec = gpio_to_irq(gpio_CODEC_IRQ);
+        printk(KERN_INFO"%s/L%d, irq_codec=%d.\n", __FUNCTION__, __LINE__, irq_codec);
+
+        /* request irq */
+        ret = request_threaded_irq(irq_codec, NULL, codec_irq_handler,
+                IRQF_SHARED | IRQF_TRIGGER_RISING |
+                IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "alc5616-headset_detection",
+                g_alc5616_client);
+        if (ret < 0) {
+            printk(KERN_INFO"%s: request irq failed!\n",__FUNCTION__);
+        }
+    }
+
+    ret = device_create_file(&pdev->dev, &dev_attr_hs_status);
+    if (ret < 0) {
+        dev_err(&pdev->dev, "attr hs_status create fail: %d\n", ret);
+        return ret;
+    }
+
+    ret = device_create_file(&pdev->dev, &dev_attr_enable_headset_detection);
+    if (ret < 0) {
+        dev_err(&pdev->dev, "attr enable_headset_detection create fail: %d\n", ret);
+        return ret;
+    }
+
+    hs_info = devm_kzalloc(&pdev->dev, sizeof(struct alc5616_hs_info), GFP_KERNEL);
+    if (!hs_info)
+        return -ENOMEM;
+
+    hs_info->hsdetect_dev = &pdev->dev;
+    return 0;
+}
+
+static int alc5616_headset_remove(struct platform_device *pdev)
+{
+    gpio_free(gpio_CODEC_IRQ);
+    free_irq(irq_codec, g_alc5616_client);
+    return 0;
+}
+
+static void alc5616_headset_shutdown(struct platform_device *pdev)
+{
+    return;
+}
+
+static struct platform_driver alc5616_headset_driver = {
+    .probe = alc5616_headset_probe,
+    .remove = alc5616_headset_remove,
+    .shutdown = alc5616_headset_shutdown,
+    .suspend = alc5616_headset_suspend,
+    .resume = alc5616_headset_resume,
+    .driver = {
+        .name = "alc5616-headset",
+    },
+};
+
+module_platform_driver(alc5616_headset_driver);
+
+MODULE_DESCRIPTION("ASR ALC5616 Headset driver");
+MODULE_AUTHOR("wenchen@asrmicro.com");
+MODULE_LICENSE("GPL");