1 /* 2 * linux/drivers/mfd/aat2870-core.c 3 * 4 * Copyright (c) 2011, NVIDIA Corporation. 5 * Author: Jin Park <jinyoungp@nvidia.com> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * version 2 as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 */ 21 22 #include <linux/kernel.h> 23 #include <linux/init.h> 24 #include <linux/debugfs.h> 25 #include <linux/slab.h> 26 #include <linux/uaccess.h> 27 #include <linux/i2c.h> 28 #include <linux/delay.h> 29 #include <linux/gpio.h> 30 #include <linux/mfd/core.h> 31 #include <linux/mfd/aat2870.h> 32 #include <linux/regulator/machine.h> 33 34 static struct aat2870_register aat2870_regs[AAT2870_REG_NUM] = { 35 /* readable, writeable, value */ 36 { 0, 1, 0x00 }, /* 0x00 AAT2870_BL_CH_EN */ 37 { 0, 1, 0x16 }, /* 0x01 AAT2870_BLM */ 38 { 0, 1, 0x16 }, /* 0x02 AAT2870_BLS */ 39 { 0, 1, 0x56 }, /* 0x03 AAT2870_BL1 */ 40 { 0, 1, 0x56 }, /* 0x04 AAT2870_BL2 */ 41 { 0, 1, 0x56 }, /* 0x05 AAT2870_BL3 */ 42 { 0, 1, 0x56 }, /* 0x06 AAT2870_BL4 */ 43 { 0, 1, 0x56 }, /* 0x07 AAT2870_BL5 */ 44 { 0, 1, 0x56 }, /* 0x08 AAT2870_BL6 */ 45 { 0, 1, 0x56 }, /* 0x09 AAT2870_BL7 */ 46 { 0, 1, 0x56 }, /* 0x0A AAT2870_BL8 */ 47 { 0, 1, 0x00 }, /* 0x0B AAT2870_FLR */ 48 { 0, 1, 0x03 }, /* 0x0C AAT2870_FM */ 49 { 0, 1, 0x03 }, /* 0x0D AAT2870_FS */ 50 { 0, 1, 0x10 }, /* 0x0E AAT2870_ALS_CFG0 */ 51 { 0, 1, 0x06 }, /* 0x0F AAT2870_ALS_CFG1 */ 52 { 0, 1, 0x00 }, /* 0x10 AAT2870_ALS_CFG2 */ 53 { 1, 0, 0x00 }, /* 0x11 AAT2870_AMB */ 54 { 0, 1, 0x00 }, /* 0x12 AAT2870_ALS0 */ 55 { 0, 1, 0x00 }, /* 0x13 AAT2870_ALS1 */ 56 { 0, 1, 0x00 }, /* 0x14 AAT2870_ALS2 */ 57 { 0, 1, 0x00 }, /* 0x15 AAT2870_ALS3 */ 58 { 0, 1, 0x00 }, /* 0x16 AAT2870_ALS4 */ 59 { 0, 1, 0x00 }, /* 0x17 AAT2870_ALS5 */ 60 { 0, 1, 0x00 }, /* 0x18 AAT2870_ALS6 */ 61 { 0, 1, 0x00 }, /* 0x19 AAT2870_ALS7 */ 62 { 0, 1, 0x00 }, /* 0x1A AAT2870_ALS8 */ 63 { 0, 1, 0x00 }, /* 0x1B AAT2870_ALS9 */ 64 { 0, 1, 0x00 }, /* 0x1C AAT2870_ALSA */ 65 { 0, 1, 0x00 }, /* 0x1D AAT2870_ALSB */ 66 { 0, 1, 0x00 }, /* 0x1E AAT2870_ALSC */ 67 { 0, 1, 0x00 }, /* 0x1F AAT2870_ALSD */ 68 { 0, 1, 0x00 }, /* 0x20 AAT2870_ALSE */ 69 { 0, 1, 0x00 }, /* 0x21 AAT2870_ALSF */ 70 { 0, 1, 0x00 }, /* 0x22 AAT2870_SUB_SET */ 71 { 0, 1, 0x00 }, /* 0x23 AAT2870_SUB_CTRL */ 72 { 0, 1, 0x00 }, /* 0x24 AAT2870_LDO_AB */ 73 { 0, 1, 0x00 }, /* 0x25 AAT2870_LDO_CD */ 74 { 0, 1, 0x00 }, /* 0x26 AAT2870_LDO_EN */ 75 }; 76 77 static struct mfd_cell aat2870_devs[] = { 78 { 79 .name = "aat2870-backlight", 80 .id = AAT2870_ID_BL, 81 .pdata_size = sizeof(struct aat2870_bl_platform_data), 82 }, 83 { 84 .name = "aat2870-regulator", 85 .id = AAT2870_ID_LDOA, 86 .pdata_size = sizeof(struct regulator_init_data), 87 }, 88 { 89 .name = "aat2870-regulator", 90 .id = AAT2870_ID_LDOB, 91 .pdata_size = sizeof(struct regulator_init_data), 92 }, 93 { 94 .name = "aat2870-regulator", 95 .id = AAT2870_ID_LDOC, 96 .pdata_size = sizeof(struct regulator_init_data), 97 }, 98 { 99 .name = "aat2870-regulator", 100 .id = AAT2870_ID_LDOD, 101 .pdata_size = sizeof(struct regulator_init_data), 102 }, 103 }; 104 105 static int __aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val) 106 { 107 int ret; 108 109 if (addr >= AAT2870_REG_NUM) { 110 dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr); 111 return -EINVAL; 112 } 113 114 if (!aat2870->reg_cache[addr].readable) { 115 *val = aat2870->reg_cache[addr].value; 116 goto out; 117 } 118 119 ret = i2c_master_send(aat2870->client, &addr, 1); 120 if (ret < 0) 121 return ret; 122 if (ret != 1) 123 return -EIO; 124 125 ret = i2c_master_recv(aat2870->client, val, 1); 126 if (ret < 0) 127 return ret; 128 if (ret != 1) 129 return -EIO; 130 131 out: 132 dev_dbg(aat2870->dev, "read: addr=0x%02x, val=0x%02x\n", addr, *val); 133 return 0; 134 } 135 136 static int __aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val) 137 { 138 u8 msg[2]; 139 int ret; 140 141 if (addr >= AAT2870_REG_NUM) { 142 dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr); 143 return -EINVAL; 144 } 145 146 if (!aat2870->reg_cache[addr].writeable) { 147 dev_err(aat2870->dev, "Address 0x%02x is not writeable\n", 148 addr); 149 return -EINVAL; 150 } 151 152 msg[0] = addr; 153 msg[1] = val; 154 ret = i2c_master_send(aat2870->client, msg, 2); 155 if (ret < 0) 156 return ret; 157 if (ret != 2) 158 return -EIO; 159 160 aat2870->reg_cache[addr].value = val; 161 162 dev_dbg(aat2870->dev, "write: addr=0x%02x, val=0x%02x\n", addr, val); 163 return 0; 164 } 165 166 static int aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val) 167 { 168 int ret; 169 170 mutex_lock(&aat2870->io_lock); 171 ret = __aat2870_read(aat2870, addr, val); 172 mutex_unlock(&aat2870->io_lock); 173 174 return ret; 175 } 176 177 static int aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val) 178 { 179 int ret; 180 181 mutex_lock(&aat2870->io_lock); 182 ret = __aat2870_write(aat2870, addr, val); 183 mutex_unlock(&aat2870->io_lock); 184 185 return ret; 186 } 187 188 static int aat2870_update(struct aat2870_data *aat2870, u8 addr, u8 mask, 189 u8 val) 190 { 191 int change; 192 u8 old_val, new_val; 193 int ret; 194 195 mutex_lock(&aat2870->io_lock); 196 197 ret = __aat2870_read(aat2870, addr, &old_val); 198 if (ret) 199 goto out_unlock; 200 201 new_val = (old_val & ~mask) | (val & mask); 202 change = old_val != new_val; 203 if (change) 204 ret = __aat2870_write(aat2870, addr, new_val); 205 206 out_unlock: 207 mutex_unlock(&aat2870->io_lock); 208 209 return ret; 210 } 211 212 static inline void aat2870_enable(struct aat2870_data *aat2870) 213 { 214 if (aat2870->en_pin >= 0) 215 gpio_set_value(aat2870->en_pin, 1); 216 217 aat2870->is_enable = 1; 218 } 219 220 static inline void aat2870_disable(struct aat2870_data *aat2870) 221 { 222 if (aat2870->en_pin >= 0) 223 gpio_set_value(aat2870->en_pin, 0); 224 225 aat2870->is_enable = 0; 226 } 227 228 #ifdef CONFIG_DEBUG_FS 229 static ssize_t aat2870_dump_reg(struct aat2870_data *aat2870, char *buf) 230 { 231 u8 addr, val; 232 ssize_t count = 0; 233 int ret; 234 235 count += sprintf(buf, "aat2870 registers\n"); 236 for (addr = 0; addr < AAT2870_REG_NUM; addr++) { 237 count += sprintf(buf + count, "0x%02x: ", addr); 238 if (count >= PAGE_SIZE - 1) 239 break; 240 241 ret = aat2870->read(aat2870, addr, &val); 242 if (ret == 0) 243 count += snprintf(buf + count, PAGE_SIZE - count, 244 "0x%02x", val); 245 else 246 count += snprintf(buf + count, PAGE_SIZE - count, 247 "<read fail: %d>", ret); 248 249 if (count >= PAGE_SIZE - 1) 250 break; 251 252 count += snprintf(buf + count, PAGE_SIZE - count, "\n"); 253 if (count >= PAGE_SIZE - 1) 254 break; 255 } 256 257 /* Truncate count; min() would cause a warning */ 258 if (count >= PAGE_SIZE) 259 count = PAGE_SIZE - 1; 260 261 return count; 262 } 263 264 static ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf, 265 size_t count, loff_t *ppos) 266 { 267 struct aat2870_data *aat2870 = file->private_data; 268 char *buf; 269 ssize_t ret; 270 271 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 272 if (!buf) 273 return -ENOMEM; 274 275 ret = aat2870_dump_reg(aat2870, buf); 276 if (ret >= 0) 277 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 278 279 kfree(buf); 280 281 return ret; 282 } 283 284 static ssize_t aat2870_reg_write_file(struct file *file, 285 const char __user *user_buf, size_t count, 286 loff_t *ppos) 287 { 288 struct aat2870_data *aat2870 = file->private_data; 289 char buf[32]; 290 ssize_t buf_size; 291 char *start = buf; 292 unsigned long addr, val; 293 int ret; 294 295 buf_size = min(count, (size_t)(sizeof(buf)-1)); 296 if (copy_from_user(buf, user_buf, buf_size)) { 297 dev_err(aat2870->dev, "Failed to copy from user\n"); 298 return -EFAULT; 299 } 300 buf[buf_size] = 0; 301 302 while (*start == ' ') 303 start++; 304 305 ret = kstrtoul(start, 16, &addr); 306 if (ret) 307 return ret; 308 309 if (addr >= AAT2870_REG_NUM) { 310 dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr); 311 return -EINVAL; 312 } 313 314 while (*start == ' ') 315 start++; 316 317 ret = kstrtoul(start, 16, &val); 318 if (ret) 319 return ret; 320 321 ret = aat2870->write(aat2870, (u8)addr, (u8)val); 322 if (ret) 323 return ret; 324 325 return buf_size; 326 } 327 328 static const struct file_operations aat2870_reg_fops = { 329 .open = simple_open, 330 .read = aat2870_reg_read_file, 331 .write = aat2870_reg_write_file, 332 }; 333 334 static void aat2870_init_debugfs(struct aat2870_data *aat2870) 335 { 336 aat2870->dentry_root = debugfs_create_dir("aat2870", NULL); 337 if (!aat2870->dentry_root) { 338 dev_warn(aat2870->dev, 339 "Failed to create debugfs root directory\n"); 340 return; 341 } 342 343 aat2870->dentry_reg = debugfs_create_file("regs", 0644, 344 aat2870->dentry_root, 345 aat2870, &aat2870_reg_fops); 346 if (!aat2870->dentry_reg) 347 dev_warn(aat2870->dev, 348 "Failed to create debugfs register file\n"); 349 } 350 351 #else 352 static inline void aat2870_init_debugfs(struct aat2870_data *aat2870) 353 { 354 } 355 #endif /* CONFIG_DEBUG_FS */ 356 357 static int aat2870_i2c_probe(struct i2c_client *client, 358 const struct i2c_device_id *id) 359 { 360 struct aat2870_platform_data *pdata = dev_get_platdata(&client->dev); 361 struct aat2870_data *aat2870; 362 int i, j; 363 int ret = 0; 364 365 aat2870 = devm_kzalloc(&client->dev, sizeof(struct aat2870_data), 366 GFP_KERNEL); 367 if (!aat2870) 368 return -ENOMEM; 369 370 aat2870->dev = &client->dev; 371 dev_set_drvdata(aat2870->dev, aat2870); 372 373 aat2870->client = client; 374 i2c_set_clientdata(client, aat2870); 375 376 aat2870->reg_cache = aat2870_regs; 377 378 if (pdata->en_pin < 0) 379 aat2870->en_pin = -1; 380 else 381 aat2870->en_pin = pdata->en_pin; 382 383 aat2870->init = pdata->init; 384 aat2870->uninit = pdata->uninit; 385 aat2870->read = aat2870_read; 386 aat2870->write = aat2870_write; 387 aat2870->update = aat2870_update; 388 389 mutex_init(&aat2870->io_lock); 390 391 if (aat2870->init) 392 aat2870->init(aat2870); 393 394 if (aat2870->en_pin >= 0) { 395 ret = devm_gpio_request_one(&client->dev, aat2870->en_pin, 396 GPIOF_OUT_INIT_HIGH, "aat2870-en"); 397 if (ret < 0) { 398 dev_err(&client->dev, 399 "Failed to request GPIO %d\n", aat2870->en_pin); 400 return ret; 401 } 402 } 403 404 aat2870_enable(aat2870); 405 406 for (i = 0; i < pdata->num_subdevs; i++) { 407 for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) { 408 if ((pdata->subdevs[i].id == aat2870_devs[j].id) && 409 !strcmp(pdata->subdevs[i].name, 410 aat2870_devs[j].name)) { 411 aat2870_devs[j].platform_data = 412 pdata->subdevs[i].platform_data; 413 break; 414 } 415 } 416 } 417 418 ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs, 419 ARRAY_SIZE(aat2870_devs), NULL, 0, NULL); 420 if (ret != 0) { 421 dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret); 422 goto out_disable; 423 } 424 425 aat2870_init_debugfs(aat2870); 426 427 return 0; 428 429 out_disable: 430 aat2870_disable(aat2870); 431 return ret; 432 } 433 434 #ifdef CONFIG_PM_SLEEP 435 static int aat2870_i2c_suspend(struct device *dev) 436 { 437 struct i2c_client *client = to_i2c_client(dev); 438 struct aat2870_data *aat2870 = i2c_get_clientdata(client); 439 440 aat2870_disable(aat2870); 441 442 return 0; 443 } 444 445 static int aat2870_i2c_resume(struct device *dev) 446 { 447 struct i2c_client *client = to_i2c_client(dev); 448 struct aat2870_data *aat2870 = i2c_get_clientdata(client); 449 struct aat2870_register *reg = NULL; 450 int i; 451 452 aat2870_enable(aat2870); 453 454 /* restore registers */ 455 for (i = 0; i < AAT2870_REG_NUM; i++) { 456 reg = &aat2870->reg_cache[i]; 457 if (reg->writeable) 458 aat2870->write(aat2870, i, reg->value); 459 } 460 461 return 0; 462 } 463 #endif /* CONFIG_PM_SLEEP */ 464 465 static SIMPLE_DEV_PM_OPS(aat2870_pm_ops, aat2870_i2c_suspend, 466 aat2870_i2c_resume); 467 468 static const struct i2c_device_id aat2870_i2c_id_table[] = { 469 { "aat2870", 0 }, 470 { } 471 }; 472 473 static struct i2c_driver aat2870_i2c_driver = { 474 .driver = { 475 .name = "aat2870", 476 .pm = &aat2870_pm_ops, 477 .suppress_bind_attrs = true, 478 }, 479 .probe = aat2870_i2c_probe, 480 .id_table = aat2870_i2c_id_table, 481 }; 482 483 static int __init aat2870_init(void) 484 { 485 return i2c_add_driver(&aat2870_i2c_driver); 486 } 487 subsys_initcall(aat2870_init); 488