extcon-arizona.c 12 KB

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