Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * Copyright (C) ST-Ericsson SA 2010
  3 *
  4 * License Terms: GNU General Public License, version 2
  5 * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
  6 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/interrupt.h>
 11#include <linux/irq.h>
 
 12#include <linux/slab.h>
 13#include <linux/i2c.h>
 
 
 14#include <linux/mfd/core.h>
 15#include <linux/mfd/tc3589x.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 16
 17#define TC3589x_CLKMODE_MODCTL_SLEEP		0x0
 18#define TC3589x_CLKMODE_MODCTL_OPERATION	(1 << 0)
 19
 20/**
 21 * tc3589x_reg_read() - read a single TC3589x register
 22 * @tc3589x:	Device to read from
 23 * @reg:	Register to read
 24 */
 25int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
 26{
 27	int ret;
 28
 29	ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
 30	if (ret < 0)
 31		dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
 32			reg, ret);
 33
 34	return ret;
 35}
 36EXPORT_SYMBOL_GPL(tc3589x_reg_read);
 37
 38/**
 39 * tc3589x_reg_read() - write a single TC3589x register
 40 * @tc3589x:	Device to write to
 41 * @reg:	Register to read
 42 * @data:	Value to write
 43 */
 44int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
 45{
 46	int ret;
 47
 48	ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
 49	if (ret < 0)
 50		dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
 51			reg, ret);
 52
 53	return ret;
 54}
 55EXPORT_SYMBOL_GPL(tc3589x_reg_write);
 56
 57/**
 58 * tc3589x_block_read() - read multiple TC3589x registers
 59 * @tc3589x:	Device to read from
 60 * @reg:	First register
 61 * @length:	Number of registers
 62 * @values:	Buffer to write to
 63 */
 64int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
 65{
 66	int ret;
 67
 68	ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
 69	if (ret < 0)
 70		dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
 71			reg, ret);
 72
 73	return ret;
 74}
 75EXPORT_SYMBOL_GPL(tc3589x_block_read);
 76
 77/**
 78 * tc3589x_block_write() - write multiple TC3589x registers
 79 * @tc3589x:	Device to write to
 80 * @reg:	First register
 81 * @length:	Number of registers
 82 * @values:	Values to write
 83 */
 84int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
 85			const u8 *values)
 86{
 87	int ret;
 88
 89	ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
 90					     values);
 91	if (ret < 0)
 92		dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
 93			reg, ret);
 94
 95	return ret;
 96}
 97EXPORT_SYMBOL_GPL(tc3589x_block_write);
 98
 99/**
100 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
101 * @tc3589x:	Device to write to
102 * @reg:	Register to write
103 * @mask:	Mask of bits to set
104 * @values:	Value to set
105 */
106int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
107{
108	int ret;
109
110	mutex_lock(&tc3589x->lock);
111
112	ret = tc3589x_reg_read(tc3589x, reg);
113	if (ret < 0)
114		goto out;
115
116	ret &= ~mask;
117	ret |= val;
118
119	ret = tc3589x_reg_write(tc3589x, reg, ret);
120
121out:
122	mutex_unlock(&tc3589x->lock);
123	return ret;
124}
125EXPORT_SYMBOL_GPL(tc3589x_set_bits);
126
127static struct resource gpio_resources[] = {
128	{
129		.start	= TC3589x_INT_GPIIRQ,
130		.end	= TC3589x_INT_GPIIRQ,
131		.flags	= IORESOURCE_IRQ,
132	},
133};
134
135static struct resource keypad_resources[] = {
136	{
137		.start  = TC3589x_INT_KBDIRQ,
138		.end    = TC3589x_INT_KBDIRQ,
139		.flags  = IORESOURCE_IRQ,
140	},
141};
142
143static struct mfd_cell tc3589x_dev_gpio[] = {
144	{
145		.name		= "tc3589x-gpio",
146		.num_resources	= ARRAY_SIZE(gpio_resources),
147		.resources	= &gpio_resources[0],
 
148	},
149};
150
151static struct mfd_cell tc3589x_dev_keypad[] = {
152	{
153		.name           = "tc3589x-keypad",
154		.num_resources  = ARRAY_SIZE(keypad_resources),
155		.resources      = &keypad_resources[0],
 
156	},
157};
158
159static irqreturn_t tc3589x_irq(int irq, void *data)
160{
161	struct tc3589x *tc3589x = data;
162	int status;
163
164again:
165	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
166	if (status < 0)
167		return IRQ_NONE;
168
169	while (status) {
170		int bit = __ffs(status);
 
171
172		handle_nested_irq(tc3589x->irq_base + bit);
173		status &= ~(1 << bit);
174	}
175
176	/*
177	 * A dummy read or write (to any register) appears to be necessary to
178	 * have the last interrupt clear (for example, GPIO IC write) take
179	 * effect. In such a case, recheck for any interrupt which is still
180	 * pending.
181	 */
182	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
183	if (status)
184		goto again;
185
186	return IRQ_HANDLED;
187}
188
189static int tc3589x_irq_init(struct tc3589x *tc3589x)
 
