extcon-arizona.c 36 KB

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