xref: /openbmc/linux/drivers/mfd/tc3589x.c (revision 90f2d0f7bf069b1a2798156b7dcc8e7d1e874406)
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>
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 
21e64c1eb4SLinus Walleij /**
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  */
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 /**
5620406ebfSSundar Iyer  * tc3589x_reg_read() - 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  */
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  */
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  */
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
121f4e8afdcSSundar Iyer  * @values:	Value to set
122f4e8afdcSSundar Iyer  */
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 
144f4e8afdcSSundar Iyer static 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 
15209c730a4SSundar Iyer static 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 
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);
19015e27b10SLee Jones 		int virq = irq_create_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 
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);
218f4e8afdcSSundar Iyer #ifdef CONFIG_ARM
21915e27b10SLee Jones 	set_irq_flags(virq, IRQF_VALID);
220f4e8afdcSSundar Iyer #else
22115e27b10SLee Jones 	irq_set_noprobe(virq);
222f4e8afdcSSundar Iyer #endif
223f4e8afdcSSundar Iyer 
224f4e8afdcSSundar Iyer 	return 0;
225f4e8afdcSSundar Iyer }
226f4e8afdcSSundar Iyer 
22715e27b10SLee Jones static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
22815e27b10SLee Jones {
22915e27b10SLee Jones #ifdef CONFIG_ARM
23015e27b10SLee Jones 	set_irq_flags(virq, 0);
23115e27b10SLee Jones #endif
23215e27b10SLee Jones 	irq_set_chip_and_handler(virq, NULL, NULL);
23315e27b10SLee Jones 	irq_set_chip_data(virq, NULL);
23415e27b10SLee Jones }
23515e27b10SLee Jones 
23615e27b10SLee Jones static struct irq_domain_ops tc3589x_irq_ops = {
23715e27b10SLee Jones 	.map    = tc3589x_irq_map,
23815e27b10SLee Jones 	.unmap  = tc3589x_irq_unmap,
239627918edSLinus Walleij 	.xlate  = irq_domain_xlate_onecell,
24015e27b10SLee Jones };
24115e27b10SLee Jones 
242a435ae1dSLee Jones static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
243f4e8afdcSSundar Iyer {
2441f0529b4SLinus Walleij 	tc3589x->domain = irq_domain_add_simple(
245*90f2d0f7SLinus Walleij 		np, TC3589x_NR_INTERNAL_IRQS, 0,
24615e27b10SLee Jones 		&tc3589x_irq_ops, tc3589x);
24715e27b10SLee Jones 
24815e27b10SLee Jones 	if (!tc3589x->domain) {
24915e27b10SLee Jones 		dev_err(tc3589x->dev, "Failed to create irqdomain\n");
25015e27b10SLee Jones 		return -ENOSYS;
25115e27b10SLee Jones 	}
25215e27b10SLee Jones 
25315e27b10SLee Jones 	return 0;
254f4e8afdcSSundar Iyer }
255f4e8afdcSSundar Iyer 
25620406ebfSSundar Iyer static int tc3589x_chip_init(struct tc3589x *tc3589x)
257f4e8afdcSSundar Iyer {
258f4e8afdcSSundar Iyer 	int manf, ver, ret;
259f4e8afdcSSundar Iyer 
26020406ebfSSundar Iyer 	manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
261f4e8afdcSSundar Iyer 	if (manf < 0)
262f4e8afdcSSundar Iyer 		return manf;
263f4e8afdcSSundar Iyer 
26420406ebfSSundar Iyer 	ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
265f4e8afdcSSundar Iyer 	if (ver < 0)
266f4e8afdcSSundar Iyer 		return ver;
267f4e8afdcSSundar Iyer 
26820406ebfSSundar Iyer 	if (manf != TC3589x_MANFCODE_MAGIC) {
26920406ebfSSundar Iyer 		dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
270f4e8afdcSSundar Iyer 		return -EINVAL;
271f4e8afdcSSundar Iyer 	}
272f4e8afdcSSundar Iyer 
27320406ebfSSundar Iyer 	dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
274f4e8afdcSSundar Iyer 
275523bc382SSundar Iyer 	/*
276523bc382SSundar Iyer 	 * Put everything except the IRQ module into reset;
277523bc382SSundar Iyer 	 * also spare the GPIO module for any pin initialization
278523bc382SSundar Iyer 	 * done during pre-kernel boot
279523bc382SSundar Iyer 	 */
28020406ebfSSundar Iyer 	ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
28120406ebfSSundar Iyer 				TC3589x_RSTCTRL_TIMRST
28220406ebfSSundar Iyer 				| TC3589x_RSTCTRL_ROTRST
283523bc382SSundar Iyer 				| TC3589x_RSTCTRL_KBDRST);
284f4e8afdcSSundar Iyer 	if (ret < 0)
285f4e8afdcSSundar Iyer 		return ret;
286f4e8afdcSSundar Iyer 
287f4e8afdcSSundar Iyer 	/* Clear the reset interrupt. */
28820406ebfSSundar Iyer 	return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
289f4e8afdcSSundar Iyer }
290f4e8afdcSSundar Iyer 
291f791be49SBill Pemberton static int tc3589x_device_init(struct tc3589x *tc3589x)
292611b7590SSundar Iyer {
293611b7590SSundar Iyer 	int ret = 0;
294611b7590SSundar Iyer 	unsigned int blocks = tc3589x->pdata->block;
295611b7590SSundar Iyer 
296611b7590SSundar Iyer 	if (blocks & TC3589x_BLOCK_GPIO) {
297611b7590SSundar Iyer 		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
298611b7590SSundar Iyer 				      ARRAY_SIZE(tc3589x_dev_gpio), NULL,
299*90f2d0f7SLinus Walleij 				      0, tc3589x->domain);
300611b7590SSundar Iyer 		if (ret) {
301611b7590SSundar Iyer 			dev_err(tc3589x->dev, "failed to add gpio child\n");
302611b7590SSundar Iyer 			return ret;
303611b7590SSundar Iyer 		}
304611b7590SSundar Iyer 		dev_info(tc3589x->dev, "added gpio block\n");
305611b7590SSundar Iyer 	}
306611b7590SSundar Iyer 
30709c730a4SSundar Iyer 	if (blocks & TC3589x_BLOCK_KEYPAD) {
30809c730a4SSundar Iyer 		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
30909c730a4SSundar Iyer 				      ARRAY_SIZE(tc3589x_dev_keypad), NULL,
310*90f2d0f7SLinus Walleij 				      0, tc3589x->domain);
31109c730a4SSundar Iyer 		if (ret) {
31209c730a4SSundar Iyer 			dev_err(tc3589x->dev, "failed to keypad child\n");
313611b7590SSundar Iyer 			return ret;
31409c730a4SSundar Iyer 		}
31509c730a4SSundar Iyer 		dev_info(tc3589x->dev, "added keypad block\n");
31609c730a4SSundar Iyer 	}
317611b7590SSundar Iyer 
31809c730a4SSundar Iyer 	return ret;
319611b7590SSundar Iyer }
320611b7590SSundar Iyer 
321a381b13eSLinus Walleij #ifdef CONFIG_OF
322a381b13eSLinus Walleij static const struct of_device_id tc3589x_match[] = {
323a381b13eSLinus Walleij 	/* Legacy compatible string */
324a381b13eSLinus Walleij 	{ .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
325a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
326a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
327a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
328a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
329a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
330a381b13eSLinus Walleij 	{ .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
331a381b13eSLinus Walleij 	{ }
332a381b13eSLinus Walleij };
333a381b13eSLinus Walleij 
334a381b13eSLinus Walleij MODULE_DEVICE_TABLE(of, tc3589x_match);
335a381b13eSLinus Walleij 
336a381b13eSLinus Walleij static struct tc3589x_platform_data *
337a381b13eSLinus Walleij tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
338a435ae1dSLee Jones {
339a381b13eSLinus Walleij 	struct device_node *np = dev->of_node;
340a381b13eSLinus Walleij 	struct tc3589x_platform_data *pdata;
341a435ae1dSLee Jones 	struct device_node *child;
342a381b13eSLinus Walleij 	const struct of_device_id *of_id;
343a381b13eSLinus Walleij 
344a381b13eSLinus Walleij 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
345a381b13eSLinus Walleij 	if (!pdata)
346a381b13eSLinus Walleij 		return ERR_PTR(-ENOMEM);
347a381b13eSLinus Walleij 
348a381b13eSLinus Walleij 	of_id = of_match_device(tc3589x_match, dev);
349a381b13eSLinus Walleij 	if (!of_id)
350a381b13eSLinus Walleij 		return ERR_PTR(-ENODEV);
351a381b13eSLinus Walleij 	*version = (enum tc3589x_version) of_id->data;
352a435ae1dSLee Jones 
353a435ae1dSLee Jones 	for_each_child_of_node(np, child) {
354a381b13eSLinus Walleij 		if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
355a435ae1dSLee Jones 			pdata->block |= TC3589x_BLOCK_GPIO;
356a381b13eSLinus Walleij 		if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
357a435ae1dSLee Jones 			pdata->block |= TC3589x_BLOCK_KEYPAD;
358a435ae1dSLee Jones 	}
359a435ae1dSLee Jones 
360a381b13eSLinus Walleij 	return pdata;
361a435ae1dSLee Jones }
362a381b13eSLinus Walleij #else
363a381b13eSLinus Walleij static inline struct tc3589x_platform_data *
364a381b13eSLinus Walleij tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
365a381b13eSLinus Walleij {
366a381b13eSLinus Walleij 	dev_err(dev, "no device tree support\n");
367a381b13eSLinus Walleij 	return ERR_PTR(-ENODEV);
368a381b13eSLinus Walleij }
369a381b13eSLinus Walleij #endif
370a435ae1dSLee Jones 
371f791be49SBill Pemberton static int tc3589x_probe(struct i2c_client *i2c,
372f4e8afdcSSundar Iyer 				   const struct i2c_device_id *id)
373f4e8afdcSSundar Iyer {
374a435ae1dSLee Jones 	struct device_node *np = i2c->dev.of_node;
375a381b13eSLinus Walleij 	struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
37620406ebfSSundar Iyer 	struct tc3589x *tc3589x;
377a381b13eSLinus Walleij 	enum tc3589x_version version;
378f4e8afdcSSundar Iyer 	int ret;
379f4e8afdcSSundar Iyer 
380a435ae1dSLee Jones 	if (!pdata) {
381a381b13eSLinus Walleij 		pdata = tc3589x_of_probe(&i2c->dev, &version);
382a381b13eSLinus Walleij 		if (IS_ERR(pdata)) {
383a435ae1dSLee Jones 			dev_err(&i2c->dev, "No platform data or DT found\n");
384a381b13eSLinus Walleij 			return PTR_ERR(pdata);
385a435ae1dSLee Jones 		}
386a381b13eSLinus Walleij 	} else {
387a381b13eSLinus Walleij 		/* When not probing from device tree we have this ID */
388a381b13eSLinus Walleij 		version = id->driver_data;
389a435ae1dSLee Jones 	}
390a435ae1dSLee Jones 
391f4e8afdcSSundar Iyer 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
392f4e8afdcSSundar Iyer 				     | I2C_FUNC_SMBUS_I2C_BLOCK))
393f4e8afdcSSundar Iyer 		return -EIO;
394f4e8afdcSSundar Iyer 
3951383e00fSJingoo Han 	tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
3961383e00fSJingoo Han 				GFP_KERNEL);
39720406ebfSSundar Iyer 	if (!tc3589x)
398f4e8afdcSSundar Iyer 		return -ENOMEM;
399f4e8afdcSSundar Iyer 
40020406ebfSSundar Iyer 	mutex_init(&tc3589x->lock);
401f4e8afdcSSundar Iyer 
40220406ebfSSundar Iyer 	tc3589x->dev = &i2c->dev;
40320406ebfSSundar Iyer 	tc3589x->i2c = i2c;
40420406ebfSSundar Iyer 	tc3589x->pdata = pdata;
405e64c1eb4SLinus Walleij 
406a381b13eSLinus Walleij 	switch (version) {
407e64c1eb4SLinus Walleij 	case TC3589X_TC35893:
408e64c1eb4SLinus Walleij 	case TC3589X_TC35895:
409e64c1eb4SLinus Walleij 	case TC3589X_TC35896:
410e64c1eb4SLinus Walleij 		tc3589x->num_gpio = 20;
411e64c1eb4SLinus Walleij 		break;
412e64c1eb4SLinus Walleij 	case TC3589X_TC35890:
413e64c1eb4SLinus Walleij 	case TC3589X_TC35892:
414e64c1eb4SLinus Walleij 	case TC3589X_TC35894:
415e64c1eb4SLinus Walleij 	case TC3589X_UNKNOWN:
416e64c1eb4SLinus Walleij 	default:
417e64c1eb4SLinus Walleij 		tc3589x->num_gpio = 24;
418e64c1eb4SLinus Walleij 		break;
419e64c1eb4SLinus Walleij 	}
420f4e8afdcSSundar Iyer 
42120406ebfSSundar Iyer 	i2c_set_clientdata(i2c, tc3589x);
422f4e8afdcSSundar Iyer 
42320406ebfSSundar Iyer 	ret = tc3589x_chip_init(tc3589x);
424f4e8afdcSSundar Iyer 	if (ret)
4251383e00fSJingoo Han 		return ret;
426f4e8afdcSSundar Iyer 
427a435ae1dSLee Jones 	ret = tc3589x_irq_init(tc3589x, np);
428f4e8afdcSSundar Iyer 	if (ret)
4291383e00fSJingoo Han 		return ret;
430f4e8afdcSSundar Iyer 
43120406ebfSSundar Iyer 	ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
432f4e8afdcSSundar Iyer 				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
43320406ebfSSundar Iyer 				   "tc3589x", tc3589x);
434f4e8afdcSSundar Iyer 	if (ret) {
43520406ebfSSundar Iyer 		dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
4361383e00fSJingoo Han 		return ret;
437f4e8afdcSSundar Iyer 	}
438f4e8afdcSSundar Iyer 
439611b7590SSundar Iyer 	ret = tc3589x_device_init(tc3589x);
440f4e8afdcSSundar Iyer 	if (ret) {
441611b7590SSundar Iyer 		dev_err(tc3589x->dev, "failed to add child devices\n");
4421383e00fSJingoo Han 		return ret;
443f4e8afdcSSundar Iyer 	}
444f4e8afdcSSundar Iyer 
445f4e8afdcSSundar Iyer 	return 0;
446f4e8afdcSSundar Iyer }
447f4e8afdcSSundar Iyer 
4484740f73fSBill Pemberton static int tc3589x_remove(struct i2c_client *client)
449f4e8afdcSSundar Iyer {
45020406ebfSSundar Iyer 	struct tc3589x *tc3589x = i2c_get_clientdata(client);
451f4e8afdcSSundar Iyer 
45220406ebfSSundar Iyer 	mfd_remove_devices(tc3589x->dev);
453f4e8afdcSSundar Iyer 
454f4e8afdcSSundar Iyer 	return 0;
455f4e8afdcSSundar Iyer }
456f4e8afdcSSundar Iyer 
457930bf022SAxel Lin #ifdef CONFIG_PM_SLEEP
458593e9d70SSundar Iyer static int tc3589x_suspend(struct device *dev)
459593e9d70SSundar Iyer {
460593e9d70SSundar Iyer 	struct tc3589x *tc3589x = dev_get_drvdata(dev);
461593e9d70SSundar Iyer 	struct i2c_client *client = tc3589x->i2c;
462593e9d70SSundar Iyer 	int ret = 0;
463593e9d70SSundar Iyer 
464593e9d70SSundar Iyer 	/* put the system to sleep mode */
465593e9d70SSundar Iyer 	if (!device_may_wakeup(&client->dev))
466593e9d70SSundar Iyer 		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
467593e9d70SSundar Iyer 				TC3589x_CLKMODE_MODCTL_SLEEP);
468593e9d70SSundar Iyer 
469593e9d70SSundar Iyer 	return ret;
470593e9d70SSundar Iyer }
471593e9d70SSundar Iyer 
472593e9d70SSundar Iyer static int tc3589x_resume(struct device *dev)
473593e9d70SSundar Iyer {
474593e9d70SSundar Iyer 	struct tc3589x *tc3589x = dev_get_drvdata(dev);
475593e9d70SSundar Iyer 	struct i2c_client *client = tc3589x->i2c;
476593e9d70SSundar Iyer 	int ret = 0;
477593e9d70SSundar Iyer 
478593e9d70SSundar Iyer 	/* enable the system into operation */
479593e9d70SSundar Iyer 	if (!device_may_wakeup(&client->dev))
480593e9d70SSundar Iyer 		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
481593e9d70SSundar Iyer 				TC3589x_CLKMODE_MODCTL_OPERATION);
482593e9d70SSundar Iyer 
483593e9d70SSundar Iyer 	return ret;
484593e9d70SSundar Iyer }
48554d8e2c3SLinus Walleij #endif
486593e9d70SSundar Iyer 
487930bf022SAxel Lin static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
488930bf022SAxel Lin 
48920406ebfSSundar Iyer static const struct i2c_device_id tc3589x_id[] = {
490e64c1eb4SLinus Walleij 	{ "tc35890", TC3589X_TC35890 },
491e64c1eb4SLinus Walleij 	{ "tc35892", TC3589X_TC35892 },
492e64c1eb4SLinus Walleij 	{ "tc35893", TC3589X_TC35893 },
493e64c1eb4SLinus Walleij 	{ "tc35894", TC3589X_TC35894 },
494e64c1eb4SLinus Walleij 	{ "tc35895", TC3589X_TC35895 },
495e64c1eb4SLinus Walleij 	{ "tc35896", TC3589X_TC35896 },
496e64c1eb4SLinus Walleij 	{ "tc3589x", TC3589X_UNKNOWN },
497f4e8afdcSSundar Iyer 	{ }
498f4e8afdcSSundar Iyer };
49920406ebfSSundar Iyer MODULE_DEVICE_TABLE(i2c, tc3589x_id);
500f4e8afdcSSundar Iyer 
50120406ebfSSundar Iyer static struct i2c_driver tc3589x_driver = {
502a381b13eSLinus Walleij 	.driver = {
503a381b13eSLinus Walleij 		.name	= "tc3589x",
504a381b13eSLinus Walleij 		.owner	= THIS_MODULE,
505a381b13eSLinus Walleij 		.pm	= &tc3589x_dev_pm_ops,
506a381b13eSLinus Walleij 		.of_match_table = of_match_ptr(tc3589x_match),
507a381b13eSLinus Walleij 	},
50820406ebfSSundar Iyer 	.probe		= tc3589x_probe,
50984449216SBill Pemberton 	.remove		= tc3589x_remove,
51020406ebfSSundar Iyer 	.id_table	= tc3589x_id,
511f4e8afdcSSundar Iyer };
512f4e8afdcSSundar Iyer 
51320406ebfSSundar Iyer static int __init tc3589x_init(void)
514f4e8afdcSSundar Iyer {
51520406ebfSSundar Iyer 	return i2c_add_driver(&tc3589x_driver);
516f4e8afdcSSundar Iyer }
51720406ebfSSundar Iyer subsys_initcall(tc3589x_init);
518f4e8afdcSSundar Iyer 
51920406ebfSSundar Iyer static void __exit tc3589x_exit(void)
520f4e8afdcSSundar Iyer {
52120406ebfSSundar Iyer 	i2c_del_driver(&tc3589x_driver);
522f4e8afdcSSundar Iyer }
52320406ebfSSundar Iyer module_exit(tc3589x_exit);
524f4e8afdcSSundar Iyer 
525f4e8afdcSSundar Iyer MODULE_LICENSE("GPL v2");
52620406ebfSSundar Iyer MODULE_DESCRIPTION("TC3589x MFD core driver");
527f4e8afdcSSundar Iyer MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");
528