mirror of
https://github.com/torvalds/linux.git
synced 2026-07-30 19:21:28 +02:00
iio: light: al3320a: Implement regmap support
Modernize and clean up the driver using the regmap framework. With the regmap implementation, the compiler produces a significantly smaller module. Size before: 72 kB Size after: 58 kB Signed-off-by: David Heidelberg <david@ixit.cz> Link: https://patch.msgid.link/20250402-al3010-iio-regmap-v4-5-d189bea87261@ixit.cz Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
parent
0e5e21e23d
commit
1850e6ae7f
|
|
@ -15,6 +15,7 @@
|
||||||
#include <linux/bitfield.h>
|
#include <linux/bitfield.h>
|
||||||
#include <linux/i2c.h>
|
#include <linux/i2c.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
|
#include <linux/regmap.h>
|
||||||
#include <linux/mod_devicetable.h>
|
#include <linux/mod_devicetable.h>
|
||||||
|
|
||||||
#include <linux/iio/iio.h>
|
#include <linux/iio/iio.h>
|
||||||
|
|
@ -57,8 +58,14 @@ static const int al3320a_scales[][2] = {
|
||||||
{0, 512000}, {0, 128000}, {0, 32000}, {0, 10000}
|
{0, 512000}, {0, 128000}, {0, 32000}, {0, 10000}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static const struct regmap_config al3320a_regmap_config = {
|
||||||
|
.reg_bits = 8,
|
||||||
|
.val_bits = 8,
|
||||||
|
.max_register = AL3320A_REG_HIGH_THRESH_HIGH,
|
||||||
|
};
|
||||||
|
|
||||||
struct al3320a_data {
|
struct al3320a_data {
|
||||||
struct i2c_client *client;
|
struct regmap *regmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct iio_chan_spec al3320a_channels[] = {
|
static const struct iio_chan_spec al3320a_channels[] = {
|
||||||
|
|
@ -80,50 +87,47 @@ static const struct attribute_group al3320a_attribute_group = {
|
||||||
.attrs = al3320a_attributes,
|
.attrs = al3320a_attributes,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int al3320a_set_pwr_on(struct i2c_client *client)
|
static int al3320a_set_pwr_on(struct al3320a_data *data)
|
||||||
{
|
{
|
||||||
return i2c_smbus_write_byte_data(client, AL3320A_REG_CONFIG, AL3320A_CONFIG_ENABLE);
|
return regmap_write(data->regmap, AL3320A_REG_CONFIG, AL3320A_CONFIG_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void al3320a_set_pwr_off(void *_data)
|
static void al3320a_set_pwr_off(void *_data)
|
||||||
{
|
{
|
||||||
struct al3320a_data *data = _data;
|
struct al3320a_data *data = _data;
|
||||||
|
struct device *dev = regmap_get_device(data->regmap);
|
||||||
|
int ret;
|
||||||
|
|
||||||
i2c_smbus_write_byte_data(data->client, AL3320A_REG_CONFIG, AL3320A_CONFIG_DISABLE);
|
ret = regmap_write(data->regmap, AL3320A_REG_CONFIG, AL3320A_CONFIG_DISABLE);
|
||||||
|
if (ret)
|
||||||
|
dev_err(dev, "failed to write system register\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int al3320a_init(struct al3320a_data *data)
|
static int al3320a_init(struct al3320a_data *data)
|
||||||
{
|
{
|
||||||
|
struct device *dev = regmap_get_device(data->regmap);
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = al3320a_set_pwr_on(data->client);
|
ret = al3320a_set_pwr_on(data);
|
||||||
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = devm_add_action_or_reset(&data->client->dev,
|
|
||||||
al3320a_set_pwr_off,
|
|
||||||
data);
|
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_CONFIG_RANGE,
|
ret = devm_add_action_or_reset(dev, al3320a_set_pwr_off, data);
|
||||||
FIELD_PREP(AL3320A_GAIN_MASK,
|
if (ret)
|
||||||
AL3320A_RANGE_3));
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_MEAN_TIME,
|
ret = regmap_write(data->regmap, AL3320A_REG_CONFIG_RANGE,
|
||||||
AL3320A_DEFAULT_MEAN_TIME);
|
FIELD_PREP(AL3320A_GAIN_MASK, AL3320A_RANGE_3));
|
||||||
if (ret < 0)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_WAIT,
|
ret = regmap_write(data->regmap, AL3320A_REG_MEAN_TIME,
|
||||||
AL3320A_DEFAULT_WAIT_TIME);
|
AL3320A_DEFAULT_MEAN_TIME);
|
||||||
if (ret < 0)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
return 0;
|
return regmap_write(data->regmap, AL3320A_REG_WAIT,
|
||||||
|
AL3320A_DEFAULT_WAIT_TIME);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int al3320a_read_raw(struct iio_dev *indio_dev,
|
static int al3320a_read_raw(struct iio_dev *indio_dev,
|
||||||
|
|
@ -131,7 +135,7 @@ static int al3320a_read_raw(struct iio_dev *indio_dev,
|
||||||
int *val2, long mask)
|
int *val2, long mask)
|
||||||
{
|
{
|
||||||
struct al3320a_data *data = iio_priv(indio_dev);
|
struct al3320a_data *data = iio_priv(indio_dev);
|
||||||
int ret;
|
int ret, gain, raw;
|
||||||
|
|
||||||
switch (mask) {
|
switch (mask) {
|
||||||
case IIO_CHAN_INFO_RAW:
|
case IIO_CHAN_INFO_RAW:
|
||||||
|
|
@ -140,21 +144,21 @@ static int al3320a_read_raw(struct iio_dev *indio_dev,
|
||||||
* - low byte of output is stored at AL3320A_REG_DATA_LOW
|
* - low byte of output is stored at AL3320A_REG_DATA_LOW
|
||||||
* - high byte of output is stored at AL3320A_REG_DATA_LOW + 1
|
* - high byte of output is stored at AL3320A_REG_DATA_LOW + 1
|
||||||
*/
|
*/
|
||||||
ret = i2c_smbus_read_word_data(data->client,
|
ret = regmap_read(data->regmap, AL3320A_REG_DATA_LOW, &raw);
|
||||||
AL3320A_REG_DATA_LOW);
|
if (ret)
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
*val = ret;
|
|
||||||
return IIO_VAL_INT;
|
|
||||||
case IIO_CHAN_INFO_SCALE:
|
|
||||||
ret = i2c_smbus_read_byte_data(data->client,
|
|
||||||
AL3320A_REG_CONFIG_RANGE);
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
ret = FIELD_GET(AL3320A_GAIN_MASK, ret);
|
*val = raw;
|
||||||
*val = al3320a_scales[ret][0];
|
|
||||||
*val2 = al3320a_scales[ret][1];
|
return IIO_VAL_INT;
|
||||||
|
case IIO_CHAN_INFO_SCALE:
|
||||||
|
ret = regmap_read(data->regmap, AL3320A_REG_CONFIG_RANGE, &gain);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
gain = FIELD_GET(AL3320A_GAIN_MASK, gain);
|
||||||
|
*val = al3320a_scales[gain][0];
|
||||||
|
*val2 = al3320a_scales[gain][1];
|
||||||
|
|
||||||
return IIO_VAL_INT_PLUS_MICRO;
|
return IIO_VAL_INT_PLUS_MICRO;
|
||||||
}
|
}
|
||||||
|
|
@ -175,9 +179,8 @@ static int al3320a_write_raw(struct iio_dev *indio_dev,
|
||||||
val2 != al3320a_scales[i][1])
|
val2 != al3320a_scales[i][1])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
return i2c_smbus_write_byte_data(data->client,
|
return regmap_write(data->regmap, AL3320A_REG_CONFIG_RANGE,
|
||||||
AL3320A_REG_CONFIG_RANGE,
|
FIELD_PREP(AL3320A_GAIN_MASK, i));
|
||||||
FIELD_PREP(AL3320A_GAIN_MASK, i));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -203,7 +206,11 @@ static int al3320a_probe(struct i2c_client *client)
|
||||||
|
|
||||||
data = iio_priv(indio_dev);
|
data = iio_priv(indio_dev);
|
||||||
i2c_set_clientdata(client, indio_dev);
|
i2c_set_clientdata(client, indio_dev);
|
||||||
data->client = client;
|
|
||||||
|
data->regmap = devm_regmap_init_i2c(client, &al3320a_regmap_config);
|
||||||
|
if (IS_ERR(data->regmap))
|
||||||
|
return dev_err_probe(dev, PTR_ERR(data->regmap),
|
||||||
|
"cannot allocate regmap\n");
|
||||||
|
|
||||||
indio_dev->info = &al3320a_info;
|
indio_dev->info = &al3320a_info;
|
||||||
indio_dev->name = "al3320a";
|
indio_dev->name = "al3320a";
|
||||||
|
|
@ -230,7 +237,9 @@ static int al3320a_suspend(struct device *dev)
|
||||||
|
|
||||||
static int al3320a_resume(struct device *dev)
|
static int al3320a_resume(struct device *dev)
|
||||||
{
|
{
|
||||||
return al3320a_set_pwr_on(to_i2c_client(dev));
|
struct al3320a_data *data = iio_priv(dev_get_drvdata(dev));
|
||||||
|
|
||||||
|
return al3320a_set_pwr_on(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static DEFINE_SIMPLE_DEV_PM_OPS(al3320a_pm_ops, al3320a_suspend,
|
static DEFINE_SIMPLE_DEV_PM_OPS(al3320a_pm_ops, al3320a_suspend,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user