190{
191	int base = tc3589x->irq_base;
192	int irq;
193
194	for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
195		irq_set_chip_data(irq, tc3589x);
196		irq_set_chip_and_handler(irq, &dummy_irq_chip,
197					 handle_edge_irq);
198		irq_set_nested_thread(irq, 1);
199#ifdef CONFIG_ARM
200		set_irq_flags(irq, IRQF_VALID);
201#else
202		irq_set_noprobe(irq);
203#endif
204	}
205
206	return 0;
207}
208
209static void tc3589x_irq_remove(struct tc3589x *tc3589x)
210{
211	int base = tc3589x->irq_base;
212	int irq;
 
213
214	for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) {
215#ifdef CONFIG_ARM
216		set_irq_flags(irq, 0);
217#endif
218		irq_set_chip_and_handler(irq, NULL, NULL);
219		irq_set_chip_data(irq, NULL);
 
 
 
 
 
 
 
 
 
220	}
 
 
221}
222
223static int tc3589x_chip_init(struct tc3589x *tc3589x)
224{
225	int manf, ver, ret;
226
227	manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
228	if (manf < 0)
229		return manf;
230
231	ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
232	if (ver < 0)
233		return ver;
234
235	if (manf != TC3589x_MANFCODE_MAGIC) {
236		dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
237		return -EINVAL;
238	}
239
240	dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
241
242	/*
243	 * Put everything except the IRQ module into reset;
244	 * also spare the GPIO module for any pin initialization
245	 * done during pre-kernel boot
246	 */
247	ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
248				TC3589x_RSTCTRL_TIMRST
249				| TC3589x_RSTCTRL_ROTRST
250				| TC3589x_RSTCTRL_KBDRST);
251	if (ret < 0)
252		return ret;
253
254	/* Clear the reset interrupt. */
255	return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
256}
257
258static int __devinit tc3589x_device_init(struct tc3589x *tc3589x)
259{
260	int ret = 0;
261	unsigned int blocks = tc3589x->pdata->block;
262
263	if (blocks & TC3589x_BLOCK_GPIO) {
264		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
265				ARRAY_SIZE(tc3589x_dev_gpio), NULL,
266				tc3589x->irq_base);
267		if (ret) {
268			dev_err(tc3589x->dev, "failed to add gpio child\n");
269			return ret;
270		}
271		dev_info(tc3589x->dev, "added gpio block\n");
272	}
273
274	if (blocks & TC3589x_BLOCK_KEYPAD) {
275		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
276				ARRAY_SIZE(tc3589x_dev_keypad), NULL,
277				tc3589x->irq_base);
278		if (ret) {
279			dev_err(tc3589x->dev, "failed to keypad child\n");
280			return ret;
281		}
282		dev_info(tc3589x->dev, "added keypad block\n");
283	}
284
285	return ret;
286}
287
288static int __devinit tc3589x_probe(struct i2c_client *i2c,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289				   const struct i2c_device_id *id)
290{
291	struct tc3589x_platform_data *pdata = i2c->dev.platform_data;
 
292	struct tc3589x *tc3589x;
 
293	int ret;
294
 
 
 
 
 
 
 
 
 
 
 
295	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
296				     | I2C_FUNC_SMBUS_I2C_BLOCK))
297		return -EIO;
298
299	tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL);
 
