blob: 245d27d695c29b588b6b2c6fcb830d86af18cd6b [file] [log] [blame]
/*
* Base driver for ASR PM813
*
* Copyright 2021 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/pm813.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 PM813_INT_STATUS1 (0x05)
#define PM813_ONKEY_INT_STS1 (1 << 0)
#define PM813_EXTON1_INT_STS1 (1 << 1)
#define PM813_CHG_INT_STS1 (1 << 2)
#define PM813_BAT_INT_STS1 (1 << 3)
#define PM813_INT_STATUS2 (0x06)
#define PM813_TINT_INT_STS2 (1 << 0)
#define PM813_GP0_INT_STS2 (1 << 1)
#define PM813_GP1_INT_STS2 (1 << 2)
#define PM813_VINLDO_INT_STS2 (1 << 3)
#define PM813_VINLDO_SLP_INT_STS2 (1 << 4)
#define PM813_RTC_INT_STS2 (1 << 7)
#define PM813_INT_ENA_1 (0x09)
#define PM813_ONKEY_INT_ENA1 (1 << 0)
#define PM813_EXTON_INT_ENA1 (1 << 1)
#define PM813_VCHG_INT_ENA1 (1 << 2)
#define PM813_BAT_INT_ENA1 (1 << 3)
#define PM813_INT_ENA_2 (0x0A)
#define PM813_TINT_INT_EN2 (1 << 0)
#define PM813_GP0_INT_EN2 (1 << 1)
#define PM813_GP1_INT_EN2 (1 << 2)
#define PM813_VINLDO_INT_EN2 (1 << 3)
#define PM813_VINLDO_SLP_INT_EN2 (1 << 4)
#define PM813_RTC_INT_ENA1 (1 << 7)
/* number of INT_ENA & INT_STATUS regs */
#define PM813_INT_REG_NUM (2)
#define PM813_BUCK1_REG_15_8 (0x24)
/* Interrupt Number in 88PM813 */
enum {
PM813_IRQ_ONKEY, /*EN1b0 *//*0 */
PM813_IRQ_EXTON1, /*EN1b1 */
PM813_IRQ_EXTON2, /*EN1b2 */
PM813_IRQ_BAT, /*EN1b3 */
PM813_IRQ_CHG, /*EN1b4 */
PM813_IRQ_VBUS_OVUV, /*EN1b5 */
PM813_IRQ_VBUS_DET, /*EN1b6 */
PM813_IRQ_CP_ST_DONE_DET, /*EN1b7 */
PM813_IRQ_TINT, /*EN2b0 */
PM813_IRQ_GPADC0, /*EN2b1 */
PM813_IRQ_GPADC1, /*EN2b2 */
PM813_IRQ_VINLDO, /*EN2b3 */
PM813_IRQ_BAT_TEMP, /*EN2b4 */
PM813_IRQ_CP_ST_ERR, /*EN2b5 */
PM813_IRQ_CLASSD_OCP, /*EN2b6 */
PM813_IRQ_RTC, /*EN2b7 */
PM813_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)
/* PM813: generation identification number */
#define PM813_CHIP_GEN_ID_NUM 0x3
extern struct pm80x_chip *pm80x_chip_g;
static const struct i2c_device_id pm80x_id_table[] = {
{"pm813", 0},
{} /* NULL terminated */
};
MODULE_DEVICE_TABLE(i2c, pm80x_id_table);
static const struct of_device_id pm80x_dt_ids[] = {
{ .compatible = "asr,pm813", },
{},
};
MODULE_DEVICE_TABLE(of, pm80x_dt_ids);
static struct resource rtc_resources[] = {
{
.name = "88pm80x-rtc",
.start = PM813_IRQ_RTC,
.end = PM813_IRQ_RTC,
.flags = IORESOURCE_IRQ,
},
};
static struct mfd_cell rtc_devs[] = {
{
.name = "88pm80x-rtc",
.of_compatible = "marvell,88pm80x-rtc",
.num_resources = ARRAY_SIZE(rtc_resources),
.resources = &rtc_resources[0],
.id = -1,
},
};
static struct resource scs_rtc_resources[] = {
{
.name = "scs-rtc",
.start = PM813_IRQ_EXTON1,
.end = PM813_IRQ_EXTON1,
.flags = IORESOURCE_IRQ,
},
};
static struct mfd_cell scs_rtc_devs[] = {
{
.name = "scs-rtc",
.of_compatible = "asr,scs-rtc",
.num_resources = ARRAY_SIZE(scs_rtc_resources),
.resources = &scs_rtc_resources[0],
.id = -1,
},
};
static struct resource onkey_resources[] = {
{
.name = "88pm80x-onkey",
.start = PM813_IRQ_ONKEY,
.end = PM813_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 = "pm813-regulator",
.of_compatible = "asr,pm813-regulator",
.id = -1,
},
};
static struct resource bat_resources[] = {
{
.name = "pm813-bat",
.start = PM813_IRQ_VINLDO,
.end = PM813_IRQ_VINLDO,
.flags = IORESOURCE_IRQ,
},
};
static struct mfd_cell bat_devs[] = {
{
.name = "pm813-bat",
.of_compatible = "asr,pm813-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 = PM813_IRQ_EXTON1,
.end = PM813_IRQ_EXTON1,
.flags = IORESOURCE_IRQ,
},
{
.name = "88pm80x-gp0-id",
.start = PM813_IRQ_GPADC0,
.end = PM813_IRQ_GPADC0,
.flags = IORESOURCE_IRQ,
},
{
.name = "88pm80x-gp1-id",
.start = PM813_IRQ_GPADC1,
.end = PM813_IRQ_GPADC1,
.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 resource scs_usb_resources[] = {
{
.name = "88pm80x-usb",
.start = PM813_IRQ_EXTON2,
.end = PM813_IRQ_EXTON2,
.flags = IORESOURCE_IRQ,
},
{
.name = "88pm80x-gp0-id",
.start = PM813_IRQ_GPADC0,
.end = PM813_IRQ_GPADC0,
.flags = IORESOURCE_IRQ,
},
{
.name = "88pm80x-gp1-id",
.start = PM813_IRQ_GPADC1,
.end = PM813_IRQ_GPADC1,
.flags = IORESOURCE_IRQ,
},
};
static struct mfd_cell scs_usb_devs[] = {
{
.name = "88pm80x-usb",
.of_compatible = "marvell,88pm80x-usb",
.num_resources = ARRAY_SIZE(scs_usb_resources),
.resources = &scs_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 pm813_irqs[] = {
/* INT0 */
[PM813_IRQ_ONKEY] = {
.mask = PM813_ONKEY_INT_ENA1,
},
[PM813_IRQ_EXTON1] = {
.mask = PM813_EXTON_INT_ENA1,
},
[PM813_IRQ_EXTON2] = {
.mask = PM813_VCHG_INT_ENA1,
},
[PM813_IRQ_BAT] = {
.mask = PM813_BAT_INT_ENA1,
},
[PM813_IRQ_TINT] = {
.reg_offset = 1,
.mask = PM813_TINT_INT_EN2,
},
[PM813_IRQ_GPADC0] = {
.reg_offset = 1,
.mask = PM813_GP0_INT_EN2,
},
[PM813_IRQ_GPADC1] = {
.reg_offset = 1,
.mask = PM813_GP1_INT_EN2,
},
[PM813_IRQ_VINLDO] = {
.reg_offset = 1,
.mask = PM813_VINLDO_INT_EN2,
},
[PM813_IRQ_RTC] = {
.reg_offset = 1,
.mask = PM813_RTC_INT_ENA1,
},
};
static bool scs_int_active_high;
static bool is_scs_mode = false;
static bool pm813_is_probed = false;
bool pm813_driver_is_probed(void)
{
return pm813_is_probed;
}
int pm813_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 PM813_BASE_PAGE:
ret = regmap_read(chip->regmap, reg, &val);
break;
case PM813_POWER_PAGE:
ret = regmap_read(chip->subchip->regmap_power,
reg, &val);
break;
case PM813_GPADC_PAGE:
ret = regmap_read(chip->subchip->regmap_gpadc,
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(pm813_extern_read);
int pm813_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 PM813_BASE_PAGE:
ret = regmap_write(chip->regmap, reg, val);
break;
case PM813_POWER_PAGE:
ret = regmap_write(chip->subchip->regmap_power,
reg, val);
break;
case PM813_GPADC_PAGE:
ret = regmap_write(chip->subchip->regmap_gpadc,
reg, val);
break;
default:
ret = -1;
break;
}
return ret;
}
EXPORT_SYMBOL(pm813_extern_write);
int pm813_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 PM813_BASE_PAGE:
ret = regmap_update_bits(chip->regmap, reg, mask, val);
break;
case PM813_POWER_PAGE:
ret = regmap_update_bits(chip->subchip->regmap_power,
reg, mask, val);
break;
case PM813_GPADC_PAGE:
ret = regmap_update_bits(chip->subchip->regmap_gpadc,
reg, mask, val);
break;
default:
ret = -1;
break;
}
return ret;
}
EXPORT_SYMBOL(pm813_extern_setbits);
static int __pm813_sw_poweroff(void)
{
u32 val;
int ret = 0;
struct pm80x_chip *chip = pm80x_chip_g;
if (!chip) {
WARN(1, "__pm813_sw_poweroff: chip is NULL\n");
return -EINVAL;
}
if (CHIP_PM813S_A1_ID == chip->type
|| CHIP_PM813S_A0_ID == chip->type)
val = 0x3;
else
val = 0x1;
/* discharge timer should be set greater than or equal to 1s */
ret = pmic_rw_reg(PM813_RTC_MISC2, 0xf, val);
if (ret < 0) {
pr_err("%s, set PM813_RTC_MISC2 fail\n", __func__);
return ret;
}
ret = pmic_rw_reg(PM813_RTC_MISC4, (0x1 << 1), 0x0);
if (ret < 0) {
pr_err("%s, set PM813_RTC_MISC4 fail\n", __func__);
return ret;
}
ret = pmic_rw_reg(PM813_WAKEUP1, PM813_SW_PDOWN, PM813_SW_PDOWN);
if (ret < 0)
pr_err("%s, turn off power fail\n", __func__);
return ret;
}
static void pm813_sw_poweroff(void)
{
int ret;
/* try 3 times to reduce the fail rate */
pr_info("turning off power....\n");
ret = __pm813_sw_poweroff();
if (ret < 0)
ret = __pm813_sw_poweroff();
if (ret < 0)
ret = __pm813_sw_poweroff();
if (ret < 0)
pr_err("%s, turn off power failed\n", __func__);
}
static int device_gpadc_init(struct pm80x_chip *chip,
struct pm80x_platform_data *pdata)
{
struct pm80x_subchip *subchip = chip->subchip;
struct regmap *map = subchip->regmap_gpadc;
int ret = 0;
u32 val;
if (!map) {
dev_warn(chip->dev,
"Warning: gpadc regmap is not available!\n");
return -EINVAL;
}
/*
* initialize GPADC without activating it turn on GPADC
* measurments
*/
ret = regmap_update_bits(map,
PM813_GPADC_MISC_CONFIG2,
PM813_GPADC_MISC_GPFSM_EN,
0x0);
if (ret < 0)
goto out;
/*
* the defult of PM813 is GPADC operates at 100Ks/s rate
* and Number of GPADC slots with active current bias prior
* to GPADC sampling = 1 slot for all GPADCs set for
* Temprature mesurmants
*
* disable all the BIAS by default, other drivers will enable
* BIAS on demand
*/
ret = regmap_update_bits(map, PM813_GP_BIAS_ENA1, 0x3, 0x0);
if (ret < 0)
goto out;
ret = regmap_update_bits(map, PM813_GPADC_MISC_CONFIG3, 0x3, 0x1);
if (ret < 0)
goto out;
ret = regmap_write(map, PM813_GPADC_CFG1, 0x0);
if (ret < 0)
goto out;
/* single conversion mode */
ret = regmap_update_bits(map, PM813_GPADC_CFG2, 0x19, 0x18);
if (ret < 0)
goto out;
ret = regmap_update_bits(map, PM813_GPADC_CFG3, (0x3 << 2), (0x3 << 2));
if (ret < 0)
goto out;
ret = regmap_read(map, PM8XX_REG_VREF_OFFSET, &val);
if (ret < 0) {
chip->adc_vref_offset = 0;
} else {
if (val & (0x1 << 7))
chip->adc_vref_offset = 0xffffff00 | (val & 0xff);
else
chip->adc_vref_offset = (val & 0xff);
chip->adc_vref_offset *= 5;
}
ret = regmap_read(map, PM8XX_REG_SAMPLE_OFFSET, &val);
if (ret < 0) {
chip->adc_sample_offset = 0;
} else {
if (val & (0x1 << 7))
chip->adc_sample_offset = 0xffffff00 | (val & 0xff);
else
chip->adc_sample_offset = (val & 0xff);
}
dev_info(chip->dev, "vref/sample offset: %d/%d\n", chip->adc_vref_offset, chip->adc_sample_offset);
dev_info(chip->dev, "PM813 device_gpadc_init: Done\n");
return 0;
out:
dev_info(chip->dev, "PM813 device_gpadc_init: Failed!\n");
return ret;
}
static int device_onkey_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, &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_rtc_init(struct pm80x_chip *chip,
struct pm80x_platform_data *pdata)
{
int ret;
#ifdef CONFIG_CPU_ASR1901
return 0;
#endif
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_scs_rtc_init(struct pm80x_chip *chip,
struct pm80x_platform_data *pdata)
{
int ret;
scs_rtc_devs[0].platform_data = pdata->rtc;
scs_rtc_devs[0].pdata_size =
pdata->rtc ? sizeof(struct pm80x_rtc_pdata) : 0;
ret = mfd_add_devices(chip->dev, 0, &scs_rtc_devs[0],
ARRAY_SIZE(scs_rtc_devs), NULL,
regmap_irq_chip_get_base(chip->irq_data), NULL);
if (ret) {
dev_err(chip->dev, "Failed to add scs rtc 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, &regulator_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_scs_usb_init(struct pm80x_chip *chip,
struct pm80x_platform_data *pdata)
{
int ret;
scs_usb_devs[0].platform_data = pdata->usb;
scs_usb_devs[0].pdata_size = sizeof(struct pm80x_usb_pdata);
ret = mfd_add_devices(chip->dev, 0, &scs_usb_devs[0],
ARRAY_SIZE(scs_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_813(struct pm80x_chip *chip)
{
struct regmap *map = chip->regmap;
#ifdef CONFIG_CPU_ASR1901
unsigned long flags = IRQF_ONESHOT | IRQF_SHARED;
#else
unsigned long flags = IRQF_ONESHOT;
#endif
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 =
PM813_WAKEUP2_INV_INT | PM813_WAKEUP2_INT_CLEAR |
PM813_WAKEUP2_INT_MASK;
data = PM813_WAKEUP2_INT_CLEAR;
ret = regmap_update_bits(map, PM813_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_813(struct pm80x_chip *chip)
{
regmap_del_irq_chip(chip->irq, chip->irq_data);
}
static struct regmap_irq_chip pm813_irq_chip = {
.name = "ASRPM813",
.irqs = pm813_irqs,
.num_irqs = ARRAY_SIZE(pm813_irqs),
.num_regs = 2,
.status_base = PM813_INT_STATUS1,
.mask_base = PM813_INT_ENA_1,
.ack_base = PM813_INT_STATUS1,
.mask_invert = 1,
};
static int pm813_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 || !subchip->gpadc_page_addr)
return -ENODEV;
/* PM813 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);
/* PM813 block GPADC */
subchip->gpadc_page = i2c_new_dummy(client->adapter,
subchip->gpadc_page_addr);
if (subchip->gpadc_page == NULL) {
ret = -ENODEV;
goto out;
}
subchip->regmap_gpadc = devm_regmap_init_i2c(subchip->gpadc_page,
&pm80x_regmap_config);
if (IS_ERR(subchip->regmap_gpadc)) {
ret = PTR_ERR(subchip->regmap_gpadc);
dev_err(chip->dev,
"Failed to allocate regmap_gpadc: %d\n", ret);
goto out;
}
i2c_set_clientdata(subchip->gpadc_page, chip);
out:
return ret;
}
static void pm813_pages_exit(struct pm80x_chip *chip)
{
struct pm80x_subchip *subchip;
subchip = chip->subchip;
if (subchip && subchip->power_page)
i2c_unregister_device(subchip->power_page);
if (subchip && subchip->gpadc_page)
i2c_unregister_device(subchip->gpadc_page);
}
static int device_813_init(struct pm80x_chip *chip,
struct pm80x_platform_data *pdata)
{
int ret;
unsigned int val;
void __iomem *scs_base;
/*
* alarm wake up bit will be clear in device_irq_init(),
* read before that
*/
ret = regmap_read(chip->regmap, PM813_RTC_CONTROL, &val);
if (ret < 0) {
dev_err(chip->dev, "Failed to read RTC register: %d\n", ret);
goto out;
}
if (val & PM813_ALARM_WAKEUP) {
if (pdata && pdata->rtc)
pdata->rtc->rtc_wakeup = 1;
}
ret = device_gpadc_init(chip, pdata);
if (ret < 0) {
dev_err(chip->dev, "[%s]Failed to init gpadc\n", __func__);
goto out;
}
chip->regmap_irq_chip = &pm813_irq_chip;
ret = device_irq_init_813(chip);
if (ret < 0) {
dev_err(chip->dev, "[%s]Failed to init PM813 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));
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);
}
if (is_scs_mode)
ret = device_scs_rtc_init(chip, pdata);
else
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");
if (is_scs_mode)
ret = device_scs_usb_init(chip, pdata);
else
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_813(chip);
out:
return ret;
}
static void parse_powerup_log(struct pm80x_chip *chip)
{
int powerup, bit;
char *powerup_name[7] = {
"ONKEY_WAKEUP ",
"EXTON1_WAKEUP ",
"EXTON2_WAKEUP ",
"BAT_WAKEUP ",
"RTC_ALARM_WAKEUP",
"FAULT_WAKEUP ",
"IVBUS_DET_WAKEUP"
};
/*power up log*/
regmap_read(chip->regmap, PM813_POWER_UP_LOG, &powerup);
dev_info(chip->dev, "Powerup log 0x%x: 0x%x\n",
PM813_POWER_UP_LOG, powerup);
printk(KERN_DEBUG " -------------------------------\n");
printk(KERN_DEBUG "| name(power up) | status |\n");
printk(KERN_DEBUG "|--------------------|----------|\n");
for (bit = 0; bit < 7; bit++)
printk(KERN_DEBUG "| %s | %x |\n",
powerup_name[bit], (powerup >> bit) & 1);
printk(KERN_DEBUG " -------------------------------\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*/
if (CHIP_PM813S_A1_ID == chip->chip_id
|| CHIP_PM813S_A0_ID == chip->chip_id) {
regmap_read(chip->regmap, PM813S_RTC_SPAREE, &powerdown1);
regmap_write(chip->regmap, PM813S_RTC_SPAREE, 0x0);
} else {
regmap_read(chip->regmap, PM813_POWER_DOWN_LOG1, &powerdown1);
}
dev_info(chip->dev, "PowerDW Log1 0x%x: 0x%x\n",
PM813_POWER_DOWN_LOG1, powerdown1);
printk(KERN_DEBUG " -------------------------------\n");
printk(KERN_DEBUG "| name(power down1) | status |\n");
printk(KERN_DEBUG "|--------------------|----------|\n");
for (bit = 0; bit < 8; bit++)
printk(KERN_DEBUG "| %s | %x |\n",
powerdown1_name[bit], (powerdown1 >> bit) & 1);
printk(KERN_DEBUG " -------------------------------\n");
/*power down log2*/
if (CHIP_PM813S_A1_ID == chip->chip_id
|| CHIP_PM813S_A0_ID == chip->chip_id) {
regmap_read(chip->regmap, PM813S_RTC_SPAREF, &powerdown1);
regmap_write(chip->regmap, PM813S_RTC_SPAREF, 0x0);
} else {
regmap_read(chip->regmap, PM813_POWER_DOWN_LOG2, &powerdown2);
}
dev_info(chip->dev, "PowerDW Log2 0x%x: 0x%x\n",
PM813_POWER_DOWN_LOG2, powerdown2);
printk(KERN_DEBUG " -------------------------------\n");
printk(KERN_DEBUG "| name(power down2) | status |\n");
printk(KERN_DEBUG "|--------------------|----------|\n");
for (bit = 0; bit < 5; bit++)
printk(KERN_DEBUG "| %s | %x |\n",
powerdown2_name[bit], (powerdown2 >> bit) & 1);
printk(KERN_DEBUG " -------------------------------\n");
/* write to clear */
regmap_write(chip->regmap, PM813_POWER_DOWN_LOG1, 0xFF);
regmap_write(chip->regmap, PM813_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 pm813_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;
}
/*base page:reg 0xd0.7 = 1 32kHZ generated from XO */
regmap_read(chip->regmap, PM813_RTC_CONTROL, &data);
if (is_scs_mode) {
pr_info("is_scs_mode: true\n");
data &= ~(1 << 7);
} else {
pr_info("is_scs_mode: false\n");
data |= (1 << 7);
}
regmap_write(chip->regmap, PM813_RTC_CONTROL, data);
regmap_read(chip->regmap, PM813_RTC_MISC4, &data);
data &= ~(0x3 << 2);
if (!is_scs_mode)
data |= (0x1 << 2);
regmap_write(chip->regmap, PM813_RTC_MISC4, data);
/* Enable voltage change in pmic, POWER_HOLD = 1 */
regmap_read(chip->regmap, PM813_WAKEUP1, &data);
data |= (1 << 7);
regmap_write(chip->regmap, PM813_WAKEUP1, data);
/*
* Enable buck sleep mode.
* But disable buck2 sleep mode here since it's for ddr/emmc, which
* is sensitive to the voltage and the chip may have latency before
* enter power save mode.
*/
regmap_update_bits(chip->subchip->regmap_power, PM813_BUCK_SLP1,
(0x3 << 3), (0x2 << 3));
regmap_update_bits(chip->subchip->regmap_power, PM813_BUCK_SLP3,
(0x3 << 3), (0x2 << 3));
/* set XO cap for B0+ chips */
regmap_read(chip->regmap, PM813_RTC_MISC13, &data);
data &= ~(0x7 << 5);
data |= (0x3 << 5);
regmap_write(chip->regmap, PM813_RTC_MISC13, data);
/* enable use_xo for none-A0 chips */
regmap_read(chip->regmap, PM813_RTC_MISC14, &data);
if (is_scs_mode)
data &= ~(0x1 << 2);
else
data |= (0x1 << 2);
regmap_write(chip->regmap, PM813_RTC_MISC14, data);
/* clear DVCFPWM for all chip revisions */
regmap_read(chip->subchip->regmap_power, PM813_BUCK1_REG_15_8, &data);
data &= ~(0x1 << 7);
/* set RC1 for B0 */
data |= (0x1 << 4);
regmap_write(chip->subchip->regmap_power, PM813_BUCK1_REG_15_8, data);
/* dump power up log */
parse_powerup_log(chip);
parse_powerdown_log(chip);
return 0;
}
static int pm813_dt_init(struct device_node *np,
struct device *dev,
struct pm80x_platform_data *pdata)
{
pdata->irq_mode =
!of_property_read_bool(np, "asr,pm813-irq-write-clear");
pdata->batt_det =
of_property_read_bool(np, "asr,pm813-battery-detection");
scs_int_active_high =
of_property_read_bool(np, "scs-int-active-high");
return 0;
}
static int pm813_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 = pm813_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, "PM813_init fail\n");
goto out_init;
}
chip = i2c_get_clientdata(client);
if (chip->type != CHIP_PM813) {
dev_err(&client->dev, "PM813 not present\n");
ret = -ENODEV;
goto out_init;
}
/* init subchip for PM813 */
subchip =
devm_kzalloc(&client->dev, sizeof(struct pm80x_subchip),
GFP_KERNEL);
if (!subchip) {
ret = -ENOMEM;
goto err_subchip_alloc;
}
/* PM813 has 2 addtional pages to support power and gpadc. */
subchip->power_page_addr = client->addr + 1;
subchip->gpadc_page_addr = client->addr + 2;
chip->subchip = subchip;
ret = pm813_pages_init(chip);
if (ret) {
dev_err(&client->dev, "PM813_pages_init failed!\n");
goto err_page_init;
}
ret = device_813_init(chip, pdata);
if (ret) {
dev_err(chip->dev, "Failed to initialize 88PM813 devices\n");
goto err_device_init;
}
if (pdata && pdata->plat_config)
pdata->plat_config(chip, pdata);
pm813_init_config(chip, NULL);
if (!pm80x_chip_g)
pm80x_chip_g = chip;
pm_power_off = pm813_sw_poweroff;
pm813_is_probed = true;
return 0;
err_device_init:
pm813_pages_exit(chip);
err_page_init:
err_subchip_alloc:
pm80x_deinit();
out_init:
return ret;
}
static int pm813_remove(struct i2c_client *client)
{
struct pm80x_chip *chip = i2c_get_clientdata(client);
mfd_remove_devices(chip->dev);
device_irq_exit_813(chip);
pm813_pages_exit(chip);
pm80x_deinit();
pm80x_chip_g = NULL;
return 0;
}
static struct i2c_driver pm813_driver = {
.driver = {
.name = "ASRPM813",
.owner = THIS_MODULE,
.pm = &pm80x_pm_ops,
.of_match_table = of_match_ptr(pm80x_dt_ids),
},
.probe = pm813_probe,
.remove = pm813_remove,
.id_table = pm80x_id_table,
};
static int __init pm813_i2c_init(void)
{
return i2c_add_driver(&pm813_driver);
}
subsys_initcall(pm813_i2c_init);
static void __exit pm813_i2c_exit(void)
{
i2c_del_driver(&pm813_driver);
}
module_exit(pm813_i2c_exit);
MODULE_DESCRIPTION("PMIC Driver for ASR PM813");
MODULE_LICENSE("GPL");