Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Battery measurement code for Zipit Z2 |
| 3 | * |
| 4 | * Copyright (C) 2009 Peter Edwards <sweetlilmre@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 12 | #include <linux/module.h> |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 13 | #include <linux/gpio.h> |
Marek Vasut | 56b925f | 2010-06-03 03:44:00 +0200 | [diff] [blame] | 14 | #include <linux/i2c.h> |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/irq.h> |
Marek Vasut | 56b925f | 2010-06-03 03:44:00 +0200 | [diff] [blame] | 17 | #include <linux/power_supply.h> |
| 18 | #include <linux/slab.h> |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 19 | #include <linux/z2_battery.h> |
| 20 | |
| 21 | #define Z2_DEFAULT_NAME "Z2" |
| 22 | |
| 23 | struct z2_charger { |
| 24 | struct z2_battery_info *info; |
| 25 | int bat_status; |
| 26 | struct i2c_client *client; |
| 27 | struct power_supply batt_ps; |
| 28 | struct mutex work_lock; |
| 29 | struct work_struct bat_work; |
| 30 | }; |
| 31 | |
| 32 | static unsigned long z2_read_bat(struct z2_charger *charger) |
| 33 | { |
| 34 | int data; |
| 35 | data = i2c_smbus_read_byte_data(charger->client, |
| 36 | charger->info->batt_I2C_reg); |
| 37 | if (data < 0) |
| 38 | return 0; |
| 39 | |
| 40 | return data * charger->info->batt_mult / charger->info->batt_div; |
| 41 | } |
| 42 | |
| 43 | static int z2_batt_get_property(struct power_supply *batt_ps, |
| 44 | enum power_supply_property psp, |
| 45 | union power_supply_propval *val) |
| 46 | { |
| 47 | struct z2_charger *charger = container_of(batt_ps, struct z2_charger, |
| 48 | batt_ps); |
| 49 | struct z2_battery_info *info = charger->info; |
| 50 | |
| 51 | switch (psp) { |
| 52 | case POWER_SUPPLY_PROP_STATUS: |
| 53 | val->intval = charger->bat_status; |
| 54 | break; |
| 55 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 56 | val->intval = info->batt_tech; |
| 57 | break; |
| 58 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 59 | if (info->batt_I2C_reg >= 0) |
| 60 | val->intval = z2_read_bat(charger); |
| 61 | else |
| 62 | return -EINVAL; |
| 63 | break; |
| 64 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| 65 | if (info->max_voltage >= 0) |
| 66 | val->intval = info->max_voltage; |
| 67 | else |
| 68 | return -EINVAL; |
| 69 | break; |
| 70 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: |
| 71 | if (info->min_voltage >= 0) |
| 72 | val->intval = info->min_voltage; |
| 73 | else |
| 74 | return -EINVAL; |
| 75 | break; |
| 76 | case POWER_SUPPLY_PROP_PRESENT: |
| 77 | val->intval = 1; |
| 78 | break; |
| 79 | default: |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void z2_batt_ext_power_changed(struct power_supply *batt_ps) |
| 87 | { |
| 88 | struct z2_charger *charger = container_of(batt_ps, struct z2_charger, |
| 89 | batt_ps); |
| 90 | schedule_work(&charger->bat_work); |
| 91 | } |
| 92 | |
| 93 | static void z2_batt_update(struct z2_charger *charger) |
| 94 | { |
| 95 | int old_status = charger->bat_status; |
| 96 | struct z2_battery_info *info; |
| 97 | |
| 98 | info = charger->info; |
| 99 | |
| 100 | mutex_lock(&charger->work_lock); |
| 101 | |
| 102 | charger->bat_status = (info->charge_gpio >= 0) ? |
| 103 | (gpio_get_value(info->charge_gpio) ? |
| 104 | POWER_SUPPLY_STATUS_CHARGING : |
| 105 | POWER_SUPPLY_STATUS_DISCHARGING) : |
| 106 | POWER_SUPPLY_STATUS_UNKNOWN; |
| 107 | |
| 108 | if (old_status != charger->bat_status) { |
| 109 | pr_debug("%s: %i -> %i\n", charger->batt_ps.name, old_status, |
| 110 | charger->bat_status); |
| 111 | power_supply_changed(&charger->batt_ps); |
| 112 | } |
| 113 | |
| 114 | mutex_unlock(&charger->work_lock); |
| 115 | } |
| 116 | |
| 117 | static void z2_batt_work(struct work_struct *work) |
| 118 | { |
| 119 | struct z2_charger *charger; |
| 120 | charger = container_of(work, struct z2_charger, bat_work); |
| 121 | z2_batt_update(charger); |
| 122 | } |
| 123 | |
| 124 | static irqreturn_t z2_charge_switch_irq(int irq, void *devid) |
| 125 | { |
| 126 | struct z2_charger *charger = devid; |
| 127 | schedule_work(&charger->bat_work); |
| 128 | return IRQ_HANDLED; |
| 129 | } |
| 130 | |
| 131 | static int z2_batt_ps_init(struct z2_charger *charger, int props) |
| 132 | { |
| 133 | int i = 0; |
| 134 | enum power_supply_property *prop; |
| 135 | struct z2_battery_info *info = charger->info; |
| 136 | |
Vasily Khoruzhick | a223246 | 2011-03-11 15:09:32 +0200 | [diff] [blame] | 137 | if (info->charge_gpio >= 0) |
| 138 | props++; /* POWER_SUPPLY_PROP_STATUS */ |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 139 | if (info->batt_tech >= 0) |
| 140 | props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */ |
| 141 | if (info->batt_I2C_reg >= 0) |
| 142 | props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */ |
| 143 | if (info->max_voltage >= 0) |
| 144 | props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */ |
| 145 | if (info->min_voltage >= 0) |
| 146 | props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */ |
| 147 | |
| 148 | prop = kzalloc(props * sizeof(*prop), GFP_KERNEL); |
| 149 | if (!prop) |
| 150 | return -ENOMEM; |
| 151 | |
| 152 | prop[i++] = POWER_SUPPLY_PROP_PRESENT; |
| 153 | if (info->charge_gpio >= 0) |
| 154 | prop[i++] = POWER_SUPPLY_PROP_STATUS; |
| 155 | if (info->batt_tech >= 0) |
| 156 | prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY; |
| 157 | if (info->batt_I2C_reg >= 0) |
| 158 | prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW; |
| 159 | if (info->max_voltage >= 0) |
| 160 | prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX; |
| 161 | if (info->min_voltage >= 0) |
| 162 | prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN; |
| 163 | |
| 164 | if (!info->batt_name) { |
| 165 | dev_info(&charger->client->dev, |
| 166 | "Please consider setting proper battery " |
| 167 | "name in platform definition file, falling " |
| 168 | "back to name \" Z2_DEFAULT_NAME \"\n"); |
| 169 | charger->batt_ps.name = Z2_DEFAULT_NAME; |
| 170 | } else |
| 171 | charger->batt_ps.name = info->batt_name; |
| 172 | |
| 173 | charger->batt_ps.properties = prop; |
| 174 | charger->batt_ps.num_properties = props; |
| 175 | charger->batt_ps.type = POWER_SUPPLY_TYPE_BATTERY; |
| 176 | charger->batt_ps.get_property = z2_batt_get_property; |
| 177 | charger->batt_ps.external_power_changed = z2_batt_ext_power_changed; |
| 178 | charger->batt_ps.use_for_apm = 1; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int __devinit z2_batt_probe(struct i2c_client *client, |
| 184 | const struct i2c_device_id *id) |
| 185 | { |
| 186 | int ret = 0; |
| 187 | int props = 1; /* POWER_SUPPLY_PROP_PRESENT */ |
| 188 | struct z2_charger *charger; |
| 189 | struct z2_battery_info *info = client->dev.platform_data; |
| 190 | |
| 191 | if (info == NULL) { |
| 192 | dev_err(&client->dev, |
| 193 | "Please set platform device platform_data" |
| 194 | " to a valid z2_battery_info pointer!\n"); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | charger = kzalloc(sizeof(*charger), GFP_KERNEL); |
| 199 | if (charger == NULL) |
| 200 | return -ENOMEM; |
| 201 | |
| 202 | charger->bat_status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 203 | charger->info = info; |
| 204 | charger->client = client; |
| 205 | i2c_set_clientdata(client, charger); |
| 206 | |
| 207 | mutex_init(&charger->work_lock); |
| 208 | |
| 209 | if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio)) { |
| 210 | ret = gpio_request(info->charge_gpio, "BATT CHRG"); |
| 211 | if (ret) |
| 212 | goto err; |
| 213 | |
| 214 | ret = gpio_direction_input(info->charge_gpio); |
| 215 | if (ret) |
| 216 | goto err2; |
| 217 | |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 218 | irq_set_irq_type(gpio_to_irq(info->charge_gpio), |
| 219 | IRQ_TYPE_EDGE_BOTH); |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 220 | ret = request_irq(gpio_to_irq(info->charge_gpio), |
| 221 | z2_charge_switch_irq, IRQF_DISABLED, |
| 222 | "AC Detect", charger); |
| 223 | if (ret) |
| 224 | goto err3; |
| 225 | } |
| 226 | |
| 227 | ret = z2_batt_ps_init(charger, props); |
| 228 | if (ret) |
| 229 | goto err3; |
| 230 | |
| 231 | INIT_WORK(&charger->bat_work, z2_batt_work); |
| 232 | |
| 233 | ret = power_supply_register(&client->dev, &charger->batt_ps); |
| 234 | if (ret) |
| 235 | goto err4; |
| 236 | |
| 237 | schedule_work(&charger->bat_work); |
| 238 | |
| 239 | return 0; |
| 240 | |
| 241 | err4: |
| 242 | kfree(charger->batt_ps.properties); |
| 243 | err3: |
| 244 | if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio)) |
| 245 | free_irq(gpio_to_irq(info->charge_gpio), charger); |
| 246 | err2: |
| 247 | if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio)) |
| 248 | gpio_free(info->charge_gpio); |
| 249 | err: |
| 250 | kfree(charger); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static int __devexit z2_batt_remove(struct i2c_client *client) |
| 255 | { |
| 256 | struct z2_charger *charger = i2c_get_clientdata(client); |
| 257 | struct z2_battery_info *info = charger->info; |
| 258 | |
Tejun Heo | bc51e7f | 2010-12-11 17:51:45 +0100 | [diff] [blame] | 259 | cancel_work_sync(&charger->bat_work); |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 260 | power_supply_unregister(&charger->batt_ps); |
| 261 | |
| 262 | kfree(charger->batt_ps.properties); |
| 263 | if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio)) { |
| 264 | free_irq(gpio_to_irq(info->charge_gpio), charger); |
| 265 | gpio_free(info->charge_gpio); |
| 266 | } |
| 267 | |
| 268 | kfree(charger); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | #ifdef CONFIG_PM |
| 274 | static int z2_batt_suspend(struct i2c_client *client, pm_message_t state) |
| 275 | { |
Tejun Heo | bc51e7f | 2010-12-11 17:51:45 +0100 | [diff] [blame] | 276 | struct z2_charger *charger = i2c_get_clientdata(client); |
| 277 | |
| 278 | flush_work_sync(&charger->bat_work); |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int z2_batt_resume(struct i2c_client *client) |
| 283 | { |
| 284 | struct z2_charger *charger = i2c_get_clientdata(client); |
| 285 | |
| 286 | schedule_work(&charger->bat_work); |
| 287 | return 0; |
| 288 | } |
| 289 | #else |
| 290 | #define z2_batt_suspend NULL |
| 291 | #define z2_batt_resume NULL |
| 292 | #endif |
| 293 | |
| 294 | static const struct i2c_device_id z2_batt_id[] = { |
| 295 | { "aer915", 0 }, |
| 296 | { } |
| 297 | }; |
Axel Lin | dcbc916 | 2011-03-01 17:24:34 +0800 | [diff] [blame] | 298 | MODULE_DEVICE_TABLE(i2c, z2_batt_id); |
Marek Vasut | e3e8d1c | 2010-02-28 12:47:49 +0100 | [diff] [blame] | 299 | |
| 300 | static struct i2c_driver z2_batt_driver = { |
| 301 | .driver = { |
| 302 | .name = "z2-battery", |
| 303 | .owner = THIS_MODULE, |
| 304 | }, |
| 305 | .probe = z2_batt_probe, |
| 306 | .remove = z2_batt_remove, |
| 307 | .suspend = z2_batt_suspend, |
| 308 | .resume = z2_batt_resume, |
| 309 | .id_table = z2_batt_id, |
| 310 | }; |
| 311 | |
| 312 | static int __init z2_batt_init(void) |
| 313 | { |
| 314 | return i2c_add_driver(&z2_batt_driver); |
| 315 | } |
| 316 | |
| 317 | static void __exit z2_batt_exit(void) |
| 318 | { |
| 319 | i2c_del_driver(&z2_batt_driver); |
| 320 | } |
| 321 | |
| 322 | module_init(z2_batt_init); |
| 323 | module_exit(z2_batt_exit); |
| 324 | |
| 325 | MODULE_LICENSE("GPL"); |
| 326 | MODULE_AUTHOR("Peter Edwards <sweetlilmre@gmail.com>"); |
| 327 | MODULE_DESCRIPTION("Zipit Z2 battery driver"); |