Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / power / supply / bq24735-charger.c
blobab2f4bf8f603ffa947458be3b209ae66be22f757
1 /*
2 * Battery charger driver for TI BQ24735
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
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;
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.
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.
20 #include <linux/err.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>
31 #include <linux/power/bq24735-charger.h>
33 #define BQ24735_CHG_OPT 0x12
34 #define BQ24735_CHG_OPT_CHARGE_DISABLE (1 << 0)
35 #define BQ24735_CHG_OPT_AC_PRESENT (1 << 4)
36 #define BQ24735_CHARGE_CURRENT 0x14
37 #define BQ24735_CHARGE_CURRENT_MASK 0x1fc0
38 #define BQ24735_CHARGE_VOLTAGE 0x15
39 #define BQ24735_CHARGE_VOLTAGE_MASK 0x7ff0
40 #define BQ24735_INPUT_CURRENT 0x3f
41 #define BQ24735_INPUT_CURRENT_MASK 0x1f80
42 #define BQ24735_MANUFACTURER_ID 0xfe
43 #define BQ24735_DEVICE_ID 0xff
45 struct bq24735 {
46 struct power_supply *charger;
47 struct power_supply_desc charger_desc;
48 struct i2c_client *client;
49 struct bq24735_platform *pdata;
50 struct mutex lock;
51 struct gpio_desc *status_gpio;
52 struct delayed_work poll;
53 u32 poll_interval;
54 bool charging;
57 static inline struct bq24735 *to_bq24735(struct power_supply *psy)
59 return power_supply_get_drvdata(psy);
62 static enum power_supply_property bq24735_charger_properties[] = {
63 POWER_SUPPLY_PROP_STATUS,
64 POWER_SUPPLY_PROP_ONLINE,
67 static int bq24735_charger_property_is_writeable(struct power_supply *psy,
68 enum power_supply_property psp)
70 switch (psp) {
71 case POWER_SUPPLY_PROP_STATUS:
72 return 1;
73 default:
74 break;
77 return 0;
80 static inline int bq24735_write_word(struct i2c_client *client, u8 reg,
81 u16 value)
83 return i2c_smbus_write_word_data(client, reg, value);
86 static inline int bq24735_read_word(struct i2c_client *client, u8 reg)
88 return i2c_smbus_read_word_data(client, reg);
91 static int bq24735_update_word(struct i2c_client *client, u8 reg,
92 u16 mask, u16 value)
94 unsigned int tmp;
95 int ret;
97 ret = bq24735_read_word(client, reg);
98 if (ret < 0)
99 return ret;
101 tmp = ret & ~mask;
102 tmp |= value & mask;
104 return bq24735_write_word(client, reg, tmp);
107 static int bq24735_config_charger(struct bq24735 *charger)
109 struct bq24735_platform *pdata = charger->pdata;
110 int ret;
111 u16 value;
113 if (pdata->ext_control)
114 return 0;
116 if (pdata->charge_current) {
117 value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK;
119 ret = bq24735_write_word(charger->client,
120 BQ24735_CHARGE_CURRENT, value);
121 if (ret < 0) {
122 dev_err(&charger->client->dev,
123 "Failed to write charger current : %d\n",
124 ret);
125 return ret;
129 if (pdata->charge_voltage) {
130 value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK;
132 ret = bq24735_write_word(charger->client,
133 BQ24735_CHARGE_VOLTAGE, value);
134 if (ret < 0) {
135 dev_err(&charger->client->dev,
136 "Failed to write charger voltage : %d\n",
137 ret);
138 return ret;
142 if (pdata->input_current) {
143 value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK;
145 ret = bq24735_write_word(charger->client,
146 BQ24735_INPUT_CURRENT, value);
147 if (ret < 0) {
148 dev_err(&charger->client->dev,
149 "Failed to write input current : %d\n",
150 ret);
151 return ret;
155 return 0;
158 static inline int bq24735_enable_charging(struct bq24735 *charger)
160 int ret;
162 if (charger->pdata->ext_control)
163 return 0;
165 ret = bq24735_config_charger(charger);
166 if (ret)
167 return ret;
169 return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
170 BQ24735_CHG_OPT_CHARGE_DISABLE, 0);
173 static inline int bq24735_disable_charging(struct bq24735 *charger)
175 if (charger->pdata->ext_control)
176 return 0;
178 return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
179 BQ24735_CHG_OPT_CHARGE_DISABLE,
180 BQ24735_CHG_OPT_CHARGE_DISABLE);
183 static bool bq24735_charger_is_present(struct bq24735 *charger)
185 if (charger->status_gpio) {
186 return !gpiod_get_value_cansleep(charger->status_gpio);
187 } else {
188 int ac = 0;
190 ac = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
191 if (ac < 0) {
192 dev_dbg(&charger->client->dev,
193 "Failed to read charger options : %d\n",
194 ac);
195 return false;
197 return (ac & BQ24735_CHG_OPT_AC_PRESENT) ? true : false;
200 return false;
203 static int bq24735_charger_is_charging(struct bq24735 *charger)
205 int ret;
207 if (!bq24735_charger_is_present(charger))
208 return 0;
210 ret = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
211 if (ret < 0)
212 return ret;
214 return !(ret & BQ24735_CHG_OPT_CHARGE_DISABLE);
217 static void bq24735_update(struct bq24735 *charger)
219 mutex_lock(&charger->lock);
221 if (charger->charging && bq24735_charger_is_present(charger))
222 bq24735_enable_charging(charger);
223 else
224 bq24735_disable_charging(charger);
226 mutex_unlock(&charger->lock);
228 power_supply_changed(charger->charger);
231 static irqreturn_t bq24735_charger_isr(int irq, void *devid)
233 struct power_supply *psy = devid;
234 struct bq24735 *charger = to_bq24735(psy);
236 bq24735_update(charger);
238 return IRQ_HANDLED;
241 static void bq24735_poll(struct work_struct *work)
243 struct bq24735 *charger = container_of(work, struct bq24735, poll.work);
245 bq24735_update(charger);
247 schedule_delayed_work(&charger->poll,
248 msecs_to_jiffies(charger->poll_interval));
251 static int bq24735_charger_get_property(struct power_supply *psy,
252 enum power_supply_property psp,
253 union power_supply_propval *val)
255 struct bq24735 *charger = to_bq24735(psy);
257 switch (psp) {
258 case POWER_SUPPLY_PROP_ONLINE:
259 val->intval = bq24735_charger_is_present(charger) ? 1 : 0;
260 break;
261 case POWER_SUPPLY_PROP_STATUS:
262 switch (bq24735_charger_is_charging(charger)) {
263 case 1:
264 val->intval = POWER_SUPPLY_STATUS_CHARGING;
265 break;
266 case 0:
267 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
268 break;
269 default:
270 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
271 break;
273 break;
274 default:
275 return -EINVAL;
278 return 0;
281 static int bq24735_charger_set_property(struct power_supply *psy,
282 enum power_supply_property psp,
283 const union power_supply_propval *val)
285 struct bq24735 *charger = to_bq24735(psy);
286 int ret;
288 switch (psp) {
289 case POWER_SUPPLY_PROP_STATUS:
290 switch (val->intval) {
291 case POWER_SUPPLY_STATUS_CHARGING:
292 mutex_lock(&charger->lock);
293 charger->charging = true;
294 ret = bq24735_enable_charging(charger);
295 mutex_unlock(&charger->lock);
296 if (ret)
297 return ret;
298 break;
299 case POWER_SUPPLY_STATUS_DISCHARGING:
300 case POWER_SUPPLY_STATUS_NOT_CHARGING:
301 mutex_lock(&charger->lock);
302 charger->charging = false;
303 ret = bq24735_disable_charging(charger);
304 mutex_unlock(&charger->lock);
305 if (ret)
306 return ret;
307 break;
308 default:
309 return -EINVAL;
311 power_supply_changed(psy);
312 break;
313 default:
314 return -EPERM;
317 return 0;
320 static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client)
322 struct bq24735_platform *pdata;
323 struct device_node *np = client->dev.of_node;
324 u32 val;
325 int ret;
327 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
328 if (!pdata) {
329 dev_err(&client->dev,
330 "Memory alloc for bq24735 pdata failed\n");
331 return NULL;
334 ret = of_property_read_u32(np, "ti,charge-current", &val);
335 if (!ret)
336 pdata->charge_current = val;
338 ret = of_property_read_u32(np, "ti,charge-voltage", &val);
339 if (!ret)
340 pdata->charge_voltage = val;
342 ret = of_property_read_u32(np, "ti,input-current", &val);
343 if (!ret)
344 pdata->input_current = val;
346 pdata->ext_control = of_property_read_bool(np, "ti,external-control");
348 return pdata;
351 static int bq24735_charger_probe(struct i2c_client *client,
352 const struct i2c_device_id *id)
354 int ret;
355 struct bq24735 *charger;
356 struct power_supply_desc *supply_desc;
357 struct power_supply_config psy_cfg = {};
358 char *name;
360 charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL);
361 if (!charger)
362 return -ENOMEM;
364 mutex_init(&charger->lock);
365 charger->charging = true;
366 charger->pdata = client->dev.platform_data;
368 if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node)
369 charger->pdata = bq24735_parse_dt_data(client);
371 if (!charger->pdata) {
372 dev_err(&client->dev, "no platform data provided\n");
373 return -EINVAL;
376 name = (char *)charger->pdata->name;
377 if (!name) {
378 name = devm_kasprintf(&client->dev, GFP_KERNEL,
379 "bq24735@%s",
380 dev_name(&client->dev));
381 if (!name) {
382 dev_err(&client->dev, "Failed to alloc device name\n");
383 return -ENOMEM;
387 charger->client = client;
389 supply_desc = &charger->charger_desc;
391 supply_desc->name = name;
392 supply_desc->type = POWER_SUPPLY_TYPE_MAINS;
393 supply_desc->properties = bq24735_charger_properties;
394 supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties);
395 supply_desc->get_property = bq24735_charger_get_property;
396 supply_desc->set_property = bq24735_charger_set_property;
397 supply_desc->property_is_writeable =
398 bq24735_charger_property_is_writeable;
400 psy_cfg.supplied_to = charger->pdata->supplied_to;
401 psy_cfg.num_supplicants = charger->pdata->num_supplicants;
402 psy_cfg.of_node = client->dev.of_node;
403 psy_cfg.drv_data = charger;
405 i2c_set_clientdata(client, charger);
407 charger->status_gpio = devm_gpiod_get_optional(&client->dev,
408 "ti,ac-detect",
409 GPIOD_IN);
410 if (IS_ERR(charger->status_gpio)) {
411 ret = PTR_ERR(charger->status_gpio);
412 dev_err(&client->dev, "Getting gpio failed: %d\n", ret);
413 return ret;
416 if (bq24735_charger_is_present(charger)) {
417 ret = bq24735_read_word(client, BQ24735_MANUFACTURER_ID);
418 if (ret < 0) {
419 dev_err(&client->dev, "Failed to read manufacturer id : %d\n",
420 ret);
421 return ret;
422 } else if (ret != 0x0040) {
423 dev_err(&client->dev,
424 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret);
425 return -ENODEV;
428 ret = bq24735_read_word(client, BQ24735_DEVICE_ID);
429 if (ret < 0) {
430 dev_err(&client->dev, "Failed to read device id : %d\n", ret);
431 return ret;
432 } else if (ret != 0x000B) {
433 dev_err(&client->dev,
434 "device id mismatch. 0x000b != 0x%04x\n", ret);
435 return -ENODEV;
438 ret = bq24735_enable_charging(charger);
439 if (ret < 0) {
440 dev_err(&client->dev, "Failed to enable charging\n");
441 return ret;
445 charger->charger = devm_power_supply_register(&client->dev, supply_desc,
446 &psy_cfg);
447 if (IS_ERR(charger->charger)) {
448 ret = PTR_ERR(charger->charger);
449 dev_err(&client->dev, "Failed to register power supply: %d\n",
450 ret);
451 return ret;
454 if (client->irq) {
455 ret = devm_request_threaded_irq(&client->dev, client->irq,
456 NULL, bq24735_charger_isr,
457 IRQF_TRIGGER_RISING |
458 IRQF_TRIGGER_FALLING |
459 IRQF_ONESHOT,
460 supply_desc->name,
461 charger->charger);
462 if (ret) {
463 dev_err(&client->dev,
464 "Unable to register IRQ %d err %d\n",
465 client->irq, ret);
466 return ret;
468 } else {
469 ret = device_property_read_u32(&client->dev, "poll-interval",
470 &charger->poll_interval);
471 if (ret)
472 return 0;
473 if (!charger->poll_interval)
474 return 0;
476 INIT_DELAYED_WORK(&charger->poll, bq24735_poll);
477 schedule_delayed_work(&charger->poll,
478 msecs_to_jiffies(charger->poll_interval));
481 return 0;
484 static int bq24735_charger_remove(struct i2c_client *client)
486 struct bq24735 *charger = i2c_get_clientdata(client);
488 if (charger->poll_interval)
489 cancel_delayed_work_sync(&charger->poll);
491 return 0;
494 static const struct i2c_device_id bq24735_charger_id[] = {
495 { "bq24735-charger", 0 },
498 MODULE_DEVICE_TABLE(i2c, bq24735_charger_id);
500 static const struct of_device_id bq24735_match_ids[] = {
501 { .compatible = "ti,bq24735", },
502 { /* end */ }
504 MODULE_DEVICE_TABLE(of, bq24735_match_ids);
506 static struct i2c_driver bq24735_charger_driver = {
507 .driver = {
508 .name = "bq24735-charger",
509 .of_match_table = bq24735_match_ids,
511 .probe = bq24735_charger_probe,
512 .remove = bq24735_charger_remove,
513 .id_table = bq24735_charger_id,
516 module_i2c_driver(bq24735_charger_driver);
518 MODULE_DESCRIPTION("bq24735 battery charging driver");
519 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
520 MODULE_LICENSE("GPL v2");