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>
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>
27 #include <linux/clk.h>
28 #include <linux/mfd/core.h>
30 #include <linux/jz4740-adc.h>
33 #define JZ_REG_ADC_ENABLE 0x00
34 #define JZ_REG_ADC_CFG 0x04
35 #define JZ_REG_ADC_CTRL 0x08
36 #define JZ_REG_ADC_STATUS 0x0c
38 #define JZ_REG_ADC_TOUCHSCREEN_BASE 0x10
39 #define JZ_REG_ADC_BATTERY_BASE 0x1c
40 #define JZ_REG_ADC_HWMON_BASE 0x20
42 #define JZ_ADC_ENABLE_TOUCH BIT(2)
43 #define JZ_ADC_ENABLE_BATTERY BIT(1)
44 #define JZ_ADC_ENABLE_ADCIN BIT(0)
67 static inline void jz4740_adc_irq_set_masked(struct jz4740_adc
*adc
, int irq
,
75 spin_lock_irqsave(&adc
->lock
, flags
);
77 val
= readb(adc
->base
+ JZ_REG_ADC_CTRL
);
82 writeb(val
, adc
->base
+ JZ_REG_ADC_CTRL
);
84 spin_unlock_irqrestore(&adc
->lock
, flags
);
87 static void jz4740_adc_irq_mask(struct irq_data
*data
)
89 struct jz4740_adc
*adc
= irq_data_get_irq_chip_data(data
);
90 jz4740_adc_irq_set_masked(adc
, data
->irq
, true);
93 static void jz4740_adc_irq_unmask(struct irq_data
*data
)
95 struct jz4740_adc
*adc
= irq_data_get_irq_chip_data(data
);
96 jz4740_adc_irq_set_masked(adc
, data
->irq
, false);
99 static void jz4740_adc_irq_ack(struct irq_data
*data
)
101 struct jz4740_adc
*adc
= irq_data_get_irq_chip_data(data
);
102 unsigned int irq
= data
->irq
- adc
->irq_base
;
103 writeb(BIT(irq
), adc
->base
+ JZ_REG_ADC_STATUS
);
106 static struct irq_chip jz4740_adc_irq_chip
= {
107 .name
= "jz4740-adc",
108 .irq_mask
= jz4740_adc_irq_mask
,
109 .irq_unmask
= jz4740_adc_irq_unmask
,
110 .irq_ack
= jz4740_adc_irq_ack
,
113 static void jz4740_adc_irq_demux(unsigned int irq
, struct irq_desc
*desc
)
115 struct jz4740_adc
*adc
= irq_desc_get_handler_data(desc
);
119 status
= readb(adc
->base
+ JZ_REG_ADC_STATUS
);
121 for (i
= 0; i
< 5; ++i
) {
123 generic_handle_irq(adc
->irq_base
+ i
);
128 /* Refcounting for the ADC clock is done in here instead of in the clock
129 * framework, because it is the only clock which is shared between multiple
130 * devices and thus is the only clock which needs refcounting */
131 static inline void jz4740_adc_clk_enable(struct jz4740_adc
*adc
)
133 if (atomic_inc_return(&adc
->clk_ref
) == 1)
134 clk_enable(adc
->clk
);
137 static inline void jz4740_adc_clk_disable(struct jz4740_adc
*adc
)
139 if (atomic_dec_return(&adc
->clk_ref
) == 0)
140 clk_disable(adc
->clk
);
143 static inline void jz4740_adc_set_enabled(struct jz4740_adc
*adc
, int engine
,
149 spin_lock_irqsave(&adc
->lock
, flags
);
151 val
= readb(adc
->base
+ JZ_REG_ADC_ENABLE
);
156 writeb(val
, adc
->base
+ JZ_REG_ADC_ENABLE
);
158 spin_unlock_irqrestore(&adc
->lock
, flags
);
161 static int jz4740_adc_cell_enable(struct platform_device
*pdev
)
163 struct jz4740_adc
*adc
= dev_get_drvdata(pdev
->dev
.parent
);
165 jz4740_adc_clk_enable(adc
);
166 jz4740_adc_set_enabled(adc
, pdev
->id
, true);
171 static int jz4740_adc_cell_disable(struct platform_device
*pdev
)
173 struct jz4740_adc
*adc
= dev_get_drvdata(pdev
->dev
.parent
);
175 jz4740_adc_set_enabled(adc
, pdev
->id
, false);
176 jz4740_adc_clk_disable(adc
);
181 int jz4740_adc_set_config(struct device
*dev
, uint32_t mask
, uint32_t val
)
183 struct jz4740_adc
*adc
= dev_get_drvdata(dev
);
190 spin_lock_irqsave(&adc
->lock
, flags
);
192 cfg
= readl(adc
->base
+ JZ_REG_ADC_CFG
);
197 writel(cfg
, adc
->base
+ JZ_REG_ADC_CFG
);
199 spin_unlock_irqrestore(&adc
->lock
, flags
);
203 EXPORT_SYMBOL_GPL(jz4740_adc_set_config
);
205 static struct resource jz4740_hwmon_resources
[] = {
207 .start
= JZ_ADC_IRQ_ADCIN
,
208 .flags
= IORESOURCE_IRQ
,
211 .start
= JZ_REG_ADC_HWMON_BASE
,
212 .end
= JZ_REG_ADC_HWMON_BASE
+ 3,
213 .flags
= IORESOURCE_MEM
,
217 static struct resource jz4740_battery_resources
[] = {
219 .start
= JZ_ADC_IRQ_BATTERY
,
220 .flags
= IORESOURCE_IRQ
,
223 .start
= JZ_REG_ADC_BATTERY_BASE
,
224 .end
= JZ_REG_ADC_BATTERY_BASE
+ 3,
225 .flags
= IORESOURCE_MEM
,
229 const struct mfd_cell jz4740_adc_cells
[] = {
232 .name
= "jz4740-hwmon",
233 .num_resources
= ARRAY_SIZE(jz4740_hwmon_resources
),
234 .resources
= jz4740_hwmon_resources
,
236 .enable
= jz4740_adc_cell_enable
,
237 .disable
= jz4740_adc_cell_disable
,
241 .name
= "jz4740-battery",
242 .num_resources
= ARRAY_SIZE(jz4740_battery_resources
),
243 .resources
= jz4740_battery_resources
,
245 .enable
= jz4740_adc_cell_enable
,
246 .disable
= jz4740_adc_cell_disable
,
250 static int __devinit
jz4740_adc_probe(struct platform_device
*pdev
)
253 struct jz4740_adc
*adc
;
254 struct resource
*mem_base
;
257 adc
= kmalloc(sizeof(*adc
), GFP_KERNEL
);
259 dev_err(&pdev
->dev
, "Failed to allocate driver structure\n");
263 adc
->irq
= platform_get_irq(pdev
, 0);
266 dev_err(&pdev
->dev
, "Failed to get platform irq: %d\n", ret
);
270 adc
->irq_base
= platform_get_irq(pdev
, 1);
271 if (adc
->irq_base
< 0) {
273 dev_err(&pdev
->dev
, "Failed to get irq base: %d\n", ret
);
277 mem_base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
280 dev_err(&pdev
->dev
, "Failed to get platform mmio resource\n");
284 /* Only request the shared registers for the MFD driver */
285 adc
->mem
= request_mem_region(mem_base
->start
, JZ_REG_ADC_STATUS
,
289 dev_err(&pdev
->dev
, "Failed to request mmio memory region\n");
293 adc
->base
= ioremap_nocache(adc
->mem
->start
, resource_size(adc
->mem
));
296 dev_err(&pdev
->dev
, "Failed to ioremap mmio memory\n");
297 goto err_release_mem_region
;
300 adc
->clk
= clk_get(&pdev
->dev
, "adc");
301 if (IS_ERR(adc
->clk
)) {
302 ret
= PTR_ERR(adc
->clk
);
303 dev_err(&pdev
->dev
, "Failed to get clock: %d\n", ret
);
307 spin_lock_init(&adc
->lock
);
308 atomic_set(&adc
->clk_ref
, 0);
310 platform_set_drvdata(pdev
, adc
);
312 for (irq
= adc
->irq_base
; irq
< adc
->irq_base
+ 5; ++irq
) {
313 irq_set_chip_data(irq
, adc
);
314 irq_set_chip_and_handler(irq
, &jz4740_adc_irq_chip
,
318 irq_set_handler_data(adc
->irq
, adc
);
319 irq_set_chained_handler(adc
->irq
, jz4740_adc_irq_demux
);
321 writeb(0x00, adc
->base
+ JZ_REG_ADC_ENABLE
);
322 writeb(0xff, adc
->base
+ JZ_REG_ADC_CTRL
);
324 ret
= mfd_add_devices(&pdev
->dev
, 0, jz4740_adc_cells
,
325 ARRAY_SIZE(jz4740_adc_cells
), mem_base
, adc
->irq_base
);
334 platform_set_drvdata(pdev
, NULL
);
336 err_release_mem_region
:
337 release_mem_region(adc
->mem
->start
, resource_size(adc
->mem
));
344 static int __devexit
jz4740_adc_remove(struct platform_device
*pdev
)
346 struct jz4740_adc
*adc
= platform_get_drvdata(pdev
);
348 mfd_remove_devices(&pdev
->dev
);
350 irq_set_handler_data(adc
->irq
, NULL
);
351 irq_set_chained_handler(adc
->irq
, NULL
);
354 release_mem_region(adc
->mem
->start
, resource_size(adc
->mem
));
358 platform_set_drvdata(pdev
, NULL
);
365 struct platform_driver jz4740_adc_driver
= {
366 .probe
= jz4740_adc_probe
,
367 .remove
= __devexit_p(jz4740_adc_remove
),
369 .name
= "jz4740-adc",
370 .owner
= THIS_MODULE
,
374 static int __init
jz4740_adc_init(void)
376 return platform_driver_register(&jz4740_adc_driver
);
378 module_init(jz4740_adc_init
);
380 static void __exit
jz4740_adc_exit(void)
382 platform_driver_unregister(&jz4740_adc_driver
);
384 module_exit(jz4740_adc_exit
);
386 MODULE_DESCRIPTION("JZ4740 SoC ADC driver");
387 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
388 MODULE_LICENSE("GPL");
389 MODULE_ALIAS("platform:jz4740-adc");