extcon-arizona.c 36 KB

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