extcon-arizona.c 36 KB

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