sensor: ak8975: Convert to DTS

Convert ak8975 sensor driver to utilize device tree.

This also supports the ak8975 embedded in a invensense MPU9150.  In such
a case the device tree node should look something like, where the ak8975
is a child of the mpu9150.

        mpu9150@68 {
                compatible = "invensense,mpu9150";
                reg = <0x68>;
                label = "mpu9150";
                #address-cells = <1>;
                #size-cells = <0>;
                ak8975@c {
                        compatible = "asahi-kasei,ak8975";
                        reg = <0xc>;
                        label = "ak8975";
                };
        };

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
This commit is contained in:
Kumar Gala 2019-11-07 09:12:07 -06:00 committed by Maureen Helm
commit 94322e503c
6 changed files with 61 additions and 62 deletions

View file

@ -3,55 +3,8 @@
# Copyright (c) 2016 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
menuconfig AK8975
config AK8975
bool "AK8975 Magnetometer"
depends on I2C
help
Enable driver for AK8975 magnetometer.
if AK8975
config AK8975_NAME
string "Driver name"
default "AK8975"
help
Device name with which the AK8975 sensor is identified.
config AK8975_I2C_ADDR
hex "I2C address"
default 0x0C
range 0x0C 0x0F
help
I2C address of the AK8975 sensor. Choose:
- 0x0C if CAD1 connected to GND and CAD0 is connected to GND
- 0x0D if CAD1 connected to GND and CAD0 is connected to VDD
- 0x0E if CAD1 connected to VDD and CAD0 is connected to GND
- 0x0F if CAD1 connected to VDD and CAD0 is connected to VDD
If the AK8975 sensor is part of a MPU9159 chip, the I2C address
needs to be 0x0C.
config AK8975_I2C_MASTER_DEV_NAME
string "I2C master where AK8975 is connected"
default "I2C_0"
help
Specify the device name of the I2C master device to which the
AK8975 chip is connected.
config MPU9150
bool "Enable MPU9180 support"
help
Enable this config option if the AK8975 sensor is part of a
MPU9150 chip.
config MPU9150_I2C_ADDR
hex "MPU9180 I2C address"
depends on MPU9150 && !MPU6050
range 0x68 0x69
default 0x68
help
I2C address of the MPU9150. If the driver for MPU6050 is enabled,
its address will be used and this option made unavailable.
endif # AK8975

View file

