Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for TI TPS6586x PMIC family |
| 3 | * |
| 4 | * Copyright (c) 2010 CompuLab Ltd. |
| 5 | * Mike Rapoport <mike@compulab.co.il> |
| 6 | * |
| 7 | * Based on da903x.c. |
| 8 | * Copyright (C) 2008 Compulab, Ltd. |
| 9 | * Mike Rapoport <mike@compulab.co.il> |
| 10 | * Copyright (C) 2006-2008 Marvell International Ltd. |
| 11 | * Eric Miao <eric.miao@marvell.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | */ |
| 17 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/irq.h> |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/gpio.h> |
| 25 | #include <linux/i2c.h> |
| 26 | |
| 27 | #include <linux/mfd/core.h> |
| 28 | #include <linux/mfd/tps6586x.h> |
| 29 | |
| 30 | /* GPIO control registers */ |
| 31 | #define TPS6586X_GPIOSET1 0x5d |
| 32 | #define TPS6586X_GPIOSET2 0x5e |
| 33 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 34 | /* interrupt control registers */ |
| 35 | #define TPS6586X_INT_ACK1 0xb5 |
| 36 | #define TPS6586X_INT_ACK2 0xb6 |
| 37 | #define TPS6586X_INT_ACK3 0xb7 |
| 38 | #define TPS6586X_INT_ACK4 0xb8 |
| 39 | |
| 40 | /* interrupt mask registers */ |
| 41 | #define TPS6586X_INT_MASK1 0xb0 |
| 42 | #define TPS6586X_INT_MASK2 0xb1 |
| 43 | #define TPS6586X_INT_MASK3 0xb2 |
| 44 | #define TPS6586X_INT_MASK4 0xb3 |
| 45 | #define TPS6586X_INT_MASK5 0xb4 |
| 46 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 47 | /* device id */ |
| 48 | #define TPS6586X_VERSIONCRC 0xcd |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 49 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 50 | struct tps6586x_irq_data { |
| 51 | u8 mask_reg; |
| 52 | u8 mask_mask; |
| 53 | }; |
| 54 | |
| 55 | #define TPS6586X_IRQ(_reg, _mask) \ |
| 56 | { \ |
| 57 | .mask_reg = (_reg) - TPS6586X_INT_MASK1, \ |
| 58 | .mask_mask = (_mask), \ |
| 59 | } |
| 60 | |
| 61 | static const struct tps6586x_irq_data tps6586x_irqs[] = { |
| 62 | [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0), |
| 63 | [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1), |
| 64 | [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2), |
| 65 | [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3), |
| 66 | [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4), |
| 67 | [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5), |
| 68 | [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6), |
| 69 | [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7), |
| 70 | [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0), |
| 71 | [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1), |
| 72 | [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2), |
| 73 | [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3), |
| 74 | [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4), |
| 75 | [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5), |
| 76 | [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6), |
| 77 | [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7), |
| 78 | [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4), |
| 79 | [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03), |
| 80 | [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2), |
| 81 | [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3), |
| 82 | [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0), |
| 83 | [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc), |
| 84 | [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06), |
| 85 | [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0), |
| 86 | [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5), |
| 87 | [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6), |
| 88 | [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1), |
| 89 | }; |
| 90 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 91 | struct tps6586x { |
| 92 | struct mutex lock; |
| 93 | struct device *dev; |
| 94 | struct i2c_client *client; |
| 95 | |
| 96 | struct gpio_chip gpio; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 97 | struct irq_chip irq_chip; |
| 98 | struct mutex irq_lock; |
| 99 | int irq_base; |
| 100 | u32 irq_en; |
| 101 | u8 mask_cache[5]; |
| 102 | u8 mask_reg[5]; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | static inline int __tps6586x_read(struct i2c_client *client, |
| 106 | int reg, uint8_t *val) |
| 107 | { |
| 108 | int ret; |
| 109 | |
| 110 | ret = i2c_smbus_read_byte_data(client, reg); |
| 111 | if (ret < 0) { |
| 112 | dev_err(&client->dev, "failed reading at 0x%02x\n", reg); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | *val = (uint8_t)ret; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static inline int __tps6586x_reads(struct i2c_client *client, int reg, |
| 122 | int len, uint8_t *val) |
| 123 | { |
| 124 | int ret; |
| 125 | |
| 126 | ret = i2c_smbus_read_i2c_block_data(client, reg, len, val); |
| 127 | if (ret < 0) { |
| 128 | dev_err(&client->dev, "failed reading from 0x%02x\n", reg); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static inline int __tps6586x_write(struct i2c_client *client, |
| 136 | int reg, uint8_t val) |
| 137 | { |
| 138 | int ret; |
| 139 | |
| 140 | ret = i2c_smbus_write_byte_data(client, reg, val); |
| 141 | if (ret < 0) { |
| 142 | dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n", |
| 143 | val, reg); |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static inline int __tps6586x_writes(struct i2c_client *client, int reg, |
| 151 | int len, uint8_t *val) |
| 152 | { |
vwadekar@nvidia.com | 4b57018 | 2011-02-24 10:18:13 +0530 | [diff] [blame] | 153 | int ret, i; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 154 | |
vwadekar@nvidia.com | 4b57018 | 2011-02-24 10:18:13 +0530 | [diff] [blame] | 155 | for (i = 0; i < len; i++) { |
| 156 | ret = __tps6586x_write(client, reg + i, *(val + i)); |
| 157 | if (ret < 0) |
| 158 | return ret; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int tps6586x_write(struct device *dev, int reg, uint8_t val) |
| 165 | { |
| 166 | return __tps6586x_write(to_i2c_client(dev), reg, val); |
| 167 | } |
| 168 | EXPORT_SYMBOL_GPL(tps6586x_write); |
| 169 | |
| 170 | int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val) |
| 171 | { |
| 172 | return __tps6586x_writes(to_i2c_client(dev), reg, len, val); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(tps6586x_writes); |
| 175 | |
| 176 | int tps6586x_read(struct device *dev, int reg, uint8_t *val) |
| 177 | { |
| 178 | return __tps6586x_read(to_i2c_client(dev), reg, val); |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(tps6586x_read); |
| 181 | |
| 182 | int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val) |
| 183 | { |
| 184 | return __tps6586x_reads(to_i2c_client(dev), reg, len, val); |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(tps6586x_reads); |
| 187 | |
| 188 | int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 189 | { |
| 190 | struct tps6586x *tps6586x = dev_get_drvdata(dev); |
| 191 | uint8_t reg_val; |
| 192 | int ret = 0; |
| 193 | |
| 194 | mutex_lock(&tps6586x->lock); |
| 195 | |
| 196 | ret = __tps6586x_read(to_i2c_client(dev), reg, ®_val); |
| 197 | if (ret) |
| 198 | goto out; |
| 199 | |
| 200 | if ((reg_val & bit_mask) == 0) { |
| 201 | reg_val |= bit_mask; |
| 202 | ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val); |
| 203 | } |
| 204 | out: |
| 205 | mutex_unlock(&tps6586x->lock); |
| 206 | return ret; |
| 207 | } |
| 208 | EXPORT_SYMBOL_GPL(tps6586x_set_bits); |
| 209 | |
| 210 | int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 211 | { |
| 212 | struct tps6586x *tps6586x = dev_get_drvdata(dev); |
| 213 | uint8_t reg_val; |
| 214 | int ret = 0; |
| 215 | |
| 216 | mutex_lock(&tps6586x->lock); |
| 217 | |
| 218 | ret = __tps6586x_read(to_i2c_client(dev), reg, ®_val); |
| 219 | if (ret) |
| 220 | goto out; |
| 221 | |
| 222 | if (reg_val & bit_mask) { |
| 223 | reg_val &= ~bit_mask; |
| 224 | ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val); |
| 225 | } |
| 226 | out: |
| 227 | mutex_unlock(&tps6586x->lock); |
| 228 | return ret; |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(tps6586x_clr_bits); |
| 231 | |
| 232 | int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask) |
| 233 | { |
| 234 | struct tps6586x *tps6586x = dev_get_drvdata(dev); |
| 235 | uint8_t reg_val; |
| 236 | int ret = 0; |
| 237 | |
| 238 | mutex_lock(&tps6586x->lock); |
| 239 | |
| 240 | ret = __tps6586x_read(tps6586x->client, reg, ®_val); |
| 241 | if (ret) |
| 242 | goto out; |
| 243 | |
| 244 | if ((reg_val & mask) != val) { |
| 245 | reg_val = (reg_val & ~mask) | val; |
| 246 | ret = __tps6586x_write(tps6586x->client, reg, reg_val); |
| 247 | } |
| 248 | out: |
| 249 | mutex_unlock(&tps6586x->lock); |
| 250 | return ret; |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(tps6586x_update); |
| 253 | |
| 254 | static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 255 | { |
| 256 | struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio); |
| 257 | uint8_t val; |
| 258 | int ret; |
| 259 | |
| 260 | ret = __tps6586x_read(tps6586x->client, TPS6586X_GPIOSET2, &val); |
| 261 | if (ret) |
| 262 | return ret; |
| 263 | |
| 264 | return !!(val & (1 << offset)); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | static void tps6586x_gpio_set(struct gpio_chip *chip, unsigned offset, |
| 269 | int value) |
| 270 | { |
| 271 | struct tps6586x *tps6586x = container_of(chip, struct tps6586x, gpio); |
| 272 | |
Vincent Palatin | e6f1945 | 2011-04-12 10:55:58 -0400 | [diff] [blame] | 273 | tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET2, |
| 274 | value << offset, 1 << offset); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 278 | int value) |
| 279 | { |
| 280 | struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio); |
| 281 | uint8_t val, mask; |
| 282 | |
| 283 | tps6586x_gpio_set(gc, offset, value); |
| 284 | |
| 285 | val = 0x1 << (offset * 2); |
| 286 | mask = 0x3 << (offset * 2); |
| 287 | |
| 288 | return tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET1, val, mask); |
| 289 | } |
| 290 | |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 291 | static int tps6586x_gpio_init(struct tps6586x *tps6586x, int gpio_base) |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 292 | { |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 293 | if (!gpio_base) |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 294 | return 0; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 295 | |
| 296 | tps6586x->gpio.owner = THIS_MODULE; |
| 297 | tps6586x->gpio.label = tps6586x->client->name; |
| 298 | tps6586x->gpio.dev = tps6586x->dev; |
| 299 | tps6586x->gpio.base = gpio_base; |
| 300 | tps6586x->gpio.ngpio = 4; |
| 301 | tps6586x->gpio.can_sleep = 1; |
| 302 | |
| 303 | /* FIXME: add handling of GPIOs as dedicated inputs */ |
| 304 | tps6586x->gpio.direction_output = tps6586x_gpio_output; |
| 305 | tps6586x->gpio.set = tps6586x_gpio_set; |
| 306 | tps6586x->gpio.get = tps6586x_gpio_get; |
| 307 | |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 308 | return gpiochip_add(&tps6586x->gpio); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static int __remove_subdev(struct device *dev, void *unused) |
| 312 | { |
| 313 | platform_device_unregister(to_platform_device(dev)); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int tps6586x_remove_subdevs(struct tps6586x *tps6586x) |
| 318 | { |
| 319 | return device_for_each_child(tps6586x->dev, NULL, __remove_subdev); |
| 320 | } |
| 321 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 322 | static void tps6586x_irq_lock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 323 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 324 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 325 | |
| 326 | mutex_lock(&tps6586x->irq_lock); |
| 327 | } |
| 328 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 329 | static void tps6586x_irq_enable(struct irq_data *irq_data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 330 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 331 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data); |
| 332 | unsigned int __irq = irq_data->irq - tps6586x->irq_base; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 333 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 334 | |
| 335 | tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask; |
| 336 | tps6586x->irq_en |= (1 << __irq); |
| 337 | } |
| 338 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 339 | static void tps6586x_irq_disable(struct irq_data *irq_data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 340 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 341 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 342 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 343 | unsigned int __irq = irq_data->irq - tps6586x->irq_base; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 344 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 345 | |
| 346 | tps6586x->mask_reg[data->mask_reg] |= data->mask_mask; |
| 347 | tps6586x->irq_en &= ~(1 << __irq); |
| 348 | } |
| 349 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 350 | static void tps6586x_irq_sync_unlock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 351 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 352 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 353 | int i; |
| 354 | |
| 355 | for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) { |
| 356 | if (tps6586x->mask_reg[i] != tps6586x->mask_cache[i]) { |
| 357 | if (!WARN_ON(tps6586x_write(tps6586x->dev, |
| 358 | TPS6586X_INT_MASK1 + i, |
| 359 | tps6586x->mask_reg[i]))) |
| 360 | tps6586x->mask_cache[i] = tps6586x->mask_reg[i]; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | mutex_unlock(&tps6586x->irq_lock); |
| 365 | } |
| 366 | |
| 367 | static irqreturn_t tps6586x_irq(int irq, void *data) |
| 368 | { |
| 369 | struct tps6586x *tps6586x = data; |
| 370 | u32 acks; |
| 371 | int ret = 0; |
| 372 | |
| 373 | ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, |
| 374 | sizeof(acks), (uint8_t *)&acks); |
| 375 | |
| 376 | if (ret < 0) { |
| 377 | dev_err(tps6586x->dev, "failed to read interrupt status\n"); |
| 378 | return IRQ_NONE; |
| 379 | } |
| 380 | |
| 381 | acks = le32_to_cpu(acks); |
| 382 | |
| 383 | while (acks) { |
| 384 | int i = __ffs(acks); |
| 385 | |
| 386 | if (tps6586x->irq_en & (1 << i)) |
| 387 | handle_nested_irq(tps6586x->irq_base + i); |
| 388 | |
| 389 | acks &= ~(1 << i); |
| 390 | } |
| 391 | |
| 392 | return IRQ_HANDLED; |
| 393 | } |
| 394 | |
| 395 | static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq, |
| 396 | int irq_base) |
| 397 | { |
| 398 | int i, ret; |
| 399 | u8 tmp[4]; |
| 400 | |
| 401 | if (!irq_base) { |
| 402 | dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n"); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
| 406 | mutex_init(&tps6586x->irq_lock); |
| 407 | for (i = 0; i < 5; i++) { |
| 408 | tps6586x->mask_cache[i] = 0xff; |
| 409 | tps6586x->mask_reg[i] = 0xff; |
| 410 | tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff); |
| 411 | } |
| 412 | |
| 413 | tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp); |
| 414 | |
| 415 | tps6586x->irq_base = irq_base; |
| 416 | |
| 417 | tps6586x->irq_chip.name = "tps6586x"; |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 418 | tps6586x->irq_chip.irq_enable = tps6586x_irq_enable; |
| 419 | tps6586x->irq_chip.irq_disable = tps6586x_irq_disable; |
| 420 | tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock; |
| 421 | tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 422 | |
| 423 | for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) { |
| 424 | int __irq = i + tps6586x->irq_base; |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 425 | irq_set_chip_data(__irq, tps6586x); |
| 426 | irq_set_chip_and_handler(__irq, &tps6586x->irq_chip, |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 427 | handle_simple_irq); |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 428 | irq_set_nested_thread(__irq, 1); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 429 | #ifdef CONFIG_ARM |
| 430 | set_irq_flags(__irq, IRQF_VALID); |
| 431 | #endif |
| 432 | } |
| 433 | |
| 434 | ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT, |
| 435 | "tps6586x", tps6586x); |
| 436 | |
| 437 | if (!ret) { |
| 438 | device_init_wakeup(tps6586x->dev, 1); |
| 439 | enable_irq_wake(irq); |
| 440 | } |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 445 | static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x, |
| 446 | struct tps6586x_platform_data *pdata) |
| 447 | { |
| 448 | struct tps6586x_subdev_info *subdev; |
| 449 | struct platform_device *pdev; |
| 450 | int i, ret = 0; |
| 451 | |
| 452 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 453 | subdev = &pdata->subdevs[i]; |
| 454 | |
| 455 | pdev = platform_device_alloc(subdev->name, subdev->id); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 456 | if (!pdev) { |
| 457 | ret = -ENOMEM; |
| 458 | goto failed; |
| 459 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 460 | |
| 461 | pdev->dev.parent = tps6586x->dev; |
| 462 | pdev->dev.platform_data = subdev->platform_data; |
| 463 | |
| 464 | ret = platform_device_add(pdev); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 465 | if (ret) { |
| 466 | platform_device_put(pdev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 467 | goto failed; |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 468 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 469 | } |
| 470 | return 0; |
| 471 | |
| 472 | failed: |
| 473 | tps6586x_remove_subdevs(tps6586x); |
| 474 | return ret; |
| 475 | } |
| 476 | |
| 477 | static int __devinit tps6586x_i2c_probe(struct i2c_client *client, |
| 478 | const struct i2c_device_id *id) |
| 479 | { |
| 480 | struct tps6586x_platform_data *pdata = client->dev.platform_data; |
| 481 | struct tps6586x *tps6586x; |
| 482 | int ret; |
| 483 | |
| 484 | if (!pdata) { |
| 485 | dev_err(&client->dev, "tps6586x requires platform data\n"); |
| 486 | return -ENOTSUPP; |
| 487 | } |
| 488 | |
| 489 | ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC); |
| 490 | if (ret < 0) { |
| 491 | dev_err(&client->dev, "Chip ID read failed: %d\n", ret); |
| 492 | return -EIO; |
| 493 | } |
| 494 | |
Stephen Warren | 4d1cdbf | 2010-12-09 10:30:11 -0700 | [diff] [blame] | 495 | dev_info(&client->dev, "VERSIONCRC is %02x\n", ret); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 496 | |
| 497 | tps6586x = kzalloc(sizeof(struct tps6586x), GFP_KERNEL); |
| 498 | if (tps6586x == NULL) |
| 499 | return -ENOMEM; |
| 500 | |
| 501 | tps6586x->client = client; |
| 502 | tps6586x->dev = &client->dev; |
| 503 | i2c_set_clientdata(client, tps6586x); |
| 504 | |
| 505 | mutex_init(&tps6586x->lock); |
| 506 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 507 | if (client->irq) { |
| 508 | ret = tps6586x_irq_init(tps6586x, client->irq, |
| 509 | pdata->irq_base); |
| 510 | if (ret) { |
| 511 | dev_err(&client->dev, "IRQ init failed: %d\n", ret); |
| 512 | goto err_irq_init; |
| 513 | } |
| 514 | } |
| 515 | |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 516 | ret = tps6586x_gpio_init(tps6586x, pdata->gpio_base); |
| 517 | if (ret) { |
| 518 | dev_err(&client->dev, "GPIO registration failed: %d\n", ret); |
| 519 | goto err_gpio_init; |
| 520 | } |
| 521 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 522 | ret = tps6586x_add_subdevs(tps6586x, pdata); |
| 523 | if (ret) { |
| 524 | dev_err(&client->dev, "add devices failed: %d\n", ret); |
| 525 | goto err_add_devs; |
| 526 | } |
| 527 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 528 | return 0; |
| 529 | |
| 530 | err_add_devs: |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 531 | if (pdata->gpio_base) { |
| 532 | ret = gpiochip_remove(&tps6586x->gpio); |
| 533 | if (ret) |
| 534 | dev_err(&client->dev, "Can't remove gpio chip: %d\n", |
| 535 | ret); |
| 536 | } |
| 537 | err_gpio_init: |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 538 | if (client->irq) |
| 539 | free_irq(client->irq, tps6586x); |
| 540 | err_irq_init: |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 541 | kfree(tps6586x); |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static int __devexit tps6586x_i2c_remove(struct i2c_client *client) |
| 546 | { |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 547 | struct tps6586x *tps6586x = i2c_get_clientdata(client); |
| 548 | struct tps6586x_platform_data *pdata = client->dev.platform_data; |
| 549 | int ret; |
| 550 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 551 | if (client->irq) |
| 552 | free_irq(client->irq, tps6586x); |
| 553 | |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 554 | if (pdata->gpio_base) { |
| 555 | ret = gpiochip_remove(&tps6586x->gpio); |
| 556 | if (ret) |
| 557 | dev_err(&client->dev, "Can't remove gpio chip: %d\n", |
| 558 | ret); |
| 559 | } |
| 560 | |
| 561 | tps6586x_remove_subdevs(tps6586x); |
| 562 | kfree(tps6586x); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static const struct i2c_device_id tps6586x_id_table[] = { |
| 567 | { "tps6586x", 0 }, |
| 568 | { }, |
| 569 | }; |
| 570 | MODULE_DEVICE_TABLE(i2c, tps6586x_id_table); |
| 571 | |
| 572 | static struct i2c_driver tps6586x_driver = { |
| 573 | .driver = { |
| 574 | .name = "tps6586x", |
| 575 | .owner = THIS_MODULE, |
| 576 | }, |
| 577 | .probe = tps6586x_i2c_probe, |
| 578 | .remove = __devexit_p(tps6586x_i2c_remove), |
| 579 | .id_table = tps6586x_id_table, |
| 580 | }; |
| 581 | |
| 582 | static int __init tps6586x_init(void) |
| 583 | { |
| 584 | return i2c_add_driver(&tps6586x_driver); |
| 585 | } |
| 586 | subsys_initcall(tps6586x_init); |
| 587 | |
| 588 | static void __exit tps6586x_exit(void) |
| 589 | { |
| 590 | i2c_del_driver(&tps6586x_driver); |
| 591 | } |
| 592 | module_exit(tps6586x_exit); |
| 593 | |
| 594 | MODULE_DESCRIPTION("TPS6586X core driver"); |
| 595 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 596 | MODULE_LICENSE("GPL"); |