axp288_adc.c 9.0 KB

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