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