extcon-arizona.c 37 KB

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