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