16731ca39SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0-only
26731ca39SAndy Shevchenko /*
36731ca39SAndy Shevchenko * STMicroelectronics LSM9DS0 IMU driver
46731ca39SAndy Shevchenko *
56731ca39SAndy Shevchenko * Copyright (C) 2021, Intel Corporation
66731ca39SAndy Shevchenko *
76731ca39SAndy Shevchenko * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
86731ca39SAndy Shevchenko */
96731ca39SAndy Shevchenko
1007d6a5a4Sandy.shevchenko@gmail.com #include <linux/device.h>
1107d6a5a4Sandy.shevchenko@gmail.com #include <linux/err.h>
1207d6a5a4Sandy.shevchenko@gmail.com #include <linux/gfp_types.h>
136731ca39SAndy Shevchenko #include <linux/i2c.h>
146731ca39SAndy Shevchenko #include <linux/module.h>
159c6cd755SJonathan Cameron #include <linux/mod_devicetable.h>
169c6cd755SJonathan Cameron #include <linux/regmap.h>
176731ca39SAndy Shevchenko
186731ca39SAndy Shevchenko #include <linux/iio/common/st_sensors_i2c.h>
196731ca39SAndy Shevchenko
206731ca39SAndy Shevchenko #include "st_lsm9ds0.h"
216731ca39SAndy Shevchenko
226731ca39SAndy Shevchenko static const struct of_device_id st_lsm9ds0_of_match[] = {
236731ca39SAndy Shevchenko {
24c0f68439SMarius Hoch .compatible = "st,lsm303d-imu",
25c0f68439SMarius Hoch .data = LSM303D_IMU_DEV_NAME,
26c0f68439SMarius Hoch },
27c0f68439SMarius Hoch {
286731ca39SAndy Shevchenko .compatible = "st,lsm9ds0-imu",
296731ca39SAndy Shevchenko .data = LSM9DS0_IMU_DEV_NAME,
306731ca39SAndy Shevchenko },
316731ca39SAndy Shevchenko {}
326731ca39SAndy Shevchenko };
336731ca39SAndy Shevchenko MODULE_DEVICE_TABLE(of, st_lsm9ds0_of_match);
346731ca39SAndy Shevchenko
356731ca39SAndy Shevchenko static const struct i2c_device_id st_lsm9ds0_id_table[] = {
36c0f68439SMarius Hoch { LSM303D_IMU_DEV_NAME },
376731ca39SAndy Shevchenko { LSM9DS0_IMU_DEV_NAME },
386731ca39SAndy Shevchenko {}
396731ca39SAndy Shevchenko };
406731ca39SAndy Shevchenko MODULE_DEVICE_TABLE(i2c, st_lsm9ds0_id_table);
416731ca39SAndy Shevchenko
42598e971fSMarius Hoch static const struct acpi_device_id st_lsm9ds0_acpi_match[] = {
43598e971fSMarius Hoch {"ACCL0001", (kernel_ulong_t)LSM303D_IMU_DEV_NAME},
44*8655d0e3Sandy.shevchenko@gmail.com {}
45598e971fSMarius Hoch };
46598e971fSMarius Hoch MODULE_DEVICE_TABLE(acpi, st_lsm9ds0_acpi_match);
47598e971fSMarius Hoch
486731ca39SAndy Shevchenko static const struct regmap_config st_lsm9ds0_regmap_config = {
496731ca39SAndy Shevchenko .reg_bits = 8,
506731ca39SAndy Shevchenko .val_bits = 8,
516731ca39SAndy Shevchenko .read_flag_mask = 0x80,
526731ca39SAndy Shevchenko };
536731ca39SAndy Shevchenko
st_lsm9ds0_i2c_probe(struct i2c_client * client)546731ca39SAndy Shevchenko static int st_lsm9ds0_i2c_probe(struct i2c_client *client)
556731ca39SAndy Shevchenko {
566731ca39SAndy Shevchenko const struct regmap_config *config = &st_lsm9ds0_regmap_config;
576731ca39SAndy Shevchenko struct device *dev = &client->dev;
586731ca39SAndy Shevchenko struct st_lsm9ds0 *lsm9ds0;
596731ca39SAndy Shevchenko struct regmap *regmap;
606731ca39SAndy Shevchenko
616731ca39SAndy Shevchenko st_sensors_dev_name_probe(dev, client->name, sizeof(client->name));
626731ca39SAndy Shevchenko
636731ca39SAndy Shevchenko lsm9ds0 = devm_kzalloc(dev, sizeof(*lsm9ds0), GFP_KERNEL);
646731ca39SAndy Shevchenko if (!lsm9ds0)
656731ca39SAndy Shevchenko return -ENOMEM;
666731ca39SAndy Shevchenko
676731ca39SAndy Shevchenko lsm9ds0->dev = dev;
686731ca39SAndy Shevchenko lsm9ds0->name = client->name;
696731ca39SAndy Shevchenko lsm9ds0->irq = client->irq;
706731ca39SAndy Shevchenko
716731ca39SAndy Shevchenko regmap = devm_regmap_init_i2c(client, config);
726731ca39SAndy Shevchenko if (IS_ERR(regmap))
736731ca39SAndy Shevchenko return PTR_ERR(regmap);
746731ca39SAndy Shevchenko
756731ca39SAndy Shevchenko i2c_set_clientdata(client, lsm9ds0);
766731ca39SAndy Shevchenko
776731ca39SAndy Shevchenko return st_lsm9ds0_probe(lsm9ds0, regmap);
786731ca39SAndy Shevchenko }
796731ca39SAndy Shevchenko
806731ca39SAndy Shevchenko static struct i2c_driver st_lsm9ds0_driver = {
816731ca39SAndy Shevchenko .driver = {
826731ca39SAndy Shevchenko .name = "st-lsm9ds0-i2c",
836731ca39SAndy Shevchenko .of_match_table = st_lsm9ds0_of_match,
84598e971fSMarius Hoch .acpi_match_table = st_lsm9ds0_acpi_match,
856731ca39SAndy Shevchenko },
867cf15f42SUwe Kleine-König .probe = st_lsm9ds0_i2c_probe,
876731ca39SAndy Shevchenko .id_table = st_lsm9ds0_id_table,
886731ca39SAndy Shevchenko };
896731ca39SAndy Shevchenko module_i2c_driver(st_lsm9ds0_driver);
906731ca39SAndy Shevchenko
916731ca39SAndy Shevchenko MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
926731ca39SAndy Shevchenko MODULE_DESCRIPTION("STMicroelectronics LSM9DS0 IMU I2C driver");
936731ca39SAndy Shevchenko MODULE_LICENSE("GPL v2");
940805b512SJonathan Cameron MODULE_IMPORT_NS(IIO_ST_SENSORS);
95