staging:iio:meter:ade7753 allocate chip state with iio_dev; allocate buffers within state
Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk> Acked-by: Michael Hennerich <michael.hennerich@analog.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
parent
ba61bb18c2
commit
bfccd4fb9a
|
@ -29,7 +29,7 @@ static int ade7753_spi_write_reg_8(struct device *dev,
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
||||||
struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
|
struct ade7753_state *st = iio_priv(indio_dev);
|
||||||
|
|
||||||
mutex_lock(&st->buf_lock);
|
mutex_lock(&st->buf_lock);
|
||||||
st->tx[0] = ADE7753_WRITE_REG(reg_address);
|
st->tx[0] = ADE7753_WRITE_REG(reg_address);
|
||||||
|
@ -47,7 +47,7 @@ static int ade7753_spi_write_reg_16(struct device *dev,
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
||||||
struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
|
struct ade7753_state *st = iio_priv(indio_dev);
|
||||||
|
|
||||||
mutex_lock(&st->buf_lock);
|
mutex_lock(&st->buf_lock);
|
||||||
st->tx[0] = ADE7753_WRITE_REG(reg_address);
|
st->tx[0] = ADE7753_WRITE_REG(reg_address);
|
||||||
|
@ -64,7 +64,7 @@ static int ade7753_spi_read_reg_8(struct device *dev,
|
||||||
u8 *val)
|
u8 *val)
|
||||||
{
|
{
|
||||||
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
||||||
struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
|
struct ade7753_state *st = iio_priv(indio_dev);
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
ret = spi_w8r8(st->us, ADE7753_READ_REG(reg_address));
|
ret = spi_w8r8(st->us, ADE7753_READ_REG(reg_address));
|
||||||
|
@ -83,7 +83,7 @@ static int ade7753_spi_read_reg_16(struct device *dev,
|
||||||
u16 *val)
|
u16 *val)
|
||||||
{
|
{
|
||||||
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
||||||
struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
|
struct ade7753_state *st = iio_priv(indio_dev);
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
ret = spi_w8r16(st->us, ADE7753_READ_REG(reg_address));
|
ret = spi_w8r16(st->us, ADE7753_READ_REG(reg_address));
|
||||||
|
@ -105,7 +105,7 @@ static int ade7753_spi_read_reg_24(struct device *dev,
|
||||||
{
|
{
|
||||||
struct spi_message msg;
|
struct spi_message msg;
|
||||||
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
||||||
struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
|
struct ade7753_state *st = iio_priv(indio_dev);
|
||||||
int ret;
|
int ret;
|
||||||
struct spi_transfer xfers[] = {
|
struct spi_transfer xfers[] = {
|
||||||
{
|
{
|
||||||
|
@ -369,10 +369,11 @@ static int ade7753_stop_device(struct device *dev)
|
||||||
return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
|
return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ade7753_initial_setup(struct ade7753_state *st)
|
static int ade7753_initial_setup(struct iio_dev *indio_dev)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
struct device *dev = &st->indio_dev->dev;
|
struct device *dev = &indio_dev->dev;
|
||||||
|
struct ade7753_state *st = iio_priv(indio_dev);
|
||||||
|
|
||||||
/* use low spi speed for init */
|
/* use low spi speed for init */
|
||||||
st->us->mode = SPI_MODE_3;
|
st->us->mode = SPI_MODE_3;
|
||||||
|
@ -416,7 +417,7 @@ static ssize_t ade7753_write_frequency(struct device *dev,
|
||||||
size_t len)
|
size_t len)
|
||||||
{
|
{
|
||||||
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
struct iio_dev *indio_dev = dev_get_drvdata(dev);
|
||||||
struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
|
struct ade7753_state *st = iio_priv(indio_dev);
|
||||||
unsigned long val;
|
unsigned long val;
|
||||||
int ret;
|
int ret;
|
||||||
u16 reg, t;
|
u16 reg, t;
|
||||||
|
@ -512,62 +513,44 @@ static const struct iio_info ade7753_info = {
|
||||||
static int __devinit ade7753_probe(struct spi_device *spi)
|
static int __devinit ade7753_probe(struct spi_device *spi)
|
||||||
{
|
{
|
||||||
int ret, regdone = 0;
|
int ret, regdone = 0;
|
||||||
struct ade7753_state *st = kzalloc(sizeof *st, GFP_KERNEL);
|
struct ade7753_state *st;
|
||||||
if (!st) {
|
struct iio_dev *indio_dev;
|
||||||
ret = -ENOMEM;
|
|
||||||
|
/* setup the industrialio driver allocated elements */
|
||||||
|
indio_dev = iio_allocate_device(sizeof(*st));
|
||||||
|
if (indio_dev == NULL) {
|
||||||
|
ret = -ENOMEM;
|
||||||
goto error_ret;
|
goto error_ret;
|
||||||
}
|
}
|
||||||
/* this is only used for removal purposes */
|
/* this is only used for removal purposes */
|
||||||
spi_set_drvdata(spi, st);
|
spi_set_drvdata(spi, indio_dev);
|
||||||
|
|
||||||
/* Allocate the comms buffers */
|
st = iio_priv(indio_dev);
|
||||||
st->rx = kzalloc(sizeof(*st->rx)*ADE7753_MAX_RX, GFP_KERNEL);
|
|
||||||
if (st->rx == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto error_free_st;
|
|
||||||
}
|
|
||||||
st->tx = kzalloc(sizeof(*st->tx)*ADE7753_MAX_TX, GFP_KERNEL);
|
|
||||||
if (st->tx == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto error_free_rx;
|
|
||||||
}
|
|
||||||
st->us = spi;
|
st->us = spi;
|
||||||
mutex_init(&st->buf_lock);
|
mutex_init(&st->buf_lock);
|
||||||
/* setup the industrialio driver allocated elements */
|
|
||||||
st->indio_dev = iio_allocate_device(0);
|
|
||||||
if (st->indio_dev == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto error_free_tx;
|
|
||||||
}
|
|
||||||
|
|
||||||
st->indio_dev->name = spi->dev.driver->name;
|
indio_dev->name = spi->dev.driver->name;
|
||||||
st->indio_dev->dev.parent = &spi->dev;
|
indio_dev->dev.parent = &spi->dev;
|
||||||
st->indio_dev->info = &ade7753_info;
|
indio_dev->info = &ade7753_info;
|
||||||
st->indio_dev->dev_data = (void *)(st);
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
||||||
st->indio_dev->modes = INDIO_DIRECT_MODE;
|
|
||||||
|
|
||||||
ret = iio_device_register(st->indio_dev);
|
ret = iio_device_register(indio_dev);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto error_free_dev;
|
goto error_free_dev;
|
||||||
regdone = 1;
|
regdone = 1;
|
||||||
|
|
||||||
/* Get the device into a sane initial state */
|
/* Get the device into a sane initial state */
|
||||||
ret = ade7753_initial_setup(st);
|
ret = ade7753_initial_setup(indio_dev);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto error_free_dev;
|
goto error_free_dev;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_free_dev:
|
error_free_dev:
|
||||||
if (regdone)
|
if (regdone)
|
||||||
iio_device_unregister(st->indio_dev);
|
iio_device_unregister(indio_dev);
|
||||||
else
|
else
|
||||||
iio_free_device(st->indio_dev);
|
iio_free_device(indio_dev);
|
||||||
error_free_tx:
|
|
||||||
kfree(st->tx);
|
|
||||||
error_free_rx:
|
|
||||||
kfree(st->rx);
|
|
||||||
error_free_st:
|
|
||||||
kfree(st);
|
|
||||||
error_ret:
|
error_ret:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -576,19 +559,13 @@ error_ret:
|
||||||
static int ade7753_remove(struct spi_device *spi)
|
static int ade7753_remove(struct spi_device *spi)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
struct ade7753_state *st = spi_get_drvdata(spi);
|
struct iio_dev *indio_dev = spi_get_drvdata(spi);
|
||||||
struct iio_dev *indio_dev = st->indio_dev;
|
|
||||||
|
|
||||||
ret = ade7753_stop_device(&(indio_dev->dev));
|
ret = ade7753_stop_device(&(indio_dev->dev));
|
||||||
if (ret)
|
if (ret)
|
||||||
goto err_ret;
|
goto err_ret;
|
||||||
|
|
||||||
iio_device_unregister(indio_dev);
|
iio_device_unregister(indio_dev);
|
||||||
kfree(st->tx);
|
|
||||||
kfree(st->rx);
|
|
||||||
kfree(st);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
err_ret:
|
err_ret:
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -60,17 +60,15 @@
|
||||||
/**
|
/**
|
||||||
* struct ade7753_state - device instance specific data
|
* struct ade7753_state - device instance specific data
|
||||||
* @us: actual spi_device
|
* @us: actual spi_device
|
||||||
* @indio_dev: industrial I/O device structure
|
|
||||||
* @tx: transmit buffer
|
* @tx: transmit buffer
|
||||||
* @rx: receive buffer
|
* @rx: receive buffer
|
||||||
* @buf_lock: mutex to protect tx and rx
|
* @buf_lock: mutex to protect tx and rx
|
||||||
**/
|
**/
|
||||||
struct ade7753_state {
|
struct ade7753_state {
|
||||||
struct spi_device *us;
|
struct spi_device *us;
|
||||||
struct iio_dev *indio_dev;
|
struct mutex buf_lock;
|
||||||
u8 *tx;
|
u8 tx[ADE7753_MAX_TX] ____cacheline_aligned;
|
||||||
u8 *rx;
|
u8 rx[ADE7753_MAX_RX];
|
||||||
struct mutex buf_lock;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue