blob: 9d67bd92edf8d39bce86130e547ab0c4e8010090 [file] [log] [blame]
Mike Rapoportc6c19332010-08-11 01:11:04 +02001/*
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 Kingc26448c2010-09-20 00:18:27 +020018#include <linux/interrupt.h>
19#include <linux/irq.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/mutex.h>
23#include <linux/slab.h>
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053024#include <linux/err.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020025#include <linux/i2c.h>
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053026#include <linux/regmap.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020027
28#include <linux/mfd/core.h>
29#include <linux/mfd/tps6586x.h>
30
Bill Huang004c15a2012-08-19 18:07:55 -070031#define TPS6586X_SUPPLYENE 0x14
32#define EXITSLREQ_BIT BIT(1)
33#define SLEEP_MODE_BIT BIT(3)
34
Gary Kingc26448c2010-09-20 00:18:27 +020035/* interrupt control registers */
36#define TPS6586X_INT_ACK1 0xb5
37#define TPS6586X_INT_ACK2 0xb6
38#define TPS6586X_INT_ACK3 0xb7
39#define TPS6586X_INT_ACK4 0xb8
40
41/* interrupt mask registers */
42#define TPS6586X_INT_MASK1 0xb0
43#define TPS6586X_INT_MASK2 0xb1
44#define TPS6586X_INT_MASK3 0xb2
45#define TPS6586X_INT_MASK4 0xb3
46#define TPS6586X_INT_MASK5 0xb4
47
Mike Rapoportc6c19332010-08-11 01:11:04 +020048/* device id */
49#define TPS6586X_VERSIONCRC 0xcd
Mike Rapoportc6c19332010-08-11 01:11:04 +020050
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053051/* Maximum register */
52#define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
53
Gary Kingc26448c2010-09-20 00:18:27 +020054struct tps6586x_irq_data {
55 u8 mask_reg;
56 u8 mask_mask;
57};
58
59#define TPS6586X_IRQ(_reg, _mask) \
60 { \
61 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
62 .mask_mask = (_mask), \
63 }
64
65static const struct tps6586x_irq_data tps6586x_irqs[] = {
66 [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
67 [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
68 [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
69 [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
70 [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
71 [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
72 [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
73 [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
74 [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
75 [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
76 [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
77 [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
78 [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
79 [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
80 [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
81 [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
82 [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
83 [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
84 [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
85 [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
86 [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
87 [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
88 [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
89 [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
90 [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
91 [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
92 [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
93};
94
Laxman Dewangan7a7487c2012-07-18 11:50:50 +053095static struct mfd_cell tps6586x_cell[] = {
96 {
97 .name = "tps6586x-gpio",
98 },
99 {
Laxman Dewangan64e48162012-10-18 19:36:09 +0530100 .name = "tps6586x-pmic",
101 },
102 {
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530103 .name = "tps6586x-rtc",
104 },
105 {
106 .name = "tps6586x-onkey",
107 },
108};
109
Mike Rapoportc6c19332010-08-11 01:11:04 +0200110struct tps6586x {
Mike Rapoportc6c19332010-08-11 01:11:04 +0200111 struct device *dev;
112 struct i2c_client *client;
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530113 struct regmap *regmap;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200114
Gary Kingc26448c2010-09-20 00:18:27 +0200115 struct irq_chip irq_chip;
116 struct mutex irq_lock;
117 int irq_base;
118 u32 irq_en;
Gary Kingc26448c2010-09-20 00:18:27 +0200119 u8 mask_reg[5];
Mike Rapoportc6c19332010-08-11 01:11:04 +0200120};
121
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530122static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200123{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530124 return i2c_get_clientdata(to_i2c_client(dev));
Mike Rapoportc6c19332010-08-11 01:11:04 +0200125}
126
127int tps6586x_write(struct device *dev, int reg, uint8_t val)
128{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530129 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
130
131 return regmap_write(tps6586x->regmap, reg, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200132}
133EXPORT_SYMBOL_GPL(tps6586x_write);
134
135int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
136{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530137 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
138
139 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200140}
141EXPORT_SYMBOL_GPL(tps6586x_writes);
142
143int tps6586x_read(struct device *dev, int reg, uint8_t *val)
144{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530145 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
146 unsigned int rval;
147 int ret;
148
149 ret = regmap_read(tps6586x->regmap, reg, &rval);
150 if (!ret)
151 *val = rval;
152 return ret;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200153}
154EXPORT_SYMBOL_GPL(tps6586x_read);
155
156int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
157{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530158 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
159
160 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200161}
162EXPORT_SYMBOL_GPL(tps6586x_reads);
163
164int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
165{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530166 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200167
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530168 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200169}
170EXPORT_SYMBOL_GPL(tps6586x_set_bits);
171
172int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
173{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530174 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200175
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530176 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200177}
178EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
179
180int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
181{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530182 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200183
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530184 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200185}
186EXPORT_SYMBOL_GPL(tps6586x_update);
187
Mike Rapoportc6c19332010-08-11 01:11:04 +0200188static int __remove_subdev(struct device *dev, void *unused)
189{
190 platform_device_unregister(to_platform_device(dev));
191 return 0;
192}
193
194static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
195{
196 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
197}
198
Mark Brown96e824b2010-12-12 12:39:28 +0000199static void tps6586x_irq_lock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200200{
Mark Brown96e824b2010-12-12 12:39:28 +0000201 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200202
203 mutex_lock(&tps6586x->irq_lock);
204}
205
Mark Brown96e824b2010-12-12 12:39:28 +0000206static void tps6586x_irq_enable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200207{
Mark Brown96e824b2010-12-12 12:39:28 +0000208 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
209 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
Gary Kingc26448c2010-09-20 00:18:27 +0200210 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
211
212 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
213 tps6586x->irq_en |= (1 << __irq);
214}
215
Mark Brown96e824b2010-12-12 12:39:28 +0000216static void tps6586x_irq_disable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200217{
Mark Brown96e824b2010-12-12 12:39:28 +0000218 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
Gary Kingc26448c2010-09-20 00:18:27 +0200219
Mark Brown96e824b2010-12-12 12:39:28 +0000220 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
Gary Kingc26448c2010-09-20 00:18:27 +0200221 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
222
223 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
224 tps6586x->irq_en &= ~(1 << __irq);
225}
226
Mark Brown96e824b2010-12-12 12:39:28 +0000227static void tps6586x_irq_sync_unlock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200228{
Mark Brown96e824b2010-12-12 12:39:28 +0000229 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200230 int i;
231
232 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530233 int ret;
234 ret = tps6586x_write(tps6586x->dev,
235 TPS6586X_INT_MASK1 + i,
236 tps6586x->mask_reg[i]);
237 WARN_ON(ret);
Gary Kingc26448c2010-09-20 00:18:27 +0200238 }
239
240 mutex_unlock(&tps6586x->irq_lock);
241}
242
243static irqreturn_t tps6586x_irq(int irq, void *data)
244{
245 struct tps6586x *tps6586x = data;
246 u32 acks;
247 int ret = 0;
248
249 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
250 sizeof(acks), (uint8_t *)&acks);
251
252 if (ret < 0) {
253 dev_err(tps6586x->dev, "failed to read interrupt status\n");
254 return IRQ_NONE;
255 }
256
257 acks = le32_to_cpu(acks);
258
259 while (acks) {
260 int i = __ffs(acks);
261
262 if (tps6586x->irq_en & (1 << i))
263 handle_nested_irq(tps6586x->irq_base + i);
264
265 acks &= ~(1 << i);
266 }
267
268 return IRQ_HANDLED;
269}
270
271static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
272 int irq_base)
273{
274 int i, ret;
275 u8 tmp[4];
276
277 if (!irq_base) {
278 dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
279 return -EINVAL;
280 }
281
282 mutex_init(&tps6586x->irq_lock);
283 for (i = 0; i < 5; i++) {
Gary Kingc26448c2010-09-20 00:18:27 +0200284 tps6586x->mask_reg[i] = 0xff;
285 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
286 }
287
288 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
289
290 tps6586x->irq_base = irq_base;
291
292 tps6586x->irq_chip.name = "tps6586x";
Mark Brown96e824b2010-12-12 12:39:28 +0000293 tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
294 tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
295 tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
296 tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
Gary Kingc26448c2010-09-20 00:18:27 +0200297
298 for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
299 int __irq = i + tps6586x->irq_base;
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000300 irq_set_chip_data(__irq, tps6586x);
301 irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
Gary Kingc26448c2010-09-20 00:18:27 +0200302 handle_simple_irq);
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000303 irq_set_nested_thread(__irq, 1);
Gary Kingc26448c2010-09-20 00:18:27 +0200304#ifdef CONFIG_ARM
305 set_irq_flags(__irq, IRQF_VALID);
306#endif
307 }
308
309 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
310 "tps6586x", tps6586x);
311
312 if (!ret) {
313 device_init_wakeup(tps6586x->dev, 1);
314 enable_irq_wake(irq);
315 }
316
317 return ret;
318}
319
Mike Rapoportc6c19332010-08-11 01:11:04 +0200320static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
321 struct tps6586x_platform_data *pdata)
322{
323 struct tps6586x_subdev_info *subdev;
324 struct platform_device *pdev;
325 int i, ret = 0;
326
327 for (i = 0; i < pdata->num_subdevs; i++) {
328 subdev = &pdata->subdevs[i];
329
330 pdev = platform_device_alloc(subdev->name, subdev->id);
Axel Lin929980a2010-08-24 13:47:22 +0800331 if (!pdev) {
332 ret = -ENOMEM;
333 goto failed;
334 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200335
336 pdev->dev.parent = tps6586x->dev;
337 pdev->dev.platform_data = subdev->platform_data;
Thierry Reding62f6b082012-04-26 16:52:21 +0200338 pdev->dev.of_node = subdev->of_node;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200339
340 ret = platform_device_add(pdev);
Axel Lin929980a2010-08-24 13:47:22 +0800341 if (ret) {
342 platform_device_put(pdev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200343 goto failed;
Axel Lin929980a2010-08-24 13:47:22 +0800344 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200345 }
346 return 0;
347
348failed:
349 tps6586x_remove_subdevs(tps6586x);
350 return ret;
351}
352
Thierry Reding62f6b082012-04-26 16:52:21 +0200353#ifdef CONFIG_OF
Thierry Reding62f6b082012-04-26 16:52:21 +0200354static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
355{
Thierry Reding62f6b082012-04-26 16:52:21 +0200356 struct device_node *np = client->dev.of_node;
357 struct tps6586x_platform_data *pdata;
Thierry Reding62f6b082012-04-26 16:52:21 +0200358
359 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
Laxman Dewangan64e48162012-10-18 19:36:09 +0530360 if (!pdata) {
361 dev_err(&client->dev, "Memory allocation failed\n");
Thierry Reding62f6b082012-04-26 16:52:21 +0200362 return NULL;
Laxman Dewangan64e48162012-10-18 19:36:09 +0530363 }
Thierry Reding62f6b082012-04-26 16:52:21 +0200364
Laxman Dewangan64e48162012-10-18 19:36:09 +0530365 pdata->num_subdevs = 0;
366 pdata->subdevs = NULL;
Thierry Reding62f6b082012-04-26 16:52:21 +0200367 pdata->gpio_base = -1;
368 pdata->irq_base = -1;
Bill Huang004c15a2012-08-19 18:07:55 -0700369 pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
Thierry Reding62f6b082012-04-26 16:52:21 +0200370
371 return pdata;
372}
373
374static struct of_device_id tps6586x_of_match[] = {
375 { .compatible = "ti,tps6586x", },
376 { },
377};
378#else
379static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
380{
381 return NULL;
382}
383#endif
384
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530385static bool is_volatile_reg(struct device *dev, unsigned int reg)
386{
387 /* Cache all interrupt mask register */
388 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
389 return false;
390
391 return true;
392}
393
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530394static const struct regmap_config tps6586x_regmap_config = {
395 .reg_bits = 8,
396 .val_bits = 8,
397 .max_register = TPS6586X_MAX_REGISTER - 1,
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530398 .volatile_reg = is_volatile_reg,
399 .cache_type = REGCACHE_RBTREE,
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530400};
401
Bill Huang004c15a2012-08-19 18:07:55 -0700402static struct device *tps6586x_dev;
403static void tps6586x_power_off(void)
404{
405 if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
406 return;
407
408 tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
409}
410
Mike Rapoportc6c19332010-08-11 01:11:04 +0200411static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
412 const struct i2c_device_id *id)
413{
414 struct tps6586x_platform_data *pdata = client->dev.platform_data;
415 struct tps6586x *tps6586x;
416 int ret;
417
Thierry Reding62f6b082012-04-26 16:52:21 +0200418 if (!pdata && client->dev.of_node)
419 pdata = tps6586x_parse_dt(client);
420
Mike Rapoportc6c19332010-08-11 01:11:04 +0200421 if (!pdata) {
422 dev_err(&client->dev, "tps6586x requires platform data\n");
423 return -ENOTSUPP;
424 }
425
426 ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
427 if (ret < 0) {
428 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
429 return -EIO;
430 }
431
Stephen Warren4d1cdbf2010-12-09 10:30:11 -0700432 dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200433
Laxman Dewanganb6719412012-07-18 11:50:45 +0530434 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
435 if (tps6586x == NULL) {
436 dev_err(&client->dev, "memory for tps6586x alloc failed\n");
Mike Rapoportc6c19332010-08-11 01:11:04 +0200437 return -ENOMEM;
Laxman Dewanganb6719412012-07-18 11:50:45 +0530438 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200439
440 tps6586x->client = client;
441 tps6586x->dev = &client->dev;
442 i2c_set_clientdata(client, tps6586x);
443
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530444 tps6586x->regmap = devm_regmap_init_i2c(client,
445 &tps6586x_regmap_config);
446 if (IS_ERR(tps6586x->regmap)) {
447 ret = PTR_ERR(tps6586x->regmap);
448 dev_err(&client->dev, "regmap init failed: %d\n", ret);
449 return ret;
450 }
451
Mike Rapoportc6c19332010-08-11 01:11:04 +0200452
Gary Kingc26448c2010-09-20 00:18:27 +0200453 if (client->irq) {
454 ret = tps6586x_irq_init(tps6586x, client->irq,
455 pdata->irq_base);
456 if (ret) {
457 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
Laxman Dewanganb6719412012-07-18 11:50:45 +0530458 return ret;
Gary Kingc26448c2010-09-20 00:18:27 +0200459 }
460 }
461
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530462 ret = mfd_add_devices(tps6586x->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800463 tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
464 NULL, 0, NULL);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530465 if (ret < 0) {
466 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
467 goto err_mfd_add;
Vincent Palatin6f9f13b2011-01-31 11:31:49 -0500468 }
469
Mike Rapoportc6c19332010-08-11 01:11:04 +0200470 ret = tps6586x_add_subdevs(tps6586x, pdata);
471 if (ret) {
472 dev_err(&client->dev, "add devices failed: %d\n", ret);
473 goto err_add_devs;
474 }
475
Bill Huang004c15a2012-08-19 18:07:55 -0700476 if (pdata->pm_off && !pm_power_off) {
477 tps6586x_dev = &client->dev;
478 pm_power_off = tps6586x_power_off;
479 }
480
Mike Rapoportc6c19332010-08-11 01:11:04 +0200481 return 0;
482
483err_add_devs:
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530484 mfd_remove_devices(tps6586x->dev);
485err_mfd_add:
Gary Kingc26448c2010-09-20 00:18:27 +0200486 if (client->irq)
487 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200488 return ret;
489}
490
491static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
492{
Axel Lin4b751cf2010-08-24 15:18:58 +0800493 struct tps6586x *tps6586x = i2c_get_clientdata(client);
Axel Lin4b751cf2010-08-24 15:18:58 +0800494
495 tps6586x_remove_subdevs(tps6586x);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530496 mfd_remove_devices(tps6586x->dev);
497 if (client->irq)
498 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200499 return 0;
500}
501
502static const struct i2c_device_id tps6586x_id_table[] = {
503 { "tps6586x", 0 },
504 { },
505};
506MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
507
508static struct i2c_driver tps6586x_driver = {
509 .driver = {
510 .name = "tps6586x",
511 .owner = THIS_MODULE,
Thierry Reding62f6b082012-04-26 16:52:21 +0200512 .of_match_table = of_match_ptr(tps6586x_of_match),
Mike Rapoportc6c19332010-08-11 01:11:04 +0200513 },
514 .probe = tps6586x_i2c_probe,
515 .remove = __devexit_p(tps6586x_i2c_remove),
516 .id_table = tps6586x_id_table,
517};
518
519static int __init tps6586x_init(void)
520{
521 return i2c_add_driver(&tps6586x_driver);
522}
523subsys_initcall(tps6586x_init);
524
525static void __exit tps6586x_exit(void)
526{
527 i2c_del_driver(&tps6586x_driver);
528}
529module_exit(tps6586x_exit);
530
531MODULE_DESCRIPTION("TPS6586X core driver");
532MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
533MODULE_LICENSE("GPL");