aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuenter Roeck <linux@roeck-us.net>2013-02-02 19:59:00 -0500
committerJonathan Cameron <jic23@kernel.org>2013-02-06 13:38:40 -0500
commita405b00e4470b15ed062de97c4ce552d41a0a60b (patch)
treec3a8efd5c7ab535628fe82500e403d3a079bd34e
parent7c3e8675f37503337d901dc254cba253326b572a (diff)
iio/adc: (max1363) Add support for external reference voltage
Implement external reference voltage as regulator named "vref". Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r--drivers/iio/adc/max1363.c52
1 files changed, 39 insertions, 13 deletions
diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c
index c5e46c62ffbb..6c1cfb74bdfc 100644
--- a/drivers/iio/adc/max1363.c
+++ b/drivers/iio/adc/max1363.c
@@ -163,6 +163,8 @@ struct max1363_chip_info {
163 * @mask_low: bitmask for enabled low thresholds 163 * @mask_low: bitmask for enabled low thresholds
164 * @thresh_high: high threshold values 164 * @thresh_high: high threshold values
165 * @thresh_low: low threshold values 165 * @thresh_low: low threshold values
166 * @vref: Reference voltage regulator
167 * @vref_uv: Actual (external or internal) reference voltage
166 */ 168 */
167struct max1363_state { 169struct max1363_state {
168 struct i2c_client *client; 170 struct i2c_client *client;
@@ -182,6 +184,8 @@ struct max1363_state {
182 /* 4x unipolar first then the fours bipolar ones */ 184 /* 4x unipolar first then the fours bipolar ones */
183 s16 thresh_high[8]; 185 s16 thresh_high[8];
184 s16 thresh_low[8]; 186 s16 thresh_low[8];
187 struct regulator *vref;
188 u32 vref_uv;
185}; 189};
186 190
187#define MAX1363_MODE_SINGLE(_num, _mask) { \ 191#define MAX1363_MODE_SINGLE(_num, _mask) { \
@@ -393,6 +397,8 @@ static int max1363_read_raw(struct iio_dev *indio_dev,
393{ 397{
394 struct max1363_state *st = iio_priv(indio_dev); 398 struct max1363_state *st = iio_priv(indio_dev);
395 int ret; 399 int ret;
400 unsigned long scale_uv;
401
396 switch (m) { 402 switch (m) {
397 case IIO_CHAN_INFO_RAW: 403 case IIO_CHAN_INFO_RAW:
398 ret = max1363_read_single_chan(indio_dev, chan, val, m); 404 ret = max1363_read_single_chan(indio_dev, chan, val, m);
@@ -400,16 +406,10 @@ static int max1363_read_raw(struct iio_dev *indio_dev,
400 return ret; 406 return ret;
401 return IIO_VAL_INT; 407 return IIO_VAL_INT;
402 case IIO_CHAN_INFO_SCALE: 408 case IIO_CHAN_INFO_SCALE:
403 if ((1 << (st->chip_info->bits + 1)) > 409 scale_uv = st->vref_uv >> st->chip_info->bits;
404 st->chip_info->int_vref_mv) { 410 *val = scale_uv / 1000;
405 *val = 0; 411 *val2 = (scale_uv % 1000) * 1000;
406 *val2 = 500000; 412 return IIO_VAL_INT_PLUS_MICRO;
407 return IIO_VAL_INT_PLUS_MICRO;
408 } else {
409 *val = (st->chip_info->int_vref_mv)
410 >> st->chip_info->bits;
411 return IIO_VAL_INT;
412 }
413 default: 413 default:
414 return -EINVAL; 414 return -EINVAL;
415 } 415 }
@@ -1390,12 +1390,16 @@ static const struct max1363_chip_info max1363_chip_info_tbl[] = {
1390 1390
1391static int max1363_initial_setup(struct max1363_state *st) 1391static int max1363_initial_setup(struct max1363_state *st)
1392{ 1392{
1393 st->setupbyte = MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD 1393 st->setupbyte = MAX1363_SETUP_INT_CLOCK
1394 | MAX1363_SETUP_POWER_UP_INT_REF
1395 | MAX1363_SETUP_INT_CLOCK
1396 | MAX1363_SETUP_UNIPOLAR 1394 | MAX1363_SETUP_UNIPOLAR
1397 | MAX1363_SETUP_NORESET; 1395 | MAX1363_SETUP_NORESET;
1398 1396
1397 if (st->vref)
1398 st->setupbyte |= MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF;
1399 else
1400 st->setupbyte |= MAX1363_SETUP_POWER_UP_INT_REF
1401 | MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT;
1402
1399 /* Set scan mode writes the config anyway so wait until then */ 1403 /* Set scan mode writes the config anyway so wait until then */
1400 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte); 1404 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
1401 st->current_mode = &max1363_mode_table[st->chip_info->default_mode]; 1405 st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
@@ -1491,6 +1495,7 @@ static int max1363_probe(struct i2c_client *client,
1491 int ret; 1495 int ret;
1492 struct max1363_state *st; 1496 struct max1363_state *st;
1493 struct iio_dev *indio_dev; 1497 struct iio_dev *indio_dev;
1498 struct regulator *vref;
1494 1499
1495 indio_dev = iio_device_alloc(sizeof(struct max1363_state)); 1500 indio_dev = iio_device_alloc(sizeof(struct max1363_state));
1496 if (indio_dev == NULL) { 1501 if (indio_dev == NULL) {
@@ -1521,6 +1526,23 @@ static int max1363_probe(struct i2c_client *client,
1521 st->chip_info = &max1363_chip_info_tbl[id->driver_data]; 1526 st->chip_info = &max1363_chip_info_tbl[id->driver_data];
1522 st->client = client; 1527 st->client = client;
1523 1528
1529 st->vref_uv = st->chip_info->int_vref_mv * 1000;
1530 vref = devm_regulator_get(&client->dev, "vref");
1531 if (!IS_ERR(vref)) {
1532 int vref_uv;
1533
1534 ret = regulator_enable(vref);
1535 if (ret)
1536 goto error_disable_reg;
1537 st->vref = vref;
1538 vref_uv = regulator_get_voltage(vref);
1539 if (vref_uv <= 0) {
1540 ret = -EINVAL;
1541 goto error_disable_reg;
1542 }
1543 st->vref_uv = vref_uv;
1544 }
1545
1524 ret = max1363_alloc_scan_masks(indio_dev); 1546 ret = max1363_alloc_scan_masks(indio_dev);
1525 if (ret) 1547 if (ret)
1526 goto error_disable_reg; 1548 goto error_disable_reg;
@@ -1562,6 +1584,8 @@ static int max1363_probe(struct i2c_client *client,
1562error_uninit_buffer: 1584error_uninit_buffer:
1563 iio_triggered_buffer_cleanup(indio_dev); 1585 iio_triggered_buffer_cleanup(indio_dev);
1564error_disable_reg: 1586error_disable_reg:
1587 if (st->vref)
1588 regulator_disable(st->vref);
1565 regulator_disable(st->reg); 1589 regulator_disable(st->reg);
1566error_unregister_map: 1590error_unregister_map:
1567 iio_map_array_unregister(indio_dev); 1591 iio_map_array_unregister(indio_dev);
@@ -1578,6 +1602,8 @@ static int max1363_remove(struct i2c_client *client)
1578 1602
1579 iio_device_unregister(indio_dev); 1603 iio_device_unregister(indio_dev);
1580 iio_triggered_buffer_cleanup(indio_dev); 1604 iio_triggered_buffer_cleanup(indio_dev);
1605 if (st->vref)
1606 regulator_disable(st->vref);
1581 regulator_disable(st->reg); 1607 regulator_disable(st->reg);
1582 iio_map_array_unregister(indio_dev); 1608 iio_map_array_unregister(indio_dev);
1583 iio_device_free(indio_dev); 1609 iio_device_free(indio_dev);