blob: 4674643687736679c69d74a337b1a0701f54f0bc [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>
Thierry Reding62f6b082012-04-26 16:52:21 +020027#include <linux/regulator/of_regulator.h>
Laxman Dewangan9394b802012-09-04 14:43:39 -060028#include <linux/regulator/machine.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020029
30#include <linux/mfd/core.h>
31#include <linux/mfd/tps6586x.h>
32
Bill Huang004c15a2012-08-19 18:07:55 -070033#define TPS6586X_SUPPLYENE 0x14
34#define EXITSLREQ_BIT BIT(1)
35#define SLEEP_MODE_BIT BIT(3)
36
Gary Kingc26448c2010-09-20 00:18:27 +020037/* 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 Rapoportc6c19332010-08-11 01:11:04 +020050/* device id */
51#define TPS6586X_VERSIONCRC 0xcd
Mike Rapoportc6c19332010-08-11 01:11:04 +020052
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053053/* Maximum register */
54#define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
55
Gary Kingc26448c2010-09-20 00:18:27 +020056struct 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
67static 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 Dewangan7a7487c2012-07-18 11:50:50 +053097static struct mfd_cell tps6586x_cell[] = {
98 {
99 .name = "tps6586x-gpio",
100 },
101 {
102 .name = "tps6586x-rtc",
103 },
104 {
105 .name = "tps6586x-onkey",
106 },
107};
108
Mike Rapoportc6c19332010-08-11 01:11:04 +0200109struct tps6586x {
Mike Rapoportc6c19332010-08-11 01:11:04 +0200110 struct device *dev;
111 struct i2c_client *client;
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530112 struct regmap *regmap;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200113
Gary Kingc26448c2010-09-20 00:18:27 +0200114 struct irq_chip irq_chip;
115 struct mutex irq_lock;
116 int irq_base;
117 u32 irq_en;
Gary Kingc26448c2010-09-20 00:18:27 +0200118 u8 mask_reg[5];
Mike Rapoportc6c19332010-08-11 01:11:04 +0200119};
120
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530121static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200122{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530123 return i2c_get_clientdata(to_i2c_client(dev));
Mike Rapoportc6c19332010-08-11 01:11:04 +0200124}
125
126int tps6586x_write(struct device *dev, int reg, uint8_t val)
127{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530128 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
129
130 return regmap_write(tps6586x->regmap, reg, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200131}
132EXPORT_SYMBOL_GPL(tps6586x_write);
133
134int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
135{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530136 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
137
138 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200139}
140EXPORT_SYMBOL_GPL(tps6586x_writes);
141
142int tps6586x_read(struct device *dev, int reg, uint8_t *val)
143{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530144 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
145 unsigned int rval;
146 int ret;
147
148 ret = regmap_read(tps6586x->regmap, reg, &rval);
149 if (!ret)
150 *val = rval;
151 return ret;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200152}
153EXPORT_SYMBOL_GPL(tps6586x_read);
154
155int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
156{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530157 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
158
159 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200160}
161EXPORT_SYMBOL_GPL(tps6586x_reads);
162
163int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
164{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530165 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200166
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530167 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200168}
169EXPORT_SYMBOL_GPL(tps6586x_set_bits);
170
171int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
172{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530173 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200174
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530175 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200176}
177EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
178
179int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
180{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530181 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200182
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530183 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200184}
185EXPORT_SYMBOL_GPL(tps6586x_update);
186
Mike Rapoportc6c19332010-08-11 01:11:04 +0200187static int __remove_subdev(struct device *dev, void *unused)
188{
189 platform_device_unregister(to_platform_device(dev));
190 return 0;
191}
192
193static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
194{
195 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
196}
197
Mark Brown96e824b2010-12-12 12:39:28 +0000198static void tps6586x_irq_lock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200199{
Mark Brown96e824b2010-12-12 12:39:28 +0000200 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200201
202 mutex_lock(&tps6586x->irq_lock);
203}
204
Mark Brown96e824b2010-12-12 12:39:28 +0000205static void tps6586x_irq_enable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200206{
Mark Brown96e824b2010-12-12 12:39:28 +0000207 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
208 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
Gary Kingc26448c2010-09-20 00:18:27 +0200209 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
210
211 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
212 tps6586x->irq_en |= (1 << __irq);
213}
214
Mark Brown96e824b2010-12-12 12:39:28 +0000215static void tps6586x_irq_disable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200216{
Mark Brown96e824b2010-12-12 12:39:28 +0000217 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
Gary Kingc26448c2010-09-20 00:18:27 +0200218
Mark Brown96e824b2010-12-12 12:39:28 +0000219 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
Gary Kingc26448c2010-09-20 00:18:27 +0200220 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
221
222 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
223 tps6586x->irq_en &= ~(1 << __irq);
224}
225
Mark Brown96e824b2010-12-12 12:39:28 +0000226static void tps6586x_irq_sync_unlock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200227{
Mark Brown96e824b2010-12-12 12:39:28 +0000228 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200229 int i;
230
231 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530232 int ret;
233 ret = tps6586x_write(tps6586x->dev,
234 TPS6586X_INT_MASK1 + i,
235 tps6586x->mask_reg[i]);
236 WARN_ON(ret);
Gary Kingc26448c2010-09-20 00:18:27 +0200237 }
238
239 mutex_unlock(&tps6586x->irq_lock);
240}
241
242static irqreturn_t tps6586x_irq(int irq, void *data)
243{
244 struct tps6586x *tps6586x = data;
245 u32 acks;
246 int ret = 0;
247
248 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
249 sizeof(acks), (uint8_t *)&acks);
250
251 if (ret < 0) {
252 dev_err(tps6586x->dev, "failed to read interrupt status\n");
253 return IRQ_NONE;
254 }
255
256 acks = le32_to_cpu(acks);
257
258 while (acks) {
259 int i = __ffs(acks);
260
261 if (tps6586x->irq_en & (1 << i))
262 handle_nested_irq(tps6586x->irq_base + i);
263
264 acks &= ~(1 << i);
265 }
266
267 return IRQ_HANDLED;
268}
269
270static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
271 int irq_base)
272{
273 int i, ret;
274 u8 tmp[4];
275
276 if (!irq_base) {
277 dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
278 return -EINVAL;
279 }
280
281 mutex_init(&tps6586x->irq_lock);
282 for (i = 0; i < 5; i++) {
Gary Kingc26448c2010-09-20 00:18:27 +0200283 tps6586x->mask_reg[i] = 0xff;
284 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
285 }
286
287 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
288
289 tps6586x->irq_base = irq_base;
290
291 tps6586x->irq_chip.name = "tps6586x";
Mark Brown96e824b2010-12-12 12:39:28 +0000292 tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
293 tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
294 tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
295 tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
Gary Kingc26448c2010-09-20 00:18:27 +0200296
297 for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
298 int __irq = i + tps6586x->irq_base;
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000299 irq_set_chip_data(__irq, tps6586x);
300 irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
Gary Kingc26448c2010-09-20 00:18:27 +0200301 handle_simple_irq);
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000302 irq_set_nested_thread(__irq, 1);
Gary Kingc26448c2010-09-20 00:18:27 +0200303#ifdef CONFIG_ARM
304 set_irq_flags(__irq, IRQF_VALID);
305#endif
306 }
307
308 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
309 "tps6586x", tps6586x);
310
311 if (!ret) {
312 device_init_wakeup(tps6586x->dev, 1);
313 enable_irq_wake(irq);
314 }
315
316 return ret;
317}
318
Mike Rapoportc6c19332010-08-11 01:11:04 +0200319static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
320 struct tps6586x_platform_data *pdata)
321{
322 struct tps6586x_subdev_info *subdev;
323 struct platform_device *pdev;
324 int i, ret = 0;
325
326 for (i = 0; i < pdata->num_subdevs; i++) {
327 subdev = &pdata->subdevs[i];
328
329 pdev = platform_device_alloc(subdev->name, subdev->id);
Axel Lin929980a2010-08-24 13:47:22 +0800330 if (!pdev) {
331 ret = -ENOMEM;
332 goto failed;
333 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200334
335 pdev->dev.parent = tps6586x->dev;
336 pdev->dev.platform_data = subdev->platform_data;
Thierry Reding62f6b082012-04-26 16:52:21 +0200337 pdev->dev.of_node = subdev->of_node;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200338
339 ret = platform_device_add(pdev);
Axel Lin929980a2010-08-24 13:47:22 +0800340 if (ret) {
341 platform_device_put(pdev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200342 goto failed;
Axel Lin929980a2010-08-24 13:47:22 +0800343 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200344 }
345 return 0;
346
347failed:
348 tps6586x_remove_subdevs(tps6586x);
349 return ret;
350}
351
Thierry Reding62f6b082012-04-26 16:52:21 +0200352#ifdef CONFIG_OF
353static struct of_regulator_match tps6586x_matches[] = {
Laxman Dewangan9394b802012-09-04 14:43:39 -0600354 { .name = "sys", .driver_data = (void *)TPS6586X_ID_SYS },
Thierry Reding62f6b082012-04-26 16:52:21 +0200355 { .name = "sm0", .driver_data = (void *)TPS6586X_ID_SM_0 },
356 { .name = "sm1", .driver_data = (void *)TPS6586X_ID_SM_1 },
357 { .name = "sm2", .driver_data = (void *)TPS6586X_ID_SM_2 },
358 { .name = "ldo0", .driver_data = (void *)TPS6586X_ID_LDO_0 },
359 { .name = "ldo1", .driver_data = (void *)TPS6586X_ID_LDO_1 },
360 { .name = "ldo2", .driver_data = (void *)TPS6586X_ID_LDO_2 },
361 { .name = "ldo3", .driver_data = (void *)TPS6586X_ID_LDO_3 },
362 { .name = "ldo4", .driver_data = (void *)TPS6586X_ID_LDO_4 },
363 { .name = "ldo5", .driver_data = (void *)TPS6586X_ID_LDO_5 },
364 { .name = "ldo6", .driver_data = (void *)TPS6586X_ID_LDO_6 },
365 { .name = "ldo7", .driver_data = (void *)TPS6586X_ID_LDO_7 },
366 { .name = "ldo8", .driver_data = (void *)TPS6586X_ID_LDO_8 },
367 { .name = "ldo9", .driver_data = (void *)TPS6586X_ID_LDO_9 },
368 { .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC },
369};
370
371static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
372{
373 const unsigned int num = ARRAY_SIZE(tps6586x_matches);
374 struct device_node *np = client->dev.of_node;
375 struct tps6586x_platform_data *pdata;
376 struct tps6586x_subdev_info *devs;
377 struct device_node *regs;
Laxman Dewangan9394b802012-09-04 14:43:39 -0600378 const char *sys_rail_name = NULL;
Thierry Reding62f6b082012-04-26 16:52:21 +0200379 unsigned int count;
380 unsigned int i, j;
381 int err;
382
383 regs = of_find_node_by_name(np, "regulators");
384 if (!regs)
385 return NULL;
386
387 err = of_regulator_match(&client->dev, regs, tps6586x_matches, num);
388 if (err < 0) {
389 of_node_put(regs);
390 return NULL;
391 }
392
393 of_node_put(regs);
394 count = err;
395
396 devs = devm_kzalloc(&client->dev, count * sizeof(*devs), GFP_KERNEL);
397 if (!devs)
398 return NULL;
399
400 for (i = 0, j = 0; i < num && j < count; i++) {
Laxman Dewangan9394b802012-09-04 14:43:39 -0600401 struct regulator_init_data *reg_idata;
402
Thierry Reding62f6b082012-04-26 16:52:21 +0200403 if (!tps6586x_matches[i].init_data)
404 continue;
405
Laxman Dewangan9394b802012-09-04 14:43:39 -0600406 reg_idata = tps6586x_matches[i].init_data;
Thierry Reding62f6b082012-04-26 16:52:21 +0200407 devs[j].name = "tps6586x-regulator";
408 devs[j].platform_data = tps6586x_matches[i].init_data;
409 devs[j].id = (int)tps6586x_matches[i].driver_data;
Laxman Dewangan9394b802012-09-04 14:43:39 -0600410 if (devs[j].id == TPS6586X_ID_SYS)
411 sys_rail_name = reg_idata->constraints.name;
412
413 if ((devs[j].id == TPS6586X_ID_LDO_5) ||
414 (devs[j].id == TPS6586X_ID_LDO_RTC))
415 reg_idata->supply_regulator = sys_rail_name;
416
Thierry Reding62f6b082012-04-26 16:52:21 +0200417 devs[j].of_node = tps6586x_matches[i].of_node;
418 j++;
419 }
420
421 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
422 if (!pdata)
423 return NULL;
424
425 pdata->num_subdevs = count;
426 pdata->subdevs = devs;
427 pdata->gpio_base = -1;
428 pdata->irq_base = -1;
Bill Huang004c15a2012-08-19 18:07:55 -0700429 pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
Thierry Reding62f6b082012-04-26 16:52:21 +0200430
431 return pdata;
432}
433
434static struct of_device_id tps6586x_of_match[] = {
435 { .compatible = "ti,tps6586x", },
436 { },
437};
438#else
439static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
440{
441 return NULL;
442}
443#endif
444
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530445static bool is_volatile_reg(struct device *dev, unsigned int reg)
446{
447 /* Cache all interrupt mask register */
448 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
449 return false;
450
451 return true;
452}
453
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530454static const struct regmap_config tps6586x_regmap_config = {
455 .reg_bits = 8,
456 .val_bits = 8,
457 .max_register = TPS6586X_MAX_REGISTER - 1,
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530458 .volatile_reg = is_volatile_reg,
459 .cache_type = REGCACHE_RBTREE,
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530460};
461
Bill Huang004c15a2012-08-19 18:07:55 -0700462static struct device *tps6586x_dev;
463static void tps6586x_power_off(void)
464{
465 if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
466 return;
467
468 tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
469}
470
Mike Rapoportc6c19332010-08-11 01:11:04 +0200471static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
472 const struct i2c_device_id *id)
473{
474 struct tps6586x_platform_data *pdata = client->dev.platform_data;
475 struct tps6586x *tps6586x;
476 int ret;
477
Thierry Reding62f6b082012-04-26 16:52:21 +0200478 if (!pdata && client->dev.of_node)
479 pdata = tps6586x_parse_dt(client);
480
Mike Rapoportc6c19332010-08-11 01:11:04 +0200481 if (!pdata) {
482 dev_err(&client->dev, "tps6586x requires platform data\n");
483 return -ENOTSUPP;
484 }
485
486 ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
487 if (ret < 0) {
488 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
489 return -EIO;
490 }
491
Stephen Warren4d1cdbf2010-12-09 10:30:11 -0700492 dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200493
Laxman Dewanganb6719412012-07-18 11:50:45 +0530494 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
495 if (tps6586x == NULL) {
496 dev_err(&client->dev, "memory for tps6586x alloc failed\n");
Mike Rapoportc6c19332010-08-11 01:11:04 +0200497 return -ENOMEM;
Laxman Dewanganb6719412012-07-18 11:50:45 +0530498 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200499
500 tps6586x->client = client;
501 tps6586x->dev = &client->dev;
502 i2c_set_clientdata(client, tps6586x);
503
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530504 tps6586x->regmap = devm_regmap_init_i2c(client,
505 &tps6586x_regmap_config);
506 if (IS_ERR(tps6586x->regmap)) {
507 ret = PTR_ERR(tps6586x->regmap);
508 dev_err(&client->dev, "regmap init failed: %d\n", ret);
509 return ret;
510 }
511
Mike Rapoportc6c19332010-08-11 01:11:04 +0200512
Gary Kingc26448c2010-09-20 00:18:27 +0200513 if (client->irq) {
514 ret = tps6586x_irq_init(tps6586x, client->irq,
515 pdata->irq_base);
516 if (ret) {
517 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
Laxman Dewanganb6719412012-07-18 11:50:45 +0530518 return ret;
Gary Kingc26448c2010-09-20 00:18:27 +0200519 }
520 }
521
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530522 ret = mfd_add_devices(tps6586x->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800523 tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
524 NULL, 0, NULL);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530525 if (ret < 0) {
526 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
527 goto err_mfd_add;
Vincent Palatin6f9f13b2011-01-31 11:31:49 -0500528 }
529
Mike Rapoportc6c19332010-08-11 01:11:04 +0200530 ret = tps6586x_add_subdevs(tps6586x, pdata);
531 if (ret) {
532 dev_err(&client->dev, "add devices failed: %d\n", ret);
533 goto err_add_devs;
534 }
535
Bill Huang004c15a2012-08-19 18:07:55 -0700536 if (pdata->pm_off && !pm_power_off) {
537 tps6586x_dev = &client->dev;
538 pm_power_off = tps6586x_power_off;
539 }
540
Mike Rapoportc6c19332010-08-11 01:11:04 +0200541 return 0;
542
543err_add_devs:
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530544 mfd_remove_devices(tps6586x->dev);
545err_mfd_add:
Gary Kingc26448c2010-09-20 00:18:27 +0200546 if (client->irq)
547 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200548 return ret;
549}
550
551static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
552{
Axel Lin4b751cf2010-08-24 15:18:58 +0800553 struct tps6586x *tps6586x = i2c_get_clientdata(client);
Axel Lin4b751cf2010-08-24 15:18:58 +0800554
555 tps6586x_remove_subdevs(tps6586x);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530556 mfd_remove_devices(tps6586x->dev);
557 if (client->irq)
558 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200559 return 0;
560}
561
562static const struct i2c_device_id tps6586x_id_table[] = {
563 { "tps6586x", 0 },
564 { },
565};
566MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
567
568static struct i2c_driver tps6586x_driver = {
569 .driver = {
570 .name = "tps6586x",
571 .owner = THIS_MODULE,
Thierry Reding62f6b082012-04-26 16:52:21 +0200572 .of_match_table = of_match_ptr(tps6586x_of_match),
Mike Rapoportc6c19332010-08-11 01:11:04 +0200573 },
574 .probe = tps6586x_i2c_probe,
575 .remove = __devexit_p(tps6586x_i2c_remove),
576 .id_table = tps6586x_id_table,
577};
578
579static int __init tps6586x_init(void)
580{
581 return i2c_add_driver(&tps6586x_driver);
582}
583subsys_initcall(tps6586x_init);
584
585static void __exit tps6586x_exit(void)
586{
587 i2c_del_driver(&tps6586x_driver);
588}
589module_exit(tps6586x_exit);
590
591MODULE_DESCRIPTION("TPS6586X core driver");
592MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
593MODULE_LICENSE("GPL");