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