| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* ADXL313 3-Axis Digital Accelerometer |
| 4 |
* |
| 5 |
* Copyright (c) 2021 Lucas Stankus <[email protected]> |
| 6 |
* |
| 7 |
* Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/i2c.h> |
| 11 |
#include <linux/mod_devicetable.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/regmap.h> |
| 14 |
|
| 15 |
#include "adxl313.h" |
| 16 |
|
| 17 |
static const struct regmap_config adxl31x_i2c_regmap_config[] = { |
| 18 |
[ADXL312] = { |
| 19 |
.reg_bits = 8, |
| 20 |
.val_bits = 8, |
| 21 |
.rd_table = &adxl312_readable_regs_table, |
| 22 |
.wr_table = &adxl312_writable_regs_table, |
| 23 |
.max_register = 0x39, |
| 24 |
.volatile_reg = adxl313_is_volatile_reg, |
| 25 |
.cache_type = REGCACHE_MAPLE, |
| 26 |
}, |
| 27 |
[ADXL313] = { |
| 28 |
.reg_bits = 8, |
| 29 |
.val_bits = 8, |
| 30 |
.rd_table = &adxl313_readable_regs_table, |
| 31 |
.wr_table = &adxl313_writable_regs_table, |
| 32 |
.max_register = 0x39, |
| 33 |
.volatile_reg = adxl313_is_volatile_reg, |
| 34 |
.cache_type = REGCACHE_MAPLE, |
| 35 |
}, |
| 36 |
[ADXL314] = { |
| 37 |
.reg_bits = 8, |
| 38 |
.val_bits = 8, |
| 39 |
.rd_table = &adxl314_readable_regs_table, |
| 40 |
.wr_table = &adxl314_writable_regs_table, |
| 41 |
.max_register = 0x39, |
| 42 |
.volatile_reg = adxl313_is_volatile_reg, |
| 43 |
.cache_type = REGCACHE_MAPLE, |
| 44 |
}, |
| 45 |
}; |
| 46 |
|
| 47 |
static const struct i2c_device_id adxl313_i2c_id[] = { |
| 48 |
{ .name = "adxl312", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL312] }, |
| 49 |
{ .name = "adxl313", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL313] }, |
| 50 |
{ .name = "adxl314", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL314] }, |
| 51 |
{ } |
| 52 |
}; |
| 53 |
|
| 54 |
MODULE_DEVICE_TABLE(i2c, adxl313_i2c_id); |
| 55 |
|
| 56 |
static const struct of_device_id adxl313_of_match[] = { |
| 57 |
{ .compatible = "adi,adxl312", .data = &adxl31x_chip_info[ADXL312] }, |
| 58 |
{ .compatible = "adi,adxl313", .data = &adxl31x_chip_info[ADXL313] }, |
| 59 |
{ .compatible = "adi,adxl314", .data = &adxl31x_chip_info[ADXL314] }, |
| 60 |
{ } |
| 61 |
}; |
| 62 |
|
| 63 |
MODULE_DEVICE_TABLE(of, adxl313_of_match); |
| 64 |
|
| 65 |
static int adxl313_i2c_probe(struct i2c_client *client) |
| 66 |
{ |
| 67 |
const struct adxl313_chip_info *chip_data; |
| 68 |
struct regmap *regmap; |
| 69 |
|
| 70 |
/* |
| 71 |
* Retrieves device specific data as a pointer to a |
| 72 |
* adxl313_chip_info structure |
| 73 |
*/ |
| 74 |
chip_data = i2c_get_match_data(client); |
| 75 |
|
| 76 |
regmap = devm_regmap_init_i2c(client, |
| 77 |
&adxl31x_i2c_regmap_config[chip_data->type]); |
| 78 |
if (IS_ERR(regmap)) { |
| 79 |
dev_err(&client->dev, "Error initializing i2c regmap: %ld\n", |
| 80 |
PTR_ERR(regmap)); |
| 81 |
return PTR_ERR(regmap); |
| 82 |
} |
| 83 |
|
| 84 |
return adxl313_core_probe(&client->dev, regmap, chip_data, NULL); |
| 85 |
} |
| 86 |
|
| 87 |
static struct i2c_driver adxl313_i2c_driver = { |
| 88 |
.driver = { |
| 89 |
.name = "adxl313_i2c", |
| 90 |
.of_match_table = adxl313_of_match, |
| 91 |
}, |
| 92 |
.probe = adxl313_i2c_probe, |
| 93 |
.id_table = adxl313_i2c_id, |
| 94 |
}; |
| 95 |
|
| 96 |
module_i2c_driver(adxl313_i2c_driver); |
| 97 |
|
| 98 |
MODULE_AUTHOR("Lucas Stankus <[email protected]>"); |
| 99 |
MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer I2C driver"); |
| 100 |
MODULE_LICENSE("GPL v2"); |
| 101 |
MODULE_IMPORT_NS("IIO_ADXL313"); |