extcon-arizona.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 "Mechanical"
  49. #define ARIZONA_CABLE_HEADPHONE "Headphone"
  50. #define ARIZONA_CABLE_HEADSET "Headset"
  51. static const char *arizona_cable[] = {
  52. ARIZONA_CABLE_MECHANICAL,
  53. ARIZONA_CABLE_HEADSET,
  54. ARIZONA_CABLE_HEADPHONE,
  55. NULL,
  56. };
  57. static const u32 arizona_exclusions[] = {
  58. 0x6, /* Headphone and headset */
  59. 0,
  60. };
  61. static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
  62. {
  63. struct arizona *arizona = info->arizona;
  64. gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
  65. info->micd_modes[mode].gpio);
  66. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  67. ARIZONA_MICD_BIAS_SRC_MASK,
  68. info->micd_modes[mode].bias);
  69. regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
  70. ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
  71. info->micd_mode = mode;
  72. dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
  73. }
  74. static void arizona_start_mic(struct arizona_extcon_info *info)
  75. {
  76. struct arizona *arizona = info->arizona;
  77. bool change;
  78. int ret;
  79. info->detecting = true;
  80. info->mic = false;
  81. info->jack_flips = 0;
  82. /* Microphone detection can't use idle mode */
  83. pm_runtime_get(info->dev);
  84. ret = regulator_enable(info->micvdd);
  85. if (ret != 0) {
  86. dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
  87. ret);
  88. }
  89. if (info->micd_reva) {
  90. regmap_write(arizona->regmap, 0x80, 0x3);
  91. regmap_write(arizona->regmap, 0x294, 0);
  92. regmap_write(arizona->regmap, 0x80, 0x0);
  93. }
  94. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  95. ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
  96. &change);
  97. if (!change) {
  98. regulator_disable(info->micvdd);
  99. pm_runtime_put_autosuspend(info->dev);
  100. }
  101. }
  102. static void arizona_stop_mic(struct arizona_extcon_info *info)
  103. {
  104. struct arizona *arizona = info->arizona;
  105. bool change;
  106. regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
  107. ARIZONA_MICD_ENA, 0,
  108. &change);
  109. if (info->micd_reva) {
  110. regmap_write(arizona->regmap, 0x80, 0x3);
  111. regmap_write(arizona->regmap, 0x294, 2);
  112. regmap_write(arizona->regmap, 0x80, 0x0);
  113. }
  114. if (change) {
  115. regulator_disable(info->micvdd);
  116. pm_runtime_put_autosuspend(info->dev);
  117. }
  118. }
  119. static irqreturn_t arizona_micdet(int irq, void *data)
  120. {
  121. struct arizona_extcon_info *info = data;
  122. struct arizona *arizona = info->arizona;
  123. unsigned int val;
  124. int ret;
  125. mutex_lock(&info->lock);
  126. ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
  127. if (ret != 0) {
  128. dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
  129. return IRQ_NONE;
  130. }
  131. dev_dbg(arizona->dev, "MICDET: %x\n", val);
  132. if (!(val & ARIZONA_MICD_VALID)) {
  133. dev_warn(arizona->dev, "Microphone detection state invalid\n");
  134. mutex_unlock(&info->lock);
  135. return IRQ_NONE;
  136. }
  137. /* Due to jack detect this should never happen */
  138. if (!(val & ARIZONA_MICD_STS)) {
  139. dev_warn(arizona->dev, "Detected open circuit\n");
  140. info->detecting = false;
  141. goto handled;
  142. }
  143. /* If we got a high impedence we should have a headset, report it. */
  144. if (info->detecting && (val & 0x400)) {
  145. ret = extcon_set_cable_state(&info->edev,
  146. ARIZONA_CABLE_HEADSET, true);
  147. if (ret != 0)
  148. dev_err(arizona->dev, "Headset report failed: %d\n",
  149. ret);
  150. info->mic = true;
  151. info->detecting = false;
  152. goto handled;
  153. }
  154. /* If we detected a lower impedence during initial startup
  155. * then we probably have the wrong polarity, flip it. Don't
  156. * do this for the lowest impedences to speed up detection of
  157. * plain headphones. If both polarities report a low
  158. * impedence then give up and report headphones.
  159. */
  160. if (info->detecting && (val & 0x3f8)) {
  161. info->jack_flips++;
  162. if (info->jack_flips >= info->micd_num_modes) {
  163. dev_dbg(arizona->dev, "Detected headphone\n");
  164. info->detecting = false;
  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. info->edev.mutually_exclusive = arizona_exclusions;
  289. ret = extcon_dev_register(&info->edev, arizona->dev);
  290. if (ret < 0) {
  291. dev_err(arizona->dev, "extcon_dev_regster() failed: %d\n",
  292. ret);
  293. goto err;
  294. }
  295. if (pdata->num_micd_configs) {
  296. info->micd_modes = pdata->micd_configs;
  297. info->micd_num_modes = pdata->num_micd_configs;
  298. } else {
  299. info->micd_modes = micd_default_modes;
  300. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  301. }
  302. if (arizona->pdata.micd_pol_gpio > 0) {
  303. if (info->micd_modes[0].gpio)
  304. mode = GPIOF_OUT_INIT_HIGH;
  305. else
  306. mode = GPIOF_OUT_INIT_LOW;
  307. ret = devm_gpio_request_one(&pdev->dev,
  308. arizona->pdata.micd_pol_gpio,
  309. mode,
  310. "MICD polarity");
  311. if (ret != 0) {
  312. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  313. arizona->pdata.micd_pol_gpio, ret);
  314. goto err_register;
  315. }
  316. }
  317. arizona_extcon_set_mode(info, 0);
  318. pm_runtime_enable(&pdev->dev);
  319. pm_runtime_idle(&pdev->dev);
  320. pm_runtime_get_sync(&pdev->dev);
  321. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_RISE,
  322. "JACKDET rise", arizona_jackdet, info);
  323. if (ret != 0) {
  324. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  325. ret);
  326. goto err_register;
  327. }
  328. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1);
  329. if (ret != 0) {
  330. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  331. ret);
  332. goto err_rise;
  333. }
  334. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_FALL,
  335. "JACKDET fall", arizona_jackdet, info);
  336. if (ret != 0) {
  337. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  338. goto err_rise_wake;
  339. }
  340. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 1);
  341. if (ret != 0) {
  342. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  343. ret);
  344. goto err_fall;
  345. }
  346. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  347. "MICDET", arizona_micdet, info);
  348. if (ret != 0) {
  349. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  350. goto err_fall_wake;
  351. }
  352. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  353. ARIZONA_MICD_BIAS_STARTTIME_MASK |
  354. ARIZONA_MICD_RATE_MASK,
  355. 7 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT |
  356. 8 << ARIZONA_MICD_RATE_SHIFT);
  357. arizona_clk32k_enable(arizona);
  358. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  359. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  360. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  361. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  362. pm_runtime_put(&pdev->dev);
  363. return 0;
  364. err_fall_wake:
  365. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  366. err_fall:
  367. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  368. err_rise_wake:
  369. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  370. err_rise:
  371. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  372. err_register:
  373. pm_runtime_disable(&pdev->dev);
  374. extcon_dev_unregister(&info->edev);
  375. err:
  376. return ret;
  377. }
  378. static int __devexit arizona_extcon_remove(struct platform_device *pdev)
  379. {
  380. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  381. struct arizona *arizona = info->arizona;
  382. pm_runtime_disable(&pdev->dev);
  383. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  384. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  385. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  386. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  387. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  388. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  389. ARIZONA_JD1_ENA, 0);
  390. arizona_clk32k_disable(arizona);
  391. extcon_dev_unregister(&info->edev);
  392. return 0;
  393. }
  394. static struct platform_driver arizona_extcon_driver = {
  395. .driver = {
  396. .name = "arizona-extcon",
  397. .owner = THIS_MODULE,
  398. },
  399. .probe = arizona_extcon_probe,
  400. .remove = __devexit_p(arizona_extcon_remove),
  401. };
  402. module_platform_driver(arizona_extcon_driver);
  403. MODULE_DESCRIPTION("Arizona Extcon driver");
  404. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  405. MODULE_LICENSE("GPL");
  406. MODULE_ALIAS("platform:extcon-arizona");