2019-05-30 02:57:44 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2013-08-28 15:01:00 +04:00
/*
* bma180 . c - IIO driver for Bosch BMA180 triaxial acceleration sensor
*
* Copyright 2013 Oleksandr Kravchenko < x0199363 @ ti . com >
*
2014-08-20 02:43:00 +04:00
* Support for BMA250 ( c ) Peter Meerwald < pmeerw @ pmeerw . net >
*
* SPI is not supported by driver
2020-05-14 23:49:01 +03:00
* BMA023 / BMA150 / SMB380 : 7 - bit I2C slave address 0x38
2014-08-20 02:43:00 +04:00
* BMA180 : 7 - bit I2C slave address 0x40 or 0x41
* BMA250 : 7 - bit I2C slave address 0x18 or 0x19
2019-12-12 00:38:19 +03:00
* BMA254 : 7 - bit I2C slave address 0x18 or 0x19
2013-08-28 15:01:00 +04:00
*/
# include <linux/module.h>
# include <linux/i2c.h>
# include <linux/interrupt.h>
# include <linux/delay.h>
2017-03-15 07:44:57 +03:00
# include <linux/of_device.h>
2013-08-28 15:01:00 +04:00
# include <linux/of.h>
# include <linux/bitops.h>
2019-12-12 00:38:17 +03:00
# include <linux/regulator/consumer.h>
2013-08-28 15:01:00 +04:00
# include <linux/slab.h>
# include <linux/string.h>
# include <linux/iio/iio.h>
# include <linux/iio/sysfs.h>
# include <linux/iio/buffer.h>
# include <linux/iio/trigger.h>
# include <linux/iio/trigger_consumer.h>
# include <linux/iio/triggered_buffer.h>
# define BMA180_DRV_NAME "bma180"
# define BMA180_IRQ_NAME "bma180_event"
2017-03-15 07:44:57 +03:00
enum chip_ids {
2020-05-14 23:48:59 +03:00
BMA023 ,
2020-05-14 23:49:01 +03:00
BMA150 ,
2014-08-20 02:43:00 +04:00
BMA180 ,
2014-08-20 02:43:00 +04:00
BMA250 ,
2019-12-12 00:38:19 +03:00
BMA254 ,
2014-08-20 02:43:00 +04:00
} ;
2014-08-20 02:43:00 +04:00
struct bma180_data ;
2014-08-20 02:43:00 +04:00
struct bma180_part_info {
2019-12-12 00:38:19 +03:00
u8 chip_id ;
2014-08-20 02:43:00 +04:00
const struct iio_chan_spec * channels ;
2017-03-17 02:35:45 +03:00
unsigned int num_channels ;
2014-08-20 02:43:00 +04:00
const int * scale_table ;
2017-03-17 02:35:45 +03:00
unsigned int num_scales ;
2014-08-20 02:43:00 +04:00
const int * bw_table ;
2017-03-17 02:35:45 +03:00
unsigned int num_bw ;
2020-05-14 23:49:00 +03:00
int temp_offset ;
2014-08-20 02:43:00 +04:00
u8 int_reset_reg , int_reset_mask ;
u8 sleep_reg , sleep_mask ;
u8 bw_reg , bw_mask ;
u8 scale_reg , scale_mask ;
u8 power_reg , power_mask , lowpower_val ;
u8 int_enable_reg , int_enable_mask ;
2019-12-12 00:38:19 +03:00
u8 int_map_reg , int_enable_dataready_int1_mask ;
2020-05-14 23:48:55 +03:00
u8 softreset_reg , softreset_val ;
2014-08-20 02:43:00 +04:00
2014-08-20 02:43:00 +04:00
int ( * chip_config ) ( struct bma180_data * data ) ;
void ( * chip_disable ) ( struct bma180_data * data ) ;
2014-08-20 02:43:00 +04:00
} ;
2013-08-28 15:01:00 +04:00
/* Register set */
2020-05-14 23:48:59 +03:00
# define BMA023_CTRL_REG0 0x0a
# define BMA023_CTRL_REG1 0x0b
# define BMA023_CTRL_REG2 0x14
# define BMA023_CTRL_REG3 0x15
# define BMA023_RANGE_MASK GENMASK(4, 3) /* Range of accel values */
# define BMA023_BW_MASK GENMASK(2, 0) /* Accel bandwidth */
# define BMA023_SLEEP BIT(0)
# define BMA023_INT_RESET_MASK BIT(6)
# define BMA023_NEW_DATA_INT BIT(5) /* Intr every new accel data is ready */
# define BMA023_RESET_VAL BIT(1)
2013-08-28 15:01:00 +04:00
# define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
# define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
2014-08-20 02:43:00 +04:00
# define BMA180_TEMP 0x08
2013-08-28 15:01:00 +04:00
# define BMA180_CTRL_REG0 0x0d
# define BMA180_RESET 0x10
# define BMA180_BW_TCS 0x20
# define BMA180_CTRL_REG3 0x21
# define BMA180_TCO_Z 0x30
# define BMA180_OFFSET_LSB1 0x35
/* BMA180_CTRL_REG0 bits */
# define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
# define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
# define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
# define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
/* BMA180_CTRL_REG3 bits */
# define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
/* BMA180_OFFSET_LSB1 skipping mode bit */
# define BMA180_SMP_SKIP BIT(0)
/* Bit masks for registers bit fields */
2014-08-20 02:43:00 +04:00
# define BMA180_RANGE 0x0e /* Range of measured accel values */
2013-08-28 15:01:00 +04:00
# define BMA180_BW 0xf0 /* Accel bandwidth */
# define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
/* We have to write this value in reset register to do soft reset */
# define BMA180_RESET_VAL 0xb6
2020-05-14 23:48:59 +03:00
# define BMA023_ID_REG_VAL 0x02
2014-08-20 02:43:00 +04:00
# define BMA180_ID_REG_VAL 0x03
2019-12-12 00:38:19 +03:00
# define BMA250_ID_REG_VAL 0x03
# define BMA254_ID_REG_VAL 0xfa /* 250 decimal */
2013-08-28 15:01:00 +04:00
/* Chip power modes */
# define BMA180_LOW_POWER 0x03
2014-08-20 02:43:00 +04:00
# define BMA250_RANGE_REG 0x0f
# define BMA250_BW_REG 0x10
# define BMA250_POWER_REG 0x11
# define BMA250_RESET_REG 0x14
# define BMA250_INT_ENABLE_REG 0x17
# define BMA250_INT_MAP_REG 0x1a
# define BMA250_INT_RESET_REG 0x21
# define BMA250_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
# define BMA250_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
# define BMA250_SUSPEND_MASK BIT(7) /* chip will sleep */
# define BMA250_LOWPOWER_MASK BIT(6)
# define BMA250_DATA_INTEN_MASK BIT(4)
# define BMA250_INT1_DATA_MASK BIT(0)
# define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
2013-08-28 15:01:00 +04:00
2019-12-12 00:38:19 +03:00
# define BMA254_RANGE_REG 0x0f
# define BMA254_BW_REG 0x10
# define BMA254_POWER_REG 0x11
# define BMA254_RESET_REG 0x14
# define BMA254_INT_ENABLE_REG 0x17
# define BMA254_INT_MAP_REG 0x1a
# define BMA254_INT_RESET_REG 0x21
# define BMA254_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
# define BMA254_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
# define BMA254_SUSPEND_MASK BIT(7) /* chip will sleep */
# define BMA254_LOWPOWER_MASK BIT(6)
# define BMA254_DATA_INTEN_MASK BIT(4)
# define BMA254_INT2_DATA_MASK BIT(7)
# define BMA254_INT1_DATA_MASK BIT(0)
# define BMA254_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
2013-08-28 15:01:00 +04:00
struct bma180_data {
2019-12-12 00:38:17 +03:00
struct regulator * vdd_supply ;
struct regulator * vddio_supply ;
2013-08-28 15:01:00 +04:00
struct i2c_client * client ;
struct iio_trigger * trig ;
2014-08-20 02:43:00 +04:00
const struct bma180_part_info * part_info ;
2019-02-21 20:02:49 +03:00
struct iio_mount_matrix orientation ;
2013-08-28 15:01:00 +04:00
struct mutex mutex ;
2014-08-20 02:43:00 +04:00
bool sleep_state ;
2013-08-28 15:01:00 +04:00
int scale ;
int bw ;
2014-08-20 02:43:00 +04:00
bool pmode ;
2014-08-20 02:43:00 +04:00
u8 buff [ 16 ] ; /* 3x 16-bit + 8-bit + padding + timestamp */
2013-08-28 15:01:00 +04:00
} ;
2014-08-20 02:43:00 +04:00
enum bma180_chan {
2013-08-28 15:01:00 +04:00
AXIS_X ,
AXIS_Y ,
AXIS_Z ,
2014-08-20 02:43:00 +04:00
TEMP
2013-08-28 15:01:00 +04:00
} ;
2020-05-14 23:48:59 +03:00
static int bma023_bw_table [ ] = { 25 , 50 , 100 , 190 , 375 , 750 , 1500 } ; /* Hz */
static int bma023_scale_table [ ] = { 2452 , 4903 , 9709 , } ;
2014-08-20 02:43:00 +04:00
static int bma180_bw_table [ ] = { 10 , 20 , 40 , 75 , 150 , 300 } ; /* Hz */
static int bma180_scale_table [ ] = { 1275 , 1863 , 2452 , 3727 , 4903 , 9709 , 19417 } ;
2013-08-28 15:01:00 +04:00
2019-12-12 00:38:19 +03:00
static int bma25x_bw_table [ ] = { 8 , 16 , 31 , 63 , 125 , 250 } ; /* Hz */
static int bma25x_scale_table [ ] = { 0 , 0 , 0 , 38344 , 0 , 76590 , 0 , 0 , 153180 , 0 ,
2014-08-20 02:43:00 +04:00
0 , 0 , 306458 } ;
2014-08-20 02:43:00 +04:00
static int bma180_get_data_reg ( struct bma180_data * data , enum bma180_chan chan )
2013-08-28 15:01:00 +04:00
{
int ret ;
if ( data - > sleep_state )
return - EBUSY ;
2014-08-20 02:43:00 +04:00
switch ( chan ) {
case TEMP :
ret = i2c_smbus_read_byte_data ( data - > client , BMA180_TEMP ) ;
if ( ret < 0 )
dev_err ( & data - > client - > dev , " failed to read temp register \n " ) ;
break ;
default :
ret = i2c_smbus_read_word_data ( data - > client ,
BMA180_ACC_X_LSB + chan * 2 ) ;
if ( ret < 0 )
dev_err ( & data - > client - > dev ,
" failed to read accel_%c register \n " ,
' x ' + chan ) ;
}
2013-08-28 15:01:00 +04:00
return ret ;
}
static int bma180_set_bits ( struct bma180_data * data , u8 reg , u8 mask , u8 val )
{
int ret = i2c_smbus_read_byte_data ( data - > client , reg ) ;
u8 reg_val = ( ret & ~ mask ) | ( val < < ( ffs ( mask ) - 1 ) ) ;
if ( ret < 0 )
return ret ;
return i2c_smbus_write_byte_data ( data - > client , reg , reg_val ) ;
}
static int bma180_reset_intr ( struct bma180_data * data )
{
2014-08-20 02:43:00 +04:00
int ret = bma180_set_bits ( data , data - > part_info - > int_reset_reg ,
data - > part_info - > int_reset_mask , 1 ) ;
2013-08-28 15:01:00 +04:00
if ( ret )
dev_err ( & data - > client - > dev , " failed to reset interrupt \n " ) ;
return ret ;
}
2014-08-20 02:43:00 +04:00
static int bma180_set_new_data_intr_state ( struct bma180_data * data , bool state )
2013-08-28 15:01:00 +04:00
{
2014-08-20 02:43:00 +04:00
int ret = bma180_set_bits ( data , data - > part_info - > int_enable_reg ,
data - > part_info - > int_enable_mask , state ) ;
2013-08-28 15:01:00 +04:00
if ( ret )
goto err ;
ret = bma180_reset_intr ( data ) ;
if ( ret )
goto err ;
return 0 ;
err :
dev_err ( & data - > client - > dev ,
" failed to set new data interrupt state %d \n " , state ) ;
return ret ;
}
2014-08-20 02:43:00 +04:00
static int bma180_set_sleep_state ( struct bma180_data * data , bool state )
2013-08-28 15:01:00 +04:00
{
2014-08-20 02:43:00 +04:00
int ret = bma180_set_bits ( data , data - > part_info - > sleep_reg ,
data - > part_info - > sleep_mask , state ) ;
2013-08-28 15:01:00 +04:00
if ( ret ) {
dev_err ( & data - > client - > dev ,
" failed to set sleep state %d \n " , state ) ;
return ret ;
}
data - > sleep_state = state ;
return 0 ;
}
2014-08-20 02:43:00 +04:00
static int bma180_set_ee_writing_state ( struct bma180_data * data , bool state )
2013-08-28 15:01:00 +04:00
{
int ret = bma180_set_bits ( data , BMA180_CTRL_REG0 , BMA180_EE_W , state ) ;
if ( ret )
dev_err ( & data - > client - > dev ,
" failed to set ee writing state %d \n " , state ) ;
return ret ;
}
static int bma180_set_bw ( struct bma180_data * data , int val )
{
int ret , i ;
if ( data - > sleep_state )
return - EBUSY ;
2014-08-20 02:43:00 +04:00
for ( i = 0 ; i < data - > part_info - > num_bw ; + + i ) {
if ( data - > part_info - > bw_table [ i ] = = val ) {
2014-08-20 02:43:00 +04:00
ret = bma180_set_bits ( data , data - > part_info - > bw_reg ,
data - > part_info - > bw_mask , i ) ;
2013-08-28 15:01:00 +04:00
if ( ret ) {
dev_err ( & data - > client - > dev ,
" failed to set bandwidth \n " ) ;
return ret ;
}
data - > bw = val ;
return 0 ;
}
}
return - EINVAL ;
}
static int bma180_set_scale ( struct bma180_data * data , int val )
{
int ret , i ;
if ( data - > sleep_state )
return - EBUSY ;
2014-08-20 02:43:00 +04:00
for ( i = 0 ; i < data - > part_info - > num_scales ; + + i )
if ( data - > part_info - > scale_table [ i ] = = val ) {
2014-08-20 02:43:00 +04:00
ret = bma180_set_bits ( data , data - > part_info - > scale_reg ,
data - > part_info - > scale_mask , i ) ;
2013-08-28 15:01:00 +04:00
if ( ret ) {
dev_err ( & data - > client - > dev ,
" failed to set scale \n " ) ;
return ret ;
}
data - > scale = val ;
return 0 ;
}
return - EINVAL ;
}
2014-08-20 02:43:00 +04:00
static int bma180_set_pmode ( struct bma180_data * data , bool mode )
2013-08-28 15:01:00 +04:00
{
2014-08-20 02:43:00 +04:00
u8 reg_val = mode ? data - > part_info - > lowpower_val : 0 ;
int ret = bma180_set_bits ( data , data - > part_info - > power_reg ,
data - > part_info - > power_mask , reg_val ) ;
2013-08-28 15:01:00 +04:00
if ( ret ) {
dev_err ( & data - > client - > dev , " failed to set power mode \n " ) ;
return ret ;
}
data - > pmode = mode ;
return 0 ;
}
static int bma180_soft_reset ( struct bma180_data * data )
{
int ret = i2c_smbus_write_byte_data ( data - > client ,
2020-05-14 23:48:55 +03:00
data - > part_info - > softreset_reg ,
data - > part_info - > softreset_val ) ;
2013-08-28 15:01:00 +04:00
if ( ret )
dev_err ( & data - > client - > dev , " failed to reset the chip \n " ) ;
return ret ;
}
static int bma180_chip_init ( struct bma180_data * data )
{
/* Try to read chip_id register. It must return 0x03. */
int ret = i2c_smbus_read_byte_data ( data - > client , BMA180_CHIP_ID ) ;
if ( ret < 0 )
2014-08-20 02:43:00 +04:00
return ret ;
2019-12-12 00:38:19 +03:00
if ( ret ! = data - > part_info - > chip_id ) {
dev_err ( & data - > client - > dev , " wrong chip ID %d expected %d \n " ,
ret , data - > part_info - > chip_id ) ;
2014-08-20 02:43:00 +04:00
return - ENODEV ;
2019-12-12 00:38:19 +03:00
}
2013-08-28 15:01:00 +04:00
ret = bma180_soft_reset ( data ) ;
if ( ret )
2014-08-20 02:43:00 +04:00
return ret ;
2013-08-28 15:01:00 +04:00
/*
* No serial transaction should occur within minimum 10 us
* after soft_reset command
*/
msleep ( 20 ) ;
2020-05-14 23:48:59 +03:00
return bma180_set_new_data_intr_state ( data , false ) ;
}
static int bma023_chip_config ( struct bma180_data * data )
{
int ret = bma180_chip_init ( data ) ;
2014-08-20 02:43:00 +04:00
if ( ret )
2020-05-14 23:48:59 +03:00
goto err ;
ret = bma180_set_bw ( data , 50 ) ; /* 50 Hz */
if ( ret )
goto err ;
ret = bma180_set_scale ( data , 2452 ) ; /* 2 G */
if ( ret )
goto err ;
2014-08-20 02:43:00 +04:00
2020-05-14 23:48:59 +03:00
return 0 ;
err :
dev_err ( & data - > client - > dev , " failed to config the chip \n " ) ;
return ret ;
2014-08-20 02:43:00 +04:00
}
static int bma180_chip_config ( struct bma180_data * data )
{
int ret = bma180_chip_init ( data ) ;
2020-05-14 23:48:59 +03:00
if ( ret )
goto err ;
ret = bma180_set_pmode ( data , false ) ;
2014-08-20 02:43:00 +04:00
if ( ret )
goto err ;
2013-08-28 15:01:00 +04:00
ret = bma180_set_bits ( data , BMA180_CTRL_REG0 , BMA180_DIS_WAKE_UP , 1 ) ;
if ( ret )
goto err ;
2014-08-20 02:43:00 +04:00
ret = bma180_set_ee_writing_state ( data , true ) ;
2013-08-28 15:01:00 +04:00
if ( ret )
goto err ;
2014-08-20 02:43:00 +04:00
ret = bma180_set_bits ( data , BMA180_OFFSET_LSB1 , BMA180_SMP_SKIP , 1 ) ;
2013-08-28 15:01:00 +04:00
if ( ret )
goto err ;
2014-08-20 02:43:00 +04:00
ret = bma180_set_bw ( data , 20 ) ; /* 20 Hz */
2013-08-28 15:01:00 +04:00
if ( ret )
goto err ;
2014-08-20 02:43:00 +04:00
ret = bma180_set_scale ( data , 2452 ) ; /* 2 G */
2013-08-28 15:01:00 +04:00
if ( ret )
goto err ;
2014-08-20 02:43:00 +04:00
return 0 ;
err :
dev_err ( & data - > client - > dev , " failed to config the chip \n " ) ;
return ret ;
}
2019-12-12 00:38:19 +03:00
static int bma25x_chip_config ( struct bma180_data * data )
2014-08-20 02:43:00 +04:00
{
int ret = bma180_chip_init ( data ) ;
2020-05-14 23:48:59 +03:00
if ( ret )
goto err ;
ret = bma180_set_pmode ( data , false ) ;
2014-08-20 02:43:00 +04:00
if ( ret )
goto err ;
ret = bma180_set_bw ( data , 16 ) ; /* 16 Hz */
2013-08-28 15:01:00 +04:00
if ( ret )
goto err ;
2014-08-20 02:43:00 +04:00
ret = bma180_set_scale ( data , 38344 ) ; /* 2 G */
if ( ret )
goto err ;
2019-12-12 00:38:19 +03:00
/*
* This enables dataready interrupt on the INT1 pin
* FIXME : support using the INT2 pin
*/
ret = bma180_set_bits ( data , data - > part_info - > int_map_reg ,
data - > part_info - > int_enable_dataready_int1_mask , 1 ) ;
2013-08-28 15:01:00 +04:00
if ( ret )
goto err ;
return 0 ;
err :
2014-08-20 02:43:00 +04:00
dev_err ( & data - > client - > dev , " failed to config the chip \n " ) ;
2013-08-28 15:01:00 +04:00
return ret ;
}
2020-05-14 23:48:59 +03:00
static void bma023_chip_disable ( struct bma180_data * data )
{
if ( bma180_set_sleep_state ( data , true ) )
goto err ;
return ;
err :
dev_err ( & data - > client - > dev , " failed to disable the chip \n " ) ;
}
2013-08-28 15:01:00 +04:00
static void bma180_chip_disable ( struct bma180_data * data )
{
2014-08-20 02:43:00 +04:00
if ( bma180_set_new_data_intr_state ( data , false ) )
2013-08-28 15:01:00 +04:00
goto err ;
2014-08-20 02:43:00 +04:00
if ( bma180_set_ee_writing_state ( data , false ) )
2013-08-28 15:01:00 +04:00
goto err ;
2014-08-20 02:43:00 +04:00
if ( bma180_set_sleep_state ( data , true ) )
2013-08-28 15:01:00 +04:00
goto err ;
return ;
err :
dev_err ( & data - > client - > dev , " failed to disable the chip \n " ) ;
}
2019-12-12 00:38:19 +03:00
static void bma25x_chip_disable ( struct bma180_data * data )
2014-08-20 02:43:00 +04:00
{
if ( bma180_set_new_data_intr_state ( data , false ) )
goto err ;
if ( bma180_set_sleep_state ( data , true ) )
goto err ;
return ;
err :
dev_err ( & data - > client - > dev , " failed to disable the chip \n " ) ;
}
2017-03-17 02:35:45 +03:00
static ssize_t bma180_show_avail ( char * buf , const int * vals , unsigned int n ,
2014-08-20 02:43:00 +04:00
bool micros )
{
size_t len = 0 ;
int i ;
for ( i = 0 ; i < n ; i + + ) {
if ( ! vals [ i ] )
continue ;
len + = scnprintf ( buf + len , PAGE_SIZE - len ,
micros ? " 0.%06d " : " %d " , vals [ i ] ) ;
}
buf [ len - 1 ] = ' \n ' ;
return len ;
}
static ssize_t bma180_show_filter_freq_avail ( struct device * dev ,
struct device_attribute * attr , char * buf )
{
struct bma180_data * data = iio_priv ( dev_to_iio_dev ( dev ) ) ;
return bma180_show_avail ( buf , data - > part_info - > bw_table ,
data - > part_info - > num_bw , false ) ;
}
static ssize_t bma180_show_scale_avail ( struct device * dev ,
struct device_attribute * attr , char * buf )
{
struct bma180_data * data = iio_priv ( dev_to_iio_dev ( dev ) ) ;
return bma180_show_avail ( buf , data - > part_info - > scale_table ,
data - > part_info - > num_scales , true ) ;
}
static IIO_DEVICE_ATTR ( in_accel_filter_low_pass_3db_frequency_available ,
S_IRUGO , bma180_show_filter_freq_avail , NULL , 0 ) ;
static IIO_DEVICE_ATTR ( in_accel_scale_available ,
S_IRUGO , bma180_show_scale_avail , NULL , 0 ) ;
2013-08-28 15:01:00 +04:00
static struct attribute * bma180_attributes [ ] = {
2014-08-20 02:43:00 +04:00
& iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available .
dev_attr . attr ,
& iio_dev_attr_in_accel_scale_available . dev_attr . attr ,
2013-08-28 15:01:00 +04:00
NULL ,
} ;
static const struct attribute_group bma180_attrs_group = {
. attrs = bma180_attributes ,
} ;
static int bma180_read_raw ( struct iio_dev * indio_dev ,
struct iio_chan_spec const * chan , int * val , int * val2 ,
long mask )
{
struct bma180_data * data = iio_priv ( indio_dev ) ;
int ret ;
switch ( mask ) {
case IIO_CHAN_INFO_RAW :
2016-07-25 22:11:20 +03:00
ret = iio_device_claim_direct_mode ( indio_dev ) ;
if ( ret )
return ret ;
2013-08-28 15:01:00 +04:00
mutex_lock ( & data - > mutex ) ;
2014-08-20 02:43:00 +04:00
ret = bma180_get_data_reg ( data , chan - > scan_index ) ;
2013-08-28 15:01:00 +04:00
mutex_unlock ( & data - > mutex ) ;
2016-07-25 22:11:20 +03:00
iio_device_release_direct_mode ( indio_dev ) ;
2013-08-28 15:01:00 +04:00
if ( ret < 0 )
return ret ;
2020-05-14 23:49:01 +03:00
if ( chan - > scan_type . sign = = ' s ' ) {
* val = sign_extend32 ( ret > > chan - > scan_type . shift ,
chan - > scan_type . realbits - 1 ) ;
} else {
* val = ret ;
}
2013-08-28 15:01:00 +04:00
return IIO_VAL_INT ;
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY :
* val = data - > bw ;
return IIO_VAL_INT ;
case IIO_CHAN_INFO_SCALE :
2014-08-20 02:43:00 +04:00
switch ( chan - > type ) {
case IIO_ACCEL :
* val = 0 ;
* val2 = data - > scale ;
return IIO_VAL_INT_PLUS_MICRO ;
case IIO_TEMP :
* val = 500 ;
return IIO_VAL_INT ;
default :
return - EINVAL ;
}
case IIO_CHAN_INFO_OFFSET :
2020-05-14 23:49:00 +03:00
* val = data - > part_info - > temp_offset ;
2014-08-20 02:43:00 +04:00
return IIO_VAL_INT ;
2013-08-28 15:01:00 +04:00
default :
return - EINVAL ;
}
}
static int bma180_write_raw ( struct iio_dev * indio_dev ,
struct iio_chan_spec const * chan , int val , int val2 , long mask )
{
struct bma180_data * data = iio_priv ( indio_dev ) ;
int ret ;
switch ( mask ) {
case IIO_CHAN_INFO_SCALE :
if ( val )
return - EINVAL ;
mutex_lock ( & data - > mutex ) ;
ret = bma180_set_scale ( data , val2 ) ;
mutex_unlock ( & data - > mutex ) ;
return ret ;
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY :
2014-07-16 22:32:00 +04:00
if ( val2 )
return - EINVAL ;
2013-08-28 15:01:00 +04:00
mutex_lock ( & data - > mutex ) ;
ret = bma180_set_bw ( data , val ) ;
mutex_unlock ( & data - > mutex ) ;
return ret ;
default :
return - EINVAL ;
}
}
static const struct iio_info bma180_info = {
. attrs = & bma180_attrs_group ,
. read_raw = bma180_read_raw ,
. write_raw = bma180_write_raw ,
} ;
2014-08-20 02:43:00 +04:00
static const char * const bma180_power_modes [ ] = { " low_noise " , " low_power " } ;
2013-08-28 15:01:00 +04:00
static int bma180_get_power_mode ( struct iio_dev * indio_dev ,
const struct iio_chan_spec * chan )
{
struct bma180_data * data = iio_priv ( indio_dev ) ;
return data - > pmode ;
}
static int bma180_set_power_mode ( struct iio_dev * indio_dev ,
const struct iio_chan_spec * chan , unsigned int mode )
{
struct bma180_data * data = iio_priv ( indio_dev ) ;
int ret ;
mutex_lock ( & data - > mutex ) ;
ret = bma180_set_pmode ( data , mode ) ;
mutex_unlock ( & data - > mutex ) ;
return ret ;
}
2019-02-21 20:02:49 +03:00
static const struct iio_mount_matrix *
bma180_accel_get_mount_matrix ( const struct iio_dev * indio_dev ,
const struct iio_chan_spec * chan )
{
struct bma180_data * data = iio_priv ( indio_dev ) ;
return & data - > orientation ;
}
2013-08-28 15:01:00 +04:00
static const struct iio_enum bma180_power_mode_enum = {
. items = bma180_power_modes ,
. num_items = ARRAY_SIZE ( bma180_power_modes ) ,
. get = bma180_get_power_mode ,
. set = bma180_set_power_mode ,
} ;
2020-05-14 23:48:59 +03:00
static const struct iio_chan_spec_ext_info bma023_ext_info [ ] = {
IIO_MOUNT_MATRIX ( IIO_SHARED_BY_DIR , bma180_accel_get_mount_matrix ) ,
{ }
} ;
2013-08-28 15:01:00 +04:00
static const struct iio_chan_spec_ext_info bma180_ext_info [ ] = {
2020-09-13 15:12:27 +03:00
IIO_ENUM ( " power_mode " , IIO_SHARED_BY_TYPE , & bma180_power_mode_enum ) ,
2013-08-28 15:01:00 +04:00
IIO_ENUM_AVAILABLE ( " power_mode " , & bma180_power_mode_enum ) ,
2019-02-21 20:02:49 +03:00
IIO_MOUNT_MATRIX ( IIO_SHARED_BY_DIR , bma180_accel_get_mount_matrix ) ,
{ }
2013-08-28 15:01:00 +04:00
} ;
2020-05-14 23:48:59 +03:00
# define BMA023_ACC_CHANNEL(_axis, _bits) { \
. type = IIO_ACCEL , \
. modified = 1 , \
. channel2 = IIO_MOD_ # # _axis , \
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) , \
. info_mask_shared_by_type = BIT ( IIO_CHAN_INFO_SCALE ) | \
BIT ( IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY ) , \
. scan_index = AXIS_ # # _axis , \
. scan_type = { \
. sign = ' s ' , \
. realbits = _bits , \
. storagebits = 16 , \
. shift = 16 - _bits , \
} , \
. ext_info = bma023_ext_info , \
}
2020-05-14 23:49:01 +03:00
# define BMA150_TEMP_CHANNEL { \
. type = IIO_TEMP , \
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) | \
BIT ( IIO_CHAN_INFO_SCALE ) | BIT ( IIO_CHAN_INFO_OFFSET ) , \
. scan_index = TEMP , \
. scan_type = { \
. sign = ' u ' , \
. realbits = 8 , \
. storagebits = 16 , \
} , \
}
2014-08-20 02:43:00 +04:00
# define BMA180_ACC_CHANNEL(_axis, _bits) { \
2013-08-28 15:01:00 +04:00
. type = IIO_ACCEL , \
2014-10-02 00:37:00 +04:00
. modified = 1 , \
. channel2 = IIO_MOD_ # # _axis , \
2014-10-02 00:37:00 +04:00
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) , \
. info_mask_shared_by_type = BIT ( IIO_CHAN_INFO_SCALE ) | \
2013-08-28 15:01:00 +04:00
BIT ( IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY ) , \
2014-10-02 00:37:00 +04:00
. scan_index = AXIS_ # # _axis , \
2013-12-11 22:45:00 +04:00
. scan_type = { \
. sign = ' s ' , \
2014-08-20 02:43:00 +04:00
. realbits = _bits , \
2013-12-11 22:45:00 +04:00
. storagebits = 16 , \
2014-08-20 02:43:00 +04:00
. shift = 16 - _bits , \
2013-12-11 22:45:00 +04:00
} , \
2013-08-28 15:01:00 +04:00
. ext_info = bma180_ext_info , \
}
2014-08-20 02:43:00 +04:00
# define BMA180_TEMP_CHANNEL { \
. type = IIO_TEMP , \
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) | \
BIT ( IIO_CHAN_INFO_SCALE ) | BIT ( IIO_CHAN_INFO_OFFSET ) , \
. scan_index = TEMP , \
. scan_type = { \
. sign = ' s ' , \
. realbits = 8 , \
. storagebits = 16 , \
} , \
}
2020-05-14 23:48:59 +03:00
static const struct iio_chan_spec bma023_channels [ ] = {
BMA023_ACC_CHANNEL ( X , 10 ) ,
BMA023_ACC_CHANNEL ( Y , 10 ) ,
BMA023_ACC_CHANNEL ( Z , 10 ) ,
IIO_CHAN_SOFT_TIMESTAMP ( 4 ) ,
} ;
2020-05-14 23:49:01 +03:00
static const struct iio_chan_spec bma150_channels [ ] = {
BMA023_ACC_CHANNEL ( X , 10 ) ,
BMA023_ACC_CHANNEL ( Y , 10 ) ,
BMA023_ACC_CHANNEL ( Z , 10 ) ,
BMA150_TEMP_CHANNEL ,
IIO_CHAN_SOFT_TIMESTAMP ( 4 ) ,
} ;
2013-08-28 15:01:00 +04:00
static const struct iio_chan_spec bma180_channels [ ] = {
2014-08-20 02:43:00 +04:00
BMA180_ACC_CHANNEL ( X , 14 ) ,
BMA180_ACC_CHANNEL ( Y , 14 ) ,
BMA180_ACC_CHANNEL ( Z , 14 ) ,
2014-08-20 02:43:00 +04:00
BMA180_TEMP_CHANNEL ,
IIO_CHAN_SOFT_TIMESTAMP ( 4 ) ,
2013-08-28 15:01:00 +04:00
} ;
2014-08-20 02:43:00 +04:00
static const struct iio_chan_spec bma250_channels [ ] = {
BMA180_ACC_CHANNEL ( X , 10 ) ,
BMA180_ACC_CHANNEL ( Y , 10 ) ,
BMA180_ACC_CHANNEL ( Z , 10 ) ,
BMA180_TEMP_CHANNEL ,
IIO_CHAN_SOFT_TIMESTAMP ( 4 ) ,
} ;
2019-12-12 00:38:19 +03:00
static const struct iio_chan_spec bma254_channels [ ] = {
BMA180_ACC_CHANNEL ( X , 12 ) ,
BMA180_ACC_CHANNEL ( Y , 12 ) ,
BMA180_ACC_CHANNEL ( Z , 12 ) ,
BMA180_TEMP_CHANNEL ,
IIO_CHAN_SOFT_TIMESTAMP ( 4 ) ,
} ;
2014-08-20 02:43:00 +04:00
static const struct bma180_part_info bma180_part_info [ ] = {
2020-05-14 23:48:59 +03:00
[ BMA023 ] = {
. chip_id = BMA023_ID_REG_VAL ,
. channels = bma023_channels ,
. num_channels = ARRAY_SIZE ( bma023_channels ) ,
. scale_table = bma023_scale_table ,
. num_scales = ARRAY_SIZE ( bma023_scale_table ) ,
. bw_table = bma023_bw_table ,
. num_bw = ARRAY_SIZE ( bma023_bw_table ) ,
/* No temperature channel */
2020-05-14 23:49:00 +03:00
. temp_offset = 0 ,
2020-05-14 23:48:59 +03:00
. int_reset_reg = BMA023_CTRL_REG0 ,
. int_reset_mask = BMA023_INT_RESET_MASK ,
. sleep_reg = BMA023_CTRL_REG0 ,
. sleep_mask = BMA023_SLEEP ,
. bw_reg = BMA023_CTRL_REG2 ,
. bw_mask = BMA023_BW_MASK ,
. scale_reg = BMA023_CTRL_REG2 ,
. scale_mask = BMA023_RANGE_MASK ,
/* No power mode on bma023 */
. power_reg = 0 ,
. power_mask = 0 ,
. lowpower_val = 0 ,
. int_enable_reg = BMA023_CTRL_REG3 ,
. int_enable_mask = BMA023_NEW_DATA_INT ,
. softreset_reg = BMA023_CTRL_REG0 ,
. softreset_val = BMA023_RESET_VAL ,
. chip_config = bma023_chip_config ,
. chip_disable = bma023_chip_disable ,
} ,
2020-05-14 23:49:01 +03:00
[ BMA150 ] = {
. chip_id = BMA023_ID_REG_VAL ,
. channels = bma150_channels ,
. num_channels = ARRAY_SIZE ( bma150_channels ) ,
. scale_table = bma023_scale_table ,
. num_scales = ARRAY_SIZE ( bma023_scale_table ) ,
. bw_table = bma023_bw_table ,
. num_bw = ARRAY_SIZE ( bma023_bw_table ) ,
. temp_offset = - 60 , /* 0 LSB @ -30 degree C */
. int_reset_reg = BMA023_CTRL_REG0 ,
. int_reset_mask = BMA023_INT_RESET_MASK ,
. sleep_reg = BMA023_CTRL_REG0 ,
. sleep_mask = BMA023_SLEEP ,
. bw_reg = BMA023_CTRL_REG2 ,
. bw_mask = BMA023_BW_MASK ,
. scale_reg = BMA023_CTRL_REG2 ,
. scale_mask = BMA023_RANGE_MASK ,
/* No power mode on bma150 */
. power_reg = 0 ,
. power_mask = 0 ,
. lowpower_val = 0 ,
. int_enable_reg = BMA023_CTRL_REG3 ,
. int_enable_mask = BMA023_NEW_DATA_INT ,
. softreset_reg = BMA023_CTRL_REG0 ,
. softreset_val = BMA023_RESET_VAL ,
. chip_config = bma023_chip_config ,
. chip_disable = bma023_chip_disable ,
} ,
2014-08-20 02:43:00 +04:00
[ BMA180 ] = {
2019-12-12 00:38:19 +03:00
. chip_id = BMA180_ID_REG_VAL ,
2019-12-12 00:38:18 +03:00
. channels = bma180_channels ,
. num_channels = ARRAY_SIZE ( bma180_channels ) ,
. scale_table = bma180_scale_table ,
. num_scales = ARRAY_SIZE ( bma180_scale_table ) ,
. bw_table = bma180_bw_table ,
. num_bw = ARRAY_SIZE ( bma180_bw_table ) ,
2020-05-14 23:49:00 +03:00
. temp_offset = 48 , /* 0 LSB @ 24 degree C */
2019-12-12 00:38:18 +03:00
. int_reset_reg = BMA180_CTRL_REG0 ,
. int_reset_mask = BMA180_RESET_INT ,
. sleep_reg = BMA180_CTRL_REG0 ,
. sleep_mask = BMA180_SLEEP ,
. bw_reg = BMA180_BW_TCS ,
. bw_mask = BMA180_BW ,
. scale_reg = BMA180_OFFSET_LSB1 ,
. scale_mask = BMA180_RANGE ,
. power_reg = BMA180_TCO_Z ,
. power_mask = BMA180_MODE_CONFIG ,
. lowpower_val = BMA180_LOW_POWER ,
. int_enable_reg = BMA180_CTRL_REG3 ,
. int_enable_mask = BMA180_NEW_DATA_INT ,
. softreset_reg = BMA180_RESET ,
2020-05-14 23:48:55 +03:00
. softreset_val = BMA180_RESET_VAL ,
2019-12-12 00:38:18 +03:00
. chip_config = bma180_chip_config ,
. chip_disable = bma180_chip_disable ,
2014-08-20 02:43:00 +04:00
} ,
2014-08-20 02:43:00 +04:00
[ BMA250 ] = {
2019-12-12 00:38:19 +03:00
. chip_id = BMA250_ID_REG_VAL ,
2019-12-12 00:38:18 +03:00
. channels = bma250_channels ,
. num_channels = ARRAY_SIZE ( bma250_channels ) ,
2019-12-12 00:38:19 +03:00
. scale_table = bma25x_scale_table ,
. num_scales = ARRAY_SIZE ( bma25x_scale_table ) ,
. bw_table = bma25x_bw_table ,
. num_bw = ARRAY_SIZE ( bma25x_bw_table ) ,
2020-05-14 23:49:00 +03:00
. temp_offset = 48 , /* 0 LSB @ 24 degree C */
2019-12-12 00:38:18 +03:00
. int_reset_reg = BMA250_INT_RESET_REG ,
. int_reset_mask = BMA250_INT_RESET_MASK ,
. sleep_reg = BMA250_POWER_REG ,
. sleep_mask = BMA250_SUSPEND_MASK ,
. bw_reg = BMA250_BW_REG ,
. bw_mask = BMA250_BW_MASK ,
. scale_reg = BMA250_RANGE_REG ,
. scale_mask = BMA250_RANGE_MASK ,
. power_reg = BMA250_POWER_REG ,
. power_mask = BMA250_LOWPOWER_MASK ,
. lowpower_val = 1 ,
. int_enable_reg = BMA250_INT_ENABLE_REG ,
. int_enable_mask = BMA250_DATA_INTEN_MASK ,
2019-12-12 00:38:19 +03:00
. int_map_reg = BMA250_INT_MAP_REG ,
. int_enable_dataready_int1_mask = BMA250_INT1_DATA_MASK ,
2019-12-12 00:38:18 +03:00
. softreset_reg = BMA250_RESET_REG ,
2020-05-14 23:48:55 +03:00
. softreset_val = BMA180_RESET_VAL ,
2019-12-12 00:38:19 +03:00
. chip_config = bma25x_chip_config ,
. chip_disable = bma25x_chip_disable ,
} ,
[ BMA254 ] = {
. chip_id = BMA254_ID_REG_VAL ,
. channels = bma254_channels ,
. num_channels = ARRAY_SIZE ( bma254_channels ) ,
. scale_table = bma25x_scale_table ,
. num_scales = ARRAY_SIZE ( bma25x_scale_table ) ,
. bw_table = bma25x_bw_table ,
. num_bw = ARRAY_SIZE ( bma25x_bw_table ) ,
2020-05-14 23:49:00 +03:00
. temp_offset = 46 , /* 0 LSB @ 23 degree C */
2019-12-12 00:38:19 +03:00
. int_reset_reg = BMA254_INT_RESET_REG ,
. int_reset_mask = BMA254_INT_RESET_MASK ,
. sleep_reg = BMA254_POWER_REG ,
. sleep_mask = BMA254_SUSPEND_MASK ,
. bw_reg = BMA254_BW_REG ,
. bw_mask = BMA254_BW_MASK ,
. scale_reg = BMA254_RANGE_REG ,
. scale_mask = BMA254_RANGE_MASK ,
. power_reg = BMA254_POWER_REG ,
. power_mask = BMA254_LOWPOWER_MASK ,
. lowpower_val = 1 ,
. int_enable_reg = BMA254_INT_ENABLE_REG ,
. int_enable_mask = BMA254_DATA_INTEN_MASK ,
. int_map_reg = BMA254_INT_MAP_REG ,
. int_enable_dataready_int1_mask = BMA254_INT1_DATA_MASK ,
. softreset_reg = BMA254_RESET_REG ,
2020-05-14 23:48:55 +03:00
. softreset_val = BMA180_RESET_VAL ,
2019-12-12 00:38:19 +03:00
. chip_config = bma25x_chip_config ,
. chip_disable = bma25x_chip_disable ,
2014-08-20 02:43:00 +04:00
} ,
2014-08-20 02:43:00 +04:00
} ;
2013-08-28 15:01:00 +04:00
static irqreturn_t bma180_trigger_handler ( int irq , void * p )
{
struct iio_poll_func * pf = p ;
struct iio_dev * indio_dev = pf - > indio_dev ;
struct bma180_data * data = iio_priv ( indio_dev ) ;
2016-03-09 21:05:49 +03:00
s64 time_ns = iio_get_time_ns ( indio_dev ) ;
2013-08-28 15:01:00 +04:00
int bit , ret , i = 0 ;
mutex_lock ( & data - > mutex ) ;
2015-03-02 22:03:05 +03:00
for_each_set_bit ( bit , indio_dev - > active_scan_mask ,
2013-08-28 15:01:00 +04:00
indio_dev - > masklength ) {
2014-08-20 02:43:00 +04:00
ret = bma180_get_data_reg ( data , bit ) ;
2013-08-28 15:01:00 +04:00
if ( ret < 0 ) {
mutex_unlock ( & data - > mutex ) ;
goto err ;
}
( ( s16 * ) data - > buff ) [ i + + ] = ret ;
}
2014-08-20 02:43:00 +04:00
2013-08-28 15:01:00 +04:00
mutex_unlock ( & data - > mutex ) ;
2013-09-19 16:59:00 +04:00
iio_push_to_buffers_with_timestamp ( indio_dev , data - > buff , time_ns ) ;
2013-08-28 15:01:00 +04:00
err :
iio_trigger_notify_done ( indio_dev - > trig ) ;
return IRQ_HANDLED ;
}
static int bma180_data_rdy_trigger_set_state ( struct iio_trigger * trig ,
bool state )
{
struct iio_dev * indio_dev = iio_trigger_get_drvdata ( trig ) ;
struct bma180_data * data = iio_priv ( indio_dev ) ;
return bma180_set_new_data_intr_state ( data , state ) ;
}
static int bma180_trig_try_reen ( struct iio_trigger * trig )
{
struct iio_dev * indio_dev = iio_trigger_get_drvdata ( trig ) ;
struct bma180_data * data = iio_priv ( indio_dev ) ;
return bma180_reset_intr ( data ) ;
}
static const struct iio_trigger_ops bma180_trigger_ops = {
. set_trigger_state = bma180_data_rdy_trigger_set_state ,
. try_reenable = bma180_trig_try_reen ,
} ;
static int bma180_probe ( struct i2c_client * client ,
const struct i2c_device_id * id )
{
2019-12-12 00:38:16 +03:00
struct device * dev = & client - > dev ;
2013-08-28 15:01:00 +04:00
struct bma180_data * data ;
struct iio_dev * indio_dev ;
2017-03-15 07:44:57 +03:00
enum chip_ids chip ;
2013-08-28 15:01:00 +04:00
int ret ;
2019-12-12 00:38:16 +03:00
indio_dev = devm_iio_device_alloc ( dev , sizeof ( * data ) ) ;
2013-08-28 15:01:00 +04:00
if ( ! indio_dev )
return - ENOMEM ;
data = iio_priv ( indio_dev ) ;
i2c_set_clientdata ( client , indio_dev ) ;
data - > client = client ;
2017-03-15 07:44:57 +03:00
if ( client - > dev . of_node )
2019-12-12 00:38:16 +03:00
chip = ( enum chip_ids ) of_device_get_match_data ( dev ) ;
2017-03-15 07:44:57 +03:00
else
chip = id - > driver_data ;
data - > part_info = & bma180_part_info [ chip ] ;
2013-08-28 15:01:00 +04:00
2019-12-12 00:38:16 +03:00
ret = iio_read_mount_matrix ( dev , " mount-matrix " ,
2019-02-21 20:02:49 +03:00
& data - > orientation ) ;
if ( ret )
return ret ;
2019-12-12 00:38:17 +03:00
data - > vdd_supply = devm_regulator_get ( dev , " vdd " ) ;
2020-08-29 09:47:09 +03:00
if ( IS_ERR ( data - > vdd_supply ) )
return dev_err_probe ( dev , PTR_ERR ( data - > vdd_supply ) ,
" Failed to get vdd regulator \n " ) ;
2019-12-12 00:38:17 +03:00
data - > vddio_supply = devm_regulator_get ( dev , " vddio " ) ;
2020-08-29 09:47:09 +03:00
if ( IS_ERR ( data - > vddio_supply ) )
return dev_err_probe ( dev , PTR_ERR ( data - > vddio_supply ) ,
" Failed to get vddio regulator \n " ) ;
2019-12-12 00:38:17 +03:00
/* Typical voltage 2.4V these are min and max */
ret = regulator_set_voltage ( data - > vdd_supply , 1620000 , 3600000 ) ;
if ( ret )
return ret ;
ret = regulator_set_voltage ( data - > vddio_supply , 1200000 , 3600000 ) ;
if ( ret )
return ret ;
ret = regulator_enable ( data - > vdd_supply ) ;
if ( ret ) {
dev_err ( dev , " Failed to enable vdd regulator: %d \n " , ret ) ;
return ret ;
}
ret = regulator_enable ( data - > vddio_supply ) ;
if ( ret ) {
dev_err ( dev , " Failed to enable vddio regulator: %d \n " , ret ) ;
goto err_disable_vdd ;
}
/* Wait to make sure we started up properly (3 ms at least) */
usleep_range ( 3000 , 5000 ) ;
2014-08-20 02:43:00 +04:00
ret = data - > part_info - > chip_config ( data ) ;
2013-08-28 15:01:00 +04:00
if ( ret < 0 )
goto err_chip_disable ;
mutex_init ( & data - > mutex ) ;
2014-08-20 02:43:00 +04:00
indio_dev - > channels = data - > part_info - > channels ;
indio_dev - > num_channels = data - > part_info - > num_channels ;
2014-08-20 02:43:00 +04:00
indio_dev - > name = id - > name ;
2013-08-28 15:01:00 +04:00
indio_dev - > modes = INDIO_DIRECT_MODE ;
indio_dev - > info = & bma180_info ;
2014-08-20 02:43:00 +04:00
if ( client - > irq > 0 ) {
data - > trig = iio_trigger_alloc ( " %s-dev%d " , indio_dev - > name ,
indio_dev - > id ) ;
if ( ! data - > trig ) {
ret = - ENOMEM ;
goto err_chip_disable ;
}
2013-08-28 15:01:00 +04:00
2019-12-12 00:38:16 +03:00
ret = devm_request_irq ( dev , client - > irq ,
2014-08-20 02:43:00 +04:00
iio_trigger_generic_data_rdy_poll , IRQF_TRIGGER_RISING ,
2014-08-20 02:43:00 +04:00
" bma180_event " , data - > trig ) ;
2014-08-20 02:43:00 +04:00
if ( ret ) {
2019-12-12 00:38:16 +03:00
dev_err ( dev , " unable to request IRQ \n " ) ;
2014-08-20 02:43:00 +04:00
goto err_trigger_free ;
}
2013-08-28 15:01:00 +04:00
2019-12-12 00:38:16 +03:00
data - > trig - > dev . parent = dev ;
2014-08-20 02:43:00 +04:00
data - > trig - > ops = & bma180_trigger_ops ;
iio_trigger_set_drvdata ( data - > trig , indio_dev ) ;
2014-09-22 18:54:10 +04:00
indio_dev - > trig = iio_trigger_get ( data - > trig ) ;
2013-08-28 15:01:00 +04:00
2014-08-20 02:43:00 +04:00
ret = iio_trigger_register ( data - > trig ) ;
if ( ret )
goto err_trigger_free ;
}
2013-08-28 15:01:00 +04:00
ret = iio_triggered_buffer_setup ( indio_dev , NULL ,
bma180_trigger_handler , NULL ) ;
if ( ret < 0 ) {
2019-12-12 00:38:16 +03:00
dev_err ( dev , " unable to setup iio triggered buffer \n " ) ;
2013-08-28 15:01:00 +04:00
goto err_trigger_unregister ;
}
ret = iio_device_register ( indio_dev ) ;
if ( ret < 0 ) {
2019-12-12 00:38:16 +03:00
dev_err ( dev , " unable to register iio device \n " ) ;
2013-08-28 15:01:00 +04:00
goto err_buffer_cleanup ;
}
return 0 ;
err_buffer_cleanup :
iio_triggered_buffer_cleanup ( indio_dev ) ;
err_trigger_unregister :
2014-08-20 02:43:00 +04:00
if ( data - > trig )
iio_trigger_unregister ( data - > trig ) ;
2013-08-28 15:01:00 +04:00
err_trigger_free :
2014-08-20 02:43:00 +04:00
iio_trigger_free ( data - > trig ) ;
2013-08-28 15:01:00 +04:00
err_chip_disable :
2014-08-20 02:43:00 +04:00
data - > part_info - > chip_disable ( data ) ;
2019-12-12 00:38:17 +03:00
regulator_disable ( data - > vddio_supply ) ;
err_disable_vdd :
regulator_disable ( data - > vdd_supply ) ;
2013-08-28 15:01:00 +04:00
return ret ;
}
static int bma180_remove ( struct i2c_client * client )
{
struct iio_dev * indio_dev = i2c_get_clientdata ( client ) ;
struct bma180_data * data = iio_priv ( indio_dev ) ;
iio_device_unregister ( indio_dev ) ;
iio_triggered_buffer_cleanup ( indio_dev ) ;
2014-08-20 02:43:00 +04:00
if ( data - > trig ) {
iio_trigger_unregister ( data - > trig ) ;
iio_trigger_free ( data - > trig ) ;
}
2013-08-28 15:01:00 +04:00
mutex_lock ( & data - > mutex ) ;
2014-08-20 02:43:00 +04:00
data - > part_info - > chip_disable ( data ) ;
2013-08-28 15:01:00 +04:00
mutex_unlock ( & data - > mutex ) ;
2019-12-12 00:38:17 +03:00
regulator_disable ( data - > vddio_supply ) ;
regulator_disable ( data - > vdd_supply ) ;
2013-08-28 15:01:00 +04:00
return 0 ;
}
# ifdef CONFIG_PM_SLEEP
static int bma180_suspend ( struct device * dev )
{
2013-09-19 01:47:00 +04:00
struct iio_dev * indio_dev = i2c_get_clientdata ( to_i2c_client ( dev ) ) ;
2013-08-28 15:01:00 +04:00
struct bma180_data * data = iio_priv ( indio_dev ) ;
int ret ;
mutex_lock ( & data - > mutex ) ;
2014-08-20 02:43:00 +04:00
ret = bma180_set_sleep_state ( data , true ) ;
2013-08-28 15:01:00 +04:00
mutex_unlock ( & data - > mutex ) ;
return ret ;
}
static int bma180_resume ( struct device * dev )
{
2013-09-19 01:47:00 +04:00
struct iio_dev * indio_dev = i2c_get_clientdata ( to_i2c_client ( dev ) ) ;
2013-08-28 15:01:00 +04:00
struct bma180_data * data = iio_priv ( indio_dev ) ;
int ret ;
mutex_lock ( & data - > mutex ) ;
2014-08-20 02:43:00 +04:00
ret = bma180_set_sleep_state ( data , false ) ;
2013-08-28 15:01:00 +04:00
mutex_unlock ( & data - > mutex ) ;
return ret ;
}
static SIMPLE_DEV_PM_OPS ( bma180_pm_ops , bma180_suspend , bma180_resume ) ;
# define BMA180_PM_OPS (&bma180_pm_ops)
# else
# define BMA180_PM_OPS NULL
# endif
2017-08-19 21:47:36 +03:00
static const struct i2c_device_id bma180_ids [ ] = {
2020-05-14 23:48:59 +03:00
{ " bma023 " , BMA023 } ,
2020-05-14 23:49:01 +03:00
{ " bma150 " , BMA150 } ,
2014-08-20 02:43:00 +04:00
{ " bma180 " , BMA180 } ,
{ " bma250 " , BMA250 } ,
2019-12-12 00:38:19 +03:00
{ " bma254 " , BMA254 } ,
2020-05-14 23:49:01 +03:00
{ " smb380 " , BMA150 } ,
2013-08-28 15:01:00 +04:00
{ }
} ;
2014-08-20 02:43:00 +04:00
MODULE_DEVICE_TABLE ( i2c , bma180_ids ) ;
2013-08-28 15:01:00 +04:00
2017-03-15 07:44:57 +03:00
static const struct of_device_id bma180_of_match [ ] = {
2020-05-14 23:48:59 +03:00
{
. compatible = " bosch,bma023 " ,
. data = ( void * ) BMA023
} ,
2020-05-14 23:49:01 +03:00
{
. compatible = " bosch,bma150 " ,
. data = ( void * ) BMA150
} ,
2017-03-15 07:44:57 +03:00
{
. compatible = " bosch,bma180 " ,
. data = ( void * ) BMA180
} ,
{
. compatible = " bosch,bma250 " ,
. data = ( void * ) BMA250
} ,
2019-12-12 00:38:19 +03:00
{
. compatible = " bosch,bma254 " ,
. data = ( void * ) BMA254
} ,
2020-05-14 23:49:01 +03:00
{
. compatible = " bosch,smb380 " ,
. data = ( void * ) BMA150
} ,
2017-03-15 07:44:57 +03:00
{ }
} ;
MODULE_DEVICE_TABLE ( of , bma180_of_match ) ;
2013-08-28 15:01:00 +04:00
static struct i2c_driver bma180_driver = {
. driver = {
2014-08-20 02:43:00 +04:00
. name = " bma180 " ,
2013-08-28 15:01:00 +04:00
. pm = BMA180_PM_OPS ,
2017-03-15 07:44:57 +03:00
. of_match_table = bma180_of_match ,
2013-08-28 15:01:00 +04:00
} ,
. probe = bma180_probe ,
. remove = bma180_remove ,
2014-08-20 02:43:00 +04:00
. id_table = bma180_ids ,
2013-08-28 15:01:00 +04:00
} ;
module_i2c_driver ( bma180_driver ) ;
MODULE_AUTHOR ( " Kravchenko Oleksandr <x0199363@ti.com> " ) ;
MODULE_AUTHOR ( " Texas Instruments, Inc. " ) ;
2020-05-14 23:49:01 +03:00
MODULE_DESCRIPTION ( " Bosch BMA023/BMA1x0/BMA25x triaxial acceleration sensor " ) ;
2013-08-28 15:01:00 +04:00
MODULE_LICENSE ( " GPL " ) ;