extcon-arizona.c 44 KB

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