xref: /linux/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2015 Intel Corporation Inc.
4 */
5 #include <linux/module.h>
6 #include <linux/acpi.h>
7 #include <linux/of.h>
8 #include <linux/property.h>
9 #include <linux/spi/spi.h>
10 #include <linux/regmap.h>
11 #include <linux/iio/iio.h>
12 #include "inv_mpu_iio.h"
13 
14 static const struct regmap_config inv_mpu_regmap_config = {
15 	.reg_bits = 8,
16 	.val_bits = 8,
17 };
18 
19 static int inv_mpu_i2c_disable(struct iio_dev *indio_dev)
20 {
21 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
22 	int ret = 0;
23 
24 	if (st->reg->i2c_if) {
25 		ret = regmap_write(st->map, st->reg->i2c_if,
26 				   INV_ICM20602_BIT_I2C_IF_DIS);
27 	} else {
28 		st->chip_config.user_ctrl |= INV_MPU6050_BIT_I2C_IF_DIS;
29 		ret = regmap_write(st->map, st->reg->user_ctrl,
30 				   st->chip_config.user_ctrl);
31 	}
32 
33 	return ret;
34 }
35 
36 static int inv_mpu_probe(struct spi_device *spi)
37 {
38 	const void *match;
39 	struct regmap *regmap;
40 	const struct spi_device_id *spi_id;
41 	const char *name = NULL;
42 	enum inv_devices chip_type;
43 
44 	if ((spi_id = spi_get_device_id(spi))) {
45 		chip_type = (enum inv_devices)spi_id->driver_data;
46 		name = spi_id->name;
47 	} else if ((match = device_get_match_data(&spi->dev))) {
48 		chip_type = (enum inv_devices)match;
49 		name = dev_name(&spi->dev);
50 	} else {
51 		return -ENODEV;
52 	}
53 
54 	regmap = devm_regmap_init_spi(spi, &inv_mpu_regmap_config);
55 	if (IS_ERR(regmap)) {
56 		dev_err(&spi->dev, "Failed to register spi regmap: %pe\n",
57 			regmap);
58 		return PTR_ERR(regmap);
59 	}
60 
61 	return inv_mpu_core_probe(regmap, spi->irq, name,
62 				  inv_mpu_i2c_disable, chip_type);
63 }
64 
65 /*
66  * device id table is used to identify what device can be
67  * supported by this driver
68  */
69 static const struct spi_device_id inv_mpu_id[] = {
70 	{"mpu6000", INV_MPU6000},
71 	{"mpu6500", INV_MPU6500},
72 	{"mpu6515", INV_MPU6515},
73 	{"mpu9250", INV_MPU9250},
74 	{"mpu9255", INV_MPU9255},
75 	{"icm20608", INV_ICM20608},
76 	{"icm20609", INV_ICM20609},
77 	{"icm20689", INV_ICM20689},
78 	{"icm20602", INV_ICM20602},
79 	{"icm20690", INV_ICM20690},
80 	{"iam20680", INV_IAM20680},
81 	{}
82 };
83 
84 MODULE_DEVICE_TABLE(spi, inv_mpu_id);
85 
86 static const struct of_device_id inv_of_match[] = {
87 	{
88 		.compatible = "invensense,mpu6000",
89 		.data = (void *)INV_MPU6000
90 	},
91 	{
92 		.compatible = "invensense,mpu6500",
93 		.data = (void *)INV_MPU6500
94 	},
95 	{
96 		.compatible = "invensense,mpu6515",
97 		.data = (void *)INV_MPU6515
98 	},
99 	{
100 		.compatible = "invensense,mpu9250",
101 		.data = (void *)INV_MPU9250
102 	},
103 	{
104 		.compatible = "invensense,mpu9255",
105 		.data = (void *)INV_MPU9255
106 	},
107 	{
108 		.compatible = "invensense,icm20608",
109 		.data = (void *)INV_ICM20608
110 	},
111 	{
112 		.compatible = "invensense,icm20609",
113 		.data = (void *)INV_ICM20609
114 	},
115 	{
116 		.compatible = "invensense,icm20689",
117 		.data = (void *)INV_ICM20689
118 	},
119 	{
120 		.compatible = "invensense,icm20602",
121 		.data = (void *)INV_ICM20602
122 	},
123 	{
124 		.compatible = "invensense,icm20690",
125 		.data = (void *)INV_ICM20690
126 	},
127 	{
128 		.compatible = "invensense,iam20680",
129 		.data = (void *)INV_IAM20680
130 	},
131 	{ }
132 };
133 MODULE_DEVICE_TABLE(of, inv_of_match);
134 
135 static const struct acpi_device_id inv_acpi_match[] = {
136 	{"INVN6000", INV_MPU6000},
137 	{ },
138 };
139 MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
140 
141 static struct spi_driver inv_mpu_driver = {
142 	.probe		=	inv_mpu_probe,
143 	.id_table	=	inv_mpu_id,
144 	.driver = {
145 		.of_match_table = inv_of_match,
146 		.acpi_match_table = ACPI_PTR(inv_acpi_match),
147 		.name	=	"inv-mpu6000-spi",
148 		.pm     =       &inv_mpu_pmops,
149 	},
150 };
151 
152 module_spi_driver(inv_mpu_driver);
153 
154 MODULE_AUTHOR("Adriana Reus <adriana.reus@intel.com>");
155 MODULE_DESCRIPTION("Invensense device MPU6000 driver");
156 MODULE_LICENSE("GPL");
157