Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 1 | /* ST Microelectronics LIS2DH 3-axis accelerometer driver |
| 2 | * |
| 3 | * Copyright (c) 2020 STMicroelectronics |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Datasheet: |
| 8 | * https://www.st.com/resource/en/datasheet/lis2dh.pdf |
| 9 | */ |
| 10 | |
Kumar Gala | e268368 | 2020-03-26 16:47:47 -0500 | [diff] [blame] | 11 | #define DT_DRV_COMPAT st_lis2dh |
| 12 | |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 13 | #include <string.h> |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 14 | #include <zephyr/drivers/i2c.h> |
| 15 | #include <zephyr/logging/log.h> |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 16 | |
| 17 | #include "lis2dh.h" |
| 18 | |
Martí Bolívar | 7e0eed9 | 2020-05-06 11:23:07 -0700 | [diff] [blame] | 19 | #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 20 | |
| 21 | LOG_MODULE_DECLARE(lis2dh, CONFIG_SENSOR_LOG_LEVEL); |
| 22 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 23 | static int lis2dh_i2c_read_data(const struct device *dev, uint8_t reg_addr, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 24 | uint8_t *value, uint8_t len) |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 25 | { |
Tomasz Bursztyka | af6140c | 2020-05-28 20:44:16 +0200 | [diff] [blame] | 26 | const struct lis2dh_config *cfg = dev->config; |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 27 | |
Benjamin Björnsson | d2aeb94 | 2022-06-30 06:58:21 +0200 | [diff] [blame] | 28 | return i2c_burst_read_dt(&cfg->bus_cfg.i2c, reg_addr | LIS2DH_AUTOINCREMENT_ADDR, value, |
| 29 | len); |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 30 | } |
| 31 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 32 | static int lis2dh_i2c_write_data(const struct device *dev, uint8_t reg_addr, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 33 | uint8_t *value, uint8_t len) |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 34 | { |
Tomasz Bursztyka | af6140c | 2020-05-28 20:44:16 +0200 | [diff] [blame] | 35 | const struct lis2dh_config *cfg = dev->config; |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 36 | |
Benjamin Björnsson | d2aeb94 | 2022-06-30 06:58:21 +0200 | [diff] [blame] | 37 | return i2c_burst_write_dt(&cfg->bus_cfg.i2c, reg_addr | LIS2DH_AUTOINCREMENT_ADDR, value, |
| 38 | len); |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 41 | static int lis2dh_i2c_read_reg(const struct device *dev, uint8_t reg_addr, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 42 | uint8_t *value) |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 43 | { |
Tomasz Bursztyka | af6140c | 2020-05-28 20:44:16 +0200 | [diff] [blame] | 44 | const struct lis2dh_config *cfg = dev->config; |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 45 | |
Benjamin Björnsson | d2aeb94 | 2022-06-30 06:58:21 +0200 | [diff] [blame] | 46 | return i2c_reg_read_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value); |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 49 | static int lis2dh_i2c_write_reg(const struct device *dev, uint8_t reg_addr, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 50 | uint8_t value) |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 51 | { |
Tomasz Bursztyka | af6140c | 2020-05-28 20:44:16 +0200 | [diff] [blame] | 52 | const struct lis2dh_config *cfg = dev->config; |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 53 | |
Benjamin Björnsson | d2aeb94 | 2022-06-30 06:58:21 +0200 | [diff] [blame] | 54 | return i2c_reg_write_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value); |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 57 | static int lis2dh_i2c_update_reg(const struct device *dev, uint8_t reg_addr, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 58 | uint8_t mask, uint8_t value) |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 59 | { |
Tomasz Bursztyka | af6140c | 2020-05-28 20:44:16 +0200 | [diff] [blame] | 60 | const struct lis2dh_config *cfg = dev->config; |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 61 | |
Benjamin Björnsson | d2aeb94 | 2022-06-30 06:58:21 +0200 | [diff] [blame] | 62 | return i2c_reg_update_byte_dt(&cfg->bus_cfg.i2c, reg_addr, mask, value); |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static const struct lis2dh_transfer_function lis2dh_i2c_transfer_fn = { |
| 66 | .read_data = lis2dh_i2c_read_data, |
| 67 | .write_data = lis2dh_i2c_write_data, |
| 68 | .read_reg = lis2dh_i2c_read_reg, |
| 69 | .write_reg = lis2dh_i2c_write_reg, |
| 70 | .update_reg = lis2dh_i2c_update_reg, |
| 71 | }; |
| 72 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 73 | int lis2dh_i2c_init(const struct device *dev) |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 74 | { |
Tomasz Bursztyka | 98d9b01 | 2020-05-28 21:23:02 +0200 | [diff] [blame] | 75 | struct lis2dh_data *data = dev->data; |
Benjamin Björnsson | d2aeb94 | 2022-06-30 06:58:21 +0200 | [diff] [blame] | 76 | const struct lis2dh_config *cfg = dev->config; |
| 77 | |
| 78 | if (!device_is_ready(cfg->bus_cfg.i2c.bus)) { |
| 79 | LOG_ERR("Bus device is not ready"); |
| 80 | return -ENODEV; |
| 81 | } |
Armando Visconti | 1ebb708 | 2020-02-20 09:31:53 +0100 | [diff] [blame] | 82 | |
| 83 | data->hw_tf = &lis2dh_i2c_transfer_fn; |
| 84 | |
| 85 | return 0; |
| 86 | } |
Martí Bolívar | 7e0eed9 | 2020-05-06 11:23:07 -0700 | [diff] [blame] | 87 | #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */ |