ab8500-gpadc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Arun R Murthy <arun.murthy@stericsson.com>
  6. * Author: Daniel Willerud <daniel.willerud@stericsson.com>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/delay.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/completion.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/list.h>
  20. #include <linux/mfd/ab8500.h>
  21. #include <linux/mfd/abx500.h>
  22. #include <linux/mfd/ab8500/ab8500-gpadc.h>
  23. /*
  24. * GPADC register offsets
  25. * Bank : 0x0A
  26. */
  27. #define AB8500_GPADC_CTRL1_REG 0x00
  28. #define AB8500_GPADC_CTRL2_REG 0x01
  29. #define AB8500_GPADC_CTRL3_REG 0x02
  30. #define AB8500_GPADC_AUTO_TIMER_REG 0x03
  31. #define AB8500_GPADC_STAT_REG 0x04
  32. #define AB8500_GPADC_MANDATAL_REG 0x05
  33. #define AB8500_GPADC_MANDATAH_REG 0x06
  34. #define AB8500_GPADC_AUTODATAL_REG 0x07
  35. #define AB8500_GPADC_AUTODATAH_REG 0x08
  36. #define AB8500_GPADC_MUX_CTRL_REG 0x09
  37. /* gpadc constants */
  38. #define EN_VINTCORE12 0x04
  39. #define EN_VTVOUT 0x02
  40. #define EN_GPADC 0x01
  41. #define DIS_GPADC 0x00
  42. #define SW_AVG_16 0x60
  43. #define ADC_SW_CONV 0x04
  44. #define EN_BUF 0x40
  45. #define DIS_ZERO 0x00
  46. #define GPADC_BUSY 0x01
  47. /**
  48. * struct ab8500_gpadc - ab8500 GPADC device information
  49. * @dev: pointer to the struct device
  50. * @node: a list of AB8500 GPADCs, hence prepared for
  51. reentrance
  52. * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
  53. * the completion of gpadc conversion
  54. * @ab8500_gpadc_lock: structure of type mutex
  55. * @regu: pointer to the struct regulator
  56. * @irq: interrupt number that is used by gpadc
  57. */
  58. struct ab8500_gpadc {
  59. struct device *dev;
  60. struct list_head node;
  61. struct completion ab8500_gpadc_complete;
  62. struct mutex ab8500_gpadc_lock;
  63. struct regulator *regu;
  64. int irq;
  65. };
  66. static LIST_HEAD(ab8500_gpadc_list);
  67. /**
  68. * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
  69. * (i.e. the first GPADC in the instance list)
  70. */
  71. struct ab8500_gpadc *ab8500_gpadc_get(char *name)
  72. {
  73. struct ab8500_gpadc *gpadc;
  74. list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
  75. if (!strcmp(name, dev_name(gpadc->dev)))
  76. return gpadc;
  77. }
  78. return ERR_PTR(-ENOENT);
  79. }
  80. EXPORT_SYMBOL(ab8500_gpadc_get);
  81. /**
  82. * ab8500_gpadc_convert() - gpadc conversion
  83. * @input: analog input to be converted to digital data
  84. *
  85. * This function converts the selected analog i/p to digital
  86. * data. Thereafter calibration has to be made to obtain the
  87. * data in the required quantity measurement.
  88. */
  89. int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 input)
  90. {
  91. int ret;
  92. u16 data = 0;
  93. int looplimit = 0;
  94. u8 val, low_data, high_data;
  95. if (!gpadc)
  96. return -ENODEV;
  97. mutex_lock(&gpadc->ab8500_gpadc_lock);
  98. /* Enable VTVout LDO this is required for GPADC */
  99. regulator_enable(gpadc->regu);
  100. /* Check if ADC is not busy, lock and proceed */
  101. do {
  102. ret = abx500_get_register_interruptible(gpadc->dev,
  103. AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
  104. if (ret < 0)
  105. goto out;
  106. if (!(val & GPADC_BUSY))
  107. break;
  108. msleep(10);
  109. } while (++looplimit < 10);
  110. if (looplimit >= 10 && (val & GPADC_BUSY)) {
  111. dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
  112. ret = -EINVAL;
  113. goto out;
  114. }
  115. /* Enable GPADC */
  116. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  117. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
  118. if (ret < 0) {
  119. dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
  120. goto out;
  121. }
  122. /* Select the input source and set average samples to 16 */
  123. ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  124. AB8500_GPADC_CTRL2_REG, (input | SW_AVG_16));
  125. if (ret < 0) {
  126. dev_err(gpadc->dev,
  127. "gpadc_conversion: set avg samples failed\n");
  128. goto out;
  129. }
  130. /* Enable ADC, Buffering and select rising edge, start Conversion */
  131. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  132. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
  133. if (ret < 0) {
  134. dev_err(gpadc->dev,
  135. "gpadc_conversion: select falling edge failed\n");
  136. goto out;
  137. }
  138. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  139. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV);
  140. if (ret < 0) {
  141. dev_err(gpadc->dev,
  142. "gpadc_conversion: start s/w conversion failed\n");
  143. goto out;
  144. }
  145. /* wait for completion of conversion */
  146. if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, 2*HZ)) {
  147. dev_err(gpadc->dev,
  148. "timeout: didnt recieve GPADC conversion interrupt\n");
  149. ret = -EINVAL;
  150. goto out;
  151. }
  152. /* Read the converted RAW data */
  153. ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
  154. AB8500_GPADC_MANDATAL_REG, &low_data);
  155. if (ret < 0) {
  156. dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n");
  157. goto out;
  158. }
  159. ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
  160. AB8500_GPADC_MANDATAH_REG, &high_data);
  161. if (ret < 0) {
  162. dev_err(gpadc->dev,
  163. "gpadc_conversion: read high data failed\n");
  164. goto out;
  165. }
  166. data = (high_data << 8) | low_data;
  167. /* Disable GPADC */
  168. ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  169. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  170. if (ret < 0) {
  171. dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
  172. goto out;
  173. }
  174. /* Disable VTVout LDO this is required for GPADC */
  175. regulator_disable(gpadc->regu);
  176. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  177. return data;
  178. out:
  179. /*
  180. * It has shown to be needed to turn off the GPADC if an error occurs,
  181. * otherwise we might have problem when waiting for the busy bit in the
  182. * GPADC status register to go low. In V1.1 there wait_for_completion
  183. * seems to timeout when waiting for an interrupt.. Not seen in V2.0
  184. */
  185. (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  186. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  187. regulator_disable(gpadc->regu);
  188. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  189. dev_err(gpadc->dev,
  190. "gpadc_conversion: Failed to AD convert channel %d\n", input);
  191. return ret;
  192. }
  193. EXPORT_SYMBOL(ab8500_gpadc_convert);
  194. /**
  195. * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion
  196. * @irq: irq number
  197. * @data: pointer to the data passed during request irq
  198. *
  199. * This is a interrupt service routine for s/w gpadc conversion completion.
  200. * Notifies the gpadc completion is completed and the converted raw value
  201. * can be read from the registers.
  202. * Returns IRQ status(IRQ_HANDLED)
  203. */
  204. static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_gpadc)
  205. {
  206. struct ab8500_gpadc *gpadc = _gpadc;
  207. complete(&gpadc->ab8500_gpadc_complete);
  208. return IRQ_HANDLED;
  209. }
  210. static int __devinit ab8500_gpadc_probe(struct platform_device *pdev)
  211. {
  212. int ret = 0;
  213. struct ab8500_gpadc *gpadc;
  214. gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
  215. if (!gpadc) {
  216. dev_err(&pdev->dev, "Error: No memory\n");
  217. return -ENOMEM;
  218. }
  219. gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END");
  220. if (gpadc->irq < 0) {
  221. dev_err(gpadc->dev, "failed to get platform irq-%d\n",
  222. gpadc->irq);
  223. ret = gpadc->irq;
  224. goto fail;
  225. }
  226. gpadc->dev = &pdev->dev;
  227. mutex_init(&gpadc->ab8500_gpadc_lock);
  228. /* Initialize completion used to notify completion of conversion */
  229. init_completion(&gpadc->ab8500_gpadc_complete);
  230. /* Register interrupt - SwAdcComplete */
  231. ret = request_threaded_irq(gpadc->irq, NULL,
  232. ab8500_bm_gpswadcconvend_handler,
  233. IRQF_NO_SUSPEND | IRQF_SHARED, "ab8500-gpadc", gpadc);
  234. if (ret < 0) {
  235. dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
  236. gpadc->irq);
  237. goto fail;
  238. }
  239. /* VTVout LDO used to power up ab8500-GPADC */
  240. gpadc->regu = regulator_get(&pdev->dev, "vddadc");
  241. if (IS_ERR(gpadc->regu)) {
  242. ret = PTR_ERR(gpadc->regu);
  243. dev_err(gpadc->dev, "failed to get vtvout LDO\n");
  244. goto fail;
  245. }
  246. list_add_tail(&gpadc->node, &ab8500_gpadc_list);
  247. dev_dbg(gpadc->dev, "probe success\n");
  248. return 0;
  249. fail:
  250. kfree(gpadc);
  251. gpadc = NULL;
  252. return ret;
  253. }
  254. static int __devexit ab8500_gpadc_remove(struct platform_device *pdev)
  255. {
  256. struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
  257. /* remove this gpadc entry from the list */
  258. list_del(&gpadc->node);
  259. /* remove interrupt - completion of Sw ADC conversion */
  260. free_irq(gpadc->irq, gpadc);
  261. /* disable VTVout LDO that is being used by GPADC */
  262. regulator_put(gpadc->regu);
  263. kfree(gpadc);
  264. gpadc = NULL;
  265. return 0;
  266. }
  267. static struct platform_driver ab8500_gpadc_driver = {
  268. .probe = ab8500_gpadc_probe,
  269. .remove = __devexit_p(ab8500_gpadc_remove),
  270. .driver = {
  271. .name = "ab8500-gpadc",
  272. .owner = THIS_MODULE,
  273. },
  274. };
  275. static int __init ab8500_gpadc_init(void)
  276. {
  277. return platform_driver_register(&ab8500_gpadc_driver);
  278. }
  279. static void __exit ab8500_gpadc_exit(void)
  280. {
  281. platform_driver_unregister(&ab8500_gpadc_driver);
  282. }
  283. subsys_initcall_sync(ab8500_gpadc_init);
  284. module_exit(ab8500_gpadc_exit);
  285. MODULE_LICENSE("GPL v2");
  286. MODULE_AUTHOR("Arun R Murthy, Daniel Willerud");
  287. MODULE_ALIAS("platform:ab8500_gpadc");
  288. MODULE_DESCRIPTION("AB8500 GPADC driver");