Merge commit 'main-jb-2012.08.03-B4' into t114-0806
[linux-2.6.git] / drivers / power / tps80031-charger.c
1 /*
2  * drivers/power/tps80031_charger.c
3  *
4  * Battery charger driver for TI's tps80031
5  *
6  * Copyright (c) 2011-2012, NVIDIA CORPORATION.  All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/err.h>
26 #include <linux/slab.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/driver.h>
29 #include <linux/regulator/machine.h>
30 #include <linux/mfd/tps80031.h>
31 #include <linux/tps80031-charger.h>
32 #include <linux/module.h>
33
34 #define CONTROLLER_CTRL1        0xe1
35 #define CONTROLLER_STAT1        0xe3
36 #define CHARGERUSB_CTRL2        0xe9
37 #define CHARGERUSB_CTRL3        0xea
38 #define CHARGERUSB_VOREG        0xec
39 #define CHARGERUSB_VICHRG       0xed
40 #define CHARGERUSB_CINLIMIT     0xee
41 #define CHARGERUSB_CTRLLIMIT2   0xf0
42 #define CHARGERUSB_CTRLLIMIT1   0xef
43 #define CHARGERUSB_VICHRG_PC    0xdd
44 #define CONTROLLER_WDG          0xe2
45 #define LINEAR_CHRG_STS         0xde
46
47 #define TPS80031_VBUS_DET       BIT(2)
48 #define TPS80031_VAC_DET        BIT(3)
49
50 struct tps80031_charger {
51         int                     max_charge_current_mA;
52         int                     max_charge_volt_mV;
53         struct device           *dev;
54         struct regulator_dev    *rdev;
55         struct regulator_desc   reg_desc;
56         struct regulator_init_data              reg_init_data;
57         struct tps80031_charger_platform_data   *pdata;
58         int (*board_init)(void *board_data);
59         void                    *board_data;
60         int                     irq_base;
61         int                     watch_time_sec;
62         enum charging_states    state;
63         int                     charging_term_current_mA;
64         charging_callback_t     charger_cb;
65         void                    *charger_cb_data;
66 };
67
68 static struct tps80031_charger *charger_data;
69
70 static uint8_t tps80031_get_vbus_input_current_limit_code(int max_uA)
71 {
72         const uint8_t current_to_code[] = {
73                 0x0,                                /* 0 - 50 mA */
74                 0x0,  0x1,  0x2,  0x3,  0x4,  0x5,  /* 50,  100,  ..., 300mA */
75                 0x6,  0x7,  0x8,  0x9,  0xA,  0xB,  /* 350, 400,  ..., 600mA */
76                 0xC,  0xD,  0xE,  0x27, 0x37, 0x28, /* 650, 700,  ..., 900mA */
77                 0x38, 0x29, 0x39, 0x2A, 0x3A, 0x2B, /* 950, 700,  ..., 1200mA */
78                 0x3B, 0x2C, 0x3C, 0x2D, 0x3D, 0x2E, /* 1200,1250, ..., 1500mA */
79         };
80         int charge_mA;
81         uint8_t code;
82
83         charge_mA = max_uA / 1000;
84         if (charge_mA < 0)
85                 BUG();
86         else if (charge_mA < 1800)
87                 code = current_to_code[charge_mA / 50];
88         else if (charge_mA < 2100)
89                 code = 0x20; /* use 1800mA code */
90         else if (charge_mA < 2250)
91                 code = 0x21; /* use 2100mA code */
92         else
93                 code = 0x22; /* use 2250mA code */
94
95         return code;
96 };
97
98 static int set_charge_current_limit(struct regulator_dev *rdev,
99                 int min_uA, int max_uA)
100 {
101         struct tps80031_charger *charger = rdev_get_drvdata(rdev);
102         int max_charge_current = 1500;
103         uint8_t code;
104         int ret;
105
106         dev_info(charger->dev, "%s(): Min curr %dmA and max current %dmA\n",
107                 __func__, min_uA/1000, max_uA/1000);
108
109         if (!max_uA) {
110                 ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
111                                                 CONTROLLER_CTRL1, 0x0);
112                 if (ret < 0)
113                         dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
114                                 __func__, CONTROLLER_CTRL1);
115
116                 ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
117                                                 CONTROLLER_WDG, 0x0);
118                 if (ret < 0)
119                         dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
120                                 __func__, CONTROLLER_WDG);
121                 charger->state = charging_state_charging_stopped;
122                 if (charger->charger_cb)
123                         charger->charger_cb(charger->state,
124                                         charger->charger_cb_data);
125                 return ret;
126         }
127
128         code = tps80031_get_vbus_input_current_limit_code(max_uA);
129         ret = tps80031_update(charger->dev->parent, SLAVE_ID2,
130                         CHARGERUSB_CINLIMIT, code, 0x3F);
131         if (ret < 0) {
132                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
133                                 __func__, CHARGERUSB_CINLIMIT);
134                 return ret;
135         }
136
137         max_charge_current = min(max_uA/1000, max_charge_current);
138         if (max_charge_current <= 300)
139                 max_charge_current = 0;
140         else if ((max_charge_current > 300) && (max_charge_current <= 500))
141                 max_charge_current = (max_charge_current - 300)/50;
142         else
143                 max_charge_current = (max_charge_current - 500) / 100 + 4;
144         ret = tps80031_update(charger->dev->parent, SLAVE_ID2,
145                         CHARGERUSB_VICHRG, (uint8_t)max_charge_current, 0xF);
146         if (ret < 0) {
147                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
148                                 __func__, CHARGERUSB_VICHRG);
149                 return ret;
150         }
151
152         /* Enable watchdog timer */
153         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
154                                 CONTROLLER_WDG, charger->watch_time_sec);
155         if (ret < 0) {
156                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
157                                 __func__, CONTROLLER_WDG);
158                 return ret;
159         }
160
161         /* Enable the charging */
162         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
163                                 CONTROLLER_CTRL1, 0x30);
164         if (ret < 0) {
165                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
166                                 __func__, CONTROLLER_CTRL1);
167                 return ret;
168         }
169         charger->state = charging_state_charging_in_progress;
170         if (charger->charger_cb)
171                 charger->charger_cb(charger->state,
172                                 charger->charger_cb_data);
173         return 0;
174 }
175
176 static struct regulator_ops tegra_regulator_ops = {
177         .set_current_limit = set_charge_current_limit,
178 };
179
180 int register_charging_state_callback(charging_callback_t cb, void *args)
181 {
182         struct tps80031_charger *charger = charger_data;
183         if (!charger_data)
184                 return -ENODEV;
185
186         charger->charger_cb = cb;
187         charger->charger_cb_data = args;
188         return 0;
189 }
190 EXPORT_SYMBOL_GPL(register_charging_state_callback);
191
192 static int configure_charging_parameter(struct tps80031_charger *charger)
193 {
194         int ret;
195         int max_charge_current;
196         int max_charge_volt;
197         int term_current;
198
199         /* Disable watchdog timer */
200         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
201                                 CONTROLLER_WDG, 0x0);
202         if (ret < 0) {
203                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
204                                 __func__, CONTROLLER_WDG);
205                 return ret;
206         }
207
208         /* Disable the charging if any */
209         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
210                                 CONTROLLER_CTRL1, 0x0);
211         if (ret < 0) {
212                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
213                                 __func__, CONTROLLER_CTRL1);
214                 return ret;
215         }
216
217         if (charger->board_init) {
218                 ret = charger->board_init(charger->board_data);
219                 if (ret < 0) {
220                         dev_err(charger->dev, "%s(): Failed in board init\n",
221                                 __func__);
222                         return ret;
223                 }
224         }
225
226         /* Unlock value */
227         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
228                         CHARGERUSB_CTRLLIMIT2, 0);
229         if (ret < 0) {
230                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
231                                 __func__, CHARGERUSB_CTRLLIMIT2);
232                 return ret;
233         }
234
235         /* Set max current limit */
236         max_charge_current = min(1500, charger->max_charge_current_mA);
237         if (max_charge_current < 100)
238                 max_charge_current = 0;
239         else
240                 max_charge_current = (max_charge_current - 100)/100;
241         max_charge_current &= 0xF;
242         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
243                 CHARGERUSB_CTRLLIMIT2, (uint8_t)max_charge_current);
244         if (ret < 0) {
245                 dev_err(charger->dev, "%s(): Failed in writing register "
246                                 "0x%02x\n", __func__, CHARGERUSB_CTRLLIMIT2);
247                 return ret;
248         }
249
250         /* Set max voltage limit */
251         max_charge_volt = min(4760, charger->max_charge_volt_mV);
252         max_charge_volt = max(3500, max_charge_volt);
253         max_charge_volt -= 3500;
254         max_charge_volt = max_charge_volt/20;
255         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
256                 CHARGERUSB_CTRLLIMIT1, (uint8_t)max_charge_volt);
257         if (ret < 0) {
258                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
259                                 __func__, CHARGERUSB_CTRLLIMIT1);
260                 return ret;
261         }
262
263         /* Lock value */
264         ret = tps80031_set_bits(charger->dev->parent, SLAVE_ID2,
265                         CHARGERUSB_CTRLLIMIT2, (1 << 4));
266         if (ret < 0) {
267                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
268                                 __func__, CHARGERUSB_CTRLLIMIT2);
269                 return ret;
270         }
271
272         /* set Pre Charge current to 400mA */
273         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
274                         CHARGERUSB_VICHRG_PC, 0x3);
275         if (ret < 0) {
276                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
277                                 __func__, 0xDD);
278                 return ret;
279         }
280
281         /* set charging termination current*/
282         if (charger->charging_term_current_mA > 400)
283                 term_current =  7;
284         else
285                 term_current = (charger->charging_term_current_mA - 50)/50;
286         term_current = term_current << 5;
287         ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
288                         CHARGERUSB_CTRL2, term_current);
289         if (ret < 0) {
290                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
291                                 __func__, CHARGERUSB_CTRL2);
292                 return ret;
293         }
294
295         return 0;
296 }
297
298 static irqreturn_t linch_status_isr(int irq, void *dev_id)
299 {
300         struct tps80031_charger *charger = dev_id;
301         uint8_t linch_status;
302         int ret;
303         dev_info(charger->dev, "%s() got called\n", __func__);
304
305         ret = tps80031_read(charger->dev->parent, SLAVE_ID2,
306                         LINEAR_CHRG_STS, &linch_status);
307         if (ret < 0) {
308                 dev_err(charger->dev, "%s(): Failed in reading register 0x%02x\n",
309                                 __func__, LINEAR_CHRG_STS);
310         } else {
311                 dev_info(charger->dev, "%s():The status of LINEAR_CHRG_STS is 0x%02x\n",
312                                  __func__, linch_status);
313                 if (linch_status & 0x20) {
314                         charger->state = charging_state_charging_completed;
315                         if (charger->charger_cb)
316                                 charger->charger_cb(charger->state,
317                                         charger->charger_cb_data);
318                 }
319         }
320
321         return IRQ_HANDLED;
322 }
323
324 static irqreturn_t watchdog_expire_isr(int irq, void *dev_id)
325 {
326         struct tps80031_charger *charger = dev_id;
327         int ret;
328
329         dev_info(charger->dev, "%s()\n", __func__);
330         if (charger->state != charging_state_charging_in_progress)
331                 return IRQ_HANDLED;
332
333         /* Enable watchdog timer again*/
334         ret = tps80031_write(charger->dev->parent, SLAVE_ID2, CONTROLLER_WDG,
335                         charger->watch_time_sec);
336         if (ret < 0)
337                 dev_err(charger->dev, "%s(): Failed in writing register 0x%02x\n",
338                                 __func__, CONTROLLER_WDG);
339
340         /* Rewrite to enable the charging */
341         if (!ret) {
342                 ret = tps80031_write(charger->dev->parent, SLAVE_ID2,
343                         CONTROLLER_CTRL1, 0x30);
344                 if (ret < 0)
345                         dev_err(charger->dev, "%s(): Failed in writing "
346                                 "register 0x%02x\n",
347                                 __func__, CONTROLLER_CTRL1);
348         }
349         return IRQ_HANDLED;
350 }
351
352 static int tps80031_charger_probe(struct platform_device *pdev)
353 {
354         int ret = 0;
355         struct device *dev = &pdev->dev;
356         struct tps80031_charger *charger;
357         struct tps80031_charger_platform_data *pdata = pdev->dev.platform_data;
358
359         dev_info(dev, "%s()\n", __func__);
360
361         if (!pdata) {
362                 dev_err(dev, "%s() No platform data, exiting..\n", __func__);
363                 return -ENODEV;
364         }
365
366         if (!pdata->num_consumer_supplies) {
367                 dev_err(dev, "%s() No consumer supply list, exiting..\n",
368                                 __func__);
369                 return -ENODEV;
370         }
371
372         charger = kzalloc(sizeof(*charger), GFP_KERNEL);
373         if (!charger) {
374                 dev_err(dev, "failed to allocate memory status\n");
375                 return -ENOMEM;
376         }
377
378         charger->dev =  &pdev->dev;
379
380         charger->max_charge_current_mA = (pdata->max_charge_current_mA) ?
381                                         pdata->max_charge_current_mA : 1000;
382         charger->max_charge_volt_mV = (pdata->max_charge_volt_mV) ?
383                                         pdata->max_charge_volt_mV : 4200;
384         charger->irq_base = pdata->irq_base;
385         charger->watch_time_sec = min(pdata->watch_time_sec, 127);
386         if (!charger->watch_time_sec)
387                 charger->watch_time_sec = 127;
388         charger->charging_term_current_mA =
389                         min(50, pdata->charging_term_current_mA);
390         if (charger->charging_term_current_mA < 50)
391                 charger->charging_term_current_mA = 50;
392
393         charger->reg_desc.name = "vbus_charger";
394         charger->reg_desc.id = pdata->regulator_id;
395         charger->reg_desc.ops = &tegra_regulator_ops;
396         charger->reg_desc.type = REGULATOR_CURRENT;
397         charger->reg_desc.owner = THIS_MODULE;
398
399         charger->reg_init_data.supply_regulator = NULL;
400         charger->reg_init_data.num_consumer_supplies =
401                                         pdata->num_consumer_supplies;
402         charger->reg_init_data.consumer_supplies = pdata->consumer_supplies;
403         charger->reg_init_data.regulator_init = NULL;
404         charger->reg_init_data.driver_data = charger;
405         charger->reg_init_data.constraints.name = "vbus_charger";
406         charger->reg_init_data.constraints.min_uA = 0;
407         charger->reg_init_data.constraints.max_uA =
408                                         pdata->max_charge_current_mA * 1000;
409         charger->reg_init_data.constraints.valid_modes_mask =
410                                         REGULATOR_MODE_NORMAL |
411                                         REGULATOR_MODE_STANDBY;
412         charger->reg_init_data.constraints.valid_ops_mask =
413                                         REGULATOR_CHANGE_MODE |
414                                         REGULATOR_CHANGE_STATUS |
415                                         REGULATOR_CHANGE_CURRENT;
416
417         charger->board_init = pdata->board_init;
418         charger->board_data = pdata->board_data;
419         charger->state = charging_state_idle;
420
421         charger->rdev = regulator_register(&charger->reg_desc, &pdev->dev,
422                                         &charger->reg_init_data, charger, NULL);
423         if (IS_ERR(charger->rdev)) {
424                 dev_err(&pdev->dev, "failed to register %s\n",
425                                                 charger->reg_desc.name);
426                 ret = PTR_ERR(charger->rdev);
427                 goto regulator_fail;
428         }
429
430         ret = request_threaded_irq(charger->irq_base + TPS80031_INT_LINCH_GATED,
431                         NULL, linch_status_isr, 0, "tps80031-linch", charger);
432         if (ret) {
433                 dev_err(&pdev->dev, "Unable to register irq %d; error %d\n",
434                         charger->irq_base + TPS80031_INT_LINCH_GATED, ret);
435                 goto irq_linch_fail;
436         }
437
438         ret = request_threaded_irq(charger->irq_base + TPS80031_INT_FAULT_WDG,
439                         NULL, watchdog_expire_isr, 0, "tps80031-wdg", charger);
440         if (ret) {
441                 dev_err(&pdev->dev, "Unable to register irq %d; error %d\n",
442                         charger->irq_base + TPS80031_INT_FAULT_WDG, ret);
443                 goto irq_wdg_fail;
444         }
445
446         ret = configure_charging_parameter(charger);
447         if (ret)
448                 goto config_fail;
449
450         dev_set_drvdata(&pdev->dev, charger);
451         charger_data = charger;
452         return ret;
453
454 config_fail:
455         free_irq(charger->irq_base + TPS80031_INT_FAULT_WDG, charger);
456 irq_wdg_fail:
457         free_irq(charger->irq_base + TPS80031_INT_LINCH_GATED, charger);
458 irq_linch_fail:
459         regulator_unregister(charger->rdev);
460 regulator_fail:
461         kfree(charger);
462         return ret;
463 }
464
465 static int tps80031_charger_remove(struct platform_device *pdev)
466 {
467         struct tps80031_charger *charger = dev_get_drvdata(&pdev->dev);
468
469         regulator_unregister(charger->rdev);
470         kfree(charger);
471         return 0;
472 }
473
474 static struct platform_driver tps80031_charger_driver = {
475         .driver = {
476                 .name   = "tps80031-charger",
477                 .owner  = THIS_MODULE,
478         },
479         .probe  = tps80031_charger_probe,
480         .remove = tps80031_charger_remove,
481 };
482
483 static int __init tps80031_charger_init(void)
484 {
485         return platform_driver_register(&tps80031_charger_driver);
486 }
487
488 static void __exit tps80031_charger_exit(void)
489 {
490         platform_driver_unregister(&tps80031_charger_driver);
491 }
492
493 subsys_initcall(tps80031_charger_init);
494 module_exit(tps80031_charger_exit);
495
496 MODULE_LICENSE("GPL");
497 MODULE_DESCRIPTION("tps80031 battery charger driver");