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