sec_jack.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /* drivers/misc/sec_jack.c
  2. *
  3. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/irq.h>
  19. #include <linux/delay.h>
  20. #include <linux/types.h>
  21. #include <linux/input.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/errno.h>
  24. #include <linux/err.h>
  25. #include <linux/switch.h>
  26. #include <linux/input.h>
  27. #include <linux/timer.h>
  28. #include <linux/wakelock.h>
  29. #include <linux/slab.h>
  30. #include <linux/gpio.h>
  31. #include <linux/gpio_event.h>
  32. #include <linux/sec_jack.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/qpnp/qpnp-adc.h>
  35. #include <linux/regulator/consumer.h>
  36. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  37. #include <linux/qpnp/pin.h>
  38. #endif
  39. #define NUM_INPUT_DEVICE_ID 2
  40. #define MAX_ZONE_LIMIT 10
  41. #ifdef CONFIG_MACH_MILLET3G_CHN_OPEN
  42. #define SEND_KEY_CHECK_TIME_MS 40 /* 40ms */
  43. #else
  44. #define SEND_KEY_CHECK_TIME_MS 30 /* 30ms */
  45. #endif
  46. #define DET_CHECK_TIME_MS 100 /* 100ms */
  47. #define DET_CHECK_TIME_MS_WITH_FSA 50 /* 50ms */
  48. #define WAKE_LOCK_TIME (HZ * 5) /* 5 sec */
  49. struct sec_jack_info {
  50. struct platform_device *client;
  51. struct sec_jack_platform_data *pdata;
  52. struct delayed_work jack_detect_work;
  53. struct work_struct buttons_work;
  54. struct work_struct detect_work;
  55. struct workqueue_struct *queue;
  56. struct workqueue_struct *buttons_queue;
  57. struct timer_list timer;
  58. struct input_dev *input_dev;
  59. struct wake_lock det_wake_lock;
  60. struct sec_jack_zone *zone;
  61. struct input_handler handler;
  62. struct input_handle handle;
  63. struct input_device_id ids[NUM_INPUT_DEVICE_ID];
  64. int det_irq;
  65. int dev_id;
  66. int pressed;
  67. int pressed_code;
  68. bool buttons_enable;
  69. struct platform_device *send_key_dev;
  70. unsigned int cur_jack_type;
  71. };
  72. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  73. int pm8941_mpp4;
  74. static struct qpnp_pin_cfg pm8941_mpp4_endis = {
  75. .mode = QPNP_PIN_MODE_AIN,
  76. .ain_route = QPNP_PIN_AIN_AMUX_CH8,
  77. .src_sel = QPNP_PIN_SEL_FUNC_CONSTANT, /* Function constant */
  78. .master_en = QPNP_PIN_MASTER_ENABLE,
  79. };
  80. #endif
  81. /* with some modifications like moving all the gpio structs inside
  82. * the platform data and getting the name for the switch and
  83. * gpio_event from the platform data, the driver could support more than
  84. * one headset jack, but currently user space is looking only for
  85. * one key file and switch for a headset so it'd be overkill and
  86. * untestable so we limit to one instantiation for now.
  87. */
  88. static atomic_t instantiated = ATOMIC_INIT(0);
  89. /* sysfs name HeadsetObserver.java looks for to track headset state
  90. */
  91. struct switch_dev switch_jack_detection = {
  92. .name = "h2w",
  93. };
  94. /* Samsung factory test application looks for to track button state
  95. */
  96. struct switch_dev switch_sendend = {
  97. .name = "send_end", /* /sys/class/switch/send_end/state */
  98. };
  99. static struct gpio_event_direct_entry sec_jack_key_map[] = {
  100. {
  101. .code = KEY_UNKNOWN,
  102. },
  103. };
  104. static struct gpio_event_input_info sec_jack_key_info = {
  105. .info.func = gpio_event_input_func,
  106. .info.no_suspend = true,
  107. .type = EV_KEY,
  108. .debounce_time.tv64 = SEND_KEY_CHECK_TIME_MS * NSEC_PER_MSEC,
  109. .keymap = sec_jack_key_map,
  110. .keymap_size = ARRAY_SIZE(sec_jack_key_map)
  111. };
  112. static struct gpio_event_info *sec_jack_input_info[] = {
  113. &sec_jack_key_info.info,
  114. };
  115. static struct gpio_event_platform_data sec_jack_input_data = {
  116. .name = "sec_jack",
  117. .info = sec_jack_input_info,
  118. .info_count = ARRAY_SIZE(sec_jack_input_info),
  119. };
  120. #ifdef CONFIG_ARCH_MSM8226
  121. /*Enabling Ear Mic Bias of WCD Codec*/
  122. extern void msm8226_enable_ear_micbias(bool state);
  123. #endif
  124. #if defined(CONFIG_MACH_KLTE_JPN)
  125. #define MAX_DETECT_LIMIT 3
  126. static void determine_jack_type(struct sec_jack_info *hi);
  127. static int detect_count = 0;
  128. #endif
  129. #if defined(CONFIG_SAMSUNG_JACK_READ_BTN_ADC)
  130. static int ear_adc_value = 0;
  131. #endif
  132. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  133. static void mpp_control(bool onoff)
  134. {
  135. if(onoff) {
  136. pr_info("%s : mpp enable =======\n",__func__);
  137. pm8941_mpp4_endis.master_en = QPNP_PIN_MASTER_ENABLE;
  138. qpnp_pin_config(pm8941_mpp4, &pm8941_mpp4_endis);
  139. } else {
  140. pr_info("%s : mpp diable =======\n",__func__);
  141. pm8941_mpp4_endis.master_en = QPNP_PIN_MASTER_DISABLE;
  142. qpnp_pin_config(pm8941_mpp4, &pm8941_mpp4_endis);
  143. }
  144. }
  145. #endif
  146. static void sec_jack_gpio_init(struct sec_jack_platform_data *pdata)
  147. {
  148. int ret;
  149. #ifdef CONFIG_ARCH_MSM8226
  150. if(pdata->ear_micbias_gpio > 0) {
  151. ret = gpio_request(pdata->ear_micbias_gpio, "ear_micbias_en");
  152. if (ret) {
  153. pr_err("%s : gpio_request failed for %d\n", __func__,
  154. pdata->ear_micbias_gpio);
  155. return;
  156. }
  157. gpio_direction_output(pdata->ear_micbias_gpio, 0);
  158. }
  159. #else
  160. #ifdef CONFIG_MACH_HEAT_AIO
  161. gpio_free(pdata->ear_micbias_gpio);
  162. pr_err("%s HEAT_AIO: gpio_free for %d\n", __func__,
  163. pdata->ear_micbias_gpio);
  164. #endif
  165. if (pdata->ear_micbias_gpio > 0) {
  166. ret = gpio_request(pdata->ear_micbias_gpio, "ear_micbias_en");
  167. if (ret) {
  168. pr_err("%s : gpio_request failed for %d\n", __func__,
  169. pdata->ear_micbias_gpio);
  170. return;
  171. }
  172. gpio_direction_output(pdata->ear_micbias_gpio, 0);
  173. }
  174. #endif
  175. if (pdata->fsa_en_gpio > 0) {
  176. ret = gpio_request(pdata->fsa_en_gpio, "fsa_en");
  177. if (ret) {
  178. pr_err("%s : gpio_request failed for %d\n", __func__,
  179. pdata->fsa_en_gpio);
  180. return;
  181. }
  182. gpio_direction_output(pdata->fsa_en_gpio, 1);
  183. }
  184. }
  185. static int sec_jack_get_adc_value(struct sec_jack_info *hi)
  186. {
  187. struct qpnp_vadc_result result;
  188. struct sec_jack_platform_data *pdata = hi->pdata;
  189. int retVal;
  190. int rc = 0;
  191. uint32_t mpp_ch;
  192. /* Initialize mpp_ch default setting
  193. * default mpp scale is < 4 1 3 >
  194. */
  195. mpp_ch = pdata->mpp_ch_scale[0] + P_MUX1_1_3 - 1;
  196. /* To get proper mpp_ch,
  197. * If scaling is 1:1 then add (P_MUX1_1_1 - 1)
  198. * If scaling is 1:3 then add (P_MUX1_1_3 - 1)
  199. */
  200. if (pdata->mpp_ch_scale[2] == 1)
  201. mpp_ch = pdata->mpp_ch_scale[0] + P_MUX1_1_1 - 1;
  202. else if (pdata->mpp_ch_scale[2] == 3)
  203. mpp_ch = pdata->mpp_ch_scale[0] + P_MUX1_1_3 - 1;
  204. else
  205. pr_err("%s - invalid channel scale=%d\n", __func__, pdata->mpp_ch_scale[2]);
  206. qpnp_get_vadc(&hi->client->dev, "earjack-read");
  207. #ifdef CONFIG_ARCH_MSM8226
  208. // Read the MPP4 VADC channel with 1:3 scaling
  209. rc = qpnp_vadc_read(pdata->vadc_dev, mpp_ch, &result);
  210. #else
  211. rc = qpnp_vadc_read(NULL, mpp_ch, &result);
  212. #endif
  213. if(rc)
  214. pr_err("%s: qpnp_vadc_read failed: %d\n", __func__, rc);
  215. // Get voltage in microvolts
  216. retVal = ((int)result.physical)/1000;
  217. return retVal;
  218. }
  219. static void set_sec_micbias_state(struct sec_jack_info *hi, bool state)
  220. {
  221. struct sec_jack_platform_data *pdata = hi->pdata;
  222. #ifdef CONFIG_ARCH_MSM8226
  223. if(pdata->ear_micbias_gpio > 0)
  224. gpio_set_value_cansleep(pdata->ear_micbias_gpio, state); /*Uses external Mic Bias*/
  225. else
  226. msm8226_enable_ear_micbias(state); /* Uses WCD Mic Bias*/
  227. #else
  228. static struct regulator *ear_micbias_regulator = NULL;
  229. static int micbias_state_count = 0;
  230. if (pdata->ear_micbias_gpio > 0) {
  231. gpio_set_value_cansleep(pdata->ear_micbias_gpio, state);
  232. } else if (pdata->ear_micbias_ldo != NULL) {
  233. if (ear_micbias_regulator == NULL) {
  234. ear_micbias_regulator = regulator_get(NULL, pdata->ear_micbias_ldo);
  235. if (IS_ERR(ear_micbias_regulator)) {
  236. ear_micbias_regulator = NULL;
  237. pr_err("%s: regulator_get failed for %s\n", __func__, pdata->ear_micbias_ldo);
  238. return;
  239. }
  240. regulator_set_voltage(ear_micbias_regulator, 2800000, 2800000);
  241. }
  242. if (state == true && micbias_state_count == 0) {
  243. if (!regulator_enable(ear_micbias_regulator))
  244. micbias_state_count = 1;
  245. } else if (state == false && micbias_state_count == 1) {
  246. if (!regulator_disable(ear_micbias_regulator))
  247. micbias_state_count = 0;
  248. }
  249. }
  250. #endif
  251. }
  252. /* gpio_input driver does not support to read adc value.
  253. * We use input filter to support 3-buttons of headset
  254. * without changing gpio_input driver.
  255. */
  256. static bool sec_jack_buttons_filter(struct input_handle *handle,
  257. unsigned int type, unsigned int code, int value)
  258. {
  259. struct sec_jack_info *hi = handle->handler->private;
  260. if (type != EV_KEY || code != KEY_UNKNOWN)
  261. return false;
  262. hi->pressed = value;
  263. /* This is called in timer handler of gpio_input driver.
  264. * We use workqueue to read adc value.
  265. */
  266. queue_work(hi->buttons_queue, &hi->buttons_work);
  267. return true;
  268. }
  269. static int sec_jack_buttons_connect(struct input_handler *handler,
  270. struct input_dev *dev, const struct input_device_id *id)
  271. {
  272. struct sec_jack_info *hi;
  273. struct sec_jack_platform_data *pdata;
  274. struct sec_jack_buttons_zone *btn_zones;
  275. int err;
  276. int i;
  277. int num_buttons_zones;
  278. /* bind input_handler to input device related to only sec_jack */
  279. if (dev->name != sec_jack_input_data.name)
  280. return -ENODEV;
  281. hi = handler->private;
  282. pdata = hi->pdata;
  283. btn_zones = pdata->jack_buttons_zones;
  284. num_buttons_zones = ARRAY_SIZE(pdata->jack_buttons_zones);
  285. hi->input_dev = dev;
  286. hi->handle.dev = dev;
  287. hi->handle.handler = handler;
  288. hi->handle.open = 0;
  289. hi->handle.name = "sec_jack_buttons";
  290. err = input_register_handle(&hi->handle);
  291. if (err) {
  292. pr_err("%s: Failed to register handle, error %d\n",
  293. __func__, err);
  294. goto err_register_handle;
  295. }
  296. err = input_open_device(&hi->handle);
  297. if (err) {
  298. pr_err("%s: Failed to open input device, error %d\n",
  299. __func__, err);
  300. goto err_open_device;
  301. }
  302. for (i = 0; i < num_buttons_zones; i++)
  303. input_set_capability(dev, EV_KEY, btn_zones[i].code);
  304. return 0;
  305. err_open_device:
  306. input_unregister_handle(&hi->handle);
  307. err_register_handle:
  308. return err;
  309. }
  310. static void sec_jack_buttons_disconnect(struct input_handle *handle)
  311. {
  312. input_close_device(handle);
  313. input_unregister_handle(handle);
  314. }
  315. static void sec_jack_set_type(struct sec_jack_info *hi, int jack_type)
  316. {
  317. //struct sec_jack_platform_data *pdata = hi->pdata;
  318. /* this can happen during slow inserts where we think we identified
  319. * the type but then we get another interrupt and do it again
  320. */
  321. if (jack_type == hi->cur_jack_type) {
  322. #if defined(CONFIG_MACH_KLTE_JPN)
  323. if ((jack_type != SEC_HEADSET_4POLE) || (jack_type != SEC_EXTERNAL_ANTENNA))
  324. #else
  325. if (jack_type != SEC_HEADSET_4POLE)
  326. #endif
  327. set_sec_micbias_state(hi, false);
  328. return;
  329. }
  330. #if defined(CONFIG_MACH_KLTE_JPN)
  331. if ((jack_type == SEC_HEADSET_4POLE) || (jack_type == SEC_EXTERNAL_ANTENNA)) {
  332. #else
  333. if (jack_type == SEC_HEADSET_4POLE) {
  334. #endif
  335. /* for a 4 pole headset, enable detection of send/end key */
  336. if (hi->send_key_dev == NULL)
  337. /* enable to get events again */
  338. hi->send_key_dev = platform_device_register_data(NULL,
  339. GPIO_EVENT_DEV_NAME,
  340. hi->dev_id,
  341. &sec_jack_input_data,
  342. sizeof(sec_jack_input_data));
  343. mod_timer(&hi->timer,
  344. jiffies + msecs_to_jiffies(1000));
  345. } else {
  346. /* for all other jacks, disable send/end key detection */
  347. if (hi->send_key_dev != NULL) {
  348. /* disable to prevent false events on next insert */
  349. platform_device_unregister(hi->send_key_dev);
  350. hi->send_key_dev = NULL;
  351. del_timer_sync(&hi->timer);
  352. hi->buttons_enable = false;
  353. }
  354. /* micbias is left enabled for 4pole and disabled otherwise */
  355. set_sec_micbias_state(hi, false);
  356. }
  357. hi->cur_jack_type = jack_type;
  358. pr_info("%s : jack_type = %d\n", __func__, jack_type);
  359. #if defined(CONFIG_MACH_KLTE_JPN)
  360. if (jack_type == SEC_EXTERNAL_ANTENNA) {
  361. if(++detect_count > MAX_DETECT_LIMIT) {
  362. detect_count = 0;
  363. return;
  364. }
  365. else {
  366. int time_left_ms = 100;
  367. while (time_left_ms > 0) {
  368. usleep_range(10000, 10000);
  369. time_left_ms -= 10;
  370. }
  371. hi->cur_jack_type = SEC_JACK_NO_DEVICE;
  372. determine_jack_type(hi);
  373. return;
  374. }
  375. }
  376. else
  377. detect_count = 0;
  378. #endif
  379. switch_set_state(&switch_jack_detection, jack_type);
  380. }
  381. static void handle_jack_not_inserted(struct sec_jack_info *hi)
  382. {
  383. sec_jack_set_type(hi, SEC_JACK_NO_DEVICE);
  384. set_sec_micbias_state(hi, false);
  385. }
  386. static void determine_jack_type(struct sec_jack_info *hi)
  387. {
  388. struct sec_jack_platform_data *pdata = hi->pdata;
  389. struct sec_jack_zone *zones = pdata->jack_zones;
  390. int size = ARRAY_SIZE(pdata->jack_zones);
  391. int count[MAX_ZONE_LIMIT] = {0};
  392. int adc;
  393. int i;
  394. unsigned npolarity = !pdata->det_active_high;
  395. /* set mic bias to enable adc */
  396. set_sec_micbias_state(hi, true);
  397. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  398. mpp_control(1);
  399. #endif
  400. while (gpio_get_value(pdata->det_gpio) ^ npolarity) {
  401. adc = sec_jack_get_adc_value(hi);
  402. pr_info("%s: adc = %d\n", __func__, adc);
  403. /* determine the type of headset based on the
  404. * adc value. An adc value can fall in various
  405. * ranges or zones. Within some ranges, the type
  406. * can be returned immediately. Within others, the
  407. * value is considered unstable and we need to sample
  408. * a few more types (up to the limit determined by
  409. * the range) before we return the type for that range.
  410. */
  411. for (i = 0; i < size; i++) {
  412. if (adc <= zones[i].adc_high) {
  413. if (++count[i] > zones[i].check_count) {
  414. sec_jack_set_type(hi,
  415. zones[i].jack_type);
  416. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  417. mpp_control(0);
  418. #endif
  419. return;
  420. }
  421. if (zones[i].delay_us > 0)
  422. usleep_range(zones[i].delay_us, zones[i].delay_us);
  423. break;
  424. }
  425. }
  426. }
  427. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  428. mpp_control(0);
  429. #endif
  430. /* jack removed before detection complete */
  431. pr_debug("%s : jack removed before detection complete\n", __func__);
  432. handle_jack_not_inserted(hi);
  433. }
  434. static ssize_t key_state_onoff_show(struct device *dev,
  435. struct device_attribute *attr, char *buf)
  436. {
  437. struct sec_jack_info *hi = dev_get_drvdata(dev);
  438. int value = 0;
  439. if (hi->pressed && hi->pressed_code == KEY_MEDIA)
  440. value = 1;
  441. return snprintf(buf, 4, "%d\n", value);
  442. }
  443. static DEVICE_ATTR(key_state, 0664 , key_state_onoff_show,
  444. NULL);
  445. static ssize_t earjack_state_onoff_show(struct device *dev,
  446. struct device_attribute *attr, char *buf)
  447. {
  448. struct sec_jack_info *hi = dev_get_drvdata(dev);
  449. int value = 0;
  450. if (hi->cur_jack_type == SEC_HEADSET_4POLE)
  451. value = 1;
  452. return snprintf(buf, 4, "%d\n", value);
  453. }
  454. static DEVICE_ATTR(state, 0664 , earjack_state_onoff_show,
  455. NULL);
  456. #if defined(CONFIG_SAMSUNG_JACK_READ_BTN_ADC)
  457. static ssize_t mic_adc_show(struct device *dev,
  458. struct device_attribute *attr, char *buf)
  459. {
  460. int value = 0;
  461. value = ear_adc_value;
  462. return snprintf(buf, 10, "%d\n", value);
  463. }
  464. static DEVICE_ATTR(mic_adc, 0444 , mic_adc_show, NULL);
  465. #endif
  466. #if defined (CONFIG_EARJACK_ADC_SYSFS)
  467. static ssize_t jack_adc_show(struct device *dev,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. struct sec_jack_info *hi = dev_get_drvdata(dev);
  471. int val[4] = {0,};
  472. val[0] = hi->pdata->jack_zones[0].adc_high;
  473. val[1] = hi->pdata->jack_zones[1].adc_high;
  474. val[2] = hi->pdata->jack_zones[2].adc_high;
  475. val[3] = hi->pdata->jack_zones[3].adc_high;
  476. return sprintf(buf, "%d %d %d %d\n",val[0],val[1],val[2],val[3]);
  477. }
  478. static ssize_t jack_adc_store(struct device *dev,
  479. struct device_attribute *attr, const char *buf, size_t count)
  480. {
  481. struct sec_jack_info *hi = dev_get_drvdata(dev);
  482. char *buffer = (char*)buf;
  483. char *token;
  484. int val[4] = {0,};
  485. token = strsep(&buffer, " ");
  486. if(kstrtoint(token,0,&val[0]) != 0)
  487. return -EINVAL;
  488. token = strsep(&buffer, " ");
  489. if(kstrtoint(token,0,&val[1]) != 0)
  490. return -EINVAL;
  491. token = strsep(&buffer, " ");
  492. if(kstrtoint(token,0,&val[2]) != 0)
  493. return -EINVAL;
  494. token = strsep(&buffer, " ");
  495. if(kstrtoint(token,0,&val[3]) != 0)
  496. return -EINVAL;
  497. hi->pdata->jack_zones[0].adc_high = val[0];
  498. hi->pdata->jack_zones[1].adc_high = val[1];
  499. hi->pdata->jack_zones[2].adc_high = val[2];
  500. hi->pdata->jack_zones[3].adc_high = val[3];
  501. return count;
  502. }
  503. static DEVICE_ATTR(jack_adc, 0666, jack_adc_show,
  504. jack_adc_store);
  505. static ssize_t send_end_btn_adc_show(struct device *dev,
  506. struct device_attribute *attr, char *buf)
  507. {
  508. struct sec_jack_info *hi = dev_get_drvdata(dev);
  509. int val[2] = {0,};
  510. val[0] = hi->pdata->jack_buttons_zones[0].adc_low;
  511. val[1] = hi->pdata->jack_buttons_zones[0].adc_high;
  512. return sprintf(buf, "%d %d\n",val[0],val[1]);
  513. }
  514. static ssize_t send_end_btn_adc_store(struct device *dev,
  515. struct device_attribute *attr, const char *buf, size_t count)
  516. {
  517. struct sec_jack_info *hi = dev_get_drvdata(dev);
  518. char *buffer = (char*)buf;
  519. char *token;
  520. int val[2] = {0,};
  521. token = strsep(&buffer, " ");
  522. if(kstrtoint(token,0,&val[0]) != 0)
  523. return -EINVAL;
  524. token = strsep(&buffer, " ");
  525. if(kstrtoint(token,0,&val[1]) != 0)
  526. return -EINVAL;
  527. hi->pdata->jack_buttons_zones[0].adc_low = val[0];
  528. hi->pdata->jack_buttons_zones[0].adc_high = val[1];
  529. return count;
  530. }
  531. static DEVICE_ATTR(send_end_btn_adc, 0666, send_end_btn_adc_show,
  532. send_end_btn_adc_store);
  533. static ssize_t vol_up_btn_adc_show(struct device *dev,
  534. struct device_attribute *attr, char *buf)
  535. {
  536. struct sec_jack_info *hi = dev_get_drvdata(dev);
  537. int val[2] = {0,};
  538. val[0] = hi->pdata->jack_buttons_zones[1].adc_low;
  539. val[1] = hi->pdata->jack_buttons_zones[1].adc_high;
  540. return sprintf(buf, "%d %d\n",val[0],val[1]);
  541. }
  542. static ssize_t vol_up_btn_adc_store(struct device *dev,
  543. struct device_attribute *attr, const char *buf, size_t count)
  544. {
  545. struct sec_jack_info *hi = dev_get_drvdata(dev);
  546. char *buffer = (char*)buf;
  547. char *token;
  548. int val[2] = {0,};
  549. token = strsep(&buffer, " ");
  550. if(kstrtoint(token,0,&val[0]) != 0)
  551. return -EINVAL;
  552. token = strsep(&buffer, " ");
  553. if(kstrtoint(token,0,&val[1]) != 0)
  554. return -EINVAL;
  555. hi->pdata->jack_buttons_zones[1].adc_low = val[0];
  556. hi->pdata->jack_buttons_zones[1].adc_high = val[1];
  557. return count;
  558. }
  559. static DEVICE_ATTR(vol_up_btn_adc, 0666, vol_up_btn_adc_show,
  560. vol_up_btn_adc_store);
  561. static ssize_t vol_down_btn_adc_show(struct device *dev,
  562. struct device_attribute *attr, char *buf)
  563. {
  564. struct sec_jack_info *hi = dev_get_drvdata(dev);
  565. int val[2] = {0,};
  566. val[0] = hi->pdata->jack_buttons_zones[2].adc_low;
  567. val[1] = hi->pdata->jack_buttons_zones[2].adc_high;
  568. return sprintf(buf, "%d %d\n",val[0],val[1]);
  569. }
  570. static ssize_t vol_down_btn_adc_store(struct device *dev,
  571. struct device_attribute *attr, const char *buf, size_t count)
  572. {
  573. struct sec_jack_info *hi = dev_get_drvdata(dev);
  574. char *buffer = (char*)buf;
  575. char *token;
  576. int val[2] = {0,};
  577. token = strsep(&buffer, " ");
  578. if(kstrtoint(token,0,&val[0]) != 0)
  579. return -EINVAL;
  580. token = strsep(&buffer, " ");
  581. if(kstrtoint(token,0,&val[1]) != 0)
  582. return -EINVAL;
  583. hi->pdata->jack_buttons_zones[2].adc_low = val[0];
  584. hi->pdata->jack_buttons_zones[2].adc_high = val[1];
  585. return count;
  586. }
  587. static DEVICE_ATTR(vol_down_btn_adc, 0666, vol_down_btn_adc_show,
  588. vol_down_btn_adc_store);
  589. #endif
  590. static void sec_jack_timer_handler(unsigned long data)
  591. {
  592. struct sec_jack_info *hi = (struct sec_jack_info *)data;
  593. hi->buttons_enable = true;
  594. }
  595. /* thread run whenever the headset detect state changes (either insertion
  596. * or removal).
  597. */
  598. static irqreturn_t sec_jack_detect_irq(int irq, void *dev_id)
  599. {
  600. #if defined(CONFIG_MACH_S3VE3G_EUR)
  601. struct sec_jack_info *hi = dev_id;
  602. disable_irq_wake(hi->det_irq);
  603. queue_work(hi->queue, &hi->detect_work);
  604. mdelay(100);
  605. enable_irq_wake(hi->det_irq);
  606. pr_info("%s: Debug code for removing flooding of jack irq\n", __func__);
  607. return IRQ_HANDLED;
  608. #else
  609. struct sec_jack_info *hi = dev_id;
  610. queue_work(hi->queue, &hi->detect_work);
  611. return IRQ_HANDLED;
  612. #endif
  613. }
  614. void sec_jack_detect_work(struct work_struct *work)
  615. {
  616. struct sec_jack_info *hi =
  617. container_of(work, struct sec_jack_info, detect_work);
  618. struct sec_jack_platform_data *pdata = hi->pdata;
  619. unsigned npolarity = !hi->pdata->det_active_high;
  620. int time_left_ms;
  621. if (pdata->fsa_en_gpio < 0)
  622. time_left_ms = DET_CHECK_TIME_MS;
  623. else
  624. time_left_ms = DET_CHECK_TIME_MS_WITH_FSA;
  625. /* prevent suspend to allow user space to respond to switch */
  626. wake_lock_timeout(&hi->det_wake_lock, WAKE_LOCK_TIME);
  627. pr_info("%s: detect_irq(%d)\n", __func__,
  628. gpio_get_value(pdata->det_gpio) ^ npolarity);
  629. /* debounce headset jack. don't try to determine the type of
  630. * headset until the detect state is true for a while.
  631. */
  632. while (time_left_ms > 0) {
  633. if (!(gpio_get_value(hi->pdata->det_gpio) ^ npolarity)) {
  634. /* jack not detected. */
  635. handle_jack_not_inserted(hi);
  636. return;
  637. }
  638. usleep_range(10000, 10000);
  639. time_left_ms -= 10;
  640. }
  641. /* jack presence was detected the whole time, figure out which type */
  642. determine_jack_type(hi);
  643. }
  644. /* thread run whenever the button of headset is pressed or released */
  645. void sec_jack_buttons_work(struct work_struct *work)
  646. {
  647. struct sec_jack_info *hi =
  648. container_of(work, struct sec_jack_info, buttons_work);
  649. struct sec_jack_platform_data *pdata = hi->pdata;
  650. struct sec_jack_buttons_zone *btn_zones = pdata->jack_buttons_zones;
  651. int num_buttons_zones = ARRAY_SIZE(pdata->jack_buttons_zones);
  652. int adc;
  653. int i;
  654. #if defined(CONFIG_SAMSUNG_JACK_READ_BTN_ADC)
  655. ear_adc_value = 0;
  656. #endif
  657. if (!hi->buttons_enable) {
  658. pr_info("%s: BTN %d is skipped\n", __func__,
  659. hi->pressed_code);
  660. return;
  661. }
  662. /* prevent suspend to allow user space to respond to switch */
  663. wake_lock_timeout(&hi->det_wake_lock, WAKE_LOCK_TIME);
  664. /* when button is released */
  665. if (hi->pressed == 0) {
  666. input_report_key(hi->input_dev, hi->pressed_code, 0);
  667. input_sync(hi->input_dev);
  668. switch_set_state(&switch_sendend, 0);
  669. pr_info("%s: BTN %d is released\n", __func__,
  670. hi->pressed_code);
  671. return;
  672. }
  673. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  674. mpp_control(1);
  675. #endif
  676. /* when button is pressed */
  677. adc = sec_jack_get_adc_value(hi);
  678. #if defined(CONFIG_SAMSUNG_JACK_READ_BTN_ADC)
  679. ear_adc_value = adc;
  680. #endif
  681. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  682. mpp_control(0);
  683. #endif
  684. #if defined(CONFIG_MACH_KLTE_JPN)
  685. if (hi->cur_jack_type == SEC_EXTERNAL_ANTENNA) {
  686. int time_left_ms = 1000;
  687. while (time_left_ms > 0) {
  688. usleep_range(10000, 10000);
  689. time_left_ms -= 10;
  690. }
  691. hi->cur_jack_type = SEC_JACK_NO_DEVICE;
  692. determine_jack_type(hi);
  693. return;
  694. }
  695. #endif
  696. for (i = 0; i < num_buttons_zones; i++)
  697. if (adc >= btn_zones[i].adc_low &&
  698. adc <= btn_zones[i].adc_high) {
  699. hi->pressed_code = btn_zones[i].code;
  700. input_report_key(hi->input_dev, btn_zones[i].code, 1);
  701. input_sync(hi->input_dev);
  702. switch_set_state(&switch_sendend, 1);
  703. pr_info("%s: adc = %d, BTN %d is pressed\n", __func__,
  704. adc, btn_zones[i].code);
  705. return;
  706. }
  707. pr_warn("%s: key is skipped. ADC value is %d\n", __func__, adc);
  708. }
  709. static struct sec_jack_platform_data *sec_jack_populate_dt_pdata(struct device *dev)
  710. {
  711. struct sec_jack_platform_data *pdata;
  712. struct of_phandle_args args;
  713. int i = 0;
  714. int ret = 0;
  715. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  716. if (!pdata) {
  717. pr_err("%s : could not allocate memory for platform data\n", __func__);
  718. goto alloc_err;
  719. }
  720. pdata->det_gpio = of_get_named_gpio(dev->of_node, "qcom,earjack-detect-gpio", 0);
  721. if (pdata->det_gpio < 0) {
  722. pr_err("%s : can not find the earjack-detect-gpio in the dt\n", __func__);
  723. } else
  724. pr_info("%s : earjack-detect-gpio =%d\n", __func__, pdata->det_gpio);
  725. pdata->send_end_gpio = of_get_named_gpio(dev->of_node, "qcom,earjack-sendend-gpio", 0);
  726. if (pdata->send_end_gpio < 0) {
  727. pr_err("%s : can not find the earjack-detect-gpio in the dt\n", __func__);
  728. } else
  729. pr_info("%s : earjack-sendend-gpio =%d\n", __func__, pdata->send_end_gpio);
  730. pdata->ear_micbias_gpio = of_get_named_gpio(dev->of_node, "qcom,earjack-micbias-gpio", 0);
  731. if (pdata->ear_micbias_gpio < 0)
  732. of_property_read_u32(dev->of_node, "qcom,earjack-micbias-expander-gpio", &pdata->ear_micbias_gpio);
  733. if (pdata->ear_micbias_gpio < 0) {
  734. pr_err("%s : can not find the earjack-micbias-gpio in the dt\n", __func__);
  735. if (of_property_read_string(dev->of_node, "qcom,earjack-micbias-ldo", &pdata->ear_micbias_ldo) < 0)
  736. pr_err("%s: can not find earjack-micbias-ldo in the dt\n", __func__);
  737. else
  738. pr_info("%s : earjack-micbias-ldo=%s\n", __func__, pdata->ear_micbias_ldo);
  739. } else
  740. pr_info("%s : earjack-micbias-gpio =%d\n", __func__, pdata->ear_micbias_gpio);
  741. pdata->fsa_en_gpio = of_get_named_gpio(dev->of_node, "qcom,earjack-fsa_en-gpio", 0);
  742. if (pdata->fsa_en_gpio < 0)
  743. of_property_read_u32(dev->of_node, "qcom,earjack-fsa_en-expander-gpio", &pdata->fsa_en_gpio);
  744. if (pdata->fsa_en_gpio < 0)
  745. pr_info("%s : No support FSA8038 chip\n", __func__);
  746. else
  747. pr_info("%s : earjack-fsa_en-gpio =%d\n", __func__, pdata->fsa_en_gpio);
  748. for( i=0; i<4; i++)
  749. {
  750. of_parse_phandle_with_args(dev->of_node, "det-zones-list","#list-det-cells", i, &args);
  751. pdata->jack_zones[i].adc_high = args.args[0];
  752. pdata->jack_zones[i].delay_us = args.args[1];
  753. pdata->jack_zones[i].check_count = args.args[2];
  754. #if defined(CONFIG_MACH_KLTE_JPN)
  755. pdata->jack_zones[i].jack_type = args.args[3];
  756. #else
  757. pdata->jack_zones[i].jack_type = args.args[3]==0?SEC_HEADSET_3POLE:SEC_HEADSET_4POLE;
  758. #endif
  759. pr_info("%s : %d, %d, %d, %d, %d \n",
  760. __func__, args.args_count, args.args[0],
  761. args.args[1], args.args[2],args.args[3]);
  762. }
  763. #if defined(CONFIG_SAMSUNG_JACK_VOICE_BTN)
  764. for( i=0; i<4; i++)
  765. #else
  766. for( i=0; i<3; i++)
  767. #endif
  768. {
  769. of_parse_phandle_with_args(dev->of_node, "but-zones-list","#list-but-cells", i, &args);
  770. #if defined(CONFIG_SAMSUNG_JACK_VOICE_BTN)
  771. pdata->jack_buttons_zones[i].code = args.args[0]==0?KEY_MEDIA:args.args[0]==1?KEY_VOLUMEUP:args.args[0]==2?KEY_VOLUMEDOWN:KEY_VOICECOMMAND;
  772. #else
  773. pdata->jack_buttons_zones[i].code = args.args[0]==0?KEY_MEDIA:args.args[0]==1?KEY_VOLUMEUP:KEY_VOLUMEDOWN;
  774. #endif
  775. pdata->jack_buttons_zones[i].adc_low = args.args[1];
  776. pdata->jack_buttons_zones[i].adc_high = args.args[2];
  777. pr_info("%s : %d, %d, %d, %d\n",
  778. __func__, args.args_count, args.args[0],
  779. args.args[1], args.args[2]);
  780. }
  781. if (of_find_property(dev->of_node, "qcom,send-end-active-high", NULL))
  782. pdata->send_end_active_high = true;
  783. ret = of_property_read_u32_array(dev->of_node, "mpp-channel-scaling", pdata->mpp_ch_scale, 3);
  784. if (ret < 0) {
  785. pr_err("%s : cannot find mpp-channel-scaling in the dt - using default MPP6_1_3\n",
  786. __func__);
  787. pdata->mpp_ch_scale[0] = 6;
  788. pdata->mpp_ch_scale[1] = 1;
  789. pdata->mpp_ch_scale[2] = 3;
  790. }
  791. pr_info("%s - mpp-channel-scaling - %d %d %d\n", __func__, pdata->mpp_ch_scale[0], pdata->mpp_ch_scale[1], pdata->mpp_ch_scale[2]);
  792. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  793. pm8941_mpp4 = of_get_named_gpio(dev->of_node, "pm8941-mpp4", 0);
  794. #endif
  795. #ifdef CONFIG_ARCH_MSM8226
  796. /*Reading vadc ptr from dtsi File*/
  797. pdata->vadc_dev = qpnp_get_vadc(dev, "earjack");
  798. if (IS_ERR(pdata->vadc_dev)) {
  799. int rc;
  800. rc = PTR_ERR(pdata->vadc_dev);
  801. if (rc != -EPROBE_DEFER)
  802. pr_err("vadc property missing, rc=%d\n", rc);
  803. goto alloc_err;
  804. }
  805. #endif
  806. return pdata;
  807. alloc_err:
  808. devm_kfree(dev, pdata);
  809. return NULL;
  810. }
  811. #ifdef CONFIG_ARCH_MSM8226
  812. extern bool is_codec_probe_done(void);
  813. #endif
  814. static int sec_jack_probe(struct platform_device *pdev)
  815. {
  816. struct sec_jack_info *hi;
  817. struct sec_jack_platform_data *pdata;
  818. struct class *audio;
  819. struct device *earjack;
  820. int ret;
  821. #ifdef CONFIG_ARCH_MSM8226
  822. if(!is_codec_probe_done()) {
  823. pr_err("%s - defer as codec_probe_done is false\n", __func__);
  824. return -EPROBE_DEFER;
  825. }
  826. #endif
  827. pr_info("%s : Registering jack driver\n", __func__);
  828. pdata = sec_jack_populate_dt_pdata(&pdev->dev);
  829. if (!pdata) {
  830. pr_err("%s : pdata is NULL.\n", __func__);
  831. return -ENODEV;
  832. } else
  833. sec_jack_gpio_init(pdata);
  834. if (atomic_xchg(&instantiated, 1)) {
  835. pr_err("%s : already instantiated, can only have one\n",
  836. __func__);
  837. return -ENODEV;
  838. }
  839. sec_jack_key_map[0].gpio = pdata->send_end_gpio;
  840. hi = kzalloc(sizeof(struct sec_jack_info), GFP_KERNEL);
  841. if (hi == NULL) {
  842. pr_err("%s : Failed to allocate memory.\n", __func__);
  843. ret = -ENOMEM;
  844. goto err_kzalloc;
  845. }
  846. hi->pdata = pdata;
  847. /* make the id of our gpi_event device the same as our platform device,
  848. * which makes it the responsiblity of the board file to make sure
  849. * it is unique relative to other gpio_event devices
  850. */
  851. hi->dev_id = pdev->id;
  852. /* to read the vadc */
  853. hi->client = pdev;
  854. #ifdef CONFIG_ARCH_MSM8226
  855. if(pdata->det_gpio > 0) {
  856. ret = gpio_request(pdata->det_gpio, "ear_jack_detect");
  857. if (ret) {
  858. pr_err("%s : gpio_request failed for %d\n",
  859. __func__, pdata->det_gpio);
  860. goto err_gpio_request;
  861. }
  862. }
  863. #else
  864. ret = gpio_request(pdata->det_gpio, "ear_jack_detect");
  865. if (ret) {
  866. pr_err("%s : gpio_request failed for %d\n",
  867. __func__, pdata->det_gpio);
  868. goto err_gpio_request;
  869. }
  870. #endif
  871. ret = switch_dev_register(&switch_jack_detection);
  872. if (ret < 0) {
  873. pr_err("%s : Failed to register switch device\n", __func__);
  874. goto err_switch_dev_register;
  875. }
  876. ret = switch_dev_register(&switch_sendend);
  877. if (ret < 0) {
  878. pr_err("%s : Failed to register switch device\n", __func__);
  879. goto err_switch_dev_register;
  880. }
  881. wake_lock_init(&hi->det_wake_lock, WAKE_LOCK_SUSPEND, "sec_jack_det");
  882. audio = class_create(THIS_MODULE, "audio");
  883. if (IS_ERR(audio))
  884. pr_err("Failed to create class(audio)!\n");
  885. earjack = device_create(audio, NULL, 0, NULL, "earjack");
  886. if (IS_ERR(earjack))
  887. pr_err("Failed to create device(earjack)!\n");
  888. ret = device_create_file(earjack, &dev_attr_key_state);
  889. if (ret)
  890. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  891. dev_attr_key_state.attr.name);
  892. ret = device_create_file(earjack, &dev_attr_state);
  893. if (ret)
  894. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  895. dev_attr_state.attr.name);
  896. #if defined(CONFIG_SAMSUNG_JACK_READ_BTN_ADC)
  897. ret = device_create_file(earjack, &dev_attr_mic_adc);
  898. if (ret)
  899. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  900. dev_attr_key_state.attr.name);
  901. #endif
  902. #if defined (CONFIG_EARJACK_ADC_SYSFS)
  903. ret = device_create_file(earjack, &dev_attr_jack_adc);
  904. if (ret)
  905. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  906. dev_attr_jack_adc.attr.name);
  907. ret = device_create_file(earjack, &dev_attr_send_end_btn_adc);
  908. if (ret)
  909. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  910. dev_attr_send_end_btn_adc.attr.name);
  911. ret = device_create_file(earjack, &dev_attr_vol_up_btn_adc);
  912. if (ret)
  913. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  914. dev_attr_vol_up_btn_adc.attr.name);
  915. ret = device_create_file(earjack, &dev_attr_vol_down_btn_adc);
  916. if (ret)
  917. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  918. dev_attr_vol_down_btn_adc.attr.name);
  919. #endif
  920. setup_timer(&hi->timer, sec_jack_timer_handler, (unsigned long)hi);
  921. INIT_WORK(&hi->buttons_work, sec_jack_buttons_work);
  922. INIT_WORK(&hi->detect_work, sec_jack_detect_work);
  923. hi->queue = create_singlethread_workqueue("sec_jack_wq");
  924. if (hi->queue == NULL) {
  925. ret = -ENOMEM;
  926. pr_err("%s: Failed to create workqueue\n", __func__);
  927. goto err_create_wq_failed;
  928. }
  929. hi->buttons_queue = create_singlethread_workqueue("sec_jack_buttons_wq");
  930. if (hi->buttons_queue == NULL) {
  931. ret = -ENOMEM;
  932. pr_err("%s: Failed to create buttons workqueue\n", __func__);
  933. goto err_create_buttons_wq_failed;
  934. }
  935. queue_work(hi->queue, &hi->detect_work);
  936. hi->det_irq = gpio_to_irq(pdata->det_gpio);
  937. set_bit(EV_KEY, hi->ids[0].evbit);
  938. hi->ids[0].flags = INPUT_DEVICE_ID_MATCH_EVBIT;
  939. hi->handler.filter = sec_jack_buttons_filter;
  940. hi->handler.connect = sec_jack_buttons_connect;
  941. hi->handler.disconnect = sec_jack_buttons_disconnect;
  942. hi->handler.name = "sec_jack_buttons";
  943. hi->handler.id_table = hi->ids;
  944. hi->handler.private = hi;
  945. ret = input_register_handler(&hi->handler);
  946. if (ret) {
  947. pr_err("%s : Failed to register_handler\n", __func__);
  948. goto err_register_input_handler;
  949. }
  950. /* We are going to remove this code later */
  951. if (pdata->send_end_active_high == true)
  952. sec_jack_key_info.flags = 1;
  953. ret = request_irq(hi->det_irq, sec_jack_detect_irq,
  954. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
  955. IRQF_ONESHOT, "sec_headset_detect", hi);
  956. if (ret) {
  957. pr_err("%s : Failed to request_irq.\n", __func__);
  958. goto err_request_detect_irq;
  959. }
  960. /* to handle insert/removal when we're sleeping in a call */
  961. ret = enable_irq_wake(hi->det_irq);
  962. if (ret) {
  963. pr_err("%s : Failed to enable_irq_wake.\n", __func__);
  964. goto err_enable_irq_wake;
  965. }
  966. dev_set_drvdata(&pdev->dev, hi);
  967. dev_set_drvdata(earjack, hi);
  968. #if defined(CONFIG_MACH_VIENNA) || defined(CONFIG_MACH_PICASSO) || defined(CONFIG_MACH_MONDRIAN) || defined(CONFIG_MACH_LT03) || defined(CONFIG_SEC_H_PROJECT) || defined(CONFIG_SEC_FRESCO_PROJECT)
  969. mpp_control(0);
  970. #endif
  971. return 0;
  972. err_enable_irq_wake:
  973. free_irq(hi->det_irq, hi);
  974. err_request_detect_irq:
  975. input_unregister_handler(&hi->handler);
  976. err_register_input_handler:
  977. destroy_workqueue(hi->buttons_queue);
  978. err_create_buttons_wq_failed:
  979. destroy_workqueue(hi->queue);
  980. err_create_wq_failed:
  981. device_remove_file(earjack, &dev_attr_state);
  982. device_remove_file(earjack, &dev_attr_key_state);
  983. #if defined(CONFIG_SAMSUNG_JACK_READ_BTN_ADC)
  984. device_remove_file(earjack, &dev_attr_mic_adc);
  985. #endif
  986. #if defined (CONFIG_EARJACK_ADC_SYSFS)
  987. device_remove_file(earjack, &dev_attr_jack_adc);
  988. device_remove_file(earjack, &dev_attr_send_end_btn_adc);
  989. device_remove_file(earjack, &dev_attr_vol_up_btn_adc);
  990. device_remove_file(earjack, &dev_attr_vol_down_btn_adc);
  991. #endif
  992. device_destroy(audio, 0);
  993. class_destroy(audio);
  994. wake_lock_destroy(&hi->det_wake_lock);
  995. switch_dev_unregister(&switch_jack_detection);
  996. switch_dev_unregister(&switch_sendend);
  997. err_switch_dev_register:
  998. gpio_free(pdata->det_gpio);
  999. err_gpio_request:
  1000. kfree(hi);
  1001. err_kzalloc:
  1002. atomic_set(&instantiated, 0);
  1003. return ret;
  1004. }
  1005. static int sec_jack_remove(struct platform_device *pdev)
  1006. {
  1007. struct sec_jack_info *hi = dev_get_drvdata(&pdev->dev);
  1008. pr_info("%s :\n", __func__);
  1009. disable_irq_wake(hi->det_irq);
  1010. free_irq(hi->det_irq, hi);
  1011. destroy_workqueue(hi->queue);
  1012. destroy_workqueue(hi->buttons_queue);
  1013. if (hi->send_key_dev) {
  1014. platform_device_unregister(hi->send_key_dev);
  1015. hi->send_key_dev = NULL;
  1016. }
  1017. input_unregister_handler(&hi->handler);
  1018. wake_lock_destroy(&hi->det_wake_lock);
  1019. switch_dev_unregister(&switch_jack_detection);
  1020. switch_dev_unregister(&switch_sendend);
  1021. gpio_free(hi->pdata->det_gpio);
  1022. kfree(hi);
  1023. atomic_set(&instantiated, 0);
  1024. return 0;
  1025. }
  1026. static const struct of_device_id sec_jack_dt_match[] = {
  1027. { .compatible = "sec_jack" },
  1028. { }
  1029. };
  1030. MODULE_DEVICE_TABLE(of, sec_jack_dt_match);
  1031. static struct platform_driver sec_jack_driver = {
  1032. .probe = sec_jack_probe,
  1033. .remove = sec_jack_remove,
  1034. .driver = {
  1035. .name = "sec_jack",
  1036. .owner = THIS_MODULE,
  1037. .of_match_table = sec_jack_dt_match,
  1038. },
  1039. };
  1040. #if defined(CONFIG_MACH_KLTE_KOR) || defined(CONFIG_MACH_KLTE_JPN)
  1041. extern unsigned int system_rev;
  1042. #endif
  1043. static int __init sec_jack_init(void)
  1044. {
  1045. #if defined(CONFIG_MACH_KLTE_KOR)
  1046. if (system_rev >= 13) {
  1047. pr_info("%s: Do not use sec jack in system_rev %d",
  1048. __func__, system_rev);
  1049. return 0;
  1050. } else {
  1051. return platform_driver_register(&sec_jack_driver);
  1052. }
  1053. #elif defined(CONFIG_MACH_KLTE_JPN)
  1054. if (system_rev >= 11) {
  1055. pr_info("%s: Do not use sec jack in system_rev %d",
  1056. __func__, system_rev);
  1057. return 0;
  1058. } else {
  1059. return platform_driver_register(&sec_jack_driver);
  1060. }
  1061. #else
  1062. return platform_driver_register(&sec_jack_driver);
  1063. #endif
  1064. }
  1065. static void __exit sec_jack_exit(void)
  1066. {
  1067. platform_driver_unregister(&sec_jack_driver);
  1068. }
  1069. late_initcall(sec_jack_init);
  1070. module_exit(sec_jack_exit);
  1071. MODULE_AUTHOR("ms17.kim@samsung.com");
  1072. MODULE_DESCRIPTION("Samsung Electronics Corp Ear-Jack detection driver");
  1073. MODULE_LICENSE("GPL");