extcon-arizona.c 38 KB

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