David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * tps65010 - driver for tps6501x power management chips |
| 3 | * |
| 4 | * Copyright (C) 2004 Texas Instruments |
| 5 | * Copyright (C) 2004-2005 David Brownell |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 21 | |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/interrupt.h> |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 27 | #include <linux/i2c.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/workqueue.h> |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/seq_file.h> |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 33 | |
David Brownell | 6d16bfb | 2008-01-27 18:14:49 +0100 | [diff] [blame^] | 34 | #include <linux/i2c/tps65010.h> |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 35 | |
| 36 | /*-------------------------------------------------------------------------*/ |
| 37 | |
| 38 | #define DRIVER_VERSION "2 May 2005" |
David Brownell | 4801bc2 | 2006-08-11 22:53:08 +0200 | [diff] [blame] | 39 | #define DRIVER_NAME (tps65010_driver.driver.name) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 40 | |
| 41 | MODULE_DESCRIPTION("TPS6501x Power Management Driver"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 44 | static struct i2c_driver tps65010_driver; |
| 45 | |
| 46 | /*-------------------------------------------------------------------------*/ |
| 47 | |
| 48 | /* This driver handles a family of multipurpose chips, which incorporate |
| 49 | * voltage regulators, lithium ion/polymer battery charging, GPIOs, LEDs, |
| 50 | * and other features often needed in portable devices like cell phones |
| 51 | * or digital cameras. |
| 52 | * |
| 53 | * The tps65011 and tps65013 have different voltage settings compared |
| 54 | * to tps65010 and tps65012. The tps65013 has a NO_CHG status/irq. |
| 55 | * All except tps65010 have "wait" mode, possibly defaulted so that |
| 56 | * battery-insert != device-on. |
| 57 | * |
| 58 | * We could distinguish between some models by checking VDCDC1.UVLO or |
| 59 | * other registers, unless they've been changed already after powerup |
| 60 | * as part of board setup by a bootloader. |
| 61 | */ |
| 62 | enum tps_model { |
| 63 | TPS_UNKNOWN = 0, |
| 64 | TPS65010, |
| 65 | TPS65011, |
| 66 | TPS65012, |
| 67 | TPS65013, |
| 68 | }; |
| 69 | |
| 70 | struct tps65010 { |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 71 | struct i2c_client *client; |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 72 | struct mutex lock; |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 73 | struct delayed_work work; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 74 | struct dentry *file; |
| 75 | unsigned charging:1; |
| 76 | unsigned por:1; |
| 77 | unsigned model:8; |
| 78 | u16 vbus; |
| 79 | unsigned long flags; |
| 80 | #define FLAG_VBUS_CHANGED 0 |
| 81 | #define FLAG_IRQ_ENABLE 1 |
| 82 | |
| 83 | /* copies of last register state */ |
| 84 | u8 chgstatus, regstatus, chgconf; |
| 85 | u8 nmask1, nmask2; |
| 86 | |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 87 | /* not currently tracking GPIO state */ |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
David Brownell | 4801bc2 | 2006-08-11 22:53:08 +0200 | [diff] [blame] | 90 | #define POWER_POLL_DELAY msecs_to_jiffies(5000) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 91 | |
| 92 | /*-------------------------------------------------------------------------*/ |
| 93 | |
| 94 | #if defined(DEBUG) || defined(CONFIG_DEBUG_FS) |
| 95 | |
| 96 | static void dbg_chgstat(char *buf, size_t len, u8 chgstatus) |
| 97 | { |
| 98 | snprintf(buf, len, "%02x%s%s%s%s%s%s%s%s\n", |
| 99 | chgstatus, |
| 100 | (chgstatus & TPS_CHG_USB) ? " USB" : "", |
| 101 | (chgstatus & TPS_CHG_AC) ? " AC" : "", |
| 102 | (chgstatus & TPS_CHG_THERM) ? " therm" : "", |
| 103 | (chgstatus & TPS_CHG_TERM) ? " done" : |
| 104 | ((chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) |
| 105 | ? " (charging)" : ""), |
| 106 | (chgstatus & TPS_CHG_TAPER_TMO) ? " taper_tmo" : "", |
| 107 | (chgstatus & TPS_CHG_CHG_TMO) ? " charge_tmo" : "", |
| 108 | (chgstatus & TPS_CHG_PRECHG_TMO) ? " prechg_tmo" : "", |
| 109 | (chgstatus & TPS_CHG_TEMP_ERR) ? " temp_err" : ""); |
| 110 | } |
| 111 | |
| 112 | static void dbg_regstat(char *buf, size_t len, u8 regstatus) |
| 113 | { |
| 114 | snprintf(buf, len, "%02x %s%s%s%s%s%s%s%s\n", |
| 115 | regstatus, |
| 116 | (regstatus & TPS_REG_ONOFF) ? "off" : "(on)", |
| 117 | (regstatus & TPS_REG_COVER) ? " uncover" : "", |
| 118 | (regstatus & TPS_REG_UVLO) ? " UVLO" : "", |
| 119 | (regstatus & TPS_REG_NO_CHG) ? " NO_CHG" : "", |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 120 | (regstatus & TPS_REG_PG_LD02) ? " ld02_bad" : "", |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 121 | (regstatus & TPS_REG_PG_LD01) ? " ld01_bad" : "", |
| 122 | (regstatus & TPS_REG_PG_MAIN) ? " main_bad" : "", |
| 123 | (regstatus & TPS_REG_PG_CORE) ? " core_bad" : ""); |
| 124 | } |
| 125 | |
| 126 | static void dbg_chgconf(int por, char *buf, size_t len, u8 chgconfig) |
| 127 | { |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 128 | const char *hibit; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 129 | |
| 130 | if (por) |
| 131 | hibit = (chgconfig & TPS_CHARGE_POR) |
| 132 | ? "POR=69ms" : "POR=1sec"; |
| 133 | else |
| 134 | hibit = (chgconfig & TPS65013_AUA) ? "AUA" : ""; |
| 135 | |
| 136 | snprintf(buf, len, "%02x %s%s%s AC=%d%% USB=%dmA %sCharge\n", |
| 137 | chgconfig, hibit, |
| 138 | (chgconfig & TPS_CHARGE_RESET) ? " reset" : "", |
| 139 | (chgconfig & TPS_CHARGE_FAST) ? " fast" : "", |
| 140 | ({int p; switch ((chgconfig >> 3) & 3) { |
| 141 | case 3: p = 100; break; |
| 142 | case 2: p = 75; break; |
| 143 | case 1: p = 50; break; |
| 144 | default: p = 25; break; |
| 145 | }; p; }), |
| 146 | (chgconfig & TPS_VBUS_CHARGING) |
| 147 | ? ((chgconfig & TPS_VBUS_500MA) ? 500 : 100) |
| 148 | : 0, |
| 149 | (chgconfig & TPS_CHARGE_ENABLE) ? "" : "No"); |
| 150 | } |
| 151 | |
| 152 | #endif |
| 153 | |
| 154 | #ifdef DEBUG |
| 155 | |
| 156 | static void show_chgstatus(const char *label, u8 chgstatus) |
| 157 | { |
| 158 | char buf [100]; |
| 159 | |
| 160 | dbg_chgstat(buf, sizeof buf, chgstatus); |
| 161 | pr_debug("%s: %s %s", DRIVER_NAME, label, buf); |
| 162 | } |
| 163 | |
| 164 | static void show_regstatus(const char *label, u8 regstatus) |
| 165 | { |
| 166 | char buf [100]; |
| 167 | |
| 168 | dbg_regstat(buf, sizeof buf, regstatus); |
| 169 | pr_debug("%s: %s %s", DRIVER_NAME, label, buf); |
| 170 | } |
| 171 | |
| 172 | static void show_chgconfig(int por, const char *label, u8 chgconfig) |
| 173 | { |
| 174 | char buf [100]; |
| 175 | |
| 176 | dbg_chgconf(por, buf, sizeof buf, chgconfig); |
| 177 | pr_debug("%s: %s %s", DRIVER_NAME, label, buf); |
| 178 | } |
| 179 | |
| 180 | #else |
| 181 | |
| 182 | static inline void show_chgstatus(const char *label, u8 chgstatus) { } |
| 183 | static inline void show_regstatus(const char *label, u8 chgstatus) { } |
| 184 | static inline void show_chgconfig(int por, const char *label, u8 chgconfig) { } |
| 185 | |
| 186 | #endif |
| 187 | |
| 188 | #ifdef CONFIG_DEBUG_FS |
| 189 | |
| 190 | static int dbg_show(struct seq_file *s, void *_) |
| 191 | { |
| 192 | struct tps65010 *tps = s->private; |
| 193 | u8 value, v2; |
| 194 | unsigned i; |
| 195 | char buf[100]; |
| 196 | const char *chip; |
| 197 | |
| 198 | switch (tps->model) { |
| 199 | case TPS65010: chip = "tps65010"; break; |
| 200 | case TPS65011: chip = "tps65011"; break; |
| 201 | case TPS65012: chip = "tps65012"; break; |
| 202 | case TPS65013: chip = "tps65013"; break; |
| 203 | default: chip = NULL; break; |
| 204 | } |
| 205 | seq_printf(s, "driver %s\nversion %s\nchip %s\n\n", |
| 206 | DRIVER_NAME, DRIVER_VERSION, chip); |
| 207 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 208 | mutex_lock(&tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 209 | |
| 210 | /* FIXME how can we tell whether a battery is present? |
| 211 | * likely involves a charge gauging chip (like BQ26501). |
| 212 | */ |
| 213 | |
| 214 | seq_printf(s, "%scharging\n\n", tps->charging ? "" : "(not) "); |
| 215 | |
| 216 | |
| 217 | /* registers for monitoring battery charging and status; note |
| 218 | * that reading chgstat and regstat may ack IRQs... |
| 219 | */ |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 220 | value = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 221 | dbg_chgconf(tps->por, buf, sizeof buf, value); |
| 222 | seq_printf(s, "chgconfig %s", buf); |
| 223 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 224 | value = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 225 | dbg_chgstat(buf, sizeof buf, value); |
| 226 | seq_printf(s, "chgstat %s", buf); |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 227 | value = i2c_smbus_read_byte_data(tps->client, TPS_MASK1); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 228 | dbg_chgstat(buf, sizeof buf, value); |
| 229 | seq_printf(s, "mask1 %s", buf); |
| 230 | /* ignore ackint1 */ |
| 231 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 232 | value = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 233 | dbg_regstat(buf, sizeof buf, value); |
| 234 | seq_printf(s, "regstat %s", buf); |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 235 | value = i2c_smbus_read_byte_data(tps->client, TPS_MASK2); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 236 | dbg_regstat(buf, sizeof buf, value); |
| 237 | seq_printf(s, "mask2 %s\n", buf); |
| 238 | /* ignore ackint2 */ |
| 239 | |
| 240 | (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY); |
| 241 | |
| 242 | |
| 243 | /* VMAIN voltage, enable lowpower, etc */ |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 244 | value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC1); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 245 | seq_printf(s, "vdcdc1 %02x\n", value); |
| 246 | |
| 247 | /* VCORE voltage, vibrator on/off */ |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 248 | value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC2); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 249 | seq_printf(s, "vdcdc2 %02x\n", value); |
| 250 | |
| 251 | /* both LD0s, and their lowpower behavior */ |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 252 | value = i2c_smbus_read_byte_data(tps->client, TPS_VREGS1); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 253 | seq_printf(s, "vregs1 %02x\n\n", value); |
| 254 | |
| 255 | |
| 256 | /* LEDs and GPIOs */ |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 257 | value = i2c_smbus_read_byte_data(tps->client, TPS_LED1_ON); |
| 258 | v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED1_PER); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 259 | seq_printf(s, "led1 %s, on=%02x, per=%02x, %d/%d msec\n", |
| 260 | (value & 0x80) |
| 261 | ? ((v2 & 0x80) ? "on" : "off") |
| 262 | : ((v2 & 0x80) ? "blink" : "(nPG)"), |
| 263 | value, v2, |
| 264 | (value & 0x7f) * 10, (v2 & 0x7f) * 100); |
| 265 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 266 | value = i2c_smbus_read_byte_data(tps->client, TPS_LED2_ON); |
| 267 | v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED2_PER); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 268 | seq_printf(s, "led2 %s, on=%02x, per=%02x, %d/%d msec\n", |
| 269 | (value & 0x80) |
| 270 | ? ((v2 & 0x80) ? "on" : "off") |
| 271 | : ((v2 & 0x80) ? "blink" : "off"), |
| 272 | value, v2, |
| 273 | (value & 0x7f) * 10, (v2 & 0x7f) * 100); |
| 274 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 275 | value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO); |
| 276 | v2 = i2c_smbus_read_byte_data(tps->client, TPS_MASK3); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 277 | seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2); |
| 278 | |
| 279 | for (i = 0; i < 4; i++) { |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 280 | if (value & (1 << (4 + i))) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 281 | seq_printf(s, " gpio%d-out %s\n", i + 1, |
| 282 | (value & (1 << i)) ? "low" : "hi "); |
| 283 | else |
| 284 | seq_printf(s, " gpio%d-in %s %s %s\n", i + 1, |
| 285 | (value & (1 << i)) ? "hi " : "low", |
| 286 | (v2 & (1 << i)) ? "no-irq" : "irq", |
| 287 | (v2 & (1 << (4 + i))) ? "rising" : "falling"); |
| 288 | } |
| 289 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 290 | mutex_unlock(&tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int dbg_tps_open(struct inode *inode, struct file *file) |
| 295 | { |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 296 | return single_open(file, dbg_show, inode->i_private); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 299 | static const struct file_operations debug_fops = { |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 300 | .open = dbg_tps_open, |
| 301 | .read = seq_read, |
| 302 | .llseek = seq_lseek, |
| 303 | .release = single_release, |
| 304 | }; |
| 305 | |
| 306 | #define DEBUG_FOPS &debug_fops |
| 307 | |
| 308 | #else |
| 309 | #define DEBUG_FOPS NULL |
| 310 | #endif |
| 311 | |
| 312 | /*-------------------------------------------------------------------------*/ |
| 313 | |
| 314 | /* handle IRQS in a task context, so we can use I2C calls */ |
| 315 | static void tps65010_interrupt(struct tps65010 *tps) |
| 316 | { |
| 317 | u8 tmp = 0, mask, poll; |
| 318 | |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 319 | /* IRQs won't trigger for certain events, but we can get |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 320 | * others by polling (normally, with external power applied). |
| 321 | */ |
| 322 | poll = 0; |
| 323 | |
| 324 | /* regstatus irqs */ |
| 325 | if (tps->nmask2) { |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 326 | tmp = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 327 | mask = tmp ^ tps->regstatus; |
| 328 | tps->regstatus = tmp; |
| 329 | mask &= tps->nmask2; |
| 330 | } else |
| 331 | mask = 0; |
| 332 | if (mask) { |
| 333 | tps->regstatus = tmp; |
| 334 | /* may need to shut something down ... */ |
| 335 | |
| 336 | /* "off" usually means deep sleep */ |
| 337 | if (tmp & TPS_REG_ONOFF) { |
| 338 | pr_info("%s: power off button\n", DRIVER_NAME); |
| 339 | #if 0 |
| 340 | /* REVISIT: this might need its own workqueue |
| 341 | * plus tweaks including deadlock avoidance ... |
Johannes Berg | ab3bfca | 2007-05-06 14:50:49 -0700 | [diff] [blame] | 342 | * also needs to get error handling and probably |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 343 | * an #ifdef CONFIG_HIBERNATION |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 344 | */ |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 345 | hibernate(); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 346 | #endif |
| 347 | poll = 1; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /* chgstatus irqs */ |
| 352 | if (tps->nmask1) { |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 353 | tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 354 | mask = tmp ^ tps->chgstatus; |
| 355 | tps->chgstatus = tmp; |
| 356 | mask &= tps->nmask1; |
| 357 | } else |
| 358 | mask = 0; |
| 359 | if (mask) { |
| 360 | unsigned charging = 0; |
| 361 | |
| 362 | show_chgstatus("chg/irq", tmp); |
| 363 | if (tmp & (TPS_CHG_USB|TPS_CHG_AC)) |
| 364 | show_chgconfig(tps->por, "conf", tps->chgconf); |
| 365 | |
| 366 | /* Unless it was turned off or disabled, we charge any |
| 367 | * battery whenever there's power available for it |
| 368 | * and the charger hasn't been disabled. |
| 369 | */ |
| 370 | if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC)) |
| 371 | && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) |
| 372 | && (tps->chgconf & TPS_CHARGE_ENABLE) |
| 373 | ) { |
| 374 | if (tps->chgstatus & TPS_CHG_USB) { |
| 375 | /* VBUS options are readonly until reconnect */ |
| 376 | if (mask & TPS_CHG_USB) |
| 377 | set_bit(FLAG_VBUS_CHANGED, &tps->flags); |
| 378 | charging = 1; |
| 379 | } else if (tps->chgstatus & TPS_CHG_AC) |
| 380 | charging = 1; |
| 381 | } |
| 382 | if (charging != tps->charging) { |
| 383 | tps->charging = charging; |
| 384 | pr_info("%s: battery %scharging\n", |
| 385 | DRIVER_NAME, charging ? "" : |
| 386 | ((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) |
| 387 | ? "NOT " : "dis")); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* always poll to detect (a) power removal, without tps65013 |
| 392 | * NO_CHG IRQ; or (b) restart of charging after stop. |
| 393 | */ |
| 394 | if ((tps->model != TPS65013 || !tps->charging) |
| 395 | && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))) |
| 396 | poll = 1; |
| 397 | if (poll) |
| 398 | (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY); |
| 399 | |
| 400 | /* also potentially gpio-in rise or fall */ |
| 401 | } |
| 402 | |
| 403 | /* handle IRQs and polling using keventd for now */ |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 404 | static void tps65010_work(struct work_struct *work) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 405 | { |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 406 | struct tps65010 *tps; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 407 | |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 408 | tps = container_of(work, struct tps65010, work.work); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 409 | mutex_lock(&tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 410 | |
| 411 | tps65010_interrupt(tps); |
| 412 | |
| 413 | if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) { |
| 414 | int status; |
| 415 | u8 chgconfig, tmp; |
| 416 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 417 | chgconfig = i2c_smbus_read_byte_data(tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 418 | TPS_CHGCONFIG); |
| 419 | chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING); |
| 420 | if (tps->vbus == 500) |
| 421 | chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING; |
| 422 | else if (tps->vbus >= 100) |
| 423 | chgconfig |= TPS_VBUS_CHARGING; |
| 424 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 425 | status = i2c_smbus_write_byte_data(tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 426 | TPS_CHGCONFIG, chgconfig); |
| 427 | |
| 428 | /* vbus update fails unless VBUS is connected! */ |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 429 | tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 430 | tps->chgconf = tmp; |
| 431 | show_chgconfig(tps->por, "update vbus", tmp); |
| 432 | } |
| 433 | |
| 434 | if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags)) |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 435 | enable_irq(tps->client->irq); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 436 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 437 | mutex_unlock(&tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 438 | } |
| 439 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 440 | static irqreturn_t tps65010_irq(int irq, void *_tps) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 441 | { |
| 442 | struct tps65010 *tps = _tps; |
| 443 | |
| 444 | disable_irq_nosync(irq); |
| 445 | set_bit(FLAG_IRQ_ENABLE, &tps->flags); |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 446 | (void) schedule_work(&tps->work.work); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 447 | return IRQ_HANDLED; |
| 448 | } |
| 449 | |
| 450 | /*-------------------------------------------------------------------------*/ |
| 451 | |
| 452 | static struct tps65010 *the_tps; |
| 453 | |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 454 | static int __exit tps65010_remove(struct i2c_client *client) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 455 | { |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 456 | struct tps65010 *tps = i2c_get_clientdata(client); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 457 | |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 458 | if (client->irq > 0) |
| 459 | free_irq(client->irq, tps); |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 460 | cancel_delayed_work(&tps->work); |
| 461 | flush_scheduled_work(); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 462 | debugfs_remove(tps->file); |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 463 | kfree(tps); |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 464 | the_tps = NULL; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 468 | static int tps65010_probe(struct i2c_client *client) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 469 | { |
| 470 | struct tps65010 *tps; |
| 471 | int status; |
| 472 | |
| 473 | if (the_tps) { |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 474 | dev_dbg(&client->dev, "only one tps6501x chip allowed\n"); |
| 475 | return -ENODEV; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 476 | } |
| 477 | |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 478 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 479 | return -EINVAL; |
| 480 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 481 | tps = kzalloc(sizeof *tps, GFP_KERNEL); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 482 | if (!tps) |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 483 | return -ENOMEM; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 484 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 485 | mutex_init(&tps->lock); |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 486 | INIT_DELAYED_WORK(&tps->work, tps65010_work); |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 487 | tps->client = client; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 488 | |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 489 | if (strcmp(client->name, "tps65010") == 0) |
| 490 | tps->model = TPS65010; |
| 491 | else if (strcmp(client->name, "tps65011") == 0) |
| 492 | tps->model = TPS65011; |
| 493 | else if (strcmp(client->name, "tps65012") == 0) |
| 494 | tps->model = TPS65012; |
| 495 | else if (strcmp(client->name, "tps65013") == 0) |
| 496 | tps->model = TPS65013; |
| 497 | else { |
| 498 | dev_warn(&client->dev, "unknown chip '%s'\n", client->name); |
| 499 | status = -ENODEV; |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 500 | goto fail1; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 501 | } |
| 502 | |
David Brownell | 4801bc2 | 2006-08-11 22:53:08 +0200 | [diff] [blame] | 503 | /* the IRQ is active low, but many gpio lines can't support that |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 504 | * so this driver uses falling-edge triggers instead. |
David Brownell | 4801bc2 | 2006-08-11 22:53:08 +0200 | [diff] [blame] | 505 | */ |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 506 | if (client->irq > 0) { |
| 507 | status = request_irq(client->irq, tps65010_irq, |
| 508 | IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING, |
| 509 | DRIVER_NAME, tps); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 510 | if (status < 0) { |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 511 | dev_dbg(&client->dev, "can't get IRQ %d, err %d\n", |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 512 | client->irq, status); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 513 | goto fail1; |
| 514 | } |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 515 | /* annoying race here, ideally we'd have an option |
| 516 | * to claim the irq now and enable it later. |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 517 | * FIXME genirq IRQF_NOAUTOEN now solves that ... |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 518 | */ |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 519 | disable_irq(client->irq); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 520 | set_bit(FLAG_IRQ_ENABLE, &tps->flags); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 521 | } else |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 522 | dev_warn(&client->dev, "IRQ not configured!\n"); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 523 | |
| 524 | |
| 525 | switch (tps->model) { |
| 526 | case TPS65010: |
| 527 | case TPS65012: |
| 528 | tps->por = 1; |
| 529 | break; |
| 530 | case TPS_UNKNOWN: |
| 531 | printk(KERN_WARNING "%s: unknown TPS chip\n", DRIVER_NAME); |
| 532 | break; |
| 533 | /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */ |
| 534 | } |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 535 | tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 536 | show_chgconfig(tps->por, "conf/init", tps->chgconf); |
| 537 | |
| 538 | show_chgstatus("chg/init", |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 539 | i2c_smbus_read_byte_data(client, TPS_CHGSTATUS)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 540 | show_regstatus("reg/init", |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 541 | i2c_smbus_read_byte_data(client, TPS_REGSTATUS)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 542 | |
| 543 | pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 544 | i2c_smbus_read_byte_data(client, TPS_VDCDC1), |
| 545 | i2c_smbus_read_byte_data(client, TPS_VDCDC2), |
| 546 | i2c_smbus_read_byte_data(client, TPS_VREGS1)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 547 | pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 548 | i2c_smbus_read_byte_data(client, TPS_DEFGPIO), |
| 549 | i2c_smbus_read_byte_data(client, TPS_MASK3)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 550 | |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 551 | the_tps = tps; |
| 552 | |
| 553 | #if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG) |
| 554 | /* USB hosts can't draw VBUS. OTG devices could, later |
| 555 | * when OTG infrastructure enables it. USB peripherals |
| 556 | * could be relying on VBUS while booting, though. |
| 557 | */ |
| 558 | tps->vbus = 100; |
| 559 | #endif |
| 560 | |
| 561 | /* unmask the "interesting" irqs, then poll once to |
| 562 | * kickstart monitoring, initialize shadowed status |
| 563 | * registers, and maybe disable VBUS draw. |
| 564 | */ |
| 565 | tps->nmask1 = ~0; |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 566 | (void) i2c_smbus_write_byte_data(client, TPS_MASK1, ~tps->nmask1); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 567 | |
| 568 | tps->nmask2 = TPS_REG_ONOFF; |
| 569 | if (tps->model == TPS65013) |
| 570 | tps->nmask2 |= TPS_REG_NO_CHG; |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 571 | (void) i2c_smbus_write_byte_data(client, TPS_MASK2, ~tps->nmask2); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 572 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 573 | (void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f |
| 574 | | i2c_smbus_read_byte_data(client, TPS_MASK3)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 575 | |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 576 | tps65010_work(&tps->work.work); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 577 | |
| 578 | tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL, |
| 579 | tps, DEBUG_FOPS); |
| 580 | return 0; |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 581 | fail1: |
| 582 | kfree(tps); |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 583 | return status; |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static struct i2c_driver tps65010_driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 587 | .driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 588 | .name = "tps65010", |
| 589 | }, |
David Brownell | 8056c6c | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 590 | .probe = tps65010_probe, |
| 591 | .remove = __exit_p(tps65010_remove), |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | /*-------------------------------------------------------------------------*/ |
| 595 | |
| 596 | /* Draw from VBUS: |
| 597 | * 0 mA -- DON'T DRAW (might supply power instead) |
| 598 | * 100 mA -- usb unit load (slowest charge rate) |
| 599 | * 500 mA -- usb high power (fast battery charge) |
| 600 | */ |
| 601 | int tps65010_set_vbus_draw(unsigned mA) |
| 602 | { |
| 603 | unsigned long flags; |
| 604 | |
| 605 | if (!the_tps) |
| 606 | return -ENODEV; |
| 607 | |
| 608 | /* assumes non-SMP */ |
| 609 | local_irq_save(flags); |
| 610 | if (mA >= 500) |
| 611 | mA = 500; |
| 612 | else if (mA >= 100) |
| 613 | mA = 100; |
| 614 | else |
| 615 | mA = 0; |
| 616 | the_tps->vbus = mA; |
| 617 | if ((the_tps->chgstatus & TPS_CHG_USB) |
| 618 | && test_and_set_bit( |
| 619 | FLAG_VBUS_CHANGED, &the_tps->flags)) { |
| 620 | /* gadget drivers call this in_irq() */ |
David Brownell | 8c1bc04 | 2006-12-13 00:33:46 -0800 | [diff] [blame] | 621 | (void) schedule_work(&the_tps->work.work); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 622 | } |
| 623 | local_irq_restore(flags); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | EXPORT_SYMBOL(tps65010_set_vbus_draw); |
| 628 | |
| 629 | /*-------------------------------------------------------------------------*/ |
| 630 | /* tps65010_set_gpio_out_value parameter: |
| 631 | * gpio: GPIO1, GPIO2, GPIO3 or GPIO4 |
| 632 | * value: LOW or HIGH |
| 633 | */ |
| 634 | int tps65010_set_gpio_out_value(unsigned gpio, unsigned value) |
| 635 | { |
| 636 | int status; |
| 637 | unsigned defgpio; |
| 638 | |
| 639 | if (!the_tps) |
| 640 | return -ENODEV; |
| 641 | if ((gpio < GPIO1) || (gpio > GPIO4)) |
| 642 | return -EINVAL; |
| 643 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 644 | mutex_lock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 645 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 646 | defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 647 | |
| 648 | /* Configure GPIO for output */ |
| 649 | defgpio |= 1 << (gpio + 3); |
| 650 | |
| 651 | /* Writing 1 forces a logic 0 on that GPIO and vice versa */ |
| 652 | switch (value) { |
| 653 | case LOW: |
| 654 | defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */ |
| 655 | break; |
| 656 | /* case HIGH: */ |
| 657 | default: |
| 658 | defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */ |
| 659 | break; |
| 660 | } |
| 661 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 662 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 663 | TPS_DEFGPIO, defgpio); |
| 664 | |
| 665 | pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME, |
| 666 | gpio, value ? "high" : "low", |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 667 | i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 668 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 669 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 670 | return status; |
| 671 | } |
| 672 | EXPORT_SYMBOL(tps65010_set_gpio_out_value); |
| 673 | |
| 674 | /*-------------------------------------------------------------------------*/ |
| 675 | /* tps65010_set_led parameter: |
| 676 | * led: LED1 or LED2 |
| 677 | * mode: ON, OFF or BLINK |
| 678 | */ |
| 679 | int tps65010_set_led(unsigned led, unsigned mode) |
| 680 | { |
| 681 | int status; |
| 682 | unsigned led_on, led_per, offs; |
| 683 | |
| 684 | if (!the_tps) |
| 685 | return -ENODEV; |
| 686 | |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 687 | if (led == LED1) |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 688 | offs = 0; |
| 689 | else { |
| 690 | offs = 2; |
| 691 | led = LED2; |
| 692 | } |
| 693 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 694 | mutex_lock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 695 | |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 696 | pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 697 | i2c_smbus_read_byte_data(the_tps->client, |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 698 | TPS_LED1_ON + offs)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 699 | |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 700 | pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 701 | i2c_smbus_read_byte_data(the_tps->client, |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 702 | TPS_LED1_PER + offs)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 703 | |
| 704 | switch (mode) { |
| 705 | case OFF: |
| 706 | led_on = 1 << 7; |
| 707 | led_per = 0 << 7; |
| 708 | break; |
| 709 | case ON: |
| 710 | led_on = 1 << 7; |
| 711 | led_per = 1 << 7; |
| 712 | break; |
| 713 | case BLINK: |
| 714 | led_on = 0x30 | (0 << 7); |
| 715 | led_per = 0x08 | (1 << 7); |
| 716 | break; |
| 717 | default: |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 718 | printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n", |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 719 | DRIVER_NAME); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 720 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 721 | return -EINVAL; |
| 722 | } |
| 723 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 724 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 725 | TPS_LED1_ON + offs, led_on); |
| 726 | |
| 727 | if (status != 0) { |
| 728 | printk(KERN_ERR "%s: Failed to write led%i_on register\n", |
| 729 | DRIVER_NAME, led); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 730 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 731 | return status; |
| 732 | } |
| 733 | |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 734 | pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 735 | i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 736 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 737 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 738 | TPS_LED1_PER + offs, led_per); |
| 739 | |
| 740 | if (status != 0) { |
| 741 | printk(KERN_ERR "%s: Failed to write led%i_per register\n", |
| 742 | DRIVER_NAME, led); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 743 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 744 | return status; |
| 745 | } |
| 746 | |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 747 | pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 748 | i2c_smbus_read_byte_data(the_tps->client, |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 749 | TPS_LED1_PER + offs)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 750 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 751 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 752 | |
| 753 | return status; |
| 754 | } |
| 755 | EXPORT_SYMBOL(tps65010_set_led); |
| 756 | |
| 757 | /*-------------------------------------------------------------------------*/ |
| 758 | /* tps65010_set_vib parameter: |
| 759 | * value: ON or OFF |
| 760 | */ |
| 761 | int tps65010_set_vib(unsigned value) |
| 762 | { |
| 763 | int status; |
| 764 | unsigned vdcdc2; |
| 765 | |
| 766 | if (!the_tps) |
| 767 | return -ENODEV; |
| 768 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 769 | mutex_lock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 770 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 771 | vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 772 | vdcdc2 &= ~(1 << 1); |
| 773 | if (value) |
| 774 | vdcdc2 |= (1 << 1); |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 775 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 776 | TPS_VDCDC2, vdcdc2); |
| 777 | |
| 778 | pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off"); |
| 779 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 780 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 781 | return status; |
| 782 | } |
| 783 | EXPORT_SYMBOL(tps65010_set_vib); |
| 784 | |
| 785 | /*-------------------------------------------------------------------------*/ |
| 786 | /* tps65010_set_low_pwr parameter: |
| 787 | * mode: ON or OFF |
| 788 | */ |
| 789 | int tps65010_set_low_pwr(unsigned mode) |
| 790 | { |
| 791 | int status; |
| 792 | unsigned vdcdc1; |
| 793 | |
| 794 | if (!the_tps) |
| 795 | return -ENODEV; |
| 796 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 797 | mutex_lock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 798 | |
| 799 | pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME, |
| 800 | mode ? "enable" : "disable", |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 801 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 802 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 803 | vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 804 | |
| 805 | switch (mode) { |
| 806 | case OFF: |
| 807 | vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */ |
| 808 | break; |
| 809 | /* case ON: */ |
| 810 | default: |
| 811 | vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */ |
| 812 | break; |
| 813 | } |
| 814 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 815 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 816 | TPS_VDCDC1, vdcdc1); |
| 817 | |
| 818 | if (status != 0) |
| 819 | printk(KERN_ERR "%s: Failed to write vdcdc1 register\n", |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 820 | DRIVER_NAME); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 821 | else |
| 822 | pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 823 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 824 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 825 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 826 | |
| 827 | return status; |
| 828 | } |
| 829 | EXPORT_SYMBOL(tps65010_set_low_pwr); |
| 830 | |
| 831 | /*-------------------------------------------------------------------------*/ |
| 832 | /* tps65010_config_vregs1 parameter: |
| 833 | * value to be written to VREGS1 register |
| 834 | * Note: The complete register is written, set all bits you need |
| 835 | */ |
| 836 | int tps65010_config_vregs1(unsigned value) |
| 837 | { |
| 838 | int status; |
| 839 | |
| 840 | if (!the_tps) |
| 841 | return -ENODEV; |
| 842 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 843 | mutex_lock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 844 | |
| 845 | pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 846 | i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 847 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 848 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 849 | TPS_VREGS1, value); |
| 850 | |
| 851 | if (status != 0) |
| 852 | printk(KERN_ERR "%s: Failed to write vregs1 register\n", |
david-b@pacbell.net | 65fc50e | 2005-06-29 07:13:00 -0700 | [diff] [blame] | 853 | DRIVER_NAME); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 854 | else |
| 855 | pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 856 | i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 857 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 858 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 859 | |
| 860 | return status; |
| 861 | } |
| 862 | EXPORT_SYMBOL(tps65010_config_vregs1); |
| 863 | |
| 864 | /*-------------------------------------------------------------------------*/ |
| 865 | /* tps65013_set_low_pwr parameter: |
| 866 | * mode: ON or OFF |
| 867 | */ |
| 868 | |
| 869 | /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not |
| 870 | required if power supply is through a battery */ |
| 871 | |
| 872 | int tps65013_set_low_pwr(unsigned mode) |
| 873 | { |
| 874 | int status; |
| 875 | unsigned vdcdc1, chgconfig; |
| 876 | |
| 877 | if (!the_tps || the_tps->por) |
| 878 | return -ENODEV; |
| 879 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 880 | mutex_lock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 881 | |
| 882 | pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n", |
| 883 | DRIVER_NAME, |
| 884 | mode ? "enable" : "disable", |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 885 | i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG), |
| 886 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 887 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 888 | chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG); |
| 889 | vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 890 | |
| 891 | switch (mode) { |
| 892 | case OFF: |
| 893 | chgconfig &= ~TPS65013_AUA; /* disable AUA bit */ |
| 894 | vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */ |
| 895 | break; |
| 896 | /* case ON: */ |
| 897 | default: |
| 898 | chgconfig |= TPS65013_AUA; /* enable AUA bit */ |
| 899 | vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */ |
| 900 | break; |
| 901 | } |
| 902 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 903 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 904 | TPS_CHGCONFIG, chgconfig); |
| 905 | if (status != 0) { |
| 906 | printk(KERN_ERR "%s: Failed to write chconfig register\n", |
| 907 | DRIVER_NAME); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 908 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 909 | return status; |
| 910 | } |
| 911 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 912 | chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 913 | the_tps->chgconf = chgconfig; |
| 914 | show_chgconfig(0, "chgconf", chgconfig); |
| 915 | |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 916 | status = i2c_smbus_write_byte_data(the_tps->client, |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 917 | TPS_VDCDC1, vdcdc1); |
| 918 | |
| 919 | if (status != 0) |
| 920 | printk(KERN_ERR "%s: Failed to write vdcdc1 register\n", |
| 921 | DRIVER_NAME); |
| 922 | else |
| 923 | pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME, |
David Brownell | b5067f8 | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 924 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 925 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 926 | mutex_unlock(&the_tps->lock); |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 927 | |
| 928 | return status; |
| 929 | } |
| 930 | EXPORT_SYMBOL(tps65013_set_low_pwr); |
| 931 | |
| 932 | /*-------------------------------------------------------------------------*/ |
| 933 | |
| 934 | static int __init tps_init(void) |
| 935 | { |
| 936 | u32 tries = 3; |
| 937 | int status = -ENODEV; |
| 938 | |
| 939 | printk(KERN_INFO "%s: version %s\n", DRIVER_NAME, DRIVER_VERSION); |
| 940 | |
| 941 | /* some boards have startup glitches */ |
| 942 | while (tries--) { |
| 943 | status = i2c_add_driver(&tps65010_driver); |
| 944 | if (the_tps) |
| 945 | break; |
| 946 | i2c_del_driver(&tps65010_driver); |
| 947 | if (!tries) { |
| 948 | printk(KERN_ERR "%s: no chip?\n", DRIVER_NAME); |
| 949 | return -ENODEV; |
| 950 | } |
| 951 | pr_debug("%s: re-probe ...\n", DRIVER_NAME); |
| 952 | msleep(10); |
| 953 | } |
| 954 | |
David Brownell | 72cd799 | 2005-05-24 17:34:51 -0700 | [diff] [blame] | 955 | return status; |
| 956 | } |
| 957 | /* NOTE: this MUST be initialized before the other parts of the system |
| 958 | * that rely on it ... but after the i2c bus on which this relies. |
| 959 | * That is, much earlier than on PC-type systems, which don't often use |
| 960 | * I2C as a core system bus. |
| 961 | */ |
| 962 | subsys_initcall(tps_init); |
| 963 | |
| 964 | static void __exit tps_exit(void) |
| 965 | { |
| 966 | i2c_del_driver(&tps65010_driver); |
| 967 | } |
| 968 | module_exit(tps_exit); |
| 969 | |