extcon-arizona.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. {
  380. struct arizona *arizona = info->arizona;
  381. int id_gpio = arizona->pdata.hpdet_id_gpio;
  382. /*
  383. * If we're using HPDET for accessory identification we need
  384. * to take multiple measurements, step through them in sequence.
  385. */
  386. if (arizona->pdata.hpdet_acc_id) {
  387. info->hpdet_res[info->num_hpdet_res++] = *reading;
  388. /*
  389. * If the impedence is too high don't measure the
  390. * second ground.
  391. */
  392. if (info->num_hpdet_res == 1 && *reading >= 45) {
  393. dev_dbg(arizona->dev, "Skipping ground flip\n");
  394. info->hpdet_res[info->num_hpdet_res++] = *reading;
  395. }
  396. if (info->num_hpdet_res == 1) {
  397. dev_dbg(arizona->dev, "Flipping ground\n");
  398. regmap_update_bits(arizona->regmap,
  399. ARIZONA_ACCESSORY_DETECT_MODE_1,
  400. ARIZONA_ACCDET_SRC,
  401. ~info->micd_modes[0].src);
  402. regmap_update_bits(arizona->regmap,
  403. ARIZONA_HEADPHONE_DETECT_1,
  404. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  405. return -EAGAIN;
  406. }
  407. /* Only check the mic directly if we didn't already ID it */
  408. if (id_gpio && info->num_hpdet_res == 2 &&
  409. !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
  410. dev_dbg(arizona->dev, "Measuring mic\n");
  411. regmap_update_bits(arizona->regmap,
  412. ARIZONA_ACCESSORY_DETECT_MODE_1,
  413. ARIZONA_ACCDET_MODE_MASK |
  414. ARIZONA_ACCDET_SRC,
  415. ARIZONA_ACCDET_MODE_HPR |
  416. info->micd_modes[0].src);
  417. gpio_set_value_cansleep(id_gpio, 1);
  418. regmap_update_bits(arizona->regmap,
  419. ARIZONA_HEADPHONE_DETECT_1,
  420. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  421. return -EAGAIN;
  422. }
  423. /* OK, got both. Now, compare... */
  424. dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
  425. info->hpdet_res[0], info->hpdet_res[1],
  426. info->hpdet_res[2]);
  427. /* Take the headphone impedance for the main report */
  428. *reading = info->hpdet_res[0];
  429. /* Sometimes we get false readings due to slow insert */
  430. if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
  431. dev_dbg(arizona->dev, "Retrying high impedance\n");
  432. info->num_hpdet_res = 0;
  433. info->hpdet_retried = true;
  434. arizona_start_hpdet_acc_id(info);
  435. pm_runtime_put(info->dev);
  436. return -EAGAIN;
  437. }
  438. /*
  439. * Either the two grounds measure differently or we
  440. * measure the mic as high impedance.
  441. */
  442. if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
  443. (id_gpio && info->hpdet_res[2] > 1257)) {
  444. dev_dbg(arizona->dev, "Detected mic\n");
  445. info->mic = true;
  446. info->detecting = true;
  447. } else {
  448. dev_dbg(arizona->dev, "Detected headphone\n");
  449. }
  450. /* Make sure everything is reset back to the real polarity */
  451. regmap_update_bits(arizona->regmap,
  452. ARIZONA_ACCESSORY_DETECT_MODE_1,
  453. ARIZONA_ACCDET_SRC,
  454. info->micd_modes[0].src);
  455. }
  456. return 0;
  457. }
  458. static irqreturn_t arizona_hpdet_irq(int irq, void *data)
  459. {
  460. struct arizona_extcon_info *info = data;
  461. struct arizona *arizona = info->arizona;
  462. int id_gpio = arizona->pdata.hpdet_id_gpio;
  463. int report = ARIZONA_CABLE_HEADPHONE;
  464. int ret, reading;
  465. mutex_lock(&info->lock);
  466. /* If we got a spurious IRQ for some reason then ignore it */
  467. if (!info->hpdet_active) {
  468. dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
  469. mutex_unlock(&info->lock);
  470. return IRQ_NONE;
  471. }
  472. /* If the cable was removed while measuring ignore the result */
  473. ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
  474. if (ret < 0) {
  475. dev_err(arizona->dev, "Failed to check cable state: %d\n",
  476. ret);
  477. goto out;
  478. } else if (!ret) {
  479. dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
  480. goto done;
  481. }
  482. ret = arizona_hpdet_read(info);
  483. if (ret == -EAGAIN) {
  484. goto out;
  485. } else if (ret < 0) {
  486. goto done;
  487. }
  488. reading = ret;
  489. /* Reset back to starting range */
  490. regmap_update_bits(arizona->regmap,
  491. ARIZONA_HEADPHONE_DETECT_1,
  492. ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
  493. 0);
  494. ret = arizona_hpdet_do_id(info, &reading);
  495. if (ret == -EAGAIN) {
  496. goto out;
  497. } else if (ret < 0) {
  498. goto done;
  499. }
  500. /* Report high impedence cables as line outputs */
  501. if (reading >= 5000)
  502. report = ARIZONA_CABLE_LINEOUT;
  503. else
  504. report = ARIZONA_CABLE_HEADPHONE;
  505. ret = extcon_set_cable_state_(&info->edev, report, true);
  506. if (ret != 0)
  507. dev_err(arizona->dev, "Failed to report HP/line: %d\n",
  508. ret);
  509. arizona_extcon_do_magic(info, 0);
  510. done:
  511. if (id_gpio)
  512. gpio_set_value_cansleep(id_gpio, 0);
  513. /* Revert back to MICDET mode */
  514. regmap_update_bits(arizona->regmap,
  515. ARIZONA_ACCESSORY_DETECT_MODE_1,
  516. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  517. /* If we have a mic then reenable MICDET */
  518. if (info->mic)
  519. arizona_start_mic(info);
  520. if (info->hpdet_active) {
  521. pm_runtime_put_autosuspend(info->dev);
  522. info->hpdet_active = false;
  523. }
  524. info->hpdet_done = true;
  525. out:
  526. mutex_unlock(&info->lock);
  527. return IRQ_HANDLED;
  528. }
  529. static void arizona_identify_headphone(struct arizona_extcon_info *info)
  530. {
  531. struct arizona *arizona = info->arizona;
  532. int ret;
  533. if (info->hpdet_done)
  534. return;
  535. dev_dbg(arizona->dev, "Starting HPDET\n");
  536. /* Make sure we keep the device enabled during the measurement */
  537. pm_runtime_get(info->dev);
  538. info->hpdet_active = true;
  539. if (info->mic)
  540. arizona_stop_mic(info);
  541. arizona_extcon_do_magic(info, 0x4000);
  542. ret = regmap_update_bits(arizona->regmap,
  543. ARIZONA_ACCESSORY_DETECT_MODE_1,
  544. ARIZONA_ACCDET_MODE_MASK,
  545. ARIZONA_ACCDET_MODE_HPL);
  546. if (ret != 0) {
  547. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  548. goto err;
  549. }
  550. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  551. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  552. if (ret != 0) {
  553. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  554. ret);
  555. goto err;
  556. }
  557. return;
  558. err:
  559. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  560. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  561. /* Just report headphone */
  562. ret = extcon_update_state(&info->edev,
  563. 1 << ARIZONA_CABLE_HEADPHONE,
  564. 1 << ARIZONA_CABLE_HEADPHONE);
  565. if (ret != 0)
  566. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  567. if (info->mic)
  568. arizona_start_mic(info);
  569. info->hpdet_active = false;
  570. }
  571. static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
  572. {
  573. struct arizona *arizona = info->arizona;
  574. int ret;
  575. dev_dbg(arizona->dev, "Starting identification via HPDET\n");
  576. /* Make sure we keep the device enabled during the measurement */
  577. pm_runtime_get_sync(info->dev);
  578. info->hpdet_active = true;
  579. arizona_extcon_do_magic(info, 0x4000);
  580. ret = regmap_update_bits(arizona->regmap,
  581. ARIZONA_ACCESSORY_DETECT_MODE_1,
  582. ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
  583. info->micd_modes[0].src |
  584. ARIZONA_ACCDET_MODE_HPL);
  585. if (ret != 0) {
  586. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  587. goto err;
  588. }
  589. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  590. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  591. if (ret != 0) {
  592. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  593. ret);
  594. goto err;
  595. }
  596. return;
  597. err:
  598. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  599. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  600. /* Just report headphone */
  601. ret = extcon_update_state(&info->edev,
  602. 1 << ARIZONA_CABLE_HEADPHONE,
  603. 1 << ARIZONA_CABLE_HEADPHONE);
  604. if (ret != 0)
  605. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  606. info->hpdet_active = false;
  607. }
  608. static irqreturn_t arizona_micdet(int irq, void *data)
  609. {
  610. struct arizona_extcon_info *info = data;
  611. struct arizona *arizona = info->arizona;
  612. unsigned int val = 0, lvl;
  613. int ret, i, key;
  614. mutex_lock(&info->lock);
  615. for (i = 0; i < 10 && !(val & 0x7fc); i++) {
  616. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  617. if (ret != 0) {
  618. dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
  619. mutex_unlock(&info->lock);
  620. return IRQ_NONE;
  621. }
  622. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  623. if (!(val & ARIZONA_MICD_VALID)) {
  624. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  625. mutex_unlock(&info->lock);
  626. return IRQ_NONE;
  627. }
  628. }
  629. if (i == 10 && !(val & 0x7fc)) {
  630. dev_err(arizona->dev, "Failed to get valid MICDET value\n");
  631. mutex_unlock(&info->lock);
  632. return IRQ_NONE;
  633. }
  634. /* Due to jack detect this should never happen */
  635. if (!(val & ARIZONA_MICD_STS)) {
  636. dev_warn(arizona->dev, "Detected open circuit\n");
  637. info->detecting = false;
  638. goto handled;
  639. }
  640. /* If we got a high impedence we should have a headset, report it. */
  641. if (info->detecting && (val & 0x400)) {
  642. arizona_identify_headphone(info);
  643. ret = extcon_update_state(&info->edev,
  644. 1 << ARIZONA_CABLE_MICROPHONE,
  645. 1 << ARIZONA_CABLE_MICROPHONE);
  646. if (ret != 0)
  647. dev_err(arizona->dev, "Headset report failed: %d\n",
  648. ret);
  649. /* Don't need to regulate for button detection */
  650. ret = regulator_allow_bypass(info->micvdd, false);
  651. if (ret != 0) {
  652. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
  653. ret);
  654. }
  655. info->mic = true;
  656. info->detecting = false;
  657. goto handled;
  658. }
  659. /* If we detected a lower impedence during initial startup
  660. * then we probably have the wrong polarity, flip it. Don't
  661. * do this for the lowest impedences to speed up detection of
  662. * plain headphones. If both polarities report a low
  663. * impedence then give up and report headphones.
  664. */
  665. if (info->detecting && (val & 0x3f8)) {
  666. if (info->jack_flips >= info->micd_num_modes * 10) {
  667. dev_dbg(arizona->dev, "Detected HP/line\n");
  668. arizona_identify_headphone(info);
  669. info->detecting = false;
  670. arizona_stop_mic(info);
  671. } else {
  672. info->micd_mode++;
  673. if (info->micd_mode == info->micd_num_modes)
  674. info->micd_mode = 0;
  675. arizona_extcon_set_mode(info, info->micd_mode);
  676. info->jack_flips++;
  677. }
  678. goto handled;
  679. }
  680. /*
  681. * If we're still detecting and we detect a short then we've
  682. * got a headphone. Otherwise it's a button press.
  683. */
  684. if (val & 0x3fc) {
  685. if (info->mic) {
  686. dev_dbg(arizona->dev, "Mic button detected\n");
  687. lvl = val & ARIZONA_MICD_LVL_MASK;
  688. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  689. WARN_ON(!lvl);
  690. WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
  691. if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
  692. key = info->micd_ranges[ffs(lvl) - 1].key;
  693. input_report_key(info->input, key, 1);
  694. input_sync(info->input);
  695. }
  696. } else if (info->detecting) {
  697. dev_dbg(arizona->dev, "Headphone detected\n");
  698. info->detecting = false;
  699. arizona_stop_mic(info);
  700. arizona_identify_headphone(info);
  701. } else {
  702. dev_warn(arizona->dev, "Button with no mic: %x\n",
  703. val);
  704. }
  705. } else {
  706. dev_dbg(arizona->dev, "Mic button released\n");
  707. for (i = 0; i < info->num_micd_ranges; i++)
  708. input_report_key(info->input,
  709. info->micd_ranges[i].key, 0);
  710. input_sync(info->input);
  711. arizona_extcon_pulse_micbias(info);
  712. }
  713. handled:
  714. pm_runtime_mark_last_busy(info->dev);
  715. mutex_unlock(&info->lock);
  716. return IRQ_HANDLED;
  717. }
  718. static void arizona_hpdet_work(struct work_struct *work)
  719. {
  720. struct arizona_extcon_info *info = container_of(work,
  721. struct arizona_extcon_info,
  722. hpdet_work.work);
  723. mutex_lock(&info->lock);
  724. arizona_start_hpdet_acc_id(info);
  725. mutex_unlock(&info->lock);
  726. }
  727. static irqreturn_t arizona_jackdet(int irq, void *data)
  728. {
  729. struct arizona_extcon_info *info = data;
  730. struct arizona *arizona = info->arizona;
  731. unsigned int val, present, mask;
  732. bool cancelled;
  733. int ret, i;
  734. cancelled = cancel_delayed_work_sync(&info->hpdet_work);
  735. pm_runtime_get_sync(info->dev);
  736. mutex_lock(&info->lock);
  737. if (arizona->pdata.jd_gpio5) {
  738. mask = ARIZONA_MICD_CLAMP_STS;
  739. present = 0;
  740. } else {
  741. mask = ARIZONA_JD1_STS;
  742. present = ARIZONA_JD1_STS;
  743. }
  744. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  745. if (ret != 0) {
  746. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  747. ret);
  748. mutex_unlock(&info->lock);
  749. pm_runtime_put_autosuspend(info->dev);
  750. return IRQ_NONE;
  751. }
  752. val &= mask;
  753. if (val == info->last_jackdet) {
  754. dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
  755. if (cancelled)
  756. schedule_delayed_work(&info->hpdet_work,
  757. msecs_to_jiffies(HPDET_DEBOUNCE));
  758. goto out;
  759. }
  760. info->last_jackdet = val;
  761. if (info->last_jackdet == present) {
  762. dev_dbg(arizona->dev, "Detected jack\n");
  763. ret = extcon_set_cable_state_(&info->edev,
  764. ARIZONA_CABLE_MECHANICAL, true);
  765. if (ret != 0)
  766. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  767. ret);
  768. if (!arizona->pdata.hpdet_acc_id) {
  769. info->detecting = true;
  770. info->mic = false;
  771. info->jack_flips = 0;
  772. arizona_start_mic(info);
  773. } else {
  774. schedule_delayed_work(&info->hpdet_work,
  775. msecs_to_jiffies(HPDET_DEBOUNCE));
  776. }
  777. regmap_update_bits(arizona->regmap,
  778. ARIZONA_JACK_DETECT_DEBOUNCE,
  779. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
  780. } else {
  781. dev_dbg(arizona->dev, "Detected jack removal\n");
  782. arizona_stop_mic(info);
  783. info->num_hpdet_res = 0;
  784. for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
  785. info->hpdet_res[i] = 0;
  786. info->mic = false;
  787. info->hpdet_done = false;
  788. info->hpdet_retried = false;
  789. for (i = 0; i < info->num_micd_ranges; i++)
  790. input_report_key(info->input,
  791. info->micd_ranges[i].key, 0);
  792. input_sync(info->input);
  793. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  794. if (ret != 0)
  795. dev_err(arizona->dev, "Removal report failed: %d\n",
  796. ret);
  797. regmap_update_bits(arizona->regmap,
  798. ARIZONA_JACK_DETECT_DEBOUNCE,
  799. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
  800. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
  801. }
  802. /* Clear trig_sts to make sure DCVDD is not forced up */
  803. regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
  804. ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
  805. ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
  806. ARIZONA_JD1_FALL_TRIG_STS |
  807. ARIZONA_JD1_RISE_TRIG_STS);
  808. out:
  809. mutex_unlock(&info->lock);
  810. pm_runtime_mark_last_busy(info->dev);
  811. pm_runtime_put_autosuspend(info->dev);
  812. return IRQ_HANDLED;
  813. }
  814. /* Map a level onto a slot in the register bank */
  815. static void arizona_micd_set_level(struct arizona *arizona, int index,
  816. unsigned int level)
  817. {
  818. int reg;
  819. unsigned int mask;
  820. reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
  821. if (!(index % 2)) {
  822. mask = 0x3f00;
  823. level <<= 8;
  824. } else {
  825. mask = 0x3f;
  826. }
  827. /* Program the level itself */
  828. regmap_update_bits(arizona->regmap, reg, mask, level);
  829. }
  830. static int arizona_extcon_probe(struct platform_device *pdev)
  831. {
  832. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  833. struct arizona_pdata *pdata;
  834. struct arizona_extcon_info *info;
  835. unsigned int val;
  836. int jack_irq_fall, jack_irq_rise;
  837. int ret, mode, i, j;
  838. if (!arizona->dapm || !arizona->dapm->card)
  839. return -EPROBE_DEFER;
  840. pdata = dev_get_platdata(arizona->dev);
  841. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  842. if (!info) {
  843. dev_err(&pdev->dev, "Failed to allocate memory\n");
  844. ret = -ENOMEM;
  845. goto err;
  846. }
  847. info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
  848. if (IS_ERR(info->micvdd)) {
  849. ret = PTR_ERR(info->micvdd);
  850. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  851. goto err;
  852. }
  853. mutex_init(&info->lock);
  854. info->arizona = arizona;
  855. info->dev = &pdev->dev;
  856. info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
  857. INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
  858. platform_set_drvdata(pdev, info);
  859. switch (arizona->type) {
  860. case WM5102:
  861. switch (arizona->rev) {
  862. case 0:
  863. info->micd_reva = true;
  864. break;
  865. default:
  866. info->micd_clamp = true;
  867. info->hpdet_ip = 1;
  868. break;
  869. }
  870. break;
  871. default:
  872. break;
  873. }
  874. info->edev.name = "Headset Jack";
  875. info->edev.supported_cable = arizona_cable;
  876. ret = extcon_dev_register(&info->edev, arizona->dev);
  877. if (ret < 0) {
  878. dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
  879. ret);
  880. goto err;
  881. }
  882. info->input = devm_input_allocate_device(&pdev->dev);
  883. if (!info->input) {
  884. dev_err(arizona->dev, "Can't allocate input dev\n");
  885. ret = -ENOMEM;
  886. goto err_register;
  887. }
  888. info->input->name = "Headset";
  889. info->input->phys = "arizona/extcon";
  890. info->input->dev.parent = &pdev->dev;
  891. if (pdata->num_micd_configs) {
  892. info->micd_modes = pdata->micd_configs;
  893. info->micd_num_modes = pdata->num_micd_configs;
  894. } else {
  895. info->micd_modes = micd_default_modes;
  896. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  897. }
  898. if (arizona->pdata.micd_pol_gpio > 0) {
  899. if (info->micd_modes[0].gpio)
  900. mode = GPIOF_OUT_INIT_HIGH;
  901. else
  902. mode = GPIOF_OUT_INIT_LOW;
  903. ret = devm_gpio_request_one(&pdev->dev,
  904. arizona->pdata.micd_pol_gpio,
  905. mode,
  906. "MICD polarity");
  907. if (ret != 0) {
  908. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  909. arizona->pdata.micd_pol_gpio, ret);
  910. goto err_register;
  911. }
  912. }
  913. if (arizona->pdata.hpdet_id_gpio > 0) {
  914. ret = devm_gpio_request_one(&pdev->dev,
  915. arizona->pdata.hpdet_id_gpio,
  916. GPIOF_OUT_INIT_LOW,
  917. "HPDET");
  918. if (ret != 0) {
  919. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  920. arizona->pdata.hpdet_id_gpio, ret);
  921. goto err_register;
  922. }
  923. }
  924. if (arizona->pdata.micd_bias_start_time)
  925. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  926. ARIZONA_MICD_BIAS_STARTTIME_MASK,
  927. arizona->pdata.micd_bias_start_time
  928. << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
  929. if (arizona->pdata.micd_rate)
  930. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  931. ARIZONA_MICD_RATE_MASK,
  932. arizona->pdata.micd_rate
  933. << ARIZONA_MICD_RATE_SHIFT);
  934. if (arizona->pdata.micd_dbtime)
  935. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  936. ARIZONA_MICD_DBTIME_MASK,
  937. arizona->pdata.micd_dbtime
  938. << ARIZONA_MICD_DBTIME_SHIFT);
  939. BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
  940. if (arizona->pdata.num_micd_ranges) {
  941. info->micd_ranges = pdata->micd_ranges;
  942. info->num_micd_ranges = pdata->num_micd_ranges;
  943. } else {
  944. info->micd_ranges = micd_default_ranges;
  945. info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
  946. }
  947. if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
  948. dev_err(arizona->dev, "Too many MICD ranges: %d\n",
  949. arizona->pdata.num_micd_ranges);
  950. }
  951. if (info->num_micd_ranges > 1) {
  952. for (i = 1; i < info->num_micd_ranges; i++) {
  953. if (info->micd_ranges[i - 1].max >
  954. info->micd_ranges[i].max) {
  955. dev_err(arizona->dev,
  956. "MICD ranges must be sorted\n");
  957. ret = -EINVAL;
  958. goto err_input;
  959. }
  960. }
  961. }
  962. /* Disable all buttons by default */
  963. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  964. ARIZONA_MICD_LVL_SEL_MASK, 0x81);
  965. /* Set up all the buttons the user specified */
  966. for (i = 0; i < info->num_micd_ranges; i++) {
  967. for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
  968. if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
  969. break;
  970. if (j == ARRAY_SIZE(arizona_micd_levels)) {
  971. dev_err(arizona->dev, "Unsupported MICD level %d\n",
  972. info->micd_ranges[i].max);
  973. ret = -EINVAL;
  974. goto err_input;
  975. }
  976. dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
  977. arizona_micd_levels[j], i);
  978. arizona_micd_set_level(arizona, i, j);
  979. input_set_capability(info->input, EV_KEY,
  980. info->micd_ranges[i].key);
  981. /* Enable reporting of that range */
  982. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
  983. 1 << i, 1 << i);
  984. }
  985. /* Set all the remaining keys to a maximum */
  986. for (; i < ARIZONA_MAX_MICD_RANGE; i++)
  987. arizona_micd_set_level(arizona, i, 0x3f);
  988. /*
  989. * If we have a clamp use it, activating in conjunction with
  990. * GPIO5 if that is connected for jack detect operation.
  991. */
  992. if (info->micd_clamp) {
  993. if (arizona->pdata.jd_gpio5) {
  994. /* Put the GPIO into input mode with optional pull */
  995. val = 0xc101;
  996. if (arizona->pdata.jd_gpio5_nopull)
  997. val &= ~ARIZONA_GPN_PU;
  998. regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
  999. val);
  1000. regmap_update_bits(arizona->regmap,
  1001. ARIZONA_MICD_CLAMP_CONTROL,
  1002. ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
  1003. } else {
  1004. regmap_update_bits(arizona->regmap,
  1005. ARIZONA_MICD_CLAMP_CONTROL,
  1006. ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
  1007. }
  1008. regmap_update_bits(arizona->regmap,
  1009. ARIZONA_JACK_DETECT_DEBOUNCE,
  1010. ARIZONA_MICD_CLAMP_DB,
  1011. ARIZONA_MICD_CLAMP_DB);
  1012. }
  1013. arizona_extcon_set_mode(info, 0);
  1014. pm_runtime_enable(&pdev->dev);
  1015. pm_runtime_idle(&pdev->dev);
  1016. pm_runtime_get_sync(&pdev->dev);
  1017. if (arizona->pdata.jd_gpio5) {
  1018. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1019. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1020. } else {
  1021. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1022. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1023. }
  1024. ret = arizona_request_irq(arizona, jack_irq_rise,
  1025. "JACKDET rise", arizona_jackdet, info);
  1026. if (ret != 0) {
  1027. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  1028. ret);
  1029. goto err_input;
  1030. }
  1031. ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
  1032. if (ret != 0) {
  1033. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  1034. ret);
  1035. goto err_rise;
  1036. }
  1037. ret = arizona_request_irq(arizona, jack_irq_fall,
  1038. "JACKDET fall", arizona_jackdet, info);
  1039. if (ret != 0) {
  1040. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  1041. goto err_rise_wake;
  1042. }
  1043. ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
  1044. if (ret != 0) {
  1045. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  1046. ret);
  1047. goto err_fall;
  1048. }
  1049. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  1050. "MICDET", arizona_micdet, info);
  1051. if (ret != 0) {
  1052. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  1053. goto err_fall_wake;
  1054. }
  1055. ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
  1056. "HPDET", arizona_hpdet_irq, info);
  1057. if (ret != 0) {
  1058. dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
  1059. goto err_micdet;
  1060. }
  1061. arizona_clk32k_enable(arizona);
  1062. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  1063. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  1064. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1065. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  1066. ret = regulator_allow_bypass(info->micvdd, true);
  1067. if (ret != 0)
  1068. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
  1069. ret);
  1070. pm_runtime_put(&pdev->dev);
  1071. ret = input_register_device(info->input);
  1072. if (ret) {
  1073. dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
  1074. goto err_hpdet;
  1075. }
  1076. return 0;
  1077. err_hpdet:
  1078. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1079. err_micdet:
  1080. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1081. err_fall_wake:
  1082. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1083. err_fall:
  1084. arizona_free_irq(arizona, jack_irq_fall, info);
  1085. err_rise_wake:
  1086. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1087. err_rise:
  1088. arizona_free_irq(arizona, jack_irq_rise, info);
  1089. err_input:
  1090. err_register:
  1091. pm_runtime_disable(&pdev->dev);
  1092. extcon_dev_unregister(&info->edev);
  1093. err:
  1094. return ret;
  1095. }
  1096. static int arizona_extcon_remove(struct platform_device *pdev)
  1097. {
  1098. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  1099. struct arizona *arizona = info->arizona;
  1100. int jack_irq_rise, jack_irq_fall;
  1101. pm_runtime_disable(&pdev->dev);
  1102. regmap_update_bits(arizona->regmap,
  1103. ARIZONA_MICD_CLAMP_CONTROL,
  1104. ARIZONA_MICD_CLAMP_MODE_MASK, 0);
  1105. if (arizona->pdata.jd_gpio5) {
  1106. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  1107. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  1108. } else {
  1109. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  1110. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  1111. }
  1112. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  1113. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  1114. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  1115. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  1116. arizona_free_irq(arizona, jack_irq_rise, info);
  1117. arizona_free_irq(arizona, jack_irq_fall, info);
  1118. cancel_delayed_work_sync(&info->hpdet_work);
  1119. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  1120. ARIZONA_JD1_ENA, 0);
  1121. arizona_clk32k_disable(arizona);
  1122. extcon_dev_unregister(&info->edev);
  1123. return 0;
  1124. }
  1125. static struct platform_driver arizona_extcon_driver = {
  1126. .driver = {
  1127. .name = "arizona-extcon",
  1128. .owner = THIS_MODULE,
  1129. },
  1130. .probe = arizona_extcon_probe,
  1131. .remove = arizona_extcon_remove,
  1132. };
  1133. module_platform_driver(arizona_extcon_driver);
  1134. MODULE_DESCRIPTION("Arizona Extcon driver");
  1135. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1136. MODULE_LICENSE("GPL");
  1137. MODULE_ALIAS("platform:extcon-arizona");