extcon-arizona.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. return IRQ_NONE;
  141. }
  142. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  143. if (!(val & ARIZONA_MICD_VALID)) {
  144. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  145. mutex_unlock(&info->lock);
  146. return IRQ_NONE;
  147. }
  148. /* Due to jack detect this should never happen */
  149. if (!(val & ARIZONA_MICD_STS)) {
  150. dev_warn(arizona->dev, "Detected open circuit\n");
  151. info->detecting = false;
  152. goto handled;
  153. }
  154. /* If we got a high impedence we should have a headset, report it. */
  155. if (info->detecting && (val & 0x400)) {
  156. ret = extcon_update_state(&info->edev,
  157. 1 << ARIZONA_CABLE_MICROPHONE |
  158. 1 << ARIZONA_CABLE_HEADPHONE,
  159. 1 << ARIZONA_CABLE_MICROPHONE |
  160. 1 << ARIZONA_CABLE_HEADPHONE);
  161. if (ret != 0)
  162. dev_err(arizona->dev, "Headset report failed: %d\n",
  163. ret);
  164. info->mic = true;
  165. info->detecting = false;
  166. goto handled;
  167. }
  168. /* If we detected a lower impedence during initial startup
  169. * then we probably have the wrong polarity, flip it. Don't
  170. * do this for the lowest impedences to speed up detection of
  171. * plain headphones. If both polarities report a low
  172. * impedence then give up and report headphones.
  173. */
  174. if (info->detecting && (val & 0x3f8)) {
  175. info->jack_flips++;
  176. if (info->jack_flips >= info->micd_num_modes) {
  177. dev_dbg(arizona->dev, "Detected headphone\n");
  178. info->detecting = false;
  179. arizona_stop_mic(info);
  180. ret = extcon_set_cable_state_(&info->edev,
  181. ARIZONA_CABLE_HEADPHONE,
  182. true);
  183. if (ret != 0)
  184. dev_err(arizona->dev,
  185. "Headphone report failed: %d\n",
  186. ret);
  187. } else {
  188. info->micd_mode++;
  189. if (info->micd_mode == info->micd_num_modes)
  190. info->micd_mode = 0;
  191. arizona_extcon_set_mode(info, info->micd_mode);
  192. info->jack_flips++;
  193. }
  194. goto handled;
  195. }
  196. /*
  197. * If we're still detecting and we detect a short then we've
  198. * got a headphone. Otherwise it's a button press.
  199. */
  200. if (val & 0x3fc) {
  201. if (info->mic) {
  202. dev_dbg(arizona->dev, "Mic button detected\n");
  203. lvl = val & ARIZONA_MICD_LVL_MASK;
  204. lvl >>= ARIZONA_MICD_LVL_SHIFT;
  205. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  206. if (lvl & arizona_lvl_to_key[i].status)
  207. input_report_key(info->input,
  208. arizona_lvl_to_key[i].report,
  209. 1);
  210. input_sync(info->input);
  211. } else if (info->detecting) {
  212. dev_dbg(arizona->dev, "Headphone detected\n");
  213. info->detecting = false;
  214. arizona_stop_mic(info);
  215. ret = extcon_set_cable_state_(&info->edev,
  216. ARIZONA_CABLE_HEADPHONE,
  217. true);
  218. if (ret != 0)
  219. dev_err(arizona->dev,
  220. "Headphone report failed: %d\n",
  221. ret);
  222. } else {
  223. dev_warn(arizona->dev, "Button with no mic: %x\n",
  224. val);
  225. }
  226. } else {
  227. dev_dbg(arizona->dev, "Mic button released\n");
  228. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  229. input_report_key(info->input,
  230. arizona_lvl_to_key[i].report, 0);
  231. input_sync(info->input);
  232. }
  233. handled:
  234. pm_runtime_mark_last_busy(info->dev);
  235. mutex_unlock(&info->lock);
  236. return IRQ_HANDLED;
  237. }
  238. static irqreturn_t arizona_jackdet(int irq, void *data)
  239. {
  240. struct arizona_extcon_info *info = data;
  241. struct arizona *arizona = info->arizona;
  242. unsigned int val;
  243. int ret, i;
  244. pm_runtime_get_sync(info->dev);
  245. mutex_lock(&info->lock);
  246. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  247. if (ret != 0) {
  248. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  249. ret);
  250. mutex_unlock(&info->lock);
  251. pm_runtime_put_autosuspend(info->dev);
  252. return IRQ_NONE;
  253. }
  254. if (val & ARIZONA_JD1_STS) {
  255. dev_dbg(arizona->dev, "Detected jack\n");
  256. ret = extcon_set_cable_state_(&info->edev,
  257. ARIZONA_CABLE_MECHANICAL, true);
  258. if (ret != 0)
  259. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  260. ret);
  261. arizona_start_mic(info);
  262. } else {
  263. dev_dbg(arizona->dev, "Detected jack removal\n");
  264. arizona_stop_mic(info);
  265. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  266. input_report_key(info->input,
  267. arizona_lvl_to_key[i].report, 0);
  268. input_sync(info->input);
  269. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  270. if (ret != 0)
  271. dev_err(arizona->dev, "Removal report failed: %d\n",
  272. ret);
  273. }
  274. mutex_unlock(&info->lock);
  275. pm_runtime_mark_last_busy(info->dev);
  276. pm_runtime_put_autosuspend(info->dev);
  277. return IRQ_HANDLED;
  278. }
  279. static int __devinit arizona_extcon_probe(struct platform_device *pdev)
  280. {
  281. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  282. struct arizona_pdata *pdata;
  283. struct arizona_extcon_info *info;
  284. int ret, mode, i;
  285. pdata = dev_get_platdata(arizona->dev);
  286. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  287. if (!info) {
  288. dev_err(&pdev->dev, "Failed to allocate memory\n");
  289. ret = -ENOMEM;
  290. goto err;
  291. }
  292. info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
  293. if (IS_ERR(info->micvdd)) {
  294. ret = PTR_ERR(info->micvdd);
  295. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  296. goto err;
  297. }
  298. mutex_init(&info->lock);
  299. info->arizona = arizona;
  300. info->dev = &pdev->dev;
  301. info->detecting = true;
  302. platform_set_drvdata(pdev, info);
  303. switch (arizona->type) {
  304. case WM5102:
  305. switch (arizona->rev) {
  306. case 0:
  307. info->micd_reva = true;
  308. break;
  309. default:
  310. break;
  311. }
  312. break;
  313. default:
  314. break;
  315. }
  316. info->edev.name = "Headset Jack";
  317. info->edev.supported_cable = arizona_cable;
  318. ret = extcon_dev_register(&info->edev, arizona->dev);
  319. if (ret < 0) {
  320. dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
  321. ret);
  322. goto err;
  323. }
  324. if (pdata->num_micd_configs) {
  325. info->micd_modes = pdata->micd_configs;
  326. info->micd_num_modes = pdata->num_micd_configs;
  327. } else {
  328. info->micd_modes = micd_default_modes;
  329. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  330. }
  331. if (arizona->pdata.micd_pol_gpio > 0) {
  332. if (info->micd_modes[0].gpio)
  333. mode = GPIOF_OUT_INIT_HIGH;
  334. else
  335. mode = GPIOF_OUT_INIT_LOW;
  336. ret = devm_gpio_request_one(&pdev->dev,
  337. arizona->pdata.micd_pol_gpio,
  338. mode,
  339. "MICD polarity");
  340. if (ret != 0) {
  341. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  342. arizona->pdata.micd_pol_gpio, ret);
  343. goto err_register;
  344. }
  345. }
  346. arizona_extcon_set_mode(info, 0);
  347. info->input = input_allocate_device();
  348. if (!info->input) {
  349. dev_err(arizona->dev, "Can't allocate input dev\n");
  350. ret = -ENOMEM;
  351. goto err_register;
  352. }
  353. for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
  354. input_set_capability(info->input, EV_KEY,
  355. arizona_lvl_to_key[i].report);
  356. info->input->name = "Headset";
  357. info->input->phys = "arizona/extcon";
  358. info->input->dev.parent = &pdev->dev;
  359. pm_runtime_enable(&pdev->dev);
  360. pm_runtime_idle(&pdev->dev);
  361. pm_runtime_get_sync(&pdev->dev);
  362. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_RISE,
  363. "JACKDET rise", arizona_jackdet, info);
  364. if (ret != 0) {
  365. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  366. ret);
  367. goto err_input;
  368. }
  369. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1);
  370. if (ret != 0) {
  371. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  372. ret);
  373. goto err_rise;
  374. }
  375. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_FALL,
  376. "JACKDET fall", arizona_jackdet, info);
  377. if (ret != 0) {
  378. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  379. goto err_rise_wake;
  380. }
  381. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 1);
  382. if (ret != 0) {
  383. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  384. ret);
  385. goto err_fall;
  386. }
  387. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  388. "MICDET", arizona_micdet, info);
  389. if (ret != 0) {
  390. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  391. goto err_fall_wake;
  392. }
  393. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  394. ARIZONA_MICD_BIAS_STARTTIME_MASK |
  395. ARIZONA_MICD_RATE_MASK,
  396. 7 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT |
  397. 8 << ARIZONA_MICD_RATE_SHIFT);
  398. arizona_clk32k_enable(arizona);
  399. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  400. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  401. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  402. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  403. ret = regulator_allow_bypass(info->micvdd, true);
  404. if (ret != 0)
  405. dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
  406. ret);
  407. pm_runtime_put(&pdev->dev);
  408. ret = input_register_device(info->input);
  409. if (ret) {
  410. dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
  411. goto err_micdet;
  412. }
  413. return 0;
  414. err_micdet:
  415. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  416. err_fall_wake:
  417. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  418. err_fall:
  419. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  420. err_rise_wake:
  421. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  422. err_rise:
  423. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  424. err_input:
  425. input_free_device(info->input);
  426. err_register:
  427. pm_runtime_disable(&pdev->dev);
  428. extcon_dev_unregister(&info->edev);
  429. err:
  430. return ret;
  431. }
  432. static int __devexit arizona_extcon_remove(struct platform_device *pdev)
  433. {
  434. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  435. struct arizona *arizona = info->arizona;
  436. pm_runtime_disable(&pdev->dev);
  437. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  438. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  439. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  440. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  441. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  442. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  443. ARIZONA_JD1_ENA, 0);
  444. arizona_clk32k_disable(arizona);
  445. input_unregister_device(info->input);
  446. extcon_dev_unregister(&info->edev);
  447. return 0;
  448. }
  449. static struct platform_driver arizona_extcon_driver = {
  450. .driver = {
  451. .name = "arizona-extcon",
  452. .owner = THIS_MODULE,
  453. },
  454. .probe = arizona_extcon_probe,
  455. .remove = __devexit_p(arizona_extcon_remove),
  456. };
  457. module_platform_driver(arizona_extcon_driver);
  458. MODULE_DESCRIPTION("Arizona Extcon driver");
  459. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  460. MODULE_LICENSE("GPL");
  461. MODULE_ALIAS("platform:extcon-arizona");