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> |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 24 | #include <linux/err.h> |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 25 | #include <linux/i2c.h> |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 26 | #include <linux/regmap.h> |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 27 | |
| 28 | #include <linux/mfd/core.h> |
| 29 | #include <linux/mfd/tps6586x.h> |
| 30 | |
Bill Huang | 004c15a | 2012-08-19 18:07:55 -0700 | [diff] [blame] | 31 | #define TPS6586X_SUPPLYENE 0x14 |
| 32 | #define EXITSLREQ_BIT BIT(1) |
| 33 | #define SLEEP_MODE_BIT BIT(3) |
| 34 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 35 | /* interrupt control registers */ |
| 36 | #define TPS6586X_INT_ACK1 0xb5 |
| 37 | #define TPS6586X_INT_ACK2 0xb6 |
| 38 | #define TPS6586X_INT_ACK3 0xb7 |
| 39 | #define TPS6586X_INT_ACK4 0xb8 |
| 40 | |
| 41 | /* interrupt mask registers */ |
| 42 | #define TPS6586X_INT_MASK1 0xb0 |
| 43 | #define TPS6586X_INT_MASK2 0xb1 |
| 44 | #define TPS6586X_INT_MASK3 0xb2 |
| 45 | #define TPS6586X_INT_MASK4 0xb3 |
| 46 | #define TPS6586X_INT_MASK5 0xb4 |
| 47 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 48 | /* device id */ |
| 49 | #define TPS6586X_VERSIONCRC 0xcd |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 50 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 51 | /* Maximum register */ |
| 52 | #define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1) |
| 53 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 54 | struct tps6586x_irq_data { |
| 55 | u8 mask_reg; |
| 56 | u8 mask_mask; |
| 57 | }; |
| 58 | |
| 59 | #define TPS6586X_IRQ(_reg, _mask) \ |
| 60 | { \ |
| 61 | .mask_reg = (_reg) - TPS6586X_INT_MASK1, \ |
| 62 | .mask_mask = (_mask), \ |
| 63 | } |
| 64 | |
| 65 | static const struct tps6586x_irq_data tps6586x_irqs[] = { |
| 66 | [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0), |
| 67 | [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1), |
| 68 | [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2), |
| 69 | [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3), |
| 70 | [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4), |
| 71 | [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5), |
| 72 | [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6), |
| 73 | [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7), |
| 74 | [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0), |
| 75 | [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1), |
| 76 | [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2), |
| 77 | [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3), |
| 78 | [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4), |
| 79 | [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5), |
| 80 | [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6), |
| 81 | [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7), |
| 82 | [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4), |
| 83 | [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03), |
| 84 | [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2), |
| 85 | [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3), |
| 86 | [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0), |
| 87 | [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc), |
| 88 | [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06), |
| 89 | [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0), |
| 90 | [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5), |
| 91 | [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6), |
| 92 | [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1), |
| 93 | }; |
| 94 | |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 95 | static struct mfd_cell tps6586x_cell[] = { |
| 96 | { |
| 97 | .name = "tps6586x-gpio", |
| 98 | }, |
| 99 | { |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame^] | 100 | .name = "tps6586x-pmic", |
| 101 | }, |
| 102 | { |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 103 | .name = "tps6586x-rtc", |
| 104 | }, |
| 105 | { |
| 106 | .name = "tps6586x-onkey", |
| 107 | }, |
| 108 | }; |
| 109 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 110 | struct tps6586x { |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 111 | struct device *dev; |
| 112 | struct i2c_client *client; |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 113 | struct regmap *regmap; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 114 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 115 | struct irq_chip irq_chip; |
| 116 | struct mutex irq_lock; |
| 117 | int irq_base; |
| 118 | u32 irq_en; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 119 | u8 mask_reg[5]; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 122 | static inline struct tps6586x *dev_to_tps6586x(struct device *dev) |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 123 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 124 | return i2c_get_clientdata(to_i2c_client(dev)); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int tps6586x_write(struct device *dev, int reg, uint8_t val) |
| 128 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 129 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 130 | |
| 131 | return regmap_write(tps6586x->regmap, reg, val); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 132 | } |
| 133 | EXPORT_SYMBOL_GPL(tps6586x_write); |
| 134 | |
| 135 | int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val) |
| 136 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 137 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 138 | |
| 139 | return regmap_bulk_write(tps6586x->regmap, reg, val, len); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 140 | } |
| 141 | EXPORT_SYMBOL_GPL(tps6586x_writes); |
| 142 | |
| 143 | int tps6586x_read(struct device *dev, int reg, uint8_t *val) |
| 144 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 145 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 146 | unsigned int rval; |
| 147 | int ret; |
| 148 | |
| 149 | ret = regmap_read(tps6586x->regmap, reg, &rval); |
| 150 | if (!ret) |
| 151 | *val = rval; |
| 152 | return ret; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 153 | } |
| 154 | EXPORT_SYMBOL_GPL(tps6586x_read); |
| 155 | |
| 156 | int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val) |
| 157 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 158 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 159 | |
| 160 | return regmap_bulk_read(tps6586x->regmap, reg, val, len); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 161 | } |
| 162 | EXPORT_SYMBOL_GPL(tps6586x_reads); |
| 163 | |
| 164 | int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 165 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 166 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 167 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 168 | return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 169 | } |
| 170 | EXPORT_SYMBOL_GPL(tps6586x_set_bits); |
| 171 | |
| 172 | int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 173 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 174 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 175 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 176 | return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 177 | } |
| 178 | EXPORT_SYMBOL_GPL(tps6586x_clr_bits); |
| 179 | |
| 180 | int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask) |
| 181 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 182 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 183 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 184 | return regmap_update_bits(tps6586x->regmap, reg, mask, val); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 185 | } |
| 186 | EXPORT_SYMBOL_GPL(tps6586x_update); |
| 187 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 188 | static int __remove_subdev(struct device *dev, void *unused) |
| 189 | { |
| 190 | platform_device_unregister(to_platform_device(dev)); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int tps6586x_remove_subdevs(struct tps6586x *tps6586x) |
| 195 | { |
| 196 | return device_for_each_child(tps6586x->dev, NULL, __remove_subdev); |
| 197 | } |
| 198 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 199 | static void tps6586x_irq_lock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 200 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 201 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 202 | |
| 203 | mutex_lock(&tps6586x->irq_lock); |
| 204 | } |
| 205 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 206 | static void tps6586x_irq_enable(struct irq_data *irq_data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 207 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 208 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data); |
| 209 | unsigned int __irq = irq_data->irq - tps6586x->irq_base; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 210 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 211 | |
| 212 | tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask; |
| 213 | tps6586x->irq_en |= (1 << __irq); |
| 214 | } |
| 215 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 216 | static void tps6586x_irq_disable(struct irq_data *irq_data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 217 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 218 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 219 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 220 | unsigned int __irq = irq_data->irq - tps6586x->irq_base; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 221 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 222 | |
| 223 | tps6586x->mask_reg[data->mask_reg] |= data->mask_mask; |
| 224 | tps6586x->irq_en &= ~(1 << __irq); |
| 225 | } |
| 226 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 227 | static void tps6586x_irq_sync_unlock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 228 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 229 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 230 | int i; |
| 231 | |
| 232 | for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) { |
Laxman Dewangan | 75edd5a | 2012-07-18 11:50:47 +0530 | [diff] [blame] | 233 | int ret; |
| 234 | ret = tps6586x_write(tps6586x->dev, |
| 235 | TPS6586X_INT_MASK1 + i, |
| 236 | tps6586x->mask_reg[i]); |
| 237 | WARN_ON(ret); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | mutex_unlock(&tps6586x->irq_lock); |
| 241 | } |
| 242 | |
| 243 | static irqreturn_t tps6586x_irq(int irq, void *data) |
| 244 | { |
| 245 | struct tps6586x *tps6586x = data; |
| 246 | u32 acks; |
| 247 | int ret = 0; |
| 248 | |
| 249 | ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, |
| 250 | sizeof(acks), (uint8_t *)&acks); |
| 251 | |
| 252 | if (ret < 0) { |
| 253 | dev_err(tps6586x->dev, "failed to read interrupt status\n"); |
| 254 | return IRQ_NONE; |
| 255 | } |
| 256 | |
| 257 | acks = le32_to_cpu(acks); |
| 258 | |
| 259 | while (acks) { |
| 260 | int i = __ffs(acks); |
| 261 | |
| 262 | if (tps6586x->irq_en & (1 << i)) |
| 263 | handle_nested_irq(tps6586x->irq_base + i); |
| 264 | |
| 265 | acks &= ~(1 << i); |
| 266 | } |
| 267 | |
| 268 | return IRQ_HANDLED; |
| 269 | } |
| 270 | |
| 271 | static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq, |
| 272 | int irq_base) |
| 273 | { |
| 274 | int i, ret; |
| 275 | u8 tmp[4]; |
| 276 | |
| 277 | if (!irq_base) { |
| 278 | dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n"); |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | mutex_init(&tps6586x->irq_lock); |
| 283 | for (i = 0; i < 5; i++) { |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 284 | tps6586x->mask_reg[i] = 0xff; |
| 285 | tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff); |
| 286 | } |
| 287 | |
| 288 | tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp); |
| 289 | |
| 290 | tps6586x->irq_base = irq_base; |
| 291 | |
| 292 | tps6586x->irq_chip.name = "tps6586x"; |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 293 | tps6586x->irq_chip.irq_enable = tps6586x_irq_enable; |
| 294 | tps6586x->irq_chip.irq_disable = tps6586x_irq_disable; |
| 295 | tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock; |
| 296 | tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 297 | |
| 298 | for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) { |
| 299 | int __irq = i + tps6586x->irq_base; |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 300 | irq_set_chip_data(__irq, tps6586x); |
| 301 | irq_set_chip_and_handler(__irq, &tps6586x->irq_chip, |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 302 | handle_simple_irq); |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 303 | irq_set_nested_thread(__irq, 1); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 304 | #ifdef CONFIG_ARM |
| 305 | set_irq_flags(__irq, IRQF_VALID); |
| 306 | #endif |
| 307 | } |
| 308 | |
| 309 | ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT, |
| 310 | "tps6586x", tps6586x); |
| 311 | |
| 312 | if (!ret) { |
| 313 | device_init_wakeup(tps6586x->dev, 1); |
| 314 | enable_irq_wake(irq); |
| 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 320 | static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x, |
| 321 | struct tps6586x_platform_data *pdata) |
| 322 | { |
| 323 | struct tps6586x_subdev_info *subdev; |
| 324 | struct platform_device *pdev; |
| 325 | int i, ret = 0; |
| 326 | |
| 327 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 328 | subdev = &pdata->subdevs[i]; |
| 329 | |
| 330 | pdev = platform_device_alloc(subdev->name, subdev->id); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 331 | if (!pdev) { |
| 332 | ret = -ENOMEM; |
| 333 | goto failed; |
| 334 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 335 | |
| 336 | pdev->dev.parent = tps6586x->dev; |
| 337 | pdev->dev.platform_data = subdev->platform_data; |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 338 | pdev->dev.of_node = subdev->of_node; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 339 | |
| 340 | ret = platform_device_add(pdev); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 341 | if (ret) { |
| 342 | platform_device_put(pdev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 343 | goto failed; |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 344 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 345 | } |
| 346 | return 0; |
| 347 | |
| 348 | failed: |
| 349 | tps6586x_remove_subdevs(tps6586x); |
| 350 | return ret; |
| 351 | } |
| 352 | |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 353 | #ifdef CONFIG_OF |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 354 | static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client) |
| 355 | { |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 356 | struct device_node *np = client->dev.of_node; |
| 357 | struct tps6586x_platform_data *pdata; |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 358 | |
| 359 | pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame^] | 360 | if (!pdata) { |
| 361 | dev_err(&client->dev, "Memory allocation failed\n"); |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 362 | return NULL; |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame^] | 363 | } |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 364 | |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame^] | 365 | pdata->num_subdevs = 0; |
| 366 | pdata->subdevs = NULL; |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 367 | pdata->gpio_base = -1; |
| 368 | pdata->irq_base = -1; |
Bill Huang | 004c15a | 2012-08-19 18:07:55 -0700 | [diff] [blame] | 369 | pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller"); |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 370 | |
| 371 | return pdata; |
| 372 | } |
| 373 | |
| 374 | static struct of_device_id tps6586x_of_match[] = { |
| 375 | { .compatible = "ti,tps6586x", }, |
| 376 | { }, |
| 377 | }; |
| 378 | #else |
| 379 | static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client) |
| 380 | { |
| 381 | return NULL; |
| 382 | } |
| 383 | #endif |
| 384 | |
Laxman Dewangan | 75edd5a | 2012-07-18 11:50:47 +0530 | [diff] [blame] | 385 | static bool is_volatile_reg(struct device *dev, unsigned int reg) |
| 386 | { |
| 387 | /* Cache all interrupt mask register */ |
| 388 | if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5)) |
| 389 | return false; |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 394 | static const struct regmap_config tps6586x_regmap_config = { |
| 395 | .reg_bits = 8, |
| 396 | .val_bits = 8, |
| 397 | .max_register = TPS6586X_MAX_REGISTER - 1, |
Laxman Dewangan | 75edd5a | 2012-07-18 11:50:47 +0530 | [diff] [blame] | 398 | .volatile_reg = is_volatile_reg, |
| 399 | .cache_type = REGCACHE_RBTREE, |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 400 | }; |
| 401 | |
Bill Huang | 004c15a | 2012-08-19 18:07:55 -0700 | [diff] [blame] | 402 | static struct device *tps6586x_dev; |
| 403 | static void tps6586x_power_off(void) |
| 404 | { |
| 405 | if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT)) |
| 406 | return; |
| 407 | |
| 408 | tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT); |
| 409 | } |
| 410 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 411 | static int __devinit tps6586x_i2c_probe(struct i2c_client *client, |
| 412 | const struct i2c_device_id *id) |
| 413 | { |
| 414 | struct tps6586x_platform_data *pdata = client->dev.platform_data; |
| 415 | struct tps6586x *tps6586x; |
| 416 | int ret; |
| 417 | |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 418 | if (!pdata && client->dev.of_node) |
| 419 | pdata = tps6586x_parse_dt(client); |
| 420 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 421 | if (!pdata) { |
| 422 | dev_err(&client->dev, "tps6586x requires platform data\n"); |
| 423 | return -ENOTSUPP; |
| 424 | } |
| 425 | |
| 426 | ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC); |
| 427 | if (ret < 0) { |
| 428 | dev_err(&client->dev, "Chip ID read failed: %d\n", ret); |
| 429 | return -EIO; |
| 430 | } |
| 431 | |
Stephen Warren | 4d1cdbf | 2010-12-09 10:30:11 -0700 | [diff] [blame] | 432 | dev_info(&client->dev, "VERSIONCRC is %02x\n", ret); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 433 | |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 434 | tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL); |
| 435 | if (tps6586x == NULL) { |
| 436 | dev_err(&client->dev, "memory for tps6586x alloc failed\n"); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 437 | return -ENOMEM; |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 438 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 439 | |
| 440 | tps6586x->client = client; |
| 441 | tps6586x->dev = &client->dev; |
| 442 | i2c_set_clientdata(client, tps6586x); |
| 443 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 444 | tps6586x->regmap = devm_regmap_init_i2c(client, |
| 445 | &tps6586x_regmap_config); |
| 446 | if (IS_ERR(tps6586x->regmap)) { |
| 447 | ret = PTR_ERR(tps6586x->regmap); |
| 448 | dev_err(&client->dev, "regmap init failed: %d\n", ret); |
| 449 | return ret; |
| 450 | } |
| 451 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 452 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 453 | if (client->irq) { |
| 454 | ret = tps6586x_irq_init(tps6586x, client->irq, |
| 455 | pdata->irq_base); |
| 456 | if (ret) { |
| 457 | dev_err(&client->dev, "IRQ init failed: %d\n", ret); |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 458 | return ret; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 462 | ret = mfd_add_devices(tps6586x->dev, -1, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 463 | tps6586x_cell, ARRAY_SIZE(tps6586x_cell), |
| 464 | NULL, 0, NULL); |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 465 | if (ret < 0) { |
| 466 | dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret); |
| 467 | goto err_mfd_add; |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 468 | } |
| 469 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 470 | ret = tps6586x_add_subdevs(tps6586x, pdata); |
| 471 | if (ret) { |
| 472 | dev_err(&client->dev, "add devices failed: %d\n", ret); |
| 473 | goto err_add_devs; |
| 474 | } |
| 475 | |
Bill Huang | 004c15a | 2012-08-19 18:07:55 -0700 | [diff] [blame] | 476 | if (pdata->pm_off && !pm_power_off) { |
| 477 | tps6586x_dev = &client->dev; |
| 478 | pm_power_off = tps6586x_power_off; |
| 479 | } |
| 480 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 481 | return 0; |
| 482 | |
| 483 | err_add_devs: |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 484 | mfd_remove_devices(tps6586x->dev); |
| 485 | err_mfd_add: |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 486 | if (client->irq) |
| 487 | free_irq(client->irq, tps6586x); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | static int __devexit tps6586x_i2c_remove(struct i2c_client *client) |
| 492 | { |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 493 | struct tps6586x *tps6586x = i2c_get_clientdata(client); |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 494 | |
| 495 | tps6586x_remove_subdevs(tps6586x); |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 496 | mfd_remove_devices(tps6586x->dev); |
| 497 | if (client->irq) |
| 498 | free_irq(client->irq, tps6586x); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static const struct i2c_device_id tps6586x_id_table[] = { |
| 503 | { "tps6586x", 0 }, |
| 504 | { }, |
| 505 | }; |
| 506 | MODULE_DEVICE_TABLE(i2c, tps6586x_id_table); |
| 507 | |
| 508 | static struct i2c_driver tps6586x_driver = { |
| 509 | .driver = { |
| 510 | .name = "tps6586x", |
| 511 | .owner = THIS_MODULE, |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 512 | .of_match_table = of_match_ptr(tps6586x_of_match), |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 513 | }, |
| 514 | .probe = tps6586x_i2c_probe, |
| 515 | .remove = __devexit_p(tps6586x_i2c_remove), |
| 516 | .id_table = tps6586x_id_table, |
| 517 | }; |
| 518 | |
| 519 | static int __init tps6586x_init(void) |
| 520 | { |
| 521 | return i2c_add_driver(&tps6586x_driver); |
| 522 | } |
| 523 | subsys_initcall(tps6586x_init); |
| 524 | |
| 525 | static void __exit tps6586x_exit(void) |
| 526 | { |
| 527 | i2c_del_driver(&tps6586x_driver); |
| 528 | } |
| 529 | module_exit(tps6586x_exit); |
| 530 | |
| 531 | MODULE_DESCRIPTION("TPS6586X core driver"); |
| 532 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 533 | MODULE_LICENSE("GPL"); |