extcon-arizona.c 36 KB

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