drivers: power: max77665: enable interrupts in charger
[linux-2.6.git] / drivers / power / max77665-charger.c
1 /*
2  * max77665-charger.c - Battery charger driver
3  *
4  *  Copyright (C) 2012 nVIDIA corporation
5  *  Syed Rafiuddin <srafiuddin@nvidia.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/err.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/platform_device.h>
26 #include <linux/power_supply.h>
27 #include <linux/mfd/max77665.h>
28 #include <linux/max77665-charger.h>
29 #include <linux/power/max17042_battery.h>
30 #include <linux/interrupt.h>
31
32 /* fast charge current in mA */
33 static const uint32_t chg_cc[]  = {
34         0, 33, 66, 99, 133, 166, 199, 233, 266, 299,
35         333, 366, 399, 432, 466, 499, 532, 566, 599, 632,
36         666, 699, 732, 765, 799, 832, 865, 899, 932, 965,
37         999, 1032, 1065, 1098, 1132, 1165, 1198, 1232, 1265,
38         1298, 1332, 1365, 1398, 1421, 1465, 1498, 1531, 1565,
39         1598, 1631, 1665, 1698, 1731, 1764, 1798, 1831, 1864,
40         1898, 1931, 1964, 1998, 2031, 2064, 2097,
41 };
42
43 /* primary charge termination voltage in mV */
44 static const uint32_t chg_cv_prm[] = {
45         3650, 3675, 3700, 3725, 3750,
46         3775, 3800, 3825, 3850, 3875,
47         3900, 3925, 3950, 3975, 4000,
48         4025, 4050, 4075, 4100, 4125,
49         4150, 4175, 4200, 4225, 4250,
50         4275, 4300, 4325, 4340, 4350,
51         4375, 4400,
52 };
53
54 /* maxim input current limit in mA*/
55 static const uint32_t chgin_ilim[] = {
56         0, 100, 200, 300, 400, 500, 600, 700,
57         800, 900, 1000, 1100, 1200, 1300, 1400,
58         1500, 1600, 1700, 1800, 1900, 2000, 2100,
59         2200, 2300, 2400, 2500,
60 };
61
62 struct max77665_charger {
63         struct device           *dev;
64         struct power_supply     ac;
65         struct power_supply     usb;
66         struct max77665_charger_plat_data *plat_data;
67         uint8_t ac_online;
68         uint8_t usb_online;
69         uint8_t num_cables;
70         struct extcon_dev *edev;
71 };
72
73 static enum power_supply_property max77665_ac_props[] = {
74         POWER_SUPPLY_PROP_ONLINE,
75 };
76
77 static enum power_supply_property max77665_usb_props[] = {
78         POWER_SUPPLY_PROP_ONLINE,
79 };
80
81 static int max77665_write_reg(struct max77665_charger *charger,
82         uint8_t reg, uint8_t value)
83 {
84         int ret = 0;
85         struct device *dev = charger->dev;
86
87         ret = max77665_write(dev->parent, MAX77665_I2C_SLAVE_PMIC, reg, value);
88         if (ret < 0)
89                 return ret;
90         return ret;
91 }
92
93 static int max77665_read_reg(struct max77665_charger *charger,
94         uint8_t reg, uint32_t *value)
95 {
96         int ret = 0;
97         uint8_t read_val;
98
99         struct device *dev = charger->dev;
100
101         ret = max77665_read(dev->parent, MAX77665_I2C_SLAVE_PMIC,
102                         reg, &read_val);
103         if (!ret)
104                 *value = read_val;
105
106         return ret;
107 }
108
109 static int max77665_update_reg(struct max77665_charger *charger,
110         uint8_t reg, uint8_t value)
111 {
112         int ret = 0;
113         uint8_t read_val;
114
115         struct device *dev = charger->dev;
116
117         ret = max77665_read(dev->parent, MAX77665_I2C_SLAVE_PMIC,
118                         reg, &read_val);
119         if (ret)
120                 return ret;
121
122         ret = max77665_write(dev->parent, MAX77665_I2C_SLAVE_PMIC, reg,
123                         read_val | value);
124         if (ret)
125                 return ret;
126
127         return ret;
128 }
129
130 /* Convert current to register value using lookup table */
131 static int convert_to_reg(const unsigned int *tbl, size_t size,
132                 unsigned int val)
133 {
134         size_t i;
135
136         for (i = 0; i < size; i++)
137                 if (val < tbl[i])
138                         break;
139         return i > 0 ? i - 1 : -EINVAL;
140 }
141
142 static int max77665_ac_get_property(struct power_supply *psy,
143                 enum power_supply_property psp,
144                                 union power_supply_propval *val)
145 {
146         struct max77665_charger *chip = container_of(psy,
147                                 struct max77665_charger, ac);
148
149         if (psp == POWER_SUPPLY_PROP_ONLINE)
150                 val->intval = chip->ac_online;
151         else
152                 return -EINVAL;
153
154         return 0;
155 }
156
157 static int max77665_usb_get_property(struct power_supply *psy,
158                                 enum power_supply_property psp,
159                                 union power_supply_propval *val)
160 {
161         struct max77665_charger *chip = container_of(psy,
162                                         struct max77665_charger, usb);
163
164         if (psp == POWER_SUPPLY_PROP_ONLINE)
165                 val->intval = chip->usb_online;
166         else
167                 return -EINVAL;
168
169         return 0;
170 }
171
172 static int max77665_enable_write(struct max77665_charger *charger, int access)
173 {
174         int ret = 0;
175
176         if (access) {
177                 /* enable write acces to registers */
178                 ret = max77665_write_reg(charger, MAX77665_CHG_CNFG_06, 0x0c);
179                 if (ret < 0) {
180                         dev_err(charger->dev, "Failed to write to reg 0x%x\n",
181                                 MAX77665_CHG_CNFG_06);
182                         return ret;
183                 }
184         } else {
185                 /* Disable write acces to registers */
186                 ret = max77665_write_reg(charger, MAX77665_CHG_CNFG_06, 0x00);
187                 if (ret < 0) {
188                         dev_err(charger->dev, "Failed to write to reg 0x%x\n",
189                                 MAX77665_CHG_CNFG_06);
190                         return ret;
191                 }
192         }
193         return 0;
194 }
195
196 static int max77665_charger_enable(struct max77665_charger *charger,
197                 enum max77665_mode mode)
198 {
199         int ret;
200
201         ret = max77665_enable_write(charger, true);
202         if (ret < 0) {
203                 dev_err(charger->dev, "failed to enable write acess\n");
204                 return ret;
205         }
206
207         if (mode == CHARGER) {
208                 /* enable charging */
209                 ret = max77665_write_reg(charger, MAX77665_CHG_CNFG_00, 0x05);
210                 if (ret < 0) {
211                         dev_err(charger->dev, "Failed in wirting to register 0x%x:\n",
212                                         MAX77665_CHG_CNFG_00);
213                         return ret;
214                 }
215         } else if (mode == OTG) {
216                 /* enable OTG mode */
217                 ret = max77665_write_reg(charger, MAX77665_CHG_CNFG_00, 0x2A);
218                 if (ret < 0) {
219                         dev_err(charger->dev, "Failed in writing to register 0x%x:\n",
220                                          MAX77665_CHG_CNFG_00);
221                         return ret;
222                 }
223         }
224
225         ret = max77665_enable_write(charger, false);
226         if (ret < 0) {
227                 dev_err(charger->dev, "failed to disable write acess\n");
228                 return ret;
229         }
230         return 0;
231 }
232
233 static int max77665_charger_init(struct max77665_charger *charger)
234 {
235         int ret = 0;
236
237         ret = max77665_enable_write(charger, true);
238         if (ret < 0) {
239                 dev_err(charger->dev, "failed to enable write acess\n");
240                 goto error;
241         }
242
243         ret = max77665_update_reg(charger, MAX77665_CHG_CNFG_01, 0xa4);
244         if (ret < 0) {
245                 dev_err(charger->dev, "Failed in writing to register 0x%x\n",
246                         MAX77665_CHG_CNFG_01);
247                 goto error;
248         }
249
250         if (charger->plat_data->fast_chg_cc) {
251                 ret = convert_to_reg(chg_cc, ARRAY_SIZE(chg_cc),
252                                         charger->plat_data->fast_chg_cc);
253                 if (ret < 0)
254                         goto error;
255
256                 ret = max77665_update_reg(charger, MAX77665_CHG_CNFG_02, ret);
257                 if (ret < 0) {
258                         dev_err(charger->dev, "Failed in writing to register 0x%x\n",
259                                 MAX77665_CHG_CNFG_02);
260                         goto error;
261                 }
262         }
263
264         if (charger->plat_data->term_volt) {
265                 ret = convert_to_reg(chg_cv_prm, ARRAY_SIZE(chg_cv_prm),
266                                         charger->plat_data->term_volt);
267                 if (ret < 0)
268                         goto error;
269
270                 ret = max77665_update_reg(charger,
271                                 MAX77665_CHG_CNFG_04, ret+1);
272                 if (ret < 0) {
273                         dev_err(charger->dev, "Failed writing to reg:0x%x\n",
274                                 MAX77665_CHG_CNFG_04);
275                         goto error;
276                 }
277         }
278
279         if (charger->plat_data->curr_lim) {
280                 ret = convert_to_reg(chgin_ilim, ARRAY_SIZE(chgin_ilim),
281                                         charger->plat_data->curr_lim);
282                 if (ret < 0)
283                         goto error;
284
285                 ret = max77665_write_reg(charger,
286                                 MAX77665_CHG_CNFG_09, ret*5);
287                 if (ret < 0) {
288                         dev_err(charger->dev, "Failed writing to reg:0x%x\n",
289                                 MAX77665_CHG_CNFG_09);
290                         goto error;
291                 }
292         }
293 error:
294         ret = max77665_enable_write(charger, false);
295         if (ret < 0) {
296                 dev_err(charger->dev, "failed to enable write acess\n");
297                 return ret;
298         }
299         return ret;
300 }
301
302 static int max77665_enable_charger(struct max77665_charger *charger)
303 {
304         int ret = 0;
305
306         if (charger->plat_data->update_status)
307                         charger->plat_data->update_status(false);
308
309         if (extcon_get_cable_state(charger->edev, "USB")) {
310
311                 ret = max77665_charger_enable(charger, CHARGER);
312                 if (ret < 0)
313                         goto error;
314
315                 charger->usb_online = 1;
316                 power_supply_changed(&charger->usb);
317                 charger->plat_data->curr_lim = 500;
318                 if (charger->plat_data->update_status)
319                         charger->plat_data->update_status(true);
320         }
321
322         if (extcon_get_cable_state(charger->edev, "USB-Host")) {
323                 ret = max77665_charger_enable(charger, OTG);
324                 if (ret < 0)
325                         goto error;
326         }
327
328         if (extcon_get_cable_state(charger->edev, "TA")) {
329                 ret = max77665_charger_enable(charger, CHARGER);
330                 if (ret < 0)
331                         goto error;
332
333                 charger->ac_online = 1;
334                 power_supply_changed(&charger->ac);
335                 if (charger->plat_data->update_status)
336                         charger->plat_data->update_status(true);
337         }
338
339         ret = max77665_charger_init(charger);
340         if (ret < 0) {
341                 dev_err(charger->dev, "failed to initialize charger\n");
342                 goto error;
343         }
344
345         return 0;
346 error:
347         return ret;
348 }
349
350 static int charger_extcon_notifier(struct notifier_block *self,
351                 unsigned long event, void *ptr)
352 {
353         return NOTIFY_DONE;
354 }
355
356 static irqreturn_t max77665_charger_irq_handler(int irq, void *data)
357 {
358         struct max77665_charger *charger = data;
359         int ret;
360         uint8_t read_val;
361
362         ret = max77665_read_reg(charger, MAX77665_CHG_INT_OK, &read_val);
363         if (ret < 0) {
364                 dev_err(charger->dev, "failed to write to reg: 0x%x\n",
365                                 MAX77665_CHG_INT_OK);
366                 goto error;
367         }
368
369         if (read_val & 0x40) {
370                 charger->usb_online = 1;
371                 power_supply_changed(&charger->usb);
372                 power_supply_changed(&charger->ac);
373                 ret = max77665_enable_charger(charger);
374                 if (ret < 0)
375                         goto error;
376         } else {
377                 charger->ac_online = 0;
378                 charger->usb_online = 0;
379                 if (charger->plat_data->update_status)
380                         charger->plat_data->update_status(false);
381                 power_supply_changed(&charger->usb);
382                 power_supply_changed(&charger->ac);
383         }
384
385         ret = max77665_read_reg(charger, MAX77665_CHG_INT, &read_val);
386         if (ret < 0) {
387                 dev_err(charger->dev, "failed in reading register: 0x%x\n",
388                                 MAX77665_CHG_INT);
389                 goto error;
390         }
391
392         ret = max77665_write_reg(charger, MAX77665_CHG_INT_MASK, 0x0a);
393         if (ret < 0) {
394                 dev_err(charger->dev, "failed to write to reg: 0x%x\n",
395                                 MAX77665_CHG_INT_MASK);
396                 goto error;
397         }
398 error:
399         return IRQ_HANDLED;
400 }
401
402 static __devinit int max77665_battery_probe(struct platform_device *pdev)
403 {
404         int ret = 0;
405         uint8_t j;
406         uint32_t read_val;
407         struct max77665_charger *charger;
408
409         charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
410         if (!charger) {
411                 dev_err(&pdev->dev, "failed to allocate memory status\n");
412                 return -ENOMEM;
413         }
414
415         charger->dev = &pdev->dev;
416
417         charger->plat_data = pdev->dev.platform_data;
418         dev_set_drvdata(&pdev->dev, charger);
419
420         /* modify OTP setting of input current limit to 100ma */
421         ret = max77665_write_reg(charger, MAX77665_CHG_CNFG_09, 0x05);
422         if (ret < 0) {
423                 dev_err(charger->dev, "failed to write to reg: 0x%x\n",
424                                 MAX77665_CHG_CNFG_09);
425                 goto error;
426         }
427
428         /* check for battery presence */
429         ret = max77665_read_reg(charger, MAX77665_CHG_DTLS_01, &read_val);
430         if (ret) {
431                 dev_err(&pdev->dev, "error in reading register 0x%x\n",
432                                 MAX77665_CHG_DTLS_01);
433                 return -ENODEV;
434         } else if (!(read_val & 0xe0)) {
435                 dev_err(&pdev->dev, "Battery not detected exiting driver..\n");
436                 return -ENODEV;
437         }
438
439         /* differentiate between E1236 and E1587*/
440         ret = maxim_get_temp();
441         if (ret == 0xff) {
442                 dev_err(&pdev->dev, "failed in reading temperaure\n");
443                 return -ENODEV;
444         } else if ((ret < MIN_TEMP) || (ret > MAX_TEMP)) {
445                         dev_err(&pdev->dev, "E1236 detected exiting driver....\n");
446                         return -ENODEV;
447         }
448
449         ret = max77665_write_reg(charger, MAX77665_CHG_INT_MASK, 0x0a);
450         if (ret < 0) {
451                 dev_err(charger->dev, "failed to write to reg: 0x%x\n",
452                                 MAX77665_CHG_INT_MASK);
453                 goto error;
454         }
455
456         if (charger->plat_data->irq_base) {
457                 ret = request_threaded_irq(charger->plat_data->irq_base +
458                                 MAX77665_IRQ_CHARGER, NULL,
459                                 max77665_charger_irq_handler,
460                                 0, "charger_irq",
461                                         charger);
462                 if (ret) {
463                         dev_err(&pdev->dev,
464                                 "failed: irq request error :%d)\n", ret);
465                         goto chrg_error;
466                 }
467         }
468         charger->ac.name                = "ac";
469         charger->ac.type                = POWER_SUPPLY_TYPE_MAINS;
470         charger->ac.get_property        = max77665_ac_get_property;
471         charger->ac.properties          = max77665_ac_props;
472         charger->ac.num_properties      = ARRAY_SIZE(max77665_ac_props);
473
474         ret = power_supply_register(charger->dev, &charger->ac);
475         if (ret) {
476                 dev_err(charger->dev, "failed: power supply register\n");
477                 goto error;
478         }
479
480         charger->usb.name               = "usb";
481         charger->usb.type               = POWER_SUPPLY_TYPE_USB;
482         charger->usb.get_property       = max77665_usb_get_property;
483         charger->usb.properties         = max77665_usb_props;
484         charger->usb.num_properties     = ARRAY_SIZE(max77665_usb_props);
485
486         ret = power_supply_register(charger->dev, &charger->usb);
487         if (ret) {
488                 dev_err(charger->dev, "failed: power supply register\n");
489                 goto pwr_sply_error;
490         }
491
492         for (j = 0 ; j < charger->plat_data->num_cables; j++) {
493                 struct max77665_charger_cable *cable =
494                                 &charger->plat_data->cables[j];
495
496                 cable->nb.notifier_call = charger_extcon_notifier;
497                 ret = extcon_register_interest(cable->extcon_dev,
498                                 "max77665-muic", cable->name, &cable->nb);
499
500                 if (ret < 0) {
501                         dev_err(charger->dev, "Cannot register for cable: %s\n",
502                                 cable->name);
503                         ret = -EINVAL;
504                 }
505         }
506
507         charger->edev = extcon_get_extcon_dev("max77665-muic");
508         if (!charger->edev)
509                 return -ENODEV;
510
511         ret = max77665_enable_charger(charger);
512         if (ret < 0) {
513                 dev_err(charger->dev, "failed to initialize charger\n");
514                 goto chrg_error;
515         }
516
517         return 0;
518
519 chrg_error:
520         power_supply_unregister(&charger->usb);
521 pwr_sply_error:
522         power_supply_unregister(&charger->ac);
523 error:
524         return ret;
525 }
526
527 static int __devexit max77665_battery_remove(struct platform_device *pdev)
528 {
529         struct max77665_charger *charger = platform_get_drvdata(pdev);
530
531         power_supply_unregister(&charger->ac);
532         power_supply_unregister(&charger->usb);
533
534         return 0;
535 }
536
537 static struct platform_driver max77665_battery_driver = {
538         .driver = {
539                 .name = "max77665-charger",
540                 .owner = THIS_MODULE,
541         },
542         .probe = max77665_battery_probe,
543         .remove = __devexit_p(max77665_battery_remove),
544
545 };
546
547 static int __init max77665_battery_init(void)
548 {
549         return platform_driver_register(&max77665_battery_driver);
550 }
551
552 static void __exit max77665_battery_exit(void)
553 {
554         platform_driver_unregister(&max77665_battery_driver);
555 }
556
557 late_initcall(max77665_battery_init);
558 module_exit(max77665_battery_exit);
559
560 MODULE_DESCRIPTION("MAXIM MAX77665 battery charging driver");
561 MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>");
562 MODULE_LICENSE("GPL v2");