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 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
  70. { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 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. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  151. ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
  152. info->micd_mode = mode;
  153. dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
  154. }
  155. static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
  156. {
  157. switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
  158. case 1:
  159. return "MICBIAS1";
  160. case 2:
  161. return "MICBIAS2";
  162. case 3:
  163. return "MICBIAS3";
  164. default:
  165. return "MICVDD";
  166. }
  167. }
  168. static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
  169. {
  170. struct arizona *arizona = info->arizona;
  171. const char *widget = arizona_extcon_get_micbias(info);
  172. struct snd_soc_dapm_context *dapm = arizona->dapm;
  173. int ret;
  174. mutex_lock(&dapm->card->dapm_mutex);
  175. ret = snd_soc_dapm_force_enable_pin(dapm, widget);
  176. if (ret != 0)
  177. dev_warn(arizona->dev, "Failed to enable %s: %d\n",
  178. widget, ret);
  179. mutex_unlock(&dapm->card->dapm_mutex);
  180. snd_soc_dapm_sync(dapm);
  181. if (!arizona->pdata.micd_force_micbias) {
  182. mutex_lock(&dapm->card->dapm_mutex);
  183. ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
  184. if (ret != 0)
  185. dev_warn(arizona->dev, "Failed to disable %s: %d\n",
  186. widget, ret);
  187. mutex_unlock(&dapm->card->dapm_mutex);
  188. snd_soc_dapm_sync(dapm);
  189. }
  190. }
  191. static void arizona_start_mic(struct arizona_extcon_info *info)
  192. {
  193. struct arizona *arizona = info->arizona;
  194. bool change;
  195. int ret;
  196. /* Microphone detection can't use idle mode */
  197. pm_runtime_get(info->dev);
  198. if (info->detecting) {
  199. ret = regulator_allow_bypass(info->micvdd, false);
  200. if (ret != 0) {
  201. dev_err(arizona->dev,
  202. "Failed to regulate MICVDD: %d\n",
  203. ret);
  204. }
  205. }
  206. ret = regulator_enable(info->micvdd);
  207. if (ret != 0) {
  208. dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
  209. ret);
  210. }
  211. if (info->micd_reva) {
  212. regmap_write(arizona->regmap, 0x80, 0x3);
  213. regmap_write(arizona->regmap, 0x294, 0);
  214. regmap_write(arizona->regmap, 0x80, 0x0);
  215. }
  216. regmap_update_bits(arizona->regmap,
  217. ARIZONA_ACCESSORY_DETECT_MODE_1,
  218. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  219. arizona_extcon_pulse_micbias(info);
  220. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  221. ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
  222. &change);
  223. if (!change) {
  224. regulator_disable(info->micvdd);
  225. pm_runtime_put_autosuspend(info->dev);
  226. }
  227. }
  228. static void arizona_stop_mic(struct arizona_extcon_info *info)
  229. {
  230. struct arizona *arizona = info->arizona;
  231. const char *widget = arizona_extcon_get_micbias(info);
  232. struct snd_soc_dapm_context *dapm = arizona->dapm;
  233. bool change;
  234. int ret;
  235. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  236. ARIZONA_MICD_ENA, 0,
  237. &change);
  238. mutex_lock(&dapm->card->dapm_mutex);
  239. ret = snd_soc_dapm_disable_pin(dapm, widget);
  240. if (ret != 0)
  241. dev_warn(arizona->dev,
  242. "Failed to disable %s: %d\n",
  243. widget, ret);
  244. mutex_unlock(&dapm->card->dapm_mutex);
  245. snd_soc_dapm_sync(dapm);
  246. if (info->micd_reva) {
  247. regmap_write(arizona->regmap, 0x80, 0x3);
  248. regmap_write(arizona->regmap, 0x294, 2);
  249. regmap_write(arizona->regmap, 0x80, 0x0);
  250. }
  251. ret = regulator_allow_bypass(info->micvdd, true);
  252. if (ret != 0) {
  253. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
  254. ret);
  255. }
  256. if (change) {
  257. regulator_disable(info->micvdd);
  258. pm_runtime_mark_last_busy(info->dev);
  259. pm_runtime_put_autosuspend(info->dev);
  260. }
  261. }
  262. static struct {
  263. unsigned int factor_a;
  264. unsigned int factor_b;
  265. } arizona_hpdet_b_ranges[] = {
  266. { 5528, 362464 },
  267. { 11084, 6186851 },
  268. { 11065, 65460395 },
  269. };
  270. static struct {
  271. int min;
  272. int max;
  273. } arizona_hpdet_c_ranges[] = {
  274. { 0, 30 },
  275. { 8, 100 },
  276. { 100, 1000 },
  277. { 1000, 10000 },
  278. };
  279. static int arizona_hpdet_read(struct arizona_extcon_info *info)
  280. {
  281. struct arizona *arizona = info->arizona;
  282. unsigned int val, range;
  283. int ret;
  284. ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
  285. if (ret != 0) {
  286. dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
  287. ret);
  288. return ret;
  289. }
  290. switch (info->hpdet_ip) {
  291. case 0:
  292. if (!(val & ARIZONA_HP_DONE)) {
  293. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  294. val);
  295. return -EAGAIN;
  296. }
  297. val &= ARIZONA_HP_LVL_MASK;
  298. break;
  299. case 1:
  300. if (!(val & ARIZONA_HP_DONE_B)) {
  301. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  302. val);
  303. return -EAGAIN;
  304. }
  305. ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
  306. if (ret != 0) {
  307. dev_err(arizona->dev, "Failed to read HP value: %d\n",
  308. ret);
  309. return -EAGAIN;
  310. }
  311. regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  312. &range);
  313. range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
  314. >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
  315. if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
  316. (val < 100 || val > 0x3fb)) {
  317. range++;
  318. dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
  319. range);
  320. regmap_update_bits(arizona->regmap,
  321. ARIZONA_HEADPHONE_DETECT_1,
  322. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  323. range <<
  324. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  325. return -EAGAIN;
  326. }
  327. /* If we go out of range report top of range */
  328. if (val < 100 || val > 0x3fb) {
  329. dev_dbg(arizona->dev, "Measurement out of range\n");
  330. return ARIZONA_HPDET_MAX;
  331. }
  332. dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
  333. val, range);
  334. val = arizona_hpdet_b_ranges[range].factor_b
  335. / ((val * 100) -
  336. arizona_hpdet_b_ranges[range].factor_a);
  337. break;
  338. default:
  339. dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
  340. info->hpdet_ip);
  341. case 2:
  342. if (!(val & ARIZONA_HP_DONE_B)) {
  343. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  344. val);
  345. return -EAGAIN;
  346. }
  347. val &= ARIZONA_HP_LVL_B_MASK;
  348. regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  349. &range);
  350. range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
  351. >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
  352. /* Skip up or down a range? */
  353. if (range && (val < arizona_hpdet_c_ranges[range].min)) {
  354. range--;
  355. dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
  356. arizona_hpdet_c_ranges[range].min,
  357. arizona_hpdet_c_ranges[range].max);
  358. regmap_update_bits(arizona->regmap,
  359. ARIZONA_HEADPHONE_DETECT_1,
  360. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  361. range <<
  362. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  363. return -EAGAIN;
  364. }
  365. if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
  366. (val >= arizona_hpdet_c_ranges[range].max)) {
  367. range++;
  368. dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
  369. arizona_hpdet_c_ranges[range].min,
  370. arizona_hpdet_c_ranges[range].max);
  371. regmap_update_bits(arizona->regmap,
  372. ARIZONA_HEADPHONE_DETECT_1,
  373. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  374. range <<
  375. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  376. return -EAGAIN;
  377. }
  378. }
  379. dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
  380. return val;
  381. }
  382. static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
  383. bool *mic)
  384. {
  385. struct arizona *arizona = info->arizona;
  386. int id_gpio = arizona->pdata.hpdet_id_gpio;
  387. /*
  388. * If we're using HPDET for accessory identification we need
  389. * to take multiple measurements, step through them in sequence.
  390. */
  391. if (arizona->pdata.hpdet_acc_id) {
  392. info->hpdet_res[info->num_hpdet_res++] = *reading;
  393. /* Only check the mic directly if we didn't already ID it */
  394. if (id_gpio && info->num_hpdet_res == 1) {
  395. dev_dbg(arizona->dev, "Measuring mic\n");
  396. regmap_update_bits(arizona->regmap,
  397. ARIZONA_ACCESSORY_DETECT_MODE_1,
  398. ARIZONA_ACCDET_MODE_MASK |
  399. ARIZONA_ACCDET_SRC,
  400. ARIZONA_ACCDET_MODE_HPR |
  401. info->micd_modes[0].src);
  402. gpio_set_value_cansleep(id_gpio, 1);
  403. regmap_update_bits(arizona->regmap,
  404. ARIZONA_HEADPHONE_DETECT_1,
  405. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  406. return -EAGAIN;
  407. }
  408. /* OK, got both. Now, compare... */
  409. dev_dbg(arizona->dev, "HPDET measured %d %d\n",
  410. info->hpdet_res[0], info->hpdet_res[1]);
  411. /* Take the headphone impedance for the main report */
  412. *reading = info->hpdet_res[0];
  413. /* Sometimes we get false readings due to slow insert */
  414. if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
  415. dev_dbg(arizona->dev, "Retrying high impedance\n");
  416. info->num_hpdet_res = 0;
  417. info->hpdet_retried = true;
  418. arizona_start_hpdet_acc_id(info);
  419. pm_runtime_put(info->dev);
  420. return -EAGAIN;
  421. }
  422. /*
  423. * If we measure the mic as
  424. */
  425. if (!id_gpio || info->hpdet_res[1] > 50) {
  426. dev_dbg(arizona->dev, "Detected mic\n");
  427. *mic = true;
  428. info->detecting = true;
  429. } else {
  430. dev_dbg(arizona->dev, "Detected headphone\n");
  431. }
  432. /* Make sure everything is reset back to the real polarity */
  433. regmap_update_bits(arizona->regmap,
  434. ARIZONA_ACCESSORY_DETECT_MODE_1,
  435. ARIZONA_ACCDET_SRC,
  436. info->micd_modes[0].src);
  437. }
  438. return 0;
  439. }
  440. static irqreturn_t arizona_hpdet_irq(int irq, void *data)
  441. {
  442. struct arizona_extcon_info *info = data;
  443. struct arizona *arizona = info->arizona;
  444. int id_gpio = arizona->pdata.hpdet_id_gpio;
  445. int report = ARIZONA_CABLE_HEADPHONE;
  446. int ret, reading;
  447. bool mic = false;
  448. mutex_lock(&info->lock);
  449. /* If we got a spurious IRQ for some reason then ignore it */
  450. if (!info->hpdet_active) {
  451. dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
  452. mutex_unlock(&info->lock);
  453. return IRQ_NONE;
  454. }
  455. /* If the cable was removed while measuring ignore the result */
  456. ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
  457. if (ret < 0) {
  458. dev_err(arizona->dev, "Failed to check cable state: %d\n",
  459. ret);
  460. goto out;
  461. } else if (!ret) {
  462. dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
  463. goto done;
  464. }
  465. ret = arizona_hpdet_read(info);
  466. if (ret == -EAGAIN) {
  467. goto out;
  468. } else if (ret < 0) {
  469. goto done;
  470. }
  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. }
  483. /* Report high impedence cables as line outputs */
  484. if (reading >= 5000)
  485. report = ARIZONA_CABLE_LINEOUT;
  486. else
  487. report = ARIZONA_CABLE_HEADPHONE;
  488. ret = extcon_set_cable_state_(&info->edev, report, true);
  489. if (ret != 0)
  490. dev_err(arizona->dev, "Failed to report HP/line: %d\n",
  491. ret);
  492. arizona_extcon_do_magic(info, 0);
  493. done:
  494. if (id_gpio)
  495. gpio_set_value_cansleep(id_gpio, 0);
  496. /* Revert back to MICDET mode */
  497. regmap_update_bits(arizona->regmap,
  498. ARIZONA_ACCESSORY_DETECT_MODE_1,
  499. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  500. /* If we have a mic then reenable MICDET */
  501. if (mic || info->mic)
  502. arizona_start_mic(info);
  503. if (info->hpdet_active) {
  504. pm_runtime_put_autosuspend(info->dev);
  505. info->hpdet_active = false;
  506. }
  507. info->hpdet_done = true;
  508. out:
  509. mutex_unlock(&info->lock);
  510. return IRQ_HANDLED;
  511. }
  512. static void arizona_identify_headphone(struct arizona_extcon_info *info)
  513. {
  514. struct arizona *arizona = info->arizona;
  515. int ret;
  516. if (info->hpdet_done)
  517. return;
  518. dev_dbg(arizona->dev, "Starting HPDET\n");
  519. /* Make sure we keep the device enabled during the measurement */
  520. pm_runtime_get(info->dev);
  521. info->hpdet_active = true;
  522. if (info->mic)
  523. arizona_stop_mic(info);
  524. arizona_extcon_do_magic(info, 0x4000);
  525. ret = regmap_update_bits(arizona->regmap,
  526. ARIZONA_ACCESSORY_DETECT_MODE_1,
  527. ARIZONA_ACCDET_MODE_MASK,
  528. ARIZONA_ACCDET_MODE_HPL);
  529. if (ret != 0) {
  530. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  531. goto err;
  532. }
  533. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  534. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  535. if (ret != 0) {
  536. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  537. ret);
  538. goto err;
  539. }
  540. return;
  541. err:
  542. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  543. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  544. /* Just report headphone */
  545. ret = extcon_update_state(&info->edev,
  546. 1 << ARIZONA_CABLE_HEADPHONE,
  547. 1 << ARIZONA_CABLE_HEADPHONE);
  548. if (ret != 0)
  549. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  550. if (info->mic)
  551. arizona_start_mic(info);
  552. info->hpdet_active = false;
  553. }
  554. static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
  555. {
  556. struct arizona *arizona = info->arizona;
  557. int hp_reading = 32;
  558. bool mic;
  559. int ret;
  560. dev_dbg(arizona->dev, "Starting identification via HPDET\n");
  561. /* Make sure we keep the device enabled during the measurement */
  562. pm_runtime_get_sync(info->dev);
  563. info->hpdet_active = true;
  564. arizona_extcon_do_magic(info, 0x4000);
  565. ret = regmap_update_bits(arizona->regmap,
  566. ARIZONA_ACCESSORY_DETECT_MODE_1,
  567. ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
  568. info->micd_modes[0].src |
  569. ARIZONA_ACCDET_MODE_HPL);
  570. if (ret != 0) {
  571. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  572. goto err;
  573. }
  574. if (arizona->pdata.hpdet_acc_id_line) {
  575. ret = regmap_update_bits(arizona->regmap,
  576. ARIZONA_HEADPHONE_DETECT_1,
  577. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  578. if (ret != 0) {
  579. dev_err(arizona->dev,
  580. "Can't start HPDETL measurement: %d\n",
  581. ret);
  582. goto err;
  583. }
  584. } else {
  585. arizona_hpdet_do_id(info, &hp_reading, &mic);
  586. }
  587. return;
  588. err:
  589. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  590. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  591. /* Just report headphone */
  592. ret = extcon_update_state(&info->edev,
  593. 1 << ARIZONA_CABLE_HEADPHONE,
  594. 1 << ARIZONA_CABLE_HEADPHONE);
  595. if (ret != 0)
  596. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  597. info->hpdet_active = false;
  598. }
  599. static void arizona_micd_timeout_work(struct work_struct *work)
  600. {
  601. struct arizona_extcon_info *info = container_of(work,
  602. struct arizona_extcon_info,
  603. micd_timeout_work.work);
  604. mutex_lock(&info->lock);
  605. dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
  606. arizona_identify_headphone(info);
  607. info->detecting = false;
  608. arizona_stop_mic(info);
  609. mutex_unlock(&info->lock);
  610. }
  611. static void arizona_micd_detect(struct work_struct *work)
  612. {
  613. struct arizona_extcon_info *info = container_of(work,
  614. struct arizona_extcon_info,
  615. micd_detect_work.work);
  616. struct arizona *arizona = info->arizona;
  617. unsigned int val = 0, lvl;
  618. int ret, i, key;
  619. cancel_delayed_work_sync(&info->micd_timeout_work);
  620. mutex_lock(&info->lock);
  621. for (i = 0; i < 10 && !(val & 0x7fc); i++) {
  622. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  623. if (ret != 0) {
  624. dev_err(arizona->dev,
  625. "Failed to read MICDET: %d\n", ret);
  626. mutex_unlock(&info->lock);
  627. return;
  628. }
  629. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  630. if (!(val & ARIZONA_MICD_VALID)) {
  631. dev_warn(arizona->dev,
  632. "Microphone detection state invalid\n");
  633. mutex_unlock(&info->lock);
  634. return;
  635. }
  636. }
  637. if (i == 10 && !(val & 0x7fc)) {
  638. dev_err(arizona->dev, "Failed to get valid MICDET value\n");
  639. mutex_unlock(&info->lock);
  640. return;
  641. }
  642. /* Due to jack detect this should never happen */
  643. if (!(val & ARIZONA_MICD_STS)) {
  644. dev_warn(arizona->dev, "Detected open circuit\n");
  645. info->detecting = false;
  646. goto handled;
  647. }
  648. /* If we got a high impedence we should have a headset, report it. */
  649. if (info->detecting && (val & 0x400)) {
  650. arizona_identify_headphone(info);
  651. ret = extcon_update_state(&info->edev,
  652. 1 << ARIZONA_CABLE_MICROPHONE,
  653. 1 << ARIZONA_CABLE_MICROPHONE);
  654. if (ret != 0)
  655. dev_err(arizona->dev, "Headset report failed: %d\n",
  656. ret);
  657. /* Don't need to regulate for button detection */
  658. ret = regulator_allow_bypass(info->micvdd, false);
  659. if (ret != 0) {
  660. dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
  661. ret);
  662. }
  663. info->mic = true;
  664. info->detecting = false;
  665. goto handled;
  666. }
  667. /* If we detected a lower impedence during initial startup
  668. * then we probably have the wrong polarity, flip it. Don't
  669. * do this for the lowest impedences to speed up detection of
  670. * plain headphones. If both polarities report a low
  671. * impedence then give up and report headphones.
  672. */
  673. if (info->detecting && (val & 0x3f8)) {
  674. if (info->jack_flips >= info->micd_num_modes * 10) {
  675. dev_dbg(arizona->dev, "Detected HP/line\n");
  676. arizona_identify_headphone(info);
  677. info->detecting = false;
  678. arizona_stop_mic(info);
  679. } else {
  680. info->micd_mode++;
  681. if (info->micd_mode == info->micd_num_modes)
  682. info->micd_mode = 0;
  683. arizona_extcon_set_mode(info, info->micd_mode);
  684. info->jack_flips++;
  685. }
  686. goto handled;
  687. }
  688. /*
  689. * If we're still detecting and we detect a short then we've
  690. * got a headphone. Otherwise it's a button press.
  691. */
  692. if (val & 0x3fc) {
  693. if (info->mic) {
  694. dev_dbg(arizona->dev, "Mic button detected\n");
  695. lvl = val & ARIZONA_MICD_LVL_MASK;
  696. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  697. for (i = 0; i < info->num_micd_ranges; i++)
  698. input_report_key(info->input,
  699. info->micd_ranges[i].key, 0);
  700. WARN_ON(!lvl);
  701. WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
  702. if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
  703. key = info->micd_ranges[ffs(lvl) - 1].key;
  704. input_report_key(info->input, key, 1);
  705. input_sync(info->input);
  706. }
  707. } else if (info->detecting) {
  708. dev_dbg(arizona->dev, "Headphone detected\n");
  709. info->detecting = false;
  710. arizona_stop_mic(info);
  711. arizona_identify_headphone(info);
  712. } else {
  713. dev_warn(arizona->dev, "Button with no mic: %x\n",
  714. val);
  715. }
  716. } else {
  717. dev_dbg(arizona->dev, "Mic button released\n");
  718. for (i = 0; i < info->num_micd_ranges; i++)
  719. input_report_key(info->input,
  720. info->micd_ranges[i].key, 0);
  721. input_sync(info->input);
  722. arizona_extcon_pulse_micbias(info);
  723. }
  724. handled:
  725. if (info->detecting)
  726. queue_delayed_work(system_power_efficient_wq,
  727. &info->micd_timeout_work,
  728. msecs_to_jiffies(info->micd_timeout));
  729. pm_runtime_mark_last_busy(info->dev);
  730. mutex_unlock(&info->lock);
  731. }
  732. static irqreturn_t arizona_micdet(int irq, void *data)
  733. {
  734. struct arizona_extcon_info *info = data;
  735. struct arizona *arizona = info->arizona;
  736. int debounce = arizona->pdata.micd_detect_debounce;
  737. cancel_delayed_work_sync(&info->micd_detect_work);
  738. cancel_delayed_work_sync(&info->micd_timeout_work);
  739. mutex_lock(&info->lock);
  740. if (!info->detecting)
  741. debounce = 0;
  742. mutex_unlock(&info->lock);
  743. if (debounce)
  744. queue_delayed_work(system_power_efficient_wq,
  745. &info->micd_detect_work,
  746. msecs_to_jiffies(debounce));
  747. else
  748. arizona_micd_detect(&info->micd_detect_work.work);
  749. return IRQ_HANDLED;
  750. }
  751. static void arizona_hpdet_work(struct work_struct *work)
  752. {
  753. struct arizona_extcon_info *info = container_of(work,
  754. struct arizona_extcon_info,
  755. hpdet_work.work);
  756. mutex_lock(&info->lock);
  757. arizona_start_hpdet_acc_id(info);
  758. mutex_unlock(&info->lock);
  759. }
  760. static irqreturn_t arizona_jackdet(int irq, void *data)
  761. {
  762. struct arizona_extcon_info *info = data;
  763. struct arizona *arizona = info->arizona;
  764. unsigned int val, present, mask;
  765. bool cancelled_hp, cancelled_mic;
  766. int ret, i;
  767. cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
  768. cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
  769. pm_runtime_get_sync(info->dev);
  770. mutex_lock(&info->lock);
  771. if (arizona->pdata.jd_gpio5) {
  772. mask = ARIZONA_MICD_CLAMP_STS;
  773. present = 0;
  774. } else {
  775. mask = ARIZONA_JD1_STS;
  776. present = ARIZONA_JD1_STS;
  777. }
  778. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  779. if (ret != 0) {
  780. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  781. ret);
  782. mutex_unlock(&info->lock);
  783. pm_runtime_put_autosuspend(info->dev);
  784. return IRQ_NONE;
  785. }
  786. val &= mask;
  787. if (val == info->last_jackdet) {
  788. dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
  789. if (cancelled_hp)
  790. queue_delayed_work(system_power_efficient_wq,
  791. &info->hpdet_work,
  792. msecs_to_jiffies(HPDET_DEBOUNCE));
  793. if (cancelled_mic) {
  794. int micd_timeout = info->micd_timeout;
  795. queue_delayed_work(system_power_efficient_wq,
  796. &info->micd_timeout_work,
  797. msecs_to_jiffies(micd_timeout));
  798. }
  799. goto out;
  800. }
  801. info->last_jackdet = val;
  802. if (info->last_jackdet == present) {
  803. dev_dbg(arizona->dev, "Detected jack\n");
  804. ret = extcon_set_cable_state_(&info->edev,
  805. ARIZONA_CABLE_MECHANICAL, true);
  806. if (ret != 0)
  807. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  808. ret);
  809. if (!arizona->pdata.hpdet_acc_id) {
  810. info->detecting = true;
  811. info->mic = false;
  812. info->jack_flips = 0;
  813. arizona_start_mic(info);
  814. } else {
  815. queue_delayed_work(system_power_efficient_wq,
  816. &info->hpdet_work,
  817. msecs_to_jiffies(HPDET_DEBOUNCE));
  818. }
  819. regmap_update_bits(arizona->regmap,
  820. ARIZONA_JACK_DETECT_DEBOUNCE,
  821. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
  822. } else {
  823. dev_dbg(arizona->dev, "Detected jack removal\n");
  824. arizona_stop_mic(info);
  825. info->num_hpdet_res = 0;
  826. for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
  827. info->hpdet_res[i] = 0;
  828. info->mic = false;
  829. info->hpdet_done = false;
  830. info->hpdet_retried = false;
  831. for (i = 0; i < info->num_micd_ranges; i++)
  832. input_report_key(info->input,
  833. info->micd_ranges[i].key, 0);
  834. input_sync(info->input);
  835. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  836. if (ret != 0)
  837. dev_err(arizona->dev, "Removal report failed: %d\n",
  838. ret);
  839. regmap_update_bits(arizona->regmap,
  840. ARIZONA_JACK_DETECT_DEBOUNCE,
  841. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
  842. ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
  843. }
  844. if (arizona->pdata.micd_timeout)
  845. info->micd_timeout = arizona->pdata.micd_timeout;
  846. else
  847. info->micd_timeout = DEFAULT_MICD_TIMEOUT;
  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. out:
  855. mutex_unlock(&info->lock);
  856. pm_runtime_mark_last_busy(info->dev);
  857. pm_runtime_put_autosuspend(info->dev);
  858. return IRQ_HANDLED;
  859. }
  860. /* Map a level onto a slot in the register bank */
  861. static void arizona_micd_set_level(struct arizona *arizona, int index,
  862. unsigned int level)
  863. {
  864. int reg;
  865. unsigned int mask;
  866. reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
  867. if (!(index % 2)) {
  868. mask = 0x3f00;
  869. level <<= 8;
  870. } else {
  871. mask = 0x3f;
  872. }
  873. /* Program the level itself */
  874. regmap_update_bits(arizona->regmap, reg, mask, level);
  875. }
  876. static int arizona_extcon_probe(struct platform_device *pdev)
  877. {
  878. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  879. struct arizona_pdata *pdata;
  880. struct arizona_extcon_info *info;
  881. unsigned int val;
  882. int jack_irq_fall, jack_irq_rise;
  883. int ret, mode, i, j;
  884. if (!arizona->dapm || !arizona->dapm->card)
  885. return -EPROBE_DEFER;
  886. pdata = dev_get_platdata(arizona->dev);
  887. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  888. if (!info) {
  889. dev_err(&pdev->dev, "Failed to allocate memory\n");
  890. ret = -ENOMEM;
  891. goto err;
  892. }
  893. info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
  894. if (IS_ERR(info->micvdd)) {
  895. ret = PTR_ERR(info->micvdd);
  896. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  897. goto err;
  898. }
  899. mutex_init(&info->lock);
  900. info->arizona = arizona;
  901. info->dev = &pdev->dev;
  902. info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
  903. INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
  904. INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
  905. INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
  906. platform_set_drvdata(pdev, info);
  907. switch (arizona->type) {
  908. case WM5102:
  909. switch (arizona->rev) {
  910. case 0:
  911. info->micd_reva = true;
  912. break;
  913. default:
  914. info->micd_clamp = true;
  915. info->hpdet_ip = 1;
  916. break;
  917. }
  918. break;
  919. default:
  920. break;
  921. }
  922. info->edev.name = "Headset Jack";
  923. info->edev.supported_cable = arizona_cable;
  924. ret = extcon_dev_register(&info->edev, arizona->dev);
  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");