1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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/gpio/consumer.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 {
47 struct power_supply *charger;
48 struct power_supply_desc charger_desc;
49 struct i2c_client *client;
50 struct bq24735_platform *pdata;
51 struct mutex lock;
52 struct gpio_desc *status_gpio;
53 struct delayed_work poll;
54 u32 poll_interval;
55 bool charging;
56};
57
58static inline struct bq24735 *to_bq24735(struct power_supply *psy)
59{
60 return power_supply_get_drvdata(psy);
61}
62
63static enum power_supply_property bq24735_charger_properties[] = {
64 POWER_SUPPLY_PROP_STATUS,
65 POWER_SUPPLY_PROP_ONLINE,
66};
67
68static int bq24735_charger_property_is_writeable(struct power_supply *psy,
69 enum power_supply_property psp)
70{
71 switch (psp) {
72 case POWER_SUPPLY_PROP_STATUS:
73 return 1;
74 default:
75 break;
76 }
77
78 return 0;
79}
80
81static inline int bq24735_write_word(struct i2c_client *client, u8 reg,
82 u16 value)
83{
84 return i2c_smbus_write_word_data(client, reg, le16_to_cpu(value));
85}
86
87static inline int bq24735_read_word(struct i2c_client *client, u8 reg)
88{
89 s32 ret = i2c_smbus_read_word_data(client, reg);
90
91 return ret < 0 ? ret : le16_to_cpu(ret);
92}
93
94static int bq24735_update_word(struct i2c_client *client, u8 reg,
95 u16 mask, u16 value)
96{
97 unsigned int tmp;
98 int ret;
99
100 ret = bq24735_read_word(client, reg);
101 if (ret < 0)
102 return ret;
103
104 tmp = ret & ~mask;
105 tmp |= value & mask;
106
107 return bq24735_write_word(client, reg, tmp);
108}
109
110static int bq24735_config_charger(struct bq24735 *charger)
111{
112 struct bq24735_platform *pdata = charger->pdata;
113 int ret;
114 u16 value;
115
116 if (pdata->ext_control)
117 return 0;
118
119 if (pdata->charge_current) {
120 value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK;
121
122 ret = bq24735_write_word(charger->client,
123 BQ24735_CHARGE_CURRENT, value);
124 if (ret < 0) {
125 dev_err(&charger->client->dev,
126 "Failed to write charger current : %d\n",
127 ret);
128 return ret;
129 }
130 }
131
132 if (pdata->charge_voltage) {
133 value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK;
134
135 ret = bq24735_write_word(charger->client,
136 BQ24735_CHARGE_VOLTAGE, value);
137 if (ret < 0) {
138 dev_err(&charger->client->dev,
139 "Failed to write charger voltage : %d\n",
140 ret);
141 return ret;
142 }
143 }
144
145 if (pdata->input_current) {
146 value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK;
147
148 ret = bq24735_write_word(charger->client,
149 BQ24735_INPUT_CURRENT, value);
150 if (ret < 0) {
151 dev_err(&charger->client->dev,
152 "Failed to write input current : %d\n",
153 ret);
154 return ret;
155 }
156 }
157
158 return 0;
159}
160
161static inline int bq24735_enable_charging(struct bq24735 *charger)
162{
163 int ret;
164
165 if (charger->pdata->ext_control)
166 return 0;
167
168 ret = bq24735_config_charger(charger);
169 if (ret)
170 return ret;
171
172 return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
173 BQ24735_CHG_OPT_CHARGE_DISABLE, 0);
174}
175
176static inline int bq24735_disable_charging(struct bq24735 *charger)
177{
178 if (charger->pdata->ext_control)
179 return 0;
180
181 return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
182 BQ24735_CHG_OPT_CHARGE_DISABLE,
183 BQ24735_CHG_OPT_CHARGE_DISABLE);
184}
185
186static bool bq24735_charger_is_present(struct bq24735 *charger)
187{
188 if (charger->status_gpio) {
189 return !gpiod_get_value_cansleep(charger->status_gpio);
190 } else {
191 int ac = 0;
192
193 ac = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
194 if (ac < 0) {
195 dev_dbg(&charger->client->dev,
196 "Failed to read charger options : %d\n",
197 ac);
198 return false;
199 }
200 return (ac & BQ24735_CHG_OPT_AC_PRESENT) ? true : false;
201 }
202
203 return false;
204}
205
206static int bq24735_charger_is_charging(struct bq24735 *charger)
207{
208 int ret;
209
210 if (!bq24735_charger_is_present(charger))
211 return 0;
212
213 ret = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
214 if (ret < 0)
215 return ret;
216
217 return !(ret & BQ24735_CHG_OPT_CHARGE_DISABLE);
218}
219
220static void bq24735_update(struct bq24735 *charger)
221{
222 mutex_lock(&charger->lock);
223
224 if (charger->charging && bq24735_charger_is_present(charger))
225 bq24735_enable_charging(charger);
226 else
227 bq24735_disable_charging(charger);
228
229 mutex_unlock(&charger->lock);
230
231 power_supply_changed(charger->charger);
232}
233
234static irqreturn_t bq24735_charger_isr(int irq, void *devid)
235{
236 struct power_supply *psy = devid;
237 struct bq24735 *charger = to_bq24735(psy);
238
239 bq24735_update(charger);
240
241 return IRQ_HANDLED;
242}
243
244static void bq24735_poll(struct work_struct *work)
245{
246 struct bq24735 *charger = container_of(work, struct bq24735, poll.work);
247
248 bq24735_update(charger);
249
250 schedule_delayed_work(&charger->poll,
251 msecs_to_jiffies(charger->poll_interval));
252}
253
254static int bq24735_charger_get_property(struct power_supply *psy,
255 enum power_supply_property psp,
256 union power_supply_propval *val)
257{
258 struct bq24735 *charger = to_bq24735(psy);
259
260 switch (psp) {
261 case POWER_SUPPLY_PROP_ONLINE:
262 val->intval = bq24735_charger_is_present(charger) ? 1 : 0;
263 break;
264 case POWER_SUPPLY_PROP_STATUS:
265 switch (bq24735_charger_is_charging(charger)) {
266 case 1:
267 val->intval = POWER_SUPPLY_STATUS_CHARGING;
268 break;
269 case 0:
270 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
271 break;
272 default:
273 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
274 break;
275 }
276 break;
277 default:
278 return -EINVAL;
279 }
280
281 return 0;
282}
283
284static int bq24735_charger_set_property(struct power_supply *psy,
285 enum power_supply_property psp,
286 const union power_supply_propval *val)
287{
288 struct bq24735 *charger = to_bq24735(psy);
289 int ret;
290
291 switch (psp) {
292 case POWER_SUPPLY_PROP_STATUS:
293 switch (val->intval) {
294 case POWER_SUPPLY_STATUS_CHARGING:
295 mutex_lock(&charger->lock);
296 charger->charging = true;
297 ret = bq24735_enable_charging(charger);
298 mutex_unlock(&charger->lock);
299 if (ret)
300 return ret;
301 break;
302 case POWER_SUPPLY_STATUS_DISCHARGING:
303 case POWER_SUPPLY_STATUS_NOT_CHARGING:
304 mutex_lock(&charger->lock);
305 charger->charging = false;
306 ret = bq24735_disable_charging(charger);
307 mutex_unlock(&charger->lock);
308 if (ret)
309 return ret;
310 break;
311 default:
312 return -EINVAL;
313 }
314 power_supply_changed(psy);
315 break;
316 default:
317 return -EPERM;
318 }
319
320 return 0;
321}
322
323static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client)
324{
325 struct bq24735_platform *pdata;
326 struct device_node *np = client->dev.of_node;
327 u32 val;
328 int ret;
329
330 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
331 if (!pdata) {
332 dev_err(&client->dev,
333 "Memory alloc for bq24735 pdata failed\n");
334 return NULL;
335 }
336
337 ret = of_property_read_u32(np, "ti,charge-current", &val);
338 if (!ret)
339 pdata->charge_current = val;
340
341 ret = of_property_read_u32(np, "ti,charge-voltage", &val);
342 if (!ret)
343 pdata->charge_voltage = val;
344
345 ret = of_property_read_u32(np, "ti,input-current", &val);
346 if (!ret)
347 pdata->input_current = val;
348
349 pdata->ext_control = of_property_read_bool(np, "ti,external-control");
350
351 return pdata;
352}
353
354static int bq24735_charger_probe(struct i2c_client *client,
355 const struct i2c_device_id *id)
356{
357 int ret;
358 struct bq24735 *charger;
359 struct power_supply_desc *supply_desc;
360 struct power_supply_config psy_cfg = {};
361 char *name;
362
363 charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL);
364 if (!charger)
365 return -ENOMEM;
366
367 mutex_init(&charger->lock);
368 charger->charging = true;
369 charger->pdata = client->dev.platform_data;
370
371 if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node)
372 charger->pdata = bq24735_parse_dt_data(client);
373
374 if (!charger->pdata) {
375 dev_err(&client->dev, "no platform data provided\n");
376 return -EINVAL;
377 }
378
379 name = (char *)charger->pdata->name;
380 if (!name) {
381 name = devm_kasprintf(&client->dev, GFP_KERNEL,
382 "bq24735@%s",
383 dev_name(&client->dev));
384 if (!name) {
385 dev_err(&client->dev, "Failed to alloc device name\n");
386 return -ENOMEM;
387 }
388 }
389
390 charger->client = client;
391
392 supply_desc = &charger->charger_desc;
393
394 supply_desc->name = name;
395 supply_desc->type = POWER_SUPPLY_TYPE_MAINS;
396 supply_desc->properties = bq24735_charger_properties;
397 supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties);
398 supply_desc->get_property = bq24735_charger_get_property;
399 supply_desc->set_property = bq24735_charger_set_property;
400 supply_desc->property_is_writeable =
401 bq24735_charger_property_is_writeable;
402
403 psy_cfg.supplied_to = charger->pdata->supplied_to;
404 psy_cfg.num_supplicants = charger->pdata->num_supplicants;
405 psy_cfg.of_node = client->dev.of_node;
406 psy_cfg.drv_data = charger;
407
408 i2c_set_clientdata(client, charger);
409
410 charger->status_gpio = devm_gpiod_get_optional(&client->dev,
411 "ti,ac-detect",
412 GPIOD_IN);
413 if (IS_ERR(charger->status_gpio)) {
414 ret = PTR_ERR(charger->status_gpio);
415 dev_err(&client->dev, "Getting gpio failed: %d\n", ret);
416 return ret;
417 }
418
419 if (bq24735_charger_is_present(charger)) {
420 ret = bq24735_read_word(client, BQ24735_MANUFACTURER_ID);
421 if (ret < 0) {
422 dev_err(&client->dev, "Failed to read manufacturer id : %d\n",
423 ret);
424 return ret;
425 } else if (ret != 0x0040) {
426 dev_err(&client->dev,
427 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret);
428 return -ENODEV;
429 }
430
431 ret = bq24735_read_word(client, BQ24735_DEVICE_ID);
432 if (ret < 0) {
433 dev_err(&client->dev, "Failed to read device id : %d\n", ret);
434 return ret;
435 } else if (ret != 0x000B) {
436 dev_err(&client->dev,
437 "device id mismatch. 0x000b != 0x%04x\n", ret);
438 return -ENODEV;
439 }
440
441 ret = bq24735_enable_charging(charger);
442 if (ret < 0) {
443 dev_err(&client->dev, "Failed to enable charging\n");
444 return ret;
445 }
446 }
447
448 charger->charger = devm_power_supply_register(&client->dev, supply_desc,
449 &psy_cfg);
450 if (IS_ERR(charger->charger)) {
451 ret = PTR_ERR(charger->charger);
452 dev_err(&client->dev, "Failed to register power supply: %d\n",
453 ret);
454 return ret;
455 }
456
457 if (client->irq) {
458 ret = devm_request_threaded_irq(&client->dev, client->irq,
459 NULL, bq24735_charger_isr,
460 IRQF_TRIGGER_RISING |
461 IRQF_TRIGGER_FALLING |
462 IRQF_ONESHOT,
463 supply_desc->name,
464 charger->charger);
465 if (ret) {
466 dev_err(&client->dev,
467 "Unable to register IRQ %d err %d\n",
468 client->irq, ret);
469 return ret;
470 }
471 } else {
472 ret = device_property_read_u32(&client->dev, "poll-interval",
473 &charger->poll_interval);
474 if (ret)
475 return 0;
476 if (!charger->poll_interval)
477 return 0;
478
479 INIT_DELAYED_WORK(&charger->poll, bq24735_poll);
480 schedule_delayed_work(&charger->poll,
481 msecs_to_jiffies(charger->poll_interval));
482 }
483
484 return 0;
485}
486
487static int bq24735_charger_remove(struct i2c_client *client)
488{
489 struct bq24735 *charger = i2c_get_clientdata(client);
490
491 if (charger->poll_interval)
492 cancel_delayed_work_sync(&charger->poll);
493
494 return 0;
495}
496
497static const struct i2c_device_id bq24735_charger_id[] = {
498 { "bq24735-charger", 0 },
499 {}
500};
501MODULE_DEVICE_TABLE(i2c, bq24735_charger_id);
502
503static const struct of_device_id bq24735_match_ids[] = {
504 { .compatible = "ti,bq24735", },
505 { }
506};
507MODULE_DEVICE_TABLE(of, bq24735_match_ids);
508
509static struct i2c_driver bq24735_charger_driver = {
510 .driver = {
511 .name = "bq24735-charger",
512 .of_match_table = bq24735_match_ids,
513 },
514 .probe = bq24735_charger_probe,
515 .remove = bq24735_charger_remove,
516 .id_table = bq24735_charger_id,
517};
518
519module_i2c_driver(bq24735_charger_driver);
520
521MODULE_DESCRIPTION("bq24735 battery charging driver");
522MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
523MODULE_LICENSE("GPL v2");
524