| 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 <[email protected]> |
| 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 |
.volatile_reg = adxl345_is_volatile_reg, |
| 23 |
.cache_type = REGCACHE_MAPLE, |
| 24 |
}; |
| 25 |
|
| 26 |
static int adxl345_spi_setup(struct device *dev, struct regmap *regmap) |
| 27 |
{ |
| 28 |
return regmap_write(regmap, ADXL345_REG_DATA_FORMAT, ADXL345_DATA_FORMAT_SPI_3WIRE); |
| 29 |
} |
| 30 |
|
| 31 |
static int adxl345_spi_probe(struct spi_device *spi) |
| 32 |
{ |
| 33 |
struct regmap *regmap; |
| 34 |
bool needs_delay; |
| 35 |
|
| 36 |
/* Bail out if max_speed_hz exceeds 5 MHz */ |
| 37 |
if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ) |
| 38 |
return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n", |
| 39 |
spi->max_speed_hz); |
| 40 |
|
| 41 |
regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config); |
| 42 |
if (IS_ERR(regmap)) |
| 43 |
return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n"); |
| 44 |
|
| 45 |
needs_delay = spi->max_speed_hz > ADXL345_MAX_FREQ_NO_FIFO_DELAY; |
| 46 |
if (spi->mode & SPI_3WIRE) |
| 47 |
return adxl345_core_probe(&spi->dev, regmap, needs_delay, adxl345_spi_setup); |
| 48 |
else |
| 49 |
return adxl345_core_probe(&spi->dev, regmap, needs_delay, NULL); |
| 50 |
} |
| 51 |
|
| 52 |
static const struct adxl345_chip_info adxl345_spi_info = { |
| 53 |
.name = "adxl345", |
| 54 |
.uscale = ADXL345_USCALE, |
| 55 |
}; |
| 56 |
|
| 57 |
static const struct adxl345_chip_info adxl375_spi_info = { |
| 58 |
.name = "adxl375", |
| 59 |
.uscale = ADXL375_USCALE, |
| 60 |
}; |
| 61 |
|
| 62 |
static const struct spi_device_id adxl345_spi_id[] = { |
| 63 |
{ "adxl345", (kernel_ulong_t)&adxl345_spi_info }, |
| 64 |
{ "adxl375", (kernel_ulong_t)&adxl375_spi_info }, |
| 65 |
{ } |
| 66 |
}; |
| 67 |
MODULE_DEVICE_TABLE(spi, adxl345_spi_id); |
| 68 |
|
| 69 |
static const struct of_device_id adxl345_of_match[] = { |
| 70 |
{ .compatible = "adi,adxl345", .data = &adxl345_spi_info }, |
| 71 |
{ .compatible = "adi,adxl375", .data = &adxl375_spi_info }, |
| 72 |
{ } |
| 73 |
}; |
| 74 |
MODULE_DEVICE_TABLE(of, adxl345_of_match); |
| 75 |
|
| 76 |
static const struct acpi_device_id adxl345_acpi_match[] = { |
| 77 |
{ "ADS0345", (kernel_ulong_t)&adxl345_spi_info }, |
| 78 |
{ } |
| 79 |
}; |
| 80 |
MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match); |
| 81 |
|
| 82 |
static struct spi_driver adxl345_spi_driver = { |
| 83 |
.driver = { |
| 84 |
.name = "adxl345_spi", |
| 85 |
.of_match_table = adxl345_of_match, |
| 86 |
.acpi_match_table = adxl345_acpi_match, |
| 87 |
}, |
| 88 |
.probe = adxl345_spi_probe, |
| 89 |
.id_table = adxl345_spi_id, |
| 90 |
}; |
| 91 |
module_spi_driver(adxl345_spi_driver); |
| 92 |
|
| 93 |
MODULE_AUTHOR("Eva Rachel Retuya <[email protected]>"); |
| 94 |
MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver"); |
| 95 |
MODULE_LICENSE("GPL v2"); |
| 96 |
MODULE_IMPORT_NS("IIO_ADXL345"); |