Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1 | /* |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 2 | * Copyright 2012 ST Ericsson. |
| 3 | * |
Lee Jones | 2fa5b0f | 2013-01-23 14:33:47 +0000 | [diff] [blame] | 4 | * Power supply driver for ST Ericsson pm2xxx_charger charger |
| 5 | * |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 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 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/power_supply.h> |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 19 | #include <linux/regulator/consumer.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/workqueue.h> |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 23 | #include <linux/mfd/abx500/ab8500.h> |
| 24 | #include <linux/mfd/abx500/ab8500-bm.h> |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 25 | #include <linux/mfd/abx500/ux500_chargalg.h> |
| 26 | #include <linux/pm2301_charger.h> |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 27 | #include <linux/gpio.h> |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 28 | #include <linux/pm_runtime.h> |
Lars-Peter Clausen | 6b17080 | 2013-04-13 13:20:07 +0200 | [diff] [blame] | 29 | #include <linux/pm.h> |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 30 | |
Lee Jones | 2fa5b0f | 2013-01-23 14:33:47 +0000 | [diff] [blame] | 31 | #include "pm2301_charger.h" |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 32 | |
| 33 | #define to_pm2xxx_charger_ac_device_info(x) container_of((x), \ |
| 34 | struct pm2xxx_charger, ac_chg) |
Mustapha Ben Zoubeir | 49fddee | 2012-06-27 15:40:58 +0200 | [diff] [blame] | 35 | #define SLEEP_MIN 50 |
| 36 | #define SLEEP_MAX 100 |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 37 | #define PM2XXX_AUTOSUSPEND_DELAY 500 |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 38 | |
| 39 | static int pm2xxx_interrupt_registers[] = { |
| 40 | PM2XXX_REG_INT1, |
| 41 | PM2XXX_REG_INT2, |
| 42 | PM2XXX_REG_INT3, |
| 43 | PM2XXX_REG_INT4, |
| 44 | PM2XXX_REG_INT5, |
| 45 | PM2XXX_REG_INT6, |
| 46 | }; |
| 47 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 48 | static enum power_supply_property pm2xxx_charger_ac_props[] = { |
| 49 | POWER_SUPPLY_PROP_HEALTH, |
| 50 | POWER_SUPPLY_PROP_PRESENT, |
| 51 | POWER_SUPPLY_PROP_ONLINE, |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 52 | POWER_SUPPLY_PROP_VOLTAGE_AVG, |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static int pm2xxx_charger_voltage_map[] = { |
| 56 | 3500, |
| 57 | 3525, |
| 58 | 3550, |
| 59 | 3575, |
| 60 | 3600, |
| 61 | 3625, |
| 62 | 3650, |
| 63 | 3675, |
| 64 | 3700, |
| 65 | 3725, |
| 66 | 3750, |
| 67 | 3775, |
| 68 | 3800, |
| 69 | 3825, |
| 70 | 3850, |
| 71 | 3875, |
| 72 | 3900, |
| 73 | 3925, |
| 74 | 3950, |
| 75 | 3975, |
| 76 | 4000, |
| 77 | 4025, |
| 78 | 4050, |
| 79 | 4075, |
| 80 | 4100, |
| 81 | 4125, |
| 82 | 4150, |
| 83 | 4175, |
| 84 | 4200, |
| 85 | 4225, |
| 86 | 4250, |
| 87 | 4275, |
| 88 | 4300, |
| 89 | }; |
| 90 | |
| 91 | static int pm2xxx_charger_current_map[] = { |
| 92 | 200, |
| 93 | 200, |
| 94 | 400, |
| 95 | 600, |
| 96 | 800, |
| 97 | 1000, |
| 98 | 1200, |
| 99 | 1400, |
| 100 | 1600, |
| 101 | 1800, |
| 102 | 2000, |
| 103 | 2200, |
| 104 | 2400, |
| 105 | 2600, |
| 106 | 2800, |
| 107 | 3000, |
| 108 | }; |
| 109 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 110 | static const struct i2c_device_id pm2xxx_ident[] = { |
| 111 | { "pm2301", 0 }, |
| 112 | { } |
| 113 | }; |
| 114 | |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 115 | static void set_lpn_pin(struct pm2xxx_charger *pm2) |
| 116 | { |
lme00437 | a21e22f | 2012-11-13 14:43:48 +0100 | [diff] [blame] | 117 | if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin)) { |
| 118 | gpio_set_value(pm2->lpn_pin, 1); |
| 119 | usleep_range(SLEEP_MIN, SLEEP_MAX); |
| 120 | } |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void clear_lpn_pin(struct pm2xxx_charger *pm2) |
| 124 | { |
lme00437 | a21e22f | 2012-11-13 14:43:48 +0100 | [diff] [blame] | 125 | if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin)) |
| 126 | gpio_set_value(pm2->lpn_pin, 0); |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 129 | static int pm2xxx_reg_read(struct pm2xxx_charger *pm2, int reg, u8 *val) |
| 130 | { |
| 131 | int ret; |
Rupesh Kumar | b64f51c | 2012-12-05 14:14:13 +0530 | [diff] [blame] | 132 | |
| 133 | /* wake up the device */ |
| 134 | pm_runtime_get_sync(pm2->dev); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 135 | |
| 136 | ret = i2c_smbus_read_i2c_block_data(pm2->config.pm2xxx_i2c, reg, |
| 137 | 1, val); |
| 138 | if (ret < 0) |
| 139 | dev_err(pm2->dev, "Error reading register at 0x%x\n", reg); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 140 | else |
| 141 | ret = 0; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 142 | |
Rupesh Kumar | 9b7f50e | 2012-12-05 16:13:31 +0530 | [diff] [blame] | 143 | pm_runtime_put_sync(pm2->dev); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int pm2xxx_reg_write(struct pm2xxx_charger *pm2, int reg, u8 val) |
| 149 | { |
| 150 | int ret; |
Rupesh Kumar | b64f51c | 2012-12-05 14:14:13 +0530 | [diff] [blame] | 151 | |
| 152 | /* wake up the device */ |
| 153 | pm_runtime_get_sync(pm2->dev); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 154 | |
| 155 | ret = i2c_smbus_write_i2c_block_data(pm2->config.pm2xxx_i2c, reg, |
| 156 | 1, &val); |
| 157 | if (ret < 0) |
| 158 | dev_err(pm2->dev, "Error writing register at 0x%x\n", reg); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 159 | else |
| 160 | ret = 0; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 161 | |
Rupesh Kumar | 9b7f50e | 2012-12-05 16:13:31 +0530 | [diff] [blame] | 162 | pm_runtime_put_sync(pm2->dev); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger *pm2) |
| 168 | { |
| 169 | int ret; |
| 170 | |
| 171 | /* Enable charging */ |
| 172 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2, |
| 173 | (PM2XXX_CH_AUTO_RESUME_EN | PM2XXX_CHARGER_ENA)); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger *pm2) |
| 179 | { |
| 180 | int ret; |
| 181 | |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 182 | /* Disable SW EOC ctrl */ |
| 183 | ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, PM2XXX_SWCTRL_HW); |
| 184 | if (ret < 0) { |
| 185 | dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__); |
| 186 | return ret; |
| 187 | } |
| 188 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 189 | /* Disable charging */ |
| 190 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2, |
| 191 | (PM2XXX_CH_AUTO_RESUME_DIS | PM2XXX_CHARGER_DIS)); |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 192 | if (ret < 0) { |
| 193 | dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__); |
| 194 | return ret; |
| 195 | } |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 196 | |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 197 | return 0; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger *pm2, int val) |
| 201 | { |
| 202 | queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | |
Sachin Kamat | 83bd57a | 2013-09-06 13:31:12 +0530 | [diff] [blame] | 208 | static int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger *pm2, int val) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 209 | { |
| 210 | queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger *pm2, int val) |
| 216 | { |
Rupesh Kumar | 789ca7b | 2012-06-19 10:21:24 +0530 | [diff] [blame] | 217 | dev_err(pm2->dev, "Overvoltage detected\n"); |
| 218 | pm2->flags.ovv = true; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 219 | power_supply_changed(pm2->ac_chg.psy); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 220 | |
Rupesh Kumar | 789ca7b | 2012-06-19 10:21:24 +0530 | [diff] [blame] | 221 | /* Schedule a new HW failure check */ |
| 222 | queue_delayed_work(pm2->charger_wq, &pm2->check_hw_failure_work, 0); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 223 | |
Rupesh Kumar | 789ca7b | 2012-06-19 10:21:24 +0530 | [diff] [blame] | 224 | return 0; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger *pm2, int val) |
| 228 | { |
Masanari Iida | 07f4225 | 2013-03-20 11:00:34 +0900 | [diff] [blame] | 229 | dev_dbg(pm2->dev , "20 minutes watchdog expired\n"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 230 | |
| 231 | pm2->ac.wd_expired = true; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 232 | power_supply_changed(pm2->ac_chg.psy); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger *pm2, int val) |
| 238 | { |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 239 | int ret; |
| 240 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 241 | switch (val) { |
| 242 | case PM2XXX_INT1_ITVBATLOWR: |
| 243 | dev_dbg(pm2->dev, "VBAT grows above VBAT_LOW level\n"); |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 244 | /* Enable SW EOC ctrl */ |
| 245 | ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, |
| 246 | PM2XXX_SWCTRL_SW); |
| 247 | if (ret < 0) { |
| 248 | dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__); |
| 249 | return ret; |
| 250 | } |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 251 | break; |
| 252 | |
| 253 | case PM2XXX_INT1_ITVBATLOWF: |
| 254 | dev_dbg(pm2->dev, "VBAT drops below VBAT_LOW level\n"); |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 255 | /* Disable SW EOC ctrl */ |
| 256 | ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, |
| 257 | PM2XXX_SWCTRL_HW); |
| 258 | if (ret < 0) { |
| 259 | dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__); |
| 260 | return ret; |
| 261 | } |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 262 | break; |
| 263 | |
| 264 | default: |
| 265 | dev_err(pm2->dev, "Unknown VBAT level\n"); |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger *pm2, int val) |
| 272 | { |
| 273 | dev_dbg(pm2->dev, "battery disconnected\n"); |
| 274 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 275 | return 0; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static int pm2xxx_charger_detection(struct pm2xxx_charger *pm2, u8 *val) |
| 279 | { |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 280 | int ret; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 281 | |
| 282 | ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT2, val); |
| 283 | |
| 284 | if (ret < 0) { |
| 285 | dev_err(pm2->dev, "Charger detection failed\n"); |
| 286 | goto out; |
| 287 | } |
| 288 | |
| 289 | *val &= (PM2XXX_INT2_S_ITVPWR1PLUG | PM2XXX_INT2_S_ITVPWR2PLUG); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 290 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 291 | out: |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger *pm2, int val) |
| 296 | { |
| 297 | |
| 298 | int ret; |
| 299 | u8 read_val; |
| 300 | |
| 301 | /* |
| 302 | * Since we can't be sure that the events are received |
| 303 | * synchronously, we have the check if the main charger is |
| 304 | * connected by reading the interrupt source register. |
| 305 | */ |
| 306 | ret = pm2xxx_charger_detection(pm2, &read_val); |
| 307 | |
| 308 | if ((ret == 0) && read_val) { |
| 309 | pm2->ac.charger_connected = 1; |
| 310 | pm2->ac_conn = true; |
| 311 | queue_work(pm2->charger_wq, &pm2->ac_work); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger *pm2, |
| 319 | int val) |
| 320 | { |
| 321 | pm2->ac.charger_connected = 0; |
| 322 | queue_work(pm2->charger_wq, &pm2->ac_work); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 327 | static int pm2_int_reg0(void *pm2_data, int val) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 328 | { |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 329 | struct pm2xxx_charger *pm2 = pm2_data; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 330 | int ret = 0; |
| 331 | |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 332 | if (val & PM2XXX_INT1_ITVBATLOWR) { |
| 333 | ret = pm2xxx_charger_vbat_lsig_mngt(pm2, |
| 334 | PM2XXX_INT1_ITVBATLOWR); |
| 335 | if (ret < 0) |
| 336 | goto out; |
| 337 | } |
| 338 | |
| 339 | if (val & PM2XXX_INT1_ITVBATLOWF) { |
| 340 | ret = pm2xxx_charger_vbat_lsig_mngt(pm2, |
| 341 | PM2XXX_INT1_ITVBATLOWF); |
| 342 | if (ret < 0) |
| 343 | goto out; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 346 | if (val & PM2XXX_INT1_ITVBATDISCONNECT) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 347 | ret = pm2xxx_charger_bat_disc_mngt(pm2, |
| 348 | PM2XXX_INT1_ITVBATDISCONNECT); |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 349 | if (ret < 0) |
| 350 | goto out; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 351 | } |
Rajkumar Kasirajan | 584f970 | 2012-06-05 12:31:25 +0530 | [diff] [blame] | 352 | out: |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 353 | return ret; |
| 354 | } |
| 355 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 356 | static int pm2_int_reg1(void *pm2_data, int val) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 357 | { |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 358 | struct pm2xxx_charger *pm2 = pm2_data; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 359 | int ret = 0; |
| 360 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 361 | if (val & (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 362 | dev_dbg(pm2->dev , "Main charger plugged\n"); |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 363 | ret = pm2xxx_charger_itv_pwr_plug_mngt(pm2, val & |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 364 | (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG)); |
| 365 | } |
| 366 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 367 | if (val & |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 368 | (PM2XXX_INT2_ITVPWR1UNPLUG | PM2XXX_INT2_ITVPWR2UNPLUG)) { |
| 369 | dev_dbg(pm2->dev , "Main charger unplugged\n"); |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 370 | ret = pm2xxx_charger_itv_pwr_unplug_mngt(pm2, val & |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 371 | (PM2XXX_INT2_ITVPWR1UNPLUG | |
| 372 | PM2XXX_INT2_ITVPWR2UNPLUG)); |
| 373 | } |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 378 | static int pm2_int_reg2(void *pm2_data, int val) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 379 | { |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 380 | struct pm2xxx_charger *pm2 = pm2_data; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 381 | int ret = 0; |
| 382 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 383 | if (val & PM2XXX_INT3_ITAUTOTIMEOUTWD) |
| 384 | ret = pm2xxx_charger_wd_exp_mngt(pm2, val); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 385 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 386 | if (val & (PM2XXX_INT3_ITCHPRECHARGEWD | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 387 | PM2XXX_INT3_ITCHCCWD | PM2XXX_INT3_ITCHCVWD)) { |
| 388 | dev_dbg(pm2->dev, |
Masanari Iida | 0b1587b | 2013-07-17 04:37:44 +0900 | [diff] [blame] | 389 | "Watchdog occurred for precharge, CC and CV charge\n"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return ret; |
| 393 | } |
| 394 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 395 | static int pm2_int_reg3(void *pm2_data, int val) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 396 | { |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 397 | struct pm2xxx_charger *pm2 = pm2_data; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 398 | int ret = 0; |
| 399 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 400 | if (val & (PM2XXX_INT4_ITCHARGINGON)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 401 | dev_dbg(pm2->dev , |
| 402 | "chargind operation has started\n"); |
| 403 | } |
| 404 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 405 | if (val & (PM2XXX_INT4_ITVRESUME)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 406 | dev_dbg(pm2->dev, |
| 407 | "battery discharged down to VResume threshold\n"); |
| 408 | } |
| 409 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 410 | if (val & (PM2XXX_INT4_ITBATTFULL)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 411 | dev_dbg(pm2->dev , "battery fully detected\n"); |
| 412 | } |
| 413 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 414 | if (val & (PM2XXX_INT4_ITCVPHASE)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 415 | dev_dbg(pm2->dev, "CV phase enter with 0.5C charging\n"); |
| 416 | } |
| 417 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 418 | if (val & (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 419 | pm2->failure_case = VPWR_OVV; |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 420 | ret = pm2xxx_charger_ovv_mngt(pm2, val & |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 421 | (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV)); |
| 422 | dev_dbg(pm2->dev, "VPWR/VSYSTEM overvoltage detected\n"); |
| 423 | } |
| 424 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 425 | if (val & (PM2XXX_INT4_S_ITBATTEMPCOLD | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 426 | PM2XXX_INT4_S_ITBATTEMPHOT)) { |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 427 | ret = pm2xxx_charger_batt_therm_mngt(pm2, val & |
| 428 | (PM2XXX_INT4_S_ITBATTEMPCOLD | |
| 429 | PM2XXX_INT4_S_ITBATTEMPHOT)); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 430 | dev_dbg(pm2->dev, "BTEMP is too Low/High\n"); |
| 431 | } |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 436 | static int pm2_int_reg4(void *pm2_data, int val) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 437 | { |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 438 | struct pm2xxx_charger *pm2 = pm2_data; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 439 | int ret = 0; |
| 440 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 441 | if (val & PM2XXX_INT5_ITVSYSTEMOVV) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 442 | pm2->failure_case = VSYSTEM_OVV; |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 443 | ret = pm2xxx_charger_ovv_mngt(pm2, val & |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 444 | PM2XXX_INT5_ITVSYSTEMOVV); |
| 445 | dev_dbg(pm2->dev, "VSYSTEM overvoltage detected\n"); |
| 446 | } |
| 447 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 448 | if (val & (PM2XXX_INT5_ITTHERMALWARNINGFALL | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 449 | PM2XXX_INT5_ITTHERMALWARNINGRISE | |
| 450 | PM2XXX_INT5_ITTHERMALSHUTDOWNFALL | |
| 451 | PM2XXX_INT5_ITTHERMALSHUTDOWNRISE)) { |
| 452 | dev_dbg(pm2->dev, "BTEMP die temperature is too Low/High\n"); |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 453 | ret = pm2xxx_charger_die_therm_mngt(pm2, val & |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 454 | (PM2XXX_INT5_ITTHERMALWARNINGFALL | |
| 455 | PM2XXX_INT5_ITTHERMALWARNINGRISE | |
| 456 | PM2XXX_INT5_ITTHERMALSHUTDOWNFALL | |
| 457 | PM2XXX_INT5_ITTHERMALSHUTDOWNRISE)); |
| 458 | } |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 463 | static int pm2_int_reg5(void *pm2_data, int val) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 464 | { |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 465 | struct pm2xxx_charger *pm2 = pm2_data; |
| 466 | int ret = 0; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 467 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 468 | if (val & (PM2XXX_INT6_ITVPWR2DROP | PM2XXX_INT6_ITVPWR1DROP)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 469 | dev_dbg(pm2->dev, "VMPWR drop to VBAT level\n"); |
| 470 | } |
| 471 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 472 | if (val & (PM2XXX_INT6_ITVPWR2VALIDRISE | |
| 473 | PM2XXX_INT6_ITVPWR1VALIDRISE | |
| 474 | PM2XXX_INT6_ITVPWR2VALIDFALL | |
| 475 | PM2XXX_INT6_ITVPWR1VALIDFALL)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 476 | dev_dbg(pm2->dev, "Falling/Rising edge on WPWR1/2\n"); |
| 477 | } |
| 478 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 479 | return ret; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static irqreturn_t pm2xxx_irq_int(int irq, void *data) |
| 483 | { |
| 484 | struct pm2xxx_charger *pm2 = data; |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 485 | struct pm2xxx_interrupts *interrupt = pm2->pm2_int; |
| 486 | int i; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 487 | |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 488 | /* wake up the device */ |
| 489 | pm_runtime_get_sync(pm2->dev); |
| 490 | |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 491 | do { |
| 492 | for (i = 0; i < PM2XXX_NUM_INT_REG; i++) { |
| 493 | pm2xxx_reg_read(pm2, |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 494 | pm2xxx_interrupt_registers[i], |
| 495 | &(interrupt->reg[i])); |
| 496 | |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 497 | if (interrupt->reg[i] > 0) |
| 498 | interrupt->handler[i](pm2, interrupt->reg[i]); |
| 499 | } |
| 500 | } while (gpio_get_value(pm2->pdata->gpio_irq_number) == 0); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 501 | |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 502 | pm_runtime_mark_last_busy(pm2->dev); |
| 503 | pm_runtime_put_autosuspend(pm2->dev); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 504 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 505 | return IRQ_HANDLED; |
| 506 | } |
| 507 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 508 | static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger *pm2) |
| 509 | { |
| 510 | int ret = 0; |
| 511 | u8 val; |
| 512 | |
| 513 | if (pm2->ac.charger_connected && pm2->ac.charger_online) { |
| 514 | |
| 515 | ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &val); |
| 516 | if (ret < 0) { |
| 517 | dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__); |
| 518 | goto out; |
| 519 | } |
| 520 | |
| 521 | if (val & PM2XXX_INT4_S_ITCVPHASE) |
| 522 | ret = PM2XXX_CONST_VOLT; |
| 523 | else |
| 524 | ret = PM2XXX_CONST_CURR; |
| 525 | } |
| 526 | out: |
| 527 | return ret; |
| 528 | } |
| 529 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 530 | static int pm2xxx_current_to_regval(int curr) |
| 531 | { |
| 532 | int i; |
| 533 | |
| 534 | if (curr < pm2xxx_charger_current_map[0]) |
| 535 | return 0; |
| 536 | |
| 537 | for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_current_map); i++) { |
| 538 | if (curr < pm2xxx_charger_current_map[i]) |
| 539 | return (i - 1); |
| 540 | } |
| 541 | |
| 542 | i = ARRAY_SIZE(pm2xxx_charger_current_map) - 1; |
| 543 | if (curr == pm2xxx_charger_current_map[i]) |
| 544 | return i; |
| 545 | else |
| 546 | return -EINVAL; |
| 547 | } |
| 548 | |
| 549 | static int pm2xxx_voltage_to_regval(int curr) |
| 550 | { |
| 551 | int i; |
| 552 | |
| 553 | if (curr < pm2xxx_charger_voltage_map[0]) |
| 554 | return 0; |
| 555 | |
| 556 | for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_voltage_map); i++) { |
| 557 | if (curr < pm2xxx_charger_voltage_map[i]) |
| 558 | return i - 1; |
| 559 | } |
| 560 | |
| 561 | i = ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1; |
| 562 | if (curr == pm2xxx_charger_voltage_map[i]) |
| 563 | return i; |
| 564 | else |
| 565 | return -EINVAL; |
| 566 | } |
| 567 | |
| 568 | static int pm2xxx_charger_update_charger_current(struct ux500_charger *charger, |
| 569 | int ich_out) |
| 570 | { |
| 571 | int ret; |
| 572 | int curr_index; |
| 573 | struct pm2xxx_charger *pm2; |
| 574 | u8 val; |
| 575 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 576 | if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 577 | pm2 = to_pm2xxx_charger_ac_device_info(charger); |
| 578 | else |
| 579 | return -ENXIO; |
| 580 | |
| 581 | curr_index = pm2xxx_current_to_regval(ich_out); |
| 582 | if (curr_index < 0) { |
| 583 | dev_err(pm2->dev, |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 584 | "Charger current too high, charging not started\n"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 585 | return -ENXIO; |
| 586 | } |
| 587 | |
| 588 | ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val); |
| 589 | if (ret >= 0) { |
| 590 | val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK; |
| 591 | val |= curr_index; |
| 592 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val); |
| 593 | if (ret < 0) { |
| 594 | dev_err(pm2->dev, |
| 595 | "%s write failed\n", __func__); |
| 596 | } |
| 597 | } |
| 598 | else |
| 599 | dev_err(pm2->dev, "%s read failed\n", __func__); |
| 600 | |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static int pm2xxx_charger_ac_get_property(struct power_supply *psy, |
| 605 | enum power_supply_property psp, |
| 606 | union power_supply_propval *val) |
| 607 | { |
| 608 | struct pm2xxx_charger *pm2; |
| 609 | |
| 610 | pm2 = to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy)); |
| 611 | |
| 612 | switch (psp) { |
| 613 | case POWER_SUPPLY_PROP_HEALTH: |
| 614 | if (pm2->flags.mainextchnotok) |
| 615 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 616 | else if (pm2->ac.wd_expired) |
| 617 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 618 | else if (pm2->flags.main_thermal_prot) |
| 619 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
Rupesh Kumar | 789ca7b | 2012-06-19 10:21:24 +0530 | [diff] [blame] | 620 | else if (pm2->flags.ovv) |
| 621 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 622 | else |
| 623 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 624 | break; |
| 625 | case POWER_SUPPLY_PROP_ONLINE: |
| 626 | val->intval = pm2->ac.charger_online; |
| 627 | break; |
| 628 | case POWER_SUPPLY_PROP_PRESENT: |
| 629 | val->intval = pm2->ac.charger_connected; |
| 630 | break; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 631 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| 632 | pm2->ac.cv_active = pm2xxx_charger_get_ac_cv(pm2); |
| 633 | val->intval = pm2->ac.cv_active; |
| 634 | break; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 635 | default: |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static int pm2xxx_charging_init(struct pm2xxx_charger *pm2) |
| 642 | { |
| 643 | int ret = 0; |
| 644 | |
| 645 | /* enable CC and CV watchdog */ |
| 646 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG3, |
| 647 | (PM2XXX_CH_WD_CV_PHASE_60MIN | PM2XXX_CH_WD_CC_PHASE_60MIN)); |
| 648 | if( ret < 0) |
| 649 | return ret; |
| 650 | |
| 651 | /* enable precharge watchdog */ |
| 652 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG4, |
| 653 | PM2XXX_CH_WD_PRECH_PHASE_60MIN); |
| 654 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 655 | /* Disable auto timeout */ |
| 656 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG5, |
| 657 | PM2XXX_CH_WD_AUTO_TIMEOUT_20MIN); |
| 658 | |
| 659 | /* |
| 660 | * EOC current level = 100mA |
| 661 | * Precharge current level = 100mA |
| 662 | * CC current level = 1000mA |
| 663 | */ |
| 664 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, |
| 665 | (PM2XXX_DIR_CH_CC_CURRENT_1000MA | |
| 666 | PM2XXX_CH_PRECH_CURRENT_100MA | |
| 667 | PM2XXX_CH_EOC_CURRENT_100MA)); |
| 668 | |
| 669 | /* |
| 670 | * recharge threshold = 3.8V |
| 671 | * Precharge to CC threshold = 2.9V |
| 672 | */ |
| 673 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG7, |
| 674 | (PM2XXX_CH_PRECH_VOL_2_9 | PM2XXX_CH_VRESUME_VOL_3_8)); |
| 675 | |
| 676 | /* float voltage charger level = 4.2V */ |
| 677 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, |
| 678 | PM2XXX_CH_VOLT_4_2); |
| 679 | |
| 680 | /* Voltage drop between VBAT and VSYS in HW charging = 300mV */ |
| 681 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG9, |
| 682 | (PM2XXX_CH_150MV_DROP_300MV | PM2XXX_CHARCHING_INFO_DIS | |
| 683 | PM2XXX_CH_CC_REDUCED_CURRENT_IDENT | |
| 684 | PM2XXX_CH_CC_MODEDROP_DIS)); |
| 685 | |
| 686 | /* Input charger level of over voltage = 10V */ |
| 687 | ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR2, |
| 688 | PM2XXX_VPWR2_OVV_10); |
| 689 | ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR1, |
| 690 | PM2XXX_VPWR1_OVV_10); |
| 691 | |
| 692 | /* Input charger drop */ |
| 693 | ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR2, |
| 694 | (PM2XXX_VPWR2_HW_OPT_DIS | PM2XXX_VPWR2_VALID_DIS | |
| 695 | PM2XXX_VPWR2_DROP_DIS)); |
| 696 | ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR1, |
| 697 | (PM2XXX_VPWR1_HW_OPT_DIS | PM2XXX_VPWR1_VALID_DIS | |
| 698 | PM2XXX_VPWR1_DROP_DIS)); |
| 699 | |
| 700 | /* Disable battery low monitoring */ |
| 701 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_LOW_LEV_COMP_REG, |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 702 | PM2XXX_VBAT_LOW_MONITORING_ENA); |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 703 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | static int pm2xxx_charger_ac_en(struct ux500_charger *charger, |
| 708 | int enable, int vset, int iset) |
| 709 | { |
| 710 | int ret; |
| 711 | int volt_index; |
| 712 | int curr_index; |
| 713 | u8 val; |
| 714 | |
| 715 | struct pm2xxx_charger *pm2 = to_pm2xxx_charger_ac_device_info(charger); |
| 716 | |
| 717 | if (enable) { |
| 718 | if (!pm2->ac.charger_connected) { |
| 719 | dev_dbg(pm2->dev, "AC charger not connected\n"); |
| 720 | return -ENXIO; |
| 721 | } |
| 722 | |
| 723 | dev_dbg(pm2->dev, "Enable AC: %dmV %dmA\n", vset, iset); |
| 724 | if (!pm2->vddadc_en_ac) { |
Sachin Kamat | 508b2c1 | 2013-09-06 13:31:11 +0530 | [diff] [blame] | 725 | ret = regulator_enable(pm2->regu); |
| 726 | if (ret) |
| 727 | dev_warn(pm2->dev, |
| 728 | "Failed to enable vddadc regulator\n"); |
| 729 | else |
| 730 | pm2->vddadc_en_ac = true; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | ret = pm2xxx_charging_init(pm2); |
| 734 | if (ret < 0) { |
| 735 | dev_err(pm2->dev, "%s charging init failed\n", |
| 736 | __func__); |
| 737 | goto error_occured; |
| 738 | } |
| 739 | |
| 740 | volt_index = pm2xxx_voltage_to_regval(vset); |
| 741 | curr_index = pm2xxx_current_to_regval(iset); |
| 742 | |
| 743 | if (volt_index < 0 || curr_index < 0) { |
| 744 | dev_err(pm2->dev, |
| 745 | "Charger voltage or current too high, " |
| 746 | "charging not started\n"); |
| 747 | return -ENXIO; |
| 748 | } |
| 749 | |
| 750 | ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG8, &val); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 751 | if (ret < 0) { |
| 752 | dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__); |
| 753 | goto error_occured; |
| 754 | } |
| 755 | val &= ~PM2XXX_CH_VOLT_MASK; |
| 756 | val |= volt_index; |
| 757 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, val); |
| 758 | if (ret < 0) { |
| 759 | dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__); |
| 760 | goto error_occured; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 764 | if (ret < 0) { |
| 765 | dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__); |
| 766 | goto error_occured; |
| 767 | } |
| 768 | val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK; |
| 769 | val |= curr_index; |
| 770 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val); |
| 771 | if (ret < 0) { |
| 772 | dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__); |
| 773 | goto error_occured; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (!pm2->bat->enable_overshoot) { |
| 777 | ret = pm2xxx_reg_read(pm2, PM2XXX_LED_CTRL_REG, &val); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 778 | if (ret < 0) { |
| 779 | dev_err(pm2->dev, "%s pm2xxx read failed\n", |
| 780 | __func__); |
| 781 | goto error_occured; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 782 | } |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 783 | val |= PM2XXX_ANTI_OVERSHOOT_EN; |
| 784 | ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG, val); |
| 785 | if (ret < 0) { |
| 786 | dev_err(pm2->dev, "%s pm2xxx write failed\n", |
| 787 | __func__); |
| 788 | goto error_occured; |
| 789 | } |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | ret = pm2xxx_charging_enable_mngt(pm2); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 793 | if (ret < 0) { |
| 794 | dev_err(pm2->dev, "Failed to enable" |
| 795 | "pm2xxx ac charger\n"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 796 | goto error_occured; |
| 797 | } |
| 798 | |
| 799 | pm2->ac.charger_online = 1; |
| 800 | } else { |
| 801 | pm2->ac.charger_online = 0; |
| 802 | pm2->ac.wd_expired = false; |
| 803 | |
| 804 | /* Disable regulator if enabled */ |
| 805 | if (pm2->vddadc_en_ac) { |
| 806 | regulator_disable(pm2->regu); |
| 807 | pm2->vddadc_en_ac = false; |
| 808 | } |
| 809 | |
| 810 | ret = pm2xxx_charging_disable_mngt(pm2); |
Rajkumar Kasirajan | e41f39e | 2012-05-28 15:57:33 +0530 | [diff] [blame] | 811 | if (ret < 0) { |
| 812 | dev_err(pm2->dev, "failed to disable" |
| 813 | "pm2xxx ac charger\n"); |
| 814 | goto error_occured; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | dev_dbg(pm2->dev, "PM2301: " "Disabled AC charging\n"); |
| 818 | } |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 819 | power_supply_changed(pm2->ac_chg.psy); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 820 | |
| 821 | error_occured: |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | static int pm2xxx_charger_watchdog_kick(struct ux500_charger *charger) |
| 826 | { |
| 827 | int ret; |
| 828 | struct pm2xxx_charger *pm2; |
| 829 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 830 | if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 831 | pm2 = to_pm2xxx_charger_ac_device_info(charger); |
| 832 | else |
| 833 | return -ENXIO; |
| 834 | |
| 835 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_WD_KICK, WD_TIMER); |
| 836 | if (ret) |
| 837 | dev_err(pm2->dev, "Failed to kick WD!\n"); |
| 838 | |
| 839 | return ret; |
| 840 | } |
| 841 | |
| 842 | static void pm2xxx_charger_ac_work(struct work_struct *work) |
| 843 | { |
| 844 | struct pm2xxx_charger *pm2 = container_of(work, |
| 845 | struct pm2xxx_charger, ac_work); |
| 846 | |
| 847 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 848 | power_supply_changed(pm2->ac_chg.psy); |
| 849 | sysfs_notify(&pm2->ac_chg.psy->dev.kobj, NULL, "present"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 850 | }; |
| 851 | |
Rupesh Kumar | 789ca7b | 2012-06-19 10:21:24 +0530 | [diff] [blame] | 852 | static void pm2xxx_charger_check_hw_failure_work(struct work_struct *work) |
| 853 | { |
| 854 | u8 reg_value; |
| 855 | |
| 856 | struct pm2xxx_charger *pm2 = container_of(work, |
| 857 | struct pm2xxx_charger, check_hw_failure_work.work); |
| 858 | |
| 859 | if (pm2->flags.ovv) { |
| 860 | pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, ®_value); |
| 861 | |
| 862 | if (!(reg_value & (PM2XXX_INT4_S_ITVPWR1OVV | |
| 863 | PM2XXX_INT4_S_ITVPWR2OVV))) { |
| 864 | pm2->flags.ovv = false; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 865 | power_supply_changed(pm2->ac_chg.psy); |
Rupesh Kumar | 789ca7b | 2012-06-19 10:21:24 +0530 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
| 869 | /* If we still have a failure, schedule a new check */ |
| 870 | if (pm2->flags.ovv) { |
| 871 | queue_delayed_work(pm2->charger_wq, |
| 872 | &pm2->check_hw_failure_work, round_jiffies(HZ)); |
| 873 | } |
| 874 | } |
| 875 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 876 | static void pm2xxx_charger_check_main_thermal_prot_work( |
| 877 | struct work_struct *work) |
| 878 | { |
Rupesh Kumar | da9e83d | 2012-07-16 12:45:19 +0530 | [diff] [blame] | 879 | int ret; |
| 880 | u8 val; |
| 881 | |
| 882 | struct pm2xxx_charger *pm2 = container_of(work, struct pm2xxx_charger, |
| 883 | check_main_thermal_prot_work); |
| 884 | |
| 885 | /* Check if die temp warning is still active */ |
| 886 | ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT5, &val); |
| 887 | if (ret < 0) { |
| 888 | dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__); |
| 889 | return; |
| 890 | } |
| 891 | if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGRISE |
| 892 | | PM2XXX_INT5_S_ITTHERMALSHUTDOWNRISE)) |
| 893 | pm2->flags.main_thermal_prot = true; |
| 894 | else if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGFALL |
| 895 | | PM2XXX_INT5_S_ITTHERMALSHUTDOWNFALL)) |
| 896 | pm2->flags.main_thermal_prot = false; |
| 897 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 898 | power_supply_changed(pm2->ac_chg.psy); |
Rupesh Kumar | da9e83d | 2012-07-16 12:45:19 +0530 | [diff] [blame] | 899 | } |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 900 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 901 | static struct pm2xxx_interrupts pm2xxx_int = { |
| 902 | .handler[0] = pm2_int_reg0, |
| 903 | .handler[1] = pm2_int_reg1, |
| 904 | .handler[2] = pm2_int_reg2, |
| 905 | .handler[3] = pm2_int_reg3, |
| 906 | .handler[4] = pm2_int_reg4, |
| 907 | .handler[5] = pm2_int_reg5, |
| 908 | }; |
| 909 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 910 | static struct pm2xxx_irq pm2xxx_charger_irq[] = { |
| 911 | {"PM2XXX_IRQ_INT", pm2xxx_irq_int}, |
| 912 | }; |
| 913 | |
Arnd Bergmann | 0df6e32 | 2016-03-02 16:58:56 +0100 | [diff] [blame] | 914 | static int __maybe_unused pm2xxx_wall_charger_resume(struct device *dev) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 915 | { |
Lars-Peter Clausen | 6b17080 | 2013-04-13 13:20:07 +0200 | [diff] [blame] | 916 | struct i2c_client *i2c_client = to_i2c_client(dev); |
Mustapha Ben Zoubeir | 49fddee | 2012-06-27 15:40:58 +0200 | [diff] [blame] | 917 | struct pm2xxx_charger *pm2; |
| 918 | |
| 919 | pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client); |
| 920 | set_lpn_pin(pm2); |
| 921 | |
| 922 | /* If we still have a HW failure, schedule a new check */ |
| 923 | if (pm2->flags.ovv) |
| 924 | queue_delayed_work(pm2->charger_wq, |
| 925 | &pm2->check_hw_failure_work, 0); |
| 926 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 927 | return 0; |
| 928 | } |
| 929 | |
Arnd Bergmann | 0df6e32 | 2016-03-02 16:58:56 +0100 | [diff] [blame] | 930 | static int __maybe_unused pm2xxx_wall_charger_suspend(struct device *dev) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 931 | { |
Lars-Peter Clausen | 6b17080 | 2013-04-13 13:20:07 +0200 | [diff] [blame] | 932 | struct i2c_client *i2c_client = to_i2c_client(dev); |
Mustapha Ben Zoubeir | 49fddee | 2012-06-27 15:40:58 +0200 | [diff] [blame] | 933 | struct pm2xxx_charger *pm2; |
| 934 | |
| 935 | pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client); |
| 936 | clear_lpn_pin(pm2); |
| 937 | |
| 938 | /* Cancel any pending HW failure check */ |
| 939 | if (delayed_work_pending(&pm2->check_hw_failure_work)) |
| 940 | cancel_delayed_work(&pm2->check_hw_failure_work); |
| 941 | |
| 942 | flush_work(&pm2->ac_work); |
| 943 | flush_work(&pm2->check_main_thermal_prot_work); |
| 944 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | |
Arnd Bergmann | 0df6e32 | 2016-03-02 16:58:56 +0100 | [diff] [blame] | 948 | static int __maybe_unused pm2xxx_runtime_suspend(struct device *dev) |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 949 | { |
| 950 | struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev); |
| 951 | struct pm2xxx_charger *pm2; |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 952 | |
| 953 | pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client); |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 954 | clear_lpn_pin(pm2); |
| 955 | |
Dan Carpenter | c802423 | 2013-11-07 11:06:17 +0300 | [diff] [blame] | 956 | return 0; |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Arnd Bergmann | 0df6e32 | 2016-03-02 16:58:56 +0100 | [diff] [blame] | 959 | static int __maybe_unused pm2xxx_runtime_resume(struct device *dev) |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 960 | { |
| 961 | struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev); |
| 962 | struct pm2xxx_charger *pm2; |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 963 | |
| 964 | pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client); |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 965 | |
| 966 | if (gpio_is_valid(pm2->lpn_pin) && gpio_get_value(pm2->lpn_pin) == 0) |
| 967 | set_lpn_pin(pm2); |
| 968 | |
Dan Carpenter | c802423 | 2013-11-07 11:06:17 +0300 | [diff] [blame] | 969 | return 0; |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Arnd Bergmann | 0df6e32 | 2016-03-02 16:58:56 +0100 | [diff] [blame] | 972 | static const struct dev_pm_ops pm2xxx_pm_ops __maybe_unused = { |
Lars-Peter Clausen | 6b17080 | 2013-04-13 13:20:07 +0200 | [diff] [blame] | 973 | SET_SYSTEM_SLEEP_PM_OPS(pm2xxx_wall_charger_suspend, |
| 974 | pm2xxx_wall_charger_resume) |
| 975 | SET_RUNTIME_PM_OPS(pm2xxx_runtime_suspend, pm2xxx_runtime_resume, NULL) |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 976 | }; |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 977 | |
Lee Jones | 116c326 | 2013-02-25 14:26:52 +0000 | [diff] [blame] | 978 | static int pm2xxx_wall_charger_probe(struct i2c_client *i2c_client, |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 979 | const struct i2c_device_id *id) |
| 980 | { |
| 981 | struct pm2xxx_platform_data *pl_data = i2c_client->dev.platform_data; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 982 | struct power_supply_config psy_cfg = {}; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 983 | struct pm2xxx_charger *pm2; |
| 984 | int ret = 0; |
| 985 | u8 val; |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 986 | int i; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 987 | |
Axel Lin | df311333 | 2013-05-16 17:31:00 +0800 | [diff] [blame] | 988 | if (!pl_data) { |
| 989 | dev_err(&i2c_client->dev, "No platform data supplied\n"); |
| 990 | return -EINVAL; |
| 991 | } |
| 992 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 993 | pm2 = kzalloc(sizeof(struct pm2xxx_charger), GFP_KERNEL); |
| 994 | if (!pm2) { |
Axel Lin | df311333 | 2013-05-16 17:31:00 +0800 | [diff] [blame] | 995 | dev_err(&i2c_client->dev, "pm2xxx_charger allocation failed\n"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 996 | return -ENOMEM; |
| 997 | } |
| 998 | |
| 999 | /* get parent data */ |
| 1000 | pm2->dev = &i2c_client->dev; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1001 | |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 1002 | pm2->pm2_int = &pm2xxx_int; |
| 1003 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1004 | /* get charger spcific platform data */ |
| 1005 | if (!pl_data->wall_charger) { |
| 1006 | dev_err(pm2->dev, "no charger platform data supplied\n"); |
| 1007 | ret = -EINVAL; |
| 1008 | goto free_device_info; |
| 1009 | } |
| 1010 | |
| 1011 | pm2->pdata = pl_data->wall_charger; |
| 1012 | |
| 1013 | /* get battery specific platform data */ |
| 1014 | if (!pl_data->battery) { |
| 1015 | dev_err(pm2->dev, "no battery platform data supplied\n"); |
| 1016 | ret = -EINVAL; |
| 1017 | goto free_device_info; |
| 1018 | } |
| 1019 | |
| 1020 | pm2->bat = pl_data->battery; |
| 1021 | |
| 1022 | if (!i2c_check_functionality(i2c_client->adapter, |
| 1023 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 1024 | I2C_FUNC_SMBUS_READ_WORD_DATA)) { |
| 1025 | ret = -ENODEV; |
| 1026 | dev_info(pm2->dev, "pm2301 i2c_check_functionality failed\n"); |
| 1027 | goto free_device_info; |
| 1028 | } |
| 1029 | |
| 1030 | pm2->config.pm2xxx_i2c = i2c_client; |
| 1031 | pm2->config.pm2xxx_id = (struct i2c_device_id *) id; |
| 1032 | i2c_set_clientdata(i2c_client, pm2); |
| 1033 | |
| 1034 | /* AC supply */ |
| 1035 | /* power_supply base class */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1036 | pm2->ac_chg_desc.name = pm2->pdata->label; |
| 1037 | pm2->ac_chg_desc.type = POWER_SUPPLY_TYPE_MAINS; |
| 1038 | pm2->ac_chg_desc.properties = pm2xxx_charger_ac_props; |
| 1039 | pm2->ac_chg_desc.num_properties = ARRAY_SIZE(pm2xxx_charger_ac_props); |
| 1040 | pm2->ac_chg_desc.get_property = pm2xxx_charger_ac_get_property; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 1041 | |
| 1042 | psy_cfg.supplied_to = pm2->pdata->supplied_to; |
| 1043 | psy_cfg.num_supplicants = pm2->pdata->num_supplicants; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1044 | /* pm2xxx_charger sub-class */ |
| 1045 | pm2->ac_chg.ops.enable = &pm2xxx_charger_ac_en; |
| 1046 | pm2->ac_chg.ops.kick_wd = &pm2xxx_charger_watchdog_kick; |
| 1047 | pm2->ac_chg.ops.update_curr = &pm2xxx_charger_update_charger_current; |
| 1048 | pm2->ac_chg.max_out_volt = pm2xxx_charger_voltage_map[ |
| 1049 | ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1]; |
| 1050 | pm2->ac_chg.max_out_curr = pm2xxx_charger_current_map[ |
| 1051 | ARRAY_SIZE(pm2xxx_charger_current_map) - 1]; |
Loic Pallardy | e07a564 | 2012-05-10 15:33:56 +0200 | [diff] [blame] | 1052 | pm2->ac_chg.wdt_refresh = WD_KICK_INTERVAL; |
Olivier Clergeaud | 006f82d | 2012-05-14 16:50:29 +0200 | [diff] [blame] | 1053 | pm2->ac_chg.enabled = true; |
Loic Pallardy | e07a564 | 2012-05-10 15:33:56 +0200 | [diff] [blame] | 1054 | pm2->ac_chg.external = true; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1055 | |
| 1056 | /* Create a work queue for the charger */ |
Axel Lin | c509a62 | 2013-05-16 21:17:04 +0800 | [diff] [blame] | 1057 | pm2->charger_wq = create_singlethread_workqueue("pm2xxx_charger_wq"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1058 | if (pm2->charger_wq == NULL) { |
Axel Lin | c509a62 | 2013-05-16 21:17:04 +0800 | [diff] [blame] | 1059 | ret = -ENOMEM; |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1060 | dev_err(pm2->dev, "failed to create work queue\n"); |
| 1061 | goto free_device_info; |
| 1062 | } |
| 1063 | |
| 1064 | /* Init work for charger detection */ |
| 1065 | INIT_WORK(&pm2->ac_work, pm2xxx_charger_ac_work); |
| 1066 | |
| 1067 | /* Init work for checking HW status */ |
| 1068 | INIT_WORK(&pm2->check_main_thermal_prot_work, |
| 1069 | pm2xxx_charger_check_main_thermal_prot_work); |
| 1070 | |
Rupesh Kumar | 789ca7b | 2012-06-19 10:21:24 +0530 | [diff] [blame] | 1071 | /* Init work for HW failure check */ |
| 1072 | INIT_DEFERRABLE_WORK(&pm2->check_hw_failure_work, |
| 1073 | pm2xxx_charger_check_hw_failure_work); |
| 1074 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1075 | /* |
| 1076 | * VDD ADC supply needs to be enabled from this driver when there |
| 1077 | * is a charger connected to avoid erroneous BTEMP_HIGH/LOW |
| 1078 | * interrupts during charging |
| 1079 | */ |
| 1080 | pm2->regu = regulator_get(pm2->dev, "vddadc"); |
| 1081 | if (IS_ERR(pm2->regu)) { |
| 1082 | ret = PTR_ERR(pm2->regu); |
| 1083 | dev_err(pm2->dev, "failed to get vddadc regulator\n"); |
| 1084 | goto free_charger_wq; |
| 1085 | } |
| 1086 | |
| 1087 | /* Register AC charger class */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1088 | pm2->ac_chg.psy = power_supply_register(pm2->dev, &pm2->ac_chg_desc, |
| 1089 | &psy_cfg); |
| 1090 | if (IS_ERR(pm2->ac_chg.psy)) { |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1091 | dev_err(pm2->dev, "failed to register AC charger\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1092 | ret = PTR_ERR(pm2->ac_chg.psy); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1093 | goto free_regulator; |
| 1094 | } |
| 1095 | |
| 1096 | /* Register interrupts */ |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1097 | ret = request_threaded_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), |
| 1098 | NULL, |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1099 | pm2xxx_charger_irq[0].isr, |
| 1100 | pm2->pdata->irq_type, |
| 1101 | pm2xxx_charger_irq[0].name, pm2); |
| 1102 | |
| 1103 | if (ret != 0) { |
| 1104 | dev_err(pm2->dev, "failed to request %s IRQ %d: %d\n", |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1105 | pm2xxx_charger_irq[0].name, |
| 1106 | gpio_to_irq(pm2->pdata->gpio_irq_number), ret); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1107 | goto unregister_pm2xxx_charger; |
| 1108 | } |
| 1109 | |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 1110 | ret = pm_runtime_set_active(pm2->dev); |
| 1111 | if (ret) |
| 1112 | dev_err(pm2->dev, "set active Error\n"); |
| 1113 | |
| 1114 | pm_runtime_enable(pm2->dev); |
| 1115 | pm_runtime_set_autosuspend_delay(pm2->dev, PM2XXX_AUTOSUSPEND_DELAY); |
| 1116 | pm_runtime_use_autosuspend(pm2->dev); |
| 1117 | pm_runtime_resume(pm2->dev); |
| 1118 | |
Rupesh Kumar | d4f510f | 2012-06-28 18:31:19 +0530 | [diff] [blame] | 1119 | /* pm interrupt can wake up system */ |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1120 | ret = enable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number)); |
Rupesh Kumar | d4f510f | 2012-06-28 18:31:19 +0530 | [diff] [blame] | 1121 | if (ret) { |
| 1122 | dev_err(pm2->dev, "failed to set irq wake\n"); |
| 1123 | goto unregister_pm2xxx_interrupt; |
| 1124 | } |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1125 | |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 1126 | mutex_init(&pm2->lock); |
| 1127 | |
lme00437 | a21e22f | 2012-11-13 14:43:48 +0100 | [diff] [blame] | 1128 | if (gpio_is_valid(pm2->pdata->lpn_gpio)) { |
| 1129 | /* get lpn GPIO from platform data */ |
| 1130 | pm2->lpn_pin = pm2->pdata->lpn_gpio; |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 1131 | |
lme00437 | a21e22f | 2012-11-13 14:43:48 +0100 | [diff] [blame] | 1132 | /* |
| 1133 | * Charger detection mechanism requires pulling up the LPN pin |
| 1134 | * while i2c communication if Charger is not connected |
| 1135 | * LPN pin of PM2301 is GPIO60 of AB9540 |
| 1136 | */ |
| 1137 | ret = gpio_request(pm2->lpn_pin, "pm2301_lpm_gpio"); |
| 1138 | |
| 1139 | if (ret < 0) { |
| 1140 | dev_err(pm2->dev, "pm2301_lpm_gpio request failed\n"); |
| 1141 | goto disable_pm2_irq_wake; |
| 1142 | } |
| 1143 | ret = gpio_direction_output(pm2->lpn_pin, 0); |
| 1144 | if (ret < 0) { |
| 1145 | dev_err(pm2->dev, "pm2301_lpm_gpio direction failed\n"); |
| 1146 | goto free_gpio; |
| 1147 | } |
| 1148 | set_lpn_pin(pm2); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1149 | } |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1150 | |
| 1151 | /* read interrupt registers */ |
| 1152 | for (i = 0; i < PM2XXX_NUM_INT_REG; i++) |
| 1153 | pm2xxx_reg_read(pm2, |
| 1154 | pm2xxx_interrupt_registers[i], |
| 1155 | &val); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1156 | |
| 1157 | ret = pm2xxx_charger_detection(pm2, &val); |
| 1158 | |
| 1159 | if ((ret == 0) && val) { |
| 1160 | pm2->ac.charger_connected = 1; |
Per Forlin | 54fbbb6 | 2012-07-05 16:52:55 +0200 | [diff] [blame] | 1161 | ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON, |
| 1162 | AB8500_MAIN_CH_DET); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1163 | pm2->ac_conn = true; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1164 | power_supply_changed(pm2->ac_chg.psy); |
| 1165 | sysfs_notify(&pm2->ac_chg.psy->dev.kobj, NULL, "present"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | return 0; |
| 1169 | |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 1170 | free_gpio: |
lme00437 | a21e22f | 2012-11-13 14:43:48 +0100 | [diff] [blame] | 1171 | if (gpio_is_valid(pm2->lpn_pin)) |
| 1172 | gpio_free(pm2->lpn_pin); |
Rupesh Kumar | d4f510f | 2012-06-28 18:31:19 +0530 | [diff] [blame] | 1173 | disable_pm2_irq_wake: |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1174 | disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number)); |
Rupesh Kumar | d4f510f | 2012-06-28 18:31:19 +0530 | [diff] [blame] | 1175 | unregister_pm2xxx_interrupt: |
| 1176 | /* disable interrupt */ |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1177 | free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1178 | unregister_pm2xxx_charger: |
| 1179 | /* unregister power supply */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1180 | power_supply_unregister(pm2->ac_chg.psy); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1181 | free_regulator: |
| 1182 | /* disable the regulator */ |
| 1183 | regulator_put(pm2->regu); |
| 1184 | free_charger_wq: |
| 1185 | destroy_workqueue(pm2->charger_wq); |
| 1186 | free_device_info: |
| 1187 | kfree(pm2); |
Rupesh Kumar | d4f510f | 2012-06-28 18:31:19 +0530 | [diff] [blame] | 1188 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1189 | return ret; |
| 1190 | } |
| 1191 | |
Lee Jones | 116c326 | 2013-02-25 14:26:52 +0000 | [diff] [blame] | 1192 | static int pm2xxx_wall_charger_remove(struct i2c_client *i2c_client) |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1193 | { |
| 1194 | struct pm2xxx_charger *pm2 = i2c_get_clientdata(i2c_client); |
| 1195 | |
Lee Jones | aee2b84 | 2013-02-25 14:28:59 +0000 | [diff] [blame] | 1196 | /* Disable pm_runtime */ |
| 1197 | pm_runtime_disable(pm2->dev); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1198 | /* Disable AC charging */ |
| 1199 | pm2xxx_charger_ac_en(&pm2->ac_chg, false, 0, 0); |
| 1200 | |
Rupesh Kumar | d4f510f | 2012-06-28 18:31:19 +0530 | [diff] [blame] | 1201 | /* Disable wake by pm interrupt */ |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1202 | disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number)); |
Rupesh Kumar | d4f510f | 2012-06-28 18:31:19 +0530 | [diff] [blame] | 1203 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1204 | /* Disable interrupts */ |
M BenZoubeir | f4095a0 | 2012-09-13 10:34:18 +0200 | [diff] [blame] | 1205 | free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1206 | |
| 1207 | /* Delete the work queue */ |
| 1208 | destroy_workqueue(pm2->charger_wq); |
| 1209 | |
| 1210 | flush_scheduled_work(); |
| 1211 | |
| 1212 | /* disable the regulator */ |
| 1213 | regulator_put(pm2->regu); |
| 1214 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1215 | power_supply_unregister(pm2->ac_chg.psy); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1216 | |
lme00437 | a21e22f | 2012-11-13 14:43:48 +0100 | [diff] [blame] | 1217 | if (gpio_is_valid(pm2->lpn_pin)) |
| 1218 | gpio_free(pm2->lpn_pin); |
Lee Jones | 3988043 | 2013-01-23 14:38:15 +0000 | [diff] [blame] | 1219 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1220 | kfree(pm2); |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | static const struct i2c_device_id pm2xxx_id[] = { |
| 1226 | { "pm2301", 0 }, |
| 1227 | { } |
| 1228 | }; |
| 1229 | |
| 1230 | MODULE_DEVICE_TABLE(i2c, pm2xxx_id); |
| 1231 | |
| 1232 | static struct i2c_driver pm2xxx_charger_driver = { |
| 1233 | .probe = pm2xxx_wall_charger_probe, |
Lee Jones | 116c326 | 2013-02-25 14:26:52 +0000 | [diff] [blame] | 1234 | .remove = pm2xxx_wall_charger_remove, |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1235 | .driver = { |
| 1236 | .name = "pm2xxx-wall_charger", |
Arnd Bergmann | 0df6e32 | 2016-03-02 16:58:56 +0100 | [diff] [blame] | 1237 | .pm = IS_ENABLED(CONFIG_PM) ? &pm2xxx_pm_ops : NULL, |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1238 | }, |
| 1239 | .id_table = pm2xxx_id, |
| 1240 | }; |
| 1241 | |
| 1242 | static int __init pm2xxx_charger_init(void) |
| 1243 | { |
| 1244 | return i2c_add_driver(&pm2xxx_charger_driver); |
| 1245 | } |
| 1246 | |
| 1247 | static void __exit pm2xxx_charger_exit(void) |
| 1248 | { |
| 1249 | i2c_del_driver(&pm2xxx_charger_driver); |
| 1250 | } |
| 1251 | |
lme00437 | a21e22f | 2012-11-13 14:43:48 +0100 | [diff] [blame] | 1252 | device_initcall_sync(pm2xxx_charger_init); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1253 | module_exit(pm2xxx_charger_exit); |
| 1254 | |
| 1255 | MODULE_LICENSE("GPL v2"); |
| 1256 | MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay"); |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1257 | MODULE_DESCRIPTION("PM2xxx charger management driver"); |