1 From 0ba56db361ac905ff2e2d4e6288206c73e3df523 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 24 Apr 2010 12:18:01 +0200
4 Subject: [PATCH] Add jz4740 adc driver
7 drivers/misc/Kconfig | 11 ++
8 drivers/misc/Makefile | 1 +
9 drivers/misc/jz4740-adc.c | 410 ++++++++++++++++++++++++++++++++++++++++++++
10 include/linux/jz4740-adc.h | 25 +++
11 4 files changed, 447 insertions(+), 0 deletions(-)
12 create mode 100644 drivers/misc/jz4740-adc.c
13 create mode 100644 include/linux/jz4740-adc.h
15 diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
16 index 0d0d625..c62f615 100644
17 --- a/drivers/misc/Kconfig
18 +++ b/drivers/misc/Kconfig
19 @@ -327,6 +327,17 @@ config VMWARE_BALLOON
20 To compile this driver as a module, choose M here: the
21 module will be called vmware_balloon.
24 + tristate "Ingenic JZ4720/JZ4740 SoC ADC driver"
25 + depends on SOC_JZ4740
27 + If you say yes here you get support for the Ingenic JZ4720/JZ4740 SoC ADC
28 + core. It is required for the JZ4720/JZ4740 battery and touchscreen driver
29 + and is used to synchronize access to the adc core between those two.
31 + This driver can also be build as a module. If so, the module will be
34 source "drivers/misc/c2port/Kconfig"
35 source "drivers/misc/eeprom/Kconfig"
36 source "drivers/misc/cb710/Kconfig"
37 diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
38 index 7b6f7ee..506bcf6 100644
39 --- a/drivers/misc/Makefile
40 +++ b/drivers/misc/Makefile
41 @@ -27,6 +27,7 @@ obj-$(CONFIG_DS1682) += ds1682.o
42 obj-$(CONFIG_TI_DAC7512) += ti_dac7512.o
43 obj-$(CONFIG_C2PORT) += c2port/
44 obj-$(CONFIG_IWMC3200TOP) += iwmc3200top/
45 +obj-$(CONFIG_JZ4740_ADC) += jz4740-adc.o
48 obj-$(CONFIG_VMWARE_BALLOON) += vmware_balloon.o
49 diff --git a/drivers/misc/jz4740-adc.c b/drivers/misc/jz4740-adc.c
51 index 0000000..a8a735a
53 +++ b/drivers/misc/jz4740-adc.c
56 + * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
57 + * JZ4720/JZ4740 SoC ADC driver
59 + * This program is free software; you can redistribute it and/or modify it
60 + * under the terms of the GNU General Public License as published by the
61 + * Free Software Foundation; either version 2 of the License, or (at your
62 + * option) any later version.
64 + * You should have received a copy of the GNU General Public License along
65 + * with this program; if not, write to the Free Software Foundation, Inc.,
66 + * 675 Mass Ave, Cambridge, MA 02139, USA.
68 + * This driver is meant to synchronize access to the adc core for the battery
69 + * and touchscreen driver. Thus these drivers should use the adc driver as a
73 +#include <linux/err.h>
74 +#include <linux/kernel.h>
75 +#include <linux/module.h>
76 +#include <linux/platform_device.h>
77 +#include <linux/slab.h>
78 +#include <linux/spinlock.h>
79 +#include <linux/interrupt.h>
81 +#include <linux/clk.h>
83 +#include <linux/jz4740-adc.h>
85 +#define JZ_REG_ADC_ENABLE 0x00
86 +#define JZ_REG_ADC_CFG 0x04
87 +#define JZ_REG_ADC_CTRL 0x08
88 +#define JZ_REG_ADC_STATUS 0x0C
89 +#define JZ_REG_ADC_SAME 0x10
90 +#define JZ_REG_ADC_WAIT 0x14
91 +#define JZ_REG_ADC_TOUCH 0x18
92 +#define JZ_REG_ADC_BATTERY 0x1C
93 +#define JZ_REG_ADC_ADCIN 0x20
95 +#define JZ_ADC_ENABLE_TOUCH BIT(2)
96 +#define JZ_ADC_ENABLE_BATTERY BIT(1)
97 +#define JZ_ADC_ENABLE_ADCIN BIT(0)
99 +#define JZ_ADC_CFG_SPZZ BIT(31)
100 +#define JZ_ADC_CFG_EX_IN BIT(30)
101 +#define JZ_ADC_CFG_DNUM_MASK (0x7 << 16)
102 +#define JZ_ADC_CFG_DMA_ENABLE BIT(15)
103 +#define JZ_ADC_CFG_XYZ_MASK (0x2 << 13)
104 +#define JZ_ADC_CFG_SAMPLE_NUM_MASK (0x7 << 10)
105 +#define JZ_ADC_CFG_CLKDIV (0xf << 5)
106 +#define JZ_ADC_CFG_BAT_MB BIT(4)
108 +#define JZ_ADC_CFG_DNUM_OFFSET 16
109 +#define JZ_ADC_CFG_XYZ_OFFSET 13
110 +#define JZ_ADC_CFG_SAMPLE_NUM_OFFSET 10
111 +#define JZ_ADC_CFG_CLKDIV_OFFSET 5
113 +#define JZ_ADC_IRQ_PENDOWN BIT(4)
114 +#define JZ_ADC_IRQ_PENUP BIT(3)
115 +#define JZ_ADC_IRQ_TOUCH BIT(2)
116 +#define JZ_ADC_IRQ_BATTERY BIT(1)
117 +#define JZ_ADC_IRQ_ADCIN BIT(0)
119 +#define JZ_ADC_TOUCH_TYPE1 BIT(31)
120 +#define JZ_ADC_TOUCH_DATA1_MASK 0xfff
121 +#define JZ_ADC_TOUCH_TYPE0 BIT(15)
122 +#define JZ_ADC_TOUCH_DATA0_MASK 0xfff
124 +#define JZ_ADC_BATTERY_MASK 0xfff
126 +#define JZ_ADC_ADCIN_MASK 0xfff
129 + struct resource *mem;
130 + void __iomem *base;
135 + unsigned int clk_ref;
137 + struct completion bat_completion;
138 + struct completion adc_completion;
143 +static irqreturn_t jz4740_adc_irq(int irq, void *data)
145 + struct jz4740_adc *adc = data;
148 + status = readb(adc->base + JZ_REG_ADC_STATUS);
150 + if (status & JZ_ADC_IRQ_BATTERY)
151 + complete(&adc->bat_completion);
152 + if (status & JZ_ADC_IRQ_ADCIN)
153 + complete(&adc->adc_completion);
155 + writeb(0xff, adc->base + JZ_REG_ADC_STATUS);
157 + return IRQ_HANDLED;
160 +static void jz4740_adc_enable_irq(struct jz4740_adc *adc, int irq)
162 + unsigned long flags;
165 + spin_lock_irqsave(&adc->lock, flags);
167 + val = readb(adc->base + JZ_REG_ADC_CTRL);
169 + writeb(val, adc->base + JZ_REG_ADC_CTRL);
171 + spin_unlock_irqrestore(&adc->lock, flags);
174 +static void jz4740_adc_disable_irq(struct jz4740_adc *adc, int irq)
176 + unsigned long flags;
179 + spin_lock_irqsave(&adc->lock, flags);
181 + val = readb(adc->base + JZ_REG_ADC_CTRL);
183 + writeb(val, adc->base + JZ_REG_ADC_CTRL);
185 + spin_unlock_irqrestore(&adc->lock, flags);
188 +static void jz4740_adc_enable_adc(struct jz4740_adc *adc, int engine)
190 + unsigned long flags;
193 + spin_lock_irqsave(&adc->lock, flags);
195 + val = readb(adc->base + JZ_REG_ADC_ENABLE);
197 + writeb(val, adc->base + JZ_REG_ADC_ENABLE);
199 + spin_unlock_irqrestore(&adc->lock, flags);
202 +static void jz4740_adc_disable_adc(struct jz4740_adc *adc, int engine)
204 + unsigned long flags;
207 + spin_lock_irqsave(&adc->lock, flags);
209 + val = readb(adc->base + JZ_REG_ADC_ENABLE);
211 + writeb(val, adc->base + JZ_REG_ADC_ENABLE);
213 + spin_unlock_irqrestore(&adc->lock, flags);
216 +static inline void jz4740_adc_set_cfg(struct jz4740_adc *adc, uint32_t mask,
219 + unsigned long flags;
222 + spin_lock_irqsave(&adc->lock, flags);
224 + cfg = readl(adc->base + JZ_REG_ADC_CFG);
229 + writel(cfg, adc->base + JZ_REG_ADC_CFG);
231 + spin_unlock_irqrestore(&adc->lock, flags);
234 +static inline void jz4740_adc_clk_enable(struct jz4740_adc *adc)
236 + unsigned long flags;
238 + spin_lock_irqsave(&adc->lock, flags);
239 + if (adc->clk_ref++ == 0)
240 + clk_enable(adc->clk);
241 + spin_unlock_irqrestore(&adc->lock, flags);
244 +static inline void jz4740_adc_clk_disable(struct jz4740_adc *adc)
246 + unsigned long flags;
248 + spin_lock_irqsave(&adc->lock, flags);
249 + if (--adc->clk_ref == 0)
250 + clk_disable(adc->clk);
251 + spin_unlock_irqrestore(&adc->lock, flags);
254 +long jz4740_adc_read_battery_voltage(struct device *dev,
255 + enum jz_adc_battery_scale scale)
257 + struct jz4740_adc *adc = dev_get_drvdata(dev);
265 + jz4740_adc_clk_enable(adc);
267 + if (scale == JZ_ADC_BATTERY_SCALE_2V5)
268 + jz4740_adc_set_cfg(adc, JZ_ADC_CFG_BAT_MB, JZ_ADC_CFG_BAT_MB);
270 + jz4740_adc_set_cfg(adc, JZ_ADC_CFG_BAT_MB, 0);
272 + jz4740_adc_enable_irq(adc, JZ_ADC_IRQ_BATTERY);
273 + jz4740_adc_enable_adc(adc, JZ_ADC_ENABLE_BATTERY);
275 + t = wait_for_completion_interruptible_timeout(&adc->bat_completion,
278 + jz4740_adc_disable_irq(adc, JZ_ADC_IRQ_BATTERY);
281 + jz4740_adc_disable_adc(adc, JZ_ADC_ENABLE_BATTERY);
282 + return t ? t : -ETIMEDOUT;
285 + val = readw(adc->base + JZ_REG_ADC_BATTERY);
287 + jz4740_adc_clk_disable(adc);
289 + if (scale == JZ_ADC_BATTERY_SCALE_2V5)
290 + voltage = (((long long)val) * 2500000LL) >> 12LL;
292 + voltage = ((((long long)val) * 7395000LL) >> 12LL) + 33000LL;
296 +EXPORT_SYMBOL_GPL(jz4740_adc_read_battery_voltage);
298 +static ssize_t jz4740_adc_read_adcin(struct device *dev,
299 + struct device_attribute *dev_attr,
302 + struct jz4740_adc *adc = dev_get_drvdata(dev);
306 + jz4740_adc_clk_enable(adc);
308 + jz4740_adc_enable_irq(adc, JZ_ADC_IRQ_ADCIN);
309 + jz4740_adc_enable_adc(adc, JZ_ADC_ENABLE_ADCIN);
311 + t = wait_for_completion_interruptible_timeout(&adc->adc_completion,
314 + jz4740_adc_disable_irq(adc, JZ_ADC_IRQ_ADCIN);
317 + jz4740_adc_disable_adc(adc, JZ_ADC_ENABLE_ADCIN);
318 + return t ? t : -ETIMEDOUT;
321 + val = readw(adc->base + JZ_REG_ADC_ADCIN);
322 + jz4740_adc_clk_disable(adc);
324 + return sprintf(buf, "%d\n", val);
327 +static DEVICE_ATTR(adcin, S_IRUGO, jz4740_adc_read_adcin, NULL);
329 +static int __devinit jz4740_adc_probe(struct platform_device *pdev)
332 + struct jz4740_adc *adc;
334 + adc = kmalloc(sizeof(*adc), GFP_KERNEL);
336 + adc->irq = platform_get_irq(pdev, 0);
338 + if (adc->irq < 0) {
340 + dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
344 + adc->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
348 + dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
352 + adc->mem = request_mem_region(adc->mem->start, resource_size(adc->mem),
357 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
361 + adc->base = ioremap_nocache(adc->mem->start, resource_size(adc->mem));
365 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
366 + goto err_release_mem_region;
369 + adc->clk = clk_get(&pdev->dev, "adc");
371 + if (IS_ERR(adc->clk)) {
372 + ret = PTR_ERR(adc->clk);
373 + dev_err(&pdev->dev, "Failed to get clock: %d\n", ret);
377 + init_completion(&adc->bat_completion);
378 + init_completion(&adc->adc_completion);
380 + spin_lock_init(&adc->lock);
384 + platform_set_drvdata(pdev, adc);
386 + ret = request_irq(adc->irq, jz4740_adc_irq, 0, pdev->name, adc);
389 + dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
393 + ret = device_create_file(&pdev->dev, &dev_attr_adcin);
395 + dev_err(&pdev->dev, "Failed to create sysfs file: %d\n", ret);
399 + writeb(0x00, adc->base + JZ_REG_ADC_ENABLE);
400 + writeb(0xff, adc->base + JZ_REG_ADC_CTRL);
405 + free_irq(adc->irq, adc);
409 + platform_set_drvdata(pdev, NULL);
410 + iounmap(adc->base);
411 +err_release_mem_region:
412 + release_mem_region(adc->mem->start, resource_size(adc->mem));
419 +static int __devexit jz4740_adc_remove(struct platform_device *pdev)
421 + struct jz4740_adc *adc = platform_get_drvdata(pdev);
423 + device_remove_file(&pdev->dev, &dev_attr_adcin);
425 + free_irq(adc->irq, adc);
427 + iounmap(adc->base);
428 + release_mem_region(adc->mem->start, resource_size(adc->mem));
432 + platform_set_drvdata(pdev, NULL);
439 +struct platform_driver jz4740_adc_driver = {
440 + .probe = jz4740_adc_probe,
441 + .remove = __devexit_p(jz4740_adc_remove),
443 + .name = "jz4740-adc",
444 + .owner = THIS_MODULE,
448 +static int __init jz4740_adc_init(void)
450 + return platform_driver_register(&jz4740_adc_driver);
452 +module_init(jz4740_adc_init);
454 +static void __exit jz4740_adc_exit(void)
456 + platform_driver_unregister(&jz4740_adc_driver);
458 +module_exit(jz4740_adc_exit);
460 +MODULE_DESCRIPTION("JZ4720/JZ4740 SoC ADC driver");
461 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
462 +MODULE_LICENSE("GPL");
463 +MODULE_ALIAS("platform:jz4740-adc");
464 +MODULE_ALIAS("platform:jz4720-adc");
465 diff --git a/include/linux/jz4740-adc.h b/include/linux/jz4740-adc.h
467 index 0000000..59cfe63
469 +++ b/include/linux/jz4740-adc.h
472 +#ifndef __LINUX_JZ4740_ADC
473 +#define __LINUX_JZ4740_ADC
475 +#include <linux/device.h>
477 +enum jz_adc_battery_scale {
478 + JZ_ADC_BATTERY_SCALE_2V5, /* Mesures voltages up to 2.5V */
479 + JZ_ADC_BATTERY_SCALE_7V5, /* Mesures voltages up to 7.5V */
483 + * jz4740_adc_read_battery_voltage - Read battery voltage from the ADC PBAT pin
484 + * @dev: Pointer to a jz4740-adc device
485 + * @scale: Whether to use 2.5V or 7.5V scale
487 + * Returns: Battery voltage in mircovolts
491 +long jz4740_adc_read_battery_voltage(struct device *dev,
492 + enum jz_adc_battery_scale scale);