2 * Copyright (C) 2012 Avionic Design GmbH
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/i2c.h>
11 #include <linux/module.h>
14 #include <linux/iio/iio.h>
15 #include <linux/regulator/consumer.h>
18 struct i2c_client
*i2c
;
19 struct regulator
*ref
;
22 #define REG_CONV_RES 0x00
24 static int adc081c_read_raw(struct iio_dev
*iio
,
25 struct iio_chan_spec
const *channel
, int *value
,
26 int *shift
, long mask
)
28 struct adc081c
*adc
= iio_priv(iio
);
32 case IIO_CHAN_INFO_RAW
:
33 err
= i2c_smbus_read_word_swapped(adc
->i2c
, REG_CONV_RES
);
37 *value
= (err
>> 4) & 0xff;
40 case IIO_CHAN_INFO_SCALE
:
41 err
= regulator_get_voltage(adc
->ref
);
48 return IIO_VAL_FRACTIONAL_LOG2
;
57 static const struct iio_chan_spec adc081c_channel
= {
59 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
60 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
63 static const struct iio_info adc081c_info
= {
64 .read_raw
= adc081c_read_raw
,
65 .driver_module
= THIS_MODULE
,
68 static int adc081c_probe(struct i2c_client
*client
,
69 const struct i2c_device_id
*id
)
75 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
78 iio
= devm_iio_device_alloc(&client
->dev
, sizeof(*adc
));
85 adc
->ref
= devm_regulator_get(&client
->dev
, "vref");
87 return PTR_ERR(adc
->ref
);
89 err
= regulator_enable(adc
->ref
);
93 iio
->dev
.parent
= &client
->dev
;
94 iio
->name
= dev_name(&client
->dev
);
95 iio
->modes
= INDIO_DIRECT_MODE
;
96 iio
->info
= &adc081c_info
;
98 iio
->channels
= &adc081c_channel
;
99 iio
->num_channels
= 1;
101 err
= iio_device_register(iio
);
103 goto regulator_disable
;
105 i2c_set_clientdata(client
, iio
);
110 regulator_disable(adc
->ref
);
115 static int adc081c_remove(struct i2c_client
*client
)
117 struct iio_dev
*iio
= i2c_get_clientdata(client
);
118 struct adc081c
*adc
= iio_priv(iio
);
120 iio_device_unregister(iio
);
121 regulator_disable(adc
->ref
);
126 static const struct i2c_device_id adc081c_id
[] = {
130 MODULE_DEVICE_TABLE(i2c
, adc081c_id
);
133 static const struct of_device_id adc081c_of_match
[] = {
134 { .compatible
= "ti,adc081c" },
137 MODULE_DEVICE_TABLE(of
, adc081c_of_match
);
140 static struct i2c_driver adc081c_driver
= {
143 .owner
= THIS_MODULE
,
144 .of_match_table
= of_match_ptr(adc081c_of_match
),
146 .probe
= adc081c_probe
,
147 .remove
= adc081c_remove
,
148 .id_table
= adc081c_id
,
150 module_i2c_driver(adc081c_driver
);
152 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
153 MODULE_DESCRIPTION("Texas Instruments ADC081C021/027 driver");
154 MODULE_LICENSE("GPL v2");