MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Fuel gauge driver for Maxim 17042 / 8966 / 8997 |
| 3 | * Note that Maxim 8966 and 8997 are mfd and this is its subdevice. |
| 4 | * |
| 5 | * Copyright (C) 2011 Samsung Electronics |
| 6 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * This driver is based on max17040_battery.c |
| 23 | */ |
| 24 | |
| 25 | #include <linux/init.h> |
Paul Gortmaker | 7e6d62d | 2011-07-03 15:28:29 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/i2c.h> |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 29 | #include <linux/delay.h> |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 30 | #include <linux/interrupt.h> |
Ramakrishna Pallala | 48bc177 | 2012-03-27 02:23:40 +0530 | [diff] [blame] | 31 | #include <linux/pm.h> |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 32 | #include <linux/mod_devicetable.h> |
| 33 | #include <linux/power_supply.h> |
| 34 | #include <linux/power/max17042_battery.h> |
Karol Lewandowski | 3832246 | 2012-02-22 19:06:22 +0100 | [diff] [blame] | 35 | #include <linux/of.h> |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 36 | #include <linux/regmap.h> |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 37 | |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 38 | /* Status register bits */ |
| 39 | #define STATUS_POR_BIT (1 << 1) |
| 40 | #define STATUS_BST_BIT (1 << 3) |
| 41 | #define STATUS_VMN_BIT (1 << 8) |
| 42 | #define STATUS_TMN_BIT (1 << 9) |
| 43 | #define STATUS_SMN_BIT (1 << 10) |
| 44 | #define STATUS_BI_BIT (1 << 11) |
| 45 | #define STATUS_VMX_BIT (1 << 12) |
| 46 | #define STATUS_TMX_BIT (1 << 13) |
| 47 | #define STATUS_SMX_BIT (1 << 14) |
| 48 | #define STATUS_BR_BIT (1 << 15) |
| 49 | |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 50 | /* Interrupt mask bits */ |
| 51 | #define CONFIG_ALRT_BIT_ENBL (1 << 2) |
Ramakrishna Pallala | 5cdd4d7 | 2012-03-21 03:03:16 +0530 | [diff] [blame] | 52 | #define STATUS_INTR_SOCMIN_BIT (1 << 10) |
| 53 | #define STATUS_INTR_SOCMAX_BIT (1 << 14) |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 54 | |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 55 | #define VFSOC0_LOCK 0x0000 |
| 56 | #define VFSOC0_UNLOCK 0x0080 |
| 57 | #define MODEL_UNLOCK1 0X0059 |
| 58 | #define MODEL_UNLOCK2 0X00C4 |
| 59 | #define MODEL_LOCK1 0X0000 |
| 60 | #define MODEL_LOCK2 0X0000 |
| 61 | |
| 62 | #define dQ_ACC_DIV 0x4 |
| 63 | #define dP_ACC_100 0x1900 |
| 64 | #define dP_ACC_200 0x3200 |
| 65 | |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 66 | #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */ |
| 67 | |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 68 | struct max17042_chip { |
| 69 | struct i2c_client *client; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 70 | struct regmap *regmap; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 71 | struct power_supply *battery; |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 72 | enum max170xx_chip_type chip_type; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 73 | struct max17042_platform_data *pdata; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 74 | struct work_struct work; |
| 75 | int init_complete; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 76 | }; |
| 77 | |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 78 | static enum power_supply_property max17042_battery_props[] = { |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 79 | POWER_SUPPLY_PROP_PRESENT, |
| 80 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 81 | POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 82 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 83 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 84 | POWER_SUPPLY_PROP_VOLTAGE_AVG, |
Ramakrishna Pallala | a2ebfe2 | 2012-04-10 16:21:20 +0530 | [diff] [blame] | 85 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 86 | POWER_SUPPLY_PROP_CAPACITY, |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 87 | POWER_SUPPLY_PROP_CHARGE_FULL, |
Ramakrishna Pallala | 5fc55bc | 2012-05-07 10:25:58 +0530 | [diff] [blame] | 88 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 89 | POWER_SUPPLY_PROP_TEMP, |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 90 | POWER_SUPPLY_PROP_TEMP_ALERT_MIN, |
| 91 | POWER_SUPPLY_PROP_TEMP_ALERT_MAX, |
| 92 | POWER_SUPPLY_PROP_TEMP_MIN, |
| 93 | POWER_SUPPLY_PROP_TEMP_MAX, |
| 94 | POWER_SUPPLY_PROP_HEALTH, |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 95 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 96 | POWER_SUPPLY_PROP_CURRENT_AVG, |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 97 | }; |
| 98 | |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 99 | static int max17042_get_temperature(struct max17042_chip *chip, int *temp) |
| 100 | { |
| 101 | int ret; |
| 102 | u32 data; |
| 103 | struct regmap *map = chip->regmap; |
| 104 | |
| 105 | ret = regmap_read(map, MAX17042_TEMP, &data); |
| 106 | if (ret < 0) |
| 107 | return ret; |
| 108 | |
| 109 | *temp = data; |
| 110 | /* The value is signed. */ |
| 111 | if (*temp & 0x8000) { |
| 112 | *temp = (0x7fff & ~*temp) + 1; |
| 113 | *temp *= -1; |
| 114 | } |
| 115 | |
| 116 | /* The value is converted into deci-centigrade scale */ |
| 117 | /* Units of LSB = 1 / 256 degree Celsius */ |
| 118 | *temp = *temp * 10 / 256; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int max17042_get_battery_health(struct max17042_chip *chip, int *health) |
| 123 | { |
| 124 | int temp, vavg, vbatt, ret; |
| 125 | u32 val; |
| 126 | |
| 127 | ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val); |
| 128 | if (ret < 0) |
| 129 | goto health_error; |
| 130 | |
| 131 | /* bits [0-3] unused */ |
| 132 | vavg = val * 625 / 8; |
| 133 | /* Convert to millivolts */ |
| 134 | vavg /= 1000; |
| 135 | |
| 136 | ret = regmap_read(chip->regmap, MAX17042_VCELL, &val); |
| 137 | if (ret < 0) |
| 138 | goto health_error; |
| 139 | |
| 140 | /* bits [0-3] unused */ |
| 141 | vbatt = val * 625 / 8; |
| 142 | /* Convert to millivolts */ |
| 143 | vbatt /= 1000; |
| 144 | |
| 145 | if (vavg < chip->pdata->vmin) { |
| 146 | *health = POWER_SUPPLY_HEALTH_DEAD; |
| 147 | goto out; |
| 148 | } |
| 149 | |
| 150 | if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) { |
| 151 | *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 152 | goto out; |
| 153 | } |
| 154 | |
| 155 | ret = max17042_get_temperature(chip, &temp); |
| 156 | if (ret < 0) |
| 157 | goto health_error; |
| 158 | |
| 159 | if (temp <= chip->pdata->temp_min) { |
| 160 | *health = POWER_SUPPLY_HEALTH_COLD; |
| 161 | goto out; |
| 162 | } |
| 163 | |
| 164 | if (temp >= chip->pdata->temp_max) { |
| 165 | *health = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | *health = POWER_SUPPLY_HEALTH_GOOD; |
| 170 | |
| 171 | out: |
| 172 | return 0; |
| 173 | |
| 174 | health_error: |
| 175 | return ret; |
| 176 | } |
| 177 | |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 178 | static int max17042_get_property(struct power_supply *psy, |
| 179 | enum power_supply_property psp, |
| 180 | union power_supply_propval *val) |
| 181 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 182 | struct max17042_chip *chip = power_supply_get_drvdata(psy); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 183 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 184 | int ret; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 185 | u32 data; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 186 | |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 187 | if (!chip->init_complete) |
| 188 | return -EAGAIN; |
| 189 | |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 190 | switch (psp) { |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 191 | case POWER_SUPPLY_PROP_PRESENT: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 192 | ret = regmap_read(map, MAX17042_STATUS, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 193 | if (ret < 0) |
| 194 | return ret; |
| 195 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 196 | if (data & MAX17042_STATUS_BattAbsent) |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 197 | val->intval = 0; |
| 198 | else |
| 199 | val->intval = 1; |
| 200 | break; |
| 201 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 202 | ret = regmap_read(map, MAX17042_Cycles, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 206 | val->intval = data; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 207 | break; |
| 208 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 209 | ret = regmap_read(map, MAX17042_MinMaxVolt, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 210 | if (ret < 0) |
| 211 | return ret; |
| 212 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 213 | val->intval = data >> 8; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 214 | val->intval *= 20000; /* Units of LSB = 20mV */ |
| 215 | break; |
| 216 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Beomho Seo | 709c2c7 | 2015-04-03 17:26:08 +0900 | [diff] [blame] | 217 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 218 | ret = regmap_read(map, MAX17042_V_empty, &data); |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 219 | else |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 220 | ret = regmap_read(map, MAX17047_V_empty, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 224 | val->intval = data >> 7; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 225 | val->intval *= 10000; /* Units of LSB = 10mV */ |
| 226 | break; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 227 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 228 | ret = regmap_read(map, MAX17042_VCELL, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 229 | if (ret < 0) |
| 230 | return ret; |
| 231 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 232 | val->intval = data * 625 / 8; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 233 | break; |
| 234 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 235 | ret = regmap_read(map, MAX17042_AvgVCELL, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 236 | if (ret < 0) |
| 237 | return ret; |
| 238 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 239 | val->intval = data * 625 / 8; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 240 | break; |
Ramakrishna Pallala | a2ebfe2 | 2012-04-10 16:21:20 +0530 | [diff] [blame] | 241 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 242 | ret = regmap_read(map, MAX17042_OCVInternal, &data); |
Ramakrishna Pallala | a2ebfe2 | 2012-04-10 16:21:20 +0530 | [diff] [blame] | 243 | if (ret < 0) |
| 244 | return ret; |
| 245 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 246 | val->intval = data * 625 / 8; |
Ramakrishna Pallala | a2ebfe2 | 2012-04-10 16:21:20 +0530 | [diff] [blame] | 247 | break; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 248 | case POWER_SUPPLY_PROP_CAPACITY: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 249 | ret = regmap_read(map, MAX17042_RepSOC, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 250 | if (ret < 0) |
| 251 | return ret; |
| 252 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 253 | val->intval = data >> 8; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 254 | break; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 255 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 256 | ret = regmap_read(map, MAX17042_FullCAP, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 257 | if (ret < 0) |
| 258 | return ret; |
| 259 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 260 | val->intval = data * 1000 / 2; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 261 | break; |
Ramakrishna Pallala | 5fc55bc | 2012-05-07 10:25:58 +0530 | [diff] [blame] | 262 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 263 | ret = regmap_read(map, MAX17042_QH, &data); |
Ramakrishna Pallala | 5fc55bc | 2012-05-07 10:25:58 +0530 | [diff] [blame] | 264 | if (ret < 0) |
| 265 | return ret; |
| 266 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 267 | val->intval = data * 1000 / 2; |
Ramakrishna Pallala | 5fc55bc | 2012-05-07 10:25:58 +0530 | [diff] [blame] | 268 | break; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 269 | case POWER_SUPPLY_PROP_TEMP: |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 270 | ret = max17042_get_temperature(chip, &val->intval); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 271 | if (ret < 0) |
| 272 | return ret; |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 273 | break; |
| 274 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 275 | ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 276 | if (ret < 0) |
| 277 | return ret; |
| 278 | /* LSB is Alert Minimum. In deci-centigrade */ |
| 279 | val->intval = (data & 0xff) * 10; |
| 280 | break; |
| 281 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 282 | ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 283 | if (ret < 0) |
| 284 | return ret; |
| 285 | /* MSB is Alert Maximum. In deci-centigrade */ |
| 286 | val->intval = (data >> 8) * 10; |
| 287 | break; |
| 288 | case POWER_SUPPLY_PROP_TEMP_MIN: |
| 289 | val->intval = chip->pdata->temp_min; |
| 290 | break; |
| 291 | case POWER_SUPPLY_PROP_TEMP_MAX: |
| 292 | val->intval = chip->pdata->temp_max; |
| 293 | break; |
| 294 | case POWER_SUPPLY_PROP_HEALTH: |
| 295 | ret = max17042_get_battery_health(chip, &val->intval); |
| 296 | if (ret < 0) |
| 297 | return ret; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 298 | break; |
| 299 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 300 | if (chip->pdata->enable_current_sense) { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 301 | ret = regmap_read(map, MAX17042_Current, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 302 | if (ret < 0) |
| 303 | return ret; |
| 304 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 305 | val->intval = data; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 306 | if (val->intval & 0x8000) { |
| 307 | /* Negative */ |
| 308 | val->intval = ~val->intval & 0x7fff; |
| 309 | val->intval++; |
| 310 | val->intval *= -1; |
| 311 | } |
Philip Rakity | 91d8b0d | 2011-08-12 21:19:57 -0700 | [diff] [blame] | 312 | val->intval *= 1562500 / chip->pdata->r_sns; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 313 | } else { |
| 314 | return -EINVAL; |
| 315 | } |
| 316 | break; |
| 317 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
| 318 | if (chip->pdata->enable_current_sense) { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 319 | ret = regmap_read(map, MAX17042_AvgCurrent, &data); |
Ramakrishna Pallala | 60a1f6e | 2011-11-26 04:11:15 +0400 | [diff] [blame] | 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 323 | val->intval = data; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 324 | if (val->intval & 0x8000) { |
| 325 | /* Negative */ |
| 326 | val->intval = ~val->intval & 0x7fff; |
| 327 | val->intval++; |
| 328 | val->intval *= -1; |
| 329 | } |
| 330 | val->intval *= 1562500 / chip->pdata->r_sns; |
| 331 | } else { |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | break; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 335 | default: |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 341 | static int max17042_set_property(struct power_supply *psy, |
| 342 | enum power_supply_property psp, |
| 343 | const union power_supply_propval *val) |
| 344 | { |
| 345 | struct max17042_chip *chip = power_supply_get_drvdata(psy); |
| 346 | struct regmap *map = chip->regmap; |
| 347 | int ret = 0; |
| 348 | u32 data; |
| 349 | int8_t temp; |
| 350 | |
| 351 | switch (psp) { |
| 352 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 353 | ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | |
| 357 | /* Input in deci-centigrade, convert to centigrade */ |
| 358 | temp = val->intval / 10; |
| 359 | /* force min < max */ |
| 360 | if (temp >= (int8_t)(data >> 8)) |
| 361 | temp = (int8_t)(data >> 8) - 1; |
| 362 | /* Write both MAX and MIN ALERT */ |
| 363 | data = (data & 0xff00) + temp; |
| 364 | ret = regmap_write(map, MAX17042_TALRT_Th, data); |
| 365 | break; |
| 366 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 367 | ret = regmap_read(map, MAX17042_TALRT_Th, &data); |
| 368 | if (ret < 0) |
| 369 | return ret; |
| 370 | |
| 371 | /* Input in Deci-Centigrade, convert to centigrade */ |
| 372 | temp = val->intval / 10; |
| 373 | /* force max > min */ |
| 374 | if (temp <= (int8_t)(data & 0xff)) |
| 375 | temp = (int8_t)(data & 0xff) + 1; |
| 376 | /* Write both MAX and MIN ALERT */ |
| 377 | data = (data & 0xff) + (temp << 8); |
| 378 | ret = regmap_write(map, MAX17042_TALRT_Th, data); |
| 379 | break; |
| 380 | default: |
| 381 | ret = -EINVAL; |
| 382 | } |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static int max17042_property_is_writeable(struct power_supply *psy, |
| 388 | enum power_supply_property psp) |
| 389 | { |
| 390 | int ret; |
| 391 | |
| 392 | switch (psp) { |
| 393 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 394 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 395 | ret = 1; |
| 396 | break; |
| 397 | default: |
| 398 | ret = 0; |
| 399 | } |
| 400 | |
| 401 | return ret; |
| 402 | } |
| 403 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 404 | static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value) |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 405 | { |
| 406 | int retries = 8; |
| 407 | int ret; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 408 | u32 read_value; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 409 | |
| 410 | do { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 411 | ret = regmap_write(map, reg, value); |
| 412 | regmap_read(map, reg, &read_value); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 413 | if (read_value != value) { |
| 414 | ret = -EIO; |
| 415 | retries--; |
| 416 | } |
| 417 | } while (retries && read_value != value); |
| 418 | |
| 419 | if (ret < 0) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 420 | pr_err("%s: err %d\n", __func__, ret); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 425 | static inline void max17042_override_por(struct regmap *map, |
| 426 | u8 reg, u16 value) |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 427 | { |
| 428 | if (value) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 429 | regmap_write(map, reg, value); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static inline void max10742_unlock_model(struct max17042_chip *chip) |
| 433 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 434 | struct regmap *map = chip->regmap; |
Beomho Seo | bbaeeaa | 2015-04-03 17:26:09 +0900 | [diff] [blame] | 435 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 436 | regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1); |
| 437 | regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static inline void max10742_lock_model(struct max17042_chip *chip) |
| 441 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 442 | struct regmap *map = chip->regmap; |
| 443 | |
| 444 | regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1); |
| 445 | regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static inline void max17042_write_model_data(struct max17042_chip *chip, |
| 449 | u8 addr, int size) |
| 450 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 451 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 452 | int i; |
Beomho Seo | bbaeeaa | 2015-04-03 17:26:09 +0900 | [diff] [blame] | 453 | |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 454 | for (i = 0; i < size; i++) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 455 | regmap_write(map, addr + i, |
| 456 | chip->pdata->config_data->cell_char_tbl[i]); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static inline void max17042_read_model_data(struct max17042_chip *chip, |
Sven Van Asbroeck | 5381cfb | 2016-08-12 09:10:27 -0400 | [diff] [blame] | 460 | u8 addr, u16 *data, int size) |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 461 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 462 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 463 | int i; |
Sven Van Asbroeck | 5381cfb | 2016-08-12 09:10:27 -0400 | [diff] [blame] | 464 | u32 tmp; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 465 | |
Sven Van Asbroeck | 5381cfb | 2016-08-12 09:10:27 -0400 | [diff] [blame] | 466 | for (i = 0; i < size; i++) { |
| 467 | regmap_read(map, addr + i, &tmp); |
| 468 | data[i] = (u16)tmp; |
| 469 | } |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static inline int max17042_model_data_compare(struct max17042_chip *chip, |
| 473 | u16 *data1, u16 *data2, int size) |
| 474 | { |
| 475 | int i; |
| 476 | |
| 477 | if (memcmp(data1, data2, size)) { |
| 478 | dev_err(&chip->client->dev, "%s compare failed\n", __func__); |
| 479 | for (i = 0; i < size; i++) |
| 480 | dev_info(&chip->client->dev, "0x%x, 0x%x", |
| 481 | data1[i], data2[i]); |
| 482 | dev_info(&chip->client->dev, "\n"); |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int max17042_init_model(struct max17042_chip *chip) |
| 489 | { |
| 490 | int ret; |
Dan Carpenter | 1ef3d8f | 2012-03-15 14:37:32 +0300 | [diff] [blame] | 491 | int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); |
Sven Van Asbroeck | 5381cfb | 2016-08-12 09:10:27 -0400 | [diff] [blame] | 492 | u16 *temp_data; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 493 | |
Dan Carpenter | 1ef3d8f | 2012-03-15 14:37:32 +0300 | [diff] [blame] | 494 | temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 495 | if (!temp_data) |
| 496 | return -ENOMEM; |
| 497 | |
| 498 | max10742_unlock_model(chip); |
| 499 | max17042_write_model_data(chip, MAX17042_MODELChrTbl, |
| 500 | table_size); |
| 501 | max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, |
| 502 | table_size); |
| 503 | |
| 504 | ret = max17042_model_data_compare( |
| 505 | chip, |
| 506 | chip->pdata->config_data->cell_char_tbl, |
Sven Van Asbroeck | 5381cfb | 2016-08-12 09:10:27 -0400 | [diff] [blame] | 507 | temp_data, |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 508 | table_size); |
| 509 | |
| 510 | max10742_lock_model(chip); |
| 511 | kfree(temp_data); |
| 512 | |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | static int max17042_verify_model_lock(struct max17042_chip *chip) |
| 517 | { |
| 518 | int i; |
Dan Carpenter | 1ef3d8f | 2012-03-15 14:37:32 +0300 | [diff] [blame] | 519 | int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); |
Sven Van Asbroeck | 5381cfb | 2016-08-12 09:10:27 -0400 | [diff] [blame] | 520 | u16 *temp_data; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 521 | int ret = 0; |
| 522 | |
Dan Carpenter | 1ef3d8f | 2012-03-15 14:37:32 +0300 | [diff] [blame] | 523 | temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 524 | if (!temp_data) |
| 525 | return -ENOMEM; |
| 526 | |
| 527 | max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, |
| 528 | table_size); |
| 529 | for (i = 0; i < table_size; i++) |
| 530 | if (temp_data[i]) |
| 531 | ret = -EINVAL; |
| 532 | |
| 533 | kfree(temp_data); |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | static void max17042_write_config_regs(struct max17042_chip *chip) |
| 538 | { |
| 539 | struct max17042_config_data *config = chip->pdata->config_data; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 540 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 541 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 542 | regmap_write(map, MAX17042_CONFIG, config->config); |
| 543 | regmap_write(map, MAX17042_LearnCFG, config->learn_cfg); |
| 544 | regmap_write(map, MAX17042_FilterCFG, |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 545 | config->filter_cfg); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 546 | regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg); |
Beomho Seo | 709c2c7 | 2015-04-03 17:26:08 +0900 | [diff] [blame] | 547 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 || |
| 548 | chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 549 | regmap_write(map, MAX17047_FullSOCThr, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 550 | config->full_soc_thresh); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static void max17042_write_custom_regs(struct max17042_chip *chip) |
| 554 | { |
| 555 | struct max17042_config_data *config = chip->pdata->config_data; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 556 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 557 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 558 | max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0); |
| 559 | max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0); |
| 560 | max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term); |
Beomho Seo | 709c2c7 | 2015-04-03 17:26:08 +0900 | [diff] [blame] | 561 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 562 | regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco); |
| 563 | max17042_write_verify_reg(map, MAX17042_K_empty0, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 564 | config->kempty0); |
| 565 | } else { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 566 | max17042_write_verify_reg(map, MAX17047_QRTbl00, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 567 | config->qrtbl00); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 568 | max17042_write_verify_reg(map, MAX17047_QRTbl10, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 569 | config->qrtbl10); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 570 | max17042_write_verify_reg(map, MAX17047_QRTbl20, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 571 | config->qrtbl20); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 572 | max17042_write_verify_reg(map, MAX17047_QRTbl30, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 573 | config->qrtbl30); |
| 574 | } |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static void max17042_update_capacity_regs(struct max17042_chip *chip) |
| 578 | { |
| 579 | struct max17042_config_data *config = chip->pdata->config_data; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 580 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 581 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 582 | max17042_write_verify_reg(map, MAX17042_FullCAP, |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 583 | config->fullcap); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 584 | regmap_write(map, MAX17042_DesignCap, config->design_cap); |
| 585 | max17042_write_verify_reg(map, MAX17042_FullCAPNom, |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 586 | config->fullcapnom); |
| 587 | } |
| 588 | |
| 589 | static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip) |
| 590 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 591 | unsigned int vfSoc; |
| 592 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 593 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 594 | regmap_read(map, MAX17042_VFSOC, &vfSoc); |
| 595 | regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK); |
| 596 | max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc); |
| 597 | regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void max17042_load_new_capacity_params(struct max17042_chip *chip) |
| 601 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 602 | u32 full_cap0, rep_cap, dq_acc, vfSoc; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 603 | u32 rem_cap; |
| 604 | |
| 605 | struct max17042_config_data *config = chip->pdata->config_data; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 606 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 607 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 608 | regmap_read(map, MAX17042_FullCAP0, &full_cap0); |
| 609 | regmap_read(map, MAX17042_VFSOC, &vfSoc); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 610 | |
| 611 | /* fg_vfSoc needs to shifted by 8 bits to get the |
| 612 | * perc in 1% accuracy, to get the right rem_cap multiply |
| 613 | * full_cap0, fg_vfSoc and devide by 100 |
| 614 | */ |
| 615 | rem_cap = ((vfSoc >> 8) * full_cap0) / 100; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 616 | max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 617 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 618 | rep_cap = rem_cap; |
| 619 | max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 620 | |
| 621 | /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */ |
| 622 | dq_acc = config->fullcap / dQ_ACC_DIV; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 623 | max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc); |
| 624 | max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 625 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 626 | max17042_write_verify_reg(map, MAX17042_FullCAP, |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 627 | config->fullcap); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 628 | regmap_write(map, MAX17042_DesignCap, |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 629 | config->design_cap); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 630 | max17042_write_verify_reg(map, MAX17042_FullCAPNom, |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 631 | config->fullcapnom); |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 632 | /* Update SOC register with new SOC */ |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 633 | regmap_write(map, MAX17042_RepSOC, vfSoc); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Block write all the override values coming from platform data. |
| 638 | * This function MUST be called before the POR initialization proceedure |
| 639 | * specified by maxim. |
| 640 | */ |
| 641 | static inline void max17042_override_por_values(struct max17042_chip *chip) |
| 642 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 643 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 644 | struct max17042_config_data *config = chip->pdata->config_data; |
| 645 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 646 | max17042_override_por(map, MAX17042_TGAIN, config->tgain); |
| 647 | max17042_override_por(map, MAx17042_TOFF, config->toff); |
| 648 | max17042_override_por(map, MAX17042_CGAIN, config->cgain); |
| 649 | max17042_override_por(map, MAX17042_COFF, config->coff); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 650 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 651 | max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh); |
| 652 | max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh); |
| 653 | max17042_override_por(map, MAX17042_SALRT_Th, |
| 654 | config->soc_alrt_thresh); |
| 655 | max17042_override_por(map, MAX17042_CONFIG, config->config); |
| 656 | max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 657 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 658 | max17042_override_por(map, MAX17042_DesignCap, config->design_cap); |
| 659 | max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 660 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 661 | max17042_override_por(map, MAX17042_AtRate, config->at_rate); |
| 662 | max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg); |
| 663 | max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg); |
| 664 | max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg); |
| 665 | max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg); |
| 666 | max17042_override_por(map, MAX17042_MaskSOC, config->masksoc); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 667 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 668 | max17042_override_por(map, MAX17042_FullCAP, config->fullcap); |
| 669 | max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom); |
Beomho Seo | 709c2c7 | 2015-04-03 17:26:08 +0900 | [diff] [blame] | 670 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 671 | max17042_override_por(map, MAX17042_SOC_empty, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 672 | config->socempty); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 673 | max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty); |
| 674 | max17042_override_por(map, MAX17042_dQacc, config->dqacc); |
| 675 | max17042_override_por(map, MAX17042_dPacc, config->dpacc); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 676 | |
Beomho Seo | 709c2c7 | 2015-04-03 17:26:08 +0900 | [diff] [blame] | 677 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 678 | max17042_override_por(map, MAX17042_V_empty, config->vempty); |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 679 | else |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 680 | max17042_override_por(map, MAX17047_V_empty, config->vempty); |
| 681 | max17042_override_por(map, MAX17042_TempNom, config->temp_nom); |
| 682 | max17042_override_por(map, MAX17042_TempLim, config->temp_lim); |
| 683 | max17042_override_por(map, MAX17042_FCTC, config->fctc); |
| 684 | max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0); |
| 685 | max17042_override_por(map, MAX17042_TempCo, config->tcompc0); |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 686 | if (chip->chip_type) { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 687 | max17042_override_por(map, MAX17042_EmptyTempCo, |
| 688 | config->empty_tempco); |
| 689 | max17042_override_por(map, MAX17042_K_empty0, |
| 690 | config->kempty0); |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 691 | } |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static int max17042_init_chip(struct max17042_chip *chip) |
| 695 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 696 | struct regmap *map = chip->regmap; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 697 | int ret; |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 698 | |
| 699 | max17042_override_por_values(chip); |
| 700 | /* After Power up, the MAX17042 requires 500mS in order |
| 701 | * to perform signal debouncing and initial SOC reporting |
| 702 | */ |
| 703 | msleep(500); |
| 704 | |
| 705 | /* Initialize configaration */ |
| 706 | max17042_write_config_regs(chip); |
| 707 | |
| 708 | /* write cell characterization data */ |
| 709 | ret = max17042_init_model(chip); |
| 710 | if (ret) { |
| 711 | dev_err(&chip->client->dev, "%s init failed\n", |
| 712 | __func__); |
| 713 | return -EIO; |
| 714 | } |
Alan Cox | a879f19 | 2012-11-18 14:59:47 -0800 | [diff] [blame] | 715 | |
| 716 | ret = max17042_verify_model_lock(chip); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 717 | if (ret) { |
| 718 | dev_err(&chip->client->dev, "%s lock verify failed\n", |
| 719 | __func__); |
| 720 | return -EIO; |
| 721 | } |
| 722 | /* write custom parameters */ |
| 723 | max17042_write_custom_regs(chip); |
| 724 | |
| 725 | /* update capacity params */ |
| 726 | max17042_update_capacity_regs(chip); |
| 727 | |
| 728 | /* delay must be atleast 350mS to allow VFSOC |
| 729 | * to be calculated from the new configuration |
| 730 | */ |
| 731 | msleep(350); |
| 732 | |
| 733 | /* reset vfsoc0 reg */ |
| 734 | max17042_reset_vfsoc0_reg(chip); |
| 735 | |
| 736 | /* load new capacity params */ |
| 737 | max17042_load_new_capacity_params(chip); |
| 738 | |
| 739 | /* Init complete, Clear the POR bit */ |
Krzysztof Kozlowski | bc35268 | 2015-02-24 10:54:47 +0100 | [diff] [blame] | 740 | regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0); |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 744 | static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off) |
| 745 | { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 746 | struct regmap *map = chip->regmap; |
| 747 | u32 soc, soc_tr; |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 748 | |
| 749 | /* program interrupt thesholds such that we should |
| 750 | * get interrupt for every 'off' perc change in the soc |
| 751 | */ |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 752 | regmap_read(map, MAX17042_RepSOC, &soc); |
| 753 | soc >>= 8; |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 754 | soc_tr = (soc + off) << 8; |
| 755 | soc_tr |= (soc - off); |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 756 | regmap_write(map, MAX17042_SALRT_Th, soc_tr); |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 759 | static irqreturn_t max17042_thread_handler(int id, void *dev) |
| 760 | { |
| 761 | struct max17042_chip *chip = dev; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 762 | u32 val; |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 763 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 764 | regmap_read(chip->regmap, MAX17042_STATUS, &val); |
Ramakrishna Pallala | 5cdd4d7 | 2012-03-21 03:03:16 +0530 | [diff] [blame] | 765 | if ((val & STATUS_INTR_SOCMIN_BIT) || |
| 766 | (val & STATUS_INTR_SOCMAX_BIT)) { |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 767 | dev_info(&chip->client->dev, "SOC threshold INTR\n"); |
| 768 | max17042_set_soc_threshold(chip, 1); |
| 769 | } |
| 770 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 771 | power_supply_changed(chip->battery); |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 772 | return IRQ_HANDLED; |
| 773 | } |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 774 | |
| 775 | static void max17042_init_worker(struct work_struct *work) |
| 776 | { |
| 777 | struct max17042_chip *chip = container_of(work, |
| 778 | struct max17042_chip, work); |
| 779 | int ret; |
| 780 | |
| 781 | /* Initialize registers according to values from the platform data */ |
| 782 | if (chip->pdata->enable_por_init && chip->pdata->config_data) { |
| 783 | ret = max17042_init_chip(chip); |
| 784 | if (ret) |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | chip->init_complete = 1; |
| 789 | } |
| 790 | |
Karol Lewandowski | 3832246 | 2012-02-22 19:06:22 +0100 | [diff] [blame] | 791 | #ifdef CONFIG_OF |
| 792 | static struct max17042_platform_data * |
| 793 | max17042_get_pdata(struct device *dev) |
| 794 | { |
| 795 | struct device_node *np = dev->of_node; |
| 796 | u32 prop; |
| 797 | struct max17042_platform_data *pdata; |
| 798 | |
| 799 | if (!np) |
| 800 | return dev->platform_data; |
| 801 | |
| 802 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 803 | if (!pdata) |
| 804 | return NULL; |
| 805 | |
| 806 | /* |
| 807 | * Require current sense resistor value to be specified for |
| 808 | * current-sense functionality to be enabled at all. |
| 809 | */ |
| 810 | if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) { |
| 811 | pdata->r_sns = prop; |
| 812 | pdata->enable_current_sense = true; |
| 813 | } |
| 814 | |
Krzysztof Kozlowski | a6e6b63 | 2015-06-08 10:11:38 +0900 | [diff] [blame] | 815 | if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min)) |
| 816 | pdata->temp_min = INT_MIN; |
| 817 | if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max)) |
| 818 | pdata->temp_max = INT_MAX; |
| 819 | if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin)) |
| 820 | pdata->vmin = INT_MIN; |
| 821 | if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax)) |
| 822 | pdata->vmax = INT_MAX; |
| 823 | |
Karol Lewandowski | 3832246 | 2012-02-22 19:06:22 +0100 | [diff] [blame] | 824 | return pdata; |
| 825 | } |
| 826 | #else |
| 827 | static struct max17042_platform_data * |
| 828 | max17042_get_pdata(struct device *dev) |
| 829 | { |
| 830 | return dev->platform_data; |
| 831 | } |
| 832 | #endif |
| 833 | |
Krzysztof Kozlowski | e029128 | 2015-01-05 09:50:17 +0100 | [diff] [blame] | 834 | static const struct regmap_config max17042_regmap_config = { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 835 | .reg_bits = 8, |
| 836 | .val_bits = 16, |
| 837 | .val_format_endian = REGMAP_ENDIAN_NATIVE, |
| 838 | }; |
| 839 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 840 | static const struct power_supply_desc max17042_psy_desc = { |
| 841 | .name = "max170xx_battery", |
| 842 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 843 | .get_property = max17042_get_property, |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 844 | .set_property = max17042_set_property, |
| 845 | .property_is_writeable = max17042_property_is_writeable, |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 846 | .properties = max17042_battery_props, |
| 847 | .num_properties = ARRAY_SIZE(max17042_battery_props), |
| 848 | }; |
| 849 | |
| 850 | static const struct power_supply_desc max17042_no_current_sense_psy_desc = { |
| 851 | .name = "max170xx_battery", |
| 852 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 853 | .get_property = max17042_get_property, |
Ramakrishna Pallala | edd4ab0 | 2015-05-24 09:11:58 +0530 | [diff] [blame] | 854 | .set_property = max17042_set_property, |
| 855 | .property_is_writeable = max17042_property_is_writeable, |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 856 | .properties = max17042_battery_props, |
| 857 | .num_properties = ARRAY_SIZE(max17042_battery_props) - 2, |
| 858 | }; |
| 859 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 860 | static int max17042_probe(struct i2c_client *client, |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 861 | const struct i2c_device_id *id) |
| 862 | { |
| 863 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 864 | const struct power_supply_desc *max17042_desc = &max17042_psy_desc; |
| 865 | struct power_supply_config psy_cfg = {}; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 866 | struct max17042_chip *chip; |
| 867 | int ret; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 868 | int i; |
| 869 | u32 val; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 870 | |
| 871 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
| 872 | return -EIO; |
| 873 | |
Karol Lewandowski | 2f3b434 | 2012-02-22 19:06:20 +0100 | [diff] [blame] | 874 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 875 | if (!chip) |
| 876 | return -ENOMEM; |
| 877 | |
| 878 | chip->client = client; |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 879 | chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config); |
| 880 | if (IS_ERR(chip->regmap)) { |
| 881 | dev_err(&client->dev, "Failed to initialize regmap\n"); |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | |
Karol Lewandowski | 3832246 | 2012-02-22 19:06:22 +0100 | [diff] [blame] | 885 | chip->pdata = max17042_get_pdata(&client->dev); |
| 886 | if (!chip->pdata) { |
| 887 | dev_err(&client->dev, "no platform data provided\n"); |
| 888 | return -EINVAL; |
| 889 | } |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 890 | |
| 891 | i2c_set_clientdata(client, chip); |
Beomho Seo | 709c2c7 | 2015-04-03 17:26:08 +0900 | [diff] [blame] | 892 | chip->chip_type = id->driver_data; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 893 | psy_cfg.drv_data = chip; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 894 | |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 895 | /* When current is not measured, |
| 896 | * CURRENT_NOW and CURRENT_AVG properties should be invisible. */ |
| 897 | if (!chip->pdata->enable_current_sense) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 898 | max17042_desc = &max17042_no_current_sense_psy_desc; |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 899 | |
Philip Rakity | 4cfa892 | 2011-08-12 21:18:18 -0700 | [diff] [blame] | 900 | if (chip->pdata->r_sns == 0) |
| 901 | chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; |
| 902 | |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 903 | if (chip->pdata->init_data) |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 904 | for (i = 0; i < chip->pdata->num_init_data; i++) |
| 905 | regmap_write(chip->regmap, |
| 906 | chip->pdata->init_data[i].addr, |
| 907 | chip->pdata->init_data[i].data); |
Donggeun Kim | 086ef50 | 2011-06-30 18:07:41 +0900 | [diff] [blame] | 908 | |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 909 | if (!chip->pdata->enable_current_sense) { |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 910 | regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000); |
| 911 | regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003); |
| 912 | regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007); |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 913 | } |
| 914 | |
Vaishali Thakkar | 50bddb9 | 2015-09-17 18:27:52 +0530 | [diff] [blame] | 915 | chip->battery = devm_power_supply_register(&client->dev, max17042_desc, |
| 916 | &psy_cfg); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 917 | if (IS_ERR(chip->battery)) { |
Ramakrishna Pallala | 243e352 | 2012-05-05 03:08:37 +0530 | [diff] [blame] | 918 | dev_err(&client->dev, "failed: power supply register\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 919 | return PTR_ERR(chip->battery); |
Ramakrishna Pallala | 243e352 | 2012-05-05 03:08:37 +0530 | [diff] [blame] | 920 | } |
| 921 | |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 922 | if (client->irq) { |
Vaishali Thakkar | 50bddb9 | 2015-09-17 18:27:52 +0530 | [diff] [blame] | 923 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 924 | NULL, |
| 925 | max17042_thread_handler, |
| 926 | IRQF_TRIGGER_FALLING | |
| 927 | IRQF_ONESHOT, |
| 928 | chip->battery->desc->name, |
| 929 | chip); |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 930 | if (!ret) { |
Krzysztof Kozlowski | bc35268 | 2015-02-24 10:54:47 +0100 | [diff] [blame] | 931 | regmap_update_bits(chip->regmap, MAX17042_CONFIG, |
| 932 | CONFIG_ALRT_BIT_ENBL, |
| 933 | CONFIG_ALRT_BIT_ENBL); |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 934 | max17042_set_soc_threshold(chip, 1); |
Ramakrishna Pallala | e5ba50b | 2012-05-05 04:43:10 +0530 | [diff] [blame] | 935 | } else { |
| 936 | client->irq = 0; |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 937 | dev_err(&client->dev, "%s(): cannot get IRQ\n", |
| 938 | __func__); |
Ramakrishna Pallala | e5ba50b | 2012-05-05 04:43:10 +0530 | [diff] [blame] | 939 | } |
Ramakrishna Pallala | e5f3872 | 2012-01-24 09:26:06 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Jonghwa Lee | 39e7213 | 2013-10-25 13:55:02 +0900 | [diff] [blame] | 942 | regmap_read(chip->regmap, MAX17042_STATUS, &val); |
| 943 | if (val & STATUS_POR_BIT) { |
Ramakrishna Pallala | f3a71a6 | 2012-03-13 22:03:52 +0400 | [diff] [blame] | 944 | INIT_WORK(&chip->work, max17042_init_worker); |
| 945 | schedule_work(&chip->work); |
| 946 | } else { |
| 947 | chip->init_complete = 1; |
| 948 | } |
| 949 | |
Ramakrishna Pallala | 243e352 | 2012-05-05 03:08:37 +0530 | [diff] [blame] | 950 | return 0; |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 951 | } |
| 952 | |
Manish Badarkhe | 3d23c7f | 2013-10-14 10:56:51 +0530 | [diff] [blame] | 953 | #ifdef CONFIG_PM_SLEEP |
Ramakrishna Pallala | 48bc177 | 2012-03-27 02:23:40 +0530 | [diff] [blame] | 954 | static int max17042_suspend(struct device *dev) |
| 955 | { |
| 956 | struct max17042_chip *chip = dev_get_drvdata(dev); |
| 957 | |
Anton Vorontsov | 48e41c7 | 2012-05-04 20:38:13 -0700 | [diff] [blame] | 958 | /* |
| 959 | * disable the irq and enable irq_wake |
Ramakrishna Pallala | 48bc177 | 2012-03-27 02:23:40 +0530 | [diff] [blame] | 960 | * capability to the interrupt line. |
| 961 | */ |
| 962 | if (chip->client->irq) { |
| 963 | disable_irq(chip->client->irq); |
| 964 | enable_irq_wake(chip->client->irq); |
| 965 | } |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static int max17042_resume(struct device *dev) |
| 971 | { |
| 972 | struct max17042_chip *chip = dev_get_drvdata(dev); |
| 973 | |
| 974 | if (chip->client->irq) { |
| 975 | disable_irq_wake(chip->client->irq); |
| 976 | enable_irq(chip->client->irq); |
| 977 | /* re-program the SOC thresholds to 1% change */ |
| 978 | max17042_set_soc_threshold(chip, 1); |
| 979 | } |
| 980 | |
| 981 | return 0; |
| 982 | } |
Ramakrishna Pallala | 48bc177 | 2012-03-27 02:23:40 +0530 | [diff] [blame] | 983 | #endif |
| 984 | |
Manish Badarkhe | 3d23c7f | 2013-10-14 10:56:51 +0530 | [diff] [blame] | 985 | static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend, |
| 986 | max17042_resume); |
| 987 | |
Karol Lewandowski | 3832246 | 2012-02-22 19:06:22 +0100 | [diff] [blame] | 988 | #ifdef CONFIG_OF |
| 989 | static const struct of_device_id max17042_dt_match[] = { |
| 990 | { .compatible = "maxim,max17042" }, |
Ramakrishna Pallala | 9a8422d | 2012-05-05 14:34:26 +0530 | [diff] [blame] | 991 | { .compatible = "maxim,max17047" }, |
| 992 | { .compatible = "maxim,max17050" }, |
Karol Lewandowski | 3832246 | 2012-02-22 19:06:22 +0100 | [diff] [blame] | 993 | { }, |
| 994 | }; |
| 995 | MODULE_DEVICE_TABLE(of, max17042_dt_match); |
| 996 | #endif |
| 997 | |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 998 | static const struct i2c_device_id max17042_id[] = { |
Beomho Seo | 709c2c7 | 2015-04-03 17:26:08 +0900 | [diff] [blame] | 999 | { "max17042", MAXIM_DEVICE_TYPE_MAX17042 }, |
| 1000 | { "max17047", MAXIM_DEVICE_TYPE_MAX17047 }, |
| 1001 | { "max17050", MAXIM_DEVICE_TYPE_MAX17050 }, |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 1002 | { } |
| 1003 | }; |
| 1004 | MODULE_DEVICE_TABLE(i2c, max17042_id); |
| 1005 | |
| 1006 | static struct i2c_driver max17042_i2c_driver = { |
| 1007 | .driver = { |
| 1008 | .name = "max17042", |
Karol Lewandowski | 3832246 | 2012-02-22 19:06:22 +0100 | [diff] [blame] | 1009 | .of_match_table = of_match_ptr(max17042_dt_match), |
Manish Badarkhe | 3d23c7f | 2013-10-14 10:56:51 +0530 | [diff] [blame] | 1010 | .pm = &max17042_pm_ops, |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 1011 | }, |
| 1012 | .probe = max17042_probe, |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 1013 | .id_table = max17042_id, |
| 1014 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 1015 | module_i2c_driver(max17042_i2c_driver); |
MyungJoo Ham | 359ab9f | 2011-01-14 14:46:11 +0900 | [diff] [blame] | 1016 | |
| 1017 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 1018 | MODULE_DESCRIPTION("MAX17042 Fuel Gauge"); |
| 1019 | MODULE_LICENSE("GPL"); |