bluetooth-power.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /* Copyright (c) 2009-2010, 2013-2018 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. #include <linux/clk.h>
  29. #if defined(CONFIG_CNSS)
  30. #include <net/cnss.h>
  31. #endif
  32. #include "btfm_slim.h"
  33. #include <linux/fs.h>
  34. #define BT_PWR_DBG(fmt, arg...) pr_debug("%s: " fmt "\n", __func__, ## arg)
  35. #define BT_PWR_INFO(fmt, arg...) pr_info("%s: " fmt "\n", __func__, ## arg)
  36. #define BT_PWR_ERR(fmt, arg...) pr_err("%s: " fmt "\n", __func__, ## arg)
  37. static const struct of_device_id bt_power_match_table[] = {
  38. { .compatible = "qca,ar3002" },
  39. { .compatible = "qca,qca6174" },
  40. { .compatible = "qca,qca9379" },
  41. { .compatible = "qca,wcn3990" },
  42. {}
  43. };
  44. static struct bluetooth_power_platform_data *bt_power_pdata;
  45. static struct platform_device *btpdev;
  46. static bool previous;
  47. static int pwr_state;
  48. struct class *bt_class;
  49. static int bt_major;
  50. static int bt_vreg_init(struct bt_power_vreg_data *vreg)
  51. {
  52. int rc = 0;
  53. struct device *dev = &btpdev->dev;
  54. BT_PWR_DBG("vreg_get for : %s", vreg->name);
  55. /* Get the regulator handle */
  56. vreg->reg = regulator_get(dev, vreg->name);
  57. if (IS_ERR(vreg->reg)) {
  58. rc = PTR_ERR(vreg->reg);
  59. pr_err("%s: regulator_get(%s) failed. rc=%d\n",
  60. __func__, vreg->name, rc);
  61. goto out;
  62. }
  63. if ((regulator_count_voltages(vreg->reg) > 0)
  64. && (vreg->low_vol_level) && (vreg->high_vol_level))
  65. vreg->set_voltage_sup = 1;
  66. out:
  67. return rc;
  68. }
  69. static int bt_vreg_enable(struct bt_power_vreg_data *vreg)
  70. {
  71. int rc = 0;
  72. BT_PWR_DBG("vreg_en for : %s", vreg->name);
  73. if (!vreg->is_enabled) {
  74. if (vreg->set_voltage_sup) {
  75. rc = regulator_set_voltage(vreg->reg,
  76. vreg->low_vol_level,
  77. vreg->high_vol_level);
  78. if (rc < 0) {
  79. BT_PWR_ERR("vreg_set_vol(%s) failed rc=%d\n",
  80. vreg->name, rc);
  81. goto out;
  82. }
  83. }
  84. if (vreg->load_uA >= 0) {
  85. rc = regulator_set_load(vreg->reg,
  86. vreg->load_uA);
  87. if (rc < 0) {
  88. BT_PWR_ERR("vreg_set_mode(%s) failed rc=%d\n",
  89. vreg->name, rc);
  90. goto out;
  91. }
  92. }
  93. rc = regulator_enable(vreg->reg);
  94. if (rc < 0) {
  95. BT_PWR_ERR("regulator_enable(%s) failed. rc=%d\n",
  96. vreg->name, rc);
  97. goto out;
  98. }
  99. vreg->is_enabled = true;
  100. }
  101. out:
  102. return rc;
  103. }
  104. static int bt_vreg_disable(struct bt_power_vreg_data *vreg)
  105. {
  106. int rc = 0;
  107. if (!vreg)
  108. return rc;
  109. BT_PWR_DBG("vreg_disable for : %s", vreg->name);
  110. if (vreg->is_enabled) {
  111. rc = regulator_disable(vreg->reg);
  112. if (rc < 0) {
  113. BT_PWR_ERR("regulator_disable(%s) failed. rc=%d\n",
  114. vreg->name, rc);
  115. goto out;
  116. }
  117. vreg->is_enabled = false;
  118. if (vreg->set_voltage_sup) {
  119. /* Set the min voltage to 0 */
  120. rc = regulator_set_voltage(vreg->reg, 0,
  121. vreg->high_vol_level);
  122. if (rc < 0) {
  123. BT_PWR_ERR("vreg_set_vol(%s) failed rc=%d\n",
  124. vreg->name, rc);
  125. goto out;
  126. }
  127. }
  128. if (vreg->load_uA >= 0) {
  129. rc = regulator_set_load(vreg->reg, 0);
  130. if (rc < 0) {
  131. BT_PWR_ERR("vreg_set_mode(%s) failed rc=%d\n",
  132. vreg->name, rc);
  133. }
  134. }
  135. }
  136. out:
  137. return rc;
  138. }
  139. static int bt_configure_vreg(struct bt_power_vreg_data *vreg)
  140. {
  141. int rc = 0;
  142. BT_PWR_DBG("config %s", vreg->name);
  143. /* Get the regulator handle for vreg */
  144. if (!(vreg->reg)) {
  145. rc = bt_vreg_init(vreg);
  146. if (rc < 0)
  147. return rc;
  148. }
  149. rc = bt_vreg_enable(vreg);
  150. return rc;
  151. }
  152. static int bt_clk_enable(struct bt_power_clk_data *clk)
  153. {
  154. int rc = 0;
  155. BT_PWR_DBG("%s", clk->name);
  156. /* Get the clock handle for vreg */
  157. if (!clk->clk || clk->is_enabled) {
  158. BT_PWR_ERR("error - node: %p, clk->is_enabled:%d",
  159. clk->clk, clk->is_enabled);
  160. return -EINVAL;
  161. }
  162. rc = clk_prepare_enable(clk->clk);
  163. if (rc) {
  164. BT_PWR_ERR("failed to enable %s, rc(%d)\n", clk->name, rc);
  165. return rc;
  166. }
  167. clk->is_enabled = true;
  168. return rc;
  169. }
  170. static int bt_clk_disable(struct bt_power_clk_data *clk)
  171. {
  172. int rc = 0;
  173. BT_PWR_DBG("%s", clk->name);
  174. /* Get the clock handle for vreg */
  175. if (!clk->clk || !clk->is_enabled) {
  176. BT_PWR_ERR("error - node: %p, clk->is_enabled:%d",
  177. clk->clk, clk->is_enabled);
  178. return -EINVAL;
  179. }
  180. clk_disable_unprepare(clk->clk);
  181. clk->is_enabled = false;
  182. return rc;
  183. }
  184. static int bt_configure_gpios(int on)
  185. {
  186. int rc = 0;
  187. int bt_reset_gpio = bt_power_pdata->bt_gpio_sys_rst;
  188. BT_PWR_DBG("bt_gpio= %d on: %d", bt_reset_gpio, on);
  189. if (on) {
  190. rc = gpio_request(bt_reset_gpio, "bt_sys_rst_n");
  191. if (rc) {
  192. BT_PWR_ERR("unable to request gpio %d (%d)\n",
  193. bt_reset_gpio, rc);
  194. return rc;
  195. }
  196. rc = gpio_direction_output(bt_reset_gpio, 0);
  197. if (rc) {
  198. BT_PWR_ERR("Unable to set direction\n");
  199. return rc;
  200. }
  201. msleep(50);
  202. rc = gpio_direction_output(bt_reset_gpio, 1);
  203. if (rc) {
  204. BT_PWR_ERR("Unable to set direction\n");
  205. return rc;
  206. }
  207. msleep(50);
  208. } else {
  209. gpio_set_value(bt_reset_gpio, 0);
  210. msleep(100);
  211. }
  212. return rc;
  213. }
  214. static int bluetooth_power(int on)
  215. {
  216. int rc = 0;
  217. BT_PWR_DBG("on: %d", on);
  218. if (on) {
  219. if (bt_power_pdata->bt_vdd_io) {
  220. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_io);
  221. if (rc < 0) {
  222. BT_PWR_ERR("bt_power vddio config failed");
  223. goto out;
  224. }
  225. }
  226. if (bt_power_pdata->bt_vdd_xtal) {
  227. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_xtal);
  228. if (rc < 0) {
  229. BT_PWR_ERR("bt_power vddxtal config failed");
  230. goto vdd_xtal_fail;
  231. }
  232. }
  233. if (bt_power_pdata->bt_vdd_core) {
  234. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_core);
  235. if (rc < 0) {
  236. BT_PWR_ERR("bt_power vddcore config failed");
  237. goto vdd_core_fail;
  238. }
  239. }
  240. if (bt_power_pdata->bt_vdd_pa) {
  241. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_pa);
  242. if (rc < 0) {
  243. BT_PWR_ERR("bt_power vddpa config failed");
  244. goto vdd_pa_fail;
  245. }
  246. }
  247. if (bt_power_pdata->bt_vdd_ldo) {
  248. rc = bt_configure_vreg(bt_power_pdata->bt_vdd_ldo);
  249. if (rc < 0) {
  250. BT_PWR_ERR("bt_power vddldo config failed");
  251. goto vdd_ldo_fail;
  252. }
  253. }
  254. if (bt_power_pdata->bt_chip_pwd) {
  255. rc = bt_configure_vreg(bt_power_pdata->bt_chip_pwd);
  256. if (rc < 0) {
  257. BT_PWR_ERR("bt_power chippwd config failed");
  258. goto chip_pwd_fail;
  259. }
  260. }
  261. /* Parse dt_info and check if a target requires clock voting.
  262. * Enable BT clock when BT is on and disable it when BT is off
  263. */
  264. if (bt_power_pdata->bt_chip_clk) {
  265. rc = bt_clk_enable(bt_power_pdata->bt_chip_clk);
  266. if (rc < 0) {
  267. BT_PWR_ERR("bt_power gpio config failed");
  268. goto clk_fail;
  269. }
  270. }
  271. if (bt_power_pdata->bt_gpio_sys_rst > 0) {
  272. rc = bt_configure_gpios(on);
  273. if (rc < 0) {
  274. BT_PWR_ERR("bt_power gpio config failed");
  275. goto gpio_fail;
  276. }
  277. }
  278. } else {
  279. if (bt_power_pdata->bt_gpio_sys_rst > 0)
  280. bt_configure_gpios(on);
  281. gpio_fail:
  282. if (bt_power_pdata->bt_gpio_sys_rst > 0)
  283. gpio_free(bt_power_pdata->bt_gpio_sys_rst);
  284. if (bt_power_pdata->bt_chip_clk)
  285. bt_clk_disable(bt_power_pdata->bt_chip_clk);
  286. clk_fail:
  287. if (bt_power_pdata->bt_chip_pwd)
  288. bt_vreg_disable(bt_power_pdata->bt_chip_pwd);
  289. chip_pwd_fail:
  290. if (bt_power_pdata->bt_vdd_ldo)
  291. bt_vreg_disable(bt_power_pdata->bt_vdd_ldo);
  292. vdd_ldo_fail:
  293. if (bt_power_pdata->bt_vdd_pa)
  294. bt_vreg_disable(bt_power_pdata->bt_vdd_pa);
  295. vdd_pa_fail:
  296. if (bt_power_pdata->bt_vdd_core)
  297. bt_vreg_disable(bt_power_pdata->bt_vdd_core);
  298. vdd_core_fail:
  299. if (bt_power_pdata->bt_vdd_xtal)
  300. bt_vreg_disable(bt_power_pdata->bt_vdd_xtal);
  301. vdd_xtal_fail:
  302. if (bt_power_pdata->bt_vdd_io)
  303. bt_vreg_disable(bt_power_pdata->bt_vdd_io);
  304. }
  305. out:
  306. return rc;
  307. }
  308. static int bluetooth_toggle_radio(void *data, bool blocked)
  309. {
  310. int ret = 0;
  311. int (*power_control)(int enable);
  312. power_control =
  313. ((struct bluetooth_power_platform_data *)data)->bt_power_setup;
  314. if (previous != blocked)
  315. ret = (*power_control)(!blocked);
  316. if (!ret)
  317. previous = blocked;
  318. return ret;
  319. }
  320. static const struct rfkill_ops bluetooth_power_rfkill_ops = {
  321. .set_block = bluetooth_toggle_radio,
  322. };
  323. #if defined(CONFIG_CNSS) && defined(CONFIG_CLD_LL_CORE)
  324. static ssize_t enable_extldo(struct device *dev, struct device_attribute *attr,
  325. char *buf)
  326. {
  327. int ret;
  328. bool enable = false;
  329. struct cnss_platform_cap cap;
  330. ret = cnss_get_platform_cap(&cap);
  331. if (ret) {
  332. BT_PWR_ERR("Platform capability info from CNSS not available!");
  333. enable = false;
  334. } else if (!ret && (cap.cap_flag & CNSS_HAS_EXTERNAL_SWREG)) {
  335. enable = true;
  336. }
  337. return snprintf(buf, 6, "%s", (enable ? "true" : "false"));
  338. }
  339. #else
  340. static ssize_t enable_extldo(struct device *dev, struct device_attribute *attr,
  341. char *buf)
  342. {
  343. return snprintf(buf, 6, "%s", "false");
  344. }
  345. #endif
  346. static DEVICE_ATTR(extldo, 0444, enable_extldo, NULL);
  347. static int bluetooth_power_rfkill_probe(struct platform_device *pdev)
  348. {
  349. struct rfkill *rfkill;
  350. int ret;
  351. rfkill = rfkill_alloc("bt_power", &pdev->dev, RFKILL_TYPE_BLUETOOTH,
  352. &bluetooth_power_rfkill_ops,
  353. pdev->dev.platform_data);
  354. if (!rfkill) {
  355. dev_err(&pdev->dev, "rfkill allocate failed\n");
  356. return -ENOMEM;
  357. }
  358. /* add file into rfkill0 to handle LDO27 */
  359. ret = device_create_file(&pdev->dev, &dev_attr_extldo);
  360. if (ret < 0)
  361. BT_PWR_ERR("device create file error!");
  362. /* force Bluetooth off during init to allow for user control */
  363. rfkill_init_sw_state(rfkill, 1);
  364. previous = 1;
  365. ret = rfkill_register(rfkill);
  366. if (ret) {
  367. dev_err(&pdev->dev, "rfkill register failed=%d\n", ret);
  368. rfkill_destroy(rfkill);
  369. return ret;
  370. }
  371. platform_set_drvdata(pdev, rfkill);
  372. return 0;
  373. }
  374. static void bluetooth_power_rfkill_remove(struct platform_device *pdev)
  375. {
  376. struct rfkill *rfkill;
  377. dev_dbg(&pdev->dev, "%s\n", __func__);
  378. rfkill = platform_get_drvdata(pdev);
  379. if (rfkill)
  380. rfkill_unregister(rfkill);
  381. rfkill_destroy(rfkill);
  382. platform_set_drvdata(pdev, NULL);
  383. }
  384. #define MAX_PROP_SIZE 32
  385. static int bt_dt_parse_vreg_info(struct device *dev,
  386. struct bt_power_vreg_data **vreg_data, const char *vreg_name)
  387. {
  388. int len, ret = 0;
  389. const __be32 *prop;
  390. char prop_name[MAX_PROP_SIZE];
  391. struct bt_power_vreg_data *vreg;
  392. struct device_node *np = dev->of_node;
  393. BT_PWR_DBG("vreg dev tree parse for %s", vreg_name);
  394. *vreg_data = NULL;
  395. snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
  396. if (of_parse_phandle(np, prop_name, 0)) {
  397. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  398. if (!vreg) {
  399. dev_err(dev, "No memory for vreg: %s\n", vreg_name);
  400. ret = -ENOMEM;
  401. goto err;
  402. }
  403. vreg->name = vreg_name;
  404. /* Parse voltage-level from each node */
  405. snprintf(prop_name, MAX_PROP_SIZE,
  406. "%s-voltage-level", vreg_name);
  407. prop = of_get_property(np, prop_name, &len);
  408. if (!prop || (len != (2 * sizeof(__be32)))) {
  409. dev_warn(dev, "%s %s property\n",
  410. prop ? "invalid format" : "no", prop_name);
  411. } else {
  412. vreg->low_vol_level = be32_to_cpup(&prop[0]);
  413. vreg->high_vol_level = be32_to_cpup(&prop[1]);
  414. }
  415. /* Parse current-level from each node */
  416. snprintf(prop_name, MAX_PROP_SIZE,
  417. "%s-current-level", vreg_name);
  418. ret = of_property_read_u32(np, prop_name, &vreg->load_uA);
  419. if (ret < 0) {
  420. BT_PWR_DBG("%s property is not valid\n", prop_name);
  421. vreg->load_uA = -1;
  422. ret = 0;
  423. }
  424. *vreg_data = vreg;
  425. BT_PWR_DBG("%s: vol=[%d %d]uV, current=[%d]uA\n",
  426. vreg->name, vreg->low_vol_level,
  427. vreg->high_vol_level,
  428. vreg->load_uA);
  429. } else
  430. BT_PWR_INFO("%s: is not provided in device tree", vreg_name);
  431. err:
  432. return ret;
  433. }
  434. static int bt_dt_parse_clk_info(struct device *dev,
  435. struct bt_power_clk_data **clk_data)
  436. {
  437. int ret = -EINVAL;
  438. struct bt_power_clk_data *clk = NULL;
  439. struct device_node *np = dev->of_node;
  440. BT_PWR_DBG("");
  441. *clk_data = NULL;
  442. if (of_parse_phandle(np, "clocks", 0)) {
  443. clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL);
  444. if (!clk) {
  445. BT_PWR_ERR("No memory for clocks");
  446. ret = -ENOMEM;
  447. goto err;
  448. }
  449. /* Allocated 20 bytes size buffer for clock name string */
  450. clk->name = devm_kzalloc(dev, 20, GFP_KERNEL);
  451. /* Parse clock name from node */
  452. ret = of_property_read_string_index(np, "clock-names", 0,
  453. &(clk->name));
  454. if (ret < 0) {
  455. BT_PWR_ERR("reading \"clock-names\" failed");
  456. return ret;
  457. }
  458. clk->clk = devm_clk_get(dev, clk->name);
  459. if (IS_ERR(clk->clk)) {
  460. ret = PTR_ERR(clk->clk);
  461. BT_PWR_ERR("failed to get %s, ret (%d)",
  462. clk->name, ret);
  463. clk->clk = NULL;
  464. return ret;
  465. }
  466. *clk_data = clk;
  467. } else {
  468. BT_PWR_ERR("clocks is not provided in device tree");
  469. }
  470. err:
  471. return ret;
  472. }
  473. static int bt_power_populate_dt_pinfo(struct platform_device *pdev)
  474. {
  475. int rc;
  476. BT_PWR_DBG("");
  477. if (!bt_power_pdata)
  478. return -ENOMEM;
  479. if (pdev->dev.of_node) {
  480. bt_power_pdata->bt_gpio_sys_rst =
  481. of_get_named_gpio(pdev->dev.of_node,
  482. "qca,bt-reset-gpio", 0);
  483. if (bt_power_pdata->bt_gpio_sys_rst < 0)
  484. BT_PWR_ERR("bt-reset-gpio not provided in device tree");
  485. rc = bt_dt_parse_vreg_info(&pdev->dev,
  486. &bt_power_pdata->bt_vdd_core,
  487. "qca,bt-vdd-core");
  488. if (rc < 0)
  489. BT_PWR_ERR("bt-vdd-core not provided in device tree");
  490. rc = bt_dt_parse_vreg_info(&pdev->dev,
  491. &bt_power_pdata->bt_vdd_io,
  492. "qca,bt-vdd-io");
  493. if (rc < 0)
  494. BT_PWR_ERR("bt-vdd-io not provided in device tree");
  495. rc = bt_dt_parse_vreg_info(&pdev->dev,
  496. &bt_power_pdata->bt_vdd_xtal,
  497. "qca,bt-vdd-xtal");
  498. if (rc < 0)
  499. BT_PWR_ERR("bt-vdd-xtal not provided in device tree");
  500. rc = bt_dt_parse_vreg_info(&pdev->dev,
  501. &bt_power_pdata->bt_vdd_pa,
  502. "qca,bt-vdd-pa");
  503. if (rc < 0)
  504. BT_PWR_ERR("bt-vdd-pa not provided in device tree");
  505. rc = bt_dt_parse_vreg_info(&pdev->dev,
  506. &bt_power_pdata->bt_vdd_ldo,
  507. "qca,bt-vdd-ldo");
  508. if (rc < 0)
  509. BT_PWR_ERR("bt-vdd-ldo not provided in device tree");
  510. rc = bt_dt_parse_vreg_info(&pdev->dev,
  511. &bt_power_pdata->bt_chip_pwd,
  512. "qca,bt-chip-pwd");
  513. if (rc < 0)
  514. BT_PWR_ERR("bt-chip-pwd not provided in device tree");
  515. rc = bt_dt_parse_clk_info(&pdev->dev,
  516. &bt_power_pdata->bt_chip_clk);
  517. if (rc < 0)
  518. BT_PWR_ERR("clock not provided in device tree");
  519. }
  520. bt_power_pdata->bt_power_setup = bluetooth_power;
  521. return 0;
  522. }
  523. static int bt_power_probe(struct platform_device *pdev)
  524. {
  525. int ret = 0;
  526. dev_dbg(&pdev->dev, "%s\n", __func__);
  527. bt_power_pdata =
  528. kzalloc(sizeof(struct bluetooth_power_platform_data),
  529. GFP_KERNEL);
  530. if (!bt_power_pdata) {
  531. BT_PWR_ERR("Failed to allocate memory");
  532. return -ENOMEM;
  533. }
  534. if (pdev->dev.of_node) {
  535. ret = bt_power_populate_dt_pinfo(pdev);
  536. if (ret < 0) {
  537. BT_PWR_ERR("Failed to populate device tree info");
  538. goto free_pdata;
  539. }
  540. pdev->dev.platform_data = bt_power_pdata;
  541. } else if (pdev->dev.platform_data) {
  542. /* Optional data set to default if not provided */
  543. if (!((struct bluetooth_power_platform_data *)
  544. (pdev->dev.platform_data))->bt_power_setup)
  545. ((struct bluetooth_power_platform_data *)
  546. (pdev->dev.platform_data))->bt_power_setup =
  547. bluetooth_power;
  548. memcpy(bt_power_pdata, pdev->dev.platform_data,
  549. sizeof(struct bluetooth_power_platform_data));
  550. pwr_state = 0;
  551. } else {
  552. BT_PWR_ERR("Failed to get platform data");
  553. goto free_pdata;
  554. }
  555. if (bluetooth_power_rfkill_probe(pdev) < 0)
  556. goto free_pdata;
  557. btpdev = pdev;
  558. return 0;
  559. free_pdata:
  560. kfree(bt_power_pdata);
  561. return ret;
  562. }
  563. static int bt_power_remove(struct platform_device *pdev)
  564. {
  565. dev_dbg(&pdev->dev, "%s\n", __func__);
  566. bluetooth_power_rfkill_remove(pdev);
  567. if (bt_power_pdata->bt_chip_pwd->reg)
  568. regulator_put(bt_power_pdata->bt_chip_pwd->reg);
  569. kfree(bt_power_pdata);
  570. return 0;
  571. }
  572. int bt_register_slimdev(struct device *dev)
  573. {
  574. BT_PWR_DBG("");
  575. if (!bt_power_pdata || (dev == NULL)) {
  576. BT_PWR_ERR("Failed to allocate memory");
  577. return -EINVAL;
  578. }
  579. bt_power_pdata->slim_dev = dev;
  580. return 0;
  581. }
  582. static long bt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  583. {
  584. int ret = 0, pwr_cntrl = 0;
  585. switch (cmd) {
  586. case BT_CMD_SLIM_TEST:
  587. if (!bt_power_pdata->slim_dev) {
  588. BT_PWR_ERR("slim_dev is null\n");
  589. return -EINVAL;
  590. }
  591. ret = btfm_slim_hw_init(
  592. bt_power_pdata->slim_dev->platform_data
  593. );
  594. break;
  595. case BT_CMD_PWR_CTRL:
  596. pwr_cntrl = (int)arg;
  597. BT_PWR_ERR("BT_CMD_PWR_CTRL pwr_cntrl:%d", pwr_cntrl);
  598. if (pwr_state != pwr_cntrl) {
  599. ret = bluetooth_power(pwr_cntrl);
  600. if (!ret)
  601. pwr_state = pwr_cntrl;
  602. } else {
  603. BT_PWR_ERR("BT chip state is already :%d no change d\n"
  604. , pwr_state);
  605. ret = 0;
  606. }
  607. break;
  608. default:
  609. return -EINVAL;
  610. }
  611. return ret;
  612. }
  613. static struct platform_driver bt_power_driver = {
  614. .probe = bt_power_probe,
  615. .remove = bt_power_remove,
  616. .driver = {
  617. .name = "bt_power",
  618. .owner = THIS_MODULE,
  619. .of_match_table = bt_power_match_table,
  620. },
  621. };
  622. static const struct file_operations bt_dev_fops = {
  623. .owner = THIS_MODULE,
  624. .unlocked_ioctl = bt_ioctl,
  625. .compat_ioctl = bt_ioctl,
  626. };
  627. static int __init bluetooth_power_init(void)
  628. {
  629. int ret;
  630. ret = platform_driver_register(&bt_power_driver);
  631. bt_major = register_chrdev(0, "bt", &bt_dev_fops);
  632. if (bt_major < 0) {
  633. BTFMSLIM_ERR("failed to allocate char dev\n");
  634. goto chrdev_unreg;
  635. }
  636. bt_class = class_create(THIS_MODULE, "bt-dev");
  637. if (IS_ERR(bt_class)) {
  638. BTFMSLIM_ERR("coudn't create class");
  639. goto chrdev_unreg;
  640. }
  641. if (device_create(bt_class, NULL, MKDEV(bt_major, 0),
  642. NULL, "btpower") == NULL) {
  643. BTFMSLIM_ERR("failed to allocate char dev\n");
  644. goto chrdev_unreg;
  645. }
  646. return 0;
  647. chrdev_unreg:
  648. unregister_chrdev(bt_major, "bt");
  649. class_destroy(bt_class);
  650. return ret;
  651. }
  652. static void __exit bluetooth_power_exit(void)
  653. {
  654. platform_driver_unregister(&bt_power_driver);
  655. }
  656. MODULE_LICENSE("GPL v2");
  657. MODULE_DESCRIPTION("MSM Bluetooth power control driver");
  658. module_init(bluetooth_power_init);
  659. module_exit(bluetooth_power_exit);