extcon-arizona.c 30 KB

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