extcon-arizona.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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] > 1257)) {
  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_do_magic(info, 0x4000);
  566. ret = regmap_update_bits(arizona->regmap,
  567. ARIZONA_ACCESSORY_DETECT_MODE_1,
  568. ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
  569. info->micd_modes[0].src |
  570. ARIZONA_ACCDET_MODE_HPL);
  571. if (ret != 0) {
  572. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  573. goto err;
  574. }
  575. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  576. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  577. if (ret != 0) {
  578. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  579. ret);
  580. goto err;
  581. }
  582. return;
  583. err:
  584. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  585. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  586. /* Just report headphone */
  587. ret = extcon_update_state(&info->edev,
  588. 1 << ARIZONA_CABLE_HEADPHONE,
  589. 1 << ARIZONA_CABLE_HEADPHONE);
  590. if (ret != 0)
  591. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  592. info->hpdet_active = false;
  593. }
  594. static irqreturn_t arizona_micdet(int irq, void *data)
  595. {
  596. struct arizona_extcon_info *info = data;
  597. struct arizona *arizona = info->arizona;
  598. unsigned int val, lvl;
  599. int ret, i, key;
  600. mutex_lock(&info->lock);
  601. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  602. if (ret != 0) {
  603. dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
  604. mutex_unlock(&info->lock);
  605. return IRQ_NONE;
  606. }
  607. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  608. if (!(val & ARIZONA_MICD_VALID)) {
  609. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  610. mutex_unlock(&info->lock);
  611. return IRQ_NONE;
  612. }
  613. /* Due to jack detect this should never happen */
  614. if (!(val & ARIZONA_MICD_STS)) {
  615. dev_warn(arizona->dev, "Detected open circuit\n");
  616. info->detecting = false;
  617. goto handled;
  618. }
  619. /* If we got a high impedence we should have a headset, report it. */
  620. if (info->detecting && (val & 0x400)) {
  621. arizona_identify_headphone(info);
  622. ret = extcon_update_state(&info->edev,
  623. 1 << ARIZONA_CABLE_MICROPHONE,
  624. 1 << ARIZONA_CABLE_MICROPHONE);
  625. if (ret != 0)
  626. dev_err(arizona->dev, "Headset report failed: %d\n",
  627. ret);
  628. /* Don't need to regulate for button detection */
  629. ret = regulator_allow_bypass(info->micvdd, false);
  630. if (ret != 0) {
  631. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
  632. ret);
  633. }
  634. info->mic = true;
  635. info->detecting = false;
  636. goto handled;
  637. }
  638. /* If we detected a lower impedence during initial startup
  639. * then we probably have the wrong polarity, flip it. Don't
  640. * do this for the lowest impedences to speed up detection of
  641. * plain headphones. If both polarities report a low
  642. * impedence then give up and report headphones.
  643. */
  644. if (info->detecting && (val & 0x3f8)) {
  645. if (info->jack_flips >= info->micd_num_modes * 10) {
  646. dev_dbg(arizona->dev, "Detected HP/line\n");
  647. arizona_identify_headphone(info);
  648. info->detecting = false;
  649. arizona_stop_mic(info);
  650. } else {
  651. info->micd_mode++;
  652. if (info->micd_mode == info->micd_num_modes)
  653. info->micd_mode = 0;
  654. arizona_extcon_set_mode(info, info->micd_mode);
  655. info->jack_flips++;
  656. }
  657. goto handled;
  658. }
  659. /*
  660. * If we're still detecting and we detect a short then we've
  661. * got a headphone. Otherwise it's a button press.
  662. */
  663. if (val & 0x3fc) {
  664. if (info->mic) {
  665. dev_dbg(arizona->dev, "Mic button detected\n");
  666. lvl = val & ARIZONA_MICD_LVL_MASK;
  667. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  668. WARN_ON(!lvl);
  669. WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
  670. if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
  671. key = info->micd_ranges[ffs(lvl) - 1].key;
  672. input_report_key(info->input, key, 1);
  673. input_sync(info->input);
  674. }
  675. } else if (info->detecting) {
  676. dev_dbg(arizona->dev, "Headphone detected\n");
  677. info->detecting = false;
  678. arizona_stop_mic(info);
  679. arizona_identify_headphone(info);
  680. } else {
  681. dev_warn(arizona->dev, "Button with no mic: %x\n",
  682. val);
  683. }
  684. } else {
  685. dev_dbg(arizona->dev, "Mic button released\n");
  686. for (i = 0; i < info->num_micd_ranges; i++)
  687. input_report_key(info->input,
  688. info->micd_ranges[i].key, 0);
  689. input_sync(info->input);
  690. arizona_extcon_pulse_micbias(info);
  691. }
  692. handled:
  693. pm_runtime_mark_last_busy(info->dev);
  694. mutex_unlock(&info->lock);
  695. return IRQ_HANDLED;
  696. }
  697. static void arizona_hpdet_work(struct work_struct *work)
  698. {
  699. struct arizona_extcon_info *info = container_of(work,
  700. struct arizona_extcon_info,
  701. hpdet_work.work);
  702. mutex_lock(&info->lock);
  703. arizona_start_hpdet_acc_id(info);
  704. mutex_unlock(&info->lock);
  705. }
  706. static irqreturn_t arizona_jackdet(int irq, void *data)
  707. {
  708. struct arizona_extcon_info *info = data;
  709. struct arizona *arizona = info->arizona;
  710. unsigned int val, present, mask;
  711. int ret, i;
  712. pm_runtime_get_sync(info->dev);
  713. cancel_delayed_work_sync(&info->hpdet_work);
  714. mutex_lock(&info->lock);
  715. if (arizona->pdata.jd_gpio5) {
  716. mask = ARIZONA_MICD_CLAMP_STS;
  717. present = 0;
  718. } else {
  719. mask = ARIZONA_JD1_STS;
  720. present = ARIZONA_JD1_STS;
  721. }
  722. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  723. if (ret != 0) {
  724. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  725. ret);
  726. mutex_unlock(&info->lock);
  727. pm_runtime_put_autosuspend(info->dev);
  728. return IRQ_NONE;
  729. }
  730. if ((val & mask) == present) {
  731. dev_dbg(arizona->dev, "Detected jack\n");
  732. ret = extcon_set_cable_state_(&info->edev,
  733. ARIZONA_CABLE_MECHANICAL, true);
  734. if (ret != 0)
  735. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  736. ret);
  737. if (!arizona->pdata.hpdet_acc_id) {
  738. info->detecting = true;
  739. info->mic = false;
  740. info->jack_flips = 0;
  741. arizona_start_mic(info);
  742. } else {
  743. schedule_delayed_work(&info->hpdet_work,
  744. msecs_to_jiffies(250));
  745. }
  746. regmap_update_bits(arizona->regmap,
  747. ARIZONA_JACK_DETECT_DEBOUNCE,
  748. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
  749. } else {
  750. dev_dbg(arizona->dev, "Detected jack removal\n");
  751. arizona_stop_mic(info);
  752. info->num_hpdet_res = 0;
  753. for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
  754. info->hpdet_res[i] = 0;
  755. info->mic = false;
  756. info->hpdet_done = false;
  757. for (i = 0; i < info->num_micd_ranges; i++)
  758. input_report_key(info->input,
  759. info->micd_ranges[i].key, 0);
  760. input_sync(info->input);
  761. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  762. if (ret != 0)
  763. dev_err(arizona->dev, "Removal report failed: %d\n",
  764. ret);
  765. regmap_update_bits(arizona->regmap,
  766. ARIZONA_JACK_DETECT_DEBOUNCE,
  767. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
  768. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
  769. }
  770. /* Clear trig_sts to make sure DCVDD is not forced up */
  771. regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
  772. ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
  773. ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
  774. ARIZONA_JD1_FALL_TRIG_STS |
  775. ARIZONA_JD1_RISE_TRIG_STS);
  776. mutex_unlock(&info->lock);
  777. pm_runtime_mark_last_busy(info->dev);
  778. pm_runtime_put_autosuspend(info->dev);
  779. return IRQ_HANDLED;
  780. }
  781. /* Map a level onto a slot in the register bank */
  782. static void arizona_micd_set_level(struct arizona *arizona, int index,
  783. unsigned int level)
  784. {
  785. int reg;
  786. unsigned int mask;
  787. reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
  788. if (!(index % 2)) {
  789. mask = 0x3f00;
  790. level <<= 8;
  791. } else {
  792. mask = 0x3f;
  793. }
  794. /* Program the level itself */
  795. regmap_update_bits(arizona->regmap, reg, mask, level);
  796. }
  797. static int arizona_extcon_probe(struct platform_device *pdev)
  798. {
  799. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  800. struct arizona_pdata *pdata;
  801. struct arizona_extcon_info *info;
  802. unsigned int val;
  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 with optional pull */
  961. val = 0xc101;
  962. if (arizona->pdata.jd_gpio5_nopull)
  963. val &= ~ARIZONA_GPN_PU;
  964. regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
  965. val);
  966. regmap_update_bits(arizona->regmap,
  967. ARIZONA_MICD_CLAMP_CONTROL,
  968. ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
  969. } else {
  970. regmap_update_bits(arizona->regmap,
  971. ARIZONA_MICD_CLAMP_CONTROL,
  972. ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
  973. }
  974. regmap_update_bits(arizona->regmap,
  975. ARIZONA_JACK_DETECT_DEBOUNCE,
  976. ARIZONA_MICD_CLAMP_DB,
  977. ARIZONA_MICD_CLAMP_DB);
  978. }
  979. arizona_extcon_set_mode(info, 0);
  980. pm_runtime_enable(&pdev->dev);
  981. pm_runtime_idle(&pdev->dev);
  982. pm_runtime_get_sync(&pdev->dev);
  983. if (arizona->pdata.jd_gpio5) {
  984. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  985. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  986. } else {
  987. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  988. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  989. }
  990. ret = arizona_request_irq(arizona, jack_irq_rise,
  991. "JACKDET rise", arizona_jackdet, info);
  992. if (ret != 0) {
  993. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  994. ret);
  995. goto err_input;
  996. }
  997. ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
  998. if (ret != 0) {
  999. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  1000. ret);
  1001. goto err_rise;
  1002. }
  1003. ret = arizona_request_irq(arizona, jack_irq_fall,
  1004. "JACKDET fall", arizona_jackdet, info);
  1005. if (ret != 0) {
  1006. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  1007. goto err_rise_wake;
  1008. }
  1009. ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
  1010. if (ret != 0) {
  1011. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  1012. ret);
  1013. goto err_fall;
  1014. }
  1015. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  1016. "MICDET", arizona_micdet, info);
  1017. if (ret != 0) {
  1018. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  1019. goto err_fall_wake;
  1020. }
  1021. ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
  1022. "HPDET", arizona_hpdet_irq, info);
  1023. if (ret != 0) {
  1024. dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
  1025. goto err_micdet;
  1026. }
  1027. arizona_clk32k_enable(arizona);
  1028. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  1029. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  1030. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1031. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  1032. ret = regulator_allow_bypass(info->micvdd, true);
  1033. if (ret != 0)
  1034. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
  1035. ret);
  1036. pm_runtime_put(&pdev->dev);
  1037. ret = input_register_device(info->input);
  1038. if (ret) {
  1039. dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
  1040. goto err_hpdet;
  1041. }
  1042. return 0;
  1043. err_hpdet:
  1044. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1045. err_micdet:
  1046. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1047. err_fall_wake:
  1048. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1049. err_fall:
  1050. arizona_free_irq(arizona, jack_irq_fall, info);
  1051. err_rise_wake:
  1052. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1053. err_rise:
  1054. arizona_free_irq(arizona, jack_irq_rise, info);
  1055. err_input:
  1056. err_register:
  1057. pm_runtime_disable(&pdev->dev);
  1058. extcon_dev_unregister(&info->edev);
  1059. err:
  1060. return ret;
  1061. }
  1062. static int arizona_extcon_remove(struct platform_device *pdev)
  1063. {
  1064. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  1065. struct arizona *arizona = info->arizona;
  1066. int jack_irq_rise, jack_irq_fall;
  1067. pm_runtime_disable(&pdev->dev);
  1068. regmap_update_bits(arizona->regmap,
  1069. ARIZONA_MICD_CLAMP_CONTROL,
  1070. ARIZONA_MICD_CLAMP_MODE_MASK, 0);
  1071. if (arizona->pdata.jd_gpio5) {
  1072. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1073. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1074. } else {
  1075. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1076. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1077. }
  1078. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1079. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1080. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1081. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1082. arizona_free_irq(arizona, jack_irq_rise, info);
  1083. arizona_free_irq(arizona, jack_irq_fall, info);
  1084. cancel_delayed_work_sync(&info->hpdet_work);
  1085. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1086. ARIZONA_JD1_ENA, 0);
  1087. arizona_clk32k_disable(arizona);
  1088. extcon_dev_unregister(&info->edev);
  1089. return 0;
  1090. }
  1091. static struct platform_driver arizona_extcon_driver = {
  1092. .driver = {
  1093. .name = "arizona-extcon",
  1094. .owner = THIS_MODULE,
  1095. },
  1096. .probe = arizona_extcon_probe,
  1097. .remove = arizona_extcon_remove,
  1098. };
  1099. module_platform_driver(arizona_extcon_driver);
  1100. MODULE_DESCRIPTION("Arizona Extcon driver");
  1101. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1102. MODULE_LICENSE("GPL");
  1103. MODULE_ALIAS("platform:extcon-arizona");