2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2012-11-23 19:13:00 +04:00
/*
2016-04-11 17:24:26 +03:00
* TI ADC081C / ADC101C / ADC121C 8 / 10 / 12 - bit ADC driver
*
2012-11-23 19:13:00 +04:00
* Copyright ( C ) 2012 Avionic Design GmbH
2016-04-11 17:24:26 +03:00
* Copyright ( C ) 2016 Intel
2012-11-23 19:13:00 +04:00
*
2016-04-11 17:24:26 +03:00
* Datasheets :
2020-07-04 22:27:43 +03:00
* https : //www.ti.com/lit/ds/symlink/adc081c021.pdf
* https : //www.ti.com/lit/ds/symlink/adc101c021.pdf
* https : //www.ti.com/lit/ds/symlink/adc121c021.pdf
2016-04-11 17:24:26 +03:00
*
* 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 19:13:00 +04:00
*/
# include <linux/err.h>
# include <linux/i2c.h>
# include <linux/module.h>
2020-06-28 15:36:46 +03:00
# include <linux/mod_devicetable.h>
2021-12-05 20:27:28 +03:00
# include <linux/property.h>
2012-11-23 19:13:00 +04:00
# include <linux/iio/iio.h>
2016-04-11 17:24:27 +03:00
# include <linux/iio/buffer.h>
# include <linux/iio/trigger_consumer.h>
# include <linux/iio/triggered_buffer.h>
2012-11-23 19:13:00 +04:00
# 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 ;
2020-07-22 18:50:56 +03:00
/* Ensure natural alignment of buffer elements */
struct {
u16 channel ;
s64 ts __aligned ( 8 ) ;
} scan ;
2012-11-23 19:13:00 +04: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 19:13:00 +04: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 19:13:00 +04:00
return IIO_VAL_FRACTIONAL_LOG2 ;
default :
break ;
}
return - EINVAL ;
}
2016-04-11 17:24:27 +03:00
# define ADCxx1C_CHAN(_bits) { \
. type = IIO_VOLTAGE , \
. info_mask_shared_by_type = BIT ( IIO_CHAN_INFO_SCALE ) , \
. info_mask_separate = BIT ( IIO_CHAN_INFO_RAW ) , \
. scan_type = { \
. sign = ' u ' , \
. realbits = ( _bits ) , \
. storagebits = 16 , \
. shift = 12 - ( _bits ) , \
. endianness = IIO_CPU , \
} , \
}
# define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
static const struct iio_chan_spec _name # # _channels [ ] = { \
ADCxx1C_CHAN ( ( _bits ) ) , \
IIO_CHAN_SOFT_TIMESTAMP ( 1 ) , \
} ; \
# define ADC081C_NUM_CHANNELS 2
2012-11-23 19:13:00 +04:00
2016-04-11 17:24:26 +03:00
struct adcxx1c_model {
2016-04-11 17:24:27 +03:00
const struct iio_chan_spec * channels ;
2016-04-11 17:24:26 +03:00
int bits ;
} ;
2016-04-11 17:24:27 +03:00
# define ADCxx1C_MODEL(_name, _bits) \
2016-04-11 17:24:26 +03:00
{ \
2016-04-11 17:24:27 +03:00
. channels = _name # # _channels , \
2016-04-11 17:24:26 +03:00
. bits = ( _bits ) , \
}
2016-04-11 17:24:27 +03:00
DEFINE_ADCxx1C_CHANNELS ( adc081c , 8 ) ;
DEFINE_ADCxx1C_CHANNELS ( adc101c , 10 ) ;
DEFINE_ADCxx1C_CHANNELS ( adc121c , 12 ) ;
2016-04-11 17:24:26 +03:00
/* Model ids are indexes in _models array */
enum adcxx1c_model_id {
ADC081C = 0 ,
ADC101C = 1 ,
ADC121C = 2 ,
} ;
static struct adcxx1c_model adcxx1c_models [ ] = {
2016-04-11 17:24:27 +03:00
ADCxx1C_MODEL ( adc081c , 8 ) ,
ADCxx1C_MODEL ( adc101c , 10 ) ,
ADCxx1C_MODEL ( adc121c , 12 ) ,
2016-04-11 17:24:26 +03:00
} ;
2012-11-23 19:13:00 +04:00
static const struct iio_info adc081c_info = {
. read_raw = adc081c_read_raw ,
} ;
2016-04-11 17:24:27 +03:00
static irqreturn_t adc081c_trigger_handler ( int irq , void * p )
{
struct iio_poll_func * pf = p ;
struct iio_dev * indio_dev = pf - > indio_dev ;
struct adc081c * data = iio_priv ( indio_dev ) ;
int ret ;
ret = i2c_smbus_read_word_swapped ( data - > i2c , REG_CONV_RES ) ;
if ( ret < 0 )
goto out ;
2020-07-22 18:50:56 +03:00
data - > scan . channel = ret ;
iio_push_to_buffers_with_timestamp ( indio_dev , & data - > scan ,
2016-03-09 21:05:49 +03:00
iio_get_time_ns ( indio_dev ) ) ;
2016-04-11 17:24:27 +03:00
out :
iio_trigger_notify_done ( indio_dev - > trig ) ;
return IRQ_HANDLED ;
}
2021-05-16 20:25:17 +03:00
static void adc081c_reg_disable ( void * reg )
{
regulator_disable ( reg ) ;
}
2012-11-23 19:13:00 +04:00
static int adc081c_probe ( struct i2c_client * client ,
const struct i2c_device_id * id )
{
struct iio_dev * iio ;
struct adc081c * adc ;
2021-12-05 20:27:28 +03:00
const struct adcxx1c_model * model ;
2012-11-23 19:13:00 +04:00
int err ;
if ( ! i2c_check_functionality ( client - > adapter , I2C_FUNC_SMBUS_WORD_DATA ) )
2016-02-27 09:13:49 +03:00
return - EOPNOTSUPP ;
2012-11-23 19:13:00 +04:00
2021-12-05 20:27:28 +03:00
if ( dev_fwnode ( & client - > dev ) )
model = device_get_match_data ( & client - > dev ) ;
else
model = & adcxx1c_models [ id - > driver_data ] ;
2016-05-27 20:37:30 +03:00
2013-07-23 12:58:00 +04:00
iio = devm_iio_device_alloc ( & client - > dev , sizeof ( * adc ) ) ;
2012-11-23 19:13:00 +04: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 19:13:00 +04:00
2013-07-23 12:58:00 +04:00
adc - > ref = devm_regulator_get ( & client - > dev , " vref " ) ;
if ( IS_ERR ( adc - > ref ) )
return PTR_ERR ( adc - > ref ) ;
2012-11-23 19:13:00 +04:00
err = regulator_enable ( adc - > ref ) ;
if ( err < 0 )
2013-07-23 12:58:00 +04:00
return err ;
2012-11-23 19:13:00 +04:00
2021-05-16 20:25:17 +03:00
err = devm_add_action_or_reset ( & client - > dev , adc081c_reg_disable ,
adc - > ref ) ;
if ( err )
return err ;
2012-11-23 19:13:00 +04:00
iio - > name = dev_name ( & client - > dev ) ;
iio - > modes = INDIO_DIRECT_MODE ;
iio - > info = & adc081c_info ;
2016-04-11 17:24:27 +03:00
iio - > channels = model - > channels ;
iio - > num_channels = ADC081C_NUM_CHANNELS ;
2021-05-16 20:25:17 +03:00
err = devm_iio_triggered_buffer_setup ( & client - > dev , iio , NULL ,
adc081c_trigger_handler , NULL ) ;
2016-04-11 17:24:27 +03:00
if ( err < 0 ) {
dev_err ( & client - > dev , " iio triggered buffer setup failed \n " ) ;
2021-05-16 20:25:17 +03:00
return err ;
2016-04-11 17:24:27 +03:00
}
2012-11-23 19:13:00 +04:00
2021-05-16 20:25:17 +03:00
return devm_iio_device_register ( & client - > dev , iio ) ;
2012-11-23 19:13:00 +04:00
}
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 19:13:00 +04:00
{ }
} ;
MODULE_DEVICE_TABLE ( i2c , adc081c_id ) ;
2021-12-05 20:27:28 +03:00
static const struct acpi_device_id adc081c_acpi_match [ ] = {
/* Used on some AAEON boards */
{ " ADC081C " , ( kernel_ulong_t ) & adcxx1c_models [ ADC081C ] } ,
{ }
} ;
MODULE_DEVICE_TABLE ( acpi , adc081c_acpi_match ) ;
2012-11-23 19:13:00 +04:00
static const struct of_device_id adc081c_of_match [ ] = {
2021-12-05 20:27:28 +03:00
{ . compatible = " ti,adc081c " , . data = & adcxx1c_models [ ADC081C ] } ,
{ . compatible = " ti,adc101c " , . data = & adcxx1c_models [ ADC101C ] } ,
{ . compatible = " ti,adc121c " , . data = & adcxx1c_models [ ADC121C ] } ,
2012-11-23 19:13:00 +04:00
{ }
} ;
MODULE_DEVICE_TABLE ( of , adc081c_of_match ) ;
static struct i2c_driver adc081c_driver = {
. driver = {
. name = " adc081c " ,
2020-06-28 15:36:46 +03:00
. of_match_table = adc081c_of_match ,
2021-12-05 20:27:28 +03:00
. acpi_match_table = adc081c_acpi_match ,
2012-11-23 19:13:00 +04:00
} ,
. probe = adc081c_probe ,
. 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 19:13:00 +04:00
MODULE_LICENSE ( " GPL v2 " ) ;