extcon-arizona.c 45 KB

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