/*
* NXP pca9698.c - 40 bit I/O Expander on I2C bus
*
* Copyright (C) 2011 Thotaka Technologies Pvt Ltd
* Author: Balaji Venkatachalam <balaji.v@xxxxxxxxxxxx>
* based on pca953x.c written by eric miao <eric.miao@xxxxxxxxxxx>
*
* 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/module.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/i2c/pca9698.h>
#ifdef CONFIG_OF_GPIO
/*TODO: Not Tested. As it is adopted from PCA959x.c*/
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#endif
#include <linux/gpio.h>
#define DRV_NAME "pca9698"
#define PCA9698_INPUT 0
#define PCA9698_OUTPUT 1
#define PCA9698_INVERT 2
#define PCA9698_DIRECTION 3
static const struct i2c_device_id pca9698_id[] = {
{ "pca9698", 40, },
{ }
};
MODULE_DEVICE_TABLE(i2c, pca9698_id);
struct pca9698_chip {
unsigned gpio_start;
uint8_t reg_output[PCA9698_MAX_BANKS];
uint8_t reg_direction[PCA9698_MAX_BANKS];
struct i2c_client *client;
struct pca9698_platform_data *dyn_pdata;
struct gpio_chip gpio_chip;
char **names;
};
uint8_t i2cdataaddr[5] = {0, 0, 0, 0, 0};
static int pca9698_write_reg(struct pca9698_chip *chip, int reg, uint8_t *val)
{
int ret;
ret = i2c_smbus_write_i2c_block_data(chip->client,
((reg << 3)|0x80), 5, val);
if (ret < 0) {
dev_err(&chip->client->dev, "failed writing register\n");
return ret;
}
return 0;
}
static int pca9698_read_reg(struct pca9698_chip *chip, int reg, uint8_t *val)
{
int ret;
ret = i2c_smbus_read_i2c_block_data(chip->client,
((reg << 3)|0x80), 5, val);
if (ret < 0) {
dev_err(&chip->client->dev, "failed reading register\n");
return ret;
}
return 0;
}
static int pca9698_gpio_direction_input(struct gpio_chip *gc, unsigned off)
{
struct pca9698_chip *chip;
int ret, bankid;
chip = container_of(gc, struct pca9698_chip, gpio_chip);
bankid = 0;
do {
i2cdataaddr[bankid] = chip->reg_direction[bankid] | (1u << off);
} while (bankid <= PCA9698_MAX_BANKS);
ret = pca9698_write_reg(chip, PCA9698_DIRECTION, i2cdataaddr);
if (ret)
return ret;
bankid = 0;
do {
chip->reg_direction[bankid] = i2cdataaddr[bankid];
} while (bankid <= PCA9698_MAX_BANKS);
return 0;
}
static int pca9698_gpio_direction_output(struct gpio_chip *gc,
unsigned off, int val)
{
struct pca9698_chip *chip;
int ret, bankid = 0;
unsigned int port = 0;
chip = container_of(gc, struct pca9698_chip, gpio_chip);
port = off/8;
off = off%8;
bankid = 0;
do {
if (bankid == port) {
if (val)
i2cdataaddr[bankid] =
chip->reg_output[bankid] | (1u << off);
else
i2cdataaddr[bankid] =
chip->reg_output[bankid] & ~(1u << off);
} else
i2cdataaddr[bankid] = chip->reg_output[bankid];
} while (bankid <= PCA9698_MAX_BANKS);
ret = pca9698_write_reg(chip, PCA9698_OUTPUT, i2cdataaddr);
if (ret)
return ret;
bankid = 0;
do {
chip->reg_output[bankid] = i2cdataaddr[bankid];
} while (bankid <= PCA9698_MAX_BANKS);
bankid = 0;
do {
if (bankid == port)
i2cdataaddr[bankid] =
chip->reg_direction[bankid] & ~(1u << off);
else
i2cdataaddr[bankid] = chip->reg_direction[bankid];
} while (bankid <= PCA9698_MAX_BANKS);
ret = pca9698_write_reg(chip, PCA9698_DIRECTION, i2cdataaddr);
if (ret)
return ret;
bankid = 0;
do {
chip->reg_direction[bankid] = i2cdataaddr[bankid];
} while (bankid <= PCA9698_MAX_BANKS);
return 0;
}
static int pca9698_gpio_get_value(struct gpio_chip *gc, unsigned off)
{
struct pca9698_chip *chip;
int ret;
unsigned int port = 0;
chip = container_of(gc, struct pca9698_chip, gpio_chip);
ret = pca9698_read_reg(chip, PCA9698_INPUT, i2cdataaddr);
if (ret < 0) {
/* NOTE: diagnostic already emitted; that's all we should
* do unless gpio_*_value_cansleep() calls become different
* from their nonsleeping siblings (and report faults).
*/
return 0;
}
port = off/8;
off = off%8;
return (i2cdataaddr[port] & (1u << off)) ? 1 : 0;
}
static void pca9698_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
{
struct pca9698_chip *chip;
int ret, bankid = 0;
unsigned int port = 0;
chip = container_of(gc, struct pca9698_chip, gpio_chip);
port = off/8;
off = off%8;
bankid = 0;
do {
if (bankid == port) {
if (val)
i2cdataaddr[bankid] =
chip->reg_output[bankid] | (1u << off);
else
i2cdataaddr[bankid] =
chip->reg_output[bankid] & ~(1u << off);
} else {
i2cdataaddr[bankid] = chip->reg_output[bankid];
}
} while (bankid <= PCA9698_MAX_BANKS);
ret = pca9698_write_reg(chip, PCA9698_OUTPUT, i2cdataaddr);
if (ret)
return;
bankid = 0;
do {
chip->reg_output[bankid] = i2cdataaddr[bankid];
} while (bankid <= PCA9698_MAX_BANKS);
}
static void pca9698_setup_gpio(struct pca9698_chip *chip, int gpios)
{
struct gpio_chip *gc;
gc = &chip->gpio_chip;
gc->direction_input = pca9698_gpio_direction_input;
gc->direction_output = pca9698_gpio_direction_output;
gc->get = pca9698_gpio_get_value;
gc->set = pca9698_gpio_set_value;
gc->can_sleep = 1;
gc->base = chip->gpio_start;
gc->ngpio = gpios;
gc->label = chip->client->name;
gc->dev = &chip->client->dev;
gc->owner = THIS_MODULE;
gc->names = chip->names;
}
/*
* Handlers for alternative sources of platform_data
*/
#ifdef CONFIG_OF_GPIO
/*
* TODO: To be tested. As it is adopted from PCA953x.c
*/
*/
/*
* Translate OpenFirmware node properties into platform_data
*/
static struct pca9698_platform_data *
pca9698_get_alt_pdata(struct i2c_client *client)
{
struct pca9698_platform_data *pdata;
struct device_node *node;
const uint16_t *val;
node = dev_archdata_get_node(&client->dev.archdata);
if (node == NULL)
return NULL;
pdata = kzalloc(sizeof(struct pca9698_platform_data), GFP_KERNEL);
if (pdata == NULL) {
dev_err(&client->dev, "Unable to allocate platform_data\n");
return NULL;
}
pdata->gpio_base = -1;
val = of_get_property(node, "linux,gpio-base", NULL);
if (val) {
if (*val < 0)
dev_warn(&client->dev,
"invalid gpio-base in device tree\n");
else
pdata->gpio_base = *val;
}
val = of_get_property(node, "polarity", NULL);
if (val)
pdata->invert = *val;
return pdata;
}
#else
static struct pca9698_platform_data *
pca9698_get_alt_pdata(struct i2c_client *client)
{
return NULL;
}
#endif
static int __devinit pca9698_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct pca9698_platform_data *pdata;
struct pca9698_chip *chip;
int ret, bankid = 0;
chip = kzalloc(sizeof(struct pca9698_chip), GFP_KERNEL);
if (chip == NULL)
return -ENOMEM;
pdata = client->dev.platform_data;
if (pdata == NULL) {
pdata = pca9698_get_alt_pdata(client);
/*
* Unlike normal platform_data, this is allocated
* dynamically and must be freed in the driver
*/
chip->dyn_pdata = pdata;
}
if (pdata == NULL) {
dev_dbg(&client->dev, "no platform data\n");
ret = -EINVAL;
goto out_failed;
}
chip->client = client;
chip->gpio_start = pdata->gpio_base;
chip->names = pdata->names;
/* initialize cached registers from their original values.
* we can't share this chip with another i2c master.
*/
pca9698_setup_gpio(chip, id->driver_data);
ret = pca9698_read_reg(chip, PCA9698_OUTPUT, i2cdataaddr);
if (ret)
goto out_failed;
bankid = 0;
do {
chip->reg_output[bankid] = i2cdataaddr[0];
} while (bankid <= PCA9698_MAX_BANKS);
ret = pca9698_read_reg(chip, PCA9698_DIRECTION, i2cdataaddr);
if (ret)
goto out_failed;
bankid = 0;
do {
chip->reg_direction[bankid] = i2cd