Linux 4.1.16
[linux/fpc-iii.git] / drivers / power / bq24735-charger.c
blob961a189300275a3d65b5885ab5d647822059ba4c
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/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>
32 #include <linux/power/bq24735-charger.h>
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
46 struct bq24735 {
47 struct power_supply *charger;
48 struct power_supply_desc charger_desc;
49 struct i2c_client *client;
50 struct bq24735_platform *pdata;
53 static inline struct bq24735 *to_bq24735(struct power_supply *psy)
55 return power_supply_get_drvdata(psy);
58 static enum power_supply_property bq24735_charger_properties[] = {
59 POWER_SUPPLY_PROP_ONLINE,
62 static inline int bq24735_write_word(struct i2c_client *client, u8 reg,
63 u16 value)
65 return i2c_smbus_write_word_data(client, reg, le16_to_cpu(value));
68 static inline int bq24735_read_word(struct i2c_client *client, u8 reg)
70 s32 ret = i2c_smbus_read_word_data(client, reg);
72 return ret < 0 ? ret : le16_to_cpu(ret);
75 static int bq24735_update_word(struct i2c_client *client, u8 reg,
76 u16 mask, u16 value)
78 unsigned int tmp;
79 int ret;
81 ret = bq24735_read_word(client, reg);
82 if (ret < 0)
83 return ret;
85 tmp = ret & ~mask;
86 tmp |= value & mask;
88 return bq24735_write_word(client, reg, tmp);
91 static inline int bq24735_enable_charging(struct bq24735 *charger)
93 return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
94 BQ24735_CHG_OPT_CHARGE_DISABLE,
95 ~BQ24735_CHG_OPT_CHARGE_DISABLE);
98 static inline int bq24735_disable_charging(struct bq24735 *charger)
100 return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
101 BQ24735_CHG_OPT_CHARGE_DISABLE,
102 BQ24735_CHG_OPT_CHARGE_DISABLE);
105 static int bq24735_config_charger(struct bq24735 *charger)
107 struct bq24735_platform *pdata = charger->pdata;
108 int ret;
109 u16 value;
111 if (pdata->charge_current) {
112 value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK;
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;
124 if (pdata->charge_voltage) {
125 value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK;
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;
137 if (pdata->input_current) {
138 value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK;
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;
150 return 0;
153 static bool bq24735_charger_is_present(struct bq24735 *charger)
155 struct bq24735_platform *pdata = charger->pdata;
156 int ret;
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;
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;
171 return (ac & BQ24735_CHG_OPT_AC_PRESENT) ? true : false;
174 return false;
177 static irqreturn_t bq24735_charger_isr(int irq, void *devid)
179 struct power_supply *psy = devid;
180 struct bq24735 *charger = to_bq24735(psy);
182 if (bq24735_charger_is_present(charger))
183 bq24735_enable_charging(charger);
184 else
185 bq24735_disable_charging(charger);
187 power_supply_changed(psy);
189 return IRQ_HANDLED;
192 static int bq24735_charger_get_property(struct power_supply *psy,
193 enum power_supply_property psp,
194 union power_supply_propval *val)
196 struct bq24735 *charger = to_bq24735(psy);
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;
206 return 0;
209 static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client)
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;
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;
224 pdata->status_gpio = of_get_named_gpio_flags(np, "ti,ac-detect-gpios",
225 0, &flags);
227 if (flags & OF_GPIO_ACTIVE_LOW)
228 pdata->status_gpio_active_low = 1;
230 ret = of_property_read_u32(np, "ti,charge-current", &val);
231 if (!ret)
232 pdata->charge_current = val;
234 ret = of_property_read_u32(np, "ti,charge-voltage", &val);
235 if (!ret)
236 pdata->charge_voltage = val;
238 ret = of_property_read_u32(np, "ti,input-current", &val);
239 if (!ret)
240 pdata->input_current = val;
242 return pdata;
245 static int bq24735_charger_probe(struct i2c_client *client,
246 const struct i2c_device_id *id)
248 int ret;
249 struct bq24735 *charger;
250 struct power_supply_desc *supply_desc;
251 struct power_supply_config psy_cfg = {};
252 char *name;
254 charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL);
255 if (!charger)
256 return -ENOMEM;
258 charger->pdata = client->dev.platform_data;
260 if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node)
261 charger->pdata = bq24735_parse_dt_data(client);
263 if (!charger->pdata) {
264 dev_err(&client->dev, "no platform data provided\n");
265 return -EINVAL;
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;
278 charger->client = client;
280 supply_desc = &charger->charger_desc;
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;
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;
291 psy_cfg.drv_data = charger;
293 i2c_set_clientdata(client, charger);
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;
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;
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);
328 charger->pdata->status_gpio_valid = !ret;
331 ret = bq24735_config_charger(charger);
332 if (ret < 0) {
333 dev_err(&client->dev, "failed in configuring charger");
334 goto err_free_name;
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;
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);
350 dev_err(&client->dev, "Failed to register power supply: %d\n",
351 ret);
352 goto err_free_name;
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,
361 supply_desc->name,
362 charger->charger);
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;
371 return 0;
372 err_unregister_supply:
373 power_supply_unregister(charger->charger);
374 err_free_name:
375 if (name != charger->pdata->name)
376 kfree(name);
378 return ret;
381 static int bq24735_charger_remove(struct i2c_client *client)
383 struct bq24735 *charger = i2c_get_clientdata(client);
385 if (charger->client->irq)
386 devm_free_irq(&charger->client->dev, charger->client->irq,
387 &charger->charger);
389 power_supply_unregister(charger->charger);
391 if (charger->charger_desc.name != charger->pdata->name)
392 kfree(charger->charger_desc.name);
394 return 0;
397 static const struct i2c_device_id bq24735_charger_id[] = {
398 { "bq24735-charger", 0 },
401 MODULE_DEVICE_TABLE(i2c, bq24735_charger_id);
403 static const struct of_device_id bq24735_match_ids[] = {
404 { .compatible = "ti,bq24735", },
405 { /* end */ }
407 MODULE_DEVICE_TABLE(of, bq24735_match_ids);
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,
415 .probe = bq24735_charger_probe,
416 .remove = bq24735_charger_remove,
417 .id_table = bq24735_charger_id,
420 module_i2c_driver(bq24735_charger_driver);
422 MODULE_DESCRIPTION("bq24735 battery charging driver");
423 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
424 MODULE_LICENSE("GPL v2");