Skip to content

Commit

Permalink
staging:iio:max1363 take advantage of new iio_device_allocate private…
Browse files Browse the repository at this point in the history
… data.

The only fiddly bit in here was ensuring the regulator was available until after
the free had occured.

Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Jonathan Cameron authored and Greg Kroah-Hartman committed Apr 26, 2011
1 parent b6b7068 commit 3dba81b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 86 deletions.
2 changes: 0 additions & 2 deletions drivers/staging/iio/adc/max1363.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ struct max1363_chip_info {

/**
* struct max1363_state - driver instance specific data
* @indio_dev: the industrial I/O device
* @client: i2c_client
* @setupbyte: cache of current device setup byte
* @configbyte: cache of current device config byte
Expand All @@ -194,7 +193,6 @@ struct max1363_chip_info {
* @thresh_work: bh work structure for event handling
*/
struct max1363_state {
struct iio_dev *indio_dev;
struct i2c_client *client;
u8 setupbyte;
u8 configbyte;
Expand Down
147 changes: 69 additions & 78 deletions drivers/staging/iio/adc/max1363_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ static ssize_t max1363_show_precision_u(struct device *dev,
char *buf)
{
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
struct iio_dev *indio_dev = ring->indio_dev;
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(ring->indio_dev);
return sprintf(buf, "u%d/16\n", st->chip_info->bits);
}

Expand All @@ -165,8 +164,7 @@ static ssize_t max1363_show_precision_s(struct device *dev,
char *buf)
{
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
struct iio_dev *indio_dev = ring->indio_dev;
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(ring->indio_dev);
return sprintf(buf, "s%d/16\n", st->chip_info->bits);
}

Expand Down Expand Up @@ -239,7 +237,7 @@ static ssize_t max1363_read_single_channel(struct device *dev,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(indio_dev);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
struct i2c_client *client = st->client;
int ret = 0, len = 0;
Expand Down Expand Up @@ -337,8 +335,7 @@ static ssize_t max1363_show_scale(struct device *dev,
char *buf)
{
/* Driver currently only support internal vref */
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(dev_get_drvdata(dev));
/* Corresponds to Vref / 2^(bits) */

if ((1 << (st->chip_info->bits + 1))
Expand All @@ -355,8 +352,7 @@ static ssize_t max1363_show_name(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(dev_get_drvdata(dev));
return sprintf(buf, "%s\n", st->client->name);
}

Expand Down Expand Up @@ -1012,8 +1008,7 @@ static ssize_t max1363_monitor_show_freq(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(dev_get_drvdata(dev));
return sprintf(buf, "%d\n", max1363_monitor_speeds[st->monitor_speed]);
}

Expand All @@ -1023,7 +1018,7 @@ static ssize_t max1363_monitor_store_freq(struct device *dev,
size_t len)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(indio_dev);
int i, ret;
unsigned long val;
bool found = false;
Expand Down Expand Up @@ -1059,7 +1054,7 @@ static ssize_t max1363_show_thresh(struct device *dev,
bool high)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(indio_dev);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);

if (high)
Expand Down Expand Up @@ -1090,8 +1085,7 @@ static ssize_t max1363_store_thresh_unsigned(struct device *dev,
size_t len,
bool high)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(dev_get_drvdata(dev));
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
unsigned long val;
int ret;
Expand Down Expand Up @@ -1144,8 +1138,7 @@ static ssize_t max1363_store_thresh_signed(struct device *dev,
size_t len,
bool high)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(dev_get_drvdata(dev));
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
long val;
int ret;
Expand Down Expand Up @@ -1255,7 +1248,7 @@ static int max1363_int_th(struct iio_dev *indio_dev,
s64 timestamp,
int not_test)
{
struct max1363_state *st = indio_dev->dev_data;
struct max1363_state *st = iio_priv(indio_dev);

st->last_timestamp = timestamp;
schedule_work(&st->thresh_work);
Expand All @@ -1266,41 +1259,42 @@ static void max1363_thresh_handler_bh(struct work_struct *work_s)
{
struct max1363_state *st = container_of(work_s, struct max1363_state,
thresh_work);
struct iio_dev *indio_dev = iio_priv_to_dev(st);
u8 rx;
u8 tx[2] = { st->setupbyte,
MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };

i2c_master_recv(st->client, &rx, 1);
if (rx & (1 << 0))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_LOW_THRESH(3),
st->last_timestamp);
if (rx & (1 << 1))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_HIGH_THRESH(3),
st->last_timestamp);
if (rx & (1 << 2))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_LOW_THRESH(2),
st->last_timestamp);
if (rx & (1 << 3))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_HIGH_THRESH(2),
st->last_timestamp);
if (rx & (1 << 4))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_LOW_THRESH(1),
st->last_timestamp);
if (rx & (1 << 5))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_HIGH_THRESH(1),
st->last_timestamp);
if (rx & (1 << 6))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_LOW_THRESH(0),
st->last_timestamp);
if (rx & (1 << 7))
iio_push_event(st->indio_dev, 0,
iio_push_event(indio_dev, 0,
IIO_EVENT_CODE_IN_HIGH_THRESH(0),
st->last_timestamp);
enable_irq(st->client->irq);
Expand All @@ -1312,7 +1306,7 @@ static ssize_t max1363_read_interrupt_config(struct device *dev,
char *buf)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(indio_dev);
struct iio_event_attr *this_attr = to_iio_event_attr(attr);
int val;

Expand Down Expand Up @@ -1466,15 +1460,15 @@ static ssize_t max1363_write_interrupt_config(struct device *dev,
size_t len)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct max1363_state *st = iio_dev_get_devdata(indio_dev);
struct max1363_state *st = iio_priv(indio_dev);
struct iio_event_attr *this_attr = to_iio_event_attr(attr);
unsigned long val;
int ret;
u16 unifiedmask;
ret = strict_strtoul(buf, 10, &val);
if (ret)
return -EINVAL;
mutex_lock(&st->indio_dev->mlock);
mutex_lock(&indio_dev->mlock);
unifiedmask = st->mask_low | st->mask_high;
if (this_attr->mask & 0x08) {
/* If we are disabling no need to test */
Expand Down Expand Up @@ -1507,7 +1501,7 @@ static ssize_t max1363_write_interrupt_config(struct device *dev,

max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low));
error_ret:
mutex_unlock(&st->indio_dev->mlock);
mutex_unlock(&indio_dev->mlock);

return len;
}
Expand Down Expand Up @@ -1687,77 +1681,76 @@ static int __devinit max1363_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret, i, regdone = 0;
struct max1363_state *st = kzalloc(sizeof(*st), GFP_KERNEL);
if (st == NULL) {
ret = -ENOMEM;
goto error_ret;
}

