extcon-arizona.c 34 KB

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