2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * 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 synchronizes access to the JZ4740 ADC core between the
15 * JZ4740 battery and hwmon drivers.
18 #include <linux/err.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
28 #include <linux/clk.h>
29 #include <linux/mfd/core.h>
31 #include <linux/jz4740-adc.h>
34 #define JZ_REG_ADC_ENABLE 0x00
35 #define JZ_REG_ADC_CFG 0x04
36 #define JZ_REG_ADC_CTRL 0x08
37 #define JZ_REG_ADC_STATUS 0x0c
39 #define JZ_REG_ADC_TOUCHSCREEN_BASE 0x10
40 #define JZ_REG_ADC_BATTERY_BASE 0x1c
41 #define JZ_REG_ADC_HWMON_BASE 0x20
43 #define JZ_ADC_ENABLE_TOUCH BIT(2)
44 #define JZ_ADC_ENABLE_BATTERY BIT(1)
45 #define JZ_ADC_ENABLE_ADCIN BIT(0)
60 struct irq_chip_generic
*gc
;
68 static void jz4740_adc_irq_demux(unsigned int irq
, struct irq_desc
*desc
)
70 struct irq_chip_generic
*gc
= irq_desc_get_handler_data(desc
);
74 status
= readb(gc
->reg_base
+ JZ_REG_ADC_STATUS
);
76 for (i
= 0; i
< 5; ++i
) {
78 generic_handle_irq(gc
->irq_base
+ i
);
83 /* Refcounting for the ADC clock is done in here instead of in the clock
84 * framework, because it is the only clock which is shared between multiple
85 * devices and thus is the only clock which needs refcounting */
86 static inline void jz4740_adc_clk_enable(struct jz4740_adc
*adc
)
88 if (atomic_inc_return(&adc
->clk_ref
) == 1)
92 static inline void jz4740_adc_clk_disable(struct jz4740_adc
*adc
)
94 if (atomic_dec_return(&adc
->clk_ref
) == 0)
95 clk_disable(adc
->clk
);
98 static inline void jz4740_adc_set_enabled(struct jz4740_adc
*adc
, int engine
,
104 spin_lock_irqsave(&adc
->lock
, flags
);
106 val
= readb(adc
->base
+ JZ_REG_ADC_ENABLE
);
111 writeb(val
, adc
->base
+ JZ_REG_ADC_ENABLE
);
113 spin_unlock_irqrestore(&adc
->lock
, flags
);
116 static int jz4740_adc_cell_enable(struct platform_device
*pdev
)
118 struct jz4740_adc
*adc
= dev_get_drvdata(pdev
->dev
.parent
);
120 jz4740_adc_clk_enable(adc
);
121 jz4740_adc_set_enabled(adc
, pdev
->id
, true);
126 static int jz4740_adc_cell_disable(struct platform_device
*pdev
)
128 struct jz4740_adc
*adc
= dev_get_drvdata(pdev
->dev
.parent
);
130 jz4740_adc_set_enabled(adc
, pdev
->id
, false);
131 jz4740_adc_clk_disable(adc
);
136 int jz4740_adc_set_config(struct device
*dev
, uint32_t mask
, uint32_t val
)
138 struct jz4740_adc
*adc
= dev_get_drvdata(dev
);
145 spin_lock_irqsave(&adc
->lock
, flags
);
147 cfg
= readl(adc
->base
+ JZ_REG_ADC_CFG
);
152 writel(cfg
, adc
->base
+ JZ_REG_ADC_CFG
);
154 spin_unlock_irqrestore(&adc
->lock
, flags
);
158 EXPORT_SYMBOL_GPL(jz4740_adc_set_config
);
160 static struct resource jz4740_hwmon_resources
[] = {
162 .start
= JZ_ADC_IRQ_ADCIN
,
163 .flags
= IORESOURCE_IRQ
,
166 .start
= JZ_REG_ADC_HWMON_BASE
,
167 .end
= JZ_REG_ADC_HWMON_BASE
+ 3,
168 .flags
= IORESOURCE_MEM
,
172 static struct resource jz4740_battery_resources
[] = {
174 .start
= JZ_ADC_IRQ_BATTERY
,
175 .flags
= IORESOURCE_IRQ
,
178 .start
= JZ_REG_ADC_BATTERY_BASE
,
179 .end
= JZ_REG_ADC_BATTERY_BASE
+ 3,
180 .flags
= IORESOURCE_MEM
,
184 static struct mfd_cell jz4740_adc_cells
[] = {
187 .name
= "jz4740-hwmon",
188 .num_resources
= ARRAY_SIZE(jz4740_hwmon_resources
),
189 .resources
= jz4740_hwmon_resources
,
191 .enable
= jz4740_adc_cell_enable
,
192 .disable
= jz4740_adc_cell_disable
,
196 .name
= "jz4740-battery",
197 .num_resources
= ARRAY_SIZE(jz4740_battery_resources
),
198 .resources
= jz4740_battery_resources
,
200 .enable
= jz4740_adc_cell_enable
,
201 .disable
= jz4740_adc_cell_disable
,
205 static int __devinit
jz4740_adc_probe(struct platform_device
*pdev
)
207 struct irq_chip_generic
*gc
;
208 struct irq_chip_type
*ct
;
209 struct jz4740_adc
*adc
;
210 struct resource
*mem_base
;
214 adc
= kmalloc(sizeof(*adc
), GFP_KERNEL
);
216 dev_err(&pdev
->dev
, "Failed to allocate driver structure\n");
220 adc
->irq
= platform_get_irq(pdev
, 0);
223 dev_err(&pdev
->dev
, "Failed to get platform irq: %d\n", ret
);
227 irq_base
= platform_get_irq(pdev
, 1);
230 dev_err(&pdev
->dev
, "Failed to get irq base: %d\n", ret
);
234 mem_base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
237 dev_err(&pdev
->dev
, "Failed to get platform mmio resource\n");
241 /* Only request the shared registers for the MFD driver */
242 adc
->mem
= request_mem_region(mem_base
->start
, JZ_REG_ADC_STATUS
,
246 dev_err(&pdev
->dev
, "Failed to request mmio memory region\n");
250 adc
->base
= ioremap_nocache(adc
->mem
->start
, resource_size(adc
->mem
));
253 dev_err(&pdev
->dev
, "Failed to ioremap mmio memory\n");
254 goto err_release_mem_region
;
257 adc
->clk
= clk_get(&pdev
->dev
, "adc");
258 if (IS_ERR(adc
->clk
)) {
259 ret
= PTR_ERR(adc
->clk
);
260 dev_err(&pdev
->dev
, "Failed to get clock: %d\n", ret
);
264 spin_lock_init(&adc
->lock
);
265 atomic_set(&adc
->clk_ref
, 0);
267 platform_set_drvdata(pdev
, adc
);
269 gc
= irq_alloc_generic_chip("INTC", 1, irq_base
, adc
->base
,
273 ct
->regs
.mask
= JZ_REG_ADC_CTRL
;
274 ct
->regs
.ack
= JZ_REG_ADC_STATUS
;
275 ct
->chip
.irq_mask
= irq_gc_mask_set_bit
;
276 ct
->chip
.irq_unmask
= irq_gc_mask_clr_bit
;
277 ct
->chip
.irq_ack
= irq_gc_ack_set_bit
;
279 irq_setup_generic_chip(gc
, IRQ_MSK(5), 0, 0, IRQ_NOPROBE
| IRQ_LEVEL
);
283 irq_set_handler_data(adc
->irq
, gc
);
284 irq_set_chained_handler(adc
->irq
, jz4740_adc_irq_demux
);
286 writeb(0x00, adc
->base
+ JZ_REG_ADC_ENABLE
);
287 writeb(0xff, adc
->base
+ JZ_REG_ADC_CTRL
);
289 ret
= mfd_add_devices(&pdev
->dev
, 0, jz4740_adc_cells
,
290 ARRAY_SIZE(jz4740_adc_cells
), mem_base
,
300 platform_set_drvdata(pdev
, NULL
);
302 err_release_mem_region
:
303 release_mem_region(adc
->mem
->start
, resource_size(adc
->mem
));
310 static int __devexit
jz4740_adc_remove(struct platform_device
*pdev
)
312 struct jz4740_adc
*adc
= platform_get_drvdata(pdev
);
314 mfd_remove_devices(&pdev
->dev
);
316 irq_remove_generic_chip(adc
->gc
, IRQ_MSK(5), IRQ_NOPROBE
| IRQ_LEVEL
, 0);
318 irq_set_handler_data(adc
->irq
, NULL
);
319 irq_set_chained_handler(adc
->irq
, NULL
);
322 release_mem_region(adc
->mem
->start
, resource_size(adc
->mem
));
326 platform_set_drvdata(pdev
, NULL
);
333 static struct platform_driver jz4740_adc_driver
= {
334 .probe
= jz4740_adc_probe
,
335 .remove
= __devexit_p(jz4740_adc_remove
),
337 .name
= "jz4740-adc",
338 .owner
= THIS_MODULE
,
342 module_platform_driver(jz4740_adc_driver
);
344 MODULE_DESCRIPTION("JZ4740 SoC ADC driver");
345 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
346 MODULE_LICENSE("GPL");
347 MODULE_ALIAS("platform:jz4740-adc");