extcon-arizona.c 36 KB

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