109d6292bSJin Park /* 209d6292bSJin Park * linux/drivers/mfd/aat2870-core.c 309d6292bSJin Park * 409d6292bSJin Park * Copyright (c) 2011, NVIDIA Corporation. 509d6292bSJin Park * Author: Jin Park <jinyoungp@nvidia.com> 609d6292bSJin Park * 709d6292bSJin Park * This program is free software; you can redistribute it and/or 809d6292bSJin Park * modify it under the terms of the GNU General Public License 909d6292bSJin Park * version 2 as published by the Free Software Foundation. 1009d6292bSJin Park * 1109d6292bSJin Park * This program is distributed in the hope that it will be useful, but 1209d6292bSJin Park * WITHOUT ANY WARRANTY; without even the implied warranty of 1309d6292bSJin Park * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1409d6292bSJin Park * General Public License for more details. 1509d6292bSJin Park * 1609d6292bSJin Park * You should have received a copy of the GNU General Public License 1709d6292bSJin Park * along with this program; if not, write to the Free Software 1809d6292bSJin Park * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 1909d6292bSJin Park * 02110-1301 USA 2009d6292bSJin Park */ 2109d6292bSJin Park 2209d6292bSJin Park #include <linux/kernel.h> 2309d6292bSJin Park #include <linux/module.h> 2409d6292bSJin Park #include <linux/init.h> 2509d6292bSJin Park #include <linux/debugfs.h> 2609d6292bSJin Park #include <linux/slab.h> 2709d6292bSJin Park #include <linux/uaccess.h> 2809d6292bSJin Park #include <linux/i2c.h> 2909d6292bSJin Park #include <linux/delay.h> 3009d6292bSJin Park #include <linux/gpio.h> 3109d6292bSJin Park #include <linux/mfd/core.h> 3209d6292bSJin Park #include <linux/mfd/aat2870.h> 3309d6292bSJin Park #include <linux/regulator/machine.h> 3409d6292bSJin Park 3509d6292bSJin Park static struct aat2870_register aat2870_regs[AAT2870_REG_NUM] = { 3609d6292bSJin Park /* readable, writeable, value */ 3709d6292bSJin Park { 0, 1, 0x00 }, /* 0x00 AAT2870_BL_CH_EN */ 3809d6292bSJin Park { 0, 1, 0x16 }, /* 0x01 AAT2870_BLM */ 3909d6292bSJin Park { 0, 1, 0x16 }, /* 0x02 AAT2870_BLS */ 4009d6292bSJin Park { 0, 1, 0x56 }, /* 0x03 AAT2870_BL1 */ 4109d6292bSJin Park { 0, 1, 0x56 }, /* 0x04 AAT2870_BL2 */ 4209d6292bSJin Park { 0, 1, 0x56 }, /* 0x05 AAT2870_BL3 */ 4309d6292bSJin Park { 0, 1, 0x56 }, /* 0x06 AAT2870_BL4 */ 4409d6292bSJin Park { 0, 1, 0x56 }, /* 0x07 AAT2870_BL5 */ 4509d6292bSJin Park { 0, 1, 0x56 }, /* 0x08 AAT2870_BL6 */ 4609d6292bSJin Park { 0, 1, 0x56 }, /* 0x09 AAT2870_BL7 */ 4709d6292bSJin Park { 0, 1, 0x56 }, /* 0x0A AAT2870_BL8 */ 4809d6292bSJin Park { 0, 1, 0x00 }, /* 0x0B AAT2870_FLR */ 4909d6292bSJin Park { 0, 1, 0x03 }, /* 0x0C AAT2870_FM */ 5009d6292bSJin Park { 0, 1, 0x03 }, /* 0x0D AAT2870_FS */ 5109d6292bSJin Park { 0, 1, 0x10 }, /* 0x0E AAT2870_ALS_CFG0 */ 5209d6292bSJin Park { 0, 1, 0x06 }, /* 0x0F AAT2870_ALS_CFG1 */ 5309d6292bSJin Park { 0, 1, 0x00 }, /* 0x10 AAT2870_ALS_CFG2 */ 5409d6292bSJin Park { 1, 0, 0x00 }, /* 0x11 AAT2870_AMB */ 5509d6292bSJin Park { 0, 1, 0x00 }, /* 0x12 AAT2870_ALS0 */ 5609d6292bSJin Park { 0, 1, 0x00 }, /* 0x13 AAT2870_ALS1 */ 5709d6292bSJin Park { 0, 1, 0x00 }, /* 0x14 AAT2870_ALS2 */ 5809d6292bSJin Park { 0, 1, 0x00 }, /* 0x15 AAT2870_ALS3 */ 5909d6292bSJin Park { 0, 1, 0x00 }, /* 0x16 AAT2870_ALS4 */ 6009d6292bSJin Park { 0, 1, 0x00 }, /* 0x17 AAT2870_ALS5 */ 6109d6292bSJin Park { 0, 1, 0x00 }, /* 0x18 AAT2870_ALS6 */ 6209d6292bSJin Park { 0, 1, 0x00 }, /* 0x19 AAT2870_ALS7 */ 6309d6292bSJin Park { 0, 1, 0x00 }, /* 0x1A AAT2870_ALS8 */ 6409d6292bSJin Park { 0, 1, 0x00 }, /* 0x1B AAT2870_ALS9 */ 6509d6292bSJin Park { 0, 1, 0x00 }, /* 0x1C AAT2870_ALSA */ 6609d6292bSJin Park { 0, 1, 0x00 }, /* 0x1D AAT2870_ALSB */ 6709d6292bSJin Park { 0, 1, 0x00 }, /* 0x1E AAT2870_ALSC */ 6809d6292bSJin Park { 0, 1, 0x00 }, /* 0x1F AAT2870_ALSD */ 6909d6292bSJin Park { 0, 1, 0x00 }, /* 0x20 AAT2870_ALSE */ 7009d6292bSJin Park { 0, 1, 0x00 }, /* 0x21 AAT2870_ALSF */ 7109d6292bSJin Park { 0, 1, 0x00 }, /* 0x22 AAT2870_SUB_SET */ 7209d6292bSJin Park { 0, 1, 0x00 }, /* 0x23 AAT2870_SUB_CTRL */ 7309d6292bSJin Park { 0, 1, 0x00 }, /* 0x24 AAT2870_LDO_AB */ 7409d6292bSJin Park { 0, 1, 0x00 }, /* 0x25 AAT2870_LDO_CD */ 7509d6292bSJin Park { 0, 1, 0x00 }, /* 0x26 AAT2870_LDO_EN */ 7609d6292bSJin Park }; 7709d6292bSJin Park 7809d6292bSJin Park static struct mfd_cell aat2870_devs[] = { 7909d6292bSJin Park { 8009d6292bSJin Park .name = "aat2870-backlight", 8109d6292bSJin Park .id = AAT2870_ID_BL, 8209d6292bSJin Park .pdata_size = sizeof(struct aat2870_bl_platform_data), 8309d6292bSJin Park }, 8409d6292bSJin Park { 8509d6292bSJin Park .name = "aat2870-regulator", 8609d6292bSJin Park .id = AAT2870_ID_LDOA, 8709d6292bSJin Park .pdata_size = sizeof(struct regulator_init_data), 8809d6292bSJin Park }, 8909d6292bSJin Park { 9009d6292bSJin Park .name = "aat2870-regulator", 9109d6292bSJin Park .id = AAT2870_ID_LDOB, 9209d6292bSJin Park .pdata_size = sizeof(struct regulator_init_data), 9309d6292bSJin Park }, 9409d6292bSJin Park { 9509d6292bSJin Park .name = "aat2870-regulator", 9609d6292bSJin Park .id = AAT2870_ID_LDOC, 9709d6292bSJin Park .pdata_size = sizeof(struct regulator_init_data), 9809d6292bSJin Park }, 9909d6292bSJin Park { 10009d6292bSJin Park .name = "aat2870-regulator", 10109d6292bSJin Park .id = AAT2870_ID_LDOD, 10209d6292bSJin Park .pdata_size = sizeof(struct regulator_init_data), 10309d6292bSJin Park }, 10409d6292bSJin Park }; 10509d6292bSJin Park 10609d6292bSJin Park static int __aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val) 10709d6292bSJin Park { 10809d6292bSJin Park int ret; 10909d6292bSJin Park 11009d6292bSJin Park if (addr >= AAT2870_REG_NUM) { 11109d6292bSJin Park dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr); 11209d6292bSJin Park return -EINVAL; 11309d6292bSJin Park } 11409d6292bSJin Park 11509d6292bSJin Park if (!aat2870->reg_cache[addr].readable) { 11609d6292bSJin Park *val = aat2870->reg_cache[addr].value; 11709d6292bSJin Park goto out; 11809d6292bSJin Park } 11909d6292bSJin Park 12009d6292bSJin Park ret = i2c_master_send(aat2870->client, &addr, 1); 12109d6292bSJin Park if (ret < 0) 12209d6292bSJin Park return ret; 12309d6292bSJin Park if (ret != 1) 12409d6292bSJin Park return -EIO; 12509d6292bSJin Park 12609d6292bSJin Park ret = i2c_master_recv(aat2870->client, val, 1); 12709d6292bSJin Park if (ret < 0) 12809d6292bSJin Park return ret; 12909d6292bSJin Park if (ret != 1) 13009d6292bSJin Park return -EIO; 13109d6292bSJin Park 13209d6292bSJin Park out: 13309d6292bSJin Park dev_dbg(aat2870->dev, "read: addr=0x%02x, val=0x%02x\n", addr, *val); 13409d6292bSJin Park return 0; 13509d6292bSJin Park } 13609d6292bSJin Park 13709d6292bSJin Park static int __aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val) 13809d6292bSJin Park { 13909d6292bSJin Park u8 msg[2]; 14009d6292bSJin Park int ret; 14109d6292bSJin Park 14209d6292bSJin Park if (addr >= AAT2870_REG_NUM) { 14309d6292bSJin Park dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr); 14409d6292bSJin Park return -EINVAL; 14509d6292bSJin Park } 14609d6292bSJin Park 14709d6292bSJin Park if (!aat2870->reg_cache[addr].writeable) { 14809d6292bSJin Park dev_err(aat2870->dev, "Address 0x%02x is not writeable\n", 14909d6292bSJin Park addr); 15009d6292bSJin Park return -EINVAL; 15109d6292bSJin Park } 15209d6292bSJin Park 15309d6292bSJin Park msg[0] = addr; 15409d6292bSJin Park msg[1] = val; 15509d6292bSJin Park ret = i2c_master_send(aat2870->client, msg, 2); 15609d6292bSJin Park if (ret < 0) 15709d6292bSJin Park return ret; 15809d6292bSJin Park if (ret != 2) 15909d6292bSJin Park return -EIO; 16009d6292bSJin Park 16109d6292bSJin Park aat2870->reg_cache[addr].value = val; 16209d6292bSJin Park 16309d6292bSJin Park dev_dbg(aat2870->dev, "write: addr=0x%02x, val=0x%02x\n", addr, val); 16409d6292bSJin Park return 0; 16509d6292bSJin Park } 16609d6292bSJin Park 16709d6292bSJin Park static int aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val) 16809d6292bSJin Park { 16909d6292bSJin Park int ret; 17009d6292bSJin Park 17109d6292bSJin Park mutex_lock(&aat2870->io_lock); 17209d6292bSJin Park ret = __aat2870_read(aat2870, addr, val); 17309d6292bSJin Park mutex_unlock(&aat2870->io_lock); 17409d6292bSJin Park 17509d6292bSJin Park return ret; 17609d6292bSJin Park } 17709d6292bSJin Park 17809d6292bSJin Park static int aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val) 17909d6292bSJin Park { 18009d6292bSJin Park int ret; 18109d6292bSJin Park 18209d6292bSJin Park mutex_lock(&aat2870->io_lock); 18309d6292bSJin Park ret = __aat2870_write(aat2870, addr, val); 18409d6292bSJin Park mutex_unlock(&aat2870->io_lock); 18509d6292bSJin Park 18609d6292bSJin Park return ret; 18709d6292bSJin Park } 18809d6292bSJin Park 18909d6292bSJin Park static int aat2870_update(struct aat2870_data *aat2870, u8 addr, u8 mask, 19009d6292bSJin Park u8 val) 19109d6292bSJin Park { 19209d6292bSJin Park int change; 19309d6292bSJin Park u8 old_val, new_val; 19409d6292bSJin Park int ret; 19509d6292bSJin Park 19609d6292bSJin Park mutex_lock(&aat2870->io_lock); 19709d6292bSJin Park 19809d6292bSJin Park ret = __aat2870_read(aat2870, addr, &old_val); 19909d6292bSJin Park if (ret) 20009d6292bSJin Park goto out_unlock; 20109d6292bSJin Park 20209d6292bSJin Park new_val = (old_val & ~mask) | (val & mask); 20309d6292bSJin Park change = old_val != new_val; 20409d6292bSJin Park if (change) 20509d6292bSJin Park ret = __aat2870_write(aat2870, addr, new_val); 20609d6292bSJin Park 20709d6292bSJin Park out_unlock: 20809d6292bSJin Park mutex_unlock(&aat2870->io_lock); 20909d6292bSJin Park 21009d6292bSJin Park return ret; 21109d6292bSJin Park } 21209d6292bSJin Park 21309d6292bSJin Park static inline void aat2870_enable(struct aat2870_data *aat2870) 21409d6292bSJin Park { 21509d6292bSJin Park if (aat2870->en_pin >= 0) 21609d6292bSJin Park gpio_set_value(aat2870->en_pin, 1); 21709d6292bSJin Park 21809d6292bSJin Park aat2870->is_enable = 1; 21909d6292bSJin Park } 22009d6292bSJin Park 22109d6292bSJin Park static inline void aat2870_disable(struct aat2870_data *aat2870) 22209d6292bSJin Park { 22309d6292bSJin Park if (aat2870->en_pin >= 0) 22409d6292bSJin Park gpio_set_value(aat2870->en_pin, 0); 22509d6292bSJin Park 22609d6292bSJin Park aat2870->is_enable = 0; 22709d6292bSJin Park } 22809d6292bSJin Park 22909d6292bSJin Park #ifdef CONFIG_DEBUG_FS 23009d6292bSJin Park static ssize_t aat2870_dump_reg(struct aat2870_data *aat2870, char *buf) 23109d6292bSJin Park { 23209d6292bSJin Park u8 addr, val; 23309d6292bSJin Park ssize_t count = 0; 23409d6292bSJin Park int ret; 23509d6292bSJin Park 23609d6292bSJin Park count += sprintf(buf, "aat2870 registers\n"); 23709d6292bSJin Park for (addr = 0; addr < AAT2870_REG_NUM; addr++) { 23809d6292bSJin Park count += sprintf(buf + count, "0x%02x: ", addr); 23909d6292bSJin Park if (count >= PAGE_SIZE - 1) 24009d6292bSJin Park break; 24109d6292bSJin Park 24209d6292bSJin Park ret = aat2870->read(aat2870, addr, &val); 24309d6292bSJin Park if (ret == 0) 24409d6292bSJin Park count += snprintf(buf + count, PAGE_SIZE - count, 24509d6292bSJin Park "0x%02x", val); 24609d6292bSJin Park else 24709d6292bSJin Park count += snprintf(buf + count, PAGE_SIZE - count, 24809d6292bSJin Park "<read fail: %d>", ret); 24909d6292bSJin Park 25009d6292bSJin Park if (count >= PAGE_SIZE - 1) 25109d6292bSJin Park break; 25209d6292bSJin Park 25309d6292bSJin Park count += snprintf(buf + count, PAGE_SIZE - count, "\n"); 25409d6292bSJin Park if (count >= PAGE_SIZE - 1) 25509d6292bSJin Park break; 25609d6292bSJin Park } 25709d6292bSJin Park 25809d6292bSJin Park /* Truncate count; min() would cause a warning */ 25909d6292bSJin Park if (count >= PAGE_SIZE) 26009d6292bSJin Park count = PAGE_SIZE - 1; 26109d6292bSJin Park 26209d6292bSJin Park return count; 26309d6292bSJin Park } 26409d6292bSJin Park 26509d6292bSJin Park static ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf, 26609d6292bSJin Park size_t count, loff_t *ppos) 26709d6292bSJin Park { 26809d6292bSJin Park struct aat2870_data *aat2870 = file->private_data; 26909d6292bSJin Park char *buf; 27009d6292bSJin Park ssize_t ret; 27109d6292bSJin Park 27209d6292bSJin Park buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 27309d6292bSJin Park if (!buf) 27409d6292bSJin Park return -ENOMEM; 27509d6292bSJin Park 27609d6292bSJin Park ret = aat2870_dump_reg(aat2870, buf); 27709d6292bSJin Park if (ret >= 0) 27809d6292bSJin Park ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 27909d6292bSJin Park 28009d6292bSJin Park kfree(buf); 28109d6292bSJin Park 28209d6292bSJin Park return ret; 28309d6292bSJin Park } 28409d6292bSJin Park 28509d6292bSJin Park static ssize_t aat2870_reg_write_file(struct file *file, 28609d6292bSJin Park const char __user *user_buf, size_t count, 28709d6292bSJin Park loff_t *ppos) 28809d6292bSJin Park { 28909d6292bSJin Park struct aat2870_data *aat2870 = file->private_data; 29009d6292bSJin Park char buf[32]; 291c38d66acSJin Park ssize_t buf_size; 29209d6292bSJin Park char *start = buf; 29309d6292bSJin Park unsigned long addr, val; 29409d6292bSJin Park int ret; 29509d6292bSJin Park 29609d6292bSJin Park buf_size = min(count, (sizeof(buf)-1)); 29709d6292bSJin Park if (copy_from_user(buf, user_buf, buf_size)) { 29809d6292bSJin Park dev_err(aat2870->dev, "Failed to copy from user\n"); 29909d6292bSJin Park return -EFAULT; 30009d6292bSJin Park } 30109d6292bSJin Park buf[buf_size] = 0; 30209d6292bSJin Park 30309d6292bSJin Park while (*start == ' ') 30409d6292bSJin Park start++; 30509d6292bSJin Park 30609d6292bSJin Park addr = simple_strtoul(start, &start, 16); 30709d6292bSJin Park if (addr >= AAT2870_REG_NUM) { 30809d6292bSJin Park dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr); 30909d6292bSJin Park return -EINVAL; 31009d6292bSJin Park } 31109d6292bSJin Park 31209d6292bSJin Park while (*start == ' ') 31309d6292bSJin Park start++; 31409d6292bSJin Park 315*8420a241SJingoo Han ret = kstrtoul(start, 16, &val); 316*8420a241SJingoo Han if (ret) 317*8420a241SJingoo Han return ret; 31809d6292bSJin Park 31909d6292bSJin Park ret = aat2870->write(aat2870, (u8)addr, (u8)val); 32009d6292bSJin Park if (ret) 32109d6292bSJin Park return ret; 32209d6292bSJin Park 32309d6292bSJin Park return buf_size; 32409d6292bSJin Park } 32509d6292bSJin Park 32609d6292bSJin Park static const struct file_operations aat2870_reg_fops = { 327234e3405SStephen Boyd .open = simple_open, 32809d6292bSJin Park .read = aat2870_reg_read_file, 32909d6292bSJin Park .write = aat2870_reg_write_file, 33009d6292bSJin Park }; 33109d6292bSJin Park 33209d6292bSJin Park static void aat2870_init_debugfs(struct aat2870_data *aat2870) 33309d6292bSJin Park { 33409d6292bSJin Park aat2870->dentry_root = debugfs_create_dir("aat2870", NULL); 33509d6292bSJin Park if (!aat2870->dentry_root) { 33609d6292bSJin Park dev_warn(aat2870->dev, 33709d6292bSJin Park "Failed to create debugfs root directory\n"); 33809d6292bSJin Park return; 33909d6292bSJin Park } 34009d6292bSJin Park 34109d6292bSJin Park aat2870->dentry_reg = debugfs_create_file("regs", 0644, 34209d6292bSJin Park aat2870->dentry_root, 34309d6292bSJin Park aat2870, &aat2870_reg_fops); 34409d6292bSJin Park if (!aat2870->dentry_reg) 34509d6292bSJin Park dev_warn(aat2870->dev, 34609d6292bSJin Park "Failed to create debugfs register file\n"); 34709d6292bSJin Park } 34809d6292bSJin Park 34909d6292bSJin Park static void aat2870_uninit_debugfs(struct aat2870_data *aat2870) 35009d6292bSJin Park { 35109d6292bSJin Park debugfs_remove_recursive(aat2870->dentry_root); 35209d6292bSJin Park } 35309d6292bSJin Park #else 35409d6292bSJin Park static inline void aat2870_init_debugfs(struct aat2870_data *aat2870) 35509d6292bSJin Park { 35609d6292bSJin Park } 35709d6292bSJin Park 35809d6292bSJin Park static inline void aat2870_uninit_debugfs(struct aat2870_data *aat2870) 35909d6292bSJin Park { 36009d6292bSJin Park } 36109d6292bSJin Park #endif /* CONFIG_DEBUG_FS */ 36209d6292bSJin Park 36309d6292bSJin Park static int aat2870_i2c_probe(struct i2c_client *client, 36409d6292bSJin Park const struct i2c_device_id *id) 36509d6292bSJin Park { 36609d6292bSJin Park struct aat2870_platform_data *pdata = client->dev.platform_data; 36709d6292bSJin Park struct aat2870_data *aat2870; 36809d6292bSJin Park int i, j; 36909d6292bSJin Park int ret = 0; 37009d6292bSJin Park 3710287714bSJingoo Han aat2870 = devm_kzalloc(&client->dev, sizeof(struct aat2870_data), 3720287714bSJingoo Han GFP_KERNEL); 37309d6292bSJin Park if (!aat2870) { 37409d6292bSJin Park dev_err(&client->dev, 37509d6292bSJin Park "Failed to allocate memory for aat2870\n"); 3760287714bSJingoo Han return -ENOMEM; 37709d6292bSJin Park } 37809d6292bSJin Park 37909d6292bSJin Park aat2870->dev = &client->dev; 38009d6292bSJin Park dev_set_drvdata(aat2870->dev, aat2870); 38109d6292bSJin Park 38209d6292bSJin Park aat2870->client = client; 38309d6292bSJin Park i2c_set_clientdata(client, aat2870); 38409d6292bSJin Park 38509d6292bSJin Park aat2870->reg_cache = aat2870_regs; 38609d6292bSJin Park 38709d6292bSJin Park if (pdata->en_pin < 0) 38809d6292bSJin Park aat2870->en_pin = -1; 38909d6292bSJin Park else 39009d6292bSJin Park aat2870->en_pin = pdata->en_pin; 39109d6292bSJin Park 39209d6292bSJin Park aat2870->init = pdata->init; 39309d6292bSJin Park aat2870->uninit = pdata->uninit; 39409d6292bSJin Park aat2870->read = aat2870_read; 39509d6292bSJin Park aat2870->write = aat2870_write; 39609d6292bSJin Park aat2870->update = aat2870_update; 39709d6292bSJin Park 39809d6292bSJin Park mutex_init(&aat2870->io_lock); 39909d6292bSJin Park 40009d6292bSJin Park if (aat2870->init) 40109d6292bSJin Park aat2870->init(aat2870); 40209d6292bSJin Park 40309d6292bSJin Park if (aat2870->en_pin >= 0) { 4040287714bSJingoo Han ret = devm_gpio_request_one(&client->dev, aat2870->en_pin, 4050287714bSJingoo Han GPIOF_OUT_INIT_HIGH, "aat2870-en"); 40609d6292bSJin Park if (ret < 0) { 40709d6292bSJin Park dev_err(&client->dev, 40809d6292bSJin Park "Failed to request GPIO %d\n", aat2870->en_pin); 4090287714bSJingoo Han return ret; 41009d6292bSJin Park } 41109d6292bSJin Park } 41209d6292bSJin Park 41309d6292bSJin Park aat2870_enable(aat2870); 41409d6292bSJin Park 41509d6292bSJin Park for (i = 0; i < pdata->num_subdevs; i++) { 41609d6292bSJin Park for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) { 41709d6292bSJin Park if ((pdata->subdevs[i].id == aat2870_devs[j].id) && 41809d6292bSJin Park !strcmp(pdata->subdevs[i].name, 41909d6292bSJin Park aat2870_devs[j].name)) { 42009d6292bSJin Park aat2870_devs[j].platform_data = 42109d6292bSJin Park pdata->subdevs[i].platform_data; 42209d6292bSJin Park break; 42309d6292bSJin Park } 42409d6292bSJin Park } 42509d6292bSJin Park } 42609d6292bSJin Park 42709d6292bSJin Park ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs, 4280848c94fSMark Brown ARRAY_SIZE(aat2870_devs), NULL, 0, NULL); 42909d6292bSJin Park if (ret != 0) { 43009d6292bSJin Park dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret); 43109d6292bSJin Park goto out_disable; 43209d6292bSJin Park } 43309d6292bSJin Park 43409d6292bSJin Park aat2870_init_debugfs(aat2870); 43509d6292bSJin Park 43609d6292bSJin Park return 0; 43709d6292bSJin Park 43809d6292bSJin Park out_disable: 43909d6292bSJin Park aat2870_disable(aat2870); 44009d6292bSJin Park return ret; 44109d6292bSJin Park } 44209d6292bSJin Park 44309d6292bSJin Park static int aat2870_i2c_remove(struct i2c_client *client) 44409d6292bSJin Park { 44509d6292bSJin Park struct aat2870_data *aat2870 = i2c_get_clientdata(client); 44609d6292bSJin Park 44709d6292bSJin Park aat2870_uninit_debugfs(aat2870); 44809d6292bSJin Park 44909d6292bSJin Park mfd_remove_devices(aat2870->dev); 45009d6292bSJin Park aat2870_disable(aat2870); 45109d6292bSJin Park if (aat2870->uninit) 45209d6292bSJin Park aat2870->uninit(aat2870); 45309d6292bSJin Park 45409d6292bSJin Park return 0; 45509d6292bSJin Park } 45609d6292bSJin Park 4575214e565SMark Brown #ifdef CONFIG_PM_SLEEP 4585214e565SMark Brown static int aat2870_i2c_suspend(struct device *dev) 45909d6292bSJin Park { 4605214e565SMark Brown struct i2c_client *client = to_i2c_client(dev); 46109d6292bSJin Park struct aat2870_data *aat2870 = i2c_get_clientdata(client); 46209d6292bSJin Park 46309d6292bSJin Park aat2870_disable(aat2870); 46409d6292bSJin Park 46509d6292bSJin Park return 0; 46609d6292bSJin Park } 46709d6292bSJin Park 4685214e565SMark Brown static int aat2870_i2c_resume(struct device *dev) 46909d6292bSJin Park { 4705214e565SMark Brown struct i2c_client *client = to_i2c_client(dev); 47109d6292bSJin Park struct aat2870_data *aat2870 = i2c_get_clientdata(client); 47209d6292bSJin Park struct aat2870_register *reg = NULL; 47309d6292bSJin Park int i; 47409d6292bSJin Park 47509d6292bSJin Park aat2870_enable(aat2870); 47609d6292bSJin Park 47709d6292bSJin Park /* restore registers */ 47809d6292bSJin Park for (i = 0; i < AAT2870_REG_NUM; i++) { 47909d6292bSJin Park reg = &aat2870->reg_cache[i]; 48009d6292bSJin Park if (reg->writeable) 48109d6292bSJin Park aat2870->write(aat2870, i, reg->value); 48209d6292bSJin Park } 48309d6292bSJin Park 48409d6292bSJin Park return 0; 48509d6292bSJin Park } 4865214e565SMark Brown #endif /* CONFIG_PM_SLEEP */ 4875214e565SMark Brown 4885214e565SMark Brown static SIMPLE_DEV_PM_OPS(aat2870_pm_ops, aat2870_i2c_suspend, 4895214e565SMark Brown aat2870_i2c_resume); 49009d6292bSJin Park 491d4e94863SAxel Lin static const struct i2c_device_id aat2870_i2c_id_table[] = { 49209d6292bSJin Park { "aat2870", 0 }, 49309d6292bSJin Park { } 49409d6292bSJin Park }; 49509d6292bSJin Park MODULE_DEVICE_TABLE(i2c, aat2870_i2c_id_table); 49609d6292bSJin Park 49709d6292bSJin Park static struct i2c_driver aat2870_i2c_driver = { 49809d6292bSJin Park .driver = { 49909d6292bSJin Park .name = "aat2870", 50009d6292bSJin Park .owner = THIS_MODULE, 5015214e565SMark Brown .pm = &aat2870_pm_ops, 50209d6292bSJin Park }, 50309d6292bSJin Park .probe = aat2870_i2c_probe, 50409d6292bSJin Park .remove = aat2870_i2c_remove, 50509d6292bSJin Park .id_table = aat2870_i2c_id_table, 50609d6292bSJin Park }; 50709d6292bSJin Park 50809d6292bSJin Park static int __init aat2870_init(void) 50909d6292bSJin Park { 51009d6292bSJin Park return i2c_add_driver(&aat2870_i2c_driver); 51109d6292bSJin Park } 51209d6292bSJin Park subsys_initcall(aat2870_init); 51309d6292bSJin Park 51409d6292bSJin Park static void __exit aat2870_exit(void) 51509d6292bSJin Park { 51609d6292bSJin Park i2c_del_driver(&aat2870_i2c_driver); 51709d6292bSJin Park } 51809d6292bSJin Park module_exit(aat2870_exit); 51909d6292bSJin Park 52009d6292bSJin Park MODULE_DESCRIPTION("Core support for the AnalogicTech AAT2870"); 52109d6292bSJin Park MODULE_LICENSE("GPL"); 52209d6292bSJin Park MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>"); 523