Advertisement
motafoca

HeadSet.c

Apr 29th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.36 KB | None | 0 0
  1. /*
  2. * H2W device detection driver.
  3. *
  4. * Copyright (C) 2008 SAMSUNG Corporation.
  5. * Copyright (C) 2008 Google, Inc.
  6. *
  7. * Authors:
  8. * Laurence Chen <Laurence_Chen@htc.com>
  9. * Nick Pelly <npelly@google.com>
  10. *
  11. * This software is licensed under the terms of the GNU General Public
  12. * License version 2, as published by the Free Software Foundation, and
  13. * may be copied, distributed, and modified under those terms.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. */
  21.  
  22. /* For detecting SAMSUNG 2 Wire devices, such as wired headset.
  23.  
  24. Logically, the H2W driver is always present, and H2W state (hi->state)
  25. indicates what is currently plugged into the H2W interface.
  26.  
  27. When the headset is plugged in, CABLE_IN1 is pulled low. When the headset
  28. button is pressed, CABLE_IN2 is pulled low. These two lines are shared with
  29. the TX and RX (respectively) of UART3 - used for serial debugging.
  30.  
  31. This headset driver keeps the CPLD configured as UART3 for as long as
  32. possible, so that we can do serial FIQ debugging even when the kernel is
  33. locked and this driver no longer runs. So it only configures the CPLD to
  34. GPIO while the headset is plugged in, and for 10ms during detection work.
  35.  
  36. Unfortunately we can't leave the CPLD as UART3 while a headset is plugged
  37. in, UART3 is pullup on TX but the headset is pull-down, causing a 55 mA
  38. drain on bigfoot.
  39.  
  40. The headset detection work involves setting CPLD to GPIO, and then pulling
  41. CABLE_IN1 high with a stronger pullup than usual. A H2W headset will still
  42. pull this line low, whereas other attachments such as a serial console
  43. would get pulled up by this stronger pullup.
  44.  
  45. Headset insertion/removal causes UEvent's to be sent, and
  46. /sys/class/switch/h2w/state to be updated.
  47.  
  48. Button presses are interpreted as input event (KEY_MEDIA). Button presses
  49. are ignored if the headset is plugged in, so the buttons on 11 pin -> 3.5mm
  50. jack adapters do not work until a headset is plugged into the adapter. This
  51. is to avoid serial RX traffic causing spurious button press events.
  52.  
  53. We tend to check the status of CABLE_IN1 a few more times than strictly
  54. necessary during headset detection, to avoid spurious headset insertion
  55. events caused by serial debugger TX traffic.
  56. */
  57.  
  58. #include <linux/module.h>
  59. #include <linux/sysdev.h>
  60. #include <linux/fs.h>
  61. #include <linux/interrupt.h>
  62. #include <linux/workqueue.h>
  63. #include <linux/irq.h>
  64. #include <linux/delay.h>
  65. #include <linux/types.h>
  66. #include <linux/input.h>
  67. #include <linux/platform_device.h>
  68. #include <linux/mutex.h>
  69. #include <linux/errno.h>
  70. #include <linux/err.h>
  71. #include <linux/hrtimer.h>
  72. #include <linux/switch.h>
  73. #include <linux/input.h>
  74. #include <linux/debugfs.h>
  75. #include <asm/gpio.h>
  76. #include <asm/atomic.h>
  77. #include <mach/board.h>
  78. #include <mach/vreg.h>
  79. #include <mach/pmic.h>
  80.  
  81. #include <linux/wakelock.h>
  82.  
  83. #include <mach/hardware.h>
  84.  
  85. #define FEATURE_SENDEND_ENABLE
  86.  
  87. #if defined(CONFIG_MACH_CALLISTO) || defined(CONFIG_MACH_CRONIN)
  88. #define JACK_ACTIVE_LOW
  89.  
  90. #define GPIO_JACK_S_35 28
  91. #define GPIO_SEND_END 29
  92.  
  93. #elif defined(CONFIG_MACH_EUROPA)
  94.  
  95. #if (CONFIG_BOARD_REVISION == 1)
  96.  
  97. #define GPIO_JACK_S_35 92
  98. #define GPIO_SEND_END 16 // for volrans rev0.3, SEND_END isn't GPIO. it should be modified
  99.  
  100. #else /* CONFIG_BOARD_REVOSION > 1 */
  101.  
  102. #define GPIO_JACK_S_35 28
  103. #define GPIO_SEND_END 29
  104.  
  105. #endif /* CONDIG_BOARD_REVISION */
  106. #endif /* CONFIG_MACH_EUROPA */
  107.  
  108. /******************* define FEATURES ***************************/
  109. #define FEATURE_SND_RPC
  110. #define FEATURE_SND_RPC_TEMP
  111. #define FEATURE_3PI_ADC
  112. /****************************************************************/
  113.  
  114. #ifdef FEATURE_SND_RPC
  115. #include <mach/msm_rpcrouter.h>
  116. #define SND_RPC_PROG 0x30000002
  117. #define SND_RPC_VERS 0x00020001
  118. #define SND_MUTE_UNMUTED 0
  119. #define SND_MUTE_MUTED 1
  120. #define SND_DEVICE_HEADSET 3
  121. #define SND_DEVICE_FORCE_SPEAKER 26
  122. #define SND_SET_DEVICE_PROC 2
  123. #define SND_SET_EXTAMP_PROC 100
  124. int flag_boot = 1;
  125. int suspend_state = 0;
  126. int resume_state = 0;
  127. int suspend_count = 0;
  128. #endif
  129.  
  130. #ifdef FEATURE_3PI_ADC
  131. #include "proc_comm.h"
  132. #define SMEM_PROC_COMM_GET_ADC PCOM_OEM_SAMSUNG_GET_ADC
  133. #define SMEM_PROC_COMM_MICBIAS_ONOFF PCOM_OEM_MICBIAS_ONOFF
  134. // SMEM_PROC_COMM_GET_ADC data 1
  135. enum {
  136. SMEM_PROC_COMM_GET_ADC_BATTERY = 0x0,
  137. SMEM_PROC_COMM_GET_ADC_TEMP,
  138. SMEM_PROC_COMM_GET_ADC_VF,
  139. SMEM_PROC_COMM_GET_ADC_ALL, // data1 : VF(MSB 2 bytes) vbatt_adc(LSB 2bytes), data2 : temp_adc
  140. SMEM_PROC_COMM_GET_ADC_EAR_ADC, // 3PI_ADC
  141. SMEM_PROC_COMM_GET_ADC_MAX,
  142. };
  143.  
  144. enum {
  145. SMEM_PROC_COMM_MICBIAS_CONTROL_OFF = 0x0,
  146. SMEM_PROC_COMM_MICBIAS_CONTROL_ON,
  147. SMEM_PROC_COMM_MICBIAS_CONTROL_MAX
  148. };
  149.  
  150.  
  151. int get_3pi_adc(void)
  152. {
  153. int res = 0;
  154. int data1 = SMEM_PROC_COMM_GET_ADC_EAR_ADC;
  155. int data2 = 0;
  156.  
  157. res = msm_proc_comm(SMEM_PROC_COMM_GET_ADC, &data1, &data2);
  158. if(res < 0)
  159. {
  160. printk(KERN_ERR "[H2W] %s :%d fail %d\n", __func__, data1, res);
  161. return res;
  162. }
  163.  
  164. return data1;
  165. }
  166. #endif
  167.  
  168. //#define CVT_FROM_MS(val) (val*1000000)
  169. //#define BTN_PRESS_DEBOUNCE_TIME CVT_FROM_MS(150)
  170.  
  171. #define CONFIG_DEBUG_H2W
  172.  
  173. #ifdef CONFIG_DEBUG_H2W
  174. #define H2W_DBG(fmt, arg...) printk(KERN_INFO "[H2W] %s " fmt "\r\n", __func__, ## arg)
  175. #else
  176. #define H2W_DBG(fmt, arg...) do {} while (0)
  177. #endif
  178.  
  179. static struct wake_lock headset_delayed_work_wake_lock;
  180. extern unsigned int on_call;
  181.  
  182. static struct workqueue_struct *g_detection_work_queue;
  183. static void detection_work(struct work_struct *work);
  184. static DECLARE_WORK(g_detection_work, detection_work);
  185.  
  186. #if 1 // for DFMS test
  187. static struct workqueue_struct *g_earbutton_work_queue;
  188. static void earbutton_work(struct work_struct *work);
  189. static DECLARE_WORK(g_earbutton_work, earbutton_work);
  190. static unsigned int earbutton_pressed = 0;
  191. #endif
  192.  
  193. #define BIT_HEADSET (1 << 0)
  194. #define BIT_HEADSET_NO_MIC (1 << 1)
  195.  
  196. static void earbutton_func_work(struct work_struct *work);
  197. static DECLARE_WORK(g_earbutton_func_work, earbutton_func_work);
  198.  
  199.  
  200. enum {
  201. H2W_NO_DEVICE = 0,
  202. H2W_SEC_HEADSET = 1,
  203. H2W_NORMAL_HEADSET = 2,
  204. };
  205.  
  206. struct h2w_info {
  207. struct switch_dev sdev;
  208. struct switch_dev button_sdev; // for DFMS test
  209.  
  210. struct input_dev *input;
  211. struct mutex mutex_lock;
  212.  
  213. atomic_t btn_state;
  214. int ignore_btn;
  215.  
  216. unsigned int irq;
  217. unsigned int irq_btn;
  218. int btn_11pin_35mm_flag;
  219.  
  220. struct hrtimer timer;
  221. ktime_t debounce_time;
  222. int headset_state;
  223.  
  224. struct hrtimer btn_timer;
  225. ktime_t btn_debounce_time;
  226. int button_state;
  227.  
  228. unsigned int use_irq : 1;
  229. };
  230. static struct h2w_info *hi;
  231.  
  232. static int h2w_suspend(struct platform_device * device, pm_message_t state)
  233. {
  234. ++suspend_count;
  235. suspend_state = gpio_get_value(GPIO_JACK_S_35);
  236. H2W_DBG("suspend_state = %s", suspend_state?"connected":"disconnected");
  237. return 0;
  238. }
  239. static int h2w_resume(struct platform_device * device)
  240. {
  241. --suspend_count;
  242. resume_state = gpio_get_value(GPIO_JACK_S_35);
  243. H2W_DBG("resume_state = %s", resume_state?"connected":"disconnected");
  244. return 0;
  245. }
  246.  
  247. static ssize_t h2w_print_name(struct switch_dev *sdev, char *buf)
  248. {
  249. switch (switch_get_state(&hi->sdev)) {
  250. case H2W_NO_DEVICE:
  251. return sprintf(buf, "No Device\n");
  252. case H2W_SEC_HEADSET:
  253. return sprintf(buf, "H2W_SEC_HEADSET\n");
  254. case H2W_NORMAL_HEADSET:
  255. return sprintf(buf, "H2W_SEC_HEADSET\n");
  256. }
  257. return -EINVAL;
  258. }
  259. static ssize_t h2w_set_state(struct switch_dev *sdev, char *buf)
  260. {
  261. int state = *buf - 48;
  262. switch(state){
  263. case H2W_NO_DEVICE:
  264. switch_set_state(&hi->sdev, H2W_NO_DEVICE);
  265. return 0;
  266. case H2W_SEC_HEADSET:
  267. switch_set_state(&hi->sdev, H2W_SEC_HEADSET);
  268. return 0;
  269. case H2W_NORMAL_HEADSET:
  270. switch_set_state(&hi->sdev, H2W_SEC_HEADSET);
  271. return 0;
  272. default:
  273. break;
  274. }
  275.  
  276. return -EINVAL;
  277. }
  278.  
  279.  
  280. #if 1 // for DFMS test
  281. static ssize_t button_print_name(struct switch_dev *sdev, char *buf)
  282. {
  283. switch (switch_get_state(&hi->button_sdev)) {
  284. case 0:
  285. return sprintf(buf, "No Input\n");
  286. case 1:
  287. return sprintf(buf, "Button Pressed\n");
  288. }
  289. return -EINVAL;
  290. }
  291. #endif
  292.  
  293. static void button_pressed(void)
  294. {
  295. mdelay(50);
  296. #ifdef JACK_ACTIVE_LOW
  297. if(gpio_get_value(GPIO_JACK_S_35))
  298. #else
  299. if(!gpio_get_value(GPIO_JACK_S_35))
  300. #endif
  301. {
  302. H2W_DBG("ignored cuz headset was removed");
  303. return;
  304. }
  305. H2W_DBG("");
  306. atomic_set(&hi->btn_state, 1);
  307. input_report_key(hi->input, KEY_MEDIA, 1);
  308. input_sync(hi->input);
  309.  
  310. earbutton_pressed = 1;
  311. // for DFMS test
  312. queue_work(g_earbutton_work_queue, &g_earbutton_work);
  313. }
  314.  
  315. static void button_released(void)
  316. {
  317. H2W_DBG("");
  318. atomic_set(&hi->btn_state, 0);
  319. input_report_key(hi->input, KEY_MEDIA, 0);
  320. input_sync(hi->input);
  321.  
  322. earbutton_pressed = 0;
  323. // for DFMS test
  324. queue_work(g_earbutton_work_queue, &g_earbutton_work);
  325. }
  326.  
  327. #ifdef FEATURE_SENDEND_ENABLE
  328. static void remove_headset(void)
  329. {
  330. int res;
  331. int data1;
  332. int data2;
  333.  
  334. unsigned long irq_flags;
  335.  
  336. mutex_lock(&hi->mutex_lock);
  337. int tmp = switch_get_state(&hi->sdev) & ~(BIT_HEADSET | BIT_HEADSET_NO_MIC);
  338. switch_set_state(&hi->sdev, tmp == 2 ? 1 : tmp);
  339. mutex_unlock(&hi->mutex_lock);
  340. // for headset detection sound
  341. wake_lock_timeout(&headset_delayed_work_wake_lock, 5*HZ);
  342.  
  343. #ifndef FEATURE_SENDEND_ENABLE
  344. set_irq_wake(hi->irq_btn, 0);
  345. /* Disable button */
  346. local_irq_save(irq_flags);
  347. disable_irq(hi->irq_btn);
  348. local_irq_restore(irq_flags);
  349. // mic_en(0);
  350.  
  351. if (atomic_read(&hi->btn_state))
  352. button_released();
  353. #else
  354. if (hi->btn_11pin_35mm_flag) {
  355. set_irq_wake(hi->irq_btn, 0);
  356. local_irq_save(irq_flags);
  357. disable_irq(hi->irq_btn);
  358. local_irq_restore(irq_flags);
  359. // imsi turn_mic_bias_on(0);
  360. //pmic_mic_en(0);
  361. hi->btn_11pin_35mm_flag = 0;
  362. if (atomic_read(&hi->btn_state))
  363. button_released();
  364. }
  365. #endif
  366.  
  367. res = 0;
  368. data1 = SMEM_PROC_COMM_MICBIAS_CONTROL_OFF;
  369. data2 = 0;
  370.  
  371. res = msm_proc_comm(SMEM_PROC_COMM_MICBIAS_ONOFF, &data1, &data2);
  372. if(res < 0)
  373. {
  374. printk(KERN_ERR "[H2W] %s :%d fail %d\n", __func__, data1, res);
  375. }
  376.  
  377. hi->debounce_time = ktime_set(0, 20000000); /* 20 ms */
  378. }
  379. #else
  380. static void remove_headset(void)
  381.  
  382. {
  383. unsigned long irq_flags;
  384.  
  385. H2W_DBG("");
  386.  
  387. switch_set_state(&hi->sdev, H2W_NO_DEVICE);
  388. #if 0
  389. /* Disable button */
  390. local_irq_save(irq_flags);
  391. disable_irq(hi->irq_btn);
  392. local_irq_restore(irq_flags);
  393. #endif
  394. if (atomic_read(&hi->btn_state))
  395. button_released();
  396.  
  397. hi->debounce_time = ktime_set(0, 20000000); /* 20 ms */
  398. }
  399.  
  400. #endif
  401.  
  402. #ifdef FEATURE_SND_RPC
  403. static int mic_bias_on(void)
  404. {
  405. int rc;
  406. struct msm_rpc_endpoint *ep = NULL;
  407. uint32_t device = SND_DEVICE_HEADSET;
  408. uint32_t ear_mute = SND_MUTE_UNMUTED;
  409. uint32_t mic_mute = SND_MUTE_UNMUTED;
  410. struct rpc_snd_set_device_args {
  411. uint32_t device;
  412. uint32_t ear_mute;
  413. uint32_t mic_mute;
  414. uint32_t cb_func;
  415. uint32_t client_data;
  416. };
  417. struct snd_set_device_msg {
  418. struct rpc_request_hdr hdr;
  419. struct rpc_snd_set_device_args args;
  420. };
  421. struct snd_set_device_msg dmsg;
  422.  
  423. struct rpc_snd_set_extamp_args {
  424. uint32_t device;
  425. uint32_t speaker_volume;
  426. uint32_t headset_volume;
  427.  
  428. uint32_t cb_func;
  429. uint32_t client_data;
  430. };
  431. struct snd_set_extamp_msg {
  432. struct rpc_request_hdr hdr;
  433. struct rpc_snd_set_extamp_args args;
  434. };
  435. struct snd_set_extamp_msg emsg;
  436. /* int data1 = 0;
  437. * int data2 = 0; */
  438.  
  439. ep = msm_rpc_connect_compatible(SND_RPC_PROG, SND_RPC_VERS, 0);
  440. if(ep == NULL)
  441. {
  442. printk("snd_rpc_prog endpoint is NULL\n");
  443. return -ENODEV;
  444. }
  445. else if (IS_ERR(ep))
  446. {
  447. rc = PTR_ERR(ep);
  448. printk("%s: rpc connect failed for SND_RPC_PROG. rc = %d\n", __func__, rc);
  449. ep = NULL;
  450. return rc;
  451. }
  452.  
  453. emsg.args.device = cpu_to_be32(device);
  454. emsg.args.speaker_volume = cpu_to_be32(ear_mute);
  455. emsg.args.headset_volume = cpu_to_be32(mic_mute);
  456. emsg.args.cb_func = -1;
  457. emsg.args.client_data = 0;
  458. rc = msm_rpc_call(ep, SND_SET_EXTAMP_PROC, &emsg, sizeof(emsg), 5 * HZ);
  459. if (rc < 0) {
  460. printk(KERN_ERR
  461. "%s: msm_rpc_call_reply failed! proc = %d rc = %d\n",
  462. __func__, SND_SET_DEVICE_PROC, rc);
  463. return rc;
  464. }
  465.  
  466. msleep(10);
  467.  
  468. H2W_DBG("%d %d %d", device, ear_mute, mic_mute);
  469. dmsg.args.device = cpu_to_be32(device);
  470. dmsg.args.ear_mute = cpu_to_be32(ear_mute);
  471. dmsg.args.mic_mute = cpu_to_be32(mic_mute);
  472. dmsg.args.cb_func = -1;
  473. dmsg.args.client_data = 0;
  474. rc = msm_rpc_call(ep, SND_SET_DEVICE_PROC, &dmsg, sizeof(dmsg), 5 * HZ);
  475. if (rc < 0) {
  476. printk(KERN_ERR
  477. "%s: msm_rpc_call_reply failed! proc = %d rc = %d\n",
  478. __func__, SND_SET_DEVICE_PROC, rc);
  479. return rc;
  480. }
  481.  
  482. msleep(10);
  483.  
  484. ear_mute = SND_MUTE_MUTED;
  485. mic_mute = SND_MUTE_MUTED;
  486. H2W_DBG("%d %d %d", device, ear_mute, mic_mute);
  487. dmsg.args.ear_mute = cpu_to_be32(ear_mute);
  488. dmsg.args.mic_mute = cpu_to_be32(mic_mute);
  489. rc = msm_rpc_call(ep, SND_SET_DEVICE_PROC, &dmsg, sizeof(dmsg), 5 * HZ);
  490. if (rc < 0) {
  491. printk(KERN_ERR
  492. "%s: msm_rpc_call_reply failed! proc = %d rc = %d\n",
  493. __func__, SND_SET_DEVICE_PROC, rc);
  494. return rc;
  495. }
  496. return 0;
  497. }
  498. static int mic_bias_on_forcely(void)
  499. {
  500. int rc;
  501. struct msm_rpc_endpoint *ep = NULL;
  502. struct rpc_snd_set_device_args {
  503. uint32_t device;
  504. uint32_t ear_mute;
  505. uint32_t mic_mute;
  506. uint32_t cb_func;
  507. uint32_t client_data;
  508. };
  509. struct snd_set_device_msg {
  510. struct rpc_request_hdr hdr;
  511. struct rpc_snd_set_device_args args;
  512. };
  513. struct snd_set_device_msg dmsg;
  514.  
  515. struct rpc_snd_set_extamp_args {
  516. uint32_t device;
  517. uint32_t speaker_volume;
  518. uint32_t headset_volume;
  519.  
  520. uint32_t cb_func;
  521. uint32_t client_data;
  522. };
  523. struct snd_set_extamp_msg {
  524. struct rpc_request_hdr hdr;
  525. struct rpc_snd_set_extamp_args args;
  526. };
  527. struct snd_set_extamp_msg emsg;
  528. /* int data1 = 0;
  529. * int data2 = 0; */
  530. uint32_t device = SND_DEVICE_FORCE_SPEAKER;
  531. uint32_t ear_mute = SND_MUTE_UNMUTED;
  532. uint32_t mic_mute = SND_MUTE_UNMUTED;
  533.  
  534. ep = msm_rpc_connect_compatible(SND_RPC_PROG, SND_RPC_VERS, 0);
  535. if(ep == NULL)
  536. {
  537. printk("snd_rpc_prog endpoint is NULL\n");
  538. return -ENODEV;
  539. }
  540. else if (IS_ERR(ep))
  541. {
  542. rc = PTR_ERR(ep);
  543. printk("%s: rpc connect failed for SND_RPC_PROG. rc = %d\n", __func__, rc);
  544. ep = NULL;
  545. return rc;
  546. }
  547.  
  548. emsg.args.device = cpu_to_be32(device);
  549. emsg.args.speaker_volume = cpu_to_be32(ear_mute);
  550. emsg.args.headset_volume = cpu_to_be32(mic_mute);
  551. emsg.args.cb_func = -1;
  552. emsg.args.client_data = 0;
  553. /* rc = msm_rpc_call(ep, SND_SET_EXTAMP_PROC, &emsg, sizeof(emsg), 5 * HZ);
  554. * if (rc < 0) {
  555. * printk(KERN_ERR
  556. * "%s: msm_rpc_call_reply failed! proc = %d rc = %d\n",
  557. * __func__, SND_SET_DEVICE_PROC, rc);
  558. * return rc;
  559. * }
  560.  
  561. * msleep(10); */
  562.  
  563. H2W_DBG("suspend_count = %d", suspend_count);
  564. //H2W_DBG("%d %d %d", device, ear_mute, mic_mute);
  565. dmsg.args.device = cpu_to_be32(device);
  566. dmsg.args.ear_mute = cpu_to_be32(ear_mute);
  567. dmsg.args.mic_mute = cpu_to_be32(mic_mute);
  568. dmsg.args.cb_func = -1;
  569. dmsg.args.client_data = 0;
  570. rc = msm_rpc_call(ep, SND_SET_DEVICE_PROC, &dmsg, sizeof(dmsg), 5 * HZ);
  571. if (rc < 0) {
  572. printk(KERN_ERR
  573. "%s: msm_rpc_call_reply failed! proc = %d rc = %d\n",
  574. __func__, SND_SET_DEVICE_PROC, rc);
  575. return rc;
  576. }
  577.  
  578. msleep(10);
  579.  
  580. /* ear_mute = SND_MUTE_MUTED;
  581. * mic_mute = SND_MUTE_MUTED;
  582. * H2W_DBG("%d %d %d", device, ear_mute, mic_mute);
  583. * dmsg.args.ear_mute = cpu_to_be32(ear_mute);
  584. * dmsg.args.mic_mute = cpu_to_be32(mic_mute);
  585. * rc = msm_rpc_call(ep, SND_SET_DEVICE_PROC, &dmsg, sizeof(dmsg), 5 * HZ);
  586. * if (rc < 0) {
  587. * printk(KERN_ERR
  588. * "%s: msm_rpc_call_reply failed! proc = %d rc = %d\n",
  589. * __func__, SND_SET_DEVICE_PROC, rc);
  590. * return rc;
  591. * } */
  592. return 0;
  593. }
  594. #endif
  595.  
  596.  
  597. #ifdef FEATURE_SENDEND_ENABLE
  598. static void insert_headset(void)
  599. {
  600. unsigned long irq_flags;
  601. int state = 0;
  602.  
  603. int res;
  604. int data1;
  605. int data2;
  606.  
  607. //int mic_test;
  608. #ifdef FEATURE_3PI_ADC
  609. int adc_3pi = 0;
  610. #endif
  611.  
  612. wake_lock_timeout(&headset_delayed_work_wake_lock, 5*HZ);
  613. /* if(!on_call)
  614. * {
  615. * if(suspend_count)
  616. * {
  617. * mic_bias_on_forcely();
  618. * }
  619. * else if((!suspend_state)&&(resume_state))
  620. * {
  621. * mic_bias_on_forcely();
  622. * }
  623. * } */
  624. #if 0
  625. /* set 4 pole headset */
  626. state = BIT_HEADSET;
  627. printk("[H2W] 1) switch_set_state : %d\n", state);
  628. mutex_lock(&hi->mutex_lock);
  629. switch_set_state(&hi->sdev, state);
  630. mutex_unlock(&hi->mutex_lock);
  631.  
  632. //state = BIT_HEADSET | BIT_HEADSET_NO_MIC;
  633.  
  634. state = switch_get_state(&hi->sdev);
  635. #else
  636. res = 0;
  637. data1 = SMEM_PROC_COMM_MICBIAS_CONTROL_ON;
  638. data2 = 0;
  639.  
  640. res = msm_proc_comm(SMEM_PROC_COMM_MICBIAS_ONOFF, &data1, &data2);
  641. if(res < 0)
  642. {
  643. printk(KERN_ERR "[H2W] %s :%d fail %d\n", __func__, data1, res);
  644. }
  645. #endif
  646. //state &= ~(BIT_HEADSET_NO_MIC | BIT_HEADSET);
  647.  
  648. //if (hi->headset_mic_35mm) {
  649. /* support 3.5mm earphone with mic */
  650. //printk(KERN_INFO "11pin_3.5mm_headset plug in\n");
  651. /* Turn On Mic Bias */
  652. // imsi turn_mic_bias_on(1);
  653. //pmic_mic_en(1);
  654. /* mic_test = pmic_mic_set_volt(MIC_VOLT_2_00V);
  655. * if(0 > mic_test)
  656. * {
  657. * printk("%s : mic_set_volt return error(%d)\n\n", __func__, mic_test);
  658. * } */
  659. /* Wait pin be stable */
  660. msleep(300);
  661. /* Detect headset with or without microphone */
  662. #if 0
  663. if (!gpio_get_value(GPIO_SEND_END)) { // active low
  664. /* without microphone */
  665. // imsi turn_mic_bias_on(0);
  666. mic_en(0);
  667. state |= BIT_HEADSET_NO_MIC;
  668. printk(KERN_INFO
  669. "11pin_3.5mm without microphone\n");
  670. } else { /* with microphone */
  671. #endif
  672. #ifdef FEATURE_3PI_ADC
  673. adc_3pi = get_3pi_adc();
  674. printk("+++++ get_3pi_adc = %d +++++++\n", adc_3pi);
  675. if(adc_3pi >= 0 && adc_3pi <= 3)
  676. {
  677. printk("[h2w] 1) 11pin_3.5mm without microphone\n");
  678. msleep(200);
  679. // recheck 3pi_adc
  680. adc_3pi = get_3pi_adc();
  681. printk("+++++ get_3pi_adc = %d +++++++\n", adc_3pi);
  682. if(adc_3pi >= 0 && adc_3pi <= 3)
  683. {
  684. state |= BIT_HEADSET_NO_MIC;
  685. printk("[h2w] 2) 11pin_3.5mm without microphone\n");
  686. }
  687. else
  688. {
  689. state |= BIT_HEADSET;
  690. /* Enable button irq */
  691. if (!hi->btn_11pin_35mm_flag) {
  692. set_irq_type(hi->irq_btn,IRQF_TRIGGER_LOW); // detect high
  693.  
  694. set_irq_wake(hi->irq_btn, 1);
  695.  
  696. local_irq_save(irq_flags);
  697. enable_irq(hi->irq_btn);
  698. local_irq_restore(irq_flags);
  699.  
  700. hi->btn_11pin_35mm_flag = 1;
  701. }
  702. printk("[h2w] 2) 11pin_3.5mm with microphone\n");
  703. }
  704. }
  705. else
  706. {
  707. state |= BIT_HEADSET;
  708. /* Enable button irq */
  709. if (!hi->btn_11pin_35mm_flag) {
  710. set_irq_type(hi->irq_btn,IRQF_TRIGGER_LOW); // detect high
  711.  
  712. set_irq_wake(hi->irq_btn, 1);
  713.  
  714. local_irq_save(irq_flags);
  715. enable_irq(hi->irq_btn);
  716. local_irq_restore(irq_flags);
  717.  
  718. hi->btn_11pin_35mm_flag = 1;
  719. }
  720. printk("[h2w] 1) 11pin_3.5mm with microphone\n");
  721. }
  722. #else
  723. state |= BIT_HEADSET;
  724. /* Enable button irq */
  725. if (!hi->btn_11pin_35mm_flag) {
  726. set_irq_type(hi->irq_btn,IRQF_TRIGGER_LOW); // detect high
  727.  
  728. set_irq_wake(hi->irq_btn, 1);
  729.  
  730. local_irq_save(irq_flags);
  731. enable_irq(hi->irq_btn);
  732. local_irq_restore(irq_flags);
  733.  
  734. hi->btn_11pin_35mm_flag = 1;
  735. }
  736. printk(KERN_INFO
  737. "11pin_3.5mm with microphone\n");
  738. #endif
  739. // }
  740. //} else /* not support 3.5mm earphone with mic */
  741. // state |= BIT_HEADSET_NO_MIC;
  742. hi->debounce_time = ktime_set(0, 20000000); /* 500 -> 20 ms */
  743.  
  744. /* in case 3 pole headset */
  745. if (!hi->btn_11pin_35mm_flag) {
  746. printk("[H2W] 2) switch_set_state : %d\n", state);
  747. mutex_lock(&hi->mutex_lock);
  748. state = state == 2 ? 1 : state;
  749. switch_set_state(&hi->sdev, state);
  750. mutex_unlock(&hi->mutex_lock);
  751. }
  752. else {
  753. /* set 4 pole headset */
  754. state = BIT_HEADSET;
  755. printk("[H2W] 1) switch_set_state : %d\n", state);
  756. mutex_lock(&hi->mutex_lock);
  757. state = state == 2 ? 1 : state;
  758. switch_set_state(&hi->sdev, state);
  759. mutex_unlock(&hi->mutex_lock);
  760. }
  761.  
  762. /* On some non-standard headset adapters (usually those without a
  763. * button) the btn line is pulled down at the same time as the detect
  764. * line. We can check here by sampling the button line, if it is
  765. * low then it is probably a bad adapter so ignore the button.
  766. * If the button is released then we stop ignoring the button, so that
  767. * the user can recover from the situation where a headset is plugged
  768. * in with button held down.
  769. */
  770. //#ifdef FEATURE_SENDEND_ENABLE
  771. // hi->ignore_btn = !gpio_get_value(GPIO_SEND_END );
  772. //#else
  773. // hi->ignore_btn = 1;
  774. //#endif
  775.  
  776. //#ifdef FEATURE_SENDEND_ENABLE
  777. // /* Enable button irq */
  778. // local_irq_save(irq_flags);
  779. // enable_irq(hi->irq_btn);
  780. // local_irq_restore(irq_flags);
  781. //#endif
  782. // hi->debounce_time = ktime_set(0, 20000000); /* 20 ms */
  783. }
  784. #else
  785. static void insert_headset(void)
  786. {
  787. unsigned long irq_flags;
  788.  
  789. H2W_DBG("");
  790.  
  791. switch_set_state(&hi->sdev, H2W_SEC_HEADSET);
  792.  
  793. /* On some non-standard headset adapters (usually those without a
  794. * button) the btn line is pulled down at the same time as the detect
  795. * line. We can check here by sampling the button line, if it is
  796. * low then it is probably a bad adapter so ignore the button.
  797. * If the button is released then we stop ignoring the button, so that
  798. * the user can recover from the situation where a headset is plugged
  799. * in with button held down.
  800. */
  801. hi->ignore_btn = 1; // !gpio_get_value(BIGFOOT_GPIO_CABLE_IN2);
  802. #if 0
  803. /* Enable button irq */
  804. local_irq_save(irq_flags);
  805. enable_irq(hi->irq_btn);
  806. local_irq_restore(irq_flags);
  807. #endif
  808. hi->debounce_time = ktime_set(0, 20000000); /* 20 ms */
  809. }
  810.  
  811.  
  812. #endif
  813.  
  814. #ifdef FEATURE_SENDEND_ENABLE
  815. static int is_accessary_pluged_in(void)
  816. {
  817. int type = 0;
  818. int jack_state = 0;
  819. int sendend_state = 0;
  820. /* int mic_test = 0;
  821.  
  822. * mic_test = pmic_mic_set_volt(MIC_VOLT_2_00V);
  823. * if(0 > mic_test)
  824. * {
  825. * printk("%s : mic_set_volt return error(%d)\n\n", __func__, mic_test);
  826. * }
  827. * pmic_mic_en(1); */
  828. /* Step 1: set both GPIO_CABLE_IN1 and GPIO_CABLE_IN2 as input */
  829. jack_state = gpio_get_value(GPIO_JACK_S_35);
  830. sendend_state = gpio_get_value(GPIO_SEND_END);
  831.  
  832. printk("[H2W] jack_state = %d, sendend_state = %d\n",jack_state,sendend_state);
  833.  
  834. #if 0
  835. if ((jack_state == 1) && (sendend_state == 1) )
  836. {
  837. type = H2W_SEC_HEADSET;
  838. }
  839. else if ((jack_state == 1) && (sendend_state == 0))
  840. {
  841. type = H2W_NORMAL_HEADSET;
  842. }
  843. #else
  844. #ifdef JACK_ACTIVE_LOW
  845. if ((jack_state == 0))
  846. #else
  847. if ((jack_state == 1))
  848. #endif
  849. {
  850. type = H2W_SEC_HEADSET;
  851. }
  852. #endif
  853. #ifdef JACK_ACTIVE_LOW
  854. else if ( jack_state == 1 )
  855. #else
  856. else if ( jack_state == 0 )
  857. #endif
  858. {
  859. type = H2W_NO_DEVICE;
  860. }
  861. // khsuh_imsi mic_en(0);
  862. //pmic_mic_en(0);
  863.  
  864. return type;
  865. }
  866. #endif
  867.  
  868. void headset_button_event(int is_press)
  869. {
  870. if (!is_press) //release
  871. {
  872. if (hi->ignore_btn)
  873. hi->ignore_btn = 0;
  874. else if (atomic_read(&hi->btn_state))
  875. button_released();
  876. }
  877. else //press
  878. {
  879. if (!hi->ignore_btn && !atomic_read(&hi->btn_state))
  880. button_pressed();
  881. }
  882. }
  883.  
  884. static void earbutton_func_work(struct work_struct *work)
  885. {
  886. int headset_state;
  887. int count = 8;
  888.  
  889. headset_state = gpio_get_value(GPIO_JACK_S_35);
  890.  
  891. while(count-- && !earbutton_pressed)
  892. {
  893. #ifdef JACK_ACTIVE_LOW
  894. if(hi->button_state != (gpio_get_value(GPIO_SEND_END)) || headset_state)
  895. #else
  896. if(hi->button_state != (gpio_get_value(GPIO_SEND_END)) || !headset_state)
  897. #endif
  898. {
  899. H2W_DBG("SEND_END key is ignored. State is unstable.");
  900. return;
  901. }
  902. mdelay(5);
  903. }
  904.  
  905. //H2W_DBG("count = %d button_state is %s", count+1, hi->button_state?"pressed":"released");
  906. if (hi->button_state) {
  907. headset_button_event(1);
  908. } else {
  909. headset_button_event(0);
  910. }
  911. }
  912.  
  913. #if 1 // for DFMS test
  914. static void earbutton_work(struct work_struct *work)
  915. {
  916. mutex_lock(&hi->mutex_lock);
  917. switch_set_state(&hi->button_sdev, earbutton_pressed);
  918. mutex_unlock(&hi->mutex_lock);
  919. }
  920. #endif
  921.  
  922. static void detection_work(struct work_struct *work)
  923. {
  924. unsigned long irq_flags;
  925. int jack_state;
  926. int type;
  927.  
  928. H2W_DBG("");
  929. #ifdef JACK_ACTIVE_LOW
  930. if (gpio_get_value(GPIO_JACK_S_35) != 0)
  931. #else
  932. if (gpio_get_value(GPIO_JACK_S_35) != 1)
  933. #endif
  934. {
  935. /* Headset not plugged in */
  936. if (switch_get_state(&hi->sdev) != H2W_NO_DEVICE)
  937. remove_headset();
  938. return;
  939. }
  940.  
  941. /* Something plugged in, lets make sure its a headset */
  942.  
  943. /* Disable headset interrupt while detecting.*/
  944. local_irq_save(irq_flags);
  945. disable_irq(hi->irq);
  946. local_irq_restore(irq_flags);
  947. #ifdef FEATURE_SENDEND_ENABLE
  948. /* Something plugged in, lets make sure its a headset */
  949. type = is_accessary_pluged_in();
  950. #endif
  951.  
  952. /* Restore IRQs */
  953. local_irq_save(irq_flags);
  954. enable_irq(hi->irq);
  955. local_irq_restore(irq_flags);
  956.  
  957. jack_state = gpio_get_value(GPIO_JACK_S_35);
  958. // msleep(1);
  959. #ifdef JACK_ACTIVE_LOW
  960. if ( gpio_get_value(GPIO_JACK_S_35) == 0 )
  961. #else
  962. if ( gpio_get_value(GPIO_JACK_S_35) == 1 )
  963. #endif
  964. {
  965. if (switch_get_state(&hi->sdev) == H2W_NO_DEVICE)
  966. {
  967. insert_headset();
  968. }
  969. else
  970. {
  971. printk("GPIO_JACK_S_35 = 1, but not insert_headset\n\n");
  972. }
  973. }
  974. else {
  975. printk("JACK_S_35 was low, but not a headset "
  976. "(recent jack_state = %d)", jack_state);
  977. }
  978. }
  979.  
  980. #if 0
  981. static enum hrtimer_restart button_event_timer_func(struct hrtimer *data)
  982. {
  983. H2W_DBG("");
  984. #ifdef FEATURE_SENDEND_ENABLE
  985. // check again
  986. if(hi->button_state != gpio_get_value(GPIO_SEND_END))
  987. {
  988. printk("ERROR - button value : %d -> %d\n", hi->button_state, gpio_get_value(GPIO_SEND_END));
  989. return HRTIMER_NORESTART;
  990. }
  991.  
  992. // butten active high
  993. if (gpio_get_value(GPIO_SEND_END)) {
  994. headset_button_event(1);
  995. /* 10 ms */
  996. //hi->btn_debounce_time = ktime_set(0, 10000000);
  997. } else {
  998. headset_button_event(0);
  999. /* 100 ms */
  1000. //hi->btn_debounce_time = on_call ? ktime_set(0, 400000000):ktime_set(0, BTN_PRESS_DEBOUNCE_TIME);
  1001. }
  1002. // printk("%s - SEND_END=%d.\n", __func__, gpio_get_value(GPIO_SEND_END));
  1003. #else
  1004. if (switch_get_state(&hi->sdev) == H2W_SEC_HEADSET) {
  1005. if (0){ // gpio_get_value(GPIO_SEND_END )) {
  1006. if (hi->ignore_btn)
  1007. hi->ignore_btn = 0;
  1008. else if (atomic_read(&hi->btn_state))
  1009. button_released();
  1010. } else {
  1011. if (!hi->ignore_btn && !atomic_read(&hi->btn_state))
  1012. button_pressed();
  1013. }
  1014. }
  1015. #endif
  1016. return HRTIMER_NORESTART;
  1017. }
  1018. #endif
  1019.  
  1020. static enum hrtimer_restart detect_event_timer_func(struct hrtimer *data)
  1021. {
  1022. H2W_DBG("");
  1023.  
  1024. queue_work(g_detection_work_queue, &g_detection_work);
  1025. return HRTIMER_NORESTART;
  1026. }
  1027.  
  1028. static irqreturn_t detect_irq_handler(int irq, void *dev_id)
  1029. {
  1030. int value1, value2;
  1031. int retry_limit = 10;
  1032.  
  1033. set_irq_type(hi->irq_btn, IRQF_TRIGGER_HIGH); // detect high
  1034.  
  1035. H2W_DBG("");
  1036. do {
  1037. value1 = gpio_get_value(GPIO_JACK_S_35);
  1038. set_irq_type(hi->irq, value1 ?
  1039. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
  1040. value2 = gpio_get_value(GPIO_JACK_S_35);
  1041. } while (value1 != value2 && retry_limit-- > 0);
  1042.  
  1043. // printk("headset value2 = %d (%d retries)\n", value2, (10-retry_limit));
  1044.  
  1045. #ifdef JACK_ACTIVE_LOW
  1046. // if ((switch_get_state(&hi->sdev) == H2W_NO_DEVICE) ^ value2)
  1047. #else
  1048. // if ((switch_get_state(&hi->sdev) == H2W_NO_DEVICE) ^ !value2)
  1049. #endif
  1050. {
  1051. hi->headset_state = value1;
  1052. H2W_DBG("GPIO_JACK_S_35 is %s",value1?"HIGH":"LOW");
  1053. if (switch_get_state(&hi->sdev) != H2W_NO_DEVICE)
  1054. hi->ignore_btn = 1;
  1055. /* Do the rest of the work in timer context */
  1056. hrtimer_start(&hi->timer, hi->debounce_time, HRTIMER_MODE_REL);
  1057. }
  1058.  
  1059. return IRQ_HANDLED;
  1060. }
  1061.  
  1062. static irqreturn_t button_irq_handler(int irq, void *dev_id)
  1063. {
  1064. int value1, value2;
  1065. int retry_limit = 10;
  1066. int headset_state;
  1067.  
  1068. H2W_DBG("");
  1069. do {
  1070. #ifdef FEATURE_SENDEND_ENABLE
  1071. value1 = gpio_get_value(GPIO_SEND_END );
  1072. set_irq_type(hi->irq_btn, value1 ?
  1073. IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH);
  1074. value2 = gpio_get_value(GPIO_SEND_END );
  1075. #else
  1076. value1 = 0;
  1077. set_irq_type(hi->irq_btn, value1 ?
  1078. IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH);
  1079. value2 = 0;
  1080. #endif
  1081. } while (value1 != value2 && retry_limit-- > 0);
  1082.  
  1083. headset_state = gpio_get_value(GPIO_JACK_S_35);
  1084.  
  1085. #ifdef JACK_ACTIVE_LOW
  1086. if(headset_state)
  1087. #else
  1088. if(!headset_state)
  1089. #endif
  1090. {
  1091. H2W_DBG("ignored cuz headset was removed");
  1092. return IRQ_HANDLED;
  1093. }
  1094.  
  1095. /* if (!gpio_get_value(GPIO_SEND_END))
  1096. * {
  1097. * hi->btn_debounce_time = ktime_set(0, 10000000);
  1098. * }
  1099. * else
  1100. * {
  1101. * //H2W_DBG("on_call = %d",on_call);
  1102. * hi->btn_debounce_time = on_call ? ktime_set(0, 600000000):ktime_set(0, BTN_PRESS_DEBOUNCE_TIME);
  1103. * } */
  1104.  
  1105. if(value1 == value2 && retry_limit > 0)
  1106. {
  1107. hi->button_state = value1;
  1108. H2W_DBG("GPIO_SEND_END is %s (%d retries)", hi->button_state?"HIGH":"LOW", (10 - retry_limit));
  1109. //hrtimer_start(&hi->btn_timer, hi->btn_debounce_time, HRTIMER_MODE_REL);
  1110. schedule_work(&g_earbutton_func_work);
  1111. }
  1112.  
  1113. return IRQ_HANDLED;
  1114. }
  1115.  
  1116. #if defined(CONFIG_DEBUG_FS)
  1117. static int h2w_debug_set(void *data, u64 val)
  1118. {
  1119. mutex_lock(&hi->mutex_lock);
  1120. int tmp = (int)val == 2 ? 1 : (int)val;
  1121. switch_set_state(&hi->sdev, tmp);
  1122. mutex_unlock(&hi->mutex_lock);
  1123. return 0;
  1124. }
  1125.  
  1126. static int h2w_debug_get(void *data, u64 *val)
  1127. {
  1128. return 0;
  1129. }
  1130.  
  1131.  
  1132. DEFINE_SIMPLE_ATTRIBUTE(h2w_debug_fops, h2w_debug_get, h2w_debug_set, "%llu\n");
  1133. static int __init h2w_debug_init(void)
  1134. {
  1135. struct dentry *dent;
  1136.  
  1137. dent = debugfs_create_dir("h2w", 0);
  1138. if (IS_ERR(dent))
  1139. return PTR_ERR(dent);
  1140.  
  1141. debugfs_create_file("state", 0644, dent, NULL, &h2w_debug_fops);
  1142.  
  1143. return 0;
  1144. }
  1145.  
  1146. device_initcall(h2w_debug_init);
  1147. #endif
  1148.  
  1149. static int h2w_probe(struct platform_device *pdev)
  1150. {
  1151. int ret;
  1152. //unsigned long irq_flags;
  1153.  
  1154. printk(KERN_INFO "[H2W] Registering H2W (headset) driver\n");
  1155. hi = kzalloc(sizeof(struct h2w_info), GFP_KERNEL);
  1156. if (!hi)
  1157. return -ENOMEM;
  1158.  
  1159. atomic_set(&hi->btn_state, 0);
  1160. hi->ignore_btn = 0;
  1161. hi->headset_state = 0;
  1162. hi->button_state = 0;
  1163.  
  1164. hi->debounce_time = ktime_set(0, 20000000); /* 100 ms -> 20 ms */
  1165. //hi->btn_debounce_time = ktime_set(0, 50000000); /* 50 ms */
  1166.  
  1167. hi->btn_11pin_35mm_flag = 0;
  1168.  
  1169. mutex_init(&hi->mutex_lock);
  1170.  
  1171. hi->sdev.name = "h2w";
  1172. hi->sdev.print_name = h2w_print_name;
  1173. hi->sdev.set_state = h2w_set_state;
  1174.  
  1175. ret = switch_dev_register(&hi->sdev);
  1176. if (ret < 0)
  1177. goto err_switch_dev_register;
  1178.  
  1179. #if 1 // for DFMS test
  1180. hi->button_sdev.name = "sec_earbutton";
  1181. hi->button_sdev.print_name = button_print_name;
  1182.  
  1183. ret = switch_dev_register(&hi->button_sdev);
  1184. if (ret < 0)
  1185. goto err_switch_dev_register;
  1186. switch_set_state(&hi->button_sdev, 0);
  1187.  
  1188. g_earbutton_work_queue = create_workqueue("earbutton");
  1189. if (g_earbutton_work_queue == NULL) {
  1190. ret = -ENOMEM;
  1191. goto err_create_work_queue;
  1192. }
  1193. #endif
  1194.  
  1195. g_detection_work_queue = create_workqueue("detection");
  1196. if (g_detection_work_queue == NULL) {
  1197. ret = -ENOMEM;
  1198. goto err_create_work_queue;
  1199. }
  1200.  
  1201. // printk("%s:%d GPIO_SEND_END=%d.\n",__func__, __LINE__, GPIO_SEND_END);
  1202.  
  1203. /*****************************************************************/
  1204. // JACK_S_35 GPIO setting
  1205. /*****************************************************************/
  1206.  
  1207. ret = gpio_request(GPIO_JACK_S_35, "h2w_detect");
  1208. if (ret < 0)
  1209. goto err_request_detect_gpio;
  1210.  
  1211. ret = gpio_direction_input(GPIO_JACK_S_35);
  1212. if (ret < 0)
  1213. goto err_set_detect_gpio;
  1214.  
  1215. hi->irq = gpio_to_irq(GPIO_JACK_S_35);
  1216. if (hi->irq < 0) {
  1217. ret = hi->irq;
  1218. goto err_get_h2w_detect_irq_num_failed;
  1219. }
  1220.  
  1221. hrtimer_init(&hi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1222. hi->timer.function = detect_event_timer_func;
  1223.  
  1224. /*****************************************************************/
  1225. // SEND_END GPIO setting
  1226. /*****************************************************************/
  1227.  
  1228. ret = gpio_request(GPIO_SEND_END , "h2w_button");
  1229. if (ret < 0)
  1230. goto err_request_button_gpio;
  1231.  
  1232. ret = gpio_direction_input(GPIO_SEND_END );
  1233. if (ret < 0)
  1234. goto err_set_button_gpio;
  1235.  
  1236. hi->irq_btn = gpio_to_irq(GPIO_SEND_END );
  1237. if (hi->irq_btn < 0) {
  1238. ret = hi->irq_btn;
  1239. goto err_get_button_irq_num_failed;
  1240. }
  1241.  
  1242. /* hrtimer_init(&hi->btn_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1243. * hi->btn_timer.function = button_event_timer_func; */
  1244.  
  1245.  
  1246.  
  1247. /*****************************************************************/
  1248. // JACK_S_35 irq setting
  1249. /*****************************************************************/
  1250. // local_irq_save(irq_flags);
  1251. // printk("%s:%d JACK_value=%d.\n",__func__, __LINE__, gpio_get_value(GPIO_JACK_S_35));
  1252.  
  1253. #ifdef JACK_ACTIVE_LOW
  1254. ret = request_irq(hi->irq, detect_irq_handler,
  1255. IRQF_TRIGGER_FALLING, "h2w_detect", NULL);
  1256. #else
  1257. ret = request_irq(hi->irq, detect_irq_handler,
  1258. IRQF_TRIGGER_RISING, "h2w_detect", NULL);
  1259. #endif
  1260. if (ret < 0)
  1261. goto err_request_detect_irq;
  1262.  
  1263. hi->use_irq = ret == 0;
  1264.  
  1265. printk(KERN_INFO "Headset Driver: Start gpio inputs for %s in %s mode\n", hi->sdev.name, hi->use_irq ? "interrupt" : "polling");
  1266.  
  1267. ret = set_irq_wake(hi->irq, 1);
  1268. if (ret < 0)
  1269. goto err_request_input_dev;
  1270.  
  1271. // local_irq_restore(irq_flags);
  1272.  
  1273.  
  1274. /*****************************************************************/
  1275. // SEND_END irq setting
  1276. /*****************************************************************/
  1277.  
  1278. // local_irq_save(irq_flags);
  1279. /* Disable button until plugged in */
  1280. set_irq_flags(hi->irq_btn, IRQF_VALID | IRQF_NOAUTOEN);
  1281. ret = request_irq(hi->irq_btn, button_irq_handler,
  1282. IRQF_TRIGGER_HIGH, "h2w_button", NULL); // detect high
  1283. if (ret < 0)
  1284. goto err_request_button_irq;
  1285.  
  1286. // local_irq_restore(irq_flags);
  1287.  
  1288. hi->input = input_allocate_device();
  1289. if (!hi->input) {
  1290. ret = -ENOMEM;
  1291. goto err_request_input_dev;
  1292. }
  1293. #if defined(CONFIG_MACH_CALLISTO) || defined(CONFIG_MACH_CRONIN)
  1294. hi->input->name = "callisto_headset";
  1295. #elif defined(CONFIG_MACH_EUROPA)
  1296. hi->input->name = "europa_headset";
  1297. #endif
  1298. set_bit(EV_KEY, hi->input->evbit);
  1299. set_bit(KEY_MEDIA, hi->input->keybit);
  1300.  
  1301. ret = input_register_device(hi->input);
  1302. if (ret < 0)
  1303. goto err_register_input_dev;
  1304.  
  1305. #ifdef FEATURE_SND_RPC
  1306. /* if(flag_boot)
  1307. * {
  1308. * mic_bias_on();
  1309. * flag_boot = 0;
  1310. * } */
  1311. #endif
  1312.  
  1313. return 0;
  1314.  
  1315. err_register_input_dev:
  1316. input_free_device(hi->input);
  1317. err_request_input_dev:
  1318. #ifdef FEATURE_SENDEND_ENABLE
  1319. free_irq(hi->irq, 0);
  1320. #endif
  1321. free_irq(hi->irq_btn, 0);
  1322. err_get_button_irq_num_failed:
  1323. err_get_h2w_detect_irq_num_failed:
  1324. err_set_button_gpio:
  1325. err_set_detect_gpio:
  1326. err_request_detect_irq:
  1327. gpio_free(GPIO_JACK_S_35);
  1328. err_request_button_gpio:
  1329. err_request_button_irq:
  1330. #if 1 // def FEATURE_SENDEND_ENABLE
  1331. gpio_free(GPIO_SEND_END );
  1332. #endif
  1333. err_request_detect_gpio:
  1334. destroy_workqueue(g_detection_work_queue);
  1335. destroy_workqueue(g_earbutton_work_queue);
  1336. err_create_work_queue:
  1337. switch_dev_unregister(&hi->sdev);
  1338. err_switch_dev_register:
  1339. printk(KERN_ERR "H2W: Failed to register driver\n");
  1340.  
  1341. return ret;
  1342. }
  1343.  
  1344. static int h2w_remove(struct platform_device *pdev)
  1345. {
  1346. H2W_DBG("");
  1347. if (switch_get_state(&hi->sdev))
  1348. remove_headset();
  1349. input_unregister_device(hi->input);
  1350. #ifdef FEATURE_SENDEND_ENABLE
  1351. gpio_free(GPIO_SEND_END );
  1352. #endif
  1353. gpio_free(GPIO_JACK_S_35);
  1354. #ifdef FEATURE_SENDEND_ENABLE
  1355. free_irq(hi->irq_btn, 0);
  1356. #endif
  1357. free_irq(hi->irq, 0);
  1358. destroy_workqueue(g_detection_work_queue);
  1359. destroy_workqueue(g_earbutton_work_queue);
  1360. switch_dev_unregister(&hi->sdev);
  1361.  
  1362. return 0;
  1363. }
  1364.  
  1365.  
  1366. static struct platform_device h2w_device = {
  1367. #if defined(CONFIG_MACH_CALLISTO) || defined(CONFIG_MACH_CRONIN)
  1368. .name = "callisto-headset",
  1369. #elif defined(CONFIG_MACH_EUROPA)
  1370. .name = "europa-headset",
  1371. #endif
  1372. };
  1373.  
  1374.  
  1375. static struct platform_driver h2w_driver = {
  1376. .probe = h2w_probe,
  1377. .remove = h2w_remove,
  1378. .suspend = h2w_suspend,
  1379. .resume = h2w_resume,
  1380. .driver = {
  1381. #if defined(CONFIG_MACH_CALLISTO) || defined(CONFIG_MACH_CRONIN)
  1382. .name = "callisto-headset",
  1383. #elif defined(CONFIG_MACH_EUROPA)
  1384. .name = "europa-headset",
  1385. #endif
  1386. .owner = THIS_MODULE,
  1387. },
  1388. };
  1389.  
  1390. int __init h2w_init(void)
  1391. {
  1392. int ret;
  1393. H2W_DBG("JACK_S_35(%d), SEND_END(%d)", GPIO_JACK_S_35, GPIO_SEND_END);
  1394. wake_lock_init(&headset_delayed_work_wake_lock, WAKE_LOCK_SUSPEND, "headset_delayed_work");
  1395. ret = platform_driver_register(&h2w_driver);
  1396. if (ret)
  1397. return ret;
  1398. return platform_device_register(&h2w_device);
  1399. }
  1400.  
  1401. static void __exit h2w_exit(void)
  1402. {
  1403. wake_lock_destroy(&headset_delayed_work_wake_lock);
  1404. platform_device_unregister(&h2w_device);
  1405.  
  1406. platform_driver_unregister(&h2w_driver);
  1407. }
  1408.  
  1409. module_init(h2w_init);
  1410. module_exit(h2w_exit);
  1411.  
  1412. MODULE_AUTHOR("anonymous <anonymous@samsung.com>");
  1413. MODULE_DESCRIPTION("SAMSUNG Headset driver");
  1414. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement