extcon-arizona.c 34 KB

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