ti-adc081c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. *
  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.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/iio/iio.h>
  12. #include <linux/regulator/consumer.h>
  13. struct adc081c {
  14. struct i2c_client *i2c;
  15. struct regulator *ref;
  16. };
  17. #define REG_CONV_RES 0x00
  18. static int adc081c_read_raw(struct iio_dev *iio,
  19. struct iio_chan_spec const *channel, int *value,
  20. int *shift, long mask)
  21. {
  22. struct adc081c *adc = iio_priv(iio);
  23. int err;
  24. switch (mask) {
  25. case IIO_CHAN_INFO_RAW:
  26. err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
  27. if (err < 0)
  28. return err;
  29. *value = (err >> 4) & 0xff;
  30. return IIO_VAL_INT;
  31. case IIO_CHAN_INFO_SCALE:
  32. err = regulator_get_voltage(adc->ref);
  33. if (err < 0)
  34. return err;
  35. *value = err / 1000;
  36. *shift = 8;
  37. return IIO_VAL_FRACTIONAL_LOG2;
  38. default:
  39. break;
  40. }
  41. return -EINVAL;
  42. }
  43. static const struct iio_chan_spec adc081c_channel = {
  44. .type = IIO_VOLTAGE,
  45. .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT |
  46. IIO_CHAN_INFO_RAW_SEPARATE_BIT,
  47. };
  48. static const struct iio_info adc081c_info = {
  49. .read_raw = adc081c_read_raw,
  50. .driver_module = THIS_MODULE,
  51. };
  52. static int adc081c_probe(struct i2c_client *client,
  53. const struct i2c_device_id *id)
  54. {
  55. struct iio_dev *iio;
  56. struct adc081c *adc;
  57. int err;
  58. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
  59. return -ENODEV;
  60. iio = iio_device_alloc(sizeof(*adc));
  61. if (!iio)
  62. return -ENOMEM;
  63. adc = iio_priv(iio);
  64. adc->i2c = client;
  65. adc->ref = regulator_get(&client->dev, "vref");
  66. if (IS_ERR(adc->ref)) {
  67. err = PTR_ERR(adc->ref);
  68. goto iio_free;
  69. }
  70. err = regulator_enable(adc->ref);
  71. if (err < 0)
  72. goto regulator_put;
  73. iio->dev.parent = &client->dev;
  74. iio->name = dev_name(&client->dev);
  75. iio->modes = INDIO_DIRECT_MODE;
  76. iio->info = &adc081c_info;
  77. iio->channels = &adc081c_channel;
  78. iio->num_channels = 1;
  79. err = iio_device_register(iio);
  80. if (err < 0)
  81. goto regulator_disable;
  82. i2c_set_clientdata(client, iio);
  83. return 0;
  84. regulator_disable:
  85. regulator_disable(adc->ref);
  86. regulator_put:
  87. regulator_put(adc->ref);
  88. iio_free:
  89. iio_device_free(iio);
  90. return err;
  91. }
  92. static int adc081c_remove(struct i2c_client *client)
  93. {
  94. struct iio_dev *iio = i2c_get_clientdata(client);
  95. struct adc081c *adc = iio_priv(iio);
  96. iio_device_unregister(iio);
  97. regulator_disable(adc->ref);
  98. regulator_put(adc->ref);
  99. iio_device_free(iio);
  100. return 0;
  101. }
  102. static const struct i2c_device_id adc081c_id[] = {
  103. { "adc081c", 0 },
  104. { }
  105. };
  106. MODULE_DEVICE_TABLE(i2c, adc081c_id);
  107. #ifdef CONFIG_OF
  108. static const struct of_device_id adc081c_of_match[] = {
  109. { .compatible = "ti,adc081c" },
  110. { }
  111. };
  112. MODULE_DEVICE_TABLE(of, adc081c_of_match);
  113. #endif
  114. static struct i2c_driver adc081c_driver = {
  115. .driver = {
  116. .name = "adc081c",
  117. .owner = THIS_MODULE,
  118. .of_match_table = of_match_ptr(adc081c_of_match),
  119. },
  120. .probe = adc081c_probe,
  121. .remove = adc081c_remove,
  122. .id_table = adc081c_id,
  123. };
  124. module_i2c_driver(adc081c_driver);
  125. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  126. MODULE_DESCRIPTION("Texas Instruments ADC081C021/027 driver");
  127. MODULE_LICENSE("GPL v2");