1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics LSM9DS0 IMU driver
4  *
5  * Copyright (C) 2021, Intel Corporation
6  *
7  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14 
15 #include <linux/iio/common/st_sensors_spi.h>
16 
17 #include "st_lsm9ds0.h"
18 
19 static const struct of_device_id st_lsm9ds0_of_match[] = {
20 	{
21 		.compatible = "st,lsm9ds0-imu",
22 		.data = LSM9DS0_IMU_DEV_NAME,
23 	},
24 	{}
25 };
26 MODULE_DEVICE_TABLE(of, st_lsm9ds0_of_match);
27 
28 static const struct spi_device_id st_lsm9ds0_id_table[] = {
29 	{ LSM9DS0_IMU_DEV_NAME },
30 	{}
31 };
32 MODULE_DEVICE_TABLE(spi, st_lsm9ds0_id_table);
33 
34 static const struct regmap_config st_lsm9ds0_regmap_config = {
35 	.reg_bits	= 8,
36 	.val_bits	= 8,
37 	.read_flag_mask	= 0xc0,
38 };
39 
40 static int st_lsm9ds0_spi_probe(struct spi_device *spi)
41 {
42 	struct device *dev = &spi->dev;
43 	struct st_lsm9ds0 *lsm9ds0;
44 	struct regmap *regmap;
45 
46 	st_sensors_dev_name_probe(dev, spi->modalias, sizeof(spi->modalias));
47 
48 	lsm9ds0 = devm_kzalloc(dev, sizeof(*lsm9ds0), GFP_KERNEL);
49 	if (!lsm9ds0)
50 		return -ENOMEM;
51 
52 	lsm9ds0->dev = dev;
53 	lsm9ds0->name = spi->modalias;
54 	lsm9ds0->irq = spi->irq;
55 
56 	regmap = devm_regmap_init_spi(spi, &st_lsm9ds0_regmap_config);
57 	if (IS_ERR(regmap))
58 		return PTR_ERR(regmap);
59 
60 	spi_set_drvdata(spi, lsm9ds0);
61 
62 	return st_lsm9ds0_probe(lsm9ds0, regmap);
63 }
64 
65 static int st_lsm9ds0_spi_remove(struct spi_device *spi)
66 {
67 	return st_lsm9ds0_remove(spi_get_drvdata(spi));
68 }
69 
70 static struct spi_driver st_lsm9ds0_driver = {
71 	.driver = {
72 		.name = "st-lsm9ds0-spi",
73 		.of_match_table = st_lsm9ds0_of_match,
74 	},
75 	.probe = st_lsm9ds0_spi_probe,
76 	.remove = st_lsm9ds0_spi_remove,
77 	.id_table = st_lsm9ds0_id_table,
78 };
79 module_spi_driver(st_lsm9ds0_driver);
80 
81 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
82 MODULE_DESCRIPTION("STMicroelectronics LSM9DS0 IMU SPI driver");
83 MODULE_LICENSE("GPL v2");
84