| /* |
| * Base driver for ASR PM803 |
| * |
| * Copyright 2018 ASR Microelectronics (Shanghai) Co., Ltd. |
| * |
| * This file is subject to the terms and conditions of the GNU General |
| * Public License. See the file "COPYING" in the main directory of this |
| * archive for more details. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| */ |
| |
| #include <linux/kernel.h> |
| #include <linux/module.h> |
| #include <linux/i2c.h> |
| #include <linux/mfd/core.h> |
| #include <linux/mfd/88pm80x.h> |
| #include <linux/mfd/pm803.h> |
| #include <linux/slab.h> |
| #include <linux/of_device.h> |
| #include <linux/reboot.h> |
| #include <soc/asr/addr-map.h> |
| #include <linux/cputype.h> |
| |
| /* Interrupt Registers */ |
| #define PM803_INT_STATUS1 (0x05) |
| #define PM803_ONKEY_INT_STS1 (1 << 0) |
| #define PM803_EXTON1_INT_STS1 (1 << 1) |
| #define PM803_RTC_INT_STS1 (1 << 2) |
| #define PM803_BAT_INT_STS1 (1 << 3) |
| |
| #define PM803_INT_ENA_1 (0x09) |
| #define PM803_ONKEY_INT_ENA1 (1 << 0) |
| #define PM803_EXTON_INT_ENA1 (1 << 1) |
| #define PM803_RTC_INT_ENA1 (1 << 2) |
| #define PM803_BAT_INT_ENA1 (1 << 3) |
| |
| /* number of INT_ENA & INT_STATUS regs */ |
| #define PM803_INT_REG_NUM (1) |
| #define PM803_BUCK1_REG_15_8 (0x24) |
| |
| /* Interrupt Number in 88PM803 */ |
| enum { |
| PM803_IRQ_ONKEY, /*EN1b0 *//*0 */ |
| PM803_IRQ_EXTON1, /*EN1b1 */ |
| PM803_IRQ_BAT, /*EN1b3 */ |
| PM803_IRQ_RTC, /*EN1b4 */ |
| PM803_MAX_IRQ, |
| }; |
| |
| #define SCS_SYNC_RTC_CNRL (0x1 << 0 | 0x1 << 4) |
| #define SCS_CLEAR_SYNC_DONE (0x1 << 0 | 0x1 << 6) |
| #define SCR_RTC_SYNC_UDELAY (130) |
| #define SCS_RTC_ACTIVE_LOW (0x1 << 2) |
| #define SCS_RTC_CNTL (0x0c) |
| #define SCS_RTC_SYNC_CFG (0x14) |
| #define SCS_DCS_MODE (0x1c) |
| |
| #define SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x03E000) |
| #define ASR1903_SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x0C0000) |
| |
| /* PM803: generation identification number */ |
| #define PM803_CHIP_GEN_ID_NUM 0x3 |
| extern struct pm80x_chip *pm80x_chip_g; |
| |
| static const struct i2c_device_id pm80x_id_table[] = { |
| {"pm803", 0}, |
| {} /* NULL terminated */ |
| }; |
| MODULE_DEVICE_TABLE(i2c, pm80x_id_table); |
| |
| static const struct of_device_id pm80x_dt_ids[] = { |
| { .compatible = "asr,pm803", }, |
| {}, |
| }; |
| MODULE_DEVICE_TABLE(of, pm80x_dt_ids); |
| |
| static struct resource rtc_resources[] = { |
| { |
| .name = "scs-rtc", |
| .start = PM803_IRQ_RTC, |
| .end = PM803_IRQ_RTC, |
| .flags = IORESOURCE_IRQ, |
| }, |
| }; |
| |
| static struct mfd_cell rtc_devs[] = { |
| { |
| .name = "scs-rtc", |
| .of_compatible = "asr,scs-rtc", |
| .num_resources = ARRAY_SIZE(rtc_resources), |
| .resources = &rtc_resources[0], |
| .id = -1, |
| }, |
| }; |
| |
| static struct resource onkey_resources[] = { |
| { |
| .name = "88pm80x-onkey", |
| .start = PM803_IRQ_ONKEY, |
| .end = PM803_IRQ_ONKEY, |
| .flags = IORESOURCE_IRQ, |
| }, |
| }; |
| |
| static struct mfd_cell onkey_devs[] = { |
| { |
| .name = "88pm80x-onkey", |
| .of_compatible = "marvell,88pm80x-onkey", |
| .num_resources = 1, |
| .resources = &onkey_resources[0], |
| .id = -1, |
| }, |
| }; |
| |
| static struct mfd_cell regulator_devs[] = { |
| { |
| .name = "pm803-regulator", |
| .of_compatible = "asr,pm803-regulator", |
| .id = -1, |
| }, |
| }; |
| |
| static struct resource bat_resources[] = { |
| { |
| .name = "pm803-bat", |
| .start = PM803_IRQ_BAT, |
| .end = PM803_IRQ_BAT, |
| .flags = IORESOURCE_IRQ, |
| }, |
| }; |
| |
| static struct mfd_cell bat_devs[] = { |
| { |
| .name = "pm803-bat", |
| .of_compatible = "asr,pm803-bat", |
| .num_resources = 1, |
| .resources = &bat_resources[0], |
| .id = -1, |
| }, |
| }; |
| |
| #ifdef CONFIG_PAPI_88PM80X |
| static struct mfd_cell papi_devs[] = { |
| { |
| .name = "88pm80x-papi", |
| .of_compatible = "marvell,88pm80x-papi", |
| .id = -1, |
| }, |
| }; |
| #endif |
| |
| static struct resource usb_resources[] = { |
| { |
| .name = "88pm80x-usb", |
| .start = PM803_IRQ_EXTON1, |
| .end = PM803_IRQ_EXTON1, |
| .flags = IORESOURCE_IRQ, |
| }, |
| }; |
| |
| static struct mfd_cell usb_devs[] = { |
| { |
| .name = "88pm80x-usb", |
| .of_compatible = "marvell,88pm80x-usb", |
| .num_resources = ARRAY_SIZE(usb_resources), |
| .resources = &usb_resources[0], |
| .id = -1, |
| }, |
| }; |
| |
| static struct mfd_cell dvc_devs[] = { |
| { |
| .name = "88pm8xx-dvc", |
| .of_compatible = "marvell,88pm8xx-dvc", |
| .id = -1, |
| }, |
| }; |
| |
| static struct mfd_cell debug_devs[] = { |
| { |
| .name = "88pm80x-debug", |
| .of_compatible = "marvell,88pm80x-debug", |
| .id = -1, |
| }, |
| }; |
| |
| static struct mfd_cell wdt_devs[] = { |
| { |
| .name = "88pm80x-wdt", |
| .of_compatible = "marvell,88pm80x-wdt", |
| .id = -1, |
| }, |
| }; |
| |
| static const struct regmap_irq pm803_irqs[] = { |
| /* INT0 */ |
| [PM803_IRQ_ONKEY] = { |
| .mask = PM803_ONKEY_INT_ENA1, |
| }, |
| [PM803_IRQ_EXTON1] = { |
| .mask = PM803_EXTON_INT_ENA1, |
| }, |
| [PM803_IRQ_RTC] = { |
| .mask = PM803_RTC_INT_ENA1, |
| }, |
| [PM803_IRQ_BAT] = { |
| .mask = PM803_BAT_INT_ENA1, |
| }, |
| }; |
| |
| static bool scs_int_active_high; |
| static bool is_scs_mode = false; |
| |
| int pm803_extern_read(int page, int reg) |
| { |
| int ret; |
| unsigned int val; |
| struct pm80x_chip *chip = pm80x_chip_g; |
| if (!chip) { |
| pr_err("%s: chip is NULL\n", __func__); |
| return -EINVAL; |
| } |
| switch (page) { |
| case PM803_BASE_PAGE: |
| ret = regmap_read(chip->regmap, reg, &val); |
| break; |
| case PM803_POWER_PAGE: |
| ret = regmap_read(chip->subchip->regmap_power, |
| reg, &val); |
| break; |
| default: |
| ret = -1; |
| break; |
| } |
| |
| if (ret < 0) { |
| pr_err("fail to read reg 0x%x\n", reg); |
| return ret; |
| } |
| |
| return val; |
| } |
| EXPORT_SYMBOL(pm803_extern_read); |
| |
| int pm803_extern_write(int page, int reg, unsigned char val) |
| { |
| int ret; |
| struct pm80x_chip *chip = pm80x_chip_g; |
| if (!chip) { |
| pr_err("%s: chip is NULL\n", __func__); |
| return -EINVAL; |
| } |
| switch (page) { |
| case PM803_BASE_PAGE: |
| ret = regmap_write(chip->regmap, reg, val); |
| break; |
| case PM803_POWER_PAGE: |
| ret = regmap_write(chip->subchip->regmap_power, |
| reg, val); |
| break; |
| default: |
| ret = -1; |
| break; |
| } |
| return ret; |
| } |
| EXPORT_SYMBOL(pm803_extern_write); |
| |
| int pm803_extern_setbits(int page, int reg, |
| unsigned char mask, unsigned char val) |
| { |
| int ret; |
| struct pm80x_chip *chip = pm80x_chip_g; |
| if (!chip) { |
| pr_err("%s: chip is NULL\n", __func__); |
| return -EINVAL; |
| } |
| switch (page) { |
| case PM803_BASE_PAGE: |
| ret = regmap_update_bits(chip->regmap, reg, mask, val); |
| break; |
| case PM803_POWER_PAGE: |
| ret = regmap_update_bits(chip->subchip->regmap_power, |
| reg, mask, val); |
| break; |
| default: |
| ret = -1; |
| break; |
| } |
| return ret; |
| } |
| EXPORT_SYMBOL(pm803_extern_setbits); |
| |
| static int __pm803_sw_poweroff(void) |
| { |
| int ret = 0; |
| |
| /* clear fault wakeup */ |
| ret = pmic_rw_reg(PM803_RTC_MISC5, (0x1 << 1), 0x0); |
| if (ret < 0) { |
| pr_err("%s, set PM803_RTC_MISC5 fail\n", __func__); |
| return ret; |
| } |
| |
| /* discharge timer should be set greater than or equal to 1s */ |
| ret = pmic_rw_reg(PM803_RTC_MISC2, 0xf, 0x1); |
| if (ret < 0) { |
| pr_err("%s, set PM803_RTC_MISC2 fail\n", __func__); |
| return ret; |
| } |
| ret = pmic_rw_reg(PM803_RTC_MISC4, (0x1 << 1), 0x0); |
| if (ret < 0) { |
| pr_err("%s, set PM803_RTC_MISC4 fail\n", __func__); |
| return ret; |
| } |
| |
| ret = pmic_rw_reg(PM803_WAKEUP1, PM803_SW_PDOWN, PM803_SW_PDOWN); |
| if (ret < 0) |
| pr_err("%s, turn off power fail\n", __func__); |
| |
| return ret; |
| } |
| |
| static void pm803_sw_poweroff(void) |
| { |
| int ret; |
| |
| /* try 3 times to reduce the fail rate */ |
| pr_info("turning off power....\n"); |
| |
| ret = __pm803_sw_poweroff(); |
| if (ret < 0) |
| ret = __pm803_sw_poweroff(); |
| if (ret < 0) |
| ret = __pm803_sw_poweroff(); |
| if (ret < 0) |
| pr_err("%s, turn off power failed\n", __func__); |
| } |
| |
| static int device_rtc_init(struct pm80x_chip *chip, |
| struct pm80x_platform_data *pdata) |
| { |
| int ret; |
| |
| rtc_devs[0].platform_data = pdata->rtc; |
| rtc_devs[0].pdata_size = |
| pdata->rtc ? sizeof(struct pm80x_rtc_pdata) : 0; |
| ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0], |
| ARRAY_SIZE(rtc_devs), NULL, |
| regmap_irq_chip_get_base(chip->irq_data), NULL); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add rtc subdev\n"); |
| return ret; |
| } |
| |
| return 0; |
| } |
| |
| static int device_onkey_init(struct pm80x_chip *chip, |
| struct pm80x_platform_data *pdata) |
| { |
| int ret; |
| |
| ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0], |
| ARRAY_SIZE(onkey_devs), &onkey_resources[0], |
| regmap_irq_chip_get_base(chip->irq_data), NULL); |
| |
| if (ret) { |
| dev_err(chip->dev, "Failed to add onkey subdev\n"); |
| return ret; |
| } |
| |
| return 0; |
| } |
| |
| static int device_regulator_init(struct pm80x_chip *chip, |
| struct pm80x_platform_data *pdata) |
| { |
| int ret; |
| |
| ret = mfd_add_devices(chip->dev, 0, ®ulator_devs[0], |
| ARRAY_SIZE(regulator_devs), NULL, 0, NULL); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add regulator subdev\n"); |
| return ret; |
| } |
| |
| return 0; |
| } |
| |
| static int device_bat_init(struct pm80x_chip *chip, |
| struct pm80x_platform_data *pdata) |
| { |
| int ret; |
| |
| ret = mfd_add_devices(chip->dev, 0, &bat_devs[0], |
| ARRAY_SIZE(bat_devs), NULL, |
| regmap_irq_chip_get_base(chip->irq_data), NULL); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add battery subdev\n"); |
| return ret; |
| } |
| |
| return 0; |
| } |
| |
| #ifdef CONFIG_PAPI_88PM80X |
| static int device_papi_init(struct pm80x_chip *chip) |
| { |
| int ret; |
| |
| ret = mfd_add_devices(chip->dev, 0, &papi_devs[0], |
| ARRAY_SIZE(papi_devs), NULL, 0, NULL); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add papi subdev\n"); |
| return ret; |
| } |
| |
| return 0; |
| } |
| #endif |
| |
| static int device_usb_init(struct pm80x_chip *chip, |
| struct pm80x_platform_data *pdata) |
| { |
| int ret; |
| |
| usb_devs[0].platform_data = pdata->usb; |
| usb_devs[0].pdata_size = sizeof(struct pm80x_usb_pdata); |
| ret = mfd_add_devices(chip->dev, 0, &usb_devs[0], |
| ARRAY_SIZE(usb_devs), NULL, |
| regmap_irq_chip_get_base(chip->irq_data), NULL); |
| if (ret < 0) { |
| dev_err(chip->dev, "Failed to add usb subdev\n"); |
| return ret; |
| } |
| |
| return 0; |
| } |
| |
| static int device_dvc_init(struct pm80x_chip *chip, |
| struct pm80x_platform_data *pdata) |
| { |
| int ret; |
| |
| #ifdef CONFIG_CPU_ASR1901 |
| return 0; |
| #endif |
| |
| ret = mfd_add_devices(chip->dev, 0, &dvc_devs[0], |
| ARRAY_SIZE(dvc_devs), NULL, 0, NULL); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add dvc subdev\n"); |
| return ret; |
| } |
| return 0; |
| } |
| |
| static int device_debug_init(struct pm80x_chip *chip) |
| { |
| return mfd_add_devices(chip->dev, 0, &debug_devs[0], |
| ARRAY_SIZE(debug_devs), NULL, 0, NULL); |
| } |
| |
| static int device_wdt_init(struct pm80x_chip *chip) |
| { |
| return mfd_add_devices(chip->dev, 0, &wdt_devs[0], |
| ARRAY_SIZE(wdt_devs), NULL, 0, NULL); |
| } |
| |
| static int device_irq_init_803(struct pm80x_chip *chip) |
| { |
| struct regmap *map = chip->regmap; |
| unsigned long flags = IRQF_ONESHOT; |
| int data, mask, ret = -EINVAL; |
| |
| if (!map || !chip->irq) { |
| if (!map) |
| pr_info("%s: map == NULL\n", __func__); |
| else |
| pr_info("%s: chip.irq == 0\n", __func__); |
| dev_err(chip->dev, "incorrect parameters\n"); |
| return -EINVAL; |
| } |
| |
| /* |
| * irq_mode defines the way of clearing interrupt. it's read-clear by |
| * default. |
| */ |
| mask = |
| PM803_WAKEUP2_INV_INT | PM803_WAKEUP2_INT_CLEAR | |
| PM803_WAKEUP2_INT_MASK; |
| |
| data = PM803_WAKEUP2_INT_CLEAR; |
| ret = regmap_update_bits(map, PM803_WAKEUP2, mask, data); |
| |
| if (ret < 0) |
| goto out; |
| |
| ret = |
| regmap_add_irq_chip(chip->regmap, chip->irq, flags, -1, |
| chip->regmap_irq_chip, &chip->irq_data); |
| |
| out: |
| return ret; |
| } |
| |
| static void device_irq_exit_803(struct pm80x_chip *chip) |
| { |
| regmap_del_irq_chip(chip->irq, chip->irq_data); |
| } |
| |
| static struct regmap_irq_chip pm803_irq_chip = { |
| .name = "ASRPM803", |
| .irqs = pm803_irqs, |
| .num_irqs = ARRAY_SIZE(pm803_irqs), |
| |
| .num_regs = 1, |
| .status_base = PM803_INT_STATUS1, |
| .mask_base = PM803_INT_ENA_1, |
| .ack_base = PM803_INT_STATUS1, |
| .mask_invert = 1, |
| }; |
| |
| static int pm803_pages_init(struct pm80x_chip *chip) |
| { |
| struct pm80x_subchip *subchip; |
| struct i2c_client *client = chip->client; |
| |
| int ret = 0; |
| |
| subchip = chip->subchip; |
| if (!subchip || !subchip->power_page_addr) |
| return -ENODEV; |
| |
| /* PM803 block power page */ |
| subchip->power_page = i2c_new_dummy(client->adapter, |
| subchip->power_page_addr); |
| if (subchip->power_page == NULL) { |
| ret = -ENODEV; |
| goto out; |
| } |
| |
| subchip->regmap_power = devm_regmap_init_i2c(subchip->power_page, |
| &pm80x_regmap_config); |
| if (IS_ERR(subchip->regmap_power)) { |
| ret = PTR_ERR(subchip->regmap_power); |
| dev_err(chip->dev, |
| "Failed to allocate regmap_power: %d\n", ret); |
| goto out; |
| } |
| |
| i2c_set_clientdata(subchip->power_page, chip); |
| out: |
| return ret; |
| } |
| |
| static void pm803_pages_exit(struct pm80x_chip *chip) |
| { |
| struct pm80x_subchip *subchip; |
| |
| subchip = chip->subchip; |
| |
| if (subchip && subchip->power_page) |
| i2c_unregister_device(subchip->power_page); |
| } |
| |
| static int device_803_init(struct pm80x_chip *chip, |
| struct pm80x_platform_data *pdata) |
| { |
| int ret; |
| void __iomem *scs_base; |
| |
| chip->regmap_irq_chip = &pm803_irq_chip; |
| |
| ret = device_irq_init_803(chip); |
| if (ret < 0) { |
| dev_err(chip->dev, "[%s]Failed to init PM803 irq\n", __func__); |
| goto out; |
| } |
| |
| ret = device_onkey_init(chip, pdata); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add onkey subdev\n"); |
| goto out_dev; |
| } |
| |
| if (cpu_is_asr1803() || cpu_is_asr1806() || cpu_is_asr1903()) { |
| if (cpu_is_asr1903()) |
| scs_base = ASR1903_SCS_RTC_VIRT_BASE; |
| else |
| scs_base = SCS_RTC_VIRT_BASE; |
| |
| is_scs_mode = (!readl(scs_base + SCS_DCS_MODE)); |
| pr_info("is_scs_mode: %d, scs_int_active_high: %d\n", |
| is_scs_mode, scs_int_active_high); |
| |
| if (scs_int_active_high) { |
| writel(readl(scs_base + SCS_RTC_CNTL) & (~SCS_RTC_ACTIVE_LOW), |
| scs_base + SCS_RTC_CNTL); |
| } else |
| writel(readl(scs_base + SCS_RTC_CNTL) | (SCS_RTC_ACTIVE_LOW), |
| scs_base + SCS_RTC_CNTL); |
| writel(SCS_SYNC_RTC_CNRL, scs_base + SCS_RTC_SYNC_CFG); |
| udelay(SCR_RTC_SYNC_UDELAY); |
| writel(SCS_CLEAR_SYNC_DONE, scs_base + SCS_RTC_SYNC_CFG); |
| } |
| |
| ret = device_rtc_init(chip, pdata); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add rtc subdev\n"); |
| goto out; |
| } |
| |
| ret = device_regulator_init(chip, pdata); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add regulators subdev\n"); |
| goto out; |
| } |
| |
| ret = device_bat_init(chip, pdata); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add bat subdev\n"); |
| goto out; |
| } |
| |
| ret = device_dvc_init(chip, pdata); |
| if (ret) |
| dev_warn(chip->dev, "Failed to add dvc subdev\n"); |
| |
| ret = device_usb_init(chip, pdata); |
| if (ret) |
| dev_warn(chip->dev, "Failed to add usb subdev\n"); |
| |
| ret = device_debug_init(chip); |
| if (ret) { |
| dev_err(chip->dev, "failed to add debug subdev\n"); |
| goto out_dev; |
| } |
| |
| ret = device_wdt_init(chip); |
| if (ret) { |
| dev_err(chip->dev, "failed to add wdt subdev\n"); |
| goto out_dev; |
| } |
| |
| #ifdef CONFIG_PAPI_88PM80X |
| ret = device_papi_init(chip); |
| if (ret) { |
| dev_err(chip->dev, "Failed to add papi subdev\n"); |
| goto out; |
| } |
| #endif |
| return 0; |
| out_dev: |
| mfd_remove_devices(chip->dev); |
| device_irq_exit_803(chip); |
| out: |
| return ret; |
| } |
| |
| static void parse_powerup_log(struct pm80x_chip *chip) |
| { |
| int powerup, bit; |
| char *powerup_name[5] = { |
| "ONKEY_WAKEUP ", |
| "EXTON1_WAKEUP ", |
| "RTC_ALARM_WAKEUP", |
| "BAT_WAKEUP ", |
| "FAULT_WAKEUP ", |
| }; |
| |
| /*power up log*/ |
| regmap_read(chip->regmap, PM803_POWER_UP_LOG, &powerup); |
| dev_info(chip->dev, "Powerup log 0x%x: 0x%x\n", |
| PM803_POWER_UP_LOG, powerup); |
| dev_info(chip->dev, " -------------------------------\n"); |
| dev_info(chip->dev, "| name(power up) | status |\n"); |
| dev_info(chip->dev, "|--------------------|----------|\n"); |
| for (bit = 0; bit < 5; bit++) |
| dev_info(chip->dev, "| %s | %x |\n", |
| powerup_name[bit], (powerup >> bit) & 1); |
| dev_info(chip->dev, " -------------------------------\n"); |
| |
| /* keep globals for external usage */ |
| chip->powerup = powerup; |
| } |
| |
| static void parse_powerdown_log(struct pm80x_chip *chip) |
| { |
| int powerdown1, powerdown2, bit; |
| |
| char *powerdown1_name[8] = { |
| "OVER_TEMP ", |
| "UV_VSYS1 ", |
| "SW_PDOWN ", |
| "SYSEN_EVENT ", |
| "WD ", |
| "LONG_ONKEY", |
| "OV_VSYS ", |
| "RTC_RESET " |
| }; |
| char *powerdown2_name[5] = { |
| "HYB_DONE ", |
| "UV_VSYS2 ", |
| "HW_RESET ", |
| "PGOOD_PDOWN", |
| "LONKEY_RTC " |
| }; |
| |
| /*power down log1*/ |
| regmap_read(chip->regmap, PM803_RTC_SPARE7, &powerdown1); |
| regmap_write(chip->regmap, PM803_RTC_SPARE7, 0x0); |
| dev_info(chip->dev, "PowerDW Log1 0x%x: 0x%x\n", |
| PM803_POWER_DOWN_LOG1, powerdown1); |
| dev_info(chip->dev, " -------------------------------\n"); |
| dev_info(chip->dev, "| name(power down1) | status |\n"); |
| dev_info(chip->dev, "|--------------------|----------|\n"); |
| for (bit = 0; bit < 8; bit++) |
| dev_info(chip->dev, "| %s | %x |\n", |
| powerdown1_name[bit], (powerdown1 >> bit) & 1); |
| dev_info(chip->dev, " -------------------------------\n"); |
| |
| /*power down log2*/ |
| regmap_read(chip->regmap, PM803_RTC_SPARE8, &powerdown2); |
| regmap_write(chip->regmap, PM803_RTC_SPARE8, 0x0); |
| dev_info(chip->dev, "PowerDW Log2 0x%x: 0x%x\n", |
| PM803_POWER_DOWN_LOG2, powerdown2); |
| dev_info(chip->dev, " -------------------------------\n"); |
| dev_info(chip->dev, "| name(power down2) | status |\n"); |
| dev_info(chip->dev, "|--------------------|----------|\n"); |
| for (bit = 0; bit < 5; bit++) |
| dev_info(chip->dev, "| %s | %x |\n", |
| powerdown2_name[bit], (powerdown2 >> bit) & 1); |
| dev_info(chip->dev, " -------------------------------\n"); |
| |
| /* write to clear */ |
| regmap_write(chip->regmap, PM803_POWER_DOWN_LOG1, 0xFF); |
| regmap_write(chip->regmap, PM803_POWER_DOWN_LOG2, 0xFF); |
| |
| /* mask reserved bits and sleep indication */ |
| powerdown2 &= 0x1E; |
| |
| /* keep globals for external usage */ |
| chip->powerdown1 = powerdown1; |
| chip->powerdown2 = powerdown2; |
| } |
| |
| static int pm803_init_config(struct pm80x_chip *chip, struct device_node *np) |
| { |
| int data; |
| if (!chip || !chip->regmap || !chip->subchip |
| || !chip->subchip->regmap_power) { |
| pr_err("%s:chip is not availiable!\n", __func__); |
| return -EINVAL; |
| } |
| |
| /* disable alarm pull down and rtc rtp reload */ |
| regmap_read(chip->regmap, PM803_RTC_CONTROL, &data); |
| data |= ((1 << 7) | (0x1 << 6)); |
| regmap_write(chip->regmap, PM803_RTC_CONTROL, data); |
| |
| /* Enable voltage change in pmic, POWER_HOLD = 1 */ |
| regmap_read(chip->regmap, PM803_WAKEUP1, &data); |
| data |= (1 << 7); |
| regmap_write(chip->regmap, PM803_WAKEUP1, data); |
| /* |
| * Enable buck1 sleep mode. |
| */ |
| regmap_update_bits(chip->subchip->regmap_power, PM803_BUCK_SLP1, |
| (0x3 << 3), (0x2 << 3)); |
| |
| /* dump power up log */ |
| parse_powerup_log(chip); |
| parse_powerdown_log(chip); |
| return 0; |
| } |
| |
| static int pm803_dt_init(struct device_node *np, |
| struct device *dev, |
| struct pm80x_platform_data *pdata) |
| { |
| pdata->irq_mode = |
| !of_property_read_bool(np, "asr,pm803-irq-write-clear"); |
| pdata->batt_det = |
| of_property_read_bool(np, "asr,pm803-battery-detection"); |
| |
| scs_int_active_high = |
| of_property_read_bool(np, "scs-int-active-high"); |
| return 0; |
| } |
| |
| static int pm803_probe(struct i2c_client *client, |
| const struct i2c_device_id *id) |
| { |
| int ret = 0; |
| struct pm80x_chip *chip; |
| struct pm80x_platform_data *pdata = client->dev.platform_data; |
| struct device_node *node = client->dev.of_node; |
| struct pm80x_subchip *subchip; |
| |
| if (IS_ENABLED(CONFIG_OF)) { |
| if (!pdata) { |
| pdata = devm_kzalloc(&client->dev, |
| sizeof(*pdata), GFP_KERNEL); |
| if (!pdata) |
| return -ENOMEM; |
| } |
| ret = pm803_dt_init(node, &client->dev, pdata); |
| if (ret) |
| return ret; |
| } else if (!pdata) { |
| return -EINVAL; |
| } |
| |
| /* |
| * RTC in pmic can run even the core is powered off, and user can set |
| * alarm in RTC. When the alarm is time out, the PMIC will power up |
| * the core, and the whole system will boot up. When PMIC driver is |
| * probed, it will read out some register to find out whether this |
| * boot is caused by RTC timeout or not, and it need pass this |
| * information to RTC driver. |
| * So we need rtc platform data to be existed to pass this information. |
| */ |
| if (!pdata->rtc) { |
| pdata->rtc = devm_kzalloc(&client->dev, |
| sizeof(*(pdata->rtc)), GFP_KERNEL); |
| if (!pdata->rtc) |
| return -ENOMEM; |
| } |
| |
| ret = pm80x_init(client); |
| if (ret) { |
| dev_err(&client->dev, "PM803_init fail\n"); |
| goto out_init; |
| } |
| |
| chip = i2c_get_clientdata(client); |
| |
| if (chip->type != CHIP_PM803) { |
| dev_err(&client->dev, "PM803 not present\n"); |
| ret = -ENODEV; |
| goto out_init; |
| } |
| |
| /* init subchip for PM803 */ |
| subchip = |
| devm_kzalloc(&client->dev, sizeof(struct pm80x_subchip), |
| GFP_KERNEL); |
| if (!subchip) { |
| ret = -ENOMEM; |
| goto err_subchip_alloc; |
| } |
| |
| /* PM803 has 2 addtional pages to support power. */ |
| subchip->power_page_addr = client->addr + 1; |
| chip->subchip = subchip; |
| |
| ret = pm803_pages_init(chip); |
| if (ret) { |
| dev_err(&client->dev, "PM803_pages_init failed!\n"); |
| goto err_page_init; |
| } |
| |
| ret = device_803_init(chip, pdata); |
| if (ret) { |
| dev_err(chip->dev, "Failed to initialize 88PM803 devices\n"); |
| goto err_device_init; |
| } |
| |
| if (pdata && pdata->plat_config) |
| pdata->plat_config(chip, pdata); |
| |
| pm803_init_config(chip, NULL); |
| |
| if (!pm80x_chip_g) |
| pm80x_chip_g = chip; |
| pm_power_off = pm803_sw_poweroff; |
| |
| return 0; |
| |
| err_device_init: |
| pm803_pages_exit(chip); |
| err_page_init: |
| err_subchip_alloc: |
| pm80x_deinit(); |
| out_init: |
| return ret; |
| } |
| |
| static int pm803_remove(struct i2c_client *client) |
| { |
| struct pm80x_chip *chip = i2c_get_clientdata(client); |
| |
| mfd_remove_devices(chip->dev); |
| device_irq_exit_803(chip); |
| |
| pm803_pages_exit(chip); |
| pm80x_deinit(); |
| pm80x_chip_g = NULL; |
| |
| return 0; |
| } |
| |
| void pm803_shutdown(struct i2c_client *client) |
| { |
| u32 data; |
| struct pm80x_chip *chip = i2c_get_clientdata(client); |
| |
| /* save reboot flag */ |
| regmap_read(chip->regmap, PM803_DUMMY_REG0, &data); |
| data &= 0x0f; |
| data |= 0x50; |
| regmap_write(chip->regmap, PM803_DUMMY_REG0, data); |
| pr_info("%s\n", __func__); |
| } |
| |
| static struct i2c_driver pm803_driver = { |
| .driver = { |
| .name = "ASRPM803", |
| .owner = THIS_MODULE, |
| .pm = &pm80x_pm_ops, |
| .of_match_table = of_match_ptr(pm80x_dt_ids), |
| }, |
| .probe = pm803_probe, |
| .remove = pm803_remove, |
| .shutdown = pm803_shutdown, |
| .id_table = pm80x_id_table, |
| }; |
| |
| static int __init pm803_i2c_init(void) |
| { |
| return i2c_add_driver(&pm803_driver); |
| } |
| subsys_initcall(pm803_i2c_init); |
| |
| static void __exit pm803_i2c_exit(void) |
| { |
| i2c_del_driver(&pm803_driver); |
| } |
| module_exit(pm803_i2c_exit); |
| |
| MODULE_DESCRIPTION("PMIC Driver for ASR PM803"); |
| MODULE_LICENSE("GPL"); |