extcon-arizona.c 30 KB

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