2 * Support for TI bq24022 (bqTINY-II) Dual Input (USB/AC Adpater)
3 * 1-Cell Li-Ion Charger connected via GPIOs.
5 * Copyright (c) 2008 Philipp Zabel
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.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/regulator/bq24022.h>
19 #include <linux/regulator/driver.h>
21 static int bq24022_set_current_limit(struct regulator_dev *rdev,
22 int min_uA, int max_uA)
24 struct platform_device *pdev = rdev_get_drvdata(rdev);
25 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
27 dev_dbg(&pdev->dev, "setting current limit to %s mA\n",
28 max_uA >= 500000 ? "500" : "100");
30 /* REVISIT: maybe return error if min_uA != 0 ? */
31 gpio_set_value(pdata->gpio_iset2, max_uA >= 500000);
35 static int bq24022_get_current_limit(struct regulator_dev *rdev)
37 struct platform_device *pdev = rdev_get_drvdata(rdev);
38 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
40 return gpio_get_value(pdata->gpio_iset2) ? 500000 : 100000;
43 static int bq24022_enable(struct regulator_dev *rdev)
45 struct platform_device *pdev = rdev_get_drvdata(rdev);
46 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
48 dev_dbg(&pdev->dev, "enabling charger\n");
50 gpio_set_value(pdata->gpio_nce, 0);
54 static int bq24022_disable(struct regulator_dev *rdev)
56 struct platform_device *pdev = rdev_get_drvdata(rdev);
57 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
59 dev_dbg(&pdev->dev, "disabling charger\n");
61 gpio_set_value(pdata->gpio_nce, 1);
65 static int bq24022_is_enabled(struct regulator_dev *rdev)
67 struct platform_device *pdev = rdev_get_drvdata(rdev);
68 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
70 return !gpio_get_value(pdata->gpio_nce);
73 static struct regulator_ops bq24022_ops = {
74 .set_current_limit = bq24022_set_current_limit,
75 .get_current_limit = bq24022_get_current_limit,
76 .enable = bq24022_enable,
77 .disable = bq24022_disable,
78 .is_enabled = bq24022_is_enabled,
81 static struct regulator_desc bq24022_desc = {
84 .type = REGULATOR_CURRENT,
87 static int __init bq24022_probe(struct platform_device *pdev)
89 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
90 struct regulator_dev *bq24022;
93 if (!pdata || !pdata->gpio_nce || !pdata->gpio_iset2)
96 ret = gpio_request(pdata->gpio_nce, "ncharge_en");
98 dev_dbg(&pdev->dev, "couldn't request nCE GPIO: %d\n",
102 ret = gpio_request(pdata->gpio_iset2, "charge_mode");
104 dev_dbg(&pdev->dev, "couldn't request ISET2 GPIO: %d\n",
108 ret = gpio_direction_output(pdata->gpio_iset2, 0);
109 ret = gpio_direction_output(pdata->gpio_nce, 1);
111 bq24022 = regulator_register(&bq24022_desc, pdev);
112 if (IS_ERR(bq24022)) {
113 dev_dbg(&pdev->dev, "couldn't register regulator\n");
114 ret = PTR_ERR(bq24022);
117 platform_set_drvdata(pdev, bq24022);
118 dev_dbg(&pdev->dev, "registered regulator\n");
122 gpio_free(pdata->gpio_iset2);
124 gpio_free(pdata->gpio_nce);
129 static int __devexit bq24022_remove(struct platform_device *pdev)
131 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
132 struct regulator_dev *bq24022 = platform_get_drvdata(pdev);
134 regulator_unregister(bq24022);
135 gpio_free(pdata->gpio_iset2);
136 gpio_free(pdata->gpio_nce);
141 static struct platform_driver bq24022_driver = {
145 .remove = __devexit_p(bq24022_remove),
148 static int __init bq24022_init(void)
150 return platform_driver_probe(&bq24022_driver, bq24022_probe);
153 static void __exit bq24022_exit(void)
155 platform_driver_unregister(&bq24022_driver);
159 * make sure this is probed before gpio_vbus and pda_power,
160 * but after asic3 or other GPIO expander drivers.
162 subsys_initcall(bq24022_init);
163 module_exit(bq24022_exit);
165 MODULE_AUTHOR("Philipp Zabel");
166 MODULE_DESCRIPTION("TI bq24022 Li-Ion Charger driver");
167 MODULE_LICENSE("GPL");