extcon-arizona.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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 <linux/mfd/arizona/core.h>
  29. #include <linux/mfd/arizona/pdata.h>
  30. #include <linux/mfd/arizona/registers.h>
  31. #define ARIZONA_DEFAULT_HP 32
  32. #define ARIZONA_NUM_BUTTONS 6
  33. #define ARIZONA_ACCDET_MODE_MIC 0
  34. #define ARIZONA_ACCDET_MODE_HPL 1
  35. #define ARIZONA_ACCDET_MODE_HPR 2
  36. struct arizona_extcon_info {
  37. struct device *dev;
  38. struct arizona *arizona;
  39. struct mutex lock;
  40. struct regulator *micvdd;
  41. struct input_dev *input;
  42. int micd_mode;
  43. const struct arizona_micd_config *micd_modes;
  44. int micd_num_modes;
  45. bool micd_reva;
  46. bool micd_clamp;
  47. bool hpdet_active;
  48. int num_hpdet_res;
  49. unsigned int hpdet_res[3];
  50. bool mic;
  51. bool detecting;
  52. int jack_flips;
  53. int hpdet_ip;
  54. struct extcon_dev edev;
  55. };
  56. static const struct arizona_micd_config micd_default_modes[] = {
  57. { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
  58. { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
  59. };
  60. static struct {
  61. u16 status;
  62. int report;
  63. } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
  64. { 0x1, BTN_0 },
  65. { 0x2, BTN_1 },
  66. { 0x4, BTN_2 },
  67. { 0x8, BTN_3 },
  68. { 0x10, BTN_4 },
  69. { 0x20, BTN_5 },
  70. };
  71. #define ARIZONA_CABLE_MECHANICAL 0
  72. #define ARIZONA_CABLE_MICROPHONE 1
  73. #define ARIZONA_CABLE_HEADPHONE 2
  74. #define ARIZONA_CABLE_LINEOUT 3
  75. static const char *arizona_cable[] = {
  76. "Mechanical",
  77. "Microphone",
  78. "Headphone",
  79. "Line-out",
  80. NULL,
  81. };
  82. static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
  83. {
  84. struct arizona *arizona = info->arizona;
  85. if (arizona->pdata.micd_pol_gpio > 0)
  86. gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
  87. info->micd_modes[mode].gpio);
  88. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  89. ARIZONA_MICD_BIAS_SRC_MASK,
  90. info->micd_modes[mode].bias);
  91. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  92. ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
  93. info->micd_mode = mode;
  94. dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
  95. }
  96. static void arizona_start_mic(struct arizona_extcon_info *info)
  97. {
  98. struct arizona *arizona = info->arizona;
  99. bool change;
  100. int ret;
  101. /* Microphone detection can't use idle mode */
  102. pm_runtime_get(info->dev);
  103. ret = regulator_enable(info->micvdd);
  104. if (ret != 0) {
  105. dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
  106. ret);
  107. }
  108. if (info->micd_reva) {
  109. regmap_write(arizona->regmap, 0x80, 0x3);
  110. regmap_write(arizona->regmap, 0x294, 0);
  111. regmap_write(arizona->regmap, 0x80, 0x0);
  112. }
  113. regmap_update_bits(arizona->regmap,
  114. ARIZONA_ACCESSORY_DETECT_MODE_1,
  115. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  116. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  117. ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
  118. &change);
  119. if (!change) {
  120. regulator_disable(info->micvdd);
  121. pm_runtime_put_autosuspend(info->dev);
  122. }
  123. }
  124. static void arizona_stop_mic(struct arizona_extcon_info *info)
  125. {
  126. struct arizona *arizona = info->arizona;
  127. bool change;
  128. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  129. ARIZONA_MICD_ENA, 0,
  130. &change);
  131. if (info->micd_reva) {
  132. regmap_write(arizona->regmap, 0x80, 0x3);
  133. regmap_write(arizona->regmap, 0x294, 2);
  134. regmap_write(arizona->regmap, 0x80, 0x0);
  135. }
  136. if (change) {
  137. regulator_disable(info->micvdd);
  138. pm_runtime_mark_last_busy(info->dev);
  139. pm_runtime_put_autosuspend(info->dev);
  140. }
  141. }
  142. static struct {
  143. unsigned int factor_a;
  144. unsigned int factor_b;
  145. } arizona_hpdet_b_ranges[] = {
  146. { 5528, 362464 },
  147. { 11084, 6186851 },
  148. { 11065, 65460395 },
  149. };
  150. static struct {
  151. int min;
  152. int max;
  153. } arizona_hpdet_c_ranges[] = {
  154. { 0, 30 },
  155. { 8, 100 },
  156. { 100, 1000 },
  157. { 1000, 10000 },
  158. };
  159. static int arizona_hpdet_read(struct arizona_extcon_info *info)
  160. {
  161. struct arizona *arizona = info->arizona;
  162. unsigned int val, range;
  163. int ret;
  164. ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
  165. if (ret != 0) {
  166. dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
  167. ret);
  168. return ret;
  169. }
  170. switch (info->hpdet_ip) {
  171. case 0:
  172. if (!(val & ARIZONA_HP_DONE)) {
  173. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  174. val);
  175. val = ARIZONA_DEFAULT_HP;
  176. }
  177. val &= ARIZONA_HP_LVL_MASK;
  178. break;
  179. case 1:
  180. if (!(val & ARIZONA_HP_DONE_B)) {
  181. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  182. val);
  183. return ARIZONA_DEFAULT_HP;
  184. }
  185. ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
  186. if (ret != 0) {
  187. dev_err(arizona->dev, "Failed to read HP value: %d\n",
  188. ret);
  189. return ARIZONA_DEFAULT_HP;
  190. }
  191. regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  192. &range);
  193. range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
  194. >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
  195. if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
  196. (val < 100 || val > 0x3fb)) {
  197. range++;
  198. dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
  199. range);
  200. regmap_update_bits(arizona->regmap,
  201. ARIZONA_HEADPHONE_DETECT_1,
  202. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  203. range <<
  204. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  205. return -EAGAIN;
  206. }
  207. /* If we go out of range report top of range */
  208. if (val < 100 || val > 0x3fb) {
  209. dev_dbg(arizona->dev, "Measurement out of range\n");
  210. return 10000;
  211. }
  212. dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
  213. val, range);
  214. val = arizona_hpdet_b_ranges[range].factor_b
  215. / ((val * 100) -
  216. arizona_hpdet_b_ranges[range].factor_a);
  217. break;
  218. default:
  219. dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
  220. info->hpdet_ip);
  221. case 2:
  222. if (!(val & ARIZONA_HP_DONE_B)) {
  223. dev_err(arizona->dev, "HPDET did not complete: %x\n",
  224. val);
  225. return ARIZONA_DEFAULT_HP;
  226. }
  227. val &= ARIZONA_HP_LVL_B_MASK;
  228. regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  229. &range);
  230. range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
  231. >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
  232. /* Skip up or down a range? */
  233. if (range && (val < arizona_hpdet_c_ranges[range].min)) {
  234. range--;
  235. dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
  236. arizona_hpdet_c_ranges[range].min,
  237. arizona_hpdet_c_ranges[range].max);
  238. regmap_update_bits(arizona->regmap,
  239. ARIZONA_HEADPHONE_DETECT_1,
  240. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  241. range <<
  242. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  243. return -EAGAIN;
  244. }
  245. if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
  246. (val >= arizona_hpdet_c_ranges[range].max)) {
  247. range++;
  248. dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
  249. arizona_hpdet_c_ranges[range].min,
  250. arizona_hpdet_c_ranges[range].max);
  251. regmap_update_bits(arizona->regmap,
  252. ARIZONA_HEADPHONE_DETECT_1,
  253. ARIZONA_HP_IMPEDANCE_RANGE_MASK,
  254. range <<
  255. ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
  256. return -EAGAIN;
  257. }
  258. }
  259. dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
  260. return val;
  261. }
  262. static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
  263. {
  264. struct arizona *arizona = info->arizona;
  265. int id_gpio = arizona->pdata.hpdet_id_gpio;
  266. int ret;
  267. /*
  268. * If we're using HPDET for accessory identification we need
  269. * to take multiple measurements, step through them in sequence.
  270. */
  271. if (arizona->pdata.hpdet_acc_id) {
  272. info->hpdet_res[info->num_hpdet_res++] = *reading;
  273. /*
  274. * If the impedence is too high don't measure the
  275. * second ground.
  276. */
  277. if (info->num_hpdet_res == 1 && *reading >= 45) {
  278. dev_dbg(arizona->dev, "Skipping ground flip\n");
  279. info->hpdet_res[info->num_hpdet_res++] = *reading;
  280. }
  281. if (info->num_hpdet_res == 1) {
  282. dev_dbg(arizona->dev, "Flipping ground\n");
  283. regmap_update_bits(arizona->regmap,
  284. ARIZONA_ACCESSORY_DETECT_MODE_1,
  285. ARIZONA_ACCDET_SRC,
  286. ~info->micd_modes[0].src);
  287. regmap_update_bits(arizona->regmap,
  288. ARIZONA_HEADPHONE_DETECT_1,
  289. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  290. return -EAGAIN;
  291. }
  292. /* Only check the mic directly if we didn't already ID it */
  293. if (id_gpio && info->num_hpdet_res == 2 &&
  294. !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
  295. dev_dbg(arizona->dev, "Measuring mic\n");
  296. regmap_update_bits(arizona->regmap,
  297. ARIZONA_ACCESSORY_DETECT_MODE_1,
  298. ARIZONA_ACCDET_MODE_MASK |
  299. ARIZONA_ACCDET_SRC,
  300. ARIZONA_ACCDET_MODE_HPR |
  301. info->micd_modes[0].src);
  302. gpio_set_value_cansleep(id_gpio, 1);
  303. regmap_update_bits(arizona->regmap,
  304. ARIZONA_HEADPHONE_DETECT_1,
  305. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  306. return -EAGAIN;
  307. }
  308. /* OK, got both. Now, compare... */
  309. dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
  310. info->hpdet_res[0], info->hpdet_res[1],
  311. info->hpdet_res[2]);
  312. /*
  313. * Either the two grounds measure differently or we
  314. * measure the mic as high impedance.
  315. */
  316. if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
  317. (id_gpio && info->hpdet_res[2] > 10)) {
  318. dev_dbg(arizona->dev, "Detected mic\n");
  319. info->mic = true;
  320. ret = extcon_set_cable_state_(&info->edev,
  321. ARIZONA_CABLE_MICROPHONE,
  322. true);
  323. if (ret != 0) {
  324. dev_err(arizona->dev,
  325. "Failed to report mic: %d\n", ret);
  326. }
  327. /* Take the headphone impedance for the main report */
  328. *reading = info->hpdet_res[1];
  329. } else {
  330. dev_dbg(arizona->dev, "Detected headphone\n");
  331. }
  332. /* Make sure everything is reset back to the real polarity */
  333. regmap_update_bits(arizona->regmap,
  334. ARIZONA_ACCESSORY_DETECT_MODE_1,
  335. ARIZONA_ACCDET_SRC,
  336. info->micd_modes[0].src);
  337. }
  338. return 0;
  339. }
  340. static irqreturn_t arizona_hpdet_irq(int irq, void *data)
  341. {
  342. struct arizona_extcon_info *info = data;
  343. struct arizona *arizona = info->arizona;
  344. int id_gpio = arizona->pdata.hpdet_id_gpio;
  345. int report = ARIZONA_CABLE_HEADPHONE;
  346. int ret, reading;
  347. mutex_lock(&info->lock);
  348. /* If we got a spurious IRQ for some reason then ignore it */
  349. if (!info->hpdet_active) {
  350. dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
  351. mutex_unlock(&info->lock);
  352. return IRQ_NONE;
  353. }
  354. /* If the cable was removed while measuring ignore the result */
  355. ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
  356. if (ret < 0) {
  357. dev_err(arizona->dev, "Failed to check cable state: %d\n",
  358. ret);
  359. goto out;
  360. } else if (!ret) {
  361. dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
  362. goto done;
  363. }
  364. ret = arizona_hpdet_read(info);
  365. if (ret == -EAGAIN) {
  366. goto out;
  367. } else if (ret < 0) {
  368. goto done;
  369. }
  370. reading = ret;
  371. /* Reset back to starting range */
  372. regmap_update_bits(arizona->regmap,
  373. ARIZONA_HEADPHONE_DETECT_1,
  374. ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
  375. 0);
  376. ret = arizona_hpdet_do_id(info, &reading);
  377. if (ret == -EAGAIN) {
  378. goto out;
  379. } else if (ret < 0) {
  380. goto done;
  381. }
  382. /* Report high impedence cables as line outputs */
  383. if (reading >= 5000)
  384. report = ARIZONA_CABLE_LINEOUT;
  385. else
  386. report = ARIZONA_CABLE_HEADPHONE;
  387. ret = extcon_set_cable_state_(&info->edev, report, true);
  388. if (ret != 0)
  389. dev_err(arizona->dev, "Failed to report HP/line: %d\n",
  390. ret);
  391. ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
  392. if (ret != 0)
  393. dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
  394. ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
  395. if (ret != 0)
  396. dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
  397. done:
  398. if (id_gpio)
  399. gpio_set_value_cansleep(id_gpio, 0);
  400. /* Revert back to MICDET mode */
  401. regmap_update_bits(arizona->regmap,
  402. ARIZONA_ACCESSORY_DETECT_MODE_1,
  403. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  404. /* If we have a mic then reenable MICDET */
  405. if (info->mic)
  406. arizona_start_mic(info);
  407. if (info->hpdet_active) {
  408. pm_runtime_put_autosuspend(info->dev);
  409. info->hpdet_active = false;
  410. }
  411. out:
  412. mutex_unlock(&info->lock);
  413. return IRQ_HANDLED;
  414. }
  415. static void arizona_identify_headphone(struct arizona_extcon_info *info)
  416. {
  417. struct arizona *arizona = info->arizona;
  418. int ret;
  419. dev_dbg(arizona->dev, "Starting HPDET\n");
  420. /* Make sure we keep the device enabled during the measurement */
  421. pm_runtime_get(info->dev);
  422. info->hpdet_active = true;
  423. if (info->mic)
  424. arizona_stop_mic(info);
  425. ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
  426. if (ret != 0)
  427. dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
  428. ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
  429. if (ret != 0)
  430. dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
  431. ret = regmap_update_bits(arizona->regmap,
  432. ARIZONA_ACCESSORY_DETECT_MODE_1,
  433. ARIZONA_ACCDET_MODE_MASK,
  434. ARIZONA_ACCDET_MODE_HPL);
  435. if (ret != 0) {
  436. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  437. goto err;
  438. }
  439. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  440. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  441. if (ret != 0) {
  442. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  443. ret);
  444. goto err;
  445. }
  446. return;
  447. err:
  448. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  449. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  450. /* Just report headphone */
  451. ret = extcon_update_state(&info->edev,
  452. 1 << ARIZONA_CABLE_HEADPHONE,
  453. 1 << ARIZONA_CABLE_HEADPHONE);
  454. if (ret != 0)
  455. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  456. if (info->mic)
  457. arizona_start_mic(info);
  458. info->hpdet_active = false;
  459. }
  460. static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
  461. {
  462. struct arizona *arizona = info->arizona;
  463. int ret;
  464. dev_dbg(arizona->dev, "Starting identification via HPDET\n");
  465. /* Make sure we keep the device enabled during the measurement */
  466. pm_runtime_get(info->dev);
  467. info->hpdet_active = true;
  468. ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
  469. if (ret != 0)
  470. dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
  471. ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
  472. if (ret != 0)
  473. dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
  474. ret = regmap_update_bits(arizona->regmap,
  475. ARIZONA_ACCESSORY_DETECT_MODE_1,
  476. ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
  477. info->micd_modes[0].src |
  478. ARIZONA_ACCDET_MODE_HPL);
  479. if (ret != 0) {
  480. dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
  481. goto err;
  482. }
  483. ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
  484. ARIZONA_HP_POLL, ARIZONA_HP_POLL);
  485. if (ret != 0) {
  486. dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
  487. ret);
  488. goto err;
  489. }
  490. return;
  491. err:
  492. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  493. ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
  494. /* Just report headphone */
  495. ret = extcon_update_state(&info->edev,
  496. 1 << ARIZONA_CABLE_HEADPHONE,
  497. 1 << ARIZONA_CABLE_HEADPHONE);
  498. if (ret != 0)
  499. dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
  500. info->hpdet_active = false;
  501. }
  502. static irqreturn_t arizona_micdet(int irq, void *data)
  503. {
  504. struct arizona_extcon_info *info = data;
  505. struct arizona *arizona = info->arizona;
  506. unsigned int val, lvl;
  507. int ret, i;
  508. mutex_lock(&info->lock);
  509. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  510. if (ret != 0) {
  511. dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
  512. mutex_unlock(&info->lock);
  513. return IRQ_NONE;
  514. }
  515. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  516. if (!(val & ARIZONA_MICD_VALID)) {
  517. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  518. mutex_unlock(&info->lock);
  519. return IRQ_NONE;
  520. }
  521. /* Due to jack detect this should never happen */
  522. if (!(val & ARIZONA_MICD_STS)) {
  523. dev_warn(arizona->dev, "Detected open circuit\n");
  524. info->detecting = false;
  525. goto handled;
  526. }
  527. /* If we got a high impedence we should have a headset, report it. */
  528. if (info->detecting && (val & 0x400)) {
  529. arizona_identify_headphone(info);
  530. ret = extcon_update_state(&info->edev,
  531. 1 << ARIZONA_CABLE_MICROPHONE,
  532. 1 << ARIZONA_CABLE_MICROPHONE);
  533. if (ret != 0)
  534. dev_err(arizona->dev, "Headset report failed: %d\n",
  535. ret);
  536. info->mic = true;
  537. info->detecting = false;
  538. goto handled;
  539. }
  540. /* If we detected a lower impedence during initial startup
  541. * then we probably have the wrong polarity, flip it. Don't
  542. * do this for the lowest impedences to speed up detection of
  543. * plain headphones. If both polarities report a low
  544. * impedence then give up and report headphones.
  545. */
  546. if (info->detecting && (val & 0x3f8)) {
  547. info->jack_flips++;
  548. if (info->jack_flips >= info->micd_num_modes) {
  549. dev_dbg(arizona->dev, "Detected HP/line\n");
  550. arizona_identify_headphone(info);
  551. info->detecting = false;
  552. arizona_stop_mic(info);
  553. } else {
  554. info->micd_mode++;
  555. if (info->micd_mode == info->micd_num_modes)
  556. info->micd_mode = 0;
  557. arizona_extcon_set_mode(info, info->micd_mode);
  558. info->jack_flips++;
  559. }
  560. goto handled;
  561. }
  562. /*
  563. * If we're still detecting and we detect a short then we've
  564. * got a headphone. Otherwise it's a button press.
  565. */
  566. if (val & 0x3fc) {
  567. if (info->mic) {
  568. dev_dbg(arizona->dev, "Mic button detected\n");
  569. lvl = val & ARIZONA_MICD_LVL_MASK;
  570. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  571. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  572. if (lvl & arizona_lvl_to_key[i].status)
  573. input_report_key(info->input,
  574. arizona_lvl_to_key[i].report,
  575. 1);
  576. input_sync(info->input);
  577. } else if (info->detecting) {
  578. dev_dbg(arizona->dev, "Headphone detected\n");
  579. info->detecting = false;
  580. arizona_stop_mic(info);
  581. arizona_identify_headphone(info);
  582. } else {
  583. dev_warn(arizona->dev, "Button with no mic: %x\n",
  584. val);
  585. }
  586. } else {
  587. dev_dbg(arizona->dev, "Mic button released\n");
  588. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  589. input_report_key(info->input,
  590. arizona_lvl_to_key[i].report, 0);
  591. input_sync(info->input);
  592. }
  593. handled:
  594. pm_runtime_mark_last_busy(info->dev);
  595. mutex_unlock(&info->lock);
  596. return IRQ_HANDLED;
  597. }
  598. static irqreturn_t arizona_jackdet(int irq, void *data)
  599. {
  600. struct arizona_extcon_info *info = data;
  601. struct arizona *arizona = info->arizona;
  602. unsigned int val, present, mask;
  603. int ret, i;
  604. pm_runtime_get_sync(info->dev);
  605. mutex_lock(&info->lock);
  606. if (arizona->pdata.jd_gpio5) {
  607. mask = ARIZONA_MICD_CLAMP_STS;
  608. present = 0;
  609. } else {
  610. mask = ARIZONA_JD1_STS;
  611. present = ARIZONA_JD1_STS;
  612. }
  613. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  614. if (ret != 0) {
  615. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  616. ret);
  617. mutex_unlock(&info->lock);
  618. pm_runtime_put_autosuspend(info->dev);
  619. return IRQ_NONE;
  620. }
  621. if ((val & mask) == present) {
  622. dev_dbg(arizona->dev, "Detected jack\n");
  623. ret = extcon_set_cable_state_(&info->edev,
  624. ARIZONA_CABLE_MECHANICAL, true);
  625. if (ret != 0)
  626. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  627. ret);
  628. if (!arizona->pdata.hpdet_acc_id) {
  629. info->detecting = true;
  630. info->mic = false;
  631. info->jack_flips = 0;
  632. arizona_start_mic(info);
  633. } else {
  634. arizona_start_hpdet_acc_id(info);
  635. }
  636. } else {
  637. dev_dbg(arizona->dev, "Detected jack removal\n");
  638. arizona_stop_mic(info);
  639. info->num_hpdet_res = 0;
  640. for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
  641. info->hpdet_res[i] = 0;
  642. info->mic = false;
  643. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  644. input_report_key(info->input,
  645. arizona_lvl_to_key[i].report, 0);
  646. input_sync(info->input);
  647. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  648. if (ret != 0)
  649. dev_err(arizona->dev, "Removal report failed: %d\n",
  650. ret);
  651. }
  652. mutex_unlock(&info->lock);
  653. pm_runtime_mark_last_busy(info->dev);
  654. pm_runtime_put_autosuspend(info->dev);
  655. return IRQ_HANDLED;
  656. }
  657. static int arizona_extcon_probe(struct platform_device *pdev)
  658. {
  659. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  660. struct arizona_pdata *pdata;
  661. struct arizona_extcon_info *info;
  662. int jack_irq_fall, jack_irq_rise;
  663. int ret, mode, i;
  664. pdata = dev_get_platdata(arizona->dev);
  665. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  666. if (!info) {
  667. dev_err(&pdev->dev, "Failed to allocate memory\n");
  668. ret = -ENOMEM;
  669. goto err;
  670. }
  671. info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
  672. if (IS_ERR(info->micvdd)) {
  673. ret = PTR_ERR(info->micvdd);
  674. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  675. goto err;
  676. }
  677. mutex_init(&info->lock);
  678. info->arizona = arizona;
  679. info->dev = &pdev->dev;
  680. platform_set_drvdata(pdev, info);
  681. switch (arizona->type) {
  682. case WM5102:
  683. switch (arizona->rev) {
  684. case 0:
  685. info->micd_reva = true;
  686. break;
  687. default:
  688. info->micd_clamp = true;
  689. info->hpdet_ip = 1;
  690. break;
  691. }
  692. break;
  693. default:
  694. break;
  695. }
  696. info->edev.name = "Headset Jack";
  697. info->edev.supported_cable = arizona_cable;
  698. ret = extcon_dev_register(&info->edev, arizona->dev);
  699. if (ret < 0) {
  700. dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
  701. ret);
  702. goto err;
  703. }
  704. if (pdata->num_micd_configs) {
  705. info->micd_modes = pdata->micd_configs;
  706. info->micd_num_modes = pdata->num_micd_configs;
  707. } else {
  708. info->micd_modes = micd_default_modes;
  709. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  710. }
  711. if (arizona->pdata.micd_pol_gpio > 0) {
  712. if (info->micd_modes[0].gpio)
  713. mode = GPIOF_OUT_INIT_HIGH;
  714. else
  715. mode = GPIOF_OUT_INIT_LOW;
  716. ret = devm_gpio_request_one(&pdev->dev,
  717. arizona->pdata.micd_pol_gpio,
  718. mode,
  719. "MICD polarity");
  720. if (ret != 0) {
  721. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  722. arizona->pdata.micd_pol_gpio, ret);
  723. goto err_register;
  724. }
  725. }
  726. if (arizona->pdata.hpdet_id_gpio > 0) {
  727. ret = devm_gpio_request_one(&pdev->dev,
  728. arizona->pdata.hpdet_id_gpio,
  729. GPIOF_OUT_INIT_LOW,
  730. "HPDET");
  731. if (ret != 0) {
  732. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  733. arizona->pdata.hpdet_id_gpio, ret);
  734. goto err_register;
  735. }
  736. }
  737. if (arizona->pdata.micd_bias_start_time)
  738. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  739. ARIZONA_MICD_BIAS_STARTTIME_MASK,
  740. arizona->pdata.micd_bias_start_time
  741. << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
  742. /*
  743. * If we have a clamp use it, activating in conjunction with
  744. * GPIO5 if that is connected for jack detect operation.
  745. */
  746. if (info->micd_clamp) {
  747. if (arizona->pdata.jd_gpio5) {
  748. /* Put the GPIO into input mode */
  749. regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
  750. 0xc101);
  751. regmap_update_bits(arizona->regmap,
  752. ARIZONA_MICD_CLAMP_CONTROL,
  753. ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
  754. } else {
  755. regmap_update_bits(arizona->regmap,
  756. ARIZONA_MICD_CLAMP_CONTROL,
  757. ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
  758. }
  759. regmap_update_bits(arizona->regmap,
  760. ARIZONA_JACK_DETECT_DEBOUNCE,
  761. ARIZONA_MICD_CLAMP_DB,
  762. ARIZONA_MICD_CLAMP_DB);
  763. }
  764. arizona_extcon_set_mode(info, 0);
  765. info->input = devm_input_allocate_device(&pdev->dev);
  766. if (!info->input) {
  767. dev_err(arizona->dev, "Can't allocate input dev\n");
  768. ret = -ENOMEM;
  769. goto err_register;
  770. }
  771. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  772. input_set_capability(info->input, EV_KEY,
  773. arizona_lvl_to_key[i].report);
  774. info->input->name = "Headset";
  775. info->input->phys = "arizona/extcon";
  776. info->input->dev.parent = &pdev->dev;
  777. pm_runtime_enable(&pdev->dev);
  778. pm_runtime_idle(&pdev->dev);
  779. pm_runtime_get_sync(&pdev->dev);
  780. if (arizona->pdata.jd_gpio5) {
  781. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  782. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  783. } else {
  784. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  785. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  786. }
  787. ret = arizona_request_irq(arizona, jack_irq_rise,
  788. "JACKDET rise", arizona_jackdet, info);
  789. if (ret != 0) {
  790. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  791. ret);
  792. goto err_input;
  793. }
  794. ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
  795. if (ret != 0) {
  796. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  797. ret);
  798. goto err_rise;
  799. }
  800. ret = arizona_request_irq(arizona, jack_irq_fall,
  801. "JACKDET fall", arizona_jackdet, info);
  802. if (ret != 0) {
  803. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  804. goto err_rise_wake;
  805. }
  806. ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
  807. if (ret != 0) {
  808. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  809. ret);
  810. goto err_fall;
  811. }
  812. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  813. "MICDET", arizona_micdet, info);
  814. if (ret != 0) {
  815. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  816. goto err_fall_wake;
  817. }
  818. ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
  819. "HPDET", arizona_hpdet_irq, info);
  820. if (ret != 0) {
  821. dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
  822. goto err_micdet;
  823. }
  824. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  825. ARIZONA_MICD_RATE_MASK,
  826. 8 << ARIZONA_MICD_RATE_SHIFT);
  827. arizona_clk32k_enable(arizona);
  828. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  829. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  830. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  831. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  832. ret = regulator_allow_bypass(info->micvdd, true);
  833. if (ret != 0)
  834. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
  835. ret);
  836. pm_runtime_put(&pdev->dev);
  837. ret = input_register_device(info->input);
  838. if (ret) {
  839. dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
  840. goto err_hpdet;
  841. }
  842. return 0;
  843. err_hpdet:
  844. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  845. err_micdet:
  846. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  847. err_fall_wake:
  848. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  849. err_fall:
  850. arizona_free_irq(arizona, jack_irq_fall, info);
  851. err_rise_wake:
  852. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  853. err_rise:
  854. arizona_free_irq(arizona, jack_irq_rise, info);
  855. err_input:
  856. err_register:
  857. pm_runtime_disable(&pdev->dev);
  858. extcon_dev_unregister(&info->edev);
  859. err:
  860. return ret;
  861. }
  862. static int arizona_extcon_remove(struct platform_device *pdev)
  863. {
  864. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  865. struct arizona *arizona = info->arizona;
  866. int jack_irq_rise, jack_irq_fall;
  867. pm_runtime_disable(&pdev->dev);
  868. regmap_update_bits(arizona->regmap,
  869. ARIZONA_MICD_CLAMP_CONTROL,
  870. ARIZONA_MICD_CLAMP_MODE_MASK, 0);
  871. if (arizona->pdata.jd_gpio5) {
  872. jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
  873. jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
  874. } else {
  875. jack_irq_rise = ARIZONA_IRQ_JD_RISE;
  876. jack_irq_fall = ARIZONA_IRQ_JD_FALL;
  877. }
  878. arizona_set_irq_wake(arizona, jack_irq_rise, 0);
  879. arizona_set_irq_wake(arizona, jack_irq_fall, 0);
  880. arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
  881. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  882. arizona_free_irq(arizona, jack_irq_rise, info);
  883. arizona_free_irq(arizona, jack_irq_fall, info);
  884. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  885. ARIZONA_JD1_ENA, 0);
  886. arizona_clk32k_disable(arizona);
  887. extcon_dev_unregister(&info->edev);
  888. return 0;
  889. }
  890. static struct platform_driver arizona_extcon_driver = {
  891. .driver = {
  892. .name = "arizona-extcon",
  893. .owner = THIS_MODULE,
  894. },
  895. .probe = arizona_extcon_probe,
  896. .remove = arizona_extcon_remove,
  897. };
  898. module_platform_driver(arizona_extcon_driver);
  899. MODULE_DESCRIPTION("Arizona Extcon driver");
  900. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  901. MODULE_LICENSE("GPL");
  902. MODULE_ALIAS("platform:extcon-arizona");