1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ADXL345 3-Axis Digital Accelerometer SPI driver 4 * 5 * Copyright (c) 2017 Eva Rachel Retuya <eraretuya@gmail.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/regmap.h> 10 #include <linux/spi/spi.h> 11 12 #include "adxl345.h" 13 14 #define ADXL345_MAX_SPI_FREQ_HZ 5000000 15 #define ADXL345_MAX_FREQ_NO_FIFO_DELAY 1500000 16 17 static const struct regmap_config adxl345_spi_regmap_config = { 18 .reg_bits = 8, 19 .val_bits = 8, 20 /* Setting bits 7 and 6 enables multiple-byte read */ 21 .read_flag_mask = BIT(7) | BIT(6), 22 }; 23 24 static int adxl345_spi_setup(struct device *dev, struct regmap *regmap) 25 { 26 return regmap_write(regmap, ADXL345_REG_DATA_FORMAT, ADXL345_DATA_FORMAT_SPI_3WIRE); 27 } 28 29 static int adxl345_spi_probe(struct spi_device *spi) 30 { 31 struct regmap *regmap; 32 bool needs_delay; 33 34 /* Bail out if max_speed_hz exceeds 5 MHz */ 35 if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ) 36 return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n", 37 spi->max_speed_hz); 38 39 regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config); 40 if (IS_ERR(regmap)) 41 return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n"); 42 43 needs_delay = spi->max_speed_hz > ADXL345_MAX_FREQ_NO_FIFO_DELAY; 44 if (spi->mode & SPI_3WIRE) 45 return adxl345_core_probe(&spi->dev, regmap, needs_delay, adxl345_spi_setup); 46 else 47 return adxl345_core_probe(&spi->dev, regmap, needs_delay, NULL); 48 } 49 50 static const struct adxl345_chip_info adxl345_spi_info = { 51 .name = "adxl345", 52 .uscale = ADXL345_USCALE, 53 }; 54 55 static const struct adxl345_chip_info adxl375_spi_info = { 56 .name = "adxl375", 57 .uscale = ADXL375_USCALE, 58 }; 59 60 static const struct spi_device_id adxl345_spi_id[] = { 61 { "adxl345", (kernel_ulong_t)&adxl345_spi_info }, 62 { "adxl375", (kernel_ulong_t)&adxl375_spi_info }, 63 { } 64 }; 65 MODULE_DEVICE_TABLE(spi, adxl345_spi_id); 66 67 static const struct of_device_id adxl345_of_match[] = { 68 { .compatible = "adi,adxl345", .data = &adxl345_spi_info }, 69 { .compatible = "adi,adxl375", .data = &adxl375_spi_info }, 70 { } 71 }; 72 MODULE_DEVICE_TABLE(of, adxl345_of_match); 73 74 static const struct acpi_device_id adxl345_acpi_match[] = { 75 { "ADS0345", (kernel_ulong_t)&adxl345_spi_info }, 76 { } 77 }; 78 MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match); 79 80 static struct spi_driver adxl345_spi_driver = { 81 .driver = { 82 .name = "adxl345_spi", 83 .of_match_table = adxl345_of_match, 84 .acpi_match_table = adxl345_acpi_match, 85 }, 86 .probe = adxl345_spi_probe, 87 .id_table = adxl345_spi_id, 88 }; 89 module_spi_driver(adxl345_spi_driver); 90 91 MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>"); 92 MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver"); 93 MODULE_LICENSE("GPL v2"); 94 MODULE_IMPORT_NS("IIO_ADXL345"); 95