extcon-arizona.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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. snd_soc_dapm_mutex_lock(arizona->dapm);
  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. snd_soc_dapm_mutex_unlock(arizona->dapm);
  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_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_state_sync(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_state_sync(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_state_sync(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_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_state_sync(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_state_sync(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. for (i = 0; i < ARRAY_SIZE(arizona_cable) - 1; i++) {
  941. ret = extcon_set_state_sync(info->edev,
  942. arizona_cable[i], false);
  943. if (ret != 0)
  944. dev_err(arizona->dev,
  945. "Removal report failed: %d\n", ret);
  946. }
  947. regmap_update_bits(arizona->regmap,
  948. ARIZONA_JACK_DETECT_DEBOUNCE,
  949. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
  950. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
  951. }
  952. if (arizona->pdata.micd_timeout)
  953. info->micd_timeout = arizona->pdata.micd_timeout;
  954. else
  955. info->micd_timeout = DEFAULT_MICD_TIMEOUT;
  956. out:
  957. /* Clear trig_sts to make sure DCVDD is not forced up */
  958. regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
  959. ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
  960. ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
  961. ARIZONA_JD1_FALL_TRIG_STS |
  962. ARIZONA_JD1_RISE_TRIG_STS);
  963. mutex_unlock(&info->lock);
  964. pm_runtime_mark_last_busy(info->dev);
  965. pm_runtime_put_autosuspend(info->dev);
  966. return IRQ_HANDLED;
  967. }
  968. /* Map a level onto a slot in the register bank */
  969. static void arizona_micd_set_level(struct arizona *arizona, int index,
  970. unsigned int level)
  971. {
  972. int reg;
  973. unsigned int mask;
  974. reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
  975. if (!(index % 2)) {
  976. mask = 0x3f00;
  977. level <<= 8;
  978. } else {
  979. mask = 0x3f;
  980. }
  981. /* Program the level itself */
  982. regmap_update_bits(arizona->regmap, reg, mask, level);
  983. }
  984. static int arizona_extcon_get_micd_configs(struct device *dev,
  985. struct arizona *arizona)
  986. {
  987. const char * const prop = "wlf,micd-configs";
  988. const int entries_per_config = 3;
  989. struct arizona_micd_config *micd_configs;
  990. int nconfs, ret;
  991. int i, j;
  992. u32 *vals;
  993. nconfs = device_property_read_u32_array(arizona->dev, prop, NULL, 0);
  994. if (nconfs <= 0)
  995. return 0;
  996. vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL);
  997. if (!vals)
  998. return -ENOMEM;
  999. ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs);
  1000. if (ret < 0)
  1001. goto out;
  1002. nconfs /= entries_per_config;
  1003. micd_configs = devm_kzalloc(dev,
  1004. nconfs * sizeof(struct arizona_micd_range),
  1005. GFP_KERNEL);
  1006. if (!micd_configs) {
  1007. ret = -ENOMEM;
  1008. goto out;
  1009. }
  1010. for (i = 0, j = 0; i < nconfs; ++i) {
  1011. micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0;
  1012. micd_configs[i].bias = vals[j++];
  1013. micd_configs[i].gpio = vals[j++];
  1014. }
  1015. arizona->pdata.micd_configs = micd_configs;
  1016. arizona->pdata.num_micd_configs = nconfs;
  1017. out:
  1018. kfree(vals);
  1019. return ret;
  1020. }
  1021. static int arizona_extcon_device_get_pdata(struct device *dev,
  1022. struct arizona *arizona)
  1023. {
  1024. struct arizona_pdata *pdata = &arizona->pdata;
  1025. unsigned int val = ARIZONA_ACCDET_MODE_HPL;
  1026. int ret;
  1027. device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
  1028. switch (val) {
  1029. case ARIZONA_ACCDET_MODE_HPL:
  1030. case ARIZONA_ACCDET_MODE_HPR:
  1031. pdata->hpdet_channel = val;
  1032. break;
  1033. default:
  1034. dev_err(arizona->dev,
  1035. "Wrong wlf,hpdet-channel DT value %d\n", val);
  1036. pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
  1037. }
  1038. device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
  1039. &pdata->micd_detect_debounce);
  1040. device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
  1041. &pdata->micd_bias_start_time);
  1042. device_property_read_u32(arizona->dev, "wlf,micd-rate",
  1043. &pdata->micd_rate);
  1044. device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
  1045. &pdata->micd_dbtime);
  1046. device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms",
  1047. &pdata->micd_timeout);
  1048. pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
  1049. "wlf,micd-force-micbias");
  1050. pdata->micd_software_compare = device_property_read_bool(arizona->dev,
  1051. "wlf,micd-software-compare");
  1052. pdata->jd_invert = device_property_read_bool(arizona->dev,
  1053. "wlf,jd-invert");
  1054. device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw);
  1055. pdata->jd_gpio5 = device_property_read_bool(arizona->dev,
  1056. "wlf,use-jd2");
  1057. pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev,
  1058. "wlf,use-jd2-nopull");
  1059. ret = arizona_extcon_get_micd_configs(dev, arizona);
  1060. if (ret < 0)
  1061. dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret);
  1062. return 0;
  1063. }
  1064. static int arizona_extcon_probe(struct platform_device *pdev)
  1065. {
  1066. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  1067. struct arizona_pdata *pdata = &arizona->pdata;
  1068. struct arizona_extcon_info *info;
  1069. unsigned int val;
  1070. unsigned int clamp_mode;
  1071. int jack_irq_fall, jack_irq_rise;
  1072. int ret, mode, i, j;
  1073. if (!arizona->dapm || !arizona->dapm->card)
  1074. return -EPROBE_DEFER;
  1075. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  1076. if (!info)
  1077. return -ENOMEM;
  1078. if (!dev_get_platdata(arizona->dev))
  1079. arizona_extcon_device_get_pdata(&pdev->dev, arizona);
  1080. info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD");
  1081. if (IS_ERR(info->micvdd)) {
  1082. ret = PTR_ERR(info->micvdd);
  1083. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  1084. return ret;
  1085. }
  1086. mutex_init(&info->lock);
  1087. info->arizona = arizona;
  1088. info->dev = &pdev->dev;
  1089. info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
  1090. INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
  1091. INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
  1092. INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
  1093. platform_set_drvdata(pdev, info);
  1094. switch (arizona->type) {
  1095. case WM5102:
  1096. switch (arizona->rev) {
  1097. case 0:
  1098. info->micd_reva = true;
  1099. break;
  1100. default:
  1101. info->micd_clamp = true;
  1102. info->hpdet_ip_version = 1;
  1103. break;
  1104. }
  1105. break;
  1106. case WM5110:
  1107. case WM8280:
  1108. switch (arizona->rev) {
  1109. case 0 ... 2:
  1110. break;
  1111. default:
  1112. info->micd_clamp = true;
  1113. info->hpdet_ip_version = 2;
  1114. break;
  1115. }
  1116. break;
  1117. case WM8998:
  1118. case WM1814:
  1119. info->micd_clamp = true;
  1120. info->hpdet_ip_version = 2;
  1121. break;
  1122. default:
  1123. break;
  1124. }
  1125. info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
  1126. if (IS_ERR(info->edev)) {
  1127. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  1128. return -ENOMEM;
  1129. }
  1130. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  1131. if (ret < 0) {
  1132. dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
  1133. ret);
  1134. return ret;
  1135. }
  1136. info->input = devm_input_allocate_device(&pdev->dev);
  1137. if (!info->input) {
  1138. dev_err(arizona->dev, "Can't allocate input dev\n");
  1139. ret = -ENOMEM;
  1140. goto err_register;
  1141. }
  1142. info->input->name = "Headset";
  1143. info->input->phys = "arizona/extcon";
  1144. if (pdata->num_micd_configs) {
  1145. info->micd_modes = pdata->micd_configs;
  1146. info->micd_num_modes = pdata->num_micd_configs;
  1147. } else {
  1148. info->micd_modes = micd_default_modes;
  1149. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  1150. }
  1151. if (arizona->pdata.gpsw > 0)
  1152. regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1,
  1153. ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw);
  1154. if (arizona->pdata.micd_pol_gpio > 0) {
  1155. if (info->micd_modes[0].gpio)
  1156. mode = GPIOF_OUT_INIT_HIGH;
  1157. else
  1158. mode = GPIOF_OUT_INIT_LOW;
  1159. ret = devm_gpio_request_one(&pdev->dev,
  1160. arizona->pdata.micd_pol_gpio,
  1161. mode,
  1162. "MICD polarity");
  1163. if (ret != 0) {
  1164. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  1165. arizona->pdata.micd_pol_gpio, ret);
  1166. goto err_register;
  1167. }
  1168. } else {
  1169. if (info->micd_modes[0].gpio)
  1170. mode = GPIOD_OUT_HIGH;
  1171. else
  1172. mode = GPIOD_OUT_LOW;
  1173. /* We can't use devm here because we need to do the get
  1174. * against the MFD device, as that is where the of_node
  1175. * will reside, but if we devm against that the GPIO
  1176. * will not be freed if the extcon driver is unloaded.
  1177. */
  1178. info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
  1179. "wlf,micd-pol",
  1180. GPIOD_OUT_LOW);
  1181. if (IS_ERR(info->micd_pol_gpio)) {
  1182. ret = PTR_ERR(info->micd_pol_gpio);
  1183. dev_err(arizona->dev,
  1184. "Failed to get microphone polarity GPIO: %d\n",
  1185. ret);
  1186. goto err_register;
  1187. }
  1188. }
  1189. if (arizona->pdata.hpdet_id_gpio > 0) {
  1190. ret = devm_gpio_request_one(&pdev->dev,
  1191. arizona->pdata.hpdet_id_gpio,
  1192. GPIOF_OUT_INIT_LOW,
  1193. "HPDET");
  1194. if (ret != 0) {
  1195. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  1196. arizona->pdata.hpdet_id_gpio, ret);
  1197. goto err_gpio;
  1198. }
  1199. }
  1200. if (arizona->pdata.micd_bias_start_time)
  1201. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1202. ARIZONA_MICD_BIAS_STARTTIME_MASK,
  1203. arizona->pdata.micd_bias_start_time
  1204. << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
  1205. if (arizona->pdata.micd_rate)
  1206. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1207. ARIZONA_MICD_RATE_MASK,
  1208. arizona->pdata.micd_rate
  1209. << ARIZONA_MICD_RATE_SHIFT);
  1210. switch (arizona->pdata.micd_dbtime) {
  1211. case MICD_DBTIME_FOUR_READINGS:
  1212. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1213. ARIZONA_MICD_DBTIME_MASK,
  1214. ARIZONA_MICD_DBTIME);
  1215. break;
  1216. case MICD_DBTIME_TWO_READINGS:
  1217. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  1218. ARIZONA_MICD_DBTIME_MASK, 0);
  1219. break;
  1220. default:
  1221. break;
  1222. }
  1223. BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) <
  1224. ARIZONA_NUM_MICD_BUTTON_LEVELS);
  1225. if (arizona->pdata.num_micd_ranges) {
  1226. info->micd_ranges = pdata->micd_ranges;
  1227. info->num_micd_ranges = pdata->num_micd_ranges;
  1228. } else {
  1229. info->micd_ranges = micd_default_ranges;
  1230. info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
  1231. }
  1232. if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
  1233. dev_err(arizona->dev, "Too many MICD ranges: %d\n",
  1234. arizona->pdata.num_micd_ranges);
  1235. }
  1236. if (info->num_micd_ranges > 1) {
  1237. for (i = 1; i < info->num_micd_ranges; i++) {
  1238. if (info->micd_ranges[i - 1].max >
  1239. info->micd_ranges[i].max) {
  1240. dev_err(arizona->dev,
  1241. "MICD ranges must be sorted\n");
  1242. ret = -EINVAL;
  1243. goto err_gpio;
  1244. }
  1245. }
  1246. }
  1247. /* Disable all buttons by default */
  1248. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  1249. ARIZONA_MICD_LVL_SEL_MASK, 0x81);
  1250. /* Set up all the buttons the user specified */
  1251. for (i = 0; i < info->num_micd_ranges; i++) {
  1252. for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++)
  1253. if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
  1254. break;
  1255. if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) {
  1256. dev_err(arizona->dev, "Unsupported MICD level %d\n",
  1257. info->micd_ranges[i].max);
  1258. ret = -EINVAL;
  1259. goto err_gpio;
  1260. }
  1261. dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
  1262. arizona_micd_levels[j], i);
  1263. arizona_micd_set_level(arizona, i, j);
  1264. input_set_capability(info->input, EV_KEY,
  1265. info->micd_ranges[i].key);
  1266. /* Enable reporting of that range */
  1267. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  1268. 1 << i, 1 << i);
  1269. }
  1270. /* Set all the remaining keys to a maximum */
  1271. for (; i < ARIZONA_MAX_MICD_RANGE; i++)
  1272. arizona_micd_set_level(arizona, i, 0x3f);
  1273. /*
  1274. * If we have a clamp use it, activating in conjunction with
  1275. * GPIO5 if that is connected for jack detect operation.
  1276. */
  1277. if (info->micd_clamp) {
  1278. if (arizona->pdata.jd_gpio5) {
  1279. /* Put the GPIO into input mode with optional pull */
  1280. val = 0xc101;
  1281. if (arizona->pdata.jd_gpio5_nopull)
  1282. val &= ~ARIZONA_GPN_PU;
  1283. regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
  1284. val);
  1285. if (arizona->pdata.jd_invert)
  1286. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
  1287. else
  1288. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
  1289. } else {
  1290. if (arizona->pdata.jd_invert)
  1291. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
  1292. else
  1293. clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
  1294. }
  1295. regmap_update_bits(arizona->regmap,
  1296. ARIZONA_MICD_CLAMP_CONTROL,
  1297. ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
  1298. regmap_update_bits(arizona->regmap,
  1299. ARIZONA_JACK_DETECT_DEBOUNCE,
  1300. ARIZONA_MICD_CLAMP_DB,
  1301. ARIZONA_MICD_CLAMP_DB);
  1302. }
  1303. arizona_extcon_set_mode(info, 0);
  1304. pm_runtime_enable(&pdev->dev);
  1305. pm_runtime_idle(&pdev->dev);
  1306. pm_runtime_get_sync(&pdev->dev);
  1307. if (info->micd_clamp) {
  1308. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1309. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1310. } else {
  1311. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1312. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1313. }
  1314. ret = arizona_request_irq(arizona, jack_irq_rise,
  1315. "JACKDET rise", arizona_jackdet, info);
  1316. if (ret != 0) {
  1317. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  1318. ret);
  1319. goto err_gpio;
  1320. }
  1321. ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
  1322. if (ret != 0) {
  1323. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  1324. ret);
  1325. goto err_rise;
  1326. }
  1327. ret = arizona_request_irq(arizona, jack_irq_fall,
  1328. "JACKDET fall", arizona_jackdet, info);
  1329. if (ret != 0) {
  1330. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  1331. goto err_rise_wake;
  1332. }
  1333. ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
  1334. if (ret != 0) {
  1335. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  1336. ret);
  1337. goto err_fall;
  1338. }
  1339. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  1340. "MICDET", arizona_micdet, info);
  1341. if (ret != 0) {
  1342. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  1343. goto err_fall_wake;
  1344. }
  1345. ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
  1346. "HPDET", arizona_hpdet_irq, info);
  1347. if (ret != 0) {
  1348. dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
  1349. goto err_micdet;
  1350. }
  1351. arizona_clk32k_enable(arizona);
  1352. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  1353. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  1354. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1355. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  1356. ret = regulator_allow_bypass(info->micvdd, true);
  1357. if (ret != 0)
  1358. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
  1359. ret);
  1360. pm_runtime_put(&pdev->dev);
  1361. ret = input_register_device(info->input);
  1362. if (ret) {
  1363. dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
  1364. goto err_hpdet;
  1365. }
  1366. return 0;
  1367. err_hpdet:
  1368. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1369. err_micdet:
  1370. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1371. err_fall_wake:
  1372. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1373. err_fall:
  1374. arizona_free_irq(arizona, jack_irq_fall, info);
  1375. err_rise_wake:
  1376. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1377. err_rise:
  1378. arizona_free_irq(arizona, jack_irq_rise, info);
  1379. err_gpio:
  1380. gpiod_put(info->micd_pol_gpio);
  1381. err_register:
  1382. pm_runtime_disable(&pdev->dev);
  1383. return ret;
  1384. }
  1385. static int arizona_extcon_remove(struct platform_device *pdev)
  1386. {
  1387. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  1388. struct arizona *arizona = info->arizona;
  1389. int jack_irq_rise, jack_irq_fall;
  1390. gpiod_put(info->micd_pol_gpio);
  1391. pm_runtime_disable(&pdev->dev);
  1392. regmap_update_bits(arizona->regmap,
  1393. ARIZONA_MICD_CLAMP_CONTROL,
  1394. ARIZONA_MICD_CLAMP_MODE_MASK, 0);
  1395. if (info->micd_clamp) {
  1396. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1397. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1398. } else {
  1399. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1400. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1401. }
  1402. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1403. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1404. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1405. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1406. arizona_free_irq(arizona, jack_irq_rise, info);
  1407. arizona_free_irq(arizona, jack_irq_fall, info);
  1408. cancel_delayed_work_sync(&info->hpdet_work);
  1409. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1410. ARIZONA_JD1_ENA, 0);
  1411. arizona_clk32k_disable(arizona);
  1412. return 0;
  1413. }
  1414. static struct platform_driver arizona_extcon_driver = {
  1415. .driver = {
  1416. .name = "arizona-extcon",
  1417. },
  1418. .probe = arizona_extcon_probe,
  1419. .remove = arizona_extcon_remove,
  1420. };
  1421. module_platform_driver(arizona_extcon_driver);
  1422. MODULE_DESCRIPTION("Arizona Extcon driver");
  1423. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1424. MODULE_LICENSE("GPL");
  1425. MODULE_ALIAS("platform:extcon-arizona");