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