12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2f52046b1SBalaji Rao /* NXP PCF50633 Power Management Unit (PMU) driver 3f52046b1SBalaji Rao * 4f52046b1SBalaji Rao * (C) 2006-2008 by Openmoko, Inc. 5f52046b1SBalaji Rao * Author: Harald Welte <laforge@openmoko.org> 6f52046b1SBalaji Rao * Balaji Rao <balajirrao@openmoko.org> 7f52046b1SBalaji Rao * All rights reserved. 8f52046b1SBalaji Rao */ 9f52046b1SBalaji Rao 10f52046b1SBalaji Rao #include <linux/kernel.h> 11f52046b1SBalaji Rao #include <linux/device.h> 12f52046b1SBalaji Rao #include <linux/sysfs.h> 13f52046b1SBalaji Rao #include <linux/module.h> 14f52046b1SBalaji Rao #include <linux/types.h> 15f52046b1SBalaji Rao #include <linux/interrupt.h> 16f52046b1SBalaji Rao #include <linux/workqueue.h> 17f52046b1SBalaji Rao #include <linux/platform_device.h> 18f52046b1SBalaji Rao #include <linux/i2c.h> 19939941d4SMark Brown #include <linux/pm.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 216e3ad118SMark Brown #include <linux/regmap.h> 226e3ad118SMark Brown #include <linux/err.h> 23f52046b1SBalaji Rao 24f52046b1SBalaji Rao #include <linux/mfd/pcf50633/core.h> 25f52046b1SBalaji Rao 26f52046b1SBalaji Rao /* Read a block of up to 32 regs */ 27f52046b1SBalaji Rao int pcf50633_read_block(struct pcf50633 *pcf, u8 reg, 28f52046b1SBalaji Rao int nr_regs, u8 *data) 29f52046b1SBalaji Rao { 30f52046b1SBalaji Rao int ret; 31f52046b1SBalaji Rao 326e3ad118SMark Brown ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs); 336e3ad118SMark Brown if (ret != 0) 34f52046b1SBalaji Rao return ret; 356e3ad118SMark Brown 366e3ad118SMark Brown return nr_regs; 37f52046b1SBalaji Rao } 38f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_read_block); 39f52046b1SBalaji Rao 40f52046b1SBalaji Rao /* Write a block of up to 32 regs */ 41f52046b1SBalaji Rao int pcf50633_write_block(struct pcf50633 *pcf , u8 reg, 42f52046b1SBalaji Rao int nr_regs, u8 *data) 43f52046b1SBalaji Rao { 4460b5c5a4SAxel Lin return regmap_raw_write(pcf->regmap, reg, data, nr_regs); 45f52046b1SBalaji Rao } 46f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_write_block); 47f52046b1SBalaji Rao 48f52046b1SBalaji Rao u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg) 49f52046b1SBalaji Rao { 506e3ad118SMark Brown unsigned int val; 516e3ad118SMark Brown int ret; 52f52046b1SBalaji Rao 536e3ad118SMark Brown ret = regmap_read(pcf->regmap, reg, &val); 546e3ad118SMark Brown if (ret < 0) 556e3ad118SMark Brown return -1; 56f52046b1SBalaji Rao 57f52046b1SBalaji Rao return val; 58f52046b1SBalaji Rao } 59f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_read); 60f52046b1SBalaji Rao 61f52046b1SBalaji Rao int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val) 62f52046b1SBalaji Rao { 636e3ad118SMark Brown return regmap_write(pcf->regmap, reg, val); 64f52046b1SBalaji Rao } 65f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_write); 66f52046b1SBalaji Rao 67f52046b1SBalaji Rao int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val) 68f52046b1SBalaji Rao { 696e3ad118SMark Brown return regmap_update_bits(pcf->regmap, reg, mask, val); 70f52046b1SBalaji Rao } 71f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask); 72f52046b1SBalaji Rao 73f52046b1SBalaji Rao int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val) 74f52046b1SBalaji Rao { 756e3ad118SMark Brown return regmap_update_bits(pcf->regmap, reg, val, 0); 76f52046b1SBalaji Rao } 77f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits); 78f52046b1SBalaji Rao 79f52046b1SBalaji Rao /* sysfs attributes */ 80426d94a9SZhen Lei static ssize_t dump_regs_show(struct device *dev, 81426d94a9SZhen Lei struct device_attribute *attr, char *buf) 82f52046b1SBalaji Rao { 83f52046b1SBalaji Rao struct pcf50633 *pcf = dev_get_drvdata(dev); 84f52046b1SBalaji Rao u8 dump[16]; 85f52046b1SBalaji Rao int n, n1, idx = 0; 86f52046b1SBalaji Rao char *buf1 = buf; 87f52046b1SBalaji Rao static u8 address_no_read[] = { /* must be ascending */ 88f52046b1SBalaji Rao PCF50633_REG_INT1, 89f52046b1SBalaji Rao PCF50633_REG_INT2, 90f52046b1SBalaji Rao PCF50633_REG_INT3, 91f52046b1SBalaji Rao PCF50633_REG_INT4, 92f52046b1SBalaji Rao PCF50633_REG_INT5, 93f52046b1SBalaji Rao 0 /* terminator */ 94f52046b1SBalaji Rao }; 95f52046b1SBalaji Rao 96f52046b1SBalaji Rao for (n = 0; n < 256; n += sizeof(dump)) { 97f52046b1SBalaji Rao for (n1 = 0; n1 < sizeof(dump); n1++) 98f52046b1SBalaji Rao if (n == address_no_read[idx]) { 99f52046b1SBalaji Rao idx++; 100f52046b1SBalaji Rao dump[n1] = 0x00; 101f52046b1SBalaji Rao } else 102f52046b1SBalaji Rao dump[n1] = pcf50633_reg_read(pcf, n + n1); 103f52046b1SBalaji Rao 104970d9fbcSAndy Shevchenko buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump); 105f52046b1SBalaji Rao } 106f52046b1SBalaji Rao 107f52046b1SBalaji Rao return buf1 - buf; 108f52046b1SBalaji Rao } 109426d94a9SZhen Lei static DEVICE_ATTR_ADMIN_RO(dump_regs); 110f52046b1SBalaji Rao 111426d94a9SZhen Lei static ssize_t resume_reason_show(struct device *dev, 112f52046b1SBalaji Rao struct device_attribute *attr, char *buf) 113f52046b1SBalaji Rao { 114f52046b1SBalaji Rao struct pcf50633 *pcf = dev_get_drvdata(dev); 115f52046b1SBalaji Rao int n; 116f52046b1SBalaji Rao 117f52046b1SBalaji Rao n = sprintf(buf, "%02x%02x%02x%02x%02x\n", 118f52046b1SBalaji Rao pcf->resume_reason[0], 119f52046b1SBalaji Rao pcf->resume_reason[1], 120f52046b1SBalaji Rao pcf->resume_reason[2], 121f52046b1SBalaji Rao pcf->resume_reason[3], 122f52046b1SBalaji Rao pcf->resume_reason[4]); 123f52046b1SBalaji Rao 124f52046b1SBalaji Rao return n; 125f52046b1SBalaji Rao } 126426d94a9SZhen Lei static DEVICE_ATTR_ADMIN_RO(resume_reason); 127f52046b1SBalaji Rao 128f52046b1SBalaji Rao static struct attribute *pcf_sysfs_entries[] = { 129f52046b1SBalaji Rao &dev_attr_dump_regs.attr, 130f52046b1SBalaji Rao &dev_attr_resume_reason.attr, 131f52046b1SBalaji Rao NULL, 132f52046b1SBalaji Rao }; 133f52046b1SBalaji Rao 134f52046b1SBalaji Rao static struct attribute_group pcf_attr_group = { 135f52046b1SBalaji Rao .name = NULL, /* put in device directory */ 136f52046b1SBalaji Rao .attrs = pcf_sysfs_entries, 137f52046b1SBalaji Rao }; 138f52046b1SBalaji Rao 139f52046b1SBalaji Rao static void 140f52046b1SBalaji Rao pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name, 141f52046b1SBalaji Rao struct platform_device **pdev) 142f52046b1SBalaji Rao { 143f52046b1SBalaji Rao int ret; 144f52046b1SBalaji Rao 145f52046b1SBalaji Rao *pdev = platform_device_alloc(name, -1); 146f52046b1SBalaji Rao if (!*pdev) { 147f058aa3fSArvind Yadav dev_err(pcf->dev, "Failed to allocate %s\n", name); 148f52046b1SBalaji Rao return; 149f52046b1SBalaji Rao } 150f52046b1SBalaji Rao 151f52046b1SBalaji Rao (*pdev)->dev.parent = pcf->dev; 152f52046b1SBalaji Rao 153f52046b1SBalaji Rao ret = platform_device_add(*pdev); 154f52046b1SBalaji Rao if (ret) { 155f52046b1SBalaji Rao dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret); 156f52046b1SBalaji Rao platform_device_put(*pdev); 157f52046b1SBalaji Rao *pdev = NULL; 158f52046b1SBalaji Rao } 159f52046b1SBalaji Rao } 160f52046b1SBalaji Rao 161939941d4SMark Brown #ifdef CONFIG_PM_SLEEP 162939941d4SMark Brown static int pcf50633_suspend(struct device *dev) 163f52046b1SBalaji Rao { 164939941d4SMark Brown struct i2c_client *client = to_i2c_client(dev); 165939941d4SMark Brown struct pcf50633 *pcf = i2c_get_clientdata(client); 166f52046b1SBalaji Rao 167380c09f6SLars-Peter Clausen return pcf50633_irq_suspend(pcf); 168f52046b1SBalaji Rao } 169f52046b1SBalaji Rao 170939941d4SMark Brown static int pcf50633_resume(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_resume(pcf); 176f52046b1SBalaji Rao } 177f52046b1SBalaji Rao #endif 178f52046b1SBalaji Rao 179939941d4SMark Brown static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume); 180939941d4SMark Brown 1811590d4a1SKrzysztof Kozlowski static const struct regmap_config pcf50633_regmap_config = { 1826e3ad118SMark Brown .reg_bits = 8, 1836e3ad118SMark Brown .val_bits = 8, 1846e3ad118SMark Brown }; 1856e3ad118SMark Brown 186f791be49SBill Pemberton static int pcf50633_probe(struct i2c_client *client, 187f52046b1SBalaji Rao const struct i2c_device_id *ids) 188f52046b1SBalaji Rao { 189f52046b1SBalaji Rao struct pcf50633 *pcf; 190cddc1141SLee Jones struct platform_device *pdev; 191334a41ceSJingoo Han struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev); 192cddc1141SLee Jones int i, j, ret; 193f52046b1SBalaji Rao int version, variant; 194f52046b1SBalaji Rao 19524213ae1SLars-Peter Clausen if (!client->irq) { 19624213ae1SLars-Peter Clausen dev_err(&client->dev, "Missing IRQ\n"); 19724213ae1SLars-Peter Clausen return -ENOENT; 19824213ae1SLars-Peter Clausen } 19924213ae1SLars-Peter Clausen 200aa4603a0SAxel Lin pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL); 201f52046b1SBalaji Rao if (!pcf) 202f52046b1SBalaji Rao return -ENOMEM; 203f52046b1SBalaji Rao 204b30dd8f2SAxel Lin i2c_set_clientdata(client, pcf); 205b30dd8f2SAxel Lin pcf->dev = &client->dev; 206f52046b1SBalaji Rao pcf->pdata = pdata; 207f52046b1SBalaji Rao 208f52046b1SBalaji Rao mutex_init(&pcf->lock); 209f52046b1SBalaji Rao 210aa4603a0SAxel Lin pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config); 2116e3ad118SMark Brown if (IS_ERR(pcf->regmap)) { 2126e3ad118SMark Brown ret = PTR_ERR(pcf->regmap); 213aa4603a0SAxel Lin dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret); 214aa4603a0SAxel Lin return ret; 2156e3ad118SMark Brown } 2166e3ad118SMark Brown 217f52046b1SBalaji Rao version = pcf50633_reg_read(pcf, 0); 218f52046b1SBalaji Rao variant = pcf50633_reg_read(pcf, 1); 219f52046b1SBalaji Rao if (version < 0 || variant < 0) { 220f52046b1SBalaji Rao dev_err(pcf->dev, "Unable to probe pcf50633\n"); 221f52046b1SBalaji Rao ret = -ENODEV; 222aa4603a0SAxel Lin return ret; 223f52046b1SBalaji Rao } 224f52046b1SBalaji Rao 225f52046b1SBalaji Rao dev_info(pcf->dev, "Probed device version %d variant %d\n", 226f52046b1SBalaji Rao version, variant); 227f52046b1SBalaji Rao 228380c09f6SLars-Peter Clausen pcf50633_irq_init(pcf, client->irq); 22924213ae1SLars-Peter Clausen 230f52046b1SBalaji Rao /* Create sub devices */ 231aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev); 232aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev); 233aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev); 234aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev); 235aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev); 236f5bf403aSLars-Peter Clausen 237f52046b1SBalaji Rao 238f52046b1SBalaji Rao for (i = 0; i < PCF50633_NUM_REGULATORS; i++) { 239561427f5SAxel Lin pdev = platform_device_alloc("pcf50633-regulator", i); 24052a3a374SChristophe JAILLET if (!pdev) { 24152a3a374SChristophe JAILLET ret = -ENOMEM; 24252a3a374SChristophe JAILLET goto err2; 24352a3a374SChristophe JAILLET } 244f52046b1SBalaji Rao 245f52046b1SBalaji Rao pdev->dev.parent = pcf->dev; 246c981015eSLee Jones ret = platform_device_add_data(pdev, &pdata->reg_init_data[i], 247c981015eSLee Jones sizeof(pdata->reg_init_data[i])); 248cddc1141SLee Jones if (ret) 249cddc1141SLee Jones goto err; 250f52046b1SBalaji Rao 251cddc1141SLee Jones ret = platform_device_add(pdev); 252cddc1141SLee Jones if (ret) 253cddc1141SLee Jones goto err; 254cddc1141SLee Jones 255cddc1141SLee Jones pcf->regulator_pdev[i] = pdev; 256f52046b1SBalaji Rao } 257f52046b1SBalaji Rao 258f52046b1SBalaji Rao ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group); 259f52046b1SBalaji Rao if (ret) 260cddc1141SLee Jones dev_warn(pcf->dev, "error creating sysfs entries\n"); 261f52046b1SBalaji Rao 262f52046b1SBalaji Rao if (pdata->probe_done) 263f52046b1SBalaji Rao pdata->probe_done(pcf); 264f52046b1SBalaji Rao 265f52046b1SBalaji Rao return 0; 266cddc1141SLee Jones 267cddc1141SLee Jones err: 268cddc1141SLee Jones platform_device_put(pdev); 26952a3a374SChristophe JAILLET err2: 270cddc1141SLee Jones for (j = 0; j < i; j++) 271cddc1141SLee Jones platform_device_put(pcf->regulator_pdev[j]); 272cddc1141SLee Jones 273cddc1141SLee Jones return ret; 274f52046b1SBalaji Rao } 275f52046b1SBalaji Rao 276*ed5c2f5fSUwe Kleine-König static void pcf50633_remove(struct i2c_client *client) 277f52046b1SBalaji Rao { 278f52046b1SBalaji Rao struct pcf50633 *pcf = i2c_get_clientdata(client); 279f52046b1SBalaji Rao int i; 280f52046b1SBalaji Rao 2818220fe4cSAxel Lin sysfs_remove_group(&client->dev.kobj, &pcf_attr_group); 282380c09f6SLars-Peter Clausen pcf50633_irq_free(pcf); 283f52046b1SBalaji Rao 284f52046b1SBalaji Rao platform_device_unregister(pcf->input_pdev); 285f52046b1SBalaji Rao platform_device_unregister(pcf->rtc_pdev); 286f52046b1SBalaji Rao platform_device_unregister(pcf->mbc_pdev); 287f52046b1SBalaji Rao platform_device_unregister(pcf->adc_pdev); 2888220fe4cSAxel Lin platform_device_unregister(pcf->bl_pdev); 289f52046b1SBalaji Rao 290f52046b1SBalaji Rao for (i = 0; i < PCF50633_NUM_REGULATORS; i++) 291f52046b1SBalaji Rao platform_device_unregister(pcf->regulator_pdev[i]); 292f52046b1SBalaji Rao } 293f52046b1SBalaji Rao 2941206552bSAxel Lin static const struct i2c_device_id pcf50633_id_table[] = { 295f52046b1SBalaji Rao {"pcf50633", 0x73}, 2968915e540SJean Delvare {/* end of list */} 297f52046b1SBalaji Rao }; 2987679089dSAxel Lin MODULE_DEVICE_TABLE(i2c, pcf50633_id_table); 299f52046b1SBalaji Rao 300f52046b1SBalaji Rao static struct i2c_driver pcf50633_driver = { 301f52046b1SBalaji Rao .driver = { 302f52046b1SBalaji Rao .name = "pcf50633", 303939941d4SMark Brown .pm = &pcf50633_pm, 304f52046b1SBalaji Rao }, 305f52046b1SBalaji Rao .id_table = pcf50633_id_table, 306f52046b1SBalaji Rao .probe = pcf50633_probe, 30784449216SBill Pemberton .remove = pcf50633_remove, 308f52046b1SBalaji Rao }; 309f52046b1SBalaji Rao 310f52046b1SBalaji Rao static int __init pcf50633_init(void) 311f52046b1SBalaji Rao { 312f52046b1SBalaji Rao return i2c_add_driver(&pcf50633_driver); 313f52046b1SBalaji Rao } 314f52046b1SBalaji Rao 315f52046b1SBalaji Rao static void __exit pcf50633_exit(void) 316f52046b1SBalaji Rao { 317f52046b1SBalaji Rao i2c_del_driver(&pcf50633_driver); 318f52046b1SBalaji Rao } 319f52046b1SBalaji Rao 320f52046b1SBalaji Rao MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU"); 321f52046b1SBalaji Rao MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>"); 322f52046b1SBalaji Rao MODULE_LICENSE("GPL"); 323f52046b1SBalaji Rao 3242021de87SSamuel Ortiz subsys_initcall(pcf50633_init); 325f52046b1SBalaji Rao module_exit(pcf50633_exit); 326