lp8788_adc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * TI LP8788 MFD - ADC driver
  3. *
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/driver.h>
  15. #include <linux/iio/machine.h>
  16. #include <linux/mfd/lp8788.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. /* register address */
  22. #define LP8788_ADC_CONF 0x60
  23. #define LP8788_ADC_RAW 0x61
  24. #define LP8788_ADC_DONE 0x63
  25. #define ADC_CONV_START 1
  26. struct lp8788_adc {
  27. struct lp8788 *lp;
  28. struct iio_map *map;
  29. struct mutex lock;
  30. };
  31. static const int lp8788_scale[LPADC_MAX] = {
  32. [LPADC_VBATT_5P5] = 1343101,
  33. [LPADC_VIN_CHG] = 3052503,
  34. [LPADC_IBATT] = 610500,
  35. [LPADC_IC_TEMP] = 61050,
  36. [LPADC_VBATT_6P0] = 1465201,
  37. [LPADC_VBATT_5P0] = 1221001,
  38. [LPADC_ADC1] = 610500,
  39. [LPADC_ADC2] = 610500,
  40. [LPADC_VDD] = 1025641,
  41. [LPADC_VCOIN] = 757020,
  42. [LPADC_ADC3] = 610500,
  43. [LPADC_ADC4] = 610500,
  44. };
  45. static int lp8788_get_adc_result(struct lp8788_adc *adc, enum lp8788_adc_id id,
  46. int *val)
  47. {
  48. unsigned int msb;
  49. unsigned int lsb;
  50. unsigned int result;
  51. u8 data;
  52. u8 rawdata[2];
  53. int size = ARRAY_SIZE(rawdata);
  54. int retry = 5;
  55. int ret;
  56. data = (id << 1) | ADC_CONV_START;
  57. ret = lp8788_write_byte(adc->lp, LP8788_ADC_CONF, data);
  58. if (ret)
  59. goto err_io;
  60. /* retry until adc conversion is done */
  61. data = 0;
  62. while (retry--) {
  63. usleep_range(100, 200);
  64. ret = lp8788_read_byte(adc->lp, LP8788_ADC_DONE, &data);
  65. if (ret)
  66. goto err_io;
  67. /* conversion done */
  68. if (data)
  69. break;
  70. }
  71. ret = lp8788_read_multi_bytes(adc->lp, LP8788_ADC_RAW, rawdata, size);
  72. if (ret)
  73. goto err_io;
  74. msb = (rawdata[0] << 4) & 0x00000ff0;
  75. lsb = (rawdata[1] >> 4) & 0x0000000f;
  76. result = msb | lsb;
  77. *val = result;
  78. return 0;
  79. err_io:
  80. return ret;
  81. }
  82. static int lp8788_adc_read_raw(struct iio_dev *indio_dev,
  83. struct iio_chan_spec const *chan,
  84. int *val, int *val2, long mask)
  85. {
  86. struct lp8788_adc *adc = iio_priv(indio_dev);
  87. enum lp8788_adc_id id = chan->channel;
  88. int ret;
  89. mutex_lock(&adc->lock);
  90. switch (mask) {
  91. case IIO_CHAN_INFO_RAW:
  92. ret = lp8788_get_adc_result(adc, id, val) ? -EIO : IIO_VAL_INT;
  93. break;
  94. case IIO_CHAN_INFO_SCALE:
  95. *val = lp8788_scale[id] / 1000000;
  96. *val2 = lp8788_scale[id] % 1000000;
  97. ret = IIO_VAL_INT_PLUS_MICRO;
  98. break;
  99. default:
  100. ret = -EINVAL;
  101. break;
  102. }
  103. mutex_unlock(&adc->lock);
  104. return ret;
  105. }
  106. static const struct iio_info lp8788_adc_info = {
  107. .read_raw = &lp8788_adc_read_raw,
  108. .driver_module = THIS_MODULE,
  109. };
  110. #define LP8788_CHAN(_id, _type) { \
  111. .type = _type, \
  112. .indexed = 1, \
  113. .channel = LPADC_##_id, \
  114. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  115. BIT(IIO_CHAN_INFO_SCALE), \
  116. .datasheet_name = #_id, \
  117. }
  118. static const struct iio_chan_spec lp8788_adc_channels[] = {
  119. [LPADC_VBATT_5P5] = LP8788_CHAN(VBATT_5P5, IIO_VOLTAGE),
  120. [LPADC_VIN_CHG] = LP8788_CHAN(VIN_CHG, IIO_VOLTAGE),
  121. [LPADC_IBATT] = LP8788_CHAN(IBATT, IIO_CURRENT),
  122. [LPADC_IC_TEMP] = LP8788_CHAN(IC_TEMP, IIO_TEMP),
  123. [LPADC_VBATT_6P0] = LP8788_CHAN(VBATT_6P0, IIO_VOLTAGE),
  124. [LPADC_VBATT_5P0] = LP8788_CHAN(VBATT_5P0, IIO_VOLTAGE),
  125. [LPADC_ADC1] = LP8788_CHAN(ADC1, IIO_VOLTAGE),
  126. [LPADC_ADC2] = LP8788_CHAN(ADC2, IIO_VOLTAGE),
  127. [LPADC_VDD] = LP8788_CHAN(VDD, IIO_VOLTAGE),
  128. [LPADC_VCOIN] = LP8788_CHAN(VCOIN, IIO_VOLTAGE),
  129. [LPADC_ADC3] = LP8788_CHAN(ADC3, IIO_VOLTAGE),
  130. [LPADC_ADC4] = LP8788_CHAN(ADC4, IIO_VOLTAGE),
  131. };
  132. /* default maps used by iio consumer (lp8788-charger driver) */
  133. static struct iio_map lp8788_default_iio_maps[] = {
  134. {
  135. .consumer_dev_name = "lp8788-charger",
  136. .consumer_channel = "lp8788_vbatt_5p0",
  137. .adc_channel_label = "VBATT_5P0",
  138. },
  139. {
  140. .consumer_dev_name = "lp8788-charger",
  141. .consumer_channel = "lp8788_adc1",
  142. .adc_channel_label = "ADC1",
  143. },
  144. { }
  145. };
  146. static int lp8788_iio_map_register(struct iio_dev *indio_dev,
  147. struct lp8788_platform_data *pdata,
  148. struct lp8788_adc *adc)
  149. {
  150. struct iio_map *map;
  151. int ret;
  152. map = (!pdata || !pdata->adc_pdata) ?
  153. lp8788_default_iio_maps : pdata->adc_pdata;
  154. ret = iio_map_array_register(indio_dev, map);
  155. if (ret) {
  156. dev_err(&indio_dev->dev, "iio map err: %d\n", ret);
  157. return ret;
  158. }
  159. adc->map = map;
  160. return 0;
  161. }
  162. static int lp8788_adc_probe(struct platform_device *pdev)
  163. {
  164. struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
  165. struct iio_dev *indio_dev;
  166. struct lp8788_adc *adc;
  167. int ret;
  168. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  169. if (!indio_dev)
  170. return -ENOMEM;
  171. adc = iio_priv(indio_dev);
  172. adc->lp = lp;
  173. platform_set_drvdata(pdev, indio_dev);
  174. indio_dev->dev.of_node = pdev->dev.of_node;
  175. ret = lp8788_iio_map_register(indio_dev, lp->pdata, adc);
  176. if (ret)
  177. return ret;
  178. mutex_init(&adc->lock);
  179. indio_dev->dev.parent = &pdev->dev;
  180. indio_dev->name = pdev->name;
  181. indio_dev->modes = INDIO_DIRECT_MODE;
  182. indio_dev->info = &lp8788_adc_info;
  183. indio_dev->channels = lp8788_adc_channels;
  184. indio_dev->num_channels = ARRAY_SIZE(lp8788_adc_channels);
  185. ret = iio_device_register(indio_dev);
  186. if (ret) {
  187. dev_err(&pdev->dev, "iio dev register err: %d\n", ret);
  188. goto err_iio_device;
  189. }
  190. return 0;
  191. err_iio_device:
  192. iio_map_array_unregister(indio_dev);
  193. return ret;
  194. }
  195. static int lp8788_adc_remove(struct platform_device *pdev)
  196. {
  197. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  198. iio_device_unregister(indio_dev);
  199. iio_map_array_unregister(indio_dev);
  200. return 0;
  201. }
  202. static struct platform_driver lp8788_adc_driver = {
  203. .probe = lp8788_adc_probe,
  204. .remove = lp8788_adc_remove,
  205. .driver = {
  206. .name = LP8788_DEV_ADC,
  207. },
  208. };
  209. module_platform_driver(lp8788_adc_driver);
  210. MODULE_DESCRIPTION("Texas Instruments LP8788 ADC Driver");
  211. MODULE_AUTHOR("Milo Kim");
  212. MODULE_LICENSE("GPL");
  213. MODULE_ALIAS("platform:lp8788-adc");