extcon-arizona.c 44 KB

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