extcon-arizona.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. ret = extcon_set_cable_state_(&info->edev,
  165. ARIZONA_CABLE_HEADPHONE,
  166. true);
  167. if (ret != 0)
  168. dev_err(arizona->dev,
  169. "Headphone report failed: %d\n",
  170. ret);
  171. } else {
  172. info->micd_mode++;
  173. if (info->micd_mode == info->micd_num_modes)
  174. info->micd_mode = 0;
  175. arizona_extcon_set_mode(info, info->micd_mode);
  176. info->jack_flips++;
  177. }
  178. goto handled;
  179. }
  180. /*
  181. * If we're still detecting and we detect a short then we've
  182. * got a headphone. Otherwise it's a button press, the
  183. * button reporting is stubbed out for now.
  184. */
  185. if (val & 0x3fc) {
  186. if (info->mic) {
  187. dev_dbg(arizona->dev, "Mic button detected\n");
  188. } else if (info->detecting) {
  189. dev_dbg(arizona->dev, "Headphone detected\n");
  190. info->detecting = false;
  191. arizona_stop_mic(info);
  192. ret = extcon_set_cable_state_(&info->edev,
  193. ARIZONA_CABLE_HEADPHONE,
  194. true);
  195. if (ret != 0)
  196. dev_err(arizona->dev,
  197. "Headphone report failed: %d\n",
  198. ret);
  199. } else {
  200. dev_warn(arizona->dev, "Button with no mic: %x\n",
  201. val);
  202. }
  203. } else {
  204. dev_dbg(arizona->dev, "Mic button released\n");
  205. }
  206. handled:
  207. pm_runtime_mark_last_busy(info->dev);
  208. mutex_unlock(&info->lock);
  209. return IRQ_HANDLED;
  210. }
  211. static irqreturn_t arizona_jackdet(int irq, void *data)
  212. {
  213. struct arizona_extcon_info *info = data;
  214. struct arizona *arizona = info->arizona;
  215. unsigned int val;
  216. int ret;
  217. pm_runtime_get_sync(info->dev);
  218. mutex_lock(&info->lock);
  219. ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
  220. if (ret != 0) {
  221. dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
  222. ret);
  223. mutex_unlock(&info->lock);
  224. pm_runtime_put_autosuspend(info->dev);
  225. return IRQ_NONE;
  226. }
  227. if (val & ARIZONA_JD1_STS) {
  228. dev_dbg(arizona->dev, "Detected jack\n");
  229. ret = extcon_set_cable_state_(&info->edev,
  230. ARIZONA_CABLE_MECHANICAL, true);
  231. if (ret != 0)
  232. dev_err(arizona->dev, "Mechanical report failed: %d\n",
  233. ret);
  234. arizona_start_mic(info);
  235. } else {
  236. dev_dbg(arizona->dev, "Detected jack removal\n");
  237. arizona_stop_mic(info);
  238. ret = extcon_update_state(&info->edev, 0xffffffff, 0);
  239. if (ret != 0)
  240. dev_err(arizona->dev, "Removal report failed: %d\n",
  241. ret);
  242. }
  243. mutex_unlock(&info->lock);
  244. pm_runtime_mark_last_busy(info->dev);
  245. pm_runtime_put_autosuspend(info->dev);
  246. return IRQ_HANDLED;
  247. }
  248. static int __devinit arizona_extcon_probe(struct platform_device *pdev)
  249. {
  250. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  251. struct arizona_pdata *pdata;
  252. struct arizona_extcon_info *info;
  253. int ret, mode;
  254. pdata = dev_get_platdata(arizona->dev);
  255. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  256. if (!info) {
  257. dev_err(&pdev->dev, "failed to allocate memory\n");
  258. ret = -ENOMEM;
  259. goto err;
  260. }
  261. info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
  262. if (IS_ERR(info->micvdd)) {
  263. ret = PTR_ERR(info->micvdd);
  264. dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
  265. goto err;
  266. }
  267. mutex_init(&info->lock);
  268. info->arizona = arizona;
  269. info->dev = &pdev->dev;
  270. info->detecting = true;
  271. platform_set_drvdata(pdev, info);
  272. switch (arizona->type) {
  273. case WM5102:
  274. switch (arizona->rev) {
  275. case 0:
  276. info->micd_reva = true;
  277. break;
  278. default:
  279. break;
  280. }
  281. break;
  282. default:
  283. break;
  284. }
  285. info->edev.name = "Headset Jack";
  286. info->edev.supported_cable = arizona_cable;
  287. ret = extcon_dev_register(&info->edev, arizona->dev);
  288. if (ret < 0) {
  289. dev_err(arizona->dev, "extcon_dev_regster() failed: %d\n",
  290. ret);
  291. goto err;
  292. }
  293. if (pdata->num_micd_configs) {
  294. info->micd_modes = pdata->micd_configs;
  295. info->micd_num_modes = pdata->num_micd_configs;
  296. } else {
  297. info->micd_modes = micd_default_modes;
  298. info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
  299. }
  300. if (arizona->pdata.micd_pol_gpio > 0) {
  301. if (info->micd_modes[0].gpio)
  302. mode = GPIOF_OUT_INIT_HIGH;
  303. else
  304. mode = GPIOF_OUT_INIT_LOW;
  305. ret = devm_gpio_request_one(&pdev->dev,
  306. arizona->pdata.micd_pol_gpio,
  307. mode,
  308. "MICD polarity");
  309. if (ret != 0) {
  310. dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
  311. arizona->pdata.micd_pol_gpio, ret);
  312. goto err_register;
  313. }
  314. }
  315. arizona_extcon_set_mode(info, 0);
  316. pm_runtime_enable(&pdev->dev);
  317. pm_runtime_idle(&pdev->dev);
  318. pm_runtime_get_sync(&pdev->dev);
  319. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_RISE,
  320. "JACKDET rise", arizona_jackdet, info);
  321. if (ret != 0) {
  322. dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
  323. ret);
  324. goto err_register;
  325. }
  326. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1);
  327. if (ret != 0) {
  328. dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
  329. ret);
  330. goto err_rise;
  331. }
  332. ret = arizona_request_irq(arizona, ARIZONA_IRQ_JD_FALL,
  333. "JACKDET fall", arizona_jackdet, info);
  334. if (ret != 0) {
  335. dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
  336. goto err_rise_wake;
  337. }
  338. ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 1);
  339. if (ret != 0) {
  340. dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
  341. ret);
  342. goto err_fall;
  343. }
  344. ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
  345. "MICDET", arizona_micdet, info);
  346. if (ret != 0) {
  347. dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
  348. goto err_fall_wake;
  349. }
  350. regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
  351. ARIZONA_MICD_BIAS_STARTTIME_MASK |
  352. ARIZONA_MICD_RATE_MASK,
  353. 7 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT |
  354. 8 << ARIZONA_MICD_RATE_SHIFT);
  355. arizona_clk32k_enable(arizona);
  356. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
  357. ARIZONA_JD1_DB, ARIZONA_JD1_DB);
  358. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  359. ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
  360. pm_runtime_put(&pdev->dev);
  361. return 0;
  362. err_fall_wake:
  363. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  364. err_fall:
  365. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  366. err_rise_wake:
  367. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  368. err_rise:
  369. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  370. err_register:
  371. pm_runtime_disable(&pdev->dev);
  372. extcon_dev_unregister(&info->edev);
  373. err:
  374. return ret;
  375. }
  376. static int __devexit arizona_extcon_remove(struct platform_device *pdev)
  377. {
  378. struct arizona_extcon_info *info = platform_get_drvdata(pdev);
  379. struct arizona *arizona = info->arizona;
  380. pm_runtime_disable(&pdev->dev);
  381. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
  382. arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
  383. arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
  384. arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
  385. arizona_free_irq(arizona, ARIZONA_IRQ_JD_FALL, info);
  386. regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
  387. ARIZONA_JD1_ENA, 0);
  388. arizona_clk32k_disable(arizona);
  389. extcon_dev_unregister(&info->edev);
  390. return 0;
  391. }
  392. static struct platform_driver arizona_extcon_driver = {
  393. .driver = {
  394. .name = "arizona-extcon",
  395. .owner = THIS_MODULE,
  396. },
  397. .probe = arizona_extcon_probe,
  398. .remove = __devexit_p(arizona_extcon_remove),
  399. };
  400. module_platform_driver(arizona_extcon_driver);
  401. MODULE_DESCRIPTION("Arizona Extcon driver");
  402. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  403. MODULE_LICENSE("GPL");
  404. MODULE_ALIAS("platform:extcon-arizona");