extcon-arizona.c 14 KB

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