axp288_adc.c 6.7 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_
  89. name, \
  90. _consumer_channel) \
  91. { \
  92. .adc_channel_label = _adc_channel_label, \
  93. .consumer_dev_name = _consumer_dev_name, \
  94. .consumer_channel = _consumer_channel, \
  95. }
  96. /* for consumer drivers */
  97. static struct iio_map axp288_adc_default_maps[] = {
  98. AXP288_ADC_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
  99. AXP288_ADC_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
  100. AXP288_ADC_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
  101. AXP288_ADC_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
  102. AXP288_ADC_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
  103. AXP288_ADC_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
  104. {},
  105. };
  106. static int axp288_adc_read_channel(int *val, unsigned long address,
  107. struct regmap *regmap)
  108. {
  109. u8 buf[2];
  110. if (regmap_bulk_read(regmap, address, buf, 2))
  111. return -EIO;
  112. *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
  113. return IIO_VAL_INT;
  114. }
  115. static int axp288_adc_set_ts(struct regmap *regmap, unsigned int mode,
  116. unsigned long address)
  117. {
  118. /* channels other than GPADC do not need to switch TS pin */
  119. if (address != AXP288_GP_ADC_H)
  120. return 0;
  121. return regmap_write(regmap, AXP288_ADC_TS_PIN_CTRL, mode);
  122. }
  123. static int axp288_adc_read_raw(struct iio_dev *indio_dev,
  124. struct iio_chan_spec const *chan,
  125. int *val, int *val2, long mask)
  126. {
  127. int ret;
  128. struct axp288_adc_info *info = iio_priv(indio_dev);
  129. mutex_lock(&indio_dev->mlock);
  130. switch (mask) {
  131. case IIO_CHAN_INFO_RAW:
  132. if (axp288_adc_set_ts(info->regmap, AXP288_ADC_TS_PIN_GPADC,
  133. chan->address)) {
  134. dev_err(&indio_dev->dev, "GPADC mode\n");
  135. ret = -EINVAL;
  136. break;
  137. }
  138. ret = axp288_adc_read_channel(val, chan->address, info->regmap);
  139. if (axp288_adc_set_ts(info->regmap, AXP288_ADC_TS_PIN_ON,
  140. chan->address))
  141. dev_err(&indio_dev->dev, "TS pin restore\n");
  142. break;
  143. default:
  144. ret = -EINVAL;
  145. }
  146. mutex_unlock(&indio_dev->mlock);
  147. return ret;
  148. }
  149. static int axp288_adc_set_state(struct regmap *regmap)
  150. {
  151. /* ADC should be always enabled for internal FG to function */
  152. if (regmap_write(regmap, AXP288_ADC_TS_PIN_CTRL, AXP288_ADC_TS_PIN_ON))
  153. return -EIO;
  154. return regmap_write(regmap, AXP20X_ADC_EN1, AXP288_ADC_EN_MASK);
  155. }
  156. static const struct iio_info axp288_adc_iio_info = {
  157. .read_raw = &axp288_adc_read_raw,
  158. .driver_module = THIS_MODULE,
  159. };
  160. static int axp288_adc_probe(struct platform_device *pdev)
  161. {
  162. int ret;
  163. struct axp288_adc_info *info;
  164. struct iio_dev *indio_dev;
  165. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  166. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  167. if (!indio_dev)
  168. return -ENOMEM;
  169. info = iio_priv(indio_dev);
  170. info->irq = platform_get_irq(pdev, 0);
  171. if (info->irq < 0) {
  172. dev_err(&pdev->dev, "no irq resource?\n");
  173. return info->irq;
  174. }
  175. platform_set_drvdata(pdev, indio_dev);
  176. info->regmap = axp20x->regmap;
  177. /*
  178. * Set ADC to enabled state at all time, including system suspend.
  179. * otherwise internal fuel gauge functionality may be affected.
  180. */
  181. ret = axp288_adc_set_state(axp20x->regmap);
  182. if (ret) {
  183. dev_err(&pdev->dev, "unable to enable ADC device\n");
  184. return ret;
  185. }
  186. indio_dev->dev.parent = &pdev->dev;
  187. indio_dev->name = pdev->name;
  188. indio_dev->channels = axp288_adc_channels;
  189. indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels);
  190. indio_dev->info = &axp288_adc_iio_info;
  191. indio_dev->modes = INDIO_DIRECT_MODE;
  192. ret = iio_map_array_register(indio_dev, axp288_adc_default_maps);
  193. if (ret < 0)
  194. return ret;
  195. ret = iio_device_register(indio_dev);
  196. if (ret < 0) {
  197. dev_err(&pdev->dev, "unable to register iio device\n");
  198. goto err_array_unregister;
  199. }
  200. return 0;
  201. err_array_unregister:
  202. iio_map_array_unregister(indio_dev);
  203. return ret;
  204. }
  205. static int axp288_adc_remove(struct platform_device *pdev)
  206. {
  207. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  208. iio_device_unregister(indio_dev);
  209. iio_map_array_unregister(indio_dev);
  210. return 0;
  211. }
  212. static const struct platform_device_id axp288_adc_id_table[] = {
  213. { .name = "axp288_adc" },
  214. {},
  215. };
  216. static struct platform_driver axp288_adc_driver = {
  217. .probe = axp288_adc_probe,
  218. .remove = axp288_adc_remove,
  219. .id_table = axp288_adc_id_table,
  220. .driver = {
  221. .name = "axp288_adc",
  222. },
  223. };
  224. MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
  225. module_platform_driver(axp288_adc_driver);
  226. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
  227. MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
  228. MODULE_LICENSE("GPL");