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