@ -24,7 +24,8 @@ static int ak8975_sample_fetch(struct device *dev, enum sensor_channel chan)
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
if (i2c_reg_write_byte(drv_data->i2c, CONFIG_AK8975_I2C_ADDR,
if (i2c_reg_write_byte(drv_data->i2c,
DT_INST_0_ASAHI_KASEI_AK8975_BASE_ADDRESS,
AK8975_REG_CNTL, AK8975_MODE_MEASURE) < 0) {
LOG_ERR("Failed to start measurement.");
return -EIO;
@ -32,7 +33,8 @@ static int ak8975_sample_fetch(struct device *dev, enum sensor_channel chan)
k_busy_wait(AK8975_MEASURE_TIME_US);
if (i2c_burst_read(drv_data->i2c, CONFIG_AK8975_I2C_ADDR,
if (i2c_burst_read(drv_data->i2c,
DT_INST_0_ASAHI_KASEI_AK8975_BASE_ADDRESS,
AK8975_REG_DATA_START, buf, 6) < 0) {
LOG_ERR("Failed to read sample data.");
return -EIO;
@ -91,13 +93,15 @@ static int ak8975_read_adjustment_data(struct ak8975_data *drv_data)
{
u8_t buf[3];
if (i2c_reg_write_byte(drv_data->i2c, CONFIG_AK8975_I2C_ADDR,
if (i2c_reg_write_byte(drv_data->i2c,
DT_INST_0_ASAHI_KASEI_AK8975_BASE_ADDRESS,
AK8975_REG_CNTL, AK8975_MODE_FUSE_ACCESS) < 0) {
LOG_ERR("Failed to set chip in fuse access mode.");
return -EIO;
}
if (i2c_burst_read(drv_data->i2c, CONFIG_AK8975_I2C_ADDR,
if (i2c_burst_read(drv_data->i2c,
DT_INST_0_ASAHI_KASEI_AK8975_BASE_ADDRESS,
AK8975_REG_ADJ_DATA_START, buf, 3) < 0) {
LOG_ERR("Failed to read adjustment data.");
return -EIO;
@ -115,14 +119,15 @@ int ak8975_init(struct device *dev)
struct ak8975_data *drv_data = dev->driver_data;
u8_t id;
drv_data->i2c = device_get_binding(CONFIG_AK8975_I2C_MASTER_DEV_NAME);
drv_data->i2c =
device_get_binding(DT_INST_0_ASAHI_KASEI_AK8975_BUS_NAME);
if (drv_data->i2c == NULL) {
LOG_ERR("Failed to get pointer to %s device!",
CONFIG_AK8975_I2C_MASTER_DEV_NAME);
DT_INST_0_ASAHI_KASEI_AK8975_BUS_NAME);
return -EINVAL;
}
#ifdef CONFIG_MPU9150
#ifdef DT_INST_0_INVENSENSE_MPU9150
/* wake up MPU9150 chip */
if (i2c_reg_update_byte(drv_data->i2c, MPU9150_I2C_ADDR,
MPU9150_REG_PWR_MGMT1, MPU9150_SLEEP_EN,
@ -141,7 +146,8 @@ int ak8975_init(struct device *dev)
#endif
/* check chip ID */
if (i2c_reg_read_byte(drv_data->i2c, CONFIG_AK8975_I2C_ADDR,
if (i2c_reg_read_byte(drv_data->i2c,
DT_INST_0_ASAHI_KASEI_AK8975_BASE_ADDRESS,
AK8975_REG_CHIP_ID, &id) < 0) {
LOG_ERR("Failed to read chip ID.");
return -EIO;
@ -161,6 +167,7 @@ int ak8975_init(struct device *dev)
struct ak8975_data ak8975_data;
DEVICE_AND_API_INIT(ak8975, CONFIG_AK8975_NAME, ak8975_init, &ak8975_data,
DEVICE_AND_API_INIT(ak8975, DT_INST_0_ASAHI_KASEI_AK8975_LABEL, ak8975_init,
&ak8975_data,
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&ak8975_driver_api);

View file

@ -23,13 +23,13 @@
#define AK8975_MEASURE_TIME_US 9000
#define AK8975_MICRO_GAUSS_PER_BIT 3000
#ifdef CONFIG_MPU9150
#if CONFIG_AK8975_I2C_ADDR != 0x0C
#ifdef DT_INST_0_INVENSENSE_MPU9150_BASE_ADDRESS
#if DT_INST_0_ASAHI_KASEI_AK8975_BASE_ADDRESS != 0x0C
#error "I2C address must be 0x0C when AK8975 is part of a MPU9150 chip"
#endif
#ifdef CONFIG_MPU9150_I2C_ADDR
#define MPU9150_I2C_ADDR CONFIG_MPU9150_I2C_ADDR
#ifdef DT_INST_0_INVENSENSE_MPU9150
#define MPU9150_I2C_ADDR DT_INST_0_INVENSENSE_MPU9150_BASE_ADDRESS
#endif
#define MPU9150_REG_BYPASS_CFG 0x37
@ -38,7 +38,7 @@
#define MPU9150_REG_PWR_MGMT1 0x6B
#define MPU9150_SLEEP_EN BIT(6)
#endif /* CONFIG_MPU9150 */
#endif /* DT_INST_0_INVENSENSE_MPU9150 */
struct ak8975_data {

View file

@ -0,0 +1,10 @@
# Copyright (c) 2020, Linaro Limited
# SPDX-License-Identifier: Apache-2.0
description: |
Asahi Kasei AK8975 Magnetometer. See datasheet at
https://www.akm.com/akm/en/file/datasheet/AK8975.pdf
compatible: "asahi-kasei,ak8975"
include: i2c-device.yaml

View file

@ -0,0 +1,23 @@
# Copyright (c) 2020, Linaro Limited
# SPDX-License-Identifier: Apache-2.0
description: |
InvenSense MPU-9150 Nine-Axis (Gyro + Accelerometer + Compass). See more
info at https://www.invensense.com/products/motion-tracking/9-axis/mpu-9150/
compatible: "invensense,mpu9150"
include: i2c-device.yaml
child-binding:
description: Embedded AK8975 Magnetometer
compatible: "asahi-kasei,ak8975"
properties:
"#address-cells":
type: int
required: true
const: 1
"#size-cells":
type: int
required: true
const: 0

View file

@ -453,6 +453,12 @@
#define DT_INST_0_HONEYWELL_HMC5883L_INT_GPIOS_PIN 0
#endif
#ifndef DT_INST_0_ASAHI_KASEI_AK8975_LABEL
#define DT_INST_0_ASAHI_KASEI_AK8975_LABEL ""
#define DT_INST_0_ASAHI_KASEI_AK8975_BASE_ADDRESS 0
#define DT_INST_0_ASAHI_KASEI_AK8975_BUS_NAME ""
#endif
#endif /* CONFIG_HAS_DTS_I2C */
#ifndef DT_ADXL372_DEV_NAME