axp288_adc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #define AXP288_ADC_TS_PIN_GPADC 0xF2
  29. #define AXP288_ADC_TS_PIN_ON 0xF3
  30. enum axp288_adc_id {
  31. AXP288_ADC_TS,
  32. AXP288_ADC_PMIC,
  33. AXP288_ADC_GP,
  34. AXP288_ADC_BATT_CHRG_I,
  35. AXP288_ADC_BATT_DISCHRG_I,
  36. AXP288_ADC_BATT_V,
  37. AXP288_ADC_NR_CHAN,
  38. };
  39. struct axp288_adc_info {
  40. int irq;
  41. struct regmap *regmap;
  42. };
  43. static const struct iio_chan_spec const axp288_adc_channels[] = {
  44. {
  45. .indexed = 1,
  46. .type = IIO_TEMP,
  47. .channel = 0,
  48. .address = AXP288_TS_ADC_H,
  49. .datasheet_name = "TS_PIN",
  50. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  51. }, {
  52. .indexed = 1,
  53. .type = IIO_TEMP,
  54. .channel = 1,
  55. .address = AXP288_PMIC_ADC_H,
  56. .datasheet_name = "PMIC_TEMP",
  57. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  58. }, {
  59. .indexed = 1,
  60. .type = IIO_TEMP,
  61. .channel = 2,
  62. .address = AXP288_GP_ADC_H,
  63. .datasheet_name = "GPADC",
  64. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  65. }, {
  66. .indexed = 1,
  67. .type = IIO_CURRENT,
  68. .channel = 3,
  69. .address = AXP20X_BATT_CHRG_I_H,
  70. .datasheet_name = "BATT_CHG_I",
  71. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  72. }, {
  73. .indexed = 1,
  74. .type = IIO_CURRENT,
  75. .channel = 4,
  76. .address = AXP20X_BATT_DISCHRG_I_H,
  77. .datasheet_name = "BATT_DISCHRG_I",
  78. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  79. }, {
  80. .indexed = 1,
  81. .type = IIO_VOLTAGE,
  82. .channel = 5,
  83. .address = AXP20X_BATT_V_H,
  84. .datasheet_name = "BATT_V",
  85. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  86. },
  87. };
  88. #define AXP288_ADC_MAP(_adc_channel_label, _consumer_dev_name, \
  89. _consumer_channel) \
  90. { \
  91. .adc_channel_label = _adc_channel_label, \
  92. .consumer_dev_name = _consumer_dev_name, \
  93. .consumer_channel = _consumer_channel, \
  94. }
  95. /* for consumer drivers */
  96. static struct iio_map axp288_adc_default_maps[] = {
  97. AXP288_ADC_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
  98. AXP288_ADC_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
  99. AXP288_ADC_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
  100. AXP288_ADC_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
  101. AXP288_ADC_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
  102. AXP288_ADC_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
  103. {},
  104. };
  105. static int axp288_adc_read_channel(int *val, unsigned long address,
  106. struct regmap *regmap)
  107. {
  108. u8 buf[2];
  109. if (regmap_bulk_read(regmap, address, buf, 2))
  110. return -EIO;
  111. *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
  112. return IIO_VAL_INT;
  113. }
  114. static int axp288_adc_set_ts(struct regmap *regmap, unsigned int mode,
  115. unsigned long address)
  116. {
  117. /* channels other than GPADC do not need to switch TS pin */
  118. if (address != AXP288_GP_ADC_H)
  119. return 0;
  120. return regmap_write(regmap, AXP288_ADC_TS_PIN_CTRL, mode);
  121. }
  122. static int axp288_adc_read_raw(struct iio_dev *indio_dev,
  123. struct iio_chan_spec const *chan,
  124. int *val, int *val2, long mask)
  125. {
  126. int ret;
  127. struct axp288_adc_info *info = iio_priv(indio_dev);
  128. mutex_lock(&indio_dev->mlock);
  129. switch (mask) {
  130. case IIO_CHAN_INFO_RAW:
  131. if (axp288_adc_set_ts(info->regmap, AXP288_ADC_TS_PIN_GPADC,
  132. chan->address)) {
  133. dev_err(&indio_dev->dev, "GPADC mode\n");
  134. ret = -EINVAL;
  135. break;
  136. }
  137. ret = axp288_adc_read_channel(val, chan->address, info->regmap);
  138. if (axp288_adc_set_ts(info->regmap, AXP288_ADC_TS_PIN_ON,
  139. chan->address))
  140. dev_err(&indio_dev->dev, "TS pin restore\n");
  141. break;
  142. default:
  143. ret = -EINVAL;
  144. }
  145. mutex_unlock(&indio_dev->mlock);
  146. return ret;
  147. }
  148. static int axp288_adc_set_state(struct regmap *regmap)
  149. {
  150. /* ADC should be always enabled for internal FG to function */
  151. if (regmap_write(regmap, AXP288_ADC_TS_PIN_CTRL, AXP288_ADC_TS_PIN_ON))
  152. return -EIO;
  153. return regmap_write(regmap, AXP20X_ADC_EN1, AXP288_ADC_EN_MASK);
  154. }
  155. static const struct iio_info axp288_adc_iio_info = {
  156. .read_raw = &axp288_adc_read_raw,
  157. .driver_module = THIS_MODULE,
  158. };
  159. static int axp288_adc_probe(struct platform_device *pdev)
  160. {
  161. int ret;
  162. struct axp288_adc_info *info;
  163. struct iio_dev *indio_dev;
  164. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  165. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  166. if (!indio_dev)
  167. return -ENOMEM;
  168. info = iio_priv(indio_dev);
  169. info->irq = platform_get_irq(pdev, 0);
  170. if (info->irq < 0) {
  171. dev_err(&pdev->dev, "no irq resource?\n");
  172. return info->irq;
  173. }
  174. platform_set_drvdata(pdev, indio_dev);
  175. info->regmap = axp20x->regmap;
  176. /*
  177. * Set ADC to enabled state at all time, including system suspend.
  178. * otherwise internal fuel gauge functionality may be affected.
  179. */
  180. ret = axp288_adc_set_state(axp20x->regmap);
  181. if (ret) {
  182. dev_err(&pdev->dev, "unable to enable ADC device\n");
  183. return ret;
  184. }
  185. indio_dev->dev.parent = &pdev->dev;
  186. indio_dev->name = pdev->name;
  187. indio_dev->channels = axp288_adc_channels;
  188. indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels);
  189. indio_dev->info = &axp288_adc_iio_info;
  190. indio_dev->modes = INDIO_DIRECT_MODE;
  191. ret = iio_map_array_register(indio_dev, axp288_adc_default_maps);
  192. if (ret < 0)
  193. return ret;
  194. ret = iio_device_register(indio_dev);
  195. if (ret < 0) {
  196. dev_err(&pdev->dev, "unable to register iio device\n");
  197. goto err_array_unregister;
  198. }
  199. return 0;
  200. err_array_unregister:
  201. iio_map_array_unregister(indio_dev);
  202. return ret;
  203. }
  204. static int axp288_adc_remove(struct platform_device *pdev)
  205. {
  206. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  207. iio_device_unregister(indio_dev);
  208. iio_map_array_unregister(indio_dev);
  209. return 0;
  210. }
  211. static const struct platform_device_id axp288_adc_id_table[] = {
  212. { .name = "axp288_adc" },
  213. {},
  214. };
  215. static struct platform_driver axp288_adc_driver = {
  216. .probe = axp288_adc_probe,
  217. .remove = axp288_adc_remove,
  218. .id_table = axp288_adc_id_table,
  219. .driver = {
  220. .name = "axp288_adc",
  221. },
  222. };
  223. MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
  224. module_platform_driver(axp288_adc_driver);
  225. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
  226. MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
  227. MODULE_LICENSE("GPL");