blob: 353765a5f44cfd8096c29edb680bc0f3d09c703e [file] [log] [blame]
Nicolas Saenz Juliennefeb583e2016-12-20 16:31:13 +01001/*
2 * Copyright (c) 2016, Prodys S.L.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This adds support for sbs-charger compilant chips as defined here:
10 * http://sbs-forum.org/specs/sbc110.pdf
11 *
12 * Implemetation based on sbs-battery.c
13 */
14
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/err.h>
19#include <linux/power_supply.h>
20#include <linux/i2c.h>
21#include <linux/slab.h>
22#include <linux/interrupt.h>
23#include <linux/gpio.h>
24#include <linux/regmap.h>
25#include <linux/of_gpio.h>
26#include <linux/bitops.h>
27
28#define SBS_CHARGER_REG_SPEC_INFO 0x11
29#define SBS_CHARGER_REG_STATUS 0x13
30#define SBS_CHARGER_REG_ALARM_WARNING 0x16
31
32#define SBS_CHARGER_STATUS_CHARGE_INHIBITED BIT(1)
33#define SBS_CHARGER_STATUS_RES_COLD BIT(9)
34#define SBS_CHARGER_STATUS_RES_HOT BIT(10)
35#define SBS_CHARGER_STATUS_BATTERY_PRESENT BIT(14)
36#define SBS_CHARGER_STATUS_AC_PRESENT BIT(15)
37
38#define SBS_CHARGER_POLL_TIME 500
39
40struct sbs_info {
41 struct i2c_client *client;
42 struct power_supply *power_supply;
43 struct regmap *regmap;
44 struct delayed_work work;
45 unsigned int last_state;
46};
47
48static int sbs_get_property(struct power_supply *psy,
49 enum power_supply_property psp,
50 union power_supply_propval *val)
51{
52 struct sbs_info *chip = power_supply_get_drvdata(psy);
53 unsigned int reg;
54
55 reg = chip->last_state;
56
57 switch (psp) {
58 case POWER_SUPPLY_PROP_PRESENT:
59 val->intval = !!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT);
60 break;
61
62 case POWER_SUPPLY_PROP_ONLINE:
63 val->intval = !!(reg & SBS_CHARGER_STATUS_AC_PRESENT);
64 break;
65
66 case POWER_SUPPLY_PROP_STATUS:
67 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
68
69 if (!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT))
70 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
71 else if (reg & SBS_CHARGER_STATUS_AC_PRESENT &&
72 !(reg & SBS_CHARGER_STATUS_CHARGE_INHIBITED))
73 val->intval = POWER_SUPPLY_STATUS_CHARGING;
74 else
75 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
76
77 break;
78
79 case POWER_SUPPLY_PROP_HEALTH:
80 if (reg & SBS_CHARGER_STATUS_RES_COLD)
81 val->intval = POWER_SUPPLY_HEALTH_COLD;
82 if (reg & SBS_CHARGER_STATUS_RES_HOT)
83 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
84 else
85 val->intval = POWER_SUPPLY_HEALTH_GOOD;
86
87 break;
88
89 default:
90 return -EINVAL;
91 }
92
93 return 0;
94}
95
96static int sbs_check_state(struct sbs_info *chip)
97{
98 unsigned int reg;
99 int ret;
100
101 ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &reg);
102 if (!ret && reg != chip->last_state) {
103 chip->last_state = reg;
104 power_supply_changed(chip->power_supply);
105 return 1;
106 }
107
108 return 0;
109}
110
111static void sbs_delayed_work(struct work_struct *work)
112{
113 struct sbs_info *chip = container_of(work, struct sbs_info, work.work);
114
115 sbs_check_state(chip);
116
117 schedule_delayed_work(&chip->work,
118 msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
119}
120
121static irqreturn_t sbs_irq_thread(int irq, void *data)
122{
123 struct sbs_info *chip = data;
124 int ret;
125
126 ret = sbs_check_state(chip);
127
128 return ret ? IRQ_HANDLED : IRQ_NONE;
129}
130
131static enum power_supply_property sbs_properties[] = {
132 POWER_SUPPLY_PROP_STATUS,
133 POWER_SUPPLY_PROP_PRESENT,
134 POWER_SUPPLY_PROP_ONLINE,
135 POWER_SUPPLY_PROP_HEALTH,
136};
137
138static bool sbs_readable_reg(struct device *dev, unsigned int reg)
139{
140 if (reg < SBS_CHARGER_REG_SPEC_INFO)
141 return false;
142 else
143 return true;
144}
145
146static bool sbs_volatile_reg(struct device *dev, unsigned int reg)
147{
148 switch (reg) {
149 case SBS_CHARGER_REG_STATUS:
150 return true;
151 }
152
153 return false;
154}
155
156static const struct regmap_config sbs_regmap = {
157 .reg_bits = 8,
158 .val_bits = 16,
159 .max_register = SBS_CHARGER_REG_ALARM_WARNING,
160 .readable_reg = sbs_readable_reg,
161 .volatile_reg = sbs_volatile_reg,
162 .val_format_endian = REGMAP_ENDIAN_LITTLE, /* since based on SMBus */
163};
164
165static const struct power_supply_desc sbs_desc = {
166 .name = "sbs-charger",
167 .type = POWER_SUPPLY_TYPE_MAINS,
168 .properties = sbs_properties,
169 .num_properties = ARRAY_SIZE(sbs_properties),
170 .get_property = sbs_get_property,
171};
172
173static int sbs_probe(struct i2c_client *client,
174 const struct i2c_device_id *id)
175{
176 struct power_supply_config psy_cfg = {};
177 struct sbs_info *chip;
178 int ret, val;
179
180 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
181 if (!chip)
182 return -ENOMEM;
183
184 chip->client = client;
185 psy_cfg.of_node = client->dev.of_node;
186 psy_cfg.drv_data = chip;
187
188 i2c_set_clientdata(client, chip);
189
190 chip->regmap = devm_regmap_init_i2c(client, &sbs_regmap);
191 if (IS_ERR(chip->regmap))
192 return PTR_ERR(chip->regmap);
193
194 /*
195 * Before we register, we need to make sure we can actually talk
196 * to the battery.
197 */
198 ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &val);
199 if (ret) {
200 dev_err(&client->dev, "Failed to get device status\n");
201 return ret;
202 }
203 chip->last_state = val;
204
205 chip->power_supply = devm_power_supply_register(&client->dev, &sbs_desc,
206 &psy_cfg);
207 if (IS_ERR(chip->power_supply)) {
208 dev_err(&client->dev, "Failed to register power supply\n");
209 return PTR_ERR(chip->power_supply);
210 }
211
212 /*
213 * The sbs-charger spec doesn't impose the use of an interrupt. So in
214 * the case it wasn't provided we use polling in order get the charger's
215 * status.
216 */
217 if (client->irq) {
218 ret = devm_request_threaded_irq(&client->dev, client->irq,
219 NULL, sbs_irq_thread,
220 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
221 dev_name(&client->dev), chip);
222 if (ret) {
223 dev_err(&client->dev, "Failed to request irq, %d\n", ret);
224 return ret;
225 }
226 } else {
227 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
228 schedule_delayed_work(&chip->work,
229 msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
230 }
231
232 dev_info(&client->dev,
233 "%s: smart charger device registered\n", client->name);
234
235 return 0;
236}
237
238static int sbs_remove(struct i2c_client *client)
239{
240 struct sbs_info *chip = i2c_get_clientdata(client);
241
242 cancel_delayed_work_sync(&chip->work);
243
244 return 0;
245}
246
247#ifdef CONFIG_OF
248static const struct of_device_id sbs_dt_ids[] = {
249 { .compatible = "sbs,sbs-charger" },
250 { },
251};
252MODULE_DEVICE_TABLE(of, sbs_dt_ids);
253#endif
254
255static const struct i2c_device_id sbs_id[] = {
256 { "sbs-charger", 0 },
257 { }
258};
259MODULE_DEVICE_TABLE(i2c, sbs_id);
260
261static struct i2c_driver sbs_driver = {
262 .probe = sbs_probe,
263 .remove = sbs_remove,
264 .id_table = sbs_id,
265 .driver = {
266 .name = "sbs-charger",
267 .of_match_table = of_match_ptr(sbs_dt_ids),
268 },
269};
270module_i2c_driver(sbs_driver);
271
272MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
273MODULE_DESCRIPTION("SBS smart charger driver");
274MODULE_LICENSE("GPL v2");