Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for TI TPS6586x PMIC family |
| 3 | * |
| 4 | * Copyright (c) 2010 CompuLab Ltd. |
| 5 | * Mike Rapoport <mike@compulab.co.il> |
| 6 | * |
| 7 | * Based on da903x.c. |
| 8 | * Copyright (C) 2008 Compulab, Ltd. |
| 9 | * Mike Rapoport <mike@compulab.co.il> |
| 10 | * Copyright (C) 2006-2008 Marvell International Ltd. |
| 11 | * Eric Miao <eric.miao@marvell.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | */ |
| 17 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/irq.h> |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/slab.h> |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 24 | #include <linux/err.h> |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 25 | #include <linux/gpio.h> |
| 26 | #include <linux/i2c.h> |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 27 | #include <linux/regmap.h> |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 28 | #include <linux/regulator/of_regulator.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 | |
| 33 | /* GPIO control registers */ |
| 34 | #define TPS6586X_GPIOSET1 0x5d |
| 35 | #define TPS6586X_GPIOSET2 0x5e |
| 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 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 97 | struct tps6586x { |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 98 | struct device *dev; |
| 99 | struct i2c_client *client; |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 100 | struct regmap *regmap; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 101 | |
| 102 | struct gpio_chip gpio; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 103 | struct irq_chip irq_chip; |
| 104 | struct mutex irq_lock; |
| 105 | int irq_base; |
| 106 | u32 irq_en; |
| 107 | u8 mask_cache[5]; |
| 108 | u8 mask_reg[5]; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 111 | static inline struct tps6586x *dev_to_tps6586x(struct device *dev) |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 112 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 113 | return i2c_get_clientdata(to_i2c_client(dev)); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int tps6586x_write(struct device *dev, int reg, uint8_t val) |
| 117 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 118 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 119 | |
| 120 | return regmap_write(tps6586x->regmap, reg, val); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 121 | } |
| 122 | EXPORT_SYMBOL_GPL(tps6586x_write); |
| 123 | |
| 124 | int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val) |
| 125 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 126 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 127 | |
| 128 | return regmap_bulk_write(tps6586x->regmap, reg, val, len); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 129 | } |
| 130 | EXPORT_SYMBOL_GPL(tps6586x_writes); |
| 131 | |
| 132 | int tps6586x_read(struct device *dev, int reg, uint8_t *val) |
| 133 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 134 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 135 | unsigned int rval; |
| 136 | int ret; |
| 137 | |
| 138 | ret = regmap_read(tps6586x->regmap, reg, &rval); |
| 139 | if (!ret) |
| 140 | *val = rval; |
| 141 | return ret; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 142 | } |
| 143 | EXPORT_SYMBOL_GPL(tps6586x_read); |
| 144 | |
| 145 | int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val) |
| 146 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 147 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
| 148 | |
| 149 | return regmap_bulk_read(tps6586x->regmap, reg, val, len); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 150 | } |
| 151 | EXPORT_SYMBOL_GPL(tps6586x_reads); |
| 152 | |
| 153 | int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 154 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 155 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 156 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 157 | return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 158 | } |
| 159 | EXPORT_SYMBOL_GPL(tps6586x_set_bits); |
| 160 | |
| 161 | int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask) |
| 162 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 163 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 164 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 165 | return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 166 | } |
| 167 | EXPORT_SYMBOL_GPL(tps6586x_clr_bits); |
| 168 | |
| 169 | int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask) |
| 170 | { |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 171 | struct tps6586x *tps6586x = dev_to_tps6586x(dev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 172 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 173 | return regmap_update_bits(tps6586x->regmap, reg, mask, val); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 174 | } |
| 175 | EXPORT_SYMBOL_GPL(tps6586x_update); |
| 176 | |
| 177 | static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 178 | { |
| 179 | struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio); |
| 180 | uint8_t val; |
| 181 | int ret; |
| 182 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 183 | ret = tps6586x_read(tps6586x->dev, TPS6586X_GPIOSET2, &val); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 184 | if (ret) |
| 185 | return ret; |
| 186 | |
| 187 | return !!(val & (1 << offset)); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static void tps6586x_gpio_set(struct gpio_chip *chip, unsigned offset, |
| 192 | int value) |
| 193 | { |
| 194 | struct tps6586x *tps6586x = container_of(chip, struct tps6586x, gpio); |
| 195 | |
Vincent Palatin | e6f1945 | 2011-04-12 10:55:58 -0400 | [diff] [blame] | 196 | tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET2, |
| 197 | value << offset, 1 << offset); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 201 | int value) |
| 202 | { |
| 203 | struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio); |
| 204 | uint8_t val, mask; |
| 205 | |
| 206 | tps6586x_gpio_set(gc, offset, value); |
| 207 | |
| 208 | val = 0x1 << (offset * 2); |
| 209 | mask = 0x3 << (offset * 2); |
| 210 | |
| 211 | return tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET1, val, mask); |
| 212 | } |
| 213 | |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 214 | static int tps6586x_gpio_init(struct tps6586x *tps6586x, int gpio_base) |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 215 | { |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 216 | if (!gpio_base) |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 217 | return 0; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 218 | |
| 219 | tps6586x->gpio.owner = THIS_MODULE; |
| 220 | tps6586x->gpio.label = tps6586x->client->name; |
| 221 | tps6586x->gpio.dev = tps6586x->dev; |
| 222 | tps6586x->gpio.base = gpio_base; |
| 223 | tps6586x->gpio.ngpio = 4; |
| 224 | tps6586x->gpio.can_sleep = 1; |
| 225 | |
| 226 | /* FIXME: add handling of GPIOs as dedicated inputs */ |
| 227 | tps6586x->gpio.direction_output = tps6586x_gpio_output; |
| 228 | tps6586x->gpio.set = tps6586x_gpio_set; |
| 229 | tps6586x->gpio.get = tps6586x_gpio_get; |
| 230 | |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 231 | return gpiochip_add(&tps6586x->gpio); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int __remove_subdev(struct device *dev, void *unused) |
| 235 | { |
| 236 | platform_device_unregister(to_platform_device(dev)); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int tps6586x_remove_subdevs(struct tps6586x *tps6586x) |
| 241 | { |
| 242 | return device_for_each_child(tps6586x->dev, NULL, __remove_subdev); |
| 243 | } |
| 244 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 245 | static void tps6586x_irq_lock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 246 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 247 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 248 | |
| 249 | mutex_lock(&tps6586x->irq_lock); |
| 250 | } |
| 251 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 252 | static void tps6586x_irq_enable(struct irq_data *irq_data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 253 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 254 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data); |
| 255 | unsigned int __irq = irq_data->irq - tps6586x->irq_base; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 256 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 257 | |
| 258 | tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask; |
| 259 | tps6586x->irq_en |= (1 << __irq); |
| 260 | } |
| 261 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 262 | static void tps6586x_irq_disable(struct irq_data *irq_data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 263 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 264 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 265 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 266 | unsigned int __irq = irq_data->irq - tps6586x->irq_base; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 267 | const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq]; |
| 268 | |
| 269 | tps6586x->mask_reg[data->mask_reg] |= data->mask_mask; |
| 270 | tps6586x->irq_en &= ~(1 << __irq); |
| 271 | } |
| 272 | |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 273 | static void tps6586x_irq_sync_unlock(struct irq_data *data) |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 274 | { |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 275 | struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 276 | int i; |
| 277 | |
| 278 | for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) { |
| 279 | if (tps6586x->mask_reg[i] != tps6586x->mask_cache[i]) { |
| 280 | if (!WARN_ON(tps6586x_write(tps6586x->dev, |
| 281 | TPS6586X_INT_MASK1 + i, |
| 282 | tps6586x->mask_reg[i]))) |
| 283 | tps6586x->mask_cache[i] = tps6586x->mask_reg[i]; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | mutex_unlock(&tps6586x->irq_lock); |
| 288 | } |
| 289 | |
| 290 | static irqreturn_t tps6586x_irq(int irq, void *data) |
| 291 | { |
| 292 | struct tps6586x *tps6586x = data; |
| 293 | u32 acks; |
| 294 | int ret = 0; |
| 295 | |
| 296 | ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, |
| 297 | sizeof(acks), (uint8_t *)&acks); |
| 298 | |
| 299 | if (ret < 0) { |
| 300 | dev_err(tps6586x->dev, "failed to read interrupt status\n"); |
| 301 | return IRQ_NONE; |
| 302 | } |
| 303 | |
| 304 | acks = le32_to_cpu(acks); |
| 305 | |
| 306 | while (acks) { |
| 307 | int i = __ffs(acks); |
| 308 | |
| 309 | if (tps6586x->irq_en & (1 << i)) |
| 310 | handle_nested_irq(tps6586x->irq_base + i); |
| 311 | |
| 312 | acks &= ~(1 << i); |
| 313 | } |
| 314 | |
| 315 | return IRQ_HANDLED; |
| 316 | } |
| 317 | |
| 318 | static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq, |
| 319 | int irq_base) |
| 320 | { |
| 321 | int i, ret; |
| 322 | u8 tmp[4]; |
| 323 | |
| 324 | if (!irq_base) { |
| 325 | dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n"); |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | |
| 329 | mutex_init(&tps6586x->irq_lock); |
| 330 | for (i = 0; i < 5; i++) { |
| 331 | tps6586x->mask_cache[i] = 0xff; |
| 332 | tps6586x->mask_reg[i] = 0xff; |
| 333 | tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff); |
| 334 | } |
| 335 | |
| 336 | tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp); |
| 337 | |
| 338 | tps6586x->irq_base = irq_base; |
| 339 | |
| 340 | tps6586x->irq_chip.name = "tps6586x"; |
Mark Brown | 96e824b | 2010-12-12 12:39:28 +0000 | [diff] [blame] | 341 | tps6586x->irq_chip.irq_enable = tps6586x_irq_enable; |
| 342 | tps6586x->irq_chip.irq_disable = tps6586x_irq_disable; |
| 343 | tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock; |
| 344 | tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 345 | |
| 346 | for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) { |
| 347 | int __irq = i + tps6586x->irq_base; |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 348 | irq_set_chip_data(__irq, tps6586x); |
| 349 | irq_set_chip_and_handler(__irq, &tps6586x->irq_chip, |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 350 | handle_simple_irq); |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 351 | irq_set_nested_thread(__irq, 1); |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 352 | #ifdef CONFIG_ARM |
| 353 | set_irq_flags(__irq, IRQF_VALID); |
| 354 | #endif |
| 355 | } |
| 356 | |
| 357 | ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT, |
| 358 | "tps6586x", tps6586x); |
| 359 | |
| 360 | if (!ret) { |
| 361 | device_init_wakeup(tps6586x->dev, 1); |
| 362 | enable_irq_wake(irq); |
| 363 | } |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 368 | static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x, |
| 369 | struct tps6586x_platform_data *pdata) |
| 370 | { |
| 371 | struct tps6586x_subdev_info *subdev; |
| 372 | struct platform_device *pdev; |
| 373 | int i, ret = 0; |
| 374 | |
| 375 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 376 | subdev = &pdata->subdevs[i]; |
| 377 | |
| 378 | pdev = platform_device_alloc(subdev->name, subdev->id); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 379 | if (!pdev) { |
| 380 | ret = -ENOMEM; |
| 381 | goto failed; |
| 382 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 383 | |
| 384 | pdev->dev.parent = tps6586x->dev; |
| 385 | pdev->dev.platform_data = subdev->platform_data; |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 386 | pdev->dev.of_node = subdev->of_node; |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 387 | |
| 388 | ret = platform_device_add(pdev); |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 389 | if (ret) { |
| 390 | platform_device_put(pdev); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 391 | goto failed; |
Axel Lin | 929980a | 2010-08-24 13:47:22 +0800 | [diff] [blame] | 392 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 393 | } |
| 394 | return 0; |
| 395 | |
| 396 | failed: |
| 397 | tps6586x_remove_subdevs(tps6586x); |
| 398 | return ret; |
| 399 | } |
| 400 | |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 401 | #ifdef CONFIG_OF |
| 402 | static struct of_regulator_match tps6586x_matches[] = { |
| 403 | { .name = "sm0", .driver_data = (void *)TPS6586X_ID_SM_0 }, |
| 404 | { .name = "sm1", .driver_data = (void *)TPS6586X_ID_SM_1 }, |
| 405 | { .name = "sm2", .driver_data = (void *)TPS6586X_ID_SM_2 }, |
| 406 | { .name = "ldo0", .driver_data = (void *)TPS6586X_ID_LDO_0 }, |
| 407 | { .name = "ldo1", .driver_data = (void *)TPS6586X_ID_LDO_1 }, |
| 408 | { .name = "ldo2", .driver_data = (void *)TPS6586X_ID_LDO_2 }, |
| 409 | { .name = "ldo3", .driver_data = (void *)TPS6586X_ID_LDO_3 }, |
| 410 | { .name = "ldo4", .driver_data = (void *)TPS6586X_ID_LDO_4 }, |
| 411 | { .name = "ldo5", .driver_data = (void *)TPS6586X_ID_LDO_5 }, |
| 412 | { .name = "ldo6", .driver_data = (void *)TPS6586X_ID_LDO_6 }, |
| 413 | { .name = "ldo7", .driver_data = (void *)TPS6586X_ID_LDO_7 }, |
| 414 | { .name = "ldo8", .driver_data = (void *)TPS6586X_ID_LDO_8 }, |
| 415 | { .name = "ldo9", .driver_data = (void *)TPS6586X_ID_LDO_9 }, |
| 416 | { .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC }, |
| 417 | }; |
| 418 | |
| 419 | static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client) |
| 420 | { |
| 421 | const unsigned int num = ARRAY_SIZE(tps6586x_matches); |
| 422 | struct device_node *np = client->dev.of_node; |
| 423 | struct tps6586x_platform_data *pdata; |
| 424 | struct tps6586x_subdev_info *devs; |
| 425 | struct device_node *regs; |
| 426 | unsigned int count; |
| 427 | unsigned int i, j; |
| 428 | int err; |
| 429 | |
| 430 | regs = of_find_node_by_name(np, "regulators"); |
| 431 | if (!regs) |
| 432 | return NULL; |
| 433 | |
| 434 | err = of_regulator_match(&client->dev, regs, tps6586x_matches, num); |
| 435 | if (err < 0) { |
| 436 | of_node_put(regs); |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | of_node_put(regs); |
| 441 | count = err; |
| 442 | |
| 443 | devs = devm_kzalloc(&client->dev, count * sizeof(*devs), GFP_KERNEL); |
| 444 | if (!devs) |
| 445 | return NULL; |
| 446 | |
| 447 | for (i = 0, j = 0; i < num && j < count; i++) { |
| 448 | if (!tps6586x_matches[i].init_data) |
| 449 | continue; |
| 450 | |
| 451 | devs[j].name = "tps6586x-regulator"; |
| 452 | devs[j].platform_data = tps6586x_matches[i].init_data; |
| 453 | devs[j].id = (int)tps6586x_matches[i].driver_data; |
| 454 | devs[j].of_node = tps6586x_matches[i].of_node; |
| 455 | j++; |
| 456 | } |
| 457 | |
| 458 | pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); |
| 459 | if (!pdata) |
| 460 | return NULL; |
| 461 | |
| 462 | pdata->num_subdevs = count; |
| 463 | pdata->subdevs = devs; |
| 464 | pdata->gpio_base = -1; |
| 465 | pdata->irq_base = -1; |
| 466 | |
| 467 | return pdata; |
| 468 | } |
| 469 | |
| 470 | static struct of_device_id tps6586x_of_match[] = { |
| 471 | { .compatible = "ti,tps6586x", }, |
| 472 | { }, |
| 473 | }; |
| 474 | #else |
| 475 | static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client) |
| 476 | { |
| 477 | return NULL; |
| 478 | } |
| 479 | #endif |
| 480 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 481 | static const struct regmap_config tps6586x_regmap_config = { |
| 482 | .reg_bits = 8, |
| 483 | .val_bits = 8, |
| 484 | .max_register = TPS6586X_MAX_REGISTER - 1, |
| 485 | }; |
| 486 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 487 | static int __devinit tps6586x_i2c_probe(struct i2c_client *client, |
| 488 | const struct i2c_device_id *id) |
| 489 | { |
| 490 | struct tps6586x_platform_data *pdata = client->dev.platform_data; |
| 491 | struct tps6586x *tps6586x; |
| 492 | int ret; |
| 493 | |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 494 | if (!pdata && client->dev.of_node) |
| 495 | pdata = tps6586x_parse_dt(client); |
| 496 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 497 | if (!pdata) { |
| 498 | dev_err(&client->dev, "tps6586x requires platform data\n"); |
| 499 | return -ENOTSUPP; |
| 500 | } |
| 501 | |
| 502 | ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC); |
| 503 | if (ret < 0) { |
| 504 | dev_err(&client->dev, "Chip ID read failed: %d\n", ret); |
| 505 | return -EIO; |
| 506 | } |
| 507 | |
Stephen Warren | 4d1cdbf | 2010-12-09 10:30:11 -0700 | [diff] [blame] | 508 | dev_info(&client->dev, "VERSIONCRC is %02x\n", ret); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 509 | |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 510 | tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL); |
| 511 | if (tps6586x == NULL) { |
| 512 | dev_err(&client->dev, "memory for tps6586x alloc failed\n"); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 513 | return -ENOMEM; |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 514 | } |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 515 | |
| 516 | tps6586x->client = client; |
| 517 | tps6586x->dev = &client->dev; |
| 518 | i2c_set_clientdata(client, tps6586x); |
| 519 | |
Laxman Dewangan | 1176b5b | 2012-07-18 11:50:46 +0530 | [diff] [blame^] | 520 | tps6586x->regmap = devm_regmap_init_i2c(client, |
| 521 | &tps6586x_regmap_config); |
| 522 | if (IS_ERR(tps6586x->regmap)) { |
| 523 | ret = PTR_ERR(tps6586x->regmap); |
| 524 | dev_err(&client->dev, "regmap init failed: %d\n", ret); |
| 525 | return ret; |
| 526 | } |
| 527 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 528 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 529 | if (client->irq) { |
| 530 | ret = tps6586x_irq_init(tps6586x, client->irq, |
| 531 | pdata->irq_base); |
| 532 | if (ret) { |
| 533 | dev_err(&client->dev, "IRQ init failed: %d\n", ret); |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 534 | return ret; |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 538 | ret = tps6586x_gpio_init(tps6586x, pdata->gpio_base); |
| 539 | if (ret) { |
| 540 | dev_err(&client->dev, "GPIO registration failed: %d\n", ret); |
| 541 | goto err_gpio_init; |
| 542 | } |
| 543 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 544 | ret = tps6586x_add_subdevs(tps6586x, pdata); |
| 545 | if (ret) { |
| 546 | dev_err(&client->dev, "add devices failed: %d\n", ret); |
| 547 | goto err_add_devs; |
| 548 | } |
| 549 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | |
| 552 | err_add_devs: |
Vincent Palatin | 6f9f13b | 2011-01-31 11:31:49 -0500 | [diff] [blame] | 553 | if (pdata->gpio_base) { |
| 554 | ret = gpiochip_remove(&tps6586x->gpio); |
| 555 | if (ret) |
| 556 | dev_err(&client->dev, "Can't remove gpio chip: %d\n", |
| 557 | ret); |
| 558 | } |
| 559 | err_gpio_init: |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 560 | if (client->irq) |
| 561 | free_irq(client->irq, tps6586x); |
Laxman Dewangan | b671941 | 2012-07-18 11:50:45 +0530 | [diff] [blame] | 562 | |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | static int __devexit tps6586x_i2c_remove(struct i2c_client *client) |
| 567 | { |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 568 | struct tps6586x *tps6586x = i2c_get_clientdata(client); |
| 569 | struct tps6586x_platform_data *pdata = client->dev.platform_data; |
| 570 | int ret; |
| 571 | |
Gary King | c26448c | 2010-09-20 00:18:27 +0200 | [diff] [blame] | 572 | if (client->irq) |
| 573 | free_irq(client->irq, tps6586x); |
| 574 | |
Axel Lin | 4b751cf | 2010-08-24 15:18:58 +0800 | [diff] [blame] | 575 | if (pdata->gpio_base) { |
| 576 | ret = gpiochip_remove(&tps6586x->gpio); |
| 577 | if (ret) |
| 578 | dev_err(&client->dev, "Can't remove gpio chip: %d\n", |
| 579 | ret); |
| 580 | } |
| 581 | |
| 582 | tps6586x_remove_subdevs(tps6586x); |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static const struct i2c_device_id tps6586x_id_table[] = { |
| 587 | { "tps6586x", 0 }, |
| 588 | { }, |
| 589 | }; |
| 590 | MODULE_DEVICE_TABLE(i2c, tps6586x_id_table); |
| 591 | |
| 592 | static struct i2c_driver tps6586x_driver = { |
| 593 | .driver = { |
| 594 | .name = "tps6586x", |
| 595 | .owner = THIS_MODULE, |
Thierry Reding | 62f6b08 | 2012-04-26 16:52:21 +0200 | [diff] [blame] | 596 | .of_match_table = of_match_ptr(tps6586x_of_match), |
Mike Rapoport | c6c1933 | 2010-08-11 01:11:04 +0200 | [diff] [blame] | 597 | }, |
| 598 | .probe = tps6586x_i2c_probe, |
| 599 | .remove = __devexit_p(tps6586x_i2c_remove), |
| 600 | .id_table = tps6586x_id_table, |
| 601 | }; |
| 602 | |
| 603 | static int __init tps6586x_init(void) |
| 604 | { |
| 605 | return i2c_add_driver(&tps6586x_driver); |
| 606 | } |
| 607 | subsys_initcall(tps6586x_init); |
| 608 | |
| 609 | static void __exit tps6586x_exit(void) |
| 610 | { |
| 611 | i2c_del_driver(&tps6586x_driver); |
| 612 | } |
| 613 | module_exit(tps6586x_exit); |
| 614 | |
| 615 | MODULE_DESCRIPTION("TPS6586X core driver"); |
| 616 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 617 | MODULE_LICENSE("GPL"); |