~ubuntu-branches/ubuntu/precise/linux-lowlatency/precise

« back to all changes in this revision

Viewing changes to drivers/power/max8997_charger.c

  • Committer: Package Import Robot
  • Author(s): Alessio Igor Bogani
  • Date: 2011-10-26 11:13:05 UTC
  • Revision ID: package-import@ubuntu.com-20111026111305-tz023xykf0i6eosh
Tags: upstream-3.2.0
ImportĀ upstreamĀ versionĀ 3.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * max8997_charger.c - Power supply consumer driver for the Maxim 8997/8966
 
3
 *
 
4
 *  Copyright (C) 2011 Samsung Electronics
 
5
 *  MyungJoo Ham <myungjoo.ham@samsung.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/module.h>
 
23
#include <linux/err.h>
 
24
#include <linux/module.h>
 
25
#include <linux/slab.h>
 
26
#include <linux/platform_device.h>
 
27
#include <linux/power_supply.h>
 
28
#include <linux/mfd/max8997.h>
 
29
#include <linux/mfd/max8997-private.h>
 
30
 
 
31
struct charger_data {
 
32
        struct device *dev;
 
33
        struct max8997_dev *iodev;
 
34
        struct power_supply battery;
 
35
};
 
36
 
 
37
static enum power_supply_property max8997_battery_props[] = {
 
38
        POWER_SUPPLY_PROP_STATUS, /* "FULL" or "NOT FULL" only. */
 
39
        POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
 
40
        POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
 
41
};
 
42
 
 
43
/* Note that the charger control is done by a current regulator "CHARGER" */
 
44
static int max8997_battery_get_property(struct power_supply *psy,
 
45
                enum power_supply_property psp,
 
46
                union power_supply_propval *val)
 
47
{
 
48
        struct charger_data *charger = container_of(psy,
 
49
                        struct charger_data, battery);
 
50
        struct i2c_client *i2c = charger->iodev->i2c;
 
51
        int ret;
 
52
        u8 reg;
 
53
 
 
54
        switch (psp) {
 
55
        case POWER_SUPPLY_PROP_STATUS:
 
56
                val->intval = 0;
 
57
                ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
 
58
                if (ret)
 
59
                        return ret;
 
60
                if ((reg & (1 << 0)) == 0x1)
 
61
                        val->intval = POWER_SUPPLY_STATUS_FULL;
 
62
 
 
63
                break;
 
64
        case POWER_SUPPLY_PROP_PRESENT:
 
65
                val->intval = 0;
 
66
                ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
 
67
                if (ret)
 
68
                        return ret;
 
69
                if ((reg & (1 << 2)) == 0x0)
 
70
                        val->intval = 1;
 
71
 
 
72
                break;
 
73
        case POWER_SUPPLY_PROP_ONLINE:
 
74
                val->intval = 0;
 
75
                ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
 
76
                if (ret)
 
77
                        return ret;
 
78
                /* DCINOK */
 
79
                if (reg & (1 << 1))
 
80
                        val->intval = 1;
 
81
 
 
82
                break;
 
83
        default:
 
84
                return -EINVAL;
 
85
        }
 
86
 
 
87
        return 0;
 
88
}
 
89
 
 
90
static __devinit int max8997_battery_probe(struct platform_device *pdev)
 
91
{
 
92
        int ret = 0;
 
93
        struct charger_data *charger;
 
94
        struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
 
95
        struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
 
96
 
 
97
        if (!pdata)
 
98
                return -EINVAL;
 
99
 
 
100
        if (pdata->eoc_mA) {
 
101
                u8 val = (pdata->eoc_mA - 50) / 10;
 
102
                if (val < 0)
 
103
                        val = 0;
 
104
                if (val > 0xf)
 
105
                        val = 0xf;
 
106
 
 
107
                ret = max8997_update_reg(iodev->i2c,
 
108
                                MAX8997_REG_MBCCTRL5, val, 0xf);
 
109
                if (ret < 0) {
 
110
                        dev_err(&pdev->dev, "Cannot use i2c bus.\n");
 
111
                        return ret;
 
112
                }
 
113
        }
 
114
 
 
115
        switch (pdata->timeout) {
 
116
        case 5:
 
117
                ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
 
118
                                0x2 << 4, 0x7 << 4);
 
119
                break;
 
120
        case 6:
 
121
                ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
 
122
                                0x3 << 4, 0x7 << 4);
 
123
                break;
 
124
        case 7:
 
125
                ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
 
126
                                0x4 << 4, 0x7 << 4);
 
127
                break;
 
128
        case 0:
 
129
                ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
 
130
                                0x7 << 4, 0x7 << 4);
 
131
                break;
 
132
        default:
 
133
                dev_err(&pdev->dev, "incorrect timeout value (%d)\n",
 
134
                                pdata->timeout);
 
135
                return -EINVAL;
 
136
        }
 
137
        if (ret < 0) {
 
138
                dev_err(&pdev->dev, "Cannot use i2c bus.\n");
 
139
                return ret;
 
140
        }
 
141
 
 
142
        charger = kzalloc(sizeof(struct charger_data), GFP_KERNEL);
 
143
        if (charger == NULL) {
 
144
                dev_err(&pdev->dev, "Cannot allocate memory.\n");
 
145
                return -ENOMEM;
 
146
        }
 
147
 
 
148
        platform_set_drvdata(pdev, charger);
 
149
 
 
150
        charger->battery.name = "max8997_pmic";
 
151
        charger->battery.type = POWER_SUPPLY_TYPE_BATTERY;
 
152
        charger->battery.get_property = max8997_battery_get_property;
 
153
        charger->battery.properties = max8997_battery_props;
 
154
        charger->battery.num_properties = ARRAY_SIZE(max8997_battery_props);
 
155
 
 
156
        charger->dev = &pdev->dev;
 
157
        charger->iodev = iodev;
 
158
 
 
159
        ret = power_supply_register(&pdev->dev, &charger->battery);
 
160
        if (ret) {
 
161
                dev_err(&pdev->dev, "failed: power supply register\n");
 
162
                goto err;
 
163
        }
 
164
 
 
165
        return 0;
 
166
err:
 
167
        kfree(charger);
 
168
        return ret;
 
169
}
 
170
 
 
171
static int __devexit max8997_battery_remove(struct platform_device *pdev)
 
172
{
 
173
        struct charger_data *charger = platform_get_drvdata(pdev);
 
174
 
 
175
        power_supply_unregister(&charger->battery);
 
176
        kfree(charger);
 
177
        return 0;
 
178
}
 
179
 
 
180
static const struct platform_device_id max8997_battery_id[] = {
 
181
        { "max8997-battery", 0 },
 
182
};
 
183
 
 
184
static struct platform_driver max8997_battery_driver = {
 
185
        .driver = {
 
186
                .name = "max8997-battery",
 
187
                .owner = THIS_MODULE,
 
188
        },
 
189
        .probe = max8997_battery_probe,
 
190
        .remove = __devexit_p(max8997_battery_remove),
 
191
        .id_table = max8997_battery_id,
 
192
};
 
193
 
 
194
static int __init max8997_battery_init(void)
 
195
{
 
196
        return platform_driver_register(&max8997_battery_driver);
 
197
}
 
198
subsys_initcall(max8997_battery_init);
 
199
 
 
200
static void __exit max8997_battery_cleanup(void)
 
201
{
 
202
        platform_driver_unregister(&max8997_battery_driver);
 
203
}
 
204
module_exit(max8997_battery_cleanup);
 
205
 
 
206
MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
 
207
MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
 
208
MODULE_LICENSE("GPL");