axp288_adc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * axp288_adc.c - X-Powers AXP288 PMIC ADC Driver
  3. *
  4. * Copyright (C) 2014 Intel Corporation
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/mfd/axp20x.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/machine.h>
  26. #include <linux/iio/driver.h>
  27. #define AXP288_ADC_EN_MASK 0xF1
  28. enum axp288_adc_id {
  29. AXP288_ADC_TS,
  30. AXP288_ADC_PMIC,
  31. AXP288_ADC_GP,
  32. AXP288_ADC_BATT_CHRG_I,
  33. AXP288_ADC_BATT_DISCHRG_I,
  34. AXP288_ADC_BATT_V,
  35. AXP288_ADC_NR_CHAN,
  36. };
  37. struct axp288_adc_info {
  38. int irq;
  39. struct regmap *regmap;
  40. };
  41. static const struct iio_chan_spec axp288_adc_channels[] = {
  42. {
  43. .indexed = 1,
  44. .type = IIO_TEMP,
  45. .channel = 0,
  46. .address = AXP288_TS_ADC_H,
  47. .datasheet_name = "TS_PIN",
  48. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  49. }, {
  50. .indexed = 1,
  51. .type = IIO_TEMP,
  52. .channel = 1,
  53. .address = AXP288_PMIC_ADC_H,
  54. .datasheet_name = "PMIC_TEMP",
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  56. }, {
  57. .indexed = 1,
  58. .type = IIO_TEMP,
  59. .channel = 2,
  60. .address = AXP288_GP_ADC_H,
  61. .datasheet_name = "GPADC",
  62. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  63. }, {
  64. .indexed = 1,
  65. .type = IIO_CURRENT,
  66. .channel = 3,
  67. .address = AXP20X_BATT_CHRG_I_H,
  68. .datasheet_name = "BATT_CHG_I",
  69. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  70. }, {
  71. .indexed = 1,
  72. .type = IIO_CURRENT,
  73. .channel = 4,
  74. .address = AXP20X_BATT_DISCHRG_I_H,
  75. .datasheet_name = "BATT_DISCHRG_I",
  76. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  77. }, {
  78. .indexed = 1,
  79. .type = IIO_VOLTAGE,
  80. .channel = 5,
  81. .address = AXP20X_BATT_V_H,
  82. .datasheet_name = "BATT_V",
  83. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  84. },
  85. };
  86. #define AXP288_ADC_MAP(_adc_channel_label, _consumer_dev_name, \
  87. _consumer_channel) \
  88. { \
  89. .adc_channel_label = _adc_channel_label, \
  90. .consumer_dev_name = _consumer_dev_name, \
  91. .consumer_channel = _consumer_channel, \
  92. }
  93. /* for consumer drivers */
  94. static struct iio_map axp288_adc_default_maps[] = {
  95. AXP288_ADC_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
  96. AXP288_ADC_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
  97. AXP288_ADC_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
  98. AXP288_ADC_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
  99. AXP288_ADC_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
  100. AXP288_ADC_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
  101. {},
  102. };
  103. static int axp288_adc_read_channel(int *val, unsigned long address,
  104. struct regmap *regmap)
  105. {
  106. u8 buf[2];
  107. if (regmap_bulk_read(regmap, address, buf, 2))
  108. return -EIO;
  109. *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
  110. return IIO_VAL_INT;
  111. }
  112. static int axp288_adc_read_raw(struct iio_dev *indio_dev,
  113. struct iio_chan_spec const *chan,
  114. int *val, int *val2, long mask)
  115. {
  116. int ret;
  117. struct axp288_adc_info *info = iio_priv(indio_dev);
  118. mutex_lock(&indio_dev->mlock);
  119. switch (mask) {
  120. case IIO_CHAN_INFO_RAW:
  121. ret = axp288_adc_read_channel(val, chan->address, info->regmap);
  122. break;
  123. default:
  124. ret = -EINVAL;
  125. }
  126. mutex_unlock(&indio_dev->mlock);
  127. return ret;
  128. }
  129. static const struct iio_info axp288_adc_iio_info = {
  130. .read_raw = &axp288_adc_read_raw,
  131. .driver_module = THIS_MODULE,
  132. };
  133. static int axp288_adc_probe(struct platform_device *pdev)
  134. {
  135. int ret;
  136. struct axp288_adc_info *info;
  137. struct iio_dev *indio_dev;
  138. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  139. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  140. if (!indio_dev)
  141. return -ENOMEM;
  142. info = iio_priv(indio_dev);
  143. info->irq = platform_get_irq(pdev, 0);
  144. if (info->irq < 0) {
  145. dev_err(&pdev->dev, "no irq resource?\n");
  146. return info->irq;
  147. }
  148. platform_set_drvdata(pdev, indio_dev);
  149. info->regmap = axp20x->regmap;
  150. /*
  151. * Set ADC to enabled state at all time, including system suspend.
  152. * otherwise internal fuel gauge functionality may be affected.
  153. */
  154. ret = regmap_write(info->regmap, AXP20X_ADC_EN1, AXP288_ADC_EN_MASK);
  155. if (ret) {
  156. dev_err(&pdev->dev, "unable to enable ADC device\n");
  157. return ret;
  158. }
  159. indio_dev->dev.parent = &pdev->dev;
  160. indio_dev->name = pdev->name;
  161. indio_dev->channels = axp288_adc_channels;
  162. indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels);
  163. indio_dev->info = &axp288_adc_iio_info;
  164. indio_dev->modes = INDIO_DIRECT_MODE;
  165. ret = iio_map_array_register(indio_dev, axp288_adc_default_maps);
  166. if (ret < 0)
  167. return ret;
  168. ret = iio_device_register(indio_dev);
  169. if (ret < 0) {
  170. dev_err(&pdev->dev, "unable to register iio device\n");
  171. goto err_array_unregister;
  172. }
  173. return 0;
  174. err_array_unregister:
  175. iio_map_array_unregister(indio_dev);
  176. return ret;
  177. }
  178. static int axp288_adc_remove(struct platform_device *pdev)
  179. {
  180. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  181. iio_device_unregister(indio_dev);
  182. iio_map_array_unregister(indio_dev);
  183. return 0;
  184. }
  185. static const struct platform_device_id axp288_adc_id_table[] = {
  186. { .name = "axp288_adc" },
  187. {},
  188. };
  189. static struct platform_driver axp288_adc_driver = {
  190. .probe = axp288_adc_probe,
  191. .remove = axp288_adc_remove,
  192. .id_table = axp288_adc_id_table,
  193. .driver = {
  194. .name = "axp288_adc",
  195. },
  196. };
  197. MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
  198. module_platform_driver(axp288_adc_driver);
  199. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
  200. MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
  201. MODULE_LICENSE("GPL");