2010-10-28 05:43:55 +04:00
/*
2011-09-02 20:25:37 +04:00
* AD7291 8 - Channel , I2C , 12 - Bit SAR ADC with Temperature Sensor
2010-10-28 05:43:55 +04:00
*
2011-09-02 20:25:37 +04:00
* Copyright 2010 - 2011 Analog Devices Inc .
2010-10-28 05:43:55 +04:00
*
* Licensed under the GPL - 2 or later .
*/
# include <linux/device.h>
2014-06-30 12:50:00 +04:00
# include <linux/err.h>
2010-10-28 05:43:55 +04:00
# include <linux/i2c.h>
2014-06-30 12:50:00 +04:00
# include <linux/interrupt.h>
# include <linux/kernel.h>
2011-07-03 23:49:50 +04:00
# include <linux/module.h>
2011-09-02 20:25:32 +04:00
# include <linux/mutex.h>
2011-09-02 20:25:39 +04:00
# include <linux/regulator/consumer.h>
2014-06-30 12:50:00 +04:00
# include <linux/slab.h>
# include <linux/sysfs.h>
2010-10-28 05:43:55 +04:00
2012-04-25 18:54:58 +04:00
# include <linux/iio/iio.h>
# include <linux/iio/sysfs.h>
# include <linux/iio/events.h>
2010-10-28 05:43:55 +04:00
2014-06-30 12:50:00 +04:00
# include <linux/platform_data/ad7291.h>
2013-06-14 19:58:00 +04:00
2011-09-02 20:25:32 +04:00
/*
* Simplified handling
*
* If no events enabled - single polled channel read
* If event enabled direct reads disable unless channel
* is in the read mask .
*
* The noise - delayed bit as per datasheet suggestion is always enabled .
*/
2011-09-02 20:25:39 +04:00
2010-10-28 05:43:55 +04:00
/*
* AD7291 registers definition
*/
2011-09-02 20:25:40 +04:00
# define AD7291_COMMAND 0x00
# define AD7291_VOLTAGE 0x01
# define AD7291_T_SENSE 0x02
# define AD7291_T_AVERAGE 0x03
2013-06-14 19:58:00 +04:00
# define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
# define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
# define AD7291_HYST(x) ((x) * 3 + 0x6)
2011-09-02 20:25:38 +04:00
# define AD7291_VOLTAGE_ALERT_STATUS 0x1F
2010-10-28 05:43:55 +04:00
# define AD7291_T_ALERT_STATUS 0x20
2014-08-07 15:06:00 +04:00
# define AD7291_BITS 12
2011-09-02 20:25:40 +04:00
# define AD7291_VOLTAGE_LIMIT_COUNT 8
2010-10-28 05:43:55 +04:00
/*
* AD7291 command
*/
2014-08-07 15:06:00 +04:00
# define AD7291_AUTOCYCLE BIT(0)
# define AD7291_RESET BIT(1)
# define AD7291_ALERT_CLEAR BIT(2)
# define AD7291_ALERT_POLARITY BIT(3)
# define AD7291_EXT_REF BIT(4)
# define AD7291_NOISE_DELAY BIT(5)
# define AD7291_T_SENSE_MASK BIT(7)
# define AD7291_VOLTAGE_MASK GENMASK(15, 8)
# define AD7291_VOLTAGE_OFFSET 8
2010-10-28 05:43:55 +04:00
/*
* AD7291 value masks
*/
2014-08-07 15:06:00 +04:00
# define AD7291_VALUE_MASK GENMASK(11, 0)
2010-10-28 05:43:55 +04:00
2014-08-07 15:06:00 +04:00
/*
* AD7291 alert register bits
*/
# define AD7291_T_LOW BIT(0)
# define AD7291_T_HIGH BIT(1)
# define AD7291_T_AVG_LOW BIT(2)
# define AD7291_T_AVG_HIGH BIT(3)
# define AD7291_V_LOW(x) BIT((x) * 2)
# define AD7291_V_HIGH(x) BIT((x) * 2 + 1)
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:39 +04:00
2010-10-28 05:43:55 +04:00
struct ad7291_chip_info {
2011-09-02 20:25:39 +04:00
struct i2c_client * client ;
struct regulator * reg ;
u16 command ;
2011-09-02 20:25:43 +04:00
u16 c_mask ; /* Active voltage channels for events */
2011-09-02 20:25:45 +04:00
struct mutex state_lock ;
2010-10-28 05:43:55 +04:00
} ;
static int ad7291_i2c_read ( struct ad7291_chip_info * chip , u8 reg , u16 * data )
{
struct i2c_client * client = chip - > client ;
int ret = 0 ;
2013-06-14 19:58:00 +04:00
ret = i2c_smbus_read_word_swapped ( client , reg ) ;
2010-10-28 05:43:55 +04:00
if ( ret < 0 ) {
dev_err ( & client - > dev , " I2C read error \n " ) ;
return ret ;
}
2013-06-14 19:58:00 +04:00
* data = ret ;
2010-10-28 05:43:55 +04:00
return 0 ;
}
static int ad7291_i2c_write ( struct ad7291_chip_info * chip , u8 reg , u16 data )
{
2013-06-14 19:58:00 +04:00
return i2c_smbus_write_word_swapped ( chip - > client , reg , data ) ;
2010-10-28 05:43:55 +04:00
}
2011-05-18 17:41:11 +04:00
static irqreturn_t ad7291_event_handler ( int irq , void * private )
2010-10-28 05:43:55 +04:00
{
2011-05-18 17:41:11 +04:00
struct iio_dev * indio_dev = private ;
2011-06-27 16:07:23 +04:00
struct ad7291_chip_info * chip = iio_priv ( private ) ;
2010-10-28 05:43:55 +04:00
u16 t_status , v_status ;
u16 command ;
int i ;
2011-05-18 17:41:11 +04:00
s64 timestamp = iio_get_time_ns ( ) ;
2010-10-28 05:43:55 +04:00
if ( ad7291_i2c_read ( chip , AD7291_T_ALERT_STATUS , & t_status ) )
2011-05-18 17:41:11 +04:00
return IRQ_HANDLED ;
2010-10-28 05:43:55 +04:00
if ( ad7291_i2c_read ( chip , AD7291_VOLTAGE_ALERT_STATUS , & v_status ) )
2011-05-18 17:41:11 +04:00
return IRQ_HANDLED ;
2010-10-28 05:43:55 +04:00
if ( ! ( t_status | | v_status ) )
2011-05-18 17:41:11 +04:00
return IRQ_HANDLED ;
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:37 +04:00
command = chip - > command | AD7291_ALERT_CLEAR ;
2010-10-28 05:43:55 +04:00
ad7291_i2c_write ( chip , AD7291_COMMAND , command ) ;
2011-09-02 20:25:37 +04:00
command = chip - > command & ~ AD7291_ALERT_CLEAR ;
2010-10-28 05:43:55 +04:00
ad7291_i2c_write ( chip , AD7291_COMMAND , command ) ;
2011-09-02 20:25:32 +04:00
/* For now treat t_sense and t_sense_average the same */
2014-08-07 15:06:00 +04:00
if ( ( t_status & AD7291_T_LOW ) | | ( t_status & AD7291_T_AVG_LOW ) )
2011-08-30 15:41:06 +04:00
iio_push_event ( indio_dev ,
2011-05-18 17:42:15 +04:00
IIO_UNMOD_EVENT_CODE ( IIO_TEMP ,
0 ,
IIO_EV_TYPE_THRESH ,
IIO_EV_DIR_FALLING ) ,
timestamp ) ;
2014-08-07 15:06:00 +04:00
if ( ( t_status & AD7291_T_HIGH ) | | ( t_status & AD7291_T_AVG_HIGH ) )
2011-08-30 15:41:06 +04:00
iio_push_event ( indio_dev ,
2011-05-18 17:42:15 +04:00
IIO_UNMOD_EVENT_CODE ( IIO_TEMP ,
0 ,
IIO_EV_TYPE_THRESH ,
IIO_EV_DIR_RISING ) ,
timestamp ) ;
2014-08-07 15:06:00 +04:00
for ( i = 0 ; i < AD7291_VOLTAGE_LIMIT_COUNT ; i + + ) {
if ( v_status & AD7291_V_LOW ( i ) )
2011-08-30 15:41:06 +04:00
iio_push_event ( indio_dev ,
2011-09-27 12:56:41 +04:00
IIO_UNMOD_EVENT_CODE ( IIO_VOLTAGE ,
2014-08-07 15:06:00 +04:00
i ,
2011-05-18 17:42:15 +04:00
IIO_EV_TYPE_THRESH ,
IIO_EV_DIR_FALLING ) ,
timestamp ) ;
2014-08-07 15:06:00 +04:00
if ( v_status & AD7291_V_HIGH ( i ) )
2011-08-30 15:41:06 +04:00
iio_push_event ( indio_dev ,
2011-09-27 12:56:41 +04:00
IIO_UNMOD_EVENT_CODE ( IIO_VOLTAGE ,
2014-08-07 15:06:00 +04:00
i ,
2011-05-18 17:42:15 +04:00
IIO_EV_TYPE_THRESH ,
IIO_EV_DIR_RISING ) ,
timestamp ) ;
2010-10-28 05:43:55 +04:00
}
2011-05-18 17:41:11 +04:00
return IRQ_HANDLED ;
2010-10-28 05:43:55 +04:00
}
2013-10-07 18:11:00 +04:00
static unsigned int ad7291_threshold_reg ( const struct iio_chan_spec * chan ,
2014-08-07 15:06:00 +04:00
enum iio_event_direction dir ,
enum iio_event_info info )
2013-06-14 19:58:00 +04:00
{
unsigned int offset ;
2013-10-07 18:11:00 +04:00
switch ( chan - > type ) {
2013-06-14 19:58:00 +04:00
case IIO_VOLTAGE :
2013-10-07 18:11:00 +04:00
offset = chan - > channel ;
2013-06-14 19:58:00 +04:00
break ;
case IIO_TEMP :
2014-08-07 15:06:00 +04:00
offset = AD7291_VOLTAGE_OFFSET ;
2013-06-14 19:58:00 +04:00
break ;
default :
return 0 ;
}
2013-10-07 18:11:00 +04:00
switch ( info ) {
case IIO_EV_INFO_VALUE :
2014-08-07 15:06:00 +04:00
if ( dir = = IIO_EV_DIR_FALLING )
return AD7291_DATA_HIGH ( offset ) ;
else
return AD7291_DATA_LOW ( offset ) ;
2013-10-07 18:11:00 +04:00
case IIO_EV_INFO_HYSTERESIS :
2014-08-07 15:06:00 +04:00
return AD7291_HYST ( offset ) ;
2013-10-07 18:11:00 +04:00
default :
2014-08-07 15:06:00 +04:00
break ;
2013-10-07 18:11:00 +04:00
}
return 0 ;
2013-06-14 19:58:00 +04:00
}
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:32 +04:00
static int ad7291_read_event_value ( struct iio_dev * indio_dev ,
2013-10-07 18:11:00 +04:00
const struct iio_chan_spec * chan ,
enum iio_event_type type ,
enum iio_event_direction dir ,
enum iio_event_info info ,
int * val , int * val2 )
2010-10-28 05:43:55 +04:00
{
2011-09-02 20:25:32 +04:00
struct ad7291_chip_info * chip = iio_priv ( indio_dev ) ;
int ret ;
u16 uval ;
2013-10-07 18:11:00 +04:00
ret = ad7291_i2c_read ( chip , ad7291_threshold_reg ( chan , dir , info ) ,
2014-08-07 15:06:00 +04:00
& uval ) ;
2013-06-14 19:58:00 +04:00
if ( ret < 0 )
return ret ;
2013-10-07 18:11:00 +04:00
if ( info = = IIO_EV_INFO_HYSTERESIS | | chan - > type = = IIO_VOLTAGE )
2011-09-02 20:25:40 +04:00
* val = uval & AD7291_VALUE_MASK ;
2013-10-07 18:11:00 +04:00
else
2013-06-14 19:58:00 +04:00
* val = sign_extend32 ( uval , 11 ) ;
2013-10-07 18:11:00 +04:00
return IIO_VAL_INT ;
2011-09-02 20:25:32 +04:00
}
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:32 +04:00
static int ad7291_write_event_value ( struct iio_dev * indio_dev ,
2013-10-07 18:11:00 +04:00
const struct iio_chan_spec * chan ,
enum iio_event_type type ,
enum iio_event_direction dir ,
enum iio_event_info info ,
int val , int val2 )
2011-09-02 20:25:32 +04:00
{
struct ad7291_chip_info * chip = iio_priv ( indio_dev ) ;
2013-10-07 18:11:00 +04:00
if ( info = = IIO_EV_INFO_HYSTERESIS | | chan - > type = = IIO_VOLTAGE ) {
2011-09-02 20:25:46 +04:00
if ( val > AD7291_VALUE_MASK | | val < 0 )
2011-09-02 20:25:32 +04:00
return - EINVAL ;
2013-10-07 18:11:00 +04:00
} else {
2011-09-02 20:25:32 +04:00
if ( val > 2047 | | val < - 2048 )
return - EINVAL ;
2013-06-14 19:58:00 +04:00
}
2013-10-07 18:11:00 +04:00
return ad7291_i2c_write ( chip , ad7291_threshold_reg ( chan , dir , info ) ,
2014-08-07 15:06:00 +04:00
val ) ;
2010-10-28 05:43:55 +04:00
}
2011-09-02 20:25:32 +04:00
static int ad7291_read_event_config ( struct iio_dev * indio_dev ,
2013-10-07 18:11:00 +04:00
const struct iio_chan_spec * chan ,
enum iio_event_type type ,
enum iio_event_direction dir )
2010-10-28 05:43:55 +04:00
{
2011-09-02 20:25:32 +04:00
struct ad7291_chip_info * chip = iio_priv ( indio_dev ) ;
2014-08-07 15:06:00 +04:00
/*
* To be enabled the channel must simply be on . If any are enabled
* we are in continuous sampling mode
*/
2011-09-02 20:25:32 +04:00
2013-10-07 18:11:00 +04:00
switch ( chan - > type ) {
2011-09-02 20:25:32 +04:00
case IIO_VOLTAGE :
2014-08-07 15:06:00 +04:00
return ! ! ( chip - > c_mask & BIT ( 15 - chan - > channel ) ) ;
2011-09-02 20:25:32 +04:00
case IIO_TEMP :
/* always on */
return 1 ;
default :
2010-10-28 05:43:55 +04:00
return - EINVAL ;
2011-09-02 20:25:32 +04:00
}
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:32 +04:00
}
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:32 +04:00
static int ad7291_write_event_config ( struct iio_dev * indio_dev ,
2013-10-07 18:11:00 +04:00
const struct iio_chan_spec * chan ,
enum iio_event_type type ,
enum iio_event_direction dir ,
2011-09-02 20:25:32 +04:00
int state )
{
int ret = 0 ;
struct ad7291_chip_info * chip = iio_priv ( indio_dev ) ;
2013-10-07 18:11:00 +04:00
unsigned int mask ;
2011-09-02 20:25:32 +04:00
u16 regval ;
mutex_lock ( & chip - > state_lock ) ;
regval = chip - > command ;
/*
* To be enabled the channel must simply be on . If any are enabled
* use continuous sampling mode .
* Possible to disable temp as well but that makes single read tricky .
*/
2011-09-02 20:25:40 +04:00
2013-10-07 18:11:00 +04:00
mask = BIT ( 15 - chan - > channel ) ;
switch ( chan - > type ) {
2011-09-02 20:25:32 +04:00
case IIO_VOLTAGE :
2013-10-07 18:11:00 +04:00
if ( ( ! state ) & & ( chip - > c_mask & mask ) )
chip - > c_mask & = ~ mask ;
else if ( state & & ( ! ( chip - > c_mask & mask ) ) )
chip - > c_mask | = mask ;
2011-09-02 20:25:32 +04:00
else
break ;
2011-09-02 20:25:40 +04:00
regval & = ~ AD7291_AUTOCYCLE ;
2011-09-02 20:25:43 +04:00
regval | = chip - > c_mask ;
2011-09-02 20:25:32 +04:00
if ( chip - > c_mask ) /* Enable autocycle? */
regval | = AD7291_AUTOCYCLE ;
ret = ad7291_i2c_write ( chip , AD7291_COMMAND , regval ) ;
if ( ret < 0 )
goto error_ret ;
chip - > command = regval ;
break ;
default :
ret = - EINVAL ;
}
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:32 +04:00
error_ret :
mutex_unlock ( & chip - > state_lock ) ;
2010-10-28 05:43:55 +04:00
return ret ;
}
2011-09-02 20:25:32 +04:00
static int ad7291_read_raw ( struct iio_dev * indio_dev ,
struct iio_chan_spec const * chan ,
int * val ,
int * val2 ,
long mask )
{
int ret ;
struct ad7291_chip_info * chip = iio_priv ( indio_dev ) ;
u16 regval ;
switch ( mask ) {
2012-04-15 20:41:18 +04:00
case IIO_CHAN_INFO_RAW :
2011-09-02 20:25:32 +04:00
switch ( chan - > type ) {
case IIO_VOLTAGE :
mutex_lock ( & chip - > state_lock ) ;
/* If in autocycle mode drop through */
2011-09-02 20:25:43 +04:00
if ( chip - > command & AD7291_AUTOCYCLE ) {
2011-09-02 20:25:32 +04:00
mutex_unlock ( & chip - > state_lock ) ;
return - EBUSY ;
}
/* Enable this channel alone */
regval = chip - > command & ( ~ AD7291_VOLTAGE_MASK ) ;
2014-08-07 15:06:00 +04:00
regval | = BIT ( 15 - chan - > channel ) ;
2011-09-02 20:25:32 +04:00
ret = ad7291_i2c_write ( chip , AD7291_COMMAND , regval ) ;
if ( ret < 0 ) {
mutex_unlock ( & chip - > state_lock ) ;
return ret ;
}
/* Read voltage */
2013-06-14 19:58:00 +04:00
ret = i2c_smbus_read_word_swapped ( chip - > client ,
2014-08-07 15:06:00 +04:00
AD7291_VOLTAGE ) ;
2011-09-02 20:25:32 +04:00
if ( ret < 0 ) {
mutex_unlock ( & chip - > state_lock ) ;
return ret ;
}
2013-06-14 19:58:00 +04:00
* val = ret & AD7291_VALUE_MASK ;
2011-09-02 20:25:32 +04:00
mutex_unlock ( & chip - > state_lock ) ;
return IIO_VAL_INT ;
case IIO_TEMP :
/* Assumes tsense bit of command register always set */
2013-06-14 19:58:00 +04:00
ret = i2c_smbus_read_word_swapped ( chip - > client ,
2014-08-07 15:06:00 +04:00
AD7291_T_SENSE ) ;
2011-09-02 20:25:32 +04:00
if ( ret < 0 )
return ret ;
2013-06-14 19:58:00 +04:00
* val = sign_extend32 ( ret , 11 ) ;
2011-09-02 20:25:32 +04:00
return IIO_VAL_INT ;
default :
return - EINVAL ;
}
2011-10-26 20:41:36 +04:00
case IIO_CHAN_INFO_AVERAGE_RAW :
2013-06-14 19:58:00 +04:00
ret = i2c_smbus_read_word_swapped ( chip - > client ,
2014-08-07 15:06:00 +04:00
AD7291_T_AVERAGE ) ;
2011-09-02 20:25:32 +04:00
if ( ret < 0 )
return ret ;
2013-06-14 19:58:00 +04:00
* val = sign_extend32 ( ret , 11 ) ;
2011-09-02 20:25:32 +04:00
return IIO_VAL_INT ;
2011-10-26 20:41:36 +04:00
case IIO_CHAN_INFO_SCALE :
switch ( chan - > type ) {
case IIO_VOLTAGE :
2013-06-14 19:58:00 +04:00
if ( chip - > reg ) {
int vref ;
2014-06-30 12:50:00 +04:00
2013-06-14 19:58:00 +04:00
vref = regulator_get_voltage ( chip - > reg ) ;
if ( vref < 0 )
return vref ;
* val = vref / 1000 ;
} else {
* val = 2500 ;
}
2013-06-14 19:58:00 +04:00
* val2 = AD7291_BITS ;
return IIO_VAL_FRACTIONAL_LOG2 ;
2011-10-26 20:41:36 +04:00
case IIO_TEMP :
/*
* One LSB of the ADC corresponds to 0.25 deg C .
* The temperature reading is in 12 - bit twos
* complement format
*/
* val = 250 ;
return IIO_VAL_INT ;
default :
return - EINVAL ;
}
2011-09-02 20:25:32 +04:00
default :
return - EINVAL ;
}
}
2010-10-28 05:43:55 +04:00
2013-10-07 18:11:00 +04:00
static const struct iio_event_spec ad7291_events [ ] = {
{
. type = IIO_EV_TYPE_THRESH ,
. dir = IIO_EV_DIR_RISING ,
. mask_separate = BIT ( IIO_EV_INFO_VALUE ) |
BIT ( IIO_EV_INFO_ENABLE ) ,
} , {
. type = IIO_EV_TYPE_THRESH ,
. dir = IIO_EV_DIR_FALLING ,
. mask_separate = BIT ( IIO_EV_INFO_VALUE ) |
BIT ( IIO_EV_INFO_ENABLE ) ,
2013-10-07 18:11:00 +04:00
} , {
. type = IIO_EV_TYPE_THRESH ,
. dir = IIO_EV_DIR_EITHER ,
. mask_separate = BIT ( IIO_EV_INFO_HYSTERESIS ) ,
2013-10-07 18:11:00 +04:00
} ,
} ;
2011-09-02 20:25:32 +04:00
# define AD7291_VOLTAGE_CHAN(_chan) \
{ \
. type = IIO_VOLTAGE , \
2013-02-27 23:37:16 +04:00
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) , \
. info_mask_shared_by_type = BIT ( IIO_CHAN_INFO_SCALE ) , \
2011-09-02 20:25:32 +04:00
. indexed = 1 , \
. channel = _chan , \
2013-10-07 18:11:00 +04:00
. event_spec = ad7291_events , \
. num_event_specs = ARRAY_SIZE ( ad7291_events ) , \
2011-09-02 20:25:32 +04:00
}
static const struct iio_chan_spec ad7291_channels [ ] = {
AD7291_VOLTAGE_CHAN ( 0 ) ,
AD7291_VOLTAGE_CHAN ( 1 ) ,
AD7291_VOLTAGE_CHAN ( 2 ) ,
AD7291_VOLTAGE_CHAN ( 3 ) ,
AD7291_VOLTAGE_CHAN ( 4 ) ,
AD7291_VOLTAGE_CHAN ( 5 ) ,
AD7291_VOLTAGE_CHAN ( 6 ) ,
AD7291_VOLTAGE_CHAN ( 7 ) ,
{
. type = IIO_TEMP ,
2013-02-27 23:37:16 +04:00
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) |
BIT ( IIO_CHAN_INFO_AVERAGE_RAW ) |
BIT ( IIO_CHAN_INFO_SCALE ) ,
2011-09-02 20:25:32 +04:00
. indexed = 1 ,
. channel = 0 ,
2013-10-07 18:11:00 +04:00
. event_spec = ad7291_events ,
. num_event_specs = ARRAY_SIZE ( ad7291_events ) ,
2011-09-02 20:25:32 +04:00
}
2010-10-28 05:43:55 +04:00
} ;
2011-05-18 17:42:37 +04:00
static const struct iio_info ad7291_info = {
2011-09-02 20:25:32 +04:00
. read_raw = & ad7291_read_raw ,
2013-12-07 14:45:00 +04:00
. read_event_config = & ad7291_read_event_config ,
. write_event_config = & ad7291_write_event_config ,
. read_event_value = & ad7291_read_event_value ,
. write_event_value = & ad7291_write_event_value ,
2013-07-04 17:46:00 +04:00
. driver_module = THIS_MODULE ,
2011-05-18 17:42:37 +04:00
} ;
2012-11-19 22:21:57 +04:00
static int ad7291_probe ( struct i2c_client * client ,
2014-08-07 15:06:00 +04:00
const struct i2c_device_id * id )
2010-10-28 05:43:55 +04:00
{
2013-06-14 19:58:00 +04:00
struct ad7291_platform_data * pdata = client - > dev . platform_data ;
2010-10-28 05:43:55 +04:00
struct ad7291_chip_info * chip ;
2011-06-27 16:07:23 +04:00
struct iio_dev * indio_dev ;
2014-06-20 23:22:00 +04:00
int ret ;
2010-10-28 05:43:55 +04:00
2013-08-31 21:12:00 +04:00
indio_dev = devm_iio_device_alloc ( & client - > dev , sizeof ( * chip ) ) ;
if ( ! indio_dev )
return - ENOMEM ;
2011-06-29 23:50:48 +04:00
chip = iio_priv ( indio_dev ) ;
2011-09-02 20:25:39 +04:00
2013-06-14 19:58:00 +04:00
if ( pdata & & pdata - > use_external_ref ) {
2013-08-31 21:12:00 +04:00
chip - > reg = devm_regulator_get ( & client - > dev , " vref " ) ;
2013-06-14 19:58:00 +04:00
if ( IS_ERR ( chip - > reg ) )
2014-06-20 23:22:00 +04:00
return PTR_ERR ( chip - > reg ) ;
2013-06-14 19:58:00 +04:00
2011-09-02 20:25:39 +04:00
ret = regulator_enable ( chip - > reg ) ;
if ( ret )
2013-08-31 21:12:00 +04:00
return ret ;
2011-09-02 20:25:39 +04:00
}
2011-09-02 20:25:32 +04:00
mutex_init ( & chip - > state_lock ) ;
2010-10-28 05:43:55 +04:00
/* this is only used for device removal purposes */
2011-06-27 16:07:23 +04:00
i2c_set_clientdata ( client , indio_dev ) ;
2010-10-28 05:43:55 +04:00
chip - > client = client ;
2011-09-02 20:25:44 +04:00
chip - > command = AD7291_NOISE_DELAY |
AD7291_T_SENSE_MASK | /* Tsense always enabled */
AD7291_ALERT_POLARITY ; /* set irq polarity low level */
2010-10-28 05:43:55 +04:00
2013-06-14 19:58:00 +04:00
if ( pdata & & pdata - > use_external_ref )
2011-09-02 20:25:39 +04:00
chip - > command | = AD7291_EXT_REF ;
2011-06-27 16:07:23 +04:00
indio_dev - > name = id - > name ;
2011-09-02 20:25:32 +04:00
indio_dev - > channels = ad7291_channels ;
indio_dev - > num_channels = ARRAY_SIZE ( ad7291_channels ) ;
2011-06-27 16:07:23 +04:00
indio_dev - > dev . parent = & client - > dev ;
indio_dev - > info = & ad7291_info ;
indio_dev - > modes = INDIO_DIRECT_MODE ;
2010-10-28 05:43:55 +04:00
2011-09-02 20:25:44 +04:00
ret = ad7291_i2c_write ( chip , AD7291_COMMAND , AD7291_RESET ) ;
if ( ret ) {
ret = - EIO ;
goto error_disable_reg ;
}
ret = ad7291_i2c_write ( chip , AD7291_COMMAND , chip - > command ) ;
if ( ret ) {
ret = - EIO ;
goto error_disable_reg ;
}
2010-10-28 05:43:55 +04:00
if ( client - > irq > 0 ) {
2011-05-18 17:41:11 +04:00
ret = request_threaded_irq ( client - > irq ,
NULL ,
& ad7291_event_handler ,
IRQF_TRIGGER_LOW | IRQF_ONESHOT ,
2011-05-18 17:41:44 +04:00
id - > name ,
2011-06-27 16:07:23 +04:00
indio_dev ) ;
2010-10-28 05:43:55 +04:00
if ( ret )
2011-09-02 20:25:39 +04:00
goto error_disable_reg ;
2010-10-28 05:43:55 +04:00
}
2011-09-02 20:14:40 +04:00
ret = iio_device_register ( indio_dev ) ;
if ( ret )
goto error_unreg_irq ;
2010-10-28 05:43:55 +04:00
return 0 ;
error_unreg_irq :
2011-09-02 20:25:32 +04:00
if ( client - > irq )
free_irq ( client - > irq , indio_dev ) ;
2011-09-02 20:25:39 +04:00
error_disable_reg :
2013-06-14 19:58:00 +04:00
if ( chip - > reg )
2011-09-02 20:25:39 +04:00
regulator_disable ( chip - > reg ) ;
2013-08-31 21:12:00 +04:00
2010-10-28 05:43:55 +04:00
return ret ;
}
2012-11-19 22:26:37 +04:00
static int ad7291_remove ( struct i2c_client * client )
2010-10-28 05:43:55 +04:00
{
2011-06-27 16:07:23 +04:00
struct iio_dev * indio_dev = i2c_get_clientdata ( client ) ;
2011-09-02 20:25:39 +04:00
struct ad7291_chip_info * chip = iio_priv ( indio_dev ) ;
2010-10-28 05:43:55 +04:00
2011-10-14 17:46:58 +04:00
iio_device_unregister ( indio_dev ) ;
2010-10-28 05:43:55 +04:00
if ( client - > irq )
2011-06-27 16:07:23 +04:00
free_irq ( client - > irq , indio_dev ) ;
2011-09-02 20:25:39 +04:00
2013-08-31 21:12:00 +04:00
if ( chip - > reg )
2011-09-02 20:25:39 +04:00
regulator_disable ( chip - > reg ) ;
2010-10-28 05:43:55 +04:00
return 0 ;
}
static const struct i2c_device_id ad7291_id [ ] = {
{ " ad7291 " , 0 } ,
{ }
} ;
MODULE_DEVICE_TABLE ( i2c , ad7291_id ) ;
static struct i2c_driver ad7291_driver = {
. driver = {
2011-09-02 20:25:45 +04:00
. name = KBUILD_MODNAME ,
2010-10-28 05:43:55 +04:00
} ,
. probe = ad7291_probe ,
2012-11-19 22:21:38 +04:00
. remove = ad7291_remove ,
2010-10-28 05:43:55 +04:00
. id_table = ad7291_id ,
} ;
2011-11-16 13:13:38 +04:00
module_i2c_driver ( ad7291_driver ) ;
2010-10-28 05:43:55 +04:00
MODULE_AUTHOR ( " Sonic Zhang <sonic.zhang@analog.com> " ) ;
2011-09-02 20:25:45 +04:00
MODULE_DESCRIPTION ( " Analog Devices AD7291 ADC driver " ) ;
2010-10-28 05:43:55 +04:00
MODULE_LICENSE ( " GPL v2 " ) ;