2 * Copyright (C) ST-Ericsson SA 2010
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
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>
16 #include <linux/mfd/core.h>
17 #include <linux/mfd/tc3589x.h>
20 * enum tc3589x_version - indicates the TC3589x version
22 enum tc3589x_version {
32 #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0
33 #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0)
36 * tc3589x_reg_read() - read a single TC3589x register
37 * @tc3589x: Device to read from
38 * @reg: Register to read
40 int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg)
44 ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg);
46 dev_err(tc3589x->dev, "failed to read reg %#x: %d\n",
51 EXPORT_SYMBOL_GPL(tc3589x_reg_read);
54 * tc3589x_reg_read() - write a single TC3589x register
55 * @tc3589x: Device to write to
56 * @reg: Register to read
57 * @data: Value to write
59 int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data)
63 ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data);
65 dev_err(tc3589x->dev, "failed to write reg %#x: %d\n",
70 EXPORT_SYMBOL_GPL(tc3589x_reg_write);
73 * tc3589x_block_read() - read multiple TC3589x registers
74 * @tc3589x: Device to read from
75 * @reg: First register
76 * @length: Number of registers
77 * @values: Buffer to write to
79 int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values)
83 ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values);
85 dev_err(tc3589x->dev, "failed to read regs %#x: %d\n",
90 EXPORT_SYMBOL_GPL(tc3589x_block_read);
93 * tc3589x_block_write() - write multiple TC3589x registers
94 * @tc3589x: Device to write to
95 * @reg: First register
96 * @length: Number of registers
97 * @values: Values to write
99 int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length,
104 ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length,
107 dev_err(tc3589x->dev, "failed to write regs %#x: %d\n",
112 EXPORT_SYMBOL_GPL(tc3589x_block_write);
115 * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register
116 * @tc3589x: Device to write to
117 * @reg: Register to write
118 * @mask: Mask of bits to set
119 * @values: Value to set
121 int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val)
125 mutex_lock(&tc3589x->lock);
127 ret = tc3589x_reg_read(tc3589x, reg);
134 ret = tc3589x_reg_write(tc3589x, reg, ret);
137 mutex_unlock(&tc3589x->lock);
140 EXPORT_SYMBOL_GPL(tc3589x_set_bits);
142 static struct resource gpio_resources[] = {
144 .start = TC3589x_INT_GPIIRQ,
145 .end = TC3589x_INT_GPIIRQ,
146 .flags = IORESOURCE_IRQ,
150 static struct resource keypad_resources[] = {
152 .start = TC3589x_INT_KBDIRQ,
153 .end = TC3589x_INT_KBDIRQ,
154 .flags = IORESOURCE_IRQ,
158 static const struct mfd_cell tc3589x_dev_gpio[] = {
160 .name = "tc3589x-gpio",
161 .num_resources = ARRAY_SIZE(gpio_resources),
162 .resources = &gpio_resources[0],
163 .of_compatible = "tc3589x-gpio",
167 static const struct mfd_cell tc3589x_dev_keypad[] = {
169 .name = "tc3589x-keypad",
170 .num_resources = ARRAY_SIZE(keypad_resources),
171 .resources = &keypad_resources[0],
172 .of_compatible = "tc3589x-keypad",
176 static irqreturn_t tc3589x_irq(int irq, void *data)
178 struct tc3589x *tc3589x = data;
182 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
187 int bit = __ffs(status);
188 int virq = irq_create_mapping(tc3589x->domain, bit);
190 handle_nested_irq(virq);
191 status &= ~(1 << bit);
195 * A dummy read or write (to any register) appears to be necessary to
196 * have the last interrupt clear (for example, GPIO IC write) take
197 * effect. In such a case, recheck for any interrupt which is still
200 status = tc3589x_reg_read(tc3589x, TC3589x_IRQST);
207 static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq,
208 irq_hw_number_t hwirq)
210 struct tc3589x *tc3589x = d->host_data;
212 irq_set_chip_data(virq, tc3589x);
213 irq_set_chip_and_handler(virq, &dummy_irq_chip,
215 irq_set_nested_thread(virq, 1);
217 set_irq_flags(virq, IRQF_VALID);
219 irq_set_noprobe(virq);
225 static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq)
228 set_irq_flags(virq, 0);
230 irq_set_chip_and_handler(virq, NULL, NULL);
231 irq_set_chip_data(virq, NULL);
234 static struct irq_domain_ops tc3589x_irq_ops = {
235 .map = tc3589x_irq_map,
236 .unmap = tc3589x_irq_unmap,
237 .xlate = irq_domain_xlate_twocell,
240 static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np)
242 int base = tc3589x->irq_base;
244 tc3589x->domain = irq_domain_add_simple(
245 np, TC3589x_NR_INTERNAL_IRQS, base,
246 &tc3589x_irq_ops, tc3589x);
248 if (!tc3589x->domain) {
249 dev_err(tc3589x->dev, "Failed to create irqdomain\n");
256 static int tc3589x_chip_init(struct tc3589x *tc3589x)
260 manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE);
264 ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION);
268 if (manf != TC3589x_MANFCODE_MAGIC) {
269 dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf);
273 dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver);
276 * Put everything except the IRQ module into reset;
277 * also spare the GPIO module for any pin initialization
278 * done during pre-kernel boot
280 ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL,
281 TC3589x_RSTCTRL_TIMRST
282 | TC3589x_RSTCTRL_ROTRST
283 | TC3589x_RSTCTRL_KBDRST);
287 /* Clear the reset interrupt. */
288 return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1);
291 static int tc3589x_device_init(struct tc3589x *tc3589x)
294 unsigned int blocks = tc3589x->pdata->block;
296 if (blocks & TC3589x_BLOCK_GPIO) {
297 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio,
298 ARRAY_SIZE(tc3589x_dev_gpio), NULL,
299 tc3589x->irq_base, tc3589x->domain);
301 dev_err(tc3589x->dev, "failed to add gpio child\n");
304 dev_info(tc3589x->dev, "added gpio block\n");
307 if (blocks & TC3589x_BLOCK_KEYPAD) {
308 ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad,
309 ARRAY_SIZE(tc3589x_dev_keypad), NULL,
310 tc3589x->irq_base, tc3589x->domain);
312 dev_err(tc3589x->dev, "failed to keypad child\n");
315 dev_info(tc3589x->dev, "added keypad block\n");
321 static int tc3589x_of_probe(struct device_node *np,
322 struct tc3589x_platform_data *pdata)
324 struct device_node *child;
326 for_each_child_of_node(np, child) {
327 if (!strcmp(child->name, "tc3589x_gpio")) {
328 pdata->block |= TC3589x_BLOCK_GPIO;
330 if (!strcmp(child->name, "tc3589x_keypad")) {
331 pdata->block |= TC3589x_BLOCK_KEYPAD;
338 static int tc3589x_probe(struct i2c_client *i2c,
339 const struct i2c_device_id *id)
341 struct tc3589x_platform_data *pdata = dev_get_platdata(&i2c->dev);
342 struct device_node *np = i2c->dev.of_node;
343 struct tc3589x *tc3589x;
348 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
352 ret = tc3589x_of_probe(np, pdata);
357 dev_err(&i2c->dev, "No platform data or DT found\n");
362 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA
363 | I2C_FUNC_SMBUS_I2C_BLOCK))
366 tc3589x = devm_kzalloc(&i2c->dev, sizeof(struct tc3589x),
371 mutex_init(&tc3589x->lock);
373 tc3589x->dev = &i2c->dev;
375 tc3589x->pdata = pdata;
376 tc3589x->irq_base = pdata->irq_base;
378 switch (id->driver_data) {
379 case TC3589X_TC35893:
380 case TC3589X_TC35895:
381 case TC3589X_TC35896:
382 tc3589x->num_gpio = 20;
384 case TC3589X_TC35890:
385 case TC3589X_TC35892:
386 case TC3589X_TC35894:
387 case TC3589X_UNKNOWN:
389 tc3589x->num_gpio = 24;
393 i2c_set_clientdata(i2c, tc3589x);
395 ret = tc3589x_chip_init(tc3589x);
399 ret = tc3589x_irq_init(tc3589x, np);
403 ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq,
404 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
407 dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret);
411 ret = tc3589x_device_init(tc3589x);
413 dev_err(tc3589x->dev, "failed to add child devices\n");
420 static int tc3589x_remove(struct i2c_client *client)
422 struct tc3589x *tc3589x = i2c_get_clientdata(client);
424 mfd_remove_devices(tc3589x->dev);
429 #ifdef CONFIG_PM_SLEEP
430 static int tc3589x_suspend(struct device *dev)
432 struct tc3589x *tc3589x = dev_get_drvdata(dev);
433 struct i2c_client *client = tc3589x->i2c;
436 /* put the system to sleep mode */
437 if (!device_may_wakeup(&client->dev))
438 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
439 TC3589x_CLKMODE_MODCTL_SLEEP);
444 static int tc3589x_resume(struct device *dev)
446 struct tc3589x *tc3589x = dev_get_drvdata(dev);
447 struct i2c_client *client = tc3589x->i2c;
450 /* enable the system into operation */
451 if (!device_may_wakeup(&client->dev))
452 ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE,
453 TC3589x_CLKMODE_MODCTL_OPERATION);
459 static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume);
461 static const struct i2c_device_id tc3589x_id[] = {
462 { "tc35890", TC3589X_TC35890 },
463 { "tc35892", TC3589X_TC35892 },
464 { "tc35893", TC3589X_TC35893 },
465 { "tc35894", TC3589X_TC35894 },
466 { "tc35895", TC3589X_TC35895 },
467 { "tc35896", TC3589X_TC35896 },
468 { "tc3589x", TC3589X_UNKNOWN },
471 MODULE_DEVICE_TABLE(i2c, tc3589x_id);
473 static struct i2c_driver tc3589x_driver = {
474 .driver.name = "tc3589x",
475 .driver.owner = THIS_MODULE,
476 .driver.pm = &tc3589x_dev_pm_ops,
477 .probe = tc3589x_probe,
478 .remove = tc3589x_remove,
479 .id_table = tc3589x_id,
482 static int __init tc3589x_init(void)
484 return i2c_add_driver(&tc3589x_driver);
486 subsys_initcall(tc3589x_init);
488 static void __exit tc3589x_exit(void)
490 i2c_del_driver(&tc3589x_driver);
492 module_exit(tc3589x_exit);
494 MODULE_LICENSE("GPL v2");
495 MODULE_DESCRIPTION("TC3589x MFD core driver");
496 MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");