blob: c8aadd39324e37e59337f1d7b09011517839b3df [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mike Rapoportc6c19332010-08-11 01:11:04 +02002/*
3 * Core driver for TI TPS6586x PMIC family
4 *
5 * Copyright (c) 2010 CompuLab Ltd.
6 * Mike Rapoport <mike@compulab.co.il>
7 *
8 * Based on da903x.c.
9 * Copyright (C) 2008 Compulab, Ltd.
10 * Mike Rapoport <mike@compulab.co.il>
11 * Copyright (C) 2006-2008 Marvell International Ltd.
12 * Eric Miao <eric.miao@marvell.com>
Mike Rapoportc6c19332010-08-11 01:11:04 +020013 */
14
Gary Kingc26448c2010-09-20 00:18:27 +020015#include <linux/interrupt.h>
16#include <linux/irq.h>
Laxman Dewangan605511a2012-11-13 19:18:05 +053017#include <linux/irqdomain.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020018#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21#include <linux/slab.h>
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053022#include <linux/err.h>
Mike Rapoportc6c19332010-08-11 01:11:04 +020023#include <linux/i2c.h>
Laxman Dewangan605511a2012-11-13 19:18:05 +053024#include <linux/platform_device.h>
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053025#include <linux/regmap.h>
Sachin Kamat06bf3c22013-10-16 14:26:54 +053026#include <linux/of.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 */
Axel Lin088d8622015-07-07 08:52:06 +080052#define TPS6586X_MAX_REGISTER TPS6586X_VERSIONCRC
Laxman Dewangan1176b5b2012-07-18 11:50:46 +053053
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 Dewangan5b8b1fe2012-11-13 19:18:06 +053095static struct resource tps6586x_rtc_resources[] = {
96 {
97 .start = TPS6586X_INT_RTC_ALM1,
98 .end = TPS6586X_INT_RTC_ALM1,
99 .flags = IORESOURCE_IRQ,
100 },
101};
102
Geert Uytterhoeven30fe2b52013-11-18 14:33:02 +0100103static const struct mfd_cell tps6586x_cell[] = {
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530104 {
105 .name = "tps6586x-gpio",
106 },
107 {
Marc Dietrichec8da802013-06-10 22:27:55 +0200108 .name = "tps6586x-regulator",
Laxman Dewangan64e48162012-10-18 19:36:09 +0530109 },
110 {
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530111 .name = "tps6586x-rtc",
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +0530112 .num_resources = ARRAY_SIZE(tps6586x_rtc_resources),
113 .resources = &tps6586x_rtc_resources[0],
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530114 },
115 {
116 .name = "tps6586x-onkey",
117 },
118};
119
Mike Rapoportc6c19332010-08-11 01:11:04 +0200120struct tps6586x {
Mike Rapoportc6c19332010-08-11 01:11:04 +0200121 struct device *dev;
122 struct i2c_client *client;
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530123 struct regmap *regmap;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100124 int version;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200125
Stephen Warren234506a2013-09-06 17:17:13 -0600126 int irq;
Gary Kingc26448c2010-09-20 00:18:27 +0200127 struct irq_chip irq_chip;
128 struct mutex irq_lock;
129 int irq_base;
130 u32 irq_en;
Gary Kingc26448c2010-09-20 00:18:27 +0200131 u8 mask_reg[5];
Laxman Dewangan605511a2012-11-13 19:18:05 +0530132 struct irq_domain *irq_domain;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200133};
134
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530135static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200136{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530137 return i2c_get_clientdata(to_i2c_client(dev));
Mike Rapoportc6c19332010-08-11 01:11:04 +0200138}
139
140int tps6586x_write(struct device *dev, int reg, uint8_t val)
141{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530142 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
143
144 return regmap_write(tps6586x->regmap, reg, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200145}
146EXPORT_SYMBOL_GPL(tps6586x_write);
147
148int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
149{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530150 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
151
152 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200153}
154EXPORT_SYMBOL_GPL(tps6586x_writes);
155
156int tps6586x_read(struct device *dev, int reg, uint8_t *val)
157{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530158 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
159 unsigned int rval;
160 int ret;
161
162 ret = regmap_read(tps6586x->regmap, reg, &rval);
163 if (!ret)
164 *val = rval;
165 return ret;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200166}
167EXPORT_SYMBOL_GPL(tps6586x_read);
168
169int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
170{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530171 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
172
173 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200174}
175EXPORT_SYMBOL_GPL(tps6586x_reads);
176
177int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
178{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530179 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200180
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530181 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200182}
183EXPORT_SYMBOL_GPL(tps6586x_set_bits);
184
185int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
186{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530187 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200188
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530189 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200190}
191EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
192
193int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
194{
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530195 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200196
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530197 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200198}
199EXPORT_SYMBOL_GPL(tps6586x_update);
200
Laxman Dewangan605511a2012-11-13 19:18:05 +0530201int tps6586x_irq_get_virq(struct device *dev, int irq)
202{
203 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
204
205 return irq_create_mapping(tps6586x->irq_domain, irq);
206}
207EXPORT_SYMBOL_GPL(tps6586x_irq_get_virq);
208
Stefan Agnere0a3da82013-12-06 13:51:45 +0100209int tps6586x_get_version(struct device *dev)
210{
211 struct tps6586x *tps6586x = dev_get_drvdata(dev);
212
213 return tps6586x->version;
214}
215EXPORT_SYMBOL_GPL(tps6586x_get_version);
216
Mike Rapoportc6c19332010-08-11 01:11:04 +0200217static int __remove_subdev(struct device *dev, void *unused)
218{
219 platform_device_unregister(to_platform_device(dev));
220 return 0;
221}
222
223static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
224{
225 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
226}
227
Mark Brown96e824b2010-12-12 12:39:28 +0000228static void tps6586x_irq_lock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200229{
Mark Brown96e824b2010-12-12 12:39:28 +0000230 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200231
232 mutex_lock(&tps6586x->irq_lock);
233}
234
Mark Brown96e824b2010-12-12 12:39:28 +0000235static void tps6586x_irq_enable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200236{
Mark Brown96e824b2010-12-12 12:39:28 +0000237 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
Laxman Dewangan605511a2012-11-13 19:18:05 +0530238 unsigned int __irq = irq_data->hwirq;
Gary Kingc26448c2010-09-20 00:18:27 +0200239 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
240
241 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
242 tps6586x->irq_en |= (1 << __irq);
243}
244
Mark Brown96e824b2010-12-12 12:39:28 +0000245static void tps6586x_irq_disable(struct irq_data *irq_data)
Gary Kingc26448c2010-09-20 00:18:27 +0200246{
Mark Brown96e824b2010-12-12 12:39:28 +0000247 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
Gary Kingc26448c2010-09-20 00:18:27 +0200248
Laxman Dewangan605511a2012-11-13 19:18:05 +0530249 unsigned int __irq = irq_data->hwirq;
Gary Kingc26448c2010-09-20 00:18:27 +0200250 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
251
252 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
253 tps6586x->irq_en &= ~(1 << __irq);
254}
255
Mark Brown96e824b2010-12-12 12:39:28 +0000256static void tps6586x_irq_sync_unlock(struct irq_data *data)
Gary Kingc26448c2010-09-20 00:18:27 +0200257{
Mark Brown96e824b2010-12-12 12:39:28 +0000258 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
Gary Kingc26448c2010-09-20 00:18:27 +0200259 int i;
260
261 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530262 int ret;
263 ret = tps6586x_write(tps6586x->dev,
264 TPS6586X_INT_MASK1 + i,
265 tps6586x->mask_reg[i]);
266 WARN_ON(ret);
Gary Kingc26448c2010-09-20 00:18:27 +0200267 }
268
269 mutex_unlock(&tps6586x->irq_lock);
270}
271
Stephen Warren234506a2013-09-06 17:17:13 -0600272#ifdef CONFIG_PM_SLEEP
273static int tps6586x_irq_set_wake(struct irq_data *irq_data, unsigned int on)
274{
275 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
276 return irq_set_irq_wake(tps6586x->irq, on);
277}
278#else
279#define tps6586x_irq_set_wake NULL
280#endif
281
Laxman Dewangan605511a2012-11-13 19:18:05 +0530282static struct irq_chip tps6586x_irq_chip = {
283 .name = "tps6586x",
284 .irq_bus_lock = tps6586x_irq_lock,
285 .irq_bus_sync_unlock = tps6586x_irq_sync_unlock,
286 .irq_disable = tps6586x_irq_disable,
287 .irq_enable = tps6586x_irq_enable,
Stephen Warren234506a2013-09-06 17:17:13 -0600288 .irq_set_wake = tps6586x_irq_set_wake,
Laxman Dewangan605511a2012-11-13 19:18:05 +0530289};
290
291static int tps6586x_irq_map(struct irq_domain *h, unsigned int virq,
292 irq_hw_number_t hw)
293{
294 struct tps6586x *tps6586x = h->host_data;
295
296 irq_set_chip_data(virq, tps6586x);
297 irq_set_chip_and_handler(virq, &tps6586x_irq_chip, handle_simple_irq);
298 irq_set_nested_thread(virq, 1);
Laxman Dewangan605511a2012-11-13 19:18:05 +0530299 irq_set_noprobe(virq);
Laxman Dewangan605511a2012-11-13 19:18:05 +0530300
301 return 0;
302}
303
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +0900304static const struct irq_domain_ops tps6586x_domain_ops = {
Laxman Dewangan605511a2012-11-13 19:18:05 +0530305 .map = tps6586x_irq_map,
306 .xlate = irq_domain_xlate_twocell,
307};
308
Gary Kingc26448c2010-09-20 00:18:27 +0200309static irqreturn_t tps6586x_irq(int irq, void *data)
310{
311 struct tps6586x *tps6586x = data;
312 u32 acks;
313 int ret = 0;
314
315 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
316 sizeof(acks), (uint8_t *)&acks);
317
318 if (ret < 0) {
319 dev_err(tps6586x->dev, "failed to read interrupt status\n");
320 return IRQ_NONE;
321 }
322
323 acks = le32_to_cpu(acks);
324
325 while (acks) {
326 int i = __ffs(acks);
327
328 if (tps6586x->irq_en & (1 << i))
Laxman Dewangan605511a2012-11-13 19:18:05 +0530329 handle_nested_irq(
330 irq_find_mapping(tps6586x->irq_domain, i));
Gary Kingc26448c2010-09-20 00:18:27 +0200331
332 acks &= ~(1 << i);
333 }
334
335 return IRQ_HANDLED;
336}
337
Bill Pembertonf791be42012-11-19 13:23:04 -0500338static int tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
Gary Kingc26448c2010-09-20 00:18:27 +0200339 int irq_base)
340{
341 int i, ret;
342 u8 tmp[4];
Laxman Dewangan605511a2012-11-13 19:18:05 +0530343 int new_irq_base;
344 int irq_num = ARRAY_SIZE(tps6586x_irqs);
Gary Kingc26448c2010-09-20 00:18:27 +0200345
Stephen Warren234506a2013-09-06 17:17:13 -0600346 tps6586x->irq = irq;
347
Gary Kingc26448c2010-09-20 00:18:27 +0200348 mutex_init(&tps6586x->irq_lock);
349 for (i = 0; i < 5; i++) {
Gary Kingc26448c2010-09-20 00:18:27 +0200350 tps6586x->mask_reg[i] = 0xff;
351 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
352 }
353
354 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
355
Laxman Dewangan605511a2012-11-13 19:18:05 +0530356 if (irq_base > 0) {
357 new_irq_base = irq_alloc_descs(irq_base, 0, irq_num, -1);
358 if (new_irq_base < 0) {
359 dev_err(tps6586x->dev,
360 "Failed to alloc IRQs: %d\n", new_irq_base);
361 return new_irq_base;
362 }
363 } else {
364 new_irq_base = 0;
Gary Kingc26448c2010-09-20 00:18:27 +0200365 }
366
Laxman Dewangan605511a2012-11-13 19:18:05 +0530367 tps6586x->irq_domain = irq_domain_add_simple(tps6586x->dev->of_node,
368 irq_num, new_irq_base, &tps6586x_domain_ops,
369 tps6586x);
370 if (!tps6586x->irq_domain) {
371 dev_err(tps6586x->dev, "Failed to create IRQ domain\n");
372 return -ENOMEM;
373 }
Gary Kingc26448c2010-09-20 00:18:27 +0200374 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
375 "tps6586x", tps6586x);
376
Stephen Warren234506a2013-09-06 17:17:13 -0600377 if (!ret)
Gary Kingc26448c2010-09-20 00:18:27 +0200378 device_init_wakeup(tps6586x->dev, 1);
Gary Kingc26448c2010-09-20 00:18:27 +0200379
380 return ret;
381}
382
Bill Pembertonf791be42012-11-19 13:23:04 -0500383static int tps6586x_add_subdevs(struct tps6586x *tps6586x,
Mike Rapoportc6c19332010-08-11 01:11:04 +0200384 struct tps6586x_platform_data *pdata)
385{
386 struct tps6586x_subdev_info *subdev;
387 struct platform_device *pdev;
388 int i, ret = 0;
389
390 for (i = 0; i < pdata->num_subdevs; i++) {
391 subdev = &pdata->subdevs[i];
392
393 pdev = platform_device_alloc(subdev->name, subdev->id);
Axel Lin929980a2010-08-24 13:47:22 +0800394 if (!pdev) {
395 ret = -ENOMEM;
396 goto failed;
397 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200398
399 pdev->dev.parent = tps6586x->dev;
400 pdev->dev.platform_data = subdev->platform_data;
Thierry Reding62f6b082012-04-26 16:52:21 +0200401 pdev->dev.of_node = subdev->of_node;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200402
403 ret = platform_device_add(pdev);
Axel Lin929980a2010-08-24 13:47:22 +0800404 if (ret) {
405 platform_device_put(pdev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200406 goto failed;
Axel Lin929980a2010-08-24 13:47:22 +0800407 }
Mike Rapoportc6c19332010-08-11 01:11:04 +0200408 }
409 return 0;
410
411failed:
412 tps6586x_remove_subdevs(tps6586x);
413 return ret;
414}
415
Thierry Reding62f6b082012-04-26 16:52:21 +0200416#ifdef CONFIG_OF
Thierry Reding62f6b082012-04-26 16:52:21 +0200417static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
418{
Thierry Reding62f6b082012-04-26 16:52:21 +0200419 struct device_node *np = client->dev.of_node;
420 struct tps6586x_platform_data *pdata;
Thierry Reding62f6b082012-04-26 16:52:21 +0200421
422 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
Markus Elfringfeafdff2018-03-08 15:30:54 +0100423 if (!pdata)
Thierry Reding62f6b082012-04-26 16:52:21 +0200424 return NULL;
425
Laxman Dewangan64e48162012-10-18 19:36:09 +0530426 pdata->num_subdevs = 0;
427 pdata->subdevs = NULL;
Thierry Reding62f6b082012-04-26 16:52:21 +0200428 pdata->gpio_base = -1;
429 pdata->irq_base = -1;
Bill Huang004c15a2012-08-19 18:07:55 -0700430 pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
Thierry Reding62f6b082012-04-26 16:52:21 +0200431
432 return pdata;
433}
434
Jingoo Hana58cc842014-05-07 17:23:15 +0900435static const struct of_device_id tps6586x_of_match[] = {
Thierry Reding62f6b082012-04-26 16:52:21 +0200436 { .compatible = "ti,tps6586x", },
437 { },
438};
439#else
440static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
441{
442 return NULL;
443}
444#endif
445
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530446static bool is_volatile_reg(struct device *dev, unsigned int reg)
447{
448 /* Cache all interrupt mask register */
449 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
450 return false;
451
452 return true;
453}
454
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530455static const struct regmap_config tps6586x_regmap_config = {
456 .reg_bits = 8,
457 .val_bits = 8,
Axel Lin088d8622015-07-07 08:52:06 +0800458 .max_register = TPS6586X_MAX_REGISTER,
Laxman Dewangan75edd5a2012-07-18 11:50:47 +0530459 .volatile_reg = is_volatile_reg,
460 .cache_type = REGCACHE_RBTREE,
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530461};
462
Bill Huang004c15a2012-08-19 18:07:55 -0700463static struct device *tps6586x_dev;
464static void tps6586x_power_off(void)
465{
466 if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
467 return;
468
469 tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
470}
471
Stefan Agnere0a3da82013-12-06 13:51:45 +0100472static void tps6586x_print_version(struct i2c_client *client, int version)
473{
474 const char *name;
475
476 switch (version) {
477 case TPS658621A:
478 name = "TPS658621A";
479 break;
480 case TPS658621CD:
481 name = "TPS658621C/D";
482 break;
483 case TPS658623:
484 name = "TPS658623";
485 break;
Alban Bedel6c46ccc2014-05-20 12:14:03 +0200486 case TPS658640:
487 case TPS658640v2:
488 name = "TPS658640";
489 break;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100490 case TPS658643:
491 name = "TPS658643";
492 break;
493 default:
494 name = "TPS6586X";
495 break;
496 }
497
498 dev_info(&client->dev, "Found %s, VERSIONCRC is %02x\n", name, version);
499}
500
Bill Pembertonf791be42012-11-19 13:23:04 -0500501static int tps6586x_i2c_probe(struct i2c_client *client,
Mike Rapoportc6c19332010-08-11 01:11:04 +0200502 const struct i2c_device_id *id)
503{
Jingoo Han334a41ce2013-07-30 17:10:05 +0900504 struct tps6586x_platform_data *pdata = dev_get_platdata(&client->dev);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200505 struct tps6586x *tps6586x;
506 int ret;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100507 int version;
Mike Rapoportc6c19332010-08-11 01:11:04 +0200508
Thierry Reding62f6b082012-04-26 16:52:21 +0200509 if (!pdata && client->dev.of_node)
510 pdata = tps6586x_parse_dt(client);
511
Mike Rapoportc6c19332010-08-11 01:11:04 +0200512 if (!pdata) {
513 dev_err(&client->dev, "tps6586x requires platform data\n");
514 return -ENOTSUPP;
515 }
516
Stefan Agnere0a3da82013-12-06 13:51:45 +0100517 version = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
518 if (version < 0) {
519 dev_err(&client->dev, "Chip ID read failed: %d\n", version);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200520 return -EIO;
521 }
522
Laxman Dewanganb6719412012-07-18 11:50:45 +0530523 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
Stefan Agnere0a3da82013-12-06 13:51:45 +0100524 if (!tps6586x)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200525 return -ENOMEM;
Stefan Agnere0a3da82013-12-06 13:51:45 +0100526
527 tps6586x->version = version;
528 tps6586x_print_version(client, tps6586x->version);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200529
530 tps6586x->client = client;
531 tps6586x->dev = &client->dev;
532 i2c_set_clientdata(client, tps6586x);
533
Laxman Dewangan1176b5b2012-07-18 11:50:46 +0530534 tps6586x->regmap = devm_regmap_init_i2c(client,
535 &tps6586x_regmap_config);
536 if (IS_ERR(tps6586x->regmap)) {
537 ret = PTR_ERR(tps6586x->regmap);
538 dev_err(&client->dev, "regmap init failed: %d\n", ret);
539 return ret;
540 }
541
Mike Rapoportc6c19332010-08-11 01:11:04 +0200542
Gary Kingc26448c2010-09-20 00:18:27 +0200543 if (client->irq) {
544 ret = tps6586x_irq_init(tps6586x, client->irq,
545 pdata->irq_base);
546 if (ret) {
547 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
Laxman Dewanganb6719412012-07-18 11:50:45 +0530548 return ret;
Gary Kingc26448c2010-09-20 00:18:27 +0200549 }
550 }
551
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530552 ret = mfd_add_devices(tps6586x->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800553 tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
Laxman Dewangan5b8b1fe2012-11-13 19:18:06 +0530554 NULL, 0, tps6586x->irq_domain);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530555 if (ret < 0) {
556 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
557 goto err_mfd_add;
Vincent Palatin6f9f13b2011-01-31 11:31:49 -0500558 }
559
Mike Rapoportc6c19332010-08-11 01:11:04 +0200560 ret = tps6586x_add_subdevs(tps6586x, pdata);
561 if (ret) {
562 dev_err(&client->dev, "add devices failed: %d\n", ret);
563 goto err_add_devs;
564 }
565
Bill Huang004c15a2012-08-19 18:07:55 -0700566 if (pdata->pm_off && !pm_power_off) {
567 tps6586x_dev = &client->dev;
568 pm_power_off = tps6586x_power_off;
569 }
570
Mike Rapoportc6c19332010-08-11 01:11:04 +0200571 return 0;
572
573err_add_devs:
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530574 mfd_remove_devices(tps6586x->dev);
575err_mfd_add:
Gary Kingc26448c2010-09-20 00:18:27 +0200576 if (client->irq)
577 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200578 return ret;
579}
580
Bill Pemberton4740f732012-11-19 13:26:01 -0500581static int tps6586x_i2c_remove(struct i2c_client *client)
Mike Rapoportc6c19332010-08-11 01:11:04 +0200582{
Axel Lin4b751cf2010-08-24 15:18:58 +0800583 struct tps6586x *tps6586x = i2c_get_clientdata(client);
Axel Lin4b751cf2010-08-24 15:18:58 +0800584
585 tps6586x_remove_subdevs(tps6586x);
Laxman Dewangan7a7487c2012-07-18 11:50:50 +0530586 mfd_remove_devices(tps6586x->dev);
587 if (client->irq)
588 free_irq(client->irq, tps6586x);
Mike Rapoportc6c19332010-08-11 01:11:04 +0200589 return 0;
590}
591
Jonathan Hunterac4ca4b92018-11-13 08:56:31 +0000592static int __maybe_unused tps6586x_i2c_suspend(struct device *dev)
593{
594 struct tps6586x *tps6586x = dev_get_drvdata(dev);
595
596 if (tps6586x->client->irq)
597 disable_irq(tps6586x->client->irq);
598
599 return 0;
600}
601
602static int __maybe_unused tps6586x_i2c_resume(struct device *dev)
603{
604 struct tps6586x *tps6586x = dev_get_drvdata(dev);
605
606 if (tps6586x->client->irq)
607 enable_irq(tps6586x->client->irq);
608
609 return 0;
610}
611
612static SIMPLE_DEV_PM_OPS(tps6586x_pm_ops, tps6586x_i2c_suspend,
613 tps6586x_i2c_resume);
614
Mike Rapoportc6c19332010-08-11 01:11:04 +0200615static const struct i2c_device_id tps6586x_id_table[] = {
616 { "tps6586x", 0 },
617 { },
618};
619MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
620
621static struct i2c_driver tps6586x_driver = {
622 .driver = {
623 .name = "tps6586x",
Thierry Reding62f6b082012-04-26 16:52:21 +0200624 .of_match_table = of_match_ptr(tps6586x_of_match),
Jonathan Hunterac4ca4b92018-11-13 08:56:31 +0000625 .pm = &tps6586x_pm_ops,
Mike Rapoportc6c19332010-08-11 01:11:04 +0200626 },
627 .probe = tps6586x_i2c_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500628 .remove = tps6586x_i2c_remove,
Mike Rapoportc6c19332010-08-11 01:11:04 +0200629 .id_table = tps6586x_id_table,
630};
631
632static int __init tps6586x_init(void)
633{
634 return i2c_add_driver(&tps6586x_driver);
635}
636subsys_initcall(tps6586x_init);
637
638static void __exit tps6586x_exit(void)
639{
640 i2c_del_driver(&tps6586x_driver);
641}
642module_exit(tps6586x_exit);
643
644MODULE_DESCRIPTION("TPS6586X core driver");
645MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
646MODULE_LICENSE("GPL");