Commit 18bdc686 authored by Biju Das's avatar Biju Das Committed by Jonathan Cameron

iio: dac: mcp4725: Use i2c_get_match_data()

Add struct mcp4725_chip_info with chan_spec and chip_id variable.
After this simplify probe() by replacing device_get_match_data() and id
lookup for retrieving match data by i2c_get_match_data() by converting
enum->pointer for data in the match table.
Signed-off-by: default avatarBiju Das <biju.das.jz@bp.renesas.com>
Link: https://lore.kernel.org/r/20230903115548.59306-3-biju.das.jz@bp.renesas.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 002d546f
...@@ -30,6 +30,11 @@ ...@@ -30,6 +30,11 @@
#define MCP472X_REF_VREF_UNBUFFERED 0x02 #define MCP472X_REF_VREF_UNBUFFERED 0x02
#define MCP472X_REF_VREF_BUFFERED 0x03 #define MCP472X_REF_VREF_BUFFERED 0x03
struct mcp4725_chip_info {
const struct iio_chan_spec *chan_spec;
unsigned int chip_id;
};
struct mcp4725_data { struct mcp4725_data {
struct i2c_client *client; struct i2c_client *client;
unsigned ref_mode; unsigned ref_mode;
...@@ -383,10 +388,10 @@ static int mcp4725_probe_dt(struct device *dev, ...@@ -383,10 +388,10 @@ static int mcp4725_probe_dt(struct device *dev,
static int mcp4725_probe(struct i2c_client *client) static int mcp4725_probe(struct i2c_client *client)
{ {
const struct i2c_device_id *id = i2c_client_get_device_id(client); const struct i2c_device_id *id = i2c_client_get_device_id(client);
const struct mcp4725_chip_info *info;
struct mcp4725_data *data; struct mcp4725_data *data;
struct iio_dev *indio_dev; struct iio_dev *indio_dev;
struct mcp4725_platform_data *pdata, pdata_dt; struct mcp4725_platform_data *pdata, pdata_dt;
int chip_id;
u8 inbuf[4]; u8 inbuf[4];
u8 pd; u8 pd;
u8 ref; u8 ref;
...@@ -398,10 +403,7 @@ static int mcp4725_probe(struct i2c_client *client) ...@@ -398,10 +403,7 @@ static int mcp4725_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->client = client;
if (dev_fwnode(&client->dev)) info = i2c_get_match_data(client);
chip_id = (uintptr_t)device_get_match_data(&client->dev);
else
chip_id = id->driver_data;
pdata = dev_get_platdata(&client->dev); pdata = dev_get_platdata(&client->dev);
if (!pdata) { if (!pdata) {
...@@ -414,7 +416,7 @@ static int mcp4725_probe(struct i2c_client *client) ...@@ -414,7 +416,7 @@ static int mcp4725_probe(struct i2c_client *client)
pdata = &pdata_dt; pdata = &pdata_dt;
} }
if (chip_id == MCP4725 && pdata->use_vref) { if (info->chip_id == MCP4725 && pdata->use_vref) {
dev_err(&client->dev, dev_err(&client->dev,
"external reference is unavailable on MCP4725"); "external reference is unavailable on MCP4725");
return -EINVAL; return -EINVAL;
...@@ -455,12 +457,12 @@ static int mcp4725_probe(struct i2c_client *client) ...@@ -455,12 +457,12 @@ static int mcp4725_probe(struct i2c_client *client)
indio_dev->name = id->name; indio_dev->name = id->name;
indio_dev->info = &mcp4725_info; indio_dev->info = &mcp4725_info;
indio_dev->channels = &mcp472x_channel[chip_id]; indio_dev->channels = info->chan_spec;
indio_dev->num_channels = 1; indio_dev->num_channels = 1;
indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->modes = INDIO_DIRECT_MODE;
/* read current DAC value and settings */ /* read current DAC value and settings */
err = i2c_master_recv(client, inbuf, chip_id == MCP4725 ? 3 : 4); err = i2c_master_recv(client, inbuf, info->chip_id == MCP4725 ? 3 : 4);
if (err < 0) { if (err < 0) {
dev_err(&client->dev, "failed to read DAC value"); dev_err(&client->dev, "failed to read DAC value");
...@@ -470,10 +472,10 @@ static int mcp4725_probe(struct i2c_client *client) ...@@ -470,10 +472,10 @@ static int mcp4725_probe(struct i2c_client *client)
data->powerdown = pd > 0; data->powerdown = pd > 0;
data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */ data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */
data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4); data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
if (chip_id == MCP4726) if (info->chip_id == MCP4726)
ref = (inbuf[3] >> 3) & 0x3; ref = (inbuf[3] >> 3) & 0x3;
if (chip_id == MCP4726 && ref != data->ref_mode) { if (info->chip_id == MCP4726 && ref != data->ref_mode) {
dev_info(&client->dev, dev_info(&client->dev,
"voltage reference mode differs (conf: %u, eeprom: %u), setting %u", "voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
data->ref_mode, ref, data->ref_mode); data->ref_mode, ref, data->ref_mode);
...@@ -510,9 +512,19 @@ static void mcp4725_remove(struct i2c_client *client) ...@@ -510,9 +512,19 @@ static void mcp4725_remove(struct i2c_client *client)
regulator_disable(data->vdd_reg); regulator_disable(data->vdd_reg);
} }
static const struct mcp4725_chip_info mcp4725 = {
.chan_spec = &mcp472x_channel[MCP4725],
.chip_id = MCP4725,
};
static const struct mcp4725_chip_info mcp4726 = {
.chan_spec = &mcp472x_channel[MCP4726],
.chip_id = MCP4726,
};
static const struct i2c_device_id mcp4725_id[] = { static const struct i2c_device_id mcp4725_id[] = {
{ "mcp4725", MCP4725 }, { "mcp4725", (kernel_ulong_t)&mcp4725 },
{ "mcp4726", MCP4726 }, { "mcp4726", (kernel_ulong_t)&mcp4726 },
{ } { }
}; };
MODULE_DEVICE_TABLE(i2c, mcp4725_id); MODULE_DEVICE_TABLE(i2c, mcp4725_id);
...@@ -520,11 +532,11 @@ MODULE_DEVICE_TABLE(i2c, mcp4725_id); ...@@ -520,11 +532,11 @@ MODULE_DEVICE_TABLE(i2c, mcp4725_id);
static const struct of_device_id mcp4725_of_match[] = { static const struct of_device_id mcp4725_of_match[] = {
{ {
.compatible = "microchip,mcp4725", .compatible = "microchip,mcp4725",
.data = (void *)MCP4725 .data = &mcp4725
}, },
{ {
.compatible = "microchip,mcp4726", .compatible = "microchip,mcp4726",
.data = (void *)MCP4726 .data = &mcp4726
}, },
{ } { }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment