xref: /openbmc/linux/drivers/mfd/tc3589x.c (revision 1383e00f79a7bd6333083a12b43481b6bf2bdcb4)
1f4e8afdcSSundar Iyer /*
2f4e8afdcSSundar Iyer  * Copyright (C) ST-Ericsson SA 2010
3f4e8afdcSSundar Iyer  *
4f4e8afdcSSundar Iyer  * License Terms: GNU General Public License, version 2
5f4e8afdcSSundar Iyer  * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
6f4e8afdcSSundar Iyer  * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
7f4e8afdcSSundar Iyer  */
8f4e8afdcSSundar Iyer 
9f4e8afdcSSundar Iyer #include <linux/module.h>
10f4e8afdcSSundar Iyer #include <linux/interrupt.h>
11f4e8afdcSSundar Iyer #include <linux/irq.h>
1215e27b10SLee Jones #include <linux/irqdomain.h>
13f4e8afdcSSundar Iyer #include <linux/slab.h>
14f4e8afdcSSundar Iyer #include <linux/i2c.h>
15a435ae1dSLee Jones #include <linux/of.h>
16f4e8afdcSSundar Iyer #include <linux/mfd/core.h>
17f4e8afdcSSundar Iyer #include <linux/mfd/tc3589x.h>
18f4e8afdcSSundar Iyer 
19593e9d70SSundar Iyer #define TC3589x_CLKMODE_MODCTL_SLEEP		0x0
20593e9d70SSundar Iyer #define TC3589x_CLKMODE_MODCTL_OPERATION	(1 << 0)
21593e9d70SSundar Iyer 
22f4e8afdcSSundar Iyer /**
2320406ebfSSundar Iyer  * tc3589x_reg_read() - read a single TC3589x register
2420406ebfSSundar Iyer  * @tc3589x:	Device to read from
25f4e8afdcSSundar Iyer  * @reg:	Register to read
26f4e8afdcSSundar Iyer  */
2720406ebfSSundar Iyer int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
28f4e8afdcSSundar Iyer {
29f4e8afdcSSundar Iyer 	int ret;
30f4e8afdcSSundar Iyer 
3120406ebfSSundar Iyer 	ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
32f4e8afdcSSundar Iyer 	if (ret < 0)
3320406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
34f4e8afdcSSundar Iyer 			reg, ret);
35f4e8afdcSSundar Iyer 
36f4e8afdcSSundar Iyer 	return ret;
37f4e8afdcSSundar Iyer }
3820406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_reg_read);
39f4e8afdcSSundar Iyer 
40f4e8afdcSSundar Iyer /**
4120406ebfSSundar Iyer  * tc3589x_reg_read() - write a single TC3589x register
4220406ebfSSundar Iyer  * @tc3589x:	Device to write to
43f4e8afdcSSundar Iyer  * @reg:	Register to read
44f4e8afdcSSundar Iyer  * @data:	Value to write
45f4e8afdcSSundar Iyer  */
4620406ebfSSundar Iyer int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
47f4e8afdcSSundar Iyer {
48f4e8afdcSSundar Iyer 	int ret;
49f4e8afdcSSundar Iyer 
5020406ebfSSundar Iyer 	ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
51f4e8afdcSSundar Iyer 	if (ret < 0)
5220406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
53f4e8afdcSSundar Iyer 			reg, ret);
54f4e8afdcSSundar Iyer 
55f4e8afdcSSundar Iyer 	return ret;
56f4e8afdcSSundar Iyer }
5720406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_reg_write);
58f4e8afdcSSundar Iyer 
59f4e8afdcSSundar Iyer /**
6020406ebfSSundar Iyer  * tc3589x_block_read() - read multiple TC3589x registers
6120406ebfSSundar Iyer  * @tc3589x:	Device to read from
62f4e8afdcSSundar Iyer  * @reg:	First register
63f4e8afdcSSundar Iyer  * @length:	Number of registers
64f4e8afdcSSundar Iyer  * @values:	Buffer to write to
65f4e8afdcSSundar Iyer  */
6620406ebfSSundar Iyer int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
67f4e8afdcSSundar Iyer {
68f4e8afdcSSundar Iyer 	int ret;
69f4e8afdcSSundar Iyer 
7020406ebfSSundar Iyer 	ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
71f4e8afdcSSundar Iyer 	if (ret < 0)
7220406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
73f4e8afdcSSundar Iyer 			reg, ret);
74f4e8afdcSSundar Iyer 
75f4e8afdcSSundar Iyer 	return ret;
76f4e8afdcSSundar Iyer }
7720406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_block_read);
78f4e8afdcSSundar Iyer 
79f4e8afdcSSundar Iyer /**
8020406ebfSSundar Iyer  * tc3589x_block_write() - write multiple TC3589x registers
8120406ebfSSundar Iyer  * @tc3589x:	Device to write to
82f4e8afdcSSundar Iyer  * @reg:	First register
83f4e8afdcSSundar Iyer  * @length:	Number of registers
84f4e8afdcSSundar Iyer  * @values:	Values to write
85f4e8afdcSSundar Iyer  */
8620406ebfSSundar Iyer int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
87f4e8afdcSSundar Iyer 			const u8 *values)
88f4e8afdcSSundar Iyer {
89f4e8afdcSSundar Iyer 	int ret;
90f4e8afdcSSundar Iyer 
9120406ebfSSundar Iyer 	ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
92f4e8afdcSSundar Iyer 					     values);
93f4e8afdcSSundar Iyer 	if (ret < 0)
9420406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
95f4e8afdcSSundar Iyer 			reg, ret);
96f4e8afdcSSundar Iyer 
97f4e8afdcSSundar Iyer 	return ret;
98f4e8afdcSSundar Iyer }
9920406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_block_write);
100f4e8afdcSSundar Iyer 
101f4e8afdcSSundar Iyer /**
10220406ebfSSundar Iyer  * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
10320406ebfSSundar Iyer  * @tc3589x:	Device to write to
104f4e8afdcSSundar Iyer  * @reg:	Register to write
105f4e8afdcSSundar Iyer  * @mask:	Mask of bits to set
106f4e8afdcSSundar Iyer  * @values:	Value to set
107f4e8afdcSSundar Iyer  */
10820406ebfSSundar Iyer int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
109f4e8afdcSSundar Iyer {
110f4e8afdcSSundar Iyer 	int ret;
111f4e8afdcSSundar Iyer 
11220406ebfSSundar Iyer 	mutex_lock(&tc3589x->lock);
113f4e8afdcSSundar Iyer 
11420406ebfSSundar Iyer 	ret = tc3589x_reg_read(tc3589x, reg);
115f4e8afdcSSundar Iyer 	if (ret < 0)
116f4e8afdcSSundar Iyer 		goto out;
117f4e8afdcSSundar Iyer 
118f4e8afdcSSundar Iyer 	ret &= ~mask;
119f4e8afdcSSundar Iyer 	ret |= val;
120f4e8afdcSSundar Iyer 
12120406ebfSSundar Iyer 	ret = tc3589x_reg_write(tc3589x, reg, ret);
122f4e8afdcSSundar Iyer 
123f4e8afdcSSundar Iyer out:
12420406ebfSSundar Iyer 	mutex_unlock(&tc3589x->lock);
125f4e8afdcSSundar Iyer 	return ret;
126f4e8afdcSSundar Iyer }
12720406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_set_bits);
128f4e8afdcSSundar Iyer 
129f4e8afdcSSundar Iyer static struct resource gpio_resources[] = {
130f4e8afdcSSundar Iyer 	{
13120406ebfSSundar Iyer 		.start	= TC3589x_INT_GPIIRQ,
13220406ebfSSundar Iyer 		.end	= TC3589x_INT_GPIIRQ,
133f4e8afdcSSundar Iyer 		.flags	= IORESOURCE_IRQ,
134f4e8afdcSSundar Iyer 	},
135f4e8afdcSSundar Iyer };
136f4e8afdcSSundar Iyer 
13709c730a4SSundar Iyer static struct resource keypad_resources[] = {
13809c730a4SSundar Iyer 	{
13909c730a4SSundar Iyer 		.start  = TC3589x_INT_KBDIRQ,
14009c730a4SSundar Iyer 		.end    = TC3589x_INT_KBDIRQ,
14109c730a4SSundar Iyer 		.flags  = IORESOURCE_IRQ,
14209c730a4SSundar Iyer 	},
14309c730a4SSundar Iyer };
14409c730a4SSundar Iyer 
145611b7590SSundar Iyer static struct mfd_cell tc3589x_dev_gpio[] = {
146f4e8afdcSSundar Iyer 	{
14720406ebfSSundar Iyer 		.name		= "tc3589x-gpio",
148f4e8afdcSSundar Iyer 		.num_resources	= ARRAY_SIZE(gpio_resources),
149f4e8afdcSSundar Iyer 		.resources	= &gpio_resources[0],
150a435ae1dSLee Jones 		.of_compatible	= "tc3589x-gpio",
151f4e8afdcSSundar Iyer 	},
152f4e8afdcSSundar Iyer };
153f4e8afdcSSundar Iyer 
15409c730a4SSundar Iyer static struct mfd_cell tc3589x_dev_keypad[] = {
15509c730a4SSundar Iyer 	{
15609c730a4SSundar Iyer 		.name           = "tc3589x-keypad",
15709c730a4SSundar Iyer 		.num_resources  = ARRAY_SIZE(keypad_resources),
15809c730a4SSundar Iyer 		.resources      = &keypad_resources[0],
159a435ae1dSLee Jones 		.of_compatible	= "tc3589x-keypad",
16009c730a4SSundar Iyer 	},
16109c730a4SSundar Iyer };
16209c730a4SSundar Iyer 
16320406ebfSSundar Iyer static irqreturn_t tc3589x_irq(int irq, void *data)
164f4e8afdcSSundar Iyer {
16520406ebfSSundar Iyer 	struct tc3589x *tc3589x = data;
166f4e8afdcSSundar Iyer 	int status;
167f4e8afdcSSundar Iyer 
168bd77efd0SSundar Iyer again:
16920406ebfSSundar Iyer 	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
170f4e8afdcSSundar Iyer 	if (status < 0)
171f4e8afdcSSundar Iyer 		return IRQ_NONE;
172f4e8afdcSSundar Iyer 
173f4e8afdcSSundar Iyer 	while (status) {
174f4e8afdcSSundar Iyer 		int bit = __ffs(status);
17515e27b10SLee Jones 		int virq = irq_create_mapping(tc3589x->domain, bit);
176f4e8afdcSSundar Iyer 
17715e27b10SLee Jones 		handle_nested_irq(virq);
178f4e8afdcSSundar Iyer 		status &= ~(1 << bit);
179f4e8afdcSSundar Iyer 	}
180f4e8afdcSSundar Iyer 
181f4e8afdcSSundar Iyer 	/*
182f4e8afdcSSundar Iyer 	 * A dummy read or write (to any register) appears to be necessary to
183f4e8afdcSSundar Iyer 	 * have the last interrupt clear (for example, GPIO IC write) take
184bd77efd0SSundar Iyer 	 * effect. In such a case, recheck for any interrupt which is still
185bd77efd0SSundar Iyer 	 * pending.
186f4e8afdcSSundar Iyer 	 */
187bd77efd0SSundar Iyer 	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
188bd77efd0SSundar Iyer 	if (status)
189bd77efd0SSundar Iyer 		goto again;
190f4e8afdcSSundar Iyer 
191f4e8afdcSSundar Iyer 	return IRQ_HANDLED;
192f4e8afdcSSundar Iyer }
193f4e8afdcSSundar Iyer 
19415e27b10SLee Jones static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
19515e27b10SLee Jones 				irq_hw_number_t hwirq)
196f4e8afdcSSundar Iyer {
19715e27b10SLee Jones 	struct tc3589x *tc3589x = d->host_data;
198f4e8afdcSSundar Iyer 
19915e27b10SLee Jones 	irq_set_chip_data(virq, tc3589x);
20015e27b10SLee Jones 	irq_set_chip_and_handler(virq, &dummy_irq_chip,
201f4e8afdcSSundar Iyer 				handle_edge_irq);
20215e27b10SLee Jones 	irq_set_nested_thread(virq, 1);
203f4e8afdcSSundar Iyer #ifdef CONFIG_ARM
20415e27b10SLee Jones 	set_irq_flags(virq, IRQF_VALID);
205f4e8afdcSSundar Iyer #else
20615e27b10SLee Jones 	irq_set_noprobe(virq);
207f4e8afdcSSundar Iyer #endif
208f4e8afdcSSundar Iyer 
209f4e8afdcSSundar Iyer 	return 0;
210f4e8afdcSSundar Iyer }
211f4e8afdcSSundar Iyer 
21215e27b10SLee Jones static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
21315e27b10SLee Jones {
21415e27b10SLee Jones #ifdef CONFIG_ARM
21515e27b10SLee Jones 	set_irq_flags(virq, 0);
21615e27b10SLee Jones #endif
21715e27b10SLee Jones 	irq_set_chip_and_handler(virq, NULL, NULL);
21815e27b10SLee Jones 	irq_set_chip_data(virq, NULL);
21915e27b10SLee Jones }
22015e27b10SLee Jones 
22115e27b10SLee Jones static struct irq_domain_ops tc3589x_irq_ops = {
22215e27b10SLee Jones 	.map    = tc3589x_irq_map,
22315e27b10SLee Jones 	.unmap  = tc3589x_irq_unmap,
22415e27b10SLee Jones 	.xlate  = irq_domain_xlate_twocell,
22515e27b10SLee Jones };
22615e27b10SLee Jones 
227a435ae1dSLee Jones static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
228f4e8afdcSSundar Iyer {
22920406ebfSSundar Iyer 	int base = tc3589x->irq_base;
230f4e8afdcSSundar Iyer 
2311f0529b4SLinus Walleij 	tc3589x->domain = irq_domain_add_simple(
2321f0529b4SLinus Walleij 		np, TC3589x_NR_INTERNAL_IRQS, base,
23315e27b10SLee Jones 		&tc3589x_irq_ops, tc3589x);
23415e27b10SLee Jones 
23515e27b10SLee Jones 	if (!tc3589x->domain) {
23615e27b10SLee Jones 		dev_err(tc3589x->dev, "Failed to create irqdomain\n");
23715e27b10SLee Jones 		return -ENOSYS;
23815e27b10SLee Jones 	}
23915e27b10SLee Jones 
24015e27b10SLee Jones 	return 0;
241f4e8afdcSSundar Iyer }
242f4e8afdcSSundar Iyer 
24320406ebfSSundar Iyer static int tc3589x_chip_init(struct tc3589x *tc3589x)
244f4e8afdcSSundar Iyer {
245f4e8afdcSSundar Iyer 	int manf, ver, ret;
246f4e8afdcSSundar Iyer 
24720406ebfSSundar Iyer 	manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
248f4e8afdcSSundar Iyer 	if (manf < 0)
249f4e8afdcSSundar Iyer 		return manf;
250f4e8afdcSSundar Iyer 
25120406ebfSSundar Iyer 	ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
252f4e8afdcSSundar Iyer 	if (ver < 0)
253f4e8afdcSSundar Iyer 		return ver;
254f4e8afdcSSundar Iyer 
25520406ebfSSundar Iyer 	if (manf != TC3589x_MANFCODE_MAGIC) {
25620406ebfSSundar Iyer 		dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
257f4e8afdcSSundar Iyer 		return -EINVAL;
258f4e8afdcSSundar Iyer 	}
259f4e8afdcSSundar Iyer 
26020406ebfSSundar Iyer 	dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
261f4e8afdcSSundar Iyer 
262523bc382SSundar Iyer 	/*
263523bc382SSundar Iyer 	 * Put everything except the IRQ module into reset;
264523bc382SSundar Iyer 	 * also spare the GPIO module for any pin initialization
265523bc382SSundar Iyer 	 * done during pre-kernel boot
266523bc382SSundar Iyer 	 */
26720406ebfSSundar Iyer 	ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
26820406ebfSSundar Iyer 				TC3589x_RSTCTRL_TIMRST
26920406ebfSSundar Iyer 				| TC3589x_RSTCTRL_ROTRST
270523bc382SSundar Iyer 				| TC3589x_RSTCTRL_KBDRST);
271f4e8afdcSSundar Iyer 	if (ret < 0)
272f4e8afdcSSundar Iyer 		return ret;
273f4e8afdcSSundar Iyer 
274f4e8afdcSSundar Iyer 	/* Clear the reset interrupt. */
27520406ebfSSundar Iyer 	return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
276f4e8afdcSSundar Iyer }
277f4e8afdcSSundar Iyer 
278f791be49SBill Pemberton static int tc3589x_device_init(struct tc3589x *tc3589x)
279611b7590SSundar Iyer {
280611b7590SSundar Iyer 	int ret = 0;
281611b7590SSundar Iyer 	unsigned int blocks = tc3589x->pdata->block;
282611b7590SSundar Iyer 
283611b7590SSundar Iyer 	if (blocks & TC3589x_BLOCK_GPIO) {
284611b7590SSundar Iyer 		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
285611b7590SSundar Iyer 				      ARRAY_SIZE(tc3589x_dev_gpio), NULL,
28615e27b10SLee Jones 				      tc3589x->irq_base, tc3589x->domain);
287611b7590SSundar Iyer 		if (ret) {
288611b7590SSundar Iyer 			dev_err(tc3589x->dev, "failed to add gpio child\n");
289611b7590SSundar Iyer 			return ret;
290611b7590SSundar Iyer 		}
291611b7590SSundar Iyer 		dev_info(tc3589x->dev, "added gpio block\n");
292611b7590SSundar Iyer 	}
293611b7590SSundar Iyer 
29409c730a4SSundar Iyer 	if (blocks & TC3589x_BLOCK_KEYPAD) {
29509c730a4SSundar Iyer 		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
29609c730a4SSundar Iyer 				      ARRAY_SIZE(tc3589x_dev_keypad), NULL,
29715e27b10SLee Jones 				      tc3589x->irq_base, tc3589x->domain);
29809c730a4SSundar Iyer 		if (ret) {
29909c730a4SSundar Iyer 			dev_err(tc3589x->dev, "failed to keypad child\n");
300611b7590SSundar Iyer 			return ret;
30109c730a4SSundar Iyer 		}
30209c730a4SSundar Iyer 		dev_info(tc3589x->dev, "added keypad block\n");
30309c730a4SSundar Iyer 	}
304611b7590SSundar Iyer 
30509c730a4SSundar Iyer 	return ret;
306611b7590SSundar Iyer }
307611b7590SSundar Iyer 
308a435ae1dSLee Jones static int tc3589x_of_probe(struct device_node *np,
309a435ae1dSLee Jones 			struct tc3589x_platform_data *pdata)
310a435ae1dSLee Jones {
311a435ae1dSLee Jones 	struct device_node *child;
312a435ae1dSLee Jones 
313a435ae1dSLee Jones 	for_each_child_of_node(np, child) {
314a435ae1dSLee Jones 		if (!strcmp(child->name, "tc3589x_gpio")) {
315a435ae1dSLee Jones 			pdata->block |= TC3589x_BLOCK_GPIO;
316a435ae1dSLee Jones 		}
317a435ae1dSLee Jones 		if (!strcmp(child->name, "tc3589x_keypad")) {
318a435ae1dSLee Jones 			pdata->block |= TC3589x_BLOCK_KEYPAD;
319a435ae1dSLee Jones 		}
320a435ae1dSLee Jones 	}
321a435ae1dSLee Jones 
322a435ae1dSLee Jones 	return 0;
323a435ae1dSLee Jones }
324a435ae1dSLee Jones 
325f791be49SBill Pemberton static int tc3589x_probe(struct i2c_client *i2c,
326f4e8afdcSSundar Iyer 				   const struct i2c_device_id *id)
327f4e8afdcSSundar Iyer {
32820406ebfSSundar Iyer 	struct tc3589x_platform_data *pdata = i2c->dev.platform_data;
329a435ae1dSLee Jones 	struct device_node *np = i2c->dev.of_node;
33020406ebfSSundar Iyer 	struct tc3589x *tc3589x;
331f4e8afdcSSundar Iyer 	int ret;
332f4e8afdcSSundar Iyer 
333a435ae1dSLee Jones 	if (!pdata) {
334a435ae1dSLee Jones 		if (np) {
335a435ae1dSLee Jones 			pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
336a435ae1dSLee Jones 			if (!pdata)
337a435ae1dSLee Jones 				return -ENOMEM;
338a435ae1dSLee Jones 
339a435ae1dSLee Jones 			ret = tc3589x_of_probe(np, pdata);
340a435ae1dSLee Jones 			if (ret)
341a435ae1dSLee Jones 				return ret;
342a435ae1dSLee Jones 		}
343a435ae1dSLee Jones 		else {
344a435ae1dSLee Jones 			dev_err(&i2c->dev, "No platform data or DT found\n");
345a435ae1dSLee Jones 			return -EINVAL;
346a435ae1dSLee Jones 		}
347a435ae1dSLee Jones 	}
348a435ae1dSLee Jones 
349f4e8afdcSSundar Iyer 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
350f4e8afdcSSundar Iyer 				     | I2C_FUNC_SMBUS_I2C_BLOCK))
351f4e8afdcSSundar Iyer 		return -EIO;
352f4e8afdcSSundar Iyer 
353*1383e00fSJingoo Han 	tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
354*1383e00fSJingoo Han 				GFP_KERNEL);
35520406ebfSSundar Iyer 	if (!tc3589x)
356f4e8afdcSSundar Iyer 		return -ENOMEM;
357f4e8afdcSSundar Iyer 
35820406ebfSSundar Iyer 	mutex_init(&tc3589x->lock);
359f4e8afdcSSundar Iyer 
36020406ebfSSundar Iyer 	tc3589x->dev = &i2c->dev;
36120406ebfSSundar Iyer 	tc3589x->i2c = i2c;
36220406ebfSSundar Iyer 	tc3589x->pdata = pdata;
36320406ebfSSundar Iyer 	tc3589x->irq_base = pdata->irq_base;
36420406ebfSSundar Iyer 	tc3589x->num_gpio = id->driver_data;
365f4e8afdcSSundar Iyer 
36620406ebfSSundar Iyer 	i2c_set_clientdata(i2c, tc3589x);
367f4e8afdcSSundar Iyer 
36820406ebfSSundar Iyer 	ret = tc3589x_chip_init(tc3589x);
369f4e8afdcSSundar Iyer 	if (ret)
370*1383e00fSJingoo Han 		return ret;
371f4e8afdcSSundar Iyer 
372a435ae1dSLee Jones 	ret = tc3589x_irq_init(tc3589x, np);
373f4e8afdcSSundar Iyer 	if (ret)
374*1383e00fSJingoo Han 		return ret;
375f4e8afdcSSundar Iyer 
37620406ebfSSundar Iyer 	ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
377f4e8afdcSSundar Iyer 				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
37820406ebfSSundar Iyer 				   "tc3589x", tc3589x);
379f4e8afdcSSundar Iyer 	if (ret) {
38020406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
381*1383e00fSJingoo Han 		return ret;
382f4e8afdcSSundar Iyer 	}
383f4e8afdcSSundar Iyer 
384611b7590SSundar Iyer 	ret = tc3589x_device_init(tc3589x);
385f4e8afdcSSundar Iyer 	if (ret) {
386611b7590SSundar Iyer 		dev_err(tc3589x->dev, "failed to add child devices\n");
387*1383e00fSJingoo Han 		return ret;
388f4e8afdcSSundar Iyer 	}
389f4e8afdcSSundar Iyer 
390f4e8afdcSSundar Iyer 	return 0;
391f4e8afdcSSundar Iyer }
392f4e8afdcSSundar Iyer 
3934740f73fSBill Pemberton static int tc3589x_remove(struct i2c_client *client)
394f4e8afdcSSundar Iyer {
39520406ebfSSundar Iyer 	struct tc3589x *tc3589x = i2c_get_clientdata(client);
396f4e8afdcSSundar Iyer 
39720406ebfSSundar Iyer 	mfd_remove_devices(tc3589x->dev);
398f4e8afdcSSundar Iyer 
399f4e8afdcSSundar Iyer 	return 0;
400f4e8afdcSSundar Iyer }
401f4e8afdcSSundar Iyer 
402930bf022SAxel Lin #ifdef CONFIG_PM_SLEEP
403593e9d70SSundar Iyer static int tc3589x_suspend(struct device *dev)
404593e9d70SSundar Iyer {
405593e9d70SSundar Iyer 	struct tc3589x *tc3589x = dev_get_drvdata(dev);
406593e9d70SSundar Iyer 	struct i2c_client *client = tc3589x->i2c;
407593e9d70SSundar Iyer 	int ret = 0;
408593e9d70SSundar Iyer 
409593e9d70SSundar Iyer 	/* put the system to sleep mode */
410593e9d70SSundar Iyer 	if (!device_may_wakeup(&client->dev))
411593e9d70SSundar Iyer 		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
412593e9d70SSundar Iyer 				TC3589x_CLKMODE_MODCTL_SLEEP);
413593e9d70SSundar Iyer 
414593e9d70SSundar Iyer 	return ret;
415593e9d70SSundar Iyer }
416593e9d70SSundar Iyer 
417593e9d70SSundar Iyer static int tc3589x_resume(struct device *dev)
418593e9d70SSundar Iyer {
419593e9d70SSundar Iyer 	struct tc3589x *tc3589x = dev_get_drvdata(dev);
420593e9d70SSundar Iyer 	struct i2c_client *client = tc3589x->i2c;
421593e9d70SSundar Iyer 	int ret = 0;
422593e9d70SSundar Iyer 
423593e9d70SSundar Iyer 	/* enable the system into operation */
424593e9d70SSundar Iyer 	if (!device_may_wakeup(&client->dev))
425593e9d70SSundar Iyer 		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
426593e9d70SSundar Iyer 				TC3589x_CLKMODE_MODCTL_OPERATION);
427593e9d70SSundar Iyer 
428593e9d70SSundar Iyer 	return ret;
429593e9d70SSundar Iyer }
43054d8e2c3SLinus Walleij #endif
431593e9d70SSundar Iyer 
432930bf022SAxel Lin static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
433930bf022SAxel Lin 
43420406ebfSSundar Iyer static const struct i2c_device_id tc3589x_id[] = {
43520406ebfSSundar Iyer 	{ "tc3589x", 24 },
436f4e8afdcSSundar Iyer 	{ }
437f4e8afdcSSundar Iyer };
43820406ebfSSundar Iyer MODULE_DEVICE_TABLE(i2c, tc3589x_id);
439f4e8afdcSSundar Iyer 
44020406ebfSSundar Iyer static struct i2c_driver tc3589x_driver = {
44120406ebfSSundar Iyer 	.driver.name	= "tc3589x",
442f4e8afdcSSundar Iyer 	.driver.owner	= THIS_MODULE,
443593e9d70SSundar Iyer 	.driver.pm	= &tc3589x_dev_pm_ops,
44420406ebfSSundar Iyer 	.probe		= tc3589x_probe,
44584449216SBill Pemberton 	.remove		= tc3589x_remove,
44620406ebfSSundar Iyer 	.id_table	= tc3589x_id,
447f4e8afdcSSundar Iyer };
448f4e8afdcSSundar Iyer 
44920406ebfSSundar Iyer static int __init tc3589x_init(void)
450f4e8afdcSSundar Iyer {
45120406ebfSSundar Iyer 	return i2c_add_driver(&tc3589x_driver);
452f4e8afdcSSundar Iyer }
45320406ebfSSundar Iyer subsys_initcall(tc3589x_init);
454f4e8afdcSSundar Iyer 
45520406ebfSSundar Iyer static void __exit tc3589x_exit(void)
456f4e8afdcSSundar Iyer {
45720406ebfSSundar Iyer 	i2c_del_driver(&tc3589x_driver);
458f4e8afdcSSundar Iyer }
45920406ebfSSundar Iyer module_exit(tc3589x_exit);
460f4e8afdcSSundar Iyer 
461f4e8afdcSSundar Iyer MODULE_LICENSE("GPL v2");
46220406ebfSSundar Iyer MODULE_DESCRIPTION("TC3589x MFD core driver");
463f4e8afdcSSundar Iyer MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");
464