blob: 5628a6b5b19becfb28e060de827cbfaef2c6a19d [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>
Sachin Kamat06bf3c22013-10-16 14:26:54 +053029#include <linux/of.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020030
31#include <linux/mfd/core.h>
32#include <linux/mfd/tps6586x.h>
33
Bill Huang004c15a2012-08-19 18:07:55 -070034#define TPS6586X_SUPPLYENE 0x14
35#define EXITSLREQ_BIT BIT(1)
36#define SLEEP_MODE_BIT BIT(3)
37
Gary Kingc26448c2010-09-20 00:18:27 +020038/* interrupt control registers */
39#define TPS6586X_INT_ACK1 0xb5
40#define TPS6586X_INT_ACK2 0xb6
41#define TPS6586X_INT_ACK3 0xb7
42#define TPS6586X_INT_ACK4 0xb8
43
44/* interrupt mask registers */
45#define TPS6586X_INT_MASK1 0xb0
46#define TPS6586X_INT_MASK2 0xb1
47#define TPS6586X_INT_MASK3 0xb2
48#define TPS6586X_INT_MASK4 0xb3
49#define TPS6586X_INT_MASK5 0xb4
50
Mike Rapoportc6c19332010-08-11 01:11:04 +020051/* device id */
52#define TPS6586X_VERSIONCRC 0xcd
Mike Rapoportc6c19332010-08-11 01:11:04 +020053
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053054/* Maximum register */
Axel Lin088d8622015-07-07 08:52:06 +080055#define TPS6586X_MAX_REGISTER TPS6586X_VERSIONCRC
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053056
Gary Kingc26448c2010-09-20 00:18:27 +020057struct tps6586x_irq_data {
58 u8 mask_reg;
59 u8 mask_mask;
60};
61
62#define TPS6586X_IRQ(_reg, _mask) \
63 { \
64 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
65 .mask_mask = (_mask), \
66 }
67
68static const struct tps6586x_irq_data tps6586x_irqs[] = {
69 [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
70 [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
71 [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
72 [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
73 [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
74 [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
75 [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
76 [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
77 [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
78 [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
79 [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
80 [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
81 [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
82 [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
83 [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
84 [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
85 [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
86 [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
87 [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
88 [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
89 [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
90 [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
91 [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
92 [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
93 [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
94 [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
95 [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
96};
97
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +053098static struct resource tps6586x_rtc_resources[] = {
99 {
100 .start = TPS6586X_INT_RTC_ALM1,
101 .end = TPS6586X_INT_RTC_ALM1,
102 .flags = IORESOURCE_IRQ,
103 },
104};
105
Geert Uytterhoeven30fe2b52013-11-18 14:33:02 +0100106static const struct mfd_cell tps6586x_cell[] = {
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530107 {
108 .name = "tps6586x-gpio",
109 },
110 {
Marc Dietrichec8da802013-06-10 22:27:55 +0200111 .name = "tps6586x-regulator",
Laxman Dewangan64e48162012-10-18 19:36:09 +0530112 },
113 {
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530114 .name = "tps6586x-rtc",
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +0530115 .num_resources = ARRAY_SIZE(tps6586x_rtc_resources),
116 .resources = &tps6586x_rtc_resources[0],
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530117 },
118 {
119 .name = "tps6586x-onkey",
120 },
121};
122
Mike Rapoportc6c19332010-08-11 01:11:04 +0200123struct tps6586x {
Mike Rapoportc6c19332010-08-11 01:11:04 +0200124 struct device *dev;
125 struct i2c_client *client;
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530126 struct regmap *regmap;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100127 int version;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200128
Stephen Warren234506a2013-09-06 17:17:13 -0600129 int irq;
Gary Kingc26448c2010-09-20 00:18:27 +0200130 struct irq_chip irq_chip;
131 struct mutex irq_lock;
132 int irq_base;
133 u32 irq_en;
Gary Kingc26448c2010-09-20 00:18:27 +0200134 u8 mask_reg[5];
Laxman Dewangan605511a2012-11-13 19:18:05 +0530135 struct irq_domain *irq_domain;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200136};
137
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530138static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200139{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530140 return i2c_get_clientdata(to_i2c_client(dev));
Mike Rapoportc6c19332010-08-11 01:11:04 +0200141}
142
143int tps6586x_write(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
147 return regmap_write(tps6586x->regmap, reg, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200148}
149EXPORT_SYMBOL_GPL(tps6586x_write);
150
151int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
152{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530153 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
154
155 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200156}
157EXPORT_SYMBOL_GPL(tps6586x_writes);
158
159int tps6586x_read(struct device *dev, int reg, uint8_t *val)
160{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530161 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
162 unsigned int rval;
163 int ret;
164
165 ret = regmap_read(tps6586x->regmap, reg, &rval);
166 if (!ret)
167 *val = rval;
168 return ret;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200169}
170EXPORT_SYMBOL_GPL(tps6586x_read);
171
172int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
173{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530174 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
175
176 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200177}
178EXPORT_SYMBOL_GPL(tps6586x_reads);
179
180int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_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, bit_mask, bit_mask);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200185}
186EXPORT_SYMBOL_GPL(tps6586x_set_bits);
187
188int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
189{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530190 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200191
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530192 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200193}
194EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
195
196int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
197{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530198 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200199
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530200 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200201}
202EXPORT_SYMBOL_GPL(tps6586x_update);
203
Laxman Dewangan605511a2012-11-13 19:18:05 +0530204int tps6586x_irq_get_virq(struct device *dev, int irq)
205{
206 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
207
208 return irq_create_mapping(tps6586x->irq_domain, irq);
209}
210EXPORT_SYMBOL_GPL(tps6586x_irq_get_virq);
211
Stefan Agnere0a3da82013-12-06 13:51:45 +0100212int tps6586x_get_version(struct device *dev)
213{
214 struct tps6586x *tps6586x = dev_get_drvdata(dev);
215
216 return tps6586x->version;
217}
218EXPORT_SYMBOL_GPL(tps6586x_get_version);
219
Mike Rapoportc6c19332010-08-11 01:11:04 +0200220static int __remove_subdev(struct device *dev, void *unused)
221{
222 platform_device_unregister(to_platform_device(dev));
223 return 0;
224}
225
226static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
227{
228 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
229}
230
Mark Brown96e824b2010-12-12 12:39:28 +0000231static void tps6586x_irq_lock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200232{
Mark Brown96e824b2010-12-12 12:39:28 +0000233 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200234
235 mutex_lock(&tps6586x->irq_lock);
236}
237
Mark Brown96e824b2010-12-12 12:39:28 +0000238static void tps6586x_irq_enable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200239{
Mark Brown96e824b2010-12-12 12:39:28 +0000240 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
Laxman Dewangan605511a2012-11-13 19:18:05 +0530241 unsigned int __irq = irq_data->hwirq;
Gary Kingc26448c2010-09-20 00:18:27 +0200242 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
243
244 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
245 tps6586x->irq_en |= (1 << __irq);
246}
247
Mark Brown96e824b2010-12-12 12:39:28 +0000248static void tps6586x_irq_disable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200249{
Mark Brown96e824b2010-12-12 12:39:28 +0000250 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
Gary Kingc26448c2010-09-20 00:18:27 +0200251
Laxman Dewangan605511a2012-11-13 19:18:05 +0530252 unsigned int __irq = irq_data->hwirq;
Gary Kingc26448c2010-09-20 00:18:27 +0200253 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
254
255 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
256 tps6586x->irq_en &= ~(1 << __irq);
257}
258
Mark Brown96e824b2010-12-12 12:39:28 +0000259static void tps6586x_irq_sync_unlock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200260{
Mark Brown96e824b2010-12-12 12:39:28 +0000261 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200262 int i;
263
264 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530265 int ret;
266 ret = tps6586x_write(tps6586x->dev,
267 TPS6586X_INT_MASK1 + i,
268 tps6586x->mask_reg[i]);
269 WARN_ON(ret);
Gary Kingc26448c2010-09-20 00:18:27 +0200270 }
271
272 mutex_unlock(&tps6586x->irq_lock);
273}
274
Stephen Warren234506a2013-09-06 17:17:13 -0600275#ifdef CONFIG_PM_SLEEP
276static int tps6586x_irq_set_wake(struct irq_data *irq_data, unsigned int on)
277{
278 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
279 return irq_set_irq_wake(tps6586x->irq, on);
280}
281#else
282#define tps6586x_irq_set_wake NULL
283#endif
284
Laxman Dewangan605511a2012-11-13 19:18:05 +0530285static struct irq_chip tps6586x_irq_chip = {
286 .name = "tps6586x",
287 .irq_bus_lock = tps6586x_irq_lock,
288 .irq_bus_sync_unlock = tps6586x_irq_sync_unlock,
289 .irq_disable = tps6586x_irq_disable,
290 .irq_enable = tps6586x_irq_enable,
Stephen Warren234506a2013-09-06 17:17:13 -0600291 .irq_set_wake = tps6586x_irq_set_wake,
Laxman Dewangan605511a2012-11-13 19:18:05 +0530292};
293
294static int tps6586x_irq_map(struct irq_domain *h, unsigned int virq,
295 irq_hw_number_t hw)
296{
297 struct tps6586x *tps6586x = h->host_data;
298
299 irq_set_chip_data(virq, tps6586x);
300 irq_set_chip_and_handler(virq, &tps6586x_irq_chip, handle_simple_irq);
301 irq_set_nested_thread(virq, 1);
Laxman Dewangan605511a2012-11-13 19:18:05 +0530302 irq_set_noprobe(virq);
Laxman Dewangan605511a2012-11-13 19:18:05 +0530303
304 return 0;
305}
306
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +0900307static const struct irq_domain_ops tps6586x_domain_ops = {
Laxman Dewangan605511a2012-11-13 19:18:05 +0530308 .map = tps6586x_irq_map,
309 .xlate = irq_domain_xlate_twocell,
310};
311
Gary Kingc26448c2010-09-20 00:18:27 +0200312static irqreturn_t tps6586x_irq(int irq, void *data)
313{
314 struct tps6586x *tps6586x = data;
315 u32 acks;
316 int ret = 0;
317
318 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
319 sizeof(acks), (uint8_t *)&acks);
320
321 if (ret < 0) {
322 dev_err(tps6586x->dev, "failed to read interrupt status\n");
323 return IRQ_NONE;
324 }
325
326 acks = le32_to_cpu(acks);
327
328 while (acks) {
329 int i = __ffs(acks);
330
331 if (tps6586x->irq_en & (1 << i))
Laxman Dewangan605511a2012-11-13 19:18:05 +0530332 handle_nested_irq(
333 irq_find_mapping(tps6586x->irq_domain, i));
Gary Kingc26448c2010-09-20 00:18:27 +0200334
335 acks &= ~(1 << i);
336 }
337
338 return IRQ_HANDLED;
339}
340
Bill Pembertonf791be42012-11-19 13:23:04 -0500341static int tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
Gary Kingc26448c2010-09-20 00:18:27 +0200342 int irq_base)
343{
344 int i, ret;
345 u8 tmp[4];
Laxman Dewangan605511a2012-11-13 19:18:05 +0530346 int new_irq_base;
347 int irq_num = ARRAY_SIZE(tps6586x_irqs);
Gary Kingc26448c2010-09-20 00:18:27 +0200348
Stephen Warren234506a2013-09-06 17:17:13 -0600349 tps6586x->irq = irq;
350
Gary Kingc26448c2010-09-20 00:18:27 +0200351 mutex_init(&tps6586x->irq_lock);
352 for (i = 0; i < 5; i++) {
Gary Kingc26448c2010-09-20 00:18:27 +0200353 tps6586x->mask_reg[i] = 0xff;
354 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
355 }
356
357 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
358
Laxman Dewangan605511a2012-11-13 19:18:05 +0530359 if (irq_base > 0) {
360 new_irq_base = irq_alloc_descs(irq_base, 0, irq_num, -1);
361 if (new_irq_base < 0) {
362 dev_err(tps6586x->dev,
363 "Failed to alloc IRQs: %d\n", new_irq_base);
364 return new_irq_base;
365 }
366 } else {
367 new_irq_base = 0;
Gary Kingc26448c2010-09-20 00:18:27 +0200368 }
369
Laxman Dewangan605511a2012-11-13 19:18:05 +0530370 tps6586x->irq_domain = irq_domain_add_simple(tps6586x->dev->of_node,
371 irq_num, new_irq_base, &tps6586x_domain_ops,
372 tps6586x);
373 if (!tps6586x->irq_domain) {
374 dev_err(tps6586x->dev, "Failed to create IRQ domain\n");
375 return -ENOMEM;
376 }
Gary Kingc26448c2010-09-20 00:18:27 +0200377 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
378 "tps6586x", tps6586x);
379
Stephen Warren234506a2013-09-06 17:17:13 -0600380 if (!ret)
Gary Kingc26448c2010-09-20 00:18:27 +0200381 device_init_wakeup(tps6586x->dev, 1);
Gary Kingc26448c2010-09-20 00:18:27 +0200382
383 return ret;
384}
385
Bill Pembertonf791be42012-11-19 13:23:04 -0500386static int tps6586x_add_subdevs(struct tps6586x *tps6586x,
Mike Rapoportc6c19332010-08-11 01:11:04 +0200387 struct tps6586x_platform_data *pdata)
388{
389 struct tps6586x_subdev_info *subdev;
390 struct platform_device *pdev;
391 int i, ret = 0;
392
393 for (i = 0; i < pdata->num_subdevs; i++) {
394 subdev = &pdata->subdevs[i];
395
396 pdev = platform_device_alloc(subdev->name, subdev->id);
Axel Lin929980a2010-08-24 13:47:22 +0800397 if (!pdev) {
398 ret = -ENOMEM;
399 goto failed;
400 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200401
402 pdev->dev.parent = tps6586x->dev;
403 pdev->dev.platform_data = subdev->platform_data;
Thierry Reding62f6b082012-04-26 16:52:21 +0200404 pdev->dev.of_node = subdev->of_node;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200405
406 ret = platform_device_add(pdev);
Axel Lin929980a2010-08-24 13:47:22 +0800407 if (ret) {
408 platform_device_put(pdev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200409 goto failed;
Axel Lin929980a2010-08-24 13:47:22 +0800410 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200411 }
412 return 0;
413
414failed:
415 tps6586x_remove_subdevs(tps6586x);
416 return ret;
417}
418
Thierry Reding62f6b082012-04-26 16:52:21 +0200419#ifdef CONFIG_OF
Thierry Reding62f6b082012-04-26 16:52:21 +0200420static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
421{
Thierry Reding62f6b082012-04-26 16:52:21 +0200422 struct device_node *np = client->dev.of_node;
423 struct tps6586x_platform_data *pdata;
Thierry Reding62f6b082012-04-26 16:52:21 +0200424
425 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
Laxman Dewangan64e48162012-10-18 19:36:09 +0530426 if (!pdata) {
427 dev_err(&client->dev, "Memory allocation failed\n");
Thierry Reding62f6b082012-04-26 16:52:21 +0200428 return NULL;
Laxman Dewangan64e48162012-10-18 19:36:09 +0530429 }
Thierry Reding62f6b082012-04-26 16:52:21 +0200430
Laxman Dewangan64e48162012-10-18 19:36:09 +0530431 pdata->num_subdevs = 0;
432 pdata->subdevs = NULL;
Thierry Reding62f6b082012-04-26 16:52:21 +0200433 pdata->gpio_base = -1;
434 pdata->irq_base = -1;
Bill Huang004c15a2012-08-19 18:07:55 -0700435 pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
Thierry Reding62f6b082012-04-26 16:52:21 +0200436
437 return pdata;
438}
439
Jingoo Hana58cc842014-05-07 17:23:15 +0900440static const struct of_device_id tps6586x_of_match[] = {
Thierry Reding62f6b082012-04-26 16:52:21 +0200441 { .compatible = "ti,tps6586x", },
442 { },
443};
444#else
445static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
446{
447 return NULL;
448}
449#endif
450
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530451static bool is_volatile_reg(struct device *dev, unsigned int reg)
452{
453 /* Cache all interrupt mask register */
454 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
455 return false;
456
457 return true;
458}
459
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530460static const struct regmap_config tps6586x_regmap_config = {
461 .reg_bits = 8,
462 .val_bits = 8,
Axel Lin088d8622015-07-07 08:52:06 +0800463 .max_register = TPS6586X_MAX_REGISTER,
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530464 .volatile_reg = is_volatile_reg,
465 .cache_type = REGCACHE_RBTREE,
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530466};
467
Bill Huang004c15a2012-08-19 18:07:55 -0700468static struct device *tps6586x_dev;
469static void tps6586x_power_off(void)
470{
471 if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
472 return;
473
474 tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
475}
476
Stefan Agnere0a3da82013-12-06 13:51:45 +0100477static void tps6586x_print_version(struct i2c_client *client, int version)
478{
479 const char *name;
480
481 switch (version) {
482 case TPS658621A:
483 name = "TPS658621A";
484 break;
485 case TPS658621CD:
486 name = "TPS658621C/D";
487 break;
488 case TPS658623:
489 name = "TPS658623";
490 break;
Alban Bedel6c46ccc2014-05-20 12:14:03 +0200491 case TPS658640:
492 case TPS658640v2:
493 name = "TPS658640";
494 break;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100495 case TPS658643:
496 name = "TPS658643";
497 break;
498 default:
499 name = "TPS6586X";
500 break;
501 }
502
503 dev_info(&client->dev, "Found %s, VERSIONCRC is %02x\n", name, version);
504}
505
Bill Pembertonf791be42012-11-19 13:23:04 -0500506static int tps6586x_i2c_probe(struct i2c_client *client,
Mike Rapoportc6c19332010-08-11 01:11:04 +0200507 const struct i2c_device_id *id)
508{
Jingoo Han334a41c2013-07-30 17:10:05 +0900509 struct tps6586x_platform_data *pdata = dev_get_platdata(&client->dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200510 struct tps6586x *tps6586x;
511 int ret;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100512 int version;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200513
Thierry Reding62f6b082012-04-26 16:52:21 +0200514 if (!pdata && client->dev.of_node)
515 pdata = tps6586x_parse_dt(client);
516
Mike Rapoportc6c19332010-08-11 01:11:04 +0200517 if (!pdata) {
518 dev_err(&client->dev, "tps6586x requires platform data\n");
519 return -ENOTSUPP;
520 }
521
Stefan Agnere0a3da82013-12-06 13:51:45 +0100522 version = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
523 if (version < 0) {
524 dev_err(&client->dev, "Chip ID read failed: %d\n", version);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200525 return -EIO;
526 }
527
Laxman Dewanganb6719412012-07-18 11:50:45 +0530528 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
Stefan Agnere0a3da82013-12-06 13:51:45 +0100529 if (!tps6586x)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200530 return -ENOMEM;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100531
532 tps6586x->version = version;
533 tps6586x_print_version(client, tps6586x->version);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200534
535 tps6586x->client = client;
536 tps6586x->dev = &client->dev;
537 i2c_set_clientdata(client, tps6586x);
538
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530539 tps6586x->regmap = devm_regmap_init_i2c(client,
540 &tps6586x_regmap_config);
541 if (IS_ERR(tps6586x->regmap)) {
542 ret = PTR_ERR(tps6586x->regmap);
543 dev_err(&client->dev, "regmap init failed: %d\n", ret);
544 return ret;
545 }
546
Mike Rapoportc6c19332010-08-11 01:11:04 +0200547
Gary Kingc26448c2010-09-20 00:18:27 +0200548 if (client->irq) {
549 ret = tps6586x_irq_init(tps6586x, client->irq,
550 pdata->irq_base);
551 if (ret) {
552 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
Laxman Dewanganb6719412012-07-18 11:50:45 +0530553 return ret;
Gary Kingc26448c2010-09-20 00:18:27 +0200554 }
555 }
556
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530557 ret = mfd_add_devices(tps6586x->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800558 tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +0530559 NULL, 0, tps6586x->irq_domain);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530560 if (ret < 0) {
561 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
562 goto err_mfd_add;
Vincent Palatin6f9f13b2011-01-31 11:31:49 -0500563 }
564
Mike Rapoportc6c19332010-08-11 01:11:04 +0200565 ret = tps6586x_add_subdevs(tps6586x, pdata);
566 if (ret) {
567 dev_err(&client->dev, "add devices failed: %d\n", ret);
568 goto err_add_devs;
569 }
570
Bill Huang004c15a2012-08-19 18:07:55 -0700571 if (pdata->pm_off && !pm_power_off) {
572 tps6586x_dev = &client->dev;
573 pm_power_off = tps6586x_power_off;
574 }
575
Mike Rapoportc6c19332010-08-11 01:11:04 +0200576 return 0;
577
578err_add_devs:
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530579 mfd_remove_devices(tps6586x->dev);
580err_mfd_add:
Gary Kingc26448c2010-09-20 00:18:27 +0200581 if (client->irq)
582 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200583 return ret;
584}
585
Bill Pemberton4740f732012-11-19 13:26:01 -0500586static int tps6586x_i2c_remove(struct i2c_client *client)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200587{
Axel Lin4b751cf2010-08-24 15:18:58 +0800588 struct tps6586x *tps6586x = i2c_get_clientdata(client);
Axel Lin4b751cf2010-08-24 15:18:58 +0800589
590 tps6586x_remove_subdevs(tps6586x);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530591 mfd_remove_devices(tps6586x->dev);
592 if (client->irq)
593 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200594 return 0;
595}
596
597static const struct i2c_device_id tps6586x_id_table[] = {
598 { "tps6586x", 0 },
599 { },
600};
601MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
602
603static struct i2c_driver tps6586x_driver = {
604 .driver = {
605 .name = "tps6586x",
Thierry Reding62f6b082012-04-26 16:52:21 +0200606 .of_match_table = of_match_ptr(tps6586x_of_match),
Mike Rapoportc6c19332010-08-11 01:11:04 +0200607 },
608 .probe = tps6586x_i2c_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500609 .remove = tps6586x_i2c_remove,
Mike Rapoportc6c19332010-08-11 01:11:04 +0200610 .id_table = tps6586x_id_table,
611};
612
613static int __init tps6586x_init(void)
614{
615 return i2c_add_driver(&tps6586x_driver);
616}
617subsys_initcall(tps6586x_init);
618
619static void __exit tps6586x_exit(void)
620{
621 i2c_del_driver(&tps6586x_driver);
622}
623module_exit(tps6586x_exit);
624
625MODULE_DESCRIPTION("TPS6586X core driver");
626MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
627MODULE_LICENSE("GPL");