blob: c11539a80fc589736a02539d5ab9d468e6a1d513 [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>
Laxman Dewangan605511a2012-11-13 19:18:05 +053020#include <linux/irqdomain.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020021#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24#include <linux/slab.h>
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053025#include <linux/err.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020026#include <linux/i2c.h>
Laxman Dewangan605511a2012-11-13 19:18:05 +053027#include <linux/platform_device.h>
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053028#include <linux/regmap.h>
Thierry Reding62f6b082012-04-26 16:52:21 +020029#include <linux/regulator/of_regulator.h>
Laxman Dewangan9394b802012-09-04 14:43:39 -060030#include <linux/regulator/machine.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020031
32#include <linux/mfd/core.h>
33#include <linux/mfd/tps6586x.h>
34
Bill Huang004c15a2012-08-19 18:07:55 -070035#define TPS6586X_SUPPLYENE 0x14
36#define EXITSLREQ_BIT BIT(1)
37#define SLEEP_MODE_BIT BIT(3)
38
Gary Kingc26448c2010-09-20 00:18:27 +020039/* interrupt control registers */
40#define TPS6586X_INT_ACK1 0xb5
41#define TPS6586X_INT_ACK2 0xb6
42#define TPS6586X_INT_ACK3 0xb7
43#define TPS6586X_INT_ACK4 0xb8
44
45/* interrupt mask registers */
46#define TPS6586X_INT_MASK1 0xb0
47#define TPS6586X_INT_MASK2 0xb1
48#define TPS6586X_INT_MASK3 0xb2
49#define TPS6586X_INT_MASK4 0xb3
50#define TPS6586X_INT_MASK5 0xb4
51
Mike Rapoportc6c19332010-08-11 01:11:04 +020052/* device id */
53#define TPS6586X_VERSIONCRC 0xcd
Mike Rapoportc6c19332010-08-11 01:11:04 +020054
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053055/* Maximum register */
56#define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
57
Gary Kingc26448c2010-09-20 00:18:27 +020058struct tps6586x_irq_data {
59 u8 mask_reg;
60 u8 mask_mask;
61};
62
63#define TPS6586X_IRQ(_reg, _mask) \
64 { \
65 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
66 .mask_mask = (_mask), \
67 }
68
69static const struct tps6586x_irq_data tps6586x_irqs[] = {
70 [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
71 [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
72 [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
73 [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
74 [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
75 [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
76 [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
77 [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
78 [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
79 [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
80 [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
81 [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
82 [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
83 [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
84 [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
85 [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
86 [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
87 [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
88 [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
89 [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
90 [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
91 [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
92 [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
93 [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
94 [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
95 [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
96 [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
97};
98
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +053099static struct resource tps6586x_rtc_resources[] = {
100 {
101 .start = TPS6586X_INT_RTC_ALM1,
102 .end = TPS6586X_INT_RTC_ALM1,
103 .flags = IORESOURCE_IRQ,
104 },
105};
106
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530107static struct mfd_cell tps6586x_cell[] = {
108 {
109 .name = "tps6586x-gpio",
110 },
111 {
112 .name = "tps6586x-rtc",
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +0530113 .num_resources = ARRAY_SIZE(tps6586x_rtc_resources),
114 .resources = &tps6586x_rtc_resources[0],
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530115 },
116 {
117 .name = "tps6586x-onkey",
118 },
119};
120
Mike Rapoportc6c19332010-08-11 01:11:04 +0200121struct tps6586x {
Mike Rapoportc6c19332010-08-11 01:11:04 +0200122 struct device *dev;
123 struct i2c_client *client;
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530124 struct regmap *regmap;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200125
Gary Kingc26448c2010-09-20 00:18:27 +0200126 struct irq_chip irq_chip;
127 struct mutex irq_lock;
128 int irq_base;
129 u32 irq_en;
Gary Kingc26448c2010-09-20 00:18:27 +0200130 u8 mask_reg[5];
Laxman Dewangan605511a2012-11-13 19:18:05 +0530131 struct irq_domain *irq_domain;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200132};
133
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530134static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200135{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530136 return i2c_get_clientdata(to_i2c_client(dev));
Mike Rapoportc6c19332010-08-11 01:11:04 +0200137}
138
139int tps6586x_write(struct device *dev, int reg, uint8_t val)
140{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530141 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
142
143 return regmap_write(tps6586x->regmap, reg, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200144}
145EXPORT_SYMBOL_GPL(tps6586x_write);
146
147int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
148{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530149 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
150
151 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200152}
153EXPORT_SYMBOL_GPL(tps6586x_writes);
154
155int tps6586x_read(struct device *dev, int reg, uint8_t *val)
156{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530157 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
158 unsigned int rval;
159 int ret;
160
161 ret = regmap_read(tps6586x->regmap, reg, &rval);
162 if (!ret)
163 *val = rval;
164 return ret;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200165}
166EXPORT_SYMBOL_GPL(tps6586x_read);
167
168int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
169{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530170 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
171
172 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200173}
174EXPORT_SYMBOL_GPL(tps6586x_reads);
175
176int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
177{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530178 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200179
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530180 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200181}
182EXPORT_SYMBOL_GPL(tps6586x_set_bits);
183
184int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
185{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530186 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200187
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530188 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200189}
190EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
191
192int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
193{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530194 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200195
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530196 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200197}
198EXPORT_SYMBOL_GPL(tps6586x_update);
199
Laxman Dewangan605511a2012-11-13 19:18:05 +0530200int tps6586x_irq_get_virq(struct device *dev, int irq)
201{
202 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
203
204 return irq_create_mapping(tps6586x->irq_domain, irq);
205}
206EXPORT_SYMBOL_GPL(tps6586x_irq_get_virq);
207
Mike Rapoportc6c19332010-08-11 01:11:04 +0200208static int __remove_subdev(struct device *dev, void *unused)
209{
210 platform_device_unregister(to_platform_device(dev));
211 return 0;
212}
213
214static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
215{
216 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
217}
218
Mark Brown96e824b2010-12-12 12:39:28 +0000219static void tps6586x_irq_lock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200220{
Mark Brown96e824b2010-12-12 12:39:28 +0000221 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200222
223 mutex_lock(&tps6586x->irq_lock);
224}
225
Mark Brown96e824b2010-12-12 12:39:28 +0000226static void tps6586x_irq_enable(struct irq_data *irq_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(irq_data);
Laxman Dewangan605511a2012-11-13 19:18:05 +0530229 unsigned int __irq = irq_data->hwirq;
Gary Kingc26448c2010-09-20 00:18:27 +0200230 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
231
232 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
233 tps6586x->irq_en |= (1 << __irq);
234}
235
Mark Brown96e824b2010-12-12 12:39:28 +0000236static void tps6586x_irq_disable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200237{
Mark Brown96e824b2010-12-12 12:39:28 +0000238 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
Gary Kingc26448c2010-09-20 00:18:27 +0200239
Laxman Dewangan605511a2012-11-13 19:18:05 +0530240 unsigned int __irq = irq_data->hwirq;
Gary Kingc26448c2010-09-20 00:18:27 +0200241 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
242
243 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
244 tps6586x->irq_en &= ~(1 << __irq);
245}
246
Mark Brown96e824b2010-12-12 12:39:28 +0000247static void tps6586x_irq_sync_unlock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200248{
Mark Brown96e824b2010-12-12 12:39:28 +0000249 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200250 int i;
251
252 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530253 int ret;
254 ret = tps6586x_write(tps6586x->dev,
255 TPS6586X_INT_MASK1 + i,
256 tps6586x->mask_reg[i]);
257 WARN_ON(ret);
Gary Kingc26448c2010-09-20 00:18:27 +0200258 }
259
260 mutex_unlock(&tps6586x->irq_lock);
261}
262
Laxman Dewangan605511a2012-11-13 19:18:05 +0530263static struct irq_chip tps6586x_irq_chip = {
264 .name = "tps6586x",
265 .irq_bus_lock = tps6586x_irq_lock,
266 .irq_bus_sync_unlock = tps6586x_irq_sync_unlock,
267 .irq_disable = tps6586x_irq_disable,
268 .irq_enable = tps6586x_irq_enable,
269};
270
271static int tps6586x_irq_map(struct irq_domain *h, unsigned int virq,
272 irq_hw_number_t hw)
273{
274 struct tps6586x *tps6586x = h->host_data;
275
276 irq_set_chip_data(virq, tps6586x);
277 irq_set_chip_and_handler(virq, &tps6586x_irq_chip, handle_simple_irq);
278 irq_set_nested_thread(virq, 1);
279
280 /* ARM needs us to explicitly flag the IRQ as valid
281 * and will set them noprobe when we do so. */
282#ifdef CONFIG_ARM
283 set_irq_flags(virq, IRQF_VALID);
284#else
285 irq_set_noprobe(virq);
286#endif
287
288 return 0;
289}
290
291static struct irq_domain_ops tps6586x_domain_ops = {
292 .map = tps6586x_irq_map,
293 .xlate = irq_domain_xlate_twocell,
294};
295
Gary Kingc26448c2010-09-20 00:18:27 +0200296static irqreturn_t tps6586x_irq(int irq, void *data)
297{
298 struct tps6586x *tps6586x = data;
299 u32 acks;
300 int ret = 0;
301
302 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
303 sizeof(acks), (uint8_t *)&acks);
304
305 if (ret < 0) {
306 dev_err(tps6586x->dev, "failed to read interrupt status\n");
307 return IRQ_NONE;
308 }
309
310 acks = le32_to_cpu(acks);
311
312 while (acks) {
313 int i = __ffs(acks);
314
315 if (tps6586x->irq_en & (1 << i))
Laxman Dewangan605511a2012-11-13 19:18:05 +0530316 handle_nested_irq(
317 irq_find_mapping(tps6586x->irq_domain, i));
Gary Kingc26448c2010-09-20 00:18:27 +0200318
319 acks &= ~(1 << i);
320 }
321
322 return IRQ_HANDLED;
323}
324
325static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
326 int irq_base)
327{
328 int i, ret;
329 u8 tmp[4];
Laxman Dewangan605511a2012-11-13 19:18:05 +0530330 int new_irq_base;
331 int irq_num = ARRAY_SIZE(tps6586x_irqs);
Gary Kingc26448c2010-09-20 00:18:27 +0200332
333 mutex_init(&tps6586x->irq_lock);
334 for (i = 0; i < 5; i++) {
Gary Kingc26448c2010-09-20 00:18:27 +0200335 tps6586x->mask_reg[i] = 0xff;
336 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
337 }
338
339 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
340
Laxman Dewangan605511a2012-11-13 19:18:05 +0530341 if (irq_base > 0) {
342 new_irq_base = irq_alloc_descs(irq_base, 0, irq_num, -1);
343 if (new_irq_base < 0) {
344 dev_err(tps6586x->dev,
345 "Failed to alloc IRQs: %d\n", new_irq_base);
346 return new_irq_base;
347 }
348 } else {
349 new_irq_base = 0;
Gary Kingc26448c2010-09-20 00:18:27 +0200350 }
351
Laxman Dewangan605511a2012-11-13 19:18:05 +0530352 tps6586x->irq_domain = irq_domain_add_simple(tps6586x->dev->of_node,
353 irq_num, new_irq_base, &tps6586x_domain_ops,
354 tps6586x);
355 if (!tps6586x->irq_domain) {
356 dev_err(tps6586x->dev, "Failed to create IRQ domain\n");
357 return -ENOMEM;
358 }
Gary Kingc26448c2010-09-20 00:18:27 +0200359 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
360 "tps6586x", tps6586x);
361
362 if (!ret) {
363 device_init_wakeup(tps6586x->dev, 1);
364 enable_irq_wake(irq);
365 }
366
367 return ret;
368}
369
Mike Rapoportc6c19332010-08-11 01:11:04 +0200370static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
371 struct tps6586x_platform_data *pdata)
372{
373 struct tps6586x_subdev_info *subdev;
374 struct platform_device *pdev;
375 int i, ret = 0;
376
377 for (i = 0; i < pdata->num_subdevs; i++) {
378 subdev = &pdata->subdevs[i];
379
380 pdev = platform_device_alloc(subdev->name, subdev->id);
Axel Lin929980a2010-08-24 13:47:22 +0800381 if (!pdev) {
382 ret = -ENOMEM;
383 goto failed;
384 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200385
386 pdev->dev.parent = tps6586x->dev;
387 pdev->dev.platform_data = subdev->platform_data;
Thierry Reding62f6b082012-04-26 16:52:21 +0200388 pdev->dev.of_node = subdev->of_node;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200389
390 ret = platform_device_add(pdev);
Axel Lin929980a2010-08-24 13:47:22 +0800391 if (ret) {
392 platform_device_put(pdev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200393 goto failed;
Axel Lin929980a2010-08-24 13:47:22 +0800394 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200395 }
396 return 0;
397
398failed:
399 tps6586x_remove_subdevs(tps6586x);
400 return ret;
401}
402
Thierry Reding62f6b082012-04-26 16:52:21 +0200403#ifdef CONFIG_OF
404static struct of_regulator_match tps6586x_matches[] = {
Laxman Dewangan9394b802012-09-04 14:43:39 -0600405 { .name = "sys", .driver_data = (void *)TPS6586X_ID_SYS },
Thierry Reding62f6b082012-04-26 16:52:21 +0200406 { .name = "sm0", .driver_data = (void *)TPS6586X_ID_SM_0 },
407 { .name = "sm1", .driver_data = (void *)TPS6586X_ID_SM_1 },
408 { .name = "sm2", .driver_data = (void *)TPS6586X_ID_SM_2 },
409 { .name = "ldo0", .driver_data = (void *)TPS6586X_ID_LDO_0 },
410 { .name = "ldo1", .driver_data = (void *)TPS6586X_ID_LDO_1 },
411 { .name = "ldo2", .driver_data = (void *)TPS6586X_ID_LDO_2 },
412 { .name = "ldo3", .driver_data = (void *)TPS6586X_ID_LDO_3 },
413 { .name = "ldo4", .driver_data = (void *)TPS6586X_ID_LDO_4 },
414 { .name = "ldo5", .driver_data = (void *)TPS6586X_ID_LDO_5 },
415 { .name = "ldo6", .driver_data = (void *)TPS6586X_ID_LDO_6 },
416 { .name = "ldo7", .driver_data = (void *)TPS6586X_ID_LDO_7 },
417 { .name = "ldo8", .driver_data = (void *)TPS6586X_ID_LDO_8 },
418 { .name = "ldo9", .driver_data = (void *)TPS6586X_ID_LDO_9 },
419 { .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC },
420};
421
422static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
423{
424 const unsigned int num = ARRAY_SIZE(tps6586x_matches);
425 struct device_node *np = client->dev.of_node;
426 struct tps6586x_platform_data *pdata;
427 struct tps6586x_subdev_info *devs;
428 struct device_node *regs;
Laxman Dewangan9394b802012-09-04 14:43:39 -0600429 const char *sys_rail_name = NULL;
Thierry Reding62f6b082012-04-26 16:52:21 +0200430 unsigned int count;
431 unsigned int i, j;
432 int err;
433
434 regs = of_find_node_by_name(np, "regulators");
435 if (!regs)
436 return NULL;
437
438 err = of_regulator_match(&client->dev, regs, tps6586x_matches, num);
439 if (err < 0) {
440 of_node_put(regs);
441 return NULL;
442 }
443
444 of_node_put(regs);
445 count = err;
446
447 devs = devm_kzalloc(&client->dev, count * sizeof(*devs), GFP_KERNEL);
448 if (!devs)
449 return NULL;
450
451 for (i = 0, j = 0; i < num && j < count; i++) {
Laxman Dewangan9394b802012-09-04 14:43:39 -0600452 struct regulator_init_data *reg_idata;
453
Thierry Reding62f6b082012-04-26 16:52:21 +0200454 if (!tps6586x_matches[i].init_data)
455 continue;
456
Laxman Dewangan9394b802012-09-04 14:43:39 -0600457 reg_idata = tps6586x_matches[i].init_data;
Thierry Reding62f6b082012-04-26 16:52:21 +0200458 devs[j].name = "tps6586x-regulator";
459 devs[j].platform_data = tps6586x_matches[i].init_data;
460 devs[j].id = (int)tps6586x_matches[i].driver_data;
Laxman Dewangan9394b802012-09-04 14:43:39 -0600461 if (devs[j].id == TPS6586X_ID_SYS)
462 sys_rail_name = reg_idata->constraints.name;
463
464 if ((devs[j].id == TPS6586X_ID_LDO_5) ||
465 (devs[j].id == TPS6586X_ID_LDO_RTC))
466 reg_idata->supply_regulator = sys_rail_name;
467
Thierry Reding62f6b082012-04-26 16:52:21 +0200468 devs[j].of_node = tps6586x_matches[i].of_node;
469 j++;
470 }
471
472 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
473 if (!pdata)
474 return NULL;
475
476 pdata->num_subdevs = count;
477 pdata->subdevs = devs;
478 pdata->gpio_base = -1;
479 pdata->irq_base = -1;
Bill Huang004c15a2012-08-19 18:07:55 -0700480 pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
Thierry Reding62f6b082012-04-26 16:52:21 +0200481
482 return pdata;
483}
484
485static struct of_device_id tps6586x_of_match[] = {
486 { .compatible = "ti,tps6586x", },
487 { },
488};
489#else
490static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
491{
492 return NULL;
493}
494#endif
495
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530496static bool is_volatile_reg(struct device *dev, unsigned int reg)
497{
498 /* Cache all interrupt mask register */
499 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
500 return false;
501
502 return true;
503}
504
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530505static const struct regmap_config tps6586x_regmap_config = {
506 .reg_bits = 8,
507 .val_bits = 8,
508 .max_register = TPS6586X_MAX_REGISTER - 1,
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530509 .volatile_reg = is_volatile_reg,
510 .cache_type = REGCACHE_RBTREE,
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530511};
512
Bill Huang004c15a2012-08-19 18:07:55 -0700513static struct device *tps6586x_dev;
514static void tps6586x_power_off(void)
515{
516 if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
517 return;
518
519 tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
520}
521
Mike Rapoportc6c19332010-08-11 01:11:04 +0200522static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
523 const struct i2c_device_id *id)
524{
525 struct tps6586x_platform_data *pdata = client->dev.platform_data;
526 struct tps6586x *tps6586x;
527 int ret;
528
Thierry Reding62f6b082012-04-26 16:52:21 +0200529 if (!pdata && client->dev.of_node)
530 pdata = tps6586x_parse_dt(client);
531
Mike Rapoportc6c19332010-08-11 01:11:04 +0200532 if (!pdata) {
533 dev_err(&client->dev, "tps6586x requires platform data\n");
534 return -ENOTSUPP;
535 }
536
537 ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
538 if (ret < 0) {
539 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
540 return -EIO;
541 }
542
Stephen Warren4d1cdbf2010-12-09 10:30:11 -0700543 dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200544
Laxman Dewanganb6719412012-07-18 11:50:45 +0530545 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
546 if (tps6586x == NULL) {
547 dev_err(&client->dev, "memory for tps6586x alloc failed\n");
Mike Rapoportc6c19332010-08-11 01:11:04 +0200548 return -ENOMEM;
Laxman Dewanganb6719412012-07-18 11:50:45 +0530549 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200550
551 tps6586x->client = client;
552 tps6586x->dev = &client->dev;
553 i2c_set_clientdata(client, tps6586x);
554
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530555 tps6586x->regmap = devm_regmap_init_i2c(client,
556 &tps6586x_regmap_config);
557 if (IS_ERR(tps6586x->regmap)) {
558 ret = PTR_ERR(tps6586x->regmap);
559 dev_err(&client->dev, "regmap init failed: %d\n", ret);
560 return ret;
561 }
562
Mike Rapoportc6c19332010-08-11 01:11:04 +0200563
Gary Kingc26448c2010-09-20 00:18:27 +0200564 if (client->irq) {
565 ret = tps6586x_irq_init(tps6586x, client->irq,
566 pdata->irq_base);
567 if (ret) {
568 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
Laxman Dewanganb6719412012-07-18 11:50:45 +0530569 return ret;
Gary Kingc26448c2010-09-20 00:18:27 +0200570 }
571 }
572
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530573 ret = mfd_add_devices(tps6586x->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800574 tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +0530575 NULL, 0, tps6586x->irq_domain);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530576 if (ret < 0) {
577 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
578 goto err_mfd_add;
Vincent Palatin6f9f13b2011-01-31 11:31:49 -0500579 }
580
Mike Rapoportc6c19332010-08-11 01:11:04 +0200581 ret = tps6586x_add_subdevs(tps6586x, pdata);
582 if (ret) {
583 dev_err(&client->dev, "add devices failed: %d\n", ret);
584 goto err_add_devs;
585 }
586
Bill Huang004c15a2012-08-19 18:07:55 -0700587 if (pdata->pm_off && !pm_power_off) {
588 tps6586x_dev = &client->dev;
589 pm_power_off = tps6586x_power_off;
590 }
591
Mike Rapoportc6c19332010-08-11 01:11:04 +0200592 return 0;
593
594err_add_devs:
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530595 mfd_remove_devices(tps6586x->dev);
596err_mfd_add:
Gary Kingc26448c2010-09-20 00:18:27 +0200597 if (client->irq)
598 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200599 return ret;
600}
601
602static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
603{
Axel Lin4b751cf2010-08-24 15:18:58 +0800604 struct tps6586x *tps6586x = i2c_get_clientdata(client);
Axel Lin4b751cf2010-08-24 15:18:58 +0800605
606 tps6586x_remove_subdevs(tps6586x);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530607 mfd_remove_devices(tps6586x->dev);
608 if (client->irq)
609 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200610 return 0;
611}
612
613static const struct i2c_device_id tps6586x_id_table[] = {
614 { "tps6586x", 0 },
615 { },
616};
617MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
618
619static struct i2c_driver tps6586x_driver = {
620 .driver = {
621 .name = "tps6586x",
622 .owner = THIS_MODULE,
Thierry Reding62f6b082012-04-26 16:52:21 +0200623 .of_match_table = of_match_ptr(tps6586x_of_match),
Mike Rapoportc6c19332010-08-11 01:11:04 +0200624 },
625 .probe = tps6586x_i2c_probe,
626 .remove = __devexit_p(tps6586x_i2c_remove),
627 .id_table = tps6586x_id_table,
628};
629
630static int __init tps6586x_init(void)
631{
632 return i2c_add_driver(&tps6586x_driver);
633}
634subsys_initcall(tps6586x_init);
635
636static void __exit tps6586x_exit(void)
637{
638 i2c_del_driver(&tps6586x_driver);
639}
640module_exit(tps6586x_exit);
641
642MODULE_DESCRIPTION("TPS6586X core driver");
643MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
644MODULE_LICENSE("GPL");