/* this is only used for device removal purposes */
i2c_set_clientdata(client, st);
struct max1363_state *st;
struct iio_dev *indio_dev;
struct regulator *reg;

atomic_set(&st->protect_ring, 0);

st->chip_info = &max1363_chip_info_tbl[id->driver_data];
st->reg = regulator_get(&client->dev, "vcc");
if (!IS_ERR(st->reg)) {
ret = regulator_enable(st->reg);
reg = regulator_get(&client->dev, "vcc");
if (!IS_ERR(reg)) {
ret = regulator_enable(reg);
if (ret)
goto error_put_reg;
}
st->client = client;

st->indio_dev = iio_allocate_device(0);
if (st->indio_dev == NULL) {
indio_dev = iio_allocate_device(sizeof(struct max1363_state));
if (indio_dev == NULL) {
ret = -ENOMEM;
goto error_disable_reg;
}
st = iio_priv(indio_dev);
st->reg = reg;
/* this is only used for device removal purposes */
i2c_set_clientdata(client, indio_dev);

atomic_set(&st->protect_ring, 0);

st->chip_info = &max1363_chip_info_tbl[id->driver_data];
st->client = client;

st->indio_dev->available_scan_masks
= kzalloc(sizeof(*st->indio_dev->available_scan_masks)*
indio_dev->available_scan_masks
= kzalloc(sizeof(*indio_dev->available_scan_masks)*
(st->chip_info->num_modes + 1), GFP_KERNEL);
if (!st->indio_dev->available_scan_masks) {
if (!indio_dev->available_scan_masks) {
ret = -ENOMEM;
goto error_free_device;
}

for (i = 0; i < st->chip_info->num_modes; i++)
st->indio_dev->available_scan_masks[i] =
indio_dev->available_scan_masks[i] =
max1363_mode_table[st->chip_info->mode_list[i]]
.modemask;
/* Estabilish that the iio_dev is a child of the i2c device */
st->indio_dev->dev.parent = &client->dev;
st->indio_dev->attrs = st->chip_info->dev_attrs;
indio_dev->dev.parent = &client->dev;
indio_dev->attrs = st->chip_info->dev_attrs;

/* Todo: this shouldn't be here. */
st->indio_dev->dev_data = (void *)(st);
st->indio_dev->driver_module = THIS_MODULE;
st->indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->driver_module = THIS_MODULE;
indio_dev->modes = INDIO_DIRECT_MODE;
if (st->chip_info->monitor_mode && client->irq) {
st->indio_dev->num_interrupt_lines = 1;
st->indio_dev->event_attrs
indio_dev->num_interrupt_lines = 1;
indio_dev->event_attrs
= &max1363_event_attribute_group;
}

ret = max1363_initial_setup(st);
if (ret)
goto error_free_available_scan_masks;

ret = max1363_register_ring_funcs_and_init(st->indio_dev);
ret = max1363_register_ring_funcs_and_init(indio_dev);
if (ret)
goto error_free_available_scan_masks;

ret = iio_device_register(st->indio_dev);
ret = iio_device_register(indio_dev);
if (ret)
goto error_cleanup_ring;
regdone = 1;
ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
ret = iio_ring_buffer_register(indio_dev->ring, 0);
if (ret)
goto error_cleanup_ring;

if (st->chip_info->monitor_mode && client->irq) {
ret = iio_register_interrupt_line(client->irq,
st->indio_dev,
indio_dev,
0,
IRQF_TRIGGER_RISING,
client->name);
Expand All @@ -1769,44 +1762,42 @@ static int __devinit max1363_probe(struct i2c_client *client,

return 0;
error_uninit_ring:
iio_ring_buffer_unregister(st->indio_dev->ring);
iio_ring_buffer_unregister(indio_dev->ring);
error_cleanup_ring:
max1363_ring_cleanup(st->indio_dev);
max1363_ring_cleanup(indio_dev);
error_free_available_scan_masks:
kfree(st->indio_dev->available_scan_masks);
kfree(indio_dev->available_scan_masks);
error_free_device:
if (!regdone)
iio_free_device(st->indio_dev);
iio_free_device(indio_dev);
else
iio_device_unregister(st->indio_dev);
iio_device_unregister(indio_dev);
error_disable_reg:
if (!IS_ERR(st->reg))
regulator_disable(st->reg);
error_put_reg:
if (!IS_ERR(st->reg))
regulator_put(st->reg);
kfree(st);

error_ret:
return ret;
}

static int max1363_remove(struct i2c_client *client)
{
struct max1363_state *st = i2c_get_clientdata(client);
struct iio_dev *indio_dev = st->indio_dev;
struct iio_dev *indio_dev = i2c_get_clientdata(client);
struct max1363_state *st = iio_priv(indio_dev);
struct regulator *reg = st->reg;

if (st->chip_info->monitor_mode && client->irq)
iio_unregister_interrupt_line(st->indio_dev, 0);
iio_unregister_interrupt_line(indio_dev, 0);
iio_ring_buffer_unregister(indio_dev->ring);
max1363_ring_cleanup(indio_dev);
kfree(st->indio_dev->available_scan_masks);
iio_device_unregister(indio_dev);
if (!IS_ERR(st->reg)) {
regulator_disable(st->reg);
regulator_put(st->reg);
kfree(indio_dev->available_scan_masks);
if (!IS_ERR(reg)) {
regulator_disable(reg);
regulator_put(reg);
}
kfree(st);
iio_device_unregister(indio_dev);

return 0;
}
Expand Down
Loading

0 comments on commit 3dba81b

Please sign in to comment.