extcon-arizona.c 38 KB

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