jz4740-adc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. dev_err(&pdev->dev, "Failed to allocate driver structure\n");
  174. return -ENOMEM;
  175. }
  176. adc->irq = platform_get_irq(pdev, 0);
  177. if (adc->irq < 0) {
  178. ret = adc->irq;
  179. dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
  180. return ret;
  181. }
  182. irq_base = platform_get_irq(pdev, 1);
  183. if (irq_base < 0) {
  184. dev_err(&pdev->dev, "Failed to get irq base: %d\n", irq_base);
  185. return irq_base;
  186. }
  187. mem_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  188. if (!mem_base) {
  189. dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
  190. return -ENOENT;
  191. }
  192. /* Only request the shared registers for the MFD driver */
  193. adc->mem = request_mem_region(mem_base->start, JZ_REG_ADC_STATUS,
  194. pdev->name);
  195. if (!adc->mem) {
  196. dev_err(&pdev->dev, "Failed to request mmio memory region\n");
  197. return -EBUSY;
  198. }
  199. adc->base = ioremap_nocache(adc->mem->start, resource_size(adc->mem));
  200. if (!adc->base) {
  201. ret = -EBUSY;
  202. dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
  203. goto err_release_mem_region;
  204. }
  205. adc->clk = clk_get(&pdev->dev, "adc");
  206. if (IS_ERR(adc->clk)) {
  207. ret = PTR_ERR(adc->clk);
  208. dev_err(&pdev->dev, "Failed to get clock: %d\n", ret);
  209. goto err_iounmap;
  210. }
  211. spin_lock_init(&adc->lock);
  212. atomic_set(&adc->clk_ref, 0);
  213. platform_set_drvdata(pdev, adc);
  214. gc = irq_alloc_generic_chip("INTC", 1, irq_base, adc->base,
  215. handle_level_irq);
  216. ct = gc->chip_types;
  217. ct->regs.mask = JZ_REG_ADC_CTRL;
  218. ct->regs.ack = JZ_REG_ADC_STATUS;
  219. ct->chip.irq_mask = irq_gc_mask_set_bit;
  220. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  221. ct->chip.irq_ack = irq_gc_ack_set_bit;
  222. irq_setup_generic_chip(gc, IRQ_MSK(5), IRQ_GC_INIT_MASK_CACHE, 0,
  223. IRQ_NOPROBE | IRQ_LEVEL);
  224. adc->gc = gc;
  225. irq_set_chained_handler_and_data(adc->irq, jz4740_adc_irq_demux, gc);
  226. writeb(0x00, adc->base + JZ_REG_ADC_ENABLE);
  227. writeb(0xff, adc->base + JZ_REG_ADC_CTRL);
  228. ret = mfd_add_devices(&pdev->dev, 0, jz4740_adc_cells,
  229. ARRAY_SIZE(jz4740_adc_cells), mem_base,
  230. irq_base, NULL);
  231. if (ret < 0)
  232. goto err_clk_put;
  233. return 0;
  234. err_clk_put:
  235. clk_put(adc->clk);
  236. err_iounmap:
  237. iounmap(adc->base);
  238. err_release_mem_region:
  239. release_mem_region(adc->mem->start, resource_size(adc->mem));
  240. return ret;
  241. }
  242. static int jz4740_adc_remove(struct platform_device *pdev)
  243. {
  244. struct jz4740_adc *adc = platform_get_drvdata(pdev);
  245. mfd_remove_devices(&pdev->dev);
  246. irq_remove_generic_chip(adc->gc, IRQ_MSK(5), IRQ_NOPROBE | IRQ_LEVEL, 0);
  247. kfree(adc->gc);
  248. irq_set_chained_handler_and_data(adc->irq, NULL, NULL);
  249. iounmap(adc->base);
  250. release_mem_region(adc->mem->start, resource_size(adc->mem));
  251. clk_put(adc->clk);
  252. return 0;
  253. }
  254. static struct platform_driver jz4740_adc_driver = {
  255. .probe = jz4740_adc_probe,
  256. .remove = jz4740_adc_remove,
  257. .driver = {
  258. .name = "jz4740-adc",
  259. },
  260. };
  261. module_platform_driver(jz4740_adc_driver);
  262. MODULE_DESCRIPTION("JZ4740 SoC ADC driver");
  263. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  264. MODULE_LICENSE("GPL");
  265. MODULE_ALIAS("platform:jz4740-adc");