arizona-jack.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * extcon-arizona.c - Extcon driver Wolfson Arizona devices
  4. *
  5. * Copyright (C) 2012-2014 Wolfson Microelectronics plc
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/err.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/gpio.h>
  14. #include <linux/input.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/property.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <sound/jack.h>
  19. #include <sound/soc.h>
  20. #include <linux/mfd/arizona/core.h>
  21. #include <linux/mfd/arizona/pdata.h>
  22. #include <linux/mfd/arizona/registers.h>
  23. #include <dt-bindings/mfd/arizona.h>
  24. #include "arizona.h"
  25. #define ARIZONA_MAX_MICD_RANGE 8
  26. /*
  27. * The hardware supports 8 ranges / buttons, but the snd-jack interface
  28. * only supports 6 buttons (button 0-5).
  29. */
  30. #define ARIZONA_MAX_MICD_BUTTONS 6
  31. #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
  32. #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
  33. #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
  34. #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
  35. #define ARIZONA_TST_CAP_DEFAULT 0x3
  36. #define ARIZONA_TST_CAP_CLAMP 0x1
  37. #define ARIZONA_HPDET_MAX 10000
  38. #define HPDET_DEBOUNCE 500
  39. #define DEFAULT_MICD_TIMEOUT 2000
  40. #define ARIZONA_HPDET_WAIT_COUNT 15
  41. #define ARIZONA_HPDET_WAIT_DELAY_MS 20
  42. #define QUICK_HEADPHONE_MAX_OHM 3
  43. #define MICROPHONE_MIN_OHM 1257
  44. #define MICROPHONE_MAX_OHM 30000
  45. #define MICD_DBTIME_TWO_READINGS 2
  46. #define MICD_DBTIME_FOUR_READINGS 4
  47. #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
  48. ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
  49. ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
  50. ARIZONA_MICD_LVL_7)
  51. #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
  52. #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
  53. static const struct arizona_micd_config micd_default_modes[] = {
  54. { ARIZONA_ACCDET_SRC, 1, 0 },
  55. { 0, 2, 1 },
  56. };
  57. static const struct arizona_micd_range micd_default_ranges[] = {
  58. { .max = 11, .key = BTN_0 },
  59. { .max = 28, .key = BTN_1 },
  60. { .max = 54, .key = BTN_2 },
  61. { .max = 100, .key = BTN_3 },
  62. { .max = 186, .key = BTN_4 },
  63. { .max = 430, .key = BTN_5 },
  64. };
  65. /* The number of levels in arizona_micd_levels valid for button thresholds */
  66. #define ARIZONA_NUM_MICD_BUTTON_LEVELS 64
  67. static const int arizona_micd_levels[] = {
  68. 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
  69. 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
  70. 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
  71. 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
  72. 1257, 30000,
  73. };
  74. static void arizona_start_hpdet_acc_id(struct arizona_priv *info);
  75. static void arizona_extcon_hp_clamp(struct arizona_priv *info,
  76. bool clamp)
  77. {
  78. struct arizona *arizona = info->arizona;
  79. unsigned int mask = 0, val = 0;
  80. unsigned int cap_sel = 0;
  81. int ret;
  82. switch (arizona->type) {
  83. case WM8998:
  84. case WM1814:
  85. mask = 0;
  86. break;
  87. case WM5110:
  88. case WM8280:
  89. mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
  90. ARIZONA_HP1L_SHRTI;
  91. if (clamp) {
  92. val = ARIZONA_HP1L_SHRTO;
  93. cap_sel = ARIZONA_TST_CAP_CLAMP;
  94. } else {
  95. val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
  96. cap_sel = ARIZONA_TST_CAP_DEFAULT;
  97. }
  98. ret = regmap_update_bits(arizona->regmap,
  99. ARIZONA_HP_TEST_CTRL_1,
  100. ARIZONA_HP1_TST_CAP_SEL_MASK,
  101. cap_sel);
  102. if (ret)
  103. dev_warn(arizona->dev, "Failed to set TST_CAP_SEL: %d\n", ret);
  104. break;
  105. default:
  106. mask = ARIZONA_RMV_SHRT_HP1L;
  107. if (clamp)
  108. val = ARIZONA_RMV_SHRT_HP1L;
  109. break;
  110. }
  111. snd_soc_dapm_mutex_lock(arizona->dapm);
  112. arizona->hpdet_clamp = clamp;
  113. /* Keep the HP output stages disabled while doing the clamp */
  114. if (clamp) {
  115. ret = regmap_update_bits(arizona->regmap,
  116. ARIZONA_OUTPUT_ENABLES_1,
  117. ARIZONA_OUT1L_ENA |
  118. ARIZONA_OUT1R_ENA, 0);
  119. if (ret)
  120. dev_warn(arizona->dev, "Failed to disable headphone outputs: %d\n", ret);
  121. }
  122. if (mask) {
  123. ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
  124. mask, val);
  125. if (ret)
  126. dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
  127. ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
  128. mask, val);
  129. if (ret)
  130. dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
  131. }
  132. /* Restore the desired state while not doing the clamp */
  133. if (!clamp) {
  134. ret = regmap_update_bits(arizona->regmap,
  135. ARIZONA_OUTPUT_ENABLES_1,
  136. ARIZONA_OUT1L_ENA |
  137. ARIZONA_OUT1R_ENA, arizona->hp_ena);
  138. if (ret)
  139. dev_warn(arizona->dev, "Failed to restore headphone outputs: %d\n", ret);
  140. }
  141. snd_soc_dapm_mutex_unlock(arizona->dapm);
  142. }
  143. static void arizona_extcon_set_mode(struct arizona_priv *info, int mode)
  144. {
  145. struct arizona *arizona = info->arizona;
  146. mode %= info->micd_num_modes;
  147. gpiod_set_value_cansleep(info->micd_pol_gpio,
  148. info->micd_modes[mode].gpio);
  149. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  150. ARIZONA_MICD_BIAS_SRC_MASK,
  151. info->micd_modes[mode].bias <<
  152. ARIZONA_MICD_BIAS_SRC_SHIFT);
  153. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  154. ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
  155. info->micd_mode = mode;
  156. dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
  157. }
  158. static const char *arizona_extcon_get_micbias(struct arizona_priv *info)
  159. {
  160. switch (info->micd_modes[0].bias) {
  161. case 1:
  162. return "MICBIAS1";
  163. case 2:
  164. return "MICBIAS2";
  165. case 3:
  166. return "MICBIAS3";
  167. default:
  168. return "MICVDD";
  169. }
  170. }
  171. static void arizona_extcon_pulse_micbias(struct arizona_priv *info)
  172. {
  173. struct arizona *arizona = info->arizona;
  174. const char *widget = arizona_extcon_get_micbias(info);
  175. struct snd_soc_dapm_context *dapm = arizona->dapm;
  176. struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
  177. int ret;
  178. ret = snd_soc_component_force_enable_pin(component, widget);
  179. if (ret)
  180. dev_warn(arizona->dev, "Failed to enable %s: %d\n", widget, ret);
  181. snd_soc_dapm_sync(dapm);
  182. if (!arizona->pdata.micd_force_micbias) {
  183. ret = snd_soc_component_disable_pin(component, widget);
  184. if (ret)
  185. dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
  186. snd_soc_dapm_sync(dapm);
  187. }
  188. }
  189. static void arizona_start_mic(struct arizona_priv *info)
  190. {
  191. struct arizona *arizona = info->arizona;
  192. bool change;
  193. int ret;
  194. unsigned int mode;
  195. /* Microphone detection can't use idle mode */
  196. pm_runtime_get_sync(arizona->dev);
  197. if (info->detecting) {
  198. ret = regulator_allow_bypass(info->micvdd, false);
  199. if (ret)
  200. dev_err(arizona->dev, "Failed to regulate MICVDD: %d\n", ret);
  201. }
  202. ret = regulator_enable(info->micvdd);
  203. if (ret)
  204. dev_err(arizona->dev, "Failed to enable MICVDD: %d\n", ret);
  205. if (info->micd_reva) {
  206. const struct reg_sequence reva[] = {
  207. { 0x80, 0x3 },
  208. { 0x294, 0x0 },
  209. { 0x80, 0x0 },
  210. };
  211. regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
  212. }
  213. if (info->detecting && arizona->pdata.micd_software_compare)
  214. mode = ARIZONA_ACCDET_MODE_ADC;
  215. else
  216. mode = ARIZONA_ACCDET_MODE_MIC;
  217. regmap_update_bits(arizona->regmap,
  218. ARIZONA_ACCESSORY_DETECT_MODE_1,
  219. ARIZONA_ACCDET_MODE_MASK, mode);
  220. arizona_extcon_pulse_micbias(info);
  221. ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  222. ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
  223. &change);
  224. if (ret < 0) {
  225. dev_err(arizona->dev, "Failed to enable micd: %d\n", ret);
  226. } else if (!change) {
  227. regulator_disable(info->micvdd);
  228. pm_runtime_put_autosuspend(arizona->dev);
  229. }
  230. }
  231. static void arizona_stop_mic(struct arizona_priv *info)
  232. {
  233. struct arizona *arizona = info->arizona;
  234. const char *widget = arizona_extcon_get_micbias(info);
  235. struct snd_soc_dapm_context *dapm = arizona->dapm;
  236. struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
  237. bool change = false;
  238. int ret;
  239. ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  240. ARIZONA_MICD_ENA, 0,
  241. &change);
  242. if (ret < 0)
  243. dev_err(arizona->dev, "Failed to disable micd: %d\n", ret);
  244. ret = snd_soc_component_disable_pin(component, widget);
  245. if (ret)
  246. dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
  247. snd_soc_dapm_sync(dapm);
  248. if (info->micd_reva) {
  249. const struct reg_sequence reva[] = {
  250. { 0x80, 0x3 },
  251. { 0x294, 0x2 },
  252. { 0x80, 0x0 },
  253. };
  254. regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
  255. }
  256. ret = regulator_allow_bypass(info->micvdd, true);
  257. if (ret)
  258. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
  259. if (change) {
  260. regulator_disable(info->micvdd);
  261. pm_runtime_mark_last_busy(arizona->dev);
  262. pm_runtime_put_autosuspend(arizona->dev);
  263. }
  264. }
  265. static struct {
  266. unsigned int threshold;
  267. unsigned int factor_a;
  268. unsigned int factor_b;
  269. } arizona_hpdet_b_ranges[] = {
  270. { 100, 5528, 362464 },
  271. { 169, 11084, 6186851 },
  272. { 169, 11065, 65460395 },
  273. };
  274. #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
  275. static struct {
  276. int min;
  277. int max;
  278. } arizona_hpdet_c_ranges[] = {
  279. { 0, 30 },
  280. { 8, 100 },
  281. { 100, 1000 },
  282. { 1000, 10000 },
  283. };
  284. static int arizona_hpdet_read(struct arizona_priv *info)
  285. {
  286. struct arizona *arizona = info->arizona;
  287. unsigned int val, range;
  288. int ret;
  289. ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
  290. if (ret) {
  291. dev_err(arizona->dev, "Failed to read HPDET status: %d\n", ret);
  292. return ret;
  293. }
  294. switch (info->hpdet_ip_version) {
  295. case 0:
  296. if (!(val & ARIZONA_HP_DONE)) {
  297. dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
  298. return -EAGAIN;
  299. }
  300. val &= ARIZONA_HP_LVL_MASK;
  301. break;
  302. case 1:
  303. if (!(val & ARIZONA_HP_DONE_B)) {
  304. dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
  305. return -EAGAIN;
  306. }
  307. ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
  308. if (ret) {
  309. dev_err(arizona->dev, "Failed to read HP value: %d\n", 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 < arizona_hpdet_b_ranges[range].threshold ||
  318. val >= ARIZONA_HPDET_B_RANGE_MAX)) {
  319. range++;
  320. dev_dbg(arizona->dev, "Moving to HPDET range %d\n", 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 < arizona_hpdet_b_ranges[range].threshold ||
  330. val >= ARIZONA_HPDET_B_RANGE_MAX) {
  331. dev_dbg(arizona->dev, "Measurement out of range\n");
  332. return ARIZONA_HPDET_MAX;
  333. }
  334. dev_dbg(arizona->dev, "HPDET read %d in range %d\n", 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. case 2:
  340. if (!(val & ARIZONA_HP_DONE_B)) {
  341. dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
  342. return -EAGAIN;
  343. }
  344. val &= ARIZONA_HP_LVL_B_MASK;
  345. /* Convert to ohms, the value is in 0.5 ohm increments */
  346. val /= 2;
  347. regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  348. &range);
  349. range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
  350. >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
  351. /* Skip up a range, or report? */
  352. if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
  353. (val >= arizona_hpdet_c_ranges[range].max)) {
  354. range++;
  355. dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
  356. arizona_hpdet_c_ranges[range].min,
  357. arizona_hpdet_c_ranges[range].max);
  358. regmap_update_bits(arizona->regmap,
  359. ARIZONA_HEADPHONE_DETECT_1,
  360. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  361. range <<
  362. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  363. return -EAGAIN;
  364. }
  365. if (range && (val < arizona_hpdet_c_ranges[range].min)) {
  366. dev_dbg(arizona->dev, "Reporting range boundary %d\n",
  367. arizona_hpdet_c_ranges[range].min);
  368. val = arizona_hpdet_c_ranges[range].min;
  369. }
  370. break;
  371. default:
  372. dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n", info->hpdet_ip_version);
  373. return -EINVAL;
  374. }
  375. dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
  376. return val;
  377. }
  378. static int arizona_hpdet_do_id(struct arizona_priv *info, int *reading,
  379. bool *mic)
  380. {
  381. struct arizona *arizona = info->arizona;
  382. int id_gpio = arizona->pdata.hpdet_id_gpio;
  383. if (!arizona->pdata.hpdet_acc_id)
  384. return 0;
  385. /*
  386. * If we're using HPDET for accessory identification we need
  387. * to take multiple measurements, step through them in sequence.
  388. */
  389. info->hpdet_res[info->num_hpdet_res++] = *reading;
  390. /* Only check the mic directly if we didn't already ID it */
  391. if (id_gpio && info->num_hpdet_res == 1) {
  392. dev_dbg(arizona->dev, "Measuring mic\n");
  393. regmap_update_bits(arizona->regmap,
  394. ARIZONA_ACCESSORY_DETECT_MODE_1,
  395. ARIZONA_ACCDET_MODE_MASK |
  396. ARIZONA_ACCDET_SRC,
  397. ARIZONA_ACCDET_MODE_HPR |
  398. info->micd_modes[0].src);
  399. gpio_set_value_cansleep(id_gpio, 1);
  400. regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  401. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  402. return -EAGAIN;
  403. }
  404. /* OK, got both. Now, compare... */
  405. dev_dbg(arizona->dev, "HPDET measured %d %d\n",
  406. info->hpdet_res[0], info->hpdet_res[1]);
  407. /* Take the headphone impedance for the main report */
  408. *reading = info->hpdet_res[0];
  409. /* Sometimes we get false readings due to slow insert */
  410. if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
  411. dev_dbg(arizona->dev, "Retrying high impedance\n");
  412. info->num_hpdet_res = 0;
  413. info->hpdet_retried = true;
  414. arizona_start_hpdet_acc_id(info);
  415. pm_runtime_put(arizona->dev);
  416. return -EAGAIN;
  417. }
  418. /*
  419. * If we measure the mic as high impedance
  420. */
  421. if (!id_gpio || info->hpdet_res[1] > 50) {
  422. dev_dbg(arizona->dev, "Detected mic\n");
  423. *mic = true;
  424. info->detecting = true;
  425. } else {
  426. dev_dbg(arizona->dev, "Detected headphone\n");
  427. }
  428. /* Make sure everything is reset back to the real polarity */
  429. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  430. ARIZONA_ACCDET_SRC, info->micd_modes[0].src);
  431. return 0;
  432. }
  433. static irqreturn_t arizona_hpdet_irq(int irq, void *data)
  434. {
  435. struct arizona_priv *info = data;
  436. struct arizona *arizona = info->arizona;
  437. int id_gpio = arizona->pdata.hpdet_id_gpio;
  438. int ret, reading, state, report;
  439. bool mic = false;
  440. mutex_lock(&info->lock);
  441. /* If we got a spurious IRQ for some reason then ignore it */
  442. if (!info->hpdet_active) {
  443. dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
  444. mutex_unlock(&info->lock);
  445. return IRQ_NONE;
  446. }
  447. /* If the cable was removed while measuring ignore the result */
  448. state = info->jack->status & SND_JACK_MECHANICAL;
  449. if (!state) {
  450. dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
  451. goto done;
  452. }
  453. ret = arizona_hpdet_read(info);
  454. if (ret == -EAGAIN)
  455. goto out;
  456. else if (ret < 0)
  457. goto done;
  458. reading = ret;
  459. /* Reset back to starting range */
  460. regmap_update_bits(arizona->regmap,
  461. ARIZONA_HEADPHONE_DETECT_1,
  462. ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
  463. 0);
  464. ret = arizona_hpdet_do_id(info, &reading, &mic);
  465. if (ret == -EAGAIN)
  466. goto out;
  467. else if (ret < 0)
  468. goto done;
  469. /* Report high impedence cables as line outputs */
  470. if (reading >= 5000)
  471. report = SND_JACK_LINEOUT;
  472. else
  473. report = SND_JACK_HEADPHONE;
  474. snd_soc_jack_report(info->jack, report, SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
  475. done:
  476. /* Reset back to starting range */
  477. regmap_update_bits(arizona->regmap,
  478. ARIZONA_HEADPHONE_DETECT_1,
  479. ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
  480. 0);
  481. arizona_extcon_hp_clamp(info, false);
  482. if (id_gpio)
  483. gpio_set_value_cansleep(id_gpio, 0);
  484. /* If we have a mic then reenable MICDET */
  485. if (state && (mic || info->mic))
  486. arizona_start_mic(info);
  487. if (info->hpdet_active) {
  488. pm_runtime_put_autosuspend(arizona->dev);
  489. info->hpdet_active = false;
  490. }
  491. /* Do not set hp_det done when the cable has been unplugged */
  492. if (state)
  493. info->hpdet_done = true;
  494. out:
  495. mutex_unlock(&info->lock);
  496. return IRQ_HANDLED;
  497. }
  498. static void arizona_identify_headphone(struct arizona_priv *info)
  499. {
  500. struct arizona *arizona = info->arizona;
  501. int ret;
  502. if (info->hpdet_done)
  503. return;
  504. dev_dbg(arizona->dev, "Starting HPDET\n");
  505. /* Make sure we keep the device enabled during the measurement */
  506. pm_runtime_get_sync(arizona->dev);
  507. info->hpdet_active = true;
  508. arizona_stop_mic(info);
  509. arizona_extcon_hp_clamp(info, true);
  510. ret = regmap_update_bits(arizona->regmap,
  511. ARIZONA_ACCESSORY_DETECT_MODE_1,
  512. ARIZONA_ACCDET_MODE_MASK,
  513. arizona->pdata.hpdet_channel);
  514. if (ret != 0) {
  515. dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
  516. goto err;
  517. }
  518. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  519. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  520. if (ret) {
  521. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
  522. goto err;
  523. }
  524. return;
  525. err:
  526. arizona_extcon_hp_clamp(info, false);
  527. pm_runtime_put_autosuspend(arizona->dev);
  528. /* Just report headphone */
  529. snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
  530. SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
  531. if (info->mic)
  532. arizona_start_mic(info);
  533. info->hpdet_active = false;
  534. }
  535. static void arizona_start_hpdet_acc_id(struct arizona_priv *info)
  536. {
  537. struct arizona *arizona = info->arizona;
  538. int hp_reading = 32;
  539. bool mic;
  540. int ret;
  541. dev_dbg(arizona->dev, "Starting identification via HPDET\n");
  542. /* Make sure we keep the device enabled during the measurement */
  543. pm_runtime_get_sync(arizona->dev);
  544. info->hpdet_active = true;
  545. arizona_extcon_hp_clamp(info, true);
  546. ret = regmap_update_bits(arizona->regmap,
  547. ARIZONA_ACCESSORY_DETECT_MODE_1,
  548. ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
  549. info->micd_modes[0].src |
  550. arizona->pdata.hpdet_channel);
  551. if (ret != 0) {
  552. dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
  553. goto err;
  554. }
  555. if (arizona->pdata.hpdet_acc_id_line) {
  556. ret = regmap_update_bits(arizona->regmap,
  557. ARIZONA_HEADPHONE_DETECT_1,
  558. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  559. if (ret) {
  560. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
  561. goto err;
  562. }
  563. } else {
  564. arizona_hpdet_do_id(info, &hp_reading, &mic);
  565. }
  566. return;
  567. err:
  568. /* Just report headphone */
  569. snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
  570. SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
  571. info->hpdet_active = false;
  572. }
  573. static void arizona_micd_timeout_work(struct work_struct *work)
  574. {
  575. struct arizona_priv *info = container_of(work,
  576. struct arizona_priv,
  577. micd_timeout_work.work);
  578. mutex_lock(&info->lock);
  579. dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
  580. info->detecting = false;
  581. arizona_identify_headphone(info);
  582. mutex_unlock(&info->lock);
  583. }
  584. static int arizona_micd_adc_read(struct arizona_priv *info)
  585. {
  586. struct arizona *arizona = info->arizona;
  587. unsigned int val;
  588. int ret;
  589. /* Must disable MICD before we read the ADCVAL */
  590. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  591. ARIZONA_MICD_ENA, 0);
  592. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val);
  593. if (ret) {
  594. dev_err(arizona->dev, "Failed to read MICDET_ADCVAL: %d\n", ret);
  595. return ret;
  596. }
  597. dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val);
  598. val &= ARIZONA_MICDET_ADCVAL_MASK;
  599. if (val < ARRAY_SIZE(arizona_micd_levels))
  600. val = arizona_micd_levels[val];
  601. else
  602. val = INT_MAX;
  603. if (val <= QUICK_HEADPHONE_MAX_OHM)
  604. val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0;
  605. else if (val <= MICROPHONE_MIN_OHM)
  606. val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1;
  607. else if (val <= MICROPHONE_MAX_OHM)
  608. val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8;
  609. else
  610. val = ARIZONA_MICD_LVL_8;
  611. return val;
  612. }
  613. static int arizona_micd_read(struct arizona_priv *info)
  614. {
  615. struct arizona *arizona = info->arizona;
  616. unsigned int val = 0;
  617. int ret, i;
  618. for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
  619. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  620. if (ret) {
  621. dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
  622. return ret;
  623. }
  624. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  625. if (!(val & ARIZONA_MICD_VALID)) {
  626. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  627. return -EINVAL;
  628. }
  629. }
  630. if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
  631. dev_err(arizona->dev, "Failed to get valid MICDET value\n");
  632. return -EINVAL;
  633. }
  634. return val;
  635. }
  636. static int arizona_micdet_reading(void *priv)
  637. {
  638. struct arizona_priv *info = priv;
  639. struct arizona *arizona = info->arizona;
  640. int ret, val;
  641. if (info->detecting && arizona->pdata.micd_software_compare)
  642. ret = arizona_micd_adc_read(info);
  643. else
  644. ret = arizona_micd_read(info);
  645. if (ret < 0)
  646. return ret;
  647. val = ret;
  648. /* Due to jack detect this should never happen */
  649. if (!(val & ARIZONA_MICD_STS)) {
  650. dev_warn(arizona->dev, "Detected open circuit\n");
  651. info->mic = false;
  652. info->detecting = false;
  653. arizona_identify_headphone(info);
  654. return 0;
  655. }
  656. /* If we got a high impedence we should have a headset, report it. */
  657. if (val & ARIZONA_MICD_LVL_8) {
  658. info->mic = true;
  659. info->detecting = false;
  660. arizona_identify_headphone(info);
  661. snd_soc_jack_report(info->jack, SND_JACK_MICROPHONE, SND_JACK_MICROPHONE);
  662. /* Don't need to regulate for button detection */
  663. ret = regulator_allow_bypass(info->micvdd, true);
  664. if (ret)
  665. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
  666. return 0;
  667. }
  668. /* If we detected a lower impedence during initial startup
  669. * then we probably have the wrong polarity, flip it. Don't
  670. * do this for the lowest impedences to speed up detection of
  671. * plain headphones. If both polarities report a low
  672. * impedence then give up and report headphones.
  673. */
  674. if (val & MICD_LVL_1_TO_7) {
  675. if (info->jack_flips >= info->micd_num_modes * 10) {
  676. dev_dbg(arizona->dev, "Detected HP/line\n");
  677. info->detecting = false;
  678. arizona_identify_headphone(info);
  679. } else {
  680. info->micd_mode++;
  681. if (info->micd_mode == info->micd_num_modes)
  682. info->micd_mode = 0;
  683. arizona_extcon_set_mode(info, info->micd_mode);
  684. info->jack_flips++;
  685. if (arizona->pdata.micd_software_compare)
  686. regmap_update_bits(arizona->regmap,
  687. ARIZONA_MIC_DETECT_1,
  688. ARIZONA_MICD_ENA,
  689. ARIZONA_MICD_ENA);
  690. queue_delayed_work(system_power_efficient_wq,
  691. &info->micd_timeout_work,
  692. msecs_to_jiffies(arizona->pdata.micd_timeout));
  693. }
  694. return 0;
  695. }
  696. /*
  697. * If we're still detecting and we detect a short then we've
  698. * got a headphone.
  699. */
  700. dev_dbg(arizona->dev, "Headphone detected\n");
  701. info->detecting = false;
  702. arizona_identify_headphone(info);
  703. return 0;
  704. }
  705. static int arizona_button_reading(void *priv)
  706. {
  707. struct arizona_priv *info = priv;
  708. struct arizona *arizona = info->arizona;
  709. int val, key, lvl;
  710. val = arizona_micd_read(info);
  711. if (val < 0)
  712. return val;
  713. /*
  714. * If we're still detecting and we detect a short then we've
  715. * got a headphone. Otherwise it's a button press.
  716. */
  717. if (val & MICD_LVL_0_TO_7) {
  718. if (info->mic) {
  719. dev_dbg(arizona->dev, "Mic button detected\n");
  720. lvl = val & ARIZONA_MICD_LVL_MASK;
  721. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  722. if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
  723. key = ffs(lvl) - 1;
  724. snd_soc_jack_report(info->jack,
  725. SND_JACK_BTN_0 >> key,
  726. info->micd_button_mask);
  727. } else {
  728. dev_err(arizona->dev, "Button out of range\n");
  729. }
  730. } else {
  731. dev_warn(arizona->dev, "Button with no mic: %x\n", val);
  732. }
  733. } else {
  734. dev_dbg(arizona->dev, "Mic button released\n");
  735. snd_soc_jack_report(info->jack, 0, info->micd_button_mask);
  736. arizona_extcon_pulse_micbias(info);
  737. }
  738. return 0;
  739. }
  740. static void arizona_micd_detect(struct work_struct *work)
  741. {
  742. struct arizona_priv *info = container_of(work,
  743. struct arizona_priv,
  744. micd_detect_work.work);
  745. struct arizona *arizona = info->arizona;
  746. cancel_delayed_work_sync(&info->micd_timeout_work);
  747. mutex_lock(&info->lock);
  748. /* If the cable was removed while measuring ignore the result */
  749. if (!(info->jack->status & SND_JACK_MECHANICAL)) {
  750. dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
  751. mutex_unlock(&info->lock);
  752. return;
  753. }
  754. if (info->detecting)
  755. arizona_micdet_reading(info);
  756. else
  757. arizona_button_reading(info);
  758. pm_runtime_mark_last_busy(arizona->dev);
  759. mutex_unlock(&info->lock);
  760. }
  761. static irqreturn_t arizona_micdet(int irq, void *data)
  762. {
  763. struct arizona_priv *info = data;
  764. struct arizona *arizona = info->arizona;
  765. int debounce = arizona->pdata.micd_detect_debounce;
  766. cancel_delayed_work_sync(&info->micd_detect_work);
  767. cancel_delayed_work_sync(&info->micd_timeout_work);
  768. mutex_lock(&info->lock);
  769. if (!info->detecting)
  770. debounce = 0;
  771. mutex_unlock(&info->lock);
  772. if (debounce)
  773. queue_delayed_work(system_power_efficient_wq,
  774. &info->micd_detect_work,
  775. msecs_to_jiffies(debounce));
  776. else
  777. arizona_micd_detect(&info->micd_detect_work.work);
  778. return IRQ_HANDLED;
  779. }
  780. static void arizona_hpdet_work(struct work_struct *work)
  781. {
  782. struct arizona_priv *info = container_of(work,
  783. struct arizona_priv,
  784. hpdet_work.work);
  785. mutex_lock(&info->lock);
  786. arizona_start_hpdet_acc_id(info);
  787. mutex_unlock(&info->lock);
  788. }
  789. static int arizona_hpdet_wait(struct arizona_priv *info)
  790. {
  791. struct arizona *arizona = info->arizona;
  792. unsigned int val;
  793. int i, ret;
  794. for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
  795. ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
  796. &val);
  797. if (ret) {
  798. dev_err(arizona->dev, "Failed to read HPDET state: %d\n", ret);
  799. return ret;
  800. }
  801. switch (info->hpdet_ip_version) {
  802. case 0:
  803. if (val & ARIZONA_HP_DONE)
  804. return 0;
  805. break;
  806. default:
  807. if (val & ARIZONA_HP_DONE_B)
  808. return 0;
  809. break;
  810. }
  811. msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
  812. }
  813. dev_warn(arizona->dev, "HPDET did not appear to complete\n");
  814. return -ETIMEDOUT;
  815. }
  816. static irqreturn_t arizona_jackdet(int irq, void *data)
  817. {
  818. struct arizona_priv *info = data;
  819. struct arizona *arizona = info->arizona;
  820. unsigned int val, present, mask;
  821. bool cancelled_hp, cancelled_mic;
  822. int ret, i;
  823. cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
  824. cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
  825. pm_runtime_get_sync(arizona->dev);
  826. mutex_lock(&info->lock);
  827. if (info->micd_clamp) {
  828. mask = ARIZONA_MICD_CLAMP_STS;
  829. present = 0;
  830. } else {
  831. mask = ARIZONA_JD1_STS;
  832. if (arizona->pdata.jd_invert)
  833. present = 0;
  834. else
  835. present = ARIZONA_JD1_STS;
  836. }
  837. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  838. if (ret) {
  839. dev_err(arizona->dev, "Failed to read jackdet status: %d\n", ret);
  840. mutex_unlock(&info->lock);
  841. pm_runtime_put_autosuspend(arizona->dev);
  842. return IRQ_NONE;
  843. }
  844. val &= mask;
  845. if (val == info->last_jackdet) {
  846. dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
  847. if (cancelled_hp)
  848. queue_delayed_work(system_power_efficient_wq,
  849. &info->hpdet_work,
  850. msecs_to_jiffies(HPDET_DEBOUNCE));
  851. if (cancelled_mic) {
  852. int micd_timeout = arizona->pdata.micd_timeout;
  853. queue_delayed_work(system_power_efficient_wq,
  854. &info->micd_timeout_work,
  855. msecs_to_jiffies(micd_timeout));
  856. }
  857. goto out;
  858. }
  859. info->last_jackdet = val;
  860. if (info->last_jackdet == present) {
  861. dev_dbg(arizona->dev, "Detected jack\n");
  862. snd_soc_jack_report(info->jack, SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
  863. info->detecting = true;
  864. info->mic = false;
  865. info->jack_flips = 0;
  866. if (!arizona->pdata.hpdet_acc_id) {
  867. arizona_start_mic(info);
  868. } else {
  869. queue_delayed_work(system_power_efficient_wq,
  870. &info->hpdet_work,
  871. msecs_to_jiffies(HPDET_DEBOUNCE));
  872. }
  873. if (info->micd_clamp || !arizona->pdata.jd_invert)
  874. regmap_update_bits(arizona->regmap,
  875. ARIZONA_JACK_DETECT_DEBOUNCE,
  876. ARIZONA_MICD_CLAMP_DB |
  877. ARIZONA_JD1_DB, 0);
  878. } else {
  879. dev_dbg(arizona->dev, "Detected jack removal\n");
  880. arizona_stop_mic(info);
  881. info->num_hpdet_res = 0;
  882. for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
  883. info->hpdet_res[i] = 0;
  884. info->mic = false;
  885. info->hpdet_done = false;
  886. info->hpdet_retried = false;
  887. snd_soc_jack_report(info->jack, 0, ARIZONA_JACK_MASK | info->micd_button_mask);
  888. /*
  889. * If the jack was removed during a headphone detection we
  890. * need to wait for the headphone detection to finish, as
  891. * it can not be aborted. We don't want to be able to start
  892. * a new headphone detection from a fresh insert until this
  893. * one is finished.
  894. */
  895. arizona_hpdet_wait(info);
  896. regmap_update_bits(arizona->regmap,
  897. ARIZONA_JACK_DETECT_DEBOUNCE,
  898. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
  899. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
  900. }
  901. out:
  902. /* Clear trig_sts to make sure DCVDD is not forced up */
  903. regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
  904. ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
  905. ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
  906. ARIZONA_JD1_FALL_TRIG_STS |
  907. ARIZONA_JD1_RISE_TRIG_STS);
  908. mutex_unlock(&info->lock);
  909. pm_runtime_mark_last_busy(arizona->dev);
  910. pm_runtime_put_autosuspend(arizona->dev);
  911. return IRQ_HANDLED;
  912. }
  913. /* Map a level onto a slot in the register bank */
  914. static void arizona_micd_set_level(struct arizona *arizona, int index,
  915. unsigned int level)
  916. {
  917. int reg;
  918. unsigned int mask;
  919. reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
  920. if (!(index % 2)) {
  921. mask = 0x3f00;
  922. level <<= 8;
  923. } else {
  924. mask = 0x3f;
  925. }
  926. /* Program the level itself */
  927. regmap_update_bits(arizona->regmap, reg, mask, level);
  928. }
  929. static int arizona_extcon_get_micd_configs(struct device *dev,
  930. struct arizona *arizona)
  931. {
  932. const char * const prop = "wlf,micd-configs";
  933. const int entries_per_config = 3;
  934. struct arizona_micd_config *micd_configs;
  935. int nconfs, ret;
  936. int i, j;
  937. u32 *vals;
  938. nconfs = device_property_count_u32(arizona->dev, prop);
  939. if (nconfs <= 0)
  940. return 0;
  941. vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL);
  942. if (!vals)
  943. return -ENOMEM;
  944. ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs);
  945. if (ret < 0)
  946. goto out;
  947. nconfs /= entries_per_config;
  948. micd_configs = devm_kcalloc(dev, nconfs, sizeof(*micd_configs),
  949. GFP_KERNEL);
  950. if (!micd_configs) {
  951. ret = -ENOMEM;
  952. goto out;
  953. }
  954. for (i = 0, j = 0; i < nconfs; ++i) {
  955. micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0;
  956. micd_configs[i].bias = vals[j++];
  957. micd_configs[i].gpio = vals[j++];
  958. }
  959. arizona->pdata.micd_configs = micd_configs;
  960. arizona->pdata.num_micd_configs = nconfs;
  961. out:
  962. kfree(vals);
  963. return ret;
  964. }
  965. static int arizona_extcon_device_get_pdata(struct device *dev,
  966. struct arizona *arizona)
  967. {
  968. struct arizona_pdata *pdata = &arizona->pdata;
  969. unsigned int val = ARIZONA_ACCDET_MODE_HPL;
  970. int ret;
  971. device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
  972. switch (val) {
  973. case ARIZONA_ACCDET_MODE_HPL:
  974. case ARIZONA_ACCDET_MODE_HPR:
  975. pdata->hpdet_channel = val;
  976. break;
  977. default:
  978. dev_err(arizona->dev, "Wrong wlf,hpdet-channel DT value %d\n", val);
  979. pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
  980. }
  981. device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
  982. &pdata->micd_detect_debounce);
  983. device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
  984. &pdata->micd_bias_start_time);
  985. device_property_read_u32(arizona->dev, "wlf,micd-rate",
  986. &pdata->micd_rate);
  987. device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
  988. &pdata->micd_dbtime);
  989. device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms",
  990. &pdata->micd_timeout);
  991. pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
  992. "wlf,micd-force-micbias");
  993. pdata->micd_software_compare = device_property_read_bool(arizona->dev,
  994. "wlf,micd-software-compare");
  995. pdata->jd_invert = device_property_read_bool(arizona->dev,
  996. "wlf,jd-invert");
  997. device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw);
  998. pdata->jd_gpio5 = device_property_read_bool(arizona->dev,
  999. "wlf,use-jd2");
  1000. pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev,
  1001. "wlf,use-jd2-nopull");
  1002. ret = arizona_extcon_get_micd_configs(dev, arizona);
  1003. if (ret < 0)
  1004. dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret);
  1005. return 0;
  1006. }
  1007. int arizona_jack_codec_dev_probe(struct arizona_priv *info, struct device *dev)
  1008. {
  1009. struct arizona *arizona = info->arizona;
  1010. struct arizona_pdata *pdata = &arizona->pdata;
  1011. int ret, mode;
  1012. if (!dev_get_platdata(arizona->dev))
  1013. arizona_extcon_device_get_pdata(dev, arizona);
  1014. info->micvdd = devm_regulator_get(dev, "MICVDD");
  1015. if (IS_ERR(info->micvdd))
  1016. return dev_err_probe(arizona->dev, PTR_ERR(info->micvdd), "getting MICVDD\n");
  1017. mutex_init(&info->lock);
  1018. info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
  1019. INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
  1020. INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
  1021. INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
  1022. switch (arizona->type) {
  1023. case WM5102:
  1024. switch (arizona->rev) {
  1025. case 0:
  1026. info->micd_reva = true;
  1027. break;
  1028. default:
  1029. info->micd_clamp = true;
  1030. info->hpdet_ip_version = 1;
  1031. break;
  1032. }
  1033. break;
  1034. case WM5110:
  1035. case WM8280:
  1036. switch (arizona->rev) {
  1037. case 0 ... 2:
  1038. break;
  1039. default:
  1040. info->micd_clamp = true;
  1041. info->hpdet_ip_version = 2;
  1042. break;
  1043. }
  1044. break;
  1045. case WM8998:
  1046. case WM1814:
  1047. info->micd_clamp = true;
  1048. info->hpdet_ip_version = 2;
  1049. break;
  1050. default:
  1051. break;
  1052. }
  1053. if (!pdata->micd_timeout)
  1054. pdata->micd_timeout = DEFAULT_MICD_TIMEOUT;
  1055. if (pdata->num_micd_configs) {
  1056. info->micd_modes = pdata->micd_configs;
  1057. info->micd_num_modes = pdata->num_micd_configs;
  1058. } else {
  1059. info->micd_modes = micd_default_modes;
  1060. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  1061. }
  1062. if (arizona->pdata.gpsw > 0)
  1063. regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1,
  1064. ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw);
  1065. if (pdata->micd_pol_gpio > 0) {
  1066. if (info->micd_modes[0].gpio)
  1067. mode = GPIOF_OUT_INIT_HIGH;
  1068. else
  1069. mode = GPIOF_OUT_INIT_LOW;
  1070. ret = devm_gpio_request_one(dev, pdata->micd_pol_gpio,
  1071. mode, "MICD polarity");
  1072. if (ret != 0) {
  1073. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  1074. pdata->micd_pol_gpio, ret);
  1075. return ret;
  1076. }
  1077. info->micd_pol_gpio = gpio_to_desc(pdata->micd_pol_gpio);
  1078. } else {
  1079. if (info->micd_modes[0].gpio)
  1080. mode = GPIOD_OUT_HIGH;
  1081. else
  1082. mode = GPIOD_OUT_LOW;
  1083. /* We can't use devm here because we need to do the get
  1084. * against the MFD device, as that is where the of_node
  1085. * will reside, but if we devm against that the GPIO
  1086. * will not be freed if the extcon driver is unloaded.
  1087. */
  1088. info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
  1089. "wlf,micd-pol",
  1090. mode);
  1091. if (IS_ERR(info->micd_pol_gpio)) {
  1092. ret = PTR_ERR(info->micd_pol_gpio);
  1093. dev_err_probe(arizona->dev, ret, "getting microphone polarity GPIO\n");
  1094. return ret;
  1095. }
  1096. }
  1097. if (arizona->pdata.hpdet_id_gpio > 0) {
  1098. ret = devm_gpio_request_one(dev, arizona->pdata.hpdet_id_gpio,
  1099. GPIOF_OUT_INIT_LOW,
  1100. "HPDET");
  1101. if (ret != 0) {
  1102. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  1103. arizona->pdata.hpdet_id_gpio, ret);
  1104. gpiod_put(info->micd_pol_gpio);
  1105. return ret;
  1106. }
  1107. }
  1108. return 0;
  1109. }
  1110. EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_probe);
  1111. int arizona_jack_codec_dev_remove(struct arizona_priv *info)
  1112. {
  1113. gpiod_put(info->micd_pol_gpio);
  1114. return 0;
  1115. }
  1116. EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_remove);
  1117. static int arizona_jack_enable_jack_detect(struct arizona_priv *info,
  1118. struct snd_soc_jack *jack)
  1119. {
  1120. struct arizona *arizona = info->arizona;
  1121. struct arizona_pdata *pdata = &arizona->pdata;
  1122. unsigned int val;
  1123. unsigned int clamp_mode;
  1124. int jack_irq_fall, jack_irq_rise;
  1125. int ret, i, j;
  1126. if (arizona->pdata.micd_bias_start_time)
  1127. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1128. ARIZONA_MICD_BIAS_STARTTIME_MASK,
  1129. arizona->pdata.micd_bias_start_time
  1130. << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
  1131. if (arizona->pdata.micd_rate)
  1132. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1133. ARIZONA_MICD_RATE_MASK,
  1134. arizona->pdata.micd_rate
  1135. << ARIZONA_MICD_RATE_SHIFT);
  1136. switch (arizona->pdata.micd_dbtime) {
  1137. case MICD_DBTIME_FOUR_READINGS:
  1138. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1139. ARIZONA_MICD_DBTIME_MASK,
  1140. ARIZONA_MICD_DBTIME);
  1141. break;
  1142. case MICD_DBTIME_TWO_READINGS:
  1143. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1144. ARIZONA_MICD_DBTIME_MASK, 0);
  1145. break;
  1146. default:
  1147. break;
  1148. }
  1149. BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) <
  1150. ARIZONA_NUM_MICD_BUTTON_LEVELS);
  1151. if (arizona->pdata.num_micd_ranges) {
  1152. info->micd_ranges = pdata->micd_ranges;
  1153. info->num_micd_ranges = pdata->num_micd_ranges;
  1154. } else {
  1155. info->micd_ranges = micd_default_ranges;
  1156. info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
  1157. }
  1158. if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_BUTTONS) {
  1159. dev_err(arizona->dev, "Too many MICD ranges: %d > %d\n",
  1160. arizona->pdata.num_micd_ranges, ARIZONA_MAX_MICD_BUTTONS);
  1161. return -EINVAL;
  1162. }
  1163. if (info->num_micd_ranges > 1) {
  1164. for (i = 1; i < info->num_micd_ranges; i++) {
  1165. if (info->micd_ranges[i - 1].max >
  1166. info->micd_ranges[i].max) {
  1167. dev_err(arizona->dev, "MICD ranges must be sorted\n");
  1168. return -EINVAL;
  1169. }
  1170. }
  1171. }
  1172. /* Disable all buttons by default */
  1173. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  1174. ARIZONA_MICD_LVL_SEL_MASK, 0x81);
  1175. /* Set up all the buttons the user specified */
  1176. for (i = 0; i < info->num_micd_ranges; i++) {
  1177. for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++)
  1178. if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
  1179. break;
  1180. if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) {
  1181. dev_err(arizona->dev, "Unsupported MICD level %d\n",
  1182. info->micd_ranges[i].max);
  1183. return -EINVAL;
  1184. }
  1185. dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
  1186. arizona_micd_levels[j], i);
  1187. arizona_micd_set_level(arizona, i, j);
  1188. /* SND_JACK_BTN_# masks start with the most significant bit */
  1189. info->micd_button_mask |= SND_JACK_BTN_0 >> i;
  1190. snd_jack_set_key(jack->jack, SND_JACK_BTN_0 >> i,
  1191. info->micd_ranges[i].key);
  1192. /* Enable reporting of that range */
  1193. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  1194. 1 << i, 1 << i);
  1195. }
  1196. /* Set all the remaining keys to a maximum */
  1197. for (; i < ARIZONA_MAX_MICD_RANGE; i++)
  1198. arizona_micd_set_level(arizona, i, 0x3f);
  1199. /*
  1200. * If we have a clamp use it, activating in conjunction with
  1201. * GPIO5 if that is connected for jack detect operation.
  1202. */
  1203. if (info->micd_clamp) {
  1204. if (arizona->pdata.jd_gpio5) {
  1205. /* Put the GPIO into input mode with optional pull */
  1206. val = 0xc101;
  1207. if (arizona->pdata.jd_gpio5_nopull)
  1208. val &= ~ARIZONA_GPN_PU;
  1209. regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
  1210. val);
  1211. if (arizona->pdata.jd_invert)
  1212. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
  1213. else
  1214. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
  1215. } else {
  1216. if (arizona->pdata.jd_invert)
  1217. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
  1218. else
  1219. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
  1220. }
  1221. regmap_update_bits(arizona->regmap,
  1222. ARIZONA_MICD_CLAMP_CONTROL,
  1223. ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
  1224. regmap_update_bits(arizona->regmap,
  1225. ARIZONA_JACK_DETECT_DEBOUNCE,
  1226. ARIZONA_MICD_CLAMP_DB,
  1227. ARIZONA_MICD_CLAMP_DB);
  1228. }
  1229. arizona_extcon_set_mode(info, 0);
  1230. info->jack = jack;
  1231. pm_runtime_get_sync(arizona->dev);
  1232. if (info->micd_clamp) {
  1233. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1234. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1235. } else {
  1236. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1237. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1238. }
  1239. ret = arizona_request_irq(arizona, jack_irq_rise,
  1240. "JACKDET rise", arizona_jackdet, info);
  1241. if (ret != 0) {
  1242. dev_err(arizona->dev, "Failed to get JACKDET rise IRQ: %d\n", ret);
  1243. goto err_pm;
  1244. }
  1245. ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
  1246. if (ret != 0) {
  1247. dev_err(arizona->dev, "Failed to set JD rise IRQ wake: %d\n", ret);
  1248. goto err_rise;
  1249. }
  1250. ret = arizona_request_irq(arizona, jack_irq_fall,
  1251. "JACKDET fall", arizona_jackdet, info);
  1252. if (ret != 0) {
  1253. dev_err(arizona->dev, "Failed to get JD fall IRQ: %d\n", ret);
  1254. goto err_rise_wake;
  1255. }
  1256. ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
  1257. if (ret != 0) {
  1258. dev_err(arizona->dev, "Failed to set JD fall IRQ wake: %d\n", ret);
  1259. goto err_fall;
  1260. }
  1261. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  1262. "MICDET", arizona_micdet, info);
  1263. if (ret != 0) {
  1264. dev_err(arizona->dev, "Failed to get MICDET IRQ: %d\n", ret);
  1265. goto err_fall_wake;
  1266. }
  1267. ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
  1268. "HPDET", arizona_hpdet_irq, info);
  1269. if (ret != 0) {
  1270. dev_err(arizona->dev, "Failed to get HPDET IRQ: %d\n", ret);
  1271. goto err_micdet;
  1272. }
  1273. arizona_clk32k_enable(arizona);
  1274. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  1275. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  1276. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1277. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  1278. ret = regulator_allow_bypass(info->micvdd, true);
  1279. if (ret != 0)
  1280. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n", ret);
  1281. pm_runtime_put(arizona->dev);
  1282. return 0;
  1283. err_micdet:
  1284. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1285. err_fall_wake:
  1286. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1287. err_fall:
  1288. arizona_free_irq(arizona, jack_irq_fall, info);
  1289. err_rise_wake:
  1290. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1291. err_rise:
  1292. arizona_free_irq(arizona, jack_irq_rise, info);
  1293. err_pm:
  1294. pm_runtime_put(arizona->dev);
  1295. info->jack = NULL;
  1296. return ret;
  1297. }
  1298. static int arizona_jack_disable_jack_detect(struct arizona_priv *info)
  1299. {
  1300. struct arizona *arizona = info->arizona;
  1301. int jack_irq_rise, jack_irq_fall;
  1302. bool change;
  1303. int ret;
  1304. if (!info->jack)
  1305. return 0;
  1306. if (info->micd_clamp) {
  1307. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1308. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1309. } else {
  1310. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1311. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1312. }
  1313. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1314. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1315. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1316. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1317. arizona_free_irq(arizona, jack_irq_rise, info);
  1318. arizona_free_irq(arizona, jack_irq_fall, info);
  1319. cancel_delayed_work_sync(&info->hpdet_work);
  1320. cancel_delayed_work_sync(&info->micd_detect_work);
  1321. cancel_delayed_work_sync(&info->micd_timeout_work);
  1322. ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1323. ARIZONA_MICD_ENA, 0,
  1324. &change);
  1325. if (ret < 0) {
  1326. dev_err(arizona->dev, "Failed to disable micd on remove: %d\n", ret);
  1327. } else if (change) {
  1328. regulator_disable(info->micvdd);
  1329. pm_runtime_put(arizona->dev);
  1330. }
  1331. regmap_update_bits(arizona->regmap,
  1332. ARIZONA_MICD_CLAMP_CONTROL,
  1333. ARIZONA_MICD_CLAMP_MODE_MASK, 0);
  1334. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1335. ARIZONA_JD1_ENA, 0);
  1336. arizona_clk32k_disable(arizona);
  1337. info->jack = NULL;
  1338. return 0;
  1339. }
  1340. int arizona_jack_set_jack(struct snd_soc_component *component,
  1341. struct snd_soc_jack *jack, void *data)
  1342. {
  1343. struct arizona_priv *info = snd_soc_component_get_drvdata(component);
  1344. if (jack)
  1345. return arizona_jack_enable_jack_detect(info, jack);
  1346. else
  1347. return arizona_jack_disable_jack_detect(info);
  1348. }
  1349. EXPORT_SYMBOL_GPL(arizona_jack_set_jack);