blob: eb2b3689de975ac1b670b2a2226b82734b7fc58e [file] [log] [blame]
Darbha Sriharshace773992013-10-11 17:15:43 -04001/*
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
46struct bq24735 {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010047 struct power_supply *charger;
48 struct power_supply_desc charger_desc;
49 struct i2c_client *client;
50 struct bq24735_platform *pdata;
Darbha Sriharshace773992013-10-11 17:15:43 -040051};
52
53static inline struct bq24735 *to_bq24735(struct power_supply *psy)
54{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010055 return power_supply_get_drvdata(psy);
Darbha Sriharshace773992013-10-11 17:15:43 -040056}
57
58static enum power_supply_property bq24735_charger_properties[] = {
59 POWER_SUPPLY_PROP_ONLINE,
60};
61
62static 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
68static 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
75static 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
91static 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
98static 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
105static 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
153static 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
177static 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
192static int bq24735_charger_get_property(struct power_supply *psy,
193 enum power_supply_property psp,
194 union power_supply_propval *val)
195{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100196 struct bq24735 *charger = to_bq24735(psy);
Darbha Sriharshace773992013-10-11 17:15:43 -0400197
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
209static 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
245static int bq24735_charger_probe(struct i2c_client *client,
246 const struct i2c_device_id *id)
247{
248 int ret;
249 struct bq24735 *charger;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100250 struct power_supply_desc *supply_desc;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100251 struct power_supply_config psy_cfg = {};
Darbha Sriharshace773992013-10-11 17:15:43 -0400252 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) {
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530270 name = devm_kasprintf(&client->dev, GFP_KERNEL,
271 "bq24735@%s",
272 dev_name(&client->dev));
Darbha Sriharshace773992013-10-11 17:15:43 -0400273 if (!name) {
274 dev_err(&client->dev, "Failed to alloc device name\n");
275 return -ENOMEM;
276 }
277 }
278
279 charger->client = client;
280
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100281 supply_desc = &charger->charger_desc;
Darbha Sriharshace773992013-10-11 17:15:43 -0400282
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100283 supply_desc->name = name;
284 supply_desc->type = POWER_SUPPLY_TYPE_MAINS;
285 supply_desc->properties = bq24735_charger_properties;
286 supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties);
287 supply_desc->get_property = bq24735_charger_get_property;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100288
289 psy_cfg.supplied_to = charger->pdata->supplied_to;
290 psy_cfg.num_supplicants = charger->pdata->num_supplicants;
291 psy_cfg.of_node = client->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100292 psy_cfg.drv_data = charger;
Darbha Sriharshace773992013-10-11 17:15:43 -0400293
294 i2c_set_clientdata(client, charger);
295
296 ret = bq24735_read_word(client, BQ24735_MANUFACTURER_ID);
297 if (ret < 0) {
298 dev_err(&client->dev, "Failed to read manufacturer id : %d\n",
299 ret);
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530300 return ret;
Darbha Sriharshace773992013-10-11 17:15:43 -0400301 } else if (ret != 0x0040) {
302 dev_err(&client->dev,
303 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret);
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530304 return -ENODEV;
Darbha Sriharshace773992013-10-11 17:15:43 -0400305 }
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);
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530310 return ret;
Darbha Sriharshace773992013-10-11 17:15:43 -0400311 } else if (ret != 0x000B) {
312 dev_err(&client->dev,
313 "device id mismatch. 0x000b != 0x%04x\n", ret);
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530314 return -ENODEV;
Darbha Sriharshace773992013-10-11 17:15:43 -0400315 }
316
317 if (gpio_is_valid(charger->pdata->status_gpio)) {
318 ret = devm_gpio_request(&client->dev,
319 charger->pdata->status_gpio,
320 name);
321 if (ret) {
322 dev_err(&client->dev,
323 "Failed GPIO request for GPIO %d: %d\n",
324 charger->pdata->status_gpio, ret);
325 }
326
327 charger->pdata->status_gpio_valid = !ret;
328 }
329
330 ret = bq24735_config_charger(charger);
331 if (ret < 0) {
332 dev_err(&client->dev, "failed in configuring charger");
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530333 return ret;
Darbha Sriharshace773992013-10-11 17:15:43 -0400334 }
335
336 /* check for AC adapter presence */
337 if (bq24735_charger_is_present(charger)) {
338 ret = bq24735_enable_charging(charger);
339 if (ret < 0) {
340 dev_err(&client->dev, "Failed to enable charging\n");
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530341 return ret;
Darbha Sriharshace773992013-10-11 17:15:43 -0400342 }
343 }
344
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530345 charger->charger = devm_power_supply_register(&client->dev, supply_desc,
346 &psy_cfg);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100347 if (IS_ERR(charger->charger)) {
348 ret = PTR_ERR(charger->charger);
Darbha Sriharshace773992013-10-11 17:15:43 -0400349 dev_err(&client->dev, "Failed to register power supply: %d\n",
350 ret);
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530351 return ret;
Darbha Sriharshace773992013-10-11 17:15:43 -0400352 }
353
354 if (client->irq) {
355 ret = devm_request_threaded_irq(&client->dev, client->irq,
356 NULL, bq24735_charger_isr,
357 IRQF_TRIGGER_RISING |
358 IRQF_TRIGGER_FALLING |
359 IRQF_ONESHOT,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100360 supply_desc->name,
361 charger->charger);
Darbha Sriharshace773992013-10-11 17:15:43 -0400362 if (ret) {
363 dev_err(&client->dev,
364 "Unable to register IRQ %d err %d\n",
365 client->irq, ret);
Vaishali Thakkar71a5a502015-07-25 09:23:16 +0530366 return ret;
Darbha Sriharshace773992013-10-11 17:15:43 -0400367 }
368 }
369
370 return 0;
Darbha Sriharshace773992013-10-11 17:15:43 -0400371}
372
373static const struct i2c_device_id bq24735_charger_id[] = {
374 { "bq24735-charger", 0 },
375 {}
376};
377MODULE_DEVICE_TABLE(i2c, bq24735_charger_id);
378
379static const struct of_device_id bq24735_match_ids[] = {
380 { .compatible = "ti,bq24735", },
381 { /* end */ }
382};
383MODULE_DEVICE_TABLE(of, bq24735_match_ids);
384
385static struct i2c_driver bq24735_charger_driver = {
386 .driver = {
387 .name = "bq24735-charger",
Darbha Sriharshace773992013-10-11 17:15:43 -0400388 .of_match_table = bq24735_match_ids,
389 },
390 .probe = bq24735_charger_probe,
Darbha Sriharshace773992013-10-11 17:15:43 -0400391 .id_table = bq24735_charger_id,
392};
393
394module_i2c_driver(bq24735_charger_driver);
395
396MODULE_DESCRIPTION("bq24735 battery charging driver");
397MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
398MODULE_LICENSE("GPL v2");