Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * BQ27x00 battery driver |
| 3 | * |
| 4 | * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> |
| 5 | * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it> |
| 6 | * |
| 7 | * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. |
| 8 | * |
| 9 | * This package is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | * |
| 17 | */ |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/param.h> |
| 20 | #include <linux/jiffies.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/power_supply.h> |
| 25 | #include <linux/idr.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 26 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Harvey Harrison | 8aef7e8 | 2008-09-22 14:53:50 -0700 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 29 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 30 | #define DRIVER_VERSION "1.1.0" |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 31 | |
| 32 | #define BQ27x00_REG_TEMP 0x06 |
| 33 | #define BQ27x00_REG_VOLT 0x08 |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 34 | #define BQ27x00_REG_AI 0x14 |
| 35 | #define BQ27x00_REG_FLAGS 0x0A |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 36 | #define BQ27x00_REG_TTE 0x16 |
| 37 | #define BQ27x00_REG_TTF 0x18 |
| 38 | #define BQ27x00_REG_TTECP 0x26 |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 39 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 40 | #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 41 | #define BQ27000_FLAG_CHGS BIT(7) |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 42 | |
| 43 | #define BQ27500_REG_SOC 0x2c |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 44 | #define BQ27500_FLAG_DSC BIT(0) |
| 45 | #define BQ27500_FLAG_FC BIT(9) |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 46 | |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 47 | #define BQ27000_RS 20 /* Resistor sense */ |
| 48 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 49 | /* If the system has several batteries we need a different name for each |
| 50 | * of them... |
| 51 | */ |
| 52 | static DEFINE_IDR(battery_id); |
| 53 | static DEFINE_MUTEX(battery_mutex); |
| 54 | |
| 55 | struct bq27x00_device_info; |
| 56 | struct bq27x00_access_methods { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 57 | int (*read)(struct bq27x00_device_info *, u8 reg, int *rt_value, |
| 58 | bool single); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 61 | enum bq27x00_chip { BQ27000, BQ27500 }; |
| 62 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 63 | struct bq27x00_device_info { |
| 64 | struct device *dev; |
| 65 | int id; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 66 | enum bq27x00_chip chip; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 67 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 68 | struct power_supply bat; |
| 69 | |
| 70 | struct bq27x00_access_methods bus; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | static enum power_supply_property bq27x00_battery_props[] = { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 74 | POWER_SUPPLY_PROP_STATUS, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 75 | POWER_SUPPLY_PROP_PRESENT, |
| 76 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 77 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 78 | POWER_SUPPLY_PROP_CAPACITY, |
| 79 | POWER_SUPPLY_PROP_TEMP, |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 80 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 81 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 82 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 83 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Common code for BQ27x00 devices |
| 88 | */ |
| 89 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 90 | static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, |
| 91 | int *rt_value, bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 92 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 93 | return di->bus.read(di, reg, rt_value, single); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* |
Grazvydas Ignotas | b4de360 | 2010-02-12 23:57:13 +0200 | [diff] [blame] | 97 | * Return the battery temperature in tenths of degree Celsius |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 98 | * Or < 0 if something fails. |
| 99 | */ |
| 100 | static int bq27x00_battery_temperature(struct bq27x00_device_info *di) |
| 101 | { |
| 102 | int ret; |
| 103 | int temp = 0; |
| 104 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 105 | ret = bq27x00_read(di, BQ27x00_REG_TEMP, &temp, false); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 106 | if (ret) { |
| 107 | dev_err(di->dev, "error reading temperature\n"); |
| 108 | return ret; |
| 109 | } |
| 110 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 111 | if (di->chip == BQ27500) |
| 112 | return temp - 2731; |
| 113 | else |
Lars-Peter Clausen | 0e9f304 | 2010-05-24 20:20:57 +0200 | [diff] [blame] | 114 | return ((temp * 5) - 5463) / 2; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Return the battery Voltage in milivolts |
| 119 | * Or < 0 if something fails. |
| 120 | */ |
| 121 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di) |
| 122 | { |
| 123 | int ret; |
| 124 | int volt = 0; |
| 125 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 126 | ret = bq27x00_read(di, BQ27x00_REG_VOLT, &volt, false); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 127 | if (ret) { |
| 128 | dev_err(di->dev, "error reading voltage\n"); |
| 129 | return ret; |
| 130 | } |
| 131 | |
Grazvydas Ignotas | afbc74f | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 132 | return volt * 1000; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Return the battery average current |
| 137 | * Note that current can be negative signed as well |
| 138 | * Or 0 if something fails. |
| 139 | */ |
| 140 | static int bq27x00_battery_current(struct bq27x00_device_info *di) |
| 141 | { |
| 142 | int ret; |
| 143 | int curr = 0; |
| 144 | int flags = 0; |
| 145 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 146 | ret = bq27x00_read(di, BQ27x00_REG_AI, &curr, false); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 147 | if (ret) { |
| 148 | dev_err(di->dev, "error reading current\n"); |
| 149 | return 0; |
| 150 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 151 | |
| 152 | if (di->chip == BQ27500) { |
| 153 | /* bq27500 returns signed value */ |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 154 | curr = (int)((s16)curr) * 1000; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 155 | } else { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 156 | ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false); |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 157 | if (ret < 0) { |
| 158 | dev_err(di->dev, "error reading flags\n"); |
| 159 | return 0; |
| 160 | } |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 161 | if (flags & BQ27000_FLAG_CHGS) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 162 | dev_dbg(di->dev, "negative current!\n"); |
Grazvydas Ignotas | afbc74f | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 163 | curr = -curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 164 | } |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 165 | curr = curr * 3570 / BQ27000_RS; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 166 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 167 | |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 168 | return curr; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Return the battery Relative State-of-Charge |
| 173 | * Or < 0 if something fails. |
| 174 | */ |
| 175 | static int bq27x00_battery_rsoc(struct bq27x00_device_info *di) |
| 176 | { |
| 177 | int ret; |
| 178 | int rsoc = 0; |
| 179 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 180 | if (di->chip == BQ27500) |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 181 | ret = bq27x00_read(di, BQ27500_REG_SOC, &rsoc, false); |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 182 | else |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 183 | ret = bq27x00_read(di, BQ27000_REG_RSOC, &rsoc, true); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 184 | if (ret) { |
| 185 | dev_err(di->dev, "error reading relative State-of-Charge\n"); |
| 186 | return ret; |
| 187 | } |
| 188 | |
Grazvydas Ignotas | 97f70c2 | 2010-02-12 23:56:46 +0200 | [diff] [blame] | 189 | return rsoc; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 192 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
| 193 | union power_supply_propval *val) |
| 194 | { |
| 195 | int flags = 0; |
| 196 | int status; |
| 197 | int ret; |
| 198 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 199 | ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 200 | if (ret < 0) { |
| 201 | dev_err(di->dev, "error reading flags\n"); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | if (di->chip == BQ27500) { |
| 206 | if (flags & BQ27500_FLAG_FC) |
| 207 | status = POWER_SUPPLY_STATUS_FULL; |
| 208 | else if (flags & BQ27500_FLAG_DSC) |
| 209 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 210 | else |
| 211 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 212 | } else { |
| 213 | if (flags & BQ27000_FLAG_CHGS) |
| 214 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 215 | else |
| 216 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 217 | } |
| 218 | |
| 219 | val->intval = status; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Read a time register. |
| 225 | * Return < 0 if something fails. |
| 226 | */ |
| 227 | static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg, |
| 228 | union power_supply_propval *val) |
| 229 | { |
| 230 | int tval = 0; |
| 231 | int ret; |
| 232 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 233 | ret = bq27x00_read(di, reg, &tval, false); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 234 | if (ret) { |
| 235 | dev_err(di->dev, "error reading register %02x\n", reg); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | if (tval == 65535) |
| 240 | return -ENODATA; |
| 241 | |
| 242 | val->intval = tval * 60; |
| 243 | return 0; |
| 244 | } |
| 245 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 246 | #define to_bq27x00_device_info(x) container_of((x), \ |
| 247 | struct bq27x00_device_info, bat); |
| 248 | |
| 249 | static int bq27x00_battery_get_property(struct power_supply *psy, |
| 250 | enum power_supply_property psp, |
| 251 | union power_supply_propval *val) |
| 252 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 253 | int ret = 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 254 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 255 | int voltage = bq27x00_battery_voltage(di); |
| 256 | |
| 257 | if (psp != POWER_SUPPLY_PROP_PRESENT && voltage <= 0) |
| 258 | return -ENODEV; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 259 | |
| 260 | switch (psp) { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 261 | case POWER_SUPPLY_PROP_STATUS: |
| 262 | ret = bq27x00_battery_status(di, val); |
| 263 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 264 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 265 | val->intval = voltage; |
| 266 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 267 | case POWER_SUPPLY_PROP_PRESENT: |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 268 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 269 | val->intval = voltage <= 0 ? 0 : 1; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 270 | break; |
| 271 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 272 | val->intval = bq27x00_battery_current(di); |
| 273 | break; |
| 274 | case POWER_SUPPLY_PROP_CAPACITY: |
| 275 | val->intval = bq27x00_battery_rsoc(di); |
| 276 | break; |
| 277 | case POWER_SUPPLY_PROP_TEMP: |
| 278 | val->intval = bq27x00_battery_temperature(di); |
| 279 | break; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 280 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
| 281 | ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val); |
| 282 | break; |
| 283 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 284 | ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val); |
| 285 | break; |
| 286 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
| 287 | ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val); |
| 288 | break; |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 289 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 290 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 291 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 292 | default: |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 296 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 299 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 300 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 301 | int ret; |
| 302 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 303 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
| 304 | di->bat.properties = bq27x00_battery_props; |
| 305 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); |
| 306 | di->bat.get_property = bq27x00_battery_get_property; |
| 307 | di->bat.external_power_changed = NULL; |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 308 | |
| 309 | ret = power_supply_register(di->dev, &di->bat); |
| 310 | if (ret) { |
| 311 | dev_err(di->dev, "failed to register battery: %d\n", ret); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 316 | |
| 317 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 321 | * i2c specific code |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 322 | */ |
| 323 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 324 | static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, |
| 325 | int *rt_value, bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 326 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 327 | struct i2c_client *client = to_i2c_client(di->dev); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 328 | struct i2c_msg msg[1]; |
| 329 | unsigned char data[2]; |
| 330 | int err; |
| 331 | |
| 332 | if (!client->adapter) |
| 333 | return -ENODEV; |
| 334 | |
| 335 | msg->addr = client->addr; |
| 336 | msg->flags = 0; |
| 337 | msg->len = 1; |
| 338 | msg->buf = data; |
| 339 | |
| 340 | data[0] = reg; |
| 341 | err = i2c_transfer(client->adapter, msg, 1); |
| 342 | |
| 343 | if (err >= 0) { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 344 | if (!single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 345 | msg->len = 2; |
| 346 | else |
| 347 | msg->len = 1; |
| 348 | |
| 349 | msg->flags = I2C_M_RD; |
| 350 | err = i2c_transfer(client->adapter, msg, 1); |
| 351 | if (err >= 0) { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 352 | if (!single) |
Grazvydas Ignotas | 97f70c2 | 2010-02-12 23:56:46 +0200 | [diff] [blame] | 353 | *rt_value = get_unaligned_le16(data); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 354 | else |
| 355 | *rt_value = data[0]; |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | } |
| 360 | return err; |
| 361 | } |
| 362 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 363 | static int bq27x00_battery_probe(struct i2c_client *client, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 364 | const struct i2c_device_id *id) |
| 365 | { |
| 366 | char *name; |
| 367 | struct bq27x00_device_info *di; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 368 | int num; |
| 369 | int retval = 0; |
| 370 | |
| 371 | /* Get new ID for the new battery device */ |
| 372 | retval = idr_pre_get(&battery_id, GFP_KERNEL); |
| 373 | if (retval == 0) |
| 374 | return -ENOMEM; |
| 375 | mutex_lock(&battery_mutex); |
| 376 | retval = idr_get_new(&battery_id, client, &num); |
| 377 | mutex_unlock(&battery_mutex); |
| 378 | if (retval < 0) |
| 379 | return retval; |
| 380 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 381 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 382 | if (!name) { |
| 383 | dev_err(&client->dev, "failed to allocate device name\n"); |
| 384 | retval = -ENOMEM; |
| 385 | goto batt_failed_1; |
| 386 | } |
| 387 | |
| 388 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 389 | if (!di) { |
| 390 | dev_err(&client->dev, "failed to allocate device info data\n"); |
| 391 | retval = -ENOMEM; |
| 392 | goto batt_failed_2; |
| 393 | } |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 394 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame^] | 395 | di->id = num; |
| 396 | di->dev = &client->dev; |
| 397 | di->chip = id->driver_data; |
| 398 | di->bat.name = name; |
| 399 | di->bus.read = &bq27x00_read_i2c; |
| 400 | |
| 401 | if (bq27x00_powersupply_init(di)) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 402 | goto batt_failed_3; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 403 | |
| 404 | i2c_set_clientdata(client, di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 408 | batt_failed_3: |
| 409 | kfree(di); |
| 410 | batt_failed_2: |
| 411 | kfree(name); |
| 412 | batt_failed_1: |
| 413 | mutex_lock(&battery_mutex); |
| 414 | idr_remove(&battery_id, num); |
| 415 | mutex_unlock(&battery_mutex); |
| 416 | |
| 417 | return retval; |
| 418 | } |
| 419 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 420 | static int bq27x00_battery_remove(struct i2c_client *client) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 421 | { |
| 422 | struct bq27x00_device_info *di = i2c_get_clientdata(client); |
| 423 | |
| 424 | power_supply_unregister(&di->bat); |
| 425 | |
| 426 | kfree(di->bat.name); |
| 427 | |
| 428 | mutex_lock(&battery_mutex); |
| 429 | idr_remove(&battery_id, di->id); |
| 430 | mutex_unlock(&battery_mutex); |
| 431 | |
| 432 | kfree(di); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Module stuff |
| 439 | */ |
| 440 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 441 | static const struct i2c_device_id bq27x00_id[] = { |
| 442 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ |
| 443 | { "bq27500", BQ27500 }, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 444 | {}, |
| 445 | }; |
| 446 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 447 | static struct i2c_driver bq27x00_battery_driver = { |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 448 | .driver = { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 449 | .name = "bq27x00-battery", |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 450 | }, |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 451 | .probe = bq27x00_battery_probe, |
| 452 | .remove = bq27x00_battery_remove, |
| 453 | .id_table = bq27x00_id, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | static int __init bq27x00_battery_init(void) |
| 457 | { |
| 458 | int ret; |
| 459 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 460 | ret = i2c_add_driver(&bq27x00_battery_driver); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 461 | if (ret) |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 462 | printk(KERN_ERR "Unable to register BQ27x00 driver\n"); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 463 | |
| 464 | return ret; |
| 465 | } |
| 466 | module_init(bq27x00_battery_init); |
| 467 | |
| 468 | static void __exit bq27x00_battery_exit(void) |
| 469 | { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 470 | i2c_del_driver(&bq27x00_battery_driver); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 471 | } |
| 472 | module_exit(bq27x00_battery_exit); |
| 473 | |
| 474 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 475 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); |
| 476 | MODULE_LICENSE("GPL"); |