powerpc/pmac/smp: Fix 32-bit PowerMac cpu_die
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mfd / htc-pasic3.c
blobfb9770b39a3253edcc92a83a3e77fb020b72b93f
1 /*
2 * Core driver for HTC PASIC3 LED/DS1WM chip.
4 * Copyright (C) 2006 Philipp Zabel <philipp.zabel@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 */
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
15 #include <linux/gpio.h>
16 #include <linux/io.h>
17 #include <linux/irq.h>
18 #include <linux/interrupt.h>
19 #include <linux/mfd/core.h>
20 #include <linux/mfd/ds1wm.h>
21 #include <linux/mfd/htc-pasic3.h>
22 #include <linux/slab.h>
24 struct pasic3_data {
25 void __iomem *mapping;
26 unsigned int bus_shift;
29 #define REG_ADDR 5
30 #define REG_DATA 6
32 #define READ_MODE 0x80
35 * write to a secondary register on the PASIC3
37 void pasic3_write_register(struct device *dev, u32 reg, u8 val)
39 struct pasic3_data *asic = dev_get_drvdata(dev);
40 int bus_shift = asic->bus_shift;
41 void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
42 void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
44 __raw_writeb(~READ_MODE & reg, addr);
45 __raw_writeb(val, data);
47 EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */
50 * read from a secondary register on the PASIC3
52 u8 pasic3_read_register(struct device *dev, u32 reg)
54 struct pasic3_data *asic = dev_get_drvdata(dev);
55 int bus_shift = asic->bus_shift;
56 void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
57 void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
59 __raw_writeb(READ_MODE | reg, addr);
60 return __raw_readb(data);
62 EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */
65 * LEDs
68 static struct mfd_cell led_cell __initdata = {
69 .name = "leds-pasic3",
73 * DS1WM
76 static int ds1wm_enable(struct platform_device *pdev)
78 struct device *dev = pdev->dev.parent;
79 int c;
81 c = pasic3_read_register(dev, 0x28);
82 pasic3_write_register(dev, 0x28, c & 0x7f);
84 dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f);
85 return 0;
88 static int ds1wm_disable(struct platform_device *pdev)
90 struct device *dev = pdev->dev.parent;
91 int c;
93 c = pasic3_read_register(dev, 0x28);
94 pasic3_write_register(dev, 0x28, c | 0x80);
96 dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80);
97 return 0;
100 static struct ds1wm_driver_data ds1wm_pdata = {
101 .active_high = 0,
104 static struct resource ds1wm_resources[] __initdata = {
105 [0] = {
106 .start = 0,
107 .flags = IORESOURCE_MEM,
109 [1] = {
110 .start = 0,
111 .end = 0,
112 .flags = IORESOURCE_IRQ,
116 static struct mfd_cell ds1wm_cell __initdata = {
117 .name = "ds1wm",
118 .enable = ds1wm_enable,
119 .disable = ds1wm_disable,
120 .mfd_data = &ds1wm_pdata,
121 .num_resources = 2,
122 .resources = ds1wm_resources,
125 static int __init pasic3_probe(struct platform_device *pdev)
127 struct pasic3_platform_data *pdata = pdev->dev.platform_data;
128 struct device *dev = &pdev->dev;
129 struct pasic3_data *asic;
130 struct resource *r;
131 int ret;
132 int irq = 0;
134 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
135 if (r) {
136 ds1wm_resources[1].flags = IORESOURCE_IRQ | (r->flags &
137 (IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE));
138 irq = r->start;
141 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 if (!r)
143 return -ENXIO;
145 if (!request_mem_region(r->start, resource_size(r), "pasic3"))
146 return -EBUSY;
148 asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL);
149 if (!asic)
150 return -ENOMEM;
152 platform_set_drvdata(pdev, asic);
154 asic->mapping = ioremap(r->start, resource_size(r));
155 if (!asic->mapping) {
156 dev_err(dev, "couldn't ioremap PASIC3\n");
157 kfree(asic);
158 return -ENOMEM;
161 /* calculate bus shift from mem resource */
162 asic->bus_shift = (resource_size(r) - 5) >> 3;
164 if (pdata && pdata->clock_rate) {
165 ds1wm_pdata.clock_rate = pdata->clock_rate;
166 /* the first 5 PASIC3 registers control the DS1WM */
167 ds1wm_resources[0].end = (5 << asic->bus_shift) - 1;
168 ret = mfd_add_devices(&pdev->dev, pdev->id,
169 &ds1wm_cell, 1, r, irq);
170 if (ret < 0)
171 dev_warn(dev, "failed to register DS1WM\n");
174 if (pdata && pdata->led_pdata) {
175 ret = mfd_add_devices(&pdev->dev, pdev->id, &led_cell, 1, r, 0);
176 if (ret < 0)
177 dev_warn(dev, "failed to register LED device\n");
180 return 0;
183 static int pasic3_remove(struct platform_device *pdev)
185 struct pasic3_data *asic = platform_get_drvdata(pdev);
186 struct resource *r;
188 mfd_remove_devices(&pdev->dev);
190 iounmap(asic->mapping);
191 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192 release_mem_region(r->start, resource_size(r));
193 kfree(asic);
194 return 0;
197 MODULE_ALIAS("platform:pasic3");
199 static struct platform_driver pasic3_driver = {
200 .driver = {
201 .name = "pasic3",
203 .remove = pasic3_remove,
206 static int __init pasic3_base_init(void)
208 return platform_driver_probe(&pasic3_driver, pasic3_probe);
211 static void __exit pasic3_base_exit(void)
213 platform_driver_unregister(&pasic3_driver);
216 module_init(pasic3_base_init);
217 module_exit(pasic3_base_exit);
219 MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
220 MODULE_DESCRIPTION("Core driver for HTC PASIC3");
221 MODULE_LICENSE("GPL");