extcon-arizona.c 42 KB

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