jz4740-adc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 SoC ADC driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. * This driver synchronizes access to the JZ4740 ADC core between the
  15. * JZ4740 battery and hwmon drivers.
  16. */
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/clk.h>
  27. #include <linux/mfd/core.h>
  28. #include <linux/jz4740-adc.h>
  29. #define JZ_REG_ADC_ENABLE 0x00
  30. #define JZ_REG_ADC_CFG 0x04
  31. #define JZ_REG_ADC_CTRL 0x08
  32. #define JZ_REG_ADC_STATUS 0x0c
  33. #define JZ_REG_ADC_TOUCHSCREEN_BASE 0x10
  34. #define JZ_REG_ADC_BATTERY_BASE 0x1c
  35. #define JZ_REG_ADC_HWMON_BASE 0x20
  36. #define JZ_ADC_ENABLE_TOUCH BIT(2)
  37. #define JZ_ADC_ENABLE_BATTERY BIT(1)
  38. #define JZ_ADC_ENABLE_ADCIN BIT(0)
  39. enum {
  40. JZ_ADC_IRQ_ADCIN = 0,
  41. JZ_ADC_IRQ_BATTERY,
  42. JZ_ADC_IRQ_TOUCH,
  43. JZ_ADC_IRQ_PENUP,
  44. JZ_ADC_IRQ_PENDOWN,
  45. };
  46. struct jz4740_adc {
  47. struct resource *mem;
  48. void __iomem *base;
  49. int irq;
  50. struct irq_chip_generic *gc;
  51. struct clk *clk;
  52. atomic_t clk_ref;
  53. spinlock_t lock;
  54. };
  55. static void jz4740_adc_irq_demux(struct irq_desc *desc)
  56. {
  57. struct irq_chip_generic *gc = irq_desc_get_handler_data(desc);
  58. uint8_t status;
  59. unsigned int i;
  60. status = readb(gc->reg_base + JZ_REG_ADC_STATUS);
  61. for (i = 0; i < 5; ++i) {
  62. if (status & BIT(i))
  63. generic_handle_irq(gc->irq_base + i);
  64. }
  65. }
  66. /* Refcounting for the ADC clock is done in here instead of in the clock
  67. * framework, because it is the only clock which is shared between multiple
  68. * devices and thus is the only clock which needs refcounting */
  69. static inline void jz4740_adc_clk_enable(struct jz4740_adc *adc)
  70. {
  71. if (atomic_inc_return(&adc->clk_ref) == 1)
  72. clk_prepare_enable(adc->clk);
  73. }
  74. static inline void jz4740_adc_clk_disable(struct jz4740_adc *adc)
  75. {
  76. if (atomic_dec_return(&adc->clk_ref) == 0)
  77. clk_disable_unprepare(adc->clk);
  78. }
  79. static inline void jz4740_adc_set_enabled(struct jz4740_adc *adc, int engine,
  80. bool enabled)
  81. {
  82. unsigned long flags;
  83. uint8_t val;
  84. spin_lock_irqsave(&adc->lock, flags);
  85. val = readb(adc->base + JZ_REG_ADC_ENABLE);
  86. if (enabled)
  87. val |= BIT(engine);
  88. else
  89. val &= ~BIT(engine);
  90. writeb(val, adc->base + JZ_REG_ADC_ENABLE);
  91. spin_unlock_irqrestore(&adc->lock, flags);
  92. }
  93. static int jz4740_adc_cell_enable(struct platform_device *pdev)
  94. {
  95. struct jz4740_adc *adc = dev_get_drvdata(pdev->dev.parent);
  96. jz4740_adc_clk_enable(adc);
  97. jz4740_adc_set_enabled(adc, pdev->id, true);
  98. return 0;
  99. }
  100. static int jz4740_adc_cell_disable(struct platform_device *pdev)
  101. {
  102. struct jz4740_adc *adc = dev_get_drvdata(pdev->dev.parent);
  103. jz4740_adc_set_enabled(adc, pdev->id, false);
  104. jz4740_adc_clk_disable(adc);
  105. return 0;
  106. }
  107. int jz4740_adc_set_config(struct device *dev, uint32_t mask, uint32_t val)
  108. {
  109. struct jz4740_adc *adc = dev_get_drvdata(dev);
  110. unsigned long flags;
  111. uint32_t cfg;
  112. if (!adc)
  113. return -ENODEV;
  114. spin_lock_irqsave(&adc->lock, flags);
  115. cfg = readl(adc->base + JZ_REG_ADC_CFG);
  116. cfg &= ~mask;
  117. cfg |= val;
  118. writel(cfg, adc->base + JZ_REG_ADC_CFG);
  119. spin_unlock_irqrestore(&adc->lock, flags);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(jz4740_adc_set_config);
  123. static struct resource jz4740_hwmon_resources[] = {
  124. {
  125. .start = JZ_ADC_IRQ_ADCIN,
  126. .flags = IORESOURCE_IRQ,
  127. },
  128. {
  129. .start = JZ_REG_ADC_HWMON_BASE,
  130. .end = JZ_REG_ADC_HWMON_BASE + 3,
  131. .flags = IORESOURCE_MEM,
  132. },
  133. };
  134. static struct resource jz4740_battery_resources[] = {
  135. {
  136. .start = JZ_ADC_IRQ_BATTERY,
  137. .flags = IORESOURCE_IRQ,
  138. },
  139. {
  140. .start = JZ_REG_ADC_BATTERY_BASE,
  141. .end = JZ_REG_ADC_BATTERY_BASE + 3,
  142. .flags = IORESOURCE_MEM,
  143. },
  144. };
  145. static const struct mfd_cell jz4740_adc_cells[] = {
  146. {
  147. .id = 0,
  148. .name = "jz4740-hwmon",
  149. .num_resources = ARRAY_SIZE(jz4740_hwmon_resources),
  150. .resources = jz4740_hwmon_resources,
  151. .enable = jz4740_adc_cell_enable,
  152. .disable = jz4740_adc_cell_disable,
  153. },
  154. {
  155. .id = 1,
  156. .name = "jz4740-battery",
  157. .num_resources = ARRAY_SIZE(jz4740_battery_resources),
  158. .resources = jz4740_battery_resources,
  159. .enable = jz4740_adc_cell_enable,
  160. .disable = jz4740_adc_cell_disable,
  161. },
  162. };
  163. static int jz4740_adc_probe(struct platform_device *pdev)
  164. {
  165. struct irq_chip_generic *gc;
  166. struct irq_chip_type *ct;
  167. struct jz4740_adc *adc;
  168. struct resource *mem_base;
  169. int ret;
  170. int irq_base;
  171. adc = devm_kzalloc(&pdev->dev, sizeof(*adc), GFP_KERNEL);
  172. if (!adc)
  173. return -ENOMEM;
  174. adc->irq = platform_get_irq(pdev, 0);
  175. if (adc->irq < 0) {
  176. ret = adc->irq;
  177. dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
  178. return ret;
  179. }
  180. irq_base = platform_get_irq(pdev, 1);
  181. if (irq_base < 0) {
  182. dev_err(&pdev->dev, "Failed to get irq base: %d\n", irq_base);
  183. return irq_base;
  184. }
  185. mem_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  186. if (!mem_base) {
  187. dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
  188. return -ENOENT;
  189. }
  190. /* Only request the shared registers for the MFD driver */
  191. adc->mem = request_mem_region(mem_base->start, JZ_REG_ADC_STATUS,
  192. pdev->name);
  193. if (!adc->mem) {
  194. dev_err(&pdev->dev, "Failed to request mmio memory region\n");
  195. return -EBUSY;
  196. }
  197. adc->base = ioremap_nocache(adc->mem->start, resource_size(adc->mem));
  198. if (!adc->base) {
  199. ret = -EBUSY;
  200. dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
  201. goto err_release_mem_region;
  202. }
  203. adc->clk = clk_get(&pdev->dev, "adc");
  204. if (IS_ERR(adc->clk)) {
  205. ret = PTR_ERR(adc->clk);
  206. dev_err(&pdev->dev, "Failed to get clock: %d\n", ret);
  207. goto err_iounmap;
  208. }
  209. spin_lock_init(&adc->lock);
  210. atomic_set(&adc->clk_ref, 0);
  211. platform_set_drvdata(pdev, adc);
  212. gc = irq_alloc_generic_chip("INTC", 1, irq_base, adc->base,
  213. handle_level_irq);
  214. ct = gc->chip_types;
  215. ct->regs.mask = JZ_REG_ADC_CTRL;
  216. ct->regs.ack = JZ_REG_ADC_STATUS;
  217. ct->chip.irq_mask = irq_gc_mask_set_bit;
  218. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  219. ct->chip.irq_ack = irq_gc_ack_set_bit;
  220. irq_setup_generic_chip(gc, IRQ_MSK(5), IRQ_GC_INIT_MASK_CACHE, 0,
  221. IRQ_NOPROBE | IRQ_LEVEL);
  222. adc->gc = gc;
  223. irq_set_chained_handler_and_data(adc->irq, jz4740_adc_irq_demux, gc);
  224. writeb(0x00, adc->base + JZ_REG_ADC_ENABLE);
  225. writeb(0xff, adc->base + JZ_REG_ADC_CTRL);
  226. ret = mfd_add_devices(&pdev->dev, 0, jz4740_adc_cells,
  227. ARRAY_SIZE(jz4740_adc_cells), mem_base,
  228. irq_base, NULL);
  229. if (ret < 0)
  230. goto err_clk_put;
  231. return 0;
  232. err_clk_put:
  233. clk_put(adc->clk);
  234. err_iounmap:
  235. iounmap(adc->base);
  236. err_release_mem_region:
  237. release_mem_region(adc->mem->start, resource_size(adc->mem));
  238. return ret;
  239. }
  240. static int jz4740_adc_remove(struct platform_device *pdev)
  241. {
  242. struct jz4740_adc *adc = platform_get_drvdata(pdev);
  243. mfd_remove_devices(&pdev->dev);
  244. irq_remove_generic_chip(adc->gc, IRQ_MSK(5), IRQ_NOPROBE | IRQ_LEVEL, 0);
  245. kfree(adc->gc);
  246. irq_set_chained_handler_and_data(adc->irq, NULL, NULL);
  247. iounmap(adc->base);
  248. release_mem_region(adc->mem->start, resource_size(adc->mem));
  249. clk_put(adc->clk);
  250. return 0;
  251. }
  252. static struct platform_driver jz4740_adc_driver = {
  253. .probe = jz4740_adc_probe,
  254. .remove = jz4740_adc_remove,
  255. .driver = {
  256. .name = "jz4740-adc",
  257. },
  258. };
  259. module_platform_driver(jz4740_adc_driver);
  260. MODULE_DESCRIPTION("JZ4740 SoC ADC driver");
  261. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  262. MODULE_LICENSE("GPL");
  263. MODULE_ALIAS("platform:jz4740-adc");