extcon-arizona.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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_NUM_BUTTONS 6
  32. struct arizona_extcon_info {
  33. struct device *dev;
  34. struct arizona *arizona;
  35. struct mutex lock;
  36. struct regulator *micvdd;
  37. struct input_dev *input;
  38. int micd_mode;
  39. const struct arizona_micd_config *micd_modes;
  40. int micd_num_modes;
  41. bool micd_reva;
  42. bool mic;
  43. bool detecting;
  44. int jack_flips;
  45. struct extcon_dev edev;
  46. };
  47. static const struct arizona_micd_config micd_default_modes[] = {
  48. { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
  49. { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
  50. };
  51. static struct {
  52. u16 status;
  53. int report;
  54. } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
  55. { 0x1, BTN_0 },
  56. { 0x2, BTN_1 },
  57. { 0x4, BTN_2 },
  58. { 0x8, BTN_3 },
  59. { 0x10, BTN_4 },
  60. { 0x20, BTN_5 },
  61. };
  62. #define ARIZONA_CABLE_MECHANICAL 0
  63. #define ARIZONA_CABLE_MICROPHONE 1
  64. #define ARIZONA_CABLE_HEADPHONE 2
  65. static const char *arizona_cable[] = {
  66. "Mechanical",
  67. "Microphone",
  68. "Headphone",
  69. NULL,
  70. };
  71. static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
  72. {
  73. struct arizona *arizona = info->arizona;
  74. gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
  75. info->micd_modes[mode].gpio);
  76. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  77. ARIZONA_MICD_BIAS_SRC_MASK,
  78. info->micd_modes[mode].bias);
  79. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  80. ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
  81. info->micd_mode = mode;
  82. dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
  83. }
  84. static void arizona_start_mic(struct arizona_extcon_info *info)
  85. {
  86. struct arizona *arizona = info->arizona;
  87. bool change;
  88. int ret;
  89. info->detecting = true;
  90. info->mic = false;
  91. info->jack_flips = 0;
  92. /* Microphone detection can't use idle mode */
  93. pm_runtime_get(info->dev);
  94. ret = regulator_enable(info->micvdd);
  95. if (ret != 0) {
  96. dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
  97. ret);
  98. }
  99. if (info->micd_reva) {
  100. regmap_write(arizona->regmap, 0x80, 0x3);
  101. regmap_write(arizona->regmap, 0x294, 0);
  102. regmap_write(arizona->regmap, 0x80, 0x0);
  103. }
  104. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  105. ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
  106. &change);
  107. if (!change) {
  108. regulator_disable(info->micvdd);
  109. pm_runtime_put_autosuspend(info->dev);
  110. }
  111. }
  112. static void arizona_stop_mic(struct arizona_extcon_info *info)
  113. {
  114. struct arizona *arizona = info->arizona;
  115. bool change;
  116. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  117. ARIZONA_MICD_ENA, 0,
  118. &change);
  119. if (info->micd_reva) {
  120. regmap_write(arizona->regmap, 0x80, 0x3);
  121. regmap_write(arizona->regmap, 0x294, 2);
  122. regmap_write(arizona->regmap, 0x80, 0x0);
  123. }
  124. if (change) {
  125. regulator_disable(info->micvdd);
  126. pm_runtime_mark_last_busy(info->dev);
  127. pm_runtime_put_autosuspend(info->dev);
  128. }
  129. }
  130. static irqreturn_t arizona_micdet(int irq, void *data)
  131. {
  132. struct arizona_extcon_info *info = data;
  133. struct arizona *arizona = info->arizona;
  134. unsigned int val, lvl;
  135. int ret, i;
  136. mutex_lock(&info->lock);
  137. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  138. if (ret != 0) {
  139. dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
  140. mutex_unlock(&info->lock);
  141. return IRQ_NONE;
  142. }
  143. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  144. if (!(val & ARIZONA_MICD_VALID)) {
  145. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  146. mutex_unlock(&info->lock);
  147. return IRQ_NONE;
  148. }
  149. /* Due to jack detect this should never happen */
  150. if (!(val & ARIZONA_MICD_STS)) {
  151. dev_warn(arizona->dev, "Detected open circuit\n");
  152. info->detecting = false;
  153. goto handled;
  154. }
  155. /* If we got a high impedence we should have a headset, report it. */
  156. if (info->detecting && (val & 0x400)) {
  157. ret = extcon_update_state(&info->edev,
  158. 1 << ARIZONA_CABLE_MICROPHONE |
  159. 1 << ARIZONA_CABLE_HEADPHONE,
  160. 1 << ARIZONA_CABLE_MICROPHONE |
  161. 1 << ARIZONA_CABLE_HEADPHONE);
  162. if (ret != 0)
  163. dev_err(arizona->dev, "Headset report failed: %d\n",
  164. ret);
  165. info->mic = true;
  166. info->detecting = false;
  167. goto handled;
  168. }
  169. /* If we detected a lower impedence during initial startup
  170. * then we probably have the wrong polarity, flip it. Don't
  171. * do this for the lowest impedences to speed up detection of
  172. * plain headphones. If both polarities report a low
  173. * impedence then give up and report headphones.
  174. */
  175. if (info->detecting && (val & 0x3f8)) {
  176. info->jack_flips++;
  177. if (info->jack_flips >= info->micd_num_modes) {
  178. dev_dbg(arizona->dev, "Detected headphone\n");
  179. info->detecting = false;
  180. arizona_stop_mic(info);
  181. ret = extcon_set_cable_state_(&info->edev,
  182. ARIZONA_CABLE_HEADPHONE,
  183. true);
  184. if (ret != 0)
  185. dev_err(arizona->dev,
  186. "Headphone report failed: %d\n",
  187. ret);
  188. } else {
  189. info->micd_mode++;
  190. if (info->micd_mode == info->micd_num_modes)
  191. info->micd_mode = 0;
  192. arizona_extcon_set_mode(info, info->micd_mode);
  193. info->jack_flips++;
  194. }
  195. goto handled;
  196. }
  197. /*
  198. * If we're still detecting and we detect a short then we've
  199. * got a headphone. Otherwise it's a button press.
  200. */
  201. if (val & 0x3fc) {
  202. if (info->mic) {
  203. dev_dbg(arizona->dev, "Mic button detected\n");
  204. lvl = val & ARIZONA_MICD_LVL_MASK;
  205. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  206. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  207. if (lvl & arizona_lvl_to_key[i].status)
  208. input_report_key(info->input,
  209. arizona_lvl_to_key[i].report,
  210. 1);
  211. input_sync(info->input);
  212. } else if (info->detecting) {
  213. dev_dbg(arizona->dev, "Headphone detected\n");
  214. info->detecting = false;
  215. arizona_stop_mic(info);
  216. ret = extcon_set_cable_state_(&info->edev,
  217. ARIZONA_CABLE_HEADPHONE,
  218. true);
  219. if (ret != 0)
  220. dev_err(arizona->dev,
  221. "Headphone report failed: %d\n",
  222. ret);
  223. } else {
  224. dev_warn(arizona->dev, "Button with no mic: %x\n",
  225. val);
  226. }
  227. } else {
  228. dev_dbg(arizona->dev, "Mic button released\n");
  229. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  230. input_report_key(info->input,
  231. arizona_lvl_to_key[i].report, 0);
  232. input_sync(info->input);
  233. }
  234. handled:
  235. pm_runtime_mark_last_busy(info->dev);
  236. mutex_unlock(&info->lock);
  237. return IRQ_HANDLED;
  238. }
  239. static irqreturn_t arizona_jackdet(int irq, void *data)
  240. {
  241. struct arizona_extcon_info *info = data;
  242. struct arizona *arizona = info->arizona;
  243. unsigned int val;
  244. int ret, i;
  245. pm_runtime_get_sync(info->dev);
  246. mutex_lock(&info->lock);
  247. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  248. if (ret != 0) {
  249. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  250. ret);
  251. mutex_unlock(&info->lock);
  252. pm_runtime_put_autosuspend(info->dev);
  253. return IRQ_NONE;
  254. }
  255. if (val & ARIZONA_JD1_STS) {
  256. dev_dbg(arizona->dev, "Detected jack\n");
  257. ret = extcon_set_cable_state_(&info->edev,
  258. ARIZONA_CABLE_MECHANICAL, true);
  259. if (ret != 0)
  260. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  261. ret);
  262. arizona_start_mic(info);
  263. } else {
  264. dev_dbg(arizona->dev, "Detected jack removal\n");
  265. arizona_stop_mic(info);
  266. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  267. input_report_key(info->input,
  268. arizona_lvl_to_key[i].report, 0);
  269. input_sync(info->input);
  270. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  271. if (ret != 0)
  272. dev_err(arizona->dev, "Removal report failed: %d\n",
  273. ret);
  274. }
  275. mutex_unlock(&info->lock);
  276. pm_runtime_mark_last_busy(info->dev);
  277. pm_runtime_put_autosuspend(info->dev);
  278. return IRQ_HANDLED;
  279. }
  280. static int arizona_extcon_probe(struct platform_device *pdev)
  281. {
  282. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  283. struct arizona_pdata *pdata;
  284. struct arizona_extcon_info *info;
  285. int ret, mode, i;
  286. pdata = dev_get_platdata(arizona->dev);
  287. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  288. if (!info) {
  289. dev_err(&pdev->dev, "Failed to allocate memory\n");
  290. ret = -ENOMEM;
  291. goto err;
  292. }
  293. info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
  294. if (IS_ERR(info->micvdd)) {
  295. ret = PTR_ERR(info->micvdd);
  296. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  297. goto err;
  298. }
  299. mutex_init(&info->lock);
  300. info->arizona = arizona;
  301. info->dev = &pdev->dev;
  302. info->detecting = true;
  303. platform_set_drvdata(pdev, info);
  304. switch (arizona->type) {
  305. case WM5102:
  306. switch (arizona->rev) {
  307. case 0:
  308. info->micd_reva = true;
  309. break;
  310. default:
  311. break;
  312. }
  313. break;
  314. default:
  315. break;
  316. }
  317. info->edev.name = "Headset Jack";
  318. info->edev.supported_cable = arizona_cable;
  319. ret = extcon_dev_register(&info->edev, arizona->dev);
  320. if (ret < 0) {
  321. dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
  322. ret);
  323. goto err;
  324. }
  325. if (pdata->num_micd_configs) {
  326. info->micd_modes = pdata->micd_configs;
  327. info->micd_num_modes = pdata->num_micd_configs;
  328. } else {
  329. info->micd_modes = micd_default_modes;
  330. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  331. }
  332. if (arizona->pdata.micd_pol_gpio > 0) {
  333. if (info->micd_modes[0].gpio)
  334. mode = GPIOF_OUT_INIT_HIGH;
  335. else
  336. mode = GPIOF_OUT_INIT_LOW;
  337. ret = devm_gpio_request_one(&pdev->dev,
  338. arizona->pdata.micd_pol_gpio,
  339. mode,
  340. "MICD polarity");
  341. if (ret != 0) {
  342. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  343. arizona->pdata.micd_pol_gpio, ret);
  344. goto err_register;
  345. }
  346. }
  347. arizona_extcon_set_mode(info, 0);
  348. info->input = input_allocate_device();
  349. if (!info->input) {
  350. dev_err(arizona->dev, "Can't allocate input dev\n");
  351. ret = -ENOMEM;
  352. goto err_register;
  353. }
  354. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  355. input_set_capability(info->input, EV_KEY,
  356. arizona_lvl_to_key[i].report);
  357. info->input->name = "Headset";
  358. info->input->phys = "arizona/extcon";
  359. info->input->dev.parent = &pdev->dev;
  360. pm_runtime_enable(&pdev->dev);
  361. pm_runtime_idle(&pdev->dev);
  362. pm_runtime_get_sync(&pdev->dev);
  363. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_RISE,
  364. "JACKDET rise", arizona_jackdet, info);
  365. if (ret != 0) {
  366. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  367. ret);
  368. goto err_input;
  369. }
  370. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1);
  371. if (ret != 0) {
  372. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  373. ret);
  374. goto err_rise;
  375. }
  376. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_FALL,
  377. "JACKDET fall", arizona_jackdet, info);
  378. if (ret != 0) {
  379. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  380. goto err_rise_wake;
  381. }
  382. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 1);
  383. if (ret != 0) {
  384. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  385. ret);
  386. goto err_fall;
  387. }
  388. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  389. "MICDET", arizona_micdet, info);
  390. if (ret != 0) {
  391. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  392. goto err_fall_wake;
  393. }
  394. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  395. ARIZONA_MICD_BIAS_STARTTIME_MASK |
  396. ARIZONA_MICD_RATE_MASK,
  397. 7 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT |
  398. 8 << ARIZONA_MICD_RATE_SHIFT);
  399. arizona_clk32k_enable(arizona);
  400. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  401. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  402. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  403. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  404. ret = regulator_allow_bypass(info->micvdd, true);
  405. if (ret != 0)
  406. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
  407. ret);
  408. pm_runtime_put(&pdev->dev);
  409. ret = input_register_device(info->input);
  410. if (ret) {
  411. dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
  412. goto err_micdet;
  413. }
  414. return 0;
  415. err_micdet:
  416. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  417. err_fall_wake:
  418. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  419. err_fall:
  420. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  421. err_rise_wake:
  422. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  423. err_rise:
  424. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  425. err_input:
  426. input_free_device(info->input);
  427. err_register:
  428. pm_runtime_disable(&pdev->dev);
  429. extcon_dev_unregister(&info->edev);
  430. err:
  431. return ret;
  432. }
  433. static int arizona_extcon_remove(struct platform_device *pdev)
  434. {
  435. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  436. struct arizona *arizona = info->arizona;
  437. pm_runtime_disable(&pdev->dev);
  438. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  439. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  440. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  441. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  442. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  443. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  444. ARIZONA_JD1_ENA, 0);
  445. arizona_clk32k_disable(arizona);
  446. input_unregister_device(info->input);
  447. extcon_dev_unregister(&info->edev);
  448. return 0;
  449. }
  450. static struct platform_driver arizona_extcon_driver = {
  451. .driver = {
  452. .name = "arizona-extcon",
  453. .owner = THIS_MODULE,
  454. },
  455. .probe = arizona_extcon_probe,
  456. .remove = arizona_extcon_remove,
  457. };
  458. module_platform_driver(arizona_extcon_driver);
  459. MODULE_DESCRIPTION("Arizona Extcon driver");
  460. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  461. MODULE_LICENSE("GPL");
  462. MODULE_ALIAS("platform:extcon-arizona");