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> 12f4e8afdcSSundar Iyer #include <linux/slab.h> 13f4e8afdcSSundar Iyer #include <linux/i2c.h> 14f4e8afdcSSundar Iyer #include <linux/mfd/core.h> 15f4e8afdcSSundar Iyer #include <linux/mfd/tc3589x.h> 16f4e8afdcSSundar Iyer 17f4e8afdcSSundar Iyer /** 1820406ebfSSundar Iyer * tc3589x_reg_read() - read a single TC3589x register 1920406ebfSSundar Iyer * @tc3589x: Device to read from 20f4e8afdcSSundar Iyer * @reg: Register to read 21f4e8afdcSSundar Iyer */ 2220406ebfSSundar Iyer int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg) 23f4e8afdcSSundar Iyer { 24f4e8afdcSSundar Iyer int ret; 25f4e8afdcSSundar Iyer 2620406ebfSSundar Iyer ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg); 27f4e8afdcSSundar Iyer if (ret < 0) 2820406ebfSSundar Iyer dev_err(tc3589x->dev, "failed to read reg %#x: %d\n", 29f4e8afdcSSundar Iyer reg, ret); 30f4e8afdcSSundar Iyer 31f4e8afdcSSundar Iyer return ret; 32f4e8afdcSSundar Iyer } 3320406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_reg_read); 34f4e8afdcSSundar Iyer 35f4e8afdcSSundar Iyer /** 3620406ebfSSundar Iyer * tc3589x_reg_read() - write a single TC3589x register 3720406ebfSSundar Iyer * @tc3589x: Device to write to 38f4e8afdcSSundar Iyer * @reg: Register to read 39f4e8afdcSSundar Iyer * @data: Value to write 40f4e8afdcSSundar Iyer */ 4120406ebfSSundar Iyer int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data) 42f4e8afdcSSundar Iyer { 43f4e8afdcSSundar Iyer int ret; 44f4e8afdcSSundar Iyer 4520406ebfSSundar Iyer ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data); 46f4e8afdcSSundar Iyer if (ret < 0) 4720406ebfSSundar Iyer dev_err(tc3589x->dev, "failed to write reg %#x: %d\n", 48f4e8afdcSSundar Iyer reg, ret); 49f4e8afdcSSundar Iyer 50f4e8afdcSSundar Iyer return ret; 51f4e8afdcSSundar Iyer } 5220406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_reg_write); 53f4e8afdcSSundar Iyer 54f4e8afdcSSundar Iyer /** 5520406ebfSSundar Iyer * tc3589x_block_read() - read multiple TC3589x registers 5620406ebfSSundar Iyer * @tc3589x: Device to read from 57f4e8afdcSSundar Iyer * @reg: First register 58f4e8afdcSSundar Iyer * @length: Number of registers 59f4e8afdcSSundar Iyer * @values: Buffer to write to 60f4e8afdcSSundar Iyer */ 6120406ebfSSundar Iyer int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values) 62f4e8afdcSSundar Iyer { 63f4e8afdcSSundar Iyer int ret; 64f4e8afdcSSundar Iyer 6520406ebfSSundar Iyer ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values); 66f4e8afdcSSundar Iyer if (ret < 0) 6720406ebfSSundar Iyer dev_err(tc3589x->dev, "failed to read regs %#x: %d\n", 68f4e8afdcSSundar Iyer reg, ret); 69f4e8afdcSSundar Iyer 70f4e8afdcSSundar Iyer return ret; 71f4e8afdcSSundar Iyer } 7220406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_block_read); 73f4e8afdcSSundar Iyer 74f4e8afdcSSundar Iyer /** 7520406ebfSSundar Iyer * tc3589x_block_write() - write multiple TC3589x registers 7620406ebfSSundar Iyer * @tc3589x: Device to write to 77f4e8afdcSSundar Iyer * @reg: First register 78f4e8afdcSSundar Iyer * @length: Number of registers 79f4e8afdcSSundar Iyer * @values: Values to write 80f4e8afdcSSundar Iyer */ 8120406ebfSSundar Iyer int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length, 82f4e8afdcSSundar Iyer const u8 *values) 83f4e8afdcSSundar Iyer { 84f4e8afdcSSundar Iyer int ret; 85f4e8afdcSSundar Iyer 8620406ebfSSundar Iyer ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length, 87f4e8afdcSSundar Iyer values); 88f4e8afdcSSundar Iyer if (ret < 0) 8920406ebfSSundar Iyer dev_err(tc3589x->dev, "failed to write regs %#x: %d\n", 90f4e8afdcSSundar Iyer reg, ret); 91f4e8afdcSSundar Iyer 92f4e8afdcSSundar Iyer return ret; 93f4e8afdcSSundar Iyer } 9420406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_block_write); 95f4e8afdcSSundar Iyer 96f4e8afdcSSundar Iyer /** 9720406ebfSSundar Iyer * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register 9820406ebfSSundar Iyer * @tc3589x: Device to write to 99f4e8afdcSSundar Iyer * @reg: Register to write 100f4e8afdcSSundar Iyer * @mask: Mask of bits to set 101f4e8afdcSSundar Iyer * @values: Value to set 102f4e8afdcSSundar Iyer */ 10320406ebfSSundar Iyer int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val) 104f4e8afdcSSundar Iyer { 105f4e8afdcSSundar Iyer int ret; 106f4e8afdcSSundar Iyer 10720406ebfSSundar Iyer mutex_lock(&tc3589x->lock); 108f4e8afdcSSundar Iyer 10920406ebfSSundar Iyer ret = tc3589x_reg_read(tc3589x, reg); 110f4e8afdcSSundar Iyer if (ret < 0) 111f4e8afdcSSundar Iyer goto out; 112f4e8afdcSSundar Iyer 113f4e8afdcSSundar Iyer ret &= ~mask; 114f4e8afdcSSundar Iyer ret |= val; 115f4e8afdcSSundar Iyer 11620406ebfSSundar Iyer ret = tc3589x_reg_write(tc3589x, reg, ret); 117f4e8afdcSSundar Iyer 118f4e8afdcSSundar Iyer out: 11920406ebfSSundar Iyer mutex_unlock(&tc3589x->lock); 120f4e8afdcSSundar Iyer return ret; 121f4e8afdcSSundar Iyer } 12220406ebfSSundar Iyer EXPORT_SYMBOL_GPL(tc3589x_set_bits); 123f4e8afdcSSundar Iyer 124f4e8afdcSSundar Iyer static struct resource gpio_resources[] = { 125f4e8afdcSSundar Iyer { 12620406ebfSSundar Iyer .start = TC3589x_INT_GPIIRQ, 12720406ebfSSundar Iyer .end = TC3589x_INT_GPIIRQ, 128f4e8afdcSSundar Iyer .flags = IORESOURCE_IRQ, 129f4e8afdcSSundar Iyer }, 130f4e8afdcSSundar Iyer }; 131f4e8afdcSSundar Iyer 132611b7590SSundar Iyer static struct mfd_cell tc3589x_dev_gpio[] = { 133f4e8afdcSSundar Iyer { 13420406ebfSSundar Iyer .name = "tc3589x-gpio", 135f4e8afdcSSundar Iyer .num_resources = ARRAY_SIZE(gpio_resources), 136f4e8afdcSSundar Iyer .resources = &gpio_resources[0], 137f4e8afdcSSundar Iyer }, 138f4e8afdcSSundar Iyer }; 139f4e8afdcSSundar Iyer 14020406ebfSSundar Iyer static irqreturn_t tc3589x_irq(int irq, void *data) 141f4e8afdcSSundar Iyer { 14220406ebfSSundar Iyer struct tc3589x *tc3589x = data; 143f4e8afdcSSundar Iyer int status; 144f4e8afdcSSundar Iyer 145bd77efd0SSundar Iyer again: 14620406ebfSSundar Iyer status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); 147f4e8afdcSSundar Iyer if (status < 0) 148f4e8afdcSSundar Iyer return IRQ_NONE; 149f4e8afdcSSundar Iyer 150f4e8afdcSSundar Iyer while (status) { 151f4e8afdcSSundar Iyer int bit = __ffs(status); 152f4e8afdcSSundar Iyer 15320406ebfSSundar Iyer handle_nested_irq(tc3589x->irq_base + bit); 154f4e8afdcSSundar Iyer status &= ~(1 << bit); 155f4e8afdcSSundar Iyer } 156f4e8afdcSSundar Iyer 157f4e8afdcSSundar Iyer /* 158f4e8afdcSSundar Iyer * A dummy read or write (to any register) appears to be necessary to 159f4e8afdcSSundar Iyer * have the last interrupt clear (for example, GPIO IC write) take 160bd77efd0SSundar Iyer * effect. In such a case, recheck for any interrupt which is still 161bd77efd0SSundar Iyer * pending. 162f4e8afdcSSundar Iyer */ 163bd77efd0SSundar Iyer status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); 164bd77efd0SSundar Iyer if (status) 165bd77efd0SSundar Iyer goto again; 166f4e8afdcSSundar Iyer 167f4e8afdcSSundar Iyer return IRQ_HANDLED; 168f4e8afdcSSundar Iyer } 169f4e8afdcSSundar Iyer 17020406ebfSSundar Iyer static void tc3589x_irq_dummy(unsigned int irq) 171f4e8afdcSSundar Iyer { 172f4e8afdcSSundar Iyer /* No mask/unmask at this level */ 173f4e8afdcSSundar Iyer } 174f4e8afdcSSundar Iyer 17520406ebfSSundar Iyer static struct irq_chip tc3589x_irq_chip = { 17620406ebfSSundar Iyer .name = "tc3589x", 17720406ebfSSundar Iyer .mask = tc3589x_irq_dummy, 17820406ebfSSundar Iyer .unmask = tc3589x_irq_dummy, 179f4e8afdcSSundar Iyer }; 180f4e8afdcSSundar Iyer 18120406ebfSSundar Iyer static int tc3589x_irq_init(struct tc3589x *tc3589x) 182f4e8afdcSSundar Iyer { 18320406ebfSSundar Iyer int base = tc3589x->irq_base; 184f4e8afdcSSundar Iyer int irq; 185f4e8afdcSSundar Iyer 18620406ebfSSundar Iyer for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) { 18720406ebfSSundar Iyer set_irq_chip_data(irq, tc3589x); 18820406ebfSSundar Iyer set_irq_chip_and_handler(irq, &tc3589x_irq_chip, 189f4e8afdcSSundar Iyer handle_edge_irq); 190f4e8afdcSSundar Iyer set_irq_nested_thread(irq, 1); 191f4e8afdcSSundar Iyer #ifdef CONFIG_ARM 192f4e8afdcSSundar Iyer set_irq_flags(irq, IRQF_VALID); 193f4e8afdcSSundar Iyer #else 194f4e8afdcSSundar Iyer set_irq_noprobe(irq); 195f4e8afdcSSundar Iyer #endif 196f4e8afdcSSundar Iyer } 197f4e8afdcSSundar Iyer 198f4e8afdcSSundar Iyer return 0; 199f4e8afdcSSundar Iyer } 200f4e8afdcSSundar Iyer 20120406ebfSSundar Iyer static void tc3589x_irq_remove(struct tc3589x *tc3589x) 202f4e8afdcSSundar Iyer { 20320406ebfSSundar Iyer int base = tc3589x->irq_base; 204f4e8afdcSSundar Iyer int irq; 205f4e8afdcSSundar Iyer 20620406ebfSSundar Iyer for (irq = base; irq < base + TC3589x_NR_INTERNAL_IRQS; irq++) { 207f4e8afdcSSundar Iyer #ifdef CONFIG_ARM 208f4e8afdcSSundar Iyer set_irq_flags(irq, 0); 209f4e8afdcSSundar Iyer #endif 210f4e8afdcSSundar Iyer set_irq_chip_and_handler(irq, NULL, NULL); 211f4e8afdcSSundar Iyer set_irq_chip_data(irq, NULL); 212f4e8afdcSSundar Iyer } 213f4e8afdcSSundar Iyer } 214f4e8afdcSSundar Iyer 21520406ebfSSundar Iyer static int tc3589x_chip_init(struct tc3589x *tc3589x) 216f4e8afdcSSundar Iyer { 217f4e8afdcSSundar Iyer int manf, ver, ret; 218f4e8afdcSSundar Iyer 21920406ebfSSundar Iyer manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE); 220f4e8afdcSSundar Iyer if (manf < 0) 221f4e8afdcSSundar Iyer return manf; 222f4e8afdcSSundar Iyer 22320406ebfSSundar Iyer ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION); 224f4e8afdcSSundar Iyer if (ver < 0) 225f4e8afdcSSundar Iyer return ver; 226f4e8afdcSSundar Iyer 22720406ebfSSundar Iyer if (manf != TC3589x_MANFCODE_MAGIC) { 22820406ebfSSundar Iyer dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf); 229f4e8afdcSSundar Iyer return -EINVAL; 230f4e8afdcSSundar Iyer } 231f4e8afdcSSundar Iyer 23220406ebfSSundar Iyer dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver); 233f4e8afdcSSundar Iyer 234*523bc382SSundar Iyer /* 235*523bc382SSundar Iyer * Put everything except the IRQ module into reset; 236*523bc382SSundar Iyer * also spare the GPIO module for any pin initialization 237*523bc382SSundar Iyer * done during pre-kernel boot 238*523bc382SSundar Iyer */ 23920406ebfSSundar Iyer ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL, 24020406ebfSSundar Iyer TC3589x_RSTCTRL_TIMRST 24120406ebfSSundar Iyer | TC3589x_RSTCTRL_ROTRST 242*523bc382SSundar Iyer | TC3589x_RSTCTRL_KBDRST); 243f4e8afdcSSundar Iyer if (ret < 0) 244f4e8afdcSSundar Iyer return ret; 245f4e8afdcSSundar Iyer 246f4e8afdcSSundar Iyer /* Clear the reset interrupt. */ 24720406ebfSSundar Iyer return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1); 248f4e8afdcSSundar Iyer } 249f4e8afdcSSundar Iyer 250611b7590SSundar Iyer static int __devinit tc3589x_device_init(struct tc3589x *tc3589x) 251611b7590SSundar Iyer { 252611b7590SSundar Iyer int ret = 0; 253611b7590SSundar Iyer unsigned int blocks = tc3589x->pdata->block; 254611b7590SSundar Iyer 255611b7590SSundar Iyer if (blocks & TC3589x_BLOCK_GPIO) { 256611b7590SSundar Iyer ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio, 257611b7590SSundar Iyer ARRAY_SIZE(tc3589x_dev_gpio), NULL, 258611b7590SSundar Iyer tc3589x->irq_base); 259611b7590SSundar Iyer if (ret) { 260611b7590SSundar Iyer dev_err(tc3589x->dev, "failed to add gpio child\n"); 261611b7590SSundar Iyer return ret; 262611b7590SSundar Iyer } 263611b7590SSundar Iyer dev_info(tc3589x->dev, "added gpio block\n"); 264611b7590SSundar Iyer } 265611b7590SSundar Iyer 266611b7590SSundar Iyer return ret; 267611b7590SSundar Iyer 268611b7590SSundar Iyer } 269611b7590SSundar Iyer 27020406ebfSSundar Iyer static int __devinit tc3589x_probe(struct i2c_client *i2c, 271f4e8afdcSSundar Iyer const struct i2c_device_id *id) 272f4e8afdcSSundar Iyer { 27320406ebfSSundar Iyer struct tc3589x_platform_data *pdata = i2c->dev.platform_data; 27420406ebfSSundar Iyer struct tc3589x *tc3589x; 275f4e8afdcSSundar Iyer int ret; 276f4e8afdcSSundar Iyer 277f4e8afdcSSundar Iyer if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA 278f4e8afdcSSundar Iyer | I2C_FUNC_SMBUS_I2C_BLOCK)) 279f4e8afdcSSundar Iyer return -EIO; 280f4e8afdcSSundar Iyer 28120406ebfSSundar Iyer tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL); 28220406ebfSSundar Iyer if (!tc3589x) 283f4e8afdcSSundar Iyer return -ENOMEM; 284f4e8afdcSSundar Iyer 28520406ebfSSundar Iyer mutex_init(&tc3589x->lock); 286f4e8afdcSSundar Iyer 28720406ebfSSundar Iyer tc3589x->dev = &i2c->dev; 28820406ebfSSundar Iyer tc3589x->i2c = i2c; 28920406ebfSSundar Iyer tc3589x->pdata = pdata; 29020406ebfSSundar Iyer tc3589x->irq_base = pdata->irq_base; 29120406ebfSSundar Iyer tc3589x->num_gpio = id->driver_data; 292f4e8afdcSSundar Iyer 29320406ebfSSundar Iyer i2c_set_clientdata(i2c, tc3589x); 294f4e8afdcSSundar Iyer 29520406ebfSSundar Iyer ret = tc3589x_chip_init(tc3589x); 296f4e8afdcSSundar Iyer if (ret) 297f4e8afdcSSundar Iyer goto out_free; 298f4e8afdcSSundar Iyer 29920406ebfSSundar Iyer ret = tc3589x_irq_init(tc3589x); 300f4e8afdcSSundar Iyer if (ret) 301f4e8afdcSSundar Iyer goto out_free; 302f4e8afdcSSundar Iyer 30320406ebfSSundar Iyer ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq, 304f4e8afdcSSundar Iyer IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 30520406ebfSSundar Iyer "tc3589x", tc3589x); 306f4e8afdcSSundar Iyer if (ret) { 30720406ebfSSundar Iyer dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret); 308f4e8afdcSSundar Iyer goto out_removeirq; 309f4e8afdcSSundar Iyer } 310f4e8afdcSSundar Iyer 311611b7590SSundar Iyer ret = tc3589x_device_init(tc3589x); 312f4e8afdcSSundar Iyer if (ret) { 313611b7590SSundar Iyer dev_err(tc3589x->dev, "failed to add child devices\n"); 314f4e8afdcSSundar Iyer goto out_freeirq; 315f4e8afdcSSundar Iyer } 316f4e8afdcSSundar Iyer 317f4e8afdcSSundar Iyer return 0; 318f4e8afdcSSundar Iyer 319f4e8afdcSSundar Iyer out_freeirq: 32020406ebfSSundar Iyer free_irq(tc3589x->i2c->irq, tc3589x); 321f4e8afdcSSundar Iyer out_removeirq: 32220406ebfSSundar Iyer tc3589x_irq_remove(tc3589x); 323f4e8afdcSSundar Iyer out_free: 32420406ebfSSundar Iyer kfree(tc3589x); 325f4e8afdcSSundar Iyer return ret; 326f4e8afdcSSundar Iyer } 327f4e8afdcSSundar Iyer 32820406ebfSSundar Iyer static int __devexit tc3589x_remove(struct i2c_client *client) 329f4e8afdcSSundar Iyer { 33020406ebfSSundar Iyer struct tc3589x *tc3589x = i2c_get_clientdata(client); 331f4e8afdcSSundar Iyer 33220406ebfSSundar Iyer mfd_remove_devices(tc3589x->dev); 333f4e8afdcSSundar Iyer 33420406ebfSSundar Iyer free_irq(tc3589x->i2c->irq, tc3589x); 33520406ebfSSundar Iyer tc3589x_irq_remove(tc3589x); 336f4e8afdcSSundar Iyer 33720406ebfSSundar Iyer kfree(tc3589x); 338f4e8afdcSSundar Iyer 339f4e8afdcSSundar Iyer return 0; 340f4e8afdcSSundar Iyer } 341f4e8afdcSSundar Iyer 34220406ebfSSundar Iyer static const struct i2c_device_id tc3589x_id[] = { 34320406ebfSSundar Iyer { "tc3589x", 24 }, 344f4e8afdcSSundar Iyer { } 345f4e8afdcSSundar Iyer }; 34620406ebfSSundar Iyer MODULE_DEVICE_TABLE(i2c, tc3589x_id); 347f4e8afdcSSundar Iyer 34820406ebfSSundar Iyer static struct i2c_driver tc3589x_driver = { 34920406ebfSSundar Iyer .driver.name = "tc3589x", 350f4e8afdcSSundar Iyer .driver.owner = THIS_MODULE, 35120406ebfSSundar Iyer .probe = tc3589x_probe, 35220406ebfSSundar Iyer .remove = __devexit_p(tc3589x_remove), 35320406ebfSSundar Iyer .id_table = tc3589x_id, 354f4e8afdcSSundar Iyer }; 355f4e8afdcSSundar Iyer 35620406ebfSSundar Iyer static int __init tc3589x_init(void) 357f4e8afdcSSundar Iyer { 35820406ebfSSundar Iyer return i2c_add_driver(&tc3589x_driver); 359f4e8afdcSSundar Iyer } 36020406ebfSSundar Iyer subsys_initcall(tc3589x_init); 361f4e8afdcSSundar Iyer 36220406ebfSSundar Iyer static void __exit tc3589x_exit(void) 363f4e8afdcSSundar Iyer { 36420406ebfSSundar Iyer i2c_del_driver(&tc3589x_driver); 365f4e8afdcSSundar Iyer } 36620406ebfSSundar Iyer module_exit(tc3589x_exit); 367f4e8afdcSSundar Iyer 368f4e8afdcSSundar Iyer MODULE_LICENSE("GPL v2"); 36920406ebfSSundar Iyer MODULE_DESCRIPTION("TC3589x MFD core driver"); 370f4e8afdcSSundar Iyer MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent"); 371