axp288_adc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * axp288_adc.c - X-Powers AXP288 PMIC ADC Driver
  4. *
  5. * Copyright (C) 2014 Intel Corporation
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. */
  9. #include <linux/dmi.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/mfd/axp20x.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/machine.h>
  19. #include <linux/iio/driver.h>
  20. /*
  21. * This mask enables all ADCs except for the battery temp-sensor (TS), that is
  22. * left as-is to avoid breaking charging on devices without a temp-sensor.
  23. */
  24. #define AXP288_ADC_EN_MASK 0xF0
  25. #define AXP288_ADC_TS_ENABLE 0x01
  26. #define AXP288_ADC_TS_BIAS_MASK GENMASK(5, 4)
  27. #define AXP288_ADC_TS_BIAS_20UA (0 << 4)
  28. #define AXP288_ADC_TS_BIAS_40UA (1 << 4)
  29. #define AXP288_ADC_TS_BIAS_60UA (2 << 4)
  30. #define AXP288_ADC_TS_BIAS_80UA (3 << 4)
  31. #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK GENMASK(1, 0)
  32. #define AXP288_ADC_TS_CURRENT_OFF (0 << 0)
  33. #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING (1 << 0)
  34. #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND (2 << 0)
  35. #define AXP288_ADC_TS_CURRENT_ON (3 << 0)
  36. enum axp288_adc_id {
  37. AXP288_ADC_TS,
  38. AXP288_ADC_PMIC,
  39. AXP288_ADC_GP,
  40. AXP288_ADC_BATT_CHRG_I,
  41. AXP288_ADC_BATT_DISCHRG_I,
  42. AXP288_ADC_BATT_V,
  43. AXP288_ADC_NR_CHAN,
  44. };
  45. struct axp288_adc_info {
  46. int irq;
  47. struct regmap *regmap;
  48. /* lock to protect against multiple access to the device */
  49. struct mutex lock;
  50. bool ts_enabled;
  51. };
  52. static const struct iio_chan_spec axp288_adc_channels[] = {
  53. {
  54. .indexed = 1,
  55. .type = IIO_TEMP,
  56. .channel = 0,
  57. .address = AXP288_TS_ADC_H,
  58. .datasheet_name = "TS_PIN",
  59. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  60. }, {
  61. .indexed = 1,
  62. .type = IIO_TEMP,
  63. .channel = 1,
  64. .address = AXP288_PMIC_ADC_H,
  65. .datasheet_name = "PMIC_TEMP",
  66. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  67. }, {
  68. .indexed = 1,
  69. .type = IIO_TEMP,
  70. .channel = 2,
  71. .address = AXP288_GP_ADC_H,
  72. .datasheet_name = "GPADC",
  73. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  74. }, {
  75. .indexed = 1,
  76. .type = IIO_CURRENT,
  77. .channel = 3,
  78. .address = AXP20X_BATT_CHRG_I_H,
  79. .datasheet_name = "BATT_CHG_I",
  80. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  81. }, {
  82. .indexed = 1,
  83. .type = IIO_CURRENT,
  84. .channel = 4,
  85. .address = AXP20X_BATT_DISCHRG_I_H,
  86. .datasheet_name = "BATT_DISCHRG_I",
  87. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  88. }, {
  89. .indexed = 1,
  90. .type = IIO_VOLTAGE,
  91. .channel = 5,
  92. .address = AXP20X_BATT_V_H,
  93. .datasheet_name = "BATT_V",
  94. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  95. },
  96. };
  97. /* for consumer drivers */
  98. static const struct iio_map axp288_adc_default_maps[] = {
  99. IIO_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
  100. IIO_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
  101. IIO_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
  102. IIO_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
  103. IIO_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
  104. IIO_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
  105. { }
  106. };
  107. static int axp288_adc_read_channel(int *val, unsigned long address,
  108. struct regmap *regmap)
  109. {
  110. u8 buf[2];
  111. if (regmap_bulk_read(regmap, address, buf, 2))
  112. return -EIO;
  113. *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
  114. return IIO_VAL_INT;
  115. }
  116. /*
  117. * The current-source used for the battery temp-sensor (TS) is shared
  118. * with the GPADC. For proper fuel-gauge and charger operation the TS
  119. * current-source needs to be permanently on. But to read the GPADC we
  120. * need to temporary switch the TS current-source to ondemand, so that
  121. * the GPADC can use it, otherwise we will always read an all 0 value.
  122. */
  123. static int axp288_adc_set_ts(struct axp288_adc_info *info,
  124. unsigned int mode, unsigned long address)
  125. {
  126. int ret;
  127. /* No need to switch the current-source if the TS pin is disabled */
  128. if (!info->ts_enabled)
  129. return 0;
  130. /* Channels other than GPADC do not need the current source */
  131. if (address != AXP288_GP_ADC_H)
  132. return 0;
  133. ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
  134. AXP288_ADC_TS_CURRENT_ON_OFF_MASK, mode);
  135. if (ret)
  136. return ret;
  137. /* When switching to the GPADC pin give things some time to settle */
  138. if (mode == AXP288_ADC_TS_CURRENT_ON_ONDEMAND)
  139. usleep_range(6000, 10000);
  140. return 0;
  141. }
  142. static int axp288_adc_read_raw(struct iio_dev *indio_dev,
  143. struct iio_chan_spec const *chan,
  144. int *val, int *val2, long mask)
  145. {
  146. int ret;
  147. struct axp288_adc_info *info = iio_priv(indio_dev);
  148. mutex_lock(&info->lock);
  149. switch (mask) {
  150. case IIO_CHAN_INFO_RAW:
  151. if (axp288_adc_set_ts(info, AXP288_ADC_TS_CURRENT_ON_ONDEMAND,
  152. chan->address)) {
  153. dev_err(&indio_dev->dev, "GPADC mode\n");
  154. ret = -EINVAL;
  155. break;
  156. }
  157. ret = axp288_adc_read_channel(val, chan->address, info->regmap);
  158. if (axp288_adc_set_ts(info, AXP288_ADC_TS_CURRENT_ON,
  159. chan->address))
  160. dev_err(&indio_dev->dev, "TS pin restore\n");
  161. break;
  162. default:
  163. ret = -EINVAL;
  164. }
  165. mutex_unlock(&info->lock);
  166. return ret;
  167. }
  168. /*
  169. * We rely on the machine's firmware to correctly setup the TS pin bias current
  170. * at boot. This lists systems with broken fw where we need to set it ourselves.
  171. */
  172. static const struct dmi_system_id axp288_adc_ts_bias_override[] = {
  173. {
  174. /* Lenovo Ideapad 100S (11 inch) */
  175. .matches = {
  176. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  177. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 100S-11IBY"),
  178. },
  179. .driver_data = (void *)(uintptr_t)AXP288_ADC_TS_BIAS_80UA,
  180. },
  181. {
  182. /* Nuvision Solo 10 Draw */
  183. .matches = {
  184. DMI_MATCH(DMI_SYS_VENDOR, "TMAX"),
  185. DMI_MATCH(DMI_PRODUCT_NAME, "TM101W610L"),
  186. },
  187. .driver_data = (void *)(uintptr_t)AXP288_ADC_TS_BIAS_80UA,
  188. },
  189. { }
  190. };
  191. static int axp288_adc_initialize(struct axp288_adc_info *info)
  192. {
  193. const struct dmi_system_id *bias_override;
  194. int ret, adc_enable_val;
  195. bias_override = dmi_first_match(axp288_adc_ts_bias_override);
  196. if (bias_override) {
  197. ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
  198. AXP288_ADC_TS_BIAS_MASK,
  199. (uintptr_t)bias_override->driver_data);
  200. if (ret)
  201. return ret;
  202. }
  203. /*
  204. * Determine if the TS pin is enabled and set the TS current-source
  205. * accordingly.
  206. */
  207. ret = regmap_read(info->regmap, AXP20X_ADC_EN1, &adc_enable_val);
  208. if (ret)
  209. return ret;
  210. if (adc_enable_val & AXP288_ADC_TS_ENABLE) {
  211. info->ts_enabled = true;
  212. ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
  213. AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
  214. AXP288_ADC_TS_CURRENT_ON);
  215. } else {
  216. info->ts_enabled = false;
  217. ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
  218. AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
  219. AXP288_ADC_TS_CURRENT_OFF);
  220. }
  221. if (ret)
  222. return ret;
  223. /* Turn on the ADC for all channels except TS, leave TS as is */
  224. return regmap_set_bits(info->regmap, AXP20X_ADC_EN1,
  225. AXP288_ADC_EN_MASK);
  226. }
  227. static const struct iio_info axp288_adc_iio_info = {
  228. .read_raw = &axp288_adc_read_raw,
  229. };
  230. static int axp288_adc_probe(struct platform_device *pdev)
  231. {
  232. int ret;
  233. struct axp288_adc_info *info;
  234. struct iio_dev *indio_dev;
  235. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  236. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  237. if (!indio_dev)
  238. return -ENOMEM;
  239. info = iio_priv(indio_dev);
  240. info->irq = platform_get_irq(pdev, 0);
  241. if (info->irq < 0)
  242. return info->irq;
  243. info->regmap = axp20x->regmap;
  244. /*
  245. * Set ADC to enabled state at all time, including system suspend.
  246. * otherwise internal fuel gauge functionality may be affected.
  247. */
  248. ret = axp288_adc_initialize(info);
  249. if (ret) {
  250. dev_err(&pdev->dev, "unable to enable ADC device\n");
  251. return ret;
  252. }
  253. indio_dev->name = pdev->name;
  254. indio_dev->channels = axp288_adc_channels;
  255. indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels);
  256. indio_dev->info = &axp288_adc_iio_info;
  257. indio_dev->modes = INDIO_DIRECT_MODE;
  258. ret = devm_iio_map_array_register(&pdev->dev, indio_dev, axp288_adc_default_maps);
  259. if (ret < 0)
  260. return ret;
  261. mutex_init(&info->lock);
  262. return devm_iio_device_register(&pdev->dev, indio_dev);
  263. }
  264. static const struct platform_device_id axp288_adc_id_table[] = {
  265. { .name = "axp288_adc" },
  266. { }
  267. };
  268. static struct platform_driver axp288_adc_driver = {
  269. .probe = axp288_adc_probe,
  270. .id_table = axp288_adc_id_table,
  271. .driver = {
  272. .name = "axp288_adc",
  273. },
  274. };
  275. MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
  276. module_platform_driver(axp288_adc_driver);
  277. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
  278. MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
  279. MODULE_LICENSE("GPL");