extcon-arizona.c 34 KB

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