extcon-arizona.c 35 KB

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