x86/intel_rdt: Fix incorrect returned value when creating rdgroup sub-directory in...
[cris-mirror.git] / drivers / power / supply / sbs-charger.c
blob15947dbb511e320dfd80d8e9968f313d4c54550a
1 /*
2 * Copyright (c) 2016, Prodys S.L.
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.
9 * This adds support for sbs-charger compilant chips as defined here:
10 * http://sbs-forum.org/specs/sbc110.pdf
12 * Implemetation based on sbs-battery.c
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>
28 #define SBS_CHARGER_REG_SPEC_INFO 0x11
29 #define SBS_CHARGER_REG_STATUS 0x13
30 #define SBS_CHARGER_REG_ALARM_WARNING 0x16
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)
38 #define SBS_CHARGER_POLL_TIME 500
40 struct 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;
48 static int sbs_get_property(struct power_supply *psy,
49 enum power_supply_property psp,
50 union power_supply_propval *val)
52 struct sbs_info *chip = power_supply_get_drvdata(psy);
53 unsigned int reg;
55 reg = chip->last_state;
57 switch (psp) {
58 case POWER_SUPPLY_PROP_PRESENT:
59 val->intval = !!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT);
60 break;
62 case POWER_SUPPLY_PROP_ONLINE:
63 val->intval = !!(reg & SBS_CHARGER_STATUS_AC_PRESENT);
64 break;
66 case POWER_SUPPLY_PROP_STATUS:
67 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
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;
77 break;
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;
87 break;
89 default:
90 return -EINVAL;
93 return 0;
96 static int sbs_check_state(struct sbs_info *chip)
98 unsigned int reg;
99 int ret;
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;
108 return 0;
111 static void sbs_delayed_work(struct work_struct *work)
113 struct sbs_info *chip = container_of(work, struct sbs_info, work.work);
115 sbs_check_state(chip);
117 schedule_delayed_work(&chip->work,
118 msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
121 static irqreturn_t sbs_irq_thread(int irq, void *data)
123 struct sbs_info *chip = data;
124 int ret;
126 ret = sbs_check_state(chip);
128 return ret ? IRQ_HANDLED : IRQ_NONE;
131 static 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,
138 static bool sbs_readable_reg(struct device *dev, unsigned int reg)
140 return reg >= SBS_CHARGER_REG_SPEC_INFO;
143 static bool sbs_volatile_reg(struct device *dev, unsigned int reg)
145 switch (reg) {
146 case SBS_CHARGER_REG_STATUS:
147 return true;
150 return false;
153 static const struct regmap_config sbs_regmap = {
154 .reg_bits = 8,
155 .val_bits = 16,
156 .max_register = SBS_CHARGER_REG_ALARM_WARNING,
157 .readable_reg = sbs_readable_reg,
158 .volatile_reg = sbs_volatile_reg,
159 .val_format_endian = REGMAP_ENDIAN_LITTLE, /* since based on SMBus */
162 static const struct power_supply_desc sbs_desc = {
163 .name = "sbs-charger",
164 .type = POWER_SUPPLY_TYPE_MAINS,
165 .properties = sbs_properties,
166 .num_properties = ARRAY_SIZE(sbs_properties),
167 .get_property = sbs_get_property,
170 static int sbs_probe(struct i2c_client *client,
171 const struct i2c_device_id *id)
173 struct power_supply_config psy_cfg = {};
174 struct sbs_info *chip;
175 int ret, val;
177 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
178 if (!chip)
179 return -ENOMEM;
181 chip->client = client;
182 psy_cfg.of_node = client->dev.of_node;
183 psy_cfg.drv_data = chip;
185 i2c_set_clientdata(client, chip);
187 chip->regmap = devm_regmap_init_i2c(client, &sbs_regmap);
188 if (IS_ERR(chip->regmap))
189 return PTR_ERR(chip->regmap);
192 * Before we register, we need to make sure we can actually talk
193 * to the battery.
195 ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &val);
196 if (ret) {
197 dev_err(&client->dev, "Failed to get device status\n");
198 return ret;
200 chip->last_state = val;
202 chip->power_supply = devm_power_supply_register(&client->dev, &sbs_desc,
203 &psy_cfg);
204 if (IS_ERR(chip->power_supply)) {
205 dev_err(&client->dev, "Failed to register power supply\n");
206 return PTR_ERR(chip->power_supply);
210 * The sbs-charger spec doesn't impose the use of an interrupt. So in
211 * the case it wasn't provided we use polling in order get the charger's
212 * status.
214 if (client->irq) {
215 ret = devm_request_threaded_irq(&client->dev, client->irq,
216 NULL, sbs_irq_thread,
217 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
218 dev_name(&client->dev), chip);
219 if (ret) {
220 dev_err(&client->dev, "Failed to request irq, %d\n", ret);
221 return ret;
223 } else {
224 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
225 schedule_delayed_work(&chip->work,
226 msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
229 dev_info(&client->dev,
230 "%s: smart charger device registered\n", client->name);
232 return 0;
235 static int sbs_remove(struct i2c_client *client)
237 struct sbs_info *chip = i2c_get_clientdata(client);
239 cancel_delayed_work_sync(&chip->work);
241 return 0;
244 #ifdef CONFIG_OF
245 static const struct of_device_id sbs_dt_ids[] = {
246 { .compatible = "sbs,sbs-charger" },
247 { },
249 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
250 #endif
252 static const struct i2c_device_id sbs_id[] = {
253 { "sbs-charger", 0 },
256 MODULE_DEVICE_TABLE(i2c, sbs_id);
258 static struct i2c_driver sbs_driver = {
259 .probe = sbs_probe,
260 .remove = sbs_remove,
261 .id_table = sbs_id,
262 .driver = {
263 .name = "sbs-charger",
264 .of_match_table = of_match_ptr(sbs_dt_ids),
267 module_i2c_driver(sbs_driver);
269 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
270 MODULE_DESCRIPTION("SBS smart charger driver");
271 MODULE_LICENSE("GPL v2");