Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * IIO driver for the MiraMEMS DA311 3-axis accelerometer
0004  *
0005  * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
0006  * Copyright (c) 2011-2013 MiraMEMS Sensing Technology Co., Ltd.
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/iio/iio.h>
0012 #include <linux/iio/sysfs.h>
0013 #include <linux/byteorder/generic.h>
0014 
0015 #define DA311_CHIP_ID           0x13
0016 
0017 /*
0018  * Note register addressed go from 0 - 0x3f and then wrap.
0019  * For some reason there are 2 banks with 0 - 0x3f addresses,
0020  * rather then a single 0-0x7f bank.
0021  */
0022 
0023 /* Bank 0 regs */
0024 #define DA311_REG_BANK          0x0000
0025 #define DA311_REG_LDO_REG       0x0006
0026 #define DA311_REG_CHIP_ID       0x000f
0027 #define DA311_REG_TEMP_CFG_REG      0x001f
0028 #define DA311_REG_CTRL_REG1     0x0020
0029 #define DA311_REG_CTRL_REG3     0x0022
0030 #define DA311_REG_CTRL_REG4     0x0023
0031 #define DA311_REG_CTRL_REG5     0x0024
0032 #define DA311_REG_CTRL_REG6     0x0025
0033 #define DA311_REG_STATUS_REG        0x0027
0034 #define DA311_REG_OUT_X_L       0x0028
0035 #define DA311_REG_OUT_X_H       0x0029
0036 #define DA311_REG_OUT_Y_L       0x002a
0037 #define DA311_REG_OUT_Y_H       0x002b
0038 #define DA311_REG_OUT_Z_L       0x002c
0039 #define DA311_REG_OUT_Z_H       0x002d
0040 #define DA311_REG_INT1_CFG      0x0030
0041 #define DA311_REG_INT1_SRC      0x0031
0042 #define DA311_REG_INT1_THS      0x0032
0043 #define DA311_REG_INT1_DURATION     0x0033
0044 #define DA311_REG_INT2_CFG      0x0034
0045 #define DA311_REG_INT2_SRC      0x0035
0046 #define DA311_REG_INT2_THS      0x0036
0047 #define DA311_REG_INT2_DURATION     0x0037
0048 #define DA311_REG_CLICK_CFG     0x0038
0049 #define DA311_REG_CLICK_SRC     0x0039
0050 #define DA311_REG_CLICK_THS     0x003a
0051 #define DA311_REG_TIME_LIMIT        0x003b
0052 #define DA311_REG_TIME_LATENCY      0x003c
0053 #define DA311_REG_TIME_WINDOW       0x003d
0054 
0055 /* Bank 1 regs */
0056 #define DA311_REG_SOFT_RESET        0x0105
0057 #define DA311_REG_OTP_XOFF_L        0x0110
0058 #define DA311_REG_OTP_XOFF_H        0x0111
0059 #define DA311_REG_OTP_YOFF_L        0x0112
0060 #define DA311_REG_OTP_YOFF_H        0x0113
0061 #define DA311_REG_OTP_ZOFF_L        0x0114
0062 #define DA311_REG_OTP_ZOFF_H        0x0115
0063 #define DA311_REG_OTP_XSO       0x0116
0064 #define DA311_REG_OTP_YSO       0x0117
0065 #define DA311_REG_OTP_ZSO       0x0118
0066 #define DA311_REG_OTP_TRIM_OSC      0x011b
0067 #define DA311_REG_LPF_ABSOLUTE      0x011c
0068 #define DA311_REG_TEMP_OFF1     0x0127
0069 #define DA311_REG_TEMP_OFF2     0x0128
0070 #define DA311_REG_TEMP_OFF3     0x0129
0071 #define DA311_REG_OTP_TRIM_THERM_H  0x011a
0072 
0073 /*
0074  * a value of + or -1024 corresponds to + or - 1G
0075  * scale = 9.81 / 1024 = 0.009580078
0076  */
0077 
0078 static const int da311_nscale = 9580078;
0079 
0080 #define DA311_CHANNEL(reg, axis) {  \
0081     .type = IIO_ACCEL,  \
0082     .address = reg, \
0083     .modified = 1,  \
0084     .channel2 = IIO_MOD_##axis, \
0085     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
0086     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
0087 }
0088 
0089 static const struct iio_chan_spec da311_channels[] = {
0090     /* | 0x80 comes from the android driver */
0091     DA311_CHANNEL(DA311_REG_OUT_X_L | 0x80, X),
0092     DA311_CHANNEL(DA311_REG_OUT_Y_L | 0x80, Y),
0093     DA311_CHANNEL(DA311_REG_OUT_Z_L | 0x80, Z),
0094 };
0095 
0096 struct da311_data {
0097     struct i2c_client *client;
0098 };
0099 
0100 static int da311_register_mask_write(struct i2c_client *client, u16 addr,
0101                      u8 mask, u8 data)
0102 {
0103     int ret;
0104     u8 tmp_data = 0;
0105 
0106     if (addr & 0xff00) {
0107         /* Select bank 1 */
0108         ret = i2c_smbus_write_byte_data(client, DA311_REG_BANK, 0x01);
0109         if (ret < 0)
0110             return ret;
0111     }
0112 
0113     if (mask != 0xff) {
0114         ret = i2c_smbus_read_byte_data(client, addr);
0115         if (ret < 0)
0116             return ret;
0117         tmp_data = ret;
0118     }
0119 
0120     tmp_data &= ~mask;
0121     tmp_data |= data & mask;
0122     ret = i2c_smbus_write_byte_data(client, addr & 0xff, tmp_data);
0123     if (ret < 0)
0124         return ret;
0125 
0126     if (addr & 0xff00) {
0127         /* Back to bank 0 */
0128         ret = i2c_smbus_write_byte_data(client, DA311_REG_BANK, 0x00);
0129         if (ret < 0)
0130             return ret;
0131     }
0132 
0133     return 0;
0134 }
0135 
0136 /* Init sequence taken from the android driver */
0137 static int da311_reset(struct i2c_client *client)
0138 {
0139     static const struct {
0140         u16 addr;
0141         u8 mask;
0142         u8 data;
0143     } init_data[] = {
0144         { DA311_REG_TEMP_CFG_REG,       0xff,   0x08 },
0145         { DA311_REG_CTRL_REG5,          0xff,   0x80 },
0146         { DA311_REG_CTRL_REG4,          0x30,   0x00 },
0147         { DA311_REG_CTRL_REG1,          0xff,   0x6f },
0148         { DA311_REG_TEMP_CFG_REG,       0xff,   0x88 },
0149         { DA311_REG_LDO_REG,            0xff,   0x02 },
0150         { DA311_REG_OTP_TRIM_OSC,       0xff,   0x27 },
0151         { DA311_REG_LPF_ABSOLUTE,       0xff,   0x30 },
0152         { DA311_REG_TEMP_OFF1,          0xff,   0x3f },
0153         { DA311_REG_TEMP_OFF2,          0xff,   0xff },
0154         { DA311_REG_TEMP_OFF3,          0xff,   0x0f },
0155     };
0156     int i, ret;
0157 
0158     /* Reset */
0159     ret = da311_register_mask_write(client, DA311_REG_SOFT_RESET,
0160                     0xff, 0xaa);
0161     if (ret < 0)
0162         return ret;
0163 
0164     for (i = 0; i < ARRAY_SIZE(init_data); i++) {
0165         ret = da311_register_mask_write(client,
0166                         init_data[i].addr,
0167                         init_data[i].mask,
0168                         init_data[i].data);
0169         if (ret < 0)
0170             return ret;
0171     }
0172 
0173     return 0;
0174 }
0175 
0176 static int da311_enable(struct i2c_client *client, bool enable)
0177 {
0178     u8 data = enable ? 0x00 : 0x20;
0179 
0180     return da311_register_mask_write(client, DA311_REG_TEMP_CFG_REG,
0181                      0x20, data);
0182 }
0183 
0184 static int da311_read_raw(struct iio_dev *indio_dev,
0185                 struct iio_chan_spec const *chan,
0186                 int *val, int *val2, long mask)
0187 {
0188     struct da311_data *data = iio_priv(indio_dev);
0189     int ret;
0190 
0191     switch (mask) {
0192     case IIO_CHAN_INFO_RAW:
0193         ret = i2c_smbus_read_word_data(data->client, chan->address);
0194         if (ret < 0)
0195             return ret;
0196         /*
0197          * Values are 12 bits, stored as 16 bits with the 4
0198          * least significant bits always 0.
0199          */
0200         *val = (short)ret >> 4;
0201         return IIO_VAL_INT;
0202     case IIO_CHAN_INFO_SCALE:
0203         *val = 0;
0204         *val2 = da311_nscale;
0205         return IIO_VAL_INT_PLUS_NANO;
0206     default:
0207         return -EINVAL;
0208     }
0209 }
0210 
0211 static const struct iio_info da311_info = {
0212     .read_raw   = da311_read_raw,
0213 };
0214 
0215 static void da311_disable(void *client)
0216 {
0217     da311_enable(client, false);
0218 }
0219 
0220 static int da311_probe(struct i2c_client *client,
0221             const struct i2c_device_id *id)
0222 {
0223     int ret;
0224     struct iio_dev *indio_dev;
0225     struct da311_data *data;
0226 
0227     ret = i2c_smbus_read_byte_data(client, DA311_REG_CHIP_ID);
0228     if (ret != DA311_CHIP_ID)
0229         return (ret < 0) ? ret : -ENODEV;
0230 
0231     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
0232     if (!indio_dev)
0233         return -ENOMEM;
0234 
0235     data = iio_priv(indio_dev);
0236     data->client = client;
0237 
0238     indio_dev->info = &da311_info;
0239     indio_dev->name = "da311";
0240     indio_dev->modes = INDIO_DIRECT_MODE;
0241     indio_dev->channels = da311_channels;
0242     indio_dev->num_channels = ARRAY_SIZE(da311_channels);
0243 
0244     ret = da311_reset(client);
0245     if (ret < 0)
0246         return ret;
0247 
0248     ret = da311_enable(client, true);
0249     if (ret < 0)
0250         return ret;
0251 
0252     ret = devm_add_action_or_reset(&client->dev, da311_disable, client);
0253     if (ret)
0254         return ret;
0255 
0256     return devm_iio_device_register(&client->dev, indio_dev);
0257 }
0258 
0259 static int da311_suspend(struct device *dev)
0260 {
0261     return da311_enable(to_i2c_client(dev), false);
0262 }
0263 
0264 static int da311_resume(struct device *dev)
0265 {
0266     return da311_enable(to_i2c_client(dev), true);
0267 }
0268 
0269 static DEFINE_SIMPLE_DEV_PM_OPS(da311_pm_ops, da311_suspend, da311_resume);
0270 
0271 static const struct i2c_device_id da311_i2c_id[] = {
0272     {"da311", 0},
0273     {}
0274 };
0275 MODULE_DEVICE_TABLE(i2c, da311_i2c_id);
0276 
0277 static struct i2c_driver da311_driver = {
0278     .driver = {
0279         .name = "da311",
0280         .pm = pm_sleep_ptr(&da311_pm_ops),
0281     },
0282     .probe      = da311_probe,
0283     .id_table   = da311_i2c_id,
0284 };
0285 
0286 module_i2c_driver(da311_driver);
0287 
0288 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0289 MODULE_DESCRIPTION("MiraMEMS DA311 3-Axis Accelerometer driver");
0290 MODULE_LICENSE("GPL v2");