bluetooth-power.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* Copyright (c) 2009-2010, 2013 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /*
  13. * Bluetooth Power Switch Module
  14. * controls power to external Bluetooth device
  15. * with interface to power management device
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/rfkill.h>
  22. #include <linux/gpio.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/delay.h>
  25. #include <linux/bluetooth-power.h>
  26. #include <linux/slab.h>
  27. #include <linux/regulator/consumer.h>
  28. #define BT_PWR_DBG(fmt, arg...) pr_debug("%s: " fmt "\n" , __func__ , ## arg)
  29. #define BT_PWR_INFO(fmt, arg...) pr_info("%s: " fmt "\n" , __func__ , ## arg)
  30. #define BT_PWR_ERR(fmt, arg...) pr_err("%s: " fmt "\n" , __func__ , ## arg)
  31. #define BT_VDD_PA_CURRENT 60000
  32. static struct of_device_id bt_power_match_table[] = {
  33. { .compatible = "qca,ar3002" },
  34. {}
  35. };
  36. static struct bluetooth_power_platform_data *bt_power_pdata;
  37. static struct platform_device *btpdev;
  38. static bool previous;
  39. static int bt_vreg_init(struct bt_power_vreg_data *vreg)
  40. {
  41. int rc = 0;
  42. struct device *dev = &btpdev->dev;
  43. BT_PWR_DBG("vreg_get for : %s", vreg->name);
  44. /* Get the regulator handle */
  45. vreg->reg = regulator_get(dev, vreg->name);
  46. if (IS_ERR(vreg->reg)) {
  47. rc = PTR_ERR(vreg->reg);
  48. pr_err("%s: regulator_get(%s) failed. rc=%d\n",
  49. __func__, vreg->name, rc);
  50. goto out;
  51. }
  52. if ((regulator_count_voltages(vreg->reg) > 0)
  53. && (vreg->low_vol_level) && (vreg->high_vol_level))
  54. vreg->set_voltage_sup = 1;
  55. out:
  56. return rc;
  57. }
  58. static int bt_vreg_enable(struct bt_power_vreg_data *vreg)
  59. {
  60. int rc = 0;
  61. BT_PWR_DBG("vreg_en for : %s", vreg->name);
  62. if (!vreg->is_enabled) {
  63. if (vreg->set_voltage_sup) {
  64. rc = regulator_set_voltage(vreg->reg,
  65. vreg->low_vol_level,
  66. vreg->high_vol_level);
  67. if (rc < 0) {
  68. BT_PWR_ERR("vreg_set_vol(%s) failed rc=%d\n",
  69. vreg->name, rc);
  70. goto out;
  71. }
  72. }
  73. rc = regulator_enable(vreg->reg);
  74. if (rc < 0) {
  75. BT_PWR_ERR("regulator_enable(%s) failed. rc=%d\n",
  76. vreg->name, rc);
  77. goto out;
  78. }
  79. vreg->is_enabled = true;
  80. }
  81. out:
  82. return rc;
  83. }
  84. static int bt_vreg_disable(struct bt_power_vreg_data *vreg)
  85. {
  86. int rc = 0;
  87. if (!vreg)
  88. return rc;
  89. BT_PWR_DBG("vreg_disable for : %s", vreg->name);
  90. if (vreg->is_enabled) {
  91. rc = regulator_disable(vreg->reg);
  92. if (rc < 0) {
  93. BT_PWR_ERR("regulator_disable(%s) failed. rc=%d\n",
  94. vreg->name, rc);
  95. goto out;
  96. }
  97. vreg->is_enabled = false;
  98. if (vreg->set_voltage_sup) {
  99. /* Set the min voltage to 0 */
  100. rc = regulator_set_voltage(vreg->reg,
  101. 0,
  102. vreg->high_vol_level);
  103. if (rc < 0) {
  104. BT_PWR_ERR("vreg_set_vol(%s) failed rc=%d\n",
  105. vreg->name, rc);
  106. goto out;
  107. }
  108. }
  109. }
  110. out:
  111. return rc;
  112. }
  113. static int bt_configure_vreg(struct bt_power_vreg_data *vreg)
  114. {
  115. int rc = 0;
  116. BT_PWR_DBG("config %s", vreg->name);
  117. /* Get the regulator handle for vreg */
  118. if (!(vreg->reg)) {
  119. rc = bt_vreg_init(vreg);
  120. if (rc < 0)
  121. return rc;
  122. }
  123. rc = bt_vreg_enable(vreg);
  124. return rc;
  125. }
  126. static int bt_configure_gpios(int on)
  127. {
  128. int rc = 0;
  129. int bt_reset_gpio = bt_power_pdata->bt_gpio_sys_rst;
  130. BT_PWR_DBG("%s bt_gpio= %d on: %d", __func__, bt_reset_gpio, on);
  131. if (on) {
  132. rc = gpio_request(bt_reset_gpio, "bt_sys_rst_n");
  133. if (rc) {
  134. BT_PWR_ERR("unable to request gpio %d (%d)\n",
  135. bt_reset_gpio, rc);
  136. return rc;
  137. }
  138. rc = gpio_direction_output(bt_reset_gpio, 0);
  139. if (rc) {
  140. BT_PWR_ERR("Unable to set direction\n");
  141. return rc;
  142. }
  143. rc = gpio_direction_output(bt_reset_gpio, 1);
  144. if (rc) {
  145. BT_PWR_ERR("Unable to set direction\n");
  146. return rc;
  147. }
  148. msleep(100);
  149. } else {
  150. gpio_set_value(bt_reset_gpio, 0);
  151. rc = gpio_direction_input(bt_reset_gpio);
  152. if (rc)
  153. BT_PWR_ERR("Unable to set direction\n");
  154. msleep(100);
  155. }
  156. return rc;
  157. }
  158. static int bluetooth_power(int on)
  159. {
  160. int rc = 0;
  161. BT_PWR_DBG("on: %d", on);
  162. if (on) {
  163. if (bt_power_pdata->bt_vdd_io) {
  164. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_io);
  165. if (rc < 0) {
  166. BT_PWR_ERR("bt_power vddio config failed");
  167. goto out;
  168. }
  169. }
  170. if (bt_power_pdata->bt_vdd_ldo) {
  171. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_ldo);
  172. if (rc < 0) {
  173. BT_PWR_ERR("bt_power vddldo config failed");
  174. goto vdd_ldo_fail;
  175. }
  176. }
  177. if (bt_power_pdata->bt_vdd_pa) {
  178. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_pa);
  179. if (rc < 0) {
  180. BT_PWR_ERR("bt_power vddpa config failed");
  181. goto vdd_pa_fail;
  182. }
  183. regulator_set_optimum_mode(
  184. bt_power_pdata->bt_vdd_pa->reg,
  185. BT_VDD_PA_CURRENT);
  186. }
  187. if (bt_power_pdata->bt_chip_pwd) {
  188. rc = bt_configure_vreg(bt_power_pdata->bt_chip_pwd);
  189. if (rc < 0) {
  190. BT_PWR_ERR("bt_power vddldo config failed");
  191. goto chip_pwd_fail;
  192. }
  193. }
  194. if (bt_power_pdata->bt_gpio_sys_rst) {
  195. rc = bt_configure_gpios(on);
  196. if (rc < 0) {
  197. BT_PWR_ERR("bt_power gpio config failed");
  198. goto gpio_fail;
  199. }
  200. }
  201. } else {
  202. bt_configure_gpios(on);
  203. gpio_fail:
  204. if (bt_power_pdata->bt_gpio_sys_rst)
  205. gpio_free(bt_power_pdata->bt_gpio_sys_rst);
  206. bt_vreg_disable(bt_power_pdata->bt_chip_pwd);
  207. chip_pwd_fail:
  208. bt_vreg_disable(bt_power_pdata->bt_vdd_pa);
  209. vdd_pa_fail:
  210. bt_vreg_disable(bt_power_pdata->bt_vdd_ldo);
  211. vdd_ldo_fail:
  212. bt_vreg_disable(bt_power_pdata->bt_vdd_io);
  213. }
  214. out:
  215. return rc;
  216. }
  217. static int bluetooth_toggle_radio(void *data, bool blocked)
  218. {
  219. int ret = 0;
  220. int (*power_control)(int enable);
  221. power_control =
  222. ((struct bluetooth_power_platform_data *)data)->bt_power_setup;
  223. if (previous != blocked)
  224. ret = (*power_control)(!blocked);
  225. if (!ret)
  226. previous = blocked;
  227. return ret;
  228. }
  229. static const struct rfkill_ops bluetooth_power_rfkill_ops = {
  230. .set_block = bluetooth_toggle_radio,
  231. };
  232. static int bluetooth_power_rfkill_probe(struct platform_device *pdev)
  233. {
  234. struct rfkill *rfkill;
  235. int ret;
  236. rfkill = rfkill_alloc("bt_power", &pdev->dev, RFKILL_TYPE_BLUETOOTH,
  237. &bluetooth_power_rfkill_ops,
  238. pdev->dev.platform_data);
  239. if (!rfkill) {
  240. dev_err(&pdev->dev, "rfkill allocate failed\n");
  241. return -ENOMEM;
  242. }
  243. /* force Bluetooth off during init to allow for user control */
  244. rfkill_init_sw_state(rfkill, 1);
  245. previous = 1;
  246. ret = rfkill_register(rfkill);
  247. if (ret) {
  248. dev_err(&pdev->dev, "rfkill register failed=%d\n", ret);
  249. rfkill_destroy(rfkill);
  250. return ret;
  251. }
  252. platform_set_drvdata(pdev, rfkill);
  253. return 0;
  254. }
  255. static void bluetooth_power_rfkill_remove(struct platform_device *pdev)
  256. {
  257. struct rfkill *rfkill;
  258. dev_dbg(&pdev->dev, "%s\n", __func__);
  259. rfkill = platform_get_drvdata(pdev);
  260. if (rfkill)
  261. rfkill_unregister(rfkill);
  262. rfkill_destroy(rfkill);
  263. platform_set_drvdata(pdev, NULL);
  264. }
  265. #define MAX_PROP_SIZE 32
  266. static int bt_dt_parse_vreg_info(struct device *dev,
  267. struct bt_power_vreg_data **vreg_data, const char *vreg_name)
  268. {
  269. int len, ret = 0;
  270. const __be32 *prop;
  271. char prop_name[MAX_PROP_SIZE];
  272. struct bt_power_vreg_data *vreg;
  273. struct device_node *np = dev->of_node;
  274. BT_PWR_DBG("vreg dev tree parse for %s", vreg_name);
  275. snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
  276. if (of_parse_phandle(np, prop_name, 0)) {
  277. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  278. if (!vreg) {
  279. dev_err(dev, "No memory for vreg: %s\n", vreg_name);
  280. ret = -ENOMEM;
  281. goto err;
  282. }
  283. vreg->name = vreg_name;
  284. snprintf(prop_name, MAX_PROP_SIZE,
  285. "qcom,%s-voltage-level", vreg_name);
  286. prop = of_get_property(np, prop_name, &len);
  287. if (!prop || (len != (2 * sizeof(__be32)))) {
  288. dev_warn(dev, "%s %s property\n",
  289. prop ? "invalid format" : "no", prop_name);
  290. } else {
  291. vreg->low_vol_level = be32_to_cpup(&prop[0]);
  292. vreg->high_vol_level = be32_to_cpup(&prop[1]);
  293. }
  294. *vreg_data = vreg;
  295. BT_PWR_DBG("%s: vol=[%d %d]uV\n",
  296. vreg->name, vreg->low_vol_level,
  297. vreg->high_vol_level);
  298. } else
  299. BT_PWR_INFO("%s: is not provided in device tree", vreg_name);
  300. err:
  301. return ret;
  302. }
  303. static int bt_power_populate_dt_pinfo(struct platform_device *pdev)
  304. {
  305. int rc;
  306. BT_PWR_DBG("");
  307. if (!bt_power_pdata)
  308. return -ENOMEM;
  309. if (pdev->dev.of_node) {
  310. bt_power_pdata->bt_gpio_sys_rst =
  311. of_get_named_gpio(pdev->dev.of_node,
  312. "qca,bt-reset-gpio", 0);
  313. if (bt_power_pdata->bt_gpio_sys_rst < 0) {
  314. BT_PWR_ERR("bt-reset-gpio not provided in device tree");
  315. return bt_power_pdata->bt_gpio_sys_rst;
  316. }
  317. rc = bt_dt_parse_vreg_info(&pdev->dev,
  318. &bt_power_pdata->bt_vdd_io,
  319. "qca,bt-vdd-io");
  320. if (rc < 0)
  321. return rc;
  322. rc = bt_dt_parse_vreg_info(&pdev->dev,
  323. &bt_power_pdata->bt_vdd_pa,
  324. "qca,bt-vdd-pa");
  325. if (rc < 0)
  326. return rc;
  327. rc = bt_dt_parse_vreg_info(&pdev->dev,
  328. &bt_power_pdata->bt_vdd_ldo,
  329. "qca,bt-vdd-ldo");
  330. if (rc < 0)
  331. return rc;
  332. rc = bt_dt_parse_vreg_info(&pdev->dev,
  333. &bt_power_pdata->bt_chip_pwd,
  334. "qca,bt-chip-pwd");
  335. if (rc < 0)
  336. return rc;
  337. }
  338. bt_power_pdata->bt_power_setup = bluetooth_power;
  339. return 0;
  340. }
  341. static int __devinit bt_power_probe(struct platform_device *pdev)
  342. {
  343. int ret = 0;
  344. dev_dbg(&pdev->dev, "%s\n", __func__);
  345. bt_power_pdata =
  346. kzalloc(sizeof(struct bluetooth_power_platform_data),
  347. GFP_KERNEL);
  348. if (!bt_power_pdata) {
  349. BT_PWR_ERR("Failed to allocate memory");
  350. return -ENOMEM;
  351. }
  352. if (pdev->dev.of_node) {
  353. ret = bt_power_populate_dt_pinfo(pdev);
  354. if (ret < 0) {
  355. BT_PWR_ERR("Failed to populate device tree info");
  356. goto free_pdata;
  357. }
  358. pdev->dev.platform_data = bt_power_pdata;
  359. } else if (pdev->dev.platform_data) {
  360. /* Optional data set to default if not provided */
  361. if (!((struct bluetooth_power_platform_data *)
  362. (pdev->dev.platform_data))->bt_power_setup)
  363. ((struct bluetooth_power_platform_data *)
  364. (pdev->dev.platform_data))->bt_power_setup =
  365. bluetooth_power;
  366. memcpy(bt_power_pdata, pdev->dev.platform_data,
  367. sizeof(struct bluetooth_power_platform_data));
  368. } else {
  369. BT_PWR_ERR("Failed to get platform data");
  370. goto free_pdata;
  371. }
  372. if (bluetooth_power_rfkill_probe(pdev) < 0)
  373. goto free_pdata;
  374. btpdev = pdev;
  375. return 0;
  376. free_pdata:
  377. kfree(bt_power_pdata);
  378. return ret;
  379. }
  380. static int __devexit bt_power_remove(struct platform_device *pdev)
  381. {
  382. dev_dbg(&pdev->dev, "%s\n", __func__);
  383. bluetooth_power_rfkill_remove(pdev);
  384. if (bt_power_pdata->bt_chip_pwd->reg)
  385. regulator_put(bt_power_pdata->bt_chip_pwd->reg);
  386. kfree(bt_power_pdata);
  387. return 0;
  388. }
  389. static struct platform_driver bt_power_driver = {
  390. .probe = bt_power_probe,
  391. .remove = __devexit_p(bt_power_remove),
  392. .driver = {
  393. .name = "bt_power",
  394. .owner = THIS_MODULE,
  395. .of_match_table = bt_power_match_table,
  396. },
  397. };
  398. static int __init bluetooth_power_init(void)
  399. {
  400. int ret;
  401. ret = platform_driver_register(&bt_power_driver);
  402. return ret;
  403. }
  404. static void __exit bluetooth_power_exit(void)
  405. {
  406. platform_driver_unregister(&bt_power_driver);
  407. }
  408. MODULE_LICENSE("GPL v2");
  409. MODULE_DESCRIPTION("MSM Bluetooth power control driver");
  410. MODULE_VERSION("1.40");
  411. module_init(bluetooth_power_init);
  412. module_exit(bluetooth_power_exit);