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> |
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 | 5b8b1fe | 2012-11-13 19:18:06 +0530 | [diff] [blame] | 97 | static struct resource tps6586x_rtc_resources[] = { |
| 98 | { |
| 99 | .start = TPS6586X_INT_RTC_ALM1, |
| 100 | .end = TPS6586X_INT_RTC_ALM1, |
| 101 | .flags = IORESOURCE_IRQ, |
| 102 | }, |
| 103 | }; |
| 104 | |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 105 | static struct mfd_cell tps6586x_cell[] = { |
| 106 | { |
| 107 | .name = "tps6586x-gpio", |
| 108 | }, |
| 109 | { |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame] | 110 | .name = "tps6586x-pmic", |
| 111 | }, |
| 112 | { |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 113 | .name = "tps6586x-rtc", |
Laxman Dewangan | 5b8b1fe | 2012-11-13 19:18:06 +0530 | [diff] [blame] | 114 | .num_resources = ARRAY_SIZE(tps6586x_rtc_resources), |
| 115 | .resources = &tps6586x_rtc_resources[0], |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 116 | }, |
| 117 | { |
| 118 | .name = "tps6586x-onkey", |
| 119 | }, |
| 120 | }; |
| 121 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 122 | struct tps6586x { |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 123 | struct device *dev; |
| 124 | struct i2c_client *client; |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 125 | struct regmap *regmap; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 126 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 127 | struct irq_chip irq_chip; |
| 128 | struct mutex irq_lock; |
| 129 | int irq_base; |
| 130 | u32 irq_en; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 131 | u8 mask_reg[5]; |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 132 | struct irq_domain *irq_domain; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 135 | static inline struct tps6586x *dev_to_tps6586x(struct device *dev) |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 136 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 137 | return i2c_get_clientdata(to_i2c_client(dev)); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | int tps6586x_write(struct device *dev, int reg, uint8_t val) |
| 141 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 142 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 143 | |
| 144 | return regmap_write(tps6586x->regmap, reg, val); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 145 | } |
| 146 | EXPORT_SYMBOL_GPL(tps6586x_write); |
| 147 | |
| 148 | int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val) |
| 149 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 150 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 151 | |
| 152 | return regmap_bulk_write(tps6586x->regmap, reg, val, len); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 153 | } |
| 154 | EXPORT_SYMBOL_GPL(tps6586x_writes); |
| 155 | |
| 156 | int tps6586x_read(struct device *dev, int reg, 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 | unsigned int rval; |
| 160 | int ret; |
| 161 | |
| 162 | ret = regmap_read(tps6586x->regmap, reg, &rval); |
| 163 | if (!ret) |
| 164 | *val = rval; |
| 165 | return ret; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 166 | } |
| 167 | EXPORT_SYMBOL_GPL(tps6586x_read); |
| 168 | |
| 169 | int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val) |
| 170 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 171 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 172 | |
| 173 | return regmap_bulk_read(tps6586x->regmap, reg, val, len); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 174 | } |
| 175 | EXPORT_SYMBOL_GPL(tps6586x_reads); |
| 176 | |
| 177 | int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 178 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 179 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 180 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 181 | return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 182 | } |
| 183 | EXPORT_SYMBOL_GPL(tps6586x_set_bits); |
| 184 | |
| 185 | int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 186 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 187 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 188 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 189 | return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 190 | } |
| 191 | EXPORT_SYMBOL_GPL(tps6586x_clr_bits); |
| 192 | |
| 193 | int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask) |
| 194 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 195 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 196 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 197 | return regmap_update_bits(tps6586x->regmap, reg, mask, val); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 198 | } |
| 199 | EXPORT_SYMBOL_GPL(tps6586x_update); |
| 200 | |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 201 | int tps6586x_irq_get_virq(struct device *dev, int irq) |
| 202 | { |
| 203 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 204 | |
| 205 | return irq_create_mapping(tps6586x->irq_domain, irq); |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(tps6586x_irq_get_virq); |
| 208 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 209 | static int __remove_subdev(struct device *dev, void *unused) |
| 210 | { |
| 211 | platform_device_unregister(to_platform_device(dev)); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int tps6586x_remove_subdevs(struct tps6586x *tps6586x) |
| 216 | { |
| 217 | return device_for_each_child(tps6586x->dev, NULL, __remove_subdev); |
| 218 | } |
| 219 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 220 | static void tps6586x_irq_lock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 221 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 222 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 223 | |
| 224 | mutex_lock(&tps6586x->irq_lock); |
| 225 | } |
| 226 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 227 | static void tps6586x_irq_enable(struct irq_data *irq_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(irq_data); |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 230 | unsigned int __irq = irq_data->hwirq; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 231 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 232 | |
| 233 | tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask; |
| 234 | tps6586x->irq_en |= (1 << __irq); |
| 235 | } |
| 236 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 237 | static void tps6586x_irq_disable(struct irq_data *irq_data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 238 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 239 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 240 | |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 241 | unsigned int __irq = irq_data->hwirq; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 242 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 243 | |
| 244 | tps6586x->mask_reg[data->mask_reg] |= data->mask_mask; |
| 245 | tps6586x->irq_en &= ~(1 << __irq); |
| 246 | } |
| 247 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 248 | static void tps6586x_irq_sync_unlock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 249 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 250 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 251 | int i; |
| 252 | |
| 253 | for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) { |
Laxman Dewangan | 75edd5a | 2012-07-18 11:50:47 +0530 | [diff] [blame] | 254 | int ret; |
| 255 | ret = tps6586x_write(tps6586x->dev, |
| 256 | TPS6586X_INT_MASK1 + i, |
| 257 | tps6586x->mask_reg[i]); |
| 258 | WARN_ON(ret); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | mutex_unlock(&tps6586x->irq_lock); |
| 262 | } |
| 263 | |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 264 | static struct irq_chip tps6586x_irq_chip = { |
| 265 | .name = "tps6586x", |
| 266 | .irq_bus_lock = tps6586x_irq_lock, |
| 267 | .irq_bus_sync_unlock = tps6586x_irq_sync_unlock, |
| 268 | .irq_disable = tps6586x_irq_disable, |
| 269 | .irq_enable = tps6586x_irq_enable, |
| 270 | }; |
| 271 | |
| 272 | static int tps6586x_irq_map(struct irq_domain *h, unsigned int virq, |
| 273 | irq_hw_number_t hw) |
| 274 | { |
| 275 | struct tps6586x *tps6586x = h->host_data; |
| 276 | |
| 277 | irq_set_chip_data(virq, tps6586x); |
| 278 | irq_set_chip_and_handler(virq, &tps6586x_irq_chip, handle_simple_irq); |
| 279 | irq_set_nested_thread(virq, 1); |
| 280 | |
| 281 | /* ARM needs us to explicitly flag the IRQ as valid |
| 282 | * and will set them noprobe when we do so. */ |
| 283 | #ifdef CONFIG_ARM |
| 284 | set_irq_flags(virq, IRQF_VALID); |
| 285 | #else |
| 286 | irq_set_noprobe(virq); |
| 287 | #endif |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static struct irq_domain_ops tps6586x_domain_ops = { |
| 293 | .map = tps6586x_irq_map, |
| 294 | .xlate = irq_domain_xlate_twocell, |
| 295 | }; |
| 296 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 297 | static irqreturn_t tps6586x_irq(int irq, void *data) |
| 298 | { |
| 299 | struct tps6586x *tps6586x = data; |
| 300 | u32 acks; |
| 301 | int ret = 0; |
| 302 | |
| 303 | ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, |
| 304 | sizeof(acks), (uint8_t *)&acks); |
| 305 | |
| 306 | if (ret < 0) { |
| 307 | dev_err(tps6586x->dev, "failed to read interrupt status\n"); |
| 308 | return IRQ_NONE; |
| 309 | } |
| 310 | |
| 311 | acks = le32_to_cpu(acks); |
| 312 | |
| 313 | while (acks) { |
| 314 | int i = __ffs(acks); |
| 315 | |
| 316 | if (tps6586x->irq_en & (1 << i)) |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 317 | handle_nested_irq( |
| 318 | irq_find_mapping(tps6586x->irq_domain, i)); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 319 | |
| 320 | acks &= ~(1 << i); |
| 321 | } |
| 322 | |
| 323 | return IRQ_HANDLED; |
| 324 | } |
| 325 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 326 | static int tps6586x_irq_init(struct tps6586x *tps6586x, int irq, |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 327 | int irq_base) |
| 328 | { |
| 329 | int i, ret; |
| 330 | u8 tmp[4]; |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 331 | int new_irq_base; |
| 332 | int irq_num = ARRAY_SIZE(tps6586x_irqs); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 333 | |
| 334 | mutex_init(&tps6586x->irq_lock); |
| 335 | for (i = 0; i < 5; i++) { |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 336 | tps6586x->mask_reg[i] = 0xff; |
| 337 | tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff); |
| 338 | } |
| 339 | |
| 340 | tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp); |
| 341 | |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 342 | if (irq_base > 0) { |
| 343 | new_irq_base = irq_alloc_descs(irq_base, 0, irq_num, -1); |
| 344 | if (new_irq_base < 0) { |
| 345 | dev_err(tps6586x->dev, |
| 346 | "Failed to alloc IRQs: %d\n", new_irq_base); |
| 347 | return new_irq_base; |
| 348 | } |
| 349 | } else { |
| 350 | new_irq_base = 0; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Laxman Dewangan | 605511a | 2012-11-13 19:18:05 +0530 | [diff] [blame] | 353 | tps6586x->irq_domain = irq_domain_add_simple(tps6586x->dev->of_node, |
| 354 | irq_num, new_irq_base, &tps6586x_domain_ops, |
| 355 | tps6586x); |
| 356 | if (!tps6586x->irq_domain) { |
| 357 | dev_err(tps6586x->dev, "Failed to create IRQ domain\n"); |
| 358 | return -ENOMEM; |
| 359 | } |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 360 | ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT, |
| 361 | "tps6586x", tps6586x); |
| 362 | |
| 363 | if (!ret) { |
| 364 | device_init_wakeup(tps6586x->dev, 1); |
| 365 | enable_irq_wake(irq); |
| 366 | } |
| 367 | |
| 368 | return ret; |
| 369 | } |
| 370 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 371 | static int tps6586x_add_subdevs(struct tps6586x *tps6586x, |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 372 | struct tps6586x_platform_data *pdata) |
| 373 | { |
| 374 | struct tps6586x_subdev_info *subdev; |
| 375 | struct platform_device *pdev; |
| 376 | int i, ret = 0; |
| 377 | |
| 378 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 379 | subdev = &pdata->subdevs[i]; |
| 380 | |
| 381 | pdev = platform_device_alloc(subdev->name, subdev->id); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 382 | if (!pdev) { |
| 383 | ret = -ENOMEM; |
| 384 | goto failed; |
| 385 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 386 | |
| 387 | pdev->dev.parent = tps6586x->dev; |
| 388 | pdev->dev.platform_data = subdev->platform_data; |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 389 | pdev->dev.of_node = subdev->of_node; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 390 | |
| 391 | ret = platform_device_add(pdev); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 392 | if (ret) { |
| 393 | platform_device_put(pdev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 394 | goto failed; |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 395 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 396 | } |
| 397 | return 0; |
| 398 | |
| 399 | failed: |
| 400 | tps6586x_remove_subdevs(tps6586x); |
| 401 | return ret; |
| 402 | } |
| 403 | |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 404 | #ifdef CONFIG_OF |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 405 | static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client) |
| 406 | { |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 407 | struct device_node *np = client->dev.of_node; |
| 408 | struct tps6586x_platform_data *pdata; |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 409 | |
| 410 | pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame] | 411 | if (!pdata) { |
| 412 | dev_err(&client->dev, "Memory allocation failed\n"); |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 413 | return NULL; |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame] | 414 | } |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 415 | |
Laxman Dewangan | 64e4816 | 2012-10-18 19:36:09 +0530 | [diff] [blame] | 416 | pdata->num_subdevs = 0; |
| 417 | pdata->subdevs = NULL; |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 418 | pdata->gpio_base = -1; |
| 419 | pdata->irq_base = -1; |
Bill Huang | 004c15a | 2012-08-19 18:07:55 -0700 | [diff] [blame] | 420 | pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller"); |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 421 | |
| 422 | return pdata; |
| 423 | } |
| 424 | |
| 425 | static struct of_device_id tps6586x_of_match[] = { |
| 426 | { .compatible = "ti,tps6586x", }, |
| 427 | { }, |
| 428 | }; |
| 429 | #else |
| 430 | static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client) |
| 431 | { |
| 432 | return NULL; |
| 433 | } |
| 434 | #endif |
| 435 | |
Laxman Dewangan | 75edd5a | 2012-07-18 11:50:47 +0530 | [diff] [blame] | 436 | static bool is_volatile_reg(struct device *dev, unsigned int reg) |
| 437 | { |
| 438 | /* Cache all interrupt mask register */ |
| 439 | if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5)) |
| 440 | return false; |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 445 | static const struct regmap_config tps6586x_regmap_config = { |
| 446 | .reg_bits = 8, |
| 447 | .val_bits = 8, |
| 448 | .max_register = TPS6586X_MAX_REGISTER - 1, |
Laxman Dewangan | 75edd5a | 2012-07-18 11:50:47 +0530 | [diff] [blame] | 449 | .volatile_reg = is_volatile_reg, |
| 450 | .cache_type = REGCACHE_RBTREE, |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 451 | }; |
| 452 | |
Bill Huang | 004c15a | 2012-08-19 18:07:55 -0700 | [diff] [blame] | 453 | static struct device *tps6586x_dev; |
| 454 | static void tps6586x_power_off(void) |
| 455 | { |
| 456 | if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT)) |
| 457 | return; |
| 458 | |
| 459 | tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT); |
| 460 | } |
| 461 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 462 | static int tps6586x_i2c_probe(struct i2c_client *client, |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 463 | const struct i2c_device_id *id) |
| 464 | { |
| 465 | struct tps6586x_platform_data *pdata = client->dev.platform_data; |
| 466 | struct tps6586x *tps6586x; |
| 467 | int ret; |
| 468 | |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 469 | if (!pdata && client->dev.of_node) |
| 470 | pdata = tps6586x_parse_dt(client); |
| 471 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 472 | if (!pdata) { |
| 473 | dev_err(&client->dev, "tps6586x requires platform data\n"); |
| 474 | return -ENOTSUPP; |
| 475 | } |
| 476 | |
| 477 | ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC); |
| 478 | if (ret < 0) { |
| 479 | dev_err(&client->dev, "Chip ID read failed: %d\n", ret); |
| 480 | return -EIO; |
| 481 | } |
| 482 | |
Stephen Warren | 4d1cdbf | 2010-12-09 10:30:11 -0700 | [diff] [blame] | 483 | dev_info(&client->dev, "VERSIONCRC is %02x\n", ret); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 484 | |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 485 | tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL); |
| 486 | if (tps6586x == NULL) { |
| 487 | dev_err(&client->dev, "memory for tps6586x alloc failed\n"); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 488 | return -ENOMEM; |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 489 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 490 | |
| 491 | tps6586x->client = client; |
| 492 | tps6586x->dev = &client->dev; |
| 493 | i2c_set_clientdata(client, tps6586x); |
| 494 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame] | 495 | tps6586x->regmap = devm_regmap_init_i2c(client, |
| 496 | &tps6586x_regmap_config); |
| 497 | if (IS_ERR(tps6586x->regmap)) { |
| 498 | ret = PTR_ERR(tps6586x->regmap); |
| 499 | dev_err(&client->dev, "regmap init failed: %d\n", ret); |
| 500 | return ret; |
| 501 | } |
| 502 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 503 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 504 | if (client->irq) { |
| 505 | ret = tps6586x_irq_init(tps6586x, client->irq, |
| 506 | pdata->irq_base); |
| 507 | if (ret) { |
| 508 | dev_err(&client->dev, "IRQ init failed: %d\n", ret); |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 509 | return ret; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 513 | ret = mfd_add_devices(tps6586x->dev, -1, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 514 | tps6586x_cell, ARRAY_SIZE(tps6586x_cell), |
Laxman Dewangan | 5b8b1fe | 2012-11-13 19:18:06 +0530 | [diff] [blame] | 515 | NULL, 0, tps6586x->irq_domain); |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 516 | if (ret < 0) { |
| 517 | dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret); |
| 518 | goto err_mfd_add; |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 521 | ret = tps6586x_add_subdevs(tps6586x, pdata); |
| 522 | if (ret) { |
| 523 | dev_err(&client->dev, "add devices failed: %d\n", ret); |
| 524 | goto err_add_devs; |
| 525 | } |
| 526 | |
Bill Huang | 004c15a | 2012-08-19 18:07:55 -0700 | [diff] [blame] | 527 | if (pdata->pm_off && !pm_power_off) { |
| 528 | tps6586x_dev = &client->dev; |
| 529 | pm_power_off = tps6586x_power_off; |
| 530 | } |
| 531 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 532 | return 0; |
| 533 | |
| 534 | err_add_devs: |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 535 | mfd_remove_devices(tps6586x->dev); |
| 536 | err_mfd_add: |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 537 | if (client->irq) |
| 538 | free_irq(client->irq, tps6586x); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 539 | return ret; |
| 540 | } |
| 541 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 542 | static int tps6586x_i2c_remove(struct i2c_client *client) |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 543 | { |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 544 | struct tps6586x *tps6586x = i2c_get_clientdata(client); |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 545 | |
| 546 | tps6586x_remove_subdevs(tps6586x); |
Laxman Dewangan | 7a7487c | 2012-07-18 11:50:50 +0530 | [diff] [blame] | 547 | mfd_remove_devices(tps6586x->dev); |
| 548 | if (client->irq) |
| 549 | free_irq(client->irq, tps6586x); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static const struct i2c_device_id tps6586x_id_table[] = { |
| 554 | { "tps6586x", 0 }, |
| 555 | { }, |
| 556 | }; |
| 557 | MODULE_DEVICE_TABLE(i2c, tps6586x_id_table); |
| 558 | |
| 559 | static struct i2c_driver tps6586x_driver = { |
| 560 | .driver = { |
| 561 | .name = "tps6586x", |
| 562 | .owner = THIS_MODULE, |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 563 | .of_match_table = of_match_ptr(tps6586x_of_match), |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 564 | }, |
| 565 | .probe = tps6586x_i2c_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 566 | .remove = tps6586x_i2c_remove, |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 567 | .id_table = tps6586x_id_table, |
| 568 | }; |
| 569 | |
| 570 | static int __init tps6586x_init(void) |
| 571 | { |
| 572 | return i2c_add_driver(&tps6586x_driver); |
| 573 | } |
| 574 | subsys_initcall(tps6586x_init); |
| 575 | |
| 576 | static void __exit tps6586x_exit(void) |
| 577 | { |
| 578 | i2c_del_driver(&tps6586x_driver); |
| 579 | } |
| 580 | module_exit(tps6586x_exit); |
| 581 | |
| 582 | MODULE_DESCRIPTION("TPS6586X core driver"); |
| 583 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 584 | MODULE_LICENSE("GPL"); |