xref: /linux/drivers/iio/imu/inv_icm42600/inv_icm42600_spi.c (revision a634dda26186cf9a51567020fcce52bcba5e1e59)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2020 InvenSense, Inc.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/spi/spi.h>
11 #include <linux/regmap.h>
12 #include <linux/property.h>
13 
14 #include "inv_icm42600.h"
15 
16 static int inv_icm42600_spi_bus_setup(struct inv_icm42600_state *st)
17 {
18 	unsigned int mask, val;
19 	int ret;
20 
21 	/* setup interface registers */
22 	val = INV_ICM42600_INTF_CONFIG6_I3C_EN |
23 	      INV_ICM42600_INTF_CONFIG6_I3C_SDR_EN |
24 	      INV_ICM42600_INTF_CONFIG6_I3C_DDR_EN;
25 	ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG6,
26 				 INV_ICM42600_INTF_CONFIG6_MASK, val);
27 	if (ret)
28 		return ret;
29 
30 	ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INTF_CONFIG4,
31 				INV_ICM42600_INTF_CONFIG4_I3C_BUS_ONLY);
32 	if (ret)
33 		return ret;
34 
35 	/* set slew rates for I2C and SPI */
36 	mask = INV_ICM42600_DRIVE_CONFIG_I2C_MASK |
37 	       INV_ICM42600_DRIVE_CONFIG_SPI_MASK;
38 	val = INV_ICM42600_DRIVE_CONFIG_I2C(INV_ICM42600_SLEW_RATE_20_60NS) |
39 	      INV_ICM42600_DRIVE_CONFIG_SPI(INV_ICM42600_SLEW_RATE_INF_2NS);
40 	ret = regmap_update_bits(st->map, INV_ICM42600_REG_DRIVE_CONFIG,
41 				 mask, val);
42 	if (ret)
43 		return ret;
44 
45 	/* disable i2c bus */
46 	return regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
47 				  INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK,
48 				  INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_I2C_DIS);
49 }
50 
51 static int inv_icm42600_probe(struct spi_device *spi)
52 {
53 	const void *match;
54 	enum inv_icm42600_chip chip;
55 	struct regmap *regmap;
56 
57 	match = device_get_match_data(&spi->dev);
58 	if (!match)
59 		return -EINVAL;
60 	chip = (uintptr_t)match;
61 
62 	/* use SPI specific regmap */
63 	regmap = devm_regmap_init_spi(spi, &inv_icm42600_spi_regmap_config);
64 	if (IS_ERR(regmap))
65 		return PTR_ERR(regmap);
66 
67 	return inv_icm42600_core_probe(regmap, chip, spi->irq,
68 				       inv_icm42600_spi_bus_setup);
69 }
70 
71 /*
72  * device id table is used to identify what device can be
73  * supported by this driver
74  */
75 static const struct spi_device_id inv_icm42600_id[] = {
76 	{ "icm42600", INV_CHIP_ICM42600 },
77 	{ "icm42602", INV_CHIP_ICM42602 },
78 	{ "icm42605", INV_CHIP_ICM42605 },
79 	{ "icm42686", INV_CHIP_ICM42686 },
80 	{ "icm42622", INV_CHIP_ICM42622 },
81 	{ "icm42688", INV_CHIP_ICM42688 },
82 	{ "icm42631", INV_CHIP_ICM42631 },
83 	{ }
84 };
85 MODULE_DEVICE_TABLE(spi, inv_icm42600_id);
86 
87 static const struct of_device_id inv_icm42600_of_matches[] = {
88 	{
89 		.compatible = "invensense,icm42600",
90 		.data = (void *)INV_CHIP_ICM42600,
91 	}, {
92 		.compatible = "invensense,icm42602",
93 		.data = (void *)INV_CHIP_ICM42602,
94 	}, {
95 		.compatible = "invensense,icm42605",
96 		.data = (void *)INV_CHIP_ICM42605,
97 	}, {
98 		.compatible = "invensense,icm42686",
99 		.data = (void *)INV_CHIP_ICM42686,
100 	}, {
101 		.compatible = "invensense,icm42622",
102 		.data = (void *)INV_CHIP_ICM42622,
103 	}, {
104 		.compatible = "invensense,icm42688",
105 		.data = (void *)INV_CHIP_ICM42688,
106 	}, {
107 		.compatible = "invensense,icm42631",
108 		.data = (void *)INV_CHIP_ICM42631,
109 	},
110 	{}
111 };
112 MODULE_DEVICE_TABLE(of, inv_icm42600_of_matches);
113 
114 static struct spi_driver inv_icm42600_driver = {
115 	.driver = {
116 		.name = "inv-icm42600-spi",
117 		.of_match_table = inv_icm42600_of_matches,
118 		.pm = pm_ptr(&inv_icm42600_pm_ops),
119 	},
120 	.id_table = inv_icm42600_id,
121 	.probe = inv_icm42600_probe,
122 };
123 module_spi_driver(inv_icm42600_driver);
124 
125 MODULE_AUTHOR("InvenSense, Inc.");
126 MODULE_DESCRIPTION("InvenSense ICM-426xx SPI driver");
127 MODULE_LICENSE("GPL");
128 MODULE_IMPORT_NS("IIO_ICM42600");
129