Skip to content

Commit

Permalink
tps65217: Enable KEY_POWER press on AC loss / PWR_BUT
Browse files Browse the repository at this point in the history
Adaption of the original patch by Andrew Bradford <andrew.bradford@omni-id.com>
Some minor devm_* changes and DT support.

Signed-off-by: Pantelis Antoniou <panto@antoniou-consulting.com>
  • Loading branch information
pantoniou committed May 27, 2013
1 parent fac4118 commit 1a1a660
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
121 changes: 119 additions & 2 deletions drivers/mfd/tps65217.c
Expand Up @@ -26,6 +26,9 @@
#include <linux/err.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>

#include <linux/mfd/core.h>
#include <linux/mfd/tps65217.h>
Expand Down Expand Up @@ -153,26 +156,128 @@ static const struct of_device_id tps65217_of_match[] = {
{ /* sentinel */ },
};

static irqreturn_t tps65217_irq(int irq, void *irq_data)
{
struct tps65217 *tps = irq_data;
unsigned int int_reg = 0, status_reg = 0;

tps65217_reg_read(tps, TPS65217_REG_INT, &int_reg);
tps65217_reg_read(tps, TPS65217_REG_STATUS, &status_reg);
if (status_reg)
dev_dbg(tps->dev, "status now: 0x%X\n", status_reg);

if (!int_reg)
return IRQ_NONE;

if (int_reg & TPS65217_INT_PBI) {
/* Handle push button */
dev_dbg(tps->dev, "power button status change\n");
input_report_key(tps->pwr_but, KEY_POWER,
status_reg & TPS65217_STATUS_PB);
input_sync(tps->pwr_but);
}
if (int_reg & TPS65217_INT_ACI) {
/* Handle AC power status change */
dev_dbg(tps->dev, "AC power status change\n");
/* Press KEY_POWER when AC not present */
input_report_key(tps->pwr_but, KEY_POWER,
~status_reg & TPS65217_STATUS_ACPWR);
input_sync(tps->pwr_but);
}
if (int_reg & TPS65217_INT_USBI) {
/* Handle USB power status change */
dev_dbg(tps->dev, "USB power status change\n");
}

return IRQ_HANDLED;
}

static int tps65217_probe_pwr_but(struct tps65217 *tps)
{
int ret;
unsigned int int_reg;

tps->pwr_but = devm_input_allocate_device(tps->dev);
if (!tps->pwr_but) {
dev_err(tps->dev,
"Failed to allocated pwr_but input device\n");
return -ENOMEM;
}

tps->pwr_but->evbit[0] = BIT_MASK(EV_KEY);
tps->pwr_but->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
tps->pwr_but->name = "tps65217_pwr_but";
ret = input_register_device(tps->pwr_but);
if (ret) {
/* NOTE: devm managed device */
dev_err(tps->dev, "Failed to register button device\n");
return ret;
}
ret = devm_request_threaded_irq(tps->dev,
tps->irq, NULL, tps65217_irq, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
"tps65217", tps);
if (ret != 0) {
dev_err(tps->dev, "Failed to request IRQ %d\n", tps->irq);
return ret;
}

/* enable the power button interrupt */
ret = tps65217_reg_read(tps, TPS65217_REG_INT, &int_reg);
if (ret < 0) {
dev_err(tps->dev, "Failed to read INT reg\n");
return ret;
}
int_reg &= ~TPS65217_INT_PBM;
tps65217_reg_write(tps, TPS65217_REG_INT, int_reg, TPS65217_PROTECT_NONE);
return 0;
}

static int tps65217_probe(struct i2c_client *client,
const struct i2c_device_id *ids)
{
struct tps65217 *tps;
unsigned int version;
unsigned int chip_id = ids->driver_data;
const struct of_device_id *match;
struct device_node *node;
bool status_off = false;
int irq = -1, irq_gpio = -1;
int ret;

if (client->dev.of_node) {
node = client->dev.of_node;
if (node) {
match = of_match_device(tps65217_of_match, &client->dev);
if (!match) {
dev_err(&client->dev,
"Failed to find matching dt id\n");
return -EINVAL;
}
chip_id = (unsigned int)match->data;
status_off = of_property_read_bool(client->dev.of_node,
status_off = of_property_read_bool(node,
"ti,pmic-shutdown-controller");

/* at first try to get irq via OF method */
irq = irq_of_parse_and_map(node, 0);
if (irq <= 0) {
irq = -1;
irq_gpio = of_get_named_gpio(node, "irq-gpio", 0);
if (irq_gpio >= 0) {
/* valid gpio; convert to irq */
ret = devm_gpio_request_one(&client->dev,
irq_gpio, GPIOF_DIR_IN,
"tps65217-gpio-irq");
if (ret != 0)
dev_warn(&client->dev, "Failed to "
"request gpio #%d\n", irq_gpio);
irq = gpio_to_irq(irq_gpio);
if (irq <= 0) {
dev_warn(&client->dev, "Failed to "
"convert gpio #%d to irq\n",
irq_gpio);
irq = -1;
}
}
}
}

if (!chip_id) {
Expand All @@ -196,6 +301,18 @@ static int tps65217_probe(struct i2c_client *client,
return ret;
}

tps->irq = irq;
tps->irq_gpio = irq_gpio;

/* we got an irq, request it */
if (tps->irq >= 0) {
ret = tps65217_probe_pwr_but(tps);
if (ret < 0) {
dev_err(tps->dev, "Failed to probe pwr_but\n");
return ret;
}
}

ret = mfd_add_devices(tps->dev, -1, tps65217s,
ARRAY_SIZE(tps65217s), NULL, 0, NULL);
if (ret < 0) {
Expand Down
6 changes: 6 additions & 0 deletions include/linux/mfd/tps65217.h
Expand Up @@ -21,6 +21,7 @@
#include <linux/i2c.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/input.h>

/* TPS chip id list */
#define TPS65217 0xF0
Expand Down Expand Up @@ -274,6 +275,11 @@ struct tps65217 {
struct regulator_dev *rdev[TPS65217_NUM_REGULATOR];
struct tps_info *info[TPS65217_NUM_REGULATOR];
struct regmap *regmap;

/* Power button and IRQ handling */
int irq_gpio; /* might not be set */
int irq;
struct input_dev *pwr_but;
};

static inline struct tps65217 *dev_to_tps65217(struct device *dev)
Expand Down

0 comments on commit 1a1a660

Please sign in to comment.