Commit e7123a4d authored by Mehdi Djait's avatar Mehdi Djait Committed by Jonathan Cameron

iio: accel: kionix-kx022a: Refactor driver and add chip_info structure

Add the chip_info structure to the driver's private data to hold all
the device specific infos.
Refactor the kx022a driver implementation to make it more generic and
extensible.
Acked-by: default avatarMatti Vaittinen <mazziesaccount@gmail.com>
Signed-off-by: default avatarMehdi Djait <mehdi.djait.k@gmail.com>
Link: https://lore.kernel.org/r/7a31d0cdefba15d7c791252ec8bc5db553b3996b.1694867379.git.mehdi.djait.k@gmail.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 13d5398a
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
static int kx022a_i2c_probe(struct i2c_client *i2c) static int kx022a_i2c_probe(struct i2c_client *i2c)
{ {
struct device *dev = &i2c->dev; struct device *dev = &i2c->dev;
const struct kx022a_chip_info *chip_info;
struct regmap *regmap; struct regmap *regmap;
if (!i2c->irq) { if (!i2c->irq) {
...@@ -22,22 +23,26 @@ static int kx022a_i2c_probe(struct i2c_client *i2c) ...@@ -22,22 +23,26 @@ static int kx022a_i2c_probe(struct i2c_client *i2c)
return -EINVAL; return -EINVAL;
} }
regmap = devm_regmap_init_i2c(i2c, &kx022a_regmap); chip_info = i2c_get_match_data(i2c);
if (!chip_info)
return -EINVAL;
regmap = devm_regmap_init_i2c(i2c, chip_info->regmap_config);
if (IS_ERR(regmap)) if (IS_ERR(regmap))
return dev_err_probe(dev, PTR_ERR(regmap), return dev_err_probe(dev, PTR_ERR(regmap),
"Failed to initialize Regmap\n"); "Failed to initialize Regmap\n");
return kx022a_probe_internal(dev); return kx022a_probe_internal(dev, chip_info);
} }
static const struct i2c_device_id kx022a_i2c_id[] = { static const struct i2c_device_id kx022a_i2c_id[] = {
{ .name = "kx022a" }, { .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info },
{ } { }
}; };
MODULE_DEVICE_TABLE(i2c, kx022a_i2c_id); MODULE_DEVICE_TABLE(i2c, kx022a_i2c_id);
static const struct of_device_id kx022a_of_match[] = { static const struct of_device_id kx022a_of_match[] = {
{ .compatible = "kionix,kx022a", }, { .compatible = "kionix,kx022a", .data = &kx022a_chip_info },
{ } { }
}; };
MODULE_DEVICE_TABLE(of, kx022a_of_match); MODULE_DEVICE_TABLE(of, kx022a_of_match);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
static int kx022a_spi_probe(struct spi_device *spi) static int kx022a_spi_probe(struct spi_device *spi)
{ {
struct device *dev = &spi->dev; struct device *dev = &spi->dev;
const struct kx022a_chip_info *chip_info;
struct regmap *regmap; struct regmap *regmap;
if (!spi->irq) { if (!spi->irq) {
...@@ -22,22 +23,26 @@ static int kx022a_spi_probe(struct spi_device *spi) ...@@ -22,22 +23,26 @@ static int kx022a_spi_probe(struct spi_device *spi)
return -EINVAL; return -EINVAL;
} }
regmap = devm_regmap_init_spi(spi, &kx022a_regmap); chip_info = spi_get_device_match_data(spi);
if (!chip_info)
return -EINVAL;
regmap = devm_regmap_init_spi(spi, chip_info->regmap_config);
if (IS_ERR(regmap)) if (IS_ERR(regmap))
return dev_err_probe(dev, PTR_ERR(regmap), return dev_err_probe(dev, PTR_ERR(regmap),
"Failed to initialize Regmap\n"); "Failed to initialize Regmap\n");
return kx022a_probe_internal(dev); return kx022a_probe_internal(dev, chip_info);
} }
static const struct spi_device_id kx022a_id[] = { static const struct spi_device_id kx022a_id[] = {
{ "kx022a" }, { .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info },
{ } { }
}; };
MODULE_DEVICE_TABLE(spi, kx022a_id); MODULE_DEVICE_TABLE(spi, kx022a_id);
static const struct of_device_id kx022a_of_match[] = { static const struct of_device_id kx022a_of_match[] = {
{ .compatible = "kionix,kx022a", }, { .compatible = "kionix,kx022a", .data = &kx022a_chip_info },
{ } { }
}; };
MODULE_DEVICE_TABLE(of, kx022a_of_match); MODULE_DEVICE_TABLE(of, kx022a_of_match);
......
This diff is collapsed.
...@@ -76,7 +76,55 @@ ...@@ -76,7 +76,55 @@
struct device; struct device;
int kx022a_probe_internal(struct device *dev); /**
extern const struct regmap_config kx022a_regmap; * struct kx022a_chip_info - Kionix accelerometer chip specific information
*
* @name: name of the device
* @regmap_config: pointer to register map configuration
* @channels: pointer to iio_chan_spec array
* @num_channels: number of iio_chan_spec channels
* @fifo_length: number of 16-bit samples in a full buffer
* @who: WHO_AM_I register
* @id: WHO_AM_I register value
* @cntl: control register 1
* @cntl2: control register 2
* @odcntl: output data control register
* @buf_cntl1: buffer control register 1
* @buf_cntl2: buffer control register 2
* @buf_clear: buffer clear register
* @buf_status1: buffer status register 1
* @buf_read: buffer read register
* @inc1: interrupt control register 1
* @inc4: interrupt control register 4
* @inc5: interrupt control register 5
* @inc6: interrupt control register 6
* @xout_l: x-axis output least significant byte
*/
struct kx022a_chip_info {
const char *name;
const struct regmap_config *regmap_config;
const struct iio_chan_spec *channels;
unsigned int num_channels;
unsigned int fifo_length;
u8 who;
u8 id;
u8 cntl;
u8 cntl2;
u8 odcntl;
u8 buf_cntl1;
u8 buf_cntl2;
u8 buf_clear;
u8 buf_status1;
u8 buf_read;
u8 inc1;
u8 inc4;
u8 inc5;
u8 inc6;
u8 xout_l;
};
int kx022a_probe_internal(struct device *dev, const struct kx022a_chip_info *chip_info);
extern const struct kx022a_chip_info kx022a_chip_info;
#endif #endif
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