Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * IIO driver for the MiraMEMS DA280 3-axis accelerometer and
0004  * IIO driver for the MiraMEMS DA226 2-axis accelerometer
0005  *
0006  * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/acpi.h>
0012 #include <linux/iio/iio.h>
0013 #include <linux/iio/sysfs.h>
0014 #include <linux/byteorder/generic.h>
0015 
0016 #define DA280_REG_CHIP_ID       0x01
0017 #define DA280_REG_ACC_X_LSB     0x02
0018 #define DA280_REG_ACC_Y_LSB     0x04
0019 #define DA280_REG_ACC_Z_LSB     0x06
0020 #define DA280_REG_MODE_BW       0x11
0021 
0022 #define DA280_CHIP_ID           0x13
0023 #define DA280_MODE_ENABLE       0x1e
0024 #define DA280_MODE_DISABLE      0x9e
0025 
0026 enum da280_chipset { da226, da280 };
0027 
0028 /*
0029  * a value of + or -4096 corresponds to + or - 1G
0030  * scale = 9.81 / 4096 = 0.002395019
0031  */
0032 
0033 static const int da280_nscale = 2395019;
0034 
0035 #define DA280_CHANNEL(reg, axis) {  \
0036     .type = IIO_ACCEL,  \
0037     .address = reg, \
0038     .modified = 1,  \
0039     .channel2 = IIO_MOD_##axis, \
0040     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
0041     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
0042 }
0043 
0044 static const struct iio_chan_spec da280_channels[] = {
0045     DA280_CHANNEL(DA280_REG_ACC_X_LSB, X),
0046     DA280_CHANNEL(DA280_REG_ACC_Y_LSB, Y),
0047     DA280_CHANNEL(DA280_REG_ACC_Z_LSB, Z),
0048 };
0049 
0050 struct da280_data {
0051     struct i2c_client *client;
0052 };
0053 
0054 static int da280_enable(struct i2c_client *client, bool enable)
0055 {
0056     u8 data = enable ? DA280_MODE_ENABLE : DA280_MODE_DISABLE;
0057 
0058     return i2c_smbus_write_byte_data(client, DA280_REG_MODE_BW, data);
0059 }
0060 
0061 static int da280_read_raw(struct iio_dev *indio_dev,
0062                 struct iio_chan_spec const *chan,
0063                 int *val, int *val2, long mask)
0064 {
0065     struct da280_data *data = iio_priv(indio_dev);
0066     int ret;
0067 
0068     switch (mask) {
0069     case IIO_CHAN_INFO_RAW:
0070         ret = i2c_smbus_read_word_data(data->client, chan->address);
0071         if (ret < 0)
0072             return ret;
0073         /*
0074          * Values are 14 bits, stored as 16 bits with the 2
0075          * least significant bits always 0.
0076          */
0077         *val = (short)ret >> 2;
0078         return IIO_VAL_INT;
0079     case IIO_CHAN_INFO_SCALE:
0080         *val = 0;
0081         *val2 = da280_nscale;
0082         return IIO_VAL_INT_PLUS_NANO;
0083     default:
0084         return -EINVAL;
0085     }
0086 }
0087 
0088 static const struct iio_info da280_info = {
0089     .read_raw   = da280_read_raw,
0090 };
0091 
0092 static enum da280_chipset da280_match_acpi_device(struct device *dev)
0093 {
0094     const struct acpi_device_id *id;
0095 
0096     id = acpi_match_device(dev->driver->acpi_match_table, dev);
0097     if (!id)
0098         return -EINVAL;
0099 
0100     return (enum da280_chipset) id->driver_data;
0101 }
0102 
0103 static void da280_disable(void *client)
0104 {
0105     da280_enable(client, false);
0106 }
0107 
0108 static int da280_probe(struct i2c_client *client,
0109             const struct i2c_device_id *id)
0110 {
0111     int ret;
0112     struct iio_dev *indio_dev;
0113     struct da280_data *data;
0114     enum da280_chipset chip;
0115 
0116     ret = i2c_smbus_read_byte_data(client, DA280_REG_CHIP_ID);
0117     if (ret != DA280_CHIP_ID)
0118         return (ret < 0) ? ret : -ENODEV;
0119 
0120     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
0121     if (!indio_dev)
0122         return -ENOMEM;
0123 
0124     data = iio_priv(indio_dev);
0125     data->client = client;
0126 
0127     indio_dev->info = &da280_info;
0128     indio_dev->modes = INDIO_DIRECT_MODE;
0129     indio_dev->channels = da280_channels;
0130 
0131     if (ACPI_HANDLE(&client->dev)) {
0132         chip = da280_match_acpi_device(&client->dev);
0133     } else {
0134         chip = id->driver_data;
0135     }
0136 
0137     if (chip == da226) {
0138         indio_dev->name = "da226";
0139         indio_dev->num_channels = 2;
0140     } else {
0141         indio_dev->name = "da280";
0142         indio_dev->num_channels = 3;
0143     }
0144 
0145     ret = da280_enable(client, true);
0146     if (ret < 0)
0147         return ret;
0148 
0149     ret = devm_add_action_or_reset(&client->dev, da280_disable, client);
0150     if (ret)
0151         return ret;
0152 
0153     return devm_iio_device_register(&client->dev, indio_dev);
0154 }
0155 
0156 static int da280_suspend(struct device *dev)
0157 {
0158     return da280_enable(to_i2c_client(dev), false);
0159 }
0160 
0161 static int da280_resume(struct device *dev)
0162 {
0163     return da280_enable(to_i2c_client(dev), true);
0164 }
0165 
0166 static DEFINE_SIMPLE_DEV_PM_OPS(da280_pm_ops, da280_suspend, da280_resume);
0167 
0168 static const struct acpi_device_id da280_acpi_match[] = {
0169     {"MIRAACC", da280},
0170     {},
0171 };
0172 MODULE_DEVICE_TABLE(acpi, da280_acpi_match);
0173 
0174 static const struct i2c_device_id da280_i2c_id[] = {
0175     { "da226", da226 },
0176     { "da280", da280 },
0177     {}
0178 };
0179 MODULE_DEVICE_TABLE(i2c, da280_i2c_id);
0180 
0181 static struct i2c_driver da280_driver = {
0182     .driver = {
0183         .name = "da280",
0184         .acpi_match_table = ACPI_PTR(da280_acpi_match),
0185         .pm = pm_sleep_ptr(&da280_pm_ops),
0186     },
0187     .probe      = da280_probe,
0188     .id_table   = da280_i2c_id,
0189 };
0190 
0191 module_i2c_driver(da280_driver);
0192 
0193 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0194 MODULE_DESCRIPTION("MiraMEMS DA280 3-Axis Accelerometer driver");
0195 MODULE_LICENSE("GPL v2");