2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4720/JZ4740 SoC ADC driver
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.
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.
14 * This driver is meant to synchronize access to the adc core for the battery
15 * and touchscreen driver. Thus these drivers should use the adc driver as a
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/jz4740-adc.h>
28 #define JZ_REG_ADC_ENABLE 0x00
29 #define JZ_REG_ADC_CFG 0x04
30 #define JZ_REG_ADC_CTRL 0x08
31 #define JZ_REG_ADC_STATUS 0x0C
32 #define JZ_REG_ADC_SAME 0x10
33 #define JZ_REG_ADC_WAIT 0x14
34 #define JZ_REG_ADC_TOUCH 0x18
35 #define JZ_REG_ADC_BATTERY 0x1C
36 #define JZ_REG_ADC_ADCIN 0x20
38 #define JZ_ADC_ENABLE_TOUCH BIT(2)
39 #define JZ_ADC_ENABLE_BATTERY BIT(1)
40 #define JZ_ADC_ENABLE_ADCIN BIT(0)
42 #define JZ_ADC_CFG_SPZZ BIT(31)
43 #define JZ_ADC_CFG_EX_IN BIT(30)
44 #define JZ_ADC_CFG_DNUM_MASK (0x7 << 16)
45 #define JZ_ADC_CFG_DMA_ENABLE BIT(15)
46 #define JZ_ADC_CFG_XYZ_MASK (0x2 << 13)
47 #define JZ_ADC_CFG_SAMPLE_NUM_MASK (0x7 << 10)
48 #define JZ_ADC_CFG_CLKDIV (0xf << 5)
49 #define JZ_ADC_CFG_BAT_MB BIT(4)
51 #define JZ_ADC_CFG_DNUM_OFFSET 16
52 #define JZ_ADC_CFG_XYZ_OFFSET 13
53 #define JZ_ADC_CFG_SAMPLE_NUM_OFFSET 10
54 #define JZ_ADC_CFG_CLKDIV_OFFSET 5
56 #define JZ_ADC_IRQ_PENDOWN BIT(4)
57 #define JZ_ADC_IRQ_PENUP BIT(3)
58 #define JZ_ADC_IRQ_TOUCH BIT(2)
59 #define JZ_ADC_IRQ_BATTERY BIT(1)
60 #define JZ_ADC_IRQ_ADCIN BIT(0)
62 #define JZ_ADC_TOUCH_TYPE1 BIT(31)
63 #define JZ_ADC_TOUCH_DATA1_MASK 0xfff
64 #define JZ_ADC_TOUCH_TYPE0 BIT(15)
65 #define JZ_ADC_TOUCH_DATA0_MASK 0xfff
67 #define JZ_ADC_BATTERY_MASK 0xfff
69 #define JZ_ADC_ADCIN_MASK 0xfff
80 struct completion bat_completion
;
81 struct completion adc_completion
;
86 static irqreturn_t
jz4740_adc_irq(int irq
, void *data
)
88 struct jz4740_adc
*adc
= data
;
91 status
= readb(adc
->base
+ JZ_REG_ADC_STATUS
);
93 if (status
& JZ_ADC_IRQ_BATTERY
)
94 complete(&adc
->bat_completion
);
95 if (status
& JZ_ADC_IRQ_ADCIN
)
96 complete(&adc
->adc_completion
);
98 writeb(0xff, adc
->base
+ JZ_REG_ADC_STATUS
);
103 static void jz4740_adc_enable_irq(struct jz4740_adc
*adc
, int irq
)
108 spin_lock_irqsave(&adc
->lock
, flags
);
110 val
= readb(adc
->base
+ JZ_REG_ADC_CTRL
);
112 writeb(val
, adc
->base
+ JZ_REG_ADC_CTRL
);
114 spin_unlock_irqrestore(&adc
->lock
, flags
);
117 static void jz4740_adc_disable_irq(struct jz4740_adc
*adc
, int irq
)
122 spin_lock_irqsave(&adc
->lock
, flags
);
124 val
= readb(adc
->base
+ JZ_REG_ADC_CTRL
);
126 writeb(val
, adc
->base
+ JZ_REG_ADC_CTRL
);
128 spin_unlock_irqrestore(&adc
->lock
, flags
);
131 static void jz4740_adc_enable_adc(struct jz4740_adc
*adc
, int engine
)
136 spin_lock_irqsave(&adc
->lock
, flags
);
138 val
= readb(adc
->base
+ JZ_REG_ADC_ENABLE
);
140 writeb(val
, adc
->base
+ JZ_REG_ADC_ENABLE
);
142 spin_unlock_irqrestore(&adc
->lock
, flags
);
145 static void jz4740_adc_disable_adc(struct jz4740_adc
*adc
, int engine
)
150 spin_lock_irqsave(&adc
->lock
, flags
);
152 val
= readb(adc
->base
+ JZ_REG_ADC_ENABLE
);
154 writeb(val
, adc
->base
+ JZ_REG_ADC_ENABLE
);
156 spin_unlock_irqrestore(&adc
->lock
, flags
);
159 static inline void jz4740_adc_set_cfg(struct jz4740_adc
*adc
, uint32_t mask
,
165 spin_lock_irqsave(&adc
->lock
, flags
);
167 cfg
= readl(adc
->base
+ JZ_REG_ADC_CFG
);
172 writel(cfg
, adc
->base
+ JZ_REG_ADC_CFG
);
174 spin_unlock_irqrestore(&adc
->lock
, flags
);
177 static inline void jz4740_adc_clk_enable(struct jz4740_adc
*adc
)
181 spin_lock_irqsave(&adc
->lock
, flags
);
182 if (adc
->clk_ref
++ == 0)
183 clk_enable(adc
->clk
);
184 spin_unlock_irqrestore(&adc
->lock
, flags
);
187 static inline void jz4740_adc_clk_disable(struct jz4740_adc
*adc
)
191 spin_lock_irqsave(&adc
->lock
, flags
);
192 if (--adc
->clk_ref
== 0)
193 clk_disable(adc
->clk
);
194 spin_unlock_irqrestore(&adc
->lock
, flags
);
197 long jz4740_adc_read_battery_voltage(struct device
*dev
,
198 enum jz_adc_battery_scale scale
)
200 struct jz4740_adc
*adc
= dev_get_drvdata(dev
);
208 jz4740_adc_clk_enable(adc
);
210 if (scale
== JZ_ADC_BATTERY_SCALE_2V5
)
211 jz4740_adc_set_cfg(adc
, JZ_ADC_CFG_BAT_MB
, JZ_ADC_CFG_BAT_MB
);
213 jz4740_adc_set_cfg(adc
, JZ_ADC_CFG_BAT_MB
, 0);
215 jz4740_adc_enable_irq(adc
, JZ_ADC_IRQ_BATTERY
);
216 jz4740_adc_enable_adc(adc
, JZ_ADC_ENABLE_BATTERY
);
218 t
= wait_for_completion_interruptible_timeout(&adc
->bat_completion
,
221 jz4740_adc_disable_irq(adc
, JZ_ADC_IRQ_BATTERY
);
224 jz4740_adc_disable_adc(adc
, JZ_ADC_ENABLE_BATTERY
);
225 return t
? t
: -ETIMEDOUT
;
228 val
= readw(adc
->base
+ JZ_REG_ADC_BATTERY
);
230 jz4740_adc_clk_disable(adc
);
232 if (scale
== JZ_ADC_BATTERY_SCALE_2V5
)
233 voltage
= (((long long)val
) * 2500000LL) >> 12LL;
235 voltage
= ((((long long)val
) * 7395000LL) >> 12LL) + 33000LL;
239 EXPORT_SYMBOL_GPL(jz4740_adc_read_battery_voltage
);
241 static ssize_t
jz4740_adc_read_adcin(struct device
*dev
,
242 struct device_attribute
*dev_attr
,
245 struct jz4740_adc
*adc
= dev_get_drvdata(dev
);
249 jz4740_adc_clk_enable(adc
);
251 jz4740_adc_enable_irq(adc
, JZ_ADC_IRQ_ADCIN
);
252 jz4740_adc_enable_adc(adc
, JZ_ADC_ENABLE_ADCIN
);
254 t
= wait_for_completion_interruptible_timeout(&adc
->adc_completion
,
257 jz4740_adc_disable_irq(adc
, JZ_ADC_IRQ_ADCIN
);
260 jz4740_adc_disable_adc(adc
, JZ_ADC_ENABLE_ADCIN
);
261 return t
? t
: -ETIMEDOUT
;
264 val
= readw(adc
->base
+ JZ_REG_ADC_ADCIN
);
265 jz4740_adc_clk_disable(adc
);
267 return sprintf(buf
, "%d\n", val
);
270 static DEVICE_ATTR(adcin
, S_IRUGO
, jz4740_adc_read_adcin
, NULL
);
272 static int __devinit
jz4740_adc_probe(struct platform_device
*pdev
)
275 struct jz4740_adc
*adc
;
277 adc
= kmalloc(sizeof(*adc
), GFP_KERNEL
);
279 adc
->irq
= platform_get_irq(pdev
, 0);
283 dev_err(&pdev
->dev
, "Failed to get platform irq: %d\n", ret
);
287 adc
->mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
291 dev_err(&pdev
->dev
, "Failed to get platform mmio resource\n");
295 adc
->mem
= request_mem_region(adc
->mem
->start
, resource_size(adc
->mem
),
300 dev_err(&pdev
->dev
, "Failed to request mmio memory region\n");
304 adc
->base
= ioremap_nocache(adc
->mem
->start
, resource_size(adc
->mem
));
308 dev_err(&pdev
->dev
, "Failed to ioremap mmio memory\n");
309 goto err_release_mem_region
;
312 adc
->clk
= clk_get(&pdev
->dev
, "adc");
314 if (IS_ERR(adc
->clk
)) {
315 ret
= PTR_ERR(adc
->clk
);
316 dev_err(&pdev
->dev
, "Failed to get clock: %d\n", ret
);
320 init_completion(&adc
->bat_completion
);
321 init_completion(&adc
->adc_completion
);
323 spin_lock_init(&adc
->lock
);
327 platform_set_drvdata(pdev
, adc
);
329 ret
= request_irq(adc
->irq
, jz4740_adc_irq
, 0, pdev
->name
, adc
);
332 dev_err(&pdev
->dev
, "Failed to request irq: %d\n", ret
);
336 ret
= device_create_file(&pdev
->dev
, &dev_attr_adcin
);
338 dev_err(&pdev
->dev
, "Failed to create sysfs file: %d\n", ret
);
342 writeb(0x00, adc
->base
+ JZ_REG_ADC_ENABLE
);
343 writeb(0xff, adc
->base
+ JZ_REG_ADC_CTRL
);
348 free_irq(adc
->irq
, adc
);
352 platform_set_drvdata(pdev
, NULL
);
354 err_release_mem_region
:
355 release_mem_region(adc
->mem
->start
, resource_size(adc
->mem
));
362 static int __devexit
jz4740_adc_remove(struct platform_device
*pdev
)
364 struct jz4740_adc
*adc
= platform_get_drvdata(pdev
);
366 device_remove_file(&pdev
->dev
, &dev_attr_adcin
);
368 free_irq(adc
->irq
, adc
);
371 release_mem_region(adc
->mem
->start
, resource_size(adc
->mem
));
375 platform_set_drvdata(pdev
, NULL
);
382 struct platform_driver jz4740_adc_driver
= {
383 .probe
= jz4740_adc_probe
,
384 .remove
= jz4740_adc_remove
,
386 .name
= "jz4740-adc",
387 .owner
= THIS_MODULE
,
391 static int __init
jz4740_adc_init(void)
393 return platform_driver_register(&jz4740_adc_driver
);
395 module_init(jz4740_adc_init
);
397 static void __exit
jz4740_adc_exit(void)
399 platform_driver_unregister(&jz4740_adc_driver
);
401 module_exit(jz4740_adc_exit
);
403 MODULE_DESCRIPTION("JZ4720/JZ4740 SoC ADC driver");
404 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
405 MODULE_LICENSE("GPL");
406 MODULE_ALIAS("platform:jz4740-adc");
407 MODULE_ALIAS("platform:jz4720-adc");