1f52046b1SBalaji Rao /* NXP PCF50633 Power Management Unit (PMU) driver 2f52046b1SBalaji Rao * 3f52046b1SBalaji Rao * (C) 2006-2008 by Openmoko, Inc. 4f52046b1SBalaji Rao * Author: Harald Welte <laforge@openmoko.org> 5f52046b1SBalaji Rao * Balaji Rao <balajirrao@openmoko.org> 6f52046b1SBalaji Rao * All rights reserved. 7f52046b1SBalaji Rao * 8f52046b1SBalaji Rao * This program is free software; you can redistribute it and/or modify it 9f52046b1SBalaji Rao * under the terms of the GNU General Public License as published by the 10f52046b1SBalaji Rao * Free Software Foundation; either version 2 of the License, or (at your 11f52046b1SBalaji Rao * option) any later version. 12f52046b1SBalaji Rao * 13f52046b1SBalaji Rao */ 14f52046b1SBalaji Rao 15f52046b1SBalaji Rao #include <linux/kernel.h> 16f52046b1SBalaji Rao #include <linux/device.h> 17f52046b1SBalaji Rao #include <linux/sysfs.h> 18f52046b1SBalaji Rao #include <linux/module.h> 19f52046b1SBalaji Rao #include <linux/types.h> 20f52046b1SBalaji Rao #include <linux/interrupt.h> 21f52046b1SBalaji Rao #include <linux/workqueue.h> 22f52046b1SBalaji Rao #include <linux/platform_device.h> 23f52046b1SBalaji Rao #include <linux/i2c.h> 24939941d4SMark Brown #include <linux/pm.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 266e3ad118SMark Brown #include <linux/regmap.h> 276e3ad118SMark Brown #include <linux/err.h> 28f52046b1SBalaji Rao 29f52046b1SBalaji Rao #include <linux/mfd/pcf50633/core.h> 30f52046b1SBalaji Rao 31f52046b1SBalaji Rao /* Read a block of up to 32 regs */ 32f52046b1SBalaji Rao int pcf50633_read_block(struct pcf50633 *pcf, u8 reg, 33f52046b1SBalaji Rao int nr_regs, u8 *data) 34f52046b1SBalaji Rao { 35f52046b1SBalaji Rao int ret; 36f52046b1SBalaji Rao 376e3ad118SMark Brown ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs); 386e3ad118SMark Brown if (ret != 0) 39f52046b1SBalaji Rao return ret; 406e3ad118SMark Brown 416e3ad118SMark Brown return nr_regs; 42f52046b1SBalaji Rao } 43f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_read_block); 44f52046b1SBalaji Rao 45f52046b1SBalaji Rao /* Write a block of up to 32 regs */ 46f52046b1SBalaji Rao int pcf50633_write_block(struct pcf50633 *pcf , u8 reg, 47f52046b1SBalaji Rao int nr_regs, u8 *data) 48f52046b1SBalaji Rao { 4960b5c5a4SAxel Lin return regmap_raw_write(pcf->regmap, reg, data, nr_regs); 50f52046b1SBalaji Rao } 51f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_write_block); 52f52046b1SBalaji Rao 53f52046b1SBalaji Rao u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg) 54f52046b1SBalaji Rao { 556e3ad118SMark Brown unsigned int val; 566e3ad118SMark Brown int ret; 57f52046b1SBalaji Rao 586e3ad118SMark Brown ret = regmap_read(pcf->regmap, reg, &val); 596e3ad118SMark Brown if (ret < 0) 606e3ad118SMark Brown return -1; 61f52046b1SBalaji Rao 62f52046b1SBalaji Rao return val; 63f52046b1SBalaji Rao } 64f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_read); 65f52046b1SBalaji Rao 66f52046b1SBalaji Rao int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val) 67f52046b1SBalaji Rao { 686e3ad118SMark Brown return regmap_write(pcf->regmap, reg, val); 69f52046b1SBalaji Rao } 70f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_write); 71f52046b1SBalaji Rao 72f52046b1SBalaji Rao int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val) 73f52046b1SBalaji Rao { 746e3ad118SMark Brown return regmap_update_bits(pcf->regmap, reg, mask, val); 75f52046b1SBalaji Rao } 76f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask); 77f52046b1SBalaji Rao 78f52046b1SBalaji Rao int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val) 79f52046b1SBalaji Rao { 806e3ad118SMark Brown return regmap_update_bits(pcf->regmap, reg, val, 0); 81f52046b1SBalaji Rao } 82f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits); 83f52046b1SBalaji Rao 84f52046b1SBalaji Rao /* sysfs attributes */ 85f52046b1SBalaji Rao static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr, 86f52046b1SBalaji Rao char *buf) 87f52046b1SBalaji Rao { 88f52046b1SBalaji Rao struct pcf50633 *pcf = dev_get_drvdata(dev); 89f52046b1SBalaji Rao u8 dump[16]; 90f52046b1SBalaji Rao int n, n1, idx = 0; 91f52046b1SBalaji Rao char *buf1 = buf; 92f52046b1SBalaji Rao static u8 address_no_read[] = { /* must be ascending */ 93f52046b1SBalaji Rao PCF50633_REG_INT1, 94f52046b1SBalaji Rao PCF50633_REG_INT2, 95f52046b1SBalaji Rao PCF50633_REG_INT3, 96f52046b1SBalaji Rao PCF50633_REG_INT4, 97f52046b1SBalaji Rao PCF50633_REG_INT5, 98f52046b1SBalaji Rao 0 /* terminator */ 99f52046b1SBalaji Rao }; 100f52046b1SBalaji Rao 101f52046b1SBalaji Rao for (n = 0; n < 256; n += sizeof(dump)) { 102f52046b1SBalaji Rao for (n1 = 0; n1 < sizeof(dump); n1++) 103f52046b1SBalaji Rao if (n == address_no_read[idx]) { 104f52046b1SBalaji Rao idx++; 105f52046b1SBalaji Rao dump[n1] = 0x00; 106f52046b1SBalaji Rao } else 107f52046b1SBalaji Rao dump[n1] = pcf50633_reg_read(pcf, n + n1); 108f52046b1SBalaji Rao 109f52046b1SBalaji Rao hex_dump_to_buffer(dump, sizeof(dump), 16, 1, buf1, 128, 0); 110f52046b1SBalaji Rao buf1 += strlen(buf1); 111f52046b1SBalaji Rao *buf1++ = '\n'; 112f52046b1SBalaji Rao *buf1 = '\0'; 113f52046b1SBalaji Rao } 114f52046b1SBalaji Rao 115f52046b1SBalaji Rao return buf1 - buf; 116f52046b1SBalaji Rao } 117f52046b1SBalaji Rao static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL); 118f52046b1SBalaji Rao 119f52046b1SBalaji Rao static ssize_t show_resume_reason(struct device *dev, 120f52046b1SBalaji Rao struct device_attribute *attr, char *buf) 121f52046b1SBalaji Rao { 122f52046b1SBalaji Rao struct pcf50633 *pcf = dev_get_drvdata(dev); 123f52046b1SBalaji Rao int n; 124f52046b1SBalaji Rao 125f52046b1SBalaji Rao n = sprintf(buf, "%02x%02x%02x%02x%02x\n", 126f52046b1SBalaji Rao pcf->resume_reason[0], 127f52046b1SBalaji Rao pcf->resume_reason[1], 128f52046b1SBalaji Rao pcf->resume_reason[2], 129f52046b1SBalaji Rao pcf->resume_reason[3], 130f52046b1SBalaji Rao pcf->resume_reason[4]); 131f52046b1SBalaji Rao 132f52046b1SBalaji Rao return n; 133f52046b1SBalaji Rao } 134f52046b1SBalaji Rao static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL); 135f52046b1SBalaji Rao 136f52046b1SBalaji Rao static struct attribute *pcf_sysfs_entries[] = { 137f52046b1SBalaji Rao &dev_attr_dump_regs.attr, 138f52046b1SBalaji Rao &dev_attr_resume_reason.attr, 139f52046b1SBalaji Rao NULL, 140f52046b1SBalaji Rao }; 141f52046b1SBalaji Rao 142f52046b1SBalaji Rao static struct attribute_group pcf_attr_group = { 143f52046b1SBalaji Rao .name = NULL, /* put in device directory */ 144f52046b1SBalaji Rao .attrs = pcf_sysfs_entries, 145f52046b1SBalaji Rao }; 146f52046b1SBalaji Rao 147f52046b1SBalaji Rao static void 148f52046b1SBalaji Rao pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name, 149f52046b1SBalaji Rao struct platform_device **pdev) 150f52046b1SBalaji Rao { 151f52046b1SBalaji Rao int ret; 152f52046b1SBalaji Rao 153f52046b1SBalaji Rao *pdev = platform_device_alloc(name, -1); 154f52046b1SBalaji Rao if (!*pdev) { 155f52046b1SBalaji Rao dev_err(pcf->dev, "Falied to allocate %s\n", name); 156f52046b1SBalaji Rao return; 157f52046b1SBalaji Rao } 158f52046b1SBalaji Rao 159f52046b1SBalaji Rao (*pdev)->dev.parent = pcf->dev; 160f52046b1SBalaji Rao 161f52046b1SBalaji Rao ret = platform_device_add(*pdev); 162f52046b1SBalaji Rao if (ret) { 163f52046b1SBalaji Rao dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret); 164f52046b1SBalaji Rao platform_device_put(*pdev); 165f52046b1SBalaji Rao *pdev = NULL; 166f52046b1SBalaji Rao } 167f52046b1SBalaji Rao } 168f52046b1SBalaji Rao 169939941d4SMark Brown #ifdef CONFIG_PM_SLEEP 170939941d4SMark Brown static int pcf50633_suspend(struct device *dev) 171f52046b1SBalaji Rao { 172939941d4SMark Brown struct i2c_client *client = to_i2c_client(dev); 173939941d4SMark Brown struct pcf50633 *pcf = i2c_get_clientdata(client); 174f52046b1SBalaji Rao 175380c09f6SLars-Peter Clausen return pcf50633_irq_suspend(pcf); 176f52046b1SBalaji Rao } 177f52046b1SBalaji Rao 178939941d4SMark Brown static int pcf50633_resume(struct device *dev) 179f52046b1SBalaji Rao { 180939941d4SMark Brown struct i2c_client *client = to_i2c_client(dev); 181939941d4SMark Brown struct pcf50633 *pcf = i2c_get_clientdata(client); 182f52046b1SBalaji Rao 183380c09f6SLars-Peter Clausen return pcf50633_irq_resume(pcf); 184f52046b1SBalaji Rao } 185f52046b1SBalaji Rao #endif 186f52046b1SBalaji Rao 187939941d4SMark Brown static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume); 188939941d4SMark Brown 1896e3ad118SMark Brown static struct regmap_config pcf50633_regmap_config = { 1906e3ad118SMark Brown .reg_bits = 8, 1916e3ad118SMark Brown .val_bits = 8, 1926e3ad118SMark Brown }; 1936e3ad118SMark Brown 194f791be49SBill Pemberton static int pcf50633_probe(struct i2c_client *client, 195f52046b1SBalaji Rao const struct i2c_device_id *ids) 196f52046b1SBalaji Rao { 197f52046b1SBalaji Rao struct pcf50633 *pcf; 198334a41ceSJingoo Han struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev); 19924213ae1SLars-Peter Clausen int i, ret; 200f52046b1SBalaji Rao int version, variant; 201f52046b1SBalaji Rao 20224213ae1SLars-Peter Clausen if (!client->irq) { 20324213ae1SLars-Peter Clausen dev_err(&client->dev, "Missing IRQ\n"); 20424213ae1SLars-Peter Clausen return -ENOENT; 20524213ae1SLars-Peter Clausen } 20624213ae1SLars-Peter Clausen 207aa4603a0SAxel Lin pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL); 208f52046b1SBalaji Rao if (!pcf) 209f52046b1SBalaji Rao return -ENOMEM; 210f52046b1SBalaji Rao 211b30dd8f2SAxel Lin i2c_set_clientdata(client, pcf); 212b30dd8f2SAxel Lin pcf->dev = &client->dev; 213f52046b1SBalaji Rao pcf->pdata = pdata; 214f52046b1SBalaji Rao 215f52046b1SBalaji Rao mutex_init(&pcf->lock); 216f52046b1SBalaji Rao 217aa4603a0SAxel Lin pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config); 2186e3ad118SMark Brown if (IS_ERR(pcf->regmap)) { 2196e3ad118SMark Brown ret = PTR_ERR(pcf->regmap); 220aa4603a0SAxel Lin dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret); 221aa4603a0SAxel Lin return ret; 2226e3ad118SMark Brown } 2236e3ad118SMark Brown 224f52046b1SBalaji Rao version = pcf50633_reg_read(pcf, 0); 225f52046b1SBalaji Rao variant = pcf50633_reg_read(pcf, 1); 226f52046b1SBalaji Rao if (version < 0 || variant < 0) { 227f52046b1SBalaji Rao dev_err(pcf->dev, "Unable to probe pcf50633\n"); 228f52046b1SBalaji Rao ret = -ENODEV; 229aa4603a0SAxel Lin return ret; 230f52046b1SBalaji Rao } 231f52046b1SBalaji Rao 232f52046b1SBalaji Rao dev_info(pcf->dev, "Probed device version %d variant %d\n", 233f52046b1SBalaji Rao version, variant); 234f52046b1SBalaji Rao 235380c09f6SLars-Peter Clausen pcf50633_irq_init(pcf, client->irq); 23624213ae1SLars-Peter Clausen 237f52046b1SBalaji Rao /* Create sub devices */ 238aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev); 239aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev); 240aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev); 241aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev); 242aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev); 243f5bf403aSLars-Peter Clausen 244f52046b1SBalaji Rao 245f52046b1SBalaji Rao for (i = 0; i < PCF50633_NUM_REGULATORS; i++) { 246f52046b1SBalaji Rao struct platform_device *pdev; 247f52046b1SBalaji Rao 248*561427f5SAxel Lin pdev = platform_device_alloc("pcf50633-regulator", i); 249f52046b1SBalaji Rao if (!pdev) { 25024213ae1SLars-Peter Clausen dev_err(pcf->dev, "Cannot create regulator %d\n", i); 251f52046b1SBalaji Rao continue; 252f52046b1SBalaji Rao } 253f52046b1SBalaji Rao 254f52046b1SBalaji Rao pdev->dev.parent = pcf->dev; 25518273c5bSAlan Cox if (platform_device_add_data(pdev, &pdata->reg_init_data[i], 25618273c5bSAlan Cox sizeof(pdata->reg_init_data[i])) < 0) { 25718273c5bSAlan Cox platform_device_put(pdev); 25818273c5bSAlan Cox dev_err(pcf->dev, "Out of memory for regulator parameters %d\n", 25918273c5bSAlan Cox i); 26018273c5bSAlan Cox continue; 26118273c5bSAlan Cox } 262f52046b1SBalaji Rao pcf->regulator_pdev[i] = pdev; 263f52046b1SBalaji Rao 264f52046b1SBalaji Rao platform_device_add(pdev); 265f52046b1SBalaji Rao } 266f52046b1SBalaji Rao 267f52046b1SBalaji Rao ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group); 268f52046b1SBalaji Rao if (ret) 269f52046b1SBalaji Rao dev_err(pcf->dev, "error creating sysfs entries\n"); 270f52046b1SBalaji Rao 271f52046b1SBalaji Rao if (pdata->probe_done) 272f52046b1SBalaji Rao pdata->probe_done(pcf); 273f52046b1SBalaji Rao 274f52046b1SBalaji Rao return 0; 275f52046b1SBalaji Rao } 276f52046b1SBalaji Rao 2774740f73fSBill Pemberton static int pcf50633_remove(struct i2c_client *client) 278f52046b1SBalaji Rao { 279f52046b1SBalaji Rao struct pcf50633 *pcf = i2c_get_clientdata(client); 280f52046b1SBalaji Rao int i; 281f52046b1SBalaji Rao 2828220fe4cSAxel Lin sysfs_remove_group(&client->dev.kobj, &pcf_attr_group); 283380c09f6SLars-Peter Clausen pcf50633_irq_free(pcf); 284f52046b1SBalaji Rao 285f52046b1SBalaji Rao platform_device_unregister(pcf->input_pdev); 286f52046b1SBalaji Rao platform_device_unregister(pcf->rtc_pdev); 287f52046b1SBalaji Rao platform_device_unregister(pcf->mbc_pdev); 288f52046b1SBalaji Rao platform_device_unregister(pcf->adc_pdev); 2898220fe4cSAxel Lin platform_device_unregister(pcf->bl_pdev); 290f52046b1SBalaji Rao 291f52046b1SBalaji Rao for (i = 0; i < PCF50633_NUM_REGULATORS; i++) 292f52046b1SBalaji Rao platform_device_unregister(pcf->regulator_pdev[i]); 293f52046b1SBalaji Rao 294f52046b1SBalaji Rao return 0; 295f52046b1SBalaji Rao } 296f52046b1SBalaji Rao 2971206552bSAxel Lin static const struct i2c_device_id pcf50633_id_table[] = { 298f52046b1SBalaji Rao {"pcf50633", 0x73}, 2998915e540SJean Delvare {/* end of list */} 300f52046b1SBalaji Rao }; 3017679089dSAxel Lin MODULE_DEVICE_TABLE(i2c, pcf50633_id_table); 302f52046b1SBalaji Rao 303f52046b1SBalaji Rao static struct i2c_driver pcf50633_driver = { 304f52046b1SBalaji Rao .driver = { 305f52046b1SBalaji Rao .name = "pcf50633", 306939941d4SMark Brown .pm = &pcf50633_pm, 307f52046b1SBalaji Rao }, 308f52046b1SBalaji Rao .id_table = pcf50633_id_table, 309f52046b1SBalaji Rao .probe = pcf50633_probe, 31084449216SBill Pemberton .remove = pcf50633_remove, 311f52046b1SBalaji Rao }; 312f52046b1SBalaji Rao 313f52046b1SBalaji Rao static int __init pcf50633_init(void) 314f52046b1SBalaji Rao { 315f52046b1SBalaji Rao return i2c_add_driver(&pcf50633_driver); 316f52046b1SBalaji Rao } 317f52046b1SBalaji Rao 318f52046b1SBalaji Rao static void __exit pcf50633_exit(void) 319f52046b1SBalaji Rao { 320f52046b1SBalaji Rao i2c_del_driver(&pcf50633_driver); 321f52046b1SBalaji Rao } 322f52046b1SBalaji Rao 323f52046b1SBalaji Rao MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU"); 324f52046b1SBalaji Rao MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>"); 325f52046b1SBalaji Rao MODULE_LICENSE("GPL"); 326f52046b1SBalaji Rao 3272021de87SSamuel Ortiz subsys_initcall(pcf50633_init); 328f52046b1SBalaji Rao module_exit(pcf50633_exit); 329