xref: /openbmc/linux/drivers/mfd/tc3589x.c (revision a2ce0002)
11f67b599SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f4e8afdcSSundar Iyer /*
3f4e8afdcSSundar Iyer  * Copyright (C) ST-Ericsson SA 2010
4f4e8afdcSSundar Iyer  *
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>
16a381b13eSLinus Walleij #include <linux/of_device.h>
17f4e8afdcSSundar Iyer #include <linux/mfd/core.h>
18f4e8afdcSSundar Iyer #include <linux/mfd/tc3589x.h>
19a381b13eSLinus Walleij #include <linux/err.h>
20f4e8afdcSSundar Iyer 
210dfae4a3SLee Jones /*
22e64c1eb4SLinus Walleij  * enum tc3589x_version - indicates the TC3589x version
23e64c1eb4SLinus Walleij  */
24e64c1eb4SLinus Walleij enum tc3589x_version {
25e64c1eb4SLinus Walleij 	TC3589X_TC35890,
26e64c1eb4SLinus Walleij 	TC3589X_TC35892,
27e64c1eb4SLinus Walleij 	TC3589X_TC35893,
28e64c1eb4SLinus Walleij 	TC3589X_TC35894,
29e64c1eb4SLinus Walleij 	TC3589X_TC35895,
30e64c1eb4SLinus Walleij 	TC3589X_TC35896,
31e64c1eb4SLinus Walleij 	TC3589X_UNKNOWN,
32e64c1eb4SLinus Walleij };
33e64c1eb4SLinus Walleij 
34593e9d70SSundar Iyer #define TC3589x_CLKMODE_MODCTL_SLEEP		0x0
35593e9d70SSundar Iyer #define TC3589x_CLKMODE_MODCTL_OPERATION	(1 << 0)
36593e9d70SSundar Iyer 
37f4e8afdcSSundar Iyer /**
3820406ebfSSundar Iyer  * tc3589x_reg_read() - read a single TC3589x register
3920406ebfSSundar Iyer  * @tc3589x:	Device to read from
40f4e8afdcSSundar Iyer  * @reg:	Register to read
41f4e8afdcSSundar Iyer  */
tc3589x_reg_read(struct tc3589x * tc3589x,u8 reg)4220406ebfSSundar Iyer int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
43f4e8afdcSSundar Iyer {
44f4e8afdcSSundar Iyer 	int ret;
45f4e8afdcSSundar Iyer 
4620406ebfSSundar Iyer 	ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
47f4e8afdcSSundar Iyer 	if (ret < 0)
4820406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
49f4e8afdcSSundar Iyer 			reg, ret);
50f4e8afdcSSundar Iyer 
51f4e8afdcSSundar Iyer 	return ret;
52f4e8afdcSSundar Iyer }
5320406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_reg_read);
54f4e8afdcSSundar Iyer 
55f4e8afdcSSundar Iyer /**
56d87814a3SJulia Lawall  * tc3589x_reg_write() - write a single TC3589x register
5720406ebfSSundar Iyer  * @tc3589x:	Device to write to
58f4e8afdcSSundar Iyer  * @reg:	Register to read
59f4e8afdcSSundar Iyer  * @data:	Value to write
60f4e8afdcSSundar Iyer  */
tc3589x_reg_write(struct tc3589x * tc3589x,u8 reg,u8 data)6120406ebfSSundar Iyer int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
62f4e8afdcSSundar Iyer {
63f4e8afdcSSundar Iyer 	int ret;
64f4e8afdcSSundar Iyer 
6520406ebfSSundar Iyer 	ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
66f4e8afdcSSundar Iyer 	if (ret < 0)
6720406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
68f4e8afdcSSundar Iyer 			reg, ret);
69f4e8afdcSSundar Iyer 
70f4e8afdcSSundar Iyer 	return ret;
71f4e8afdcSSundar Iyer }
7220406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_reg_write);
73f4e8afdcSSundar Iyer 
74f4e8afdcSSundar Iyer /**
7520406ebfSSundar Iyer  * tc3589x_block_read() - read multiple TC3589x registers
7620406ebfSSundar Iyer  * @tc3589x:	Device to read from
77f4e8afdcSSundar Iyer  * @reg:	First register
78f4e8afdcSSundar Iyer  * @length:	Number of registers
79f4e8afdcSSundar Iyer  * @values:	Buffer to write to
80f4e8afdcSSundar Iyer  */
tc3589x_block_read(struct tc3589x * tc3589x,u8 reg,u8 length,u8 * values)8120406ebfSSundar Iyer int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
82f4e8afdcSSundar Iyer {
83f4e8afdcSSundar Iyer 	int ret;
84f4e8afdcSSundar Iyer 
8520406ebfSSundar Iyer 	ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
86f4e8afdcSSundar Iyer 	if (ret < 0)
8720406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
88f4e8afdcSSundar Iyer 			reg, ret);
89f4e8afdcSSundar Iyer 
90f4e8afdcSSundar Iyer 	return ret;
91f4e8afdcSSundar Iyer }
9220406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_block_read);
93f4e8afdcSSundar Iyer 
94f4e8afdcSSundar Iyer /**
9520406ebfSSundar Iyer  * tc3589x_block_write() - write multiple TC3589x registers
9620406ebfSSundar Iyer  * @tc3589x:	Device to write to
97f4e8afdcSSundar Iyer  * @reg:	First register
98f4e8afdcSSundar Iyer  * @length:	Number of registers
99f4e8afdcSSundar Iyer  * @values:	Values to write
100f4e8afdcSSundar Iyer  */
tc3589x_block_write(struct tc3589x * tc3589x,u8 reg,u8 length,const u8 * values)10120406ebfSSundar Iyer int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
102f4e8afdcSSundar Iyer 			const u8 *values)
103f4e8afdcSSundar Iyer {
104f4e8afdcSSundar Iyer 	int ret;
105f4e8afdcSSundar Iyer 
10620406ebfSSundar Iyer 	ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
107f4e8afdcSSundar Iyer 					     values);
108f4e8afdcSSundar Iyer 	if (ret < 0)
10920406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
110f4e8afdcSSundar Iyer 			reg, ret);
111f4e8afdcSSundar Iyer 
112f4e8afdcSSundar Iyer 	return ret;
113f4e8afdcSSundar Iyer }
11420406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_block_write);
115f4e8afdcSSundar Iyer 
116f4e8afdcSSundar Iyer /**
11720406ebfSSundar Iyer  * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
11820406ebfSSundar Iyer  * @tc3589x:	Device to write to
119f4e8afdcSSundar Iyer  * @reg:	Register to write
120f4e8afdcSSundar Iyer  * @mask:	Mask of bits to set
121d87814a3SJulia Lawall  * @val:	Value to set
122f4e8afdcSSundar Iyer  */
tc3589x_set_bits(struct tc3589x * tc3589x,u8 reg,u8 mask,u8 val)12320406ebfSSundar Iyer int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
124f4e8afdcSSundar Iyer {
125f4e8afdcSSundar Iyer 	int ret;
126f4e8afdcSSundar Iyer 
12720406ebfSSundar Iyer 	mutex_lock(&tc3589x->lock);
128f4e8afdcSSundar Iyer 
12920406ebfSSundar Iyer 	ret = tc3589x_reg_read(tc3589x, reg);
130f4e8afdcSSundar Iyer 	if (ret < 0)
131f4e8afdcSSundar Iyer 		goto out;
132f4e8afdcSSundar Iyer 
133f4e8afdcSSundar Iyer 	ret &= ~mask;
134f4e8afdcSSundar Iyer 	ret |= val;
135f4e8afdcSSundar Iyer 
13620406ebfSSundar Iyer 	ret = tc3589x_reg_write(tc3589x, reg, ret);
137f4e8afdcSSundar Iyer 
138f4e8afdcSSundar Iyer out:
13920406ebfSSundar Iyer 	mutex_unlock(&tc3589x->lock);
140f4e8afdcSSundar Iyer 	return ret;
141f4e8afdcSSundar Iyer }
14220406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_set_bits);
143f4e8afdcSSundar Iyer 
144c4a164f4SRikard Falkeborn static const struct resource gpio_resources[] = {
145f4e8afdcSSundar Iyer 	{
14620406ebfSSundar Iyer 		.start	= TC3589x_INT_GPIIRQ,
14720406ebfSSundar Iyer 		.end	= TC3589x_INT_GPIIRQ,
148f4e8afdcSSundar Iyer 		.flags	= IORESOURCE_IRQ,
149f4e8afdcSSundar Iyer 	},
150f4e8afdcSSundar Iyer };
151f4e8afdcSSundar Iyer 
152c4a164f4SRikard Falkeborn static const struct resource keypad_resources[] = {
15309c730a4SSundar Iyer 	{
15409c730a4SSundar Iyer 		.start  = TC3589x_INT_KBDIRQ,
15509c730a4SSundar Iyer 		.end    = TC3589x_INT_KBDIRQ,
15609c730a4SSundar Iyer 		.flags  = IORESOURCE_IRQ,
15709c730a4SSundar Iyer 	},
15809c730a4SSundar Iyer };
15909c730a4SSundar Iyer 
160afb580a9SGeert Uytterhoeven static const struct mfd_cell tc3589x_dev_gpio[] = {
161f4e8afdcSSundar Iyer 	{
16220406ebfSSundar Iyer 		.name		= "tc3589x-gpio",
163f4e8afdcSSundar Iyer 		.num_resources	= ARRAY_SIZE(gpio_resources),
164f4e8afdcSSundar Iyer 		.resources	= &gpio_resources[0],
165a381b13eSLinus Walleij 		.of_compatible	= "toshiba,tc3589x-gpio",
166f4e8afdcSSundar Iyer 	},
167f4e8afdcSSundar Iyer };
168f4e8afdcSSundar Iyer 
169afb580a9SGeert Uytterhoeven static const struct mfd_cell tc3589x_dev_keypad[] = {
17009c730a4SSundar Iyer 	{
17109c730a4SSundar Iyer 		.name           = "tc3589x-keypad",
17209c730a4SSundar Iyer 		.num_resources  = ARRAY_SIZE(keypad_resources),
17309c730a4SSundar Iyer 		.resources      = &keypad_resources[0],
174a381b13eSLinus Walleij 		.of_compatible	= "toshiba,tc3589x-keypad",
17509c730a4SSundar Iyer 	},
17609c730a4SSundar Iyer };
17709c730a4SSundar Iyer 
tc3589x_irq(int irq,void * data)17820406ebfSSundar Iyer static irqreturn_t tc3589x_irq(int irq, void *data)
179f4e8afdcSSundar Iyer {
18020406ebfSSundar Iyer 	struct tc3589x *tc3589x = data;
181f4e8afdcSSundar Iyer 	int status;
182f4e8afdcSSundar Iyer 
183bd77efd0SSundar Iyer again:
18420406ebfSSundar Iyer 	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
185f4e8afdcSSundar Iyer 	if (status < 0)
186f4e8afdcSSundar Iyer 		return IRQ_NONE;
187f4e8afdcSSundar Iyer 
188f4e8afdcSSundar Iyer 	while (status) {
189f4e8afdcSSundar Iyer 		int bit = __ffs(status);
1909ff80e2dSMarc Zyngier 		int virq = irq_find_mapping(tc3589x->domain, bit);
191f4e8afdcSSundar Iyer 
19215e27b10SLee Jones 		handle_nested_irq(virq);
193f4e8afdcSSundar Iyer 		status &= ~(1 << bit);
194f4e8afdcSSundar Iyer 	}
195f4e8afdcSSundar Iyer 
196f4e8afdcSSundar Iyer 	/*
197f4e8afdcSSundar Iyer 	 * A dummy read or write (to any register) appears to be necessary to
198f4e8afdcSSundar Iyer 	 * have the last interrupt clear (for example, GPIO IC write) take
199bd77efd0SSundar Iyer 	 * effect. In such a case, recheck for any interrupt which is still
200bd77efd0SSundar Iyer 	 * pending.
201f4e8afdcSSundar Iyer 	 */
202bd77efd0SSundar Iyer 	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
203bd77efd0SSundar Iyer 	if (status)
204bd77efd0SSundar Iyer 		goto again;
205f4e8afdcSSundar Iyer 
206f4e8afdcSSundar Iyer 	return IRQ_HANDLED;
207f4e8afdcSSundar Iyer }
208f4e8afdcSSundar Iyer 
tc3589x_irq_map(struct irq_domain * d,unsigned int virq,irq_hw_number_t hwirq)20915e27b10SLee Jones static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
21015e27b10SLee Jones 				irq_hw_number_t hwirq)
211f4e8afdcSSundar Iyer {
21215e27b10SLee Jones 	struct tc3589x *tc3589x = d->host_data;
213f4e8afdcSSundar Iyer 
21415e27b10SLee Jones 	irq_set_chip_data(virq, tc3589x);
21515e27b10SLee Jones 	irq_set_chip_and_handler(virq, &dummy_irq_chip,
216f4e8afdcSSundar Iyer 				handle_edge_irq);
21715e27b10SLee Jones 	irq_set_nested_thread(virq, 1);
21815e27b10SLee Jones 	irq_set_noprobe(virq);
219f4e8afdcSSundar Iyer 
220f4e8afdcSSundar Iyer 	return 0;
221f4e8afdcSSundar Iyer }
222f4e8afdcSSundar Iyer 
tc3589x_irq_unmap(struct irq_domain * d,unsigned int virq)22315e27b10SLee Jones static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
22415e27b10SLee Jones {
22515e27b10SLee Jones 	irq_set_chip_and_handler(virq, NULL, NULL);
22615e27b10SLee Jones 	irq_set_chip_data(virq, NULL);
22715e27b10SLee Jones }
22815e27b10SLee Jones 
2297ce7b26fSKrzysztof Kozlowski static const struct irq_domain_ops tc3589x_irq_ops = {
23015e27b10SLee Jones 	.map    = tc3589x_irq_map,
23115e27b10SLee Jones 	.unmap  = tc3589x_irq_unmap,
232627918edSLinus Walleij 	.xlate  = irq_domain_xlate_onecell,
23315e27b10SLee Jones };
23415e27b10SLee Jones 
tc3589x_irq_init(struct tc3589x * tc3589x,struct device_node * np)235a435ae1dSLee Jones static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
236f4e8afdcSSundar Iyer {
2371f0529b4SLinus Walleij 	tc3589x->domain = irq_domain_add_simple(
23890f2d0f7SLinus Walleij 		np, TC3589x_NR_INTERNAL_IRQS, 0,
23915e27b10SLee Jones 		&tc3589x_irq_ops, tc3589x);
24015e27b10SLee Jones 
24115e27b10SLee Jones 	if (!tc3589x->domain) {
24215e27b10SLee Jones 		dev_err(tc3589x->dev, "Failed to create irqdomain\n");
24315e27b10SLee Jones 		return -ENOSYS;
24415e27b10SLee Jones 	}
24515e27b10SLee Jones 
24615e27b10SLee Jones 	return 0;
247f4e8afdcSSundar Iyer }
248f4e8afdcSSundar Iyer 
tc3589x_chip_init(struct tc3589x * tc3589x)24920406ebfSSundar Iyer static int tc3589x_chip_init(struct tc3589x *tc3589x)
250f4e8afdcSSundar Iyer {
251f4e8afdcSSundar Iyer 	int manf, ver, ret;
252f4e8afdcSSundar Iyer 
25320406ebfSSundar Iyer 	manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
254f4e8afdcSSundar Iyer 	if (manf < 0)
255f4e8afdcSSundar Iyer 		return manf;
256f4e8afdcSSundar Iyer 
25720406ebfSSundar Iyer 	ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
258f4e8afdcSSundar Iyer 	if (ver < 0)
259f4e8afdcSSundar Iyer 		return ver;
260f4e8afdcSSundar Iyer 
26120406ebfSSundar Iyer 	if (manf != TC3589x_MANFCODE_MAGIC) {
26220406ebfSSundar Iyer 		dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
263f4e8afdcSSundar Iyer 		return -EINVAL;
264f4e8afdcSSundar Iyer 	}
265f4e8afdcSSundar Iyer 
26620406ebfSSundar Iyer 	dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
267f4e8afdcSSundar Iyer 
268523bc382SSundar Iyer 	/*
269523bc382SSundar Iyer 	 * Put everything except the IRQ module into reset;
270523bc382SSundar Iyer 	 * also spare the GPIO module for any pin initialization
271523bc382SSundar Iyer 	 * done during pre-kernel boot
272523bc382SSundar Iyer 	 */
27320406ebfSSundar Iyer 	ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
27420406ebfSSundar Iyer 				TC3589x_RSTCTRL_TIMRST
27520406ebfSSundar Iyer 				| TC3589x_RSTCTRL_ROTRST
276523bc382SSundar Iyer 				| TC3589x_RSTCTRL_KBDRST);
277f4e8afdcSSundar Iyer 	if (ret < 0)
278f4e8afdcSSundar Iyer 		return ret;
279f4e8afdcSSundar Iyer 
280f4e8afdcSSundar Iyer 	/* Clear the reset interrupt. */
28120406ebfSSundar Iyer 	return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
282f4e8afdcSSundar Iyer }
283f4e8afdcSSundar Iyer 
tc3589x_device_init(struct tc3589x * tc3589x)284f791be49SBill Pemberton static int tc3589x_device_init(struct tc3589x *tc3589x)
285611b7590SSundar Iyer {
286611b7590SSundar Iyer 	int ret = 0;
287611b7590SSundar Iyer 	unsigned int blocks = tc3589x->pdata->block;
288611b7590SSundar Iyer 
289611b7590SSundar Iyer 	if (blocks & TC3589x_BLOCK_GPIO) {
290611b7590SSundar Iyer 		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
291611b7590SSundar Iyer 				      ARRAY_SIZE(tc3589x_dev_gpio), NULL,
29290f2d0f7SLinus Walleij 				      0, tc3589x->domain);
293611b7590SSundar Iyer 		if (ret) {
294611b7590SSundar Iyer 			dev_err(tc3589x->dev, "failed to add gpio child\n");
295611b7590SSundar Iyer 			return ret;
296611b7590SSundar Iyer 		}
297611b7590SSundar Iyer 		dev_info(tc3589x->dev, "added gpio block\n");
298611b7590SSundar Iyer 	}
299611b7590SSundar Iyer 
30009c730a4SSundar Iyer 	if (blocks & TC3589x_BLOCK_KEYPAD) {
30109c730a4SSundar Iyer 		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
30209c730a4SSundar Iyer 				      ARRAY_SIZE(tc3589x_dev_keypad), NULL,
30390f2d0f7SLinus Walleij 				      0, tc3589x->domain);
30409c730a4SSundar Iyer 		if (ret) {
30509c730a4SSundar Iyer 			dev_err(tc3589x->dev, "failed to keypad child\n");
306611b7590SSundar Iyer 			return ret;
30709c730a4SSundar Iyer 		}
30809c730a4SSundar Iyer 		dev_info(tc3589x->dev, "added keypad block\n");
30909c730a4SSundar Iyer 	}
310611b7590SSundar Iyer 
31109c730a4SSundar Iyer 	return ret;
312611b7590SSundar Iyer }
313611b7590SSundar Iyer 
314a381b13eSLinus Walleij static const struct of_device_id tc3589x_match[] = {
315a381b13eSLinus Walleij 	/* Legacy compatible string */
316a381b13eSLinus Walleij 	{ .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
317a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
318a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
319a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
320a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
321a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
322a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
323a381b13eSLinus Walleij 	{ }
324a381b13eSLinus Walleij };
325a381b13eSLinus Walleij 
326a381b13eSLinus Walleij MODULE_DEVICE_TABLE(of, tc3589x_match);
327a381b13eSLinus Walleij 
328a381b13eSLinus Walleij static struct tc3589x_platform_data *
tc3589x_of_probe(struct device * dev,enum tc3589x_version * version)329a381b13eSLinus Walleij tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
330a435ae1dSLee Jones {
331a381b13eSLinus Walleij 	struct device_node *np = dev->of_node;
332a381b13eSLinus Walleij 	struct tc3589x_platform_data *pdata;
333a435ae1dSLee Jones 	struct device_node *child;
334a381b13eSLinus Walleij 	const struct of_device_id *of_id;
335a381b13eSLinus Walleij 
336a381b13eSLinus Walleij 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
337a381b13eSLinus Walleij 	if (!pdata)
338a381b13eSLinus Walleij 		return ERR_PTR(-ENOMEM);
339a381b13eSLinus Walleij 
340a381b13eSLinus Walleij 	of_id = of_match_device(tc3589x_match, dev);
341a381b13eSLinus Walleij 	if (!of_id)
342a381b13eSLinus Walleij 		return ERR_PTR(-ENODEV);
343*a2ce0002SKrzysztof Kozlowski 	*version = (uintptr_t) of_id->data;
344a435ae1dSLee Jones 
345a435ae1dSLee Jones 	for_each_child_of_node(np, child) {
346a381b13eSLinus Walleij 		if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
347a435ae1dSLee Jones 			pdata->block |= TC3589x_BLOCK_GPIO;
348a381b13eSLinus Walleij 		if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
349a435ae1dSLee Jones 			pdata->block |= TC3589x_BLOCK_KEYPAD;
350a435ae1dSLee Jones 	}
351a435ae1dSLee Jones 
352a381b13eSLinus Walleij 	return pdata;
353a435ae1dSLee Jones }
354a435ae1dSLee Jones 
tc3589x_probe(struct i2c_client * i2c)355d28fa288SUwe Kleine-König static int tc3589x_probe(struct i2c_client *i2c)
356f4e8afdcSSundar Iyer {
357d28fa288SUwe Kleine-König 	const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
358a435ae1dSLee Jones 	struct device_node *np = i2c->dev.of_node;
359a381b13eSLinus Walleij 	struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
36020406ebfSSundar Iyer 	struct tc3589x *tc3589x;
361a381b13eSLinus Walleij 	enum tc3589x_version version;
362f4e8afdcSSundar Iyer 	int ret;
363f4e8afdcSSundar Iyer 
364a435ae1dSLee Jones 	if (!pdata) {
365a381b13eSLinus Walleij 		pdata = tc3589x_of_probe(&i2c->dev, &version);
366a381b13eSLinus Walleij 		if (IS_ERR(pdata)) {
367a435ae1dSLee Jones 			dev_err(&i2c->dev, "No platform data or DT found\n");
368a381b13eSLinus Walleij 			return PTR_ERR(pdata);
369a435ae1dSLee Jones 		}
370a381b13eSLinus Walleij 	} else {
371a381b13eSLinus Walleij 		/* When not probing from device tree we have this ID */
372a381b13eSLinus Walleij 		version = id->driver_data;
373a435ae1dSLee Jones 	}
374a435ae1dSLee Jones 
375f4e8afdcSSundar Iyer 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
376f4e8afdcSSundar Iyer 				     | I2C_FUNC_SMBUS_I2C_BLOCK))
377f4e8afdcSSundar Iyer 		return -EIO;
378f4e8afdcSSundar Iyer 
3791383e00fSJingoo Han 	tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
3801383e00fSJingoo Han 				GFP_KERNEL);
38120406ebfSSundar Iyer 	if (!tc3589x)
382f4e8afdcSSundar Iyer 		return -ENOMEM;
383f4e8afdcSSundar Iyer 
38420406ebfSSundar Iyer 	mutex_init(&tc3589x->lock);
385f4e8afdcSSundar Iyer 
38620406ebfSSundar Iyer 	tc3589x->dev = &i2c->dev;
38720406ebfSSundar Iyer 	tc3589x->i2c = i2c;
38820406ebfSSundar Iyer 	tc3589x->pdata = pdata;
389e64c1eb4SLinus Walleij 
390a381b13eSLinus Walleij 	switch (version) {
391e64c1eb4SLinus Walleij 	case TC3589X_TC35893:
392e64c1eb4SLinus Walleij 	case TC3589X_TC35895:
393e64c1eb4SLinus Walleij 	case TC3589X_TC35896:
394e64c1eb4SLinus Walleij 		tc3589x->num_gpio = 20;
395e64c1eb4SLinus Walleij 		break;
396e64c1eb4SLinus Walleij 	case TC3589X_TC35890:
397e64c1eb4SLinus Walleij 	case TC3589X_TC35892:
398e64c1eb4SLinus Walleij 	case TC3589X_TC35894:
399e64c1eb4SLinus Walleij 	case TC3589X_UNKNOWN:
400e64c1eb4SLinus Walleij 	default:
401e64c1eb4SLinus Walleij 		tc3589x->num_gpio = 24;
402e64c1eb4SLinus Walleij 		break;
403e64c1eb4SLinus Walleij 	}
404f4e8afdcSSundar Iyer 
40520406ebfSSundar Iyer 	i2c_set_clientdata(i2c, tc3589x);
406f4e8afdcSSundar Iyer 
40720406ebfSSundar Iyer 	ret = tc3589x_chip_init(tc3589x);
408f4e8afdcSSundar Iyer 	if (ret)
4091383e00fSJingoo Han 		return ret;
410f4e8afdcSSundar Iyer 
411a435ae1dSLee Jones 	ret = tc3589x_irq_init(tc3589x, np);
412f4e8afdcSSundar Iyer 	if (ret)
4131383e00fSJingoo Han 		return ret;
414f4e8afdcSSundar Iyer 
41520406ebfSSundar Iyer 	ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
416f4e8afdcSSundar Iyer 				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
41720406ebfSSundar Iyer 				   "tc3589x", tc3589x);
418f4e8afdcSSundar Iyer 	if (ret) {
41920406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
4201383e00fSJingoo Han 		return ret;
421f4e8afdcSSundar Iyer 	}
422f4e8afdcSSundar Iyer 
423611b7590SSundar Iyer 	ret = tc3589x_device_init(tc3589x);
424f4e8afdcSSundar Iyer 	if (ret) {
425611b7590SSundar Iyer 		dev_err(tc3589x->dev, "failed to add child devices\n");
4261383e00fSJingoo Han 		return ret;
427f4e8afdcSSundar Iyer 	}
428f4e8afdcSSundar Iyer 
429f4e8afdcSSundar Iyer 	return 0;
430f4e8afdcSSundar Iyer }
431f4e8afdcSSundar Iyer 
tc3589x_remove(struct i2c_client * client)432ed5c2f5fSUwe Kleine-König static void tc3589x_remove(struct i2c_client *client)
433f4e8afdcSSundar Iyer {
43420406ebfSSundar Iyer 	struct tc3589x *tc3589x = i2c_get_clientdata(client);
435f4e8afdcSSundar Iyer 
43620406ebfSSundar Iyer 	mfd_remove_devices(tc3589x->dev);
437f4e8afdcSSundar Iyer }
438f4e8afdcSSundar Iyer 
tc3589x_suspend(struct device * dev)439593e9d70SSundar Iyer static int tc3589x_suspend(struct device *dev)
440593e9d70SSundar Iyer {
441593e9d70SSundar Iyer 	struct tc3589x *tc3589x = dev_get_drvdata(dev);
442593e9d70SSundar Iyer 	struct i2c_client *client = tc3589x->i2c;
443593e9d70SSundar Iyer 	int ret = 0;
444593e9d70SSundar Iyer 
445593e9d70SSundar Iyer 	/* put the system to sleep mode */
446593e9d70SSundar Iyer 	if (!device_may_wakeup(&client->dev))
447593e9d70SSundar Iyer 		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
448593e9d70SSundar Iyer 				TC3589x_CLKMODE_MODCTL_SLEEP);
449593e9d70SSundar Iyer 
450593e9d70SSundar Iyer 	return ret;
451593e9d70SSundar Iyer }
452593e9d70SSundar Iyer 
tc3589x_resume(struct device * dev)453593e9d70SSundar Iyer static int tc3589x_resume(struct device *dev)
454593e9d70SSundar Iyer {
455593e9d70SSundar Iyer 	struct tc3589x *tc3589x = dev_get_drvdata(dev);
456593e9d70SSundar Iyer 	struct i2c_client *client = tc3589x->i2c;
457593e9d70SSundar Iyer 	int ret = 0;
458593e9d70SSundar Iyer 
459593e9d70SSundar Iyer 	/* enable the system into operation */
460593e9d70SSundar Iyer 	if (!device_may_wakeup(&client->dev))
461593e9d70SSundar Iyer 		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
462593e9d70SSundar Iyer 				TC3589x_CLKMODE_MODCTL_OPERATION);
463593e9d70SSundar Iyer 
464593e9d70SSundar Iyer 	return ret;
465593e9d70SSundar Iyer }
466593e9d70SSundar Iyer 
46702313a90SPaul Cercueil static DEFINE_SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops,
46802313a90SPaul Cercueil 				tc3589x_suspend, tc3589x_resume);
469930bf022SAxel Lin 
47020406ebfSSundar Iyer static const struct i2c_device_id tc3589x_id[] = {
471e64c1eb4SLinus Walleij 	{ "tc35890", TC3589X_TC35890 },
472e64c1eb4SLinus Walleij 	{ "tc35892", TC3589X_TC35892 },
473e64c1eb4SLinus Walleij 	{ "tc35893", TC3589X_TC35893 },
474e64c1eb4SLinus Walleij 	{ "tc35894", TC3589X_TC35894 },
475e64c1eb4SLinus Walleij 	{ "tc35895", TC3589X_TC35895 },
476e64c1eb4SLinus Walleij 	{ "tc35896", TC3589X_TC35896 },
477e64c1eb4SLinus Walleij 	{ "tc3589x", TC3589X_UNKNOWN },
478f4e8afdcSSundar Iyer 	{ }
479f4e8afdcSSundar Iyer };
48020406ebfSSundar Iyer MODULE_DEVICE_TABLE(i2c, tc3589x_id);
481f4e8afdcSSundar Iyer 
48220406ebfSSundar Iyer static struct i2c_driver tc3589x_driver = {
483a381b13eSLinus Walleij 	.driver = {
484a381b13eSLinus Walleij 		.name	= "tc3589x",
48502313a90SPaul Cercueil 		.pm	= pm_sleep_ptr(&tc3589x_dev_pm_ops),
4868e922937SRuan Jinjie 		.of_match_table = tc3589x_match,
487a381b13eSLinus Walleij 	},
4889816d859SUwe Kleine-König 	.probe		= tc3589x_probe,
48984449216SBill Pemberton 	.remove		= tc3589x_remove,
49020406ebfSSundar Iyer 	.id_table	= tc3589x_id,
491f4e8afdcSSundar Iyer };
492f4e8afdcSSundar Iyer 
tc3589x_init(void)49320406ebfSSundar Iyer static int __init tc3589x_init(void)
494f4e8afdcSSundar Iyer {
49520406ebfSSundar Iyer 	return i2c_add_driver(&tc3589x_driver);
496f4e8afdcSSundar Iyer }
49720406ebfSSundar Iyer subsys_initcall(tc3589x_init);
498f4e8afdcSSundar Iyer 
tc3589x_exit(void)49920406ebfSSundar Iyer static void __exit tc3589x_exit(void)
500f4e8afdcSSundar Iyer {
50120406ebfSSundar Iyer 	i2c_del_driver(&tc3589x_driver);
502f4e8afdcSSundar Iyer }
50320406ebfSSundar Iyer module_exit(tc3589x_exit);
504f4e8afdcSSundar Iyer 
50520406ebfSSundar Iyer MODULE_DESCRIPTION("TC3589x MFD core driver");
506f4e8afdcSSundar Iyer MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");
507