static int mcp3422_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct iio_dev *iio; struct mcp3422 *adc; int err; u8 config; if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) return -ENODEV; iio = iio_device_alloc(sizeof(*adc)); if (!iio) return -ENOMEM; adc = iio_priv(iio); adc->i2c = client; mutex_init(&adc->lock); iio->dev.parent = &client->dev; iio->name = dev_name(&client->dev); iio->modes = INDIO_DIRECT_MODE; iio->info = &mcp3422_info; switch ((unsigned int)(id->driver_data)) { case 2: case 3: iio->channels = mcp3422_channels; iio->num_channels = ARRAY_SIZE(mcp3422_channels); break; case 4: iio->channels = mcp3424_channels; iio->num_channels = ARRAY_SIZE(mcp3424_channels); break; } /* meaningful default configuration */ config = (MCP3422_CONT_SAMPLING | MCP3422_CHANNEL_VALUE(1) | MCP3422_PGA_VALUE(MCP3422_PGA_1) | MCP3422_SAMPLE_RATE_VALUE(MCP3422_SRATE_240)); mcp3422_update_config(adc, config); err = iio_device_register(iio); if (err < 0) goto iio_free; i2c_set_clientdata(client, iio); return 0; iio_free: iio_device_free(iio); return err; }
static int mcp3422_read_channel(struct mcp3422 *adc, struct iio_chan_spec const *channel, int *value) { int ret; u8 config; u8 req_channel = channel->channel; if (req_channel != MCP3422_CHANNEL(adc->config)) { config = adc->config; config &= ~MCP3422_CHANNEL_MASK; config |= MCP3422_CHANNEL_VALUE(req_channel); config &= ~MCP3422_PGA_MASK; config |= MCP3422_PGA_VALUE(adc->pga[req_channel]); ret = mcp3422_update_config(adc, config); if (ret < 0) return ret; msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]); } return mcp3422_read(adc, value, &config); }
static int mcp3422_write_raw(struct iio_dev *iio, struct iio_chan_spec const *channel, int val1, int val2, long mask) { struct mcp3422 *adc = iio_priv(iio); u8 temp; u8 config = adc->config; u8 req_channel = channel->channel; u8 sample_rate = MCP3422_SAMPLE_RATE(config); u8 i; switch (mask) { case IIO_CHAN_INFO_SCALE: if (val1 != 0) return -EINVAL; for (i = 0; i < ARRAY_SIZE(mcp3422_scales[0]); i++) { if (val2 == mcp3422_scales[sample_rate][i]) { adc->pga[req_channel] = i; config &= ~MCP3422_CHANNEL_MASK; config |= MCP3422_CHANNEL_VALUE(req_channel); config &= ~MCP3422_PGA_MASK; config |= MCP3422_PGA_VALUE(adc->pga[req_channel]); return mcp3422_update_config(adc, config); } } return -EINVAL; case IIO_CHAN_INFO_SAMP_FREQ: switch (val1) { case 240: temp = MCP3422_SRATE_240; break; case 60: temp = MCP3422_SRATE_60; break; case 15: temp = MCP3422_SRATE_15; break; case 3: if (adc->id > 4) return -EINVAL; temp = MCP3422_SRATE_3; break; default: return -EINVAL; } config &= ~MCP3422_CHANNEL_MASK; config |= MCP3422_CHANNEL_VALUE(req_channel); config &= ~MCP3422_SRATE_MASK; config |= MCP3422_SAMPLE_RATE_VALUE(temp); return mcp3422_update_config(adc, config); default: break; } return -EINVAL; }