extcon-arizona.c 38 KB

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