extcon-arizona.c 36 KB

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