extcon-arizona.c 34 KB

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