blob: 4edc120a6359490a816eca96f30dbbc3a5eda624 [file] [log] [blame]
Philipp Zabel5dc33392008-04-12 13:25:41 +01001/*
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
15#include <linux/ds1wm.h>
16#include <linux/gpio.h>
17#include <linux/io.h>
18#include <linux/irq.h>
19#include <linux/interrupt.h>
20#include <linux/mfd/htc-pasic3.h>
21
Philipp Zabel5dc33392008-04-12 13:25:41 +010022struct pasic3_data {
23 void __iomem *mapping;
24 unsigned int bus_shift;
25 struct platform_device *ds1wm_pdev;
26 struct platform_device *led_pdev;
27};
28
29#define REG_ADDR 5
30#define REG_DATA 6
Philipp Zabel5dc33392008-04-12 13:25:41 +010031
32#define READ_MODE 0x80
33
34/*
35 * write to a secondary register on the PASIC3
36 */
37void pasic3_write_register(struct device *dev, u32 reg, u8 val)
38{
39 struct pasic3_data *asic = dev->driver_data;
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}
47EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */
48
49/*
50 * read from a secondary register on the PASIC3
51 */
52u8 pasic3_read_register(struct device *dev, u32 reg)
53{
54 struct pasic3_data *asic = dev->driver_data;
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}
62EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */
63
64/*
65 * LEDs
66 */
67
68static int led_device_add(struct device *pasic3_dev,
69 const struct pasic3_leds_machinfo *pdata)
70{
71 struct pasic3_data *asic = pasic3_dev->driver_data;
72 struct platform_device *pdev;
73 int ret;
74
75 pdev = platform_device_alloc("pasic3-led", -1);
76 if (!pdev) {
77 dev_dbg(pasic3_dev, "failed to allocate LED platform device\n");
78 return -ENOMEM;
79 }
80
81 ret = platform_device_add_data(pdev, pdata,
82 sizeof(struct pasic3_leds_machinfo));
83 if (ret < 0) {
84 dev_dbg(pasic3_dev, "failed to add LED platform data\n");
85 goto exit_pdev_put;
86 }
87
88 pdev->dev.parent = pasic3_dev;
89 ret = platform_device_add(pdev);
90 if (ret < 0) {
91 dev_dbg(pasic3_dev, "failed to add LED platform device\n");
92 goto exit_pdev_put;
93 }
94
95 asic->led_pdev = pdev;
96 return 0;
97
98exit_pdev_put:
99 platform_device_put(pdev);
100 return ret;
101}
102
103/*
104 * DS1WM
105 */
106
107static void ds1wm_enable(struct platform_device *pdev)
108{
109 struct device *dev = pdev->dev.parent;
110 int c;
111
112 c = pasic3_read_register(dev, 0x28);
113 pasic3_write_register(dev, 0x28, c & 0x7f);
114
115 dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f);
116}
117
118static void ds1wm_disable(struct platform_device *pdev)
119{
120 struct device *dev = pdev->dev.parent;
121 int c;
122
123 c = pasic3_read_register(dev, 0x28);
124 pasic3_write_register(dev, 0x28, c | 0x80);
125
126 dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80);
127}
128
129static struct ds1wm_platform_data ds1wm_pdata = {
130 .bus_shift = 2,
131 .enable = ds1wm_enable,
132 .disable = ds1wm_disable,
133};
134
135static int ds1wm_device_add(struct device *pasic3_dev, int bus_shift)
136{
137 struct pasic3_data *asic = pasic3_dev->driver_data;
138 struct platform_device *pdev;
139 int ret;
140
141 pdev = platform_device_alloc("ds1wm", -1);
142 if (!pdev) {
143 dev_dbg(pasic3_dev, "failed to allocate DS1WM platform device\n");
144 return -ENOMEM;
145 }
146
147 ret = platform_device_add_resources(pdev, pdev->resource,
148 pdev->num_resources);
149 if (ret < 0) {
150 dev_dbg(pasic3_dev, "failed to add DS1WM resources\n");
151 goto exit_pdev_put;
152 }
153
154 ds1wm_pdata.bus_shift = asic->bus_shift;
155 ret = platform_device_add_data(pdev, &ds1wm_pdata,
156 sizeof(struct ds1wm_platform_data));
157 if (ret < 0) {
158 dev_dbg(pasic3_dev, "failed to add DS1WM platform data\n");
159 goto exit_pdev_put;
160 }
161
162 pdev->dev.parent = pasic3_dev;
163 ret = platform_device_add(pdev);
164 if (ret < 0) {
165 dev_dbg(pasic3_dev, "failed to add DS1WM platform device\n");
166 goto exit_pdev_put;
167 }
168
169 asic->ds1wm_pdev = pdev;
170 return 0;
171
172exit_pdev_put:
173 platform_device_put(pdev);
174 return ret;
175}
176
177static int __init pasic3_probe(struct platform_device *pdev)
178{
179 struct pasic3_platform_data *pdata = pdev->dev.platform_data;
180 struct device *dev = &pdev->dev;
181 struct pasic3_data *asic;
182 struct resource *r;
183 int ret;
184
185 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
186 if (!r)
187 return -ENXIO;
188
189 if (!request_mem_region(r->start, r->end - r->start + 1, "pasic3"))
190 return -EBUSY;
191
192 asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL);
193 if (!asic)
194 return -ENOMEM;
195
196 platform_set_drvdata(pdev, asic);
197
198 if (pdata && pdata->bus_shift)
199 asic->bus_shift = pdata->bus_shift;
200 else
201 asic->bus_shift = 2;
202
203 asic->mapping = ioremap(r->start, r->end - r->start + 1);
204 if (!asic->mapping) {
205 dev_err(dev, "couldn't ioremap PASIC3\n");
206 kfree(asic);
207 return -ENOMEM;
208 }
209
210 ret = ds1wm_device_add(dev, asic->bus_shift);
211 if (ret < 0)
212 dev_warn(dev, "failed to register DS1WM\n");
213
214 if (pdata->led_pdata) {
215 ret = led_device_add(dev, pdata->led_pdata);
216 if (ret < 0)
217 dev_warn(dev, "failed to register LED device\n");
218 }
219
220 return 0;
221}
222
223static int pasic3_remove(struct platform_device *pdev)
224{
225 struct pasic3_data *asic = platform_get_drvdata(pdev);
226 struct resource *r;
227
228 if (asic->led_pdev)
229 platform_device_unregister(asic->led_pdev);
230 if (asic->ds1wm_pdev)
231 platform_device_unregister(asic->ds1wm_pdev);
232
233 iounmap(asic->mapping);
234 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
235 release_mem_region(r->start, r->end - r->start + 1);
236 kfree(asic);
237 return 0;
238}
239
240static struct platform_driver pasic3_driver = {
241 .driver = {
242 .name = "pasic3",
243 },
244 .remove = pasic3_remove,
245};
246
247static int __init pasic3_base_init(void)
248{
249 return platform_driver_probe(&pasic3_driver, pasic3_probe);
250}
251
252static void __exit pasic3_base_exit(void)
253{
254 platform_driver_unregister(&pasic3_driver);
255}
256
257module_init(pasic3_base_init);
258module_exit(pasic3_base_exit);
259
260MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
261MODULE_DESCRIPTION("Core driver for HTC PASIC3");
262MODULE_LICENSE("GPL");