2012-11-23 15:13:00 +00:00
/*
2016-04-11 17:24:26 +03:00
* TI ADC081C / ADC101C / ADC121C 8 / 10 / 12 - bit ADC driver
*
2012-11-23 15:13:00 +00:00
* Copyright ( C ) 2012 Avionic Design GmbH
2016-04-11 17:24:26 +03:00
* Copyright ( C ) 2016 Intel
2012-11-23 15:13:00 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation .
2016-04-11 17:24:26 +03:00
*
* Datasheets :
* http : //www.ti.com/lit/ds/symlink/adc081c021.pdf
* http : //www.ti.com/lit/ds/symlink/adc101c021.pdf
* http : //www.ti.com/lit/ds/symlink/adc121c021.pdf
*
* The devices have a very similar interface and differ mostly in the number of
* bits handled . For the 8 - bit and 10 - bit models the least - significant 4 or 2
* bits of value registers are reserved .
2012-11-23 15:13:00 +00:00
*/
# include <linux/err.h>
# include <linux/i2c.h>
# include <linux/module.h>
2013-10-18 13:04:00 +01:00
# include <linux/of.h>
2012-11-23 15:13:00 +00:00
# include <linux/iio/iio.h>
# include <linux/regulator/consumer.h>
struct adc081c {
struct i2c_client * i2c ;
struct regulator * ref ;
2016-04-11 17:24:26 +03:00
/* 8, 10 or 12 */
int bits ;
2012-11-23 15:13:00 +00:00
} ;
# define REG_CONV_RES 0x00
static int adc081c_read_raw ( struct iio_dev * iio ,
struct iio_chan_spec const * channel , int * value ,
int * shift , long mask )
{
struct adc081c * adc = iio_priv ( iio ) ;
int err ;
switch ( mask ) {
case IIO_CHAN_INFO_RAW :
err = i2c_smbus_read_word_swapped ( adc - > i2c , REG_CONV_RES ) ;
if ( err < 0 )
return err ;
2016-04-11 17:24:26 +03:00
* value = ( err & 0xFFF ) > > ( 12 - adc - > bits ) ;
2012-11-23 15:13:00 +00:00
return IIO_VAL_INT ;
case IIO_CHAN_INFO_SCALE :
err = regulator_get_voltage ( adc - > ref ) ;
if ( err < 0 )
return err ;
* value = err / 1000 ;
2016-04-11 17:24:26 +03:00
* shift = adc - > bits ;
2012-11-23 15:13:00 +00:00
return IIO_VAL_FRACTIONAL_LOG2 ;
default :
break ;
}
return - EINVAL ;
}
static const struct iio_chan_spec adc081c_channel = {
. type = IIO_VOLTAGE ,
2013-02-27 19:06:39 +00:00
. info_mask_shared_by_type = BIT ( IIO_CHAN_INFO_SCALE ) ,
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) ,
2012-11-23 15:13:00 +00:00
} ;
2016-04-11 17:24:26 +03:00
struct adcxx1c_model {
int bits ;
} ;
# define ADCxx1C_MODEL(_bits) \
{ \
. bits = ( _bits ) , \
}
/* Model ids are indexes in _models array */
enum adcxx1c_model_id {
ADC081C = 0 ,
ADC101C = 1 ,
ADC121C = 2 ,
} ;
static struct adcxx1c_model adcxx1c_models [ ] = {
ADCxx1C_MODEL ( 8 ) ,
ADCxx1C_MODEL ( 10 ) ,
ADCxx1C_MODEL ( 12 ) ,
} ;
2012-11-23 15:13:00 +00:00
static const struct iio_info adc081c_info = {
. read_raw = adc081c_read_raw ,
. driver_module = THIS_MODULE ,
} ;
static int adc081c_probe ( struct i2c_client * client ,
const struct i2c_device_id * id )
{
struct iio_dev * iio ;
struct adc081c * adc ;
2016-04-11 17:24:26 +03:00
struct adcxx1c_model * model = & adcxx1c_models [ id - > driver_data ] ;
2012-11-23 15:13:00 +00:00
int err ;
if ( ! i2c_check_functionality ( client - > adapter , I2C_FUNC_SMBUS_WORD_DATA ) )
2016-02-26 22:13:49 -08:00
return - EOPNOTSUPP ;
2012-11-23 15:13:00 +00:00
2013-07-23 09:58:00 +01:00
iio = devm_iio_device_alloc ( & client - > dev , sizeof ( * adc ) ) ;
2012-11-23 15:13:00 +00:00
if ( ! iio )
return - ENOMEM ;
adc = iio_priv ( iio ) ;
adc - > i2c = client ;
2016-04-11 17:24:26 +03:00
adc - > bits = model - > bits ;
2012-11-23 15:13:00 +00:00
2013-07-23 09:58:00 +01:00
adc - > ref = devm_regulator_get ( & client - > dev , " vref " ) ;
if ( IS_ERR ( adc - > ref ) )
return PTR_ERR ( adc - > ref ) ;
2012-11-23 15:13:00 +00:00
err = regulator_enable ( adc - > ref ) ;
if ( err < 0 )
2013-07-23 09:58:00 +01:00
return err ;
2012-11-23 15:13:00 +00:00
iio - > dev . parent = & client - > dev ;
iio - > name = dev_name ( & client - > dev ) ;
iio - > modes = INDIO_DIRECT_MODE ;
iio - > info = & adc081c_info ;
iio - > channels = & adc081c_channel ;
iio - > num_channels = 1 ;
err = iio_device_register ( iio ) ;
if ( err < 0 )
goto regulator_disable ;
i2c_set_clientdata ( client , iio ) ;
return 0 ;
regulator_disable :
regulator_disable ( adc - > ref ) ;
return err ;
}
static int adc081c_remove ( struct i2c_client * client )
{
struct iio_dev * iio = i2c_get_clientdata ( client ) ;
struct adc081c * adc = iio_priv ( iio ) ;
iio_device_unregister ( iio ) ;
regulator_disable ( adc - > ref ) ;
return 0 ;
}
static const struct i2c_device_id adc081c_id [ ] = {
2016-04-11 17:24:26 +03:00
{ " adc081c " , ADC081C } ,
{ " adc101c " , ADC101C } ,
{ " adc121c " , ADC121C } ,
2012-11-23 15:13:00 +00:00
{ }
} ;
MODULE_DEVICE_TABLE ( i2c , adc081c_id ) ;
# ifdef CONFIG_OF
static const struct of_device_id adc081c_of_match [ ] = {
{ . compatible = " ti,adc081c " } ,
2016-04-11 17:24:26 +03:00
{ . compatible = " ti,adc101c " } ,
{ . compatible = " ti,adc121c " } ,
2012-11-23 15:13:00 +00:00
{ }
} ;
MODULE_DEVICE_TABLE ( of , adc081c_of_match ) ;
# endif
static struct i2c_driver adc081c_driver = {
. driver = {
. name = " adc081c " ,
. of_match_table = of_match_ptr ( adc081c_of_match ) ,
} ,
. probe = adc081c_probe ,
. remove = adc081c_remove ,
. id_table = adc081c_id ,
} ;
module_i2c_driver ( adc081c_driver ) ;
MODULE_AUTHOR ( " Thierry Reding <thierry.reding@avionic-design.de> " ) ;
2016-04-11 17:24:26 +03:00
MODULE_DESCRIPTION ( " Texas Instruments ADC081C/ADC101C/ADC121C driver " ) ;
2012-11-23 15:13:00 +00:00
MODULE_LICENSE ( " GPL v2 " ) ;