300	if (!tc3589x)
301		return -ENOMEM;
302
303	mutex_init(&tc3589x->lock);
304
305	tc3589x->dev = &i2c->dev;
306	tc3589x->i2c = i2c;
307	tc3589x->pdata = pdata;
308	tc3589x->irq_base = pdata->irq_base;
309	tc3589x->num_gpio = id->driver_data;
 
 
 
 
 
 
 
 
 
 
 
 
 
310
311	i2c_set_clientdata(i2c, tc3589x);
312
313	ret = tc3589x_chip_init(tc3589x);
314	if (ret)
315		goto out_free;
316
317	ret = tc3589x_irq_init(tc3589x);
318	if (ret)
319		goto out_free;
320
321	ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
322				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
323				   "tc3589x", tc3589x);
324	if (ret) {
325		dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
326		goto out_removeirq;
327	}
328
329	ret = tc3589x_device_init(tc3589x);
330	if (ret) {
331		dev_err(tc3589x->dev, "failed to add child devices\n");
332		goto out_freeirq;
333	}
334
335	return 0;
336
337out_freeirq:
338	free_irq(tc3589x->i2c->irq, tc3589x);
339out_removeirq:
340	tc3589x_irq_remove(tc3589x);
341out_free:
342	kfree(tc3589x);
343	return ret;
344}
345
346static int __devexit tc3589x_remove(struct i2c_client *client)
347{
348	struct tc3589x *tc3589x = i2c_get_clientdata(client);
349
350	mfd_remove_devices(tc3589x->dev);
351
352	free_irq(tc3589x->i2c->irq, tc3589x);
353	tc3589x_irq_remove(tc3589x);
354
355	kfree(tc3589x);
356
357	return 0;
358}
359
 
360static int tc3589x_suspend(struct device *dev)
361{
362	struct tc3589x *tc3589x = dev_get_drvdata(dev);
363	struct i2c_client *client = tc3589x->i2c;
364	int ret = 0;
365
366	/* put the system to sleep mode */
367	if (!device_may_wakeup(&client->dev))
368		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
369				TC3589x_CLKMODE_MODCTL_SLEEP);
370
371	return ret;
372}
373
374static int tc3589x_resume(struct device *dev)
375{
376	struct tc3589x *tc3589x = dev_get_drvdata(dev);
377	struct i2c_client *client = tc3589x->i2c;
378	int ret = 0;
379
380	/* enable the system into operation */
381	if (!device_may_wakeup(&client->dev))
382		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
383				TC3589x_CLKMODE_MODCTL_OPERATION);
384
385	return ret;
386}
 
