]>
Commit | Line | Data |
---|---|---|
5dc3339a PZ |
1 | /* |
2 | * Core driver for HTC PASIC3 LED/DS1WM chip. | |
3 | * | |
4 | * Copyright (C) 2006 Philipp Zabel <philipp.zabel@gmail.com> | |
5 | * | |
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 | */ | |
10 | ||
11 | #include <linux/init.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/platform_device.h> | |
14 | ||
5dc3339a PZ |
15 | #include <linux/gpio.h> |
16 | #include <linux/io.h> | |
17 | #include <linux/irq.h> | |
18 | #include <linux/interrupt.h> | |
0254a8f4 PZ |
19 | #include <linux/mfd/core.h> |
20 | #include <linux/mfd/ds1wm.h> | |
5dc3339a | 21 | #include <linux/mfd/htc-pasic3.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
5dc3339a | 23 | |
5dc3339a PZ |
24 | struct pasic3_data { |
25 | void __iomem *mapping; | |
26 | unsigned int bus_shift; | |
5dc3339a PZ |
27 | }; |
28 | ||
29 | #define REG_ADDR 5 | |
30 | #define REG_DATA 6 | |
5dc3339a PZ |
31 | |
32 | #define READ_MODE 0x80 | |
33 | ||
34 | /* | |
35 | * write to a secondary register on the PASIC3 | |
36 | */ | |
37 | void pasic3_write_register(struct device *dev, u32 reg, u8 val) | |
38 | { | |
1902a9e6 | 39 | struct pasic3_data *asic = dev_get_drvdata(dev); |
5dc3339a PZ |
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); | |
43 | ||
44 | __raw_writeb(~READ_MODE & reg, addr); | |
45 | __raw_writeb(val, data); | |
46 | } | |
47 | EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */ | |
48 | ||
49 | /* | |
50 | * read from a secondary register on the PASIC3 | |
51 | */ | |
52 | u8 pasic3_read_register(struct device *dev, u32 reg) | |
53 | { | |
1902a9e6 | 54 | struct pasic3_data *asic = dev_get_drvdata(dev); |
5dc3339a PZ |
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); | |
58 | ||
59 | __raw_writeb(READ_MODE | reg, addr); | |
60 | return __raw_readb(data); | |
61 | } | |
62 | EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */ | |
63 | ||
64 | /* | |
65 | * LEDs | |
66 | */ | |
67 | ||
0254a8f4 PZ |
68 | static struct mfd_cell led_cell __initdata = { |
69 | .name = "leds-pasic3", | |
70 | }; | |
5dc3339a PZ |
71 | |
72 | /* | |
73 | * DS1WM | |
74 | */ | |
75 | ||
0254a8f4 | 76 | static int ds1wm_enable(struct platform_device *pdev) |
5dc3339a PZ |
77 | { |
78 | struct device *dev = pdev->dev.parent; | |
79 | int c; | |
80 | ||
81 | c = pasic3_read_register(dev, 0x28); | |
82 | pasic3_write_register(dev, 0x28, c & 0x7f); | |
83 | ||
84 | dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f); | |
0254a8f4 | 85 | return 0; |
5dc3339a PZ |
86 | } |
87 | ||
0254a8f4 | 88 | static int ds1wm_disable(struct platform_device *pdev) |
5dc3339a PZ |
89 | { |
90 | struct device *dev = pdev->dev.parent; | |
91 | int c; | |
92 | ||
93 | c = pasic3_read_register(dev, 0x28); | |
94 | pasic3_write_register(dev, 0x28, c | 0x80); | |
95 | ||
96 | dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80); | |
0254a8f4 | 97 | return 0; |
5dc3339a PZ |
98 | } |
99 | ||
0254a8f4 PZ |
100 | static struct ds1wm_driver_data ds1wm_pdata = { |
101 | .active_high = 0, | |
f607e7fc | 102 | .reset_recover_delay = 1, |
5dc3339a PZ |
103 | }; |
104 | ||
0254a8f4 PZ |
105 | static struct resource ds1wm_resources[] __initdata = { |
106 | [0] = { | |
107 | .start = 0, | |
108 | .flags = IORESOURCE_MEM, | |
109 | }, | |
110 | [1] = { | |
111 | .start = 0, | |
112 | .end = 0, | |
113 | .flags = IORESOURCE_IRQ, | |
114 | }, | |
115 | }; | |
5dc3339a | 116 | |
0254a8f4 PZ |
117 | static struct mfd_cell ds1wm_cell __initdata = { |
118 | .name = "ds1wm", | |
119 | .enable = ds1wm_enable, | |
120 | .disable = ds1wm_disable, | |
121ea573 SO |
121 | .platform_data = &ds1wm_pdata, |
122 | .pdata_size = sizeof(ds1wm_pdata), | |
0254a8f4 PZ |
123 | .num_resources = 2, |
124 | .resources = ds1wm_resources, | |
125 | }; | |
5dc3339a PZ |
126 | |
127 | static int __init pasic3_probe(struct platform_device *pdev) | |
128 | { | |
129 | struct pasic3_platform_data *pdata = pdev->dev.platform_data; | |
130 | struct device *dev = &pdev->dev; | |
131 | struct pasic3_data *asic; | |
132 | struct resource *r; | |
133 | int ret; | |
0254a8f4 PZ |
134 | int irq = 0; |
135 | ||
136 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
137 | if (r) { | |
138 | ds1wm_resources[1].flags = IORESOURCE_IRQ | (r->flags & | |
139 | (IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE)); | |
140 | irq = r->start; | |
141 | } | |
142 | ||
5dc3339a PZ |
143 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
144 | if (!r) | |
145 | return -ENXIO; | |
146 | ||
0254a8f4 | 147 | if (!request_mem_region(r->start, resource_size(r), "pasic3")) |
5dc3339a PZ |
148 | return -EBUSY; |
149 | ||
150 | asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL); | |
151 | if (!asic) | |
152 | return -ENOMEM; | |
153 | ||
154 | platform_set_drvdata(pdev, asic); | |
155 | ||
0254a8f4 | 156 | asic->mapping = ioremap(r->start, resource_size(r)); |
5dc3339a PZ |
157 | if (!asic->mapping) { |
158 | dev_err(dev, "couldn't ioremap PASIC3\n"); | |
159 | kfree(asic); | |
160 | return -ENOMEM; | |
161 | } | |
162 | ||
0254a8f4 PZ |
163 | /* calculate bus shift from mem resource */ |
164 | asic->bus_shift = (resource_size(r) - 5) >> 3; | |
165 | ||
47c10edd PZ |
166 | if (pdata && pdata->clock_rate) { |
167 | ds1wm_pdata.clock_rate = pdata->clock_rate; | |
168 | /* the first 5 PASIC3 registers control the DS1WM */ | |
169 | ds1wm_resources[0].end = (5 << asic->bus_shift) - 1; | |
47c10edd | 170 | ret = mfd_add_devices(&pdev->dev, pdev->id, |
0848c94f | 171 | &ds1wm_cell, 1, r, irq, NULL); |
47c10edd PZ |
172 | if (ret < 0) |
173 | dev_warn(dev, "failed to register DS1WM\n"); | |
174 | } | |
175 | ||
176 | if (pdata && pdata->led_pdata) { | |
8ac93bea SO |
177 | led_cell.platform_data = pdata->led_pdata; |
178 | led_cell.pdata_size = sizeof(struct pasic3_leds_machinfo); | |
0848c94f MB |
179 | ret = mfd_add_devices(&pdev->dev, pdev->id, &led_cell, 1, r, |
180 | 0, NULL); | |
5dc3339a PZ |
181 | if (ret < 0) |
182 | dev_warn(dev, "failed to register LED device\n"); | |
183 | } | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | static int pasic3_remove(struct platform_device *pdev) | |
189 | { | |
190 | struct pasic3_data *asic = platform_get_drvdata(pdev); | |
191 | struct resource *r; | |
192 | ||
0254a8f4 | 193 | mfd_remove_devices(&pdev->dev); |
5dc3339a PZ |
194 | |
195 | iounmap(asic->mapping); | |
196 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
0254a8f4 | 197 | release_mem_region(r->start, resource_size(r)); |
5dc3339a PZ |
198 | kfree(asic); |
199 | return 0; | |
200 | } | |
201 | ||
4f46d6e7 KS |
202 | MODULE_ALIAS("platform:pasic3"); |
203 | ||
5dc3339a PZ |
204 | static struct platform_driver pasic3_driver = { |
205 | .driver = { | |
206 | .name = "pasic3", | |
207 | }, | |
208 | .remove = pasic3_remove, | |
209 | }; | |
210 | ||
211 | static int __init pasic3_base_init(void) | |
212 | { | |
213 | return platform_driver_probe(&pasic3_driver, pasic3_probe); | |
214 | } | |
215 | ||
216 | static void __exit pasic3_base_exit(void) | |
217 | { | |
218 | platform_driver_unregister(&pasic3_driver); | |
219 | } | |
220 | ||
221 | module_init(pasic3_base_init); | |
222 | module_exit(pasic3_base_exit); | |
223 | ||
224 | MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>"); | |
225 | MODULE_DESCRIPTION("Core driver for HTC PASIC3"); | |
226 | MODULE_LICENSE("GPL"); |