extcon-arizona.c 40 KB

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