blob: 20fa01d8d3ca381e2e87caf978678a23617c8645 [file] [log] [blame]
Armando Visconti1ebb7082020-02-20 09:31:53 +01001/* 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 Galae2683682020-03-26 16:47:47 -050011#define DT_DRV_COMPAT st_lis2dh
12
Armando Visconti1ebb7082020-02-20 09:31:53 +010013#include <string.h>
Gerard Marull-Paretasfb60aab2022-05-06 10:25:46 +020014#include <zephyr/drivers/i2c.h>
15#include <zephyr/logging/log.h>
Armando Visconti1ebb7082020-02-20 09:31:53 +010016
17#include "lis2dh.h"
18
Martí Bolívar7e0eed92020-05-06 11:23:07 -070019#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
Armando Visconti1ebb7082020-02-20 09:31:53 +010020
21LOG_MODULE_DECLARE(lis2dh, CONFIG_SENSOR_LOG_LEVEL);
22
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020023static int lis2dh_i2c_read_data(const struct device *dev, uint8_t reg_addr,
Kumar Galaa1b77fd2020-05-27 11:26:57 -050024 uint8_t *value, uint8_t len)
Armando Visconti1ebb7082020-02-20 09:31:53 +010025{
Tomasz Bursztykaaf6140c2020-05-28 20:44:16 +020026 const struct lis2dh_config *cfg = dev->config;
Armando Visconti1ebb7082020-02-20 09:31:53 +010027
Benjamin Björnssond2aeb942022-06-30 06:58:21 +020028 return i2c_burst_read_dt(&cfg->bus_cfg.i2c, reg_addr | LIS2DH_AUTOINCREMENT_ADDR, value,
29 len);
Armando Visconti1ebb7082020-02-20 09:31:53 +010030}
31
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020032static int lis2dh_i2c_write_data(const struct device *dev, uint8_t reg_addr,
Kumar Galaa1b77fd2020-05-27 11:26:57 -050033 uint8_t *value, uint8_t len)
Armando Visconti1ebb7082020-02-20 09:31:53 +010034{
Tomasz Bursztykaaf6140c2020-05-28 20:44:16 +020035 const struct lis2dh_config *cfg = dev->config;
Armando Visconti1ebb7082020-02-20 09:31:53 +010036
Benjamin Björnssond2aeb942022-06-30 06:58:21 +020037 return i2c_burst_write_dt(&cfg->bus_cfg.i2c, reg_addr | LIS2DH_AUTOINCREMENT_ADDR, value,
38 len);
Armando Visconti1ebb7082020-02-20 09:31:53 +010039}
40
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020041static int lis2dh_i2c_read_reg(const struct device *dev, uint8_t reg_addr,
Kumar Galaa1b77fd2020-05-27 11:26:57 -050042 uint8_t *value)
Armando Visconti1ebb7082020-02-20 09:31:53 +010043{
Tomasz Bursztykaaf6140c2020-05-28 20:44:16 +020044 const struct lis2dh_config *cfg = dev->config;
Armando Visconti1ebb7082020-02-20 09:31:53 +010045
Benjamin Björnssond2aeb942022-06-30 06:58:21 +020046 return i2c_reg_read_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value);
Armando Visconti1ebb7082020-02-20 09:31:53 +010047}
48
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020049static int lis2dh_i2c_write_reg(const struct device *dev, uint8_t reg_addr,
Kumar Galaa1b77fd2020-05-27 11:26:57 -050050 uint8_t value)
Armando Visconti1ebb7082020-02-20 09:31:53 +010051{
Tomasz Bursztykaaf6140c2020-05-28 20:44:16 +020052 const struct lis2dh_config *cfg = dev->config;
Armando Visconti1ebb7082020-02-20 09:31:53 +010053
Benjamin Björnssond2aeb942022-06-30 06:58:21 +020054 return i2c_reg_write_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value);
Armando Visconti1ebb7082020-02-20 09:31:53 +010055}
56
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020057static int lis2dh_i2c_update_reg(const struct device *dev, uint8_t reg_addr,
Kumar Galaa1b77fd2020-05-27 11:26:57 -050058 uint8_t mask, uint8_t value)
Armando Visconti1ebb7082020-02-20 09:31:53 +010059{
Tomasz Bursztykaaf6140c2020-05-28 20:44:16 +020060 const struct lis2dh_config *cfg = dev->config;
Armando Visconti1ebb7082020-02-20 09:31:53 +010061
Benjamin Björnssond2aeb942022-06-30 06:58:21 +020062 return i2c_reg_update_byte_dt(&cfg->bus_cfg.i2c, reg_addr, mask, value);
Armando Visconti1ebb7082020-02-20 09:31:53 +010063}
64
65static 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 Bursztykae18fcbb2020-04-30 20:33:38 +020073int lis2dh_i2c_init(const struct device *dev)
Armando Visconti1ebb7082020-02-20 09:31:53 +010074{
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +020075 struct lis2dh_data *data = dev->data;
Benjamin Björnssond2aeb942022-06-30 06:58:21 +020076 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 Visconti1ebb7082020-02-20 09:31:53 +010082
83 data->hw_tf = &lis2dh_i2c_transfer_fn;
84
85 return 0;
86}
Martí Bolívar7e0eed92020-05-06 11:23:07 -070087#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */