SmartAudio/lichee/linux-4.9/drivers/power/supply/axp/axp20x/axp20x.c

521 lines
13 KiB
C
Executable File

/*
* drivers/power/supply/axp/axp20x/axp20x.c
* (C) Copyright 2010-2016
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
* caiyongheng <caiyongheng@allwinnertech.com>
*
* driver of axp20x
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
*/
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/reboot.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/mfd/core.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_device.h>
#include <linux/err.h>
#include <linux/power/aw_pm.h>
#include "../axp-core.h"
#include "../axp-charger.h"
#include "../axp-regulator.h"
#include "axp20x.h"
#include "axp20x-regulator.h"
static struct axp_dev *axp20x_pm_power;
struct axp_config_info axp20x_config;
struct wakeup_source *axp20x_ws;
static int axp20x_pmu_num;
static const struct axp_compatible_name_mapping axp20x_cn_mapping[] = {
{
.device_name = "axp203",
.mfd_name = {
.powerkey_name = "axp203-powerkey",
.charger_name = "axp203-charger",
.regulator_name = "axp203-regulator",
.gpio_name = "axp203-gpio",
},
},
{
.device_name = "axp209",
.mfd_name = {
.powerkey_name = "axp209-powerkey",
.charger_name = "axp209-charger",
.regulator_name = "axp209-regulator",
.gpio_name = "axp209-gpio",
},
},
};
static struct axp_regmap_irq_chip axp20x_regmap_irq_chip = {
.name = "axp20x_irq_chip",
.status_base = AXP20X_INTSTS1,
.enable_base = AXP20X_INTEN1,
.num_regs = 5,
};
static struct resource axp20x_pek_resources[] = {
{AXP20X_IRQ_PEKRE, AXP20X_IRQ_PEKRE, "PEK_DBR", IORESOURCE_IRQ,},
{AXP20X_IRQ_PEKFE, AXP20X_IRQ_PEKFE, "PEK_DBF", IORESOURCE_IRQ,},
};
static struct resource axp20x_charger_resources[] = {
{AXP20X_IRQ_USBIN, AXP20X_IRQ_USBIN, "usb in", IORESOURCE_IRQ,},
{AXP20X_IRQ_USBRE, AXP20X_IRQ_USBRE, "usb out", IORESOURCE_IRQ,},
{AXP20X_IRQ_ACIN, AXP20X_IRQ_ACIN, "ac in", IORESOURCE_IRQ,},
{AXP20X_IRQ_ACRE, AXP20X_IRQ_ACRE, "ac out", IORESOURCE_IRQ,},
{AXP20X_IRQ_BATIN, AXP20X_IRQ_BATIN, "bat in", IORESOURCE_IRQ,},
{AXP20X_IRQ_BATRE, AXP20X_IRQ_BATRE, "bat out", IORESOURCE_IRQ,},
{AXP20X_IRQ_TEMLO, AXP20X_IRQ_TEMLO, "bat temp low", IORESOURCE_IRQ,},
{AXP20X_IRQ_TEMOV, AXP20X_IRQ_TEMOV, "bat temp over", IORESOURCE_IRQ,},
{AXP20X_IRQ_CHAST, AXP20X_IRQ_CHAST, "charging", IORESOURCE_IRQ,},
{AXP20X_IRQ_CHAOV, AXP20X_IRQ_CHAOV, "charge over", IORESOURCE_IRQ,},
};
static struct resource axp20x_gpio_resources[] = {
{AXP20X_IRQ_GPIO0, AXP20X_IRQ_GPIO0, "gpio0", IORESOURCE_IRQ,},
{AXP20X_IRQ_GPIO1, AXP20X_IRQ_GPIO1, "gpio1", IORESOURCE_IRQ,},
};
static struct mfd_cell axp20x_cells[] = {
{
.name = "axp20x-powerkey",
.num_resources = ARRAY_SIZE(axp20x_pek_resources),
.resources = axp20x_pek_resources,
},
{
.name = "axp20x-regulator",
},
{
.name = "axp20x-charger",
.num_resources = ARRAY_SIZE(axp20x_charger_resources),
.resources = axp20x_charger_resources,
},
{
.name = "axp20x-gpio",
.num_resources = ARRAY_SIZE(axp20x_gpio_resources),
.resources = axp20x_gpio_resources,
},
};
void axp20x_power_off(void)
{
uint8_t val;
pr_info("[axp] send power-off command!\n");
mdelay(20);
if (axp20x_config.power_start != 1) {
axp_regmap_read(axp20x_pm_power->regmap, AXP20X_STATUS, &val);
if (val & 0xF0) {
axp_regmap_read(axp20x_pm_power->regmap,
AXP20X_MODE_CHGSTATUS, &val);
if ((axp20x_config.pmu_bat_unused == 0)
&& (val & 0x20)
&& (val & 0x10)) {
pr_info("[axp] set flag!\n");
axp_regmap_read(axp20x_pm_power->regmap,
AXP20X_BUFFERC, &val);
if (0x0d != val)
axp_regmap_write(
axp20x_pm_power->regmap,
AXP20X_BUFFERC, 0x0f);
mdelay(20);
pr_info("[axp] reboot!\n");
machine_restart(NULL);
pr_warn("[axp] warning!!! arch can't reboot,\"\
\" maybe some error happend!\n");
}
}
}
axp_regmap_read(axp20x_pm_power->regmap, AXP20X_BUFFERC, &val);
if (0x0d != val)
axp_regmap_write(axp20x_pm_power->regmap, AXP20X_BUFFERC, 0x00);
mdelay(20);
axp_regmap_set_bits(axp20x_pm_power->regmap, AXP20X_OFF_CTL, 0x80);
mdelay(20);
pr_warn("[axp] warning!!! axp can't power-off,\"\
\" maybe some error happend!\n");
}
static int axp20x_init_chip(struct axp_dev *axp20x)
{
uint8_t chip_id, dcdc2_ctl;
int err;
err = axp_regmap_read(axp20x->regmap, AXP20X_IC_TYPE, &chip_id);
if (err) {
pr_err("[%s] try to read chip id failed!\n",
axp_name[axp20x_pmu_num]);
return err;
}
if (chip_id & 0x01) {
pr_info("[%s] chip id detect 0x%x !\n",
axp_name[axp20x_pmu_num], chip_id);
} else {
pr_info("[%s] chip id not detect 0x%x !\n",
axp_name[axp20x_pmu_num], chip_id);
}
#if 0
/* enable dcdc2 dvm */
err = axp_regmap_read(axp20x->regmap, AXP20X_DC23_DVM_CTL, &dcdc2_ctl);
if (err) {
pr_err("[%s] try to read dcdc2 dvm failed!\n",
axp_name[axp20x_pmu_num]);
return err;
}
dcdc2_ctl |= (0x1<<2);
err = axp_regmap_write(axp20x->regmap, AXP20X_DC23_DVM_CTL, dcdc2_ctl);
if (err) {
pr_err("[%s] try to enable dcdc2 dvm failed!\n",
axp_name[axp20x_pmu_num]);
return err;
}
pr_info("[%s] enable dcdc2 dvm.\n",
axp_name[axp20x_pmu_num]);
/*init N_VBUSEN status*/
if (axp20x_config.pmu_vbusen_func)
axp_regmap_set_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x10);
else
axp_regmap_clr_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x10);
/*init 16's reset pmu en */
if (axp20x_config.pmu_reset)
axp_regmap_set_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x08);
else
axp_regmap_clr_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x08);
/*init irq wakeup en*/
if (axp20x_config.pmu_irq_wakeup)
axp_regmap_set_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x80);
else
axp_regmap_clr_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x80);
/*init pmu over temperature protection*/
if (axp20x_config.pmu_hot_shutdown)
axp_regmap_set_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x04);
else
axp_regmap_clr_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x04);
/*init inshort status*/
if (axp20x_config.pmu_inshort)
axp_regmap_set_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x60);
else
axp_regmap_clr_bits(axp20x->regmap, AXP20X_HOTOVER_CTL, 0x60);
#endif
return 0;
}
static void axp20x_wakeup_event(void)
{
__pm_wakeup_event(axp20x_ws, 0);
}
static s32 axp20x_usb_det(void)
{
u8 value = 0;
int ret = 0;
axp_regmap_read(axp20x_pm_power->regmap, AXP20X_STATUS, &value);
if (value & 0x10) {
axp_usb_connect = 1;
ret = 1;
}
return ret;
}
static s32 axp20x_usb_vbus_output(int high)
{
u8 ret = 0;
ret = axp_regmap_clr_bits_sync(axp20x_pm_power->regmap,
AXP20X_HOTOVER_CTL, 0x10);
if (ret)
return ret;
if (high)
ret = axp_regmap_set_bits_sync(axp20x_pm_power->regmap,
AXP20X_IPS_SET, 0x04);
else
ret = axp_regmap_clr_bits_sync(axp20x_pm_power->regmap,
AXP20X_IPS_SET, 0x04);
return ret;
}
static int axp20x_cfg_pmux_para(int num, struct aw_pm_info *api, int *pmu_id)
{
char name[8];
struct device_node *np;
sprintf(name, "pmu%d", num);
np = of_find_node_by_type(NULL, name);
if (NULL == np) {
pr_err("can not find device_type for %s\n", name);
return -1;
}
if (!of_device_is_available(np)) {
pr_err("can not find node for %s\n", name);
return -1;
}
#ifdef CONFIG_AXP_TWI_USED
if (api != NULL) {
api->pmu_arg.twi_port = axp20x_pm_power->regmap->client->adapter->nr;
api->pmu_arg.dev_addr = axp20x_pm_power->regmap->client->addr;
}
#endif
*pmu_id = axp20x_config.pmu_id;
return 0;
}
static const char *axp20x_get_pmu_name(void)
{
return axp_name[axp20x_pmu_num];
}
static struct axp_dev *axp20x_get_pmu_dev(void)
{
return axp20x_pm_power;
}
struct axp_platform_ops axp20x_platform_ops = {
.usb_det = axp20x_usb_det,
.usb_vbus_output = axp20x_usb_vbus_output,
.cfg_pmux_para = axp20x_cfg_pmux_para,
.get_pmu_name = axp20x_get_pmu_name,
.get_pmu_dev = axp20x_get_pmu_dev,
.pmu_regulator_save = axp20x_regulator_save,
.pmu_regulator_restore = axp20x_regulator_restore,
};
static const struct i2c_device_id axp20x_id_table[] = {
{ "axp201s", 0 },
{ "axp203", 0 },
{ "axp209", 0 },
{ "axp233", 0 },
{}
};
static const struct of_device_id axp20x_dt_ids[] = {
{ .compatible = "axp201s", },
{ .compatible = "axp203", },
{ .compatible = "axp209", },
{ .compatible = "axp233", },
{},
};
MODULE_DEVICE_TABLE(of, axp20x_dt_ids);
#ifdef CONFIG_AXP_TWI_USED
static int axp20x_probe(struct i2c_client *client,
const struct i2c_device_id *id)
#else
static int axp20x_probe(struct platform_device *pdev)
#endif
{
int ret;
struct axp_dev *axp20x;
struct device_node *node;
struct device *device;
#ifdef CONFIG_AXP_TWI_USED
node = client->dev.of_node;
device = &client->dev;
#else
node = pdev->dev.of_node;
device = &pdev->dev;
#endif
axp20x_pmu_num = axp_get_pmu_num(axp20x_cn_mapping,
ARRAY_SIZE(axp20x_cn_mapping));
if (axp20x_pmu_num < 0) {
pr_err("%s get pmu num failed\n", __func__);
return axp20x_pmu_num;
}
if (node) {
/* get dt and sysconfig */
if (!of_device_is_available(node)) {
axp20x_config.pmu_used = 0;
pr_err("%s: pmu_used = %u\n", __func__,
axp20x_config.pmu_used);
return -EPERM;
} else {
axp20x_config.pmu_used = 1;
ret = axp_dt_parse(node, axp20x_pmu_num,
&axp20x_config);
if (ret) {
pr_err("%s parse device tree err\n", __func__);
return -EINVAL;
}
}
} else {
pr_err("AXP20x device tree err!\n");
return -EBUSY;
}
axp20x = devm_kzalloc(device, sizeof(*axp20x), GFP_KERNEL);
if (!axp20x)
return -ENOMEM;
axp20x->dev = device;
axp20x->nr_cells = ARRAY_SIZE(axp20x_cells);
axp20x->cells = axp20x_cells;
axp20x->pmu_num = axp20x_pmu_num;
ret = axp_mfd_cell_name_init(axp20x_cn_mapping,
ARRAY_SIZE(axp20x_cn_mapping), axp20x->pmu_num,
axp20x->nr_cells, axp20x->cells);
if (ret)
return ret;
#ifdef CONFIG_AXP_TWI_USED
axp20x->regmap = axp_regmap_init_i2c(device);
#else
axp20x->regmap = axp_regmap_init_arisc_rsb(device, AXP20X_RSB_RTSADDR);
#endif
if (IS_ERR(axp20x->regmap)) {
ret = PTR_ERR(axp20x->regmap);
dev_err(device, "regmap init failed: %d\n", ret);
return ret;
}
#ifdef CONFIG_AXP_TWI_USED
i2c_set_clientdata(client, axp20x);
#else
platform_set_drvdata(pdev, axp20x);
#endif
ret = axp20x_init_chip(axp20x);
if (ret)
return ret;
axp20x_pm_power = axp20x;
axp_platform_ops_set(axp20x->pmu_num, &axp20x_platform_ops);
ret = axp_mfd_add_devices(axp20x);
if (ret) {
dev_err(axp20x->dev, "failed to add MFD devices: %d\n", ret);
return ret;
}
#ifdef CONFIG_AXP_TWI_USED
axp20x->irq = client->irq;
#else
axp20x->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
#endif
axp20x->irq_data = axp_irq_chip_register(axp20x->regmap, axp20x->irq,
IRQF_SHARED
| IRQF_NO_SUSPEND,
&axp20x_regmap_irq_chip,
axp20x_wakeup_event);
if (IS_ERR(axp20x->irq_data)) {
ret = PTR_ERR(axp20x->irq_data);
dev_err(device, "axp init irq failed: %d\n", ret);
return ret;
}
if (!pm_power_off)
pm_power_off = axp20x_power_off;
axp20x_ws = wakeup_source_register("axp20_wakeup_source");
return 0;
}
#ifdef CONFIG_AXP_TWI_USED
static int axp20x_remove(struct i2c_client *client)
#else
static int axp20x_remove(struct platform_device *pdev)
#endif
{
struct axp_dev *axp20x;
#ifdef CONFIG_AXP_TWI_USED
axp20x = i2c_get_clientdata(client);
#else
axp20x = platform_get_drvdata(pdev);
#endif
if (axp20x == axp20x_pm_power) {
axp20x_pm_power = NULL;
pm_power_off = NULL;
}
axp_mfd_remove_devices(axp20x);
axp_irq_chip_unregister(axp20x->irq, axp20x->irq_data);
return 0;
}
#ifdef CONFIG_AXP_TWI_USED
static struct i2c_driver axp20x_driver = {
#else
static struct platform_driver axp20x_driver = {
#endif
.driver = {
.name = "axp20x",
.owner = THIS_MODULE,
.of_match_table = axp20x_dt_ids,
},
.probe = axp20x_probe,
.remove = axp20x_remove,
#ifdef CONFIG_AXP_TWI_USED
.id_table = axp20x_id_table,
#endif
};
static int __init axp20x_init(void)
{
int ret;
#ifdef CONFIG_AXP_TWI_USED
ret = i2c_add_driver(&axp20x_driver);
#else
ret = platform_driver_register(&axp20x_driver);
#endif
if (ret != 0)
pr_err("Failed to register axp20x I2C driver: %d\n", ret);
return ret;
}
subsys_initcall_sync(axp20x_init);
static void __exit axp20x_exit(void)
{
#ifdef CONFIG_AXP_TWI_USED
i2c_del_driver(&axp20x_driver);
#else
platform_driver_unregister(&axp20x_driver);
#endif
}
module_exit(axp20x_exit);
MODULE_DESCRIPTION("Driver of AXP20X");
MODULE_AUTHOR("pannan");
MODULE_LICENSE("GPL");