387
388static const SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend,
389						tc3589x_resume);
390
391static const struct i2c_device_id tc3589x_id[] = {
392	{ "tc3589x", 24 },
 
 
 
 
 
 
393	{ }
394};
395MODULE_DEVICE_TABLE(i2c, tc3589x_id);
396
397static struct i2c_driver tc3589x_driver = {
398	.driver.name	= "tc3589x",
399	.driver.owner	= THIS_MODULE,
400#ifdef CONFIG_PM
401	.driver.pm	= &tc3589x_dev_pm_ops,
402#endif
403	.probe		= tc3589x_probe,
404	.remove		= __devexit_p(tc3589x_remove),
405	.id_table	= tc3589x_id,
406};
407
408static int __init tc3589x_init(void)
409{
410	return i2c_add_driver(&tc3589x_driver);
411}
412subsys_initcall(tc3589x_init);
413
414static void __exit tc3589x_exit(void)
415{
416	i2c_del_driver(&tc3589x_driver);
417}
418module_exit(tc3589x_exit);
419
420MODULE_LICENSE("GPL v2");
421MODULE_DESCRIPTION("TC3589x MFD core driver");
422MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) ST-Ericsson SA 2010
  4 *
 
  5 * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
  6 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/interrupt.h>
 11#include <linux/irq.h>
 12#include <linux/irqdomain.h>
 13#include <linux/slab.h>
 14#include <linux/i2c.h>
 15#include <linux/of.h>
 16#include <linux/of_device.h>
 17#include <linux/mfd/core.h>
 18#include <linux/mfd/tc3589x.h>
 19#include <linux/err.h>
 20
 21/*
 22 * enum tc3589x_version - indicates the TC3589x version
 23 */
 24enum tc3589x_version {
 25	TC3589X_TC35890,
 26	TC3589X_TC35892,
 27	TC3589X_TC35893,
 28	TC3589X_TC35894,
 29	TC3589X_TC35895,
 30	TC3589X_TC35896,
 31	TC3589X_UNKNOWN,
 32};
 33
 34#define TC3589x_CLKMODE_MODCTL_SLEEP		0x0
 35#define TC3589x_CLKMODE_MODCTL_OPERATION	(1 << 0)
 36
 37/**
 38 * tc3589x_reg_read() - read a single TC3589x register
 39 * @tc3589x:	Device to read from
 40 * @reg:	Register to read
 41 */
 42int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
 43{
 44	int ret;
 45
 46	ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
 47	if (ret < 0)
 48		dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
 49			reg, ret);
 50
 51	return ret;
 52}
 53EXPORT_SYMBOL_GPL(tc3589x_reg_read);
 54
 55/**
 56 * tc3589x_reg_write() - write a single TC3589x register
 57 * @tc3589x:	Device to write to
 58 * @reg:	Register to read
 59 * @data:	Value to write
 60 */
 61int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
 62{
 63	int ret;
 64
 65	ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
 66	if (ret < 0)
 67		dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
 68			reg, ret);
 69
 70	return ret;
 71}
 72EXPORT_SYMBOL_GPL(tc3589x_reg_write);
 73
 74/**
 75 * tc3589x_block_read() - read multiple TC3589x registers
 76 * @tc3589x:	Device to read from
 77 * @reg:	First register
 78 * @length:	Number of registers
 79 * @values:	Buffer to write to
 80 */
 81int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
 82{
 83	int ret;
 84
 85	ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
 86	if (ret < 0)
 87		dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
 88			reg, ret);
 89
 90	return ret;
 91}
 92EXPORT_SYMBOL_GPL(tc3589x_block_read);
 93
 94/**
 95 * tc3589x_block_write() - write multiple TC3589x registers
 96 * @tc3589x:	Device to write to
 97 * @reg:	First register
 98 * @length:	Number of registers
 99 * @values:	Values to write
100 */
101int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
102			const u8 *values)
103{
104	int ret;
105
106	ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
107					     values);
108	if (ret < 0)
109		dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
110			reg, ret);
111
112	return ret;
113}
114EXPORT_SYMBOL_GPL(tc3589x_block_write);
115
116/**
117 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
118 * @tc3589x:	Device to write to
119 * @reg:	Register to write
120 * @mask:	Mask of bits to set
121 * @val:	Value to set
122 */
123int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
124{
125	int ret;
126
127	mutex_lock(&tc3589x->lock);
128
129	ret = tc3589x_reg_read(tc3589x, reg);
130	if (ret < 0)
131		goto out;
132
133	ret &= ~mask;
134	ret |= val;
135
136	ret = tc3589x_reg_write(tc3589x, reg, ret);
137
138out:
139	mutex_unlock(&tc3589x->lock);
140	return ret;
141}
142EXPORT_SYMBOL_GPL(tc3589x_set_bits);
143
144static const struct resource gpio_resources[] = {
145	{
146		.start	= TC3589x_INT_GPIIRQ,
147		.end	= TC3589x_INT_GPIIRQ,
148		.flags	= IORESOURCE_IRQ,
149	},
150};
151
152static const struct resource keypad_resources[] = {
153	{
154		.start  = TC3589x_INT_KBDIRQ,
155		.end    = TC3589x_INT_KBDIRQ,
156		.flags  = IORESOURCE_IRQ,
157	},
158};
159
160static const struct mfd_cell tc3589x_dev_gpio[] = {
161	{
162		.name		= "tc3589x-gpio",
163		.num_resources	= ARRAY_SIZE(gpio_resources),
164		.resources	= &gpio_resources[0],
165		.of_compatible	= "toshiba,tc3589x-gpio",
166	},
167};
168
169static const struct mfd_cell tc3589x_dev_keypad[] = {
170	{
171		.name           = "tc3589x-keypad",
172		.num_resources  = ARRAY_SIZE(keypad_resources),
173		.resources      = &keypad_resources[0],
174		.of_compatible	= "toshiba,tc3589x-keypad",
175	},
176};
177
178static irqreturn_t tc3589x_irq(int irq, void *data)
179{
180	struct tc3589x *tc3589x = data;
181	int status;
182
183again:
184	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
185	if (status < 0)
186		return IRQ_NONE;
187
188	while (status) {
189		int bit = __ffs(status);
190		int virq = irq_find_mapping(tc3589x->domain, bit);
191
192		handle_nested_irq(virq);
193		status &= ~(1 << bit);
194	}
195
196	/*
197	 * A dummy read or write (to any register) appears to be necessary to
198	 * have the last interrupt clear (for example, GPIO IC write) take
199	 * effect. In such a case, recheck for any interrupt which is still
200	 * pending.
201	 */
202	status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
203	if (status)
204		goto again;
205
206	return IRQ_HANDLED;
207}
208
209static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
210				irq_hw_number_t hwirq)
211{
212	struct tc3589x *tc3589x = d->host_data;
 
213
214	irq_set_chip_data(virq, tc3589x);
215	irq_set_chip_and_handler(virq, &dummy_irq_chip,
216				handle_edge_irq);
217	irq_set_nested_thread(virq, 1);
218	irq_set_noprobe(virq);
 
 
 
 
 
 
219
220	return 0;
221}
222
223static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
224{
225	irq_set_chip_and_handler(virq, NULL, NULL);
226	irq_set_chip_data(virq, NULL);
227}
228
229static const struct irq_domain_ops tc3589x_irq_ops = {
230	.map    = tc3589x_irq_map,
231	.unmap  = tc3589x_irq_unmap,
232	.xlate  = irq_domain_xlate_onecell,
233};
234
235static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
236{
237	tc3589x->domain = irq_domain_add_simple(
238		np, TC3589x_NR_INTERNAL_IRQS, 0,
239		&tc3589x_irq_ops, tc3589x);
240
241	if (!tc3589x->domain) {
242		dev_err(tc3589x->dev, "Failed to create irqdomain\n");
243		return -ENOSYS;
244	}
245
246	return 0;
247}
248
249static int tc3589x_chip_init(struct tc3589x *tc3589x)
250{
251	int manf, ver, ret;
252
253	manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
254	if (manf < 0)
255		return manf;
256
257	ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
258	if (ver < 0)
259		return ver;
260
261	if (manf != TC3589x_MANFCODE_MAGIC) {
262		dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
263		return -EINVAL;
264	}
265
266	dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
267
268	/*
269	 * Put everything except the IRQ module into reset;
270	 * also spare the GPIO module for any pin initialization
271	 * done during pre-kernel boot
272	 */
273	ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
274				TC3589x_RSTCTRL_TIMRST
275				| TC3589x_RSTCTRL_ROTRST
276				| TC3589x_RSTCTRL_KBDRST);
277	if (ret < 0)
278		return ret;
279
280	/* Clear the reset interrupt. */
281	return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
282}
283
284static int tc3589x_device_init(struct tc3589x *tc3589x)
285{
286	int ret = 0;
287	unsigned int blocks = tc3589x->pdata->block;
288
289	if (blocks & TC3589x_BLOCK_GPIO) {
290		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
291				      ARRAY_SIZE(tc3589x_dev_gpio), NULL,
292				      0, tc3589x->domain);
293		if (ret) {
294			dev_err(tc3589x->dev, "failed to add gpio child\n");
295			return ret;
296		}
297		dev_info(tc3589x->dev, "added gpio block\n");
298	}
299
300	if (blocks & TC3589x_BLOCK_KEYPAD) {
301		ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
302				      ARRAY_SIZE(tc3589x_dev_keypad), NULL,
303				      0, tc3589x->domain);
304		if (ret) {
305			dev_err(tc3589x->dev, "failed to keypad child\n");
306			return ret;
307		}
308		dev_info(tc3589x->dev, "added keypad block\n");
309	}
310
311	return ret;
312}
313
314static const struct of_device_id tc3589x_match[] = {
315	/* Legacy compatible string */
316	{ .compatible = "tc3589x", .data = (void *) TC3589X_UNKNOWN },
317	{ .compatible = "toshiba,tc35890", .data = (void *) TC3589X_TC35890 },
318	{ .compatible = "toshiba,tc35892", .data = (void *) TC3589X_TC35892 },
319	{ .compatible = "toshiba,tc35893", .data = (void *) TC3589X_TC35893 },
320	{ .compatible = "toshiba,tc35894", .data = (void *) TC3589X_TC35894 },
321	{ .compatible = "toshiba,tc35895", .data = (void *) TC3589X_TC35895 },
322	{ .compatible = "toshiba,tc35896", .data = (void *) TC3589X_TC35896 },
323	{ }
324};
325
326MODULE_DEVICE_TABLE(of, tc3589x_match);
327
328static struct tc3589x_platform_data *
329tc3589x_of_probe(struct device *dev, enum tc3589x_version *version)
330{
331	struct device_node *np = dev->of_node;
332	struct tc3589x_platform_data *pdata;
333	struct device_node *child;
334	const struct of_device_id *of_id;
335
336	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
337	if (!pdata)
338		return ERR_PTR(-ENOMEM);
339
340	of_id = of_match_device(tc3589x_match, dev);
341	if (!of_id)
342		return ERR_PTR(-ENODEV);
343	*version = (enum tc3589x_version) of_id->data;
344
345	for_each_child_of_node(np, child) {
346		if (of_device_is_compatible(child, "toshiba,tc3589x-gpio"))
347			pdata->block |= TC3589x_BLOCK_GPIO;
348		if (of_device_is_compatible(child, "toshiba,tc3589x-keypad"))
349			pdata->block |= TC3589x_BLOCK_KEYPAD;
350	}
351
352	return pdata;
353}
354
355static int tc3589x_probe(struct i2c_client *i2c,
356				   const struct i2c_device_id *id)
357{
358	struct device_node *np = i2c->dev.of_node;
359	struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
360	struct tc3589x *tc3589x;
361	enum tc3589x_version version;
362	int ret;
363
364	if (!pdata) {
365		pdata = tc3589x_of_probe(&i2c->dev, &version);
366		if (IS_ERR(pdata)) {
367			dev_err(&i2c->dev, "No platform data or DT found\n");
368			return PTR_ERR(pdata);
369		}
370	} else {
371		/* When not probing from device tree we have this ID */
372		version = id->driver_data;
373	}
374
375	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
376				     | I2C_FUNC_SMBUS_I2C_BLOCK))
377		return -EIO;
378
379	tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
380				GFP_KERNEL);
381	if (!tc3589x)
382		return -ENOMEM;
383
384	mutex_init(&tc3589x->lock);
385
386	tc3589x->dev = &i2c->dev;
387	tc3589x->i2c = i2c;
388	tc3589x->pdata = pdata;
389
390	switch (version) {
391	case TC3589X_TC35893:
392	case TC3589X_TC35895:
393	case TC3589X_TC35896:
394		tc3589x->num_gpio = 20;
395		break;
396	case TC3589X_TC35890:
397	case TC3589X_TC35892:
398	case TC3589X_TC35894:
399	case TC3589X_UNKNOWN:
400	default:
401		tc3589x->num_gpio = 24;
402		break;
403	}
404
405	i2c_set_clientdata(i2c, tc3589x);
406
407	ret = tc3589x_chip_init(tc3589x);
408	if (ret)
409		return ret;
410
411	ret = tc3589x_irq_init(tc3589x, np);
412	if (ret)
413		return ret;
414
415	ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
416				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
417				   "tc3589x", tc3589x);
418	if (ret) {
419		dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
420		return ret;
421	}
422
423	ret = tc3589x_device_init(tc3589x);
424	if (ret) {
425		dev_err(tc3589x->dev, "failed to add child devices\n");
426		return ret;
427	}
428
429	return 0;
 
 
 
 
 
 
 
 
430}
431
432static int tc3589x_remove(struct i2c_client *client)
433{
434	struct tc3589x *tc3589x = i2c_get_clientdata(client);
435
436	mfd_remove_devices(tc3589x->dev);
437
 
 
 
 
 
438	return 0;
439}
440
441#ifdef CONFIG_PM_SLEEP
442static int tc3589x_suspend(struct device *dev)
443{
444	struct tc3589x *tc3589x = dev_get_drvdata(dev);
445	struct i2c_client *client = tc3589x->i2c;
446	int ret = 0;
447
448	/* put the system to sleep mode */
449	if (!device_may_wakeup(&client->dev))
450		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
451				TC3589x_CLKMODE_MODCTL_SLEEP);
452
453	return ret;
454}
455
456static int tc3589x_resume(struct device *dev)
457{
458	struct tc3589x *tc3589x = dev_get_drvdata(dev);
459	struct i2c_client *client = tc3589x->i2c;
460	int ret = 0;
461
462	/* enable the system into operation */
463	if (!device_may_wakeup(&client->dev))
464		ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
465				TC3589x_CLKMODE_MODCTL_OPERATION);
466
467	return ret;
468}
469#endif
470
471static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
 
