1 /*
2 * Copyright (C) 2015 Intel Corporation Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 */
13 #include <linux/module.h>
14 #include <linux/acpi.h>
15 #include <linux/spi/spi.h>
16 #include <linux/regmap.h>
17 #include <linux/iio/iio.h>
18 #include "inv_mpu_iio.h"
19 
20 static const struct regmap_config inv_mpu_regmap_config = {
21 	.reg_bits = 8,
22 	.val_bits = 8,
23 };
24 
25 static int inv_mpu_i2c_disable(struct iio_dev *indio_dev)
26 {
27 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
28 	int ret = 0;
29 
30 	ret = inv_mpu6050_set_power_itg(st, true);
31 	if (ret)
32 		return ret;
33 
34 	if (st->reg->i2c_if) {
35 		ret = regmap_write(st->map, st->reg->i2c_if,
36 				   INV_ICM20602_BIT_I2C_IF_DIS);
37 	} else {
38 		st->chip_config.user_ctrl |= INV_MPU6050_BIT_I2C_IF_DIS;
39 		ret = regmap_write(st->map, st->reg->user_ctrl,
40 				   st->chip_config.user_ctrl);
41 	}
42 	if (ret) {
43 		inv_mpu6050_set_power_itg(st, false);
44 		return ret;
45 	}
46 
47 	return inv_mpu6050_set_power_itg(st, false);
48 }
49 
50 static int inv_mpu_probe(struct spi_device *spi)
51 {
52 	struct regmap *regmap;
53 	const struct spi_device_id *spi_id;
54 	const struct acpi_device_id *acpi_id;
55 	const char *name = NULL;
56 	enum inv_devices chip_type;
57 
58 	if ((spi_id = spi_get_device_id(spi))) {
59 		chip_type = (enum inv_devices)spi_id->driver_data;
60 		name = spi_id->name;
61 	} else if ((acpi_id = acpi_match_device(spi->dev.driver->acpi_match_table, &spi->dev))) {
62 		chip_type = (enum inv_devices)acpi_id->driver_data;
63 	} else {
64 		return -ENODEV;
65 	}
66 
67 	regmap = devm_regmap_init_spi(spi, &inv_mpu_regmap_config);
68 	if (IS_ERR(regmap)) {
69 		dev_err(&spi->dev, "Failed to register spi regmap %d\n",
70 			(int)PTR_ERR(regmap));
71 		return PTR_ERR(regmap);
72 	}
73 
74 	return inv_mpu_core_probe(regmap, spi->irq, name,
75 				  inv_mpu_i2c_disable, chip_type);
76 }
77 
78 /*
79  * device id table is used to identify what device can be
80  * supported by this driver
81  */
82 static const struct spi_device_id inv_mpu_id[] = {
83 	{"mpu6000", INV_MPU6000},
84 	{"mpu6500", INV_MPU6500},
85 	{"mpu9150", INV_MPU9150},
86 	{"mpu9250", INV_MPU9250},
87 	{"mpu9255", INV_MPU9255},
88 	{"icm20608", INV_ICM20608},
89 	{"icm20602", INV_ICM20602},
90 	{}
91 };
92 
93 MODULE_DEVICE_TABLE(spi, inv_mpu_id);
94 
95 static const struct acpi_device_id inv_acpi_match[] = {
96 	{"INVN6000", INV_MPU6000},
97 	{ },
98 };
99 MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
100 
101 static struct spi_driver inv_mpu_driver = {
102 	.probe		=	inv_mpu_probe,
103 	.id_table	=	inv_mpu_id,
104 	.driver = {
105 		.acpi_match_table = ACPI_PTR(inv_acpi_match),
106 		.name	=	"inv-mpu6000-spi",
107 		.pm     =       &inv_mpu_pmops,
108 	},
109 };
110 
111 module_spi_driver(inv_mpu_driver);
112 
113 MODULE_AUTHOR("Adriana Reus <adriana.reus@intel.com>");
114 MODULE_DESCRIPTION("Invensense device MPU6000 driver");
115 MODULE_LICENSE("GPL");
116