ti-adc081c.c 3.1 KB

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