bq24735-charger.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Battery charger driver for TI BQ24735
  3. *
  4. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  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;
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include <linux/err.h>
  20. #include <linux/gpio.h>
  21. #include <linux/i2c.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/gpio/consumer.h>
  28. #include <linux/power_supply.h>
  29. #include <linux/slab.h>
  30. #include <linux/power/bq24735-charger.h>
  31. #define BQ24735_CHG_OPT 0x12
  32. #define BQ24735_CHG_OPT_CHARGE_DISABLE (1 << 0)
  33. #define BQ24735_CHG_OPT_AC_PRESENT (1 << 4)
  34. #define BQ24735_CHARGE_CURRENT 0x14
  35. #define BQ24735_CHARGE_CURRENT_MASK 0x1fc0
  36. #define BQ24735_CHARGE_VOLTAGE 0x15
  37. #define BQ24735_CHARGE_VOLTAGE_MASK 0x7ff0
  38. #define BQ24735_INPUT_CURRENT 0x3f
  39. #define BQ24735_INPUT_CURRENT_MASK 0x1f80
  40. #define BQ24735_MANUFACTURER_ID 0xfe
  41. #define BQ24735_DEVICE_ID 0xff
  42. struct bq24735 {
  43. struct power_supply *charger;
  44. struct power_supply_desc charger_desc;
  45. struct i2c_client *client;
  46. struct bq24735_platform *pdata;
  47. struct mutex lock;
  48. struct gpio_desc *status_gpio;
  49. bool charging;
  50. };
  51. static inline struct bq24735 *to_bq24735(struct power_supply *psy)
  52. {
  53. return power_supply_get_drvdata(psy);
  54. }
  55. static enum power_supply_property bq24735_charger_properties[] = {
  56. POWER_SUPPLY_PROP_STATUS,
  57. POWER_SUPPLY_PROP_ONLINE,
  58. };
  59. static int bq24735_charger_property_is_writeable(struct power_supply *psy,
  60. enum power_supply_property psp)
  61. {
  62. switch (psp) {
  63. case POWER_SUPPLY_PROP_STATUS:
  64. return 1;
  65. default:
  66. break;
  67. }
  68. return 0;
  69. }
  70. static inline int bq24735_write_word(struct i2c_client *client, u8 reg,
  71. u16 value)
  72. {
  73. return i2c_smbus_write_word_data(client, reg, le16_to_cpu(value));
  74. }
  75. static inline int bq24735_read_word(struct i2c_client *client, u8 reg)
  76. {
  77. s32 ret = i2c_smbus_read_word_data(client, reg);
  78. return ret < 0 ? ret : le16_to_cpu(ret);
  79. }
  80. static int bq24735_update_word(struct i2c_client *client, u8 reg,
  81. u16 mask, u16 value)
  82. {
  83. unsigned int tmp;
  84. int ret;
  85. ret = bq24735_read_word(client, reg);
  86. if (ret < 0)
  87. return ret;
  88. tmp = ret & ~mask;
  89. tmp |= value & mask;
  90. return bq24735_write_word(client, reg, tmp);
  91. }
  92. static inline int bq24735_enable_charging(struct bq24735 *charger)
  93. {
  94. if (charger->pdata->ext_control)
  95. return 0;
  96. return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
  97. BQ24735_CHG_OPT_CHARGE_DISABLE,
  98. ~BQ24735_CHG_OPT_CHARGE_DISABLE);
  99. }
  100. static inline int bq24735_disable_charging(struct bq24735 *charger)
  101. {
  102. if (charger->pdata->ext_control)
  103. return 0;
  104. return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
  105. BQ24735_CHG_OPT_CHARGE_DISABLE,
  106. BQ24735_CHG_OPT_CHARGE_DISABLE);
  107. }
  108. static int bq24735_config_charger(struct bq24735 *charger)
  109. {
  110. struct bq24735_platform *pdata = charger->pdata;
  111. int ret;
  112. u16 value;
  113. if (pdata->ext_control)
  114. return 0;
  115. if (pdata->charge_current) {
  116. value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK;
  117. ret = bq24735_write_word(charger->client,
  118. BQ24735_CHARGE_CURRENT, value);
  119. if (ret < 0) {
  120. dev_err(&charger->client->dev,
  121. "Failed to write charger current : %d\n",
  122. ret);
  123. return ret;
  124. }
  125. }
  126. if (pdata->charge_voltage) {
  127. value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK;
  128. ret = bq24735_write_word(charger->client,
  129. BQ24735_CHARGE_VOLTAGE, value);
  130. if (ret < 0) {
  131. dev_err(&charger->client->dev,
  132. "Failed to write charger voltage : %d\n",
  133. ret);
  134. return ret;
  135. }
  136. }
  137. if (pdata->input_current) {
  138. value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK;
  139. ret = bq24735_write_word(charger->client,
  140. BQ24735_INPUT_CURRENT, value);
  141. if (ret < 0) {
  142. dev_err(&charger->client->dev,
  143. "Failed to write input current : %d\n",
  144. ret);
  145. return ret;
  146. }
  147. }
  148. return 0;
  149. }
  150. static bool bq24735_charger_is_present(struct bq24735 *charger)
  151. {
  152. if (charger->status_gpio) {
  153. return !gpiod_get_value_cansleep(charger->status_gpio);
  154. } else {
  155. int ac = 0;
  156. ac = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
  157. if (ac < 0) {
  158. dev_err(&charger->client->dev,
  159. "Failed to read charger options : %d\n",
  160. ac);
  161. return false;
  162. }
  163. return (ac & BQ24735_CHG_OPT_AC_PRESENT) ? true : false;
  164. }
  165. return false;
  166. }
  167. static int bq24735_charger_is_charging(struct bq24735 *charger)
  168. {
  169. int ret;
  170. if (!bq24735_charger_is_present(charger))
  171. return 0;
  172. ret = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
  173. if (ret < 0)
  174. return ret;
  175. return !(ret & BQ24735_CHG_OPT_CHARGE_DISABLE);
  176. }
  177. static irqreturn_t bq24735_charger_isr(int irq, void *devid)
  178. {
  179. struct power_supply *psy = devid;
  180. struct bq24735 *charger = to_bq24735(psy);
  181. mutex_lock(&charger->lock);
  182. if (charger->charging && bq24735_charger_is_present(charger))
  183. bq24735_enable_charging(charger);
  184. else
  185. bq24735_disable_charging(charger);
  186. mutex_unlock(&charger->lock);
  187. power_supply_changed(psy);
  188. return IRQ_HANDLED;
  189. }
  190. static int bq24735_charger_get_property(struct power_supply *psy,
  191. enum power_supply_property psp,
  192. union power_supply_propval *val)
  193. {
  194. struct bq24735 *charger = to_bq24735(psy);
  195. switch (psp) {
  196. case POWER_SUPPLY_PROP_ONLINE:
  197. val->intval = bq24735_charger_is_present(charger) ? 1 : 0;
  198. break;
  199. case POWER_SUPPLY_PROP_STATUS:
  200. switch (bq24735_charger_is_charging(charger)) {
  201. case 1:
  202. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  203. break;
  204. case 0:
  205. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  206. break;
  207. default:
  208. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  209. break;
  210. }
  211. break;
  212. default:
  213. return -EINVAL;
  214. }
  215. return 0;
  216. }
  217. static int bq24735_charger_set_property(struct power_supply *psy,
  218. enum power_supply_property psp,
  219. const union power_supply_propval *val)
  220. {
  221. struct bq24735 *charger = to_bq24735(psy);
  222. int ret;
  223. switch (psp) {
  224. case POWER_SUPPLY_PROP_STATUS:
  225. switch (val->intval) {
  226. case POWER_SUPPLY_STATUS_CHARGING:
  227. mutex_lock(&charger->lock);
  228. charger->charging = true;
  229. ret = bq24735_enable_charging(charger);
  230. mutex_unlock(&charger->lock);
  231. if (ret)
  232. return ret;
  233. bq24735_config_charger(charger);
  234. break;
  235. case POWER_SUPPLY_STATUS_DISCHARGING:
  236. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  237. mutex_lock(&charger->lock);
  238. charger->charging = false;
  239. ret = bq24735_disable_charging(charger);
  240. mutex_unlock(&charger->lock);
  241. if (ret)
  242. return ret;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. power_supply_changed(psy);
  248. break;
  249. default:
  250. return -EPERM;
  251. }
  252. return 0;
  253. }
  254. static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client)
  255. {
  256. struct bq24735_platform *pdata;
  257. struct device_node *np = client->dev.of_node;
  258. u32 val;
  259. int ret;
  260. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  261. if (!pdata) {
  262. dev_err(&client->dev,
  263. "Memory alloc for bq24735 pdata failed\n");
  264. return NULL;
  265. }
  266. ret = of_property_read_u32(np, "ti,charge-current", &val);
  267. if (!ret)
  268. pdata->charge_current = val;
  269. ret = of_property_read_u32(np, "ti,charge-voltage", &val);
  270. if (!ret)
  271. pdata->charge_voltage = val;
  272. ret = of_property_read_u32(np, "ti,input-current", &val);
  273. if (!ret)
  274. pdata->input_current = val;
  275. pdata->ext_control = of_property_read_bool(np, "ti,external-control");
  276. return pdata;
  277. }
  278. static int bq24735_charger_probe(struct i2c_client *client,
  279. const struct i2c_device_id *id)
  280. {
  281. int ret;
  282. struct bq24735 *charger;
  283. struct power_supply_desc *supply_desc;
  284. struct power_supply_config psy_cfg = {};
  285. char *name;
  286. charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL);
  287. if (!charger)
  288. return -ENOMEM;
  289. mutex_init(&charger->lock);
  290. charger->charging = true;
  291. charger->pdata = client->dev.platform_data;
  292. if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node)
  293. charger->pdata = bq24735_parse_dt_data(client);
  294. if (!charger->pdata) {
  295. dev_err(&client->dev, "no platform data provided\n");
  296. return -EINVAL;
  297. }
  298. name = (char *)charger->pdata->name;
  299. if (!name) {
  300. name = devm_kasprintf(&client->dev, GFP_KERNEL,
  301. "bq24735@%s",
  302. dev_name(&client->dev));
  303. if (!name) {
  304. dev_err(&client->dev, "Failed to alloc device name\n");
  305. return -ENOMEM;
  306. }
  307. }
  308. charger->client = client;
  309. supply_desc = &charger->charger_desc;
  310. supply_desc->name = name;
  311. supply_desc->type = POWER_SUPPLY_TYPE_MAINS;
  312. supply_desc->properties = bq24735_charger_properties;
  313. supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties);
  314. supply_desc->get_property = bq24735_charger_get_property;
  315. supply_desc->set_property = bq24735_charger_set_property;
  316. supply_desc->property_is_writeable =
  317. bq24735_charger_property_is_writeable;
  318. psy_cfg.supplied_to = charger->pdata->supplied_to;
  319. psy_cfg.num_supplicants = charger->pdata->num_supplicants;
  320. psy_cfg.of_node = client->dev.of_node;
  321. psy_cfg.drv_data = charger;
  322. i2c_set_clientdata(client, charger);
  323. charger->status_gpio = devm_gpiod_get_optional(&client->dev,
  324. "ti,ac-detect",
  325. GPIOD_IN);
  326. if (IS_ERR(charger->status_gpio)) {
  327. ret = PTR_ERR(charger->status_gpio);
  328. dev_err(&client->dev, "Getting gpio failed: %d\n", ret);
  329. return ret;
  330. }
  331. if (!charger->status_gpio || bq24735_charger_is_present(charger)) {
  332. ret = bq24735_read_word(client, BQ24735_MANUFACTURER_ID);
  333. if (ret < 0) {
  334. dev_err(&client->dev, "Failed to read manufacturer id : %d\n",
  335. ret);
  336. return ret;
  337. } else if (ret != 0x0040) {
  338. dev_err(&client->dev,
  339. "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret);
  340. return -ENODEV;
  341. }
  342. ret = bq24735_read_word(client, BQ24735_DEVICE_ID);
  343. if (ret < 0) {
  344. dev_err(&client->dev, "Failed to read device id : %d\n", ret);
  345. return ret;
  346. } else if (ret != 0x000B) {
  347. dev_err(&client->dev,
  348. "device id mismatch. 0x000b != 0x%04x\n", ret);
  349. return -ENODEV;
  350. }
  351. }
  352. ret = bq24735_config_charger(charger);
  353. if (ret < 0) {
  354. dev_err(&client->dev, "failed in configuring charger");
  355. return ret;
  356. }
  357. /* check for AC adapter presence */
  358. if (bq24735_charger_is_present(charger)) {
  359. ret = bq24735_enable_charging(charger);
  360. if (ret < 0) {
  361. dev_err(&client->dev, "Failed to enable charging\n");
  362. return ret;
  363. }
  364. }
  365. charger->charger = devm_power_supply_register(&client->dev, supply_desc,
  366. &psy_cfg);
  367. if (IS_ERR(charger->charger)) {
  368. ret = PTR_ERR(charger->charger);
  369. dev_err(&client->dev, "Failed to register power supply: %d\n",
  370. ret);
  371. return ret;
  372. }
  373. if (client->irq) {
  374. ret = devm_request_threaded_irq(&client->dev, client->irq,
  375. NULL, bq24735_charger_isr,
  376. IRQF_TRIGGER_RISING |
  377. IRQF_TRIGGER_FALLING |
  378. IRQF_ONESHOT,
  379. supply_desc->name,
  380. charger->charger);
  381. if (ret) {
  382. dev_err(&client->dev,
  383. "Unable to register IRQ %d err %d\n",
  384. client->irq, ret);
  385. return ret;
  386. }
  387. }
  388. return 0;
  389. }
  390. static const struct i2c_device_id bq24735_charger_id[] = {
  391. { "bq24735-charger", 0 },
  392. {}
  393. };
  394. MODULE_DEVICE_TABLE(i2c, bq24735_charger_id);
  395. static const struct of_device_id bq24735_match_ids[] = {
  396. { .compatible = "ti,bq24735", },
  397. { /* end */ }
  398. };
  399. MODULE_DEVICE_TABLE(of, bq24735_match_ids);
  400. static struct i2c_driver bq24735_charger_driver = {
  401. .driver = {
  402. .name = "bq24735-charger",
  403. .of_match_table = bq24735_match_ids,
  404. },
  405. .probe = bq24735_charger_probe,
  406. .id_table = bq24735_charger_id,
  407. };
  408. module_i2c_driver(bq24735_charger_driver);
  409. MODULE_DESCRIPTION("bq24735 battery charging driver");
  410. MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
  411. MODULE_LICENSE("GPL v2");