extcon-arizona.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /*
  2. * extcon-arizona.c - Extcon driver Wolfson Arizona devices
  3. *
  4. * Copyright (C) 2012 Wolfson Microelectronics plc
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/err.h>
  22. #include <linux/gpio.h>
  23. #include <linux/input.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/extcon.h>
  28. #include <sound/soc.h>
  29. #include <linux/mfd/arizona/core.h>
  30. #include <linux/mfd/arizona/pdata.h>
  31. #include <linux/mfd/arizona/registers.h>
  32. #define ARIZONA_MAX_MICD_RANGE 8
  33. #define ARIZONA_ACCDET_MODE_MIC 0
  34. #define ARIZONA_ACCDET_MODE_HPL 1
  35. #define ARIZONA_ACCDET_MODE_HPR 2
  36. #define ARIZONA_HPDET_MAX 10000
  37. #define HPDET_DEBOUNCE 500
  38. struct arizona_extcon_info {
  39. struct device *dev;
  40. struct arizona *arizona;
  41. struct mutex lock;
  42. struct regulator *micvdd;
  43. struct input_dev *input;
  44. u16 last_jackdet;
  45. int micd_mode;
  46. const struct arizona_micd_config *micd_modes;
  47. int micd_num_modes;
  48. const struct arizona_micd_range *micd_ranges;
  49. int num_micd_ranges;
  50. bool micd_reva;
  51. bool micd_clamp;
  52. struct delayed_work hpdet_work;
  53. bool hpdet_active;
  54. bool hpdet_done;
  55. bool hpdet_retried;
  56. int num_hpdet_res;
  57. unsigned int hpdet_res[3];
  58. bool mic;
  59. bool detecting;
  60. int jack_flips;
  61. int hpdet_ip;
  62. struct extcon_dev edev;
  63. };
  64. static const struct arizona_micd_config micd_default_modes[] = {
  65. { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
  66. { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
  67. };
  68. static const struct arizona_micd_range micd_default_ranges[] = {
  69. { .max = 11, .key = BTN_0 },
  70. { .max = 28, .key = BTN_1 },
  71. { .max = 54, .key = BTN_2 },
  72. { .max = 100, .key = BTN_3 },
  73. { .max = 186, .key = BTN_4 },
  74. { .max = 430, .key = BTN_5 },
  75. };
  76. static const int arizona_micd_levels[] = {
  77. 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
  78. 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
  79. 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
  80. 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
  81. 1257,
  82. };
  83. #define ARIZONA_CABLE_MECHANICAL 0
  84. #define ARIZONA_CABLE_MICROPHONE 1
  85. #define ARIZONA_CABLE_HEADPHONE 2
  86. #define ARIZONA_CABLE_LINEOUT 3
  87. static const char *arizona_cable[] = {
  88. "Mechanical",
  89. "Microphone",
  90. "Headphone",
  91. "Line-out",
  92. NULL,
  93. };
  94. static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
  95. static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
  96. unsigned int magic)
  97. {
  98. struct arizona *arizona = info->arizona;
  99. int ret;
  100. mutex_lock(&arizona->dapm->card->dapm_mutex);
  101. arizona->hpdet_magic = magic;
  102. /* Keep the HP output stages disabled while doing the magic */
  103. if (magic) {
  104. ret = regmap_update_bits(arizona->regmap,
  105. ARIZONA_OUTPUT_ENABLES_1,
  106. ARIZONA_OUT1L_ENA |
  107. ARIZONA_OUT1R_ENA, 0);
  108. if (ret != 0)
  109. dev_warn(arizona->dev,
  110. "Failed to disable headphone outputs: %d\n",
  111. ret);
  112. }
  113. ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
  114. magic);
  115. if (ret != 0)
  116. dev_warn(arizona->dev, "Failed to do magic: %d\n",
  117. ret);
  118. ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
  119. magic);
  120. if (ret != 0)
  121. dev_warn(arizona->dev, "Failed to do magic: %d\n",
  122. ret);
  123. /* Restore the desired state while not doing the magic */
  124. if (!magic) {
  125. ret = regmap_update_bits(arizona->regmap,
  126. ARIZONA_OUTPUT_ENABLES_1,
  127. ARIZONA_OUT1L_ENA |
  128. ARIZONA_OUT1R_ENA, arizona->hp_ena);
  129. if (ret != 0)
  130. dev_warn(arizona->dev,
  131. "Failed to restore headphone outputs: %d\n",
  132. ret);
  133. }
  134. mutex_unlock(&arizona->dapm->card->dapm_mutex);
  135. }
  136. static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
  137. {
  138. struct arizona *arizona = info->arizona;
  139. mode %= info->micd_num_modes;
  140. if (arizona->pdata.micd_pol_gpio > 0)
  141. gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
  142. info->micd_modes[mode].gpio);
  143. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  144. ARIZONA_MICD_BIAS_SRC_MASK,
  145. info->micd_modes[mode].bias);
  146. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  147. ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
  148. info->micd_mode = mode;
  149. dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
  150. }
  151. static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
  152. {
  153. switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
  154. case 1:
  155. return "MICBIAS1";
  156. case 2:
  157. return "MICBIAS2";
  158. case 3:
  159. return "MICBIAS3";
  160. default:
  161. return "MICVDD";
  162. }
  163. }
  164. static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
  165. {
  166. struct arizona *arizona = info->arizona;
  167. const char *widget = arizona_extcon_get_micbias(info);
  168. struct snd_soc_dapm_context *dapm = arizona->dapm;
  169. int ret;
  170. mutex_lock(&dapm->card->dapm_mutex);
  171. ret = snd_soc_dapm_force_enable_pin(dapm, widget);
  172. if (ret != 0)
  173. dev_warn(arizona->dev, "Failed to enable %s: %d\n",
  174. widget, ret);
  175. mutex_unlock(&dapm->card->dapm_mutex);
  176. snd_soc_dapm_sync(dapm);
  177. if (!arizona->pdata.micd_force_micbias) {
  178. mutex_lock(&dapm->card->dapm_mutex);
  179. ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
  180. if (ret != 0)
  181. dev_warn(arizona->dev, "Failed to disable %s: %d\n",
  182. widget, ret);
  183. mutex_unlock(&dapm->card->dapm_mutex);
  184. snd_soc_dapm_sync(dapm);
  185. }
  186. }
  187. static void arizona_start_mic(struct arizona_extcon_info *info)
  188. {
  189. struct arizona *arizona = info->arizona;
  190. bool change;
  191. int ret;
  192. /* Microphone detection can't use idle mode */
  193. pm_runtime_get(info->dev);
  194. if (info->detecting) {
  195. ret = regulator_allow_bypass(info->micvdd, false);
  196. if (ret != 0) {
  197. dev_err(arizona->dev,
  198. "Failed to regulate MICVDD: %d\n",
  199. ret);
  200. }
  201. }
  202. ret = regulator_enable(info->micvdd);
  203. if (ret != 0) {
  204. dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
  205. ret);
  206. }
  207. if (info->micd_reva) {
  208. regmap_write(arizona->regmap, 0x80, 0x3);
  209. regmap_write(arizona->regmap, 0x294, 0);
  210. regmap_write(arizona->regmap, 0x80, 0x0);
  211. }
  212. regmap_update_bits(arizona->regmap,
  213. ARIZONA_ACCESSORY_DETECT_MODE_1,
  214. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  215. arizona_extcon_pulse_micbias(info);
  216. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  217. ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
  218. &change);
  219. if (!change) {
  220. regulator_disable(info->micvdd);
  221. pm_runtime_put_autosuspend(info->dev);
  222. }
  223. }
  224. static void arizona_stop_mic(struct arizona_extcon_info *info)
  225. {
  226. struct arizona *arizona = info->arizona;
  227. const char *widget = arizona_extcon_get_micbias(info);
  228. struct snd_soc_dapm_context *dapm = arizona->dapm;
  229. bool change;
  230. int ret;
  231. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  232. ARIZONA_MICD_ENA, 0,
  233. &change);
  234. mutex_lock(&dapm->card->dapm_mutex);
  235. ret = snd_soc_dapm_disable_pin(dapm, widget);
  236. if (ret != 0)
  237. dev_warn(arizona->dev,
  238. "Failed to disable %s: %d\n",
  239. widget, ret);
  240. mutex_unlock(&dapm->card->dapm_mutex);
  241. snd_soc_dapm_sync(dapm);
  242. if (info->micd_reva) {
  243. regmap_write(arizona->regmap, 0x80, 0x3);
  244. regmap_write(arizona->regmap, 0x294, 2);
  245. regmap_write(arizona->regmap, 0x80, 0x0);
  246. }
  247. ret = regulator_allow_bypass(info->micvdd, true);
  248. if (ret != 0) {
  249. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
  250. ret);
  251. }
  252. if (change) {
  253. regulator_disable(info->micvdd);
  254. pm_runtime_mark_last_busy(info->dev);
  255. pm_runtime_put_autosuspend(info->dev);
  256. }
  257. }
  258. static struct {
  259. unsigned int factor_a;
  260. unsigned int factor_b;
  261. } arizona_hpdet_b_ranges[] = {
  262. { 5528, 362464 },
  263. { 11084, 6186851 },
  264. { 11065, 65460395 },
  265. };
  266. static struct {
  267. int min;
  268. int max;
  269. } arizona_hpdet_c_ranges[] = {
  270. { 0, 30 },
  271. { 8, 100 },
  272. { 100, 1000 },
  273. { 1000, 10000 },
  274. };
  275. static int arizona_hpdet_read(struct arizona_extcon_info *info)
  276. {
  277. struct arizona *arizona = info->arizona;
  278. unsigned int val, range;
  279. int ret;
  280. ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
  281. if (ret != 0) {
  282. dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
  283. ret);
  284. return ret;
  285. }
  286. switch (info->hpdet_ip) {
  287. case 0:
  288. if (!(val & ARIZONA_HP_DONE)) {
  289. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  290. val);
  291. return -EAGAIN;
  292. }
  293. val &= ARIZONA_HP_LVL_MASK;
  294. break;
  295. case 1:
  296. if (!(val & ARIZONA_HP_DONE_B)) {
  297. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  298. val);
  299. return -EAGAIN;
  300. }
  301. ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
  302. if (ret != 0) {
  303. dev_err(arizona->dev, "Failed to read HP value: %d\n",
  304. ret);
  305. return -EAGAIN;
  306. }
  307. regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  308. &range);
  309. range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
  310. >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
  311. if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
  312. (val < 100 || val > 0x3fb)) {
  313. range++;
  314. dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
  315. range);
  316. regmap_update_bits(arizona->regmap,
  317. ARIZONA_HEADPHONE_DETECT_1,
  318. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  319. range <<
  320. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  321. return -EAGAIN;
  322. }
  323. /* If we go out of range report top of range */
  324. if (val < 100 || val > 0x3fb) {
  325. dev_dbg(arizona->dev, "Measurement out of range\n");
  326. return ARIZONA_HPDET_MAX;
  327. }
  328. dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
  329. val, range);
  330. val = arizona_hpdet_b_ranges[range].factor_b
  331. / ((val * 100) -
  332. arizona_hpdet_b_ranges[range].factor_a);
  333. break;
  334. default:
  335. dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
  336. info->hpdet_ip);
  337. case 2:
  338. if (!(val & ARIZONA_HP_DONE_B)) {
  339. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  340. val);
  341. return -EAGAIN;
  342. }
  343. val &= ARIZONA_HP_LVL_B_MASK;
  344. regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  345. &range);
  346. range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
  347. >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
  348. /* Skip up or down a range? */
  349. if (range && (val < arizona_hpdet_c_ranges[range].min)) {
  350. range--;
  351. dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
  352. arizona_hpdet_c_ranges[range].min,
  353. arizona_hpdet_c_ranges[range].max);
  354. regmap_update_bits(arizona->regmap,
  355. ARIZONA_HEADPHONE_DETECT_1,
  356. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  357. range <<
  358. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  359. return -EAGAIN;
  360. }
  361. if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
  362. (val >= arizona_hpdet_c_ranges[range].max)) {
  363. range++;
  364. dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
  365. arizona_hpdet_c_ranges[range].min,
  366. arizona_hpdet_c_ranges[range].max);
  367. regmap_update_bits(arizona->regmap,
  368. ARIZONA_HEADPHONE_DETECT_1,
  369. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  370. range <<
  371. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  372. return -EAGAIN;
  373. }
  374. }
  375. dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
  376. return val;
  377. }
  378. static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
  379. {
  380. struct arizona *arizona = info->arizona;
  381. int id_gpio = arizona->pdata.hpdet_id_gpio;
  382. /*
  383. * If we're using HPDET for accessory identification we need
  384. * to take multiple measurements, step through them in sequence.
  385. */
  386. if (arizona->pdata.hpdet_acc_id) {
  387. info->hpdet_res[info->num_hpdet_res++] = *reading;
  388. info->hpdet_res[info->num_hpdet_res++] = *reading;
  389. /* Only check the mic directly if we didn't already ID it */
  390. if (id_gpio && info->num_hpdet_res == 2 &&
  391. !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
  392. dev_dbg(arizona->dev, "Measuring mic\n");
  393. regmap_update_bits(arizona->regmap,
  394. ARIZONA_ACCESSORY_DETECT_MODE_1,
  395. ARIZONA_ACCDET_MODE_MASK |
  396. ARIZONA_ACCDET_SRC,
  397. ARIZONA_ACCDET_MODE_HPR |
  398. info->micd_modes[0].src);
  399. gpio_set_value_cansleep(id_gpio, 1);
  400. regmap_update_bits(arizona->regmap,
  401. ARIZONA_HEADPHONE_DETECT_1,
  402. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  403. return -EAGAIN;
  404. }
  405. /* OK, got both. Now, compare... */
  406. dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
  407. info->hpdet_res[0], info->hpdet_res[1],
  408. info->hpdet_res[2]);
  409. /* Take the headphone impedance for the main report */
  410. *reading = info->hpdet_res[0];
  411. /* Sometimes we get false readings due to slow insert */
  412. if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
  413. dev_dbg(arizona->dev, "Retrying high impedance\n");
  414. info->num_hpdet_res = 0;
  415. info->hpdet_retried = true;
  416. arizona_start_hpdet_acc_id(info);
  417. pm_runtime_put(info->dev);
  418. return -EAGAIN;
  419. }
  420. /*
  421. * Either the two grounds measure differently or we
  422. * measure the mic as high impedance.
  423. */
  424. if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
  425. (id_gpio && info->hpdet_res[2] > 1257)) {
  426. dev_dbg(arizona->dev, "Detected mic\n");
  427. info->mic = true;
  428. info->detecting = true;
  429. } else {
  430. dev_dbg(arizona->dev, "Detected headphone\n");
  431. }
  432. /* Make sure everything is reset back to the real polarity */
  433. regmap_update_bits(arizona->regmap,
  434. ARIZONA_ACCESSORY_DETECT_MODE_1,
  435. ARIZONA_ACCDET_SRC,
  436. info->micd_modes[0].src);
  437. }
  438. return 0;
  439. }
  440. static irqreturn_t arizona_hpdet_irq(int irq, void *data)
  441. {
  442. struct arizona_extcon_info *info = data;
  443. struct arizona *arizona = info->arizona;
  444. int id_gpio = arizona->pdata.hpdet_id_gpio;
  445. int report = ARIZONA_CABLE_HEADPHONE;
  446. int ret, reading;
  447. mutex_lock(&info->lock);
  448. /* If we got a spurious IRQ for some reason then ignore it */
  449. if (!info->hpdet_active) {
  450. dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
  451. mutex_unlock(&info->lock);
  452. return IRQ_NONE;
  453. }
  454. /* If the cable was removed while measuring ignore the result */
  455. ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
  456. if (ret < 0) {
  457. dev_err(arizona->dev, "Failed to check cable state: %d\n",
  458. ret);
  459. goto out;
  460. } else if (!ret) {
  461. dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
  462. goto done;
  463. }
  464. ret = arizona_hpdet_read(info);
  465. if (ret == -EAGAIN) {
  466. goto out;
  467. } else if (ret < 0) {
  468. goto done;
  469. }
  470. reading = ret;
  471. /* Reset back to starting range */
  472. regmap_update_bits(arizona->regmap,
  473. ARIZONA_HEADPHONE_DETECT_1,
  474. ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
  475. 0);
  476. ret = arizona_hpdet_do_id(info, &reading);
  477. if (ret == -EAGAIN) {
  478. goto out;
  479. } else if (ret < 0) {
  480. goto done;
  481. }
  482. /* Report high impedence cables as line outputs */
  483. if (reading >= 5000)
  484. report = ARIZONA_CABLE_LINEOUT;
  485. else
  486. report = ARIZONA_CABLE_HEADPHONE;
  487. ret = extcon_set_cable_state_(&info->edev, report, true);
  488. if (ret != 0)
  489. dev_err(arizona->dev, "Failed to report HP/line: %d\n",
  490. ret);
  491. arizona_extcon_do_magic(info, 0);
  492. done:
  493. if (id_gpio)
  494. gpio_set_value_cansleep(id_gpio, 0);
  495. /* Revert back to MICDET mode */
  496. regmap_update_bits(arizona->regmap,
  497. ARIZONA_ACCESSORY_DETECT_MODE_1,
  498. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  499. /* If we have a mic then reenable MICDET */
  500. if (info->mic)
  501. arizona_start_mic(info);
  502. if (info->hpdet_active) {
  503. pm_runtime_put_autosuspend(info->dev);
  504. info->hpdet_active = false;
  505. }
  506. info->hpdet_done = true;
  507. out:
  508. mutex_unlock(&info->lock);
  509. return IRQ_HANDLED;
  510. }
  511. static void arizona_identify_headphone(struct arizona_extcon_info *info)
  512. {
  513. struct arizona *arizona = info->arizona;
  514. int ret;
  515. if (info->hpdet_done)
  516. return;
  517. dev_dbg(arizona->dev, "Starting HPDET\n");
  518. /* Make sure we keep the device enabled during the measurement */
  519. pm_runtime_get(info->dev);
  520. info->hpdet_active = true;
  521. if (info->mic)
  522. arizona_stop_mic(info);
  523. arizona_extcon_do_magic(info, 0x4000);
  524. ret = regmap_update_bits(arizona->regmap,
  525. ARIZONA_ACCESSORY_DETECT_MODE_1,
  526. ARIZONA_ACCDET_MODE_MASK,
  527. ARIZONA_ACCDET_MODE_HPL);
  528. if (ret != 0) {
  529. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  530. goto err;
  531. }
  532. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  533. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  534. if (ret != 0) {
  535. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  536. ret);
  537. goto err;
  538. }
  539. return;
  540. err:
  541. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  542. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  543. /* Just report headphone */
  544. ret = extcon_update_state(&info->edev,
  545. 1 << ARIZONA_CABLE_HEADPHONE,
  546. 1 << ARIZONA_CABLE_HEADPHONE);
  547. if (ret != 0)
  548. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  549. if (info->mic)
  550. arizona_start_mic(info);
  551. info->hpdet_active = false;
  552. }
  553. static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
  554. {
  555. struct arizona *arizona = info->arizona;
  556. int ret;
  557. dev_dbg(arizona->dev, "Starting identification via HPDET\n");
  558. /* Make sure we keep the device enabled during the measurement */
  559. pm_runtime_get_sync(info->dev);
  560. info->hpdet_active = true;
  561. arizona_extcon_do_magic(info, 0x4000);
  562. ret = regmap_update_bits(arizona->regmap,
  563. ARIZONA_ACCESSORY_DETECT_MODE_1,
  564. ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
  565. info->micd_modes[0].src |
  566. ARIZONA_ACCDET_MODE_HPL);
  567. if (ret != 0) {
  568. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  569. goto err;
  570. }
  571. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  572. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  573. if (ret != 0) {
  574. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  575. ret);
  576. goto err;
  577. }
  578. return;
  579. err:
  580. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  581. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  582. /* Just report headphone */
  583. ret = extcon_update_state(&info->edev,
  584. 1 << ARIZONA_CABLE_HEADPHONE,
  585. 1 << ARIZONA_CABLE_HEADPHONE);
  586. if (ret != 0)
  587. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  588. info->hpdet_active = false;
  589. }
  590. static irqreturn_t arizona_micdet(int irq, void *data)
  591. {
  592. struct arizona_extcon_info *info = data;
  593. struct arizona *arizona = info->arizona;
  594. unsigned int val = 0, lvl;
  595. int ret, i, key;
  596. mutex_lock(&info->lock);
  597. for (i = 0; i < 10 && !(val & 0x7fc); i++) {
  598. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  599. if (ret != 0) {
  600. dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
  601. mutex_unlock(&info->lock);
  602. return IRQ_NONE;
  603. }
  604. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  605. if (!(val & ARIZONA_MICD_VALID)) {
  606. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  607. mutex_unlock(&info->lock);
  608. return IRQ_NONE;
  609. }
  610. }
  611. if (i == 10 && !(val & 0x7fc)) {
  612. dev_err(arizona->dev, "Failed to get valid MICDET value\n");
  613. mutex_unlock(&info->lock);
  614. return IRQ_NONE;
  615. }
  616. /* Due to jack detect this should never happen */
  617. if (!(val & ARIZONA_MICD_STS)) {
  618. dev_warn(arizona->dev, "Detected open circuit\n");
  619. info->detecting = false;
  620. goto handled;
  621. }
  622. /* If we got a high impedence we should have a headset, report it. */
  623. if (info->detecting && (val & 0x400)) {
  624. arizona_identify_headphone(info);
  625. ret = extcon_update_state(&info->edev,
  626. 1 << ARIZONA_CABLE_MICROPHONE,
  627. 1 << ARIZONA_CABLE_MICROPHONE);
  628. if (ret != 0)
  629. dev_err(arizona->dev, "Headset report failed: %d\n",
  630. ret);
  631. /* Don't need to regulate for button detection */
  632. ret = regulator_allow_bypass(info->micvdd, false);
  633. if (ret != 0) {
  634. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
  635. ret);
  636. }
  637. info->mic = true;
  638. info->detecting = false;
  639. goto handled;
  640. }
  641. /* If we detected a lower impedence during initial startup
  642. * then we probably have the wrong polarity, flip it. Don't
  643. * do this for the lowest impedences to speed up detection of
  644. * plain headphones. If both polarities report a low
  645. * impedence then give up and report headphones.
  646. */
  647. if (info->detecting && (val & 0x3f8)) {
  648. if (info->jack_flips >= info->micd_num_modes * 10) {
  649. dev_dbg(arizona->dev, "Detected HP/line\n");
  650. arizona_identify_headphone(info);
  651. info->detecting = false;
  652. arizona_stop_mic(info);
  653. } else {
  654. info->micd_mode++;
  655. if (info->micd_mode == info->micd_num_modes)
  656. info->micd_mode = 0;
  657. arizona_extcon_set_mode(info, info->micd_mode);
  658. info->jack_flips++;
  659. }
  660. goto handled;
  661. }
  662. /*
  663. * If we're still detecting and we detect a short then we've
  664. * got a headphone. Otherwise it's a button press.
  665. */
  666. if (val & 0x3fc) {
  667. if (info->mic) {
  668. dev_dbg(arizona->dev, "Mic button detected\n");
  669. lvl = val & ARIZONA_MICD_LVL_MASK;
  670. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  671. WARN_ON(!lvl);
  672. WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
  673. if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
  674. key = info->micd_ranges[ffs(lvl) - 1].key;
  675. input_report_key(info->input, key, 1);
  676. input_sync(info->input);
  677. }
  678. } else if (info->detecting) {
  679. dev_dbg(arizona->dev, "Headphone detected\n");
  680. info->detecting = false;
  681. arizona_stop_mic(info);
  682. arizona_identify_headphone(info);
  683. } else {
  684. dev_warn(arizona->dev, "Button with no mic: %x\n",
  685. val);
  686. }
  687. } else {
  688. dev_dbg(arizona->dev, "Mic button released\n");
  689. for (i = 0; i < info->num_micd_ranges; i++)
  690. input_report_key(info->input,
  691. info->micd_ranges[i].key, 0);
  692. input_sync(info->input);
  693. arizona_extcon_pulse_micbias(info);
  694. }
  695. handled:
  696. pm_runtime_mark_last_busy(info->dev);
  697. mutex_unlock(&info->lock);
  698. return IRQ_HANDLED;
  699. }
  700. static void arizona_hpdet_work(struct work_struct *work)
  701. {
  702. struct arizona_extcon_info *info = container_of(work,
  703. struct arizona_extcon_info,
  704. hpdet_work.work);
  705. mutex_lock(&info->lock);
  706. arizona_start_hpdet_acc_id(info);
  707. mutex_unlock(&info->lock);
  708. }
  709. static irqreturn_t arizona_jackdet(int irq, void *data)
  710. {
  711. struct arizona_extcon_info *info = data;
  712. struct arizona *arizona = info->arizona;
  713. unsigned int val, present, mask;
  714. bool cancelled;
  715. int ret, i;
  716. cancelled = cancel_delayed_work_sync(&info->hpdet_work);
  717. pm_runtime_get_sync(info->dev);
  718. mutex_lock(&info->lock);
  719. if (arizona->pdata.jd_gpio5) {
  720. mask = ARIZONA_MICD_CLAMP_STS;
  721. present = 0;
  722. } else {
  723. mask = ARIZONA_JD1_STS;
  724. present = ARIZONA_JD1_STS;
  725. }
  726. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  727. if (ret != 0) {
  728. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  729. ret);
  730. mutex_unlock(&info->lock);
  731. pm_runtime_put_autosuspend(info->dev);
  732. return IRQ_NONE;
  733. }
  734. val &= mask;
  735. if (val == info->last_jackdet) {
  736. dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
  737. if (cancelled)
  738. schedule_delayed_work(&info->hpdet_work,
  739. msecs_to_jiffies(HPDET_DEBOUNCE));
  740. goto out;
  741. }
  742. info->last_jackdet = val;
  743. if (info->last_jackdet == present) {
  744. dev_dbg(arizona->dev, "Detected jack\n");
  745. ret = extcon_set_cable_state_(&info->edev,
  746. ARIZONA_CABLE_MECHANICAL, true);
  747. if (ret != 0)
  748. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  749. ret);
  750. if (!arizona->pdata.hpdet_acc_id) {
  751. info->detecting = true;
  752. info->mic = false;
  753. info->jack_flips = 0;
  754. arizona_start_mic(info);
  755. } else {
  756. schedule_delayed_work(&info->hpdet_work,
  757. msecs_to_jiffies(HPDET_DEBOUNCE));
  758. }
  759. regmap_update_bits(arizona->regmap,
  760. ARIZONA_JACK_DETECT_DEBOUNCE,
  761. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
  762. } else {
  763. dev_dbg(arizona->dev, "Detected jack removal\n");
  764. arizona_stop_mic(info);
  765. info->num_hpdet_res = 0;
  766. for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
  767. info->hpdet_res[i] = 0;
  768. info->mic = false;
  769. info->hpdet_done = false;
  770. info->hpdet_retried = false;
  771. for (i = 0; i < info->num_micd_ranges; i++)
  772. input_report_key(info->input,
  773. info->micd_ranges[i].key, 0);
  774. input_sync(info->input);
  775. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  776. if (ret != 0)
  777. dev_err(arizona->dev, "Removal report failed: %d\n",
  778. ret);
  779. regmap_update_bits(arizona->regmap,
  780. ARIZONA_JACK_DETECT_DEBOUNCE,
  781. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
  782. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
  783. }
  784. /* Clear trig_sts to make sure DCVDD is not forced up */
  785. regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
  786. ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
  787. ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
  788. ARIZONA_JD1_FALL_TRIG_STS |
  789. ARIZONA_JD1_RISE_TRIG_STS);
  790. out:
  791. mutex_unlock(&info->lock);
  792. pm_runtime_mark_last_busy(info->dev);
  793. pm_runtime_put_autosuspend(info->dev);
  794. return IRQ_HANDLED;
  795. }
  796. /* Map a level onto a slot in the register bank */
  797. static void arizona_micd_set_level(struct arizona *arizona, int index,
  798. unsigned int level)
  799. {
  800. int reg;
  801. unsigned int mask;
  802. reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
  803. if (!(index % 2)) {
  804. mask = 0x3f00;
  805. level <<= 8;
  806. } else {
  807. mask = 0x3f;
  808. }
  809. /* Program the level itself */
  810. regmap_update_bits(arizona->regmap, reg, mask, level);
  811. }
  812. static int arizona_extcon_probe(struct platform_device *pdev)
  813. {
  814. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  815. struct arizona_pdata *pdata;
  816. struct arizona_extcon_info *info;
  817. unsigned int val;
  818. int jack_irq_fall, jack_irq_rise;
  819. int ret, mode, i, j;
  820. if (!arizona->dapm || !arizona->dapm->card)
  821. return -EPROBE_DEFER;
  822. pdata = dev_get_platdata(arizona->dev);
  823. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  824. if (!info) {
  825. dev_err(&pdev->dev, "Failed to allocate memory\n");
  826. ret = -ENOMEM;
  827. goto err;
  828. }
  829. info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
  830. if (IS_ERR(info->micvdd)) {
  831. ret = PTR_ERR(info->micvdd);
  832. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  833. goto err;
  834. }
  835. mutex_init(&info->lock);
  836. info->arizona = arizona;
  837. info->dev = &pdev->dev;
  838. info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
  839. INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
  840. platform_set_drvdata(pdev, info);
  841. switch (arizona->type) {
  842. case WM5102:
  843. switch (arizona->rev) {
  844. case 0:
  845. info->micd_reva = true;
  846. break;
  847. default:
  848. info->micd_clamp = true;
  849. info->hpdet_ip = 1;
  850. break;
  851. }
  852. break;
  853. default:
  854. break;
  855. }
  856. info->edev.name = "Headset Jack";
  857. info->edev.supported_cable = arizona_cable;
  858. ret = extcon_dev_register(&info->edev, arizona->dev);
  859. if (ret < 0) {
  860. dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
  861. ret);
  862. goto err;
  863. }
  864. info->input = devm_input_allocate_device(&pdev->dev);
  865. if (!info->input) {
  866. dev_err(arizona->dev, "Can't allocate input dev\n");
  867. ret = -ENOMEM;
  868. goto err_register;
  869. }
  870. info->input->name = "Headset";
  871. info->input->phys = "arizona/extcon";
  872. info->input->dev.parent = &pdev->dev;
  873. if (pdata->num_micd_configs) {
  874. info->micd_modes = pdata->micd_configs;
  875. info->micd_num_modes = pdata->num_micd_configs;
  876. } else {
  877. info->micd_modes = micd_default_modes;
  878. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  879. }
  880. if (arizona->pdata.micd_pol_gpio > 0) {
  881. if (info->micd_modes[0].gpio)
  882. mode = GPIOF_OUT_INIT_HIGH;
  883. else
  884. mode = GPIOF_OUT_INIT_LOW;
  885. ret = devm_gpio_request_one(&pdev->dev,
  886. arizona->pdata.micd_pol_gpio,
  887. mode,
  888. "MICD polarity");
  889. if (ret != 0) {
  890. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  891. arizona->pdata.micd_pol_gpio, ret);
  892. goto err_register;
  893. }
  894. }
  895. if (arizona->pdata.hpdet_id_gpio > 0) {
  896. ret = devm_gpio_request_one(&pdev->dev,
  897. arizona->pdata.hpdet_id_gpio,
  898. GPIOF_OUT_INIT_LOW,
  899. "HPDET");
  900. if (ret != 0) {
  901. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  902. arizona->pdata.hpdet_id_gpio, ret);
  903. goto err_register;
  904. }
  905. }
  906. if (arizona->pdata.micd_bias_start_time)
  907. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  908. ARIZONA_MICD_BIAS_STARTTIME_MASK,
  909. arizona->pdata.micd_bias_start_time
  910. << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
  911. if (arizona->pdata.micd_rate)
  912. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  913. ARIZONA_MICD_RATE_MASK,
  914. arizona->pdata.micd_rate
  915. << ARIZONA_MICD_RATE_SHIFT);
  916. if (arizona->pdata.micd_dbtime)
  917. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  918. ARIZONA_MICD_DBTIME_MASK,
  919. arizona->pdata.micd_dbtime
  920. << ARIZONA_MICD_DBTIME_SHIFT);
  921. BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
  922. if (arizona->pdata.num_micd_ranges) {
  923. info->micd_ranges = pdata->micd_ranges;
  924. info->num_micd_ranges = pdata->num_micd_ranges;
  925. } else {
  926. info->micd_ranges = micd_default_ranges;
  927. info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
  928. }
  929. if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
  930. dev_err(arizona->dev, "Too many MICD ranges: %d\n",
  931. arizona->pdata.num_micd_ranges);
  932. }
  933. if (info->num_micd_ranges > 1) {
  934. for (i = 1; i < info->num_micd_ranges; i++) {
  935. if (info->micd_ranges[i - 1].max >
  936. info->micd_ranges[i].max) {
  937. dev_err(arizona->dev,
  938. "MICD ranges must be sorted\n");
  939. ret = -EINVAL;
  940. goto err_input;
  941. }
  942. }
  943. }
  944. /* Disable all buttons by default */
  945. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  946. ARIZONA_MICD_LVL_SEL_MASK, 0x81);
  947. /* Set up all the buttons the user specified */
  948. for (i = 0; i < info->num_micd_ranges; i++) {
  949. for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
  950. if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
  951. break;
  952. if (j == ARRAY_SIZE(arizona_micd_levels)) {
  953. dev_err(arizona->dev, "Unsupported MICD level %d\n",
  954. info->micd_ranges[i].max);
  955. ret = -EINVAL;
  956. goto err_input;
  957. }
  958. dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
  959. arizona_micd_levels[j], i);
  960. arizona_micd_set_level(arizona, i, j);
  961. input_set_capability(info->input, EV_KEY,
  962. info->micd_ranges[i].key);
  963. /* Enable reporting of that range */
  964. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  965. 1 << i, 1 << i);
  966. }
  967. /* Set all the remaining keys to a maximum */
  968. for (; i < ARIZONA_MAX_MICD_RANGE; i++)
  969. arizona_micd_set_level(arizona, i, 0x3f);
  970. /*
  971. * If we have a clamp use it, activating in conjunction with
  972. * GPIO5 if that is connected for jack detect operation.
  973. */
  974. if (info->micd_clamp) {
  975. if (arizona->pdata.jd_gpio5) {
  976. /* Put the GPIO into input mode with optional pull */
  977. val = 0xc101;
  978. if (arizona->pdata.jd_gpio5_nopull)
  979. val &= ~ARIZONA_GPN_PU;
  980. regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
  981. val);
  982. regmap_update_bits(arizona->regmap,
  983. ARIZONA_MICD_CLAMP_CONTROL,
  984. ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
  985. } else {
  986. regmap_update_bits(arizona->regmap,
  987. ARIZONA_MICD_CLAMP_CONTROL,
  988. ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
  989. }
  990. regmap_update_bits(arizona->regmap,
  991. ARIZONA_JACK_DETECT_DEBOUNCE,
  992. ARIZONA_MICD_CLAMP_DB,
  993. ARIZONA_MICD_CLAMP_DB);
  994. }
  995. arizona_extcon_set_mode(info, 0);
  996. pm_runtime_enable(&pdev->dev);
  997. pm_runtime_idle(&pdev->dev);
  998. pm_runtime_get_sync(&pdev->dev);
  999. if (arizona->pdata.jd_gpio5) {
  1000. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1001. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1002. } else {
  1003. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1004. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1005. }
  1006. ret = arizona_request_irq(arizona, jack_irq_rise,
  1007. "JACKDET rise", arizona_jackdet, info);
  1008. if (ret != 0) {
  1009. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  1010. ret);
  1011. goto err_input;
  1012. }
  1013. ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
  1014. if (ret != 0) {
  1015. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  1016. ret);
  1017. goto err_rise;
  1018. }
  1019. ret = arizona_request_irq(arizona, jack_irq_fall,
  1020. "JACKDET fall", arizona_jackdet, info);
  1021. if (ret != 0) {
  1022. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  1023. goto err_rise_wake;
  1024. }
  1025. ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
  1026. if (ret != 0) {
  1027. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  1028. ret);
  1029. goto err_fall;
  1030. }
  1031. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  1032. "MICDET", arizona_micdet, info);
  1033. if (ret != 0) {
  1034. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  1035. goto err_fall_wake;
  1036. }
  1037. ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
  1038. "HPDET", arizona_hpdet_irq, info);
  1039. if (ret != 0) {
  1040. dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
  1041. goto err_micdet;
  1042. }
  1043. arizona_clk32k_enable(arizona);
  1044. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  1045. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  1046. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1047. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  1048. ret = regulator_allow_bypass(info->micvdd, true);
  1049. if (ret != 0)
  1050. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
  1051. ret);
  1052. pm_runtime_put(&pdev->dev);
  1053. ret = input_register_device(info->input);
  1054. if (ret) {
  1055. dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
  1056. goto err_hpdet;
  1057. }
  1058. return 0;
  1059. err_hpdet:
  1060. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1061. err_micdet:
  1062. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1063. err_fall_wake:
  1064. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1065. err_fall:
  1066. arizona_free_irq(arizona, jack_irq_fall, info);
  1067. err_rise_wake:
  1068. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1069. err_rise:
  1070. arizona_free_irq(arizona, jack_irq_rise, info);
  1071. err_input:
  1072. err_register:
  1073. pm_runtime_disable(&pdev->dev);
  1074. extcon_dev_unregister(&info->edev);
  1075. err:
  1076. return ret;
  1077. }
  1078. static int arizona_extcon_remove(struct platform_device *pdev)
  1079. {
  1080. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  1081. struct arizona *arizona = info->arizona;
  1082. int jack_irq_rise, jack_irq_fall;
  1083. pm_runtime_disable(&pdev->dev);
  1084. regmap_update_bits(arizona->regmap,
  1085. ARIZONA_MICD_CLAMP_CONTROL,
  1086. ARIZONA_MICD_CLAMP_MODE_MASK, 0);
  1087. if (arizona->pdata.jd_gpio5) {
  1088. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1089. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1090. } else {
  1091. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1092. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1093. }
  1094. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1095. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1096. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1097. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1098. arizona_free_irq(arizona, jack_irq_rise, info);
  1099. arizona_free_irq(arizona, jack_irq_fall, info);
  1100. cancel_delayed_work_sync(&info->hpdet_work);
  1101. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1102. ARIZONA_JD1_ENA, 0);
  1103. arizona_clk32k_disable(arizona);
  1104. extcon_dev_unregister(&info->edev);
  1105. return 0;
  1106. }
  1107. static struct platform_driver arizona_extcon_driver = {
  1108. .driver = {
  1109. .name = "arizona-extcon",
  1110. .owner = THIS_MODULE,
  1111. },
  1112. .probe = arizona_extcon_probe,
  1113. .remove = arizona_extcon_remove,
  1114. };
  1115. module_platform_driver(arizona_extcon_driver);
  1116. MODULE_DESCRIPTION("Arizona Extcon driver");
  1117. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1118. MODULE_LICENSE("GPL");
  1119. MODULE_ALIAS("platform:extcon-arizona");