axp288_adc.c 8.5 KB

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