extcon-arizona.c 40 KB

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