472
473static const struct i2c_device_id tc3589x_id[] = {
474	{ "tc35890", TC3589X_TC35890 },
475	{ "tc35892", TC3589X_TC35892 },
476	{ "tc35893", TC3589X_TC35893 },
477	{ "tc35894", TC3589X_TC35894 },
478	{ "tc35895", TC3589X_TC35895 },
479	{ "tc35896", TC3589X_TC35896 },
480	{ "tc3589x", TC3589X_UNKNOWN },
481	{ }
482};
483MODULE_DEVICE_TABLE(i2c, tc3589x_id);
484
485static struct i2c_driver tc3589x_driver = {
486	.driver = {
487		.name	= "tc3589x",
488		.pm	= &tc3589x_dev_pm_ops,
489		.of_match_table = of_match_ptr(tc3589x_match),
490	},
491	.probe		= tc3589x_probe,
492	.remove		= tc3589x_remove,
493	.id_table	= tc3589x_id,
494};
495
496static int __init tc3589x_init(void)
497{
498	return i2c_add_driver(&tc3589x_driver);
499}
500subsys_initcall(tc3589x_init);
501
502static void __exit tc3589x_exit(void)
503{
504	i2c_del_driver(&tc3589x_driver);
505}
506module_exit(tc3589x_exit);
507
508MODULE_LICENSE("GPL v2");
509MODULE_DESCRIPTION("TC3589x MFD core driver");
510MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");