Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 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 | |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/of.h> |
| 28 | #include <linux/of_gpio.h> |
| 29 | #include <linux/power_supply.h> |
| 30 | #include <linux/slab.h> |
| 31 | |
| 32 | #include <linux/power/bq24735-charger.h> |
| 33 | |
| 34 | #define BQ24735_CHG_OPT 0x12 |
| 35 | #define BQ24735_CHG_OPT_CHARGE_DISABLE (1 << 0) |
| 36 | #define BQ24735_CHG_OPT_AC_PRESENT (1 << 4) |
| 37 | #define BQ24735_CHARGE_CURRENT 0x14 |
| 38 | #define BQ24735_CHARGE_CURRENT_MASK 0x1fc0 |
| 39 | #define BQ24735_CHARGE_VOLTAGE 0x15 |
| 40 | #define BQ24735_CHARGE_VOLTAGE_MASK 0x7ff0 |
| 41 | #define BQ24735_INPUT_CURRENT 0x3f |
| 42 | #define BQ24735_INPUT_CURRENT_MASK 0x1f80 |
| 43 | #define BQ24735_MANUFACTURER_ID 0xfe |
| 44 | #define BQ24735_DEVICE_ID 0xff |
| 45 | |
| 46 | struct bq24735 { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 47 | struct power_supply *charger; |
| 48 | struct power_supply_desc charger_desc; |
| 49 | struct i2c_client *client; |
| 50 | struct bq24735_platform *pdata; |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static inline struct bq24735 *to_bq24735(struct power_supply *psy) |
| 54 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 55 | return power_supply_get_drvdata(psy); |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static enum power_supply_property bq24735_charger_properties[] = { |
| 59 | POWER_SUPPLY_PROP_ONLINE, |
| 60 | }; |
| 61 | |
| 62 | static inline int bq24735_write_word(struct i2c_client *client, u8 reg, |
| 63 | u16 value) |
| 64 | { |
| 65 | return i2c_smbus_write_word_data(client, reg, le16_to_cpu(value)); |
| 66 | } |
| 67 | |
| 68 | static inline int bq24735_read_word(struct i2c_client *client, u8 reg) |
| 69 | { |
| 70 | s32 ret = i2c_smbus_read_word_data(client, reg); |
| 71 | |
| 72 | return ret < 0 ? ret : le16_to_cpu(ret); |
| 73 | } |
| 74 | |
| 75 | static int bq24735_update_word(struct i2c_client *client, u8 reg, |
| 76 | u16 mask, u16 value) |
| 77 | { |
| 78 | unsigned int tmp; |
| 79 | int ret; |
| 80 | |
| 81 | ret = bq24735_read_word(client, reg); |
| 82 | if (ret < 0) |
| 83 | return ret; |
| 84 | |
| 85 | tmp = ret & ~mask; |
| 86 | tmp |= value & mask; |
| 87 | |
| 88 | return bq24735_write_word(client, reg, tmp); |
| 89 | } |
| 90 | |
| 91 | static inline int bq24735_enable_charging(struct bq24735 *charger) |
| 92 | { |
| 93 | return bq24735_update_word(charger->client, BQ24735_CHG_OPT, |
| 94 | BQ24735_CHG_OPT_CHARGE_DISABLE, |
| 95 | ~BQ24735_CHG_OPT_CHARGE_DISABLE); |
| 96 | } |
| 97 | |
| 98 | static inline int bq24735_disable_charging(struct bq24735 *charger) |
| 99 | { |
| 100 | return bq24735_update_word(charger->client, BQ24735_CHG_OPT, |
| 101 | BQ24735_CHG_OPT_CHARGE_DISABLE, |
| 102 | BQ24735_CHG_OPT_CHARGE_DISABLE); |
| 103 | } |
| 104 | |
| 105 | static int bq24735_config_charger(struct bq24735 *charger) |
| 106 | { |
| 107 | struct bq24735_platform *pdata = charger->pdata; |
| 108 | int ret; |
| 109 | u16 value; |
| 110 | |
| 111 | if (pdata->charge_current) { |
| 112 | value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK; |
| 113 | |
| 114 | ret = bq24735_write_word(charger->client, |
| 115 | BQ24735_CHARGE_CURRENT, value); |
| 116 | if (ret < 0) { |
| 117 | dev_err(&charger->client->dev, |
| 118 | "Failed to write charger current : %d\n", |
| 119 | ret); |
| 120 | return ret; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (pdata->charge_voltage) { |
| 125 | value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK; |
| 126 | |
| 127 | ret = bq24735_write_word(charger->client, |
| 128 | BQ24735_CHARGE_VOLTAGE, value); |
| 129 | if (ret < 0) { |
| 130 | dev_err(&charger->client->dev, |
| 131 | "Failed to write charger voltage : %d\n", |
| 132 | ret); |
| 133 | return ret; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (pdata->input_current) { |
| 138 | value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK; |
| 139 | |
| 140 | ret = bq24735_write_word(charger->client, |
| 141 | BQ24735_INPUT_CURRENT, value); |
| 142 | if (ret < 0) { |
| 143 | dev_err(&charger->client->dev, |
| 144 | "Failed to write input current : %d\n", |
| 145 | ret); |
| 146 | return ret; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static bool bq24735_charger_is_present(struct bq24735 *charger) |
| 154 | { |
| 155 | struct bq24735_platform *pdata = charger->pdata; |
| 156 | int ret; |
| 157 | |
| 158 | if (pdata->status_gpio_valid) { |
| 159 | ret = gpio_get_value_cansleep(pdata->status_gpio); |
| 160 | return ret ^= pdata->status_gpio_active_low == 0; |
| 161 | } else { |
| 162 | int ac = 0; |
| 163 | |
| 164 | ac = bq24735_read_word(charger->client, BQ24735_CHG_OPT); |
| 165 | if (ac < 0) { |
| 166 | dev_err(&charger->client->dev, |
| 167 | "Failed to read charger options : %d\n", |
| 168 | ac); |
| 169 | return false; |
| 170 | } |
| 171 | return (ac & BQ24735_CHG_OPT_AC_PRESENT) ? true : false; |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 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 | |
| 182 | if (bq24735_charger_is_present(charger)) |
| 183 | bq24735_enable_charging(charger); |
| 184 | else |
| 185 | bq24735_disable_charging(charger); |
| 186 | |
| 187 | power_supply_changed(psy); |
| 188 | |
| 189 | return IRQ_HANDLED; |
| 190 | } |
| 191 | |
| 192 | static int bq24735_charger_get_property(struct power_supply *psy, |
| 193 | enum power_supply_property psp, |
| 194 | union power_supply_propval *val) |
| 195 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 196 | struct bq24735 *charger = to_bq24735(psy); |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 197 | |
| 198 | switch (psp) { |
| 199 | case POWER_SUPPLY_PROP_ONLINE: |
| 200 | val->intval = bq24735_charger_is_present(charger) ? 1 : 0; |
| 201 | break; |
| 202 | default: |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client) |
| 210 | { |
| 211 | struct bq24735_platform *pdata; |
| 212 | struct device_node *np = client->dev.of_node; |
| 213 | u32 val; |
| 214 | int ret; |
| 215 | enum of_gpio_flags flags; |
| 216 | |
| 217 | pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); |
| 218 | if (!pdata) { |
| 219 | dev_err(&client->dev, |
| 220 | "Memory alloc for bq24735 pdata failed\n"); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | pdata->status_gpio = of_get_named_gpio_flags(np, "ti,ac-detect-gpios", |
| 225 | 0, &flags); |
| 226 | |
| 227 | if (flags & OF_GPIO_ACTIVE_LOW) |
| 228 | pdata->status_gpio_active_low = 1; |
| 229 | |
| 230 | ret = of_property_read_u32(np, "ti,charge-current", &val); |
| 231 | if (!ret) |
| 232 | pdata->charge_current = val; |
| 233 | |
| 234 | ret = of_property_read_u32(np, "ti,charge-voltage", &val); |
| 235 | if (!ret) |
| 236 | pdata->charge_voltage = val; |
| 237 | |
| 238 | ret = of_property_read_u32(np, "ti,input-current", &val); |
| 239 | if (!ret) |
| 240 | pdata->input_current = val; |
| 241 | |
| 242 | return pdata; |
| 243 | } |
| 244 | |
| 245 | static int bq24735_charger_probe(struct i2c_client *client, |
| 246 | const struct i2c_device_id *id) |
| 247 | { |
| 248 | int ret; |
| 249 | struct bq24735 *charger; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 250 | struct power_supply_desc *supply_desc; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 251 | struct power_supply_config psy_cfg = {}; |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 252 | char *name; |
| 253 | |
| 254 | charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL); |
| 255 | if (!charger) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | charger->pdata = client->dev.platform_data; |
| 259 | |
| 260 | if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node) |
| 261 | charger->pdata = bq24735_parse_dt_data(client); |
| 262 | |
| 263 | if (!charger->pdata) { |
| 264 | dev_err(&client->dev, "no platform data provided\n"); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | name = (char *)charger->pdata->name; |
| 269 | if (!name) { |
| 270 | name = kasprintf(GFP_KERNEL, "bq24735@%s", |
| 271 | dev_name(&client->dev)); |
| 272 | if (!name) { |
| 273 | dev_err(&client->dev, "Failed to alloc device name\n"); |
| 274 | return -ENOMEM; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | charger->client = client; |
| 279 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 280 | supply_desc = &charger->charger_desc; |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 281 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 282 | supply_desc->name = name; |
| 283 | supply_desc->type = POWER_SUPPLY_TYPE_MAINS; |
| 284 | supply_desc->properties = bq24735_charger_properties; |
| 285 | supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties); |
| 286 | supply_desc->get_property = bq24735_charger_get_property; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 287 | |
| 288 | psy_cfg.supplied_to = charger->pdata->supplied_to; |
| 289 | psy_cfg.num_supplicants = charger->pdata->num_supplicants; |
| 290 | psy_cfg.of_node = client->dev.of_node; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 291 | psy_cfg.drv_data = charger; |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 292 | |
| 293 | i2c_set_clientdata(client, charger); |
| 294 | |
| 295 | ret = bq24735_read_word(client, BQ24735_MANUFACTURER_ID); |
| 296 | if (ret < 0) { |
| 297 | dev_err(&client->dev, "Failed to read manufacturer id : %d\n", |
| 298 | ret); |
| 299 | goto err_free_name; |
| 300 | } else if (ret != 0x0040) { |
| 301 | dev_err(&client->dev, |
| 302 | "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret); |
| 303 | ret = -ENODEV; |
| 304 | goto err_free_name; |
| 305 | } |
| 306 | |
| 307 | ret = bq24735_read_word(client, BQ24735_DEVICE_ID); |
| 308 | if (ret < 0) { |
| 309 | dev_err(&client->dev, "Failed to read device id : %d\n", ret); |
| 310 | goto err_free_name; |
| 311 | } else if (ret != 0x000B) { |
| 312 | dev_err(&client->dev, |
| 313 | "device id mismatch. 0x000b != 0x%04x\n", ret); |
| 314 | ret = -ENODEV; |
| 315 | goto err_free_name; |
| 316 | } |
| 317 | |
| 318 | if (gpio_is_valid(charger->pdata->status_gpio)) { |
| 319 | ret = devm_gpio_request(&client->dev, |
| 320 | charger->pdata->status_gpio, |
| 321 | name); |
| 322 | if (ret) { |
| 323 | dev_err(&client->dev, |
| 324 | "Failed GPIO request for GPIO %d: %d\n", |
| 325 | charger->pdata->status_gpio, ret); |
| 326 | } |
| 327 | |
| 328 | charger->pdata->status_gpio_valid = !ret; |
| 329 | } |
| 330 | |
| 331 | ret = bq24735_config_charger(charger); |
| 332 | if (ret < 0) { |
| 333 | dev_err(&client->dev, "failed in configuring charger"); |
| 334 | goto err_free_name; |
| 335 | } |
| 336 | |
| 337 | /* check for AC adapter presence */ |
| 338 | if (bq24735_charger_is_present(charger)) { |
| 339 | ret = bq24735_enable_charging(charger); |
| 340 | if (ret < 0) { |
| 341 | dev_err(&client->dev, "Failed to enable charging\n"); |
| 342 | goto err_free_name; |
| 343 | } |
| 344 | } |
| 345 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 346 | charger->charger = power_supply_register(&client->dev, supply_desc, |
| 347 | &psy_cfg); |
| 348 | if (IS_ERR(charger->charger)) { |
| 349 | ret = PTR_ERR(charger->charger); |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 350 | dev_err(&client->dev, "Failed to register power supply: %d\n", |
| 351 | ret); |
| 352 | goto err_free_name; |
| 353 | } |
| 354 | |
| 355 | if (client->irq) { |
| 356 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 357 | NULL, bq24735_charger_isr, |
| 358 | IRQF_TRIGGER_RISING | |
| 359 | IRQF_TRIGGER_FALLING | |
| 360 | IRQF_ONESHOT, |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 361 | supply_desc->name, |
| 362 | charger->charger); |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 363 | if (ret) { |
| 364 | dev_err(&client->dev, |
| 365 | "Unable to register IRQ %d err %d\n", |
| 366 | client->irq, ret); |
| 367 | goto err_unregister_supply; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | err_unregister_supply: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 373 | power_supply_unregister(charger->charger); |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 374 | err_free_name: |
| 375 | if (name != charger->pdata->name) |
| 376 | kfree(name); |
| 377 | |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | static int bq24735_charger_remove(struct i2c_client *client) |
| 382 | { |
| 383 | struct bq24735 *charger = i2c_get_clientdata(client); |
| 384 | |
| 385 | if (charger->client->irq) |
| 386 | devm_free_irq(&charger->client->dev, charger->client->irq, |
| 387 | &charger->charger); |
| 388 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 389 | power_supply_unregister(charger->charger); |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 390 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 391 | if (charger->charger_desc.name != charger->pdata->name) |
| 392 | kfree(charger->charger_desc.name); |
Darbha Sriharsha | ce77399 | 2013-10-11 17:15:43 -0400 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static const struct i2c_device_id bq24735_charger_id[] = { |
| 398 | { "bq24735-charger", 0 }, |
| 399 | {} |
| 400 | }; |
| 401 | MODULE_DEVICE_TABLE(i2c, bq24735_charger_id); |
| 402 | |
| 403 | static const struct of_device_id bq24735_match_ids[] = { |
| 404 | { .compatible = "ti,bq24735", }, |
| 405 | { /* end */ } |
| 406 | }; |
| 407 | MODULE_DEVICE_TABLE(of, bq24735_match_ids); |
| 408 | |
| 409 | static struct i2c_driver bq24735_charger_driver = { |
| 410 | .driver = { |
| 411 | .name = "bq24735-charger", |
| 412 | .owner = THIS_MODULE, |
| 413 | .of_match_table = bq24735_match_ids, |
| 414 | }, |
| 415 | .probe = bq24735_charger_probe, |
| 416 | .remove = bq24735_charger_remove, |
| 417 | .id_table = bq24735_charger_id, |
| 418 | }; |
| 419 | |
| 420 | module_i2c_driver(bq24735_charger_driver); |
| 421 | |
| 422 | MODULE_DESCRIPTION("bq24735 battery charging driver"); |
| 423 | MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>"); |
| 424 | MODULE_LICENSE("GPL v2"); |