2024-09-09 08:52:07 +00:00
|
|
|
/*
|
2024-09-09 08:57:42 +00:00
|
|
|
* Copyright (c) 2011 Bosch Sensortec GmbH
|
|
|
|
* Copyright (c) 2011 Unixphere
|
|
|
|
*
|
|
|
|
* This driver adds support for Bosch Sensortec's digital acceleration
|
|
|
|
* sensors BMA150 and SMB380.
|
|
|
|
* The SMB380 is fully compatible with BMA150 and only differs in packaging.
|
|
|
|
*
|
|
|
|
* The datasheet for the BMA150 chip can be found here:
|
|
|
|
* http://www.bosch-sensortec.com/content/language1/downloads/BST-BMA150-DS000-07.pdf
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2024-09-09 08:52:07 +00:00
|
|
|
*/
|
2024-09-09 08:57:42 +00:00
|
|
|
#include <linux/kernel.h>
|
2024-09-09 08:52:07 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/input.h>
|
2024-09-09 08:57:42 +00:00
|
|
|
#include <linux/input-polldev.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/delay.h>
|
2024-09-09 08:52:07 +00:00
|
|
|
#include <linux/slab.h>
|
2024-09-09 08:57:42 +00:00
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
2024-09-09 08:52:07 +00:00
|
|
|
#include <linux/bma150.h>
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define ABSMAX_ACC_VAL 0x01FF
|
|
|
|
#define ABSMIN_ACC_VAL -(ABSMAX_ACC_VAL)
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Each axis is represented by a 2-byte data word */
|
|
|
|
#define BMA150_XYZ_DATA_SIZE 6
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Input poll interval in milliseconds */
|
|
|
|
#define BMA150_POLL_INTERVAL 10
|
|
|
|
#define BMA150_POLL_MAX 200
|
|
|
|
#define BMA150_POLL_MIN 0
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_MODE_NORMAL 0
|
|
|
|
#define BMA150_MODE_SLEEP 2
|
|
|
|
#define BMA150_MODE_WAKE_UP 3
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Data register addresses */
|
|
|
|
#define BMA150_DATA_0_REG 0x00
|
|
|
|
#define BMA150_DATA_1_REG 0x01
|
|
|
|
#define BMA150_DATA_2_REG 0x02
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Control register addresses */
|
|
|
|
#define BMA150_CTRL_0_REG 0x0A
|
|
|
|
#define BMA150_CTRL_1_REG 0x0B
|
|
|
|
#define BMA150_CTRL_2_REG 0x14
|
|
|
|
#define BMA150_CTRL_3_REG 0x15
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Configuration/Setting register addresses */
|
|
|
|
#define BMA150_CFG_0_REG 0x0C
|
|
|
|
#define BMA150_CFG_1_REG 0x0D
|
|
|
|
#define BMA150_CFG_2_REG 0x0E
|
|
|
|
#define BMA150_CFG_3_REG 0x0F
|
|
|
|
#define BMA150_CFG_4_REG 0x10
|
|
|
|
#define BMA150_CFG_5_REG 0x11
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_CHIP_ID 2
|
|
|
|
#define BMA180_CHIP_ID 3
|
|
|
|
#define BMA150_CHIP_ID_REG BMA150_DATA_0_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_ACC_X_LSB_REG BMA150_DATA_2_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_SLEEP_POS 0
|
|
|
|
#define BMA150_SLEEP_MSK 0x01
|
|
|
|
#define BMA150_SLEEP_REG BMA150_CTRL_0_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_BANDWIDTH_POS 0
|
|
|
|
#define BMA150_BANDWIDTH_MSK 0x07
|
|
|
|
#define BMA150_BANDWIDTH_REG BMA150_CTRL_2_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_RANGE_POS 3
|
|
|
|
#define BMA150_RANGE_MSK 0x18
|
|
|
|
#define BMA150_RANGE_REG BMA150_CTRL_2_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_WAKE_UP_POS 0
|
|
|
|
#define BMA150_WAKE_UP_MSK 0x01
|
|
|
|
#define BMA150_WAKE_UP_REG BMA150_CTRL_3_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_SW_RES_POS 1
|
|
|
|
#define BMA150_SW_RES_MSK 0x02
|
|
|
|
#define BMA150_SW_RES_REG BMA150_CTRL_0_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Any-motion interrupt register fields */
|
|
|
|
#define BMA150_ANY_MOTION_EN_POS 6
|
|
|
|
#define BMA150_ANY_MOTION_EN_MSK 0x40
|
|
|
|
#define BMA150_ANY_MOTION_EN_REG BMA150_CTRL_1_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_ANY_MOTION_DUR_POS 6
|
|
|
|
#define BMA150_ANY_MOTION_DUR_MSK 0xC0
|
|
|
|
#define BMA150_ANY_MOTION_DUR_REG BMA150_CFG_5_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_ANY_MOTION_THRES_REG BMA150_CFG_4_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Advanced interrupt register fields */
|
|
|
|
#define BMA150_ADV_INT_EN_POS 6
|
|
|
|
#define BMA150_ADV_INT_EN_MSK 0x40
|
|
|
|
#define BMA150_ADV_INT_EN_REG BMA150_CTRL_3_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* High-G interrupt register fields */
|
|
|
|
#define BMA150_HIGH_G_EN_POS 1
|
|
|
|
#define BMA150_HIGH_G_EN_MSK 0x02
|
|
|
|
#define BMA150_HIGH_G_EN_REG BMA150_CTRL_1_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_HIGH_G_HYST_POS 3
|
|
|
|
#define BMA150_HIGH_G_HYST_MSK 0x38
|
|
|
|
#define BMA150_HIGH_G_HYST_REG BMA150_CFG_5_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_HIGH_G_DUR_REG BMA150_CFG_3_REG
|
|
|
|
#define BMA150_HIGH_G_THRES_REG BMA150_CFG_2_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Low-G interrupt register fields */
|
|
|
|
#define BMA150_LOW_G_EN_POS 0
|
|
|
|
#define BMA150_LOW_G_EN_MSK 0x01
|
|
|
|
#define BMA150_LOW_G_EN_REG BMA150_CTRL_1_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_LOW_G_HYST_POS 0
|
|
|
|
#define BMA150_LOW_G_HYST_MSK 0x07
|
|
|
|
#define BMA150_LOW_G_HYST_REG BMA150_CFG_5_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#define BMA150_LOW_G_DUR_REG BMA150_CFG_1_REG
|
|
|
|
#define BMA150_LOW_G_THRES_REG BMA150_CFG_0_REG
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
struct bma150_data {
|
|
|
|
struct i2c_client *client;
|
|
|
|
struct input_polled_dev *input_polled;
|
|
|
|
struct input_dev *input;
|
|
|
|
u8 mode;
|
|
|
|
};
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/*
|
|
|
|
* The settings for the given range, bandwidth and interrupt features
|
|
|
|
* are stated and verified by Bosch Sensortec where they are configured
|
|
|
|
* to provide a generic sensitivity performance.
|
|
|
|
*/
|
|
|
|
static struct bma150_cfg default_cfg = {
|
|
|
|
.any_motion_int = 1,
|
|
|
|
.hg_int = 1,
|
|
|
|
.lg_int = 1,
|
|
|
|
.any_motion_dur = 0,
|
|
|
|
.any_motion_thres = 0,
|
|
|
|
.hg_hyst = 0,
|
|
|
|
.hg_dur = 150,
|
|
|
|
.hg_thres = 160,
|
|
|
|
.lg_hyst = 0,
|
|
|
|
.lg_dur = 150,
|
|
|
|
.lg_thres = 20,
|
|
|
|
.range = BMA150_RANGE_2G,
|
|
|
|
.bandwidth = BMA150_BW_50HZ
|
|
|
|
};
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_write_byte(struct i2c_client *client, u8 reg, u8 val)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
s32 ret;
|
|
|
|
|
|
|
|
/* As per specification, disable irq in between register writes */
|
|
|
|
if (client->irq)
|
|
|
|
disable_irq_nosync(client->irq);
|
|
|
|
|
|
|
|
ret = i2c_smbus_write_byte_data(client, reg, val);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
if (client->irq)
|
|
|
|
enable_irq(client->irq);
|
|
|
|
|
|
|
|
return ret;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_set_reg_bits(struct i2c_client *client,
|
|
|
|
int val, int shift, u8 mask, u8 reg)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
int data;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
data = i2c_smbus_read_byte_data(client, reg);
|
|
|
|
if (data < 0)
|
|
|
|
return data;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
data = (data & ~mask) | ((val << shift) & mask);
|
|
|
|
return bma150_write_byte(client, reg, data);
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_set_mode(struct bma150_data *bma150, u8 mode)
|
|
|
|
{
|
|
|
|
int error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_reg_bits(bma150->client, mode, BMA150_WAKE_UP_POS,
|
|
|
|
BMA150_WAKE_UP_MSK, BMA150_WAKE_UP_REG);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_reg_bits(bma150->client, mode, BMA150_SLEEP_POS,
|
|
|
|
BMA150_SLEEP_MSK, BMA150_SLEEP_REG);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
if (mode == BMA150_MODE_NORMAL)
|
|
|
|
msleep(2);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
bma150->mode = mode;
|
|
|
|
return 0;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_soft_reset(struct bma150_data *bma150)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
int error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_reg_bits(bma150->client, 1, BMA150_SW_RES_POS,
|
|
|
|
BMA150_SW_RES_MSK, BMA150_SW_RES_REG);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
msleep(2);
|
|
|
|
return 0;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_set_range(struct bma150_data *bma150, u8 range)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
return bma150_set_reg_bits(bma150->client, range, BMA150_RANGE_POS,
|
|
|
|
BMA150_RANGE_MSK, BMA150_RANGE_REG);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_set_bandwidth(struct bma150_data *bma150, u8 bw)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
return bma150_set_reg_bits(bma150->client, bw, BMA150_BANDWIDTH_POS,
|
|
|
|
BMA150_BANDWIDTH_MSK, BMA150_BANDWIDTH_REG);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_set_low_g_interrupt(struct bma150_data *bma150,
|
|
|
|
u8 enable, u8 hyst, u8 dur, u8 thres)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_reg_bits(bma150->client, hyst,
|
|
|
|
BMA150_LOW_G_HYST_POS, BMA150_LOW_G_HYST_MSK,
|
|
|
|
BMA150_LOW_G_HYST_REG);
|
2024-09-09 08:52:07 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_write_byte(bma150->client, BMA150_LOW_G_DUR_REG, dur);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
error = bma150_write_byte(bma150->client, BMA150_LOW_G_THRES_REG, thres);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return bma150_set_reg_bits(bma150->client, !!enable,
|
|
|
|
BMA150_LOW_G_EN_POS, BMA150_LOW_G_EN_MSK,
|
|
|
|
BMA150_LOW_G_EN_REG);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
2024-09-09 08:57:42 +00:00
|
|
|
|
|
|
|
static int bma150_set_high_g_interrupt(struct bma150_data *bma150,
|
|
|
|
u8 enable, u8 hyst, u8 dur, u8 thres)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
int error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_reg_bits(bma150->client, hyst,
|
|
|
|
BMA150_HIGH_G_HYST_POS, BMA150_HIGH_G_HYST_MSK,
|
|
|
|
BMA150_HIGH_G_HYST_REG);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_write_byte(bma150->client,
|
|
|
|
BMA150_HIGH_G_DUR_REG, dur);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
error = bma150_write_byte(bma150->client,
|
|
|
|
BMA150_HIGH_G_THRES_REG, thres);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
return bma150_set_reg_bits(bma150->client, !!enable,
|
|
|
|
BMA150_HIGH_G_EN_POS, BMA150_HIGH_G_EN_MSK,
|
|
|
|
BMA150_HIGH_G_EN_REG);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
|
|
|
|
static int bma150_set_any_motion_interrupt(struct bma150_data *bma150,
|
|
|
|
u8 enable, u8 dur, u8 thres)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_reg_bits(bma150->client, dur,
|
|
|
|
BMA150_ANY_MOTION_DUR_POS,
|
|
|
|
BMA150_ANY_MOTION_DUR_MSK,
|
|
|
|
BMA150_ANY_MOTION_DUR_REG);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
error = bma150_write_byte(bma150->client,
|
|
|
|
BMA150_ANY_MOTION_THRES_REG, thres);
|
2024-09-09 08:52:07 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_reg_bits(bma150->client, !!enable,
|
|
|
|
BMA150_ADV_INT_EN_POS, BMA150_ADV_INT_EN_MSK,
|
|
|
|
BMA150_ADV_INT_EN_REG);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
return bma150_set_reg_bits(bma150->client, !!enable,
|
|
|
|
BMA150_ANY_MOTION_EN_POS,
|
|
|
|
BMA150_ANY_MOTION_EN_MSK,
|
|
|
|
BMA150_ANY_MOTION_EN_REG);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static void bma150_report_xyz(struct bma150_data *bma150)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
u8 data[BMA150_XYZ_DATA_SIZE];
|
|
|
|
s16 x, y, z;
|
|
|
|
s32 ret;
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_i2c_block_data(bma150->client,
|
|
|
|
BMA150_ACC_X_LSB_REG, BMA150_XYZ_DATA_SIZE, data);
|
|
|
|
if (ret != BMA150_XYZ_DATA_SIZE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
x = ((0xc0 & data[0]) >> 6) | (data[1] << 2);
|
|
|
|
y = ((0xc0 & data[2]) >> 6) | (data[3] << 2);
|
|
|
|
z = ((0xc0 & data[4]) >> 6) | (data[5] << 2);
|
|
|
|
|
|
|
|
/* sign extension */
|
|
|
|
x = (s16) (x << 6) >> 6;
|
|
|
|
y = (s16) (y << 6) >> 6;
|
|
|
|
z = (s16) (z << 6) >> 6;
|
|
|
|
|
|
|
|
input_report_abs(bma150->input, ABS_X, x);
|
|
|
|
input_report_abs(bma150->input, ABS_Y, y);
|
|
|
|
input_report_abs(bma150->input, ABS_Z, z);
|
|
|
|
input_sync(bma150->input);
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static irqreturn_t bma150_irq_thread(int irq, void *dev)
|
|
|
|
{
|
|
|
|
bma150_report_xyz(dev);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static void bma150_poll(struct input_polled_dev *dev)
|
|
|
|
{
|
|
|
|
bma150_report_xyz(dev->private);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_open(struct bma150_data *bma150)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = pm_runtime_get_sync(&bma150->client->dev);
|
|
|
|
if (error < 0 && error != -ENOSYS)
|
2024-09-09 08:52:07 +00:00
|
|
|
return error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/*
|
|
|
|
* See if runtime PM woke up the device. If runtime PM
|
|
|
|
* is disabled we need to do it ourselves.
|
|
|
|
*/
|
|
|
|
if (bma150->mode != BMA150_MODE_NORMAL) {
|
|
|
|
error = bma150_set_mode(bma150, BMA150_MODE_NORMAL);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static void bma150_close(struct bma150_data *bma150)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
pm_runtime_put_sync(&bma150->client->dev);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
if (bma150->mode != BMA150_MODE_SLEEP)
|
|
|
|
bma150_set_mode(bma150, BMA150_MODE_SLEEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bma150_irq_open(struct input_dev *input)
|
|
|
|
{
|
|
|
|
struct bma150_data *bma150 = input_get_drvdata(input);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return bma150_open(bma150);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static void bma150_irq_close(struct input_dev *input)
|
|
|
|
{
|
|
|
|
struct bma150_data *bma150 = input_get_drvdata(input);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
bma150_close(bma150);
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static void bma150_poll_open(struct input_polled_dev *ipoll_dev)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
struct bma150_data *bma150 = ipoll_dev->private;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
bma150_open(bma150);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bma150_poll_close(struct input_polled_dev *ipoll_dev)
|
|
|
|
{
|
|
|
|
struct bma150_data *bma150 = ipoll_dev->private;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
bma150_close(bma150);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_initialize(struct bma150_data *bma150,
|
|
|
|
const struct bma150_cfg *cfg)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_soft_reset(bma150);
|
2024-09-09 08:52:07 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_bandwidth(bma150, cfg->bandwidth);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_set_range(bma150, cfg->range);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
if (bma150->client->irq) {
|
|
|
|
error = bma150_set_any_motion_interrupt(bma150,
|
|
|
|
cfg->any_motion_int,
|
|
|
|
cfg->any_motion_dur,
|
|
|
|
cfg->any_motion_thres);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
error = bma150_set_high_g_interrupt(bma150,
|
|
|
|
cfg->hg_int, cfg->hg_hyst,
|
|
|
|
cfg->hg_dur, cfg->hg_thres);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
error = bma150_set_low_g_interrupt(bma150,
|
|
|
|
cfg->lg_int, cfg->lg_hyst,
|
|
|
|
cfg->lg_dur, cfg->lg_thres);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static void bma150_init_input_device(struct bma150_data *bma150,
|
|
|
|
struct input_dev *idev)
|
|
|
|
{
|
|
|
|
idev->name = BMA150_DRIVER;
|
|
|
|
idev->phys = BMA150_DRIVER "/input0";
|
|
|
|
idev->id.bustype = BUS_I2C;
|
|
|
|
idev->dev.parent = &bma150->client->dev;
|
|
|
|
|
|
|
|
idev->evbit[0] = BIT_MASK(EV_ABS);
|
|
|
|
input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
|
|
|
|
input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
|
|
|
|
input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_register_input_device(struct bma150_data *bma150)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
struct input_dev *idev;
|
|
|
|
int error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
idev = input_allocate_device();
|
|
|
|
if (!idev)
|
2024-09-09 08:52:07 +00:00
|
|
|
return -ENOMEM;
|
2024-09-09 08:57:42 +00:00
|
|
|
|
|
|
|
bma150_init_input_device(bma150, idev);
|
|
|
|
|
|
|
|
idev->open = bma150_irq_open;
|
|
|
|
idev->close = bma150_irq_close;
|
|
|
|
input_set_drvdata(idev, bma150);
|
|
|
|
|
|
|
|
error = input_register_device(idev);
|
|
|
|
if (error) {
|
|
|
|
input_free_device(idev);
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
bma150->input = idev;
|
2024-09-09 08:52:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_register_polled_device(struct bma150_data *bma150)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
struct input_polled_dev *ipoll_dev;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
ipoll_dev = input_allocate_polled_device();
|
|
|
|
if (!ipoll_dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ipoll_dev->private = bma150;
|
|
|
|
ipoll_dev->open = bma150_poll_open;
|
|
|
|
ipoll_dev->close = bma150_poll_close;
|
|
|
|
ipoll_dev->poll = bma150_poll;
|
|
|
|
ipoll_dev->poll_interval = BMA150_POLL_INTERVAL;
|
|
|
|
ipoll_dev->poll_interval_min = BMA150_POLL_MIN;
|
|
|
|
ipoll_dev->poll_interval_max = BMA150_POLL_MAX;
|
|
|
|
|
|
|
|
bma150_init_input_device(bma150, ipoll_dev->input);
|
|
|
|
|
|
|
|
error = input_register_polled_device(ipoll_dev);
|
|
|
|
if (error) {
|
|
|
|
input_free_polled_device(ipoll_dev);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bma150->input_polled = ipoll_dev;
|
|
|
|
bma150->input = ipoll_dev->input;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return 0;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bma150_probe(struct i2c_client *client,
|
2024-09-09 08:57:42 +00:00
|
|
|
const struct i2c_device_id *id)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
const struct bma150_platform_data *pdata =
|
|
|
|
dev_get_platdata(&client->dev);
|
|
|
|
const struct bma150_cfg *cfg;
|
|
|
|
struct bma150_data *bma150;
|
|
|
|
int chip_id;
|
|
|
|
int error;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
|
2024-09-09 08:57:42 +00:00
|
|
|
dev_err(&client->dev, "i2c_check_functionality error\n");
|
|
|
|
return -EIO;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
chip_id = i2c_smbus_read_byte_data(client, BMA150_CHIP_ID_REG);
|
|
|
|
if (chip_id != BMA150_CHIP_ID && chip_id != BMA180_CHIP_ID) {
|
|
|
|
dev_err(&client->dev, "BMA150 chip id error: %d\n", chip_id);
|
|
|
|
return -EINVAL;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
|
|
|
|
if (!bma150)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
bma150->client = client;
|
|
|
|
|
|
|
|
if (pdata) {
|
|
|
|
if (pdata->irq_gpio_cfg) {
|
|
|
|
error = pdata->irq_gpio_cfg();
|
|
|
|
if (error) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"IRQ GPIO conf. error %d, error %d\n",
|
|
|
|
client->irq, error);
|
|
|
|
goto err_free_mem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg = &pdata->cfg;
|
|
|
|
} else {
|
|
|
|
cfg = &default_cfg;
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
error = bma150_initialize(bma150, cfg);
|
|
|
|
if (error)
|
|
|
|
goto err_free_mem;
|
|
|
|
|
|
|
|
if (client->irq > 0) {
|
|
|
|
error = bma150_register_input_device(bma150);
|
|
|
|
if (error)
|
|
|
|
goto err_free_mem;
|
|
|
|
|
|
|
|
error = request_threaded_irq(client->irq,
|
|
|
|
NULL, bma150_irq_thread,
|
|
|
|
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
|
|
|
|
BMA150_DRIVER, bma150);
|
|
|
|
if (error) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"irq request failed %d, error %d\n",
|
|
|
|
client->irq, error);
|
|
|
|
input_unregister_device(bma150->input);
|
|
|
|
goto err_free_mem;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error = bma150_register_polled_device(bma150);
|
|
|
|
if (error)
|
|
|
|
goto err_free_mem;
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
i2c_set_clientdata(client, bma150);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
pm_runtime_enable(&client->dev);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
err_free_mem:
|
|
|
|
kfree(bma150);
|
|
|
|
return error;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_remove(struct i2c_client *client)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
struct bma150_data *bma150 = i2c_get_clientdata(client);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
pm_runtime_disable(&client->dev);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
if (client->irq > 0) {
|
|
|
|
free_irq(client->irq, bma150);
|
|
|
|
input_unregister_device(bma150->input);
|
|
|
|
} else {
|
|
|
|
input_unregister_polled_device(bma150->input_polled);
|
|
|
|
input_free_polled_device(bma150->input_polled);
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
kfree(bma150);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int bma150_suspend(struct device *dev)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
|
|
|
struct bma150_data *bma150 = i2c_get_clientdata(client);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static int bma150_resume(struct device *dev)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
|
|
|
struct bma150_data *bma150 = i2c_get_clientdata(client);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
2024-09-09 08:57:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
static const struct i2c_device_id bma150_id[] = {
|
2024-09-09 08:57:42 +00:00
|
|
|
{ "bma150", 0 },
|
|
|
|
{ "bma180", 0 },
|
|
|
|
{ "smb380", 0 },
|
|
|
|
{ "bma023", 0 },
|
2024-09-09 08:52:07 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(i2c, bma150_id);
|
|
|
|
|
|
|
|
static struct i2c_driver bma150_driver = {
|
|
|
|
.driver = {
|
|
|
|
.owner = THIS_MODULE,
|
2024-09-09 08:57:42 +00:00
|
|
|
.name = BMA150_DRIVER,
|
|
|
|
.pm = &bma150_pm,
|
2024-09-09 08:52:07 +00:00
|
|
|
},
|
2024-09-09 08:57:42 +00:00
|
|
|
.class = I2C_CLASS_HWMON,
|
2024-09-09 08:52:07 +00:00
|
|
|
.id_table = bma150_id,
|
|
|
|
.probe = bma150_probe,
|
|
|
|
.remove = bma150_remove,
|
|
|
|
};
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
module_i2c_driver(bma150_driver);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
|
2024-09-09 08:52:07 +00:00
|
|
|
MODULE_DESCRIPTION("BMA150 driver");
|
2024-09-09 08:57:42 +00:00
|
|
|
MODULE_LICENSE("GPL");
|