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/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_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  46. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  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 = devm_iio_device_alloc(&client->dev, sizeof(*adc));
  61. if (!iio)
  62. return -ENOMEM;
  63. adc = iio_priv(iio);
  64. adc->i2c = client;
  65. adc->ref = devm_regulator_get(&client->dev, "vref");
  66. if (IS_ERR(adc->ref))
  67. return PTR_ERR(adc->ref);
  68. err = regulator_enable(adc->ref);
  69. if (err < 0)
  70. return err;
  71. iio->dev.parent = &client->dev;
  72. iio->name = dev_name(&client->dev);
  73. iio->modes = INDIO_DIRECT_MODE;
  74. iio->info = &adc081c_info;
  75. iio->channels = &adc081c_channel;
  76. iio->num_channels = 1;
  77. err = iio_device_register(iio);
  78. if (err < 0)
  79. goto regulator_disable;
  80. i2c_set_clientdata(client, iio);
  81. return 0;
  82. regulator_disable:
  83. regulator_disable(adc->ref);
  84. return err;
  85. }
  86. static int adc081c_remove(struct i2c_client *client)
  87. {
  88. struct iio_dev *iio = i2c_get_clientdata(client);
  89. struct adc081c *adc = iio_priv(iio);
  90. iio_device_unregister(iio);
  91. regulator_disable(adc->ref);
  92. return 0;
  93. }
  94. static const struct i2c_device_id adc081c_id[] = {
  95. { "adc081c", 0 },
  96. { }
  97. };
  98. MODULE_DEVICE_TABLE(i2c, adc081c_id);
  99. #ifdef CONFIG_OF
  100. static const struct of_device_id adc081c_of_match[] = {
  101. { .compatible = "ti,adc081c" },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(of, adc081c_of_match);
  105. #endif
  106. static struct i2c_driver adc081c_driver = {
  107. .driver = {
  108. .name = "adc081c",
  109. .owner = THIS_MODULE,
  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");