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