blob: 9fab6d1bbe80d28a1f388e8bc2bdb14589c8702e [file] [log] [blame]
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +02001/*
2 * Support for TI bq24022 (bqTINY-II) Dual Input (USB/AC Adpater)
3 * 1-Cell Li-Ion Charger connected via GPIOs.
4 *
5 * Copyright (c) 2008 Philipp Zabel
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 version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/platform_device.h>
16#include <linux/err.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040017#include <linux/module.h>
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020018#include <linux/gpio.h>
19#include <linux/regulator/bq24022.h>
20#include <linux/regulator/driver.h>
21
Liam Girdwooda5766f12008-10-10 13:22:20 +010022
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020023static int bq24022_set_current_limit(struct regulator_dev *rdev,
24 int min_uA, int max_uA)
25{
Liam Girdwooda5766f12008-10-10 13:22:20 +010026 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020027
Liam Girdwooda5766f12008-10-10 13:22:20 +010028 dev_dbg(rdev_get_dev(rdev), "setting current limit to %s mA\n",
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020029 max_uA >= 500000 ? "500" : "100");
30
31 /* REVISIT: maybe return error if min_uA != 0 ? */
32 gpio_set_value(pdata->gpio_iset2, max_uA >= 500000);
33 return 0;
34}
35
36static int bq24022_get_current_limit(struct regulator_dev *rdev)
37{
Liam Girdwooda5766f12008-10-10 13:22:20 +010038 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020039
40 return gpio_get_value(pdata->gpio_iset2) ? 500000 : 100000;
41}
42
43static int bq24022_enable(struct regulator_dev *rdev)
44{
Liam Girdwooda5766f12008-10-10 13:22:20 +010045 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020046
Liam Girdwooda5766f12008-10-10 13:22:20 +010047 dev_dbg(rdev_get_dev(rdev), "enabling charger\n");
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020048
49 gpio_set_value(pdata->gpio_nce, 0);
50 return 0;
51}
52
53static int bq24022_disable(struct regulator_dev *rdev)
54{
Liam Girdwooda5766f12008-10-10 13:22:20 +010055 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020056
Liam Girdwooda5766f12008-10-10 13:22:20 +010057 dev_dbg(rdev_get_dev(rdev), "disabling charger\n");
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020058
59 gpio_set_value(pdata->gpio_nce, 1);
60 return 0;
61}
62
63static int bq24022_is_enabled(struct regulator_dev *rdev)
64{
Philipp Zabel030853b2009-04-28 13:34:14 +020065 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020066
67 return !gpio_get_value(pdata->gpio_nce);
68}
69
70static struct regulator_ops bq24022_ops = {
71 .set_current_limit = bq24022_set_current_limit,
72 .get_current_limit = bq24022_get_current_limit,
73 .enable = bq24022_enable,
74 .disable = bq24022_disable,
75 .is_enabled = bq24022_is_enabled,
76};
77
78static struct regulator_desc bq24022_desc = {
79 .name = "bq24022",
80 .ops = &bq24022_ops,
81 .type = REGULATOR_CURRENT,
Axel Lin64714352010-05-13 17:33:01 +080082 .owner = THIS_MODULE,
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +020083};
84
85static int __init bq24022_probe(struct platform_device *pdev)
86{
87 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
88 struct regulator_dev *bq24022;
89 int ret;
90
91 if (!pdata || !pdata->gpio_nce || !pdata->gpio_iset2)
92 return -EINVAL;
93
94 ret = gpio_request(pdata->gpio_nce, "ncharge_en");
95 if (ret) {
96 dev_dbg(&pdev->dev, "couldn't request nCE GPIO: %d\n",
97 pdata->gpio_nce);
98 goto err_ce;
99 }
100 ret = gpio_request(pdata->gpio_iset2, "charge_mode");
101 if (ret) {
102 dev_dbg(&pdev->dev, "couldn't request ISET2 GPIO: %d\n",
103 pdata->gpio_iset2);
104 goto err_iset2;
105 }
106 ret = gpio_direction_output(pdata->gpio_iset2, 0);
107 ret = gpio_direction_output(pdata->gpio_nce, 1);
108
Mark Brown93c62da2009-01-19 13:37:03 +0000109 bq24022 = regulator_register(&bq24022_desc, &pdev->dev,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +0530110 pdata->init_data, pdata, NULL);
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +0200111 if (IS_ERR(bq24022)) {
112 dev_dbg(&pdev->dev, "couldn't register regulator\n");
113 ret = PTR_ERR(bq24022);
114 goto err_reg;
115 }
116 platform_set_drvdata(pdev, bq24022);
117 dev_dbg(&pdev->dev, "registered regulator\n");
118
119 return 0;
120err_reg:
121 gpio_free(pdata->gpio_iset2);
122err_iset2:
123 gpio_free(pdata->gpio_nce);
124err_ce:
125 return ret;
126}
127
128static int __devexit bq24022_remove(struct platform_device *pdev)
129{
130 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
131 struct regulator_dev *bq24022 = platform_get_drvdata(pdev);
132
133 regulator_unregister(bq24022);
134 gpio_free(pdata->gpio_iset2);
135 gpio_free(pdata->gpio_nce);
136
137 return 0;
138}
139
140static struct platform_driver bq24022_driver = {
141 .driver = {
142 .name = "bq24022",
143 },
144 .remove = __devexit_p(bq24022_remove),
145};
146
147static int __init bq24022_init(void)
148{
149 return platform_driver_probe(&bq24022_driver, bq24022_probe);
150}
151
152static void __exit bq24022_exit(void)
153{
154 platform_driver_unregister(&bq24022_driver);
155}
156
Philipp Zabel9bf503e62009-01-18 14:32:27 +0100157module_init(bq24022_init);
Philipp Zabel0eb5d5a2008-07-11 17:28:06 +0200158module_exit(bq24022_exit);
159
160MODULE_AUTHOR("Philipp Zabel");
161MODULE_DESCRIPTION("TI bq24022 Li-Ion Charger driver");
162MODULE_LICENSE("GPL");