136edc939SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2fb0e5250SEva Rachel Retuya /* 3fb0e5250SEva Rachel Retuya * ADXL345 3-Axis Digital Accelerometer SPI driver 4fb0e5250SEva Rachel Retuya * 5fb0e5250SEva Rachel Retuya * Copyright (c) 2017 Eva Rachel Retuya <eraretuya@gmail.com> 6fb0e5250SEva Rachel Retuya */ 7fb0e5250SEva Rachel Retuya 8fb0e5250SEva Rachel Retuya #include <linux/module.h> 9fb0e5250SEva Rachel Retuya #include <linux/regmap.h> 10fb0e5250SEva Rachel Retuya #include <linux/spi/spi.h> 11fb0e5250SEva Rachel Retuya 12fb0e5250SEva Rachel Retuya #include "adxl345.h" 13fb0e5250SEva Rachel Retuya 14fb0e5250SEva Rachel Retuya #define ADXL345_MAX_SPI_FREQ_HZ 5000000 15fb0e5250SEva Rachel Retuya 16fb0e5250SEva Rachel Retuya static const struct regmap_config adxl345_spi_regmap_config = { 17fb0e5250SEva Rachel Retuya .reg_bits = 8, 18fb0e5250SEva Rachel Retuya .val_bits = 8, 19fb0e5250SEva Rachel Retuya /* Setting bits 7 and 6 enables multiple-byte read */ 20fb0e5250SEva Rachel Retuya .read_flag_mask = BIT(7) | BIT(6), 21fb0e5250SEva Rachel Retuya }; 22fb0e5250SEva Rachel Retuya 23*2f896dd9SLothar Rubusch static int adxl345_spi_setup(struct device *dev, struct regmap *regmap) 24*2f896dd9SLothar Rubusch { 25*2f896dd9SLothar Rubusch return regmap_write(regmap, ADXL345_REG_DATA_FORMAT, ADXL345_DATA_FORMAT_SPI_3WIRE); 26*2f896dd9SLothar Rubusch } 27*2f896dd9SLothar Rubusch 28fb0e5250SEva Rachel Retuya static int adxl345_spi_probe(struct spi_device *spi) 29fb0e5250SEva Rachel Retuya { 30fb0e5250SEva Rachel Retuya struct regmap *regmap; 31fb0e5250SEva Rachel Retuya 32fb0e5250SEva Rachel Retuya /* Bail out if max_speed_hz exceeds 5 MHz */ 3301676b0fSAndy Shevchenko if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ) 3401676b0fSAndy Shevchenko return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n", 35fb0e5250SEva Rachel Retuya spi->max_speed_hz); 36fb0e5250SEva Rachel Retuya 37fb0e5250SEva Rachel Retuya regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config); 3801676b0fSAndy Shevchenko if (IS_ERR(regmap)) 3901676b0fSAndy Shevchenko return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n"); 40fb0e5250SEva Rachel Retuya 41*2f896dd9SLothar Rubusch if (spi->mode & SPI_3WIRE) 42*2f896dd9SLothar Rubusch return adxl345_core_probe(&spi->dev, regmap, adxl345_spi_setup); 43*2f896dd9SLothar Rubusch else 4441561abcSLothar Rubusch return adxl345_core_probe(&spi->dev, regmap, NULL); 45fb0e5250SEva Rachel Retuya } 46fb0e5250SEva Rachel Retuya 477d0ba6dbSBiju Das static const struct adxl345_chip_info adxl345_spi_info = { 487d0ba6dbSBiju Das .name = "adxl345", 49dcc3ac13SBiju Das .uscale = ADXL345_USCALE, 507d0ba6dbSBiju Das }; 517d0ba6dbSBiju Das 527d0ba6dbSBiju Das static const struct adxl345_chip_info adxl375_spi_info = { 537d0ba6dbSBiju Das .name = "adxl375", 54dcc3ac13SBiju Das .uscale = ADXL375_USCALE, 557d0ba6dbSBiju Das }; 567d0ba6dbSBiju Das 57fb0e5250SEva Rachel Retuya static const struct spi_device_id adxl345_spi_id[] = { 587d0ba6dbSBiju Das { "adxl345", (kernel_ulong_t)&adxl345_spi_info }, 597d0ba6dbSBiju Das { "adxl375", (kernel_ulong_t)&adxl375_spi_info }, 60fb0e5250SEva Rachel Retuya { } 61fb0e5250SEva Rachel Retuya }; 62fb0e5250SEva Rachel Retuya MODULE_DEVICE_TABLE(spi, adxl345_spi_id); 63fb0e5250SEva Rachel Retuya 64fb0e5250SEva Rachel Retuya static const struct of_device_id adxl345_of_match[] = { 657d0ba6dbSBiju Das { .compatible = "adi,adxl345", .data = &adxl345_spi_info }, 667d0ba6dbSBiju Das { .compatible = "adi,adxl375", .data = &adxl375_spi_info }, 67b8f83abdSAndy Shevchenko { } 68fb0e5250SEva Rachel Retuya }; 69fb0e5250SEva Rachel Retuya MODULE_DEVICE_TABLE(of, adxl345_of_match); 70fb0e5250SEva Rachel Retuya 71b9493d59SKai-Heng Feng static const struct acpi_device_id adxl345_acpi_match[] = { 727d0ba6dbSBiju Das { "ADS0345", (kernel_ulong_t)&adxl345_spi_info }, 73b9493d59SKai-Heng Feng { } 74b9493d59SKai-Heng Feng }; 75b9493d59SKai-Heng Feng MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match); 76b9493d59SKai-Heng Feng 77fb0e5250SEva Rachel Retuya static struct spi_driver adxl345_spi_driver = { 78fb0e5250SEva Rachel Retuya .driver = { 79fb0e5250SEva Rachel Retuya .name = "adxl345_spi", 80fb0e5250SEva Rachel Retuya .of_match_table = adxl345_of_match, 81b9493d59SKai-Heng Feng .acpi_match_table = adxl345_acpi_match, 82fb0e5250SEva Rachel Retuya }, 83fb0e5250SEva Rachel Retuya .probe = adxl345_spi_probe, 84fb0e5250SEva Rachel Retuya .id_table = adxl345_spi_id, 85fb0e5250SEva Rachel Retuya }; 86fb0e5250SEva Rachel Retuya module_spi_driver(adxl345_spi_driver); 87fb0e5250SEva Rachel Retuya 88fb0e5250SEva Rachel Retuya MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>"); 89fb0e5250SEva Rachel Retuya MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver"); 90fb0e5250SEva Rachel Retuya MODULE_LICENSE("GPL v2"); 910a18114dSJonathan Cameron MODULE_IMPORT_NS(IIO_ADXL345); 92