extcon-arizona.c 44 KB

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