Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (c) 2011 Bosch Sensortec GmbH
0004  * Copyright (c) 2011 Unixphere
0005  *
0006  * This driver adds support for Bosch Sensortec's digital acceleration
0007  * sensors BMA150 and SMB380.
0008  * The SMB380 is fully compatible with BMA150 and only differs in packaging.
0009  *
0010  * The datasheet for the BMA150 chip can be found here:
0011  * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMA150-DS000-07.pdf
0012  */
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/i2c.h>
0016 #include <linux/input.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/delay.h>
0019 #include <linux/slab.h>
0020 #include <linux/pm.h>
0021 #include <linux/pm_runtime.h>
0022 #include <linux/bma150.h>
0023 
0024 #define ABSMAX_ACC_VAL      0x01FF
0025 #define ABSMIN_ACC_VAL      -(ABSMAX_ACC_VAL)
0026 
0027 /* Each axis is represented by a 2-byte data word */
0028 #define BMA150_XYZ_DATA_SIZE    6
0029 
0030 /* Input poll interval in milliseconds */
0031 #define BMA150_POLL_INTERVAL    10
0032 #define BMA150_POLL_MAX     200
0033 #define BMA150_POLL_MIN     0
0034 
0035 #define BMA150_MODE_NORMAL  0
0036 #define BMA150_MODE_SLEEP   2
0037 #define BMA150_MODE_WAKE_UP 3
0038 
0039 /* Data register addresses */
0040 #define BMA150_DATA_0_REG   0x00
0041 #define BMA150_DATA_1_REG   0x01
0042 #define BMA150_DATA_2_REG   0x02
0043 
0044 /* Control register addresses */
0045 #define BMA150_CTRL_0_REG   0x0A
0046 #define BMA150_CTRL_1_REG   0x0B
0047 #define BMA150_CTRL_2_REG   0x14
0048 #define BMA150_CTRL_3_REG   0x15
0049 
0050 /* Configuration/Setting register addresses */
0051 #define BMA150_CFG_0_REG    0x0C
0052 #define BMA150_CFG_1_REG    0x0D
0053 #define BMA150_CFG_2_REG    0x0E
0054 #define BMA150_CFG_3_REG    0x0F
0055 #define BMA150_CFG_4_REG    0x10
0056 #define BMA150_CFG_5_REG    0x11
0057 
0058 #define BMA150_CHIP_ID      2
0059 #define BMA150_CHIP_ID_REG  BMA150_DATA_0_REG
0060 
0061 #define BMA150_ACC_X_LSB_REG    BMA150_DATA_2_REG
0062 
0063 #define BMA150_SLEEP_POS    0
0064 #define BMA150_SLEEP_MSK    0x01
0065 #define BMA150_SLEEP_REG    BMA150_CTRL_0_REG
0066 
0067 #define BMA150_BANDWIDTH_POS    0
0068 #define BMA150_BANDWIDTH_MSK    0x07
0069 #define BMA150_BANDWIDTH_REG    BMA150_CTRL_2_REG
0070 
0071 #define BMA150_RANGE_POS    3
0072 #define BMA150_RANGE_MSK    0x18
0073 #define BMA150_RANGE_REG    BMA150_CTRL_2_REG
0074 
0075 #define BMA150_WAKE_UP_POS  0
0076 #define BMA150_WAKE_UP_MSK  0x01
0077 #define BMA150_WAKE_UP_REG  BMA150_CTRL_3_REG
0078 
0079 #define BMA150_SW_RES_POS   1
0080 #define BMA150_SW_RES_MSK   0x02
0081 #define BMA150_SW_RES_REG   BMA150_CTRL_0_REG
0082 
0083 /* Any-motion interrupt register fields */
0084 #define BMA150_ANY_MOTION_EN_POS    6
0085 #define BMA150_ANY_MOTION_EN_MSK    0x40
0086 #define BMA150_ANY_MOTION_EN_REG    BMA150_CTRL_1_REG
0087 
0088 #define BMA150_ANY_MOTION_DUR_POS   6
0089 #define BMA150_ANY_MOTION_DUR_MSK   0xC0
0090 #define BMA150_ANY_MOTION_DUR_REG   BMA150_CFG_5_REG
0091 
0092 #define BMA150_ANY_MOTION_THRES_REG BMA150_CFG_4_REG
0093 
0094 /* Advanced interrupt register fields */
0095 #define BMA150_ADV_INT_EN_POS       6
0096 #define BMA150_ADV_INT_EN_MSK       0x40
0097 #define BMA150_ADV_INT_EN_REG       BMA150_CTRL_3_REG
0098 
0099 /* High-G interrupt register fields */
0100 #define BMA150_HIGH_G_EN_POS        1
0101 #define BMA150_HIGH_G_EN_MSK        0x02
0102 #define BMA150_HIGH_G_EN_REG        BMA150_CTRL_1_REG
0103 
0104 #define BMA150_HIGH_G_HYST_POS      3
0105 #define BMA150_HIGH_G_HYST_MSK      0x38
0106 #define BMA150_HIGH_G_HYST_REG      BMA150_CFG_5_REG
0107 
0108 #define BMA150_HIGH_G_DUR_REG       BMA150_CFG_3_REG
0109 #define BMA150_HIGH_G_THRES_REG     BMA150_CFG_2_REG
0110 
0111 /* Low-G interrupt register fields */
0112 #define BMA150_LOW_G_EN_POS     0
0113 #define BMA150_LOW_G_EN_MSK     0x01
0114 #define BMA150_LOW_G_EN_REG     BMA150_CTRL_1_REG
0115 
0116 #define BMA150_LOW_G_HYST_POS       0
0117 #define BMA150_LOW_G_HYST_MSK       0x07
0118 #define BMA150_LOW_G_HYST_REG       BMA150_CFG_5_REG
0119 
0120 #define BMA150_LOW_G_DUR_REG        BMA150_CFG_1_REG
0121 #define BMA150_LOW_G_THRES_REG      BMA150_CFG_0_REG
0122 
0123 struct bma150_data {
0124     struct i2c_client *client;
0125     struct input_dev *input;
0126     u8 mode;
0127 };
0128 
0129 /*
0130  * The settings for the given range, bandwidth and interrupt features
0131  * are stated and verified by Bosch Sensortec where they are configured
0132  * to provide a generic sensitivity performance.
0133  */
0134 static const struct bma150_cfg default_cfg = {
0135     .any_motion_int = 1,
0136     .hg_int = 1,
0137     .lg_int = 1,
0138     .any_motion_dur = 0,
0139     .any_motion_thres = 0,
0140     .hg_hyst = 0,
0141     .hg_dur = 150,
0142     .hg_thres = 160,
0143     .lg_hyst = 0,
0144     .lg_dur = 150,
0145     .lg_thres = 20,
0146     .range = BMA150_RANGE_2G,
0147     .bandwidth = BMA150_BW_50HZ
0148 };
0149 
0150 static int bma150_write_byte(struct i2c_client *client, u8 reg, u8 val)
0151 {
0152     s32 ret;
0153 
0154     /* As per specification, disable irq in between register writes */
0155     if (client->irq)
0156         disable_irq_nosync(client->irq);
0157 
0158     ret = i2c_smbus_write_byte_data(client, reg, val);
0159 
0160     if (client->irq)
0161         enable_irq(client->irq);
0162 
0163     return ret;
0164 }
0165 
0166 static int bma150_set_reg_bits(struct i2c_client *client,
0167                     int val, int shift, u8 mask, u8 reg)
0168 {
0169     int data;
0170 
0171     data = i2c_smbus_read_byte_data(client, reg);
0172     if (data < 0)
0173         return data;
0174 
0175     data = (data & ~mask) | ((val << shift) & mask);
0176     return bma150_write_byte(client, reg, data);
0177 }
0178 
0179 static int bma150_set_mode(struct bma150_data *bma150, u8 mode)
0180 {
0181     int error;
0182 
0183     error = bma150_set_reg_bits(bma150->client, mode, BMA150_WAKE_UP_POS,
0184                 BMA150_WAKE_UP_MSK, BMA150_WAKE_UP_REG);
0185     if (error)
0186         return error;
0187 
0188     error = bma150_set_reg_bits(bma150->client, mode, BMA150_SLEEP_POS,
0189                 BMA150_SLEEP_MSK, BMA150_SLEEP_REG);
0190     if (error)
0191         return error;
0192 
0193     if (mode == BMA150_MODE_NORMAL)
0194         usleep_range(2000, 2100);
0195 
0196     bma150->mode = mode;
0197     return 0;
0198 }
0199 
0200 static int bma150_soft_reset(struct bma150_data *bma150)
0201 {
0202     int error;
0203 
0204     error = bma150_set_reg_bits(bma150->client, 1, BMA150_SW_RES_POS,
0205                 BMA150_SW_RES_MSK, BMA150_SW_RES_REG);
0206     if (error)
0207         return error;
0208 
0209     usleep_range(2000, 2100);
0210     return 0;
0211 }
0212 
0213 static int bma150_set_range(struct bma150_data *bma150, u8 range)
0214 {
0215     return bma150_set_reg_bits(bma150->client, range, BMA150_RANGE_POS,
0216                 BMA150_RANGE_MSK, BMA150_RANGE_REG);
0217 }
0218 
0219 static int bma150_set_bandwidth(struct bma150_data *bma150, u8 bw)
0220 {
0221     return bma150_set_reg_bits(bma150->client, bw, BMA150_BANDWIDTH_POS,
0222                 BMA150_BANDWIDTH_MSK, BMA150_BANDWIDTH_REG);
0223 }
0224 
0225 static int bma150_set_low_g_interrupt(struct bma150_data *bma150,
0226                     u8 enable, u8 hyst, u8 dur, u8 thres)
0227 {
0228     int error;
0229 
0230     error = bma150_set_reg_bits(bma150->client, hyst,
0231                 BMA150_LOW_G_HYST_POS, BMA150_LOW_G_HYST_MSK,
0232                 BMA150_LOW_G_HYST_REG);
0233     if (error)
0234         return error;
0235 
0236     error = bma150_write_byte(bma150->client, BMA150_LOW_G_DUR_REG, dur);
0237     if (error)
0238         return error;
0239 
0240     error = bma150_write_byte(bma150->client, BMA150_LOW_G_THRES_REG, thres);
0241     if (error)
0242         return error;
0243 
0244     return bma150_set_reg_bits(bma150->client, !!enable,
0245                 BMA150_LOW_G_EN_POS, BMA150_LOW_G_EN_MSK,
0246                 BMA150_LOW_G_EN_REG);
0247 }
0248 
0249 static int bma150_set_high_g_interrupt(struct bma150_data *bma150,
0250                     u8 enable, u8 hyst, u8 dur, u8 thres)
0251 {
0252     int error;
0253 
0254     error = bma150_set_reg_bits(bma150->client, hyst,
0255                 BMA150_HIGH_G_HYST_POS, BMA150_HIGH_G_HYST_MSK,
0256                 BMA150_HIGH_G_HYST_REG);
0257     if (error)
0258         return error;
0259 
0260     error = bma150_write_byte(bma150->client,
0261                 BMA150_HIGH_G_DUR_REG, dur);
0262     if (error)
0263         return error;
0264 
0265     error = bma150_write_byte(bma150->client,
0266                 BMA150_HIGH_G_THRES_REG, thres);
0267     if (error)
0268         return error;
0269 
0270     return bma150_set_reg_bits(bma150->client, !!enable,
0271                 BMA150_HIGH_G_EN_POS, BMA150_HIGH_G_EN_MSK,
0272                 BMA150_HIGH_G_EN_REG);
0273 }
0274 
0275 
0276 static int bma150_set_any_motion_interrupt(struct bma150_data *bma150,
0277                         u8 enable, u8 dur, u8 thres)
0278 {
0279     int error;
0280 
0281     error = bma150_set_reg_bits(bma150->client, dur,
0282                 BMA150_ANY_MOTION_DUR_POS,
0283                 BMA150_ANY_MOTION_DUR_MSK,
0284                 BMA150_ANY_MOTION_DUR_REG);
0285     if (error)
0286         return error;
0287 
0288     error = bma150_write_byte(bma150->client,
0289                 BMA150_ANY_MOTION_THRES_REG, thres);
0290     if (error)
0291         return error;
0292 
0293     error = bma150_set_reg_bits(bma150->client, !!enable,
0294                 BMA150_ADV_INT_EN_POS, BMA150_ADV_INT_EN_MSK,
0295                 BMA150_ADV_INT_EN_REG);
0296     if (error)
0297         return error;
0298 
0299     return bma150_set_reg_bits(bma150->client, !!enable,
0300                 BMA150_ANY_MOTION_EN_POS,
0301                 BMA150_ANY_MOTION_EN_MSK,
0302                 BMA150_ANY_MOTION_EN_REG);
0303 }
0304 
0305 static void bma150_report_xyz(struct bma150_data *bma150)
0306 {
0307     u8 data[BMA150_XYZ_DATA_SIZE];
0308     s16 x, y, z;
0309     s32 ret;
0310 
0311     ret = i2c_smbus_read_i2c_block_data(bma150->client,
0312             BMA150_ACC_X_LSB_REG, BMA150_XYZ_DATA_SIZE, data);
0313     if (ret != BMA150_XYZ_DATA_SIZE)
0314         return;
0315 
0316     x = ((0xc0 & data[0]) >> 6) | (data[1] << 2);
0317     y = ((0xc0 & data[2]) >> 6) | (data[3] << 2);
0318     z = ((0xc0 & data[4]) >> 6) | (data[5] << 2);
0319 
0320     x = sign_extend32(x, 9);
0321     y = sign_extend32(y, 9);
0322     z = sign_extend32(z, 9);
0323 
0324     input_report_abs(bma150->input, ABS_X, x);
0325     input_report_abs(bma150->input, ABS_Y, y);
0326     input_report_abs(bma150->input, ABS_Z, z);
0327     input_sync(bma150->input);
0328 }
0329 
0330 static irqreturn_t bma150_irq_thread(int irq, void *dev)
0331 {
0332     bma150_report_xyz(dev);
0333 
0334     return IRQ_HANDLED;
0335 }
0336 
0337 static void bma150_poll(struct input_dev *input)
0338 {
0339     struct bma150_data *bma150 = input_get_drvdata(input);
0340 
0341     bma150_report_xyz(bma150);
0342 }
0343 
0344 static int bma150_open(struct input_dev *input)
0345 {
0346     struct bma150_data *bma150 = input_get_drvdata(input);
0347     int error;
0348 
0349     error = pm_runtime_get_sync(&bma150->client->dev);
0350     if (error < 0 && error != -ENOSYS)
0351         return error;
0352 
0353     /*
0354      * See if runtime PM woke up the device. If runtime PM
0355      * is disabled we need to do it ourselves.
0356      */
0357     if (bma150->mode != BMA150_MODE_NORMAL) {
0358         error = bma150_set_mode(bma150, BMA150_MODE_NORMAL);
0359         if (error)
0360             return error;
0361     }
0362 
0363     return 0;
0364 }
0365 
0366 static void bma150_close(struct input_dev *input)
0367 {
0368     struct bma150_data *bma150 = input_get_drvdata(input);
0369 
0370     pm_runtime_put_sync(&bma150->client->dev);
0371 
0372     if (bma150->mode != BMA150_MODE_SLEEP)
0373         bma150_set_mode(bma150, BMA150_MODE_SLEEP);
0374 }
0375 
0376 static int bma150_initialize(struct bma150_data *bma150,
0377                  const struct bma150_cfg *cfg)
0378 {
0379     int error;
0380 
0381     error = bma150_soft_reset(bma150);
0382     if (error)
0383         return error;
0384 
0385     error = bma150_set_bandwidth(bma150, cfg->bandwidth);
0386     if (error)
0387         return error;
0388 
0389     error = bma150_set_range(bma150, cfg->range);
0390     if (error)
0391         return error;
0392 
0393     if (bma150->client->irq) {
0394         error = bma150_set_any_motion_interrupt(bma150,
0395                     cfg->any_motion_int,
0396                     cfg->any_motion_dur,
0397                     cfg->any_motion_thres);
0398         if (error)
0399             return error;
0400 
0401         error = bma150_set_high_g_interrupt(bma150,
0402                     cfg->hg_int, cfg->hg_hyst,
0403                     cfg->hg_dur, cfg->hg_thres);
0404         if (error)
0405             return error;
0406 
0407         error = bma150_set_low_g_interrupt(bma150,
0408                     cfg->lg_int, cfg->lg_hyst,
0409                     cfg->lg_dur, cfg->lg_thres);
0410         if (error)
0411             return error;
0412     }
0413 
0414     return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
0415 }
0416 
0417 static int bma150_probe(struct i2c_client *client,
0418             const struct i2c_device_id *id)
0419 {
0420     const struct bma150_platform_data *pdata =
0421             dev_get_platdata(&client->dev);
0422     const struct bma150_cfg *cfg;
0423     struct bma150_data *bma150;
0424     struct input_dev *idev;
0425     int chip_id;
0426     int error;
0427 
0428     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0429         dev_err(&client->dev, "i2c_check_functionality error\n");
0430         return -EIO;
0431     }
0432 
0433     chip_id = i2c_smbus_read_byte_data(client, BMA150_CHIP_ID_REG);
0434     if (chip_id != BMA150_CHIP_ID) {
0435         dev_err(&client->dev, "BMA150 chip id error: %d\n", chip_id);
0436         return -EINVAL;
0437     }
0438 
0439     bma150 = devm_kzalloc(&client->dev, sizeof(*bma150), GFP_KERNEL);
0440     if (!bma150)
0441         return -ENOMEM;
0442 
0443     bma150->client = client;
0444 
0445     if (pdata) {
0446         if (pdata->irq_gpio_cfg) {
0447             error = pdata->irq_gpio_cfg();
0448             if (error) {
0449                 dev_err(&client->dev,
0450                     "IRQ GPIO conf. error %d, error %d\n",
0451                     client->irq, error);
0452                 return error;
0453             }
0454         }
0455         cfg = &pdata->cfg;
0456     } else {
0457         cfg = &default_cfg;
0458     }
0459 
0460     error = bma150_initialize(bma150, cfg);
0461     if (error)
0462         return error;
0463 
0464     idev = devm_input_allocate_device(&bma150->client->dev);
0465     if (!idev)
0466         return -ENOMEM;
0467 
0468     input_set_drvdata(idev, bma150);
0469     bma150->input = idev;
0470 
0471     idev->name = BMA150_DRIVER;
0472     idev->phys = BMA150_DRIVER "/input0";
0473     idev->id.bustype = BUS_I2C;
0474 
0475     idev->open = bma150_open;
0476     idev->close = bma150_close;
0477 
0478     input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
0479     input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
0480     input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
0481 
0482     if (client->irq <= 0) {
0483         error = input_setup_polling(idev, bma150_poll);
0484         if (error)
0485             return error;
0486 
0487         input_set_poll_interval(idev, BMA150_POLL_INTERVAL);
0488         input_set_min_poll_interval(idev, BMA150_POLL_MIN);
0489         input_set_max_poll_interval(idev, BMA150_POLL_MAX);
0490     }
0491 
0492     error = input_register_device(idev);
0493     if (error)
0494         return error;
0495 
0496     if (client->irq > 0) {
0497         error = devm_request_threaded_irq(&client->dev, client->irq,
0498                     NULL, bma150_irq_thread,
0499                     IRQF_TRIGGER_RISING | IRQF_ONESHOT,
0500                     BMA150_DRIVER, bma150);
0501         if (error) {
0502             dev_err(&client->dev,
0503                 "irq request failed %d, error %d\n",
0504                 client->irq, error);
0505             return error;
0506         }
0507     }
0508 
0509     i2c_set_clientdata(client, bma150);
0510 
0511     pm_runtime_enable(&client->dev);
0512 
0513     return 0;
0514 }
0515 
0516 static int bma150_remove(struct i2c_client *client)
0517 {
0518     pm_runtime_disable(&client->dev);
0519 
0520     return 0;
0521 }
0522 
0523 static int __maybe_unused bma150_suspend(struct device *dev)
0524 {
0525     struct i2c_client *client = to_i2c_client(dev);
0526     struct bma150_data *bma150 = i2c_get_clientdata(client);
0527 
0528     return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
0529 }
0530 
0531 static int __maybe_unused bma150_resume(struct device *dev)
0532 {
0533     struct i2c_client *client = to_i2c_client(dev);
0534     struct bma150_data *bma150 = i2c_get_clientdata(client);
0535 
0536     return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
0537 }
0538 
0539 static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
0540 
0541 static const struct i2c_device_id bma150_id[] = {
0542     { "bma150", 0 },
0543     { "smb380", 0 },
0544     { "bma023", 0 },
0545     { }
0546 };
0547 
0548 MODULE_DEVICE_TABLE(i2c, bma150_id);
0549 
0550 static struct i2c_driver bma150_driver = {
0551     .driver = {
0552         .name   = BMA150_DRIVER,
0553         .pm = &bma150_pm,
0554     },
0555     .class      = I2C_CLASS_HWMON,
0556     .id_table   = bma150_id,
0557     .probe      = bma150_probe,
0558     .remove     = bma150_remove,
0559 };
0560 
0561 module_i2c_driver(bma150_driver);
0562 
0563 MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
0564 MODULE_DESCRIPTION("BMA150 driver");
0565 MODULE_LICENSE("GPL");