blob: aefa0c9a3007dcd729859542900f902801719286 [file] [log] [blame]
Donggeun Kim149c0772011-06-22 19:40:06 +09001/*
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/err.h>
Axel Lin71aa79a2011-08-01 07:29:31 +080023#include <linux/module.h>
Donggeun Kim149c0772011-06-22 19:40:06 +090024#include <linux/slab.h>
25#include <linux/platform_device.h>
26#include <linux/power_supply.h>
27#include <linux/mfd/max8997.h>
28#include <linux/mfd/max8997-private.h>
29
30struct charger_data {
31 struct device *dev;
32 struct max8997_dev *iodev;
33 struct power_supply battery;
34};
35
36static enum power_supply_property max8997_battery_props[] = {
37 POWER_SUPPLY_PROP_STATUS, /* "FULL" or "NOT FULL" only. */
38 POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
39 POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
40};
41
42/* Note that the charger control is done by a current regulator "CHARGER" */
43static int max8997_battery_get_property(struct power_supply *psy,
44 enum power_supply_property psp,
45 union power_supply_propval *val)
46{
47 struct charger_data *charger = container_of(psy,
48 struct charger_data, battery);
49 struct i2c_client *i2c = charger->iodev->i2c;
50 int ret;
51 u8 reg;
52
53 switch (psp) {
54 case POWER_SUPPLY_PROP_STATUS:
55 val->intval = 0;
56 ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
57 if (ret)
58 return ret;
59 if ((reg & (1 << 0)) == 0x1)
60 val->intval = POWER_SUPPLY_STATUS_FULL;
61
62 break;
63 case POWER_SUPPLY_PROP_PRESENT:
64 val->intval = 0;
65 ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
66 if (ret)
67 return ret;
68 if ((reg & (1 << 2)) == 0x0)
69 val->intval = 1;
70
71 break;
72 case POWER_SUPPLY_PROP_ONLINE:
73 val->intval = 0;
74 ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
75 if (ret)
76 return ret;
77 /* DCINOK */
78 if (reg & (1 << 1))
79 val->intval = 1;
80
81 break;
82 default:
83 return -EINVAL;
84 }
85
86 return 0;
87}
88
Bill Pembertonc8afa642012-11-19 13:22:23 -050089static int max8997_battery_probe(struct platform_device *pdev)
Donggeun Kim149c0772011-06-22 19:40:06 +090090{
91 int ret = 0;
92 struct charger_data *charger;
93 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
94 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
95
96 if (!pdata)
97 return -EINVAL;
98
99 if (pdata->eoc_mA) {
Jonghwan Choi44abd772011-10-29 07:56:32 +0900100 int val = (pdata->eoc_mA - 50) / 10;
Donggeun Kim149c0772011-06-22 19:40:06 +0900101 if (val < 0)
102 val = 0;
103 if (val > 0xf)
104 val = 0xf;
105
106 ret = max8997_update_reg(iodev->i2c,
107 MAX8997_REG_MBCCTRL5, val, 0xf);
108 if (ret < 0) {
109 dev_err(&pdev->dev, "Cannot use i2c bus.\n");
110 return ret;
111 }
112 }
113
114 switch (pdata->timeout) {
115 case 5:
116 ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
117 0x2 << 4, 0x7 << 4);
118 break;
119 case 6:
120 ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
121 0x3 << 4, 0x7 << 4);
122 break;
123 case 7:
124 ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
125 0x4 << 4, 0x7 << 4);
126 break;
127 case 0:
128 ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
129 0x7 << 4, 0x7 << 4);
130 break;
131 default:
132 dev_err(&pdev->dev, "incorrect timeout value (%d)\n",
133 pdata->timeout);
134 return -EINVAL;
135 }
136 if (ret < 0) {
137 dev_err(&pdev->dev, "Cannot use i2c bus.\n");
138 return ret;
139 }
140
Jingoo Hand4a20f52013-03-11 15:34:55 +0900141 charger = devm_kzalloc(&pdev->dev, sizeof(struct charger_data),
142 GFP_KERNEL);
Donggeun Kim149c0772011-06-22 19:40:06 +0900143 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");
Jingoo Hand4a20f52013-03-11 15:34:55 +0900162 return ret;
Donggeun Kim149c0772011-06-22 19:40:06 +0900163 }
164
165 return 0;
Donggeun Kim149c0772011-06-22 19:40:06 +0900166}
167
Bill Pemberton415ec692012-11-19 13:26:07 -0500168static int max8997_battery_remove(struct platform_device *pdev)
Donggeun Kim149c0772011-06-22 19:40:06 +0900169{
170 struct charger_data *charger = platform_get_drvdata(pdev);
171
172 power_supply_unregister(&charger->battery);
Donggeun Kim149c0772011-06-22 19:40:06 +0900173 return 0;
174}
175
176static const struct platform_device_id max8997_battery_id[] = {
177 { "max8997-battery", 0 },
Axel Linc03bfab2011-08-01 07:23:00 +0800178 { }
Donggeun Kim149c0772011-06-22 19:40:06 +0900179};
180
181static struct platform_driver max8997_battery_driver = {
182 .driver = {
183 .name = "max8997-battery",
Donggeun Kim149c0772011-06-22 19:40:06 +0900184 },
185 .probe = max8997_battery_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500186 .remove = max8997_battery_remove,
Donggeun Kim149c0772011-06-22 19:40:06 +0900187 .id_table = max8997_battery_id,
188};
189
190static int __init max8997_battery_init(void)
191{
192 return platform_driver_register(&max8997_battery_driver);
193}
194subsys_initcall(max8997_battery_init);
195
196static void __exit max8997_battery_cleanup(void)
197{
198 platform_driver_unregister(&max8997_battery_driver);
199}
200module_exit(max8997_battery_cleanup);
201
202MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
203MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
204MODULE_LICENSE("GPL");